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-2014 Marco Costalba, Joona Kiiski, Tord Romstad
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.
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.
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/>.
25 /// Simple macro to wrap a very common while loop, no fancy, no flexibility,
26 /// hardcoded names 'mlist' and 'from'.
27 #define SERIALIZE(b) while (b) (mlist++)->move = make_move(from, pop_lsb(&b))
29 /// Version used for pawns, where the 'from' square is given as a delta from the 'to' square
30 #define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_lsb(&b); \
31 (mlist++)->move = make_move(to - (d), to); }
34 template<CastlingSide Side, bool Checks, bool Chess960>
35 ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
37 if (pos.castling_impeded(us, Side) || !pos.can_castle(make_castling_flag(us, Side)))
40 // After castling, the rook and king final positions are the same in Chess960
41 // as they would be in standard chess.
42 Square kfrom = pos.king_square(us);
43 Square rfrom = pos.castling_rook_square(us, Side);
44 Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
45 Bitboard enemies = pos.pieces(~us);
47 assert(!pos.checkers());
49 const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
50 : Side == KING_SIDE ? DELTA_W : DELTA_E;
52 for (Square s = kto; s != kfrom; s += K)
53 if (pos.attackers_to(s) & enemies)
56 // Because we generate only legal castling moves we need to verify that
57 // when moving the castling rook we do not discover some hidden checker.
58 // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
59 if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
62 Move m = make<CASTLING>(kfrom, rfrom);
64 if (Checks && !pos.gives_check(m, *ci))
73 template<GenType Type, Square Delta>
74 inline ExtMove* generate_promotions(ExtMove* mlist, Bitboard pawnsOn7,
75 Bitboard target, const CheckInfo* ci) {
77 Bitboard b = shift_bb<Delta>(pawnsOn7) & target;
81 Square to = pop_lsb(&b);
83 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
84 (mlist++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
86 if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
88 (mlist++)->move = make<PROMOTION>(to - Delta, to, ROOK);
89 (mlist++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
90 (mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
93 // Knight promotion is the only promotion that can give a direct check
94 // that's not already included in the queen promotion.
95 if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
96 (mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
98 (void)ci; // Silence a warning under MSVC
105 template<Color Us, GenType Type>
106 ExtMove* generate_pawn_moves(const Position& pos, ExtMove* mlist,
107 Bitboard target, const CheckInfo* ci) {
109 // Compute our parametrized parameters at compile time, named according to
110 // the point of view of white side.
111 const Color Them = (Us == WHITE ? BLACK : WHITE);
112 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
113 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
114 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
115 const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
116 const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
117 const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
119 Bitboard b1, b2, dc1, dc2, emptySquares;
121 Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
122 Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
124 Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
125 Type == CAPTURES ? target : pos.pieces(Them));
127 // Single and double pawn pushes, no promotions
128 if (Type != CAPTURES)
130 emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
132 b1 = shift_bb<Up>(pawnsNotOn7) & emptySquares;
133 b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
135 if (Type == EVASIONS) // Consider only blocking squares
141 if (Type == QUIET_CHECKS)
143 b1 &= pos.attacks_from<PAWN>(ci->ksq, Them);
144 b2 &= pos.attacks_from<PAWN>(ci->ksq, Them);
146 // Add pawn pushes which give discovered check. This is possible only
147 // if the pawn is not on the same file as the enemy king, because we
148 // don't generate captures. Note that a possible discovery check
149 // promotion has been already generated amongst the captures.
150 if (pawnsNotOn7 & ci->dcCandidates)
152 dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
153 dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
160 SERIALIZE_PAWNS(b1, Up);
161 SERIALIZE_PAWNS(b2, Up + Up);
164 // Promotions and underpromotions
165 if (pawnsOn7 && (Type != EVASIONS || (target & TRank8BB)))
167 if (Type == CAPTURES)
168 emptySquares = ~pos.pieces();
170 if (Type == EVASIONS)
171 emptySquares &= target;
173 mlist = generate_promotions<Type, Right>(mlist, pawnsOn7, enemies, ci);
174 mlist = generate_promotions<Type, Left >(mlist, pawnsOn7, enemies, ci);
175 mlist = generate_promotions<Type, Up>(mlist, pawnsOn7, emptySquares, ci);
178 // Standard and en-passant captures
179 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
181 b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
182 b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
184 SERIALIZE_PAWNS(b1, Right);
185 SERIALIZE_PAWNS(b2, Left);
187 if (pos.ep_square() != SQ_NONE)
189 assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
191 // An en passant capture can be an evasion only if the checking piece
192 // is the double pushed pawn and so is in the target. Otherwise this
193 // is a discovery check and we are forced to do otherwise.
194 if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
197 b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
202 (mlist++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
210 template<PieceType Pt, bool Checks> FORCE_INLINE
211 ExtMove* generate_moves(const Position& pos, ExtMove* mlist, Color us,
212 Bitboard target, const CheckInfo* ci) {
214 assert(Pt != KING && Pt != PAWN);
216 const Square* pl = pos.list<Pt>(us);
218 for (Square from = *pl; from != SQ_NONE; from = *++pl)
222 if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
223 && !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
226 if (unlikely(ci->dcCandidates) && (ci->dcCandidates & from))
230 Bitboard b = pos.attacks_from<Pt>(from) & target;
233 b &= ci->checkSq[Pt];
242 template<Color Us, GenType Type> FORCE_INLINE
243 ExtMove* generate_all(const Position& pos, ExtMove* mlist, Bitboard target,
244 const CheckInfo* ci = NULL) {
246 const bool Checks = Type == QUIET_CHECKS;
248 mlist = generate_pawn_moves<Us, Type>(pos, mlist, target, ci);
249 mlist = generate_moves<KNIGHT, Checks>(pos, mlist, Us, target, ci);
250 mlist = generate_moves<BISHOP, Checks>(pos, mlist, Us, target, ci);
251 mlist = generate_moves< ROOK, Checks>(pos, mlist, Us, target, ci);
252 mlist = generate_moves< QUEEN, Checks>(pos, mlist, Us, target, ci);
254 if (Type != QUIET_CHECKS && Type != EVASIONS)
256 Square from = pos.king_square(Us);
257 Bitboard b = pos.attacks_from<KING>(from) & target;
261 if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
263 if (pos.is_chess960())
265 mlist = generate_castling< KING_SIDE, Checks, true>(pos, mlist, Us, ci);
266 mlist = generate_castling<QUEEN_SIDE, Checks, true>(pos, mlist, Us, ci);
270 mlist = generate_castling< KING_SIDE, Checks, false>(pos, mlist, Us, ci);
271 mlist = generate_castling<QUEEN_SIDE, Checks, false>(pos, mlist, Us, ci);
282 /// generate<CAPTURES> generates all pseudo-legal captures and queen
283 /// promotions. Returns a pointer to the end of the move list.
285 /// generate<QUIETS> generates all pseudo-legal non-captures and
286 /// underpromotions. Returns a pointer to the end of the move list.
288 /// generate<NON_EVASIONS> generates all pseudo-legal captures and
289 /// non-captures. Returns a pointer to the end of the move list.
291 template<GenType Type>
292 ExtMove* generate(const Position& pos, ExtMove* mlist) {
294 assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
295 assert(!pos.checkers());
297 Color us = pos.side_to_move();
299 Bitboard target = Type == CAPTURES ? pos.pieces(~us)
300 : Type == QUIETS ? ~pos.pieces()
301 : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
303 return us == WHITE ? generate_all<WHITE, Type>(pos, mlist, target)
304 : generate_all<BLACK, Type>(pos, mlist, target);
307 // Explicit template instantiations
308 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
309 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
310 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
313 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
314 /// underpromotions that give check. Returns a pointer to the end of the move list.
316 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* mlist) {
318 assert(!pos.checkers());
320 Color us = pos.side_to_move();
322 Bitboard dc = ci.dcCandidates;
326 Square from = pop_lsb(&dc);
327 PieceType pt = type_of(pos.piece_on(from));
330 continue; // Will be generated together with direct checks
332 Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
335 b &= ~PseudoAttacks[QUEEN][ci.ksq];
340 return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci)
341 : generate_all<BLACK, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci);
345 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
346 /// to move is in check. Returns a pointer to the end of the move list.
348 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* mlist) {
350 assert(pos.checkers());
353 Color us = pos.side_to_move();
354 Square ksq = pos.king_square(us), from = ksq /* For SERIALIZE */, checksq;
355 Bitboard sliderAttacks = 0;
356 Bitboard b = pos.checkers();
358 assert(pos.checkers());
360 // Find all the squares attacked by slider checkers. We will remove them from
361 // the king evasions in order to skip known illegal moves, which avoids any
362 // useless legality checks later on.
366 checksq = pop_lsb(&b);
368 assert(color_of(pos.piece_on(checksq)) == ~us);
370 if (type_of(pos.piece_on(checksq)) > KNIGHT) // A slider
371 sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
375 // Generate evasions for king, capture and non capture moves
376 b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
380 return mlist; // Double check, only a king move can save the day
382 // Generate blocking evasions or captures of the checking piece
383 Bitboard target = between_bb(checksq, ksq) | checksq;
385 return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, mlist, target)
386 : generate_all<BLACK, EVASIONS>(pos, mlist, target);
390 /// generate<LEGAL> generates all the legal moves in the given position
393 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* mlist) {
395 ExtMove *end, *cur = mlist;
396 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
397 Square ksq = pos.king_square(pos.side_to_move());
399 end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
400 : generate<NON_EVASIONS>(pos, mlist);
402 if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
403 && !pos.legal(cur->move, pinned))
404 cur->move = (--end)->move;