X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmovegen.cpp;h=3dfd8f6a488bd2348ec6d0486d5801f57ee99a69;hb=1bd1f5a29374f1f4b20e88c38f40b18604eb70d3;hp=259d276b3d79f122de26a1ad4c826fd671498bf4;hpb=f036239521fe4f6afb7e8cbc51d860ffa476f6bd;p=stockfish diff --git a/src/movegen.cpp b/src/movegen.cpp index 259d276b..3dfd8f6a 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -56,11 +56,19 @@ 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_castle_moves(const Position&, MoveStack*, Color); - 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); @@ -70,6 +78,93 @@ namespace { 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; + } } @@ -91,9 +186,9 @@ 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); + n = generate_pawn_captures(pos, mlist); n += generate_piece_moves(pos, mlist+n, us, target); n += generate_piece_moves(pos, mlist+n, us, target); @@ -117,9 +212,9 @@ 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); n += generate_piece_moves(pos, mlist+n, us, target); n += generate_piece_moves(pos, mlist+n, us, target); @@ -151,9 +246,9 @@ 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); @@ -172,17 +267,8 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { if (b) 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.piece_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! @@ -284,9 +370,9 @@ 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; @@ -551,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); @@ -623,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); @@ -680,28 +772,6 @@ namespace { } - template - int generate_piece_moves(const Position &pos, MoveStack *mlist, - Color side, Bitboard target) { - - 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.piece_attacks(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; @@ -767,41 +837,12 @@ namespace { 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; - } - 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. @@ -850,26 +891,12 @@ namespace { return n; } + template + int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned, + Bitboard blockSquares, MoveStack* mlist, int 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; - } - + 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;