]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
134127eb387837acd51e38a197ab4807c2b41fc2
[stockfish] / src / movegen.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008 Marco Costalba
4
5   Stockfish 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.
9
10   Stockfish 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.
14
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/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <cassert>
25
26 #include "movegen.h"
27
28 // Simple macro to wrap a very common while loop, no facny, no flexibility,
29 // hardcoded list name 'mlist' and from square 'from'.
30 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
31
32 ////
33 //// Local definitions
34 ////
35
36 namespace {
37
38   enum CastlingSide {
39     KING_SIDE,
40     QUEEN_SIDE
41   };
42
43   const bool CAPTURE = true;
44   const bool NON_CAPTURE = false;
45
46   // Functions
47   bool castling_is_check(const Position&, CastlingSide);
48
49   // Helper templates
50   template<CastlingSide Side>
51   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
52
53   template<Color Us, Rank, Bitboard, SquareDelta>
54   MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
55
56   template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
57   MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
58
59   template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
60   MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
61
62   template<Color, Color, Bitboard, Bitboard, SquareDelta>
63   MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
64
65   // Template generate_piece_checks() with specializations
66   template<PieceType>
67   MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
68
69   template<>
70   inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
71
72     if (us == WHITE)
73         return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
74     else
75         return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
76
77   }
78
79   // Template generate_piece_moves() with specializations
80   template<PieceType>
81   MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
82
83   template<>
84   MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
85
86   template<PieceType Piece, bool Capture>
87   inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
88
89       assert(Piece == PAWN);
90
91       if (Capture)
92           return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
93                               : generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
94       else
95           return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
96                               : generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
97   }
98
99   // Template generate_piece_blocking_evasions() with specializations
100   template<PieceType>
101   MoveStack* generate_piece_blocking_evasions(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
102
103   template<>
104   inline MoveStack* generate_piece_blocking_evasions<PAWN>(const Position& p, MoveStack* m, Color us,
105                                                            Bitboard pnd, Bitboard bs) {
106     if (us == WHITE)
107         return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, bs, m);
108     else
109         return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, bs, m);
110   }
111 }
112
113
114 ////
115 //// Functions
116 ////
117
118
119 /// generate_captures generates() all pseudo-legal captures and queen
120 /// promotions.  The return value is the number of moves generated.
121
122 int generate_captures(const Position& pos, MoveStack* mlist) {
123
124   assert(pos.is_ok());
125   assert(!pos.is_check());
126
127   Color us = pos.side_to_move();
128   Bitboard target = pos.pieces_of_color(opposite_color(us));
129   MoveStack* mlist_start = mlist;
130
131   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
132   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
133   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
134   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
135   mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
136   mlist = generate_piece_moves<KING>(pos, mlist, us, target);
137   return int(mlist - mlist_start);
138 }
139
140
141 /// generate_noncaptures() generates all pseudo-legal non-captures and
142 /// underpromotions. The return value is the number of moves generated.
143
144 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
145
146   assert(pos.is_ok());
147   assert(!pos.is_check());
148
149   Color us = pos.side_to_move();
150   Bitboard target = pos.empty_squares();
151   MoveStack* mlist_start = mlist;
152
153   mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
154   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
155   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
156   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
157   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
158   mlist = generate_piece_moves<KING>(pos, mlist, us, target);
159   mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
160   mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
161   return int(mlist - mlist_start);
162 }
163
164
165 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
166 /// checks. It returns the number of generated moves.
167
168 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
169
170   assert(pos.is_ok());
171   assert(!pos.is_check());
172
173   Color us = pos.side_to_move();
174   Square ksq = pos.king_square(opposite_color(us));
175   MoveStack* mlist_start = mlist;
176
177   assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
178
179   // Pieces moves
180   mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
181   mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
182   mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
183   mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
184   mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
185   mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
186
187   // Castling moves that give check. Very rare but nice to have!
188   if (   pos.can_castle_queenside(us)
189       && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
190       && castling_is_check(pos, QUEEN_SIDE))
191       mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
192
193   if (   pos.can_castle_kingside(us)
194       && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
195       && castling_is_check(pos, KING_SIDE))
196       mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
197
198   return int(mlist - mlist_start);
199 }
200
201
202 /// generate_evasions() generates all check evasions when the side to move is
203 /// in check.  Unlike the other move generation functions, this one generates
204 /// only legal moves.  It returns the number of generated moves. This
205 /// function is very ugly, and needs cleaning up some time later.  FIXME
206
207 int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
208
209   assert(pos.is_ok());
210   assert(pos.is_check());
211
212   Square from, to;
213   Color us = pos.side_to_move();
214   Color them = opposite_color(us);
215   Square ksq = pos.king_square(us);
216   MoveStack* mlist_start = mlist;
217
218   assert(pos.piece_on(ksq) == king_of_color(us));
219
220   // The bitboard of occupied pieces without our king
221   Bitboard b2 = pos.occupied_squares();
222   clear_bit(&b2, ksq);
223
224   // Find squares attacked by slider checkers, we will
225   // remove them from king evasions set so to avoid a couple
226   // of cycles in the slow king evasions legality check loop
227   // and to be able to use square_is_attacked().
228   Bitboard checkers = pos.checkers();
229   Bitboard checkersAttacks = EmptyBoardBB;
230   Bitboard b = checkers & (pos.queens() | pos.bishops());
231   while (b)
232   {
233       from = pop_1st_bit(&b);
234       checkersAttacks |= bishop_attacks_bb(from, b2);
235   }
236
237   b = checkers & (pos.queens() | pos.rooks());
238   while (b)
239   {
240       from = pop_1st_bit(&b);
241       checkersAttacks |= rook_attacks_bb(from, b2);
242   }
243
244   // Generate evasions for king
245   Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
246   while (b1)
247   {
248       to = pop_1st_bit(&b1);
249       // Note that we can use square_is_attacked() only because we
250       // have already removed sliders checkers.
251       if (!pos.square_is_attacked(to, them))
252           (*mlist++).move = make_move(ksq, to);
253   }
254
255   // Generate evasions for other pieces only if not double check. We use a
256   // simple bit twiddling hack here rather than calling count_1s in order to
257   // save some time (we know that pos.checkers() has at most two nonzero bits).
258   if (!(checkers & (checkers - 1))) // Only one bit set?
259   {
260       Square checksq = first_1(checkers);
261
262       assert(pos.color_of_piece_on(checksq) == them);
263
264       // Generate captures of the checking piece
265
266       // Pawn captures
267       b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
268       while (b1)
269       {
270           from = pop_1st_bit(&b1);
271           if (relative_rank(us, checksq) == RANK_8)
272           {
273               (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
274               (*mlist++).move = make_promotion_move(from, checksq, ROOK);
275               (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
276               (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
277           } else
278               (*mlist++).move = make_move(from, checksq);
279       }
280
281       // Pieces captures
282       b1 = (  (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
283             | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
284             | (pos.piece_attacks<ROOK>(checksq)   & pos.rooks_and_queens(us)) ) & ~pinned;
285
286       while (b1)
287       {
288           from = pop_1st_bit(&b1);
289           (*mlist++).move = make_move(from, checksq);
290       }
291
292       // Blocking check evasions are possible only if the checking piece is
293       // a slider.
294       if (checkers & pos.sliders())
295       {
296           Bitboard blockSquares = squares_between(checksq, ksq);
297
298           assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
299
300           // Pieces moves
301           mlist = generate_piece_blocking_evasions<PAWN>(pos, mlist, us, pinned, blockSquares);
302           mlist = generate_piece_blocking_evasions<KNIGHT>(pos, mlist, us, pinned, blockSquares);
303           mlist = generate_piece_blocking_evasions<BISHOP>(pos, mlist, us, pinned, blockSquares);
304           mlist = generate_piece_blocking_evasions<ROOK>(pos, mlist, us, pinned, blockSquares);
305           mlist = generate_piece_blocking_evasions<QUEEN>(pos, mlist, us, pinned, blockSquares);
306     }
307
308     // Finally, the ugly special case of en passant captures. An en passant
309     // capture can only be a check evasion if the check is not a discovered
310     // check. If pos.ep_square() is set, the last move made must have been
311     // a double pawn push. If, furthermore, the checking piece is a pawn,
312     // an en passant check evasion may be possible.
313     if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
314     {
315         to = pos.ep_square();
316         b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
317
318         // The checking pawn cannot be a discovered (bishop) check candidate
319         // otherwise we were in check also before last double push move.
320         assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
321         assert(count_1s(b1) == 1 || count_1s(b1) == 2);
322
323         b1 &= ~pinned;
324         while (b1)
325         {
326             from = pop_1st_bit(&b1);
327             // Move is always legal because checking pawn is not a discovered
328             // check candidate and our capturing pawn has been already tested
329             // against pinned pieces.
330             (*mlist++).move = make_ep_move(from, to);
331         }
332     }
333   }
334   return int(mlist - mlist_start);
335 }
336
337
338 /// generate_legal_moves() computes a complete list of legal moves in the
339 /// current position. This function is not very fast, and should be used
340 /// only in situations where performance is unimportant. It wouldn't be
341 /// very hard to write an efficient legal move generator, but for the moment
342 /// we don't need it.
343
344 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
345
346   assert(pos.is_ok());
347
348   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
349
350   if (pos.is_check())
351       return generate_evasions(pos, mlist, pinned);
352
353   // Generate pseudo-legal moves
354   int n = generate_captures(pos, mlist);
355   n += generate_noncaptures(pos, mlist + n);
356
357   // Remove illegal moves from the list
358   for (int i = 0; i < n; i++)
359       if (!pos.pl_move_is_legal(mlist[i].move, pinned))
360           mlist[i--].move = mlist[--n].move;
361
362   return n;
363 }
364
365
366 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
367 /// move and a pinned pieces bitboard as input, and tests whether
368 /// the move is legal.  If the move is legal, the move itself is
369 /// returned. If not, the function returns false.  This function must
370 /// only be used when the side to move is not in check.
371
372 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
373
374   assert(pos.is_ok());
375   assert(!pos.is_check());
376   assert(move_is_ok(m));
377   assert(pinned == pos.pinned_pieces(pos.side_to_move()));
378
379   Color us = pos.side_to_move();
380   Color them = opposite_color(us);
381   Square from = move_from(m);
382   Piece pc = pos.piece_on(from);
383
384   // If the from square is not occupied by a piece belonging to the side to
385   // move, the move is obviously not legal.
386   if (color_of_piece(pc) != us)
387       return false;
388
389   Square to = move_to(m);
390
391   // En passant moves
392   if (move_is_ep(m))
393   {
394       // The piece must be a pawn and destination square must be the
395       // en passant square.
396       if (   type_of_piece(pc) != PAWN
397           || to != pos.ep_square())
398           return false;
399
400       assert(pos.square_is_empty(to));
401       assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
402
403       // The move is pseudo-legal, check if it is also legal
404       return pos.pl_move_is_legal(m, pinned);
405   }
406
407   // Castling moves
408   if (move_is_short_castle(m))
409   {
410       // The piece must be a king and side to move must still have
411       // the right to castle kingside.
412       if (   type_of_piece(pc) != KING
413           ||!pos.can_castle_kingside(us))
414           return false;
415
416       assert(from == pos.king_square(us));
417       assert(to == pos.initial_kr_square(us));
418       assert(pos.piece_on(to) == rook_of_color(us));
419
420       Square g1 = relative_square(us, SQ_G1);
421       Square f1 = relative_square(us, SQ_F1);
422       Square s;
423       bool illegal = false;
424
425       // Check if any of the squares between king and rook
426       // is occupied or under attack.
427       for (s = Min(from, g1); s <= Max(from, g1); s++)
428           if (  (s != from && s != to && !pos.square_is_empty(s))
429               || pos.square_is_attacked(s, them))
430               illegal = true;
431
432       // Check if any of the squares between king and rook
433       // is occupied.
434       for (s = Min(to, f1); s <= Max(to, f1); s++)
435           if (s != from && s != to && !pos.square_is_empty(s))
436               illegal = true;
437
438       return !illegal;
439   }
440
441   if (move_is_long_castle(m))
442   {
443       // The piece must be a king and side to move must still have
444       // the right to castle kingside.
445       if (   type_of_piece(pc) != KING
446           ||!pos.can_castle_queenside(us))
447           return false;
448
449       assert(from == pos.king_square(us));
450       assert(to == pos.initial_qr_square(us));
451       assert(pos.piece_on(to) == rook_of_color(us));
452
453       Square c1 = relative_square(us, SQ_C1);
454       Square d1 = relative_square(us, SQ_D1);
455       Square s;
456       bool illegal = false;
457
458       for (s = Min(from, c1); s <= Max(from, c1); s++)
459           if(  (s != from && s != to && !pos.square_is_empty(s))
460              || pos.square_is_attacked(s, them))
461               illegal = true;
462
463       for (s = Min(to, d1); s <= Max(to, d1); s++)
464           if(s != from && s != to && !pos.square_is_empty(s))
465               illegal = true;
466
467       if (   square_file(to) == FILE_B
468           && (   pos.piece_on(to + DELTA_W) == rook_of_color(them)
469               || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
470           illegal = true;
471
472       return !illegal;
473   }
474
475   // Normal moves
476
477   // The destination square cannot be occupied by a friendly piece
478   if (pos.color_of_piece_on(to) == us)
479       return false;
480
481   // Proceed according to the type of the moving piece.
482   if (type_of_piece(pc) == PAWN)
483   {
484       // If the destination square is on the 8/1th rank, the move must
485       // be a promotion.
486       if (   (  (square_rank(to) == RANK_8 && us == WHITE)
487               ||(square_rank(to) == RANK_1 && us != WHITE))
488            && !move_promotion(m))
489           return false;
490
491       // Proceed according to the square delta between the source and
492       // destionation squares.
493       switch (to - from)
494       {
495       case DELTA_NW:
496       case DELTA_NE:
497       case DELTA_SW:
498       case DELTA_SE:
499       // Capture. The destination square must be occupied by an enemy
500       // piece (en passant captures was handled earlier).
501           if (pos.color_of_piece_on(to) != them)
502               return false;
503           break;
504
505       case DELTA_N:
506       case DELTA_S:
507       // Pawn push. The destination square must be empty.
508           if (!pos.square_is_empty(to))
509               return false;
510           break;
511
512       case DELTA_NN:
513       // Double white pawn push. The destination square must be on the fourth
514       // rank, and both the destination square and the square between the
515       // source and destination squares must be empty.
516       if (   square_rank(to) != RANK_4
517           || !pos.square_is_empty(to)
518           || !pos.square_is_empty(from + DELTA_N))
519           return false;
520           break;
521
522       case DELTA_SS:
523       // Double black pawn push. The destination square must be on the fifth
524       // rank, and both the destination square and the square between the
525       // source and destination squares must be empty.
526           if (   square_rank(to) != RANK_5
527               || !pos.square_is_empty(to)
528               || !pos.square_is_empty(from + DELTA_S))
529               return false;
530           break;
531
532       default:
533           return false;
534       }
535       // The move is pseudo-legal, check if it is also legal
536       return pos.pl_move_is_legal(m, pinned);
537   }
538
539   // Luckly we can handle all the other pieces in one go
540   return (   pos.piece_attacks_square(from, to)
541           && pos.pl_move_is_legal(m, pinned)
542           && !move_promotion(m));
543 }
544
545
546 namespace {
547
548   template<PieceType Piece>
549   MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
550
551     Square from;
552     Bitboard b;
553
554     for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
555     {
556         from = pos.piece_list(us, Piece, i);
557         b = pos.piece_attacks<Piece>(from) & target;
558         SERIALIZE_MOVES(b);
559     }
560     return mlist;
561   }
562
563   template<>
564   MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
565
566     Bitboard b;
567     Square from = pos.king_square(us);
568
569     b = pos.piece_attacks<KING>(from) & target;
570     SERIALIZE_MOVES(b);
571     return mlist;
572   }
573
574   template<PieceType Piece>
575   MoveStack* generate_piece_blocking_evasions(const Position& pos, MoveStack* mlist, Color us,
576                                               Bitboard pinned, Bitboard blockSquares) {
577     Square from;
578     Bitboard b;
579
580     for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
581     {
582         from = pos.piece_list(us, Piece, i);
583         if (pinned && bit_is_set(pinned, from))
584             continue;
585
586         b = pos.piece_attacks<Piece>(from) & blockSquares;
587         SERIALIZE_MOVES(b);
588     }
589     return mlist;
590   }
591
592   template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
593            SquareDelta TDELTA_NW, SquareDelta TDELTA_N
594           >
595   MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
596
597     Square to;
598     Bitboard pawns = pos.pawns(Us);
599     Bitboard enemyPieces = pos.pieces_of_color(Them);
600
601     // Captures in the a1-h8 (a8-h1 for black) direction
602     Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
603
604     // Capturing promotions
605     Bitboard b2 = b1 & TRank8BB;
606     while (b2)
607     {
608         to = pop_1st_bit(&b2);
609         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
610     }
611
612     // Capturing non-promotions
613     b2 = b1 & ~TRank8BB;
614     while (b2)
615     {
616         to = pop_1st_bit(&b2);
617         (*mlist++).move = make_move(to - TDELTA_NE, to);
618     }
619
620     // Captures in the h1-a8 (h8-a1 for black) direction
621     b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
622
623     // Capturing promotions
624     b2 = b1 & TRank8BB;
625     while (b2)
626     {
627         to = pop_1st_bit(&b2);
628         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
629     }
630
631     // Capturing non-promotions
632     b2 = b1 & ~TRank8BB;
633     while (b2)
634     {
635         to = pop_1st_bit(&b2);
636         (*mlist++).move = make_move(to - TDELTA_NW, to);
637     }
638
639     // Non-capturing promotions
640     b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
641     while (b1)
642     {
643         to = pop_1st_bit(&b1);
644         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
645     }
646
647     // En passant captures
648     if (pos.ep_square() != SQ_NONE)
649     {
650         assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
651         assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
652
653         b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
654         assert(b1 != EmptyBoardBB);
655
656         while (b1)
657         {
658             to = pop_1st_bit(&b1);
659             (*mlist++).move = make_ep_move(to, pos.ep_square());
660         }
661     }
662     return mlist;
663   }
664
665   template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
666            SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
667           >
668   MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
669
670     Bitboard pawns = pos.pawns(Us);
671     Bitboard enemyPieces = pos.pieces_of_color(Them);
672     Bitboard emptySquares = pos.empty_squares();
673     Bitboard b1, b2;
674     Square to;
675
676     // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
677     b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
678     while (b1)
679     {
680         to = pop_1st_bit(&b1);
681         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
682         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
683         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
684     }
685
686     // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
687     b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
688     while (b1)
689     {
690         to = pop_1st_bit(&b1);
691         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
692         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
693         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
694     }
695
696     // Single pawn pushes
697     b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
698     b2 = b1 & TRank8BB;
699     while (b2)
700     {
701         to = pop_1st_bit(&b2);
702         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
703         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
704         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
705     }
706     b2 = b1 & ~TRank8BB;
707     while (b2)
708     {
709         to = pop_1st_bit(&b2);
710         (*mlist++).move = make_move(to - TDELTA_N, to);
711     }
712
713     // Double pawn pushes
714     b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
715     while (b2)
716     {
717         to = pop_1st_bit(&b2);
718         (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
719     }
720     return mlist;
721   }
722
723
724   template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
725   MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
726   {
727     // Find all friendly pawns not on the enemy king's file
728     Bitboard b1, b2, b3;
729     Bitboard empty = pos.empty_squares();
730
731     if (dc != EmptyBoardBB)
732     {
733         // Pawn moves which gives discovered check. This is possible only if the
734         // pawn is not on the same file as the enemy king, because we don't
735         // generate captures.
736         b1 = pos.pawns(Us) & ~file_bb(ksq);
737
738         // Discovered checks, single pawn pushes, no promotions
739         b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
740         while (b3)
741         {
742             Square to = pop_1st_bit(&b3);
743             (*mlist++).move = make_move(to - TDELTA_N, to);
744         }
745
746         // Discovered checks, double pawn pushes
747         b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
748         while (b3)
749         {
750             Square to = pop_1st_bit(&b3);
751             (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
752         }
753     }
754
755     // Direct checks. These are possible only for pawns on neighboring files
756     // of the enemy king.
757     b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
758
759     // Direct checks, single pawn pushes
760     b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
761     b3 = b2 & pos.pawn_attacks(Them, ksq);
762     while (b3)
763     {
764         Square to = pop_1st_bit(&b3);
765         (*mlist++).move = make_move(to - TDELTA_N, to);
766     }
767
768     // Direct checks, double pawn pushes
769     b3 =  (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
770         & empty
771         & pos.pawn_attacks(Them, ksq);
772     while (b3)
773     {
774         Square to = pop_1st_bit(&b3);
775         (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
776     }
777     return mlist;
778   }
779
780   template<PieceType Piece>
781   MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
782                                    Bitboard dc, Square ksq) {
783
784     Bitboard target = pos.pieces_of_color_and_type(us, Piece);
785
786     // Discovered checks
787     Bitboard b = target & dc;
788     while (b)
789     {
790         Square from = pop_1st_bit(&b);
791         Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
792         if (Piece == KING)
793             bb &= ~QueenPseudoAttacks[ksq];
794
795         SERIALIZE_MOVES(bb);
796     }
797
798     // Direct checks
799     b = target & ~dc;
800     if (Piece == KING || !b)
801         return mlist;
802
803     Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
804     while (b)
805     {
806         Square from = pop_1st_bit(&b);
807         Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
808         SERIALIZE_MOVES(bb);
809     }
810     return mlist;
811   }
812
813   template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
814   MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
815                                              Bitboard blockSquares, MoveStack* mlist) {
816     Square to;
817
818     // Find non-pinned pawns
819     Bitboard b1 = pos.pawns(Us) & ~pinned;
820
821     // Single pawn pushes. We don't have to AND with empty squares here,
822     // because the blocking squares will always be empty.
823     Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
824     while (b2)
825     {
826         to = pop_1st_bit(&b2);
827
828         assert(pos.piece_on(to) == EMPTY);
829
830         if (square_rank(to) == TRANK_8)
831         {
832             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
833             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
834             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
835             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
836         } else
837             (*mlist++).move = make_move(to - TDELTA_N, to);
838     }
839
840     // Double pawn pushes
841     b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
842     b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;
843     while (b2)
844     {
845         to = pop_1st_bit(&b2);
846
847         assert(pos.piece_on(to) == EMPTY);
848         assert(Us != WHITE || square_rank(to) == RANK_4);
849         assert(Us != BLACK || square_rank(to) == RANK_5);
850
851         (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
852     }
853     return mlist;
854   }
855
856   template<CastlingSide Side>
857   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
858
859     Color us = pos.side_to_move();
860
861     if (  (Side == KING_SIDE && pos.can_castle_kingside(us))
862         ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
863     {
864         Color them = opposite_color(us);
865         Square ksq = pos.king_square(us);
866
867         assert(pos.piece_on(ksq) == king_of_color(us));
868
869         Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
870         Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
871         Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
872         Square s;
873         bool illegal = false;
874
875         assert(pos.piece_on(rsq) == rook_of_color(us));
876
877         // It is a bit complicated to correctly handle Chess960
878         for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
879             if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
880                 || pos.square_is_attacked(s, them))
881                 illegal = true;
882
883         for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
884             if (s != ksq && s != rsq && pos.square_is_occupied(s))
885                 illegal = true;
886
887         if (   Side == QUEEN_SIDE
888             && square_file(rsq) == FILE_B
889             && (   pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
890                 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
891             illegal = true;
892
893         if (!illegal)
894             (*mlist++).move = make_castle_move(ksq, rsq);
895     }
896     return mlist;
897   }
898
899   bool castling_is_check(const Position& pos, CastlingSide side) {
900
901     // After castling opponent king is attacked by the castled rook?
902     File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
903     Color us = pos.side_to_move();
904     Square ksq = pos.king_square(us);
905     Bitboard occ = pos.occupied_squares();
906
907     clear_bit(&occ, ksq); // Remove our king from the board
908     Square rsq = make_square(rookFile, square_rank(ksq));
909     return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));
910   }
911 }