Rev 169 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 169 | Rev 185 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
| 5 | Copyright (C) 2015- |
5 | Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
| 6 | 6 | ||
| 7 | Stockfish is free software: you can redistribute it and/or modify |
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 |
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 |
9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
10 | (at your option) any later version. |
| Line 37... | Line 37... | ||
| 37 | void init(); |
37 | void init(); |
| 38 | const std::string pretty(Bitboard b); |
38 | const std::string pretty(Bitboard b); |
| 39 | 39 | ||
| 40 | } |
40 | } |
| 41 | 41 | ||
| 42 |
|
42 | constexpr Bitboard AllSquares = ~Bitboard(0); |
| 43 |
|
43 | constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL; |
| 44 | 44 | ||
| 45 |
|
45 | constexpr Bitboard FileABB = 0x0101010101010101ULL; |
| 46 |
|
46 | constexpr Bitboard FileBBB = FileABB << 1; |
| 47 |
|
47 | constexpr Bitboard FileCBB = FileABB << 2; |
| 48 |
|
48 | constexpr Bitboard FileDBB = FileABB << 3; |
| 49 |
|
49 | constexpr Bitboard FileEBB = FileABB << 4; |
| 50 |
|
50 | constexpr Bitboard FileFBB = FileABB << 5; |
| 51 |
|
51 | constexpr Bitboard FileGBB = FileABB << 6; |
| 52 |
|
52 | constexpr Bitboard FileHBB = FileABB << 7; |
| 53 | 53 | ||
| 54 |
|
54 | constexpr Bitboard Rank1BB = 0xFF; |
| 55 |
|
55 | constexpr Bitboard Rank2BB = Rank1BB << (8 * 1); |
| 56 |
|
56 | constexpr Bitboard Rank3BB = Rank1BB << (8 * 2); |
| 57 |
|
57 | constexpr Bitboard Rank4BB = Rank1BB << (8 * 3); |
| 58 |
|
58 | constexpr Bitboard Rank5BB = Rank1BB << (8 * 4); |
| 59 |
|
59 | constexpr Bitboard Rank6BB = Rank1BB << (8 * 5); |
| 60 |
|
60 | constexpr Bitboard Rank7BB = Rank1BB << (8 * 6); |
| 61 |
|
61 | constexpr Bitboard Rank8BB = Rank1BB << (8 * 7); |
| 62 | 62 | ||
| 63 | extern int SquareDistance[SQUARE_NB][SQUARE_NB]; |
63 | extern int SquareDistance[SQUARE_NB][SQUARE_NB]; |
| 64 | 64 | ||
| 65 | extern Bitboard SquareBB[SQUARE_NB]; |
65 | extern Bitboard SquareBB[SQUARE_NB]; |
| 66 | extern Bitboard FileBB[FILE_NB]; |
66 | extern Bitboard FileBB[FILE_NB]; |
| Line 105... | Line 105... | ||
| 105 | 105 | ||
| 106 | /// Overloads of bitwise operators between a Bitboard and a Square for testing |
106 | /// Overloads of bitwise operators between a Bitboard and a Square for testing |
| 107 | /// whether a given bit is set in a bitboard, and for setting and clearing bits. |
107 | /// whether a given bit is set in a bitboard, and for setting and clearing bits. |
| 108 | 108 | ||
| 109 | inline Bitboard operator&(Bitboard b, Square s) { |
109 | inline Bitboard operator&(Bitboard b, Square s) { |
| - | 110 | assert(s >= SQ_A1 && s <= SQ_H8); |
|
| 110 | return b & SquareBB[s]; |
111 | return b & SquareBB[s]; |
| 111 | } |
112 | } |
| 112 | 113 | ||
| 113 | inline Bitboard operator|(Bitboard b, Square s) { |
114 | inline Bitboard operator|(Bitboard b, Square s) { |
| - | 115 | assert(s >= SQ_A1 && s <= SQ_H8); |
|
| 114 | return b | SquareBB[s]; |
116 | return b | SquareBB[s]; |
| 115 | } |
117 | } |
| 116 | 118 | ||
| 117 | inline Bitboard operator^(Bitboard b, Square s) { |
119 | inline Bitboard operator^(Bitboard b, Square s) { |
| - | 120 | assert(s >= SQ_A1 && s <= SQ_H8); |
|
| 118 | return b ^ SquareBB[s]; |
121 | return b ^ SquareBB[s]; |
| 119 | } |
122 | } |
| 120 | 123 | ||
| 121 | inline Bitboard& operator|=(Bitboard& b, Square s) { |
124 | inline Bitboard& operator|=(Bitboard& b, Square s) { |
| - | 125 | assert(s >= SQ_A1 && s <= SQ_H8); |
|
| 122 | return b |= SquareBB[s]; |
126 | return b |= SquareBB[s]; |
| 123 | } |
127 | } |
| 124 | 128 | ||
| 125 | inline Bitboard& operator^=(Bitboard& b, Square s) { |
129 | inline Bitboard& operator^=(Bitboard& b, Square s) { |
| - | 130 | assert(s >= SQ_A1 && s <= SQ_H8); |
|
| 126 | return b ^= SquareBB[s]; |
131 | return b ^= SquareBB[s]; |
| 127 | } |
132 | } |
| 128 | 133 | ||
| 129 | constexpr bool more_than_one(Bitboard b) { |
134 | constexpr bool more_than_one(Bitboard b) { |
| 130 | return b & (b - 1); |
135 | return b & (b - 1); |
| Line 148... | Line 153... | ||
| 148 | inline Bitboard file_bb(Square s) { |
153 | inline Bitboard file_bb(Square s) { |
| 149 | return FileBB[file_of(s)]; |
154 | return FileBB[file_of(s)]; |
| 150 | } |
155 | } |
| 151 | 156 | ||
| 152 | 157 | ||
| 153 | /// shift() moves a bitboard one step along direction D |
158 | /// shift() moves a bitboard one step along direction D (mainly for pawns) |
| 154 | 159 | ||
| 155 | template<Direction D> |
160 | template<Direction D> |
| 156 | constexpr Bitboard shift(Bitboard b) { |
161 | constexpr Bitboard shift(Bitboard b) { |
| 157 | return D == NORTH ? b << 8 : D == SOUTH ? b >> 8 |
162 | return D == NORTH ? b << 8 : D == SOUTH ? b >> 8 |
| - | 163 | : D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1 |
|
| 158 | : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == |
164 | : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7 |
| 159 | : D == |
165 | : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9 |
| 160 | : 0; |
166 | : 0; |
| - | 167 | } |
|
| - | 168 | ||
| - | 169 | ||
| - | 170 | /// pawn_attacks_bb() returns the pawn attacks for the given color from the |
|
| - | 171 | /// squares in the given bitboard. |
|
| - | 172 | ||
| - | 173 | template<Color C> |
|
| - | 174 | constexpr Bitboard pawn_attacks_bb(Bitboard b) { |
|
| - | 175 | return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b) |
|
| - | 176 | : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b); |
|
| 161 | } |
177 | } |
| 162 | 178 | ||
| 163 | 179 | ||
| 164 | /// adjacent_files_bb() returns a bitboard representing all the squares on the |
180 | /// adjacent_files_bb() returns a bitboard representing all the squares on the |
| 165 | /// adjacent files of the given one. |
181 | /// adjacent files of the given one. |
| Line 177... | Line 193... | ||
| 177 | inline Bitboard between_bb(Square s1, Square s2) { |
193 | inline Bitboard between_bb(Square s1, Square s2) { |
| 178 | return BetweenBB[s1][s2]; |
194 | return BetweenBB[s1][s2]; |
| 179 | } |
195 | } |
| 180 | 196 | ||
| 181 | 197 | ||
| 182 | /// forward_ranks_bb() returns a bitboard representing |
198 | /// forward_ranks_bb() returns a bitboard representing the squares on all the ranks |
| 183 | /// in front of the given one, from the point of view of the given color. For |
199 | /// in front of the given one, from the point of view of the given color. For instance, |
| 184 | /// |
200 | /// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2. |
| 185 | 201 | ||
| 186 | inline Bitboard forward_ranks_bb(Color c, Square s) { |
202 | inline Bitboard forward_ranks_bb(Color c, Square s) { |
| 187 | return ForwardRanksBB[c][rank_of(s)]; |
203 | return ForwardRanksBB[c][rank_of(s)]; |
| 188 | } |
204 | } |
| 189 | 205 | ||
| Line 281... | Line 297... | ||
| 281 | } |
297 | } |
| 282 | 298 | ||
| 283 | 299 | ||
| 284 | /// lsb() and msb() return the least/most significant bit in a non-zero bitboard |
300 | /// lsb() and msb() return the least/most significant bit in a non-zero bitboard |
| 285 | 301 | ||
| 286 | #if defined(__GNUC__) |
302 | #if defined(__GNUC__) // GCC, Clang, ICC |
| 287 | 303 | ||
| 288 | inline Square lsb(Bitboard b) { |
304 | inline Square lsb(Bitboard b) { |
| 289 | assert(b); |
305 | assert(b); |
| 290 | return Square(__builtin_ctzll(b)); |
306 | return Square(__builtin_ctzll(b)); |
| 291 | } |
307 | } |
| Line 293... | Line 309... | ||
| 293 | inline Square msb(Bitboard b) { |
309 | inline Square msb(Bitboard b) { |
| 294 | assert(b); |
310 | assert(b); |
| 295 | return Square(63 ^ __builtin_clzll(b)); |
311 | return Square(63 ^ __builtin_clzll(b)); |
| 296 | } |
312 | } |
| 297 | 313 | ||
| 298 | #elif defined( |
314 | #elif defined(_MSC_VER) // MSVC |
| - | 315 | ||
| - | 316 | #ifdef _WIN64 // MSVC, WIN64 |
|
| 299 | 317 | ||
| 300 | inline Square lsb(Bitboard b) { |
318 | inline Square lsb(Bitboard b) { |
| 301 | assert(b); |
319 | assert(b); |
| 302 | unsigned long idx; |
320 | unsigned long idx; |
| 303 | _BitScanForward64(&idx, b); |
321 | _BitScanForward64(&idx, b); |
| Line 309... | Line 327... | ||
| 309 | unsigned long idx; |
327 | unsigned long idx; |
| 310 | _BitScanReverse64(&idx, b); |
328 | _BitScanReverse64(&idx, b); |
| 311 | return (Square) idx; |
329 | return (Square) idx; |
| 312 | } |
330 | } |
| 313 | 331 | ||
| - | 332 | #else // MSVC, WIN32 |
|
| - | 333 | ||
| - | 334 | inline Square lsb(Bitboard b) { |
|
| - | 335 | assert(b); |
|
| - | 336 | unsigned long idx; |
|
| - | 337 | ||
| - | 338 | if (b & 0xffffffff) { |
|
| - | 339 | _BitScanForward(&idx, int32_t(b)); |
|
| - | 340 | return Square(idx); |
|
| - | 341 | } else { |
|
| - | 342 | _BitScanForward(&idx, int32_t(b >> 32)); |
|
| - | 343 | return Square(idx + 32); |
|
| - | 344 | } |
|
| - | 345 | } |
|
| - | 346 | ||
| - | 347 | inline Square msb(Bitboard b) { |
|
| - | 348 | assert(b); |
|
| - | 349 | unsigned long idx; |
|
| - | 350 | ||
| - | 351 | if (b >> 32) { |
|
| - | 352 | _BitScanReverse(&idx, int32_t(b >> 32)); |
|
| - | 353 | return Square(idx + 32); |
|
| - | 354 | } else { |
|
| - | 355 | _BitScanReverse(&idx, int32_t(b)); |
|
| - | 356 | return Square(idx); |
|
| - | 357 | } |
|
| - | 358 | } |
|
| - | 359 | ||
| 314 | |
360 | #endif |
| 315 | 361 | ||
| 316 | |
362 | #else // Compiler is neither GCC nor MSVC compatible |
| 317 | 363 | ||
| 318 | Square lsb(Bitboard b); |
- | |
| 319 |
|
364 | #error "Compiler not supported." |
| 320 | 365 | ||
| 321 | #endif |
366 | #endif |
| 322 | 367 | ||
| 323 | 368 | ||
| 324 | /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard |
369 | /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard |