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