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
44 bool castling_is_check(const Position&, CastlingSide);
47 template<CastlingSide Side>
48 MoveStack* generate_castle_moves(const Position&, MoveStack*);
50 // Template generate_pawn_captures() with specializations
51 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
52 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist);
55 inline MoveStack* generate_pawn_captures(const Position& p, MoveStack* m) {
56 return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
59 inline MoveStack* generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
60 return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
63 // Template generate_pawn_noncaptures() with specializations
64 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
65 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
68 inline MoveStack* generate_pawn_noncaptures(const Position& p, MoveStack* m) {
69 return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
72 inline MoveStack* generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
73 return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
76 // Template generate_pawn_blocking_evasions() with specializations
77 template<Color Us, Rank, Bitboard, SquareDelta>
78 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
79 Bitboard blockSquares, MoveStack* mlist);
81 inline MoveStack* generate_pawn_blocking_evasions(const Position& p, MoveStack* m, Bitboard np, Bitboard bs) {
82 return do_generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, np, bs, m);
85 inline MoveStack* generate_pawn_blocking_evasions<BLACK>(const Position& p, MoveStack* m, Bitboard np, Bitboard bs) {
86 return do_generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, np, bs, m);
89 // Template generate_pawn_checks() with specializations
90 template<Color, Color, Bitboard, Bitboard, SquareDelta>
91 MoveStack* do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
94 inline MoveStack* generate_pawn_checks(const Position& p, MoveStack* m, Bitboard dc, Square ksq) {
95 return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
98 inline MoveStack* generate_pawn_checks<BLACK>(const Position& p, MoveStack* m, Bitboard dc, Square ksq) {
99 return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
102 // non-pawn templates
104 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
106 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
109 MoveStack* generate_piece_checks(const Position&, MoveStack*, Color us, Bitboard, Square);
112 MoveStack* generate_piece_blocking_evasions(const Position&, MoveStack*, Bitboard, Bitboard);
121 /// generate_captures generates() all pseudo-legal captures and queen
122 /// promotions. The return value is the number of moves generated.
124 int generate_captures(const Position& pos, MoveStack* mlist) {
127 assert(!pos.is_check());
129 Color us = pos.side_to_move();
130 Bitboard target = pos.pieces_of_color(opposite_color(us));
131 MoveStack* mlist_start = mlist;
133 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
134 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
135 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
136 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
139 mlist = generate_pawn_captures<WHITE>(pos, mlist);
141 mlist = generate_pawn_captures<BLACK>(pos, mlist);
143 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
144 return int(mlist - mlist_start);
148 /// generate_noncaptures() generates all pseudo-legal non-captures and
149 /// underpromotions. The return value is the number of moves generated.
151 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
154 assert(!pos.is_check());
156 Color us = pos.side_to_move();
157 Bitboard target = pos.empty_squares();
158 MoveStack* mlist_start = mlist;
161 mlist = generate_pawn_noncaptures<WHITE>(pos, mlist);
163 mlist = generate_pawn_noncaptures<BLACK>(pos, mlist);
165 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
166 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
167 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
168 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
169 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
170 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
171 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
172 return int(mlist - mlist_start);
176 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
177 /// checks. It returns the number of generated moves.
179 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
182 assert(!pos.is_check());
184 Color us = pos.side_to_move();
185 Square ksq = pos.king_square(opposite_color(us));
186 MoveStack* mlist_start = mlist;
188 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
192 mlist = generate_pawn_checks<WHITE>(pos, mlist, dc, ksq);
194 mlist = generate_pawn_checks<BLACK>(pos, mlist, dc, ksq);
197 mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
198 mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
199 mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
200 mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
201 mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
203 // Castling moves that give check. Very rare but nice to have!
204 if ( pos.can_castle_queenside(us)
205 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
206 && castling_is_check(pos, QUEEN_SIDE))
207 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
209 if ( pos.can_castle_kingside(us)
210 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
211 && castling_is_check(pos, KING_SIDE))
212 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
214 return int(mlist - mlist_start);
218 /// generate_evasions() generates all check evasions when the side to move is
219 /// in check. Unlike the other move generation functions, this one generates
220 /// only legal moves. It returns the number of generated moves. This
221 /// function is very ugly, and needs cleaning up some time later. FIXME
223 int generate_evasions(const Position& pos, MoveStack* mlist) {
226 assert(pos.is_check());
229 Color us = pos.side_to_move();
230 Color them = opposite_color(us);
231 Square ksq = pos.king_square(us);
232 MoveStack* mlist_start = mlist;
234 assert(pos.piece_on(ksq) == king_of_color(us));
236 // The bitboard of occupied pieces without our king
237 Bitboard b2 = pos.occupied_squares();
240 // Find squares attacked by slider checkers, we will
241 // remove them from king evasions set so to avoid a couple
242 // of cycles in the slow king evasions legality check loop.
243 Bitboard checkers = pos.checkers();
244 Bitboard checkersAttacks = EmptyBoardBB;
245 Bitboard b = checkers & (pos.queens() | pos.bishops());
248 from = pop_1st_bit(&b);
249 checkersAttacks |= bishop_attacks_bb(from, b2);
252 b = checkers & (pos.queens() | pos.rooks());
255 from = pop_1st_bit(&b);
256 checkersAttacks |= rook_attacks_bb(from, b2);
259 // Generate evasions for king
260 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
263 to = pop_1st_bit(&b1);
265 // Make sure 'to' is not attacked by the other side. This is a bit ugly,
266 // because we can't use Position::square_is_attacked. Instead we use
267 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
268 // b2 (the occupied squares with the king removed) in order to test whether
269 // the king will remain in check on the destination square.
270 if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
271 || (pos.pawn_attacks(us, to) & pos.pawns(them))
272 || (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
273 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
274 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
275 (*mlist++).move = make_move(ksq, to);
278 // Generate evasions for other pieces only if not double check. We use a
279 // simple bit twiddling hack here rather than calling count_1s in order to
280 // save some time (we know that pos.checkers() has at most two nonzero bits).
281 if (!(checkers & (checkers - 1))) // Only one bit set?
283 Square checksq = first_1(checkers);
285 assert(pos.color_of_piece_on(checksq) == them);
287 // Find pinned pieces
288 Bitboard not_pinned = ~pos.pinned_pieces(us);
290 // Generate captures of the checking piece
293 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
296 from = pop_1st_bit(&b1);
297 if (relative_rank(us, checksq) == RANK_8)
299 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
300 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
301 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
302 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
304 (*mlist++).move = make_move(from, checksq);
308 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
309 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
310 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
314 from = pop_1st_bit(&b1);
315 (*mlist++).move = make_move(from, checksq);
318 // Blocking check evasions are possible only if the checking piece is
320 if (checkers & pos.sliders())
322 Bitboard blockSquares = squares_between(checksq, ksq);
324 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
326 // Pawn moves. Because a blocking evasion can never be a capture, we
327 // only generate pawn pushes.
329 mlist = generate_pawn_blocking_evasions<WHITE>(pos, mlist, not_pinned, blockSquares);
331 mlist = generate_pawn_blocking_evasions<BLACK>(pos, mlist, not_pinned, blockSquares);
334 b1 = pos.knights(us) & not_pinned;
336 mlist = generate_piece_blocking_evasions<KNIGHT>(pos, mlist, b1, blockSquares);
338 b1 = pos.bishops(us) & not_pinned;
340 mlist = generate_piece_blocking_evasions<BISHOP>(pos, mlist, b1, blockSquares);
342 b1 = pos.rooks(us) & not_pinned;
344 mlist = generate_piece_blocking_evasions<ROOK>(pos, mlist, b1, blockSquares);
346 b1 = pos.queens(us) & not_pinned;
348 mlist = generate_piece_blocking_evasions<QUEEN>(pos, mlist, b1, blockSquares);
351 // Finally, the ugly special case of en passant captures. An en passant
352 // capture can only be a check evasion if the check is not a discovered
353 // check. If pos.ep_square() is set, the last move made must have been
354 // a double pawn push. If, furthermore, the checking piece is a pawn,
355 // an en passant check evasion may be possible.
356 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
358 to = pos.ep_square();
359 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
361 assert(b1 != EmptyBoardBB);
366 from = pop_1st_bit(&b1);
368 // Before generating the move, we have to make sure it is legal.
369 // This is somewhat tricky, because the two disappearing pawns may
370 // cause new "discovered checks". We test this by removing the
371 // two relevant bits from the occupied squares bitboard, and using
372 // the low-level bitboard functions for bishop and rook attacks.
373 b2 = pos.occupied_squares();
374 clear_bit(&b2, from);
375 clear_bit(&b2, checksq);
376 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
377 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
379 (*mlist++).move = make_ep_move(from, to);
383 return int(mlist - mlist_start);
387 /// generate_legal_moves() computes a complete list of legal moves in the
388 /// current position. This function is not very fast, and should be used
389 /// only in situations where performance is unimportant. It wouldn't be
390 /// very hard to write an efficient legal move generator, but for the moment
391 /// we don't need it.
393 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
398 return generate_evasions(pos, mlist);
400 // Generate pseudo-legal moves
401 int n = generate_captures(pos, mlist);
402 n += generate_noncaptures(pos, mlist + n);
404 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
406 // Remove illegal moves from the list
407 for (int i = 0; i < n; i++)
408 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
409 mlist[i--].move = mlist[--n].move;
415 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
416 /// move and a pinned pieces bitboard as input, and tests whether
417 /// the move is legal. If the move is legal, the move itself is
418 /// returned. If not, the function returns false. This function must
419 /// only be used when the side to move is not in check.
421 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
424 assert(!pos.is_check());
425 assert(move_is_ok(m));
426 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
428 Color us = pos.side_to_move();
429 Color them = opposite_color(us);
430 Square from = move_from(m);
431 Piece pc = pos.piece_on(from);
433 // If the from square is not occupied by a piece belonging to the side to
434 // move, the move is obviously not legal.
435 if (color_of_piece(pc) != us)
438 Square to = move_to(m);
443 // The piece must be a pawn and destination square must be the
444 // en passant square.
445 if ( type_of_piece(pc) != PAWN
446 || to != pos.ep_square())
449 assert(pos.square_is_empty(to));
450 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
452 // The move is pseudo-legal, check if it is also legal
453 return pos.pl_move_is_legal(m, pinned);
457 if (move_is_short_castle(m))
459 // The piece must be a king and side to move must still have
460 // the right to castle kingside.
461 if ( type_of_piece(pc) != KING
462 ||!pos.can_castle_kingside(us))
465 assert(from == pos.king_square(us));
466 assert(to == pos.initial_kr_square(us));
467 assert(pos.piece_on(to) == rook_of_color(us));
469 Square g1 = relative_square(us, SQ_G1);
470 Square f1 = relative_square(us, SQ_F1);
472 bool illegal = false;
474 // Check if any of the squares between king and rook
475 // is occupied or under attack.
476 for (s = Min(from, g1); s <= Max(from, g1); s++)
477 if ( (s != from && s != to && !pos.square_is_empty(s))
478 || pos.square_is_attacked(s, them))
481 // Check if any of the squares between king and rook
483 for (s = Min(to, f1); s <= Max(to, f1); s++)
484 if (s != from && s != to && !pos.square_is_empty(s))
490 if (move_is_long_castle(m))
492 // The piece must be a king and side to move must still have
493 // the right to castle kingside.
494 if ( type_of_piece(pc) != KING
495 ||!pos.can_castle_queenside(us))
498 assert(from == pos.king_square(us));
499 assert(to == pos.initial_qr_square(us));
500 assert(pos.piece_on(to) == rook_of_color(us));
502 Square c1 = relative_square(us, SQ_C1);
503 Square d1 = relative_square(us, SQ_D1);
505 bool illegal = false;
507 for (s = Min(from, c1); s <= Max(from, c1); s++)
508 if( (s != from && s != to && !pos.square_is_empty(s))
509 || pos.square_is_attacked(s, them))
512 for (s = Min(to, d1); s <= Max(to, d1); s++)
513 if(s != from && s != to && !pos.square_is_empty(s))
516 if ( square_file(to) == FILE_B
517 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
518 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
526 // The destination square cannot be occupied by a friendly piece
527 if (pos.color_of_piece_on(to) == us)
530 // Proceed according to the type of the moving piece.
531 if (type_of_piece(pc) == PAWN)
533 // If the destination square is on the 8/1th rank, the move must
535 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
536 ||(square_rank(to) == RANK_1 && us != WHITE))
537 && !move_promotion(m))
540 // Proceed according to the square delta between the source and
541 // destionation squares.
548 // Capture. The destination square must be occupied by an enemy
549 // piece (en passant captures was handled earlier).
550 if (pos.color_of_piece_on(to) != them)
556 // Pawn push. The destination square must be empty.
557 if (!pos.square_is_empty(to))
562 // Double white pawn push. The destination square must be on the fourth
563 // rank, and both the destination square and the square between the
564 // source and destination squares must be empty.
565 if ( square_rank(to) != RANK_4
566 || !pos.square_is_empty(to)
567 || !pos.square_is_empty(from + DELTA_N))
572 // Double black pawn push. The destination square must be on the fifth
573 // rank, and both the destination square and the square between the
574 // source and destination squares must be empty.
575 if ( square_rank(to) != RANK_5
576 || !pos.square_is_empty(to)
577 || !pos.square_is_empty(from + DELTA_S))
584 // The move is pseudo-legal, check if it is also legal
585 return pos.pl_move_is_legal(m, pinned);
588 // Luckly we can handle all the other pieces in one go
589 return ( pos.piece_attacks_square(from, to)
590 && pos.pl_move_is_legal(m, pinned)
591 && !move_promotion(m));
597 template<PieceType Piece>
598 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
603 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
605 from = pos.piece_list(us, Piece, i);
606 b = pos.piece_attacks<Piece>(from) & target;
613 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
616 Square from = pos.king_square(us);
618 b = pos.piece_attacks<KING>(from) & target;
623 template<PieceType Piece>
624 MoveStack* generate_piece_blocking_evasions(const Position& pos, MoveStack* mlist,
625 Bitboard b, Bitboard blockSquares) {
628 Square from = pop_1st_bit(&b);
629 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
636 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
637 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
639 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
642 Bitboard pawns = pos.pawns(Us);
643 Bitboard enemyPieces = pos.pieces_of_color(Them);
645 // Captures in the a1-h8 (a8-h1 for black) direction
646 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
648 // Capturing promotions
649 Bitboard b2 = b1 & TRank8BB;
652 to = pop_1st_bit(&b2);
653 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
656 // Capturing non-promotions
660 to = pop_1st_bit(&b2);
661 (*mlist++).move = make_move(to - TDELTA_NE, to);
664 // Captures in the h1-a8 (h8-a1 for black) direction
665 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
667 // Capturing promotions
671 to = pop_1st_bit(&b2);
672 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
675 // Capturing non-promotions
679 to = pop_1st_bit(&b2);
680 (*mlist++).move = make_move(to - TDELTA_NW, to);
683 // Non-capturing promotions
684 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
687 to = pop_1st_bit(&b1);
688 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
691 // En passant captures
692 if (pos.ep_square() != SQ_NONE)
694 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
695 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
697 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
698 assert(b1 != EmptyBoardBB);
702 to = pop_1st_bit(&b1);
703 (*mlist++).move = make_ep_move(to, pos.ep_square());
709 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
710 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
712 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
714 Bitboard pawns = pos.pawns(Us);
715 Bitboard enemyPieces = pos.pieces_of_color(Them);
716 Bitboard emptySquares = pos.empty_squares();
720 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
721 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
724 to = pop_1st_bit(&b1);
725 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
726 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
727 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
730 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
731 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
734 to = pop_1st_bit(&b1);
735 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
736 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
737 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
740 // Single pawn pushes
741 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
745 to = pop_1st_bit(&b2);
746 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
747 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
748 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
753 to = pop_1st_bit(&b2);
754 (*mlist++).move = make_move(to - TDELTA_N, to);
757 // Double pawn pushes
758 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
761 to = pop_1st_bit(&b2);
762 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
768 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
769 MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
771 // Find all friendly pawns not on the enemy king's file
773 Bitboard empty = pos.empty_squares();
775 if (dc != EmptyBoardBB)
777 // Pawn moves which gives discovered check. This is possible only if the
778 // pawn is not on the same file as the enemy king, because we don't
779 // generate captures.
780 b1 = pos.pawns(Us) & ~file_bb(ksq);
782 // Discovered checks, single pawn pushes, no promotions
783 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
786 Square to = pop_1st_bit(&b3);
787 (*mlist++).move = make_move(to - TDELTA_N, to);
790 // Discovered checks, double pawn pushes
791 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
794 Square to = pop_1st_bit(&b3);
795 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
799 // Direct checks. These are possible only for pawns on neighboring files
800 // of the enemy king.
801 b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
803 // Direct checks, single pawn pushes
804 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
805 b3 = b2 & pos.pawn_attacks(Them, ksq);
808 Square to = pop_1st_bit(&b3);
809 (*mlist++).move = make_move(to - TDELTA_N, to);
812 // Direct checks, double pawn pushes
813 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
815 & pos.pawn_attacks(Them, ksq);
818 Square to = pop_1st_bit(&b3);
819 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
824 template<PieceType Piece>
825 MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
826 Bitboard dc, Square ksq) {
828 Bitboard target = pos.pieces_of_color_and_type(us, Piece);
831 Bitboard b = target & dc;
834 Square from = pop_1st_bit(&b);
835 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
837 bb &= ~QueenPseudoAttacks[ksq];
844 if (Piece == KING || !b)
847 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
850 Square from = pop_1st_bit(&b);
851 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
857 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
858 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
859 Bitboard blockSquares, MoveStack* mlist) {
862 // Find non-pinned pawns
863 Bitboard b1 = pos.pawns(Us) & not_pinned;
865 // Single pawn pushes. We don't have to AND with empty squares here,
866 // because the blocking squares will always be empty.
867 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
870 to = pop_1st_bit(&b2);
872 assert(pos.piece_on(to) == EMPTY);
874 if (square_rank(to) == TRANK_8)
876 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
877 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
878 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
879 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
881 (*mlist++).move = make_move(to - TDELTA_N, to);
884 // Double pawn pushes
885 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
886 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
889 to = pop_1st_bit(&b2);
891 assert(pos.piece_on(to) == EMPTY);
892 assert(Us != WHITE || square_rank(to) == RANK_4);
893 assert(Us != BLACK || square_rank(to) == RANK_5);
895 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
900 template<CastlingSide Side>
901 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
903 Color us = pos.side_to_move();
905 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
906 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
908 Color them = opposite_color(us);
909 Square ksq = pos.king_square(us);
911 assert(pos.piece_on(ksq) == king_of_color(us));
913 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
914 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
915 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
917 bool illegal = false;
919 assert(pos.piece_on(rsq) == rook_of_color(us));
921 // It is a bit complicated to correctly handle Chess960
922 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
923 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
924 || pos.square_is_attacked(s, them))
927 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
928 if (s != ksq && s != rsq && pos.square_is_occupied(s))
931 if ( Side == QUEEN_SIDE
932 && square_file(rsq) == FILE_B
933 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
934 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
938 (*mlist++).move = make_castle_move(ksq, rsq);
943 bool castling_is_check(const Position& pos, CastlingSide side) {
945 // After castling opponent king is attacked by the castled rook?
946 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
947 Color us = pos.side_to_move();
948 Square ksq = pos.king_square(us);
949 Bitboard occ = pos.occupied_squares();
951 clear_bit(&occ, ksq); // Remove our king from the board
952 Square rsq = make_square(rookFile, square_rank(ksq));
953 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));