"Das U-Boot" Source Tree
1#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4#include <linux/types.h>
5#include <linux/printk.h> /* for printf/pr_* utilities */
6#include <limits.h>
7
8#define STACK_MAGIC 0xdeadbeef
9
10#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
11
12#define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
13#define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
14#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
15#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
16#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
17
18#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
19
20/*
21 * This looks more complex than it should be. But we need to
22 * get the type for the ~ right in round_down (it needs to be
23 * as wide as the result!), and we want to evaluate the macro
24 * arguments just once each.
25 */
26#define __round_mask(x, y) ((__typeof__(x))((y)-1))
27#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
28#define round_down(x, y) ((x) & ~__round_mask(x, y))
29
30#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
31#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
32
33#define DIV_ROUND_DOWN_ULL(ll, d) \
34 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
35
36#define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
37
38#define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
39
40#if BITS_PER_LONG == 32
41# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
42#else
43# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
44#endif
45
46/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
47#define roundup(x, y) ( \
48{ \
49 const typeof(y) __y = y; \
50 (((x) + (__y - 1)) / __y) * __y; \
51} \
52)
53#define rounddown(x, y) ( \
54{ \
55 typeof(x) __x = (x); \
56 __x - (__x % (y)); \
57} \
58)
59
60/*
61 * Divide positive or negative dividend by positive divisor and round
62 * to closest integer. Result is undefined for negative divisors and
63 * for negative dividends if the divisor variable type is unsigned.
64 */
65#define DIV_ROUND_CLOSEST(x, divisor)( \
66{ \
67 typeof(x) __x = x; \
68 typeof(divisor) __d = divisor; \
69 (((typeof(x))-1) > 0 || \
70 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
71 (((__x) + ((__d) / 2)) / (__d)) : \
72 (((__x) - ((__d) / 2)) / (__d)); \
73} \
74)
75/*
76 * Same as above but for u64 dividends. divisor must be a 32-bit
77 * number.
78 */
79#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
80{ \
81 typeof(divisor) __d = divisor; \
82 unsigned long long _tmp = (x) + (__d) / 2; \
83 do_div(_tmp, __d); \
84 _tmp; \
85} \
86)
87
88/*
89 * Multiplies an integer by a fraction, while avoiding unnecessary
90 * overflow or loss of precision.
91 */
92#define mult_frac(x, numer, denom)( \
93{ \
94 typeof(x) quot = (x) / (denom); \
95 typeof(x) rem = (x) % (denom); \
96 (quot * (numer)) + ((rem * (numer)) / (denom)); \
97} \
98)
99
100/**
101 * upper_32_bits - return bits 32-63 of a number
102 * @n: the number we're accessing
103 *
104 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
105 * the "right shift count >= width of type" warning when that quantity is
106 * 32-bits.
107 */
108#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
109
110/**
111 * lower_32_bits - return bits 0-31 of a number
112 * @n: the number we're accessing
113 */
114#define lower_32_bits(n) ((u32)((n) & 0xffffffff))
115
116/**
117 * upper_16_bits - return bits 16-31 of a number
118 * @n: the number we're accessing
119 */
120#define upper_16_bits(n) ((u16)((n) >> 16))
121
122/**
123 * lower_16_bits - return bits 0-15 of a number
124 * @n: the number we're accessing
125 */
126#define lower_16_bits(n) ((u16)((n) & 0xffff))
127
128/*
129 * abs() handles unsigned and signed longs, ints, shorts and chars. For all
130 * input types abs() returns a signed long.
131 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
132 * for those.
133 */
134#define abs(x) ({ \
135 long ret; \
136 if (sizeof(x) == sizeof(long)) { \
137 long __x = (x); \
138 ret = (__x < 0) ? -__x : __x; \
139 } else { \
140 int __x = (x); \
141 ret = (__x < 0) ? -__x : __x; \
142 } \
143 ret; \
144 })
145
146#define abs64(x) ({ \
147 s64 __x = (x); \
148 (__x < 0) ? -__x : __x; \
149 })
150
151/*
152 * min()/max()/clamp() macros that also do
153 * strict type-checking.. See the
154 * "unnecessary" pointer comparison.
155 */
156#define min(x, y) ({ \
157 typeof(x) _min1 = (x); \
158 typeof(y) _min2 = (y); \
159 (void) (&_min1 == &_min2); \
160 _min1 < _min2 ? _min1 : _min2; })
161
162#define max(x, y) ({ \
163 typeof(x) _max1 = (x); \
164 typeof(y) _max2 = (y); \
165 (void) (&_max1 == &_max2); \
166 _max1 > _max2 ? _max1 : _max2; })
167
168#define min3(x, y, z) min((typeof(x))min(x, y), z)
169#define max3(x, y, z) max((typeof(x))max(x, y), z)
170
171/**
172 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
173 * @x: value1
174 * @y: value2
175 */
176#define min_not_zero(x, y) ({ \
177 typeof(x) __x = (x); \
178 typeof(y) __y = (y); \
179 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
180
181/**
182 * clamp - return a value clamped to a given range with strict typechecking
183 * @val: current value
184 * @lo: lowest allowable value
185 * @hi: highest allowable value
186 *
187 * This macro does strict typechecking of lo/hi to make sure they are of the
188 * same type as val. See the unnecessary pointer comparisons.
189 */
190#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
191
192/*
193 * ..and if you can't take the strict
194 * types, you can specify one yourself.
195 *
196 * Or not use min/max/clamp at all, of course.
197 */
198#define min_t(type, x, y) ({ \
199 type __min1 = (x); \
200 type __min2 = (y); \
201 __min1 < __min2 ? __min1: __min2; })
202
203#define max_t(type, x, y) ({ \
204 type __max1 = (x); \
205 type __max2 = (y); \
206 __max1 > __max2 ? __max1: __max2; })
207
208/**
209 * clamp_t - return a value clamped to a given range using a given type
210 * @type: the type of variable to use
211 * @val: current value
212 * @lo: minimum allowable value
213 * @hi: maximum allowable value
214 *
215 * This macro does no typechecking and uses temporary variables of type
216 * 'type' to make all the comparisons.
217 */
218#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
219
220/**
221 * clamp_val - return a value clamped to a given range using val's type
222 * @val: current value
223 * @lo: minimum allowable value
224 * @hi: maximum allowable value
225 *
226 * This macro does no typechecking and uses temporary variables of whatever
227 * type the input argument 'val' is. This is useful when val is an unsigned
228 * type and min and max are literals that will otherwise be assigned a signed
229 * integer type.
230 */
231#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
232
233/*
234 * swap - swap value of @a and @b
235 */
236#define swap(a, b) \
237 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
238
239/**
240 * container_of - cast a member of a structure out to the containing structure
241 * @ptr: the pointer to the member.
242 * @type: the type of the container struct this is embedded in.
243 * @member: the name of the member within the struct.
244 *
245 */
246#define container_of(ptr, type, member) ({ \
247 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
248 (type *)( (char *)__mptr - offsetof(type,member) );})
249
250/*
251 * check_member() - Check the offset of a structure member
252 *
253 * @structure: Name of structure (e.g. global_data)
254 * @member: Name of member (e.g. baudrate)
255 * @offset: Expected offset in bytes
256 */
257#define check_member(structure, member, offset) _Static_assert( \
258 offsetof(struct structure, member) == (offset), \
259 "`struct " #structure "` offset for `" #member "` is not " #offset)
260
261#define __find_closest(x, a, as, op) \
262({ \
263 typeof(as) __fc_i, __fc_as = (as) - 1; \
264 typeof(x) __fc_x = (x); \
265 typeof(*a) const *__fc_a = (a); \
266 for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \
267 if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \
268 __fc_a[__fc_i + 1], 2)) \
269 break; \
270 } \
271 (__fc_i); \
272})
273
274/**
275 * find_closest - locate the closest element in a sorted array
276 * @x: The reference value.
277 * @a: The array in which to look for the closest element. Must be sorted
278 * in ascending order.
279 * @as: Size of 'a'.
280 *
281 * Returns the index of the element closest to 'x'.
282 */
283#define find_closest(x, a, as) __find_closest(x, a, as, <=)
284
285#endif