div64.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /* 64bit divisor, dividend and result. dynamic precision */
  70. #ifndef div64_u64
  71. u64 div64_u64(u64 dividend, u64 divisor)
  72. {
  73. u32 high, d;
  74. high = divisor >> 32;
  75. if (high) {
  76. unsigned int shift = fls(high);
  77. d = divisor >> shift;
  78. dividend >>= shift;
  79. } else
  80. d = divisor;
  81. return div_u64(dividend, d);
  82. }
  83. EXPORT_SYMBOL(div64_u64);
  84. #endif
  85. #endif /* BITS_PER_LONG == 32 */
  86. /*
  87. * Iterative div/mod for use when dividend is not expected to be much
  88. * bigger than divisor.
  89. */
  90. u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
  91. {
  92. return __iter_div_u64_rem(dividend, divisor, remainder);
  93. }
  94. EXPORT_SYMBOL(iter_div_u64_rem);