]> git.sesse.net Git - stockfish/commitdiff
Rewrite some bitboard init code
authorMarco Costalba <mcostalba@gmail.com>
Sat, 30 Nov 2013 09:27:23 +0000 (10:27 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 30 Nov 2013 10:02:56 +0000 (11:02 +0100)
And move the static function Position::attacks_from() to
bitboard code renaming it attacks_bb()

No functional change.

src/bitboard.cpp
src/bitboard.h
src/position.cpp
src/position.h
src/search.cpp

index 3683b363c1d90c6c961a09525b2d63a2e2c6fda7..a6502330ab7b23105c4ba917f5e502f91a712d68 100644 (file)
@@ -212,24 +212,23 @@ void Bitboards::init() {
   init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
   init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
 
   init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
   init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
 
-  for (Square s = SQ_A1; s <= SQ_H8; ++s)
+  for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
   {
   {
-      PseudoAttacks[QUEEN][s]  = PseudoAttacks[BISHOP][s] = attacks_bb<BISHOP>(s, 0);
-      PseudoAttacks[QUEEN][s] |= PseudoAttacks[  ROOK][s] = attacks_bb<  ROOK>(s, 0);
-  }
+      PseudoAttacks[QUEEN][s1]  = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(s1, 0);
+      PseudoAttacks[QUEEN][s1] |= PseudoAttacks[  ROOK][s1] = attacks_bb<  ROOK>(s1, 0);
 
 
-  for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
       for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
       for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
-          if (PseudoAttacks[QUEEN][s1] & s2)
-          {
-              Square delta = (s2 - s1) / square_distance(s1, s2);
+      {
+          Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP :
+                     (PseudoAttacks[ROOK][s1]   & s2) ? W_ROOK   : NO_PIECE;
 
 
-              for (Square s = s1 + delta; s != s2; s += delta)
-                  BetweenBB[s1][s2] |= s;
+          if (pc == NO_PIECE)
+              continue;
 
 
-              PieceType pt = (PseudoAttacks[BISHOP][s1] & s2) ? BISHOP : ROOK;
-              LineBB[s1][s2] = (PseudoAttacks[pt][s1] & PseudoAttacks[pt][s2]) | s1 | s2;
-          }
+          LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2;
+          BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]);
+      }
+  }
 }
 
 
 }
 
 
index e95bf0d275996f0965efb36617c840408821d6f3..349005787fd402d6ab958818f46e1262fc4b8356 100644 (file)
@@ -254,6 +254,16 @@ inline Bitboard attacks_bb(Square s, Bitboard occ) {
   return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
 }
 
   return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
 }
 
+inline Bitboard attacks_bb(Piece p, Square s, Bitboard occ) {
+
+  switch (type_of(p))
+  {
+  case BISHOP: return attacks_bb<BISHOP>(s, occ);
+  case ROOK  : return attacks_bb<ROOK>(s, occ);
+  case QUEEN : return attacks_bb<BISHOP>(s, occ) | attacks_bb<ROOK>(s, occ);
+  default    : return StepAttacksBB[p][s];
+  }
+}
 
 /// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
 /// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
 
 /// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
 /// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
index c40b1199bd823f0f6f9268bff8ba70e42304fd21..ca7c460047bdd16716584916bbe4bd3abfcfe804 100644 (file)
@@ -455,23 +455,6 @@ Bitboard Position::attackers_to(Square s, Bitboard occ) const {
 }
 
 
 }
 
 
-/// Position::attacks_from() computes a bitboard of all attacks of a given piece
-/// put in a given square. Slider attacks use occ bitboard as occupancy.
-
-Bitboard Position::attacks_from(Piece p, Square s, Bitboard occ) {
-
-  assert(is_ok(s));
-
-  switch (type_of(p))
-  {
-  case BISHOP: return attacks_bb<BISHOP>(s, occ);
-  case ROOK  : return attacks_bb<ROOK>(s, occ);
-  case QUEEN : return attacks_bb<BISHOP>(s, occ) | attacks_bb<ROOK>(s, occ);
-  default    : return StepAttacksBB[p][s];
-  }
-}
-
-
 /// Position::legal() tests whether a pseudo-legal move is legal
 
 bool Position::legal(Move m, Bitboard pinned) const {
 /// Position::legal() tests whether a pseudo-legal move is legal
 
 bool Position::legal(Move m, Bitboard pinned) const {
@@ -672,7 +655,7 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const {
   switch (type_of(m))
   {
   case PROMOTION:
   switch (type_of(m))
   {
   case PROMOTION:
-      return attacks_from(Piece(promotion_type(m)), to, pieces() ^ from) & ksq;
+      return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & ksq;
 
   // En passant capture with check ? We have already handled the case
   // of direct checks and ordinary discovered check, the only case we
 
   // En passant capture with check ? We have already handled the case
   // of direct checks and ordinary discovered check, the only case we
index c333e55a63ab859e43d7085f2d80f89e4ab64df6..cd297df737839e15967b37a280f7cc054c8a70c0 100644 (file)
@@ -114,7 +114,6 @@ public:
   Bitboard attackers_to(Square s) const;
   Bitboard attackers_to(Square s, Bitboard occ) const;
   Bitboard attacks_from(Piece p, Square s) const;
   Bitboard attackers_to(Square s) const;
   Bitboard attackers_to(Square s, Bitboard occ) const;
   Bitboard attacks_from(Piece p, Square s) const;
-  static Bitboard attacks_from(Piece p, Square s, Bitboard occ);
   template<PieceType> Bitboard attacks_from(Square s) const;
   template<PieceType> Bitboard attacks_from(Square s, Color c) const;
 
   template<PieceType> Bitboard attacks_from(Square s) const;
   template<PieceType> Bitboard attacks_from(Square s, Color c) const;
 
@@ -304,7 +303,7 @@ inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
 }
 
 inline Bitboard Position::attacks_from(Piece p, Square s) const {
 }
 
 inline Bitboard Position::attacks_from(Piece p, Square s) const {
-  return attacks_from(p, s, byTypeBB[ALL_PIECES]);
+  return attacks_bb(p, s, byTypeBB[ALL_PIECES]);
 }
 
 inline Bitboard Position::attackers_to(Square s) const {
 }
 
 inline Bitboard Position::attackers_to(Square s) const {
index f1ab80f29294e20401fb93e9736b3b2b679668de..e235c3fc615733c6e68ae879381c6b48ba7bba2e 100644 (file)
@@ -1351,7 +1351,7 @@ moves_loop: // When in check and at SpNode search starts from here
       return true;
 
     // Second's destination is defended by the first move's piece
       return true;
 
     // Second's destination is defended by the first move's piece
-    Bitboard m1att = pos.attacks_from(pos.piece_on(m1to), m1to, pos.pieces() ^ m2from);
+    Bitboard m1att = attacks_bb(pos.piece_on(m1to), m1to, pos.pieces() ^ m2from);
     if (m1att & m2to)
         return true;
 
     if (m1att & m2to)
         return true;
 
@@ -1395,7 +1395,7 @@ moves_loop: // When in check and at SpNode search starts from here
         Piece pc = pos.piece_on(m1from);
 
         // The moved piece attacks the square 'tto' ?
         Piece pc = pos.piece_on(m1from);
 
         // The moved piece attacks the square 'tto' ?
-        if (pos.attacks_from(pc, m1to, occ) & m2to)
+        if (attacks_bb(pc, m1to, occ) & m2to)
             return true;
 
         // Scan for possible X-ray attackers behind the moved piece
             return true;
 
         // Scan for possible X-ray attackers behind the moved piece