1
/* Test mpz_setbit, mpz_clrbit, mpz_tstbit.
3
Copyright 1997, 2000, 2001, 2002 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
35
debug_mp (mpz_srcptr x, int base)
37
mpz_out_str (stdout, base, x); fputc ('\n', stdout);
41
/* See that mpz_tstbit matches a twos complement calculated explicitly, for
49
mp_limb_t pos[1+NUM_LIMBS+MAX_ZEROS];
50
mp_limb_t neg[1+NUM_LIMBS+MAX_ZEROS];
57
for (zeros = 0; zeros <= MAX_ZEROS; zeros++)
59
MPN_ZERO (pos, numberof(pos));
60
mpn_random2 (pos+zeros, NUM_LIMBS);
62
for (low1 = 0; low1 <= 1; low1++)
67
refmpn_neg_n (neg, pos, numberof(neg));
68
mpz_set_n (z, neg, numberof(neg));
71
for (i = 0; i < numberof(pos)*GMP_NUMB_BITS; i++)
73
got = mpz_tstbit (z, i);
74
want = refmpn_tstbit (pos, i);
77
printf ("wrong at bit %lu, with %d zeros\n", i, zeros);
78
printf ("z neg "); debug_mp (z, -16);
79
mpz_set_n (z, pos, numberof(pos));
80
printf ("pos "); debug_mp (z, -16);
81
mpz_set_n (z, neg, numberof(neg));
82
printf ("neg "); debug_mp (z, -16);
96
int limb, offset, initial;
101
for (limb = 0; limb < 4; limb++)
103
for (offset = (limb==0 ? 0 : -2); offset <= 2; offset++)
105
for (initial = 0; initial >= -1; initial--)
107
mpz_set_si (x, (long) initial);
109
bit = (unsigned long) limb*BITS_PER_MP_LIMB + offset;
112
MPZ_CHECK_FORMAT (x);
113
if (mpz_tstbit (x, bit) != 0)
115
printf ("check_single(): expected 0\n");
120
MPZ_CHECK_FORMAT (x);
121
if (mpz_tstbit (x, bit) != 1)
123
printf ("check_single(): expected 0\n");
128
MPZ_CHECK_FORMAT (x);
129
if (mpz_tstbit (x, bit) != 0)
131
printf ("check_single(): expected 0\n");
143
check_random (int argc, char *argv[])
145
mpz_t x, s0, s1, s2, s3, m;
149
int bit0, bit1, bit2, bit3;
150
unsigned long int bitindex;
154
reps = atoi (argv[1]);
163
for (i = 0; i < reps; i++)
165
xsize = urandom () % (2 * SIZE) - SIZE;
166
mpz_random2 (x, xsize);
167
bitindex = urandom () % SIZE;
170
bit0 = mpz_tstbit (x, bitindex);
171
mpz_setbit (x, bitindex);
172
MPZ_CHECK_FORMAT (x);
175
bit1 = mpz_tstbit (x, bitindex);
176
mpz_clrbit (x, bitindex);
177
MPZ_CHECK_FORMAT (x);
180
bit2 = mpz_tstbit (x, bitindex);
181
mpz_setbit (x, bitindex);
182
MPZ_CHECK_FORMAT (x);
185
bit3 = mpz_tstbit (x, bitindex);
187
#define FAIL(str) do { s = str; goto fail; } while (0)
189
if (bit1 != 1) FAIL ("bit1 != 1");
190
if (bit2 != 0) FAIL ("bit2 != 0");
191
if (bit3 != 1) FAIL ("bit3 != 1");
195
if (mpz_cmp (s0, s1) == 0 || mpz_cmp (s0, s2) != 0 || mpz_cmp (s0, s3) == 0)
200
if (mpz_cmp (s0, s1) != 0 || mpz_cmp (s0, s2) == 0 || mpz_cmp (s0, s3) != 0)
204
if (mpz_cmp (s1, s2) == 0 || mpz_cmp (s1, s3) != 0)
206
if (mpz_cmp (s2, s3) == 0)
209
mpz_ui_pow_ui (m, 2L, bitindex);
210
MPZ_CHECK_FORMAT (m);
212
MPZ_CHECK_FORMAT (x);
213
if (mpz_cmp (x, s3) != 0)
217
MPZ_CHECK_FORMAT (m);
219
MPZ_CHECK_FORMAT (x);
220
if (mpz_cmp (x, s2) != 0)
235
printf ("bitindex = %lu\n", bitindex);
236
printf ("x = "); mpz_out_str (stdout, -16, x); printf (" hex\n");
243
main (int argc, char *argv[])
248
check_random (argc, argv);