- // Search starting from ss+1 to allow referencing (ss-1). This is
- // needed by update_gains() and ss copy when splitting at Root.
- value = search<Root>(pos, ss+1, alpha, beta, depth * ONE_PLY);
-
- // It is critical that sorting is done with a stable algorithm
- // because all the values but the first are usually set to
- // -VALUE_INFINITE and we want to keep the same order for all
- // the moves but the new PV that goes to head.
- sort<RootMove>(Rml.begin() + MultiPVIteration, Rml.end());
-
- // In case we have found an exact score reorder the PV moves
- // before leaving the fail high/low loop, otherwise leave the
- // last PV move in its position so to be searched again.
- if (value > alpha && value < beta)
- sort<RootMove>(Rml.begin(), Rml.begin() + MultiPVIteration);
-
- // Write PV back to transposition table in case the relevant entries
- // have been overwritten during the search.
- for (int i = 0; i <= MultiPVIteration; i++)
- Rml[i].insert_pv_in_tt(pos);
-
- // Value cannot be trusted. Break out immediately!
- if (StopRequest)
+ // Search starts from ss+1 to allow referencing (ss-1). This is
+ // needed by update gains and ss copy when splitting at Root.
+ bestValue = search<Root>(pos, ss+1, alpha, beta, depth * ONE_PLY);
+
+ // Bring to front the best move. 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 but the new
+ // PV that goes to the front. Note that in case of MultiPV search
+ // the already searched PV lines are preserved.
+ sort<RootMove>(RootMoves.begin() + PVIdx, RootMoves.end());
+
+ // In case we have found an exact score and we are going to leave
+ // the fail high/low loop then reorder the PV moves, otherwise
+ // leave the last PV move in its position so to be searched again.
+ // Of course this is needed only in MultiPV search.
+ if (PVIdx && bestValue > alpha && bestValue < beta)
+ sort<RootMove>(RootMoves.begin(), RootMoves.begin() + PVIdx);
+
+ // 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 exit the aspiration window loop.
+ // Sorting and writing PV back to TT is safe becuase RootMoves
+ // is still valid, although refers to previous iteration.
+ if (Signals.stop)