1 /* mpn_divrem -- Divide natural numbers, producing both remainder and
4 Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 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 Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
28 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
29 the NSIZE-DSIZE least significant quotient limbs at QP
30 and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
31 non-zero, generate that many fraction bits and append them after the
33 Return the most significant limb of the quotient, this is always 0 or 1.
37 1. The most significant bit of the divisor must be set.
38 2. QP must either not overlap with the input operands at all, or
39 QP + DSIZE >= NP must hold true. (This means that it's
40 possible to put the quotient in the high part of NUM, right after the
42 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero. */
46 mpn_divrem (mp_ptr qp, mp_size_t qextra_limbs,
47 mp_ptr np, mp_size_t nsize,
48 mp_srcptr dp, mp_size_t dsize)
50 mpn_divrem (qp, qextra_limbs, np, nsize, dp, dsize)
52 mp_size_t qextra_limbs;
59 mp_limb_t most_significant_q_limb = 0;
64 /* We are asked to divide by zero, so go ahead and do it! (To make
65 the compiler not remove this statement, return the value.) */
80 most_significant_q_limb = 1;
84 for (i = nsize - 2; i >= 0; i--)
85 udiv_qrnnd (qp[i], n1, n1, np[i], d);
88 for (i = qextra_limbs - 1; i >= 0; i--)
89 udiv_qrnnd (qp[i], n1, n1, 0, d);
107 if (n1 >= d1 && (n1 > d1 || n0 >= d0))
109 sub_ddmmss (n1, n0, n1, n0, d1, d0);
110 most_significant_q_limb = 1;
113 for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--)
118 if (i >= qextra_limbs)
125 /* Q should be either 111..111 or 111..110. Need special
126 treatment of this rare case as normal division would
131 if (r < d1) /* Carry in the addition? */
133 add_ssaaaa (n1, n0, r - d0, np[0], 0, d0);
142 udiv_qrnnd (q, r, n1, n0, d1);
143 umul_ppmm (n1, n0, d0, q);
148 if (n1 > r || (n1 == r && n0 > n2))
150 /* The estimated Q was too large. */
153 sub_ddmmss (n1, n0, n1, n0, 0, d0);
155 if (r >= d1) /* If not carry, test Q again. */
160 sub_ddmmss (n1, n0, r, n2, n1, n0);
170 mp_limb_t dX, d1, n0;
179 if (n0 > dX || mpn_cmp (np, dp, dsize - 1) >= 0)
181 mpn_sub_n (np, np, dp, dsize);
183 most_significant_q_limb = 1;
187 for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--)
193 if (i >= qextra_limbs)
201 MPN_COPY_DECR (np + 1, np, dsize);
206 /* This might over-estimate q, but it's probably not worth
207 the extra code here to find out. */
213 udiv_qrnnd (q, r, n0, np[dsize - 1], dX);
214 umul_ppmm (n1, n0, d1, q);
216 while (n1 > r || (n1 == r && n0 > np[dsize - 2]))
220 if (r < dX) /* I.e. "carry in previous addition?" */
227 /* Possible optimization: We already have (q * n0) and (1 * n1)
228 after the calculation of q. Taking advantage of that, we
229 could make this loop make two iterations less. */
231 cy_limb = mpn_submul_1 (np, dp, dsize, q);
235 mpn_add_n (np, np, dp, dsize);
245 return most_significant_q_limb;