]> git.sesse.net Git - stockfish/commitdiff
Micro-optimize castling handling in do_move()
authorMarco Costalba <mcostalba@gmail.com>
Sat, 11 Jun 2011 11:41:20 +0000 (12:41 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 11 Jun 2011 14:31:39 +0000 (15:31 +0100)
And better self-document the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/move.cpp
src/move.h
src/position.cpp
src/position.h

index 4a0a43b30fca539da6fb0d7db0e065904d8ab020..0a620781ce1be0a85eb9929bbf60691b504a7f42 100644 (file)
@@ -59,7 +59,7 @@ const string move_to_uci(Move m, bool chess960) {
       return from == SQ_E1 ? "e1c1" : "e8c8";
 
   if (move_is_promotion(m))
-      promotion = char(tolower(piece_type_to_char(move_promotion_piece(m))));
+      promotion = char(tolower(piece_type_to_char(promotion_piece_type(m))));
 
   return square_to_string(from) + square_to_string(to) + promotion;
 }
@@ -157,7 +157,7 @@ const string move_to_san(Position& pos, Move m) {
       if (move_is_promotion(m))
       {
           san += '=';
-          san += piece_type_to_char(move_promotion_piece(m));
+          san += piece_type_to_char(promotion_piece_type(m));
       }
   }
 
index 8c5ce22acc33154d1d0986a78f145c42365c68bf..42d3bbe9750d31e55993911d6c72f704d255d12f 100644 (file)
@@ -161,7 +161,7 @@ inline bool move_is_long_castle(Move m) {
   return move_is_castle(m) && (move_to(m) < move_from(m));
 }
 
-inline PieceType move_promotion_piece(Move m) {
+inline PieceType promotion_piece_type(Move m) {
   return PieceType(((m >> 12) & 3) + 2);
 }
 
index ef90913cdfa9ed99c4d69a8227e9aafe45154cda..8f70b5dfd3ab3af4c644072396622c75257ce381 100644 (file)
@@ -270,7 +270,7 @@ bool Position::set_castling_rights(char token) {
         for (Square sq = sqH; sq >= sqA; sq--)
             if (piece_on(sq) == rook)
             {
-                do_allow_oo(c);
+                set_castle_kingside(c);
                 initialKRFile = square_file(sq);
                 break;
             }
@@ -280,7 +280,7 @@ bool Position::set_castling_rights(char token) {
         for (Square sq = sqA; sq <= sqH; sq++)
             if (piece_on(sq) == rook)
             {
-                do_allow_ooo(c);
+                set_castle_queenside(c);
                 initialQRFile = square_file(sq);
                 break;
             }
@@ -290,12 +290,12 @@ bool Position::set_castling_rights(char token) {
         File rookFile = File(token - 'A') + FILE_A;
         if (rookFile < initialKFile)
         {
-            do_allow_ooo(c);
+            set_castle_queenside(c);
             initialQRFile = rookFile;
         }
         else
         {
-            do_allow_oo(c);
+            set_castle_kingside(c);
             initialKRFile = rookFile;
         }
     }
@@ -637,7 +637,7 @@ bool Position::move_is_pl(const Move m) const {
       return move_is_pl_slow(m);
 
   // Is not a promotion, so promotion piece must be empty
-  if (move_promotion_piece(m) - 2 != PIECE_TYPE_NONE)
+  if (promotion_piece_type(m) - 2 != PIECE_TYPE_NONE)
       return false;
 
   // If the from square is not occupied by a piece belonging to the side to
@@ -788,7 +788,7 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const {
   {
       clear_bit(&b, from);
 
-      switch (move_promotion_piece(m))
+      switch (promotion_piece_type(m))
       {
       case KNIGHT:
           return bit_is_set(attacks_from<KNIGHT>(to), ci.ksq);
@@ -949,13 +949,12 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
       st->epSquare = SQ_NONE;
   }
 
-  // Update castle rights, try to shortcut a common case
-  int cm = castleRightsMask[from] & castleRightsMask[to];
-  if (cm != ALL_CASTLES && ((cm & st->castleRights) != st->castleRights))
+  // Update castle rights if needed
+  if (    st->castleRights != CASTLES_NONE
+      && (castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
   {
       key ^= zobCastle[st->castleRights];
-      st->castleRights &= castleRightsMask[from];
-      st->castleRights &= castleRightsMask[to];
+      st->castleRights &= castleRightsMask[from] & castleRightsMask[to];
       key ^= zobCastle[st->castleRights];
   }
 
@@ -998,7 +997,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
 
       if (pm) // promotion ?
       {
-          PieceType promotion = move_promotion_piece(m);
+          PieceType promotion = promotion_piece_type(m);
 
           assert(promotion >= KNIGHT && promotion <= QUEEN);
 
@@ -1278,7 +1277,7 @@ void Position::undo_move(Move m) {
 
   if (pm) // promotion ?
   {
-      PieceType promotion = move_promotion_piece(m);
+      PieceType promotion = promotion_piece_type(m);
       pt = PAWN;
 
       assert(promotion >= KNIGHT && promotion <= QUEEN);
@@ -1851,10 +1850,10 @@ void Position::flip() {
   sideToMove = opposite_color(pos.side_to_move());
 
   // Castling rights
-  if (pos.can_castle_kingside(WHITE))  do_allow_oo(BLACK);
-  if (pos.can_castle_queenside(WHITE)) do_allow_ooo(BLACK);
-  if (pos.can_castle_kingside(BLACK))  do_allow_oo(WHITE);
-  if (pos.can_castle_queenside(BLACK)) do_allow_ooo(WHITE);
+  if (pos.can_castle_kingside(WHITE))  set_castle_kingside(BLACK);
+  if (pos.can_castle_queenside(WHITE)) set_castle_queenside(BLACK);
+  if (pos.can_castle_kingside(BLACK))  set_castle_kingside(WHITE);
+  if (pos.can_castle_queenside(BLACK)) set_castle_queenside(WHITE);
 
   initialKFile  = pos.initialKFile;
   initialKRFile = pos.initialKRFile;
index 6c48b54c341676f98a58243a7f42869e2a69e74b..56f5546936650d751db0e14f081120ac49b6b9e2 100644 (file)
@@ -257,8 +257,8 @@ private:
   void clear();
   void detach();
   void put_piece(Piece p, Square s);
-  void do_allow_oo(Color c);
-  void do_allow_ooo(Color c);
+  void set_castle_kingside(Color c);
+  void set_castle_queenside(Color c);
   bool set_castling_rights(char token);
   bool move_is_pl_slow(const Move m) const;
 
@@ -406,16 +406,24 @@ inline Square Position::king_square(Color c) const {
   return pieceList[c][KING][0];
 }
 
-inline bool Position::can_castle_kingside(Color side) const {
-  return st->castleRights & (1+int(side));
+inline bool Position::can_castle_kingside(Color c) const {
+  return st->castleRights & (WHITE_OO << c);
 }
 
-inline bool Position::can_castle_queenside(Color side) const {
-  return st->castleRights & (4+4*int(side));
+inline bool Position::can_castle_queenside(Color c) const {
+  return st->castleRights & (WHITE_OOO << c);
 }
 
-inline bool Position::can_castle(Color side) const {
-  return can_castle_kingside(side) || can_castle_queenside(side);
+inline bool Position::can_castle(Color c) const {
+  return st->castleRights & ((WHITE_OO | WHITE_OOO) << c);
+}
+
+inline void Position::set_castle_kingside(Color c) {
+  st->castleRights |= (WHITE_OO << c);
+}
+
+inline void Position::set_castle_queenside(Color c) {
+  st->castleRights |= (WHITE_OOO << c);
 }
 
 inline Square Position::initial_kr_square(Color c) const {
@@ -542,12 +550,4 @@ inline int Position::thread() const {
   return threadID;
 }
 
-inline void Position::do_allow_oo(Color c) {
-  st->castleRights |= (1 + int(c));
-}
-
-inline void Position::do_allow_ooo(Color c) {
-  st->castleRights |= (4 + 4*int(c));
-}
-
 #endif // !defined(POSITION_H_INCLUDED)