blob: bd88483b9f6d915f3927094967e6968b1470c9d7 [file] [log] [blame]
Masahiro Yamadacba1da42014-11-07 03:03:28 +09001#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4
5#include <linux/types.h>
6
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
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090040#define STACK_MAGIC 0xdeadbeef
41
42#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090043
44#define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
Masahiro Yamada75db00e2017-12-21 13:51:46 +090045#define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090046#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090047#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
48#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
Masahiro Yamadacba1da42014-11-07 03:03:28 +090049
50#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
51
52/*
53 * This looks more complex than it should be. But we need to
54 * get the type for the ~ right in round_down (it needs to be
55 * as wide as the result!), and we want to evaluate the macro
56 * arguments just once each.
57 */
58#define __round_mask(x, y) ((__typeof__(x))((y)-1))
59#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
60#define round_down(x, y) ((x) & ~__round_mask(x, y))
61
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090062#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090063#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
64
Masahiro Yamada84570a02017-09-13 19:16:44 +090065#define DIV_ROUND_DOWN_ULL(ll, d) \
66 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
67
68#define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
69
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090070#if BITS_PER_LONG == 32
71# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
72#else
73# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
74#endif
75
Masahiro Yamada111396c2014-11-07 03:03:30 +090076/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
77#define roundup(x, y) ( \
78{ \
79 const typeof(y) __y = y; \
80 (((x) + (__y - 1)) / __y) * __y; \
81} \
82)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090083#define rounddown(x, y) ( \
84{ \
85 typeof(x) __x = (x); \
86 __x - (__x % (y)); \
87} \
88)
89
Masahiro Yamadacba1da42014-11-07 03:03:28 +090090/*
91 * Divide positive or negative dividend by positive divisor and round
92 * to closest integer. Result is undefined for negative divisors and
93 * for negative dividends if the divisor variable type is unsigned.
94 */
95#define DIV_ROUND_CLOSEST(x, divisor)( \
96{ \
97 typeof(x) __x = x; \
98 typeof(divisor) __d = divisor; \
99 (((typeof(x))-1) > 0 || \
100 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
101 (((__x) + ((__d) / 2)) / (__d)) : \
102 (((__x) - ((__d) / 2)) / (__d)); \
103} \
104)
105
106/*
107 * Multiplies an integer by a fraction, while avoiding unnecessary
108 * overflow or loss of precision.
109 */
110#define mult_frac(x, numer, denom)( \
111{ \
112 typeof(x) quot = (x) / (denom); \
113 typeof(x) rem = (x) % (denom); \
114 (quot * (numer)) + ((rem * (numer)) / (denom)); \
115} \
116)
117
118/**
119 * upper_32_bits - return bits 32-63 of a number
120 * @n: the number we're accessing
121 *
122 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
123 * the "right shift count >= width of type" warning when that quantity is
124 * 32-bits.
125 */
126#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
127
128/**
129 * lower_32_bits - return bits 0-31 of a number
130 * @n: the number we're accessing
131 */
132#define lower_32_bits(n) ((u32)(n))
133
134/*
135 * abs() handles unsigned and signed longs, ints, shorts and chars. For all
136 * input types abs() returns a signed long.
137 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
138 * for those.
139 */
140#define abs(x) ({ \
141 long ret; \
142 if (sizeof(x) == sizeof(long)) { \
143 long __x = (x); \
144 ret = (__x < 0) ? -__x : __x; \
145 } else { \
146 int __x = (x); \
147 ret = (__x < 0) ? -__x : __x; \
148 } \
149 ret; \
150 })
151
152#define abs64(x) ({ \
153 s64 __x = (x); \
154 (__x < 0) ? -__x : __x; \
155 })
156
157/*
158 * min()/max()/clamp() macros that also do
159 * strict type-checking.. See the
160 * "unnecessary" pointer comparison.
161 */
162#define min(x, y) ({ \
163 typeof(x) _min1 = (x); \
164 typeof(y) _min2 = (y); \
Masahiro Yamadab4141192014-11-07 03:03:31 +0900165 (void) (&_min1 == &_min2); \
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900166 _min1 < _min2 ? _min1 : _min2; })
167
168#define max(x, y) ({ \
169 typeof(x) _max1 = (x); \
170 typeof(y) _max2 = (y); \
Masahiro Yamadab4141192014-11-07 03:03:31 +0900171 (void) (&_max1 == &_max2); \
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900172 _max1 > _max2 ? _max1 : _max2; })
173
Masahiro Yamadab4141192014-11-07 03:03:31 +0900174#define min3(x, y, z) min((typeof(x))min(x, y), z)
175#define max3(x, y, z) max((typeof(x))max(x, y), z)
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900176
Masahiro Yamada48c7ea32014-11-07 03:03:29 +0900177/**
178 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
179 * @x: value1
180 * @y: value2
181 */
182#define min_not_zero(x, y) ({ \
183 typeof(x) __x = (x); \
184 typeof(y) __y = (y); \
185 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
186
187/**
188 * clamp - return a value clamped to a given range with strict typechecking
189 * @val: current value
190 * @lo: lowest allowable value
191 * @hi: highest allowable value
192 *
193 * This macro does strict typechecking of lo/hi to make sure they are of the
194 * same type as val. See the unnecessary pointer comparisons.
195 */
196#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
197
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900198/*
199 * ..and if you can't take the strict
200 * types, you can specify one yourself.
201 *
202 * Or not use min/max/clamp at all, of course.
203 */
204#define min_t(type, x, y) ({ \
205 type __min1 = (x); \
206 type __min2 = (y); \
207 __min1 < __min2 ? __min1: __min2; })
208
209#define max_t(type, x, y) ({ \
210 type __max1 = (x); \
211 type __max2 = (y); \
212 __max1 > __max2 ? __max1: __max2; })
213
214/**
Masahiro Yamada48c7ea32014-11-07 03:03:29 +0900215 * clamp_t - return a value clamped to a given range using a given type
216 * @type: the type of variable to use
217 * @val: current value
218 * @lo: minimum allowable value
219 * @hi: maximum allowable value
220 *
221 * This macro does no typechecking and uses temporary variables of type
222 * 'type' to make all the comparisons.
223 */
224#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
225
226/**
227 * clamp_val - return a value clamped to a given range using val's type
228 * @val: current value
229 * @lo: minimum allowable value
230 * @hi: maximum allowable value
231 *
232 * This macro does no typechecking and uses temporary variables of whatever
233 * type the input argument 'val' is. This is useful when val is an unsigned
234 * type and min and max are literals that will otherwise be assigned a signed
235 * integer type.
236 */
237#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
238
239
240/*
241 * swap - swap value of @a and @b
242 */
243#define swap(a, b) \
244 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
245
246/**
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900247 * container_of - cast a member of a structure out to the containing structure
248 * @ptr: the pointer to the member.
249 * @type: the type of the container struct this is embedded in.
250 * @member: the name of the member within the struct.
251 *
252 */
253#define container_of(ptr, type, member) ({ \
254 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
255 (type *)( (char *)__mptr - offsetof(type,member) );})
256
257#endif