coreboot/src/lib/clog2.c
<<
>>
Prefs
   1#undef DEBUG_LOG2
   2
   3#ifdef DEBUG_LOG2
   4#include <console/console.h>
   5#endif
   6
   7#include <lib.h>
   8
   9/* Assume 8 bits per byte */
  10#define CHAR_BIT 8
  11
  12unsigned long log2(unsigned long x)
  13{
  14        // assume 8 bits per byte.
  15        unsigned long i = 1ULL << (sizeof(x)* CHAR_BIT - 1ULL);
  16        unsigned long pow = sizeof(x) * CHAR_BIT - 1ULL;
  17
  18        if (! x) {
  19#ifdef DEBUG_LOG2
  20                printk(BIOS_WARNING, "%s called with invalid parameter of 0\n",
  21                        __func__);
  22#endif
  23                return -1;
  24        }
  25        for(; i > x; i >>= 1, pow--)
  26                ;
  27
  28        return pow;
  29}
  30
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.