From 85ba2716394bdea77ac940a0a9d39b4c41966c50 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 10 Mar 2021 12:57:46 -0800 Subject: [PATCH] Implement 128-bit count_digits in terms of count_digits_fallback --- include/fmt/format.h | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 5d7a2e64..4b233f72 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1016,6 +1016,11 @@ template FMT_CONSTEXPR int count_digits_fallback(T n) { count += 4; } } +#if FMT_USE_INT128 +FMT_CONSTEXPR inline int count_digits(uint128_t n) { + return count_digits_fallback(n); +} +#endif #ifdef FMT_BUILTIN_CLZLL // Returns the number of decimal digits in n. Leading zeros are not counted @@ -1035,23 +1040,6 @@ FMT_CONSTEXPR inline int count_digits(uint64_t n) { } #endif -#if FMT_USE_INT128 -FMT_CONSTEXPR inline int count_digits(uint128_t n) { - int count = 1; - for (;;) { - // Integer division is slow so do it for a group of four digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - if (n < 10) return count; - if (n < 100) return count + 1; - if (n < 1000) return count + 2; - if (n < 10000) return count + 3; - n /= 10000U; - count += 4; - } -} -#endif - // Counts the number of digits in n. BITS = log2(radix). template FMT_CONSTEXPR int count_digits(UInt n) { int num_digits = 0;