Computer Chess Club Archives


Search

Terms

Messages

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

Author: Miguel A. Ballicora

Date: 12:47:01 01/10/03

Go up one level in this thread


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.

Don't mess up with assembler if you are not absolutely sure that it will speed
up your program. I really doubt that is your case! You will probably use just a
bit of bitboards that the information will go to the pawn table anyway.

[snip]
>
>int PopCount(BitBoard a)
>{
>  unsigned long w;
>  int n = 0;
>  w = a&0xffffffffUL;

here you are mixing 'a' which is long long with the constant that is long.
It is legal but it might gives you a warning. Because the result is long long
(the bigger one) and then you try to put it into a variable that is just 'long'.
Since you bought K&RII check the part that talks about integral promotions etc.

In fact

w = a; /* it is legal C, will give you what you need, but it could give a
          warning, all the upper bits are cut off by defintion*/

Try

w = (unsigned long) a; /* This cuts off all the upper bits by definition too,
                         but the typecast tells the compiler that you know
                         what you're doing, however, you assume that long is
                         32 bits, which is true in the hardware you use */

or more portable

w = (unsigned long)a & 0xffffffffUL; /* this one I like better */

even these should work
w = (unsigned long)(a & (Bitboard)0xffffffff);
w = (unsigned long)(a & 0xffffffffUL);

The compiler should give the same optimized code, I guess.

Miguel

>  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 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.