Skip to content

Commit 95b980d

Browse files
masahir0ytorvalds
authored andcommitted
linux/bits.h: make BIT(), GENMASK(), and friends available in assembly
BIT(), GENMASK(), etc. are useful to define register bits of hardware. However, low-level code is often written in assembly, where they are not available due to the hard-coded 1UL, 0UL. In fact, in-kernel headers such as arch/arm64/include/asm/sysreg.h use _BITUL() instead of BIT() so that the register bit macros are available in assembly. Using macros in include/uapi/linux/const.h have two reasons: [1] For use in uapi headers We should use underscore-prefixed variants for user-space. [2] For use in assembly code Since _BITUL() uses UL(1) instead of 1UL, it can be used as an alternative of BIT(). For [2], it is pretty easy to change BIT() etc. for use in assembly. This allows to replace _BUTUL() in kernel-space headers with BIT(). Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Masahiro Yamada <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Christian Borntraeger <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: Vasily Gorbik <[email protected]> Cc: Vineet Gupta <[email protected]> Cc: Will Deacon <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 65f50f2 commit 95b980d

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

include/linux/bits.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#ifndef __LINUX_BITS_H
33
#define __LINUX_BITS_H
4+
5+
#include <linux/const.h>
46
#include <asm/bitsperlong.h>
57

6-
#define BIT(nr) (1UL << (nr))
7-
#define BIT_ULL(nr) (1ULL << (nr))
8-
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
8+
#define BIT(nr) (UL(1) << (nr))
9+
#define BIT_ULL(nr) (ULL(1) << (nr))
10+
#define BIT_MASK(nr) (UL(1) << ((nr) % BITS_PER_LONG))
911
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
10-
#define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
12+
#define BIT_ULL_MASK(nr) (ULL(1) << ((nr) % BITS_PER_LONG_LONG))
1113
#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
1214
#define BITS_PER_BYTE 8
1315

@@ -17,10 +19,11 @@
1719
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
1820
*/
1921
#define GENMASK(h, l) \
20-
(((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
22+
(((~UL(0)) - (UL(1) << (l)) + 1) & \
23+
(~UL(0) >> (BITS_PER_LONG - 1 - (h))))
2124

2225
#define GENMASK_ULL(h, l) \
23-
(((~0ULL) - (1ULL << (l)) + 1) & \
24-
(~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
26+
(((~ULL(0)) - (ULL(1) << (l)) + 1) & \
27+
(~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
2528

2629
#endif /* __LINUX_BITS_H */

0 commit comments

Comments
 (0)