divrem.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* mpn_divrem -- Divide natural numbers, producing both remainder and
  2. quotient.
  3. Copyright (C) 1993, 1994, 1995, 1996 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, write to
  15. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16. MA 02111-1307, USA. */
  17. #include <config.h>
  18. #include "gmp-impl.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. #if __STDC__
  35. mpn_divrem (mp_ptr qp, mp_size_t qextra_limbs,
  36. mp_ptr np, mp_size_t nsize,
  37. mp_srcptr dp, mp_size_t dsize)
  38. #else
  39. mpn_divrem (qp, qextra_limbs, np, nsize, dp, dsize)
  40. mp_ptr qp;
  41. mp_size_t qextra_limbs;
  42. mp_ptr np;
  43. mp_size_t nsize;
  44. mp_srcptr dp;
  45. mp_size_t dsize;
  46. #endif
  47. {
  48. mp_limb_t most_significant_q_limb = 0;
  49. switch (dsize)
  50. {
  51. case 0:
  52. /* We are asked to divide by zero, so go ahead and do it! (To make
  53. the compiler not remove this statement, return the value.) */
  54. return 1 / dsize;
  55. case 1:
  56. {
  57. mp_size_t i;
  58. mp_limb_t n1;
  59. mp_limb_t d;
  60. d = dp[0];
  61. n1 = np[nsize - 1];
  62. if (n1 >= d)
  63. {
  64. n1 -= d;
  65. most_significant_q_limb = 1;
  66. }
  67. qp += qextra_limbs;
  68. for (i = nsize - 2; i >= 0; i--)
  69. udiv_qrnnd (qp[i], n1, n1, np[i], d);
  70. qp -= qextra_limbs;
  71. for (i = qextra_limbs - 1; i >= 0; i--)
  72. udiv_qrnnd (qp[i], n1, n1, 0, d);
  73. np[0] = n1;
  74. }
  75. break;
  76. case 2:
  77. {
  78. mp_size_t i;
  79. mp_limb_t n1, n0, n2;
  80. mp_limb_t d1, d0;
  81. np += nsize - 2;
  82. d1 = dp[1];
  83. d0 = dp[0];
  84. n1 = np[1];
  85. n0 = np[0];
  86. if (n1 >= d1 && (n1 > d1 || n0 >= d0))
  87. {
  88. sub_ddmmss (n1, n0, n1, n0, d1, d0);
  89. most_significant_q_limb = 1;
  90. }
  91. for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--)
  92. {
  93. mp_limb_t q;
  94. mp_limb_t r;
  95. if (i >= qextra_limbs)
  96. np--;
  97. else
  98. np[0] = 0;
  99. if (n1 == d1)
  100. {
  101. /* Q should be either 111..111 or 111..110. Need special
  102. treatment of this rare case as normal division would
  103. give overflow. */
  104. q = ~(mp_limb_t) 0;
  105. r = n0 + d1;
  106. if (r < d1) /* Carry in the addition? */
  107. {
  108. add_ssaaaa (n1, n0, r - d0, np[0], 0, d0);
  109. qp[i] = q;
  110. continue;
  111. }
  112. n1 = d0 - (d0 != 0);
  113. n0 = -d0;
  114. }
  115. else
  116. {
  117. udiv_qrnnd (q, r, n1, n0, d1);
  118. umul_ppmm (n1, n0, d0, q);
  119. }
  120. n2 = np[0];
  121. q_test:
  122. if (n1 > r || (n1 == r && n0 > n2))
  123. {
  124. /* The estimated Q was too large. */
  125. q--;
  126. sub_ddmmss (n1, n0, n1, n0, 0, d0);
  127. r += d1;
  128. if (r >= d1) /* If not carry, test Q again. */
  129. goto q_test;
  130. }
  131. qp[i] = q;
  132. sub_ddmmss (n1, n0, r, n2, n1, n0);
  133. }
  134. np[1] = n1;
  135. np[0] = n0;
  136. }
  137. break;
  138. default:
  139. {
  140. mp_size_t i;
  141. mp_limb_t dX, d1, n0;
  142. np += nsize - dsize;
  143. dX = dp[dsize - 1];
  144. d1 = dp[dsize - 2];
  145. n0 = np[dsize - 1];
  146. if (n0 >= dX)
  147. {
  148. if (n0 > dX || mpn_cmp (np, dp, dsize - 1) >= 0)
  149. {
  150. mpn_sub_n (np, np, dp, dsize);
  151. n0 = np[dsize - 1];
  152. most_significant_q_limb = 1;
  153. }
  154. }
  155. for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--)
  156. {
  157. mp_limb_t q;
  158. mp_limb_t n1, n2;
  159. mp_limb_t cy_limb;
  160. if (i >= qextra_limbs)
  161. {
  162. np--;
  163. n2 = np[dsize];
  164. }
  165. else
  166. {
  167. n2 = np[dsize - 1];
  168. MPN_COPY_DECR (np + 1, np, dsize);
  169. np[0] = 0;
  170. }
  171. if (n0 == dX)
  172. /* This might over-estimate q, but it's probably not worth
  173. the extra code here to find out. */
  174. q = ~(mp_limb_t) 0;
  175. else
  176. {
  177. mp_limb_t r;
  178. udiv_qrnnd (q, r, n0, np[dsize - 1], dX);
  179. umul_ppmm (n1, n0, d1, q);
  180. while (n1 > r || (n1 == r && n0 > np[dsize - 2]))
  181. {
  182. q--;
  183. r += dX;
  184. if (r < dX) /* I.e. "carry in previous addition?" */
  185. break;
  186. n1 -= n0 < d1;
  187. n0 -= d1;
  188. }
  189. }
  190. /* Possible optimization: We already have (q * n0) and (1 * n1)
  191. after the calculation of q. Taking advantage of that, we
  192. could make this loop make two iterations less. */
  193. cy_limb = mpn_submul_1 (np, dp, dsize, q);
  194. if (n2 != cy_limb)
  195. {
  196. mpn_add_n (np, np, dp, dsize);
  197. q--;
  198. }
  199. qp[i] = q;
  200. n0 = np[dsize - 1];
  201. }
  202. }
  203. }
  204. return most_significant_q_limb;
  205. }