@@ -605,7 +605,7 @@ namespace {
605605 (ss+2 )->killers [0 ] = (ss+2 )->killers [1 ] = MOVE_NONE;
606606 (ss+2 )->cutoffCnt = 0 ;
607607 ss->doubleExtensions = (ss-1 )->doubleExtensions ;
608- Square prevSq = to_sq ((ss-1 )->currentMove );
608+ Square prevSq = is_ok ((ss- 1 )-> currentMove ) ? to_sq ((ss-1 )->currentMove ) : SQ_NONE ;
609609
610610 // Initialize statScore to zero for the grandchildren of the current position.
611611 // So statScore is shared between all grandchildren and only the first grandchild
@@ -647,7 +647,7 @@ namespace {
647647 update_quiet_stats (pos, ss, ttMove, stat_bonus (depth));
648648
649649 // Extra penalty for early quiet moves of the previous ply (~0 Elo on STC, ~2 Elo on LTC)
650- if ((ss-1 )->moveCount <= 2 && !priorCapture)
650+ if (prevSq != SQ_NONE && (ss-1 )->moveCount <= 2 && !priorCapture)
651651 update_continuation_histories (ss-1 , pos.piece_on (prevSq), prevSq, -stat_bonus (depth + 1 ));
652652 }
653653 // Penalty for a quiet ttMove that fails low (~1 Elo)
@@ -935,7 +935,7 @@ namespace {
935935 nullptr , (ss-4 )->continuationHistory ,
936936 nullptr , (ss-6 )->continuationHistory };
937937
938- Move countermove = thisThread->counterMoves [pos.piece_on (prevSq)][prevSq];
938+ Move countermove = prevSq != SQ_NONE ? thisThread->counterMoves [pos.piece_on (prevSq)][prevSq] : MOVE_NONE ;
939939
940940 MovePicker mp (pos, ttMove, depth, &thisThread->mainHistory ,
941941 &captureHistory,
@@ -1383,7 +1383,7 @@ namespace {
13831383 quietsSearched, quietCount, capturesSearched, captureCount, depth);
13841384
13851385 // Bonus for prior countermove that caused the fail low
1386- else if (!priorCapture)
1386+ else if (!priorCapture && prevSq != SQ_NONE )
13871387 {
13881388 int bonus = (depth > 5 ) + (PvNode || cutNode) + (bestValue < alpha - 97 * depth) + ((ss-1 )->moveCount > 10 );
13891389 update_continuation_histories (ss-1 , pos.piece_on (prevSq), prevSq, stat_bonus (depth) * bonus);
@@ -1525,7 +1525,7 @@ namespace {
15251525 // to search the moves. Because the depth is <= 0 here, only captures,
15261526 // queen promotions, and other checks (only if depth >= DEPTH_QS_CHECKS)
15271527 // will be generated.
1528- Square prevSq = to_sq ((ss-1 )->currentMove );
1528+ Square prevSq = (ss- 1 )-> currentMove != MOVE_NULL ? to_sq ((ss-1 )->currentMove ) : SQ_NONE ;
15291529 MovePicker mp (pos, ttMove, depth, &thisThread->mainHistory ,
15301530 &thisThread->captureHistory ,
15311531 contHist,
@@ -1714,7 +1714,8 @@ namespace {
17141714 Thread* thisThread = pos.this_thread ();
17151715 CapturePieceToHistory& captureHistory = thisThread->captureHistory ;
17161716 Piece moved_piece = pos.moved_piece (bestMove);
1717- PieceType captured = type_of (pos.piece_on (to_sq (bestMove)));
1717+ PieceType captured;
1718+
17181719 int bonus1 = stat_bonus (depth + 1 );
17191720
17201721 if (!pos.capture_stage (bestMove))
@@ -1733,12 +1734,16 @@ namespace {
17331734 }
17341735 }
17351736 else
1737+ {
17361738 // Increase stats for the best move in case it was a capture move
1739+ captured = type_of (pos.piece_on (to_sq (bestMove)));
17371740 captureHistory[moved_piece][to_sq (bestMove)][captured] << bonus1;
1741+ }
17381742
17391743 // Extra penalty for a quiet early move that was not a TT move or
17401744 // main killer move in previous ply when it gets refuted.
1741- if ( ((ss-1 )->moveCount == 1 + (ss-1 )->ttHit || ((ss-1 )->currentMove == (ss-1 )->killers [0 ]))
1745+ if ( prevSq != SQ_NONE
1746+ && ((ss-1 )->moveCount == 1 + (ss-1 )->ttHit || ((ss-1 )->currentMove == (ss-1 )->killers [0 ]))
17421747 && !pos.captured_piece ())
17431748 update_continuation_histories (ss-1 , pos.piece_on (prevSq), prevSq, -bonus1);
17441749
0 commit comments