Simply working out the magic shifted by the each bit in the key (what I did here) or doing it the other way around (key shifted by every bit in the magic...but I find this way harder to visualize because what you shift is changing) should make it clear why 5 bits are enough to shift all the relevant bits into the upper bits:Aleks Peshkov wrote:Thanks, picture makes me understand why 1 bit is sufficient for horizontal rook attacks. But diagonals and verticals still unclear.Gerd Isenberg wrote:You have to consider that we mask off the outer squares. Thus for a rook on h1 one bit for the six rank-bits, and three further bit-factors to somehow shift and squeeze the six file-bit to the upper 12 seems the minimum.Code: Select all
0x008080808080807E . . . . . . . . . . . . . . . 1 . . . . . . . 1 . . . . . . . 1 . . . . . . . 1 . . . . . . . 1 . . . . . . . 1 . 1 1 1 1 1 1 .
My question is not idle. Each square attack table have a set significant attack bitboards separated by unused space. Used and unused bitboards seems to have a regular pattern, so it is probably possible to find a simple way to gather all separate squares hash tables in a one big hash table with less total size. At least bishops have a simple odd/even regularity, so it may be possible to interlace light/dark correspondent squares data together.
Code: Select all
key(H8) = something & 0x008080808080807E
magic(H8) = 0x0080002040800100
indexmapping = magic*key =
(starting from LSB to MSB)
Start with 0
If bit 1 in the key is "on", add on 0x0100004081000200
10000000
00000000
00000000
00000010
10000001
00000000
01000000
00000000
If bit 2 in the key is "on", add on 0x0200008102000400
01000000
00000000
00000000
10000001
01000000
00000000
00100000
00000000
If bit 3 in the key is "on", add on 0x0400010204000800
00100000
00000000
10000000
01000000
00100000
00000000
00010000
00000000
If bit 4 in the key is "on", add on 0x0800020408001000
00010000
00000000
01000000
00100000
00010000
00000000
00001000
00000000
If bit 5 in the key is "on", add on 0x1000040810002000
00001000
00000000
00100000
00010000
00001000
00000000
00000100
00000000
If bit 6 in the key is "on", add on 0x2000081020004000
00000100
00000000
00010000
00001000
00000100
00000000
00000010
00000000
If bit 15 in the key is "on", add on 0x0010204000800000
00000000
00001000
00000100
00000010
00000000
00000001
00000000
00000000
If bit 23 in the key is "on", add on 0x1020400080000000
00001000
00000100
00000010
00000000
00000001
00000000
00000000
00000000
If bit 31 in the key is "on", add on 0x2040008000000000
00000100
00000010
00000000
00000001
00000000
00000000
00000000
00000000
If bit 39 in the key is "on", add on 0x4000800000000000
00000010
00000000
00000001
00000000
00000000
00000000
00000000
00000000
If bit 47 in the key is "on", add on 0x0080000000000000
00000000
00000001
00000000
00000000
00000000
00000000
00000000
00000000
If bit 55 in the key is "on", add on 0x8000000000000000
00000001
00000000
00000000
00000000
00000000
00000000
00000000
00000000