X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fmovegen.cpp;h=3dfd8f6a488bd2348ec6d0486d5801f57ee99a69;hp=0646c230f50c778360a48c368258d47998df2e68;hb=1bd1f5a29374f1f4b20e88c38f40b18604eb70d3;hpb=00bcc6478737caae740dbf511d3587b9332ad2e6 diff --git a/src/movegen.cpp b/src/movegen.cpp index 0646c230..3dfd8f6a 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -56,14 +56,115 @@ namespace { 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, 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); + + template + int generate_pawn_captures(const Position&, MoveStack*); + + template + int generate_pawn_noncaptures(const Position&, MoveStack*); + + template + int generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*, int); + + template + int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int); + + template + int generate_piece_moves(const Position&, MoveStack*, Color, Bitboard); + + template + int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int); + + template + int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int); + + + /// Templates are defined here to avoid lookup issues with specializations + + template + int generate_piece_moves(const Position &pos, MoveStack *mlist, + Color side, Bitboard target) { + int n = 0; + for (int i = 0; i < pos.piece_count(side, Piece); i++) + { + Square from = pos.piece_list(side, Piece, i); + Bitboard b = pos.piece_attacks(from) & target; + while (b) + { + Square to = pop_1st_bit(&b); + mlist[n++].move = make_move(from, to); + } + } + return n; + } + + + template + int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc, + Square ksq, MoveStack* mlist, int n) { + // Discovered checks + Bitboard b = target & dc; + while (b) + { + Square from = pop_1st_bit(&b); + Bitboard bb = pos.piece_attacks(from) & pos.empty_squares(); + while (bb) + { + Square to = pop_1st_bit(&bb); + mlist[n++].move = make_move(from, to); + } + } + // Direct checks + b = target & ~dc; + Bitboard checkSqs = pos.piece_attacks(ksq) & pos.empty_squares(); + while (b) + { + Square from = pop_1st_bit(&b); + Bitboard bb = pos.piece_attacks(from) & checkSqs; + while (bb) + { + Square to = pop_1st_bit(&bb); + mlist[n++].move = make_move(from, to); + } + } + return n; + } + + + template<> // Special case the King + int generate_piece_checks(const Position& pos, Bitboard, Bitboard dc, + Square ksq, MoveStack* mlist, int n) { + if (bit_is_set(dc, ksq)) + { + Bitboard bb = pos.piece_attacks(ksq) + & pos.empty_squares() + & ~QueenPseudoAttacks[ksq]; + while (bb) + { + Square to = pop_1st_bit(&bb); + mlist[n++].move = make_move(ksq, to); + } + } + return n; + } + + + template + int generate_piece_blocking_evasions(const Position& pos, Bitboard b, + Bitboard blockSquares, MoveStack* mlist, int n) { + while (b) + { + Square from = pop_1st_bit(&b); + Bitboard bb = pos.piece_attacks(from) & blockSquares; + while (bb) + { + Square to = pop_1st_bit(&bb); + mlist[n++].move = make_move(from, to); + } + } + return n; + } } @@ -85,13 +186,15 @@ int generate_captures(const Position& pos, MoveStack* mlist) { int n; if (us == WHITE) - n = generate_pawn_captures(WhitePawnOffsets, pos, mlist); + n = generate_pawn_captures(pos, mlist); else - n = generate_pawn_captures(BlackPawnOffsets, pos, mlist); - - for (PieceType pce = KNIGHT; pce <= KING; pce++) - n += generate_piece_moves(pce, pos, mlist+n, us, target); + n = generate_pawn_captures(pos, mlist); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); return n; } @@ -109,12 +212,15 @@ int generate_noncaptures(const Position& pos, MoveStack *mlist) { int n; if (us == WHITE) - n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist); + n = generate_pawn_noncaptures(pos, mlist); else - n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist); + n = generate_pawn_noncaptures(pos, mlist); - for (PieceType pce = KNIGHT; pce <= KING; pce++) - n += generate_piece_moves(pce, pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); + n += generate_piece_moves(pos, mlist+n, us, target); n += generate_castle_moves(pos, mlist+n, us); return n; @@ -140,38 +246,29 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { // Pawn moves if (us == WHITE) - n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0); + n = generate_pawn_checks(pos, dc, ksq, mlist, 0); else - n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0); + n = generate_pawn_checks(pos, dc, ksq, mlist, 0); // Pieces moves Bitboard b = pos.knights(us); if (b) - n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n); + n = generate_piece_checks(pos, b, dc, ksq, mlist, n); b = pos.bishops(us); if (b) - n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n); + n = generate_piece_checks(pos, b, dc, ksq, mlist, n); b = pos.rooks(us); if (b) - n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n); + n = generate_piece_checks(pos, b, dc, ksq, mlist, n); b = pos.queens(us); if (b) - n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n); + n = generate_piece_checks(pos, b, dc, ksq, mlist, n); - // King moves - Square from = pos.king_square(us); - if (bit_is_set(dc, from)) - { - b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq]; - while (b) - { - Square to = pop_1st_bit(&b); - mlist[n++].move = make_move(from, to); - } - } + // Hopefully we always have a king ;-) + n = generate_piece_checks(pos, b, dc, pos.king_square(us), mlist, n); // TODO: Castling moves! @@ -198,7 +295,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist) { assert(pos.piece_on(ksq) == king_of_color(us)); // Generate evasions for king - Bitboard b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us); + Bitboard b1 = pos.piece_attacks(ksq) & ~pos.pieces_of_color(us); Bitboard b2 = pos.occupied_squares(); clear_bit(&b2, ksq); @@ -213,9 +310,9 @@ int generate_evasions(const Position& pos, MoveStack* mlist) { // the king will remain in check on the destination square. if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them)) || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them)) - || (pos.knight_attacks(to) & pos.knights(them)) + || (pos.piece_attacks(to) & pos.knights(them)) || (pos.pawn_attacks(us, to) & pos.pawns(them)) - || (pos.king_attacks(to) & pos.kings(them)))) + || (pos.piece_attacks(to) & pos.kings(them)))) mlist[n++].move = make_move(ksq, to); } @@ -252,9 +349,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)) ) & not_pinned; + b1 = ( (pos.piece_attacks(checksq) & pos.knights(us)) + | (pos.piece_attacks(checksq) & pos.bishops_and_queens(us)) + | (pos.piece_attacks(checksq) & pos.rooks_and_queens(us)) ) & not_pinned; while (b1) { @@ -273,26 +370,26 @@ int generate_evasions(const Position& pos, MoveStack* mlist) { // Pawn moves. Because a blocking evasion can never be a capture, we // only generate pawn pushes. if (us == WHITE) - n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n); + n = generate_pawn_blocking_evasions(pos, not_pinned, blockSquares, mlist, n); else - n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n); + n = generate_pawn_blocking_evasions(pos, not_pinned, blockSquares, mlist, n); // Pieces moves b1 = pos.knights(us) & not_pinned; if (b1) - n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n); + n = generate_piece_blocking_evasions(pos, b1, blockSquares, mlist, n); b1 = pos.bishops(us) & not_pinned; if (b1) - n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n); + n = generate_piece_blocking_evasions(pos, b1, blockSquares, mlist, n); b1 = pos.rooks(us) & not_pinned; if (b1) - n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n); + n = generate_piece_blocking_evasions(pos, b1, blockSquares, mlist, n); b1 = pos.queens(us) & not_pinned; if (b1) - n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n); + n = generate_piece_blocking_evasions(pos, b1, blockSquares, mlist, n); } // Finally, the ugly special case of en passant captures. An en passant @@ -540,7 +637,10 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) { namespace { - int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) { + template + int generate_pawn_captures(const Position& pos, MoveStack* mlist) { + + static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets); Bitboard pawns = pos.pawns(ofs.us); Bitboard enemyPieces = pos.pieces_of_color(ofs.them); @@ -612,7 +712,10 @@ namespace { } - int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) { + template + int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) { + + static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets); Bitboard pawns = pos.pawns(ofs.us); Bitboard enemyPieces = pos.pieces_of_color(ofs.them); @@ -669,28 +772,6 @@ namespace { } - int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist, - Color side, Bitboard target) { - - const Piece_attacks_fn mem_fn = piece_attacks_fn[piece]; - Square from, to; - Bitboard b; - int n = 0; - - for (int i = 0; i < pos.piece_count(side, piece); i++) - { - from = pos.piece_list(side, piece, i); - b = (pos.*mem_fn)(from) & target; - while (b) - { - to = pop_1st_bit(&b); - mlist[n++].move = make_move(from, to); - } - } - return n; - } - - int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) { int n = 0; @@ -756,42 +837,12 @@ namespace { return n; } - int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target, - Bitboard dc, Square ksq, MoveStack* mlist, int n) { - - const Piece_attacks_fn mem_fn = piece_attacks_fn[pce]; - // Discovered checks - Bitboard b = target & dc; - while (b) - { - Square from = pop_1st_bit(&b); - Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares(); - while (bb) - { - Square to = pop_1st_bit(&bb); - mlist[n++].move = make_move(from, to); - } - } - - // Direct checks - b = target & ~dc; - Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares(); - while (b) - { - Square from = pop_1st_bit(&b); - Bitboard bb = (pos.*mem_fn)(from) & checkSqs; - while (bb) - { - Square to = pop_1st_bit(&bb); - mlist[n++].move = make_move(from, to); - } - } - return n; - } - - int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n) + template + int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n) { + static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets); + // Pawn moves which give 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. @@ -840,28 +891,12 @@ namespace { return n; } + template + int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned, + Bitboard blockSquares, MoveStack* mlist, int 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; - } - + static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets); - 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;