summaryrefslogtreecommitdiff
path: root/src/lib/clog2.c
blob: 092b49c821f6ee2365116905d460e248865477a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}