xref: /linux-6.15/include/linux/bitops.h (revision 0e862838)
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(name, generic_##name))
41 
42 __check_bitop_pr(__set_bit);
43 __check_bitop_pr(__clear_bit);
44 __check_bitop_pr(__change_bit);
45 __check_bitop_pr(__test_and_set_bit);
46 __check_bitop_pr(__test_and_clear_bit);
47 __check_bitop_pr(__test_and_change_bit);
48 __check_bitop_pr(test_bit);
49 
50 #undef __check_bitop_pr
51 
52 static inline int get_bitmask_order(unsigned int count)
53 {
54 	int order;
55 
56 	order = fls(count);
57 	return order;	/* We could be slightly more clever with -1 here... */
58 }
59 
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
152 static __always_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  */
163 static __always_inline __s64 sign_extend64(__u64 value, int index)
164 {
165 	__u8 shift = 63 - index;
166 	return (__s64)(value << shift) >> shift;
167 }
168 
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 static inline int get_count_order(unsigned int count)
177 {
178 	if (count == 0)
179 		return -1;
180 
181 	return fls(--count);
182 }
183 
184 /**
185  * get_count_order_long - get order after rounding @l up to power of 2
186  * @l: parameter
187  *
188  * it is same as get_count_order() but with long type parameter
189  */
190 static inline int get_count_order_long(unsigned long l)
191 {
192 	if (l == 0UL)
193 		return -1;
194 	return (int)fls_long(--l);
195 }
196 
197 /**
198  * __ffs64 - find first set bit in a 64 bit word
199  * @word: The 64 bit word
200  *
201  * On 64 bit arches this is a synonym for __ffs
202  * The result is not defined if no bits are set, so check that @word
203  * is non-zero before calling this.
204  */
205 static inline unsigned long __ffs64(u64 word)
206 {
207 #if BITS_PER_LONG == 32
208 	if (((u32)word) == 0UL)
209 		return __ffs((u32)(word >> 32)) + 32;
210 #elif BITS_PER_LONG != 64
211 #error BITS_PER_LONG not 32 or 64
212 #endif
213 	return __ffs((unsigned long)word);
214 }
215 
216 /**
217  * assign_bit - Assign value to a bit in memory
218  * @nr: the bit to set
219  * @addr: the address to start counting from
220  * @value: the value to assign
221  */
222 static __always_inline void assign_bit(long nr, volatile unsigned long *addr,
223 				       bool value)
224 {
225 	if (value)
226 		set_bit(nr, addr);
227 	else
228 		clear_bit(nr, addr);
229 }
230 
231 static __always_inline void __assign_bit(long nr, volatile unsigned long *addr,
232 					 bool value)
233 {
234 	if (value)
235 		__set_bit(nr, addr);
236 	else
237 		__clear_bit(nr, addr);
238 }
239 
240 /**
241  * __ptr_set_bit - Set bit in a pointer's value
242  * @nr: the bit to set
243  * @addr: the address of the pointer variable
244  *
245  * Example:
246  *	void *p = foo();
247  *	__ptr_set_bit(bit, &p);
248  */
249 #define __ptr_set_bit(nr, addr)                         \
250 	({                                              \
251 		typecheck_pointer(*(addr));             \
252 		__set_bit(nr, (unsigned long *)(addr)); \
253 	})
254 
255 /**
256  * __ptr_clear_bit - Clear bit in a pointer's value
257  * @nr: the bit to clear
258  * @addr: the address of the pointer variable
259  *
260  * Example:
261  *	void *p = foo();
262  *	__ptr_clear_bit(bit, &p);
263  */
264 #define __ptr_clear_bit(nr, addr)                         \
265 	({                                                \
266 		typecheck_pointer(*(addr));               \
267 		__clear_bit(nr, (unsigned long *)(addr)); \
268 	})
269 
270 /**
271  * __ptr_test_bit - Test bit in a pointer's value
272  * @nr: the bit to test
273  * @addr: the address of the pointer variable
274  *
275  * Example:
276  *	void *p = foo();
277  *	if (__ptr_test_bit(bit, &p)) {
278  *	        ...
279  *	} else {
280  *		...
281  *	}
282  */
283 #define __ptr_test_bit(nr, addr)                       \
284 	({                                             \
285 		typecheck_pointer(*(addr));            \
286 		test_bit(nr, (unsigned long *)(addr)); \
287 	})
288 
289 #ifdef __KERNEL__
290 
291 #ifndef set_mask_bits
292 #define set_mask_bits(ptr, mask, bits)	\
293 ({								\
294 	const typeof(*(ptr)) mask__ = (mask), bits__ = (bits);	\
295 	typeof(*(ptr)) old__, new__;				\
296 								\
297 	do {							\
298 		old__ = READ_ONCE(*(ptr));			\
299 		new__ = (old__ & ~mask__) | bits__;		\
300 	} while (cmpxchg(ptr, old__, new__) != old__);		\
301 								\
302 	old__;							\
303 })
304 #endif
305 
306 #ifndef bit_clear_unless
307 #define bit_clear_unless(ptr, clear, test)	\
308 ({								\
309 	const typeof(*(ptr)) clear__ = (clear), test__ = (test);\
310 	typeof(*(ptr)) old__, new__;				\
311 								\
312 	do {							\
313 		old__ = READ_ONCE(*(ptr));			\
314 		new__ = old__ & ~clear__;			\
315 	} while (!(old__ & test__) &&				\
316 		 cmpxchg(ptr, old__, new__) != old__);		\
317 								\
318 	!(old__ & test__);					\
319 })
320 #endif
321 
322 #endif /* __KERNEL__ */
323 #endif
324