[util] Add constexpr log2/is_pow2 helpers

I didn't end up using these, but constexpr log2 and is_pow2 functions
might be helpful.
This commit is contained in:
Justin C. Miller
2022-01-30 21:02:32 -08:00
parent 5dfc6ae62e
commit e3ecd73cd8

View File

@@ -6,6 +6,14 @@
namespace util {
constexpr size_t const_log2(size_t n) {
return n < 2 ? 1 : 1 + const_log2(n/2);
}
constexpr bool is_pow2(size_t n) {
return (n & (n-1)) == 0;
}
// Get the base-2 logarithm of i
inline unsigned log2(uint64_t i) {
if (i < 2) return 0;