Annotation of OpenXM/src/kan96xx/gmp-2.0.2-ssh-2/mpn/generic/popcount.c, Revision 1.1
1.1 ! takayama 1: /* popcount.c
! 2:
! 3: Copyright (C) 1994, 1996 Free Software Foundation, Inc.
! 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
! 8: it under the terms of the GNU Library General Public License as published by
! 9: the Free Software Foundation; either version 2 of the License, or (at your
! 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
! 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
! 15: License for more details.
! 16:
! 17: You should have received a copy of the GNU Library 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. */
! 21:
! 22: #include "gmp.h"
! 23: #include "gmp-impl.h"
! 24:
! 25: #if defined __GNUC__
! 26: #if defined __sparc_v9__ && BITS_PER_MP_LIMB == 64
! 27: #define popc_limb(a) \
! 28: ({ \
! 29: DItype __res; \
! 30: asm ("popc %1,%0" : "=r" (__res) : "rI" (a)); \
! 31: __res; \
! 32: })
! 33: #endif
! 34: #endif
! 35:
! 36: #ifndef popc_limb
! 37:
! 38: /* Cool population count of a mp_limb_t.
! 39: You have to figure out how this works, I won't tell you! */
! 40:
! 41: static inline unsigned int
! 42: popc_limb (x)
! 43: mp_limb_t x;
! 44: {
! 45: #if BITS_PER_MP_LIMB == 64
! 46: /* We have to go into some trouble to define these constants.
! 47: (For mp_limb_t being `long long'.) */
! 48: mp_limb_t cnst;
! 49: cnst = 0x55555555L | ((mp_limb_t) 0x55555555L << BITS_PER_MP_LIMB/2);
! 50: x = ((x & ~cnst) >> 1) + (x & cnst);
! 51: cnst = 0x33333333L | ((mp_limb_t) 0x33333333L << BITS_PER_MP_LIMB/2);
! 52: x = ((x & ~cnst) >> 2) + (x & cnst);
! 53: cnst = 0x0f0f0f0fL | ((mp_limb_t) 0x0f0f0f0fL << BITS_PER_MP_LIMB/2);
! 54: x = ((x >> 4) + x) & cnst;
! 55: x = ((x >> 8) + x);
! 56: x = ((x >> 16) + x);
! 57: x = ((x >> 32) + x) & 0xff;
! 58: #endif
! 59: #if BITS_PER_MP_LIMB == 32
! 60: x = ((x >> 1) & 0x55555555L) + (x & 0x55555555L);
! 61: x = ((x >> 2) & 0x33333333L) + (x & 0x33333333L);
! 62: x = ((x >> 4) + x) & 0x0f0f0f0fL;
! 63: x = ((x >> 8) + x);
! 64: x = ((x >> 16) + x) & 0xff;
! 65: #endif
! 66: return x;
! 67: }
! 68: #endif
! 69:
! 70: unsigned long int
! 71: #if __STDC__
! 72: mpn_popcount (register mp_srcptr p, register mp_size_t size)
! 73: #else
! 74: mpn_popcount (p, size)
! 75: register mp_srcptr p;
! 76: register mp_size_t size;
! 77: #endif
! 78: {
! 79: unsigned long int popcnt;
! 80: mp_size_t i;
! 81:
! 82: popcnt = 0;
! 83: for (i = 0; i < size; i++)
! 84: popcnt += popc_limb (p[i]);
! 85:
! 86: return popcnt;
! 87: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>