From e587adb4e931a3770196ec32d28e95310c49ebfc Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 3 Aug 2020 15:45:48 -0700 Subject: [PATCH] Simplify count_digits --- include/fmt/format-inl.h | 4 ++-- include/fmt/format.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index a4134336..35bae2dc 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -281,12 +281,12 @@ const uint64_t basic_data::powers_of_10_64[] = { 10000000000000000000ULL}; template -const uint32_t basic_data::zero_or_powers_of_10_32[] = {0, +const uint32_t basic_data::zero_or_powers_of_10_32[] = {0, 0, FMT_POWERS_OF_10(1)}; template const uint64_t basic_data::zero_or_powers_of_10_64[] = { - 0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL), + 0, 0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL), 10000000000000000000ULL}; // Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340. diff --git a/include/fmt/format.h b/include/fmt/format.h index aaea683b..b52da26f 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -770,7 +770,7 @@ using uint32_or_64_or_128_t = // Static data is placed in this class template for the header-only config. template struct FMT_EXTERN_TEMPLATE_API basic_data { - // Maps the result of bsr(n) to ceil(log10(n)). + // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)). static const uint16_t bsr2log10[]; static const uint64_t powers_of_10_64[]; static const uint32_t zero_or_powers_of_10_32[]; @@ -803,7 +803,7 @@ struct data : basic_data<> {}; inline int count_digits(uint64_t n) { // https://github.com/fmtlib/format-benchmark/blob/master/digits10 auto t = data::bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63]; - return t - (n < data::zero_or_powers_of_10_64[t - 1]); + return t - (n < data::zero_or_powers_of_10_64[t]); } #else // Fallback version of count_digits used when __builtin_clz is not available. @@ -861,7 +861,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n); // Optional version of count_digits for better performance on 32-bit platforms. inline int count_digits(uint32_t n) { auto t = data::bsr2log10[FMT_BUILTIN_CLZ(n | 1) ^ 31]; - return t - (n < data::zero_or_powers_of_10_32[t - 1]); + return t - (n < data::zero_or_powers_of_10_32[t]); } #endif