[BACK]Return to popcount.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpn / generic

Annotation of OpenXM_contrib/gmp/mpn/generic/popcount.c, Revision 1.1.1.2

1.1       maekawa     1: /* popcount.c
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1994, 1996, 2000 Free Software Foundation, Inc.
1.1       maekawa     4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2 ! maekawa     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
1.1       maekawa    10: option) any later version.
                     11:
                     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
1.1.1.2 ! maekawa    14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! maekawa    17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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. */
                     21:
                     22: #include "gmp.h"
                     23: #include "gmp-impl.h"
                     24:
                     25: #if defined __GNUC__
1.1.1.2 ! maekawa    26: /* No processor claiming to be SPARC v9 compliant seem to
        !            27:    implement the POPC instruction.  Disable pattern for now.  */
        !            28: #if 0 && defined __sparc_v9__ && BITS_PER_MP_LIMB == 64
1.1       maekawa    29: #define popc_limb(a) \
                     30:   ({                                                                   \
                     31:     DItype __res;                                                      \
                     32:     asm ("popc %1,%0" : "=r" (__res) : "rI" (a));                      \
                     33:     __res;                                                             \
                     34:   })
                     35: #endif
                     36: #endif
                     37:
                     38: #ifndef popc_limb
                     39:
                     40: /* Cool population count of a mp_limb_t.
                     41:    You have to figure out how this works, I won't tell you!  */
                     42:
                     43: static inline unsigned int
1.1.1.2 ! maekawa    44: #if __STDC__
        !            45: popc_limb (mp_limb_t x)
        !            46: #else
1.1       maekawa    47: popc_limb (x)
                     48:      mp_limb_t x;
1.1.1.2 ! maekawa    49: #endif
1.1       maekawa    50: {
                     51: #if BITS_PER_MP_LIMB == 64
                     52:   /* We have to go into some trouble to define these constants.
                     53:      (For mp_limb_t being `long long'.)  */
                     54:   mp_limb_t cnst;
1.1.1.2 ! maekawa    55:   cnst = 0xaaaaaaaaL | ((mp_limb_t) 0xaaaaaaaaL << BITS_PER_MP_LIMB/2);
        !            56:   x -= (x & cnst) >> 1;
1.1       maekawa    57:   cnst = 0x33333333L | ((mp_limb_t) 0x33333333L << BITS_PER_MP_LIMB/2);
                     58:   x = ((x & ~cnst) >> 2) + (x & cnst);
                     59:   cnst = 0x0f0f0f0fL | ((mp_limb_t) 0x0f0f0f0fL << BITS_PER_MP_LIMB/2);
                     60:   x = ((x >> 4) + x) & cnst;
                     61:   x = ((x >> 8) + x);
                     62:   x = ((x >> 16) + x);
                     63:   x = ((x >> 32) + x) & 0xff;
                     64: #endif
                     65: #if BITS_PER_MP_LIMB == 32
1.1.1.2 ! maekawa    66:   x -= (x & 0xaaaaaaaa) >> 1;
1.1       maekawa    67:   x = ((x >> 2) & 0x33333333L) + (x & 0x33333333L);
                     68:   x = ((x >> 4) + x) & 0x0f0f0f0fL;
                     69:   x = ((x >> 8) + x);
                     70:   x = ((x >> 16) + x) & 0xff;
                     71: #endif
                     72:   return x;
                     73: }
                     74: #endif
                     75:
                     76: unsigned long int
                     77: #if __STDC__
                     78: mpn_popcount (register mp_srcptr p, register mp_size_t size)
                     79: #else
                     80: mpn_popcount (p, size)
                     81:      register mp_srcptr p;
                     82:      register mp_size_t size;
                     83: #endif
                     84: {
                     85:   unsigned long int popcnt;
                     86:   mp_size_t i;
                     87:
                     88:   popcnt = 0;
                     89:   for (i = 0; i < size; i++)
                     90:     popcnt += popc_limb (p[i]);
                     91:
                     92:   return popcnt;
                     93: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>