]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Micro optimize generate_piece_checks() take 2
[stockfish] / src / movegen.cpp
index a002e19fe98c92b64f29e547143633ba619e1416..dff9e8c3b4624056f0642b29e152ffac3e9e12eb 100644 (file)
@@ -64,6 +64,19 @@ namespace {
   template<Color, Color, Bitboard, Bitboard, SquareDelta>
   MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
 
+  template<Color Us, SquareDelta Direction>
+  inline Bitboard move_pawns(Bitboard p) {
+
+    if (Direction == DELTA_N)
+        return Us == WHITE ? p << 8 : p >> 8;
+    else if (Direction == DELTA_NE)
+        return Us == WHITE ? p << 9 : p >> 7;
+    else if (Direction == DELTA_NW)
+        return Us == WHITE ? p << 7 : p >> 9;
+    else
+        return p;
+  }
+
   // Template generate_piece_checks() with specializations
   template<PieceType>
   MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
@@ -175,7 +188,7 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
   Square ksq = pos.king_square(opposite_color(us));
   MoveStack* mlist_start = mlist;
 
-  assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
+  assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
 
   // Pieces moves
   mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
@@ -215,7 +228,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
   Square ksq = pos.king_square(us);
   MoveStack* mlist_start = mlist;
 
-  assert(pos.piece_on(ksq) == king_of_color(us));
+  assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
 
   // The bitboard of occupied pieces without our king
   Bitboard b_noKing = pos.occupied_squares();
@@ -400,7 +413,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
           return false;
 
       assert(pos.square_is_empty(to));
-      assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
+      assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
 
       // The move is pseudo-legal, check if it is also legal
       return pos.pl_move_is_legal(m, pinned);
@@ -417,7 +430,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
 
       assert(from == pos.king_square(us));
       assert(to == pos.initial_kr_square(us));
-      assert(pos.piece_on(to) == rook_of_color(us));
+      assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
 
       Square g1 = relative_square(us, SQ_G1);
       Square f1 = relative_square(us, SQ_F1);
@@ -450,7 +463,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
 
       assert(from == pos.king_square(us));
       assert(to == pos.initial_qr_square(us));
-      assert(pos.piece_on(to) == rook_of_color(us));
+      assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
 
       Square c1 = relative_square(us, SQ_C1);
       Square d1 = relative_square(us, SQ_D1);
@@ -601,7 +614,7 @@ namespace {
     Bitboard enemyPieces = pos.pieces_of_color(Them);
 
     // Captures in the a1-h8 (a8-h1 for black) direction
-    Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
+    Bitboard b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces;
 
     // Capturing promotions
     Bitboard b2 = b1 & TRank8BB;
@@ -620,7 +633,7 @@ namespace {
     }
 
     // Captures in the h1-a8 (h8-a1 for black) direction
-    b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
+    b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces;
 
     // Capturing promotions
     b2 = b1 & TRank8BB;
@@ -639,7 +652,7 @@ namespace {
     }
 
     // Non-capturing promotions
-    b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
+    b1 = move_pawns<Us, DELTA_N>(pawns) & pos.empty_squares() & TRank8BB;
     while (b1)
     {
         to = pop_1st_bit(&b1);
@@ -676,7 +689,7 @@ namespace {
     Square to;
 
     // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
-    b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
+    b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces & TRank8BB;
     while (b1)
     {
         to = pop_1st_bit(&b1);
@@ -686,7 +699,7 @@ namespace {
     }
 
     // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
-    b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
+    b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces & TRank8BB;
     while (b1)
     {
         to = pop_1st_bit(&b1);
@@ -696,7 +709,7 @@ namespace {
     }
 
     // Single pawn pushes
-    b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
+    b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares;
     b2 = b1 & TRank8BB;
     while (b2)
     {
@@ -713,7 +726,7 @@ namespace {
     }
 
     // Double pawn pushes
-    b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
+    b2 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
     while (b2)
     {
         to = pop_1st_bit(&b2);
@@ -738,7 +751,7 @@ namespace {
         b1 = pos.pawns(Us) & ~file_bb(ksq);
 
         // Discovered checks, single pawn pushes, no promotions
-        b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
+        b2 = b3 = move_pawns<Us, DELTA_N>(b1 & dc) & empty & ~TRank8BB;
         while (b3)
         {
             Square to = pop_1st_bit(&b3);
@@ -746,7 +759,7 @@ namespace {
         }
 
         // Discovered checks, double pawn pushes
-        b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
+        b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty;
         while (b3)
         {
             Square to = pop_1st_bit(&b3);
@@ -759,7 +772,7 @@ namespace {
     b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
 
     // Direct checks, single pawn pushes
-    b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
+    b2 = move_pawns<Us, DELTA_N>(b1) & empty;
     b3 = b2 & pos.pawn_attacks(Them, ksq);
     while (b3)
     {
@@ -768,9 +781,7 @@ namespace {
     }
 
     // Direct checks, double pawn pushes
-    b3 =  (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
-        & empty
-        & pos.pawn_attacks(Them, ksq);
+    b3 =  move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty & pos.pawn_attacks(Them, ksq);
     while (b3)
     {
         Square to = pop_1st_bit(&b3);
@@ -803,9 +814,17 @@ namespace {
         return mlist;
 
     Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
+    if (!checkSqs)
+        return mlist;
+
     while (b)
     {
         Square from = pop_1st_bit(&b);
+        if (   (Piece == QUEEN  && !(QueenPseudoAttacks[from]  & checkSqs))
+            || (Piece == ROOK   && !(RookPseudoAttacks[from]   & checkSqs))
+            || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
+            continue;
+
         Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
         SERIALIZE_MOVES(bb);
     }
@@ -817,12 +836,12 @@ namespace {
                                              Bitboard blockSquares, MoveStack* mlist) {
     Square to;
 
-    // Find non-pinned pawns
-    Bitboard b1 = pos.pawns(Us) & ~pinned;
+    // Find non-pinned pawns and push them one square
+    Bitboard b1 = move_pawns<Us, DELTA_N>(pos.pawns(Us) & ~pinned);
 
-    // Single pawn pushes. We don't have to AND with empty squares here,
+    // We don't have to AND with empty squares here,
     // because the blocking squares will always be empty.
-    Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
+    Bitboard b2 = b1 & blockSquares;
     while (b2)
     {
         to = pop_1st_bit(&b2);
@@ -840,8 +859,8 @@ namespace {
     }
 
     // Double pawn pushes
-    b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
-    b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;
+    b2 = b1 & pos.empty_squares() & TRank3BB;
+    b2 = move_pawns<Us, DELTA_N>(b2) & blockSquares;
     while (b2)
     {
         to = pop_1st_bit(&b2);
@@ -866,7 +885,7 @@ namespace {
         Color them = opposite_color(us);
         Square ksq = pos.king_square(us);
 
-        assert(pos.piece_on(ksq) == king_of_color(us));
+        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);
@@ -874,7 +893,7 @@ namespace {
         Square s;
         bool illegal = false;
 
-        assert(pos.piece_on(rsq) == rook_of_color(us));
+        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++)