diff options
| author | Scott Gasch <[email protected]> | 2026-10-15 15:21:26 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2026-10-15 15:21:26 -0700 |
| commit | 2bf4eb27a473089b9b30726a678fbac27d0f8a69 (patch) | |
| tree | 23e7271eab238cf2fa97128d902c0c828e7d1583 /src/move.c | |
| parent | ac7572166bd24868fcaf5980a91bb82fe61ba677 (diff) | |
NNUE accumulator incremental maintenance: MakeMove/UnmakeMove hookup
Wires the live NNUE accumulator (SEARCHER_THREAD_CONTEXT.sNnueAccum[2],
added in the previous commit) into real incremental maintenance,
hooked directly into the piece-movement primitives rather than
re-deriving move type from MOVE in a separate pass:
- chess.h: shared HalfKAv2_hm feature-index formula/constants
(NnueFeatureIndex and friends), moved here from nnuefeatures.c so
the reference dump and the real hot-path code can never drift
apart. Prototypes for NnueAccumAddFeature/RemoveFeature/Recompute
and the (still placeholder, zero-initialized) embedding table.
LiftPiece/PlacePiece/SlidePiece/SlidePawn/SlidePieceWithoutSigs
signatures gain an NNUE_ACCUMULATOR sNnueAccum[2] parameter.
- nnueaccum.c (new): the embedding table storage and the three
accumulator operations. Written as straight-line 256-float loops,
structured for -O3 -ffast-math auto-vectorization on this box's
AVX2+FMA target; explicit SIMD intrinsics and real weight loading
are separate, deliberately deferred follow-ups.
- move.c: all 8 Lift/Place/Slide(WithoutSigs) primitives now keep
sNnueAccum incrementally correct for both perspectives on every
call -- except when the piece is a king, where each primitive skips
its own-color perspective entirely (that perspective needs a full
recompute, not a delta, since every HalfKAv2_hm feature in it is
own-king-relative; computing an incremental update for it only to
discard it unread would be wasted work). MakeMove backs up that
perspective into the current ply's PLY_INFO.sNnueAccumBackup before
a king moves (castling or a plain step) and overwrites it via
NnueAccumRecompute once the board reflects the final position;
UnmakeMove restores it from the same backup slot. DEBUG builds tag
the backup with the color it belongs to and poison it after
consumption, release builds carry no overhead for either.
- nnuefeatures.c: now a thin board-walking wrapper around the shared
chess.h formula instead of its own duplicated copy.
- testmove.c: TestLiftPlaceSlidePiece passes a local (unread) dummy
accumulator pair -- the only caller of these primitives outside
move.c, and it never needed NNUE awareness, just a real argument.
Verified via the full precommit_check.sh (all 4 gates, including the
new combined DEBUG=1 TEST=1 self-test, which exercises MakeMove/
UnmakeMove heavily via TestMoveGenerator's perft counts and
TestLegalMoveGenerator) -- confirms nothing crashes and no existing
invariant breaks. Does NOT yet confirm the actual property that
matters (incrementally-maintained accumulator == recompute-from-
scratch) -- no test exercises that yet; next step.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Diffstat (limited to 'src/move.c')
| -rw-r--r-- | src/move.c | 227 |
1 files changed, 187 insertions, 40 deletions
@@ -22,8 +22,8 @@ Revision History: #include "chess.h" -void -SlidePiece(POSITION *pos, COOR cFrom, COOR cTo) +void +SlidePiece(POSITION *pos, COOR cFrom, COOR cTo, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -36,6 +36,12 @@ Parameters: POSITION *pos : the board COOR cFrom : square moving from COOR cTo : square moving to + NNUE_ACCUMULATOR sNnueAccum[2] : mover's live accumulator pair, + kept incrementally up to date for both perspectives -- even + when p is a king, in which case the caller (MakeMove) is + expected to treat that one perspective's result here as + transient/wrong and immediately overwrite it via + NnueAccumRecompute; see chess.h's prototype comment. Return value: @@ -80,13 +86,38 @@ Return value: pos->rgSquare[cTo].uIndex = INVALID_PIECE_INDEX; #ifdef DEBUG pos->rgSquare[cFrom].uIndex = INVALID_PIECE_INDEX; +#endif + // A king move is about to have ITS OWN perspective fully + // overwritten by a recompute at the MakeMove/UnmakeMove call + // site right after this returns (every HalfKAv2_hm feature in + // that perspective depends on its own king square -- see + // chess.h's NNUE_ACCUMULATOR comment) -- skip that perspective's + // update here entirely rather than computing it only to discard + // it unread. The opponent's perspective is unaffected by whose + // king this is and still needs the ordinary incremental update. + // Single branch, predicts near-perfectly (king moves are a small + // minority of all SlidePiece calls). + if (IS_KING(p)) + { + ULONG xC = FLIP(c); + NnueAccumRemoveFeature(&sNnueAccum[xC], xC, pos->cKingSq[xC], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[xC], xC, pos->cKingSq[xC], cTo, p); + } + else + { + NnueAccumRemoveFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cTo, p); + NnueAccumRemoveFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cTo, p); + } +#ifdef DEBUG VerifyPositionConsistency(pos, FALSE); #endif } -void -SlidePawn(POSITION *pos, COOR cFrom, COOR cTo) +void +SlidePawn(POSITION *pos, COOR cFrom, COOR cTo, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -99,6 +130,9 @@ Parameters: POSITION *pos : the board COOR cFrom : square moving from COOR cTo : square moving to + NNUE_ACCUMULATOR sNnueAccum[2] : mover's live accumulator pair -- + see SlidePiece's comment (a pawn is never a king, so there's + no own-perspective-recompute exception to worry about here). Return value: @@ -133,13 +167,19 @@ Return value: pos->rgSquare[cTo].uIndex = INVALID_PIECE_INDEX; #ifdef DEBUG pos->rgSquare[cFrom].uIndex = INVALID_PIECE_INDEX; +#endif + NnueAccumRemoveFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cTo, p); + NnueAccumRemoveFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cTo, p); +#ifdef DEBUG VerifyPositionConsistency(pos, FALSE); #endif } void -SlidePieceWithoutSigs(POSITION *pos, COOR cFrom, COOR cTo) +SlidePieceWithoutSigs(POSITION *pos, COOR cFrom, COOR cTo, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -149,11 +189,20 @@ Routine description: of the routine doesn't maintain the consistency of the position signatures and should therefore only be called from UnmakeMove. + Note: unlike signatures, the NNUE accumulator has no equivalent + cheap "restore wholesale from a saved scalar" trick for a + non-king-move's opponent-perspective update, so sNnueAccum is + still kept incrementally correct here even though sigs aren't -- + see SlidePiece's comment for the king-move exception, handled the + same way at the UnmakeMove call site (restore-from-backup + immediately after, for the castling color's own perspective). + Parameters: POSITION *pos : the board COOR cFrom : square moving from COOR cTo : square moving to + NNUE_ACCUMULATOR sNnueAccum[2] : mover's live accumulator pair Return value: @@ -190,11 +239,25 @@ Return value: #ifdef DEBUG pos->rgSquare[cFrom].uIndex = INVALID_PIECE_INDEX; #endif + // See SlidePiece's comment on the same check. + if (IS_KING(p)) + { + ULONG xC = FLIP(c); + NnueAccumRemoveFeature(&sNnueAccum[xC], xC, pos->cKingSq[xC], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[xC], xC, pos->cKingSq[xC], cTo, p); + } + else + { + NnueAccumRemoveFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cTo, p); + NnueAccumRemoveFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cTo, p); + } } void -SlidePawnWithoutSigs(POSITION *pos, COOR cFrom, COOR cTo) +SlidePawnWithoutSigs(POSITION *pos, COOR cFrom, COOR cTo, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -203,12 +266,15 @@ Routine description: faster than LiftPiece / PlacePiece. Also note that this version of the routine doesn't maintain the consistency of the position signatures and should therefore only be called from UnmakeMove. + See SlidePieceWithoutSigs's comment on why sNnueAccum is still + kept correct here regardless. Parameters: POSITION *pos : the board COOR cFrom : square moving from COOR cTo : square moving to + NNUE_ACCUMULATOR sNnueAccum[2] : mover's live accumulator pair Return value: @@ -241,11 +307,15 @@ Return value: #ifdef DEBUG pos->rgSquare[cFrom].uIndex = INVALID_PIECE_INDEX; #endif + NnueAccumRemoveFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cTo, p); + NnueAccumRemoveFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cFrom, p); + NnueAccumAddFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cTo, p); } PIECE -LiftPiece(POSITION *pos, COOR cSquare) +LiftPiece(POSITION *pos, COOR cSquare, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -257,6 +327,10 @@ Parameters: POSITION *pos, COOR cSquare + NNUE_ACCUMULATOR sNnueAccum[2] : mover's live accumulator pair -- + a king is never lifted (see ASSERT below, kings are never + captured), so there's no own-perspective exception to worry + about here, unlike SlidePiece. Return value: @@ -323,6 +397,8 @@ Return value: pos->bbPieces[color][u] &= ~COOR_TO_BB(cSquare); } + NnueAccumRemoveFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cSquare, pLifted); + NnueAccumRemoveFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cSquare, pLifted); #ifdef DEBUG VerifyPositionConsistency(pos, FALSE); #endif @@ -331,7 +407,7 @@ Return value: PIECE -LiftPieceWithoutSigs(POSITION *pos, COOR cSquare) +LiftPieceWithoutSigs(POSITION *pos, COOR cSquare, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -409,12 +485,14 @@ Return value: pos->bbPieces[color][u] &= ~COOR_TO_BB(cSquare); } + NnueAccumRemoveFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cSquare, pLifted); + NnueAccumRemoveFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cSquare, pLifted); return(pLifted); } void -PlacePiece(POSITION *pos, COOR cSquare, PIECE pPiece) +PlacePiece(POSITION *pos, COOR cSquare, PIECE pPiece, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -485,6 +563,8 @@ Return value: // pos->rgSquare[cSquare].pPiece = pPiece; + NnueAccumAddFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cSquare, pPiece); + NnueAccumAddFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cSquare, pPiece); #ifdef DEBUG VerifyPositionConsistency(pos, FALSE); #endif @@ -492,7 +572,7 @@ Return value: void -PlacePieceWithoutSigs(POSITION *pos, COOR cSquare, PIECE pPiece) +PlacePieceWithoutSigs(POSITION *pos, COOR cSquare, PIECE pPiece, NNUE_ACCUMULATOR sNnueAccum[2]) /** Routine description: @@ -562,10 +642,13 @@ Return value: // Place the piece on the board // pos->rgSquare[cSquare].pPiece = pPiece; + + NnueAccumAddFeature(&sNnueAccum[WHITE], WHITE, pos->cKingSq[WHITE], cSquare, pPiece); + NnueAccumAddFeature(&sNnueAccum[BLACK], BLACK, pos->cKingSq[BLACK], cSquare, pPiece); } -void +void SaveCurrentPositionHistoryInContext(SEARCHER_THREAD_CONTEXT *ctx, MOVE mv) /** @@ -712,10 +795,10 @@ Return value: { ASSERT(!IS_PAWN(pPromoted)); ASSERT(GET_COLOR(pPromoted) == uColor); - pPiece = LiftPiece(pos, mv.cFrom); - pCaptured = LiftPiece(pos, mv.cTo); + pPiece = LiftPiece(pos, mv.cFrom, ctx->sNnueAccum); + pCaptured = LiftPiece(pos, mv.cTo, ctx->sNnueAccum); ASSERT(!pCaptured || (OPPOSITE_COLORS(pCaptured, pPiece))); - PlacePiece(pos, mv.cTo, pPromoted); + PlacePiece(pos, mv.cTo, pPromoted, ctx->sNnueAccum); pos->uFifty = 0; goto update_castle_and_clear_ep; } @@ -727,12 +810,12 @@ Return value: { ASSERT(mv.cTo == pos->cEpSquare); pCaptured = LiftPiece(pos, mv.cTo + - iSign[uColor] * 16); + iSign[uColor] * 16, ctx->sNnueAccum); ASSERT(IS_PAWN(pCaptured)); ASSERT(OPPOSITE_COLORS(pPiece, pCaptured)); ASSERT(IS_EMPTY(pos->rgSquare[mv.cTo].pPiece)); ASSERT(IS_PAWN(pPiece)); - SlidePawn(pos, mv.cFrom, mv.cTo); + SlidePawn(pos, mv.cFrom, mv.cTo, ctx->sNnueAccum); pos->uFifty = 0; goto clear_ep; } @@ -750,9 +833,9 @@ Return value: ASSERT(IS_EMPTY(pos->rgSquare[c].pPiece)); ASSERT(IS_EMPTY(pos->rgSquare[mv.cTo].pPiece)); ASSERT(IS_PAWN(pPiece)); - SlidePawn(pos, mv.cFrom, mv.cTo); + SlidePawn(pos, mv.cFrom, mv.cTo, ctx->sNnueAccum); pos->uFifty = 0; - + // // Only change the position signature to reflect the // possible en-passant if there's an enemy who can @@ -858,8 +941,29 @@ Return value: ASSERT(IS_EMPTY(pos->rgSquare[mv.cTo].pPiece)); ASSERT(IS_ROOK(pos->rgSquare[cRookFrom].pPiece)); ASSERT(IS_EMPTY(pos->rgSquare[cRookTo].pPiece)); - SlidePiece(pos, mv.cFrom, mv.cTo); // king - SlidePiece(pos, cRookFrom, cRookTo); // rook + // Back up uColor's pre-move accumulator before touching + // the board: every HalfKAv2_hm feature in THAT + // perspective depends on its own king square, so the + // king's move below invalidates the whole thing, not + // just a delta -- see chess.h's NNUE_ACCUMULATOR comment. + // Indexed by ply (not a single shared slot) so nested + // own-king moves at different plies never collide, same + // reasoning as cTrapped/cEnprise's existing per-ply + // indexing. + ctx->sPlyInfo[ctx->uPly].sNnueAccumBackup = ctx->sNnueAccum[uColor]; +#ifdef DEBUG + ctx->sPlyInfo[ctx->uPly].uNnueAccumBackupColor = uColor; +#endif + SlidePiece(pos, mv.cFrom, mv.cTo, ctx->sNnueAccum); // king + SlidePiece(pos, cRookFrom, cRookTo, ctx->sNnueAccum); // rook + // Both slides above already skipped uColor's own + // perspective (SlidePiece's own IS_KING check) and kept + // xColor's perspective incrementally correct. Recompute + // uColor's perspective from scratch against the + // now-fully-updated board (king AND rook already in + // their final squares) -- the only correct way to update + // it, not just the cheapest. + NnueAccumRecompute(&ctx->sNnueAccum[uColor], pos, uColor); pos->uFifty = 0; goto clear_ep; } @@ -879,19 +983,30 @@ Return value: if (pCaptured) { pos->uFifty = 0; - pCaptured = LiftPiece(pos, mv.cTo); + pCaptured = LiftPiece(pos, mv.cTo, ctx->sNnueAccum); ASSERT(!IS_KING(pCaptured)); ASSERT(!IS_EMPTY(pCaptured)); ASSERT(OPPOSITE_COLORS(pCaptured, pPiece)); } - if (IS_PAWN(pPiece)) + if (IS_PAWN(pPiece)) { pos->uFifty = 0; - SlidePawn(pos, mv.cFrom, mv.cTo); + SlidePawn(pos, mv.cFrom, mv.cTo, ctx->sNnueAccum); + } + else if (IS_KING(pPiece)) + { + // Ordinary (non-castling) king step -- same backup/recompute + // wrapper as the castling branch above, see its comment. + ctx->sPlyInfo[ctx->uPly].sNnueAccumBackup = ctx->sNnueAccum[uColor]; +#ifdef DEBUG + ctx->sPlyInfo[ctx->uPly].uNnueAccumBackupColor = uColor; +#endif + SlidePiece(pos, mv.cFrom, mv.cTo, ctx->sNnueAccum); + NnueAccumRecompute(&ctx->sNnueAccum[uColor], pos, uColor); } - else + else { - SlidePiece(pos, mv.cFrom, mv.cTo); + SlidePiece(pos, mv.cFrom, mv.cTo, ctx->sNnueAccum); } update_castle_and_clear_ep: @@ -1156,26 +1271,26 @@ Return value: // No need to call SlidePiece here, the signatures will // get reset to their pre-move state below // - SlidePieceWithoutSigs(pos, D1, A1); // rook - SlidePieceWithoutSigs(pos, C1, E1); // king + SlidePieceWithoutSigs(pos, D1, A1, ctx->sNnueAccum); // rook + SlidePieceWithoutSigs(pos, C1, E1, ctx->sNnueAccum); // king ASSERT(IS_KING(pos->rgSquare[E1].pPiece)); ASSERT(IS_ROOK(pos->rgSquare[A1].pPiece)); break; case G1: - SlidePieceWithoutSigs(pos, F1, H1); // rook - SlidePieceWithoutSigs(pos, G1, E1); // king + SlidePieceWithoutSigs(pos, F1, H1, ctx->sNnueAccum); // rook + SlidePieceWithoutSigs(pos, G1, E1, ctx->sNnueAccum); // king ASSERT(IS_KING(pos->rgSquare[E1].pPiece)); ASSERT(IS_ROOK(pos->rgSquare[H1].pPiece)); break; case C8: - SlidePieceWithoutSigs(pos, D8, A8); // rook - SlidePieceWithoutSigs(pos, C8, E8); // king + SlidePieceWithoutSigs(pos, D8, A8, ctx->sNnueAccum); // rook + SlidePieceWithoutSigs(pos, C8, E8, ctx->sNnueAccum); // king ASSERT(IS_KING(pos->rgSquare[E8].pPiece)); ASSERT(IS_ROOK(pos->rgSquare[A8].pPiece)); break; case G8: - SlidePieceWithoutSigs(pos, F8, H8); // rook - SlidePieceWithoutSigs(pos, G8, E8); // king + SlidePieceWithoutSigs(pos, F8, H8, ctx->sNnueAccum); // rook + SlidePieceWithoutSigs(pos, G8, E8, ctx->sNnueAccum); // king ASSERT(IS_KING(pos->rgSquare[E8].pPiece)); ASSERT(IS_ROOK(pos->rgSquare[H8].pPiece)); break; @@ -1197,21 +1312,29 @@ Return value: // LiftPiece/PlacePiece must be done here to maintain // piece list integrity. // - (void)LiftPieceWithoutSigs(pos, mv.cTo); + (void)LiftPieceWithoutSigs(pos, mv.cTo, ctx->sNnueAccum); pPiece |= 2; pPiece &= 3; ASSERT(pPiece == mv.pMoved); - PlacePieceWithoutSigs(pos, mv.cFrom, pPiece); + PlacePieceWithoutSigs(pos, mv.cFrom, pPiece, ctx->sNnueAccum); } else { if (IS_PAWN(pPiece)) { - SlidePawnWithoutSigs(pos, mv.cTo, mv.cFrom); + SlidePawnWithoutSigs(pos, mv.cTo, mv.cFrom, ctx->sNnueAccum); } else { - SlidePieceWithoutSigs(pos, mv.cTo, mv.cFrom); + // IS_KING(pPiece) here means an ordinary (non-castling) + // king step undo -- SlidePieceWithoutSigs's own + // IS_KING check already skips uColor's own + // perspective; the restore-from-backup for that + // perspective happens once, generically, below + // (restore_saved_position_data), after ctx->uPly's + // decrement lines up with the same ply MakeMove wrote + // the backup to. + SlidePieceWithoutSigs(pos, mv.cTo, mv.cFrom, ctx->sNnueAccum); } } @@ -1237,16 +1360,16 @@ Return value: iSign[GET_COLOR(mv.pMoved)]].pPiece)); PlacePieceWithoutSigs(pos, mv.cTo + 16 * iSign[GET_COLOR(mv.pMoved)], - pCaptured); + pCaptured, ctx->sNnueAccum); } else { // // Normal capture... resurrect the dead piece where the // killer moved. - // + // ASSERT(IS_EMPTY(pos->rgSquare[mv.cTo].pPiece)); - PlacePieceWithoutSigs(pos, mv.cTo, pCaptured); + PlacePieceWithoutSigs(pos, mv.cTo, pCaptured, ctx->sNnueAccum); } } } @@ -1256,6 +1379,30 @@ Return value: ASSERT(ctx->uPly < MAX_PLY_PER_SEARCH); pi = &ctx->sPlyInfo[ctx->uPly]; + + // If mv moved a king (castling or plain step), that color's own + // NNUE accumulator was never incrementally maintained above -- + // every SlidePiece/SlidePieceWithoutSigs call for a king move + // deliberately skips its own perspective (see SlidePiece's + // comment) since MakeMove always overwrote it via + // NnueAccumRecompute instead. Restore it here from the backup + // MakeMove saved at this exact ply before making the move, the + // same way pos->u64NonPawnSig etc. are restored from pi just + // below -- not via any incremental undo, there isn't one. + if ((mv.uMove != 0) && IS_KING(mv.pMoved)) + { + ULONG uMoverColor = GET_COLOR(mv.pMoved); +#ifdef DEBUG + ASSERT(pi->uNnueAccumBackupColor == uMoverColor); +#endif + ctx->sNnueAccum[uMoverColor] = pi->sNnueAccumBackup; +#ifdef DEBUG + // Poison, not a real "clear" anything downstream depends on + // -- see PLY_INFO's sNnueAccumBackup comment (chess.h). + memset(&pi->sNnueAccumBackup, 0xCC, sizeof(pi->sNnueAccumBackup)); +#endif + } + pos->u64NonPawnSig = pi->u64NonPawnSig; pos->u64PawnSig = pi->u64PawnSig; ASSERT((pos->u64NonPawnSig & WHITE) == FLIP(pos->uToMove)); |
