]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Move depth computation out of fail low loop
[stockfish] / src / search.cpp
index 83ce84d87c64cfd16e0e7f646a6893b7f7bcc5c5..fff00026c6cdad7380c32d1e4b53471f65efae05 100644 (file)
@@ -38,6 +38,7 @@
 #include "lock.h"
 #include "san.h"
 #include "search.h"
+#include "timeman.h"
 #include "thread.h"
 #include "tt.h"
 #include "ucioption.h"
@@ -90,7 +91,7 @@ namespace {
 
     template <bool Fake>
     void split(const Position& pos, SearchStack* ss, int ply, Value* alpha, const Value beta, Value* bestValue,
-               Depth depth, bool mateThreat, int* moveCount, MovePicker* mp, bool pvNode);
+               Depth depth, Move threatMove, bool mateThreat, int* moveCount, MovePicker* mp, bool pvNode);
 
   private:
     friend void poll();
@@ -198,7 +199,7 @@ namespace {
   Depth PassedPawnExtension[2], PawnEndgameExtension[2], MateThreatExtension[2];
 
   // Minimum depth for use of singular extension
-  const Depth SingularExtensionDepth[2] = { 8 * OnePly /* non-PV */, 6 * OnePly /* PV */};
+  const Depth SingularExtensionDepth[2] = { 7 * OnePly /* non-PV */, 6 * OnePly /* PV */};
 
   // If the TT move is at least SingularExtensionMargin better then the
   // remaining ones we will extend it.
@@ -250,10 +251,10 @@ namespace {
   int MultiPV;
 
   // Time managment variables
-  int SearchStartTime, MaxNodes, MaxDepth, MaxSearchTime;
-  int AbsoluteMaxSearchTime, ExtraSearchTime, ExactMaxTime;
+  int SearchStartTime, MaxNodes, MaxDepth, ExactMaxTime;
   bool UseTimeManagement, InfiniteSearch, PonderSearch, StopOnPonderhit;
   bool FirstRootMove, AbortSearch, Quit, AspirationFailLow;
+  TimeManager TimeMgr;
 
   // Log file
   bool UseLogFile;
@@ -262,7 +263,7 @@ namespace {
   // Multi-threads related variables
   Depth MinimumSplitDepth;
   int MaxThreadsPerSplitPoint;
-  ThreadsManager TM;
+  ThreadsManager ThreadsMgr;
 
   // Node counters, used only by thread[0] but try to keep in different cache
   // lines (64 bytes each) from the heavy multi-thread read accessed variables.
@@ -309,6 +310,8 @@ namespace {
   void wait_for_stop_or_ponderhit();
   void init_ss_array(SearchStack* ss, int size);
   void print_pv_info(const Position& pos, Move pv[], Value alpha, Value beta, Value value);
+  void insert_pv_in_tt(const Position& pos, Move pv[]);
+  void extract_pv_from_tt(const Position& pos, Move bestMove, Move pv[]);
 
 #if !defined(_MSC_VER)
   void *init_thread(void *threadID);
@@ -326,9 +329,9 @@ namespace {
 /// init_threads(), exit_threads() and nodes_searched() are helpers to
 /// give accessibility to some TM methods from outside of current file.
 
-void init_threads() { TM.init_threads(); }
-void exit_threads() { TM.exit_threads(); }
-int64_t nodes_searched() { return TM.nodes_searched(); }
+void init_threads() { ThreadsMgr.init_threads(); }
+void exit_threads() { ThreadsMgr.exit_threads(); }
+int64_t nodes_searched() { return ThreadsMgr.nodes_searched(); }
 
 
 /// init_search() is called during startup. It initializes various lookup tables
@@ -358,45 +361,32 @@ void init_search() {
 }
 
 
-// SearchStack::init() initializes a search stack entry.
-// Called at the beginning of search() when starting to examine a new node.
-void SearchStack::init() {
-
-  currentMove = threatMove = bestMove = MOVE_NONE;
-}
-
-// SearchStack::initKillers() initializes killers for a search stack entry
-void SearchStack::initKillers() {
-
-  killers[0] = killers[1] = mateKiller = MOVE_NONE;
-}
-
-
 /// perft() is our utility to verify move generation is bug free. All the legal
 /// moves up to given depth are generated and counted and the sum returned.
 
 int perft(Position& pos, Depth depth)
 {
+    MoveStack mlist[256];
     StateInfo st;
-    Move move;
+    Move m;
     int sum = 0;
-    MovePicker mp(pos, MOVE_NONE, depth, H);
+
+    // Generate all legal moves
+    MoveStack* last = generate_moves(pos, mlist);
 
     // If we are at the last ply we don't need to do and undo
     // the moves, just to count them.
-    if (depth <= OnePly) // Replace with '<' to test also qsearch
-    {
-        while (mp.get_next_move()) sum++;
-        return sum;
-    }
+    if (depth <= OnePly)
+        return int(last - mlist);
 
     // Loop through all legal moves
     CheckInfo ci(pos);
-    while ((move = mp.get_next_move()) != MOVE_NONE)
+    for (MoveStack* cur = mlist; cur != last; cur++)
     {
-        pos.do_move(move, st, ci, pos.move_is_check(move, ci));
+        m = cur->move;
+        pos.do_move(m, st, ci, pos.move_is_check(m, ci));
         sum += perft(pos, depth - OnePly);
-        pos.undo_move(move);
+        pos.undo_move(m);
     }
     return sum;
 }
@@ -412,9 +402,8 @@ bool think(const Position& pos, bool infinite, bool ponder, int time[], int incr
 
   // Initialize global search variables
   StopOnPonderhit = AbortSearch = Quit = AspirationFailLow = false;
-  MaxSearchTime = AbsoluteMaxSearchTime = ExtraSearchTime = 0;
   NodesSincePoll = 0;
-  TM.resetNodeCounters();
+  ThreadsMgr.resetNodeCounters();
   SearchStartTime = get_system_time();
   ExactMaxTime = maxTime;
   MaxDepth = maxDepth;
@@ -471,53 +460,20 @@ bool think(const Position& pos, bool infinite, bool ponder, int time[], int incr
 
   // Set the number of active threads
   int newActiveThreads = get_option_value_int("Threads");
-  if (newActiveThreads != TM.active_threads())
+  if (newActiveThreads != ThreadsMgr.active_threads())
   {
-      TM.set_active_threads(newActiveThreads);
-      init_eval(TM.active_threads());
+      ThreadsMgr.set_active_threads(newActiveThreads);
+      init_eval(ThreadsMgr.active_threads());
   }
 
   // Wake up sleeping threads
-  TM.wake_sleeping_threads();
+  ThreadsMgr.wake_sleeping_threads();
 
   // Set thinking time
   int myTime = time[pos.side_to_move()];
   int myIncrement = increment[pos.side_to_move()];
   if (UseTimeManagement)
-  {
-      if (!movesToGo) // Sudden death time control
-      {
-          if (myIncrement)
-          {
-              MaxSearchTime = myTime / 30 + myIncrement;
-              AbsoluteMaxSearchTime = Max(myTime / 4, myIncrement - 100);
-          }
-          else // Blitz game without increment
-          {
-              MaxSearchTime = myTime / 30;
-              AbsoluteMaxSearchTime = myTime / 8;
-          }
-      }
-      else // (x moves) / (y minutes)
-      {
-          if (movesToGo == 1)
-          {
-              MaxSearchTime = myTime / 2;
-              AbsoluteMaxSearchTime = (myTime > 3000)? (myTime - 500) : ((myTime * 3) / 4);
-          }
-          else
-          {
-              MaxSearchTime = myTime / Min(movesToGo, 20);
-              AbsoluteMaxSearchTime = Min((4 * myTime) / movesToGo, myTime / 3);
-          }
-      }
-
-      if (get_option_value_bool("Ponder"))
-      {
-          MaxSearchTime += MaxSearchTime / 4;
-          MaxSearchTime = Min(MaxSearchTime, AbsoluteMaxSearchTime);
-      }
-  }
+      TimeMgr.init(myTime, myIncrement, movesToGo, pos.startpos_ply_counter());
 
   // Set best NodesBetweenPolls interval to avoid lagging under
   // heavy time pressure.
@@ -545,7 +501,7 @@ bool think(const Position& pos, bool infinite, bool ponder, int time[], int incr
   if (UseLogFile)
       LogFile.close();
 
-  TM.put_threads_to_sleep();
+  ThreadsMgr.put_threads_to_sleep();
 
   return !Quit;
 }
@@ -584,7 +540,7 @@ namespace {
          << "\ninfo depth " << 1
          << " score " << value_to_uci(rml.get_move_score(0))
          << " time " << current_search_time()
-         << " nodes " << TM.nodes_searched()
+         << " nodes " << ThreadsMgr.nodes_searched()
          << " nps " << nps()
          << " pv " << rml.get_move(0) << "\n";
 
@@ -628,7 +584,7 @@ namespace {
 
         // Write PV to transposition table, in case the relevant entries have
         // been overwritten during the search.
-        TT.insert_pv(p, pv);
+        insert_pv_in_tt(p, pv);
 
         if (AbortSearch)
             break; // Value cannot be trusted. Break out immediately!
@@ -657,24 +613,24 @@ namespace {
                 stopSearch = true;
 
             // Stop search early if one move seems to be much better than the others
-            int64_t nodes = TM.nodes_searched();
+            int64_t nodes = ThreadsMgr.nodes_searched();
             if (   Iteration >= 8
                 && EasyMove == pv[0]
                 && (  (   rml.get_move_cumulative_nodes(0) > (nodes * 85) / 100
-                       && current_search_time() > MaxSearchTime / 16)
+                       && current_search_time() > TimeMgr.available_time() / 16)
                     ||(   rml.get_move_cumulative_nodes(0) > (nodes * 98) / 100
-                       && current_search_time() > MaxSearchTime / 32)))
+                       && current_search_time() > TimeMgr.available_time() / 32)))
                 stopSearch = true;
 
             // Add some extra time if the best move has changed during the last two iterations
             if (Iteration > 5 && Iteration <= 50)
-                ExtraSearchTime = BestMoveChangesByIteration[Iteration]   * (MaxSearchTime / 2)
-                                + BestMoveChangesByIteration[Iteration-1] * (MaxSearchTime / 3);
+                TimeMgr.pv_unstability(BestMoveChangesByIteration[Iteration],
+                                       BestMoveChangesByIteration[Iteration-1]);
 
             // Stop search if most of MaxSearchTime is consumed at the end of the
             // iteration. We probably don't have enough time to search the first
             // move at the next iteration anyway.
-            if (current_search_time() > ((MaxSearchTime + ExtraSearchTime) * 80) / 128)
+            if (current_search_time() > (TimeMgr.available_time() * 80) / 128)
                 stopSearch = true;
 
             if (stopSearch)
@@ -696,7 +652,7 @@ namespace {
         wait_for_stop_or_ponderhit();
     else
         // Print final search statistics
-        cout << "info nodes " << TM.nodes_searched()
+        cout << "info nodes " << ThreadsMgr.nodes_searched()
              << " nps " << nps()
              << " time " << current_search_time() << endl;
 
@@ -724,7 +680,7 @@ namespace {
         if (dbg_show_hit_rate)
             dbg_print_hit_rate(LogFile);
 
-        LogFile << "\nNodes: " << TM.nodes_searched()
+        LogFile << "\nNodes: " << ThreadsMgr.nodes_searched()
                 << "\nNodes/second: " << nps()
                 << "\nBest move: " << move_to_san(p, pv[0]);
 
@@ -759,9 +715,10 @@ namespace {
     alpha = *alphaPtr;
     beta = *betaPtr;
     isCheck = pos.is_check();
+    depth = (Iteration - 2) * OnePly + InitialDepth;
 
     // Step 1. Initialize node (polling is omitted at root)
-    ss->init();
+    ss->currentMove = ss->bestMove = MOVE_NONE;
 
     // Step 2. Check for aborted search (omitted at root)
     // Step 3. Mate distance pruning (omitted at root)
@@ -791,10 +748,10 @@ namespace {
             FirstRootMove = (i == 0);
 
             // Save the current node count before the move is searched
-            nodes = TM.nodes_searched();
+            nodes = ThreadsMgr.nodes_searched();
 
             // Reset beta cut-off counters
-            TM.resetBetaCounters();
+            ThreadsMgr.resetBetaCounters();
 
             // Pick the next root move, and print the move and the move number to
             // the standard output.
@@ -808,7 +765,6 @@ namespace {
             captureOrPromotion = pos.move_is_capture_or_promotion(move);
 
             // Step 11. Decide the new search depth
-            depth = (Iteration - 2) * OnePly + InitialDepth;
             ext = extension<PV>(pos, move, captureOrPromotion, moveIsCheck, false, false, &dangerous);
             newDepth = depth + ext;
 
@@ -895,7 +851,7 @@ namespace {
                 // the score before research in case we run out of time while researching.
                 rml.set_move_score(i, value);
                 ss->bestMove = move;
-                TT.extract_pv(pos, move, pv, PLY_MAX);
+                extract_pv_from_tt(pos, move, pv);
                 rml.set_move_pv(i, pv);
 
                 // Print information to the standard output
@@ -918,9 +874,9 @@ namespace {
             // Remember beta-cutoff and searched nodes counts for this move. The
             // info is used to sort the root moves for the next iteration.
             int64_t our, their;
-            TM.get_beta_counters(pos.side_to_move(), our, their);
+            ThreadsMgr.get_beta_counters(pos.side_to_move(), our, their);
             rml.set_beta_counters(i, our, their);
-            rml.set_move_nodes(i, TM.nodes_searched() - nodes);
+            rml.set_move_nodes(i, ThreadsMgr.nodes_searched() - nodes);
 
             assert(value >= -VALUE_INFINITE && value <= VALUE_INFINITE);
             assert(value < beta);
@@ -935,7 +891,7 @@ namespace {
                 // Update PV
                 rml.set_move_score(i, value);
                 ss->bestMove = move;
-                TT.extract_pv(pos, move, pv, PLY_MAX);
+                extract_pv_from_tt(pos, move, pv);
                 rml.set_move_pv(i, pv);
 
                 if (MultiPV == 1)
@@ -962,7 +918,7 @@ namespace {
                              << " score " << value_to_uci(rml.get_move_score(j))
                              << " depth " << (j <= i ? Iteration : Iteration - 1)
                              << " time " << current_search_time()
-                             << " nodes " << TM.nodes_searched()
+                             << " nodes " << ThreadsMgr.nodes_searched()
                              << " nps " << nps()
                              << " pv ";
 
@@ -1009,14 +965,14 @@ namespace {
     assert(beta > alpha && beta <= VALUE_INFINITE);
     assert(PvNode || alpha == beta - 1);
     assert(ply > 0 && ply < PLY_MAX);
-    assert(pos.thread() >= 0 && pos.thread() < TM.active_threads());
+    assert(pos.thread() >= 0 && pos.thread() < ThreadsMgr.active_threads());
 
     Move movesSearched[256];
     EvalInfo ei;
     StateInfo st;
-    const TTEntry* tte;
+    const TTEntry *tte, *ttx;
     Key posKey;
-    Move ttMove, move, excludedMove;
+    Move ttMove, move, excludedMove, threatMove;
     Depth ext, newDepth;
     Value bestValue, value, oldAlpha;
     Value refinedValue, nullValue, futilityValueScaled; // Non-PV specific
@@ -1028,9 +984,9 @@ namespace {
     oldAlpha = alpha;
 
     // Step 1. Initialize node and poll. Polling can abort search
-    TM.incrementNodeCounter(threadID);
-    ss->init();
-    (ss+2)->initKillers();
+    ThreadsMgr.incrementNodeCounter(threadID);
+    ss->currentMove = ss->bestMove = threatMove = MOVE_NONE;
+    (ss+2)->killers[0] = (ss+2)->killers[1] = (ss+2)->mateKiller = MOVE_NONE;
 
     if (threadID == 0 && ++NodesSincePoll > NodesBetweenPolls)
     {
@@ -1039,7 +995,7 @@ namespace {
     }
 
     // Step 2. Check for aborted search and immediate draw
-    if (AbortSearch || TM.thread_should_stop(threadID))
+    if (AbortSearch || ThreadsMgr.thread_should_stop(threadID))
         return Value(0);
 
     if (pos.is_draw() || ply >= PLY_MAX - 1)
@@ -1074,32 +1030,31 @@ namespace {
         // Refresh tte entry to avoid aging
         TT.store(posKey, tte->value(), tte->type(), tte->depth(), ttMove, tte->static_value(), tte->king_danger());
 
-        ss->currentMove = ttMove; // Can be MOVE_NONE
+        ss->bestMove = ttMove; // Can be MOVE_NONE
         return value_from_tt(tte->value(), ply);
     }
 
-    // Step 5. Evaluate the position statically
-    // At PV nodes we do this only to update gain statistics
+    // Step 5. Evaluate the position statically and
+    // update gain statistics of parent move.
     isCheck = pos.is_check();
-    if (!isCheck)
+    if (isCheck)
+        ss->eval = VALUE_NONE;
+    else if (tte)
     {
-        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()]);
-        }
+        assert(tte->static_value() != VALUE_NONE);
 
-        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);
+        ss->eval = tte->static_value();
+        ei.kingDanger[pos.side_to_move()] = tte->king_danger();
+        refinedValue = refine_eval(tte, ss->eval, ply);
     }
     else
-        ss->eval = VALUE_NONE;
+    {
+        refinedValue = 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()]);
+    }
+
+    // Save gain for the parent non-capture move
+    update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
 
     // Step 6. Razoring (is omitted in PV nodes)
     if (   !PvNode
@@ -1125,8 +1080,8 @@ namespace {
     if (   !PvNode
         && !ss->skipNullMove
         &&  depth < RazorDepth
-        &&  refinedValue >= beta + futility_margin(depth, 0)
         && !isCheck
+        &&  refinedValue >= beta + futility_margin(depth, 0)
         && !value_is_mate(beta)
         &&  pos.non_pawn_material(pos.side_to_move()))
         return refinedValue - futility_margin(depth, 0);
@@ -1138,8 +1093,8 @@ namespace {
     if (   !PvNode
         && !ss->skipNullMove
         &&  depth > OnePly
-        &&  refinedValue >= beta - (depth >= 4 * OnePly ? NullMoveMargin : 0)
         && !isCheck
+        &&  refinedValue >= beta - (depth >= 4 * OnePly ? NullMoveMargin : 0)
         && !value_is_mate(beta)
         &&  pos.non_pawn_material(pos.side_to_move()))
     {
@@ -1188,10 +1143,10 @@ namespace {
             if (nullValue == value_mated_in(ply + 2))
                 mateThreat = true;
 
-            ss->threatMove = (ss+1)->currentMove;
+            threatMove = (ss+1)->bestMove;
             if (   depth < ThreatDepth
                 && (ss-1)->reduction
-                && connected_moves(pos, (ss-1)->currentMove, ss->threatMove))
+                && connected_moves(pos, (ss-1)->currentMove, threatMove))
                 return beta - 1;
         }
     }
@@ -1218,9 +1173,11 @@ namespace {
     // Initialize a MovePicker object for the current position
     MovePicker mp = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta));
     CheckInfo ci(pos);
+    ss->bestMove = MOVE_NONE;
     singleEvasion = isCheck && mp.number_of_evasions() == 1;
     singularExtensionNode =   depth >= SingularExtensionDepth[PvNode]
-                           && tte && tte->move()
+                           && tte
+                           && tte->move()
                            && !excludedMove // Do not allow recursive singular extension search
                            && is_lower_bound(tte->type())
                            && tte->depth() >= depth - 3 * OnePly;
@@ -1229,7 +1186,7 @@ namespace {
     // Loop through all legal moves until no moves remain or a beta cutoff occurs
     while (   bestValue < beta
            && (move = mp.get_next_move()) != MOVE_NONE
-           && !TM.thread_should_stop(threadID))
+           && !ThreadsMgr.thread_should_stop(threadID))
     {
       assert(move_is_ok(move));
 
@@ -1250,9 +1207,22 @@ namespace {
           && move == tte->move()
           && ext < OnePly)
       {
+          // Avoid to do an expensive singular extension search on nodes where
+          // such search have already been done in the past, so assume the last
+          // singular extension search result is still valid.
+          if (  !PvNode
+              && depth < SingularExtensionDepth[PvNode] + 5 * OnePly
+              && (ttx = TT.retrieve(pos.get_exclusion_key())) != NULL)
+          {
+              if (is_upper_bound(ttx->type()))
+                  ext = OnePly;
+
+              singularExtensionNode = false;
+          }
+
           Value ttValue = value_from_tt(tte->value(), ply);
 
-          if (abs(ttValue) < VALUE_KNOWN_WIN)
+          if (singularExtensionNode && abs(ttValue) < VALUE_KNOWN_WIN)
           {
               Value b = ttValue - SingularExtensionMargin;
               ss->excludedMove = move;
@@ -1260,6 +1230,7 @@ namespace {
               Value v = search<NonPV>(pos, ss, b - 1, b, depth / 2, ply);
               ss->skipNullMove = false;
               ss->excludedMove = MOVE_NONE;
+              ss->bestMove = MOVE_NONE;
               if (v < b)
                   ext = OnePly;
           }
@@ -1280,7 +1251,7 @@ namespace {
       {
           // Move count based pruning
           if (   moveCount >= futility_move_count(depth)
-              && !(ss->threatMove && connected_threat(pos, move, ss->threatMove))
+              && !(threatMove && connected_threat(pos, move, threatMove))
               && bestValue > value_mated_in(PLY_MAX))
               continue;
 
@@ -1369,7 +1340,7 @@ namespace {
           bestValue = value;
           if (value > alpha)
           {
-              if (PvNode && value < beta) // This guarantees that always: alpha < beta
+              if (PvNode && value < beta) // We want always alpha < beta
                   alpha = value;
 
               if (value == value_mate_in(ply + 1))
@@ -1381,14 +1352,14 @@ namespace {
 
       // Step 18. Check for split
       if (   depth >= MinimumSplitDepth
-          && TM.active_threads() > 1
+          && ThreadsMgr.active_threads() > 1
           && bestValue < beta
-          && TM.available_thread_exists(threadID)
+          && ThreadsMgr.available_thread_exists(threadID)
           && !AbortSearch
-          && !TM.thread_should_stop(threadID)
+          && !ThreadsMgr.thread_should_stop(threadID)
           && Iteration <= 99)
-          TM.split<FakeSplit>(pos, ss, ply, &alpha, beta, &bestValue, depth,
-                              mateThreat, &moveCount, &mp, PvNode);
+          ThreadsMgr.split<FakeSplit>(pos, ss, ply, &alpha, beta, &bestValue, depth,
+                                      threatMove, mateThreat, &moveCount, &mp, PvNode);
     }
 
     // Step 19. Check for mate and stalemate
@@ -1396,22 +1367,22 @@ namespace {
     // no legal moves, it must be mate or stalemate.
     // If one move was excluded return fail low score.
     if (!moveCount)
-        return excludedMove ? oldAlpha : (isCheck ? value_mated_in(ply) : VALUE_DRAW);
+        return excludedMove ? oldAlpha : isCheck ? value_mated_in(ply) : VALUE_DRAW;
 
     // Step 20. Update tables
     // If the search is not aborted, update the transposition table,
     // history counters, and killer moves.
-    if (AbortSearch || TM.thread_should_stop(threadID))
+    if (AbortSearch || ThreadsMgr.thread_should_stop(threadID))
         return bestValue;
 
-    ValueType f = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
+    ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
     move = (bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove);
-    TT.store(posKey, value_to_tt(bestValue, ply), f, depth, move, ss->eval, ei.kingDanger[pos.side_to_move()]);
+    TT.store(posKey, value_to_tt(bestValue, ply), vt, depth, move, ss->eval, ei.kingDanger[pos.side_to_move()]);
 
     // Update killers and history only for non capture moves that fails high
     if (bestValue >= beta)
     {
-        TM.incrementBetaCounter(pos.side_to_move(), depth, threadID);
+        ThreadsMgr.incrementBetaCounter(pos.side_to_move(), depth, threadID);
         if (!pos.move_is_capture_or_promotion(move))
         {
             update_history(pos, move, depth, movesSearched, moveCount);
@@ -1437,7 +1408,7 @@ namespace {
     assert(PvNode || alpha == beta - 1);
     assert(depth <= 0);
     assert(ply > 0 && ply < PLY_MAX);
-    assert(pos.thread() >= 0 && pos.thread() < TM.active_threads());
+    assert(pos.thread() >= 0 && pos.thread() < ThreadsMgr.active_threads());
 
     EvalInfo ei;
     StateInfo st;
@@ -1447,7 +1418,7 @@ namespace {
     const TTEntry* tte;
     Value oldAlpha = alpha;
 
-    TM.incrementNodeCounter(pos.thread());
+    ThreadsMgr.incrementNodeCounter(pos.thread());
     ss->bestMove = ss->currentMove = MOVE_NONE;
 
     // Check for an instant draw or maximum ply reached
@@ -1461,7 +1432,7 @@ namespace {
 
     if (!PvNode && tte && ok_to_use_TT(tte, depth, beta, ply))
     {
-        ss->currentMove = ttMove; // Can be MOVE_NONE
+        ss->bestMove = ttMove; // Can be MOVE_NONE
         return value_from_tt(tte->value(), ply);
     }
 
@@ -1479,6 +1450,7 @@ namespace {
         if (tte)
         {
             assert(tte->static_value() != VALUE_NONE);
+
             ei.kingDanger[pos.side_to_move()] = tte->king_danger();
             bestValue = tte->static_value();
         }
@@ -1588,8 +1560,8 @@ namespace {
 
     // Update transposition table
     Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
-    ValueType f = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
-    TT.store(pos.get_key(), value_to_tt(bestValue, ply), f, d, ss->bestMove, ss->eval, ei.kingDanger[pos.side_to_move()]);
+    ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
+    TT.store(pos.get_key(), value_to_tt(bestValue, ply), vt, d, ss->bestMove, ss->eval, ei.kingDanger[pos.side_to_move()]);
 
     // Update killers only for checking moves that fails high
     if (    bestValue >= beta
@@ -1613,8 +1585,8 @@ namespace {
   template <NodeType PvNode>
   void sp_search(SplitPoint* sp, int threadID) {
 
-    assert(threadID >= 0 && threadID < TM.active_threads());
-    assert(TM.active_threads() > 1);
+    assert(threadID >= 0 && threadID < ThreadsMgr.active_threads());
+    assert(ThreadsMgr.active_threads() > 1);
 
     StateInfo st;
     Move move;
@@ -1636,7 +1608,7 @@ namespace {
 
     while (    sp->bestValue < sp->beta
            && (move = sp->mp->get_next_move()) != MOVE_NONE
-           && !TM.thread_should_stop(threadID))
+           && !ThreadsMgr.thread_should_stop(threadID))
     {
       moveCount = ++sp->moveCount;
       lock_release(&(sp->lock));
@@ -1662,7 +1634,7 @@ namespace {
       {
           // Move count based pruning
           if (   moveCount >= futility_move_count(sp->depth)
-              && !(ss->threatMove && connected_threat(pos, move, ss->threatMove))
+              && !(sp->threatMove && connected_threat(pos, move, sp->threatMove))
               && sp->bestValue > value_mated_in(PLY_MAX))
           {
               lock_grab(&(sp->lock));
@@ -1745,7 +1717,7 @@ namespace {
       // Step 17. Check for new best move
       lock_grab(&(sp->lock));
 
-      if (value > sp->bestValue && !TM.thread_should_stop(threadID))
+      if (value > sp->bestValue && !ThreadsMgr.thread_should_stop(threadID))
       {
           sp->bestValue = value;
 
@@ -2008,8 +1980,7 @@ namespace {
 
   Value refine_eval(const TTEntry* tte, Value defaultEval, int ply) {
 
-      if (!tte)
-          return defaultEval;
+      assert(tte);
 
       Value v = value_from_tt(tte->value(), ply);
 
@@ -2065,8 +2036,7 @@ namespace {
         && before != VALUE_NONE
         && after != VALUE_NONE
         && pos.captured_piece() == NO_PIECE_TYPE
-        && !move_is_castle(m)
-        && !move_is_promotion(m))
+        && !move_is_special(m))
         H.set_gain(pos.piece_on(move_to(m)), move_to(m), -(before + after));
   }
 
@@ -2099,7 +2069,7 @@ namespace {
   int nps() {
 
     int t = current_search_time();
-    return (t > 0 ? int((TM.nodes_searched() * 1000) / t) : 0);
+    return (t > 0 ? int((ThreadsMgr.nodes_searched() * 1000) / t) : 0);
   }
 
 
@@ -2156,7 +2126,7 @@ namespace {
         if (dbg_show_hit_rate)
             dbg_print_hit_rate();
 
-        cout << "info nodes " << TM.nodes_searched() << " nps " << nps()
+        cout << "info nodes " << ThreadsMgr.nodes_searched() << " nps " << nps()
              << " time " << t << endl;
     }
 
@@ -2166,14 +2136,14 @@ namespace {
 
     bool stillAtFirstMove =    FirstRootMove
                            && !AspirationFailLow
-                           &&  t > MaxSearchTime + ExtraSearchTime;
+                           &&  t > TimeMgr.available_time();
 
-    bool noMoreTime =   t > AbsoluteMaxSearchTime
+    bool noMoreTime =   t > TimeMgr.maximum_time()
                      || stillAtFirstMove;
 
     if (   (Iteration >= 3 && UseTimeManagement && noMoreTime)
         || (ExactMaxTime && t >= ExactMaxTime)
-        || (Iteration >= 3 && MaxNodes && TM.nodes_searched() >= MaxNodes))
+        || (Iteration >= 3 && MaxNodes && ThreadsMgr.nodes_searched() >= MaxNodes))
         AbortSearch = true;
   }
 
@@ -2189,9 +2159,9 @@ namespace {
 
     bool stillAtFirstMove =    FirstRootMove
                            && !AspirationFailLow
-                           &&  t > MaxSearchTime + ExtraSearchTime;
+                           &&  t > TimeMgr.available_time();
 
-    bool noMoreTime =   t > AbsoluteMaxSearchTime
+    bool noMoreTime =   t > TimeMgr.maximum_time()
                      || stillAtFirstMove;
 
     if (Iteration >= 3 && UseTimeManagement && (noMoreTime || StopOnPonderhit))
@@ -2211,7 +2181,7 @@ namespace {
         ss->reduction = Depth(0);
 
         if (i < 3)
-            ss->initKillers();
+            ss->killers[0] = ss->killers[1] = ss->mateKiller = MOVE_NONE;
     }
   }
 
@@ -2252,7 +2222,7 @@ namespace {
          << " score "     << value_to_uci(value)
          << (value >= beta ? " lowerbound" : value <= alpha ? " upperbound" : "")
          << " time "  << current_search_time()
-         << " nodes " << TM.nodes_searched()
+         << " nodes " << ThreadsMgr.nodes_searched()
          << " nps "   << nps()
          << " pv ";
 
@@ -2267,8 +2237,63 @@ namespace {
                       value <= alpha ? VALUE_TYPE_UPPER : VALUE_TYPE_EXACT;
 
         LogFile << pretty_pv(pos, current_search_time(), Iteration,
-                             TM.nodes_searched(), value, t, pv) << endl;
+                             ThreadsMgr.nodes_searched(), value, t, pv) << endl;
+    }
+  }
+
+
+  // insert_pv_in_tt() is called at the end of a search iteration, and inserts
+  // the PV back into the TT. This makes sure the old PV moves are searched
+  // first, even if the old TT entries have been overwritten.
+
+  void insert_pv_in_tt(const Position& pos, Move pv[]) {
+
+    StateInfo st;
+    TTEntry* tte;
+    Position p(pos, pos.thread());
+    EvalInfo ei;
+    Value v;
+
+    for (int i = 0; pv[i] != MOVE_NONE; i++)
+    {
+        tte = TT.retrieve(p.get_key());
+        if (!tte || tte->move() != pv[i])
+        {
+            v = (p.is_check() ? VALUE_NONE : evaluate(p, ei));
+            TT.store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, pv[i], v, ei.kingDanger[pos.side_to_move()]);
+        }
+        p.do_move(pv[i], st);
+    }
+  }
+
+
+  // extract_pv_from_tt() builds a PV by adding moves from the transposition table.
+  // We consider also failing high nodes and not only VALUE_TYPE_EXACT nodes. This
+  // allow to always have a ponder move even when we fail high at root and also a
+  // long PV to print that is important for position analysis.
+
+  void extract_pv_from_tt(const Position& pos, Move bestMove, Move pv[]) {
+
+    StateInfo st;
+    TTEntry* tte;
+    Position p(pos, pos.thread());
+    int ply = 0;
+
+    assert(bestMove != MOVE_NONE);
+
+    pv[ply] = bestMove;
+    p.do_move(pv[ply++], st);
+
+    while (   (tte = TT.retrieve(p.get_key())) != NULL
+           && tte->move() != MOVE_NONE
+           && move_is_legal(p, tte->move())
+           && ply < PLY_MAX
+           && (!p.is_draw() || ply < 2))
+    {
+        pv[ply] = tte->move();
+        p.do_move(pv[ply++], st);
     }
+    pv[ply] = MOVE_NONE;
   }
 
 
@@ -2281,7 +2306,7 @@ namespace {
 
   void* init_thread(void *threadID) {
 
-    TM.idle_loop(*(int*)threadID, NULL);
+    ThreadsMgr.idle_loop(*(int*)threadID, NULL);
     return NULL;
   }
 
@@ -2289,7 +2314,7 @@ namespace {
 
   DWORD WINAPI init_thread(LPVOID threadID) {
 
-    TM.idle_loop(*(int*)threadID, NULL);
+    ThreadsMgr.idle_loop(*(int*)threadID, NULL);
     return 0;
   }
 
@@ -2427,8 +2452,8 @@ namespace {
 #endif
 
     // Initialize global locks
-    lock_init(&MPLock, NULL);
-    lock_init(&WaitLock, NULL);
+    lock_init(&MPLock);
+    lock_init(&WaitLock);
 
 #if !defined(_MSC_VER)
     pthread_cond_init(&WaitCond, NULL);
@@ -2440,7 +2465,7 @@ namespace {
     // Initialize splitPoints[] locks
     for (i = 0; i < MAX_THREADS; i++)
         for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
-            lock_init(&(threads[i].splitPoints[j].lock), NULL);
+            lock_init(&(threads[i].splitPoints[j].lock));
 
     // Will be set just before program exits to properly end the threads
     AllThreadsShouldExit = false;
@@ -2583,8 +2608,8 @@ namespace {
 
   template <bool Fake>
   void ThreadsManager::split(const Position& p, SearchStack* ss, int ply, Value* alpha,
-                             const Value beta, Value* bestValue, Depth depth, bool mateThreat,
-                             int* moveCount, MovePicker* mp, bool pvNode) {
+                             const Value beta, Value* bestValue, Depth depth, Move threatMove,
+                             bool mateThreat, int* moveCount, MovePicker* mp, bool pvNode) {
     assert(p.is_ok());
     assert(ply > 0 && ply < PLY_MAX);
     assert(*bestValue >= -VALUE_INFINITE);
@@ -2617,6 +2642,7 @@ namespace {
     splitPoint.stopRequest = false;
     splitPoint.ply = ply;
     splitPoint.depth = depth;
+    splitPoint.threatMove = threatMove;
     splitPoint.mateThreat = mateThreat;
     splitPoint.alpha = *alpha;
     splitPoint.beta = beta;
@@ -2733,7 +2759,7 @@ namespace {
 
     // Initialize search stack
     init_ss_array(ss, PLY_MAX_PLUS_2);
-    ss[0].init();
+    ss[0].currentMove = ss[0].bestMove = MOVE_NONE;
     ss[0].eval = VALUE_NONE;
 
     // Generate all legal moves