From 2ce3570dc6aa67f82dd6f02fc4e518be3e33aabf Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sat, 5 Sep 2026 00:19:59 -0700 Subject: Maintain pos->bbOccupied incrementally, dedupe its two from-scratch builders chess.h: add POSITION::bbOccupied (full-board occupancy, both colors, every piece including kings), maintained incrementally alongside bbPieces/bbPawns rather than rebuilt on demand -- resolves the open question in EVAL.md section 0 about whether this is worth doing given both generate.c and the planned eval.c mobility rewrite need it. move.c: SlidePiece/SlidePawn/LiftPiece/PlacePiece and their WithoutSigs variants now maintain bbOccupied at the same choke points that already maintain bbPieces/bbPawns -- unconditionally, since occupancy doesn't care about piece type or color. Kings only ever move through SlidePiece/SlidePieceWithoutSigs (never Lift/Place), so no separate king-specific update site was needed. fen.c: populate bbOccupied when parsing a FEN. board.c: VerifyPositionConsistency cross-checks pos->bbOccupied against a from-scratch rebuild, same pattern already used for bbPieces/bbPawns. generate.c/movesup.c/see.c: replace call sites that rebuilt full occupancy via _BuildFullOccupiedBB/_BuildOccupiedBB with direct reads of pos->bbOccupied; delete see.c's _BuildOccupiedBB, which was a byte-for-byte duplicate of generate.c's _BuildFullOccupiedBB (kept only as the from-scratch ground truth for the new consistency check and testgenerate.c's benchmark harness). testsup.c: GenerateRandomLegalPosition builds POSITIONs by poking rgSquare/bbPieces/bbPawns directly, bypassing both move.c and fen.c -- a third construction path the above missed. It never set bbOccupied, so the new VerifyPositionConsistency check failed on every generated position, and since generation retries until a position verifies, the self-test suite spun forever (100% CPU, no progress) instead of crashing outright. Fixed by setting bbOccupied at all four placement sites (both kings, pawn, non-pawn piece). Full self-test suite and precommit_check.sh verified clean afterward. board_representation/EVAL.md: record the bbOccupied decision and rationale, resolving section 0's open question. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01P6g6iF6mD1Hau6nCZCzwYj --- src/see.c | 43 ++++++++----------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) (limited to 'src/see.c') diff --git a/src/see.c b/src/see.c index 4ba62ad..dc308d5 100755 --- a/src/see.c +++ b/src/see.c @@ -167,39 +167,12 @@ Return value: // out-of-line FirstBit/LastBit -- worth avoiding call overhead in a // per-move-generated, per-node hot path like this one. // -static BITBOARD -_BuildOccupiedBB(IN POSITION *pos) -/** - -Routine description: - - Full-board occupancy (both colors, every piece including pawns - and kings), built from the incrementally-maintained bbPieces[2][8] - and bbPawns[2] fields plus the king mailbox array - (cNonPawns[.][0], a single square per side -- a bitboard for that - adds nothing). All O(1) ORs now that bbPawns exists; this used to - loop cPawns[2][8] (up to 16 iterations) to build the pawn portion, - which ran on every single call regardless of how few pawns were - actually relevant. - -Parameters: - - POSITION *pos - -Return value: - - BITBOARD - -**/ -{ - return (pos->bbPieces[WHITE][KNIGHT] | pos->bbPieces[WHITE][BISHOP] | - pos->bbPieces[WHITE][ROOK] | pos->bbPieces[WHITE][QUEEN] | - pos->bbPieces[BLACK][KNIGHT] | pos->bbPieces[BLACK][BISHOP] | - pos->bbPieces[BLACK][ROOK] | pos->bbPieces[BLACK][QUEEN] | - pos->bbPawns[WHITE] | pos->bbPawns[BLACK] | - COOR_TO_BB(pos->cNonPawns[WHITE][0]) | - COOR_TO_BB(pos->cNonPawns[BLACK][0])); -} +// (This file used to have its own static _BuildOccupiedBB here, +// byte-for-byte identical to generate.c's _BuildFullOccupiedBB -- +// removed 2026-09-05 now that pos->bbOccupied is incrementally +// maintained directly on POSITION; see board_representation/ +// EVAL.md section 0. Callers below just read pos->bbOccupied.) +// // Non-static (unlike its historical file-local status) so // generate.c's Part B (_GenerateEscapes) bitboard work can call it @@ -249,7 +222,7 @@ Parameters: POSITION *pos, COOR cSquare : target square ULONG uSide : side whose attackers on cSquare we want - BITBOARD bbOccupied : full-board occupancy (see _BuildOccupiedBB) + BITBOARD bbOccupied : full-board occupancy (see pos->bbOccupied) Return value: @@ -412,7 +385,7 @@ Return value: // // Knights/bishops/rooks/queens/king, via the bitboard primitive. // - bbOccupied = _BuildOccupiedBB(pos); + bbOccupied = pos->bbOccupied; bbAttackers = _WhoAttacksSquareBB(pos, cSquare, uSide, bbOccupied); while (bbAttackers) { -- cgit v1.3