From 605ce5e429824c2f0b75ee413010dc50a4ac0e8d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 22 Sep 2020 20:44:29 -0700 Subject: [PATCH] Simplify divisible_by_power_of_2 --- include/fmt/format-inl.h | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 76bd34aa..87a56050 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1858,30 +1858,23 @@ template struct decimal_fp { int exponent; }; -// Fast divisibility test for powers of 2 (float). +// Returns true iff x is divisible by pow(2, exp). inline bool divisible_by_power_of_2(uint32_t x, int exp) FMT_NOEXCEPT { FMT_ASSERT(exp >= 1, ""); FMT_ASSERT(x != 0, ""); #ifdef FMT_BUILTIN_CTZ return FMT_BUILTIN_CTZ(x) >= exp; #else - if (exp >= num_bits()) { - return false; - } - return x == ((x >> exp) << exp); + return exp < num_bits() && x == ((x >> exp) << exp); #endif } -// Fast divisibility test for powers of 2 (double). inline bool divisible_by_power_of_2(uint64_t x, int exp) FMT_NOEXCEPT { FMT_ASSERT(exp >= 1, ""); FMT_ASSERT(x != 0, ""); #ifdef FMT_BUILTIN_CTZLL return FMT_BUILTIN_CTZLL(x) >= exp; #else - if (exp >= num_bits()) { - return false; - } - return x == ((x >> exp) << exp); + return exp < num_bits()) && x == ((x >> exp) << exp); #endif }