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 4b62f42 commit 8cbe7a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
32 changes: 16 additions & 16 deletions src/bitboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Bitboard BetweenBB[64][64];
static Bitboard BishopAttacks[5248];
static Bitboard RookAttacks[102400];

Magic BishopTable[64];
Magic RookTable[64];
Magic Magics[64][2];

Bitboard PseudoAttacks[TYPE_NB][64];
Bitboard PawnAttacks[COLOR_NB][64];
Expand All @@ -52,10 +51,14 @@ INLINE Bitboard LandingSquareBB(const Square sq, const int step) {
}

// Makes a slider attack bitboard
static Bitboard MakeSliderAttackBB(const Square sq, const Bitboard occupied, const int steps[]) {
static Bitboard MakeSliderAttackBB(const Square sq, const PieceType pt, const Bitboard occupied) {

Bitboard attacks = 0;

const int BSteps[] = { 7, 9, -7, -9 };
const int RSteps[] = { 8, 1, -8, -1 };
const int *steps = pt == BISHOP ? BSteps : RSteps;

for (int dir = 0; dir < 4; ++dir) {
Square s = sq;
// Step in the direction until hitting a piece or the edge of the board
Expand Down Expand Up @@ -89,32 +92,32 @@ static void InitNonSliderAttacks() {
}

// Initializes slider attack lookups
static void InitSliderAttacks(Magic m[], Bitboard table[], const int steps[]) {
static void InitSliderAttacks(PieceType pt, Bitboard table[]) {

#ifndef USE_PEXT
const uint64_t *magics = steps[0] == 8 ? RookMagics : BishopMagics;
const uint64_t *magics = pt == ROOK ? RookMagics : BishopMagics;
#endif

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

m[sq].attacks = table;
MagicAttacks(sq, pt) = table;

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

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

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

// Loop through all possible combinations of occupied squares, filling the table
Bitboard occupied = 0;
do {
MagicAttacks(sq, occupied, m) = MakeSliderAttackBB(sq, occupied, steps);
occupied = (occupied - m[sq].mask) & m[sq].mask; // Carry rippler
MagicAttack(sq, pt, occupied) = MakeSliderAttackBB(sq, pt, occupied);
occupied = (occupied - MagicMask(sq, pt)) & MagicMask(sq, pt); // Carry rippler
table++;
} while (occupied);
}
Expand All @@ -125,11 +128,8 @@ CONSTR(2) InitBitboards() {

InitNonSliderAttacks();

const int BSteps[4] = { 7, 9, -7, -9 };
const int RSteps[4] = { 8, 1, -8, -1 };

InitSliderAttacks(BishopTable, BishopAttacks, BSteps);
InitSliderAttacks( RookTable, RookAttacks, RSteps);
InitSliderAttacks(BISHOP, BishopAttacks);
InitSliderAttacks(ROOK, RookAttacks);

for (Square sq1 = A1; sq1 <= H8; sq1++)
for (Square sq2 = A1; sq2 <= H8; sq2++)
Expand Down
21 changes: 13 additions & 8 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@
#ifdef USE_PEXT
// Uses the bmi2 pext instruction in place of magic bitboards
#include "x86intrin.h"
#define AttackIndex(sq, occ, table) (_pext_u64(occ, table[sq].mask))
#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)))

#else
// Uses magic bitboards as explained on https://www.chessprogramming.org/Magic_Bitboards
#define AttackIndex(sq, occ, table) (((occ & table[sq].mask) * table[sq].magic) >> table[sq].shift)
#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] = {
0xA180022080400230ull, 0x0040100040022000ull, 0x0080088020001002ull, 0x0080080280841000ull,
Expand Down Expand Up @@ -70,11 +76,11 @@ static const uint64_t BishopMagics[64] = {
};
#endif

#define MagicAttacks(sq, occ, table) (table[sq].attacks[AttackIndex(sq, occ, table)])
#define MagicAttack(sq, pt, occ) (MagicAttacks(sq, pt)[AttackIndex(sq, pt, occ)])

typedef struct {
Bitboard *attacks;
Bitboard mask;
Bitboard *attacks;
#ifndef USE_PEXT
uint64_t magic;
int shift;
Expand Down Expand Up @@ -111,8 +117,7 @@ extern const Bitboard RankBB[RANK_NB];

extern Bitboard BetweenBB[64][64];

extern Magic BishopTable[64];
extern Magic RookTable[64];
extern Magic Magics[64][2];

extern Bitboard PseudoAttacks[TYPE_NB][64];
extern Bitboard PawnAttacks[COLOR_NB][64];
Expand Down Expand Up @@ -189,8 +194,8 @@ INLINE Bitboard AttackBB(PieceType pt, Square sq, Bitboard occupied) {
assert(pt != PAWN);

switch (pt) {
case BISHOP: return MagicAttacks(sq, occupied, BishopTable);
case ROOK : return MagicAttacks(sq, occupied, RookTable);
case BISHOP:
case ROOK : return MagicAttack(sq, pt, occupied);
case QUEEN : return AttackBB(ROOK, sq, occupied) | AttackBB(BISHOP, sq, occupied);
default : return PseudoAttacks[pt][sq];
}
Expand Down

0 comments on commit 8cbe7a2

Please sign in to comment.