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
|
/* Test mpz_cmp, mpz_mul.
Copyright 1991, 1993, 1994, 1996, 1997, 2000-2004 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 "longlong.h"
#include "tests.h"
void debug_mp (mpz_t);
static void refmpz_mul (mpz_t, const mpz_t, const mpz_t);
void dump_abort (int, const char *, mpz_t, mpz_t, mpz_t, mpz_t);
#define FFT_MIN_BITSIZE 100000
char *extra_fft;
void
one (int i, mpz_t multiplicand, mpz_t multiplier)
{
mpz_t product, ref_product;
mpz_init (product);
mpz_init (ref_product);
/* Test plain multiplication comparing results against reference code. */
mpz_mul (product, multiplier, multiplicand);
refmpz_mul (ref_product, multiplier, multiplicand);
if (mpz_cmp (product, ref_product))
dump_abort (i, "incorrect plain product",
multiplier, multiplicand, product, ref_product);
/* Test squaring, comparing results against plain multiplication */
mpz_mul (product, multiplier, multiplier);
mpz_set (multiplicand, multiplier);
mpz_mul (ref_product, multiplier, multiplicand);
if (mpz_cmp (product, ref_product))
dump_abort (i, "incorrect square product",
multiplier, multiplier, product, ref_product);
mpz_clear (product);
mpz_clear (ref_product);
}
int
main (int argc, char **argv)
{
mpz_t op1, op2;
int i;
int fft_max_2exp;
gmp_randstate_ptr rands;
mpz_t bs;
unsigned long bsi, size_range, fsize_range;
tests_start ();
rands = RANDS;
extra_fft = getenv ("GMP_CHECK_FFT");
fft_max_2exp = 0;
if (extra_fft != NULL)
{
fft_max_2exp = atoi (extra_fft);
printf ("GMP_CHECK_FFT=%d (include this in bug reports)\n", fft_max_2exp);
}
if (fft_max_2exp <= 1) /* compat with old use of GMP_CHECK_FFT */
fft_max_2exp = 22; /* default limit, good for any machine */
mpz_init (bs);
mpz_init (op1);
mpz_init (op2);
fsize_range = 4 << 8; /* a fraction 1/256 of size_range */
for (i = 0;; i++)
{
size_range = fsize_range >> 8;
fsize_range = fsize_range * 33 / 32;
if (size_range > fft_max_2exp)
break;
mpz_urandomb (bs, rands, size_range);
mpz_rrandomb (op1, rands, mpz_get_ui (bs));
if (i & 1)
mpz_urandomb (bs, rands, size_range);
mpz_rrandomb (op2, rands, mpz_get_ui (bs));
mpz_urandomb (bs, rands, 4);
bsi = mpz_get_ui (bs);
if ((bsi & 0x3) == 0)
mpz_neg (op1, op1);
if ((bsi & 0xC) == 0)
mpz_neg (op2, op2);
/* printf ("%d %d\n", SIZ (op1), SIZ (op2)); */
one (i, op2, op1);
}
for (i = -50; i < 0; i++)
{
mpz_urandomb (bs, rands, 32);
size_range = mpz_get_ui (bs) % fft_max_2exp;
mpz_urandomb (bs, rands, size_range);
mpz_rrandomb (op1, rands, mpz_get_ui (bs) + FFT_MIN_BITSIZE);
mpz_urandomb (bs, rands, size_range);
mpz_rrandomb (op2, rands, mpz_get_ui (bs) + FFT_MIN_BITSIZE);
/* printf ("%d: %d %d\n", i, SIZ (op1), SIZ (op2)); */
fflush (stdout);
one (-1, op2, op1);
}
mpz_clear (bs);
mpz_clear (op1);
mpz_clear (op2);
tests_end ();
exit (0);
}
static void
refmpz_mul (mpz_t w, const mpz_t u, const mpz_t v)
{
mp_size_t usize = u->_mp_size;
mp_size_t vsize = v->_mp_size;
mp_size_t wsize;
mp_size_t sign_product;
mp_ptr up, vp;
mp_ptr wp;
mp_size_t talloc;
sign_product = usize ^ vsize;
usize = ABS (usize);
vsize = ABS (vsize);
if (usize == 0 || vsize == 0)
{
SIZ (w) = 0;
return;
}
talloc = usize + vsize;
up = u->_mp_d;
vp = v->_mp_d;
wp = __GMP_ALLOCATE_FUNC_LIMBS (talloc);
if (usize > vsize)
refmpn_mul (wp, up, usize, vp, vsize);
else
refmpn_mul (wp, vp, vsize, up, usize);
wsize = usize + vsize;
wsize -= wp[wsize - 1] == 0;
MPZ_REALLOC (w, wsize);
MPN_COPY (PTR(w), wp, wsize);
SIZ(w) = sign_product < 0 ? -wsize : wsize;
__GMP_FREE_FUNC_LIMBS (wp, talloc);
}
void
dump_abort (int i, const char *s,
mpz_t op1, mpz_t op2, mpz_t product, mpz_t ref_product)
{
mp_size_t b, e;
fprintf (stderr, "ERROR: %s in test %d\n", s, i);
fprintf (stderr, "op1 = "); debug_mp (op1);
fprintf (stderr, "op2 = "); debug_mp (op2);
fprintf (stderr, " product = "); debug_mp (product);
fprintf (stderr, "ref_product = "); debug_mp (ref_product);
for (b = 0; b < ABSIZ(ref_product); b++)
if (PTR(ref_product)[b] != PTR(product)[b])
break;
for (e = ABSIZ(ref_product) - 1; e >= 0; e--)
if (PTR(ref_product)[e] != PTR(product)[e])
break;
printf ("ERRORS in %ld--%ld\n", b, e);
abort();
}
void
debug_mp (mpz_t x)
{
size_t siz = mpz_sizeinbase (x, 16);
if (siz > 65)
{
mpz_t q;
mpz_init (q);
mpz_tdiv_q_2exp (q, x, 4 * (mpz_sizeinbase (x, 16) - 25));
gmp_fprintf (stderr, "%ZX...", q);
mpz_tdiv_r_2exp (q, x, 4 * 25);
gmp_fprintf (stderr, "%025ZX [%d]\n", q, (int) siz);
mpz_clear (q);
}
else
{
gmp_fprintf (stderr, "%ZX\n", x);
}
}
|