1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 16:53:09 +00:00

Partially revert previous patches

Due to a -2% speed penalty. This patch takes the best
of the previous series without the regression due to
introduction of Magic struct.

Speedup against previous revision is of almost 3% !!!!

No functional change both in 32 and 64 bits.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2011-06-06 09:58:59 +02:00
parent b1b0c64046
commit 6d665b7f78
2 changed files with 48 additions and 45 deletions

View file

@ -24,8 +24,13 @@
// 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().
Magics RMagics[64]; Bitboard RMask[64];
Magics BMagics[64]; Bitboard* RAttacks[64];
int RShift[64];
Bitboard BMask[64];
Bitboard* BAttacks[64];
int BShift[64];
Bitboard SetMaskBB[65]; Bitboard SetMaskBB[65];
Bitboard ClearMaskBB[65]; Bitboard ClearMaskBB[65];
@ -48,20 +53,9 @@ Bitboard QueenPseudoAttacks[64];
uint8_t BitCount8Bit[256]; 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) #if defined(IS_64BIT)
const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL; static const uint64_t DeBruijnMagic = 0x218A392CD3D5DBFULL;
const uint64_t BMult[64] = { const uint64_t BMult[64] = {
0x0440049104032280ULL, 0x1021023C82008040ULL, 0x0404040082000048ULL, 0x0440049104032280ULL, 0x1021023C82008040ULL, 0x0404040082000048ULL,
@ -115,7 +109,7 @@ const uint64_t RMult[64] = {
#else // if !defined(IS_64BIT) #else // if !defined(IS_64BIT)
const uint32_t DeBruijnMagic = 0x783A9B23; static const uint32_t DeBruijnMagic = 0x783A9B23;
const uint64_t BMult[64] = { const uint64_t BMult[64] = {
0x54142844C6A22981ULL, 0x710358A6EA25C19EULL, 0x704F746D63A4A8DCULL, 0x54142844C6A22981ULL, 0x710358A6EA25C19EULL, 0x704F746D63A4A8DCULL,
@ -169,6 +163,17 @@ const uint64_t RMult[64] = {
#endif // defined(IS_64BIT) #endif // defined(IS_64BIT)
namespace {
CACHE_LINE_ALIGNMENT
int BSFTable[64];
Bitboard RAttacksTable[0x19000];
Bitboard BAttacksTable[0x1480];
void init_sliding_attacks(Bitboard attacksTable[], Bitboard* attacks[], Bitboard mask[],
int shift[], const Bitboard mult[], Square deltas[]);
} }
@ -328,8 +333,8 @@ void init_bitboards() {
Square RDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W }; Square RDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W };
Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW }; Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
init_sliding_attacks(RAttacks, RMagics, RMult, RDeltas); init_sliding_attacks(RAttacksTable, RAttacks, RMask, RShift, RMult, RDeltas);
init_sliding_attacks(BAttacks, BMagics, BMult, BDeltas); init_sliding_attacks(BAttacksTable, BAttacks, BMask, BShift, BMult, BDeltas);
for (Square s = SQ_A1; s <= SQ_H8; s++) for (Square s = SQ_A1; s <= SQ_H8; s++)
{ {
@ -391,7 +396,8 @@ namespace {
return attacks; return attacks;
} }
void init_sliding_attacks(Bitboard attacks[], Magics m[], const Bitboard mult[], Square deltas[]) { void init_sliding_attacks(Bitboard attacksTable[], Bitboard* attacks[], Bitboard mask[],
int shift[], const Bitboard mult[], Square deltas[]) {
Bitboard occupancy, index, excluded; Bitboard occupancy, index, excluded;
int maxKey, offset = 0; int maxKey, offset = 0;
@ -400,21 +406,20 @@ 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].attacks = &attacks[offset]; attacks[s] = &attacksTable[offset];
m[s].mult = mult[s]; mask[s] = sliding_attacks(s, EmptyBoardBB, deltas, excluded);
m[s].mask = sliding_attacks(s, EmptyBoardBB, deltas, excluded); shift[s] = (CpuIs64Bit ? 64 : 32) - count_1s<CNT64>(mask[s]);
m[s].shift = (CpuIs64Bit ? 64 : 32) - count_1s<CNT64>(m[s].mask);
maxKey = 1 << count_1s<CNT64>(m[s].mask); maxKey = 1 << count_1s<CNT64>(mask[s]);
for (int key = 0; key < maxKey; key++) for (int key = 0; key < maxKey; key++)
{ {
occupancy = submask(m[s].mask, key); occupancy = submask(mask[s], key);
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));
m[s].attacks[index >> m[s].shift] = sliding_attacks(s, occupancy, deltas, EmptyBoardBB); attacks[s][index >> shift[s]] = sliding_attacks(s, occupancy, deltas, EmptyBoardBB);
} }
offset += maxKey; offset += maxKey;
} }

View file

@ -60,22 +60,22 @@ extern Bitboard SquaresInFrontMask[2][64];
extern Bitboard PassedPawnMask[2][64]; extern Bitboard PassedPawnMask[2][64];
extern Bitboard AttackSpanMask[2][64]; extern Bitboard AttackSpanMask[2][64];
extern const uint64_t RMult[64];
extern int RShift[64];
extern Bitboard RMask[64];
extern Bitboard* RAttacks[64];
extern const uint64_t BMult[64];
extern int BShift[64];
extern Bitboard BMask[64];
extern Bitboard* BAttacks[64];
extern Bitboard BishopPseudoAttacks[64]; extern Bitboard BishopPseudoAttacks[64];
extern Bitboard RookPseudoAttacks[64]; extern Bitboard RookPseudoAttacks[64];
extern Bitboard QueenPseudoAttacks[64]; extern Bitboard QueenPseudoAttacks[64];
extern uint8_t BitCount8Bit[256]; extern uint8_t BitCount8Bit[256];
struct Magics {
Bitboard mask;
uint64_t mult;
uint32_t shift;
Bitboard* attacks;
};
extern Magics RMagics[64];
extern Magics BMagics[64];
/// 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.
@ -172,27 +172,25 @@ inline Bitboard in_front_bb(Color c, Square s) {
#if defined(IS_64BIT) #if defined(IS_64BIT)
inline Bitboard rook_attacks_bb(Square s, Bitboard occ) { inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
const Magics& m = RMagics[s]; return RAttacks[s][((occ & RMask[s]) * RMult[s]) >> RShift[s]];
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]; return BAttacks[s][((occ & BMask[s]) * BMult[s]) >> BShift[s]];
return m.attacks[((occ & m.mask) * m.mult) >> m.shift];
} }
#else // if !defined(IS_64BIT) #else // if !defined(IS_64BIT)
inline Bitboard rook_attacks_bb(Square s, Bitboard occ) { inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
const Magics& m = RMagics[s]; Bitboard b = occ & RMask[s];
Bitboard b = occ & m.mask; return RAttacks[s]
return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift]; [unsigned(int(b) * int(RMult[s]) ^ int(b >> 32) * int(RMult[s] >> 32)) >> RShift[s]];
} }
inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) { inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
const Magics& m = BMagics[s]; Bitboard b = occ & BMask[s];
Bitboard b = occ & m.mask; return BAttacks[s]
return m.attacks[(unsigned(b) * unsigned(m.mult) ^ unsigned(b >> 32) * unsigned(m.mult >> 32)) >> m.shift]; [unsigned(int(b) * int(BMult[s]) ^ int(b >> 32) * int(BMult[s] >> 32)) >> BShift[s]];
} }
#endif #endif