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 generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist);
311 generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist);
314 b1 = pos.knights(us) & not_pinned;
316 generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist);
318 b1 = pos.bishops(us) & not_pinned;
320 generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist);
322 b1 = pos.rooks(us) & not_pinned;
324 generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist);
326 b1 = pos.queens(us) & not_pinned;
328 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.move_is_legal(mlist[i].move, pinned))
389 mlist[i--].move = mlist[--n].move;
395 /// generate_move_if_legal() takes a position and a (not necessarily
396 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
397 /// whether the move is legal. If the move is legal, the move itself is
398 /// returned. If not, the function returns MOVE_NONE. This function must
399 /// only be used when the side to move is not in check.
401 Move generate_move_if_legal(const Position& pos, Move m, Bitboard pinned) {
404 assert(!pos.is_check());
405 assert(move_is_ok(m));
407 Color us = pos.side_to_move();
408 Color them = opposite_color(us);
409 Square from = move_from(m);
410 Piece pc = pos.piece_on(from);
412 // If the from square is not occupied by a piece belonging to the side to
413 // move, the move is obviously not legal.
414 if (color_of_piece(pc) != us)
417 Square to = move_to(m);
422 // The piece must be a pawn and destination square must be the
423 // en passant square.
424 if ( type_of_piece(pc) != PAWN
425 || to != pos.ep_square())
428 assert(pos.square_is_empty(to));
429 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
431 // The move is pseudo-legal. If it is legal, return it.
432 return (pos.move_is_legal(m) ? m : MOVE_NONE);
436 if (move_is_short_castle(m))
438 // The piece must be a king and side to move must still have
439 // the right to castle kingside.
440 if ( type_of_piece(pc) != KING
441 ||!pos.can_castle_kingside(us))
444 assert(from == pos.king_square(us));
445 assert(to == pos.initial_kr_square(us));
446 assert(pos.piece_on(to) == rook_of_color(us));
448 Square g1 = relative_square(us, SQ_G1);
449 Square f1 = relative_square(us, SQ_F1);
451 bool illegal = false;
453 // Check if any of the squares between king and rook
454 // is occupied or under attack.
455 for (s = Min(from, g1); s <= Max(from, g1); s++)
456 if ( (s != from && s != to && !pos.square_is_empty(s))
457 || pos.square_is_attacked(s, them))
460 // Check if any of the squares between king and rook
462 for (s = Min(to, f1); s <= Max(to, f1); s++)
463 if (s != from && s != to && !pos.square_is_empty(s))
466 return (!illegal ? m : MOVE_NONE);
469 if (move_is_long_castle(m))
471 // The piece must be a king and side to move must still have
472 // the right to castle kingside.
473 if ( type_of_piece(pc) != KING
474 ||!pos.can_castle_queenside(us))
477 assert(from == pos.king_square(us));
478 assert(to == pos.initial_qr_square(us));
479 assert(pos.piece_on(to) == rook_of_color(us));
481 Square c1 = relative_square(us, SQ_C1);
482 Square d1 = relative_square(us, SQ_D1);
484 bool illegal = false;
486 for (s = Min(from, c1); s <= Max(from, c1); s++)
487 if( (s != from && s != to && !pos.square_is_empty(s))
488 || pos.square_is_attacked(s, them))
491 for (s = Min(to, d1); s <= Max(to, d1); s++)
492 if(s != from && s != to && !pos.square_is_empty(s))
495 if ( square_file(to) == FILE_B
496 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
497 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
500 return (!illegal ? m : MOVE_NONE);
505 // The destination square cannot be occupied by a friendly piece
506 if (pos.color_of_piece_on(to) == us)
509 // Proceed according to the type of the moving piece.
510 if (type_of_piece(pc) == PAWN)
512 // If the destination square is on the 8/1th rank, the move must
514 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
515 ||(square_rank(to) == RANK_1 && us != WHITE))
516 && !move_promotion(m))
519 // Proceed according to the square delta between the source and
520 // destionation squares.
527 // Capture. The destination square must be occupied by an enemy
528 // piece (en passant captures was handled earlier).
529 if (pos.color_of_piece_on(to) != them)
535 // Pawn push. The destination square must be empty.
536 if (!pos.square_is_empty(to))
541 // Double white pawn push. The destination square must be on the fourth
542 // rank, and both the destination square and the square between the
543 // source and destination squares must be empty.
544 if ( square_rank(to) != RANK_4
545 || !pos.square_is_empty(to)
546 || !pos.square_is_empty(from + DELTA_N))
551 // Double black pawn push. The destination square must be on the fifth
552 // rank, and both the destination square and the square between the
553 // source and destination squares must be empty.
554 if ( square_rank(to) != RANK_5
555 || !pos.square_is_empty(to)
556 || !pos.square_is_empty(from + DELTA_S))
563 // The move is pseudo-legal. Return it if it is legal.
564 return (pos.move_is_legal(m) ? m : MOVE_NONE);
567 // Luckly we can handle all the other pieces in one go
568 return ( pos.piece_attacks_square(from, to)
569 && pos.move_is_legal(m)
570 && !move_promotion(m) ? m : MOVE_NONE);
576 template<PieceType Piece>
577 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
582 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
584 from = pos.piece_list(us, Piece, i);
585 b = pos.piece_attacks<Piece>(from) & target;
592 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
595 Square from = pos.king_square(us);
597 b = pos.piece_attacks<KING>(from) & target;
602 template<PieceType Piece>
603 MoveStack* generate_piece_blocking_evasions(const Position& pos, Bitboard b,
604 Bitboard blockSquares, MoveStack* mlist) {
607 Square from = pop_1st_bit(&b);
608 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
615 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
616 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
618 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
621 Bitboard pawns = pos.pawns(Us);
622 Bitboard enemyPieces = pos.pieces_of_color(Them);
624 // Captures in the a1-h8 (a8-h1 for black) direction
625 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
627 // Capturing promotions
628 Bitboard b2 = b1 & TRank8BB;
631 to = pop_1st_bit(&b2);
632 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
635 // Capturing non-promotions
639 to = pop_1st_bit(&b2);
640 (*mlist++).move = make_move(to - TDELTA_NE, to);
643 // Captures in the h1-a8 (h8-a1 for black) direction
644 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
646 // Capturing promotions
650 to = pop_1st_bit(&b2);
651 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
654 // Capturing non-promotions
658 to = pop_1st_bit(&b2);
659 (*mlist++).move = make_move(to - TDELTA_NW, to);
662 // Non-capturing promotions
663 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
666 to = pop_1st_bit(&b1);
667 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
670 // En passant captures
671 if (pos.ep_square() != SQ_NONE)
673 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
674 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
676 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
677 assert(b1 != EmptyBoardBB);
681 to = pop_1st_bit(&b1);
682 (*mlist++).move = make_ep_move(to, pos.ep_square());
688 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
689 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
691 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
693 Bitboard pawns = pos.pawns(Us);
694 Bitboard enemyPieces = pos.pieces_of_color(Them);
695 Bitboard emptySquares = pos.empty_squares();
699 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
700 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
703 to = pop_1st_bit(&b1);
704 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
705 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
706 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
709 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
710 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
713 to = pop_1st_bit(&b1);
714 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
715 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
716 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
719 // Single pawn pushes
720 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
724 to = pop_1st_bit(&b2);
725 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
726 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
727 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
732 to = pop_1st_bit(&b2);
733 (*mlist++).move = make_move(to - TDELTA_N, to);
736 // Double pawn pushes
737 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
740 to = pop_1st_bit(&b2);
741 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
747 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
748 MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
750 // Pawn moves which give discovered check. This is possible only if the
751 // pawn is not on the same file as the enemy king, because we don't
752 // generate captures.
753 Bitboard empty = pos.empty_squares();
755 // Find all friendly pawns not on the enemy king's file
756 Bitboard b1 = pos.pawns(Us) & ~file_bb(ksq), b2, b3;
758 // Discovered checks, single pawn pushes
759 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~TRank8BB & empty;
762 Square to = pop_1st_bit(&b3);
763 (*mlist++).move = make_move(to - TDELTA_N, to);
766 // Discovered checks, double pawn pushes
767 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
770 Square to = pop_1st_bit(&b3);
771 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
774 // Direct checks. These are possible only for pawns on neighboring files
777 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
779 // Direct checks, single pawn pushes
780 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
781 b3 = b2 & pos.pawn_attacks(Them, ksq);
784 Square to = pop_1st_bit(&b3);
785 (*mlist++).move = make_move(to - TDELTA_N, to);
788 // Direct checks, double pawn pushes
789 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
791 & pos.pawn_attacks(Them, ksq);
795 Square to = pop_1st_bit(&b3);
796 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
801 template<PieceType Piece>
802 MoveStack* generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
803 Square ksq, MoveStack* mlist) {
805 Bitboard b = target & dc;
808 Square from = pop_1st_bit(&b);
809 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
814 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
817 Square from = pop_1st_bit(&b);
818 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
824 MoveStack* generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
825 Square ksq, MoveStack* mlist) {
826 if (bit_is_set(dc, from))
828 Bitboard b = pos.piece_attacks<KING>(from)
829 & pos.empty_squares()
830 & ~QueenPseudoAttacks[ksq];
837 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
838 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
839 Bitboard blockSquares, MoveStack* mlist) {
842 // Find non-pinned pawns
843 Bitboard b1 = pos.pawns(Us) & not_pinned;
845 // Single pawn pushes. We don't have to AND with empty squares here,
846 // because the blocking squares will always be empty.
847 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
850 to = pop_1st_bit(&b2);
852 assert(pos.piece_on(to) == EMPTY);
854 if (square_rank(to) == TRANK_8)
856 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
857 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
858 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
859 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
861 (*mlist++).move = make_move(to - TDELTA_N, to);
864 // Double pawn pushes
865 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
866 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
869 to = pop_1st_bit(&b2);
871 assert(pos.piece_on(to) == EMPTY);
872 assert(Us != WHITE || square_rank(to) == RANK_4);
873 assert(Us != BLACK || square_rank(to) == RANK_5);
875 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
881 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
883 Color us = pos.side_to_move();
885 if (pos.can_castle(us))
887 Color them = opposite_color(us);
888 Square ksq = pos.king_square(us);
890 assert(pos.piece_on(ksq) == king_of_color(us));
892 if (pos.can_castle_kingside(us))
894 Square rsq = pos.initial_kr_square(us);
895 Square g1 = relative_square(us, SQ_G1);
896 Square f1 = relative_square(us, SQ_F1);
898 bool illegal = false;
900 assert(pos.piece_on(rsq) == rook_of_color(us));
902 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
903 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
904 || pos.square_is_attacked(s, them))
907 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
908 if (s != ksq && s != rsq && pos.square_is_occupied(s))
912 (*mlist++).move = make_castle_move(ksq, rsq);
915 if (pos.can_castle_queenside(us))
917 Square rsq = pos.initial_qr_square(us);
918 Square c1 = relative_square(us, SQ_C1);
919 Square d1 = relative_square(us, SQ_D1);
921 bool illegal = false;
923 assert(pos.piece_on(rsq) == rook_of_color(us));
925 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
926 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
927 || pos.square_is_attacked(s, them))
930 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
931 if (s != ksq && s != rsq && pos.square_is_occupied(s))
934 if ( square_file(rsq) == FILE_B
935 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
936 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
940 (*mlist++).move = make_castle_move(ksq, rsq);