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