]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Store position static score in TT as soon as possible
[stockfish] / src / search.cpp
index 0f5e6907b5f173de954483812068d4d6b70f08ca..83ce84d87c64cfd16e0e7f646a6893b7f7bcc5c5 100644 (file)
@@ -368,9 +368,7 @@ void SearchStack::init() {
 // SearchStack::initKillers() initializes killers for a search stack entry
 void SearchStack::initKillers() {
 
-  mateKiller = MOVE_NONE;
-  for (int i = 0; i < KILLER_MAX; i++)
-      killers[i] = MOVE_NONE;
+  killers[0] = killers[1] = mateKiller = MOVE_NONE;
 }
 
 
@@ -762,7 +760,9 @@ namespace {
     beta = *betaPtr;
     isCheck = pos.is_check();
 
-    // Step 1. Initialize node and poll (omitted at root, init_ss_array() has already initialized root node)
+    // Step 1. Initialize node (polling is omitted at root)
+    ss->init();
+
     // Step 2. Check for aborted search (omitted at root)
     // Step 3. Mate distance pruning (omitted at root)
     // Step 4. Transposition table lookup (omitted at root)
@@ -1083,13 +1083,17 @@ namespace {
     isCheck = pos.is_check();
     if (!isCheck)
     {
-        if (tte && tte->static_value() != VALUE_NONE)
+        if (tte)
         {
+            assert(tte->static_value() != VALUE_NONE);
             ss->eval = tte->static_value();
             ei.kingDanger[pos.side_to_move()] = tte->king_danger();
         }
         else
+        {
             ss->eval = evaluate(pos, ei);
+            TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
+        }
 
         refinedValue = refine_eval(tte, ss->eval, ply); // Enhance accuracy with TT value if possible
         update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
@@ -1107,10 +1111,6 @@ namespace {
         && !value_is_mate(beta)
         && !pos.has_pawn_on_7th(pos.side_to_move()))
     {
-        // Pass ss->eval to qsearch() and avoid an evaluate call
-        if (!tte || tte->static_value() == VALUE_NONE)
-            TT.store(posKey, ss->eval, VALUE_TYPE_EXACT, Depth(-127*OnePly), MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
-
         Value rbeta = beta - razor_margin(depth);
         Value v = qsearch<NonPV>(pos, ss, rbeta-1, rbeta, Depth(0), ply);
         if (v < rbeta)
@@ -1476,8 +1476,9 @@ namespace {
     }
     else
     {
-        if (tte && tte->static_value() != VALUE_NONE)
+        if (tte)
         {
+            assert(tte->static_value() != VALUE_NONE);
             ei.kingDanger[pos.side_to_move()] = tte->king_danger();
             bestValue = tte->static_value();
         }
@@ -1491,7 +1492,7 @@ namespace {
         if (bestValue >= beta)
         {
             if (!tte)
-                TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, Depth(-127*OnePly), MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
+                TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, DEPTH_NONE, MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
 
             return bestValue;
         }
@@ -1872,10 +1873,8 @@ namespace {
 
   bool move_is_killer(Move m, SearchStack* ss) {
 
-      const Move* k = ss->killers;
-      for (int i = 0; i < KILLER_MAX; i++, k++)
-          if (*k == m)
-              return true;
+      if (ss->killers[0] == m || ss->killers[1] == m)
+          return true;
 
       return false;
   }
@@ -2052,9 +2051,7 @@ namespace {
     if (m == ss->killers[0])
         return;
 
-    for (int i = KILLER_MAX - 1; i > 0; i--)
-        ss->killers[i] = ss->killers[i - 1];
-
+    ss->killers[1] = ss->killers[0];
     ss->killers[0] = m;
   }
 
@@ -2214,10 +2211,7 @@ namespace {
         ss->reduction = Depth(0);
 
         if (i < 3)
-        {
-            ss->init();
             ss->initKillers();
-        }
     }
   }
 
@@ -2737,6 +2731,11 @@ namespace {
     StateInfo st;
     bool includeAllMoves = (searchMoves[0] == MOVE_NONE);
 
+    // Initialize search stack
+    init_ss_array(ss, PLY_MAX_PLUS_2);
+    ss[0].init();
+    ss[0].eval = VALUE_NONE;
+
     // Generate all legal moves
     MoveStack* last = generate_moves(pos, mlist);
 
@@ -2752,10 +2751,8 @@ namespace {
             continue;
 
         // Find a quick score for the move
-        init_ss_array(ss, PLY_MAX_PLUS_2);
-        ss[0].eval = VALUE_NONE;
-        ss[0].currentMove = cur->move;
         pos.do_move(cur->move, st);
+        ss[0].currentMove = cur->move;
         moves[count].move = cur->move;
         moves[count].score = -qsearch<PV>(pos, ss+1, -VALUE_INFINITE, VALUE_INFINITE, Depth(0), 1);
         moves[count].pv[0] = cur->move;