Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Counting bits

Author: Gerd Isenberg

Date: 12:58:39 04/17/03

Go up one level in this thread


On April 17, 2003 at 14:29:28, Andreas Herrmann wrote:

>Hi,
>
>i'm looking for a fast way to count all bits, which are set to 1 in a integer
>value. Is this possible, whithout a loop in Assembly or Delphi/Pascal.
>
>Andreas

Hi Andreas,

have a look on this side:
http://aggregate.org/MAGIC/

I use this bitboard one for athlon only, slightly modified from amd's
optimization guide:

Regards,
Gerd


#define CACHE_LINE  32
#define CACHE_ALIGN __declspec(align(CACHE_LINE))

struct SBitCountConsts
{
	BitBoard C33;
	BitBoard C55;
	BitBoard C0F;
        ....
};
extern const SBitCountConsts BitCountConsts;

__forceinline
int BitCount (BitBoard bb)
{
	__asm
	{
		movd mm0, word ptr bb
		punpckldq mm0, word ptr bb + 4
		lea  eax, [BitCountConsts]
		movq mm1,mm0
		psrld mm0,1
		pand mm0,[eax].C55
		psubd mm1,mm0
		movq mm0,mm1
		psrld mm1,2
		pand mm0,[eax].C33
		pand mm1,[eax].C33
		paddd mm0,mm1
		movq mm1,mm0
		psrld mm0,4
		paddd mm0,mm1
		pand mm0,[eax].C0F
		pxor mm1,mm1
		psadbw (mm0,mm1)
		movd eax,mm0
	}
}


// in some cpp file

const SBitCountConsts CACHE_ALIGN BitCountConsts =
{
	0x3333333333333333,	// C33
	0x5555555555555555,	// C55
	0x0f0f0f0f0f0f0f0f,	// C0F
        ....
};




This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.