3 Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2009, 2012, 2013 Free
4 Software Foundation, Inc.
6 This file is part of the GNU MP Library test suite.
8 The GNU MP Library test suite is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 3 of the License,
11 or (at your option) any later version.
13 The GNU MP Library test suite is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along with
19 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
28 void debug_mp (mpz_t);
29 unsigned long int mpz_refremove (mpz_t, const mpz_t, const mpz_t);
32 main (int argc, char **argv)
34 unsigned long int exp;
35 mpz_t t, dest, refdest, dividend, divisor;
36 mp_size_t dividend_size, divisor_size;
39 unsigned long int pwr, refpwr;
40 gmp_randstate_ptr rands;
42 unsigned long size_range;
48 reps = atoi (argv[1]);
50 mpz_inits (bs, t, dest, refdest, dividend, divisor, NULL);
52 for (i = 0; i < reps; i++)
54 mpz_urandomb (bs, rands, 32);
55 size_range = mpz_get_ui (bs) % 18 + 1; /* 1..524288 bit operands */
59 mpz_urandomb (bs, rands, size_range);
60 divisor_size = mpz_get_ui (bs);
61 mpz_rrandomb (divisor, rands, divisor_size);
63 while (mpz_sgn (divisor) == 0);
65 mpz_urandomb (bs, rands, size_range);
66 dividend_size = mpz_get_ui (bs) + divisor_size;
67 mpz_rrandomb (dividend, rands, dividend_size);
69 mpz_urandomb (bs, rands, 32);
70 exp = mpz_get_ui (bs) % (5 + 10000 / mpz_sizeinbase (divisor, 2));
71 if (mpz_get_ui (bs) & 2)
72 mpz_neg (divisor, divisor);
73 mpz_pow_ui (t, divisor, exp);
74 mpz_mul (dividend, dividend, t);
76 refpwr = mpz_refremove (refdest, dividend, divisor);
77 pwr = mpz_remove (dest, dividend, divisor);
79 if (refpwr != pwr || mpz_cmp (refdest, dest) != 0)
81 fprintf (stderr, "ERROR after %d tests\n", i);
82 fprintf (stderr, "refpower = %lu\n", refpwr);
83 fprintf (stderr, " power = %lu\n", pwr);
84 fprintf (stderr, " op1 = "); debug_mp (dividend);
85 fprintf (stderr, " op2 = "); debug_mp (divisor);
86 fprintf (stderr, "refdest = "); debug_mp (refdest);
87 fprintf (stderr, " dest = "); debug_mp (dest);
92 mpz_clears (bs, t, dest, refdest, dividend, divisor, NULL);
99 mpz_refremove (mpz_t dest, const mpz_t src, const mpz_t f)
101 unsigned long int pwr;
106 if (mpz_cmpabs_ui (f, 1) > 0)
115 mpz_tdiv_qr (x, rem, dest, f);
116 if (mpz_cmp_ui (rem, 0) != 0)
131 size_t siz = mpz_sizeinbase (x, 16);
137 mpz_tdiv_q_2exp (q, x, 4 * (mpz_sizeinbase (x, 16) - 25));
138 gmp_fprintf (stderr, "%ZX...", q);
139 mpz_tdiv_r_2exp (q, x, 4 * 25);
140 gmp_fprintf (stderr, "%025ZX [%d]\n", q, (int) siz);
145 gmp_fprintf (stderr, "%ZX\n", x);