div64.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 - unsigned 64bit divide with 64bit divisor
  72. * @dividend: 64bit dividend
  73. * @divisor: 64bit divisor
  74. *
  75. * This implementation is a modified version of the algorithm proposed
  76. * by the book 'Hacker's Delight'. The original source and full proof
  77. * can be found here and is available for use without restriction.
  78. *
  79. * 'http://www.hackersdelight.org/HDcode/newCode/divDouble.c'
  80. */
  81. #ifndef div64_u64
  82. u64 div64_u64(u64 dividend, u64 divisor)
  83. {
  84. u32 high = divisor >> 32;
  85. u64 quot;
  86. if (high == 0) {
  87. quot = div_u64(dividend, divisor);
  88. } else {
  89. int n = 1 + fls(high);
  90. quot = div_u64(dividend >> n, divisor >> n);
  91. if (quot != 0)
  92. quot--;
  93. if ((dividend - quot * divisor) >= divisor)
  94. quot++;
  95. }
  96. return quot;
  97. }
  98. EXPORT_SYMBOL(div64_u64);
  99. #endif
  100. /**
  101. * div64_s64 - signed 64bit divide with 64bit divisor
  102. * @dividend: 64bit dividend
  103. * @divisor: 64bit divisor
  104. */
  105. #ifndef div64_s64
  106. s64 div64_s64(s64 dividend, s64 divisor)
  107. {
  108. s64 quot, t;
  109. quot = div64_u64(abs64(dividend), abs64(divisor));
  110. t = (dividend ^ divisor) >> 63;
  111. return (quot ^ t) - t;
  112. }
  113. EXPORT_SYMBOL(div64_s64);
  114. #endif
  115. #endif /* BITS_PER_LONG == 32 */
  116. /*
  117. * Iterative div/mod for use when dividend is not expected to be much
  118. * bigger than divisor.
  119. */
  120. u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
  121. {
  122. return __iter_div_u64_rem(dividend, divisor, remainder);
  123. }
  124. EXPORT_SYMBOL(iter_div_u64_rem);