]> git.sesse.net Git - stockfish/blobdiff - src/bitboard.h
Reordering magic data
[stockfish] / src / bitboard.h
index c957a40fe23d61296a0e094bf36bd44581cda5d6..3ea92bdf2e56400b273cafaf4f1689bc12a5de7e 100644 (file)
@@ -209,41 +209,47 @@ template<> inline int distance<File>(Square x, Square y) { return distance(file_
 template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
 
 
+/// Magic holds all magic relevant data for a single square
+struct Magic {
+
+    Bitboard  mask;
+    Bitboard  magic;
+    Bitboard* attacks;
+    unsigned  shift;
+};
+
 /// attacks_bb() returns a bitboard representing all the squares attacked by a
 /// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index()
 /// looks up the index using the 'magic bitboards' approach.
 template<PieceType Pt>
 inline unsigned magic_index(Square s, Bitboard occupied) {
 
-  extern Bitboard RookMasks[SQUARE_NB];
-  extern Bitboard RookMagics[SQUARE_NB];
-  extern unsigned RookShifts[SQUARE_NB];
-  extern Bitboard BishopMasks[SQUARE_NB];
-  extern Bitboard BishopMagics[SQUARE_NB];
-  extern unsigned BishopShifts[SQUARE_NB];
+  extern Magic RookMagics[SQUARE_NB];
+  extern Magic BishopMagics[SQUARE_NB];
 
-  Bitboard* const Masks  = Pt == ROOK ? RookMasks  : BishopMasks;
-  Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics;
-  unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts;
+  const Magic* Magics = Pt == ROOK ? RookMagics : BishopMagics;
+  Bitboard mask  = Magics[s].mask;
+  Bitboard magic = Magics[s].magic;
+  unsigned shift = Magics[s].shift;
 
   if (HasPext)
-      return unsigned(pext(occupied, Masks[s]));
+      return unsigned(pext(occupied, mask));
 
   if (Is64Bit)
-      return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]);
+      return unsigned(((occupied & mask) * magic) >> shift);
 
-  unsigned lo = unsigned(occupied) & unsigned(Masks[s]);
-  unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32);
-  return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
+  unsigned lo = unsigned(occupied) & unsigned(mask);
+  unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
+  return  (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
 }
 
 template<PieceType Pt>
 inline Bitboard attacks_bb(Square s, Bitboard occupied) {
 
-  extern Bitboard* RookAttacks[SQUARE_NB];
-  extern Bitboard* BishopAttacks[SQUARE_NB];
+    extern Magic RookMagics[SQUARE_NB];
+    extern Magic BishopMagics[SQUARE_NB];
 
-  return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)];
+    return (Pt == ROOK ? RookMagics : BishopMagics)[s].attacks[magic_index<Pt>(s, occupied)];
 }
 
 inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {