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