From: Dan Schmidt Date: Sat, 3 Aug 2013 12:42:58 +0000 (-0400) Subject: Refactor do_castle() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=f7096ea7cedeb81d9799e4440a670736825c6a7e Refactor do_castle() Not a real functional change, but bench changed due to different piecelist reordering. To verify it a temporary my canonicalize_rooks function was written as follows. It just ensures that the rook on the "smaller" square is listed first. void Position::canonicalize_rooks(Color c) { if (pieceCount[c][ROOK] == 2) { Square s0 = pieceList[c][ROOK][0]; Square s1 = pieceList[c][ROOK][1]; if (s0 > s1) { pieceList[c][ROOK][0] = s1; pieceList[c][ROOK][1] = s0; index[s0] = 1; index[s1] = 0; } } } With this both bench and the test on Chess960 positions ./stockfish bench 128 1 8 Chess960.epd file > /dev/null Gives same result. bench: 4424151 --- diff --git a/src/position.cpp b/src/position.cpp index 1e0f46b7..98ba5c60 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -989,25 +989,12 @@ void Position::undo_move(Move m) { void Position::do_castle(Square kfrom, Square kto, Square rfrom, Square rto) { - Color us = sideToMove; - Bitboard k_from_to_bb = SquareBB[kfrom] ^ SquareBB[kto]; - Bitboard r_from_to_bb = SquareBB[rfrom] ^ SquareBB[rto]; - byTypeBB[KING] ^= k_from_to_bb; - byTypeBB[ROOK] ^= r_from_to_bb; - byTypeBB[ALL_PIECES] ^= k_from_to_bb ^ r_from_to_bb; - byColorBB[us] ^= k_from_to_bb ^ r_from_to_bb; - - // Could be from == to, so first set NO_PIECE then KING and ROOK - board[kfrom] = board[rfrom] = NO_PIECE; - board[kto] = make_piece(us, KING); - board[rto] = make_piece(us, ROOK); - - // Could be kfrom == rto, so use a 'tmp' variable - int tmp = index[kfrom]; - index[rto] = index[rfrom]; - index[kto] = tmp; - pieceList[us][KING][index[kto]] = kto; - pieceList[us][ROOK][index[rto]] = rto; + // Remove both pieces first since squares could overlap in Chess960 + remove_piece(kfrom, sideToMove, KING); + remove_piece(rfrom, sideToMove, ROOK); + board[kfrom] = board[rfrom] = NO_PIECE; // Since remove_piece doesn't do it for us + put_piece(kto, sideToMove, KING); + put_piece(rto, sideToMove, ROOK); }