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-2012 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 uint32_t probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm);
42 extern Bitboard RMasks[64];
43 extern Bitboard RMagics[64];
44 extern Bitboard* RAttacks[64];
45 extern unsigned RShifts[64];
47 extern Bitboard BMasks[64];
48 extern Bitboard BMagics[64];
49 extern Bitboard* BAttacks[64];
50 extern unsigned BShifts[64];
52 extern Bitboard SquareBB[64];
53 extern Bitboard FileBB[8];
54 extern Bitboard RankBB[8];
55 extern Bitboard AdjacentFilesBB[8];
56 extern Bitboard ThisAndAdjacentFilesBB[8];
57 extern Bitboard InFrontBB[2][8];
58 extern Bitboard StepAttacksBB[16][64];
59 extern Bitboard BetweenBB[64][64];
60 extern Bitboard DistanceRingsBB[64][8];
61 extern Bitboard ForwardBB[2][64];
62 extern Bitboard PassedPawnMask[2][64];
63 extern Bitboard AttackSpanMask[2][64];
64 extern Bitboard PseudoAttacks[6][64];
67 /// Overloads of bitwise operators between a Bitboard and a Square for testing
68 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
70 inline Bitboard operator&(Bitboard b, Square s) {
71 return b & SquareBB[s];
74 inline Bitboard& operator|=(Bitboard& b, Square s) {
75 return b |= SquareBB[s];
78 inline Bitboard& operator^=(Bitboard& b, Square s) {
79 return b ^= SquareBB[s];
82 inline Bitboard operator|(Bitboard b, Square s) {
83 return b | SquareBB[s];
86 inline Bitboard operator^(Bitboard b, Square s) {
87 return b ^ SquareBB[s];
91 /// more_than_one() returns true if in 'b' there is more than one bit set
93 inline bool more_than_one(Bitboard b) {
98 /// rank_bb() and file_bb() take a file or a square as input and return
99 /// a bitboard representing all squares on the given file or rank.
101 inline Bitboard rank_bb(Rank r) {
105 inline Bitboard rank_bb(Square s) {
106 return RankBB[rank_of(s)];
109 inline Bitboard file_bb(File f) {
113 inline Bitboard file_bb(Square s) {
114 return FileBB[file_of(s)];
118 /// adjacent_files_bb takes a file as input and returns a bitboard representing
119 /// all squares on the adjacent files.
121 inline Bitboard adjacent_files_bb(File f) {
122 return AdjacentFilesBB[f];
126 /// this_and_adjacent_files_bb takes a file as input and returns a bitboard
127 /// representing all squares on the given and adjacent files.
129 inline Bitboard this_and_adjacent_files_bb(File f) {
130 return ThisAndAdjacentFilesBB[f];
134 /// in_front_bb() takes a color and a rank or square as input, and returns a
135 /// bitboard representing all the squares on all ranks in front of the rank
136 /// (or square), from the given color's point of view. For instance,
137 /// in_front_bb(WHITE, RANK_5) will give all squares on ranks 6, 7 and 8, while
138 /// in_front_bb(BLACK, SQ_D3) will give all squares on ranks 1 and 2.
140 inline Bitboard in_front_bb(Color c, Rank r) {
141 return InFrontBB[c][r];
144 inline Bitboard in_front_bb(Color c, Square s) {
145 return InFrontBB[c][rank_of(s)];
149 /// between_bb returns a bitboard representing all squares between two squares.
150 /// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
151 /// square d5 and e6 set. If s1 and s2 are not on the same line, file or diagonal,
154 inline Bitboard between_bb(Square s1, Square s2) {
155 return BetweenBB[s1][s2];
159 /// forward_bb takes a color and a square as input, and returns a bitboard
160 /// representing all squares along the line in front of the square, from the
161 /// point of view of the given color. Definition of the table is:
162 /// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
164 inline Bitboard forward_bb(Color c, Square s) {
165 return ForwardBB[c][s];
169 /// passed_pawn_mask takes a color and a square as input, and returns a
170 /// bitboard mask which can be used to test if a pawn of the given color on
171 /// the given square is a passed pawn. Definition of the table is:
172 /// PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_adjacent_files_bb(s)
174 inline Bitboard passed_pawn_mask(Color c, Square s) {
175 return PassedPawnMask[c][s];
179 /// attack_span_mask takes a color and a square as input, and returns a bitboard
180 /// representing all squares that can be attacked by a pawn of the given color
181 /// when it moves along its file starting from the given square. Definition is:
182 /// AttackSpanMask[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
184 inline Bitboard attack_span_mask(Color c, Square s) {
185 return AttackSpanMask[c][s];
189 /// squares_aligned returns true if the squares s1, s2 and s3 are aligned
190 /// either on a straight or on a diagonal line.
192 inline bool squares_aligned(Square s1, Square s2, Square s3) {
193 return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3])
194 & ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]);
198 /// same_color_squares() returns a bitboard representing all squares with
199 /// the same color of the given square.
201 inline Bitboard same_color_squares(Square s) {
202 return Bitboard(0xAA55AA55AA55AA55ULL) & s ? 0xAA55AA55AA55AA55ULL
203 : ~0xAA55AA55AA55AA55ULL;
207 /// Functions for computing sliding attack bitboards. Function attacks_bb() takes
208 /// a square and a bitboard of occupied squares as input, and returns a bitboard
209 /// representing all squares attacked by Pt (bishop or rook) on the given square.
210 template<PieceType Pt>
211 FORCE_INLINE unsigned magic_index(Square s, Bitboard occ) {
213 Bitboard* const Masks = Pt == ROOK ? RMasks : BMasks;
214 Bitboard* const Magics = Pt == ROOK ? RMagics : BMagics;
215 unsigned* const Shifts = Pt == ROOK ? RShifts : BShifts;
218 return unsigned(((occ & Masks[s]) * Magics[s]) >> Shifts[s]);
220 unsigned lo = unsigned(occ) & unsigned(Masks[s]);
221 unsigned hi = unsigned(occ >> 32) & unsigned(Masks[s] >> 32);
222 return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
225 template<PieceType Pt>
226 inline Bitboard attacks_bb(Square s, Bitboard occ) {
227 return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
231 /// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
232 /// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
234 #if defined(USE_BSFQ)
236 # if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
238 FORCE_INLINE Square lsb(Bitboard b) {
240 _BitScanForward64(&index, b);
241 return (Square) index;
244 FORCE_INLINE Square msb(Bitboard b) {
246 _BitScanReverse64(&index, b);
247 return (Square) index;
252 FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
254 __asm__("bsfq %1, %0": "=r"(index): "rm"(b) );
255 return (Square) index;
258 FORCE_INLINE Square msb(Bitboard b) {
260 __asm__("bsrq %1, %0": "=r"(index): "rm"(b) );
261 return (Square) index;
266 FORCE_INLINE Square pop_lsb(Bitboard* b) {
267 const Square s = lsb(*b);
272 #else // if !defined(USE_BSFQ)
274 extern Square msb(Bitboard b);
275 extern Square lsb(Bitboard b);
276 extern Square pop_lsb(Bitboard* b);
280 #endif // !defined(BITBOARD_H_INCLUDED)