]> git.sesse.net Git - stockfish/blobdiff - src/movepick.cpp
Use acquire() and release() for spinlocks
[stockfish] / src / movepick.cpp
index 3b636b935d431ac1af263b6e1ede2fc882fb3e23..b82e68b66a8019160998e7c0880c71ef58bbac2a 100644 (file)
@@ -67,8 +67,8 @@ namespace {
 /// search captures, promotions and some checks) and how important good move
 /// ordering is at the current node.
 
-MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
-                       Move* cm, Move* fm, Search::Stack* s) : pos(p), history(h), depth(d) {
+MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h, const CounterMovesHistoryStats& cmh,
+                       Move* cm, Move* fm, Search::Stack* s) : pos(p), history(h), counterMovesHistory(cmh), depth(d) {
 
   assert(d > DEPTH_ZERO);
 
@@ -87,8 +87,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
   endMoves += (ttMove != MOVE_NONE);
 }
 
-MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
-                       Square s) : pos(p), history(h) {
+MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h, const CounterMovesHistoryStats& cmh,
+                       Square s) : pos(p), history(h), counterMovesHistory(cmh) {
 
   assert(d <= DEPTH_ZERO);
 
@@ -112,8 +112,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
   endMoves += (ttMove != MOVE_NONE);
 }
 
-MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
-                       : pos(p), history(h) {
+MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, const CounterMovesHistoryStats& cmh, PieceType pt)
+                       : pos(p), history(h), counterMovesHistory(cmh) {
 
   assert(!pos.checkers());
 
@@ -162,8 +162,13 @@ void MovePicker::score<CAPTURES>() {
 
 template<>
 void MovePicker::score<QUIETS>() {
+
+  Square prevSq = to_sq((ss-1)->currentMove);
+  const HistoryStats& cmh = counterMovesHistory[pos.piece_on(prevSq)][prevSq];
+
   for (auto& m : *this)
-      m.value = history[pos.moved_piece(m)][to_sq(m)];
+      m.value =  history[pos.moved_piece(m)][to_sq(m)]
+               + cmh[pos.moved_piece(m)][to_sq(m)];
 }
 
 template<>
@@ -201,13 +206,28 @@ void MovePicker::generate_next_stage() {
 
   case KILLERS_S1:
       cur = killers;
-      endMoves = cur + 6;
+      endMoves = cur + 2;
+
       killers[0] = ss->killers[0];
       killers[1] = ss->killers[1];
-      killers[2] = countermoves[0];
-      killers[3] = countermoves[1];
-      killers[4] = followupmoves[0];
-      killers[5] = followupmoves[1];
+      killers[2].move = killers[3].move = MOVE_NONE;
+      killers[4].move = killers[5].move = MOVE_NONE;
+
+      // In SMP case countermoves[] and followupmoves[] could have duplicated entries
+      // in rare cases (less than 1 out of a million). This is harmless.
+
+      // Be sure countermoves and followupmoves are different from killers
+      for (int i = 0; i < 2; ++i)
+          if (   countermoves[i] != killers[0]
+              && countermoves[i] != killers[1])
+              *endMoves++ = countermoves[i];
+
+      for (int i = 0; i < 2; ++i)
+          if (   followupmoves[i] != killers[0]
+              && followupmoves[i] != killers[1]
+              && followupmoves[i] != killers[2]
+              && followupmoves[i] != killers[3])
+              *endMoves++ = followupmoves[i];
       break;
 
   case QUIETS_1_S1:
@@ -292,13 +312,7 @@ Move MovePicker::next_move<false>() {
               &&  move != ttMove
               &&  pos.pseudo_legal(move)
               && !pos.capture(move))
-          {
-              for (int i = 0; i < cur - 1 - killers; i++) // Skip duplicated
-                  if (move == killers[i])
-                      goto skip;
               return move;
-          }
-      skip:
           break;
 
       case QUIETS_1_S1: case QUIETS_2_S1: