]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Retire piece_type_from_char()
[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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
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 "bitcount.h"
28 #include "movegen.h"
29 #include "types.h"
30
31 // Simple macro to wrap a very common while loop, no facny, no flexibility,
32 // hardcoded list name 'mlist' and from square 'from'.
33 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
34
35 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
36 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
37
38 ////
39 //// Local definitions
40 ////
41
42 namespace {
43
44   enum CastlingSide {
45     KING_SIDE,
46     QUEEN_SIDE
47   };
48
49   template<CastlingSide Side>
50   MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
51
52   template<Color Us, MoveType Type>
53   MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
54
55   template<PieceType Piece>
56   inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
57
58     assert(Piece != QUEEN);
59
60     Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
61     if (Piece == KING)
62     {
63         Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
64         b &= ~QueenPseudoAttacks[ksq];
65     }
66     SERIALIZE_MOVES(b);
67     return mlist;
68   }
69
70   template<PieceType Piece>
71   inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
72                                            Bitboard dc, Square ksq) {
73     assert(Piece != KING);
74
75     Bitboard checkSqs, b;
76     Square from;
77     const Square* ptr = pos.piece_list_begin(us, Piece);
78
79     if ((from = *ptr++) == SQ_NONE)
80         return mlist;
81
82     checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
83
84     do
85     {
86         if (   (Piece == QUEEN  && !(QueenPseudoAttacks[from]  & checkSqs))
87             || (Piece == ROOK   && !(RookPseudoAttacks[from]   & checkSqs))
88             || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
89             continue;
90
91         if (dc && bit_is_set(dc, from))
92             continue;
93
94         b = pos.attacks_from<Piece>(from) & checkSqs;
95         SERIALIZE_MOVES(b);
96
97     } while ((from = *ptr++) != SQ_NONE);
98
99     return mlist;
100   }
101
102   template<>
103   FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
104
105     return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
106                         : generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
107   }
108
109   template<PieceType Piece, MoveType Type>
110   FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
111
112     assert(Piece == PAWN);
113     assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
114
115     return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
116                         : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
117   }
118
119   template<PieceType Piece>
120   FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
121
122     Bitboard b;
123     Square from;
124     const Square* ptr = pos.piece_list_begin(us, Piece);
125
126     if (*ptr != SQ_NONE)
127     {
128         do {
129             from = *ptr;
130             b = pos.attacks_from<Piece>(from) & target;
131             SERIALIZE_MOVES(b);
132         } while (*++ptr != SQ_NONE);
133     }
134     return mlist;
135   }
136
137   template<>
138   FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
139
140     Bitboard b;
141     Square from = pos.king_square(us);
142
143     b = pos.attacks_from<KING>(from) & target;
144     SERIALIZE_MOVES(b);
145     return mlist;
146   }
147
148 }
149
150 ////
151 //// Functions
152 ////
153
154
155 /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
156 /// promotions. Returns a pointer to the end of the move list.
157 ///
158 /// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
159 /// underpromotions. Returns a pointer to the end of the move list.
160 ///
161 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
162 /// non-captures. Returns a pointer to the end of the move list.
163
164 template<MoveType Type>
165 MoveStack* generate(const Position& pos, MoveStack* mlist) {
166
167   assert(pos.is_ok());
168   assert(!pos.is_check());
169
170   Color us = pos.side_to_move();
171   Bitboard target;
172
173   if (Type == MV_CAPTURE || Type == MV_NON_EVASION)
174       target = pos.pieces_of_color(opposite_color(us));
175   else if (Type == MV_NON_CAPTURE)
176       target = pos.empty_squares();
177   else
178       assert(false);
179
180   if (Type == MV_NON_EVASION)
181   {
182       mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
183       mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
184       target |= pos.empty_squares();
185   }
186   else
187       mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);
188
189   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
190   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
191   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
192   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
193   mlist = generate_piece_moves<KING>(pos, mlist, us, target);
194
195   if (Type != MV_CAPTURE)
196   {
197       if (pos.can_castle_kingside(us))
198           mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
199
200       if (pos.can_castle_queenside(us))
201           mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
202   }
203
204   return mlist;
205 }
206
207 // Explicit template instantiation
208 template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
209 template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
210 template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
211
212
213 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
214 /// underpromotions that give check. Returns a pointer to the end of the move list.
215 template<>
216 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
217
218   assert(pos.is_ok());
219   assert(!pos.is_check());
220
221   Bitboard b, dc;
222   Square from;
223   Color us = pos.side_to_move();
224   Square ksq = pos.king_square(opposite_color(us));
225
226   assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
227
228   // Discovered non-capture checks
229   b = dc = pos.discovered_check_candidates(us);
230
231   while (b)
232   {
233      from = pop_1st_bit(&b);
234      switch (pos.type_of_piece_on(from))
235      {
236       case PAWN:   /* Will be generated togheter with pawns direct checks */     break;
237       case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
238       case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
239       case ROOK:   mlist = generate_discovered_checks<ROOK>(pos, mlist, from);   break;
240       case KING:   mlist = generate_discovered_checks<KING>(pos, mlist, from);   break;
241       default: assert(false); break;
242      }
243   }
244
245   // Direct non-capture checks
246   mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
247   mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
248   mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
249   mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
250   return  generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
251 }
252
253
254 /// generate_evasions() generates all pseudo-legal check evasions when
255 /// the side to move is in check. Returns a pointer to the end of the move list.
256 template<>
257 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
258
259   assert(pos.is_ok());
260   assert(pos.is_check());
261
262   Bitboard b, target;
263   Square from, checksq;
264   int checkersCnt = 0;
265   Color us = pos.side_to_move();
266   Square ksq = pos.king_square(us);
267   Bitboard checkers = pos.checkers();
268   Bitboard sliderAttacks = EmptyBoardBB;
269
270   assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
271   assert(checkers);
272
273   // Find squares attacked by slider checkers, we will remove
274   // them from the king evasions set so to early skip known
275   // illegal moves and avoid an useless legality check later.
276   b = checkers;
277   do
278   {
279       checkersCnt++;
280       checksq = pop_1st_bit(&b);
281
282       assert(pos.color_of_piece_on(checksq) == opposite_color(us));
283
284       switch (pos.type_of_piece_on(checksq))
285       {
286       case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
287       case ROOK:   sliderAttacks |= RookPseudoAttacks[checksq];   break;
288       case QUEEN:
289           // In case of a queen remove also squares attacked in the other direction to
290           // avoid possible illegal moves when queen and king are on adjacent squares.
291           if (RookPseudoAttacks[checksq] & (1ULL << ksq))
292               sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
293           else
294               sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
295       default:
296           break;
297       }
298   } while (b);
299
300   // Generate evasions for king, capture and non capture moves
301   b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
302   from = ksq;
303   SERIALIZE_MOVES(b);
304
305   // Generate evasions for other pieces only if not double check
306   if (checkersCnt > 1)
307       return mlist;
308
309   // Find squares where a blocking evasion or a capture of the
310   // checker piece is possible.
311   target = squares_between(checksq, ksq) | checkers;
312
313   mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
314   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
315   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
316   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
317   return  generate_piece_moves<QUEEN>(pos, mlist, us, target);
318 }
319
320
321 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
322 /// or pseudo-legal moves in the current position.
323 template<>
324 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
325
326   assert(pos.is_ok());
327
328   return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
329                         : generate<MV_NON_EVASION>(pos, mlist);
330 }
331
332 template<>
333 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
334
335   assert(pos.is_ok());
336
337   MoveStack *last, *cur = mlist;
338   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
339
340   last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
341
342   // Remove illegal moves from the list
343   while (cur != last)
344       if (pos.pl_move_is_legal(cur->move, pinned))
345           cur++;
346       else
347           cur->move = (--last)->move;
348
349   return last;
350 }
351
352
353 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
354 /// move and tests whether the move is legal. This version is not very fast
355 /// and should be used only in non time-critical paths.
356
357 bool move_is_legal(const Position& pos, const Move m) {
358
359   MoveStack mlist[MOVES_MAX];
360   MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
361
362    for (cur = mlist; cur != last; cur++)
363       if (cur->move == m)
364           return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
365
366   return false;
367 }
368
369
370 /// Fast version of move_is_legal() that takes a position a move and a
371 /// bitboard of pinned pieces as input, and tests whether the move is legal.
372
373 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
374
375   assert(pos.is_ok());
376   assert(pinned == pos.pinned_pieces(pos.side_to_move()));
377
378   Color us = pos.side_to_move();
379   Color them = opposite_color(us);
380   Square from = move_from(m);
381   Square to = move_to(m);
382   Piece pc = pos.piece_on(from);
383
384   // Use a slower but simpler function for uncommon cases
385   if (move_is_special(m))
386       return move_is_legal(pos, m);
387
388   // If the from square is not occupied by a piece belonging to the side to
389   // move, the move is obviously not legal.
390   if (color_of_piece(pc) != us)
391       return false;
392
393   // The destination square cannot be occupied by a friendly piece
394   if (pos.color_of_piece_on(to) == us)
395       return false;
396
397   // Handle the special case of a pawn move
398   if (type_of_piece(pc) == PAWN)
399   {
400       // Move direction must be compatible with pawn color
401       int direction = to - from;
402       if ((us == WHITE) != (direction > 0))
403           return false;
404
405       // We have already handled promotion moves, so destination
406       // cannot be on the 8/1th rank.
407       if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
408           return false;
409
410       // Proceed according to the square delta between the origin and
411       // destination squares.
412       switch (direction)
413       {
414       case DELTA_NW:
415       case DELTA_NE:
416       case DELTA_SW:
417       case DELTA_SE:
418       // Capture. The destination square must be occupied by an enemy
419       // piece (en passant captures was handled earlier).
420           if (pos.color_of_piece_on(to) != them)
421               return false;
422           break;
423
424       case DELTA_N:
425       case DELTA_S:
426       // Pawn push. The destination square must be empty.
427           if (!pos.square_is_empty(to))
428               return false;
429           break;
430
431       case DELTA_NN:
432       // Double white pawn push. The destination square must be on the fourth
433       // rank, and both the destination square and the square between the
434       // source and destination squares must be empty.
435       if (   square_rank(to) != RANK_4
436           || !pos.square_is_empty(to)
437           || !pos.square_is_empty(from + DELTA_N))
438           return false;
439           break;
440
441       case DELTA_SS:
442       // Double black pawn push. The destination square must be on the fifth
443       // rank, and both the destination square and the square between the
444       // source and destination squares must be empty.
445           if (   square_rank(to) != RANK_5
446               || !pos.square_is_empty(to)
447               || !pos.square_is_empty(from + DELTA_S))
448               return false;
449           break;
450
451       default:
452           return false;
453       }
454   }
455   else if (!bit_is_set(pos.attacks_from(pc, from), to))
456       return false;
457
458   // The move is pseudo-legal, check if it is also legal
459   return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
460 }
461
462
463 namespace {
464
465   template<Square Delta>
466   inline Bitboard move_pawns(Bitboard p) {
467
468     return Delta == DELTA_N  ? p << 8 : Delta == DELTA_S  ? p >> 8 :
469            Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
470            Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
471   }
472
473   template<MoveType Type, Square Delta>
474   inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
475
476     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
477
478     Bitboard b;
479     Square to;
480
481     // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
482     b = move_pawns<Delta>(pawns) & target & ~TFileABB;
483     SERIALIZE_MOVES_D(b, -Delta);
484     return mlist;
485   }
486
487   template<Color Us, MoveType Type, Square Delta>
488   inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
489
490     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
491
492     Bitboard b;
493     Square to;
494
495     // Promotions and under-promotions, both captures and non-captures
496     b = move_pawns<Delta>(pawnsOn7) & target;
497
498     if (Delta != DELTA_N && Delta != DELTA_S)
499         b &= ~TFileABB;
500
501     while (b)
502     {
503         to = pop_1st_bit(&b);
504
505         if (Type == MV_CAPTURE || Type == MV_EVASION)
506             (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
507
508         if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
509         {
510             (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
511             (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
512             (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
513         }
514
515         // This is the only possible under promotion that can give a check
516         // not already included in the queen-promotion.
517         if (   Type == MV_CHECK
518             && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
519             (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
520         else (void)pos; // Silence a warning under MSVC
521     }
522     return mlist;
523   }
524
525   template<Color Us, MoveType Type>
526   MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
527
528     // Calculate our parametrized parameters at compile time, named
529     // according to the point of view of white side.
530     const Color    Them      = (Us == WHITE ? BLACK    : WHITE);
531     const Bitboard TRank7BB  = (Us == WHITE ? Rank7BB  : Rank2BB);
532     const Bitboard TRank3BB  = (Us == WHITE ? Rank3BB  : Rank6BB);
533     const Square   TDELTA_N  = (Us == WHITE ? DELTA_N  : DELTA_S);
534     const Square   TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
535     const Square   TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
536
537     Square to;
538     Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
539     Bitboard pawns = pos.pieces(PAWN, Us);
540     Bitboard pawnsOn7 = pawns & TRank7BB;
541     Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces_of_color(Them));
542
543     // Pre-calculate pawn pushes before changing emptySquares definition
544     if (Type != MV_CAPTURE)
545     {
546         emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
547         pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
548     }
549
550     if (Type == MV_EVASION)
551     {
552         emptySquares &= target; // Only blocking squares
553         enemyPieces  &= target; // Capture only the checker piece
554     }
555
556     // Promotions and underpromotions
557     if (pawnsOn7)
558     {
559         if (Type == MV_CAPTURE)
560             emptySquares = pos.empty_squares();
561
562         pawns &= ~TRank7BB;
563         mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
564         mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
565         mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
566     }
567
568     // Standard captures
569     if (Type == MV_CAPTURE || Type == MV_EVASION)
570     {
571         mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
572         mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
573     }
574
575     // Single and double pawn pushes
576     if (Type != MV_CAPTURE)
577     {
578         b1 = pawnPushes & emptySquares;
579         b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
580
581         if (Type == MV_CHECK)
582         {
583             // Consider only pawn moves which give direct checks
584             b1 &= pos.attacks_from<PAWN>(ksq, Them);
585             b2 &= pos.attacks_from<PAWN>(ksq, Them);
586
587             // Add pawn moves which gives discovered check. This is possible only
588             // if the pawn is not on the same file as the enemy king, because we
589             // don't generate captures.
590             if (pawns & target) // For CHECK type target is dc bitboard
591             {
592                 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
593                 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
594
595                 b1 |= dc1;
596                 b2 |= dc2;
597             }
598         }
599         SERIALIZE_MOVES_D(b1, -TDELTA_N);
600         SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
601     }
602
603     // En passant captures
604     if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
605     {
606         assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
607         assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
608
609         // An en passant capture can be an evasion only if the checking piece
610         // is the double pushed pawn and so is in the target. Otherwise this
611         // is a discovery check and we are forced to do otherwise.
612         if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
613             return mlist;
614
615         b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
616
617         assert(b1 != EmptyBoardBB);
618
619         while (b1)
620         {
621             to = pop_1st_bit(&b1);
622             (*mlist++).move = make_ep_move(to, pos.ep_square());
623         }
624     }
625     return mlist;
626   }
627
628   template<CastlingSide Side>
629   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
630
631     Color them = opposite_color(us);
632     Square ksq = pos.king_square(us);
633
634     assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
635
636     Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
637     Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
638     Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
639     Square s;
640     bool illegal = false;
641
642     assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
643
644     // It is a bit complicated to correctly handle Chess960
645     for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
646         if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
647             ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
648             illegal = true;
649
650     for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
651         if (s != ksq && s != rsq && pos.square_is_occupied(s))
652             illegal = true;
653
654     if (   Side == QUEEN_SIDE
655         && square_file(rsq) == FILE_B
656         && (   pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
657             || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
658         illegal = true;
659
660     if (!illegal)
661         (*mlist++).move = make_castle_move(ksq, rsq);
662
663     return mlist;
664   }
665
666 } // namespace