diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 517859d..1f74ec4 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -69,7 +69,8 @@ void RunBenchmark() { // Clearing TT.Clear(); std::memset(killerMoves, 0, sizeof(killerMoves)); - std::memset(historyMoves, 0, sizeof(historyMoves)); + + history.Clear(); nodes = 0; diff --git a/source/search.cpp b/source/search.cpp index b0f7e22..10364b2 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -38,7 +38,7 @@ static int ScoreMove(Board &board, Move &move, int ply) { return 18000; } else { // Max 16384 - return historyMoves[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + return history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; } } @@ -265,8 +265,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { killerMoves[1][ply] = killerMoves[0][ply]; killerMoves[0][ply] = currMove; - historyMoves[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()] = - std::min(16384, historyMoves[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()] + depth * depth); + history.Update(board.sideToMove, currMove, depth * depth); } TT.WriteEntry(board.hashKey, depth, score, CutNode, Move()); diff --git a/source/search.h b/source/search.h index 93a7ca9..55cb9bb 100644 --- a/source/search.h +++ b/source/search.h @@ -3,8 +3,10 @@ #include "uci.h" #include #include "move.h" +#include constexpr int maxDepth = 64; +constexpr int maxHistory = 16384; extern U64 nodes; extern bool benchStarted; @@ -15,8 +17,6 @@ inline bool searchStopped = false; // [id][ply] inline int killerMoves[2][64]; -// [stm][from][to] -inline int historyMoves[2][64][64]; class SearchResults { public: @@ -35,6 +35,33 @@ public: } }; +class History { +private: + int historyMoves[2][64][64]; +public: + void Update(bool stm, Move move, int bonus) { + int clampedBonus = std::clamp(bonus, -maxHistory, maxHistory); + historyMoves[stm][move.MoveFrom()][move.MoveTo()] += + clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / maxHistory; + } + + void Clear() { + for (auto &side : historyMoves) { + for (auto &from : side) { + for (auto &to : from) { + to = 0; + } + } + } + } + + auto& operator[](int index) { + return historyMoves[index]; + } +}; + +inline History history; + class PVLine { private: int length[maxDepth] = {}; diff --git a/source/uci.cpp b/source/uci.cpp index 7c59ae8..9b308d6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -119,7 +119,8 @@ void UCILoop(Board &board) { // Clearing TT.Clear(); std::memset(killerMoves, 0, sizeof(killerMoves)); - std::memset(historyMoves, 0, sizeof(historyMoves)); + + history.Clear(); positionIndex = 0; continue;