From f581d919b9bbbab6d78da42614c4f491c4b70c48 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 21 Jul 2025 16:36:44 +0200 Subject: [PATCH 1/5] different indexing for conthist | bench 2298837 --- source/search.cpp | 20 ++++++++++++-------- source/search.h | 15 +++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index cc45fd7..3be5049 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -22,6 +22,15 @@ void InitLMRTable() { } } +int16_t ContHistory::GetOnePly(Board& board, Move& move, bool otherColor, SearchContext* ctx, int ply) { + int prevType = ctx->ss[ply-1].pieceType; + int prevTo = ctx->ss[ply-1].moveTo; + int pieceType = board.GetPieceType(move.MoveFrom()); + int to = move.MoveTo(); + + return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to]; +} + static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { TTEntry *current = ctx->TT.GetRawEntry(board.hashKey); if (current->hashKey == board.hashKey && current->bestMove == move) { @@ -47,12 +56,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { int conthistScore = 0; if (ply > 0) { - int prevType = ctx->ss[ply-1].pieceType; - int prevTo = ctx->ss[ply-1].moveTo; - int pieceType = board.GetPieceType(move.MoveFrom()); - int to = move.MoveTo(); - - conthistScore = ctx->conthist[board.sideToMove][prevType][prevTo][pieceType][to]; + conthistScore = ctx->conthist.GetOnePly(board, move, !board.sideToMove, ctx, ply); } return 20000 + historyScore + conthistScore; @@ -530,14 +534,14 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int pieceType = ctx->ss[ply].pieceType; int to = ctx->ss[ply].moveTo; - ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, bonus); + ctx->conthist.Update(board.sideToMove, !board.sideToMove, prevType, prevTo, pieceType, to, bonus); // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); to = seenQuiets[moveIndex].MoveTo(); - ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, -bonus); + ctx->conthist.Update(board.sideToMove, !board.sideToMove, prevType, prevTo, pieceType, to, -bonus); } } diff --git a/source/search.h b/source/search.h index 3ba9fea..0f9e6fc 100644 --- a/source/search.h +++ b/source/search.h @@ -33,22 +33,25 @@ constexpr std::array SEEPieceValues = { }; void InitLMRTable(); +class SearchContext; class ContHistory { private: - // indexed by [stm][prev move piece][prev move to][piece][to] - MultiArray contHistMoves; + // indexed by [other color][prev move piece][stm][prev move to][piece][to] + MultiArray contHistMoves; public: - void Update(bool stm, int prevType, int prevTo, int type, int to, int bonus) { + void Update(bool stm, bool otherColor, int prevType, int prevTo, int type, int to, int bonus) { int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY); - contHistMoves[stm][prevType][prevTo][type][to] += - clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY; + contHistMoves[otherColor][prevType][prevTo][stm][type][to] += + clampedBonus - contHistMoves[otherColor][prevType][prevTo][stm][type][to] * std::abs(clampedBonus) / MAX_HISTORY; } void Clear() { - std::fill(&contHistMoves[0][0][0][0][0], &contHistMoves[0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0); + std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0); } + + int16_t GetOnePly(Board& board, Move& move, bool otherColor, SearchContext* ctx, int ply); auto& operator[](int index) { return contHistMoves[index]; -- 2.47.3 From b74f6f4cbb5971607de7cf59a1e30d216818f1d2 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 21 Jul 2025 16:41:56 +0200 Subject: [PATCH 2/5] storing otherColor in stack | bench 2298837 --- source/search.cpp | 12 ++++++++---- source/search.h | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 3be5049..8afbca6 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -22,11 +22,12 @@ void InitLMRTable() { } } -int16_t ContHistory::GetOnePly(Board& board, Move& move, bool otherColor, SearchContext* ctx, int ply) { +int16_t ContHistory::GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply) { int prevType = ctx->ss[ply-1].pieceType; int prevTo = ctx->ss[ply-1].moveTo; int pieceType = board.GetPieceType(move.MoveFrom()); int to = move.MoveTo(); + bool otherColor = ctx->ss[ply-1].side; return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to]; } @@ -56,7 +57,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { int conthistScore = 0; if (ply > 0) { - conthistScore = ctx->conthist.GetOnePly(board, move, !board.sideToMove, ctx, ply); + conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply); } return 20000 + historyScore + conthistScore; @@ -278,6 +279,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom()); ctx->ss[ply].moveTo = board.moveList[i].MoveTo(); + ctx->ss[ply].side = board.sideToMove; if (!SEE(board, board.moveList[i], 0)) continue; @@ -446,6 +448,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom()); ctx->ss[ply].moveTo = currMove.MoveTo(); + ctx->ss[ply].side = board.sideToMove; if (copy.positionIndex >= ctx->positionHistory.size()) { ctx->positionHistory.resize(copy.positionIndex + 100); @@ -533,15 +536,16 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int prevTo = ctx->ss[ply-1].moveTo; int pieceType = ctx->ss[ply].pieceType; int to = ctx->ss[ply].moveTo; + bool otherColor = ctx->ss[ply-1].side; - ctx->conthist.Update(board.sideToMove, !board.sideToMove, prevType, prevTo, pieceType, to, bonus); + ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, bonus); // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); to = seenQuiets[moveIndex].MoveTo(); - ctx->conthist.Update(board.sideToMove, !board.sideToMove, prevType, prevTo, pieceType, to, -bonus); + ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -bonus); } } diff --git a/source/search.h b/source/search.h index 0f9e6fc..71e4bff 100644 --- a/source/search.h +++ b/source/search.h @@ -51,7 +51,7 @@ public: std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0); } - int16_t GetOnePly(Board& board, Move& move, bool otherColor, SearchContext* ctx, int ply); + int16_t GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply); auto& operator[](int index) { return contHistMoves[index]; @@ -115,6 +115,7 @@ public: int pieceType = -1; int moveTo = -1; + bool side = -1; }; class SearchContext { -- 2.47.3 From 092ca2a2d10573832e4ace38d6ae2f38c99a1bf1 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 5 Aug 2025 02:52:12 +0200 Subject: [PATCH 3/5] temporarily removed 2ply conthist | bench 24146828 --- source/search.cpp | 64 +++++++++++++++++++++++++++++++++++------------ source/search.h | 1 + 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 8afbca6..963cff4 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -32,6 +32,16 @@ int16_t ContHistory::GetOnePly(Board& board, Move& move, SearchContext* ctx, int return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to]; } +int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply) { + int prevType = ctx->ss[ply-2].pieceType; + int prevTo = ctx->ss[ply-2].moveTo; + int pieceType = board.GetPieceType(move.MoveFrom()); + int to = move.MoveTo(); + bool otherColor = ctx->ss[ply-2].side; + + return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to]; +} + static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { TTEntry *current = ctx->TT.GetRawEntry(board.hashKey); if (current->hashKey == board.hashKey && current->bestMove == move) { @@ -58,6 +68,11 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { if (ply > 0) { conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply); + /* + if (ply > 1) { + conthistScore += ctx->conthist.GetTwoPly(board, move, ctx, ply); + } + */ } return 20000 + historyScore + conthistScore; @@ -71,24 +86,24 @@ static void SortMoves(Board &board, int ply, SearchContext* ctx) { if (board.currentMoveIndex <= 1) { return; } - + int scores[MAX_MOVES]; - + for (int i = 0; i < board.currentMoveIndex; i++) { scores[i] = ScoreMove(board, board.moveList[i], ply, ctx); } - + for (int i = 1; i < board.currentMoveIndex; i++) { Move tempMove = board.moveList[i]; int tempScore = scores[i]; int j = i - 1; - + while (j >= 0 && scores[j] < tempScore) { board.moveList[j + 1] = board.moveList[j]; scores[j + 1] = scores[j]; j--; } - + board.moveList[j + 1] = tempMove; scores[j + 1] = tempScore; } @@ -122,7 +137,7 @@ static bool IsFifty(Board &board) { } static bool IsInsuffMat(Board &board) { - return (board.occupied.PopCount() <= 3 + return (board.occupied.PopCount() <= 3 && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); } @@ -133,7 +148,7 @@ bool IsDraw(Board &board, SearchContext* ctx) { template static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, SearchContext* ctx) { int reduction = 0; - + // Late Move Reduction if (depth >= 3 && moveSeen >= 2 + (2 * isPV) && !move.IsCapture()) { reduction = lmrTable[depth][moveSeen]; @@ -182,7 +197,7 @@ bool SEE(Board& board, Move& move, int threshold) { if (flags == epCapture) occupied.SetBit(board.enPassantTarget); - + Bitboard attackers = board.AttacksTo(to, occupied) & occupied; bool color = !board.sideToMove; @@ -205,7 +220,7 @@ bool SEE(Board& board, Move& move, int threshold) { attackers |= MOVEGEN::getRookAttack(to, occupied) & rooks; attackers &= occupied; - + color = !color; balance = -balance - 1 - SEEPieceValues[nextVictim]; @@ -246,10 +261,10 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear if (ply > ctx->seldepth) ctx->seldepth = ply; - + int bestScore = NNUE::net.Evaluate(board); ctx->ss[ply].eval = bestScore; - + if (!ctx->excluded) { TTEntry *entry = ctx->TT.GetRawEntry(board.hashKey); if (entry->hashKey == board.hashKey) { @@ -387,9 +402,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->doingNullMove = true; int score = -PVS(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score; ctx->doingNullMove = false; - + if (ctx->searchStopped) return 0; - if (score >= beta) return score; + if (score >= beta) return score; } } } @@ -542,13 +557,30 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { - pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); - to = seenQuiets[moveIndex].MoveTo(); + int pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); + int to = seenQuiets[moveIndex].MoveTo(); ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -bonus); } + + if (ply > 1) { + prevType = ctx->ss[ply-2].pieceType; + prevTo = ctx->ss[ply-2].moveTo; + otherColor = ctx->ss[ply-2].side; + + ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, bonus); + + // Malus + for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { + int pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); + int to = seenQuiets[moveIndex].MoveTo(); + + ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -bonus); + } + + } } - + // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { diff --git a/source/search.h b/source/search.h index 71e4bff..8dcd560 100644 --- a/source/search.h +++ b/source/search.h @@ -52,6 +52,7 @@ public: } int16_t GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply); + int16_t GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply); auto& operator[](int index) { return contHistMoves[index]; -- 2.47.3 From 21148655dd03776e0f0c9cb5f3b2a25abb1a4bf8 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 21 Jul 2025 17:25:28 +0200 Subject: [PATCH 4/5] fixed move scores | bench 2451113 --- source/search.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/source/search.cpp b/source/search.cpp index 963cff4..dfb6480 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -45,7 +45,11 @@ int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { TTEntry *current = ctx->TT.GetRawEntry(board.hashKey); if (current->hashKey == board.hashKey && current->bestMove == move) { +<<<<<<< HEAD return 100000; +======= + return 1000000; +>>>>>>> 2a9d1c1 (fixed move scores | bench 2451113) } if (move.IsCapture()) { @@ -56,12 +60,21 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } +<<<<<<< HEAD return 50000 * ((SEE(board, move, -100))) + (100 * targetType - attackerType + 105); } else { if (ctx->killerMoves[0][ply] == move) { return 41000; } else if (ctx->killerMoves[1][ply] == move) { return 40000; +======= + return 500000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + } else { + if (ctx->killerMoves[0][ply] == move) { + return 410000; + } else if (ctx->killerMoves[1][ply] == move) { + return 400000; +>>>>>>> 2a9d1c1 (fixed move scores | bench 2451113) } else { int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; int conthistScore = 0; -- 2.47.3 From 0e88db9b4c4da8770052d02a8f1647cfb9c76fc7 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 5 Aug 2025 02:57:11 +0200 Subject: [PATCH 5/5] removed git markers | bench 24146828 --- source/search.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index dfb6480..963cff4 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -45,11 +45,7 @@ int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { TTEntry *current = ctx->TT.GetRawEntry(board.hashKey); if (current->hashKey == board.hashKey && current->bestMove == move) { -<<<<<<< HEAD return 100000; -======= - return 1000000; ->>>>>>> 2a9d1c1 (fixed move scores | bench 2451113) } if (move.IsCapture()) { @@ -60,21 +56,12 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } -<<<<<<< HEAD return 50000 * ((SEE(board, move, -100))) + (100 * targetType - attackerType + 105); } else { if (ctx->killerMoves[0][ply] == move) { return 41000; } else if (ctx->killerMoves[1][ply] == move) { return 40000; -======= - return 500000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; - } else { - if (ctx->killerMoves[0][ply] == move) { - return 410000; - } else if (ctx->killerMoves[1][ply] == move) { - return 400000; ->>>>>>> 2a9d1c1 (fixed move scores | bench 2451113) } else { int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; int conthistScore = 0; -- 2.47.3