From 32691d2d65d52e2396fc1c5257008408fb678511 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 10 Sep 2025 01:26:42 +0200 Subject: [PATCH] reworked corrhist handling | bench 6409786 --- source/board.cpp | 5 +++++ source/board.h | 1 + source/search.cpp | 6 +++--- source/search.h | 20 +++++++++++++------- source/uci.cpp | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 51dcd8f..d52fa8d 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -32,6 +32,7 @@ void Board::Reset() { hashKey = 0ULL; pawnKey = 0ULL; + nonPawnKey = 0ULL; } void Board::SetByFen(std::string_view fen) { @@ -275,6 +276,8 @@ void Board::SetPiece(int piece, int square, bool color) { if (piece == Pawn) { pawnKey ^= UTILS::zKeys[color][Pawn][square]; + } else { + nonPawnKey ^= UTILS::zKeys[color][piece][square]; } } @@ -287,6 +290,8 @@ void Board::RemovePiece(int piece, int square, bool color) { if (piece == Pawn) { pawnKey ^= UTILS::zKeys[color][Pawn][square]; + } else { + nonPawnKey ^= UTILS::zKeys[color][piece][square]; } } diff --git a/source/board.h b/source/board.h index aa37ff7..e93b491 100644 --- a/source/board.h +++ b/source/board.h @@ -41,6 +41,7 @@ public: U64 hashKey = 0ULL; U64 pawnKey = 0ULL; + U64 nonPawnKey = 0ULL; void Reset(); void SetByFen(std::string_view fen); diff --git a/source/search.cpp b/source/search.cpp index 847b0d9..69d6c97 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -42,11 +42,11 @@ int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int } static int AdjustEval(Board &board, SearchContext* ctx, int eval) { - int pawnHist = ctx->corrhist.GetPawnHist(board); + int corrhist = ctx->corrhist.GetAllHist(board); int mateFound = MATE_SCORE - MAX_PLY; - return std::clamp(eval + pawnHist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1); + return std::clamp(eval + corrhist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1); } static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { @@ -666,7 +666,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT); - ctx->corrhist.UpdatePawnHist(board, depth, corrHistBonus); + ctx->corrhist.UpdateAll(board, depth, corrHistBonus); } ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); diff --git a/source/search.h b/source/search.h index 46fce6c..6e4e88c 100644 --- a/source/search.h +++ b/source/search.h @@ -52,10 +52,9 @@ class CorrHist { private: // indexed by [stm][key] MultiArray pawnHist; + //MultiArray nonPawnHist; public: - void UpdatePawnHist(Board& board, int depth, int diff) { - int* entry = &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]; - + void Update(Board& board, int depth, int diff, int* entry) { const int scaledDiff = diff * CORRHIST_GRAIN; const int newWeight = std::min(depth * depth + 2 * depth + 1, 128); @@ -63,12 +62,19 @@ public: *entry = std::clamp(*entry, -CORRHIST_MAX, CORRHIST_MAX); } - int GetPawnHist(Board& board) { - return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]; + void UpdateAll(Board& board, int depth, int diff) { + Update(board, depth, diff, &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]); + //Update(board, depth, diff, &nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]); } - void ClearPawnHist() { + int GetAllHist(Board& board) { + return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]; + // + nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]; + } + + void Clear() { std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0); + //std::fill(&nonPawnHist[0][0], &nonPawnHist[0][0] + sizeof(nonPawnHist) / sizeof(int), 0); } }; @@ -188,7 +194,7 @@ public: pvLine.Clear(); history.Clear(); conthist.Clear(); - corrhist.ClearPawnHist(); + corrhist.Clear(); TT.Clear(); sw.Restart(); killerMoves = {}; diff --git a/source/uci.cpp b/source/uci.cpp index 4bba4e9..b624a30 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -250,7 +250,7 @@ void UCILoop(Board &board) { ctx->killerMoves = {}; ctx->history.Clear(); ctx->conthist.Clear(); - ctx->corrhist.ClearPawnHist(); + ctx->corrhist.Clear(); ctx->positionHistory = {}; ctx->ss = {};