Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ set(srcs
src/util/bit.hpp
src/util/parse.hpp
src/util/pretty.hpp
src/util/large_pages.hpp
src/util/mem.hpp
src/util/static_vector.hpp
src/util/types.hpp
src/util/vec/sse2.hpp
Expand Down
12 changes: 9 additions & 3 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "common.hpp"
#include "geometry.hpp"
#include "psqt_state.hpp"
#include "util/mem.hpp"
#include "util/parse.hpp"
#include "util/types.hpp"
#include "zobrist.hpp"
Expand Down Expand Up @@ -366,7 +367,7 @@ void Position::add_attacks(bool color, PieceId id, Square sq, PieceType ptype, m
}

template<bool UPDATE_PSQT>
Position Position::move(Move m, PsqtState* psqtState) const {
Position Position::move(Move m, PsqtState* psqtState, const TT* tt) const {
Position new_pos = *this;
PsqtUpdates updates{};

Expand Down Expand Up @@ -514,6 +515,11 @@ Position Position::move(Move m, PsqtState* psqtState) const {
new_pos.m_rook_info[0].as_index() | (new_pos.m_rook_info[1].as_index() << 2);
new_pos.m_hash_key ^= Zobrist::castling_zobrist[new_castle_index];

// Prefetch hash key tt entry
if (tt != nullptr) {
prefetch(tt->addr_key(new_pos.m_hash_key));
}

new_pos.m_active_color = invert(m_active_color);
new_pos.m_ply++;

Expand All @@ -524,8 +530,8 @@ Position Position::move(Move m, PsqtState* psqtState) const {
return new_pos;
}

template Position Position::move<true>(Move m, PsqtState* psqtState) const;
template Position Position::move<false>(Move m, PsqtState* psqtState) const;
template Position Position::move<true>(Move m, PsqtState* psqtState, const TT* tt = nullptr) const;
template Position Position::move<false>(Move m, PsqtState* psqtState, const TT* tt = nullptr) const;

Position Position::null_move() const {
Position new_pos = *this;
Expand Down
16 changes: 11 additions & 5 deletions src/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "board.hpp"
#include "move.hpp"
#include "square.hpp"
#include "tt.hpp"
#include "util/types.hpp"
#include <array>
#include <bit>
Expand All @@ -12,6 +13,8 @@

namespace Clockwork {

class TT;

struct PsqtState;
struct PsqtUpdates;

Expand Down Expand Up @@ -259,15 +262,18 @@ struct Position {
}

template<bool UPDATE_PSQT>
[[nodiscard]] Position move(Move m, PsqtState* psqt_state) const;
[[nodiscard]] Position null_move() const;

[[nodiscard]] Position move(Move m) const {
return move<false>(m, nullptr);
[[nodiscard]] Position move(Move m, PsqtState* psqt_state, const TT* tt = nullptr) const;
[[nodiscard]] Position move(Move m, PsqtState& psqt_state, const TT* tt = nullptr) const {
return move<true>(m, &psqt_state, tt);
}
[[nodiscard]] Position move(Move m, PsqtState& psqt_state) const {
return move<true>(m, &psqt_state);
}
[[nodiscard]] Position move(Move m) const {
return move<false>(m, nullptr);
}

[[nodiscard]] Position null_move() const;

[[nodiscard]] std::tuple<Wordboard, Bitboard> calc_pin_mask() const;

Expand Down
8 changes: 4 additions & 4 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "tm.hpp"
#include "tuned.hpp"
#include "uci.hpp"
#include "util/large_pages.hpp"
#include "util/log2.hpp"
#include "util/mem.hpp"
#include "util/types.hpp"
#include <algorithm>
#include <array>
Expand Down Expand Up @@ -555,7 +555,7 @@ Value Worker::search(
for (Move m = moves.next(); m != Move::none(); m = moves.next()) {

ss->cont_hist_entry = &m_td.history.get_cont_hist_entry(pos, m);
Position pos_after = pos.move(m, m_td.push_psqt_state());
Position pos_after = pos.move(m, m_td.push_psqt_state(), &m_searcher.tt);
repetition_info.push(pos_after.get_hash_key(), pos_after.is_reversible(m));

Value probcut_value =
Expand Down Expand Up @@ -708,7 +708,7 @@ Value Worker::search(
// Do move
ss->cont_hist_entry = &m_td.history.get_cont_hist_entry(pos, m);

Position pos_after = pos.move(m, m_td.push_psqt_state());
Position pos_after = pos.move(m, m_td.push_psqt_state(), &m_searcher.tt);
moves_played++;

// Put hash into repetition table. TODO: encapsulate this and any other future adjustment to do "on move" into a proper function
Expand Down Expand Up @@ -988,7 +988,7 @@ Value Worker::quiesce(const Position& pos, Stack* ss, Value alpha, Value beta, i

// Do move
ss->cont_hist_entry = &m_td.history.get_cont_hist_entry(pos, m);
Position pos_after = pos.move(m, m_td.push_psqt_state());
Position pos_after = pos.move(m, m_td.push_psqt_state(), &m_searcher.tt);
moves_searched++;

// If we've found a legal move, then we can begin skipping quiet moves.
Expand Down
5 changes: 5 additions & 0 deletions src/tt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ std::optional<TTData> TT::probe(const Position& pos, i32 ply) const {
return {};
}

TTClusterMemory* TT::addr_key(const u64 key) const {
size_t idx = mulhi64(key, m_size);
return &this->m_clusters[idx];
}

void TT::store(const Position& pos,
i32 ply,
Value eval,
Expand Down
4 changes: 3 additions & 1 deletion src/tt.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "position.hpp"
#include "util/large_pages.hpp"
#include "util/mem.hpp"
#include <array>
#include <atomic>
#include <bit>
Expand Down Expand Up @@ -105,6 +105,8 @@ class TT {
void clear();
void increment_age();
i32 hashfull() const;
TTClusterMemory* addr_key(const u64 key) const;


private:
unique_ptr_huge_page<TTClusterMemory[]> m_clusters;
Expand Down
8 changes: 8 additions & 0 deletions src/util/large_pages.hpp → src/util/mem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <windows.h>
#endif

// Large page allocation utilities

template<typename T>
using unique_ptr_huge_page =
std::conditional_t<std::is_array_v<T>,
Expand Down Expand Up @@ -154,3 +156,9 @@ unique_ptr_huge_page<T> make_unique_for_overwrite_huge_page(std::size_t n) {
template<class T, class... Args>
requires std::is_bounded_array_v<T>
void make_unique_for_overwrite_huge_page(Args&&...) = delete;


// Prefetching utilities
inline void prefetch(const void* ptr) {
__builtin_prefetch(ptr);
}
Loading