Streamline generate_moves()
authorMarco Costalba <mcostalba@gmail.com>
Wed, 22 Aug 2012 07:29:49 +0000 (08:29 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Wed, 22 Aug 2012 08:23:03 +0000 (09:23 +0100)
Greatly simplify these very performace critical functions.
Amazingly we don't have any speed regression actually under
MSVC we have the same assembly for generate_moves() !

In generate_direct_checks() 'target' is calculated only
once being a loop invariant.

On Clang there is even a slight speed up.

No functional change.

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

index c275109d4bc0e967f9ca55b4b0b9d8d629931640..dcd7e60b6abf6c9c65835a501beee7f2655dee58 100644 (file)
@@ -216,31 +216,25 @@ namespace {
 
 
   template<PieceType Pt>
-  inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist,
-                                           Color us, const CheckInfo& ci) {
+  FORCE_INLINE MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist,
+                                                 Color us, const CheckInfo& ci) {
     assert(Pt != KING && Pt != PAWN);
 
-    Bitboard b, target;
-    Square from;
     const Square* pl = pos.piece_list(us, Pt);
 
-    if (*pl != SQ_NONE)
+    for (Square from = *pl; from != SQ_NONE; from = *++pl)
     {
-        target = ci.checkSq[Pt] & ~pos.pieces(); // Non capture checks only
+        Bitboard target = ci.checkSq[Pt] & ~pos.pieces(); // Non capture checks only
 
-        do {
-            from = *pl;
+        if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
+            && !(PseudoAttacks[Pt][from] & target))
+            continue;
 
-            if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
-                && !(PseudoAttacks[Pt][from] & target))
-                continue;
+        if (ci.dcCandidates && (ci.dcCandidates & from))
+            continue;
 
-            if (ci.dcCandidates && (ci.dcCandidates & from))
-                continue;
-
-            b = pos.attacks_from<Pt>(from) & target;
-            SERIALIZE(b);
-        } while (*++pl != SQ_NONE);
+        Bitboard b = pos.attacks_from<Pt>(from) & target;
+        SERIALIZE(b);
     }
 
     return mlist;
@@ -252,16 +246,13 @@ namespace {
                                          Color us, Bitboard target) {
     assert(Pt != KING && Pt != PAWN);
 
-    Bitboard b;
-    Square from;
     const Square* pl = pos.piece_list(us, Pt);
 
-    if (*pl != SQ_NONE)
-        do {
-            from = *pl;
-            b = pos.attacks_from<Pt>(from) & target;
-            SERIALIZE(b);
-        } while (*++pl != SQ_NONE);
+    for (Square from = *pl; from != SQ_NONE; from = *++pl)
+    {
+        Bitboard b = pos.attacks_from<Pt>(from) & target;
+        SERIALIZE(b);
+    }
 
     return mlist;
   }