]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Simplify and micro-optimize hidden_checkers()
[stockfish] / src / position.cpp
index 281109b19a34a8ba6d3f6602115d7b61d4cbb43a..43de667841764ee5fcf4bfe58e46955856a3d2d4 100644 (file)
@@ -361,36 +361,28 @@ void Position::print(Move move) const {
 
 
 /// Position:hidden_checkers<>() returns a bitboard of all pinned (against the
-/// king) pieces for the given color and for the given pinner type. Or, when
-/// template parameter FindPinned is false, the pieces of the given color
-/// candidate for a discovery check against the enemy king.
-/// Bitboard checkersBB must be already updated when looking for pinners.
+/// king) pieces for the given color. Or, when template parameter FindPinned is
+/// false, the function return the pieces of the given color candidate for a
+/// discovery check against the enemy king.
 
 template<bool FindPinned>
 Bitboard Position::hidden_checkers(Color c) const {
 
-  Bitboard result = EmptyBoardBB;
+  // Pinned pieces protect our king, dicovery checks attack the enemy king
+  Bitboard b, result = EmptyBoardBB;
   Bitboard pinners = pieces(FindPinned ? opposite_color(c) : c);
-
-  // Pinned pieces protect our king, dicovery checks attack
-  // the enemy king.
   Square ksq = king_square(FindPinned ? c : opposite_color(c));
 
-  // Pinners are sliders, not checkers, that give check when candidate pinned is removed
-  pinners &= (pieces(ROOK, QUEEN) & RookPseudoAttacks[ksq]) | (pieces(BISHOP, QUEEN) & BishopPseudoAttacks[ksq]);
-
-  if (FindPinned && pinners)
-      pinners &= ~st->checkersBB;
+  // Pinners are sliders, that give check when candidate pinned is removed
+  pinners &=  (pieces(ROOK, QUEEN) & RookPseudoAttacks[ksq])
+            | (pieces(BISHOP, QUEEN) & BishopPseudoAttacks[ksq]);
 
   while (pinners)
   {
-      Square s = pop_1st_bit(&pinners);
-      Bitboard b = squares_between(s, ksq) & occupied_squares();
+      b = squares_between(ksq, pop_1st_bit(&pinners)) & occupied_squares();
 
-      assert(b);
-
-      if (  !(b & (b - 1)) // Only one bit set?
-          && (b & pieces(c))) // Is an our piece?
+      // Only one bit set and is an our piece?
+      if (b && !(b & (b - 1)) && (b & pieces(c)))
           result |= b;
   }
   return result;
@@ -513,14 +505,14 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
   assert(piece_color(piece_on(from)) == us);
   assert(piece_on(king_square(us)) == make_piece(us, KING));
 
-  // En passant captures are a tricky special case. Because they are
-  // rather uncommon, we do it simply by testing whether the king is attacked
-  // after the move is made
+  // En passant captures are a tricky special case. Because they are rather
+  // uncommon, we do it simply by testing whether the king is attacked after
+  // the move is made.
   if (move_is_ep(m))
   {
       Color them = opposite_color(us);
       Square to = move_to(m);
-      Square capsq = make_square(square_file(to), square_rank(from));
+      Square capsq = to + pawn_push(them);
       Square ksq = king_square(us);
       Bitboard b = occupied_squares();
 
@@ -930,7 +922,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
       // Set en passant square, only if moved pawn can be captured
       if ((to ^ from) == 16)
       {
-          if (attacks_from<PAWN>(from + (us == WHITE ? DELTA_N : DELTA_S), us) & pieces(PAWN, them))
+          if (attacks_from<PAWN>(from + pawn_push(us), us) & pieces(PAWN, them))
           {
               st->epSquare = Square((int(from) + int(to)) / 2);
               key ^= zobEp[st->epSquare];
@@ -1039,7 +1031,7 @@ void Position::do_capture_move(Key& key, PieceType capture, Color them, Square t
     {
         if (ep) // en passant ?
         {
-            capsq = (them == BLACK)? (to - DELTA_N) : (to - DELTA_S);
+            capsq = to + pawn_push(them);
 
             assert(to == st->epSquare);
             assert(relative_rank(opposite_color(them), to) == RANK_6);
@@ -1262,7 +1254,7 @@ void Position::undo_move(Move m) {
       Square capsq = to;
 
       if (ep)
-          capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
+          capsq = to - pawn_push(us);
 
       assert(st->capturedType != KING);
       assert(!ep || square_is_empty(capsq));
@@ -1467,7 +1459,7 @@ int Position::see(Move m) const {
   // Handle en passant moves
   if (st->epSquare == to && piece_type(piece_on(from)) == PAWN)
   {
-      Square capQq = (side_to_move() == WHITE ? to - DELTA_N : to - DELTA_S);
+      Square capQq = to - pawn_push(side_to_move());
 
       assert(capturedType == PIECE_TYPE_NONE);
       assert(piece_type(piece_on(capQq)) == PAWN);