summaryrefslogtreecommitdiff
path: root/src/lib/clog2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/clog2.c')
-rw-r--r--src/lib/clog2.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/lib/clog2.c b/src/lib/clog2.c
new file mode 100644
index 0000000000..092b49c821
--- /dev/null
+++ b/src/lib/clog2.c
@@ -0,0 +1,18 @@
+#include <console/console.h>
+
+unsigned long log2(unsigned long x)
+{
+ // assume 8 bits per byte.
+ unsigned long i = 1 << (sizeof(x)*8 - 1);
+ unsigned long pow = sizeof(x) * 8 - 1;
+
+ if (! x) {
+ printk_warning("%s called with invalid parameter of 0\n",
+ __FUNCTION__);
+ return -1;
+ }
+ for(; i > x; i >>= 1, pow--)
+ ;
+
+ return pow;
+}