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>
This commit is contained in:
Nonlinear2
2025-05-23 18:33:01 +02:00
committed by Joost VandeVondele
parent f58d923fe0
commit b1b5893a8e
3 changed files with 25 additions and 27 deletions

View File

@@ -173,7 +173,7 @@ void MovePicker::score() {
if (KNIGHT <= pt && pt <= QUEEN) if (KNIGHT <= pt && pt <= QUEEN)
{ {
static constexpr int bonus[QUEEN + 1] = {0, 0, 144, 144, 256, 517}; 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; m.value += bonus[pt] * v;
} }
@@ -200,7 +200,7 @@ void MovePicker::score() {
template<typename Pred> template<typename Pred>
Move MovePicker::select(Pred filter) { Move MovePicker::select(Pred filter) {
for (; cur < endMoves; ++cur) for (; cur < endCur; ++cur)
if (*cur != ttMove && filter()) if (*cur != ttMove && filter())
return *cur++; return *cur++;
@@ -227,10 +227,10 @@ top:
case PROBCUT_INIT : case PROBCUT_INIT :
case QCAPTURE_INIT : case QCAPTURE_INIT :
cur = endBadCaptures = moves; cur = endBadCaptures = moves;
endMoves = generate<CAPTURES>(pos, cur); endCur = generate<CAPTURES>(pos, cur);
score<CAPTURES>(); score<CAPTURES>();
partial_insertion_sort(cur, endMoves, std::numeric_limits<int>::min()); partial_insertion_sort(cur, endCur, std::numeric_limits<int>::min());
++stage; ++stage;
goto top; goto top;
@@ -250,10 +250,10 @@ top:
if (!skipQuiets) if (!skipQuiets)
{ {
cur = endBadQuiets = endBadCaptures; cur = endBadQuiets = endBadCaptures;
endMoves = generate<QUIETS>(pos, cur); endCur = generate<QUIETS>(pos, cur);
score<QUIETS>(); score<QUIETS>();
partial_insertion_sort(cur, endMoves, -3560 * depth); partial_insertion_sort(cur, endCur, -3560 * depth);
} }
++stage; ++stage;
@@ -261,13 +261,16 @@ top:
case GOOD_QUIET : case GOOD_QUIET :
if (!skipQuiets && select([&]() { if (!skipQuiets && select([&]() {
return cur->value > -14000 ? true : (*endBadQuiets++ = *cur, false); if (cur->value > -14000)
return true;
*endBadQuiets++ = *cur;
return false;
})) }))
return *(cur - 1); return *(cur - 1);
// Prepare the pointers to loop over the bad captures // Prepare the pointers to loop over the bad captures
cur = moves; cur = moves;
endMoves = endBadCaptures; endCur = endBadCaptures;
++stage; ++stage;
[[fallthrough]]; [[fallthrough]];
@@ -277,8 +280,8 @@ top:
return *(cur - 1); return *(cur - 1);
// Prepare the pointers to loop over the bad quiets // Prepare the pointers to loop over the bad quiets
cur = endBadCaptures; cur = endBadCaptures;
endMoves = endBadQuiets; endCur = endBadQuiets;
++stage; ++stage;
[[fallthrough]]; [[fallthrough]];
@@ -290,11 +293,11 @@ top:
return Move::none(); return Move::none();
case EVASION_INIT : case EVASION_INIT :
cur = moves; cur = moves;
endMoves = generate<EVASIONS>(pos, cur); endCur = generate<EVASIONS>(pos, cur);
score<EVASIONS>(); score<EVASIONS>();
partial_insertion_sort(cur, endMoves, std::numeric_limits<int>::min()); partial_insertion_sort(cur, endCur, std::numeric_limits<int>::min());
++stage; ++stage;
[[fallthrough]]; [[fallthrough]];
@@ -317,7 +320,7 @@ bool MovePicker::can_move_king_or_pawn() {
// SEE negative captures shouldn't be returned in GOOD_CAPTURE stage // SEE negative captures shouldn't be returned in GOOD_CAPTURE stage
assert(stage > GOOD_CAPTURE && stage != EVASION_INIT); 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)); PieceType movedPieceType = type_of(pos.moved_piece(*m));
if ((movedPieceType == PAWN || movedPieceType == KING) && pos.legal(*m)) if ((movedPieceType == PAWN || movedPieceType == KING) && pos.legal(*m))

View File

@@ -58,7 +58,7 @@ class MovePicker {
template<GenType> template<GenType>
void score(); void score();
ExtMove* begin() { return cur; } ExtMove* begin() { return cur; }
ExtMove* end() { return endMoves; } ExtMove* end() { return endCur; }
const Position& pos; const Position& pos;
const ButterflyHistory* mainHistory; const ButterflyHistory* mainHistory;
@@ -67,7 +67,7 @@ class MovePicker {
const PieceToHistory** continuationHistory; const PieceToHistory** continuationHistory;
const PawnHistory* pawnHistory; const PawnHistory* pawnHistory;
Move ttMove; Move ttMove;
ExtMove * cur, *endMoves, *endBadCaptures, *endBadQuiets; ExtMove * cur, *endCur, *endBadCaptures, *endBadQuiets;
int stage; int stage;
int threshold; int threshold;
Depth depth; Depth depth;

View File

@@ -680,8 +680,6 @@ Value Search::Worker::search(
&& (ttData.bound & (ttData.value >= beta ? BOUND_LOWER : BOUND_UPPER)) && (ttData.bound & (ttData.value >= beta ? BOUND_LOWER : BOUND_UPPER))
&& (cutNode == (ttData.value >= beta) || depth > 5)) && (cutNode == (ttData.value >= beta) || depth > 5))
{ {
// If ttMove is quiet, update move sorting heuristics on TT hit // If ttMove is quiet, update move sorting heuristics on TT hit
if (ttData.move && ttData.value >= beta) if (ttData.move && ttData.value >= beta)
{ {
@@ -712,15 +710,15 @@ Value Search::Worker::search(
if (!is_valid(ttDataNext.value)) if (!is_valid(ttDataNext.value))
return ttData.value; return ttData.value;
if (ttData.value >= beta && -ttDataNext.value >= beta) if (ttData.value >= beta && -ttDataNext.value >= beta)
return ttData.value; return ttData.value;
if (ttData.value <= alpha && -ttDataNext.value <= alpha) if (ttData.value <= alpha && -ttDataNext.value <= alpha)
return ttData.value; return ttData.value;
} }
else else
{
return ttData.value; return ttData.value;
}
} }
} }
@@ -1215,7 +1213,7 @@ moves_loop: // When in check, search starts here
if (cutNode) if (cutNode)
r += 2864 + 966 * !ttData.move; 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) if (ttCapture)
r += 1210 + (depth < 8) * 963; 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. // std::clamp has been replaced by a more robust implementation.
Depth d = std::max(1, std::min(newDepth - r / 1024, Depth d = std::max(1, std::min(newDepth - r / 1024,
newDepth + !allNode + (PvNode && !bestMove))) newDepth + !allNode + (PvNode && !bestMove)))
+ ((ss - 1)->isPvNode); + (ss - 1)->isPvNode;
ss->reduction = newDepth - d; ss->reduction = newDepth - d;
value = -search<NonPV>(pos, ss + 1, -(alpha + 1), -alpha, d, true); value = -search<NonPV>(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, update_all_stats(pos, ss, *this, bestMove, prevSq, quietsSearched, capturesSearched, depth,
ttData.move, moveCount); ttData.move, moveCount);
if (!PvNode) if (!PvNode)
{ ttMoveHistory << (bestMove == ttData.move ? 800 : -879);
int bonus = bestMove == ttData.move ? 800 : -879;
ttMoveHistory << bonus;
}
} }
// Bonus for prior quiet countermove that caused the fail low // Bonus for prior quiet countermove that caused the fail low