blob: 87d112650dfbb0ec9ac6825ae2746c61b2659960 [file] [log] [blame]
Will Deacon8bd9cb52018-06-19 13:53:08 +01001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_BITS_H
3#define __LINUX_BITS_H
Masahiro Yamada95b980d2019-07-16 16:26:57 -07004
5#include <linux/const.h>
Vincenzo Frascino3945ff32020-03-20 14:53:27 +00006#include <vdso/bits.h>
Will Deacon8bd9cb52018-06-19 13:53:08 +01007#include <asm/bitsperlong.h>
8
Masahiro Yamada95b980d2019-07-16 16:26:57 -07009#define BIT_ULL(nr) (ULL(1) << (nr))
10#define BIT_MASK(nr) (UL(1) << ((nr) % BITS_PER_LONG))
Will Deacon8bd9cb52018-06-19 13:53:08 +010011#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Masahiro Yamada95b980d2019-07-16 16:26:57 -070012#define BIT_ULL_MASK(nr) (ULL(1) << ((nr) % BITS_PER_LONG_LONG))
Will Deacon8bd9cb52018-06-19 13:53:08 +010013#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
14#define BITS_PER_BYTE 8
15
16/*
17 * Create a contiguous bitmask starting at bit position @l and ending at
18 * position @h. For example
19 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
20 */
Linus Torvalds6ec44762020-07-08 10:48:35 -070021#if !defined(__ASSEMBLY__)
Rikard Falkeborn295bcca2020-04-06 20:10:38 -070022#include <linux/build_bug.h>
23#define GENMASK_INPUT_CHECK(h, l) \
24 (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
Rikard Falkeborn1354ec82021-05-22 17:42:02 -070025 __is_constexpr((l) > (h)), (l) > (h), 0)))
Rikard Falkeborn295bcca2020-04-06 20:10:38 -070026#else
27/*
28 * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
29 * disable the input check if that is the case.
30 */
31#define GENMASK_INPUT_CHECK(h, l) 0
32#endif
33
34#define __GENMASK(h, l) \
Masahiro Yamada95b980d2019-07-16 16:26:57 -070035 (((~UL(0)) - (UL(1) << (l)) + 1) & \
36 (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
Rikard Falkeborn295bcca2020-04-06 20:10:38 -070037#define GENMASK(h, l) \
38 (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
Will Deacon8bd9cb52018-06-19 13:53:08 +010039
Rikard Falkeborn295bcca2020-04-06 20:10:38 -070040#define __GENMASK_ULL(h, l) \
Masahiro Yamada95b980d2019-07-16 16:26:57 -070041 (((~ULL(0)) - (ULL(1) << (l)) + 1) & \
42 (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
Rikard Falkeborn295bcca2020-04-06 20:10:38 -070043#define GENMASK_ULL(h, l) \
44 (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
Will Deacon8bd9cb52018-06-19 13:53:08 +010045
46#endif /* __LINUX_BITS_H */