Lines Matching +full:y +full:- +full:max
1 /* SPDX-License-Identifier: GPL-2.0 */
11 * min()/max()/clamp() macros must accomplish three things:
13 * - Avoid multiple evaluations of the arguments (so side-effects like
14 * "x++" happen only once) when non-constant.
15 * - Retain result as a constant expressions when called with only
18 * - Perform signed v unsigned type-checking (to generate compile
20 * - Unsigned char/short are always promoted to signed int and can be
22 * - Unsigned arguments can be compared against non-negative signed constants.
23 * - Comparison of a signed argument against an unsigned constant fails
26 #define __typecheck(x, y) \ argument
27 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
34 * In particular, statically non-negative signed integer
63 * On 64-bit we can just always use 'long', since any
66 * This does not work for 128-bit signed integers since
81 #define __types_ok(x,y,ux,uy) \ argument
82 (__sign_use(x,ux) & __sign_use(y,uy))
84 #define __types_ok3(x,y,z,ux,uy,uz) \ argument
85 (__sign_use(x,ux) & __sign_use(y,uy) & __sign_use(z,uz))
90 #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y)) argument
92 #define __cmp_once_unique(op, type, x, y, ux, uy) \ argument
93 ({ type ux = (x); type uy = (y); __cmp(op, ux, uy); })
95 #define __cmp_once(op, type, x, y) \ argument
96 __cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))
98 #define __careful_cmp_once(op, x, y, ux, uy) ({ \ argument
99 __auto_type ux = (x); __auto_type uy = (y); \
100 BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy), \
101 #op"("#x", "#y") signedness error"); \
104 #define __careful_cmp(op, x, y) \ argument
105 __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))
125 * min - return minimum of two values of the same or compatible types
127 * @y: second value
129 #define min(x, y) __careful_cmp(min, x, y) argument
132 * max - return maximum of two values of the same or compatible types
134 * @y: second value
136 #define max(x, y) __careful_cmp(max, x, y) macro
139 * umin - return minimum of two non-negative values
142 * @y: second value
144 #define umin(x, y) \ argument
145 __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
148 * umax - return maximum of two non-negative values
150 * @y: second value
152 #define umax(x, y) \ argument
153 __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
155 #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ argument
156 __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\
157 BUILD_BUG_ON_MSG(!__types_ok3(x,y,z,ux,uy,uz), \
158 #op"3("#x", "#y", "#z") signedness error"); \
162 * min3 - return minimum of three values
164 * @y: second value
167 #define min3(x, y, z) \ argument
168 __careful_op3(min, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_))
171 * max3 - return maximum of three values
173 * @y: second value
176 #define max3(x, y, z) \ argument
177 __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_))
180 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
182 * @y: value2
184 #define min_not_zero(x, y) ({ \ argument
186 typeof(y) __y = (y); \
190 * clamp - return a value clamped to a given range with strict typechecking
204 * Or not use min/max/clamp at all, of course.
208 * min_t - return minimum of two values, using the specified type
211 * @y: second value
213 #define min_t(type, x, y) __cmp_once(min, type, x, y) argument
216 * max_t - return maximum of two values, using the specified type
219 * @y: second value
221 #define max_t(type, x, y) __cmp_once(max, type, x, y) argument
225 * In the following legit use-case where the "array" passed is a simple pointer,
227 * --- 8< ---
231 * --- 8< ---
243 __unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
244 while (__len--) \
249 * min_array - return minimum of values present in an array
258 * max_array - return maximum of values present in an array
264 #define max_array(array, len) __minmax_array(max, array, len)
267 * clamp_t - return a value clamped to a given range using a given type
279 * clamp_val - return a value clamped to a given range using val's type
293 return (val - start) < len; in in_range64()
298 return (val - start) < len; in in_range32()
302 * in_range - Determine if a value lies within a range.
318 * swap - swap values of @a and @b
330 #define MAX(a,b) __cmp(max,a,b) macro
332 #define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b))