]> mj.ucw.cz Git - libucw.git/blob - ucw/bitops.h
make Sherlock compilable on Darwin without hacks due to missing direct IO
[libucw.git] / ucw / bitops.h
1 /*
2  *      UCW Library -- Bit Operations
3  *
4  *      (c) 2005 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 #ifndef _UCW_BITOPS_H
11 #define _UCW_BITOPS_H
12
13 /* Find highest bit set (i.e., the floor of the binary logarithm) (bit-fls.c) */
14
15 int bit_fls(u32 x);             /* bit_fls(0)=-1 */
16
17 /* Find lowest bit set, undefined for zero argument (bit-ffs.c) */
18
19 extern const byte ffs_table[256];
20
21 #ifdef __pentium4               /* On other ia32 machines, the C version is faster */
22
23 static inline uns bit_ffs(uns w)
24 {
25   asm("bsfl %1,%0" :"=r" (w) :"rm" (w));
26   return w;
27 }
28
29 #else
30
31 static inline uns bit_ffs(uns w)
32 {
33   uns b = (w & 0xffff) ? 0 : 16;
34   b += ((w >> b) & 0xff) ? 0 : 8;
35   return b + ffs_table[(w >> b) & 0xff];
36 }
37
38 #endif
39
40 #endif