]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Update Makefile for Mac OS X compilation
[stockfish] / src / bitboard.cpp
index b1bf4d91e583b3f07f1ac53cb669dce2a4522eb0..32efaedafd55fcd92977babc906d493cb3bf9b85 100644 (file)
@@ -1,7 +1,7 @@
 /*
   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
-  Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
+  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
 
   Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <iostream>
+#include <algorithm>
+#include <cstring>   // For std::memset
 
 #include "bitboard.h"
 #include "bitcount.h"
+#include "misc.h"
+
+int SquareDistance[SQUARE_NB][SQUARE_NB];
+
+Bitboard  RookMasks  [SQUARE_NB];
+Bitboard  RookMagics [SQUARE_NB];
+Bitboard* RookAttacks[SQUARE_NB];
+unsigned  RookShifts [SQUARE_NB];
+
+Bitboard  BishopMasks  [SQUARE_NB];
+Bitboard  BishopMagics [SQUARE_NB];
+Bitboard* BishopAttacks[SQUARE_NB];
+unsigned  BishopShifts [SQUARE_NB];
+
+Bitboard SquareBB[SQUARE_NB];
+Bitboard FileBB[FILE_NB];
+Bitboard RankBB[RANK_NB];
+Bitboard AdjacentFilesBB[FILE_NB];
+Bitboard InFrontBB[COLOR_NB][RANK_NB];
+Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
+Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
+Bitboard LineBB[SQUARE_NB][SQUARE_NB];
+Bitboard DistanceRingBB[SQUARE_NB][8];
+Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
+Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
+Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
+Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
 
-// Global bitboards definitions with static storage duration are
-// automatically set to zero before enter main().
-Bitboard RAttacks[0x19000];
-Bitboard BAttacks[0x1480];
+namespace {
 
-Magics RMagics[64];
-Magics BMagics[64];
+  // De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
+  const uint64_t DeBruijn64 = 0x3F79D71B4CB0A89ULL;
+  const uint32_t DeBruijn32 = 0x783A9B23;
 
-Bitboard SetMaskBB[65];
-Bitboard ClearMaskBB[65];
+  int MS1BTable[256];           // To implement software msb()
+  Square BSFTable[SQUARE_NB];   // To implement software bitscan
+  Bitboard RookTable[0x19000];  // To store rook attacks
+  Bitboard BishopTable[0x1480]; // To store bishop attacks
 
-Bitboard SquaresByColorBB[2];
-Bitboard FileBB[8];
-Bitboard RankBB[8];
-Bitboard NeighboringFilesBB[8];
-Bitboard ThisAndNeighboringFilesBB[8];
-Bitboard InFrontBB[2][8];
-Bitboard StepAttacksBB[16][64];
-Bitboard BetweenBB[64][64];
-Bitboard SquaresInFrontMask[2][64];
-Bitboard PassedPawnMask[2][64];
-Bitboard AttackSpanMask[2][64];
+  typedef unsigned (Fn)(Square, Bitboard);
 
-Bitboard BishopPseudoAttacks[64];
-Bitboard RookPseudoAttacks[64];
-Bitboard QueenPseudoAttacks[64];
+  void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
+                   Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
 
-uint8_t BitCount8Bit[256];
+  // bsf_index() returns the index into BSFTable[] to look up the bitscan. Uses
+  // Matt Taylor's folding for 32 bit case, extended to 64 bit by Kim Walisch.
 
-namespace {
+  FORCE_INLINE unsigned bsf_index(Bitboard b) {
+    b ^= b - 1;
+    return Is64Bit ? (b * DeBruijn64) >> 58
+                   : ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn32) >> 26;
+  }
+}
 
-#if defined(IS_64BIT)
-
-const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
-
-const uint64_t BMult[64] = {
-  0x0440049104032280ULL, 0x1021023C82008040ULL, 0x0404040082000048ULL,
-  0x48C4440084048090ULL, 0x2801104026490000ULL, 0x4100880442040800ULL,
-  0x0181011002E06040ULL, 0x9101004104200E00ULL, 0x1240848848310401ULL,
-  0x2000142828050024ULL, 0x00001004024D5000ULL, 0x0102044400800200ULL,
-  0x8108108820112000ULL, 0xA880818210C00046ULL, 0x4008008801082000ULL,
-  0x0060882404049400ULL, 0x0104402004240810ULL, 0x000A002084250200ULL,
-  0x00100B0880801100ULL, 0x0004080201220101ULL, 0x0044008080A00000ULL,
-  0x0000202200842000ULL, 0x5006004882D00808ULL, 0x0000200045080802ULL,
-  0x0086100020200601ULL, 0xA802080A20112C02ULL, 0x0080411218080900ULL,
-  0x000200A0880080A0ULL, 0x9A01010000104000ULL, 0x0028008003100080ULL,
-  0x0211021004480417ULL, 0x0401004188220806ULL, 0x00825051400C2006ULL,
-  0x00140C0210943000ULL, 0x0000242800300080ULL, 0x00C2208120080200ULL,
-  0x2430008200002200ULL, 0x1010100112008040ULL, 0x8141050100020842ULL,
-  0x0000822081014405ULL, 0x800C049E40400804ULL, 0x4A0404028A000820ULL,
-  0x0022060201041200ULL, 0x0360904200840801ULL, 0x0881A08208800400ULL,
-  0x0060202C00400420ULL, 0x1204440086061400ULL, 0x0008184042804040ULL,
-  0x0064040315300400ULL, 0x0C01008801090A00ULL, 0x0808010401140C00ULL,
-  0x04004830C2020040ULL, 0x0080005002020054ULL, 0x40000C14481A0490ULL,
-  0x0010500101042048ULL, 0x1010100200424000ULL, 0x0000640901901040ULL,
-  0x00000A0201014840ULL, 0x00840082AA011002ULL, 0x010010840084240AULL,
-  0x0420400810420608ULL, 0x8D40230408102100ULL, 0x4A00200612222409ULL,
-  0x0A08520292120600ULL
-};
-
-const uint64_t RMult[64] = {
-  0x0A8002C000108020ULL, 0x4440200140003000ULL, 0x8080200010011880ULL,
-  0x0380180080141000ULL, 0x1A00060008211044ULL, 0x410001000A0C0008ULL,
-  0x9500060004008100ULL, 0x0100024284A20700ULL, 0x0000802140008000ULL,
-  0x0080C01002A00840ULL, 0x0402004282011020ULL, 0x9862000820420050ULL,
-  0x0001001448011100ULL, 0x6432800200800400ULL, 0x040100010002000CULL,
-  0x0002800D0010C080ULL, 0x90C0008000803042ULL, 0x4010004000200041ULL,
-  0x0003010010200040ULL, 0x0A40828028001000ULL, 0x0123010008000430ULL,
-  0x0024008004020080ULL, 0x0060040001104802ULL, 0x00582200028400D1ULL,
-  0x4000802080044000ULL, 0x0408208200420308ULL, 0x0610038080102000ULL,
-  0x3601000900100020ULL, 0x0000080080040180ULL, 0x00C2020080040080ULL,
-  0x0080084400100102ULL, 0x4022408200014401ULL, 0x0040052040800082ULL,
-  0x0B08200280804000ULL, 0x008A80A008801000ULL, 0x4000480080801000ULL,
-  0x0911808800801401ULL, 0x822A003002001894ULL, 0x401068091400108AULL,
-  0x000004A10A00004CULL, 0x2000800640008024ULL, 0x1486408102020020ULL,
-  0x000100A000D50041ULL, 0x00810050020B0020ULL, 0x0204000800808004ULL,
-  0x00020048100A000CULL, 0x0112000831020004ULL, 0x0009000040810002ULL,
-  0x0440490200208200ULL, 0x8910401000200040ULL, 0x6404200050008480ULL,
-  0x4B824A2010010100ULL, 0x04080801810C0080ULL, 0x00000400802A0080ULL,
-  0x8224080110026400ULL, 0x40002C4104088200ULL, 0x01002100104A0282ULL,
-  0x1208400811048021ULL, 0x3201014A40D02001ULL, 0x0005100019200501ULL,
-  0x0101000208001005ULL, 0x0002008450080702ULL, 0x001002080301D00CULL,
-  0x410201CE5C030092ULL
-};
-
-const int BShift[64] = {
-  58, 59, 59, 59, 59, 59, 59, 58, 59, 59, 59, 59, 59, 59, 59, 59,
-  59, 59, 57, 57, 57, 57, 59, 59, 59, 59, 57, 55, 55, 57, 59, 59,
-  59, 59, 57, 55, 55, 57, 59, 59, 59, 59, 57, 57, 57, 57, 59, 59,
-  59, 59, 59, 59, 59, 59, 59, 59, 58, 59, 59, 59, 59, 59, 59, 58
-};
-
-const int RShift[64] = {
-  52, 53, 53, 53, 53, 53, 53, 52, 53, 54, 54, 54, 54, 54, 54, 53,
-  53, 54, 54, 54, 54, 54, 54, 53, 53, 54, 54, 54, 54, 54, 54, 53,
-  53, 54, 54, 54, 54, 54, 54, 53, 53, 54, 54, 54, 54, 54, 54, 53,
-  53, 54, 54, 54, 54, 54, 54, 53, 52, 53, 53, 53, 53, 53, 53, 52
-};
-
-#else // if !defined(IS_64BIT)
-
-const uint32_t DeBruijnMagic = 0x783A9B23;
-
-const uint64_t BMult[64] = {
-  0x54142844C6A22981ULL, 0x710358A6EA25C19EULL, 0x704F746D63A4A8DCULL,
-  0xBFED1A0B80F838C5ULL, 0x90561D5631E62110ULL, 0x2804260376E60944ULL,
-  0x84A656409AA76871ULL, 0xF0267F64C28B6197ULL, 0x70764EBB762F0585ULL,
-  0x92AA09E0CFE161DEULL, 0x41EE1F6BB266F60EULL, 0xDDCBF04F6039C444ULL,
-  0x5A3FAB7BAC0D988AULL, 0xD3727877FA4EAA03ULL, 0xD988402D868DDAAEULL,
-  0x812B291AFA075C7CULL, 0x94FAF987B685A932ULL, 0x3ED867D8470D08DBULL,
-  0x92517660B8901DE8ULL, 0x2D97E43E058814B4ULL, 0x880A10C220B25582ULL,
-  0xC7C6520D1F1A0477ULL, 0xDBFC7FBCD7656AA6ULL, 0x78B1B9BFB1A2B84FULL,
-  0x2F20037F112A0BC1ULL, 0x657171EA2269A916ULL, 0xC08302B07142210EULL,
-  0x0880A4403064080BULL, 0x3602420842208C00ULL, 0x852800DC7E0B6602ULL,
-  0x595A3FBBAA0F03B2ULL, 0x9F01411558159D5EULL, 0x2B4A4A5F88B394F2ULL,
-  0x4AFCBFFC292DD03AULL, 0x4A4094A3B3F10522ULL, 0xB06F00B491F30048ULL,
-  0xD5B3820280D77004ULL, 0x8B2E01E7C8E57A75ULL, 0x2D342794E886C2E6ULL,
-  0xC302C410CDE21461ULL, 0x111F426F1379C274ULL, 0xE0569220ABB31588ULL,
-  0x5026D3064D453324ULL, 0xE2076040C343CD8AULL, 0x93EFD1E1738021EEULL,
-  0xB680804BED143132ULL, 0x44E361B21986944CULL, 0x44C60170EF5C598CULL,
-  0xF4DA475C195C9C94ULL, 0xA3AFBB5F72060B1DULL, 0xBC75F410E41C4FFCULL,
-  0xB51C099390520922ULL, 0x902C011F8F8EC368ULL, 0x950B56B3D6F5490AULL,
-  0x3909E0635BF202D0ULL, 0x5744F90206EC10CCULL, 0xDC59FD76317ABBC1ULL,
-  0x881C7C67FCBFC4F6ULL, 0x47CA41E7E440D423ULL, 0xEB0C88112048D004ULL,
-  0x51C60E04359AEF1AULL, 0x1AA1FE0E957A5554ULL, 0xDD9448DB4F5E3104ULL,
-  0xDC01F6DCA4BEBBDCULL,
-};
-
-const uint64_t RMult[64] = {
-  0xD7445CDEC88002C0ULL, 0xD0A505C1F2001722ULL, 0xE065D1C896002182ULL,
-  0x9A8C41E75A000892ULL, 0x8900B10C89002AA8ULL, 0x9B28D1C1D60005A2ULL,
-  0x015D6C88DE002D9AULL, 0xB1DBFC802E8016A9ULL, 0x149A1042D9D60029ULL,
-  0xB9C08050599E002FULL, 0x132208C3AF300403ULL, 0xC1000CE2E9C50070ULL,
-  0x9D9AA13C99020012ULL, 0xB6B078DAF71E0046ULL, 0x9D880182FB6E002EULL,
-  0x52889F467E850037ULL, 0xDA6DC008D19A8480ULL, 0x468286034F902420ULL,
-  0x7140AC09DC54C020ULL, 0xD76FFFFA39548808ULL, 0xEA901C4141500808ULL,
-  0xC91004093F953A02ULL, 0x02882AFA8F6BB402ULL, 0xAEBE335692442C01ULL,
-  0x0E904A22079FB91EULL, 0x13A514851055F606ULL, 0x76C782018C8FE632ULL,
-  0x1DC012A9D116DA06ULL, 0x3C9E0037264FFFA6ULL, 0x2036002853C6E4A2ULL,
-  0xE3FE08500AFB47D4ULL, 0xF38AF25C86B025C2ULL, 0xC0800E2182CF9A40ULL,
-  0x72002480D1F60673ULL, 0x2500200BAE6E9B53ULL, 0xC60018C1EEFCA252ULL,
-  0x0600590473E3608AULL, 0x46002C4AB3FE51B2ULL, 0xA200011486BCC8D2ULL,
-  0xB680078095784C63ULL, 0x2742002639BF11AEULL, 0xC7D60021A5BDB142ULL,
-  0xC8C04016BB83D820ULL, 0xBD520028123B4842ULL, 0x9D1600344AC2A832ULL,
-  0x6A808005631C8A05ULL, 0x604600A148D5389AULL, 0xE2E40103D40DEA65ULL,
-  0x945B5A0087C62A81ULL, 0x012DC200CD82D28EULL, 0x2431C600B5F9EF76ULL,
-  0xFB142A006A9B314AULL, 0x06870E00A1C97D62ULL, 0x2A9DB2004A2689A2ULL,
-  0xD3594600CAF5D1A2ULL, 0xEE0E4900439344A7ULL, 0x89C4D266CA25007AULL,
-  0x3E0013A2743F97E3ULL, 0x0180E31A0431378AULL, 0x3A9E465A4D42A512ULL,
-  0x98D0A11A0C0D9CC2ULL, 0x8E711C1ABA19B01EULL, 0x8DCDC836DD201142ULL,
-  0x5AC08A4735370479ULL,
-};
-
-const int BShift[64] = {
-  26, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 27, 27,
-  27, 27, 25, 25, 25, 25, 27, 27, 27, 27, 25, 23, 23, 25, 27, 27,
-  27, 27, 25, 23, 23, 25, 27, 27, 27, 27, 25, 25, 25, 25, 27, 27,
-  27, 27, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 26
-};
-
-const int RShift[64] = {
-  20, 21, 21, 21, 21, 21, 21, 20, 21, 22, 22, 22, 22, 22, 22, 21,
-  21, 22, 22, 22, 22, 22, 22, 21, 21, 22, 22, 22, 22, 22, 22, 21,
-  21, 22, 22, 22, 22, 22, 22, 21, 21, 22, 22, 22, 22, 22, 22, 21,
-  21, 22, 22, 22, 22, 22, 22, 21, 20, 21, 21, 21, 21, 21, 21, 20
-};
-
-#endif // defined(IS_64BIT)
-
-  CACHE_LINE_ALIGNMENT int BSFTable[64];
-
-  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
-                            const Bitboard mult[], Square deltas[]);
+#ifndef USE_BSFQ
+
+/// Software fall-back of lsb() and msb() for CPU lacking hardware support
+
+Square lsb(Bitboard b) {
+  return BSFTable[bsf_index(b)];
 }
 
+Square msb(Bitboard b) {
 
-/// print_bitboard() prints a bitboard in an easily readable format to the
-/// standard output. This is sometimes useful for debugging.
+  unsigned b32;
+  int result = 0;
 
-void print_bitboard(Bitboard b) {
+  if (b > 0xFFFFFFFF)
+  {
+      b >>= 32;
+      result = 32;
+  }
+
+  b32 = unsigned(b);
 
-  for (Rank r = RANK_8; r >= RANK_1; r--)
+  if (b32 > 0xFFFF)
   {
-      std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
-      for (File f = FILE_A; f <= FILE_H; f++)
-          std::cout << "| " << (bit_is_set(b, make_square(f, r)) ? 'X' : ' ') << ' ';
+      b32 >>= 16;
+      result += 16;
+  }
 
-      std::cout << "|\n";
+  if (b32 > 0xFF)
+  {
+      b32 >>= 8;
+      result += 8;
   }
-  std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
+
+  return Square(result + MS1BTable[b32]);
 }
 
+#endif // ifndef USE_BSFQ
 
-/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
-/// pop_1st_bit() finds and clears the least significant nonzero bit in a
-/// nonzero bitboard.
 
-#if defined(IS_64BIT) && !defined(USE_BSFQ)
+/// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
+/// to be printed to standard output. Useful for debugging.
 
-Square first_1(Bitboard b) {
-  return Square(BSFTable[((b & -b) * DeBruijnMagic) >> 58]);
-}
+const std::string Bitboards::pretty(Bitboard b) {
 
-Square pop_1st_bit(Bitboard* b) {
-  Bitboard bb = *b;
-  *b &= (*b - 1);
-  return Square(BSFTable[((bb & -bb) * DeBruijnMagic) >> 58]);
-}
+  std::string s = "+---+---+---+---+---+---+---+---+\n";
 
-#elif !defined(USE_BSFQ)
+  for (Rank r = RANK_8; r >= RANK_1; --r)
+  {
+      for (File f = FILE_A; f <= FILE_H; ++f)
+          s.append(b & make_square(f, r) ? "| X " : "|   ");
 
-Square first_1(Bitboard b) {
-  b ^= (b - 1);
-  uint32_t fold = unsigned(b) ^ unsigned(b >> 32);
-  return Square(BSFTable[(fold * DeBruijnMagic) >> 26]);
-}
+      s.append("|\n+---+---+---+---+---+---+---+---+\n");
+  }
 
-// Use type-punning
-union b_union {
-
-    Bitboard b;
-    struct {
-#if defined (BIGENDIAN)
-        uint32_t h;
-        uint32_t l;
-#else
-        uint32_t l;
-        uint32_t h;
-#endif
-    } dw;
-};
-
-Square pop_1st_bit(Bitboard* bb) {
-
-   b_union u;
-   Square ret;
-
-   u.b = *bb;
-
-   if (u.dw.l)
-   {
-       ret = Square(BSFTable[((u.dw.l ^ (u.dw.l - 1)) * DeBruijnMagic) >> 26]);
-       u.dw.l &= (u.dw.l - 1);
-       *bb = u.b;
-       return ret;
-   }
-   ret = Square(BSFTable[((~(u.dw.h ^ (u.dw.h - 1))) * DeBruijnMagic) >> 26]);
-   u.dw.h &= (u.dw.h - 1);
-   *bb = u.b;
-   return ret;
+  return s;
 }
 
-#endif // !defined(USE_BSFQ)
 
+/// Bitboards::init() initializes various bitboard tables. It is called at
+/// startup and relies on global objects to be already zero-initialized.
 
-/// init_bitboards() initializes various bitboard arrays. It is called during
-/// program initialization.
+void Bitboards::init() {
 
-void init_bitboards() {
-
-  SquaresByColorBB[DARK]  =  0xAA55AA55AA55AA55ULL;
-  SquaresByColorBB[LIGHT] = ~SquaresByColorBB[DARK];
-
-  for (Square s = SQ_A1; s <= SQ_H8; s++)
+  for (Square s = SQ_A1; s <= SQ_H8; ++s)
   {
-      SetMaskBB[s] = (1ULL << s);
-      ClearMaskBB[s] = ~SetMaskBB[s];
+      SquareBB[s] = 1ULL << s;
+      BSFTable[bsf_index(SquareBB[s])] = s;
   }
 
-  ClearMaskBB[SQ_NONE] = ~EmptyBoardBB;
+  for (Bitboard b = 1; b < 256; ++b)
+      MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b);
 
-  FileBB[FILE_A] = FileABB;
-  RankBB[RANK_1] = Rank1BB;
+  for (File f = FILE_A; f <= FILE_H; ++f)
+      FileBB[f] = f > FILE_A ? FileBB[f - 1] << 1 : FileABB;
 
-  for (int f = FILE_B; f <= FILE_H; f++)
-  {
-      FileBB[f] = FileBB[f - 1] << 1;
-      RankBB[f] = RankBB[f - 1] << 8;
-  }
+  for (Rank r = RANK_1; r <= RANK_8; ++r)
+      RankBB[r] = r > RANK_1 ? RankBB[r - 1] << 8 : Rank1BB;
 
-  for (int f = FILE_A; f <= FILE_H; f++)
-  {
-      NeighboringFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
-      ThisAndNeighboringFilesBB[f] = FileBB[f] | NeighboringFilesBB[f];
-  }
+  for (File f = FILE_A; f <= FILE_H; ++f)
+      AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
 
-  for (int rw = RANK_7, rb = RANK_2; rw >= RANK_1; rw--, rb++)
-  {
-      InFrontBB[WHITE][rw] = InFrontBB[WHITE][rw + 1] | RankBB[rw + 1];
-      InFrontBB[BLACK][rb] = InFrontBB[BLACK][rb - 1] | RankBB[rb - 1];
-  }
+  for (Rank r = RANK_1; r < RANK_8; ++r)
+      InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
 
-  for (Color c = WHITE; c <= BLACK; c++)
-      for (Square s = SQ_A1; s <= SQ_H8; s++)
+  for (Color c = WHITE; c <= BLACK; ++c)
+      for (Square s = SQ_A1; s <= SQ_H8; ++s)
       {
-          SquaresInFrontMask[c][s] = in_front_bb(c, s) & file_bb(s);
-          PassedPawnMask[c][s]     = in_front_bb(c, s) & this_and_neighboring_files_bb(s);
-          AttackSpanMask[c][s]     = in_front_bb(c, s) & neighboring_files_bb(s);
+          ForwardBB[c][s]      = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
+          PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
+          PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
       }
 
-  for (Bitboard b = 0; b < 256; b++)
-      BitCount8Bit[b] = (uint8_t)count_1s<CNT32>(b);
-
-  for (int i = 1; i < 64; i++)
-      if (!CpuIs64Bit) // Matt Taylor's folding trick for 32 bit systems
-      {
-          Bitboard b = 1ULL << i;
-          b ^= b - 1;
-          b ^= b >> 32;
-          BSFTable[uint32_t(b * DeBruijnMagic) >> 26] = i;
-      }
-      else
-          BSFTable[((1ULL << i) * DeBruijnMagic) >> 58] = i;
+  for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
+      for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
+          if (s1 != s2)
+          {
+              SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
+              DistanceRingBB[s1][SquareDistance[s1][s2] - 1] |= s2;
+          }
 
-  int steps[][9] = {
-    {0}, {7,9,0}, {17,15,10,6,-6,-10,-15,-17,0}, {0}, {0}, {0}, {9,7,-7,-9,8,1,-1,-8,0}
-  };
+  int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
+                     {}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } };
 
-  for (Color c = WHITE; c <= BLACK; c++)
-      for (Square s = SQ_A1; s <= SQ_H8; s++)
-          for (PieceType pt = PAWN; pt <= KING; pt++)
-              for (int k = 0; steps[pt][k]; k++)
+  for (Color c = WHITE; c <= BLACK; ++c)
+      for (PieceType pt = PAWN; pt <= KING; ++pt)
+          for (Square s = SQ_A1; s <= SQ_H8; ++s)
+              for (int i = 0; steps[pt][i]; ++i)
               {
-                  Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]);
+                  Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
 
-                  if (square_is_ok(to) && square_distance(s, to) < 3)
-                      set_bit(&StepAttacksBB[make_piece(c, pt)][s], to);
+                  if (is_ok(to) && distance(s, to) < 3)
+                      StepAttacksBB[make_piece(c, pt)][s] |= to;
               }
 
-  Square RDeltas[] = { DELTA_N,  DELTA_E,  DELTA_S,  DELTA_W  };
-  Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
+  Square RookDeltas[] = { DELTA_N,  DELTA_E,  DELTA_S,  DELTA_W  };
+  Square BishopDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
 
-  init_sliding_attacks(RAttacks, RMagics, RShift, RMult, RDeltas);
-  init_sliding_attacks(BAttacks, BMagics, BShift, BMult, BDeltas);
+  init_magics(RookTable, RookAttacks, RookMagics, RookMasks, RookShifts, RookDeltas, magic_index<ROOK>);
+  init_magics(BishopTable, BishopAttacks, BishopMagics, BishopMasks, BishopShifts, BishopDeltas, magic_index<BISHOP>);
 
-  for (Square s = SQ_A1; s <= SQ_H8; s++)
+  for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
   {
-      BishopPseudoAttacks[s] = bishop_attacks_bb(s, EmptyBoardBB);
-      RookPseudoAttacks[s]   = rook_attacks_bb(s, EmptyBoardBB);
-      QueenPseudoAttacks[s]  = queen_attacks_bb(s, EmptyBoardBB);
-  }
+      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++)
-          if (bit_is_set(QueenPseudoAttacks[s1], s2))
-          {
-              int f = file_distance(s1, s2);
-              int r = rank_distance(s1, s2);
+      for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
+      {
+          Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP :
+                     (PseudoAttacks[ROOK][s1]   & s2) ? W_ROOK   : NO_PIECE;
 
-              Square d = (s2 - s1) / Max(f, r);
+          if (pc == NO_PIECE)
+              continue;
 
-              for (Square s3 = s1 + d; s3 != s2; s3 += d)
-                  set_bit(&BetweenBB[s1][s2], s3);
-          }
+          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]);
+      }
+  }
 }
 
 
 namespace {
 
-  Bitboard submask(Bitboard mask, int key) {
+  Bitboard sliding_attack(Square deltas[], Square sq, Bitboard occupied) {
 
-    Bitboard subMask = 0;
-    int bitNum = -1;
-
-    // Extract an unique submask out of a mask according to the given key
-    for (Square s = SQ_A1; s <= SQ_H8; s++)
-        if (bit_is_set(mask, s) && bit_is_set(key, Square(++bitNum)))
-            set_bit(&subMask, s);
-
-    return subMask;
-  }
+    Bitboard attack = 0;
 
-  Bitboard sliding_attacks(Square sq, Bitboard occupied, Square deltas[], bool skipLast) {
-
-    Bitboard attacks = 0;
-
-    for (int i = 0; i < 4; i++)
-    {
-        Square s = sq + deltas[i];
-
-        while (    square_is_ok(s)
-               &&  square_distance(s, s - deltas[i]) == 1
-               && (!skipLast || !file_distance(s, sq) || (square_file(s) != FILE_A && square_file(s) != FILE_H))
-               && (!skipLast || !rank_distance(s, sq) || (square_rank(s) != RANK_1 && square_rank(s) != RANK_8)))
+    for (int i = 0; i < 4; ++i)
+        for (Square s = sq + deltas[i];
+             is_ok(s) && distance(s, s - deltas[i]) == 1;
+             s += deltas[i])
         {
-            attacks |= 1ULL << s;
+            attack |= s;
 
-            if (occupied & (1ULL << s))
+            if (occupied & s)
                 break;
-
-            s += deltas[i];
         }
-    }
-    return attacks;
+
+    return attack;
   }
 
-  void init_sliding_attacks(Bitboard attacks[], Magics m[], const int shift[],
-                            const Bitboard mult[], Square deltas[]) {
-    Bitboard occupancy, index;
-    int maxKey, offset = 0;
 
-    for (Square s = SQ_A1; s <= SQ_H8; s++)
-    {
-        m[s].offset = offset;
-        m[s].mult   = mult[s];
-        m[s].shift  = shift[s];
-        m[s].mask   = sliding_attacks(s, EmptyBoardBB, deltas, true);
+  // init_magics() computes all rook and bishop attacks at startup. Magic
+  // bitboards are used to look up attacks of sliding pieces. As a reference see
+  // chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we
+  // use the so called "fancy" approach.
 
-        maxKey = 1 << (CpuIs64Bit ? 64 - shift[s] : 32 - shift[s]);
+  void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
+                   Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
 
-        for (int key = 0; key < maxKey; key++)
-        {
-            occupancy = submask(m[s].mask, key);
+    int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998,  5731, 95205, 104912, 17020 },
+                             {  728, 10316, 55013, 32803, 12281, 15100,  16645,   255 } };
 
-            index = CpuIs64Bit ? occupancy * mult[s]
-                               : unsigned(occupancy * mult[s] ^ (occupancy >> 32) * (mult[s] >> 32));
+    Bitboard occupancy[4096], reference[4096], edges, b;
+    int i, size;
 
-            attacks[offset + (index >> shift[s])] = sliding_attacks(s, occupancy, deltas, false);
-        }
-        offset += maxKey;
+    // attacks[s] is a pointer to the beginning of the attacks table for square 's'
+    attacks[SQ_A1] = table;
+
+    for (Square s = SQ_A1; s <= SQ_H8; ++s)
+    {
+        // Board edges are not considered in the relevant occupancies
+        edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
+
+        // Given a square 's', the mask is the bitboard of sliding attacks from
+        // 's' computed on an empty board. The index must be big enough to contain
+        // all the attacks for each possible subset of the mask and so is 2 power
+        // the number of 1s of the mask. Hence we deduce the size of the shift to
+        // apply to the 64 or 32 bits word to get the index.
+        masks[s]  = sliding_attack(deltas, s, 0) & ~edges;
+        shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);
+
+        // Use Carry-Rippler trick to enumerate all subsets of masks[s] and
+        // store the corresponding sliding attack bitboard in reference[].
+        b = size = 0;
+        do {
+            occupancy[size] = b;
+            reference[size] = sliding_attack(deltas, s, b);
+
+            if (HasPext)
+                attacks[s][pext(b, masks[s])] = reference[size];
+
+            size++;
+            b = (b - masks[s]) & masks[s];
+        } while (b);
+
+        // Set the offset for the table of the next square. We have individual
+        // table sizes for each square with "Fancy Magic Bitboards".
+        if (s < SQ_H8)
+            attacks[s + 1] = attacks[s] + size;
+
+        if (HasPext)
+            continue;
+
+        PRNG rng(seeds[Is64Bit][rank_of(s)]);
+
+        // Find a magic for square 's' picking up an (almost) random number
+        // until we find the one that passes the verification test.
+        do {
+            do
+                magics[s] = rng.sparse_rand<Bitboard>();
+            while (popcount<Max15>((magics[s] * masks[s]) >> 56) < 6);
+
+            std::memset(attacks[s], 0, size * sizeof(Bitboard));
+
+            // A good magic must map every possible occupancy to an index that
+            // looks up the correct sliding attack in the attacks[s] database.
+            // Note that we build up the database for square 's' as a side
+            // effect of verifying the magic.
+            for (i = 0; i < size; ++i)
+            {
+                Bitboard& attack = attacks[s][index(s, occupancy[i])];
+
+                if (attack && attack != reference[i])
+                    break;
+
+                assert(reference[i]);
+
+                attack = reference[i];
+            }
+        } while (i < size);
     }
   }
 }