forked from fmtlib/fmt
Simplify count_digits
This commit is contained in:
@ -281,12 +281,12 @@ const uint64_t basic_data<T>::powers_of_10_64[] = {
|
|||||||
10000000000000000000ULL};
|
10000000000000000000ULL};
|
||||||
|
|
||||||
template <typename T>
|
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)};
|
FMT_POWERS_OF_10(1)};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const uint64_t basic_data<T>::zero_or_powers_of_10_64[] = {
|
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};
|
10000000000000000000ULL};
|
||||||
|
|
||||||
// Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340.
|
// Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340.
|
||||||
|
@ -770,7 +770,7 @@ using uint32_or_64_or_128_t =
|
|||||||
|
|
||||||
// Static data is placed in this class template for the header-only config.
|
// Static data is placed in this class template for the header-only config.
|
||||||
template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
|
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 uint16_t bsr2log10[];
|
||||||
static const uint64_t powers_of_10_64[];
|
static const uint64_t powers_of_10_64[];
|
||||||
static const uint32_t zero_or_powers_of_10_32[];
|
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) {
|
inline int count_digits(uint64_t n) {
|
||||||
// https://github.com/fmtlib/format-benchmark/blob/master/digits10
|
// https://github.com/fmtlib/format-benchmark/blob/master/digits10
|
||||||
auto t = data::bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63];
|
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
|
#else
|
||||||
// Fallback version of count_digits used when __builtin_clz is not available.
|
// 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.
|
// Optional version of count_digits for better performance on 32-bit platforms.
|
||||||
inline int count_digits(uint32_t n) {
|
inline int count_digits(uint32_t n) {
|
||||||
auto t = data::bsr2log10[FMT_BUILTIN_CLZ(n | 1) ^ 31];
|
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
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user