Skip to content

Commit

Permalink
Bench: 22158564
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Nov 13, 2024
1 parent 8cbe7a2 commit 51f948c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/bitboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,25 @@ static void InitSliderAttacks(PieceType pt, Bitboard table[]) {

for (Square sq = A1; sq <= H8; ++sq) {

MagicAttacks(sq, pt) = table;
Magic *m = &Magics[sq][pt - BISHOP];
(*m).attacks = table;

// Construct the mask
Bitboard edges = ((rank1BB | rank8BB) & ~RankBB[RankOf(sq)])
| ((fileABB | fileHBB) & ~FileBB[FileOf(sq)]);

MagicMask(sq, pt) = MakeSliderAttackBB(sq, pt, 0) & ~edges;
(*m).mask = MakeSliderAttackBB(sq, pt, 0) & ~edges;

#ifndef USE_PEXT
MagicMagic(sq, pt) = magics[sq];
MagicShift(sq, pt) = 64 - PopCount(MagicMask(sq, pt));
m.magic = magics[sq];
m.shift = 64 - PopCount(m.mask);
#endif

// Loop through all possible combinations of occupied squares, filling the table
Bitboard occupied = 0;
do {
MagicAttack(sq, pt, occupied) = MakeSliderAttackBB(sq, pt, occupied);
occupied = (occupied - MagicMask(sq, pt)) & MagicMask(sq, pt); // Carry rippler
occupied = (occupied - (*m).mask) & (*m).mask; // Carry rippler
table++;
} while (occupied);
}
Expand Down
10 changes: 2 additions & 8 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@
#ifdef USE_PEXT
// Uses the bmi2 pext instruction in place of magic bitboards
#include "x86intrin.h"
#define MagicMask(sq, pt) (Magics[sq][pt - BISHOP].mask)
#define MagicAttacks(sq, pt) (Magics[sq][pt - BISHOP].attacks)
#define AttackIndex(sq, pt, occ) (_pext_u64(occ, MagicMask(sq, pt)))
#define AttackIndex(sq, pt, occ) (_pext_u64(occ, Magics[sq][pt - BISHOP].mask))

#else
// Uses magic bitboards as explained on https://www.chessprogramming.org/Magic_Bitboards
#define MagicMask(sq, pt) (Magics[sq][pt - BISHOP].mask)
#define MagicAttacks(sq, pt) (Magics[sq][pt - BISHOP].attacks)
#define MagicShift(sq, pt) (Magics[sq][pt - BISHOP].shift)
#define MagicMagic(sq, pt) (Magics[sq][pt - BISHOP].magic)
#define AttackIndex(sq, pt, occ) (((occ & MagicMask(sq, pt)) * MagicMagic(sq, pt)) >> MagicShift(sq, pt)

static const uint64_t RookMagics[64] = {
Expand Down Expand Up @@ -76,7 +70,7 @@ static const uint64_t BishopMagics[64] = {
};
#endif

#define MagicAttack(sq, pt, occ) (MagicAttacks(sq, pt)[AttackIndex(sq, pt, occ)])
#define MagicAttack(sq, pt, occ) (Magics[sq][pt - BISHOP].attacks[AttackIndex(sq, pt, occ)])

typedef struct {
Bitboard mask;
Expand Down

0 comments on commit 51f948c

Please sign in to comment.