mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-14 23:11:05 +00:00
Add countl_zero function
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
This commit is contained in:
parent
8d50d814db
commit
d24be2e95c
@ -472,6 +472,16 @@ inline auto bit_cast(const From& from) -> To {
|
||||
return result;
|
||||
}
|
||||
|
||||
FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int {
|
||||
#ifdef FMT_BUILTIN_CLZ
|
||||
if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n);
|
||||
#endif
|
||||
int lz = 0;
|
||||
constexpr uint32_t msb_mask = 1u << (num_bits<uint32_t>() - 1);
|
||||
for (; (n & msb_mask) == 0; n <<= 1) lz++;
|
||||
return lz;
|
||||
}
|
||||
|
||||
FMT_INLINE void assume(bool condition) {
|
||||
(void)condition;
|
||||
#if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION
|
||||
@ -3149,7 +3159,7 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
|
||||
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 - FMT_BUILTIN_CLZ(leading_xdigit) - 1);
|
||||
if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1);
|
||||
|
||||
int print_xdigits = num_xdigits - 1;
|
||||
if (precision >= 0 && print_xdigits > precision) {
|
||||
|
@ -353,6 +353,16 @@ TEST(format_impl_test, count_digits) {
|
||||
test_count_digits<uint64_t>();
|
||||
}
|
||||
|
||||
TEST(format_impl_test, countl_zero) {
|
||||
constexpr auto num_bits = fmt::detail::num_bits<uint32_t>();
|
||||
uint32_t 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);
|
||||
}
|
||||
}
|
||||
|
||||
#if FMT_USE_FLOAT128
|
||||
TEST(format_impl_test, write_float128) {
|
||||
auto s = std::string();
|
||||
|
Loading…
Reference in New Issue
Block a user