1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MINMAX_H
3 #define _LINUX_MINMAX_H
5 #include <linux/build_bug.h>
6 #include <linux/compiler.h>
7 #include <linux/const.h>
8 #include <linux/types.h>
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 * - perform strict type-checking (to generate warnings instead of
16 * nasty runtime surprises). See the "unnecessary" pointer comparison
18 * - retain result as a constant expressions when called with only
19 * constant expressions (to avoid tripping VLA warnings in stack
22 #define __typecheck(x, y) \
23 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
25 #define __no_side_effects(x, y) \
26 (__is_constexpr(x) && __is_constexpr(y))
28 #define __safe_cmp(x, y) \
29 (__typecheck(x, y) && __no_side_effects(x, y))
31 #define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
33 #define __cmp_once(x, y, unique_x, unique_y, op) ({ \
34 typeof(x) unique_x = (x); \
35 typeof(y) unique_y = (y); \
36 __cmp(unique_x, unique_y, op); })
38 #define __careful_cmp(x, y, op) \
39 __builtin_choose_expr(__safe_cmp(x, y), \
41 __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
43 #define __clamp(val, lo, hi) \
44 ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
46 #define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \
47 typeof(val) unique_val = (val); \
48 typeof(lo) unique_lo = (lo); \
49 typeof(hi) unique_hi = (hi); \
50 __clamp(unique_val, unique_lo, unique_hi); })
52 #define __clamp_input_check(lo, hi) \
53 (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
54 __is_constexpr((lo) > (hi)), (lo) > (hi), false)))
56 #define __careful_clamp(val, lo, hi) ({ \
57 __clamp_input_check(lo, hi) + \
58 __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \
59 __typecheck(hi, lo) && __is_constexpr(val) && \
60 __is_constexpr(lo) && __is_constexpr(hi), \
61 __clamp(val, lo, hi), \
62 __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
63 __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
66 * min - return minimum of two values of the same or compatible types
70 #define min(x, y) __careful_cmp(x, y, <)
73 * max - return maximum of two values of the same or compatible types
77 #define max(x, y) __careful_cmp(x, y, >)
80 * umin - return minimum of two non-negative values
81 * Signed types are zero extended to match a larger unsigned type.
86 __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <)
89 * umax - return maximum of two non-negative values
94 __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, >)
97 * min3 - return minimum of three values
102 #define min3(x, y, z) min((typeof(x))min(x, y), z)
105 * max3 - return maximum of three values
110 #define max3(x, y, z) max((typeof(x))max(x, y), z)
113 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
117 #define min_not_zero(x, y) ({ \
118 typeof(x) __x = (x); \
119 typeof(y) __y = (y); \
120 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
123 * clamp - return a value clamped to a given range with strict typechecking
124 * @val: current value
125 * @lo: lowest allowable value
126 * @hi: highest allowable value
128 * This macro does strict typechecking of @lo/@hi to make sure they are of the
129 * same type as @val. See the unnecessary pointer comparisons.
131 #define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
134 * ..and if you can't take the strict
135 * types, you can specify one yourself.
137 * Or not use min/max/clamp at all, of course.
141 * min_t - return minimum of two values, using the specified type
142 * @type: data type to use
146 #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
149 * max_t - return maximum of two values, using the specified type
150 * @type: data type to use
154 #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
157 * Do not check the array parameter using __must_be_array().
158 * In the following legit use-case where the "array" passed is a simple pointer,
159 * __must_be_array() will return a failure.
163 * min = min_array(buff, nb_items);
166 * The first typeof(&(array)[0]) is needed in order to support arrays of both
167 * 'int *buff' and 'int buff[N]' types.
169 * The array can be an array of const items.
170 * typeof() keeps the const qualifier. Use __unqual_scalar_typeof() in order
171 * to discard the const qualifier for the __element variable.
173 #define __minmax_array(op, array, len) ({ \
174 typeof(&(array)[0]) __array = (array); \
175 typeof(len) __len = (len); \
176 __unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
178 __element = op(__element, __array[__len]); \
182 * min_array - return minimum of values present in an array
186 * Note that @len must not be zero (empty array).
188 #define min_array(array, len) __minmax_array(min, array, len)
191 * max_array - return maximum of values present in an array
195 * Note that @len must not be zero (empty array).
197 #define max_array(array, len) __minmax_array(max, array, len)
200 * clamp_t - return a value clamped to a given range using a given type
201 * @type: the type of variable to use
202 * @val: current value
203 * @lo: minimum allowable value
204 * @hi: maximum allowable value
206 * This macro does no typechecking and uses temporary variables of type
207 * @type to make all the comparisons.
209 #define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
212 * clamp_val - return a value clamped to a given range using val's type
213 * @val: current value
214 * @lo: minimum allowable value
215 * @hi: maximum allowable value
217 * This macro does no typechecking and uses temporary variables of whatever
218 * type the input argument @val is. This is useful when @val is an unsigned
219 * type and @lo and @hi are literals that will otherwise be assigned a signed
222 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
224 static inline bool in_range64(u64 val, u64 start, u64 len)
226 return (val - start) < len;
229 static inline bool in_range32(u32 val, u32 start, u32 len)
231 return (val - start) < len;
235 * in_range - Determine if a value lies within a range.
236 * @val: Value to test.
237 * @start: First value in range.
238 * @len: Number of values in range.
240 * This is more efficient than "if (start <= val && val < (start + len))".
241 * It also gives a different answer if @start + @len overflows the size of
242 * the type by a sufficient amount to encompass @val. Decide for yourself
243 * which behaviour you want, or prove that start + len never overflow.
244 * Do not blindly replace one form with the other.
246 #define in_range(val, start, len) \
247 ((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
248 in_range32(val, start, len) : in_range64(val, start, len))
251 * swap - swap values of @a and @b
256 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
258 #endif /* _LINUX_MINMAX_H */