From 66bf22bfbac9623d3f0183bb4cc6a8c98647cb92 Mon Sep 17 00:00:00 2001 From: TerjeKir <42723273+TerjeKir@users.noreply.github.com> Date: Thu, 2 Nov 2023 23:25:31 +0100 Subject: [PATCH] Clean up search (#689) No functional change Bench: 21963671 --- src/search.c | 20 +++++++------------- src/transposition.h | 6 ++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/search.c b/src/search.c index bcf28edc..adfde500 100644 --- a/src/search.c +++ b/src/search.c @@ -88,8 +88,7 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) { // Probe transposition table bool ttHit; - Key key = pos->key; - TTEntry *tte = ProbeTT(key, &ttHit); + TTEntry *tte = ProbeTT(pos->key, &ttHit); Move ttMove = ttHit ? tte->move : NOMOVE; int ttScore = ttHit ? ScoreFromTT(tte->score, ss->ply) : NOSCORE; @@ -101,9 +100,7 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) { ttHit = false, ttMove = NOMOVE, ttScore = NOSCORE, ttEval = NOSCORE; // Trust TT if not a pvnode - if ( !pvNode - && ttHit - && (ttBound & (ttScore >= beta ? BOUND_LOWER : BOUND_UPPER))) + if (!pvNode && ttHit && TTScoreIsMoreInformative(ttBound, ttScore, beta)) return ttScore; // Do a static evaluation for pruning considerations @@ -130,7 +127,8 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) { moveloop: - if (!inCheck) InitNoisyMP(&mp, thread, ss, ttMove); else InitNormalMP(&mp, thread, ss, 0, ttMove, NOMOVE, NOMOVE); + if (!inCheck) InitNoisyMP(&mp, thread, ss, ttMove); + else InitNormalMP(&mp, thread, ss, 0, ttMove, NOMOVE, NOMOVE); // Move loop Move bestMove = NOMOVE; @@ -186,7 +184,7 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) { if (inCheck && bestScore == -INFINITE) return -MATE + ss->ply; - StoreTTEntry(tte, key, bestMove, ScoreToTT(bestScore, ss->ply), eval, 0, + StoreTTEntry(tte, pos->key, bestMove, ScoreToTT(bestScore, ss->ply), eval, 0, bestScore >= beta ? BOUND_LOWER : BOUND_UPPER); return bestScore; @@ -250,10 +248,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth ttHit = false, ttMove = NOMOVE, ttScore = NOSCORE, ttEval = NOSCORE; // Trust TT if not a pvnode and the entry depth is sufficiently high - if ( !pvNode - && ttHit - && ttDepth >= depth - && (ttBound & (ttScore >= beta ? BOUND_LOWER : BOUND_UPPER))) { + if (!pvNode && ttHit && ttDepth >= depth && TTScoreIsMoreInformative(ttBound, ttScore, beta)) { // Give a history bonus to quiet tt moves that causes a cutoff if (ttScore >= beta && moveIsQuiet(ttMove)) @@ -296,8 +291,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth : EvalPosition(pos, thread->pawnCache); // Use ttScore as eval if it is more informative - if ( ttScore != NOSCORE - && ttBound & (ttScore > eval ? BOUND_LOWER : BOUND_UPPER)) + if (ttScore != NOSCORE && TTScoreIsMoreInformative(ttBound, ttScore, eval)) eval = ttScore; // Improving if not in check, and current eval is higher than 2 plies ago diff --git a/src/transposition.h b/src/transposition.h index f4d5d9b1..e93f54a1 100644 --- a/src/transposition.h +++ b/src/transposition.h @@ -93,6 +93,12 @@ INLINE int ScoreFromTT (const int score, const uint8_t ply) { : score; } +// Checks whether the TT score is more informative than score +// (TT score is either exact, a lower bound above score, or an upper bound below score) +INLINE bool TTScoreIsMoreInformative(uint8_t bound, int ttScore, int score) { + return bound & (ttScore >= score ? BOUND_LOWER : BOUND_UPPER); +} + INLINE uint64_t TTIndex(Key key) { return ((unsigned __int128)key * (unsigned __int128)TT.count) >> 64; }