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, Bitboard np, Bitboard bs, MoveStack* m) {
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, Bitboard np, Bitboard bs, MoveStack* m) {
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, Bitboard dc, Square ksq, MoveStack* m) {
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, Bitboard dc, Square ksq, MoveStack* m) {
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&, Bitboard, Bitboard, Square, MoveStack*);
112 MoveStack* generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
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)));
190 dc = pos.discovered_check_candidates(us);
194 mlist = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
196 mlist = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
199 Bitboard b = pos.knights(us);
201 mlist = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist);
205 mlist = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist);
209 mlist = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist);
213 mlist = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist);
215 // Hopefully we always have a king ;-)
216 mlist = generate_piece_checks<KING>(pos, pos.kings(us), dc, ksq, mlist);
218 // Castling moves that give check. Very rare but nice to have!
219 if ( pos.can_castle_queenside(us)
220 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
221 && castling_is_check(pos, QUEEN_SIDE))
222 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
224 if ( pos.can_castle_kingside(us)
225 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
226 && castling_is_check(pos, KING_SIDE))
227 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
229 return int(mlist - mlist_start);
233 /// generate_evasions() generates all check evasions when the side to move is
234 /// in check. Unlike the other move generation functions, this one generates
235 /// only legal moves. It returns the number of generated moves. This
236 /// function is very ugly, and needs cleaning up some time later. FIXME
238 int generate_evasions(const Position& pos, MoveStack* mlist) {
241 assert(pos.is_check());
244 Color us = pos.side_to_move();
245 Color them = opposite_color(us);
246 Square ksq = pos.king_square(us);
247 MoveStack* mlist_start = mlist;
249 assert(pos.piece_on(ksq) == king_of_color(us));
251 // The bitboard of occupied pieces without our king
252 Bitboard b2 = pos.occupied_squares();
255 // Find squares attacked by slider checkers, we will
256 // remove them from king evasions set so to avoid a couple
257 // of cycles in the slow king evasions legality check loop.
258 Bitboard checkers = pos.checkers();
259 Bitboard checkersAttacks = EmptyBoardBB;
260 Bitboard b = checkers & (pos.queens() | pos.bishops());
263 from = pop_1st_bit(&b);
264 checkersAttacks |= bishop_attacks_bb(from, b2);
267 b = checkers & (pos.queens() | pos.rooks());
270 from = pop_1st_bit(&b);
271 checkersAttacks |= rook_attacks_bb(from, b2);
274 // Generate evasions for king
275 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
278 to = pop_1st_bit(&b1);
280 // Make sure 'to' is not attacked by the other side. This is a bit ugly,
281 // because we can't use Position::square_is_attacked. Instead we use
282 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
283 // b2 (the occupied squares with the king removed) in order to test whether
284 // the king will remain in check on the destination square.
285 if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
286 || (pos.pawn_attacks(us, to) & pos.pawns(them))
287 || (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
288 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
289 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
290 (*mlist++).move = make_move(ksq, to);
293 // Generate evasions for other pieces only if not double check. We use a
294 // simple bit twiddling hack here rather than calling count_1s in order to
295 // save some time (we know that pos.checkers() has at most two nonzero bits).
296 if (!(checkers & (checkers - 1))) // Only one bit set?
298 Square checksq = first_1(checkers);
300 assert(pos.color_of_piece_on(checksq) == them);
302 // Find pinned pieces
303 Bitboard not_pinned = ~pos.pinned_pieces(us);
305 // Generate captures of the checking piece
308 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
311 from = pop_1st_bit(&b1);
312 if (relative_rank(us, checksq) == RANK_8)
314 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
315 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
316 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
317 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
319 (*mlist++).move = make_move(from, checksq);
323 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
324 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
325 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
329 from = pop_1st_bit(&b1);
330 (*mlist++).move = make_move(from, checksq);
333 // Blocking check evasions are possible only if the checking piece is
335 if (checkers & pos.sliders())
337 Bitboard blockSquares = squares_between(checksq, ksq);
339 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
341 // Pawn moves. Because a blocking evasion can never be a capture, we
342 // only generate pawn pushes.
344 mlist = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist);
346 mlist = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist);
349 b1 = pos.knights(us) & not_pinned;
351 mlist = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist);
353 b1 = pos.bishops(us) & not_pinned;
355 mlist = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist);
357 b1 = pos.rooks(us) & not_pinned;
359 mlist = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist);
361 b1 = pos.queens(us) & not_pinned;
363 mlist = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist);
366 // Finally, the ugly special case of en passant captures. An en passant
367 // capture can only be a check evasion if the check is not a discovered
368 // check. If pos.ep_square() is set, the last move made must have been
369 // a double pawn push. If, furthermore, the checking piece is a pawn,
370 // an en passant check evasion may be possible.
371 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
373 to = pos.ep_square();
374 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
376 assert(b1 != EmptyBoardBB);
381 from = pop_1st_bit(&b1);
383 // Before generating the move, we have to make sure it is legal.
384 // This is somewhat tricky, because the two disappearing pawns may
385 // cause new "discovered checks". We test this by removing the
386 // two relevant bits from the occupied squares bitboard, and using
387 // the low-level bitboard functions for bishop and rook attacks.
388 b2 = pos.occupied_squares();
389 clear_bit(&b2, from);
390 clear_bit(&b2, checksq);
391 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
392 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
394 (*mlist++).move = make_ep_move(from, to);
398 return int(mlist - mlist_start);
402 /// generate_legal_moves() computes a complete list of legal moves in the
403 /// current position. This function is not very fast, and should be used
404 /// only in situations where performance is unimportant. It wouldn't be
405 /// very hard to write an efficient legal move generator, but for the moment
406 /// we don't need it.
408 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
413 return generate_evasions(pos, mlist);
415 // Generate pseudo-legal moves
416 int n = generate_captures(pos, mlist);
417 n += generate_noncaptures(pos, mlist + n);
419 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
421 // Remove illegal moves from the list
422 for (int i = 0; i < n; i++)
423 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
424 mlist[i--].move = mlist[--n].move;
430 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
431 /// move and a pinned pieces bitboard as input, and tests whether
432 /// the move is legal. If the move is legal, the move itself is
433 /// returned. If not, the function returns false. This function must
434 /// only be used when the side to move is not in check.
436 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
439 assert(!pos.is_check());
440 assert(move_is_ok(m));
441 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
443 Color us = pos.side_to_move();
444 Color them = opposite_color(us);
445 Square from = move_from(m);
446 Piece pc = pos.piece_on(from);
448 // If the from square is not occupied by a piece belonging to the side to
449 // move, the move is obviously not legal.
450 if (color_of_piece(pc) != us)
453 Square to = move_to(m);
458 // The piece must be a pawn and destination square must be the
459 // en passant square.
460 if ( type_of_piece(pc) != PAWN
461 || to != pos.ep_square())
464 assert(pos.square_is_empty(to));
465 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
467 // The move is pseudo-legal, check if it is also legal
468 return pos.pl_move_is_legal(m, pinned);
472 if (move_is_short_castle(m))
474 // The piece must be a king and side to move must still have
475 // the right to castle kingside.
476 if ( type_of_piece(pc) != KING
477 ||!pos.can_castle_kingside(us))
480 assert(from == pos.king_square(us));
481 assert(to == pos.initial_kr_square(us));
482 assert(pos.piece_on(to) == rook_of_color(us));
484 Square g1 = relative_square(us, SQ_G1);
485 Square f1 = relative_square(us, SQ_F1);
487 bool illegal = false;
489 // Check if any of the squares between king and rook
490 // is occupied or under attack.
491 for (s = Min(from, g1); s <= Max(from, g1); s++)
492 if ( (s != from && s != to && !pos.square_is_empty(s))
493 || pos.square_is_attacked(s, them))
496 // Check if any of the squares between king and rook
498 for (s = Min(to, f1); s <= Max(to, f1); s++)
499 if (s != from && s != to && !pos.square_is_empty(s))
505 if (move_is_long_castle(m))
507 // The piece must be a king and side to move must still have
508 // the right to castle kingside.
509 if ( type_of_piece(pc) != KING
510 ||!pos.can_castle_queenside(us))
513 assert(from == pos.king_square(us));
514 assert(to == pos.initial_qr_square(us));
515 assert(pos.piece_on(to) == rook_of_color(us));
517 Square c1 = relative_square(us, SQ_C1);
518 Square d1 = relative_square(us, SQ_D1);
520 bool illegal = false;
522 for (s = Min(from, c1); s <= Max(from, c1); s++)
523 if( (s != from && s != to && !pos.square_is_empty(s))
524 || pos.square_is_attacked(s, them))
527 for (s = Min(to, d1); s <= Max(to, d1); s++)
528 if(s != from && s != to && !pos.square_is_empty(s))
531 if ( square_file(to) == FILE_B
532 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
533 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
541 // The destination square cannot be occupied by a friendly piece
542 if (pos.color_of_piece_on(to) == us)
545 // Proceed according to the type of the moving piece.
546 if (type_of_piece(pc) == PAWN)
548 // If the destination square is on the 8/1th rank, the move must
550 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
551 ||(square_rank(to) == RANK_1 && us != WHITE))
552 && !move_promotion(m))
555 // Proceed according to the square delta between the source and
556 // destionation squares.
563 // Capture. The destination square must be occupied by an enemy
564 // piece (en passant captures was handled earlier).
565 if (pos.color_of_piece_on(to) != them)
571 // Pawn push. The destination square must be empty.
572 if (!pos.square_is_empty(to))
577 // Double white pawn push. The destination square must be on the fourth
578 // rank, and both the destination square and the square between the
579 // source and destination squares must be empty.
580 if ( square_rank(to) != RANK_4
581 || !pos.square_is_empty(to)
582 || !pos.square_is_empty(from + DELTA_N))
587 // Double black pawn push. The destination square must be on the fifth
588 // rank, and both the destination square and the square between the
589 // source and destination squares must be empty.
590 if ( square_rank(to) != RANK_5
591 || !pos.square_is_empty(to)
592 || !pos.square_is_empty(from + DELTA_S))
599 // The move is pseudo-legal, check if it is also legal
600 return pos.pl_move_is_legal(m, pinned);
603 // Luckly we can handle all the other pieces in one go
604 return ( pos.piece_attacks_square(from, to)
605 && pos.pl_move_is_legal(m, pinned)
606 && !move_promotion(m));
612 template<PieceType Piece>
613 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
618 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
620 from = pos.piece_list(us, Piece, i);
621 b = pos.piece_attacks<Piece>(from) & target;
628 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
631 Square from = pos.king_square(us);
633 b = pos.piece_attacks<KING>(from) & target;
638 template<PieceType Piece>
639 MoveStack* generate_piece_blocking_evasions(const Position& pos, Bitboard b,
640 Bitboard blockSquares, MoveStack* mlist) {
643 Square from = pop_1st_bit(&b);
644 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
651 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
652 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
654 MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
657 Bitboard pawns = pos.pawns(Us);
658 Bitboard enemyPieces = pos.pieces_of_color(Them);
660 // Captures in the a1-h8 (a8-h1 for black) direction
661 Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
663 // Capturing promotions
664 Bitboard b2 = b1 & TRank8BB;
667 to = pop_1st_bit(&b2);
668 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
671 // Capturing non-promotions
675 to = pop_1st_bit(&b2);
676 (*mlist++).move = make_move(to - TDELTA_NE, to);
679 // Captures in the h1-a8 (h8-a1 for black) direction
680 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
682 // Capturing promotions
686 to = pop_1st_bit(&b2);
687 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
690 // Capturing non-promotions
694 to = pop_1st_bit(&b2);
695 (*mlist++).move = make_move(to - TDELTA_NW, to);
698 // Non-capturing promotions
699 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
702 to = pop_1st_bit(&b1);
703 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
706 // En passant captures
707 if (pos.ep_square() != SQ_NONE)
709 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
710 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
712 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
713 assert(b1 != EmptyBoardBB);
717 to = pop_1st_bit(&b1);
718 (*mlist++).move = make_ep_move(to, pos.ep_square());
724 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
725 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
727 MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
729 Bitboard pawns = pos.pawns(Us);
730 Bitboard enemyPieces = pos.pieces_of_color(Them);
731 Bitboard emptySquares = pos.empty_squares();
735 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
736 b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
739 to = pop_1st_bit(&b1);
740 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
741 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
742 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
745 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
746 b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
749 to = pop_1st_bit(&b1);
750 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
751 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
752 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
755 // Single pawn pushes
756 b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
760 to = pop_1st_bit(&b2);
761 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
762 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
763 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
768 to = pop_1st_bit(&b2);
769 (*mlist++).move = make_move(to - TDELTA_N, to);
772 // Double pawn pushes
773 b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
776 to = pop_1st_bit(&b2);
777 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
783 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
784 MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
786 // Find all friendly pawns not on the enemy king's file
788 Bitboard empty = pos.empty_squares();
790 if (dc != EmptyBoardBB)
792 // Pawn moves which gives discovered check. This is possible only if the
793 // pawn is not on the same file as the enemy king, because we don't
794 // generate captures.
795 b1 = pos.pawns(Us) & ~file_bb(ksq);
797 // Discovered checks, single pawn pushes, no promotions
798 b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
801 Square to = pop_1st_bit(&b3);
802 (*mlist++).move = make_move(to - TDELTA_N, to);
805 // Discovered checks, double pawn pushes
806 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
809 Square to = pop_1st_bit(&b3);
810 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
814 // Direct checks. These are possible only for pawns on neighboring files
815 // of the enemy king.
816 b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
818 // Direct checks, single pawn pushes
819 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
820 b3 = b2 & pos.pawn_attacks(Them, ksq);
823 Square to = pop_1st_bit(&b3);
824 (*mlist++).move = make_move(to - TDELTA_N, to);
827 // Direct checks, double pawn pushes
828 b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
830 & pos.pawn_attacks(Them, ksq);
833 Square to = pop_1st_bit(&b3);
834 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
839 template<PieceType Piece>
840 MoveStack* generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
841 Square ksq, MoveStack* mlist) {
843 Bitboard b = target & dc;
846 Square from = pop_1st_bit(&b);
847 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
849 bb &= ~QueenPseudoAttacks[ksq];
859 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
862 Square from = pop_1st_bit(&b);
863 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
869 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
870 MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
871 Bitboard blockSquares, MoveStack* mlist) {
874 // Find non-pinned pawns
875 Bitboard b1 = pos.pawns(Us) & not_pinned;
877 // Single pawn pushes. We don't have to AND with empty squares here,
878 // because the blocking squares will always be empty.
879 Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
882 to = pop_1st_bit(&b2);
884 assert(pos.piece_on(to) == EMPTY);
886 if (square_rank(to) == TRANK_8)
888 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
889 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
890 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
891 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
893 (*mlist++).move = make_move(to - TDELTA_N, to);
896 // Double pawn pushes
897 b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
898 b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
901 to = pop_1st_bit(&b2);
903 assert(pos.piece_on(to) == EMPTY);
904 assert(Us != WHITE || square_rank(to) == RANK_4);
905 assert(Us != BLACK || square_rank(to) == RANK_5);
907 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
912 template<CastlingSide Side>
913 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
915 Color us = pos.side_to_move();
917 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
918 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
920 Color them = opposite_color(us);
921 Square ksq = pos.king_square(us);
923 assert(pos.piece_on(ksq) == king_of_color(us));
925 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
926 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
927 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
929 bool illegal = false;
931 assert(pos.piece_on(rsq) == rook_of_color(us));
933 // It is a bit complicated to correctly handle Chess960
934 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
935 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
936 || pos.square_is_attacked(s, them))
939 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
940 if (s != ksq && s != rsq && pos.square_is_occupied(s))
943 if ( Side == QUEEN_SIDE
944 && square_file(rsq) == FILE_B
945 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
946 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
950 (*mlist++).move = make_castle_move(ksq, rsq);
955 bool castling_is_check(const Position& pos, CastlingSide side) {
957 // After castling opponent king is attacked by the castled rook?
958 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
959 Color us = pos.side_to_move();
960 Square ksq = pos.king_square(us);
961 Bitboard occ = pos.occupied_squares();
963 clear_bit(&occ, ksq); // Remove our king from the board
964 Square rsq = make_square(rookFile, square_rank(ksq));
965 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));