beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / sbpi1_div_q.c
blob3abbd579336f0fce001569e3bbd24e09629c9203
1 /* mpn_sbpi1_div_q -- Schoolbook division using the Möller-Granlund 3/2
2 division algorithm.
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 either:
17 * the GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
23 * the GNU General Public License as published by the Free Software
24 Foundation; either version 2 of the License, or (at your option) any
25 later version.
27 or both in parallel, as here.
29 The GNU MP Library is distributed in the hope that it will be useful, but
30 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
32 for more details.
34 You should have received copies of the GNU General Public License and the
35 GNU Lesser General Public License along with the GNU MP Library. If not,
36 see https://www.gnu.org/licenses/. */
39 #include "gmp.h"
40 #include "gmp-impl.h"
41 #include "longlong.h"
43 mp_limb_t
44 mpn_sbpi1_div_q (mp_ptr qp,
45 mp_ptr np, mp_size_t nn,
46 mp_srcptr dp, mp_size_t dn,
47 mp_limb_t dinv)
49 mp_limb_t qh;
50 mp_size_t qn, i;
51 mp_limb_t n1, n0;
52 mp_limb_t d1, d0;
53 mp_limb_t cy, cy1;
54 mp_limb_t q;
55 mp_limb_t flag;
57 mp_size_t dn_orig = dn;
58 mp_srcptr dp_orig = dp;
59 mp_ptr np_orig = np;
61 ASSERT (dn > 2);
62 ASSERT (nn >= dn);
63 ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
65 np += nn;
67 qn = nn - dn;
68 if (qn + 1 < dn)
70 dp += dn - (qn + 1);
71 dn = qn + 1;
74 qh = mpn_cmp (np - dn, dp, dn) >= 0;
75 if (qh != 0)
76 mpn_sub_n (np - dn, np - dn, dp, dn);
78 qp += qn;
80 dn -= 2; /* offset dn by 2 for main division loops,
81 saving two iterations in mpn_submul_1. */
82 d1 = dp[dn + 1];
83 d0 = dp[dn + 0];
85 np -= 2;
87 n1 = np[1];
89 for (i = qn - (dn + 2); i >= 0; i--)
91 np--;
92 if (UNLIKELY (n1 == d1) && np[1] == d0)
94 q = GMP_NUMB_MASK;
95 mpn_submul_1 (np - dn, dp, dn + 2, q);
96 n1 = np[1]; /* update n1, last loop's value will now be invalid */
98 else
100 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
102 cy = mpn_submul_1 (np - dn, dp, dn, q);
104 cy1 = n0 < cy;
105 n0 = (n0 - cy) & GMP_NUMB_MASK;
106 cy = n1 < cy1;
107 n1 -= cy1;
108 np[0] = n0;
110 if (UNLIKELY (cy != 0))
112 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
113 q--;
117 *--qp = q;
120 flag = ~CNST_LIMB(0);
122 if (dn >= 0)
124 for (i = dn; i > 0; i--)
126 np--;
127 if (UNLIKELY (n1 >= (d1 & flag)))
129 q = GMP_NUMB_MASK;
130 cy = mpn_submul_1 (np - dn, dp, dn + 2, q);
132 if (UNLIKELY (n1 != cy))
134 if (n1 < (cy & flag))
136 q--;
137 mpn_add_n (np - dn, np - dn, dp, dn + 2);
139 else
140 flag = 0;
142 n1 = np[1];
144 else
146 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
148 cy = mpn_submul_1 (np - dn, dp, dn, q);
150 cy1 = n0 < cy;
151 n0 = (n0 - cy) & GMP_NUMB_MASK;
152 cy = n1 < cy1;
153 n1 -= cy1;
154 np[0] = n0;
156 if (UNLIKELY (cy != 0))
158 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
159 q--;
163 *--qp = q;
165 /* Truncate operands. */
166 dn--;
167 dp++;
170 np--;
171 if (UNLIKELY (n1 >= (d1 & flag)))
173 q = GMP_NUMB_MASK;
174 cy = mpn_submul_1 (np, dp, 2, q);
176 if (UNLIKELY (n1 != cy))
178 if (n1 < (cy & flag))
180 q--;
181 add_ssaaaa (np[1], np[0], np[1], np[0], dp[1], dp[0]);
183 else
184 flag = 0;
186 n1 = np[1];
188 else
190 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
192 np[0] = n0;
193 np[1] = n1;
196 *--qp = q;
198 ASSERT_ALWAYS (np[1] == n1);
199 np += 2;
202 dn = dn_orig;
203 if (UNLIKELY (n1 < (dn & flag)))
205 mp_limb_t q, x;
207 /* The quotient may be too large if the remainder is small. Recompute
208 for above ignored operand parts, until the remainder spills.
210 FIXME: The quality of this code isn't the same as the code above.
211 1. We don't compute things in an optimal order, high-to-low, in order
212 to terminate as quickly as possible.
213 2. We mess with pointers and sizes, adding and subtracting and
214 adjusting to get things right. It surely could be streamlined.
215 3. The only termination criteria are that we determine that the
216 quotient needs to be adjusted, or that we have recomputed
217 everything. We should stop when the remainder is so large
218 that no additional subtracting could make it spill.
219 4. If nothing else, we should not do two loops of submul_1 over the
220 data, instead handle both the triangularization and chopping at
221 once. */
223 x = n1;
225 if (dn > 2)
227 /* Compensate for triangularization. */
228 mp_limb_t y;
230 dp = dp_orig;
231 if (qn + 1 < dn)
233 dp += dn - (qn + 1);
234 dn = qn + 1;
237 y = np[-2];
239 for (i = dn - 3; i >= 0; i--)
241 q = qp[i];
242 cy = mpn_submul_1 (np - (dn - i), dp, dn - i - 2, q);
244 if (y < cy)
246 if (x == 0)
248 cy = mpn_sub_1 (qp, qp, qn, 1);
249 ASSERT_ALWAYS (cy == 0);
250 return qh - cy;
252 x--;
254 y -= cy;
256 np[-2] = y;
259 dn = dn_orig;
260 if (qn + 1 < dn)
262 /* Compensate for ignored dividend and divisor tails. */
264 dp = dp_orig;
265 np = np_orig;
267 if (qh != 0)
269 cy = mpn_sub_n (np + qn, np + qn, dp, dn - (qn + 1));
270 if (cy != 0)
272 if (x == 0)
274 if (qn != 0)
275 cy = mpn_sub_1 (qp, qp, qn, 1);
276 return qh - cy;
278 x--;
282 if (qn == 0)
283 return qh;
285 for (i = dn - qn - 2; i >= 0; i--)
287 cy = mpn_submul_1 (np + i, qp, qn, dp[i]);
288 cy = mpn_sub_1 (np + qn + i, np + qn + i, dn - qn - i - 1, cy);
289 if (cy != 0)
291 if (x == 0)
293 cy = mpn_sub_1 (qp, qp, qn, 1);
294 return qh;
296 x--;
302 return qh;