Use fmt's clz in zmij
diff --git a/include/fmt/format.h b/include/fmt/format.h index d990cac..d2d112c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h
@@ -183,6 +183,55 @@ # define FMT_ZMIJ_USE_SSE 0 #endif +FMT_BEGIN_NAMESPACE +namespace detail { + +// Returns the number of leading zero bits in x, which must be nonzero. +FMT_CONSTEXPR20 inline auto clz(uint32_t x) -> int { + FMT_ASSERT(x != 0, ""); + if (!is_constant_evaluated()) { + // GCC before 10 has the clz builtins but not __has_builtin. +#if FMT_HAS_BUILTIN(__builtin_clz) || defined(__GNUC__) + return __builtin_clz(x); +#elif FMT_MSC_VERSION + unsigned long r; + _BitScanReverse(&r, x); + return 31 ^ static_cast<int>(r); +#endif + } + int n = 32; + for (; x != 0; x >>= 1) --n; + return n; +} + +FMT_CONSTEXPR20 inline auto clz(uint64_t x) -> int { + FMT_ASSERT(x != 0, ""); + if (!is_constant_evaluated()) { +#if FMT_HAS_BUILTIN(__builtin_clzll) || defined(__GNUC__) + return __builtin_clzll(x); +#elif defined(_M_AMD64) && defined(__AVX2__) + // Use lzcnt only on AVX2-capable CPUs that have this BMI instruction. + return static_cast<int>(__lzcnt64(x)); +#elif defined(_M_AMD64) || defined(_M_ARM64) + unsigned long r; + _BitScanReverse64(&r, x); + return 63 ^ static_cast<int>(r); +#elif FMT_MSC_VERSION + unsigned long r; + if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32))) + return 31 ^ static_cast<int>(r); + _BitScanReverse(&r, static_cast<uint32_t>(x)); + return 63 ^ static_cast<int>(r); +#endif + } + int n = 64; + for (; x != 0; x >>= 1) --n; + return n; +} + +} // namespace detail +FMT_END_NAMESPACE + #ifndef FMT_MODULE # include <assert.h> // assert # include <float.h> // DBL_MANT_DIG, LDBL_MANT_DIG @@ -355,29 +404,7 @@ #endif } -inline FMT_CONSTEXPR20 auto clz(uint64_t x) noexcept -> int { - assert(x != 0); - if (!is_constant_evaluated()) { -#if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_ZMIJ_GNUC_BUILTINS - return __builtin_clzll(x); -#elif defined(_M_AMD64) && defined(__AVX2__) - // Use lzcnt only on AVX2-capable CPUs that have this BMI instruction. - return __lzcnt64(x); -#elif defined(_M_AMD64) || defined(_M_ARM64) - unsigned long idx; - _BitScanReverse64(&idx, x); - return 63 - idx; -#elif FMT_MSC_VERSION - unsigned long idx; - if (_BitScanReverse(&idx, uint32_t(x >> 32))) return 31 - idx; - _BitScanReverse(&idx, uint32_t(x)); - return 63 - idx; -#endif - } - int n = 64; - for (; x > 0; x >>= 1) --n; - return n; -} +using fmt::detail::clz; struct uint128 { uint64_t lo; @@ -1571,54 +1598,6 @@ namespace detail { -// __builtin_clz is broken in clang with Microsoft codegen: -// https://github.com/fmtlib/fmt/issues/519. -#if !FMT_MSC_VERSION -# if FMT_HAS_BUILTIN(__builtin_clz) || FMT_GCC_VERSION || FMT_ICC_VERSION -# define FMT_BUILTIN_CLZ(n) __builtin_clz(n) -# endif -# if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_GCC_VERSION || FMT_ICC_VERSION -# define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) -# endif -#endif - -// Some compilers masquerade as both MSVC and GCC but otherwise support -// __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the -// MSVC intrinsics if the clz and clzll builtins are not available. -#if FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL) -// Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning. -# ifndef __clang__ -# pragma intrinsic(_BitScanReverse) -# ifdef _WIN64 -# pragma intrinsic(_BitScanReverse64) -# endif -# endif - -inline auto clz(uint32_t x) -> int { - FMT_ASSERT(x != 0, ""); - unsigned long r = 0; - _BitScanReverse(&r, x); - return 31 ^ static_cast<int>(r); -} -# define FMT_BUILTIN_CLZ(n) detail::clz(n) - -inline auto clzll(uint64_t x) -> int { - FMT_ASSERT(x != 0, ""); - unsigned long r = 0; -# ifdef _WIN64 - _BitScanReverse64(&r, x); -# else - // Scan the high 32 bits. - if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32))) - return 63 ^ static_cast<int>(r + 32); - // Scan the low 32 bits. - _BitScanReverse(&r, static_cast<uint32_t>(x)); -# endif - return 63 ^ static_cast<int>(r); -} -# define FMT_BUILTIN_CLZLL(n) detail::clzll(n) -#endif // FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL) - FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) { ignore_unused(condition); #ifdef FMT_FUZZ @@ -1818,21 +1797,6 @@ return result; } -template <typename UInt> -FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int { - int lz = 0; - constexpr UInt msb_mask = static_cast<UInt>(1) << (num_bits<UInt>() - 1); - for (; (n & msb_mask) == 0; n <<= 1) lz++; - return lz; -} - -FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int { -#ifdef FMT_BUILTIN_CLZ - if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n); -#endif - return countl_zero_fallback(n); -} - FMT_INLINE void assume(bool condition) { (void)condition; #if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION @@ -2631,7 +2595,6 @@ count += 4; } } -#ifdef FMT_BUILTIN_CLZLL // It is a separate function rather than a part of count_digits to workaround // the lack of static constexpr in constexpr functions. inline auto do_count_digits(uint64_t n) -> int { @@ -2644,30 +2607,25 @@ 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20}; - auto t = bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63]; + auto t = bsr2log10[clz(n | 1) ^ 63]; static constexpr uint64_t zero_or_powers_of_10[] = { 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL), 10000000000000000000ULL}; return t - (n < zero_or_powers_of_10[t]); } -#endif // 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 auto count_digits(uint64_t n) -> int { -#ifdef FMT_BUILTIN_CLZLL if (!is_constant_evaluated() && !FMT_OPTIMIZE_SIZE) return do_count_digits(n); -#endif return count_digits_fallback(n); } // Counts the number of digits in n. BITS = log2(radix). template <int BITS, typename UInt> FMT_CONSTEXPR auto count_digits(UInt n) -> int { -#ifdef FMT_BUILTIN_CLZ if (!is_constant_evaluated() && num_bits<UInt>() == 32) - return (FMT_BUILTIN_CLZ(static_cast<uint32_t>(n) | 1) ^ 31) / BITS + 1; -#endif + return (clz(static_cast<uint32_t>(n) | 1) ^ 31) / BITS + 1; // Lambda avoids unreachable code warnings from NVHPC. return [](UInt m) { int num_digits = 0; @@ -2678,13 +2636,12 @@ }(n); } -#ifdef FMT_BUILTIN_CLZ // It is a separate function rather than a part of count_digits to workaround // the lack of static constexpr in constexpr functions. FMT_INLINE auto do_count_digits(uint32_t n) -> int { // An optimization by Kendall Willets from https://bit.ly/3uOIQrB. // This increments the upper 32 bits (log10(T) - 1) when >= T is added. -# define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T) +#define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T) static constexpr uint64_t table[] = { FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8 FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64 @@ -2698,16 +2655,13 @@ FMT_INC(1000000000), FMT_INC(1000000000), FMT_INC(1000000000), // 1024M FMT_INC(1000000000), FMT_INC(1000000000) // 4B }; - auto inc = table[FMT_BUILTIN_CLZ(n | 1) ^ 31]; + auto inc = table[clz(n | 1) ^ 31]; return static_cast<int>((n + inc) >> 32); } -#endif // Optional version of count_digits for better performance on 32-bit platforms. FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int { -#ifdef FMT_BUILTIN_CLZ if (!is_constant_evaluated() && !FMT_OPTIMIZE_SIZE) return do_count_digits(n); -#endif return count_digits_fallback(n); } @@ -4335,8 +4289,8 @@ FMT_CONSTEXPR20 void assign_pow10(int exp) { FMT_ASSERT(exp >= 0, ""); if (exp == 0) return *this = 1; - int bitmask = 1 << (num_bits<unsigned>() - - countl_zero(static_cast<uint32_t>(exp)) - 1); + int bitmask = + 1 << (num_bits<unsigned>() - clz(static_cast<uint32_t>(exp)) - 1); // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by // repeated squaring and multiplication. *this = 5; @@ -4583,7 +4537,7 @@ const auto leading_mask = carrier_uint(0xF) << leading_shift; const auto leading_xdigit = static_cast<uint32_t>((f.f & leading_mask) >> leading_shift); - if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1); + if (leading_xdigit > 1) f.e -= (32 - clz(leading_xdigit) - 1); int print_xdigits = num_xdigits - 1; if (specs.precision >= 0 && print_xdigits > specs.precision) {
diff --git a/support/import-zmij.py b/support/import-zmij.py index fc89e4d..4dbaa24 100755 --- a/support/import-zmij.py +++ b/support/import-zmij.py
@@ -113,6 +113,31 @@ ' return false;\n' '#endif\n' '}\n', 'using fmt::detail::is_constant_evaluated;\n') + # fmt's clz has the same implementation. + h = sub(h, 'inline ZMIJ_CONSTEXPR20 auto clz(uint64_t x) noexcept -> int {\n' + ' assert(x != 0);\n' + ' if (!is_constant_evaluated()) {\n' + '#if ZMIJ_HAS_BUILTIN(__builtin_clzll) || ZMIJ_GNUC_BUILTINS\n' + ' return __builtin_clzll(x);\n' + '#elif defined(_M_AMD64) && defined(__AVX2__)\n' + ' // Use lzcnt only on AVX2-capable CPUs that have this BMI ' + 'instruction.\n' + ' return __lzcnt64(x);\n' + '#elif defined(_M_AMD64) || defined(_M_ARM64)\n' + ' unsigned long idx;\n' + ' _BitScanReverse64(&idx, x);\n' + ' return 63 - idx;\n' + '#elif ZMIJ_MSC_VERSION\n' + ' unsigned long idx;\n' + ' if (_BitScanReverse(&idx, uint32_t(x >> 32))) return 31 - idx;\n' + ' _BitScanReverse(&idx, uint32_t(x));\n' + ' return 63 - idx;\n' + '#endif\n' + ' }\n' + ' int n = 64;\n' + ' for (; x > 0; x >>= 1) --n;\n' + ' return n;\n' + '}\n', 'using fmt::detail::clz;\n') # Integer formatting. h = cut(h, '// Minimum buffer capacities for integer write functions.', '};\n\n')
diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 450b960..29fedce 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc
@@ -286,16 +286,21 @@ } #endif -TEST(format_impl_test, countl_zero) { - constexpr auto num_bits = fmt::detail::num_bits<uint32_t>(); - uint32_t n = 1u; +template <typename UInt> void test_clz() { + constexpr auto num_bits = fmt::detail::num_bits<UInt>(); + UInt n = 1u; for (int i = 1; i < num_bits - 1; i++) { n <<= 1; - EXPECT_EQ(fmt::detail::countl_zero(n - 1), num_bits - i); - EXPECT_EQ(fmt::detail::countl_zero(n), num_bits - i - 1); + EXPECT_EQ(fmt::detail::clz(n - 1), num_bits - i); + EXPECT_EQ(fmt::detail::clz(n), num_bits - i - 1); } } +TEST(format_impl_test, clz) { + test_clz<uint32_t>(); + test_clz<uint64_t>(); +} + #if FMT_USE_FLOAT128 TEST(format_impl_test, write_float128) { auto s = std::string();