|
12 | 12 | #define BIT_ULL_MASK(nr) (ULL(1) << ((nr) % BITS_PER_LONG_LONG))
|
13 | 13 | #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
|
14 | 14 | #define BITS_PER_BYTE 8
|
| 15 | +#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) |
15 | 16 |
|
16 | 17 | /*
|
17 | 18 | * Create a contiguous bitmask starting at bit position @l and ending at
|
18 | 19 | * position @h. For example
|
19 | 20 | * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
|
20 | 21 | */
|
21 | 22 | #if !defined(__ASSEMBLY__)
|
| 23 | + |
| 24 | +/* |
| 25 | + * Missing asm support |
| 26 | + * |
| 27 | + * GENMASK_U*() and BIT_U*() depend on BITS_PER_TYPE() which relies on sizeof(), |
| 28 | + * something not available in asm. Nevertheless, fixed width integers is a C |
| 29 | + * concept. Assembly code can rely on the long and long long versions instead. |
| 30 | + */ |
| 31 | + |
22 | 32 | #include <linux/build_bug.h>
|
23 | 33 | #include <linux/compiler.h>
|
| 34 | +#include <linux/overflow.h> |
| 35 | + |
24 | 36 | #define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(const_true((l) > (h)))
|
25 |
| -#else |
| 37 | + |
| 38 | +/* |
| 39 | + * Generate a mask for the specified type @t. Additional checks are made to |
| 40 | + * guarantee the value returned fits in that type, relying on |
| 41 | + * -Wshift-count-overflow compiler check to detect incompatible arguments. |
| 42 | + * For example, all these create build errors or warnings: |
| 43 | + * |
| 44 | + * - GENMASK(15, 20): wrong argument order |
| 45 | + * - GENMASK(72, 15): doesn't fit unsigned long |
| 46 | + * - GENMASK_U32(33, 15): doesn't fit in a u32 |
| 47 | + */ |
| 48 | +#define GENMASK_TYPE(t, h, l) \ |
| 49 | + ((t)(GENMASK_INPUT_CHECK(h, l) + \ |
| 50 | + (type_max(t) << (l) & \ |
| 51 | + type_max(t) >> (BITS_PER_TYPE(t) - 1 - (h))))) |
| 52 | + |
| 53 | +#define GENMASK_U8(h, l) GENMASK_TYPE(u8, h, l) |
| 54 | +#define GENMASK_U16(h, l) GENMASK_TYPE(u16, h, l) |
| 55 | +#define GENMASK_U32(h, l) GENMASK_TYPE(u32, h, l) |
| 56 | +#define GENMASK_U64(h, l) GENMASK_TYPE(u64, h, l) |
| 57 | + |
| 58 | +/* |
| 59 | + * Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The |
| 60 | + * following examples generate compiler warnings due to -Wshift-count-overflow: |
| 61 | + * |
| 62 | + * - BIT_U8(8) |
| 63 | + * - BIT_U32(-1) |
| 64 | + * - BIT_U32(40) |
| 65 | + */ |
| 66 | +#define BIT_INPUT_CHECK(type, nr) \ |
| 67 | + BUILD_BUG_ON_ZERO(const_true((nr) >= BITS_PER_TYPE(type))) |
| 68 | + |
| 69 | +#define BIT_TYPE(type, nr) ((type)(BIT_INPUT_CHECK(type, nr) + BIT_ULL(nr))) |
| 70 | + |
| 71 | +#define BIT_U8(nr) BIT_TYPE(u8, nr) |
| 72 | +#define BIT_U16(nr) BIT_TYPE(u16, nr) |
| 73 | +#define BIT_U32(nr) BIT_TYPE(u32, nr) |
| 74 | +#define BIT_U64(nr) BIT_TYPE(u64, nr) |
| 75 | + |
| 76 | +#else /* defined(__ASSEMBLY__) */ |
| 77 | + |
26 | 78 | /*
|
27 | 79 | * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
|
28 | 80 | * disable the input check if that is the case.
|
29 | 81 | */
|
30 | 82 | #define GENMASK_INPUT_CHECK(h, l) 0
|
31 |
| -#endif |
| 83 | + |
| 84 | +#endif /* !defined(__ASSEMBLY__) */ |
32 | 85 |
|
33 | 86 | #define GENMASK(h, l) \
|
34 | 87 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
|
|
0 commit comments