From edfa6ead28a2025338277bf940540ed433a4941b Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 3 Oct 2026 17:16:16 +0200 Subject: [PATCH] storing score into tt correctly on mate | bench 1632393 --- source/search.cpp | 14 +++++++++----- source/search.h | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 02d709a..524d46b 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -302,6 +302,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear if (entry.hashKey == board.hashKey) { ttHit = true; + entry.score = ScoreFromTT(entry.score, ply); switch (entry.nodeType) { case PV: @@ -387,7 +388,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear if (score >= beta) { if (searchStopped.load(std::memory_order_relaxed)) return 0; - ctx->TT->WriteEntry(board.hashKey, 0, score, CutNode, currMove, ttpv); + ctx->TT->WriteEntry(board.hashKey, 0, ScoreToTT(score, ply), CutNode, currMove, ttpv); return score; } @@ -402,7 +403,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear results.score = bestScore; if (searchStopped.load(std::memory_order_relaxed)) return 0; - ctx->TT->WriteEntry(board.hashKey, 0, results.score, nodeType, results.bestMove, ttpv); + ctx->TT->WriteEntry(board.hashKey, 0, ScoreToTT(results.score, ply), nodeType, results.bestMove, ttpv); return results; } @@ -426,6 +427,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC const bool ttHit = entry.hashKey == board.hashKey; const bool ttpv = isPV | entry.ttpv; + if (ttHit) + entry.score = ScoreFromTT(entry.score, ply); + if constexpr (!isPV) { if (ttHit) { if (entry.depth >= depth && @@ -555,7 +559,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (searchStopped.load(std::memory_order_relaxed)) return 0; if (score >= probcutBeta) { - ctx->TT->WriteEntry(board.hashKey, probcutDepth, score, CutNode, currMove, ttpv); + ctx->TT->WriteEntry(board.hashKey, probcutDepth, ScoreToTT(score, ply), CutNode, currMove, ttpv); return score; } @@ -784,7 +788,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } if (!ctx->excluded) - ctx->TT->WriteEntry(board.hashKey, depth, score, CutNode, currMove, ttpv); + ctx->TT->WriteEntry(board.hashKey, depth, ScoreToTT(score, ply), CutNode, currMove, ttpv); return score; } } @@ -805,7 +809,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->corrhist.UpdateAll(board, depth, corrHistBonus); } - ctx->TT->WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove, ttpv); + ctx->TT->WriteEntry(board.hashKey, depth, ScoreToTT(results.score, ply), nodeType, results.bestMove, ttpv); } return results; } diff --git a/source/search.h b/source/search.h index 37ec93a..2245f0c 100644 --- a/source/search.h +++ b/source/search.h @@ -34,6 +34,22 @@ constexpr int CORRHIST_MAX = 16384; constexpr int32_t ScoreNone = -255000; constexpr int inf = 100000; +inline int ScoreToTT(int score, int ply) { + if (score >= MATE_SCORE - MAX_DEPTH) + return score + ply; + if (score <= -MATE_SCORE + MAX_DEPTH) + return score - ply; + return score; +} + +inline int ScoreFromTT(int score, int ply) { + if (score >= MATE_SCORE - MAX_DEPTH) + return score - ply; + if (score <= -MATE_SCORE + MAX_DEPTH) + return score + ply; + return score; +} + inline int lmrTable[2][MAX_DEPTH+1][MAX_MOVES]; #ifdef TUNING