]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Final semplification of generate_evasions()
[stockfish] / src / movegen.cpp
index aecf28f4bacc05b54519b3661682454bd12d0bf6..d942d9c290c47abc77f90b770f49f4cc56fc7162 100644 (file)
@@ -43,24 +43,27 @@ namespace {
   struct PawnOffsets {
 
       Bitboard Rank3BB, Rank8BB;
+      Rank RANK_8;
       SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
       Color us, them;
       typedef Bitboard (*Shift_fn)(Bitboard b);
       Shift_fn forward, forward_left, forward_right;
   };
 
-  const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK,
+  const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, RANK_8, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK,
                                          &forward_white, forward_left_white, forward_right_white };
 
-  const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
+  const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
                                          &forward_black, &forward_left_black, &forward_right_black };
   
   int generate_pawn_captures(const PawnOffsets&, const Position&, MoveStack*);  
   int generate_pawn_noncaptures(const PawnOffsets&, const Position&, MoveStack*);
-  int generate_pawn_checks(const PawnOffsets&, const Position&, Bitboard dc, Square ksq, MoveStack*, int n);
-  int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target, Bitboard dc, Square ksq, MoveStack* mlist, int n);
-  int generate_piece_moves(PieceType, const Position&, MoveStack*, Color side, Bitboard t);
-  int generate_castle_moves(const Position&, MoveStack*, Color us);
+  int generate_pawn_checks(const PawnOffsets&, const Position&, Bitboard, Square, MoveStack*, int);
+  int generate_piece_checks(PieceType, const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
+  int generate_piece_moves(PieceType, const Position&, MoveStack*, Color, Bitboard);
+  int generate_castle_moves(const Position&, MoveStack*, Color);
+  int generate_piece_blocking_evasions(PieceType, const Position&, Bitboard, Bitboard, MoveStack*, int);
+  int generate_pawn_blocking_evasions(const PawnOffsets&, const Position&, Bitboard, Bitboard, MoveStack*, int);
 }
 
 
@@ -249,9 +252,9 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
       }
 
       // Pieces captures
-      b1 =  pos.knight_attacks(checksq) & pos.knights(us)
-          & pos.bishop_attacks(checksq) & pos.bishops_and_queens(us)
-          & pos.rook_attacks(checksq)   & pos.rooks_and_queens(us)
+      b1 =  (pos.knight_attacks(checksq) & pos.knights(us))
+          | (pos.bishop_attacks(checksq) & pos.bishops_and_queens(us))
+          | (pos.rook_attacks(checksq)   & pos.rooks_and_queens(us))
           & not_pinned;
 
       while (b1)
@@ -269,131 +272,28 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
           assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
 
           // Pawn moves. Because a blocking evasion can never be a capture, we
-          // only generate pawn pushes.  As so often, the code for pawns is a bit
-          // ugly, and uses separate clauses for white and black pawns. :-(
+          // only generate pawn pushes.
           if (us == WHITE)
