summaryrefslogtreecommitdiff
path: root/src/data.c
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2026-09-04 01:27:59 -0700
committerScott Gasch <[email protected]>2026-09-04 01:27:59 -0700
commit6c045be8a37a8eae1ea6aed250944e22af2335a1 (patch)
tree7cc90d47795e25d5ad0848cbc904136525596057 /src/data.c
parent5c8d794782d3be6368dbba613ef129b11d878d97 (diff)
Add bbPieces incremental piece-location bitboards (migration plan section 1)
Board-representation migration, section 1: add POSITION.bbPieces[2][8] (per-color, per-piece-type location bitboards, indexed like the existing uNonPawnCount) as incrementally-maintained state, not a per-Eval()-call rebuild -- the structural fix for why the earlier attack-presence-bitboard work measured slower, not faster. - chess.h: bbPieces[2][8] field; extern decls for data.c's g_RookRayToEdge/g_BishopRayToEdge/g_KnightAttacksBB ray tables and their Initialize* functions (needed by the planned bitboard-backed GetAttacks/CountKingSafetyDefects primitive, section 3). - fen.c: populate bbPieces during piece placement; zeroing is free via the existing memset(p, 0, sizeof(POSITION)). - move.c: maintain bbPieces at all 6 non-pawn piece-movement functions (SlidePiece/LiftPiece/PlacePiece and their WithoutSigs siblings used by UnmakeMove) -- covers every move type: normal moves, captures, both-side castling, promotion with/without capture, en passant, and every undo. - board.c: extend VerifyPositionConsistency's existing non-pawn piece-list walk with a parallel bbPieces reconstruction-and-compare, rather than a separate bespoke check. - data.c/main.c: pulled ray-to-edge/knight-attack tables from stash (needed by section 3, not section 1 itself, but zero-risk to land now). Also, while verifying: COOR_TO_BB was a table lookup (BBSQUARE[idx]) measured ~5-7% slower than the pure-ALU shift already sitting unused in SLOWCOOR_TO_BB (whose "SLOW" name reflects a stale assumption about variable shifts never actually tested on this hardware). Switched COOR_TO_BB to the shift; fixed testbitboard.c's existing but broken (dead-code-eliminated, silently reporting "0 cycles/op") comparison benchmark for both while at it. Verified via gmake TEST=1 (including TestMakeUnmakeMove's explicit en-passant/promotion-with-capture/both-castling coverage) and debug_smoke_test.sh, both clean; release build clean and runs normally. Nothing reads bbPieces yet -- pure addition, zero behavioral risk. See board_representation/MIGRATION.md for the full plan. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Jntky4yGUTyQVaGCXms4F2
Diffstat (limited to 'src/data.c')
-rwxr-xr-xsrc/data.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/data.c b/src/data.c
index 23b71c5..1ebbeff 100755
--- a/src/data.c
+++ b/src/data.c
@@ -563,3 +563,160 @@ InitializeDistanceTable(void)
}
#endif
}
+
+//
+// Per-square, per-direction "ray to board edge" bitboards for the
+// rook, indexed [direction][c] with direction matching
+// g_RookRayDeltas below (N, S, E, W). Only entries for real board
+// squares (IS_ON_BOARD(c)) are ever populated/queried; off-board
+// indices are left zeroed and unused. Built once at startup by
+// InitializeRookRayTables() -- part of eval.c's occupancy-bitboard
+// PoC (_EvalRookOccupancyBB et al.), turning a per-call
+// walk-to-the-edge loop into an O(1) table lookup.
+//
+BITBOARD g_RookRayToEdge[4][128];
+const int g_RookRayDeltas[4] = { 16, -16, 1, -1 }; // N, S, E, W (0x88)
+const FLAG g_RookRayPositiveDir[4] = { TRUE, FALSE, TRUE, FALSE };
+
+void
+InitializeRookRayTables(void)
+/**
+
+Routine description:
+
+ One-time startup init for g_RookRayToEdge -- see its comment.
+
+Parameters:
+
+ void
+
+Return value:
+
+ void
+
+**/
+{
+ ULONG uRank, uFile, uDir;
+ COOR c, cSquare;
+
+ memset(g_RookRayToEdge, 0, sizeof(g_RookRayToEdge));
+ for (uRank = 0; uRank < 8; uRank++)
+ {
+ for (uFile = 0; uFile < 8; uFile++)
+ {
+ c = (uRank << 4) | uFile;
+ for (uDir = 0; uDir < 4; uDir++)
+ {
+ for (cSquare = c + g_RookRayDeltas[uDir];
+ IS_ON_BOARD(cSquare);
+ cSquare += g_RookRayDeltas[uDir])
+ {
+ g_RookRayToEdge[uDir][c] |= COOR_TO_BB(cSquare);
+ }
+ }
+ }
+ }
+}
+
+//
+// Same idea as g_RookRayToEdge, for the bishop's 4 diagonal directions.
+//
+BITBOARD g_BishopRayToEdge[4][128];
+const int g_BishopRayDeltas[4] = { 17, -17, 15, -15 }; // NE, SW, NW, SE (0x88)
+const FLAG g_BishopRayPositiveDir[4] = { TRUE, FALSE, TRUE, FALSE };
+
+void
+InitializeBishopRayTables(void)
+/**
+
+Routine description:
+
+ One-time startup init for g_BishopRayToEdge -- see its comment.
+
+Parameters:
+
+ void
+
+Return value:
+
+ void
+
+**/
+{
+ ULONG uRank, uFile, uDir;
+ COOR c, cSquare;
+
+ memset(g_BishopRayToEdge, 0, sizeof(g_BishopRayToEdge));
+ for (uRank = 0; uRank < 8; uRank++)
+ {
+ for (uFile = 0; uFile < 8; uFile++)
+ {
+ c = (uRank << 4) | uFile;
+ for (uDir = 0; uDir < 4; uDir++)
+ {
+ for (cSquare = c + g_BishopRayDeltas[uDir];
+ IS_ON_BOARD(cSquare);
+ cSquare += g_BishopRayDeltas[uDir])
+ {
+ g_BishopRayToEdge[uDir][c] |= COOR_TO_BB(cSquare);
+ }
+ }
+ }
+ }
+}
+
+// A combined 8-ray queen table (rook's 4 directions + bishop's 4,
+// concatenated) was tried here and measured SLOWER than
+// _EvalQueenOccupancyBB's two-pass version reusing g_RookRayToEdge/
+// g_BishopRayToEdge directly -- see that function's comment for why
+// (probable lost constant-folding on the orthogonal-ray flag). Removed
+// rather than left around unused.
+
+//
+// Per-square "all squares a knight on c can hop to" bitboard. Unlike
+// the rook/bishop ray tables, a knight has no blocking to account for
+// -- there's nothing "in between" a knight and its landing square --
+// so this is the complete, final answer for a given square, not a
+// ray-to-edge that still needs an occupancy AND to find blockers.
+// Built once at startup by InitializeKnightAttackTables().
+//
+BITBOARD g_KnightAttacksBB[128];
+
+void
+InitializeKnightAttackTables(void)
+/**
+
+Routine description:
+
+ One-time startup init for g_KnightAttacksBB -- see its comment.
+
+Parameters:
+
+ void
+
+Return value:
+
+ void
+
+**/
+{
+ ULONG uRank, uFile, uDir;
+ COOR c, cSquare;
+
+ memset(g_KnightAttacksBB, 0, sizeof(g_KnightAttacksBB));
+ for (uRank = 0; uRank < 8; uRank++)
+ {
+ for (uFile = 0; uFile < 8; uFile++)
+ {
+ c = (uRank << 4) | uFile;
+ for (uDir = 0; g_iNDeltas[uDir] != 0; uDir++)
+ {
+ cSquare = c + g_iNDeltas[uDir];
+ if (IS_ON_BOARD(cSquare))
+ {
+ g_KnightAttacksBB[c] |= COOR_TO_BB(cSquare);
+ }
+ }
+ }
+ }
+}