]> git.sesse.net Git - stockfish/blob - src/bitboard.h
Assorted work in uci.cpp
[stockfish] / src / bitboard.h
1 /*
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-2014 Marco Costalba, Joona Kiiski, Tord Romstad
5
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.
10
11
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.
16
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/>.
19 */
20
21 #ifndef BITBOARD_H_INCLUDED
22 #define BITBOARD_H_INCLUDED
23
24 #include <string>
25
26 #include "types.h"
27
28 namespace Bitboards {
29
30 void init();
31 const std::string pretty(Bitboard b);
32
33 }
34
35 namespace Bitbases {
36
37 void init_kpk();
38 bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
39
40 }
41
42 const Bitboard FileABB = 0x0101010101010101ULL;
43 const Bitboard FileBBB = FileABB << 1;
44 const Bitboard FileCBB = FileABB << 2;
45 const Bitboard FileDBB = FileABB << 3;
46 const Bitboard FileEBB = FileABB << 4;
47 const Bitboard FileFBB = FileABB << 5;
48 const Bitboard FileGBB = FileABB << 6;
49 const Bitboard FileHBB = FileABB << 7;
50
51 const Bitboard Rank1BB = 0xFF;
52 const Bitboard Rank2BB = Rank1BB << (8 * 1);
53 const Bitboard Rank3BB = Rank1BB << (8 * 2);
54 const Bitboard Rank4BB = Rank1BB << (8 * 3);
55 const Bitboard Rank5BB = Rank1BB << (8 * 4);
56 const Bitboard Rank6BB = Rank1BB << (8 * 5);
57 const Bitboard Rank7BB = Rank1BB << (8 * 6);
58 const Bitboard Rank8BB = Rank1BB << (8 * 7);
59
60 extern Bitboard RookMasks[SQUARE_NB];
61 extern Bitboard RookMagics[SQUARE_NB];
62 extern Bitboard* RookAttacks[SQUARE_NB];
63 extern unsigned RookShifts[SQUARE_NB];
64
65 extern Bitboard BishopMasks[SQUARE_NB];
66 extern Bitboard BishopMagics[SQUARE_NB];
67 extern Bitboard* BishopAttacks[SQUARE_NB];
68 extern unsigned BishopShifts[SQUARE_NB];
69
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 LineBB[SQUARE_NB][SQUARE_NB];
78 extern Bitboard DistanceRingsBB[SQUARE_NB][8];
79 extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
80 extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
81 extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
82 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
83
84 extern int SquareDistance[SQUARE_NB][SQUARE_NB];
85
86 const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
87
88 /// Overloads of bitwise operators between a Bitboard and a Square for testing
89 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
90
91 inline Bitboard operator&(Bitboard b, Square s) {
92   return b & SquareBB[s];
93 }
94
95 inline Bitboard& operator|=(Bitboard& b, Square s) {
96   return b |= SquareBB[s];
97 }
98
99 inline Bitboard& operator^=(Bitboard& b, Square s) {
100   return b ^= SquareBB[s];
101 }
102
103 inline Bitboard operator|(Bitboard b, Square s) {
104   return b | SquareBB[s];
105 }
106
107 inline Bitboard operator^(Bitboard b, Square s) {
108   return b ^ SquareBB[s];
109 }
110
111 inline bool more_than_one(Bitboard b) {
112   return b & (b - 1);
113 }
114
115 template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
116 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
117
118 template<typename T1, typename T2> inline int distance(T2 x, T2 y);
119 template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
120 template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
121
122
123 /// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
124
125 template<Square Delta>
126 inline Bitboard shift_bb(Bitboard b) {
127
128   return  Delta == DELTA_N  ?  b             << 8 : Delta == DELTA_S  ?  b             >> 8
129         : Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
130         : Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
131         : 0;
132 }
133
134
135 /// rank_bb() and file_bb() take a file or a square as input and return
136 /// a bitboard representing all squares on the given file or rank.
137
138 inline Bitboard rank_bb(Rank r) {
139   return RankBB[r];
140 }
141
142 inline Bitboard rank_bb(Square s) {
143   return RankBB[rank_of(s)];
144 }
145
146 inline Bitboard file_bb(File f) {
147   return FileBB[f];
148 }
149
150 inline Bitboard file_bb(Square s) {
151   return FileBB[file_of(s)];
152 }
153
154
155 /// adjacent_files_bb() takes a file as input and returns a bitboard representing
156 /// all squares on the adjacent files.
157
158 inline Bitboard adjacent_files_bb(File f) {
159   return AdjacentFilesBB[f];
160 }
161
162
163 /// in_front_bb() takes a color and a rank as input, and returns a bitboard
164 /// representing all the squares on all ranks in front of the rank, from the
165 /// given color's point of view. For instance, in_front_bb(BLACK, RANK_3) will
166 /// give all squares on ranks 1 and 2.
167
168 inline Bitboard in_front_bb(Color c, Rank r) {
169   return InFrontBB[c][r];
170 }
171
172
173 /// between_bb() returns a bitboard representing all squares between two squares.
174 /// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
175 /// square d5 and e6 set.  If s1 and s2 are not on the same rank, file or diagonal,
176 /// 0 is returned.
177
178 inline Bitboard between_bb(Square s1, Square s2) {
179   return BetweenBB[s1][s2];
180 }
181
182
183 /// forward_bb() takes a color and a square as input, and returns a bitboard
184 /// representing all squares along the line in front of the square, from the
185 /// point of view of the given color. Definition of the table is:
186 /// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
187
188 inline Bitboard forward_bb(Color c, Square s) {
189   return ForwardBB[c][s];
190 }
191
192
193 /// pawn_attack_span() takes a color and a square as input, and returns a bitboard
194 /// representing all squares that can be attacked by a pawn of the given color
195 /// when it moves along its file starting from the given square. Definition is:
196 /// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
197
198 inline Bitboard pawn_attack_span(Color c, Square s) {
199   return PawnAttackSpan[c][s];
200 }
201
202
203 /// passed_pawn_mask() takes a color and a square as input, and returns a
204 /// bitboard mask which can be used to test if a pawn of the given color on
205 /// the given square is a passed pawn. Definition of the table is:
206 /// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
207
208 inline Bitboard passed_pawn_mask(Color c, Square s) {
209   return PassedPawnMask[c][s];
210 }
211
212
213 /// squares_of_color() returns a bitboard representing all squares with the same
214 /// color of the given square.
215
216 inline Bitboard squares_of_color(Square s) {
217   return DarkSquares & s ? DarkSquares : ~DarkSquares;
218 }
219
220
221 /// aligned() returns true if the squares s1, s2 and s3 are aligned
222 /// either on a straight or on a diagonal line.
223
224 inline bool aligned(Square s1, Square s2, Square s3) {
225   return LineBB[s1][s2] & s3;
226 }
227
228
229 /// Functions for computing sliding attack bitboards. Function attacks_bb() takes
230 /// a square and a bitboard of occupied squares as input, and returns a bitboard
231 /// representing all squares attacked by Pt (bishop or rook) on the given square.
232 template<PieceType Pt>
233 FORCE_INLINE unsigned magic_index(Square s, Bitboard occupied) {
234
235   Bitboard* const Masks  = Pt == ROOK ? RookMasks  : BishopMasks;
236   Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics;
237   unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts;
238
239   if (HasPext)
240       return unsigned(_pext_u64(occupied, Masks[s]));
241
242   if (Is64Bit)
243       return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]);
244
245   unsigned lo = unsigned(occupied) & unsigned(Masks[s]);
246   unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32);
247   return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
248 }
249
250 template<PieceType Pt>
251 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
252   return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)];
253 }
254
255 inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) {
256
257   switch (type_of(pc))
258   {
259   case BISHOP: return attacks_bb<BISHOP>(s, occupied);
260   case ROOK  : return attacks_bb<ROOK>(s, occupied);
261   case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
262   default    : return StepAttacksBB[pc][s];
263   }
264 }
265
266 /// lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
267 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
268
269 #ifdef USE_BSFQ
270
271 #  if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
272
273 FORCE_INLINE Square lsb(Bitboard b) {
274   unsigned long idx;
275   _BitScanForward64(&idx, b);
276   return (Square) idx;
277 }
278
279 FORCE_INLINE Square msb(Bitboard b) {
280   unsigned long idx;
281   _BitScanReverse64(&idx, b);
282   return (Square) idx;
283 }
284
285 #  elif defined(__arm__)
286
287 FORCE_INLINE int lsb32(uint32_t v) {
288   __asm__("rbit %0, %1" : "=r"(v) : "r"(v));
289   return __builtin_clz(v);
290 }
291
292 FORCE_INLINE Square msb(Bitboard b) {
293   return (Square) (63 - __builtin_clzll(b));
294 }
295
296 FORCE_INLINE Square lsb(Bitboard b) {
297   return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
298 }
299
300 #  else
301
302 FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
303   Bitboard idx;
304   __asm__("bsfq %1, %0": "=r"(idx): "rm"(b) );
305   return (Square) idx;
306 }
307
308 FORCE_INLINE Square msb(Bitboard b) {
309   Bitboard idx;
310   __asm__("bsrq %1, %0": "=r"(idx): "rm"(b) );
311   return (Square) idx;
312 }
313
314 #  endif
315
316 FORCE_INLINE Square pop_lsb(Bitboard* b) {
317   const Square s = lsb(*b);
318   *b &= *b - 1;
319   return s;
320 }
321
322 #else // if defined(USE_BSFQ)
323
324 extern Square msb(Bitboard b);
325 extern Square lsb(Bitboard b);
326 extern Square pop_lsb(Bitboard* b);
327
328 #endif
329
330 /// frontmost_sq() and backmost_sq() find the square corresponding to the
331 /// most/least advanced bit relative to the given color.
332
333 inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
334 inline Square  backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
335
336 #endif // #ifndef BITBOARD_H_INCLUDED