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