1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MINMAX_H
3 #define _LINUX_MINMAX_H
6 * min()/max()/clamp() macros must accomplish three things:
8 * - avoid multiple evaluations of the arguments (so side-effects like
9 * "x++" happen only once) when non-constant.
10 * - perform strict type-checking (to generate warnings instead of
11 * nasty runtime surprises). See the "unnecessary" pointer comparison
13 * - retain result as a constant expressions when called with only
14 * constant expressions (to avoid tripping VLA warnings in stack
17 #define __typecheck(x, y) \
18 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
21 * This returns a constant expression while determining if an argument is
22 * a constant expression, most importantly without evaluating the argument.
23 * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
25 #define __is_constexpr(x) \
26 (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
28 #define __no_side_effects(x, y) \
29 (__is_constexpr(x) && __is_constexpr(y))
31 #define __safe_cmp(x, y) \
32 (__typecheck(x, y) && __no_side_effects(x, y))
34 #define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
36 #define __cmp_once(x, y, unique_x, unique_y, op) ({ \
37 typeof(x) unique_x = (x); \
38 typeof(y) unique_y = (y); \
39 __cmp(unique_x, unique_y, op); })
41 #define __careful_cmp(x, y, op) \
42 __builtin_choose_expr(__safe_cmp(x, y), \
44 __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
47 * min - return minimum of two values of the same or compatible types
51 #define min(x, y) __careful_cmp(x, y, <)
54 * max - return maximum of two values of the same or compatible types
58 #define max(x, y) __careful_cmp(x, y, >)
61 * min3 - return minimum of three values
66 #define min3(x, y, z) min((typeof(x))min(x, y), z)
69 * max3 - return maximum of three values
74 #define max3(x, y, z) max((typeof(x))max(x, y), z)
77 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
81 #define min_not_zero(x, y) ({ \
82 typeof(x) __x = (x); \
83 typeof(y) __y = (y); \
84 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
87 * clamp - return a value clamped to a given range with strict typechecking
89 * @lo: lowest allowable value
90 * @hi: highest allowable value
92 * This macro does strict typechecking of @lo/@hi to make sure they are of the
93 * same type as @val. See the unnecessary pointer comparisons.
95 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
98 * ..and if you can't take the strict
99 * types, you can specify one yourself.
101 * Or not use min/max/clamp at all, of course.
105 * min_t - return minimum of two values, using the specified type
106 * @type: data type to use
110 #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
113 * max_t - return maximum of two values, using the specified type
114 * @type: data type to use
118 #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
121 * clamp_t - return a value clamped to a given range using a given type
122 * @type: the type of variable to use
123 * @val: current value
124 * @lo: minimum allowable value
125 * @hi: maximum allowable value
127 * This macro does no typechecking and uses temporary variables of type
128 * @type to make all the comparisons.
130 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
133 * clamp_val - return a value clamped to a given range using val's type
134 * @val: current value
135 * @lo: minimum allowable value
136 * @hi: maximum allowable value
138 * This macro does no typechecking and uses temporary variables of whatever
139 * type the input argument @val is. This is useful when @val is an unsigned
140 * type and @lo and @hi are literals that will otherwise be assigned a signed
143 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
146 * swap - swap values of @a and @b
151 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
153 #endif /* _LINUX_MINMAX_H */