1 /* Test mpz_remove.
2
3 Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2009, 2012, 2013 Free
4 Software Foundation, Inc.
5
6 This file is part of the GNU MP Library test suite.
7
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.
12
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.
17
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/. */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "gmp-impl.h"
25 #include "tests.h"
26
27 void debug_mp (mpz_t);
28 unsigned long int mpz_refremove (mpz_t, const mpz_t, const mpz_t);
29
30 int
main(int argc,char ** argv)31 main (int argc, char **argv)
32 {
33 unsigned long int exp;
34 mpz_t t, dest, refdest, dividend, divisor;
35 mp_size_t dividend_size, divisor_size;
36 int i;
37 int reps = 1000;
38 unsigned long int pwr, refpwr;
39 gmp_randstate_ptr rands;
40 mpz_t bs;
41 unsigned long size_range;
42
43 tests_start ();
44 rands = RANDS;
45
46 if (argc == 2)
47 reps = atoi (argv[1]);
48
49 mpz_inits (bs, t, dest, refdest, dividend, divisor, NULL);
50
51 for (i = 0; i < reps; i++)
52 {
53 mpz_urandomb (bs, rands, 32);
54 size_range = mpz_get_ui (bs) % 18 + 1; /* 1..524288 bit operands */
55
56 do
57 {
58 mpz_urandomb (bs, rands, size_range);
59 divisor_size = mpz_get_ui (bs);
60 mpz_rrandomb (divisor, rands, divisor_size);
61 }
62 while (mpz_sgn (divisor) == 0);
63
64 mpz_urandomb (bs, rands, size_range);
65 dividend_size = mpz_get_ui (bs) + divisor_size;
66 mpz_rrandomb (dividend, rands, dividend_size);
67
68 mpz_urandomb (bs, rands, 32);
69 exp = mpz_get_ui (bs) % (5 + 10000 / mpz_sizeinbase (divisor, 2));
70 if (mpz_get_ui (bs) & 2)
71 mpz_neg (divisor, divisor);
72 mpz_pow_ui (t, divisor, exp);
73 mpz_mul (dividend, dividend, t);
74
75 refpwr = mpz_refremove (refdest, dividend, divisor);
76 pwr = mpz_remove (dest, dividend, divisor);
77
78 if (refpwr != pwr || mpz_cmp (refdest, dest) != 0)
79 {
80 fprintf (stderr, "ERROR after %d tests\n", i);
81 fprintf (stderr, "refpower = %lu\n", refpwr);
82 fprintf (stderr, " power = %lu\n", pwr);
83 fprintf (stderr, " op1 = "); debug_mp (dividend);
84 fprintf (stderr, " op2 = "); debug_mp (divisor);
85 fprintf (stderr, "refdest = "); debug_mp (refdest);
86 fprintf (stderr, " dest = "); debug_mp (dest);
87 abort ();
88 }
89 }
90
91 mpz_clears (bs, t, dest, refdest, dividend, divisor, NULL);
92
93 tests_end ();
94 exit (0);
95 }
96
97 unsigned long int
mpz_refremove(mpz_t dest,const mpz_t src,const mpz_t f)98 mpz_refremove (mpz_t dest, const mpz_t src, const mpz_t f)
99 {
100 unsigned long int pwr;
101
102 pwr = 0;
103
104 mpz_set (dest, src);
105 if (mpz_cmpabs_ui (f, 1) > 0)
106 {
107 mpz_t rem, x;
108
109 mpz_init (x);
110 mpz_init (rem);
111
112 for (;; pwr++)
113 {
114 mpz_tdiv_qr (x, rem, dest, f);
115 if (mpz_cmp_ui (rem, 0) != 0)
116 break;
117 mpz_swap (dest, x);
118 }
119
120 mpz_clear (x);
121 mpz_clear (rem);
122 }
123
124 return pwr;
125 }
126
127 void
debug_mp(mpz_t x)128 debug_mp (mpz_t x)
129 {
130 size_t siz = mpz_sizeinbase (x, 16);
131
132 if (siz > 65)
133 {
134 mpz_t q;
135 mpz_init (q);
136 mpz_tdiv_q_2exp (q, x, 4 * (mpz_sizeinbase (x, 16) - 25));
137 gmp_fprintf (stderr, "%ZX...", q);
138 mpz_tdiv_r_2exp (q, x, 4 * 25);
139 gmp_fprintf (stderr, "%025ZX [%d]\n", q, (int) siz);
140 mpz_clear (q);
141 }
142 else
143 {
144 gmp_fprintf (stderr, "%ZX\n", x);
145 }
146 }
147