]> git.sesse.net Git - stockfish/commitdiff
Rewrite initialization of PseudoMoves
authorprotonspring <mike@whiteley.org>
Fri, 3 Jan 2020 18:33:18 +0000 (11:33 -0700)
committerStéphane Nicolet <cassio@free.fr>
Fri, 10 Jan 2020 00:58:27 +0000 (01:58 +0100)
This is a non-functional code style change. I believe master is a bit convoluted
here and propose this version for clarity.

No functional change

src/bitboard.cpp

index 45d5150408751a078be90973559d79cfbe1b40ba..70114f20fadd3485e2c30d93f2760038ea50c61d 100644 (file)
@@ -76,25 +76,29 @@ void Bitboards::init() {
 
   for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
       for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
-              SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
-
-  int steps[][5] = { {}, { 7, 9 }, { 6, 10, 15, 17 }, {}, {}, {}, { 1, 7, 8, 9 } };
-
-  for (Color c : { WHITE, BLACK })
-      for (PieceType pt : { PAWN, KNIGHT, KING })
-          for (Square s = SQ_A1; s <= SQ_H8; ++s)
-              for (int i = 0; steps[pt][i]; ++i)
-              {
-                  Square to = s + Direction(c == WHITE ? steps[pt][i] : -steps[pt][i]);
-
-                  if (is_ok(to) && distance(s, to) < 3)
-                  {
-                      if (pt == PAWN)
-                          PawnAttacks[c][s] |= to;
-                      else
-                          PseudoAttacks[pt][s] |= to;
-                  }
-              }
+          SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
+
+  for (Square s = SQ_A1; s <= SQ_H8; ++s)
+  {
+      PawnAttacks[WHITE][s] = pawn_attacks_bb<WHITE>(square_bb(s));
+      PawnAttacks[BLACK][s] = pawn_attacks_bb<BLACK>(square_bb(s));
+  }
+
+  // Helper returning the target bitboard of a step from a square
+  auto landing_square_bb = [&](Square s, int step)
+  {
+      Square to = Square(s + step);
+      return is_ok(to) && distance(s, to) <= 2 ? square_bb(to) : Bitboard(0);
+  };
+
+  for (Square s = SQ_A1; s <= SQ_H8; ++s)
+  {
+      for (int step : {-9, -8, -7, -1, 1, 7, 8, 9} )
+         PseudoAttacks[KING][s] |= landing_square_bb(s, step);
+
+      for (int step : {-17, -15, -10, -6, 6, 10, 15, 17} )
+         PseudoAttacks[KNIGHT][s] |= landing_square_bb(s, step);
+  }
 
   Direction RookDirections[] = { NORTH, EAST, SOUTH, WEST };
   Direction BishopDirections[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };