From 2e0e0623360fc6d68004f14780bc674d2703136d Mon Sep 17 00:00:00 2001 From: Terje Date: Sat, 30 Dec 2023 18:14:40 +0100 Subject: [PATCH] Bench: 26879122 --- src/history.h | 8 ++++++++ src/search.c | 11 +++++++++++ src/threads.c | 1 + src/threads.h | 7 +++++++ 4 files changed, 27 insertions(+) diff --git a/src/history.h b/src/history.h index 9bf81ead..d1789395 100644 --- a/src/history.h +++ b/src/history.h @@ -28,11 +28,13 @@ #define QuietEntry(move) (&thread->history[thread->pos.stm][fromSq(move)][toSq(move)]) #define PawnEntry(move) (&thread->pawnHistory[PawnStructure(&thread->pos)][piece(move)][toSq(move)]) +#define CorrEntry() (&thread->corrHistory[thread->pos.stm][CorrectionStructure(&thread->pos)]) #define NoisyEntry(move) (&thread->captureHistory[piece(move)][toSq(move)][PieceTypeOf(capturing(move))]) #define ContEntry(offset, move) (&(*(ss-offset)->continuation)[piece(move)][toSq(move)]) #define QuietHistoryUpdate(move, bonus) (HistoryBonus(QuietEntry(move), bonus, 6880)) #define PawnHistoryUpdate(move, bonus) (HistoryBonus(PawnEntry(move), bonus, 8192)) +#define CorrHistoryUpdate(bonus) (HistoryBonus(CorrEntry(), bonus, 512)) #define NoisyHistoryUpdate(move, bonus) (HistoryBonus(NoisyEntry(move), bonus, 16384)) #define ContHistoryUpdate(offset, move, bonus) (HistoryBonus(ContEntry(offset, move), bonus, 30000)) @@ -55,6 +57,12 @@ INLINE void UpdateContHistories(Stack *ss, Move move, int bonus) { ContHistoryUpdate(4, move, bonus); } +INLINE void UpdateCorrHistory(Thread *thread, int score, int staticEval) { + int correction = score - staticEval; + CorrHistoryUpdate(CLAMP(correction, -512, 512)); + if (*CorrEntry() > 512) puts("CorrEntry() > 512"); +} + // Updates history heuristics when a quiet move is the best move INLINE void UpdateQuietHistory(Thread *thread, Stack *ss, Move bestMove, Depth depth, Move quiets[], int qCount) { diff --git a/src/search.c b/src/search.c index 174b0409..d39e7311 100644 --- a/src/search.c +++ b/src/search.c @@ -107,6 +107,8 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, const int beta) { : ttEval != NOSCORE ? ttEval : EvalPosition(pos, thread->pawnCache); + eval += *CorrEntry(); + // If we are at max depth, return static eval if (ss->ply >= MAX_PLY) return eval; @@ -295,6 +297,8 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth if (ttScore != NOSCORE && TTScoreIsMoreInformative(ttBound, ttScore, eval)) eval = ttScore; + eval += *CorrEntry(); + // Improving if not in check, and current eval is higher than 2 plies ago bool improving = !inCheck && eval > (ss-2)->staticEval; @@ -567,6 +571,13 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth : pvNode && bestMove ? BOUND_EXACT : BOUND_UPPER); + // Update correction history + if ( !inCheck + && (!bestMove || moveIsQuiet(bestMove)) + && !(bestScore >= beta && bestScore <= ss->staticEval) + && !(!bestMove && bestScore >= ss->staticEval)) + UpdateCorrHistory(thread, bestScore, ss->staticEval); + return bestScore; } diff --git a/src/threads.c b/src/threads.c index 335e13dd..6fd2eb1a 100644 --- a/src/threads.c +++ b/src/threads.c @@ -127,6 +127,7 @@ void ResetThreads() { memset(threads[i].pawnCache, 0, sizeof(PawnCache)), memset(threads[i].history, 0, sizeof(threads[i].history)), memset(threads[i].pawnHistory, 0, sizeof(threads[i].pawnHistory)), + memset(threads[i].corrHistory, 0, sizeof(threads[i].corrHistory)), memset(threads[i].captureHistory, 0, sizeof(threads[i].captureHistory)), memset(threads[i].continuation, 0, sizeof(threads[i].continuation)); } diff --git a/src/threads.h b/src/threads.h index bab7ff35..620321d7 100644 --- a/src/threads.h +++ b/src/threads.h @@ -28,13 +28,19 @@ #define SS_OFFSET 10 #define MULTI_PV_MAX 64 #define PAWN_HISTORY_SIZE 512 +#define CORRECTION_HISTORY_SIZE 131072 INLINE int PawnStructure(const Position *pos) { return pos->pawnKey & (PAWN_HISTORY_SIZE - 1); } +INLINE int CorrectionStructure(const Position *pos) { + return pos->pawnKey & (CORRECTION_HISTORY_SIZE - 1); +} + typedef int16_t ButterflyHistory[COLOR_NB][64][64]; typedef int16_t PawnHistory[PAWN_HISTORY_SIZE][PIECE_NB][64]; +typedef int16_t CorrectionHistory[COLOR_NB][CORRECTION_HISTORY_SIZE]; typedef int16_t CaptureToHistory[PIECE_NB][64][TYPE_NB]; typedef int16_t PieceToHistory[PIECE_NB][64]; typedef PieceToHistory ContinuationHistory[PIECE_NB][64]; @@ -74,6 +80,7 @@ typedef struct Thread { PawnCache pawnCache; ButterflyHistory history; PawnHistory pawnHistory; + CorrectionHistory corrHistory; CaptureToHistory captureHistory; ContinuationHistory continuation[2][2];