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)) ) & not_pinned;
261 from = pop_1st_bit(&b1);
262 mlist[n++].move = make_move(from, checksq);
265 // Blocking check evasions are possible only if the checking piece is
267 if (checkers & pos.sliders())
269 Bitboard blockSquares = squares_between(checksq, ksq);
271 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
273 // Pawn moves. Because a blocking evasion can never be a capture, we
274 // only generate pawn pushes.
276 n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
278 n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
281 b1 = pos.knights(us) & not_pinned;
283 n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
285 b1 = pos.bishops(us) & not_pinned;
287 n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
289 b1 = pos.rooks(us) & not_pinned;
291 n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
293 b1 = pos.queens(us) & not_pinned;
295 n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
298 // Finally, the ugly special case of en passant captures. An en passant
299 // capture can only be a check evasion if the check is not a discovered
300 // check. If pos.ep_square() is set, the last move made must have been
301 // a double pawn push. If, furthermore, the checking piece is a pawn,
302 // an en passant check evasion may be possible.
303 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
305 to = pos.ep_square();
306 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
308 assert(b1 != EmptyBoardBB);
313 from = pop_1st_bit(&b1);
315 // Before generating the move, we have to make sure it is legal.
316 // This is somewhat tricky, because the two disappearing pawns may
317 // cause new "discovered checks". We test this by removing the
318 // two relevant bits from the occupied squares bitboard, and using
319 // the low-level bitboard functions for bishop and rook attacks.
320 b2 = pos.occupied_squares();
321 clear_bit(&b2, from);
322 clear_bit(&b2, checksq);
323 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
324 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
326 mlist[n++].move = make_ep_move(from, to);
334 /// generate_legal_moves() computes a complete list of legal moves in the
335 /// current position. This function is not very fast, and should be used
336 /// only in situations where performance is unimportant. It wouldn't be
337 /// very hard to write an efficient legal move generator, but for the moment
338 /// we don't need it.
340 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
345 return generate_evasions(pos, mlist);
347 // Generate pseudo-legal moves:
348 int n = generate_captures(pos, mlist);
349 n += generate_noncaptures(pos, mlist + n);
351 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
353 // Remove illegal moves from the list:
354 for (int i = 0; i < n; i++)
355 if (!pos.move_is_legal(mlist[i].move, pinned))
356 mlist[i--].move = mlist[--n].move;
362 /// generate_move_if_legal() takes a position and a (not necessarily
363 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
364 /// whether the move is legal. If the move is legal, the move itself is
365 /// returned. If not, the function returns MOVE_NONE. This function must
366 /// only be used when the side to move is not in check.
368 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
371 assert(!pos.is_check());
372 assert(move_is_ok(m));
374 Color us = pos.side_to_move();
375 Color them = opposite_color(us);
376 Square from = move_from(m);
377 Piece pc = pos.piece_on(from);
379 // If the from square is not occupied by a piece belonging to the side to
380 // move, the move is obviously not legal.
381 if (color_of_piece(pc) != us)
384 Square to = move_to(m);
389 // The piece must be a pawn and destination square must be the
390 // en passant square.
391 if ( type_of_piece(pc) != PAWN
392 || to != pos.ep_square())
395 assert(pos.square_is_empty(to));
396 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
398 // The move is pseudo-legal. If it is legal, return it.
399 return (pos.move_is_legal(m) ? m : MOVE_NONE);
403 if (move_is_short_castle(m))
405 // The piece must be a king and side to move must still have
406 // the right to castle kingside.
407 if ( type_of_piece(pc) != KING
408 ||!pos.can_castle_kingside(us))
411 assert(from == pos.king_square(us));
412 assert(to == pos.initial_kr_square(us));
413 assert(pos.piece_on(to) == rook_of_color(us));
415 Square g1 = relative_square(us, SQ_G1);
416 Square f1 = relative_square(us, SQ_F1);
418 bool illegal = false;
420 // Check if any of the squares between king and rook
421 // is occupied or under attack.
422 for (s = Min(from, g1); s <= Max(from, g1); s++)
423 if ( (s != from && s != to && !pos.square_is_empty(s))
424 || pos.square_is_attacked(s, them))
427 // Check if any of the squares between king and rook
429 for (s = Min(to, f1); s <= Max(to, f1); s++)
430 if (s != from && s != to && !pos.square_is_empty(s))
433 return (!illegal ? m : MOVE_NONE);
436 if (move_is_long_castle(m))
438 // The piece must be a king and side to move must still have
439 // the right to castle kingside.
440 if ( type_of_piece(pc) != KING
441 ||!pos.can_castle_queenside(us))
444 assert(from == pos.king_square(us));
445 assert(to == pos.initial_qr_square(us));
446 assert(pos.piece_on(to) == rook_of_color(us));
448 Square c1 = relative_square(us, SQ_C1);
449 Square d1 = relative_square(us, SQ_D1);
451 bool illegal = false;
453 for (s = Min(from, c1); s <= Max(from, c1); s++)
454 if( (s != from && s != to && !pos.square_is_empty(s))
455 || pos.square_is_attacked(s, them))
458 for (s = Min(to, d1); s <= Max(to, d1); s++)
459 if(s != from && s != to && !pos.square_is_empty(s))
462 if ( square_file(to) == FILE_B
463 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
464 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
467 return (!illegal ? m : MOVE_NONE);
472 // The destination square cannot be occupied by a friendly piece
473 if (pos.color_of_piece_on(to) == us)
476 // Proceed according to the type of the moving piece.
477 if (type_of_piece(pc) == PAWN)
479 // If the destination square is on the 8/1th rank, the move must
481 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
482 ||(square_rank(to) == RANK_1 && us != WHITE))
483 && !move_promotion(m))
486 // Proceed according to the square delta between the source and
487 // destionation squares.
494 // Capture. The destination square must be occupied by an enemy
495 // piece (en passant captures was handled earlier).
496 if (pos.color_of_piece_on(to) != them)
502 // Pawn push. The destination square must be empty.
503 if (!pos.square_is_empty(to))
508 // Double white pawn push. The destination square must be on the fourth
509 // rank, and both the destination square and the square between the
510 // source and destination squares must be empty.
511 if ( square_rank(to) != RANK_4
512 || !pos.square_is_empty(to)
513 || !pos.square_is_empty(from + DELTA_N))
518 // Double black pawn push. The destination square must be on the fifth
519 // rank, and both the destination square and the square between the
520 // source and destination squares must be empty.
521 if ( square_rank(to) != RANK_5
522 || !pos.square_is_empty(to)
523 || !pos.square_is_empty(from + DELTA_S))
530 // The move is pseudo-legal. Return it if it is legal.
531 return (pos.move_is_legal(m) ? m : MOVE_NONE);
534 // Luckly we can handle all the other pieces in one go
535 return ( pos.piece_attacks_square(from, to)
536 && pos.move_is_legal(m)
537 && !move_promotion(m) ? m : MOVE_NONE);
543 int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
545 Bitboard pawns = pos.pawns(ofs.us);
546 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
550 // Captures in the a1-h8 (a8-h1 for black) direction
551 Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
553 // Capturing promotions
554 Bitboard b2 = b1 & ofs.Rank8BB;
557 sq = pop_1st_bit(&b2);
558 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
561 // Capturing non-promotions
562 b2 = b1 & ~ofs.Rank8BB;
565 sq = pop_1st_bit(&b2);
566 mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
569 // Captures in the h1-a8 (h8-a1 for black) direction
570 b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
572 // Capturing promotions
573 b2 = b1 & ofs.Rank8BB;
576 sq = pop_1st_bit(&b2);
577 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
580 // Capturing non-promotions
581 b2 = b1 & ~ofs.Rank8BB;
584 sq = pop_1st_bit(&b2);
585 mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
588 // Non-capturing promotions
589 b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
592 sq = pop_1st_bit(&b1);
593 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
596 // En passant captures
597 if (pos.ep_square() != SQ_NONE)
599 assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
600 assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
602 b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
603 assert(b1 != EmptyBoardBB);
607 sq = pop_1st_bit(&b1);
608 mlist[n++].move = make_ep_move(sq, pos.ep_square());
615 int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
617 Bitboard pawns = pos.pawns(ofs.us);
618 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
619 Bitboard emptySquares = pos.empty_squares();
624 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
625 b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
628 sq = pop_1st_bit(&b1);
629 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
630 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
631 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
634 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
635 b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
638 sq = pop_1st_bit(&b1);
639 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
640 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
641 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
644 // Single pawn pushes
645 b1 = ofs.forward(pawns) & emptySquares;
646 b2 = b1 & ofs.Rank8BB;
649 sq = pop_1st_bit(&b2);
650 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
651 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
652 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
654 b2 = b1 & ~ofs.Rank8BB;
657 sq = pop_1st_bit(&b2);
658 mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
661 // Double pawn pushes
662 b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
665 sq = pop_1st_bit(&b2);
666 mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
672 int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
673 Color side, Bitboard target) {
675 const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
680 for (int i = 0; i < pos.piece_count(side, piece); i++)
682 from = pos.piece_list(side, piece, i);
683 b = (pos.*mem_fn)(from) & target;
686 to = pop_1st_bit(&b);
687 mlist[n++].move = make_move(from, to);
694 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
698 if (pos.can_castle(us))
700 Color them = opposite_color(us);
701 Square ksq = pos.king_square(us);
703 assert(pos.piece_on(ksq) == king_of_color(us));
705 if (pos.can_castle_kingside(us))
707 Square rsq = pos.initial_kr_square(us);
708 Square g1 = relative_square(us, SQ_G1);
709 Square f1 = relative_square(us, SQ_F1);
711 bool illegal = false;
713 assert(pos.piece_on(rsq) == rook_of_color(us));
715 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
716 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
717 || pos.square_is_attacked(s, them))
720 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
721 if (s != ksq && s != rsq && pos.square_is_occupied(s))
725 mlist[n++].move = make_castle_move(ksq, rsq);
728 if (pos.can_castle_queenside(us))
730 Square rsq = pos.initial_qr_square(us);
731 Square c1 = relative_square(us, SQ_C1);
732 Square d1 = relative_square(us, SQ_D1);
734 bool illegal = false;
736 assert(pos.piece_on(rsq) == rook_of_color(us));
738 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
739 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
740 || pos.square_is_attacked(s, them))
743 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
744 if (s != ksq && s != rsq && pos.square_is_occupied(s))
747 if ( square_file(rsq) == FILE_B
748 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
749 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
753 mlist[n++].move = make_castle_move(ksq, rsq);
759 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
760 Bitboard dc, Square ksq, MoveStack* mlist, int n) {
762 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
765 Bitboard b = target & dc;
768 Square from = pop_1st_bit(&b);
769 Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
772 Square to = pop_1st_bit(&bb);
773 mlist[n++].move = make_move(from, to);
779 Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
782 Square from = pop_1st_bit(&b);
783 Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
786 Square to = pop_1st_bit(&bb);
787 mlist[n++].move = make_move(from, to);
793 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
795 // Pawn moves which give discovered check. This is possible only if the
796 // pawn is not on the same file as the enemy king, because we don't
797 // generate captures.
798 Bitboard empty = pos.empty_squares();
800 // Find all friendly pawns not on the enemy king's file
801 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
803 // Discovered checks, single pawn pushes
804 b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
807 Square to = pop_1st_bit(&b3);
808 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
811 // Discovered checks, double pawn pushes
812 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
815 Square to = pop_1st_bit(&b3);
816 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
819 // Direct checks. These are possible only for pawns on neighboring files
822 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
824 // Direct checks, single pawn pushes
825 b2 = (ofs.forward)(b1) & empty;
826 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
829 Square to = pop_1st_bit(&b3);
830 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
833 // Direct checks, double pawn pushes
834 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
837 Square to = pop_1st_bit(&b3);
838 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
844 int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
845 Bitboard blockSquares, MoveStack* mlist, int n) {
847 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
851 Square from = pop_1st_bit(&b);
852 Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
855 Square to = pop_1st_bit(&bb);
856 mlist[n++].move = make_move(from, to);
863 int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
864 Bitboard blockSquares, MoveStack* mlist, int n) {
865 // Find non-pinned pawns
866 Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
868 // Single pawn pushes. We don't have to AND with empty squares here,
869 // because the blocking squares will always be empty.
870 Bitboard b2 = (ofs.forward)(b1) & blockSquares;
873 Square to = pop_1st_bit(&b2);
875 assert(pos.piece_on(to) == EMPTY);
877 if (square_rank(to) == ofs.RANK_8)
879 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
880 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
881 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
882 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
884 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
887 // Double pawn pushes
888 b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
891 Square to = pop_1st_bit(&b2);
893 assert(pos.piece_on(to) == EMPTY);
894 assert(ofs.us != WHITE || square_rank(to) == RANK_4);
895 assert(ofs.us != BLACK || square_rank(to) == RANK_5);
897 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);