]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.h
Skip offset calculation in slider attacks
[stockfish] / src / bitboard.h
index b962226bfb9e5048f950a2dd954622e48db28f7d..00f0f576d91972762e7f2781a585c3600f371e24 100644 (file)
@@ -43,41 +43,39 @@ const Bitboard Rank6BB = Rank1BB << (8 * 5);
 const Bitboard Rank7BB = Rank1BB << (8 * 6);
 const Bitboard Rank8BB = Rank1BB << (8 * 7);
 
-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 InFrontBB[2][8];
+extern Bitboard SquaresByColorBB[2];
+extern Bitboard FileBB[8];
+extern Bitboard NeighboringFilesBB[8];
+extern Bitboard ThisAndNeighboringFilesBB[8];
+extern Bitboard RankBB[8];
+extern Bitboard InFrontBB[2][8];
 
 extern Bitboard SetMaskBB[65];
 extern Bitboard ClearMaskBB[65];
 
-extern Bitboard NonSlidingAttacksBB[16][64];
+extern Bitboard StepAttacksBB[16][64];
 extern Bitboard BetweenBB[64][64];
 
 extern Bitboard SquaresInFrontMask[2][64];
 extern Bitboard PassedPawnMask[2][64];
 extern Bitboard AttackSpanMask[2][64];
 
-extern const uint64_t RMult[64];
-extern const int RShift[64];
-extern Bitboard RMask[64];
-extern int RAttackIndex[64];
-extern Bitboard RAttacks[0x19000];
-
-extern const uint64_t BMult[64];
-extern const int BShift[64];
-extern Bitboard BMask[64];
-extern int BAttackIndex[64];
-extern Bitboard BAttacks[0x1480];
-
 extern Bitboard BishopPseudoAttacks[64];
 extern Bitboard RookPseudoAttacks[64];
 extern Bitboard QueenPseudoAttacks[64];
 
 extern uint8_t BitCount8Bit[256];
 
+struct Magics {
+  Bitboard mask;
+  uint64_t mult;
+  uint32_t shift;
+  Bitboard* attacks;
+};
+
+extern Magics RMagics[64];
+extern Magics BMagics[64];
+
 
 /// Functions for testing whether a given bit is set in a bitboard, and for
 /// setting and clearing bits.
@@ -173,28 +171,28 @@ inline Bitboard in_front_bb(Color c, Square s) {
 
 #if defined(IS_64BIT)
 
-inline Bitboard rook_attacks_bb(Square s, Bitboard blockers) {
-  Bitboard b = blockers & RMask[s];
-  return RAttacks[RAttackIndex[s] + ((b * RMult[s]) >> RShift[s])];
+inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
+  const Magics& m = RMagics[s];
+  return m.attacks[((occ & m.mask) * m.mult) >> m.shift];
 }
 
-inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) {
-  Bitboard b = blockers & BMask[s];
-  return BAttacks[BAttackIndex[s] + ((b * BMult[s]) >> BShift[s])];
+inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
+  const Magics& m = BMagics[s];
+  return m.attacks[((occ & m.mask) * m.mult) >> m.shift];
 }
 
 #else // if !defined(IS_64BIT)
 
-inline Bitboard rook_attacks_bb(Square s, Bitboard blockers) {
-  Bitboard b = blockers & RMask[s];
-  return RAttacks[RAttackIndex[s] +
-        (unsigned(int(b) * int(RMult[s]) ^ int(b >> 32) * int(RMult[s] >> 32)) >> RShift[s])];
+inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
+  const Magics& m = RMagics[s];
+  Bitboard b = occ & m.mask;
+  return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift];
 }
 
-inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) {
-  Bitboard b = blockers & BMask[s];
-  return BAttacks[BAttackIndex[s] +
-        (unsigned(int(b) * int(BMult[s]) ^ int(b >> 32) * int(BMult[s] >> 32)) >> BShift[s])];
+inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
+  const Magics& m = BMagics[s];
+  Bitboard b = occ & m.mask;
+  return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift];
 }
 
 #endif
@@ -257,15 +255,25 @@ inline bool squares_aligned(Square s1, Square s2, Square s3) {
 /// pop_1st_bit() finds and clears the least significant nonzero bit in a
 /// nonzero bitboard.
 
-#if defined(USE_BSFQ) // Assembly code by Heinz van Saanen
+#if defined(USE_BSFQ)
 
-inline Square first_1(Bitboard b) {
+#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+
+FORCE_INLINE Square first_1(Bitboard b) {
+   unsigned long index;
+   _BitScanForward64(&index, b);
+   return (Square) index;
+}
+#else
+
+FORCE_INLINE Square first_1(Bitboard b) { // Assembly code by Heinz van Saanen
   Bitboard dummy;
   __asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) );
-  return (Square)(dummy);
+  return (Square) dummy;
 }
+#endif
 
-inline Square pop_1st_bit(Bitboard* b) {
+FORCE_INLINE Square pop_1st_bit(Bitboard* b) {
   const Square s = first_1(*b);
   *b &= ~(1ULL<<s);
   return s;