From a67c22611aea4b643e8278fa6007da316bd06b13 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Fri, 8 Aug 2014 10:59:28 +0200 Subject: [PATCH] Rework perft implementation Unify various perft functions and move all the code to search.cpp. Avoid perft implementation to be splitted between benchmark.cpp (where it has no reason to be) and search.cpp No functional and no speed change (tested). --- src/benchmark.cpp | 11 ++--------- src/search.cpp | 27 +++++++++++++++++---------- src/search.h | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index a92a634c..3161c4ca 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -139,15 +139,8 @@ void benchmark(const Position& current, istream& is) { cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl; if (limitType == "perft") - for (MoveList it(pos); *it; ++it) - { - StateInfo si; - pos.do_move(*it, si); - uint64_t cnt = limits.depth > 1 ? Search::perft(pos, (limits.depth - 1) * ONE_PLY) : 1; - pos.undo_move(*it); - cout << move_to_uci(*it, pos.is_chess960()) << ": " << cnt << endl; - nodes += cnt; - } + nodes += Search::perft(pos, limits.depth * ONE_PLY); + else { Threads.start_thinking(pos, limits, st); diff --git a/src/search.cpp b/src/search.cpp index 3fb1345a..8410f73b 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -152,26 +152,33 @@ void Search::init() { /// Search::perft() is our utility to verify move generation. All the leaf nodes /// up to the given depth are generated and counted and the sum returned. - -static uint64_t perft(Position& pos, Depth depth) { +template +uint64_t Search::perft(Position& pos, Depth depth) { StateInfo st; - uint64_t cnt = 0; + uint64_t cnt, nodes = 0; CheckInfo ci(pos); const bool leaf = depth == 2 * ONE_PLY; for (MoveList it(pos); *it; ++it) { - pos.do_move(*it, st, ci, pos.gives_check(*it, ci)); - cnt += leaf ? MoveList(pos).size() : ::perft(pos, depth - ONE_PLY); - pos.undo_move(*it); + if (Root && depth <= ONE_PLY) + cnt = 1; + else + { + pos.do_move(*it, st, ci, pos.gives_check(*it, ci)); + cnt = leaf ? MoveList(pos).size() : perft(pos, depth - ONE_PLY); + nodes += cnt; + pos.undo_move(*it); + } + if (Root) + sync_cout << move_to_uci(*it, pos.is_chess960()) << ": " << cnt << sync_endl; } - return cnt; + return nodes; } -uint64_t Search::perft(Position& pos, Depth depth) { - return depth > ONE_PLY ? ::perft(pos, depth) : MoveList(pos).size(); -} +template uint64_t Search::perft(Position& pos, Depth depth); + /// Search::think() is the external interface to Stockfish's search, and is /// called by the main thread when the program receives the UCI 'go' command. It diff --git a/src/search.h b/src/search.h index 5c2dfc8a..4fe5a5b4 100644 --- a/src/search.h +++ b/src/search.h @@ -105,8 +105,8 @@ extern Time::point SearchTime; extern StateStackPtr SetupStates; extern void init(); -extern uint64_t perft(Position& pos, Depth depth); extern void think(); +template uint64_t perft(Position& pos, Depth depth); } // namespace Search -- 2.39.2