Skip to content

Commit

Permalink
Reformat some comments and conditions
Browse files Browse the repository at this point in the history
closes #4814

No functional change
  • Loading branch information
snicolet authored and vondele committed Oct 21, 2023
1 parent a4fedd8 commit fe53a18
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
5 changes: 3 additions & 2 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ namespace {

/// <CAPTURES> Generates all pseudo-legal captures plus queen promotions
/// <QUIETS> Generates all pseudo-legal non-captures and underpromotions
/// <EVASIONS> Generates all pseudo-legal check evasions when the side to move is in check
/// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check, except castling and promotions
/// <EVASIONS> Generates all pseudo-legal check evasions
/// <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.

Expand Down
18 changes: 9 additions & 9 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) {
}


// 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
// allow fast detection of recurring positions. For details see:
// 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 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

// First and second hash functions for indexing the cuckoo tables
Expand Down Expand Up @@ -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
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
if there is a pawn in position to make an en passant capture, and if there really
is a pawn that might have advanced two squares.
is the position "behind" the pawn. Following X-FEN standard, this is recorded
only if there is a pawn in position to make an en passant capture, and if
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
or capture. This is used to determine if a draw can be claimed under the
Expand Down Expand Up @@ -587,8 +587,8 @@ bool Position::pseudo_legal(const Move m) const {
return false;

if ( !(pawn_attacks_bb(us, from) & pieces(~us) & to) // Not a capture
&& !((from + pawn_push(us) == to) && empty(to)) // Not a single push
&& !( (from + 2 * pawn_push(us) == to) // Not a double push
&& !((from + pawn_push(us) == to) && empty(to)) // Not a single push
&& !( (from + 2 * pawn_push(us) == to) // Not a double push
&& (relative_rank(us, from) == RANK_2)
&& empty(to)
&& empty(to - pawn_push(us))))
Expand Down Expand Up @@ -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_piece(Do ? from : to);
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, ROOK), Do ? rto : rfrom);
}
Expand Down
88 changes: 44 additions & 44 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ namespace {
return VALUE_DRAW - 1 + Value(thisThread->nodes & 0x2);
}

