2004-05-28 17:10:04 +02:00
|
|
|
#undef DEBUG_LOG2
|
|
|
|
|
|
|
|
#ifdef DEBUG_LOG2
|
2003-04-22 21:02:15 +02:00
|
|
|
#include <console/console.h>
|
2004-05-28 17:10:04 +02:00
|
|
|
#endif
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2005-07-08 04:49:49 +02:00
|
|
|
/* Assume 8 bits per byte */
|
|
|
|
#define CHAR_BIT 8
|
|
|
|
|
2009-10-27 15:29:29 +01:00
|
|
|
unsigned long log2(unsigned long x);
|
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
unsigned long log2(unsigned long x)
|
|
|
|
{
|
|
|
|
// assume 8 bits per byte.
|
2005-07-08 04:49:49 +02:00
|
|
|
unsigned long i = 1ULL << (sizeof(x)* CHAR_BIT - 1ULL);
|
|
|
|
unsigned long pow = sizeof(x) * CHAR_BIT - 1ULL;
|
2003-04-22 21:02:15 +02:00
|
|
|
|
|
|
|
if (! x) {
|
2004-05-28 17:10:04 +02:00
|
|
|
#ifdef DEBUG_LOG2
|
2003-04-22 21:02:15 +02:00
|
|
|
printk_warning("%s called with invalid parameter of 0\n",
|
2005-07-08 04:49:49 +02:00
|
|
|
__func__);
|
2004-05-28 17:10:04 +02:00
|
|
|
#endif
|
2003-04-22 21:02:15 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for(; i > x; i >>= 1, pow--)
|
|
|
|
;
|
|
|
|
|
|
|
|
return pow;
|
|
|
|
}
|