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