]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Better name and document magic botboard stuff
[stockfish] / src / bitboard.cpp
index fd4cd1ef5dfc5b1d8e22c83b5539ba6b41e8b54f..b1bf4d91e583b3f07f1ac53cb669dce2a4522eb0 100644 (file)
 #include "bitboard.h"
 #include "bitcount.h"
 
 #include "bitboard.h"
 #include "bitcount.h"
 
+// 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];
+
+Bitboard SetMaskBB[65];
+Bitboard ClearMaskBB[65];
+
+Bitboard SquaresByColorBB[2];
+Bitboard FileBB[8];
+Bitboard RankBB[8];
+Bitboard NeighboringFilesBB[8];
+Bitboard ThisAndNeighboringFilesBB[8];
+Bitboard InFrontBB[2][8];
+Bitboard StepAttacksBB[16][64];
+Bitboard BetweenBB[64][64];
+Bitboard SquaresInFrontMask[2][64];
+Bitboard PassedPawnMask[2][64];
+Bitboard AttackSpanMask[2][64];
+
+Bitboard BishopPseudoAttacks[64];
+Bitboard RookPseudoAttacks[64];
+Bitboard QueenPseudoAttacks[64];
+
+uint8_t BitCount8Bit[256];
+
+namespace {
+
 #if defined(IS_64BIT)
 
 #if defined(IS_64BIT)
 
-static const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
+const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
 
 const uint64_t BMult[64] = {
   0x0440049104032280ULL, 0x1021023C82008040ULL, 0x0404040082000048ULL,
 
 const uint64_t BMult[64] = {
   0x0440049104032280ULL, 0x1021023C82008040ULL, 0x0404040082000048ULL,
@@ -92,7 +123,7 @@ const int RShift[64] = {
 
 #else // if !defined(IS_64BIT)
 
 
 #else // if !defined(IS_64BIT)
 
-static const uint32_t DeBruijnMagic = 0x783A9B23;
+const uint32_t DeBruijnMagic = 0x783A9B23;
 
 const uint64_t BMult[64] = {
   0x54142844C6A22981ULL, 0x710358A6EA25C19EULL, 0x704F746D63A4A8DCULL,
 
 const uint64_t BMult[64] = {
   0x54142844C6A22981ULL, 0x710358A6EA25C19EULL, 0x704F746D63A4A8DCULL,
@@ -160,44 +191,10 @@ const int RShift[64] = {
 
 #endif // defined(IS_64BIT)
 
 
 #endif // defined(IS_64BIT)
 
-// Global bitboards definitions with static storage duration are
-// automatically set to zero before enter main().
-Bitboard RMask[64];
-int RAttackIndex[64];
-Bitboard RAttacks[0x19000];
-
-Bitboard BMask[64];
-int BAttackIndex[64];
-Bitboard BAttacks[0x1480];
-
-Bitboard SetMaskBB[65];
-Bitboard ClearMaskBB[65];
-
-Bitboard SquaresByColorBB[2];
-Bitboard FileBB[8];
-Bitboard RankBB[8];
-Bitboard NeighboringFilesBB[8];
-Bitboard ThisAndNeighboringFilesBB[8];
-Bitboard InFrontBB[2][8];
-Bitboard StepAttacksBB[16][64];
-Bitboard BetweenBB[64][64];
-Bitboard SquaresInFrontMask[2][64];
-Bitboard PassedPawnMask[2][64];
-Bitboard AttackSpanMask[2][64];
-
-Bitboard BishopPseudoAttacks[64];
-Bitboard RookPseudoAttacks[64];
-Bitboard QueenPseudoAttacks[64];
-
-uint8_t BitCount8Bit[256];
-
-
-namespace {
-
   CACHE_LINE_ALIGNMENT int BSFTable[64];
 
   CACHE_LINE_ALIGNMENT int BSFTable[64];
 
-  void init_sliding_attacks(Bitboard attacks[], int attackIndex[], Bitboard mask[],
-                            const int shift[], const Bitboard mult[], int deltas[][2]);
+  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
+                            const Bitboard mult[], Square deltas[]);
 }
 
 
 }
 
 
@@ -354,11 +351,11 @@ void init_bitboards() {
                       set_bit(&StepAttacksBB[make_piece(c, pt)][s], to);
               }
 
                       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, RAttackIndex, RMask, RShift, RMult, rookDeltas);
-  init_sliding_attacks(BAttacks, BAttackIndex, BMask, BShift, BMult, bishopDeltas);
+  init_sliding_attacks(RAttacks, RMagics, RShift, RMult, RDeltas);
+  init_sliding_attacks(BAttacks, BMagics, BShift, BMult, BDeltas);
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
   {
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
   {
@@ -384,68 +381,67 @@ void init_bitboards() {
 
 namespace {
 
 
 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[], bool skipLast) {
+
     Bitboard attacks = 0;
     Bitboard attacks = 0;
-    int dx, dy, f, r;
-    int rk = sq / 8;
-    int fl = sq % 8;
 
     for (int i = 0; i < 4; i++)
     {
 
     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
+               && (!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)))
         {
         {
-            attacks |= (1ULL << (f + r * 8));
+            attacks |= 1ULL << s;
 
 
-            if (occupied & (1ULL << (f + r * 8)))
+            if (occupied & (1ULL << s))
                 break;
 
                 break;
 
-            f += dx;
-            r += dy;
+            s += deltas[i];
         }
     }
     return attacks;
   }
 
         }
     }
     return attacks;
   }
 
-  void init_sliding_attacks(Bitboard attacks[], int attackIndex[], Bitboard mask[],
-                            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 int shift[],
+                            const Bitboard mult[], Square deltas[]) {
+    Bitboard occupancy, index;
+    int maxKey, offset = 0;
 
 
-    for (i = index = 0; i < 64; i++)
+    for (Square s = SQ_A1; s <= SQ_H8; s++)
     {
     {
-        attackIndex[i] = index;
-        mask[i] = sliding_attacks(i, 0, deltas, 1, 6, 1, 6);
-        j = 1 << ((CpuIs64Bit ? 64 : 32) - shift[i]);
+        m[s].offset = offset;
+        m[s].mult   = mult[s];
+        m[s].shift  = shift[s];
+        m[s].mask   = sliding_attacks(s, EmptyBoardBB, deltas, true);
+
+        maxKey = 1 << (CpuIs64Bit ? 64 - shift[s] : 32 - shift[s]);
 
 
-        for (int k = 0; k < j; k++)
+        for (int key = 0; key < maxKey; key++)
         {
         {
-            b = index_to_bitboard(k, mask[i]);
-            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));
+
+            attacks[offset + (index >> shift[s])] = sliding_attacks(s, occupancy, deltas, false);
         }
         }
-        index += j;
+        offset += maxKey;
     }
   }
 }
     }
   }
 }