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*);
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*);
56 int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
59 int generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
61 int generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
64 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*);
65 int generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
68 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
77 /// generate_captures generates() all pseudo-legal captures and queen
78 /// promotions. The return value is the number of moves generated.
80 int generate_captures(const Position& pos, MoveStack* mlist) {
83 assert(!pos.is_check());
85 Color us = pos.side_to_move();
86 Bitboard target = pos.pieces_of_color(opposite_color(us));
90 n = generate_pawn_captures<WHITE>(pos, mlist);
92 n = generate_pawn_captures<BLACK>(pos, mlist);
94 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
95 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
96 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
97 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
98 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
103 /// generate_noncaptures() generates all pseudo-legal non-captures and
104 /// underpromotions. The return value is the number of moves generated.
106 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
109 assert(!pos.is_check());
111 Color us = pos.side_to_move();
112 Bitboard target = pos.empty_squares();
116 n = generate_pawn_noncaptures<WHITE>(pos, mlist);
118 n = generate_pawn_noncaptures<BLACK>(pos, mlist);
120 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
121 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
122 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
123 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
124 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
125 n += generate_castle_moves(pos, mlist+n);
130 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
131 /// checks, except castling moves (will add this later). It returns the
132 /// number of generated moves.
134 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
137 assert(!pos.is_check());
140 Color us = pos.side_to_move();
141 Square ksq = pos.king_square(opposite_color(us));
143 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
145 dc = pos.discovered_check_candidates(us);
149 n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
151 n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
154 Bitboard b = pos.knights(us);
156 n += generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist+n);
160 n += generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist+n);
164 n += generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist+n);
168 n += generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist+n);
170 // Hopefully we always have a king ;-)
171 n += generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist+n);
173 // TODO: Castling moves!
179 /// generate_evasions() generates all check evasions when the side to move is
180 /// in check. Unlike the other move generation functions, this one generates
181 /// only legal moves. It returns the number of generated moves. This
182 /// function is very ugly, and needs cleaning up some time later. FIXME
184 int generate_evasions(const Position& pos, MoveStack* mlist) {
187 assert(pos.is_check());
189 Color us = pos.side_to_move();
190 Color them = opposite_color(us);
191 Square ksq = pos.king_square(us);
195 assert(pos.piece_on(ksq) == king_of_color(us));
197 // Generate evasions for king
198 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
199 Bitboard b2 = pos.occupied_squares();
204 to = pop_1st_bit(&b1);
206 // Make sure to is not attacked by the other side. This is a bit ugly,
207 // because we can't use Position::square_is_attacked. Instead we use
208 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
209 // b2 (the occupied squares with the king removed) in order to test whether
210 // the king will remain in check on the destination square.
211 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
212 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
213 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
214 || (pos.pawn_attacks(us, to) & pos.pawns(them))
215 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
217 mlist[n++].move = make_move(ksq, to);
220 // Generate evasions for other pieces only if not double check. We use a
221 // simple bit twiddling hack here rather than calling count_1s in order to
222 // save some time (we know that pos.checkers() has at most two nonzero bits).
223 Bitboard checkers = pos.checkers();
225 if (!(checkers & (checkers - 1))) // Only one bit set?
227 Square checksq = first_1(checkers);
229 assert(pos.color_of_piece_on(checksq) == them);
231 // Find pinned pieces
232 Bitboard not_pinned = ~pos.pinned_pieces(us);
234 // Generate captures of the checking piece
237 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
240 from = pop_1st_bit(&b1);
241 if (relative_rank(us, checksq) == RANK_8)
243 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
244 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
245 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
246 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
248 mlist[n++].move = make_move(from, checksq);
252 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
253 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
254 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
258 from = pop_1st_bit(&b1);
259 mlist[n++].move = make_move(from, checksq);
262 // Blocking check evasions are possible only if the checking piece is
264 if (checkers & pos.sliders())
266 Bitboard blockSquares = squares_between(checksq, ksq);
268 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
270 // Pawn moves. Because a blocking evasion can never be a capture, we
271 // only generate pawn pushes.
273 n += generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist+n);
275 n += generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist+n);
278 b1 = pos.knights(us) & not_pinned;
280 n += generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist+n);
282 b1 = pos.bishops(us) & not_pinned;
284 n += generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist+n);
286 b1 = pos.rooks(us) & not_pinned;
288 n += generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist+n);
290 b1 = pos.queens(us) & not_pinned;
292 n += generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist+n);
295 // Finally, the ugly special case of en passant captures. An en passant
296 // capture can only be a check evasion if the check is not a discovered
297 // check. If pos.ep_square() is set, the last move made must have been
298 // a double pawn push. If, furthermore, the checking piece is a pawn,
299 // an en passant check evasion may be possible.
300 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
302 to = pos.ep_square();
303 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
305 assert(b1 != EmptyBoardBB);
310 from = pop_1st_bit(&b1);
312 // Before generating the move, we have to make sure it is legal.
313 // This is somewhat tricky, because the two disappearing pawns may
314 // cause new "discovered checks". We test this by removing the
315 // two relevant bits from the occupied squares bitboard, and using
316 // the low-level bitboard functions for bishop and rook attacks.
317 b2 = pos.occupied_squares();
318 clear_bit(&b2, from);
319 clear_bit(&b2, checksq);
320 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
321 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
323 mlist[n++].move = make_ep_move(from, to);
331 /// generate_legal_moves() computes a complete list of legal moves in the
332 /// current position. This function is not very fast, and should be used
333 /// only in situations where performance is unimportant. It wouldn't be
334 /// very hard to write an efficient legal move generator, but for the moment
335 /// we don't need it.
337 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
342 return generate_evasions(pos, mlist);
344 // Generate pseudo-legal moves:
345 int n = generate_captures(pos, mlist);
346 n += generate_noncaptures(pos, mlist + n);
348 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
350 // Remove illegal moves from the list:
351 for (int i = 0; i < n; i++)
352 if (!pos.move_is_legal(mlist[i].move, pinned))
353 mlist[i--].move = mlist[--n].move;
359 /// generate_move_if_legal() takes a position and a (not necessarily
360 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
361 /// whether the move is legal. If the move is legal, the move itself is
362 /// returned. If not, the function returns MOVE_NONE. This function must
363 /// only be used when the side to move is not in check.
365 Move generate_move_if_legal(const Position& pos, Move m, Bitboard pinned) {
368 assert(!pos.is_check());
369 assert(move_is_ok(m));
371 Color us = pos.side_to_move();
372 Color them = opposite_color(us);
373 Square from = move_from(m);
374 Piece pc = pos.piece_on(from);
376 // If the from square is not occupied by a piece belonging to the side to
377 // move, the move is obviously not legal.
378 if (color_of_piece(pc) != us)
381 Square to = move_to(m);
386 // The piece must be a pawn and destination square must be the
387 // en passant square.
388 if ( type_of_piece(pc) != PAWN
389 || to != pos.ep_square())
392 assert(pos.square_is_empty(to));
393 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
395 // The move is pseudo-legal. If it is legal, return it.
396 return (pos.move_is_legal(m) ? m : MOVE_NONE);
400 if (move_is_short_castle(m))
402 // The piece must be a king and side to move must still have
403 // the right to castle kingside.
404 if ( type_of_piece(pc) != KING
405 ||!pos.can_castle_kingside(us))
408 assert(from == pos.king_square(us));
409 assert(to == pos.initial_kr_square(us));
410 assert(pos.piece_on(to) == rook_of_color(us));
412 Square g1 = relative_square(us, SQ_G1);
413 Square f1 = relative_square(us, SQ_F1);
415 bool illegal = false;
417 // Check if any of the squares between king and rook
418 // is occupied or under attack.
419 for (s = Min(from, g1); s <= Max(from, g1); s++)
420 if ( (s != from && s != to && !pos.square_is_empty(s))
421 || pos.square_is_attacked(s, them))
424 // Check if any of the squares between king and rook
426 for (s = Min(to, f1); s <= Max(to, f1); s++)
427 if (s != from && s != to && !pos.square_is_empty(s))
430 return (!illegal ? m : MOVE_NONE);
433 if (move_is_long_castle(m))
435 // The piece must be a king and side to move must still have
436 // the right to castle kingside.
437 if ( type_of_piece(pc) != KING
438 ||!pos.can_castle_queenside(us))
441 assert(from == pos.king_square(us));
442 assert(to == pos.initial_qr_square(us));
443 assert(pos.piece_on(to) == rook_of_color(us));
445 Square c1 = relative_square(us, SQ_C1);
446 Square d1 = relative_square(us, SQ_D1);
448 bool illegal = false;
450 for (s = Min(from, c1); s <= Max(from, c1); s++)
451 if( (s != from && s != to && !pos.square_is_empty(s))
452 || pos.square_is_attacked(s, them))
455 for (s = Min(to, d1); s <= Max(to, d1); s++)
456 if(s != from && s != to && !pos.square_is_empty(s))
459 if ( square_file(to) == FILE_B
460 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
461 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
464 return (!illegal ? m : MOVE_NONE);
469 // The destination square cannot be occupied by a friendly piece
470 if (pos.color_of_piece_on(to) == us)
473 // Proceed according to the type of the moving piece.
474 if (type_of_piece(pc) == PAWN)
476 // If the destination square is on the 8/1th rank, the move must
478 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
479 ||(square_rank(to) == RANK_1 && us != WHITE))
480 && !move_promotion(m))
483 // Proceed according to the square delta between the source and
484 // destionation squares.
491 // Capture. The destination square must be occupied by an enemy
492 // piece (en passant captures was handled earlier).
493 if (pos.color_of_piece_on(to) != them)
499 // Pawn push. The destination square must be empty.
500 if (!pos.square_is_empty(to))
505 // Double white pawn push. The destination square must be on the fourth
506 // rank, and both the destination square and the square between the
507 // source and destination squares must be empty.
508 if ( square_rank(to) != RANK_4
509 || !pos.square_is_empty(to)
510 || !pos.square_is_empty(from + DELTA_N))
515 // Double black pawn push. The destination square must be on the fifth
516 // rank, and both the destination square and the square between the
517 // source and destination squares must be empty.
518 if ( square_rank(to) != RANK_5
519 || !pos.square_is_empty(to)
520 || !pos.square_is_empty(from + DELTA_S))
527 // The move is pseudo-legal. Return it if it is legal.
528 return (pos.move_is_legal(m) ? m : MOVE_NONE);
531 // Luckly we can handle all the other pieces in one go
532 return ( pos.piece_attacks_square(from, to)
533 && pos.move_is_legal(m)
534 && !move_promotion(m) ? m : MOVE_NONE);
540 template<PieceType Piece>
541 int generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
547 for (int i = 0; i < pos.piece_count(us, Piece); i++)
549 from = pos.piece_list(us, Piece, i);
550 b = pos.piece_attacks<Piece>(from) & target;
553 to = pop_1st_bit(&b);
554 mlist[n++].move = make_move(from, to);
561 int generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
564 Square to, from = pos.king_square(us);
567 b = pos.piece_attacks<KING>(from) & target;
570 to = pop_1st_bit(&b);
571 mlist[n++].move = make_move(from, to);
576 template<PieceType Piece>
577 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
578 Bitboard blockSquares, MoveStack* mlist) {
582 Square from = pop_1st_bit(&b);
583 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
586 Square to = pop_1st_bit(&bb);
587 mlist[n++].move = make_move(from, to);
595 int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
597 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
599 Bitboard pawns = pos.pawns(PP.us);
600 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
604 // Captures in the a1-h8 (a8-h1 for black) direction
605 Bitboard b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
607 // Capturing promotions
608 Bitboard b2 = b1 & PP.Rank8BB;
611 sq = pop_1st_bit(&b2);
612 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, QUEEN);
615 // Capturing non-promotions
616 b2 = b1 & ~PP.Rank8BB;
619 sq = pop_1st_bit(&b2);
620 mlist[n++].move = make_move(sq - PP.DELTA_NE, sq);
623 // Captures in the h1-a8 (h8-a1 for black) direction
624 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
626 // Capturing promotions
627 b2 = b1 & PP.Rank8BB;
630 sq = pop_1st_bit(&b2);
631 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, QUEEN);
634 // Capturing non-promotions
635 b2 = b1 & ~PP.Rank8BB;
638 sq = pop_1st_bit(&b2);
639 mlist[n++].move = make_move(sq - PP.DELTA_NW, sq);
642 // Non-capturing promotions
643 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & Rank8BB;
646 sq = pop_1st_bit(&b1);
647 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, QUEEN);
650 // En passant captures
651 if (pos.ep_square() != SQ_NONE)
653 assert(PP.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
654 assert(PP.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
656 b1 = pawns & pos.pawn_attacks(PP.them, pos.ep_square());
657 assert(b1 != EmptyBoardBB);
661 sq = pop_1st_bit(&b1);
662 mlist[n++].move = make_ep_move(sq, pos.ep_square());
670 int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
672 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
674 Bitboard pawns = pos.pawns(PP.us);
675 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
676 Bitboard emptySquares = pos.empty_squares();
681 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
682 b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & PP.Rank8BB;
685 sq = pop_1st_bit(&b1);
686 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, ROOK);
687 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, BISHOP);
688 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, KNIGHT);
691 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
692 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & PP.Rank8BB;
695 sq = pop_1st_bit(&b1);
696 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, ROOK);
697 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, BISHOP);
698 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, KNIGHT);
701 // Single pawn pushes
702 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
703 b2 = b1 & PP.Rank8BB;
706 sq = pop_1st_bit(&b2);
707 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, ROOK);
708 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, BISHOP);
709 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, KNIGHT);
711 b2 = b1 & ~PP.Rank8BB;
714 sq = pop_1st_bit(&b2);
715 mlist[n++].move = make_move(sq - PP.DELTA_N, sq);
718 // Double pawn pushes
719 b2 = (C == WHITE ? (b1 & PP.Rank3BB) << 8 : (b1 & PP.Rank3BB) >> 8) & emptySquares;
722 sq = pop_1st_bit(&b2);
723 mlist[n++].move = make_move(sq - PP.DELTA_N - PP.DELTA_N, sq);
730 int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
732 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
734 // Pawn moves which give discovered check. This is possible only if the
735 // pawn is not on the same file as the enemy king, because we don't
736 // generate captures.
738 Bitboard empty = pos.empty_squares();
740 // Find all friendly pawns not on the enemy king's file
741 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
743 // Discovered checks, single pawn pushes
744 b2 = b3 = (C == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~PP.Rank8BB & empty;
747 Square to = pop_1st_bit(&b3);
748 mlist[n++].move = make_move(to - PP.DELTA_N, to);
751 // Discovered checks, double pawn pushes
752 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8) & empty;
755 Square to = pop_1st_bit(&b3);
756 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
759 // Direct checks. These are possible only for pawns on neighboring files
762 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
764 // Direct checks, single pawn pushes
765 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & empty;
766 b3 = b2 & pos.pawn_attacks(PP.them, ksq);
769 Square to = pop_1st_bit(&b3);
770 mlist[n++].move = make_move(to - PP.DELTA_N, to);
773 // Direct checks, double pawn pushes
774 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8)
776 & pos.pawn_attacks(PP.them, ksq);
780 Square to = pop_1st_bit(&b3);
781 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
786 template<PieceType Piece>
787 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
788 Square ksq, MoveStack* mlist) {
791 Bitboard b = target & dc;
794 Square from = pop_1st_bit(&b);
795 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
798 Square to = pop_1st_bit(&bb);
799 mlist[n++].move = make_move(from, to);
804 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
807 Square from = pop_1st_bit(&b);
808 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
811 Square to = pop_1st_bit(&bb);
812 mlist[n++].move = make_move(from, to);
818 int generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
819 Square ksq, MoveStack* mlist) {
821 if (bit_is_set(dc, from))
823 Bitboard b = pos.piece_attacks<KING>(from)
824 & pos.empty_squares()
825 & ~QueenPseudoAttacks[ksq];
828 Square to = pop_1st_bit(&b);
829 mlist[n++].move = make_move(from, to);
837 int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
838 Bitboard blockSquares, MoveStack* mlist) {
840 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
842 // Find non-pinned pawns
844 Bitboard b1 = pos.pawns(PP.us) & not_pinned;
846 // Single pawn pushes. We don't have to AND with empty squares here,
847 // because the blocking squares will always be empty.
848 Bitboard b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
851 Square to = pop_1st_bit(&b2);
853 assert(pos.piece_on(to) == EMPTY);
855 if (square_rank(to) == PP.RANK_8)
857 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, QUEEN);
858 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, ROOK);
859 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, BISHOP);
860 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, KNIGHT);
862 mlist[n++].move = make_move(to - PP.DELTA_N, to);
865 // Double pawn pushes
866 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & PP.Rank3BB;
867 b2 = (C == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
870 Square to = pop_1st_bit(&b2);
872 assert(pos.piece_on(to) == EMPTY);
873 assert(PP.us != WHITE || square_rank(to) == RANK_4);
874 assert(PP.us != BLACK || square_rank(to) == RANK_5);
876 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
882 int generate_castle_moves(const Position& pos, MoveStack* mlist) {
885 Color us = pos.side_to_move();
887 if (pos.can_castle(us))
889 Color them = opposite_color(us);
890 Square ksq = pos.king_square(us);
892 assert(pos.piece_on(ksq) == king_of_color(us));
894 if (pos.can_castle_kingside(us))
896 Square rsq = pos.initial_kr_square(us);
897 Square g1 = relative_square(us, SQ_G1);
898 Square f1 = relative_square(us, SQ_F1);
900 bool illegal = false;
902 assert(pos.piece_on(rsq) == rook_of_color(us));
904 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
905 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
906 || pos.square_is_attacked(s, them))
909 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
910 if (s != ksq && s != rsq && pos.square_is_occupied(s))
914 mlist[n++].move = make_castle_move(ksq, rsq);
917 if (pos.can_castle_queenside(us))
919 Square rsq = pos.initial_qr_square(us);
920 Square c1 = relative_square(us, SQ_C1);
921 Square d1 = relative_square(us, SQ_D1);
923 bool illegal = false;
925 assert(pos.piece_on(rsq) == rook_of_color(us));
927 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
928 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
929 || pos.square_is_attacked(s, them))
932 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
933 if (s != ksq && s != rsq && pos.square_is_occupied(s))
936 if ( square_file(rsq) == FILE_B
937 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
938 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
942 mlist[n++].move = make_castle_move(ksq, rsq);