From fd5d6c53402517cf8277641a8e37af153b7540c5 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 30 Oct 2011 10:57:00 +0100 Subject: [PATCH] Retire do_capture_move() It is called only in do_move() that now has been fully expanded. This is the most time consuming function of all the engine. No functional change. Signed-off-by: Marco Costalba --- src/position.cpp | 124 ++++++++++++++++++++++------------------------- src/position.h | 1 - 2 files changed, 58 insertions(+), 67 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index a1823c12..397c87dd 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -781,11 +781,66 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI assert(color_of(piece_on(from)) == us); assert(color_of(piece_on(to)) == them || square_is_empty(to)); - assert(!(ep || pm) || piece == make_piece(us, PAWN)); - assert(!pm || relative_rank(us, to) == RANK_8); + assert(capture != KING); if (capture) - do_capture_move(key, capture, them, to, ep); + { + Square capsq = to; + + // If the captured piece was a pawn, update pawn hash key, otherwise + // update non-pawn material. + if (capture == PAWN) + { + if (ep) // En passant? + { + capsq += pawn_push(them); + + assert(pt == PAWN); + assert(to == st->epSquare); + assert(relative_rank(us, to) == RANK_6); + assert(piece_on(to) == PIECE_NONE); + assert(piece_on(capsq) == make_piece(them, PAWN)); + + board[capsq] = PIECE_NONE; + } + + st->pawnKey ^= zobrist[them][PAWN][capsq]; + } + else + st->npMaterial[them] -= PieceValueMidgame[capture]; + + // Remove captured piece + clear_bit(&byColorBB[them], capsq); + clear_bit(&byTypeBB[capture], capsq); + clear_bit(&byTypeBB[0], capsq); + + // Update hash key + key ^= zobrist[them][capture][capsq]; + + // Update incremental scores + st->value -= pst(make_piece(them, capture), capsq); + + // Update piece count + pieceCount[them][capture]--; + + // Update material hash key + st->materialKey ^= zobrist[them][capture][pieceCount[them][capture]]; + + // Update piece list, move the last piece at index[capsq] position + // + // WARNING: This is a not perfectly revresible operation. When we + // will reinsert the captured piece in undo_move() we will put it + // at the end of the list and not in its original place, it means + // index[] and pieceList[] are not guaranteed to be invariant to a + // do_move() + undo_move() sequence. + Square lastPieceSquare = pieceList[them][capture][pieceCount[them][capture]]; + index[lastPieceSquare] = index[capsq]; + pieceList[them][capture][index[lastPieceSquare]] = lastPieceSquare; + pieceList[them][capture][pieceCount[them][capture]] = SQ_NONE; + + // Reset rule 50 counter + st->rule50 = 0; + } // Update hash key key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to]; @@ -930,69 +985,6 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI } -/// Position::do_capture_move() is a private method used to update captured -/// piece info. It is called from the main Position::do_move function. - -void Position::do_capture_move(Key& key, PieceType capture, Color them, Square to, bool ep) { - - assert(capture != KING); - - Square capsq = to; - - // If the captured piece was a pawn, update pawn hash key, - // otherwise update non-pawn material. - if (capture == PAWN) - { - if (ep) // en passant ? - { - capsq = to + pawn_push(them); - - assert(to == st->epSquare); - assert(relative_rank(flip(them), to) == RANK_6); - assert(piece_on(to) == PIECE_NONE); - assert(piece_on(capsq) == make_piece(them, PAWN)); - - board[capsq] = PIECE_NONE; - } - st->pawnKey ^= zobrist[them][PAWN][capsq]; - } - else - st->npMaterial[them] -= PieceValueMidgame[capture]; - - // Remove captured piece - clear_bit(&byColorBB[them], capsq); - clear_bit(&byTypeBB[capture], capsq); - clear_bit(&byTypeBB[0], capsq); - - // Update hash key - key ^= zobrist[them][capture][capsq]; - - // Update incremental scores - st->value -= pst(make_piece(them, capture), capsq); - - // Update piece count - pieceCount[them][capture]--; - - // Update material hash key - st->materialKey ^= zobrist[them][capture][pieceCount[them][capture]]; - - // Update piece list, move the last piece at index[capsq] position - // - // WARNING: This is a not perfectly revresible operation. When we - // will reinsert the captured piece in undo_move() we will put it - // at the end of the list and not in its original place, it means - // index[] and pieceList[] are not guaranteed to be invariant to a - // do_move() + undo_move() sequence. - Square lastPieceSquare = pieceList[them][capture][pieceCount[them][capture]]; - index[lastPieceSquare] = index[capsq]; - pieceList[them][capture][index[lastPieceSquare]] = lastPieceSquare; - pieceList[them][capture][pieceCount[them][capture]] = SQ_NONE; - - // Reset rule 50 counter - st->rule50 = 0; -} - - /// Position::do_castle_move() is a private method used to do/undo a castling /// move. Note that castling moves are encoded as "king captures friendly rook" /// moves, for instance white short castling in a non-Chess960 game is encoded diff --git a/src/position.h b/src/position.h index 1bb2e388..8af624ca 100644 --- a/src/position.h +++ b/src/position.h @@ -217,7 +217,6 @@ private: bool move_is_legal(const Move m) const; // Helper functions for doing and undoing moves - void do_capture_move(Key& key, PieceType capture, Color them, Square to, bool ep); template void do_castle_move(Move m); template -- 2.39.2