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