From 6c025520aa8371eb40ac06e6d4569575d7e65802 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 23 Sep 2020 15:12:03 -0700 Subject: [PATCH] Test that max_k is correctly defined --- include/fmt/format-inl.h | 9 +++++---- test/format-impl-test.cc | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 1c13902a..e3817886 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1198,10 +1198,11 @@ inline fp operator*(fp x, fp y) { return {multiply(x.f, y.f), x.e + y.e + 64}; } // (binary) exponent satisfies `min_exponent <= c_k.e <= min_exponent + 28`. inline fp get_cached_power(int min_exponent, int& pow10_exponent) { const int shift = 32; - int index = static_cast(((min_exponent + fp::significand_size - 1) * - (data::log10_2_significand >> shift) + - ((int64_t(1) << shift) - 1)) // ceil - >> 32 // arithmetic shift + const auto significand = static_cast(data::log10_2_significand); + int index = static_cast( + ((min_exponent + fp::significand_size - 1) * (significand >> shift) + + ((int64_t(1) << shift) - 1)) // ceil + >> 32 // arithmetic shift ); // Decimal exponent of the first (smallest) cached power of 10. const int first_dec_exp = -348; diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index b0df24aa..dbb635bc 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -220,6 +220,19 @@ TEST(FPTest, GetCachedPower) { } } +TEST(FPTest, DragonboxMaxK) { + using fmt::detail::dragonbox::floor_log10_pow2; + using float_info = fmt::detail::dragonbox::float_info; + EXPECT_EQ(fmt::detail::const_check(float_info::max_k), + float_info::kappa - floor_log10_pow2(float_info::min_exponent - + float_info::significand_bits)); + using double_info = fmt::detail::dragonbox::float_info; + EXPECT_EQ( + fmt::detail::const_check(double_info::max_k), + double_info::kappa - floor_log10_pow2(double_info::min_exponent - + double_info::significand_bits)); +} + TEST(FPTest, GetRoundDirection) { using fmt::detail::get_round_direction; using fmt::detail::round_direction;