2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #ifndef __SHARED_COMPILER_H
18 #define __SHARED_COMPILER_H
20 #define __noreturn __attribute__((noreturn))
21 #define __inline __attribute__((always_inline))
22 #define __hot __attribute__((hot))
23 #define __cold __attribute__((cold))
24 #define __force __attribute__((force))
25 #define __in_section(s) __attribute__((section("." # s)))
26 #define __visible __attribute__((externally_visible))
28 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
31 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
32 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
34 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
35 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
37 #define __roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
39 #define __must_be_array(a) \
40 BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
41 #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]) + __must_be_array(arr))
43 #define BIT(b) (1 << (b))
44 #define MASK(w) (BIT(w) - 1)
47 * __struct_group() - Create a mirrored named and anonyomous struct
49 * @TAG: The tag name for the named sub-struct (usually empty)
50 * @NAME: The identifier name of the mirrored sub-struct
51 * @ATTRS: Any struct attributes (usually empty)
52 * @MEMBERS: The member declarations for the mirrored structs
54 * Used to create an anonymous union of two structs with identical layout
55 * and size: one anonymous and one named. The former's members can be used
56 * normally without sub-struct naming, and the latter can be used to
57 * reason about the start, end, and size of the group of struct members.
58 * The named struct can also be explicitly tagged for layer reuse, as well
59 * as both having struct attributes appended.
61 #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
63 struct { MEMBERS } ATTRS; \
64 struct TAG { MEMBERS } ATTRS NAME; \
68 * struct_group() - Wrap a set of declarations in a mirrored struct
70 * @NAME: The identifier name of the mirrored sub-struct
71 * @MEMBERS: The member declarations for the mirrored structs
73 * Used to create an anonymous union of two structs with identical
74 * layout and size: one anonymous and one named. The former can be
75 * used normally without sub-struct naming, and the latter can be
76 * used to reason about the start, end, and size of the group of
79 #define struct_group(NAME, MEMBERS...) \
80 __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
83 #ifdef __compiler_offsetof
84 # define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
86 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
90 * sizeof_field() - Report the size of a struct field in bytes
92 * @TYPE: The structure containing the field of interest
93 * @MEMBER: The field to return the size of
95 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
98 * offsetofend() - Report the offset of a struct field within the struct
100 * @TYPE: The type of the structure
101 * @MEMBER: The member within the structure to get the end offset of
103 #define offsetofend(TYPE, MEMBER) \
104 (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
106 #define likely(x) __builtin_expect(!!(x), 1)
107 #define unlikely(x) __builtin_expect(!!(x), 0)
109 #define min(x, y) ({ \
110 typeof(x) _min1 = (x); \
111 typeof(y) _min2 = (y); \
112 (void) (&_min1 == &_min2); \
113 _min1 < _min2 ? _min1 : _min2; })
115 #define max(x, y) ({ \
116 typeof(x) _max1 = (x); \
117 typeof(y) _max2 = (y); \
118 (void) (&_max1 == &_max2); \
119 _max1 > _max2 ? _max1 : _max2; })
121 #define min_t(type, x, y) ({ \
124 __min1 < __min2 ? __min1 : __min2; })
126 #define max_t(type, x, y) ({ \
129 __max1 > __max2 ? __max1 : __max2; })
132 #define container_of(ptr, type, member) ({ \
133 const typeof(((type *)0)->member) * __mptr = (ptr); \
134 (type *)(((unsigned long)__mptr - offsetof(type, member))); })
136 #define MAX_ERRNO 4095
138 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
140 static inline void *ERR_PTR(long errornr)
142 return (void *) errornr;
145 static inline long PTR_ERR(const void *ptr)
150 static inline long IS_ERR(const void *ptr)
152 return IS_ERR_VALUE((unsigned long)ptr);
155 static inline long IS_ERR_OR_NULL(const void *ptr)
157 return !ptr || IS_ERR_VALUE((unsigned long)ptr);
160 static inline unsigned int hweight8(unsigned int w)
162 unsigned int res = w - ((w >> 1) & 0x55);
163 res = (res & 0x33) + ((res >> 2) & 0x33);
164 return (res + (res >> 4)) & 0x0F;
167 static inline unsigned int hweight16(unsigned int w)
169 unsigned int res = w - ((w >> 1) & 0x5555);
170 res = (res & 0x3333) + ((res >> 2) & 0x3333);
171 res = (res + (res >> 4)) & 0x0F0F;
172 return (res + (res >> 8)) & 0x00FF;
176 * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
178 * @TYPE: The type of each flexible array element
179 * @NAME: The name of the flexible array member
181 * In order to have a flexible array member in a union or alone in a
182 * struct, it needs to be wrapped in an anonymous struct with at least 1
183 * named member, but that member can be empty.
185 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
187 struct { } __empty_ ## NAME; \
191 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
193 #define FIELD_MAX(_mask) \
195 (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
198 #if __has_attribute(__error__)
199 # define __compiletime_error(msg) __attribute__((__error__(msg)))
201 # define __compiletime_error(msg)
204 extern void __compiletime_error("value doesn't fit into mask")
205 __field_overflow(void);
206 extern void __compiletime_error("bad bitfield mask")
208 static inline u64 field_multiplier(u32 field)
210 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
212 return field & -field;
214 static inline u64 field_mask(const u32 field)
216 return field / field_multiplier(field);
218 #define field_max(field) ((typeof(field))field_mask(field))
219 #define ____MAKE_OP(type,base,to,from) \
220 static inline __##type type##_encode_bits(base v, base field) \
222 if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
223 __field_overflow(); \
224 return to((v & field_mask(field)) * field_multiplier(field)); \
226 static inline __##type type##_replace_bits(__##type old, \
227 base val, base field) \
229 return (old & ~to(field)) | type##_encode_bits(val, field); \
231 static inline void type##p_replace_bits(__##type *p, \
232 base val, base field) \
234 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
236 static inline base type##_get_bits(__##type v, base field) \
238 return (from(v) & field)/field_multiplier(field); \
240 #define __MAKE_OP(size) \
241 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
242 ____MAKE_OP(u##size,u##size,,)
249 #endif /* __SHARED_COMPILER_H */