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