This will never happen unless the code is buggy (in which case it's easy to
[coreboot.git] / src / lib / clog2.c
1 #undef DEBUG_LOG2
2
3 #ifdef DEBUG_LOG2
4 #include <console/console.h>
5 #endif
6
7 unsigned long log2(unsigned long x)
8 {
9         // assume 8 bits per byte.
10         unsigned long i = 1 << (sizeof(x)*8 - 1);
11         unsigned long pow = sizeof(x) * 8 - 1;
12
13         if (! x) {
14 #ifdef DEBUG_LOG2
15                 printk_warning("%s called with invalid parameter of 0\n",
16                         __FUNCTION__);
17 #endif
18                 return -1;
19         }
20         for(; i > x; i >>= 1, pow--)
21                 ;
22
23         return pow;
24 }