]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Implement PEXT based attacks
[stockfish] / src / bitboard.cpp
index b7eed922d10b0badeb0c4580ea34b76e31adaabf..b7fce20d9206a4fdab6716ef7158b08e27a5b12f 100644 (file)
@@ -18,8 +18,7 @@
 */
 
 #include <algorithm>
-#include <cstring>
-#include <sstream>
+#include <cstring> // For memset
 
 #include "bitboard.h"
 #include "bitcount.h"
@@ -80,6 +79,23 @@ namespace {
   }
 }
 
+
+/// Intel PEXT (parallel extraction) software implementation
+Bitboard pext(Bitboard b, Bitboard mask) {
+
+  Bitboard res = 0;
+
+  for (Bitboard bb = 1; mask; bb += bb)
+  {
+      if (b & mask & -mask)
+          res |= bb;
+
+      mask &= mask - 1;
+  }
+  return res;
+}
+
+
 /// lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
 /// pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
 
@@ -130,19 +146,17 @@ Square msb(Bitboard b) {
 
 const std::string Bitboards::pretty(Bitboard b) {
 
-  std::ostringstream ss;
+  std::string s = "+---+---+---+---+---+---+---+---+\n";
 
-  for (Rank rank = RANK_8; rank >= RANK_1; --rank)
+  for (Rank r = RANK_8; r >= RANK_1; --r)
   {
-      ss << "+---+---+---+---+---+---+---+---+" << '\n';
+      for (File f = FILE_A; f <= FILE_H; ++f)
+          s.append(b & make_square(f, r) ? "| X " : "|   ");
 
-      for (File file = FILE_A; file <= FILE_H; ++file)
-          ss << "| " << (b & (file | rank) ? "X " : "  ");
-
-      ss << "|\n";
+      s.append("|\n+---+---+---+---+---+---+---+---+\n");
   }
-  ss << "+---+---+---+---+---+---+---+---+";
-  return ss.str();
+
+  return s;
 }
 
 
@@ -255,7 +269,7 @@ namespace {
                    Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
 
     int MagicBoosters[][8] = { {  969, 1976, 2850,  542, 2069, 2852, 1708,  164 },
-                               { 3101,  552, 3555,  926,  834, 4122, 2131, 1117 } };
+                               { 3101,  552, 3555,  926,  834,   26, 2131, 1117 } };
 
     RKISS rk;
     Bitboard occupancy[4096], reference[4096], edges, b;
@@ -282,7 +296,12 @@ namespace {
         b = size = 0;
         do {
             occupancy[size] = b;
-            reference[size++] = sliding_attack(deltas, s, b);
+            reference[size] = sliding_attack(deltas, s, b);
+
+            if (HasPext)
+                attacks[s][pext(occupancy[size], masks[s])] = reference[size];
+
+            size++;
             b = (b - masks[s]) & masks[s];
         } while (b);
 
@@ -291,6 +310,9 @@ namespace {
         if (s < SQ_H8)
             attacks[s + 1] = attacks[s] + size;
 
+        if (HasPext)
+            continue;
+
         booster = MagicBoosters[Is64Bit][rank_of(s)];
 
         // Find a magic for square 's' picking up an (almost) random number