]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Avoid double function dispatch
[stockfish] / src / search.cpp
index 02e171b76bd49a3bb87ddaa34a524e00143c6fbd..cd4bf3e4d26faad6a5f3a3c7f5e535d4bab13e14 100644 (file)
@@ -296,7 +296,6 @@ namespace {
   template <NodeType PvNode>
   Depth extension(const Position& pos, Move m, bool captureOrPromotion, bool moveIsCheck, bool singleEvasion, bool mateThreat, bool* dangerous);
 
-  void init_node(SearchStack* ss, int ply, int threadID);
   void update_pv(SearchStack* ss, int ply);
   void sp_update_pv(SearchStack* pss, SearchStack* ss, int ply);
   bool connected_moves(const Position& pos, Move m1, Move m2);
@@ -1057,12 +1056,16 @@ namespace {
     refinedValue = bestValue = value = -VALUE_INFINITE;
     oldAlpha = alpha;
 
-    if (depth < OnePly)
-        return qsearch<PvNode>(pos, ss, alpha, beta, Depth(0), threadID);
+    // Step 1. Initialize node and poll. Polling can abort search
+    TM.incrementNodeCounter(threadID);
+    ss->init(ply);
+    (ss + 2)->initKillers();
 
-    // Step 1. Initialize node and poll
-    // Polling can abort search.
-    init_node(ss, ply, threadID);
+    if (threadID == 0 && ++NodesSincePoll > NodesBetweenPolls)
+    {
+        NodesSincePoll = 0;
+        poll();
+    }
 
     // Step 2. Check for aborted search and immediate draw
     if (AbortSearch || TM.thread_should_stop(threadID))
@@ -1173,8 +1176,8 @@ namespace {
 
         pos.do_null_move(st);
 
-        nullValue = -search<NonPV>(pos, ss+1, -beta, -alpha, depth-R*OnePly, false, threadID);
-
+        nullValue = depth-R*OnePly < OnePly ? -qsearch<NonPV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
+                                            : - search<NonPV>(pos, ss+1, -beta, -alpha, depth-R*OnePly, false, threadID);
         pos.undo_null_move();
 
         if (nullValue >= beta)
@@ -1308,7 +1311,8 @@ namespace {
       // Step extra. pv search (only in PV nodes)
       // The first move in list is the expected PV
       if (PvNode && moveCount == 1)
-          value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
+          value = newDepth < OnePly ? -qsearch<PV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
+                                    : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
       else
       {
           // Step 14. Reduced depth search
@@ -1324,7 +1328,10 @@ namespace {
               ss->reduction = reduction<PvNode>(depth, moveCount);
               if (ss->reduction)
               {
-                  value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, threadID);
+                  Depth r = newDepth - ss->reduction;
+                  value = r < OnePly ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, Depth(0), threadID)
+                                     : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, r, true, threadID);
+
                   doFullDepthSearch = (value > alpha);
               }
 
@@ -1333,6 +1340,8 @@ namespace {
               // if the move fails high again then go with full depth search.
               if (doFullDepthSearch && ss->reduction > 2 * OnePly)
               {
+                  assert(newDepth - OnePly >= OnePly);
+
                   ss->reduction = OnePly;
                   value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, threadID);
                   doFullDepthSearch = (value > alpha);
@@ -1343,13 +1352,15 @@ namespace {
           if (doFullDepthSearch)
           {
               ss->reduction = Depth(0);
-              value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, true, threadID);
+              value = newDepth < OnePly ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, Depth(0), threadID)
+                                        : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, true, threadID);
 
               // Step extra. pv search (only in PV nodes)
               // Search only for possible new PV nodes, if instead value >= beta then
               // parent node fails low with value <= alpha and tries another move.
               if (PvNode && value > alpha && value < beta)
-                  value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
+                  value = newDepth < OnePly ? -qsearch<PV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
+                                            : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
           }
       }
 
@@ -1446,14 +1457,10 @@ namespace {
     int ply = pos.ply();
     Value oldAlpha = alpha;
 
-    // Initialize, and make an early exit in case of an aborted search,
-    // an instant draw, maximum ply reached, etc.
-    init_node(ss, ply, threadID);
-
-    // After init_node() that calls poll()
-    if (AbortSearch || TM.thread_should_stop(threadID))
-        return Value(0);
+    TM.incrementNodeCounter(threadID);
+    ss->init(ply);
 
+    // Check for an instant draw or maximum ply reached
     if (pos.is_draw() || ply >= PLY_MAX - 1)
         return VALUE_DRAW;
 
@@ -1771,32 +1778,6 @@ namespace {
     lock_release(&(sp->lock));
   }
 
-  // init_node() is called at the beginning of all the search functions
-  // (search() qsearch(), and so on) and initializes the
-  // search stack object corresponding to the current node. Once every
-  // NodesBetweenPolls nodes, init_node() also calls poll(), which polls
-  // for user input and checks whether it is time to stop the search.
-
-  void init_node(SearchStack* ss, int ply, int threadID) {
-
-    assert(ply >= 0 && ply < PLY_MAX);
-    assert(threadID >= 0 && threadID < TM.active_threads());
-
-    TM.incrementNodeCounter(threadID);
-
-    if (threadID == 0)
-    {
-        NodesSincePoll++;
-        if (NodesSincePoll >= NodesBetweenPolls)
-        {
-            poll();
-            NodesSincePoll = 0;
-        }
-    }
-    ss->init(ply);
-    (ss + 2)->initKillers();
-  }
-
   // update_pv() is called whenever a search returns a value > alpha.
   // It updates the PV in the SearchStack object corresponding to the
   // current node.
@@ -2480,10 +2461,7 @@ namespace {
     // Initialize SplitPointStack locks
     for (i = 0; i < MAX_THREADS; i++)
         for (int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++)
-        {
-            SplitPointStack[i][j].parent = NULL;
             lock_init(&(SplitPointStack[i][j].lock), NULL);
-        }
 
     // Will be set just before program exits to properly end the threads
     AllThreadsShouldExit = false;
@@ -2533,7 +2511,7 @@ namespace {
 
     // Wait for thread termination
     for (int i = 1; i < MAX_THREADS; i++)
-        while (threads[i].state != THREAD_TERMINATED);
+        while (threads[i].state != THREAD_TERMINATED) {}
 
     // Now we can safely destroy the locks
     for (int i = 0; i < MAX_THREADS; i++)