]> git.sesse.net Git - stockfish/blob - src/bitboard.h
Remove classical evaluation
[stockfish] / src / bitboard.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
4
5   Stockfish 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   Stockfish 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 #ifndef BITBOARD_H_INCLUDED
20 #define BITBOARD_H_INCLUDED
21
22 #include <string>
23
24 #include "types.h"
25
26 namespace Stockfish {
27
28 namespace Bitboards {
29
30 void init();
31 std::string pretty(Bitboard b);
32
33 } // namespace Stockfish::Bitboards
34
35 constexpr Bitboard AllSquares = ~Bitboard(0);
36 constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
37
38 constexpr Bitboard FileABB = 0x0101010101010101ULL;
39 constexpr Bitboard FileBBB = FileABB << 1;
40 constexpr Bitboard FileCBB = FileABB << 2;
41 constexpr Bitboard FileDBB = FileABB << 3;
42 constexpr Bitboard FileEBB = FileABB << 4;
43 constexpr Bitboard FileFBB = FileABB << 5;
44 constexpr Bitboard FileGBB = FileABB << 6;
45 constexpr Bitboard FileHBB = FileABB << 7;
46
47 constexpr Bitboard Rank1BB = 0xFF;
48 constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
49 constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
50 constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
51 constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
52 constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
53 constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
54 constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
55
56 constexpr Bitboard QueenSide   = FileABB | FileBBB | FileCBB | FileDBB;
57 constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
58 constexpr Bitboard KingSide    = FileEBB | FileFBB | FileGBB | FileHBB;
59 constexpr Bitboard Center      = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
60
61 constexpr Bitboard KingFlank[FILE_NB] = {
62   QueenSide ^ FileDBB, QueenSide, QueenSide,
63   CenterFiles, CenterFiles,
64   KingSide, KingSide, KingSide ^ FileEBB
65 };
66
67 extern uint8_t PopCnt16[1 << 16];
68 extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
69
70 extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
71 extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
72 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
73 extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
74
75
76 /// Magic holds all magic bitboards relevant data for a single square
77 struct Magic {
78   Bitboard  mask;
79   Bitboard  magic;
80   Bitboard* attacks;
81   unsigned  shift;
82
83   // Compute the attack's index using the 'magic bitboards' approach
84   unsigned index(Bitboard occupied) const {
85
86     if (HasPext)
87         return unsigned(pext(occupied, mask));
88
89     if (Is64Bit)
90         return unsigned(((occupied & mask) * magic) >> shift);
91
92     unsigned lo = unsigned(occupied) & unsigned(mask);
93     unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
94     return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
95   }
96 };
97
98 extern Magic RookMagics[SQUARE_NB];
99 extern Magic BishopMagics[SQUARE_NB];
100
101 inline Bitboard square_bb(Square s) {
102   assert(is_ok(s));
103   return (1ULL << s);
104 }
105
106
107 /// Overloads of bitwise operators between a Bitboard and a Square for testing
108 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
109
110 inline Bitboard  operator&( Bitboard  b, Square s) { return b &  square_bb(s); }
111 inline Bitboard  operator|( Bitboard  b, Square s) { return b |  square_bb(s); }
112 inline Bitboard  operator^( Bitboard  b, Square s) { return b ^  square_bb(s); }
113 inline Bitboard& operator|=(Bitboard& b, Square s) { return b |= square_bb(s); }
114 inline Bitboard& operator^=(Bitboard& b, Square s) { return b ^= square_bb(s); }
115
116 inline Bitboard  operator&(Square s, Bitboard b) { return b & s; }
117 inline Bitboard  operator|(Square s, Bitboard b) { return b | s; }
118 inline Bitboard  operator^(Square s, Bitboard b) { return b ^ s; }
119
120 inline Bitboard  operator|(Square s1, Square s2) { return square_bb(s1) | s2; }
121
122 constexpr bool more_than_one(Bitboard b) {
123   return b & (b - 1);
124 }
125
126
127 constexpr bool opposite_colors(Square s1, Square s2) {
128   return (s1 + rank_of(s1) + s2 + rank_of(s2)) & 1;
129 }
130
131
132 /// rank_bb() and file_bb() return a bitboard representing all the squares on
133 /// the given file or rank.
134
135 constexpr Bitboard rank_bb(Rank r) {
136   return Rank1BB << (8 * r);
137 }
138
139 constexpr Bitboard rank_bb(Square s) {
140   return rank_bb(rank_of(s));
141 }
142
143 constexpr Bitboard file_bb(File f) {
144   return FileABB << f;
145 }
146
147 constexpr Bitboard file_bb(Square s) {
148   return file_bb(file_of(s));
149 }
150
151
152 /// shift() moves a bitboard one or two steps as specified by the direction D
153
154 template<Direction D>
155 constexpr Bitboard shift(Bitboard b) {
156   return  D == NORTH      ?  b             << 8 : D == SOUTH      ?  b             >> 8
157         : D == NORTH+NORTH?  b             <<16 : D == SOUTH+SOUTH?  b             >>16
158         : D == EAST       ? (b & ~FileHBB) << 1 : D == WEST       ? (b & ~FileABB) >> 1
159         : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
160         : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
161         : 0;
162 }
163
164
165 /// pawn_attacks_bb() returns the squares attacked by pawns of the given color
166 /// from the squares in the given bitboard.
167
168 template<Color C>
169 constexpr Bitboard pawn_attacks_bb(Bitboard b) {
170   return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
171                     : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
172 }
173
174 inline Bitboard pawn_attacks_bb(Color c, Square s) {
175
176   assert(is_ok(s));
177   return PawnAttacks[c][s];
178 }
179
180
181 /// pawn_double_attacks_bb() returns the squares doubly attacked by pawns of the
182 /// given color from the squares in the given bitboard.
183
184 template<Color C>
185 constexpr Bitboard pawn_double_attacks_bb(Bitboard b) {
186   return C == WHITE ? shift<NORTH_WEST>(b) & shift<NORTH_EAST>(b)
187                     : shift<SOUTH_WEST>(b) & shift<SOUTH_EAST>(b);
188 }
189
190
191 /// adjacent_files_bb() returns a bitboard representing all the squares on the
192 /// adjacent files of a given square.
193
194 constexpr Bitboard adjacent_files_bb(Square s) {
195   return shift<EAST>(file_bb(s)) | shift<WEST>(file_bb(s));
196 }
197
198
199 /// line_bb() returns a bitboard representing an entire line (from board edge
200 /// to board edge) that intersects the two given squares. If the given squares
201 /// are not on a same file/rank/diagonal, the function returns 0. For instance,
202 /// line_bb(SQ_C4, SQ_F7) will return a bitboard with the A2-G8 diagonal.
203
204 inline Bitboard line_bb(Square s1, Square s2) {
205
206   assert(is_ok(s1) && is_ok(s2));
207
208   return LineBB[s1][s2];
209 }
210
211
212 /// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
213 /// segment between the squares s1 and s2 (excluding s1 but including s2). If the
214 /// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
215 /// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
216 /// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
217 /// allows to generate non-king evasion moves faster: the defending piece must either
218 /// interpose itself to cover the check or capture the checking piece.
219
220 inline Bitboard between_bb(Square s1, Square s2) {
221
222   assert(is_ok(s1) && is_ok(s2));
223
224   return BetweenBB[s1][s2];
225 }
226
227
228 /// forward_ranks_bb() returns a bitboard representing the squares on the ranks in
229 /// front of the given one, from the point of view of the given color. For instance,
230 /// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
231
232 constexpr Bitboard forward_ranks_bb(Color c, Square s) {
233   return c == WHITE ? ~Rank1BB << 8 * relative_rank(WHITE, s)
234                     : ~Rank8BB >> 8 * relative_rank(BLACK, s);
235 }
236
237
238 /// forward_file_bb() returns a bitboard representing all the squares along the
239 /// line in front of the given one, from the point of view of the given color.
240
241 constexpr Bitboard forward_file_bb(Color c, Square s) {
242   return forward_ranks_bb(c, s) & file_bb(s);
243 }
244
245
246 /// pawn_attack_span() returns a bitboard representing all the squares that can
247 /// be attacked by a pawn of the given color when it moves along its file, starting
248 /// from the given square.
249
250 constexpr Bitboard pawn_attack_span(Color c, Square s) {
251   return forward_ranks_bb(c, s) & adjacent_files_bb(s);
252 }
253
254
255 /// passed_pawn_span() returns a bitboard which can be used to test if a pawn of
256 /// the given color and on the given square is a passed pawn.
257
258 constexpr Bitboard passed_pawn_span(Color c, Square s) {
259   return pawn_attack_span(c, s) | forward_file_bb(c, s);
260 }
261
262
263 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
264 /// straight or on a diagonal line.
265
266 inline bool aligned(Square s1, Square s2, Square s3) {
267   return line_bb(s1, s2) & s3;
268 }
269
270
271 /// distance() functions return the distance between x and y, defined as the
272 /// number of steps for a king in x to reach y.
273
274 template<typename T1 = Square> inline int distance(Square x, Square y);
275 template<> inline int distance<File>(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
276 template<> inline int distance<Rank>(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
277 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
278
279 inline int edge_distance(File f) { return std::min(f, File(FILE_H - f)); }
280 inline int edge_distance(Rank r) { return std::min(r, Rank(RANK_8 - r)); }
281
282
283 /// attacks_bb(Square) returns the pseudo attacks of the give piece type
284 /// assuming an empty board.
285
286 template<PieceType Pt>
287 inline Bitboard attacks_bb(Square s) {
288
289   assert((Pt != PAWN) && (is_ok(s)));
290
291   return PseudoAttacks[Pt][s];
292 }
293
294
295 /// attacks_bb(Square, Bitboard) returns the attacks by the given piece
296 /// assuming the board is occupied according to the passed Bitboard.
297 /// Sliding piece attacks do not continue passed an occupied square.
298
299 template<PieceType Pt>
300 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
301
302   assert((Pt != PAWN) && (is_ok(s)));
303
304   switch (Pt)
305   {
306   case BISHOP: return BishopMagics[s].attacks[BishopMagics[s].index(occupied)];
307   case ROOK  : return   RookMagics[s].attacks[  RookMagics[s].index(occupied)];
308   case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
309   default    : return PseudoAttacks[Pt][s];
310   }
311 }
312
313 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
314
315   assert((pt != PAWN) && (is_ok(s)));
316
317   switch (pt)
318   {
319   case BISHOP: return attacks_bb<BISHOP>(s, occupied);
320   case ROOK  : return attacks_bb<  ROOK>(s, occupied);
321   case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
322   default    : return PseudoAttacks[pt][s];
323   }
324 }
325
326
327 /// popcount() counts the number of non-zero bits in a bitboard
328
329 inline int popcount(Bitboard b) {
330
331 #ifndef USE_POPCNT
332
333   union { Bitboard bb; uint16_t u[4]; } v = { b };
334   return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
335
336 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
337
338   return (int)_mm_popcnt_u64(b);
339
340 #else // Assumed gcc or compatible compiler
341
342   return __builtin_popcountll(b);
343
344 #endif
345 }
346
347
348 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
349
350 #if defined(__GNUC__)  // GCC, Clang, ICC
351
352 inline Square lsb(Bitboard b) {
353   assert(b);
354   return Square(__builtin_ctzll(b));
355 }
356
357 inline Square msb(Bitboard b) {
358   assert(b);
359   return Square(63 ^ __builtin_clzll(b));
360 }
361
362 #elif defined(_MSC_VER)  // MSVC
363
364 #ifdef _WIN64  // MSVC, WIN64
365
366 inline Square lsb(Bitboard b) {
367   assert(b);
368   unsigned long idx;
369   _BitScanForward64(&idx, b);
370   return (Square) idx;
371 }
372
373 inline Square msb(Bitboard b) {
374   assert(b);
375   unsigned long idx;
376   _BitScanReverse64(&idx, b);
377   return (Square) idx;
378 }
379
380 #else  // MSVC, WIN32
381
382 inline Square lsb(Bitboard b) {
383   assert(b);
384   unsigned long idx;
385
386   if (b & 0xffffffff) {
387       _BitScanForward(&idx, int32_t(b));
388       return Square(idx);
389   } else {
390       _BitScanForward(&idx, int32_t(b >> 32));
391       return Square(idx + 32);
392   }
393 }
394
395 inline Square msb(Bitboard b) {
396   assert(b);
397   unsigned long idx;
398
399   if (b >> 32) {
400       _BitScanReverse(&idx, int32_t(b >> 32));
401       return Square(idx + 32);
402   } else {
403       _BitScanReverse(&idx, int32_t(b));
404       return Square(idx);
405   }
406 }
407
408 #endif
409
410 #else  // Compiler is neither GCC nor MSVC compatible
411
412 #error "Compiler not supported."
413
414 #endif
415
416 /// least_significant_square_bb() returns the bitboard of the least significant
417 /// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).
418
419 inline Bitboard least_significant_square_bb(Bitboard b) {
420   assert(b);
421   return b & -b;
422 }
423
424 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
425
426 inline Square pop_lsb(Bitboard& b) {
427   assert(b);
428   const Square s = lsb(b);
429   b &= b - 1;
430   return s;
431 }
432
433
434 /// frontmost_sq() returns the most advanced square for the given color,
435 /// requires a non-zero bitboard.
436 inline Square frontmost_sq(Color c, Bitboard b) {
437   assert(b);
438   return c == WHITE ? msb(b) : lsb(b);
439 }
440
441 } // namespace Stockfish
442
443 #endif // #ifndef BITBOARD_H_INCLUDED