Simplify count_digits

This commit is contained in:
Victor Zverovich 2020-08-03 15:45:48 -07:00
parent 279d698e1b
commit e587adb4e9
2 changed files with 5 additions and 5 deletions

View File

@ -281,12 +281,12 @@ const uint64_t basic_data<T>::powers_of_10_64[] = {
10000000000000000000ULL};
template <typename T>
const uint32_t basic_data<T>::zero_or_powers_of_10_32[] = {0,
const uint32_t basic_data<T>::zero_or_powers_of_10_32[] = {0, 0,
FMT_POWERS_OF_10(1)};
template <typename T>
const uint64_t basic_data<T>::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.

View File

@ -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 <typename T = void> 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