XenevaOS
Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
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, &reg, c);
45 */
46
47#define __bf_shf __builtin_ctzll
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
108#define FIELD_MAX(_mask) \
109 ({ \
110 __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
111 (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
112 })
113
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
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
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
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
189#define FIELD_GET_SIGNED(mask, reg) \
190 ({ \
191 __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \
192 ((__signed_scalar_typeof(mask)) \
193 (((long long)(reg) << __builtin_clzll(mask)) >> \
194 (__builtin_clzll(mask) + __builtin_ctzll(mask)))); \
195 })
196
206#define FIELD_MODIFY(_mask, _reg_p, _val) \
207 ({ \
208 typecheck_pointer(_reg_p); \
209 __BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: "); \
210 *(_reg_p) &= ~(_mask); \
211 *(_reg_p) |= (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)); \
212 })
213
214extern void __compiletime_error("value doesn't fit into mask")
215__field_overflow(void);
216extern void __compiletime_error("bad bitfield mask")
217__bad_mask(void);
218static __always_inline u64 field_multiplier(u64 field)
219{
220 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
221 __bad_mask();
222 return field & -field;
223}
224static __always_inline u64 field_mask(u64 field)
225{
226 return field / field_multiplier(field);
227}
228#define field_max(field) ((typeof(field))field_mask(field))
229#define ____MAKE_OP(type,base,to,from) \
230static __always_inline __##type __must_check type##_encode_bits(base v, base field) \
231{ \
232 if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
233 __field_overflow(); \
234 return to((v & field_mask(field)) * field_multiplier(field)); \
235} \
236static __always_inline __##type __must_check type##_replace_bits(__##type old, \
237 base val, base field) \
238{ \
239 return (old & ~to(field)) | type##_encode_bits(val, field); \
240} \
241static __always_inline void type##p_replace_bits(__##type *p, \
242 base val, base field) \
243{ \
244 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
245} \
246static __always_inline base __must_check type##_get_bits(__##type v, base field) \
247{ \
248 return (from(v) & field)/field_multiplier(field); \
249}
250#define __MAKE_OP(size) \
251 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
252 ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
253 ____MAKE_OP(u##size,u##size,,)
255__MAKE_OP(16)
256__MAKE_OP(32)
257__MAKE_OP(64)
258#undef __MAKE_OP
259#undef ____MAKE_OP
260
261#define __field_prep(mask, val) \
262 ({ \
263 auto __mask = (mask); \
264 typeof(__mask) __val = (val); \
265 unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
266 __ffs(__mask) : __ffs64(__mask); \
267 (__val << __shift) & __mask; \
268 })
269
270#define __field_get(mask, reg) \
271 ({ \
272 auto __mask = (mask); \
273 typeof(__mask) __reg = (reg); \
274 unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
275 __ffs(__mask) : __ffs64(__mask); \
276 (__reg & __mask) >> __shift; \
277 })
278
295#define field_prep(mask, val) \
296 (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \
297 : __field_prep(mask, val))
298
315#define field_get(mask, reg) \
316 (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \
317 : __field_get(mask, reg))
318
319#endif
#define ____MAKE_OP(type, base, to, from)
Definition bitfield.h:229
#define __MAKE_OP(size)
Definition bitfield.h:250
#define __compiletime_error(msg)
Definition compiler_attributes.h:140
#define __always_inline
Definition compiler_attributes.h:55
uint64_t u64
Definition kernel.h:17
uint8_t u8
Definition kernel.h:14