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