Speed up slider blockers calculation
authorFabian Fichter <ianfab@users.noreply.github.com>
Thu, 31 Jan 2019 22:15:54 +0000 (23:15 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Thu, 31 Jan 2019 22:15:54 +0000 (23:15 +0100)
Improves speed by >10%.

No functional change.

src/evaluate.cpp
src/position.cpp
src/position.h

index 937e734..9ed1a3d 100644 (file)
@@ -437,7 +437,7 @@ namespace {
         {
             // Penalty if any relative pin or discovered attack against the queen
             Bitboard queenPinners;
-            if (pos.slider_blockers(pos.pieces(Them, ROOK, BISHOP), s, queenPinners))
+            if (pos.slider_blockers(pos.pieces(Them, ROOK, BISHOP), s, queenPinners, Them))
                 score -= WeakQueen;
         }
     }
index 2b52675..ee5b2f1 100644 (file)
@@ -476,8 +476,8 @@ void Position::set_castling_right(Color c, Square rfrom) {
 
 void Position::set_check_info(StateInfo* si) const {
 
-  si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), count<KING>(WHITE) ? square<KING>(WHITE) : SQ_NONE, si->pinners[BLACK]);
-  si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), count<KING>(BLACK) ? square<KING>(BLACK) : SQ_NONE, si->pinners[WHITE]);
+  si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), count<KING>(WHITE) ? square<KING>(WHITE) : SQ_NONE, si->pinners[BLACK], BLACK);
+  si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), count<KING>(BLACK) ? square<KING>(BLACK) : SQ_NONE, si->pinners[WHITE], WHITE);
 
   Square ksq = count<KING>(~sideToMove) ? square<KING>(~sideToMove) : SQ_NONE;
 
@@ -664,7 +664,7 @@ const string Position::fen() const {
 /// a pinned or a discovered check piece, according if its color is the opposite
 /// or the same of the color of the slider.
 
-Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const {
+Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners, Color c) const {
 
   Bitboard blockers = 0;
   pinners = 0;
@@ -673,9 +673,14 @@ Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners
       return blockers;
 
   // Snipers are sliders that attack 's' when a piece is removed
-  Bitboard snipers =   sliders
-                    &  attackers_to(s, 0)
-                    & ~attackers_to(s);
+  Bitboard snipers = 0;
+
+  for (PieceType pt : piece_types())
+  {
+      Bitboard b = sliders & (PseudoAttacks[~c][pt][s] ^ LeaperAttacks[~c][pt][s]) & pieces(c, pt);
+      if (b)
+          snipers |= b & ~attacks_from(~c, pt, s);
+  }
 
   while (snipers)
   {
index df16fcf..b3686c2 100644 (file)
@@ -182,7 +182,7 @@ public:
   Bitboard attacks_from(Color c, PieceType pt, Square s) const;
   template<PieceType> Bitboard attacks_from(Color c, Square s) const;
   Bitboard moves_from(Color c, PieceType pt, Square s) const;
-  Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const;
+  Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners, Color c) const;
 
   // Properties of moves
   bool legal(Move m) const;