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