Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: FirstOne/LastOne

Author: Dieter Buerssner

Date: 11:46:10 06/12/04

Go up one level in this thread


On June 12, 2004 at 13:09:30, Gian-Carlo Pascutto wrote:

>The best thing seems to be one of the "Magic bitscans".
>
>const BITBOARD magic = 0x03f08c5392f756cdULL;
>
>const unsigned int magictable[64] = {
>0, 1, 12, 2, 13, 22, 17, 3,
>14, 33, 23, 36, 18, 58, 28, 4,
>62, 15, 34, 26, 24, 48, 50, 37,
>19, 55, 59, 52, 29, 44, 39, 5,
>63, 11, 21, 16, 32, 35, 57, 27,
>61, 25, 47, 49, 54, 51, 43, 38,
>10, 20, 31, 56, 60, 46, 53, 42,
>9, 30, 45, 41, 8, 40, 7, 6,
>};
>
>unsigned int FindFirst (const BITBOARD b) {
                         ^^^^^

I think, this const buys nothing.

> const BITBOARD lsb = b & -b;
> return magictable[(lsb * magic) >> 58];
>}

Perhaps, doing the multiplication "manually" will produce better code.

const unsigned int magicl = 0x92f756cd, magich=0x03f08c53;

unsigned int FindFirst(BITBOARD b)
{
  unsigned int bl, bh, r;
  bh = b >> 32;
  bl = b;
  r = (bl*(BITBOARD)magicl)>>32;
  r += bl*magich;
  r += bh*magicl;
  return magictable[r>>26];
}

Totally untested. But I compiled it with MSVC and GCC and looked at assembler
output. Seems better than above. Basically one mul, 2 imul, 2 add, one shr and a
few mov to get the index into the table.

Regards,
Dieter



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.