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