Skip to content

AICState: four functions from gynt's drafts that stop short of 100%, with what is left on each - #424

Draft
kotijekriv wants to merge 8 commits into
sourcehold:mainfrom
kotijekriv:reimpl/AICState-development
Draft

kotijekriv wants to merge 8 commits into
sourcehold:mainfrom
kotijekriv:reimpl/AICState-development

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Draft: four AI::AICState functions, taken as far as I can take them. Each one says below what is left, and three of them come with a question.

All four build on gynt's drafts from reimpl/AICState. Each file's history on this branch is his commit first, unchanged and authored by him, and ours on top. The status lines record reccmp's raw numbers. No header changes.

#418 holds the four AICState functions from the same drafts that reached 100%; this draft does not depend on it.

Function Address reccmp raw Normalised Status line gynt's draft
AICState::aiCreateSiegeUnits 0x004D2000 99.7 95.5 99.7 99.69 (31aa939)
AICState::aiShouldAttackOrWaitForTeamCoordination 0x004CDCF0 99.0 95.2 99.0 83.25 (f4967de)
AICState::updateAIStrengthState 0x004D3320 97.2 83.3 97.2 97.2 (49ee095)
AICState::sellExcessGoods 0x004D1D60 66.9 66.9 66.9 62.41 (124830b)

Measured on 305c962, this branch's head, in a real build of the four files. The bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie, none of the four on the /GL list. A full all-on build of this head, with both resolver sets on: 0 failed, average 99.56 over 2,443 functions, and no function on main moves. openshc-pr-check gives 0 ERROR. Not run in game.

What is left on each

aiCreateSiegeUnits, 99.7: the source is right, and the match depends on the translation unit's symbol count.
The whole difference is the order of three reloads at the inner loop's latch: the original loads eax, esi, ecx (0x004D23AF, 0x004D23B3, 0x004D23B7), ours eax, ecx, esi. With 30 dummy typedef short lines after the last #include and nothing else changed, the same file reads 100.0 raw and normalised. Over all 64 possible shifts the function compiles to two code streams: shifts 20 to 31 and 36 to 44 give the original's code, the other 43 (shift 0 among them) give the committed one. This is the effect gynt describes on #366 ("The TU symbol count decides some register choices"). As that note asks, nothing is padded here: the next header edit above the function moves it again. The source is gynt's; on top of it are a countof bound with ++siegeIndex and a one-line comment on the while loop.

aiShouldAttackOrWaitForTeamCoordination, 99.0: the operand order of one compare.
0x004CDD74 is cmp ebp, dword ptr [esi + level] in the original and cmp dword ptr [esi + level], ebp in ours, where ebp is the zero that readyTeamMembers starts with. Same length, same jump, and it is the only instruction that differs: 99 instructions on both sides, every jump on the same instruction. Our compiler folds the test to "level against 0" and then puts the memory operand first. Spellings that all give our order: level != 0, 0 != level, the plain truth value, readyTeamMembers != level and the reverse, unreadyTeamMembers != level, the sum of both counts, the counts declared in the other order or set by a chained assignment, and short, char and unsigned counts.
What took it from 83.2 to 99.0: the tests nest so that the returns are shared (one return TRUE serves "no coordination", "enough ready members" and "out of patience"). With an early return TRUE the constant 1 gets a fourth use before the loop, the compiler keeps it in a register, and the loop counter's mov edi, 1 moves up, which was gynt's remark on his draft ("Pools 1 in a register"). The two counts are declared above the force test, and otherPlayerID in the loop's header.

updateAIStrengthState, 97.2: one variable's register.
The 326 instructions of both sides are the same instructions in the same order with the same memory operands and jump targets. They differ only in the register of one variable, the recruit choice between the random draw and its store: ebp in the original, ebx in ours, on nine lines (0x004D34BB to 0x004D3524). The source is gynt's file; on top of it, unused using lines and includes are gone, aiType is tested against AITA_NULL, and resourcesToAcquireArray is indexed with RT_WOOD. The local _aiRecruitUnitChoice keeps gynt's name; it is the variable whose register differs.

sellExcessGoods, 66.9: two pairs of callee-saved registers, swapped through the whole function.
The 132 instructions of both sides are the same instructions in the same order with the same memory operands, the stack slots included, and the jump targets are the same. ebp and ebx are swapped, and so are edi and esi: the player's count (later the flat offset) is in ebp in the original and ebx in ours, the player's offset (later the pointer into the sell list) in edi and esi, the AIC's address in ebx and ebp, the variance in esi and edi. Against gynt's draft (62.4) one change moved the code: the player's product is written inside the offset's expression instead of in a local before the loop, which removes the one hunk that was more than register names. Tried for the registers on the draft's form, all the same or worse: the sum's operands the other way round, the offset before the variance, the variance before the resource type, the base before aicIndex, a signed product, the nervous count in a local, the call's amount read through the offset, and plain array indexing.

Two questions

Question 1: do updateAIStrengthState and sellExcessGoods count as finished under #414?
Both differ from the original in register names only, as described above. #414 isn't settled: gynt set the rule, and TheRedDaemon wrote there that he'd accept such functions for now as non-100%. So both status lines record the raw numbers (97.2 and 66.9). If the answer is yes, they can move to a ready PR as they are.

