div64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2003 Bernardo Innocenti <[email protected]>
  4. *
  5. * Based on former do_div() implementation from asm-parisc/div64.h:
  6. * Copyright (C) 1999 Hewlett-Packard Co
  7. * Copyright (C) 1999 David Mosberger-Tang <[email protected]>
  8. *
  9. *
  10. * Generic C version of 64bit/32bit division and modulo, with
  11. * 64bit result and 32bit remainder.
  12. *
  13. * The fast case for (n>>32 == 0) is handled inline by do_div().
  14. *
  15. * Code generated for this function might be very inefficient
  16. * for some CPUs. __div64_32() can be overridden by linking arch-specific
  17. * assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S
  18. * or by defining a preprocessor macro in arch/include/asm/div64.h.
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/export.h>
  22. #include <linux/math.h>
  23. #include <linux/math64.h>
  24. #include <linux/log2.h>
  25. /* Not needed on 64bit architectures */
  26. #if BITS_PER_LONG == 32
  27. #ifndef __div64_32
  28. uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
  29. {
  30. uint64_t rem = *n;
  31. uint64_t b = base;
  32. uint64_t res, d = 1;
  33. uint32_t high = rem >> 32;
  34. /* Reduce the thing a bit first */
  35. res = 0;
  36. if (high >= base) {
  37. high /= base;
  38. res = (uint64_t) high << 32;
  39. rem -= (uint64_t) (high*base) << 32;
  40. }
  41. while ((int64_t)b > 0 && b < rem) {
  42. b = b+b;
  43. d = d+d;
  44. }
  45. do {
  46. if (rem >= b) {
  47. rem -= b;
  48. res += d;
  49. }
  50. b >>= 1;
  51. d >>= 1;
  52. } while (d);
  53. *n = res;
  54. return rem;
  55. }
  56. EXPORT_SYMBOL(__div64_32);
  57. #endif
  58. /**
  59. * div_s64_rem - signed 64bit divide with 64bit divisor and remainder
  60. * @dividend: 64bit dividend
  61. * @divisor: 64bit divisor
  62. * @remainder: 64bit remainder
  63. */
  64. #ifndef div_s64_rem
  65. s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
  66. {
  67. u64 quotient;
  68. if (dividend < 0) {
  69. quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
  70. *remainder = -*remainder;
  71. if (divisor > 0)
  72. quotient = -quotient;
  73. } else {
  74. quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
  75. if (divisor < 0)
  76. quotient = -quotient;
  77. }
  78. return quotient;
  79. }
  80. EXPORT_SYMBOL(div_s64_rem);
  81. #endif
  82. /**
  83. * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
  84. * @dividend: 64bit dividend
  85. * @divisor: 64bit divisor
  86. * @remainder: 64bit remainder
  87. *
  88. * This implementation is a comparable to algorithm used by div64_u64.
  89. * But this operation, which includes math for calculating the remainder,
  90. * is kept distinct to avoid slowing down the div64_u64 operation on 32bit
  91. * systems.
  92. */
  93. #ifndef div64_u64_rem
  94. u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
  95. {
  96. u32 high = divisor >> 32;
  97. u64 quot;
  98. if (high == 0) {
  99. u32 rem32;
  100. quot = div_u64_rem(dividend, divisor, &rem32);
  101. *remainder = rem32;
  102. } else {
  103. int n = fls(high);
  104. quot = div_u64(dividend >> n, divisor >> n);
  105. if (quot != 0)
  106. quot--;
  107. *remainder = dividend - quot * divisor;
  108. if (*remainder >= divisor) {
  109. quot++;
  110. *remainder -= divisor;
  111. }
  112. }
  113. return quot;
  114. }
  115. EXPORT_SYMBOL(div64_u64_rem);
  116. #endif
  117. /**
  118. * div64_u64 - unsigned 64bit divide with 64bit divisor
  119. * @dividend: 64bit dividend
  120. * @divisor: 64bit divisor
  121. *
  122. * This implementation is a modified version of the algorithm proposed
  123. * by the book 'Hacker's Delight'. The original source and full proof
  124. * can be found here and is available for use without restriction.
  125. *
  126. * 'http://www.hackersdelight.org/hdcodetxt/divDouble.c.txt'
  127. */
  128. #ifndef div64_u64
  129. u64 div64_u64(u64 dividend, u64 divisor)
  130. {
  131. u32 high = divisor >> 32;
  132. u64 quot;
  133. if (high == 0) {
  134. quot = div_u64(dividend, divisor);
  135. } else {
  136. int n = fls(high);
  137. quot = div_u64(dividend >> n, divisor >> n);
  138. if (quot != 0)
  139. quot--;
  140. if ((dividend - quot * divisor) >= divisor)
  141. quot++;
  142. }
  143. return quot;
  144. }
  145. EXPORT_SYMBOL(div64_u64);
  146. #endif
  147. /**
  148. * div64_s64 - signed 64bit divide with 64bit divisor
  149. * @dividend: 64bit dividend
  150. * @divisor: 64bit divisor
  151. */
  152. #ifndef div64_s64
  153. s64 div64_s64(s64 dividend, s64 divisor)
  154. {
  155. s64 quot, t;
  156. quot = div64_u64(abs(dividend), abs(divisor));
  157. t = (dividend ^ divisor) >> 63;
  158. return (quot ^ t) - t;
  159. }
  160. EXPORT_SYMBOL(div64_s64);
  161. #endif
  162. #endif /* BITS_PER_LONG == 32 */
  163. /*
  164. * Iterative div/mod for use when dividend is not expected to be much
  165. * bigger than divisor.
  166. */
  167. u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
  168. {
  169. return __iter_div_u64_rem(dividend, divisor, remainder);
  170. }
  171. EXPORT_SYMBOL(iter_div_u64_rem);
  172. #ifndef mul_u64_u64_div_u64
  173. u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
  174. {
  175. u64 res = 0, div, rem;
  176. int shift;
  177. /* can a * b overflow ? */
  178. if (ilog2(a) + ilog2(b) > 62) {
  179. /*
  180. * (b * a) / c is equal to
  181. *
  182. * (b / c) * a +
  183. * (b % c) * a / c
  184. *
  185. * if nothing overflows. Can the 1st multiplication
  186. * overflow? Yes, but we do not care: this can only
  187. * happen if the end result can't fit in u64 anyway.
  188. *
  189. * So the code below does
  190. *
  191. * res = (b / c) * a;
  192. * b = b % c;
  193. */
  194. div = div64_u64_rem(b, c, &rem);
  195. res = div * a;
  196. b = rem;
  197. shift = ilog2(a) + ilog2(b) - 62;
  198. if (shift > 0) {
  199. /* drop precision */
  200. b >>= shift;
  201. c >>= shift;
  202. if (!c)
  203. return res;
  204. }
  205. }
  206. return res + div64_u64(a * b, c);
  207. }
  208. EXPORT_SYMBOL(mul_u64_u64_div_u64);
  209. #endif