1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-03 01:59:36 +00:00

Reformat some comments and conditions

closes https://github.com/official-stockfish/Stockfish/pull/4814

No functional change
This commit is contained in:
Stéphane Nicolet 2023-10-01 19:18:05 +02:00 committed by Joost VandeVondele
parent a4fedd8152
commit fe53a18f7a
3 changed files with 56 additions and 55 deletions

View file

@ -236,9 +236,10 @@ namespace {
/// <CAPTURES> Generates all pseudo-legal captures plus queen promotions /// <CAPTURES> Generates all pseudo-legal captures plus queen promotions
/// <QUIETS> Generates all pseudo-legal non-captures and underpromotions /// <QUIETS> Generates all pseudo-legal non-captures and underpromotions
/// <EVASIONS> Generates all pseudo-legal check evasions when the side to move is in check /// <EVASIONS> Generates all pseudo-legal check evasions
/// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check, except castling and promotions
/// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
/// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check,
/// except castling and promotions
/// ///
/// Returns a pointer to the end of the move list. /// Returns a pointer to the end of the move list.

View file

@ -103,8 +103,8 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) {
// Implements Marcel van Kervinck's cuckoo algorithm to detect repetition of positions // Implements Marcel van Kervinck's cuckoo algorithm to detect repetition of positions
// for 3-fold repetition draws. The algorithm uses two hash tables with Zobrist hashes to // for 3-fold repetition draws. The algorithm uses two hash tables with Zobrist hashes
// allow fast detection of recurring positions. For details see: // to allow fast detection of recurring positions. For details see:
// http://web.archive.org/web/20201107002606/https://marcelk.net/2013-04-06/paper/upcoming-rep-v2.pdf // http://web.archive.org/web/20201107002606/https://marcelk.net/2013-04-06/paper/upcoming-rep-v2.pdf
// First and second hash functions for indexing the cuckoo tables // First and second hash functions for indexing the cuckoo tables
@ -188,9 +188,9 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
4) En passant target square (in algebraic notation). If there's no en passant 4) En passant target square (in algebraic notation). If there's no en passant
target square, this is "-". If a pawn has just made a 2-square move, this target square, this is "-". If a pawn has just made a 2-square move, this
is the position "behind" the pawn. Following X-FEN standard, this is recorded only is the position "behind" the pawn. Following X-FEN standard, this is recorded
if there is a pawn in position to make an en passant capture, and if there really only if there is a pawn in position to make an en passant capture, and if
is a pawn that might have advanced two squares. there really is a pawn that might have advanced two squares.
5) Halfmove clock. This is the number of halfmoves since the last pawn advance 5) Halfmove clock. This is the number of halfmoves since the last pawn advance
or capture. This is used to determine if a draw can be claimed under the or capture. This is used to determine if a draw can be claimed under the
@ -959,7 +959,7 @@ void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Squ
// Remove both pieces first since squares could overlap in Chess960 // Remove both pieces first since squares could overlap in Chess960
remove_piece(Do ? from : to); remove_piece(Do ? from : to);
remove_piece(Do ? rfrom : rto); remove_piece(Do ? rfrom : rto);
board[Do ? from : to] = board[Do ? rfrom : rto] = NO_PIECE; // Since remove_piece doesn't do this for us board[Do ? from : to] = board[Do ? rfrom : rto] = NO_PIECE; // remove_piece does not do this for us
put_piece(make_piece(us, KING), Do ? to : from); put_piece(make_piece(us, KING), Do ? to : from);
put_piece(make_piece(us, ROOK), Do ? rto : rfrom); put_piece(make_piece(us, ROOK), Do ? rto : rfrom);
} }

View file

