Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
4 * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
5 */
6
7#ifndef _LINUX_BITFIELD_H
8#define _LINUX_BITFIELD_H
9
10#include <linux/build_bug.h>
11#include <linux/compiler.h>
12#include <linux/typecheck.h>
13#include <asm/byteorder.h>
14
15/*
16 * Bitfield access macros
17 *
18 * FIELD_{GET,PREP} macros take as first parameter shifted mask
19 * from which they extract the base mask and shift amount.
20 * Mask must be a compilation time constant.
21 * field_{get,prep} are variants that take a non-const mask.
22 *
23 * Example:
24 *
25 * #include <linux/bitfield.h>
26 * #include <linux/bits.h>
27 *
28 * #define REG_FIELD_A GENMASK(6, 0)
29 * #define REG_FIELD_B BIT(7)
30 * #define REG_FIELD_C GENMASK(15, 8)
31 * #define REG_FIELD_D GENMASK(31, 16)
32 *
33 * Get:
34 * a = FIELD_GET(REG_FIELD_A, reg);
35 * b = FIELD_GET(REG_FIELD_B, reg);
36 *
37 * Set:
38 * reg = FIELD_PREP(REG_FIELD_A, 1) |
39 * FIELD_PREP(REG_FIELD_B, 0) |
40 * FIELD_PREP(REG_FIELD_C, c) |
41 * FIELD_PREP(REG_FIELD_D, 0x40);
42 *
43 * Modify:
44 * FIELD_MODIFY(REG_FIELD_C, ®, c);
45 */
46
47#define __bf_shf(x) (__builtin_ffsll(x) - 1)
48
49#define __scalar_type_to_unsigned_cases(type) \
50 unsigned type: (unsigned type)0, \
51 signed type: (unsigned type)0
52
53#define __unsigned_scalar_typeof(x) typeof( \
54 _Generic((x), \
55 char: (unsigned char)0, \
56 __scalar_type_to_unsigned_cases(char), \
57 __scalar_type_to_unsigned_cases(short), \
58 __scalar_type_to_unsigned_cases(int), \
59 __scalar_type_to_unsigned_cases(long), \
60 __scalar_type_to_unsigned_cases(long long), \
61 default: (x)))
62
63#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
64
65#define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \
66 ({ \
67 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
68 _pfx "mask is not constant"); \
69 BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
70 BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
71 ~((_mask) >> __bf_shf(_mask)) & \
72 (0 + (_val)) : 0, \
73 _pfx "value too large for the field"); \
74 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
75 (1ULL << __bf_shf(_mask))); \
76 })
77
78#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \
79 BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \
80 __bf_cast_unsigned(reg, ~0ull), \
81 pfx "type of reg too small for mask")
82
83#define __BF_FIELD_CHECK(mask, reg, val, pfx) \
84 ({ \
85 __BF_FIELD_CHECK_MASK(mask, val, pfx); \
86 __BF_FIELD_CHECK_REG(mask, reg, pfx); \
87 })
88
89#define __FIELD_PREP(mask, val, pfx) \
90 ({ \
91 __BF_FIELD_CHECK_MASK(mask, val, pfx); \
92 ((typeof(mask))(val) << __bf_shf(mask)) & (mask); \
93 })
94
95#define __FIELD_GET(mask, reg, pfx) \
96 ({ \
97 __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \
98 (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \
99 })
100
101/**
102 * FIELD_MAX() - produce the maximum value representable by a field
103 * @_mask: shifted mask defining the field's length and position
104 *
105 * FIELD_MAX() returns the maximum value that can be held in the field
106 * specified by @_mask.
107 */
108#define FIELD_MAX(_mask) \
109 ({ \
110 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
111 (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
112 })
113
114/**
115 * FIELD_FIT() - check if value fits in the field
116 * @_mask: shifted mask defining the field's length and position
117 * @_val: value to test against the field
118 *
119 * Return: true if @_val can fit inside @_mask, false if @_val is too big.
120 */
121#define FIELD_FIT(_mask, _val) \
122 ({ \
123 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
124 !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
125 })
126
127/**
128 * FIELD_PREP() - prepare a bitfield element
129 * @_mask: shifted mask defining the field's length and position
130 * @_val: value to put in the field
131 *
132 * FIELD_PREP() masks and shifts up the value. The result should
133 * be combined with other fields of the bitfield using logical OR.
134 */
135#define FIELD_PREP(_mask, _val) \
136 ({ \
137 __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \
138 __FIELD_PREP(_mask, _val, "FIELD_PREP: "); \
139 })
140
141#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
142
143/**
144 * FIELD_PREP_CONST() - prepare a constant bitfield element
145 * @_mask: shifted mask defining the field's length and position
146 * @_val: value to put in the field
147 *
148 * FIELD_PREP_CONST() masks and shifts up the value. The result should
149 * be combined with other fields of the bitfield using logical OR.
150 *
151 * Unlike FIELD_PREP() this is a constant expression and can therefore
152 * be used in initializers. Error checking is less comfortable for this
153 * version, and non-constant masks cannot be used.
154 */
155#define FIELD_PREP_CONST(_mask, _val) \
156 ( \
157 /* mask must be non-zero */ \
158 BUILD_BUG_ON_ZERO((_mask) == 0) + \
159 /* check if value fits */ \
160 BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
161 /* check if mask is contiguous */ \
162 __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) + \
163 /* and create the value */ \
164 (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)) \
165 )
166
167/**
168 * FIELD_GET() - extract a bitfield element
169 * @_mask: shifted mask defining the field's length and position
170 * @_reg: value of entire bitfield
171 *
172 * FIELD_GET() extracts the field specified by @_mask from the
173 * bitfield passed in as @_reg by masking and shifting it down.
174 */
175#define FIELD_GET(_mask, _reg) \
176 ({ \
177 __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \
178 __FIELD_GET(_mask, _reg, "FIELD_GET: "); \
179 })
180
181/**
182 * FIELD_MODIFY() - modify a bitfield element
183 * @_mask: shifted mask defining the field's length and position
184 * @_reg_p: pointer to the memory that should be updated
185 * @_val: value to store in the bitfield
186 *
187 * FIELD_MODIFY() modifies the set of bits in @_reg_p specified by @_mask,
188 * by replacing them with the bitfield value passed in as @_val.
189 */
190#define FIELD_MODIFY(_mask, _reg_p, _val) \
191 ({ \
192 typecheck_pointer(_reg_p); \
193 __BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: "); \
194 *(_reg_p) &= ~(_mask); \
195 *(_reg_p) |= (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)); \
196 })
197
198extern void __compiletime_error("value doesn't fit into mask")
199__field_overflow(void);
200extern void __compiletime_error("bad bitfield mask")
201__bad_mask(void);
202static __always_inline u64 field_multiplier(u64 field)
203{
204 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
205 __bad_mask();
206 return field & -field;
207}
208static __always_inline u64 field_mask(u64 field)
209{
210 return field / field_multiplier(field);
211}
212#define field_max(field) ((typeof(field))field_mask(field))
213#define ____MAKE_OP(type,base,to,from) \
214static __always_inline __##type __must_check type##_encode_bits(base v, base field) \
215{ \
216 if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
217 __field_overflow(); \
218 return to((v & field_mask(field)) * field_multiplier(field)); \
219} \
220static __always_inline __##type __must_check type##_replace_bits(__##type old, \
221 base val, base field) \
222{ \
223 return (old & ~to(field)) | type##_encode_bits(val, field); \
224} \
225static __always_inline void type##p_replace_bits(__##type *p, \
226 base val, base field) \
227{ \
228 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
229} \
230static __always_inline base __must_check type##_get_bits(__##type v, base field) \
231{ \
232 return (from(v) & field)/field_multiplier(field); \
233}
234#define __MAKE_OP(size) \
235 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
236 ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
237 ____MAKE_OP(u##size,u##size,,)
238____MAKE_OP(u8,u8,,)
239__MAKE_OP(16)
240__MAKE_OP(32)
241__MAKE_OP(64)
242#undef __MAKE_OP
243#undef ____MAKE_OP
244
245#define __field_prep(mask, val) \
246 ({ \
247 auto __mask = (mask); \
248 typeof(__mask) __val = (val); \
249 unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
250 __ffs(__mask) : __ffs64(__mask); \
251 (__val << __shift) & __mask; \
252 })
253
254#define __field_get(mask, reg) \
255 ({ \
256 auto __mask = (mask); \
257 typeof(__mask) __reg = (reg); \
258 unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
259 __ffs(__mask) : __ffs64(__mask); \
260 (__reg & __mask) >> __shift; \
261 })
262
263/**
264 * field_prep() - prepare a bitfield element
265 * @mask: shifted mask defining the field's length and position, must be
266 * non-zero
267 * @val: value to put in the field
268 *
269 * Return: field value masked and shifted to its final destination
270 *
271 * field_prep() masks and shifts up the value. The result should be
272 * combined with other fields of the bitfield using logical OR.
273 * Unlike FIELD_PREP(), @mask is not limited to a compile-time constant.
274 * Typical usage patterns are a value stored in a table, or calculated by
275 * shifting a constant by a variable number of bits.
276 * If you want to ensure that @mask is a compile-time constant, please use
277 * FIELD_PREP() directly instead.
278 */
279#define field_prep(mask, val) \
280 (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \
281 : __field_prep(mask, val))
282
283/**
284 * field_get() - extract a bitfield element
285 * @mask: shifted mask defining the field's length and position, must be
286 * non-zero
287 * @reg: value of entire bitfield
288 *
289 * Return: extracted field value
290 *
291 * field_get() extracts the field specified by @mask from the
292 * bitfield passed in as @reg by masking and shifting it down.
293 * Unlike FIELD_GET(), @mask is not limited to a compile-time constant.
294 * Typical usage patterns are a value stored in a table, or calculated by
295 * shifting a constant by a variable number of bits.
296 * If you want to ensure that @mask is a compile-time constant, please use
297 * FIELD_GET() directly instead.
298 */
299#define field_get(mask, reg) \
300 (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \
301 : __field_get(mask, reg))
302
303#endif