Understanding Magic Bitboards
Posted: 03 Apr 2013, 06:56
Hi all.
Sorry, if these questions seem trivial or if they have already been asked (I searched, but could not get the answers). I am experimenting with Magic Bitboards in Vicki 2.0. I can simply use the existing set of magics, but that misses the point for me. I would like a good understanding on how and why it works. For now, I will be content if I can generate my own set of magics. I have more or less an understanding of the basic ideas.
For a bitscan application the magic is always a proper De Bruijn number (i.e. a number part of the De Bruijn sequence). Since only one bit is set in the occupancy bitboard (before multiplying with the magic and shifting), perfect hashing is achieved. For chess, this is not the case since multiple bits could be set. To generate the magic, a brute-force, trial-and-error approach must be used.
My questions as follows:
(1) Since multiple bits are potentially set, a magic number for rook/bishop lookups is not necessarily a De-Bruijn number. It needs to be sparsely populated though (i.e. only a few bits is set). Is this correct?
(2) From what I've read, a rook on A1, requires a database of size 1024 (10 bits) to hash without collisions using the currently best known magic of 0x1234567890123456. Given an ample amount of time, would it be possible to find a magic for say, a 9-bit database? Since a rook on A1 has 12 possible blockers or none at all (giving 14), the absolute minimum would be 49, i.e. pick 1 from 7 horizontally, or not at all and 1 from 7 vertically, or not at all. This gives a total of 49. Am I talking nonsense here? Of course, finding a magic number that hashes all those boards (2^7^2=16384, i think) to 42 would be a pipe dream. However, is there a proof of some sorts to give the absolute minimum database size required? I don't think there is, or whether it is even possible.
Thank you.
Sorry, if these questions seem trivial or if they have already been asked (I searched, but could not get the answers). I am experimenting with Magic Bitboards in Vicki 2.0. I can simply use the existing set of magics, but that misses the point for me. I would like a good understanding on how and why it works. For now, I will be content if I can generate my own set of magics. I have more or less an understanding of the basic ideas.
For a bitscan application the magic is always a proper De Bruijn number (i.e. a number part of the De Bruijn sequence). Since only one bit is set in the occupancy bitboard (before multiplying with the magic and shifting), perfect hashing is achieved. For chess, this is not the case since multiple bits could be set. To generate the magic, a brute-force, trial-and-error approach must be used.
My questions as follows:
(1) Since multiple bits are potentially set, a magic number for rook/bishop lookups is not necessarily a De-Bruijn number. It needs to be sparsely populated though (i.e. only a few bits is set). Is this correct?
(2) From what I've read, a rook on A1, requires a database of size 1024 (10 bits) to hash without collisions using the currently best known magic of 0x1234567890123456. Given an ample amount of time, would it be possible to find a magic for say, a 9-bit database? Since a rook on A1 has 12 possible blockers or none at all (giving 14), the absolute minimum would be 49, i.e. pick 1 from 7 horizontally, or not at all and 1 from 7 vertically, or not at all. This gives a total of 49. Am I talking nonsense here? Of course, finding a magic number that hashes all those boards (2^7^2=16384, i think) to 42 would be a pipe dream. However, is there a proof of some sorts to give the absolute minimum database size required? I don't think there is, or whether it is even possible.
Thank you.