1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 #include <asm/types.h>
4 #include <linux/bits.h>
5
6 #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
7 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
8
9 extern unsigned int __sw_hweight8(unsigned int w);
10 extern unsigned int __sw_hweight16(unsigned int w);
11 extern unsigned int __sw_hweight32(unsigned int w);
12 extern unsigned long __sw_hweight64(__u64 w);
13
14 /*
15 * Include this here because some architectures need generic_ffs/fls in
16 * scope
17 */
18 #include <asm/bitops.h>
19
20 #define for_each_set_bit(bit, addr, size) \
21 for ((bit) = find_first_bit((addr), (size)); \
22 (bit) < (size); \
23 (bit) = find_next_bit((addr), (size), (bit) + 1))
24
25 /* same as for_each_set_bit() but use bit as value to start with */
26 #define for_each_set_bit_from(bit, addr, size) \
27 for ((bit) = find_next_bit((addr), (size), (bit)); \
28 (bit) < (size); \
29 (bit) = find_next_bit((addr), (size), (bit) + 1))
30
31 #define for_each_clear_bit(bit, addr, size) \
32 for ((bit) = find_first_zero_bit((addr), (size)); \
33 (bit) < (size); \
34 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
35
36 /* same as for_each_clear_bit() but use bit as value to start with */
37 #define for_each_clear_bit_from(bit, addr, size) \
38 for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
39 (bit) < (size); \
40 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
41
get_bitmask_order(unsigned int count)42 static inline int get_bitmask_order(unsigned int count)
43 {
44 int order;
45
46 order = fls(count);
47 return order; /* We could be slightly more clever with -1 here... */
48 }
49
get_count_order(unsigned int count)50 static inline int get_count_order(unsigned int count)
51 {
52 int order;
53
54 order = fls(count) - 1;
55 if (count & (count - 1))
56 order++;
57 return order;
58 }
59
hweight_long(unsigned long w)60 static __always_inline unsigned long hweight_long(unsigned long w)
61 {
62 return sizeof(w) == 4 ? hweight32(w) : hweight64((__u64)w);
63 }
64
65 /**
66 * rol64 - rotate a 64-bit value left
67 * @word: value to rotate
68 * @shift: bits to roll
69 */
rol64(__u64 word,unsigned int shift)70 static inline __u64 rol64(__u64 word, unsigned int shift)
71 {
72 return (word << (shift & 63)) | (word >> ((-shift) & 63));
73 }
74
75 /**
76 * ror64 - rotate a 64-bit value right
77 * @word: value to rotate
78 * @shift: bits to roll
79 */
ror64(__u64 word,unsigned int shift)80 static inline __u64 ror64(__u64 word, unsigned int shift)
81 {
82 return (word >> (shift & 63)) | (word << ((-shift) & 63));
83 }
84
85 /**
86 * rol32 - rotate a 32-bit value left
87 * @word: value to rotate
88 * @shift: bits to roll
89 */
rol32(__u32 word,unsigned int shift)90 static inline __u32 rol32(__u32 word, unsigned int shift)
91 {
92 return (word << (shift & 31)) | (word >> ((-shift) & 31));
93 }
94
95 /**
96 * ror32 - rotate a 32-bit value right
97 * @word: value to rotate
98 * @shift: bits to roll
99 */
ror32(__u32 word,unsigned int shift)100 static inline __u32 ror32(__u32 word, unsigned int shift)
101 {
102 return (word >> (shift & 31)) | (word << ((-shift) & 31));
103 }
104
105 /**
106 * rol16 - rotate a 16-bit value left
107 * @word: value to rotate
108 * @shift: bits to roll
109 */
rol16(__u16 word,unsigned int shift)110 static inline __u16 rol16(__u16 word, unsigned int shift)
111 {
112 return (word << (shift & 15)) | (word >> ((-shift) & 15));
113 }
114
115 /**
116 * ror16 - rotate a 16-bit value right
117 * @word: value to rotate
118 * @shift: bits to roll
119 */
ror16(__u16 word,unsigned int shift)120 static inline __u16 ror16(__u16 word, unsigned int shift)
121 {
122 return (word >> (shift & 15)) | (word << ((-shift) & 15));
123 }
124
125 /**
126 * rol8 - rotate an 8-bit value left
127 * @word: value to rotate
128 * @shift: bits to roll
129 */
rol8(__u8 word,unsigned int shift)130 static inline __u8 rol8(__u8 word, unsigned int shift)
131 {
132 return (word << (shift & 7)) | (word >> ((-shift) & 7));
133 }
134
135 /**
136 * ror8 - rotate an 8-bit value right
137 * @word: value to rotate
138 * @shift: bits to roll
139 */
ror8(__u8 word,unsigned int shift)140 static inline __u8 ror8(__u8 word, unsigned int shift)
141 {
142 return (word >> (shift & 7)) | (word << ((-shift) & 7));
143 }
144
145 /**
146 * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
147 * @value: value to sign extend
148 * @index: 0 based bit index (0<=index<32) to sign bit
149 *
150 * This is safe to use for 16- and 8-bit types as well.
151 */
sign_extend32(__u32 value,int index)152 static inline __s32 sign_extend32(__u32 value, int index)
153 {
154 __u8 shift = 31 - index;
155 return (__s32)(value << shift) >> shift;
156 }
157
158 /**
159 * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
160 * @value: value to sign extend
161 * @index: 0 based bit index (0<=index<64) to sign bit
162 */
sign_extend64(__u64 value,int index)163 static inline __s64 sign_extend64(__u64 value, int index)
164 {
165 __u8 shift = 63 - index;
166 return (__s64)(value << shift) >> shift;
167 }
168
fls_long(unsigned long l)169 static inline unsigned fls_long(unsigned long l)
170 {
171 if (sizeof(l) == 4)
172 return fls(l);
173 return fls64(l);
174 }
175
176 /**
177 * __ffs64 - find first set bit in a 64 bit word
178 * @word: The 64 bit word
179 *
180 * On 64 bit arches this is a synomyn for __ffs
181 * The result is not defined if no bits are set, so check that @word
182 * is non-zero before calling this.
183 */
__ffs64(u64 word)184 static inline unsigned long __ffs64(u64 word)
185 {
186 #if BITS_PER_LONG == 32
187 if (((u32)word) == 0UL)
188 return __ffs((u32)(word >> 32)) + 32;
189 #elif BITS_PER_LONG != 64
190 #error BITS_PER_LONG not 32 or 64
191 #endif
192 return __ffs((unsigned long)word);
193 }
194
195 #ifdef __KERNEL__
196
197 #ifndef set_mask_bits
198 #define set_mask_bits(ptr, _mask, _bits) \
199 ({ \
200 const typeof(*ptr) mask = (_mask), bits = (_bits); \
201 typeof(*ptr) old, new; \
202 \
203 do { \
204 old = ACCESS_ONCE(*ptr); \
205 new = (old & ~mask) | bits; \
206 } while (cmpxchg(ptr, old, new) != old); \
207 \
208 new; \
209 })
210 #endif
211
212 #ifndef find_last_bit
213 /**
214 * find_last_bit - find the last set bit in a memory region
215 * @addr: The address to start the search at
216 * @size: The number of bits to search
217 *
218 * Returns the bit number of the last set bit, or size.
219 */
220 extern unsigned long find_last_bit(const unsigned long *addr,
221 unsigned long size);
222 #endif
223
224 #endif /* __KERNEL__ */
225 #endif
226