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