2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2009 Marco Costalba
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 // Simple macro to wrap a very common while loop, no facny, no flexibility,
31 // hardcoded list name 'mlist' and from square 'from'.
32 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
34 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
35 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
38 //// Local definitions
54 bool castling_is_check(const Position&, CastlingSide);
57 template<CastlingSide Side>
58 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
61 MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
64 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
66 template<Color Us, SquareDelta Diagonal>
67 MoveStack* generate_pawn_captures_diagonal(MoveStack* mlist, Bitboard pawns, Bitboard enemyPieces, bool promotion);
70 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
73 MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
75 template<Color Us, SquareDelta Direction>
76 inline Bitboard move_pawns(Bitboard p) {
78 if (Direction == DELTA_N)
79 return Us == WHITE ? p << 8 : p >> 8;
80 else if (Direction == DELTA_NE)
81 return Us == WHITE ? p << 9 : p >> 7;
82 else if (Direction == DELTA_NW)
83 return Us == WHITE ? p << 7 : p >> 9;
88 // Template generate_piece_checks() with specializations
90 MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
93 inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
95 return (us == WHITE ? generate_pawn_checks<WHITE>(p, dc, ksq, m)
96 : generate_pawn_checks<BLACK>(p, dc, ksq, m));
99 // Template generate_piece_moves() with specializations and overloads
101 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
104 MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
106 template<PieceType Piece, MoveType Type>
107 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
109 assert(Piece == PAWN);
112 return (us == WHITE ? generate_pawn_captures<WHITE>(p, m)
113 : generate_pawn_captures<BLACK>(p, m));
115 return (us == WHITE ? generate_pawn_noncaptures<WHITE>(p, m)
116 : generate_pawn_noncaptures<BLACK>(p, m));
120 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
123 inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
124 Color us, Bitboard t, Bitboard pnd) {
126 return (us == WHITE ? generate_pawn_blocking_evasions<WHITE>(p, pnd, t, m)
127 : generate_pawn_blocking_evasions<BLACK>(p, pnd, t, m));
137 /// generate_captures generates() all pseudo-legal captures and queen
138 /// promotions. The return value is the number of moves generated.
140 int generate_captures(const Position& pos, MoveStack* mlist) {
143 assert(!pos.is_check());
145 Color us = pos.side_to_move();
146 Bitboard target = pos.pieces_of_color(opposite_color(us));
147 MoveStack* mlist_start = mlist;
149 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
150 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
151 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
152 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
153 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
154 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
155 return int(mlist - mlist_start);
159 /// generate_noncaptures() generates all pseudo-legal non-captures and
160 /// underpromotions. The return value is the number of moves generated.
162 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
165 assert(!pos.is_check());
167 Color us = pos.side_to_move();
168 Bitboard target = pos.empty_squares();
169 MoveStack* mlist_start = mlist;
171 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
172 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
173 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
174 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
175 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
176 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
177 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
178 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
179 return int(mlist - mlist_start);
183 /// generate_non_capture_checks() generates all pseudo-legal non-capturing,
184 /// non-promoting checks. It returns the number of generated moves.
186 int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
189 assert(!pos.is_check());
191 Color us = pos.side_to_move();
192 Square ksq = pos.king_square(opposite_color(us));
193 MoveStack* mlist_start = mlist;
195 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
198 mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
199 mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
200 mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
201 mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
202 mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
203 mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
205 // Castling moves that give check. Very rare but nice to have!
206 if ( pos.can_castle_queenside(us)
207 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
208 && castling_is_check(pos, QUEEN_SIDE))
209 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
211 if ( pos.can_castle_kingside(us)
212 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
213 && castling_is_check(pos, KING_SIDE))
214 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
216 return int(mlist - mlist_start);
220 /// generate_evasions() generates all check evasions when the side to move is
221 /// in check. Unlike the other move generation functions, this one generates
222 /// only legal moves. It returns the number of generated moves.
224 int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
227 assert(pos.is_check());
230 Color us = pos.side_to_move();
231 Color them = opposite_color(us);
232 Square ksq = pos.king_square(us);
233 MoveStack* mlist_start = mlist;
235 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
237 // The bitboard of occupied pieces without our king
238 Bitboard b_noKing = pos.occupied_squares();
239 clear_bit(&b_noKing, ksq);
241 // Find squares attacked by slider checkers, we will
242 // remove them from king evasions set so to avoid a couple
243 // of cycles in the slow king evasions legality check loop
244 // and to be able to use square_is_attacked().
245 Bitboard checkers = pos.checkers();
246 Bitboard checkersAttacks = EmptyBoardBB;
247 Bitboard b = checkers & (pos.queens() | pos.bishops());
250 from = pop_1st_bit(&b);
251 checkersAttacks |= bishop_attacks_bb(from, b_noKing);
254 b = checkers & (pos.queens() | pos.rooks());
257 from = pop_1st_bit(&b);
258 checkersAttacks |= rook_attacks_bb(from, b_noKing);
261 // Generate evasions for king
262 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
265 to = pop_1st_bit(&b1);
266 // Note that we can use square_is_attacked() only because we
267 // have already removed slider checkers.
268 if (!pos.square_is_attacked(to, them))
269 (*mlist++).move = make_move(ksq, to);
272 // Generate evasions for other pieces only if not double check. We use a
273 // simple bit twiddling hack here rather than calling count_1s in order to
274 // save some time (we know that pos.checkers() has at most two nonzero bits).
275 if (!(checkers & (checkers - 1))) // Only one bit set?
277 Square checksq = first_1(checkers);
279 assert(pos.color_of_piece_on(checksq) == them);
281 // Generate captures of the checking piece
284 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
287 from = pop_1st_bit(&b1);
288 if (relative_rank(us, checksq) == RANK_8)
290 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
291 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
292 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
293 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
295 (*mlist++).move = make_move(from, checksq);
299 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
300 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
301 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & ~pinned;
305 from = pop_1st_bit(&b1);
306 (*mlist++).move = make_move(from, checksq);
309 // Blocking check evasions are possible only if the checking piece is
311 if (checkers & pos.sliders())
313 Bitboard blockSquares = squares_between(checksq, ksq);
315 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
317 if (blockSquares != EmptyBoardBB)
319 mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
320 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
321 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
322 mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
323 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
327 // Finally, the special case of en passant captures. An en passant
328 // capture can only be a check evasion if the check is not a discovered
329 // check. If pos.ep_square() is set, the last move made must have been
330 // a double pawn push. If, furthermore, the checking piece is a pawn,
331 // an en passant check evasion may be possible.
332 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
334 to = pos.ep_square();
335 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
337 // The checking pawn cannot be a discovered (bishop) check candidate
338 // otherwise we were in check also before last double push move.
339 assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
340 assert(count_1s(b1) == 1 || count_1s(b1) == 2);
345 from = pop_1st_bit(&b1);
346 // Move is always legal because checking pawn is not a discovered
347 // check candidate and our capturing pawn has been already tested
348 // against pinned pieces.
349 (*mlist++).move = make_ep_move(from, to);
353 return int(mlist - mlist_start);
357 /// generate_legal_moves() computes a complete list of legal moves in the
358 /// current position. This function is not very fast, and should be used
359 /// only in situations where performance is unimportant. It wouldn't be
360 /// very hard to write an efficient legal move generator, but for the moment
361 /// we don't need it.
363 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
367 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
370 return generate_evasions(pos, mlist, pinned);
372 // Generate pseudo-legal moves
373 int n = generate_captures(pos, mlist);
374 n += generate_noncaptures(pos, mlist + n);
376 // Remove illegal moves from the list
377 for (int i = 0; i < n; i++)
378 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
379 mlist[i--].move = mlist[--n].move;
385 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
386 /// move and a pinned pieces bitboard as input, and tests whether
387 /// the move is legal. If the move is legal, the move itself is
388 /// returned. If not, the function returns false. This function must
389 /// only be used when the side to move is not in check.
391 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
394 assert(!pos.is_check());
395 assert(move_is_ok(m));
396 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
398 Color us = pos.side_to_move();
399 Square from = move_from(m);
400 Piece pc = pos.piece_on(from);
402 // If the from square is not occupied by a piece belonging to the side to
403 // move, the move is obviously not legal.
404 if (color_of_piece(pc) != us)
407 Color them = opposite_color(us);
408 Square to = move_to(m);
413 // The piece must be a pawn and destination square must be the
414 // en passant square.
415 if ( type_of_piece(pc) != PAWN
416 || to != pos.ep_square())
419 assert(pos.square_is_empty(to));
420 assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
422 // The move is pseudo-legal, check if it is also legal
423 return pos.pl_move_is_legal(m, pinned);
427 if (move_is_short_castle(m))
429 // The piece must be a king and side to move must still have
430 // the right to castle kingside.
431 if ( type_of_piece(pc) != KING
432 ||!pos.can_castle_kingside(us))
435 assert(from == pos.king_square(us));
436 assert(to == pos.initial_kr_square(us));
437 assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
439 Square g1 = relative_square(us, SQ_G1);
440 Square f1 = relative_square(us, SQ_F1);
442 bool illegal = false;
444 // Check if any of the squares between king and rook
445 // is occupied or under attack.
446 for (s = Min(from, g1); s <= Max(from, g1); s++)
447 if ( (s != from && s != to && !pos.square_is_empty(s))
448 || pos.square_is_attacked(s, them))
451 // Check if any of the squares between king and rook
453 for (s = Min(to, f1); s <= Max(to, f1); s++)
454 if (s != from && s != to && !pos.square_is_empty(s))
460 if (move_is_long_castle(m))
462 // The piece must be a king and side to move must still have
463 // the right to castle kingside.
464 if ( type_of_piece(pc) != KING
465 ||!pos.can_castle_queenside(us))
468 assert(from == pos.king_square(us));
469 assert(to == pos.initial_qr_square(us));
470 assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
472 Square c1 = relative_square(us, SQ_C1);
473 Square d1 = relative_square(us, SQ_D1);
475 bool illegal = false;
477 for (s = Min(from, c1); s <= Max(from, c1); s++)
478 if( (s != from && s != to && !pos.square_is_empty(s))
479 || pos.square_is_attacked(s, them))
482 for (s = Min(to, d1); s <= Max(to, d1); s++)
483 if(s != from && s != to && !pos.square_is_empty(s))
486 if ( square_file(to) == FILE_B
487 && ( pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, ROOK)
488 || pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, QUEEN)))
496 // The destination square cannot be occupied by a friendly piece
497 if (pos.color_of_piece_on(to) == us)
500 // Proceed according to the type of the moving piece.
501 if (type_of_piece(pc) == PAWN)
503 // Move direction must be compatible with pawn color
504 int direction = to - from;
505 if ((us == WHITE) != (direction > 0))
508 // If the destination square is on the 8/1th rank, the move must
510 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
511 ||(square_rank(to) == RANK_1 && us != WHITE))
512 && !move_is_promotion(m))
515 // Proceed according to the square delta between the source and
516 // destionation squares.
523 // Capture. The destination square must be occupied by an enemy
524 // piece (en passant captures was handled earlier).
525 if (pos.color_of_piece_on(to) != them)
531 // Pawn push. The destination square must be empty.
532 if (!pos.square_is_empty(to))
537 // Double white pawn push. The destination square must be on the fourth
538 // rank, and both the destination square and the square between the
539 // source and destination squares must be empty.
540 if ( square_rank(to) != RANK_4
541 || !pos.square_is_empty(to)
542 || !pos.square_is_empty(from + DELTA_N))
547 // Double black pawn push. The destination square must be on the fifth
548 // rank, and both the destination square and the square between the
549 // source and destination squares must be empty.
550 if ( square_rank(to) != RANK_5
551 || !pos.square_is_empty(to)
552 || !pos.square_is_empty(from + DELTA_S))
559 // The move is pseudo-legal, check if it is also legal
560 return pos.pl_move_is_legal(m, pinned);
563 // Luckly we can handle all the other pieces in one go
564 return ( pos.piece_attacks_square(pos.piece_on(from), from, to)
565 && pos.pl_move_is_legal(m, pinned)
566 && !move_is_promotion(m));
570 /// Another version of move_is_legal(), which takes only a position and a move
571 /// as input. This function does not require that the side to move is not in
572 /// check. It is not optimized for speed, and is only used for verifying move
573 /// legality when building a PV from the transposition table.
575 bool move_is_legal(const Position& pos, const Move m) {
577 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
579 return move_is_legal(pos, m, pinned);
584 int n = generate_evasions(p, moves, pinned);
585 for (int i = 0; i < n; i++)
586 if (moves[i].move == m)
595 template<PieceType Piece>
596 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
601 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
603 from = pos.piece_list(us, Piece, i);
604 b = pos.piece_attacks<Piece>(from) & target;
610 template<PieceType Piece>
611 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
612 Color us, Bitboard target, Bitboard pinned) {
616 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
618 from = pos.piece_list(us, Piece, i);
619 if (pinned && bit_is_set(pinned, from))
622 b = pos.piece_attacks<Piece>(from) & target;
629 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
632 Square from = pos.king_square(us);
634 b = pos.piece_attacks<KING>(from) & target;
639 template<Color Us, SquareDelta Diagonal>
640 MoveStack* generate_pawn_captures_diagonal(MoveStack* mlist, Bitboard pawns, Bitboard enemyPieces, bool promotion) {
642 // Calculate our parametrized parameters at compile time
643 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
644 const Bitboard TFileABB = (Diagonal == DELTA_NE ? FileABB : FileHBB);
645 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
646 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
647 const SquareDelta TTDELTA_NE = (Diagonal == DELTA_NE ? TDELTA_NE : TDELTA_NW);
651 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
652 Bitboard b1 = move_pawns<Us, Diagonal>(pawns) & ~TFileABB & enemyPieces;
654 // Capturing promotions
657 Bitboard b2 = b1 & TRank8BB;
661 to = pop_1st_bit(&b2);
662 (*mlist++).move = make_promotion_move(to - TTDELTA_NE, to, QUEEN);
666 // Capturing non-promotions
667 SERIALIZE_MOVES_D(b1, -TTDELTA_NE);
672 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
674 // Calculate our parametrized parameters at compile time
675 const Color Them = (Us == WHITE ? BLACK : WHITE);
676 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
677 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
678 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
681 Bitboard pawns = pos.pawns(Us);
682 Bitboard enemyPieces = pos.pieces_of_color(opposite_color(Us));
683 bool possiblePromotion = (pawns & TRank7BB);
685 // Standard captures and capturing promotions in both directions
686 mlist = generate_pawn_captures_diagonal<Us, DELTA_NE>(mlist, pawns, enemyPieces, possiblePromotion);
687 mlist = generate_pawn_captures_diagonal<Us, DELTA_NW>(mlist, pawns, enemyPieces, possiblePromotion);
689 // Non-capturing promotions
690 if (possiblePromotion)
692 Bitboard b1 = move_pawns<Us, DELTA_N>(pawns) & pos.empty_squares() & TRank8BB;
695 to = pop_1st_bit(&b1);
696 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
700 // En passant captures
701 if (pos.ep_square() != SQ_NONE)
703 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
704 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
706 Bitboard b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
707 assert(b1 != EmptyBoardBB);
711 to = pop_1st_bit(&b1);
712 (*mlist++).move = make_ep_move(to, pos.ep_square());
719 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
721 // Calculate our parametrized parameters at compile time
722 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
723 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
724 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
725 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
726 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
727 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
731 Bitboard pawns = pos.pawns(Us);
732 Bitboard emptySquares = pos.empty_squares();
734 if (pawns & TRank7BB) // There is some promotion candidate ?
736 Bitboard enemyPieces = pos.pieces_of_color(opposite_color(Us));
738 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
739 b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces & TRank8BB;
742 to = pop_1st_bit(&b1);
743 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
744 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
745 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
748 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
749 b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces & TRank8BB;
752 to = pop_1st_bit(&b1);
753 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
754 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
755 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
758 // Underpromotion pawn pushes
759 b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares & TRank8BB;
762 to = pop_1st_bit(&b1);
763 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
764 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
765 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
769 // Single pawn pushes
770 b2 = b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares & ~TRank8BB;
771 SERIALIZE_MOVES_D(b2, -TDELTA_N);
773 // Double pawn pushes
774 b2 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
775 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
781 MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
783 // Calculate our parametrized parameters at compile time
784 const Color Them = (Us == WHITE ? BLACK : WHITE);
785 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
786 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
787 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
788 const SquareDelta TDELTA_S = (Us == WHITE ? DELTA_S : DELTA_N);
792 Bitboard pawns = pos.pawns(Us);
796 Bitboard empty = pos.empty_squares();
798 // Pawn moves which gives discovered check. This is possible only if the
799 // pawn is not on the same file as the enemy king, because we don't
800 // generate captures.
801 b1 = pawns & ~file_bb(ksq);
803 // Discovered checks, single pawn pushes, no promotions
804 b2 = b3 = move_pawns<Us, DELTA_N>(b1 & dc) & empty & ~TRank8BB;
805 SERIALIZE_MOVES_D(b3, -TDELTA_N);
807 // Discovered checks, double pawn pushes
808 b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty;
809 SERIALIZE_MOVES_D(b3, -TDELTA_N -TDELTA_N);
812 // Direct checks. These are possible only for pawns on neighboring files
813 // and in the two ranks that, after the push, are in front of the enemy king.
814 b1 = pawns & neighboring_files_bb(ksq) & ~dc;
816 // We can get false positives if (ksq + x) is not in [0,63] range but
817 // is not a problem, they will be filtered out later.
818 b2 = b1 & (rank_bb(ksq + 2 * TDELTA_S) | rank_bb(ksq + 3 * TDELTA_S));
822 // Direct checks, single pawn pushes
823 Bitboard empty = pos.empty_squares();
824 b2 = move_pawns<Us, DELTA_N>(b1) & empty;
825 b3 = b2 & pos.pawn_attacks(Them, ksq);
826 SERIALIZE_MOVES_D(b3, -TDELTA_N);
828 // Direct checks, double pawn pushes
829 b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty & pos.pawn_attacks(Them, ksq);
830 SERIALIZE_MOVES_D(b3, -TDELTA_N -TDELTA_N);
834 template<PieceType Piece>
835 MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
836 Bitboard dc, Square ksq) {
838 Bitboard target = pos.pieces_of_color_and_type(us, Piece);
841 Bitboard b = target & dc;
844 Square from = pop_1st_bit(&b);
845 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
847 bb &= ~QueenPseudoAttacks[ksq];
854 if (Piece != KING || b)
856 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
862 Square from = pop_1st_bit(&b);
863 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
864 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
865 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
868 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
876 MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
877 Bitboard blockSquares, MoveStack* mlist) {
879 // Calculate our parametrized parameters at compile time
880 const Rank TRANK_8 = (Us == WHITE ? RANK_8 : RANK_1);
881 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
882 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
886 // Find non-pinned pawns and push them one square
887 Bitboard b1 = move_pawns<Us, DELTA_N>(pos.pawns(Us) & ~pinned);
889 // We don't have to AND with empty squares here,
890 // because the blocking squares will always be empty.
891 Bitboard b2 = b1 & blockSquares;
894 to = pop_1st_bit(&b2);
896 assert(pos.piece_on(to) == EMPTY);
898 if (square_rank(to) == TRANK_8)
900 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
901 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
902 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
903 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
905 (*mlist++).move = make_move(to - TDELTA_N, to);
908 // Double pawn pushes
909 b2 = b1 & pos.empty_squares() & TRank3BB;
910 b2 = move_pawns<Us, DELTA_N>(b2) & blockSquares;
913 to = pop_1st_bit(&b2);
915 assert(pos.piece_on(to) == EMPTY);
916 assert(Us != WHITE || square_rank(to) == RANK_4);
917 assert(Us != BLACK || square_rank(to) == RANK_5);
919 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
924 template<CastlingSide Side>
925 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
927 Color us = pos.side_to_move();
929 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
930 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
932 Color them = opposite_color(us);
933 Square ksq = pos.king_square(us);
935 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
937 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
938 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
939 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
941 bool illegal = false;
943 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
945 // It is a bit complicated to correctly handle Chess960
946 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
947 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
948 || pos.square_is_attacked(s, them))
951 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
952 if (s != ksq && s != rsq && pos.square_is_occupied(s))
955 if ( Side == QUEEN_SIDE
956 && square_file(rsq) == FILE_B
957 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
958 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
962 (*mlist++).move = make_castle_move(ksq, rsq);
967 bool castling_is_check(const Position& pos, CastlingSide side) {
969 // After castling opponent king is attacked by the castled rook?
970 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
971 Color us = pos.side_to_move();
972 Square ksq = pos.king_square(us);
973 Bitboard occ = pos.occupied_squares();
975 clear_bit(&occ, ksq); // Remove our king from the board
976 Square rsq = make_square(rookFile, square_rank(ksq));
977 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));