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
|
/* mpn_compute_powtab.
Contributed to the GNU project by Torbjorn Granlund.
THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. 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 GNU MP RELEASE.
Copyright 1991-2017 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 either:
* 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.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
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 General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
/*
CAVEATS:
* The exptab and powtab vectors are in opposite orders. Probably OK.
* Consider getting rid of exptab, doing bit ops on the un argument instead.
* Consider rounding greatest power slightly upwards to save adjustments.
* In powtab_decide, consider computing cost from just the 2-3 largest
operands, since smaller operand contribute little. This makes most sense
if exptab is suppressed.
*/
#include "gmp-impl.h"
#ifndef DIV_1_VS_MUL_1_PERCENT
#define DIV_1_VS_MUL_1_PERCENT 150
#endif
#define SET_powers_t(dest, ptr, size, dib, b, sh) \
do { \
dest.p = ptr; \
dest.n = size; \
dest.digits_in_base = dib; \
dest.base = b; \
dest.shift = sh; \
} while (0)
#if DIV_1_VS_MUL_1_PERCENT > 120
#define HAVE_mpn_compute_powtab_mul 1
static void
mpn_compute_powtab_mul (powers_t *powtab, mp_ptr powtab_mem, mp_size_t un,
int base, const size_t *exptab, size_t n_pows)
{
mp_size_t n;
mp_ptr p, t;
mp_limb_t cy;
long start_idx;
int c;
mp_limb_t big_base = mp_bases[base].big_base;
int chars_per_limb = mp_bases[base].chars_per_limb;
mp_ptr powtab_mem_ptr = powtab_mem;
size_t digits_in_base = chars_per_limb;
powers_t *pt = powtab;
p = powtab_mem_ptr;
powtab_mem_ptr += 1;
p[0] = big_base;
SET_powers_t (pt[0], p, 1, digits_in_base, base, 0);
pt++;
t = powtab_mem_ptr;
powtab_mem_ptr += 2;
t[1] = mpn_mul_1 (t, p, 1, big_base);
n = 2;
digits_in_base *= 2;
c = t[0] == 0;
t += c;
n -= c;
mp_size_t shift = c;
SET_powers_t (pt[0], t, n, digits_in_base, base, shift);
p = t;
pt++;
if (exptab[0] == ((size_t) chars_per_limb << n_pows))
{
start_idx = n_pows - 2;
}
else
{
if (((digits_in_base + chars_per_limb) << (n_pows-2)) <= exptab[0])
{
/* 3, sometimes adjusted to 4. */
t = powtab_mem_ptr;
powtab_mem_ptr += 4;
t[n] = cy = mpn_mul_1 (t, p, n, big_base);
n += cy != 0;;
digits_in_base += chars_per_limb;
c = t[0] == 0;
t += c;
n -= c;
shift += c;
}
else
{
/* 2 copy, will always become 3 with back-multiplication. */
t = powtab_mem_ptr;
powtab_mem_ptr += 3;
t[0] = p[0];
t[1] = p[1];
}
SET_powers_t (pt[0], t, n, digits_in_base, base, shift);
p = t;
pt++;
start_idx = n_pows - 3;
}
for (long pi = start_idx; pi >= 0; pi--)
{
t = powtab_mem_ptr;
powtab_mem_ptr += 2 * n + 2;
ASSERT (powtab_mem_ptr < powtab_mem + mpn_str_powtab_alloc (un));
mpn_sqr (t, p, n);
digits_in_base *= 2;
n *= 2;
n -= t[n - 1] == 0;
shift *= 2;
c = t[0] == 0;
t += c;
n -= c;
shift += c;
/* Adjust new value if it is too small as input to the next squaring. */
if (((digits_in_base + chars_per_limb) << pi) <= exptab[0])
{
t[n] = cy = mpn_mul_1 (t, t, n, big_base);
n += cy != 0;
digits_in_base += chars_per_limb;
c = t[0] == 0;
t += c;
n -= c;
shift += c;
}
SET_powers_t (pt[0], t, n, digits_in_base, base, shift);
/* Adjust previous value if it is not at its target power. */
if (pt[-1].digits_in_base < exptab[pi + 1])
{
mp_size_t n = pt[-1].n;
mp_ptr p = pt[-1].p;
p[n] = cy = mpn_mul_1 (p, p, n, big_base);
n += cy != 0;
ASSERT (pt[-1].digits_in_base + chars_per_limb == exptab[pi + 1]);
pt[-1].digits_in_base = exptab[pi + 1];
c = p[0] == 0;
pt[-1].p = p + c;
pt[-1].n = n - c;
pt[-1].shift += c;
}
p = t;
pt++;
}
}
#endif
#if DIV_1_VS_MUL_1_PERCENT < 275
#define HAVE_mpn_compute_powtab_div 1
static void
mpn_compute_powtab_div (powers_t *powtab, mp_ptr powtab_mem, mp_size_t un,
int base, const size_t *exptab, size_t n_pows)
{
mp_ptr p, t;
mp_limb_t big_base = mp_bases[base].big_base;
int chars_per_limb = mp_bases[base].chars_per_limb;
mp_ptr powtab_mem_ptr = powtab_mem;
size_t digits_in_base = chars_per_limb;
powers_t *pt = powtab;
p = powtab_mem_ptr;
powtab_mem_ptr += 1;
p[0] = big_base;
SET_powers_t (pt[0], p, 1, digits_in_base, base, 0);
pt++;
mp_size_t n = 1;
mp_size_t shift = 0;
for (long pi = n_pows - 1; pi >= 0; pi--)
{
t = powtab_mem_ptr;
powtab_mem_ptr += 2 * n;
ASSERT (powtab_mem_ptr < powtab_mem + mpn_str_powtab_alloc (un));
mpn_sqr (t, p, n);
n = 2 * n - 1; n += t[n] != 0;
digits_in_base *= 2;
if (digits_in_base != exptab[pi]) /* if ((((un - 1) >> pi) & 2) == 0) */
{
#if HAVE_NATIVE_mpn_pi1_bdiv_q_1 || ! HAVE_NATIVE_mpn_divexact_1
if (__GMP_LIKELY (base == 10))
mpn_pi1_bdiv_q_1 (t, t, n, big_base >> MP_BASES_BIG_BASE_CTZ_10,
MP_BASES_BIG_BASE_BINVERTED_10,
MP_BASES_BIG_BASE_CTZ_10);
else
#endif
/* FIXME: We could use _pi1 here if we add big_base_binverted and
big_base_ctz fields to struct bases. That would add about 2 KiB
to mp_bases.c.
FIXME: Use mpn_bdiv_q_1 here when mpn_divexact_1 is converted to
mpn_bdiv_q_1 for more machines. */
mpn_divexact_1 (t, t, n, big_base);
n -= t[n - 1] == 0;
digits_in_base -= chars_per_limb;
}
shift *= 2;
/* Strip low zero limbs, but be careful to keep the result divisible by
big_base. */
while (t[0] == 0 && (t[1] & ((big_base & -big_base) - 1)) == 0)
{
t++;
n--;
shift++;
}
p = t;
SET_powers_t (pt[0], p, n, digits_in_base, base, shift);
pt++;
}
/* Strip any remaining low zero limbs. */
pt -= n_pows + 1;
for (long pi = n_pows; pi >= 0; pi--)
{
mp_ptr t = pt[pi].p;
mp_size_t shift = pt[pi].shift;
mp_size_t n = pt[pi].n;
int c;
c = t[0] == 0;
t += c;
n -= c;
shift += c;
pt[pi].p = t;
pt[pi].shift = shift;
pt[pi].n = n;
}
}
#endif
static long
powtab_decide (size_t *exptab, size_t un, int base)
{
int chars_per_limb = mp_bases[base].chars_per_limb;
long n_pows = 0;
for (size_t pn = (un + 1) >> 1; pn != 1; pn = (pn + 1) >> 1)
{
exptab[n_pows] = pn * chars_per_limb;
n_pows++;
}
exptab[n_pows] = chars_per_limb;
#if HAVE_mpn_compute_powtab_mul && HAVE_mpn_compute_powtab_div
size_t pn = un - 1;
size_t xn = (un + 1) >> 1;
unsigned mcost = 1;
unsigned dcost = 1;
for (long i = n_pows - 2; i >= 0; i--)
{
size_t pow = (pn >> (i + 1)) + 1;
if (pow & 1)
dcost += pow;
if (xn != (pow << i))
{
if (pow > 2 && (pow & 1) == 0)
mcost += 2 * pow;
else
mcost += pow;
}
else
{
if (pow & 1)
mcost += pow;
}
}
dcost = dcost * DIV_1_VS_MUL_1_PERCENT / 100;
if (mcost <= dcost)
return n_pows;
else
return -n_pows;
#elif HAVE_mpn_compute_powtab_mul
return n_pows;
#elif HAVE_mpn_compute_powtab_div
return -n_pows;
#else
#error "no powtab function available"
#endif
}
size_t
mpn_compute_powtab (powers_t *powtab, mp_ptr powtab_mem, mp_size_t un, int base)
{
size_t exptab[GMP_LIMB_BITS];
long n_pows = powtab_decide (exptab, un, base);
#if HAVE_mpn_compute_powtab_mul && HAVE_mpn_compute_powtab_div
if (n_pows >= 0)
{
mpn_compute_powtab_mul (powtab, powtab_mem, un, base, exptab, n_pows);
return n_pows;
}
else
{
mpn_compute_powtab_div (powtab, powtab_mem, un, base, exptab, -n_pows);
return -n_pows;
}
#elif HAVE_mpn_compute_powtab_mul
ASSERT (n_pows > 0);
mpn_compute_powtab_mul (powtab, powtab_mem, un, base, exptab, n_pows);
return n_pows;
#elif HAVE_mpn_compute_powtab_div
ASSERT (n_pows < 0);
mpn_compute_powtab_div (powtab, powtab_mem, un, base, exptab, -n_pows);
return -n_pows;
#else
#error "no powtab function available"
#endif
}
|