X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fbitboard.cpp;h=4a7a0af241c53939622aad53b267e2085d8108ba;hb=341f42be8c873db348e74756281ad8dacce0a486;hp=ba03d66ef85669cfe4d9f3c8ddf3fa19e272d99c;hpb=063e2441b17b8260de443b3d580f49b3d65d03c7;p=stockfish diff --git a/src/bitboard.cpp b/src/bitboard.cpp index ba03d66e..4a7a0af2 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -161,7 +161,7 @@ const int RShift[64] = { 21, 22, 22, 22, 22, 22, 22, 21, 20, 21, 21, 21, 21, 21, 21, 20 }; -#endif +#endif // defined(IS_64BIT) Bitboard RMask[64]; @@ -242,19 +242,43 @@ void init_bitboards() { } +/// first_1() finds the least significant nonzero bit in a nonzero bitboard. /// pop_1st_bit() finds and clears the least significant nonzero bit in a /// nonzero bitboard. -#if defined(IS_64BIT) +#if defined(IS_64BIT) && !defined(USE_BSFQ) + +static const int BitTable[64] = { + 0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40, 5, 17, 26, 38, 15, + 46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57, 63, 6, 12, 18, 24, 27, 33, 39, + 16, 37, 45, 47, 30, 53, 49, 56, 62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43, + 51, 60, 42, 59, 58 +}; -Square pop_1st_bit(Bitboard *b) { - Bitboard bb = *b ^ (*b - 1); - uint32_t fold = int(bb) ^ int(bb >> 32); +Square first_1(Bitboard b) { + return Square(BitTable[((b & -b) * 0x218a392cd3d5dbfULL) >> 58]); +} + +Square pop_1st_bit(Bitboard* b) { + Bitboard bb = *b; *b &= (*b - 1); - return Square(BitTable[(fold * 0x783a9b23) >> 26]); + return Square(BitTable[((bb & -bb) * 0x218a392cd3d5dbfULL) >> 58]); } -#else +#elif !defined(USE_BSFQ) + +static const int BitTable[64] = { + 63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2, + 51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52, + 26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28, + 58, 20, 37, 17, 36, 8 +}; + +Square first_1(Bitboard b) { + b ^= (b - 1); + uint32_t fold = int(b) ^ int(b >> 32); + return Square(BitTable[(fold * 0x783a9b23) >> 26]); +} // Use type-punning union b_union { @@ -267,7 +291,7 @@ union b_union { }; // WARNING: Needs -fno-strict-aliasing compiler option -Square pop_1st_bit(Bitboard *bb) { +Square pop_1st_bit(Bitboard* bb) { b_union u; uint32_t b;