1#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4
5#include <linux/types.h>
6
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)
19#ifndef SIZE_MAX
20#define SIZE_MAX (~(size_t)0)
21#endif
22
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))
35
36
37#define UINT32_MAX U32_MAX
38#define UINT64_MAX U64_MAX
39
40#define STACK_MAGIC 0xdeadbeef
41
42#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
43
44#define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
45#define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
46#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
47#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
48#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
49
50#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
51
52
53
54
55
56
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
62#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
63#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
64
65#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
70#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
76
77#define roundup(x, y) ( \
78{ \
79 const typeof(y) __y = y; \
80 (((x) + (__y - 1)) / __y) * __y; \
81} \
82)
83#define rounddown(x, y) ( \
84{ \
85 typeof(x) __x = (x); \
86 __x - (__x % (y)); \
87} \
88)
89
90
91
92
93
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
108
109#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
110{ \
111 typeof(divisor) __d = divisor; \
112 unsigned long long _tmp = (x) + (__d) / 2; \
113 do_div(_tmp, __d); \
114 _tmp; \
115} \
116)
117
118
119
120
121
122#define mult_frac(x, numer, denom)( \
123{ \
124 typeof(x) quot = (x) / (denom); \
125 typeof(x) rem = (x) % (denom); \
126 (quot * (numer)) + ((rem * (numer)) / (denom)); \
127} \
128)
129
130
131
132
133
134
135
136
137
138#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
139
140
141
142
143
144#define lower_32_bits(n) ((u32)(n))
145
146
147
148
149
150
151
152#define abs(x) ({ \
153 long ret; \
154 if (sizeof(x) == sizeof(long)) { \
155 long __x = (x); \
156 ret = (__x < 0) ? -__x : __x; \
157 } else { \
158 int __x = (x); \
159 ret = (__x < 0) ? -__x : __x; \
160 } \
161 ret; \
162 })
163
164#define abs64(x) ({ \
165 s64 __x = (x); \
166 (__x < 0) ? -__x : __x; \
167 })
168
169
170
171
172
173
174#define min(x, y) ({ \
175 typeof(x) _min1 = (x); \
176 typeof(y) _min2 = (y); \
177 (void) (&_min1 == &_min2); \
178 _min1 < _min2 ? _min1 : _min2; })
179
180#define max(x, y) ({ \
181 typeof(x) _max1 = (x); \
182 typeof(y) _max2 = (y); \
183 (void) (&_max1 == &_max2); \
184 _max1 > _max2 ? _max1 : _max2; })
185
186#define min3(x, y, z) min((typeof(x))min(x, y), z)
187#define max3(x, y, z) max((typeof(x))max(x, y), z)
188
189
190
191
192
193
194#define min_not_zero(x, y) ({ \
195 typeof(x) __x = (x); \
196 typeof(y) __y = (y); \
197 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
198
199
200
201
202
203
204
205
206
207
208#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
209
210
211
212
213
214
215
216#define min_t(type, x, y) ({ \
217 type __min1 = (x); \
218 type __min2 = (y); \
219 __min1 < __min2 ? __min1: __min2; })
220
221#define max_t(type, x, y) ({ \
222 type __max1 = (x); \
223 type __max2 = (y); \
224 __max1 > __max2 ? __max1: __max2; })
225
226
227
228
229
230
231
232
233
234
235
236#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
237
238
239
240
241
242
243
244
245
246
247
248
249#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
250
251
252
253
254
255#define swap(a, b) \
256 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
257
258
259
260
261
262
263
264
265#define container_of(ptr, type, member) ({ \
266 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
267 (type *)( (char *)__mptr - offsetof(type,member) );})
268
269#endif
270