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 int generate_castle_moves(const Position&, MoveStack*);
38 // Template generate_pawn_captures() with specializations
39 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
40 int do_generate_pawn_captures(const Position& pos, MoveStack* mlist);
43 inline int generate_pawn_captures(const Position& p, MoveStack* m) {
44 return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
47 inline int generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
48 return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
51 // Template generate_pawn_noncaptures() with specializations
52 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
53 int do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
56 inline int generate_pawn_noncaptures(const Position& p, MoveStack* m) {
57 return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
60 inline int generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
61 return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
64 // Template generate_pawn_blocking_evasions() with specializations
65 template<Color Us, Bitboard, Bitboard, SquareDelta>
66 int do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
67 Bitboard blockSquares, MoveStack* mlist);
69 inline int generate_pawn_blocking_evasions(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
70 return do_generate_pawn_blocking_evasions<WHITE, Rank8BB, Rank3BB, DELTA_N>(p, np, bs, m);
73 inline int generate_pawn_blocking_evasions<BLACK>(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
74 return do_generate_pawn_blocking_evasions<BLACK, Rank1BB, Rank6BB, DELTA_S>(p, np, bs, m);
77 // Template generate_pawn_checks() with specializations
78 template<Color, Color, Bitboard, Bitboard, SquareDelta>
79 int do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
82 inline int generate_pawn_checks(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
83 return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
86 inline int generate_pawn_checks<BLACK>(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
87 return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
92 int generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
94 int generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
97 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*);
98 int generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
101 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
110 /// generate_captures generates() all pseudo-legal captures and queen
111 /// promotions. The return value is the number of moves generated.
113 int generate_captures(const Position& pos, MoveStack* mlist) {
116 assert(!pos.is_check());
118 Color us = pos.side_to_move();
119 Bitboard target = pos.pieces_of_color(opposite_color(us));
123 n = generate_pawn_captures<WHITE>(pos, mlist);
125 n = generate_pawn_captures<BLACK>(pos, mlist);
127 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
128 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
129 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
130 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
131 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
136 /// generate_noncaptures() generates all pseudo-legal non-captures and
137 /// underpromotions. The return value is the number of moves generated.
139 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
142 assert(!pos.is_check());
144 Color us = pos.side_to_move();
145 Bitboard target = pos.empty_squares();
149 n = generate_pawn_noncaptures<WHITE>(pos, mlist);
151 n = generate_pawn_noncaptures<BLACK>(pos, mlist);
153 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
154 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
155 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
156 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
157 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
158 n += generate_castle_moves(pos, mlist+n);
163 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
164 /// checks, except castling moves (will add this later). It returns the
165 /// number of generated moves.
167 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
170 assert(!pos.is_check());
173 Color us = pos.side_to_move();
174 Square ksq = pos.king_square(opposite_color(us));
176 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
178 dc = pos.discovered_check_candidates(us);
182 n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
184 n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
187 Bitboard b = pos.knights(us);
189 n += generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist+n);
193 n += generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist+n);
197 n += generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist+n);
201 n += generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist+n);
203 // Hopefully we always have a king ;-)
204 n += generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist+n);
206 // TODO: Castling moves!
212 /// generate_evasions() generates all check evasions when the side to move is
213 /// in check. Unlike the other move generation functions, this one generates
214 /// only legal moves. It returns the number of generated moves. This
215 /// function is very ugly, and needs cleaning up some time later. FIXME
217 int generate_evasions(const Position& pos, MoveStack* mlist) {
220 assert(pos.is_check());
222 Color us = pos.side_to_move();
223 Color them = opposite_color(us);
224 Square ksq = pos.king_square(us);
228 assert(pos.piece_on(ksq) == king_of_color(us));
230 // Generate evasions for king
231 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
232 Bitboard b2 = pos.occupied_squares();
237 to = pop_1st_bit(&b1);
239 // Make sure to is not attacked by the other side. This is a bit ugly,
240 // because we can't use Position::square_is_attacked. Instead we use
241 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
242 // b2 (the occupied squares with the king removed) in order to test whether
243 // the king will remain in check on the destination square.
244 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
245 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
246 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
247 || (pos.pawn_attacks(us, to) & pos.pawns(them))
248 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
250 mlist[n++].move = make_move(ksq, to);
253 // Generate evasions for other pieces only if not double check. We use a
254 // simple bit twiddling hack here rather than calling count_1s in order to
255 // save some time (we know that pos.checkers() has at most two nonzero bits).
256 Bitboard checkers = pos.checkers();
258 if (!(checkers & (checkers - 1))) // Only one bit set?
260 Square checksq = first_1(checkers);
262 assert(pos.color_of_piece_on(checksq) == them);
264 // Find pinned pieces
265 Bitboard not_pinned = ~pos.pinned_pieces(us);
267 // Generate captures of the checking piece
270 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
273 from = pop_1st_bit(&b1);
274 if (relative_rank(us, checksq) == RANK_8)
276 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
277 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
278 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
279 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
281 mlist[n++].move = make_move(from, checksq);
285 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
286 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
287 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
291 from = pop_1st_bit(&b1);
292 mlist[n++].move = make_move(from, checksq);
295 // Blocking check evasions are possible only if the checking piece is
297 if (checkers & pos.sliders())
299 Bitboard blockSquares = squares_between(checksq, ksq);
301 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
303 // Pawn moves. Because a blocking evasion can never be a capture, we
304 // only generate pawn pushes.
306 n += generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist+n);
308 n += generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist+n);
311 b1 = pos.knights(us) & not_pinned;
313 n += generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist+n);
315 b1 = pos.bishops(us) & not_pinned;
317 n += generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist+n);
319 b1 = pos.rooks(us) & not_pinned;
321 n += generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist+n);
323 b1 = pos.queens(us) & not_pinned;
325 n += generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist+n);
328 // Finally, the ugly special case of en passant captures. An en passant
329 // capture can only be a check evasion if the check is not a discovered
330 // check. If pos.ep_square() is set, the last move made must have been
331 // a double pawn push. If, furthermore, the checking piece is a pawn,
332 // an en passant check evasion may be possible.
333 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
335 to = pos.ep_square();
336 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
338 assert(b1 != EmptyBoardBB);
343 from = pop_1st_bit(&b1);
345 // Before generating the move, we have to make sure it is legal.
346 // This is somewhat tricky, because the two disappearing pawns may
347 // cause new "discovered checks". We test this by removing the
348 // two relevant bits from the occupied squares bitboard, and using
349 // the low-level bitboard functions for bishop and rook attacks.
350 b2 = pos.occupied_squares();
351 clear_bit(&b2, from);
352 clear_bit(&b2, checksq);
353 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
354 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
356 mlist[n++].move = make_ep_move(from, to);
364 /// generate_legal_moves() computes a complete list of legal moves in the
365 /// current position. This function is not very fast, and should be used
366 /// only in situations where performance is unimportant. It wouldn't be
367 /// very hard to write an efficient legal move generator, but for the moment
368 /// we don't need it.
370 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
375 return generate_evasions(pos, mlist);
377 // Generate pseudo-legal moves:
378 int n = generate_captures(pos, mlist);
379 n += generate_noncaptures(pos, mlist + n);
381 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
383 // Remove illegal moves from the list:
384 for (int i = 0; i < n; i++)
385 if (!pos.move_is_legal(mlist[i].move, pinned))
386 mlist[i--].move = mlist[--n].move;
392 /// generate_move_if_legal() takes a position and a (not necessarily
393 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
394 /// whether the move is legal. If the move is legal, the move itself is
395 /// returned. If not, the function returns MOVE_NONE. This function must
396 /// only be used when the side to move is not in check.
398 Move generate_move_if_legal(const Position& pos, Move m, Bitboard pinned) {
401 assert(!pos.is_check());
402 assert(move_is_ok(m));
404 Color us = pos.side_to_move();
405 Color them = opposite_color(us);
406 Square from = move_from(m);
407 Piece pc = pos.piece_on(from);
409 // If the from square is not occupied by a piece belonging to the side to
410 // move, the move is obviously not legal.
411 if (color_of_piece(pc) != us)
414 Square to = move_to(m);
419 // The piece must be a pawn and destination square must be the
420 // en passant square.
421 if ( type_of_piece(pc) != PAWN
422 || to != pos.ep_square())
425 assert(pos.square_is_empty(to));
426 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
428 // The move is pseudo-legal. If it is legal, return it.
429 return (pos.move_is_legal(m) ? m : MOVE_NONE);
433 if (move_is_short_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_kingside(us))
441 assert(from == pos.king_square(us));
442 assert(to == pos.initial_kr_square(us));
443 assert(pos.piece_on(to) == rook_of_color(us));
445 Square g1 = relative_square(us, SQ_G1);
446 Square f1 = relative_square(us, SQ_F1);
448 bool illegal = false;
450 // Check if any of the squares between king and rook
451 // is occupied or under attack.
452 for (s = Min(from, g1); s <= Max(from, g1); s++)
453 if ( (s != from && s != to && !pos.square_is_empty(s))
454 || pos.square_is_attacked(s, them))
457 // Check if any of the squares between king and rook
459 for (s = Min(to, f1); s <= Max(to, f1); s++)
460 if (s != from && s != to && !pos.square_is_empty(s))
463 return (!illegal ? m : MOVE_NONE);
466 if (move_is_long_castle(m))
468 // The piece must be a king and side to move must still have
469 // the right to castle kingside.
470 if ( type_of_piece(pc) != KING
471 ||!pos.can_castle_queenside(us))
474 assert(from == pos.king_square(us));
475 assert(to == pos.initial_qr_square(us));
476 assert(pos.piece_on(to) == rook_of_color(us));
478 Square c1 = relative_square(us, SQ_C1);
479 Square d1 = relative_square(us, SQ_D1);
481 bool illegal = false;
483 for (s = Min(from, c1); s <= Max(from, c1); s++)
484 if( (s != from && s != to && !pos.square_is_empty(s))
485 || pos.square_is_attacked(s, them))
488 for (s = Min(to, d1); s <= Max(to, d1); s++)
489 if(s != from && s != to && !pos.square_is_empty(s))
492 if ( square_file(to) == FILE_B
493 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
494 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
497 return (!illegal ? m : MOVE_NONE);
502 // The destination square cannot be occupied by a friendly piece
503 if (pos.color_of_piece_on(to) == us)
506 // Proceed according to the type of the moving piece.
507 if (type_of_piece(pc) == PAWN)
509 // If the destination square is on the 8/1th rank, the move must
511 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
512 ||(square_rank(to) == RANK_1 && us != WHITE))
513 && !move_promotion(m))
516 // Proceed according to the square delta between the source and
517 // destionation squares.
524 // Capture. The destination square must be occupied by an enemy
525 // piece (en passant captures was handled earlier).
526 if (pos.color_of_piece_on(to) != them)
532 // Pawn push. The destination square must be empty.
533 if (!pos.square_is_empty(to))
538 // Double white pawn push. The destination square must be on the fourth
539 // rank, and both the destination square and the square between the
540 // source and destination squares must be empty.
541 if ( square_rank(to) != RANK_4
542 || !pos.square_is_empty(to)
543 || !pos.square_is_empty(from + DELTA_N))
548 // Double black pawn push. The destination square must be on the fifth
549 // rank, and both the destination square and the square between the
550 // source and destination squares must be empty.
551 if ( square_rank(to) != RANK_5
552 || !pos.square_is_empty(to)
553 || !pos.square_is_empty(from + DELTA_S))
560 // The move is pseudo-legal. Return it if it is legal.
561 return (pos.move_is_legal(m) ? m : MOVE_NONE);
564 // Luckly we can handle all the other pieces in one go
565 return ( pos.piece_attacks_square(from, to)
566 && pos.move_is_legal(m)
567 && !move_promotion(m) ? m : MOVE_NONE);
573 template<PieceType Piece>
574 int generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
580 for (int i = 0; i < pos.piece_count(us, Piece); i++)
582 from = pos.piece_list(us, Piece, i);
583 b = pos.piece_attacks<Piece>(from) & target;
586 to = pop_1st_bit(&b);
587 mlist[n++].move = make_move(from, to);
594 int generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
597 Square to, from = pos.king_square(us);
600 b = pos.piece_attacks<KING>(from) & target;
603 to = pop_1st_bit(&b);
604 mlist[n++].move = make_move(from, to);
609 template<PieceType Piece>
610 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
611 Bitboard blockSquares, MoveStack* mlist) {
615 Square from = pop_1st_bit(&b);
616 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
619 Square to = pop_1st_bit(&bb);
620 mlist[n++].move = make_move(from, to);
627 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
628 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
630 int do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
632 Bitboard pawns = pos.pawns(Us);
633 Bitboard enemyPieces = pos.pieces_of_color(Them);
637 // Captures in the a1-h8 (a8-h1 for black) direction
638 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
640 // Capturing promotions
641 Bitboard b2 = b1 & TRank8BB;
644 sq = pop_1st_bit(&b2);
645 mlist[n++].move = make_promotion_move(sq - TDELTA_NE, sq, QUEEN);
648 // Capturing non-promotions
652 sq = pop_1st_bit(&b2);
653 mlist[n++].move = make_move(sq - TDELTA_NE, sq);
656 // Captures in the h1-a8 (h8-a1 for black) direction
657 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
659 // Capturing promotions
663 sq = pop_1st_bit(&b2);
664 mlist[n++].move = make_promotion_move(sq - TDELTA_NW, sq, QUEEN);
667 // Capturing non-promotions
671 sq = pop_1st_bit(&b2);
672 mlist[n++].move = make_move(sq - TDELTA_NW, sq);
675 // Non-capturing promotions
676 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
679 sq = pop_1st_bit(&b1);
680 mlist[n++].move = make_promotion_move(sq - TDELTA_N, sq, QUEEN);
683 // En passant captures
684 if (pos.ep_square() != SQ_NONE)
686 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
687 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
689 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
690 assert(b1 != EmptyBoardBB);
694 sq = pop_1st_bit(&b1);
695 mlist[n++].move = make_ep_move(sq, pos.ep_square());
701 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
702 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
704 int do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
706 Bitboard pawns = pos.pawns(Us);
707 Bitboard enemyPieces = pos.pieces_of_color(Them);
708 Bitboard emptySquares = pos.empty_squares();
713 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
714 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
717 sq = pop_1st_bit(&b1);
718 mlist[n++].move = make_promotion_move(sq - TDELTA_NE, sq, ROOK);
719 mlist[n++].move = make_promotion_move(sq - TDELTA_NE, sq, BISHOP);
720 mlist[n++].move = make_promotion_move(sq - TDELTA_NE, sq, KNIGHT);
723 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
724 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
727 sq = pop_1st_bit(&b1);
728 mlist[n++].move = make_promotion_move(sq - TDELTA_NW, sq, ROOK);
729 mlist[n++].move = make_promotion_move(sq - TDELTA_NW, sq, BISHOP);
730 mlist[n++].move = make_promotion_move(sq - TDELTA_NW, sq, KNIGHT);
733 // Single pawn pushes
734 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
738 sq = pop_1st_bit(&b2);
739 mlist[n++].move = make_promotion_move(sq - TDELTA_N, sq, ROOK);
740 mlist[n++].move = make_promotion_move(sq - TDELTA_N, sq, BISHOP);
741 mlist[n++].move = make_promotion_move(sq - TDELTA_N, sq, KNIGHT);
746 sq = pop_1st_bit(&b2);
747 mlist[n++].move = make_move(sq - TDELTA_N, sq);
750 // Double pawn pushes
751 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
754 sq = pop_1st_bit(&b2);
755 mlist[n++].move = make_move(sq - TDELTA_N - TDELTA_N, sq);
761 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
762 int do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
764 // Pawn moves which give discovered check. This is possible only if the
765 // pawn is not on the same file as the enemy king, because we don't
766 // 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(Us) & ~file_bb(ksq), b2, b3;
773 // Discovered checks, single pawn pushes
774 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~TRank8BB & empty;
777 Square to = pop_1st_bit(&b3);
778 mlist[n++].move = make_move(to - TDELTA_N, to);
781 // Discovered checks, double pawn pushes
782 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
785 Square to = pop_1st_bit(&b3);
786 mlist[n++].move = make_move(to - TDELTA_N - TDELTA_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 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
796 b3 = b2 & pos.pawn_attacks(Them, ksq);
799 Square to = pop_1st_bit(&b3);
800 mlist[n++].move = make_move(to - TDELTA_N, to);
803 // Direct checks, double pawn pushes
804 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
806 & pos.pawn_attacks(Them, ksq);
810 Square to = pop_1st_bit(&b3);
811 mlist[n++].move = make_move(to - TDELTA_N - TDELTA_N, to);
816 template<PieceType Piece>
817 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
818 Square ksq, MoveStack* mlist) {
821 Bitboard b = target & dc;
824 Square from = pop_1st_bit(&b);
825 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
828 Square to = pop_1st_bit(&bb);
829 mlist[n++].move = make_move(from, to);
834 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
837 Square from = pop_1st_bit(&b);
838 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
841 Square to = pop_1st_bit(&bb);
842 mlist[n++].move = make_move(from, to);
848 int generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
849 Square ksq, MoveStack* mlist) {
851 if (bit_is_set(dc, from))
853 Bitboard b = pos.piece_attacks<KING>(from)
854 & pos.empty_squares()
855 & ~QueenPseudoAttacks[ksq];
858 Square to = pop_1st_bit(&b);
859 mlist[n++].move = make_move(from, to);
866 template<Color Us, Bitboard TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
867 int do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
868 Bitboard blockSquares, MoveStack* mlist) {
869 // Find non-pinned pawns
871 Bitboard b1 = pos.pawns(Us) & not_pinned;
873 // Single pawn pushes. We don't have to AND with empty squares here,
874 // because the blocking squares will always be empty.
875 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
878 Square to = pop_1st_bit(&b2);
880 assert(pos.piece_on(to) == EMPTY);
882 if (square_rank(to) == TRANK_8)
884 mlist[n++].move = make_promotion_move(to - TDELTA_N, to, QUEEN);
885 mlist[n++].move = make_promotion_move(to - TDELTA_N, to, ROOK);
886 mlist[n++].move = make_promotion_move(to - TDELTA_N, to, BISHOP);
887 mlist[n++].move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
889 mlist[n++].move = make_move(to - TDELTA_N, to);
892 // Double pawn pushes
893 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
894 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
897 Square to = pop_1st_bit(&b2);
899 assert(pos.piece_on(to) == EMPTY);
900 assert(Us != WHITE || square_rank(to) == RANK_4);
901 assert(Us != BLACK || square_rank(to) == RANK_5);
903 mlist[n++].move = make_move(to - TDELTA_N - TDELTA_N, to);
909 int generate_castle_moves(const Position& pos, MoveStack* mlist) {
912 Color us = pos.side_to_move();
914 if (pos.can_castle(us))
916 Color them = opposite_color(us);
917 Square ksq = pos.king_square(us);
919 assert(pos.piece_on(ksq) == king_of_color(us));
921 if (pos.can_castle_kingside(us))
923 Square rsq = pos.initial_kr_square(us);
924 Square g1 = relative_square(us, SQ_G1);
925 Square f1 = relative_square(us, SQ_F1);
927 bool illegal = false;
929 assert(pos.piece_on(rsq) == rook_of_color(us));
931 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
932 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
933 || pos.square_is_attacked(s, them))
936 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
937 if (s != ksq && s != rsq && pos.square_is_occupied(s))
941 mlist[n++].move = make_castle_move(ksq, rsq);
944 if (pos.can_castle_queenside(us))
946 Square rsq = pos.initial_qr_square(us);
947 Square c1 = relative_square(us, SQ_C1);
948 Square d1 = relative_square(us, SQ_D1);
950 bool illegal = false;
952 assert(pos.piece_on(rsq) == rook_of_color(us));
954 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
955 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
956 || pos.square_is_attacked(s, them))
959 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
960 if (s != ksq && s != rsq && pos.square_is_occupied(s))
963 if ( square_file(rsq) == FILE_B
964 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
965 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
969 mlist[n++].move = make_castle_move(ksq, rsq);