div64.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/module.h>
  19. #include <linux/math64.h>
  20. /* Not needed on 64bit architectures */
  21. #if BITS_PER_LONG == 32
  22. uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
  23. {
  24. uint64_t rem = *n;
  25. uint64_t b = base;
  26. uint64_t res, d = 1;
  27. uint32_t high = rem >> 32;
  28. /* Reduce the thing a bit first */
  29. res = 0;
  30. if (high >= base) {
  31. high /= base;
  32. res = (uint64_t) high << 32;
  33. rem -= (uint64_t) (high*base) << 32;
  34. }
  35. while ((int64_t)b > 0 && b < rem) {
  36. b = b+b;
  37. d = d+d;
  38. }
  39. do {
  40. if (rem >= b) {
  41. rem -= b;
  42. res += d;
  43. }
  44. b >>= 1;
  45. d >>= 1;
  46. } while (d);
  47. *n = res;
  48. return rem;
  49. }
  50. EXPORT_SYMBOL(__div64_32);
  51. #ifndef div_s64_rem
  52. s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
  53. {
  54. u64 quotient;
  55. if (dividend < 0) {
  56. quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
  57. *remainder = -*remainder;
  58. if (divisor > 0)
  59. quotient = -quotient;
  60. } else {
  61. quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
  62. if (divisor < 0)
  63. quotient = -quotient;
  64. }
  65. return quotient;
  66. }
  67. EXPORT_SYMBOL(div_s64_rem);
  68. #endif
  69. /**
  70. * div64_u64 - unsigned 64bit divide with 64bit divisor
  71. * @dividend: 64bit dividend
  72. * @divisor: 64bit divisor
  73. *
  74. * This implementation is a modified version of the algorithm proposed
  75. * by the book 'Hacker's Delight'. The original source and full proof
  76. * can be found here and is available for use without restriction.
  77. *
  78. * 'http://www.hackersdelight.org/HDcode/newCode/divDouble.c'
  79. */
  80. #ifndef div64_u64
  81. u64 div64_u64(u64 dividend, u64 divisor)
  82. {
  83. u32 high = divisor >> 32;
  84. u64 quot;
  85. if (high == 0) {
  86. quot = div_u64(dividend, divisor);
  87. } else {
  88. int n = 1 + fls(high);
  89. quot = div_u64(dividend >> n, divisor >> n);
  90. if (quot != 0)
  91. quot--;
  92. if ((dividend - quot * divisor) >= divisor)
  93. quot++;
  94. }
  95. return quot;
  96. }
  97. EXPORT_SYMBOL(div64_u64);
  98. #endif
  99. /**
  100. * div64_s64 - signed 64bit divide with 64bit divisor
  101. * @dividend: 64bit dividend
  102. * @divisor: 64bit divisor
  103. */
  104. #ifndef div64_s64
  105. s64 div64_s64(s64 dividend, s64 divisor)
  106. {
  107. s64 quot, t;
  108. quot = div64_u64(abs64(dividend), abs64(divisor));
  109. t = (dividend ^ divisor) >> 63;
  110. return (quot ^ t) - t;
  111. }
  112. EXPORT_SYMBOL(div64_s64);
  113. #endif
  114. #endif /* BITS_PER_LONG == 32 */
  115. /*
  116. * Iterative div/mod for use when dividend is not expected to be much
  117. * bigger than divisor.
  118. */
  119. u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
  120. {
  121. return __iter_div_u64_rem(dividend, divisor, remainder);
  122. }
  123. EXPORT_SYMBOL(iter_div_u64_rem);