4 #include <linux/types.h>
7 #define __ALIGN_LSTDDEF_MASK(x, mask) (((x) + (mask)) & ~(mask))
8 #define __ALIGN_LSTDDEF(x, a) __ALIGN_LSTDDEF_MASK(x, (typeof(x))(a) - 1)
9 #define __LSTDDEF_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
11 #define ALIGN(x, a) __ALIGN_LSTDDEF((x), (a))
12 #define ALIGN_DOWN(x, a) __ALIGN_LSTDDEF((x) - ((a) - 1), (a))
13 #define __ALIGN_MASK(x, mask) __ALIGN_LSTDDEF_MASK((x), (mask))
14 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
15 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
17 #ifndef __must_be_array
18 # define __must_be_array(arr) 0
21 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
24 * This looks more complex than it should be. But we need to
25 * get the type for the ~ right in round_down (it needs to be
26 * as wide as the result!), and we want to evaluate the macro
27 * arguments just once each.
29 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
30 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
31 #define round_down(x, y) ((x) & ~__round_mask(x, y))
33 #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
34 #define DIV_ROUND_UP __USER_DIV_ROUND_UP
36 #define DIV_ROUND_DOWN_ULL(ll, d) \
37 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
39 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
41 #if BITS_PER_LONG == 32
42 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
44 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
47 #define rounddown(x, y) ({ \
48 typeof(x) __x = (x); \
53 * Divide positive or negative dividend by positive divisor and round
54 * to closest integer. Result is undefined for negative divisors and
55 * for negative dividends if the divisor variable type is unsigned.
57 #define DIV_ROUND_CLOSEST(x, divisor) ({ \
59 typeof(divisor) __d = divisor; \
60 (((typeof(x))-1) > 0 || \
61 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
62 (((__x) + ((__d) / 2)) / (__d)) : \
63 (((__x) - ((__d) / 2)) / (__d)); \
67 * Same as above but for u64 dividends. divisor must be a 32-bit
70 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
71 typeof(divisor) __d = divisor; \
72 unsigned long long _tmp = (x) + (__d) / 2; \
78 * Multiplies an integer by a fraction, while avoiding unnecessary
79 * overflow or loss of precision.
81 #define mult_frac(x, numer, denom) ({ \
82 typeof(x) quot = (x) / (denom); \
83 typeof(x) rem = (x) % (denom); \
84 (quot * (numer)) + ((rem * (numer)) / (denom)); \
88 * upper_32_bits - return bits 32-63 of a number
89 * @n: the number we're accessing
91 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
92 * the "right shift count >= width of type" warning when that quantity is
95 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
98 * lower_32_bits - return bits 0-31 of a number
99 * @n: the number we're accessing
101 #define lower_32_bits(n) ((__u32)(n))
104 * abs - return absolute value of an argument
105 * @x: the value. If it is unsigned type, it is converted to signed type first
106 * (s64, long or int depending on its size).
108 * Return: an absolute value of x. If x is 64-bit, macro's return type is s64,
109 * otherwise it is signed long.
111 #define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(__s64), ({ \
113 (__x < 0) ? -__x : __x; \
116 if (sizeof(x) == sizeof(long)) { \
118 ret = (__x < 0) ? -__x : __x; \
121 ret = (__x < 0) ? -__x : __x; \
127 * reciprocal_scale - "scale" a value into range [0, ep_ro)
129 * @ep_ro: right open interval endpoint
131 * Perform a "reciprocal multiplication" in order to "scale" a value into
132 * range [0, ep_ro), where the upper interval endpoint is right-open.
133 * This is useful, e.g. for accessing a index of an array containing
134 * ep_ro elements, for example. Think of it as sort of modulus, only that
135 * the result isn't that of modulo. ;) Note that if initial input is a
136 * small value, then result will return 0.
138 * Return: a result based on val in interval [0, ep_ro).
140 static inline __u32 reciprocal_scale(__u32 val, __u32 ep_ro)
142 return (__u32)(((__u64) val * ep_ro) >> 32);
146 * min()/max()/clamp() macros that also do
147 * strict type-checking.. See the
148 * "unnecessary" pointer comparison.
150 #define min(x, y) ({ \
151 typeof(x) _min1 = (x); \
152 typeof(y) _min2 = (y); \
153 (void) (&_min1 == &_min2); \
154 _min1 < _min2 ? _min1 : _min2; \
157 #define max(x, y) ({ \
158 typeof(x) _max1 = (x); \
159 typeof(y) _max2 = (y); \
160 (void) (&_max1 == &_max2); \
161 _max1 > _max2 ? _max1 : _max2; \
164 #define min3(x, y, z) ({ \
165 typeof(x) _min1 = (x); \
166 typeof(y) _min2 = (y); \
167 typeof(z) _min3 = (z); \
168 (void) (&_min1 == &_min2); \
169 (void) (&_min1 == &_min3); \
170 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
171 (_min2 < _min3 ? _min2 : _min3); \
174 #define max3(x, y, z) ({ \
175 typeof(x) _max1 = (x); \
176 typeof(y) _max2 = (y); \
177 typeof(z) _max3 = (z); \
178 (void) (&_max1 == &_max2); \
179 (void) (&_max1 == &_max3); \
180 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
181 (_max2 > _max3 ? _max2 : _max3); \
185 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
189 #define min_not_zero(x, y) ({ \
190 typeof(x) __x = (x); \
191 typeof(y) __y = (y); \
192 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
196 * clamp - return a value clamped to a given range with strict typechecking
197 * @val: current value
198 * @min: minimum allowable value
199 * @max: maximum allowable value
201 * This macro does strict typechecking of min/max to make sure they are of the
202 * same type as val. See the unnecessary pointer comparisons.
204 #define clamp(val, min, max) ({ \
205 typeof(val) __val = (val); \
206 typeof(min) __min = (min); \
207 typeof(max) __max = (max); \
208 (void) (&__val == &__min); \
209 (void) (&__val == &__max); \
210 __val = __val < __min ? __min : __val; \
211 __val > __max ? __max : __val; \
215 * ..and if you can't take the strict
216 * types, you can specify one yourself.
218 * Or not use min/max/clamp at all, of course.
220 #define min_t(type, x, y) ({ \
223 __min1 < __min2 ? __min1 : __min2; \
226 #define max_t(type, x, y) ({ \
229 __max1 > __max2 ? __max1 : __max2; \
233 * clamp_t - return a value clamped to a given range using a given type
234 * @type: the type of variable to use
235 * @val: current value
236 * @min: minimum allowable value
237 * @max: maximum allowable value
239 * This macro does no typechecking and uses temporary variables of type
240 * 'type' to make all the comparisons.
242 #define clamp_t(type, val, min, max) ({ \
243 type __val = (val); \
244 type __min = (min); \
245 type __max = (max); \
246 __val = __val < __min ? __min : __val; \
247 __val > __max ? __max : __val; \
251 * clamp_val - return a value clamped to a given range using val's type
252 * @val: current value
253 * @min: minimum allowable value
254 * @max: maximum allowable value
256 * This macro does no typechecking and uses temporary variables of whatever
257 * type the input argument 'val' is. This is useful when val is an unsigned
258 * type and min and max are literals that will otherwise be assigned a signed
261 #define clamp_val(val, min, max) ({ \
262 typeof(val) __val = (val); \
263 typeof(val) __min = (min); \
264 typeof(val) __max = (max); \
265 __val = __val < __min ? __min : __val; \
266 __val > __max ? __max : __val; \
270 * swap - swap value of @a and @b
272 #define swap(a, b) do { \
273 typeof(a) __tmp = (a); \
279 * container_of - cast a member of a structure out to the containing structure
280 * @ptr: the pointer to the member.
281 * @type: the type of the container struct this is embedded in.
282 * @member: the name of the member within the struct.
285 #define container_of(ptr, type, member) ({ \
286 const typeof(((type *)0)->member) *__mptr = (ptr); \
287 (type *)((char *)__mptr - offsetof(type, member)); \
290 #ifndef HAVE_COPY_FILE_RANGE
292 #ifndef __NR_copy_file_range
294 #if defined(_ASM_X86_UNISTD_64_H)
295 #define __NR_copy_file_range 326
296 #elif defined(_ASM_X86_UNISTD_32_H)
297 #define __NR_copy_file_range 285
299 #define __NR_copy_file_range 285
304 static inline loff_t copy_file_range(int fd_in, loff_t *off_in, int fd_out,
305 loff_t *off_out, size_t len,
308 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
309 off_out, len, flags);
311 #endif /* !HAVE_COPY_FILE_RANGE */
313 #endif /* !_LSTDDEF_H */