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 MoveStack* generate_castle_moves(const Position&, MoveStack*, unsigned = (KING_SIDE | QUEEN_SIDE));
45 bool castling_is_check(const Position& pos, CastlingSide side);
47 // Template generate_pawn_captures() with specializations
48 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
49 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist);
52 inline MoveStack* generate_pawn_captures(const Position& p, MoveStack* m) {
53 return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
56 inline MoveStack* generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
57 return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
60 // Template generate_pawn_noncaptures() with specializations
61 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
62 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
65 inline MoveStack* generate_pawn_noncaptures(const Position& p, MoveStack* m) {
66 return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
69 inline MoveStack* generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
70 return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
73 // Template generate_pawn_blocking_evasions() with specializations
74 template<Color Us, Rank, Bitboard, SquareDelta>
75 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
76 Bitboard blockSquares, MoveStack* mlist);
78 inline MoveStack* generate_pawn_blocking_evasions(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
79 return do_generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, np, bs, m);
82 inline MoveStack* generate_pawn_blocking_evasions<BLACK>(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
83 return do_generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, np, bs, m);
86 // Template generate_pawn_checks() with specializations
87 template<Color, Color, Bitboard, Bitboard, SquareDelta>
88 MoveStack* do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
91 inline MoveStack* generate_pawn_checks(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
92 return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
95 inline MoveStack* generate_pawn_checks<BLACK>(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
96 return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
101 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
103 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
106 MoveStack* generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*);
107 MoveStack* generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
110 MoveStack* generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
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);
137 mlist = generate_pawn_captures<WHITE>(pos, mlist);
139 mlist = generate_pawn_captures<BLACK>(pos, mlist);
141 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
142 return int(mlist - mlist_start);
146 /// generate_noncaptures() generates all pseudo-legal non-captures and
147 /// underpromotions. The return value is the number of moves generated.
149 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
152 assert(!pos.is_check());
154 Color us = pos.side_to_move();
155 Bitboard target = pos.empty_squares();
156 MoveStack* mlist_start = mlist;
159 mlist = generate_pawn_noncaptures<WHITE>(pos, mlist);
161 mlist = generate_pawn_noncaptures<BLACK>(pos, mlist);
163 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
164 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
165 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
166 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
167 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
168 mlist = generate_castle_moves(pos, mlist);
169 return int(mlist - mlist_start);
173 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
174 /// checks. It returns the number of generated moves.
176 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
179 assert(!pos.is_check());
181 Color us = pos.side_to_move();
182 Square ksq = pos.king_square(opposite_color(us));
183 MoveStack* mlist_start = mlist;
185 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
187 dc = pos.discovered_check_candidates(us);
191 mlist = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
193 mlist = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
196 Bitboard b = pos.knights(us);
198 mlist = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist);
202 mlist = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist);
206 mlist = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist);
210 mlist = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist);
212 // Hopefully we always have a king ;-)
213 mlist = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist);
215 // Castling moves that give check. Very rare but nice to have!
216 if ( pos.can_castle_queenside(us)
217 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
218 && castling_is_check(pos, QUEEN_SIDE))
219 mlist = generate_castle_moves(pos, mlist, QUEEN_SIDE);
221 if ( pos.can_castle_kingside(us)
222 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
223 && castling_is_check(pos, KING_SIDE))
224 mlist = generate_castle_moves(pos, mlist, KING_SIDE);
226 return int(mlist - mlist_start);
230 /// generate_evasions() generates all check evasions when the side to move is
231 /// in check. Unlike the other move generation functions, this one generates
232 /// only legal moves. It returns the number of generated moves. This
233 /// function is very ugly, and needs cleaning up some time later. FIXME
235 int generate_evasions(const Position& pos, MoveStack* mlist) {
238 assert(pos.is_check());
241 Color us = pos.side_to_move();
242 Color them = opposite_color(us);
243 Square ksq = pos.king_square(us);
244 MoveStack* mlist_start = mlist;
246 assert(pos.piece_on(ksq) == king_of_color(us));
248 // Generate evasions for king
249 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
250 Bitboard b2 = pos.occupied_squares();
255 to = pop_1st_bit(&b1);
257 // Make sure 'to' is not attacked by the other side. This is a bit ugly,
258 // because we can't use Position::square_is_attacked. Instead we use
259 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
260 // b2 (the occupied squares with the king removed) in order to test whether
261 // the king will remain in check on the destination square.
262 if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
263 || (pos.pawn_attacks(us, to) & pos.pawns(them))
264 || (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
265 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
266 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
268 (*mlist++).move = make_move(ksq, to);
271 // Generate evasions for other pieces only if not double check. We use a
272 // simple bit twiddling hack here rather than calling count_1s in order to
273 // save some time (we know that pos.checkers() has at most two nonzero bits).
274 Bitboard checkers = pos.checkers();
276 if (!(checkers & (checkers - 1))) // Only one bit set?
278 Square checksq = first_1(checkers);
280 assert(pos.color_of_piece_on(checksq) == them);
282 // Find pinned pieces
283 Bitboard not_pinned = ~pos.pinned_pieces(us);
285 // Generate captures of the checking piece
288 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
291 from = pop_1st_bit(&b1);
292 if (relative_rank(us, checksq) == RANK_8)
294 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
295 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
296 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
297 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
299 (*mlist++).move = make_move(from, checksq);
303 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
304 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
305 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
309 from = pop_1st_bit(&b1);
310 (*mlist++).move = make_move(from, checksq);
313 // Blocking check evasions are possible only if the checking piece is
315 if (checkers & pos.sliders())
317 Bitboard blockSquares = squares_between(checksq, ksq);
319 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
321 // Pawn moves. Because a blocking evasion can never be a capture, we
322 // only generate pawn pushes.
324 mlist = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist);
326 mlist = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist);
329 b1 = pos.knights(us) & not_pinned;
331 mlist = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist);
333 b1 = pos.bishops(us) & not_pinned;
335 mlist = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist);
337 b1 = pos.rooks(us) & not_pinned;
339 mlist = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist);
341 b1 = pos.queens(us) & not_pinned;
343 mlist = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist);
346 // Finally, the ugly special case of en passant captures. An en passant
347 // capture can only be a check evasion if the check is not a discovered
348 // check. If pos.ep_square() is set, the last move made must have been
349 // a double pawn push. If, furthermore, the checking piece is a pawn,
350 // an en passant check evasion may be possible.
351 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
353 to = pos.ep_square();
354 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
356 assert(b1 != EmptyBoardBB);
361 from = pop_1st_bit(&b1);
363 // Before generating the move, we have to make sure it is legal.
364 // This is somewhat tricky, because the two disappearing pawns may
365 // cause new "discovered checks". We test this by removing the
366 // two relevant bits from the occupied squares bitboard, and using
367 // the low-level bitboard functions for bishop and rook attacks.
368 b2 = pos.occupied_squares();
369 clear_bit(&b2, from);
370 clear_bit(&b2, checksq);
371 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
372 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
374 (*mlist++).move = make_ep_move(from, to);
378 return int(mlist - mlist_start);
382 /// generate_legal_moves() computes a complete list of legal moves in the
383 /// current position. This function is not very fast, and should be used
384 /// only in situations where performance is unimportant. It wouldn't be
385 /// very hard to write an efficient legal move generator, but for the moment
386 /// we don't need it.
388 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
393 return generate_evasions(pos, mlist);
395 // Generate pseudo-legal moves
396 int n = generate_captures(pos, mlist);
397 n += generate_noncaptures(pos, mlist + n);
399 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
401 // Remove illegal moves from the list
402 for (int i = 0; i < n; i++)
403 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
404 mlist[i--].move = mlist[--n].move;
410 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
411 /// move and a pinned pieces bitboard as input, and tests whether
412 /// the move is legal. If the move is legal, the move itself is
413 /// returned. If not, the function returns false. This function must
414 /// only be used when the side to move is not in check.
416 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
419 assert(!pos.is_check());
420 assert(move_is_ok(m));
421 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
423 Color us = pos.side_to_move();
424 Color them = opposite_color(us);
425 Square from = move_from(m);
426 Piece pc = pos.piece_on(from);
428 // If the from square is not occupied by a piece belonging to the side to
429 // move, the move is obviously not legal.
430 if (color_of_piece(pc) != us)
433 Square to = move_to(m);
438 // The piece must be a pawn and destination square must be the
439 // en passant square.
440 if ( type_of_piece(pc) != PAWN
441 || to != pos.ep_square())
444 assert(pos.square_is_empty(to));
445 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
447 // The move is pseudo-legal, check if it is also legal
448 return pos.pl_move_is_legal(m, pinned);
452 if (move_is_short_castle(m))
454 // The piece must be a king and side to move must still have
455 // the right to castle kingside.
456 if ( type_of_piece(pc) != KING
457 ||!pos.can_castle_kingside(us))
460 assert(from == pos.king_square(us));
461 assert(to == pos.initial_kr_square(us));
462 assert(pos.piece_on(to) == rook_of_color(us));
464 Square g1 = relative_square(us, SQ_G1);
465 Square f1 = relative_square(us, SQ_F1);
467 bool illegal = false;
469 // Check if any of the squares between king and rook
470 // is occupied or under attack.
471 for (s = Min(from, g1); s <= Max(from, g1); s++)
472 if ( (s != from && s != to && !pos.square_is_empty(s))
473 || pos.square_is_attacked(s, them))
476 // Check if any of the squares between king and rook
478 for (s = Min(to, f1); s <= Max(to, f1); s++)
479 if (s != from && s != to && !pos.square_is_empty(s))
485 if (move_is_long_castle(m))
487 // The piece must be a king and side to move must still have
488 // the right to castle kingside.
489 if ( type_of_piece(pc) != KING
490 ||!pos.can_castle_queenside(us))
493 assert(from == pos.king_square(us));
494 assert(to == pos.initial_qr_square(us));
495 assert(pos.piece_on(to) == rook_of_color(us));
497 Square c1 = relative_square(us, SQ_C1);
498 Square d1 = relative_square(us, SQ_D1);
500 bool illegal = false;
502 for (s = Min(from, c1); s <= Max(from, c1); s++)
503 if( (s != from && s != to && !pos.square_is_empty(s))
504 || pos.square_is_attacked(s, them))
507 for (s = Min(to, d1); s <= Max(to, d1); s++)
508 if(s != from && s != to && !pos.square_is_empty(s))
511 if ( square_file(to) == FILE_B
512 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
513 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
521 // The destination square cannot be occupied by a friendly piece
522 if (pos.color_of_piece_on(to) == us)
525 // Proceed according to the type of the moving piece.
526 if (type_of_piece(pc) == PAWN)
528 // If the destination square is on the 8/1th rank, the move must
530 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
531 ||(square_rank(to) == RANK_1 && us != WHITE))
532 && !move_promotion(m))
535 // Proceed according to the square delta between the source and
536 // destionation squares.
543 // Capture. The destination square must be occupied by an enemy
544 // piece (en passant captures was handled earlier).
545 if (pos.color_of_piece_on(to) != them)
551 // Pawn push. The destination square must be empty.
552 if (!pos.square_is_empty(to))
557 // Double white pawn push. The destination square must be on the fourth
558 // rank, and both the destination square and the square between the
559 // source and destination squares must be empty.
560 if ( square_rank(to) != RANK_4
561 || !pos.square_is_empty(to)
562 || !pos.square_is_empty(from + DELTA_N))
567 // Double black pawn push. The destination square must be on the fifth
568 // rank, and both the destination square and the square between the
569 // source and destination squares must be empty.
570 if ( square_rank(to) != RANK_5
571 || !pos.square_is_empty(to)
572 || !pos.square_is_empty(from + DELTA_S))
579 // The move is pseudo-legal, check if it is also legal
580 return pos.pl_move_is_legal(m, pinned);
583 // Luckly we can handle all the other pieces in one go
584 return ( pos.piece_attacks_square(from, to)
585 && pos.pl_move_is_legal(m, pinned)
586 && !move_promotion(m));
592 template<PieceType Piece>
593 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
598 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
600 from = pos.piece_list(us, Piece, i);
601 b = pos.piece_attacks<Piece>(from) & target;
608 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
611 Square from = pos.king_square(us);
613 b = pos.piece_attacks<KING>(from) & target;
618 template<PieceType Piece>
619 MoveStack* generate_piece_blocking_evasions(const Position& pos, Bitboard b,
620 Bitboard blockSquares, MoveStack* mlist) {
623 Square from = pop_1st_bit(&b);
624 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
631 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
632 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
634 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
637 Bitboard pawns = pos.pawns(Us);
638 Bitboard enemyPieces = pos.pieces_of_color(Them);
640 // Captures in the a1-h8 (a8-h1 for black) direction
641 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
643 // Capturing promotions
644 Bitboard b2 = b1 & TRank8BB;
647 to = pop_1st_bit(&b2);
648 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
651 // Capturing non-promotions
655 to = pop_1st_bit(&b2);
656 (*mlist++).move = make_move(to - TDELTA_NE, to);
659 // Captures in the h1-a8 (h8-a1 for black) direction
660 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
662 // Capturing promotions
666 to = pop_1st_bit(&b2);
667 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
670 // Capturing non-promotions
674 to = pop_1st_bit(&b2);
675 (*mlist++).move = make_move(to - TDELTA_NW, to);
678 // Non-capturing promotions
679 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
682 to = pop_1st_bit(&b1);
683 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
686 // En passant captures
687 if (pos.ep_square() != SQ_NONE)
689 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
690 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
692 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
693 assert(b1 != EmptyBoardBB);
697 to = pop_1st_bit(&b1);
698 (*mlist++).move = make_ep_move(to, pos.ep_square());
704 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
705 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
707 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
709 Bitboard pawns = pos.pawns(Us);
710 Bitboard enemyPieces = pos.pieces_of_color(Them);
711 Bitboard emptySquares = pos.empty_squares();
715 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
716 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
719 to = pop_1st_bit(&b1);
720 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
721 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
722 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
725 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
726 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
729 to = pop_1st_bit(&b1);
730 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
731 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
732 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
735 // Single pawn pushes
736 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
740 to = pop_1st_bit(&b2);
741 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
742 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
743 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
748 to = pop_1st_bit(&b2);
749 (*mlist++).move = make_move(to - TDELTA_N, to);
752 // Double pawn pushes
753 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
756 to = pop_1st_bit(&b2);
757 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
763 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
764 MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
766 // Pawn moves which give discovered check. This is possible only if the
767 // pawn is not on the same file as the enemy king, because we don't
768 // generate captures.
769 Bitboard empty = pos.empty_squares();
771 // Find all friendly pawns not on the enemy king's file
772 Bitboard b1 = pos.pawns(Us) & ~file_bb(ksq), b2, b3;
774 // Discovered checks, single pawn pushes
775 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~TRank8BB & empty;
778 Square to = pop_1st_bit(&b3);
779 (*mlist++).move = make_move(to - TDELTA_N, to);
782 // Discovered checks, double pawn pushes
783 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
786 Square to = pop_1st_bit(&b3);
787 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
790 // Direct checks. These are possible only for pawns on neighboring files
793 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
795 // Direct checks, single pawn pushes
796 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
797 b3 = b2 & pos.pawn_attacks(Them, ksq);
800 Square to = pop_1st_bit(&b3);
801 (*mlist++).move = make_move(to - TDELTA_N, to);
804 // Direct checks, double pawn pushes
805 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
807 & pos.pawn_attacks(Them, ksq);
811 Square to = pop_1st_bit(&b3);
812 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
817 template<PieceType Piece>
818 MoveStack* generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
819 Square ksq, MoveStack* mlist) {
821 Bitboard b = target & dc;
824 Square from = pop_1st_bit(&b);
825 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
830 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
833 Square from = pop_1st_bit(&b);
834 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
840 MoveStack* generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
841 Square ksq, MoveStack* mlist) {
842 if (bit_is_set(dc, from))
844 Bitboard b = pos.piece_attacks<KING>(from)
845 & pos.empty_squares()
846 & ~QueenPseudoAttacks[ksq];
853 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
854 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
855 Bitboard blockSquares, MoveStack* mlist) {
858 // Find non-pinned pawns
859 Bitboard b1 = pos.pawns(Us) & not_pinned;
861 // Single pawn pushes. We don't have to AND with empty squares here,
862 // because the blocking squares will always be empty.
863 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
866 to = pop_1st_bit(&b2);
868 assert(pos.piece_on(to) == EMPTY);
870 if (square_rank(to) == TRANK_8)
872 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
873 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
874 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
875 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
877 (*mlist++).move = make_move(to - TDELTA_N, to);
880 // Double pawn pushes
881 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
882 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
885 to = pop_1st_bit(&b2);
887 assert(pos.piece_on(to) == EMPTY);
888 assert(Us != WHITE || square_rank(to) == RANK_4);
889 assert(Us != BLACK || square_rank(to) == RANK_5);
891 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
896 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, unsigned side) {
898 Color us = pos.side_to_move();
900 if (pos.can_castle_kingside(us) && (side & KING_SIDE))
902 Color them = opposite_color(us);
903 Square ksq = pos.king_square(us);
905 assert(pos.piece_on(ksq) == king_of_color(us));
907 Square rsq = pos.initial_kr_square(us);
908 Square g1 = relative_square(us, SQ_G1);
909 Square f1 = relative_square(us, SQ_F1);
911 bool illegal = false;
913 assert(pos.piece_on(rsq) == rook_of_color(us));
915 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
916 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
917 || pos.square_is_attacked(s, them))
920 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
921 if (s != ksq && s != rsq && pos.square_is_occupied(s))
925 (*mlist++).move = make_castle_move(ksq, rsq);
928 if (pos.can_castle_queenside(us) && (side & QUEEN_SIDE))
930 Color them = opposite_color(us);
931 Square ksq = pos.king_square(us);
933 assert(pos.piece_on(ksq) == king_of_color(us));
935 Square rsq = pos.initial_qr_square(us);
936 Square c1 = relative_square(us, SQ_C1);
937 Square d1 = relative_square(us, SQ_D1);
939 bool illegal = false;
941 assert(pos.piece_on(rsq) == rook_of_color(us));
943 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
944 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
945 || pos.square_is_attacked(s, them))
948 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
949 if (s != ksq && s != rsq && pos.square_is_occupied(s))
952 if ( square_file(rsq) == FILE_B
953 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
954 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
958 (*mlist++).move = make_castle_move(ksq, rsq);
963 bool castling_is_check(const Position& pos, CastlingSide side) {
965 // After castling opponent king is attacked by the castled rook?
966 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
967 Color us = pos.side_to_move();
968 Square ksq = pos.king_square(us);
969 Bitboard occ = pos.occupied_squares(), oppKingBB = EmptyBoardBB;
971 set_bit(&oppKingBB, pos.king_square(opposite_color(us)));
972 clear_bit(&occ, ksq); // Remove our king from the board
973 Square rsq = make_square(rookFile, square_rank(ksq));
974 return (rook_attacks_bb(rsq, occ) & oppKingBB);