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