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-2013 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 #if !defined(BITBOARD_H_INCLUDED)
22 #define BITBOARD_H_INCLUDED
29 void print(Bitboard b);
36 bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
40 const Bitboard FileABB = 0x0101010101010101ULL;
41 const Bitboard FileBBB = FileABB << 1;
42 const Bitboard FileCBB = FileABB << 2;
43 const Bitboard FileDBB = FileABB << 3;
44 const Bitboard FileEBB = FileABB << 4;
45 const Bitboard FileFBB = FileABB << 5;
46 const Bitboard FileGBB = FileABB << 6;
47 const Bitboard FileHBB = FileABB << 7;
49 const Bitboard Rank1BB = 0xFF;
50 const Bitboard Rank2BB = Rank1BB << (8 * 1);
51 const Bitboard Rank3BB = Rank1BB << (8 * 2);
52 const Bitboard Rank4BB = Rank1BB << (8 * 3);
53 const Bitboard Rank5BB = Rank1BB << (8 * 4);
54 const Bitboard Rank6BB = Rank1BB << (8 * 5);
55 const Bitboard Rank7BB = Rank1BB << (8 * 6);
56 const Bitboard Rank8BB = Rank1BB << (8 * 7);
60 extern Bitboard RMasks[SQUARE_NB];
61 extern Bitboard RMagics[SQUARE_NB];
62 extern Bitboard* RAttacks[SQUARE_NB];
63 extern unsigned RShifts[SQUARE_NB];
65 extern Bitboard BMasks[SQUARE_NB];
66 extern Bitboard BMagics[SQUARE_NB];
67 extern Bitboard* BAttacks[SQUARE_NB];
68 extern unsigned BShifts[SQUARE_NB];
70 extern Bitboard SquareBB[SQUARE_NB];
71 extern Bitboard FileBB[FILE_NB];
72 extern Bitboard RankBB[RANK_NB];
73 extern Bitboard AdjacentFilesBB[FILE_NB];
74 extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
75 extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
76 extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
77 extern Bitboard DistanceRingsBB[SQUARE_NB][8];
78 extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
79 extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
80 extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
81 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
83 const Bitboard BlackSquares = 0xAA55AA55AA55AA55ULL;
85 /// Overloads of bitwise operators between a Bitboard and a Square for testing
86 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
88 inline Bitboard operator&(Bitboard b, Square s) {
89 return b & SquareBB[s];
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];
109 /// more_than_one() returns true if in 'b' there is more than one bit set
111 inline bool more_than_one(Bitboard b) {
116 /// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
118 template<Square Delta>
119 inline Bitboard shift_bb(Bitboard b) {
121 return Delta == DELTA_N ? b << 8 : Delta == DELTA_S ? b >> 8
122 : Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
123 : Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
128 /// rank_bb() and file_bb() take a file or a square as input and return
129 /// a bitboard representing all squares on the given file or rank.
131 inline Bitboard rank_bb(Rank r) {
135 inline Bitboard rank_bb(Square s) {
136 return RankBB[rank_of(s)];
139 inline Bitboard file_bb(File f) {
143 inline Bitboard file_bb(Square s) {
144 return FileBB[file_of(s)];
148 /// adjacent_files_bb takes a file as input and returns a bitboard representing
149 /// all squares on the adjacent files.
151 inline Bitboard adjacent_files_bb(File f) {
152 return AdjacentFilesBB[f];
156 /// in_front_bb() takes a color and a rank as input, and returns a bitboard
157 /// representing all the squares on all ranks in front of the rank, from the
158 /// given color's point of view. For instance, in_front_bb(BLACK, RANK_3) will
159 /// give all squares on ranks 1 and 2.
161 inline Bitboard in_front_bb(Color c, Rank r) {
162 return InFrontBB[c][r];
166 /// between_bb returns a bitboard representing all squares between two squares.
167 /// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
168 /// square d5 and e6 set. If s1 and s2 are not on the same line, file or diagonal,
171 inline Bitboard between_bb(Square s1, Square s2) {
172 return BetweenBB[s1][s2];
176 /// forward_bb takes a color and a square as input, and returns a bitboard
177 /// representing all squares along the line in front of the square, from the
178 /// point of view of the given color. Definition of the table is:
179 /// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
181 inline Bitboard forward_bb(Color c, Square s) {
182 return ForwardBB[c][s];
186 /// passed_pawn_mask takes a color and a square as input, and returns a
187 /// bitboard mask which can be used to test if a pawn of the given color on
188 /// the given square is a passed pawn. Definition of the table is:
189 /// PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_adjacent_files_bb(s)
191 inline Bitboard passed_pawn_mask(Color c, Square s) {
192 return PassedPawnMask[c][s];
196 /// attack_span_mask takes a color and a square as input, and returns a bitboard
197 /// representing all squares that can be attacked by a pawn of the given color
198 /// when it moves along its file starting from the given square. Definition is:
199 /// AttackSpanMask[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
201 inline Bitboard pawn_attack_span(Color c, Square s) {
202 return PawnAttackSpan[c][s];
206 /// squares_aligned returns true if the squares s1, s2 and s3 are aligned
207 /// either on a straight or on a diagonal line.
209 inline bool squares_aligned(Square s1, Square s2, Square s3) {
210 return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3])
211 & ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]);
215 /// same_color_squares() returns a bitboard representing all squares with
216 /// the same color of the given square.
218 inline Bitboard same_color_squares(Square s) {
219 return BlackSquares & s ? BlackSquares : ~BlackSquares;
223 /// Functions for computing sliding attack bitboards. Function attacks_bb() takes
224 /// a square and a bitboard of occupied squares as input, and returns a bitboard
225 /// representing all squares attacked by Pt (bishop or rook) on the given square.
226 template<PieceType Pt>
227 FORCE_INLINE unsigned magic_index(Square s, Bitboard occ) {
229 Bitboard* const Masks = Pt == ROOK ? RMasks : BMasks;
230 Bitboard* const Magics = Pt == ROOK ? RMagics : BMagics;
231 unsigned* const Shifts = Pt == ROOK ? RShifts : BShifts;
234 return unsigned(((occ & Masks[s]) * Magics[s]) >> Shifts[s]);
236 unsigned lo = unsigned(occ) & unsigned(Masks[s]);
237 unsigned hi = unsigned(occ >> 32) & unsigned(Masks[s] >> 32);
238 return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
241 template<PieceType Pt>
242 inline Bitboard attacks_bb(Square s, Bitboard occ) {
243 return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
247 /// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
248 /// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
250 #if defined(USE_BSFQ)
252 # if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
254 FORCE_INLINE Square lsb(Bitboard b) {
256 _BitScanForward64(&index, b);
257 return (Square) index;
260 FORCE_INLINE Square msb(Bitboard b) {
262 _BitScanReverse64(&index, b);
263 return (Square) index;
266 # elif defined(__arm__)
268 FORCE_INLINE int lsb32(uint32_t v) {
269 __asm__("rbit %0, %1" : "=r"(v) : "r"(v));
270 return __builtin_clz(v);
273 FORCE_INLINE Square msb(Bitboard b) {
274 return (Square) (63 - __builtin_clzll(b));
277 FORCE_INLINE Square lsb(Bitboard b) {
278 return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
283 FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
285 __asm__("bsfq %1, %0": "=r"(index): "rm"(b) );
286 return (Square) index;
289 FORCE_INLINE Square msb(Bitboard b) {
291 __asm__("bsrq %1, %0": "=r"(index): "rm"(b) );
292 return (Square) index;
297 FORCE_INLINE Square pop_lsb(Bitboard* b) {
298 const Square s = lsb(*b);
303 #else // if !defined(USE_BSFQ)
305 extern Square msb(Bitboard b);
306 extern Square lsb(Bitboard b);
307 extern Square pop_lsb(Bitboard* b);
311 #endif // !defined(BITBOARD_H_INCLUDED)