X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=cc0f42e9c9ac1b004938a29968b554001e75c9f6;hp=91fbd103a739ccfd84b5fec8717ec90b0e1b4858;hb=243fa483d7b329ad8512c86eb152cca7a0982674;hpb=4324276419caf3e1caf7012ebbf47192e9d94648 diff --git a/src/position.cpp b/src/position.cpp index 91fbd103..cc0f42e9 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -23,8 +23,9 @@ //// #include -#include +#include #include +#include #include "mersenne.h" #include "movegen.h" @@ -206,6 +207,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 +321,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 +435,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 +447,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 { @@ -689,6 +677,25 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square } +/// Position::init_new_state() copies from the current state the fields +/// that will be updated incrementally, skips the fields, like bitboards +/// that will be recalculated form scratch anyway. + +void Position::init_new_state(StateInfo& newSt) { + + newSt.key = st->key; + newSt.pawnKey = st->pawnKey; + newSt.materialKey = st->materialKey; + newSt.castleRights = st->castleRights; + newSt.rule50 = st->rule50; + newSt.epSquare = st->epSquare; + newSt.mgValue = st->mgValue; + newSt.egValue = st->egValue; + newSt.capture = NO_PIECE_TYPE; + newSt.previous = st; +} + + /// Position::do_move() makes a move, and saves all information necessary /// to a StateInfo object. The move is assumed to be legal. /// Pseudo-legal moves should be filtered out before this function is called. @@ -698,17 +705,14 @@ void Position::do_move(Move m, StateInfo& newSt) { assert(is_ok()); assert(move_is_ok(m)); - // Get now the current (pre-move) dc candidates that we will use + // Get now the current (before to move) dc candidates that we will use // in update_checkers(). Bitboard oldDcCandidates = discovered_check_candidates(side_to_move()); - // Copy the old state to our new StateInfo object (except the - // captured piece, which is taken care of later. - // TODO do not copy pinners and checkersBB because are recalculated - // anyway. - newSt = *st; - newSt.capture = NO_PIECE_TYPE; - newSt.previous = st; + // Copy some fields of old state to our new StateInfo object (except the + // captured piece, which is taken care of later) and switch state pointer + // to point to the new, ready to be updated, state. + init_new_state(newSt); st = &newSt; // Save the current key to the history[] array, in order to be able to @@ -719,10 +723,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 +823,7 @@ void Position::do_move(Move m, StateInfo& newSt) { } // Finish + find_pinned(); st->key ^= zobSideToMove; sideToMove = opposite_color(sideToMove); gamePly++; @@ -1645,16 +1646,14 @@ int Position::see(Square from, Square to) const { void Position::clear() { st = &startState; - st->previous = NULL; // We should never dereference this + memset(st, 0, sizeof(StateInfo)); + st->epSquare = SQ_NONE; + + memset(index, 0, sizeof(int) * 64); + memset(byColorBB, 0, sizeof(Bitboard) * 2); for (int i = 0; i < 64; i++) - { board[i] = EMPTY; - index[i] = 0; - } - - for (int i = 0; i < 2; i++) - byColorBB[i] = EmptyBoardBB; for (int i = 0; i < 7; i++) { @@ -1664,21 +1663,11 @@ void Position::clear() { pieceList[0][i][j] = pieceList[1][i][j] = SQ_NONE; } - st->checkersBB = EmptyBoardBB; - for (Color c = WHITE; c <= BLACK; c++) - st->pinners[c] = st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB; - sideToMove = WHITE; gamePly = 0; initialKFile = FILE_E; initialKRFile = FILE_H; initialQRFile = FILE_A; - - st->lastMove = MOVE_NONE; - st->castleRights = NO_CASTLES; - st->epSquare = SQ_NONE; - st->rule50 = 0; - st->previous = NULL; }