Lines Matching +full:min +full:- +full:len

1 /* SPDX-License-Identifier: GPL-2.0 */
11 * min()/max()/clamp() macros must accomplish three things:
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
22 * - Unsigned arguments can be compared against non-negative signed constants.
23 * - Comparison of a signed argument against an unsigned constant fails
34 /* True for a non-negative signed int constant */
36 (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0)
56 __builtin_choose_expr(__is_constexpr((x) - (y)), \
75 __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \
81 * min - return minimum of two values of the same or compatible types
85 #define min(x, y) __careful_cmp(min, x, y) macro
88 * max - return maximum of two values of the same or compatible types
95 * umin - return minimum of two non-negative values
101 __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
104 * umax - return maximum of two non-negative values
112 * min3 - return minimum of three values
117 #define min3(x, y, z) min((typeof(x))min(x, y), z)
120 * max3 - return maximum of three values
128 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
135 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
138 * clamp - return a value clamped to a given range with strict typechecking
152 * Or not use min/max/clamp at all, of course.
156 * min_t - return minimum of two values, using the specified type
161 #define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y))
164 * max_t - return maximum of two values, using the specified type
173 * In the following legit use-case where the "array" passed is a simple pointer,
175 * --- 8< ---
178 * min = min_array(buff, nb_items);
179 * --- 8< ---
188 #define __minmax_array(op, array, len) ({ \ argument
190 typeof(len) __len = (len); \
191 __unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
192 while (__len--) \
197 * min_array - return minimum of values present in an array
199 * @len: array length
201 * Note that @len must not be zero (empty array).
203 #define min_array(array, len) __minmax_array(min, array, len) argument
206 * max_array - return maximum of values present in an array
208 * @len: array length
210 * Note that @len must not be zero (empty array).
212 #define max_array(array, len) __minmax_array(max, array, len) argument
215 * clamp_t - return a value clamped to a given range using a given type
227 * clamp_val - return a value clamped to a given range using val's type
239 static inline bool in_range64(u64 val, u64 start, u64 len) in in_range64() argument
241 return (val - start) < len; in in_range64()
244 static inline bool in_range32(u32 val, u32 start, u32 len) in in_range32() argument
246 return (val - start) < len; in in_range32()
250 * in_range - Determine if a value lies within a range.
253 * @len: Number of values in range.
255 * This is more efficient than "if (start <= val && val < (start + len))".
256 * It also gives a different answer if @start + @len overflows the size of
258 * which behaviour you want, or prove that start + len never overflow.
261 #define in_range(val, start, len) \ argument
262 ((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
263 in_range32(val, start, len) : in_range64(val, start, len))
266 * swap - swap values of @a and @b