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
43 const bool CAPTURE = true;
44 const bool NON_CAPTURE = false;
47 bool castling_is_check(const Position&, CastlingSide);
50 template<CastlingSide Side>
51 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
53 template<Color Us, Rank, Bitboard, SquareDelta>
54 MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
56 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
57 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
59 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
60 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
62 template<Color, Color, Bitboard, Bitboard, SquareDelta>
63 MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
65 // Template generate_piece_checks() with specializations
67 MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
70 inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
73 return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
75 return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
79 // Template generate_piece_moves() with specializations
81 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
84 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
86 template<PieceType Piece, bool Capture>
87 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
89 assert(Piece == PAWN);
92 return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
93 : generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
95 return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
96 : generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
99 // Template generate_piece_blocking_evasions() with specializations
101 MoveStack* generate_piece_blocking_evasions(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
104 inline MoveStack* generate_piece_blocking_evasions<PAWN>(const Position& p, MoveStack* m, Color us,
105 Bitboard pnd, Bitboard bs) {
107 return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, bs, m);
109 return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, bs, m);
119 /// generate_captures generates() all pseudo-legal captures and queen
120 /// promotions. The return value is the number of moves generated.
122 int generate_captures(const Position& pos, MoveStack* mlist) {
125 assert(!pos.is_check());
127 Color us = pos.side_to_move();
128 Bitboard target = pos.pieces_of_color(opposite_color(us));
129 MoveStack* mlist_start = mlist;
131 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
132 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
133 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
134 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
135 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
136 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
137 return int(mlist - mlist_start);
141 /// generate_noncaptures() generates all pseudo-legal non-captures and
142 /// underpromotions. The return value is the number of moves generated.
144 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
147 assert(!pos.is_check());
149 Color us = pos.side_to_move();
150 Bitboard target = pos.empty_squares();
151 MoveStack* mlist_start = mlist;
153 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
154 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
155 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
156 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
157 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
158 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
159 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
160 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
161 return int(mlist - mlist_start);
165 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
166 /// checks. It returns the number of generated moves.
168 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
171 assert(!pos.is_check());
173 Color us = pos.side_to_move();
174 Square ksq = pos.king_square(opposite_color(us));
175 MoveStack* mlist_start = mlist;
177 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
180 mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
181 mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
182 mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
183 mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
184 mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
185 mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
187 // Castling moves that give check. Very rare but nice to have!
188 if ( pos.can_castle_queenside(us)
189 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
190 && castling_is_check(pos, QUEEN_SIDE))
191 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
193 if ( pos.can_castle_kingside(us)
194 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
195 && castling_is_check(pos, KING_SIDE))
196 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
198 return int(mlist - mlist_start);
202 /// generate_evasions() generates all check evasions when the side to move is
203 /// in check. Unlike the other move generation functions, this one generates
204 /// only legal moves. It returns the number of generated moves. This
205 /// function is very ugly, and needs cleaning up some time later. FIXME
207 int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
210 assert(pos.is_check());
213 Color us = pos.side_to_move();
214 Color them = opposite_color(us);
215 Square ksq = pos.king_square(us);
216 MoveStack* mlist_start = mlist;
218 assert(pos.piece_on(ksq) == king_of_color(us));
220 // The bitboard of occupied pieces without our king
221 Bitboard b2 = pos.occupied_squares();
224 // Find squares attacked by slider checkers, we will
225 // remove them from king evasions set so to avoid a couple
226 // of cycles in the slow king evasions legality check loop.
227 Bitboard checkers = pos.checkers();
228 Bitboard checkersAttacks = EmptyBoardBB;
229 Bitboard b = checkers & (pos.queens() | pos.bishops());
232 from = pop_1st_bit(&b);
233 checkersAttacks |= bishop_attacks_bb(from, b2);
236 b = checkers & (pos.queens() | pos.rooks());
239 from = pop_1st_bit(&b);
240 checkersAttacks |= rook_attacks_bb(from, b2);
243 // Generate evasions for king
244 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
247 to = pop_1st_bit(&b1);
249 // Make sure 'to' is not attacked by the other side. This is a bit ugly,
250 // because we can't use Position::square_is_attacked. Instead we use
251 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
252 // b2 (the occupied squares with the king removed) in order to test whether
253 // the king will remain in check on the destination square.
254 if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
255 || (pos.pawn_attacks(us, to) & pos.pawns(them))
256 || (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
257 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
258 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
259 (*mlist++).move = make_move(ksq, to);
262 // Generate evasions for other pieces only if not double check. We use a
263 // simple bit twiddling hack here rather than calling count_1s in order to
264 // save some time (we know that pos.checkers() has at most two nonzero bits).
265 if (!(checkers & (checkers - 1))) // Only one bit set?
267 Square checksq = first_1(checkers);
269 assert(pos.color_of_piece_on(checksq) == them);
271 // Generate captures of the checking piece
274 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
277 from = pop_1st_bit(&b1);
278 if (relative_rank(us, checksq) == RANK_8)
280 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
281 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
282 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
283 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
285 (*mlist++).move = make_move(from, checksq);
289 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
290 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
291 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & ~pinned;
295 from = pop_1st_bit(&b1);
296 (*mlist++).move = make_move(from, checksq);
299 // Blocking check evasions are possible only if the checking piece is
301 if (checkers & pos.sliders())
303 Bitboard blockSquares = squares_between(checksq, ksq);
305 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
308 mlist = generate_piece_blocking_evasions<PAWN>(pos, mlist, us, pinned, blockSquares);
309 mlist = generate_piece_blocking_evasions<KNIGHT>(pos, mlist, us, pinned, blockSquares);
310 mlist = generate_piece_blocking_evasions<BISHOP>(pos, mlist, us, pinned, blockSquares);
311 mlist = generate_piece_blocking_evasions<ROOK>(pos, mlist, us, pinned, blockSquares);
312 mlist = generate_piece_blocking_evasions<QUEEN>(pos, mlist, us, pinned, blockSquares);
315 // Finally, the ugly special case of en passant captures. An en passant
316 // capture can only be a check evasion if the check is not a discovered
317 // check. If pos.ep_square() is set, the last move made must have been
318 // a double pawn push. If, furthermore, the checking piece is a pawn,
319 // an en passant check evasion may be possible.
320 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
322 to = pos.ep_square();
323 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
325 assert(b1 != EmptyBoardBB);
330 from = pop_1st_bit(&b1);
332 // Before generating the move, we have to make sure it is legal.
333 // This is somewhat tricky, because the two disappearing pawns may
334 // cause new "discovered checks". We test this by removing the
335 // two relevant bits from the occupied squares bitboard, and using
336 // the low-level bitboard functions for bishop and rook attacks.
337 b2 = pos.occupied_squares();
338 clear_bit(&b2, from);
339 clear_bit(&b2, checksq);
340 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
341 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
343 (*mlist++).move = make_ep_move(from, to);
347 return int(mlist - mlist_start);
351 /// generate_legal_moves() computes a complete list of legal moves in the
352 /// current position. This function is not very fast, and should be used
353 /// only in situations where performance is unimportant. It wouldn't be
354 /// very hard to write an efficient legal move generator, but for the moment
355 /// we don't need it.
357 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
361 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
364 return generate_evasions(pos, mlist, pinned);
366 // Generate pseudo-legal moves
367 int n = generate_captures(pos, mlist);
368 n += generate_noncaptures(pos, mlist + n);
370 // Remove illegal moves from the list
371 for (int i = 0; i < n; i++)
372 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
373 mlist[i--].move = mlist[--n].move;
379 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
380 /// move and a pinned pieces bitboard as input, and tests whether
381 /// the move is legal. If the move is legal, the move itself is
382 /// returned. If not, the function returns false. This function must
383 /// only be used when the side to move is not in check.
385 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
388 assert(!pos.is_check());
389 assert(move_is_ok(m));
390 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
392 Color us = pos.side_to_move();
393 Color them = opposite_color(us);
394 Square from = move_from(m);
395 Piece pc = pos.piece_on(from);
397 // If the from square is not occupied by a piece belonging to the side to
398 // move, the move is obviously not legal.
399 if (color_of_piece(pc) != us)
402 Square to = move_to(m);
407 // The piece must be a pawn and destination square must be the
408 // en passant square.
409 if ( type_of_piece(pc) != PAWN
410 || to != pos.ep_square())
413 assert(pos.square_is_empty(to));
414 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
416 // The move is pseudo-legal, check if it is also legal
417 return pos.pl_move_is_legal(m, pinned);
421 if (move_is_short_castle(m))
423 // The piece must be a king and side to move must still have
424 // the right to castle kingside.
425 if ( type_of_piece(pc) != KING
426 ||!pos.can_castle_kingside(us))
429 assert(from == pos.king_square(us));
430 assert(to == pos.initial_kr_square(us));
431 assert(pos.piece_on(to) == rook_of_color(us));
433 Square g1 = relative_square(us, SQ_G1);
434 Square f1 = relative_square(us, SQ_F1);
436 bool illegal = false;
438 // Check if any of the squares between king and rook
439 // is occupied or under attack.
440 for (s = Min(from, g1); s <= Max(from, g1); s++)
441 if ( (s != from && s != to && !pos.square_is_empty(s))
442 || pos.square_is_attacked(s, them))
445 // Check if any of the squares between king and rook
447 for (s = Min(to, f1); s <= Max(to, f1); s++)
448 if (s != from && s != to && !pos.square_is_empty(s))
454 if (move_is_long_castle(m))
456 // The piece must be a king and side to move must still have
457 // the right to castle kingside.
458 if ( type_of_piece(pc) != KING
459 ||!pos.can_castle_queenside(us))
462 assert(from == pos.king_square(us));
463 assert(to == pos.initial_qr_square(us));
464 assert(pos.piece_on(to) == rook_of_color(us));
466 Square c1 = relative_square(us, SQ_C1);
467 Square d1 = relative_square(us, SQ_D1);
469 bool illegal = false;
471 for (s = Min(from, c1); s <= Max(from, c1); s++)
472 if( (s != from && s != to && !pos.square_is_empty(s))
473 || pos.square_is_attacked(s, them))
476 for (s = Min(to, d1); s <= Max(to, d1); s++)
477 if(s != from && s != to && !pos.square_is_empty(s))
480 if ( square_file(to) == FILE_B
481 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
482 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
490 // The destination square cannot be occupied by a friendly piece
491 if (pos.color_of_piece_on(to) == us)
494 // Proceed according to the type of the moving piece.
495 if (type_of_piece(pc) == PAWN)
497 // If the destination square is on the 8/1th rank, the move must
499 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
500 ||(square_rank(to) == RANK_1 && us != WHITE))
501 && !move_promotion(m))
504 // Proceed according to the square delta between the source and
505 // destionation squares.
512 // Capture. The destination square must be occupied by an enemy
513 // piece (en passant captures was handled earlier).
514 if (pos.color_of_piece_on(to) != them)
520 // Pawn push. The destination square must be empty.
521 if (!pos.square_is_empty(to))
526 // Double white pawn push. The destination square must be on the fourth
527 // rank, and both the destination square and the square between the
528 // source and destination squares must be empty.
529 if ( square_rank(to) != RANK_4
530 || !pos.square_is_empty(to)
531 || !pos.square_is_empty(from + DELTA_N))
536 // Double black pawn push. The destination square must be on the fifth
537 // rank, and both the destination square and the square between the
538 // source and destination squares must be empty.
539 if ( square_rank(to) != RANK_5
540 || !pos.square_is_empty(to)
541 || !pos.square_is_empty(from + DELTA_S))
548 // The move is pseudo-legal, check if it is also legal
549 return pos.pl_move_is_legal(m, pinned);
552 // Luckly we can handle all the other pieces in one go
553 return ( pos.piece_attacks_square(from, to)
554 && pos.pl_move_is_legal(m, pinned)
555 && !move_promotion(m));
561 template<PieceType Piece>
562 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
567 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
569 from = pos.piece_list(us, Piece, i);
570 b = pos.piece_attacks<Piece>(from) & target;
577 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
580 Square from = pos.king_square(us);
582 b = pos.piece_attacks<KING>(from) & target;
587 template<PieceType Piece>
588 MoveStack* generate_piece_blocking_evasions(const Position& pos, MoveStack* mlist, Color us,
589 Bitboard pinned, Bitboard blockSquares) {
593 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
595 from = pos.piece_list(us, Piece, i);
596 if (pinned && bit_is_set(pinned, from))
599 b = pos.piece_attacks<Piece>(from) & blockSquares;
605 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
606 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
608 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
611 Bitboard pawns = pos.pawns(Us);
612 Bitboard enemyPieces = pos.pieces_of_color(Them);
614 // Captures in the a1-h8 (a8-h1 for black) direction
615 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
617 // Capturing promotions
618 Bitboard b2 = b1 & TRank8BB;
621 to = pop_1st_bit(&b2);
622 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
625 // Capturing non-promotions
629 to = pop_1st_bit(&b2);
630 (*mlist++).move = make_move(to - TDELTA_NE, to);
633 // Captures in the h1-a8 (h8-a1 for black) direction
634 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
636 // Capturing promotions
640 to = pop_1st_bit(&b2);
641 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
644 // Capturing non-promotions
648 to = pop_1st_bit(&b2);
649 (*mlist++).move = make_move(to - TDELTA_NW, to);
652 // Non-capturing promotions
653 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
656 to = pop_1st_bit(&b1);
657 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
660 // En passant captures
661 if (pos.ep_square() != SQ_NONE)
663 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
664 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
666 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
667 assert(b1 != EmptyBoardBB);
671 to = pop_1st_bit(&b1);
672 (*mlist++).move = make_ep_move(to, pos.ep_square());
678 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
679 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
681 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
683 Bitboard pawns = pos.pawns(Us);
684 Bitboard enemyPieces = pos.pieces_of_color(Them);
685 Bitboard emptySquares = pos.empty_squares();
689 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
690 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
693 to = pop_1st_bit(&b1);
694 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
695 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
696 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
699 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
700 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
703 to = pop_1st_bit(&b1);
704 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
705 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
706 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
709 // Single pawn pushes
710 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
714 to = pop_1st_bit(&b2);
715 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
716 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
717 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
722 to = pop_1st_bit(&b2);
723 (*mlist++).move = make_move(to - TDELTA_N, to);
726 // Double pawn pushes
727 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
730 to = pop_1st_bit(&b2);
731 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
737 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
738 MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
740 // Find all friendly pawns not on the enemy king's file
742 Bitboard empty = pos.empty_squares();
744 if (dc != EmptyBoardBB)
746 // Pawn moves which gives discovered check. This is possible only if the
747 // pawn is not on the same file as the enemy king, because we don't
748 // generate captures.
749 b1 = pos.pawns(Us) & ~file_bb(ksq);
751 // Discovered checks, single pawn pushes, no promotions
752 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
755 Square to = pop_1st_bit(&b3);
756 (*mlist++).move = make_move(to - TDELTA_N, to);
759 // Discovered checks, double pawn pushes
760 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
763 Square to = pop_1st_bit(&b3);
764 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
768 // Direct checks. These are possible only for pawns on neighboring files
769 // of the enemy king.
770 b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
772 // Direct checks, single pawn pushes
773 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
774 b3 = b2 & pos.pawn_attacks(Them, ksq);
777 Square to = pop_1st_bit(&b3);
778 (*mlist++).move = make_move(to - TDELTA_N, to);
781 // Direct checks, double pawn pushes
782 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
784 & pos.pawn_attacks(Them, ksq);
787 Square to = pop_1st_bit(&b3);
788 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
793 template<PieceType Piece>
794 MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
795 Bitboard dc, Square ksq) {
797 Bitboard target = pos.pieces_of_color_and_type(us, Piece);
800 Bitboard b = target & dc;
803 Square from = pop_1st_bit(&b);
804 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
806 bb &= ~QueenPseudoAttacks[ksq];
813 if (Piece == KING || !b)
816 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
819 Square from = pop_1st_bit(&b);
820 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
826 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
827 MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
828 Bitboard blockSquares, MoveStack* mlist) {
831 // Find non-pinned pawns
832 Bitboard b1 = pos.pawns(Us) & ~pinned;
834 // Single pawn pushes. We don't have to AND with empty squares here,
835 // because the blocking squares will always be empty.
836 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
839 to = pop_1st_bit(&b2);
841 assert(pos.piece_on(to) == EMPTY);
843 if (square_rank(to) == TRANK_8)
845 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
846 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
847 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
848 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
850 (*mlist++).move = make_move(to - TDELTA_N, to);
853 // Double pawn pushes
854 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
855 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;
858 to = pop_1st_bit(&b2);
860 assert(pos.piece_on(to) == EMPTY);
861 assert(Us != WHITE || square_rank(to) == RANK_4);
862 assert(Us != BLACK || square_rank(to) == RANK_5);
864 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
869 template<CastlingSide Side>
870 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
872 Color us = pos.side_to_move();
874 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
875 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
877 Color them = opposite_color(us);
878 Square ksq = pos.king_square(us);
880 assert(pos.piece_on(ksq) == king_of_color(us));
882 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
883 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
884 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
886 bool illegal = false;
888 assert(pos.piece_on(rsq) == rook_of_color(us));
890 // It is a bit complicated to correctly handle Chess960
891 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
892 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
893 || pos.square_is_attacked(s, them))
896 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
897 if (s != ksq && s != rsq && pos.square_is_occupied(s))
900 if ( Side == QUEEN_SIDE
901 && square_file(rsq) == FILE_B
902 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
903 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
907 (*mlist++).move = make_castle_move(ksq, rsq);
912 bool castling_is_check(const Position& pos, CastlingSide side) {
914 // After castling opponent king is attacked by the castled rook?
915 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
916 Color us = pos.side_to_move();
917 Square ksq = pos.king_square(us);
918 Bitboard occ = pos.occupied_squares();
920 clear_bit(&occ, ksq); // Remove our king from the board
921 Square rsq = make_square(rookFile, square_rank(ksq));
922 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));