+ // Search to the current depth
+ ValueByIteration[Iteration] = root_search(p, ss, rml);
+
+ // Erase the easy move if it differs from the new best move
+ if (ss[0].pv[0] != EasyMove)
+ EasyMove = MOVE_NONE;
+
+ Problem = false;
+
+ if (!InfiniteSearch)
+ {
+ // Time to stop?
+ bool stopSearch = false;
+
+ // Stop search early if there is only a single legal move:
+ if (Iteration >= 6 && rml.move_count() == 1)
+ stopSearch = true;
+
+ // Stop search early when the last two iterations returned a mate score
+ if ( Iteration >= 6
+ && abs(ValueByIteration[Iteration]) >= abs(VALUE_MATE) - 100
+ && abs(ValueByIteration[Iteration-1]) >= abs(VALUE_MATE) - 100)
+ stopSearch = true;
+
+ // Stop search early if one move seems to be much better than the rest
+ int64_t nodes = nodes_searched();
+ if ( Iteration >= 8
+ && EasyMove == ss[0].pv[0]
+ && ( ( rml.get_move_cumulative_nodes(0) > (nodes * 85) / 100
+ && current_search_time() > MaxSearchTime / 16)
+ ||( rml.get_move_cumulative_nodes(0) > (nodes * 98) / 100
+ && current_search_time() > MaxSearchTime / 32)))
+ stopSearch = true;
+
+ // Add some extra time if the best move has changed during the last two iterations
+ if (Iteration > 5 && Iteration <= 50)
+ ExtraSearchTime = BestMoveChangesByIteration[Iteration] * (MaxSearchTime / 2)
+ + BestMoveChangesByIteration[Iteration-1] * (MaxSearchTime / 3);
+
+ // If we need some more and we are in time advantage take it
+ if (ExtraSearchTime > 0 && TimeAdvantage > 2 * MaxSearchTime)
+ ExtraSearchTime += MaxSearchTime / 2;
+
+ // Try to guess if the current iteration is the last one or the last two
+ LastIterations = (current_search_time() > ((MaxSearchTime + ExtraSearchTime)*58) / 128);
+
+ // Stop search if most of MaxSearchTime is consumed at the end of the
+ // iteration. We probably don't have enough time to search the first
+ // move at the next iteration anyway.
+ if (current_search_time() > ((MaxSearchTime + ExtraSearchTime)*80) / 128)
+ stopSearch = true;
+
+ if (stopSearch)
+ {
+ if (!PonderSearch)
+ break;
+ else
+ StopOnPonderhit = true;
+ }
+ }
+ // Write PV to transposition table, in case the relevant entries have
+ // been overwritten during the search:
+ TT.insert_pv(p, ss[0].pv);
+
+ if (MaxDepth && Iteration >= MaxDepth)
+ break;