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 int generate_white_pawn_captures(const Position&, MoveStack*);
36 int generate_black_pawn_captures(const Position&, MoveStack*);
37 int generate_white_pawn_noncaptures(const Position&, MoveStack*);
38 int generate_black_pawn_noncaptures(const Position&, MoveStack*);
39 int generate_piece_moves(PieceType, const Position&, MoveStack*, Color side, Bitboard t);
40 int generate_castle_moves(const Position&, MoveStack*, Color us);
42 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
43 Bitboard dc, Square ksq, MoveStack* mlist, int n);
45 inline Bitboard next_row_white(Bitboard b) { return b << 8; }
46 inline Bitboard next_row_black(Bitboard b) { return b >> 8; }
54 Bitboard (*next_row_fn)(Bitboard b);
56 const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, DELTA_N, BLACK, &next_row_white };
57 const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, DELTA_S, WHITE, &next_row_black };
59 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc,
60 Square ksq, MoveStack* mlist, int n);
69 /// generate_captures generates() all pseudo-legal captures and queen
70 /// promotions. The return value is the number of moves generated.
72 int generate_captures(const Position& pos, MoveStack* mlist) {
75 assert(!pos.is_check());
77 Color us = pos.side_to_move();
78 Bitboard target = pos.pieces_of_color(opposite_color(us));
82 n = generate_white_pawn_captures(pos, mlist);
84 n = generate_black_pawn_captures(pos, mlist);
86 for (PieceType pce = KNIGHT; pce <= KING; pce++)
87 n += generate_piece_moves(pce, pos, mlist+n, us, target);
93 /// generate_noncaptures() generates all pseudo-legal non-captures and
94 /// underpromotions. The return value is the number of moves generated.
96 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
99 assert(!pos.is_check());
101 Color us = pos.side_to_move();
102 Bitboard target = pos.empty_squares();
106 n = generate_white_pawn_noncaptures(pos, mlist);
108 n = generate_black_pawn_noncaptures(pos, mlist);
110 for (PieceType pce = KNIGHT; pce <= KING; pce++)
111 n += generate_piece_moves(pce, pos, mlist+n, us, target);
113 n += generate_castle_moves(pos, mlist+n, us);
118 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
119 /// checks, except castling moves (will add this later). It returns the
120 /// number of generated moves.
122 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
125 assert(!pos.is_check());
127 Color us = pos.side_to_move();
128 Square ksq = pos.king_square(opposite_color(us));
131 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
133 dc = pos.discovered_check_candidates(us);
137 n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
139 n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
142 Bitboard b = pos.knights(us);
144 n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n);
148 n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n);
152 n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n);
156 n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n);
159 Square from = pos.king_square(us);
160 if (bit_is_set(dc, from))
162 b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
165 Square to = pop_1st_bit(&b);
166 mlist[n++].move = make_move(from, to);
170 // TODO: Castling moves!
176 /// generate_evasions() generates all check evasions when the side to move is
177 /// in check. Unlike the other move generation functions, this one generates
178 /// only legal moves. It returns the number of generated moves. This
179 /// function is very ugly, and needs cleaning up some time later. FIXME
181 int generate_evasions(const Position &pos, MoveStack *mlist) {
184 assert(pos.is_check());
187 Bitboard checkers = pos.checkers();
188 Bitboard pinned, b1, b2;
189 Square ksq, from, to;
192 us = pos.side_to_move();
193 them = opposite_color(us);
195 ksq = pos.king_square(us);
196 assert(pos.piece_on(ksq) == king_of_color(us));
198 // Generate evasions for king:
199 b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
200 b2 = pos.occupied_squares();
203 to = pop_1st_bit(&b1);
205 // Make sure to is not attacked by the other side. This is a bit ugly,
206 // because we can't use Position::square_is_attacked. Instead we use
207 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
208 // b2 (the occupied squares with the king removed) in order to test whether
209 // the king will remain in check on the destination square.
210 if(((pos.pawn_attacks(us, to) & pos.pawns(them)) == EmptyBoardBB) &&
211 ((pos.knight_attacks(to) & pos.knights(them)) == EmptyBoardBB) &&
212 ((pos.king_attacks(to) & pos.kings(them)) == EmptyBoardBB) &&
213 ((bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
215 ((rook_attacks_bb(to, b2) & pos.rooks_and_queens(them)) == EmptyBoardBB))
216 mlist[n++].move = make_move(ksq, to);
220 // Generate evasions for other pieces only if not double check. We use a
221 // simple bit twiddling hack here rather than calling count_1s in order to
222 // save some time (we know that pos.checkers() has at most two nonzero bits).
223 if(!(checkers & (checkers - 1))) {
224 Square checksq = first_1(checkers);
225 assert(pos.color_of_piece_on(checksq) == them);
227 // Find pinned pieces:
228 pinned = pos.pinned_pieces(us);
230 // Generate captures of the checking piece:
233 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
235 from = pop_1st_bit(&b1);
236 if(relative_rank(us, checksq) == RANK_8) {
237 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
238 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
239 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
240 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
243 mlist[n++].move = make_move(from, checksq);
247 b1 = pos.knight_attacks(checksq) & pos.knights(us) & ~pinned;
249 from = pop_1st_bit(&b1);
250 mlist[n++].move = make_move(from, checksq);
253 // Bishop and queen captures:
254 b1 = pos.bishop_attacks(checksq) & pos.bishops_and_queens(us)
257 from = pop_1st_bit(&b1);
258 mlist[n++].move = make_move(from, checksq);
261 // Rook and queen captures:
262 b1 = pos.rook_attacks(checksq) & pos.rooks_and_queens(us)
265 from = pop_1st_bit(&b1);
266 mlist[n++].move = make_move(from, checksq);
269 // Blocking check evasions are possible only if the checking piece is
271 if(checkers & pos.sliders()) {
272 Bitboard blockSquares = squares_between(checksq, ksq);
273 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
275 // Pawn moves. Because a blocking evasion can never be a capture, we
276 // only generate pawn pushes. As so often, the code for pawns is a bit
277 // ugly, and uses separate clauses for white and black pawns. :-(
279 // Find non-pinned pawns:
280 b1 = pos.pawns(WHITE) & ~pinned;
282 // Single pawn pushes. We don't have to AND with empty squares here,
283 // because the blocking squares will always be empty.
284 b2 = (b1 << 8) & blockSquares;
286 to = pop_1st_bit(&b2);
287 assert(pos.piece_on(to) == EMPTY);
288 if(square_rank(to) == RANK_8) {
289 mlist[n++].move = make_promotion_move(to - DELTA_N, to, QUEEN);
290 mlist[n++].move = make_promotion_move(to - DELTA_N, to, ROOK);
291 mlist[n++].move = make_promotion_move(to - DELTA_N, to, BISHOP);
292 mlist[n++].move = make_promotion_move(to - DELTA_N, to, KNIGHT);
295 mlist[n++].move = make_move(to - DELTA_N, to);
297 // Double pawn pushes.
298 b2 = (((b1 << 8) & pos.empty_squares() & Rank3BB) << 8) & blockSquares;
300 to = pop_1st_bit(&b2);
301 assert(pos.piece_on(to) == EMPTY);
302 assert(square_rank(to) == RANK_4);
303 mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
306 else { // (us == BLACK)
307 // Find non-pinned pawns:
308 b1 = pos.pawns(BLACK) & ~pinned;
310 // Single pawn pushes. We don't have to AND with empty squares here,
311 // because the blocking squares will always be empty.
312 b2 = (b1 >> 8) & blockSquares;
314 to = pop_1st_bit(&b2);
315 assert(pos.piece_on(to) == EMPTY);
316 if(square_rank(to) == RANK_1) {
317 mlist[n++].move = make_promotion_move(to - DELTA_S, to, QUEEN);
318 mlist[n++].move = make_promotion_move(to - DELTA_S, to, ROOK);
319 mlist[n++].move = make_promotion_move(to - DELTA_S, to, BISHOP);
320 mlist[n++].move = make_promotion_move(to - DELTA_S, to, KNIGHT);
323 mlist[n++].move = make_move(to - DELTA_S, to);
325 // Double pawn pushes.
326 b2 = (((b1 >> 8) & pos.empty_squares() & Rank6BB) >> 8) & blockSquares;
328 to = pop_1st_bit(&b2);
329 assert(pos.piece_on(to) == EMPTY);
330 assert(square_rank(to) == RANK_5);
331 mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
336 b1 = pos.knights(us) & ~pinned;
338 from = pop_1st_bit(&b1);
339 b2 = pos.knight_attacks(from) & blockSquares;
341 to = pop_1st_bit(&b2);
342 mlist[n++].move = make_move(from, to);
347 b1 = pos.bishops(us) & ~pinned;
349 from = pop_1st_bit(&b1);
350 b2 = pos.bishop_attacks(from) & blockSquares;
352 to = pop_1st_bit(&b2);
353 mlist[n++].move = make_move(from, to);
358 b1 = pos.rooks(us) & ~pinned;
360 from = pop_1st_bit(&b1);
361 b2 = pos.rook_attacks(from) & blockSquares;
363 to = pop_1st_bit(&b2);
364 mlist[n++].move = make_move(from, to);
369 b1 = pos.queens(us) & ~pinned;
371 from = pop_1st_bit(&b1);
372 b2 = pos.queen_attacks(from) & blockSquares;
374 to = pop_1st_bit(&b2);
375 mlist[n++].move = make_move(from, to);
380 // Finally, the ugly special case of en passant captures. An en passant
381 // capture can only be a check evasion if the check is not a discovered
382 // check. If pos.ep_square() is set, the last move made must have been
383 // a double pawn push. If, furthermore, the checking piece is a pawn,
384 // an en passant check evasion may be possible.
385 if(pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them))) {
386 to = pos.ep_square();
387 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
388 assert(b1 != EmptyBoardBB);
391 from = pop_1st_bit(&b1);
393 // Before generating the move, we have to make sure it is legal.
394 // This is somewhat tricky, because the two disappearing pawns may
395 // cause new "discovered checks". We test this by removing the
396 // two relevant bits from the occupied squares bitboard, and using
397 // the low-level bitboard functions for bishop and rook attacks.
398 b2 = pos.occupied_squares();
399 clear_bit(&b2, from);
400 clear_bit(&b2, checksq);
401 if(((bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
403 ((rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))
405 mlist[n++].move = make_ep_move(from, to);
414 /// generate_legal_moves() computes a complete list of legal moves in the
415 /// current position. This function is not very fast, and should be used
416 /// only in situations where performance is unimportant. It wouldn't be
417 /// very hard to write an efficient legal move generator, but for the moment
418 /// we don't need it.
420 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
425 return generate_evasions(pos, mlist);
427 // Generate pseudo-legal moves:
428 int n = generate_captures(pos, mlist);
429 n += generate_noncaptures(pos, mlist + n);
431 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
433 // Remove illegal moves from the list:
434 for (int i = 0; i < n; i++)
435 if (!pos.move_is_legal(mlist[i].move, pinned))
436 mlist[i--].move = mlist[--n].move;
442 /// generate_move_if_legal() takes a position and a (not necessarily
443 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
444 /// whether the move is legal. If the move is legal, the move itself is
445 /// returned. If not, the function returns MOVE_NONE. This function must
446 /// only be used when the side to move is not in check.
448 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
451 assert(!pos.is_check());
452 assert(move_is_ok(m));
454 Color us = pos.side_to_move();
455 Color them = opposite_color(us);
456 Square from = move_from(m);
457 Piece pc = pos.piece_on(from);
459 // If the from square is not occupied by a piece belonging to the side to
460 // move, the move is obviously not legal.
461 if (color_of_piece(pc) != us)
464 Square to = move_to(m);
469 // The piece must be a pawn and destination square must be the
470 // en passant square.
471 if ( type_of_piece(pc) != PAWN
472 || to != pos.ep_square())
475 assert(pos.square_is_empty(to));
476 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
478 // The move is pseudo-legal. If it is legal, return it.
479 return (pos.move_is_legal(m) ? m : MOVE_NONE);
483 if (move_is_short_castle(m))
485 // The piece must be a king and side to move must still have
486 // the right to castle kingside.
487 if ( type_of_piece(pc) != KING
488 ||!pos.can_castle_kingside(us))
491 assert(from == pos.king_square(us));
492 assert(to == pos.initial_kr_square(us));
493 assert(pos.piece_on(to) == rook_of_color(us));
495 Square g1 = relative_square(us, SQ_G1);
496 Square f1 = relative_square(us, SQ_F1);
498 bool illegal = false;
500 // Check if any of the squares between king and rook
501 // is occupied or under attack.
502 for (s = Min(from, g1); s <= Max(from, g1); s++)
503 if ( (s != from && s != to && !pos.square_is_empty(s))
504 || pos.square_is_attacked(s, them))
507 // Check if any of the squares between king and rook
509 for (s = Min(to, f1); s <= Max(to, f1); s++)
510 if (s != from && s != to && !pos.square_is_empty(s))
513 return (!illegal ? m : MOVE_NONE);
516 if (move_is_long_castle(m))
518 // The piece must be a king and side to move must still have
519 // the right to castle kingside.
520 if ( type_of_piece(pc) != KING
521 ||!pos.can_castle_queenside(us))
524 assert(from == pos.king_square(us));
525 assert(to == pos.initial_qr_square(us));
526 assert(pos.piece_on(to) == rook_of_color(us));
528 Square c1 = relative_square(us, SQ_C1);
529 Square d1 = relative_square(us, SQ_D1);
531 bool illegal = false;
533 for (s = Min(from, c1); s <= Max(from, c1); s++)
534 if( (s != from && s != to && !pos.square_is_empty(s))
535 || pos.square_is_attacked(s, them))
538 for (s = Min(to, d1); s <= Max(to, d1); s++)
539 if(s != from && s != to && !pos.square_is_empty(s))
542 if ( square_file(to) == FILE_B
543 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
544 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
547 return (!illegal ? m : MOVE_NONE);
552 // The destination square cannot be occupied by a friendly piece
553 if (pos.color_of_piece_on(to) == us)
556 // Proceed according to the type of the moving piece.
557 if (type_of_piece(pc) == PAWN)
559 // If the destination square is on the 8/1th rank, the move must
561 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
562 ||(square_rank(to) == RANK_1 && us != WHITE))
563 && !move_promotion(m))
566 // Proceed according to the square delta between the source and
567 // destionation squares.
574 // Capture. The destination square must be occupied by an enemy
575 // piece (en passant captures was handled earlier).
576 if (pos.color_of_piece_on(to) != them)
582 // Pawn push. The destination square must be empty.
583 if (!pos.square_is_empty(to))
588 // Double white pawn push. The destination square must be on the fourth
589 // rank, and both the destination square and the square between the
590 // source and destination squares must be empty.
591 if ( square_rank(to) != RANK_4
592 || !pos.square_is_empty(to)
593 || !pos.square_is_empty(from + DELTA_N))
598 // Double black pawn push. The destination square must be on the fifth
599 // rank, and both the destination square and the square between the
600 // source and destination squares must be empty.
601 if ( square_rank(to) != RANK_5
602 || !pos.square_is_empty(to)
603 || !pos.square_is_empty(from + DELTA_S))
610 // The move is pseudo-legal. Return it if it is legal.
611 return (pos.move_is_legal(m) ? m : MOVE_NONE);
614 // Luckly we can handle all the other pieces in one go
615 return ( pos.piece_attacks_square(from, to)
616 && pos.move_is_legal(m)
617 && !move_promotion(m) ? m : MOVE_NONE);
623 int generate_white_pawn_captures(const Position &pos, MoveStack *mlist) {
625 Bitboard pawns = pos.pawns(WHITE);
626 Bitboard enemyPieces = pos.pieces_of_color(BLACK);
630 // Captures in the a1-h8 direction
631 Bitboard b1 = (pawns << 9) & ~FileABB & enemyPieces;
633 // Capturing promotions
634 Bitboard b2 = b1 & Rank8BB;
637 sq = pop_1st_bit(&b2);
638 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, QUEEN);
641 // Capturing non-promotions
645 sq = pop_1st_bit(&b2);
646 mlist[n++].move = make_move(sq - DELTA_NE, sq);
649 // Captures in the h1-a8 direction
650 b1 = (pawns << 7) & ~FileHBB & enemyPieces;
652 // Capturing promotions
656 sq = pop_1st_bit(&b2);
657 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, QUEEN);
660 // Capturing non-promotions
664 sq = pop_1st_bit(&b2);
665 mlist[n++].move = make_move(sq - DELTA_NW, sq);
668 // Non-capturing promotions
669 b1 = (pawns << 8) & pos.empty_squares() & Rank8BB;
672 sq = pop_1st_bit(&b1);
673 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, QUEEN);
676 // En passant captures
677 if (pos.ep_square() != SQ_NONE)
679 assert(square_rank(pos.ep_square()) == RANK_6);
680 b1 = pawns & pos.black_pawn_attacks(pos.ep_square());
681 assert(b1 != EmptyBoardBB);
684 sq = pop_1st_bit(&b1);
685 mlist[n++].move = make_ep_move(sq, pos.ep_square());
692 int generate_black_pawn_captures(const Position &pos, MoveStack *mlist) {
694 Bitboard pawns = pos.pawns(BLACK);
695 Bitboard enemyPieces = pos.pieces_of_color(WHITE);
699 // Captures in the a8-h1 direction
700 Bitboard b1 = (pawns >> 7) & ~FileABB & enemyPieces;
702 // Capturing promotions
703 Bitboard b2 = b1 & Rank1BB;
706 sq = pop_1st_bit(&b2);
707 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, QUEEN);
710 // Capturing non-promotions
714 sq = pop_1st_bit(&b2);
715 mlist[n++].move = make_move(sq - DELTA_SE, sq);
718 // Captures in the h8-a1 direction
719 b1 = (pawns >> 9) & ~FileHBB & enemyPieces;
721 // Capturing promotions
725 sq = pop_1st_bit(&b2);
726 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, QUEEN);
729 // Capturing Non-promotions
733 sq = pop_1st_bit(&b2);
734 mlist[n++].move = make_move(sq - DELTA_SW, sq);
737 // Non-capturing promotions
738 b1 = (pawns >> 8) & pos.empty_squares() & Rank1BB;
741 sq = pop_1st_bit(&b1);
742 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, QUEEN);
745 // En passant captures
746 if (pos.ep_square() != SQ_NONE)
748 assert(square_rank(pos.ep_square()) == RANK_3);
749 b1 = pawns & pos.white_pawn_attacks(pos.ep_square());
750 assert(b1 != EmptyBoardBB);
753 sq = pop_1st_bit(&b1);
754 mlist[n++].move = make_ep_move(sq, pos.ep_square());
761 int generate_white_pawn_noncaptures(const Position &pos, MoveStack *mlist) {
763 Bitboard pawns = pos.pawns(WHITE);
764 Bitboard enemyPieces = pos.pieces_of_color(BLACK);
765 Bitboard emptySquares = pos.empty_squares();
770 // Underpromotion captures in the a1-h8 direction:
771 b1 = (pawns << 9) & ~FileABB & enemyPieces & Rank8BB;
773 sq = pop_1st_bit(&b1);
774 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, ROOK);
775 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, BISHOP);
776 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, KNIGHT);
779 // Underpromotion captures in the h1-a8 direction:
780 b1 = (pawns << 7) & ~FileHBB & enemyPieces & Rank8BB;
782 sq = pop_1st_bit(&b1);
783 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, ROOK);
784 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, BISHOP);
785 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, KNIGHT);
788 // Single pawn pushes:
789 b1 = (pawns << 8) & emptySquares;
792 sq = pop_1st_bit(&b2);
793 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, ROOK);
794 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, BISHOP);
795 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, KNIGHT);
799 sq = pop_1st_bit(&b2);
800 mlist[n++].move = make_move(sq - DELTA_N, sq);
803 // Double pawn pushes:
804 b2 = ((b1 & Rank3BB) << 8) & emptySquares;
806 sq = pop_1st_bit(&b2);
807 mlist[n++].move = make_move(sq - DELTA_N - DELTA_N, sq);
814 int generate_black_pawn_noncaptures(const Position &pos, MoveStack *mlist) {
815 Bitboard pawns = pos.pawns(BLACK);
816 Bitboard enemyPieces = pos.pieces_of_color(WHITE);
817 Bitboard emptySquares = pos.empty_squares();
822 // Underpromotion captures in the a8-h1 direction:
823 b1 = (pawns >> 7) & ~FileABB & enemyPieces & Rank1BB;
825 sq = pop_1st_bit(&b1);
826 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, ROOK);
827 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, BISHOP);
828 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, KNIGHT);
831 // Underpromotion captures in the h8-a1 direction:
832 b1 = (pawns >> 9) & ~FileHBB & enemyPieces & Rank1BB;
834 sq = pop_1st_bit(&b1);
835 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, ROOK);
836 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, BISHOP);
837 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, KNIGHT);
840 // Single pawn pushes:
841 b1 = (pawns >> 8) & emptySquares;
844 sq = pop_1st_bit(&b2);
845 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, ROOK);
846 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, BISHOP);
847 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, KNIGHT);
851 sq = pop_1st_bit(&b2);
852 mlist[n++].move = make_move(sq - DELTA_S, sq);
855 // Double pawn pushes:
856 b2 = ((b1 & Rank6BB) >> 8) & emptySquares;
858 sq = pop_1st_bit(&b2);
859 mlist[n++].move = make_move(sq - DELTA_S - DELTA_S, sq);
866 int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
867 Color side, Bitboard target) {
869 const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
874 for (int i = 0; i < pos.piece_count(side, piece); i++)
876 from = pos.piece_list(side, piece, i);
877 b = (pos.*mem_fn)(from) & target;
880 to = pop_1st_bit(&b);
881 mlist[n++].move = make_move(from, to);
888 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
892 if (pos.can_castle(us))
894 Color them = opposite_color(us);
895 Square ksq = pos.king_square(us);
897 assert(pos.piece_on(ksq) == king_of_color(us));
899 if (pos.can_castle_kingside(us))
901 Square rsq = pos.initial_kr_square(us);
902 Square g1 = relative_square(us, SQ_G1);
903 Square f1 = relative_square(us, SQ_F1);
905 bool illegal = false;
907 assert(pos.piece_on(rsq) == rook_of_color(us));
909 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
910 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
911 || pos.square_is_attacked(s, them))
914 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
915 if (s != ksq && s != rsq && pos.square_is_occupied(s))
919 mlist[n++].move = make_castle_move(ksq, rsq);
922 if (pos.can_castle_queenside(us))
924 Square rsq = pos.initial_qr_square(us);
925 Square c1 = relative_square(us, SQ_C1);
926 Square d1 = relative_square(us, SQ_D1);
928 bool illegal = false;
930 assert(pos.piece_on(rsq) == rook_of_color(us));
932 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
933 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
934 || pos.square_is_attacked(s, them))
937 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
938 if (s != ksq && s != rsq && pos.square_is_occupied(s))
941 if ( square_file(rsq) == FILE_B
942 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
943 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
947 mlist[n++].move = make_castle_move(ksq, rsq);
953 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
954 Bitboard dc, Square ksq, MoveStack* mlist, int n) {
956 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
959 Bitboard b = target & dc;
962 Square from = pop_1st_bit(&b);
963 Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
966 Square to = pop_1st_bit(&bb);
967 mlist[n++].move = make_move(from, to);
973 Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
976 Square from = pop_1st_bit(&b);
977 Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
980 Square to = pop_1st_bit(&bb);
981 mlist[n++].move = make_move(from, to);
987 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
989 // Pawn moves which give discovered check. This is possible only if the
990 // pawn is not on the same file as the enemy king, because we don't
991 // generate captures.
992 Bitboard empty = pos.empty_squares();
994 // Find all friendly pawns not on the enemy king's file
995 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
997 // Discovered checks, single pawn pushes
998 b2 = b3 = (ofs.next_row_fn)(b1 & dc) & ~ofs.Rank8BB & empty;
1001 Square to = pop_1st_bit(&b3);
1002 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
1005 // Discovered checks, double pawn pushes
1006 b3 = (ofs.next_row_fn)(b2 & ofs.Rank3BB) & empty;
1009 Square to = pop_1st_bit(&b3);
1010 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
1013 // Direct checks. These are possible only for pawns on neighboring files
1014 // of the enemy king
1016 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
1018 // Direct checks, single pawn pushes
1019 b2 = (ofs.next_row_fn)(b1) & empty;
1020 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
1023 Square to = pop_1st_bit(&b3);
1024 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
1027 // Direct checks, double pawn pushes
1028 b3 = (ofs.next_row_fn)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
1031 Square to = pop_1st_bit(&b3);
1032 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);