X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fmovegen.cpp;h=00e1adf5caa293bcff46b0b263f55196319d2fb7;hp=738b2acc6f1494e43ce651b7bc50de9b2926bfe0;hb=23de3e16f153f91601e0a6a19b793c54a1ba35cd;hpb=02fd34a5e840f0dd7e6e810f4fcea5ddf82858a9 diff --git a/src/movegen.cpp b/src/movegen.cpp index 738b2acc..00e1adf5 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -52,9 +52,6 @@ namespace { EVASION }; - // Functions - bool castling_is_check(const Position&, CastlingSide); - // Helper templates template MoveStack* generate_castle_moves(const Position&, MoveStack*); @@ -80,12 +77,16 @@ namespace { : generate_pawn_moves(p, m)); } - // Template generate_piece_checks with specializations + // Templates for non-capture checks generation + + template + MoveStack* generate_discovered_checks(const Position& pos, Square from, MoveStack* mlist); + template - MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square); + MoveStack* generate_direct_checks(const Position&, MoveStack*, Color, Bitboard, Square); template<> - inline MoveStack* generate_piece_checks(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) { + inline MoveStack* generate_direct_checks(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) { return (us == WHITE ? generate_pawn_moves(p, m, dc, ksq) : generate_pawn_moves(p, m, dc, ksq)); @@ -165,26 +166,28 @@ MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bi assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING)); - // Pieces moves - mlist = generate_piece_checks(pos, mlist, us, dc, ksq); - mlist = generate_piece_checks(pos, mlist, us, dc, ksq); - mlist = generate_piece_checks(pos, mlist, us, dc, ksq); - mlist = generate_piece_checks(pos, mlist, us, dc, ksq); - mlist = generate_piece_checks(pos, mlist, us, dc, ksq); - mlist = generate_piece_checks(pos, mlist, us, dc, ksq); - - // Castling moves that give check. Very rare but nice to have! - if ( pos.can_castle_queenside(us) - && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D) - && castling_is_check(pos, QUEEN_SIDE)) - mlist = generate_castle_moves(pos, mlist); - - if ( pos.can_castle_kingside(us) - && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F) - && castling_is_check(pos, KING_SIDE)) - mlist = generate_castle_moves(pos, mlist); + // Discovered non-capture checks + Bitboard b = dc; + while (b) + { + Square from = pop_1st_bit(&b); + switch (pos.type_of_piece_on(from)) + { + case PAWN: /* Will be generated togheter with pawns direct checks */ break; + case KNIGHT: mlist = generate_discovered_checks(pos, from, mlist); break; + case BISHOP: mlist = generate_discovered_checks(pos, from, mlist); break; + case ROOK: mlist = generate_discovered_checks(pos, from, mlist); break; + case KING: mlist = generate_discovered_checks(pos, from, mlist); break; + default: assert(false); break; + } + } - return mlist; + // Direct non-capture checks + mlist = generate_direct_checks(pos, mlist, us, dc, ksq); + mlist = generate_direct_checks(pos, mlist, us, dc, ksq); + mlist = generate_direct_checks(pos, mlist, us, dc, ksq); + mlist = generate_direct_checks(pos, mlist, us, dc, ksq); + return generate_direct_checks(pos, mlist, us, dc, ksq); } @@ -476,10 +479,10 @@ namespace { Square from; Bitboard b; + const Square* ptr = pos.piece_list_begin(us, Piece); - for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++) + while ((from = *ptr++) != SQ_NONE) { - from = pos.piece_list(us, Piece, i); b = pos.attacks_from(from) & target; SERIALIZE_MOVES(b); } @@ -502,10 +505,10 @@ namespace { Color us, Bitboard target, Bitboard pinned) { Square from; Bitboard b; + const Square* ptr = pos.piece_list_begin(us, Piece); - for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++) + while ((from = *ptr++) != SQ_NONE) { - from = pos.piece_list(us, Piece, i); if (pinned && bit_is_set(pinned, from)) continue; @@ -576,7 +579,6 @@ namespace { Bitboard b1, b2, dcPawns1, dcPawns2; Square to; Bitboard pawns = (Type == EVASION ? pos.pieces(PAWN, Us) & ~dcp : pos.pieces(PAWN, Us)); - Bitboard emptySquares = pos.empty_squares(); bool possiblePromotion = pawns & TRank7BB; if (Type == CAPTURE) @@ -621,7 +623,7 @@ namespace { // Underpromotion pawn pushes. Also queen promotions for evasions and captures. b1 = move_pawns(pp) & TRank8BB; - b1 &= (Type == EVASION ? blockSquares : emptySquares); + b1 &= (Type == EVASION ? blockSquares : pos.empty_squares()); while (b1) { @@ -638,88 +640,92 @@ namespace { } } - if (Type == CAPTURE) + if (Type != CAPTURE) { - // En passant captures - if (pos.ep_square() != SQ_NONE) + Bitboard emptySquares = pos.empty_squares(); + dcPawns1 = dcPawns2 = EmptyBoardBB; + if (Type == CHECK && (pawns & dcp)) { - assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6); - assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3); + // Pawn moves which gives discovered check. This is possible only if the + // pawn is not on the same file as the enemy king, because we don't + // generate captures. + dcPawns1 = move_pawns(pawns & dcp & ~file_bb(ksq)) & emptySquares & ~TRank8BB; + dcPawns2 = move_pawns(dcPawns1 & TRank3BB) & emptySquares; + } + + // Single pawn pushes + b1 = move_pawns(pawns) & emptySquares & ~TRank8BB; + b2 = (Type == CHECK ? (b1 & pos.attacks_from(ksq, Them)) | dcPawns1 : + (Type == EVASION ? b1 & blockSquares : b1)); + SERIALIZE_MOVES_D(b2, -TDELTA_N); + + // Double pawn pushes + b1 = move_pawns(b1 & TRank3BB) & emptySquares; + b2 = (Type == CHECK ? (b1 & pos.attacks_from(ksq, Them)) | dcPawns2 : + (Type == EVASION ? b1 & blockSquares : b1)); + SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N); + } + else if (pos.ep_square() != SQ_NONE) // En passant captures + { + assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6); + assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3); - Bitboard b1 = pawns & pos.attacks_from(pos.ep_square(), Them); - assert(b1 != EmptyBoardBB); + b1 = pawns & pos.attacks_from(pos.ep_square(), Them); + assert(b1 != EmptyBoardBB); - while (b1) - { - to = pop_1st_bit(&b1); - (*mlist++).move = make_ep_move(to, pos.ep_square()); - } + while (b1) + { + to = pop_1st_bit(&b1); + (*mlist++).move = make_ep_move(to, pos.ep_square()); } - return mlist; } + return mlist; + } - dcPawns1 = dcPawns2 = EmptyBoardBB; - if (Type == CHECK && (pawns & dcp)) + template + MoveStack* generate_discovered_checks(const Position& pos, Square from, MoveStack* mlist) { + + assert(Piece != QUEEN); + + Bitboard b = pos.attacks_from(from) & pos.empty_squares(); + if (Piece == KING) { - // Pawn moves which gives discovered check. This is possible only if the - // pawn is not on the same file as the enemy king, because we don't - // generate captures. - dcPawns1 = move_pawns(pawns & dcp & ~file_bb(ksq)) & emptySquares & ~TRank8BB; - dcPawns2 = move_pawns(dcPawns1 & TRank3BB) & emptySquares; + Square ksq = pos.king_square(opposite_color(pos.side_to_move())); + b &= ~QueenPseudoAttacks[ksq]; } - - // Single pawn pushes - b1 = move_pawns(pawns) & emptySquares & ~TRank8BB; - b2 = (Type == CHECK ? (b1 & pos.attacks_from(ksq, Them)) | dcPawns1 : - (Type == EVASION ? b1 & blockSquares : b1)); - SERIALIZE_MOVES_D(b2, -TDELTA_N); - - // Double pawn pushes - b1 = move_pawns(b1 & TRank3BB) & emptySquares; - b2 = (Type == CHECK ? (b1 & pos.attacks_from(ksq, Them)) | dcPawns2 : - (Type == EVASION ? b1 & blockSquares : b1)); - SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N); + SERIALIZE_MOVES(b); return mlist; } template - MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us, + MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us, Bitboard dc, Square ksq) { + assert(Piece != KING); - Bitboard target = pos.pieces(Piece, us); - - // Discovered non-capture checks - Bitboard b = target & dc; - - assert(Piece != QUEEN || !b); - - while (b) - { - Square from = pop_1st_bit(&b); - Bitboard bb = pos.attacks_from(from) & pos.empty_squares(); - if (Piece == KING) - bb &= ~QueenPseudoAttacks[ksq]; - - SERIALIZE_MOVES(bb); - } + Square from; + Bitboard checkSqs; + const Square* ptr = pos.piece_list_begin(us, Piece); - // Direct non-capture checks - b = target & ~dc; - Bitboard checkSqs = pos.attacks_from(ksq) & pos.empty_squares(); - if (Piece == KING || !checkSqs) + if ((from = *ptr++) == SQ_NONE) return mlist; - while (b) + checkSqs = pos.attacks_from(ksq) & pos.empty_squares(); + + do { - Square from = pop_1st_bit(&b); if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs)) || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs)) || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs))) continue; + if (dc && bit_is_set(dc, from)) + continue; + Bitboard bb = pos.attacks_from(from) & checkSqs; SERIALIZE_MOVES(bb); - } + + } while ((from = *ptr++) != SQ_NONE); + return mlist; } @@ -765,17 +771,4 @@ namespace { } return mlist; } - - bool castling_is_check(const Position& pos, CastlingSide side) { - - // After castling opponent king is attacked by the castled rook? - File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F); - Color us = pos.side_to_move(); - Square ksq = pos.king_square(us); - Bitboard occ = pos.occupied_squares(); - - clear_bit(&occ, ksq); // Remove our king from the board - Square rsq = make_square(rookFile, square_rank(ksq)); - return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us))); - } }