diff options
| author | Scott Gasch <[email protected]> | 2026-09-18 09:43:17 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2026-09-18 09:43:17 -0700 |
| commit | f698c02de213d0ed8968fb7cca5f70df51fa0763 (patch) | |
| tree | 080d6443fbe62d71de092f9acde61da387dff408 /src/eval.c | |
| parent | 4a7ec9ac91939957fb3d3e5564fc24a385b422ff (diff) | |
Redesign lazy/super-lazy eval margins with position-specific percentile
tables, and fix four real search/recognizer bugs found while validating it
Main theme: replace the flat, heavily-padded LAZY_EVAL_BASE_MARGIN /
SUPER_LAZY_MARGIN_BY_ARMY constants with per-material-bucket percentile
tables (king safety by attacker army, residual by own army, separate
tables for the lazy vs super-lazy baselines) derived from a 30k-position
calibration pool, targeting a measured ~1-1.5% exceeded-margin rate
instead of an unmeasured "add lots of headroom" floor. QSearch's
FUTILITY_BASE_MARGIN_LAZY/SUPERLAZY were re-derived to match the new,
now-meaningful (p50-based) iPositional this produces. Self-play confirms
the working tree beats both HEAD, a more liberal (~5% miss target)
variant, and a more conservative (near-zero miss) variant of the same
margin mechanism.
Along the way, an initial self-play regression (this margin work bundled
with several changes in one pass, one of which broke things) led to a
day of ZFS-snapshot bisection and then targeted DEBUG-build fuzzing
against a stratified 200k-position corpus (tests/cross_sectional_200k.ep_,
new), which is what actually found and fixed four distinct, real,
pre-existing engine bugs -- none of them related to the margin redesign
itself:
- searchsup.c (CommonSearchInit): three early-return paths (timer
abort, draw, too-deep bailout) left a node's own PV[] row stale
instead of clearing it, letting an ancestor's UpdatePV splice a
leftover move from a completely unrelated node into the displayed
PV -- root cause of a WalkPV/MakeMove assert ("moving a piece to
capture, but nothing was on the target square").
- dynamic.c (RecordEnprisePieceAtPly): the two-slot en-prise dedup
compared (square, piece) instead of square alone, so a same-square
re-record with a different piece (legitimate -- a fail-high
capture's victim is restored by UnmakeMove and can differ across
calls at the same ply) shifted a duplicate square into both slots,
tripping ValueOfMaterialInTroubleDespiteMove's two-distinct-squares
assert.
- recogn.c (_RecognizeKBKP): the wrong-colored-bishop corner-fortress
draw shortcut fired even when the weak side has its own pawn(s),
which can hand the strong side real zugzwang resources the fortress
logic doesn't account for -- produced a false hard draw bound where
EGTB shows a near-forced loss.
- eval.c (_EvalPasserRacesAgainstLoneKings): a rook pawn's race was
credited as winning even when the strong side's own king sits
exactly on its own pawn's queening square (distance 0 specifically --
adjacent is fine, the square is empty) -- a classic stalemate
resource the race heuristic can't see. Only reachable through
_RecognizeKPK's pawn-hash-cache-hit path, which is why
testrecogn.c's TestRecognExhaustiveKPK reported "0 wrong" the whole
time; extended it to cover both color assignments and the full board
(no mirror-shortcut) and documented the remaining cache-hit coverage
gap for a future session to close.
Also: match_play.py gains a local_ebf metric (geometric-mean of
depth-normalized node-growth ratios between consecutive completed
iterations) alongside the existing cumulative nodes**(1/depth) ebf,
which turned out to be misleading when comparing engines with
different lazy-exit aggressiveness -- and probe.c/hash.c pick up two
small, independently-found correctness fixes (TB root-probe promotion
piece missing its color bit; ASSERT(g_pHashTable) crashing on the
legitimate "--hash none" config) noticed along the way.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Diffstat (limited to 'src/eval.c')
| -rw-r--r-- | src/eval.c | 2320 |
1 files changed, 1744 insertions, 576 deletions
@@ -352,9 +352,19 @@ static SCORE PASSER_BONUS_AS_MATERIAL_COMES_OFF[32] = 0, 0, 0, 0, 0, 0, 0, 0, }; -SCORE RACER_WINS_RACE = +800; +// 2026-09-17: was +800 -- found via fuzzing that _RecognizeKPK stacks +// material balance and a flat 2*VALUE_PAWN on top of this before +// returning it as a hard recognizer bound (RECOGN_LOWER/UPPER), so a +// "winning" race claim could reach ~988 total even before considering +// whether the claim itself was right. Crafty's equivalent term +// (pawn_can_promote, evaluate.h) is a flat 525 and -- critically -- +// only ever an additive eval nudge feeding real alpha-beta search, +// never a hard bound. Turned down to ~300/pawn regardless of that +// separate soundness fix (see the losing-side-has-no-pawns guard in +// _RecognizeKPK) since the two problems compound independently. +SCORE RACER_WINS_RACE = +300; -static SCORE UNDEVELOPED_MINORS_IN_OPENING[5] = +static SCORE UNDEVELOPED_MINORS_IN_OPENING[5] = {// 0 1 2 3 4 0, -6, -10, -16, -22 }; @@ -2964,38 +2974,131 @@ Return value: #endif // CALIBRATE_POSITIONAL -#if defined(CALIBRATE_BASE_MARGIN) || defined(CALIBRATE_MARGIN_SAFETY) +// 2026-09-17: BASE_MARGIN_MAT_BUCKETS/WIDTH and _MaterialBucket() pulled +// out of the CALIBRATE_BASE_MARGIN/CALIBRATE_MARGIN_SAFETY-only guard +// below -- now also used unconditionally (see g_u64EvalExitByBucket in +// Eval(), and root.c's per-bucket exit-rate report) so a normal build +// can report exit rate by material bucket, not just the flat aggregate +// "Eval exit breakdown" line -- needed to see whether the p98/p2 margin +// redesign actually moved the exit rate in any given bucket, since the +// aggregate rate can mask buckets moving in opposite directions. +#define BASE_MARGIN_MAT_BUCKETS 8 +#define BASE_MARGIN_MAT_WIDTH 8 + +static ULONG +_MaterialBucket(IN POSITION *pos) +/** + +Routine description: + + Coarse combined-material bucket, 0 (bare kings) to + BASE_MARGIN_MAT_BUCKETS-1 (full army). Originally CALIBRATE_BASE_ + MARGIN-only; now also used by the always-on per-bucket eval-exit- + rate counters. + +Parameters: + + POSITION *pos + +Return value: + + ULONG : 0..BASE_MARGIN_MAT_BUCKETS-1 + +**/ +{ + ULONG uCombined = pos->uArmyScaler[WHITE] + pos->uArmyScaler[BLACK]; + return MINU(BASE_MARGIN_MAT_BUCKETS - 1, uCombined / BASE_MARGIN_MAT_WIDTH); +} + +// Always-on per-material-bucket eval-exit counters -- separate from +// (and simpler than) the CALIBRATE_MARGIN_SAFETY machinery below: just +// "how many nodes exited via this tier, in this material bucket", +// enough to compute exit RATE per bucket (this array divided by the +// per-bucket sum across all three) without needing a special +// calibration build. See the five INC-site call pairs in Eval() below +// and root.c's report. +static UINT64 g_u64SuperLazyEvalsByBucket[BASE_MARGIN_MAT_BUCKETS]; +static UINT64 g_u64LazyEvalsByBucket[BASE_MARGIN_MAT_BUCKETS]; +static UINT64 g_u64FullEvalsByBucket[BASE_MARGIN_MAT_BUCKETS]; + +void +DumpEvalExitRateByBucket(void) +/** + +Routine description: + + Print per-material-bucket eval exit rate (super lazy / lazy / full, + as a percentage of that bucket's own total) -- command.c's + "calibrate exitrate". Complements the flat aggregate "Eval exit + breakdown" line (root.c, printed at the end of every search) by + showing whether that aggregate is hiding buckets moving in opposite + directions. + +Parameters: + + void + +Return value: + + void + +**/ +{ + ULONG m; + + Trace("Eval exit rate by material bucket (combined army scaler):\n"); + for (m = 0; m < BASE_MARGIN_MAT_BUCKETS; m++) + { + UINT64 u64SL = g_u64SuperLazyEvalsByBucket[m]; + UINT64 u64L = g_u64LazyEvalsByBucket[m]; + UINT64 u64F = g_u64FullEvalsByBucket[m]; + UINT64 u64Total = u64SL + u64L + u64F; + if (0 == u64Total) + { + Trace(" bucket %u (scaler %u-%u): no samples\n", + m, m * BASE_MARGIN_MAT_WIDTH, + (m * BASE_MARGIN_MAT_WIDTH) + BASE_MARGIN_MAT_WIDTH - 1); + continue; + } + Trace(" bucket %u (scaler %2u-%2u): n=%12" COMPILER_LONGLONG_UNSIGNED_FORMAT + " super=%5.2f%% lazy=%5.2f%% full=%5.2f%%\n", + m, m * BASE_MARGIN_MAT_WIDTH, + (m * BASE_MARGIN_MAT_WIDTH) + BASE_MARGIN_MAT_WIDTH - 1, + u64Total, + 100.0 * (double)u64SL / (double)u64Total, + 100.0 * (double)u64L / (double)u64Total, + 100.0 * (double)u64F / (double)u64Total); + } +} + + +#ifdef CALIBRATE_BASE_MARGIN // // board_representation/EVAL.md section 9: is LAZY_EVAL_BASE_MARGIN (the // flat, un-widened margin Eval() screens with before ever calling -// EstimatePositionalScore) well-sized? Two distinct questions, tracked -// separately, neither requiring the lazy-exit machinery to be disabled +// EstimatePositionalScore) well-sized? Tracks: once the gate passes and +// EstimatePositionalScore has widened the margin, does a real exit +// actually happen, broken out by the worse (max) of the two sides' +// cheap defect-index buckets? This answers "does the 'very dangerous' +// bucket (10) ever actually exit, or does its wide margin effectively +// rule it out". Doesn't require the lazy-exit machinery to be disabled // (unlike the CALIBRATE_POSITIONAL king-safety work above) -- real -// exits still happen normally here, so this runs at normal speed: +// exits still happen normally here, so this runs at normal speed. // -// 1. Once the gate passes and EstimatePositionalScore has widened the -// margin, does a real exit actually happen, broken out by the -// worse (max) of the two sides' cheap defect-index buckets? This -// answers "does the 'very dangerous' bucket (10) ever actually -// exit, or does its wide margin effectively rule it out". -// 2. When the gate comes back *false* (we skip straight to full eval, -// no lazy attempt tried at all) -- would a real exit have been -// available anyway, had we bothered to compute the widened margin? -// A "yes" here is a false negative: LAZY_EVAL_BASE_MARGIN was -// tight enough to force full eval on a node that genuinely could -// have taken the cheap path. +// 2026-09-17: this used to also track a false-negative rate (the gate +// coming back false, then checking whether a real exit would've been +// available anyway) -- that required a flat-gate-false branch, which +// stopped existing once the flat pre-check itself was retired (see the +// `if (TRUE)` removal in Eval() below); removed alongside it rather +// than leaving permanently-zero counters. // // 2026-09-06: added a material dimension -- pos->uArmyScaler[WHITE] + // pos->uArmyScaler[BLACK] (0-62), bucketed into 8 coarse bands, since // the lazy/full cost ratio measured directly (opening/middlegame vs a // bare K+P endgame) varies ~7x by material, and LAZY_EVAL_BASE_MARGIN // is currently one flat constant across all of it. -#define BASE_MARGIN_MAT_BUCKETS 8 -#define BASE_MARGIN_MAT_WIDTH 8 static UINT64 g_uBaseMarginExitAttempted[BASE_MARGIN_MAT_BUCKETS][11]; static UINT64 g_uBaseMarginExitTaken[BASE_MARGIN_MAT_BUCKETS][11]; -static UINT64 g_uBaseMarginFalseNegative[BASE_MARGIN_MAT_BUCKETS]; -static UINT64 g_uBaseMarginCorrectlyFull[BASE_MARGIN_MAT_BUCKETS]; static ULONG _MaxKingDangerBucket(IN SEARCHER_THREAD_CONTEXT *ctx, @@ -3034,30 +3137,6 @@ Return value: } -static ULONG -_MaterialBucket(IN POSITION *pos) -/** - -Routine description: - - CALIBRATE_BASE_MARGIN instrumentation only: coarse combined-material - bucket, 0 (bare kings) to BASE_MARGIN_MAT_BUCKETS-1 (full army). - -Parameters: - - POSITION *pos - -Return value: - - ULONG : 0..BASE_MARGIN_MAT_BUCKETS-1 - -**/ -{ - ULONG uCombined = pos->uArmyScaler[WHITE] + pos->uArmyScaler[BLACK]; - return MINU(BASE_MARGIN_MAT_BUCKETS - 1, uCombined / BASE_MARGIN_MAT_WIDTH); -} - - void DumpBaseMarginCalibration(void) /** @@ -3066,9 +3145,7 @@ Routine description: Print LAZY_EVAL_BASE_MARGIN calibration stats gathered so far this run: per-(material bucket, king-danger bucket) exit-attempted/ - exit-taken counts (command.c's "calibrate basemargin"), and the - false-negative rate of the flat pre-EstimatePositionalScore gate, - broken out by material bucket. + exit-taken counts (command.c's "calibrate basemargin"). Parameters: @@ -3081,7 +3158,6 @@ Return value: **/ { ULONG m, u; - UINT64 u64FN, u64CF; for (m = 0; m < BASE_MARGIN_MAT_BUCKETS; m++) { @@ -3103,14 +3179,6 @@ Return value: (100.0 * (double)g_uBaseMarginExitTaken[m][u] / (double)g_uBaseMarginExitAttempted[m][u])); } - u64FN = g_uBaseMarginFalseNegative[m]; - u64CF = g_uBaseMarginCorrectlyFull[m]; - Trace(" false negatives: %" COMPILER_LONGLONG_UNSIGNED_FORMAT - " correctly full: %" COMPILER_LONGLONG_UNSIGNED_FORMAT - " rate: %.4f%%\n", - u64FN, u64CF, - (u64FN + u64CF > 0) ? - (100.0 * (double)u64FN / (double)(u64FN + u64CF)) : 0.0); } } #endif // CALIBRATE_BASE_MARGIN @@ -3133,11 +3201,29 @@ Return value: static UINT64 g_uMarginSwingMax[BASE_MARGIN_MAT_BUCKETS]; static UINT64 g_uMarginSwingCount[BASE_MARGIN_MAT_BUCKETS]; static UINT64 g_uMarginSwingSum[BASE_MARGIN_MAT_BUCKETS]; +// 2026-09-17: max/avg alone hide the shape of the swing distribution -- +// a bucket could have a huge max from one outlier and a small avg from +// a long thin tail of near-zero swings, or a genuinely fat middle. +// Histogram (same fixed-width-bin technique as RESID_HIST_* above, +// swings are always >= 0 so no negative-offset needed) plus a sum-of- +// squares accumulator lets DumpMarginSafetyCalibration report p10/p50/ +// p90 and stdev per bucket, not just max/avg. +#define SWING_HIST_BIN_WIDTH 10 +#define SWING_HIST_BINS 300 // covers [0, 3000) cp; observed max so far ~1300 +static UINT64 g_uMarginSwingHist[BASE_MARGIN_MAT_BUCKETS][SWING_HIST_BINS]; +static UINT64 g_uMarginSwingSumSq[BASE_MARGIN_MAT_BUCKETS]; // Genuine soundness check: did the swing ever exceed the *actual* // (already-widened) margin that was used to justify this specific // exit? Unlike the raw max above (which just says "how far things // moved"), this directly answers "was any real exit actually unsound". static UINT64 g_uMarginExceeded[BASE_MARGIN_MAT_BUCKETS]; +// 2026-09-17: how many exceeded-margin FENs have already been printed +// for this (tier, material bucket) -- capped at MARGIN_EXCEEDED_FEN_ +// SAMPLE_CAP so a run with hundreds of thousands of exceeded events +// (a real possibility while tuning loose margins) doesn't flood the +// log; just enough examples per bucket to eyeball by hand. +#define MARGIN_EXCEEDED_FEN_SAMPLE_CAP 5 +static UINT64 g_uMarginExceededFenSamples[BASE_MARGIN_MAT_BUCKETS]; // Same four counters, kept in a fully separate set for the super-lazy // exit (eval.c's material-only check before the regular lazy gate). @@ -3148,27 +3234,56 @@ static UINT64 g_uMarginExceeded[BASE_MARGIN_MAT_BUCKETS]; static UINT64 g_uSuperLazyMarginSwingMax[BASE_MARGIN_MAT_BUCKETS]; static UINT64 g_uSuperLazyMarginSwingCount[BASE_MARGIN_MAT_BUCKETS]; static UINT64 g_uSuperLazyMarginSwingSum[BASE_MARGIN_MAT_BUCKETS]; +static UINT64 g_uSuperLazyMarginSwingHist[BASE_MARGIN_MAT_BUCKETS][SWING_HIST_BINS]; +static UINT64 g_uSuperLazyMarginSwingSumSq[BASE_MARGIN_MAT_BUCKETS]; static UINT64 g_uSuperLazyMarginExceeded[BASE_MARGIN_MAT_BUCKETS]; +static UINT64 g_uSuperLazyMarginExceededFenSamples[BASE_MARGIN_MAT_BUCKETS]; static void _RecordMarginSafetySwingInto(IN OUT UINT64 *puMax, IN OUT UINT64 *puCount, IN OUT UINT64 *puSum, + IN OUT UINT64 *puSumSq, + IN OUT UINT64 puHist[][SWING_HIST_BINS], IN OUT UINT64 *puExceeded, + IN OUT UINT64 *puFenSamples, + IN CHAR *szTierLabel, IN POSITION *pos, IN SCORE iSwing, IN SCORE iActualMarginUsed) { ULONG uMatBucket = _MaterialBucket(pos); + ULONG uHistBin = MINU(SWING_HIST_BINS - 1, + (ULONG)iSwing / SWING_HIST_BIN_WIDTH); if (iSwing > iActualMarginUsed) { puExceeded[uMatBucket]++; + // 2026-09-17: capped FEN sampler, a few examples per (tier, + // bucket) to eyeball by hand -- generalized from a 2026-09-16 + // one-off (super-lazy bucket 0 alone) that first found a real + // eval bug this way ("in/out of action"). Cheap: PositionToFen + // only runs MARGIN_EXCEEDED_FEN_SAMPLE_CAP times per bucket for + // the life of the process, not once per exceeded event. + if (puFenSamples[uMatBucket] < MARGIN_EXCEEDED_FEN_SAMPLE_CAP) + { + CHAR *pszOutlierFen = PositionToFen(pos); + if (NULL != pszOutlierFen) + { + puFenSamples[uMatBucket]++; + Trace("MARGINSAFETY_EXCEEDED tier=%s bucket=%u swing=%d margin=%d fen=%s\n", + szTierLabel, uMatBucket, (int)iSwing, + (int)iActualMarginUsed, pszOutlierFen); + SystemFreeMemory(pszOutlierFen); + } + } } ASSERT(iSwing >= 0); puCount[uMatBucket]++; puSum[uMatBucket] += (UINT64)iSwing; + puSumSq[uMatBucket] += (UINT64)iSwing * (UINT64)iSwing; + puHist[uMatBucket][uHistBin]++; if ((UINT64)iSwing > puMax[uMatBucket]) { puMax[uMatBucket] = (UINT64)iSwing; @@ -3203,7 +3318,9 @@ Return value: **/ { _RecordMarginSafetySwingInto(g_uMarginSwingMax, g_uMarginSwingCount, - g_uMarginSwingSum, g_uMarginExceeded, + g_uMarginSwingSum, g_uMarginSwingSumSq, + g_uMarginSwingHist, g_uMarginExceeded, + g_uMarginExceededFenSamples, "lazy", pos, iSwing, iActualMarginUsed); } @@ -3235,24 +3352,151 @@ Return value: _RecordMarginSafetySwingInto(g_uSuperLazyMarginSwingMax, g_uSuperLazyMarginSwingCount, g_uSuperLazyMarginSwingSum, + g_uSuperLazyMarginSwingSumSq, + g_uSuperLazyMarginSwingHist, g_uSuperLazyMarginExceeded, + g_uSuperLazyMarginExceededFenSamples, + "superlazy", pos, iSwing, iActualMarginUsed); } +// Plain Newton's-method sqrt, no <math.h> -- chess.h's own VALUE_KING +// #defines INFINITY as a board-eval sentinel, which collides with +// <math.h>'s own INFINITY macro, so libm's sqrt() isn't available +// here without one shadowing the other. Variance is always small +// (swings are bounded well under 2^31), so a fixed handful of Newton +// iterations from a decent seed converges to full double precision. +static double +_Sqrt(IN double d) +{ + double dGuess; + int i; + + if (d <= 0.0) + { + return 0.0; + } + dGuess = d; + for (i = 0; i < 40; i++) + { + dGuess = 0.5 * (dGuess + (d / dGuess)); + } + return dGuess; +} + + +// 2026-09-17: same idea as _PersistRawHistogramsToFile above, for the +// swing histograms -- lets multiple sharded runs (e.g. N chunks of a +// large position pool run in parallel on separate cores) be combined +// after the fact into one true aggregate: sum count/exceeded/sum/ +// sumSq/histogram bins bucket-by-bucket across shard files, then +// recompute mean/stdev/percentiles from the combined totals. Doing +// this from raw counts is required for correctness -- percentiles +// (p10/p50/p90) can't be validly averaged across shards, only derived +// fresh from the combined histogram. +static void +_PersistMarginSwingHistogramToFile(IN FILE *f, IN CHAR *szTierLabel, + IN UINT64 *puCount, IN UINT64 *puExceeded, + IN UINT64 *puSum, IN UINT64 *puSumSq, + IN UINT64 *puMax, + IN UINT64 puHist[][SWING_HIST_BINS]) +{ + ULONG b, i; + + for (b = 0; b < BASE_MARGIN_MAT_BUCKETS; b++) + { + fprintf(f, "%s %u %" COMPILER_LONGLONG_UNSIGNED_FORMAT " %" + COMPILER_LONGLONG_UNSIGNED_FORMAT " %" + COMPILER_LONGLONG_UNSIGNED_FORMAT " %" + COMPILER_LONGLONG_UNSIGNED_FORMAT " %" + COMPILER_LONGLONG_UNSIGNED_FORMAT, + szTierLabel, b, puCount[b], puExceeded[b], puSum[b], + puSumSq[b], puMax[b]); + for (i = 0; i < SWING_HIST_BINS; i++) + { + fprintf(f, " %llu", (unsigned long long)puHist[b][i]); + } + fprintf(f, "\n"); + } +} + + +void +PersistMarginSafetyHistogramsToFile(IN CHAR *szPathOverride) +/** + +Routine description: + + Write both tiers' swing histograms (plus count/exceeded/sum/sumSq/ + max per bucket) to a scratch file, in a format a small external + script can sum bucket-by-bucket across multiple shard runs and + recompute exact combined statistics from. See the comment on + _PersistMarginSwingHistogramToFile above. + +Parameters: + + CHAR *szPathOverride : output path, or NULL to use the default + (/var/tmp/typhoon/marginsafety_raw_histograms.txt) -- pass a + distinct path per shard when running N copies of a large + position pool in parallel, since they'd otherwise all clobber + the same default file. + +Return value: + + void + +**/ +{ + FILE *f; + CHAR *szPath = (NULL != szPathOverride) ? szPathOverride : + "/var/tmp/typhoon/marginsafety_raw_histograms.txt"; + + f = fopen(szPath, "w"); + if (NULL == f) + { + Trace("PersistMarginSafetyHistogramsToFile: could not open output file %s.\n", + szPath); + return; + } + fprintf(f, "# SWING_HIST_BINS=%d SWING_HIST_BIN_WIDTH=%d SWING_HIST_MIN=0\n", + SWING_HIST_BINS, SWING_HIST_BIN_WIDTH); + fprintf(f, "# format: <tier> <bucket> <count> <exceeded> <sum> <sumSq> " + "<max> <bin0> <bin1> ... <binN-1>\n"); + _PersistMarginSwingHistogramToFile(f, "lazy", g_uMarginSwingCount, + g_uMarginExceeded, g_uMarginSwingSum, + g_uMarginSwingSumSq, g_uMarginSwingMax, + g_uMarginSwingHist); + _PersistMarginSwingHistogramToFile(f, "superlazy", + g_uSuperLazyMarginSwingCount, + g_uSuperLazyMarginExceeded, + g_uSuperLazyMarginSwingSum, + g_uSuperLazyMarginSwingSumSq, + g_uSuperLazyMarginSwingMax, + g_uSuperLazyMarginSwingHist); + fclose(f); + Trace("Raw margin-safety histograms written to %s\n", szPath); +} + + static void _DumpMarginSafetyCalibrationInto(IN CHAR *szLabel, IN UINT64 *puMax, IN UINT64 *puCount, IN UINT64 *puSum, + IN UINT64 *puSumSq, IN UINT64 *puExceeded) { ULONG m; - Trace("%s -- max/avg |real - lazy| swing among exits taken, " + Trace("%s -- |real - lazy| swing among exits taken, " "by material bucket:\n", szLabel); for (m = 0; m < BASE_MARGIN_MAT_BUCKETS; m++) { + double dMean; + double dVariance; + double dStdev; + if (0 == puCount[m]) { Trace(" material bucket %u (scaler %u-%u): no exits taken\n", @@ -3260,25 +3504,42 @@ _DumpMarginSafetyCalibrationInto(IN CHAR *szLabel, (m * BASE_MARGIN_MAT_WIDTH) + BASE_MARGIN_MAT_WIDTH - 1); continue; } + dMean = (double)puSum[m] / (double)puCount[m]; + // E[X^2] - E[X]^2; clamp to 0 to guard against fp rounding + // producing a tiny negative just under zero. + dVariance = ((double)puSumSq[m] / (double)puCount[m]) - + (dMean * dMean); + if (dVariance < 0.0) + { + dVariance = 0.0; + } + dStdev = _Sqrt(dVariance); + // 2026-09-17: no longer printing p10/p50/p90 here -- percentiles + // don't combine validly across sharded runs (see + // PersistMarginSafetyHistogramsToFile), so a single shard's + // in-process percentile read stopped being the useful number + // once the workflow moved to persisting raw histogram bins and + // computing exact combined percentiles externally instead. Trace(" material bucket %u (scaler %u-%u): max=%" - COMPILER_LONGLONG_UNSIGNED_FORMAT " avg=%.1f (n=%" + COMPILER_LONGLONG_UNSIGNED_FORMAT " avg=%.1f stdev=%.1f " + "(n=%" COMPILER_LONGLONG_UNSIGNED_FORMAT ") exceeded=%" COMPILER_LONGLONG_UNSIGNED_FORMAT "\n", m, m * BASE_MARGIN_MAT_WIDTH, (m * BASE_MARGIN_MAT_WIDTH) + BASE_MARGIN_MAT_WIDTH - 1, - puMax[m], (double)puSum[m] / (double)puCount[m], + puMax[m], dMean, dStdev, puCount[m], puExceeded[m]); } } void -DumpMarginSafetyCalibration(void) +DumpMarginSafetyCalibration(IN CHAR *szHistogramPathOverride) /** Routine description: - Print, per material bucket, the max and average |real - lazy| swing + Print, per material bucket, the max/avg/stdev |real - lazy| swing observed among nodes that actually took a lazy exit this run (command.c's "calibrate marginsafety") -- once for the regular lazy exit (LAZY_EVAL_BASE_MARGIN / EstimatePositionalScore) and once for @@ -3287,11 +3548,14 @@ Routine description: two together would hide whichever one is actually unsound. Either section's max is the number that answers "how large would that margin need to be, at that material level, before an exit there - could have been unsound." + could have been unsound." Also persists the raw swing histograms + to file (see PersistMarginSafetyHistogramsToFile) so percentiles + can be computed exactly, including across multiple sharded runs. Parameters: - void + CHAR *szHistogramPathOverride : forwarded to + PersistMarginSafetyHistogramsToFile -- NULL for the default path. Return value: @@ -3301,16 +3565,314 @@ Return value: { _DumpMarginSafetyCalibrationInto("Regular lazy margin safety", g_uMarginSwingMax, g_uMarginSwingCount, - g_uMarginSwingSum, g_uMarginExceeded); + g_uMarginSwingSum, g_uMarginSwingSumSq, + g_uMarginExceeded); _DumpMarginSafetyCalibrationInto("Super lazy margin safety", g_uSuperLazyMarginSwingMax, g_uSuperLazyMarginSwingCount, g_uSuperLazyMarginSwingSum, + g_uSuperLazyMarginSwingSumSq, g_uSuperLazyMarginExceeded); + PersistMarginSafetyHistogramsToFile(szHistogramPathOverride); } #endif // CALIBRATE_MARGIN_SAFETY +// 2026-09-16/17: shared, file-scope tables for the margin_a/margin_b +// (super-lazy, in Eval() below) and margin_a'/margin_b' +// (EstimatePositionalScore, regular lazy, right after this) margin +// constructions. King safety is ARMY-ONLY for both tiers (no +// CountKingSafetyDefects call from either -- a joint attacker-army x +// defect-count table was considered for regular lazy specifically, but +// dropped: CountKingSafetyDefects is slow despite being bitboard-based, +// not worth paying for if the simpler table is already good enough; +// it stays in the codebase for its existing, unrelated search +// extension/reduction-gate use). Residual is NOT shared between tiers, +// despite living together here -- iResidualP2/98ByOwnArmy is scoped +// to regular lazy's own baseline (after bishop pairs: excludes pawns/ +// passers/bad-trade/bishop-pairs, since regular lazy already has all +// of that by the time it asks); iSuperLazyResidualP2/98ByOwnArmy is +// scoped to super-lazy's own baseline (pure material: INCLUDES pawns/ +// passers/bad-trade/bishop-pairs, since super-lazy has none of it yet). +// Using the regular-lazy-scoped table for super-lazy was a real bug in +// the first version of this redesign -- it silently excluded exactly +// the terms super-lazy needed most and got quietly papered over with a +// MAX() against the old flat floor, which defeated the whole point of +// building a position-specific estimate. Fixed by measuring the right +// thing instead of re-adding a workaround. +// +// Speed/accuracy knob: measured exceeded rate (fraction of lazy/ +// super-lazy exits where the real full-eval score actually would have +// landed back across alpha/beta -- i.e. this lazy exit was WRONG) by +// percentile choice, both from the same 30k-position stratified pool, +// same `sd 6` conditions, via CALIBRATE_MARGIN_SAFETY's `calibrate +// marginsafety`. Bucket numbering here is the MATERIAL bucket +// (_MaterialBucket: combined non-pawn army scaler for BOTH sides, +// divided by 8 -- 0-7) -- NOT the same bucketing as the per-side +// army-scaler/4 buckets the percentile tables below are keyed by. +// Buckets 0-1 never take a regular-lazy exit at all (see +// LAZY_EVAL_MIN_MATERIAL) so have no regular-lazy row. +// +// material bucket: 2 3 4 5 6 7 +// (combined scaler) (16-23) (24-31) (32-39) (40-47) (48-55) (56-63) +// ----------------------- ------- ------- ------- ------- ------- ------- +// regular lazy, p90/p10: 0.97% 0.48% 0.72% 1.61% 1.54% 0.72% +// regular lazy, p98/p2: 0.046% 0.013% 0.010% 0.029% 0.032% 0.041% +// +// material bucket: 1 2 3 4 5 6 7 +// (combined scaler) (8-15) (16-23) (24-31) (32-39) (40-47) (48-55) (56-63) +// ----------------------- ------- ------- ------- ------- ------- ------- ------- +// super lazy, p90/p10: 9.43% 5.08% 3.73% 1.32% 1.14% 1.99% 0.54% +// super lazy, p98/p2: 1.08% 0.349% 0.209% 0.045% 0.063% 0.069% 0.018% +// +// (Super-lazy has no material-bucket-0 row: excluded entirely, see the +// bucket-0 comment at its exclusion gate below.) +// +// 2026-09-17 re-recalibration: switched from p90/p10 to p98/p2 given +// the above -- p90/p10 exceeded on a non-trivial fraction of exits +// (regular lazy: 0.5-1.6% across buckets; super-lazy: 0.5-9.4%, worst +// in the low-material buckets), p98/p2 brings every bucket in both +// tiers under 1.1%, an order of magnitude tighter. If p98/p2 still +// isn't tight enough once self-play validates it, p99/p1 is the next +// lever -- re-derive via `calibrate residual` against the same 30k +// pool, no new position sample needed. +// All four tables below are FINAL, derived from the same 30k-position +// stratified pool (~104M-240M samples/bucket for residual/king, +// buckets keyed by scaler/4). Full percentile spread recorded so a +// future re-tightening (e.g. p99/p1) can read values straight off +// these charts without re-running the 30k pool -- only the two +// _P2By.../_P98By... lines actually feed the margin construction +// below; min/p1/p5/p10/p50/p90/p95/p99/max are kept for reference. +// +// Residual by OWN army-scaler bucket (regular-lazy baseline: excludes +// pawns/passers/bad-trade/bishop-pairs): +// bucket 0 (scaler 0-3): n=104187433 min=-144 p1=-30 p2=-10 p5=-10 p10=-10 p50=10 p90=110 p95=130 p98=190 p99=230 max=767 +// bucket 1 (scaler 4-7): n=125821895 min=-166 p1=-10 p2=-10 p5=-10 p10=10 p50=50 p90=150 p95=210 p98=250 p99=290 max=817 +// bucket 2 (scaler 8-11): n=239860406 min=-174 p1=-30 p2=-30 p5=-10 p10=-10 p50=30 p90=130 p95=170 p98=230 p99=270 max=761 +// bucket 3 (scaler 12-15): n=190456720 min=-209 p1=-50 p2=-30 p5=-30 p10=-10 p50=30 p90=110 p95=130 p98=190 p99=230 max=708 +// bucket 4 (scaler 16-19): n=216350902 min=-220 p1=-70 p2=-70 p5=-50 p10=-30 p50=30 p90=90 p95=110 p98=150 p99=170 max=735 +// bucket 5 (scaler 20-23): n=189558383 min=-225 p1=-70 p2=-50 p5=-30 p10=-30 p50=30 p90=70 p95=90 p98=130 p99=150 max=728 +// bucket 6 (scaler 24-27): n=135722645 min=-231 p1=-90 p2=-70 p5=-50 p10=-30 p50=10 p90=70 p95=90 p98=110 p99=130 max=760 +// bucket 7 (scaler 28-31): n=164358044 min=-256 p1=-110 p2=-110 p5=-90 p10=-90 p50=-30 p90=30 p95=50 p98=70 p99=90 max=796 +// 2026-09-17 BEST-GUESS #2: guess #1 (p95/p5 uniformly) measured +// 0.002-0.270% on a 500-position sample (buckets 2-7) -- still under +// the ~0.4% target, especially buckets 4-5 (0.002% each). Per direct +// instruction, loosened buckets 2-5 one more step to p90/p10 (values +// read off the chart above, no new run); buckets 6-7 already looked +// close to target at p95/p5 (0.270%/0.152%) and are left unchanged. +// Buckets 0-1 never take a regular-lazy exit at all (LAZY_EVAL_MIN_ +// MATERIAL) so their values are moot either way. Caveat: the 500- +// position sample is noisy (small event counts in low buckets, and +// not truly 500 independent draws -- a single game's tree can dominate +// a material bucket's node population), so this is still a guess +// pending the next full 30k-position `calibrate marginsafety` run. +// 2026-09-17 BEST-GUESS #3: real 30k-pool re-check of guess #2 (not +// the noisy 500-position sample) measured bucket2=0.385% (right on +// target) but buckets 3-7 all 0.048-0.099%, still well under the +// 0.4-0.5% target -- loosened further to p85/p15 (exact values from +// the persisted raw histogram, /var/tmp/typhoon/residual_raw_ +// histograms.txt -- see hist_percentile.py). Bucket 2 left unchanged +// (already at target). +// 2026-09-17 BEST-GUESS #4: buckets 0-2 left at guess #3 (0.06-0.39% +// measured, buckets 0-1 take no lazy exits so moot). Buckets 3-7 +// measured 0.048-0.099%, well under the ~0.4-0.5% target -- loosened +// one more notch (values read off the persisted 30k histogram via +// hist_percentile.py, no new sample needed): bucket 3 p85/p15 -> +// p80/p20, bucket 4 p85/p15 -> p80(tie)/p20, bucket 5 p85/p15 -> +// p85(tie)/p20, bucket 6 p85/p15 -> p75/p25, bucket 7 p90/p20-ish -> +// p75/p25 (P98 only, P2 already at its floor). Re-verify with a full +// `calibrate marginsafety` pass -- not expected to land exactly on +// target given cross-bucket confounds (see guess #3 comment above). +// 2026-09-17 BEST-GUESS #5: buckets 3-5 loosened one more notch +// (p80/p20-ish -> p70/p30, values from the persisted 30k histogram). +// Bucket 0-2, 6-7 unchanged pending their own re-check (guess #4's +// 30k run showed bucket 2 already over target without being touched, +// and buckets 6-7 already over target from guess #4's loosening -- +// see command history). +// 2026-09-17 BEST-GUESS #6: targeting ~0.66% in every bucket. Bucket 2 +// was drifting up untouched (0.72% guess #4 -> 1.04% guess #5) -- +// tightened p90/p10-ish -> p80/p20. Bucket 5 was under target (0.39%) +// -- loosened a tad, p70/p30 -> p90/p30 (P98 side only). +// 2026-09-17 BEST-GUESS #7: targeting ~0.66% in every bucket. +// Bucket 2 (1.62%, way over) -- guess #6's "tighten" actually +// narrowed the margin by mistake (tighten = lower exceeded rate = +// WIDEN the percentile spread, not narrow it); corrected direction, +// widened past guess #5's own (-10,130) to (-30,190) i.e. p97/p3. +// Buckets 3/4/6 (0.71-0.74%, slightly over) widened a little (P98 +// +20 each). Bucket 5 (0.30%, under) narrowed back (P98 70->50, +// reverting guess #6's accidental widen). Bucket 7 (0.47%, under) +// narrowed a little (P2 -70->-50). +// 2026-09-17 BEST-GUESS #8: buckets 3-5 came in well under target +// (0.27-0.39% vs ~0.66%) -- narrowed further for speed (guess #5's +// bucket 5 narrow was too small a step, barely moved anything; +// bigger step this time). Bucket 3: p75/p30 -> p60/p40. Bucket 4: +// P98 only, p75/p40 -> p55/p40 (P2 unchanged, bin ties at that end). +// Bucket 5: p65/p35-ish -> p55/p45, moved both sides this time. +// Buckets 0-2/6-7 untouched pending their own re-check. +// 2026-09-18: restored to the guess-#11 values (11 rounds of +// calibration against the 30k-position pool and the 200k-position +// cross-sectional run, see the guess #1-11 history above) after +// briefly suspecting -- and ruling out, via the 16:15 ZFS snapshot +// still showing a winning match_play series with guesses #9/#10 +// already applied -- that this table's loosening was the source of +// the 2026-09-17 self-play regression. The real cause was util.c's +// WalkPV/MakeUserMove change (reverted separately). +static const SCORE iResidualPessimisticByOwnArmy[8] = +{ + -10, -10, -10, 10, -10, 30, -10, -50, +}; +static const SCORE iResidualOptimisticByOwnArmy[8] = +{ + 130, 210, 170, 50, 70, 30, 50, -10, +}; +// Super-lazy-scoped residual by OWN army-scaler bucket (material-only +// baseline: includes pawns/passers/bad-trade/bishop-pairs): +// bucket 0 (scaler 0-3): n=104187433 min=-363 p1=-110 p2=-90 p5=-50 p10=-30 p50=30 p90=210 p95=290 p98=410 p99=530 max=2383 +// bucket 1 (scaler 4-7): n=125821895 min=-356 p1=-70 p2=-50 p5=-30 p10=-10 p50=90 p90=290 p95=370 p98=490 p99=830 max=2456 +// bucket 2 (scaler 8-11): n=239860406 min=-341 p1=-70 p2=-50 p5=-30 p10=-10 p50=70 p90=250 p95=330 p98=430 p99=530 max=2065 +// bucket 3 (scaler 12-15): n=190456720 min=-324 p1=-70 p2=-50 p5=-30 p10=-10 p50=70 p90=230 p95=290 p98=390 p99=450 max=2183 +// bucket 4 (scaler 16-19): n=216350902 min=-343 p1=-90 p2=-70 p5=-50 p10=-30 p50=50 p90=190 p95=250 p98=310 p99=370 max=1946 +// bucket 5 (scaler 20-23): n=189558383 min=-299 p1=-70 p2=-50 p5=-30 p10=-10 p50=70 p90=170 p95=230 p98=290 p99=330 max=1959 +// bucket 6 (scaler 24-27): n=135722645 min=-322 p1=-90 p2=-70 p5=-50 p10=-30 p50=50 p90=150 p95=190 p98=250 p99=290 max=1921 +// bucket 7 (scaler 28-31): n=164358044 min=-304 p1=-90 p2=-90 p5=-70 p10=-50 p50=10 p90=90 p95=130 p98=170 p99=210 max=2039 +// 2026-09-17 BEST-GUESS #3: real 30k-pool re-check of guess #2 revealed +// the 500-position sample had misled guess #2 in the dangerous +// direction on two buckets: material bucket 1 (reverted to p98/p2 +// based on a 500-position reading of 0.035%) actually measured 1.13% +// at full scale, and material bucket 3 (table never touched at all) +// measured 0.94% -- a real cross-bucket confound (changing OTHER +// buckets' margins shifts real search behavior/tree shape everywhere, +// so even an untouched bucket's measured exceeded rate moves). Per- +// side buckets 0-3 tightened to p99/p1 (exact values from the +// persisted raw histogram) to address both -- material buckets 1 and 3 +// are populated by a diffuse mix of per-side bucket combinations, not +// a clean 1:1 mapping, so this can't precisely target either bucket +// alone, only reduce risk broadly in the low-army region feeding both. +// Buckets 4-6 measured 0.22-0.30%, still under target -- loosened +// further to p85/p15. Bucket 7 (0.15%) and the king table (negligible +// spread above p90 in every bucket, confirmed against the persisted +// histogram) left unchanged. Real search-tree-shape changes are +// expected from this (not just a measurement artifact) -- re-verify +// with a full `calibrate marginsafety` pass, best-effort, not +// expected to land exactly on target for every bucket given the +// diffuse material-bucket-to-per-side-bucket mapping. +// 2026-09-17 BEST-GUESS #4: bucket 1 was the worst outlier at 1.13% +// measured (target ~0.4-0.5%) -- tightened one more notch past p99/p1, +// to p99.5/p0.5 (values from the persisted 30k histogram). Buckets 0 +// and 2-3 left at guess #3 (bucket 3 measured 0.94%, also over target, +// but a diffuse material-bucket confound with bucket 1 -- see guess #3 +// comment above -- so left alone pending its own re-check rather than +// guessing a second simultaneous change). Buckets 4-7 measured +// 0.15-0.30%, well under target -- loosened one more notch: buckets +// 4-6 p85/p15 -> p80/p20, bucket 7 p90/p10 -> p85/p15. +// 2026-09-17 BEST-GUESS #5: bucket 3 was over target (0.94% guess #3, +// 1.23% guess #4, moving the wrong direction despite never being +// touched -- a cross-bucket confound from bucket 1's changes) -- +// tightened past p99/p1 to p99.5/p0.5, same lever used for bucket 1. +// Bucket 7 was still under target (0.15%/0.29%) -- loosened one more +// notch, p85/p15 -> p75/p25 (P98 side only; P2 already at p85's floor). +// 2026-09-17 BEST-GUESS #6: targeting ~0.66% in every bucket. Bucket +// 1 (0.458%, under target) was at p99.5/p0.5 -- backed off a tiny bit +// to p99/p1. Bucket 3 (1.11%, worst outlier, already at p99.5/p0.5) +// has no looser-percentile lever left in this histogram's resolution +// -- padded a fixed +120/-20 beyond p99.5/p0.5 instead. Bucket 6 +// (0.96%, over target, was p80/p20) tightened to p90/p10. Bucket 7 +// (0.36%, under target, was p75/p25) loosened a tad, P2 side only +// (p25 -> p30-ish). +// 2026-09-17 BEST-GUESS #7: targeting ~0.66% in every bucket. +// Bucket 1 (0.49%, slightly under) narrowed a little (P2 -70->-50). +// Buckets 2/4/6 (0.68/0.61/0.71%) already close enough -- left +// alone. Bucket 3 (1.09%, still worst, already past p99.5) widened +// further via a bigger pad (-110,650) -> (-150,850). Buckets 5/7 +// (0.54%/0.56%, slightly under) narrowed a little (P98 -20 each). +// 2026-09-17 BEST-GUESS #9: same 200k/sd3 cross-sectional finding as +// the regular-lazy table above, new target 1.5%. Bucket 2 (2.99%, +// worst of all despite never being touched before -- already sitting +// at p99/p0.5, percentile table exhausted) padded further, same lever +// used for bucket 3 previously. Bucket 3 (1.95%, least-bad of these +// three but still over, already heavily padded) padded a bit more. +// Bucket 6 (2.19%) widened p90/p10 -> p96/p6. +// 2026-09-17 BEST-GUESS #10: target ~1-1.5%. Bucket 1 (0.34%, well +// under) loosened, p99/p2-ish -> ~p98/p4. Bucket 2 (3.45%, worst -- +// got WORSE after widening in guess #9 despite already sitting past +// p99.5) padded further again regardless; this bucket has resisted +// two widening passes in a row, likely the same cross-bucket-combo +// issue diagnosed for bucket 3 earlier (material bucket != per-side +// bucket, diffuse mapping) rather than something this table alone can +// fix -- flagging for a real root-cause dig rather than more blind +// widening if this doesn't help either. Buckets 3-7 left alone. +// 2026-09-17 BEST-GUESS #11: bucket 2 (2.97%, still worst after three +// widening rounds in a row -- see guess #10's note, likely a +// cross-bucket-combo issue this table alone can't fully fix) padded +// further again. Rest left alone. +// 2026-09-18: restored to the guess-#11 values (see the matching note +// on iResidualPessimisticByOwnArmy above) after ruling out this +// table's loosening as the cause of the 2026-09-17 self-play +// regression -- the 16:15 ZFS snapshot already had guesses #9/#10 +// applied and was still winning. The real cause was util.c's WalkPV/ +// MakeUserMove change (reverted separately). +static const SCORE iSuperLazyResidualPessimisticByOwnArmy[8] = +{ + -110, -30, -190, -170, 10, 10, -50, -10, +}; +static const SCORE iSuperLazyResidualOptimisticByOwnArmy[8] = +{ + 530, 490, 1050, 950, 150, 110, 190, 40, +}; +// King safety by ATTACKER army-scaler bucket: +// bucket 0 (scaler 0-3): n=104187433 min=-68 p1=-10 p2=10 p5=10 p10=10 p50=10 p90=30 p95=30 p98=50 p99=50 max=165 +// bucket 1 (scaler 4-7): n=125821895 min=0 p1=10 p2=10 p5=10 p10=10 p50=10 p90=30 p95=30 p98=50 p99=50 max=162 +// bucket 2 (scaler 8-11): n=239860406 min=-89 p1=-30 p2=-30 p5=-30 p10=-10 p50=10 p90=10 p95=10 p98=30 p99=30 max=162 +// bucket 3 (scaler 12-15): n=190456720 min=-176 p1=-70 p2=-70 p5=-50 p10=-50 p50=-10 p90=-10 p95=10 p98=10 p99=10 max=137 +// bucket 4 (scaler 16-19): n=216350902 min=-307 p1=-190 p2=-170 p5=-130 p10=-110 p50=-30 p90=-10 p95=-10 p98=-10 p99=10 max=129 +// bucket 5 (scaler 20-23): n=189558383 min=-354 p1=-250 p2=-230 p5=-190 p10=-130 p50=-50 p90=-30 p95=-10 p98=-10 p99=-10 max=45 +// bucket 6 (scaler 24-27): n=135722645 min=-401 p1=-270 p2=-250 p5=-170 p10=-150 p50=-70 p90=-30 p95=-10 p98=-10 p99=-10 max=13 +// bucket 7 (scaler 28-31): n=164358044 min=-394 p1=-190 p2=-170 p5=-130 p10=-90 p50=-50 p90=-10 p95=-10 p98=-10 p99=10 max=6 +static const SCORE iKingP2ByAttackerArmy[8] = +{ + 10, 10, -30, -70, -170, -230, -250, -170, +}; +static const SCORE iKingP98ByAttackerArmy[8] = +{ + 50, 50, 30, 10, -10, -10, -10, -10, +}; + +// 2026-09-17: p50 tables, same 30k-position pool, same bucketing as +// their P2/P98 siblings above -- but used for a different purpose: +// a signed, unbounded (well, table-bounded) *typical-value estimate* +// of that side's own missing positional terms, fed to QSearch's +// futility margin as iPositional[side] on a lazy/super-lazy exit +// (matches full-eval's own iPositional convention exactly -- see +// Eval()'s "Adjust dynamic positional component" block below, which +// computes a side's *own* real positional score minus its material, +// not a relative-to-opponent quantity). This is deliberately NOT the +// p98/p2 pessimistic/optimistic pairing margin_a/margin_b use -- +// p50 is the right statistic for a bias correction, p98/p2 is the +// right statistic for a soundness bound. iPositional[color] = +// iKingP50ByAttackerArmyOnly[opponent's army bucket] (opponent is the +// attacker of this side's own king) + iResidualP50ByOwnArmy[this +// side's own army bucket] (or the super-lazy-scoped sibling, for the +// super-lazy exit). +// residual p50 (regular-lazy baseline), by OWN army bucket: +// 10, 50, 30, 30, 30, 30, 10, -30 +// super-lazy-scoped residual p50 (material-only baseline), by OWN army bucket: +// 30, 90, 70, 70, 50, 70, 50, 10 +// king safety p50, by ATTACKER army bucket: +// 10, 10, 10, -10, -30, -50, -70, -50 +static const SCORE iResidualP50ByOwnArmy[8] = +{ + 10, 50, 30, 30, 30, 30, 10, -30, +}; +static const SCORE iSuperLazyResidualP50ByOwnArmy[8] = +{ + 30, 90, 70, 70, 50, 70, 50, 10, +}; +static const SCORE iKingP50ByAttackerArmy[8] = +{ + 10, 10, 10, -10, -30, -50, -70, -50, +}; + + static void EstimatePositionalScore(IN SEARCHER_THREAD_CONTEXT *ctx, IN POSITION *pos, @@ -3325,16 +3887,15 @@ Routine description: the alpha/beta margins to account for the positional terms that have not been computed yet at this point in Eval() -- king safety plus a flat residual covering mobility, passers, and everything - else that genuinely requires attack-generation to know exactly. + else that genuinely requires full evaluation to find out. Both terms below are calibrated from measured data (CALIBRATE_ - POSITIONAL instrumentation, ~1.6M full-eval samples over an ECM - slice), not guessed: for each value, p90 of the *actual* score - swing between this point in Eval() and full-eval completion, so - the resulting margin is wrong (too narrow) on at most ~10% of - calls, in either bucket. See conversation history for the - percentile tables -- if re-tuning, regenerate them, don't hand- - edit these numbers. + POSITIONAL instrumentation, 200M full-eval samples: for each + value, p90 of the *actual* score swing between this point in + Eval() and full-eval completion, so the resulting margin is wrong + (too narrow) on at most ~10% of calls, in either bucket. See + conversation history for the percentile tables -- if re-tuning, + regenerate them, don't hand- edit these numbers. Note: king safety and "everything else" turned out to be close to independent of piece count and of each other, so summing their @@ -3366,125 +3927,103 @@ Return value: **/ { - // p90 of the real, unscaled |KING_SAFETY_BY_COUNTER| magnitude - // _EvalKing computes, indexed per-side by MINU(10, - // CountKingSafetyDefects(side)). + // 2026-09-16 redesign: replaces the old symmetric "sum both sides' + // unscaled p90 magnitude, rescale by attacker army at runtime" + // construction entirely -- that mechanism pooled the p90 fit + // across every material level for a given CountKingSafetyDefects + // bucket, then linearly rescaled by attacker army at use time; that + // rescale assumed a linear relationship between "how much raw + // danger a defect-count implies" and "how much material the + // attacker actually has" that the real terms (each individually + // gated on specific attacking pieces existing) don't obey -- a + // structural mismatch, not just a stale fit (see conversation + // history for the full derivation). // - // STALE as of 2026-09-06 (board_representation/EVAL.md section 9): - // Refit 2026-09-15 against _EvalKing's linear-SCORE rewrite (king_ - // safety_data/HANDOFF.md's 2026-09-15 addendum), replacing the - // pre-rewrite table below (which was itself already flagged stale -- - // fit against a since-changed bucket index, see git history). - // _RecordPositionalCalibration's own bucket index was ALSO fixed at - // the same time (dropped _GetFileStormDefects, which had silently - // drifted out of sync with this function's own CountKingSafetyDefects- - // only lookup back on 2026-09-06) -- this refit is against the - // corrected, now-matching index, not a second layer of the same - // mismatch. CountKingSafetyDefects itself is unchanged (its backing - // table, KING_INITIAL_COUNTER_BY_LOCATION, was deliberately split - // off from _EvalKing's own copy -- see KING_EVAL_INITIAL_LOCATION -- - // specifically so this recalibration never needs to touch it again). + // New approach: two direct, jointly-collected percentile tables -- + // king safety bucketed by the ATTACKER's own army scaler (danger + // to a king is a function of the attacker's material, not the + // king-owner's), and residual (mobility/trapped/misc, with + // material/pawns/passers/bad-trade/bishop-pairs/king safety already + // excluded) bucketed by each side's OWN army scaler (residual is + // relative/congestion-driven, not attacker-driven, unlike king + // safety -- bucketing it by one side's own army alone, ignoring the + // enemy, was tried and came back noisy/non-monotonic; the + // differential does not, but this per-side percentile construction + // sidesteps needing the differential directly by keying each side's + // OWN contribution off its OWN army only, then combining per-side + // via the asymmetric construction below). // - // Sample: 5305 positions (5005 real TWIC games >=2400 Elo, quiet - // plies, plus 300 ECM positions mixed in for tactical-position - // coverage -- TWIC chosen as the primary source since it's - // representative of ordinary cross-sectional positions, unlike - // ECM's attack-the-king selection bias), sd4 each, 31.8M real - // Eval() calls / 17.1M samples through the super-lazy gate. Bucket - // 10 had zero samples in this run (the new terms apparently rarely - // if ever co-occur with CountKingSafetyDefects>=10) -- extrapolated - // from the bucket 8->9 trend (+25/step) rather than left stale; - // re-run with a larger/more adversarial sample if this bucket ever - // matters in practice. + // The margin construction itself mirrors this file's existing + // QSearch futility precedent (iPositional is returned per-color, + // not as a single "estimate minus full eval" blob, because a + // combined delta lets the two sides' real swings partially cancel + // and hides the structure that actually matters) -- and is the + // same operation as futility pruning, one stack frame up: for the + // fail-high check (score - margin_a > beta), soundness requires + // assuming the most PESSIMISTIC-for-me, OPTIMISTIC-for-my-opponent + // outcome for each term; the fail-low check (score + margin_b < + // alpha) requires the mirror. For every term, uniformly (no sign- + // flipping needed regardless of whether the raw term is usually + // positive or negative): // - // search.c's ">2" and searchsup.c's ">1" fixed-threshold extension - // gates still read CountKingSafetyDefects' raw, unscaled return - // value directly -- untouched by this refit (that function's own - // behavior didn't change), so no separate recalibration needed there. + // margin_a = [opponent's p90(term) - my p10(term)], summed over terms + // margin_b = [my p90(term) - opponent's p10(term)], summed over terms // - static const SCORE iKingSwingP90[11] = { - 30, 65, 90, 120, 155, 190, 220, 255, 265, 290, 315 - }; - // p90 of |mobility + passers + everything else combined|, measured - // directly (no useful correlation found with piece count). - static const SCORE iResidualP90 = 154; - // Per-side lookup-then-scale, deliberately mirroring _EvalKing's - // own shape exactly (look the raw counter up in a table first, - // *then* scale that resulting estimate by the attacker's army, - // rather than scaling the raw counter before the lookup) -- by - // direct instruction, so this stays as symmetric as possible with - // the real term it's estimating. CountKingSafetyDefects itself is - // untouched by this: it keeps returning a small, unscaled count, - // used as-is by its other two callers. - SCORE iWhiteKingSwing; - SCORE iBlackKingSwing; - SCORE iKingTerm; - ULONG uWhiteDefects, uBlackDefects; -#ifdef EVAL_TIME - UINT64 uSubTimer; -#endif - -#ifdef EVAL_TIME - uSubTimer = SystemReadTimeStampCounter(); -#endif - uWhiteDefects = CountKingSafetyDefects(pos, WHITE); - uBlackDefects = CountKingSafetyDefects(pos, BLACK); -#ifdef EVAL_TIME - ctx->sCounters.tree.u64CyclesEvalCountKingSafetyDefects += - (SystemReadTimeStampCounter() - uSubTimer); -#endif - iWhiteKingSwing = iKingSwingP90[MINU(10, uWhiteDefects)]; - iBlackKingSwing = iKingSwingP90[MINU(10, uBlackDefects)]; - - iWhiteKingSwing = (iWhiteKingSwing * - (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[BLACK]]) / 8; - iBlackKingSwing = (iBlackKingSwing * - (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[WHITE]]) / 8; - iKingTerm = iWhiteKingSwing + iBlackKingSwing; + // King safety is cross-referenced (my king's term uses the + // OPPONENT's army bucket, since they're the attacker; the + // opponent's king term uses MY army bucket) -- residual is not + // (each side's residual is keyed by that side's own army). + // + // PROVISIONAL tables below: from a 200-position stratified + // (material-balanced) sample, ~8.76M full-eval samples -- adequate + // for validating the construction and getting real numbers in + // place, but due for a refresh from the much larger 30k-position + // stratified histogram run once it lands (board_representation/ + // EVAL.md section 9 / 2026-09-16 super-lazy speed investigation). + // Re-derive via `calibrate residual` (DUMP_RESIDUAL_SAMPLES build). + // Uses the file-scope army-only king table (iKingP2/98ByAttacker- + // ArmyOnly) and residual table (iResidualP2/98ByOwnArmy), shared + // with super-lazy's identical construction below in Eval(). A + // joint (attacker-army x defect-count) king table was considered + // (CountKingSafetyDefects would supersede the army-only estimate + // once we're already committed to the more expensive lazy-eval + // path) but deliberately dropped -- CountKingSafetyDefects is slow + // despite being bitboard-based, not worth paying for here if the + // simpler army-only table is already good enough. It stays in the + // codebase for its existing, unrelated use as a search extension/ + // reduction gate (search.c/searchsup.c) -- just no longer called + // from eval's margin construction at all, either tier. + ULONG uColor = pos->uToMove; + ULONG xColor = FLIP(uColor); + ULONG uMyBucket = MINU(7, pos->uArmyScaler[uColor] / 4); + ULONG uOppBucket = MINU(7, pos->uArmyScaler[xColor] / 4); + SCORE iSelfPessimisticEnemyOptimisticMargin, iSelfOptimisticEnemyPessimisticMargin; - *piAlphaMargin += iKingTerm + iResidualP90; - *piBetaMargin += iKingTerm + iResidualP90; + iSelfPessimisticEnemyOptimisticMargin = MAX0((iKingP98ByAttackerArmy[uMyBucket] - + iKingP2ByAttackerArmy[uOppBucket]) + + (iResidualOptimisticByOwnArmy[uOppBucket] - + iResidualPessimisticByOwnArmy[uMyBucket])); + iSelfOptimisticEnemyPessimisticMargin = MAX0((iKingP98ByAttackerArmy[uOppBucket] - + iKingP2ByAttackerArmy[uMyBucket]) + + (iResidualOptimisticByOwnArmy[uMyBucket] - + iResidualPessimisticByOwnArmy[uOppBucket])); - // 2026-09-08: the p90 estimate above, while within its own - // documented "~10% wrong" design tolerance, still let the regular - // lazy exit's real swing exceed the margin actually used 2.11- - // 2.38% of the time at combined army scaler 16-31 (CALIBRATE_ - // MARGIN_SAFETY, 1500 real-game positions, tests/twic_sample.ep_, - // sd 8) -- not because the swing itself is much bigger there (max - // observed 855/779, comparable to every other bucket's 603-701), - // but because REDUCED_MATERIAL_DOWN_SCALER shrinks the king term - // faster than the swing actually shrinks at that material level. - // Floor the combined margin at the measured max-per-bucket (with - // ~25% headroom) rather than re-deriving the whole p90 shape -- - // this leaves the estimate above as the primary driver wherever - // it's already wide enough (buckets 4-7, where it already clears - // these floors) and only kicks in where it wasn't. Buckets 0-1 - // never take a regular lazy exit at all (see LAZY_EVAL_MIN_ - // MATERIAL) so were never measured; use bucket 2's floor there - // defensively rather than leaving them unfloored. Provisional -- - // re-derive with `calibrate marginsafety` if this margin's shape - // changes again. - { - // 2026-09-08 re-calibration: original values above measured - // |real - lazy| swing symmetrically, conflating "swung toward - // the boundary" (the only direction that can make an exit - // unsound) with "swung further away" (harmless). Re-measured - // with a directional split (CALIBRATE_MARGIN_SAFETY, same - // 1500-position tests/twic_sample.ep_, sd 8): the true - // dangerous-direction max is 30-80% smaller in every bucket - // than the old symmetric max, and every bucket showed - // exceeded=0 even before adding headroom. New values are - // directional-max * ~1.15-1.3 (more headroom on the smaller - // buckets, where sample size is thinner). - static const SCORE iSwingFloorByArmy[8] = - { - 297, 297, 297, 286, 461, 453, 582, 600, - }; - ULONG uMatBucket = MINU( - 7, (pos->uArmyScaler[WHITE] + pos->uArmyScaler[BLACK]) / 8); - *piAlphaMargin = MAX(*piAlphaMargin, iSwingFloorByArmy[uMatBucket]); - *piBetaMargin = MAX(*piBetaMargin, iSwingFloorByArmy[uMatBucket]); - } + // Caller's iBetaMargin gates "score - margin > beta" (fail-high, + // needs margin_a); iAlphaMargin gates "score + margin < alpha" + // (fail-low, needs margin_b). + // + // 2026-09-17: the flat iSwingFloorByArmy MAX() floor that used to + // sit here is GONE, for the same reason super-lazy's analogous + // floor was already dropped (see Eval()'s super-lazy margin + // construction, above) -- a MAX() against a much larger flat number + // just silently overrides the position-specific estimate in most + // cases, defeating the entire point of building it. margin_a/ + // margin_b now stand alone. If CALIBRATE_MARGIN_SAFETY turns up a + // real, non-trivial exceeded rate once this is validated against + // the 30k pool, the fix is tightening the percentile choice (e.g. + // p95/p5 or p99/p1), not resurrecting a flat backstop. + *piAlphaMargin += iSelfOptimisticEnemyPessimisticMargin; + *piBetaMargin += iSelfPessimisticEnemyOptimisticMargin; } @@ -3671,75 +4210,74 @@ Return value: // count, no x-ray -- simply excluded by the friendly-occupied // mask, nothing else needed. // - { - BITBOARD bbAttack = _BishopAttacksBB(c, pos->bbOccupied); - BITBOARD bbEnemySame = pos->bbPieces[xColor][BISHOP] | - pos->bbPieces[xColor][KNIGHT]; - BITBOARD bbEnemyGEContinue = pos->bbPieces[xColor][ROOK] | - pos->bbPieces[xColor][QUEEN] | - COOR_TO_BB(KingPosition(pos, xColor)); - BITBOARD bbFriendBQ = pos->bbPieces[uColor][BISHOP] | - pos->bbPieces[uColor][QUEEN]; - BITBOARD bbUnsafeForMinor = pos->bbPawnAttacks[xColor]; - BITBOARD bbExclude = 0; - BITBOARD bbSeen = 0; - BITBOARD bbLayer = bbAttack; - BITBOARD bbMobility = 0; - BITBOARD bbXrayAccum = 0; - BITBOARD bbFirstLayerMask = 0; + BITBOARD bbAttack = _BishopAttacksBB(c, pos->bbOccupied); + BITBOARD bbEnemySame = pos->bbPieces[xColor][BISHOP] | + pos->bbPieces[xColor][KNIGHT]; + BITBOARD bbEnemyGEContinue = pos->bbPieces[xColor][ROOK] | + pos->bbPieces[xColor][QUEEN] | + COOR_TO_BB(KingPosition(pos, xColor)); + BITBOARD bbFriendBQ = pos->bbPieces[uColor][BISHOP] | + pos->bbPieces[uColor][QUEEN]; + BITBOARD bbUnsafeForMinor = pos->bbPawnAttacks[xColor]; + BITBOARD bbExclude = 0; + BITBOARD bbSeen = 0; + BITBOARD bbLayer = bbAttack; + BITBOARD bbMobility = 0; + BITBOARD bbXrayAccum = 0; + BITBOARD bbFirstLayerMask = 0; - pos->bbMinorAttacks[uColor] |= bbAttack; + pos->bbMinorAttacks[uColor] |= bbAttack; - // - // Unified chain walk over both mobility and x-ray population - // in one pass -- same technique and same reason as rook's - // identical rewrite (board_representation/EVAL.md section 9): - // the old mailbox walk's BMOB_FRIEND_XRAY/BMOB_ENEMY_GREATER - // cases don't just x-ray past their blocker for attack-bit - // purposes, they keep walking (fStop=FALSE) and keep crediting - // mobility for whatever lies beyond. The originally-committed - // bitboard version (and its single-hop-then-chain-following - // x-ray fix) only ever handled the near side for *mobility*, - // an under-count caught the same way rook's was: a two-bishop - // battery on one diagonal with open squares beyond (old - // mailbox: 12/8 mobility for the two bishops; near-side-only - // bitboard version: 11/5). - // - for (;;) - { - BITBOARD bbNew = bbLayer & ~bbSeen; - BITBOARD bbBlockersHere = bbNew & pos->bbOccupied; - BITBOARD bbEmptyHere = bbNew & ~pos->bbOccupied; - BITBOARD bbFriendBQHere = bbBlockersHere & bbFriendBQ; - BITBOARD bbEnemySameHere = bbBlockersHere & bbEnemySame; - BITBOARD bbEnemyGEHere = bbBlockersHere & bbEnemyGEContinue; - BITBOARD bbFriendPawnHere = bbBlockersHere & pos->bbPawns[uColor]; - BITBOARD bbEnemyPawnHere = bbBlockersHere & - pos->bbPawns[xColor]; - BITBOARD bbTransientHere = bbFriendPawnHere & bbPc; - BITBOARD bbContinueHere; + // + // Unified chain walk over both mobility and x-ray population + // in one pass -- same technique and same reason as rook's + // identical rewrite (board_representation/EVAL.md section 9): + // the old mailbox walk's BMOB_FRIEND_XRAY/BMOB_ENEMY_GREATER + // cases don't just x-ray past their blocker for attack-bit + // purposes, they keep walking (fStop=FALSE) and keep crediting + // mobility for whatever lies beyond. The originally-committed + // bitboard version (and its single-hop-then-chain-following + // x-ray fix) only ever handled the near side for *mobility*, + // an under-count caught the same way rook's was: a two-bishop + // battery on one diagonal with open squares beyond (old + // mailbox: 12/8 mobility for the two bishops; near-side-only + // bitboard version: 11/5). + // + for (;;) + { + BITBOARD bbNew = bbLayer & ~bbSeen; + BITBOARD bbBlockersHere = bbNew & pos->bbOccupied; + BITBOARD bbEmptyHere = bbNew & ~pos->bbOccupied; + BITBOARD bbFriendBQHere = bbBlockersHere & bbFriendBQ; + BITBOARD bbEnemySameHere = bbBlockersHere & bbEnemySame; + BITBOARD bbEnemyGEHere = bbBlockersHere & bbEnemyGEContinue; + BITBOARD bbFriendPawnHere = bbBlockersHere & pos->bbPawns[uColor]; + BITBOARD bbEnemyPawnHere = bbBlockersHere & + pos->bbPawns[xColor]; + BITBOARD bbTransientHere = bbFriendPawnHere & bbPc; + BITBOARD bbContinueHere; - bbSeen |= bbNew; - bbXrayAccum |= (bbNew & bbFirstLayerMask); - bbFirstLayerMask = ~(BITBOARD)0; + bbSeen |= bbNew; + bbXrayAccum |= (bbNew & bbFirstLayerMask); + bbFirstLayerMask = ~(BITBOARD)0; - bbMobility |= (bbEmptyHere & ~bbUnsafeForMinor) | - bbEnemySameHere | bbEnemyGEHere | - (bbEnemyPawnHere & ~bbUnsafeForMinor) | - bbTransientHere; + bbMobility |= (bbEmptyHere & ~bbUnsafeForMinor) | + bbEnemySameHere | bbEnemyGEHere | + (bbEnemyPawnHere & ~bbUnsafeForMinor) | + bbTransientHere; - bbContinueHere = bbFriendBQHere | bbEnemyGEHere; - if (!bbContinueHere) - { - break; - } - bbExclude |= bbContinueHere; - bbLayer = _BishopAttacksBB(c, pos->bbOccupied & ~bbExclude); + bbContinueHere = bbFriendBQHere | bbEnemyGEHere; + if (!bbContinueHere) + { + break; } - pos->bbMinorXrayAttacks[uColor] |= bbXrayAccum; - - uTotalMobility = CountBits(bbMobility); + bbExclude |= bbContinueHere; + bbLayer = _BishopAttacksBB(c, pos->bbOccupied & ~bbExclude); } + pos->bbMinorXrayAttacks[uColor] |= bbXrayAccum; + + uTotalMobility = CountBits(bbMobility); + ASSERT(uTotalMobility <= 13); EVAL_TERM(uColor, BISHOP, @@ -4847,6 +5385,31 @@ Return value: // if ((u == 2) && (uColor == WHITE)) { + // 2026-09-16: this used to sum the raw per-pawn king- + // distance differential (each term in [-7,+7]) over EVERY + // pawn on the board, then multiply the whole sum by 12 -- + // unbounded in pawn count, so a genuine K+P endgame with + // 10+ pawns still on the board could reach several hundred + // centipawns from this one soft "my king is a bit more + // active" term (confirmed directly: CALIBRATE_MARGIN_ + // SAFETY-adjacent sampling turned up real +391/-384 + // outliers traced to exactly this line). That's worth more + // than a whole extra rook or bishop would be anywhere else + // in this eval, for a term that only ever fires when + // NEITHER side has a single piece left besides pawns (u==2 + // gate) -- clearly wrong on its face regardless of the + // exact right number. Kept the raw per-pawn sum (rather + // than normalizing to an average) deliberately -- more + // pawns still on the board is a real reason this term + // could matter more, not just noise to average away -- and + // instead shrunk the per-pawn weight from 12 to 2, so even + // an unrealistic worst case (16 pawns, every one at the + // max +/-7 distance differential) caps at 224 and realistic + // cases land around 40-80, versus the old 500-700+. + // PROVISIONAL: the "2" stops the runaway magnitude but + // isn't derived from any calibration. Needs the same self- + // play validation discipline as every other king-safety + // magnitude in this file before trusting the new ceiling. i = 0; bb = (pos->bbPawns[WHITE] | pos->bbPawns[BLACK]); while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&bb))) @@ -4858,7 +5421,7 @@ Return value: KING, c, iKingScore, - i * 12, + i * 2, "in/out of \"action\""); } } @@ -4958,6 +5521,55 @@ Return value: cQueen = FILE_RANK_TO_COOR(FILE(c), QUEENING_RANK[uColor]); ASSERT(FILE(cQueen) == FILE(c)); + // 2026-09-18: rook pawn, strong side's OWN king on + // the same file, on or adjacent to its own + // queening square -- i.e. the strong king is in + // front of its own pawn. Checked up front, before + // either race-credit computation below gets a + // chance to touch uRacerDist[uColor] for this + // pawn, and skips via `continue` (not by resetting + // uRacerDist[uColor], which would also wipe out a + // legitimate credit already earned by a different + // passer of the same color from an earlier loop + // iteration -- bb[uColor] can hold more than one + // passer). A friendly king blocking its own rook + // pawn's queening square is a classic drawing + // resource (the defender can potentially reach a + // stalemate), not a neutral or helpful thing -- + // unlike a center pawn, where the king merely costs + // a tempo (handled below via the existing +1 case), + // a rook pawn's single queening square occupied by + // its own king has no safe way to step aside + // without delicate tempo/opposition analysis this + // cheap eval-time heuristic can't do (that's what + // recogn.c's _RecognizeKPK critical-square logic is + // for). Found via fuzzing tests/cross_sectional_ + // 200k.ep_: two of this session's real recognizer/ + // eval bugs both had the strong side's own king in + // front of its own rook pawn (8/8/8/8/8/8/4K2p/7k + // w - - -- White can stalemate Black rather than + // let Ph2 queen). + // + // Distance must be exactly 0 (king ON the queening + // square), not <=1 -- if the king is merely + // adjacent, the queening square itself is empty + // and the pawn walks in and promotes next move + // with nothing in the way; that's a genuine win, + // not a stalemate risk. Only the king literally + // occupying its own pawn's only queening square is + // the danger this guards against. + if (((A == FILE(c)) || (H == FILE(c))) && + (FILE(c) == FILE(KingPosition(pos, uColor))) && + (0 == DISTANCE(KingPosition(pos, uColor), cQueen))) + { + // Matches the unconditional assignment every + // other path through this loop body makes -- + // this color's own passer was genuinely + // examined here, even though disqualified. + fDontCountMeOut[uColor] = TRUE; + continue; + } + uPawnDist = RANK_DISTANCE(cQueen, c); if ((RANK(c) == JUMPING_RANK[uColor]) && (IS_EMPTY(pos->rgSquare[c + d1].pPiece) && @@ -4998,6 +5610,14 @@ Return value: uRacerDist[uColor] = MINU(uRacerDist[uColor], (uPawnDist + 1)); } + // else: rook pawn, own king on the same file + // but more than 1 square from the queening + // square -- the specific adjacent/on-queening- + // square danger this session found (see the + // up-front `continue` guard above, right after + // cQueen is computed) doesn't apply here; leave + // as-is (no extra credit, no suppression), + // same as this branch's original behavior. } fDontCountMeOut[uColor] = TRUE; @@ -5344,8 +5964,14 @@ Return value: ASSERT(IS_ON_BOARD(c)); if (_WhoControlsSquareFast(pos, c) == FLIP(uColor)) { -#ifdef DEBUG + // Unconditional even outside DEBUG builds: EVAL_TERM + // below reads p unconditionally (its PIE argument + // feeds EvalTrace() in EVAL_TRACE builds), so an + // assignment gated behind #ifdef DEBUG alone left p + // uninitialized on read in a non-DEBUG/EVAL_TRACE + // build. p = pos->rgSquare[c].pPiece; +#ifdef DEBUG ASSERT(p); ASSERT(!IS_PAWN(p)); ASSERT(GET_COLOR(p) == uColor); @@ -5480,6 +6106,8 @@ Return value: ULONG uEnemy = FLIP(uSide); INT iMajorDiff; INT iMinorDiff; + ULONG uSideKnights; + ULONG uSideBishops; // // A pawn always gives some winning chances (it can always try to @@ -5490,14 +6118,20 @@ Return value: return(TRUE); } + // uSide's knight/bishop counts are each read three times below + // (the lone-minor check, iMinorDiff, and the two-knights check) -- + // compute them once and reuse rather than re-popcounting the same + // two bitboards repeatedly. + uSideKnights = FastCountBits(pos->bbPieces[uSide][KNIGHT]); + uSideBishops = FastCountBits(pos->bbPieces[uSide][BISHOP]); + // // No pawns and the only piece besides the king is a single minor: // never enough material to force mate (K+N or K+B vs anything is // never a forced win on material alone). // if ((2 == TotalNonPawnCount(pos, uSide)) && - (1 == (FastCountBits(pos->bbPieces[uSide][KNIGHT]) + - FastCountBits(pos->bbPieces[uSide][BISHOP])))) + (1 == (uSideKnights + uSideBishops))) { return(FALSE); } @@ -5516,8 +6150,7 @@ Return value: { iMinorDiff = (INT)(FastCountBits(pos->bbPieces[uEnemy][KNIGHT]) + FastCountBits(pos->bbPieces[uEnemy][BISHOP])) - - (INT)(FastCountBits(pos->bbPieces[uSide][KNIGHT]) + - FastCountBits(pos->bbPieces[uSide][BISHOP])); + (INT)(uSideKnights + uSideBishops); if (iMajorDiff == iMinorDiff) { return(FALSE); @@ -5532,8 +6165,8 @@ Return value: // excluded, since that has one bishop, not zero) -- both are // genuine, if sometimes technique-heavy, forced wins. // - if ((2 == FastCountBits(pos->bbPieces[uSide][KNIGHT])) && - (0 == FastCountBits(pos->bbPieces[uSide][BISHOP])) && + if ((2 == uSideKnights) && + (0 == uSideBishops) && (0 == FastCountBits(pos->bbPieces[uSide][ROOK])) && (0 == FastCountBits(pos->bbPieces[uSide][QUEEN])) && (1 == TotalNonPawnCount(pos, uEnemy)) && @@ -5556,12 +6189,59 @@ InitEval(void) } +// 2026-09-17: DRAWISH_FACTS holds the three position-only inputs +// EvalLookForDrawishSituations needs (_SideHasWinningChances for +// both sides, and the BOOC opposite-bishop check) -- none of these +// depend on the score at all, only on pos, which doesn't change +// between Eval()'s two call sites (the regular-lazy pre-exit check +// and the end of full eval). A node that attempts the lazy exit and +// then falls through to full eval anyway used to pay for +// _SideHasWinningChances (two calls) and the BOOC check twice, once +// per site, for identical answers both times -- only the score-vs- +// drawscore/50-move comparisons genuinely need to be re-run per call +// site, since the score itself legitimately differs between the +// early lazy-preview value and the final full-eval value. +// _EnsureDrawishFactsComputed is a cheap (single-branch) fetch-or- +// compute -- called at both sites so either call order (lazy-then- +// full, or full alone when the lazy attempt is skipped entirely for +// bare-king/low-material positions) computes the facts exactly once. +typedef struct _DRAWISH_FACTS +{ + FLAG fComputed; + FLAG fSideToMoveHasWinningChances; + FLAG fOpponentHasWinningChances; + FLAG fBOOC; +} DRAWISH_FACTS; + +static void +_EnsureDrawishFactsComputed(IN POSITION *pos, IN ULONG uColor, + IN OUT DRAWISH_FACTS *pFacts) +{ + if (pFacts->fComputed) + { + return; + } + pFacts->fSideToMoveHasWinningChances = _SideHasWinningChances(pos, uColor); + pFacts->fOpponentHasWinningChances = + _SideHasWinningChances(pos, FLIP(uColor)); + pFacts->fBOOC = (TotalNonPawnCount(pos, WHITE) == 2) && + (TotalNonPawnCount(pos, BLACK) == 2) && + (FastCountBits(pos->bbPieces[WHITE][BISHOP]) == 1) && + (FastCountBits(pos->bbPieces[BLACK][BISHOP]) == 1) && + (WhiteSqBishopCount(pos, WHITE) != WhiteSqBishopCount(pos, BLACK)); + pFacts->fComputed = TRUE; +} + + static void EvalLookForDrawishSituations(POSITION *pos, - SCORE *piScoreForSideToMove) + SCORE *piScoreForSideToMove, + IN DRAWISH_FACTS *pFacts) { ULONG uColor = pos->uToMove; + ASSERT(pFacts->fComputed); + // If the side who's ahead by raw material/positional score can't // actually force a win with what it has left on the board (see // _SideHasWinningChances), squash the score hard towards draw -- @@ -5574,24 +6254,21 @@ EvalLookForDrawishSituations(POSITION *pos, // defender with the checks _SideHasWinningChances gates on, so // some residual signal survives. if ((*piScoreForSideToMove > g_iDrawScore[uColor]) && - (FALSE == _SideHasWinningChances(pos, uColor))) + (FALSE == pFacts->fSideToMoveHasWinningChances)) { *piScoreForSideToMove = g_iDrawScore[uColor] + ((*piScoreForSideToMove - g_iDrawScore[uColor]) / 16); } else if ((*piScoreForSideToMove < g_iDrawScore[uColor]) && - (FALSE == _SideHasWinningChances(pos, FLIP(uColor)))) + (FALSE == pFacts->fOpponentHasWinningChances)) { *piScoreForSideToMove = g_iDrawScore[uColor] + ((*piScoreForSideToMove - g_iDrawScore[uColor]) / 16); } // If this is a BOOC endgame, push towards draw score too. - if ((TotalNonPawnCount(pos, WHITE) == 2) && - (TotalNonPawnCount(pos, BLACK) == 2) && - (FastCountBits(pos->bbPieces[WHITE][BISHOP]) == 1) && - (FastCountBits(pos->bbPieces[BLACK][BISHOP]) == 1) && - (WhiteSqBishopCount(pos, WHITE) != WhiteSqBishopCount(pos, BLACK))) { + if (pFacts->fBOOC) + { *piScoreForSideToMove = g_iDrawScore[uColor] + ((*piScoreForSideToMove - g_iDrawScore[uColor]) / 4); } @@ -5615,6 +6292,249 @@ EvalLookForDrawishSituations(POSITION *pos, } +#ifdef DUMP_RESIDUAL_SAMPLES +// +// Histogram-based percentile capture for residual (bucketed by OWN +// army scaler) and king safety (bucketed by ATTACKER's army scaler) -- +// see the two call sites inside Eval() below. Deliberately a histogram +// (same idea as CALIBRATE_POSITIONAL's g_CalibrateHist), not a raw +// per-sample Trace: a raw dump against the 30k-position calibration +// pool would run into the billions of lines (a 200-position sample +// alone already produced 8.76M), while this stays a fixed, tiny amount +// of memory regardless of sample count and prints one summary via +// `calibrate residual`. +// +#define RESID_HIST_BUCKETS 8 +#define RESID_HIST_BINS 90 +#define RESID_HIST_BIN_WIDTH 20 +#define RESID_HIST_MIN (-900) + +static UINT64 g_ResidualHist[RESID_HIST_BUCKETS][RESID_HIST_BINS]; +static UINT64 g_SuperLazyResidualHist[RESID_HIST_BUCKETS][RESID_HIST_BINS]; +static UINT64 g_KingSafetyHist[RESID_HIST_BUCKETS][RESID_HIST_BINS]; +static INT g_ResidualMin[RESID_HIST_BUCKETS]; +static INT g_ResidualMax[RESID_HIST_BUCKETS]; +static INT g_SuperLazyResidualMin[RESID_HIST_BUCKETS]; +static INT g_SuperLazyResidualMax[RESID_HIST_BUCKETS]; +static INT g_KingSafetyMin[RESID_HIST_BUCKETS]; +static INT g_KingSafetyMax[RESID_HIST_BUCKETS]; +static FLAG g_fResidHistInit = FALSE; +static ULONG g_uResidualOutliersPrinted = 0; +static ULONG g_uKingSafetyOutliersPrinted = 0; + +static void +_ResidHistInitIfNeeded(void) +{ + ULONG b; + if (TRUE == g_fResidHistInit) return; + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + g_ResidualMin[b] = 999999; + g_ResidualMax[b] = -999999; + g_SuperLazyResidualMin[b] = 999999; + g_SuperLazyResidualMax[b] = -999999; + g_KingSafetyMin[b] = 999999; + g_KingSafetyMax[b] = -999999; + } + g_fResidHistInit = TRUE; +} + +static void +_RecordResidualSample(ULONG uOwnArmyBucket, INT iValue) +{ + INT idx; + _ResidHistInitIfNeeded(); + idx = (iValue - RESID_HIST_MIN) / RESID_HIST_BIN_WIDTH; + if (idx < 0) idx = 0; + if (idx >= RESID_HIST_BINS) idx = RESID_HIST_BINS - 1; + g_ResidualHist[uOwnArmyBucket][idx]++; + if (iValue < g_ResidualMin[uOwnArmyBucket]) g_ResidualMin[uOwnArmyBucket] = iValue; + if (iValue > g_ResidualMax[uOwnArmyBucket]) g_ResidualMax[uOwnArmyBucket] = iValue; +} + +static void +_RecordSuperLazyResidualSample(ULONG uOwnArmyBucket, INT iValue) +{ + INT idx; + _ResidHistInitIfNeeded(); + idx = (iValue - RESID_HIST_MIN) / RESID_HIST_BIN_WIDTH; + if (idx < 0) idx = 0; + if (idx >= RESID_HIST_BINS) idx = RESID_HIST_BINS - 1; + g_SuperLazyResidualHist[uOwnArmyBucket][idx]++; + if (iValue < g_SuperLazyResidualMin[uOwnArmyBucket]) g_SuperLazyResidualMin[uOwnArmyBucket] = iValue; + if (iValue > g_SuperLazyResidualMax[uOwnArmyBucket]) g_SuperLazyResidualMax[uOwnArmyBucket] = iValue; +} + +static void +_RecordKingSafetySample(ULONG uAttackerArmyBucket, INT iValue) +{ + INT idx; + _ResidHistInitIfNeeded(); + idx = (iValue - RESID_HIST_MIN) / RESID_HIST_BIN_WIDTH; + if (idx < 0) idx = 0; + if (idx >= RESID_HIST_BINS) idx = RESID_HIST_BINS - 1; + g_KingSafetyHist[uAttackerArmyBucket][idx]++; + if (iValue < g_KingSafetyMin[uAttackerArmyBucket]) g_KingSafetyMin[uAttackerArmyBucket] = iValue; + if (iValue > g_KingSafetyMax[uAttackerArmyBucket]) g_KingSafetyMax[uAttackerArmyBucket] = iValue; +} + +static INT +_HistPercentile(UINT64 *puHist, UINT64 uTotal, double dP) +{ + UINT64 uTarget = (UINT64)(dP * (double)uTotal); + UINT64 uRunning = 0; + ULONG i; + for (i = 0; i < RESID_HIST_BINS; i++) + { + uRunning += puHist[i]; + if (uRunning >= uTarget) + { + return RESID_HIST_MIN + (INT)(i * RESID_HIST_BIN_WIDTH) + + (RESID_HIST_BIN_WIDTH / 2); + } + } + return RESID_HIST_MIN + (RESID_HIST_BINS * RESID_HIST_BIN_WIDTH); +} + +static void +_PersistRawHistogramsToFile(void) +/** + +Routine description: + + 2026-09-17: write every raw bin of all three histograms to a fixed + scratch file, so any future percentile (not just the ones + DumpResidualAndKingSafetyHistograms happens to print) can be read + off after the fact -- no need to re-run the 30k pool just to try a + different percentile choice. Plain text, one line per (histogram, + bucket): the bucket's min, max, then all RESID_HIST_BINS raw counts + space-separated. A future script can reconstruct any percentile via + the same cumulative-sum logic _HistPercentile uses. + +Parameters: + + void + +Return value: + + void + +**/ +{ + FILE *f; + ULONG b, i; + + f = fopen("/var/tmp/typhoon/residual_raw_histograms.txt", "w"); + if (NULL == f) + { + Trace("_PersistRawHistogramsToFile: could not open output file.\n"); + return; + } + fprintf(f, "# RESID_HIST_BINS=%d RESID_HIST_BIN_WIDTH=%d RESID_HIST_MIN=%d\n", + RESID_HIST_BINS, RESID_HIST_BIN_WIDTH, RESID_HIST_MIN); + fprintf(f, "# format: <histogram> <bucket> <min> <max> <bin0> <bin1> ... <binN-1>\n"); + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + fprintf(f, "residual %u %d %d", b, g_ResidualMin[b], g_ResidualMax[b]); + for (i = 0; i < RESID_HIST_BINS; i++) + { + fprintf(f, " %llu", (unsigned long long)g_ResidualHist[b][i]); + } + fprintf(f, "\n"); + } + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + fprintf(f, "sl_residual %u %d %d", b, g_SuperLazyResidualMin[b], + g_SuperLazyResidualMax[b]); + for (i = 0; i < RESID_HIST_BINS; i++) + { + fprintf(f, " %llu", (unsigned long long)g_SuperLazyResidualHist[b][i]); + } + fprintf(f, "\n"); + } + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + fprintf(f, "king %u %d %d", b, g_KingSafetyMin[b], g_KingSafetyMax[b]); + for (i = 0; i < RESID_HIST_BINS; i++) + { + fprintf(f, " %llu", (unsigned long long)g_KingSafetyHist[b][i]); + } + fprintf(f, "\n"); + } + fclose(f); + Trace("Raw histograms written to /var/tmp/typhoon/residual_raw_histograms.txt\n"); +} + +void +DumpResidualAndKingSafetyHistograms(void) +{ + ULONG b, i; + UINT64 uTotal; + + _PersistRawHistogramsToFile(); + +#define TRACE_HIST_ROW(hist, min, max) \ + Trace(" bucket %u (scaler %u-%u): n=%llu min=%d p1=%d p2=%d " \ + "p5=%d p10=%d p50=%d p90=%d p95=%d p98=%d p99=%d max=%d\n", \ + b, b * 4, b * 4 + 3, (unsigned long long)uTotal, (min), \ + _HistPercentile((hist), uTotal, 0.01), \ + _HistPercentile((hist), uTotal, 0.02), \ + _HistPercentile((hist), uTotal, 0.05), \ + _HistPercentile((hist), uTotal, 0.10), \ + _HistPercentile((hist), uTotal, 0.50), \ + _HistPercentile((hist), uTotal, 0.90), \ + _HistPercentile((hist), uTotal, 0.95), \ + _HistPercentile((hist), uTotal, 0.98), \ + _HistPercentile((hist), uTotal, 0.99), \ + (max)) + + Trace("Residual by OWN army-scaler bucket:\n"); + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + uTotal = 0; + for (i = 0; i < RESID_HIST_BINS; i++) uTotal += g_ResidualHist[b][i]; + if (0 == uTotal) + { + Trace(" bucket %u: no samples\n", b); + continue; + } + TRACE_HIST_ROW(g_ResidualHist[b], g_ResidualMin[b], g_ResidualMax[b]); + } + + Trace("Super-lazy-scoped residual (material baseline, includes " + "pawns/passers/bad-trade/bishop-pairs) by OWN army-scaler " + "bucket:\n"); + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + uTotal = 0; + for (i = 0; i < RESID_HIST_BINS; i++) uTotal += g_SuperLazyResidualHist[b][i]; + if (0 == uTotal) + { + Trace(" bucket %u: no samples\n", b); + continue; + } + TRACE_HIST_ROW(g_SuperLazyResidualHist[b], g_SuperLazyResidualMin[b], + g_SuperLazyResidualMax[b]); + } + + Trace("King safety by ATTACKER army-scaler bucket:\n"); + for (b = 0; b < RESID_HIST_BUCKETS; b++) + { + uTotal = 0; + for (i = 0; i < RESID_HIST_BINS; i++) uTotal += g_KingSafetyHist[b][i]; + if (0 == uTotal) + { + Trace(" bucket %u: no samples\n", b); + continue; + } + TRACE_HIST_ROW(g_KingSafetyHist[b], g_KingSafetyMin[b], + g_KingSafetyMax[b]); + } +#undef TRACE_HIST_ROW +} +#endif + + SCORE Eval(IN SEARCHER_THREAD_CONTEXT *ctx, IN SCORE iAlpha, @@ -5656,6 +6576,10 @@ Return value: ULONG xColor = FLIP(uColor); BITBOARD bb; FLAG fDeferred; + // Computed at most once per Eval() call, on first need, by + // whichever of the two EvalLookForDrawishSituations call sites + // runs first -- see DRAWISH_FACTS's comment above. + DRAWISH_FACTS sDrawishFacts = {0}; #ifdef EVAL_TIME UINT64 uTimer = SystemReadTimeStampCounter(); #endif @@ -5670,12 +6594,20 @@ Return value: FLAG fWouldHaveExited = FALSE; SCORE iSavedLazyScore = 0; SCORE iSavedLazyPositional = 0; + // 2026-09-17: iPositional is no longer flat/symmetric across colors + // (p50-based, keyed by each side's own army bucket) -- iSavedLazy- + // Positional above stays the margin-safety accounting value (still + // symmetric, unrelated to iPositional), these two are what actually + // gets restored into (*piPositional)[WHITE]/[BLACK] below. + SCORE iSavedLazyPositionalWhite = 0; + SCORE iSavedLazyPositionalBlack = 0; // Same idea, kept separate from the pair above: the super-lazy // exit (added 2026-09-07) is a much cheaper, much-larger-gap check // than the regular lazy exit and was never hooked into this // harness -- see RecordSuperLazyMarginSafetySwing below. FLAG fWouldHaveExitedSuperLazy = FALSE; SCORE iSavedSuperLazyScore = 0; + SCORE iSavedSuperLazyMarginUsed = 0; #endif ASSERT(IS_VALID_SCORE(iAlpha)); ASSERT(IS_VALID_SCORE(iBeta)); @@ -5692,7 +6624,11 @@ Return value: (PawnMaterial(pos, xColor) + pos->uNonPawnMaterial[xColor]); #ifdef EVAL_DUMP EvalTraceClear(); - Trace("Material:\n%d\t\t%d\n", pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("Material:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif // Initialize army scalers here (moved ahead of the super-lazy exit @@ -5717,53 +6653,118 @@ Return value: (SystemReadTimeStampCounter() - u64MaterialSetupTimer); #endif +#ifdef DUMP_RESIDUAL_SAMPLES + // Super-lazy's OWN baseline -- material only, before _EvalPawns + // even runs. Separate from iResidualBaselineWhite/Black below + // (captured after bishop pairs, i.e. regular-lazy's baseline): + // that measurement deliberately excludes pawns/passers/bad-trade/ + // bishop-pairs, which is correct for regular lazy (which already + // has all of that by the time it asks) but wrong for super-lazy, + // which has NONE of it yet. This is what a super-lazy-scoped + // residual needs to be measured against instead. + SCORE iSuperLazyResidualBaselineWhite = pos->iScore[WHITE]; + SCORE iSuperLazyResidualBaselineBlack = pos->iScore[BLACK]; +#endif + + // 2026-09-16: shared "too dangerous to shortcut" gate for both lazy + // tiers -- exactly the condition search.c's own passer-race code + // (_EvalPasserRacesAgainstLoneKings' caller) uses to decide whether + // the volatile won-passer-race verdict can fire: one side down to + // literally just a king (TotalNonPawnCount counts the king itself + // as 1, so ==1 means no other non-pawn material at all). Regular + // lazy already had a blunter, broader guard for this material + // region (LAZY_EVAL_MIN_MATERIAL, a combined-army-scaler band) -- + // but that band doesn't actually imply either side is bare-king + // (e.g. a heavy attacker vs. a bare king can have a large combined + // scaler despite one side having zero other material), so it + // wasn't sufficient on its own. Super-lazy had no such guard at + // all. Folding EvalLookForDrawishSituations into both tiers (above/ + // below) closes the "insufficient winning chances"/BOOC/50-move + // gap; this closes the remaining one -- the passer-race verdict + // needs pHash->bbPasserLocations, which isn't available yet at + // super-lazy's point in Eval() (it runs before _EvalPawns), so + // that correction can't cheaply be folded in the same way and the + // honest answer here is to just not shortcut in this regime. + FLAG fBareKingEitherSide = (1 == TotalNonPawnCount(pos, WHITE)) || + (1 == TotalNonPawnCount(pos, BLACK)); + // Super-lazy exit point. #ifdef LAZY_EVAL - // 2026-09-08: was a single flat 625 for every material level, then - // split by material bucket using a 100-position/sd8 calibration - // (symmetric |swing|, ~15-20% headroom). Re-calibrated against a - // larger 1500-position sample with a directional split (only a - // swing *toward* the alpha/beta boundary can make an exit unsound; - // a swing away is harmless, and the original symmetric measurement - // conflated the two) -- true dangerous-direction max ran 15-50% - // below the old symmetric max in most buckets, and every bucket - // showed exceeded=0 even before headroom. Values below are - // directional-max * ~1.15-1.3 (more headroom on buckets with - // thinner samples, e.g. bucket 0's n=512); buckets 2 and 5 kept at - // their prior value since it was already tighter than a fresh 15% - // headroom would give. Table indexed the same way _MaterialBucket - // buckets the calibration data (combined army scaler / 8, 8 - // buckets), so it can be re-derived directly from a "calibrate - // marginsafety" run. Not a hard theoretical bound -- re-check - // before trusting this at sd well beyond 8. - static const SCORE SUPER_LAZY_MARGIN_BY_ARMY[8] = - { - 300, 1635, 1800, 1070, 946, 850, 734, 698, - }; + // Combined-army bucket, still needed by the bucket-0 exclusion + // below (a structural exclusion of pawn-dominated near-bare-army + // positions, unrelated to the margin construction itself). ULONG uSuperLazyMatBucket = MINU( 7, (pos->uArmyScaler[WHITE] + pos->uArmyScaler[BLACK]) / 8); - SCORE iSuperLazyMargin = SUPER_LAZY_MARGIN_BY_ARMY[uSuperLazyMatBucket]; + + // 2026-09-16: exclude material bucket 0 (combined non-pawn army + // scaler 0-7) from super-lazy entirely -- CALIBRATE_MARGIN_SAFETY + // against 30k stratified positions found a 6.8% exceeded rate + // there even after the bare-king exclusion above and the + // EvalLookForDrawishSituations fix, traced by hand to real, + // externally-corroborated (cross-checked against Crafty's own eval + // breakdown on the same position, within 7%) swings of 650-750+ + // centipawns from knight-vs-bishop-with-several-pawns-per-side + // endgames. Root cause: this bucket is defined purely by non-pawn + // material, so it says nothing about how many pawns are on the + // board, and super-lazy runs before _EvalPawns entirely -- meaning + // it's not just imprecise about pawn structure here, it's totally + // blind to it, in exactly the material regime where pawns (passed- + // pawn recognition, king-supports-own-passer, _ReEvalPassers) can + // be nearly the entire evaluation. Not a bug to fix with a bigger + // constant -- a structural mismatch between what this bucket + // measures (piece count) and what actually drives the score + // (pawn count) in this exact material range. Bucket 1+ (scaler + // >=8) doesn't have this problem to nearly the same degree since + // there's enough non-pawn material for pieces to matter alongside + // pawns, not be dwarfed by them. + if ((FALSE == fBareKingEitherSide) && (uSuperLazyMatBucket > 0)) { #ifdef EVAL_TIME UINT64 uSuperLazyTimer = SystemReadTimeStampCounter(); #endif iScoreForSideToMove = (pos->iScore[uColor] -pos->iScore[xColor]); ASSERT(IS_VALID_SCORE(iScoreForSideToMove)); - if (iScoreForSideToMove + iSuperLazyMargin < iAlpha) + + // We have a super-rough eval -- material only at this point. + // Compute two margins to check whether things look so good or + // bad that we need not refine our eval because we are sure to + // FH/FL anyway. + ULONG uSuperLazyMyBucket = MINU(7, pos->uArmyScaler[uColor] / 4); + ULONG uSuperLazyOppBucket = MINU(7, pos->uArmyScaler[xColor] / 4); + SCORE iSuperLazySelfPessEnemyOptMargin = MAX0( + (iKingP98ByAttackerArmy[uSuperLazyMyBucket] - + iKingP2ByAttackerArmy[uSuperLazyOppBucket]) + + (iSuperLazyResidualOptimisticByOwnArmy[uSuperLazyOppBucket] - + iSuperLazyResidualPessimisticByOwnArmy[uSuperLazyMyBucket])); + SCORE iSuperLazySelfOptEnemyPessMargin = MAX0( + (iKingP98ByAttackerArmy[uSuperLazyOppBucket] - + iKingP2ByAttackerArmy[uSuperLazyMyBucket]) + + (iSuperLazyResidualOptimisticByOwnArmy[uSuperLazyMyBucket] - + iSuperLazyResidualPessimisticByOwnArmy[uSuperLazyOppBucket])); + + if (iScoreForSideToMove + iSuperLazySelfOptEnemyPessMargin < iAlpha) { ASSERT(iScoreForSideToMove < iAlpha); +#ifdef CALIBRATE_MARGIN_SAFETY + iSavedSuperLazyMarginUsed = iSuperLazySelfOptEnemyPessMargin; +#endif INC(ctx->sCounters.tree.u64SuperLazyEvals); + g_u64SuperLazyEvalsByBucket[_MaterialBucket(pos)]++; if (NULL != piPositional) { - // No positional credit here -- a super-lazy verdict is - // already a confident material-only read that we're - // well outside the window; handing back extra slack - // just re-inflates the qsearch futility margin and - // defeats the point of taking the cheap exit. Let - // FUTILITY_BASE_MARGIN_BY_SOURCE[SUPERLAZY] alone - // govern from here. - (*piPositional)[WHITE] = 0; - (*piPositional)[BLACK] = 0; + // 2026-09-17: no longer flat 0 -- same p50-based typical- + // value estimate as regular lazy, using the super-lazy- + // scoped residual table (material-only baseline). See + // iKingP50ByAttackerArmyOnly/iSuperLazyResidualP50By- + // OwnArmy (file-scope, above). + ULONG uWhiteBucket = MINU(7, pos->uArmyScaler[WHITE] / 4); + ULONG uBlackBucket = MINU(7, pos->uArmyScaler[BLACK] / 4); + (*piPositional)[WHITE] = + iKingP50ByAttackerArmy[uBlackBucket] + + iSuperLazyResidualP50ByOwnArmy[uWhiteBucket]; + (*piPositional)[BLACK] = + iKingP50ByAttackerArmy[uWhiteBucket] + + iSuperLazyResidualP50ByOwnArmy[uBlackBucket]; ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_SUPERLAZY; } #ifdef EVAL_TIME @@ -5790,14 +6791,26 @@ Return value: goto end; #endif } - if (iScoreForSideToMove - iSuperLazyMargin > iBeta) + if (iScoreForSideToMove - iSuperLazySelfPessEnemyOptMargin > iBeta) { ASSERT(iScoreForSideToMove > iBeta); +#ifdef CALIBRATE_MARGIN_SAFETY + iSavedSuperLazyMarginUsed = iSuperLazySelfPessEnemyOptMargin; +#endif INC(ctx->sCounters.tree.u64SuperLazyEvals); + g_u64SuperLazyEvalsByBucket[_MaterialBucket(pos)]++; if (NULL != piPositional) { - (*piPositional)[WHITE] = 0; - (*piPositional)[BLACK] = 0; + // See the analogous alpha-side branch above -- same p50 + // typical-value estimate. + ULONG uWhiteBucket = MINU(7, pos->uArmyScaler[WHITE] / 4); + ULONG uBlackBucket = MINU(7, pos->uArmyScaler[BLACK] / 4); + (*piPositional)[WHITE] = + iKingP50ByAttackerArmy[uBlackBucket] + + iSuperLazyResidualP50ByOwnArmy[uWhiteBucket]; + (*piPositional)[BLACK] = + iKingP50ByAttackerArmy[uWhiteBucket] + + iSuperLazyResidualP50ByOwnArmy[uBlackBucket]; ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_SUPERLAZY; } #ifdef EVAL_TIME @@ -5826,30 +6839,29 @@ Return value: } #endif - // + // If we get here, we did not take the super lazy exit. This is + // either a lazy exit node or a full eval node. + // Pawn eval. Note: if fDeferred comes back as TRUE then we have // neither cleared nor initialized the attack tables. This is - // because we hope that we can get a lazy eval cutoff here and + // because we hope that we can still get a lazy eval exit here and // save some time. BEFORE ANY CODE BELOW TOUCHES THE ATTACK // TABLES IT NEEDS TO CLEAR/POPULATE THEM THOUGH!!! - // #ifdef EVAL_TIME + UINT64 u64PawnTimer = SystemReadTimeStampCounter(); + pHash = _EvalPawns(ctx, &fDeferred); { - UINT64 u64PawnTimer = SystemReadTimeStampCounter(); - pHash = _EvalPawns(ctx, &fDeferred); + UINT64 u64Elapsed = SystemReadTimeStampCounter() - u64PawnTimer; + ctx->sCounters.tree.u64CyclesEvalPawns += u64Elapsed; + if (fDeferred) { - UINT64 u64Elapsed = SystemReadTimeStampCounter() - u64PawnTimer; - ctx->sCounters.tree.u64CyclesEvalPawns += u64Elapsed; - if (fDeferred) - { - ctx->sCounters.tree.u64CyclesEvalPawnsHit += u64Elapsed; - ctx->sCounters.tree.u64CountEvalPawnsHit++; - } - else - { - ctx->sCounters.tree.u64CyclesEvalPawnsMiss += u64Elapsed; - ctx->sCounters.tree.u64CountEvalPawnsMiss++; - } + ctx->sCounters.tree.u64CyclesEvalPawnsHit += u64Elapsed; + ctx->sCounters.tree.u64CountEvalPawnsHit++; + } + else + { + ctx->sCounters.tree.u64CyclesEvalPawnsMiss += u64Elapsed; + ctx->sCounters.tree.u64CountEvalPawnsMiss++; } } #else @@ -5861,63 +6873,89 @@ Return value: pos->iScore[BLACK] += pHash->iScore[BLACK]; ASSERT(IS_VALID_SCORE(pos->iScore[WHITE] - pos->iScore[BLACK])); #ifdef EVAL_DUMP - Trace("After pawns:\n%d\t\t%d\n", pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After pawns:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif + // Look for won passers races. Note: This code cannot trust the + // state of the attack tables yet! #ifdef EVAL_TIME UINT64 u64PasserBadtradeBishopTimer = SystemReadTimeStampCounter(); #endif - // - // Look for won passers races. Note: This code cannot trust the - // state of the attack tables yet! - // - if ((TotalNonPawnCount(pos, WHITE) == 1) || - (TotalNonPawnCount(pos, BLACK) == 1)) + if (fBareKingEitherSide) { (void)_EvalPasserRacesAgainstLoneKings(pos, pHash); #ifdef EVAL_DUMP - Trace("After passer races:\n%d\t\t%d\n", pos->iScore[WHITE], - pos->iScore[BLACK]); + Trace("After passer races:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } - // - // Bad trade code. Right. Note: this code cannot trust the state - // of the attack tables... - // + // Bad trade code. Note: this code cannot trust the state of the + // attack tables... if (pos->uNonPawnMaterial[WHITE] != pos->uNonPawnMaterial[BLACK]) { _EvalBadTrades(pos); #ifdef EVAL_DUMP - Trace("After bad trades:\n%d\t\t%d\n", pos->iScore[WHITE], - pos->iScore[BLACK]); + Trace("After bad trades:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], + pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } - // // Bishop pairs. Again... Note: this code cannot trust the state // of the attack tables. - // _EvalBishopPairs(pos); #ifdef EVAL_DUMP - Trace("After bishop pair bonus:\n%d\t\t%d\n", pos->iScore[WHITE], - pos->iScore[BLACK]); + Trace("After bishop pair bonus:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], + pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalPasserBadtradeBishop += (SystemReadTimeStampCounter() - u64PasserBadtradeBishopTimer); #endif +#ifdef DUMP_RESIDUAL_SAMPLES + // Snapshot right here: material + pawns/passers + bad-trade + + // bishop pairs, before king safety or mobility/misc positional + // terms run. Subtracting this (the same baseline regular lazy + // itself reads its score from) plus the real per-side king score + // (ctx->sPlyInfo[ctx->uPly].iKingScore[color], stored once + // _EvalKing runs for that side) from the final full-eval score + // isolates exactly what iResidualP98 is trying to estimate: + // mobility + trapped pieces + everything else, with passers + // already excluded. See the dump site below, near "Almost done". + SCORE iResidualBaselineWhite = pos->iScore[WHITE]; + SCORE iResidualBaselineBlack = pos->iScore[BLACK]; +#endif + #ifdef LAZY_EVAL - // // Below LAZY_EVAL_MIN_MATERIAL combined army scaler, skip lazy eval // entirely (see its chess.h comment) -- measured directly via // CALIBRATE_MARGIN_SAFETY, the regular lazy exit's real swing // exceeded its own assumed margin 20.6% of the time in near-bare- // king endgames, far above the ~0.01-0.36% rate everywhere else. - // - if (pos->uArmyScaler[WHITE] + pos->uArmyScaler[BLACK] >= - LAZY_EVAL_MIN_MATERIAL) + // fBareKingEitherSide (set above, before the super-lazy exit point) + // is a separate, more precise guard for the specific passer-race + // mechanism -- kept in addition to this band, not instead of it, + // since a heavy attacker vs. a bare king can still clear this + // combined-scaler threshold despite one side being bare-king. + if ((pos->uArmyScaler[WHITE] + pos->uArmyScaler[BLACK] >= + LAZY_EVAL_MIN_MATERIAL) && + (FALSE == fBareKingEitherSide)) { #ifdef EVAL_TIME UINT64 uLazyDecisionTimer = SystemReadTimeStampCounter(); @@ -5930,179 +6968,201 @@ Return value: // 2. Pawn structure bonuses/penalties (incl passers/candidates) // 3. Passer races are detected already // 4. "Bad trade" code has already run - // 5. We've already detected unwinnable endgames - // 6. Bishop pairs + // 5. Bishop pairs // - iScoreForSideToMove = (pos->iScore[pos->uToMove] - - pos->iScore[FLIP(pos->uToMove)]); + iScoreForSideToMove = (pos->iScore[uColor] - pos->iScore[xColor]); ASSERT(IS_VALID_SCORE(iScoreForSideToMove)); + // 2026-09-16: item 5 used to claim "we've already detected + // unwinnable endgames" here, but nothing before this point + // actually did -- EvalLookForDrawishSituations (insufficient- + // winning-chances squash, BOOC, 50-move dampening) was only + // ever called once, at the very end of the FULL eval path + // (see its call site below), so a regular-lazy exit could + // return a raw material-ahead score (e.g. "+300, bishop up") + // in a position full eval would have squashed hard toward a + // draw (BOOC: /4; no forcing material: /16) -- a large swing + // with nothing to do with king safety or mobility. Call it + // here too so regular lazy sees the same correction full eval + // would apply. Cheap and safe to do this early: unlike + // king safety/mobility, none of EvalLookForDrawishSituations' + // three checks touch attack bitboards, only piece bitboards/ + // counts and pos->uFifty, all already valid by this point. + // _EnsureDrawishFactsComputed is a no-op if the call site + // below (full eval, if this node falls through to it) already + // populated sDrawishFacts -- can't happen in this order since + // this is the first potential call site, but written this way + // so either call order is correct. + TIMED_EVAL_CALL(ctx, u64CyclesEvalDrawish, + (_EnsureDrawishFactsComputed(pos, uColor, &sDrawishFacts), + EvalLookForDrawishSituations(pos, &iScoreForSideToMove, + &sDrawishFacts))); + // // Eval has not considered several potentially large terms: // // 1. Piece positional components (such as mobility, trapped) - // 2. King safety penalties + // 2. King safety penalties (which can be large!) // 3. Other miscellaneous bonuses/penalties // // We build two "margins" to account for these components of the // score. // - iAlphaMargin = iBetaMargin = LAZY_EVAL_BASE_MARGIN; - - // - // If (score + alpha_margin) is already > alpha -OR- - // (score - beta_margin) is already < beta - // - // ...then we can stop thinking about lazy eval; the rest of the - // computation only increases the margin so we know LE will fail - // and can save some work here. - // + // 2026-09-17: no flat base margin, no floor -- matches super- + // lazy's convention. margin_a'/margin_b' (built entirely by + // EstimatePositionalScore below) stand alone. If CALIBRATE_ + // MARGIN_SAFETY turns up a real, non-trivial exceeded rate once + // this is validated against the 30k pool, the fix is tightening + // the percentile choice there (e.g. p95/p5 or p99/p1), not + // resurrecting a flat backstop that would just override the + // position-specific estimate again. + iAlphaMargin = iBetaMargin = 0; #ifdef CALIBRATE_POSITIONAL ctx->fCalibrateCandidate = FALSE; g_uCalibrateGateChecked++; #endif - if ((iScoreForSideToMove + iAlphaMargin < iAlpha) || - (iScoreForSideToMove - iBetaMargin > iBeta)) - { #ifdef CALIBRATE_POSITIONAL - g_uCalibrateGateTrue++; + g_uCalibrateGateTrue++; #endif - // - // Ok, we can't say for sure that we won't take a lazy exit - // yet. So do the expensive part of lazy eval estimation and - // widen the margin further for king safety issues and passed - // pawns. - // + // + // Ok, we can't say for sure that we won't take a lazy exit + // yet. So do the expensive part of lazy eval estimation and + // widen the margin further for king safety issues and passed + // pawns. + // #ifdef CALIBRATE_POSITIONAL - // This node is one where a real (uninstrumented) build would - // actually call EstimatePositionalScore and consider a lazy - // exit -- the population _RecordPositionalCalibration should - // be sampling from, per _EvalKing's calls below. - ctx->fCalibrateCandidate = TRUE; + // This node is one where a real (uninstrumented) build would + // actually call EstimatePositionalScore and consider a lazy + // exit -- the population _RecordPositionalCalibration should + // be sampling from, per _EvalKing's calls below. + ctx->fCalibrateCandidate = TRUE; #endif - EstimatePositionalScore(ctx, pos, pHash, &iAlphaMargin, &iBetaMargin); + EstimatePositionalScore(ctx, pos, pHash, &iAlphaMargin, &iBetaMargin); #ifdef CALIBRATE_BASE_MARGIN - { - ULONG uBucket = _MaxKingDangerBucket(ctx, pos, pHash); - ULONG uMatBucket = _MaterialBucket(pos); - g_uBaseMarginExitAttempted[uMatBucket][uBucket]++; - if ((iScoreForSideToMove + iAlphaMargin <= iAlpha) || - (iScoreForSideToMove - iBetaMargin >= iBeta)) - { - g_uBaseMarginExitTaken[uMatBucket][uBucket]++; - } - } + ULONG uBucket = _MaxKingDangerBucket(ctx, pos, pHash); + ULONG uMatBucket = _MaterialBucket(pos); + g_uBaseMarginExitAttempted[uMatBucket][uBucket]++; + if ((iScoreForSideToMove + iAlphaMargin <= iAlpha) || + (iScoreForSideToMove - iBetaMargin >= iBeta)) + { + g_uBaseMarginExitTaken[uMatBucket][uBucket]++; + } #endif - // - // CALIBRATE_POSITIONAL builds never take the lazy exit here -- - // the whole point of a calibration run is to compute the cheap - // defect-count estimate *and* let Eval() continue on to - // _EvalKing's real computation on every node, so the two can be - // logged and compared. See _RecordPositionalCalibration below - // (called from _EvalKing) and DumpPositionalCalibration - // (command.c's "calibrate dump"). - // + + // CALIBRATE_POSITIONAL builds never take the lazy exit here -- + // the whole point of a calibration run is to compute the cheap + // defect-count estimate *and* let Eval() continue on to + // _EvalKing's real computation on every node, so the two can be + // logged and compared. See _RecordPositionalCalibration below + // (called from _EvalKing) and DumpPositionalCalibration + // (command.c's "calibrate dump"). #ifndef CALIBRATE_POSITIONAL - if (iScoreForSideToMove + iAlphaMargin <= iAlpha) + if (iScoreForSideToMove + iAlphaMargin <= iAlpha) + { + INC(ctx->sCounters.tree.u64LazyEvals); + g_u64LazyEvalsByBucket[_MaterialBucket(pos)]++; + if (NULL != piPositional) { - INC(ctx->sCounters.tree.u64LazyEvals); - if (NULL != piPositional) - { - (*piPositional)[WHITE] = iAlphaMargin; - (*piPositional)[BLACK] = iAlphaMargin; - ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_LAZY; - } + // 2026-09-17: no longer flat 0 -- a p50-based typical- + // value estimate of this side's own missing positional + // terms (king safety + residual), matching full-eval's + // own iPositional convention (that side's own real + // positional score, not a relative-to-opponent + // quantity). See iKingP50ByAttackerArmyOnly/ + // iResidualP50ByOwnArmy (file-scope, above) for the + // full percentile charts this was derived from. + ULONG uWhiteBucket = MINU(7, pos->uArmyScaler[WHITE] / 4); + ULONG uBlackBucket = MINU(7, pos->uArmyScaler[BLACK] / 4); + (*piPositional)[WHITE] = + iKingP50ByAttackerArmy[uBlackBucket] + + iResidualP50ByOwnArmy[uWhiteBucket]; + (*piPositional)[BLACK] = + iKingP50ByAttackerArmy[uWhiteBucket] + + iResidualP50ByOwnArmy[uBlackBucket]; + ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_LAZY; + } #ifdef EVAL_TIME - ctx->sCounters.tree.u64CyclesEvalPreLazy += - (SystemReadTimeStampCounter() - uTimer); + ctx->sCounters.tree.u64CyclesEvalPreLazy += + (SystemReadTimeStampCounter() - uTimer); #endif #ifdef CALIBRATE_MARGIN_SAFETY - // Don't exit yet -- let the real full eval run below so we - // can measure the true swing, then restore these values - // right before `end:` so the caller sees exactly what a - // normal build would have returned. - fWouldHaveExited = TRUE; - iSavedLazyScore = iScoreForSideToMove; - iSavedLazyPositional = iAlphaMargin; + // Don't exit yet -- let the real full eval run below so we + // can measure the true swing, then restore these values + // right before `end:` so the caller sees exactly what a + // normal build would have returned. + fWouldHaveExited = TRUE; + iSavedLazyScore = iScoreForSideToMove; + iSavedLazyPositional = iAlphaMargin; + iSavedLazyPositionalWhite = + iKingP50ByAttackerArmyOnly[MINU(7, pos->uArmyScaler[BLACK] / 4)] + + iResidualP50ByOwnArmy[MINU(7, pos->uArmyScaler[WHITE] / 4)]; + iSavedLazyPositionalBlack = + iKingP50ByAttackerArmyOnly[MINU(7, pos->uArmyScaler[WHITE] / 4)] + + iResidualP50ByOwnArmy[MINU(7, pos->uArmyScaler[BLACK] / 4)]; #else #ifdef EVAL_TIME - ctx->sCounters.tree.u64CyclesEvalLazyDecision += - (SystemReadTimeStampCounter() - uLazyDecisionTimer); - ctx->sCounters.tree.u64CyclesLazyExit += - (SystemReadTimeStampCounter() - uTimer); - ctx->sCounters.tree.u64CyclesInEval += - (SystemReadTimeStampCounter() - uTimer); + ctx->sCounters.tree.u64CyclesEvalLazyDecision += + (SystemReadTimeStampCounter() - uLazyDecisionTimer); + ctx->sCounters.tree.u64CyclesLazyExit += + (SystemReadTimeStampCounter() - uTimer); + ctx->sCounters.tree.u64CyclesInEval += + (SystemReadTimeStampCounter() - uTimer); #endif - goto end; + goto end; #endif - } - else if (iScoreForSideToMove - iBetaMargin >= iBeta) + } + else if (iScoreForSideToMove - iBetaMargin >= iBeta) + { + INC(ctx->sCounters.tree.u64LazyEvals); + g_u64LazyEvalsByBucket[_MaterialBucket(pos)]++; + if (NULL != piPositional) { - INC(ctx->sCounters.tree.u64LazyEvals); - if (NULL != piPositional) - { - (*piPositional)[WHITE] = iBetaMargin; - (*piPositional)[BLACK] = iBetaMargin; - ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_LAZY; - } + // See the analogous fail-low branch above -- same p50 + // typical-value estimate, not a flat 0 or the margin + // itself. + ULONG uWhiteBucket = MINU(7, pos->uArmyScaler[WHITE] / 4); + ULONG uBlackBucket = MINU(7, pos->uArmyScaler[BLACK] / 4); + (*piPositional)[WHITE] = + iKingP50ByAttackerArmy[uBlackBucket] + + iResidualP50ByOwnArmy[uWhiteBucket]; + (*piPositional)[BLACK] = + iKingP50ByAttackerArmy[uWhiteBucket] + + iResidualP50ByOwnArmy[uBlackBucket]; + ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_LAZY; + } #ifdef EVAL_TIME - ctx->sCounters.tree.u64CyclesEvalPreLazy += - (SystemReadTimeStampCounter() - uTimer); + ctx->sCounters.tree.u64CyclesEvalPreLazy += + (SystemReadTimeStampCounter() - uTimer); #endif #ifdef CALIBRATE_MARGIN_SAFETY - fWouldHaveExited = TRUE; - iSavedLazyScore = iScoreForSideToMove; - iSavedLazyPositional = iBetaMargin; + fWouldHaveExited = TRUE; + iSavedLazyScore = iScoreForSideToMove; + iSavedLazyPositional = iBetaMargin; + iSavedLazyPositionalWhite = + iKingP50ByAttackerArmyOnly[MINU(7, pos->uArmyScaler[BLACK] / 4)] + + iResidualP50ByOwnArmy[MINU(7, pos->uArmyScaler[WHITE] / 4)]; + iSavedLazyPositionalBlack = + iKingP50ByAttackerArmyOnly[MINU(7, pos->uArmyScaler[WHITE] / 4)] + + iResidualP50ByOwnArmy[MINU(7, pos->uArmyScaler[BLACK] / 4)]; #else #ifdef EVAL_TIME - ctx->sCounters.tree.u64CyclesEvalLazyDecision += - (SystemReadTimeStampCounter() - uLazyDecisionTimer); - ctx->sCounters.tree.u64CyclesLazyExit += - (SystemReadTimeStampCounter() - uTimer); - ctx->sCounters.tree.u64CyclesInEval += - (SystemReadTimeStampCounter() - uTimer); -#endif - goto end; -#endif - } -#endif // !CALIBRATE_POSITIONAL -#ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalLazyDecision += (SystemReadTimeStampCounter() - uLazyDecisionTimer); + ctx->sCounters.tree.u64CyclesLazyExit += + (SystemReadTimeStampCounter() - uTimer); + ctx->sCounters.tree.u64CyclesInEval += + (SystemReadTimeStampCounter() - uTimer); +#endif + goto end; #endif } -#ifdef CALIBRATE_BASE_MARGIN - else - { - // - // The flat gate said "don't even try" -- measure whether the - // real, widened margin would have allowed an exit anyway had - // we bothered to compute it. A "yes" here is a false negative - // attributable to LAZY_EVAL_BASE_MARGIN being tighter than it - // needs to be. This extra EstimatePositionalScore call is - // measurement-only overhead, real exit behavior is unaffected - // (this is the branch where a real build already skips lazy - // eval entirely). - // - SCORE iMeasuredAlphaMargin = LAZY_EVAL_BASE_MARGIN; - SCORE iMeasuredBetaMargin = LAZY_EVAL_BASE_MARGIN; - ULONG uMatBucket = _MaterialBucket(pos); - - EstimatePositionalScore(ctx, pos, pHash, &iMeasuredAlphaMargin, - &iMeasuredBetaMargin); - if ((iScoreForSideToMove + iMeasuredAlphaMargin <= iAlpha) || - (iScoreForSideToMove - iMeasuredBetaMargin >= iBeta)) - { - g_uBaseMarginFalseNegative[uMatBucket]++; - } - else - { - g_uBaseMarginCorrectlyFull[uMatBucket]++; - } - } +#endif // !CALIBRATE_POSITIONAL +#ifdef EVAL_TIME + ctx->sCounters.tree.u64CyclesEvalLazyDecision += + (SystemReadTimeStampCounter() - uLazyDecisionTimer); #endif - } // LAZY_EVAL_MIN_MATERIAL + } // lazy eval #endif #ifdef EVAL_TIME @@ -6110,67 +7170,30 @@ Return value: (SystemReadTimeStampCounter() - uTimer); #endif - // - // If we have to clear/populate the attack table, do it now that - // we know we aren't taking a lazy exit. - // + // If we get here we are doing a full eval. + ctx->sPlyInfo[ctx->uPly].u64FullEvalSig = (pos->u64NonPawnSig ^ pos->u64PawnSig); + + // See if we have to clear/populate the pawn attack table. if (TRUE == fDeferred) { TIMED_EVAL_CALL(ctx, u64CyclesEvalAttackTablePop, - _PopulatePawnAttackBits(pos)); + _PopulatePawnAttackBits(pos)); } - // // Pre-compute some common terms used in per-piece evals: - // pos->uNumTrapped[BLACK] = pos->uNumTrapped[WHITE] = 0; pos->iReducedMaterialDownScaler[BLACK] = pos->iReducedMaterialDownScaler[WHITE] = 0; - - // - // This is a "position is closed|open" number. - // pos->uClosedScaler = (FastCountBits(pos->bbPawns[WHITE]) + FastCountBits(pos->bbPawns[BLACK])) - (pHash->uNumUnmovedPawns[WHITE] + pHash->uNumUnmovedPawns[BLACK]) + (pHash->uNumRammedPawns); ASSERT(pos->uClosedScaler >= 0); ASSERT(pos->uClosedScaler <= 32); + pos->uMinorsAtHome[BLACK] = pos->uMinorsAtHome[WHITE] = 0; // // Evaluate individual pieces. // - // board_representation/EVAL.md section 0/2: replaces the old - // cNonPawns[color][1..N] walk + per-piece mailbox lookup + p&0x4/ - // IS_KNIGHT branch dispatch (a hard-to-predict branch per piece, - // on top of a redundant mailbox read the callee already redoes - // for its own ASSERT) with a direct per-type location-bitboard - // walk. pos->bbPieces[color][PIECE_TYPE] already exists, - // incrementally maintained by move.c, and was simply unused by - // Eval() until now -- no new infrastructure, purely a consumer - // change. Also retires the cDefer/uDefer "remember the rooks and - // queens for later" bookkeeping entirely: since each type is now - // its own direct bitboard walk, "evaluate rooks after minors" is - // just "do the rook-bitboard walk after the minor-bitboard walks" - // -- nothing to defer. - // - // board_representation/EVAL.md section 9: the real dependency - // isn't "side to move first" -- it's "both colors' minors done - // before any rook or queen, both rooks done before any queen, - // everything above done before either king" (_EvalRook/_EvalQueen - // read the enemy's pos->bbMinorAttacks; _EvalKing reads both - // colors' bbMinorAttacks/bbQueenAttacks; _ReEvalPassers further - // down needs the king scores). Interleaving ours-then-enemy within - // each type (rather than both colors' minors, then both colors' - // rooks, then both colors' queens as separate phases) still - // satisfies that same ordering -- knight-vs-bishop order *within* - // a color, and encounter order within a single type's own bitboard - // walk, were never meaningful (cNonPawns' order is arbitrary - // swap-with-last-on-removal, not stable) and stay that way -- - // EVAL_TERM's plain score accumulation doesn't care. - // - pos->uMinorsAtHome[BLACK] = pos->uMinorsAtHome[WHITE] = 0; - uColor = pos->uToMove; - xColor = FLIP(uColor); // Knights. bb = pos->bbPieces[uColor][KNIGHT]; @@ -6182,8 +7205,12 @@ Return value: TIMED_EVAL_CALL(ctx, u64CyclesEvalKnight, _EvalKnight(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After our N at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After our N at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6196,10 +7223,14 @@ Return value: bb &= (bb - 1); c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalKnight, - _EvalKnight(pos, c, pHash)); + _EvalKnight(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After enemy N at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After enemy N at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6211,10 +7242,14 @@ Return value: bb &= (bb - 1); c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalBishop, - _EvalBishop(pos, c, pHash)); + _EvalBishop(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After our B at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After our B at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6228,8 +7263,12 @@ Return value: TIMED_EVAL_CALL(ctx, u64CyclesEvalBishop, _EvalBishop(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After enemy B at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After enemy B at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6242,8 +7281,12 @@ Return value: c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalRook, _EvalRook(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After our R at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After our R at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6256,8 +7299,12 @@ Return value: c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalRook, _EvalRook(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After enemy R at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After enemy R at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6270,8 +7317,12 @@ Return value: c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalQueen, _EvalQueen(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After our Q at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After our Q at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6284,8 +7335,12 @@ Return value: c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalQueen, _EvalQueen(pos, c, pHash)); #ifdef EVAL_DUMP - Trace("After enemy Q at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After enemy Q at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } @@ -6293,7 +7348,6 @@ Return value: // once here rather than in _EvalKing itself -- it's called twice // below (once per king) and used to recompute the identical // TotalNonPawnCount(WHITE)+TotalNonPawnCount(BLACK) sum both times. - { ULONG uTotalNonPawnsBothSides = TotalNonPawnCount(pos, WHITE) + TotalNonPawnCount(pos, BLACK); @@ -6310,8 +7364,12 @@ Return value: TIMED_EVAL_CALL(ctx, u64CyclesEvalKing, _EvalKing(pos, c, pHash, ctx, uTotalNonPawnsBothSides)); #ifdef EVAL_DUMP - Trace("After our K at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After our K at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif c = KingPosition(pos, xColor); @@ -6326,10 +7384,13 @@ Return value: #endif TIMED_EVAL_CALL(ctx, u64CyclesEvalKing, _EvalKing(pos, c, pHash, ctx, uTotalNonPawnsBothSides)); - } #ifdef EVAL_DUMP - Trace("After enemy K at %s:\n%d\t\t%d\n", - CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After enemy K at %s:\n%d\t\t%d\t\t(%d for %s)\n", + CoorToString(c), + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif // Now that we have the whole attack table generated, think about @@ -6343,71 +7404,171 @@ Return value: { _ReEvalPassers(pos, pHash); #ifdef EVAL_DUMP - Trace("After passers:\n%d\t\t%d\n", pos->iScore[WHITE], - pos->iScore[BLACK]); + Trace("After re-eval passers:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif } - // // _EvalLookForDanger/_EvalTrappedPieces only ever *add* a hint // when they find one; a full eval that finds nothing this time // would otherwise leave a stale, unrelated hint from an earlier // visit to this ply sitting here. Clear this ply's self-danger // slots first so a clean full eval reliably means a clean slate. - // ctx->cEnprise[ctx->uPly][0] = ctx->cEnprise[ctx->uPly][1] = ILLEGAL_COOR; ctx->cTrapped[ctx->uPly] = ILLEGAL_COOR; _EvalLookForDanger(ctx); _EvalTrappedPieces(ctx); - // We are now committed to the full-eval path (no more lazy/ - // super-lazy exits below this point) -- stamp this ply's - // PLY_INFO with the position we just fully evaluated so every - // "REQUIRES SIG MATCH" field this call populates (iKingScore - // below; cEnprise/cTrapped above; the eval-scoped bb*Attacks - // tables) can be validated by a downstream reader with one cheap - // compare against this position's live signature, instead of the - // old per-field piece-identity re-check. See PLY_INFO's own - // comment in chess.h for why this, not u64Sig, is the field to - // compare against. - ctx->sPlyInfo[ctx->uPly].u64FullEvalSig = - (pos->u64NonPawnSig ^ pos->u64PawnSig); - // TODO: endgame-specific knowledge? e.g. B over N in an endgame // with 2 pawn wings? // Roll in the reduced material down scaler terms (aggregate). ASSERT(pos->iReducedMaterialDownScaler[BLACK] > -200); ASSERT(pos->iReducedMaterialDownScaler[BLACK] < +200); - iAlphaMargin = (pos->iReducedMaterialDownScaler[BLACK] * - (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[BLACK]]) / 8; - pos->iScore[BLACK] += iAlphaMargin; + pos->iScore[BLACK] += ( + pos->iReducedMaterialDownScaler[BLACK] * + (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[BLACK]] + ) / 8; ASSERT(pos->iReducedMaterialDownScaler[WHITE] > -200); ASSERT(pos->iReducedMaterialDownScaler[WHITE] < +200); - iAlphaMargin = (pos->iReducedMaterialDownScaler[WHITE] * - (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[WHITE]]) / 8; - pos->iScore[WHITE] += iAlphaMargin; + pos->iScore[WHITE] += ( + pos->iReducedMaterialDownScaler[WHITE] * + (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[WHITE]] + ) / 8; #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalPostLazyMisc += (SystemReadTimeStampCounter() - u64PostLazyMiscTimer); #endif #ifdef EVAL_DUMP - Trace("After all pieces:\n%d\t\t%d\n", - pos->iScore[WHITE], pos->iScore[BLACK]); + Trace("After all pieces:\n%d\t\t%d\t\t(%d for %s)\n", + pos->iScore[WHITE], pos->iScore[BLACK], + (pos->iScore[uColor] - pos->iScore[xColor]), + COLOR_NAME(uColor) + ); #endif // Almost done - iScoreForSideToMove = (pos->iScore[pos->uToMove] - - pos->iScore[FLIP(pos->uToMove)]); + iScoreForSideToMove = (pos->iScore[uColor] - pos->iScore[xColor]); - // Drive drawish situations towards the draw score. - TIMED_EVAL_CALL(ctx, u64CyclesEvalDrawish, - EvalLookForDrawishSituations(pos, &iScoreForSideToMove)); +#ifdef DUMP_RESIDUAL_SAMPLES + // One CSV-ish line per full Eval() call: combined army scaler + // (total pieces proxy), signed army-scaler diff (piece-balance + // proxy), and each side's isolated residual (mobility/trapped/ + // misc, with material/pawns/passers/bad-trade/bishop-pairs/king + // safety already subtracted out via the baseline captured after + // bishop pairs, above, and the real per-side king score). Sampled + // before EvalLookForDrawishSituations' squash below, since that + // squash is a separate, already-handled mechanism, not part of + // what iResidualP98 is meant to estimate. + { + SCORE iResidualWhite = pos->iScore[WHITE] - iResidualBaselineWhite - + ctx->sPlyInfo[ctx->uPly].iKingScore[WHITE]; + SCORE iResidualBlack = pos->iScore[BLACK] - iResidualBaselineBlack - + ctx->sPlyInfo[ctx->uPly].iKingScore[BLACK]; - // - // TODO: detect and discourage blocked positions? - // + // Histogram, not a raw per-sample Trace -- at 30k-position + // scale a raw dump would run into the billions of lines (a + // 200-position sample alone produced 8.76M). Bucketed by OWN + // army scaler (color-blind: each side contributes its own + // row), same convention as the earlier ad hoc Python analysis. + _RecordResidualSample(MINU(7, pos->uArmyScaler[WHITE] / 4), + (INT)iResidualWhite); + _RecordResidualSample(MINU(7, pos->uArmyScaler[BLACK] / 4), + (INT)iResidualBlack); + + // Super-lazy-scoped residual: same idea, but relative to + // super-lazy's own (material-only) baseline, so pawns/passers/ + // bad-trade/bishop-pairs are included instead of excluded -- + // this is what super-lazy's own margin_a/margin_b construction + // actually needs (2026-09-17). + { + SCORE iSLResidualWhite = pos->iScore[WHITE] - + iSuperLazyResidualBaselineWhite - + ctx->sPlyInfo[ctx->uPly].iKingScore[WHITE]; + SCORE iSLResidualBlack = pos->iScore[BLACK] - + iSuperLazyResidualBaselineBlack - + ctx->sPlyInfo[ctx->uPly].iKingScore[BLACK]; + _RecordSuperLazyResidualSample(MINU(7, pos->uArmyScaler[WHITE] / 4), + (INT)iSLResidualWhite); + _RecordSuperLazyResidualSample(MINU(7, pos->uArmyScaler[BLACK] / 4), + (INT)iSLResidualBlack); + } + + // Outlier FEN capture -- thresholds picked comfortably beyond + // the p10/p90 range seen in every bucket of the 200-position + // smoke sample (worst bucket's p90/p10 topped out around + // +/-230), so this only fires for genuine tail events worth + // going back and looking at by hand, not routine variance. + // 2026-09-17: capped -- the 200-position sample this threshold + // was tuned against never came close to needing a cap, but the + // far larger/more-varied 30k stratified pool hits this on a + // real fraction of nodes and produced 2GB+ of Trace output + // before filling /tmp on the first attempt. The histogram data + // above is unaffected by this cap; only the (nice-to-have, + // not essential) worked-example FEN capture is limited. + if (((abs((int)iResidualWhite) > 250) || (abs((int)iResidualBlack) > 250)) && + (g_uResidualOutliersPrinted < 200)) + { + CHAR *pszOutlierFen = PositionToFen(pos); + if (NULL != pszOutlierFen) + { + Trace("RESIDUAL_OUTLIER_FEN resW=%d resB=%d fen=%s\n", + (int)iResidualWhite, (int)iResidualBlack, pszOutlierFen); + SystemFreeMemory(pszOutlierFen); + g_uResidualOutliersPrinted++; + } + } + + // Same color-blind, per-side idea for king safety: each side's + // REAL, already-scaled king contribution (ctx->sPlyInfo[...] + // .iKingScore[color] -- scaled by REDUCED_MATERIAL_DOWN_SCALER + // inside _EvalKing already, so this is the true realized + // magnitude, not the pre-scale raw value the old iKingSwingP90 + // table was fit against), paired with the ATTACKER's own army + // (the side actually threatening that king), not the king + // owner's. Bucketing directly by attacker army this way means + // no runtime rescale step is needed at all in the redesign -- + // the bucketing itself carries the army dependence. + _RecordKingSafetySample(MINU(7, pos->uArmyScaler[BLACK] / 4), + (INT)ctx->sPlyInfo[ctx->uPly].iKingScore[WHITE]); + _RecordKingSafetySample(MINU(7, pos->uArmyScaler[WHITE] / 4), + (INT)ctx->sPlyInfo[ctx->uPly].iKingScore[BLACK]); + + // Same idea as the residual outlier capture above -- threshold + // picked beyond the p10/p90 range seen in every king-safety + // bucket (worst bucket topped out around +/-160). + if (((abs((int)ctx->sPlyInfo[ctx->uPly].iKingScore[WHITE]) > 200) || + (abs((int)ctx->sPlyInfo[ctx->uPly].iKingScore[BLACK]) > 200)) && + (g_uKingSafetyOutliersPrinted < 200)) + { + CHAR *pszOutlierFen = PositionToFen(pos); + if (NULL != pszOutlierFen) + { + Trace("KINGSAFETY_OUTLIER_FEN kingW=%d kingB=%d fen=%s\n", + (int)ctx->sPlyInfo[ctx->uPly].iKingScore[WHITE], + (int)ctx->sPlyInfo[ctx->uPly].iKingScore[BLACK], + pszOutlierFen); + SystemFreeMemory(pszOutlierFen); + g_uKingSafetyOutliersPrinted++; + } + } + } +#endif + + // Drive drawish situations towards the draw score. Usually a + // no-op fetch here (sDrawishFacts already computed by the + // regular-lazy attempt above) -- only does real work when that + // attempt was skipped entirely (bare-king/low-material nodes, + // see LAZY_EVAL_MIN_MATERIAL/fBareKingEitherSide above), in which + // case this is that node's only call site. + TIMED_EVAL_CALL(ctx, u64CyclesEvalDrawish, + (_EnsureDrawishFactsComputed(pos, uColor, &sDrawishFacts), + EvalLookForDrawishSituations(pos, &iScoreForSideToMove, + &sDrawishFacts))); // Adjust dynamic positional component. Unlike the lazy-exit // margins above (always >= 0 by construction), this can go @@ -6459,12 +7620,18 @@ Return value: (iSavedSuperLazyScore < iAlpha) ? MAX0(iScoreForSideToMove - iSavedSuperLazyScore) : MAX0(iSavedSuperLazyScore - iScoreForSideToMove), - iSuperLazyMargin); + iSavedSuperLazyMarginUsed); iScoreForSideToMove = iSavedSuperLazyScore; if (NULL != piPositional) { - (*piPositional)[WHITE] = 0; - (*piPositional)[BLACK] = 0; + ULONG uWhiteBucket = MINU(7, pos->uArmyScaler[WHITE] / 4); + ULONG uBlackBucket = MINU(7, pos->uArmyScaler[BLACK] / 4); + (*piPositional)[WHITE] = + iKingP50ByAttackerArmyOnly[uBlackBucket] + + iSuperLazyResidualP50ByOwnArmy[uWhiteBucket]; + (*piPositional)[BLACK] = + iKingP50ByAttackerArmyOnly[uWhiteBucket] + + iSuperLazyResidualP50ByOwnArmy[uBlackBucket]; // CALIBRATE_MARGIN_SAFETY builds: the FULL tag set above // reflects the fallthrough that just ran for measurement // purposes, not what a normal build actually returns here @@ -6497,8 +7664,8 @@ Return value: iScoreForSideToMove = iSavedLazyScore; if (NULL != piPositional) { - (*piPositional)[WHITE] = iSavedLazyPositional; - (*piPositional)[BLACK] = iSavedLazyPositional; + (*piPositional)[WHITE] = iSavedLazyPositionalWhite; + (*piPositional)[BLACK] = iSavedLazyPositionalBlack; ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_LAZY; } } @@ -6511,6 +7678,7 @@ Return value: uTimer); #endif INC(ctx->sCounters.tree.u64FullEvals); + g_u64FullEvalsByBucket[_MaterialBucket(pos)]++; end: ASSERT(IS_VALID_SCORE(iScoreForSideToMove)); |
