2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2009 Marco Costalba
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 // Simple macro to wrap a very common while loop, no facny, no flexibility,
31 // hardcoded list name 'mlist' and from square 'from'.
32 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
34 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
35 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
38 //// Local definitions
54 bool castling_is_check(const Position&, CastlingSide);
57 template<CastlingSide Side>
58 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
61 MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
64 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
66 template<Color Us, SquareDelta Diagonal>
67 MoveStack* generate_pawn_captures_diagonal(MoveStack* mlist, Bitboard pawns, Bitboard enemyPieces, bool promotion);
69 template<Color Us, bool Checks>
70 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist, Bitboard dc = EmptyBoardBB, Square ksq = SQ_NONE);
72 template<Color Us, SquareDelta Direction>
73 inline Bitboard move_pawns(Bitboard p) {
75 if (Direction == DELTA_N)
76 return Us == WHITE ? p << 8 : p >> 8;
77 else if (Direction == DELTA_NE)
78 return Us == WHITE ? p << 9 : p >> 7;
79 else if (Direction == DELTA_NW)
80 return Us == WHITE ? p << 7 : p >> 9;
85 // Template generate_piece_checks() with specializations
87 MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
90 inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
92 return (us == WHITE ? generate_pawn_noncaptures<WHITE, true>(p, m, dc, ksq)
93 : generate_pawn_noncaptures<BLACK, true>(p, m, dc, ksq));
96 // Template generate_piece_moves() with specializations and overloads
98 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
101 MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
103 template<PieceType Piece, MoveType Type>
104 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
106 assert(Piece == PAWN);
109 return (us == WHITE ? generate_pawn_captures<WHITE>(p, m)
110 : generate_pawn_captures<BLACK>(p, m));
112 return (us == WHITE ? generate_pawn_noncaptures<WHITE, false>(p, m)
113 : generate_pawn_noncaptures<BLACK, false>(p, m));
117 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
120 inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
121 Color us, Bitboard t, Bitboard pnd) {
123 return (us == WHITE ? generate_pawn_blocking_evasions<WHITE>(p, pnd, t, m)
124 : generate_pawn_blocking_evasions<BLACK>(p, pnd, t, m));
134 /// generate_captures generates() all pseudo-legal captures and queen
135 /// promotions. Returns a pointer to the end of the move list.
137 MoveStack* generate_captures(const Position& pos, MoveStack* mlist) {
140 assert(!pos.is_check());
142 Color us = pos.side_to_move();
143 Bitboard target = pos.pieces_of_color(opposite_color(us));
145 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
146 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
147 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
148 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
149 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
150 return generate_piece_moves<KING>(pos, mlist, us, target);
154 /// generate_noncaptures() generates all pseudo-legal non-captures and
155 /// underpromotions. Returns a pointer to the end of the move list.
157 MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist) {
160 assert(!pos.is_check());
162 Color us = pos.side_to_move();
163 Bitboard target = pos.empty_squares();
165 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
166 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
167 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
168 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
169 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
170 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
171 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
172 return generate_castle_moves<QUEEN_SIDE>(pos, mlist);
176 /// generate_non_capture_checks() generates all pseudo-legal non-capturing,
177 /// non-promoting checks. Returns a pointer to the end of the move list.
179 MoveStack* generate_non_capture_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));
187 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
190 mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
191 mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
192 mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
193 mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
194 mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
195 mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
197 // Castling moves that give check. Very rare but nice to have!
198 if ( pos.can_castle_queenside(us)
199 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
200 && castling_is_check(pos, QUEEN_SIDE))
201 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
203 if ( pos.can_castle_kingside(us)
204 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
205 && castling_is_check(pos, KING_SIDE))
206 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
212 /// generate_evasions() generates all check evasions when the side to move is
213 /// in check. Unlike the other move generation functions, this one generates
214 /// only legal moves. Returns a pointer to the end of the move list.
216 MoveStack* generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
219 assert(pos.is_check());
222 Color us = pos.side_to_move();
223 Color them = opposite_color(us);
224 Square ksq = pos.king_square(us);
225 Bitboard sliderAttacks = EmptyBoardBB;
226 Bitboard checkers = pos.checkers();
228 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
230 // The bitboard of occupied pieces without our king
231 Bitboard b_noKing = pos.occupied_squares();
232 clear_bit(&b_noKing, ksq);
234 // Find squares attacked by slider checkers, we will
235 // remove them from king evasions set so to avoid a couple
236 // of cycles in the slow king evasions legality check loop
237 // and to be able to use attackers_to().
238 Bitboard b = checkers & pos.pieces(BISHOP, QUEEN);
241 from = pop_1st_bit(&b);
242 sliderAttacks |= bishop_attacks_bb(from, b_noKing);
245 b = checkers & pos.pieces(ROOK, QUEEN);
248 from = pop_1st_bit(&b);
249 sliderAttacks |= rook_attacks_bb(from, b_noKing);
252 // Generate evasions for king, both captures and non captures
253 Bitboard b1 = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
254 Bitboard enemy = pos.pieces_of_color(them);
257 to = pop_1st_bit(&b1);
258 // Note that we can use attackers_to() only because we
259 // have already removed slider checkers attacked squares.
260 if (!(pos.attackers_to(to) & enemy))
261 (*mlist++).move = make_move(ksq, to);
264 // Generate evasions for other pieces only if not double check. We use a
265 // simple bit twiddling hack here rather than calling count_1s in order to
266 // save some time (we know that pos.checkers() has at most two nonzero bits).
267 if (!(checkers & (checkers - 1))) // Only one bit set?
269 Square checksq = first_1(checkers);
271 assert(pos.color_of_piece_on(checksq) == them);
273 // Generate captures of the checking piece
276 b1 = pos.attacks_from<PAWN>(checksq, them) & pos.pieces(PAWN, us) & ~pinned;
279 from = pop_1st_bit(&b1);
280 if (relative_rank(us, checksq) == RANK_8)
282 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
283 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
284 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
285 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
287 (*mlist++).move = make_move(from, checksq);
291 b1 = ( (pos.attacks_from<KNIGHT>(checksq) & pos.pieces(KNIGHT, us))
292 | (pos.attacks_from<BISHOP>(checksq) & pos.pieces(BISHOP, QUEEN, us))
293 | (pos.attacks_from<ROOK>(checksq) & pos.pieces(ROOK, QUEEN, us)) ) & ~pinned;
297 from = pop_1st_bit(&b1);
298 (*mlist++).move = make_move(from, checksq);
301 // Blocking check evasions are possible only if the checking piece is a slider
304 Bitboard blockSquares = squares_between(checksq, ksq);
306 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
310 mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
311 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
312 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
313 mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
314 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
318 // Finally, the special case of en passant captures. An en passant
319 // capture can only be a check evasion if the check is not a discovered
320 // check. If pos.ep_square() is set, the last move made must have been
321 // a double pawn push. If, furthermore, the checking piece is a pawn,
322 // an en passant check evasion may be possible.
323 if (pos.ep_square() != SQ_NONE && (checkers & pos.pieces(PAWN, them)))
325 to = pos.ep_square();
326 b1 = pos.attacks_from<PAWN>(to, them) & pos.pieces(PAWN, us);
328 // The checking pawn cannot be a discovered (bishop) check candidate
329 // otherwise we were in check also before last double push move.
330 assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
331 assert(count_1s(b1) == 1 || count_1s(b1) == 2);
336 from = pop_1st_bit(&b1);
337 // Move is always legal because checking pawn is not a discovered
338 // check candidate and our capturing pawn has been already tested
339 // against pinned pieces.
340 (*mlist++).move = make_ep_move(from, to);
348 /// generate_moves() computes a complete list of legal or pseudo legal moves in
349 /// the current position. This function is not very fast, and should be used
350 /// only in situations where performance is unimportant.
352 MoveStack* generate_moves(const Position& pos, MoveStack* mlist, bool pseudoLegal) {
356 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
359 return generate_evasions(pos, mlist, pinned);
361 // Generate pseudo-legal moves
362 MoveStack* last = generate_captures(pos, mlist);
363 last = generate_noncaptures(pos, last);
367 // Remove illegal moves from the list
368 for (MoveStack* cur = mlist; cur != last; cur++)
369 if (!pos.pl_move_is_legal(cur->move, pinned))
371 cur->move = (--last)->move;
378 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
379 /// move and tests whether the move is legal. This version is not very fast
380 /// and should be used only for non time-critical paths.
382 bool move_is_legal(const Position& pos, const Move m) {
384 MoveStack mlist[256];
385 MoveStack* last = generate_moves(pos, mlist, true);
386 for (MoveStack* cur = mlist; cur != last; cur++)
388 return pos.pl_move_is_legal(m);
394 /// Fast version of move_is_legal() that takes a position a move and
395 /// a pinned pieces bitboard as input, and tests whether the move is legal.
396 /// This version must only be used when the side to move is not in check.
398 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
401 assert(!pos.is_check());
402 assert(move_is_ok(m));
403 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
405 // Use a slower but simpler function for uncommon cases
406 if (move_is_ep(m) || move_is_castle(m))
407 return move_is_legal(pos, m);
409 Color us = pos.side_to_move();
410 Square from = move_from(m);
411 Piece pc = pos.piece_on(from);
413 // If the from square is not occupied by a piece belonging to the side to
414 // move, the move is obviously not legal.
415 if (color_of_piece(pc) != us)
418 Color them = opposite_color(us);
419 Square to = move_to(m);
421 // The destination square cannot be occupied by a friendly piece
422 if (pos.color_of_piece_on(to) == us)
425 // Proceed according to the type of the moving piece.
426 if (type_of_piece(pc) == PAWN)
428 // Move direction must be compatible with pawn color
429 int direction = to - from;
430 if ((us == WHITE) != (direction > 0))
433 // If the destination square is on the 8/1th rank, the move must
435 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
436 ||(square_rank(to) == RANK_1 && us != WHITE))
437 && !move_is_promotion(m))
440 // Proceed according to the square delta between the source and
441 // destionation squares.
448 // Capture. The destination square must be occupied by an enemy
449 // piece (en passant captures was handled earlier).
450 if (pos.color_of_piece_on(to) != them)
456 // Pawn push. The destination square must be empty.
457 if (!pos.square_is_empty(to))
462 // Double white pawn push. The destination square must be on the fourth
463 // rank, and both the destination square and the square between the
464 // source and destination squares must be empty.
465 if ( square_rank(to) != RANK_4
466 || !pos.square_is_empty(to)
467 || !pos.square_is_empty(from + DELTA_N))
472 // Double black pawn push. The destination square must be on the fifth
473 // rank, and both the destination square and the square between the
474 // source and destination squares must be empty.
475 if ( square_rank(to) != RANK_5
476 || !pos.square_is_empty(to)
477 || !pos.square_is_empty(from + DELTA_S))
484 // The move is pseudo-legal, check if it is also legal
485 return pos.pl_move_is_legal(m, pinned);
488 // Luckly we can handle all the other pieces in one go
489 return ( bit_is_set(pos.attacks_from(pc, from), to)
490 && pos.pl_move_is_legal(m, pinned)
491 && !move_is_promotion(m));
497 template<PieceType Piece>
498 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
503 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
505 from = pos.piece_list(us, Piece, i);
506 b = pos.attacks_from<Piece>(from) & target;
512 template<PieceType Piece>
513 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
514 Color us, Bitboard target, Bitboard pinned) {
518 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
520 from = pos.piece_list(us, Piece, i);
521 if (pinned && bit_is_set(pinned, from))
524 b = pos.attacks_from<Piece>(from) & target;
531 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
534 Square from = pos.king_square(us);
536 b = pos.attacks_from<KING>(from) & target;
541 template<Color Us, SquareDelta Diagonal>
542 MoveStack* generate_pawn_captures_diagonal(MoveStack* mlist, Bitboard pawns, Bitboard enemyPieces, bool promotion) {
544 // Calculate our parametrized parameters at compile time
545 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
546 const Bitboard TFileABB = (Diagonal == DELTA_NE ? FileABB : FileHBB);
547 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
548 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
549 const SquareDelta TTDELTA_NE = (Diagonal == DELTA_NE ? TDELTA_NE : TDELTA_NW);
553 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
554 Bitboard b1 = move_pawns<Us, Diagonal>(pawns) & ~TFileABB & enemyPieces;
556 // Capturing promotions
559 Bitboard b2 = b1 & TRank8BB;
563 to = pop_1st_bit(&b2);
564 (*mlist++).move = make_promotion_move(to - TTDELTA_NE, to, QUEEN);
568 // Capturing non-promotions
569 SERIALIZE_MOVES_D(b1, -TTDELTA_NE);
574 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
576 // Calculate our parametrized parameters at compile time
577 const Color Them = (Us == WHITE ? BLACK : WHITE);
578 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
579 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
580 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
583 Bitboard pawns = pos.pieces(PAWN, Us);
584 Bitboard enemyPieces = pos.pieces_of_color(opposite_color(Us));
585 bool possiblePromotion = (pawns & TRank7BB);
587 // Standard captures and capturing promotions in both directions
588 mlist = generate_pawn_captures_diagonal<Us, DELTA_NE>(mlist, pawns, enemyPieces, possiblePromotion);
589 mlist = generate_pawn_captures_diagonal<Us, DELTA_NW>(mlist, pawns, enemyPieces, possiblePromotion);
591 // Non-capturing promotions
592 if (possiblePromotion)
594 Bitboard b1 = move_pawns<Us, DELTA_N>(pawns) & pos.empty_squares() & TRank8BB;
597 to = pop_1st_bit(&b1);
598 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
602 // En passant captures
603 if (pos.ep_square() != SQ_NONE)
605 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
606 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
608 Bitboard b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
609 assert(b1 != EmptyBoardBB);
613 to = pop_1st_bit(&b1);
614 (*mlist++).move = make_ep_move(to, pos.ep_square());
620 template<Color Us, bool GenerateChecks>
621 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist, Bitboard dc, Square ksq) {
623 // Calculate our parametrized parameters at compile time
624 const Color Them = (Us == WHITE ? BLACK : WHITE);
625 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
626 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
627 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
628 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
629 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
630 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
632 Bitboard b1, b2, dcPawns1, dcPawns2;
634 Bitboard pawns = pos.pieces(PAWN, Us);
635 Bitboard emptySquares = pos.empty_squares();
637 if (pawns & TRank7BB) // There is some promotion candidate ?
639 // When generating checks consider under-promotion moves (both captures
640 // and non captures) only if can give a discovery check.
641 Bitboard pp = GenerateChecks ? pawns & dc : pawns;
642 Bitboard enemyPieces = pos.pieces_of_color(opposite_color(Us));
644 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
645 b1 = move_pawns<Us, DELTA_NE>(pp) & ~FileABB & enemyPieces & TRank8BB;
648 to = pop_1st_bit(&b1);
649 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
650 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
651 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
654 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
655 b1 = move_pawns<Us, DELTA_NW>(pp) & ~FileHBB & enemyPieces & TRank8BB;
658 to = pop_1st_bit(&b1);
659 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
660 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
661 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
664 // Underpromotion pawn pushes
665 b1 = move_pawns<Us, DELTA_N>(pp) & emptySquares & TRank8BB;
668 to = pop_1st_bit(&b1);
669 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
670 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
671 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
675 dcPawns1 = dcPawns2 = EmptyBoardBB;
676 if (GenerateChecks && (dc & pawns))
678 // Pawn moves which gives discovered check. This is possible only if the
679 // pawn is not on the same file as the enemy king, because we don't
680 // generate captures.
681 dcPawns1 = move_pawns<Us, DELTA_N>(pawns & dc & ~file_bb(ksq)) & emptySquares & ~TRank8BB;
682 dcPawns2 = move_pawns<Us, DELTA_N>(dcPawns1 & TRank3BB) & emptySquares;
685 // Single pawn pushes
686 b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares & ~TRank8BB;
687 b2 = GenerateChecks ? (b1 & pos.attacks_from<PAWN>(ksq, Them)) | dcPawns1 : b1;
688 SERIALIZE_MOVES_D(b2, -TDELTA_N);
690 // Double pawn pushes
691 b1 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
692 b2 = GenerateChecks ? (b1 & pos.attacks_from<PAWN>(ksq, Them)) | dcPawns2 : b1;
693 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
697 template<PieceType Piece>
698 MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
699 Bitboard dc, Square ksq) {
701 Bitboard target = pos.pieces(Piece, us);
704 Bitboard b = target & dc;
707 Square from = pop_1st_bit(&b);
708 Bitboard bb = pos.attacks_from<Piece>(from) & pos.empty_squares();
710 bb &= ~QueenPseudoAttacks[ksq];
717 Bitboard checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
718 if (Piece == KING || !checkSqs)
723 Square from = pop_1st_bit(&b);
724 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
725 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
726 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
729 Bitboard bb = pos.attacks_from<Piece>(from) & checkSqs;
736 MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
737 Bitboard blockSquares, MoveStack* mlist) {
739 // Calculate our parametrized parameters at compile time
740 const Rank TRANK_8 = (Us == WHITE ? RANK_8 : RANK_1);
741 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
742 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
746 // Find non-pinned pawns and push them one square
747 Bitboard b1 = move_pawns<Us, DELTA_N>(pos.pieces(PAWN, Us) & ~pinned);
749 // We don't have to AND with empty squares here,
750 // because the blocking squares will always be empty.
751 Bitboard b2 = b1 & blockSquares;
754 to = pop_1st_bit(&b2);
756 assert(pos.piece_on(to) == EMPTY);
758 if (square_rank(to) == TRANK_8)
760 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
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);
765 (*mlist++).move = make_move(to - TDELTA_N, to);
768 // Double pawn pushes
769 b2 = b1 & pos.empty_squares() & TRank3BB;
770 b2 = move_pawns<Us, DELTA_N>(b2) & blockSquares;
773 to = pop_1st_bit(&b2);
775 assert(pos.piece_on(to) == EMPTY);
776 assert(Us != WHITE || square_rank(to) == RANK_4);
777 assert(Us != BLACK || square_rank(to) == RANK_5);
779 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
784 template<CastlingSide Side>
785 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
787 Color us = pos.side_to_move();
789 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
790 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
792 Color them = opposite_color(us);
793 Square ksq = pos.king_square(us);
795 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
797 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
798 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
799 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
801 bool illegal = false;
803 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
805 // It is a bit complicated to correctly handle Chess960
806 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
807 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
808 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
811 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
812 if (s != ksq && s != rsq && pos.square_is_occupied(s))
815 if ( Side == QUEEN_SIDE
816 && square_file(rsq) == FILE_B
817 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
818 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
822 (*mlist++).move = make_castle_move(ksq, rsq);
827 bool castling_is_check(const Position& pos, CastlingSide side) {
829 // After castling opponent king is attacked by the castled rook?
830 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
831 Color us = pos.side_to_move();
832 Square ksq = pos.king_square(us);
833 Bitboard occ = pos.occupied_squares();
835 clear_bit(&occ, ksq); // Remove our king from the board
836 Square rsq = make_square(rookFile, square_rank(ksq));
837 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));