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