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