2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008 Marco Costalba
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 //// Local definitions
36 Bitboard Rank3BB, Rank8BB;
38 SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
41 const PawnParams WhitePawnParams = { Rank3BB, Rank8BB, RANK_8, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK };
42 const PawnParams BlackPawnParams = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE };
44 int generate_castle_moves(const Position&, MoveStack*, Color);
47 int generate_pawn_captures(const Position&, MoveStack*);
50 int generate_pawn_noncaptures(const Position&, MoveStack*);
53 int generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*, int);
56 int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
59 int generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
62 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
65 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
68 /// Templates with specializations are defined here to avoid lookup issues
70 template<PieceType Piece>
71 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
72 Square ksq, MoveStack* mlist, int n) {
74 Bitboard b = target & dc;
77 Square from = pop_1st_bit(&b);
78 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
81 Square to = pop_1st_bit(&bb);
82 mlist[n++].move = make_move(from, to);
87 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
90 Square from = pop_1st_bit(&b);
91 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
94 Square to = pop_1st_bit(&bb);
95 mlist[n++].move = make_move(from, to);
102 template<> // Special case the King
103 int generate_piece_checks<KING>(const Position& pos, Bitboard, Bitboard dc,
104 Square ksq, MoveStack* mlist, int n) {
105 if (bit_is_set(dc, ksq))
107 Bitboard bb = pos.piece_attacks<KING>(ksq)
108 & pos.empty_squares()
109 & ~QueenPseudoAttacks[ksq];
112 Square to = pop_1st_bit(&bb);
113 mlist[n++].move = make_move(ksq, to);
126 /// generate_captures generates() all pseudo-legal captures and queen
127 /// promotions. The return value is the number of moves generated.
129 int generate_captures(const Position& pos, MoveStack* mlist) {
132 assert(!pos.is_check());
134 Color us = pos.side_to_move();
135 Bitboard target = pos.pieces_of_color(opposite_color(us));
139 n = generate_pawn_captures<WHITE>(pos, mlist);
141 n = generate_pawn_captures<BLACK>(pos, mlist);
143 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
144 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
145 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
146 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
147 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
152 /// generate_noncaptures() generates all pseudo-legal non-captures and
153 /// underpromotions. The return value is the number of moves generated.
155 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
158 assert(!pos.is_check());
160 Color us = pos.side_to_move();
161 Bitboard target = pos.empty_squares();
165 n = generate_pawn_noncaptures<WHITE>(pos, mlist);
167 n = generate_pawn_noncaptures<BLACK>(pos, mlist);
169 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
170 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
171 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
172 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
173 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
175 n += generate_castle_moves(pos, mlist+n, us);
180 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
181 /// checks, except castling moves (will add this later). It returns the
182 /// number of generated moves.
184 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
187 assert(!pos.is_check());
190 Color us = pos.side_to_move();
191 Square ksq = pos.king_square(opposite_color(us));
193 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
195 dc = pos.discovered_check_candidates(us);
199 n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist, 0);
201 n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist, 0);
204 Bitboard b = pos.knights(us);
206 n = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist, n);
210 n = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist, n);
214 n = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist, n);
218 n = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist, n);
220 // Hopefully we always have a king ;-)
221 n = generate_piece_checks<KING>(pos, b, dc, pos.king_square(us), mlist, n);
223 // TODO: Castling moves!
229 /// generate_evasions() generates all check evasions when the side to move is
230 /// in check. Unlike the other move generation functions, this one generates
231 /// only legal moves. It returns the number of generated moves. This
232 /// function is very ugly, and needs cleaning up some time later. FIXME
234 int generate_evasions(const Position& pos, MoveStack* mlist) {
237 assert(pos.is_check());
239 Color us = pos.side_to_move();
240 Color them = opposite_color(us);
241 Square ksq = pos.king_square(us);
245 assert(pos.piece_on(ksq) == king_of_color(us));
247 // Generate evasions for king
248 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
249 Bitboard b2 = pos.occupied_squares();
254 Square to = pop_1st_bit(&b1);
256 // Make sure to is not attacked by the other side. This is a bit ugly,
257 // because we can't use Position::square_is_attacked. Instead we use
258 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
259 // b2 (the occupied squares with the king removed) in order to test whether
260 // the king will remain in check on the destination square.
261 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
262 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
263 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
264 || (pos.pawn_attacks(us, to) & pos.pawns(them))
265 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
267 mlist[n++].move = make_move(ksq, to);
270 // Generate evasions for other pieces only if not double check. We use a
271 // simple bit twiddling hack here rather than calling count_1s in order to
272 // save some time (we know that pos.checkers() has at most two nonzero bits).
273 Bitboard checkers = pos.checkers();
275 if (!(checkers & (checkers - 1))) // Only one bit set?
277 Square checksq = first_1(checkers);
279 assert(pos.color_of_piece_on(checksq) == them);
281 // Find pinned pieces
282 Bitboard not_pinned = ~pos.pinned_pieces(us);
284 // Generate captures of the checking piece
287 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
290 from = pop_1st_bit(&b1);
291 if (relative_rank(us, checksq) == RANK_8)
293 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
294 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
295 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
296 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
298 mlist[n++].move = make_move(from, checksq);
302 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
303 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
304 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
308 from = pop_1st_bit(&b1);
309 mlist[n++].move = make_move(from, checksq);
312 // Blocking check evasions are possible only if the checking piece is
314 if (checkers & pos.sliders())
316 Bitboard blockSquares = squares_between(checksq, ksq);
318 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
320 // Pawn moves. Because a blocking evasion can never be a capture, we
321 // only generate pawn pushes.
323 n = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist, n);
325 n = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist, n);
328 b1 = pos.knights(us) & not_pinned;
330 n = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist, n);
332 b1 = pos.bishops(us) & not_pinned;
334 n = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist, n);
336 b1 = pos.rooks(us) & not_pinned;
338 n = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist, n);
340 b1 = pos.queens(us) & not_pinned;
342 n = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist, n);
345 // Finally, the ugly special case of en passant captures. An en passant
346 // capture can only be a check evasion if the check is not a discovered
347 // check. If pos.ep_square() is set, the last move made must have been
348 // a double pawn push. If, furthermore, the checking piece is a pawn,
349 // an en passant check evasion may be possible.
350 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
352 to = pos.ep_square();
353 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
355 assert(b1 != EmptyBoardBB);
360 from = pop_1st_bit(&b1);
362 // Before generating the move, we have to make sure it is legal.
363 // This is somewhat tricky, because the two disappearing pawns may
364 // cause new "discovered checks". We test this by removing the
365 // two relevant bits from the occupied squares bitboard, and using
366 // the low-level bitboard functions for bishop and rook attacks.
367 b2 = pos.occupied_squares();
368 clear_bit(&b2, from);
369 clear_bit(&b2, checksq);
370 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
371 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
373 mlist[n++].move = make_ep_move(from, to);
381 /// generate_legal_moves() computes a complete list of legal moves in the
382 /// current position. This function is not very fast, and should be used
383 /// only in situations where performance is unimportant. It wouldn't be
384 /// very hard to write an efficient legal move generator, but for the moment
385 /// we don't need it.
387 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
392 return generate_evasions(pos, mlist);
394 // Generate pseudo-legal moves:
395 int n = generate_captures(pos, mlist);
396 n += generate_noncaptures(pos, mlist + n);
398 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
400 // Remove illegal moves from the list:
401 for (int i = 0; i < n; i++)
402 if (!pos.move_is_legal(mlist[i].move, pinned))
403 mlist[i--].move = mlist[--n].move;
409 /// generate_move_if_legal() takes a position and a (not necessarily
410 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
411 /// whether the move is legal. If the move is legal, the move itself is
412 /// returned. If not, the function returns MOVE_NONE. This function must
413 /// only be used when the side to move is not in check.
415 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
418 assert(!pos.is_check());
419 assert(move_is_ok(m));
421 Color us = pos.side_to_move();
422 Color them = opposite_color(us);
423 Square from = move_from(m);
424 Piece pc = pos.piece_on(from);
426 // If the from square is not occupied by a piece belonging to the side to
427 // move, the move is obviously not legal.
428 if (color_of_piece(pc) != us)
431 Square to = move_to(m);
436 // The piece must be a pawn and destination square must be the
437 // en passant square.
438 if ( type_of_piece(pc) != PAWN
439 || to != pos.ep_square())
442 assert(pos.square_is_empty(to));
443 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
445 // The move is pseudo-legal. If it is legal, return it.
446 return (pos.move_is_legal(m) ? m : MOVE_NONE);
450 if (move_is_short_castle(m))
452 // The piece must be a king and side to move must still have
453 // the right to castle kingside.
454 if ( type_of_piece(pc) != KING
455 ||!pos.can_castle_kingside(us))
458 assert(from == pos.king_square(us));
459 assert(to == pos.initial_kr_square(us));
460 assert(pos.piece_on(to) == rook_of_color(us));
462 Square g1 = relative_square(us, SQ_G1);
463 Square f1 = relative_square(us, SQ_F1);
465 bool illegal = false;
467 // Check if any of the squares between king and rook
468 // is occupied or under attack.
469 for (s = Min(from, g1); s <= Max(from, g1); s++)
470 if ( (s != from && s != to && !pos.square_is_empty(s))
471 || pos.square_is_attacked(s, them))
474 // Check if any of the squares between king and rook
476 for (s = Min(to, f1); s <= Max(to, f1); s++)
477 if (s != from && s != to && !pos.square_is_empty(s))
480 return (!illegal ? m : MOVE_NONE);
483 if (move_is_long_castle(m))
485 // The piece must be a king and side to move must still have
486 // the right to castle kingside.
487 if ( type_of_piece(pc) != KING
488 ||!pos.can_castle_queenside(us))
491 assert(from == pos.king_square(us));
492 assert(to == pos.initial_qr_square(us));
493 assert(pos.piece_on(to) == rook_of_color(us));
495 Square c1 = relative_square(us, SQ_C1);
496 Square d1 = relative_square(us, SQ_D1);
498 bool illegal = false;
500 for (s = Min(from, c1); s <= Max(from, c1); s++)
501 if( (s != from && s != to && !pos.square_is_empty(s))
502 || pos.square_is_attacked(s, them))
505 for (s = Min(to, d1); s <= Max(to, d1); s++)
506 if(s != from && s != to && !pos.square_is_empty(s))
509 if ( square_file(to) == FILE_B
510 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
511 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
514 return (!illegal ? m : MOVE_NONE);
519 // The destination square cannot be occupied by a friendly piece
520 if (pos.color_of_piece_on(to) == us)
523 // Proceed according to the type of the moving piece.
524 if (type_of_piece(pc) == PAWN)
526 // If the destination square is on the 8/1th rank, the move must
528 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
529 ||(square_rank(to) == RANK_1 && us != WHITE))
530 && !move_promotion(m))
533 // Proceed according to the square delta between the source and
534 // destionation squares.
541 // Capture. The destination square must be occupied by an enemy
542 // piece (en passant captures was handled earlier).
543 if (pos.color_of_piece_on(to) != them)
549 // Pawn push. The destination square must be empty.
550 if (!pos.square_is_empty(to))
555 // Double white pawn push. The destination square must be on the fourth
556 // rank, and both the destination square and the square between the
557 // source and destination squares must be empty.
558 if ( square_rank(to) != RANK_4
559 || !pos.square_is_empty(to)
560 || !pos.square_is_empty(from + DELTA_N))
565 // Double black pawn push. The destination square must be on the fifth
566 // rank, and both the destination square and the square between the
567 // source and destination squares must be empty.
568 if ( square_rank(to) != RANK_5
569 || !pos.square_is_empty(to)
570 || !pos.square_is_empty(from + DELTA_S))
577 // The move is pseudo-legal. Return it if it is legal.
578 return (pos.move_is_legal(m) ? m : MOVE_NONE);
581 // Luckly we can handle all the other pieces in one go
582 return ( pos.piece_attacks_square(from, to)
583 && pos.move_is_legal(m)
584 && !move_promotion(m) ? m : MOVE_NONE);
590 template<PieceType Piece>
591 int generate_piece_moves(const Position &pos, MoveStack *mlist,
592 Color side, Bitboard target) {
594 for (int i = 0; i < pos.piece_count(side, Piece); i++)
596 Square from = pos.piece_list(side, Piece, i);
597 Bitboard b = pos.piece_attacks<Piece>(from) & target;
600 Square to = pop_1st_bit(&b);
601 mlist[n++].move = make_move(from, to);
608 template<PieceType Piece>
609 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
610 Bitboard blockSquares, MoveStack* mlist, int n) {
613 Square from = pop_1st_bit(&b);
614 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
617 Square to = pop_1st_bit(&bb);
618 mlist[n++].move = make_move(from, to);
626 int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
628 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
630 Bitboard pawns = pos.pawns(PP.us);
631 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
635 // Captures in the a1-h8 (a8-h1 for black) direction
636 Bitboard b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
638 // Capturing promotions
639 Bitboard b2 = b1 & PP.Rank8BB;
642 sq = pop_1st_bit(&b2);
643 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, QUEEN);
646 // Capturing non-promotions
647 b2 = b1 & ~PP.Rank8BB;
650 sq = pop_1st_bit(&b2);
651 mlist[n++].move = make_move(sq - PP.DELTA_NE, sq);
654 // Captures in the h1-a8 (h8-a1 for black) direction
655 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
657 // Capturing promotions
658 b2 = b1 & PP.Rank8BB;
661 sq = pop_1st_bit(&b2);
662 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, QUEEN);
665 // Capturing non-promotions
666 b2 = b1 & ~PP.Rank8BB;
669 sq = pop_1st_bit(&b2);
670 mlist[n++].move = make_move(sq - PP.DELTA_NW, sq);
673 // Non-capturing promotions
674 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & Rank8BB;
677 sq = pop_1st_bit(&b1);
678 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, QUEEN);
681 // En passant captures
682 if (pos.ep_square() != SQ_NONE)
684 assert(PP.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
685 assert(PP.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
687 b1 = pawns & pos.pawn_attacks(PP.them, pos.ep_square());
688 assert(b1 != EmptyBoardBB);
692 sq = pop_1st_bit(&b1);
693 mlist[n++].move = make_ep_move(sq, pos.ep_square());
701 int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
703 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
705 Bitboard pawns = pos.pawns(PP.us);
706 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
707 Bitboard emptySquares = pos.empty_squares();
712 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
713 b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & PP.Rank8BB;
716 sq = pop_1st_bit(&b1);
717 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, ROOK);
718 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, BISHOP);
719 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, KNIGHT);
722 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
723 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & PP.Rank8BB;
726 sq = pop_1st_bit(&b1);
727 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, ROOK);
728 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, BISHOP);
729 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, KNIGHT);
732 // Single pawn pushes
733 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
734 b2 = b1 & PP.Rank8BB;
737 sq = pop_1st_bit(&b2);
738 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, ROOK);
739 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, BISHOP);
740 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, KNIGHT);
742 b2 = b1 & ~PP.Rank8BB;
745 sq = pop_1st_bit(&b2);
746 mlist[n++].move = make_move(sq - PP.DELTA_N, sq);
749 // Double pawn pushes
750 b2 = (C == WHITE ? (b1 & PP.Rank3BB) << 8 : (b1 & PP.Rank3BB) >> 8) & emptySquares;
753 sq = pop_1st_bit(&b2);
754 mlist[n++].move = make_move(sq - PP.DELTA_N - PP.DELTA_N, sq);
761 int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
763 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
765 // Pawn moves which give discovered check. This is possible only if the
766 // pawn is not on the same file as the enemy king, because we don't
767 // generate captures.
768 Bitboard empty = pos.empty_squares();
770 // Find all friendly pawns not on the enemy king's file
771 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
773 // Discovered checks, single pawn pushes
774 b2 = b3 = (C == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~PP.Rank8BB & empty;
777 Square to = pop_1st_bit(&b3);
778 mlist[n++].move = make_move(to - PP.DELTA_N, to);
781 // Discovered checks, double pawn pushes
782 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8) & empty;
785 Square to = pop_1st_bit(&b3);
786 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
789 // Direct checks. These are possible only for pawns on neighboring files
792 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
794 // Direct checks, single pawn pushes
795 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & empty;
796 b3 = b2 & pos.pawn_attacks(PP.them, ksq);
799 Square to = pop_1st_bit(&b3);
800 mlist[n++].move = make_move(to - PP.DELTA_N, to);
803 // Direct checks, double pawn pushes
804 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8)
806 & pos.pawn_attacks(PP.them, ksq);
810 Square to = pop_1st_bit(&b3);
811 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
818 int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
819 Bitboard blockSquares, MoveStack* mlist, int n) {
821 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
823 // Find non-pinned pawns
824 Bitboard b1 = pos.pawns(PP.us) & not_pinned;
826 // Single pawn pushes. We don't have to AND with empty squares here,
827 // because the blocking squares will always be empty.
828 Bitboard b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
831 Square to = pop_1st_bit(&b2);
833 assert(pos.piece_on(to) == EMPTY);
835 if (square_rank(to) == PP.RANK_8)
837 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, QUEEN);
838 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, ROOK);
839 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, BISHOP);
840 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, KNIGHT);
842 mlist[n++].move = make_move(to - PP.DELTA_N, to);
845 // Double pawn pushes
846 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & PP.Rank3BB;
847 b2 = (C == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
850 Square to = pop_1st_bit(&b2);
852 assert(pos.piece_on(to) == EMPTY);
853 assert(PP.us != WHITE || square_rank(to) == RANK_4);
854 assert(PP.us != BLACK || square_rank(to) == RANK_5);
856 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
862 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
866 if (pos.can_castle(us))
868 Color them = opposite_color(us);
869 Square ksq = pos.king_square(us);
871 assert(pos.piece_on(ksq) == king_of_color(us));
873 if (pos.can_castle_kingside(us))
875 Square rsq = pos.initial_kr_square(us);
876 Square g1 = relative_square(us, SQ_G1);
877 Square f1 = relative_square(us, SQ_F1);
879 bool illegal = false;
881 assert(pos.piece_on(rsq) == rook_of_color(us));
883 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
884 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
885 || pos.square_is_attacked(s, them))
888 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
889 if (s != ksq && s != rsq && pos.square_is_occupied(s))
893 mlist[n++].move = make_castle_move(ksq, rsq);
896 if (pos.can_castle_queenside(us))
898 Square rsq = pos.initial_qr_square(us);
899 Square c1 = relative_square(us, SQ_C1);
900 Square d1 = relative_square(us, SQ_D1);
902 bool illegal = false;
904 assert(pos.piece_on(rsq) == rook_of_color(us));
906 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
907 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
908 || pos.square_is_attacked(s, them))
911 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
912 if (s != ksq && s != rsq && pos.square_is_occupied(s))
915 if ( square_file(rsq) == FILE_B
916 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
917 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
921 mlist[n++].move = make_castle_move(ksq, rsq);