1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MINMAX_H
3 #define _LINUX_MINMAX_H
4
5 #include "linux/const.h"
6 #include "linux/compiler.h"
7 #include "types.h"
8
9 /*
10 * min()/max()/clamp() macros must accomplish three things:
11 *
12 * - avoid multiple evaluations of the arguments (so side-effects like
13 * "x++" happen only once) when non-constant.
14 * - perform strict type-checking (to generate warnings instead of
15 * nasty runtime surprises). See the "unnecessary" pointer comparison
16 * in __typecheck().
17 * - retain result as a constant expressions when called with only
18 * constant expressions (to avoid tripping VLA warnings in stack
19 * allocation usage).
20 */
21 #define __typecheck(x, y) (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
22
23 #define __no_side_effects(x, y) (__is_constexpr(x) && __is_constexpr(y))
24
25 #define __safe_cmp(x, y) (__typecheck(x, y) && __no_side_effects(x, y))
26
27 #define __cmp(x, y, op) ((x)op(y) ? (x) : (y))
28
29 #define __cmp_once(x, y, unique_x, unique_y, op) \
30 ({ \
31 typeof(x) unique_x = (x); \
32 typeof(y) unique_y = (y); \
33 __cmp(unique_x, unique_y, op); \
34 })
35
36 #define __careful_cmp(x, y, op) \
37 __builtin_choose_expr(__safe_cmp(x, y), __cmp(x, y, op), \
38 __cmp_once(x, y, __UNIQUE_ID(__x), \
39 __UNIQUE_ID(__y), op))
40
41 #define __clamp(val, lo, hi) \
42 ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
43
44 #define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) \
45 ({ \
46 typeof(val) unique_val = (val); \
47 typeof(lo) unique_lo = (lo); \
48 typeof(hi) unique_hi = (hi); \
49 __clamp(unique_val, unique_lo, unique_hi); \
50 })
51
52 #define __clamp_input_check(lo, hi) \
53 (BUILD_BUG_ON_ZERO(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \
54 (lo) > (hi), false)))
55
56 #define __careful_clamp(val, lo, hi) \
57 ({ \
58 __clamp_input_check(lo, hi) + \
59 __builtin_choose_expr( \
60 __typecheck(val, lo) && \
61 __typecheck(val, hi) && \
62 __typecheck(hi, lo) && \
63 __is_constexpr(val) && \
64 __is_constexpr(lo) && \
65 __is_constexpr(hi), \
66 __clamp(val, lo, hi), \
67 __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
68 __UNIQUE_ID(__lo), \
69 __UNIQUE_ID(__hi))); \
70 })
71
72 /**
73 * min - return minimum of two values of the same or compatible types
74 * @x: first value
75 * @y: second value
76 */
77 #define min(x, y) __careful_cmp(x, y, <)
78
79 /**
80 * max - return maximum of two values of the same or compatible types
81 * @x: first value
82 * @y: second value
83 */
84 #define max(x, y) __careful_cmp(x, y, >)
85
86 /**
87 * min3 - return minimum of three values
88 * @x: first value
89 * @y: second value
90 * @z: third value
91 */
92 #define min3(x, y, z) min((typeof(x))min(x, y), z)
93
94 /**
95 * max3 - return maximum of three values
96 * @x: first value
97 * @y: second value
98 * @z: third value
99 */
100 #define max3(x, y, z) max((typeof(x))max(x, y), z)
101
102 /**
103 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
104 * @x: value1
105 * @y: value2
106 */
107 #define min_not_zero(x, y) \
108 ({ \
109 typeof(x) __x = (x); \
110 typeof(y) __y = (y); \
111 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
112 })
113
114 /**
115 * clamp - return a value clamped to a given range with strict typechecking
116 * @val: current value
117 * @lo: lowest allowable value
118 * @hi: highest allowable value
119 *
120 * This macro does strict typechecking of @lo/@hi to make sure they are of the
121 * same type as @val. See the unnecessary pointer comparisons.
122 */
123 #define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
124
125 /*
126 * ..and if you can't take the strict
127 * types, you can specify one yourself.
128 *
129 * Or not use min/max/clamp at all, of course.
130 */
131
132 /**
133 * min_t - return minimum of two values, using the specified type
134 * @type: data type to use
135 * @x: first value
136 * @y: second value
137 */
138 #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
139
140 /**
141 * max_t - return maximum of two values, using the specified type
142 * @type: data type to use
143 * @x: first value
144 * @y: second value
145 */
146 #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
147
148 /*
149 * Remove a const qualifier from integer types
150 * _Generic(foo, type-name: association, ..., default: association) performs a
151 * comparison against the foo type (not the qualified type).
152 * Do not use the const keyword in the type-name as it will not match the
153 * unqualified type of foo.
154 */
155 #define __unconst_integer_type_cases(type) \
156 unsigned type : (unsigned type)0, signed type : (signed type)0
157
158 #define __unconst_integer_typeof(x) \
159 typeof(_Generic((x), char \
160 : (char)0, __unconst_integer_type_cases(char), \
161 __unconst_integer_type_cases(short), \
162 __unconst_integer_type_cases(int), \
163 __unconst_integer_type_cases(long), \
164 __unconst_integer_type_cases(long long), default \
165 : (x)))
166
167 /*
168 * Do not check the array parameter using __must_be_array().
169 * In the following legit use-case where the "array" passed is a simple pointer,
170 * __must_be_array() will return a failure.
171 * --- 8< ---
172 * int *buff
173 * ...
174 * min = min_array(buff, nb_items);
175 * --- 8< ---
176 *
177 * The first typeof(&(array)[0]) is needed in order to support arrays of both
178 * 'int *buff' and 'int buff[N]' types.
179 *
180 * The array can be an array of const items.
181 * typeof() keeps the const qualifier. Use __unconst_integer_typeof() in order
182 * to discard the const qualifier for the __element variable.
183 */
184 #define __minmax_array(op, array, len) \
185 ({ \
186 typeof(&(array)[0]) __array = (array); \
187 typeof(len) __len = (len); \
188 __unconst_integer_typeof(__array[0]) \
189 __element = __array[--__len]; \
190 while (__len--) \
191 __element = op(__element, __array[__len]); \
192 __element; \
193 })
194
195 /**
196 * min_array - return minimum of values present in an array
197 * @array: array
198 * @len: array length
199 *
200 * Note that @len must not be zero (empty array).
201 */
202 #define min_array(array, len) __minmax_array(min, array, len)
203
204 /**
205 * max_array - return maximum of values present in an array
206 * @array: array
207 * @len: array length
208 *
209 * Note that @len must not be zero (empty array).
210 */
211 #define max_array(array, len) __minmax_array(max, array, len)
212
213 /**
214 * clamp_t - return a value clamped to a given range using a given type
215 * @type: the type of variable to use
216 * @val: current value
217 * @lo: minimum allowable value
218 * @hi: maximum allowable value
219 *
220 * This macro does no typechecking and uses temporary variables of type
221 * @type to make all the comparisons.
222 */
223 #define clamp_t(type, val, lo, hi) \
224 __careful_clamp((type)(val), (type)(lo), (type)(hi))
225
226 /**
227 * clamp_val - return a value clamped to a given range using val's type
228 * @val: current value
229 * @lo: minimum allowable value
230 * @hi: maximum allowable value
231 *
232 * This macro does no typechecking and uses temporary variables of whatever
233 * type the input argument @val is. This is useful when @val is an unsigned
234 * type and @lo and @hi are literals that will otherwise be assigned a signed
235 * integer type.
236 */
237 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
238
in_range64(u64 val,u64 start,u64 len)239 static inline bool in_range64(u64 val, u64 start, u64 len)
240 {
241 return (val - start) < len;
242 }
243
in_range32(u32 val,u32 start,u32 len)244 static inline bool in_range32(u32 val, u32 start, u32 len)
245 {
246 return (val - start) < len;
247 }
248
249 /**
250 * in_range - Determine if a value lies within a range.
251 * @val: Value to test.
252 * @start: First value in range.
253 * @len: Number of values in range.
254 *
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
257 * the type by a sufficient amount to encompass @val. Decide for yourself
258 * which behaviour you want, or prove that start + len never overflow.
259 * Do not blindly replace one form with the other.
260 */
261 #define in_range(val, start, len) \
262 ((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
263 in_range32(val, start, len) : \
264 in_range64(val, start, len))
265
266 /**
267 * swap - swap values of @a and @b
268 * @a: first value
269 * @b: second value
270 */
271 #define swap(a, b) \
272 do { \
273 typeof(a) __tmp = (a); \
274 (a) = (b); \
275 (b) = __tmp; \
276 } while (0)
277
278 #endif /* _LINUX_MINMAX_H */
279