@ -100,12 +100,12 @@ namespace {
return VALUE_DRAW - 1 + Value(thisThread->nodes & 0x2); return VALUE_DRAW - 1 + Value(thisThread->nodes & 0x2);
} }
// Skill structure is used to implement strength limit. // Skill structure is used to implement strength limit. If we have a UCI_Elo,
// If we have a UCI_Elo, we convert it to an appropriate skill level, anchored to the Stash engine. // we convert it to an appropriate skill level, anchored to the Stash engine.
// This method is based on a fit of the Elo results for games played between the master at various // This method is based on a fit of the Elo results for games played between
// skill levels and various versions of the Stash engine, all ranked at CCRL. // Stockfish at various skill levels and various versions of the Stash engine.
// Skill 0 .. 19 now covers CCRL Blitz Elo from 1320 to 3190, approximately // Skill 0 .. 19 now covers CCRL Blitz Elo from 1320 to 3190, approximately
// Reference: https://github.com/vondele/Stockfish/commit/a08b8d4e9711c20acedbfe17d618c3c384b339ec // Reference: https://github.com/vondele/Stockfish/commit/a08b8d4e9711c2
struct Skill { struct Skill {
Skill(int skill_level, int uci_elo) { Skill(int skill_level, int uci_elo) {
if (uci_elo) if (uci_elo)
@ -274,9 +274,9 @@ void MainThread::search() {
void Thread::search() { void Thread::search() {
// Allocate stack with extra size to allow access from (ss-7) to (ss+2) // Allocate stack with extra size to allow access from (ss-7) to (ss+2):
// (ss-7) is needed for update_continuation_histories(ss-1, ...) which accesses (ss-6) // (ss-7) is needed for update_continuation_histories(ss-1) which accesses (ss-6),
// (ss+2) is needed for initialization of statScore and killers // (ss+2) is needed for initialization of statScore and killers.
Stack stack[MAX_PLY+10], *ss = stack+7; Stack stack[MAX_PLY+10], *ss = stack+7;
Move pv[MAX_PLY+1]; Move pv[MAX_PLY+1];
Value alpha, beta, delta; Value alpha, beta, delta;
@ -478,8 +478,7 @@ void Thread::search() {
double totalTime = Time.optimum() * fallingEval * reduction * bestMoveInstability; double totalTime = Time.optimum() * fallingEval * reduction * bestMoveInstability;
// Cap used time in case of a single legal move for a better viewer experience in tournaments // Cap used time in case of a single legal move for a better viewer experience
// yielding correct scores and sufficiently fast moves.
if (rootMoves.size() == 1) if (rootMoves.size() == 1)
totalTime = std::min(500.0, totalTime); totalTime = std::min(500.0, totalTime);
@ -574,7 +573,8 @@ namespace {
static_cast<MainThread*>(thisThread)->check_time(); static_cast<MainThread*>(thisThread)->check_time();
// Used to send selDepth info to GUI (selDepth counts from 1, ply from 0) // Used to send selDepth info to GUI (selDepth counts from 1, ply from 0)
if (PvNode && thisThread->selDepth < ss->ply + 1) if ( PvNode
&& thisThread->selDepth < ss->ply + 1)
thisThread->selDepth = ss->ply + 1; thisThread->selDepth = ss->ply + 1;
if (!rootNode) if (!rootNode)
@ -640,7 +640,9 @@ namespace {
update_quiet_stats(pos, ss, ttMove, stat_bonus(depth)); update_quiet_stats(pos, ss, ttMove, stat_bonus(depth));
// Extra penalty for early quiet moves of the previous ply (~0 Elo on STC, ~2 Elo on LTC) // Extra penalty for early quiet moves of the previous ply (~0 Elo on STC, ~2 Elo on LTC)
if (prevSq != SQ_NONE && (ss-1)->moveCount <= 2 && !priorCapture) if ( prevSq != SQ_NONE
&& (ss-1)->moveCount <= 2
&& !priorCapture)
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + 1)); update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + 1));
} }
// Penalty for a quiet ttMove that fails low (~1 Elo) // Penalty for a quiet ttMove that fails low (~1 Elo)
@ -748,7 +750,9 @@ namespace {
} }
// Use static evaluation difference to improve quiet move ordering (~4 Elo) // Use static evaluation difference to improve quiet move ordering (~4 Elo)
if (is_ok((ss-1)->currentMove) && !(ss-1)->inCheck && !priorCapture) if ( is_ok((ss-1)->currentMove)
&& !(ss-1)->inCheck
&& !priorCapture)
{ {
int bonus = std::clamp(-18 * int((ss-1)->staticEval + ss->staticEval), -1812, 1812); int bonus = std::clamp(-18 * int((ss-1)->staticEval + ss->staticEval), -1812, 1812);
thisThread->mainHistory[~us][from_to((ss-1)->currentMove)] << bonus; thisThread->mainHistory[~us][from_to((ss-1)->currentMove)] << bonus;
@ -979,7 +983,8 @@ moves_loop: // When in check, search starts here
Depth r = reduction(improving, depth, moveCount, delta, thisThread->rootDelta); Depth r = reduction(improving, depth, moveCount, delta, thisThread->rootDelta);
// Step 14. Pruning at shallow depth (~120 Elo). Depth conditions are important for mate finding. // Step 14. Pruning at shallow depth (~120 Elo).
// Depth conditions are important for mate finding.
if ( !rootNode if ( !rootNode
&& pos.non_pawn_material(us) && pos.non_pawn_material(us)
&& bestValue > VALUE_TB_LOSS_IN_MAX_PLY) && bestValue > VALUE_TB_LOSS_IN_MAX_PLY)
@ -1052,7 +1057,6 @@ moves_loop: // When in check, search starts here
&& depth >= 4 - (thisThread->completedDepth > 24) + 2 * (PvNode && tte->is_pv()) && depth >= 4 - (thisThread->completedDepth > 24) + 2 * (PvNode && tte->is_pv())
&& move == ttMove && move == ttMove
&& !excludedMove // Avoid recursive singular search && !excludedMove // Avoid recursive singular search
/* && ttValue != VALUE_NONE Already implicit in the next condition */
&& abs(ttValue) < VALUE_TB_WIN_IN_MAX_PLY && abs(ttValue) < VALUE_TB_WIN_IN_MAX_PLY
&& (tte->bound() & BOUND_LOWER) && (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3) && tte->depth() >= depth - 3)
@ -1130,8 +1134,7 @@ moves_loop: // When in check, search starts here
// Step 16. Make the move // Step 16. Make the move
pos.do_move(move, st, givesCheck); pos.do_move(move, st, givesCheck);
// Decrease reduction if position is or has been on the PV and not likely to fail low. (~3 Elo) // Decrease reduction if position is or has been on the PV (~4 Elo)
// Decrease further on cutNodes. (~1 Elo)
if ( ss->ttPv if ( ss->ttPv
&& !likelyFailLow) && !likelyFailLow)
r -= cutNode && tte->depth() >= depth ? 3 : 2; r -= cutNode && tte->depth() >= depth ? 3 : 2;
@ -1196,10 +1199,11 @@ moves_loop: // When in check, search starts here
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, true); value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, true);
// Do a full-depth search when reduced LMR search fails high // Do a full-depth search when reduced LMR search fails high
if (value > alpha && d < newDepth) if ( value > alpha
&& d < newDepth)
{ {
// Adjust full-depth search based on LMR results - if the result // Adjust full-depth search based on LMR results - if the result
// was good enough search deeper, if it was bad enough search shallower // was good enough search deeper, if it was bad enough search shallower.
const bool doDeeperSearch = value > (bestValue + 51 + 10 * (newDepth - d)); const bool doDeeperSearch = value > (bestValue + 51 + 10 * (newDepth - d));
const bool doEvenDeeperSearch = value > alpha + 700 && ss->doubleExtensions <= 6; const bool doEvenDeeperSearch = value > alpha + 700 && ss->doubleExtensions <= 6;
const bool doShallowerSearch = value < bestValue + newDepth; const bool doShallowerSearch = value < bestValue + newDepth;
@ -1219,19 +1223,22 @@ moves_loop: // When in check, search starts here
} }
} }
// Step 18. Full-depth search when LMR is skipped. If expected reduction is high, reduce its depth by 1. // Step 18. Full-depth search when LMR is skipped
else if (!PvNode || moveCount > 1) else if (!PvNode || moveCount > 1)
{ {
// Increase reduction for cut nodes and not ttMove (~1 Elo) // Increase reduction for cut nodes and not ttMove (~1 Elo)
if (!ttMove && cutNode) if ( !ttMove
&& cutNode)
r += 2; r += 2;
// Note that if expected reduction is high, we reduce search depth by 1 here
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth - (r > 3), !cutNode); value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth - (r > 3), !cutNode);
} }
// For PV nodes only, do a full PV search on the first move or after a fail high, // For PV nodes only, do a full PV search on the first move or after a fail high,
// otherwise let the parent node fail low with value <= alpha and try another move. // otherwise let the parent node fail low with value <= alpha and try another move.
if (PvNode && (moveCount == 1 || value > alpha)) if ( PvNode
&& (moveCount == 1 || value > alpha))
{ {
(ss+1)->pv = pv; (ss+1)->pv = pv;
(ss+1)->pv[0] = MOVE_NONE; (ss+1)->pv[0] = MOVE_NONE;
@ -1329,8 +1336,8 @@ moves_loop: // When in check, search starts here
} }
} }
// If the move is worse than some previously searched move,
// If the move is worse than some previously searched move, remember it, to update its stats later // remember it, to update its stats later.
if (move != bestMove && moveCount <= 32) if (move != bestMove && moveCount <= 32)
{ {
if (capture) if (capture)
@ -1341,14 +1348,6 @@ moves_loop: // When in check, search starts here
} }
} }
// The following condition would detect a stop only after move loop has been
// completed. But in this case, bestValue is valid because we have fully
// searched our subtree, and we can anyhow save the result in TT.
/*
if (Threads.stop)
return VALUE_DRAW;
*/
// Step 21. Check for mate and stalemate // Step 21. Check for mate and stalemate
// All legal moves have been searched and if there are no legal moves, it // All legal moves have been searched and if there are no legal moves, it
// must be a mate or a stalemate. If we are in a singular extension search then // must be a mate or a stalemate. If we are in a singular extension search then
@ -1494,7 +1493,6 @@ moves_loop: // When in check, search starts here
// Stand pat. Return immediately if static value is at least beta // Stand pat. Return immediately if static value is at least beta
if (bestValue >= beta) if (bestValue >= beta)
{ {
// Save gathered info in transposition table
if (!ss->ttHit) if (!ss->ttHit)
tte->save(posKey, value_to_tt(bestValue, ss->ply), false, BOUND_LOWER, tte->save(posKey, value_to_tt(bestValue, ss->ply), false, BOUND_LOWER,
DEPTH_NONE, MOVE_NONE, ss->staticEval); DEPTH_NONE, MOVE_NONE, ss->staticEval);
@ -1539,8 +1537,9 @@ moves_loop: // When in check, search starts here
moveCount++; moveCount++;
// Step 6. Pruning. // Step 6. Pruning
if (bestValue > VALUE_TB_LOSS_IN_MAX_PLY && pos.non_pawn_material(us)) if ( bestValue > VALUE_TB_LOSS_IN_MAX_PLY
&& pos.non_pawn_material(us))
{ {
// Futility pruning and moveCount pruning (~10 Elo) // Futility pruning and moveCount pruning (~10 Elo)
if ( !givesCheck if ( !givesCheck
@ -1554,7 +1553,7 @@ moves_loop: // When in check, search starts here
futilityValue = futilityBase + PieceValue[pos.piece_on(to_sq(move))]; futilityValue = futilityBase + PieceValue[pos.piece_on(to_sq(move))];
// If static eval + value of piece we are going to capture is much lower // If static eval + value of piece we are going to capture is much lower
// than alpha we can prune this move // than alpha we can prune this move.
if (futilityValue <= alpha) if (futilityValue <= alpha)
{ {
bestValue = std::max(bestValue, futilityValue); bestValue = std::max(bestValue, futilityValue);
@ -1562,15 +1561,16 @@ moves_loop: // When in check, search starts here
} }
// If static eval is much lower than alpha and move is not winning material // If static eval is much lower than alpha and move is not winning material
// we can prune this move // we can prune this move.
if (futilityBase <= alpha && !pos.see_ge(move, VALUE_ZERO + 1)) if ( futilityBase <= alpha
&& !pos.see_ge(move, VALUE_ZERO + 1))
{ {
bestValue = std::max(bestValue, futilityBase); bestValue = std::max(bestValue, futilityBase);
continue; continue;
} }
// If static exchange evaluation is much worse than what is needed to not // If static exchange evaluation is much worse than what is needed to not
// fall below alpha we can prune this move // fall below alpha we can prune this move.
if (futilityBase > alpha && !pos.see_ge(move, (alpha - futilityBase) * 4)) if (futilityBase > alpha && !pos.see_ge(move, (alpha - futilityBase) * 4))
{ {
bestValue = alpha; bestValue = alpha;
@ -1655,8 +1655,8 @@ moves_loop: // When in check, search starts here
} }
// value_to_tt() adjusts a mate or TB score from "plies to mate from the root" to // value_to_tt() adjusts a mate or TB score from "plies to mate from the root"
// "plies to mate from the current position". Standard scores are unchanged. // to "plies to mate from the current position". Standard scores are unchanged.
// The function is called before storing a value in the transposition table. // The function is called before storing a value in the transposition table.
Value value_to_tt(Value v, int ply) { Value value_to_tt(Value v, int ply) {
@ -1670,9 +1670,9 @@ moves_loop: // When in check, search starts here
// value_from_tt() is the inverse of value_to_tt(): it adjusts a mate or TB score // value_from_tt() is the inverse of value_to_tt(): it adjusts a mate or TB score
// from the transposition table (which refers to the plies to mate/be mated from // from the transposition table (which refers to the plies to mate/be mated from
// current position) to "plies to mate/be mated (TB win/loss) from the root". However, // current position) to "plies to mate/be mated (TB win/loss) from the root".
// for mate scores, to avoid potentially false mate scores related to the 50 moves rule // However, to avoid potentially false mate scores related to the 50 moves rule
// and the graph history interaction, we return an optimal TB score instead. // and the graph history interaction problem, we return an optimal TB score instead.
Value value_from_tt(Value v, int ply, int r50c) { Value value_from_tt(Value v, int ply, int r50c) {