summaryrefslogtreecommitdiff
path: root/src/move.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/move.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/move.c')
-rwxr-xr-xsrc/move.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/move.c b/src/move.c
index 5c1a0db..1ed1485 100755
--- a/src/move.c
+++ b/src/move.c
@@ -62,6 +62,8 @@ Return value:
ASSERT(IS_VALID_COLOR(c));
ASSERT(pos->cNonPawns[c][uIndex] == cFrom);
pos->cNonPawns[c][uIndex] = cTo;
+ pos->bbPieces[c][PIECE_TYPE(p)] &= ~COOR_TO_BB(cFrom);
+ pos->bbPieces[c][PIECE_TYPE(p)] |= COOR_TO_BB(cTo);
pos->u64NonPawnSig ^= g_u64SigSeeds[cFrom][PIECE_TYPE(p)][c];
pos->u64NonPawnSig ^= g_u64SigSeeds[cTo][PIECE_TYPE(p)][c];
#ifdef DEBUG
@@ -171,6 +173,8 @@ Return value:
ASSERT(IS_VALID_COLOR(c));
ASSERT(pos->cNonPawns[c][uIndex] == cFrom);
pos->cNonPawns[c][uIndex] = cTo;
+ pos->bbPieces[c][PIECE_TYPE(p)] &= ~COOR_TO_BB(cFrom);
+ pos->bbPieces[c][PIECE_TYPE(p)] |= COOR_TO_BB(cTo);
pos->rgSquare[cTo].pPiece = p;
pos->rgSquare[cTo].uIndex = uIndex;
#ifdef DEBUG
@@ -345,11 +349,12 @@ Return value:
pos->u64NonPawnSig ^= g_u64SigSeeds[cSquare][u][color];
pos->uNonPawnCount[color][u]--;
ASSERT(pos->uNonPawnCount[color][u] <= 9);
+ pos->bbPieces[color][u] &= ~COOR_TO_BB(cSquare);
pos->uWhiteSqBishopCount[color] -= (IS_BISHOP(pLifted) &
IS_SQUARE_WHITE(cSquare));
ASSERT(pos->uWhiteSqBishopCount[color] <= 9);
}
-
+
#ifdef DEBUG
VerifyPositionConsistency(pos, FALSE);
#endif
@@ -477,11 +482,12 @@ Return value:
ASSERT((u >= KNIGHT) && (u < KING));
pos->uNonPawnCount[color][u]--;
ASSERT(pos->uNonPawnCount[color][u] <= 9);
- pos->uWhiteSqBishopCount[color] -= (IS_BISHOP(pLifted) &
+ pos->bbPieces[color][u] &= ~COOR_TO_BB(cSquare);
+ pos->uWhiteSqBishopCount[color] -= (IS_BISHOP(pLifted) &
IS_SQUARE_WHITE(cSquare));
ASSERT(pos->uWhiteSqBishopCount[color] <= 9);
}
-
+
return(pLifted);
}
@@ -556,7 +562,8 @@ Return value:
pos->u64NonPawnSig ^= g_u64SigSeeds[cSquare][u][color];
pos->uNonPawnCount[color][u]++;
ASSERT(pos->uNonPawnCount[color][u] <= 10);
-
+ pos->bbPieces[color][u] |= COOR_TO_BB(cSquare);
+
pos->uWhiteSqBishopCount[color] += (IS_BISHOP(pPiece) &
IS_SQUARE_WHITE(cSquare));
ASSERT(pos->uWhiteSqBishopCount[color] <= 10);
@@ -564,17 +571,17 @@ Return value:
//
// Place the piece on the board
- //
+ //
pos->rgSquare[cSquare].pPiece = pPiece;
pos->rgSquare[cSquare].uIndex = uIndex;
-
+
#ifdef DEBUG
VerifyPositionConsistency(pos, FALSE);
#endif
}
-void
+void
PlacePieceWithoutSigs(POSITION *pos, COOR cSquare, PIECE pPiece)
/**
@@ -645,7 +652,8 @@ Return value:
ASSERT((u >= KNIGHT) && (u < KING));
pos->uNonPawnCount[color][u]++;
ASSERT(pos->uNonPawnCount[color][u] <= 10);
-
+ pos->bbPieces[color][u] |= COOR_TO_BB(cSquare);
+
pos->uWhiteSqBishopCount[color] += (IS_BISHOP(pPiece) &
IS_SQUARE_WHITE(cSquare));
ASSERT(pos->uWhiteSqBishopCount[color] <= 10);