3 Copyright 2001, 2005 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/. */
35 for (i = 0; i < 5 * GMP_LIMB_BITS; i++)
38 got = mpz_popcount (n);
41 printf ("mpz_popcount wrong on single bit at %lu\n", i);
42 printf (" got %lu, want 1\n", got);
58 { "-1", ~ (unsigned long) 0 },
59 { "-12345678", ~ (unsigned long) 0 },
66 { "0xFFFFFFFFFFFFFFFF", 64 },
67 { "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128 },
75 for (i = 0; i < numberof (data); i++)
77 mpz_set_str_or_abort (n, data[i].n, 0);
78 got = mpz_popcount (n);
79 if (got != data[i].want)
81 printf ("mpz_popcount wrong at data[%d]\n", i);
82 printf (" n \"%s\"\n", data[i].n);
83 printf (" "); mpz_out_str (stdout, 10, n); printf ("\n");
84 printf (" 0x"); mpz_out_str (stdout, 16, n); printf ("\n");
85 printf (" got %lu\n", got);
86 printf (" want %lu\n", data[i].want);
94 refmpz_popcount (mpz_t arg)
102 return ~(unsigned long) 0;
105 for (i = 0; i < n; i++)
120 gmp_randstate_ptr rands;
123 unsigned long arg_size, size_range;
124 unsigned long got, ref;
132 for (i = 0; i < 10000; i++)
134 mpz_urandomb (bs, rands, 32);
135 size_range = mpz_get_ui (bs) % 11 + 2; /* 0..4096 bit operands */
137 mpz_urandomb (bs, rands, size_range);
138 arg_size = mpz_get_ui (bs);
139 mpz_rrandomb (arg, rands, arg_size);
141 got = mpz_popcount (arg);
142 ref = refmpz_popcount (arg);
145 printf ("mpz_popcount wrong on random\n");
146 printf (" "); mpz_out_str (stdout, 10, arg); printf ("\n");
147 printf (" 0x"); mpz_out_str (stdout, 16, arg); printf ("\n");
148 printf (" got %lu\n", got);
149 printf (" want %lu\n", ref);