]> mj.ucw.cz Git - libucw.git/blob - lib/log2.c
Removed xprintf() -- it was very ugly and its only raison d'etre was
[libucw.git] / lib / log2.c
1 /*
2  *      Sherlock Library -- Binary Logarithm
3  *
4  *      (c) 1997 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11
12 #undef log2
13
14 int
15 log2(u32 x)
16 {
17   uns l;
18
19   if (!x)
20         return 0;
21
22   l = 0;
23   if (x & 0xffff0000) { l += 16; x &= 0xffff0000; }
24   if (x & 0xff00ff00) { l += 8;  x &= 0xff00ff00; }
25   if (x & 0xf0f0f0f0) { l += 4;  x &= 0xf0f0f0f0; }
26   if (x & 0xcccccccc) { l += 2;  x &= 0xcccccccc; }
27   if (x & 0xaaaaaaaa) l++;
28   return l;
29 }