aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-20 20:24:49 +0200
committerAdam <adammegarules1@gmail.com>2026-08-20 20:24:49 +0200
commite4a8a445acd8f966861174ec5d6e2537f060dc1a (patch)
tree7b7c7f89b8e8fce9b094e7b2ed96ab442e25b9b0
parent058fecaefaf07fc3d84f395313eb6ad0f10e81a7 (diff)
perf(board): using magic bitboards for is square attacked helper
-rw-r--r--src/moves.cpp73
1 files changed, 24 insertions, 49 deletions
diff --git a/src/moves.cpp b/src/moves.cpp
index 531d8a9..8990621 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -345,12 +345,12 @@ computeBishopAttacks() {
return BISHOP_ATTACKS;
}
-std::array<std::array<uint64_t, MAGIC_INDEX_COUNT>, 64> rookAttacks;
-std::array<std::array<uint64_t, MAGIC_INDEX_COUNT>, 64> bishopAttacks;
+std::array<std::array<uint64_t, MAGIC_INDEX_COUNT>, 64> ROOK_ATTACKS;
+std::array<std::array<uint64_t, MAGIC_INDEX_COUNT>, 64> BISHOP_ATTACKS;
void initMagicBitboards() {
- rookAttacks = computeRookAttacks();
- bishopAttacks = computeBishopAttacks();
+ ROOK_ATTACKS = computeRookAttacks();
+ BISHOP_ATTACKS = computeBishopAttacks();
};
constexpr std::array<std::uint64_t, 64> KNIGHT_ATTACKS = computeKnightAttacks();
@@ -389,7 +389,7 @@ static void GenerateRookMoves(const uint8_t &from, const Game &g,
const move_generate_options &options) {
uint64_t mask = ROOK_MASKS[from] & g.PieceBitboard;
uint64_t index = turnMaskAndSquareToSmallerUsingMagic(mask, rookMagics[from]);
- uint64_t rook_attacks = rookAttacks[from][index];
+ uint64_t rook_attacks = ROOK_ATTACKS[from][index];
rook_attacks &= ~(g.turn ? g.WhitePieceBitboard : g.BlackPieceBitboard);
while (rook_attacks != 0) {
@@ -412,7 +412,7 @@ static void GenerateBishopMoves(const uint8_t &from, const Game &g,
uint64_t mask = BISHOP_MASKS[from] & g.PieceBitboard;
uint64_t index =
turnMaskAndSquareToSmallerUsingMagic(mask, bishopMagics[from]);
- uint64_t rook_attacks = bishopAttacks[from][index];
+ uint64_t rook_attacks = BISHOP_ATTACKS[from][index];
rook_attacks &= ~(g.turn ? g.WhitePieceBitboard : g.BlackPieceBitboard);
while (rook_attacks != 0) {
@@ -573,51 +573,26 @@ bool IsSquareAttacked(const Game &g, Position square, bool byColor) {
if (king_attacks > 0) {
return true;
}
- // Rooks + queens (orthogonal rays)
- constexpr std::array<int, 4> rookDirs{-8, 8, -1, 1};
- for (int dir : rookDirs) {
- int pos = target;
- while (true) {
- int next = pos + dir;
- if (next < 0 || next >= 64) {
- break;
- }
- if ((dir == 1 || dir == -1) && next / 8 != pos / 8) {
- break; // horizontal wrap
- }
-
- Piece p = g.pieces[next];
- if (p.type != NONEPIECE) {
- if (p.color == byColor && (p.type == ROOK || p.type == QUEEN))
- return true;
- break;
- }
- pos = next;
- }
- }
-
- // Bishops + queens (diagonal rays)
- constexpr std::array<int, 4> bishopDirs{-9, 9, -7, 7};
- for (int dir : bishopDirs) {
- int pos = target;
- while (true) {
- int next = pos + dir;
- if (next < 0 || next >= 64)
- break;
- if (std::abs((next % 8) - (pos % 8)) != 1)
- break; // diagonal wrap
-
- Piece p = g.pieces[next];
- if (p.type != NONEPIECE) {
- if (p.color == byColor && (p.type == BISHOP || p.type == QUEEN))
- return true;
- break;
- }
- pos = next;
- }
+ // Rooks + queens
+ auto t = static_cast<size_t>(target);
+ uint64_t rookMask = ROOK_MASKS[t] & g.PieceBitboard;
+ uint64_t rookIndex =
+ turnMaskAndSquareToSmallerUsingMagic(rookMask, rookMagics[t]);
+ uint64_t rookAttacks = ROOK_ATTACKS[t][rookIndex];
+ rookAttacks &= g.PieceBitboards[static_cast<size_t>(byColor)][ROOK] |
+ g.PieceBitboards[static_cast<size_t>(byColor)][QUEEN];
+ if (rookAttacks > 0) {
+ return true;
}
- return false;
+ // Bishops + queens
+ uint64_t bishopMask = BISHOP_MASKS[t] & g.PieceBitboard;
+ uint64_t bishopIndex =
+ turnMaskAndSquareToSmallerUsingMagic(bishopMask, bishopMagics[t]);
+ uint64_t bishop_attacks = BISHOP_ATTACKS[t][bishopIndex];
+ bishop_attacks &= g.PieceBitboards[static_cast<size_t>(byColor)][BISHOP] |
+ g.PieceBitboards[static_cast<size_t>(byColor)][QUEEN];
+ return bishop_attacks > 0;
}
GameState GetNewGameState(Game *g) {
// make sure we dont override game ending states