From: Marco Costalba Date: Sun, 5 Jun 2011 13:41:59 +0000 (+0100) Subject: Skip offset calculation in slider attacks X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=b1b0c640462aed199a3665e575f9cb208b8e4687 Skip offset calculation in slider attacks Another small simplification and micro optimization. No functional change in both 32 and 64 bits. Signed-off-by: Marco Costalba --- diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 0d288898..c6ee517d 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -24,9 +24,6 @@ // Global bitboards definitions with static storage duration are // automatically set to zero before enter main(). -Bitboard RAttacks[0x19000]; -Bitboard BAttacks[0x1480]; - Magics RMagics[64]; Magics BMagics[64]; @@ -53,6 +50,15 @@ uint8_t BitCount8Bit[256]; namespace { + CACHE_LINE_ALIGNMENT + + int BSFTable[64]; + + Bitboard RAttacks[0x19000]; + Bitboard BAttacks[0x1480]; + + void init_sliding_attacks(Bitboard attacks[], Magics m[], const Bitboard mult[], Square deltas[]); + #if defined(IS_64BIT) const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL; @@ -163,9 +169,6 @@ const uint64_t RMult[64] = { #endif // defined(IS_64BIT) - CACHE_LINE_ALIGNMENT int BSFTable[64]; - - void init_sliding_attacks(Bitboard attacks[], Magics m[], const Bitboard mult[], Square deltas[]); } @@ -397,10 +400,10 @@ namespace { { excluded = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s)); - m[s].offset = offset; - m[s].mult = mult[s]; - m[s].mask = sliding_attacks(s, EmptyBoardBB, deltas, excluded); - m[s].shift = (CpuIs64Bit ? 64 : 32) - count_1s(m[s].mask); + m[s].attacks = &attacks[offset]; + m[s].mult = mult[s]; + m[s].mask = sliding_attacks(s, EmptyBoardBB, deltas, excluded); + m[s].shift = (CpuIs64Bit ? 64 : 32) - count_1s(m[s].mask); maxKey = 1 << count_1s(m[s].mask); @@ -411,7 +414,7 @@ namespace { index = CpuIs64Bit ? occupancy * mult[s] : unsigned(occupancy * mult[s] ^ (occupancy >> 32) * (mult[s] >> 32)); - attacks[offset + (index >> m[s].shift)] = sliding_attacks(s, occupancy, deltas, EmptyBoardBB); + m[s].attacks[index >> m[s].shift] = sliding_attacks(s, occupancy, deltas, EmptyBoardBB); } offset += maxKey; } diff --git a/src/bitboard.h b/src/bitboard.h index b3a0033c..00f0f576 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -69,16 +69,13 @@ extern uint8_t BitCount8Bit[256]; struct Magics { Bitboard mask; uint64_t mult; - uint32_t offset; uint32_t shift; + Bitboard* attacks; }; extern Magics RMagics[64]; extern Magics BMagics[64]; -extern Bitboard RAttacks[0x19000]; -extern Bitboard BAttacks[0x1480]; - /// Functions for testing whether a given bit is set in a bitboard, and for /// setting and clearing bits. @@ -176,12 +173,12 @@ inline Bitboard in_front_bb(Color c, Square s) { inline Bitboard rook_attacks_bb(Square s, Bitboard occ) { const Magics& m = RMagics[s]; - return RAttacks[m.offset + (((occ & m.mask) * m.mult) >> m.shift)]; + return m.attacks[((occ & m.mask) * m.mult) >> m.shift]; } inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) { const Magics& m = BMagics[s]; - return BAttacks[m.offset + (((occ & m.mask) * m.mult) >> m.shift)]; + return m.attacks[((occ & m.mask) * m.mult) >> m.shift]; } #else // if !defined(IS_64BIT) @@ -189,15 +186,13 @@ inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) { inline Bitboard rook_attacks_bb(Square s, Bitboard occ) { const Magics& m = RMagics[s]; Bitboard b = occ & m.mask; - return RAttacks[m.offset + - ((unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift)]; + return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift]; } inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) { const Magics& m = BMagics[s]; Bitboard b = occ & m.mask; - return BAttacks[m.offset + - ((unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift)]; + return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift]; } #endif