6 #define __ALIGN_LSTDDEF_MASK(x, mask) (((x) + (mask)) & ~(mask))
7 #define __ALIGN_LSTDDEF(x, a) __ALIGN_LSTDDEF_MASK(x, (typeof(x))(a) - 1)
8 #define __LSTDDEF_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
10 #define ALIGN(x, a) __ALIGN_LSTDDEF((x), (a))
11 #define ALIGN_DOWN(x, a) __ALIGN_LSTDDEF((x) - ((a) - 1), (a))
12 #define __ALIGN_MASK(x, mask) __ALIGN_LSTDDEF_MASK((x), (mask))
13 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
14 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
16 #ifndef __must_be_array
17 # define __must_be_array(arr) 0
20 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
23 * This looks more complex than it should be. But we need to
24 * get the type for the ~ right in round_down (it needs to be
25 * as wide as the result!), and we want to evaluate the macro
26 * arguments just once each.
28 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
29 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
30 #define round_down(x, y) ((x) & ~__round_mask(x, y))
32 #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
33 #define DIV_ROUND_UP __USER_DIV_ROUND_UP
35 #define DIV_ROUND_DOWN_ULL(ll, d) \
36 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
38 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
40 #if BITS_PER_LONG == 32
41 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
43 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
46 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
47 #define roundup(x, y) ({ \
48 const typeof(y) __y = y; \
49 (((x) + (__y - 1)) / __y) * __y; \
52 #define rounddown(x, y) ({ \
53 typeof(x) __x = (x); \
58 * Divide positive or negative dividend by positive divisor and round
59 * to closest integer. Result is undefined for negative divisors and
60 * for negative dividends if the divisor variable type is unsigned.
62 #define DIV_ROUND_CLOSEST(x, divisor) ({ \
64 typeof(divisor) __d = divisor; \
65 (((typeof(x))-1) > 0 || \
66 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
67 (((__x) + ((__d) / 2)) / (__d)) : \
68 (((__x) - ((__d) / 2)) / (__d)); \
72 * Same as above but for u64 dividends. divisor must be a 32-bit
75 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
76 typeof(divisor) __d = divisor; \
77 unsigned long long _tmp = (x) + (__d) / 2; \
83 * Multiplies an integer by a fraction, while avoiding unnecessary
84 * overflow or loss of precision.
86 #define mult_frac(x, numer, denom) ({ \
87 typeof(x) quot = (x) / (denom); \
88 typeof(x) rem = (x) % (denom); \
89 (quot * (numer)) + ((rem * (numer)) / (denom)); \
93 * upper_32_bits - return bits 32-63 of a number
94 * @n: the number we're accessing
96 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
97 * the "right shift count >= width of type" warning when that quantity is
100 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
103 * lower_32_bits - return bits 0-31 of a number
104 * @n: the number we're accessing
106 #define lower_32_bits(n) ((__u32)(n))
109 * abs - return absolute value of an argument
110 * @x: the value. If it is unsigned type, it is converted to signed type first
111 * (s64, long or int depending on its size).
113 * Return: an absolute value of x. If x is 64-bit, macro's return type is s64,
114 * otherwise it is signed long.
116 #define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(__s64), ({ \
118 (__x < 0) ? -__x : __x; \
121 if (sizeof(x) == sizeof(long)) { \
123 ret = (__x < 0) ? -__x : __x; \
126 ret = (__x < 0) ? -__x : __x; \
132 * reciprocal_scale - "scale" a value into range [0, ep_ro)
134 * @ep_ro: right open interval endpoint
136 * Perform a "reciprocal multiplication" in order to "scale" a value into
137 * range [0, ep_ro), where the upper interval endpoint is right-open.
138 * This is useful, e.g. for accessing a index of an array containing
139 * ep_ro elements, for example. Think of it as sort of modulus, only that
140 * the result isn't that of modulo. ;) Note that if initial input is a
141 * small value, then result will return 0.
143 * Return: a result based on val in interval [0, ep_ro).
145 static inline __u32 reciprocal_scale(__u32 val, __u32 ep_ro)
147 return (__u32)(((__u64) val * ep_ro) >> 32);
151 * min()/max()/clamp() macros that also do
152 * strict type-checking.. See the
153 * "unnecessary" pointer comparison.
155 #define min(x, y) ({ \
156 typeof(x) _min1 = (x); \
157 typeof(y) _min2 = (y); \
158 (void) (&_min1 == &_min2); \
159 _min1 < _min2 ? _min1 : _min2; \
162 #define max(x, y) ({ \
163 typeof(x) _max1 = (x); \
164 typeof(y) _max2 = (y); \
165 (void) (&_max1 == &_max2); \
166 _max1 > _max2 ? _max1 : _max2; \
169 #define min3(x, y, z) ({ \
170 typeof(x) _min1 = (x); \
171 typeof(y) _min2 = (y); \
172 typeof(z) _min3 = (z); \
173 (void) (&_min1 == &_min2); \
174 (void) (&_min1 == &_min3); \
175 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
176 (_min2 < _min3 ? _min2 : _min3); \
179 #define max3(x, y, z) ({ \
180 typeof(x) _max1 = (x); \
181 typeof(y) _max2 = (y); \
182 typeof(z) _max3 = (z); \
183 (void) (&_max1 == &_max2); \
184 (void) (&_max1 == &_max3); \
185 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
186 (_max2 > _max3 ? _max2 : _max3); \
190 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
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)); \
201 * clamp - return a value clamped to a given range with strict typechecking
202 * @val: current value
203 * @min: minimum allowable value
204 * @max: maximum allowable value
206 * This macro does strict typechecking of min/max to make sure they are of the
207 * same type as val. See the unnecessary pointer comparisons.
209 #define clamp(val, min, max) ({ \
210 typeof(val) __val = (val); \
211 typeof(min) __min = (min); \
212 typeof(max) __max = (max); \
213 (void) (&__val == &__min); \
214 (void) (&__val == &__max); \
215 __val = __val < __min ? __min : __val; \
216 __val > __max ? __max : __val; \
220 * ..and if you can't take the strict
221 * types, you can specify one yourself.
223 * Or not use min/max/clamp at all, of course.
225 #define min_t(type, x, y) ({ \
228 __min1 < __min2 ? __min1 : __min2; \
231 #define max_t(type, x, y) ({ \
234 __max1 > __max2 ? __max1 : __max2; \
238 * clamp_t - return a value clamped to a given range using a given type
239 * @type: the type of variable to use
240 * @val: current value
241 * @min: minimum allowable value
242 * @max: maximum allowable value
244 * This macro does no typechecking and uses temporary variables of type
245 * 'type' to make all the comparisons.
247 #define clamp_t(type, val, min, max) ({ \
248 type __val = (val); \
249 type __min = (min); \
250 type __max = (max); \
251 __val = __val < __min ? __min : __val; \
252 __val > __max ? __max : __val; \
256 * clamp_val - return a value clamped to a given range using val's type
257 * @val: current value
258 * @min: minimum allowable value
259 * @max: maximum allowable value
261 * This macro does no typechecking and uses temporary variables of whatever
262 * type the input argument 'val' is. This is useful when val is an unsigned
263 * type and min and max are literals that will otherwise be assigned a signed
266 #define clamp_val(val, min, max) ({ \
267 typeof(val) __val = (val); \
268 typeof(val) __min = (min); \
269 typeof(val) __max = (max); \
270 __val = __val < __min ? __min : __val; \
271 __val > __max ? __max : __val; \
275 * swap - swap value of @a and @b
277 #define swap(a, b) do { \
278 typeof(a) __tmp = (a); \
284 * container_of - cast a member of a structure out to the containing structure
285 * @ptr: the pointer to the member.
286 * @type: the type of the container struct this is embedded in.
287 * @member: the name of the member within the struct.
290 #define container_of(ptr, type, member) ({ \
291 const typeof(((type *)0)->member) *__mptr = (ptr); \
292 (type *)((char *)__mptr - offsetof(type, member)); \
295 #endif /* !_LSTDDEF_H */