From b1b5893a8e344155d01a1658b79670a8ed1fd160 Mon Sep 17 00:00:00 2001 From: Nonlinear2 <131959792+Nonlinear2@users.noreply.github.com> Date: Fri, 23 May 2025 18:33:01 +0200 Subject: [PATCH] Minor code improvements - Remove / add empty lines - fix the `ttcapture` comment - remove the `bonus` variable for `ttMoveHistory` - remove unnecessary parentheses / brackets - refactor the movepick good quiet stage - rename `endMoves` to `endCur`, as the previous name suggests that it points to the end of all generated moves, which it does not. closes https://github.com/official-stockfish/Stockfish/pull/6089 No functional change. Co-Authored-By: xu-shawn <50402888+xu-shawn@users.noreply.github.com> --- src/movepick.cpp | 33 ++++++++++++++++++--------------- src/movepick.h | 4 ++-- src/search.cpp | 15 +++++---------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/movepick.cpp b/src/movepick.cpp index f3d6fef8..faef753b 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -173,7 +173,7 @@ void MovePicker::score() { if (KNIGHT <= pt && pt <= QUEEN) { static constexpr int bonus[QUEEN + 1] = {0, 0, 144, 144, 256, 517}; - int v = (threatByLesser[pt] & to ? -95 : 100 * bool(threatByLesser[pt] & from)); + int v = threatByLesser[pt] & to ? -95 : 100 * bool(threatByLesser[pt] & from); m.value += bonus[pt] * v; } @@ -200,7 +200,7 @@ void MovePicker::score() { template Move MovePicker::select(Pred filter) { - for (; cur < endMoves; ++cur) + for (; cur < endCur; ++cur) if (*cur != ttMove && filter()) return *cur++; @@ -227,10 +227,10 @@ top: case PROBCUT_INIT : case QCAPTURE_INIT : cur = endBadCaptures = moves; - endMoves = generate(pos, cur); + endCur = generate(pos, cur); score(); - partial_insertion_sort(cur, endMoves, std::numeric_limits::min()); + partial_insertion_sort(cur, endCur, std::numeric_limits::min()); ++stage; goto top; @@ -250,10 +250,10 @@ top: if (!skipQuiets) { cur = endBadQuiets = endBadCaptures; - endMoves = generate(pos, cur); + endCur = generate(pos, cur); score(); - partial_insertion_sort(cur, endMoves, -3560 * depth); + partial_insertion_sort(cur, endCur, -3560 * depth); } ++stage; @@ -261,13 +261,16 @@ top: case GOOD_QUIET : if (!skipQuiets && select([&]() { - return cur->value > -14000 ? true : (*endBadQuiets++ = *cur, false); + if (cur->value > -14000) + return true; + *endBadQuiets++ = *cur; + return false; })) return *(cur - 1); // Prepare the pointers to loop over the bad captures - cur = moves; - endMoves = endBadCaptures; + cur = moves; + endCur = endBadCaptures; ++stage; [[fallthrough]]; @@ -277,8 +280,8 @@ top: return *(cur - 1); // Prepare the pointers to loop over the bad quiets - cur = endBadCaptures; - endMoves = endBadQuiets; + cur = endBadCaptures; + endCur = endBadQuiets; ++stage; [[fallthrough]]; @@ -290,11 +293,11 @@ top: return Move::none(); case EVASION_INIT : - cur = moves; - endMoves = generate(pos, cur); + cur = moves; + endCur = generate(pos, cur); score(); - partial_insertion_sort(cur, endMoves, std::numeric_limits::min()); + partial_insertion_sort(cur, endCur, std::numeric_limits::min()); ++stage; [[fallthrough]]; @@ -317,7 +320,7 @@ bool MovePicker::can_move_king_or_pawn() { // SEE negative captures shouldn't be returned in GOOD_CAPTURE stage assert(stage > GOOD_CAPTURE && stage != EVASION_INIT); - for (ExtMove* m = moves; m < endMoves; ++m) + for (ExtMove* m = moves; m < endCur; ++m) { PieceType movedPieceType = type_of(pos.moved_piece(*m)); if ((movedPieceType == PAWN || movedPieceType == KING) && pos.legal(*m)) diff --git a/src/movepick.h b/src/movepick.h index 4218ab5a..2634fdd7 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -58,7 +58,7 @@ class MovePicker { template void score(); ExtMove* begin() { return cur; } - ExtMove* end() { return endMoves; } + ExtMove* end() { return endCur; } const Position& pos; const ButterflyHistory* mainHistory; @@ -67,7 +67,7 @@ class MovePicker { const PieceToHistory** continuationHistory; const PawnHistory* pawnHistory; Move ttMove; - ExtMove * cur, *endMoves, *endBadCaptures, *endBadQuiets; + ExtMove * cur, *endCur, *endBadCaptures, *endBadQuiets; int stage; int threshold; Depth depth; diff --git a/src/search.cpp b/src/search.cpp index ee876f85..673caacb 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -680,8 +680,6 @@ Value Search::Worker::search( && (ttData.bound & (ttData.value >= beta ? BOUND_LOWER : BOUND_UPPER)) && (cutNode == (ttData.value >= beta) || depth > 5)) { - - // If ttMove is quiet, update move sorting heuristics on TT hit if (ttData.move && ttData.value >= beta) { @@ -712,15 +710,15 @@ Value Search::Worker::search( if (!is_valid(ttDataNext.value)) return ttData.value; + if (ttData.value >= beta && -ttDataNext.value >= beta) return ttData.value; + if (ttData.value <= alpha && -ttDataNext.value <= alpha) return ttData.value; } else - { return ttData.value; - } } } @@ -1215,7 +1213,7 @@ moves_loop: // When in check, search starts here if (cutNode) r += 2864 + 966 * !ttData.move; - // Increase reduction if ttMove is a capture but the current move is not a capture + // Increase reduction if ttMove is a capture if (ttCapture) r += 1210 + (depth < 8) * 963; @@ -1253,7 +1251,7 @@ moves_loop: // When in check, search starts here // std::clamp has been replaced by a more robust implementation. Depth d = std::max(1, std::min(newDepth - r / 1024, newDepth + !allNode + (PvNode && !bestMove))) - + ((ss - 1)->isPvNode); + + (ss - 1)->isPvNode; ss->reduction = newDepth - d; value = -search(pos, ss + 1, -(alpha + 1), -alpha, d, true); @@ -1440,10 +1438,7 @@ moves_loop: // When in check, search starts here update_all_stats(pos, ss, *this, bestMove, prevSq, quietsSearched, capturesSearched, depth, ttData.move, moveCount); if (!PvNode) - { - int bonus = bestMove == ttData.move ? 800 : -879; - ttMoveHistory << bonus; - } + ttMoveHistory << (bestMove == ttData.move ? 800 : -879); } // Bonus for prior quiet countermove that caused the fail low