]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Tweak cutnode reduction
[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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cassert>
22
23 #include "movegen.h"
24 #include "position.h"
25
26 namespace {
27
28   template<GenType Type, Direction D>
29   ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
30
31     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
32     {
33         *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
34         if (attacks_bb<KNIGHT>(to) & ksq)
35             *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
36     }
37
38     if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
39     {
40         *moveList++ = make<PROMOTION>(to - D, to, ROOK);
41         *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
42         if (!(attacks_bb<KNIGHT>(to) & ksq))
43             *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
44     }
45
46     return moveList;
47   }
48
49
50   template<Color Us, GenType Type>
51   ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
52
53     constexpr Color     Them     = ~Us;
54     constexpr Bitboard  TRank7BB = (Us == WHITE ? Rank7BB    : Rank2BB);
55     constexpr Bitboard  TRank3BB = (Us == WHITE ? Rank3BB    : Rank6BB);
56     constexpr Direction Up       = pawn_push(Us);
57     constexpr Direction UpRight  = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
58     constexpr Direction UpLeft   = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
59
60     const Square ksq = pos.square<KING>(Them);
61     Bitboard emptySquares;
62
63     Bitboard pawnsOn7    = pos.pieces(Us, PAWN) &  TRank7BB;
64     Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
65
66     Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
67                         Type == CAPTURES ? target : pos.pieces(Them));
68
69     // Single and double pawn pushes, no promotions
70     if (Type != CAPTURES)
71     {
72         emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
73
74         Bitboard b1 = shift<Up>(pawnsNotOn7)   & emptySquares;
75         Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
76
77         if (Type == EVASIONS) // Consider only blocking squares
78         {
79             b1 &= target;
80             b2 &= target;
81         }
82
83         if (Type == QUIET_CHECKS)
84         {
85             b1 &= pawn_attacks_bb(Them, ksq);
86             b2 &= pawn_attacks_bb(Them, ksq);
87
88             // Add pawn pushes which give discovered check. This is possible only
89             // if the pawn is not on the same file as the enemy king, because we
90             // don't generate captures. Note that a possible discovery check
91             // promotion has been already generated amongst the captures.
92             Bitboard dcCandidateQuiets = pos.blockers_for_king(Them) & pawnsNotOn7;
93             if (dcCandidateQuiets)
94             {
95                 Bitboard dc1 = shift<Up>(dcCandidateQuiets) & emptySquares & ~file_bb(ksq);
96                 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
97
98                 b1 |= dc1;
99                 b2 |= dc2;
100             }
101         }
102
103         while (b1)
104         {
105             Square to = pop_lsb(&b1);
106             *moveList++ = make_move(to - Up, to);
107         }
108
109         while (b2)
110         {
111             Square to = pop_lsb(&b2);
112             *moveList++ = make_move(to - Up - Up, to);
113         }
114     }
115
116     // Promotions and underpromotions
117     if (pawnsOn7)
118     {
119         if (Type == CAPTURES)
120             emptySquares = ~pos.pieces();
121
122         if (Type == EVASIONS)
123             emptySquares &= target;
124
125         Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
126         Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
127         Bitboard b3 = shift<Up     >(pawnsOn7) & emptySquares;
128
129         while (b1)
130             moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
131
132         while (b2)
133             moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
134
135         while (b3)
136             moveList = make_promotions<Type, Up     >(moveList, pop_lsb(&b3), ksq);
137     }
138
139     // Standard and en-passant captures
140     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
141     {
142         Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
143         Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
144
145         while (b1)
146         {
147             Square to = pop_lsb(&b1);
148             *moveList++ = make_move(to - UpRight, to);
149         }
150
151         while (b2)
152         {
153             Square to = pop_lsb(&b2);
154             *moveList++ = make_move(to - UpLeft, to);
155         }
156
157         if (pos.ep_square() != SQ_NONE)
158         {
159             assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
160
161             // An en passant capture can be an evasion only if the checking piece
162             // is the double pushed pawn and so is in the target. Otherwise this
163             // is a discovery check and we are forced to do otherwise.
164             if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
165                 return moveList;
166
167             b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
168
169             assert(b1);
170
171             while (b1)
172                 *moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
173         }
174     }
175
176     return moveList;
177   }
178
179
180   template<Color Us, PieceType Pt, bool Checks>
181   ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
182
183     static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
184
185     const Square* pl = pos.squares<Pt>(Us);
186
187     for (Square from = *pl; from != SQ_NONE; from = *++pl)
188     {
189         if (Checks)
190         {
191             if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
192                 && !(attacks_bb<Pt>(from) & target & pos.check_squares(Pt)))
193                 continue;
194
195             if (pos.blockers_for_king(~Us) & from)
196                 continue;
197         }
198
199         Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
200
201         if (Checks)
202             b &= pos.check_squares(Pt);
203
204         while (b)
205             *moveList++ = make_move(from, pop_lsb(&b));
206     }
207
208     return moveList;
209   }
210
211
212   template<Color Us, GenType Type>
213   ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
214     constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantations
215     Bitboard target;
216
217     switch (Type)
218     {
219         case CAPTURES:
220             target =  pos.pieces(~Us);
221             break;
222         case QUIETS:
223         case QUIET_CHECKS:
224             target = ~pos.pieces();
225             break;
226         case EVASIONS:
227         {
228             Square checksq = lsb(pos.checkers());
229             target = between_bb(pos.square<KING>(Us), checksq) | checksq;
230             break;
231         }
232         case NON_EVASIONS:
233             target = ~pos.pieces(Us);
234             break;
235         default:
236             static_assert(true, "Unsupported type in generate_all()");
237     }
238
239     moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
240     moveList = generate_moves<Us, KNIGHT, Checks>(pos, moveList, target);
241     moveList = generate_moves<Us, BISHOP, Checks>(pos, moveList, target);
242     moveList = generate_moves<Us,   ROOK, Checks>(pos, moveList, target);
243     moveList = generate_moves<Us,  QUEEN, Checks>(pos, moveList, target);
244
245     if (Type != QUIET_CHECKS && Type != EVASIONS)
246     {
247         Square ksq = pos.square<KING>(Us);
248         Bitboard b = attacks_bb<KING>(ksq) & target;
249         while (b)
250             *moveList++ = make_move(ksq, pop_lsb(&b));
251
252         if ((Type != CAPTURES) && pos.can_castle(Us & ANY_CASTLING))
253             for(CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
254                 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
255                     *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
256     }
257
258     return moveList;
259   }
260
261 } // namespace
262
263
264 /// <CAPTURES>     Generates all pseudo-legal captures plus queen and checking knight promotions
265 /// <QUIETS>       Generates all pseudo-legal non-captures and underpromotions(except checking knight)
266 /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
267 ///
268 /// Returns a pointer to the end of the move list.
269
270 template<GenType Type>
271 ExtMove* generate(const Position& pos, ExtMove* moveList) {
272
273   static_assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS, "Unsupported type in generate()");
274   assert(!pos.checkers());
275
276   Color us = pos.side_to_move();
277
278   return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
279                      : generate_all<BLACK, Type>(pos, moveList);
280 }
281
282 // Explicit template instantiations
283 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
284 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
285 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
286
287
288 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures.
289 /// Returns a pointer to the end of the move list.
290 template<>
291 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
292
293   assert(!pos.checkers());
294
295   Color us = pos.side_to_move();
296   Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us) & ~pos.pieces(PAWN);
297
298   while (dc)
299   {
300      Square from = pop_lsb(&dc);
301      PieceType pt = type_of(pos.piece_on(from));
302
303      Bitboard b = attacks_bb(pt, from, pos.pieces()) & ~pos.pieces();
304
305      if (pt == KING)
306          b &= ~attacks_bb<QUEEN>(pos.square<KING>(~us));
307
308      while (b)
309          *moveList++ = make_move(from, pop_lsb(&b));
310   }
311
312   return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList)
313                      : generate_all<BLACK, QUIET_CHECKS>(pos, moveList);
314 }
315
316
317 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
318 /// to move is in check. Returns a pointer to the end of the move list.
319 template<>
320 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
321
322   assert(pos.checkers());
323
324   Color us = pos.side_to_move();
325   Square ksq = pos.square<KING>(us);
326   Bitboard sliderAttacks = 0;
327   Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
328
329   // Find all the squares attacked by slider checkers. We will remove them from
330   // the king evasions in order to skip known illegal moves, which avoids any
331   // useless legality checks later on.
332   while (sliders)
333       sliderAttacks |= line_bb(ksq, pop_lsb(&sliders)) & ~pos.checkers();
334
335   // Generate evasions for king, capture and non capture moves
336   Bitboard b = attacks_bb<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
337   while (b)
338       *moveList++ = make_move(ksq, pop_lsb(&b));
339
340   if (more_than_one(pos.checkers()))
341       return moveList; // Double check, only a king move can save the day
342
343   // Generate blocking evasions or captures of the checking piece
344   return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList)
345                      : generate_all<BLACK, EVASIONS>(pos, moveList);
346 }
347
348
349 /// generate<LEGAL> generates all the legal moves in the given position
350
351 template<>
352 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
353
354   Color us = pos.side_to_move();
355   Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
356   Square ksq = pos.square<KING>(us);
357   ExtMove* cur = moveList;
358
359   moveList = pos.checkers() ? generate<EVASIONS    >(pos, moveList)
360                             : generate<NON_EVASIONS>(pos, moveList);
361   while (cur != moveList)
362       if (   (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
363           && !pos.legal(*cur))
364           *cur = (--moveList)->move;
365       else
366           ++cur;
367
368   return moveList;
369 }