tdiv_qr.c revision 1.1.1.3 1 1.1 mrg /* mpn_tdiv_qr -- Divide the numerator (np,nn) by the denominator (dp,dn) and
2 1.1 mrg write the nn-dn+1 quotient limbs at qp and the dn remainder limbs at rp. If
3 1.1 mrg qxn is non-zero, generate that many fraction limbs and append them after the
4 1.1 mrg other quotient limbs, and update the remainder accordingly. The input
5 1.1 mrg operands are unaffected.
6 1.1 mrg
7 1.1 mrg Preconditions:
8 1.1.1.3 mrg 1. The most significant limb of the divisor must be non-zero.
9 1.1 mrg 2. nn >= dn, even if qxn is non-zero. (??? relax this ???)
10 1.1 mrg
11 1.1 mrg The time complexity of this is O(qn*qn+M(dn,qn)), where M(m,n) is the time
12 1.1 mrg complexity of multiplication.
13 1.1 mrg
14 1.1.1.3 mrg Copyright 1997, 2000-2002, 2005, 2009, 2015 Free Software Foundation, Inc.
15 1.1 mrg
16 1.1 mrg This file is part of the GNU MP Library.
17 1.1 mrg
18 1.1 mrg The GNU MP Library is free software; you can redistribute it and/or modify
19 1.1.1.2 mrg it under the terms of either:
20 1.1.1.2 mrg
21 1.1.1.2 mrg * the GNU Lesser General Public License as published by the Free
22 1.1.1.2 mrg Software Foundation; either version 3 of the License, or (at your
23 1.1.1.2 mrg option) any later version.
24 1.1.1.2 mrg
25 1.1.1.2 mrg or
26 1.1.1.2 mrg
27 1.1.1.2 mrg * the GNU General Public License as published by the Free Software
28 1.1.1.2 mrg Foundation; either version 2 of the License, or (at your option) any
29 1.1.1.2 mrg later version.
30 1.1.1.2 mrg
31 1.1.1.2 mrg or both in parallel, as here.
32 1.1 mrg
33 1.1 mrg The GNU MP Library is distributed in the hope that it will be useful, but
34 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35 1.1.1.2 mrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
36 1.1.1.2 mrg for more details.
37 1.1 mrg
38 1.1.1.2 mrg You should have received copies of the GNU General Public License and the
39 1.1.1.2 mrg GNU Lesser General Public License along with the GNU MP Library. If not,
40 1.1.1.2 mrg see https://www.gnu.org/licenses/. */
41 1.1 mrg
42 1.1 mrg #include "gmp-impl.h"
43 1.1 mrg #include "longlong.h"
44 1.1 mrg
45 1.1 mrg
46 1.1 mrg void
47 1.1 mrg mpn_tdiv_qr (mp_ptr qp, mp_ptr rp, mp_size_t qxn,
48 1.1 mrg mp_srcptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
49 1.1 mrg {
50 1.1 mrg ASSERT_ALWAYS (qxn == 0);
51 1.1 mrg
52 1.1 mrg ASSERT (nn >= 0);
53 1.1 mrg ASSERT (dn >= 0);
54 1.1 mrg ASSERT (dn == 0 || dp[dn - 1] != 0);
55 1.1 mrg ASSERT (! MPN_OVERLAP_P (qp, nn - dn + 1 + qxn, np, nn));
56 1.1 mrg ASSERT (! MPN_OVERLAP_P (qp, nn - dn + 1 + qxn, dp, dn));
57 1.1 mrg
58 1.1 mrg switch (dn)
59 1.1 mrg {
60 1.1 mrg case 0:
61 1.1 mrg DIVIDE_BY_ZERO;
62 1.1 mrg
63 1.1 mrg case 1:
64 1.1 mrg {
65 1.1 mrg rp[0] = mpn_divrem_1 (qp, (mp_size_t) 0, np, nn, dp[0]);
66 1.1 mrg return;
67 1.1 mrg }
68 1.1 mrg
69 1.1 mrg case 2:
70 1.1 mrg {
71 1.1.1.3 mrg mp_ptr n2p;
72 1.1 mrg mp_limb_t qhl, cy;
73 1.1 mrg TMP_DECL;
74 1.1 mrg TMP_MARK;
75 1.1 mrg if ((dp[1] & GMP_NUMB_HIGHBIT) == 0)
76 1.1 mrg {
77 1.1 mrg int cnt;
78 1.1.1.3 mrg mp_limb_t d2p[2];
79 1.1 mrg count_leading_zeros (cnt, dp[1]);
80 1.1 mrg cnt -= GMP_NAIL_BITS;
81 1.1 mrg d2p[1] = (dp[1] << cnt) | (dp[0] >> (GMP_NUMB_BITS - cnt));
82 1.1 mrg d2p[0] = (dp[0] << cnt) & GMP_NUMB_MASK;
83 1.1 mrg n2p = TMP_ALLOC_LIMBS (nn + 1);
84 1.1 mrg cy = mpn_lshift (n2p, np, nn, cnt);
85 1.1 mrg n2p[nn] = cy;
86 1.1 mrg qhl = mpn_divrem_2 (qp, 0L, n2p, nn + (cy != 0), d2p);
87 1.1 mrg if (cy == 0)
88 1.1 mrg qp[nn - 2] = qhl; /* always store nn-2+1 quotient limbs */
89 1.1 mrg rp[0] = (n2p[0] >> cnt)
90 1.1 mrg | ((n2p[1] << (GMP_NUMB_BITS - cnt)) & GMP_NUMB_MASK);
91 1.1 mrg rp[1] = (n2p[1] >> cnt);
92 1.1 mrg }
93 1.1 mrg else
94 1.1 mrg {
95 1.1 mrg n2p = TMP_ALLOC_LIMBS (nn);
96 1.1 mrg MPN_COPY (n2p, np, nn);
97 1.1.1.3 mrg qhl = mpn_divrem_2 (qp, 0L, n2p, nn, dp);
98 1.1 mrg qp[nn - 2] = qhl; /* always store nn-2+1 quotient limbs */
99 1.1 mrg rp[0] = n2p[0];
100 1.1 mrg rp[1] = n2p[1];
101 1.1 mrg }
102 1.1 mrg TMP_FREE;
103 1.1 mrg return;
104 1.1 mrg }
105 1.1 mrg
106 1.1 mrg default:
107 1.1 mrg {
108 1.1 mrg int adjust;
109 1.1 mrg gmp_pi1_t dinv;
110 1.1 mrg TMP_DECL;
111 1.1 mrg TMP_MARK;
112 1.1 mrg adjust = np[nn - 1] >= dp[dn - 1]; /* conservative tests for quotient size */
113 1.1 mrg if (nn + adjust >= 2 * dn)
114 1.1 mrg {
115 1.1 mrg mp_ptr n2p, d2p;
116 1.1 mrg mp_limb_t cy;
117 1.1 mrg int cnt;
118 1.1 mrg
119 1.1 mrg qp[nn - dn] = 0; /* zero high quotient limb */
120 1.1 mrg if ((dp[dn - 1] & GMP_NUMB_HIGHBIT) == 0) /* normalize divisor */
121 1.1 mrg {
122 1.1 mrg count_leading_zeros (cnt, dp[dn - 1]);
123 1.1 mrg cnt -= GMP_NAIL_BITS;
124 1.1 mrg d2p = TMP_ALLOC_LIMBS (dn);
125 1.1 mrg mpn_lshift (d2p, dp, dn, cnt);
126 1.1 mrg n2p = TMP_ALLOC_LIMBS (nn + 1);
127 1.1 mrg cy = mpn_lshift (n2p, np, nn, cnt);
128 1.1 mrg n2p[nn] = cy;
129 1.1 mrg nn += adjust;
130 1.1 mrg }
131 1.1 mrg else
132 1.1 mrg {
133 1.1 mrg cnt = 0;
134 1.1 mrg d2p = (mp_ptr) dp;
135 1.1 mrg n2p = TMP_ALLOC_LIMBS (nn + 1);
136 1.1 mrg MPN_COPY (n2p, np, nn);
137 1.1 mrg n2p[nn] = 0;
138 1.1 mrg nn += adjust;
139 1.1 mrg }
140 1.1 mrg
141 1.1 mrg invert_pi1 (dinv, d2p[dn - 1], d2p[dn - 2]);
142 1.1 mrg if (BELOW_THRESHOLD (dn, DC_DIV_QR_THRESHOLD))
143 1.1 mrg mpn_sbpi1_div_qr (qp, n2p, nn, d2p, dn, dinv.inv32);
144 1.1 mrg else if (BELOW_THRESHOLD (dn, MUPI_DIV_QR_THRESHOLD) || /* fast condition */
145 1.1 mrg BELOW_THRESHOLD (nn, 2 * MU_DIV_QR_THRESHOLD) || /* fast condition */
146 1.1 mrg (double) (2 * (MU_DIV_QR_THRESHOLD - MUPI_DIV_QR_THRESHOLD)) * dn /* slow... */
147 1.1 mrg + (double) MUPI_DIV_QR_THRESHOLD * nn > (double) dn * nn) /* ...condition */
148 1.1 mrg mpn_dcpi1_div_qr (qp, n2p, nn, d2p, dn, &dinv);
149 1.1 mrg else
150 1.1 mrg {
151 1.1 mrg mp_size_t itch = mpn_mu_div_qr_itch (nn, dn, 0);
152 1.1 mrg mp_ptr scratch = TMP_ALLOC_LIMBS (itch);
153 1.1 mrg mpn_mu_div_qr (qp, rp, n2p, nn, d2p, dn, scratch);
154 1.1 mrg n2p = rp;
155 1.1 mrg }
156 1.1 mrg
157 1.1 mrg if (cnt != 0)
158 1.1 mrg mpn_rshift (rp, n2p, dn, cnt);
159 1.1 mrg else
160 1.1 mrg MPN_COPY (rp, n2p, dn);
161 1.1 mrg TMP_FREE;
162 1.1 mrg return;
163 1.1 mrg }
164 1.1 mrg
165 1.1 mrg /* When we come here, the numerator/partial remainder is less
166 1.1 mrg than twice the size of the denominator. */
167 1.1 mrg
168 1.1 mrg {
169 1.1 mrg /* Problem:
170 1.1 mrg
171 1.1 mrg Divide a numerator N with nn limbs by a denominator D with dn
172 1.1 mrg limbs forming a quotient of qn=nn-dn+1 limbs. When qn is small
173 1.1 mrg compared to dn, conventional division algorithms perform poorly.
174 1.1 mrg We want an algorithm that has an expected running time that is
175 1.1 mrg dependent only on qn.
176 1.1 mrg
177 1.1 mrg Algorithm (very informally stated):
178 1.1 mrg
179 1.1 mrg 1) Divide the 2 x qn most significant limbs from the numerator
180 1.1 mrg by the qn most significant limbs from the denominator. Call
181 1.1 mrg the result qest. This is either the correct quotient, but
182 1.1 mrg might be 1 or 2 too large. Compute the remainder from the
183 1.1.1.2 mrg division. (This step is implemented by an mpn_divrem call.)
184 1.1 mrg
185 1.1 mrg 2) Is the most significant limb from the remainder < p, where p
186 1.1 mrg is the product of the most significant limb from the quotient
187 1.1 mrg and the next(d)? (Next(d) denotes the next ignored limb from
188 1.1 mrg the denominator.) If it is, decrement qest, and adjust the
189 1.1 mrg remainder accordingly.
190 1.1 mrg
191 1.1 mrg 3) Is the remainder >= qest? If it is, qest is the desired
192 1.1 mrg quotient. The algorithm terminates.
193 1.1 mrg
194 1.1 mrg 4) Subtract qest x next(d) from the remainder. If there is
195 1.1 mrg borrow out, decrement qest, and adjust the remainder
196 1.1 mrg accordingly.
197 1.1 mrg
198 1.1 mrg 5) Skip one word from the denominator (i.e., let next(d) denote
199 1.1 mrg the next less significant limb. */
200 1.1 mrg
201 1.1 mrg mp_size_t qn;
202 1.1 mrg mp_ptr n2p, d2p;
203 1.1 mrg mp_ptr tp;
204 1.1 mrg mp_limb_t cy;
205 1.1 mrg mp_size_t in, rn;
206 1.1 mrg mp_limb_t quotient_too_large;
207 1.1 mrg unsigned int cnt;
208 1.1 mrg
209 1.1 mrg qn = nn - dn;
210 1.1 mrg qp[qn] = 0; /* zero high quotient limb */
211 1.1 mrg qn += adjust; /* qn cannot become bigger */
212 1.1 mrg
213 1.1 mrg if (qn == 0)
214 1.1 mrg {
215 1.1 mrg MPN_COPY (rp, np, dn);
216 1.1 mrg TMP_FREE;
217 1.1 mrg return;
218 1.1 mrg }
219 1.1 mrg
220 1.1 mrg in = dn - qn; /* (at least partially) ignored # of limbs in ops */
221 1.1 mrg /* Normalize denominator by shifting it to the left such that its
222 1.1 mrg most significant bit is set. Then shift the numerator the same
223 1.1 mrg amount, to mathematically preserve quotient. */
224 1.1 mrg if ((dp[dn - 1] & GMP_NUMB_HIGHBIT) == 0)
225 1.1 mrg {
226 1.1 mrg count_leading_zeros (cnt, dp[dn - 1]);
227 1.1 mrg cnt -= GMP_NAIL_BITS;
228 1.1 mrg
229 1.1 mrg d2p = TMP_ALLOC_LIMBS (qn);
230 1.1 mrg mpn_lshift (d2p, dp + in, qn, cnt);
231 1.1 mrg d2p[0] |= dp[in - 1] >> (GMP_NUMB_BITS - cnt);
232 1.1 mrg
233 1.1 mrg n2p = TMP_ALLOC_LIMBS (2 * qn + 1);
234 1.1 mrg cy = mpn_lshift (n2p, np + nn - 2 * qn, 2 * qn, cnt);
235 1.1 mrg if (adjust)
236 1.1 mrg {
237 1.1 mrg n2p[2 * qn] = cy;
238 1.1 mrg n2p++;
239 1.1 mrg }
240 1.1 mrg else
241 1.1 mrg {
242 1.1 mrg n2p[0] |= np[nn - 2 * qn - 1] >> (GMP_NUMB_BITS - cnt);
243 1.1 mrg }
244 1.1 mrg }
245 1.1 mrg else
246 1.1 mrg {
247 1.1 mrg cnt = 0;
248 1.1 mrg d2p = (mp_ptr) dp + in;
249 1.1 mrg
250 1.1 mrg n2p = TMP_ALLOC_LIMBS (2 * qn + 1);
251 1.1 mrg MPN_COPY (n2p, np + nn - 2 * qn, 2 * qn);
252 1.1 mrg if (adjust)
253 1.1 mrg {
254 1.1 mrg n2p[2 * qn] = 0;
255 1.1 mrg n2p++;
256 1.1 mrg }
257 1.1 mrg }
258 1.1 mrg
259 1.1 mrg /* Get an approximate quotient using the extracted operands. */
260 1.1 mrg if (qn == 1)
261 1.1 mrg {
262 1.1 mrg mp_limb_t q0, r0;
263 1.1 mrg udiv_qrnnd (q0, r0, n2p[1], n2p[0] << GMP_NAIL_BITS, d2p[0] << GMP_NAIL_BITS);
264 1.1 mrg n2p[0] = r0 >> GMP_NAIL_BITS;
265 1.1 mrg qp[0] = q0;
266 1.1 mrg }
267 1.1 mrg else if (qn == 2)
268 1.1 mrg mpn_divrem_2 (qp, 0L, n2p, 4L, d2p); /* FIXME: obsolete function */
269 1.1 mrg else
270 1.1 mrg {
271 1.1 mrg invert_pi1 (dinv, d2p[qn - 1], d2p[qn - 2]);
272 1.1 mrg if (BELOW_THRESHOLD (qn, DC_DIV_QR_THRESHOLD))
273 1.1 mrg mpn_sbpi1_div_qr (qp, n2p, 2 * qn, d2p, qn, dinv.inv32);
274 1.1 mrg else if (BELOW_THRESHOLD (qn, MU_DIV_QR_THRESHOLD))
275 1.1 mrg mpn_dcpi1_div_qr (qp, n2p, 2 * qn, d2p, qn, &dinv);
276 1.1 mrg else
277 1.1 mrg {
278 1.1 mrg mp_size_t itch = mpn_mu_div_qr_itch (2 * qn, qn, 0);
279 1.1 mrg mp_ptr scratch = TMP_ALLOC_LIMBS (itch);
280 1.1 mrg mp_ptr r2p = rp;
281 1.1 mrg if (np == r2p) /* If N and R share space, put ... */
282 1.1 mrg r2p += nn - qn; /* intermediate remainder at N's upper end. */
283 1.1 mrg mpn_mu_div_qr (qp, r2p, n2p, 2 * qn, d2p, qn, scratch);
284 1.1 mrg MPN_COPY (n2p, r2p, qn);
285 1.1 mrg }
286 1.1 mrg }
287 1.1 mrg
288 1.1 mrg rn = qn;
289 1.1 mrg /* Multiply the first ignored divisor limb by the most significant
290 1.1 mrg quotient limb. If that product is > the partial remainder's
291 1.1 mrg most significant limb, we know the quotient is too large. This
292 1.1 mrg test quickly catches most cases where the quotient is too large;
293 1.1 mrg it catches all cases where the quotient is 2 too large. */
294 1.1 mrg {
295 1.1 mrg mp_limb_t dl, x;
296 1.1 mrg mp_limb_t h, dummy;
297 1.1 mrg
298 1.1 mrg if (in - 2 < 0)
299 1.1 mrg dl = 0;
300 1.1 mrg else
301 1.1 mrg dl = dp[in - 2];
302 1.1 mrg
303 1.1 mrg #if GMP_NAIL_BITS == 0
304 1.1 mrg x = (dp[in - 1] << cnt) | ((dl >> 1) >> ((~cnt) % GMP_LIMB_BITS));
305 1.1 mrg #else
306 1.1 mrg x = (dp[in - 1] << cnt) & GMP_NUMB_MASK;
307 1.1 mrg if (cnt != 0)
308 1.1 mrg x |= dl >> (GMP_NUMB_BITS - cnt);
309 1.1 mrg #endif
310 1.1 mrg umul_ppmm (h, dummy, x, qp[qn - 1] << GMP_NAIL_BITS);
311 1.1 mrg
312 1.1 mrg if (n2p[qn - 1] < h)
313 1.1 mrg {
314 1.1 mrg mp_limb_t cy;
315 1.1 mrg
316 1.1 mrg mpn_decr_u (qp, (mp_limb_t) 1);
317 1.1 mrg cy = mpn_add_n (n2p, n2p, d2p, qn);
318 1.1 mrg if (cy)
319 1.1 mrg {
320 1.1 mrg /* The partial remainder is safely large. */
321 1.1 mrg n2p[qn] = cy;
322 1.1 mrg ++rn;
323 1.1 mrg }
324 1.1 mrg }
325 1.1 mrg }
326 1.1 mrg
327 1.1 mrg quotient_too_large = 0;
328 1.1 mrg if (cnt != 0)
329 1.1 mrg {
330 1.1 mrg mp_limb_t cy1, cy2;
331 1.1 mrg
332 1.1 mrg /* Append partially used numerator limb to partial remainder. */
333 1.1 mrg cy1 = mpn_lshift (n2p, n2p, rn, GMP_NUMB_BITS - cnt);
334 1.1 mrg n2p[0] |= np[in - 1] & (GMP_NUMB_MASK >> cnt);
335 1.1 mrg
336 1.1 mrg /* Update partial remainder with partially used divisor limb. */
337 1.1 mrg cy2 = mpn_submul_1 (n2p, qp, qn, dp[in - 1] & (GMP_NUMB_MASK >> cnt));
338 1.1 mrg if (qn != rn)
339 1.1 mrg {
340 1.1 mrg ASSERT_ALWAYS (n2p[qn] >= cy2);
341 1.1 mrg n2p[qn] -= cy2;
342 1.1 mrg }
343 1.1 mrg else
344 1.1 mrg {
345 1.1 mrg n2p[qn] = cy1 - cy2; /* & GMP_NUMB_MASK; */
346 1.1 mrg
347 1.1 mrg quotient_too_large = (cy1 < cy2);
348 1.1 mrg ++rn;
349 1.1 mrg }
350 1.1 mrg --in;
351 1.1 mrg }
352 1.1 mrg /* True: partial remainder now is neutral, i.e., it is not shifted up. */
353 1.1 mrg
354 1.1 mrg tp = TMP_ALLOC_LIMBS (dn);
355 1.1 mrg
356 1.1 mrg if (in < qn)
357 1.1 mrg {
358 1.1 mrg if (in == 0)
359 1.1 mrg {
360 1.1 mrg MPN_COPY (rp, n2p, rn);
361 1.1 mrg ASSERT_ALWAYS (rn == dn);
362 1.1 mrg goto foo;
363 1.1 mrg }
364 1.1 mrg mpn_mul (tp, qp, qn, dp, in);
365 1.1 mrg }
366 1.1 mrg else
367 1.1 mrg mpn_mul (tp, dp, in, qp, qn);
368 1.1 mrg
369 1.1 mrg cy = mpn_sub (n2p, n2p, rn, tp + in, qn);
370 1.1 mrg MPN_COPY (rp + in, n2p, dn - in);
371 1.1 mrg quotient_too_large |= cy;
372 1.1 mrg cy = mpn_sub_n (rp, np, tp, in);
373 1.1 mrg cy = mpn_sub_1 (rp + in, rp + in, rn, cy);
374 1.1 mrg quotient_too_large |= cy;
375 1.1 mrg foo:
376 1.1 mrg if (quotient_too_large)
377 1.1 mrg {
378 1.1 mrg mpn_decr_u (qp, (mp_limb_t) 1);
379 1.1 mrg mpn_add_n (rp, rp, dp, dn);
380 1.1 mrg }
381 1.1 mrg }
382 1.1 mrg TMP_FREE;
383 1.1 mrg return;
384 1.1 mrg }
385 1.1 mrg }
386 1.1 mrg }
387