]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Small Position::clear() cleanup
[stockfish] / src / position.cpp
index 0a7ebadda7f3b5614668781f715f6c067cc7ad3f..cc0f42e9c9ac1b004938a29968b554001e75c9f6 100644 (file)
@@ -23,8 +23,9 @@
 ////
 
 #include <cassert>
-#include <iostream>
+#include <cstring>
 #include <fstream>
+#include <iostream>
 
 #include "mersenne.h"
 #include "movegen.h"
@@ -676,6 +677,25 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square
 }
 
 
+/// Position::init_new_state() copies from the current state the fields
+/// that will be updated incrementally, skips the fields, like bitboards
+/// that will be recalculated form scratch anyway.
+
+void Position::init_new_state(StateInfo& newSt) {
+
+  newSt.key          = st->key;
+  newSt.pawnKey      = st->pawnKey;
+  newSt.materialKey  = st->materialKey;
+  newSt.castleRights = st->castleRights;
+  newSt.rule50       = st->rule50;
+  newSt.epSquare     = st->epSquare;
+  newSt.mgValue      = st->mgValue;
+  newSt.egValue      = st->egValue;
+  newSt.capture      = NO_PIECE_TYPE;
+  newSt.previous     = st;
+}
+
+
 /// Position::do_move() makes a move, and saves all information necessary
 /// to a StateInfo object. The move is assumed to be legal.
 /// Pseudo-legal moves should be filtered out before this function is called.
@@ -685,17 +705,14 @@ void Position::do_move(Move m, StateInfo& newSt) {
   assert(is_ok());
   assert(move_is_ok(m));
 
-  // Get now the current (pre-move) dc candidates that we will use
+  // Get now the current (before to move) dc candidates that we will use
   // in update_checkers().
   Bitboard oldDcCandidates = discovered_check_candidates(side_to_move());
 
-  // Copy the old state to our new StateInfo object (except the
-  // captured piece, which is taken care of later.
-  // TODO do not copy pinners and checkersBB because are recalculated
-  // anyway.
-  newSt = *st;
-  newSt.capture = NO_PIECE_TYPE;
-  newSt.previous = st;
+  // Copy some fields of old state to our new StateInfo object (except the
+  // captured piece, which is taken care of later) and switch state pointer
+  // to point to the new, ready to be updated, state.
+  init_new_state(newSt);
   st = &newSt;
 
   // Save the current key to the history[] array, in order to be able to
@@ -1629,16 +1646,14 @@ int Position::see(Square from, Square to) const {
 void Position::clear() {
 
   st = &startState;
-  st->previous = NULL; // We should never dereference this
+  memset(st, 0, sizeof(StateInfo));
+  st->epSquare = SQ_NONE;
+
+  memset(index, 0, sizeof(int) * 64);
+  memset(byColorBB, 0, sizeof(Bitboard) * 2);
 
   for (int i = 0; i < 64; i++)
-  {
       board[i] = EMPTY;
-      index[i] = 0;
-  }
-
-  for (int i = 0; i < 2; i++)
-      byColorBB[i] = EmptyBoardBB;
 
   for (int i = 0; i < 7; i++)
   {
@@ -1648,21 +1663,11 @@ void Position::clear() {
           pieceList[0][i][j] = pieceList[1][i][j] = SQ_NONE;
   }
 
-  st->checkersBB = EmptyBoardBB;
-  for (Color c = WHITE; c <= BLACK; c++)
-      st->pinners[c] = st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB;
-
   sideToMove = WHITE;
   gamePly = 0;
   initialKFile = FILE_E;
   initialKRFile = FILE_H;
   initialQRFile = FILE_A;
-
-  st->lastMove = MOVE_NONE;
-  st->castleRights = NO_CASTLES;
-  st->epSquare = SQ_NONE;
-  st->rule50 = 0;
-  st->previous = NULL;
 }