From: Ben Pfaff Date: Wed, 25 Jul 2007 03:13:19 +0000 (+0000) Subject: Improve name: "count-one-bits" is better than "popcount". X-Git-Tag: cvs-readonly~96 X-Git-Url: https://erislabs.net/gitweb/?a=commitdiff_plain;h=e9cfd9f2d0d75d5d09d0acff6cb578a5c039c45c;p=gnulib.git Improve name: "count-one-bits" is better than "popcount". * MODULES.html.sh: Update name. * lib/popcount.h: Renamed lib/count-one-bits.h. (popcount): Renamed count_one_bits. (popcountl): Renamed count_one_bits_l. (popcountll): Renamed count_one_bits_ll. * m4/popcount.m4: Renamed m4/count-one-bits.m4. * modules/popcount: Renamed module/count-one-bits. * modules/popcount-tests: Renamed module/count-one-bits-tests. * tests/test-popcount.c: Renamed tests/test-count-one-bits.c. --- diff --git a/ChangeLog b/ChangeLog index 6c0445071..fa33df85b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2007-07-24 Ben Pfaff + + Improve name: "count-one-bits" is better than "popcount". + * MODULES.html.sh: Update name. + * lib/popcount.h: Renamed lib/count-one-bits.h. + (popcount): Renamed count_one_bits. + (popcountl): Renamed count_one_bits_l. + (popcountll): Renamed count_one_bits_ll. + * m4/popcount.m4: Renamed m4/count-one-bits.m4. + * modules/popcount: Renamed module/count-one-bits. + * modules/popcount-tests: Renamed module/count-one-bits-tests. + * tests/test-popcount.c: Renamed tests/test-count-one-bits.c. + 2007-07-23 Ben Pfaff * lib/popcount.h (popcount32): Reduce size of constants, to allow diff --git a/MODULES.html.sh b/MODULES.html.sh index a3a6adbd7..5fc995260 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1609,9 +1609,9 @@ func_all_modules () func_echo "$element" func_begin_table + func_module count-one-bits func_module gcd func_module minmax - func_module popcount func_end_table element="Environment variables " diff --git a/lib/count-one-bits.h b/lib/count-one-bits.h new file mode 100644 index 000000000..7553bec4e --- /dev/null +++ b/lib/count-one-bits.h @@ -0,0 +1,74 @@ +/* count-one-bits.h -- counts the number of 1-bits in a word. + Copyright (C) 2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Ben Pfaff. */ + +#ifndef COUNT_ONE_BITS_H +# define COUNT_ONE_BITS_H 1 + +#include +#include +#include "verify.h" + +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR >= 4) +#define COUNT_ONE_BITS(BUILTIN, TYPE) \ + return BUILTIN (x); +#else +#define COUNT_ONE_BITS(BUILTIN, TYPE) \ + int count = count_one_bits_32 (x); \ + if (CHAR_BIT * sizeof (TYPE) > 32) \ + count += count_one_bits_32 (x >> 31 >> 1); \ + (void) verify_true (CHAR_BIT * sizeof (TYPE) <= 64); \ + return count; + +/* Compute and return the the number of 1-bits set in the least + significant 32 bits of X. */ +static inline int +count_one_bits_32 (unsigned int x) +{ + x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U); + x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U); + x = (x >> 16) + (x & 0xffff); + x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f); + return (x >> 8) + (x & 0x00ff); +} +#endif + +/* Compute and return the number of 1-bits set in X. */ +static inline int +count_one_bits (unsigned int x) +{ + COUNT_ONE_BITS (__builtin_popcount, unsigned int); +} + +/* Compute and return the number of 1-bits set in X. */ +static inline int +count_one_bits_l (unsigned long int x) +{ + COUNT_ONE_BITS (__builtin_popcountl, unsigned long int); +} + +#if HAVE_UNSIGNED_LONG_LONG_INT +/* Compute and return the number of 1-bits set in X. */ +static inline int +count_one_bits_ll (unsigned long long int x) +{ + COUNT_ONE_BITS (__builtin_popcountll, unsigned long long int); +} +#endif + +#endif /* COUNT_ONE_BITS_H */ diff --git a/lib/popcount.h b/lib/popcount.h deleted file mode 100644 index 0ea6ed837..000000000 --- a/lib/popcount.h +++ /dev/null @@ -1,78 +0,0 @@ -/* popcount.h -- population count - Copyright (C) 2007 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -/* Written by Ben Pfaff. */ - -#ifndef POPCOUNT_H -# define POPCOUNT_H 1 - -#include -#include -#include "verify.h" - -#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR >= 4) -#define POPCOUNT_CALCULATION(NAME, TYPE) \ - return __builtin_##NAME (x); -#else -#define POPCOUNT_CALCULATION(NAME, TYPE) \ - int pop = popcount32 (x); \ - if (CHAR_BIT * sizeof (TYPE) > 32) \ - pop += popcount32 (x >> 31 >> 1); \ - (void) verify_true (CHAR_BIT * sizeof (TYPE) <= 64); \ - return pop; - -/* Compute and return the population count of the low 32 bits of - X, that is, the number of 1-bits set in its least significant - 32 bits. */ -static inline int -popcount32 (unsigned int x) -{ - x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U); - x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U); - x = (x >> 16) + (x & 0xffff); - x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f); - return (x >> 8) + (x & 0x00ff); -} -#endif - -/* Compute and return the population count of X, that is, the - number of 1-bits set in X. */ -static inline int -popcount (unsigned int x) -{ - POPCOUNT_CALCULATION (popcount, unsigned int); -} - -/* Compute and return the population count of X, that is, the - number of 1-bits set in X. */ -static inline int -popcountl (unsigned long int x) -{ - POPCOUNT_CALCULATION (popcountl, unsigned long int); -} - -#if HAVE_UNSIGNED_LONG_LONG_INT -/* Compute and return the population count of X, that is, the - number of 1-bits set in X. */ -static inline int -popcountll (unsigned long long int x) -{ - POPCOUNT_CALCULATION (popcountll, unsigned long long int); -} -#endif - -#endif /* POPCOUNT_H */ diff --git a/m4/count-one-bits.m4 b/m4/count-one-bits.m4 new file mode 100644 index 000000000..8d1410a57 --- /dev/null +++ b/m4/count-one-bits.m4 @@ -0,0 +1,12 @@ +# count-one-bits.m4 serial 1 +dnl Copyright (C) 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_COUNT_ONE_BITS], +[ + dnl We don't need (and can't compile) count_one_bits_ll + dnl unless the type 'unsigned long long int' exists. + AC_REQUIRE([AC_TYPE_UNSIGNED_LONG_LONG_INT]) +]) diff --git a/m4/popcount.m4 b/m4/popcount.m4 deleted file mode 100644 index 5cec20c3f..000000000 --- a/m4/popcount.m4 +++ /dev/null @@ -1,12 +0,0 @@ -# popcount.m4 serial 1 -dnl Copyright (C) 2007 Free Software Foundation, Inc. -dnl This file is free software; the Free Software Foundation -dnl gives unlimited permission to copy and/or distribute it, -dnl with or without modifications, as long as this notice is preserved. - -AC_DEFUN([gl_POPCOUNT], -[ - dnl We don't need (and can't compile) popcountll - dnl unless the type 'unsigned long long int' exists. - AC_REQUIRE([AC_TYPE_UNSIGNED_LONG_LONG_INT]) -]) diff --git a/modules/count-one-bits b/modules/count-one-bits new file mode 100644 index 000000000..84160d3d2 --- /dev/null +++ b/modules/count-one-bits @@ -0,0 +1,24 @@ +Description: +Counts the number of 1-bits in a word. + +Files: +lib/count-one-bits.h +m4/count-one-bits.m4 + +Depends-on: +inline +verify + +configure.ac: +gl_COUNT_ONE_BITS + +Makefile.am: + +Include: +"count-one-bits.h" + +License: +GPL + +Maintainer: +Ben Pfaff diff --git a/modules/count-one-bits-tests b/modules/count-one-bits-tests new file mode 100644 index 000000000..c8241b357 --- /dev/null +++ b/modules/count-one-bits-tests @@ -0,0 +1,10 @@ +Files: +tests/test-count-one-bits.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-count-one-bits +check_PROGRAMS += test-count-one-bits diff --git a/modules/popcount b/modules/popcount deleted file mode 100644 index 45fd56b70..000000000 --- a/modules/popcount +++ /dev/null @@ -1,24 +0,0 @@ -Description: -Compute population count (number of 1-bits in a word). - -Files: -lib/popcount.h -m4/popcount.m4 - -Depends-on: -inline -verify - -configure.ac: -gl_POPCOUNT - -Makefile.am: - -Include: -"popcount.h" - -License: -GPL - -Maintainer: -Ben Pfaff diff --git a/modules/popcount-tests b/modules/popcount-tests deleted file mode 100644 index 7579ee797..000000000 --- a/modules/popcount-tests +++ /dev/null @@ -1,10 +0,0 @@ -Files: -tests/test-popcount.c - -Depends-on: - -configure.ac: - -Makefile.am: -TESTS += test-popcount -check_PROGRAMS += test-popcount diff --git a/tests/test-count-one-bits.c b/tests/test-count-one-bits.c new file mode 100644 index 000000000..2375d7bea --- /dev/null +++ b/tests/test-count-one-bits.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2007 Free Software Foundation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. */ + +/* Written by Ben Pfaff. */ + +#include + +#include "count-one-bits.h" + +#include +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion \"%s\" failed\n", \ + __FILE__, __LINE__, #expr); \ + abort (); \ + } \ + } \ + while (0) + +#define UINT_BIT (sizeof (unsigned int) * CHAR_BIT) +#define ULONG_BIT (sizeof (unsigned long int) * CHAR_BIT) +#define ULLONG_BIT (sizeof (unsigned long long int) * CHAR_BIT) + +#ifndef ULLONG_MAX +# define HALF (1ULL << (sizeof (unsigned long long int) * CHAR_BIT - 1)) +# define ULLONG_MAX (HALF - 1 + HALF) +#endif + +int +main (int argc, char *argv[]) +{ + int i, j; + +#define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \ + ASSERT (FUNC (0) == 0); \ + for (i = 0; i < BITS; i++) \ + { \ + ASSERT (FUNC (ONE << i) == 1); \ + for (j = i + 1; j < BITS; j++) \ + ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \ + } \ + for (i = 0; i < 1000; i++) \ + { \ + TYPE value = rand () ^ (rand () << 31 << 1); \ + int count = 0; \ + for (j = 0; j < BITS; j++) \ + count += (value & (ONE << j)) != 0; \ + ASSERT (count == FUNC (value)); \ + } \ + ASSERT (FUNC (MAX) == BITS); + + TEST_COUNT_ONE_BITS (count_one_bits, unsigned int, UINT_BIT, UINT_MAX, 1U); + TEST_COUNT_ONE_BITS (count_one_bits_l, unsigned long int, + ULONG_BIT, ULONG_MAX, 1UL); +#ifdef HAVE_UNSIGNED_LONG_LONG_INT + TEST_COUNT_ONE_BITS (count_one_bits_ll, + unsigned long long int, ULLONG_BIT, ULLONG_MAX, 1ULL); +#endif + + return 0; +} diff --git a/tests/test-popcount.c b/tests/test-popcount.c deleted file mode 100644 index 495b5748d..000000000 --- a/tests/test-popcount.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2007 Free Software Foundation - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. */ - -/* Written by Ben Pfaff. */ - -#include - -#include "popcount.h" - -#include -#include -#include - -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (stderr, "%s:%d: assertion \"%s\" failed\n", \ - __FILE__, __LINE__, #expr); \ - abort (); \ - } \ - } \ - while (0) - -#define UINT_BIT (sizeof (unsigned int) * CHAR_BIT) -#define ULONG_BIT (sizeof (unsigned long int) * CHAR_BIT) -#define ULLONG_BIT (sizeof (unsigned long long int) * CHAR_BIT) - -#ifndef ULLONG_MAX -# define HALF (1ULL << (sizeof (unsigned long long int) * CHAR_BIT - 1)) -# define ULLONG_MAX (HALF - 1 + HALF) -#endif - -int -main (int argc, char *argv[]) -{ - int i, j; - -#define TEST_POPCOUNT(FUNC, TYPE, BITS, MAX, ONE) \ - ASSERT (FUNC (0) == 0); \ - for (i = 0; i < BITS; i++) \ - { \ - ASSERT (FUNC (ONE << i) == 1); \ - for (j = i + 1; j < BITS; j++) \ - ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \ - } \ - for (i = 0; i < 1000; i++) \ - { \ - TYPE value = rand () ^ (rand () << 31 << 1); \ - int count = 0; \ - for (j = 0; j < BITS; j++) \ - count += (value & (ONE << j)) != 0; \ - ASSERT (count == FUNC (value)); \ - } \ - ASSERT (FUNC (MAX) == BITS); - - TEST_POPCOUNT (popcount, unsigned int, UINT_BIT, UINT_MAX, 1U); - TEST_POPCOUNT (popcountl, unsigned long int, ULONG_BIT, ULONG_MAX, 1UL); -#ifdef HAVE_UNSIGNED_LONG_LONG_INT - TEST_POPCOUNT (popcountll, - unsigned long long int, ULLONG_BIT, ULLONG_MAX, 1ULL); -#endif - - return 0; -}