1 /* mpn_sbpi1_div_q -- Schoolbook division using the Möller-Granlund 3/2
4 Contributed to the GNU project by Torbjorn Granlund.
6 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
7 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
8 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10 Copyright 2007, 2009 Free Software Foundation, Inc.
12 This file is part of the GNU MP Library.
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of the GNU Lesser General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
19 The GNU MP Library is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
22 License for more details.
24 You should have received a copy of the GNU Lesser General Public License
25 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
33 mpn_sbpi1_div_q (mp_ptr qp,
34 mp_ptr np, mp_size_t nn,
35 mp_srcptr dp, mp_size_t dn,
46 mp_size_t dn_orig = dn;
47 mp_srcptr dp_orig = dp;
52 ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
63 qh = mpn_cmp (np - dn, dp, dn) >= 0;
65 mpn_sub_n (np - dn, np - dn, dp, dn);
69 dn -= 2; /* offset dn by 2 for main division loops,
70 saving two iterations in mpn_submul_1. */
78 for (i = qn - (dn + 2); i >= 0; i--)
81 if (UNLIKELY (n1 == d1) && np[1] == d0)
84 mpn_submul_1 (np - dn, dp, dn + 2, q);
85 n1 = np[1]; /* update n1, last loop's value will now be invalid */
89 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
91 cy = mpn_submul_1 (np - dn, dp, dn, q);
94 n0 = (n0 - cy) & GMP_NUMB_MASK;
99 if (UNLIKELY (cy != 0))
101 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
109 flag = ~CNST_LIMB(0);
113 for (i = dn; i > 0; i--)
116 if (UNLIKELY (n1 >= (d1 & flag)))
119 cy = mpn_submul_1 (np - dn, dp, dn + 2, q);
121 if (UNLIKELY (n1 != cy))
123 if (n1 < (cy & flag))
126 mpn_add_n (np - dn, np - dn, dp, dn + 2);
135 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
137 cy = mpn_submul_1 (np - dn, dp, dn, q);
140 n0 = (n0 - cy) & GMP_NUMB_MASK;
145 if (UNLIKELY (cy != 0))
147 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
154 /* Truncate operands. */
160 if (UNLIKELY (n1 >= (d1 & flag)))
163 cy = mpn_submul_1 (np, dp, 2, q);
165 if (UNLIKELY (n1 != cy))
167 if (n1 < (cy & flag))
170 add_ssaaaa (np[1], np[0], np[1], np[0], dp[1], dp[0]);
179 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
187 ASSERT_ALWAYS (np[1] == n1);
192 if (UNLIKELY (n1 < (dn & flag)))
196 /* The quotient may be too large if the remainder is small. Recompute
197 for above ignored operand parts, until the remainder spills.
199 FIXME: The quality of this code isn't the same as the code above.
200 1. We don't compute things in an optimal order, high-to-low, in order
201 to terminate as quickly as possible.
202 2. We mess with pointers and sizes, adding and subtracting and
203 adjusting to get things right. It surely could be streamlined.
204 3. The only termination criteria are that we determine that the
205 quotient needs to be adjusted, or that we have recomputed
206 everything. We should stop when the remainder is so large
207 that no additional subtracting could make it spill.
208 4. If nothing else, we should not do two loops of submul_1 over the
209 data, instead handle both the triangularization and chopping at
216 /* Compensate for triangularization. */
228 for (i = dn - 3; i >= 0; i--)
231 cy = mpn_submul_1 (np - (dn - i), dp, dn - i - 2, q);
237 cy = mpn_sub_1 (qp, qp, qn, 1);
238 ASSERT_ALWAYS (cy == 0);
251 /* Compensate for ignored dividend and divisor tails. */
258 cy = mpn_sub_n (np + qn, np + qn, dp, dn - (qn + 1));
264 cy = mpn_sub_1 (qp, qp, qn, 1);
274 for (i = dn - qn - 2; i >= 0; i--)
276 cy = mpn_submul_1 (np + i, qp, qn, dp[i]);
277 cy = mpn_sub_1 (np + qn + i, np + qn + i, dn - qn - i - 1, cy);
282 cy = mpn_sub_1 (qp, qp, qn, 1);