记录《hack's delight》 中一个神奇算法。
static const int lsz64_tbl[64] = {
0, 31, 4, 33, 60, 15, 12, 34,
61, 25, 51, 10, 56, 20, 22, 35,
62, 30, 3, 54, 52, 24, 42, 19,
57, 29, 2, 44, 47, 28, 1, 36,
63, 32, 59, 5, 6, 50, 55, 7,
16, 53, 13, 41, 8, 43, 46, 17,
26, 58, 49, 14, 11, 40, 9, 45,
21, 48, 39, 23, 18, 38, 37, 27
};
//______________________________________________________________________________
/* FirstPiece():
*
* Return square number (0 to 63) of the least significant set bit
* in bitboard 'bb'
*
* source: Matt Taylor's "de Bruijn method" implementation
* Probably best if you don't even TRY to understand this one. I certainly don't...
*/
//______________________________________________________________________________
int FirstPiece(const BITBOARD bb) {
const BITBOARD lsb = (bb & -bb) - 1;
const unsigned int foldedLSB = ((unsigned int) lsb) ^ ((unsigned int)(lsb >> 32));
return lsz64_tbl[foldedLSB * 0x78291ACF >> 26];
}