1 /* Test mpz_cmp, mpz_mul.
3 Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2003, 2004 Free
4 Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
29 void debug_mp __GMP_PROTO ((mpz_t));
30 static void refmpz_mul __GMP_PROTO ((mpz_t, const mpz_t, const mpz_t));
31 void dump_abort __GMP_PROTO ((int, char *, mpz_t, mpz_t, mpz_t, mpz_t));
33 #define FFT_MIN_BITSIZE 100000
38 one (int i, mpz_t multiplicand, mpz_t multiplier)
40 mpz_t product, ref_product;
43 mpz_init (ref_product);
45 /* Test plain multiplication comparing results against reference code. */
46 mpz_mul (product, multiplier, multiplicand);
47 refmpz_mul (ref_product, multiplier, multiplicand);
48 if (mpz_cmp (product, ref_product))
49 dump_abort (i, "incorrect plain product",
50 multiplier, multiplicand, product, ref_product);
52 /* Test squaring, comparing results against plain multiplication */
53 mpz_mul (product, multiplier, multiplier);
54 mpz_set (multiplicand, multiplier);
55 mpz_mul (ref_product, multiplier, multiplicand);
56 if (mpz_cmp (product, ref_product))
57 dump_abort (i, "incorrect square product",
58 multiplier, multiplier, product, ref_product);
61 mpz_clear (ref_product);
65 main (int argc, char **argv)
71 gmp_randstate_ptr rands;
73 unsigned long bsi, size_range, fsize_range;
78 extra_fft = getenv ("GMP_CHECK_FFT");
80 if (extra_fft != NULL)
81 fft_max_2exp = atoi (extra_fft);
83 if (fft_max_2exp <= 1) /* compat with old use of GMP_CHECK_FFT */
84 fft_max_2exp = 22; /* default limit, good for any machine */
90 fsize_range = 4 << 8; /* a fraction 1/256 of size_range */
93 size_range = fsize_range >> 8;
94 fsize_range = fsize_range * 33 / 32;
96 if (size_range > fft_max_2exp)
99 mpz_urandomb (bs, rands, size_range);
100 mpz_rrandomb (op1, rands, mpz_get_ui (bs));
102 mpz_urandomb (bs, rands, size_range);
103 mpz_rrandomb (op2, rands, mpz_get_ui (bs));
105 mpz_urandomb (bs, rands, 4);
106 bsi = mpz_get_ui (bs);
107 if ((bsi & 0x3) == 0)
109 if ((bsi & 0xC) == 0)
112 /* printf ("%d %d\n", SIZ (op1), SIZ (op2)); */
116 for (i = -50; i < 0; i++)
118 mpz_urandomb (bs, rands, 32);
119 size_range = mpz_get_ui (bs) % fft_max_2exp;
121 mpz_urandomb (bs, rands, size_range);
122 mpz_rrandomb (op1, rands, mpz_get_ui (bs) + FFT_MIN_BITSIZE);
123 mpz_urandomb (bs, rands, size_range);
124 mpz_rrandomb (op2, rands, mpz_get_ui (bs) + FFT_MIN_BITSIZE);
126 /* printf ("%d: %d %d\n", i, SIZ (op1), SIZ (op2)); */
140 refmpz_mul (mpz_t w, const mpz_t u, const mpz_t v)
142 mp_size_t usize = u->_mp_size;
143 mp_size_t vsize = v->_mp_size;
145 mp_size_t sign_product;
150 sign_product = usize ^ vsize;
154 if (usize == 0 || vsize == 0)
160 talloc = usize + vsize;
165 wp = __GMP_ALLOCATE_FUNC_LIMBS (talloc);
168 refmpn_mul (wp, up, usize, vp, vsize);
170 refmpn_mul (wp, vp, vsize, up, usize);
171 wsize = usize + vsize;
172 wsize -= wp[wsize - 1] == 0;
173 MPZ_REALLOC (w, wsize);
174 MPN_COPY (PTR(w), wp, wsize);
176 SIZ(w) = sign_product < 0 ? -wsize : wsize;
177 __GMP_FREE_FUNC_LIMBS (wp, talloc);
181 dump_abort (int i, char *s,
182 mpz_t op1, mpz_t op2, mpz_t product, mpz_t ref_product)
185 fprintf (stderr, "ERROR: %s in test %d\n", s, i);
186 fprintf (stderr, "op1 = "); debug_mp (op1);
187 fprintf (stderr, "op2 = "); debug_mp (op2);
188 fprintf (stderr, " product = "); debug_mp (product);
189 fprintf (stderr, "ref_product = "); debug_mp (ref_product);
190 for (b = 0; b < ABSIZ(ref_product); b++)
191 if (PTR(ref_product)[b] != PTR(product)[b])
193 for (e = ABSIZ(ref_product) - 1; e >= 0; e--)
194 if (PTR(ref_product)[e] != PTR(product)[e])
196 printf ("ERRORS in %ld--%ld\n", b, e);
203 size_t siz = mpz_sizeinbase (x, 16);
209 mpz_tdiv_q_2exp (q, x, 4 * (mpz_sizeinbase (x, 16) - 25));
210 gmp_fprintf (stderr, "%ZX...", q);
211 mpz_tdiv_r_2exp (q, x, 4 * 25);
212 gmp_fprintf (stderr, "%025ZX [%d]\n", q, (int) siz);
217 gmp_fprintf (stderr, "%ZX\n", x);