1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MINMAX_H
3 #define _LINUX_MINMAX_H
5 #include <linux/const.h>
6 #include <linux/types.h>
9 * min()/max()/clamp() macros must accomplish three things:
11 * - avoid multiple evaluations of the arguments (so side-effects like
12 * "x++" happen only once) when non-constant.
13 * - perform strict type-checking (to generate warnings instead of
14 * nasty runtime surprises). See the "unnecessary" pointer comparison
16 * - retain result as a constant expressions when called with only
17 * constant expressions (to avoid tripping VLA warnings in stack
20 #define __typecheck(x, y) \
21 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
23 #define __no_side_effects(x, y) \
24 (__is_constexpr(x) && __is_constexpr(y))
26 #define __safe_cmp(x, y) \
27 (__typecheck(x, y) && __no_side_effects(x, y))
29 #define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
31 #define __cmp_once(x, y, unique_x, unique_y, op) ({ \
32 typeof(x) unique_x = (x); \
33 typeof(y) unique_y = (y); \
34 __cmp(unique_x, unique_y, op); })
36 #define __careful_cmp(x, y, op) \
37 __builtin_choose_expr(__safe_cmp(x, y), \
39 __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
41 #define __clamp(val, lo, hi) \
42 ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
44 #define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \
45 typeof(val) unique_val = (val); \
46 typeof(lo) unique_lo = (lo); \
47 typeof(hi) unique_hi = (hi); \
48 __clamp(unique_val, unique_lo, unique_hi); })
50 #define __clamp_input_check(lo, hi) \
51 (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
52 __is_constexpr((lo) > (hi)), (lo) > (hi), false)))
54 #define __careful_clamp(val, lo, hi) ({ \
55 __clamp_input_check(lo, hi) + \
56 __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \
57 __typecheck(hi, lo) && __is_constexpr(val) && \
58 __is_constexpr(lo) && __is_constexpr(hi), \
59 __clamp(val, lo, hi), \
60 __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
61 __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
64 * min - return minimum of two values of the same or compatible types
68 #define min(x, y) __careful_cmp(x, y, <)
71 * max - return maximum of two values of the same or compatible types
75 #define max(x, y) __careful_cmp(x, y, >)
78 * min3 - return minimum of three values
83 #define min3(x, y, z) min((typeof(x))min(x, y), z)
86 * max3 - return maximum of three values
91 #define max3(x, y, z) max((typeof(x))max(x, y), z)
94 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
98 #define min_not_zero(x, y) ({ \
99 typeof(x) __x = (x); \
100 typeof(y) __y = (y); \
101 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
104 * clamp - return a value clamped to a given range with strict typechecking
105 * @val: current value
106 * @lo: lowest allowable value
107 * @hi: highest allowable value
109 * This macro does strict typechecking of @lo/@hi to make sure they are of the
110 * same type as @val. See the unnecessary pointer comparisons.
112 #define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
115 * ..and if you can't take the strict
116 * types, you can specify one yourself.
118 * Or not use min/max/clamp at all, of course.
122 * min_t - return minimum of two values, using the specified type
123 * @type: data type to use
127 #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
130 * max_t - return maximum of two values, using the specified type
131 * @type: data type to use
135 #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
138 * clamp_t - return a value clamped to a given range using a given type
139 * @type: the type of variable to use
140 * @val: current value
141 * @lo: minimum allowable value
142 * @hi: maximum allowable value
144 * This macro does no typechecking and uses temporary variables of type
145 * @type to make all the comparisons.
147 #define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
150 * clamp_val - return a value clamped to a given range using val's type
151 * @val: current value
152 * @lo: minimum allowable value
153 * @hi: maximum allowable value
155 * This macro does no typechecking and uses temporary variables of whatever
156 * type the input argument @val is. This is useful when @val is an unsigned
157 * type and @lo and @hi are literals that will otherwise be assigned a signed
160 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
162 static inline bool in_range64(u64 val, u64 start, u64 len)
164 return (val - start) < len;
167 static inline bool in_range32(u32 val, u32 start, u32 len)
169 return (val - start) < len;
173 * in_range - Determine if a value lies within a range.
174 * @val: Value to test.
175 * @start: First value in range.
176 * @len: Number of values in range.
178 * This is more efficient than "if (start <= val && val < (start + len))".
179 * It also gives a different answer if @start + @len overflows the size of
180 * the type by a sufficient amount to encompass @val. Decide for yourself
181 * which behaviour you want, or prove that start + len never overflow.
182 * Do not blindly replace one form with the other.
184 #define in_range(val, start, len) \
185 ((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
186 in_range32(val, start, len) : in_range64(val, start, len))
189 * swap - swap values of @a and @b
194 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
196 #endif /* _LINUX_MINMAX_H */