div64.h 10 KB

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