bit_util: Add CountLeadingZeros

This commit is contained in:
MerryMage 2020-04-02 12:51:35 +01:00
parent 5d93e6e580
commit aa225a7dc4

View file

@ -150,6 +150,17 @@ inline size_t BitCount(Integral value) {
return std::bitset<BitSize<Integral>()>(value).count(); return std::bitset<BitSize<Integral>()>(value).count();
} }
template <typename T>
constexpr size_t CountLeadingZeros(T value) {
auto x = static_cast<std::make_unsigned_t<T>>(value);
size_t result = BitSize<T>();
while (x != 0) {
x >>= 1;
result--;
}
return result;
}
template <typename T> template <typename T>
constexpr int HighestSetBit(T value) { constexpr int HighestSetBit(T value) {
auto x = static_cast<std::make_unsigned_t<T>>(value); auto x = static_cast<std::make_unsigned_t<T>>(value);