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