@@ -676,6 +676,7 @@ namespace {
676676 ss->ttPv = PvNode || (ss->ttHit && tte->is_pv ());
677677 formerPv = ss->ttPv && !PvNode;
678678
679+ // Update low ply history for previous move if we are near root and position is or has been in PV
679680 if ( ss->ttPv
680681 && depth > 12
681682 && ss->ply - 1 < MAX_LPH
@@ -700,6 +701,7 @@ namespace {
700701 {
701702 if (ttValue >= beta)
702703 {
704+ // Bonus for a quiet ttMove that fails high
703705 if (!pos.capture_or_promotion (ttMove))
704706 update_quiet_stats (pos, ss, ttMove, stat_bonus (depth), depth);
705707
@@ -716,6 +718,8 @@ namespace {
716718 }
717719 }
718720
721+ // Partial workaround for the graph history interaction problem
722+ // For high rule50 counts don't produce transposition table cutoffs.
719723 if (pos.rule50_count () < 90 )
720724 return ttValue;
721725 }
@@ -789,6 +793,7 @@ namespace {
789793 if (eval == VALUE_NONE)
790794 ss->staticEval = eval = evaluate (pos);
791795
796+ // Randomize draw evaluation
792797 if (eval == VALUE_DRAW)
793798 eval = value_draw (thisThread);
794799
@@ -799,11 +804,14 @@ namespace {
799804 }
800805 else
801806 {
807+ // In case of null move search use previous static eval with a different sign
808+ // and addition of two tempos
802809 if ((ss-1 )->currentMove != MOVE_NULL)
803810 ss->staticEval = eval = evaluate (pos);
804811 else
805812 ss->staticEval = eval = -(ss-1 )->staticEval + 2 * Tempo;
806813
814+ // Save static evaluation into transposition table
807815 tte->save (posKey, VALUE_NONE, ss->ttPv , BOUND_NONE, DEPTH_NONE, MOVE_NONE, eval);
808816 }
809817
@@ -822,6 +830,10 @@ namespace {
822830 && eval <= alpha - RazorMargin)
823831 return qsearch<NT>(pos, ss, alpha, beta);
824832
833+ // Set up improving flag that is used in various pruning heuristics
834+ // We define position as improving if static evaluation of position is better
835+ // Than the previous static evaluation at our turn
836+ // In case of us being in check at our previous move we look at move prior to it
825837 improving = (ss-2 )->staticEval == VALUE_NONE
826838 ? ss->staticEval > (ss-4 )->staticEval || (ss-4 )->staticEval == VALUE_NONE
827839 : ss->staticEval > (ss-2 )->staticEval ;
@@ -1183,6 +1195,7 @@ namespace {
11831195 if ((rootNode || !PvNode) && depth > 10 && thisThread->bestMoveChanges <= 2 )
11841196 r++;
11851197
1198+ // More reductions for late moves if position was not in previous PV
11861199 if (moveCountPruning && !formerPv)
11871200 r++;
11881201
@@ -1258,6 +1271,7 @@ namespace {
12581271 {
12591272 value = -search<NonPV>(pos, ss+1 , -(alpha+1 ), -alpha, newDepth, !cutNode);
12601273
1274+ // If the move passed LMR update its stats
12611275 if (didLMR && !captureOrPromotion)
12621276 {
12631277 int bonus = value > alpha ? stat_bonus (newDepth)
@@ -1309,8 +1323,7 @@ namespace {
13091323 rm.pv .push_back (*m);
13101324
13111325 // We record how often the best move has been changed in each
1312- // iteration. This information is used for time management: when
1313- // the best move changes frequently, we allocate some more time.
1326+ // iteration. This information is used for time management and LMR
13141327 if (moveCount > 1 )
13151328 ++thisThread->bestMoveChanges ;
13161329 }
@@ -1343,6 +1356,7 @@ namespace {
13431356 }
13441357 }
13451358
1359+ // If the move is worse than some previously searched move, remember it to update its stats later
13461360 if (move != bestMove)
13471361 {
13481362 if (captureOrPromotion && captureCount < 32 )
@@ -1372,6 +1386,7 @@ namespace {
13721386 bestValue = excludedMove ? alpha
13731387 : ss->inCheck ? mated_in (ss->ply ) : VALUE_DRAW;
13741388
1389+ // If there is a move which produces search value greater than alpha we update stats of searched moves
13751390 else if (bestMove)
13761391 update_all_stats (pos, ss, bestMove, bestValue, beta, prevSq,
13771392 quietsSearched, quietCount, capturesSearched, captureCount, depth);
@@ -1393,6 +1408,7 @@ namespace {
13931408 else if (depth > 3 )
13941409 ss->ttPv = ss->ttPv && (ss+1 )->ttPv ;
13951410
1411+ // Write gathered information in transposition table
13961412 if (!excludedMove && !(rootNode && thisThread->pvIdx ))
13971413 tte->save (posKey, value_to_tt (bestValue, ss->ply ), ss->ttPv ,
13981414 bestValue >= beta ? BOUND_LOWER :
@@ -1488,13 +1504,16 @@ namespace {
14881504 bestValue = ttValue;
14891505 }
14901506 else
1507+ // In case of null move search use previous static eval with a different sign
1508+ // and addition of two tempos
14911509 ss->staticEval = bestValue =
14921510 (ss-1 )->currentMove != MOVE_NULL ? evaluate (pos)
14931511 : -(ss-1 )->staticEval + 2 * Tempo;
14941512
14951513 // Stand pat. Return immediately if static value is at least beta
14961514 if (bestValue >= beta)
14971515 {
1516+ // Save gathered info in transposition table
14981517 if (!ss->ttHit )
14991518 tte->save (posKey, value_to_tt (bestValue, ss->ply ), false , BOUND_LOWER,
15001519 DEPTH_NONE, MOVE_NONE, ss->staticEval );
@@ -1623,6 +1642,7 @@ namespace {
16231642 return mated_in (ss->ply ); // Plies to mate from the root
16241643 }
16251644
1645+ // Save gathered info in transposition table
16261646 tte->save (posKey, value_to_tt (bestValue, ss->ply ), pvHit,
16271647 bestValue >= beta ? BOUND_LOWER :
16281648 PvNode && bestValue > oldAlpha ? BOUND_EXACT : BOUND_UPPER,
@@ -1706,24 +1726,27 @@ namespace {
17061726
17071727 if (!pos.capture_or_promotion (bestMove))
17081728 {
1729+ // Increase stats for the best move in case it was a quiet move
17091730 update_quiet_stats (pos, ss, bestMove, bonus2, depth);
17101731
1711- // Decrease all the non-best quiet moves
1732+ // Decrease stats for all non-best quiet moves
17121733 for (int i = 0 ; i < quietCount; ++i)
17131734 {
17141735 thisThread->mainHistory [us][from_to (quietsSearched[i])] << -bonus2;
17151736 update_continuation_histories (ss, pos.moved_piece (quietsSearched[i]), to_sq (quietsSearched[i]), -bonus2);
17161737 }
17171738 }
17181739 else
1740+ // Increase stats for the best move in case it was a capture move
17191741 captureHistory[moved_piece][to_sq (bestMove)][captured] << bonus1;
17201742
1721- // Extra penalty for a quiet early move that was not a TT move or main killer move in previous ply when it gets refuted
1743+ // Extra penalty for a quiet early move that was not a TT move or
1744+ // main killer move in previous ply when it gets refuted.
17221745 if ( ((ss-1 )->moveCount == 1 + (ss-1 )->ttHit || ((ss-1 )->currentMove == (ss-1 )->killers [0 ]))
17231746 && !pos.captured_piece ())
17241747 update_continuation_histories (ss-1 , pos.piece_on (prevSq), prevSq, -bonus1);
17251748
1726- // Decrease all the non-best capture moves
1749+ // Decrease stats for all non-best capture moves
17271750 for (int i = 0 ; i < captureCount; ++i)
17281751 {
17291752 moved_piece = pos.moved_piece (capturesSearched[i]);
@@ -1740,6 +1763,7 @@ namespace {
17401763
17411764 for (int i : {1 , 2 , 4 , 6 })
17421765 {
1766+ // Only update first 2 continuation histories if we are in check
17431767 if (ss->inCheck && i > 2 )
17441768 break ;
17451769 if (is_ok ((ss-i)->currentMove ))
@@ -1752,6 +1776,7 @@ namespace {
17521776
17531777 void update_quiet_stats (const Position& pos, Stack* ss, Move move, int bonus, int depth) {
17541778
1779+ // Update killers
17551780 if (ss->killers [0 ] != move)
17561781 {
17571782 ss->killers [1 ] = ss->killers [0 ];
@@ -1763,15 +1788,18 @@ namespace {
17631788 thisThread->mainHistory [us][from_to (move)] << bonus;
17641789 update_continuation_histories (ss, pos.moved_piece (move), to_sq (move), bonus);
17651790
1791+ // Penalty for reversed move in case of moved piece not being a pawn
17661792 if (type_of (pos.moved_piece (move)) != PAWN)
17671793 thisThread->mainHistory [us][from_to (reverse_move (move))] << -bonus;
17681794
1795+ // Update countermove history
17691796 if (is_ok ((ss-1 )->currentMove ))
17701797 {
17711798 Square prevSq = to_sq ((ss-1 )->currentMove );
17721799 thisThread->counterMoves [pos.piece_on (prevSq)][prevSq] = move;
17731800 }
17741801
1802+ // Update low ply history
17751803 if (depth > 11 && ss->ply < MAX_LPH)
17761804 thisThread->lowPlyHistory [ss->ply ][from_to (move)] << stat_bonus (depth - 7 );
17771805 }
0 commit comments