Skip to content

Commit 289fbcc

Browse files
mstemberaFanael
authored andcommitted
Use stable sort to make sure bench with TB yields same results everywhere.
std::sort() is not stable so different implementations can produce different results: use the stable version instead. Observed for '8/6k1/5r2/8/8/8/1K6/Q7 w - - 0 1' yielding different bench results for gcc and MSVC and 3-4-5 syzygy TB prior to this patch. closes official-stockfish#3083 No functional change.
1 parent 5a344f1 commit 289fbcc

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/search.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ void Tablebases::rank_root_moves(Position& pos, Search::RootMoves& rootMoves) {
19391939
if (RootInTB)
19401940
{
19411941
// Sort moves according to TB rank
1942-
std::sort(rootMoves.begin(), rootMoves.end(),
1942+
std::stable_sort(rootMoves.begin(), rootMoves.end(),
19431943
[](const RootMove &a, const RootMove &b) { return a.tbRank > b.tbRank; } );
19441944

19451945
// Probe during search only if DTZ is not available and we are winning

src/syzygy/tbprobe.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
759759
if (entry->hasPawns) {
760760
idx = LeadPawnIdx[leadPawnsCnt][squares[0]];
761761

762-
std::sort(squares + 1, squares + leadPawnsCnt, pawns_comp);
762+
std::stable_sort(squares + 1, squares + leadPawnsCnt, pawns_comp);
763763

764764
for (int i = 1; i < leadPawnsCnt; ++i)
765765
idx += Binomial[i][MapPawns[squares[i]]];
@@ -860,7 +860,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
860860

861861
while (d->groupLen[++next])
862862
{
863-
std::sort(groupSq, groupSq + d->groupLen[next]);
863+
std::stable_sort(groupSq, groupSq + d->groupLen[next]);
864864
uint64_t n = 0;
865865

866866
// Map down a square if "comes later" than a square in the previous

0 commit comments

Comments
 (0)