Rev 96 | Rev 169 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 96 | pmbaty | 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-2016 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 StepAttacksBB[PIECE_NB][SQUARE_NB]; | ||
| 70 | extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB]; | ||
| 71 | extern Bitboard LineBB[SQUARE_NB][SQUARE_NB]; | ||
| 72 | extern Bitboard DistanceRingBB[SQUARE_NB][8]; | ||
| 73 | extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB]; | ||
| 74 | extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB]; | ||
| 75 | extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB]; | ||
| 76 | extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB]; | ||
| 77 | |||
| 78 | |||
| 79 | /// Overloads of bitwise operators between a Bitboard and a Square for testing | ||
| 80 | /// whether a given bit is set in a bitboard, and for setting and clearing bits. | ||
| 81 | |||
| 82 | inline Bitboard operator&(Bitboard b, Square s) { | ||
| 83 | return b & SquareBB[s]; | ||
| 84 | } | ||
| 85 | |||
| 86 | inline Bitboard operator|(Bitboard b, Square s) { | ||
| 87 | return b | SquareBB[s]; | ||
| 88 | } | ||
| 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 bool more_than_one(Bitboard b) { | ||
| 103 | return b & (b - 1); | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | /// rank_bb() and file_bb() return a bitboard representing all the squares on | ||
| 108 | /// the given file or rank. | ||
| 109 | |||
| 110 | inline Bitboard rank_bb(Rank r) { | ||
| 111 | return RankBB[r]; | ||
| 112 | } | ||
| 113 | |||
| 114 | inline Bitboard rank_bb(Square s) { | ||
| 115 | return RankBB[rank_of(s)]; | ||
| 116 | } | ||
| 117 | |||
| 118 | inline Bitboard file_bb(File f) { | ||
| 119 | return FileBB[f]; | ||
| 120 | } | ||
| 121 | |||
| 122 | inline Bitboard file_bb(Square s) { | ||
| 123 | return FileBB[file_of(s)]; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 154 | pmbaty | 127 | /// shift() moves a bitboard one step along direction D. Mainly for pawns | 
| 96 | pmbaty | 128 | |
| 154 | pmbaty | 129 | template<Square D> | 
| 130 | inline Bitboard shift(Bitboard b) { | ||
| 131 | return D == NORTH ? b << 8 : D == SOUTH ? b >> 8 | ||
| 132 | : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 | ||
| 133 | : D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9 | ||
| 96 | pmbaty | 134 | : 0; | 
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | /// adjacent_files_bb() returns a bitboard representing all the squares on the | ||
| 139 | /// adjacent files of the given one. | ||
| 140 | |||
| 141 | inline Bitboard adjacent_files_bb(File f) { | ||
| 142 | return AdjacentFilesBB[f]; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | /// between_bb() returns a bitboard representing all the squares between the two | ||
| 147 | /// given ones. For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with | ||
| 148 | /// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank, file | ||
| 149 | /// or diagonal, 0 is returned. | ||
| 150 | |||
| 151 | inline Bitboard between_bb(Square s1, Square s2) { | ||
| 152 | return BetweenBB[s1][s2]; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | /// in_front_bb() returns a bitboard representing all the squares on all the ranks | ||
| 157 | /// in front of the given one, from the point of view of the given color. For | ||
| 158 | /// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2. | ||
| 159 | |||
| 160 | inline Bitboard in_front_bb(Color c, Rank r) { | ||
| 161 | return InFrontBB[c][r]; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | /// forward_bb() returns a bitboard representing all the squares along the line | ||
| 166 | /// in front of the given one, from the point of view of the given color: | ||
| 167 | ///        ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s) | ||
| 168 | |||
| 169 | inline Bitboard forward_bb(Color c, Square s) { | ||
| 170 | return ForwardBB[c][s]; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | /// pawn_attack_span() returns a bitboard representing all the squares that can be | ||
| 175 | /// attacked by a pawn of the given color when it moves along its file, starting | ||
| 176 | /// from the given square: | ||
| 177 | ///       PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s); | ||
| 178 | |||
| 179 | inline Bitboard pawn_attack_span(Color c, Square s) { | ||
| 180 | return PawnAttackSpan[c][s]; | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | /// passed_pawn_mask() returns a bitboard mask which can be used to test if a | ||
| 185 | /// pawn of the given color and on the given square is a passed pawn: | ||
| 186 | ///       PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s) | ||
| 187 | |||
| 188 | inline Bitboard passed_pawn_mask(Color c, Square s) { | ||
| 189 | return PassedPawnMask[c][s]; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | /// aligned() returns true if the squares s1, s2 and s3 are aligned either on a | ||
| 194 | /// straight or on a diagonal line. | ||
| 195 | |||
| 196 | inline bool aligned(Square s1, Square s2, Square s3) { | ||
| 197 | return LineBB[s1][s2] & s3; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | /// distance() functions return the distance between x and y, defined as the | ||
| 202 | /// number of steps for a king in x to reach y. Works with squares, ranks, files. | ||
| 203 | |||
| 204 | template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; } | ||
| 205 | template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; } | ||
| 206 | |||
| 207 | template<typename T1, typename T2> inline int distance(T2 x, T2 y); | ||
| 208 | template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); } | ||
| 209 | template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); } | ||
| 210 | |||
| 211 | |||
| 212 | /// attacks_bb() returns a bitboard representing all the squares attacked by a | ||
| 213 | /// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index() | ||
| 214 | /// looks up the index using the 'magic bitboards' approach. | ||
| 215 | template<PieceType Pt> | ||
| 216 | inline unsigned magic_index(Square s, Bitboard occupied) { | ||
| 217 | |||
| 154 | pmbaty | 218 | extern Bitboard RookMasks[SQUARE_NB]; | 
| 219 | extern Bitboard RookMagics[SQUARE_NB]; | ||
| 220 | extern unsigned RookShifts[SQUARE_NB]; | ||
| 221 | extern Bitboard BishopMasks[SQUARE_NB]; | ||
| 222 | extern Bitboard BishopMagics[SQUARE_NB]; | ||
| 223 | extern unsigned BishopShifts[SQUARE_NB]; | ||
| 224 | |||
| 96 | pmbaty | 225 | Bitboard* const Masks = Pt == ROOK ? RookMasks : BishopMasks; | 
| 226 | Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics; | ||
| 227 | unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts; | ||
| 228 | |||
| 229 | if (HasPext) | ||
| 230 | return unsigned(pext(occupied, Masks[s])); | ||
| 231 | |||
| 232 | if (Is64Bit) | ||
| 233 | return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]); | ||
| 234 | |||
| 235 | unsigned lo = unsigned(occupied) & unsigned(Masks[s]); | ||
| 236 | unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32); | ||
| 237 | return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s]; | ||
| 238 | } | ||
| 239 | |||
| 240 | template<PieceType Pt> | ||
| 241 | inline Bitboard attacks_bb(Square s, Bitboard occupied) { | ||
| 154 | pmbaty | 242 | |
| 243 | extern Bitboard* RookAttacks[SQUARE_NB]; | ||
| 244 | extern Bitboard* BishopAttacks[SQUARE_NB]; | ||
| 245 | |||
| 96 | pmbaty | 246 | return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)]; | 
| 247 | } | ||
| 248 | |||
| 249 | inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) { | ||
| 250 | |||
| 251 | switch (type_of(pc)) | ||
| 252 |   { | ||
| 253 | case BISHOP: return attacks_bb<BISHOP>(s, occupied); | ||
| 254 | case ROOK : return attacks_bb<ROOK>(s, occupied); | ||
| 255 | case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied); | ||
| 256 | default : return StepAttacksBB[pc][s]; | ||
| 257 |   } | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 154 | pmbaty | 261 | /// popcount() counts the number of non-zero bits in a bitboard | 
| 96 | pmbaty | 262 | |
| 154 | pmbaty | 263 | inline int popcount(Bitboard b) { | 
| 96 | pmbaty | 264 | |
| 154 | pmbaty | 265 | #ifndef USE_POPCNT | 
| 96 | pmbaty | 266 | |
| 154 | pmbaty | 267 | extern uint8_t PopCnt16[1 << 16]; | 
| 268 | union { Bitboard bb; uint16_t u[4]; } v = { b }; | ||
| 269 | return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]]; | ||
| 96 | pmbaty | 270 | |
| 154 | pmbaty | 271 | #elif defined(_MSC_VER) || defined(__INTEL_COMPILER) | 
| 272 | |||
| 273 | return (int)_mm_popcnt_u64(b); | ||
| 274 | |||
| 275 | #else // Assumed gcc or compatible compiler | ||
| 276 | |||
| 277 | return __builtin_popcountll(b); | ||
| 278 | |||
| 279 | #endif | ||
| 96 | pmbaty | 280 | } | 
| 281 | |||
| 282 | |||
| 154 | pmbaty | 283 | /// lsb() and msb() return the least/most significant bit in a non-zero bitboard | 
| 284 | |||
| 285 | #if defined(__GNUC__) | ||
| 286 | |||
| 287 | inline Square lsb(Bitboard b) { | ||
| 288 | assert(b); | ||
| 289 | return Square(__builtin_ctzll(b)); | ||
| 96 | pmbaty | 290 | } | 
| 291 | |||
| 292 | inline Square msb(Bitboard b) { | ||
| 154 | pmbaty | 293 | assert(b); | 
| 294 | return Square(63 - __builtin_clzll(b)); | ||
| 96 | pmbaty | 295 | } | 
| 296 | |||
| 154 | pmbaty | 297 | #elif defined(_WIN64) && defined(_MSC_VER) | 
| 298 | |||
| 96 | pmbaty | 299 | inline Square lsb(Bitboard b) { | 
| 154 | pmbaty | 300 | assert(b); | 
| 301 | unsigned long idx; | ||
| 302 | _BitScanForward64(&idx, b); | ||
| 96 | pmbaty | 303 | return (Square) idx; | 
| 304 | } | ||
| 305 | |||
| 306 | inline Square msb(Bitboard b) { | ||
| 154 | pmbaty | 307 | assert(b); | 
| 308 | unsigned long idx; | ||
| 309 | _BitScanReverse64(&idx, b); | ||
| 96 | pmbaty | 310 | return (Square) idx; | 
| 311 | } | ||
| 312 | |||
| 154 | pmbaty | 313 | #else | 
| 96 | pmbaty | 314 | |
| 154 | pmbaty | 315 | #define NO_BSF // Fallback on software implementation for other cases | 
| 96 | pmbaty | 316 | |
| 317 | Square lsb(Bitboard b); | ||
| 318 | Square msb(Bitboard b); | ||
| 319 | |||
| 320 | #endif | ||
| 321 | |||
| 322 | |||
| 323 | /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard | ||
| 324 | |||
| 325 | inline Square pop_lsb(Bitboard* b) { | ||
| 326 | const Square s = lsb(*b); | ||
| 327 | *b &= *b - 1; | ||
| 328 | return s; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | /// frontmost_sq() and backmost_sq() return the square corresponding to the | ||
| 333 | /// most/least advanced bit relative to the given color. | ||
| 334 | |||
| 335 | inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); } | ||
| 336 | inline Square backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); } | ||
| 337 | |||
| 338 | #endif // #ifndef BITBOARD_H_INCLUDED |