Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: questions about using popcount function to count bitboard

Author: Bas Hamstra

Date: 12:31:05 01/10/03

Go up one level in this thread


Hi Uri,

Personally I use a lookup table for 8 bits at a time (count each row). You can
see a bitboard as an array of 8 unsigned chars.

BB Test = 423434234ui64;
int Count = PopCnt(Test);

inline int PopCnt(BB BitBoard)

{    unsigned char* p = (unsigned char*) BitBoard;
     return PopCnt8[p[0]] + PopCnt8[p[1]] ... + PopCnt8[p[7]];
}

You have to create an small precalculated array PopCnt8[256] for this. It is not
the absolute fastest, but also not much slower than that.

Bas.









On January 10, 2003 at 15:16:52, Uri Blass wrote:

>1)How do I use assembler code under C to count the number of 1's in a bitboard?
>I do not know how to copy assembler code from crafty.
>
>I understood from previous post that the relevant code is in crafty but I do not
>know how to copy it correctly with all the #defines in crafty.
>
>I understood that there are some relevant codes(x86.s, vcline.h that I could not
>find and boolean.h
>
>2)I tried a code of dieter and got warning from the compiler(see the bottom of
>this post for that code)
>
>warning C4244: '=' : conversion from 'unsigned __int64 ' to 'unsigned long ',
>possible loss of data
>
>Note that I changed
>int PopCount(unsigned long long a)
>to
>int PopCount(BitBoard a)
>
>I also have in my code
>typedef unsigned __int64 BitBoard;
>
>I also never use the word long in my program except the code that I copied from
>Dieter and I use int for 32 bit numbers.
>
>The warning was about w=
>
>int PopCount(BitBoard a)
>{
>  unsigned long w;
>  int n = 0;
>  w = a&0xffffffffUL;
>  if (w)
>    do
>    {
>      n++;
>    }
>    while ((w &= w-1) != 0);
>  w = (a>>32)&0xffffffffUL;
>  if (w)
>    do
>    {
>      n++;
>    }
>    while ((w &= w-1) != 0);
>  return n;
>}
>
>Uri



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.