]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Micro optimize Position::move_is_check()
[stockfish] / src / position.cpp
index 3e72c0e9c88c1185f6eb1a03e44fa60cd2a1a160..5a42792eb8a97bc105883a0436cf6cc42f903de5 100644 (file)
@@ -599,19 +599,17 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const {
 
   case BISHOP:
     return   (dcCandidates && bit_is_set(dcCandidates, from))
-          || (   direction_between_squares(ksq, to) != DIR_NONE
-              && bit_is_set(piece_attacks<BISHOP>(ksq), to));
+          || (direction_is_diagonal(ksq, to) && bit_is_set(piece_attacks<BISHOP>(ksq), to));
 
   case ROOK:
     return   (dcCandidates && bit_is_set(dcCandidates, from))
-          || (   direction_between_squares(ksq, to) != DIR_NONE
-              && bit_is_set(piece_attacks<ROOK>(ksq), to));
+          || (direction_is_straight(ksq, to) && bit_is_set(piece_attacks<ROOK>(ksq), to));
 
   case QUEEN:
       // Discovered checks are impossible!
       assert(!bit_is_set(dcCandidates, from));
-      return (   direction_between_squares(ksq, to) != DIR_NONE
-              && bit_is_set(piece_attacks<QUEEN>(ksq), to));
+      return (   (direction_is_straight(ksq, to) && bit_is_set(piece_attacks<ROOK>(ksq), to))
+              || (direction_is_diagonal(ksq, to) && bit_is_set(piece_attacks<BISHOP>(ksq), to)));
 
   case KING:
       // Discovered check?
@@ -651,20 +649,6 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const {
 }
 
 
-/// Position::move_is_capture() tests whether a move from the current
-/// position is a capture. Move must not be MOVE_NONE.
-
-bool Position::move_is_capture(Move m) const {
-
-  assert(m != MOVE_NONE);
-
-  return (   !square_is_empty(move_to(m))
-          && (color_of_piece_on(move_to(m)) != color_of_piece_on(move_from(m)))
-         )
-         || move_is_ep(m);
-}
-
-
 /// Position::update_checkers() udpates chekers info given the move. It is called
 /// in do_move() and is faster then find_checkers().
 
@@ -1903,20 +1887,13 @@ bool Position::is_mate() const {
 
 
 /// Position::has_mate_threat() tests whether a given color has a mate in one
-/// from the current position. This function is quite slow, but it doesn't
-/// matter, because it is currently only called from PV nodes, which are rare.
+/// from the current position.
 
 bool Position::has_mate_threat(Color c) {
 
   StateInfo st1, st2;
   Color stm = side_to_move();
 
-  // The following lines are useless and silly, but prevents gcc from
-  // emitting a stupid warning stating that u1.lastMove and u1.epSquare might
-  // be used uninitialized.
-  st1.lastMove = st->lastMove;
-  st1.epSquare = st->epSquare;
-
   if (is_check())
       return false;
 
@@ -1927,18 +1904,26 @@ bool Position::has_mate_threat(Color c) {
   MoveStack mlist[120];
   int count;
   bool result = false;
+  Bitboard dc = discovered_check_candidates(sideToMove);
+  Bitboard pinned = pinned_pieces(sideToMove);
 
-  // Generate legal moves
-  count = generate_legal_moves(*this, mlist);
+  // Generate pseudo-legal non-capture and capture check moves
+  count = generate_non_capture_checks(*this, mlist, dc);
+  count += generate_captures(*this, mlist + count);
 
   // Loop through the moves, and see if one of them is mate
   for (int i = 0; i < count; i++)
   {
-      do_move(mlist[i].move, st2);
+      Move move = mlist[i].move;
+
+      if (!pl_move_is_legal(move, pinned))
+          continue;
+
+      do_move(move, st2);
       if (is_mate())
           result = true;
 
-      undo_move(mlist[i].move);
+      undo_move(move);
   }
 
   // Undo null move, if necessary