]> git.sesse.net Git - stockfish/commitdiff
Small tweak to generate_castle_moves()
authorMarco Costalba <mcostalba@gmail.com>
Fri, 7 Jan 2011 14:17:28 +0000 (15:17 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 7 Jan 2011 15:57:14 +0000 (16:57 +0100)
Move the castling condition test out of the
function. This avoids a function call most of
the times.

No functional change.

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

index b130247bea5f151cd810ec3e73ddd8b1a1204a83..5f7ffb3564e039cd8f25d9902d0e172c7cdf3b08 100644 (file)
@@ -46,7 +46,7 @@ namespace {
   };
 
   template<CastlingSide Side>
-  MoveStack* generate_castle_moves(const Position&, MoveStack*);
+  MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
 
   template<Color Us, MoveType Type>
   MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
@@ -193,8 +193,11 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
 
   if (T != MV_CAPTURE)
   {
-      mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
-      mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
+      if (pos.can_castle_kingside(us))
+          mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
+
+      if (pos.can_castle_queenside(us))
+          mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
   }
 
   return mlist;
@@ -622,45 +625,41 @@ namespace {
   }
 
   template<CastlingSide Side>
-  MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
+  MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
 
-    Color us = pos.side_to_move();
+    Color them = opposite_color(us);
+    Square ksq = pos.king_square(us);
 
-    if (  (Side == KING_SIDE  && pos.can_castle_kingside(us))
-        ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
-    {
-        Color them = opposite_color(us);
-        Square ksq = pos.king_square(us);
+    assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
 
-        assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
+    Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
+    Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
+    Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
+    Square s;
+    bool illegal = false;
 
-        Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
-        Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
-        Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
-        Square s;
-        bool illegal = false;
+    assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
 
-        assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
+    // It is a bit complicated to correctly handle Chess960
+    for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
+        if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
+            ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
+            illegal = true;
 
-        // It is a bit complicated to correctly handle Chess960
-        for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
-            if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
-                ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
-                illegal = true;
+    for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
+        if (s != ksq && s != rsq && pos.square_is_occupied(s))
+            illegal = true;
 
-        for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
-            if (s != ksq && s != rsq && pos.square_is_occupied(s))
-                illegal = true;
+    if (   Side == QUEEN_SIDE
+        && square_file(rsq) == FILE_B
+        && (   pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
+            || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
+        illegal = true;
 
-        if (   Side == QUEEN_SIDE
-            && square_file(rsq) == FILE_B
-            && (   pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
-                || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
-            illegal = true;
+    if (!illegal)
+        (*mlist++).move = make_castle_move(ksq, rsq);
 
-        if (!illegal)
-            (*mlist++).move = make_castle_move(ksq, rsq);
-    }
     return mlist;
   }
-}
+
+} // namespace