diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 68975cd32599..f8fa77e89099 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -223,18 +223,20 @@ template constexpr int num_significand_bits() { } // A floating-point number f * pow(2, e). -struct fp { - uint64_t f; +template struct basic_fp { + F f; int e; - static constexpr const int num_significand_bits = bits::value; + static constexpr const int num_significand_bits = bits::value; - constexpr fp() : f(0), e(0) {} - constexpr fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {} + constexpr basic_fp() : f(0), e(0) {} + constexpr basic_fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {} // Constructs fp from an IEEE754 floating-point number. It is a template to // prevent compile errors on systems where n is not IEEE754. - template explicit FMT_CONSTEXPR fp(Float n) { assign(n); } + template explicit FMT_CONSTEXPR basic_fp(Float n) { + assign(n); + } template using is_supported = bool_constant::digits <= 64>; @@ -272,8 +274,11 @@ struct fp { } }; +using fp = basic_fp; + // Normalizes the value converted from double and multiplied by (1 << SHIFT). -template FMT_CONSTEXPR fp normalize(fp value) { +template +FMT_CONSTEXPR basic_fp normalize(basic_fp value) { // Handle subnormals. const uint64_t implicit_bit = 1ULL << num_significand_bits(); const auto shifted_implicit_bit = implicit_bit << SHIFT; @@ -289,7 +294,9 @@ template FMT_CONSTEXPR fp normalize(fp value) { return value; } -inline bool operator==(fp x, fp y) { return x.f == y.f && x.e == y.e; } +template inline bool operator==(basic_fp x, basic_fp y) { + return x.f == y.f && x.e == y.e; +} // Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking. FMT_CONSTEXPR inline uint64_t multiply(uint64_t lhs, uint64_t rhs) { diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 41f736c6b21d..72445a680b02 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -190,14 +190,14 @@ TEST(fp_test, get_cached_power) { using limits = std::numeric_limits; for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) { int dec_exp = 0; - auto fp = fmt::detail::get_cached_power(exp, dec_exp); - bigint exact, cache(fp.f); + auto power = fmt::detail::get_cached_power(exp, dec_exp); + bigint exact, cache(power.f); if (dec_exp >= 0) { exact.assign_pow10(dec_exp); - if (fp.e <= 0) - exact <<= -fp.e; + if (power.e <= 0) + exact <<= -power.e; else - cache <<= fp.e; + cache <<= power.e; exact.align(cache); cache.align(exact); auto exact_str = fmt::format("{}", exact); @@ -211,9 +211,9 @@ TEST(fp_test, get_cached_power) { EXPECT_EQ(diff, 0); } else { cache.assign_pow10(-dec_exp); - cache *= fp.f + 1; // Inexact check. + cache *= power.f + 1; // Inexact check. exact.assign(1); - exact <<= -fp.e; + exact <<= -power.e; exact.align(cache); auto exact_str = fmt::format("{}", exact); auto cache_str = fmt::format("{}", cache);