Skip to content

Commit

Permalink
Add TT prefetch
Browse files Browse the repository at this point in the history
Use a prefetch operation to speedup loading a value from the TT.
To do so, a small utility for computing the hash after a move has been added, along with its tests.

Tested for speedup
STC:
LLR:  2.99/2.94<0.00, 10.00> Elo diff: 13.30 [5.12, 21.49] (95%)
Games: 1568 W: 226 L: 166 D: 1176 Draw ratio: 75.0%
Pntl: [3, 128, 471, 170, 12]

No functional change
  • Loading branch information
ruicoelhopedro committed Jan 18, 2024
1 parent 8c912f0 commit 49706f3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ class HashTable
{
return m_full * 1000 / m_table.size();
}

void prefetch(Hash hash)
{
__builtin_prefetch(&m_table[index(hash)]);
}
};


Expand Down
69 changes: 69 additions & 0 deletions src/Position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,69 @@ Board Board::make_move(Move move) const
}


Hash Board::hash_after(Move move) const
{
Hash result = hash() ^ Zobrist::get_black_move();

// Moved piece (and handle promotions)
PieceType piece = get_piece_at(move.from());
PieceType target_piece = move.is_promotion() ? move.promo_piece() : piece;
result ^= Zobrist::get_piece_turn_square(piece, turn(), move.from());
result ^= Zobrist::get_piece_turn_square(target_piece, turn(), move.to());

// Handle special moves
if (move.is_capture())
{
// Remove captured piece, if any
Square target = move.is_ep_capture() ? move.to() - (turn() == WHITE ? 8 : -8) : move.to();
PieceType captured = get_piece_at(target);
result ^= Zobrist::get_piece_turn_square(captured, ~turn(), target);

// Castling: check if any rook has been captured
if (captured == ROOK)
for (auto side : { KINGSIDE, QUEENSIDE })
if (castling_rights(side, ~turn()) && move.to() == get_rook_square(m_castling_rights[side][~turn()], ~turn()))
result ^= Zobrist::get_castle_side_turn(side, ~turn());
}
else if (move.is_double_pawn_push())
{
// Update ep square
result ^= Zobrist::get_ep_file(file(move.to()));
}
else if (move.is_castle())
{
// Castling: update the hash of the rook
CastleSide side = file(move.to()) >= 4 ? KINGSIDE : QUEENSIDE;
Square iS = get_rook_square(m_castling_rights[side][turn()], turn());
Square iE = move.to() + (side == KINGSIDE ? -1 : +1);
result ^= Zobrist::get_piece_turn_square(ROOK, turn(), iS);
result ^= Zobrist::get_piece_turn_square(ROOK, turn(), iE);
}

// Reset castling rights after a king or rook move
if (piece == KING)
{
// Unset all castling rights after a king move
for (auto side : { KINGSIDE, QUEENSIDE })
if (castling_rights(side, turn()))
result ^= Zobrist::get_castle_side_turn(side, turn());
}
else if (piece == ROOK)
{
// Unset castling rights for a certain side if a rook moves
for (auto side : { KINGSIDE, QUEENSIDE })
if (castling_rights(side, turn()) && move.from() == get_rook_square(m_castling_rights[side][turn()], turn()))
result ^= Zobrist::get_castle_side_turn(side, turn());
}

// Reset previous en-passant hash
if (m_enpassant_square != SQUARE_NULL)
result ^= Zobrist::get_ep_file(file(m_enpassant_square));

return result;
}


bool Board::is_valid() const
{
// Side not to move in check?
Expand Down Expand Up @@ -915,6 +978,12 @@ Move Position::last_move(std::size_t offset) const
}


const Board& Position::last_board() const
{
return m_boards[m_boards.size() - 2];
}


std::ostream& operator<<(std::ostream& out, const Board& board)
{
out << " +------------------------+\n";
Expand Down
6 changes: 6 additions & 0 deletions src/Position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ class Board
void generate_moves(MoveList& list, MoveGenType type) const;


Hash hash_after(Move move) const;


inline void set_piece(PieceType piece, Turn turn, Square square)
{
m_pieces[piece][turn].set(square);
Expand Down Expand Up @@ -756,6 +759,9 @@ class Position


Move last_move(std::size_t offset = 0) const;


const Board& last_board() const;
};


Expand Down
4 changes: 4 additions & 0 deletions src/Search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ namespace Search
}
}

ttable.prefetch(position.board().hash_after(move));

// Make the move
Score score = -SCORE_INFINITE;
bool captureOrPromotion = move.is_capture() || move.is_promotion();
Expand Down Expand Up @@ -825,6 +827,8 @@ namespace Search
if (!InCheck && position.board().see(move) < 0)
continue;

ttable.prefetch(position.board().hash_after(move));

// PVS
Score score;
position.make_move(move);
Expand Down
4 changes: 4 additions & 0 deletions src/Search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ namespace Search
int64_t n_nodes = 0;
auto move_list = position.generate_moves(MoveGenType::LEGAL);

// Hash update
if (VALIDITY && position.last_move() != MOVE_NULL && position.last_board().hash_after(position.last_move()) != position.hash())
return 0;

// Move counting
if (USE_ORDER)
{
Expand Down

0 comments on commit 49706f3

Please sign in to comment.