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