X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fmovegen.cpp;h=d2b55bd28053f262c2513bdfd17c19a0b307041d;hp=94bc173e10c94f142043a2ff5c94726f8b3371a0;hb=a52ab2afbf38e42376732d41b14279fd29fcf23f;hpb=0a0ea36e25b2f6fff8859e02027c5842edeccc03 diff --git a/src/movegen.cpp b/src/movegen.cpp index 94bc173e..d2b55bd2 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -64,6 +64,20 @@ namespace { template MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*); + template + 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; + + assert(false); + return p; + } + // Template generate_piece_checks() with specializations template MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square); @@ -166,14 +180,13 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) { /// generate_checks() generates all pseudo-legal non-capturing, non-promoting /// checks. It returns the number of generated moves. -int generate_checks(const Position& pos, MoveStack* mlist) { +int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { assert(pos.is_ok()); assert(!pos.is_check()); Color us = pos.side_to_move(); Square ksq = pos.king_square(opposite_color(us)); - Bitboard dc = pos.discovered_check_candidates(us); MoveStack* mlist_start = mlist; assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING)); @@ -205,7 +218,7 @@ int generate_checks(const Position& pos, MoveStack* mlist) { /// in check. Unlike the other move generation functions, this one generates /// only legal moves. It returns the number of generated moves. -int generate_evasions(const Position& pos, MoveStack* mlist) { +int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { assert(pos.is_ok()); assert(pos.is_check()); @@ -259,7 +272,6 @@ int generate_evasions(const Position& pos, MoveStack* mlist) { if (!(checkers & (checkers - 1))) // Only one bit set? { Square checksq = first_1(checkers); - Bitboard pinned = pos.pinned_pieces(us); assert(pos.color_of_piece_on(checksq) == them); @@ -352,7 +364,7 @@ int generate_legal_moves(const Position& pos, MoveStack* mlist) { Bitboard pinned = pos.pinned_pieces(pos.side_to_move()); if (pos.is_check()) - return generate_evasions(pos, mlist); + return generate_evasions(pos, mlist, pinned); // Generate pseudo-legal moves int n = generate_captures(pos, mlist); @@ -373,17 +385,17 @@ int generate_legal_moves(const Position& pos, MoveStack* mlist) { /// returned. If not, the function returns false. This function must /// only be used when the side to move is not in check. -bool move_is_legal(const Position& pos, const Move m) { +bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) { assert(pos.is_ok()); assert(!pos.is_check()); assert(move_is_ok(m)); + assert(pinned == pos.pinned_pieces(pos.side_to_move())); Color us = pos.side_to_move(); Color them = opposite_color(us); Square from = move_from(m); Piece pc = pos.piece_on(from); - Bitboard pinned = pos.pinned_pieces(us); // If the from square is not occupied by a piece belonging to the side to // move, the move is obviously not legal. @@ -603,7 +615,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(pawns) & ~FileABB & enemyPieces; // Capturing promotions Bitboard b2 = b1 & TRank8BB; @@ -622,7 +634,7 @@ namespace { } // Captures in the h1-a8 (h8-a1 for black) direction - b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces; + b1 = move_pawns(pawns) & ~FileHBB & enemyPieces; // Capturing promotions b2 = b1 & TRank8BB; @@ -641,7 +653,7 @@ namespace { } // Non-capturing promotions - b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB; + b1 = move_pawns(pawns) & pos.empty_squares() & TRank8BB; while (b1) { to = pop_1st_bit(&b1); @@ -678,7 +690,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(pawns) & ~FileABB & enemyPieces & TRank8BB; while (b1) { to = pop_1st_bit(&b1); @@ -688,7 +700,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(pawns) & ~FileHBB & enemyPieces & TRank8BB; while (b1) { to = pop_1st_bit(&b1); @@ -698,7 +710,7 @@ namespace { } // Single pawn pushes - b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares; + b1 = move_pawns(pawns) & emptySquares; b2 = b1 & TRank8BB; while (b2) { @@ -715,7 +727,7 @@ namespace { } // Double pawn pushes - b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares; + b2 = move_pawns(b1 & TRank3BB) & emptySquares; while (b2) { to = pop_1st_bit(&b2); @@ -740,7 +752,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(b1 & dc) & empty & ~TRank8BB; while (b3) { Square to = pop_1st_bit(&b3); @@ -748,7 +760,7 @@ namespace { } // Discovered checks, double pawn pushes - b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty; + b3 = move_pawns(b2 & TRank3BB) & empty; while (b3) { Square to = pop_1st_bit(&b3); @@ -761,7 +773,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(b1) & empty; b3 = b2 & pos.pawn_attacks(Them, ksq); while (b3) { @@ -770,9 +782,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(b2 & TRank3BB) & empty & pos.pawn_attacks(Them, ksq); while (b3) { Square to = pop_1st_bit(&b3); @@ -805,6 +815,9 @@ namespace { return mlist; Bitboard checkSqs = pos.piece_attacks(ksq) & pos.empty_squares(); + if (!checkSqs) + return mlist; + while (b) { Square from = pop_1st_bit(&b); @@ -819,12 +832,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(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); @@ -842,8 +855,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(b2) & blockSquares; while (b2) { to = pop_1st_bit(&b2);