]> git.sesse.net Git - stockfish/blob - src/bitboard.h
Tweak initiative and Pawn PSQT (#1957)
[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 extern int SquareDistance[SQUARE_NB][SQUARE_NB];
64
65 extern Bitboard SquareBB[SQUARE_NB];
66 extern Bitboard FileBB[FILE_NB];
67 extern Bitboard RankBB[RANK_NB];
68 extern Bitboard ForwardRanksBB[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 ForwardFileBB[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   // Compute the attack's 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   assert(s >= SQ_A1 && s <= SQ_H8);
110   return b & SquareBB[s];
111 }
112
113 inline Bitboard operator|(Bitboard b, Square s) {
114   assert(s >= SQ_A1 && s <= SQ_H8);
115   return b | SquareBB[s];
116 }
117
118 inline Bitboard operator^(Bitboard b, Square s) {
119   assert(s >= SQ_A1 && s <= SQ_H8);
120   return b ^ SquareBB[s];
121 }
122
123 inline Bitboard& operator|=(Bitboard& b, Square s) {
124   assert(s >= SQ_A1 && s <= SQ_H8);
125   return b |= SquareBB[s];
126 }
127
128 inline Bitboard& operator^=(Bitboard& b, Square s) {
129   assert(s >= SQ_A1 && s <= SQ_H8);
130   return b ^= SquareBB[s];
131 }
132
133 constexpr bool more_than_one(Bitboard b) {
134   return b & (b - 1);
135 }
136
137 inline bool opposite_colors(Square s1, Square s2) {
138   return bool(DarkSquares & s1) != bool(DarkSquares & s2);
139 }
140
141 /// rank_bb() and file_bb() return a bitboard representing all the squares on
142 /// 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 /// shift() moves a bitboard one step along direction D (mainly for pawns)
162
163 template<Direction D>
164 constexpr Bitboard shift(Bitboard b) {
165   return  D == NORTH      ?  b             << 8 : D == SOUTH      ?  b             >> 8
166         : D == EAST       ? (b & ~FileHBB) << 1 : D == WEST       ? (b & ~FileABB) >> 1
167         : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
168         : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
169         : 0;
170 }
171
172
173 /// pawn_attacks_bb() returns the pawn attacks for the given color from the
174 /// squares in the given bitboard.
175
176 template<Color C>
177 constexpr Bitboard pawn_attacks_bb(Bitboard b) {
178   return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
179                     : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
180 }
181
182
183 /// double_pawn_attacks_bb() returns the pawn attacks for the given color
184 /// from the squares in the given bitboard.
185
186 template<Color C>
187 constexpr Bitboard double_pawn_attacks_bb(Bitboard b) {
188   return C == WHITE ? shift<NORTH_WEST>(b) & shift<NORTH_EAST>(b)
189                     : shift<SOUTH_WEST>(b) & shift<SOUTH_EAST>(b);
190 }
191
192
193 /// adjacent_files_bb() returns a bitboard representing all the squares on the
194 /// adjacent files of the given one.
195
196 inline Bitboard adjacent_files_bb(File f) {
197   return shift<EAST>(FileBB[f]) | shift<WEST>(FileBB[f]);
198 }
199
200
201 /// between_bb() returns a bitboard representing all the squares between the two
202 /// given ones. For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with
203 /// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank, file
204 /// or diagonal, 0 is returned.
205
206 inline Bitboard between_bb(Square s1, Square s2) {
207   return BetweenBB[s1][s2];
208 }
209
210
211 /// forward_ranks_bb() returns a bitboard representing the squares on all the ranks
212 /// in front of the given one, from the point of view of the given color. For instance,
213 /// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
214
215 inline Bitboard forward_ranks_bb(Color c, Square s) {
216   return ForwardRanksBB[c][rank_of(s)];
217 }
218
219
220 /// forward_file_bb() returns a bitboard representing all the squares along the line
221 /// in front of the given one, from the point of view of the given color:
222 ///      ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s)
223
224 inline Bitboard forward_file_bb(Color c, Square s) {
225   return ForwardFileBB[c][s];
226 }
227
228
229 /// pawn_attack_span() returns a bitboard representing all the squares that can be
230 /// attacked by a pawn of the given color when it moves along its file, starting
231 /// from the given square:
232 ///      PawnAttackSpan[c][s] = forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));
233
234 inline Bitboard pawn_attack_span(Color c, Square s) {
235   return PawnAttackSpan[c][s];
236 }
237
238
239 /// passed_pawn_mask() returns a bitboard mask which can be used to test if a
240 /// pawn of the given color and on the given square is a passed pawn:
241 ///      PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_file_bb(c, s)
242
243 inline Bitboard passed_pawn_mask(Color c, Square s) {
244   return PassedPawnMask[c][s];
245 }
246
247
248 /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
249 /// straight or on a diagonal line.
250
251 inline bool aligned(Square s1, Square s2, Square s3) {
252   return LineBB[s1][s2] & s3;
253 }
254
255
256 /// distance() functions return the distance between x and y, defined as the
257 /// number of steps for a king in x to reach y. Works with squares, ranks, files.
258
259 template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
260 template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
261
262 template<typename T1, typename T2> inline int distance(T2 x, T2 y);
263 template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
264 template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
265
266
267 /// attacks_bb() returns a bitboard representing all the squares attacked by a
268 /// piece of type Pt (bishop or rook) placed on 's'.
269
270 template<PieceType Pt>
271 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
272
273   const Magic& m = Pt == ROOK ? RookMagics[s] : BishopMagics[s];
274   return m.attacks[m.index(occupied)];
275 }
276
277 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
278
279   assert(pt != PAWN);
280
281   switch (pt)
282   {
283   case BISHOP: return attacks_bb<BISHOP>(s, occupied);
284   case ROOK  : return attacks_bb<  ROOK>(s, occupied);
285   case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
286   default    : return PseudoAttacks[pt][s];
287   }
288 }
289
290
291 /// popcount() counts the number of non-zero bits in a bitboard
292
293 inline int popcount(Bitboard b) {
294
295 #ifndef USE_POPCNT
296
297   extern uint8_t PopCnt16[1 << 16];
298   union { Bitboard bb; uint16_t u[4]; } v = { b };
299   return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
300
301 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
302
303   return (int)_mm_popcnt_u64(b);
304
305 #else // Assumed gcc or compatible compiler
306
307   return __builtin_popcountll(b);
308
309 #endif
310 }
311
312
313 /// lsb() and msb() return the least/most significant bit in a non-zero bitboard
314
315 #if defined(__GNUC__)  // GCC, Clang, ICC
316
317 inline Square lsb(Bitboard b) {
318   assert(b);
319   return Square(__builtin_ctzll(b));
320 }
321
322 inline Square msb(Bitboard b) {
323   assert(b);
324   return Square(63 ^ __builtin_clzll(b));
325 }
326
327 #elif defined(_MSC_VER)  // MSVC
328
329 #ifdef _WIN64  // MSVC, WIN64
330
331 inline Square lsb(Bitboard b) {
332   assert(b);
333   unsigned long idx;
334   _BitScanForward64(&idx, b);
335   return (Square) idx;
336 }
337
338 inline Square msb(Bitboard b) {
339   assert(b);
340   unsigned long idx;
341   _BitScanReverse64(&idx, b);
342   return (Square) idx;
343 }
344
345 #else  // MSVC, WIN32
346
347 inline Square lsb(Bitboard b) {
348   assert(b);
349   unsigned long idx;
350
351   if (b & 0xffffffff) {
352       _BitScanForward(&idx, int32_t(b));
353       return Square(idx);
354   } else {
355       _BitScanForward(&idx, int32_t(b >> 32));
356       return Square(idx + 32);
357   }
358 }
359
360 inline Square msb(Bitboard b) {
361   assert(b);
362   unsigned long idx;
363
364   if (b >> 32) {
365       _BitScanReverse(&idx, int32_t(b >> 32));
366       return Square(idx + 32);
367   } else {
368       _BitScanReverse(&idx, int32_t(b));
369       return Square(idx);
370   }
371 }
372
373 #endif
374
375 #else  // Compiler is neither GCC nor MSVC compatible
376
377 #error "Compiler not supported."
378
379 #endif
380
381
382 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
383
384 inline Square pop_lsb(Bitboard* b) {
385   const Square s = lsb(*b);
386   *b &= *b - 1;
387   return s;
388 }
389
390
391 /// frontmost_sq() and backmost_sq() return the square corresponding to the
392 /// most/least advanced bit relative to the given color.
393
394 inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
395 inline Square  backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
396
397 #endif // #ifndef BITBOARD_H_INCLUDED