2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
19 #ifndef BITBOARD_H_INCLUDED
20 #define BITBOARD_H_INCLUDED
31 std::string pretty(Bitboard b);
33 } // namespace Stockfish::Bitboards
35 constexpr Bitboard FileABB = 0x0101010101010101ULL;
36 constexpr Bitboard FileBBB = FileABB << 1;
37 constexpr Bitboard FileCBB = FileABB << 2;
38 constexpr Bitboard FileDBB = FileABB << 3;
39 constexpr Bitboard FileEBB = FileABB << 4;
40 constexpr Bitboard FileFBB = FileABB << 5;
41 constexpr Bitboard FileGBB = FileABB << 6;
42 constexpr Bitboard FileHBB = FileABB << 7;
44 constexpr Bitboard Rank1BB = 0xFF;
45 constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
46 constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
47 constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
48 constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
49 constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
50 constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
51 constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
53 extern uint8_t PopCnt16[1 << 16];
54 extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
56 extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
57 extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
58 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
59 extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
62 /// Magic holds all magic bitboards relevant data for a single square
69 // Compute the attack's index using the 'magic bitboards' approach
70 unsigned index(Bitboard occupied) const {
73 return unsigned(pext(occupied, mask));
76 return unsigned(((occupied & mask) * magic) >> shift);
78 unsigned lo = unsigned(occupied) & unsigned(mask);
79 unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
80 return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
84 extern Magic RookMagics[SQUARE_NB];
85 extern Magic BishopMagics[SQUARE_NB];
87 inline Bitboard square_bb(Square s) {
93 /// Overloads of bitwise operators between a Bitboard and a Square for testing
94 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
96 inline Bitboard operator&( Bitboard b, Square s) { return b & square_bb(s); }
97 inline Bitboard operator|( Bitboard b, Square s) { return b | square_bb(s); }
98 inline Bitboard operator^( Bitboard b, Square s) { return b ^ square_bb(s); }
99 inline Bitboard& operator|=(Bitboard& b, Square s) { return b |= square_bb(s); }
100 inline Bitboard& operator^=(Bitboard& b, Square s) { return b ^= square_bb(s); }
102 inline Bitboard operator&(Square s, Bitboard b) { return b & s; }
103 inline Bitboard operator|(Square s, Bitboard b) { return b | s; }
104 inline Bitboard operator^(Square s, Bitboard b) { return b ^ s; }
106 inline Bitboard operator|(Square s1, Square s2) { return square_bb(s1) | s2; }
108 constexpr bool more_than_one(Bitboard b) {
113 /// rank_bb() and file_bb() return a bitboard representing all the squares on
114 /// the given file or rank.
116 constexpr Bitboard rank_bb(Rank r) {
117 return Rank1BB << (8 * r);
120 constexpr Bitboard rank_bb(Square s) {
121 return rank_bb(rank_of(s));
124 constexpr Bitboard file_bb(File f) {
128 constexpr Bitboard file_bb(Square s) {
129 return file_bb(file_of(s));
133 /// shift() moves a bitboard one or two steps as specified by the direction D
135 template<Direction D>
136 constexpr Bitboard shift(Bitboard b) {
137 return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
138 : D == NORTH+NORTH? b <<16 : D == SOUTH+SOUTH? b >>16
139 : D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1
140 : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
141 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
146 /// pawn_attacks_bb() returns the squares attacked by pawns of the given color
147 /// from the squares in the given bitboard.
150 constexpr Bitboard pawn_attacks_bb(Bitboard b) {
151 return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
152 : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
155 inline Bitboard pawn_attacks_bb(Color c, Square s) {
158 return PawnAttacks[c][s];
161 /// line_bb() returns a bitboard representing an entire line (from board edge
162 /// to board edge) that intersects the two given squares. If the given squares
163 /// are not on a same file/rank/diagonal, the function returns 0. For instance,
164 /// line_bb(SQ_C4, SQ_F7) will return a bitboard with the A2-G8 diagonal.
166 inline Bitboard line_bb(Square s1, Square s2) {
168 assert(is_ok(s1) && is_ok(s2));
170 return LineBB[s1][s2];
174 /// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
175 /// segment between the squares s1 and s2 (excluding s1 but including s2). If the
176 /// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
177 /// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
178 /// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
179 /// allows to generate non-king evasion moves faster: the defending piece must either
180 /// interpose itself to cover the check or capture the checking piece.
182 inline Bitboard between_bb(Square s1, Square s2) {
184 assert(is_ok(s1) && is_ok(s2));
186 return BetweenBB[s1][s2];
190 /// forward_ranks_bb() returns a bitboard representing the squares on the ranks in
191 /// front of the given one, from the point of view of the given color. For instance,
192 /// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
194 constexpr Bitboard forward_ranks_bb(Color c, Square s) {
195 return c == WHITE ? ~Rank1BB << 8 * relative_rank(WHITE, s)
196 : ~Rank8BB >> 8 * relative_rank(BLACK, s);
199 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
200 /// straight or on a diagonal line.
202 inline bool aligned(Square s1, Square s2, Square s3) {
203 return line_bb(s1, s2) & s3;
207 /// distance() functions return the distance between x and y, defined as the
208 /// number of steps for a king in x to reach y.
210 template<typename T1 = Square> inline int distance(Square x, Square y);
211 template<> inline int distance<File>(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
212 template<> inline int distance<Rank>(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
213 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
215 inline int edge_distance(File f) { return std::min(f, File(FILE_H - f)); }
217 /// attacks_bb(Square) returns the pseudo attacks of the give piece type
218 /// assuming an empty board.
220 template<PieceType Pt>
221 inline Bitboard attacks_bb(Square s) {
223 assert((Pt != PAWN) && (is_ok(s)));
225 return PseudoAttacks[Pt][s];
229 /// attacks_bb(Square, Bitboard) returns the attacks by the given piece
230 /// assuming the board is occupied according to the passed Bitboard.
231 /// Sliding piece attacks do not continue passed an occupied square.
233 template<PieceType Pt>
234 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
236 assert((Pt != PAWN) && (is_ok(s)));
240 case BISHOP: return BishopMagics[s].attacks[BishopMagics[s].index(occupied)];
241 case ROOK : return RookMagics[s].attacks[ RookMagics[s].index(occupied)];
242 case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
243 default : return PseudoAttacks[Pt][s];
247 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
249 assert((pt != PAWN) && (is_ok(s)));
253 case BISHOP: return attacks_bb<BISHOP>(s, occupied);
254 case ROOK : return attacks_bb< ROOK>(s, occupied);
255 case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
256 default : return PseudoAttacks[pt][s];
261 /// popcount() counts the number of non-zero bits in a bitboard
263 inline int popcount(Bitboard b) {
267 union { Bitboard bb; uint16_t u[4]; } v = { b };
268 return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
270 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
272 return (int)_mm_popcnt_u64(b);
274 #else // Assumed gcc or compatible compiler
276 return __builtin_popcountll(b);
282 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
284 #if defined(__GNUC__) // GCC, Clang, ICC
286 inline Square lsb(Bitboard b) {
288 return Square(__builtin_ctzll(b));
291 inline Square msb(Bitboard b) {
293 return Square(63 ^ __builtin_clzll(b));
296 #elif defined(_MSC_VER) // MSVC
298 #ifdef _WIN64 // MSVC, WIN64
300 inline Square lsb(Bitboard b) {
303 _BitScanForward64(&idx, b);
307 inline Square msb(Bitboard b) {
310 _BitScanReverse64(&idx, b);
316 inline Square lsb(Bitboard b) {
320 if (b & 0xffffffff) {
321 _BitScanForward(&idx, int32_t(b));
324 _BitScanForward(&idx, int32_t(b >> 32));
325 return Square(idx + 32);
329 inline Square msb(Bitboard b) {
334 _BitScanReverse(&idx, int32_t(b >> 32));
335 return Square(idx + 32);
337 _BitScanReverse(&idx, int32_t(b));
344 #else // Compiler is neither GCC nor MSVC compatible
346 #error "Compiler not supported."
350 /// least_significant_square_bb() returns the bitboard of the least significant
351 /// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).
353 inline Bitboard least_significant_square_bb(Bitboard b) {
358 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
360 inline Square pop_lsb(Bitboard& b) {
362 const Square s = lsb(b);
367 } // namespace Stockfish
369 #endif // #ifndef BITBOARD_H_INCLUDED