Simplify divisible_by_power_of_2

This commit is contained in:
Victor Zverovich 2020-09-22 20:44:29 -07:00
parent 085171e7e6
commit 605ce5e429

View File

@ -1858,30 +1858,23 @@ template <class T> struct decimal_fp {
int exponent; 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 { inline bool divisible_by_power_of_2(uint32_t x, int exp) FMT_NOEXCEPT {
FMT_ASSERT(exp >= 1, ""); FMT_ASSERT(exp >= 1, "");
FMT_ASSERT(x != 0, ""); FMT_ASSERT(x != 0, "");
#ifdef FMT_BUILTIN_CTZ #ifdef FMT_BUILTIN_CTZ
return FMT_BUILTIN_CTZ(x) >= exp; return FMT_BUILTIN_CTZ(x) >= exp;
#else #else
if (exp >= num_bits<uint32_t>()) { return exp < num_bits<uint32_t>() && x == ((x >> exp) << exp);
return false;
}
return x == ((x >> exp) << exp);
#endif #endif
} }
// Fast divisibility test for powers of 2 (double).
inline bool divisible_by_power_of_2(uint64_t x, int exp) FMT_NOEXCEPT { inline bool divisible_by_power_of_2(uint64_t x, int exp) FMT_NOEXCEPT {
FMT_ASSERT(exp >= 1, ""); FMT_ASSERT(exp >= 1, "");
FMT_ASSERT(x != 0, ""); FMT_ASSERT(x != 0, "");
#ifdef FMT_BUILTIN_CTZLL #ifdef FMT_BUILTIN_CTZLL
return FMT_BUILTIN_CTZLL(x) >= exp; return FMT_BUILTIN_CTZLL(x) >= exp;
#else #else
if (exp >= num_bits<uint64_t>()) { return exp < num_bits<uint64_t>()) && x == ((x >> exp) << exp);
return false;
}
return x == ((x >> exp) << exp);
#endif #endif
} }