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
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.
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.
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/>.
21 #ifndef BITBOARD_H_INCLUDED
22 #define BITBOARD_H_INCLUDED
31 bool probe(Square wksq, Square wpsq, Square bksq, Color us);
38 const std::string pretty(Bitboard b);
42 constexpr Bitboard AllSquares = ~Bitboard(0);
43 constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
45 constexpr Bitboard FileABB = 0x0101010101010101ULL;
46 constexpr Bitboard FileBBB = FileABB << 1;
47 constexpr Bitboard FileCBB = FileABB << 2;
48 constexpr Bitboard FileDBB = FileABB << 3;
49 constexpr Bitboard FileEBB = FileABB << 4;
50 constexpr Bitboard FileFBB = FileABB << 5;
51 constexpr Bitboard FileGBB = FileABB << 6;
52 constexpr Bitboard FileHBB = FileABB << 7;
54 constexpr Bitboard Rank1BB = 0xFF;
55 constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
56 constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
57 constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
58 constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
59 constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
60 constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
61 constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
63 constexpr Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
64 constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
65 constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;
66 constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
68 constexpr Bitboard KingFlank[FILE_NB] = {
69 QueenSide ^ FileDBB, QueenSide, QueenSide,
70 CenterFiles, CenterFiles,
71 KingSide, KingSide, KingSide ^ FileEBB
74 extern uint8_t PopCnt16[1 << 16];
75 extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
77 extern Bitboard SquareBB[SQUARE_NB];
78 extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
79 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
80 extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
83 /// Magic holds all magic bitboards relevant data for a single square
90 // Compute the attack's index using the 'magic bitboards' approach
91 unsigned index(Bitboard occupied) const {
94 return unsigned(pext(occupied, mask));
97 return unsigned(((occupied & mask) * magic) >> shift);
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;
105 extern Magic RookMagics[SQUARE_NB];
106 extern Magic BishopMagics[SQUARE_NB];
108 inline Bitboard square_bb(Square s) {
109 assert(s >= SQ_A1 && s <= SQ_H8);
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.
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 inline Bitboard& operator^=(Bitboard& b, Square s) { return b ^= square_bb(s); }
122 inline Bitboard operator&(Square s, Bitboard b) { return b & s; }
123 inline Bitboard operator|(Square s, Bitboard b) { return b | s; }
124 inline Bitboard operator^(Square s, Bitboard b) { return b ^ s; }
126 inline Bitboard operator|(Square s, Square s2) { return square_bb(s) | square_bb(s2); }
128 constexpr bool more_than_one(Bitboard b) {
132 inline bool opposite_colors(Square s1, Square s2) {
133 return bool(DarkSquares & s1) != bool(DarkSquares & s2);
137 /// rank_bb() and file_bb() return a bitboard representing all the squares on
138 /// the given file or rank.
140 inline Bitboard rank_bb(Rank r) {
141 return Rank1BB << (8 * r);
144 inline Bitboard rank_bb(Square s) {
145 return rank_bb(rank_of(s));
148 inline Bitboard file_bb(File f) {
152 inline Bitboard file_bb(Square s) {
153 return file_bb(file_of(s));
157 /// shift() moves a bitboard one step along direction D
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
170 /// pawn_attacks_bb() returns the squares attacked by pawns of the given color
171 /// from the squares in the given bitboard.
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);
180 /// pawn_double_attacks_bb() returns the squares doubly attacked by pawns of the
181 /// given color from the squares in the given bitboard.
184 constexpr Bitboard pawn_double_attacks_bb(Bitboard b) {
185 return C == WHITE ? shift<NORTH_WEST>(b) & shift<NORTH_EAST>(b)
186 : shift<SOUTH_WEST>(b) & shift<SOUTH_EAST>(b);
190 /// adjacent_files_bb() returns a bitboard representing all the squares on the
191 /// adjacent files of the given one.
193 inline Bitboard adjacent_files_bb(Square s) {
194 return shift<EAST>(file_bb(s)) | shift<WEST>(file_bb(s));
198 /// between_bb() returns squares that are linearly between the given squares
199 /// If the given squares are not on a same file/rank/diagonal, return 0.
201 inline Bitboard between_bb(Square s1, Square s2) {
202 return LineBB[s1][s2] & ( (AllSquares << (s1 + (s1 < s2)))
203 ^(AllSquares << (s2 + !(s1 < s2))));
207 /// forward_ranks_bb() returns a bitboard representing the squares on the ranks
208 /// in front of the given one, from the point of view of the given color. For instance,
209 /// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
211 inline Bitboard forward_ranks_bb(Color c, Square s) {
212 return c == WHITE ? ~Rank1BB << 8 * (rank_of(s) - RANK_1)
213 : ~Rank8BB >> 8 * (RANK_8 - rank_of(s));
217 /// forward_file_bb() returns a bitboard representing all the squares along the
218 /// line in front of the given one, from the point of view of the given color.
220 inline Bitboard forward_file_bb(Color c, Square s) {
221 return forward_ranks_bb(c, s) & file_bb(s);
225 /// pawn_attack_span() returns a bitboard representing all the squares that can
226 /// be attacked by a pawn of the given color when it moves along its file,
227 /// starting from the given square.
229 inline Bitboard pawn_attack_span(Color c, Square s) {
230 return forward_ranks_bb(c, s) & adjacent_files_bb(s);
234 /// passed_pawn_span() returns a bitboard which can be used to test if a pawn of
235 /// the given color and on the given square is a passed pawn.
237 inline Bitboard passed_pawn_span(Color c, Square s) {
238 return forward_ranks_bb(c, s) & (adjacent_files_bb(s) | file_bb(s));
242 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
243 /// straight or on a diagonal line.
245 inline bool aligned(Square s1, Square s2, Square s3) {
246 return LineBB[s1][s2] & s3;
250 /// distance() functions return the distance between x and y, defined as the
251 /// number of steps for a king in x to reach y.
253 template<typename T1 = Square> inline int distance(Square x, Square y);
254 template<> inline int distance<File>(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
255 template<> inline int distance<Rank>(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
256 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
258 template<class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
259 return v < lo ? lo : v > hi ? hi : v;
262 /// attacks_bb() returns a bitboard representing all the squares attacked by a
263 /// piece of type Pt (bishop or rook) placed on 's'.
265 template<PieceType Pt>
266 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
268 const Magic& m = Pt == ROOK ? RookMagics[s] : BishopMagics[s];
269 return m.attacks[m.index(occupied)];
272 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
278 case BISHOP: return attacks_bb<BISHOP>(s, occupied);
279 case ROOK : return attacks_bb< ROOK>(s, occupied);
280 case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
281 default : return PseudoAttacks[pt][s];
286 /// popcount() counts the number of non-zero bits in a bitboard
288 inline int popcount(Bitboard b) {
292 union { Bitboard bb; uint16_t u[4]; } v = { b };
293 return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
295 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
297 return (int)_mm_popcnt_u64(b);
299 #else // Assumed gcc or compatible compiler
301 return __builtin_popcountll(b);
307 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
309 #if defined(__GNUC__) // GCC, Clang, ICC
311 inline Square lsb(Bitboard b) {
313 return Square(__builtin_ctzll(b));
316 inline Square msb(Bitboard b) {
318 return Square(63 ^ __builtin_clzll(b));
321 #elif defined(_MSC_VER) // MSVC
323 #ifdef _WIN64 // MSVC, WIN64
325 inline Square lsb(Bitboard b) {
328 _BitScanForward64(&idx, b);
332 inline Square msb(Bitboard b) {
335 _BitScanReverse64(&idx, b);
341 inline Square lsb(Bitboard b) {
345 if (b & 0xffffffff) {
346 _BitScanForward(&idx, int32_t(b));
349 _BitScanForward(&idx, int32_t(b >> 32));
350 return Square(idx + 32);
354 inline Square msb(Bitboard b) {
359 _BitScanReverse(&idx, int32_t(b >> 32));
360 return Square(idx + 32);
362 _BitScanReverse(&idx, int32_t(b));
369 #else // Compiler is neither GCC nor MSVC compatible
371 #error "Compiler not supported."
376 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
378 inline Square pop_lsb(Bitboard* b) {
379 const Square s = lsb(*b);
385 /// frontmost_sq() returns the most advanced square for the given color
386 inline Square frontmost_sq(Color c, Bitboard b) {
387 return c == WHITE ? msb(b) : lsb(b);
390 #endif // #ifndef BITBOARD_H_INCLUDED