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
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (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 const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
44 const Bitboard FileABB = 0x0101010101010101ULL;
45 const Bitboard FileBBB = FileABB << 1;
46 const Bitboard FileCBB = FileABB << 2;
47 const Bitboard FileDBB = FileABB << 3;
48 const Bitboard FileEBB = FileABB << 4;
49 const Bitboard FileFBB = FileABB << 5;
50 const Bitboard FileGBB = FileABB << 6;
51 const Bitboard FileHBB = FileABB << 7;
53 const Bitboard Rank1BB = 0xFF;
54 const Bitboard Rank2BB = Rank1BB << (8 * 1);
55 const Bitboard Rank3BB = Rank1BB << (8 * 2);
56 const Bitboard Rank4BB = Rank1BB << (8 * 3);
57 const Bitboard Rank5BB = Rank1BB << (8 * 4);
58 const Bitboard Rank6BB = Rank1BB << (8 * 5);
59 const Bitboard Rank7BB = Rank1BB << (8 * 6);
60 const Bitboard Rank8BB = Rank1BB << (8 * 7);
62 extern int SquareDistance[SQUARE_NB][SQUARE_NB];
64 extern Bitboard RookMasks [SQUARE_NB];
65 extern Bitboard RookMagics [SQUARE_NB];
66 extern Bitboard* RookAttacks[SQUARE_NB];
67 extern unsigned RookShifts [SQUARE_NB];
69 extern Bitboard BishopMasks [SQUARE_NB];
70 extern Bitboard BishopMagics [SQUARE_NB];
71 extern Bitboard* BishopAttacks[SQUARE_NB];
72 extern unsigned BishopShifts [SQUARE_NB];
74 extern Bitboard SquareBB[SQUARE_NB];
75 extern Bitboard FileBB[FILE_NB];
76 extern Bitboard RankBB[RANK_NB];
77 extern Bitboard AdjacentFilesBB[FILE_NB];
78 extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
79 extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
80 extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
81 extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
82 extern Bitboard DistanceRingBB[SQUARE_NB][8];
83 extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
84 extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
85 extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
86 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
89 /// Overloads of bitwise operators between a Bitboard and a Square for testing
90 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
92 inline Bitboard operator&(Bitboard b, Square s) {
93 return b & SquareBB[s];
96 inline Bitboard operator|(Bitboard b, Square s) {
97 return b | SquareBB[s];
100 inline Bitboard operator^(Bitboard b, Square s) {
101 return b ^ SquareBB[s];
104 inline Bitboard& operator|=(Bitboard& b, Square s) {
105 return b |= SquareBB[s];
108 inline Bitboard& operator^=(Bitboard& b, Square s) {
109 return b ^= SquareBB[s];
112 inline bool more_than_one(Bitboard b) {
117 /// rank_bb() and file_bb() return a bitboard representing all the squares on
118 /// the given file or rank.
120 inline Bitboard rank_bb(Rank r) {
124 inline Bitboard rank_bb(Square s) {
125 return RankBB[rank_of(s)];
128 inline Bitboard file_bb(File f) {
132 inline Bitboard file_bb(Square s) {
133 return FileBB[file_of(s)];
137 /// shift_bb() moves a bitboard one step along direction Delta. Mainly for pawns
139 template<Square Delta>
140 inline Bitboard shift_bb(Bitboard b) {
141 return Delta == DELTA_N ? b << 8 : Delta == DELTA_S ? b >> 8
142 : Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
143 : Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
148 /// adjacent_files_bb() returns a bitboard representing all the squares on the
149 /// adjacent files of the given one.
151 inline Bitboard adjacent_files_bb(File f) {
152 return AdjacentFilesBB[f];
156 /// between_bb() returns a bitboard representing all the squares between the two
157 /// given ones. For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with
158 /// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank, file
159 /// or diagonal, 0 is returned.
161 inline Bitboard between_bb(Square s1, Square s2) {
162 return BetweenBB[s1][s2];
166 /// in_front_bb() returns a bitboard representing all the squares on all the ranks
167 /// in front of the given one, from the point of view of the given color. For
168 /// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
170 inline Bitboard in_front_bb(Color c, Rank r) {
171 return InFrontBB[c][r];
175 /// forward_bb() returns a bitboard representing all the squares along the line
176 /// in front of the given one, from the point of view of the given color:
177 /// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
179 inline Bitboard forward_bb(Color c, Square s) {
180 return ForwardBB[c][s];
184 /// pawn_attack_span() returns a bitboard representing all the squares that can be
185 /// attacked by a pawn of the given color when it moves along its file, starting
186 /// from the given square:
187 /// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
189 inline Bitboard pawn_attack_span(Color c, Square s) {
190 return PawnAttackSpan[c][s];
194 /// passed_pawn_mask() returns a bitboard mask which can be used to test if a
195 /// pawn of the given color and on the given square is a passed pawn:
196 /// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
198 inline Bitboard passed_pawn_mask(Color c, Square s) {
199 return PassedPawnMask[c][s];
203 /// squares_of_color() returns a bitboard representing all the squares of the
204 /// same color of the given one.
206 inline Bitboard squares_of_color(Square s) {
207 return DarkSquares & s ? DarkSquares : ~DarkSquares;
211 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
212 /// straight or on a diagonal line.
214 inline bool aligned(Square s1, Square s2, Square s3) {
215 return LineBB[s1][s2] & s3;
219 /// distance() functions return the distance between x and y, defined as the
220 /// number of steps for a king in x to reach y. Works with squares, ranks, files.
222 template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
223 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
225 template<typename T1, typename T2> inline int distance(T2 x, T2 y);
226 template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
227 template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
230 /// attacks_bb() returns a bitboard representing all the squares attacked by a
231 /// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index()
232 /// looks up the index using the 'magic bitboards' approach.
233 template<PieceType Pt>
234 FORCE_INLINE unsigned magic_index(Square s, Bitboard occupied) {
236 Bitboard* const Masks = Pt == ROOK ? RookMasks : BishopMasks;
237 Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics;
238 unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts;
241 return unsigned(pext(occupied, Masks[s]));
244 return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]);
246 unsigned lo = unsigned(occupied) & unsigned(Masks[s]);
247 unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32);
248 return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
251 template<PieceType Pt>
252 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
253 return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)];
256 inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) {
260 case BISHOP: return attacks_bb<BISHOP>(s, occupied);
261 case ROOK : return attacks_bb<ROOK>(s, occupied);
262 case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
263 default : return StepAttacksBB[pc][s];
268 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
272 # if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
274 FORCE_INLINE Square lsb(Bitboard b) {
276 _BitScanForward64(&idx, b);
280 FORCE_INLINE Square msb(Bitboard b) {
282 _BitScanReverse64(&idx, b);
286 # elif defined(__arm__)
288 FORCE_INLINE int lsb32(uint32_t v) {
289 __asm__("rbit %0, %1" : "=r"(v) : "r"(v));
290 return __builtin_clz(v);
293 FORCE_INLINE Square msb(Bitboard b) {
294 return (Square) (63 - __builtin_clzll(b));
297 FORCE_INLINE Square lsb(Bitboard b) {
298 return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
301 # else // Assumed gcc or compatible compiler
303 FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
305 __asm__("bsfq %1, %0": "=r"(idx): "rm"(b) );
309 FORCE_INLINE Square msb(Bitboard b) {
311 __asm__("bsrq %1, %0": "=r"(idx): "rm"(b) );
317 #else // ifdef(USE_BSFQ)
319 Square lsb(Bitboard b);
320 Square msb(Bitboard b);
325 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
327 FORCE_INLINE Square pop_lsb(Bitboard* b) {
328 const Square s = lsb(*b);
334 /// frontmost_sq() and backmost_sq() return the square corresponding to the
335 /// most/least advanced bit relative to the given color.
337 inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
338 inline Square backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
340 #endif // #ifndef BITBOARD_H_INCLUDED