2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung 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 Glaurung 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/>.
30 //// Local definitions
35 inline Bitboard forward_white(Bitboard b) { return b << 8; }
36 inline Bitboard forward_right_white(Bitboard b) { return b << 9; }
37 inline Bitboard forward_left_white(Bitboard b) { return b << 7; }
39 inline Bitboard forward_black(Bitboard b) { return b >> 8; }
40 inline Bitboard forward_right_black(Bitboard b) { return b >> 7; }
41 inline Bitboard forward_left_black(Bitboard b) { return b >> 9; }
45 Bitboard Rank3BB, Rank8BB;
47 SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
49 typedef Bitboard (*Shift_fn)(Bitboard b);
50 Shift_fn forward, forward_left, forward_right;
53 const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, RANK_8, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK,
54 &forward_white, forward_left_white, forward_right_white };
56 const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
57 &forward_black, &forward_left_black, &forward_right_black };
59 int generate_pawn_captures(const PawnOffsets&, const Position&, MoveStack*);
60 int generate_pawn_noncaptures(const PawnOffsets&, const Position&, MoveStack*);
61 int generate_pawn_checks(const PawnOffsets&, const Position&, Bitboard, Square, MoveStack*, int);
62 int generate_castle_moves(const Position&, MoveStack*, Color);
63 int generate_pawn_blocking_evasions(const PawnOffsets&, const Position&, Bitboard, Bitboard, MoveStack*, int);
66 int generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
69 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
72 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
81 /// generate_captures generates() all pseudo-legal captures and queen
82 /// promotions. The return value is the number of moves generated.
84 int generate_captures(const Position& pos, MoveStack* mlist) {
87 assert(!pos.is_check());
89 Color us = pos.side_to_move();
90 Bitboard target = pos.pieces_of_color(opposite_color(us));
94 n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
96 n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
98 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
99 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
100 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
101 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
102 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
107 /// generate_noncaptures() generates all pseudo-legal non-captures and
108 /// underpromotions. The return value is the number of moves generated.
110 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
113 assert(!pos.is_check());
115 Color us = pos.side_to_move();
116 Bitboard target = pos.empty_squares();
120 n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
122 n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
124 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
125 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
126 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
127 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
128 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
130 n += generate_castle_moves(pos, mlist+n, us);
135 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
136 /// checks, except castling moves (will add this later). It returns the
137 /// number of generated moves.
139 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
142 assert(!pos.is_check());
145 Color us = pos.side_to_move();
146 Square ksq = pos.king_square(opposite_color(us));
148 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
150 dc = pos.discovered_check_candidates(us);
154 n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
156 n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
159 Bitboard b = pos.knights(us);
161 n = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist, n);
165 n = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist, n);
169 n = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist, n);
173 n = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist, n);
176 Square from = pos.king_square(us);
177 if (bit_is_set(dc, from))
179 b = pos.piece_attacks<KING>(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
182 Square to = pop_1st_bit(&b);
183 mlist[n++].move = make_move(from, to);
187 // TODO: Castling moves!
193 /// generate_evasions() generates all check evasions when the side to move is
194 /// in check. Unlike the other move generation functions, this one generates
195 /// only legal moves. It returns the number of generated moves. This
196 /// function is very ugly, and needs cleaning up some time later. FIXME
198 int generate_evasions(const Position& pos, MoveStack* mlist) {
201 assert(pos.is_check());
203 Color us = pos.side_to_move();
204 Color them = opposite_color(us);
205 Square ksq = pos.king_square(us);
209 assert(pos.piece_on(ksq) == king_of_color(us));
211 // Generate evasions for king
212 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
213 Bitboard b2 = pos.occupied_squares();
218 Square to = pop_1st_bit(&b1);
220 // Make sure to is not attacked by the other side. This is a bit ugly,
221 // because we can't use Position::square_is_attacked. Instead we use
222 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
223 // b2 (the occupied squares with the king removed) in order to test whether
224 // the king will remain in check on the destination square.
225 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
226 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
227 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
228 || (pos.pawn_attacks(us, to) & pos.pawns(them))
229 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
231 mlist[n++].move = make_move(ksq, to);
234 // Generate evasions for other pieces only if not double check. We use a
235 // simple bit twiddling hack here rather than calling count_1s in order to
236 // save some time (we know that pos.checkers() has at most two nonzero bits).
237 Bitboard checkers = pos.checkers();
239 if (!(checkers & (checkers - 1))) // Only one bit set?
241 Square checksq = first_1(checkers);
243 assert(pos.color_of_piece_on(checksq) == them);
245 // Find pinned pieces
246 Bitboard not_pinned = ~pos.pinned_pieces(us);
248 // Generate captures of the checking piece
251 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
254 from = pop_1st_bit(&b1);
255 if (relative_rank(us, checksq) == RANK_8)
257 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
258 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
259 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
260 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
262 mlist[n++].move = make_move(from, checksq);
266 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
267 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
268 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
272 from = pop_1st_bit(&b1);
273 mlist[n++].move = make_move(from, checksq);
276 // Blocking check evasions are possible only if the checking piece is
278 if (checkers & pos.sliders())
280 Bitboard blockSquares = squares_between(checksq, ksq);
282 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
284 // Pawn moves. Because a blocking evasion can never be a capture, we
285 // only generate pawn pushes.
287 n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
289 n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
292 b1 = pos.knights(us) & not_pinned;
294 n = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist, n);
296 b1 = pos.bishops(us) & not_pinned;
298 n = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist, n);
300 b1 = pos.rooks(us) & not_pinned;
302 n = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist, n);
304 b1 = pos.queens(us) & not_pinned;
306 n = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist, n);
309 // Finally, the ugly special case of en passant captures. An en passant
310 // capture can only be a check evasion if the check is not a discovered
311 // check. If pos.ep_square() is set, the last move made must have been
312 // a double pawn push. If, furthermore, the checking piece is a pawn,
313 // an en passant check evasion may be possible.
314 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
316 to = pos.ep_square();
317 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
319 assert(b1 != EmptyBoardBB);
324 from = pop_1st_bit(&b1);
326 // Before generating the move, we have to make sure it is legal.
327 // This is somewhat tricky, because the two disappearing pawns may
328 // cause new "discovered checks". We test this by removing the
329 // two relevant bits from the occupied squares bitboard, and using
330 // the low-level bitboard functions for bishop and rook attacks.
331 b2 = pos.occupied_squares();
332 clear_bit(&b2, from);
333 clear_bit(&b2, checksq);
334 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
335 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
337 mlist[n++].move = make_ep_move(from, to);
345 /// generate_legal_moves() computes a complete list of legal moves in the
346 /// current position. This function is not very fast, and should be used
347 /// only in situations where performance is unimportant. It wouldn't be
348 /// very hard to write an efficient legal move generator, but for the moment
349 /// we don't need it.
351 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
356 return generate_evasions(pos, mlist);
358 // Generate pseudo-legal moves:
359 int n = generate_captures(pos, mlist);
360 n += generate_noncaptures(pos, mlist + n);
362 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
364 // Remove illegal moves from the list:
365 for (int i = 0; i < n; i++)
366 if (!pos.move_is_legal(mlist[i].move, pinned))
367 mlist[i--].move = mlist[--n].move;
373 /// generate_move_if_legal() takes a position and a (not necessarily
374 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
375 /// whether the move is legal. If the move is legal, the move itself is
376 /// returned. If not, the function returns MOVE_NONE. This function must
377 /// only be used when the side to move is not in check.
379 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
382 assert(!pos.is_check());
383 assert(move_is_ok(m));
385 Color us = pos.side_to_move();
386 Color them = opposite_color(us);
387 Square from = move_from(m);
388 Piece pc = pos.piece_on(from);
390 // If the from square is not occupied by a piece belonging to the side to
391 // move, the move is obviously not legal.
392 if (color_of_piece(pc) != us)
395 Square to = move_to(m);
400 // The piece must be a pawn and destination square must be the
401 // en passant square.
402 if ( type_of_piece(pc) != PAWN
403 || to != pos.ep_square())
406 assert(pos.square_is_empty(to));
407 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
409 // The move is pseudo-legal. If it is legal, return it.
410 return (pos.move_is_legal(m) ? m : MOVE_NONE);
414 if (move_is_short_castle(m))
416 // The piece must be a king and side to move must still have
417 // the right to castle kingside.
418 if ( type_of_piece(pc) != KING
419 ||!pos.can_castle_kingside(us))
422 assert(from == pos.king_square(us));
423 assert(to == pos.initial_kr_square(us));
424 assert(pos.piece_on(to) == rook_of_color(us));
426 Square g1 = relative_square(us, SQ_G1);
427 Square f1 = relative_square(us, SQ_F1);
429 bool illegal = false;
431 // Check if any of the squares between king and rook
432 // is occupied or under attack.
433 for (s = Min(from, g1); s <= Max(from, g1); s++)
434 if ( (s != from && s != to && !pos.square_is_empty(s))
435 || pos.square_is_attacked(s, them))
438 // Check if any of the squares between king and rook
440 for (s = Min(to, f1); s <= Max(to, f1); s++)
441 if (s != from && s != to && !pos.square_is_empty(s))
444 return (!illegal ? m : MOVE_NONE);
447 if (move_is_long_castle(m))
449 // The piece must be a king and side to move must still have
450 // the right to castle kingside.
451 if ( type_of_piece(pc) != KING
452 ||!pos.can_castle_queenside(us))
455 assert(from == pos.king_square(us));
456 assert(to == pos.initial_qr_square(us));
457 assert(pos.piece_on(to) == rook_of_color(us));
459 Square c1 = relative_square(us, SQ_C1);
460 Square d1 = relative_square(us, SQ_D1);
462 bool illegal = false;
464 for (s = Min(from, c1); s <= Max(from, c1); s++)
465 if( (s != from && s != to && !pos.square_is_empty(s))
466 || pos.square_is_attacked(s, them))
469 for (s = Min(to, d1); s <= Max(to, d1); s++)
470 if(s != from && s != to && !pos.square_is_empty(s))
473 if ( square_file(to) == FILE_B
474 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
475 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
478 return (!illegal ? m : MOVE_NONE);
483 // The destination square cannot be occupied by a friendly piece
484 if (pos.color_of_piece_on(to) == us)
487 // Proceed according to the type of the moving piece.
488 if (type_of_piece(pc) == PAWN)
490 // If the destination square is on the 8/1th rank, the move must
492 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
493 ||(square_rank(to) == RANK_1 && us != WHITE))
494 && !move_promotion(m))
497 // Proceed according to the square delta between the source and
498 // destionation squares.
505 // Capture. The destination square must be occupied by an enemy
506 // piece (en passant captures was handled earlier).
507 if (pos.color_of_piece_on(to) != them)
513 // Pawn push. The destination square must be empty.
514 if (!pos.square_is_empty(to))
519 // Double white pawn push. The destination square must be on the fourth
520 // rank, and both the destination square and the square between the
521 // source and destination squares must be empty.
522 if ( square_rank(to) != RANK_4
523 || !pos.square_is_empty(to)
524 || !pos.square_is_empty(from + DELTA_N))
529 // Double black pawn push. The destination square must be on the fifth
530 // rank, and both the destination square and the square between the
531 // source and destination squares must be empty.
532 if ( square_rank(to) != RANK_5
533 || !pos.square_is_empty(to)
534 || !pos.square_is_empty(from + DELTA_S))
541 // The move is pseudo-legal. Return it if it is legal.
542 return (pos.move_is_legal(m) ? m : MOVE_NONE);
545 // Luckly we can handle all the other pieces in one go
546 return ( pos.piece_attacks_square(from, to)
547 && pos.move_is_legal(m)
548 && !move_promotion(m) ? m : MOVE_NONE);
554 int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
556 Bitboard pawns = pos.pawns(ofs.us);
557 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
561 // Captures in the a1-h8 (a8-h1 for black) direction
562 Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
564 // Capturing promotions
565 Bitboard b2 = b1 & ofs.Rank8BB;
568 sq = pop_1st_bit(&b2);
569 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
572 // Capturing non-promotions
573 b2 = b1 & ~ofs.Rank8BB;
576 sq = pop_1st_bit(&b2);
577 mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
580 // Captures in the h1-a8 (h8-a1 for black) direction
581 b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
583 // Capturing promotions
584 b2 = b1 & ofs.Rank8BB;
587 sq = pop_1st_bit(&b2);
588 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
591 // Capturing non-promotions
592 b2 = b1 & ~ofs.Rank8BB;
595 sq = pop_1st_bit(&b2);
596 mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
599 // Non-capturing promotions
600 b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
603 sq = pop_1st_bit(&b1);
604 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
607 // En passant captures
608 if (pos.ep_square() != SQ_NONE)
610 assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
611 assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
613 b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
614 assert(b1 != EmptyBoardBB);
618 sq = pop_1st_bit(&b1);
619 mlist[n++].move = make_ep_move(sq, pos.ep_square());
626 int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
628 Bitboard pawns = pos.pawns(ofs.us);
629 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
630 Bitboard emptySquares = pos.empty_squares();
635 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
636 b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
639 sq = pop_1st_bit(&b1);
640 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
641 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
642 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
645 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
646 b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
649 sq = pop_1st_bit(&b1);
650 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
651 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
652 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
655 // Single pawn pushes
656 b1 = ofs.forward(pawns) & emptySquares;
657 b2 = b1 & ofs.Rank8BB;
660 sq = pop_1st_bit(&b2);
661 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
662 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
663 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
665 b2 = b1 & ~ofs.Rank8BB;
668 sq = pop_1st_bit(&b2);
669 mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
672 // Double pawn pushes
673 b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
676 sq = pop_1st_bit(&b2);
677 mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
683 template<PieceType Piece>
684 int generate_piece_moves(const Position &pos, MoveStack *mlist,
685 Color side, Bitboard target) {
691 for (int i = 0; i < pos.piece_count(side, Piece); i++)
693 from = pos.piece_list(side, Piece, i);
694 b = pos.piece_attacks<Piece>(from) & target;
697 to = pop_1st_bit(&b);
698 mlist[n++].move = make_move(from, to);
705 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
709 if (pos.can_castle(us))
711 Color them = opposite_color(us);
712 Square ksq = pos.king_square(us);
714 assert(pos.piece_on(ksq) == king_of_color(us));
716 if (pos.can_castle_kingside(us))
718 Square rsq = pos.initial_kr_square(us);
719 Square g1 = relative_square(us, SQ_G1);
720 Square f1 = relative_square(us, SQ_F1);
722 bool illegal = false;
724 assert(pos.piece_on(rsq) == rook_of_color(us));
726 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
727 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
728 || pos.square_is_attacked(s, them))
731 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
732 if (s != ksq && s != rsq && pos.square_is_occupied(s))
736 mlist[n++].move = make_castle_move(ksq, rsq);
739 if (pos.can_castle_queenside(us))
741 Square rsq = pos.initial_qr_square(us);
742 Square c1 = relative_square(us, SQ_C1);
743 Square d1 = relative_square(us, SQ_D1);
745 bool illegal = false;
747 assert(pos.piece_on(rsq) == rook_of_color(us));
749 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
750 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
751 || pos.square_is_attacked(s, them))
754 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
755 if (s != ksq && s != rsq && pos.square_is_occupied(s))
758 if ( square_file(rsq) == FILE_B
759 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
760 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
764 mlist[n++].move = make_castle_move(ksq, rsq);
770 template<PieceType Piece>
771 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
772 Square ksq, MoveStack* mlist, int n) {
775 Bitboard b = target & dc;
778 Square from = pop_1st_bit(&b);
779 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
782 Square to = pop_1st_bit(&bb);
783 mlist[n++].move = make_move(from, to);
789 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
792 Square from = pop_1st_bit(&b);
793 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
796 Square to = pop_1st_bit(&bb);
797 mlist[n++].move = make_move(from, to);
803 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
805 // Pawn moves which give discovered check. This is possible only if the
806 // pawn is not on the same file as the enemy king, because we don't
807 // generate captures.
808 Bitboard empty = pos.empty_squares();
810 // Find all friendly pawns not on the enemy king's file
811 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
813 // Discovered checks, single pawn pushes
814 b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
817 Square to = pop_1st_bit(&b3);
818 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
821 // Discovered checks, double pawn pushes
822 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
825 Square to = pop_1st_bit(&b3);
826 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
829 // Direct checks. These are possible only for pawns on neighboring files
832 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
834 // Direct checks, single pawn pushes
835 b2 = (ofs.forward)(b1) & empty;
836 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
839 Square to = pop_1st_bit(&b3);
840 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
843 // Direct checks, double pawn pushes
844 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
847 Square to = pop_1st_bit(&b3);
848 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
854 template<PieceType Piece>
855 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
856 Bitboard blockSquares, MoveStack* mlist, int n) {
859 Square from = pop_1st_bit(&b);
860 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
863 Square to = pop_1st_bit(&bb);
864 mlist[n++].move = make_move(from, to);
871 int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
872 Bitboard blockSquares, MoveStack* mlist, int n) {
873 // Find non-pinned pawns
874 Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
876 // Single pawn pushes. We don't have to AND with empty squares here,
877 // because the blocking squares will always be empty.
878 Bitboard b2 = (ofs.forward)(b1) & blockSquares;
881 Square to = pop_1st_bit(&b2);
883 assert(pos.piece_on(to) == EMPTY);
885 if (square_rank(to) == ofs.RANK_8)
887 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
888 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
889 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
890 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
892 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
895 // Double pawn pushes
896 b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
899 Square to = pop_1st_bit(&b2);
901 assert(pos.piece_on(to) == EMPTY);
902 assert(ofs.us != WHITE || square_rank(to) == RANK_4);
903 assert(ofs.us != BLACK || square_rank(to) == RANK_5);
905 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);