Skip to content

Commit

Permalink
remove two xors by setting the hash keys for unreachable squares to zero
Browse files Browse the repository at this point in the history
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`)

speedup seems significant, probably around .5%
  • Loading branch information
ces42 committed Jan 27, 2025
1 parent f50d52a commit bbfad96
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Key>();
// 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<Key>();
Expand Down Expand Up @@ -844,8 +847,8 @@ 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];
// Zobrist::psq[pc][to] is zero, so we don't need to clear it
k ^= Zobrist::psq[promotion][to];
st->materialKey ^=
Zobrist::psq[promotion][pieceCount[promotion] - 1] ^ Zobrist::psq[pc][pieceCount[pc]];

Expand Down

0 comments on commit bbfad96

Please sign in to comment.