From 7db4fcf59c31cf8e49d8693e02eef1ac15d28925 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 5 Apr 2026 02:52:47 +0200 Subject: [PATCH 1/3] small refactor | bench 1181803 --- source/search.cpp | 127 ++++++++++++++++++---------------------------- source/search.h | 2 +- source/tunables.h | 1 + 3 files changed, 50 insertions(+), 80 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index c75d0dd..cf67683 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -81,7 +81,7 @@ static int AdjustEval(Board &board, SearchContext* ctx, int eval) { return std::clamp(eval + corrhist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1); } -static bool IsThreefold(Board &board, SearchContext* ctx) { +static bool IsTwoFold(Board &board, SearchContext* ctx) { for (int i = 0; i < board.positionIndex; i++) { if (ctx->positionHistory[i] == board.hashKey) { // repetition found @@ -103,7 +103,7 @@ static bool IsInsuffMat(Board &board) { } bool IsDraw(Board &board, SearchContext* ctx) { - return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); + return IsFifty(board) || IsInsuffMat(board) || IsTwoFold(board, ctx); } template @@ -324,7 +324,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear alpha = bestScore; } - const int fpScore = bestScore + 100; + const int fpScore = bestScore + qsFPMargin; MovePicker mp(board, ctx, ply, entry.bestMove); @@ -332,11 +332,13 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear int nodeType = AllNode; + const bool inCheck = board.InCheck(); + Move currMove; while ((currMove = mp.Next())) { if (!IsLoss(bestScore)) { // QS FP - if (!board.InCheck() && currMove.IsCapture() && + if (!inCheck && currMove.IsCapture() && fpScore <= alpha && !SEE(board, currMove, 1)) { bestScore = std::max(bestScore, fpScore); @@ -427,7 +429,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int ttAdjustedEval = staticEval; - if (!ctx->excluded && !board.InCheck() && ttHit && + const bool inCheck = board.InCheck(); + + if (!ctx->excluded && !inCheck && ttHit && ((entry.nodeType == PV) || (entry.nodeType == AllNode && entry.score <= staticEval) || (entry.nodeType == CutNode && entry.score >= staticEval))) { @@ -439,7 +443,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC const bool improving = [&] { - if (board.InCheck()) + if (inCheck) return false; if (ply > 1 && ctx->ss[ply - 2].eval != ScoreNone) return staticEval > ctx->ss[ply - 2].eval; @@ -449,7 +453,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC return true; }(); - if (!board.InCheck() && !ctx->excluded) { + if (!inCheck && !ctx->excluded) { if (ply) { // Reverse Futility Pruning int margin = rfpBase + rfpMargin * (depth - improving); @@ -591,12 +595,8 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // that a quiet move will help us so we skip them int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()][sourceThreatened][targetThreatened]; - if (ply > 0) { - historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 1); - - if (ply > 1) - historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 2); - } + if (ply > 0) historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 1); + if (ply > 1) historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 2); int margin = fpMargin * (lmrDepth + improving) + historyScore / 32; @@ -664,7 +664,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } else if (cutnode) { extension--; } - } else if (depth <= 7 && !board.InCheck() && staticEval <= alpha - ldseMargin && entry.nodeType == CutNode) { + } else if (depth <= 7 && !inCheck && staticEval <= alpha - ldseMargin && entry.nodeType == CutNode) { extension++; } } @@ -728,7 +728,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Fail high (beta cutoff) if (score >= beta) { - const int bonusDepth = depth + (!board.InCheck() && staticEval <= alpha); + const int bonusDepth = depth + (!inCheck && staticEval <= alpha); int historyBonus = historyBonusMultiplier * bonusDepth - historyBonusSub; int historyMalus = historyMalusMultiplier * bonusDepth - historyMalusSub; int contHistoryBonus = contHistoryBonusMultiplier * bonusDepth - contHistoryBonusSub; @@ -736,80 +736,49 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int captHistoryBonus = captHistoryBonusMultiplier * bonusDepth - captHistoryBonusSub; int captHistoryMalus = captHistoryMalusMultiplier * bonusDepth - captHistoryMalusSub; + auto UpdateCaptHist = [&](Move move, int bonus) { + int movingPiece = board.GetPieceType(move.MoveFrom()); + int capturedPiece = move.GetFlags() == epCapture ? Pawn : board.GetPieceType(move.MoveTo()); + ctx->capthist.Update(board.sideToMove, movingPiece, capturedPiece, move.MoveTo(), bonus); + }; + + auto UpdateContHistNPly = [&](int n) { + int prevType = ctx->ss[ply - n].pieceType; + int prevTo = ctx->ss[ply - n].moveTo; + bool otherColor = ctx->ss[ply - n].side; + int pieceType = ctx->ss[ply].pieceType; + int to = ctx->ss[ply].moveTo; + + ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, contHistoryBonus); + + for (int i = 0; i < seenQuietsCount - 1; i++) { + int pt = board.GetPieceType(seenQuiets[i].MoveFrom()); + int t = seenQuiets[i].MoveTo(); + ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pt, t, -contHistoryMalus); + } + }; + if (!currMove.IsCapture()) { ctx->killerMoves[ply] = currMove; ctx->history.Update(board.sideToMove, currMove, sourceThreatened, targetThreatened, historyBonus); - if (ply > 0) { - int prevType = ctx->ss[ply-1].pieceType; - 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, otherColor, prevType, prevTo, pieceType, to, contHistoryBonus); - - // 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, -contHistoryMalus); - } - - 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, contHistoryBonus); - - // 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, -contHistoryMalus); - } - - } - } - + if (ply > 0) UpdateContHistNPly(1); + if (ply > 1) UpdateContHistNPly(2); // Malus - for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { - sourceThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveFrom()); - targetThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveTo()); - - ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], sourceThreatened, targetThreatened, -historyMalus); + for (int i = 0; i < seenQuietsCount - 1; i++) { + sourceThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[i].MoveFrom()); + targetThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[i].MoveTo()); + ctx->history.Update(board.sideToMove, seenQuiets[i], sourceThreatened, targetThreatened, -historyMalus); } } else { - int movingPiece = board.GetPieceType(currMove.MoveFrom()); - int capturedPiece = board.GetPieceType(currMove.MoveTo()); - - if (currMove.GetFlags() == epCapture) { - capturedPiece = Pawn; - } - - ctx->capthist.Update(board.sideToMove, movingPiece, capturedPiece, currMove.MoveTo(), captHistoryBonus); + UpdateCaptHist(currMove, captHistoryBonus); } // Capthist malus - for (int moveIndex = 0; moveIndex < seenCapturesCount - currMove.IsCapture(); moveIndex++) { - int movingPiece = board.GetPieceType(seenCaptures[moveIndex].MoveFrom()); - int capturedPiece = board.GetPieceType(seenCaptures[moveIndex].MoveTo()); - - if (seenCaptures[moveIndex].GetFlags() == epCapture) { - capturedPiece = Pawn; - } - - if (movingPiece == nullPieceType || capturedPiece == nullPieceType) { - std::cout << "asd\n"; - } - - ctx->capthist.Update(board.sideToMove, movingPiece, capturedPiece, - seenCaptures[moveIndex].MoveTo(), -captHistoryMalus); + for (int i = 0; i < seenCapturesCount - currMove.IsCapture(); i++) { + UpdateCaptHist(seenCaptures[i], -captHistoryMalus); } if (!ctx->excluded) @@ -819,7 +788,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } if (moveSeen == 0) { - if (board.InCheck()) { // checkmate + if (inCheck) { // checkmate return -MATE_SCORE + ply; } else { // stalemate return 0; @@ -829,7 +798,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (searchStopped) return 0; if (!ctx->excluded) { - if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove)) + if (!inCheck && ((results.bestMove.IsQuiet() || !results.bestMove)) && !(nodeType == AllNode && results.score >= staticEval)) { int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT); diff --git a/source/search.h b/source/search.h index 21d075f..e636ff6 100644 --- a/source/search.h +++ b/source/search.h @@ -178,7 +178,7 @@ public: std::cout << termcolor::bright_cyan; table[n][i].PrintMove(); std::cout << termcolor::reset; - + if (isEven) { std::cout << termcolor::color<247>; } else { diff --git a/source/tunables.h b/source/tunables.h index c9e917c..ec3e836 100644 --- a/source/tunables.h +++ b/source/tunables.h @@ -25,6 +25,7 @@ X_INT(rfpBase, 21, 0, 100) \ X_INT(rfpMargin, 97, 50, 200) \ X_INT(fpMargin, 86, 50, 200) \ + X_INT(qsFPMargin, 100, 50, 200) \ X_INT(probcutBetaMargin, 190, 100, 300) \ X_INT(razoringScalar, 195, 80, 320) \ X_INT(doubleExtMargin, 32, 8, 64) \ -- 2.47.3 From 68d7dcbccb2e70170a13cdc5168264a204efd0d3 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 5 Apr 2026 02:58:40 +0200 Subject: [PATCH 2/3] GetPromoPiece helper function | bench 1181803 --- source/board.cpp | 13 ++++--------- source/move.h | 14 +++++++++++--- source/search.cpp | 13 ++----------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index fc726ee..462b527 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -24,7 +24,7 @@ void Board::Reset() { mailbox = std::array(); mailbox.fill(nullPieceType); - + pinned = std::array(); pieces = std::array(); @@ -424,11 +424,10 @@ void Board::MakeMove(Move move) { RemovePiece(attackerPiece, move.MoveFrom(), attackerColor); if (move.IsPromo()) { + endPiece = move.GetPromoPiece(); if (move.IsCapture()) { - endPiece = move.GetFlags() - 9; Promote(move.MoveTo(), endPiece, sideToMove, true); } else { - endPiece = move.GetFlags() - 5; Promote(move.MoveTo(), endPiece, sideToMove, false); } } @@ -708,7 +707,7 @@ Bitboard Board::CalcPinned(bool color) { int kingSquare = (pieces[King] & colors[color]).getLS1BIndex(); assert(kingSquare != -1); - + Bitboard oppQueens = pieces[Queen] & colors[!color]; Bitboard potentialAttackers = MOVEGEN::getBishopAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color])) @@ -977,11 +976,7 @@ bool Board::GivesDirectCheck(Move &move) { int attackerType = nullPieceType; if (move.IsPromo()) { - if (move.IsCapture()) { - attackerType = move.GetFlags() - 9; - } else { - attackerType = move.GetFlags() - 5; - } + attackerType = move.GetPromoPiece(); } else { attackerType = GetPieceType(move.MoveFrom()); } diff --git a/source/move.h b/source/move.h index 6c046ad..b918a32 100644 --- a/source/move.h +++ b/source/move.h @@ -65,7 +65,7 @@ public: } bool IsCastle() { - return (GetFlags() == kingCastle || GetFlags() == queenCastle); + return (GetFlags() == kingCastle || GetFlags() == queenCastle); } void PrintMove() { @@ -84,7 +84,7 @@ public: } } - std::string GetMoveString() { + std::string GetMoveString() { std::string result = squareCoords[MoveFrom()]; result += squareCoords[MoveTo()]; @@ -106,7 +106,7 @@ public: uint16_t ConvertToViriMoveFormat() { uint16_t viriMove = 0; int flags = GetFlags(); - + viriMove = MoveFrom(); if (flags == kingCastle || flags == queenCastle) { @@ -150,6 +150,14 @@ public: return viriMove; } + int GetPromoPiece() { + if (IsCapture()) { + return GetFlags() - 9; + } else { + return GetFlags() - 5; + } + } + operator uint16_t() { return m_move; } diff --git a/source/search.cpp b/source/search.cpp index cf67683..c0799be 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -174,11 +174,7 @@ bool SEE(Board& board, Move& move, int threshold) { // Next victim is moved piece or promo piece if (move.IsPromo()) { - if (move.IsCapture()) { - nextVictim = move.GetFlags() - 9; - } else { - nextVictim = move.GetFlags() - 5; - } + nextVictim = move.GetPromoPiece(); } int balance = MoveEstimatedValue(board, move) - threshold; @@ -956,12 +952,7 @@ int MoveEstimatedValue(Board& board, Move& move) { int value = pieceType != nullPieceType ? SEEPieceValues[pieceType] : 0; if (move.IsPromo()) { - int promoPiece = -1; - if (move.IsCapture()) { - promoPiece = move.GetFlags() - 9; - } else { - promoPiece = move.GetFlags() - 5; - } + int promoPiece = move.GetPromoPiece(); value += SEEPieceValues[promoPiece] - SEEPieceValues[Pawn]; } else if (move.GetFlags() == epCapture) { -- 2.47.3 From e4d114a763dbe17eac497c9c4a9582d04e965168 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 5 Apr 2026 03:06:43 +0200 Subject: [PATCH 3/3] very small refactor | bench 1181803 --- source/search.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index c0799be..cbfaf58 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -393,11 +393,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (ply > ctx->seldepth) ctx->seldepth = ply; - ctx->pvLine.SetLength(ply); if (ply && (IsDraw(board, ctx))) return 0; - TTEntry entry; if (!ctx->excluded) entry = ctx->TT->GetEntry(board.hashKey); @@ -561,7 +559,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->killerMoves[ply + 1] = Move(); - // For all moves + // Move-loop Move currMove; while ((currMove = mp.Next())) { @@ -586,14 +584,14 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, currMove.MoveFrom()); bool targetThreatened = board.IsSquareThreatened(board.sideToMove, currMove.MoveTo()); - // Futility pruning - // If our static eval is far below alpha, there is only a small chance - // that a quiet move will help us so we skip them int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()][sourceThreatened][targetThreatened]; if (ply > 0) historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 1); if (ply > 1) historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 2); + // Futility pruning + // If our static eval is far below alpha, there is only a small chance + // that a quiet move will help us so we skip them int margin = fpMargin * (lmrDepth + improving) + historyScore / 32; if (!isPV && ply && currMove.IsQuiet() -- 2.47.3