Skip to content

Commit

Permalink
Bench: 26879122
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Dec 30, 2023
1 parent 5baabfd commit 2e0e062
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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) {

Expand Down
11 changes: 11 additions & 0 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
7 changes: 7 additions & 0 deletions src/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -74,6 +80,7 @@ typedef struct Thread {
PawnCache pawnCache;
ButterflyHistory history;
PawnHistory pawnHistory;
CorrectionHistory corrHistory;
CaptureToHistory captureHistory;
ContinuationHistory continuation[2][2];

Expand Down

0 comments on commit 2e0e062

Please sign in to comment.