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 2.1 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; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32
check_one (mpz_srcptr a, mpz_srcptr c, unsigned long d, int want)
38
for (swap = 0; swap <= 1; swap++)
40
got = (mpz_congruent_2exp_p (a, c, d) != 0);
47
mpz_set_ui (d2exp, 1L);
48
mpz_mul_2exp (d2exp, d2exp, d);
50
printf ("mpz_congruent_2exp_p wrong\n");
51
printf (" expected %d got %d\n", want, got);
54
mpz_trace (" a-c", diff);
55
mpz_trace (" 2^d", d2exp);
56
printf (" d=%lu\n", d);
61
mpz_trace (" a-c", diff);
62
mpz_trace (" 2^d", d2exp);
63
printf (" d=0x%lX\n", d);
67
MPZ_SRCPTR_SWAP (a, c);
83
/* anything is congruent mod 1 */
87
{ "123", "456", 0, 1 },
88
{ "0x123456789123456789", "0x987654321987654321", 0, 1 },
98
for (i = 0; i < numberof (data); i++)
100
mpz_set_str_or_abort (a, data[i].a, 0);
101
mpz_set_str_or_abort (c, data[i].c, 0);
102
check_one (a, c, data[i].d, data[i].want);
111
check_random (int argc, char *argv[])
113
gmp_randstate_ptr rands = RANDS;
121
reps = atoi (argv[1]);
128
for (i = 0; i < reps; i++)
130
mpz_errandomb (a, rands, 8*BITS_PER_MP_LIMB);
131
mpz_errandomb (c, rands, 8*BITS_PER_MP_LIMB);
132
d = urandom() % (8*BITS_PER_MP_LIMB);
134
mpz_mul_2exp (a, a, urandom() % (2*BITS_PER_MP_LIMB));
135
mpz_mul_2exp (c, c, urandom() % (2*BITS_PER_MP_LIMB));
137
mpz_negrandom (a, rands);
138
mpz_negrandom (c, rands);
140
mpz_fdiv_r_2exp (ra, a, d);
141
mpz_fdiv_r_2exp (rc, c, d);
143
want = (mpz_cmp (ra, rc) == 0);
144
check_one (a, c, d, want);
146
mpz_sub (ra, ra, rc);
148
check_one (a, c, d, 1);
159
main (int argc, char *argv[])
164
check_random (argc, argv);