- // Start with a small aspiration window and, in the case of a fail
- // high/low, re-search with a bigger window until we're not failing
- // high/low anymore.
- while (true)
- {
- bestValue = search<Root>(pos, ss, alpha, beta, depth * ONE_PLY, false);
-
- DrawValue[ RootColor] = VALUE_DRAW - Contempt[bestValue > VALUE_DRAW];
- DrawValue[~RootColor] = VALUE_DRAW + Contempt[bestValue > VALUE_DRAW];
-
- // Bring the best move to the front. It is critical that sorting
- // is done with a stable algorithm because all the values but the
- // first and eventually the new best one are set to -VALUE_INFINITE
- // and we want to keep the same order for all the moves except the
- // new PV that goes to the front. Note that in case of MultiPV
- // search the already searched PV lines are preserved.
- std::stable_sort(RootMoves.begin() + PVIdx, RootMoves.end());
-
- // Write PV back to transposition table in case the relevant
- // entries have been overwritten during the search.
- for (size_t i = 0; i <= PVIdx; ++i)
- RootMoves[i].insert_pv_in_tt(pos);
-
- // If search has been stopped break immediately. Sorting and
- // writing PV back to TT is safe because RootMoves is still
- // valid, although it refers to previous iteration.
- if (Signals.stop)
- break;
-
- // When failing high/low give some update (without cluttering
- // the UI) before a re-search.
- if ( (bestValue <= alpha || bestValue >= beta)
- && Time::now() - SearchTime > 3000)
- sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
-
- // In case of failing low/high increase aspiration window and
- // re-search, otherwise exit the loop.
- if (bestValue <= alpha)
- {
- alpha = std::max(bestValue - delta, -VALUE_INFINITE);
-
- Signals.failedLowAtRoot = true;
- Signals.stopOnPonderhit = false;
- }
- else if (bestValue >= beta)
- beta = std::min(bestValue + delta, VALUE_INFINITE);
-
- else
- break;
-
- delta += delta / 2;
-
- assert(alpha >= -VALUE_INFINITE && beta <= VALUE_INFINITE);
- }