div64.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * __div64_32 implementation for 31 bit.
  3. *
  4. * Copyright IBM Corp. 2006
  5. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  6. */
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. #ifdef CONFIG_MARCH_G5
  10. /*
  11. * Function to divide an unsigned 64 bit integer by an unsigned
  12. * 31 bit integer using signed 64/32 bit division.
  13. */
  14. static uint32_t __div64_31(uint64_t *n, uint32_t base)
  15. {
  16. register uint32_t reg2 asm("2");
  17. register uint32_t reg3 asm("3");
  18. uint32_t *words = (uint32_t *) n;
  19. uint32_t tmp;
  20. /* Special case base==1, remainder = 0, quotient = n */
  21. if (base == 1)
  22. return 0;
  23. /*
  24. * Special case base==0 will cause a fixed point divide exception
  25. * on the dr instruction and may not happen anyway. For the
  26. * following calculation we can assume base > 1. The first
  27. * signed 64 / 32 bit division with an upper half of 0 will
  28. * give the correct upper half of the 64 bit quotient.
  29. */
  30. reg2 = 0UL;
  31. reg3 = words[0];
  32. asm volatile(
  33. " dr %0,%2\n"
  34. : "+d" (reg2), "+d" (reg3) : "d" (base) : "cc" );
  35. words[0] = reg3;
  36. reg3 = words[1];
  37. /*
  38. * To get the lower half of the 64 bit quotient and the 32 bit
  39. * remainder we have to use a little trick. Since we only have
  40. * a signed division the quotient can get too big. To avoid this
  41. * the 64 bit dividend is halved, then the signed division will
  42. * work. Afterwards the quotient and the remainder are doubled.
  43. * If the last bit of the dividend has been one the remainder
  44. * is increased by one then checked against the base. If the
  45. * remainder has overflown subtract base and increase the
  46. * quotient. Simple, no ?
  47. */
  48. asm volatile(
  49. " nr %2,%1\n"
  50. " srdl %0,1\n"
  51. " dr %0,%3\n"
  52. " alr %0,%0\n"
  53. " alr %1,%1\n"
  54. " alr %0,%2\n"
  55. " clr %0,%3\n"
  56. " jl 0f\n"
  57. " slr %0,%3\n"
  58. " ahi %1,1\n"
  59. "0:\n"
  60. : "+d" (reg2), "+d" (reg3), "=d" (tmp)
  61. : "d" (base), "2" (1UL) : "cc" );
  62. words[1] = reg3;
  63. return reg2;
  64. }
  65. /*
  66. * Function to divide an unsigned 64 bit integer by an unsigned
  67. * 32 bit integer using the unsigned 64/31 bit division.
  68. */
  69. uint32_t __div64_32(uint64_t *n, uint32_t base)
  70. {
  71. uint32_t r;
  72. /*
  73. * If the most significant bit of base is set, divide n by
  74. * (base/2). That allows to use 64/31 bit division and gives a
  75. * good approximation of the result: n = (base/2)*q + r. The
  76. * result needs to be corrected with two simple transformations.
  77. * If base is already < 2^31-1 __div64_31 can be used directly.
  78. */
  79. r = __div64_31(n, ((signed) base < 0) ? (base/2) : base);
  80. if ((signed) base < 0) {
  81. uint64_t q = *n;
  82. /*
  83. * First transformation:
  84. * n = (base/2)*q + r
  85. * = ((base/2)*2)*(q/2) + ((q&1) ? (base/2) : 0) + r
  86. * Since r < (base/2), r + (base/2) < base.
  87. * With q1 = (q/2) and r1 = r + ((q&1) ? (base/2) : 0)
  88. * n = ((base/2)*2)*q1 + r1 with r1 < base.
  89. */
  90. if (q & 1)
  91. r += base/2;
  92. q >>= 1;
  93. /*
  94. * Second transformation. ((base/2)*2) could have lost the
  95. * last bit.
  96. * n = ((base/2)*2)*q1 + r1
  97. * = base*q1 - ((base&1) ? q1 : 0) + r1
  98. */
  99. if (base & 1) {
  100. int64_t rx = r - q;
  101. /*
  102. * base is >= 2^31. The worst case for the while
  103. * loop is n=2^64-1 base=2^31+1. That gives a
  104. * maximum for q=(2^64-1)/2^31 = 0x1ffffffff. Since
  105. * base >= 2^31 the loop is finished after a maximum
  106. * of three iterations.
  107. */
  108. while (rx < 0) {
  109. rx += base;
  110. q--;
  111. }
  112. r = rx;
  113. }
  114. *n = q;
  115. }
  116. return r;
  117. }
  118. #else /* MARCH_G5 */
  119. uint32_t __div64_32(uint64_t *n, uint32_t base)
  120. {
  121. register uint32_t reg2 asm("2");
  122. register uint32_t reg3 asm("3");
  123. uint32_t *words = (uint32_t *) n;
  124. reg2 = 0UL;
  125. reg3 = words[0];
  126. asm volatile(
  127. " dlr %0,%2\n"
  128. : "+d" (reg2), "+d" (reg3) : "d" (base) : "cc" );
  129. words[0] = reg3;
  130. reg3 = words[1];
  131. asm volatile(
  132. " dlr %0,%2\n"
  133. : "+d" (reg2), "+d" (reg3) : "d" (base) : "cc" );
  134. words[1] = reg3;
  135. return reg2;
  136. }
  137. #endif /* MARCH_G5 */