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/>.
28 // Simple macro to wrap a very common while loop, no facny, no flexibility,
29 // hardcoded list name 'mlist' and from square 'from'.
30 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
33 //// Local definitions
39 MoveStack* generate_castle_moves(const Position&, MoveStack*);
41 // Template generate_pawn_captures() with specializations
42 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
43 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist);
46 inline MoveStack* generate_pawn_captures(const Position& p, MoveStack* m) {
47 return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
50 inline MoveStack* generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
51 return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
54 // Template generate_pawn_noncaptures() with specializations
55 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
56 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
59 inline MoveStack* generate_pawn_noncaptures(const Position& p, MoveStack* m) {
60 return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
63 inline MoveStack* generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
64 return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
67 // Template generate_pawn_blocking_evasions() with specializations
68 template<Color Us, Rank, Bitboard, SquareDelta>
69 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
70 Bitboard blockSquares, MoveStack* mlist);
72 inline MoveStack* generate_pawn_blocking_evasions(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
73 return do_generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, np, bs, m);
76 inline MoveStack* generate_pawn_blocking_evasions<BLACK>(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
77 return do_generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, np, bs, m);
80 // Template generate_pawn_checks() with specializations
81 template<Color, Color, Bitboard, Bitboard, SquareDelta>
82 MoveStack* do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
85 inline MoveStack* generate_pawn_checks(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
86 return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
89 inline MoveStack* generate_pawn_checks<BLACK>(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
90 return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
95 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
97 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
100 MoveStack* generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*);
101 MoveStack* generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
104 MoveStack* generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
113 /// generate_captures generates() all pseudo-legal captures and queen
114 /// promotions. The return value is the number of moves generated.
116 int generate_captures(const Position& pos, MoveStack* mlist) {
119 assert(!pos.is_check());
121 Color us = pos.side_to_move();
122 Bitboard target = pos.pieces_of_color(opposite_color(us));
123 MoveStack* mlist_start = mlist;
126 mlist = generate_pawn_captures<WHITE>(pos, mlist);
128 mlist = generate_pawn_captures<BLACK>(pos, mlist);
130 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
131 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
132 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
133 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
134 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
135 return int(mlist - mlist_start);
139 /// generate_noncaptures() generates all pseudo-legal non-captures and
140 /// underpromotions. The return value is the number of moves generated.
142 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
145 assert(!pos.is_check());
147 Color us = pos.side_to_move();
148 Bitboard target = pos.empty_squares();
149 MoveStack* mlist_start = mlist;
152 mlist = generate_pawn_noncaptures<WHITE>(pos, mlist);
154 mlist = generate_pawn_noncaptures<BLACK>(pos, mlist);
156 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
157 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
158 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
159 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
160 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
161 mlist = generate_castle_moves(pos, mlist);
162 return int(mlist - mlist_start);
166 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
167 /// checks, except castling moves (will add this later). It returns the
168 /// number of generated moves.
170 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
173 assert(!pos.is_check());
175 Color us = pos.side_to_move();
176 Square ksq = pos.king_square(opposite_color(us));
177 MoveStack* mlist_start = mlist;
179 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
181 dc = pos.discovered_check_candidates(us);
185 mlist = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
187 mlist = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
190 Bitboard b = pos.knights(us);
192 mlist = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist);
196 mlist = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist);
200 mlist = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist);
204 mlist = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist);
206 // Hopefully we always have a king ;-)
207 mlist = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist);
209 // TODO: Castling moves!
211 return int(mlist - mlist_start);
215 /// generate_evasions() generates all check evasions when the side to move is
216 /// in check. Unlike the other move generation functions, this one generates
217 /// only legal moves. It returns the number of generated moves. This
218 /// function is very ugly, and needs cleaning up some time later. FIXME
220 int generate_evasions(const Position& pos, MoveStack* mlist) {
223 assert(pos.is_check());
226 Color us = pos.side_to_move();
227 Color them = opposite_color(us);
228 Square ksq = pos.king_square(us);
229 MoveStack* mlist_start = mlist;
231 assert(pos.piece_on(ksq) == king_of_color(us));
233 // Generate evasions for king
234 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
235 Bitboard b2 = pos.occupied_squares();
240 to = pop_1st_bit(&b1);
242 // Make sure 'to' is not attacked by the other side. This is a bit ugly,
243 // because we can't use Position::square_is_attacked. Instead we use
244 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
245 // b2 (the occupied squares with the king removed) in order to test whether
246 // the king will remain in check on the destination square.
247 if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
248 || (pos.pawn_attacks(us, to) & pos.pawns(them))
249 || (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
250 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
251 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
253 (*mlist++).move = make_move(ksq, to);
256 // Generate evasions for other pieces only if not double check. We use a
257 // simple bit twiddling hack here rather than calling count_1s in order to
258 // save some time (we know that pos.checkers() has at most two nonzero bits).
259 Bitboard checkers = pos.checkers();
261 if (!(checkers & (checkers - 1))) // Only one bit set?
263 Square checksq = first_1(checkers);
265 assert(pos.color_of_piece_on(checksq) == them);
267 // Find pinned pieces
268 Bitboard not_pinned = ~pos.pinned_pieces(us);
270 // Generate captures of the checking piece
273 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
276 from = pop_1st_bit(&b1);
277 if (relative_rank(us, checksq) == RANK_8)
279 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
280 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
281 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
282 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
284 (*mlist++).move = make_move(from, checksq);
288 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
289 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
290 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
294 from = pop_1st_bit(&b1);
295 (*mlist++).move = make_move(from, checksq);
298 // Blocking check evasions are possible only if the checking piece is
300 if (checkers & pos.sliders())
302 Bitboard blockSquares = squares_between(checksq, ksq);
304 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
306 // Pawn moves. Because a blocking evasion can never be a capture, we
307 // only generate pawn pushes.
309 mlist = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist);
311 mlist = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist);
314 b1 = pos.knights(us) & not_pinned;
316 mlist = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist);
318 b1 = pos.bishops(us) & not_pinned;
320 mlist = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist);
322 b1 = pos.rooks(us) & not_pinned;
324 mlist = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist);
326 b1 = pos.queens(us) & not_pinned;
328 mlist = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist);
331 // Finally, the ugly special case of en passant captures. An en passant
332 // capture can only be a check evasion if the check is not a discovered
333 // check. If pos.ep_square() is set, the last move made must have been
334 // a double pawn push. If, furthermore, the checking piece is a pawn,
335 // an en passant check evasion may be possible.
336 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
338 to = pos.ep_square();
339 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
341 assert(b1 != EmptyBoardBB);
346 from = pop_1st_bit(&b1);
348 // Before generating the move, we have to make sure it is legal.
349 // This is somewhat tricky, because the two disappearing pawns may
350 // cause new "discovered checks". We test this by removing the
351 // two relevant bits from the occupied squares bitboard, and using
352 // the low-level bitboard functions for bishop and rook attacks.
353 b2 = pos.occupied_squares();
354 clear_bit(&b2, from);
355 clear_bit(&b2, checksq);
356 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
357 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
359 (*mlist++).move = make_ep_move(from, to);
363 return int(mlist - mlist_start);
367 /// generate_legal_moves() computes a complete list of legal moves in the
368 /// current position. This function is not very fast, and should be used
369 /// only in situations where performance is unimportant. It wouldn't be
370 /// very hard to write an efficient legal move generator, but for the moment
371 /// we don't need it.
373 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
378 return generate_evasions(pos, mlist);
380 // Generate pseudo-legal moves
381 int n = generate_captures(pos, mlist);
382 n += generate_noncaptures(pos, mlist + n);
384 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
386 // Remove illegal moves from the list
387 for (int i = 0; i < n; i++)
388 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
389 mlist[i--].move = mlist[--n].move;
395 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
396 /// move and a pinned pieces bitboard as input, and tests whether
397 /// the move is legal. If the move is legal, the move itself is
398 /// returned. If not, the function returns false. This function must
399 /// only be used when the side to move is not in check.
401 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
404 assert(!pos.is_check());
405 assert(move_is_ok(m));
406 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
408 Color us = pos.side_to_move();
409 Color them = opposite_color(us);
410 Square from = move_from(m);
411 Piece pc = pos.piece_on(from);
413 // If the from square is not occupied by a piece belonging to the side to
414 // move, the move is obviously not legal.
415 if (color_of_piece(pc) != us)
418 Square to = move_to(m);
423 // The piece must be a pawn and destination square must be the
424 // en passant square.
425 if ( type_of_piece(pc) != PAWN
426 || to != pos.ep_square())
429 assert(pos.square_is_empty(to));
430 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
432 // The move is pseudo-legal, check if it is also legal
433 return pos.pl_move_is_legal(m, pinned);
437 if (move_is_short_castle(m))
439 // The piece must be a king and side to move must still have
440 // the right to castle kingside.
441 if ( type_of_piece(pc) != KING
442 ||!pos.can_castle_kingside(us))
445 assert(from == pos.king_square(us));
446 assert(to == pos.initial_kr_square(us));
447 assert(pos.piece_on(to) == rook_of_color(us));
449 Square g1 = relative_square(us, SQ_G1);
450 Square f1 = relative_square(us, SQ_F1);
452 bool illegal = false;
454 // Check if any of the squares between king and rook
455 // is occupied or under attack.
456 for (s = Min(from, g1); s <= Max(from, g1); s++)
457 if ( (s != from && s != to && !pos.square_is_empty(s))
458 || pos.square_is_attacked(s, them))
461 // Check if any of the squares between king and rook
463 for (s = Min(to, f1); s <= Max(to, f1); s++)
464 if (s != from && s != to && !pos.square_is_empty(s))
470 if (move_is_long_castle(m))
472 // The piece must be a king and side to move must still have
473 // the right to castle kingside.
474 if ( type_of_piece(pc) != KING
475 ||!pos.can_castle_queenside(us))
478 assert(from == pos.king_square(us));
479 assert(to == pos.initial_qr_square(us));
480 assert(pos.piece_on(to) == rook_of_color(us));
482 Square c1 = relative_square(us, SQ_C1);
483 Square d1 = relative_square(us, SQ_D1);
485 bool illegal = false;
487 for (s = Min(from, c1); s <= Max(from, c1); s++)
488 if( (s != from && s != to && !pos.square_is_empty(s))
489 || pos.square_is_attacked(s, them))
492 for (s = Min(to, d1); s <= Max(to, d1); s++)
493 if(s != from && s != to && !pos.square_is_empty(s))
496 if ( square_file(to) == FILE_B
497 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
498 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
506 // The destination square cannot be occupied by a friendly piece
507 if (pos.color_of_piece_on(to) == us)
510 // Proceed according to the type of the moving piece.
511 if (type_of_piece(pc) == PAWN)
513 // If the destination square is on the 8/1th rank, the move must
515 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
516 ||(square_rank(to) == RANK_1 && us != WHITE))
517 && !move_promotion(m))
520 // Proceed according to the square delta between the source and
521 // destionation squares.
528 // Capture. The destination square must be occupied by an enemy
529 // piece (en passant captures was handled earlier).
530 if (pos.color_of_piece_on(to) != them)
536 // Pawn push. The destination square must be empty.
537 if (!pos.square_is_empty(to))
542 // Double white pawn push. The destination square must be on the fourth
543 // rank, and both the destination square and the square between the
544 // source and destination squares must be empty.
545 if ( square_rank(to) != RANK_4
546 || !pos.square_is_empty(to)
547 || !pos.square_is_empty(from + DELTA_N))
552 // Double black pawn push. The destination square must be on the fifth
553 // rank, and both the destination square and the square between the
554 // source and destination squares must be empty.
555 if ( square_rank(to) != RANK_5
556 || !pos.square_is_empty(to)
557 || !pos.square_is_empty(from + DELTA_S))
564 // The move is pseudo-legal, check if it is also legal
565 return pos.pl_move_is_legal(m, pinned);
568 // Luckly we can handle all the other pieces in one go
569 return ( pos.piece_attacks_square(from, to)
570 && pos.pl_move_is_legal(m, pinned)
571 && !move_promotion(m));
577 template<PieceType Piece>
578 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
583 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
585 from = pos.piece_list(us, Piece, i);
586 b = pos.piece_attacks<Piece>(from) & target;
593 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
596 Square from = pos.king_square(us);
598 b = pos.piece_attacks<KING>(from) & target;
603 template<PieceType Piece>
604 MoveStack* generate_piece_blocking_evasions(const Position& pos, Bitboard b,
605 Bitboard blockSquares, MoveStack* mlist) {
608 Square from = pop_1st_bit(&b);
609 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
616 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
617 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
619 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
622 Bitboard pawns = pos.pawns(Us);
623 Bitboard enemyPieces = pos.pieces_of_color(Them);
625 // Captures in the a1-h8 (a8-h1 for black) direction
626 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
628 // Capturing promotions
629 Bitboard b2 = b1 & TRank8BB;
632 to = pop_1st_bit(&b2);
633 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
636 // Capturing non-promotions
640 to = pop_1st_bit(&b2);
641 (*mlist++).move = make_move(to - TDELTA_NE, to);
644 // Captures in the h1-a8 (h8-a1 for black) direction
645 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
647 // Capturing promotions
651 to = pop_1st_bit(&b2);
652 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
655 // Capturing non-promotions
659 to = pop_1st_bit(&b2);
660 (*mlist++).move = make_move(to - TDELTA_NW, to);
663 // Non-capturing promotions
664 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
667 to = pop_1st_bit(&b1);
668 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
671 // En passant captures
672 if (pos.ep_square() != SQ_NONE)
674 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
675 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
677 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
678 assert(b1 != EmptyBoardBB);
682 to = pop_1st_bit(&b1);
683 (*mlist++).move = make_ep_move(to, pos.ep_square());
689 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
690 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
692 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
694 Bitboard pawns = pos.pawns(Us);
695 Bitboard enemyPieces = pos.pieces_of_color(Them);
696 Bitboard emptySquares = pos.empty_squares();
700 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
701 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
704 to = pop_1st_bit(&b1);
705 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
706 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
707 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
710 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
711 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
714 to = pop_1st_bit(&b1);
715 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
716 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
717 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
720 // Single pawn pushes
721 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
725 to = pop_1st_bit(&b2);
726 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
727 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
728 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
733 to = pop_1st_bit(&b2);
734 (*mlist++).move = make_move(to - TDELTA_N, to);
737 // Double pawn pushes
738 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
741 to = pop_1st_bit(&b2);
742 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
748 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
749 MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
751 // Pawn moves which give discovered check. This is possible only if the
752 // pawn is not on the same file as the enemy king, because we don't
753 // generate captures.
754 Bitboard empty = pos.empty_squares();
756 // Find all friendly pawns not on the enemy king's file
757 Bitboard b1 = pos.pawns(Us) & ~file_bb(ksq), b2, b3;
759 // Discovered checks, single pawn pushes
760 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~TRank8BB & empty;
763 Square to = pop_1st_bit(&b3);
764 (*mlist++).move = make_move(to - TDELTA_N, to);
767 // Discovered checks, double pawn pushes
768 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
771 Square to = pop_1st_bit(&b3);
772 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
775 // Direct checks. These are possible only for pawns on neighboring files
778 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
780 // Direct checks, single pawn pushes
781 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
782 b3 = b2 & pos.pawn_attacks(Them, ksq);
785 Square to = pop_1st_bit(&b3);
786 (*mlist++).move = make_move(to - TDELTA_N, to);
789 // Direct checks, double pawn pushes
790 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
792 & pos.pawn_attacks(Them, ksq);
796 Square to = pop_1st_bit(&b3);
797 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
802 template<PieceType Piece>
803 MoveStack* generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
804 Square ksq, MoveStack* mlist) {
806 Bitboard b = target & dc;
809 Square from = pop_1st_bit(&b);
810 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
815 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
818 Square from = pop_1st_bit(&b);
819 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
825 MoveStack* generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
826 Square ksq, MoveStack* mlist) {
827 if (bit_is_set(dc, from))
829 Bitboard b = pos.piece_attacks<KING>(from)
830 & pos.empty_squares()
831 & ~QueenPseudoAttacks[ksq];
838 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
839 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
840 Bitboard blockSquares, MoveStack* mlist) {
843 // Find non-pinned pawns
844 Bitboard b1 = pos.pawns(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 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
851 to = pop_1st_bit(&b2);
853 assert(pos.piece_on(to) == EMPTY);
855 if (square_rank(to) == TRANK_8)
857 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
858 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
859 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
860 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
862 (*mlist++).move = make_move(to - TDELTA_N, to);
865 // Double pawn pushes
866 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
867 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
870 to = pop_1st_bit(&b2);
872 assert(pos.piece_on(to) == EMPTY);
873 assert(Us != WHITE || square_rank(to) == RANK_4);
874 assert(Us != BLACK || square_rank(to) == RANK_5);
876 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
882 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
884 Color us = pos.side_to_move();
886 if (pos.can_castle(us))
888 Color them = opposite_color(us);
889 Square ksq = pos.king_square(us);
891 assert(pos.piece_on(ksq) == king_of_color(us));
893 if (pos.can_castle_kingside(us))
895 Square rsq = pos.initial_kr_square(us);
896 Square g1 = relative_square(us, SQ_G1);
897 Square f1 = relative_square(us, SQ_F1);
899 bool illegal = false;
901 assert(pos.piece_on(rsq) == rook_of_color(us));
903 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
904 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
905 || pos.square_is_attacked(s, them))
908 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
909 if (s != ksq && s != rsq && pos.square_is_occupied(s))
913 (*mlist++).move = make_castle_move(ksq, rsq);
916 if (pos.can_castle_queenside(us))
918 Square rsq = pos.initial_qr_square(us);
919 Square c1 = relative_square(us, SQ_C1);
920 Square d1 = relative_square(us, SQ_D1);
922 bool illegal = false;
924 assert(pos.piece_on(rsq) == rook_of_color(us));
926 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
927 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
928 || pos.square_is_attacked(s, them))
931 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
932 if (s != ksq && s != rsq && pos.square_is_occupied(s))
935 if ( square_file(rsq) == FILE_B
936 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
937 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
941 (*mlist++).move = make_castle_move(ksq, rsq);