]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - include/bitops.h
build: Add fls check into autoconf
[thirdparty/xfsprogs-dev.git] / include / bitops.h
1 #ifndef __BITOPS_H__
2 #define __BITOPS_H__
3
4 /*
5 * fls: find last bit set.
6 */
7
8 #ifndef HAVE_FLS
9 static inline int fls(int x)
10 {
11 int r = 32;
12
13 if (!x)
14 return 0;
15 if (!(x & 0xffff0000u)) {
16 x <<= 16;
17 r -= 16;
18 }
19 if (!(x & 0xff000000u)) {
20 x <<= 8;
21 r -= 8;
22 }
23 if (!(x & 0xf0000000u)) {
24 x <<= 4;
25 r -= 4;
26 }
27 if (!(x & 0xc0000000u)) {
28 x <<= 2;
29 r -= 2;
30 }
31 if (!(x & 0x80000000u)) {
32 r -= 1;
33 }
34 return r;
35 }
36 #endif /* HAVE_FLS */
37
38 static inline int fls64(__u64 x)
39 {
40 __u32 h = x >> 32;
41 if (h)
42 return fls(h) + 32;
43 return fls(x);
44 }
45
46 static inline unsigned fls_long(unsigned long l)
47 {
48 if (sizeof(l) == 4)
49 return fls(l);
50 return fls64(l);
51 }
52
53 /*
54 * ffz: find first zero bit.
55 * Result is undefined if no zero bit exists.
56 */
57 #define ffz(x) ffs(~(x))
58
59 #endif