mod_1.c 5.2 KB

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