]>
Commit | Line | Data |
---|---|---|
b15cbc0b WD |
1 | #ifndef _LINUX_BITOPS_H |
2 | #define _LINUX_BITOPS_H | |
3 | ||
02f99901 | 4 | #include <asm/types.h> |
e1b27d27 | 5 | #include <asm-generic/bitsperlong.h> |
de4d2e9e | 6 | #include <linux/compiler.h> |
b15cbc0b | 7 | |
67345287 JT |
8 | #define BIT(nr) (1UL << (nr)) |
9 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) | |
10 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) | |
92a3188d | 11 | |
89b5c81b JT |
12 | /* |
13 | * Create a contiguous bitmask starting at bit position @l and ending at | |
14 | * position @h. For example | |
15 | * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000. | |
16 | */ | |
17 | #define GENMASK(h, l) \ | |
18 | (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) | |
19 | ||
20 | #define GENMASK_ULL(h, l) \ | |
21 | (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) | |
22 | ||
b15cbc0b WD |
23 | /* |
24 | * ffs: find first bit set. This is defined the same way as | |
25 | * the libc and compiler builtin ffs routines, therefore | |
26 | * differs in spirit from the above ffz (man ffs). | |
27 | */ | |
28 | ||
29 | static inline int generic_ffs(int x) | |
30 | { | |
31 | int r = 1; | |
32 | ||
33 | if (!x) | |
34 | return 0; | |
35 | if (!(x & 0xffff)) { | |
36 | x >>= 16; | |
37 | r += 16; | |
38 | } | |
39 | if (!(x & 0xff)) { | |
40 | x >>= 8; | |
41 | r += 8; | |
42 | } | |
43 | if (!(x & 0xf)) { | |
44 | x >>= 4; | |
45 | r += 4; | |
46 | } | |
47 | if (!(x & 3)) { | |
48 | x >>= 2; | |
49 | r += 2; | |
50 | } | |
51 | if (!(x & 1)) { | |
52 | x >>= 1; | |
53 | r += 1; | |
54 | } | |
55 | return r; | |
56 | } | |
57 | ||
52d61227 SK |
58 | /** |
59 | * fls - find last (most-significant) bit set | |
60 | * @x: the word to search | |
61 | * | |
62 | * This is defined the same way as ffs. | |
63 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. | |
64 | */ | |
65 | static inline int generic_fls(int x) | |
66 | { | |
67 | int r = 32; | |
68 | ||
69 | if (!x) | |
70 | return 0; | |
71 | if (!(x & 0xffff0000u)) { | |
72 | x <<= 16; | |
73 | r -= 16; | |
74 | } | |
75 | if (!(x & 0xff000000u)) { | |
76 | x <<= 8; | |
77 | r -= 8; | |
78 | } | |
79 | if (!(x & 0xf0000000u)) { | |
80 | x <<= 4; | |
81 | r -= 4; | |
82 | } | |
83 | if (!(x & 0xc0000000u)) { | |
84 | x <<= 2; | |
85 | r -= 2; | |
86 | } | |
87 | if (!(x & 0x80000000u)) { | |
88 | x <<= 1; | |
89 | r -= 1; | |
90 | } | |
91 | return r; | |
92 | } | |
93 | ||
94 | ||
b15cbc0b WD |
95 | /* |
96 | * hweightN: returns the hamming weight (i.e. the number | |
97 | * of bits set) of a N-bit word | |
98 | */ | |
99 | ||
100 | static inline unsigned int generic_hweight32(unsigned int w) | |
101 | { | |
8bde7f77 WD |
102 | unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); |
103 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
104 | res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); | |
105 | res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); | |
106 | return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); | |
b15cbc0b WD |
107 | } |
108 | ||
109 | static inline unsigned int generic_hweight16(unsigned int w) | |
110 | { | |
8bde7f77 WD |
111 | unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555); |
112 | res = (res & 0x3333) + ((res >> 2) & 0x3333); | |
113 | res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F); | |
114 | return (res & 0x00FF) + ((res >> 8) & 0x00FF); | |
b15cbc0b WD |
115 | } |
116 | ||
117 | static inline unsigned int generic_hweight8(unsigned int w) | |
118 | { | |
8bde7f77 WD |
119 | unsigned int res = (w & 0x55) + ((w >> 1) & 0x55); |
120 | res = (res & 0x33) + ((res >> 2) & 0x33); | |
121 | return (res & 0x0F) + ((res >> 4) & 0x0F); | |
b15cbc0b WD |
122 | } |
123 | ||
124 | #include <asm/bitops.h> | |
125 | ||
02f99901 SK |
126 | /* linux/include/asm-generic/bitops/non-atomic.h */ |
127 | ||
0413cfec | 128 | #ifndef PLATFORM__SET_BIT |
02f99901 SK |
129 | # define __set_bit generic_set_bit |
130 | #endif | |
131 | ||
0413cfec | 132 | #ifndef PLATFORM__CLEAR_BIT |
02f99901 SK |
133 | # define __clear_bit generic_clear_bit |
134 | #endif | |
135 | ||
0413cfec | 136 | #ifndef PLATFORM_FFS |
52d61227 SK |
137 | # define ffs generic_ffs |
138 | #endif | |
139 | ||
0413cfec | 140 | #ifndef PLATFORM_FLS |
52d61227 SK |
141 | # define fls generic_fls |
142 | #endif | |
143 | ||
de4d2e9e FE |
144 | static inline unsigned fls_long(unsigned long l) |
145 | { | |
146 | if (sizeof(l) == 4) | |
147 | return fls(l); | |
148 | return fls64(l); | |
149 | } | |
150 | ||
151 | /** | |
152 | * __ffs64 - find first set bit in a 64 bit word | |
153 | * @word: The 64 bit word | |
154 | * | |
155 | * On 64 bit arches this is a synomyn for __ffs | |
156 | * The result is not defined if no bits are set, so check that @word | |
157 | * is non-zero before calling this. | |
158 | */ | |
159 | static inline unsigned long __ffs64(u64 word) | |
160 | { | |
161 | #if BITS_PER_LONG == 32 | |
162 | if (((u32)word) == 0UL) | |
163 | return __ffs((u32)(word >> 32)) + 32; | |
164 | #elif BITS_PER_LONG != 64 | |
165 | #error BITS_PER_LONG not 32 or 64 | |
166 | #endif | |
167 | return __ffs((unsigned long)word); | |
168 | } | |
169 | ||
02f99901 SK |
170 | /** |
171 | * __set_bit - Set a bit in memory | |
172 | * @nr: the bit to set | |
173 | * @addr: the address to start counting from | |
174 | * | |
175 | * Unlike set_bit(), this function is non-atomic and may be reordered. | |
176 | * If it's called on the same region of memory simultaneously, the effect | |
177 | * may be that only one operation succeeds. | |
178 | */ | |
179 | static inline void generic_set_bit(int nr, volatile unsigned long *addr) | |
180 | { | |
181 | unsigned long mask = BIT_MASK(nr); | |
182 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); | |
183 | ||
184 | *p |= mask; | |
185 | } | |
186 | ||
187 | static inline void generic_clear_bit(int nr, volatile unsigned long *addr) | |
188 | { | |
189 | unsigned long mask = BIT_MASK(nr); | |
190 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); | |
191 | ||
192 | *p &= ~mask; | |
193 | } | |
b15cbc0b WD |
194 | |
195 | #endif |