1 /* Test mpz_setbit, mpz_clrbit, mpz_tstbit.
3 Copyright 1997, 2000, 2001, 2002, 2003 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 3 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. If not, see http://www.gnu.org/licenses/. */
33 debug_mp (mpz_srcptr x, int base)
35 mpz_out_str (stdout, base, x); fputc ('\n', stdout);
39 /* exercise the case where mpz_clrbit or mpz_combit ends up extending a
40 value like -2^(k*GMP_NUMB_BITS-1) when clearing bit k*GMP_NUMB_BITS-1. */
42 check_clr_extend (void)
51 for (i = 1; i < 5; i++)
53 for (f = 0; f <= 1; f++)
55 /* lots of 1 bits in _mp_d */
57 mpz_mul_2exp (got, got, 10*GMP_NUMB_BITS);
58 mpz_sub_ui (got, got, 1L);
60 /* value -2^(n-1) representing ..11100..00 */
61 mpz_set_si (got, -1L);
62 mpz_mul_2exp (got, got, i*GMP_NUMB_BITS-1);
64 /* complement bit n, giving ..11000..00 which is -2^n */
66 mpz_clrbit (got, i*GMP_NUMB_BITS-1);
68 mpz_combit (got, i*GMP_NUMB_BITS-1);
69 MPZ_CHECK_FORMAT (got);
71 mpz_set_si (want, -1L);
72 mpz_mul_2exp (want, want, i*GMP_NUMB_BITS);
74 if (mpz_cmp (got, want) != 0)
77 printf ("mpz_clrbit: ");
79 printf ("mpz_combit: ");
80 printf ("wrong after extension\n");
81 mpz_trace ("got ", got);
82 mpz_trace ("want", want);
102 { GMP_NUMB_BITS, 2, { 1, 1 }, 1, { 1 } },
103 { GMP_NUMB_BITS+1, 2, { 1, 1 }, 2, { 1, 3 } },
105 { GMP_NUMB_BITS, 2, { 0, 1 }, 2, { 0, 2 } },
106 { GMP_NUMB_BITS+1, 2, { 0, 1 }, 2, { 0, 3 } },
108 mpz_t inp, got, want;
115 for (i = 0; i < numberof (data); i++)
117 mpz_set_n (inp, data[i].inp_n, data[i].inp_size);
120 mpz_set_n (want, data[i].want_n, data[i].want_size);
121 mpz_neg (want, want);
124 mpz_combit (got, data[i].bit);
126 if (mpz_cmp (got, want) != 0)
128 printf ("mpz_combit: wrong on neg data[%d]\n", i);
129 mpz_trace ("inp ", inp);
130 printf ("bit %lu\n", data[i].bit);
131 mpz_trace ("got ", got);
132 mpz_trace ("want", want);
142 /* See that mpz_tstbit matches a twos complement calculated explicitly, for
143 various low zeros. */
150 mp_limb_t pos[1+NUM_LIMBS+MAX_ZEROS];
151 mp_limb_t neg[1+NUM_LIMBS+MAX_ZEROS];
158 for (zeros = 0; zeros <= MAX_ZEROS; zeros++)
160 MPN_ZERO (pos, numberof(pos));
161 mpn_random2 (pos+zeros, (mp_size_t) NUM_LIMBS);
163 for (low1 = 0; low1 <= 1; low1++)
168 refmpn_neg (neg, pos, (mp_size_t) numberof(neg));
169 mpz_set_n (z, neg, (mp_size_t) numberof(neg));
172 for (i = 0; i < numberof(pos)*GMP_NUMB_BITS; i++)
174 got = mpz_tstbit (z, i);
175 want = refmpn_tstbit (pos, i);
178 printf ("wrong at bit %lu, with %d zeros\n", i, zeros);
179 printf ("z neg "); debug_mp (z, -16);
180 mpz_set_n (z, pos, (mp_size_t) numberof(pos));
181 printf ("pos "); debug_mp (z, -16);
182 mpz_set_n (z, neg, (mp_size_t) numberof(neg));
183 printf ("neg "); debug_mp (z, -16);
197 int limb, offset, initial;
202 for (limb = 0; limb < 4; limb++)
204 for (offset = (limb==0 ? 0 : -2); offset <= 2; offset++)
206 for (initial = 0; initial >= -1; initial--)
208 mpz_set_si (x, (long) initial);
210 bit = (unsigned long) limb*GMP_LIMB_BITS + offset;
213 MPZ_CHECK_FORMAT (x);
214 if (mpz_tstbit (x, bit) != 0)
216 printf ("check_single(): expected 0\n");
221 MPZ_CHECK_FORMAT (x);
222 if (mpz_tstbit (x, bit) != 1)
224 printf ("check_single(): expected 1\n");
229 MPZ_CHECK_FORMAT (x);
230 if (mpz_tstbit (x, bit) != 0)
232 printf ("check_single(): expected 0\n");
237 MPZ_CHECK_FORMAT (x);
238 if (mpz_tstbit (x, bit) != 1)
240 printf ("check_single(): expected 1\n");
245 MPZ_CHECK_FORMAT (x);
246 if (mpz_tstbit (x, bit) != 0)
248 printf ("check_single(): expected 0\n");
260 check_random (int argc, char *argv[])
262 mpz_t x, s0, s1, s2, s3, m;
266 int bit0, bit1, bit2, bit3;
267 unsigned long int bitindex;
271 reps = atoi (argv[1]);
280 for (i = 0; i < reps; i++)
282 xsize = urandom () % (2 * SIZE) - SIZE;
283 mpz_random2 (x, xsize);
284 bitindex = urandom () % SIZE;
287 bit0 = mpz_tstbit (x, bitindex);
288 mpz_setbit (x, bitindex);
289 MPZ_CHECK_FORMAT (x);
292 bit1 = mpz_tstbit (x, bitindex);
293 mpz_clrbit (x, bitindex);
294 MPZ_CHECK_FORMAT (x);
297 bit2 = mpz_tstbit (x, bitindex);
298 mpz_setbit (x, bitindex);
299 MPZ_CHECK_FORMAT (x);
302 bit3 = mpz_tstbit (x, bitindex);
304 #define FAIL(str) do { s = str; goto fail; } while (0)
306 if (bit1 != 1) FAIL ("bit1 != 1");
307 if (bit2 != 0) FAIL ("bit2 != 0");
308 if (bit3 != 1) FAIL ("bit3 != 1");
312 if (mpz_cmp (s0, s1) == 0 || mpz_cmp (s0, s2) != 0 || mpz_cmp (s0, s3) == 0)
317 if (mpz_cmp (s0, s1) != 0 || mpz_cmp (s0, s2) == 0 || mpz_cmp (s0, s3) != 0)
321 if (mpz_cmp (s1, s2) == 0 || mpz_cmp (s1, s3) != 0)
323 if (mpz_cmp (s2, s3) == 0)
326 mpz_ui_pow_ui (m, 2L, bitindex);
327 MPZ_CHECK_FORMAT (m);
329 MPZ_CHECK_FORMAT (x);
330 if (mpz_cmp (x, s3) != 0)
334 MPZ_CHECK_FORMAT (m);
336 MPZ_CHECK_FORMAT (x);
337 if (mpz_cmp (x, s2) != 0)
352 printf ("bitindex = %lu\n", bitindex);
353 printf ("x = "); mpz_out_str (stdout, -16, x); printf (" hex\n");
360 main (int argc, char *argv[])
368 check_random (argc, argv);