]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Micro optimize and rename move_promotion()
[stockfish] / src / position.cpp
index 6e6bf38b3dda159a5703386f3d7e1fe192d693ab..64b833857b9976080db3b587715e8319f5da8e39 100644 (file)
@@ -315,9 +315,10 @@ void Position::print(Move m) const {
 
 /// Position::copy() creates a copy of the input position.
 
-void Position::copy(const Position &pos) {
+void Position::copy(const Positionpos) {
 
   memcpy(this, &pos, sizeof(Position));
+  saveState(); // detach and copy state info
 }
 
 
@@ -554,12 +555,12 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const {
           && (direction_between_squares(from, ksq) != direction_between_squares(to, ksq)))
           return true;
 
-      if (move_promotion(m)) // Promotion with check?
+      if (move_is_promotion(m)) // Promotion with check?
       {
           Bitboard b = occupied_squares();
           clear_bit(&b, from);
 
-          switch (move_promotion(m))
+          switch (move_promotion_piece(m))
           {
           case KNIGHT:
               return bit_is_set(piece_attacks<KNIGHT>(to), ksq);
@@ -720,7 +721,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
 
   if (move_is_castle(m))
       do_castle_move(m);
-  else if (move_promotion(m))
+  else if (move_is_promotion(m))
       do_promotion_move(m);
   else if (move_is_ep(m))
       do_ep_move(m);
@@ -795,11 +796,14 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
     pieceList[us][piece][index[from]] = to;
     index[to] = index[from];
 
-    // Update castle rights
-    st->key ^= zobCastle[st->castleRights];
-    st->castleRights &= castleRightsMask[from];
-    st->castleRights &= castleRightsMask[to];
-    st->key ^= zobCastle[st->castleRights];
+    // Update castle rights, try to shortcut a common case
+    if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
+    {
+        st->key ^= zobCastle[st->castleRights];
+        st->castleRights &= castleRightsMask[from];
+        st->castleRights &= castleRightsMask[to];
+        st->key ^= zobCastle[st->castleRights];
+    }
 
     // Update checkers bitboard, piece must be already moved
     st->checkersBB = EmptyBoardBB;
@@ -1001,7 +1005,7 @@ void Position::do_promotion_move(Move m) {
   board[from] = EMPTY;
 
   // Insert promoted piece
-  promotion = move_promotion(m);
+  promotion = move_promotion_piece(m);
   assert(promotion >= KNIGHT && promotion <= QUEEN);
   set_bit(&(byColorBB[us]), to);
   set_bit(&(byTypeBB[promotion]), to);
@@ -1148,7 +1152,7 @@ void Position::undo_move(Move m) {
 
   if (move_is_castle(m))
       undo_castle_move(m);
-  else if (move_promotion(m))
+  else if (move_is_promotion(m))
       undo_promotion_move(m);
   else if (move_is_ep(m))
       undo_ep_move(m);
@@ -1300,7 +1304,7 @@ void Position::undo_promotion_move(Move m) {
   assert(piece_on(from) == EMPTY);
 
   // Remove promoted piece
-  promotion = move_promotion(m);
+  promotion = move_promotion_piece(m);
   assert(piece_on(to)==piece_of_color_and_type(us, promotion));
   assert(promotion >= KNIGHT && promotion <= QUEEN);
   clear_bit(&(byColorBB[us]), to);
@@ -1593,7 +1597,7 @@ int Position::see(Square from, Square to) const {
       if (pt == KING && stmAttackers)
       {
           assert(n < 32);
-          swapList[n++] = 100;
+          swapList[n++] = QueenValueMidgame*10;
           break;
       }
   } while (stmAttackers);
@@ -1607,15 +1611,16 @@ int Position::see(Square from, Square to) const {
 }
 
 
-/// Position::setStartState() copies the content of the argument
+/// Position::saveState() copies the content of the current state
 /// inside startState and makes st point to it. This is needed
 /// when the st pointee could become stale, as example because
 /// the caller is about to going out of scope.
 
-void Position::setStartState(const StateInfo& s) {
+void Position::saveState() {
 
-  startState = s;
+  startState = *st;
   st = &startState;
+  st->previous = NULL; // as a safe guard
 }
 
 
@@ -1966,7 +1971,7 @@ void Position::init_piece_square_tables() {
 /// the white and black sides reversed. This is only useful for debugging,
 /// especially for finding evaluation symmetry bugs.
 
-void Position::flipped_copy(const Position &pos) {
+void Position::flipped_copy(const Positionpos) {
 
   assert(pos.is_ok());
 
@@ -2091,7 +2096,7 @@ bool Position::is_ok(int* failedStep) const {
 
   // Is there more than 2 checkers?
   if (failedStep) (*failedStep)++;
-  if (debugCheckerCount && count_1s<false>(st->checkersBB) > 2)
+  if (debugCheckerCount && count_1s(st->checkersBB) > 2)
       return false;
 
   // Bitboards OK?
@@ -2166,7 +2171,7 @@ bool Position::is_ok(int* failedStep) const {
   if (debugPieceCounts)
       for (Color c = WHITE; c <= BLACK; c++)
           for (PieceType pt = PAWN; pt <= KING; pt++)
-              if (pieceCount[c][pt] != count_1s<false>(pieces_of_color_and_type(c, pt)))
+              if (pieceCount[c][pt] != count_1s(pieces_of_color_and_type(c, pt)))
                   return false;
 
   if (failedStep) (*failedStep)++;