X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fmovegen.cpp;h=259d276b3d79f122de26a1ad4c826fd671498bf4;hp=aecf28f4bacc05b54519b3661682454bd12d0bf6;hb=f036239521fe4f6afb7e8cbc51d860ffa476f6bd;hpb=987ff3b4b6254bc4696a579c15c8e6eeab3425c8 diff --git a/src/movegen.cpp b/src/movegen.cpp index aecf28f4..259d276b 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -43,24 +43,33 @@ 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_castle_moves(const Position&, MoveStack*, Color); + int generate_pawn_blocking_evasions(const PawnOffsets&, 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); } @@ -86,9 +95,11 @@ int generate_captures(const Position& pos, MoveStack* 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_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; } @@ -110,8 +121,11 @@ int generate_noncaptures(const Position& pos, MoveStack *mlist) { else n = generate_pawn_noncaptures(BlackPawnOffsets, 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; @@ -144,25 +158,25 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { // 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]; + b = pos.piece_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq]; while (b) { Square to = pop_1st_bit(&b); @@ -195,7 +209,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); @@ -210,9 +224,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); } @@ -249,10 +263,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) { @@ -269,131 +282,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(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(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(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(pos, b1, blockSquares, mlist, n); } // Finally, the ugly special case of en passant captures. An en passant @@ -770,18 +680,18 @@ namespace { } - int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist, + template + int generate_piece_moves(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++) + for (int i = 0; i < pos.piece_count(side, Piece); i++) { - from = pos.piece_list(side, piece, i); - b = (pos.*mem_fn)(from) & target; + from = pos.piece_list(side, Piece, i); + b = pos.piece_attacks(from) & target; while (b) { to = pop_1st_bit(&b); @@ -857,17 +767,16 @@ 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]; + 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.*mem_fn)(from) & pos.empty_squares(); + Bitboard bb = pos.piece_attacks(from) & pos.empty_squares(); while (bb) { Square to = pop_1st_bit(&bb); @@ -877,11 +786,11 @@ namespace { // Direct checks b = target & ~dc; - Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares(); + Bitboard checkSqs = pos.piece_attacks(ksq) & pos.empty_squares(); while (b) { Square from = pop_1st_bit(&b); - Bitboard bb = (pos.*mem_fn)(from) & checkSqs; + Bitboard bb = pos.piece_attacks(from) & checkSqs; while (bb) { Square to = pop_1st_bit(&bb); @@ -940,4 +849,61 @@ namespace { } 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; + } + + + 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; + } }