Question 2: how should aiCreateSiegeUnits be recorded?
It matches for 21 of the 64 possible symbol-count shifts and not for the one this file has today. The status line says "matches modulo TU symbol count" at 99.7. If the note on #366 lands and such functions count as matched, it can move to a ready PR as it is.

The checker's 15 CHECKs

Count Where Finding Why it stays
4 the branch gynt's newest commit on the file is not cited the checker finds older local copies of his commits, from before this branch was rewritten in review and recut onto main; each message cites the commit on this branch that carries his file, and its upstream hash
2 aiCreateSiegeUnits :61 while loop, and the style bot's finding for it with the step in a for header the function reads 0.696 in quick_diff (gynt's measurement); the comment in the code says so
1 the branch the style bot will post that one finding as above
3 aiCreateSiegeUnits :171, :233, updateAIStrengthState :103 cast on a global :171: the field is a 32-bit MappersEnum and the original reads only its low 16 bits (movzx at 0x004D23EC). :103: (int)mapTimeInTicks < 4800 is the original's signed compare of an unsigned field. :233: PathConnectionLayer is already short, so this cast and the one at :228 are redundant; gynt's lines, left as they are.
2 sellExcessGoods :34, updateAIStrengthState :98 declared without a value amount and _aiRecruitUnitChoice are assigned in the if/else arms that follow, so there is no single first assignment to declare them at
1 updateAIStrengthState :31 comparison with TRUE the original compares with 1; a comment in the code says so
2 updateAIStrengthState :52, :60 step by one written += 1 gynt's lines, left as they are; main has both forms

Other PRs

  • gynt's reimpl/AICState holds all four files. Once these land, his branch can take main's versions.

🤖 Generated with Claude Code

gynt and others added 8 commits November 18, 2026 16:25
AICState::updateAIStrengthState as gynt has it on upstream's reimpl/AICState (5e32940), unchanged: his 06102f2
and 49ee095 (2026-11-07, his newest commit on the file), with his status line. The next commit records what the
remaining difference is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…bp in the original, ebx in ours

