blob: 66e021eaca87a29a970020bef21a68f725b595a3 [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* Test mpz_cmp, mpz_mul.
2
3Copyright 1991, 1993, 1994, 1996, 1997, 2000-2004 Free Software Foundation,
4Inc.
5
6This file is part of the GNU MP Library test suite.
7
8The GNU MP Library test suite is free software; you can redistribute it
9and/or modify it under the terms of the GNU General Public License as
10published by the Free Software Foundation; either version 3 of the License,
11or (at your option) any later version.
12
13The GNU MP Library test suite is distributed in the hope that it will be
14useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16Public License for more details.
17
18You should have received a copy of the GNU General Public License along with
19the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
20
21#include <stdio.h>
22#include <stdlib.h>
23
24#include "gmp-impl.h"
25#include "longlong.h"
26#include "tests.h"
27
28void debug_mp (mpz_t);
29static void refmpz_mul (mpz_t, const mpz_t, const mpz_t);
30void dump_abort (int, const char *, mpz_t, mpz_t, mpz_t, mpz_t);
31
32#define FFT_MIN_BITSIZE 100000
33
34char *extra_fft;
35
36void
37one (int i, mpz_t multiplicand, mpz_t multiplier)
38{
39 mpz_t product, ref_product;
40
41 mpz_init (product);
42 mpz_init (ref_product);
43
44 /* Test plain multiplication comparing results against reference code. */
45 mpz_mul (product, multiplier, multiplicand);
46 refmpz_mul (ref_product, multiplier, multiplicand);
47 if (mpz_cmp (product, ref_product))
48 dump_abort (i, "incorrect plain product",
49 multiplier, multiplicand, product, ref_product);
50
51 /* Test squaring, comparing results against plain multiplication */
52 mpz_mul (product, multiplier, multiplier);
53 mpz_set (multiplicand, multiplier);
54 mpz_mul (ref_product, multiplier, multiplicand);
55 if (mpz_cmp (product, ref_product))
56 dump_abort (i, "incorrect square product",
57 multiplier, multiplier, product, ref_product);
58
59 mpz_clear (product);
60 mpz_clear (ref_product);
61}
62
63int
64main (int argc, char **argv)
65{
66 mpz_t op1, op2;
67 int i;
68 int fft_max_2exp;
69
70 gmp_randstate_ptr rands;
71 mpz_t bs;
72 unsigned long bsi, size_range, fsize_range;
73
74 tests_start ();
75 rands = RANDS;
76
77 extra_fft = getenv ("GMP_CHECK_FFT");
78 fft_max_2exp = 0;
79 if (extra_fft != NULL)
80 fft_max_2exp = atoi (extra_fft);
81
82 if (fft_max_2exp <= 1) /* compat with old use of GMP_CHECK_FFT */
83 fft_max_2exp = 22; /* default limit, good for any machine */
84
85 mpz_init (bs);
86 mpz_init (op1);
87 mpz_init (op2);
88
89 fsize_range = 4 << 8; /* a fraction 1/256 of size_range */
90 for (i = 0;; i++)
91 {
92 size_range = fsize_range >> 8;
93 fsize_range = fsize_range * 33 / 32;
94
95 if (size_range > fft_max_2exp)
96 break;
97
98 mpz_urandomb (bs, rands, size_range);
99 mpz_rrandomb (op1, rands, mpz_get_ui (bs));
100 if (i & 1)
101 mpz_urandomb (bs, rands, size_range);
102 mpz_rrandomb (op2, rands, mpz_get_ui (bs));
103
104 mpz_urandomb (bs, rands, 4);
105 bsi = mpz_get_ui (bs);
106 if ((bsi & 0x3) == 0)
107 mpz_neg (op1, op1);
108 if ((bsi & 0xC) == 0)
109 mpz_neg (op2, op2);
110
111 /* printf ("%d %d\n", SIZ (op1), SIZ (op2)); */
112 one (i, op2, op1);
113 }
114
115 for (i = -50; i < 0; i++)
116 {
117 mpz_urandomb (bs, rands, 32);
118 size_range = mpz_get_ui (bs) % fft_max_2exp;
119
120 mpz_urandomb (bs, rands, size_range);
121 mpz_rrandomb (op1, rands, mpz_get_ui (bs) + FFT_MIN_BITSIZE);
122 mpz_urandomb (bs, rands, size_range);
123 mpz_rrandomb (op2, rands, mpz_get_ui (bs) + FFT_MIN_BITSIZE);
124
125 /* printf ("%d: %d %d\n", i, SIZ (op1), SIZ (op2)); */
126 fflush (stdout);
127 one (-1, op2, op1);
128 }
129
130 mpz_clear (bs);
131 mpz_clear (op1);
132 mpz_clear (op2);
133
134 tests_end ();
135 exit (0);
136}
137
138static void
139refmpz_mul (mpz_t w, const mpz_t u, const mpz_t v)
140{
141 mp_size_t usize = u->_mp_size;
142 mp_size_t vsize = v->_mp_size;
143 mp_size_t wsize;
144 mp_size_t sign_product;
145 mp_ptr up, vp;
146 mp_ptr wp;
147 mp_size_t talloc;
148
149 sign_product = usize ^ vsize;
150 usize = ABS (usize);
151 vsize = ABS (vsize);
152
153 if (usize == 0 || vsize == 0)
154 {
155 SIZ (w) = 0;
156 return;
157 }
158
159 talloc = usize + vsize;
160
161 up = u->_mp_d;
162 vp = v->_mp_d;
163
164 wp = __GMP_ALLOCATE_FUNC_LIMBS (talloc);
165
166 if (usize > vsize)
167 refmpn_mul (wp, up, usize, vp, vsize);
168 else
169 refmpn_mul (wp, vp, vsize, up, usize);
170 wsize = usize + vsize;
171 wsize -= wp[wsize - 1] == 0;
172 MPZ_REALLOC (w, wsize);
173 MPN_COPY (PTR(w), wp, wsize);
174
175 SIZ(w) = sign_product < 0 ? -wsize : wsize;
176 __GMP_FREE_FUNC_LIMBS (wp, talloc);
177}
178
179void
180dump_abort (int i, const char *s,
181 mpz_t op1, mpz_t op2, mpz_t product, mpz_t ref_product)
182{
183 mp_size_t b, e;
184 fprintf (stderr, "ERROR: %s in test %d\n", s, i);
185 fprintf (stderr, "op1 = "); debug_mp (op1);
186 fprintf (stderr, "op2 = "); debug_mp (op2);
187 fprintf (stderr, " product = "); debug_mp (product);
188 fprintf (stderr, "ref_product = "); debug_mp (ref_product);
189 for (b = 0; b < ABSIZ(ref_product); b++)
190 if (PTR(ref_product)[b] != PTR(product)[b])
191 break;
192 for (e = ABSIZ(ref_product) - 1; e >= 0; e--)
193 if (PTR(ref_product)[e] != PTR(product)[e])
194 break;
195 printf ("ERRORS in %ld--%ld\n", b, e);
196 abort();
197}
198
199void
200debug_mp (mpz_t x)
201{
202 size_t siz = mpz_sizeinbase (x, 16);
203
204 if (siz > 65)
205 {
206 mpz_t q;
207 mpz_init (q);
208 mpz_tdiv_q_2exp (q, x, 4 * (mpz_sizeinbase (x, 16) - 25));
209 gmp_fprintf (stderr, "%ZX...", q);
210 mpz_tdiv_r_2exp (q, x, 4 * 25);
211 gmp_fprintf (stderr, "%025ZX [%d]\n", q, (int) siz);
212 mpz_clear (q);
213 }
214 else
215 {
216 gmp_fprintf (stderr, "%ZX\n", x);
217 }
218}