]> mj.ucw.cz Git - libucw.git/blob - ucw/bitops.h
Build: Fixed few compilation warnings/errors.
[libucw.git] / ucw / bitops.h
1 /*
2  *      UCW Library -- Bit Operations
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  *      (c) 2012 Pavel Charvat <pchar@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_BITOPS_H
12 #define _UCW_BITOPS_H
13
14 #ifdef CONFIG_UCW_CLEAN_ABI
15 #define bit_fls ucw_bit_fls
16 #define ffs_table ucw_ffs_table
17 #endif
18
19 /* Find highest bit set (i.e., the floor of the binary logarithm) (bit-fls.c) */
20
21 int bit_fls(u32 x);             /* bit_fls(0)=-1 */
22
23 /* Find lowest bit set, undefined for zero argument (bit-ffs.c) */
24
25 extern const byte ffs_table[256];
26
27 #ifdef __pentium4               /* On other ia32 machines, the C version is faster */
28
29 static inline uns bit_ffs(uns w)
30 {
31   asm("bsfl %1,%0" :"=r" (w) :"rm" (w));
32   return w;
33 }
34
35 #else
36
37 static inline uns bit_ffs(uns w)
38 {
39   uns b = (w & 0xffff) ? 0 : 16;
40   b += ((w >> b) & 0xff) ? 0 : 8;
41   return b + ffs_table[(w >> b) & 0xff];
42 }
43
44 #endif
45
46 /* Count the number of bits set */
47
48 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
49
50 static inline uns bit_count(uns w)
51 {
52   return __builtin_popcount(w);
53 }
54
55 #else
56
57 static inline uns bit_count(uns w)
58 {
59   uns n = 0;
60   while (w)
61     {
62       w &= w - 1;
63       n++;
64     }
65   return n;
66 }
67
68 #endif
69
70 #endif