]> git.sesse.net Git - stockfish/blob - src/bitboard.h
Magic::index()
[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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
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 Bitbases {
29
30 void init();
31 bool probe(Square wksq, Square wpsq, Square bksq, Color us);
32
33 }
34
35 namespace Bitboards {
36
37 void init();
38 const std::string pretty(Bitboard b);
39
40 }
41
42 const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
43
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;
52
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);
61
62 extern int SquareDistance[SQUARE_NB][SQUARE_NB];
63
64 extern Bitboard SquareBB[SQUARE_NB];
65 extern Bitboard FileBB[FILE_NB];
66 extern Bitboard RankBB[RANK_NB];
67 extern Bitboard AdjacentFilesBB[FILE_NB];
68 extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
69 extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
70 extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
71 extern Bitboard DistanceRingBB[SQUARE_NB][8];
72 extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
73 extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
74 extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
75 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
76 extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
77
78
79 /// Magic holds all magic bitboards relevant data for a single square
80 struct Magic {
81   Bitboard  mask;
82   Bitboard  magic;
83   Bitboard* attacks;
84   unsigned  shift;
85
86   /// looks up the index using the 'magic bitboards' approach.
87   unsigned index(Bitboard occupied) const {
88
89       if (HasPext)
90           return unsigned(pext(occupied, mask));
91
92       if (Is64Bit)
93           return unsigned(((occupied & mask) * magic) >> shift);
94
95       unsigned lo = unsigned(occupied) & unsigned(mask);
96       unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
97       return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
98   }
99 };
100
101 extern Magic RookMagics[SQUARE_NB];
102 extern Magic BishopMagics[SQUARE_NB];
103
104
105 /// Overloads of bitwise operators between a Bitboard and a Square for testing
106 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
107
108 inline Bitboard operator&(Bitboard b, Square s) {
109   return b & SquareBB[s];
110 }
111
112 inline Bitboard operator|(Bitboard b, Square s) {
113   return b | SquareBB[s];
114 }
115
116 inline Bitboard operator^(Bitboard b, Square s) {
117   return b ^ SquareBB[s];
118 }
119
120 inline Bitboard& operator|=(Bitboard& b, Square s) {
121   return b |= SquareBB[s];
122 }
123
124 inline Bitboard& operator^=(Bitboard& b, Square s) {
125   return b ^= SquareBB[s];
126 }
127
128 inline bool more_than_one(Bitboard b) {
129   return b & (b - 1);
130 }
131
132
133 /// rank_bb() and file_bb() return a bitboard representing all the squares on
134 /// the given file or rank.
135
136 inline Bitboard rank_bb(Rank r) {
137   return RankBB[r];
138 }
139
140 inline Bitboard rank_bb(Square s) {
141   return RankBB[rank_of(s)];
142 }
143
144 inline Bitboard file_bb(File f) {
145   return FileBB[f];
146 }
147
148 inline Bitboard file_bb(Square s) {
149   return FileBB[file_of(s)];
150 }
151
152
153 /// shift() moves a bitboard one step along direction D. Mainly for pawns
154
155 template<Square D>
156 inline Bitboard shift(Bitboard b) {
157   return  D == NORTH      ?  b             << 8 : D == SOUTH      ?  b             >> 8
158         : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7
159         : D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
160         : 0;
161 }
162
163
164 /// adjacent_files_bb() returns a bitboard representing all the squares on the
165 /// adjacent files of the given one.
166
167 inline Bitboard adjacent_files_bb(File f) {
168   return AdjacentFilesBB[f];
169 }
170
171
172 /// between_bb() returns a bitboard representing all the squares between the two
173 /// given ones. For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with
174 /// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank, file
175 /// or diagonal, 0 is returned.
176
177 inline Bitboard between_bb(Square s1, Square s2) {
178   return BetweenBB[s1][s2];
179 }
180
181
182 /// in_front_bb() returns a bitboard representing all the squares on all the ranks
183 /// in front of the given one, from the point of view of the given color. For
184 /// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
185
186 inline Bitboard in_front_bb(Color c, Rank r) {
187   return InFrontBB[c][r];
188 }
189
190
191 /// forward_bb() returns a bitboard representing all the squares along the line
192 /// in front of the given one, from the point of view of the given color:
193 ///        ForwardBB[c][s] = in_front_bb(c, rank_of(s)) & file_bb(s)
194
195 inline Bitboard forward_bb(Color c, Square s) {
196   return ForwardBB[c][s];
197 }
198
199
200 /// pawn_attack_span() returns a bitboard representing all the squares that can be
201 /// attacked by a pawn of the given color when it moves along its file, starting
202 /// from the given square:
203 ///       PawnAttackSpan[c][s] = in_front_bb(c, rank_of(s)) & adjacent_files_bb(file_of(s));
204
205 inline Bitboard pawn_attack_span(Color c, Square s) {
206   return PawnAttackSpan[c][s];
207 }
208
209
210 /// passed_pawn_mask() returns a bitboard mask which can be used to test if a
211 /// pawn of the given color and on the given square is a passed pawn:
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 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
220 /// straight or on a diagonal line.
221
222 inline bool aligned(Square s1, Square s2, Square s3) {
223   return LineBB[s1][s2] & s3;
224 }
225
226
227 /// distance() functions return the distance between x and y, defined as the
228 /// number of steps for a king in x to reach y. Works with squares, ranks, files.
229
230 template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
231 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
232
233 template<typename T1, typename T2> inline int distance(T2 x, T2 y);
234 template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
235 template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
236
237
238 /// attacks_bb() returns a bitboard representing all the squares attacked by a
239 /// piece of type Pt (bishop or rook) placed on 's'.
240
241 template<PieceType Pt>
242 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
243
244   const Magic& M = Pt == ROOK ? RookMagics[s] : BishopMagics[s];
245   return M.attacks[M.index(occupied)];
246 }
247
248 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
249
250   assert(pt != PAWN);
251
252   switch (pt)
253   {
254   case BISHOP: return attacks_bb<BISHOP>(s, occupied);
255   case ROOK  : return attacks_bb<  ROOK>(s, occupied);
256   case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
257   default    : return PseudoAttacks[pt][s];
258   }
259 }
260
261
262 /// popcount() counts the number of non-zero bits in a bitboard
263
264 inline int popcount(Bitboard b) {
265
266 #ifndef USE_POPCNT
267
268   extern uint8_t PopCnt16[1 << 16];
269   union { Bitboard bb; uint16_t u[4]; } v = { b };
270   return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
271
272 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
273
274   return (int)_mm_popcnt_u64(b);
275
276 #else // Assumed gcc or compatible compiler
277
278   return __builtin_popcountll(b);
279
280 #endif
281 }
282
283
284 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
285
286 #if defined(__GNUC__)
287
288 inline Square lsb(Bitboard b) {
289   assert(b);
290   return Square(__builtin_ctzll(b));
291 }
292
293 inline Square msb(Bitboard b) {
294   assert(b);
295   return Square(63 ^ __builtin_clzll(b));
296 }
297
298 #elif defined(_WIN64) && defined(_MSC_VER)
299
300 inline Square lsb(Bitboard b) {
301   assert(b);
302   unsigned long idx;
303   _BitScanForward64(&idx, b);
304   return (Square) idx;
305 }
306
307 inline Square msb(Bitboard b) {
308   assert(b);
309   unsigned long idx;
310   _BitScanReverse64(&idx, b);
311   return (Square) idx;
312 }
313
314 #else
315
316 #define NO_BSF // Fallback on software implementation for other cases
317
318 Square lsb(Bitboard b);
319 Square msb(Bitboard b);
320
321 #endif
322
323
324 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
325
326 inline Square pop_lsb(Bitboard* b) {
327   const Square s = lsb(*b);
328   *b &= *b - 1;
329   return s;
330 }
331
332
333 /// frontmost_sq() and backmost_sq() return the square corresponding to the
334 /// most/least advanced bit relative to the given color.
335
336 inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
337 inline Square  backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
338
339 #endif // #ifndef BITBOARD_H_INCLUDED