From e089f723d87e18dc15c95a4628a65db62f44ed9c Mon Sep 17 00:00:00 2001 From: Carlos Esparza Date: Tue, 7 Jan 2025 01:47:37 +0100 Subject: [PATCH] Remove two xors by setting the hash keys for unreachable squares to zero performance before: 3.6714 +- 0.20% Gcycles 3.6620 +- 0.12% Gcycles 3.6704 +- 0.26% Gcycles 3.6602 +- 0.27% Gcycles 3.6799 +- 0.37% Gcycles after: 3.6540 +- 0.30% Gcycles 3.6388 +- 0.25% Gcycles 3.6557 +- 0.17% Gcycles 3.6449 +- 0.15% Gcycles 3.6460 +- 0.26% Gcycles (every line is a different `profile-build` and shows the number of cycles needed for `./stockfish bench`, measured with `perf stat -r 10`) closes https://github.com/official-stockfish/Stockfish/pull/5754 No functional change --- src/position.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 37871aa2..37e9a2eb 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -119,6 +119,9 @@ void Position::init() { for (Piece pc : Pieces) for (Square s = SQ_A1; s <= SQ_H8; ++s) Zobrist::psq[pc][s] = rng.rand(); + // pawns on these squares will promote + std::fill_n(Zobrist::psq[W_PAWN] + SQ_A8, 8, 0); + std::fill_n(Zobrist::psq[B_PAWN], 8, 0); for (File f = FILE_A; f <= FILE_H; ++f) Zobrist::enpassant[f] = rng.rand(); @@ -376,7 +379,7 @@ void Position::set_state() const { for (Piece pc : Pieces) for (int cnt = 0; cnt < pieceCount[pc]; ++cnt) - st->materialKey ^= Zobrist::psq[pc][cnt]; + st->materialKey ^= Zobrist::psq[pc][8 + cnt]; } @@ -776,7 +779,7 @@ void Position::do_move(Move m, remove_piece(capsq); k ^= Zobrist::psq[captured][capsq]; - st->materialKey ^= Zobrist::psq[captured][pieceCount[captured]]; + st->materialKey ^= Zobrist::psq[captured][8 + pieceCount[captured]]; // Reset rule 50 counter st->rule50 = 0; @@ -840,10 +843,10 @@ void Position::do_move(Move m, dp.dirty_num++; // Update hash keys - k ^= Zobrist::psq[pc][to] ^ Zobrist::psq[promotion][to]; - st->pawnKey ^= Zobrist::psq[pc][to]; - st->materialKey ^= - Zobrist::psq[promotion][pieceCount[promotion] - 1] ^ Zobrist::psq[pc][pieceCount[pc]]; + // Zobrist::psq[pc][to] is zero, so we don't need to clear it + k ^= Zobrist::psq[promotion][to]; + st->materialKey ^= Zobrist::psq[promotion][8 + pieceCount[promotion] - 1] + ^ Zobrist::psq[pc][8 + pieceCount[pc]]; if (promotionType <= BISHOP) st->minorPieceKey ^= Zobrist::psq[promotion][to];