Skip to content

Commit

Permalink
Bench: 29925582
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Nov 25, 2023
1 parent 05f856f commit 53dda56
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/bitboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ Bitboard PassedMask[COLOR_NB][64];
Bitboard IsolatedMask[64];


// Helper function that returns a bitboard with the landing square of
// the step, or an empty bitboard if the step would go outside the board
// Returns a bitboard with the landing square of the step,
// or an empty bitboard if the step would go outside the board
INLINE Bitboard LandingSquareBB(const Square sq, const int step) {
const Square to = sq + step;
return (Bitboard)(to <= H8 && Distance(sq, to) <= 2) << (to & H8);
const bool inside = to <= H8 && Distance(sq, to) <= 2;
return inside ? BB(to) : 0;
}

// Helper function that makes a slider attack bitboard
// Makes a slider attack bitboard
static Bitboard MakeSliderAttackBB(const Square sq, const Bitboard occupied, const int steps[]) {

Bitboard attacks = 0;

for (int dir = 0; dir < 4; ++dir) {
Square s = sq;
// Step in the direction until hitting a piece or the edge of the board
while(!(occupied & BB(s)) && LandingSquareBB(s, steps[dir]))
attacks |= BB(s += steps[dir]);
}
Expand All @@ -69,7 +71,6 @@ static void InitNonSliderAttacks() {

int KSteps[8] = { -9, -8, -7, -1, 1, 7, 8, 9 };
int NSteps[8] = { -17,-15,-10, -6, 6, 10, 15, 17 };
int PSteps[COLOR_NB][2] = { { -9, -7 }, { 7, 9 } };

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

Expand All @@ -80,10 +81,10 @@ static void InitNonSliderAttacks() {
}

// Pawns
for (int i = 0; i < 2; ++i) {
PawnAttacks[WHITE][sq] |= LandingSquareBB(sq, PSteps[WHITE][i]);
PawnAttacks[BLACK][sq] |= LandingSquareBB(sq, PSteps[BLACK][i]);
}
PawnAttacks[WHITE][sq] = LandingSquareBB(sq, NORTH + WEST)
| LandingSquareBB(sq, NORTH + EAST);
PawnAttacks[BLACK][sq] = LandingSquareBB(sq, SOUTH + WEST)
| LandingSquareBB(sq, SOUTH + EAST);
}
}

Expand All @@ -109,9 +110,10 @@ static void InitSliderAttacks(Magic m[], Bitboard table[], const int steps[]) {
m[sq].shift = 64 - PopCount(m[sq].mask);
#endif

// Loop through all possible combinations of occupied squares, filling the table
Bitboard occupied = 0;
do {
m[sq].attacks[AttackIndex(sq, occupied, m)] = MakeSliderAttackBB(sq, occupied, steps);
MagicAttacks(sq, occupied, m) = MakeSliderAttackBB(sq, occupied, steps);
occupied = (occupied - m[sq].mask) & m[sq].mask; // Carry rippler
table++;
} while (occupied);
Expand Down
8 changes: 5 additions & 3 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static const uint64_t BishopMagics[64] = {
};
#endif

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

typedef struct {
Bitboard *attacks;
Bitboard mask;
Expand Down Expand Up @@ -187,8 +189,8 @@ INLINE Bitboard AttackBB(PieceType pt, Square sq, Bitboard occupied) {
assert(pt != PAWN);

switch (pt) {
case BISHOP: return BishopTable[sq].attacks[AttackIndex(sq, occupied, BishopTable)];
case ROOK : return RookTable[sq].attacks[AttackIndex(sq, occupied, RookTable)];
case BISHOP: return MagicAttacks(sq, occupied, BishopTable);
case ROOK : return MagicAttacks(sq, occupied, RookTable);
case QUEEN : return AttackBB(ROOK, sq, occupied) | AttackBB(BISHOP, sq, occupied);
default : return PseudoAttacks[pt][sq];
}
Expand All @@ -213,7 +215,7 @@ INLINE Bitboard PawnAttackBB(Color color, Square sq) {

// Returns the combined attack bitboard of all pawns in the given bitboard
INLINE Bitboard PawnBBAttackBB(Bitboard pawns, Color color) {
const Direction up = (color == WHITE ? NORTH : SOUTH);
const Direction up = color == WHITE ? NORTH : SOUTH;
return ShiftBB(pawns, up+WEST) | ShiftBB(pawns, up+EAST);
}

Expand Down
2 changes: 1 addition & 1 deletion src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ bool PositionOk(const Position *pos) {
assert(nonPawnCount[c] == pos->nonPawnCount[c]);
}

assert(pieceBB(ALL) == (colorBB(WHITE) | colorBB(BLACK)));
assert(pieceBB(ALL) == colorBB(WHITE) | colorBB(BLACK));

assert(sideToMove == WHITE || sideToMove == BLACK);

Expand Down
4 changes: 2 additions & 2 deletions src/uci.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ static void Pos(Position *pos, char *str) {
// Parses a 'setoption' and updates settings
static void SetOption(char *str) {

char *optionName = (strstr(str, "name") + 5);
char *optionValue = (strstr(str, "value") + 6);
char *optionName = strstr(str, "name") + 5;
char *optionValue = strstr(str, "value") + 6;

#define OptionNameIs(name) (!strncmp(optionName, name, strlen(name)))
#define BooleanValue (!strncmp(optionValue, "true", 4))
Expand Down

0 comments on commit 53dda56

Please sign in to comment.