// Skill structure is used to implement strength limit.
// If we have a UCI_Elo, 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
// skill levels and various versions of the Stash engine, all ranked at CCRL.
// Skill structure is used to implement strength limit. If we have a UCI_Elo,
// 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
// 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
// Reference: https://github.com/vondele/Stockfish/commit/a08b8d4e9711c20acedbfe17d618c3c384b339ec
// Reference: https://github.com/vondele/Stockfish/commit/a08b8d4e9711c2
struct Skill {
Skill(int skill_level, int uci_elo) {
if (uci_elo)
Expand Down Expand Up @@ -274,9 +274,9 @@ void MainThread::search() {

void Thread::search() {

// 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+2) is needed for initialization of statScore and killers
// 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+2) is needed for initialization of statScore and killers.
Stack stack[MAX_PLY+10], *ss = stack+7;
Move pv[MAX_PLY+1];
Value alpha, beta, delta;
Expand Down Expand Up @@ -478,8 +478,7 @@ void Thread::search() {

double totalTime = Time.optimum() * fallingEval * reduction * bestMoveInstability;

// Cap used time in case of a single legal move for a better viewer experience in tournaments
// yielding correct scores and sufficiently fast moves.
// Cap used time in case of a single legal move for a better viewer experience
if (rootMoves.size() == 1)
totalTime = std::min(500.0, totalTime);

Expand Down Expand Up @@ -574,7 +573,8 @@ namespace {
static_cast<MainThread*>(thisThread)->check_time();

// 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;

if (!rootNode)
Expand Down Expand Up @@ -640,7 +640,9 @@ namespace {
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)
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));
}
// Penalty for a quiet ttMove that fails low (~1 Elo)
Expand Down Expand Up @@ -748,7 +750,9 @@ namespace {
}

// 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);
thisThread->mainHistory[~us][from_to((ss-1)->currentMove)] << bonus;
Expand Down Expand Up @@ -979,7 +983,8 @@ namespace {

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
&& pos.non_pawn_material(us)
&& bestValue > VALUE_TB_LOSS_IN_MAX_PLY)
Expand Down Expand Up @@ -1052,7 +1057,6 @@ namespace {
&& depth >= 4 - (thisThread->completedDepth > 24) + 2 * (PvNode && tte->is_pv())
&& move == ttMove
&& !excludedMove // Avoid recursive singular search
/* && ttValue != VALUE_NONE Already implicit in the next condition */
&& abs(ttValue) < VALUE_TB_WIN_IN_MAX_PLY
&& (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3)
Expand Down Expand Up @@ -1130,8 +1134,7 @@ namespace {
// Step 16. Make the move
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 further on cutNodes. (~1 Elo)
// Decrease reduction if position is or has been on the PV (~4 Elo)
if ( ss->ttPv
&& !likelyFailLow)
r -= cutNode && tte->depth() >= depth ? 3 : 2;
Expand Down Expand Up @@ -1196,10 +1199,11 @@ namespace {
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, true);

// 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
// 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 doEvenDeeperSearch = value > alpha + 700 && ss->doubleExtensions <= 6;
const bool doShallowerSearch = value < bestValue + newDepth;
Expand All @@ -1219,19 +1223,22 @@ namespace {
}
}

// 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)
{
// Increase reduction for cut nodes and not ttMove (~1 Elo)
if (!ttMove && cutNode)
if ( !ttMove
&& cutNode)
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);
}

// 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.
if (PvNode && (moveCount == 1 || value > alpha))
if ( PvNode
&& (moveCount == 1 || value > alpha))
{
(ss+1)->pv = pv;
(ss+1)->pv[0] = MOVE_NONE;
Expand Down Expand Up @@ -1329,8 +1336,8 @@ namespace {
}
}


// If the move is worse than some previously searched move, remember it, to update its stats later
// If the move is worse than some previously searched move,
// remember it, to update its stats later.
if (move != bestMove && moveCount <= 32)
{
if (capture)
Expand All @@ -1341,14 +1348,6 @@ namespace {
}
}

// 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
// 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
Expand Down Expand Up @@ -1494,7 +1493,6 @@ namespace {
// Stand pat. Return immediately if static value is at least beta
if (bestValue >= beta)
{
// Save gathered info in transposition table
if (!ss->ttHit)
tte->save(posKey, value_to_tt(bestValue, ss->ply), false, BOUND_LOWER,
DEPTH_NONE, MOVE_NONE, ss->staticEval);
Expand Down Expand Up @@ -1539,8 +1537,9 @@ namespace {

moveCount++;

// Step 6. Pruning.
if (bestValue > VALUE_TB_LOSS_IN_MAX_PLY && pos.non_pawn_material(us))
// Step 6. Pruning
if ( bestValue > VALUE_TB_LOSS_IN_MAX_PLY
&& pos.non_pawn_material(us))
{
// Futility pruning and moveCount pruning (~10 Elo)
if ( !givesCheck
Expand All @@ -1554,23 +1553,24 @@ namespace {
futilityValue = futilityBase + PieceValue[pos.piece_on(to_sq(move))];

// 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)
{
bestValue = std::max(bestValue, futilityValue);
continue;
}

// If static eval is much lower than alpha and move is not winning material
// we can prune this move
if (futilityBase <= alpha && !pos.see_ge(move, VALUE_ZERO + 1))
// we can prune this move.
if ( futilityBase <= alpha
&& !pos.see_ge(move, VALUE_ZERO + 1))
{
bestValue = std::max(bestValue, futilityBase);
continue;
}

// 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))
{
bestValue = alpha;
Expand Down Expand Up @@ -1655,8 +1655,8 @@ namespace {
}


// value_to_tt() adjusts a mate or TB score from "plies to mate from the root" to
// "plies to mate from the current position". Standard scores are unchanged.
// value_to_tt() adjusts a mate or TB score from "plies to mate from the root"
// to "plies to mate from the current position". Standard scores are unchanged.
// The function is called before storing a value in the transposition table.

Value value_to_tt(Value v, int ply) {
Expand All @@ -1670,9 +1670,9 @@ namespace {

// 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
// current position) to "plies to mate/be mated (TB win/loss) from the root". However,
// for mate scores, to avoid potentially false mate scores related to the 50 moves rule
// and the graph history interaction, we return an optimal TB score instead.
// current position) to "plies to mate/be mated (TB win/loss) from the root".
// However, to avoid potentially false mate scores related to the 50 moves rule
// and the graph history interaction problem, we return an optimal TB score instead.

Value value_from_tt(Value v, int ply, int r50c) {

Expand Down

0 comments on commit fe53a18

Please sign in to comment.