1 /* test mpz_congruent_2exp_p */
4 Copyright 2001 Free 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 check_one (mpz_srcptr a, mpz_srcptr c, unsigned long d, int want)
35 for (swap = 0; swap <= 1; swap++)
37 got = (mpz_congruent_2exp_p (a, c, d) != 0);
44 mpz_set_ui (d2exp, 1L);
45 mpz_mul_2exp (d2exp, d2exp, d);
47 printf ("mpz_congruent_2exp_p wrong\n");
48 printf (" expected %d got %d\n", want, got);
51 mpz_trace (" a-c", diff);
52 mpz_trace (" 2^d", d2exp);
53 printf (" d=%lu\n", d);
58 mpz_trace (" a-c", diff);
59 mpz_trace (" 2^d", d2exp);
60 printf (" d=0x%lX\n", d);
64 MPZ_SRCPTR_SWAP (a, c);
80 /* anything is congruent mod 1 */
84 { "123", "456", 0, 1 },
85 { "0x123456789123456789", "0x987654321987654321", 0, 1 },
95 for (i = 0; i < numberof (data); i++)
97 mpz_set_str_or_abort (a, data[i].a, 0);
98 mpz_set_str_or_abort (c, data[i].c, 0);
99 check_one (a, c, data[i].d, data[i].want);
108 check_random (int argc, char *argv[])
110 gmp_randstate_ptr rands = RANDS;
118 reps = atoi (argv[1]);
125 for (i = 0; i < reps; i++)
127 mpz_errandomb (a, rands, 8*GMP_LIMB_BITS);
128 mpz_errandomb (c, rands, 8*GMP_LIMB_BITS);
129 d = urandom() % (8*GMP_LIMB_BITS);
131 mpz_mul_2exp (a, a, urandom() % (2*GMP_LIMB_BITS));
132 mpz_mul_2exp (c, c, urandom() % (2*GMP_LIMB_BITS));
134 mpz_negrandom (a, rands);
135 mpz_negrandom (c, rands);
137 mpz_fdiv_r_2exp (ra, a, d);
138 mpz_fdiv_r_2exp (rc, c, d);
140 want = (mpz_cmp (ra, rc) == 0);
141 check_one (a, c, d, want);
143 mpz_sub (ra, ra, rc);
145 check_one (a, c, d, 1);
156 main (int argc, char *argv[])
161 check_random (argc, argv);