]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Partially revert previous patches
[stockfish] / src / bitboard.cpp
index b1bf4d91e583b3f07f1ac53cb669dce2a4522eb0..88682948c3f7d724ce9dd05e6b313b74955f48a5 100644 (file)
 
 // Global bitboards definitions with static storage duration are
 // automatically set to zero before enter main().
-Bitboard RAttacks[0x19000];
-Bitboard BAttacks[0x1480];
+Bitboard RMask[64];
+Bitboard* RAttacks[64];
+int RShift[64];
 
-Magics RMagics[64];
-Magics BMagics[64];
+Bitboard BMask[64];
+Bitboard* BAttacks[64];
+int BShift[64];
 
 Bitboard SetMaskBB[65];
 Bitboard ClearMaskBB[65];
@@ -51,11 +53,9 @@ Bitboard QueenPseudoAttacks[64];
 
 uint8_t BitCount8Bit[256];
 
-namespace {
-
 #if defined(IS_64BIT)
 
-const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
+static const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
 
 const uint64_t BMult[64] = {
   0x0440049104032280ULL, 0x1021023C82008040ULL, 0x0404040082000048ULL,
@@ -107,23 +107,9 @@ const uint64_t RMult[64] = {
   0x410201CE5C030092ULL
 };
 
-const int BShift[64] = {
-  58, 59, 59, 59, 59, 59, 59, 58, 59, 59, 59, 59, 59, 59, 59, 59,
-  59, 59, 57, 57, 57, 57, 59, 59, 59, 59, 57, 55, 55, 57, 59, 59,
-  59, 59, 57, 55, 55, 57, 59, 59, 59, 59, 57, 57, 57, 57, 59, 59,
-  59, 59, 59, 59, 59, 59, 59, 59, 58, 59, 59, 59, 59, 59, 59, 58
-};
-
-const int RShift[64] = {
-  52, 53, 53, 53, 53, 53, 53, 52, 53, 54, 54, 54, 54, 54, 54, 53,
-  53, 54, 54, 54, 54, 54, 54, 53, 53, 54, 54, 54, 54, 54, 54, 53,
-  53, 54, 54, 54, 54, 54, 54, 53, 53, 54, 54, 54, 54, 54, 54, 53,
-  53, 54, 54, 54, 54, 54, 54, 53, 52, 53, 53, 53, 53, 53, 53, 52
-};
-
 #else // if !defined(IS_64BIT)
 
-const uint32_t DeBruijnMagic = 0x783A9B23;
+static const uint32_t DeBruijnMagic = 0x783A9B23;
 
 const uint64_t BMult[64] = {
   0x54142844C6A22981ULL, 0x710358A6EA25C19EULL, 0x704F746D63A4A8DCULL,
@@ -175,26 +161,19 @@ const uint64_t RMult[64] = {
   0x5AC08A4735370479ULL,
 };
 
-const int BShift[64] = {
-  26, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 27, 27,
-  27, 27, 25, 25, 25, 25, 27, 27, 27, 27, 25, 23, 23, 25, 27, 27,
-  27, 27, 25, 23, 23, 25, 27, 27, 27, 27, 25, 25, 25, 25, 27, 27,
-  27, 27, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 26
-};
+#endif // defined(IS_64BIT)
 
-const int RShift[64] = {
-  20, 21, 21, 21, 21, 21, 21, 20, 21, 22, 22, 22, 22, 22, 22, 21,
-  21, 22, 22, 22, 22, 22, 22, 21, 21, 22, 22, 22, 22, 22, 22, 21,
-  21, 22, 22, 22, 22, 22, 22, 21, 21, 22, 22, 22, 22, 22, 22, 21,
-  21, 22, 22, 22, 22, 22, 22, 21, 20, 21, 21, 21, 21, 21, 21, 20
-};
 
-#endif // defined(IS_64BIT)
+namespace {
 
-  CACHE_LINE_ALIGNMENT int BSFTable[64];
+  CACHE_LINE_ALIGNMENT
 
-  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
-                            const Bitboard mult[], Square deltas[]);
+  int BSFTable[64];
+  Bitboard RAttacksTable[0x19000];
+  Bitboard BAttacksTable[0x1480];
+
+  void init_sliding_attacks(Bitboard attacksTable[], Bitboard* attacks[], Bitboard mask[],
+                            int shift[], const Bitboard mult[], Square deltas[]);
 }
 
 
@@ -354,8 +333,8 @@ void init_bitboards() {
   Square RDeltas[] = { DELTA_N,  DELTA_E,  DELTA_S,  DELTA_W  };
   Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
 
-  init_sliding_attacks(RAttacks, RMagics, RShift, RMult, RDeltas);
-  init_sliding_attacks(BAttacks, BMagics, BShift, BMult, BDeltas);
+  init_sliding_attacks(RAttacksTable, RAttacks, RMask, RShift, RMult, RDeltas);
+  init_sliding_attacks(BAttacksTable, BAttacks, BMask, BShift, BMult, BDeltas);
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
   {
@@ -394,7 +373,7 @@ namespace {
     return subMask;
   }
 
-  Bitboard sliding_attacks(Square sq, Bitboard occupied, Square deltas[], bool skipLast) {
+  Bitboard sliding_attacks(Square sq, Bitboard occupied, Square deltas[], Bitboard excluded) {
 
     Bitboard attacks = 0;
 
@@ -404,12 +383,11 @@ namespace {
 
         while (    square_is_ok(s)
                &&  square_distance(s, s - deltas[i]) == 1
-               && (!skipLast || !file_distance(s, sq) || (square_file(s) != FILE_A && square_file(s) != FILE_H))
-               && (!skipLast || !rank_distance(s, sq) || (square_rank(s) != RANK_1 && square_rank(s) != RANK_8)))
+               && !bit_is_set(excluded, s))
         {
-            attacks |= 1ULL << s;
+            set_bit(&attacks, s);
 
-            if (occupied & (1ULL << s))
+            if (bit_is_set(occupied, s))
                 break;
 
             s += deltas[i];
@@ -418,28 +396,30 @@ namespace {
     return attacks;
   }
 
-  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
-                            const Bitboard mult[], Square deltas[]) {
-    Bitboard occupancy, index;
+  void init_sliding_attacks(Bitboard attacksTable[], Bitboard* attacks[], Bitboard mask[],
+                            int shift[], const Bitboard mult[], Square deltas[]) {
+
+    Bitboard occupancy, index, excluded;
     int maxKey, offset = 0;
 
     for (Square s = SQ_A1; s <= SQ_H8; s++)
     {
-        m[s].offset = offset;
-        m[s].mult   = mult[s];
-        m[s].shift  = shift[s];
-        m[s].mask   = sliding_attacks(s, EmptyBoardBB, deltas, true);
+        excluded = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
+
+        attacks[s] = &attacksTable[offset];
+        mask[s]    = sliding_attacks(s, EmptyBoardBB, deltas, excluded);
+        shift[s]   = (CpuIs64Bit ? 64 : 32) - count_1s<CNT64>(mask[s]);
 
-        maxKey = 1 << (CpuIs64Bit ? 64 - shift[s] : 32 - shift[s]);
+        maxKey = 1 << count_1s<CNT64>(mask[s]);
 
         for (int key = 0; key < maxKey; key++)
         {
-            occupancy = submask(m[s].mask, key);
+            occupancy = submask(mask[s], key);
 
             index = CpuIs64Bit ? occupancy * mult[s]
                                : unsigned(occupancy * mult[s] ^ (occupancy >> 32) * (mult[s] >> 32));
 
-            attacks[offset + (index >> shift[s])] = sliding_attacks(s, occupancy, deltas, false);
+            attacks[s][index >> shift[s]] = sliding_attacks(s, occupancy, deltas, EmptyBoardBB);
         }
         offset += maxKey;
     }