]> mj.ucw.cz Git - libucw.git/blob - lib/log2.c
I decided to turn off cf/url-equiv for indexation. however, after the indexer
[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 int
13 fls(u32 x)
14 {
15   uns l;
16
17   if (!x)
18         return 0;
19
20   l = 0;
21   if (x & 0xffff0000) { l += 16; x &= 0xffff0000; }
22   if (x & 0xff00ff00) { l += 8;  x &= 0xff00ff00; }
23   if (x & 0xf0f0f0f0) { l += 4;  x &= 0xf0f0f0f0; }
24   if (x & 0xcccccccc) { l += 2;  x &= 0xcccccccc; }
25   if (x & 0xaaaaaaaa) l++;
26   return l;
27 }