X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitboard.h;h=46f8fbd46a104ad173d80ec73db4608f1f331b73;hp=3429c42ea02a0146fe05c767634ef346d7a96396;hb=af3dd21e9053feaf66ad884fae0a014d85c700f2;hpb=063e2441b17b8260de443b3d580f49b3d65d03c7 diff --git a/src/bitboard.h b/src/bitboard.h index 3429c42e..46f8fbd4 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -22,20 +22,6 @@ #if !defined(BITBOARD_H_INCLUDED) #define BITBOARD_H_INCLUDED -//// -//// Defines -//// - -// Quiet a warning on Intel compiler -#if !defined(__SIZEOF_INT__ ) -#define __SIZEOF_INT__ 0 -#endif - -// Check for 64 bits for different compilers: Intel, MSVC and gcc -#if defined(__x86_64) || defined(_WIN64) || (__SIZEOF_INT__ > 4) -#define IS_64BIT -#endif - //// //// Includes //// @@ -46,13 +32,6 @@ #include "types.h" -//// -//// Types -//// - -typedef uint64_t Bitboard; - - //// //// Constants and variables //// @@ -61,7 +40,6 @@ const Bitboard EmptyBoardBB = 0ULL; const Bitboard WhiteSquaresBB = 0x55AA55AA55AA55AAULL; const Bitboard BlackSquaresBB = 0xAA55AA55AA55AA55ULL; -const Bitboard SquaresByColorBB[2] = { BlackSquaresBB, WhiteSquaresBB }; const Bitboard FileABB = 0x0101010101010101ULL; const Bitboard FileBBB = 0x0202020202020202ULL; @@ -72,22 +50,6 @@ const Bitboard FileFBB = 0x2020202020202020ULL; const Bitboard FileGBB = 0x4040404040404040ULL; const Bitboard FileHBB = 0x8080808080808080ULL; -const Bitboard FileBB[8] = { - FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB -}; - -const Bitboard NeighboringFilesBB[8] = { - FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB, - FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB -}; - -const Bitboard ThisAndNeighboringFilesBB[8] = { - FileABB|FileBBB, FileABB|FileBBB|FileCBB, - FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB, - FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB, - FileFBB|FileGBB|FileHBB, FileGBB|FileHBB -}; - const Bitboard Rank1BB = 0xFFULL; const Bitboard Rank2BB = 0xFF00ULL; const Bitboard Rank3BB = 0xFF0000ULL; @@ -97,42 +59,13 @@ const Bitboard Rank6BB = 0xFF0000000000ULL; const Bitboard Rank7BB = 0xFF000000000000ULL; const Bitboard Rank8BB = 0xFF00000000000000ULL; -const Bitboard RankBB[8] = { - Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB -}; - -const Bitboard RelativeRankBB[2][8] = { - { Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB }, - { Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB } -}; - -const Bitboard InFrontBB[2][8] = { - { Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB, - Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB, - Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB, - Rank5BB | Rank6BB | Rank7BB | Rank8BB, - Rank6BB | Rank7BB | Rank8BB, - Rank7BB | Rank8BB, - Rank8BB, - EmptyBoardBB - }, - { EmptyBoardBB, - Rank1BB, - Rank2BB | Rank1BB, - Rank3BB | Rank2BB | Rank1BB, - Rank4BB | Rank3BB | Rank2BB | Rank1BB, - Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB, - Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB, - Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB - } -}; - -const int BitTable[64] = { - 63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2, - 51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52, - 26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28, - 58, 20, 37, 17, 36, 8 -}; +extern const Bitboard SquaresByColorBB[2]; +extern const Bitboard FileBB[8]; +extern const Bitboard NeighboringFilesBB[8]; +extern const Bitboard ThisAndNeighboringFilesBB[8]; +extern const Bitboard RankBB[8]; +extern const Bitboard RelativeRankBB[2][8]; +extern const Bitboard InFrontBB[2][8]; extern Bitboard SetMaskBB[65]; extern Bitboard ClearMaskBB[65]; @@ -160,6 +93,8 @@ extern Bitboard BishopPseudoAttacks[64]; extern Bitboard RookPseudoAttacks[64]; extern Bitboard QueenPseudoAttacks[64]; +extern uint8_t BitCount8Bit[256]; + //// //// Inline functions @@ -383,21 +318,28 @@ inline Bitboard isolated_pawn_mask(Square s) { /// first_1() finds the least significant nonzero bit in a nonzero bitboard. +/// pop_1st_bit() finds and clears the least significant nonzero bit in a +/// nonzero bitboard. -#if defined(IS_64BIT) +#if defined(USE_BSFQ) // Assembly code by Heinz van Saanen inline Square first_1(Bitboard b) { - return Square(BitTable[((b & -b) * 0x218a392cd3d5dbfULL) >> 58]); + Bitboard dummy; + __asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) ); + return (Square)(dummy); } -#else - -inline Square first_1(Bitboard b) { - b ^= (b - 1); - uint32_t fold = int(b) ^ int(b >> 32); - return Square(BitTable[(fold * 0x783a9b23) >> 26]); +inline Square pop_1st_bit(Bitboard* b) { + const Square s = first_1(*b); + *b &= ~(1ULL<