-          {
-              // Find non-pinned pawns
-              b1 = pos.pawns(WHITE) & not_pinned;
-
-              // Single pawn pushes. We don't have to AND with empty squares here,
-              // because the blocking squares will always be empty.
-              b2 = (b1 << 8) & blockSquares;
-              while(b2)
-              {
-                  to = pop_1st_bit(&b2);
-
-                  assert(pos.piece_on(to) == EMPTY);
-
-                  if (square_rank(to) == RANK_8)
-                  {
-                      mlist[n++].move = make_promotion_move(to - DELTA_N, to, QUEEN);
-                      mlist[n++].move = make_promotion_move(to - DELTA_N, to, ROOK);
-                      mlist[n++].move = make_promotion_move(to - DELTA_N, to, BISHOP);
-                      mlist[n++].move = make_promotion_move(to - DELTA_N, to, KNIGHT);
-                  } else
-                      mlist[n++].move = make_move(to - DELTA_N, to);
-              }
-
-              // Double pawn pushes
-              b2 = (((b1 << 8) & pos.empty_squares() & Rank3BB) << 8) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-                
-                  assert(pos.piece_on(to) == EMPTY);
-                  assert(square_rank(to) == RANK_4);
-
-                  mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
-              }
-          } else { // (us == BLACK)
-
-              // Find non-pinned pawns
-              b1 = pos.pawns(BLACK) & not_pinned;
-
-              // Single pawn pushes. We don't have to AND with empty squares here,
-              // because the blocking squares will always be empty.
-              b2 = (b1 >> 8) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-
-                  assert(pos.piece_on(to) == EMPTY);
-        
-                  if (square_rank(to) == RANK_1)
-                  {
-                      mlist[n++].move = make_promotion_move(to - DELTA_S, to, QUEEN);
-                      mlist[n++].move = make_promotion_move(to - DELTA_S, to, ROOK);
-                      mlist[n++].move = make_promotion_move(to - DELTA_S, to, BISHOP);
-                      mlist[n++].move = make_promotion_move(to - DELTA_S, to, KNIGHT);
-                  } else
-                      mlist[n++].move = make_move(to - DELTA_S, to);
-              }
-
-              // Double pawn pushes
-              b2 = (((b1 >> 8) & pos.empty_squares() & Rank6BB) >> 8) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-
-                  assert(pos.piece_on(to) == EMPTY);
-                  assert(square_rank(to) == RANK_5);
-
-                  mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
-              }
-          }
-
-          // Knight moves
+              n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
+          else
+              n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
+
+          // Pieces moves
           b1 = pos.knights(us) & not_pinned;
-          while (b1)
-          {
-              from = pop_1st_bit(&b1);
-              b2 = pos.knight_attacks(from) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-                  mlist[n++].move = make_move(from, to);
-              }
-          }
-          
-          // Bishop moves
+          if (b1)
+              n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
+
           b1 = pos.bishops(us) & not_pinned;
-          while (b1)
-          {
-              from = pop_1st_bit(&b1);
-              b2 = pos.bishop_attacks(from) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-                  mlist[n++].move = make_move(from, to);
-              }
-          }
-          
-          // Rook moves
+          if (b1)
+              n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
+
           b1 = pos.rooks(us) & not_pinned;
-          while (b1)
-          {
-              from = pop_1st_bit(&b1);
-              b2 = pos.rook_attacks(from) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-                  mlist[n++].move = make_move(from, to);
-              }
-          }
-          
-          // Queen moves
+          if (b1)
+              n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
+
           b1 = pos.queens(us) & not_pinned;
-          while (b1)
-          {
-              from = pop_1st_bit(&b1);
-              b2 = pos.queen_attacks(from) & blockSquares;
-              while (b2)
-              {
-                  to = pop_1st_bit(&b2);
-                  mlist[n++].move = make_move(from, to);
-              }
-          }
+          if (b1)
+              n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
     }
 
     // Finally, the ugly special case of en passant captures. An en passant
@@ -940,4 +840,63 @@ namespace {
     }
     return n;
   }
+
+
+   int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
+                                        Bitboard blockSquares, MoveStack* mlist, int n) {
+
+    const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
+
+    while (b)
+    {
+        Square from = pop_1st_bit(&b);
+        Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
+        while (bb)
+        {
+            Square to = pop_1st_bit(&bb);
+            mlist[n++].move = make_move(from, to);
+        }
+    }
+    return n;
+  }
+
+
+  int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
+                                      Bitboard blockSquares, MoveStack* mlist, int n) {
+    // Find non-pinned pawns
+    Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
+
+    // Single pawn pushes. We don't have to AND with empty squares here,
+    // because the blocking squares will always be empty.
+    Bitboard b2 = (ofs.forward)(b1) & blockSquares;
+    while (b2)
+    {
+        Square to = pop_1st_bit(&b2);
+
+        assert(pos.piece_on(to) == EMPTY);
+
+        if (square_rank(to) == ofs.RANK_8)
+        {
+            mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
+            mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
+            mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
+            mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
+        } else
+            mlist[n++].move = make_move(to - ofs.DELTA_N, to);
+    }
+
+    // Double pawn pushes
+    b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
+    while (b2)
+    {
+        Square to = pop_1st_bit(&b2);
+
+        assert(pos.piece_on(to) == EMPTY);
+        assert(ofs.us != WHITE || square_rank(to) == RANK_4);
+        assert(ofs.us != BLACK || square_rank(to) == RANK_5);
+
+        mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
+    }
+    return n;
+  }
 }