1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/* mpn_mu_div_qr, mpn_preinv_mu_div_qr.
Compute Q = floor(N / D) and R = N-QD. N is nn limbs and D is dn limbs and
must be normalized, and Q must be nn-dn limbs. The requirement that Q is
nn-dn limbs (and not nn-dn+1 limbs) was put in place in order to allow us to
let N be unmodified during the operation.
Contributed to the GNU project by Torbjorn Granlund.
THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH A MUTABLE INTERFACE. IT IS
ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS
ALMOST GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP
RELEASE.
Copyright 2005, 2006, 2007 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
/* We use the "misunderstanding algorithm" (MUA), discovered by Paul Zimmermann
and Torbjorn Granlund when Torbjorn misunderstood Paul's explanation of
Jebelean's bidirectional exact division algorithm.
The idea of this algorithm is to compute a smaller inverted value than used
in the standard Barrett algorithm, and thus save time in the Newton
iterations, and pay just a small price when using the inverted value for
developing quotient bits.
Written by Torbjorn Granlund. Paul Zimmermann suggested the use of the
"wrap around" trick. Based on the GMP divexact code and inspired by code
contributed to GMP by Karl Hasselstroem.
*/
/* CAUTION: This code and the code in mu_divappr_q.c should be edited in lockstep.
Things to work on:
* Passing k isn't a great interface. Either 'in' should be passed, or
determined by the code.
* The current mpn_mu_div_qr_itch isn't exactly scientifically written.
Scratch space buffer overruns are not unlikely before some analysis is
applied. Since scratch requirements are expected to change, such an
analysis will have to wait til things settle.
* This isn't optimal when the remainder isn't needed, since the final
multiplication could be made special and take O(1) time on average, in that
case. This is particularly bad when qn << dn. At some level, code as in
GMP 4 mpn_tdiv_qr should be used, effectively dividing the leading 2qn
dividend limbs by the qn divisor limbs.
* This isn't optimal when the quotient isn't needed, as it might take a lot
of space. The computation is always needed, though, so there is not time
to save with special code.
* The itch/scratch scheme isn't perhaps such a good idea as it once seemed,
demonstrated by the fact that the mpn_inv function's scratch needs means
that we need to keep a large allocation long after it is needed. Things
are worse as mpn_mul_fft does not accept any scratch parameter, which means
we'll have a large memory hole while in mpn_mul_fft. In general, a peak
scratch need in the beginning of a function isn't well-handled by the
itch/scratch scheme.
* Some ideas from comments in divexact.c apply to this code too.
*/
/* the NOSTAT stuff handles properly the case where files are concatenated */
#ifdef NOSTAT
#undef STAT
#endif
#ifdef STAT
#undef STAT
#define STAT(x) x
#else
#define NOSTAT
#define STAT(x)
#endif
#include <stdlib.h> /* for NULL */
#include "gmp.h"
#include "gmp-impl.h"
/* In case k=0 (automatic choice), we distinguish 3 cases:
(a) dn < qn: in = ceil(qn / ceil(qn/dn))
(b) dn/3 < qn <= dn: in = ceil(qn / 2)
(c) qn < dn/3: in = qn
In all cases we have in <= dn.
*/
mp_size_t
mpn_mu_div_qr_choose_in (mp_size_t qn, mp_size_t dn, int k)
{
mp_size_t in;
if (k == 0)
{
mp_size_t b;
if (qn > dn)
{
/* Compute an inverse size that is a nice partition of the quotient. */
b = (qn - 1) / dn + 1; /* ceil(qn/dn), number of blocks */
in = (qn - 1) / b + 1; /* ceil(qn/b) = ceil(qn / ceil(qn/dn)) */
}
else if (3 * qn > dn)
{
in = (qn - 1) / 2 + 1; /* b = 2 */
}
else
{
in = (qn - 1) / 1 + 1; /* b = 1 */
}
}
else
{
mp_size_t xn;
xn = MIN (dn, qn);
in = (xn - 1) / k + 1;
}
return in;
}
static mp_limb_t
mpn_mu_div_qr2 (mp_ptr qp,
mp_ptr rp,
mp_ptr np,
mp_size_t nn,
mp_srcptr dp,
mp_size_t dn,
mp_ptr scratch)
{
mp_size_t qn, in;
mp_limb_t cy;
mp_ptr ip, tp;
/* FIXME: We should probably not handle tiny operands, but do it for now. */
if (dn == 1)
{
rp[0] = mpn_divrem_1 (scratch, 0L, np, nn, dp[0]);
MPN_COPY (qp, scratch, nn - 1);
return scratch[nn - 1];
}
qn = nn - dn;
/* Compute the inverse size. */
in = mpn_mu_div_qr_choose_in (qn, dn, 0);
ASSERT (in <= dn);
#if 1
/* This alternative inverse computation method gets slightly more accurate
results. FIXMEs: (1) Temp allocation needs not analysed (2) itch function
not adapted (3) mpn_invert scratch needs not met. */
ip = scratch;
tp = scratch + in + 1;
/* compute an approximate inverse on (in+1) limbs */
if (dn == in)
{
MPN_COPY (tp + 1, dp, in);
tp[0] = 1;
mpn_invert (ip, tp, in + 1, NULL);
MPN_COPY_INCR (ip, ip + 1, in);
}
else
{
cy = mpn_add_1 (tp, dp + dn - (in + 1), in + 1, 1);
if (UNLIKELY (cy != 0))
MPN_ZERO (ip, in);
else
{
mpn_invert (ip, tp, in + 1, NULL);
MPN_COPY_INCR (ip, ip + 1, in);
}
}
#else
/* This older inverse computation method gets slightly worse results than the
one above. */
ip = scratch;
tp = scratch + in;
/* Compute inverse of D to in+1 limbs, then round to 'in' limbs. Ideally the
inversion function should do this automatically. */
if (dn == in)
{
tp[in + 1] = 0;
MPN_COPY (tp + in + 2, dp, in);
mpn_invert (tp, tp + in + 1, in + 1, NULL);
}
else
{
mpn_invert (tp, dp + dn - (in + 1), in + 1, NULL);
}
cy = mpn_sub_1 (tp, tp, in + 1, GMP_NUMB_HIGHBIT);
if (UNLIKELY (cy != 0))
MPN_ZERO (tp + 1, in);
MPN_COPY (ip, tp + 1, in);
#endif
/* We can't really handle qh = 1 like this since we'd here clobber N, which is
not allowed in the way we've defined this function's API. */
#if 0
qh = mpn_cmp (np + qn, dp, dn) >= 0;
if (qh != 0)
mpn_sub_n (np + qn, np + qn, dp, dn);
#endif
mpn_preinv_mu_div_qr (qp, rp, np, nn, dp, dn, ip, in, scratch + in);
/* return qh; */
return 0;
}
void
mpn_preinv_mu_div_qr (mp_ptr qp,
mp_ptr rp,
mp_ptr np,
mp_size_t nn,
mp_srcptr dp,
mp_size_t dn,
mp_srcptr ip,
mp_size_t in,
mp_ptr scratch)
{
mp_size_t qn;
mp_limb_t cy;
mp_ptr tp;
mp_limb_t r;
qn = nn - dn;
if (qn == 0)
{
MPN_COPY (rp, np, dn);
return;
}
tp = scratch;
np += qn;
qp += qn;
MPN_COPY (rp, np, dn);
while (qn > 0)
{
if (qn < in)
{
ip += in - qn;
in = qn;
}
np -= in;
qp -= in;
/* Compute the next block of quotient limbs by multiplying the inverse I
by the upper part of the partial remainder R. */
mpn_mul_n (tp, rp + dn - in, ip, in); /* mulhi */
cy = mpn_add_n (qp, tp + in, rp + dn - in, in); /* I's msb implicit */
ASSERT_ALWAYS (cy == 0); /* FIXME */
/* Compute the product of the quotient block and the divisor D, to be
subtracted from the partial remainder combined with new limbs from the
dividend N. We only really need the low dn limbs. */
#if WANT_FFT
if (ABOVE_THRESHOLD (dn, MUL_FFT_MODF_THRESHOLD))
{
/* Use the wrap-around trick. */
mp_size_t m, wn;
int k;
k = mpn_fft_best_k (dn + 1, 0);
m = mpn_fft_next_size (dn + 1, k);
wn = dn + in - m; /* number of wrapped limbs */
mpn_mul_fft (tp, m, dp, dn, qp, in, k);
if (wn > 0)
{
cy = mpn_add_n (tp, tp, rp + dn - wn, wn);
mpn_incr_u (tp + wn, cy);
cy = mpn_cmp (rp + dn - in, tp + dn, m - dn) < 0;
mpn_decr_u (tp, cy);
}
}
else
#endif
mpn_mul (tp, dp, dn, qp, in); /* dn+in limbs, high 'in' cancels */
r = rp[dn - in] - tp[dn];
/* Subtract the product from the partial remainder combined with new
limbs from the dividend N, generating a new partial remainder R. */
if (dn != in)
{
cy = mpn_sub_n (tp, np, tp, in); /* get next 'in' limbs from N */
cy = mpn_sub_nc (tp + in, rp, tp + in, dn - in, cy);
MPN_COPY (rp, tp, dn); /* FIXME: try to avoid this */
}
else
{
cy = mpn_sub_n (rp, np, tp, in); /* get next 'in' limbs from N */
}
STAT (int i; int err = 0;
static int errarr[5]; static int err_rec; static int tot);
/* Check the remainder R and adjust the quotient as needed. */
r -= cy;
while (r != 0)
{
/* We loop 0 times with about 69% probability, 1 time with about 31%
probability, 2 times with about 0.6% probability, if inverse is
computed as recommended. */
mpn_incr_u (qp, 1);
cy = mpn_sub_n (rp, rp, dp, dn);
r -= cy;
STAT (err++);
}
if (mpn_cmp (rp, dp, dn) >= 0)
{
/* This is executed with about 76% probability. */
mpn_incr_u (qp, 1);
cy = mpn_sub_n (rp, rp, dp, dn);
STAT (err++);
}
STAT (
tot++;
errarr[err]++;
if (err > err_rec)
err_rec = err;
if (tot % 0x10000 == 0)
{
for (i = 0; i <= err_rec; i++)
printf (" %d(%.1f%%)", errarr[i], 100.0*errarr[i]/tot);
printf ("\n");
}
);
qn -= in;
}
}
#define THRES 100 /* FIXME: somewhat arbitrary */
#ifdef CHECK
#undef THRES
#define THRES 1
#endif
mp_limb_t
mpn_mu_div_qr (mp_ptr qp,
mp_ptr rp,
mp_ptr np,
mp_size_t nn,
mp_srcptr dp,
mp_size_t dn,
mp_ptr scratch)
{
mp_size_t qn;
qn = nn - dn;
if (qn + THRES < dn)
{
/* |______________|________| dividend nn
|_______|________| divisor dn
|______| quotient (prel) qn
|_______________| quotient * ignored-part-of(divisor) dn-1
*/
mp_limb_t cy, x;
if (mpn_cmp (np + nn - (qn + 1), dp + dn - (qn + 1), qn + 1) >= 0)
{
/* Quotient is 111...111, could optimize this rare case at some point. */
mpn_mu_div_qr2 (qp, rp, np, nn, dp, dn, scratch);
return 0;
}
/* Compute a preliminary quotient and a partial remainder by dividing the
most significant limbs of each operand. */
mpn_mu_div_qr2 (qp, rp + nn - (2 * qn + 1),
np + nn - (2 * qn + 1), 2 * qn + 1,
dp + dn - (qn + 1), qn + 1,
scratch);
/* Multiply the quotient by the divisor limbs ignored above. */
if (dn - (qn + 1) > qn)
mpn_mul (scratch, dp, dn - (qn + 1), qp, qn); /* prod is dn-1 limbs */
else
mpn_mul (scratch, qp, qn, dp, dn - (qn + 1)); /* prod is dn-1 limbs */
cy = mpn_sub_n (rp, np, scratch, nn - (2 * qn + 1));
cy = mpn_sub_nc (rp + nn - (2 * qn + 1),
rp + nn - (2 * qn + 1),
scratch + nn - (2 * qn + 1),
qn, cy);
x = rp[dn - 1];
rp[dn - 1] = x - cy;
if (cy > x)
{
mpn_decr_u (qp, 1);
mpn_add_n (rp, rp, dp, dn);
}
}
else
{
return mpn_mu_div_qr2 (qp, rp, np, nn, dp, dn, scratch);
}
return 0; /* FIXME */
}
mp_size_t
mpn_mu_div_qr_itch (mp_size_t nn, mp_size_t dn, int mua_k)
{
mp_size_t qn, m;
int k;
/* FIXME: This isn't very carefully written, and might grossly overestimate
the amount of scratch needed, and might perhaps also underestimate it,
leading to potential buffer overruns. In particular k=0 might lead to
gross overestimates. */
if (dn == 1)
return nn;
qn = nn - dn;
if (qn >= dn)
{
k = mpn_fft_best_k (dn + 1, 0);
m = mpn_fft_next_size (dn + 1, k);
return (mua_k <= 1
? 6 * dn
: m + 2 * dn);
}
else
{
k = mpn_fft_best_k (dn + 1, 0);
m = mpn_fft_next_size (dn + 1, k);
return (mua_k <= 1
? m + 4 * qn
: m + 2 * qn);
}
}
|