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 FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
37 #define DIV_ROUND_UP __USER_DIV_ROUND_UP
39 #define DIV_ROUND_DOWN_ULL(ll, d) \
40 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
42 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
44 #if BITS_PER_LONG == 32
45 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
47 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
50 #define rounddown(x, y) ({ \
51 typeof(x) __x = (x); \
56 * Divide positive or negative dividend by positive divisor and round
57 * to closest integer. Result is undefined for negative divisors and
58 * for negative dividends if the divisor variable type is unsigned.
60 #define DIV_ROUND_CLOSEST(x, divisor) ({ \
62 typeof(divisor) __d = divisor; \
63 (((typeof(x))-1) > 0 || \
64 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
65 (((__x) + ((__d) / 2)) / (__d)) : \
66 (((__x) - ((__d) / 2)) / (__d)); \
70 * Same as above but for u64 dividends. divisor must be a 32-bit
73 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
74 typeof(divisor) __d = divisor; \
75 unsigned long long _tmp = (x) + (__d) / 2; \
81 * Multiplies an integer by a fraction, while avoiding unnecessary
82 * overflow or loss of precision.
84 #define mult_frac(x, numer, denom) ({ \
85 typeof(x) quot = (x) / (denom); \
86 typeof(x) rem = (x) % (denom); \
87 (quot * (numer)) + ((rem * (numer)) / (denom)); \
91 * upper_32_bits - return bits 32-63 of a number
92 * @n: the number we're accessing
94 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
95 * the "right shift count >= width of type" warning when that quantity is
98 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
101 * lower_32_bits - return bits 0-31 of a number
102 * @n: the number we're accessing
104 #define lower_32_bits(n) ((__u32)(n))
107 * abs - return absolute value of an argument
108 * @x: the value. If it is unsigned type, it is converted to signed type first
109 * (s64, long or int depending on its size).
111 * Return: an absolute value of x. If x is 64-bit, macro's return type is s64,
112 * otherwise it is signed long.
114 #define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(__s64), ({ \
116 (__x < 0) ? -__x : __x; \
119 if (sizeof(x) == sizeof(long)) { \
121 ret = (__x < 0) ? -__x : __x; \
124 ret = (__x < 0) ? -__x : __x; \
130 * reciprocal_scale - "scale" a value into range [0, ep_ro)
132 * @ep_ro: right open interval endpoint
134 * Perform a "reciprocal multiplication" in order to "scale" a value into
135 * range [0, ep_ro), where the upper interval endpoint is right-open.
136 * This is useful, e.g. for accessing a index of an array containing
137 * ep_ro elements, for example. Think of it as sort of modulus, only that
138 * the result isn't that of modulo. ;) Note that if initial input is a
139 * small value, then result will return 0.
141 * Return: a result based on val in interval [0, ep_ro).
143 static inline __u32 reciprocal_scale(__u32 val, __u32 ep_ro)
145 return (__u32)(((__u64) val * ep_ro) >> 32);
149 * min()/max()/clamp() macros that also do
150 * strict type-checking.. See the
151 * "unnecessary" pointer comparison.
153 #define min(x, y) ({ \
154 typeof(x) _min1 = (x); \
155 typeof(y) _min2 = (y); \
156 (void) (&_min1 == &_min2); \
157 _min1 < _min2 ? _min1 : _min2; \
160 #define max(x, y) ({ \
161 typeof(x) _max1 = (x); \
162 typeof(y) _max2 = (y); \
163 (void) (&_max1 == &_max2); \
164 _max1 > _max2 ? _max1 : _max2; \
167 #define min3(x, y, z) ({ \
168 typeof(x) _min1 = (x); \
169 typeof(y) _min2 = (y); \
170 typeof(z) _min3 = (z); \
171 (void) (&_min1 == &_min2); \
172 (void) (&_min1 == &_min3); \
173 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
174 (_min2 < _min3 ? _min2 : _min3); \
177 #define max3(x, y, z) ({ \
178 typeof(x) _max1 = (x); \
179 typeof(y) _max2 = (y); \
180 typeof(z) _max3 = (z); \
181 (void) (&_max1 == &_max2); \
182 (void) (&_max1 == &_max3); \
183 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
184 (_max2 > _max3 ? _max2 : _max3); \
188 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
192 #define min_not_zero(x, y) ({ \
193 typeof(x) __x = (x); \
194 typeof(y) __y = (y); \
195 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
199 * clamp - return a value clamped to a given range with strict typechecking
200 * @val: current value
201 * @min: minimum allowable value
202 * @max: maximum allowable value
204 * This macro does strict typechecking of min/max to make sure they are of the
205 * same type as val. See the unnecessary pointer comparisons.
207 #define clamp(val, min, max) ({ \
208 typeof(val) __val = (val); \
209 typeof(min) __min = (min); \
210 typeof(max) __max = (max); \
211 (void) (&__val == &__min); \
212 (void) (&__val == &__max); \
213 __val = __val < __min ? __min : __val; \
214 __val > __max ? __max : __val; \
218 * ..and if you can't take the strict
219 * types, you can specify one yourself.
221 * Or not use min/max/clamp at all, of course.
223 #define min_t(type, x, y) ({ \
226 __min1 < __min2 ? __min1 : __min2; \
229 #define max_t(type, x, y) ({ \
232 __max1 > __max2 ? __max1 : __max2; \
236 * clamp_t - return a value clamped to a given range using a given type
237 * @type: the type of variable to use
238 * @val: current value
239 * @min: minimum allowable value
240 * @max: maximum allowable value
242 * This macro does no typechecking and uses temporary variables of type
243 * 'type' to make all the comparisons.
245 #define clamp_t(type, val, min, max) ({ \
246 type __val = (val); \
247 type __min = (min); \
248 type __max = (max); \
249 __val = __val < __min ? __min : __val; \
250 __val > __max ? __max : __val; \
254 * clamp_val - return a value clamped to a given range using val's type
255 * @val: current value
256 * @min: minimum allowable value
257 * @max: maximum allowable value
259 * This macro does no typechecking and uses temporary variables of whatever
260 * type the input argument 'val' is. This is useful when val is an unsigned
261 * type and min and max are literals that will otherwise be assigned a signed
264 #define clamp_val(val, min, max) ({ \
265 typeof(val) __val = (val); \
266 typeof(val) __min = (min); \
267 typeof(val) __max = (max); \
268 __val = __val < __min ? __min : __val; \
269 __val > __max ? __max : __val; \
273 * swap - swap value of @a and @b
275 #define swap(a, b) do { \
276 typeof(a) __tmp = (a); \
282 * container_of - cast a member of a structure out to the containing structure
283 * @ptr: the pointer to the member.
284 * @type: the type of the container struct this is embedded in.
285 * @member: the name of the member within the struct.
288 #define container_of(ptr, type, member) ({ \
289 const typeof(((type *)0)->member) *__mptr = (ptr); \
290 (type *)((char *)__mptr - offsetof(type, member)); \
293 #ifndef HAVE_COPY_FILE_RANGE
295 #ifndef __NR_copy_file_range
297 #if defined(_ASM_X86_UNISTD_64_H)
298 #define __NR_copy_file_range 326
299 #elif defined(_ASM_X86_UNISTD_32_H)
300 #define __NR_copy_file_range 285
302 #define __NR_copy_file_range 285
307 static inline loff_t copy_file_range(int fd_in, loff_t *off_in, int fd_out,
308 loff_t *off_out, size_t len,
311 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
312 off_out, len, flags);
314 #endif /* !HAVE_COPY_FILE_RANGE */
316 #endif /* !_LSTDDEF_H */