]> mj.ucw.cz Git - libucw.git/blob - lib/log2.c
Split page key to pos and fd. Wastes extra 4 bytes, but simplifies the code a lot.
[libucw.git] / lib / log2.c
1 /*
2  *      Sherlock Library -- Binary Logarithm
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8
9 #include "lib.h"
10
11 #undef log2
12
13 int
14 log2(u32 x)
15 {
16   uns l;
17
18   if (!x)
19         return 0;
20
21   l = 0;
22   if (x & 0xffff0000) { l += 16; x &= 0xffff0000; }
23   if (x & 0xff00ff00) { l += 8;  x &= 0xff00ff00; }
24   if (x & 0xf0f0f0f0) { l += 4;  x &= 0xf0f0f0f0; }
25   if (x & 0xcccccccc) { l += 2;  x &= 0xcccccccc; }
26   if (x & 0xaaaaaaaa) l++;
27   return l;
28 }