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