From 8d9ab9673680aec487380c5ff7980103f4b25091 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 1 Aug 2020 17:53:45 -0700 Subject: [PATCH] Cut a few cycles from count_digits --- include/fmt/format.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index ceda63ec..1e8382c0 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -801,7 +801,7 @@ struct data : basic_data<> {}; inline int count_digits(uint64_t n) { // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 // 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; } #else @@ -859,7 +859,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n); #ifdef FMT_BUILTIN_CLZ // Optional version of count_digits for better performance on 32-bit platforms. 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; } #endif