div64.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <bernie@develer.com>
  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; \
  71. bool ___bias = false; \
  72. \
  73. /* determine MSB of b */ \
  74. ___p = 1 << ilog2(___b); \
  75. \
  76. /* compute m = ((p << 64) + b - 1) / b */ \
  77. ___m = (~0ULL / ___b) * ___p; \
  78. ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
  79. \
  80. /* one less than the dividend with highest result */ \
  81. ___x = ~0ULL / ___b * ___b - 1; \
  82. \
  83. /* test our ___m with res = m * x / (p << 64) */ \
  84. ___res = (___m & 0xffffffff) * (___x & 0xffffffff); \
  85. ___t = (___m & 0xffffffff) * (___x >> 32) + (___res >> 32); \
  86. ___res = (___m >> 32) * (___x >> 32) + (___t >> 32); \
  87. ___t = (___m >> 32) * (___x & 0xffffffff) + (___t & 0xffffffff);\
  88. ___res = (___res + (___t >> 32)) / ___p; \
  89. \
  90. /* Now validate what we've got. */ \
  91. if (___res != ___x / ___b) { \
  92. /* \
  93. * We can't get away without a bias to compensate \
  94. * for bit truncation errors. To avoid it we'd need an \
  95. * additional bit to represent m which would overflow \
  96. * a 64-bit variable. \
  97. * \
  98. * Instead we do m = p / b and n / b = (n * m + m) / p. \
  99. */ \
  100. ___bias = true; \
  101. /* Compute m = (p << 64) / b */ \
  102. ___m = (~0ULL / ___b) * ___p; \
  103. ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
  104. } \
  105. \
  106. /* Reduce m / p to help avoid overflow handling later. */ \
  107. ___p /= (___m & -___m); \
  108. ___m /= (___m & -___m); \
  109. \
  110. /* \
  111. * Perform (m_bias + m * n) / (1 << 64). \
  112. * From now on there will be actual runtime code generated. \
  113. */ \
  114. ___res = __arch_xprod_64(___m, ___n, ___bias); \
  115. \
  116. ___res /= ___p; \
  117. })
  118. #ifndef __arch_xprod_64
  119. /*
  120. * Default C implementation for __arch_xprod_64()
  121. *
  122. * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
  123. * Semantic: retval = ((bias ? m : 0) + m * n) >> 64
  124. *
  125. * The product is a 128-bit value, scaled down to 64 bits.
  126. * Hoping for compile-time optimization of conditional code.
  127. * Architectures may provide their own optimized assembly implementation.
  128. */
  129. #ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
  130. static __always_inline
  131. #else
  132. static inline
  133. #endif
  134. uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
  135. {
  136. uint32_t m_lo = m;
  137. uint32_t m_hi = m >> 32;
  138. uint32_t n_lo = n;
  139. uint32_t n_hi = n >> 32;
  140. uint64_t x, y;
  141. /* Determine if overflow handling can be dispensed with. */
  142. bool no_ovf = __builtin_constant_p(m) &&
  143. ((m >> 32) + (m & 0xffffffff) < 0x100000000);
  144. if (no_ovf) {
  145. x = (uint64_t)m_lo * n_lo + (bias ? m : 0);
  146. x >>= 32;
  147. x += (uint64_t)m_lo * n_hi;
  148. x += (uint64_t)m_hi * n_lo;
  149. x >>= 32;
  150. x += (uint64_t)m_hi * n_hi;
  151. } else {
  152. x = (uint64_t)m_lo * n_lo + (bias ? m_lo : 0);
  153. y = (uint64_t)m_lo * n_hi + (uint32_t)(x >> 32) + (bias ? m_hi : 0);
  154. x = (uint64_t)m_hi * n_hi + (uint32_t)(y >> 32);
  155. y = (uint64_t)m_hi * n_lo + (uint32_t)y;
  156. x += (uint32_t)(y >> 32);
  157. }
  158. return x;
  159. }
  160. #endif
  161. #ifndef __div64_32
  162. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  163. #endif
  164. /* The unnecessary pointer compare is there
  165. * to check for type safety (n must be 64bit)
  166. */
  167. # define do_div(n,base) ({ \
  168. uint32_t __base = (base); \
  169. uint32_t __rem; \
  170. (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
  171. if (__builtin_constant_p(__base) && \
  172. is_power_of_2(__base)) { \
  173. __rem = (n) & (__base - 1); \
  174. (n) >>= ilog2(__base); \
  175. } else if (__builtin_constant_p(__base) && \
  176. __base != 0) { \
  177. uint32_t __res_lo, __n_lo = (n); \
  178. (n) = __div64_const32(n, __base); \
  179. /* the remainder can be computed with 32-bit regs */ \
  180. __res_lo = (n); \
  181. __rem = __n_lo - __res_lo * __base; \
  182. } else if (likely(((n) >> 32) == 0)) { \
  183. __rem = (uint32_t)(n) % __base; \
  184. (n) = (uint32_t)(n) / __base; \
  185. } else { \
  186. __rem = __div64_32(&(n), __base); \
  187. } \
  188. __rem; \
  189. })
  190. #else /* BITS_PER_LONG == ?? */
  191. # error do_div() does not yet support the C64
  192. #endif /* BITS_PER_LONG */
  193. #endif /* _ASM_GENERIC_DIV64_H */