Skip to content

Commit

Permalink
Clean up search (#689)
Browse files Browse the repository at this point in the history
No functional change

Bench: 21963671
  • Loading branch information
TerjeKir committed Nov 2, 2023
1 parent 4f061db commit 66bf22b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/transposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 66bf22b

Please sign in to comment.