]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Enable link time optimization only when optimizing
[stockfish] / src / movegen.cpp
index 2c71c680e9f857485a931a350e3bf69ed9afd9c2..ced938e8c2b9e835ef3b44e16088a7d2f4a31a2c 100644 (file)
 
 /// Simple macro to wrap a very common while loop, no facny, no flexibility,
 /// hardcoded names 'mlist' and 'from'.
-#define SERIALIZE(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
+#define SERIALIZE(b) while (b) (*mlist++).move = make_move(from, pop_lsb(&b))
 
 /// Version used for pawns, where the 'from' square is given as a delta from the 'to' square
-#define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_1st_bit(&b); \
+#define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_lsb(&b); \
                                          (*mlist++).move = make_move(to - (d), to); }
 namespace {
 
@@ -59,7 +59,7 @@ namespace {
         && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
             return mlist;
 
-    (*mlist++).move = make_castle(kfrom, rfrom);
+    (*mlist++).move = make<CASTLE>(kfrom, rfrom);
 
     if (OnlyChecks && !pos.move_gives_check((mlist - 1)->move, CheckInfo(pos)))
         mlist--;
@@ -87,22 +87,22 @@ namespace {
 
     while (b)
     {
-        Square to = pop_1st_bit(&b);
+        Square to = pop_lsb(&b);
 
         if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
-            (*mlist++).move = make_promotion(to - Delta, to, QUEEN);
+            (*mlist++).move = make<PROMOTION>(to - Delta, to, QUEEN);
 
         if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
         {
-            (*mlist++).move = make_promotion(to - Delta, to, ROOK);
-            (*mlist++).move = make_promotion(to - Delta, to, BISHOP);
-            (*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
+            (*mlist++).move = make<PROMOTION>(to - Delta, to, ROOK);
+            (*mlist++).move = make<PROMOTION>(to - Delta, to, BISHOP);
+            (*mlist++).move = make<PROMOTION>(to - Delta, to, KNIGHT);
         }
 
         // Knight-promotion is the only one that can give a direct check not
         // already included in the queen-promotion.
         if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ksq))
-            (*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
+            (*mlist++).move = make<PROMOTION>(to - Delta, to, KNIGHT);
         else
             (void)ksq; // Silence a warning under MSVC
     }
@@ -207,7 +207,7 @@ namespace {
             assert(b1);
 
             while (b1)
-                (*mlist++).move = make_enpassant(pop_1st_bit(&b1), pos.ep_square());
+                (*mlist++).move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
         }
     }
 
@@ -215,61 +215,40 @@ namespace {
   }
 
 
-  template<PieceType Pt>
-  inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist,
-                                           Color us, const CheckInfo& ci) {
+  template<PieceType Pt, bool OnlyChecks> FORCE_INLINE
+  MoveStack* generate_moves(const Position& pos, MoveStack* mlist, Color us,
+                            Bitboard target, const CheckInfo* ci = NULL) {
+
     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
-
-        do {
-            from = *pl;
-
+        if (OnlyChecks)
+        {
             if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
-                && !(PseudoAttacks[Pt][from] & target))
+                && !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
                 continue;
 
-            if (ci.dcCandidates && (ci.dcCandidates & from))
+            if (ci->dcCandidates && (ci->dcCandidates & from))
                 continue;
+        }
 
-            b = pos.attacks_from<Pt>(from) & target;
-            SERIALIZE(b);
-        } while (*++pl != SQ_NONE);
-    }
-
-    return mlist;
-  }
-
+        Bitboard b = pos.attacks_from<Pt>(from) & target;
 
-  template<PieceType Pt>
-  FORCE_INLINE MoveStack* generate_moves(const Position& pos, MoveStack* mlist,
-                                         Color us, Bitboard target) {
-    assert(Pt != KING && Pt != PAWN);
+        if (OnlyChecks)
+            b &= ci->checkSq[Pt];
 
-    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);
+        SERIALIZE(b);
+    }
 
     return mlist;
   }
 
