1 #ifndef _LINUX_KERNEL_H
2 #define _LINUX_KERNEL_H
5 #include <linux/types.h>
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 #define SIZE_MAX (~(size_t)0)
21 #define U8_MAX ((u8)~0U)
22 #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))
27 #define U32_MAX ((u32)~0U)
28 #define S32_MAX ((s32)(U32_MAX>>1))
29 #define S32_MIN ((s32)(-S32_MAX - 1))
30 #define U64_MAX ((u64)~0ULL)
31 #define S64_MAX ((s64)(U64_MAX>>1))
32 #define S64_MIN ((s64)(-S64_MAX - 1))
34 #define STACK_MAGIC 0xdeadbeef
36 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
38 #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
39 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
40 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
41 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
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.
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))
55 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
56 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
58 #if BITS_PER_LONG == 32
59 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
61 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
64 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
66 #define rounddown(x, y) ( \
68 typeof(x) __x = (x); \
74 * Divide positive or negative dividend by positive divisor and round
75 * to closest integer. Result is undefined for negative divisors and
76 * for negative dividends if the divisor variable type is unsigned.
78 #define DIV_ROUND_CLOSEST(x, divisor)( \
81 typeof(divisor) __d = divisor; \
82 (((typeof(x))-1) > 0 || \
83 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
84 (((__x) + ((__d) / 2)) / (__d)) : \
85 (((__x) - ((__d) / 2)) / (__d)); \
90 * Multiplies an integer by a fraction, while avoiding unnecessary
91 * overflow or loss of precision.
93 #define mult_frac(x, numer, denom)( \
95 typeof(x) quot = (x) / (denom); \
96 typeof(x) rem = (x) % (denom); \
97 (quot * (numer)) + ((rem * (numer)) / (denom)); \
102 * upper_32_bits - return bits 32-63 of a number
103 * @n: the number we're accessing
105 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
106 * the "right shift count >= width of type" warning when that quantity is
109 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
112 * lower_32_bits - return bits 0-31 of a number
113 * @n: the number we're accessing
115 #define lower_32_bits(n) ((u32)(n))
118 * abs() handles unsigned and signed longs, ints, shorts and chars. For all
119 * input types abs() returns a signed long.
120 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
125 if (sizeof(x) == sizeof(long)) { \
127 ret = (__x < 0) ? -__x : __x; \
130 ret = (__x < 0) ? -__x : __x; \
135 #define abs64(x) ({ \
137 (__x < 0) ? -__x : __x; \
141 * min()/max()/clamp() macros that also do
142 * strict type-checking.. See the
143 * "unnecessary" pointer comparison.
145 #define min(x, y) ({ \
146 typeof(x) _min1 = (x); \
147 typeof(y) _min2 = (y); \
148 _min1 < _min2 ? _min1 : _min2; })
150 #define max(x, y) ({ \
151 typeof(x) _max1 = (x); \
152 typeof(y) _max2 = (y); \
153 _max1 > _max2 ? _max1 : _max2; })
155 #define min3(x, y, z) ({ \
156 typeof(x) _min1 = (x); \
157 typeof(y) _min2 = (y); \
158 typeof(z) _min3 = (z); \
159 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
160 (_min2 < _min3 ? _min2 : _min3); })
162 #define max3(x, y, z) ({ \
163 typeof(x) _max1 = (x); \
164 typeof(y) _max2 = (y); \
165 typeof(z) _max3 = (z); \
166 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
167 (_max2 > _max3 ? _max2 : _max3); })
170 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
174 #define min_not_zero(x, y) ({ \
175 typeof(x) __x = (x); \
176 typeof(y) __y = (y); \
177 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
180 * clamp - return a value clamped to a given range with strict typechecking
181 * @val: current value
182 * @lo: lowest allowable value
183 * @hi: highest allowable value
185 * This macro does strict typechecking of lo/hi to make sure they are of the
186 * same type as val. See the unnecessary pointer comparisons.
188 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
191 * ..and if you can't take the strict
192 * types, you can specify one yourself.
194 * Or not use min/max/clamp at all, of course.
196 #define min_t(type, x, y) ({ \
199 __min1 < __min2 ? __min1: __min2; })
201 #define max_t(type, x, y) ({ \
204 __max1 > __max2 ? __max1: __max2; })
207 * clamp_t - return a value clamped to a given range using a given type
208 * @type: the type of variable to use
209 * @val: current value
210 * @lo: minimum allowable value
211 * @hi: maximum allowable value
213 * This macro does no typechecking and uses temporary variables of type
214 * 'type' to make all the comparisons.
216 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
219 * clamp_val - return a value clamped to a given range using val's type
220 * @val: current value
221 * @lo: minimum allowable value
222 * @hi: maximum allowable value
224 * This macro does no typechecking and uses temporary variables of whatever
225 * type the input argument 'val' is. This is useful when val is an unsigned
226 * type and min and max are literals that will otherwise be assigned a signed
229 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
233 * swap - swap value of @a and @b
236 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
239 * container_of - cast a member of a structure out to the containing structure
240 * @ptr: the pointer to the member.
241 * @type: the type of the container struct this is embedded in.
242 * @member: the name of the member within the struct.
245 #define container_of(ptr, type, member) ({ \
246 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
247 (type *)( (char *)__mptr - offsetof(type,member) );})