blob: 0e838de094569ac5b2ede26846ac248e6a37ab81 [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)
19#define SIZE_MAX (~(size_t)0)
Masahiro Yamadacba1da42014-11-07 03:03:28 +090020
21#define U8_MAX ((u8)~0U)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090022#define S8_MAX ((s8)(U8_MAX>>1))
23#define S8_MIN ((s8)(-S8_MAX - 1))
24#define U16_MAX ((u16)~0U)
25#define S16_MAX ((s16)(U16_MAX>>1))
26#define S16_MIN ((s16)(-S16_MAX - 1))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090027#define U32_MAX ((u32)~0U)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090028#define S32_MAX ((s32)(U32_MAX>>1))
29#define S32_MIN ((s32)(-S32_MAX - 1))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090030#define U64_MAX ((u64)~0ULL)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090031#define S64_MAX ((s64)(U64_MAX>>1))
32#define S64_MIN ((s64)(-S64_MAX - 1))
33
34#define STACK_MAGIC 0xdeadbeef
35
36#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090037
38#define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
39#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090040#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
41#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
Masahiro Yamadacba1da42014-11-07 03:03:28 +090042
43#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
44
45/*
46 * This looks more complex than it should be. But we need to
47 * get the type for the ~ right in round_down (it needs to be
48 * as wide as the result!), and we want to evaluate the macro
49 * arguments just once each.
50 */
51#define __round_mask(x, y) ((__typeof__(x))((y)-1))
52#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
53#define round_down(x, y) ((x) & ~__round_mask(x, y))
54
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090055#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
Masahiro Yamadacba1da42014-11-07 03:03:28 +090056#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
57
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090058#if BITS_PER_LONG == 32
59# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
60#else
61# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
62#endif
63
Masahiro Yamada111396c2014-11-07 03:03:30 +090064/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
65#define roundup(x, y) ( \
66{ \
67 const typeof(y) __y = y; \
68 (((x) + (__y - 1)) / __y) * __y; \
69} \
70)
Masahiro Yamada48c7ea32014-11-07 03:03:29 +090071#define rounddown(x, y) ( \
72{ \
73 typeof(x) __x = (x); \
74 __x - (__x % (y)); \
75} \
76)
77
Masahiro Yamadacba1da42014-11-07 03:03:28 +090078/*
79 * Divide positive or negative dividend by positive divisor and round
80 * to closest integer. Result is undefined for negative divisors and
81 * for negative dividends if the divisor variable type is unsigned.
82 */
83#define DIV_ROUND_CLOSEST(x, divisor)( \
84{ \
85 typeof(x) __x = x; \
86 typeof(divisor) __d = divisor; \
87 (((typeof(x))-1) > 0 || \
88 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
89 (((__x) + ((__d) / 2)) / (__d)) : \
90 (((__x) - ((__d) / 2)) / (__d)); \
91} \
92)
93
94/*
95 * Multiplies an integer by a fraction, while avoiding unnecessary
96 * overflow or loss of precision.
97 */
98#define mult_frac(x, numer, denom)( \
99{ \
100 typeof(x) quot = (x) / (denom); \
101 typeof(x) rem = (x) % (denom); \
102 (quot * (numer)) + ((rem * (numer)) / (denom)); \
103} \
104)
105
106/**
107 * upper_32_bits - return bits 32-63 of a number
108 * @n: the number we're accessing
109 *
110 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
111 * the "right shift count >= width of type" warning when that quantity is
112 * 32-bits.
113 */
114#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
115
116/**
117 * lower_32_bits - return bits 0-31 of a number
118 * @n: the number we're accessing
119 */
120#define lower_32_bits(n) ((u32)(n))
121
122/*
123 * abs() handles unsigned and signed longs, ints, shorts and chars. For all
124 * input types abs() returns a signed long.
125 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
126 * for those.
127 */
128#define abs(x) ({ \
129 long ret; \
130 if (sizeof(x) == sizeof(long)) { \
131 long __x = (x); \
132 ret = (__x < 0) ? -__x : __x; \
133 } else { \
134 int __x = (x); \
135 ret = (__x < 0) ? -__x : __x; \
136 } \
137 ret; \
138 })
139
140#define abs64(x) ({ \
141 s64 __x = (x); \
142 (__x < 0) ? -__x : __x; \
143 })
144
145/*
146 * min()/max()/clamp() macros that also do
147 * strict type-checking.. See the
148 * "unnecessary" pointer comparison.
149 */
150#define min(x, y) ({ \
151 typeof(x) _min1 = (x); \
152 typeof(y) _min2 = (y); \
153 _min1 < _min2 ? _min1 : _min2; })
154
155#define max(x, y) ({ \
156 typeof(x) _max1 = (x); \
157 typeof(y) _max2 = (y); \
158 _max1 > _max2 ? _max1 : _max2; })
159
160#define min3(x, y, z) ({ \
161 typeof(x) _min1 = (x); \
162 typeof(y) _min2 = (y); \
163 typeof(z) _min3 = (z); \
164 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
165 (_min2 < _min3 ? _min2 : _min3); })
166
167#define max3(x, y, z) ({ \
168 typeof(x) _max1 = (x); \
169 typeof(y) _max2 = (y); \
170 typeof(z) _max3 = (z); \
171 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
172 (_max2 > _max3 ? _max2 : _max3); })
173
Masahiro Yamada48c7ea32014-11-07 03:03:29 +0900174/**
175 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
176 * @x: value1
177 * @y: value2
178 */
179#define min_not_zero(x, y) ({ \
180 typeof(x) __x = (x); \
181 typeof(y) __y = (y); \
182 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
183
184/**
185 * clamp - return a value clamped to a given range with strict typechecking
186 * @val: current value
187 * @lo: lowest allowable value
188 * @hi: highest allowable value
189 *
190 * This macro does strict typechecking of lo/hi to make sure they are of the
191 * same type as val. See the unnecessary pointer comparisons.
192 */
193#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
194
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900195/*
196 * ..and if you can't take the strict
197 * types, you can specify one yourself.
198 *
199 * Or not use min/max/clamp at all, of course.
200 */
201#define min_t(type, x, y) ({ \
202 type __min1 = (x); \
203 type __min2 = (y); \
204 __min1 < __min2 ? __min1: __min2; })
205
206#define max_t(type, x, y) ({ \
207 type __max1 = (x); \
208 type __max2 = (y); \
209 __max1 > __max2 ? __max1: __max2; })
210
211/**
Masahiro Yamada48c7ea32014-11-07 03:03:29 +0900212 * clamp_t - return a value clamped to a given range using a given type
213 * @type: the type of variable to use
214 * @val: current value
215 * @lo: minimum allowable value
216 * @hi: maximum allowable value
217 *
218 * This macro does no typechecking and uses temporary variables of type
219 * 'type' to make all the comparisons.
220 */
221#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
222
223/**
224 * clamp_val - return a value clamped to a given range using val's type
225 * @val: current value
226 * @lo: minimum allowable value
227 * @hi: maximum allowable value
228 *
229 * This macro does no typechecking and uses temporary variables of whatever
230 * type the input argument 'val' is. This is useful when val is an unsigned
231 * type and min and max are literals that will otherwise be assigned a signed
232 * integer type.
233 */
234#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
235
236
237/*
238 * swap - swap value of @a and @b
239 */
240#define swap(a, b) \
241 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
242
243/**
Masahiro Yamadacba1da42014-11-07 03:03:28 +0900244 * container_of - cast a member of a structure out to the containing structure
245 * @ptr: the pointer to the member.
246 * @type: the type of the container struct this is embedded in.
247 * @member: the name of the member within the struct.
248 *
249 */
250#define container_of(ptr, type, member) ({ \
251 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
252 (type *)( (char *)__mptr - offsetof(type,member) );})
253
254#endif