Micro optimize Position::move_is_check()
authorMarco Costalba <mcostalba@gmail.com>
Mon, 27 Apr 2009 15:39:59 +0000 (17:39 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Mon, 27 Apr 2009 15:39:59 +0000 (17:39 +0200)
More then optimization it is worth a bit of better
code self documenting.

No functional change.

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

index 4ca264a..f2ba6ec 100644 (file)
@@ -74,6 +74,13 @@ inline SignedDirection signed_direction_between_squares(Square s1, Square s2) {
   return SignedDirection(SignedDirectionTable[s1][s2]);
 }
 
+inline bool direction_is_diagonal(Square s1, Square s2) {
+  return DirectionTable[s1][s2] & 2;
+}
+
+inline bool direction_is_straight(Square s1, Square s2) {
+  return DirectionTable[s1][s2] < 2;
+}
 
 ////
 //// Prototypes
index 2ede62f..5a42792 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?