Cut a few cycles from count_digits

This commit is contained in:
Victor Zverovich 2020-08-01 17:53:45 -07:00
parent 734344931f
commit 8d9ab96736

View File

@ -801,7 +801,7 @@ struct data : basic_data<> {};
inline int count_digits(uint64_t n) { inline int count_digits(uint64_t n) {
// Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
// and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits. // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12; int t = ((FMT_BUILTIN_CLZLL(n | 1) ^ 63) + 1) * 1233 >> 12;
return t - (n < data::zero_or_powers_of_10_64[t]) + 1; return t - (n < data::zero_or_powers_of_10_64[t]) + 1;
} }
#else #else
@ -859,7 +859,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n);
#ifdef FMT_BUILTIN_CLZ #ifdef FMT_BUILTIN_CLZ
// 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) {
int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12; int t = ((FMT_BUILTIN_CLZ(n | 1) ^ 31) + 1) * 1233 >> 12;
return t - (n < data::zero_or_powers_of_10_32[t]) + 1; return t - (n < data::zero_or_powers_of_10_32[t]) + 1;
} }
#endif #endif