summaryrefslogtreecommitdiffstats
path: root/lib/bitops.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bitops.c')
-rw-r--r--lib/bitops.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/bitops.c b/lib/bitops.c
index 6ca0505..88cef78 100644
--- a/lib/bitops.c
+++ b/lib/bitops.c
@@ -45,3 +45,24 @@ u32_masklen(u32 x)
if (x & 0xaaaaaaaa) l++;
return l;
}
+
+/**
+ * u32_log2 - compute a binary logarithm.
+ * @v: number
+ *
+ * This function computes a integral part of binary logarithm of given
+ * integer @v and returns it. The computed value is also an index of the
+ * first non-zero bit position.
+ */
+
+u32
+u32_log2(u32 v)
+{
+ u32 r, shift;
+ r = (v > 0xFFFF) << 4; v >>= r;
+ shift = (v > 0xFF ) << 3; v >>= shift; r |= shift;
+ shift = (v > 0xF ) << 2; v >>= shift; r |= shift;
+ shift = (v > 0x3 ) << 1; v >>= shift; r |= shift;
+ r |= (v >> 1);
+ return r;
+}