Skip to content

Commit

Permalink
Align pinmask handling with checkmask (#295)
Browse files Browse the repository at this point in the history
Bench: 6645600
  • Loading branch information
PGG106 authored Jan 3, 2024
1 parent 4a081e7 commit 9633c74
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 48 deletions.
36 changes: 35 additions & 1 deletion src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ void ParseFen(const std::string& command, S_Board* pos) {
else
pos->checkMask = fullCheckmask;

UpdatePinMasks(pos, pos->side);

// Update nnue accumulator to reflect board state
Accumulate(pos->accumStack[0], pos);
pos->accumStackHead = 1;
Expand Down Expand Up @@ -402,8 +404,8 @@ int KingSQ(const S_Board* pos, const int c) {
}

Bitboard GetCheckersBB(const S_Board* pos, const int side) {
const int kingSquare = KingSQ(pos, side);
Bitboard Occ = pos->Occupancy(BOTH);
int kingSquare = KingSQ(pos, side);
// Bitboard of pawns that attack the king square
const Bitboard pawn_mask = pos->GetPieceColorBB(PAWN, side ^ 1) & pawn_attacks[side][kingSquare];
// Bitboard of knights that attack the king square
Expand Down Expand Up @@ -439,6 +441,38 @@ Bitboard GetCheckersBB(const S_Board* pos, const int side) {
return checkers;
}

void UpdatePinMasks(S_Board* pos, const int side) {
const int kingSquare = KingSQ(pos, side);
Bitboard them = pos->Enemy();
// Bitboard of bishops and queens that diagonally attack the king square
const Bitboard bishopsQueens = pos->GetPieceColorBB(BISHOP, side^1) | pos->GetPieceColorBB(QUEEN, side^1);
// Bitboard of rooks and queens that attack the king square
const Bitboard rooksQueens = pos->GetPieceColorBB(ROOK, side^1) | pos->GetPieceColorBB(QUEEN, side^1);
Bitboard bishop_pin_mask = bishopsQueens & GetBishopAttacks(kingSquare, them);
// Bitboard of rooks and queens that attack the king square
Bitboard rook_pin_mask = rooksQueens & GetRookAttacks(kingSquare, them);

Bitboard bishop_pin, rook_pin;
bishop_pin = rook_pin = 0ULL;

while (bishop_pin_mask) {
int index = GetLsbIndex(bishop_pin_mask);
Bitboard possible_pin = RayBetween(kingSquare, index) | (1ULL << index);
if (CountBits(possible_pin & pos->Occupancy(side)) == 1)
bishop_pin |= possible_pin;
pop_bit(bishop_pin_mask, index);
}
while (rook_pin_mask) {
int index = GetLsbIndex(rook_pin_mask);
Bitboard possible_pin = RayBetween(kingSquare, index) | (1ULL << index);
if (CountBits(possible_pin & pos->Occupancy(side)) == 1)
rook_pin |= possible_pin;
pop_bit(rook_pin_mask, index);
}
pos->pinHV = rook_pin;
pos->pinD = bishop_pin;
}

Bitboard RayBetween(int square1, int square2) {
return SQUARES_BETWEEN_BB[square1][square2];
}
Expand Down
3 changes: 3 additions & 0 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ struct S_Undo {
int plyFromNull = 0;
Bitboard checkers = 0ULL;
Bitboard checkMask = fullCheckmask;
Bitboard pinHV;
Bitboard pinD;
}; // stores a move and the state of the game before that move is made
// for rollback purposes

Expand Down Expand Up @@ -252,6 +254,7 @@ void ResetInfo(S_SearchINFO* info);
// Get on what square of the board the king of color c resides
[[nodiscard]] int KingSQ(const S_Board* pos, const int c);
[[nodiscard]] Bitboard GetCheckersBB(const S_Board* pos, const int side);
void UpdatePinMasks(S_Board* pos, const int side);
Bitboard RayBetween(int square1, int square2);
[[nodiscard]] int GetEpSquare(const S_Board* pos);
[[nodiscard]] uint64_t GetMaterialValue(const S_Board* pos);
Expand Down
31 changes: 0 additions & 31 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,6 @@ void initializeLookupTables() {
}
}

void DoPinMask(S_Board* pos, int color, int sq) {
Bitboard them = pos->Enemy();
Bitboard bishop_mask = (pos->bitboards[(color ^ 1) * 6 + 2] |
pos->bitboards[(color ^ 1) * 6 + 4]) &
GetBishopAttacks(sq, them);
Bitboard rook_mask = (pos->bitboards[(color ^ 1) * 6 + 3] |
pos->bitboards[0 + (color ^ 1) * 6 + 4]) &
GetRookAttacks(sq, them);
Bitboard rook_pin = 0ULL;
Bitboard bishop_pin = 0ULL;
pos->pinD = 0ULL;
pos->pinHV = 0ULL;

while (rook_mask) {
int index = GetLsbIndex(rook_mask);
Bitboard possible_pin = (SQUARES_BETWEEN_BB[sq][index] | (1ULL << index));
if (CountBits(possible_pin & pos->occupancies[color]) == 1)
rook_pin |= possible_pin;
pop_bit(rook_mask, index);
}
while (bishop_mask) {
int index = GetLsbIndex(bishop_mask);
Bitboard possible_pin = (SQUARES_BETWEEN_BB[sq][index] | (1ULL << index));
if (CountBits(possible_pin & pos->occupancies[color]) == 1)
bishop_pin |= possible_pin;
pop_bit(bishop_mask, index);
}
pos->pinHV = rook_pin;
pos->pinD = bishop_pin;
}

// PreCalculate the logarithms used in the reduction calculation
void InitReductions() {
// Avoid log(0) because it's bad
Expand Down
4 changes: 0 additions & 4 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ void InitLeapersAttacks();
void InitSlidersAttacks();

void InitAll();

[[nodiscard]] Bitboard DoCheckmask(S_Board* pos, int color, int sq);

void DoPinMask(S_Board* pos, int color, int sq);
20 changes: 17 additions & 3 deletions src/makemove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ void MakeUCIMove(const int move, S_Board* pos) {
}
else
pos->checkMask = fullCheckmask;
// Update pinmasks
UpdatePinMasks(pos, pos->side);
}

// make move on chess board
Expand All @@ -182,7 +184,8 @@ void MakeMove(const int move, S_Board* pos) {
pos->history[pos->hisPly].plyFromNull = pos->plyFromNull;
pos->history[pos->hisPly].checkers = pos->checkers;
pos->history[pos->hisPly].checkMask = pos->checkMask;

pos->history[pos->hisPly].pinHV = pos->pinHV;
pos->history[pos->hisPly].pinD = pos->pinD;
// Store position key in the array of searched position
pos->played_positions.emplace_back(pos->posKey);

Expand Down Expand Up @@ -217,6 +220,7 @@ void MakeMove(const int move, S_Board* pos) {
else if (capture) {
const int pieceCap = pos->pieces[targetSquare];
assert(pieceCap != EMPTY);
assert(GetPieceType(pieceCap) != KING);
ClearPieceNNUE(pieceCap, targetSquare, pos);

pos->history[pos->hisPly].capture = pieceCap;
Expand Down Expand Up @@ -292,6 +296,8 @@ void MakeMove(const int move, S_Board* pos) {
}
else
pos->checkMask = fullCheckmask;
// Update pinmasks
UpdatePinMasks(pos, pos->side);
}

void UnmakeMove(const int move, S_Board* pos) {
Expand All @@ -305,6 +311,8 @@ void UnmakeMove(const int move, S_Board* pos) {
pos->plyFromNull = pos->history[pos->hisPly].plyFromNull;
pos->checkers = pos->history[pos->hisPly].checkers;
pos->checkMask = pos->history[pos->hisPly].checkMask;
pos->pinHV = pos->history[pos->hisPly].pinHV;
pos->pinD = pos->history[pos->hisPly].pinD;

// parse move
const int sourceSquare = From(move);
Expand Down Expand Up @@ -389,6 +397,8 @@ void MakeNullMove(S_Board* pos) {
pos->history[pos->hisPly].plyFromNull = pos->plyFromNull;
pos->history[pos->hisPly].checkers = pos->checkers;
pos->history[pos->hisPly].checkMask = pos->checkMask;
pos->history[pos->hisPly].pinHV = pos->pinHV;
pos->history[pos->hisPly].pinD = pos->pinD;
// Store position key in the array of searched position
pos->played_positions.emplace_back(pos->posKey);

Expand All @@ -404,18 +414,22 @@ void MakeNullMove(S_Board* pos) {

pos->ChangeSide();
HashKey(pos, SideKey);
// Update pinmasks
UpdatePinMasks(pos, pos->side);
}

// Take back a null move
void TakeNullMove(S_Board* pos) {
pos->hisPly--;

pos->castleperm = pos->history[pos->hisPly].castlePerm;
pos->fiftyMove = pos->history[pos->hisPly].fiftyMove;
pos->enPas = pos->history[pos->hisPly].enPas;
pos->fiftyMove = pos->history[pos->hisPly].fiftyMove;
pos->castleperm = pos->history[pos->hisPly].castlePerm;
pos->plyFromNull = pos->history[pos->hisPly].plyFromNull;
pos->checkers = pos->history[pos->hisPly].checkers;
pos->checkMask = pos->history[pos->hisPly].checkMask;
pos->pinHV = pos->history[pos->hisPly].pinHV;
pos->pinD = pos->history[pos->hisPly].pinD;

pos->ChangeSide();
pos->posKey = pos->played_positions.back();
Expand Down
9 changes: 1 addition & 8 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ static inline Bitboard PawnPush(int color, int sq) {
return (1ULL << (sq + 8));
}

static inline void init(S_Board* pos, int color, int sq) {
DoPinMask(pos, color, sq);
}
// Check for move legality by generating the list of legal moves in a position and checking if that move is present
int MoveExists(S_Board* pos, const int move) {
S_MOVELIST list[1];
Expand Down Expand Up @@ -199,10 +196,8 @@ void GenerateMoves(S_MOVELIST* move_list, S_Board* pos) { // init move count
// define source & target squares
int sourceSquare, targetSquare;

init(pos, pos->side, KingSQ(pos, pos->side));

const int checks = CountBits(pos->checkers);

if (checks < 2) {
Bitboard pawns = pos->GetPieceColorBB(PAWN, pos->side);
while (pawns) {
Expand Down Expand Up @@ -353,8 +348,6 @@ void GenerateCaptures(S_MOVELIST* move_list, S_Board* pos) {
// define source & target squares
int sourceSquare, targetSquare;

init(pos, pos->side, KingSQ(pos, pos->side));

const int checks = CountBits(pos->checkers);

if (checks < 2) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <cstdint>

#define NAME "Alexandria-5.1.11.1"
#define NAME "Alexandria-5.1.12"

// define bitboard data type
using Bitboard = uint64_t;
Expand Down

0 comments on commit 9633c74

Please sign in to comment.