5 #include <linux/types.h>
7 #include <sys/syscall.h>
10 #define __ALIGN_LSTDDEF_MASK(x, mask) (((x) + (mask)) & ~(mask))
11 #define __ALIGN_LSTDDEF(x, a) __ALIGN_LSTDDEF_MASK(x, (typeof(x))(a) - 1)
12 #define __LSTDDEF_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
14 #define ALIGN(x, a) __ALIGN_LSTDDEF((x), (a))
15 #define ALIGN_DOWN(x, a) __ALIGN_LSTDDEF((x) - ((a) - 1), (a))
16 #define __ALIGN_MASK(x, mask) __ALIGN_LSTDDEF_MASK((x), (mask))
17 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
18 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
20 #ifndef __must_be_array
21 # define __must_be_array(arr) 0
24 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
27 * This looks more complex than it should be. But we need to
28 * get the type for the ~ right in round_down (it needs to be
29 * as wide as the result!), and we want to evaluate the macro
30 * arguments just once each.
32 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
33 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
34 #define round_down(x, y) ((x) & ~__round_mask(x, y))
36 #define DIV_ROUND_UP __USER_DIV_ROUND_UP
38 #define DIV_ROUND_DOWN_ULL(ll, d) \
39 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
41 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
43 #if BITS_PER_LONG == 32
44 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
46 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
49 #define rounddown(x, y) ({ \
50 typeof(x) __x = (x); \
55 * Divide positive or negative dividend by positive divisor and round
56 * to closest integer. Result is undefined for negative divisors and
57 * for negative dividends if the divisor variable type is unsigned.
59 #define DIV_ROUND_CLOSEST(x, divisor) ({ \
61 typeof(divisor) __d = divisor; \
62 (((typeof(x))-1) > 0 || \
63 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
64 (((__x) + ((__d) / 2)) / (__d)) : \
65 (((__x) - ((__d) / 2)) / (__d)); \
69 * Same as above but for u64 dividends. divisor must be a 32-bit
72 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
73 typeof(divisor) __d = divisor; \
74 unsigned long long _tmp = (x) + (__d) / 2; \
80 * Multiplies an integer by a fraction, while avoiding unnecessary
81 * overflow or loss of precision.
83 #define mult_frac(x, numer, denom) ({ \
84 typeof(x) quot = (x) / (denom); \
85 typeof(x) rem = (x) % (denom); \
86 (quot * (numer)) + ((rem * (numer)) / (denom)); \
90 * upper_32_bits - return bits 32-63 of a number
91 * @n: the number we're accessing
93 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
94 * the "right shift count >= width of type" warning when that quantity is
97 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
100 * lower_32_bits - return bits 0-31 of a number
101 * @n: the number we're accessing
103 #define lower_32_bits(n) ((__u32)(n))
106 * abs - return absolute value of an argument
107 * @x: the value. If it is unsigned type, it is converted to signed type first
108 * (s64, long or int depending on its size).
110 * Return: an absolute value of x. If x is 64-bit, macro's return type is s64,
111 * otherwise it is signed long.
113 #define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(__s64), ({ \
115 (__x < 0) ? -__x : __x; \
118 if (sizeof(x) == sizeof(long)) { \
120 ret = (__x < 0) ? -__x : __x; \
123 ret = (__x < 0) ? -__x : __x; \
129 * reciprocal_scale - "scale" a value into range [0, ep_ro)
131 * @ep_ro: right open interval endpoint
133 * Perform a "reciprocal multiplication" in order to "scale" a value into
134 * range [0, ep_ro), where the upper interval endpoint is right-open.
135 * This is useful, e.g. for accessing a index of an array containing
136 * ep_ro elements, for example. Think of it as sort of modulus, only that
137 * the result isn't that of modulo. ;) Note that if initial input is a
138 * small value, then result will return 0.
140 * Return: a result based on val in interval [0, ep_ro).
142 static inline __u32 reciprocal_scale(__u32 val, __u32 ep_ro)
144 return (__u32)(((__u64) val * ep_ro) >> 32);
148 * min()/max()/clamp() macros that also do
149 * strict type-checking.. See the
150 * "unnecessary" pointer comparison.
152 #define min(x, y) ({ \
153 typeof(x) _min1 = (x); \
154 typeof(y) _min2 = (y); \
155 (void) (&_min1 == &_min2); \
156 _min1 < _min2 ? _min1 : _min2; \
159 #define max(x, y) ({ \
160 typeof(x) _max1 = (x); \
161 typeof(y) _max2 = (y); \
162 (void) (&_max1 == &_max2); \
163 _max1 > _max2 ? _max1 : _max2; \
166 #define min3(x, y, z) ({ \
167 typeof(x) _min1 = (x); \
168 typeof(y) _min2 = (y); \
169 typeof(z) _min3 = (z); \
170 (void) (&_min1 == &_min2); \
171 (void) (&_min1 == &_min3); \
172 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
173 (_min2 < _min3 ? _min2 : _min3); \
176 #define max3(x, y, z) ({ \
177 typeof(x) _max1 = (x); \
178 typeof(y) _max2 = (y); \
179 typeof(z) _max3 = (z); \
180 (void) (&_max1 == &_max2); \
181 (void) (&_max1 == &_max3); \
182 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
183 (_max2 > _max3 ? _max2 : _max3); \
187 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
191 #define min_not_zero(x, y) ({ \
192 typeof(x) __x = (x); \
193 typeof(y) __y = (y); \
194 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
198 * clamp - return a value clamped to a given range with strict typechecking
199 * @val: current value
200 * @min: minimum allowable value
201 * @max: maximum allowable value
203 * This macro does strict typechecking of min/max to make sure they are of the
204 * same type as val. See the unnecessary pointer comparisons.
206 #define clamp(val, min, max) ({ \
207 typeof(val) __val = (val); \
208 typeof(min) __min = (min); \
209 typeof(max) __max = (max); \
210 (void) (&__val == &__min); \
211 (void) (&__val == &__max); \
212 __val = __val < __min ? __min : __val; \
213 __val > __max ? __max : __val; \
217 * ..and if you can't take the strict
218 * types, you can specify one yourself.
220 * Or not use min/max/clamp at all, of course.
222 #define min_t(type, x, y) ({ \
225 __min1 < __min2 ? __min1 : __min2; \
228 #define max_t(type, x, y) ({ \
231 __max1 > __max2 ? __max1 : __max2; \
235 * clamp_t - return a value clamped to a given range using a given type
236 * @type: the type of variable to use
237 * @val: current value
238 * @min: minimum allowable value
239 * @max: maximum allowable value
241 * This macro does no typechecking and uses temporary variables of type
242 * 'type' to make all the comparisons.
244 #define clamp_t(type, val, min, max) ({ \
245 type __val = (val); \
246 type __min = (min); \
247 type __max = (max); \
248 __val = __val < __min ? __min : __val; \
249 __val > __max ? __max : __val; \
253 * clamp_val - return a value clamped to a given range using val's type
254 * @val: current value
255 * @min: minimum allowable value
256 * @max: maximum allowable value
258 * This macro does no typechecking and uses temporary variables of whatever
259 * type the input argument 'val' is. This is useful when val is an unsigned
260 * type and min and max are literals that will otherwise be assigned a signed
263 #define clamp_val(val, min, max) ({ \
264 typeof(val) __val = (val); \
265 typeof(val) __min = (min); \
266 typeof(val) __max = (max); \
267 __val = __val < __min ? __min : __val; \
268 __val > __max ? __max : __val; \
272 * swap - swap value of @a and @b
274 #define swap(a, b) do { \
275 typeof(a) __tmp = (a); \
281 * container_of - cast a member of a structure out to the containing structure
282 * @ptr: the pointer to the member.
283 * @type: the type of the container struct this is embedded in.
284 * @member: the name of the member within the struct.
287 #define container_of(ptr, type, member) ({ \
288 const typeof(((type *)0)->member) *__mptr = (ptr); \
289 (type *)((char *)__mptr - offsetof(type, member)); \
292 #ifndef HAVE_COPY_FILE_RANGE
294 #ifndef __NR_copy_file_range
296 #if defined(_ASM_X86_UNISTD_64_H)
297 #define __NR_copy_file_range 326
298 #elif defined(_ASM_X86_UNISTD_32_H)
299 #define __NR_copy_file_range 285
301 #define __NR_copy_file_range 285
306 static inline loff_t copy_file_range(int fd_in, loff_t *off_in, int fd_out,
307 loff_t *off_out, size_t len,
310 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
311 off_out, len, flags);
313 #endif /* !HAVE_COPY_FILE_RANGE */
315 #endif /* !_LSTDDEF_H */