Re: List of magics for bitboard move geneartion
Posted:
23 Aug 2006, 21:35
by Gerd Isenberg
Pradu wrote:A good set of magic keys for bitboard move generation can be found here:
http://www.prism.gatech.edu/~gtg365v/Bu ... magics.txtSune Fischer and I are wiriting a program to produce a comparison in speed between rotated and magic move generation. I guess we shall post the source and results here to see if magic move generation outperforms rotated at modern hardware.
Looks like you already resigned on the 11-bit key issue - due to snooby?
Is the goal a perft-test, a test inside for instance crafty, or a complete own open source program under some public licence?
I would like to compete with my 1.5KByte approach as well - with a slightly improved 64-bit version
Best regards to Sune as well.
Cheers,
Gerd
Re: List of magics for bitboard move geneartion
Posted:
23 Aug 2006, 22:20
by Pradu
Gerd Isenberg wrote:Looks like you already resigned on the 11-bit key issue - due to snooby?
Snooby is a genius algorithm I must admit! 11 bit keys exist for most squares but not for all squares. Taking a look at Lasse's keys, he dosen't have all 11 bit keys, some are 12 bit. So it isn't truly a 11 bit magic set. Usually if a key is possible it is generated very quickly by the popcount-iterative approach. I have also produced keys that are the least I could find for each square and these are probably going to be the keys being used in my move generator (including many 8-10 bit keys for rooks). These are the minimal+n hash keys in the text file (minimal bits + extra bits). The 9 bit keys and the 12 bit keys are just for the sake of completeness for move generation that require the same shift for each square in the hash function -- but I guess the minimal+n bits can also be used as 9 bit / 12 bit keys if needed.
Lasse's hashkeys.c wrote:const U64 HashKeyRook[64]={
0x0880008210604000, // H8 12
0x6040012001401000, // H7 11
0x0200100920820041, // H6 11
0x0200104020182e00, // H5 11
0x4b00080003001014, // H4 11
0x7100088900024400, // H3 11
0x02000c3e00010088, // H2 11
0xe600004085062204, // H1 12
0x1002800040128020, // G8 11
...
Gerd wrote:Is the goal a perft-test, a test inside for instance crafty, or a complete own open source program under some public licence?
I would like to compete with my 1.5KByte approach as well - with a slightly improved 64-bit version
Best regards to Sune as well.
Cheers,
Gerd
The initial idea was to take one of Sune's old rotated bitboard move generators, because I'm not as experienced in writing efficient rotated bitboard move generators; but then I decided to write a new one that will be easier to read/modify. I will make the program opensource.
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 08:33
by Lasse Hansen
Hi!
It is possible to get even smaller tables than 41k+800k by using a post-mask in the move generation. If as indices, one uses:
U32 IdxKeyRook[64]={
8192, 34816, 36864, 38912, 40960, 43008, 45056, 12288,
34816, 8192, 38912, 36864, 43008, 40960, 12288, 45056,
32768, 30720, 0, 5120, 6144, 1024, 49152, 47104,
30720, 28672, 5120, 0, 1024, 6144, 47104, 49152,
28672, 26624, 4096, 3072, 2048, 7168, 53248, 51200,
26624, 24576, 3072, 4096, 7168, 2048, 51200, 53248,
24576, 20480, 61440, 63488, 57344, 59392, 16384, 55296,
20480, 65536, 63488, 61440, 59392, 57344, 55296, 16384,
}; // 67584 entries
U32 IdxKeyBishop[64]={
1728, 1472, 1632, 1632, 1632, 1632, 1568, 1344,
1728, 1472, 1600, 1600, 1600, 1600, 1568, 1344,
1728, 1472, 1152, 1152, 1152, 1152, 1568, 1344,
1728, 1472, 0, 0, 0, 0, 1568, 1344,
1280, 1504, 512, 512, 512, 512, 1536, 1408,
1280, 1504, 1024, 1024, 1024, 1024, 1536, 1408,
1280, 1504, 1664, 1664, 1664, 1664, 1536, 1408,
1280, 1504, 1696, 1696, 1696, 1696, 1536, 1408,
}; // 1792 entries
one is down to around 14.3k (+0.5k) + 540k (+0.5k) of lookups. The new masks must include all borders (i.e. no 64 bit trick). Works for me!
Regards, Lasse
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 17:15
by Pradu
Lasse Hansen wrote:Hi!
It is possible to get even smaller tables than 41k+800k by using a post-mask in the move generation. If as indices, one uses:
U32 IdxKeyRook[64]={
8192, 34816, 36864, 38912, 40960, 43008, 45056, 12288,
34816, 8192, 38912, 36864, 43008, 40960, 12288, 45056,
32768, 30720, 0, 5120, 6144, 1024, 49152, 47104,
30720, 28672, 5120, 0, 1024, 6144, 47104, 49152,
28672, 26624, 4096, 3072, 2048, 7168, 53248, 51200,
26624, 24576, 3072, 4096, 7168, 2048, 51200, 53248,
24576, 20480, 61440, 63488, 57344, 59392, 16384, 55296,
20480, 65536, 63488, 61440, 59392, 57344, 55296, 16384,
}; // 67584 entries
U32 IdxKeyBishop[64]={
1728, 1472, 1632, 1632, 1632, 1632, 1568, 1344,
1728, 1472, 1600, 1600, 1600, 1600, 1568, 1344,
1728, 1472, 1152, 1152, 1152, 1152, 1568, 1344,
1728, 1472, 0, 0, 0, 0, 1568, 1344,
1280, 1504, 512, 512, 512, 512, 1536, 1408,
1280, 1504, 1024, 1024, 1024, 1024, 1536, 1408,
1280, 1504, 1664, 1664, 1664, 1664, 1536, 1408,
1280, 1504, 1696, 1696, 1696, 1696, 1536, 1408,
}; // 1792 entries
one is down to around 14.3k (+0.5k) + 540k (+0.5k) of lookups. The new masks must include all borders (i.e. no 64 bit trick). Works for me!
Regards, Lasse
Really clever. So what you are doing is
- Code: Select all
Key = OccupancyALL & mask[sq]
Index = Key*magic[sq]>>shift[sq]
Moves = *(Ptr[sq]+Index) & postmask[sq]
And instead in each move-bitboard in the database you will have multiple sets of moves that you will & out? You should still be able to use the 64-trick for mask[sq] right (as long as the moves are entirely on different lines)?
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 20:13
by Lasse Hansen
If you have a rook at A1 and a rook at B2, they will always share the attack targets A2 and B1, but no one else. So yes, there will then be multiple attack bitmaps in the same entry. One only have to mask it out afterwards, using full mask, without the 64 bit trick. For bishops you can have 4 pieces in line, and the only common targets will always be there anyway.
So move generation is done as before, one only apply a bit wider mask to get the wanted attack board.
I havent seen anyone else do this, in
http://chessprogramming.wikispaces.com/ it seems that the current minimum is 800k + 39k. Neither here or at Talkchess, unless Ive overseen it of course.
pattern = Allpieces & maskrook64[sqr]; // mask with 64 bit trick
address = (pattern*rookmagic[sq])>>rookshift(sq);
attack = RookAttacks[rookindex[sqr]+address] & maskfull[sqr];
//maskfull have to cover edges
//rookindex[sqr] is the table I gave in the earlier post
It should be easy to implement.
Regards, Lasse
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 21:14
by bob
also, aren't those magic values the same ones Pradu produced last year? Those are the exact same numbers I have in Crafty and I took the last values posted by Pradu when I made the change to magic generation...
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 21:21
by Pradu
bob wrote:also, aren't those magic values the same ones Pradu produced last year? Those are the exact same numbers I have in Crafty and I took the last values posted by Pradu when I made the change to magic generation...
The top post is from Wed Aug 23, 2006 4:06 pm so yes it was a while back.
New recent post was by Lasse in this thread.
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 21:30
by Pradu
Lasse Hansen wrote:If you have a rook at A1 and a rook at B2, they will always share the attack targets A2 and B1, but no one else. So yes, there will then be multiple attack bitmaps in the same entry. One only have to mask it out afterwards, using full mask, without the 64 bit trick. For bishops you can have 4 pieces in line, and the only common targets will always be there anyway.
So move generation is done as before, one only apply a bit wider mask to get the wanted attack board.
I havent seen anyone else do this, in
http://chessprogramming.wikispaces.com/ it seems that the current minimum is 800k + 39k. Neither here or at Talkchess, unless Ive overseen it of course.
I believe Grant Osborne was able to get it even smaller by trying to find magics that will hash as sequentially as possible from 0..n without any hits above n. This way you don't have to use the entire 2^numbitsindex entries.
pattern = Allpieces & maskrook64[sqr]; // mask with 64 bit trick
address = (pattern*rookmagic[sq])>>rookshift(sq);
attack = RookAttacks[rookindex[sqr]+address] & maskfull[sqr];
//maskfull have to cover edges
//rookindex[sqr] is the table I gave in the earlier post
It should be easy to implement.
Regards, Lasse
Yes, nobody has done this before. As far as I know, your approach is new. Do you have performance results of the new approach against the one? I'll try to implement it and try to get some benchmarks as well. I'm not a optimization expert; maybe Gerd can help on this but wouldn't
- Code: Select all
attacks = *(aptr[sq]+index) & maskfull[sq]
be faster than
- Code: Select all
attacks = RookAttacks[rookindex[sqr]+address] & maskfull[sq]
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 21:34
by Gerd Isenberg
Lasse Hansen wrote:If you have a rook at A1 and a rook at B2, they will always share the attack targets A2 and B1, but no one else. So yes, there will then be multiple attack bitmaps in the same entry. One only have to mask it out afterwards, using full mask, without the 64 bit trick. For bishops you can have 4 pieces in line, and the only common targets will always be there anyway.
So move generation is done as before, one only apply a bit wider mask to get the wanted attack board.
I havent seen anyone else do this, in
http://chessprogramming.wikispaces.com/ it seems that the current minimum is 800k + 39k. Neither here or at Talkchess, unless Ive overseen it of course.
pattern = Allpieces & maskrook64[sqr]; // mask with 64 bit trick
address = (pattern*rookmagic[sq])>>rookshift(sq);
attack = RookAttacks[rookindex[sqr]+address] & maskfull[sqr];
//maskfull have to cover edges
//rookindex[sqr] is the table I gave in the earlier post
It should be easy to implement.
Regards, Lasse
Great idea, Lasse!
Bishop sharing is simpler to understand, since there are light and dark colored bishops with disjoint attacks-sets, able to share the precalculated union of two square-attacks.
Your genius trick is to share even equal colored bishops and rooks where both attack-sets are obviously not always disjoint - but all members of the intersection are always set, since they are direct neighbors of both sliders.
The post-masking reminds me a bit on kindergarten bitboards, where 8 ranks and 30 diagonals share one attack-set. At least kindergarten bitboards has the "advantage" to use same mask for occupancy as well for the attack-set
Cheers,
Gerd
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 23:35
by Gerd Isenberg
Pradu wrote:What I had stated in this post was wrong. Too bad I can't delete this post anymore.
HeHe - brain storming and thinking out loud
Annother idea is to overlap attack sets of different squares a bit. There are still a lot of gaps. I think <= 512KByte is possible. There are so many possibilities to recombine and overlap the attack-arrays.
Re: List of magics for bitboard move geneartion
Posted:
09 May 2008, 23:48
by Zach Wegner
I was thinking this at first, but I don't think it will be possible, at least easily. Theoretically you can have up to 8 rooks stored in the same bitboard, but the problem is that for each intersection of rays you need to make sure that the intersection square maps to the same bit index for each square's magic. You can ignore that in Lasse's solution because the squares are always attacked.
Actually, I think the theoretical max is 4 rooks, because you can't have a conditional square in a rook's attack set intersect with an unconditional square in another rook's. I.e. Two rooks on A1 and B4, the magic bits have to determine B1 and A4 when those are always set in one of the rook's sets. (Does that make sense? I sometimes wonder if I say exactly what I'm thinking...)
Without that, there is just one square to gain, as the IdxKeyRook table has only 2 entries that aren't duplicated.
Re: List of magics for bitboard move geneartion
Posted:
10 May 2008, 06:19
by Zach Wegner
A conditional square is one that might or might not be set, depending on the bits in the index. For all the sliders, its just the squares that are more than one square away from it. The unconditional squares are the squares right next to the slider, that it will attack no matter what other pieces are on the board.
I'm pretty sure the theoretical max for rooks is six. You can have unconditional squares overlap, or conditional squares overlap, but they can't overlap each other (Well, I guess they could, but each time they do requires an extra bit in the index).