-
-  template<>
-  FORCE_INLINE MoveStack* generate_moves<KING>(const Position& pos, MoveStack* mlist,
-                                               Color us, Bitboard target) {
+  template<> FORCE_INLINE
+  MoveStack* generate_moves<KING, false>(const Position& pos, MoveStack* mlist, Color us,
+                                         Bitboard target, const CheckInfo*) {
     Square from = pos.king_square(us);
     Bitboard b = pos.attacks_from<KING>(from) & target;
     SERIALIZE(b);
@@ -309,15 +288,15 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
   mlist = (us == WHITE ? generate_pawn_moves<WHITE, Type>(pos, mlist, target)
                        : generate_pawn_moves<BLACK, Type>(pos, mlist, target));
 
-  mlist = generate_moves<KNIGHT>(pos, mlist, us, target);
-  mlist = generate_moves<BISHOP>(pos, mlist, us, target);
-  mlist = generate_moves<ROOK>(pos, mlist, us, target);
-  mlist = generate_moves<QUEEN>(pos, mlist, us, target);
-  mlist = generate_moves<KING>(pos, mlist, us, target);
+  mlist = generate_moves<KNIGHT, false>(pos, mlist, us, target);
+  mlist = generate_moves<BISHOP, false>(pos, mlist, us, target);
+  mlist = generate_moves<ROOK,   false>(pos, mlist, us, target);
+  mlist = generate_moves<QUEEN,  false>(pos, mlist, us, target);
+  mlist = generate_moves<KING,   false>(pos, mlist, us, target);
 
   if (Type != CAPTURES && pos.can_castle(us))
   {
-      mlist = generate_castle<KING_SIDE, false>(pos, mlist, us);
+      mlist = generate_castle<KING_SIDE,  false>(pos, mlist, us);
       mlist = generate_castle<QUEEN_SIDE, false>(pos, mlist, us);
   }
 
@@ -339,11 +318,12 @@ MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
 
   Color us = pos.side_to_move();
   CheckInfo ci(pos);
+  Bitboard empty = ~pos.pieces();
   Bitboard dc = ci.dcCandidates;
 
   while (dc)
   {
-     Square from = pop_1st_bit(&dc);
+     Square from = pop_lsb(&dc);
      PieceType pt = type_of(pos.piece_on(from));
 
      if (pt == PAWN)
@@ -360,14 +340,14 @@ MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
   mlist = (us == WHITE ? generate_pawn_moves<WHITE, QUIET_CHECKS>(pos, mlist, ci.dcCandidates, ci.ksq)
                        : generate_pawn_moves<BLACK, QUIET_CHECKS>(pos, mlist, ci.dcCandidates, ci.ksq));
 
-  mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, ci);
-  mlist = generate_direct_checks<BISHOP>(pos, mlist, us, ci);
-  mlist = generate_direct_checks<ROOK>(pos, mlist, us, ci);
-  mlist = generate_direct_checks<QUEEN>(pos, mlist, us, ci);
+  mlist = generate_moves<KNIGHT, true>(pos, mlist, us, empty, &ci);
+  mlist = generate_moves<BISHOP, true>(pos, mlist, us, empty, &ci);
+  mlist = generate_moves<ROOK,   true>(pos, mlist, us, empty, &ci);
+  mlist = generate_moves<QUEEN,  true>(pos, mlist, us, empty, &ci);
 
   if (pos.can_castle(us))
   {
-      mlist = generate_castle<KING_SIDE, true>(pos, mlist, us);
+      mlist = generate_castle<KING_SIDE,  true>(pos, mlist, us);
       mlist = generate_castle<QUEEN_SIDE, true>(pos, mlist, us);
   }
 
@@ -398,7 +378,7 @@ MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
   do
   {
       checkersCnt++;
-      checksq = pop_1st_bit(&b);
+      checksq = pop_lsb(&b);
 
       assert(color_of(pos.piece_on(checksq)) == ~us);
 
@@ -439,10 +419,10 @@ MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
   mlist = (us == WHITE ? generate_pawn_moves<WHITE, EVASIONS>(pos, mlist, target)
                        : generate_pawn_moves<BLACK, EVASIONS>(pos, mlist, target));
 
-  mlist = generate_moves<KNIGHT>(pos, mlist, us, target);
-  mlist = generate_moves<BISHOP>(pos, mlist, us, target);
-  mlist = generate_moves<ROOK>(pos, mlist, us, target);
-  return  generate_moves<QUEEN>(pos, mlist, us, target);
+  mlist = generate_moves<KNIGHT, false>(pos, mlist, us, target);
+  mlist = generate_moves<BISHOP, false>(pos, mlist, us, target);
+  mlist = generate_moves<ROOK,   false>(pos, mlist, us, target);
+  return  generate_moves<QUEEN,  false>(pos, mlist, us, target);
 }