]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Add TT prefetching support
[stockfish] / src / search.cpp
index d922986b7fd7b3744441e92e352391925dd5d967..61475a2f3f3be641fc19b2222d5665cc85d1f214 100644 (file)
@@ -306,7 +306,6 @@ namespace {
   void update_history(const Position& pos, Move m, Depth depth, Move movesSearched[], int moveCount);
   void update_killers(Move m, SearchStack& ss);
   void slowdown(const Position& pos);
   void update_history(const Position& pos, Move m, Depth depth, Move movesSearched[], int moveCount);
   void update_killers(Move m, SearchStack& ss);
   void slowdown(const Position& pos);
-  void build_pv(const Position& pos, Move pv[]);
 
   bool fail_high_ply_1();
   int current_search_time();
 
   bool fail_high_ply_1();
   int current_search_time();
@@ -895,7 +894,7 @@ namespace {
         // Decide search depth for this move
         bool moveIsCapture = pos.move_is_capture(move);
         bool dangerous;
         // Decide search depth for this move
         bool moveIsCapture = pos.move_is_capture(move);
         bool dangerous;
-        ext = extension(pos, move, true, pos.move_is_capture(move), pos.move_is_check(move), false, false, &dangerous);
+        ext = extension(pos, move, true, moveIsCapture, pos.move_is_check(move), false, false, &dangerous);
         newDepth = (Iteration - 2) * OnePly + ext + InitialDepth;
 
         // Make the move, and search it
         newDepth = (Iteration - 2) * OnePly + ext + InitialDepth;
 
         // Make the move, and search it
@@ -919,8 +918,8 @@ namespace {
         }
         else
         {
         }
         else
         {
-            if (newDepth >= 3*OnePly
-                && i + MultiPV >= LMRPVMoves
+            if (   newDepth >= 3*OnePly
+                && i >= MultiPV + LMRPVMoves - 2 // Remove -2 and decrease LMRPVMoves instead ?
                 && !dangerous
                 && !moveIsCapture
                 && !move_is_promotion(move)
                 && !dangerous
                 && !moveIsCapture
                 && !move_is_promotion(move)
@@ -928,10 +927,10 @@ namespace {
             {
                 ss[0].reduction = OnePly;
                 value = -search(pos, ss, -alpha, newDepth-OnePly, 1, true, 0);
             {
                 ss[0].reduction = OnePly;
                 value = -search(pos, ss, -alpha, newDepth-OnePly, 1, true, 0);
-            }
-            else
+            } else
                 value = alpha + 1; // Just to trigger next condition
                 value = alpha + 1; // Just to trigger next condition
-            if(value > alpha)
+
+            if (value > alpha)
             {
                 value = -search(pos, ss, -alpha, newDepth, 1, true, 0);
                 if (value > alpha)
             {
                 value = -search(pos, ss, -alpha, newDepth, 1, true, 0);
                 if (value > alpha)
@@ -976,7 +975,7 @@ namespace {
             // Update PV
             rml.set_move_score(i, value);
             update_pv(ss, 0);
             // Update PV
             rml.set_move_score(i, value);
             update_pv(ss, 0);
-            build_pv(pos, ss[0].pv);
+            TT.extract_pv(pos, ss[0].pv);
             rml.set_move_pv(i, ss[0].pv);
 
             if (MultiPV == 1)
             rml.set_move_pv(i, ss[0].pv);
 
             if (MultiPV == 1)
@@ -990,6 +989,8 @@ namespace {
                 // Print search information to the standard output
                 std::cout << "info depth " << Iteration
                           << " score " << value_to_string(value)
                 // Print search information to the standard output
                 std::cout << "info depth " << Iteration
                           << " score " << value_to_string(value)
+                          << ((value >= beta)?
+                              " lowerbound" : ((value <= alpha)? " upperbound" : ""))
                           << " time " << current_search_time()
                           << " nodes " << nodes_searched()
                           << " nps " << nps()
                           << " time " << current_search_time()
                           << " nodes " << nodes_searched()
                           << " nps " << nps()
@@ -1125,6 +1126,7 @@ namespace {
       // Make and search the move
       StateInfo st;
       pos.do_move(move, st, dcCandidates);
       // Make and search the move
       StateInfo st;
       pos.do_move(move, st, dcCandidates);
+      TT.prefetch(pos.get_key());
 
       if (moveCount == 1) // The first move in list is the PV
           value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
 
       if (moveCount == 1) // The first move in list is the PV
           value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
@@ -1295,6 +1297,8 @@ namespace {
 
         StateInfo st;
         pos.do_null_move(st);
 
         StateInfo st;
         pos.do_null_move(st);
+        TT.prefetch(pos.get_key());
+
         int R = (depth >= 5 * OnePly ? 4 : 3); // Null move dynamic reduction
 
         Value nullValue = -search(pos, ss, -(beta-1), depth-R*OnePly, ply+1, false, threadID);
         int R = (depth >= 5 * OnePly ? 4 : 3); // Null move dynamic reduction
 
         Value nullValue = -search(pos, ss, -(beta-1), depth-R*OnePly, ply+1, false, threadID);
@@ -1409,6 +1413,7 @@ namespace {
       // Make and search the move
       StateInfo st;
       pos.do_move(move, st, dcCandidates);
       // Make and search the move
       StateInfo st;
       pos.do_move(move, st, dcCandidates);
+      TT.prefetch(pos.get_key());
 
       // Try to reduce non-pv search depth by one ply if move seems not problematic,
       // if the move fails high will be re-searched at full depth.
 
       // Try to reduce non-pv search depth by one ply if move seems not problematic,
       // if the move fails high will be re-searched at full depth.
@@ -1618,6 +1623,7 @@ namespace {
       // Make and search the move.
       StateInfo st;
       pos.do_move(move, st, dcCandidates);
       // Make and search the move.
       StateInfo st;
       pos.do_move(move, st, dcCandidates);
+      TT.prefetch(pos.get_key());
       Value value = -qsearch(pos, ss, -beta, -alpha, depth-OnePly, ply+1, threadID);
       pos.undo_move(move);
 
       Value value = -qsearch(pos, ss, -beta, -alpha, depth-OnePly, ply+1, threadID);
       pos.undo_move(move);
 
@@ -2446,37 +2452,6 @@ namespace {
   }
 
 
   }
 
 
-  // build_pv() extends a PV by adding moves from the transposition table at
-  // the end. This should ensure that the PV is almost always at least two
-  // plies long, which is important, because otherwise we will often get
-  // single-move PVs when the search stops while failing high, and a
-  // single-move PV means that we don't have a ponder move.
-
-  void build_pv(const Position& pos, Move pv[]) {
-    int ply;
-    Position p(pos);
-    StateInfo st[100];
-
-    for (ply = 0; pv[ply] != MOVE_NONE; ply++)
-        p.do_move(pv[ply], st[ply]);
-
-    bool stop;
-    const TTEntry* tte;
-    for (stop = false, tte = TT.retrieve(p.get_key());
-         tte && tte->move() != MOVE_NONE && !stop;
-         tte = TT.retrieve(p.get_key()), ply++)
-    {
-        if (!move_is_legal(p, tte->move(), p.pinned_pieces(p.side_to_move())))
-            break;
-        pv[ply] = tte->move();
-        p.do_move(pv[ply], st[ply]);
-        for (int j = 0; j < ply; j++)
-            if (st[j].key == p.get_key()) stop = true;
-    }
-    pv[ply] = MOVE_NONE;
-  }
-
-
   // fail_high_ply_1() checks if some thread is currently resolving a fail
   // high at ply 1 at the node below the first root node.  This information
   // is used for time managment.
   // fail_high_ply_1() checks if some thread is currently resolving a fail
   // high at ply 1 at the node below the first root node.  This information
   // is used for time managment.