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