/** Copyright (c) Scott Gasch Module Name: eval.c Abstract: Position evaluation routines. Author: Scott Gasch (scott.gasch@gmail.com) 14 Jun 2004 Revision History: $Id: eval.c 357 2008-07-03 16:18:11Z scott $ **/ #include "chess.h" // // CountBits and the CoorFromBitBoard{Rank8ToRank1,Rank1ToRank8} // wrappers (bitboard.c) are real out-of-line asm calls on every one of // eval.c's ~20 production call sites (passed-pawn detection, bishop- // pair logic, etc.), hit on every real Eval() call. CountBits' asm // body (x64.asm) isn't even O(1) popcnt, it's a Kernighan bit-clearing // loop -- O(popcount) iterations plus call overhead. Redirected via // #define, the same mechanism chess.h already uses for the OTHER // direction (#ifdef CROUTINES routes CountBits/FirstBit/LastBit to the // slow C fallbacks) -- this routes them to the fast inline // FastCountBits/FastCoorFromBitBoardRank8ToRank1/ // FastCoorFromBitBoardRank1ToRank8 wrappers (chess.h) instead, for // every existing call site below with no further edits. Those used to // be duplicated here as file-local statics; promoted to chess.h so // every other translation unit's bitboard-scan loops (recogn.c, // board.c, generate.c, movesup.c, probe.c, testmove.c -- added during // the cNonPawns/cPawns/uPawnCount/uNonPawnCount retirement work) get // the fast path too, not just eval.c -- confirmed by disassembly that // those files were still emitting real callq+PLT relocations to the // slow extern versions before this. Gated on !CROUTINES so the // CROUTINES debug/comparison build still gets the real (slow, // cross-checked) implementations. // #ifndef CROUTINES #define CountBits FastCountBits #define CoorFromBitBoardRank8ToRank1 FastCoorFromBitBoardRank8ToRank1 #define CoorFromBitBoardRank1ToRank8 FastCoorFromBitBoardRank1ToRank8 #endif // !CROUTINES // // To simplify code / maintenance I use the same loop for both colors // in some places. These globals coorespond to "ahead of the piece" // or "behind the piece" for each color. // const int g_iAhead[2] = { +1, -1 }; const int g_iBehind[2] = { -1, +1 }; // // General eval terms // --------------------------------------------------------------------------- // //static SCORE TRADE_PIECES[3][17] = //{// 0 1 2 3 4 5 6 7 8 | 9..15 -- down piece count // { -1, 90, 81, 73, 67, 62, 57, 52, 50, 50,50,50,50,50,50,50,-1}, // { -1, 125, 117, 109, 100, 91, 84, 78, 71, 66,66,66,66,66,66,66,-1}, // { -1, 150, 132, 124, 116, 109, 102, 94, 87, 77,77,77,77,77,77,77,-1}, //}; //static SCORE DONT_TRADE_PAWNS[3][9] = //{// 0 1 2 3 4 5 6 7 8 -- up pawn count // { -43, -15, 0, +3, +6, +10, +15, +21, +28 }, // // 0 // { -63, -25, 0, +4, +9, +14, +19, +25, +32 }, // // 1 // { -10, 0, 0, +7, +15, +20, +24, +29, +36 }, // // 2 //}; static ULONG REDUCED_MATERIAL_DOWN_SCALER[32] = { // none na na m na R 2m na 0, 0, 0, 0, 0, 0, 0, 0, // Rm 3m/Q 2R R2m 4m/Qm 2Rm R3m/QR Q2m 0, 1, 1, 1, 1, 1, 2, 3, // 2R2m R4m/QRm Q3m 2R3m/ QR2m Q4m 2R4m/ QR3m // Q2R Q2Rm 4, 5, 6, 6, 7, 7, 7, 7, // na Q2R2m QR4m na Q2R3m na na full 8, 8, 8, 8, 8, 8, 8, 8, }; // // Same shape as REDUCED_MATERIAL_DOWN_SCALER (0 with little material left, // ramping to 8 with a full army) but decays to 0 much sooner -- "haven't // castled yet" / "minors still at home" are opening-specific concerns that // stop mattering well before the point where real king danger fades, so // this table's zero region extends much further up the index range than // REDUCED_MATERIAL_DOWN_SCALER's does. // static ULONG CASTLE_AND_DEVELOPMENT_SCALER[32] = { // none na na m na R 2m na 0, 0, 0, 0, 0, 0, 0, 0, // Rm 3m/Q 2R R2m 4m/Qm 2Rm R3m/QR Q2m 0, 0, 0, 0, 0, 0, 0, 0, // 2R2m R4m/QRm Q3m 2R3m/ QR2m Q4m 2R4m/ QR3m // Q2R Q2Rm 0, 1, 2, 3, 4, 5, 6, 7, // na Q2R2m QR4m na Q2R3m na na full 7, 8, 8, 8, 8, 8, 8, 8, }; static ULONG REDUCED_MATERIAL_UP_SCALER[32] = { // none na na m na R 2m na 8, 8, 8, 8, 8, 8, 8, 8, // Rm 3m/Q 2R R2m 4m/Qm 2Rm R3m/QR Q2m 8, 7, 7, 7, 7, 6, 6, 5, // 2R2m R4m/QRm Q3m 2R3m/ QR2m Q4m 2R4m/ QR3m // Q2R Q2Rm 4, 3, 3, 3, 2, 2, 2, 1, // na Q2R2m QR4m na Q2R3m na na full 1, 1, 0, 0, 0, 0, 0, 0, }; static ULONG PASSER_MATERIAL_UP_SCALER[32] = { // none na na m na R 2m na 8, 8, 8, 8, 8, 8, 7, 7, // Rm 3m/Q 2R R2m 4m/Qm 2Rm R3m/QR Q2m 7, 6, 7, 5, 4, 5, 4, 3, // 2R2m R4m/QRm Q3m 2R3m/ QR2m Q4m 2R4m/ QR3m // Q2R Q2Rm 2, 2, 1, 1, 0, 0, 0, 0, // na Q2R2m QR4m na Q2R3m na na full 0, 0, 0, 0, 0, 0, 0, 0, }; COOR QUEENING_RANK[2] = { 1, 8 }; COOR JUMPING_RANK[2] = { 7, 2 }; // // Pawn eval terms // --------------------------------------------------------------------------- // // "There is one case which can be treated as positional or material, // namely the rook's pawn, which differs from other pawns in that it // can only capture one way instead of two. Since this handicap cannot // be corrected without the opponent's help, I teach my students to // regard the rook's pawn as a different piece type, a crippled // pawn. Database statistics indicate that it is on average worth // about 15% less than a normal pawn. The difference is enough so that // it is usually advantageous to make a capture with a rook's pawn, // promoting it to a knights pawn, even if that produces doubled pawns // and even if there is no longer a rook on the newly opened rook's // file." // --Larry Kaufman, IM // "Evaluation of Material Imbalance" // static SCORE PAWN_CENTRALITY_BONUS[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0, -8, 0, 0, 0, 0, 0, 0, -8, 0,0,0,0,0,0,0,0, -8, 0, 5, 5, 5, 5, 0, -8, 0,0,0,0,0,0,0,0, -8, 0, 5, 8, 8, 5, 0, -8, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- -8, 0, 5, 8, 8, 5, 0, -8, 0,0,0,0,0,0,0,0, -8, 0, 5, 5, 5, 5, 0, -8, 0,0,0,0,0,0,0,0, -8, 0, 0, 0, 0, 0, 0, -8, 0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0 }; // Hand-restored 2026-08-30: the DNA-tuned values here had drifted to // mostly-positive across the board (a shielded backward pawn was scoring // as a +12..+17 bonus on most files, and several EXPOSED entries were // less negative than the corresponding SHIELDED ones on the same file -- // i.e. "more exposed" scored better than "less exposed"). Both the // ASSERT(BACKWARD_SHIELDED_BY_LOCATION[c] < 0) and the analogous EXPOSED // assert below had been silently commented out rather than investigated. // Replaced with a monotonic, strictly-negative, rank-mirrored shape: // backward pawns are always a defect, exposed is always worse than // shielded at the same rank, and the penalty grows as the pawn advances // toward the more contested center ranks (4/5) where a backward pawn is // most exploitable. Table is indexed by absolute square (not // color-relative), hence the rank2/rank7, rank3/rank6, rank4/rank5 // mirroring -- same as the original layout. static SCORE BACKWARD_SHIELDED_BY_LOCATION[128] = { +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0, -7, -5, -5, -7, -7, -5, -5, -7, 0,0,0,0,0,0,0,0, -5, -4, -4, -5, -5, -4, -4, -5, 0,0,0,0,0,0,0,0, -4, -3, -3, -4, -4, -3, -3, -4, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- -4, -3, -3, -4, -4, -3, -3, -4, 0,0,0,0,0,0,0,0, -5, -4, -4, -5, -5, -4, -4, -5, 0,0,0,0,0,0,0,0, -7, -5, -5, -7, -7, -5, -5, -7, 0,0,0,0,0,0,0,0, +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0 }; static SCORE BACKWARD_EXPOSED_BY_LOCATION[128] = { +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0, -12, -9, -9, -11, -11, -9, -9, -12, 0,0,0,0,0,0,0,0, -9, -7, -7, -9, -9, -7, -7, -9, 0,0,0,0,0,0,0,0, -7, -6, -6, -7, -7, -6, -6, -7, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- -7, -6, -6, -7, -7, -6, -6, -7, 0,0,0,0,0,0,0,0, -9, -7, -7, -9, -9, -7, -7, -9, 0,0,0,0,0,0,0,0, -12, -9, -9, -11, -11, -9, -9, -12, 0,0,0,0,0,0,0,0, +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0 }; // // "The first statement I can confirm is that doubled pawns are indeed // on average undesirable. However this statistic needs to be broken // down to be useful. Doubled pawns themselves are really more // serious than this generally, but when your pawns are doubled you // automatically get an extra half-open file for your rooks (or queen, // but the queen can also use diagonals). So it follows logically // that the net cost of doubled pawns is much greater in the absence // of major pieces. The database shows that with all rooks present, // doubled pawns "cost" only about 1/16th of a pawn on average. With // one rook each the cost rises to 1/4th of a pawn and with no rooks // present to 3/8ths of a pawn. With the queens present the cost is // again only 1/16th of a pawn; without them it's a quarter pawn. So // the lessons are clear; beware of doubled pawns when major pieces // have been exchanged, and beware of exchanging major pieces when you // are the one with the doubled pawns." // --Larry Kaufman, IM // "All About Doubled Pawns" // // Note: indexed by number of files with 1+ pawn on them. static SCORE DOUBLED_PAWN_PENALTY_BY_COUNT[4][9] = { // ------------ count of doubled+ pawns ------------ {// 0 1 2 3 4 5 6 7 8 : no majors alive +0, -32, -65, -99, -134, -170, -207, -222, -250 }, {// : 1 rook +0, -23, -47, -76, -108, -144, -184, -200, -216 }, {// : 2 rooks or 1 queen +0, -13, -23, -34, -46, -64, -86, -111, -138 }, {// : all majors alive +0, -7, -13, -25, -39, -55, -73, -95, -121 }, }; static SCORE ISOLATED_PAWN_BY_PAWNFILE[9] = { 0, -7, -8, -9, -10, -10, -9, -8, -7 }; static SCORE ISOLATED_EXPOSED_PAWN = -5; // Turned down (2026-08-30): a per-pawn modifier that stacks on top of // DOUBLED_PAWN_PENALTY_BY_COUNT's whole-position doubled-pawn aggregate // for the specific case of a pawn that's both isolated and doubled -- // a different angle (single-worst-case flag vs. whole-position // severity), not the same fact counted twice, so scaled down rather // than removed. static SCORE ISOLATED_DOUBLED_PAWN = -7; // // Note: -25% to -33% if the enemy occupies or controls the next sq. // static SCORE PASSER_BY_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0,+162,+111, +62, +36, +18, +13, +0 }, // black { +0, +0, +13, +18, +36, +62,+111,+162, +0 } // white }; static SCORE CANDIDATE_PASSER_BY_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0, +0, +48, +34, +22, +13, +9, +0 }, // black { +0, +0, +9, +13, +22, +34, +48, +0, +0 } // white }; // Note: x2 // Scaled to 1/3 of the original hand-tuned magnitude (2026-08-30): these // three terms, plus PASSER_BY_RANK and OUTSIDE_PASSER_BY_DISTANCE, all // separately price "how good is this passer" from different angles // (connected to a partner passer, backed by an ordinary pawn, outside // the opposing pawn majority) and can stack for the same pawn -- a // rank-7 connected+outside passer could hit ~370cp in this pawn-hash- // cached family alone, ~9x Crafty's ~40cp ceiling for the equivalent // concept. Rather than remove any of these (they're each pricing a // genuinely distinct structural fact, not re-counting the same one -- // and pawn-hash caching makes the compute cost of keeping all of them // free), turned the magnitude down on the three secondary/modifier // terms so the stack lands in a saner range; PASSER_BY_RANK (the core // "how far advanced" signal) is left as the primary, least-reduced // term. static SCORE CONNECTED_PASSERS_BY_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0, +32, +25, +16, +7, +3, +2, +0 }, // black { +0, +0, +2, +3, +7, +16, +25, +32, +0 } // white }; static SCORE SUPPORTED_PASSER_BY_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0, +20, +13, +4, +2, +1, +1, +0 }, // black { +0, +0, +1, +1, +2, +4, +13, +20, +0 } // white }; // Split out 2026-08-30 from SUPPORTED_PASSER_BY_RANK, which _EvalKing's // "kings in front of passers" endgame bonus was silently reusing -- // pawn-support and king-escort are different concepts (this fires when // the KING stands next to its own passer, not when a pawn does), so // scaling one for its real purpose was silently also scaling the // other. Seeded with SUPPORTED_PASSER_BY_RANK's original (pre-scaling) // hand-tuned magnitude, since that's what this use case was actually // getting before today's pawn-hash pass touched the shared table for // an unrelated reason. static SCORE KING_SUPPORTING_OWN_PASSER_BY_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0, +60, +40, +13, +6, +3, +1, +0 }, // black { +0, +0, +1, +3, +6, +13, +40, +60, +0 } // white }; static SCORE OUTSIDE_PASSER_BY_DISTANCE[9] = {// 0 1 2 3 4 5 6 7 8 +0, +0, +2, +5, +7, +9, +11, +14, +18 }; static SCORE PASSER_BONUS_AS_MATERIAL_COMES_OFF[32] = { // none na na m na R 2m na +60, -1, -1, +37, -1, +43, +19, -1, // Rm 3m/Q 2R R2m 4m/Qm 2Rm R3m/QR Q2m +14, +9, +20, +4, +3, +3, 0, 0, // 2R2m R4m/QRm Q3m 2R3m/ QR2m Q4m 2R4m/ QR3m // Q2R Q2Rm 0, 0, 0, 0, 0, 0, 0, 0, // na Q2R2m QR4m na Q2R3m na na full 0, 0, 0, 0, 0, 0, 0, 0, }; // 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] = {// 0 1 2 3 4 0, -6, -10, -16, -22 }; // // Bishop eval terms // --------------------------------------------------------------------------- // static SCORE BISHOP_OVER_KNIGHT_IN_ENDGAME = +33; // // "The bishop pair has an average value of half a pawn (more when the // opponent has no minor pieces to exchange for one of the bishops), // enough to regard it as part of the material evaluation of the // position, and enough to overwhelm most positional considerations. // Moreover, this substantial bishop pair value holds up in all // situations tested, regardless of what else is on the board... // // ...One rule which I often teach to students is that if you have the // bishop pair, and your opponent's single bishop is a bad bishop // (hemmed in by his own pawns), you already have full compensation // for a pawn... Kasparov has said something similar... // // As noted before, the bishop pair is worth more with fewer pawns on // the board. Aside from this factor, the half pawn value of the // bishop pair is remarkably constant, applying even when there are no // pieces on the board except two minors each." // --Larry Kaufman, IM // "Evaluation of Material Imbalance" static SCORE BISHOP_PAIR_BONUS = 40; static SCORE STATIONARY_PAWN_ON_BISHOP_COLOR[128] = { +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0, -4, -6, -7, -7, -7, -7, -6, -4, 0,0,0,0,0,0,0,0, -6, -7, -9, -8, -8, -9, -7, -6, 0,0,0,0,0,0,0,0, -7, -8, -10, -11, -11, -10, -8, -7, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- -7, -8, -10, -11, -11, -10, -8, -7, 0,0,0,0,0,0,0,0, -6, -7, -9, -8, -8, -9, -7, -6, 0,0,0,0,0,0,0,0, -4, -6, -7, -7, -7, -7, -6, -4, 0,0,0,0,0,0,0,0, +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0 }; static SCORE TRANSIENT_PAWN_ON_BISHOP_COLOR[128] = { +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0, -1, -1, -2, -2, -2, -2, -1, -1, 0,0,0,0,0,0,0,0, -3, -4, -4, -4, -4, -4, -4, -3, 0,0,0,0,0,0,0,0, -4, -5, -7, -8, -8, -7, -5, -4, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- -4, -5, -7, -8, -8, -7, -5, -4, 0,0,0,0,0,0,0,0, -3, -4, -4, -4, -4, -4, -4, -3, 0,0,0,0,0,0,0,0, -1, -1, -2, -2, -2, -2, -1, -1, 0,0,0,0,0,0,0,0, +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0 }; // // A bishop can move to between 0..13 squares. // static SCORE BISHOP_MOBILITY_BY_SQUARES[14] = {// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 -22, -14, -10, -5, -1, 0, +1, +3, +4, +5, +6, +7, +8, +9 };// ^ | static SCORE BISHOP_UNASSAILABLE_BY_DIST_FROM_EKING[9] = {// 0 1 2 3 4 5 6 7 8 +0, +33, +28, +18, +8, +4, +0, -10, -16 }; // // Knight eval terms // --------------------------------------------------------------------------- // static SCORE KNIGHT_CENTRALITY_BONUS[128] = { -12, -8, -8, -8, -8, -8, -8, -12, 0,0,0,0,0,0,0,0, -8, -2, -2, 0, 0, -2, -2, -8, 0,0,0,0,0,0,0,0, -8, -2, +4, +5, +5, +4, -2, -8, 0,0,0,0,0,0,0,0, -8, -2, +5, +8, +8, +5, -2, -8, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- -8, -2, +5, +8, +8, +5, -2, -8, 0,0,0,0,0,0,0,0, -8, -2, +4, +5, +5, +4, -2, -8, 0,0,0,0,0,0,0,0, -8, -2, -2, 0, 0, -2, -2, -8, 0,0,0,0,0,0,0,0, -12, -8, -8, -8, -8, -8, -8, -12, 0,0,0,0,0,0,0,0 }; static SCORE KNIGHT_KING_TROPISM_BONUS[9] = {// 0 1 2 3 4 5 6 7 8 0, +15, +12, +9, +4, +0, +0, +0, +0 }; static SCORE KNIGHT_UNASSAILABLE_BY_DIST_FROM_EKING[9] = {// 0 1 2 3 4 5 6 7 8 +0, +25, +19, +14, +6, +0, +0, +0, +0 }; static SCORE KNIGHT_ON_INTERESTING_SQUARE_BY_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +12, +11, +9, +6, +3, +0, +0, +0 }, // black { +0, +0, +0, +0, +3, +6, +9, +11, +12 } // white }; // // A knight can move to between 0..8 squares // ----------------------------------------- // 0 = supported enemy pawn // 1 = unsupported enemy pawn, friend pawn, enemy B/N // enemy controlled empty sq. // 2 = enemy >B, friend controlled empty sq, no one controlled empty sq // // Total: 16 mobility max // static SCORE KNIGHT_MOBILITY_BY_COUNT[9] = {// 0 1 2 3 4 5 6 7 8 -17, -10, -6, 0, +3, +5, +7, +8, +8 };// ^ | static SCORE KNIGHT_WITH_N_PAWNS_SUPPORTING[3] = { +0, +4, +8 }; static SCORE KNIGHT_IN_CLOSED_POSITION[33] = {// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 9, 11, 12, 13, 15, 17, 19, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27 // 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 }; // // Rook eval terms // --------------------------------------------------------------------------- // // Simplified 2026-09-06 (board_representation/EVAL.md section 9): flat // average bonus, not scaled by distance from the enemy king -- by direct // instruction. Each was a [8]-by-distance table; these are that table's // simple mean, rounded. static SCORE ROOK_ON_FULL_OPEN = 18; static SCORE ROOK_ON_HALF_OPEN_WITH_ENEMY = 9; static SCORE ROOK_ON_HALF_OPEN_WITH_FRIEND = 10; // Cache of the three constants above, indexed [friend pawn on file?] // [enemy pawn on file?] so _EvalRook's hot path is a single array read // instead of rebuilding a local array every call. Not `static const` // -- the three source constants are DNA-tunable globals (plain mutable // SCORE, not compile-time constants), so this has to be a plain // mutable array, rebuilt by InitEval() whenever they change (startup, // and after every `evaldna read`), not frozen at compile time. static SCORE ROOK_FULL_HALF_OPEN_BONUS[2][2] = { { 16, 9 }, { 10, 0 } }; // Same treatment for the enemy/friend-passer-on-file bonuses -- was // PASSER_BY_RANK[xColor][rank]/4 (enemy case) and a rank-scaled // behind/leads-passer split (friend case, +1..+25 behind, -3..-22 in // front); flat means of the meaningful (non-padding) entries. static SCORE ROOK_WITH_ENEMY_PASSER = 17; static SCORE ROOK_WITH_FRIEND_PASSER = 10; // A rook on the 7th (White) or 2nd (Black) rank attacks along the // enemy's back-ranks-minus-one, hitting undeveloped/unmoved pawns and // cutting the enemy king off from that rank -- a small, classic bonus // independent of the file-openness/passer bonuses above. BBRANK22/ // BBRANK77 are compile-time constants (chess.h), so this table (unlike // ROOK_FULL_HALF_OPEN_BONUS above) can be a real `static const`: it // depends on the board geometry, not on any DNA-tunable value. static const BITBOARD BBROOK_7TH_RANK[2] = { BBRANK22, BBRANK77 }; // [BLACK], [WHITE] static SCORE ROOK_ON_SEVENTH_BONUS = 9; static SCORE ROOK_BEHIND_PASSER_BY_PASSER_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0, +25, +17, +12, +6, +1, +0, +0 }, // black { +0, +0, +0, +1, +6, +12, +17, +25, +0 } // white }; static SCORE ROOK_LEADS_PASSER_BY_PASSER_RANK[2][9] = {// 0 1 2 3 4 5 6 7 8 { +0, +0, -22, -16, -13, -9, -5, -3, +0 }, // black { +0, +0, -3, -5, -9, -13, -16, -22, +0 } // white }; static SCORE KING_TRAPPING_ROOK = -40; static SCORE ROOK_VALUE_AS_PAWNS_COME_OFF[17] = {// 0 1 2 3 4 5 6 7 8 +55, +51, +44, +38, +33, +27, +22, +16, +7, // 9 10 11 12 13 14 15 16 +1, -3, -6, -9, -12, -18, -22, -25 }; // // Note: these are multiplied by two (one per rook). // static SCORE ROOK_CONNECTED_VERT = +7; // x2 static SCORE ROOK_CONNECTED_HORIZ = +4; // x2 // // A rook can move to between 0..14 squares // ---------------------------------------- // 0 = supported enemy P/N/B, friend piece // 1 = unsupported enemy piece, enemy controlled empty sq, enemy R // 2 = enemy >R, friend controlled empty sq, no one controlled empty sq // // Total: 28 mobility max // static SCORE ROOK_MOBILITY_BY_SQUARES[15] = {// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 -28, -24, -20, -14, -7, -2, +0, +4, +8, +12, +15, +17, +19, +21, +22 };// ^ | // // Queen eval terms // --------------------------------------------------------------------------- // // // A queen can move to between 0..27 squares // ----------------------------------------- // 0 = supported enemy P/N/B/R, friend piece // 1 = unsupported enemy piece, enemy controlled empty sq, enemy Q // 2 = enemy K, no one controlled empty sq, friend controlled empty sq // // Total: 54 mobility max // static SCORE QUEEN_MOBILITY_BY_SQUARES[28] = {// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 -30, -26, -22, -17, -11, -8, -4, -2, -1, +2, +4, +7, +10, +12, +14, // | //15 16 17 18 19 20 21 22 23 24 25 26 27 +15, +16, +17, +18, +19, +20, +20, +21, +21, +22, +22, +23, +23 }; // // In "Evaluation of Material Imbalance", IM Larry Kaufman makes the // point that queens are worth more then the standard nine "points". // Thus, this scale is biased upwards by about 0.15 pawn. // static SCORE QUEEN_KING_TROPISM[8] = {// 0 1 2 3 4 5 6 7 0, +34, +28, +22, +19, +17, +16, +15 }; // // King eval terms // --------------------------------------------------------------------------- // // 2026-09-15: this table is ONLY for CountKingSafetyDefects (the cheap // lazy-eval-proxy/search-extension-reduction-gate sibling of _EvalKing, // called directly from search.c/searchsup.c independent of Eval() ever // running) -- deliberately frozen/split off from _EvalKing's own real // computation (see KING_EVAL_INITIAL_LOCATION below), which used to // read this exact array. That coupling meant any future retune of // _EvalKing's king-location term would silently perturb search // extension/reduction gate frequency too, an unrelated and much // higher-blast-radius side effect than "retune an eval term" should // have. DO NOT repoint _EvalKing at this table again -- add to/reshape // KING_EVAL_INITIAL_LOCATION instead. See king_safety_data/HANDOFF.md's // 2026-09-15 addendum. static ULONG KING_INITIAL_COUNTER_BY_LOCATION[2][128] = { { 2, 0, 0, 0, 1, 0, 0, 2, 0,0,0,0,0,0,0,0, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0,0,0,0,0,0, 3, 3, 3, 3, 3, 3, 3, 3, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0 }, { 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 3, 3, 3, 3, 3, 3, 3, 3, 0,0,0,0,0,0,0,0, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0,0,0,0,0,0, 2, 0, 0, 0, 1, 0, 0, 2, 0,0,0,0,0,0,0,0 } }; // 2026-09-15: _EvalKing's own copy -- same starting values as // KING_INITIAL_COUNTER_BY_LOCATION above (no retuning done yet, this // split is purely about ownership/decoupling), but SCORE-typed (this // function's real ring-scan is SCORE-native post-rewrite, no more // shared uCounter) and free to be reshaped independently -- e.g. the // offboard-ring-exposure idea from king_safety_data/HANDOFF.md's // session notes (a1-style corner exposure, distinct from this table's // current pure-rank shape) without touching CountKingSafetyDefects' // search-gate behavior at all. static SCORE KING_EVAL_INITIAL_LOCATION[2][128] = { { 1, 0, 0, 0, 1, 0, 0, 1, 0,0,0,0,0,0,0,0, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0,0,0,0,0,0, 3, 3, 3, 3, 3, 3, 3, 3, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0 }, { 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------- 4, 4, 4, 4, 4, 4, 4, 4, 0,0,0,0,0,0,0,0, 3, 3, 3, 3, 3, 3, 3, 3, 0,0,0,0,0,0,0,0, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0,0,0,0,0,0, 1, 0, 0, 0, 1, 0, 0, 1, 0,0,0,0,0,0,0,0 } }; static SCORE KING_TO_CENTER[128] = { +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0, +1, +3, +5, +7, +7, +5, +3, +1, 0,0,0,0,0,0,0,0, +3, +5, +13, +15, +15, +13, +5, +3, 0,0,0,0,0,0,0,0, +5, +7, +17, +23, +23, +17, +7, +5, 0,0,0,0,0,0,0,0, // ------------------------------------------------------------------ +5, +7, +17, +23, +23, +17, +7, +5, 0,0,0,0,0,0,0,0, +3, +5, +13, +15, +15, +13, +5, +3, 0,0,0,0,0,0,0,0, +1, +3, +5, +7, +7, +5, +3, +1, 0,0,0,0,0,0,0,0, +0, +0, +0, +0, +0, +0, +0, +0, 0,0,0,0,0,0,0,0 }; // // If a side has less than this much material we don't bother scoring // the other side's king safety. Note: this doesn't incl pawn // material. // #define DO_KING_SAFETY_THRESHOLD \ (VALUE_ROOK + VALUE_BISHOP + VALUE_KING + 1) // // If a side has less than this much material the other side's king // can come out. Note: this doesn't incl pawn material. // #define KEEP_KING_AT_HOME_THRESHOLD \ (VALUE_ROOK + VALUE_ROOK + VALUE_BISHOP + VALUE_KING + 1) static ULONG KING_COUNTER_BY_ATTACK_PATTERN[32] = { //p 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 //m 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 //r 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 //q 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 //k 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0,0,2,2,1,1,3,3,1,1,3,3,2,2,4,4,2,2,3,3,2,2,4,4,2,2,4,4,3,4,5,5 }; static SCORE KING_SAFETY_BY_COUNTER[42] = { -0, -4, -8, -12, -16, -20, -25, -31, -38, -50, -63, -76, -90, -105,-120,-135,-150,-165,-180,-195,-210,-225,-240,-255,-270,-290, -310,-330,-350,-370,-390,-410,-440,-470,-500,-500,-500,-500,-500, -500,-500,-500 }; // Added 2026-08-30: penalty for the enemy queen directly attacking or // x-raying squares near this king, indexed by count (capped at 6). // Replaces the old per-queen "pointing near enemy K" bolt-on // (QUEEN_ATTACKS_SQ_NEXT_TO_KING, computed via the queen's own // mobility ray-cast, direct-attacks only) with a version computed from // data this loop already reads for king safety (no extra attack-table // work) and that also catches x-ray/latent queen threats the old // version missed. Calibrated against the removed term's own magnitude // (uNearKing * 8, capped at 6 -> max 48) rather than guessed, since // that's what was empirically shown to matter for real solve rate -- // see the 180-position king-safety-term distribution sampled // 2026-08-30 (median -27, p75 -16, only ~1% worse than -200) for the // scale this needs to slot into. // 2026-09-14: rescaled from the original -8/count step (see the // 2026-08-30 comment above -- that step was calibrated against a // *removed* legacy term's own magnitude, not against real mate data). // king_safety_data/HANDOFF.md's mate-mining logistic regression found // `queenproximity` (after folding in the collinear front/back/sq3/sq4 // atk_queen sub-features it was absorbing -- see // eval_tune/king_safety_data/HANDOFF.md's 2026-09-14 addendum) has a // per-unit raw coefficient of +1.00, calibrated against // `misscastleshort`'s own cleaned coefficient (+0.71) and this table's // sibling constant KING_MISSING_ONE_CASTLE_OPTION's already-live -23 // value (same direct-EVAL_TERM unit family, unlike the file-storm // tables below which feed the separate uCounter/KING_SAFETY_BY_COUNTER // lookup and need their own, different calibration) -- see // (misscastleshort_live / misscastleshort_raw_coef) = k, then // k * queenproximity_raw_coef =~ -32/count. // // -32/count self-play-tested net negative (2780 games, score 0.490, // llr -1.91 trending to H0) alongside the back-flight bonus below -- // consistent with this term's own weaker calibration confidence flagged // up front (anchored against misscastleshort's never-independently- // validated historical value, a bigger single-step jump than the // file-storm retune's already-large ~1.75-2x). Direction still looks // right (a real, non-collinear predictor), so trimmed 33% to -21/count // rather than reverted outright. Re-validate via sd10 + self-play // before trusting this magnitude either. // -21/count + material scaling (see above) pulled self-play from // clearly negative (score 0.490, llr -1.91 unscaled) to nearly flat // (770 games, score 0.494, llr -0.34, drifting slowly) -- scaling was // the bigger fix, but still trending slightly negative. Trimmed // another ~33% to -14/count. static SCORE KING_QUEEN_PROXIMITY_DANGER[7] = {// 0 1 2 3 4 5 6 +0, -14, -28, -42, -56, -70, -84 }; static SCORE KING_MISSING_ONE_CASTLE_OPTION = -23; // 2026-09-14: new term, not a retune of an existing one. // king_safety_data/HANDOFF.md's regression found `back_flight` (an // empty, unattacked ring square on the side of the king AWAY from the // attacker) is a real, non-collinear PROTECTIVE signal (negative raw // coefficient -0.035, i.e. more open escape squares behind the king // predicts LESS mate risk) -- unlike `front_flight`, which is a real // danger signal already captured by EmptyUnattackedSquare's front // weight. Calibrated the same way as KING_QUEEN_PROXIMITY_DANGER above // (misscastleshort-anchored k =~ -32.4, applied to back_flight's raw // coefficient) =~ +1.1/square, originally rounded to +1 -- but that // +1 was never independently self-play tested in isolation (the // "worth landing and validating" below didn't happen before other // king-safety changes piled on top of it), so it never carried the // weight of a validated constant, just a first guess like everything // else in this file. // // 2026-09-15: nudged +1 -> +3. Every regression refit run this session // (the original mate-mining regression, and 6,890-row and 92,468-row // ElasticNet/Ridge fits on curated mate/control data) puts // back_flight's magnitude at roughly half of front_flight's // (KING_RING_FRONT_FLIGHT_PENALTY, -6); the +1 baseline's ratio (1/6) // was much smaller than that. Small and deliberately so -- this is // the single most novel, least-collinear finding from the regression, // worth landing and validating in isolation before combining with // anything else. static SCORE KING_BACK_FLIGHT_BONUS = 3; // 2026-09-15: _EvalKing's linear-SCORE ring-scan terms, promoted here // from local statics inside that function so they're DNA-registered // (see g_EvalDNA below) and Texel-tunable -- see that promotion's // comment in _EvalKing for why (hand-translating mate-mining // regression coefficients into centipawns via a single assumed anchor // constant was the likely source of this rewrite's self-play // regression, not the underlying feature signal). Starting values are // unchanged from the original hand-translated guesses; only their // storage location/tunability changed here. // // 2026-09-15 (later same day): nudged, NOT re-derived from scratch -- // absolute-magnitude derivation (regression coefficient -> centipawns // via a single anchor constant, then Texel coordinate descent on top of // curated data) was tried and abandoned; every method produced huge, // untrustworthy magnitude swings (see eval_tune/ king-safety session // notes). What *did* replicate cleanly across the original mate-mining // regression, a small-scale (6,890-row) and full-scale (92,468-row) // ElasticNet/Ridge refit was the *relative ranking* between terms, so // only reordering/small nudges within the existing scale are applied: // - MINOR: data unanimously ranks side ~ front > back in danger; // baseline had back rated *most* dangerous (backwards) -- permuted // the same three magnitudes rather than resizing them. // - ROOK: baseline's front > back > side already matched the data's // ranking untouched. // - PAWN: data ranks pawn-ring-attacks mid-tier (stronger than // back-tier piece attacks), baseline had it tied for smallest -- // small bump. // - UNDEFENDED: baseline already had back as the largest magnitude, // matching the one sub-signal (back_undef) that was stable across // every fit -- left alone. front/side undefended-square counts were // the single most consistently weak/sign-unstable feature across // every regression run tried (zeroed by ElasticNet, near-zero and // sign-flipping under Ridge) -- softened slightly rather than left // implying confidence the data doesn't support. // - XRAY: consistently signed, non-trivial magnitude in every fit; // baseline had it smallest in the whole file -- small bump. // - FRONT_FLIGHT_PENALTY: already correctly signed/positioned once // PAWN moved above it -- untouched. // - BACK_FLIGHT_BONUS (see above): data puts this at roughly half of // FRONT_FLIGHT_PENALTY's magnitude; baseline's ratio was much // smaller than that -- small bump. // KING_QUEEN_PROXIMITY_DANGER and KING_MISSING_ONE_CASTLE_OPTION // deliberately NOT touched: the former is already self-play validated // and not cleanly re-derivable at this feature granularity (a 7-bucket // nonlinear table vs. one linear coefficient); the latter applies one // shared constant to both short- and long-castle loss even though the // regression finds those two nowhere near equally dangerous -- fixing // that is a structural change (splitting the constant), not a nudge, // and wasn't made without discussing it first. // // 2026-09-15 (later still): PAWN and XRAY nudged again after a THIRD, // independent calibration method (logistic regression against material // balance -- computed straight from the FEN via python-chess, not // anything derived from these same ring features, so it doesn't have // iSafety's circularity problem) confirmed the same finding as the // first pass: both terms are still ranked mid/low here while every // method tried puts them near the top tier, comparable to front-tier // piece attacks. Rank order elsewhere (9 of 13 comparable terms) // already matched this newest fit without further changes, including // UNDEFENDED[back] and both flight terms -- only these two still // disagreed, so only these two moved again. static SCORE KING_RING_ATTACK_MINOR[3] = { -15, -14, -17 }; static SCORE KING_RING_ATTACK_ROOK[3] = { -14, -11, -8 }; static SCORE KING_RING_ATTACK_PAWN = -12; static SCORE KING_RING_UNDEFENDED[3] = { -4, -12, -5 }; static SCORE KING_RING_XRAY_BONUS = -8; static SCORE KING_RING_FRONT_FLIGHT_PENALTY = -6; static SCORE KING_INITIAL_LOCATION_SCALE = -3; static SCORE KING_ATTACK_PATTERN_SCALE = -10; static SCORE KING_DANGER_ACCELERATOR[7] = {// 0-55 56-111 112-167 168-223 224-279 280-335 336+ 128, 175, 225, 200, 145, 128, 128 }; // Flat "this is bad" flags for _EvalTrappedPieces -- not an attempt to // price the material outcome (search resolves that), just a nudge away // from positions with a piece that looks stuck. ENPRISE_AND_TRAPPED is // larger because that case is opponent-to-move / imminently capturable; // TRAPPED_WITH_MOVE is our own move, so there's still a chance to // wriggle out. static SCORE ENPRISE_AND_TRAPPED_PENALTY = -50; static SCORE TRAPPED_WITH_MOVE_PENALTY = -10; typedef struct _DNA_BASE_SIZE { SCORE *pBase; ULONG uCount; } DNA_BASE_SIZE; #define DNA_VAR(x) {(SCORE *)&(x), 1} #define DNA_ARRAY(x) {(SCORE *)(x), ARRAY_LENGTH(x)} #define DNA_MATRIX(x) {(SCORE *)(x), ARRAY_LENGTH(x) * ARRAY_LENGTH((x)[0])} static DNA_BASE_SIZE g_EvalDNA[] = { DNA_ARRAY(REDUCED_MATERIAL_DOWN_SCALER), DNA_ARRAY(CASTLE_AND_DEVELOPMENT_SCALER), DNA_ARRAY(REDUCED_MATERIAL_UP_SCALER), DNA_ARRAY(PASSER_MATERIAL_UP_SCALER), DNA_ARRAY(PAWN_CENTRALITY_BONUS), DNA_ARRAY(BACKWARD_SHIELDED_BY_LOCATION), DNA_ARRAY(BACKWARD_EXPOSED_BY_LOCATION), DNA_MATRIX(DOUBLED_PAWN_PENALTY_BY_COUNT), DNA_ARRAY(ISOLATED_PAWN_BY_PAWNFILE), DNA_VAR(ISOLATED_EXPOSED_PAWN), DNA_VAR(ISOLATED_DOUBLED_PAWN), DNA_MATRIX(PASSER_BY_RANK), DNA_MATRIX(CANDIDATE_PASSER_BY_RANK), DNA_MATRIX(CONNECTED_PASSERS_BY_RANK), DNA_MATRIX(SUPPORTED_PASSER_BY_RANK), DNA_MATRIX(KING_SUPPORTING_OWN_PASSER_BY_RANK), DNA_ARRAY(OUTSIDE_PASSER_BY_DISTANCE), DNA_ARRAY(PASSER_BONUS_AS_MATERIAL_COMES_OFF), DNA_VAR(RACER_WINS_RACE), DNA_ARRAY(UNDEVELOPED_MINORS_IN_OPENING), DNA_VAR(BISHOP_OVER_KNIGHT_IN_ENDGAME), DNA_VAR(BISHOP_PAIR_BONUS), DNA_ARRAY(STATIONARY_PAWN_ON_BISHOP_COLOR), DNA_ARRAY(TRANSIENT_PAWN_ON_BISHOP_COLOR), DNA_ARRAY(BISHOP_MOBILITY_BY_SQUARES), DNA_ARRAY(BISHOP_UNASSAILABLE_BY_DIST_FROM_EKING), DNA_ARRAY(KNIGHT_CENTRALITY_BONUS), DNA_ARRAY(KNIGHT_KING_TROPISM_BONUS), DNA_ARRAY(KNIGHT_UNASSAILABLE_BY_DIST_FROM_EKING), DNA_MATRIX(KNIGHT_ON_INTERESTING_SQUARE_BY_RANK), DNA_ARRAY(KNIGHT_MOBILITY_BY_COUNT), DNA_ARRAY(KNIGHT_WITH_N_PAWNS_SUPPORTING), DNA_ARRAY(KNIGHT_IN_CLOSED_POSITION), DNA_VAR(ROOK_ON_FULL_OPEN), DNA_VAR(ROOK_ON_HALF_OPEN_WITH_ENEMY), DNA_VAR(ROOK_ON_HALF_OPEN_WITH_FRIEND), DNA_VAR(ROOK_WITH_ENEMY_PASSER), DNA_VAR(ROOK_WITH_FRIEND_PASSER), DNA_VAR(ROOK_ON_SEVENTH_BONUS), DNA_MATRIX(ROOK_BEHIND_PASSER_BY_PASSER_RANK), DNA_MATRIX(ROOK_LEADS_PASSER_BY_PASSER_RANK), DNA_VAR(KING_TRAPPING_ROOK), DNA_ARRAY(ROOK_VALUE_AS_PAWNS_COME_OFF), DNA_VAR(ROOK_CONNECTED_VERT), DNA_VAR(ROOK_CONNECTED_HORIZ), DNA_ARRAY(ROOK_MOBILITY_BY_SQUARES), DNA_ARRAY(QUEEN_MOBILITY_BY_SQUARES), DNA_ARRAY(QUEEN_KING_TROPISM), DNA_MATRIX(KING_INITIAL_COUNTER_BY_LOCATION), DNA_MATRIX(KING_EVAL_INITIAL_LOCATION), DNA_ARRAY(KING_TO_CENTER), DNA_ARRAY(KING_SAFETY_BY_COUNTER), DNA_ARRAY(KING_QUEEN_PROXIMITY_DANGER), DNA_VAR(KING_MISSING_ONE_CASTLE_OPTION), DNA_VAR(KING_BACK_FLIGHT_BONUS), DNA_ARRAY(KING_RING_ATTACK_MINOR), DNA_ARRAY(KING_RING_ATTACK_ROOK), DNA_VAR(KING_RING_ATTACK_PAWN), DNA_ARRAY(KING_RING_UNDEFENDED), DNA_VAR(KING_RING_XRAY_BONUS), DNA_VAR(KING_RING_FRONT_FLIGHT_PENALTY), DNA_VAR(KING_INITIAL_LOCATION_SCALE), DNA_VAR(KING_ATTACK_PATTERN_SCALE), DNA_ARRAY(KING_DANGER_ACCELERATOR) }; ULONG DNABufferSizeBytes() { ULONG u; ULONG uSize = 0; for (u = 0; u < ARRAY_LENGTH(g_EvalDNA); u++) { uSize += 10 * g_EvalDNA[u].uCount; } return uSize + 1; } char * ExportEvalDNA() { ULONG uSize = DNABufferSizeBytes(); char *p = malloc(uSize); ULONG u, v; SCORE *q; FLAG fFirst = TRUE; memset(p, 0, uSize); for (u = 0; u < ARRAY_LENGTH(g_EvalDNA); u++) { q = g_EvalDNA[u].pBase; for (v = 0; v < g_EvalDNA[u].uCount; v++) { if (!fFirst) { sprintf(p, "%s,%d", p, *q); } else { sprintf(p, "%s%d", p, *q); fFirst = FALSE; } q++; } strcat(p, "\n"); fFirst = TRUE; } return p; // caller must free } FLAG WriteEvalDNA(char *szFilename) { FILE *p = NULL; char *q = NULL; FLAG fRet = FALSE; if (SystemDoesFileExist(szFilename)) goto end; p = fopen(szFilename, "a+b"); if (!p) goto end; q = ExportEvalDNA(); if (!q) goto end; fprintf(p, "%s", q); fRet = TRUE; end: if (p) fclose(p); if (q) free(q); return fRet; } FLAG ImportEvalDNA(char *p) { ULONG u, v; for (u = 0; u < ARRAY_LENGTH(g_EvalDNA); u++) { for (v = 0; v < g_EvalDNA[u].uCount; v++) { while(*p && (!isdigit(*p) && (*p != '-'))) p++; if (*p == '\0') return FALSE; *(g_EvalDNA[u].pBase + v) = atoi(p); while(*p && (isdigit(*p) || (*p == '-'))) p++; } } return TRUE; } FLAG ReadEvalDNA(char *szFilename) { FILE *p = NULL; ULONG uSize = DNABufferSizeBytes(); char *q = malloc(uSize); FLAG fRet = FALSE; static char line[1024]; char *c; if (!q) goto end; memset(q, 0, uSize); p = fopen(szFilename, "rb"); if (!p) { Trace("Failed to open file \"%s\"\n", szFilename); goto end; } while(fgets(line, 1024, p) != NULL) { c = strchr(line, '#'); if (c) *c = '\0'; c = line; strcat(q, c); } fRet = ImportEvalDNA(q); end: if (q) free(q); if (p) fclose(p); return fRet; } // // Misc stuff // --------------------------------------------------------------------------- // // // The three ranks "around" a rank (indexed by rank, used in eval.c) // BITBOARD BBADJACENT_RANKS[9] = { 0, BBRANK11 | BBRANK22, BBRANK11 | BBRANK22 | BBRANK33, BBRANK22 | BBRANK33 | BBRANK44, BBRANK33 | BBRANK44 | BBRANK55, BBRANK44 | BBRANK55 | BBRANK66, BBRANK55 | BBRANK66 | BBRANK77, BBRANK66 | BBRANK77 | BBRANK88, BBRANK77 | BBRANK88 }; // // The files "around" one, indexed by file (used in eval.c) // BITBOARD BBADJACENT_FILES[8] = { // A = 0 BBFILEB, // B = 1 BBFILEA | BBFILEC, // C = 2 BBFILEB | BBFILED, // D = 3 BBFILEC | BBFILEE, // E = 4 BBFILED | BBFILEF, // F = 5 BBFILEE | BBFILEG, // G = 6 BBFILEF | BBFILEH, // H = 7 BBFILEG }; // // Same as BBADJACENT_FILES, but including the file itself -- callers // that used to OR in BBFILE[file] separately at every call (only one // site, eval.c's _EvalKing) can index this instead and skip the extra // load + OR. // BITBOARD BBADJACENT_FILES_INCLUSIVE[8] = { // A = 0 BBFILEA | BBFILEB, // B = 1 BBFILEA | BBFILEB | BBFILEC, // C = 2 BBFILEB | BBFILEC | BBFILED, // D = 3 BBFILEC | BBFILED | BBFILEE, // E = 4 BBFILED | BBFILEE | BBFILEF, // F = 5 BBFILEE | BBFILEF | BBFILEG, // G = 6 BBFILEF | BBFILEG | BBFILEH, // H = 7 BBFILEG | BBFILEH }; // // The ranks preceeding one, indexed by rank/color of pawn (used in // eval.c) // BITBOARD BBPRECEEDING_RANKS[8][2] = { // BLACK WHITE // ----- ----- { // 0 == RANK8 BBRANK72, 0 }, { // 1 == RANK7 BBRANK62, 0 }, { // 2 == RANK6 BBRANK52, BBRANK77 }, { // 3 == RANK5 BBRANK42, BBRANK67 }, { // 4 == RANK4 BBRANK32, BBRANK57 }, { // 5 == RANK3 BBRANK22, BBRANK47 }, { // 6 == RANK2 0, BBRANK37 }, { // 7 == RANK1 0, BBRANK27 } }; static FLAG _IsSquareSafeFromEnemyPawn(IN POSITION *pos, IN COOR c, IN BITBOARD bb) /** Routine description: Determine if a piece in square c is "outposted". A piece is outposted if no enemy pawns can advance to attack it / drive it away. Parameters: POSITION *pos : the board COOR c : the square (must not be empty!) BITBOARD bb : the bitboard of pawns you want to consider Return value: FLAG : TRUE if the piece is safe/outposted, FALSE otherwise **/ { ULONG uColor = GET_COLOR(pos->rgSquare[c].pPiece); ULONG uFile, uRank; #ifdef DEBUG COOR cSquare; PIECE p = pos->rgSquare[c].pPiece; BITBOARD dbb = bb; ASSERT(IS_ON_BOARD(c)); ASSERT(p && (IS_BISHOP(p) || IS_KNIGHT(p) || IS_PAWN(p))); ASSERT(IS_VALID_COLOR(uColor)); #endif uFile = FILE(c); ASSERT(uFile < 8); bb &= BBADJACENT_FILES[uFile]; ASSERT(((c & 0x70) >> 4) == (c >> 4)); uRank = c >> 4; ASSERT(uRank < 8); bb &= BBPRECEEDING_RANKS[uRank][uColor]; #ifdef DEBUG if (bb != 0) { while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&dbb))) { ASSERT(pos->rgSquare[cSquare].pPiece); ASSERT(IS_PAWN(pos->rgSquare[cSquare].pPiece)); if ((FILE(cSquare) == (FILE(c) - 1)) || (FILE(cSquare) == (FILE(c) + 1))) { switch(uColor) { case WHITE: if (RANK(cSquare) > RANK(c)) { return(FALSE); } break; case BLACK: if (RANK(cSquare) < RANK(c)) { return(FALSE); } break; } } } ASSERT(FALSE); } else { ASSERT(bb == 0); while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&dbb))) { ASSERT(pos->rgSquare[cSquare].pPiece); ASSERT(IS_PAWN(pos->rgSquare[cSquare].pPiece)); if ((FILE(cSquare) == (FILE(c) - 1)) || (FILE(cSquare) == (FILE(c) + 1))) { switch(uColor) { case WHITE: if (RANK(cSquare) > RANK(c)) { ASSERT(FALSE); } break; case BLACK: if (RANK(cSquare) < RANK(c)) { ASSERT(FALSE); } break; } } } } #endif return(bb == 0); } static ULONG _WhoControlsSquareFast(IN POSITION *pos, IN COOR c) /** Routine description: Determine which side controls a board square. Parameters: POSITION *pos : the board COOR c : the square in question Return value: static ULONG : (ULONG)-1 if neither side controls it or the sides control it evenly, WHITE if white controls the square, or BLACK if black controls the square. **/ { // // board_representation/EVAL.md section 9: bvAttacks/ATTACK_BITV // retired entirely now that king (the last piece to write it) has // converted -- every side's presence at this square is read // straight off its own bbXAttacks accumulator instead. Bit // positions (PAWN_BIT/MINOR_BIT/ROOK_BIT/QUEEN_BIT/KING_BIT) are // unchanged so g_SwapTable's indexing below still sees the same // shape it always has; x-ray-only presence (bbMinorXrayAttacks/ // bbRookXrayAttacks/bbQueenXrayAttacks) ORs into the same // byte-scale bit as its direct-attack counterpart, matching the // old struct's .uSmall/.uXray sharing one bit position for // "attacks or x-rays" (see g_SwapTable's own construction). King // has no x-ray (can't move through a blocker). This function is // only ever called after *both* kings finish evaluating (the // passed-pawn re-check and trapped-piece/danger passes all run // after Eval()'s king-eval block), so pos->bbKingAttacks is always // fully populated for both colors here -- unlike _EvalKing's own, // deliberately asymmetric internal bvAttack (see that function's // comment), this is a plain, symmetric fact query. // BITBOARD sq = COOR_TO_BB(c); ULONG uWhite = ((pos->bbPawnAttacks[WHITE] & sq) ? PAWN_BIT : 0) | ((pos->bbMinorAttacks[WHITE] & sq) ? MINOR_BIT : 0) | ((pos->bbMinorXrayAttacks[WHITE] & sq) ? MINOR_BIT : 0) | ((pos->bbRookAttacks[WHITE] & sq) ? ROOK_BIT : 0) | ((pos->bbRookXrayAttacks[WHITE] & sq) ? ROOK_BIT : 0) | ((pos->bbQueenAttacks[WHITE] & sq) ? QUEEN_BIT : 0) | ((pos->bbQueenXrayAttacks[WHITE] & sq) ? QUEEN_BIT : 0) | ((pos->bbKingAttacks[WHITE] & sq) ? KING_BIT : 0); ULONG uBlack = ((pos->bbPawnAttacks[BLACK] & sq) ? PAWN_BIT : 0) | ((pos->bbMinorAttacks[BLACK] & sq) ? MINOR_BIT : 0) | ((pos->bbMinorXrayAttacks[BLACK] & sq) ? MINOR_BIT : 0) | ((pos->bbRookAttacks[BLACK] & sq) ? ROOK_BIT : 0) | ((pos->bbRookXrayAttacks[BLACK] & sq) ? ROOK_BIT : 0) | ((pos->bbQueenAttacks[BLACK] & sq) ? QUEEN_BIT : 0) | ((pos->bbQueenXrayAttacks[BLACK] & sq) ? QUEEN_BIT : 0) | ((pos->bbKingAttacks[BLACK] & sq) ? KING_BIT : 0); ULONG u; PIECE p; CHAR ch; // g_SwapTable is only [14][32][32] (data.c) -- this is the bound // that actually matters, not just "no garbage above bit 7" below. // PAWN_BIT..KING_BIT briefly didn't fit in bits 0-4 earlier this // session (when _WhoControlsSquareFast was converted to read them // directly instead of going through the old ATTACK_BITV struct) // and silently indexed out of bounds on every attacked square -- // the (& 0xFFFFFF00) checks below never caught it (board_ // representation/EVAL.md section 9). ASSERT(uWhite < 32); ASSERT(uBlack < 32); ASSERT((uWhite & 0xFFFFFF00) == 0); ASSERT((uBlack & 0xFFFFFF00) == 0); p = pos->rgSquare[c].pPiece; ch = g_SwapTable[p][uWhite][uBlack]; if (ch != 0) { u = ch; u >>= 7; u = u & 1; ASSERT(((u == 1) && (ch < 0)) || ((u == 0) && (ch > 0))); return(FLIP(u)); } return((ULONG)-1); } /** Routine description: Zero out the Eval()-scoped attack-bitboard accumulators before building them for this call. Used to clear the old per-square rgSquare[c|8].bvAttacks/ATTACK_BITV structure too (a full-board macro-unrolled loop, since every square's storage needed zeroing); retired along with that structure (2026-09-05, board_ representation/EVAL.md section 9) -- nothing left to clear but the bbXAttacks accumulators themselves. Parameters: POSITION *pos Return value: void **/ static void _ClearAttackTables(IN OUT POSITION *pos) { pos->bbMinorAttacks[WHITE] = pos->bbMinorAttacks[BLACK] = 0; pos->bbMinorXrayAttacks[WHITE] = pos->bbMinorXrayAttacks[BLACK] = 0; pos->bbRookAttacks[WHITE] = pos->bbRookAttacks[BLACK] = 0; pos->bbRookXrayAttacks[WHITE] = pos->bbRookXrayAttacks[BLACK] = 0; pos->bbQueenAttacks[WHITE] = pos->bbQueenAttacks[BLACK] = 0; pos->bbQueenXrayAttacks[WHITE] = pos->bbQueenXrayAttacks[BLACK] = 0; pos->bbKingAttacks[WHITE] = pos->bbKingAttacks[BLACK] = 0; } static INLINE void _InitializePawnHashEntry(IN OUT PAWN_HASH_ENTRY *pHash, IN POSITION *pos) /** Routine description: Parameters: PAWN_HASH_ENTRY *pHash, POSITION *pos Return value: static INLINE void **/ { memset(pHash, 0, sizeof(PAWN_HASH_ENTRY)); pHash->u64Key = pos->u64PawnSig; } static INLINE void _EvaluateCandidatePasser(IN POSITION *pos, IN OUT PAWN_HASH_ENTRY *pHash, IN COOR c) /** Routine description: Parameters: POSITION *pos, PAWN_HASH_ENTRY *pHash, COOR c Return value: static INLINE **/ { COOR c1, cSquare; PIECE pSentry, pHelper; ULONG ucFile = FILE(c); ULONG uPawnFile = ucFile + 1; BITBOARD bb; ULONG uSentries, uHelpers; ULONG uColor; ULONG xColor; int d1; #ifdef DEBUG COOR cVerifySquare; ULONG uVerifySentries; #endif ASSERT(IS_ON_BOARD(c)); ASSERT(RANK(c) >= 2); ASSERT(RANK(c) <= 7); pHelper = pos->rgSquare[c].pPiece; ASSERT(IS_PAWN(pHelper)); uColor = GET_COLOR(pHelper); ASSERT(IS_VALID_COLOR(uColor)); xColor = FLIP(uColor); // The only way a pawn can be a passer/candidate if the other // side has a pawn on its same file is if that enemy pawn is // behind it. This is a corner case but it is important to // detect all passers. if (pHash->uCountPerFile[xColor][uPawnFile] != 0) { ASSERT((pos->bbPawns[xColor] & BBFILE[ucFile]) != 0); switch(uColor) { case WHITE: bb = pos->bbPawns[BLACK] & BBFILE[ucFile]; ASSERT(bb); while(IS_ON_BOARD(c1 = CoorFromBitBoardRank8ToRank1(&bb))) { ASSERT(FILE(c1) == ucFile); if (c1 < c) return; } break; case BLACK: bb = pos->bbPawns[WHITE] & BBFILE[ucFile]; ASSERT(bb); while(IS_ON_BOARD(c1 = CoorFromBitBoardRank1ToRank8(&bb))) { ASSERT(FILE(c1) == ucFile); if (c1 > c) return; } break; } } // Count xColor's sentry pawns and determine the location of the // critical square for the pawn at c to become a passer. Note if // there are no sentries then this pawn (on square c) is a passer // already, not a candidate. bb = pos->bbPawns[xColor] & BBADJACENT_FILES[ucFile]; bb &= BBPRECEEDING_RANKS[(c & 0x70) >> 4][uColor]; if (!bb) { ASSERT(CountBits(bb) == 0); // There are no enemy sentries so this pawn is a passer. pHash->bbPasserLocations[uColor] |= COOR_TO_BB(c); ASSERT(CountBits(pHash->bbPasserLocations[uColor]) > 0); ASSERT(CountBits(pHash->bbPasserLocations[uColor]) <= 8); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], PASSER_BY_RANK[uColor][RANK(c)], "passed pawn"); // However, don't give doubled passers such a big bonus. if (IS_PAWN(pos->rgSquare[c - 16 * g_iAhead[uColor]].pPiece)) { EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], -(PASSER_BY_RANK[uColor][RANK(c)] / 2), "doubled passer"); } return; } uSentries = CountBits(bb); // There's one or more sentry pawns so we'll look for friendly // helpers to decide if this pawn is a candidate passer. First // figure out the critical square we need to get the passer // through. // // Cheap short-circuit first: an advanced pawn (rank 6/7 for WHITE, // rank 2/3 for BLACK) with a friend pawn on either adjacent file // already qualifies as a candidate regardless of what the (far // more expensive) helper-square walk below would find -- the two // conditions are OR'd together at the bottom of this function, so // computing uHelpers when this is already true is pure wasted // work. Skip straight to the candidate-passer bonus in that case. // if ((pHash->uCountPerFile[uColor][uPawnFile + 1] + pHash->uCountPerFile[uColor][uPawnFile - 1]) && (((WHITE == uColor) && (RANK(c) > 5)) || ((BLACK == uColor) && (RANK(c) < 4)))) { goto give_candidate_bonus; } if (uColor == WHITE) { pSentry = BLACK_PAWN; cSquare = CoorFromBitBoardRank1ToRank8(&bb); cSquare += 0x10; } else { pSentry = WHITE_PAWN; cSquare = CoorFromBitBoardRank8ToRank1(&bb); cSquare -= 0x10; } cSquare &= 0xF0; cSquare |= ucFile; ASSERT(IS_ON_BOARD(cSquare)); ASSERT(FILE(cSquare) == ucFile); #ifdef DEBUG uVerifySentries = 0; cVerifySquare = 0; d1 = 16 * g_iAhead[uColor]; c1 = c + d1; do { if (IS_ON_BOARD(c1 - 1)) { if (pos->rgSquare[c1 - 1].pPiece == pSentry) { uVerifySentries++; if (0 == cVerifySquare) { cVerifySquare = c1 - d1; ASSERT(cVerifySquare == cSquare); } } } if (IS_ON_BOARD(c1 + 1)) { if (pos->rgSquare[c1 + 1].pPiece == pSentry) { uVerifySentries++; if (0 == cVerifySquare) { cVerifySquare = c1 - d1; ASSERT(cVerifySquare == cSquare); } } } c1 = c1 + d1; } while(!RANK8(c1) && !RANK1(c1)); ASSERT(uVerifySentries == uSentries); #endif // // Note: we don't do this with bitboards because we want to not // consider a pawn to be a potential passer if it can't safely // advance to get into helper position. // // IDEA: scale the candidate passer bonus based on rank AND on // the distance the helper(s) have to go to get into position. // // FIXED 2026-09-05 (found while removing rgSquare[c|8].bvAttacks // entirely, board_representation/EVAL.md section 9; landed as its // own change with its own before/after check, not bundled into // that mechanical cleanup commit). This function runs from // _EvalPawns, which is the *first* piece type evaluated each // Eval() call, so the only attack data that actually exists yet is // pos->bbPawnAttacks -- every non-pawn piece, and (since commit // 57502d6) pawns' own old bvAttacks write, happens later in the // same call. The original condition here read the old combined // bvAttacks word (all piece types), which had been silently // always-zero -- and this gate silently always-true -- since // 57502d6; narrowed to what's actually valid at this point: // pos->bbPawnAttacks only. This is narrower than the original // presumably intended (any piece type, not just pawns), but it's // a real, correct check instead of a fake one, and pawns are the // dominant real-world case for contesting a helper-pawn square // anyway. // uHelpers = 0; d1 = 16 * g_iAhead[uColor]; ASSERT(-d1 == 16 * g_iBehind[uColor]); c1 = cSquare + 1 - d1; if ((IS_ON_BOARD(c1)) && (pHash->uCountPerFile[uColor][FILE(c1) + 1])) { ASSERT(pos->bbPawns[uColor] & BBFILE[FILE(c1)]); if (!(pos->bbPawnAttacks[xColor] & COOR_TO_BB(c1)) || (pos->bbPawnAttacks[uColor] & COOR_TO_BB(c1))) { // Friend helper pawn there? if (pos->rgSquare[c1].pPiece == pHelper) { uHelpers = 1; goto do_left; } // There is no helper pawn in the support position yet. // See if one can get there. c1 = c1 - d1; while (IS_ON_BOARD(c1) && ((!(pos->bbPawnAttacks[xColor] & COOR_TO_BB(c1))) || (pos->bbPawnAttacks[uColor] & COOR_TO_BB(c1)))) { if (pos->rgSquare[c1].pPiece == pHelper) { uHelpers = 1; break; } else if (pos->rgSquare[c1].pPiece == pSentry) { break; } c1 = c1 - d1; } } } do_left: c1 = cSquare - 1 - d1; if ((IS_ON_BOARD(c1)) && (pHash->uCountPerFile[uColor][FILE(c1) + 1])) { ASSERT(pos->bbPawns[uColor] & BBFILE[FILE(c1)]); if (!(pos->bbPawnAttacks[xColor] & COOR_TO_BB(c1)) || (pos->bbPawnAttacks[uColor] & COOR_TO_BB(c1))) { // Already a helper there? if (pos->rgSquare[c1].pPiece == pHelper) { uHelpers++; goto done_helpers; } // There is no pawn in the left support position yet. See // if one can get there. c1 -= d1; while (IS_ON_BOARD(c1) && ((!(pos->bbPawnAttacks[xColor] & COOR_TO_BB(c1))) || (pos->bbPawnAttacks[uColor] & COOR_TO_BB(c1)))) { if (pos->rgSquare[c1].pPiece == pHelper) { uHelpers++; break; } else if (pos->rgSquare[c1].pPiece == pSentry) { break; } c1 -= d1; } } } done_helpers: if (uHelpers >= uSentries) { give_candidate_bonus: ASSERT(CANDIDATE_PASSER_BY_RANK[uColor][RANK(c)] > 0); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], CANDIDATE_PASSER_BY_RANK[uColor][RANK(c)], "candidate passer"); // If the other side has no pieces then give this candidate an // extra bonus -- used to be a second full copy of the same // CANDIDATE_PASSER_BY_RANK value (an exact duplicate of the // term just added above, not a different angle on it), now a // fractional modifier. if (TotalNonPawnCount(pos, xColor) == 1) { EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], CANDIDATE_PASSER_BY_RANK[uColor][RANK(c)] / 2, "candidate passer in endgame"); } } } static void _EvaluateConnectedSupportedOutsidePassers(IN POSITION *pos, IN OUT PAWN_HASH_ENTRY *pHash) /** Routine description: Parameters: POSITION *pos, PAWN_HASH_ENTRY *pHash Return value: void **/ { ULONG uColor; ULONG u, v; COOR c; COOR cLeftmostPasser, cRightmostPasser; PIECE pFriend; COOR cSupport; BITBOARD bb; static const INT iDelta[5] = { -1, +1, +17, +15, 0 }; FOREACH_COLOR(uColor) { ASSERT(IS_VALID_COLOR(uColor)); if (pHash->bbPasserLocations[uColor]) { ASSERT(FastCountBits(pos->bbPawns[uColor]) > 0); pFriend = BLACK_PAWN | uColor; cLeftmostPasser = cRightmostPasser = ILLEGAL_COOR; // Leftmost/rightmost is just a min/max over FILE(c) -- no // need to wrap the passer walk in an outer per-file loop // just to get a file-ordered traversal; a single flat walk // over the whole bbPasserLocations bitboard visits the same // squares (CoorFromBitBoardRank8ToRank1 pops in rank-major // order, so leftmost/rightmost has to be tracked by explicit // comparison either way, not by iteration order). bb = pHash->bbPasserLocations[uColor]; while(IS_ON_BOARD(c = CoorFromBitBoardRank8ToRank1(&bb))) { ASSERT(pHash->uCountPerFile[uColor][FILE(c)+1] != 0); ASSERT(RANK(c) > 1); ASSERT(RANK(c) < 8); if ((cLeftmostPasser == ILLEGAL_COOR) || (FILE(c) < FILE(cLeftmostPasser))) { cLeftmostPasser = c; } if ((cRightmostPasser == ILLEGAL_COOR) || (FILE(c) > FILE(cRightmostPasser))) { cRightmostPasser = c; } v = 0; while(iDelta[v] != 0) { cSupport = c + iDelta[v] * g_iBehind[uColor]; if (IS_ON_BOARD(cSupport)) { if (pHash->bbPasserLocations[uColor] & COOR_TO_BB(cSupport)) { ASSERT(CONNECTED_PASSERS_BY_RANK[uColor] [RANK(c)] > 0); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], CONNECTED_PASSERS_BY_RANK[uColor] [RANK(c)], "connected passers"); // // TODO: connected passers vs a R is // strong. // } else if (pos->rgSquare[cSupport].pPiece == pFriend) { ASSERT(SUPPORTED_PASSER_BY_RANK[uColor] [RANK(c)] > 0); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], SUPPORTED_PASSER_BY_RANK[uColor] [RANK(c)], "supported passer"); } } v++; } } #ifdef DEBUG ASSERT(IS_ON_BOARD(cLeftmostPasser)); ASSERT(IS_ON_BOARD(cRightmostPasser)); ASSERT(RANK(cLeftmostPasser) > 1); ASSERT(RANK(cRightmostPasser) > 1); ASSERT(RANK(cLeftmostPasser) < 8); ASSERT(RANK(cRightmostPasser) < 8); if (CountBits(pHash->bbPasserLocations[uColor]) == 1) { ASSERT(cLeftmostPasser == cRightmostPasser); } #endif for (u = A; u <= D; u++) { if (pHash->uCountPerFile[FLIP(uColor)][u + 1] != 0) { ASSERT(pos->bbPawns[FLIP(uColor)] & BBFILE[u]); if (!(pHash->bbPasserLocations[FLIP(uColor)] & BBFILE[u])) { break; } } } if (u > FILE(cLeftmostPasser)) { EVAL_TERM(uColor, PAWN, ILLEGAL_COOR, pHash->iScore[uColor], OUTSIDE_PASSER_BY_DISTANCE[u-FILE(cLeftmostPasser)], "left outside passer"); } for (u = H; u >= E; u--) { if (pHash->uCountPerFile[FLIP(uColor)][u + 1] != 0) { ASSERT(pos->bbPawns[FLIP(uColor)] & BBFILE[u]); if (!(pHash->bbPasserLocations[FLIP(uColor)] & BBFILE[u])) { break; } } } if (u < FILE(cRightmostPasser)) { EVAL_TERM(uColor, PAWN, ILLEGAL_COOR, pHash->iScore[uColor], OUTSIDE_PASSER_BY_DISTANCE[FILE(cRightmostPasser)-u], "right outside passer"); } } } } // board_representation/EVAL.md section 2/0d: bitboard replacement for // the old per-pawn mailbox delta+IS_ON_BOARD population this function // used to do. pos->bbPawns[2] already has every pawn location // (incrementally maintained, chess.h) with zero per-pawn iteration // needed to query it -- the exact shift-and-mask technique // generate.c's _GenerateAllPawnMovesBB already uses to bulk-generate a // whole side's pawn captures (its bbCapLeft/bbCapRight, before being // masked down to actual enemy-occupied squares) is exactly "every // square this side's pawns attack" -- reused here verbatim, minus the // enemy-occupancy mask, since attack-bit population doesn't care what // (if anything) sits on the attacked square. See that function's // header comment for the full square-numbering/shift-direction // derivation (WHITE forward = bb >> 8, BLACK forward = bb << 8, // diagonals need the *opposite* file excluded to prevent same-row // wraparound) -- not repeated here. // // Unlike the old version, this does NOT write PAWN_BIT into the old // rgSquare[c|8].bvAttacks/ATTACK_BITV mechanism (retired entirely as // of 2026-09-05, once king -- the last piece writing it -- converted // too) -- pos->bbPawnAttacks[2] (chess.h) is the single source of // truth for "does a pawn attack this square", read directly by every // consumer. _ClearAttackTables(pos) still needs to run here first, to // zero the other bbXAttacks accumulators knight/bishop/rook/queen/king // populate as they each evaluate. static void _PopulatePawnAttackBits(IN OUT POSITION *pos) /** Routine description: Populate the attack table with pawn bits. Parameters: POSITION *pos : the board Return value: void **/ { _ClearAttackTables(pos); pos->bbPawnAttacks[BLACK] = ((pos->bbPawns[BLACK] & ~BBFILE[0]) << 7) | ((pos->bbPawns[BLACK] & ~BBFILE[7]) << 9); pos->bbPawnAttacks[WHITE] = ((pos->bbPawns[WHITE] & ~BBFILE[0]) >> 9) | ((pos->bbPawns[WHITE] & ~BBFILE[7]) >> 7); } static INLINE FLAG _FileCanSupportPawn(IN POSITION *pos, IN PAWN_HASH_ENTRY *pHash, IN ULONG uColor, IN ULONG uPawnFile, IN COOR c, IN BITBOARD bbBlockingRanks) /** Routine description: Can a friendly pawn on uPawnFile ever defend the pawn on square c -- either right now (already within defending range) or later (nothing permanently blocks it from advancing into range)? NOTE: on a doubled adjacent file, this only looks at whether *a* helper pawn on uPawnFile can reach c -- if the nearest one's only route there is an unsafe one-step push, this returns FALSE even though a second, more distant pawn on the same file might still reach c safely later. Rare (requires doubled pawns on the adjacent file specifically) and consistent with the rest of this function's approximate, snapshot-in-time nature. Parameters: POSITION *pos PAWN_HASH_ENTRY *pHash ULONG uColor : color of the pawn on c (and of the potential helper) ULONG uPawnFile : the adjacent file to test (0-7) COOR c : the pawn being supported BITBOARD bbBlockingRanks : c's rank plus every rank behind it, from uColor's forward direction (see caller) Return value: FLAG **/ { if (0 == pHash->uCountPerFile[uColor][uPawnFile + 1]) { return FALSE; // no friendly pawn on that file at all } if (pos->bbPawns[uColor] & BBFILE[uPawnFile] & BBADJACENT_RANKS[RANK(c)]) { return TRUE; // already defending } // A friendly pawn one step short of defending range would have to // push onto its destination square to get there. If a live enemy // pawn attacks that destination and no friendly pawn attack of // our own covers it too (pos->bbPawnAttacks[] is already // populated by _PopulatePawnAttackBits before either _EvalPawns // pass runs, so both are real, current facts, not a guess about // some future position), pushing there just loses the pawn for // nothing -- not a safe way to arrive as a defender. If we also // attack the destination, the push is at worst an even trade, so // it doesn't disqualify this file. WHITE pushes toward decreasing // bitboard index (see _PopulatePawnAttackBits), so a pawn one // push short of the defending band sits at (band << 8), and its // destination is (that square >> 8) -- i.e. the original band // itself; BLACK is mirrored. { BITBOARD bbOneStepShort = (uColor == WHITE) ? (BBADJACENT_RANKS[RANK(c)] << 8) : (BBADJACENT_RANKS[RANK(c)] >> 8); BITBOARD bbHelper = pos->bbPawns[uColor] & BBFILE[uPawnFile] & bbOneStepShort; if (bbHelper) { BITBOARD bbDestination = (uColor == WHITE) ? (bbHelper >> 8) : (bbHelper << 8); if ((bbDestination & pos->bbPawnAttacks[FLIP(uColor)]) && !(bbDestination & pos->bbPawnAttacks[uColor])) { return FALSE; } } } // A friendly pawn is on the file but not yet close enough to // defend -- it can still march up and defend later, unless an // enemy pawn on this same file already sits at c's rank or behind // it (from our own forward direction), permanently blocking the // way. return (0 == (pos->bbPawns[FLIP(uColor)] & BBFILE[uPawnFile] & bbBlockingRanks)); } static INLINE FLAG _IsBackwardPawn(IN POSITION *pos, IN ULONG uColor, IN COOR c, IN COOR cSquare) /** Routine description: Is the pawn on square c "backward"? See "Pawn Power in Chess" pp. 25-27. The caller has already established the one condition that needs pos->bbPawnAttacks[]: the enemy, not a friendly pawn, controls c's stop square (cSquare). This checks the three remaining, independently-necessary conditions: 1. cSquare isn't itself occupied by a pawn (either color) -- otherwise this is a blocked/rammed pawn, not a backward one. 2. c has advanced far enough for the classification to apply. 3. Neither neighbor on an adjacent file, one rank ahead of c, is a friendly pawn -- i.e. our neighbors have already advanced past c, so they can no longer shield it. (0x88 coordinates make cSquare +/- 1 safe to test directly: IS_ON_BOARD() catches the file-edge case for free, unlike a bitboard shift which would need explicit BBFILE[0]/BBFILE[7] masking.) Parameters: POSITION *pos ULONG uColor : color of the pawn on c COOR c : the pawn being tested COOR cSquare : c's stop square (one square ahead of c) Return value: FLAG **/ { PIECE p; if (IS_PAWN(pos->rgSquare[cSquare].pPiece)) { return FALSE; // stop square is blocked by a pawn } if (!(((WHITE == uColor) && (RANK(c) < 4)) || ((BLACK == uColor) && (RANK(c) > 5)))) { return FALSE; // not advanced enough yet } p = (BLACK_PAWN | uColor); // uColor's own pawn, regardless of color return ((IS_ON_BOARD(cSquare - 1) && (pos->rgSquare[cSquare - 1].pPiece == p)) || (IS_ON_BOARD(cSquare + 1) && (pos->rgSquare[cSquare + 1].pPiece == p))); } static PAWN_HASH_ENTRY * _EvalPawns(IN OUT SEARCHER_THREAD_CONTEXT *ctx, OUT FLAG *pfDeferred) /** Routine description: Evaluate pawn structures; return a ptr to a pawn hash entry. Parameters: SEARCHER_THREAD_CONTEXT *ctx : the searcher thread context Return value: PAWN_HASH_ENTRY * **/ { static ULONG uUnmovedRank[2] = { 0x10, 0x60 }; POSITION *pos = &ctx->sPosition; PAWN_HASH_ENTRY *pHash; COOR c, cSquare; ULONG uIsolated[2]; ULONG uDoubled[2]; SCORE iDuos[2]; ULONG u, uPawnFile; ULONG uColor; BITBOARD bb; BITBOARD bbBlockingRanks; int d1; #ifdef DEBUG SCORE t; #endif // Look up the hash entry for this position. INC(ctx->sCounters.pawnhash.u64Probes); pHash = PawnHashLookup(ctx); ASSERT(NULL != pHash); if (pHash->u64Key == pos->u64PawnSig) { *pfDeferred = TRUE; INC(ctx->sCounters.pawnhash.u64Hits); return(pHash); } // We need the attack table to evaluate the pawns... and we missed // the hash table so we have to populate it now. Can't defer this // one. *pfDeferred = FALSE; _InitializePawnHashEntry(pHash, pos); _PopulatePawnAttackBits(pos); uIsolated[BLACK] = uIsolated[WHITE] = 0; uDoubled[BLACK] = uDoubled[WHITE] = 0; iDuos[BLACK] = iDuos[WHITE] = 0; // First pass -- everything here except the per-square centrality // bonus is a closed-form aggregate over pos->bbPawns[uColor] // (uCountPerFile, uNumUnmovedPawns, bbStationaryPawns/ // uNumRammedPawns), so none of it needs a per-pawn mailbox walk; // only the centrality lookup genuinely needs one (it's a per-square // table, not summable any other way). BITBOARD bbAllPawns = pos->bbPawns[WHITE] | pos->bbPawns[BLACK]; FOREACH_COLOR(uColor) { ASSERT(IS_VALID_COLOR(uColor)); // Pre-count number of pawns per file, unrolled. ASSERT(pHash->uCountPerFile[uColor][0] == 0); pHash->uCountPerFile[uColor][A + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[A]); pHash->uCountPerFile[uColor][B + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[B]); pHash->uCountPerFile[uColor][C + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[C]); pHash->uCountPerFile[uColor][D + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[D]); pHash->uCountPerFile[uColor][E + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[E]); pHash->uCountPerFile[uColor][F + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[F]); pHash->uCountPerFile[uColor][G + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[G]); pHash->uCountPerFile[uColor][H + 1] = CountBits(pos->bbPawns[uColor] & BBFILE[H]); ASSERT(pHash->uCountPerFile[uColor][9] == 0); // Count number of unmoved pawns. pHash->uNumUnmovedPawns[uColor] = CountBits(pos->bbPawns[uColor] & BBRANK[RANK(uUnmovedRank[uColor])]); ASSERT(pHash->uNumUnmovedPawns[uColor] <= 8); // Detect rammed and other stationary pawns. A pawn is // "stationary" if *any* pawn (either color -- e.g. a doubled // own-pawn block counts too) sits on its "stop square". It's // specifically "rammed" only when that blocker is the // enemy's. WHITE's stop square is one rank toward rank8 (bit // index -8 in the linear 0-63 bitboard numbering -- see // _PopulatePawnAttackBits' >>8/<<8 shifts for the same // convention), BLACK's is +8. pHash->bbStationaryPawns[uColor] = (uColor == WHITE) ? (pos->bbPawns[WHITE] & (bbAllPawns << 8)) : (pos->bbPawns[BLACK] & (bbAllPawns >> 8)); ASSERT(CountBits(pHash->bbStationaryPawns[uColor]) <= 8); // Give central pawns a small bonus and rook pawns a small // penalty. bb = pos->bbPawns[uColor]; while(IS_ON_BOARD(c = CoorFromBitBoardRank8ToRank1(&bb))) { EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], PAWN_CENTRALITY_BONUS[c], "location"); } } // Rammed pairs (an enemy pawn, not just any pawn, on the stop // square): each pair shows up as exactly one bit in each // side's own stationary-pawn count, so no double-counting // (and no /2 fixup) is needed the way the old per-pawn, // per-color loop required. pHash->uNumRammedPawns = CountBits(pos->bbPawns[WHITE] & (pos->bbPawns[BLACK] << 8)); ASSERT(pHash->uNumRammedPawns <= 8); // Second pass, requires pHash->uCountPerFile[] populated for both // colors. FOREACH_COLOR(uColor) { ASSERT(IS_VALID_COLOR(uColor)); ULONG xColor = FLIP(uColor); ASSERT(IS_VALID_COLOR(xColor)); ASSERT(CountBits(pos->bbPawns[uColor]) <= 8); d1 = 16 * g_iAhead[uColor]; BITBOARD bbPawnsToWalk = pos->bbPawns[uColor]; while (IS_ON_BOARD(c = CoorFromBitBoardRank8ToRank1(&bbPawnsToWalk))) { ASSERT(IS_PAWN(pos->rgSquare[c].pPiece)); ASSERT((1 < RANK(c)) && (RANK(c) < 8)); // Find weak pawns... pawns that are isolated are weak as // are pawns that have advanced beyond their support. // Both of these can tie up one or more pieces in order to // defend them. A pawn is a target unless a friendly pawn // on *either* adjacent file can defend it, now or later // (see _FileCanSupportPawn). bbBlockingRanks is c's rank // plus every rank behind it (from uColor's forward // direction) -- an enemy pawn there on an adjacent file // permanently blocks that file from ever producing a // defender. bbBlockingRanks = BBRANK[RANK(c)] | BBPRECEEDING_RANKS[c >> 4][uColor]; if (!_FileCanSupportPawn(pos, pHash, uColor, FILE(c) - 1, c, bbBlockingRanks) && !_FileCanSupportPawn(pos, pHash, uColor, FILE(c) + 1, c, bbBlockingRanks)) { // // Single consolidated isolated-pawn penalty: base // severity by file, plus modifiers if the pawn is also // exposed and/or doubled. These used to be three // separate additive EVAL_TERM calls that all fired for // the same pawn, plus a fourth, whole-position // "exponential isolated" aggregate below that // double-counted the same uIsolated[] count this loop // already prices in one pawn at a time -- that // aggregate is removed entirely below rather than just // consolidated (see the comment there). // uPawnFile = FILE(c) + 1; uIsolated[uColor] += 1; ASSERT(uIsolated[uColor] > 0); ASSERT(uIsolated[uColor] <= 8); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], (ISOLATED_PAWN_BY_PAWNFILE[uPawnFile] + ((pHash->uCountPerFile[xColor][uPawnFile]==0) * ISOLATED_EXPOSED_PAWN) + ((pHash->uCountPerFile[uColor][uPawnFile] > 1) * ISOLATED_DOUBLED_PAWN)), "isolated pawn"); } uPawnFile = FILE(c) + 1; ASSERT((1 <= uPawnFile) && (uPawnFile <= 8)); ASSERT(CountBits(pos->bbPawns[uColor] & BBFILE[FILE(c)]) == pHash->uCountPerFile[uColor][uPawnFile]); // // Keep count of doubled pawns // uDoubled[uColor] += (pHash->uCountPerFile[uColor][uPawnFile] > 1); ASSERT((0 <= uDoubled[uColor]) && (uDoubled[uColor] <= 8)); // // We can detect pawn duos (triads, etc...) and backward pawns // by considering the control of the pawn's stopsquare. // cSquare = c + d1; ASSERT(IS_ON_BOARD(cSquare)); // // At this point in Eval()'s sequence (inside _EvalPawns, // before any other piece type has run this call), pawns // are the only thing that could possibly have marked an // attack -- bvAttacks itself no longer carries the pawn // bit at all (see _PopulatePawnAttackBits), so this reads // pos->bbPawnAttacks directly instead of the (permanently // zero, at this point in execution) bvAttacks word. // if (pos->bbPawnAttacks[uColor] & COOR_TO_BB(cSquare)) { // // Count pawn duos. See "Pawn Power in Chess" pp 10-16 // #ifdef DEBUG t = ((uColor * RANK(cSquare)) + (xColor * (9 - RANK(cSquare)))); if (uColor == WHITE) { ASSERT(t == RANK(cSquare)); } else { ASSERT(uColor == BLACK); ASSERT(t == (9 - RANK(cSquare))); } #endif iDuos[uColor] += ((uColor * RANK(cSquare)) + (xColor * (9 - RANK(cSquare)))); ASSERT(iDuos[uColor] <= (7 * 8)); } else if (pos->bbPawnAttacks[xColor] & COOR_TO_BB(cSquare)) { // // Detect backwards pawns. See "Pawn Power in Chess" pp 25-27 // if (_IsBackwardPawn(pos, uColor, c, cSquare)) { // // The pawn at c is backward. Determine whether // it is shielded or exposed. // pHash->bbStationaryPawns[uColor] |= COOR_TO_BB(c); ASSERT(CountBits(pHash->bbStationaryPawns[uColor]) <= 8); if (pHash->uCountPerFile[xColor][uPawnFile]) { ASSERT(BACKWARD_SHIELDED_BY_LOCATION[c] < 0); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], BACKWARD_SHIELDED_BY_LOCATION[c], "shielded backward pawn"); } else { ASSERT(BACKWARD_EXPOSED_BY_LOCATION[c] < 0); EVAL_TERM(uColor, PAWN, c, pHash->iScore[uColor], BACKWARD_EXPOSED_BY_LOCATION[c], "exposed backward pawn"); } } // // TODO: Think about backward doubled pawns; read Kauffman's // paper "All About Doubled Pawns". // } // // Handle passers / candidate passers // _EvaluateCandidatePasser(pos, pHash, c); } } // // Reward pawn duos, see "Pawn Power in Chess" pp. 10-16 // ASSERT(iDuos[WHITE] >= 0); ASSERT(iDuos[WHITE] <= 56); ASSERT(iDuos[BLACK] >= 0); ASSERT(iDuos[BLACK] <= 56); EVAL_TERM(WHITE, PAWN, ILLEGAL_COOR, pHash->iScore[WHITE], iDuos[WHITE] / 2, "pawn duos"); EVAL_TERM(BLACK, PAWN, ILLEGAL_COOR, pHash->iScore[BLACK], iDuos[BLACK] / 2, "pawn duos"); // // The penalty for doubled pawns is scaled based on two primary // factors: the presence of major pieces for the side with doubled // pawns and the number of doubled pawns on the board. The // presence of major pieces makes the doubled pawns less severe // while the presence of many doubled pawns makes the penalty for // each more severe. // ASSERT((uDoubled[WHITE] >= 0) && (uDoubled[WHITE] <= 8)); ASSERT((uDoubled[BLACK] >= 0) && (uDoubled[BLACK] <= 8)); u = FastCountBits(pos->bbPieces[BLACK][ROOK]) + FastCountBits(pos->bbPieces[BLACK][QUEEN]) * 2; u = MINU(u, 3); EVAL_TERM(BLACK, PAWN, ILLEGAL_COOR, pHash->iScore[BLACK], DOUBLED_PAWN_PENALTY_BY_COUNT[u][uDoubled[BLACK]], "exponential doubled"); u = FastCountBits(pos->bbPieces[WHITE][ROOK]) + FastCountBits(pos->bbPieces[WHITE][QUEEN]) * 2; u = MINU(u, 3); EVAL_TERM(WHITE, PAWN, ILLEGAL_COOR, pHash->iScore[WHITE], DOUBLED_PAWN_PENALTY_BY_COUNT[u][uDoubled[WHITE]], "exponential doubled"); ASSERT(uIsolated[WHITE] >= 0); ASSERT(uIsolated[WHITE] <= 8); ASSERT(uIsolated[BLACK] >= 0); ASSERT(uIsolated[BLACK] <= 8); // // Look for connected, supported and outside passed pawns to give // extra bonuses. // _EvaluateConnectedSupportedOutsidePassers(pos, pHash); // // TODO: recognize quartgrips and stonewalls // return(pHash); } ULONG CountKingSafetyDefects(IN OUT POSITION *pos, IN ULONG uColor) /** Routine description: Determine how many defects uColor's king position has _quickly_. TODO: add more knowledge as cheaply as possible... Parameters: POSITION *pos, ULONG uColor Return value: ULONG **/ { ULONG uCounter = 0; ULONG xColor = FLIP(uColor); COOR cKing; BITBOARD bbKingZone; BITBOARD bbBlockers; BITBOARD bb; COOR c; // // Don't count king safety defects if the real eval code in // _EvalKing would not... // if (pos->uNonPawnMaterial[xColor] < DO_KING_SAFETY_THRESHOLD) { return 0; } cKing = KingPosition(pos, uColor); uCounter = KING_INITIAL_COUNTER_BY_LOCATION[uColor][cKing] >> 1; ASSERT(IS_KING(pos->rgSquare[cKing].pPiece)); ASSERT(IS_ON_BOARD(cKing)); ASSERT(GET_COLOR(pos->rgSquare[cKing].pPiece) == uColor); // // board_representation/EVAL.md section 9 follow-up (2026-09-05): // real, blocker-aware attack tests against the king's actual // 8-neighbor zone (the same zone _EvalKing's own KingSafetyDeltas // loop tests, now that its own bug -- testing 11 squares including // two bogus same-rank-2-away entries -- is fixed), replacing the // old CHECK_VECTOR_WITH_INDEX geometry-only lookup. That lookup // tested three hypothetical king positions (cKing-1, cKing, // cKing+1 -- always on the same rank, driven by CHECK_VECTOR's // one-dimensional index arithmetic) via a table with no board- // occupancy awareness at all -- it couldn't tell a rook on an open // file from one blocked by its own pawn, and never tested whether // a piece threatened a square one rank away from the king. This // version can't reuse pos->bbXAttacks (Eval()-scoped, and this // function is also called directly from search.c/searchsup.c as // an extension/reduction gate, independent of whether Eval() ran // on the current position at all) -- but pos->bbOccupied is a // real, always-current POSITION field regardless, so a fresh, // per-piece magic-bitboard lookup here is both cheap (same O(1) // shape as the old table lookups) and genuinely blocker-aware. // X-rays deliberately not considered via chain-following (that // would double the per-slider cost of a function called at every // lazy-eval check across the whole search tree, for what's already // a rough magnitude estimate, not an exact score) -- but the // occupancy fed to the slider lookups below is deliberately just // xColor's own pieces (_BuildFriendlySideBB(pos, xColor)), not the // whole board: a piece belonging to uColor (a defending pawn, the // king itself) is transparent to these rays, only xColor's own // pieces actually block. This gets latent-threat detection (a // rook aimed at the king zone but currently shielded by the // king's own pawn still counts) for free, at zero extra cost -- // still one magic lookup per slider, just against a different // occupancy bitboard -- by direct instruction: we don't count our // own pieces as blocking the enemy king. // bbKingZone = g_KingAttacksBB[cKing] | COOR_TO_BB(cKing); bbBlockers = _BuildFriendlySideBB(pos, xColor); bb = pos->bbPieces[xColor][KNIGHT]; while (bb) { c = CoorFromBitBoardRank8ToRank1(&bb); uCounter += (g_KnightAttacksBB[c] & bbKingZone) != 0; } bb = pos->bbPieces[xColor][BISHOP]; while (bb) { c = CoorFromBitBoardRank8ToRank1(&bb); uCounter += (_BishopAttacksBB(c, bbBlockers) & bbKingZone) != 0; } bb = pos->bbPieces[xColor][ROOK]; while (bb) { c = CoorFromBitBoardRank8ToRank1(&bb); uCounter += (_RookAttacksBB(c, bbBlockers) & bbKingZone) != 0; } // Queen hits count double -- by direct instruction: a queen near // the king is a real, distinct danger source _EvalKing's own // KING_QUEEN_PROXIMITY_DANGER term prices separately from generic // attack presence, and this counter had no way to reflect that // (a queen hitting the zone counted exactly the same as a lone // knight doing so). Raises the bound below accordingly (up to 8 // queens are legal via promotion, each now worth 2). bb = pos->bbPieces[xColor][QUEEN]; while (bb) { c = CoorFromBitBoardRank8ToRank1(&bb); uCounter += 2 * (((_RookAttacksBB(c, bbBlockers) | _BishopAttacksBB(c, bbBlockers)) & bbKingZone) != 0); } ASSERT(uCounter < 30); return uCounter; } static SCORE _ComputeFileStormDefects(IN POSITION *pos, IN const PAWN_HASH_ENTRY *pHash, IN ULONG uColor) /** Routine description: Real, exact open-file/storming-pawn defect count for uColor's king -- king's own file plus the two adjacent ones (2026-08-30: collapsed from three copy-pasted blocks that differed only in c-1/c/c+1). Extracted unchanged from _EvalKing (2026-09-05, board_representation/EVAL.md section 9's king-safety-hash prototype) so it can be called once per side from _GetFileStormDefects's cache-miss path instead of inline. 2026-09-15: converted from an unsigned "counter" (fed through the old shared uCounter/KING_SAFETY_BY_COUNTER lookup) to a direct signed SCORE (centipawns), part of _EvalKing's move away from that shared nonlinear counter entirely -- see king_safety_data/ HANDOFF.md's 2026-09-15 addendum. Table SHAPE below is the ORIGINAL, pre-2026-09-14 relative weighting (0,4,3,1,.../+2,+1,+0), not the already-1.75-2x-bumped one -- that's deliberate: the mate- mining regression's filestormopenfile/filestormpawn coefficients were fit against CSV data extracted using this original shape, so scaling *this* shape by the regression-derived conversion factor is the correct one-step translation (scaling the already-bumped shape again would double-apply the 2026-09-14 tuning). Magnitude derived the same way as KING_QUEEN_PROXIMITY_DANGER's self-play-validated -14/count: k = (validated queenproximity weight) / (queenproximity's own cleaned raw regression coefficient) =~ -14 per raw coefficient unit, applied to filestormpawn's raw coefficient (+0.374) and filestormopenfile's (+0.360) against the original table's own integer steps. Unvalidated by self-play as of this writing -- expect this exact magnitude to move once tested, same as every other freshly-converted term here. Parameters: POSITION *pos PAWN_HASH_ENTRY *pHash ULONG uColor Return value: SCORE **/ { static SCORE KingStormingPawnDefects[8] = { +0, -21, -16, -5, 0, 0, 0, 0 }; static SCORE KingFileDefects[3] = { -10, -5, 0 }; // Old code's edge-file (a/h) kicker was a flat +1 counter-unit; // scaled by the same filestormopenfile-derived factor. static SCORE KING_FILE_EDGE_KICKER = -5; ULONG ufColor = FLIP(uColor); COOR c = KingPosition(pos, uColor); SCORE iScore = 0; COOR cFileSq, cSquare; ULONG u, v; BITBOARD bb; int w; for (w = -1; w <= 1; w++) { cFileSq = c + w; if (!IS_ON_BOARD(cFileSq)) { continue; } u = FILE(cFileSq) + 1; v = (pHash->uCountPerFile[WHITE][u] > 0) + (pHash->uCountPerFile[BLACK][u] > 0); ASSERT((v >= 0) && (v <= 2)); iScore += KingFileDefects[v]; if ((v < 2) && ((u == 1) || (u == 8))) { iScore += KING_FILE_EDGE_KICKER; } bb = pos->bbPawns[ufColor] & BBFILE[u - 1]; if (bb) { if (uColor == WHITE) { cSquare = CoorFromBitBoardRank1ToRank8(&bb); } else { cSquare = CoorFromBitBoardRank8ToRank1(&bb); } ASSERT(IS_ON_BOARD(cSquare)); iScore += KingStormingPawnDefects[DISTANCE(cSquare, c)]; } } return iScore; } #ifdef KING_DEFECT_DUMP static void _ComputeFileStormDefectsSplit(IN POSITION *pos, IN const PAWN_HASH_ENTRY *pHash, IN ULONG uColor, OUT ULONG *puOpenFile, OUT ULONG *puStormingPawn) /** Routine description: Debug-only sibling of _ComputeFileStormDefects that reports its two independent sub-ideas (open/half-open file bonus vs. storming-pawn distance penalty) separately instead of pre-summed, so a regression can weigh them independently -- they already live in two distinct hand-picked tables (KingFileDefects / KingStormingPawnDefects) with no structural change needed to retune them individually once this tells us their relative importance. See CLAUDE.md's king-safety discussion. 2026-09-15: DELIBERATELY DIVERGED from _ComputeFileStormDefects as of the linear-SCORE rewrite (king_safety_data/HANDOFF.md's 2026-09-15 addendum) -- the real path now returns signed centipawns off a differently-shaped table, but this dump sibling must keep reporting the OLD unsigned counter-style values so king_features_v4.csv-shaped mining data stays interpretable/ comparable across sessions (extract_king_features.py, this project's whole regression pipeline, assumes these units). Traversal shape (own file + two adjacent, same v/distance logic) still matches -- only the two tables' actual values are now intentionally frozen at their pre-rewrite originals, not kept in sync. **/ { static ULONG KingStormingPawnDefects[8] = { +0, +7, +5, +2, +0, +0, +0, +0 }; static ULONG KingFileDefects[3] = { +4, +2, +0 }; ULONG ufColor = FLIP(uColor); COOR c = KingPosition(pos, uColor); COOR cFileSq, cSquare; ULONG u, v; BITBOARD bb; int w; *puOpenFile = 0; *puStormingPawn = 0; for (w = -1; w <= 1; w++) { cFileSq = c + w; if (!IS_ON_BOARD(cFileSq)) { continue; } u = FILE(cFileSq) + 1; v = (pHash->uCountPerFile[WHITE][u] > 0) + (pHash->uCountPerFile[BLACK][u] > 0); ASSERT((v >= 0) && (v <= 2)); *puOpenFile += (KingFileDefects[v] + ((v < 2) && ((u == 1) || (u == 8)))); bb = pos->bbPawns[ufColor] & BBFILE[u - 1]; if (bb) { if (uColor == WHITE) { cSquare = CoorFromBitBoardRank1ToRank8(&bb); } else { cSquare = CoorFromBitBoardRank8ToRank1(&bb); } ASSERT(IS_ON_BOARD(cSquare)); *puStormingPawn += KingStormingPawnDefects[DISTANCE(cSquare, c)]; } } } #endif static SCORE _GetFileStormDefects(IN SEARCHER_THREAD_CONTEXT *ctx, IN POSITION *pos, IN const PAWN_HASH_ENTRY *pHash, IN ULONG uColor) /** Routine description: EXPERIMENTAL, prototype-only (2026-09-05, board_representation/ EVAL.md section 9's king-safety-hash discussion). Cache front end for _ComputeFileStormDefects, keyed on (pos->u64PawnSig, both kings' squares) -- deliberately covers only the DO_KING_SAFETY_ THRESHOLD-gated "safety branch" term (measured ~96% hit rate in general/middlegame positions), not the u<8-gated "endgame branch" terms (king-supporting-own-passer, in/out-of-action), which measured a much worse ~75-85% hit rate in bare K+P endgames and are cheap enough there that caching them isn't expected to pay off -- those stay inline in _EvalKing, uncached. On a miss, computes *both* sides' defect counts and stores them together, so whichever king _EvalKing evaluates second within the same Eval() call (kings are always evaluated black-then-white) is a guaranteed hit -- only one real miss per position, not one per king. 2026-09-15: return type/cache field changed ULONG -> SCORE along with _ComputeFileStormDefects -- see that function's comment. This function's own cache/probe/hit-rate mechanics are unaffected, only the value being cached changed shape. NOTE: CALIBRATE_POSITIONAL's two call sites (_RecordPositionalCalibration and the DumpPositional Calibration-ish block near EstimatePositionalScore) still treat this return value as a small unsigned index component (MINU(10, CountKingSafetyDefects(...) + _GetFileStormDefects(...))) -- now wrong now that this returns signed centipawns, but left alone by direct instruction (that non-default build flag's own recalibration is deferred as separate follow-up work, not part of this rewrite). Parameters: SEARCHER_THREAD_CONTEXT *ctx POSITION *pos PAWN_HASH_ENTRY *pHash ULONG uColor Return value: SCORE **/ { UINT64 u64Key = pos->u64PawnSig ^ g_u64SigSeeds[KingPosition(pos, WHITE)][KING][WHITE] ^ g_u64SigSeeds[KingPosition(pos, BLACK)][KING][BLACK]; ULONG uSlot = (ULONG)u64Key & (KING_SAFETY_HASH_TABLE_SIZE - 1); KING_SAFETY_HASH_ENTRY *pEntry = &ctx->rgKingSafetyHash[uSlot]; #ifdef PERF_COUNTERS UINT64 u64Start = SystemReadTimeStampCounter(); #endif INC(ctx->sCounters.kingsafetyhash.u64Probes); if (pEntry->u64Key == u64Key) { INC(ctx->sCounters.kingsafetyhash.u64Hits); #ifdef PERF_COUNTERS ctx->sCounters.kingsafetyhash.u64CyclesHit += (SystemReadTimeStampCounter() - u64Start); #endif return pEntry->iFileStormDefects[uColor]; } pEntry->u64Key = u64Key; pEntry->iFileStormDefects[WHITE] = _ComputeFileStormDefects(pos, pHash, WHITE); pEntry->iFileStormDefects[BLACK] = _ComputeFileStormDefects(pos, pHash, BLACK); #ifdef PERF_COUNTERS ctx->sCounters.kingsafetyhash.u64CyclesMiss += (SystemReadTimeStampCounter() - u64Start); #endif return pEntry->iFileStormDefects[uColor]; } #ifdef CALIBRATE_POSITIONAL // // board_representation/EVAL.md section 9 recalibration (2026-09-05): // histogram of (cheap defect index -> real king-safety magnitude), one // row per possible EstimatePositionalScore bucket, so a p90-per-row // read after a calibration run over a large sample (curated suites or // a slice of ecm.ep_, at real search depth so the distribution matches // what the search tree actually sees, not just root positions) gives a // fresh iKingSwingP90 table -- coarse cp bins, not raw samples, to keep // this cheap enough to leave compiled into a whole search run rather // than needing to buffer millions of individual values. // // 2026-09-15: bucket index fixed to MINU(10, CountKingSafetyDefects) // ONLY, dropping _GetFileStormDefects -- it had drifted out of sync // with EstimatePositionalScore's own real lookup index back on // 2026-09-06 (_GetFileStormDefects was pulled out of that hot path for // being too expensive to call on ~90% of Eval() calls regardless of // exit, but this histogram's bucketing was never updated to match, // silently refitting against an index EstimatePositionalScore was no // longer using). Also: this histogram used to bin the pre-scale // KING_SAFETY_BY_COUNTER magnitude, which was always <= 0 by // construction; the 2026-09-15 linear-SCORE rewrite's iSafety can be // mildly positive (a well-covered king with real back-rank escape // squares and no attacks), so this now bins ABS(iSafety) -- by direct // instruction, the rare positive case is small enough not to need its // own separate treatment. // #define CALIBRATE_NUM_BUCKETS 11 #define CALIBRATE_BIN_WIDTH 5 #define CALIBRATE_NUM_BINS 200 // covers |raw king-safety magnitude| 0..999 static UINT64 g_CalibrateHist[CALIBRATE_NUM_BUCKETS][CALIBRATE_NUM_BINS]; UINT64 g_uCalibrateGateChecked = 0; UINT64 g_uCalibrateGateTrue = 0; UINT64 g_uCalibrateEvalCalls = 0; static void _RecordPositionalCalibration(IN SEARCHER_THREAD_CONTEXT *ctx, IN POSITION *pos, IN const PAWN_HASH_ENTRY *pHash, IN ULONG uColor, IN ULONG uRawMagnitude) /** Routine description: Bucket one real _EvalKing sample (this side's raw, pre-material-scaled king-safety magnitude) by the same cheap defect index EstimatePositionalScore would have used to estimate it, so a later DumpPositionalCalibration can compute a fresh p90-per-bucket table. Parameters: SEARCHER_THREAD_CONTEXT *ctx POSITION *pos PAWN_HASH_ENTRY *pHash : unused as of 2026-09-15 (see the bucket- index fix in this file's #ifdef CALIBRATE_POSITIONAL comment above) -- kept in the signature for call-site stability. ULONG uColor ULONG uRawMagnitude : ABS(iSafety), before the REDUCED_MATERIAL_DOWN_SCALER scaling _EvalKing applies next -- EstimatePositionalScore's table is looked up then scaled the same way, so the table itself should be fit against the unscaled value. Return value: void **/ { ULONG uIndex; ULONG uBin; // Only sample nodes Eval()'s own base-margin ("super lazy") gate // would actually pass through to EstimatePositionalScore in a real // build -- see ctx->fCalibrateCandidate's comment in chess.h. if (FALSE == ctx->fCalibrateCandidate) { return; } uIndex = MINU(10, CountKingSafetyDefects(pos, uColor)); uBin = MINU(CALIBRATE_NUM_BINS - 1, uRawMagnitude / CALIBRATE_BIN_WIDTH); g_CalibrateHist[uIndex][uBin]++; } void DumpPositionalCalibration(void) /** Routine description: Print a fresh iKingSwingP90[] table (p90 of the real, unscaled KING_SAFETY_BY_COUNTER value observed at each cheap defect-index bucket) from the histogram _RecordPositionalCalibration has been filling for the duration of this run. Only meaningful in a CALIBRATE_POSITIONAL build, after running a representative workload (the curated suites, or an ecm.ep_ slice, at real search depth -- root-only positions won't exercise the distribution of positions the search tree actually evaluates). Parameters: void Return value: void **/ { ULONG uIndex; ULONG uBin; UINT64 u64Total; UINT64 u64Running; SCORE iP90; Trace("Positional calibration: Eval() called %" COMPILER_LONGLONG_UNSIGNED_FORMAT " times\n", g_uCalibrateEvalCalls); Trace("Positional calibration: gate checked %" COMPILER_LONGLONG_UNSIGNED_FORMAT " times, true %" COMPILER_LONGLONG_UNSIGNED_FORMAT " times\n", g_uCalibrateGateChecked, g_uCalibrateGateTrue); Trace("Positional calibration (p90 per bucket, sample counts):\n"); for (uIndex = 0; uIndex < CALIBRATE_NUM_BUCKETS; uIndex++) { u64Total = 0; for (uBin = 0; uBin < CALIBRATE_NUM_BINS; uBin++) { u64Total += g_CalibrateHist[uIndex][uBin]; } if (0 == u64Total) { Trace(" bucket %2u: no samples\n", uIndex); continue; } u64Running = 0; iP90 = (SCORE)((CALIBRATE_NUM_BINS - 1) * CALIBRATE_BIN_WIDTH); for (uBin = 0; uBin < CALIBRATE_NUM_BINS; uBin++) { u64Running += g_CalibrateHist[uIndex][uBin]; if (u64Running * 10 >= u64Total * 9) { iP90 = (SCORE)(uBin * CALIBRATE_BIN_WIDTH); break; } } Trace(" bucket %2u: p90=%d (n=%" COMPILER_LONGLONG_UNSIGNED_FORMAT ")\n", uIndex, iP90, u64Total); } } #endif // CALIBRATE_POSITIONAL // 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? 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. // // 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. static UINT64 g_uBaseMarginExitAttempted[BASE_MARGIN_MAT_BUCKETS][11]; static UINT64 g_uBaseMarginExitTaken[BASE_MARGIN_MAT_BUCKETS][11]; static ULONG _MaxKingDangerBucket(IN SEARCHER_THREAD_CONTEXT *ctx, IN POSITION *pos, IN PAWN_HASH_ENTRY *pHash) /** Routine description: Cheap helper for CALIBRATE_BASE_MARGIN instrumentation only: the worse of the two sides' combined cheap defect-index buckets, same definition EstimatePositionalScore's iKingSwingP90 lookup uses. Re-reads CountKingSafetyDefects/_GetFileStormDefects rather than threading extra output params through EstimatePositionalScore -- both hit the king-safety hash on a repeat call within the same Eval(), so this is cheap (see rgKingSafetyHash's "guaranteed intra-call hit" comment). Parameters: SEARCHER_THREAD_CONTEXT *ctx POSITION *pos PAWN_HASH_ENTRY *pHash Return value: ULONG : 0-10 **/ { ULONG uWhite = MINU(10, CountKingSafetyDefects(pos, WHITE) + _GetFileStormDefects(ctx, pos, pHash, WHITE)); ULONG uBlack = MINU(10, CountKingSafetyDefects(pos, BLACK) + _GetFileStormDefects(ctx, pos, pHash, BLACK)); return MAXU(uWhite, uBlack); } void DumpBaseMarginCalibration(void) /** 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"). Parameters: void Return value: void **/ { ULONG m, u; for (m = 0; m < BASE_MARGIN_MAT_BUCKETS; m++) { Trace("Base margin calibration -- material bucket %u " "(combined army scaler %u-%u):\n", m, m * BASE_MARGIN_MAT_WIDTH, (m * BASE_MARGIN_MAT_WIDTH) + BASE_MARGIN_MAT_WIDTH - 1); for (u = 0; u <= 10; u++) { if (0 == g_uBaseMarginExitAttempted[m][u]) { continue; } Trace(" king-danger bucket %2u: %" COMPILER_LONGLONG_UNSIGNED_FORMAT " attempted, %" COMPILER_LONGLONG_UNSIGNED_FORMAT " exited (%.2f%%)\n", u, g_uBaseMarginExitAttempted[m][u], g_uBaseMarginExitTaken[m][u], (100.0 * (double)g_uBaseMarginExitTaken[m][u] / (double)g_uBaseMarginExitAttempted[m][u])); } } } #endif // CALIBRATE_BASE_MARGIN #ifdef CALIBRATE_MARGIN_SAFETY // // board_representation/EVAL.md section 9: for every node that actually // took a lazy exit, how far off was the real (full-eval) score from // the lazy-path score that got returned? The *maximum* observed swing, // not just its p90, is what answers "how large would LAZY_EVAL_BASE_ // MARGIN have had to be before some exit here would have been // unsound" -- unlike iKingSwingP90 (an estimate that's allowed to be // wrong ~10% of the time by design), a used margin being exceeded even // once is a genuine soundness question, so max is the right statistic // here, not a percentile. Bucketed by combined material (same bucketing // as CALIBRATE_BASE_MARGIN) since the earlier lazy/full cost-ratio // measurement showed the two regimes are very different. // 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). // Its own SUPER_LAZY_MARGIN was never run through this harness when // it was added -- a much cheaper, much-larger-required-gap check than // the regular lazy exit, so mixing its swings into the buckets above // would wash out both regimes' signal. 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; } } static void RecordMarginSafetySwing(IN POSITION *pos, IN SCORE iSwing, IN SCORE iActualMarginUsed) /** Routine description: Record one (material bucket, |real - lazy| swing) sample for DumpBaseMarginCalibration's margin-safety report, and check whether this specific exit's swing exceeded the actual margin that was used to justify it (a genuine unsoundness event, not just "large"). Parameters: POSITION *pos SCORE iSwing : >= 0 SCORE iActualMarginUsed : the real (post-EstimatePositionalScore) alpha or beta margin this exit was taken under Return value: void **/ { _RecordMarginSafetySwingInto(g_uMarginSwingMax, g_uMarginSwingCount, g_uMarginSwingSum, g_uMarginSwingSumSq, g_uMarginSwingHist, g_uMarginExceeded, g_uMarginExceededFenSamples, "lazy", pos, iSwing, iActualMarginUsed); } static void RecordSuperLazyMarginSafetySwing(IN POSITION *pos, IN SCORE iSwing, IN SCORE iActualMarginUsed) /** Routine description: Same as RecordMarginSafetySwing, but for the super-lazy exit's own separate bucket set -- see the comment on the g_uSuperLazyMargin* arrays above for why these are kept apart. Parameters: POSITION *pos SCORE iSwing : >= 0 SCORE iActualMarginUsed : SUPER_LAZY_MARGIN Return value: void **/ { _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 -- chess.h's own VALUE_KING // #defines INFINITY as a board-eval sentinel, which collides with // '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: " " ... \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 -- |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", m, m * BASE_MARGIN_MAT_WIDTH, (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 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], dMean, dStdev, puCount[m], puExceeded[m]); } } void DumpMarginSafetyCalibration(IN CHAR *szHistogramPathOverride) /** Routine description: 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 the super-lazy exit (SUPER_LAZY_MARGIN), which is a different-enough regime (cheaper check, much larger required gap) that lumping the 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." Also persists the raw swing histograms to file (see PersistMarginSafetyHistogramsToFile) so percentiles can be computed exactly, including across multiple sharded runs. Parameters: CHAR *szHistogramPathOverride : forwarded to PersistMarginSafetyHistogramsToFile -- NULL for the default path. Return value: void **/ { _DumpMarginSafetyCalibrationInto("Regular lazy margin safety", g_uMarginSwingMax, g_uMarginSwingCount, 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, IN PAWN_HASH_ENTRY *pHash, IN OUT SCORE *piAlphaMargin, IN OUT SCORE *piBetaMargin) /** Routine description: Before doing an early lazy eval, look at the position and widen 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 full evaluation to find out. Both terms below are calibrated from measured data (CALIBRATE_ 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 two p90s (rather than deriving one joint p90) is a deliberately conservative (wider than strictly necessary) combination. O(1) -- no attack bitboard generation -- so it's safe to call whenever the cheap material-only lazy check (the caller's first-pass margin) wasn't enough to resolve the cutoff on its own. Parameters: SEARCHER_THREAD_CONTEXT *ctx : unused as of 2026-09-06 (_GetFileStormDefects, the only user, was pulled out of this hot path -- see the iKingSwingP90 staleness comment below); kept in the signature for call-site stability. POSITION *pos PAWN_HASH_ENTRY *pHash : unused as of 2026-09-06, same reason as ctx above. pHash->iScore is already folded into pos->iScore by this point regardless, and the passer-specific estimate this used to compute turned out to be negligible next to the residual term below. SCORE *piAlphaMargin, *piBetaMargin : widened in place, identically (no measured basis for an asymmetric alpha/beta split) Return value: void **/ { // 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). // // 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). // // 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): // // margin_a = [opponent's p90(term) - my p10(term)], summed over terms // margin_b = [my p90(term) - opponent's p10(term)], summed over terms // // 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; iSelfPessimisticEnemyOptimisticMargin = MAX0((iKingP98ByAttackerArmy[uMyBucket] - iKingP2ByAttackerArmy[uOppBucket]) + (iResidualOptimisticByOwnArmy[uOppBucket] - iResidualPessimisticByOwnArmy[uMyBucket])); iSelfOptimisticEnemyPessimisticMargin = MAX0((iKingP98ByAttackerArmy[uOppBucket] - iKingP2ByAttackerArmy[uMyBucket]) + (iResidualOptimisticByOwnArmy[uMyBucket] - iResidualPessimisticByOwnArmy[uOppBucket])); // 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; } // // ====================================================================== // static void _RecordTrappedCandidate(IN OUT POSITION *pos, IN ULONG uColor, IN COOR c) /** Routine description: Record a zero-mobility piece as a trapped-piece candidate for uColor. Used by _EvalBishop/_EvalKnight/_EvalRook/_EvalQueen; the actual attacked-or-not check happens later, in _EvalTrappedPieces. Capped at ARRAY_LENGTH(pos->cTrapped[0]) candidates per side -- extras are just dropped, not a real concern in practice. Parameters: POSITION *pos, ULONG uColor, COOR c Return value: void **/ { if (pos->uNumTrapped[uColor] < ARRAY_LENGTH(pos->cTrapped[uColor])) { pos->cTrapped[uColor][pos->uNumTrapped[uColor]++] = c; } } static void _EvalBishop(IN OUT POSITION *pos, IN COOR c, IN PAWN_HASH_ENTRY *pHash) /** Routine description: Parameters: POSITION *pos, COOR c, PAWN_HASH_ENTRY *pHash, Return value: FLAG **/ { static const BITBOARD bbColorSq[2] = { 0x55aa55aa55aa55aaULL, 0xaa55aa55aa55aa55ULL }; static const COOR cBishopAtHome[2][2] = { { C8, F8 }, // BLACK { C1, F1 } // WHITE }; ULONG uColor; ULONG xColor; BITBOARD bb; BITBOARD bbMask; BITBOARD bbPc; COOR cSquare; SCORE i; ULONG u; ULONG uTotalMobility; PIECE p; ASSERT(IS_ON_BOARD(c)); p = pos->rgSquare[c].pPiece; ASSERT(p && IS_BISHOP(p)); uColor = GET_COLOR(p); xColor = FLIP(uColor); // Undeveloped minor piece; maybe penalized later in EvalKing. pos->uMinorsAtHome[uColor] += (c == cBishopAtHome[uColor][0]); pos->uMinorsAtHome[uColor] += (c == cBishopAtHome[uColor][1]); ASSERT(pos->uMinorsAtHome[uColor] <= 4); // Good and bad bishops given stationary pawn structure. i = 16; bbMask = bbColorSq[IS_SQUARE_WHITE(c)]; bb = pHash->bbStationaryPawns[uColor] & bbMask; ASSERT(CountBits(bb) <= 16); while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&bb))) { // Consider stationary (rammed or backward) pawns of the same // color as the bishop. ASSERT(pos->rgSquare[cSquare].pPiece); ASSERT(IS_PAWN(pos->rgSquare[cSquare].pPiece)); ASSERT(GET_COLOR(pos->rgSquare[cSquare].pPiece) == uColor); i += STATIONARY_PAWN_ON_BISHOP_COLOR[cSquare]; } EVAL_TERM(uColor, BISHOP, c, pos->iScore[uColor], i, "good/bad stationary pawns ++"); i = 0; bbPc = ~(pHash->bbStationaryPawns[WHITE] | pHash->bbStationaryPawns[BLACK]); bbPc &= bbMask; bb = pos->bbPawns[uColor] & bbPc; while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&bb))) { ASSERT(pos->rgSquare[cSquare].pPiece); ASSERT(IS_PAWN(pos->rgSquare[cSquare].pPiece)); ASSERT(GET_COLOR(pos->rgSquare[cSquare].pPiece) == uColor); ASSERT(pos->bbPawns[uColor] & COOR_TO_BB(cSquare)); i += TRANSIENT_PAWN_ON_BISHOP_COLOR[cSquare]; } bb = pos->bbPawns[xColor] & bbPc; while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&bb))) { // N.B. Only count enemy pawns that are supported by another // pawn. ASSERT(pos->rgSquare[cSquare].pPiece); ASSERT(IS_PAWN(pos->rgSquare[cSquare].pPiece)); ASSERT(GET_COLOR(pos->rgSquare[cSquare].pPiece) == xColor); ASSERT(pos->bbPawns[xColor] & COOR_TO_BB(cSquare)); i += ((pos->bbPawnAttacks[xColor] & COOR_TO_BB(cSquare)) != 0) * TRANSIENT_PAWN_ON_BISHOP_COLOR[cSquare] / 2; } EVAL_TERM(uColor, BISHOP, c, pos->iScore[uColor], i, "good/bad transient pawns"); // // Removed 2026-08-30: BISHOP_IN_CLOSED_POSITION[pos->uClosedScaler] // duplicated bishop mobility (computed just below) rather than // adding a distinct angle on it. uClosedScaler is a coarse, // whole-board openness proxy for exactly the same fact bishop // mobility measures directly and per-bishop via ray-casting -- how // much this specific bishop's diagonals are blocked by pawns. The // mobility term is strictly more precise (opposite-bishop-color // endgames are the clearest case where "position is open overall" // and "this bishop's diagonal is blocked" diverge), so cut the // redundant whole-board proxy rather than scale it down. // // // Bishop mobility (and update attack tables) -- board_representation/ // EVAL.md section 1b: _BishopAttacksBB(c, pos->bbOccupied) // (generate.c's magic-bitboard slider lookup, already used by move // generation) gives the whole ray-to-first-blocker attack set in // one shot -- no per-square delta walk, no mailbox read, no switch // dispatch. The old BMobCaseTable's seven cases collapse to: // - terminal enemy non-pawn (opposing minor/rook/queen/king): // counts unconditionally -- covers both BMOB_ENEMY_SAME (no // x-ray) and BMOB_ENEMY_GREATER (x-rays past too); the // x-ray/no-x-ray distinction only matters for attack-bit // population below, not the mobility count itself. // - empty or terminal enemy pawn: counts unless pawn-unsafe // (BMOB_EMPTY / BMOB_ENEMY_PAWN) -- both terminal-square // categories together are exactly "not friend-occupied and not // enemy-non-pawn-occupied," the same partition knight's // reduction already uses. // - terminal friendly, non-stationary pawn on this bishop's own // color complex (BMOB_FRIEND_PAWN): still counts, the one case // that isn't a pure occupancy mask -- bbPc (already computed // above for the good/bad transient-pawn scoring) is exactly // "non-stationary pawns of either color on this color complex"; // ANDing with pos->bbPawns[uColor] and the attack set isolates // just this bishop's own transient pawns. // - any other friendly piece (knight/rook/king): blocks, no // 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; 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; bbSeen |= bbNew; bbXrayAccum |= (bbNew & bbFirstLayerMask); bbFirstLayerMask = ~(BITBOARD)0; bbMobility |= (bbEmptyHere & ~bbUnsafeForMinor) | bbEnemySameHere | bbEnemyGEHere | (bbEnemyPawnHere & ~bbUnsafeForMinor) | bbTransientHere; bbContinueHere = bbFriendBQHere | bbEnemyGEHere; if (!bbContinueHere) { break; } bbExclude |= bbContinueHere; bbLayer = _BishopAttacksBB(c, pos->bbOccupied & ~bbExclude); } pos->bbMinorXrayAttacks[uColor] |= bbXrayAccum; uTotalMobility = CountBits(bbMobility); ASSERT(uTotalMobility <= 13); EVAL_TERM(uColor, BISHOP, c, pos->iScore[uColor], BISHOP_MOBILITY_BY_SQUARES[uTotalMobility], "bishop mobility"); // // Look for bishops with no mobility, they are trapped and, later, // we'll see if they are also under attack too. // if (uTotalMobility == 0) { _RecordTrappedCandidate(pos, uColor, c); } // // Bonus for a bishop that's securely placed -- safe from ever // being challenged by an enemy pawn, and (checked below) defended // by a friendly one -- near the enemy king. This is a // bishop-specific king-tropism/outpost bonus. // bb = pos->bbPawns[xColor] & (~pHash->bbStationaryPawns[xColor]); if (TRUE == _IsSquareSafeFromEnemyPawn(pos, c, bb)) { // Defended (and defending) a friendly pawn. if (pos->bbPawnAttacks[uColor] & COOR_TO_BB(c)) { #ifdef DEBUG p = BLACK_PAWN | uColor; ASSERT(((IS_ON_BOARD(c + 17 * g_iBehind[uColor]) && (pos->rgSquare[c + 17 * g_iBehind[uColor]].pPiece==p)) || ((IS_ON_BOARD(c + 15 * g_iBehind[uColor])) && (pos->rgSquare[c + 15 * g_iBehind[uColor]].pPiece==p)))); #endif u = DISTANCE(c, KingPosition(pos, xColor)); i = BISHOP_UNASSAILABLE_BY_DIST_FROM_EKING[u]; EVAL_TERM(uColor, BISHOP, c, pos->iReducedMaterialDownScaler[uColor], i, "[scaled] unassailable"); } } } static void _EvalKnight(IN OUT POSITION *pos, IN COOR c, IN PAWN_HASH_ENTRY *pHash) /** Routine description: Parameters: POSITION *pos, COOR c, PAWN_HASH_ENTRY *pHash, Return value: FLAG **/ { static const int iPawnStart[2] = { -17, +15 }; static const COOR cKnightAtHome[2][2] = { { B8, G8 }, // BLACK { B1, G1 } // WHITE }; PIECE p; COOR cSquare; BITBOARD bb; ULONG uColor; ULONG xColor; ULONG uPawnsSupporting; ULONG uMobilitySquares; SCORE i; ULONG uDist; p = pos->rgSquare[c].pPiece; ASSERT(p && IS_KNIGHT(p)); uColor = GET_COLOR(p); ASSERT(IS_VALID_COLOR(uColor)); xColor = FLIP(uColor); // Unmoved piece; potentially penalized in EvalKing. pos->uMinorsAtHome[uColor] += (c == cKnightAtHome[uColor][0]); pos->uMinorsAtHome[uColor] += (c == cKnightAtHome[uColor][1]); ASSERT(pos->uMinorsAtHome[uColor] <= 4); // Centrality bonus EVAL_TERM(uColor, KNIGHT, c, pos->iScore[uColor], KNIGHT_CENTRALITY_BONUS[c], "board centrality"); // Give a bonus to knights on a closed / busy board. ASSERT(pos->uClosedScaler <= 32); EVAL_TERM(uColor, KNIGHT, c, pos->iScore[uColor], KNIGHT_IN_CLOSED_POSITION[pos->uClosedScaler], "in closed/open position"); // Outposted knights. See if square c is safe from enemy pawns; // if so give bonus for outposted knight which increases the // closer it is to the enemy king and the more pawns it has // supporting it. uDist = DISTANCE(c, KingPosition(pos, xColor)); ASSERT((uDist > 0) && (uDist <= 8)); bb = pos->bbPawns[xColor] & (~pHash->bbStationaryPawns[xColor]); if (TRUE == _IsSquareSafeFromEnemyPawn(pos, c, bb)) { // Count the number of supporting pawns the knight has uPawnsSupporting = 0; p = BLACK_PAWN | uColor; cSquare = c + iPawnStart[uColor]; uPawnsSupporting = (IS_ON_BOARD(cSquare) && (pos->rgSquare[cSquare].pPiece == p)); cSquare += 2; uPawnsSupporting += (IS_ON_BOARD(cSquare) && (pos->rgSquare[cSquare].pPiece == p)); ASSERT(uPawnsSupporting <= 2); // Give a bonus based on distance from enemy king i = KNIGHT_UNASSAILABLE_BY_DIST_FROM_EKING[uDist] + KNIGHT_WITH_N_PAWNS_SUPPORTING[uPawnsSupporting]; EVAL_TERM(uColor, KNIGHT, c, pos->iReducedMaterialDownScaler[uColor], i, "[scaled] unassailable/support"); // Now, if there's zero or one supporter see if the enemy has a // bishop the color of the knight it could capture with. if (uPawnsSupporting != 2) { if (IS_SQUARE_WHITE(c)) { if (WhiteSqBishopCount(pos, xColor)) { EVAL_TERM(uColor, KNIGHT, c, pos->iScore[uColor], -(i / 2), "not safe from enemy B"); } } else { if (FastCountBits(pos->bbPieces[xColor][BISHOP]) - WhiteSqBishopCount(pos, xColor)) { EVAL_TERM(uColor, KNIGHT, c, pos->iScore[uColor], -(i / 2), "not safe from enemy B"); } } } } else // not outposted { // Still good to be close to the enemy king. i = KNIGHT_KING_TROPISM_BONUS[uDist]; EVAL_TERM(uColor, KNIGHT, c, pos->iReducedMaterialDownScaler[uColor], i, "[scaled] enemy king tropism"); } // Give a bonus for blockading an enemy backward pawn. cSquare = c + 16 * g_iAhead[uColor]; bb = pHash->bbStationaryPawns[xColor]; if (bb & COOR_TO_BB(cSquare)) { ASSERT(pos->rgSquare[cSquare].pPiece); ASSERT(IS_PAWN(pos->rgSquare[cSquare].pPiece)); EVAL_TERM(uColor, KNIGHT, c, pos->iScore[uColor], KNIGHT_ON_INTERESTING_SQUARE_BY_RANK[uColor][RANK(c)], "blockades enemy pawn"); } // // Removed 2026-08-30 ("A knight with an open file behind it is // good"): dubious chess reasoning on its own (open-file bonuses // are classically a rook concept -- knights don't use files the // way rooks do) compounded by reusing KNIGHT_ON_INTERESTING_SQUARE // _BY_RANK, the *same* table as the backward-pawn-blockade bonus // just above, for a completely unrelated condition. That reuse // muddied what the table means and made it untunable // independently for either concept. // // // Do mobility and piece relevance. Also update attack tables. // // board_representation/EVAL.md section 1b/2: g_KnightAttacksBB[c] // (generate.c's precomputed table, already used by move // generation) is exactly the old per-square g_iNDeltas walk's // destination set, IS_ON_BOARD baked in at table-build time -- no // per-square branch, no mailbox read, no switch. Knights never // x-ray or have a battery partner, so the old NMobCaseTable's four // cases collapse to two bitboard masks: NMOB_ENEMY_OTHER (any // enemy non-pawn -- count unconditionally) and NMOB_MOBILE_SQUARE // (empty or enemy pawn -- count unless pawn-unsafe). NMOB_FRIEND // needs no term at all, it's just "neither of the above." // { BITBOARD bbAttack = g_KnightAttacksBB[c]; BITBOARD bbFriendOcc = _BuildFriendlySideBB(pos, uColor); BITBOARD bbEnemyNonPawnOcc = _BuildFriendlySideBB(pos, xColor) & ~pos->bbPawns[xColor]; BITBOARD bbUnsafeForMinor = pos->bbPawnAttacks[xColor]; BITBOARD bbMobility; pos->bbMinorAttacks[uColor] |= bbAttack; bbMobility = (bbAttack & bbEnemyNonPawnOcc) | (bbAttack & ~bbFriendOcc & ~bbEnemyNonPawnOcc & ~bbUnsafeForMinor); uMobilitySquares = CountBits(bbMobility); } ASSERT(uMobilitySquares >= 0); ASSERT(uMobilitySquares <= 8); EVAL_TERM(uColor, KNIGHT, c, pos->iScore[uColor], KNIGHT_MOBILITY_BY_COUNT[uMobilitySquares], "knight mobility"); if (uMobilitySquares == 0) { _RecordTrappedCandidate(pos, uColor, c); } } static void _EvalRook(IN OUT POSITION *pos, IN COOR c, IN PAWN_HASH_ENTRY *pHash) /** Routine description: Parameters: IN POSITION *pos, IN COOR c, IN PAWN_HASH_ENTRY *pHash, Return value: FLAG **/ { PIECE p; ULONG uColor; ULONG uTotalMobility; COOR cSquare; SCORE i; ASSERT(IS_ON_BOARD(c)); p = pos->rgSquare[c].pPiece; ASSERT(p && IS_ROOK(p)); uColor = GET_COLOR(p); ASSERT(IS_VALID_COLOR(uColor)); ULONG xColor = FLIP(uColor); ASSERT(IS_VALID_COLOR(xColor)); BITBOARD bbFile = BBFILE[FILE(c)]; BITBOARD bbFriendPawns = pos->bbPawns[uColor] & bbFile; BITBOARD bbEnemyPawns = pos->bbPawns[xColor] & bbFile; SCORE bonus = ROOK_FULL_HALF_OPEN_BONUS[bbFriendPawns != 0][bbEnemyPawns != 0]; bonus += (SCORE)((bbEnemyPawns & pHash->bbPasserLocations[xColor]) != 0) * ROOK_WITH_ENEMY_PASSER; bonus += (SCORE)((bbFriendPawns & pHash->bbPasserLocations[uColor]) != 0) * ROOK_WITH_FRIEND_PASSER; bonus += (SCORE)((COOR_TO_BB(c) & BBROOK_7TH_RANK[uColor]) != 0) * ROOK_ON_SEVENTH_BONUS; EVAL_TERM(uColor, ROOK, c, pos->iScore[uColor], bonus, "file/passer bonus"); // // Removed 2026-08-30 ("rook trapping enemy king"): a rook on the // 7th/8th rank aligned with the enemy king is exactly the // geometric pattern CountKingSafetyDefects' CHECK_VECTOR // line-of-sight scan already picks up into // uPiecesPointingAtKing/king-safety -- this was pricing the same // king-danger fact a second time via a rook-specific bonus. Belongs // in king safety, not here. (pFriendRook, only ever assigned in // this block and never read, is gone too.) // // // Rooks increase in value as pawns come off: // // "A further refinement would be to raise the knight's value by // 1/16 and lower the rook's value by 1/8 for each pawn above five // of the side being valued, with the opposite adjustment for each // pawn short of five." // --Larry Kaufman, IM // "Evaluation of Material Imbalance" // EVAL_TERM(uColor, ROOK, c, pos->iScore[uColor], ROOK_VALUE_AS_PAWNS_COME_OFF[FastCountBits(pos->bbPawns[uColor]) + FastCountBits(pos->bbPawns[FLIP(uColor)])], "increase in value/pawns"); // // Rook mobility (and update attack tables) -- board_representation/ // EVAL.md section 1b/9: _RookAttacksBB(c, pos->bbOccupied) // (generate.c's magic-bitboard slider lookup) gives the whole // ray-to-first-blocker attack set in one shot, same technique // already landed for knight/bishop. The old RMobCaseTable's six // live cases collapse to: // - terminal enemy rook/queen/king (RMOB_ENEMY_SAME/_GREATER): // counts unconditionally, no unsafe check -- capturing an // equal-or-higher piece is never "unsafe" in the mobility // sense. Queen/king additionally x-ray past (_GREATER); rook // does not (_SAME). // - empty or terminal enemy pawn/knight/bishop (RMOB_EMPTY / // RMOB_ENEMY_LESS): counts unless the square is attacked by an // enemy pawn or minor -- both terminal-square categories // together are exactly "not friend-occupied and not // enemy-rook/queen/king-occupied," the same partition // bishop/knight's reductions already use with their own value // thresholds. // - any friendly piece (RMOB_FRIEND_BLOCK/_ROOK/_QUEEN): never // counts, simply excluded by the friendly-occupied mask. // Friendly rook/queen additionally x-ray past (no mobility // credit at the blocker itself, matching the old table); a // friendly rook blocker also earns the connected-rook bonus. // { BITBOARD bbAttack = _RookAttacksBB(c, pos->bbOccupied); BITBOARD bbEnemyOcc = _BuildFriendlySideBB(pos, FLIP(uColor)); BITBOARD bbEnemySame = pos->bbPieces[FLIP(uColor)][ROOK]; BITBOARD bbEnemyGEContinue = pos->bbPieces[FLIP(uColor)][QUEEN] | COOR_TO_BB(KingPosition(pos, FLIP(uColor))); BITBOARD bbFriendRQ = pos->bbPieces[uColor][ROOK] | pos->bbPieces[uColor][QUEEN]; BITBOARD bbUnsafeForRook = pos->bbPawnAttacks[FLIP(uColor)] | pos->bbMinorAttacks[FLIP(uColor)]; BITBOARD bbExclude = 0; BITBOARD bbSeen = 0; BITBOARD bbLayer = bbAttack; BITBOARD bbMobility = 0; BITBOARD bbXrayAccum = 0; BITBOARD bbFirstLayerMask = 0; pos->bbRookAttacks[uColor] |= bbAttack; // // Unified chain walk over both mobility and x-ray population // in one pass, board_representation/EVAL.md section 9. The // old mailbox walk's RMOB_FRIEND_ROOK/_QUEEN and // RMOB_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 safe/empty squares // and further captures lie beyond, however many such blockers // are stacked on one ray. A first version of this conversion // (committed, since fixed) only handled the near side (up to // the first blocker) for mobility and treated the chain purely // as an attack-bit population exercise -- caught by comparing // against the old mailbox walk directly on a battery position // (two same-color rooks on an open file: old code credited 13 // mobility to the far rook's near companion via squares beyond // it, the near-side-only version credited only 9). Fixed by // reusing the exact same "recompute with blockers excluded" // chain used for x-ray population to also accumulate mobility // credit at every layer, not just the first. // // Each iteration classifies the newly-revealed blockers on // each still-open ray: friendly rook/queen and enemy queen/ // king continue the chain (recompute with them excluded too); // everything else (friendly non-R/Q, enemy pawn/knight/bishop, // enemy rook, or the board edge) stops that ray. Bounded: each // iteration either terminates or consumes at least one new // piece into the exclusion set, so this can loop at most // once per piece on the board. // for (;;) { BITBOARD bbNew = bbLayer & ~bbSeen; BITBOARD bbBlockersHere = bbNew & pos->bbOccupied; BITBOARD bbEmptyHere = bbNew & ~pos->bbOccupied; BITBOARD bbFriendRQHere = bbBlockersHere & bbFriendRQ; BITBOARD bbEnemySameHere = bbBlockersHere & bbEnemySame; BITBOARD bbEnemyGEHere = bbBlockersHere & bbEnemyGEContinue; BITBOARD bbEnemyLEHere = bbBlockersHere & bbEnemyOcc & ~bbEnemySameHere & ~bbEnemyGEHere; BITBOARD bbFriendRookHere = bbBlockersHere & pos->bbPieces[uColor][ROOK]; BITBOARD bbContinueHere; bbSeen |= bbNew; bbXrayAccum |= (bbNew & bbFirstLayerMask); bbFirstLayerMask = ~(BITBOARD)0; bbMobility |= (bbEmptyHere & ~bbUnsafeForRook) | bbEnemySameHere | bbEnemyGEHere | (bbEnemyLEHere & ~bbUnsafeForRook); // // Connected-rook bonus fires at every friendly rook found // along the chain, not just the first (matches the old // walk: each RMOB_FRIEND_ROOK hit its own EVAL_TERM call). // while (bbFriendRookHere) { COOR cBlocker = CoorFromBitBoardRank8ToRank1(&bbFriendRookHere); FLAG fHoriz = (RANK(cBlocker) == RANK(c)); EVAL_TERM(uColor, ROOK, cBlocker, pos->iScore[uColor], (ROOK_CONNECTED_HORIZ * fHoriz + ROOK_CONNECTED_VERT * FLIP(fHoriz)), "rook connected"); } bbContinueHere = bbFriendRQHere | bbEnemyGEHere; if (!bbContinueHere) { break; } bbExclude |= bbContinueHere; bbLayer = _RookAttacksBB(c, pos->bbOccupied & ~bbExclude); } pos->bbRookXrayAttacks[uColor] |= bbXrayAccum; uTotalMobility = CountBits(bbMobility); } ASSERT(uTotalMobility <= 14); ASSERT(pos->uArmyScaler[FLIP(uColor)] <= 31); i = ROOK_MOBILITY_BY_SQUARES[uTotalMobility]; i *= (int)(REDUCED_MATERIAL_UP_SCALER[pos->uArmyScaler[FLIP(uColor)]] + 1); i /= 8; EVAL_TERM(uColor, ROOK, c, pos->iScore[uColor], i, "rook mobility"); if (uTotalMobility < 3) { // Look for rooks with no mobility who are under attack. These // pieces are trapped! if (uTotalMobility == 0) { _RecordTrappedCandidate(pos, uColor, c); } // Rook trapped in the corner by a stupid friendly king? -- // color-indexed instead of a WHITE/BLACK branch over two // mirrored blocks (board_representation/EVAL.md section 9). ASSERT(IS_VALID_COLOR(uColor)); { static const ULONG BACK_RANK_MASK[2] = { 0x00, 0x70 }; // BLACK, WHITE static const COOR E_CORNER[2] = { E8, E1 }; static const COOR D_CORNER[2] = { D8, D1 }; ASSERT(IS_ON_BOARD(c)); if ((c & 0xF0) == BACK_RANK_MASK[uColor]) { cSquare = KingPosition(pos, uColor); ASSERT(IS_ON_BOARD(cSquare)); if ((cSquare & 0xF0) == BACK_RANK_MASK[uColor]) { if (((cSquare > E_CORNER[uColor]) && (c > cSquare)) || ((cSquare < D_CORNER[uColor]) && (c < cSquare))) { EVAL_TERM(uColor, ROOK, c, pos->iScore[uColor], KING_TRAPPING_ROOK, "king trapping rook"); } } } } } // if low mobility } static void _EvalQueen(IN OUT POSITION *pos, IN COOR c, IN PAWN_HASH_ENTRY *pHash) /** Parameters: POSITION *pos, COOR c, PAWN_HASH_ENTRY *pHash, Return value: FLAG **/ { PIECE p = pos->rgSquare[c].pPiece; ULONG uColor; ULONG uTotalMobility; ULONG u; COOR cKing; ASSERT(IS_ON_BOARD(c)); ASSERT(p && IS_QUEEN(p)); uColor = GET_COLOR(p); ASSERT(IS_VALID_COLOR(uColor)); cKing = KingPosition(pos, FLIP(uColor)); ASSERT(IS_ON_BOARD(cKing)); ASSERT(IS_KING(pos->rgSquare[cKing].pPiece)); // Encourage enemy king tropism, but not too early. if ((TRUE == pos->fCastled[uColor]) || (pos->uMinorsAtHome[uColor] <= 1)) { u = DISTANCE(c, cKing); ASSERT((u <= 7) && (u > 0)); EVAL_TERM(uColor, QUEEN, c, pos->iScore[uColor], QUEEN_KING_TROPISM[u], "enemy king tropism"); } // // Do queen mobility (and update attack tables) -- two-pass // rook-direction/bishop-direction magic lookups, board_ // representation/EVAL.md section 1b/9: MOVEGEN_MIGRATION.md // already found (in the stashed first bitboard-eval attempt's // _EvalQueenOccupancyBB PoC) that a combined 8-ray table measures // *slower* than reusing the rook/bishop tables in two passes -- // reuse that structure here too, don't rediscover the regression. // Each pass is its own unified chain walk, identical technique to // rook's/bishop's own conversions (including their fix for // crediting mobility beyond a battery partner, not just x-ray // attack bits): continue through a friendly queen in both passes, // a friendly rook only in the rook-direction pass, a friendly // bishop only in the bishop-direction pass (QMOB_FRIEND_ROOK/ // _BISHOP's old per-ray-family split falls out for free from // *which* lookup a blocker shows up in -- no fOrthogonalRay flag // needed). Unlike rook/bishop, queen never x-rays through *any* // enemy piece (QMOB_ENEMY_GE always stopped in the old table, the // one case that's a *fewer*-cases asymmetry versus bishop, not // more), so the enemy side of each pass's continue-set is empty. // { BITBOARD bbEnemyOcc = _BuildFriendlySideBB(pos, FLIP(uColor)); BITBOARD bbEnemyGE = pos->bbPieces[FLIP(uColor)][QUEEN] | COOR_TO_BB(cKing); BITBOARD bbUnsafeForQueen = pos->bbPawnAttacks[FLIP(uColor)] | pos->bbMinorAttacks[FLIP(uColor)] | pos->bbRookAttacks[FLIP(uColor)]; BITBOARD bbAttackTotal = 0; BITBOARD bbXrayAccum = 0; BITBOARD bbMobility = 0; ULONG uPass; for (uPass = 0; uPass < 2; uPass++) { BITBOARD bbFriendContinue = (uPass == 0) ? (pos->bbPieces[uColor][ROOK] | pos->bbPieces[uColor][QUEEN]) : (pos->bbPieces[uColor][BISHOP] | pos->bbPieces[uColor][QUEEN]); BITBOARD bbInitial = (uPass == 0) ? _RookAttacksBB(c, pos->bbOccupied) : _BishopAttacksBB(c, pos->bbOccupied); BITBOARD bbExclude = 0; BITBOARD bbSeen = 0; BITBOARD bbLayer = bbInitial; BITBOARD bbFirstLayerMask = 0; bbAttackTotal |= bbInitial; for (;;) { BITBOARD bbNew = bbLayer & ~bbSeen; BITBOARD bbBlockersHere = bbNew & pos->bbOccupied; BITBOARD bbEmptyHere = bbNew & ~pos->bbOccupied; BITBOARD bbFriendContinueHere = bbBlockersHere & bbFriendContinue; BITBOARD bbEnemyGEHere = bbBlockersHere & bbEnemyGE; BITBOARD bbEnemyLEHere = bbBlockersHere & bbEnemyOcc & ~bbEnemyGEHere; bbSeen |= bbNew; bbXrayAccum |= (bbNew & bbFirstLayerMask); bbFirstLayerMask = ~(BITBOARD)0; bbMobility |= (bbEmptyHere & ~bbUnsafeForQueen) | bbEnemyGEHere | (bbEnemyLEHere & ~bbUnsafeForQueen); if (!bbFriendContinueHere) { break; } bbExclude |= bbFriendContinueHere; bbLayer = (uPass == 0) ? _RookAttacksBB(c, pos->bbOccupied & ~bbExclude) : _BishopAttacksBB(c, pos->bbOccupied & ~bbExclude); } } pos->bbQueenAttacks[uColor] |= bbAttackTotal; pos->bbQueenXrayAttacks[uColor] |= bbXrayAccum; uTotalMobility = CountBits(bbMobility); } ASSERT(uTotalMobility <= 27); EVAL_TERM(uColor, QUEEN, c, pos->iScore[uColor], QUEEN_MOBILITY_BY_SQUARES[uTotalMobility], "queen mobility"); // Look for queens with no mobility who are under attack. These // pieces are trapped! if (uTotalMobility == 0) { _RecordTrappedCandidate(pos, uColor, c); } } static void _EvalKing(IN OUT POSITION *pos, IN const COOR c, IN const PAWN_HASH_ENTRY *pHash, IN SEARCHER_THREAD_CONTEXT *ctx, IN const ULONG uTotalNonPawnsBothSides) /** Routine description: Parameters: POSITION *pos, COOR c, PAWN_HASH_ENTRY *pHash, ULONG uTotalNonPawnsBothSides : TotalNonPawnCount(pos, WHITE) + TotalNonPawnCount(pos, BLACK), hoisted up to Eval() (chess.h's TotalNonPawnCount is a POPCNT now, not a stored field -- see its comment) since this function runs twice per Eval() call (once per king) and used to recompute the identical sum both times. Return value: FLAG **/ { PIECE p; ULONG uColor, xColor; COOR cSquare; ULONG u; BITV bvPattern; ULONG uQueenNearKing; BITBOARD bb; SCORE i; SCORE iKingScore = 0; #ifdef KING_DEFECT_DUMP KING_DEFECT_DUMP_INFO *pDump; #endif // 2026-09-15: linear-SCORE rewrite -- see king_safety_data/ // HANDOFF.md's 2026-09-15 addendum for the full rationale. Replaces // the old shared uCounter -> KING_SAFETY_BY_COUNTER nonlinear-lookup // mechanism (EmptyAttackedSquare/EmptyUnattackedSquare/ // OccupiedAttackedSquare/KingFlightDefects/KingSafetyDeltas/ // KING_RING_IS_BACK all retired) with per-defect SCORE weights that // accumulate into one running total and get scaled by // REDUCED_MATERIAL_DOWN_SCALER exactly ONCE at the end -- by direct // instruction, since this is the hottest inner loop in eval.c and // scaler*(A+B+C+...) is one multiply instead of scaler*A + scaler*B // + ... Ring-square attack/defense/flight terms below are computed // via bitboard intersection + CountBits() against the whole front/ // back/side bucket at once (g_KingRingFrontBB/g_KingRingBackBB/ // g_KingRingSideBB, data.c), not a per-square branchy loop -- a // fixed handful of AND+popcount ops regardless of how many ring // squares are actually occupied/attacked. // // Magnitudes are the mate-mining logistic regression's cleaned, // collinearity-reduced coefficients (front_def/back_def/*_atk_queen // dropped -- collinear with the atk_* terms and queenproximity // respectively), unstandardized (divided by each raw feature's own // std-dev) and converted to centipawns via k =~ -14, the same // conversion constant KING_QUEEN_PROXIMITY_DANGER's self-play- // validated -14/count already established (anchor: validated weight // / that term's own cleaned raw coefficient). None of the NEW // per-piece-type/undefended/xray/flight terms below have been // through self-play yet as of this writing -- expect every one of // these starting magnitudes to move once tested, same discipline as // the file-storm and queenproximity retunes before them. // // 2026-09-15 (later): promoted from local statics to file scope // and DNA-registered (see g_EvalDNA below) so they can be Texel- // tuned directly against real game outcomes instead of hand- // translated from regression coefficients -- see king_safety_data/ // HANDOFF.md's addendum on why the log-odds-to-centipawn // translation step was the likely source of this rewrite's // self-play regression, not the underlying feature signal itself. const ULONG uKingDangerAcceleratorBucketWidth = 56; ASSERT(IS_ON_BOARD(c)); p = pos->rgSquare[c].pPiece; ASSERT(p && IS_KING(p)); uColor = GET_COLOR(p); xColor = FLIP(uColor); ASSERT(IS_VALID_COLOR(uColor)); // Update attack bitvs for squares around the king. BITBOARD bbRing = g_KingAttacksBB[c]; pos->bbKingAttacks[uColor] |= bbRing; #ifdef KING_DEFECT_DUMP pDump = &ctx->sKingDefectDump[uColor]; memset(pDump, 0, sizeof(KING_DEFECT_DUMP_INFO)); #endif // Don't bother with king safety when the other side has no mating // material. u = pos->uNonPawnMaterial[xColor]; ASSERT(u >= VALUE_KING); if (u < DO_KING_SAFETY_THRESHOLD) { goto skip_safety; } // Depending on whether we are assessing a black or white king, // the "front" and "back" directions swap. Front is "towards // the center of the board at game start time". BITBOARD bbFront = g_KingRingFrontBB[uColor][c]; BITBOARD bbBack = g_KingRingBackBB[uColor][c]; BITBOARD bbSide = g_KingRingSideBB[c]; ASSERT(bbRing == (bbFront | bbBack | bbSide)); BITBOARD bbEnemyAtkAny = pos->bbPawnAttacks[xColor] | pos->bbMinorAttacks[xColor] | pos->bbRookAttacks[xColor] | pos->bbQueenAttacks[xColor]; BITBOARD bbEnemyXrayAny = pos->bbMinorXrayAttacks[xColor] | pos->bbRookXrayAttacks[xColor] | pos->bbQueenXrayAttacks[xColor]; BITBOARD bbOwnDefendNoKing = pos->bbPawnAttacks[uColor] | pos->bbMinorAttacks[uColor] | pos->bbRookAttacks[uColor] | pos->bbQueenAttacks[uColor]; BITBOARD bbEmpty = ~pos->bbOccupied; BITBOARD bbUndefended = bbEnemyAtkAny & ~bbOwnDefendNoKing; SCORE iSafety = 0; // Pay attention to the types of pieces currently attacking. bvPattern = (((pos->bbPawnAttacks[xColor] & bbRing) != 0) ? PAWN_BIT : 0) | ((((pos->bbMinorAttacks[xColor] | pos->bbMinorXrayAttacks[xColor]) & bbRing) != 0) ? MINOR_BIT : 0) | ((((pos->bbRookAttacks[xColor] | pos->bbRookXrayAttacks[xColor]) & bbRing) != 0) ? ROOK_BIT : 0) | ((((pos->bbQueenAttacks[xColor] | pos->bbQueenXrayAttacks[xColor]) & bbRing) != 0) ? QUEEN_BIT : 0); ASSERT(bvPattern < 32); iSafety += KING_ATTACK_PATTERN_SCALE * (SCORE)KING_COUNTER_BY_ATTACK_PATTERN[bvPattern]; // Count how many ring squares are attacked by the enemy queen. uQueenNearKing = CountBits( bbRing & (pos->bbQueenAttacks[xColor] | pos->bbQueenXrayAttacks[xColor]) ); ASSERT(uQueenNearKing <= 8); iSafety += KING_QUEEN_PROXIMITY_DANGER[MINU(uQueenNearKing, 6)]; // Minor attacks at front, side, back. iSafety += KING_RING_ATTACK_MINOR[0] * (SCORE)CountBits(bbFront & pos->bbMinorAttacks[xColor]); iSafety += KING_RING_ATTACK_MINOR[1] * (SCORE)CountBits(bbBack & pos->bbMinorAttacks[xColor]); iSafety += KING_RING_ATTACK_MINOR[2] * (SCORE)CountBits(bbSide & pos->bbMinorAttacks[xColor]); // Rook attacks at front, side, back. iSafety += KING_RING_ATTACK_ROOK[0] * (SCORE)CountBits(bbFront & pos->bbRookAttacks[xColor]); iSafety += KING_RING_ATTACK_ROOK[1] * (SCORE)CountBits(bbBack & pos->bbRookAttacks[xColor]); iSafety += KING_RING_ATTACK_ROOK[2] * (SCORE)CountBits(bbSide & pos->bbRookAttacks[xColor]); // Pawn attacks. iSafety += KING_RING_ATTACK_PAWN * (SCORE)CountBits(bbRing & pos->bbPawnAttacks[xColor]); // Undefended squares in front, side, back. iSafety += KING_RING_UNDEFENDED[0] * (SCORE)CountBits(bbFront & bbUndefended); iSafety += KING_RING_UNDEFENDED[1] * (SCORE)CountBits(bbBack & bbUndefended); iSafety += KING_RING_UNDEFENDED[2] * (SCORE)CountBits(bbSide & bbUndefended); // Only xray attack, no direct attack. iSafety += KING_RING_XRAY_BONUS * (SCORE)CountBits(bbRing & bbEnemyXrayAny & ~bbEnemyAtkAny); // Flight squares are empty, unattacked squares around the king. // In front of the king, though, these are gaps and a negative // whereas behind the king they are legitimately small bonuses. iSafety += KING_RING_FRONT_FLIGHT_PENALTY * (SCORE)CountBits(bbFront & bbEmpty & ~bbEnemyAtkAny); // Small bonus for empty squares behind the king to retreat into. iSafety += KING_BACK_FLIGHT_BONUS * (SCORE)CountBits(bbBack & bbEmpty & ~bbEnemyAtkAny); #ifdef KING_DEFECT_DUMP { // Debug-only, per-square breakdown for the mining pipeline // (extract_king_features.py) -- kept fully independent of // the bitboard computation above, same traversal shape as // the pre-rewrite loop so king_features_v4.csv's column // semantics stay unchanged. Does not feed iSafety. static INT KingSafetyDeltas[9] = { -17, -16, -15, -1, +1, +15, +16, +17, 0 }; ULONG uSlot = 0; BITV bvSlotAttack, bvSlotXray, bvSlotDefend; do { cSquare = c + KingSafetyDeltas[uSlot]; if (IS_ON_BOARD(cSquare)) { BITBOARD sq = COOR_TO_BB(cSquare); BITV bvEither; bvSlotAttack = ((pos->bbPawnAttacks[xColor] & sq) ? PAWN_BIT : 0) | ((pos->bbMinorAttacks[xColor] & sq) ? MINOR_BIT : 0) | ((pos->bbRookAttacks[xColor] & sq) ? ROOK_BIT : 0) | ((pos->bbQueenAttacks[xColor] & sq) ? QUEEN_BIT : 0); bvSlotXray = ((pos->bbMinorXrayAttacks[xColor] & sq) ? MINOR_BIT : 0) | ((pos->bbRookXrayAttacks[xColor] & sq) ? ROOK_BIT : 0) | ((pos->bbQueenXrayAttacks[xColor] & sq) ? QUEEN_BIT : 0); bvSlotDefend = ((pos->bbPawnAttacks[uColor] & sq) ? PAWN_BIT : 0) | ((pos->bbMinorAttacks[uColor] & sq) ? MINOR_BIT : 0) | ((pos->bbRookAttacks[uColor] & sq) ? ROOK_BIT : 0) | ((pos->bbQueenAttacks[uColor] & sq) ? QUEEN_BIT : 0) | ((pos->bbKingAttacks[uColor] & sq) ? KING_BIT : 0); bvEither = bvSlotAttack | bvSlotXray; ASSERT(uSlot < KING_RING_NUM_SLOTS); pDump->fOnBoard[uSlot] = TRUE; pDump->fAttackedByPieceType[uSlot][KING_RING_PAWN] = ((bvEither & PAWN_BIT) != 0); pDump->fAttackedByPieceType[uSlot][KING_RING_MINOR] = ((bvEither & MINOR_BIT) != 0); pDump->fAttackedByPieceType[uSlot][KING_RING_ROOK] = ((bvEither & ROOK_BIT) != 0); pDump->fAttackedByPieceType[uSlot][KING_RING_QUEEN] = ((bvEither & QUEEN_BIT) != 0); if (bvSlotAttack != 0) { BITV bvDefendForDump = bvSlotDefend; if (bvSlotXray || (bvSlotAttack & (bvSlotAttack - 1))) { bvDefendForDump &= ~KING_BIT; } if (bvDefendForDump == 0) { pDump->fAttackedUndefended[uSlot] = TRUE; } else { pDump->fAttackedDefended[uSlot] = TRUE; } } else { if (bvSlotXray != 0) { pDump->fXrayedUnattacked[uSlot] = TRUE; } if (!(pos->bbOccupied & sq)) { pDump->fEmptyFlight[uSlot] = TRUE; } } } uSlot++; } while (KingSafetyDeltas[uSlot] != 0); } #endif // // King's own file plus the two adjacent ones. EXPERIMENTAL, // prototype-only (2026-09-05, board_representation/EVAL.md // section 9's king-safety-hash discussion): now routed through // _GetFileStormDefects's cache instead of computed inline. // Already a direct SCORE as of the 2026-09-15 rewrite -- see // _ComputeFileStormDefects's comment. // iSafety += _GetFileStormDefects(ctx, pos, pHash, uColor); #ifdef KING_DEFECT_DUMP _ComputeFileStormDefectsSplit(pos, pHash, uColor, &pDump->uFileStormOpenFile, &pDump->uFileStormStormingPawn); pDump->uFileStormDefects = pDump->uFileStormOpenFile + pDump->uFileStormStormingPawn; #endif if (u >= KEEP_KING_AT_HOME_THRESHOLD) { iSafety += KING_INITIAL_LOCATION_SCALE * KING_EVAL_INITIAL_LOCATION[uColor][c]; } #ifdef KING_DEFECT_DUMP pDump->uInitialCounterByLocation = (u >= KEEP_KING_AT_HOME_THRESHOLD) ? (ULONG)KING_EVAL_INITIAL_LOCATION[uColor][c] : 0; #endif // Danger-magnitude acceleration -- see KING_DANGER_ACCELERATOR's // comment above. Applied here (as part of the real per-king // computation) rather than folded into the final material-scale // multiply below, since it's a function of the aggregate danger // itself, not of the attacker's material. if (iSafety < 0) { ULONG uMagBucket = MINU(6, (ULONG)(-iSafety) / uKingDangerAcceleratorBucketWidth); iSafety = (iSafety * KING_DANGER_ACCELERATOR[uMagBucket]) / 128; } #ifdef CALIBRATE_POSITIONAL // Restored 2026-09-15 (was dropped when the old uCounter/ // KING_SAFETY_BY_COUNTER-shaped call site disappeared in the // linear-SCORE rewrite) -- see _RecordPositionalCalibration's // comment for why this now bins ABS(iSafety) instead of the old // always-nonnegative -i. _RecordPositionalCalibration(ctx, pos, pHash, uColor, (ULONG)((iSafety < 0) ? -iSafety : iSafety)); #endif // Single multiply for the entire accumulated total -- the // whole point of this rewrite (see the function-header comment // above). Note: can't use pos->iReducedMaterialDownScaler here // because we are scaling based on the _other_ side's material. iSafety = (iSafety * (SCORE)REDUCED_MATERIAL_DOWN_SCALER[pos->uArmyScaler[xColor]]) / 8; EVAL_TERM(uColor, KING, c, iKingScore, iSafety, "king safety"); #ifdef KING_DEFECT_DUMP pDump->iRealSafety = iSafety; pDump->uQueenNearKing = uQueenNearKing; // uCounterTotal/uSafetyTableValue no longer have a coherent // meaning post-rewrite (no more shared uCounter/KING_SAFETY_ // BY_COUNTER lookup) -- left zeroed by the memset above rather // than populated with something misleading. Both were REFERENCE- // only columns in the training pipeline (train_king_safety_ // model.py explicitly excludes them from the feature set), so // this doesn't affect the regression itself. pDump->fCastled = pos->fCastled[uColor]; pDump->uMinorsAtHome = pos->uMinorsAtHome[uColor]; pDump->uAttackerArmyScaler = pos->uArmyScaler[xColor]; #endif // Bonus for castling / penalty for loss of castle. Also, if side has // not yet castled, be concerned with undeveloped minor pieces. if (FALSE == pos->fCastled[uColor]) { ULONG uDevelopmentScaler = CASTLE_AND_DEVELOPMENT_SCALER[pos->uArmyScaler[uColor]]; if (uDevelopmentScaler > 0) { ASSERT(pos->uMinorsAtHome[uColor] <= 4); EVAL_TERM(uColor, 0, ILLEGAL_COOR, pos->iScore[uColor], (SCORE)((UNDEVELOPED_MINORS_IN_OPENING[pos->uMinorsAtHome[uColor]] * (SCORE)uDevelopmentScaler) / 8), "development"); // Color-indexed instead of a WHITE/BLACK branch over // two copy-pasted blocks -- one fewer per-piece branch // for the processor to mispredict (board_representation/ // EVAL.md section 9). static const BITV CASTLE_SHORT[2] = { CASTLE_BLACK_SHORT, CASTLE_WHITE_SHORT }; static const BITV CASTLE_LONG[2] = { CASTLE_BLACK_LONG, CASTLE_WHITE_LONG }; EVAL_TERM(uColor, KING, c, iKingScore, (SCORE)((KING_MISSING_ONE_CASTLE_OPTION * ((CASTLE_SHORT[uColor] & pos->bvCastleInfo) == 0) * (SCORE)uDevelopmentScaler) / 8), "can't castle short"); EVAL_TERM(uColor, KING, c, iKingScore, (SCORE)((KING_MISSING_ONE_CASTLE_OPTION * ((CASTLE_LONG[uColor] & pos->bvCastleInfo) == 0) * (SCORE)uDevelopmentScaler) / 8), "can't castle long"); #ifdef KING_DEFECT_DUMP pDump->fMissingCastleShort = ((CASTLE_SHORT[uColor] & pos->bvCastleInfo) == 0); pDump->fMissingCastleLong = ((CASTLE_LONG[uColor] & pos->bvCastleInfo) == 0); #endif } } skip_safety: // // Special code for kings in the endgame // u = uTotalNonPawnsBothSides; if (u < 8) { // Encourage kings to come to the center when safe. i = KING_TO_CENTER[c]; EVAL_TERM(uColor, KING, c, iKingScore, i, "centralize king"); // Kings in front of passers in the late endgame are strong... // -- bb here is exactly the own-passer squares at king-move // distance 1 (g_KingAttacksBB is that mask by construction), so // every popped square counts; no separate DISTANCE() filter or // ASSERTs needed. The extra "leading the pawn toward promotion" // bonus applies iff the passer square is in the king's own // back ring (g_KingRingBackBB) -- side- and front-ring passers // still get the base rank-scaled support bonus, just not this // extra. bb = pHash->bbPasserLocations[uColor] & g_KingAttacksBB[c]; while(IS_ON_BOARD(cSquare = CoorFromBitBoardRank8ToRank1(&bb))) { i = 2; i += KING_SUPPORTING_OWN_PASSER_BY_RANK[uColor][RANK(cSquare)]; i += 8 * ((COOR_TO_BB(cSquare) & g_KingRingBackBB[uColor][c]) != 0); EVAL_TERM(uColor, KING, c, iKingScore, i, "supporting own passer"); } // // Detect kings way out of the action in KP endgames: // // 8/8/1p6/p6k/P3K3/8/1P6/8 w - - 0 11 // 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))) { i += (DISTANCE(cSquare, KingPosition(pos, BLACK)) - DISTANCE(cSquare, KingPosition(pos, WHITE))); } EVAL_TERM(WHITE, KING, c, iKingScore, i * 2, "in/out of \"action\""); } } pos->iScore[uColor] += iKingScore; ctx->sPlyInfo[ctx->uPly].iKingScore[uColor] = iKingScore; } FLAG _EvalPasserRacesAgainstLoneKings(IN OUT POSITION *pos, IN PAWN_HASH_ENTRY *pHash) /** Routine description: Determine if one side has a pawn that is unstoppable. A 2026-09-16 investigation (see recogn.c's _RecognizeKPK for the full writeup) found this function's own king-catches-pawn race distance comparison did NOT actually reproduce the bug in 7k/8/5KP1/8/8/8/8/8 w - - (Kf6/Pg6/Kh8, White to move -- a draw via stalemate defense that a sibling function, _RecognizeKPK, used to misjudge as a win) -- a file-specific guard added here on suspicion turned out to be unnecessary once _RecognizeKPK's real bug was fixed and was removed after TestRecognExhaustiveKPK (testrecogn.c) confirmed 0 wrong verdicts across all EGTB-covered legal KPK positions without it. Left undocumented/unfixed here: whether this function has its own independent bugs of the same general shape was NOT exhaustively verified (TestRecognExhaustiveKPK exercises it only via RecognLookup's pHash-lookup call path, which doesn't reach every branch of this function directly) -- worth exhaustively checking on its own if this area gets touched again. Parameters: POSITION *pos, PAWN_HASH_ENTRY *pHash, Return value: void **/ { BITBOARD bb[2]; ULONG uColor; int d1; COOR cQueen; COOR cKing, c; ULONG uKingDist, uPawnDist, uFriendDist; ULONG uRacerDist[2]; FLAG fDontCountMeOut[2]; bb[BLACK] = pHash->bbPasserLocations[BLACK]; bb[WHITE] = pHash->bbPasserLocations[WHITE]; if (!(bb[BLACK] | bb[WHITE])) { return(FALSE); } ASSERT((TotalNonPawnCount(pos, WHITE) == 1) || (TotalNonPawnCount(pos, BLACK) == 1)) // Both deferred past the no-passer early return above -- no need to // pay for this on the common case (most positions have no passer at // all, and _EvalPasserRacesAgainstLoneKings itself is only even called when a side // is down to a bare king, so this function runs unconditionally in // the pre-lazy-exit segment whenever that's true). uRacerDist[BLACK] = 99; uRacerDist[WHITE] = 99; fDontCountMeOut[BLACK] = ((RANK(KingPosition(pos, BLACK)) <= 3) && (pos->bbPawns[BLACK] != 0)); fDontCountMeOut[WHITE] = ((RANK(KingPosition(pos, WHITE)) >= 6) && (pos->bbPawns[WHITE] != 0)); FOREACH_COLOR(uColor) { if (TotalNonPawnCount(pos, FLIP(uColor)) == 1) { d1 = 16 * g_iAhead[uColor]; ASSERT((d1 * 2) == (32 * g_iAhead[uColor])); if (bb[uColor]) { while(IS_ON_BOARD(c = CoorFromBitBoardRank8ToRank1( &(bb[uColor])))) { // If the enemy king can take this passer and is // on the move, all bets are off! cKing = KingPosition(pos, FLIP(uColor)); ASSERT(IS_KING(pos->rgSquare[cKing].pPiece)); ASSERT(GET_COLOR(pos->rgSquare[cKing].pPiece) != uColor); if (pos->uToMove == FLIP(uColor) && (DISTANCE(cKing, c) == 1)) { // TODO: something clever here about protected pawns continue; } 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) && IS_EMPTY(pos->rgSquare[c + d1 * 2].pPiece))) { uPawnDist--; } ASSERT((uPawnDist >= 1) && (uPawnDist <= 6)); uKingDist = DISTANCE(cKing, cQueen); if (uKingDist > 0) { uKingDist -= (pos->uToMove == FLIP(uColor)); } ASSERT((uKingDist >= 0) && (uPawnDist <= 7)); if (uPawnDist < uKingDist) { uRacerDist[uColor] = MINU(uRacerDist[uColor], uPawnDist); } cKing = KingPosition(pos, uColor); ASSERT(IS_KING(pos->rgSquare[cKing].pPiece)); ASSERT(GET_COLOR(pos->rgSquare[cKing].pPiece) == uColor); uFriendDist = DISTANCE(cKing, cQueen); if ((uFriendDist <= 1) && (DISTANCE(cKing, c) <= 1)) { if (FILE(c) != FILE(cKing)) { uRacerDist[uColor] = MINU(uRacerDist[uColor], uPawnDist); } else if ((FILE(cKing) != A) && (FILE(cKing) != H)) { 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; // // TODO: winning connected passers // } } } } if ((uRacerDist[WHITE] < uRacerDist[BLACK]) && (FALSE == fDontCountMeOut[BLACK])) { EVAL_TERM(WHITE, PAWN, ILLEGAL_COOR, pos->iScore[WHITE], RACER_WINS_RACE - uRacerDist[WHITE] * 4, "winning racer pawn"); return(TRUE); } else if ((uRacerDist[BLACK] < uRacerDist[WHITE]) && (FALSE == fDontCountMeOut[WHITE])) { EVAL_TERM(BLACK, PAWN, ILLEGAL_COOR, pos->iScore[BLACK], RACER_WINS_RACE - uRacerDist[BLACK] * 4, "winning racer pawn"); return(TRUE); } return(FALSE); } static void _ReEvalPassers(IN OUT POSITION *pos, IN PAWN_HASH_ENTRY *pHash) /** Routine description: Parameters: POSITION *pos, PAWN_HASH_ENTRY *pHash, Return value: void **/ { ULONG u; ULONG uColor; COOR c; COOR cSquare; BITBOARD bb; PIECE p; SCORE i; ULONG uNumPassers[2] = { 0, 0 }; int d1; ASSERT(pHash->bbPasserLocations[WHITE] | pHash->bbPasserLocations[BLACK]); FOREACH_COLOR(uColor) { bb = pHash->bbPasserLocations[uColor]; if (bb != 0) { uNumPassers[uColor] = CountBits(bb); ASSERT(uNumPassers[uColor] > 0); d1 = 16 * g_iAhead[uColor]; ASSERT((d1 * 2) == (32 * g_iAhead[uColor])); while(IS_ON_BOARD(c = CoorFromBitBoardRank8ToRank1(&bb))) { // // Consider the control of the square in front of the passer // cSquare = c + d1; ASSERT(IS_ON_BOARD(cSquare)); u = _WhoControlsSquareFast(pos, cSquare); if (u == FLIP(uColor)) { EVAL_TERM(uColor, PAWN, c, pos->iScore[uColor], -((PASSER_BY_RANK[uColor][RANK(c)] / 4) + (PASSER_BY_RANK[uColor][RANK(c)] / 16)), "enemy controls sq ahead"); } else if (u == (ULONG)-1) { p = pos->rgSquare[cSquare].pPiece; if (!IS_EMPTY(p) && (OPPOSITE_COLORS(p, uColor))) { ASSERT(GET_COLOR(p) != uColor); EVAL_TERM(uColor, PAWN, c, pos->iScore[uColor], -(PASSER_BY_RANK[uColor][RANK(c)] / 4), "enemy occupies sq ahead"); } } #ifdef DEBUG else { ASSERT(u == uColor); } #endif // // Consider distance from friend king / enemy king // i = (8 - DISTANCE(c, KingPosition(pos, uColor))); if (((uColor == WHITE) && ((KingPosition(pos, BLACK) >> 4) <= (c >> 4))) || ((uColor == BLACK) && ((KingPosition(pos, WHITE) >> 4) >= (c >> 4)))) { // // The enemy king is ahead of the passer // i += FILE_DISTANCE(c, KingPosition(pos, FLIP(uColor))) * 4; } else { // // The enemy king is behind the passer // i += 10 + DISTANCE(c, KingPosition(pos, FLIP(uColor))) * 4; } i *= PASSER_MATERIAL_UP_SCALER[pos->uArmyScaler[FLIP(uColor)]]; i /= 8; EVAL_TERM(uColor, PAWN, c, pos->iScore[uColor], i, "passer distance to kings"); // // If a side is a piece down then the other side's passers // are even more dangerous. // if (TotalNonPawnCount(pos, uColor) > TotalNonPawnCount(pos, FLIP(uColor))) { i = (PASSER_BY_RANK[uColor][RANK(c)] / 2); EVAL_TERM(uColor, PAWN, c, pos->iScore[uColor], i, "passer and piece up"); } // // TODO: For connected passers vs KR, consult the oracle // } } } // // Game stage bonus // u = pos->uArmyScaler[BLACK]; EVAL_TERM(WHITE, PAWN, ILLEGAL_COOR, pos->iScore[WHITE], uNumPassers[WHITE] * PASSER_BONUS_AS_MATERIAL_COMES_OFF[u], "passer value material"); // ASSERT(PASSER_BONUS_AS_MATERIAL_COMES_OFF[u] >= 0); u = pos->uArmyScaler[WHITE]; EVAL_TERM(BLACK, PAWN, ILLEGAL_COOR, pos->iScore[BLACK], uNumPassers[BLACK] * PASSER_BONUS_AS_MATERIAL_COMES_OFF[u], "passer value material"); // ASSERT(PASSER_BONUS_AS_MATERIAL_COMES_OFF[u] >= 0); } static void _EvalBadTrades(IN OUT POSITION *pos) /** Routine description: Parameters: POSITION *pos, Return value: void **/ { ASSERT(pos->uNonPawnMaterial[WHITE] != pos->uNonPawnMaterial[BLACK]); ULONG uAhead = (pos->uNonPawnMaterial[WHITE] > pos->uNonPawnMaterial[BLACK]); ULONG uBehind = FLIP(uAhead); #ifdef DEBUG if (pos->uNonPawnMaterial[WHITE] > pos->uNonPawnMaterial[BLACK]) { ASSERT(uAhead == WHITE); ASSERT(uBehind == BLACK); } else { ASSERT(pos->uNonPawnMaterial[BLACK] > pos->uNonPawnMaterial[WHITE]); ASSERT(uAhead == BLACK); ASSERT(uBehind == WHITE); } #endif // // Encourage the side that is ahead in piece material to continue // trading pieces and not to trade pawns. This also has the // effect of making the side that is behind in material want to // trade pawns and not pieces. This also gives the side that is // ahead a bit of a "bad trade" bonus. Note that the bonus is not // given if the side "ahead" has no pawns; this is so that the // engine will not like positions like KNB vs KRP - the side with // the pawn has the winning chances. // ULONG uAheadPawnCount = FastCountBits(pos->bbPawns[uAhead]); ULONG uInverseBehindPieceCount = 9 - MINU(9, TotalNonPawnCount(pos, uBehind)); EVAL_TERM(uAhead, 0, ILLEGAL_COOR, pos->iScore[uAhead], (uInverseBehindPieceCount * 16) * (uAheadPawnCount != 0), "trade pieces"); // Zero pawns left for the ahead side is the classic false-positive // (KNB vs KRP, minor-up-no-pawns endings that are often drawn or // even lost) -- the old DONT_TRADE_PAWNS table existed specifically // to punish it (-43/-10 depending on lead size) rather than just // trailing off toward a small positive number. Keep that guard as // a cheap branch rather than a lookup table; the positive slope // for 1+ pawns doesn't need the same care. EVAL_TERM(uAhead, 0, ILLEGAL_COOR, pos->iScore[uAhead], (0 == uAheadPawnCount) ? -30 : (SCORE)(uAheadPawnCount * 2), "don't trade pawns"); } static void _EvalLookForDanger(IN OUT SEARCHER_THREAD_CONTEXT *ctx) /** Routine description: This routine looks for squares on the board where the side to move has a piece that is en prise. So far in eval we will have tagged pieces in situations where they are attacked by lesser-valued enemy pieces as "in danger". However, because of the order in which the attack table was generated, we will have missed situations where a piece is undefended (or underdefended) and attacked by an enemy piece that is the same (or greater) value. Note: This routine only considers side to move. Also Note: this routine does not find pawns that are en prise, only pieces. Parameters: SEARCHER_THREAD_CONTEXT *ctx Return value: void **/ { COOR c; BITBOARD bbNonKingPieces; POSITION *pos = &ctx->sPosition; ULONG uSide = pos->uToMove; #ifdef DEBUG PIECE p; #endif bbNonKingPieces = NON_KING_PIECES_BB(pos, uSide); while (IS_ON_BOARD(c = CoorFromBitBoardRank8ToRank1(&bbNonKingPieces))) { #ifdef DEBUG ASSERT(IS_ON_BOARD(c)); p = pos->rgSquare[c].pPiece; ASSERT(p); ASSERT(GET_COLOR(p) == uSide); ASSERT(!IS_PAWN(p)); #endif if (_WhoControlsSquareFast(pos, c) == FLIP(uSide)) { RecordEnprisePiece(ctx, c); } } } static void _EvalTrappedPieces(IN OUT SEARCHER_THREAD_CONTEXT *ctx) /** Routine description: Parameters: SEARCHER_THREAD_CONTEXT *ctx Return value: static void **/ { COOR c; COOR cBestOwnTrapped; ULONG uBestOwnTrappedValue; ULONG uColor; ULONG u; POSITION *pos = &ctx->sPosition; PIECE p; FOREACH_COLOR(uColor) { cBestOwnTrapped = ILLEGAL_COOR; uBestOwnTrappedValue = 0; for (u = 0; u < pos->uNumTrapped[uColor]; u++) { c = pos->cTrapped[uColor][u]; ASSERT(IS_ON_BOARD(c)); if (_WhoControlsSquareFast(pos, c) == FLIP(uColor)) { // 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); #endif // See if the side who created the trap and controls // the square the trapped piece is sitting on has the // move too. If so, uColor moved at ply-1. if (OPPOSITE_COLORS(uColor, pos->uToMove)) { if (ctx->uPly > 0) { RecordEnprisePieceAtPly(ctx, ctx->uPly - 1, c); } EVAL_TERM(uColor, p, c, pos->iScore[uColor], ENPRISE_AND_TRAPPED_PENALTY, "en prise and trapped"); } // ctx->cTrapped[uPly] (RecordTrappedPiece's target) // is a single slot, not a list -- if more than one // of our own pieces is genuinely trapped this ply, // only report the most valuable one. else { if (PIECE_VALUE(pos->rgSquare[c].pPiece) > uBestOwnTrappedValue) { uBestOwnTrappedValue = PIECE_VALUE(pos->rgSquare[c].pPiece); cBestOwnTrapped = c; } } } } // uColor has the move but we found at least one piece that // seems to have no safe place to move and is actively under // attack now. It may not be lost, at least uColor has the // move. But this is still a bad thing. if (IS_ON_BOARD(cBestOwnTrapped)) { RecordTrappedPiece(ctx, cBestOwnTrapped); p = pos->rgSquare[cBestOwnTrapped].pPiece; EVAL_TERM(uColor, p, cBestOwnTrapped, pos->iScore[uColor], TRAPPED_WITH_MOVE_PENALTY, "trapped piece, our move"); } } } static void _EvalBishopPairs(IN POSITION *pos) /*++ Routine description: Give a flat bonus to sides that have a viable bishop pair (one light-squared, one dark-squared) -- not scaled by pawn count, by direct instruction (2026-09-06). Parameters: IN POSITION *pos - position Return value: static void --*/ { ULONG uColor; FOREACH_COLOR(uColor) { ULONG uBishopCount = FastCountBits(pos->bbPieces[uColor][BISHOP]); ASSERT(uBishopCount <= 10); ULONG uWhiteSqBishopCount = WhiteSqBishopCount(pos, uColor); ASSERT(uWhiteSqBishopCount <= 10); FLAG fPair = ((uBishopCount > 1) & (uWhiteSqBishopCount != 0) & (uWhiteSqBishopCount != uBishopCount)); EVAL_TERM(uColor, 0, ILLEGAL_COOR, pos->iScore[uColor], fPair * BISHOP_PAIR_BONUS, "bishop pair"); } } static FLAG _SideHasWinningChances(IN POSITION *pos, IN ULONG uSide) /** Routine description: Cheap, material-count-only classifier (the same idea as Crafty's EvaluateWinningChances): can uSide possibly force a win from this material alone, ignoring the actual position entirely? Used only to soften (scale toward drawscore) an otherwise-misleading raw material lead in Eval() below -- unlike recogn.c's interior-node recognizers (RECOGN_EXACT/UPPER/LOWER), this is never a hard claim fed into a search bound, so a wrong answer here just biases the eval a little; it can't corrupt alpha-beta the way a wrong hard recognizer bound can (see recogn.c's _RecognizeKNKP/_RecognizeKBNK incident). Deliberately conservative: only covers material shapes where "no, this can't be forced" is basic, textbook chess knowledge (insufficient mating material, up the exchange with nothing else changed, bare knights vs a bare king), not trickier exceptions (fortress draws, wrong-bishop-pawn-with-king-in-time geometry) that need real board information -- those are recogn.c's hard-recognizer territory (already exact for tiny material there) or plain search's job, not this cheap pre-check's. Parameters: POSITION *pos, ULONG uSide Return value: static FLAG : TRUE if uSide has any winning chances at all, FALSE if this exact material can provably never be forced to a win regardless of position. **/ { ULONG uEnemy = FLIP(uSide); INT iMajorDiff; INT iMinorDiff; ULONG uSideKnights; ULONG uSideBishops; // // A pawn always gives some winning chances (it can always try to // queen with support) -- nothing below applies. // if (FastCountBits(pos->bbPawns[uSide]) > 0) { 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 == (uSideKnights + uSideBishops))) { return(FALSE); } // // No pawns and up exactly the exchange (one extra rook/queen, // balanced by one extra enemy minor elsewhere): not enough to // force a win either -- e.g. KRB vs KR can be held by the // defender with correct play. // iMajorDiff = (INT)(FastCountBits(pos->bbPieces[uSide][ROOK]) + 2 * FastCountBits(pos->bbPieces[uSide][QUEEN])) - (INT)(FastCountBits(pos->bbPieces[uEnemy][ROOK]) + 2 * FastCountBits(pos->bbPieces[uEnemy][QUEEN])); if ((1 == iMajorDiff) || (-1 == iMajorDiff)) { iMinorDiff = (INT)(FastCountBits(pos->bbPieces[uEnemy][KNIGHT]) + FastCountBits(pos->bbPieces[uEnemy][BISHOP])) - (INT)(uSideKnights + uSideBishops); if (iMajorDiff == iMinorDiff) { return(FALSE); } } // // No pawns, exactly two bare knights (no bishops/rooks/queens) for // uSide, and the enemy has nothing left at all: two knights can't // force mate against a bare king. Two bishops CAN (excluded here // by requiring BISHOP count == 0) and knight+bishop CAN too (also // excluded, since that has one bishop, not zero) -- both are // genuine, if sometimes technique-heavy, forced wins. // if ((2 == uSideKnights) && (0 == uSideBishops) && (0 == FastCountBits(pos->bbPieces[uSide][ROOK])) && (0 == FastCountBits(pos->bbPieces[uSide][QUEEN])) && (1 == TotalNonPawnCount(pos, uEnemy)) && (0 == FastCountBits(pos->bbPawns[uEnemy]))) { return(FALSE); } return(TRUE); } void InitEval(void) { ROOK_FULL_HALF_OPEN_BONUS[0][0] = ROOK_ON_FULL_OPEN; ROOK_FULL_HALF_OPEN_BONUS[0][1] = ROOK_ON_HALF_OPEN_WITH_ENEMY; ROOK_FULL_HALF_OPEN_BONUS[1][0] = ROOK_ON_HALF_OPEN_WITH_FRIEND; ROOK_FULL_HALF_OPEN_BONUS[1][1] = 0; } // 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, 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 -- // same idiom as the 50-move dampening just below (and the same // signed-arithmetic care: g_iDrawScore is currently always 0, but // write this relative to it rather than assuming that, matching // the 50-move code's own convention). /16 (not a full collapse to // drawscore) deliberately mirrors Crafty's EvaluateDraws -- e.g. // KRB vs KR is still theoretically losable by the run-of-the-mill // defender with the checks _SideHasWinningChances gates on, so // some residual signal survives. if ((*piScoreForSideToMove > g_iDrawScore[uColor]) && (FALSE == pFacts->fSideToMoveHasWinningChances)) { *piScoreForSideToMove = g_iDrawScore[uColor] + ((*piScoreForSideToMove - g_iDrawScore[uColor]) / 16); } else if ((*piScoreForSideToMove < g_iDrawScore[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 (pFacts->fBOOC) { *piScoreForSideToMove = g_iDrawScore[uColor] + ((*piScoreForSideToMove - g_iDrawScore[uColor]) / 4); } // Drive the score towards draw as we approach a 50 move w/o // progress draw. if (pos->uFifty > 84) { ULONG uDrawDist = 101 - pos->uFifty; ASSERT(uDrawDist > 0); // uDrawDist is ULONG -- multiplying a negative SCORE by it // directly promotes the SCORE to unsigned first (usual // arithmetic conversions, same rank), wrapping a negative // *piScoreForSideToMove into a huge positive garbage value // instead of scaling it down. Cast uDrawDist to SCORE so the // multiply happens in signed arithmetic; its range (1-16) is // always representable. *piScoreForSideToMove = g_iDrawScore[uColor] + (*piScoreForSideToMove * (SCORE)uDrawDist / 16); } } #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: ... \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, IN SCORE iBeta, OUT SCORE (*piPositional)[2]) /** Routine description: Parameters: SEARCHER_THREAD_CONTEXT *ctx, SCORE iAlpha, SCORE iBeta, SCORE (*piPositional)[2] : if non-NULL, filled in with an estimate of the non-material component of the score for each side, indexed by absolute color (WHITE/BLACK) -- exact if a full eval ran, a cheap estimate otherwise. On a lazy exit this is always >= 0 (an optimistic margin); on a full eval it can be negative (a side whose positional terms net worse than its raw material), floored at -FUTILITY_BASE_MARGIN_FULL/2 so it can only shrink a caller's margin so far. Callers must treat it as an estimate either way; it's only ever used to size a pruning margin, never as a hard fact. Return value: SCORE **/ { POSITION *pos = &(ctx->sPosition); SCORE iScoreForSideToMove; SCORE iAlphaMargin, iBetaMargin; PAWN_HASH_ENTRY *pHash; COOR c; ULONG u; ULONG uColor = pos->uToMove; 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 #ifdef CALIBRATE_POSITIONAL g_uCalibrateEvalCalls++; #endif #ifdef CALIBRATE_MARGIN_SAFETY // Set when a real exit would have been taken; the rest of Eval() // still runs (for measurement), but the saved values get restored // just before returning so real search behavior is bit-for-bit // unaffected -- see the swing recorded near `end:` below. 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)); ASSERT(iAlpha < iBeta); ASSERT((pos->iMaterialBalance[WHITE] * -1) == pos->iMaterialBalance[BLACK]); #ifdef EVAL_TIME UINT64 u64MaterialSetupTimer = SystemReadTimeStampCounter(); #endif pos->iScore[uColor] = (PawnMaterial(pos, uColor) + pos->uNonPawnMaterial[uColor]); pos->iScore[xColor] = (PawnMaterial(pos, xColor) + pos->uNonPawnMaterial[xColor]); #ifdef EVAL_DUMP EvalTraceClear(); 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 // point below, 2026-09-08 -- their only inputs, uNonPawnMaterial, // are already available this early, and the super-lazy margin // table needs a material bucket before it can run). We skip lazy // eval if we're too late into an endgame. pos->uArmyScaler[BLACK] = pos->uNonPawnMaterial[BLACK] - VALUE_KING; pos->uArmyScaler[WHITE] = pos->uNonPawnMaterial[WHITE] - VALUE_KING; pos->uArmyScaler[BLACK] /= VALUE_PAWN; pos->uArmyScaler[WHITE] /= VALUE_PAWN; ASSERT(!(pos->uArmyScaler[BLACK] & 0x80000000)); ASSERT(!(pos->uArmyScaler[WHITE] & 0x80000000)); pos->uArmyScaler[BLACK] = MINU(31, pos->uArmyScaler[BLACK]); pos->uArmyScaler[WHITE] = MINU(31, pos->uArmyScaler[WHITE]); ASSERT(pos->uArmyScaler[BLACK] >= 0); ASSERT(pos->uArmyScaler[BLACK] <= 31); ASSERT(pos->uArmyScaler[WHITE] >= 0); ASSERT(pos->uArmyScaler[WHITE] <= 31); #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalMaterialSetup += (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 // 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); // 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)); // 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) { // 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 ctx->sCounters.tree.u64CyclesEvalSuperLazy += (SystemReadTimeStampCounter() - uSuperLazyTimer); #endif #ifdef CALIBRATE_MARGIN_SAFETY // Don't exit yet -- let the real full eval run below so we // can measure the true swing against SUPER_LAZY_MARGIN, // then restore this exact (zeroed) piPositional right // before `end:` so the caller sees what a normal build // would have returned. fWouldHaveExitedSuperLazy = TRUE; iSavedSuperLazyScore = iScoreForSideToMove; #else #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalPreLazy += (SystemReadTimeStampCounter() - uTimer); ctx->sCounters.tree.u64CyclesSuperLazyExit += (SystemReadTimeStampCounter() - uTimer); ctx->sCounters.tree.u64CyclesInEval += (SystemReadTimeStampCounter() - uTimer); #endif goto end; #endif } 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) { // 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 ctx->sCounters.tree.u64CyclesEvalSuperLazy += (SystemReadTimeStampCounter() - uSuperLazyTimer); #endif #ifdef CALIBRATE_MARGIN_SAFETY fWouldHaveExitedSuperLazy = TRUE; iSavedSuperLazyScore = iScoreForSideToMove; #else #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalPreLazy += (SystemReadTimeStampCounter() - uTimer); ctx->sCounters.tree.u64CyclesSuperLazyExit += (SystemReadTimeStampCounter() - uTimer); ctx->sCounters.tree.u64CyclesInEval += (SystemReadTimeStampCounter() - uTimer); #endif goto end; #endif } #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalSuperLazy += (SystemReadTimeStampCounter() - uSuperLazyTimer); #endif } #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 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 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++; } } #else pHash = _EvalPawns(ctx, &fDeferred); #endif ASSERT(NULL != pHash); ASSERT(IS_VALID_FLAG(fDeferred)); pos->iScore[WHITE] += pHash->iScore[WHITE]; 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\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 if (fBareKingEitherSide) { (void)_EvalPasserRacesAgainstLoneKings(pos, pHash); #ifdef EVAL_DUMP 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. 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\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\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. // 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(); #endif // // Compute an estimate of the score for the side to move based on the // eval terms we have already considered: // // 1. Material balance // 2. Pawn structure bonuses/penalties (incl passers/candidates) // 3. Passer races are detected already // 4. "Bad trade" code has already run // 5. Bishop pairs // 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 (which can be large!) // 3. Other miscellaneous bonuses/penalties // // We build two "margins" to account for these components of the // score. // // 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 #ifdef CALIBRATE_POSITIONAL 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. // #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; #endif 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]++; } #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"). #ifndef CALIBRATE_POSITIONAL if (iScoreForSideToMove + iAlphaMargin <= iAlpha) { INC(ctx->sCounters.tree.u64LazyEvals); g_u64LazyEvalsByBucket[_MaterialBucket(pos)]++; if (NULL != piPositional) { // 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); #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; 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 } else if (iScoreForSideToMove - iBetaMargin >= iBeta) { INC(ctx->sCounters.tree.u64LazyEvals); g_u64LazyEvalsByBucket[_MaterialBucket(pos)]++; if (NULL != piPositional) { // 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); #endif #ifdef CALIBRATE_MARGIN_SAFETY 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); #endif } // lazy eval #endif #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesEvalPreLazy += (SystemReadTimeStampCounter() - uTimer); #endif // 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)); } // Pre-compute some common terms used in per-piece evals: pos->uNumTrapped[BLACK] = pos->uNumTrapped[WHITE] = 0; pos->iReducedMaterialDownScaler[BLACK] = pos->iReducedMaterialDownScaler[WHITE] = 0; 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. // // Knights. bb = pos->bbPieces[uColor][KNIGHT]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalKnight, _EvalKnight(pos, c, pHash)); #ifdef EVAL_DUMP 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 } // Enemy knights. ASSERT(xColor != pos->uToMove); bb = pos->bbPieces[xColor][KNIGHT]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalKnight, _EvalKnight(pos, c, pHash)); #ifdef EVAL_DUMP 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 } // Bishops. bb = pos->bbPieces[uColor][BISHOP]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalBishop, _EvalBishop(pos, c, pHash)); #ifdef EVAL_DUMP 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 } // Enemy bishops. bb = pos->bbPieces[xColor][BISHOP]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); c = BIT_NUMBER_TO_COOR(u); TIMED_EVAL_CALL(ctx, u64CyclesEvalBishop, _EvalBishop(pos, c, pHash)); #ifdef EVAL_DUMP 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 } // Rooks. bb = pos->bbPieces[uColor][ROOK]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); 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\t\t(%d for %s)\n", CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK], (pos->iScore[uColor] - pos->iScore[xColor]), COLOR_NAME(uColor) ); #endif } // Enemy rooks. bb = pos->bbPieces[xColor][ROOK]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); 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\t\t(%d for %s)\n", CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK], (pos->iScore[uColor] - pos->iScore[xColor]), COLOR_NAME(uColor) ); #endif } // Our queen(s). bb = pos->bbPieces[uColor][QUEEN]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); 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\t\t(%d for %s)\n", CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK], (pos->iScore[uColor] - pos->iScore[xColor]), COLOR_NAME(uColor) ); #endif } // Enemy queen(s). bb = pos->bbPieces[xColor][QUEEN]; while (bb) { u = FastFirstBit(bb) - 1; bb &= (bb - 1); 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\t\t(%d for %s)\n", CoorToString(c), pos->iScore[WHITE], pos->iScore[BLACK], (pos->iScore[uColor] - pos->iScore[xColor]), COLOR_NAME(uColor) ); #endif } // Evaluate the two kings last. Compute both sides' non-pawn count // 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); c = KingPosition(pos, uColor); #ifdef DEBUG ASSERT(IS_ON_BOARD(c)); { PIECE pDebugKing = pos->rgSquare[c].pPiece; ASSERT(IS_VALID_PIECE(pDebugKing)); ASSERT(GET_COLOR(pDebugKing) == uColor); ASSERT(IS_KING(pDebugKing)); } #endif 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\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); #ifdef DEBUG ASSERT(IS_ON_BOARD(c)); { PIECE pDebugKing = pos->rgSquare[c].pPiece; ASSERT(IS_VALID_PIECE(pDebugKing)); ASSERT(GET_COLOR(pDebugKing) == xColor); ASSERT(IS_KING(pDebugKing)); } #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\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 // passed pawns identified by the pawn eval routine again. Also // see if the side not on move has a trapped piece. #ifdef EVAL_TIME UINT64 u64PostLazyMiscTimer = SystemReadTimeStampCounter(); #endif bb = (pHash->bbPasserLocations[WHITE] | pHash->bbPasserLocations[BLACK]); if (0 != bb) { _ReEvalPassers(pos, pHash); #ifdef EVAL_DUMP 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); // 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); 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); 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\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[uColor] - pos->iScore[xColor]); #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]; // 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 // negative -- a side whose positional terms (king safety, pawn // structure, hanging pieces, ...) net worse than its raw material // gets a genuinely negative value here. search.c's futility // margin (FUTILITY_BASE_MARGIN_BY_SOURCE[side's tier] + // piPositional[side]) deliberately lets that shrink the margin -- // a positionally-bad side is less likely to be saved by an // unexamined quiet move -- but floor it so one bad eval term // can't collapse the margin arbitrarily far; this only ever runs // on the full-eval exit, so -FUTILITY_BASE_MARGIN_FULL/2 (not the // lazy/super-lazy tiers) caps the damage at half of that tier's // base. if (NULL != piPositional) { (*piPositional)[WHITE] = MAX(pos->iScore[WHITE] - (SCORE)(PawnMaterial(pos, WHITE) + pos->uNonPawnMaterial[WHITE]), -(FUTILITY_BASE_MARGIN_FULL / 2)); (*piPositional)[BLACK] = MAX(pos->iScore[BLACK] - (SCORE)(PawnMaterial(pos, BLACK) + pos->uNonPawnMaterial[BLACK]), -(FUTILITY_BASE_MARGIN_FULL / 2)); ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_FULL; } g_Options.iLastEvalScore = iScoreForSideToMove; #ifdef CALIBRATE_MARGIN_SAFETY if (TRUE == fWouldHaveExitedSuperLazy) { // // Real (non-calibration) code checks super-lazy first and // returns immediately on a hit -- so if this call would *also* // have satisfied the regular lazy condition below, super-lazy // is still what a normal build actually returns. Record/ // restore this one, not the regular-lazy save in the other // branch. // // Direction matters: for an alpha-side exit (saved score below // alpha), only an *upward* real swing is dangerous -- it's the // one that could pull the score back over alpha. A downward // swing just makes the verdict even more clearly correct. Same // in reverse for a beta-side exit. abs() was conflating both // directions, so "exceeded" was really an upper bound on real // unsoundness, not a measurement of it. RecordSuperLazyMarginSafetySwing( pos, (iSavedSuperLazyScore < iAlpha) ? MAX0(iScoreForSideToMove - iSavedSuperLazyScore) : MAX0(iSavedSuperLazyScore - iScoreForSideToMove), iSavedSuperLazyMarginUsed); iScoreForSideToMove = iSavedSuperLazyScore; if (NULL != piPositional) { 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 // -- fix it back up to match the restored (super-lazy) // values. ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_SUPERLAZY; } } else if (TRUE == fWouldHaveExited) { // // The real swing: how far the true, full-eval score actually // ended up from the lazy-exit score this node would have // returned. RecordMarginSafetySwing buckets this by material; // its max (not just p90) is the number that answers "how // large would LAZY_EVAL_BASE_MARGIN have had to be before an // exit here would have been unsound." // // Same directional fix as the super-lazy branch above. RecordMarginSafetySwing( pos, (iSavedLazyScore < iAlpha) ? MAX0(iScoreForSideToMove - iSavedLazyScore) : MAX0(iSavedLazyScore - iScoreForSideToMove), iSavedLazyPositional); // // Restore exactly what a normal build would have returned -- // this measurement must not change real search behavior. // iScoreForSideToMove = iSavedLazyScore; if (NULL != piPositional) { (*piPositional)[WHITE] = iSavedLazyPositionalWhite; (*piPositional)[BLACK] = iSavedLazyPositionalBlack; ctx->sPlyInfo[ctx->uPly].uLastPositionalSource = EVAL_POSITIONAL_SOURCE_LAZY; } } #endif #ifdef EVAL_TIME ctx->sCounters.tree.u64CyclesFullEvalExit += (SystemReadTimeStampCounter() - uTimer); ctx->sCounters.tree.u64CyclesInEval += (SystemReadTimeStampCounter() - uTimer); #endif INC(ctx->sCounters.tree.u64FullEvals); g_u64FullEvalsByBucket[_MaterialBucket(pos)]++; end: ASSERT(IS_VALID_SCORE(iScoreForSideToMove)); ASSERT((iScoreForSideToMove > -NMATE) && (iScoreForSideToMove < +NMATE)); ASSERT(abs(ctx->sPlyInfo[ctx->uPly].iKingScore[BLACK]) < 700); ASSERT(abs(ctx->sPlyInfo[ctx->uPly].iKingScore[WHITE]) < 700); return(iScoreForSideToMove); }