Make round_direction a scoped enum

This commit is contained in:
Victor Zverovich 2019-12-21 09:24:42 -08:00
parent 162995fedd
commit b308159be5
2 changed files with 18 additions and 16 deletions

View File

@ -722,7 +722,7 @@ class bigint {
} }
}; };
enum round_direction { unknown, up, down }; enum class round_direction { unknown, up, down };
// Given the divisor (normally a power of 10), the remainder = v % divisor for // Given the divisor (normally a power of 10), the remainder = v % divisor for
// some number v and the error, returns whether v should be rounded up, down, or // some number v and the error, returns whether v should be rounded up, down, or
@ -735,13 +735,13 @@ inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder,
FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow. FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow.
// Round down if (remainder + error) * 2 <= divisor. // Round down if (remainder + error) * 2 <= divisor.
if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2) if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2)
return down; return round_direction::down;
// Round up if (remainder - error) * 2 >= divisor. // Round up if (remainder - error) * 2 >= divisor.
if (remainder >= error && if (remainder >= error &&
remainder - error >= divisor - (remainder - error)) { remainder - error >= divisor - (remainder - error)) {
return up; return round_direction::up;
} }
return unknown; return round_direction::unknown;
} }
namespace digits { namespace digits {
@ -857,8 +857,8 @@ struct fixed_handler {
if (precision > 0) return digits::more; if (precision > 0) return digits::more;
if (precision < 0) return digits::done; if (precision < 0) return digits::done;
auto dir = get_round_direction(divisor, remainder, error); auto dir = get_round_direction(divisor, remainder, error);
if (dir == unknown) return digits::error; if (dir == round_direction::unknown) return digits::error;
buf[size++] = dir == up ? '1' : '0'; buf[size++] = dir == round_direction::up ? '1' : '0';
return digits::done; return digits::done;
} }
@ -876,7 +876,8 @@ struct fixed_handler {
FMT_ASSERT(error == 1 && divisor > 2, ""); FMT_ASSERT(error == 1 && divisor > 2, "");
} }
auto dir = get_round_direction(divisor, remainder, error); auto dir = get_round_direction(divisor, remainder, error);
if (dir != up) return dir == down ? digits::done : digits::error; if (dir != round_direction::up)
return dir == round_direction::down ? digits::done : digits::error;
++buf[size - 1]; ++buf[size - 1];
for (int i = size - 1; i > 0 && buf[i] > '9'; --i) { for (int i = size - 1; i > 0 && buf[i] > '9'; --i) {
buf[i] = '0'; buf[i] = '0';

View File

@ -272,26 +272,27 @@ TEST(FPTest, GetCachedPower) {
} }
TEST(FPTest, GetRoundDirection) { TEST(FPTest, GetRoundDirection) {
using fmt::internal::round_direction;
using fmt::internal::get_round_direction; using fmt::internal::get_round_direction;
EXPECT_EQ(fmt::internal::down, get_round_direction(100, 50, 0)); EXPECT_EQ(round_direction::down, get_round_direction(100, 50, 0));
EXPECT_EQ(fmt::internal::up, get_round_direction(100, 51, 0)); EXPECT_EQ(round_direction::up, get_round_direction(100, 51, 0));
EXPECT_EQ(fmt::internal::down, get_round_direction(100, 40, 10)); EXPECT_EQ(round_direction::down, get_round_direction(100, 40, 10));
EXPECT_EQ(fmt::internal::up, get_round_direction(100, 60, 10)); EXPECT_EQ(round_direction::up, get_round_direction(100, 60, 10));
for (size_t i = 41; i < 60; ++i) for (size_t i = 41; i < 60; ++i)
EXPECT_EQ(fmt::internal::unknown, get_round_direction(100, i, 10)); EXPECT_EQ(round_direction::unknown, get_round_direction(100, i, 10));
uint64_t max = max_value<uint64_t>(); uint64_t max = max_value<uint64_t>();
EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure); EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure);
EXPECT_THROW(get_round_direction(100, 0, 100), assertion_failure); EXPECT_THROW(get_round_direction(100, 0, 100), assertion_failure);
EXPECT_THROW(get_round_direction(100, 0, 50), assertion_failure); EXPECT_THROW(get_round_direction(100, 0, 50), assertion_failure);
// Check that remainder + error doesn't overflow. // Check that remainder + error doesn't overflow.
EXPECT_EQ(fmt::internal::up, get_round_direction(max, max - 1, 2)); EXPECT_EQ(round_direction::up, get_round_direction(max, max - 1, 2));
// Check that 2 * (remainder + error) doesn't overflow. // Check that 2 * (remainder + error) doesn't overflow.
EXPECT_EQ(fmt::internal::unknown, EXPECT_EQ(round_direction::unknown,
get_round_direction(max, max / 2 + 1, max / 2)); get_round_direction(max, max / 2 + 1, max / 2));
// Check that remainder - error doesn't overflow. // Check that remainder - error doesn't overflow.
EXPECT_EQ(fmt::internal::unknown, get_round_direction(100, 40, 41)); EXPECT_EQ(round_direction::unknown, get_round_direction(100, 40, 41));
// Check that 2 * (remainder - error) doesn't overflow. // Check that 2 * (remainder - error) doesn't overflow.
EXPECT_EQ(fmt::internal::up, get_round_direction(max, max - 1, 1)); EXPECT_EQ(round_direction::up, get_round_direction(max, max - 1, 1));
} }
TEST(FPTest, FixedHandler) { TEST(FPTest, FixedHandler) {