1 #ifndef _LINUX_KERNEL_H
2 #define _LINUX_KERNEL_H
4 #include <linux/types.h>
5 #include <linux/printk.h> /* for printf/pr_* utilities */
7 #define USHRT_MAX ((u16)(~0U))
8 #define SHRT_MAX ((s16)(USHRT_MAX>>1))
9 #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
10 #define INT_MAX ((int)(~0U>>1))
11 #define INT_MIN (-INT_MAX - 1)
12 #define UINT_MAX (~0U)
13 #define LONG_MAX ((long)(~0UL>>1))
14 #define LONG_MIN (-LONG_MAX - 1)
15 #define ULONG_MAX (~0UL)
16 #define LLONG_MAX ((long long)(~0ULL>>1))
17 #define LLONG_MIN (-LLONG_MAX - 1)
18 #define ULLONG_MAX (~0ULL)
20 #define SIZE_MAX (~(size_t)0)
23 #define U8_MAX ((u8)~0U)
24 #define S8_MAX ((s8)(U8_MAX>>1))
25 #define S8_MIN ((s8)(-S8_MAX - 1))
26 #define U16_MAX ((u16)~0U)
27 #define S16_MAX ((s16)(U16_MAX>>1))
28 #define S16_MIN ((s16)(-S16_MAX - 1))
29 #define U32_MAX ((u32)~0U)
30 #define S32_MAX ((s32)(U32_MAX>>1))
31 #define S32_MIN ((s32)(-S32_MAX - 1))
32 #define U64_MAX ((u64)~0ULL)
33 #define S64_MAX ((s64)(U64_MAX>>1))
34 #define S64_MIN ((s64)(-S64_MAX - 1))
36 /* Aliases defined by stdint.h */
37 #define UINT32_MAX U32_MAX
38 #define UINT64_MAX U64_MAX
40 #define INT32_MAX S32_MAX
42 #define STACK_MAGIC 0xdeadbeef
44 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
46 #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
47 #define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
48 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
49 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
50 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
52 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
55 * This looks more complex than it should be. But we need to
56 * get the type for the ~ right in round_down (it needs to be
57 * as wide as the result!), and we want to evaluate the macro
58 * arguments just once each.
60 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
61 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
62 #define round_down(x, y) ((x) & ~__round_mask(x, y))
64 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
65 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
67 #define DIV_ROUND_DOWN_ULL(ll, d) \
68 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
70 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
72 #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
74 #if BITS_PER_LONG == 32
75 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
77 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
80 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
81 #define roundup(x, y) ( \
83 const typeof(y) __y = y; \
84 (((x) + (__y - 1)) / __y) * __y; \
87 #define rounddown(x, y) ( \
89 typeof(x) __x = (x); \
95 * Divide positive or negative dividend by positive divisor and round
96 * to closest integer. Result is undefined for negative divisors and
97 * for negative dividends if the divisor variable type is unsigned.
99 #define DIV_ROUND_CLOSEST(x, divisor)( \
102 typeof(divisor) __d = divisor; \
103 (((typeof(x))-1) > 0 || \
104 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
105 (((__x) + ((__d) / 2)) / (__d)) : \
106 (((__x) - ((__d) / 2)) / (__d)); \
110 * Same as above but for u64 dividends. divisor must be a 32-bit
113 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
115 typeof(divisor) __d = divisor; \
116 unsigned long long _tmp = (x) + (__d) / 2; \
123 * Multiplies an integer by a fraction, while avoiding unnecessary
124 * overflow or loss of precision.
126 #define mult_frac(x, numer, denom)( \
128 typeof(x) quot = (x) / (denom); \
129 typeof(x) rem = (x) % (denom); \
130 (quot * (numer)) + ((rem * (numer)) / (denom)); \
135 * upper_32_bits - return bits 32-63 of a number
136 * @n: the number we're accessing
138 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
139 * the "right shift count >= width of type" warning when that quantity is
142 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
145 * lower_32_bits - return bits 0-31 of a number
146 * @n: the number we're accessing
148 #define lower_32_bits(n) ((u32)(n))
151 * abs() handles unsigned and signed longs, ints, shorts and chars. For all
152 * input types abs() returns a signed long.
153 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
158 if (sizeof(x) == sizeof(long)) { \
160 ret = (__x < 0) ? -__x : __x; \
163 ret = (__x < 0) ? -__x : __x; \
168 #define abs64(x) ({ \
170 (__x < 0) ? -__x : __x; \
174 * min()/max()/clamp() macros that also do
175 * strict type-checking.. See the
176 * "unnecessary" pointer comparison.
178 #define min(x, y) ({ \
179 typeof(x) _min1 = (x); \
180 typeof(y) _min2 = (y); \
181 (void) (&_min1 == &_min2); \
182 _min1 < _min2 ? _min1 : _min2; })
184 #define max(x, y) ({ \
185 typeof(x) _max1 = (x); \
186 typeof(y) _max2 = (y); \
187 (void) (&_max1 == &_max2); \
188 _max1 > _max2 ? _max1 : _max2; })
190 #define min3(x, y, z) min((typeof(x))min(x, y), z)
191 #define max3(x, y, z) max((typeof(x))max(x, y), z)
194 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
198 #define min_not_zero(x, y) ({ \
199 typeof(x) __x = (x); \
200 typeof(y) __y = (y); \
201 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
204 * clamp - return a value clamped to a given range with strict typechecking
205 * @val: current value
206 * @lo: lowest allowable value
207 * @hi: highest allowable value
209 * This macro does strict typechecking of lo/hi to make sure they are of the
210 * same type as val. See the unnecessary pointer comparisons.
212 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
215 * ..and if you can't take the strict
216 * types, you can specify one yourself.
218 * Or not use min/max/clamp at all, of course.
220 #define min_t(type, x, y) ({ \
223 __min1 < __min2 ? __min1: __min2; })
225 #define max_t(type, x, y) ({ \
228 __max1 > __max2 ? __max1: __max2; })
231 * clamp_t - return a value clamped to a given range using a given type
232 * @type: the type of variable to use
233 * @val: current value
234 * @lo: minimum allowable value
235 * @hi: maximum allowable value
237 * This macro does no typechecking and uses temporary variables of type
238 * 'type' to make all the comparisons.
240 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
243 * clamp_val - return a value clamped to a given range using val's type
244 * @val: current value
245 * @lo: minimum allowable value
246 * @hi: maximum allowable value
248 * This macro does no typechecking and uses temporary variables of whatever
249 * type the input argument 'val' is. This is useful when val is an unsigned
250 * type and min and max are literals that will otherwise be assigned a signed
253 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
257 * swap - swap value of @a and @b
260 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
263 * container_of - cast a member of a structure out to the containing structure
264 * @ptr: the pointer to the member.
265 * @type: the type of the container struct this is embedded in.
266 * @member: the name of the member within the struct.
269 #define container_of(ptr, type, member) ({ \
270 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
271 (type *)( (char *)__mptr - offsetof(type,member) );})
274 * check_member() - Check the offset of a structure member
276 * @structure: Name of structure (e.g. global_data)
277 * @member: Name of member (e.g. baudrate)
278 * @offset: Expected offset in bytes
280 #define check_member(structure, member, offset) _Static_assert( \
281 offsetof(struct structure, member) == (offset), \
282 "`struct " #structure "` offset for `" #member "` is not " #offset)