xref: /linux-6.15/include/linux/bitops.h (revision c320592f)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_BITOPS_H
31da177e4SLinus Torvalds #define _LINUX_BITOPS_H
4aa6159abSAndy Shevchenko 
51da177e4SLinus Torvalds #include <asm/types.h>
68bd9cb51SWill Deacon #include <linux/bits.h>
7cb0f8003SKumar Kartikeya Dwivedi #include <linux/typecheck.h>
81da177e4SLinus Torvalds 
9aa6159abSAndy Shevchenko #include <uapi/linux/kernel.h>
10aa6159abSAndy Shevchenko 
119144d75eSChris Wilson #define BITS_PER_TYPE(type)	(sizeof(type) * BITS_PER_BYTE)
12aa6159abSAndy Shevchenko #define BITS_TO_LONGS(nr)	__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
13aa6159abSAndy Shevchenko #define BITS_TO_U64(nr)		__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
14aa6159abSAndy Shevchenko #define BITS_TO_U32(nr)		__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
15aa6159abSAndy Shevchenko #define BITS_TO_BYTES(nr)	__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
1610ef6b0dSChen, Gong 
177d8296b2SAlexander Lobakin #define BYTES_TO_BITS(nb)	((nb) * BITS_PER_BYTE)
187d8296b2SAlexander Lobakin 
194677d4a5SBorislav Petkov extern unsigned int __sw_hweight8(unsigned int w);
204677d4a5SBorislav Petkov extern unsigned int __sw_hweight16(unsigned int w);
214677d4a5SBorislav Petkov extern unsigned int __sw_hweight32(unsigned int w);
224677d4a5SBorislav Petkov extern unsigned long __sw_hweight64(__u64 w);
234677d4a5SBorislav Petkov 
24e69eb9c4SAlexander Lobakin /*
25e69eb9c4SAlexander Lobakin  * Defined here because those may be needed by architecture-specific static
26e69eb9c4SAlexander Lobakin  * inlines.
27e69eb9c4SAlexander Lobakin  */
28e69eb9c4SAlexander Lobakin 
290e862838SAlexander Lobakin #include <asm-generic/bitops/generic-non-atomic.h>
300e862838SAlexander Lobakin 
31b03fc117SAlexander Lobakin /*
32b03fc117SAlexander Lobakin  * Many architecture-specific non-atomic bitops contain inline asm code and due
33b03fc117SAlexander Lobakin  * to that the compiler can't optimize them to compile-time expressions or
34b03fc117SAlexander Lobakin  * constants. In contrary, generic_*() helpers are defined in pure C and
35b03fc117SAlexander Lobakin  * compilers optimize them just well.
36b03fc117SAlexander Lobakin  * Therefore, to make `unsigned long foo = 0; __set_bit(BAR, &foo)` effectively
37b03fc117SAlexander Lobakin  * equal to `unsigned long foo = BIT(BAR)`, pick the generic C alternative when
38b03fc117SAlexander Lobakin  * the arguments can be resolved at compile time. That expression itself is a
39b03fc117SAlexander Lobakin  * constant and doesn't bring any functional changes to the rest of cases.
40b03fc117SAlexander Lobakin  * The casts to `uintptr_t` are needed to mitigate `-Waddress` warnings when
41b03fc117SAlexander Lobakin  * passing a bitmap from .bss or .data (-> `!!addr` is always true).
42b03fc117SAlexander Lobakin  */
43e69eb9c4SAlexander Lobakin #define bitop(op, nr, addr)						\
44b03fc117SAlexander Lobakin 	((__builtin_constant_p(nr) &&					\
45b03fc117SAlexander Lobakin 	  __builtin_constant_p((uintptr_t)(addr) != (uintptr_t)NULL) &&	\
46b03fc117SAlexander Lobakin 	  (uintptr_t)(addr) != (uintptr_t)NULL &&			\
47b03fc117SAlexander Lobakin 	  __builtin_constant_p(*(const unsigned long *)(addr))) ?	\
48b03fc117SAlexander Lobakin 	 const##op(nr, addr) : op(nr, addr))
49e69eb9c4SAlexander Lobakin 
50e0eeb938SDan Carpenter /*
51e0eeb938SDan Carpenter  * The following macros are non-atomic versions of their non-underscored
52e0eeb938SDan Carpenter  * counterparts.
53e0eeb938SDan Carpenter  */
54e69eb9c4SAlexander Lobakin #define __set_bit(nr, addr)		bitop(___set_bit, nr, addr)
55e69eb9c4SAlexander Lobakin #define __clear_bit(nr, addr)		bitop(___clear_bit, nr, addr)
56e69eb9c4SAlexander Lobakin #define __change_bit(nr, addr)		bitop(___change_bit, nr, addr)
57e69eb9c4SAlexander Lobakin #define __test_and_set_bit(nr, addr)	bitop(___test_and_set_bit, nr, addr)
58e69eb9c4SAlexander Lobakin #define __test_and_clear_bit(nr, addr)	bitop(___test_and_clear_bit, nr, addr)
59e69eb9c4SAlexander Lobakin #define __test_and_change_bit(nr, addr)	bitop(___test_and_change_bit, nr, addr)
60e0eeb938SDan Carpenter 
61e69eb9c4SAlexander Lobakin #define test_bit(nr, addr)		bitop(_test_bit, nr, addr)
628238b457SMikulas Patocka #define test_bit_acquire(nr, addr)	bitop(_test_bit_acquire, nr, addr)
63e69eb9c4SAlexander Lobakin 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * Include this here because some architectures need generic_ffs/fls in
661da177e4SLinus Torvalds  * scope
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds #include <asm/bitops.h>
691da177e4SLinus Torvalds 
700e862838SAlexander Lobakin /* Check that the bitops prototypes are sane */
710e862838SAlexander Lobakin #define __check_bitop_pr(name)						\
720e862838SAlexander Lobakin 	static_assert(__same_type(arch_##name, generic_##name) &&	\
73bb7379bfSAlexander Lobakin 		      __same_type(const_##name, generic_##name) &&	\
74e69eb9c4SAlexander Lobakin 		      __same_type(_##name, generic_##name))
750e862838SAlexander Lobakin 
760e862838SAlexander Lobakin __check_bitop_pr(__set_bit);
770e862838SAlexander Lobakin __check_bitop_pr(__clear_bit);
780e862838SAlexander Lobakin __check_bitop_pr(__change_bit);
790e862838SAlexander Lobakin __check_bitop_pr(__test_and_set_bit);
800e862838SAlexander Lobakin __check_bitop_pr(__test_and_clear_bit);
810e862838SAlexander Lobakin __check_bitop_pr(__test_and_change_bit);
820e862838SAlexander Lobakin __check_bitop_pr(test_bit);
8372cc1980SAlexander Lobakin __check_bitop_pr(test_bit_acquire);
840e862838SAlexander Lobakin 
850e862838SAlexander Lobakin #undef __check_bitop_pr
860e862838SAlexander Lobakin 
get_bitmask_order(unsigned int count)871a1d48a4SDenys Vlasenko static inline int get_bitmask_order(unsigned int count)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	int order;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	order = fls(count);
921da177e4SLinus Torvalds 	return order;	/* We could be slightly more clever with -1 here... */
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
hweight_long(unsigned long w)951a1d48a4SDenys Vlasenko static __always_inline unsigned long hweight_long(unsigned long w)
961da177e4SLinus Torvalds {
97bd93f003SArnd Bergmann 	return sizeof(w) == 4 ? hweight32(w) : hweight64((__u64)w);
981da177e4SLinus Torvalds }
999f41699eSPeter Zijlstra 
10045f8bde0SRobert P. J. Day /**
101f2ea0f5fSAlexey Dobriyan  * rol64 - rotate a 64-bit value left
102f2ea0f5fSAlexey Dobriyan  * @word: value to rotate
103f2ea0f5fSAlexey Dobriyan  * @shift: bits to roll
104f2ea0f5fSAlexey Dobriyan  */
rol64(__u64 word,unsigned int shift)105f2ea0f5fSAlexey Dobriyan static inline __u64 rol64(__u64 word, unsigned int shift)
106f2ea0f5fSAlexey Dobriyan {
107ef4d6f6bSRasmus Villemoes 	return (word << (shift & 63)) | (word >> ((-shift) & 63));
108f2ea0f5fSAlexey Dobriyan }
109f2ea0f5fSAlexey Dobriyan 
110f2ea0f5fSAlexey Dobriyan /**
111f2ea0f5fSAlexey Dobriyan  * ror64 - rotate a 64-bit value right
112f2ea0f5fSAlexey Dobriyan  * @word: value to rotate
113f2ea0f5fSAlexey Dobriyan  * @shift: bits to roll
114f2ea0f5fSAlexey Dobriyan  */
ror64(__u64 word,unsigned int shift)115f2ea0f5fSAlexey Dobriyan static inline __u64 ror64(__u64 word, unsigned int shift)
116f2ea0f5fSAlexey Dobriyan {
117ef4d6f6bSRasmus Villemoes 	return (word >> (shift & 63)) | (word << ((-shift) & 63));
118f2ea0f5fSAlexey Dobriyan }
119f2ea0f5fSAlexey Dobriyan 
120f2ea0f5fSAlexey Dobriyan /**
1211da177e4SLinus Torvalds  * rol32 - rotate a 32-bit value left
1221da177e4SLinus Torvalds  * @word: value to rotate
1231da177e4SLinus Torvalds  * @shift: bits to roll
1241da177e4SLinus Torvalds  */
rol32(__u32 word,unsigned int shift)1251da177e4SLinus Torvalds static inline __u32 rol32(__u32 word, unsigned int shift)
1261da177e4SLinus Torvalds {
127ef4d6f6bSRasmus Villemoes 	return (word << (shift & 31)) | (word >> ((-shift) & 31));
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
13045f8bde0SRobert P. J. Day /**
1311da177e4SLinus Torvalds  * ror32 - rotate a 32-bit value right
1321da177e4SLinus Torvalds  * @word: value to rotate
1331da177e4SLinus Torvalds  * @shift: bits to roll
1341da177e4SLinus Torvalds  */
ror32(__u32 word,unsigned int shift)1351da177e4SLinus Torvalds static inline __u32 ror32(__u32 word, unsigned int shift)
1361da177e4SLinus Torvalds {
137ef4d6f6bSRasmus Villemoes 	return (word >> (shift & 31)) | (word << ((-shift) & 31));
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1403afe3925SHarvey Harrison /**
1413afe3925SHarvey Harrison  * rol16 - rotate a 16-bit value left
1423afe3925SHarvey Harrison  * @word: value to rotate
1433afe3925SHarvey Harrison  * @shift: bits to roll
1443afe3925SHarvey Harrison  */
rol16(__u16 word,unsigned int shift)1453afe3925SHarvey Harrison static inline __u16 rol16(__u16 word, unsigned int shift)
1463afe3925SHarvey Harrison {
147ef4d6f6bSRasmus Villemoes 	return (word << (shift & 15)) | (word >> ((-shift) & 15));
1483afe3925SHarvey Harrison }
1493afe3925SHarvey Harrison 
1503afe3925SHarvey Harrison /**
1513afe3925SHarvey Harrison  * ror16 - rotate a 16-bit value right
1523afe3925SHarvey Harrison  * @word: value to rotate
1533afe3925SHarvey Harrison  * @shift: bits to roll
1543afe3925SHarvey Harrison  */
ror16(__u16 word,unsigned int shift)1553afe3925SHarvey Harrison static inline __u16 ror16(__u16 word, unsigned int shift)
1563afe3925SHarvey Harrison {
157ef4d6f6bSRasmus Villemoes 	return (word >> (shift & 15)) | (word << ((-shift) & 15));
1583afe3925SHarvey Harrison }
1593afe3925SHarvey Harrison 
1603afe3925SHarvey Harrison /**
1613afe3925SHarvey Harrison  * rol8 - rotate an 8-bit value left
1623afe3925SHarvey Harrison  * @word: value to rotate
1633afe3925SHarvey Harrison  * @shift: bits to roll
1643afe3925SHarvey Harrison  */
rol8(__u8 word,unsigned int shift)1653afe3925SHarvey Harrison static inline __u8 rol8(__u8 word, unsigned int shift)
1663afe3925SHarvey Harrison {
167ef4d6f6bSRasmus Villemoes 	return (word << (shift & 7)) | (word >> ((-shift) & 7));
1683afe3925SHarvey Harrison }
1693afe3925SHarvey Harrison 
1703afe3925SHarvey Harrison /**
1713afe3925SHarvey Harrison  * ror8 - rotate an 8-bit value right
1723afe3925SHarvey Harrison  * @word: value to rotate
1733afe3925SHarvey Harrison  * @shift: bits to roll
1743afe3925SHarvey Harrison  */
ror8(__u8 word,unsigned int shift)1753afe3925SHarvey Harrison static inline __u8 ror8(__u8 word, unsigned int shift)
1763afe3925SHarvey Harrison {
177ef4d6f6bSRasmus Villemoes 	return (word >> (shift & 7)) | (word << ((-shift) & 7));
1783afe3925SHarvey Harrison }
1793afe3925SHarvey Harrison 
1807919a57bSAndreas Herrmann /**
1817919a57bSAndreas Herrmann  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
1827919a57bSAndreas Herrmann  * @value: value to sign extend
1837919a57bSAndreas Herrmann  * @index: 0 based bit index (0<=index<32) to sign bit
184e2eb53aaSMartin Kepplinger  *
185e2eb53aaSMartin Kepplinger  * This is safe to use for 16- and 8-bit types as well.
1867919a57bSAndreas Herrmann  */
sign_extend32(__u32 value,int index)187f80ac98aSJosh Poimboeuf static __always_inline __s32 sign_extend32(__u32 value, int index)
1887919a57bSAndreas Herrmann {
1897919a57bSAndreas Herrmann 	__u8 shift = 31 - index;
1907919a57bSAndreas Herrmann 	return (__s32)(value << shift) >> shift;
1917919a57bSAndreas Herrmann }
1927919a57bSAndreas Herrmann 
19348e203e2SMartin Kepplinger /**
19448e203e2SMartin Kepplinger  * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
19548e203e2SMartin Kepplinger  * @value: value to sign extend
19648e203e2SMartin Kepplinger  * @index: 0 based bit index (0<=index<64) to sign bit
19748e203e2SMartin Kepplinger  */
sign_extend64(__u64 value,int index)198f80ac98aSJosh Poimboeuf static __always_inline __s64 sign_extend64(__u64 value, int index)
19948e203e2SMartin Kepplinger {
20048e203e2SMartin Kepplinger 	__u8 shift = 63 - index;
20148e203e2SMartin Kepplinger 	return (__s64)(value << shift) >> shift;
20248e203e2SMartin Kepplinger }
20348e203e2SMartin Kepplinger 
fls_long(unsigned long l)2049c313ccdSThorsten Blum static inline unsigned int fls_long(unsigned long l)
205962749afSAndrew Morton {
206962749afSAndrew Morton 	if (sizeof(l) == 4)
207962749afSAndrew Morton 		return fls(l);
208962749afSAndrew Morton 	return fls64(l);
209962749afSAndrew Morton }
210962749afSAndrew Morton 
get_count_order(unsigned int count)211252e5c6eSzijun_hu static inline int get_count_order(unsigned int count)
212252e5c6eSzijun_hu {
213004fba1aSWei Yang 	if (count == 0)
214004fba1aSWei Yang 		return -1;
215252e5c6eSzijun_hu 
216004fba1aSWei Yang 	return fls(--count);
217252e5c6eSzijun_hu }
218252e5c6eSzijun_hu 
219252e5c6eSzijun_hu /**
220252e5c6eSzijun_hu  * get_count_order_long - get order after rounding @l up to power of 2
221252e5c6eSzijun_hu  * @l: parameter
222252e5c6eSzijun_hu  *
223252e5c6eSzijun_hu  * it is same as get_count_order() but with long type parameter
224252e5c6eSzijun_hu  */
get_count_order_long(unsigned long l)225252e5c6eSzijun_hu static inline int get_count_order_long(unsigned long l)
226252e5c6eSzijun_hu {
227252e5c6eSzijun_hu 	if (l == 0UL)
228252e5c6eSzijun_hu 		return -1;
229a9eb6370SWei Yang 	return (int)fls_long(--l);
230252e5c6eSzijun_hu }
231252e5c6eSzijun_hu 
232952043acSSteven Whitehouse /**
233*c320592fSWolfram Sang  * parity8 - get the parity of an u8 value
234*c320592fSWolfram Sang  * @value: the value to be examined
235*c320592fSWolfram Sang  *
236*c320592fSWolfram Sang  * Determine the parity of the u8 argument.
237*c320592fSWolfram Sang  *
238*c320592fSWolfram Sang  * Returns:
239*c320592fSWolfram Sang  * 0 for even parity, 1 for odd parity
240*c320592fSWolfram Sang  *
241*c320592fSWolfram Sang  * Note: This function informs you about the current parity. Example to bail
242*c320592fSWolfram Sang  * out when parity is odd:
243*c320592fSWolfram Sang  *
244*c320592fSWolfram Sang  *	if (parity8(val) == 1)
245*c320592fSWolfram Sang  *		return -EBADMSG;
246*c320592fSWolfram Sang  *
247*c320592fSWolfram Sang  * If you need to calculate a parity bit, you need to draw the conclusion from
248*c320592fSWolfram Sang  * this result yourself. Example to enforce odd parity, parity bit is bit 7:
249*c320592fSWolfram Sang  *
250*c320592fSWolfram Sang  *	if (parity8(val) == 0)
251*c320592fSWolfram Sang  *		val ^= BIT(7);
252*c320592fSWolfram Sang  */
parity8(u8 val)253*c320592fSWolfram Sang static inline int parity8(u8 val)
254*c320592fSWolfram Sang {
255*c320592fSWolfram Sang 	/*
256*c320592fSWolfram Sang 	 * One explanation of this algorithm:
257*c320592fSWolfram Sang 	 * https://funloop.org/codex/problem/parity/README.html
258*c320592fSWolfram Sang 	 */
259*c320592fSWolfram Sang 	val ^= val >> 4;
260*c320592fSWolfram Sang 	return (0x6996 >> (val & 0xf)) & 1;
261*c320592fSWolfram Sang }
262*c320592fSWolfram Sang 
263*c320592fSWolfram Sang /**
264952043acSSteven Whitehouse  * __ffs64 - find first set bit in a 64 bit word
265952043acSSteven Whitehouse  * @word: The 64 bit word
266952043acSSteven Whitehouse  *
2674945cca2SGeert Uytterhoeven  * On 64 bit arches this is a synonym for __ffs
268952043acSSteven Whitehouse  * The result is not defined if no bits are set, so check that @word
269952043acSSteven Whitehouse  * is non-zero before calling this.
270952043acSSteven Whitehouse  */
__ffs64(u64 word)2719c313ccdSThorsten Blum static inline unsigned int __ffs64(u64 word)
272952043acSSteven Whitehouse {
273952043acSSteven Whitehouse #if BITS_PER_LONG == 32
274952043acSSteven Whitehouse 	if (((u32)word) == 0UL)
275952043acSSteven Whitehouse 		return __ffs((u32)(word >> 32)) + 32;
276952043acSSteven Whitehouse #elif BITS_PER_LONG != 64
277952043acSSteven Whitehouse #error BITS_PER_LONG not 32 or 64
278952043acSSteven Whitehouse #endif
279952043acSSteven Whitehouse 	return __ffs((unsigned long)word);
280952043acSSteven Whitehouse }
281952043acSSteven Whitehouse 
2825307e2adSLukas Wunner /**
2833cea8d47SYury Norov  * fns - find N'th set bit in a word
2843cea8d47SYury Norov  * @word: The word to search
2853cea8d47SYury Norov  * @n: Bit to find
2863cea8d47SYury Norov  */
fns(unsigned long word,unsigned int n)2879c313ccdSThorsten Blum static inline unsigned int fns(unsigned long word, unsigned int n)
2883cea8d47SYury Norov {
2891c2aa561SKuan-Wei Chiu 	while (word && n--)
2901c2aa561SKuan-Wei Chiu 		word &= word - 1;
2913cea8d47SYury Norov 
2921c2aa561SKuan-Wei Chiu 	return word ? __ffs(word) : BITS_PER_LONG;
2933cea8d47SYury Norov }
2943cea8d47SYury Norov 
2953cea8d47SYury Norov /**
2965307e2adSLukas Wunner  * assign_bit - Assign value to a bit in memory
2975307e2adSLukas Wunner  * @nr: the bit to set
2985307e2adSLukas Wunner  * @addr: the address to start counting from
2995307e2adSLukas Wunner  * @value: the value to assign
3005307e2adSLukas Wunner  */
3015259401eSAlexander Lobakin #define assign_bit(nr, addr, value)					\
3025259401eSAlexander Lobakin 	((value) ? set_bit((nr), (addr)) : clear_bit((nr), (addr)))
3035307e2adSLukas Wunner 
3045259401eSAlexander Lobakin #define __assign_bit(nr, addr, value)					\
3055259401eSAlexander Lobakin 	((value) ? __set_bit((nr), (addr)) : __clear_bit((nr), (addr)))
3065307e2adSLukas Wunner 
307cb0f8003SKumar Kartikeya Dwivedi /**
308cb0f8003SKumar Kartikeya Dwivedi  * __ptr_set_bit - Set bit in a pointer's value
309cb0f8003SKumar Kartikeya Dwivedi  * @nr: the bit to set
310cb0f8003SKumar Kartikeya Dwivedi  * @addr: the address of the pointer variable
311cb0f8003SKumar Kartikeya Dwivedi  *
312cb0f8003SKumar Kartikeya Dwivedi  * Example:
313cb0f8003SKumar Kartikeya Dwivedi  *	void *p = foo();
314cb0f8003SKumar Kartikeya Dwivedi  *	__ptr_set_bit(bit, &p);
315cb0f8003SKumar Kartikeya Dwivedi  */
316cb0f8003SKumar Kartikeya Dwivedi #define __ptr_set_bit(nr, addr)                         \
317cb0f8003SKumar Kartikeya Dwivedi 	({                                              \
318cb0f8003SKumar Kartikeya Dwivedi 		typecheck_pointer(*(addr));             \
319cb0f8003SKumar Kartikeya Dwivedi 		__set_bit(nr, (unsigned long *)(addr)); \
320cb0f8003SKumar Kartikeya Dwivedi 	})
321cb0f8003SKumar Kartikeya Dwivedi 
322cb0f8003SKumar Kartikeya Dwivedi /**
323cb0f8003SKumar Kartikeya Dwivedi  * __ptr_clear_bit - Clear bit in a pointer's value
324cb0f8003SKumar Kartikeya Dwivedi  * @nr: the bit to clear
325cb0f8003SKumar Kartikeya Dwivedi  * @addr: the address of the pointer variable
326cb0f8003SKumar Kartikeya Dwivedi  *
327cb0f8003SKumar Kartikeya Dwivedi  * Example:
328cb0f8003SKumar Kartikeya Dwivedi  *	void *p = foo();
329cb0f8003SKumar Kartikeya Dwivedi  *	__ptr_clear_bit(bit, &p);
330cb0f8003SKumar Kartikeya Dwivedi  */
331cb0f8003SKumar Kartikeya Dwivedi #define __ptr_clear_bit(nr, addr)                         \
332cb0f8003SKumar Kartikeya Dwivedi 	({                                                \
333cb0f8003SKumar Kartikeya Dwivedi 		typecheck_pointer(*(addr));               \
334cb0f8003SKumar Kartikeya Dwivedi 		__clear_bit(nr, (unsigned long *)(addr)); \
335cb0f8003SKumar Kartikeya Dwivedi 	})
336cb0f8003SKumar Kartikeya Dwivedi 
337cb0f8003SKumar Kartikeya Dwivedi /**
338cb0f8003SKumar Kartikeya Dwivedi  * __ptr_test_bit - Test bit in a pointer's value
339cb0f8003SKumar Kartikeya Dwivedi  * @nr: the bit to test
340cb0f8003SKumar Kartikeya Dwivedi  * @addr: the address of the pointer variable
341cb0f8003SKumar Kartikeya Dwivedi  *
342cb0f8003SKumar Kartikeya Dwivedi  * Example:
343cb0f8003SKumar Kartikeya Dwivedi  *	void *p = foo();
344cb0f8003SKumar Kartikeya Dwivedi  *	if (__ptr_test_bit(bit, &p)) {
345cb0f8003SKumar Kartikeya Dwivedi  *	        ...
346cb0f8003SKumar Kartikeya Dwivedi  *	} else {
347cb0f8003SKumar Kartikeya Dwivedi  *		...
348cb0f8003SKumar Kartikeya Dwivedi  *	}
349cb0f8003SKumar Kartikeya Dwivedi  */
350cb0f8003SKumar Kartikeya Dwivedi #define __ptr_test_bit(nr, addr)                       \
351cb0f8003SKumar Kartikeya Dwivedi 	({                                             \
352cb0f8003SKumar Kartikeya Dwivedi 		typecheck_pointer(*(addr));            \
353cb0f8003SKumar Kartikeya Dwivedi 		test_bit(nr, (unsigned long *)(addr)); \
354cb0f8003SKumar Kartikeya Dwivedi 	})
355cb0f8003SKumar Kartikeya Dwivedi 
35664970b68SAlexander van Heukelum #ifdef __KERNEL__
35777b9bd9cSAlexander van Heukelum 
35800a1a053STheodore Ts'o #ifndef set_mask_bits
35918127429SMiklos Szeredi #define set_mask_bits(ptr, mask, bits)	\
36000a1a053STheodore Ts'o ({								\
36118127429SMiklos Szeredi 	const typeof(*(ptr)) mask__ = (mask), bits__ = (bits);	\
36218127429SMiklos Szeredi 	typeof(*(ptr)) old__, new__;				\
36300a1a053STheodore Ts'o 								\
36418127429SMiklos Szeredi 	old__ = READ_ONCE(*(ptr));				\
365e1d7c760SUros Bizjak 	do {							\
36618127429SMiklos Szeredi 		new__ = (old__ & ~mask__) | bits__;		\
367e1d7c760SUros Bizjak 	} while (!try_cmpxchg(ptr, &old__, new__));		\
36800a1a053STheodore Ts'o 								\
3691db604f6SVineet Gupta 	old__;							\
37000a1a053STheodore Ts'o })
37100a1a053STheodore Ts'o #endif
37200a1a053STheodore Ts'o 
37385ad1d13SGuoqing Jiang #ifndef bit_clear_unless
374edfa8728SMiklos Szeredi #define bit_clear_unless(ptr, clear, test)	\
37585ad1d13SGuoqing Jiang ({								\
376edfa8728SMiklos Szeredi 	const typeof(*(ptr)) clear__ = (clear), test__ = (test);\
377edfa8728SMiklos Szeredi 	typeof(*(ptr)) old__, new__;				\
37885ad1d13SGuoqing Jiang 								\
379edfa8728SMiklos Szeredi 	old__ = READ_ONCE(*(ptr));				\
380e1d7c760SUros Bizjak 	do {							\
381e1d7c760SUros Bizjak 		if (old__ & test__)				\
382e1d7c760SUros Bizjak 			break;					\
383edfa8728SMiklos Szeredi 		new__ = old__ & ~clear__;			\
384e1d7c760SUros Bizjak 	} while (!try_cmpxchg(ptr, &old__, new__));		\
38585ad1d13SGuoqing Jiang 								\
386edfa8728SMiklos Szeredi 	!(old__ & test__);					\
38785ad1d13SGuoqing Jiang })
38885ad1d13SGuoqing Jiang #endif
38985ad1d13SGuoqing Jiang 
39064970b68SAlexander van Heukelum #endif /* __KERNEL__ */
3911da177e4SLinus Torvalds #endif
392