]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.h
Simplify endgame functions handling
[stockfish] / src / bitboard.h
index 3429c42ea02a0146fe05c767634ef346d7a96396..0064fb26d83bf42decc3db7d9eb3102b47e4ce8a 100644 (file)
 #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
 ////
 #include "types.h"
 
 
-////
-//// Types
-////
-
-typedef uint64_t Bitboard;
-
-
 ////
 //// Constants and variables
 ////
@@ -127,13 +106,6 @@ const Bitboard InFrontBB[2][8] = {
   }
 };
 
-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 Bitboard SetMaskBB[65];
 extern Bitboard ClearMaskBB[65];
 
@@ -383,21 +355,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]);
+inline Square __attribute__((always_inline)) first_1(Bitboard b) {
+  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 __attribute__((always_inline)) pop_1st_bit(Bitboard* b) {
+  const Square s = first_1(*b);
+  *b &= ~(1ULL<<s);
+  return s;
 }
 
+#else // if !defined(USE_BSFQ)
+
+extern Square first_1(Bitboard b);
+extern Square pop_1st_bit(Bitboard* b);
+
 #endif
 
 
@@ -407,7 +386,6 @@ inline Square first_1(Bitboard b) {
 
 extern void print_bitboard(Bitboard b);
 extern void init_bitboards();
-extern Square pop_1st_bit(Bitboard *b);
 
 
 #endif // !defined(BITBOARD_H_INCLUDED)