X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitboard.h;h=ed7e318ca3198ed970aa9508752bf10e1e7d4ded;hp=38870a2be69fbd8a33ca8a50e610db4a29dd7c86;hb=69eb391cd779223c331fb0de80392cbd323055a9;hpb=27ba611a3da37423a3502e49beeebe11c9a11d8e diff --git a/src/bitboard.h b/src/bitboard.h index 38870a2b..ed7e318c 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -82,6 +82,20 @@ struct Magic { Bitboard magic; Bitboard* attacks; unsigned shift; + + /// looks up the index using the 'magic bitboards' approach. + unsigned index(Bitboard occupied) const { + + if (HasPext) + return unsigned(pext(occupied, mask)); + + if (Is64Bit) + return unsigned(((occupied & mask) * magic) >> shift); + + unsigned lo = unsigned(occupied) & unsigned(mask); + unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32); + return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift; + } }; extern Magic RookMagics[SQUARE_NB]; @@ -186,7 +200,7 @@ inline Bitboard forward_bb(Color c, Square s) { /// pawn_attack_span() returns a bitboard representing all the squares that can be /// attacked by a pawn of the given color when it moves along its file, starting /// from the given square: -/// PawnAttackSpan[c][s] = in_front_bb(c, rank_of(s)) & adjacent_files_bb(s); +/// PawnAttackSpan[c][s] = in_front_bb(c, rank_of(s)) & adjacent_files_bb(file_of(s)); inline Bitboard pawn_attack_span(Color c, Square s) { return PawnAttackSpan[c][s]; @@ -222,26 +236,13 @@ template<> inline int distance(Square x, Square y) { return distance(rank_ /// attacks_bb() returns a bitboard representing all the squares attacked by a -/// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index() -/// looks up the index using the 'magic bitboards' approach. -inline unsigned magic_index(const Magic& m, Bitboard occupied) { - - if (HasPext) - return unsigned(pext(occupied, m.mask)); - - if (Is64Bit) - return unsigned(((occupied & m.mask) * m.magic) >> m.shift); - - unsigned lo = unsigned(occupied) & unsigned(m.mask); - unsigned hi = unsigned(occupied >> 32) & unsigned(m.mask >> 32); - return (lo * unsigned(m.magic) ^ hi * unsigned(m.magic >> 32)) >> m.shift; -} +/// piece of type Pt (bishop or rook) placed on 's'. template inline Bitboard attacks_bb(Square s, Bitboard occupied) { const Magic& M = Pt == ROOK ? RookMagics[s] : BishopMagics[s]; - return M.attacks[magic_index(M, occupied)]; + return M.attacks[M.index(occupied)]; } inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) { @@ -251,7 +252,7 @@ inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) { switch (pt) { case BISHOP: return attacks_bb(s, occupied); - case ROOK : return attacks_bb(s, occupied); + case ROOK : return attacks_bb< ROOK>(s, occupied); case QUEEN : return attacks_bb(s, occupied) | attacks_bb(s, occupied); default : return PseudoAttacks[pt][s]; }