1 /* mpn_divrem_2 -- Divide natural numbers, producing both remainder and
2 quotient. The divisor is two limbs.
4 THIS FILE CONTAINS INTERNAL FUNCTIONS WITH MUTABLE INTERFACES. IT IS
5 ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS
6 ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP
10 Copyright 1993, 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of the GNU Lesser General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or (at your
18 option) any later version.
20 The GNU MP Library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23 License for more details.
25 You should have received a copy of the GNU Lesser General Public License
26 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
33 /* The size where udiv_qrnnd_preinv should be used rather than udiv_qrnnd,
34 meaning the quotient size where that should happen, the quotient size
35 being how many udiv divisions will be done.
37 The default is to use preinv always, CPUs where this doesn't suit have
38 tuned thresholds. Note in particular that preinv should certainly be
39 used if that's the only division available (USE_PREINV_ALWAYS). */
41 #ifndef DIVREM_2_THRESHOLD
42 #define DIVREM_2_THRESHOLD 0
46 /* Divide num (NP/NSIZE) by den (DP/2) and write
47 the NSIZE-2 least significant quotient limbs at QP
48 and the 2 long remainder at NP. If QEXTRA_LIMBS is
49 non-zero, generate that many fraction bits and append them after the
51 Return the most significant limb of the quotient, this is always 0 or 1.
55 1. The most significant bit of the divisor must be set.
56 2. QP must either not overlap with the input operands at all, or
57 QP + 2 >= NP must hold true. (This means that it's
58 possible to put the quotient in the high part of NUM, right after the
60 3. NSIZE >= 2, even if QEXTRA_LIMBS is non-zero. */
63 mpn_divrem_2 (mp_ptr qp, mp_size_t qxn,
64 mp_ptr np, mp_size_t nn,
67 mp_limb_t most_significant_q_limb = 0;
76 ASSERT (dp[1] & GMP_NUMB_HIGHBIT);
77 ASSERT (! MPN_OVERLAP_P (qp, nn-2+qxn, np, nn) || qp+2 >= np);
87 if (n1 >= d1 && (n1 > d1 || n0 >= d0))
89 #if GMP_NAIL_BITS == 0
90 sub_ddmmss (n1, n0, n1, n0, d1, d0);
93 n1 = n1 - d1 - (n0 >> GMP_LIMB_BITS - 1);
96 most_significant_q_limb = 1;
99 use_preinv = ABOVE_THRESHOLD (qxn + nn - 2, DIVREM_2_THRESHOLD);
101 invert_limb (d1inv, d1);
103 for (i = qxn + nn - 2 - 1; i >= 0; i--)
115 /* Q should be either 111..111 or 111..110. Need special handling
116 of this rare case as normal division would give overflow. */
119 r = (n0 + d1) & GMP_NUMB_MASK;
120 if (r < d1) /* Carry in the addition? */
122 #if GMP_NAIL_BITS == 0
123 add_ssaaaa (n1, n0, r - d0, np[0], 0, d0);
126 n1 = (r - d0 + (n0 >> GMP_NUMB_BITS)) & GMP_NUMB_MASK;
133 n0 = -d0 & GMP_NUMB_MASK;
138 udiv_qrnnd_preinv (q, r, n1, n0, d1, d1inv);
140 udiv_qrnnd (q, r, n1, n0 << GMP_NAIL_BITS, d1 << GMP_NAIL_BITS);
142 umul_ppmm (n1, n0, d0, q << GMP_NAIL_BITS);
143 n0 >>= GMP_NAIL_BITS;
149 if (n1 > r || (n1 == r && n0 > n2))
151 /* The estimated Q was too large. */
154 #if GMP_NAIL_BITS == 0
155 sub_ddmmss (n1, n0, n1, n0, 0, d0);
158 n1 = n1 - (n0 >> GMP_LIMB_BITS - 1);
162 if (r >= d1) /* If not carry, test Q again. */
167 #if GMP_NAIL_BITS == 0
168 sub_ddmmss (n1, n0, r, n2, n1, n0);
171 n1 = r - n1 - (n0 >> GMP_LIMB_BITS - 1);
178 return most_significant_q_limb;