minmax.h 9.0 KB

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