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