]> git.sesse.net Git - stockfish/commitdiff
Retire enum Direction
authorMarco Costalba <mcostalba@gmail.com>
Sun, 26 Dec 2010 11:41:30 +0000 (12:41 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 26 Dec 2010 15:17:51 +0000 (16:17 +0100)
Use SquareDelta instead

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/bitboard.cpp
src/direction.cpp
src/position.cpp
src/square.h

index 43054cfb9f54532f5329f5ea45a6f79700bb5c6f..01c9954add14ac064a7955bf074ec86906e6d7f1 100644 (file)
@@ -455,25 +455,19 @@ namespace {
 
   void init_between_bitboards() {
 
-    const SquareDelta directionToDelta[] = {
-        DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE
-    };
-
     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);
+            d = SquareDelta(DirectionTable[s1][s2]);
 
-            if (d == DIR_NONE)
+            if (d == DELTA_NONE)
                 continue;
 
-            SquareDelta sd = directionToDelta[s2 > s1 ? d : d + 4];
-
-            for (s3 = s1 + sd; s3 != s2; s3 += sd)
+            for (s3 = s1 + d; s3 != s2; s3 += d)
                 set_bit(&(BetweenBB[s1][s2]), s3);
       }
   }
index 77a985b1bae683fd696c817d089603390019ff62..ebff65b16b9b781f307810524a005f2f32cab3cc 100644 (file)
 
 #include "square.h"
 
-uint8_t DirectionTable[64][64];
+int8_t DirectionTable[64][64];
 
 
-static bool reachable(Square orig, Square dest, Direction d) {
+static SquareDelta direction(Square orig, Square dest) {
 
-  const SquareDelta directionToDelta[] = {
+  const SquareDelta directions[] = {
       DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE
   };
 
-  SquareDelta delta = directionToDelta[dest > orig ? d : d + 4];
-  Square from = orig;
-  Square to = from + delta;
+  Square from, to;
 
-  while (to != dest && square_distance(to, from) == 1 && square_is_ok(to))
+  for (int idx = 0; idx < 8; idx++)
   {
-      from = to;
-      to += delta;
+      from = orig;
+      to = from + directions[idx];
+
+      while (to != dest && square_distance(to, from) == 1 && square_is_ok(to))
+      {
+          from = to;
+          to += directions[idx];
+      }
+
+      if (to == dest && square_distance(from, to) == 1)
+          return directions[idx];
   }
-  return to == dest && square_distance(from, to) == 1;
+  return DELTA_NONE;
 }
 
 void init_direction_table() {
 
   for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
       for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
-      {
-          DirectionTable[s1][s2] = uint8_t(DIR_NONE);
-
-          if (s1 == s2)
-              continue;
-
-          for (Direction d = DIR_E; d != DIR_NONE; d++)
-          {
-              if (reachable(s1, s2, d))
-              {
-                  DirectionTable[s1][s2] = uint8_t(d);
-                  break;
-              }
-          }
-      }
+          DirectionTable[s1][s2] = uint8_t(direction(s1, s2));
 }
index 2d1e476391c19db02f1f26cdcc5c25a7a45ac3c0..f0301e4ec9308af1a78394c5728b0246b1af30a3 100644 (file)
@@ -638,9 +638,9 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
 
   // A non-king move is legal if and only if it is not pinned or it
   // is moving along the ray towards or away from the king.
-  return   !pinned
-          || !bit_is_set(pinned, from)
-          || (direction_between_squares(from, king_square(us)) == direction_between_squares(move_to(m), king_square(us))));
+  return   !pinned
+        || !bit_is_set(pinned, from)
+        ||  squares_aligned(from, move_to(m), king_square(us));
 }
 
 
@@ -698,7 +698,7 @@ bool Position::move_is_check(Move m, const CheckInfo& ci) const {
   {
       // For pawn and king moves we need to verify also direction
       if (  (pt != PAWN && pt != KING)
-          ||(direction_between_squares(from, ci.ksq) != direction_between_squares(to, ci.ksq)))
+          || !squares_aligned(from, to, ci.ksq))
           return true;
   }
 
index 731a898e83444f549339d370587ea9f8be2d6b28..7a59e078fdc7ec4a02a0a7e2068752778a513668 100644 (file)
@@ -58,26 +58,20 @@ enum Rank {
 
 enum SquareDelta {
 
-  DELTA_N = 8, DELTA_E = 1, DELTA_S = -8, DELTA_W = -1,
+  DELTA_N = 8, DELTA_E = 1, DELTA_S = -8, DELTA_W = -1, DELTA_NONE = 0,
 
   DELTA_NN = DELTA_N + DELTA_N,
   DELTA_NE = DELTA_N + DELTA_E,
   DELTA_SE = DELTA_S + DELTA_E,
   DELTA_SS = DELTA_S + DELTA_S,
   DELTA_SW = DELTA_S + DELTA_W,
-  DELTA_NW = DELTA_N + DELTA_W,
+  DELTA_NW = DELTA_N + DELTA_W
 };
 
-enum Direction {
-  DIR_E = 0, DIR_N = 1, DIR_NE = 2, DIR_NW = 3, DIR_NONE = 4
-};
-
-
 ENABLE_OPERATORS_ON(Square);
 ENABLE_OPERATORS_ON(File);
 ENABLE_OPERATORS_ON(Rank);
 ENABLE_OPERATORS_ON(SquareDelta);
-ENABLE_OPERATORS_ON(Direction);
 
 
 ////
@@ -87,7 +81,7 @@ ENABLE_OPERATORS_ON(Direction);
 const int FlipMask = 56;
 const int FlopMask =  7;
 
-extern uint8_t DirectionTable[64][64];
+extern int8_t DirectionTable[64][64];
 
 
 ////
@@ -189,16 +183,14 @@ inline bool square_is_ok(Square s) {
   return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
 }
 
-inline Direction direction_between_squares(Square s1, Square s2) {
-  return Direction(DirectionTable[s1][s2]);
-}
-
-inline int direction_is_diagonal(Square s1, Square s2) {
-  return DirectionTable[s1][s2] & 2;
+inline bool squares_aligned(Square s1, Square s2, Square s3) {
+  return   DirectionTable[s1][s2] != DELTA_NONE
+        && abs(DirectionTable[s1][s2]) ==  abs(DirectionTable[s2][s3]);
 }
 
 inline bool direction_is_straight(Square s1, Square s2) {
-  return DirectionTable[s1][s2] < 2;
+  return   abs(DirectionTable[s1][s2]) == DELTA_N
+        || abs(DirectionTable[s1][s2]) == DELTA_E;
 }
 
 ////