div64.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
  3. *
  4. * Based on former do_div() implementation from asm-parisc/div64.h:
  5. * Copyright (C) 1999 Hewlett-Packard Co
  6. * Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
  7. *
  8. *
  9. * Generic C version of 64bit/32bit division and modulo, with
  10. * 64bit result and 32bit remainder.
  11. *
  12. * The fast case for (n>>32 == 0) is handled inline by do_div().
  13. *
  14. * Code generated for this function might be very inefficient
  15. * for some CPUs. __div64_32() can be overridden by linking arch-specific
  16. * assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S.
  17. */
  18. #include <linux/export.h>
  19. #include <linux/kernel.h>
  20. #include <linux/math64.h>
  21. /* Not needed on 64bit architectures */
  22. #if BITS_PER_LONG == 32
  23. uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
  24. {
  25. uint64_t rem = *n;
  26. uint64_t b = base;
  27. uint64_t res, d = 1;
  28. uint32_t high = rem >> 32;
  29. /* Reduce the thing a bit first */
  30. res = 0;
  31. if (high >= base) {
  32. high /= base;
  33. res = (uint64_t) high << 32;
  34. rem -= (uint64_t) (high*base) << 32;
  35. }
  36. while ((int64_t)b > 0 && b < rem) {
  37. b = b+b;
  38. d = d+d;
  39. }
  40. do {
  41. if (rem >= b) {
  42. rem -= b;
  43. res += d;
  44. }
  45. b >>= 1;
  46. d >>= 1;
  47. } while (d);
  48. *n = res;
  49. return rem;
  50. }
  51. EXPORT_SYMBOL(__div64_32);
  52. #ifndef div_s64_rem
  53. s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
  54. {
  55. u64 quotient;
  56. if (dividend < 0) {
  57. quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
  58. *remainder = -*remainder;
  59. if (divisor > 0)
  60. quotient = -quotient;
  61. } else {
  62. quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
  63. if (divisor < 0)
  64. quotient = -quotient;
  65. }
  66. return quotient;
  67. }
  68. EXPORT_SYMBOL(div_s64_rem);
  69. #endif
  70. /**
  71. * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
  72. * @dividend: 64bit dividend
  73. * @divisor: 64bit divisor
  74. * @remainder: 64bit remainder
  75. *
  76. * This implementation is a comparable to algorithm used by div64_u64.
  77. * But this operation, which includes math for calculating the remainder,
  78. * is kept distinct to avoid slowing down the div64_u64 operation on 32bit
  79. * systems.
  80. */
  81. #ifndef div64_u64_rem
  82. u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
  83. {
  84. u32 high = divisor >> 32;
  85. u64 quot;
  86. if (high == 0) {
  87. u32 rem32;
  88. quot = div_u64_rem(dividend, divisor, &rem32);
  89. *remainder = rem32;
  90. } else {
  91. int n = 1 + fls(high);
  92. quot = div_u64(dividend >> n, divisor >> n);
  93. if (quot != 0)
  94. quot--;
  95. *remainder = dividend - quot * divisor;
  96. if (*remainder >= divisor) {
  97. quot++;
  98. *remainder -= divisor;
  99. }
  100. }
  101. return quot;
  102. }
  103. EXPORT_SYMBOL(div64_u64_rem);
  104. #endif
  105. /**
  106. * div64_u64 - unsigned 64bit divide with 64bit divisor
  107. * @dividend: 64bit dividend
  108. * @divisor: 64bit divisor
  109. *
  110. * This implementation is a modified version of the algorithm proposed
  111. * by the book 'Hacker's Delight'. The original source and full proof
  112. * can be found here and is available for use without restriction.
  113. *
  114. * 'http://www.hackersdelight.org/hdcodetxt/divDouble.c.txt'
  115. */
  116. #ifndef div64_u64
  117. u64 div64_u64(u64 dividend, u64 divisor)
  118. {
  119. u32 high = divisor >> 32;
  120. u64 quot;
  121. if (high == 0) {
  122. quot = div_u64(dividend, divisor);
  123. } else {
  124. int n = 1 + fls(high);
  125. quot = div_u64(dividend >> n, divisor >> n);
  126. if (quot != 0)
  127. quot--;
  128. if ((dividend - quot * divisor) >= divisor)
  129. quot++;
  130. }
  131. return quot;
  132. }
  133. EXPORT_SYMBOL(div64_u64);
  134. #endif
  135. /**
  136. * div64_s64 - signed 64bit divide with 64bit divisor
  137. * @dividend: 64bit dividend
  138. * @divisor: 64bit divisor
  139. */
  140. #ifndef div64_s64
  141. s64 div64_s64(s64 dividend, s64 divisor)
  142. {
  143. s64 quot, t;
  144. quot = div64_u64(abs64(dividend), abs64(divisor));
  145. t = (dividend ^ divisor) >> 63;
  146. return (quot ^ t) - t;
  147. }
  148. EXPORT_SYMBOL(div64_s64);
  149. #endif
  150. #endif /* BITS_PER_LONG == 32 */
  151. /*
  152. * Iterative div/mod for use when dividend is not expected to be much
  153. * bigger than divisor.
  154. */
  155. u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
  156. {
  157. return __iter_div_u64_rem(dividend, divisor, remainder);
  158. }
  159. EXPORT_SYMBOL(iter_div_u64_rem);