2003-04-22 21:02:15 +02:00
|
|
|
#include <console/console.h>
|
2009-10-28 17:13:28 +01:00
|
|
|
#include <lib.h>
|
|
|
|
|
2005-07-08 04:49:49 +02:00
|
|
|
/* Assume 8 bits per byte */
|
|
|
|
#define CHAR_BIT 8
|
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
unsigned long log2(unsigned long x)
|
|
|
|
{
|
2014-06-17 10:53:36 +02:00
|
|
|
/* assume 8 bits per byte. */
|
|
|
|
unsigned long pow = sizeof(x) * CHAR_BIT - 1ULL;
|
|
|
|
unsigned long i = 1ULL << pow;
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2014-06-17 10:53:36 +02:00
|
|
|
if (!x) {
|
|
|
|
printk(BIOS_WARNING, "%s called with invalid parameter of 0\n",
|
2005-07-08 04:49:49 +02:00
|
|
|
__func__);
|
2014-06-17 10:53:36 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2014-06-17 10:53:36 +02:00
|
|
|
for (; i > x; i >>= 1, pow--);
|
|
|
|
|
|
|
|
return pow;
|
2003-04-22 21:02:15 +02:00
|
|
|
}
|
2014-01-13 07:49:49 +01:00
|
|
|
|
|
|
|
unsigned long log2_ceil(unsigned long x)
|
|
|
|
{
|
|
|
|
unsigned long pow;
|
|
|
|
|
2014-06-17 10:53:36 +02:00
|
|
|
if (!x)
|
2014-01-13 07:49:49 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
pow = log2(x);
|
|
|
|
|
|
|
|
if (x > (1ULL << pow))
|
|
|
|
pow++;
|
|
|
|
|
|
|
|
return pow;
|
|
|
|
}
|