AICState::updateAIStrengthState, gynt's version from the commit before this one (368a5f0: his 49ee095 on upstream's
reimpl/AICState, unchanged). reccmp reads 97.2, as for his file: the 326 instructions of both sides are the same
instructions in the same order with the same memory operands and jump targets, and they differ only in the
register of one variable, the recruit choice between the random draw and its store: ebp in the original, ebx in
ours, on nine lines (0x004D34BB to 0x004D3524). It is recorded at reccmp's number, not as 100: whether a
difference in register names alone counts as a match (sourcehold#414) is not settled.

quick_diff reads 0.9724 with five hunks, all of them register names only.

On top of gynt's file, measured the same: the seven using lines that nothing uses and the two includes that
nothing uses (AINervousDestroyBuildingCategoryInt.hpp, BOOLEnum.hpp) are gone, aiType is tested against
AITA_NULL, resourcesToAcquireArray is indexed with RT_WOOD, and three comments say why a line has its shape (the
low byte of the preference, the comparison with TRUE).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::aiCreateSiegeUnits as gynt has it on upstream's reimpl/AICState (5e32940), unchanged: his 31aa939
(2026-11-03, 99.69%), d76377d and his style commit 5e32940 (2026-11-07, his newest on the file), with his
status line. The next commit records what the remaining difference is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ount

AICState::aiCreateSiegeUnits, gynt's version from the commit before this one (da9605a: his 31aa939 at 99.69%
on upstream's reimpl/AICState, with d76377d and 5e32940). The source is his and it is right: the function
matches modulo the translation unit's symbol count.

reccmp reads 99.7 (normalized 95.5) as committed, and 100.0 (100.0) with 30 dummy `typedef short` lines after the
last #include and nothing else changed; both numbers are from builds of this commit's file. Over the 64 possible
shifts (0 to 63 such lines; quick_diff, one compile of this commit's file per shift) the function compiles to two
code streams: shifts 20 to 31 and 36 to 44 give the original's code, the other 43, shift 0 among them, give the
committed one. On gynt's file, before the countof bound and ++siegeIndex went in, the same two streams sit six
shifts higher (26 to 37 and 42 to 50). The whole difference is the order of three reloads at the inner loop's
latch: the original loads eax, esi, ecx (0x004D23AF, 0x004D23B3, 0x004D23B7), the other stream eax, ecx, esi.

This is the effect gynt describes on sourcehold#366 in tools/compiler-internals/msvc-backend.md ("The TU symbol count
decides some register choices"): c2 files address constants into 64 buckets by a symbol number counted over the
whole unit, and passes that walk the candidates in that order (here probably the one that places the reloads) come
out differently when a header above the function gains or loses a declaration. As that note asks, nothing is padded
here and the count is not steered: the next header edit above the function moves it again. The note is not on
main yet, so the function is recorded at the 99.7 that reccmp reads there, not as 100.

On top of gynt's file: the outer loop's bound is countof(SiegeEngineMetaInfoArray) with ++siegeIndex, which leaves
both streams as they are and only moves the shifts at which each appears, as said above; and the comment on the
while loop is one line without the scores (with the step in a for header the function reads 0.696 in quick_diff,
his measurement).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::sellExcessGoods as gynt has it on upstream's reimpl/AICState (5e32940), unchanged: his 124830b
(2026-11-03, 62.41%) and his style commit 5e32940 (2026-11-07, his newest on the file), with his status line.
The next commit records what the remaining difference is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…bx and edi/esi swapped

AICState::sellExcessGoods, from gynt's draft: c2cffa4, the commit before this one, is his file from upstream's
reimpl/AICState (124830b at 62.41%, "register permutation"). The function's structure, the flat offset for the
two resource arrays and everything else are his.

Only register names differ: reccmp reads 66.9, and the 132 instructions of both sides are the same instructions in
the same order with the same memory operands, the stack slots included. What differs is two pairs of callee-saved
registers, each swapped through the whole function (ebp with ebx, edi with esi): the player's int count, later
the flat offset, is in ebp in the original and in ebx in ours; the player's offset, later the pointer into the
sell list, in edi and esi; the AIC's address in ebx and ebp; the variance in esi and edi. The four pushes and
pops stand at the same places under those names, and the jump targets are the same. It is recorded at reccmp's
number, not as 100: whether a difference in register names alone counts as a match (sourcehold#414) is not settled.

What changed against the draft: the player's product is written inside the offset's expression instead of in a
local declared before the loop. With the local, the loop head stores the resource type and forms the offset in the
resource type's register, in another order than the original (one hunk of the diff is more than register names:
quick_diff 0.6515); written inline, the offset is formed in the product's register as in the original and only
register names are left (0.6667).

Tried for the registers, on the draft's form (quick_diff): the sum's operands the other way round, the offset
before the variance, the variance before the resource type, the base before aicIndex, a signed product (all
0.6515), the nervous count in a local (0.6107, 130 instructions), the call's amount read through the offset
(0.6336, 130) and plain array indexing (0.6054, 129).

Style, measured the same: the loop's bound is countof(AIResourceTradeAICMapping), the steps are ++i and ++j, the two
quarterings are amount /= 4, the six using lines and three includes that nothing uses are gone, and the comment on
the flat offset says what the original does without the scores. gynt's draft reads 62.4 in reccmp.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::aiShouldAttackOrWaitForTeamCoordination as gynt has it on upstream's reimpl/AICState (5e32940),
unchanged: his f4967de (2026-11-03, 83.25%) and his style commit 5e32940 (his newest on the file), with his
status line. The next commit works on it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…re (cmp ebp, [level])

AICState::aiShouldAttackOrWaitForTeamCoordination, from gynt's draft: 5452f1b, the commit before this one, is his
file from upstream's reimpl/AICState (f4967de at 83.25%, "Pools 1 in a register"). reccmp reads 99.0
(normalized 95.2) where the draft reads 83.2 (78.1); quick_diff 0.9899, 99 instructions on both sides, every jump
lands on the same instruction as in the original, and one instruction differs. Not 100.

What changed:

- The tests nest so that the returns are shared. The coordination level test wraps the loop and the patience
  test, and the force test wraps that: one `return TRUE` serves "no coordination", "enough ready members" and
  "out of patience", and one `return FALSE` serves the force test. With an early `return TRUE` at the level test
  the constant 1 is used four times before the loop (the requestStateUnk compare, two returns and the loop
  counter's start), the compiler keeps it in edi from the requestStateUnk compare on, and the counter's
  `mov edi, 1` moves up there: gynt's remark. With three uses it stays an immediate. (The allocator's probe shows
  the four-use piece with a benefit of 1, the lowest that still gets a register.)
- The two counts are declared above the force test, where the original stores unreadyTeamMembers (0x004CDD6C).
- otherPlayerID is declared in the loop's header. The draft declared it at the top of the function, which kept
  the pooled 1 in place there.

What is left: 0x004CDD74 is `cmp ebp, dword ptr [esi + level]` in the original and
`cmp dword ptr [esi + level], ebp` in ours; ebp is the zero that readyTeamMembers starts with. Same length, same
jump. The compiler folds the test to "level against 0" and then puts the memory operand first; the original
compared a register value that was still a variable when the compare was built. Spellings that all give our
order: level != 0, 0 != level, plain truth value, readyTeamMembers != level and the reverse,
unreadyTeamMembers != level, the sum of both counts, the counts declared in the other order or set by a chained
assignment, short, char and unsigned counts. (readyTeamMembers < level gives a different jump, jle.)

Measured (quick_diff): the draft 0.8900; the nested form with an early return for the level test 0.8293 (106
instructions); with the counts declared at the top of the function 0.7839; as committed 0.9899.

Besides that: the loop's bound is countof(playerTeams) and the increments are prefix ones. Neither changes the
code.

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

1 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 4 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
9 Prefer for over while/do 1
Where

Rule 9 — Prefer for over while/do

  • src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp:61 — while-loop; the guide asks for a for

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants