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_piece_checks(PieceType, const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
63 int generate_piece_moves(PieceType, const Position&, MoveStack*, Color, Bitboard);
64 int generate_castle_moves(const Position&, MoveStack*, Color);
65 int generate_piece_blocking_evasions(PieceType, const Position&, Bitboard, Bitboard, MoveStack*, int);
66 int generate_pawn_blocking_evasions(const PawnOffsets&, const Position&, Bitboard, Bitboard, MoveStack*, int);
75 /// generate_captures generates() all pseudo-legal captures and queen
76 /// promotions. The return value is the number of moves generated.
78 int generate_captures(const Position& pos, MoveStack* mlist) {
81 assert(!pos.is_check());
83 Color us = pos.side_to_move();
84 Bitboard target = pos.pieces_of_color(opposite_color(us));
88 n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
90 n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
92 for (PieceType pce = KNIGHT; pce <= KING; pce++)
93 n += generate_piece_moves(pce, pos, mlist+n, us, target);
99 /// generate_noncaptures() generates all pseudo-legal non-captures and
100 /// underpromotions. The return value is the number of moves generated.
102 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
105 assert(!pos.is_check());
107 Color us = pos.side_to_move();
108 Bitboard target = pos.empty_squares();
112 n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
114 n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
116 for (PieceType pce = KNIGHT; pce <= KING; pce++)
117 n += generate_piece_moves(pce, pos, mlist+n, us, target);
119 n += generate_castle_moves(pos, mlist+n, us);
124 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
125 /// checks, except castling moves (will add this later). It returns the
126 /// number of generated moves.
128 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
131 assert(!pos.is_check());
134 Color us = pos.side_to_move();
135 Square ksq = pos.king_square(opposite_color(us));
137 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
139 dc = pos.discovered_check_candidates(us);
143 n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
145 n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
148 Bitboard b = pos.knights(us);
150 n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n);
154 n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n);
158 n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n);
162 n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n);
165 Square from = pos.king_square(us);
166 if (bit_is_set(dc, from))
168 b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
171 Square to = pop_1st_bit(&b);
172 mlist[n++].move = make_move(from, to);
176 // TODO: Castling moves!
182 /// generate_evasions() generates all check evasions when the side to move is
183 /// in check. Unlike the other move generation functions, this one generates
184 /// only legal moves. It returns the number of generated moves. This
185 /// function is very ugly, and needs cleaning up some time later. FIXME
187 int generate_evasions(const Position& pos, MoveStack* mlist) {
190 assert(pos.is_check());
192 Color us = pos.side_to_move();
193 Color them = opposite_color(us);
194 Square ksq = pos.king_square(us);
198 assert(pos.piece_on(ksq) == king_of_color(us));
200 // Generate evasions for king
201 Bitboard b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
202 Bitboard b2 = pos.occupied_squares();
207 Square to = pop_1st_bit(&b1);
209 // Make sure to is not attacked by the other side. This is a bit ugly,
210 // because we can't use Position::square_is_attacked. Instead we use
211 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
212 // b2 (the occupied squares with the king removed) in order to test whether
213 // the king will remain in check on the destination square.
214 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
215 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
216 || (pos.knight_attacks(to) & pos.knights(them))
217 || (pos.pawn_attacks(us, to) & pos.pawns(them))
218 || (pos.king_attacks(to) & pos.kings(them))))
220 mlist[n++].move = make_move(ksq, to);
223 // Generate evasions for other pieces only if not double check. We use a
224 // simple bit twiddling hack here rather than calling count_1s in order to
225 // save some time (we know that pos.checkers() has at most two nonzero bits).
226 Bitboard checkers = pos.checkers();
228 if (!(checkers & (checkers - 1))) // Only one bit set?
230 Square checksq = first_1(checkers);
232 assert(pos.color_of_piece_on(checksq) == them);
234 // Find pinned pieces
235 Bitboard not_pinned = ~pos.pinned_pieces(us);
237 // Generate captures of the checking piece
240 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
243 from = pop_1st_bit(&b1);
244 if (relative_rank(us, checksq) == RANK_8)
246 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
247 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
248 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
249 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
251 mlist[n++].move = make_move(from, checksq);
255 b1 = (pos.knight_attacks(checksq) & pos.knights(us))
256 | (pos.bishop_attacks(checksq) & pos.bishops_and_queens(us))
257 | (pos.rook_attacks(checksq) & pos.rooks_and_queens(us))
262 from = pop_1st_bit(&b1);
263 mlist[n++].move = make_move(from, checksq);
266 // Blocking check evasions are possible only if the checking piece is
268 if (checkers & pos.sliders())
270 Bitboard blockSquares = squares_between(checksq, ksq);
272 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
274 // Pawn moves. Because a blocking evasion can never be a capture, we
275 // only generate pawn pushes.
277 n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
279 n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
282 b1 = pos.knights(us) & not_pinned;
284 n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
286 b1 = pos.bishops(us) & not_pinned;
288 n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
290 b1 = pos.rooks(us) & not_pinned;
292 n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
294 b1 = pos.queens(us) & not_pinned;
296 n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
299 // Finally, the ugly special case of en passant captures. An en passant
300 // capture can only be a check evasion if the check is not a discovered
301 // check. If pos.ep_square() is set, the last move made must have been
302 // a double pawn push. If, furthermore, the checking piece is a pawn,
303 // an en passant check evasion may be possible.
304 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
306 to = pos.ep_square();
307 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
309 assert(b1 != EmptyBoardBB);
314 from = pop_1st_bit(&b1);
316 // Before generating the move, we have to make sure it is legal.
317 // This is somewhat tricky, because the two disappearing pawns may
318 // cause new "discovered checks". We test this by removing the
319 // two relevant bits from the occupied squares bitboard, and using
320 // the low-level bitboard functions for bishop and rook attacks.
321 b2 = pos.occupied_squares();
322 clear_bit(&b2, from);
323 clear_bit(&b2, checksq);
324 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
325 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
327 mlist[n++].move = make_ep_move(from, to);
335 /// generate_legal_moves() computes a complete list of legal moves in the
336 /// current position. This function is not very fast, and should be used
337 /// only in situations where performance is unimportant. It wouldn't be
338 /// very hard to write an efficient legal move generator, but for the moment
339 /// we don't need it.
341 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
346 return generate_evasions(pos, mlist);
348 // Generate pseudo-legal moves:
349 int n = generate_captures(pos, mlist);
350 n += generate_noncaptures(pos, mlist + n);
352 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
354 // Remove illegal moves from the list:
355 for (int i = 0; i < n; i++)
356 if (!pos.move_is_legal(mlist[i].move, pinned))
357 mlist[i--].move = mlist[--n].move;
363 /// generate_move_if_legal() takes a position and a (not necessarily
364 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
365 /// whether the move is legal. If the move is legal, the move itself is
366 /// returned. If not, the function returns MOVE_NONE. This function must
367 /// only be used when the side to move is not in check.
369 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
372 assert(!pos.is_check());
373 assert(move_is_ok(m));
375 Color us = pos.side_to_move();
376 Color them = opposite_color(us);
377 Square from = move_from(m);
378 Piece pc = pos.piece_on(from);
380 // If the from square is not occupied by a piece belonging to the side to
381 // move, the move is obviously not legal.
382 if (color_of_piece(pc) != us)
385 Square to = move_to(m);
390 // The piece must be a pawn and destination square must be the
391 // en passant square.
392 if ( type_of_piece(pc) != PAWN
393 || to != pos.ep_square())
396 assert(pos.square_is_empty(to));
397 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
399 // The move is pseudo-legal. If it is legal, return it.
400 return (pos.move_is_legal(m) ? m : MOVE_NONE);
404 if (move_is_short_castle(m))
406 // The piece must be a king and side to move must still have
407 // the right to castle kingside.
408 if ( type_of_piece(pc) != KING
409 ||!pos.can_castle_kingside(us))
412 assert(from == pos.king_square(us));
413 assert(to == pos.initial_kr_square(us));
414 assert(pos.piece_on(to) == rook_of_color(us));
416 Square g1 = relative_square(us, SQ_G1);
417 Square f1 = relative_square(us, SQ_F1);
419 bool illegal = false;
421 // Check if any of the squares between king and rook
422 // is occupied or under attack.
423 for (s = Min(from, g1); s <= Max(from, g1); s++)
424 if ( (s != from && s != to && !pos.square_is_empty(s))
425 || pos.square_is_attacked(s, them))
428 // Check if any of the squares between king and rook
430 for (s = Min(to, f1); s <= Max(to, f1); s++)
431 if (s != from && s != to && !pos.square_is_empty(s))
434 return (!illegal ? m : MOVE_NONE);
437 if (move_is_long_castle(m))
439 // The piece must be a king and side to move must still have
440 // the right to castle kingside.
441 if ( type_of_piece(pc) != KING
442 ||!pos.can_castle_queenside(us))
445 assert(from == pos.king_square(us));
446 assert(to == pos.initial_qr_square(us));
447 assert(pos.piece_on(to) == rook_of_color(us));
449 Square c1 = relative_square(us, SQ_C1);
450 Square d1 = relative_square(us, SQ_D1);
452 bool illegal = false;
454 for (s = Min(from, c1); s <= Max(from, c1); s++)
455 if( (s != from && s != to && !pos.square_is_empty(s))
456 || pos.square_is_attacked(s, them))
459 for (s = Min(to, d1); s <= Max(to, d1); s++)
460 if(s != from && s != to && !pos.square_is_empty(s))
463 if ( square_file(to) == FILE_B
464 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
465 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
468 return (!illegal ? m : MOVE_NONE);
473 // The destination square cannot be occupied by a friendly piece
474 if (pos.color_of_piece_on(to) == us)
477 // Proceed according to the type of the moving piece.
478 if (type_of_piece(pc) == PAWN)
480 // If the destination square is on the 8/1th rank, the move must
482 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
483 ||(square_rank(to) == RANK_1 && us != WHITE))
484 && !move_promotion(m))
487 // Proceed according to the square delta between the source and
488 // destionation squares.
495 // Capture. The destination square must be occupied by an enemy
496 // piece (en passant captures was handled earlier).
497 if (pos.color_of_piece_on(to) != them)
503 // Pawn push. The destination square must be empty.
504 if (!pos.square_is_empty(to))
509 // Double white pawn push. The destination square must be on the fourth
510 // rank, and both the destination square and the square between the
511 // source and destination squares must be empty.
512 if ( square_rank(to) != RANK_4
513 || !pos.square_is_empty(to)
514 || !pos.square_is_empty(from + DELTA_N))
519 // Double black pawn push. The destination square must be on the fifth
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_5
523 || !pos.square_is_empty(to)
524 || !pos.square_is_empty(from + DELTA_S))
531 // The move is pseudo-legal. Return it if it is legal.
532 return (pos.move_is_legal(m) ? m : MOVE_NONE);
535 // Luckly we can handle all the other pieces in one go
536 return ( pos.piece_attacks_square(from, to)
537 && pos.move_is_legal(m)
538 && !move_promotion(m) ? m : MOVE_NONE);
544 int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
546 Bitboard pawns = pos.pawns(ofs.us);
547 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
551 // Captures in the a1-h8 (a8-h1 for black) direction
552 Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
554 // Capturing promotions
555 Bitboard b2 = b1 & ofs.Rank8BB;
558 sq = pop_1st_bit(&b2);
559 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
562 // Capturing non-promotions
563 b2 = b1 & ~ofs.Rank8BB;
566 sq = pop_1st_bit(&b2);
567 mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
570 // Captures in the h1-a8 (h8-a1 for black) direction
571 b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
573 // Capturing promotions
574 b2 = b1 & ofs.Rank8BB;
577 sq = pop_1st_bit(&b2);
578 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
581 // Capturing non-promotions
582 b2 = b1 & ~ofs.Rank8BB;
585 sq = pop_1st_bit(&b2);
586 mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
589 // Non-capturing promotions
590 b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
593 sq = pop_1st_bit(&b1);
594 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
597 // En passant captures
598 if (pos.ep_square() != SQ_NONE)
600 assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
601 assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
603 b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
604 assert(b1 != EmptyBoardBB);
608 sq = pop_1st_bit(&b1);
609 mlist[n++].move = make_ep_move(sq, pos.ep_square());
616 int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
618 Bitboard pawns = pos.pawns(ofs.us);
619 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
620 Bitboard emptySquares = pos.empty_squares();
625 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
626 b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
629 sq = pop_1st_bit(&b1);
630 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
631 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
632 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
635 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
636 b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
639 sq = pop_1st_bit(&b1);
640 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
641 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
642 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
645 // Single pawn pushes
646 b1 = ofs.forward(pawns) & emptySquares;
647 b2 = b1 & ofs.Rank8BB;
650 sq = pop_1st_bit(&b2);
651 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
652 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
653 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
655 b2 = b1 & ~ofs.Rank8BB;
658 sq = pop_1st_bit(&b2);
659 mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
662 // Double pawn pushes
663 b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
666 sq = pop_1st_bit(&b2);
667 mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
673 int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
674 Color side, Bitboard target) {
676 const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
681 for (int i = 0; i < pos.piece_count(side, piece); i++)
683 from = pos.piece_list(side, piece, i);
684 b = (pos.*mem_fn)(from) & target;
687 to = pop_1st_bit(&b);
688 mlist[n++].move = make_move(from, to);
695 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
699 if (pos.can_castle(us))
701 Color them = opposite_color(us);
702 Square ksq = pos.king_square(us);
704 assert(pos.piece_on(ksq) == king_of_color(us));
706 if (pos.can_castle_kingside(us))
708 Square rsq = pos.initial_kr_square(us);
709 Square g1 = relative_square(us, SQ_G1);
710 Square f1 = relative_square(us, SQ_F1);
712 bool illegal = false;
714 assert(pos.piece_on(rsq) == rook_of_color(us));
716 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
717 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
718 || pos.square_is_attacked(s, them))
721 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
722 if (s != ksq && s != rsq && pos.square_is_occupied(s))
726 mlist[n++].move = make_castle_move(ksq, rsq);
729 if (pos.can_castle_queenside(us))
731 Square rsq = pos.initial_qr_square(us);
732 Square c1 = relative_square(us, SQ_C1);
733 Square d1 = relative_square(us, SQ_D1);
735 bool illegal = false;
737 assert(pos.piece_on(rsq) == rook_of_color(us));
739 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
740 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
741 || pos.square_is_attacked(s, them))
744 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
745 if (s != ksq && s != rsq && pos.square_is_occupied(s))
748 if ( square_file(rsq) == FILE_B
749 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
750 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
754 mlist[n++].move = make_castle_move(ksq, rsq);
760 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
761 Bitboard dc, Square ksq, MoveStack* mlist, int n) {
763 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
766 Bitboard b = target & dc;
769 Square from = pop_1st_bit(&b);
770 Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
773 Square to = pop_1st_bit(&bb);
774 mlist[n++].move = make_move(from, to);
780 Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
783 Square from = pop_1st_bit(&b);
784 Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
787 Square to = pop_1st_bit(&bb);
788 mlist[n++].move = make_move(from, to);
794 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
796 // Pawn moves which give discovered check. This is possible only if the
797 // pawn is not on the same file as the enemy king, because we don't
798 // generate captures.
799 Bitboard empty = pos.empty_squares();
801 // Find all friendly pawns not on the enemy king's file
802 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
804 // Discovered checks, single pawn pushes
805 b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
808 Square to = pop_1st_bit(&b3);
809 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
812 // Discovered checks, double pawn pushes
813 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
816 Square to = pop_1st_bit(&b3);
817 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
820 // Direct checks. These are possible only for pawns on neighboring files
823 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
825 // Direct checks, single pawn pushes
826 b2 = (ofs.forward)(b1) & empty;
827 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
830 Square to = pop_1st_bit(&b3);
831 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
834 // Direct checks, double pawn pushes
835 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
838 Square to = pop_1st_bit(&b3);
839 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
845 int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
846 Bitboard blockSquares, MoveStack* mlist, int n) {
848 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
852 Square from = pop_1st_bit(&b);
853 Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
856 Square to = pop_1st_bit(&bb);
857 mlist[n++].move = make_move(from, to);
864 int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
865 Bitboard blockSquares, MoveStack* mlist, int n) {
866 // Find non-pinned pawns
867 Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
869 // Single pawn pushes. We don't have to AND with empty squares here,
870 // because the blocking squares will always be empty.
871 Bitboard b2 = (ofs.forward)(b1) & blockSquares;
874 Square to = pop_1st_bit(&b2);
876 assert(pos.piece_on(to) == EMPTY);
878 if (square_rank(to) == ofs.RANK_8)
880 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
881 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
882 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
883 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
885 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
888 // Double pawn pushes
889 b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
892 Square to = pop_1st_bit(&b2);
894 assert(pos.piece_on(to) == EMPTY);
895 assert(ofs.us != WHITE || square_rank(to) == RANK_4);
896 assert(ofs.us != BLACK || square_rank(to) == RANK_5);
898 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);