]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Micro-optmize castling moves
[stockfish] / src / position.cpp
index 2cc384a31e8267a525250e9f69738314c38f0e82..537674c9119c123370c44456cfb7b9ea94301451 100644 (file)
@@ -242,14 +242,27 @@ void Position::from_fen(const string& fenStr, bool isChess960) {
 /// Position::set_castle_right() is an helper function used to set castling
 /// rights given the corresponding color and the rook starting square.
 
-void Position::set_castle_right(Color c, Square rsq) {
+void Position::set_castle_right(Color c, Square rfrom) {
 
-  int f = (rsq < king_square(c) ? WHITE_OOO : WHITE_OO) << c;
+  Square kfrom = king_square(c);
+  bool kingSide = kfrom < rfrom;
+  int cr = (kingSide ? WHITE_OO : WHITE_OOO) << c;
 
-  st->castleRights |= f;
-  castleRightsMask[king_square(c)] |= f;
-  castleRightsMask[rsq] |= f;
-  castleRookSquare[f] = rsq;
+  st->castleRights |= cr;
+  castleRightsMask[kfrom] |= cr;
+  castleRightsMask[rfrom] |= cr;
+  castleRookSquare[cr] = rfrom;
+
+  Square kto = relative_square(c, kingSide ? SQ_G1 : SQ_C1);
+  Square rto = relative_square(c, kingSide ? SQ_F1 : SQ_D1);
+
+  for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); s++)
+      if (s != kfrom && s != rfrom)
+          castlePath[cr] |= s;
+
+  for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); s++)
+      if (s != kfrom && s != rfrom)
+          castlePath[cr] |= s;
 }
 
 
@@ -518,7 +531,7 @@ bool Position::is_pseudo_legal(const Move m) const {
       return move_is_legal(m);
 
   // Is not a promotion, so promotion piece must be empty
-  if (promotion_piece_type(m) - 2 != NO_PIECE_TYPE)
+  if (promotion_type(m) - 2 != NO_PIECE_TYPE)
       return false;
 
   // If the from square is not occupied by a piece belonging to the side to
@@ -656,7 +669,7 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const {
 
   // Promotion with check ?
   if (is_promotion(m))
-      return attacks_from(Piece(promotion_piece_type(m)), to, occupied_squares() ^ from) & ksq;
+      return attacks_from(Piece(promotion_type(m)), to, occupied_squares() ^ from) & ksq;
 
   // En passant capture with check ? We have already handled the case
   // of direct checks and ordinary discovered check, the only case we
@@ -850,7 +863,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
 
       if (is_promotion(m))
       {
-          PieceType promotion = promotion_piece_type(m);
+          PieceType promotion = promotion_type(m);
 
           assert(relative_rank(us, to) == RANK_8);
           assert(promotion >= KNIGHT && promotion <= QUEEN);
@@ -965,7 +978,7 @@ void Position::undo_move(Move m) {
 
   if (is_promotion(m))
   {
-      PieceType promotion = promotion_piece_type(m);
+      PieceType promotion = promotion_type(m);
 
       assert(promotion == pt);
       assert(relative_rank(us, to) == RANK_8);