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