xref: /linux/tools/include/linux/bitfield.h (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
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/kernel.h>
12 #include <asm/byteorder.h>
13 
14 /*
15  * Bitfield access macros
16  *
17  * FIELD_{GET,PREP} macros take as first parameter shifted mask
18  * from which they extract the base mask and shift amount.
19  * Mask must be a compilation time constant.
20  *
21  * Example:
22  *
23  *  #define REG_FIELD_A  GENMASK(6, 0)
24  *  #define REG_FIELD_B  BIT(7)
25  *  #define REG_FIELD_C  GENMASK(15, 8)
26  *  #define REG_FIELD_D  GENMASK(31, 16)
27  *
28  * Get:
29  *  a = FIELD_GET(REG_FIELD_A, reg);
30  *  b = FIELD_GET(REG_FIELD_B, reg);
31  *
32  * Set:
33  *  reg = FIELD_PREP(REG_FIELD_A, 1) |
34  *	  FIELD_PREP(REG_FIELD_B, 0) |
35  *	  FIELD_PREP(REG_FIELD_C, c) |
36  *	  FIELD_PREP(REG_FIELD_D, 0x40);
37  *
38  * Modify:
39  *  reg &= ~REG_FIELD_C;
40  *  reg |= FIELD_PREP(REG_FIELD_C, c);
41  */
42 
43 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
44 
45 #define __scalar_type_to_unsigned_cases(type)				\
46 		unsigned type:	(unsigned type)0,			\
47 		signed type:	(unsigned type)0
48 
49 #define __unsigned_scalar_typeof(x) typeof(				\
50 		_Generic((x),						\
51 			char:	(unsigned char)0,			\
52 			__scalar_type_to_unsigned_cases(char),		\
53 			__scalar_type_to_unsigned_cases(short),		\
54 			__scalar_type_to_unsigned_cases(int),		\
55 			__scalar_type_to_unsigned_cases(long),		\
56 			__scalar_type_to_unsigned_cases(long long),	\
57 			default: (x)))
58 
59 #define __bf_cast_unsigned(type, x)	((__unsigned_scalar_typeof(type))(x))
60 
61 #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx)			\
62 	({								\
63 		BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask),		\
64 				 _pfx "mask is not constant");		\
65 		BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");	\
66 		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
67 				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
68 				 _pfx "value too large for the field"); \
69 		BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) >	\
70 				 __bf_cast_unsigned(_reg, ~0ull),	\
71 				 _pfx "type of reg too small for mask"); \
72 		__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) +			\
73 					      (1ULL << __bf_shf(_mask))); \
74 	})
75 
76 /**
77  * FIELD_MAX() - produce the maximum value representable by a field
78  * @_mask: shifted mask defining the field's length and position
79  *
80  * FIELD_MAX() returns the maximum value that can be held in the field
81  * specified by @_mask.
82  */
83 #define FIELD_MAX(_mask)						\
84 	({								\
85 		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: ");	\
86 		(typeof(_mask))((_mask) >> __bf_shf(_mask));		\
87 	})
88 
89 /**
90  * FIELD_FIT() - check if value fits in the field
91  * @_mask: shifted mask defining the field's length and position
92  * @_val:  value to test against the field
93  *
94  * Return: true if @_val can fit inside @_mask, false if @_val is too big.
95  */
96 #define FIELD_FIT(_mask, _val)						\
97 	({								\
98 		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: ");	\
99 		!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
100 	})
101 
102 /**
103  * FIELD_PREP() - prepare a bitfield element
104  * @_mask: shifted mask defining the field's length and position
105  * @_val:  value to put in the field
106  *
107  * FIELD_PREP() masks and shifts up the value.  The result should
108  * be combined with other fields of the bitfield using logical OR.
109  */
110 #define FIELD_PREP(_mask, _val)						\
111 	({								\
112 		__BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: ");	\
113 		((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask);	\
114 	})
115 
116 /**
117  * FIELD_GET() - extract a bitfield element
118  * @_mask: shifted mask defining the field's length and position
119  * @_reg:  value of entire bitfield
120  *
121  * FIELD_GET() extracts the field specified by @_mask from the
122  * bitfield passed in as @_reg by masking and shifting it down.
123  */
124 #define FIELD_GET(_mask, _reg)						\
125 	({								\
126 		__BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: ");	\
127 		(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask));	\
128 	})
129 
130 extern void __compiletime_error("value doesn't fit into mask")
131 __field_overflow(void);
132 extern void __compiletime_error("bad bitfield mask")
133 __bad_mask(void);
field_multiplier(u64 field)134 static __always_inline u64 field_multiplier(u64 field)
135 {
136 	if ((field | (field - 1)) & ((field | (field - 1)) + 1))
137 		__bad_mask();
138 	return field & -field;
139 }
field_mask(u64 field)140 static __always_inline u64 field_mask(u64 field)
141 {
142 	return field / field_multiplier(field);
143 }
144 #define field_max(field)	((typeof(field))field_mask(field))
145 #define ____MAKE_OP(type,base,to,from)					\
146 static __always_inline __##type type##_encode_bits(base v, base field)	\
147 {									\
148 	if (__builtin_constant_p(v) && (v & ~field_mask(field)))	\
149 		__field_overflow();					\
150 	return to((v & field_mask(field)) * field_multiplier(field));	\
151 }									\
152 static __always_inline __##type type##_replace_bits(__##type old,	\
153 					base val, base field)		\
154 {									\
155 	return (old & ~to(field)) | type##_encode_bits(val, field);	\
156 }									\
157 static __always_inline void type##p_replace_bits(__##type *p,		\
158 					base val, base field)		\
159 {									\
160 	*p = (*p & ~to(field)) | type##_encode_bits(val, field);	\
161 }									\
162 static __always_inline base type##_get_bits(__##type v, base field)	\
163 {									\
164 	return (from(v) & field)/field_multiplier(field);		\
165 }
166 #define __MAKE_OP(size)							\
167 	____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu)	\
168 	____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu)	\
169 	____MAKE_OP(u##size,u##size,,)
170 ____MAKE_OP(u8,u8,,)
171 __MAKE_OP(16)
172 __MAKE_OP(32)
173 __MAKE_OP(64)
174 #undef __MAKE_OP
175 #undef ____MAKE_OP
176 
177 #endif
178