From: Marco Costalba Date: Mon, 23 Feb 2009 10:37:31 +0000 (+0100) Subject: Update pinned bitboards and friends in do_move() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=0bf45823da3d76d25d0e3cfdf131bf8823ecb58e;ds=sidebyside Update pinned bitboards and friends in do_move() Probably is slightly slow, but code is surely better in this way. We will optimize later for speed. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/position.cpp b/src/position.cpp index 91fbd103..0a7ebadd 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -206,6 +206,7 @@ void Position::from_fen(const std::string& fen) { castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO; find_checkers(); + find_pinned(); st->key = compute_key(); st->pawnKey = compute_pawn_key(); @@ -319,44 +320,11 @@ void Position::copy(const Position &pos) { } -/// Position:pinned_pieces() returns a bitboard of all pinned (against the -/// king) pieces for the given color. -Bitboard Position::pinned_pieces(Color c) const { - - if (st->pinned[c] != ~EmptyBoardBB) - return st->pinned[c]; - - Bitboard p1, p2; - Square ksq = king_square(c); - st->pinned[c] = hidden_checks(c, ksq, p1) | hidden_checks(c, ksq, p2); - st->pinners[c] = p1 | p2; - return st->pinned[c]; -} - -Bitboard Position::pinned_pieces(Color c, Bitboard& p) const { - - if (st->pinned[c] == ~EmptyBoardBB) - pinned_pieces(c); - - p = st->pinners[c]; - return st->pinned[c]; -} - -Bitboard Position::discovered_check_candidates(Color c) const { - - if (st->dcCandidates[c] != ~EmptyBoardBB) - return st->dcCandidates[c]; - - Bitboard dummy; - Square ksq = king_square(opposite_color(c)); - st->dcCandidates[c] = hidden_checks(c, ksq, dummy) | hidden_checks(c, ksq, dummy); - return st->dcCandidates[c]; -} - /// Position:hidden_checks<>() returns a bitboard of all pinned (against the /// king) pieces for the given color and for the given pinner type. Or, when /// template parameter FindPinned is false, the pinned pieces of opposite color /// that are, indeed, the pieces candidate for a discovery check. +/// Note that checkersBB bitboard must be already updated. template Bitboard Position::hidden_checks(Color c, Square ksq, Bitboard& pinners) const { @@ -466,7 +434,7 @@ bool Position::move_attacks_square(Move m, Square s) const { /// Position::find_checkers() computes the checkersBB bitboard, which -/// contains a nonzero bit for each checking piece (0, 1 or 2). It +/// contains a nonzero bit for each checking piece (0, 1 or 2). It /// currently works by calling Position::attacks_to, which is probably /// inefficient. Consider rewriting this function to use the last move /// played, like in non-bitboard versions of Glaurung. @@ -478,6 +446,25 @@ void Position::find_checkers() { } +/// Position:find_pinned() computes the pinned, pinners and dcCandidates +/// bitboards for both colors. Bitboard checkersBB must be already updated. + +void Position::find_pinned() { + + Bitboard p1, p2; + Square ksq; + + for (Color c = WHITE; c <= BLACK; c++) + { + ksq = king_square(c); + st->pinned[c] = hidden_checks(c, ksq, p1) | hidden_checks(c, ksq, p2); + st->pinners[c] = p1 | p2; + ksq = king_square(opposite_color(c)); + st->dcCandidates[c] = hidden_checks(c, ksq, p1) | hidden_checks(c, ksq, p2); + } +} + + /// Position::pl_move_is_legal() tests whether a pseudo-legal move is legal bool Position::pl_move_is_legal(Move m) const { @@ -719,10 +706,6 @@ void Position::do_move(Move m, StateInfo& newSt) { // case of non-reversible moves is taken care of later. st->rule50++; - // Reset pinned bitboard and its friends - for (Color c = WHITE; c <= BLACK; c++) - st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB; - if (move_is_castle(m)) do_castle_move(m); else if (move_promotion(m)) @@ -823,6 +806,7 @@ void Position::do_move(Move m, StateInfo& newSt) { } // Finish + find_pinned(); st->key ^= zobSideToMove; sideToMove = opposite_color(sideToMove); gamePly++; diff --git a/src/position.h b/src/position.h index 8d1ba359..981bad3e 100644 --- a/src/position.h +++ b/src/position.h @@ -302,6 +302,7 @@ private: void undo_promotion_move(Move m); void undo_ep_move(Move m); void find_checkers(); + void find_pinned(); template void update_checkers(Bitboard* pCheckersBB, Square ksq, Square from, Square to, Bitboard dcCandidates); @@ -556,6 +557,19 @@ inline Bitboard Position::piece_attacks(Square s) const { return StepAttackBB[KING][s]; } +inline Bitboard Position::pinned_pieces(Color c) const { + return st->pinned[c]; +} + +inline Bitboard Position::pinned_pieces(Color c, Bitboard& p) const { + p = st->pinners[c]; + return st->pinned[c]; +} + +inline Bitboard Position::discovered_check_candidates(Color c) const { + return st->dcCandidates[c]; +} + inline Bitboard Position::checkers() const { return st->checkersBB; }