]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
In Position backup and restore contiguous data
[stockfish] / src / position.cpp
index 6d4e66cab6c4ddd2eee1e002e1864a0a84ade466..2fc73b660d912898b9563f87a127ff682bf6d5e3 100644 (file)
@@ -678,24 +678,23 @@ bool Position::move_is_capture(Move m) const {
 
 void Position::backup(UndoInfo& u) const {
 
-  u.castleRights = castleRights;
-  u.epSquare     = epSquare;
+  for (Color c = WHITE; c <= BLACK; c++)
+  {
+      u.pinners[c]      = pinners[c];
+      u.pinned[c]       = pinned[c];
+      u.dcCandidates[c] = dcCandidates[c];
+  }
   u.checkersBB   = checkersBB;
   u.key          = key;
   u.pawnKey      = pawnKey;
   u.materialKey  = materialKey;
+  u.castleRights = castleRights;
   u.rule50       = rule50;
+  u.epSquare     = epSquare;
   u.lastMove     = lastMove;
   u.mgValue      = mgValue;
   u.egValue      = egValue;
   u.capture      = NO_PIECE_TYPE;
-
-  for (Color c = WHITE; c <= BLACK; c++)
-  {
-      u.pinners[c]      = pinners[c];
-      u.pinned[c]       = pinned[c];
-      u.dcCandidates[c] = dcCandidates[c];
-  }
 }
 
 
@@ -704,24 +703,23 @@ void Position::backup(UndoInfo& u) const {
 
 void Position::restore(const UndoInfo& u) {
 
-  castleRights = u.castleRights;
-  epSquare     = u.epSquare;
+  for (Color c = WHITE; c <= BLACK; c++)
+  {
+      pinners[c]      = u.pinners[c];
+      pinned[c]       = u.pinned[c];
+      dcCandidates[c] = u.dcCandidates[c];
+  }
   checkersBB   = u.checkersBB;
   key          = u.key;
   pawnKey      = u.pawnKey;
   materialKey  = u.materialKey;
+  castleRights = u.castleRights;
   rule50       = u.rule50;
+  epSquare     = u.epSquare;
   lastMove     = u.lastMove;
   mgValue     = u.mgValue;
   egValue     = u.egValue;
   // u.capture is restored in undo_move()
-
-  for (Color c = WHITE; c <= BLACK; c++)
-  {
-      pinners[c]      = u.pinners[c];
-      pinned[c]       = u.pinned[c];
-      dcCandidates[c] = u.dcCandidates[c];
-  }
 }
 
 
@@ -748,22 +746,16 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square
 /// Position::do_move() makes a move, and backs up all information necessary
 /// to undo the move to an UndoInfo object. The move is assumed to be legal.
 /// Pseudo-legal moves should be filtered out before this function is called.
-/// There are two versions of this function, one which takes only the move and
-/// the UndoInfo as input, and one which takes a third parameter, a bitboard of
-/// discovered check candidates. The second version is faster, because knowing
-/// the discovered check candidates makes it easier to update the checkersBB
-/// member variable in the position object.
 
 void Position::do_move(Move m, UndoInfo& u) {
 
-  do_move(m, u, discovered_check_candidates(side_to_move()));
-}
-
-void Position::do_move(Move m, UndoInfo& u, Bitboard dc) {
-
   assert(is_ok());
   assert(move_is_ok(m));
 
+  // Get now the current (pre-move) dc candidates that we will use
+  // in update_checkers().
+  Bitboard oldDcCandidates = discovered_check_candidates(side_to_move());
+
   // Back up the necessary information to our UndoInfo object (except the
   // captured piece, which is taken care of later.
   backup(u);
@@ -871,12 +863,12 @@ void Position::do_move(Move m, UndoInfo& u, Bitboard dc) {
     Square ksq = king_square(them);
     switch (piece)
     {
-    case PAWN:   update_checkers<PAWN>(&checkersBB, ksq, from, to, dc);   break;
-    case KNIGHT: update_checkers<KNIGHT>(&checkersBB, ksq, from, to, dc); break;
-    case BISHOP: update_checkers<BISHOP>(&checkersBB, ksq, from, to, dc); break;
-    case ROOK:   update_checkers<ROOK>(&checkersBB, ksq, from, to, dc);   break;
-    case QUEEN:  update_checkers<QUEEN>(&checkersBB, ksq, from, to, dc);  break;
-    case KING:   update_checkers<KING>(&checkersBB, ksq, from, to, dc);   break;
+    case PAWN:   update_checkers<PAWN>(&checkersBB, ksq, from, to, oldDcCandidates);   break;
+    case KNIGHT: update_checkers<KNIGHT>(&checkersBB, ksq, from, to, oldDcCandidates); break;
+    case BISHOP: update_checkers<BISHOP>(&checkersBB, ksq, from, to, oldDcCandidates); break;
+    case ROOK:   update_checkers<ROOK>(&checkersBB, ksq, from, to, oldDcCandidates);   break;
+    case QUEEN:  update_checkers<QUEEN>(&checkersBB, ksq, from, to, oldDcCandidates);  break;
+    case KING:   update_checkers<KING>(&checkersBB, ksq, from, to, oldDcCandidates);   break;
     default: assert(false); break;
     }
   }