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
20 changes: 18 additions & 2 deletions src/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ void History::update_noisy_stats(const Position& pos, Move move, i32 bonus) {
bonus);
}

void History::update_correction_history(const Position& pos, i32 depth, i32 diff) {
static i32* get_cont_corr_slot(i32 ply, const Search::Stack* ss) {
if (ply < 2 || (ss - 2)->cont_corr_entry == nullptr || (ss - 1)->move == Move::none()) {
return nullptr;
}
usize prev_pt = static_cast<usize>((ss - 1)->moved_piece) - static_cast<usize>(PieceType::Pawn);
return &(*(ss - 2)->cont_corr_entry)[prev_pt][(ss - 1)->move.to().raw];
}

void History::update_correction_history(
const Position& pos, i32 depth, i32 diff, i32 ply, const Search::Stack* ss) {
usize side_index = static_cast<usize>(pos.active_color());
u64 pawn_key = pos.get_pawn_key();
u64 white_non_pawn_key = pos.get_non_pawn_key(Color::White);
Expand Down Expand Up @@ -120,9 +129,12 @@ void History::update_correction_history(const Position& pos, i32 depth, i32 diff
update_entry(m_non_pawn_corr_hist[1][side_index][black_non_pawn_index]);
update_entry(m_major_corr_hist[side_index][major_index]);
update_entry(m_minor_corr_hist[side_index][minor_index]);
if (i32* cont_corr = get_cont_corr_slot(ply, ss)) {
update_entry(*cont_corr);
}
}

i32 History::get_correction(const Position& pos) const {
i32 History::get_correction(const Position& pos, i32 ply, const Search::Stack* ss) const {
usize side_index = static_cast<usize>(pos.active_color());
u64 pawn_key = pos.get_pawn_key();
u64 white_non_pawn_key = pos.get_non_pawn_key(Color::White);
Expand All @@ -143,6 +155,9 @@ i32 History::get_correction(const Position& pos) const {
correction += m_non_pawn_corr_hist[1][side_index][black_non_pawn_index];
correction += m_major_corr_hist[side_index][major_index];
correction += m_minor_corr_hist[side_index][minor_index];
if (const i32* cont_corr = get_cont_corr_slot(ply, ss)) {
correction += *cont_corr;
}

return correction / CORRECTION_HISTORY_GRAIN;
}
Expand All @@ -156,6 +171,7 @@ void History::clear() {
std::memset(&m_non_pawn_corr_hist[1], 0, sizeof(CorrectionHistory));
std::memset(&m_major_corr_hist, 0, sizeof(CorrectionHistory));
std::memset(&m_minor_corr_hist, 0, sizeof(CorrectionHistory));
std::memset(&m_cont_corr_hist, 0, sizeof(ContCorrHistory));
}

} // namespace Clockwork
21 changes: 16 additions & 5 deletions src/history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

namespace Clockwork {

using MainHistory = std::array<std::array<std::array<i32, 4>, 4096>, 2>;
using ContHistEntry = std::array<std::array<std::array<i32, 64>, 6>, 2>;
using ContHistory = std::array<std::array<std::array<ContHistEntry, 64>, 6>, 2>;
using MainHistory = std::array<std::array<std::array<i32, 4>, 4096>, 2>;
using ContHistEntry = std::array<std::array<std::array<i32, 64>, 6>, 2>;
using ContHistory = std::array<std::array<std::array<ContHistEntry, 64>, 6>, 2>;
using ContCorrEntry = std::array<std::array<i32, 64>, 6>;
using ContCorrHistory = std::array<std::array<std::array<ContCorrEntry, 64>, 6>, 2>;
// king can't get captured
using CaptHistory = std::array<std::array<std::array<std::array<i32, 64>, 6>, 6>, 2>;
using CorrectionHistory = std::array<std::array<i32, 16384>, 2>;
Expand All @@ -34,16 +36,24 @@
return m_cont_hist[stm_idx][pt_idx][move.to().raw];
}

ContCorrEntry& get_cont_corr_entry(const Position& pos, Move move) {
usize stm_idx = static_cast<usize>(pos.active_color());
PieceType pt = pos.piece_at(move.from());
usize pt_idx = static_cast<usize>(pt) - static_cast<usize>(PieceType::Pawn);
return m_cont_corr_hist[stm_idx][pt_idx][move.to().raw];
}

i32 get_conthist(const Position& pos, Move move, i32 ply, Search::Stack* ss) const;
i32 get_quiet_stats(const Position& pos, Move move, i32 ply, Search::Stack* ss) const;
void update_cont_hist(const Position& pos, Move move, i32 ply, Search::Stack* ss, i32 bonus);
void update_quiet_stats(const Position& pos, Move move, i32 ply, Search::Stack* ss, i32 bonus);

i32 get_noisy_stats(const Position& pos, Move move) const;

Check warning on line 51 in src/history.hpp

View workflow job for this annotation

GitHub Actions / Linter / cpp-linter

src/history.hpp:51:5 [modernize-use-nodiscard]

function 'get_noisy_stats' should be marked [[nodiscard]]
void update_noisy_stats(const Position& pos, Move move, i32 bonus);

void update_correction_history(const Position& pos, i32 depth, i32 diff);
i32 get_correction(const Position& pos) const;
void update_correction_history(
const Position& pos, i32 depth, i32 diff, i32 ply, const Search::Stack* ss);
i32 get_correction(const Position& pos, i32 ply, const Search::Stack* ss) const;

void clear();

Expand All @@ -64,6 +74,7 @@
std::array<CorrectionHistory, 2> m_non_pawn_corr_hist = {};
CorrectionHistory m_major_corr_hist = {};
CorrectionHistory m_minor_corr_hist = {};
ContCorrHistory m_cont_corr_hist = {};
};

} // namespace Clockwork
29 changes: 24 additions & 5 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,10 @@ Value Worker::search(
if (alpha < 0 && repetition_info.has_game_cycle(pos, static_cast<usize>(ply))) {
if (!is_in_check) {
auto raw_eval = evaluate(pos);
auto correction = m_td.history.get_correction(pos);
auto correction = m_td.history.get_correction(pos, ply, ss);
auto eval = adj_shuffle(pos, raw_eval) + correction;
if (eval < 0) {
m_td.history.update_correction_history(pos, depth, -eval);
m_td.history.update_correction_history(pos, depth, -eval, ply, ss);
}
}

Expand Down Expand Up @@ -616,7 +616,7 @@ Value Worker::search(
Value raw_eval = -VALUE_INF;
ss->static_eval = -VALUE_INF;
if (!is_in_check) {
correction = excluded ? 0 : m_td.history.get_correction(pos);
correction = excluded ? 0 : m_td.history.get_correction(pos, ply, ss);
raw_eval = tt_data && !is_decisive_score(tt_data->eval) ? tt_data->eval : evaluate(pos);
ss->static_eval = adj_shuffle(pos, raw_eval) + correction;
improving =
Expand Down Expand Up @@ -711,6 +711,9 @@ 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);
ss->move = m;
ss->moved_piece = pos.piece_at(m.from());
ss->cont_corr_entry = &m_td.history.get_cont_corr_entry(pos, m);
Position pos_after = pos.move(m, m_td.push_psqt_state(), &m_searcher.tt);
repetition_info.push(pos_after.get_hash_key(), pos.is_reversible(m));

Expand All @@ -726,6 +729,9 @@ Value Worker::search(
repetition_info.pop();
m_td.pop_psqt_state();
ss->cont_hist_entry = nullptr;
ss->move = Move::none();
ss->moved_piece = PieceType::None;
ss->cont_corr_entry = nullptr;

if (probcut_value >= probcut_beta) {
m_searcher.tt.store(pos, ply, raw_eval, m, probcut_value, probcut_depth, false,
Expand Down Expand Up @@ -878,6 +884,9 @@ Value Worker::search(

// Do move
ss->cont_hist_entry = &m_td.history.get_cont_hist_entry(pos, m);
ss->move = m;
ss->moved_piece = pos.piece_at(m.from());
ss->cont_corr_entry = &m_td.history.get_cont_corr_entry(pos, m);

Position pos_after = pos.move(m, m_td.push_psqt_state(), &m_searcher.tt);
moves_played++;
Expand Down Expand Up @@ -986,6 +995,9 @@ Value Worker::search(
repetition_info.pop();
m_td.pop_psqt_state();
ss->cont_hist_entry = nullptr;
ss->move = Move::none();
ss->moved_piece = PieceType::None;
ss->cont_corr_entry = nullptr;

if (m_stopped) {
return 0;
Expand Down Expand Up @@ -1099,7 +1111,8 @@ Value Worker::search(
&& !(best_move != Move::none() && (best_move.is_capture() || best_move.is_promotion()))
&& !((bound == Bound::Lower && best_value <= ss->static_eval)
|| (bound == Bound::Upper && best_value >= ss->static_eval))) {
m_td.history.update_correction_history(pos, depth, best_value - ss->static_eval);
m_td.history.update_correction_history(pos, depth, best_value - ss->static_eval, ply,
ss);
}
}

Expand Down Expand Up @@ -1166,7 +1179,7 @@ Value Worker::quiesce(const Position& pos, Stack* ss, Value alpha, Value beta, i
Value raw_eval = -VALUE_INF;
Value static_eval = -VALUE_INF;
if (!is_in_check) {
correction = m_td.history.get_correction(pos);
correction = m_td.history.get_correction(pos, ply, ss);
raw_eval = tt_data && !is_decisive_score(tt_data->eval) ? tt_data->eval : evaluate(pos);
static_eval = adj_shuffle(pos, raw_eval) + correction;

Expand Down Expand Up @@ -1204,6 +1217,9 @@ 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);
ss->move = m;
ss->moved_piece = pos.piece_at(m.from());
ss->cont_corr_entry = &m_td.history.get_cont_corr_entry(pos, m);
Position pos_after = pos.move(m, m_td.push_psqt_state(), &m_searcher.tt);
moves_searched++;

Expand All @@ -1220,6 +1236,9 @@ Value Worker::quiesce(const Position& pos, Stack* ss, Value alpha, Value beta, i
repetition_info.pop();
m_td.pop_psqt_state();
ss->cont_hist_entry = nullptr;
ss->move = Move::none();
ss->moved_piece = PieceType::None;
ss->cont_corr_entry = nullptr;

if (m_stopped) {
return 0;
Expand Down
3 changes: 3 additions & 0 deletions src/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ struct Stack {
Move killer = Move::none();
Move excluded_move;
ContHistEntry* cont_hist_entry = nullptr;
Move move = Move::none();
PieceType moved_piece = PieceType::None;
ContCorrEntry* cont_corr_entry = nullptr;
i32 fail_high_count = 0;
PV pv;
};
Expand Down
Loading