mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
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 <mcostalba@gmail.com>
This commit is contained in:
parent
670cee44f0
commit
b1b0c64046
2 changed files with 19 additions and 21 deletions
|
@ -24,9 +24,6 @@
|
||||||
|
|
||||||
// Global bitboards definitions with static storage duration are
|
// Global bitboards definitions with static storage duration are
|
||||||
// automatically set to zero before enter main().
|
// automatically set to zero before enter main().
|
||||||
Bitboard RAttacks[0x19000];
|
|
||||||
Bitboard BAttacks[0x1480];
|
|
||||||
|
|
||||||
Magics RMagics[64];
|
Magics RMagics[64];
|
||||||
Magics BMagics[64];
|
Magics BMagics[64];
|
||||||
|
|
||||||
|
@ -53,6 +50,15 @@ uint8_t BitCount8Bit[256];
|
||||||
|
|
||||||
namespace {
|
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)
|
#if defined(IS_64BIT)
|
||||||
|
|
||||||
const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
|
const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
|
||||||
|
@ -163,9 +169,6 @@ const uint64_t RMult[64] = {
|
||||||
|
|
||||||
#endif // defined(IS_64BIT)
|
#endif // defined(IS_64BIT)
|
||||||
|
|
||||||
CACHE_LINE_ALIGNMENT int BSFTable[64];
|
|
||||||
|
|
||||||
void init_sliding_attacks(Bitboard attacks[], Magics m[], const Bitboard mult[], Square deltas[]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -397,7 +400,7 @@ namespace {
|
||||||
{
|
{
|
||||||
excluded = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
|
excluded = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
|
||||||
|
|
||||||
m[s].offset = offset;
|
m[s].attacks = &attacks[offset];
|
||||||
m[s].mult = mult[s];
|
m[s].mult = mult[s];
|
||||||
m[s].mask = sliding_attacks(s, EmptyBoardBB, deltas, excluded);
|
m[s].mask = sliding_attacks(s, EmptyBoardBB, deltas, excluded);
|
||||||
m[s].shift = (CpuIs64Bit ? 64 : 32) - count_1s<CNT64>(m[s].mask);
|
m[s].shift = (CpuIs64Bit ? 64 : 32) - count_1s<CNT64>(m[s].mask);
|
||||||
|
@ -411,7 +414,7 @@ namespace {
|
||||||
index = CpuIs64Bit ? occupancy * mult[s]
|
index = CpuIs64Bit ? occupancy * mult[s]
|
||||||
: unsigned(occupancy * mult[s] ^ (occupancy >> 32) * (mult[s] >> 32));
|
: 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;
|
offset += maxKey;
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,16 +69,13 @@ extern uint8_t BitCount8Bit[256];
|
||||||
struct Magics {
|
struct Magics {
|
||||||
Bitboard mask;
|
Bitboard mask;
|
||||||
uint64_t mult;
|
uint64_t mult;
|
||||||
uint32_t offset;
|
|
||||||
uint32_t shift;
|
uint32_t shift;
|
||||||
|
Bitboard* attacks;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Magics RMagics[64];
|
extern Magics RMagics[64];
|
||||||
extern Magics BMagics[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
|
/// Functions for testing whether a given bit is set in a bitboard, and for
|
||||||
/// setting and clearing bits.
|
/// 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) {
|
inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
|
||||||
const Magics& m = RMagics[s];
|
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) {
|
inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
|
||||||
const Magics& m = BMagics[s];
|
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)
|
#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) {
|
inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
|
||||||
const Magics& m = RMagics[s];
|
const Magics& m = RMagics[s];
|
||||||
Bitboard b = occ & m.mask;
|
Bitboard b = occ & m.mask;
|
||||||
return RAttacks[m.offset +
|
return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift];
|
||||||
((unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
|
inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
|
||||||
const Magics& m = BMagics[s];
|
const Magics& m = BMagics[s];
|
||||||
Bitboard b = occ & m.mask;
|
Bitboard b = occ & m.mask;
|
||||||
return BAttacks[m.offset +
|
return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift];
|
||||||
((unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue