]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Split calculation of pinners from dc candidates
[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) {
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   Bitboard dc = pos.discovered_check_candidates(us);
177   MoveStack* mlist_start = mlist;
178
179   assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
180
181   // Pieces moves
182   mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
183   mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
184   mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
185   mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
186   mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
187   mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
188
189   // Castling moves that give check. Very rare but nice to have!
190   if (   pos.can_castle_queenside(us)
191       && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
192       && castling_is_check(pos, QUEEN_SIDE))
193       mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
194
195   if (   pos.can_castle_kingside(us)
196       && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
197       && castling_is_check(pos, KING_SIDE))
198       mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
199
200   return int(mlist - mlist_start);
201 }
202
203
204 /// generate_evasions() generates all check evasions when the side to move is
205 /// in check. Unlike the other move generation functions, this one generates
206 /// only legal moves. It returns the number of generated moves.
207
208 int generate_evasions(const Position& pos, MoveStack* mlist) {
209
210   assert(pos.is_ok());
211   assert(pos.is_check());
212
213   Square from, to;
214   Color us = pos.side_to_move();
215   Color them = opposite_color(us);
216   Square ksq = pos.king_square(us);
217   MoveStack* mlist_start = mlist;
218
219   assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
220
221   // The bitboard of occupied pieces without our king
222   Bitboard b_noKing = pos.occupied_squares();
223   clear_bit(&b_noKing, ksq);
224
225   // Find squares attacked by slider checkers, we will
226   // remove them from king evasions set so to avoid a couple
227   // of cycles in the slow king evasions legality check loop
228   // and to be able to use square_is_attacked().
229   Bitboard checkers = pos.checkers();
230   Bitboard checkersAttacks = EmptyBoardBB;
231   Bitboard b = checkers & (pos.queens() | pos.bishops());
232   while (b)
233   {
234       from = pop_1st_bit(&b);
235       checkersAttacks |= bishop_attacks_bb(from, b_noKing);
236   }
237
238   b = checkers & (pos.queens() | pos.rooks());
239   while (b)
240   {
241       from = pop_1st_bit(&b);
242       checkersAttacks |= rook_attacks_bb(from, b_noKing);
243   }
244
245   // Generate evasions for king
246   Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
247   while (b1)
248   {
249       to = pop_1st_bit(&b1);
250       // Note that we can use square_is_attacked() only because we
251       // have already removed slider checkers.
252       if (!pos.square_is_attacked(to, them))
253           (*mlist++).move = make_move(ksq, to);
254   }
255
256   // Generate evasions for other pieces only if not double check. We use a
257   // simple bit twiddling hack here rather than calling count_1s in order to
258   // save some time (we know that pos.checkers() has at most two nonzero bits).
259   if (!(checkers & (checkers - 1))) // Only one bit set?
260   {
261       Square checksq = first_1(checkers);
262       Bitboard pinned = pos.pinned_pieces(us);
263
264       assert(pos.color_of_piece_on(checksq) == them);
265
266       // Generate captures of the checking piece
267
268       // Pawn captures
269       b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
270       while (b1)
271       {
272           from = pop_1st_bit(&b1);
273           if (relative_rank(us, checksq) == RANK_8)
274           {
275               (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
276               (*mlist++).move = make_promotion_move(from, checksq, ROOK);
277               (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
278               (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
279           } else
280               (*mlist++).move = make_move(from, checksq);
281       }
282
283       // Pieces captures
284       b1 = (  (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
285             | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
286             | (pos.piece_attacks<ROOK>(checksq)   & pos.rooks_and_queens(us)) ) & ~pinned;
287
288       while (b1)
289       {
290           from = pop_1st_bit(&b1);
291           (*mlist++).move = make_move(from, checksq);
292       }
293
294       // Blocking check evasions are possible only if the checking piece is
295       // a slider.
296       if (checkers & pos.sliders())
297       {
298           Bitboard blockSquares = squares_between(checksq, ksq);
299
300           assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
301
302           if (blockSquares != EmptyBoardBB)
303           {
304               mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
305               mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
306               mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
307               mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
308               mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
309           }
310       }
311
312       // Finally, the special case of en passant captures. An en passant
313       // capture can only be a check evasion if the check is not a discovered
314       // check. If pos.ep_square() is set, the last move made must have been
315       // a double pawn push. If, furthermore, the checking piece is a pawn,
316       // an en passant check evasion may be possible.
317       if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
318       {
319           to = pos.ep_square();
320           b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
321
322           // The checking pawn cannot be a discovered (bishop) check candidate
323           // otherwise we were in check also before last double push move.
324           assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
325           assert(count_1s(b1) == 1 || count_1s(b1) == 2);
326
327           b1 &= ~pinned;
328           while (b1)
329           {
330               from = pop_1st_bit(&b1);
331               // Move is always legal because checking pawn is not a discovered
332               // check candidate and our capturing pawn has been already tested
333               // against pinned pieces.
334               (*mlist++).move = make_ep_move(from, to);
335           }
336       }
337   }
338   return int(mlist - mlist_start);
339 }
340
341
342 /// generate_legal_moves() computes a complete list of legal moves in the
343 /// current position. This function is not very fast, and should be used
344 /// only in situations where performance is unimportant. It wouldn't be
345 /// very hard to write an efficient legal move generator, but for the moment
346 /// we don't need it.
347
348 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
349
350   assert(pos.is_ok());
351
352   if (pos.is_check())
353       return generate_evasions(pos, mlist);
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))
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) {
375
376   assert(pos.is_ok());
377   assert(!pos.is_check());
378   assert(move_is_ok(m));
379
380   Color us = pos.side_to_move();
381   Color them = opposite_color(us);
382   Square from = move_from(m);
383   Piece pc = pos.piece_on(from);
384
385   // If the from square is not occupied by a piece belonging to the side to
386   // move, the move is obviously not legal.
387   if (color_of_piece(pc) != us)
388       return false;
389
390   Square to = move_to(m);
391
392   // En passant moves
393   if (move_is_ep(m))
394   {
395       // The piece must be a pawn and destination square must be the
396       // en passant square.
397       if (   type_of_piece(pc) != PAWN
398           || to != pos.ep_square())
399           return false;
400
401       assert(pos.square_is_empty(to));
402       assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
403
404       // The move is pseudo-legal, check if it is also legal
405       return pos.pl_move_is_legal(m);
406   }
407
408   // Castling moves
409   if (move_is_short_castle(m))
410   {
411       // The piece must be a king and side to move must still have
412       // the right to castle kingside.
413       if (   type_of_piece(pc) != KING
414           ||!pos.can_castle_kingside(us))
415           return false;
416
417       assert(from == pos.king_square(us));
418       assert(to == pos.initial_kr_square(us));
419       assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
420
421       Square g1 = relative_square(us, SQ_G1);
422       Square f1 = relative_square(us, SQ_F1);
423       Square s;
424       bool illegal = false;
425
426       // Check if any of the squares between king and rook
427       // is occupied or under attack.
428       for (s = Min(from, g1); s <= Max(from, g1); s++)
429           if (  (s != from && s != to && !pos.square_is_empty(s))
430               || pos.square_is_attacked(s, them))
431               illegal = true;
432
433       // Check if any of the squares between king and rook
434       // is occupied.
435       for (s = Min(to, f1); s <= Max(to, f1); s++)
436           if (s != from && s != to && !pos.square_is_empty(s))
437               illegal = true;
438
439       return !illegal;
440   }
441
442   if (move_is_long_castle(m))
443   {
444       // The piece must be a king and side to move must still have
445       // the right to castle kingside.
446       if (   type_of_piece(pc) != KING
447           ||!pos.can_castle_queenside(us))
448           return false;
449
450       assert(from == pos.king_square(us));
451       assert(to == pos.initial_qr_square(us));
452       assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
453
454       Square c1 = relative_square(us, SQ_C1);
455       Square d1 = relative_square(us, SQ_D1);
456       Square s;
457       bool illegal = false;
458
459       for (s = Min(from, c1); s <= Max(from, c1); s++)
460           if(  (s != from && s != to && !pos.square_is_empty(s))
461              || pos.square_is_attacked(s, them))
462               illegal = true;
463
464       for (s = Min(to, d1); s <= Max(to, d1); s++)
465           if(s != from && s != to && !pos.square_is_empty(s))
466               illegal = true;
467
468       if (   square_file(to) == FILE_B
469           && (   pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, ROOK)
470               || pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, QUEEN)))
471           illegal = true;
472
473       return !illegal;
474   }
475
476   // Normal moves
477
478   // The destination square cannot be occupied by a friendly piece
479   if (pos.color_of_piece_on(to) == us)
480       return false;
481
482   // Proceed according to the type of the moving piece.
483   if (type_of_piece(pc) == PAWN)
484   {
485       // If the destination square is on the 8/1th rank, the move must
486       // be a promotion.
487       if (   (  (square_rank(to) == RANK_8 && us == WHITE)
488               ||(square_rank(to) == RANK_1 && us != WHITE))
489            && !move_promotion(m))
490           return false;
491
492       // Proceed according to the square delta between the source and
493       // destionation squares.
494       switch (to - from)
495       {
496       case DELTA_NW:
497       case DELTA_NE:
498       case DELTA_SW:
499       case DELTA_SE:
500       // Capture. The destination square must be occupied by an enemy
501       // piece (en passant captures was handled earlier).
502           if (pos.color_of_piece_on(to) != them)
503               return false;
504           break;
505
506       case DELTA_N:
507       case DELTA_S:
508       // Pawn push. The destination square must be empty.
509           if (!pos.square_is_empty(to))
510               return false;
511           break;
512
513       case DELTA_NN:
514       // Double white pawn push. The destination square must be on the fourth
515       // rank, and both the destination square and the square between the
516       // source and destination squares must be empty.
517       if (   square_rank(to) != RANK_4
518           || !pos.square_is_empty(to)
519           || !pos.square_is_empty(from + DELTA_N))
520           return false;
521           break;
522
523       case DELTA_SS:
524       // Double black pawn push. The destination square must be on the fifth
525       // rank, and both the destination square and the square between the
526       // source and destination squares must be empty.
527           if (   square_rank(to) != RANK_5
528               || !pos.square_is_empty(to)
529               || !pos.square_is_empty(from + DELTA_S))
530               return false;
531           break;
532
533       default:
534           return false;
535       }
536       // The move is pseudo-legal, check if it is also legal
537       return pos.pl_move_is_legal(m);
538   }
539
540   // Luckly we can handle all the other pieces in one go
541   return (   pos.piece_attacks_square(pos.piece_on(from), from, to)
542           && pos.pl_move_is_legal(m)
543           && !move_promotion(m));
544 }
545
546
547 namespace {
548
549   template<PieceType Piece>
550   MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
551
552     Square from;
553     Bitboard b;
554
555     for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
556     {
557         from = pos.piece_list(us, Piece, i);
558         b = pos.piece_attacks<Piece>(from) & target;
559         SERIALIZE_MOVES(b);
560     }
561     return mlist;
562   }
563
564   template<PieceType Piece>
565   MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
566                                   Color us, Bitboard target, Bitboard pinned) {
567     Square from;
568     Bitboard b;
569
570     for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
571     {
572         from = pos.piece_list(us, Piece, i);
573         if (pinned && bit_is_set(pinned, from))
574             continue;
575
576         b = pos.piece_attacks<Piece>(from) & target;
577         SERIALIZE_MOVES(b);
578     }
579     return mlist;
580   }
581
582   template<>
583   MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
584
585     Bitboard b;
586     Square from = pos.king_square(us);
587
588     b = pos.piece_attacks<KING>(from) & target;
589     SERIALIZE_MOVES(b);
590     return mlist;
591   }
592
593   template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
594            SquareDelta TDELTA_NW, SquareDelta TDELTA_N
595           >
596   MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
597
598     Square to;
599     Bitboard pawns = pos.pawns(Us);
600     Bitboard enemyPieces = pos.pieces_of_color(Them);
601
602     // Captures in the a1-h8 (a8-h1 for black) direction
603     Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
604
605     // Capturing promotions
606     Bitboard b2 = b1 & TRank8BB;
607     while (b2)
608     {
609         to = pop_1st_bit(&b2);
610         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
611     }
612
613     // Capturing non-promotions
614     b2 = b1 & ~TRank8BB;
615     while (b2)
616     {
617         to = pop_1st_bit(&b2);
618         (*mlist++).move = make_move(to - TDELTA_NE, to);
619     }
620
621     // Captures in the h1-a8 (h8-a1 for black) direction
622     b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
623
624     // Capturing promotions
625     b2 = b1 & TRank8BB;
626     while (b2)
627     {
628         to = pop_1st_bit(&b2);
629         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
630     }
631
632     // Capturing non-promotions
633     b2 = b1 & ~TRank8BB;
634     while (b2)
635     {
636         to = pop_1st_bit(&b2);
637         (*mlist++).move = make_move(to - TDELTA_NW, to);
638     }
639
640     // Non-capturing promotions
641     b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
642     while (b1)
643     {
644         to = pop_1st_bit(&b1);
645         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
646     }
647
648     // En passant captures
649     if (pos.ep_square() != SQ_NONE)
650     {
651         assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
652         assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
653
654         b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
655         assert(b1 != EmptyBoardBB);
656
657         while (b1)
658         {
659             to = pop_1st_bit(&b1);
660             (*mlist++).move = make_ep_move(to, pos.ep_square());
661         }
662     }
663     return mlist;
664   }
665
666   template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
667            SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
668           >
669   MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
670
671     Bitboard pawns = pos.pawns(Us);
672     Bitboard enemyPieces = pos.pieces_of_color(Them);
673     Bitboard emptySquares = pos.empty_squares();
674     Bitboard b1, b2;
675     Square to;
676
677     // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
678     b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
679     while (b1)
680     {
681         to = pop_1st_bit(&b1);
682         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
683         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
684         (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
685     }
686
687     // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
688     b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
689     while (b1)
690     {
691         to = pop_1st_bit(&b1);
692         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
693         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
694         (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
695     }
696
697     // Single pawn pushes
698     b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
699     b2 = b1 & TRank8BB;
700     while (b2)
701     {
702         to = pop_1st_bit(&b2);
703         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
704         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
705         (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
706     }
707     b2 = b1 & ~TRank8BB;
708     while (b2)
709     {
710         to = pop_1st_bit(&b2);
711         (*mlist++).move = make_move(to - TDELTA_N, to);
712     }
713
714     // Double pawn pushes
715     b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
716     while (b2)
717     {
718         to = pop_1st_bit(&b2);
719         (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
720     }
721     return mlist;
722   }
723
724
725   template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
726   MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
727   {
728     // Find all friendly pawns not on the enemy king's file
729     Bitboard b1, b2, b3;
730     Bitboard empty = pos.empty_squares();
731
732     if (dc != EmptyBoardBB)
733     {
734         // Pawn moves which gives discovered check. This is possible only if the
735         // pawn is not on the same file as the enemy king, because we don't
736         // generate captures.
737         b1 = pos.pawns(Us) & ~file_bb(ksq);
738
739         // Discovered checks, single pawn pushes, no promotions
740         b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & empty & ~TRank8BB;
741         while (b3)
742         {
743             Square to = pop_1st_bit(&b3);
744             (*mlist++).move = make_move(to - TDELTA_N, to);
745         }
746
747         // Discovered checks, double pawn pushes
748         b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
749         while (b3)
750         {
751             Square to = pop_1st_bit(&b3);
752             (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
753         }
754     }
755
756     // Direct checks. These are possible only for pawns on neighboring files
757     // of the enemy king.
758     b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
759
760     // Direct checks, single pawn pushes
761     b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
762     b3 = b2 & pos.pawn_attacks(Them, ksq);
763     while (b3)
764     {
765         Square to = pop_1st_bit(&b3);
766         (*mlist++).move = make_move(to - TDELTA_N, to);
767     }
768
769     // Direct checks, double pawn pushes
770     b3 =  (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
771         & empty
772         & pos.pawn_attacks(Them, ksq);
773     while (b3)
774     {
775         Square to = pop_1st_bit(&b3);
776         (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
777     }
778     return mlist;
779   }
780
781   template<PieceType Piece>
782   MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
783                                    Bitboard dc, Square ksq) {
784
785     Bitboard target = pos.pieces_of_color_and_type(us, Piece);
786
787     // Discovered checks
788     Bitboard b = target & dc;
789     while (b)
790     {
791         Square from = pop_1st_bit(&b);
792         Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
793         if (Piece == KING)
794             bb &= ~QueenPseudoAttacks[ksq];
795
796         SERIALIZE_MOVES(bb);
797     }
798
799     // Direct checks
800     b = target & ~dc;
801     if (Piece == KING || !b)
802         return mlist;
803
804     Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
805     while (b)
806     {
807         Square from = pop_1st_bit(&b);
808         Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
809         SERIALIZE_MOVES(bb);
810     }
811     return mlist;
812   }
813
814   template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
815   MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
816                                              Bitboard blockSquares, MoveStack* mlist) {
817     Square to;
818
819     // Find non-pinned pawns
820     Bitboard b1 = pos.pawns(Us) & ~pinned;
821
822     // Single pawn pushes. We don't have to AND with empty squares here,
823     // because the blocking squares will always be empty.
824     Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
825     while (b2)
826     {
827         to = pop_1st_bit(&b2);
828
829         assert(pos.piece_on(to) == EMPTY);
830
831         if (square_rank(to) == TRANK_8)
832         {
833             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
834             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
835             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
836             (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
837         } else
838             (*mlist++).move = make_move(to - TDELTA_N, to);
839     }
840
841     // Double pawn pushes
842     b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
843     b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;
844     while (b2)
845     {
846         to = pop_1st_bit(&b2);
847
848         assert(pos.piece_on(to) == EMPTY);
849         assert(Us != WHITE || square_rank(to) == RANK_4);
850         assert(Us != BLACK || square_rank(to) == RANK_5);
851
852         (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
853     }
854     return mlist;
855   }
856
857   template<CastlingSide Side>
858   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
859
860     Color us = pos.side_to_move();
861
862     if (  (Side == KING_SIDE && pos.can_castle_kingside(us))
863         ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
864     {
865         Color them = opposite_color(us);
866         Square ksq = pos.king_square(us);
867
868         assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
869
870         Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
871         Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
872         Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
873         Square s;
874         bool illegal = false;
875
876         assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
877
878         // It is a bit complicated to correctly handle Chess960
879         for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
880             if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
881                 || pos.square_is_attacked(s, them))
882                 illegal = true;
883
884         for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
885             if (s != ksq && s != rsq && pos.square_is_occupied(s))
886                 illegal = true;
887
888         if (   Side == QUEEN_SIDE
889             && square_file(rsq) == FILE_B
890             && (   pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
891                 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
892             illegal = true;
893
894         if (!illegal)
895             (*mlist++).move = make_castle_move(ksq, rsq);
896     }
897     return mlist;
898   }
899
900   bool castling_is_check(const Position& pos, CastlingSide side) {
901
902     // After castling opponent king is attacked by the castled rook?
903     File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
904     Color us = pos.side_to_move();
905     Square ksq = pos.king_square(us);
906     Bitboard occ = pos.occupied_squares();
907
908     clear_bit(&occ, ksq); // Remove our king from the board
909     Square rsq = make_square(rookFile, square_rank(ksq));
910     return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));
911   }
912 }