]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Space inflate bottom part of search.cpp
[stockfish] / src / search.cpp
index c79f11ccc0bd8f32aaf6dc0a513e708ac366d407..03993be432a43ce24d496bc0b7bac92fd8d016a6 100644 (file)
@@ -208,7 +208,7 @@ namespace {
   bool loseOnTime = false;
 
   // Extensions. Array index 0 is used at non-PV nodes, index 1 at PV nodes.
-  Depth CheckExtension[2], SingleReplyExtension[2], PawnPushTo7thExtension[2];
+  Depth CheckExtension[2], SingleEvasionExtension[2], PawnPushTo7thExtension[2];
   Depth PassedPawnExtension[2], PawnEndgameExtension[2], MateThreatExtension[2];
 
   // Iteration counters
@@ -246,8 +246,7 @@ namespace {
   Lock MPLock;
   Lock IOLock;
   bool AllThreadsShouldExit = false;
-  const int MaxActiveSplitPoints = 8; // FIXME, sync with UCI Option
-  SplitPoint SplitPointStack[THREAD_MAX][MaxActiveSplitPoints];
+  SplitPoint SplitPointStack[THREAD_MAX][ACTIVE_SPLIT_POINTS_MAX];
   bool Idle = true;
 
 #if !defined(_MSC_VER)
@@ -281,7 +280,7 @@ namespace {
   bool connected_moves(const Position& pos, Move m1, Move m2);
   bool value_is_mate(Value value);
   bool move_is_killer(Move m, const SearchStack& ss);
-  Depth extension(const Position& pos, Move m, bool pvNode, bool capture, bool check, bool singleReply, bool mateThreat, bool* dangerous);
+  Depth extension(const Position&, Move, bool, bool, bool, bool, bool, bool*);
   bool ok_to_do_nullmove(const Position& pos);
   bool ok_to_prune(const Position& pos, Move m, Move threat);
   bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply);
@@ -406,8 +405,8 @@ bool think(const Position& pos, bool infinite, bool ponder, int side_to_move,
   CheckExtension[1] = Depth(get_option_value_int("Check Extension (PV nodes)"));
   CheckExtension[0] = Depth(get_option_value_int("Check Extension (non-PV nodes)"));
 
-  SingleReplyExtension[1] = Depth(get_option_value_int("Single Reply Extension (PV nodes)"));
-  SingleReplyExtension[0] = Depth(get_option_value_int("Single Reply Extension (non-PV nodes)"));
+  SingleEvasionExtension[1] = Depth(get_option_value_int("Single Evasion Extension (PV nodes)"));
+  SingleEvasionExtension[0] = Depth(get_option_value_int("Single Evasion Extension (non-PV nodes)"));
 
   PawnPushTo7thExtension[1] = Depth(get_option_value_int("Pawn Push to 7th Extension (PV nodes)"));
   PawnPushTo7thExtension[0] = Depth(get_option_value_int("Pawn Push to 7th Extension (non-PV nodes)"));
@@ -593,7 +592,7 @@ void init_threads() {
   }
 
   // Launch the helper threads
-  for(i = 1; i < THREAD_MAX; i++)
+  for (i = 1; i < THREAD_MAX; i++)
   {
 #if !defined(_MSC_VER)
       pthread_create(pthread, NULL, init_thread, (void*)(&i));
@@ -620,7 +619,7 @@ void stop_threads() {
   for (int i = 1; i < THREAD_MAX; i++)
   {
       Threads[i].stop = true;
-      while(Threads[i].running);
+      while (Threads[i].running);
   }
   destroy_split_point_stack();
 }
@@ -1090,7 +1089,7 @@ namespace {
     Move ttMove, move;
     Depth ext, newDepth;
     Value oldAlpha, value;
-    bool isCheck, mateThreat, singleReply, moveIsCheck, captureOrPromotion, dangerous;
+    bool isCheck, mateThreat, singleEvasion, moveIsCheck, captureOrPromotion, dangerous;
     int moveCount = 0;
     Value bestValue = -VALUE_INFINITE;
 
@@ -1131,7 +1130,9 @@ namespace {
     ttMove = (tte ? tte->move() : MOVE_NONE);
 
     // Go with internal iterative deepening if we don't have a TT move
-    if (UseIIDAtPVNodes && ttMove == MOVE_NONE && depth >= 5*OnePly)
+    if (   UseIIDAtPVNodes
+        && depth >= 5*OnePly
+        && ttMove == MOVE_NONE)
     {
         search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID);
         ttMove = ss[ply].pv[ply];
@@ -1153,12 +1154,12 @@ namespace {
     {
       assert(move_is_ok(move));
 
-      singleReply = (isCheck && mp.number_of_evasions() == 1);
+      singleEvasion = (isCheck && mp.number_of_evasions() == 1);
       moveIsCheck = pos.move_is_check(move, ci);
       captureOrPromotion = pos.move_is_capture_or_promotion(move);
 
       // Decide the new search depth
-      ext = extension(pos, move, true, captureOrPromotion, moveIsCheck, singleReply, mateThreat, &dangerous);
+      ext = extension(pos, move, true, captureOrPromotion, moveIsCheck, singleEvasion, mateThreat, &dangerous);
 
       // Singular extension search. We extend the TT move if its value is much better than
       // its siblings. To verify this we do a reduced search on all the other moves but the
@@ -1312,7 +1313,7 @@ namespace {
     Move ttMove, move;
     Depth ext, newDepth;
     Value approximateEval, nullValue, value, futilityValue, futilityValueScaled;
-    bool isCheck, useFutilityPruning, singleReply, moveIsCheck, captureOrPromotion, dangerous;
+    bool isCheck, useFutilityPruning, singleEvasion, moveIsCheck, captureOrPromotion, dangerous;
     bool mateThreat = false;
     int moveCount = 0;
     Value bestValue = -VALUE_INFINITE;
@@ -1455,12 +1456,12 @@ namespace {
       if (move == excludedMove)
           continue;
 
-      singleReply = (isCheck && mp.number_of_evasions() == 1);
+      singleEvasion = (isCheck && mp.number_of_evasions() == 1);
       moveIsCheck = pos.move_is_check(move, ci);
       captureOrPromotion = pos.move_is_capture_or_promotion(move);
 
       // Decide the new search depth
-      ext = extension(pos, move, false, captureOrPromotion, moveIsCheck, singleReply, mateThreat, &dangerous);
+      ext = extension(pos, move, false, captureOrPromotion, moveIsCheck, singleEvasion, mateThreat, &dangerous);
 
       // Singular extension search. We extend the TT move if its value is much better than
       // its siblings. To verify this we do a reduced search on all the other moves but the
@@ -2166,7 +2167,7 @@ namespace {
 
   void RootMoveList::set_move_pv(int moveNum, const Move pv[]) {
     int j;
-    for(j = 0; pv[j] != MOVE_NONE; j++)
+    for (j = 0; pv[j] != MOVE_NONE; j++)
       moves[moveNum].pv[j] = pv[j];
     moves[moveNum].pv[j] = MOVE_NONE;
   }
@@ -2249,7 +2250,7 @@ namespace {
 
     ss[ply].pv[ply] = ss[ply].currentMove;
     int p;
-    for(p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++)
+    for (p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++)
       ss[ply].pv[p] = ss[ply+1].pv[p];
     ss[ply].pv[p] = MOVE_NONE;
   }
@@ -2264,7 +2265,7 @@ namespace {
 
     ss[ply].pv[ply] = pss[ply].pv[ply] = ss[ply].currentMove;
     int p;
-    for(p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++)
+    for (p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++)
       ss[ply].pv[p] = pss[ply].pv[p] = ss[ply+1].pv[p];
     ss[ply].pv[p] = pss[ply].pv[p] = MOVE_NONE;
   }
@@ -2373,20 +2374,20 @@ namespace {
   // the move is marked as 'dangerous' so, at least, we avoid to prune it.
 
   Depth extension(const Position& pos, Move m, bool pvNode, bool captureOrPromotion,
-                  bool check, bool singleReply, bool mateThreat, bool* dangerous) {
+                  bool moveIsCheck, bool singleEvasion, bool mateThreat, bool* dangerous) {
 
     assert(m != MOVE_NONE);
 
     Depth result = Depth(0);
-    *dangerous = check | singleReply | mateThreat;
+    *dangerous = moveIsCheck | singleEvasion | mateThreat;
 
     if (*dangerous)
     {
-        if (check)
+        if (moveIsCheck)
             result += CheckExtension[pvNode];
 
-        if (singleReply)
-            result += SingleReplyExtension[pvNode];
+        if (singleEvasion)
+            result += SingleEvasionExtension[pvNode];
 
         if (mateThreat)
             result += MateThreatExtension[pvNode];
@@ -2549,7 +2550,7 @@ namespace {
 
   bool fail_high_ply_1() {
 
-    for(int i = 0; i < ActiveThreads; i++)
+    for (int i = 0; i < ActiveThreads; i++)
         if (Threads[i].failHighPly1)
             return true;
 
@@ -2561,6 +2562,7 @@ namespace {
   // since the beginning of the current search.
 
   int current_search_time() {
+
     return get_system_time() - SearchStartTime;
   }
 
@@ -2568,12 +2570,13 @@ namespace {
   // nps() computes the current nodes/second count.
 
   int nps() {
+
     int t = current_search_time();
-    return (t > 0)? int((nodes_searched() * 1000) / t) : 0;
+    return (t > 0 ? int((nodes_searched() * 1000) / t) : 0);
   }
 
 
-  // poll() performs two different functions:  It polls for user input, and it
+  // poll() performs two different functions: It polls for user input, and it
   // looks at the time consumed so far and decides if it's time to abort the
   // search.
 
@@ -2587,6 +2590,7 @@ namespace {
     {
         // We are line oriented, don't read single chars
         std::string command;
+
         if (!std::getline(std::cin, command))
             command = "quit";
 
@@ -2605,6 +2609,7 @@ namespace {
         else if (command == "ponderhit")
             ponderhit();
     }
+
     // Print search information
     if (t < 1000)
         lastInfoTime = 0;
@@ -2618,6 +2623,7 @@ namespace {
     {
         lastInfoTime = t;
         lock_grab(&IOLock);
+
         if (dbg_show_mean)
             dbg_print_mean();
 
@@ -2626,20 +2632,32 @@ namespace {
 
         cout << "info nodes " << nodes_searched() << " nps " << nps()
              << " time " << t << " hashfull " << TT.full() << endl;
+
         lock_release(&IOLock);
+
         if (ShowCurrentLine)
             Threads[0].printCurrentLine = true;
     }
+
     // Should we stop the search?
     if (PonderSearch)
         return;
 
-    bool overTime =     t > AbsoluteMaxSearchTime
-                     || (RootMoveNumber == 1 && t > MaxSearchTime + ExtraSearchTime && !FailLow) //FIXME: We are not checking any problem flags, BUG?
-                     || (  !FailHigh && !FailLow && !fail_high_ply_1() && !Problem
-                         && t > 6*(MaxSearchTime + ExtraSearchTime));
+    bool stillAtFirstMove =    RootMoveNumber == 1
+                           && !FailLow
+                           &&  t > MaxSearchTime + ExtraSearchTime;
+
+    bool noProblemFound =   !FailHigh
+                         && !FailLow
+                         && !fail_high_ply_1()
+                         && !Problem
+                         &&  t > 6 * (MaxSearchTime + ExtraSearchTime);
 
-    if (   (Iteration >= 3 && (!InfiniteSearch && overTime))
+    bool noMoreTime =   t > AbsoluteMaxSearchTime
+                     || stillAtFirstMove //FIXME: We are not checking any problem flags, BUG?
+                     || noProblemFound;
+
+    if (   (Iteration >= 3 && !InfiniteSearch && noMoreTime)
         || (ExactMaxTime && t >= ExactMaxTime)
         || (Iteration >= 3 && MaxNodes && nodes_searched() >= MaxNodes))
         AbortSearch = true;
@@ -2654,19 +2672,28 @@ namespace {
 
     int t = current_search_time();
     PonderSearch = false;
-    if (Iteration >= 3 &&
-       (!InfiniteSearch && (StopOnPonderhit ||
-                            t > AbsoluteMaxSearchTime ||
-                            (RootMoveNumber == 1 &&
-                             t > MaxSearchTime + ExtraSearchTime && !FailLow) ||
-                            (!FailHigh && !FailLow && !fail_high_ply_1() && !Problem &&
-                             t > 6*(MaxSearchTime + ExtraSearchTime)))))
-      AbortSearch = true;
+
+    bool stillAtFirstMove =    RootMoveNumber == 1
+                           && !FailLow
+                           &&  t > MaxSearchTime + ExtraSearchTime;
+
+    bool noProblemFound =   !FailHigh
+                         && !FailLow
+                         && !fail_high_ply_1()
+                         && !Problem
+                         &&  t > 6 * (MaxSearchTime + ExtraSearchTime);
+
+    bool noMoreTime =   t > AbsoluteMaxSearchTime
+                     || stillAtFirstMove
+                     || noProblemFound;
+
+    if (Iteration >= 3 && !InfiniteSearch && (noMoreTime || StopOnPonderhit))
+        AbortSearch = true;
   }
 
 
   // print_current_line() prints the current line of search for a given
-  // thread.  Called when the UCI option UCI_ShowCurrLine is 'true'.
+  // thread. Called when the UCI option UCI_ShowCurrLine is 'true'.
 
   void print_current_line(SearchStack ss[], int ply, int threadID) {
 
@@ -2702,8 +2729,8 @@ namespace {
 
 
   // wait_for_stop_or_ponderhit() is called when the maximum depth is reached
-  // while the program is pondering.  The point is to work around a wrinkle in
-  // the UCI protocol:  When pondering, the engine is not allowed to give a
+  // while the program is pondering. The point is to work around a wrinkle in
+  // the UCI protocol: When pondering, the engine is not allowed to give a
   // "bestmove" before the GUI sends it a "stop" or "ponderhit" command.
   // We simply wait here until one of these commands is sent, and return,
   // after which the bestmove and pondermove will be printed (in id_loop()).
@@ -2733,41 +2760,48 @@ namespace {
   // object for which the current thread is the master.
 
   void idle_loop(int threadID, SplitPoint* waitSp) {
+
     assert(threadID >= 0 && threadID < THREAD_MAX);
 
     Threads[threadID].running = true;
 
-    while(true) {
-      if(AllThreadsShouldExit && threadID != 0)
-        break;
+    while (true)
+    {
+        if (AllThreadsShouldExit && threadID != 0)
+            break;
+
+        // If we are not thinking, wait for a condition to be signaled instead
+        // of wasting CPU time polling for work.
+        while (threadID != 0 && (Idle || threadID >= ActiveThreads))
+        {
 
-      // If we are not thinking, wait for a condition to be signaled instead
-      // of wasting CPU time polling for work:
-      while(threadID != 0 && (Idle || threadID >= ActiveThreads)) {
 #if !defined(_MSC_VER)
-        pthread_mutex_lock(&WaitLock);
-        if(Idle || threadID >= ActiveThreads)
-          pthread_cond_wait(&WaitCond, &WaitLock);
-        pthread_mutex_unlock(&WaitLock);
+            pthread_mutex_lock(&WaitLock);
+            if (Idle || threadID >= ActiveThreads)
+                pthread_cond_wait(&WaitCond, &WaitLock);
+
+            pthread_mutex_unlock(&WaitLock);
 #else
-        WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
+            WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
 #endif
-      }
+        }
 
       // If this thread has been assigned work, launch a search
-      if(Threads[threadID].workIsWaiting) {
-        Threads[threadID].workIsWaiting = false;
-        if(Threads[threadID].splitPoint->pvNode)
-          sp_search_pv(Threads[threadID].splitPoint, threadID);
-        else
-          sp_search(Threads[threadID].splitPoint, threadID);
-        Threads[threadID].idle = true;
+      if (Threads[threadID].workIsWaiting)
+      {
+          Threads[threadID].workIsWaiting = false;
+          if (Threads[threadID].splitPoint->pvNode)
+              sp_search_pv(Threads[threadID].splitPoint, threadID);
+          else
+              sp_search(Threads[threadID].splitPoint, threadID);
+
+          Threads[threadID].idle = true;
       }
 
       // If this thread is the master of a split point and all threads have
       // finished their work at this split point, return from the idle loop.
-      if(waitSp != NULL && waitSp->cpus == 0)
-        return;
+      if (waitSp != NULL && waitSp->cpus == 0)
+          return;
     }
 
     Threads[threadID].running = false;
@@ -2778,11 +2812,13 @@ namespace {
   // initializes all split point objects.
 
   void init_split_point_stack() {
-    for(int i = 0; i < THREAD_MAX; i++)
-      for(int j = 0; j < MaxActiveSplitPoints; j++) {
-        SplitPointStack[i][j].parent = NULL;
-        lock_init(&(SplitPointStack[i][j].lock), NULL);
-      }
+
+    for (int i = 0; i < THREAD_MAX; i++)
+        for (int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++)
+        {
+            SplitPointStack[i][j].parent = NULL;
+            lock_init(&(SplitPointStack[i][j].lock), NULL);
+        }
   }
 
 
@@ -2790,62 +2826,66 @@ namespace {
   // destroys all locks in the precomputed split point objects.
 
   void destroy_split_point_stack() {
-    for(int i = 0; i < THREAD_MAX; i++)
-      for(int j = 0; j < MaxActiveSplitPoints; j++)
-        lock_destroy(&(SplitPointStack[i][j].lock));
+
+    for (int i = 0; i < THREAD_MAX; i++)
+        for (int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++)
+            lock_destroy(&(SplitPointStack[i][j].lock));
   }
 
 
   // thread_should_stop() checks whether the thread with a given threadID has
-  // been asked to stop, directly or indirectly.  This can happen if a beta
-  // cutoff has occured in thre thread's currently active split point, or in
+  // been asked to stop, directly or indirectly. This can happen if a beta
+  // cutoff has occured in the thread's currently active split point, or in
   // some ancestor of the current split point.
 
   bool thread_should_stop(int threadID) {
+
     assert(threadID >= 0 && threadID < ActiveThreads);
 
     SplitPoint* sp;
 
-    if(Threads[threadID].stop)
-      return true;
-    if(ActiveThreads <= 2)
-      return false;
-    for(sp = Threads[threadID].splitPoint; sp != NULL; sp = sp->parent)
-      if(sp->finished) {
-        Threads[threadID].stop = true;
+    if (Threads[threadID].stop)
         return true;
-      }
+    if (ActiveThreads <= 2)
+        return false;
+    for (sp = Threads[threadID].splitPoint; sp != NULL; sp = sp->parent)
+        if (sp->finished)
+        {
+            Threads[threadID].stop = true;
+            return true;
+        }
     return false;
   }
 
 
   // thread_is_available() checks whether the thread with threadID "slave" is
-  // available to help the thread with threadID "master" at a split point.  An
-  // obvious requirement is that "slave" must be idle.  With more than two
+  // available to help the thread with threadID "master" at a split point. An
+  // obvious requirement is that "slave" must be idle. With more than two
   // threads, this is not by itself sufficient:  If "slave" is the master of
   // some active split point, it is only available as a slave to the other
   // threads which are busy searching the split point at the top of "slave"'s
   // split point stack (the "helpful master concept" in YBWC terminology).
 
   bool thread_is_available(int slave, int master) {
+
     assert(slave >= 0 && slave < ActiveThreads);
     assert(master >= 0 && master < ActiveThreads);
     assert(ActiveThreads > 1);
 
-    if(!Threads[slave].idle || slave == master)
-      return false;
+    if (!Threads[slave].idle || slave == master)
+        return false;
 
-    if(Threads[slave].activeSplitPoints == 0)
-      // No active split points means that the thread is available as a slave
-      // for any other thread.
-      return true;
+    if (Threads[slave].activeSplitPoints == 0)
+        // No active split points means that the thread is available as
+        // a slave for any other thread.
+        return true;
 
-    if(ActiveThreads == 2)
-      return true;
+    if (ActiveThreads == 2)
+        return true;
 
     // Apply the "helpful master" concept if possible.
-    if(SplitPointStack[slave][Threads[slave].activeSplitPoints-1].slaves[master])
-      return true;
+    if (SplitPointStack[slave][Threads[slave].activeSplitPoints - 1].slaves[master])
+        return true;
 
     return false;
   }
@@ -2855,25 +2895,27 @@ namespace {
   // a slave for the thread with threadID "master".
 
   bool idle_thread_exists(int master) {
+
     assert(master >= 0 && master < ActiveThreads);
     assert(ActiveThreads > 1);
 
-    for(int i = 0; i < ActiveThreads; i++)
-      if(thread_is_available(i, master))
-        return true;
+    for (int i = 0; i < ActiveThreads; i++)
+        if (thread_is_available(i, master))
+            return true;
+
     return false;
   }
 
 
   // split() does the actual work of distributing the work at a node between
-  // several threads at PV nodes.  If it does not succeed in splitting the
+  // several threads at PV nodes. If it does not succeed in splitting the
   // node (because no idle threads are available, or because we have no unused
-  // split point objects), the function immediately returns false.  If
+  // split point objects), the function immediately returns false. If
   // splitting is possible, a SplitPoint object is initialized with all the
   // data that must be copied to the helper threads (the current position and
   // search stack, alpha, beta, the search depth, etc.), and we tell our
-  // helper threads that they have been assigned work.  This will cause them
-  // to instantly leave their idle loops and call sp_search_pv().  When all
+  // helper threads that they have been assigned work. This will cause them
+  // to instantly leave their idle loops and call sp_search_pv(). When all
   // threads have returned from sp_search_pv (or, equivalently, when
   // splitPoint->cpus becomes 0), split() returns true.
 
@@ -2898,22 +2940,23 @@ namespace {
 
     // If no other thread is available to help us, or if we have too many
     // active split points, don't split.
-    if(!idle_thread_exists(master) ||
-       Threads[master].activeSplitPoints >= MaxActiveSplitPoints) {
-      lock_release(&MPLock);
-      return false;
+    if (   !idle_thread_exists(master)
+        || Threads[master].activeSplitPoints >= ACTIVE_SPLIT_POINTS_MAX)
+    {
+        lock_release(&MPLock);
+        return false;
     }
 
     // Pick the next available split point object from the split point stack
     splitPoint = SplitPointStack[master] + Threads[master].activeSplitPoints;
     Threads[master].activeSplitPoints++;
 
-    // Initialize the split point object
+    // Initialize the split point object and copy current position
     splitPoint->parent = Threads[master].splitPoint;
     splitPoint->finished = false;
     splitPoint->ply = ply;
     splitPoint->depth = depth;
-    splitPoint->alpha = pvNode? *alpha : (*beta - 1);
+    splitPoint->alpha = pvNode ? *alpha : (*beta - 1);
     splitPoint->beta = *beta;
     splitPoint->pvNode = pvNode;
     splitPoint->bestValue = *bestValue;
@@ -2924,54 +2967,58 @@ namespace {
     splitPoint->cpus = 1;
     splitPoint->pos.copy(p);
     splitPoint->parentSstack = sstck;
-    for(i = 0; i < ActiveThreads; i++)
-      splitPoint->slaves[i] = 0;
+    for (i = 0; i < ActiveThreads; i++)
+        splitPoint->slaves[i] = 0;
 
-    // Copy the current position and the search stack to the master thread
-    memcpy(splitPoint->sstack[master], sstck, (ply+1)*sizeof(SearchStack));
+    // Copy the current search stack to the master thread
+    memcpy(splitPoint->sstack[master], sstck, (ply+1) * sizeof(SearchStack));
     Threads[master].splitPoint = splitPoint;
 
     // Make copies of the current position and search stack for each thread
-    for(i = 0; i < ActiveThreads && splitPoint->cpus < MaxThreadsPerSplitPoint;
-        i++)
-      if(thread_is_available(i, master)) {
-        memcpy(splitPoint->sstack[i], sstck, (ply+1)*sizeof(SearchStack));
-        Threads[i].splitPoint = splitPoint;
-        splitPoint->slaves[i] = 1;
-        splitPoint->cpus++;
-      }
+    for (i = 0; i < ActiveThreads && splitPoint->cpus < MaxThreadsPerSplitPoint; i++)
+        if (thread_is_available(i, master))
+        {
+            memcpy(splitPoint->sstack[i], sstck, (ply+1) * sizeof(SearchStack));
+            Threads[i].splitPoint = splitPoint;
+            splitPoint->slaves[i] = 1;
+            splitPoint->cpus++;
+        }
 
-    // Tell the threads that they have work to do.  This will make them leave
+    // Tell the threads that they have work to do. This will make them leave
     // their idle loop.
-    for(i = 0; i < ActiveThreads; i++)
-      if(i == master || splitPoint->slaves[i]) {
-        Threads[i].workIsWaiting = true;
-        Threads[i].idle = false;
-        Threads[i].stop = false;
-      }
+    for (i = 0; i < ActiveThreads; i++)
+        if (i == master || splitPoint->slaves[i])
+        {
+            Threads[i].workIsWaiting = true;
+            Threads[i].idle = false;
+            Threads[i].stop = false;
+        }
 
     lock_release(&MPLock);
 
-    // Everything is set up.  The master thread enters the idle loop, from
+    // Everything is set up. The master thread enters the idle loop, from
     // which it will instantly launch a search, because its workIsWaiting
     // slot is 'true'.  We send the split point as a second parameter to the
     // idle loop, which means that the main thread will return from the idle
     // loop when all threads have finished their work at this split point
-    // (i.e. when // splitPoint->cpus == 0).
+    // (i.e. when splitPoint->cpus == 0).
     idle_loop(master, splitPoint);
 
     // We have returned from the idle loop, which means that all threads are
-    // finished. Update alpha, beta and bestvalue, and return.
+    // finished. Update alpha, beta and bestValue, and return.
     lock_grab(&MPLock);
-    if(pvNode) *alpha = splitPoint->alpha;
+
+    if (pvNode)
+        *alpha = splitPoint->alpha;
+
     *beta = splitPoint->beta;
     *bestValue = splitPoint->bestValue;
     Threads[master].stop = false;
     Threads[master].idle = false;
     Threads[master].activeSplitPoints--;
     Threads[master].splitPoint = splitPoint->parent;
-    lock_release(&MPLock);
 
+    lock_release(&MPLock);
     return true;
   }
 
@@ -2980,39 +3027,45 @@ namespace {
   // to start a new search from the root.
 
   void wake_sleeping_threads() {
-    if(ActiveThreads > 1) {
-      for(int i = 1; i < ActiveThreads; i++) {
-        Threads[i].idle = true;
-        Threads[i].workIsWaiting = false;
-      }
+
+    if (ActiveThreads > 1)
+    {
+        for (int i = 1; i < ActiveThreads; i++)
+        {
+            Threads[i].idle = true;
+            Threads[i].workIsWaiting = false;
+        }
+
 #if !defined(_MSC_VER)
       pthread_mutex_lock(&WaitLock);
       pthread_cond_broadcast(&WaitCond);
       pthread_mutex_unlock(&WaitLock);
 #else
-      for(int i = 1; i < THREAD_MAX; i++)
-        SetEvent(SitIdleEvent[i]);
+      for (int i = 1; i < THREAD_MAX; i++)
+          SetEvent(SitIdleEvent[i]);
 #endif
     }
   }
 
 
   // init_thread() is the function which is called when a new thread is
-  // launched.  It simply calls the idle_loop() function with the supplied
-  // threadID.  There are two versions of this function; one for POSIX threads
-  // and one for Windows threads.
+  // launched. It simply calls the idle_loop() function with the supplied
+  // threadID. There are two versions of this function; one for POSIX
+  // threads and one for Windows threads.
 
 #if !defined(_MSC_VER)
 
-  void *init_thread(void *threadID) {
-    idle_loop(*(int *)threadID, NULL);
+  void* init_thread(void *threadID) {
+
+    idle_loop(*(int*)threadID, NULL);
     return NULL;
   }
 
 #else
 
   DWORD WINAPI init_thread(LPVOID threadID) {
-    idle_loop(*(int *)threadID, NULL);
+
+    idle_loop(*(int*)threadID, NULL);
     return NULL;
   }