]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.cpp
Avoid to calculate reduction for each move
[stockfish] / src / bitboard.cpp
index e4fcb7ce438de974b17303135427868b441ad002..dd8023e9bd5770eef292f87301750dc55b1ccbcf 100644 (file)
@@ -1,13 +1,14 @@
 /*
-  Glaurung, a UCI chess playing engine.
-  Copyright (C) 2004-2008 Tord Romstad
+  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+  Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
+  Copyright (C) 2008-2009 Marco Costalba
 
-  Glaurung is free software: you can redistribute it and/or modify
+  Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
-  Glaurung is distributed in the hope that it will be useful,
+  Stockfish is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 #include <iostream>
 
 #include "bitboard.h"
+#include "bitcount.h"
 #include "direction.h"
 
 
-////
-//// Constants and variables
-////
-
-const Bitboard SquaresByColorBB[2] = {BlackSquaresBB, WhiteSquaresBB};
-
-const Bitboard FileBB[8] = {
-  FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
-};
-
-const Bitboard NeighboringFilesBB[8] = {
-  FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
-  FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
-};
-
-const Bitboard ThisAndNeighboringFilesBB[8] = {
-  FileABB|FileBBB, FileABB|FileBBB|FileCBB,
-  FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
-  FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
-  FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
-};
+#if defined(IS_64BIT)
 
-const Bitboard RankBB[8] = {
-  Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
-};
-
-const Bitboard RelativeRankBB[2][8] = {
-  {
-    Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
-  },
-  {
-    Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB
-  }
-};
-
-const Bitboard InFrontBB[2][8] = {
-  {
-    Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
-    Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
-    Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
-    Rank5BB | Rank6BB | Rank7BB | Rank8BB,
-    Rank6BB | Rank7BB | Rank8BB,
-    Rank7BB | Rank8BB,
-    Rank8BB,
-    EmptyBoardBB
-  },
-  {
-    EmptyBoardBB,
-    Rank1BB,
-    Rank2BB | Rank1BB,
-    Rank3BB | Rank2BB | Rank1BB,
-    Rank4BB | Rank3BB | Rank2BB | Rank1BB,
-    Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
-    Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
-    Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
-  }
-};
-
-#if defined(USE_COMPACT_ROOK_ATTACKS)
-
-Bitboard RankAttacks[8][64], FileAttacks[8][64];
-
-#elif defined(USE_32BIT_ATTACKS)
-
-const uint64_t RMult[64] = {
-  0xd7445cdec88002c0ULL, 0xd0a505c1f2001722ULL, 0xe065d1c896002182ULL,
-  0x9a8c41e75a000892ULL, 0x8900b10c89002aa8ULL, 0x9b28d1c1d60005a2ULL,
-  0x15d6c88de002d9aULL, 0xb1dbfc802e8016a9ULL, 0x149a1042d9d60029ULL,
-  0xb9c08050599e002fULL, 0x132208c3af300403ULL, 0xc1000ce2e9c50070ULL,
-  0x9d9aa13c99020012ULL, 0xb6b078daf71e0046ULL, 0x9d880182fb6e002eULL,
-  0x52889f467e850037ULL, 0xda6dc008d19a8480ULL, 0x468286034f902420ULL,
-  0x7140ac09dc54c020ULL, 0xd76ffffa39548808ULL, 0xea901c4141500808ULL,
-  0xc91004093f953a02ULL, 0x2882afa8f6bb402ULL, 0xaebe335692442c01ULL,
-  0xe904a22079fb91eULL, 0x13a514851055f606ULL, 0x76c782018c8fe632ULL,
-  0x1dc012a9d116da06ULL, 0x3c9e0037264fffa6ULL, 0x2036002853c6e4a2ULL,
-  0xe3fe08500afb47d4ULL, 0xf38af25c86b025c2ULL, 0xc0800e2182cf9a40ULL,
-  0x72002480d1f60673ULL, 0x2500200bae6e9b53ULL, 0xc60018c1eefca252ULL,
-  0x600590473e3608aULL, 0x46002c4ab3fe51b2ULL, 0xa200011486bcc8d2ULL,
-  0xb680078095784c63ULL, 0x2742002639bf11aeULL, 0xc7d60021a5bdb142ULL,
-  0xc8c04016bb83d820ULL, 0xbd520028123b4842ULL, 0x9d1600344ac2a832ULL,
-  0x6a808005631c8a05ULL, 0x604600a148d5389aULL, 0xe2e40103d40dea65ULL,
-  0x945b5a0087c62a81ULL, 0x12dc200cd82d28eULL, 0x2431c600b5f9ef76ULL,
-  0xfb142a006a9b314aULL, 0x6870e00a1c97d62ULL, 0x2a9db2004a2689a2ULL,
-  0xd3594600caf5d1a2ULL, 0xee0e4900439344a7ULL, 0x89c4d266ca25007aULL,
-  0x3e0013a2743f97e3ULL, 0x180e31a0431378aULL, 0x3a9e465a4d42a512ULL,
-  0x98d0a11a0c0d9cc2ULL, 0x8e711c1aba19b01eULL, 0x8dcdc836dd201142ULL,
-  0x5ac08a4735370479ULL,
-};
-
-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
+const uint64_t BMult[64] = {
+  0x440049104032280ULL, 0x1021023c82008040ULL, 0x404040082000048ULL,
+  0x48c4440084048090ULL, 0x2801104026490000ULL, 0x4100880442040800ULL,
+  0x181011002e06040ULL, 0x9101004104200e00ULL, 0x1240848848310401ULL,
+  0x2000142828050024ULL, 0x1004024d5000ULL, 0x102044400800200ULL,
+  0x8108108820112000ULL, 0xa880818210c00046ULL, 0x4008008801082000ULL,
+  0x60882404049400ULL, 0x104402004240810ULL, 0xa002084250200ULL,
+  0x100b0880801100ULL, 0x4080201220101ULL, 0x44008080a00000ULL,
+  0x202200842000ULL, 0x5006004882d00808ULL, 0x200045080802ULL,
+  0x86100020200601ULL, 0xa802080a20112c02ULL, 0x80411218080900ULL,
+  0x200a0880080a0ULL, 0x9a01010000104000ULL, 0x28008003100080ULL,
+  0x211021004480417ULL, 0x401004188220806ULL, 0x825051400c2006ULL,
+  0x140c0210943000ULL, 0x242800300080ULL, 0xc2208120080200ULL,
+  0x2430008200002200ULL, 0x1010100112008040ULL, 0x8141050100020842ULL,
+  0x822081014405ULL, 0x800c049e40400804ULL, 0x4a0404028a000820ULL,
+  0x22060201041200ULL, 0x360904200840801ULL, 0x881a08208800400ULL,
+  0x60202c00400420ULL, 0x1204440086061400ULL, 0x8184042804040ULL,
+  0x64040315300400ULL, 0xc01008801090a00ULL, 0x808010401140c00ULL,
+  0x4004830c2020040ULL, 0x80005002020054ULL, 0x40000c14481a0490ULL,
+  0x10500101042048ULL, 0x1010100200424000ULL, 0x640901901040ULL,
+  0xa0201014840ULL, 0x840082aa011002ULL, 0x10010840084240aULL,
+  0x420400810420608ULL, 0x8d40230408102100ULL, 0x4a00200612222409ULL,
+  0xa08520292120600ULL
 };
 
-#else // if defined(USE_32BIT_ATTACKS)
-
 const uint64_t RMult[64] = {
   0xa8002c000108020ULL, 0x4440200140003000ULL, 0x8080200010011880ULL,
   0x380180080141000ULL, 0x1a00060008211044ULL, 0x410001000a0c0008ULL,
@@ -150,6 +81,13 @@ const uint64_t RMult[64] = {
   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,
@@ -157,15 +95,7 @@ const int RShift[64] = {
   53, 54, 54, 54, 54, 54, 54, 53, 52, 53, 53, 53, 53, 53, 53, 52
 };
 
-#endif // defined(USE_32BIT_ATTACKS)
-
-#if !defined(USE_COMPACT_ROOK_ATTACKS)
-Bitboard RMask[64];
-int RAttackIndex[64];
-Bitboard RAttacks[0x19000];
-#endif
-
-#if defined(USE_32BIT_ATTACKS)
+#else // if !defined(IS_64BIT)
 
 const uint64_t BMult[64] = {
   0x54142844c6a22981ULL, 0x710358a6ea25c19eULL, 0x704f746d63a4a8dcULL,
@@ -192,6 +122,31 @@ const uint64_t BMult[64] = {
   0xdc01f6dca4bebbdcULL,
 };
 
+const uint64_t RMult[64] = {
+  0xd7445cdec88002c0ULL, 0xd0a505c1f2001722ULL, 0xe065d1c896002182ULL,
+  0x9a8c41e75a000892ULL, 0x8900b10c89002aa8ULL, 0x9b28d1c1d60005a2ULL,
+  0x15d6c88de002d9aULL, 0xb1dbfc802e8016a9ULL, 0x149a1042d9d60029ULL,
+  0xb9c08050599e002fULL, 0x132208c3af300403ULL, 0xc1000ce2e9c50070ULL,
+  0x9d9aa13c99020012ULL, 0xb6b078daf71e0046ULL, 0x9d880182fb6e002eULL,
+  0x52889f467e850037ULL, 0xda6dc008d19a8480ULL, 0x468286034f902420ULL,
+  0x7140ac09dc54c020ULL, 0xd76ffffa39548808ULL, 0xea901c4141500808ULL,
+  0xc91004093f953a02ULL, 0x2882afa8f6bb402ULL, 0xaebe335692442c01ULL,
+  0xe904a22079fb91eULL, 0x13a514851055f606ULL, 0x76c782018c8fe632ULL,
+  0x1dc012a9d116da06ULL, 0x3c9e0037264fffa6ULL, 0x2036002853c6e4a2ULL,
+  0xe3fe08500afb47d4ULL, 0xf38af25c86b025c2ULL, 0xc0800e2182cf9a40ULL,
+  0x72002480d1f60673ULL, 0x2500200bae6e9b53ULL, 0xc60018c1eefca252ULL,
+  0x600590473e3608aULL, 0x46002c4ab3fe51b2ULL, 0xa200011486bcc8d2ULL,
+  0xb680078095784c63ULL, 0x2742002639bf11aeULL, 0xc7d60021a5bdb142ULL,
+  0xc8c04016bb83d820ULL, 0xbd520028123b4842ULL, 0x9d1600344ac2a832ULL,
+  0x6a808005631c8a05ULL, 0x604600a148d5389aULL, 0xe2e40103d40dea65ULL,
+  0x945b5a0087c62a81ULL, 0x12dc200cd82d28eULL, 0x2431c600b5f9ef76ULL,
+  0xfb142a006a9b314aULL, 0x6870e00a1c97d62ULL, 0x2a9db2004a2689a2ULL,
+  0xd3594600caf5d1a2ULL, 0xee0e4900439344a7ULL, 0x89c4d266ca25007aULL,
+  0x3e0013a2743f97e3ULL, 0x180e31a0431378aULL, 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,
@@ -199,48 +154,73 @@ const int BShift[64] = {
   27, 27, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 26
 };
 
-#else // if defined(USE_32BIT_ATTACKS)
+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
+};
 
-const uint64_t BMult[64] = {
-  0x440049104032280ULL, 0x1021023c82008040ULL, 0x404040082000048ULL,
-  0x48c4440084048090ULL, 0x2801104026490000ULL, 0x4100880442040800ULL,
-  0x181011002e06040ULL, 0x9101004104200e00ULL, 0x1240848848310401ULL,
-  0x2000142828050024ULL, 0x1004024d5000ULL, 0x102044400800200ULL,
-  0x8108108820112000ULL, 0xa880818210c00046ULL, 0x4008008801082000ULL,
-  0x60882404049400ULL, 0x104402004240810ULL, 0xa002084250200ULL,
-  0x100b0880801100ULL, 0x4080201220101ULL, 0x44008080a00000ULL,
-  0x202200842000ULL, 0x5006004882d00808ULL, 0x200045080802ULL,
-  0x86100020200601ULL, 0xa802080a20112c02ULL, 0x80411218080900ULL,
-  0x200a0880080a0ULL, 0x9a01010000104000ULL, 0x28008003100080ULL,
-  0x211021004480417ULL, 0x401004188220806ULL, 0x825051400c2006ULL,
-  0x140c0210943000ULL, 0x242800300080ULL, 0xc2208120080200ULL,
-  0x2430008200002200ULL, 0x1010100112008040ULL, 0x8141050100020842ULL,
-  0x822081014405ULL, 0x800c049e40400804ULL, 0x4a0404028a000820ULL,
-  0x22060201041200ULL, 0x360904200840801ULL, 0x881a08208800400ULL,
-  0x60202c00400420ULL, 0x1204440086061400ULL, 0x8184042804040ULL,
-  0x64040315300400ULL, 0xc01008801090a00ULL, 0x808010401140c00ULL,
-  0x4004830c2020040ULL, 0x80005002020054ULL, 0x40000c14481a0490ULL,
-  0x10500101042048ULL, 0x1010100200424000ULL, 0x640901901040ULL,
-  0xa0201014840ULL, 0x840082aa011002ULL, 0x10010840084240aULL,
-  0x420400810420608ULL, 0x8d40230408102100ULL, 0x4a00200612222409ULL,
-  0xa08520292120600ULL
+#endif // defined(IS_64BIT)
+
+const Bitboard SquaresByColorBB[2] = { BlackSquaresBB, WhiteSquaresBB };
+
+const Bitboard FileBB[8] = {
+  FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
 };
 
-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 Bitboard NeighboringFilesBB[8] = {
+  FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
+  FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
+};
+
+const Bitboard ThisAndNeighboringFilesBB[8] = {
+  FileABB|FileBBB, FileABB|FileBBB|FileCBB,
+  FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
+  FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
+  FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
+};
+
+const Bitboard RankBB[8] = {
+  Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
+};
+
+const Bitboard RelativeRankBB[2][8] = {
+  { Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB },
+  { Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB }
+};
+
+const Bitboard InFrontBB[2][8] = {
+  { Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
+    Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
+    Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
+    Rank5BB | Rank6BB | Rank7BB | Rank8BB,
+    Rank6BB | Rank7BB | Rank8BB,
+    Rank7BB | Rank8BB,
+    Rank8BB,
+    EmptyBoardBB
+  },
+  { EmptyBoardBB,
+    Rank1BB,
+    Rank2BB | Rank1BB,
+    Rank3BB | Rank2BB | Rank1BB,
+    Rank4BB | Rank3BB | Rank2BB | Rank1BB,
+    Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
+    Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
+    Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
+  }
 };
 
-#endif // defined(USE_32BIT_ATTACKS)
+Bitboard RMask[64];
+int RAttackIndex[64];
+Bitboard RAttacks[0x19000];
 
 Bitboard BMask[64];
 int BAttackIndex[64];
 Bitboard BAttacks[0x1480];
 
-Bitboard SetMaskBB[64];
-Bitboard ClearMaskBB[64];
+Bitboard SetMaskBB[65];
+Bitboard ClearMaskBB[65];
 
 Bitboard StepAttackBB[16][64];
 Bitboard RayBB[64][8];
@@ -253,12 +233,15 @@ Bitboard BishopPseudoAttacks[64];
 Bitboard RookPseudoAttacks[64];
 Bitboard QueenPseudoAttacks[64];
 
+uint8_t BitCount8Bit[256];
+
 
 ////
 //// Local definitions
 ////
 
 namespace {
+
   void init_masks();
   void init_ray_bitboards();
   void init_attacks();
@@ -271,10 +254,7 @@ namespace {
                             const int shift[2], const Bitboard mult[],
                             int deltas[][2]);
   void init_pseudo_attacks();
-#if defined(USE_COMPACT_ROOK_ATTACKS)
-  void init_file_and_rank_attacks();
-#endif
-};
+}
 
 
 ////
@@ -305,96 +285,123 @@ void init_bitboards() {
   init_ray_bitboards();
   init_attacks();
   init_between_bitboards();
-#if defined(USE_COMPACT_ROOK_ATTACKS)
-  init_file_and_rank_attacks();
-#else
-  init_sliding_attacks(RAttacks, RAttackIndex, RMask, RShift,
-                       RMult, rookDeltas);
-#endif
-  init_sliding_attacks(BAttacks, BAttackIndex, BMask, BShift,
-                       BMult, bishopDeltas);
+  init_sliding_attacks(RAttacks, RAttackIndex, RMask, RShift, RMult, rookDeltas);
+  init_sliding_attacks(BAttacks, BAttackIndex, BMask, BShift, BMult, bishopDeltas);
   init_pseudo_attacks();
 }
 
 
-#if defined(USE_FOLDED_BITSCAN)
+/// 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)
+
+static CACHE_LINE_ALIGNMENT
+const int BitTable[64] = {
+  0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40, 5, 17, 26, 38, 15,
+  46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57, 63, 6, 12, 18, 24, 27, 33, 39,
+  16, 37, 45, 47, 30, 53, 49, 56, 62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43,
+  51, 60, 42, 59, 58
+};
+
+Square first_1(Bitboard b) {
+  return Square(BitTable[((b & -b) * 0x218a392cd3d5dbfULL) >> 58]);
+}
+
+Square pop_1st_bit(Bitboard* b) {
+  Bitboard bb = *b;
+  *b &= (*b - 1);
+  return Square(BitTable[((bb & -bb) * 0x218a392cd3d5dbfULL) >> 58]);
+}
+
+#elif !defined(USE_BSFQ)
 
-static const int BitTable[64] = {
+static CACHE_LINE_ALIGNMENT
+const int BitTable[64] = {
   63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2,
   51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52,
   26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28,
   58, 20, 37, 17, 36, 8
 };
 
-
-/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
-
 Square first_1(Bitboard b) {
   b ^= (b - 1);
   uint32_t fold = int(b) ^ int(b >> 32);
   return Square(BitTable[(fold * 0x783a9b23) >> 26]);
 }
 
+// Use type-punning
+union b_union {
 
-/// pop_1st_bit() finds and clears the least significant nonzero bit in a
-/// nonzero bitboard.
-
-#if defined(USE_32BIT_ATTACKS)
-
-Square pop_1st_bit(Bitboard *bb) {
-
-  uint32_t  a = uint32_t(*bb);
-  uint32_t* ptr = a ? (uint32_t*)bb : (uint32_t*)bb + 1; // Little endian only?
-  uint32_t  b = a ? a : *ptr;
-  uint32_t  c = ~(b ^ (b - 1));
-
-  *ptr = b & c; // clear the bit
-  if (a)
-     c = ~c;
-
-  return Square(BitTable[(c * 0x783a9b23) >> 26]);
-}
-
+    Bitboard b;
+    struct {
+#if defined (BIGENDIAN)
+        uint32_t h;
+        uint32_t l;
 #else
-
-Square pop_1st_bit(Bitboard *b) {
-  Bitboard bb = *b ^ (*b - 1);
-  uint32_t fold = int(bb) ^ int(bb >> 32);
-  *b &= (*b - 1);
-  return Square(BitTable[(fold * 0x783a9b23) >> 26]);
-}
-
+        uint32_t l;
+        uint32_t h;
 #endif
-
-#else
-
-static const int BitTable[64] = {
-  0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40, 5, 17, 26, 38, 15,
-  46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57, 63, 6, 12, 18, 24, 27, 33, 39,
-  16, 37, 45, 47, 30, 53, 49, 56, 62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43,
-  51, 60, 42, 59, 58
+    } dw;
 };
 
+Square pop_1st_bit(Bitboard* bb) {
 
-/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
+   b_union u;
+   Square ret;
 
-Square first_1(Bitboard b) {
-  return Square(BitTable[((b & -b) * 0x218a392cd3d5dbfULL) >> 58]);
+   u.b = *bb;
+
+   if (u.dw.l)
+   {
+       ret = Square(BitTable[((u.dw.l ^ (u.dw.l - 1)) * 0x783a9b23) >> 26]);
+       u.dw.l &= (u.dw.l - 1);
+       *bb = u.b;
+       return ret;
+   }
+   ret = Square(BitTable[((~(u.dw.h ^ (u.dw.h - 1))) * 0x783a9b23) >> 26]);
+   u.dw.h &= (u.dw.h - 1);
+   *bb = u.b;
+   return ret;
 }
 
+#endif
 
-/// pop_1st_bit() finds and clears the least significant nonzero bit in a
-/// nonzero bitboard.
+// Optimized bitScanReverse32() implementation by Pascal Georges. Note
+// that first bit is 1, this allow to differentiate between 0 and 1.
+static CACHE_LINE_ALIGNMENT
+const char MsbTable[256] = {
+  0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
+  5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
+};
 
-Square pop_1st_bit(Bitboard *b) {
-  Bitboard bb = *b;
-  *b &= (*b - 1);
-  return Square(BitTable[((bb & -bb) * 0x218a392cd3d5dbfULL) >> 58]);
+int bitScanReverse32(uint32_t b)
+{
+   int result = 0;
+
+   if (b > 0xFFFF)
+   {
+      b >>= 16;
+      result += 16;
+   }
+   if (b > 0xFF)
+   {
+      b >>= 8;
+      result += 8;
+   }
+   return result + MsbTable[b];
 }
 
-#endif // defined(USE_FOLDED_BITSCAN)
-
-
 namespace {
 
   // All functions below are used to precompute various bitboards during
@@ -403,6 +410,8 @@ namespace {
   // be necessary to touch any of them.
 
   void init_masks() {
+    SetMaskBB[SQ_NONE] = 0ULL;
+    ClearMaskBB[SQ_NONE] = ~SetMaskBB[SQ_NONE];
     for(Square s = SQ_A1; s <= SQ_H8; s++) {
       SetMaskBB[s] = (1ULL << s);
       ClearMaskBB[s] = ~SetMaskBB[s];
@@ -413,12 +422,15 @@ namespace {
           in_front_bb(c, s) & this_and_neighboring_files_bb(s);
         OutpostMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(s);
       }
+
+    for (Bitboard b = 0ULL; b < 256ULL; b++)
+        BitCount8Bit[b] = (uint8_t)count_1s(b);
   }
 
 
   void init_ray_bitboards() {
     int d[8] = {1, -1, 16, -16, 17, -17, 15, -15};
-    for(int i = 0; i < 128; i = i + 9 & ~8) {
+    for(int i = 0; i < 128; i = (i + 9) & ~8) {
       for(int j = 0; j < 8; j++) {
         RayBB[(i&7)|((i>>4)<<3)][j] = EmptyBoardBB;
         for(int k = i + d[j]; (k & 0x88) == 0; k += d[j])
@@ -504,19 +516,26 @@ namespace {
     for(i = 0; i < 64; i++) {
       attackIndex[i] = index;
       mask[i] = sliding_attacks(i, 0ULL, 4, deltas, 1, 6, 1, 6);
+
+#if defined(IS_64BIT)
       j = (1 << (64 - shift[i]));
+#else
+      j = (1 << (32 - shift[i]));
+#endif
+
       for(k = 0; k < j; k++) {
-#if defined(USE_32BIT_ATTACKS)
+
+#if defined(IS_64BIT)
+        b = index_to_bitboard(k, mask[i]);
+        attacks[index + ((b * mult[i]) >> shift[i])] =
+          sliding_attacks(i, b, 4, deltas);
+#else
         b = index_to_bitboard(k, mask[i]);
         attacks[index +
                  (unsigned(int(b) * int(mult[i]) ^
                            int(b >> 32) * int(mult[i] >> 32))
                   >> shift[i])] =
           sliding_attacks(i, b, 4, deltas);
-#else
-        b = index_to_bitboard(k, mask[i]);
-        attacks[index + ((b * mult[i]) >> shift[i])] =
-          sliding_attacks(i, b, 4, deltas);
 #endif
       }
       index += j;
@@ -533,29 +552,4 @@ namespace {
     }
   }
 
-#if defined(USE_COMPACT_ROOK_ATTACKS)
-  void init_file_and_rank_attacks() {
-    int i, j, k, l, m, s;
-    Bitboard b1, b2;
-    for(i = 0; i < 64; i++) {
-
-      for(m = 0; m <= 1; m++) {
-        b1 = 0ULL;
-        for(j = 0; j < 6; j++) if(i & (1<<j)) b1 |= (1ULL << ((j+1)*(1+m*7)));
-        for(j = 0; j < 8; j++) {
-          b2 = 0ULL;
-          for(k = 0, s = 1; k < 2; k++, s *= -1) {
-            for(l = j+s; l >= 0 && l <= 7; l += s) {
-              b2 |= (m? RankBB[l] : FileBB[l]);
-              if(b1 & (1ULL << (l*(1+m*7)))) break;
-            }
-          }
-          if(m) FileAttacks[j][(b1*0xd6e8802041d0c441ULL) >> 58] = b2;
-          else RankAttacks[j][i] = b2;
-        }
-      }
-    }
-  }
-#endif // defined(USE_COMPACT_ROOK_ATTACKS)
-
 }