Lines Matching +full:len +full:- +full:or +full:- +full:define

1 /* SPDX-License-Identifier: GPL-2.0 */
3 #define _LINUX_MINMAX_H
13 * - Avoid multiple evaluations of the arguments (so side-effects like
14 * "x++" happen only once) when non-constant.
15 * - Retain result as a constant expressions when called with only
18 * - Perform signed v unsigned type-checking (to generate compile
20 * - Unsigned char/short are always promoted to signed int and can be
21 * compared against signed or unsigned arguments.
22 * - Unsigned arguments can be compared against non-negative signed constants.
23 * - Comparison of a signed argument against an unsigned constant fails
26 #define __typecheck(x, y) \
34 * In particular, statically non-negative signed integer
47 * with sizeof() or __builtin_constant_p() etc).
54 #define __signed_type_use(x,ux) (2+__is_nonneg(x,ux))
55 #define __unsigned_type_use(x,ux) (1+2*(sizeof(ux)<4))
56 #define __sign_use(x,ux) (is_signed_type(typeof(ux))? \
63 * On 64-bit we can just always use 'long', since any
64 * integer or pointer type can just be cast to that.
66 * This does not work for 128-bit signed integers since
75 #define __signed_type(ux) long
77 #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux)>4,1LL,1L))
79 #define __is_nonneg(x,ux) statically_true((__signed_type(ux))(x)>=0)
81 #define __types_ok(x,y,ux,uy) \
84 #define __types_ok3(x,y,z,ux,uy,uz) \
87 #define __cmp_op_min <
88 #define __cmp_op_max >
90 #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
92 #define __cmp_once_unique(op, type, x, y, ux, uy) \
95 #define __cmp_once(op, type, x, y) \
98 #define __careful_cmp_once(op, x, y, ux, uy) ({ \
104 #define __careful_cmp(op, x, y) \
107 #define __clamp(val, lo, hi) \
110 #define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \
121 #define __careful_clamp(val, lo, hi) \
125 * min - return minimum of two values of the same or compatible types
129 #define min(x, y) __careful_cmp(min, x, y)
132 * max - return maximum of two values of the same or compatible types
136 #define max(x, y) __careful_cmp(max, x, y)
139 * umin - return minimum of two non-negative values
144 #define umin(x, y) \
148 * umax - return maximum of two non-negative values
152 #define umax(x, y) \
155 #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \
162 * min3 - return minimum of three values
167 #define min3(x, y, z) \
171 * max3 - return maximum of three values
176 #define max3(x, y, z) \
180 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
184 #define min_not_zero(x, y) ({ \
190 * clamp - return a value clamped to a given range with strict typechecking
198 #define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
204 * Or not use min/max/clamp at all, of course.
208 * min_t - return minimum of two values, using the specified type
213 #define min_t(type, x, y) __cmp_once(min, type, x, y)
216 * max_t - return maximum of two values, using the specified type
221 #define max_t(type, x, y) __cmp_once(max, type, x, y)
225 * In the following legit use-case where the "array" passed is a simple pointer,
227 * --- 8< ---
231 * --- 8< ---
240 #define __minmax_array(op, array, len) ({ \ argument
242 typeof(len) __len = (len); \
243 __unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
244 while (__len--) \
249 * min_array - return minimum of values present in an array
251 * @len: array length
253 * Note that @len must not be zero (empty array).
255 #define min_array(array, len) __minmax_array(min, array, len) argument
258 * max_array - return maximum of values present in an array
260 * @len: array length
262 * Note that @len must not be zero (empty array).
264 #define max_array(array, len) __minmax_array(max, array, len) argument
267 * clamp_t - return a value clamped to a given range using a given type
276 #define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
279 * clamp_val - return a value clamped to a given range using val's type
289 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
291 static inline bool in_range64(u64 val, u64 start, u64 len) in in_range64() argument
293 return (val - start) < len; in in_range64()
296 static inline bool in_range32(u32 val, u32 start, u32 len) in in_range32() argument
298 return (val - start) < len; in in_range32()
302 * in_range - Determine if a value lies within a range.
305 * @len: Number of values in range.
307 * This is more efficient than "if (start <= val && val < (start + len))".
308 * It also gives a different answer if @start + @len overflows the size of
310 * which behaviour you want, or prove that start + len never overflow.
313 #define in_range(val, start, len) \ argument
314 ((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
315 in_range32(val, start, len) : in_range64(val, start, len))
318 * swap - swap values of @a and @b
322 #define swap(a, b) \
329 #define MIN(a,b) __cmp(min,a,b)
330 #define MAX(a,b) __cmp(max,a,b)
331 #define MIN_T(type,a,b) __cmp(min,(type)(a),(type)(b))
332 #define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b))