- tte = TT.retrieve(pos.get_key());
- ttMove = (tte ? tte->move() : MOVE_NONE);
-
- // Step 5. Evaluate the position statically
- // At PV nodes we do this only to update gain statistics
- isCheck = pos.is_check();
- if (!isCheck)
- {
- ss[ply].eval = evaluate(pos, ei, threadID);
- update_gains(pos, ss[ply - 1].currentMove, ss[ply - 1].eval, ss[ply].eval);
- }
-
- // Step 6. Razoring (is omitted in PV nodes)
- // Step 7. Static null move pruning (is omitted in PV nodes)
- // Step 8. Null move search with verification search (is omitted in PV nodes)
-
- // Step 9. Internal iterative deepening
- if ( depth >= IIDDepthAtPVNodes
- && ttMove == MOVE_NONE)
- {
- search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID);
- ttMove = ss[ply].pv[ply];
- tte = TT.retrieve(pos.get_key());
- }
-
- // Step 10. Loop through moves
- // Loop through all legal moves until no moves remain or a beta cutoff occurs
-
- // Initialize a MovePicker object for the current position
- mateThreat = pos.has_mate_threat(opposite_color(pos.side_to_move()));
- MovePicker mp = MovePicker(pos, ttMove, depth, H, &ss[ply]);
- CheckInfo ci(pos);
-
- while ( alpha < beta
- && (move = mp.get_next_move()) != MOVE_NONE
- && !TM.thread_should_stop(threadID))
- {
- assert(move_is_ok(move));
-
- singleEvasion = (isCheck && mp.number_of_evasions() == 1);
- moveIsCheck = pos.move_is_check(move, ci);
- captureOrPromotion = pos.move_is_capture_or_promotion(move);
-
- // Step 11. Decide the new search depth
- ext = extension(pos, move, true, captureOrPromotion, moveIsCheck, singleEvasion, mateThreat, &dangerous);
-
- // Singular extension search. We extend the TT move if its value is much better than
- // its siblings. To verify this we do a reduced search on all the other moves but the
- // ttMove, if result is lower then ttValue minus a margin then we extend ttMove.
- if ( depth >= SingularExtensionDepthAtPVNodes
- && tte
- && move == tte->move()
- && ext < OnePly
- && is_lower_bound(tte->type())
- && tte->depth() >= depth - 3 * OnePly)
- {
- Value ttValue = value_from_tt(tte->value(), ply);
-
- if (abs(ttValue) < VALUE_KNOWN_WIN)
- {
- Value excValue = search(pos, ss, ttValue - SingularExtensionMargin, depth / 2, ply, false, threadID, move);
-
- if (excValue < ttValue - SingularExtensionMargin)
- ext = OnePly;
- }
- }
-
- newDepth = depth - OnePly + ext;
-
- // Update current move (this must be done after singular extension search)
- movesSearched[moveCount++] = ss[ply].currentMove = move;
-
- // Step 12. Futility pruning (is omitted in PV nodes)
-
- // Step 13. Make the move
- pos.do_move(move, st, ci, moveIsCheck);
-
- // Step extra. pv search (only in PV nodes)
- // The first move in list is the expected PV
- if (moveCount == 1)
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
- else
- {
- // Step 14. Reduced search
- // if the move fails high will be re-searched at full depth.
- bool doFullDepthSearch = true;
-
- if ( depth >= 3*OnePly
- && !dangerous
- && !captureOrPromotion
- && !move_is_castle(move)
- && !move_is_killer(move, ss[ply]))
- {
- ss[ply].reduction = pv_reduction(depth, moveCount);
- if (ss[ply].reduction)
- {
- value = -search(pos, ss, -alpha, newDepth-ss[ply].reduction, ply+1, true, threadID);
- doFullDepthSearch = (value > alpha);
- }
- }
-
- // Step 15. Full depth search
- if (doFullDepthSearch)
- {
- ss[ply].reduction = Depth(0);
- value = -search(pos, ss, -alpha, newDepth, ply+1, true, threadID);
-
- // Step extra. pv search (only in PV nodes)
- if (value > alpha && value < beta)
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
- }
- }
-
- // Step 16. Undo move
- pos.undo_move(move);