]> git.sesse.net Git - stockfish/blob - src/bitboard.h
Remove DistanceRing #2107
[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-2019 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 constexpr Bitboard AllSquares = ~Bitboard(0);
43 constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
44
45 constexpr Bitboard FileABB = 0x0101010101010101ULL;
46 constexpr Bitboard FileBBB = FileABB << 1;
47 constexpr Bitboard FileCBB = FileABB << 2;
48 constexpr Bitboard FileDBB = FileABB << 3;
49 constexpr Bitboard FileEBB = FileABB << 4;
50 constexpr Bitboard FileFBB = FileABB << 5;
51 constexpr Bitboard FileGBB = FileABB << 6;
52 constexpr Bitboard FileHBB = FileABB << 7;
53
54 constexpr Bitboard Rank1BB = 0xFF;
55 constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
56 constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
57 constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
58 constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
59 constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
60 constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
61 constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
62
63 constexpr Bitboard QueenSide   = FileABB | FileBBB | FileCBB | FileDBB;
64 constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
65 constexpr Bitboard KingSide    = FileEBB | FileFBB | FileGBB | FileHBB;
66 constexpr Bitboard Center      = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
67
68 extern uint8_t PopCnt16[1 << 16];
69 extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
70
71 extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
72 extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
73 extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
74 extern Bitboard KingFlank[FILE_NB];
75 extern Bitboard SquareBB[SQUARE_NB];
76
77
78 /// Magic holds all magic bitboards relevant data for a single square
79 struct Magic {
80   Bitboard  mask;
81   Bitboard  magic;
82   Bitboard* attacks;
83   unsigned  shift;
84
85   // Compute the attack's index using the 'magic bitboards' approach
86   unsigned index(Bitboard occupied) const {
87
88     if (HasPext)
89         return unsigned(pext(occupied, mask));
90
91     if (Is64Bit)
92         return unsigned(((occupied & mask) * magic) >> shift);
93
94     unsigned lo = unsigned(occupied) & unsigned(mask);
95     unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
96     return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
97   }
98 };
99
100 extern Magic RookMagics[SQUARE_NB];
101 extern Magic BishopMagics[SQUARE_NB];
102
103 inline Bitboard square_bb(Square s) {
104   assert(s >= SQ_A1 && s <= SQ_H8);
105   return SquareBB[s];
106 }
107
108 /// Overloads of bitwise operators between a Bitboard and a Square for testing
109 /// whether a given bit is set in a bitboard, and for setting and clearing bits.
110
111 inline Bitboard  operator&( Bitboard  b, Square s) { return b &  square_bb(s); }
112 inline Bitboard  operator|( Bitboard  b, Square s) { return b |  square_bb(s); }
113 inline Bitboard  operator^( Bitboard  b, Square s) { return b ^  square_bb(s); }
114 inline Bitboard& operator|=(Bitboard& b, Square s) { return b |= square_bb(s); }
115 inline Bitboard& operator^=(Bitboard& b, Square s) { return b ^= square_bb(s); }
116
117 constexpr bool more_than_one(Bitboard b) {
118   return b & (b - 1);
119 }
120
121 inline bool opposite_colors(Square s1, Square s2) {
122   return bool(DarkSquares & s1) != bool(DarkSquares & s2);
123 }
124
125
126 /// rank_bb() and file_bb() return a bitboard representing all the squares on
127 /// the given file or rank.
128
129 inline Bitboard rank_bb(Rank r) {
130   return Rank1BB << (8 * r);
131 }
132
133 inline Bitboard rank_bb(Square s) {
134   return rank_bb(rank_of(s));
135 }
136
137 inline Bitboard file_bb(File f) {
138   return FileABB << f;
139 }
140
141 inline Bitboard file_bb(Square s) {
142   return file_bb(file_of(s));
143 }
144
145
146 /// shift() moves a bitboard one step along direction D
147
148 template<Direction D>
149 constexpr Bitboard shift(Bitboard b) {
150   return  D == NORTH      ?  b             << 8 : D == SOUTH      ?  b             >> 8
151         : D == EAST       ? (b & ~FileHBB) << 1 : D == WEST       ? (b & ~FileABB) >> 1
152         : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
153         : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
154         : 0;
155 }
156
157
158 /// pawn_attacks_bb() returns the squares attacked by pawns of the given color
159 /// from the squares in the given bitboard.
160
161 template<Color C>
162 constexpr Bitboard pawn_attacks_bb(Bitboard b) {
163   return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
164                     : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
165 }
166
167
168 /// pawn_double_attacks_bb() returns the squares doubly attacked by pawns of the
169 /// given color from the squares in the given bitboard.
170
171 template<Color C>
172 constexpr Bitboard pawn_double_attacks_bb(Bitboard b) {
173   return C == WHITE ? shift<NORTH_WEST>(b) & shift<NORTH_EAST>(b)
174                     : shift<SOUTH_WEST>(b) & shift<SOUTH_EAST>(b);
175 }
176
177
178 /// adjacent_files_bb() returns a bitboard representing all the squares on the
179 /// adjacent files of the given one.
180
181 inline Bitboard adjacent_files_bb(File f) {
182   return shift<EAST>(file_bb(f)) | shift<WEST>(file_bb(f));
183 }
184
185
186 /// between_bb() returns squares that are linearly between the given squares
187 /// If the given squares are not on a same file/rank/diagonal, return 0.
188
189 inline Bitboard between_bb(Square s1, Square s2) {
190   return LineBB[s1][s2] & ( (AllSquares << (s1 +  (s1 < s2)))
191                            ^(AllSquares << (s2 + !(s1 < s2))));
192 }
193
194
195 /// forward_ranks_bb() returns a bitboard representing the squares on the ranks
196 /// in front of the given one, from the point of view of the given color. For instance,
197 /// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
198
199 inline Bitboard forward_ranks_bb(Color c, Square s) {
200   return c == WHITE ? ~Rank1BB << 8 * (rank_of(s) - RANK_1)
201                     : ~Rank8BB >> 8 * (RANK_8 - rank_of(s));
202 }
203
204
205 /// forward_file_bb() returns a bitboard representing all the squares along the
206 /// line in front of the given one, from the point of view of the given color.
207
208 inline Bitboard forward_file_bb(Color c, Square s) {
209   return forward_ranks_bb(c, s) & file_bb(s);
210 }
211
212
213 /// pawn_attack_span() returns a bitboard representing all the squares that can
214 /// be attacked by a pawn of the given color when it moves along its file,
215 /// starting from the given square.
216
217 inline Bitboard pawn_attack_span(Color c, Square s) {
218   return forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));
219 }
220
221
222 /// passed_pawn_span() returns a bitboard which can be used to test if a pawn of
223 /// the given color and on the given square is a passed pawn.
224
225 inline Bitboard passed_pawn_span(Color c, Square s) {
226   return forward_ranks_bb(c, s) & (adjacent_files_bb(file_of(s)) | file_bb(s));
227 }
228
229
230 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
231 /// straight or on a diagonal line.
232
233 inline bool aligned(Square s1, Square s2, Square s3) {
234   return LineBB[s1][s2] & s3;
235 }
236
237
238 /// distance() functions return the distance between x and y, defined as the
239 /// number of steps for a king in x to reach y.
240
241 template<typename T1 = Square> inline int distance(Square x, Square y);
242 template<> inline int distance<File>(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
243 template<> inline int distance<Rank>(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
244 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
245
246 template<class T> constexpr const T& clamp(const T& v, const T& lo, const T&  hi) {
247   return v < lo ? lo : v > hi ? hi : v;
248 }
249
250 /// attacks_bb() returns a bitboard representing all the squares attacked by a
251 /// piece of type Pt (bishop or rook) placed on 's'.
252
253 template<PieceType Pt>
254 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
255
256   const Magic& m = Pt == ROOK ? RookMagics[s] : BishopMagics[s];
257   return m.attacks[m.index(occupied)];
258 }
259
260 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
261
262   assert(pt != PAWN);
263
264   switch (pt)
265   {
266   case BISHOP: return attacks_bb<BISHOP>(s, occupied);
267   case ROOK  : return attacks_bb<  ROOK>(s, occupied);
268   case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
269   default    : return PseudoAttacks[pt][s];
270   }
271 }
272
273
274 /// popcount() counts the number of non-zero bits in a bitboard
275
276 inline int popcount(Bitboard b) {
277
278 #ifndef USE_POPCNT
279
280   union { Bitboard bb; uint16_t u[4]; } v = { b };
281   return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
282
283 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
284
285   return (int)_mm_popcnt_u64(b);
286
287 #else // Assumed gcc or compatible compiler
288
289   return __builtin_popcountll(b);
290
291 #endif
292 }
293
294
295 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
296
297 #if defined(__GNUC__)  // GCC, Clang, ICC
298
299 inline Square lsb(Bitboard b) {
300   assert(b);
301   return Square(__builtin_ctzll(b));
302 }
303
304 inline Square msb(Bitboard b) {
305   assert(b);
306   return Square(63 ^ __builtin_clzll(b));
307 }
308
309 #elif defined(_MSC_VER)  // MSVC
310
311 #ifdef _WIN64  // MSVC, WIN64
312
313 inline Square lsb(Bitboard b) {
314   assert(b);
315   unsigned long idx;
316   _BitScanForward64(&idx, b);
317   return (Square) idx;
318 }
319
320 inline Square msb(Bitboard b) {
321   assert(b);
322   unsigned long idx;
323   _BitScanReverse64(&idx, b);
324   return (Square) idx;
325 }
326
327 #else  // MSVC, WIN32
328
329 inline Square lsb(Bitboard b) {
330   assert(b);
331   unsigned long idx;
332
333   if (b & 0xffffffff) {
334       _BitScanForward(&idx, int32_t(b));
335       return Square(idx);
336   } else {
337       _BitScanForward(&idx, int32_t(b >> 32));
338       return Square(idx + 32);
339   }
340 }
341
342 inline Square msb(Bitboard b) {
343   assert(b);
344   unsigned long idx;
345
346   if (b >> 32) {
347       _BitScanReverse(&idx, int32_t(b >> 32));
348       return Square(idx + 32);
349   } else {
350       _BitScanReverse(&idx, int32_t(b));
351       return Square(idx);
352   }
353 }
354
355 #endif
356
357 #else  // Compiler is neither GCC nor MSVC compatible
358
359 #error "Compiler not supported."
360
361 #endif
362
363
364 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
365
366 inline Square pop_lsb(Bitboard* b) {
367   const Square s = lsb(*b);
368   *b &= *b - 1;
369   return s;
370 }
371
372
373 /// frontmost_sq() and backmost_sq() return the square corresponding to the
374 /// most/least advanced bit relative to the given color.
375
376 inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
377 inline Square  backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
378
379 #endif // #ifndef BITBOARD_H_INCLUDED