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