1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MINMAX_H
3 #define _LINUX_MINMAX_H
5 #include <linux/const.h>
8 * min()/max()/clamp() macros must accomplish three things:
10 * - avoid multiple evaluations of the arguments (so side-effects like
11 * "x++" happen only once) when non-constant.
12 * - perform strict type-checking (to generate warnings instead of
13 * nasty runtime surprises). See the "unnecessary" pointer comparison
15 * - retain result as a constant expressions when called with only
16 * constant expressions (to avoid tripping VLA warnings in stack
19 #define __typecheck(x, y) \
20 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
22 #define __no_side_effects(x, y) \
23 (__is_constexpr(x) && __is_constexpr(y))
25 #define __safe_cmp(x, y) \
26 (__typecheck(x, y) && __no_side_effects(x, y))
28 #define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
30 #define __cmp_once(x, y, unique_x, unique_y, op) ({ \
31 typeof(x) unique_x = (x); \
32 typeof(y) unique_y = (y); \
33 __cmp(unique_x, unique_y, op); })
35 #define __careful_cmp(x, y, op) \
36 __builtin_choose_expr(__safe_cmp(x, y), \
38 __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
40 #define __clamp(val, lo, hi) \
41 ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
43 #define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \
44 typeof(val) unique_val = (val); \
45 typeof(lo) unique_lo = (lo); \
46 typeof(hi) unique_hi = (hi); \
47 __clamp(unique_val, unique_lo, unique_hi); })
49 #define __clamp_input_check(lo, hi) \
50 (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
51 __is_constexpr((lo) > (hi)), (lo) > (hi), false)))
53 #define __careful_clamp(val, lo, hi) ({ \
54 __clamp_input_check(lo, hi) + \
55 __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \
56 __typecheck(hi, lo) && __is_constexpr(val) && \
57 __is_constexpr(lo) && __is_constexpr(hi), \
58 __clamp(val, lo, hi), \
59 __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
60 __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
63 * min - return minimum of two values of the same or compatible types
67 #define min(x, y) __careful_cmp(x, y, <)
70 * max - return maximum of two values of the same or compatible types
74 #define max(x, y) __careful_cmp(x, y, >)
77 * min3 - return minimum of three values
82 #define min3(x, y, z) min((typeof(x))min(x, y), z)
85 * max3 - return maximum of three values
90 #define max3(x, y, z) max((typeof(x))max(x, y), z)
93 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
97 #define min_not_zero(x, y) ({ \
98 typeof(x) __x = (x); \
99 typeof(y) __y = (y); \
100 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
103 * clamp - return a value clamped to a given range with strict typechecking
104 * @val: current value
105 * @lo: lowest allowable value
106 * @hi: highest allowable value
108 * This macro does strict typechecking of @lo/@hi to make sure they are of the
109 * same type as @val. See the unnecessary pointer comparisons.
111 #define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
114 * ..and if you can't take the strict
115 * types, you can specify one yourself.
117 * Or not use min/max/clamp at all, of course.
121 * min_t - return minimum of two values, using the specified type
122 * @type: data type to use
126 #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
129 * max_t - return maximum of two values, using the specified type
130 * @type: data type to use
134 #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
137 * clamp_t - return a value clamped to a given range using a given type
138 * @type: the type of variable to use
139 * @val: current value
140 * @lo: minimum allowable value
141 * @hi: maximum allowable value
143 * This macro does no typechecking and uses temporary variables of type
144 * @type to make all the comparisons.
146 #define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
149 * clamp_val - return a value clamped to a given range using val's type
150 * @val: current value
151 * @lo: minimum allowable value
152 * @hi: maximum allowable value
154 * This macro does no typechecking and uses temporary variables of whatever
155 * type the input argument @val is. This is useful when @val is an unsigned
156 * type and @lo and @hi are literals that will otherwise be assigned a signed
159 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
162 * swap - swap values of @a and @b
167 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
169 #endif /* _LINUX_MINMAX_H */