Kindergarten Bitboard help

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Greg Strong
Posts: 388
Joined: Sun Dec 21, 2008 6:57 pm
Location: Washington, DC

Kindergarten Bitboard help

Post by Greg Strong »

I'm moving from rotated bitboard move generation to kindergarten bitboards. I'm following the examples on the excellent Chess Programming Wiki. Everything has worked perfectly, except for the file attacks, which must be handled a little differently from the other directions. (Well, for my program it's actually the rank attacks, because I orient my bits differently, but that's not important for present purposes.)

On the wiki, it rotates and shifts the B-G bits of the first file to the first 6 bits by multiplying by c7-h2 antidiagonal and right-shifting by 58. But that doesn't seem to work. Lets take the simple case of the entire first rank being occupied ...

Code: Select all

U64 aFile = 0x0101010101010101ULL;
U64 c7h2 = 0x0204081020400000ULL;
U64 test = (aFile * c7h2) >> 58;
After this, the value of test is 0x1F instead of 0x3F. I'm guessing it's supposed to be a different antidiagonal, but I don't know as I really don't understand the magic bit-flipping multiplication.

As always, any help is much appreciated!
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Kindergarten Bitboard help

Post by Gerd Isenberg »

Greg Strong wrote:I'm moving from rotated bitboard move generation to kindergarten bitboards. I'm following the examples on the excellent Chess Programming Wiki. Everything has worked perfectly, except for the file attacks, which must be handled a little differently from the other directions. (Well, for my program it's actually the rank attacks, because I orient my bits differently, but that's not important for present purposes.)

On the wiki, it rotates and shifts the B-G bits of the first file to the first 6 bits by multiplying by c7-h2 antidiagonal and right-shifting by 58. But that doesn't seem to work. Lets take the simple case of the entire first rank being occupied ...

Code: Select all

U64 aFile = 0x0101010101010101ULL;
U64 c7h2 = 0x0204081020400000ULL;
U64 test = (aFile * c7h2) >> 58;
After this, the value of test is 0x1F instead of 0x3F. I'm guessing it's supposed to be a different antidiagonal, but I don't know as I really don't understand the magic bit-flipping multiplication.

As always, any help is much appreciated!
Your c7h2 antidiagonal seems false:

Code: Select all

masked A-file    *  c7-h2 AntiDiag   =  occupancy
0x0101010101010101  0x0004081020408000  0xFCF8F0E0C0800000
H . . . . . . .     . . . . . . . .     . .[B C D E F G]    . . . . . . . .
G . . . . . . .     . . 1 . . . . .     . . A B C D E F     . . . . . . . .
F . . . . . . .     . . . 1 . . . .     . . . A B C D E     . . . . . . . .
E . . . . . . .     . . . . 1 . . .     . . . . A B C D  >> . . . . . . . .
D . . . . . . .  *  . . . . . 1 . .  =  . . . . . A B C  58 . . . . . . . .
C . . . . . . .     . . . . . . 1 .     . . . . . . A B     . . . . . . . .
B . . . . . . .     . . . . . . . 1     . . . . . . . A     . . . . . . . .
A . . . . . . .     . . . . . . . .     . . . . . . . .    [B C D E F G]. .
User avatar
Greg Strong
Posts: 388
Joined: Sun Dec 21, 2008 6:57 pm
Location: Washington, DC

Re: Kindergarten Bitboard help

Post by Greg Strong »

Oops... Thanks! :oops:
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Kindergarten Bitboard help

Post by Gerd Isenberg »

Greg Strong wrote:Oops... Thanks! :oops:
No problem ;-)

The confusing point is that the constants need to have same values independent from square mapping, but 0x0101010101010101 might be A- or H-file or 1st or 8th rank. Same for the six-bit (anti) diagonals 0x0004081020408000 or 0x0080402010080400 as "magic" factor, which both map square B-G or better bits {8,16,24,32,40,48} to the upper six bits {58..63}. The "kindergarten multiplication" has no intermediate overflows, whether you map 8 to 56 or reverse to 63.