]> git.sesse.net Git - stockfish/commitdiff
Update pinned bitboards and friends in do_move()
authorMarco Costalba <mcostalba@gmail.com>
Mon, 23 Feb 2009 10:37:31 +0000 (11:37 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Mon, 23 Feb 2009 20:44:17 +0000 (21:44 +0100)
Probably is slightly slow, but code is surely better
in this way. We will optimize later for speed.

No functional change.

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

index 91fbd103a739ccfd84b5fec8717ec90b0e1b4858..0a7ebadda7f3b5614668781f715f6c067cc7ad3f 100644 (file)
@@ -206,6 +206,7 @@ void Position::from_fen(const std::string& fen) {
   castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO;
 
   find_checkers();
   castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO;
 
   find_checkers();
+  find_pinned();
 
   st->key = compute_key();
   st->pawnKey = compute_pawn_key();
 
   st->key = compute_key();
   st->pawnKey = compute_pawn_key();
@@ -319,44 +320,11 @@ void Position::copy(const Position &pos) {
 }
 
 
 }
 
 
-/// Position:pinned_pieces() returns a bitboard of all pinned (against the
-/// king) pieces for the given color.
-Bitboard Position::pinned_pieces(Color c) const {
-
-  if (st->pinned[c] != ~EmptyBoardBB)
-      return st->pinned[c];
-
-  Bitboard p1, p2;
-  Square ksq = king_square(c);
-  st->pinned[c] = hidden_checks<ROOK, true>(c, ksq, p1) | hidden_checks<BISHOP, true>(c, ksq, p2);
-  st->pinners[c] = p1 | p2;
-  return st->pinned[c];
-}
-
-Bitboard Position::pinned_pieces(Color c, Bitboard& p) const {
-
-  if (st->pinned[c] == ~EmptyBoardBB)
-      pinned_pieces(c);
-
-  p = st->pinners[c];
-  return st->pinned[c];
-}
-
-Bitboard Position::discovered_check_candidates(Color c) const {
-
-  if (st->dcCandidates[c] != ~EmptyBoardBB)
-      return st->dcCandidates[c];
-
-  Bitboard dummy;
-  Square ksq = king_square(opposite_color(c));
-  st->dcCandidates[c] = hidden_checks<ROOK, false>(c, ksq, dummy) | hidden_checks<BISHOP, false>(c, ksq, dummy);
-  return st->dcCandidates[c];
-}
-
 /// Position:hidden_checks<>() 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 pinned pieces of opposite color
 /// that are, indeed, the pieces candidate for a discovery check.
 /// Position:hidden_checks<>() 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 pinned pieces of opposite color
 /// that are, indeed, the pieces candidate for a discovery check.
+/// Note that checkersBB bitboard must be already updated.
 template<PieceType Piece, bool FindPinned>
 Bitboard Position::hidden_checks(Color c, Square ksq, Bitboard& pinners) const {
 
 template<PieceType Piece, bool FindPinned>
 Bitboard Position::hidden_checks(Color c, Square ksq, Bitboard& pinners) const {
 
@@ -466,7 +434,7 @@ bool Position::move_attacks_square(Move m, Square s) const {
 
 
 /// Position::find_checkers() computes the checkersBB bitboard, which
 
 
 /// Position::find_checkers() computes the checkersBB bitboard, which
-/// contains a nonzero bit for each checking piece (0, 1 or 2).  It
+/// contains a nonzero bit for each checking piece (0, 1 or 2). It
 /// currently works by calling Position::attacks_to, which is probably
 /// inefficient. Consider rewriting this function to use the last move
 /// played, like in non-bitboard versions of Glaurung.
 /// currently works by calling Position::attacks_to, which is probably
 /// inefficient. Consider rewriting this function to use the last move
 /// played, like in non-bitboard versions of Glaurung.
@@ -478,6 +446,25 @@ void Position::find_checkers() {
 }
 
 
 }
 
 
+/// Position:find_pinned() computes the pinned, pinners and dcCandidates
+/// bitboards for both colors. Bitboard checkersBB must be already updated.
+
+void Position::find_pinned() {
+
+  Bitboard p1, p2;
+  Square ksq;
+
+  for (Color c = WHITE; c <= BLACK; c++)
+  {
+      ksq = king_square(c);
+      st->pinned[c] = hidden_checks<ROOK, true>(c, ksq, p1) | hidden_checks<BISHOP, true>(c, ksq, p2);
+      st->pinners[c] = p1 | p2;
+      ksq = king_square(opposite_color(c));
+      st->dcCandidates[c] = hidden_checks<ROOK, false>(c, ksq, p1) | hidden_checks<BISHOP, false>(c, ksq, p2);
+  }
+}
+
+
 /// Position::pl_move_is_legal() tests whether a pseudo-legal move is legal
 
 bool Position::pl_move_is_legal(Move m) const {
 /// Position::pl_move_is_legal() tests whether a pseudo-legal move is legal
 
 bool Position::pl_move_is_legal(Move m) const {
@@ -719,10 +706,6 @@ void Position::do_move(Move m, StateInfo& newSt) {
   // case of non-reversible moves is taken care of later.
   st->rule50++;
 
   // case of non-reversible moves is taken care of later.
   st->rule50++;
 
-  // Reset pinned bitboard and its friends
-  for (Color c = WHITE; c <= BLACK; c++)
-      st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB;
-
   if (move_is_castle(m))
       do_castle_move(m);
   else if (move_promotion(m))
   if (move_is_castle(m))
       do_castle_move(m);
   else if (move_promotion(m))
@@ -823,6 +806,7 @@ void Position::do_move(Move m, StateInfo& newSt) {
   }
 
   // Finish
   }
 
   // Finish
+  find_pinned();
   st->key ^= zobSideToMove;
   sideToMove = opposite_color(sideToMove);
   gamePly++;
   st->key ^= zobSideToMove;
   sideToMove = opposite_color(sideToMove);
   gamePly++;
index 8d1ba3598c2da91c9252bbdf39efa3be3fea3fda..981bad3e846f31cb8459efd82f5884ba5d17aa62 100644 (file)
@@ -302,6 +302,7 @@ private:
   void undo_promotion_move(Move m);
   void undo_ep_move(Move m);
   void find_checkers();
   void undo_promotion_move(Move m);
   void undo_ep_move(Move m);
   void find_checkers();
+  void find_pinned();
 
   template<PieceType Piece>
   void update_checkers(Bitboard* pCheckersBB, Square ksq, Square from, Square to, Bitboard dcCandidates);
 
   template<PieceType Piece>
   void update_checkers(Bitboard* pCheckersBB, Square ksq, Square from, Square to, Bitboard dcCandidates);
@@ -556,6 +557,19 @@ inline Bitboard Position::piece_attacks<KING>(Square s) const {
   return StepAttackBB[KING][s];
 }
 
   return StepAttackBB[KING][s];
 }
 
+inline Bitboard Position::pinned_pieces(Color c) const {
+  return st->pinned[c];
+}
+
+inline Bitboard Position::pinned_pieces(Color c, Bitboard& p) const {
+  p = st->pinners[c];
+  return st->pinned[c];
+}
+
+inline Bitboard Position::discovered_check_candidates(Color c) const {
+  return st->dcCandidates[c];
+}
+
 inline Bitboard Position::checkers() const {
   return st->checkersBB;
 }
 inline Bitboard Position::checkers() const {
   return st->checkersBB;
 }