1 /* SPDX-License-Identifier: GPL-2.0-only */
7 #ifndef _LINUX_BITFIELD_H
8 #define _LINUX_BITFIELD_H
10 #include <linux/build_bug.h>
11 #include <asm/byteorder.h>
14 * Bitfield access macros
16 * FIELD_{GET,PREP} macros take as first parameter shifted mask
17 * from which they extract the base mask and shift amount.
18 * Mask must be a compilation time constant.
22 * #include <linux/bitfield.h>
23 * #include <linux/bits.h>
25 * #define REG_FIELD_A GENMASK(6, 0)
26 * #define REG_FIELD_B BIT(7)
27 * #define REG_FIELD_C GENMASK(15, 8)
28 * #define REG_FIELD_D GENMASK(31, 16)
31 * a = FIELD_GET(REG_FIELD_A, reg);
32 * b = FIELD_GET(REG_FIELD_B, reg);
35 * reg = FIELD_PREP(REG_FIELD_A, 1) |
36 * FIELD_PREP(REG_FIELD_B, 0) |
37 * FIELD_PREP(REG_FIELD_C, c) |
38 * FIELD_PREP(REG_FIELD_D, 0x40);
41 * reg &= ~REG_FIELD_C;
42 * reg |= FIELD_PREP(REG_FIELD_C, c);
45 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
47 #define __scalar_type_to_unsigned_cases(type) \
48 unsigned type: (unsigned type)0, \
49 signed type: (unsigned type)0
51 #define __unsigned_scalar_typeof(x) typeof( \
53 char: (unsigned char)0, \
54 __scalar_type_to_unsigned_cases(char), \
55 __scalar_type_to_unsigned_cases(short), \
56 __scalar_type_to_unsigned_cases(int), \
57 __scalar_type_to_unsigned_cases(long), \
58 __scalar_type_to_unsigned_cases(long long), \
61 #define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
63 #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
65 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
66 _pfx "mask is not constant"); \
67 BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
68 BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
69 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
70 _pfx "value too large for the field"); \
71 BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \
72 __bf_cast_unsigned(_reg, ~0ull), \
73 _pfx "type of reg too small for mask"); \
74 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
75 (1ULL << __bf_shf(_mask))); \
79 * FIELD_MAX() - produce the maximum value representable by a field
80 * @_mask: shifted mask defining the field's length and position
82 * FIELD_MAX() returns the maximum value that can be held in the field
83 * specified by @_mask.
85 #define FIELD_MAX(_mask) \
87 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
88 (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
92 * FIELD_FIT() - check if value fits in the field
93 * @_mask: shifted mask defining the field's length and position
94 * @_val: value to test against the field
96 * Return: true if @_val can fit inside @_mask, false if @_val is too big.
98 #define FIELD_FIT(_mask, _val) \
100 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
101 !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
105 * FIELD_PREP() - prepare a bitfield element
106 * @_mask: shifted mask defining the field's length and position
107 * @_val: value to put in the field
109 * FIELD_PREP() masks and shifts up the value. The result should
110 * be combined with other fields of the bitfield using logical OR.
112 #define FIELD_PREP(_mask, _val) \
114 __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
115 ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
119 * FIELD_GET() - extract a bitfield element
120 * @_mask: shifted mask defining the field's length and position
121 * @_reg: value of entire bitfield
123 * FIELD_GET() extracts the field specified by @_mask from the
124 * bitfield passed in as @_reg by masking and shifting it down.
126 #define FIELD_GET(_mask, _reg) \
128 __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
129 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
132 extern void __compiletime_error("value doesn't fit into mask")
133 __field_overflow(void);
134 extern void __compiletime_error("bad bitfield mask")
136 static __always_inline u64 field_multiplier(u64 field)
138 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
140 return field & -field;
142 static __always_inline u64 field_mask(u64 field)
144 return field / field_multiplier(field);
146 #define field_max(field) ((typeof(field))field_mask(field))
147 #define ____MAKE_OP(type,base,to,from) \
148 static __always_inline __##type type##_encode_bits(base v, base field) \
150 if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
151 __field_overflow(); \
152 return to((v & field_mask(field)) * field_multiplier(field)); \
154 static __always_inline __##type type##_replace_bits(__##type old, \
155 base val, base field) \
157 return (old & ~to(field)) | type##_encode_bits(val, field); \
159 static __always_inline void type##p_replace_bits(__##type *p, \
160 base val, base field) \
162 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
164 static __always_inline base type##_get_bits(__##type v, base field) \
166 return (from(v) & field)/field_multiplier(field); \
168 #define __MAKE_OP(size) \
169 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
170 ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
171 ____MAKE_OP(u##size,u##size,,)