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 * - Perform signed v unsigned type-checking (to generate compile
17 * - Unsigned char/short are always promoted to signed int and can be
18 * compared against signed or unsigned arguments.
19 * - Unsigned arguments can be compared against non-negative signed constants.
20 * - Comparison of a signed argument against an unsigned constant fails
23 #define __typecheck(x, y) \
31 * In particular, statically non-negative signed integer expressions
43 * Careful to only ever evaluate it with sizeof() or __builtin_constant_p() etc).
49 #define __sign_use(ux) (is_signed_type(typeof(ux)) ? \
53 * Check whether a signed value is always non-negative.
58 * On 64-bit any integer or pointer type can safely be cast to 'long long'.
59 * But on 32-bit we need to avoid warnings about casting pointers to integers
60 * of different sizes without truncating 64-bit values so 'long' or 'long long'
63 * This does not work for 128-bit signed integers since the cast would truncate
68 #define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
70 #define __is_nonneg(ux) statically_true( \
74 #define __types_ok(ux, uy) \
77 #define __types_ok3(ux, uy, uz) \
80 #define __cmp_op_min <
81 #define __cmp_op_max >
83 #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
85 #define __cmp_once_unique(op, type, x, y, ux, uy) \
88 #define __cmp_once(op, type, x, y) \
91 #define __careful_cmp_once(op, x, y, ux, uy) ({ \
97 #define __careful_cmp(op, x, y) \
101 * min - return minimum of two values of the same or compatible types
105 #define min(x, y) __careful_cmp(min, x, y)
108 * max - return maximum of two values of the same or compatible types
112 #define max(x, y) __careful_cmp(max, x, y)
115 * umin - return minimum of two non-negative values
120 #define umin(x, y) \
124 * umax - return maximum of two non-negative values
128 #define umax(x, y) \
131 #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \
138 * min3 - return minimum of three values
143 #define min3(x, y, z) \
147 * max3 - return maximum of three values
152 #define max3(x, y, z) \
156 * min_t - return minimum of two values, using the specified type
161 #define min_t(type, x, y) __cmp_once(min, type, x, y)
164 * max_t - return maximum of two values, using the specified type
169 #define max_t(type, x, y) __cmp_once(max, type, x, y)
172 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
176 #define min_not_zero(x, y) ({ \
181 #define __clamp(val, lo, hi) \
184 #define __clamp_once(type, val, lo, hi, uval, ulo, uhi) ({ \
194 #define __careful_clamp(type, val, lo, hi) \
198 * clamp - return a value clamped to a given range with typechecking
206 #define clamp(val, lo, hi) __careful_clamp(__auto_type, val, lo, hi)
209 * clamp_t - return a value clamped to a given range using a given type
218 #define clamp_t(type, val, lo, hi) __careful_clamp(type, val, lo, hi)
221 * clamp_val - return a value clamped to a given range using val's type
231 #define clamp_val(val, lo, hi) __careful_clamp(typeof(val), val, lo, hi)
235 * In the following legit use-case where the "array" passed is a simple pointer,
237 * --- 8< ---
241 * --- 8< ---
250 #define __minmax_array(op, array, len) ({ \ argument
252 typeof(len) __len = (len); \
253 __unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
254 while (__len--) \
259 * min_array - return minimum of values present in an array
261 * @len: array length
263 * Note that @len must not be zero (empty array).
265 #define min_array(array, len) __minmax_array(min, array, len) argument
268 * max_array - return maximum of values present in an array
270 * @len: array length
272 * Note that @len must not be zero (empty array).
274 #define max_array(array, len) __minmax_array(max, array, len) argument
276 static inline bool in_range64(u64 val, u64 start, u64 len) in in_range64() argument
278 return (val - start) < len; in in_range64()
281 static inline bool in_range32(u32 val, u32 start, u32 len) in in_range32() argument
283 return (val - start) < len; in in_range32()
287 * in_range - Determine if a value lies within a range.
290 * @len: Number of values in range.
292 * This is more efficient than "if (start <= val && val < (start + len))".
293 * It also gives a different answer if @start + @len overflows the size of
295 * which behaviour you want, or prove that start + len never overflow.
298 #define in_range(val, start, len) \ argument
299 ((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
300 in_range32(val, start, len) : in_range64(val, start, len))
303 * swap - swap values of @a and @b
307 #define swap(a, b) \
314 #define MIN(a, b) __cmp(min, a, b)
315 #define MAX(a, b) __cmp(max, a, b)
316 #define MIN_T(type, a, b) __cmp(min, (type)(a), (type)(b))
317 #define MAX_T(type, a, b) __cmp(max, (type)(a), (type)(b))