aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/bits.h
diff options
Diffstat (limited to 'bits.h')
-rw-r--r--bits.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/bits.h b/bits.h
index c0dc952e..9908190d 100644
--- a/bits.h
+++ b/bits.h
@@ -58,4 +58,16 @@ static inline long long bits_extend(long long val, unsigned size, int is_signed)
return val;
}
+static inline int is_power_of_2(long long val)
+{
+ return val && !(val & (val - 1));
+}
+
+///
+// log base 2 of an exact power-of-2
+static inline int log2_exact(unsigned long long val)
+{
+ return 8 * sizeof(val) - __builtin_clzl(val) - 1;
+}
+
#endif