]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Skip offset calculation in slider attacks
[stockfish] / src / bitboard.cpp
index db2d9a0593b9b6eaef841b94bac32ef698a47702..c6ee517d4aa8e7b5f7f3dfc0464f814f59f54e39 100644 (file)
@@ -24,9 +24,6 @@
 
 // Global bitboards definitions with static storage duration are
 // automatically set to zero before enter main().
-Bitboard RAttacks[0x19000];
-Bitboard BAttacks[0x1480];
-
 Magics RMagics[64];
 Magics BMagics[64];
 
@@ -53,6 +50,15 @@ uint8_t BitCount8Bit[256];
 
 namespace {
 
+  CACHE_LINE_ALIGNMENT
+
+  int BSFTable[64];
+
+  Bitboard RAttacks[0x19000];
+  Bitboard BAttacks[0x1480];
+
+  void init_sliding_attacks(Bitboard attacks[], Magics m[], const Bitboard mult[], Square deltas[]);
+
 #if defined(IS_64BIT)
 
 const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
@@ -107,20 +113,6 @@ 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;
@@ -175,26 +167,8 @@ 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
-};
-
-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)
 
-  CACHE_LINE_ALIGNMENT int BSFTable[64];
-
-  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
-                            const Bitboard mult[], int deltas[][2]);
 }
 
 
@@ -351,11 +325,11 @@ void init_bitboards() {
                       set_bit(&StepAttacksBB[make_piece(c, pt)][s], to);
               }
 
-  int rookDeltas[4][2]   = { {0,1}, {0 ,-1}, {1, 0}, {-1, 0} };
-  int bishopDeltas[4][2] = { {1,1}, {-1, 1}, {1,-1}, {-1,-1} };
+  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, rookDeltas);
-  init_sliding_attacks(BAttacks, BMagics, BShift, BMult, bishopDeltas);
+  init_sliding_attacks(RAttacks, RMagics, RMult, RDeltas);
+  init_sliding_attacks(BAttacks, BMagics, BMult, BDeltas);
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
   {
@@ -381,70 +355,68 @@ void init_bitboards() {
 
 namespace {
 
-  Bitboard index_to_bitboard(int index, Bitboard mask) {
+  Bitboard submask(Bitboard mask, int key) {
 
-    Bitboard result = 0;
-    int sq, cnt = 0;
+    Bitboard subMask = 0;
+    int bitNum = -1;
 
-    while (mask)
-    {
-        sq = pop_1st_bit(&mask);
+    // Extract an unique submask out of a mask according to the given key
+    for (Square s = SQ_A1; s <= SQ_H8; s++)
+        if (bit_is_set(mask, s) && bit_is_set(key, Square(++bitNum)))
+            set_bit(&subMask, s);
 
-        if (index & (1 << cnt++))
-            result |= (1ULL << sq);
-    }
-    return result;
+    return subMask;
   }
 
-  Bitboard sliding_attacks(int sq, Bitboard occupied, int deltas[][2],
-                           int fmin, int fmax, int rmin, int rmax) {
+  Bitboard sliding_attacks(Square sq, Bitboard occupied, Square deltas[], Bitboard excluded) {
+
     Bitboard attacks = 0;
-    int dx, dy, f, r;
-    int rk = sq / 8;
-    int fl = sq % 8;
 
     for (int i = 0; i < 4; i++)
     {
-        dx = deltas[i][0];
-        dy = deltas[i][1];
-        f = fl + dx;
-        r = rk + dy;
+        Square s = sq + deltas[i];
 
-        while (   (dx == 0 || (f >= fmin && f <= fmax))
-               && (dy == 0 || (r >= rmin && r <= rmax)))
+        while (    square_is_ok(s)
+               &&  square_distance(s, s - deltas[i]) == 1
+               && !bit_is_set(excluded, s))
         {
-            attacks |= (1ULL << (f + r * 8));
+            set_bit(&attacks, s);
 
-            if (occupied & (1ULL << (f + r * 8)))
+            if (bit_is_set(occupied, s))
                 break;
 
-            f += dx;
-            r += dy;
+            s += deltas[i];
         }
     }
     return attacks;
   }
 
-  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
-                            const Bitboard mult[], int deltas[][2]) {
-    Bitboard b, v;
-    int i, j, index;
+  void init_sliding_attacks(Bitboard attacks[], Magics m[], const Bitboard mult[], Square deltas[]) {
 
-    for (i = index = 0; i < 64; i++)
+    Bitboard occupancy, index, excluded;
+    int maxKey, offset = 0;
+
+    for (Square s = SQ_A1; s <= SQ_H8; s++)
     {
-        m[i].index = index;
-        m[i].mult = mult[i];
-        m[i].shift = shift[i];
-        m[i].mask = sliding_attacks(i, 0, deltas, 1, 6, 1, 6);
-        j = 1 << ((CpuIs64Bit ? 64 : 32) - m[i].shift);
+        excluded = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
+
+        m[s].attacks = &attacks[offset];
+        m[s].mult    = mult[s];
+        m[s].mask    = sliding_attacks(s, EmptyBoardBB, deltas, excluded);
+        m[s].shift   = (CpuIs64Bit ? 64 : 32) - count_1s<CNT64>(m[s].mask);
+
+        maxKey = 1 << count_1s<CNT64>(m[s].mask);
 
-        for (int k = 0; k < j; k++)
+        for (int key = 0; key < maxKey; key++)
         {
-            b = index_to_bitboard(k, m[i].mask);
-            v = CpuIs64Bit ? b * mult[i] : unsigned(b * mult[i] ^ (b >> 32) * (mult[i] >> 32));
-            attacks[index + (v >> shift[i])] = sliding_attacks(i, b, deltas, 0, 7, 0, 7);
+            occupancy = submask(m[s].mask, key);
+
+            index = CpuIs64Bit ? occupancy * mult[s]
+                               : unsigned(occupancy * mult[s] ^ (occupancy >> 32) * (mult[s] >> 32));
+
+            m[s].attacks[index >> m[s].shift] = sliding_attacks(s, occupancy, deltas, EmptyBoardBB);
         }
-        index += j;
+        offset += maxKey;
     }
   }
 }