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
|
/* Test mpn_fib2m.
Copyright 2018 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU 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 test suite 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 a copy of the GNU General Public License along with
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"
#define MAX_K_BITS 16
#define MAX_K (1L << MAX_K_BITS)
#define MIN_K 1
#define MAX_MN 20
#define MAX_KN 30
#define COUNT 200
static int
test_fib2_fib2m (int count, gmp_randstate_ptr rands)
{
int test;
mp_ptr fk, fks1, fkm, fks1m, mp, qp;
mp_size_t mn, fn, size, max_mn;
TMP_DECL;
size = MPN_FIB2_SIZE (MAX_K);
max_mn = size / 4 + 10;
ASSERT (max_mn < size);
TMP_MARK;
fk = TMP_ALLOC_LIMBS (size);
fks1 = TMP_ALLOC_LIMBS (size);
qp = TMP_ALLOC_LIMBS (size);
mp = TMP_ALLOC_LIMBS (max_mn);
fkm = 1 + TMP_ALLOC_LIMBS (max_mn * 2 + 1 + 2);
fks1m = 1 + TMP_ALLOC_LIMBS (max_mn * 2 + 1 + 2);
for (test = 1; test <= count; ++test)
{
mp_limb_t fk_before, fk_after, fk1_before, fk1_after;
int signflip;
unsigned long k;
k = MIN_K +
gmp_urandomm_ui (rands, test < MAX_K_BITS ?
MAX_K >> test : (MAX_K - MIN_K));
fn = mpn_fib2_ui (fk, fks1, k);
do {
mn = gmp_urandomm_ui (rands, MAX_K) % (fn / 4 + 10);
} while (mn == 0);
ASSERT (mn <= max_mn);
mpn_random2 (mp, mn);
ASSERT (mp [mn - 1] != 0);
if (fn >= mn)
{
mpn_tdiv_qr (qp, fk, 0, fk, fn, mp, mn);
mpn_tdiv_qr (qp, fks1, 0, fks1, fn, mp, mn);
}
else
{
MPN_ZERO (fk + fn, mn - fn);
MPN_ZERO (fks1 + fn, mn - fn);
}
mpn_random2 (fkm - 1, 2*mn+1+2);
fk_before = fkm [-1];
fk_after = fkm [2 * mn + 1];
mpn_random2 (fks1m - 1, 2*mn+1+2);
fk1_before = fks1m [-1];
fk1_after = fks1m [2 * mn + 1];
qp [0] = k;
signflip = mpn_fib2m (fkm, fks1m, qp, 1, mp, mn);
if (fkm [-1] != fk_before || fkm [2 * mn + 1] != fk_after
|| fks1m [-1] != fk1_before || fks1m [2 * mn + 1] != fk1_after)
{
printf ("REDZONE violation in test %d, k = %lu, mn = %u\n",
test, k, (unsigned) mn);
if (fkm[-1] != fk_before)
{
printf ("before fkm:"); mpn_dump (fkm - 1, 1);
printf ("keep: "); mpn_dump (&fk_before, 1);
}
if (fkm[2 * mn + 1] != fk_after)
{
printf ("after fkm:"); mpn_dump (fkm + 2 * mn + 1, 1);
printf ("keep: "); mpn_dump (&fk_after, 1);
}
if (fks1m[-1] != fk1_before)
{
printf ("before fks1m:"); mpn_dump (fks1m - 1, 1);
printf ("keep: "); mpn_dump (&fk1_before, 1);
}
if (fks1m[2 * mn + 1] != fk1_after)
{
printf ("after fks1m:"); mpn_dump (fks1m + 2 * mn + 1, 1);
printf ("keep: "); mpn_dump (&fk1_after, 1);
}
abort();
}
if (mpn_cmp (fkm, fk, mn) != 0)
{
if (mpn_sub_n (fk, mp, fk, mn) || mpn_cmp (fkm, fk, mn) != 0)
{
printf ("ERROR(k) in test %d, k = %lu, mn = %u\n",
test, k, (unsigned) mn);
mpn_dump (fk, mn);
mpn_dump (fkm, mn);
mpn_dump (mp, mn);
abort();
}
signflip ^= 1;
}
if (mpn_cmp (fks1m, fks1, mn) != 0)
{
if (mpn_sub_n (fks1, mp, fks1, mn) || mpn_cmp (fks1m, fks1, mn) != 0)
{
printf ("ERROR(k-1) in test %d, k = %lu, mn = %u\n",
test, k, (unsigned) mn);
mpn_dump (fks1, mn);
mpn_dump (fks1m, mn);
mpn_dump (mp, mn);
abort();
}
signflip ^= 1;
}
if (signflip != 0 && ! mpn_zero_p (fks1m, mn) && ! mpn_zero_p (fkm, mn))
{
if ((mp [0] & 1) == 0) /* Should we test only odd modulus-es? */
{
if (! mpn_lshift (fks1m, fks1m, mn, 1) &&
mpn_cmp (mp, fks1m, mn) == 0)
continue;
if (! mpn_lshift (fkm, fkm, mn, 1) &&
mpn_cmp (mp, fkm, mn) == 0)
continue;
}
printf ("ERROR(sign) in test %d, k = %lu, mn = %u\n",
test, k, (unsigned) mn);
abort();
}
}
TMP_FREE;
return 0;
}
static int
test_fib2m_2exp (int count, gmp_randstate_ptr rands)
{
int test;
mp_ptr fka, fks1a, fkb, fks1b, mp, kp;
TMP_DECL;
TMP_MARK;
kp = TMP_ALLOC_LIMBS (MAX_KN);
mp = TMP_ALLOC_LIMBS (MAX_MN);
fka = 1 + TMP_ALLOC_LIMBS (MAX_MN * 2 + 1 + 2);
fks1a = 1 + TMP_ALLOC_LIMBS (MAX_MN * 2 + 1 + 2);
fkb = 1 + TMP_ALLOC_LIMBS (MAX_MN * 2 + 1 + 2);
fks1b = 1 + TMP_ALLOC_LIMBS (MAX_MN * 2 + 1 + 2);
for (test = 1; test <= count; ++test)
{
mp_limb_t fka_before, fka_after, fk1a_before, fk1a_after;
mp_limb_t fkb_before, fkb_after, fk1b_before, fk1b_after;
mp_size_t mn, kn;
int signflip;
mp_bitcnt_t exp2;
mn = gmp_urandomm_ui (rands, MAX_MN - 1) + 1;
mpn_random2 (mp, mn);
exp2 = MIN_K + 1 + gmp_urandomm_ui (rands, MAX_KN * GMP_NUMB_BITS - MIN_K - 1);
kn = BITS_TO_LIMBS (exp2);
MPN_ZERO (kp, kn - 1);
kp [kn - 1] = CNST_LIMB (1) << ((exp2 - 1) % GMP_NUMB_BITS);
mpn_random2 (fka - 1, 2*mn+1+2);
fka_before = fka [-1];
fka_after = fka [2 * mn + 1];
mpn_random2 (fks1a - 1, 2*mn+1+2);
fk1a_before = fks1a [-1];
fk1a_after = fks1a [2 * mn + 1];
signflip = mpn_fib2m (fka, fks1a, kp, kn, mp, mn);
if (fka [-1] != fka_before || fka [2 * mn + 1] != fka_after
|| fks1a [-1] != fk1a_before || fks1a [2 * mn + 1] != fk1a_after)
{
printf ("REDZONE(a) violation in test %d, exp2 = %lu\n", test, exp2);
if (fka[-1] != fka_before)
{
printf ("before fka:"); mpn_dump (fka - 1, 1);
printf ("keep: "); mpn_dump (&fka_before, 1);
}
if (fka[2 * mn + 1] != fka_after)
{
printf ("after fka:"); mpn_dump (fka + 2 * mn + 1, 1);
printf ("keep: "); mpn_dump (&fka_after, 1);
}
if (fks1a[-1] != fk1a_before)
{
printf ("before fks1a:"); mpn_dump (fks1a - 1, 1);
printf ("keep: "); mpn_dump (&fk1a_before, 1);
}
if (fks1a[2 * mn + 1] != fk1a_after)
{
printf ("after fks1a:"); mpn_dump (fks1a + 2 * mn + 1, 1);
printf ("keep: "); mpn_dump (&fk1a_after, 1);
}
abort();
}
if (signflip && ! mpn_zero_p (fks1a, mn))
mpn_sub_n (fks1a, mp, fks1a, mn);
if (mpn_sub_n (fka, fka, fks1a, mn))
ASSERT_CARRY (mpn_add_n (fka, fka, mp, mn));
mpn_sub_1 (kp, kp, kn, 1);
ASSERT (exp2 % GMP_NUMB_BITS == 1 || kp [kn - 1] != 0);
kn -= kp [kn - 1] == 0;
mpn_random2 (fkb - 1, 2*mn+1+2);
fkb_before = fkb [-1];
fkb_after = fkb [2 * mn + 1];
mpn_random2 (fks1b - 1, 2*mn+1+2);
fk1b_before = fks1b [-1];
fk1b_after = fks1b [2 * mn + 1];
signflip = mpn_fib2m (fkb, fks1b, kp, kn, mp, mn);
if (fkb [-1] != fkb_before || fkb [2 * mn + 1] != fkb_after
|| fks1b [-1] != fk1b_before || fks1b [2 * mn + 1] != fk1b_after)
{
printf ("REDZONE(b) violation in test %d, exp2 = %lu\n", test, exp2);
if (fkb[-1] != fkb_before)
{
printf ("before fkb:"); mpn_dump (fkb - 1, 1);
printf ("keep: "); mpn_dump (&fkb_before, 1);
}
if (fkb[2 * mn + 1] != fkb_after)
{
printf ("after fkb:"); mpn_dump (fkb + 2 * mn + 1, 1);
printf ("keep: "); mpn_dump (&fkb_after, 1);
}
if (fks1b[-1] != fk1b_before)
{
printf ("before fks1b:"); mpn_dump (fks1b - 1, 1);
printf ("keep: "); mpn_dump (&fk1b_before, 1);
}
if (fks1b[2 * mn + 1] != fk1b_after)
{
printf ("after fks1b:"); mpn_dump (fks1b + 2 * mn + 1, 1);
printf ("keep: "); mpn_dump (&fk1b_after, 1);
}
abort();
}
if (mpn_cmp (fks1a, fkb, mn) != 0)
{
if (mpn_sub_n (fkb, mp, fkb, mn) || mpn_cmp (fks1a, fkb, mn) != 0)
{
printf ("ERROR(k) in test %d, exp2 = %lu\n", test, exp2);
mpn_dump (fks1a, mn);
mpn_dump (fkb, mn);
mpn_dump (mp, mn);
abort();
}
signflip ^= 1;
}
if (mpn_cmp (fka, fks1b, mn) != 0)
{
if (mpn_sub_n (fks1b, mp, fks1b, mn) || mpn_cmp (fka, fks1b, mn) != 0)
{
printf ("ERROR(k-1) in test %d, exp2 = %lu\n", test, exp2);
mpn_dump (fka, mn);
mpn_dump (fks1b, mn);
mpn_dump (mp, mn);
abort();
}
signflip ^= 1;
}
if (signflip != 0 && ! mpn_zero_p (fks1b, mn) && ! mpn_zero_p (fkb, mn))
{
if ((mp [0] & 1) == 0) /* Should we test only odd modulus-es? */
{
if (! mpn_lshift (fks1b, fks1b, mn, 1) &&
mpn_cmp (mp, fks1b, mn) == 0)
continue;
if (! mpn_lshift (fkb, fkb, mn, 1) &&
mpn_cmp (mp, fkb, mn) == 0)
continue;
}
printf ("ERROR(sign) in test %d, exp2 = %lu\n",
test, exp2);
abort();
}
}
TMP_FREE;
return 0;
}
int
main (int argc, char **argv)
{
int count = COUNT;
gmp_randstate_ptr rands;
tests_start ();
TESTS_REPS (count, argv, argc);
rands = RANDS;
test_fib2_fib2m (count / 2, rands);
test_fib2m_2exp (count / 2, rands);
tests_end ();
exit (0);
}
|