From aa225a7dc44d61516a20163c90b12dfecb8bd030 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Thu, 2 Apr 2020 12:51:35 +0100 Subject: [PATCH] bit_util: Add CountLeadingZeros --- src/common/bit_util.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 4eea814c..05da355f 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -150,6 +150,17 @@ inline size_t BitCount(Integral value) { return std::bitset()>(value).count(); } +template +constexpr size_t CountLeadingZeros(T value) { + auto x = static_cast>(value); + size_t result = BitSize(); + while (x != 0) { + x >>= 1; + result--; + } + return result; +} + template constexpr int HighestSetBit(T value) { auto x = static_cast>(value);