mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 08:37:44 +00:00
Reordering magic data
Gather all magic relevant data into a struct. This changes memory layout putting everything necessary for processing a single square in the same memory location thus speeding up access. Original patch by @snicolet No functional change. Closes #1127 Closes #1128
This commit is contained in:
+23
-17
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user