diff --git a/src/search.c b/src/search.c index 307064dc..251cc4d2 100644 --- a/src/search.c +++ b/src/search.c @@ -682,6 +682,8 @@ static void AspirationWindow(Thread *thread, Stack *ss) { int score = AlphaBeta(thread, ss, alpha, beta, depth, false); + SortRootMoves(thread, multiPV); + // Give an update when failing high/low in longer searches if ( mainThread && Limits.multiPV == 1 @@ -732,7 +734,7 @@ static void *IterativeDeepening(void *voidThread) { AspirationWindow(thread, ss); // Sort root moves so they are printed in the right order in multi-pv mode - SortRootMoves(thread, multiPV); + SortRootMoves(thread, 0); // Only the main thread concerns itself with the rest if (!mainThread) continue; diff --git a/src/threads.c b/src/threads.c index ef8f2d18..d379d377 100644 --- a/src/threads.c +++ b/src/threads.c @@ -48,20 +48,14 @@ void InitThreads(int count) { Threads[i].count = count; } -// Sorts all rootmoves searched by multiPV -void SortRootMoves(Thread *thread, int multiPV) { - for (int i = 0; i < multiPV; ++i) { - - int bestIdx = i; - int bestScore = thread->rootMoves[i].score; - - for (int k = i + 1; k < thread->rootMoveCount; ++k) - if (thread->rootMoves[k].score > bestScore) - bestScore = thread->rootMoves[bestIdx = k].score; - - RootMove best = thread->rootMoves[bestIdx]; - thread->rootMoves[bestIdx] = thread->rootMoves[i]; - thread->rootMoves[i] = best; +// Sorts all rootmoves beginning from the given index +void SortRootMoves(Thread *thread, int begin) { + for (int i = begin + 1; i < thread->rootMoveCount; ++i) { + RootMove temp = thread->rootMoves[i]; + int j = i - 1; + while (j >= 0 && thread->rootMoves[j].score < temp.score) + thread->rootMoves[j+1] = thread->rootMoves[j], --j; + thread->rootMoves[j+1] = temp; } } diff --git a/src/threads.h b/src/threads.h index b546487f..cf1bf13a 100644 --- a/src/threads.h +++ b/src/threads.h @@ -92,7 +92,7 @@ extern Thread *Threads; void InitThreads(int threadCount); -void SortRootMoves(Thread *thread, int multiPV); +void SortRootMoves(Thread *thread, int begin); uint64_t TotalNodes(); uint64_t TotalTBHits(); void PrepareSearch(Position *pos, Move searchmoves[]);