divmod_1.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* mpn_divmod_1(quot_ptr, dividend_ptr, dividend_size, divisor_limb) --
  2. Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
  3. Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
  4. Return the single-limb remainder.
  5. There are no constraints on the value of the divisor.
  6. QUOT_PTR and DIVIDEND_PTR might point to the same limb.
  7. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  8. This file is part of the GNU MP Library.
  9. The GNU MP Library is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU Lesser General Public License as published by
  11. the Free Software Foundation; either version 2.1 of the License, or (at your
  12. option) any later version.
  13. The GNU MP Library is distributed in the hope that it will be useful, but
  14. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  16. License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with the GNU MP Library; see the file COPYING.LIB. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include <gmp.h>
  21. #include "gmp-impl.h"
  22. #include "longlong.h"
  23. #ifndef UMUL_TIME
  24. #define UMUL_TIME 1
  25. #endif
  26. #ifndef UDIV_TIME
  27. #define UDIV_TIME UMUL_TIME
  28. #endif
  29. /* FIXME: We should be using invert_limb (or invert_normalized_limb)
  30. here (not udiv_qrnnd). */
  31. mp_limb_t
  32. mpn_divmod_1 (mp_ptr quot_ptr,
  33. mp_srcptr dividend_ptr, mp_size_t dividend_size,
  34. mp_limb_t divisor_limb)
  35. {
  36. mp_size_t i;
  37. mp_limb_t n1, n0, r;
  38. mp_limb_t dummy __attribute__ ((unused));
  39. /* ??? Should this be handled at all? Rely on callers? */
  40. if (dividend_size == 0)
  41. return 0;
  42. /* If multiplication is much faster than division, and the
  43. dividend is large, pre-invert the divisor, and use
  44. only multiplications in the inner loop. */
  45. /* This test should be read:
  46. Does it ever help to use udiv_qrnnd_preinv?
  47. && Does what we save compensate for the inversion overhead? */
  48. if (UDIV_TIME > (2 * UMUL_TIME + 6)
  49. && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME)
  50. {
  51. int normalization_steps;
  52. count_leading_zeros (normalization_steps, divisor_limb);
  53. if (normalization_steps != 0)
  54. {
  55. mp_limb_t divisor_limb_inverted;
  56. divisor_limb <<= normalization_steps;
  57. /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
  58. result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  59. most significant bit (with weight 2**N) implicit. */
  60. /* Special case for DIVISOR_LIMB == 100...000. */
  61. if (divisor_limb << 1 == 0)
  62. divisor_limb_inverted = ~(mp_limb_t) 0;
  63. else
  64. udiv_qrnnd (divisor_limb_inverted, dummy,
  65. -divisor_limb, 0, divisor_limb);
  66. n1 = dividend_ptr[dividend_size - 1];
  67. r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
  68. /* Possible optimization:
  69. if (r == 0
  70. && divisor_limb > ((n1 << normalization_steps)
  71. | (dividend_ptr[dividend_size - 2] >> ...)))
  72. ...one division less... */
  73. for (i = dividend_size - 2; i >= 0; i--)
  74. {
  75. n0 = dividend_ptr[i];
  76. udiv_qrnnd_preinv (quot_ptr[i + 1], r, r,
  77. ((n1 << normalization_steps)
  78. | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
  79. divisor_limb, divisor_limb_inverted);
  80. n1 = n0;
  81. }
  82. udiv_qrnnd_preinv (quot_ptr[0], r, r,
  83. n1 << normalization_steps,
  84. divisor_limb, divisor_limb_inverted);
  85. return r >> normalization_steps;
  86. }
  87. else
  88. {
  89. mp_limb_t divisor_limb_inverted;
  90. /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
  91. result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  92. most significant bit (with weight 2**N) implicit. */
  93. /* Special case for DIVISOR_LIMB == 100...000. */
  94. if (divisor_limb << 1 == 0)
  95. divisor_limb_inverted = ~(mp_limb_t) 0;
  96. else
  97. udiv_qrnnd (divisor_limb_inverted, dummy,
  98. -divisor_limb, 0, divisor_limb);
  99. i = dividend_size - 1;
  100. r = dividend_ptr[i];
  101. if (r >= divisor_limb)
  102. r = 0;
  103. else
  104. {
  105. quot_ptr[i] = 0;
  106. i--;
  107. }
  108. for (; i >= 0; i--)
  109. {
  110. n0 = dividend_ptr[i];
  111. udiv_qrnnd_preinv (quot_ptr[i], r, r,
  112. n0, divisor_limb, divisor_limb_inverted);
  113. }
  114. return r;
  115. }
  116. }
  117. else
  118. {
  119. if (UDIV_NEEDS_NORMALIZATION)
  120. {
  121. int normalization_steps;
  122. count_leading_zeros (normalization_steps, divisor_limb);
  123. if (normalization_steps != 0)
  124. {
  125. divisor_limb <<= normalization_steps;
  126. n1 = dividend_ptr[dividend_size - 1];
  127. r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
  128. /* Possible optimization:
  129. if (r == 0
  130. && divisor_limb > ((n1 << normalization_steps)
  131. | (dividend_ptr[dividend_size - 2] >> ...)))
  132. ...one division less... */
  133. for (i = dividend_size - 2; i >= 0; i--)
  134. {
  135. n0 = dividend_ptr[i];
  136. udiv_qrnnd (quot_ptr[i + 1], r, r,
  137. ((n1 << normalization_steps)
  138. | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
  139. divisor_limb);
  140. n1 = n0;
  141. }
  142. udiv_qrnnd (quot_ptr[0], r, r,
  143. n1 << normalization_steps,
  144. divisor_limb);
  145. return r >> normalization_steps;
  146. }
  147. }
  148. /* No normalization needed, either because udiv_qrnnd doesn't require
  149. it, or because DIVISOR_LIMB is already normalized. */
  150. i = dividend_size - 1;
  151. r = dividend_ptr[i];
  152. if (r >= divisor_limb)
  153. r = 0;
  154. else
  155. {
  156. quot_ptr[i] = 0;
  157. i--;
  158. }
  159. for (; i >= 0; i--)
  160. {
  161. n0 = dividend_ptr[i];
  162. udiv_qrnnd (quot_ptr[i], r, r, n0, divisor_limb);
  163. }
  164. return r;
  165. }
  166. }