X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitboard.h;h=00f0f576d91972762e7f2781a585c3600f371e24;hp=2451c13928e174208335433f8e1459948b504a56;hb=b1b0c640462aed199a3665e575f9cb208b8e4687;hpb=09d217bff73d48c39f136c61383609315b0ae173 diff --git a/src/bitboard.h b/src/bitboard.h index 2451c139..00f0f576 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -60,24 +60,22 @@ extern Bitboard SquaresInFrontMask[2][64]; extern Bitboard PassedPawnMask[2][64]; extern Bitboard AttackSpanMask[2][64]; -extern const uint64_t RMult[64]; -extern const int RShift[64]; -extern Bitboard RMask[64]; -extern int RAttackIndex[64]; -extern Bitboard RAttacks[0x19000]; - -extern const uint64_t BMult[64]; -extern const int BShift[64]; -extern Bitboard BMask[64]; -extern int BAttackIndex[64]; -extern Bitboard BAttacks[0x1480]; - extern Bitboard BishopPseudoAttacks[64]; extern Bitboard RookPseudoAttacks[64]; extern Bitboard QueenPseudoAttacks[64]; extern uint8_t BitCount8Bit[256]; +struct Magics { + Bitboard mask; + uint64_t mult; + uint32_t shift; + Bitboard* attacks; +}; + +extern Magics RMagics[64]; +extern Magics BMagics[64]; + /// Functions for testing whether a given bit is set in a bitboard, and for /// setting and clearing bits. @@ -173,28 +171,28 @@ inline Bitboard in_front_bb(Color c, Square s) { #if defined(IS_64BIT) -inline Bitboard rook_attacks_bb(Square s, Bitboard blockers) { - Bitboard b = blockers & RMask[s]; - return RAttacks[RAttackIndex[s] + ((b * RMult[s]) >> RShift[s])]; +inline Bitboard rook_attacks_bb(Square s, Bitboard occ) { + const Magics& m = RMagics[s]; + return m.attacks[((occ & m.mask) * m.mult) >> m.shift]; } -inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) { - Bitboard b = blockers & BMask[s]; - return BAttacks[BAttackIndex[s] + ((b * BMult[s]) >> BShift[s])]; +inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) { + const Magics& m = BMagics[s]; + return m.attacks[((occ & m.mask) * m.mult) >> m.shift]; } #else // if !defined(IS_64BIT) -inline Bitboard rook_attacks_bb(Square s, Bitboard blockers) { - Bitboard b = blockers & RMask[s]; - return RAttacks[RAttackIndex[s] + - (unsigned(int(b) * int(RMult[s]) ^ int(b >> 32) * int(RMult[s] >> 32)) >> RShift[s])]; +inline Bitboard rook_attacks_bb(Square s, Bitboard occ) { + const Magics& m = RMagics[s]; + Bitboard b = occ & m.mask; + return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift]; } -inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) { - Bitboard b = blockers & BMask[s]; - return BAttacks[BAttackIndex[s] + - (unsigned(int(b) * int(BMult[s]) ^ int(b >> 32) * int(BMult[s] >> 32)) >> BShift[s])]; +inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) { + const Magics& m = BMagics[s]; + Bitboard b = occ & m.mask; + return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift]; } #endif @@ -257,15 +255,25 @@ inline bool squares_aligned(Square s1, Square s2, Square s3) { /// pop_1st_bit() finds and clears the least significant nonzero bit in a /// nonzero bitboard. -#if defined(USE_BSFQ) // Assembly code by Heinz van Saanen +#if defined(USE_BSFQ) + +#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) -inline Square first_1(Bitboard b) { +FORCE_INLINE Square first_1(Bitboard b) { + unsigned long index; + _BitScanForward64(&index, b); + return (Square) index; +} +#else + +FORCE_INLINE Square first_1(Bitboard b) { // Assembly code by Heinz van Saanen Bitboard dummy; __asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) ); - return (Square)(dummy); + return (Square) dummy; } +#endif -inline Square pop_1st_bit(Bitboard* b) { +FORCE_INLINE Square pop_1st_bit(Bitboard* b) { const Square s = first_1(*b); *b &= ~(1ULL<