]>
Commit | Line | Data |
---|---|---|
5e656dbb BN |
1 | #ifndef __BITOPS_H__ |
2 | #define __BITOPS_H__ | |
3 | ||
4 | /* | |
5 | * fls: find last bit set. | |
6 | */ | |
7 | ||
5121281b | 8 | #ifndef HAVE_FLS |
5e656dbb BN |
9 | static inline int fls(int x) |
10 | { | |
11 | int r = 32; | |
12 | ||
13 | if (!x) | |
14 | return 0; | |
15 | if (!(x & 0xffff0000u)) { | |
b0e515d6 | 16 | x = (x & 0xffffu) << 16; |
5e656dbb BN |
17 | r -= 16; |
18 | } | |
19 | if (!(x & 0xff000000u)) { | |
b0e515d6 | 20 | x = (x & 0xffffffu) << 8; |
5e656dbb BN |
21 | r -= 8; |
22 | } | |
23 | if (!(x & 0xf0000000u)) { | |
b0e515d6 | 24 | x = (x & 0xfffffffu) << 4; |
5e656dbb BN |
25 | r -= 4; |
26 | } | |
27 | if (!(x & 0xc0000000u)) { | |
b0e515d6 | 28 | x = (x & 0x3fffffffu) << 2; |
5e656dbb BN |
29 | r -= 2; |
30 | } | |
31 | if (!(x & 0x80000000u)) { | |
5e656dbb BN |
32 | r -= 1; |
33 | } | |
34 | return r; | |
35 | } | |
5121281b | 36 | #endif /* HAVE_FLS */ |
5e656dbb BN |
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 | ||
4071f725 DC |
53 | /* |
54 | * ffz: find first zero bit. | |
55 | * Result is undefined if no zero bit exists. | |
56 | */ | |
57 | #define ffz(x) ffs(~(x)) | |
58 | ||
1ca68d67 DW |
59 | /* |
60 | * XFS bit manipulation routines. Repeated here so that some programs | |
61 | * don't have to link in all of libxfs just to have bit manipulation. | |
62 | */ | |
63 | ||
64 | /* | |
65 | * masks with n high/low bits set, 64-bit values | |
66 | */ | |
67 | static inline uint64_t mask64hi(int n) | |
68 | { | |
69 | return (uint64_t)-1 << (64 - (n)); | |
70 | } | |
71 | static inline uint32_t mask32lo(int n) | |
72 | { | |
73 | return ((uint32_t)1 << (n)) - 1; | |
74 | } | |
75 | static inline uint64_t mask64lo(int n) | |
76 | { | |
77 | return ((uint64_t)1 << (n)) - 1; | |
78 | } | |
79 | ||
80 | /* Get high bit set out of 32-bit argument, -1 if none set */ | |
81 | static inline int highbit32(uint32_t v) | |
82 | { | |
83 | return fls(v) - 1; | |
84 | } | |
85 | ||
86 | /* Get high bit set out of 64-bit argument, -1 if none set */ | |
87 | static inline int highbit64(uint64_t v) | |
88 | { | |
89 | return fls64(v) - 1; | |
90 | } | |
91 | ||
92 | /* Get low bit set out of 32-bit argument, -1 if none set */ | |
93 | static inline int lowbit32(uint32_t v) | |
94 | { | |
95 | return ffs(v) - 1; | |
96 | } | |
97 | ||
98 | /* Get low bit set out of 64-bit argument, -1 if none set */ | |
99 | static inline int lowbit64(uint64_t v) | |
100 | { | |
101 | uint32_t w = (uint32_t)v; | |
102 | int n = 0; | |
103 | ||
104 | if (w) { /* lower bits */ | |
105 | n = ffs(w); | |
106 | } else { /* upper bits */ | |
107 | w = (uint32_t)(v >> 32); | |
108 | if (w) { | |
109 | n = ffs(w); | |
110 | if (n) | |
111 | n += 32; | |
112 | } | |
113 | } | |
114 | return n - 1; | |
115 | } | |
116 | ||
5e656dbb | 117 | #endif |