div64.c 3.8 KB

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