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