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