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
36 std::string pretty(Bitboard b);
38 } // namespace Stockfish::Bitboards
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;
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);
58 extern uint8_t PopCnt16[1 << 16];
59 extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
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];
67 /// Magic holds all magic bitboards relevant data for a single square
74 // Compute the attack's index using the 'magic bitboards' approach
75 unsigned index(Bitboard occupied) const {
78 return unsigned(pext(occupied, mask));
81 return unsigned(((occupied & mask) * magic) >> shift);
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;
89 extern Magic RookMagics[SQUARE_NB];
90 extern Magic BishopMagics[SQUARE_NB];
92 inline Bitboard square_bb(Square s) {
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.
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); }
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; }
111 inline Bitboard operator|(Square s1, Square s2) { return square_bb(s1) | s2; }
113 constexpr bool more_than_one(Bitboard b) {
118 /// rank_bb() and file_bb() return a bitboard representing all the squares on
119 /// the given file or rank.
121 constexpr Bitboard rank_bb(Rank r) {
122 return Rank1BB << (8 * r);
125 constexpr Bitboard rank_bb(Square s) {
126 return rank_bb(rank_of(s));
129 constexpr Bitboard file_bb(File f) {
133 constexpr Bitboard file_bb(Square s) {
134 return file_bb(file_of(s));
138 /// shift() moves a bitboard one or two steps as specified by the direction D
140 template<Direction D>
141 constexpr Bitboard shift(Bitboard b) {
142 return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
143 : D == NORTH+NORTH? b <<16 : D == SOUTH+SOUTH? b >>16
144 : D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1
145 : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
146 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
151 /// pawn_attacks_bb() returns the squares attacked by pawns of the given color
152 /// from the squares in the given bitboard.
155 constexpr Bitboard pawn_attacks_bb(Bitboard b) {
156 return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
157 : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
160 inline Bitboard pawn_attacks_bb(Color c, Square s) {
163 return PawnAttacks[c][s];
166 /// line_bb() returns a bitboard representing an entire line (from board edge
167 /// to board edge) that intersects the two given squares. If the given squares
168 /// are not on a same file/rank/diagonal, the function returns 0. For instance,
169 /// line_bb(SQ_C4, SQ_F7) will return a bitboard with the A2-G8 diagonal.
171 inline Bitboard line_bb(Square s1, Square s2) {
173 assert(is_ok(s1) && is_ok(s2));
175 return LineBB[s1][s2];
179 /// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
180 /// segment between the squares s1 and s2 (excluding s1 but including s2). If the
181 /// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
182 /// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
183 /// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
184 /// allows to generate non-king evasion moves faster: the defending piece must either
185 /// interpose itself to cover the check or capture the checking piece.
187 inline Bitboard between_bb(Square s1, Square s2) {
189 assert(is_ok(s1) && is_ok(s2));
191 return BetweenBB[s1][s2];
194 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
195 /// straight or on a diagonal line.
197 inline bool aligned(Square s1, Square s2, Square s3) {
198 return line_bb(s1, s2) & s3;
202 /// distance() functions return the distance between x and y, defined as the
203 /// number of steps for a king in x to reach y.
205 template<typename T1 = Square> inline int distance(Square x, Square y);
206 template<> inline int distance<File>(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
207 template<> inline int distance<Rank>(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
208 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
210 inline int edge_distance(File f) { return std::min(f, File(FILE_H - f)); }
212 /// attacks_bb(Square) returns the pseudo attacks of the given piece type
213 /// assuming an empty board.
215 template<PieceType Pt>
216 inline Bitboard attacks_bb(Square s) {
218 assert((Pt != PAWN) && (is_ok(s)));
220 return PseudoAttacks[Pt][s];
224 /// attacks_bb(Square, Bitboard) 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.
228 template<PieceType Pt>
229 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
231 assert((Pt != PAWN) && (is_ok(s)));
235 case BISHOP: return BishopMagics[s].attacks[BishopMagics[s].index(occupied)];
236 case ROOK : return RookMagics[s].attacks[ RookMagics[s].index(occupied)];
237 case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
238 default : return PseudoAttacks[Pt][s];
242 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
244 assert((pt != PAWN) && (is_ok(s)));
248 case BISHOP: return attacks_bb<BISHOP>(s, occupied);
249 case ROOK : return attacks_bb< ROOK>(s, occupied);
250 case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
251 default : return PseudoAttacks[pt][s];
256 /// popcount() counts the number of non-zero bits in a bitboard
258 inline int popcount(Bitboard b) {
262 union { Bitboard bb; uint16_t u[4]; } v = { b };
263 return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
265 #elif defined(_MSC_VER)
267 return int(_mm_popcnt_u64(b));
269 #else // Assumed gcc or compatible compiler
271 return __builtin_popcountll(b);
277 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
279 #if defined(__GNUC__) // GCC, Clang, ICX
281 inline Square lsb(Bitboard b) {
283 return Square(__builtin_ctzll(b));
286 inline Square msb(Bitboard b) {
288 return Square(63 ^ __builtin_clzll(b));
291 #elif defined(_MSC_VER) // MSVC
293 #ifdef _WIN64 // MSVC, WIN64
295 inline Square lsb(Bitboard b) {
298 _BitScanForward64(&idx, b);
302 inline Square msb(Bitboard b) {
305 _BitScanReverse64(&idx, b);
311 inline Square lsb(Bitboard b) {
315 if (b & 0xffffffff) {
316 _BitScanForward(&idx, int32_t(b));
319 _BitScanForward(&idx, int32_t(b >> 32));
320 return Square(idx + 32);
324 inline Square msb(Bitboard b) {
329 _BitScanReverse(&idx, int32_t(b >> 32));
330 return Square(idx + 32);
332 _BitScanReverse(&idx, int32_t(b));
339 #else // Compiler is neither GCC nor MSVC compatible
341 #error "Compiler not supported."
345 /// least_significant_square_bb() returns the bitboard of the least significant
346 /// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).
348 inline Bitboard least_significant_square_bb(Bitboard b) {
353 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
355 inline Square pop_lsb(Bitboard& b) {
357 const Square s = lsb(b);
362 } // namespace Stockfish
364 #endif // #ifndef BITBOARD_H_INCLUDED