Repository navigation
Reimplement UnitsState::processUnitAttackOtherUnit at 100%, from gynt's text on main - #542
Merged
Merged
Conversation
UnitsState::processUnitAttackOtherUnit from 79.5% / 72.1% (gynt's text, on main since sourcehold#238) to 100%. One `return TRUE` at the end instead of one in every arm (36 on main). With those MSVC keeps the constant 1 in ebx from `dying = 1` on and returns with `mov eax, ebx`; the original has the 1 in ecx for the word stores only (0x005313BC) and returns with `mov eax, 1`. The arms now leave through `break`: the special death sounds and the switch on the attacker's type stand in the `default:` of the switch on the victim's type, and `isStalked == 0` stands around both (an early return for a stalked victim gets an epilogue of its own with `mov eax, ecx`). quick_diff 0.8467 -> 0.9515. The block for a victim that is still alive stands last, as in the original (0x005317E8), with the rest inside `health <= 0`. The spear arm plays its hit sound and otherwise falls through into the pike arm: the original has the two death-spear sounds once (0x005316C4), reached from both arms, and hands out its calls' scratch registers in that order. The fire thrower has an arm of its own in the death-state chain and US_STONE_DEATH_01 stands in the last else, which gives the 32-bit `add edx, 0x71` (0x005314B4) for a 16-bit one. `tunnelerFinishedDigging = 1` stands in front of `_deathVariant` (the order of two stores, 0x005314D0), and the victim's type is read in the damage expression and declared behind it, so that `_unit2Type` takes the dead unitID parameter's stack slot (0x0053126F) and the flag the local one. The same listing as the original (quick_diff 1.0000). The body is gynt's (775d904 56.1%, 017d972 56.8%, 3c8da9c 57.0%, 398ea19 79.5%). Measured with real builds of this text on main 0cea04d (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off and in one with it on (500 functions each). With the option off no other function reads differently from the same build of main. The comment lines and `const` on `_deathVariant` came after those builds: the file compiled alone gives the same object code with them (compared section by section). Tried and not taken, each for the match (quick_diff): one test over the eleven attacker types for the death state 0.8204, a switch with grouped labels 0.8402; `UnitTypeShort const _unit2Type` 0.7453; `_hasSpecialDeathSound` declared in the arm that uses it 0.8958; `_deathVariant` declared at its first use 0.9000. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
gynt
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
UnitsState::processUnitAttackOtherUnit(0x00531220) came to main with #238 at 79.5% / 72.1% (reccmp raw / normalised). The body is gynt's; this PR changes how it is laid out, not what it does. It reads 100.0% / 100.0% withOPENSHC_FUNCTION_VISIBILITY_UNITSoff and on.processUnitAttackOtherUnitreturn TRUEinstead of 36, the "still alive" block last"Before" is main's reading, the same with the option off and on. The status file said 57.0%.
What matched it
return TRUE. Main's text returnsTRUEfrom every arm, 36 times. MSVC then keeps the constant 1 in ebx fromdying = 1on and returns withmov eax, ebx, and it copies the epilogue into more arms (391 instructions against the original's 372). The original has the 1 in ecx for the word stores only (0x005313BC) and returns withmov eax, 1. Here the arms leave throughbreakand the function returnsTRUEonce:default:of the switch on the victim's type;isStalked == 0stands around both switches. An earlyreturn TRUEfor a stalked victim gets an epilogue of its own withmov eax, ecx, where the original jumps to the shared one (0x005314DC).return FALSE) stands last, as in the original (0x005317E8), and the rest is insidehealth <= 0.US_STONE_DEATH_01stands in the lastelse. Main's(type != UT_A_FIRETHROWER) + US_DEATH_03adds in 16 bits; the original adds in 32 (add edx, 0x71, 0x005314B4).tunnelerFinishedDigging = 1stands in front of_deathVariant(the order of two stores, 0x005314D0). The victim's type is read in the damage expression and_unit2Typeis declared behind it, so that_unit2Typetakes the deadunitIDparameter's stack slot (0x0053126F) and the flag the local one.Kept as it is
quick_diff), an earlyreturn TRUEfor a stalked victim reads 0.9854,return TRUEin the shield's two arms 0.9557._hitSoundnamesfixedRng & 7in the default arm, as main'sprocessEntityDamageToUnitdoes. It gives the same code as the expression written out eight times. The original makes eight separate calls, each with its own constant, so it can't be one call withFX_BODY_HIT + _hitSound.quick_diff, aswitchwith grouped labels 0.8402. One comment line above the chain.int const _unit2Typefor a field that is aUnitTypeShort: asUnitTypeShort constit reads 0.7453._hasSpecialDeathSoundis declared next to it, at the top: declared at the top of thedefault:arm, where it is first used, it reads 0.8958. One comment line covers both._deathVariantis read behind thetunnelerFinishedDiggingstore, about 100 lines ahead of its first use: declared at that use it reads 0.9000. One comment line. It isuint constnow, like_hitSound(the same object code)._hasSpecialDeathSoundis a flag and is tested by truth;_deathVariantand_hitSoundare numbers (0 or 1, 0 to 7) and are compared with their values.DAT_UnitsState::instancein a member of UnitsState, twice (the owner for the fear-factor call, the owner in the under-attack test): main's text, and the original reads the owner through the global in both places (0x0053127D, 0x005317F1).this->loses the match (0.7419 and 0.8242). Each has a one-line comment.spriteID == 129,fixedRng & 1,isStalkedcompared with 0 and_tribeIDwithoutconstare main's text.Measured
Setup: 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. The branch is cut from main 0cea04d, and every build is of that main or of this branch./GLfile of main, this function and five moreUnitsStatefunctions with their callers and callee closures, every file of a function-visibility group that touches them): BUILD_OK and 0 failures in both.processUnitAttackOtherUnitreads 100.0% / 100.0% in both.processMeleeInitiationreads 85.9% / 71.9% in all four builds./GLlist and stays there): BUILD_OK, 0 failures, average 99.35. Against the same build of main onlyprocessUnitAttackOtherUnitreads differently, 79.5% / 72.1% -> 100.0% / 100.0%.conston_deathVariant) were checked by object compare, not rebuilt: the file compiled alone gives the same object code as the built text (every section but the debug ones compared byte for byte, with the relocations).Other PRs
#536, #539 and #540 are ours and open. They share no file with this one but the status file, in other lines. No other open PR changes
processUnitAttackOtherUnit.cpp.🤖 Generated with Claude Code