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);
52 /// generate_captures generates() all pseudo-legal captures and queen
53 /// promotions. The return value is the number of moves generated.
55 int generate_captures(const Position& pos, MoveStack* mlist) {
58 assert(!pos.is_check());
60 Color us = pos.side_to_move();
61 Bitboard target = pos.pieces_of_color(opposite_color(us));
65 n = generate_white_pawn_captures(pos, mlist);
67 n = generate_black_pawn_captures(pos, mlist);
69 for (PieceType pce = KNIGHT; pce <= KING; pce++)
70 n += generate_piece_moves(pce, pos, mlist+n, us, target);
76 /// generate_noncaptures() generates all pseudo-legal non-captures and
77 /// underpromotions. The return value is the number of moves generated.
79 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
82 assert(!pos.is_check());
84 Color us = pos.side_to_move();
85 Bitboard target = pos.empty_squares();
89 n = generate_white_pawn_noncaptures(pos, mlist);
91 n = generate_black_pawn_noncaptures(pos, mlist);
93 for (PieceType pce = KNIGHT; pce <= KING; pce++)
94 n += generate_piece_moves(pce, pos, mlist+n, us, target);
96 n += generate_castle_moves(pos, mlist+n, us);
101 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
102 /// checks, except castling moves (will add this later). It returns the
103 /// number of generated moves.
105 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
108 assert(!pos.is_check());
111 Square ksq, from, to;
112 Bitboard empty, b1, b2, b3;
115 us = pos.side_to_move();
116 them = opposite_color(us);
118 ksq = pos.king_square(them);
119 assert(pos.piece_on(ksq) == king_of_color(them));
121 dc = pos.discovered_check_candidates(us);
122 empty = pos.empty_squares();
124 // Pawn moves. This is somewhat messy, and we use separate code for white
125 // and black, because we can't shift by negative numbers in C/C++. :-(
129 // Pawn moves which give discovered check. This is possible only if the
130 // pawn is not on the same file as the enemy king, because we don't
131 // generate captures.
133 // Find all friendly pawns not on the enemy king's file
134 b1 = pos.pawns(us) & ~file_bb(ksq);
136 // Discovered checks, single pawn pushes
137 b2 = b3 = ((b1 & dc) << 8) & ~Rank8BB & empty;
140 to = pop_1st_bit(&b3);
141 mlist[n++].move = make_move(to - DELTA_N, to);
144 // Discovered checks, double pawn pushes
145 b3 = ((b2 & Rank3BB) << 8) & empty;
148 to = pop_1st_bit(&b3);
149 mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
152 // Direct checks. These are possible only for pawns on neighboring files
155 b1 &= (~dc & neighboring_files_bb(ksq));
157 // Direct checks, single pawn pushes
158 b2 = (b1 << 8) & empty;
159 b3 = b2 & pos.black_pawn_attacks(ksq);
162 to = pop_1st_bit(&b3);
163 mlist[n++].move = make_move(to - DELTA_N, to);
166 // Direct checks, double pawn pushes
167 b3 = ((b2 & Rank3BB) << 8) & empty & pos.black_pawn_attacks(ksq);
170 to = pop_1st_bit(&b3);
171 mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
174 else { // (us == BLACK)
176 // Pawn moves which give discovered check. This is possible only if the
177 // pawn is not on the same file as the enemy king, because we don't
178 // generate captures.
180 // Find all friendly pawns not on the enemy king's file:
181 b1 = pos.pawns(us) & ~file_bb(ksq);
183 // Discovered checks, single pawn pushes
184 b2 = b3 = ((b1 & dc) >> 8) & ~Rank1BB & empty;
187 to = pop_1st_bit(&b3);
188 mlist[n++].move = make_move(to - DELTA_S, to);
191 // Discovered checks, double pawn pushes
192 b3 = ((b2 & Rank6BB) >> 8) & empty;
195 to = pop_1st_bit(&b3);
196 mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
199 // Direct checks. These are possible only for pawns on neighboring files
202 b1 &= (~dc & neighboring_files_bb(ksq));
204 // Direct checks, single pawn pushes:
205 b2 = (b1 >> 8) & empty;
206 b3 = b2 & pos.white_pawn_attacks(ksq);
209 to = pop_1st_bit(&b3);
210 mlist[n++].move = make_move(to - DELTA_S, to);
213 // Direct checks, double pawn pushes
214 b3 = ((b2 & Rank6BB) >> 8) & empty & pos.black_pawn_attacks(ksq);
217 to = pop_1st_bit(&b3);
218 mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
223 b1 = pos.knights(us);
225 n += generate_piece_checks(KNIGHT, pos, b1, dc, ksq, mlist);
228 b1 = pos.bishops(us);
230 n += generate_piece_checks(BISHOP, pos, b1, dc, ksq, mlist);
235 n += generate_piece_checks(ROOK, pos, b1, dc, ksq, mlist);
240 n += generate_piece_checks(QUEEN, pos, b1, dc, ksq, mlist);
243 from = pos.king_square(us);
244 if (bit_is_set(dc, from))
246 b1 = pos.king_attacks(from) & empty & ~QueenPseudoAttacks[ksq];
249 to = pop_1st_bit(&b1);
250 mlist[n++].move = make_move(from, to);
254 // TODO: Castling moves!
260 /// generate_evasions() generates all check evasions when the side to move is
261 /// in check. Unlike the other move generation functions, this one generates
262 /// only legal moves. It returns the number of generated moves. This
263 /// function is very ugly, and needs cleaning up some time later. FIXME
265 int generate_evasions(const Position &pos, MoveStack *mlist) {
268 assert(pos.is_check());
271 Bitboard checkers = pos.checkers();
272 Bitboard pinned, b1, b2;
273 Square ksq, from, to;
276 us = pos.side_to_move();
277 them = opposite_color(us);
279 ksq = pos.king_square(us);
280 assert(pos.piece_on(ksq) == king_of_color(us));
282 // Generate evasions for king:
283 b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
284 b2 = pos.occupied_squares();
287 to = pop_1st_bit(&b1);
289 // Make sure to is not attacked by the other side. This is a bit ugly,
290 // because we can't use Position::square_is_attacked. Instead we use
291 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
292 // b2 (the occupied squares with the king removed) in order to test whether
293 // the king will remain in check on the destination square.
294 if(((pos.pawn_attacks(us, to) & pos.pawns(them)) == EmptyBoardBB) &&
295 ((pos.knight_attacks(to) & pos.knights(them)) == EmptyBoardBB) &&
296 ((pos.king_attacks(to) & pos.kings(them)) == EmptyBoardBB) &&
297 ((bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
299 ((rook_attacks_bb(to, b2) & pos.rooks_and_queens(them)) == EmptyBoardBB))
300 mlist[n++].move = make_move(ksq, to);
304 // Generate evasions for other pieces only if not double check. We use a
305 // simple bit twiddling hack here rather than calling count_1s in order to
306 // save some time (we know that pos.checkers() has at most two nonzero bits).
307 if(!(checkers & (checkers - 1))) {
308 Square checksq = first_1(checkers);
309 assert(pos.color_of_piece_on(checksq) == them);
311 // Find pinned pieces:
312 pinned = pos.pinned_pieces(us);
314 // Generate captures of the checking piece:
317 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
319 from = pop_1st_bit(&b1);
320 if(relative_rank(us, checksq) == RANK_8) {
321 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
322 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
323 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
324 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
327 mlist[n++].move = make_move(from, checksq);
331 b1 = pos.knight_attacks(checksq) & pos.knights(us) & ~pinned;
333 from = pop_1st_bit(&b1);
334 mlist[n++].move = make_move(from, checksq);
337 // Bishop and queen captures:
338 b1 = pos.bishop_attacks(checksq) & pos.bishops_and_queens(us)
341 from = pop_1st_bit(&b1);
342 mlist[n++].move = make_move(from, checksq);
345 // Rook and queen captures:
346 b1 = pos.rook_attacks(checksq) & pos.rooks_and_queens(us)
349 from = pop_1st_bit(&b1);
350 mlist[n++].move = make_move(from, checksq);
353 // Blocking check evasions are possible only if the checking piece is
355 if(checkers & pos.sliders()) {
356 Bitboard blockSquares = squares_between(checksq, ksq);
357 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
359 // Pawn moves. Because a blocking evasion can never be a capture, we
360 // only generate pawn pushes. As so often, the code for pawns is a bit
361 // ugly, and uses separate clauses for white and black pawns. :-(
363 // Find non-pinned pawns:
364 b1 = pos.pawns(WHITE) & ~pinned;
366 // Single pawn pushes. We don't have to AND with empty squares here,
367 // because the blocking squares will always be empty.
368 b2 = (b1 << 8) & blockSquares;
370 to = pop_1st_bit(&b2);
371 assert(pos.piece_on(to) == EMPTY);
372 if(square_rank(to) == RANK_8) {
373 mlist[n++].move = make_promotion_move(to - DELTA_N, to, QUEEN);
374 mlist[n++].move = make_promotion_move(to - DELTA_N, to, ROOK);
375 mlist[n++].move = make_promotion_move(to - DELTA_N, to, BISHOP);
376 mlist[n++].move = make_promotion_move(to - DELTA_N, to, KNIGHT);
379 mlist[n++].move = make_move(to - DELTA_N, to);
381 // Double pawn pushes.
382 b2 = (((b1 << 8) & pos.empty_squares() & Rank3BB) << 8) & blockSquares;
384 to = pop_1st_bit(&b2);
385 assert(pos.piece_on(to) == EMPTY);
386 assert(square_rank(to) == RANK_4);
387 mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
390 else { // (us == BLACK)
391 // Find non-pinned pawns:
392 b1 = pos.pawns(BLACK) & ~pinned;
394 // Single pawn pushes. We don't have to AND with empty squares here,
395 // because the blocking squares will always be empty.
396 b2 = (b1 >> 8) & blockSquares;
398 to = pop_1st_bit(&b2);
399 assert(pos.piece_on(to) == EMPTY);
400 if(square_rank(to) == RANK_1) {
401 mlist[n++].move = make_promotion_move(to - DELTA_S, to, QUEEN);
402 mlist[n++].move = make_promotion_move(to - DELTA_S, to, ROOK);
403 mlist[n++].move = make_promotion_move(to - DELTA_S, to, BISHOP);
404 mlist[n++].move = make_promotion_move(to - DELTA_S, to, KNIGHT);
407 mlist[n++].move = make_move(to - DELTA_S, to);
409 // Double pawn pushes.
410 b2 = (((b1 >> 8) & pos.empty_squares() & Rank6BB) >> 8) & blockSquares;
412 to = pop_1st_bit(&b2);
413 assert(pos.piece_on(to) == EMPTY);
414 assert(square_rank(to) == RANK_5);
415 mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
420 b1 = pos.knights(us) & ~pinned;
422 from = pop_1st_bit(&b1);
423 b2 = pos.knight_attacks(from) & blockSquares;
425 to = pop_1st_bit(&b2);
426 mlist[n++].move = make_move(from, to);
431 b1 = pos.bishops(us) & ~pinned;
433 from = pop_1st_bit(&b1);
434 b2 = pos.bishop_attacks(from) & blockSquares;
436 to = pop_1st_bit(&b2);
437 mlist[n++].move = make_move(from, to);
442 b1 = pos.rooks(us) & ~pinned;
444 from = pop_1st_bit(&b1);
445 b2 = pos.rook_attacks(from) & blockSquares;
447 to = pop_1st_bit(&b2);
448 mlist[n++].move = make_move(from, to);
453 b1 = pos.queens(us) & ~pinned;
455 from = pop_1st_bit(&b1);
456 b2 = pos.queen_attacks(from) & blockSquares;
458 to = pop_1st_bit(&b2);
459 mlist[n++].move = make_move(from, to);
464 // Finally, the ugly special case of en passant captures. An en passant
465 // capture can only be a check evasion if the check is not a discovered
466 // check. If pos.ep_square() is set, the last move made must have been
467 // a double pawn push. If, furthermore, the checking piece is a pawn,
468 // an en passant check evasion may be possible.
469 if(pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them))) {
470 to = pos.ep_square();
471 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
472 assert(b1 != EmptyBoardBB);
475 from = pop_1st_bit(&b1);
477 // Before generating the move, we have to make sure it is legal.
478 // This is somewhat tricky, because the two disappearing pawns may
479 // cause new "discovered checks". We test this by removing the
480 // two relevant bits from the occupied squares bitboard, and using
481 // the low-level bitboard functions for bishop and rook attacks.
482 b2 = pos.occupied_squares();
483 clear_bit(&b2, from);
484 clear_bit(&b2, checksq);
485 if(((bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
487 ((rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))
489 mlist[n++].move = make_ep_move(from, to);
498 /// generate_legal_moves() computes a complete list of legal moves in the
499 /// current position. This function is not very fast, and should be used
500 /// only in situations where performance is unimportant. It wouldn't be
501 /// very hard to write an efficient legal move generator, but for the moment
502 /// we don't need it.
504 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
509 return generate_evasions(pos, mlist);
511 // Generate pseudo-legal moves:
512 int n = generate_captures(pos, mlist);
513 n += generate_noncaptures(pos, mlist + n);
515 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
517 // Remove illegal moves from the list:
518 for (int i = 0; i < n; i++)
519 if (!pos.move_is_legal(mlist[i].move, pinned))
520 mlist[i--].move = mlist[--n].move;
526 /// generate_move_if_legal() takes a position and a (not necessarily
527 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
528 /// whether the move is legal. If the move is legal, the move itself is
529 /// returned. If not, the function returns MOVE_NONE. This function must
530 /// only be used when the side to move is not in check.
532 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
535 assert(!pos.is_check());
536 assert(move_is_ok(m));
538 Color us = pos.side_to_move();
539 Color them = opposite_color(us);
540 Square from = move_from(m);
541 Piece pc = pos.piece_on(from);
543 // If the from square is not occupied by a piece belonging to the side to
544 // move, the move is obviously not legal.
545 if (color_of_piece(pc) != us)
548 Square to = move_to(m);
553 // The piece must be a pawn and destination square must be the
554 // en passant square.
555 if ( type_of_piece(pc) != PAWN
556 || to != pos.ep_square())
559 assert(pos.square_is_empty(to));
560 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
562 // The move is pseudo-legal. If it is legal, return it.
563 return (pos.move_is_legal(m) ? m : MOVE_NONE);
567 if (move_is_short_castle(m))
569 // The piece must be a king and side to move must still have
570 // the right to castle kingside.
571 if ( type_of_piece(pc) != KING
572 ||!pos.can_castle_kingside(us))
575 assert(from == pos.king_square(us));
576 assert(to == pos.initial_kr_square(us));
577 assert(pos.piece_on(to) == rook_of_color(us));
579 Square g1 = relative_square(us, SQ_G1);
580 Square f1 = relative_square(us, SQ_F1);
582 bool illegal = false;
584 // Check if any of the squares between king and rook
585 // is occupied or under attack.
586 for (s = Min(from, g1); s <= Max(from, g1); s++)
587 if ( (s != from && s != to && !pos.square_is_empty(s))
588 || pos.square_is_attacked(s, them))
591 // Check if any of the squares between king and rook
593 for (s = Min(to, f1); s <= Max(to, f1); s++)
594 if (s != from && s != to && !pos.square_is_empty(s))
597 return (!illegal ? m : MOVE_NONE);
600 if (move_is_long_castle(m))
602 // The piece must be a king and side to move must still have
603 // the right to castle kingside.
604 if ( type_of_piece(pc) != KING
605 ||!pos.can_castle_queenside(us))
608 assert(from == pos.king_square(us));
609 assert(to == pos.initial_qr_square(us));
610 assert(pos.piece_on(to) == rook_of_color(us));
612 Square c1 = relative_square(us, SQ_C1);
613 Square d1 = relative_square(us, SQ_D1);
615 bool illegal = false;
617 for (s = Min(from, c1); s <= Max(from, c1); s++)
618 if( (s != from && s != to && !pos.square_is_empty(s))
619 || pos.square_is_attacked(s, them))
622 for (s = Min(to, d1); s <= Max(to, d1); s++)
623 if(s != from && s != to && !pos.square_is_empty(s))
626 if ( square_file(to) == FILE_B
627 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
628 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
631 return (!illegal ? m : MOVE_NONE);
636 // The destination square cannot be occupied by a friendly piece
637 if (pos.color_of_piece_on(to) == us)
640 // Proceed according to the type of the moving piece.
641 if (type_of_piece(pc) == PAWN)
643 // If the destination square is on the 8/1th rank, the move must
645 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
646 ||(square_rank(to) == RANK_1 && us != WHITE))
647 && !move_promotion(m))
650 // Proceed according to the square delta between the source and
651 // destionation squares.
658 // Capture. The destination square must be occupied by an enemy
659 // piece (en passant captures was handled earlier).
660 if (pos.color_of_piece_on(to) != them)
666 // Pawn push. The destination square must be empty.
667 if (!pos.square_is_empty(to))
672 // Double white pawn push. The destination square must be on the fourth
673 // rank, and both the destination square and the square between the
674 // source and destination squares must be empty.
675 if ( square_rank(to) != RANK_4
676 || !pos.square_is_empty(to)
677 || !pos.square_is_empty(from + DELTA_N))
682 // Double black pawn push. The destination square must be on the fifth
683 // rank, and both the destination square and the square between the
684 // source and destination squares must be empty.
685 if ( square_rank(to) != RANK_5
686 || !pos.square_is_empty(to)
687 || !pos.square_is_empty(from + DELTA_S))
694 // The move is pseudo-legal. Return it if it is legal.
695 return (pos.move_is_legal(m) ? m : MOVE_NONE);
698 // Luckly we can handle all the other pieces in one go
699 return ( pos.piece_attacks_square(from, to)
700 && pos.move_is_legal(m)
701 && !move_promotion(m) ? m : MOVE_NONE);
707 int generate_white_pawn_captures(const Position &pos, MoveStack *mlist) {
709 Bitboard pawns = pos.pawns(WHITE);
710 Bitboard enemyPieces = pos.pieces_of_color(BLACK);
714 // Captures in the a1-h8 direction
715 Bitboard b1 = (pawns << 9) & ~FileABB & enemyPieces;
717 // Capturing promotions
718 Bitboard b2 = b1 & Rank8BB;
721 sq = pop_1st_bit(&b2);
722 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, QUEEN);
725 // Capturing non-promotions
729 sq = pop_1st_bit(&b2);
730 mlist[n++].move = make_move(sq - DELTA_NE, sq);
733 // Captures in the h1-a8 direction
734 b1 = (pawns << 7) & ~FileHBB & enemyPieces;
736 // Capturing promotions
740 sq = pop_1st_bit(&b2);
741 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, QUEEN);
744 // Capturing non-promotions
748 sq = pop_1st_bit(&b2);
749 mlist[n++].move = make_move(sq - DELTA_NW, sq);
752 // Non-capturing promotions
753 b1 = (pawns << 8) & pos.empty_squares() & Rank8BB;
756 sq = pop_1st_bit(&b1);
757 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, QUEEN);
760 // En passant captures
761 if (pos.ep_square() != SQ_NONE)
763 assert(square_rank(pos.ep_square()) == RANK_6);
764 b1 = pawns & pos.black_pawn_attacks(pos.ep_square());
765 assert(b1 != EmptyBoardBB);
768 sq = pop_1st_bit(&b1);
769 mlist[n++].move = make_ep_move(sq, pos.ep_square());
776 int generate_black_pawn_captures(const Position &pos, MoveStack *mlist) {
778 Bitboard pawns = pos.pawns(BLACK);
779 Bitboard enemyPieces = pos.pieces_of_color(WHITE);
783 // Captures in the a8-h1 direction
784 Bitboard b1 = (pawns >> 7) & ~FileABB & enemyPieces;
786 // Capturing promotions
787 Bitboard b2 = b1 & Rank1BB;
790 sq = pop_1st_bit(&b2);
791 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, QUEEN);
794 // Capturing non-promotions
798 sq = pop_1st_bit(&b2);
799 mlist[n++].move = make_move(sq - DELTA_SE, sq);
802 // Captures in the h8-a1 direction
803 b1 = (pawns >> 9) & ~FileHBB & enemyPieces;
805 // Capturing promotions
809 sq = pop_1st_bit(&b2);
810 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, QUEEN);
813 // Capturing Non-promotions
817 sq = pop_1st_bit(&b2);
818 mlist[n++].move = make_move(sq - DELTA_SW, sq);
821 // Non-capturing promotions
822 b1 = (pawns >> 8) & pos.empty_squares() & Rank1BB;
825 sq = pop_1st_bit(&b1);
826 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, QUEEN);
829 // En passant captures
830 if (pos.ep_square() != SQ_NONE)
832 assert(square_rank(pos.ep_square()) == RANK_3);
833 b1 = pawns & pos.white_pawn_attacks(pos.ep_square());
834 assert(b1 != EmptyBoardBB);
837 sq = pop_1st_bit(&b1);
838 mlist[n++].move = make_ep_move(sq, pos.ep_square());
845 int generate_white_pawn_noncaptures(const Position &pos, MoveStack *mlist) {
847 Bitboard pawns = pos.pawns(WHITE);
848 Bitboard enemyPieces = pos.pieces_of_color(BLACK);
849 Bitboard emptySquares = pos.empty_squares();
854 // Underpromotion captures in the a1-h8 direction:
855 b1 = (pawns << 9) & ~FileABB & enemyPieces & Rank8BB;
857 sq = pop_1st_bit(&b1);
858 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, ROOK);
859 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, BISHOP);
860 mlist[n++].move = make_promotion_move(sq - DELTA_NE, sq, KNIGHT);
863 // Underpromotion captures in the h1-a8 direction:
864 b1 = (pawns << 7) & ~FileHBB & enemyPieces & Rank8BB;
866 sq = pop_1st_bit(&b1);
867 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, ROOK);
868 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, BISHOP);
869 mlist[n++].move = make_promotion_move(sq - DELTA_NW, sq, KNIGHT);
872 // Single pawn pushes:
873 b1 = (pawns << 8) & emptySquares;
876 sq = pop_1st_bit(&b2);
877 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, ROOK);
878 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, BISHOP);
879 mlist[n++].move = make_promotion_move(sq - DELTA_N, sq, KNIGHT);
883 sq = pop_1st_bit(&b2);
884 mlist[n++].move = make_move(sq - DELTA_N, sq);
887 // Double pawn pushes:
888 b2 = ((b1 & Rank3BB) << 8) & emptySquares;
890 sq = pop_1st_bit(&b2);
891 mlist[n++].move = make_move(sq - DELTA_N - DELTA_N, sq);
898 int generate_black_pawn_noncaptures(const Position &pos, MoveStack *mlist) {
899 Bitboard pawns = pos.pawns(BLACK);
900 Bitboard enemyPieces = pos.pieces_of_color(WHITE);
901 Bitboard emptySquares = pos.empty_squares();
906 // Underpromotion captures in the a8-h1 direction:
907 b1 = (pawns >> 7) & ~FileABB & enemyPieces & Rank1BB;
909 sq = pop_1st_bit(&b1);
910 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, ROOK);
911 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, BISHOP);
912 mlist[n++].move = make_promotion_move(sq - DELTA_SE, sq, KNIGHT);
915 // Underpromotion captures in the h8-a1 direction:
916 b1 = (pawns >> 9) & ~FileHBB & enemyPieces & Rank1BB;
918 sq = pop_1st_bit(&b1);
919 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, ROOK);
920 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, BISHOP);
921 mlist[n++].move = make_promotion_move(sq - DELTA_SW, sq, KNIGHT);
924 // Single pawn pushes:
925 b1 = (pawns >> 8) & emptySquares;
928 sq = pop_1st_bit(&b2);
929 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, ROOK);
930 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, BISHOP);
931 mlist[n++].move = make_promotion_move(sq - DELTA_S, sq, KNIGHT);
935 sq = pop_1st_bit(&b2);
936 mlist[n++].move = make_move(sq - DELTA_S, sq);
939 // Double pawn pushes:
940 b2 = ((b1 & Rank6BB) >> 8) & emptySquares;
942 sq = pop_1st_bit(&b2);
943 mlist[n++].move = make_move(sq - DELTA_S - DELTA_S, sq);
950 int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
951 Color side, Bitboard target) {
953 const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
958 for (int i = 0; i < pos.piece_count(side, piece); i++)
960 from = pos.piece_list(side, piece, i);
961 b = (pos.*mem_fn)(from) & target;
964 to = pop_1st_bit(&b);
965 mlist[n++].move = make_move(from, to);
972 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
975 if(pos.can_castle(us)) {
976 Color them = opposite_color(us);
977 Square ksq = pos.king_square(us);
978 assert(pos.piece_on(ksq) == king_of_color(us));
980 if(pos.can_castle_kingside(us)) {
981 Square rsq = pos.initial_kr_square(us);
982 Square g1 = relative_square(us, SQ_G1);
983 Square f1 = relative_square(us, SQ_F1);
985 bool illegal = false;
987 assert(pos.piece_on(rsq) == rook_of_color(us));
989 for(s = Min(ksq, g1); s <= Max(ksq, g1); s++)
990 if((s != ksq && s != rsq && pos.square_is_occupied(s))
991 || pos.square_is_attacked(s, them))
993 for(s = Min(rsq, f1); s <= Max(rsq, f1); s++)
994 if(s != ksq && s != rsq && pos.square_is_occupied(s))
998 mlist[n++].move = make_castle_move(ksq, rsq);
1001 if(pos.can_castle_queenside(us)) {
1002 Square rsq = pos.initial_qr_square(us);
1003 Square c1 = relative_square(us, SQ_C1);
1004 Square d1 = relative_square(us, SQ_D1);
1006 bool illegal = false;
1008 assert(pos.piece_on(rsq) == rook_of_color(us));
1010 for(s = Min(ksq, c1); s <= Max(ksq, c1); s++)
1011 if((s != ksq && s != rsq && pos.square_is_occupied(s))
1012 || pos.square_is_attacked(s, them))
1014 for(s = Min(rsq, d1); s <= Max(rsq, d1); s++)
1015 if(s != ksq && s != rsq && pos.square_is_occupied(s))
1017 if(square_file(rsq) == FILE_B &&
1018 (pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them) ||
1019 pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
1023 mlist[n++].move = make_castle_move(ksq, rsq);
1030 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
1031 Bitboard dc, Square ksq, MoveStack* mlist) {
1033 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
1036 // Discovered checks
1037 Bitboard b = target & dc;
1040 Square from = pop_1st_bit(&b);
1041 Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
1044 Square to = pop_1st_bit(&bb);
1045 mlist[n++].move = make_move(from, to);
1051 Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
1054 Square from = pop_1st_bit(&b);
1055 Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
1058 Square to = pop_1st_bit(&bb);
1059 mlist[n++].move = make_move(from, to);