]> git.sesse.net Git - stockfish/commitdiff
Rework perft implementation
authorMarco Costalba <mcostalba@gmail.com>
Fri, 8 Aug 2014 08:59:28 +0000 (10:59 +0200)
committerlucasart <lucas.braesch@gmail.com>
Sat, 9 Aug 2014 05:00:59 +0000 (13:00 +0800)
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
src/search.cpp
src/search.h

index a92a634c615a18c707c4dd6ba53be097a7448d0f..3161c4cabd123bb60727c17004d4e03f31f83994 100644 (file)
@@ -139,15 +139,8 @@ void benchmark(const Position& current, istream& is) {
       cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
 
       if (limitType == "perft")
       cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
 
       if (limitType == "perft")
-          for (MoveList<LEGAL> 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<true>(pos, limits.depth * ONE_PLY);
+
       else
       {
           Threads.start_thinking(pos, limits, st);
       else
       {
           Threads.start_thinking(pos, limits, st);
index 3fb1345a892a9f7937135290fc60146a86ec8e33..8410f73bbf8ee8ad49f45f1e1777cb200e745759 100644 (file)
@@ -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.
 
 /// 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<bool Root>
+uint64_t Search::perft(Position& pos, Depth depth) {
 
   StateInfo st;
 
   StateInfo st;
-  uint64_t cnt = 0;
+  uint64_t cnt, nodes = 0;
   CheckInfo ci(pos);
   const bool leaf = depth == 2 * ONE_PLY;
 
   for (MoveList<LEGAL> it(pos); *it; ++it)
   {
   CheckInfo ci(pos);
   const bool leaf = depth == 2 * ONE_PLY;
 
   for (MoveList<LEGAL> it(pos); *it; ++it)
   {
-      pos.do_move(*it, st, ci, pos.gives_check(*it, ci));
-      cnt += leaf ? MoveList<LEGAL>(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<LEGAL>(pos).size() : perft<false>(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<LEGAL>(pos).size();
-}
+template uint64_t Search::perft<true>(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
 
 /// 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
index 5c2dfc8ac422b47777dca33164ea6ad6e7887d41..4fe5a5b4f60bfd41a528781f5d0dff74137e607b 100644 (file)
@@ -105,8 +105,8 @@ extern Time::point SearchTime;
 extern StateStackPtr SetupStates;
 
 extern void init();
 extern StateStackPtr SetupStates;
 
 extern void init();
-extern uint64_t perft(Position& pos, Depth depth);
 extern void think();
 extern void think();
+template<bool Root> uint64_t perft(Position& pos, Depth depth);
 
 } // namespace Search
 
 
 } // namespace Search