mirror of
https://github.com/justinian/jsix.git
synced 2025-12-09 16:04:32 -08:00
[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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user