[PATCH 5/6] add log base 2 function: log2_exact()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add log2_exact() to get the base 2 logarithm of a value known
to be a power of 2.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 bits.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/bits.h b/bits.h
index 63a663c248e4..9908190d8c2f 100644
--- a/bits.h
+++ b/bits.h
@@ -63,4 +63,11 @@ 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
-- 
2.29.2




[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux