diff --git a/include/fmt/format.h b/include/fmt/format.h index 4b233f72..90f83ca0 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1022,26 +1022,27 @@ FMT_CONSTEXPR inline int count_digits(uint128_t n) { } #endif -#ifdef FMT_BUILTIN_CLZLL // Returns the number of decimal digits in n. Leading zeros are not counted // except for n == 0 in which case count_digits returns 1. FMT_CONSTEXPR20 inline int count_digits(uint64_t n) { if (is_constant_evaluated()) { return count_digits_fallback(n); } +#ifdef FMT_BUILTIN_CLZLL // https://github.com/fmtlib/format-benchmark/blob/master/digits10 auto t = bsr2log10(FMT_BUILTIN_CLZLL(n | 1) ^ 63); return t - (n < data::zero_or_powers_of_10_64_new[t]); -} #else -// Fallback version of count_digits used when __builtin_clz is not available. -FMT_CONSTEXPR inline int count_digits(uint64_t n) { return count_digits_fallback(n); -} #endif +} // Counts the number of digits in n. BITS = log2(radix). -template FMT_CONSTEXPR int count_digits(UInt n) { +template FMT_CONSTEXPR int count_digits(UInt n) { +#ifdef FMT_BUILTIN_CLZ + if (num_bits() == 32) + return (FMT_BUILTIN_CLZ(static_cast(n) | 1) ^ 31) / BITS + 1; +#endif int num_digits = 0; do { ++num_digits;