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