X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmovegen.cpp;h=ff8f6c6e16f6f2f186e27dad9972f43b8cf9bb1f;hb=2a461b4b745b2542f6e13bab8c60abdb366bc128;hp=8a03acd68b566e29c7b3ae5247c2868b2d5d9284;hpb=d63ff85a43667f8e0de7b7e163cba02193a70815;p=stockfish diff --git a/src/movegen.cpp b/src/movegen.cpp index 8a03acd6..ff8f6c6e 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -29,7 +29,7 @@ // Simple macro to wrap a very common while loop, no facny, no flexibility, // hardcoded list name 'mlist' and from square 'from'. -#define SERIALIZE_MOVES(b, bsf) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b)) +#define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b)) //// //// Local definitions @@ -83,22 +83,22 @@ namespace { } // Template generate_piece_checks() with specializations - template + template MoveStack* generate_piece_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_piece_checks(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) { return (us == WHITE ? generate_pawn_checks(p, dc, ksq, m) : generate_pawn_checks(p, dc, ksq, m)); } // Template generate_piece_moves() with specializations and overloads - template + template MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard); template<> - MoveStack* generate_piece_moves(const Position&, MoveStack*, Color, Bitboard); + MoveStack* generate_piece_moves(const Position&, MoveStack*, Color, Bitboard); template inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) { @@ -113,12 +113,12 @@ namespace { : generate_pawn_noncaptures(p, m)); } - template + template MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard); template<> - inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, - Color us, Bitboard t, Bitboard pnd) { + inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, + Color us, Bitboard t, Bitboard pnd) { return (us == WHITE ? generate_pawn_blocking_evasions(p, pnd, t, m) : generate_pawn_blocking_evasions(p, pnd, t, m)); @@ -132,8 +132,8 @@ namespace { /// generate_captures generates() all pseudo-legal captures and queen -/// promotions. The return value is the number of moves generated. -template +/// promotions. The return value is the number of moves generated. + int generate_captures(const Position& pos, MoveStack* mlist) { assert(pos.is_ok()); @@ -143,25 +143,19 @@ int generate_captures(const Position& pos, MoveStack* mlist) { Bitboard target = pos.pieces_of_color(opposite_color(us)); MoveStack* mlist_start = mlist; - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); mlist = generate_piece_moves(pos, mlist, us); - mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); return int(mlist - mlist_start); } -int generate_captures(const Position& pos, MoveStack* mlist) { - - return CpuHasPOPCNT ? generate_captures(pos, mlist) - : generate_captures(pos, mlist); -} - /// generate_noncaptures() generates all pseudo-legal non-captures and /// underpromotions. The return value is the number of moves generated. -template + int generate_noncaptures(const Position& pos, MoveStack* mlist) { assert(pos.is_ok()); @@ -172,26 +166,20 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) { MoveStack* mlist_start = mlist; mlist = generate_piece_moves(pos, mlist, us); - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); - mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); mlist = generate_castle_moves(pos, mlist); mlist = generate_castle_moves(pos, mlist); return int(mlist - mlist_start); } -int generate_noncaptures(const Position& pos, MoveStack* mlist) { - - return CpuHasPOPCNT ? generate_noncaptures(pos, mlist) - : generate_noncaptures(pos, mlist); -} - /// generate_non_capture_checks() generates all pseudo-legal non-capturing, /// non-promoting checks. It returns the number of generated moves. -template + int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { assert(pos.is_ok()); @@ -204,12 +192,12 @@ int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard 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); + 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) @@ -225,17 +213,11 @@ int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard return int(mlist - mlist_start); } -int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { - - return CpuHasPOPCNT ? generate_non_capture_checks(pos, mlist, dc) - : generate_non_capture_checks(pos, mlist, dc); -} - /// generate_evasions() generates all check evasions when the side to move is /// in check. Unlike the other move generation functions, this one generates /// only legal moves. It returns the number of generated moves. -template + int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { assert(pos.is_ok()); @@ -262,14 +244,14 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { Bitboard b = checkers & (pos.queens() | pos.bishops()); while (b) { - from = pop_1st_bit(&b); + from = pop_1st_bit(&b); checkersAttacks |= bishop_attacks_bb(from, b_noKing); } b = checkers & (pos.queens() | pos.rooks()); while (b) { - from = pop_1st_bit(&b); + from = pop_1st_bit(&b); checkersAttacks |= rook_attacks_bb(from, b_noKing); } @@ -277,7 +259,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { Bitboard b1 = pos.piece_attacks(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks; while (b1) { - to = pop_1st_bit(&b1); + to = pop_1st_bit(&b1); // Note that we can use square_is_attacked() only because we // have already removed slider checkers. if (!pos.square_is_attacked(to, them)) @@ -299,7 +281,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned; while (b1) { - from = pop_1st_bit(&b1); + from = pop_1st_bit(&b1); if (relative_rank(us, checksq) == RANK_8) { (*mlist++).move = make_promotion_move(from, checksq, QUEEN); @@ -317,7 +299,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { while (b1) { - from = pop_1st_bit(&b1); + from = pop_1st_bit(&b1); (*mlist++).move = make_move(from, checksq); } @@ -331,11 +313,11 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { if (blockSquares != EmptyBoardBB) { - mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); - mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); - mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); - mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); - mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); + mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); + mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); + mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); + mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); + mlist = generate_piece_moves(pos, mlist, us, blockSquares, pinned); } } @@ -357,7 +339,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { b1 &= ~pinned; while (b1) { - from = pop_1st_bit(&b1); + from = pop_1st_bit(&b1); // Move is always legal because checking pawn is not a discovered // check candidate and our capturing pawn has been already tested // against pinned pieces. @@ -368,12 +350,6 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { return int(mlist - mlist_start); } -int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { - - return CpuHasPOPCNT ? generate_evasions(pos, mlist, pinned) - : generate_evasions(pos, mlist, pinned); -} - /// generate_legal_moves() computes a complete list of legal moves in the /// current position. This function is not very fast, and should be used @@ -417,7 +393,6 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) { 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); @@ -426,6 +401,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) { if (color_of_piece(pc) != us) return false; + Color them = opposite_color(us); Square to = move_to(m); // En passant moves @@ -530,7 +506,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) { // be a promotion. if ( ( (square_rank(to) == RANK_8 && us == WHITE) ||(square_rank(to) == RANK_1 && us != WHITE)) - && !move_promotion(m)) + && !move_is_promotion(m)) return false; // Proceed according to the square delta between the source and @@ -584,13 +560,13 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) { // Luckly we can handle all the other pieces in one go return ( pos.piece_attacks_square(pos.piece_on(from), from, to) && pos.pl_move_is_legal(m, pinned) - && !move_promotion(m)); + && !move_is_promotion(m)); } namespace { - template + template MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { Square from; @@ -600,12 +576,12 @@ namespace { { from = pos.piece_list(us, Piece, i); b = pos.piece_attacks(from) & target; - SERIALIZE_MOVES(b, HasBSF); + SERIALIZE_MOVES(b); } return mlist; } - template + template MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target, Bitboard pinned) { Square from; @@ -618,19 +594,19 @@ namespace { continue; b = pos.piece_attacks(from) & target; - SERIALIZE_MOVES(b, HasBSF); + SERIALIZE_MOVES(b); } return mlist; } template<> - MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { + MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { Bitboard b; Square from = pos.king_square(us); b = pos.piece_attacks(from) & target; - SERIALIZE_MOVES(b, false); + SERIALIZE_MOVES(b); return mlist; } @@ -815,9 +791,11 @@ namespace { // Direct checks. These are possible only for pawns on neighboring files // and in the two ranks that, after the push, are in front of the enemy king. b1 = pawns & neighboring_files_bb(ksq) & ~dc; - b2 = rank_bb(ksq + 2 * TDELTA_S) | rank_bb(ksq + 3 * TDELTA_S); - b1 &= b2; - if (!b1) + + // We can get false positives if (ksq + x) is not in [0,63] range but + // is not a problem, they will be filtered out later. + b2 = b1 & (rank_bb(ksq + 2 * TDELTA_S) | rank_bb(ksq + 3 * TDELTA_S)); + if (!b2) return mlist; // Direct checks, single pawn pushes @@ -840,7 +818,7 @@ namespace { return mlist; } - template + template MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us, Bitboard dc, Square ksq) { @@ -850,12 +828,12 @@ namespace { Bitboard b = target & dc; while (b) { - Square from = pop_1st_bit(&b); + Square from = pop_1st_bit(&b); Bitboard bb = pos.piece_attacks(from) & pos.empty_squares(); if (Piece == KING) bb &= ~QueenPseudoAttacks[ksq]; - SERIALIZE_MOVES(bb, HasBSF); + SERIALIZE_MOVES(bb); } // Direct checks @@ -868,14 +846,14 @@ namespace { while (b) { - Square from = pop_1st_bit(&b); + Square from = pop_1st_bit(&b); if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs)) || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs)) || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs))) continue; Bitboard bb = pos.piece_attacks(from) & checkSqs; - SERIALIZE_MOVES(bb, HasBSF); + SERIALIZE_MOVES(bb); } } return mlist; @@ -886,7 +864,7 @@ namespace { Bitboard blockSquares, MoveStack* mlist) { // Calculate our parametrized parameters at compile time - const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB); + const Rank TRANK_8 = (Us == WHITE ? RANK_8 : RANK_1); const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S); @@ -904,7 +882,7 @@ namespace { assert(pos.piece_on(to) == EMPTY); - if (square_rank(to) == TRank8BB) + if (square_rank(to) == TRANK_8) { (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN); (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);