div64.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_DIV64_H
  3. #define _ASM_GENERIC_DIV64_H
  4. /*
  5. * Copyright (C) 2003 Bernardo Innocenti <[email protected]>
  6. * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
  7. *
  8. * Optimization for constant divisors on 32-bit machines:
  9. * Copyright (C) 2006-2015 Nicolas Pitre
  10. *
  11. * The semantics of do_div() is, in C++ notation, observing that the name
  12. * is a function-like macro and the n parameter has the semantics of a C++
  13. * reference:
  14. *
  15. * uint32_t do_div(uint64_t &n, uint32_t base)
  16. * {
  17. * uint32_t remainder = n % base;
  18. * n = n / base;
  19. * return remainder;
  20. * }
  21. *
  22. * NOTE: macro parameter n is evaluated multiple times,
  23. * beware of side effects!
  24. */
  25. #include <linux/types.h>
  26. #include <linux/compiler.h>
  27. #if BITS_PER_LONG == 64
  28. /**
  29. * do_div - returns 2 values: calculate remainder and update new dividend
  30. * @n: uint64_t dividend (will be updated)
  31. * @base: uint32_t divisor
  32. *
  33. * Summary:
  34. * ``uint32_t remainder = n % base;``
  35. * ``n = n / base;``
  36. *
  37. * Return: (uint32_t)remainder
  38. *
  39. * NOTE: macro parameter @n is evaluated multiple times,
  40. * beware of side effects!
  41. */
  42. # define do_div(n,base) ({ \
  43. uint32_t __base = (base); \
  44. uint32_t __rem; \
  45. __rem = ((uint64_t)(n)) % __base; \
  46. (n) = ((uint64_t)(n)) / __base; \
  47. __rem; \
  48. })
  49. #elif BITS_PER_LONG == 32
  50. #include <linux/log2.h>
  51. /*
  52. * If the divisor happens to be constant, we determine the appropriate
  53. * inverse at compile time to turn the division into a few inline
  54. * multiplications which ought to be much faster.
  55. *
  56. * (It is unfortunate that gcc doesn't perform all this internally.)
  57. */
  58. #define __div64_const32(n, ___b) \
  59. ({ \
  60. /* \
  61. * Multiplication by reciprocal of b: n / b = n * (p / b) / p \
  62. * \
  63. * We rely on the fact that most of this code gets optimized \
  64. * away at compile time due to constant propagation and only \
  65. * a few multiplication instructions should remain. \
  66. * Hence this monstrous macro (static inline doesn't always \
  67. * do the trick here). \
  68. */ \
  69. uint64_t ___res, ___x, ___t, ___m, ___n = (n); \
  70. uint32_t ___p, ___bias; \
  71. \
  72. /* determine MSB of b */ \
  73. ___p = 1 << ilog2(___b); \
  74. \
  75. /* compute m = ((p << 64) + b - 1) / b */ \
  76. ___m = (~0ULL / ___b) * ___p; \
  77. ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
  78. \
  79. /* one less than the dividend with highest result */ \
  80. ___x = ~0ULL / ___b * ___b - 1; \
  81. \
  82. /* test our ___m with res = m * x / (p << 64) */ \
  83. ___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32; \
  84. ___t = ___res += (___m & 0xffffffff) * (___x >> 32); \
  85. ___res += (___x & 0xffffffff) * (___m >> 32); \
  86. ___t = (___res < ___t) ? (1ULL << 32) : 0; \
  87. ___res = (___res >> 32) + ___t; \
  88. ___res += (___m >> 32) * (___x >> 32); \
  89. ___res /= ___p; \
  90. \
  91. /* Now sanitize and optimize what we've got. */ \
  92. if (~0ULL % (___b / (___b & -___b)) == 0) { \
  93. /* special case, can be simplified to ... */ \
  94. ___n /= (___b & -___b); \
  95. ___m = ~0ULL / (___b / (___b & -___b)); \
  96. ___p = 1; \
  97. ___bias = 1; \
  98. } else if (___res != ___x / ___b) { \
  99. /* \
  100. * We can't get away without a bias to compensate \
  101. * for bit truncation errors. To avoid it we'd need an \
  102. * additional bit to represent m which would overflow \
  103. * a 64-bit variable. \
  104. * \
  105. * Instead we do m = p / b and n / b = (n * m + m) / p. \
  106. */ \
  107. ___bias = 1; \
  108. /* Compute m = (p << 64) / b */ \
  109. ___m = (~0ULL / ___b) * ___p; \
  110. ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
  111. } else { \
  112. /* \
  113. * Reduce m / p, and try to clear bit 31 of m when \
  114. * possible, otherwise that'll need extra overflow \
  115. * handling later. \
  116. */ \
  117. uint32_t ___bits = -(___m & -___m); \
  118. ___bits |= ___m >> 32; \
  119. ___bits = (~___bits) << 1; \
  120. /* \
  121. * If ___bits == 0 then setting bit 31 is unavoidable. \
  122. * Simply apply the maximum possible reduction in that \
  123. * case. Otherwise the MSB of ___bits indicates the \
  124. * best reduction we should apply. \
  125. */ \
  126. if (!___bits) { \
  127. ___p /= (___m & -___m); \
  128. ___m /= (___m & -___m); \
  129. } else { \
  130. ___p >>= ilog2(___bits); \
  131. ___m >>= ilog2(___bits); \
  132. } \
  133. /* No bias needed. */ \
  134. ___bias = 0; \
  135. } \
  136. \
  137. /* \
  138. * Now we have a combination of 2 conditions: \
  139. * \
  140. * 1) whether or not we need to apply a bias, and \
  141. * \
  142. * 2) whether or not there might be an overflow in the cross \
  143. * product determined by (___m & ((1 << 63) | (1 << 31))). \
  144. * \
  145. * Select the best way to do (m_bias + m * n) / (1 << 64). \
  146. * From now on there will be actual runtime code generated. \
  147. */ \
  148. ___res = __arch_xprod_64(___m, ___n, ___bias); \
  149. \
  150. ___res /= ___p; \
  151. })
  152. #ifndef __arch_xprod_64
  153. /*
  154. * Default C implementation for __arch_xprod_64()
  155. *
  156. * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
  157. * Semantic: retval = ((bias ? m : 0) + m * n) >> 64
  158. *
  159. * The product is a 128-bit value, scaled down to 64 bits.
  160. * Assuming constant propagation to optimize away unused conditional code.
  161. * Architectures may provide their own optimized assembly implementation.
  162. */
  163. static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
  164. {
  165. uint32_t m_lo = m;
  166. uint32_t m_hi = m >> 32;
  167. uint32_t n_lo = n;
  168. uint32_t n_hi = n >> 32;
  169. uint64_t res;
  170. uint32_t res_lo, res_hi, tmp;
  171. if (!bias) {
  172. res = ((uint64_t)m_lo * n_lo) >> 32;
  173. } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
  174. /* there can't be any overflow here */
  175. res = (m + (uint64_t)m_lo * n_lo) >> 32;
  176. } else {
  177. res = m + (uint64_t)m_lo * n_lo;
  178. res_lo = res >> 32;
  179. res_hi = (res_lo < m_hi);
  180. res = res_lo | ((uint64_t)res_hi << 32);
  181. }
  182. if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
  183. /* there can't be any overflow here */
  184. res += (uint64_t)m_lo * n_hi;
  185. res += (uint64_t)m_hi * n_lo;
  186. res >>= 32;
  187. } else {
  188. res += (uint64_t)m_lo * n_hi;
  189. tmp = res >> 32;
  190. res += (uint64_t)m_hi * n_lo;
  191. res_lo = res >> 32;
  192. res_hi = (res_lo < tmp);
  193. res = res_lo | ((uint64_t)res_hi << 32);
  194. }
  195. res += (uint64_t)m_hi * n_hi;
  196. return res;
  197. }
  198. #endif
  199. #ifndef __div64_32
  200. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  201. #endif
  202. /* The unnecessary pointer compare is there
  203. * to check for type safety (n must be 64bit)
  204. */
  205. # define do_div(n,base) ({ \
  206. uint32_t __base = (base); \
  207. uint32_t __rem; \
  208. (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
  209. if (__builtin_constant_p(__base) && \
  210. is_power_of_2(__base)) { \
  211. __rem = (n) & (__base - 1); \
  212. (n) >>= ilog2(__base); \
  213. } else if (__builtin_constant_p(__base) && \
  214. __base != 0) { \
  215. uint32_t __res_lo, __n_lo = (n); \
  216. (n) = __div64_const32(n, __base); \
  217. /* the remainder can be computed with 32-bit regs */ \
  218. __res_lo = (n); \
  219. __rem = __n_lo - __res_lo * __base; \
  220. } else if (likely(((n) >> 32) == 0)) { \
  221. __rem = (uint32_t)(n) % __base; \
  222. (n) = (uint32_t)(n) / __base; \
  223. } else { \
  224. __rem = __div64_32(&(n), __base); \
  225. } \
  226. __rem; \
  227. })
  228. #else /* BITS_PER_LONG == ?? */
  229. # error do_div() does not yet support the C64
  230. #endif /* BITS_PER_LONG */
  231. #endif /* _ASM_GENERIC_DIV64_H */