From 1f9404434dcfd1013e20266a79dfed5d0271294a Mon Sep 17 00:00:00 2001 From: Carlos Esparza Date: Sun, 24 Nov 2024 16:17:42 -0800 Subject: [PATCH] Simplify picking of evasion moves Sort evasions before we start returning them in next_move() (just like every other kind of move) instead of looking for the biggest element on every call to next_move(). The bench number changes because the old method is not equivalent to a stable sort. Passed STC: LLR: 2.93 (-2.94,2.94) <-1.75,0.25> Total: 132064 W: 34318 L: 34204 D: 63542 Ptnml(0-2): 392, 15522, 34106, 15604, 408 https://tests.stockfishchess.org/tests/view/6743fee086d5ee47d953f9ca Passed LTC: LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 97542 W: 24899 L: 24757 D: 47886 Ptnml(0-2): 63, 10646, 27193, 10824, 45 https://tests.stockfishchess.org/tests/view/674509cd86d5ee47d953fb96 closes https://github.com/official-stockfish/Stockfish/pull/5700 Bench: 1094825 --- AUTHORS | 1 + src/movepick.cpp | 29 ++++++++++------------------- src/movepick.h | 7 +------ 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/AUTHORS b/AUTHORS index 31a64c17..ddc53ec0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -45,6 +45,7 @@ Bruno de Melo Costa (BM123499) Bruno Pellanda (pellanda) Bryan Cross (crossbr) candirufish +Carlos Esparza Sánchez (ces42) Chess13234 Chris Cain (ceebo) Ciekce diff --git a/src/movepick.cpp b/src/movepick.cpp index df722ece..96f03171 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -18,11 +18,9 @@ #include "movepick.h" -#include #include #include #include -#include #include "bitboard.h" #include "position.h" @@ -199,19 +197,13 @@ void MovePicker::score() { // Returns the next move satisfying a predicate function. // This never returns the TT move, as it was emitted before. -template +template Move MovePicker::select(Pred filter) { - while (cur < endMoves) - { - if constexpr (T == Best) - std::swap(*cur, *std::max_element(cur, endMoves)); - + for (; cur < endMoves; ++cur) if (*cur != ttMove && filter()) return *cur++; - cur++; - } return Move::none(); } @@ -245,7 +237,7 @@ top: goto top; case GOOD_CAPTURE : - if (select([&]() { + if (select([&]() { // Move losing capture to endBadCaptures to be tried later return pos.see_ge(*cur, -cur->value / 18) ? true : (*endBadCaptures++ = *cur, false); @@ -269,7 +261,7 @@ top: [[fallthrough]]; case GOOD_QUIET : - if (!skipQuiets && select([]() { return true; })) + if (!skipQuiets && select([]() { return true; })) { if ((cur - 1)->value > -7998 || (cur - 1)->value <= quiet_threshold(depth)) return *(cur - 1); @@ -286,7 +278,7 @@ top: [[fallthrough]]; case BAD_CAPTURE : - if (select([]() { return true; })) + if (select([]() { return true; })) return *(cur - 1); // Prepare the pointers to loop over the bad quiets @@ -298,7 +290,7 @@ top: case BAD_QUIET : if (!skipQuiets) - return select([]() { return true; }); + return select([]() { return true; }); return Move::none(); @@ -307,17 +299,16 @@ top: endMoves = generate(pos, cur); score(); + partial_insertion_sort(cur, endMoves, std::numeric_limits::min()); ++stage; [[fallthrough]]; case EVASION : - return select([]() { return true; }); + case QCAPTURE : + return select([]() { return true; }); case PROBCUT : - return select([&]() { return pos.see_ge(*cur, threshold); }); - - case QCAPTURE : - return select([]() { return true; }); + return select([&]() { return pos.see_ge(*cur, threshold); }); } assert(false); diff --git a/src/movepick.h b/src/movepick.h index 0278b70e..ab4e832f 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -35,11 +35,6 @@ class Position; // a cut-off first. class MovePicker { - enum PickType { - Next, - Best - }; - public: MovePicker(const MovePicker&) = delete; MovePicker& operator=(const MovePicker&) = delete; @@ -57,7 +52,7 @@ class MovePicker { void skip_quiet_moves(); private: - template + template Move select(Pred); template void score();