]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Fix a crash on multi-pv
[stockfish] / src / bitboard.cpp
index 43054cfb9f54532f5329f5ea45a6f79700bb5c6f..9d9a16e8ba3614f51ff55eec3f14f813bd0d6e24 100644 (file)
@@ -248,6 +248,7 @@ namespace {
   void init_attacks();
   void init_between_bitboards();
   void init_pseudo_attacks();
+  SquareDelta squares_delta(Square orig, Square dest);
   Bitboard index_to_bitboard(int index, Bitboard mask);
   Bitboard sliding_attacks(int sq, Bitboard block, int dirs, int deltas[][2],
                            int fmin, int fmax, int rmin, int rmax);
@@ -453,28 +454,40 @@ namespace {
     return result;
   }
 
-  void init_between_bitboards() {
+  SquareDelta squares_delta(Square orig, Square dest) {
 
-    const SquareDelta directionToDelta[] = {
-        DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE
-    };
+    const SquareDelta deltas[] = { DELTA_N, DELTA_NE, DELTA_E, DELTA_SE,
+                                   DELTA_S, DELTA_SW, DELTA_W, DELTA_NW };
+
+    for (int idx = 0; idx < 8; idx++)
+    {
+        Square s = orig + deltas[idx];
+
+        while (square_is_ok(s) && square_distance(s, s - deltas[idx]) == 1)
+        {
+            if (s == dest)
+                return deltas[idx];
+
+            s += deltas[idx];
+        }
+    }
+    return DELTA_NONE;
+  }
+
+  void init_between_bitboards() {
 
     Square s1, s2, s3;
-    Direction d;
+    SquareDelta d;
 
     for (s1 = SQ_A1; s1 <= SQ_H8; s1++)
         for (s2 = SQ_A1; s2 <= SQ_H8; s2++)
         {
             BetweenBB[s1][s2] = EmptyBoardBB;
-            d = direction_between_squares(s1, s2);
-
-            if (d == DIR_NONE)
-                continue;
-
-            SquareDelta sd = directionToDelta[s2 > s1 ? d : d + 4];
+            d = squares_delta(s1, s2);
 
-            for (s3 = s1 + sd; s3 != s2; s3 += sd)
-                set_bit(&(BetweenBB[s1][s2]), s3);
+            if (d != DELTA_NONE)
+                for (s3 = s1 + d; s3 != s2; s3 += d)
+                    set_bit(&(BetweenBB[s1][s2]), s3);
       }
   }