1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef _LINUX_BITFIELD_H
16#define _LINUX_BITFIELD_H
17
18#include <linux/build_bug.h>
19#include <asm/byteorder.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#define __bf_shf(x) (__builtin_ffsll(x) - 1)
51
52#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
53 ({ \
54 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
55 _pfx "mask is not constant"); \
56 BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
57 BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
58 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
59 _pfx "value too large for the field"); \
60 BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
61 _pfx "type of reg too small for mask"); \
62 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
63 (1ULL << __bf_shf(_mask))); \
64 })
65
66
67
68
69
70
71
72
73#define FIELD_FIT(_mask, _val) \
74 ({ \
75 __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \
76 !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
77 })
78
79
80
81
82
83
84
85
86
87#define FIELD_PREP(_mask, _val) \
88 ({ \
89 __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
90 ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
91 })
92
93
94
95
96
97
98
99
100
101#define FIELD_GET(_mask, _reg) \
102 ({ \
103 __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
104 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
105 })
106
107extern void __compiletime_warning("value doesn't fit into mask")
108__field_overflow(void);
109extern void __compiletime_error("bad bitfield mask")
110__bad_mask(void);
111static __always_inline u64 field_multiplier(u64 field)
112{
113 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
114 __bad_mask();
115 return field & -field;
116}
117static __always_inline u64 field_mask(u64 field)
118{
119 return field / field_multiplier(field);
120}
121#define ____MAKE_OP(type,base,to,from) \
122static __always_inline __##type type##_encode_bits(base v, base field) \
123{ \
124 if (__builtin_constant_p(v) && (v & ~field_multiplier(field))) \
125 __field_overflow(); \
126 return to((v & field_mask(field)) * field_multiplier(field)); \
127} \
128static __always_inline __##type type##_replace_bits(__##type old, \
129 base val, base field) \
130{ \
131 return (old & ~to(field)) | type##_encode_bits(val, field); \
132} \
133static __always_inline void type##p_replace_bits(__##type *p, \
134 base val, base field) \
135{ \
136 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
137} \
138static __always_inline base type##_get_bits(__##type v, base field) \
139{ \
140 return (from(v) & field)/field_multiplier(field); \
141}
142#define __MAKE_OP(size) \
143 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
144 ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
145 ____MAKE_OP(u##size,u##size,,)
146__MAKE_OP(16)
147__MAKE_OP(32)
148__MAKE_OP(64)
149#undef __MAKE_OP
150#undef ____MAKE_OP
151
152#endif
153