minmax.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MINMAX_H
  3. #define _LINUX_MINMAX_H
  4. #include <linux/const.h>
  5. /*
  6. * min()/max()/clamp() macros must accomplish three things:
  7. *
  8. * - avoid multiple evaluations of the arguments (so side-effects like
  9. * "x++" happen only once) when non-constant.
  10. * - perform strict type-checking (to generate warnings instead of
  11. * nasty runtime surprises). See the "unnecessary" pointer comparison
  12. * in __typecheck().
  13. * - retain result as a constant expressions when called with only
  14. * constant expressions (to avoid tripping VLA warnings in stack
  15. * allocation usage).
  16. */
  17. #define __typecheck(x, y) \
  18. (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
  19. #define __no_side_effects(x, y) \
  20. (__is_constexpr(x) && __is_constexpr(y))
  21. #define __safe_cmp(x, y) \
  22. (__typecheck(x, y) && __no_side_effects(x, y))
  23. #define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
  24. #define __cmp_once(x, y, unique_x, unique_y, op) ({ \
  25. typeof(x) unique_x = (x); \
  26. typeof(y) unique_y = (y); \
  27. __cmp(unique_x, unique_y, op); })
  28. #define __careful_cmp(x, y, op) \
  29. __builtin_choose_expr(__safe_cmp(x, y), \
  30. __cmp(x, y, op), \
  31. __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
  32. /**
  33. * min - return minimum of two values of the same or compatible types
  34. * @x: first value
  35. * @y: second value
  36. */
  37. #define min(x, y) __careful_cmp(x, y, <)
  38. /**
  39. * max - return maximum of two values of the same or compatible types
  40. * @x: first value
  41. * @y: second value
  42. */
  43. #define max(x, y) __careful_cmp(x, y, >)
  44. /**
  45. * min3 - return minimum of three values
  46. * @x: first value
  47. * @y: second value
  48. * @z: third value
  49. */
  50. #define min3(x, y, z) min((typeof(x))min(x, y), z)
  51. /**
  52. * max3 - return maximum of three values
  53. * @x: first value
  54. * @y: second value
  55. * @z: third value
  56. */
  57. #define max3(x, y, z) max((typeof(x))max(x, y), z)
  58. /**
  59. * min_not_zero - return the minimum that is _not_ zero, unless both are zero
  60. * @x: value1
  61. * @y: value2
  62. */
  63. #define min_not_zero(x, y) ({ \
  64. typeof(x) __x = (x); \
  65. typeof(y) __y = (y); \
  66. __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
  67. /**
  68. * clamp - return a value clamped to a given range with strict typechecking
  69. * @val: current value
  70. * @lo: lowest allowable value
  71. * @hi: highest allowable value
  72. *
  73. * This macro does strict typechecking of @lo/@hi to make sure they are of the
  74. * same type as @val. See the unnecessary pointer comparisons.
  75. */
  76. #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  77. /*
  78. * ..and if you can't take the strict
  79. * types, you can specify one yourself.
  80. *
  81. * Or not use min/max/clamp at all, of course.
  82. */
  83. /**
  84. * min_t - return minimum of two values, using the specified type
  85. * @type: data type to use
  86. * @x: first value
  87. * @y: second value
  88. */
  89. #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
  90. /**
  91. * max_t - return maximum of two values, using the specified type
  92. * @type: data type to use
  93. * @x: first value
  94. * @y: second value
  95. */
  96. #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
  97. /**
  98. * clamp_t - return a value clamped to a given range using a given type
  99. * @type: the type of variable to use
  100. * @val: current value
  101. * @lo: minimum allowable value
  102. * @hi: maximum allowable value
  103. *
  104. * This macro does no typechecking and uses temporary variables of type
  105. * @type to make all the comparisons.
  106. */
  107. #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
  108. /**
  109. * clamp_val - return a value clamped to a given range using val's type
  110. * @val: current value
  111. * @lo: minimum allowable value
  112. * @hi: maximum allowable value
  113. *
  114. * This macro does no typechecking and uses temporary variables of whatever
  115. * type the input argument @val is. This is useful when @val is an unsigned
  116. * type and @lo and @hi are literals that will otherwise be assigned a signed
  117. * integer type.
  118. */
  119. #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
  120. /**
  121. * swap - swap values of @a and @b
  122. * @a: first value
  123. * @b: second value
  124. */
  125. #define swap(a, b) \
  126. do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  127. #endif /* _LINUX_MINMAX_H */