blob: b88c21006591d3117cc857a36d381a5cdaf71c0f [file] [log] [blame]
Masahiro Yamadacba1da42014-11-07 03:03:28 +09001#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
Masahiro Yamadacba1da42014-11-07 03:03:28 +09004#include <linux/types.h>
AKASHI Takahiro29852bf2019-11-13 09:44:51 +09005#include <linux/printk.h> /* for printf/pr_* utilities */
Masahiro Yamadacba1da42014-11-07 03:03:28 +09006
Masahiro Yamada48c7ea32014-11-07 03:03:29 +09007#define USHRT_MAX ((u16)(~0U))
8#define SHRT_MAX ((s16)(USHRT_MAX>>1))
9#define SHRT_MIN ((s16)(-SHRT_MAX - 1))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090010#define INT_MAX ((int)(~0U>>1))
11#define INT_MIN (-INT_MAX - 1)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090012#define UINT_MAX (~0U)
13#define LONG_MAX ((long)(~0UL>>1))
14#define LONG_MIN (-LONG_MAX - 1)
15#define ULONG_MAX (~0UL)
Masahiro Yamadacba1da42014-11-07 03:03:28 +090016#define LLONG_MAX ((long long)(~0ULL>>1))
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090017#define LLONG_MIN (-LLONG_MAX - 1)
18#define ULLONG_MAX (~0ULL)
Simon Glass803f2eb2014-11-24 21:18:21 -070019#ifndef SIZE_MAX
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090020#define SIZE_MAX (~(size_t)0)
Simon Glass803f2eb2014-11-24 21:18:21 -070021#endif
Masahiro Yamadacba1da42014-11-07 03:03:28 +090022
23#define U8_MAX ((u8)~0U)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090024#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))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090029#define U32_MAX ((u32)~0U)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090030#define S32_MAX ((s32)(U32_MAX>>1))
31#define S32_MIN ((s32)(-S32_MAX - 1))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090032#define U64_MAX ((u64)~0ULL)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090033#define S64_MAX ((s64)(U64_MAX>>1))
34#define S64_MIN ((s64)(-S64_MAX - 1))
35
Simon Glass277f4eb2018-11-23 21:29:42 -070036/* Aliases defined by stdint.h */
37#define UINT32_MAX U32_MAX
38#define UINT64_MAX U64_MAX
39
Simon Glass86598952019-10-27 09:47:39 -060040#define INT32_MAX S32_MAX
41
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090042#define STACK_MAGIC 0xdeadbeef
43
44#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090045
46#define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
Masahiro Yamada75db00e2017-12-21 13:51:46 +090047#define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090048#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090049#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
50#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
Masahiro Yamadacba1da42014-11-07 03:03:28 +090051
52#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
53
54/*
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.
59 */
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))
63
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090064#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090065#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
66
Masahiro Yamada84570a02017-09-13 19:16:44 +090067#define DIV_ROUND_DOWN_ULL(ll, d) \
68 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
69
70#define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
71
Simon Glass6df75132019-12-28 10:45:09 -070072#define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
73
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090074#if BITS_PER_LONG == 32
75# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
76#else
77# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
78#endif
79
Masahiro Yamada111396c2014-11-07 03:03:30 +090080/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
81#define roundup(x, y) ( \
82{ \
83 const typeof(y) __y = y; \
84 (((x) + (__y - 1)) / __y) * __y; \
85} \
86)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090087#define rounddown(x, y) ( \
88{ \
89 typeof(x) __x = (x); \
90 __x - (__x % (y)); \
91} \
92)
93
Masahiro Yamadacba1da42014-11-07 03:03:28 +090094/*
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.
98 */
99#define DIV_ROUND_CLOSEST(x, divisor)( \
100{ \
101 typeof(x) __x = x; \
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)); \
107} \
108)
Masahiro Yamadab32aa9e2018-12-19 20:03:16 +0900109/*
110 * Same as above but for u64 dividends. divisor must be a 32-bit
111 * number.
112 */
113#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
114{ \
115 typeof(divisor) __d = divisor; \
116 unsigned long long _tmp = (x) + (__d) / 2; \
117 do_div(_tmp, __d); \
118 _tmp; \
119} \
120)
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900121
122/*
123 * Multiplies an integer by a fraction, while avoiding unnecessary
124 * overflow or loss of precision.
125 */
126#define mult_frac(x, numer, denom)( \
127{ \
128 typeof(x) quot = (x) / (denom); \
129 typeof(x) rem = (x) % (denom); \
130 (quot * (numer)) + ((rem * (numer)) / (denom)); \
131} \
132)
133
134/**
135 * upper_32_bits - return bits 32-63 of a number
136 * @n: the number we're accessing
137 *
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
140 * 32-bits.
141 */
142#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
143
144/**
145 * lower_32_bits - return bits 0-31 of a number
146 * @n: the number we're accessing
147 */
148#define lower_32_bits(n) ((u32)(n))
149
150/*
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()
154 * for those.
155 */
156#define abs(x) ({ \
157 long ret; \
158 if (sizeof(x) == sizeof(long)) { \
159 long __x = (x); \
160 ret = (__x < 0) ? -__x : __x; \
161 } else { \
162 int __x = (x); \
163 ret = (__x < 0) ? -__x : __x; \
164 } \
165 ret; \
166 })
167
168#define abs64(x) ({ \
169 s64 __x = (x); \
170 (__x < 0) ? -__x : __x; \
171 })
172
173/*
174 * min()/max()/clamp() macros that also do
175 * strict type-checking.. See the
176 * "unnecessary" pointer comparison.
177 */
178#define min(x, y) ({ \
179 typeof(x) _min1 = (x); \
180 typeof(y) _min2 = (y); \
Masahiro Yamadab4141192014-11-07 03:03:31 +0900181 (void) (&_min1 == &_min2); \
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900182 _min1 < _min2 ? _min1 : _min2; })
183
184#define max(x, y) ({ \
185 typeof(x) _max1 = (x); \
186 typeof(y) _max2 = (y); \
Masahiro Yamadab4141192014-11-07 03:03:31 +0900187 (void) (&_max1 == &_max2); \
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900188 _max1 > _max2 ? _max1 : _max2; })
189
Masahiro Yamadab4141192014-11-07 03:03:31 +0900190#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)
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900192
Masahiro Yamada48c7ea32014-11-07 03:03:29 +0900193/**
194 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
195 * @x: value1
196 * @y: value2
197 */
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)); })
202
203/**
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
208 *
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.
211 */
212#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
213
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900214/*
215 * ..and if you can't take the strict
216 * types, you can specify one yourself.
217 *
218 * Or not use min/max/clamp at all, of course.
219 */
220#define min_t(type, x, y) ({ \
221 type __min1 = (x); \
222 type __min2 = (y); \
223 __min1 < __min2 ? __min1: __min2; })
224
225#define max_t(type, x, y) ({ \
226 type __max1 = (x); \
227 type __max2 = (y); \
228 __max1 > __max2 ? __max1: __max2; })
229
230/**
Masahiro Yamada48c7ea32014-11-07 03:03:29 +0900231 * 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
236 *
237 * This macro does no typechecking and uses temporary variables of type
238 * 'type' to make all the comparisons.
239 */
240#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
241
242/**
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
247 *
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
251 * integer type.
252 */
253#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
254
255
256/*
257 * swap - swap value of @a and @b
258 */
259#define swap(a, b) \
260 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
261
262/**
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900263 * 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.
267 *
268 */
269#define container_of(ptr, type, member) ({ \
270 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
271 (type *)( (char *)__mptr - offsetof(type,member) );})
272
Simon Glass825faeb2019-12-28 10:45:08 -0700273/*
274 * check_member() - Check the offset of a structure member
275 *
276 * @structure: Name of structure (e.g. global_data)
277 * @member: Name of member (e.g. baudrate)
278 * @offset: Expected offset in bytes
279 */
280#define check_member(structure, member, offset) _Static_assert( \
281 offsetof(struct structure, member) == (offset), \
282 "`struct " #structure "` offset for `" #member "` is not " #offset)
283
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900284#endif