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
49 bool castling_is_check(const Position&, CastlingSide);
52 template<CastlingSide Side>
53 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
55 template<Color Us, Rank, Bitboard, SquareDelta>
56 MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
58 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
59 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
61 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
62 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
64 template<Color, Color, Bitboard, Bitboard, SquareDelta>
65 MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
67 // Template generate_piece_checks() with specializations
69 MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
72 inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
75 return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
77 return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
81 // Template generate_piece_moves() with specializations and overloads
83 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
86 MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
88 template<PieceType Piece, MoveType Type>
89 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
91 assert(Piece == PAWN);
94 return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
95 : generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
97 return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
98 : generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
102 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
105 inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
106 Color us, Bitboard t, Bitboard pnd) {
108 return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, t, m);
110 return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, t, m);
120 /// generate_captures generates() all pseudo-legal captures and queen
121 /// promotions. The return value is the number of moves generated.
123 int generate_captures(const Position& pos, MoveStack* mlist) {
126 assert(!pos.is_check());
128 Color us = pos.side_to_move();
129 Bitboard target = pos.pieces_of_color(opposite_color(us));
130 MoveStack* mlist_start = mlist;
132 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
133 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
134 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
135 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
136 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
137 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
138 return int(mlist - mlist_start);
142 /// generate_noncaptures() generates all pseudo-legal non-captures and
143 /// underpromotions. The return value is the number of moves generated.
145 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
148 assert(!pos.is_check());
150 Color us = pos.side_to_move();
151 Bitboard target = pos.empty_squares();
152 MoveStack* mlist_start = mlist;
154 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
155 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
156 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
157 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
158 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
159 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
160 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
161 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
162 return int(mlist - mlist_start);
166 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
167 /// checks. It returns the number of generated moves.
169 int generate_checks(const Position& pos, MoveStack* mlist) {
172 assert(!pos.is_check());
174 Color us = pos.side_to_move();
175 Square ksq = pos.king_square(opposite_color(us));
176 Bitboard dc = pos.discovered_check_candidates(us);
177 MoveStack* mlist_start = mlist;
179 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
182 mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
183 mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
184 mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
185 mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
186 mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
187 mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
189 // Castling moves that give check. Very rare but nice to have!
190 if ( pos.can_castle_queenside(us)
191 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
192 && castling_is_check(pos, QUEEN_SIDE))
193 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
195 if ( pos.can_castle_kingside(us)
196 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
197 && castling_is_check(pos, KING_SIDE))
198 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
200 return int(mlist - mlist_start);
204 /// generate_evasions() generates all check evasions when the side to move is
205 /// in check. Unlike the other move generation functions, this one generates
206 /// only legal moves. It returns the number of generated moves.
208 int generate_evasions(const Position& pos, MoveStack* mlist) {
211 assert(pos.is_check());
214 Color us = pos.side_to_move();
215 Color them = opposite_color(us);
216 Square ksq = pos.king_square(us);
217 MoveStack* mlist_start = mlist;
219 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
221 // The bitboard of occupied pieces without our king
222 Bitboard b_noKing = pos.occupied_squares();
223 clear_bit(&b_noKing, ksq);
225 // Find squares attacked by slider checkers, we will
226 // remove them from king evasions set so to avoid a couple
227 // of cycles in the slow king evasions legality check loop
228 // and to be able to use square_is_attacked().
229 Bitboard checkers = pos.checkers();
230 Bitboard checkersAttacks = EmptyBoardBB;
231 Bitboard b = checkers & (pos.queens() | pos.bishops());
234 from = pop_1st_bit(&b);
235 checkersAttacks |= bishop_attacks_bb(from, b_noKing);
238 b = checkers & (pos.queens() | pos.rooks());
241 from = pop_1st_bit(&b);
242 checkersAttacks |= rook_attacks_bb(from, b_noKing);
245 // Generate evasions for king
246 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
249 to = pop_1st_bit(&b1);
250 // Note that we can use square_is_attacked() only because we
251 // have already removed slider checkers.
252 if (!pos.square_is_attacked(to, 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 if (!(checkers & (checkers - 1))) // Only one bit set?
261 Square checksq = first_1(checkers);
262 Bitboard pinned = pos.pinned_pieces(us);
264 assert(pos.color_of_piece_on(checksq) == them);
266 // Generate captures of the checking piece
269 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
272 from = pop_1st_bit(&b1);
273 if (relative_rank(us, checksq) == RANK_8)
275 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
276 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
277 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
278 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
280 (*mlist++).move = make_move(from, checksq);
284 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
285 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
286 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & ~pinned;
290 from = pop_1st_bit(&b1);
291 (*mlist++).move = make_move(from, checksq);
294 // Blocking check evasions are possible only if the checking piece is
296 if (checkers & pos.sliders())
298 Bitboard blockSquares = squares_between(checksq, ksq);
300 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
302 if (blockSquares != EmptyBoardBB)
304 mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
305 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
306 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
307 mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
308 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
312 // Finally, the special case of en passant captures. An en passant
313 // capture can only be a check evasion if the check is not a discovered
314 // check. If pos.ep_square() is set, the last move made must have been
315 // a double pawn push. If, furthermore, the checking piece is a pawn,
316 // an en passant check evasion may be possible.
317 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
319 to = pos.ep_square();
320 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
322 // The checking pawn cannot be a discovered (bishop) check candidate
323 // otherwise we were in check also before last double push move.
324 assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
325 assert(count_1s(b1) == 1 || count_1s(b1) == 2);
330 from = pop_1st_bit(&b1);
331 // Move is always legal because checking pawn is not a discovered
332 // check candidate and our capturing pawn has been already tested
333 // against pinned pieces.
334 (*mlist++).move = make_ep_move(from, to);
338 return int(mlist - mlist_start);
342 /// generate_legal_moves() computes a complete list of legal moves in the
343 /// current position. This function is not very fast, and should be used
344 /// only in situations where performance is unimportant. It wouldn't be
345 /// very hard to write an efficient legal move generator, but for the moment
346 /// we don't need it.
348 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
353 return generate_evasions(pos, mlist);
355 // Generate pseudo-legal moves
356 int n = generate_captures(pos, mlist);
357 n += generate_noncaptures(pos, mlist + n);
359 // Remove illegal moves from the list
360 for (int i = 0; i < n; i++)
361 if (!pos.pl_move_is_legal(mlist[i].move))
362 mlist[i--].move = mlist[--n].move;
368 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
369 /// move and a pinned pieces bitboard as input, and tests whether
370 /// the move is legal. If the move is legal, the move itself is
371 /// returned. If not, the function returns false. This function must
372 /// only be used when the side to move is not in check.
374 bool move_is_legal(const Position& pos, const Move m) {
377 assert(!pos.is_check());
378 assert(move_is_ok(m));
380 Color us = pos.side_to_move();
381 Color them = opposite_color(us);
382 Square from = move_from(m);
383 Piece pc = pos.piece_on(from);
385 // If the from square is not occupied by a piece belonging to the side to
386 // move, the move is obviously not legal.
387 if (color_of_piece(pc) != us)
390 Square to = move_to(m);
395 // The piece must be a pawn and destination square must be the
396 // en passant square.
397 if ( type_of_piece(pc) != PAWN
398 || to != pos.ep_square())
401 assert(pos.square_is_empty(to));
402 assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
404 // The move is pseudo-legal, check if it is also legal
405 return pos.pl_move_is_legal(m);
409 if (move_is_short_castle(m))
411 // The piece must be a king and side to move must still have
412 // the right to castle kingside.
413 if ( type_of_piece(pc) != KING
414 ||!pos.can_castle_kingside(us))
417 assert(from == pos.king_square(us));
418 assert(to == pos.initial_kr_square(us));
419 assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
421 Square g1 = relative_square(us, SQ_G1);
422 Square f1 = relative_square(us, SQ_F1);
424 bool illegal = false;
426 // Check if any of the squares between king and rook
427 // is occupied or under attack.
428 for (s = Min(from, g1); s <= Max(from, g1); s++)
429 if ( (s != from && s != to && !pos.square_is_empty(s))
430 || pos.square_is_attacked(s, them))
433 // Check if any of the squares between king and rook
435 for (s = Min(to, f1); s <= Max(to, f1); s++)
436 if (s != from && s != to && !pos.square_is_empty(s))
442 if (move_is_long_castle(m))
444 // The piece must be a king and side to move must still have
445 // the right to castle kingside.
446 if ( type_of_piece(pc) != KING
447 ||!pos.can_castle_queenside(us))
450 assert(from == pos.king_square(us));
451 assert(to == pos.initial_qr_square(us));
452 assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
454 Square c1 = relative_square(us, SQ_C1);
455 Square d1 = relative_square(us, SQ_D1);
457 bool illegal = false;
459 for (s = Min(from, c1); s <= Max(from, c1); s++)
460 if( (s != from && s != to && !pos.square_is_empty(s))
461 || pos.square_is_attacked(s, them))
464 for (s = Min(to, d1); s <= Max(to, d1); s++)
465 if(s != from && s != to && !pos.square_is_empty(s))
468 if ( square_file(to) == FILE_B
469 && ( pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, ROOK)
470 || pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, QUEEN)))
478 // The destination square cannot be occupied by a friendly piece
479 if (pos.color_of_piece_on(to) == us)
482 // Proceed according to the type of the moving piece.
483 if (type_of_piece(pc) == PAWN)
485 // If the destination square is on the 8/1th rank, the move must
487 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
488 ||(square_rank(to) == RANK_1 && us != WHITE))
489 && !move_promotion(m))
492 // Proceed according to the square delta between the source and
493 // destionation squares.
500 // Capture. The destination square must be occupied by an enemy
501 // piece (en passant captures was handled earlier).
502 if (pos.color_of_piece_on(to) != them)
508 // Pawn push. The destination square must be empty.
509 if (!pos.square_is_empty(to))
514 // Double white pawn push. The destination square must be on the fourth
515 // rank, and both the destination square and the square between the
516 // source and destination squares must be empty.
517 if ( square_rank(to) != RANK_4
518 || !pos.square_is_empty(to)
519 || !pos.square_is_empty(from + DELTA_N))
524 // Double black pawn push. The destination square must be on the fifth
525 // rank, and both the destination square and the square between the
526 // source and destination squares must be empty.
527 if ( square_rank(to) != RANK_5
528 || !pos.square_is_empty(to)
529 || !pos.square_is_empty(from + DELTA_S))
536 // The move is pseudo-legal, check if it is also legal
537 return pos.pl_move_is_legal(m);
540 // Luckly we can handle all the other pieces in one go
541 return ( pos.piece_attacks_square(pos.piece_on(from), from, to)
542 && pos.pl_move_is_legal(m)
543 && !move_promotion(m));
549 template<PieceType Piece>
550 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
555 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
557 from = pos.piece_list(us, Piece, i);
558 b = pos.piece_attacks<Piece>(from) & target;
564 template<PieceType Piece>
565 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
566 Color us, Bitboard target, Bitboard pinned) {
570 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
572 from = pos.piece_list(us, Piece, i);
573 if (pinned && bit_is_set(pinned, from))
576 b = pos.piece_attacks<Piece>(from) & target;
583 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
586 Square from = pos.king_square(us);
588 b = pos.piece_attacks<KING>(from) & target;
593 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
594 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
596 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
599 Bitboard pawns = pos.pawns(Us);
600 Bitboard enemyPieces = pos.pieces_of_color(Them);
602 // Captures in the a1-h8 (a8-h1 for black) direction
603 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
605 // Capturing promotions
606 Bitboard b2 = b1 & TRank8BB;
609 to = pop_1st_bit(&b2);
610 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
613 // Capturing non-promotions
617 to = pop_1st_bit(&b2);
618 (*mlist++).move = make_move(to - TDELTA_NE, to);
621 // Captures in the h1-a8 (h8-a1 for black) direction
622 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
624 // Capturing promotions
628 to = pop_1st_bit(&b2);
629 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
632 // Capturing non-promotions
636 to = pop_1st_bit(&b2);
637 (*mlist++).move = make_move(to - TDELTA_NW, to);
640 // Non-capturing promotions
641 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
644 to = pop_1st_bit(&b1);
645 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
648 // En passant captures
649 if (pos.ep_square() != SQ_NONE)
651 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
652 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
654 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
655 assert(b1 != EmptyBoardBB);
659 to = pop_1st_bit(&b1);
660 (*mlist++).move = make_ep_move(to, pos.ep_square());
666 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
667 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
669 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
671 Bitboard pawns = pos.pawns(Us);
672 Bitboard enemyPieces = pos.pieces_of_color(Them);
673 Bitboard emptySquares = pos.empty_squares();
677 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
678 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
681 to = pop_1st_bit(&b1);
682 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
683 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
684 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
687 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
688 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
691 to = pop_1st_bit(&b1);
692 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
693 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
694 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
697 // Single pawn pushes
698 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
702 to = pop_1st_bit(&b2);
703 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
704 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
705 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
710 to = pop_1st_bit(&b2);
711 (*mlist++).move = make_move(to - TDELTA_N, to);
714 // Double pawn pushes
715 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
718 to = pop_1st_bit(&b2);
719 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
725 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
726 MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
728 // Find all friendly pawns not on the enemy king's file
730 Bitboard empty = pos.empty_squares();
732 if (dc != EmptyBoardBB)
734 // Pawn moves which gives discovered check. This is possible only if the
735 // pawn is not on the same file as the enemy king, because we don't
736 // generate captures.
737 b1 = pos.pawns(Us) & ~file_bb(ksq);
739 // Discovered checks, single pawn pushes, no promotions
740 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
743 Square to = pop_1st_bit(&b3);
744 (*mlist++).move = make_move(to - TDELTA_N, to);
747 // Discovered checks, double pawn pushes
748 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
751 Square to = pop_1st_bit(&b3);
752 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
756 // Direct checks. These are possible only for pawns on neighboring files
757 // of the enemy king.
758 b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
760 // Direct checks, single pawn pushes
761 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
762 b3 = b2 & pos.pawn_attacks(Them, ksq);
765 Square to = pop_1st_bit(&b3);
766 (*mlist++).move = make_move(to - TDELTA_N, to);
769 // Direct checks, double pawn pushes
770 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
772 & pos.pawn_attacks(Them, ksq);
775 Square to = pop_1st_bit(&b3);
776 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
781 template<PieceType Piece>
782 MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
783 Bitboard dc, Square ksq) {
785 Bitboard target = pos.pieces_of_color_and_type(us, Piece);
788 Bitboard b = target & dc;
791 Square from = pop_1st_bit(&b);
792 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
794 bb &= ~QueenPseudoAttacks[ksq];
801 if (Piece == KING || !b)
804 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
807 Square from = pop_1st_bit(&b);
808 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
814 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
815 MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
816 Bitboard blockSquares, MoveStack* mlist) {
819 // Find non-pinned pawns
820 Bitboard b1 = pos.pawns(Us) & ~pinned;
822 // Single pawn pushes. We don't have to AND with empty squares here,
823 // because the blocking squares will always be empty.
824 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
827 to = pop_1st_bit(&b2);
829 assert(pos.piece_on(to) == EMPTY);
831 if (square_rank(to) == TRANK_8)
833 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
834 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
835 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
836 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
838 (*mlist++).move = make_move(to - TDELTA_N, to);
841 // Double pawn pushes
842 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
843 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;
846 to = pop_1st_bit(&b2);
848 assert(pos.piece_on(to) == EMPTY);
849 assert(Us != WHITE || square_rank(to) == RANK_4);
850 assert(Us != BLACK || square_rank(to) == RANK_5);
852 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
857 template<CastlingSide Side>
858 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
860 Color us = pos.side_to_move();
862 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
863 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
865 Color them = opposite_color(us);
866 Square ksq = pos.king_square(us);
868 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
870 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
871 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
872 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
874 bool illegal = false;
876 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
878 // It is a bit complicated to correctly handle Chess960
879 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
880 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
881 || pos.square_is_attacked(s, them))
884 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
885 if (s != ksq && s != rsq && pos.square_is_occupied(s))
888 if ( Side == QUEEN_SIDE
889 && square_file(rsq) == FILE_B
890 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
891 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
895 (*mlist++).move = make_castle_move(ksq, rsq);
900 bool castling_is_check(const Position& pos, CastlingSide side) {
902 // After castling opponent king is attacked by the castled rook?
903 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
904 Color us = pos.side_to_move();
905 Square ksq = pos.king_square(us);
906 Bitboard occ = pos.occupied_squares();
908 clear_bit(&occ, ksq); // Remove our king from the board
909 Square rsq = make_square(rookFile, square_rank(ksq));
910 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));