divrem.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* mpn_divrem -- Divide natural numbers, producing both remainder and
  2. quotient.
  3. Copyright (C) 1993-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU MP Library.
  5. The GNU MP Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or (at your
  8. option) any later version.
  9. The GNU MP Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MP Library; see the file COPYING.LIB. If not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <gmp.h>
  17. #include "gmp-impl.h"
  18. #include "longlong.h"
  19. /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
  20. the NSIZE-DSIZE least significant quotient limbs at QP
  21. and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
  22. non-zero, generate that many fraction bits and append them after the
  23. other quotient limbs.
  24. Return the most significant limb of the quotient, this is always 0 or 1.
  25. Preconditions:
  26. 0. NSIZE >= DSIZE.
  27. 1. The most significant bit of the divisor must be set.
  28. 2. QP must either not overlap with the input operands at all, or
  29. QP + DSIZE >= NP must hold true. (This means that it's
  30. possible to put the quotient in the high part of NUM, right after the
  31. remainder in NUM.
  32. 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero. */
  33. mp_limb_t
  34. mpn_divrem (mp_ptr qp, mp_size_t qextra_limbs,
  35. mp_ptr np, mp_size_t nsize,
  36. mp_srcptr dp, mp_size_t dsize)
  37. {
  38. mp_limb_t most_significant_q_limb = 0;
  39. switch (dsize)
  40. {
  41. case 0:
  42. /* We are asked to divide by zero, so go ahead and do it! (To make
  43. the compiler not remove this statement, return the value.) */
  44. return 1 / dsize;
  45. case 1:
  46. {
  47. mp_size_t i;
  48. mp_limb_t n1;
  49. mp_limb_t d;
  50. d = dp[0];
  51. n1 = np[nsize - 1];
  52. if (n1 >= d)
  53. {
  54. n1 -= d;
  55. most_significant_q_limb = 1;
  56. }
  57. qp += qextra_limbs;
  58. for (i = nsize - 2; i >= 0; i--)
  59. udiv_qrnnd (qp[i], n1, n1, np[i], d);
  60. qp -= qextra_limbs;
  61. for (i = qextra_limbs - 1; i >= 0; i--)
  62. udiv_qrnnd (qp[i], n1, n1, 0, d);
  63. np[0] = n1;
  64. }
  65. break;
  66. case 2:
  67. {
  68. mp_size_t i;
  69. mp_limb_t n1, n0, n2;
  70. mp_limb_t d1, d0;
  71. np += nsize - 2;
  72. d1 = dp[1];
  73. d0 = dp[0];
  74. n1 = np[1];
  75. n0 = np[0];
  76. if (n1 >= d1 && (n1 > d1 || n0 >= d0))
  77. {
  78. sub_ddmmss (n1, n0, n1, n0, d1, d0);
  79. most_significant_q_limb = 1;
  80. }
  81. for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--)
  82. {
  83. mp_limb_t q;
  84. mp_limb_t r;
  85. if (i >= qextra_limbs)
  86. np--;
  87. else
  88. np[0] = 0;
  89. if (n1 == d1)
  90. {
  91. /* Q should be either 111..111 or 111..110. Need special
  92. treatment of this rare case as normal division would
  93. give overflow. */
  94. q = ~(mp_limb_t) 0;
  95. r = n0 + d1;
  96. if (r < d1) /* Carry in the addition? */
  97. {
  98. add_ssaaaa (n1, n0, r - d0, np[0], 0, d0);
  99. qp[i] = q;
  100. continue;
  101. }
  102. n1 = d0 - (d0 != 0);
  103. n0 = -d0;
  104. }
  105. else
  106. {
  107. udiv_qrnnd (q, r, n1, n0, d1);
  108. umul_ppmm (n1, n0, d0, q);
  109. }
  110. n2 = np[0];
  111. q_test:
  112. if (n1 > r || (n1 == r && n0 > n2))
  113. {
  114. /* The estimated Q was too large. */
  115. q--;
  116. sub_ddmmss (n1, n0, n1, n0, 0, d0);
  117. r += d1;
  118. if (r >= d1) /* If not carry, test Q again. */
  119. goto q_test;
  120. }
  121. qp[i] = q;
  122. sub_ddmmss (n1, n0, r, n2, n1, n0);
  123. }
  124. np[1] = n1;
  125. np[0] = n0;
  126. }
  127. break;
  128. default:
  129. {
  130. mp_size_t i;
  131. mp_limb_t dX, d1, n0;
  132. np += nsize - dsize;
  133. dX = dp[dsize - 1];
  134. d1 = dp[dsize - 2];
  135. n0 = np[dsize - 1];
  136. if (n0 >= dX)
  137. {
  138. if (n0 > dX || mpn_cmp (np, dp, dsize - 1) >= 0)
  139. {
  140. mpn_sub_n (np, np, dp, dsize);
  141. n0 = np[dsize - 1];
  142. most_significant_q_limb = 1;
  143. }
  144. }
  145. for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--)
  146. {
  147. mp_limb_t q;
  148. mp_limb_t n1, n2;
  149. mp_limb_t cy_limb;
  150. if (i >= qextra_limbs)
  151. {
  152. np--;
  153. n2 = np[dsize];
  154. }
  155. else
  156. {
  157. n2 = np[dsize - 1];
  158. MPN_COPY_DECR (np + 1, np, dsize);
  159. np[0] = 0;
  160. }
  161. if (n0 == dX)
  162. /* This might over-estimate q, but it's probably not worth
  163. the extra code here to find out. */
  164. q = ~(mp_limb_t) 0;
  165. else
  166. {
  167. mp_limb_t r;
  168. udiv_qrnnd (q, r, n0, np[dsize - 1], dX);
  169. umul_ppmm (n1, n0, d1, q);
  170. while (n1 > r || (n1 == r && n0 > np[dsize - 2]))
  171. {
  172. q--;
  173. r += dX;
  174. if (r < dX) /* I.e. "carry in previous addition?" */
  175. break;
  176. n1 -= n0 < d1;
  177. n0 -= d1;
  178. }
  179. }
  180. /* Possible optimization: We already have (q * n0) and (1 * n1)
  181. after the calculation of q. Taking advantage of that, we
  182. could make this loop make two iterations less. */
  183. cy_limb = mpn_submul_1 (np, dp, dsize, q);
  184. if (n2 != cy_limb)
  185. {
  186. mpn_add_n (np, np, dp, dsize);
  187. q--;
  188. }
  189. qp[i] = q;
  190. n0 = np[dsize - 1];
  191. }
  192. }
  193. }
  194. return most_significant_q_limb;
  195. }