From: bmc4 Date: Thu, 3 Jun 2021 16:52:39 +0000 (-0300) Subject: Introducing NodeType Root X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=0b7cc8bd2f5a51de9f911fcdfb253b1c9f513897 Introducing NodeType Root We transform rootNode into constexpr by adding a new NodeType `Root`, which causes a speed up. Local test: ``` Build Tester: 1.4.7.0 Windows 10 (Version 10.0, Build 0, 64-bit Edition) Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz SafeMode: No Running In VM: No HyperThreading Enabled: Yes CPU Warmup: Yes Command Line: bench Tests per Build: 25 ANOVA: n/a Engine# (NPS) Speedup Sp Conf. 95% S.S. patch (920.179,4) ---> master (906.329,2) ---> 1,528% 20.336,5 Yes No ``` --------- STC: LLR: 2.94 (-2.94,2.94) <-0.50,2.50> Total: 98216 W: 8348 L: 8102 D: 81766 Ptnml(0-2): 295, 6357, 35549, 6621, 286 https://tests.stockfishchess.org/tests/view/60b797e2457376eb8bcaa0ab Yellow LTC: LLR: -2.95 (-2.94,2.94) <0.50,3.50> Total: 76936 W: 2651 L: 2626 D: 71659 Ptnml(0-2): 29, 2233, 33916, 2264, 26 https://tests.stockfishchess.org/tests/view/60b80d6d457376eb8bcaa145 closes https://github.com/official-stockfish/Stockfish/pull/3522 No functional change --- diff --git a/src/search.cpp b/src/search.cpp index 100ddc71..f634b2d2 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -59,7 +59,7 @@ using namespace Search; namespace { // Different node types, used as a template parameter - enum NodeType { NonPV, PV }; + enum NodeType { NonPV, PV, Root }; constexpr uint64_t TtHitAverageWindow = 4096; constexpr uint64_t TtHitAverageResolution = 1024; @@ -102,10 +102,10 @@ namespace { Move best = MOVE_NONE; }; - template + template Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode); - template + template Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth = 0); Value value_to_tt(Value v, int ply); @@ -384,7 +384,7 @@ void Thread::search() { while (true) { Depth adjustedDepth = std::max(1, rootDepth - failedHighCnt - searchAgainCounter); - bestValue = Stockfish::search(rootPos, ss, alpha, beta, adjustedDepth, false); + bestValue = Stockfish::search(rootPos, ss, alpha, beta, adjustedDepth, false); // Bring the best move to the front. It is critical that sorting // is done with a stable algorithm because all the values but the @@ -527,18 +527,18 @@ namespace { // search<>() is the main search function for both PV and non-PV nodes - template + template Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode) { - constexpr bool PvNode = NT == PV; - const bool rootNode = PvNode && ss->ply == 0; + constexpr bool PvNode = nodeType != NonPV; + constexpr bool rootNode = nodeType == Root; const Depth maxNextDepth = rootNode ? depth : depth + 1; // Check if we have an upcoming move which draws by repetition, or // if the opponent had an alternative move earlier to this position. - if ( pos.rule50_count() >= 3 + if ( !rootNode + && pos.rule50_count() >= 3 && alpha < VALUE_DRAW - && !rootNode && pos.has_game_cycle(ss->ply)) { alpha = value_draw(pos.this_thread()); @@ -548,7 +548,7 @@ namespace { // Dive into quiescence search when the depth reaches zero if (depth <= 0) - return qsearch(pos, ss, alpha, beta); + return qsearch(pos, ss, alpha, beta); assert(-VALUE_INFINITE <= alpha && alpha < beta && beta <= VALUE_INFINITE); assert(PvNode || (alpha == beta - 1)); @@ -1054,9 +1054,9 @@ moves_loop: // When in check, search starts from here // then that move is singular and should be extended. To verify this we do // a reduced search on all the other moves but the ttMove and if the // result is lower than ttValue minus a margin, then we will extend the ttMove. - if ( depth >= 7 + if ( !rootNode + && depth >= 7 && move == ttMove - && !rootNode && !excludedMove // Avoid recursive singular search /* && ttValue != VALUE_NONE Already implicit in the next condition */ && abs(ttValue) < VALUE_KNOWN_WIN @@ -1351,10 +1351,11 @@ moves_loop: // When in check, search starts from here // qsearch() is the quiescence search function, which is called by the main search // function with zero depth, or recursively with further decreasing depth per call. - template + template Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) { - constexpr bool PvNode = NT == PV; + static_assert(nodeType != Root); + constexpr bool PvNode = nodeType == PV; assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE); assert(PvNode || (alpha == beta - 1)); @@ -1532,7 +1533,7 @@ moves_loop: // When in check, search starts from here // Make and search the move pos.do_move(move, st, givesCheck); - value = -qsearch(pos, ss+1, -beta, -alpha, depth - 1); + value = -qsearch(pos, ss+1, -beta, -alpha, depth - 1); pos.undo_move(move); assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);