math.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MATH_H
  3. #define _LINUX_MATH_H
  4. #include <linux/types.h>
  5. #include <asm/div64.h>
  6. #include <uapi/linux/kernel.h>
  7. /*
  8. * This looks more complex than it should be. But we need to
  9. * get the type for the ~ right in round_down (it needs to be
  10. * as wide as the result!), and we want to evaluate the macro
  11. * arguments just once each.
  12. */
  13. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  14. /**
  15. * round_up - round up to next specified power of 2
  16. * @x: the value to round
  17. * @y: multiple to round up to (must be a power of 2)
  18. *
  19. * Rounds @x up to next multiple of @y (which must be a power of 2).
  20. * To perform arbitrary rounding up, use roundup() below.
  21. */
  22. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  23. /**
  24. * round_down - round down to next specified power of 2
  25. * @x: the value to round
  26. * @y: multiple to round down to (must be a power of 2)
  27. *
  28. * Rounds @x down to next multiple of @y (which must be a power of 2).
  29. * To perform arbitrary rounding down, use rounddown() below.
  30. */
  31. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  32. #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
  33. #define DIV_ROUND_DOWN_ULL(ll, d) \
  34. ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
  35. #define DIV_ROUND_UP_ULL(ll, d) \
  36. DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
  37. #if BITS_PER_LONG == 32
  38. # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
  39. #else
  40. # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
  41. #endif
  42. /**
  43. * roundup - round up to the next specified multiple
  44. * @x: the value to up
  45. * @y: multiple to round up to
  46. *
  47. * Rounds @x up to next multiple of @y. If @y will always be a power
  48. * of 2, consider using the faster round_up().
  49. */
  50. #define roundup(x, y) ( \
  51. { \
  52. typeof(y) __y = y; \
  53. (((x) + (__y - 1)) / __y) * __y; \
  54. } \
  55. )
  56. /**
  57. * rounddown - round down to next specified multiple
  58. * @x: the value to round
  59. * @y: multiple to round down to
  60. *
  61. * Rounds @x down to next multiple of @y. If @y will always be a power
  62. * of 2, consider using the faster round_down().
  63. */
  64. #define rounddown(x, y) ( \
  65. { \
  66. typeof(x) __x = (x); \
  67. __x - (__x % (y)); \
  68. } \
  69. )
  70. /*
  71. * Divide positive or negative dividend by positive or negative divisor
  72. * and round to closest integer. Result is undefined for negative
  73. * divisors if the dividend variable type is unsigned and for negative
  74. * dividends if the divisor variable type is unsigned.
  75. */
  76. #define DIV_ROUND_CLOSEST(x, divisor)( \
  77. { \
  78. typeof(x) __x = x; \
  79. typeof(divisor) __d = divisor; \
  80. (((typeof(x))-1) > 0 || \
  81. ((typeof(divisor))-1) > 0 || \
  82. (((__x) > 0) == ((__d) > 0))) ? \
  83. (((__x) + ((__d) / 2)) / (__d)) : \
  84. (((__x) - ((__d) / 2)) / (__d)); \
  85. } \
  86. )
  87. /*
  88. * Same as above but for u64 dividends. divisor must be a 32-bit
  89. * number.
  90. */
  91. #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
  92. { \
  93. typeof(divisor) __d = divisor; \
  94. unsigned long long _tmp = (x) + (__d) / 2; \
  95. do_div(_tmp, __d); \
  96. _tmp; \
  97. } \
  98. )
  99. #define __STRUCT_FRACT(type) \
  100. struct type##_fract { \
  101. __##type numerator; \
  102. __##type denominator; \
  103. };
  104. __STRUCT_FRACT(s16)
  105. __STRUCT_FRACT(u16)
  106. __STRUCT_FRACT(s32)
  107. __STRUCT_FRACT(u32)
  108. #undef __STRUCT_FRACT
  109. /*
  110. * Multiplies an integer by a fraction, while avoiding unnecessary
  111. * overflow or loss of precision.
  112. */
  113. #define mult_frac(x, numer, denom)( \
  114. { \
  115. typeof(x) quot = (x) / (denom); \
  116. typeof(x) rem = (x) % (denom); \
  117. (quot * (numer)) + ((rem * (numer)) / (denom)); \
  118. } \
  119. )
  120. #define sector_div(a, b) do_div(a, b)
  121. /**
  122. * abs - return absolute value of an argument
  123. * @x: the value. If it is unsigned type, it is converted to signed type first.
  124. * char is treated as if it was signed (regardless of whether it really is)
  125. * but the macro's return type is preserved as char.
  126. *
  127. * Return: an absolute value of x.
  128. */
  129. #define abs(x) __abs_choose_expr(x, long long, \
  130. __abs_choose_expr(x, long, \
  131. __abs_choose_expr(x, int, \
  132. __abs_choose_expr(x, short, \
  133. __abs_choose_expr(x, char, \
  134. __builtin_choose_expr( \
  135. __builtin_types_compatible_p(typeof(x), char), \
  136. (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
  137. ((void)0)))))))
  138. #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
  139. __builtin_types_compatible_p(typeof(x), signed type) || \
  140. __builtin_types_compatible_p(typeof(x), unsigned type), \
  141. ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
  142. /**
  143. * reciprocal_scale - "scale" a value into range [0, ep_ro)
  144. * @val: value
  145. * @ep_ro: right open interval endpoint
  146. *
  147. * Perform a "reciprocal multiplication" in order to "scale" a value into
  148. * range [0, @ep_ro), where the upper interval endpoint is right-open.
  149. * This is useful, e.g. for accessing a index of an array containing
  150. * @ep_ro elements, for example. Think of it as sort of modulus, only that
  151. * the result isn't that of modulo. ;) Note that if initial input is a
  152. * small value, then result will return 0.
  153. *
  154. * Return: a result based on @val in interval [0, @ep_ro).
  155. */
  156. static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
  157. {
  158. return (u32)(((u64) val * ep_ro) >> 32);
  159. }
  160. u64 int_pow(u64 base, unsigned int exp);
  161. unsigned long int_sqrt(unsigned long);
  162. #if BITS_PER_LONG < 64
  163. u32 int_sqrt64(u64 x);
  164. #else
  165. static inline u32 int_sqrt64(u64 x)
  166. {
  167. return (u32)int_sqrt(x);
  168. }
  169. #endif
  170. #endif /* _LINUX_MATH_H */