|
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
|
|
20 | 21 | */
|
21 | 22 | #if !defined(__ASSEMBLY__)
|
22 | 23 |
|
| 24 | +/* |
| 25 | + * Missing asm support |
| 26 | + * |
| 27 | + * GENMASK_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 | + |
23 | 32 | #include <linux/build_bug.h>
|
24 | 33 | #include <linux/compiler.h>
|
| 34 | +#include <linux/overflow.h> |
25 | 35 |
|
26 | 36 | #define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(const_true((l) > (h)))
|
27 | 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 | + |
28 | 58 | #else /* defined(__ASSEMBLY__) */
|
29 | 59 |
|
30 | 60 | /*
|
|
0 commit comments