Skip to content

Reimplement 9 Map::Entities functions at 100% from #527 (one with visibility units), plus 1 with register allocation differences - #556

Open
gynt wants to merge 22 commits into
sourcehold:mainfrom
gynt:reimpl/pr527-matched
Open

gynt wants to merge 22 commits into
sourcehold:mainfrom
gynt:reimpl/pr527-matched

Conversation

@gynt

@gynt gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

This PR takes 10 functions of draft #527 (reimpl/entities-projectiles-fire):

  • the 9 at 100%, two of them counted as 100% by main's convention, and IgniteFireAtMiniTile at 100% only with OPENSHC_FUNCTION_VISIBILITY_UNITS on (94.2% in the default build);
  • UpdateFlag_1_2_4_Entity, where only register allocation differs (see below).

The other 7 functions of #527 stay in the draft.

Measured on origin/main (46f21c5), and again after merging main 592a17d with the same results:

  • Branch reimpl/pr527-matched was cut from that commit and the files were taken from the draft's head (daf0c69).
  • I built the DLL with these files in the local build list. It also held the already-100% callees they reach (activateProjectileEntity, deleteEntity, getFireEntityIDAtTile, markEntityDestroyed, ...), which are measuring-only and not part of this PR.
  • Both resolver sets were on: the struct resolvers and the function resolvers of that list. Then I ran reccmp.
  • I measured once before the style pass and once after it, and got the same numbers.
Function Address Raw % Normalized % Status remark
Entities::UpdateEntity_CowFlying 0x00402180 100.0 100.0 Reimplemented
EntityState::angleToRotationFrameIndex 0x00402BD0 100.0 100.0 Reimplemented
EntityState::calculateEntityDrawOffset 0x004036F0 100.0 100.0 Reimplemented
EntityState::initializeProjectileVelocities 0x00403A20 100.0 100.0 Reimplemented
EntityState::handleProjectileWallBounce 0x00403FF0 100.0 100.0 Reimplemented
Entities::IgniteFireAtMiniTile 0x004052E0 94.2 (100.0 with visibility units) 90.0 (100.0 with visibility units) 94.2% | 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on
EntityState::spawnProjectileImpactDebris 0x00405DA0 100.0 100.0 Reimplemented
EntityState::updateProjectileHeightAndCollision 0x004081E0 100.0 100.0 Reimplemented
EntityState::updateEntities 0x004087C0 100.0 100.0 Reimplemented
Entities::UpdateFlag_1_2_4_Entity 0x00402900 68.2 68.2 Register allocation differs

Since main's reccmp/float-literals.csv, handleProjectileWallBounce and initializeProjectileVelocities score a plain 100.0.

UpdateFlag_1_2_4_Entity is register allocation only. The original keeps the constant 0 in edi:

  • it compares against edi, stores di for 0, and builds 3 as lea edx, [edi + 3];
  • so it pushes and pops edi.

Ours uses immediates and test reg, reg, and pops esi earlier. Block order, stores and computation are identical, so nothing differs in behaviour.

The other 7 functions of the draft fail the register-blind check. Each has at least one non-allocation difference:

  • moveProjectileEntity: an extra jmp over loop-alignment padding, and different x87 instruction forms.
  • spawnProjectileEntity: field store order, a jmp, and an indirect call.
  • UpdateFireEntity: different tail-merge points.
  • processSeaGulls: a duplicated ret, CSE'd loads, and movzx vs mov.
  • processEntityHitBuildingOrUnit: branch layout, add -10 vs sub 10, and different addressing modes.
  • computeLineOfSightDistance: missing branches, and lea vs sub.
  • math_atan_1: constant folding and x87 instruction selection.

Style pass, following REIMPLEMENTATION_STYLE_GUIDE.md on main:

  • The class's own .func.hpp is the first include. Unneeded Map.func.hpp includes and the unused using declarations are dropped.
  • Enums instead of numbers:
    • Enums::NM_* (NeighbourMask) for the wall-neighbour masks in handleProjectileWallBounce and spawnProjectileImpactDebris.
    • Enums::MO_* in calculateEntityDrawOffset.
    • LogicHelpers::L_* for the non-flammable tile mask and L_TREE in IgniteFireAtMiniTile.
    • ET_UNKNOWN_30 and EntityTypeInt__ET_EXPLOSION.
  • Decimal literals for orientations, offsets and entity types without a name, with a comment where no enum exists.
  • Named parameters instead of param_N, plus ++ / -- / += / *= self-operations.
  • Pre-review pass:
    • Redundant casts dropped, and MC_MAP_XY_LIMIT(_INCLUSIVE) used for the map bounds.
    • The non-flammable Logic1 mask is written inline instead of as a local enum.
    • The PathAxisCase enum (src/OpenSHC/Map/Entities/Enums/PathAxisCase.hpp) is used for pathAxisCase. It is on main now through 0701799.
  • Forms kept because the match needs them (the pointer decrement in UpdateEntity_CowFlying, the gotos) are explained in the PR thread, not in code comments.
  • I ran clang-format 22.1.5 on all 10 files.

Header changes: none. PathAxisCase.hpp and PathAxisCaseShort.hpp are on main now through 0701799 ("upstream ghidra changes"), and the branch merges it:

  • PathAxisCase.hpp is identical.
  • For PathAxisCaseShort.hpp the branch takes main's version, whose typedef is int. No file uses it.

Merge of main 0701799:

  • I rebuilt with all 10 files and the callees above, with both resolver sets on. OPENSHC_FUNCTION_VISIBILITY_UNITS was on, so that the function-visibility.txt entry takes effect.
  • Every function keeps its score.
  • With the option off, IgniteFireAtMiniTile measures 94.2 / 90.0, both before and after the merge. Without the wrapper unit, the original's reload of BuildingLayer[tile] after getFireEntityIDAtTile becomes a stack spill.
  • So its status line follows main's convention for such functions: 94.2% | 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on. With the option off, every other function measures the same as with it on.

Build entries:

  • cmake/compiler-flags-gl.txt: angleToRotationFrameIndex.cpp, updateProjectileHeightAndCollision.cpp, IgniteFireAtMiniTile.cpp.
  • cmake/function-visibility.txt: OpenSHC::Map::Entities::IgniteFireAtMiniTile -> EntityState::getFireEntityIDAtTile.

All three entries are the same as in the draft.

Overlap with open PRs:

There is one commit per function. Each sets the function's line in status/addresses-SHC-3BB0A8C1.txt. There is also one merge of main 592a17d. One more commit adds the build entries.

Draft #527 stays open for the remaining 7 functions:

  • computeLineOfSightDistance
  • processSeaGulls
  • processEntityHitBuildingOrUnit
  • UpdateFireEntity
  • moveProjectileEntity
  • math_atan_1
  • spawnProjectileEntity

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 10 commits December 4, 2026 15:39
…functions

angleToRotationFrameIndex and updateProjectileHeightAndCollision are built with /GL so their callers
see their register use, and IgniteFireAtMiniTile is built with /GL with getFireEntityIDAtTile made
visible to it. Taken from draft sourcehold#527.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x00402180 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x00402BD0 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x004036F0 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…00.0%

From draft sourcehold#527. Measured 98.5% raw / 94.4% normalized on main 46f21c5; the only differences are the float constant labels and the _CIcos/_CIsin call targets.

Status: SHC_3BB0A8C1_0x00403A20 | 100.0% | Reimplemented (counted: only float constant names and the CRT math helpers (_CIcos, _CIsin) differ)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 99.7% raw / 94.9% normalized on main 46f21c5; the only differences are the float constant labels and the _CIcos/_CIsin call targets.

Status: SHC_3BB0A8C1_0x00403FF0 | 100.0% | Reimplemented (counted: only float constant names and the CRT math helpers (_CIcos, _CIsin) differ)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x004052E0 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x00405DA0 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…on 100.0%

From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x004081E0 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 100.0% raw on main 46f21c5.

Status: SHC_3BB0A8C1_0x004087C0 | 100.0% | Reimplemented

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Match regression check

1 of the 10 status entries behind the files this PR changes are below the best match already known on HEAD or on a fork's branch:

file id here best known where known remark
src/OpenSHC/Map/Entities/IgniteFireAtMiniTile.cpp SHC_3BB0A8C1_0x004052E0 94.2% (100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on) 100.0% origin/reimpl/entities-projectiles-fire Reimplemented

Percentages are read from the status/addresses-SHC-*.txt files as written; nothing is rebuilt here. If a figure above is stale, update the status file.

gynt and others added 2 commits December 4, 2026 16:27
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
From draft sourcehold#527. Measured 68.2% raw / 68.2% normalized on main 592a17d.

The original keeps 0 in edi: it compares against edi instead of 0, stores di for 0, builds 3 as
lea edx, [edi + 3], and pushes/pops edi. Ours uses immediates and test reg, reg, and pops esi before
the jump table. Block order, stores and computation are the same, so nothing differs in behaviour.

Status: SHC_3BB0A8C1_0x00402900 | 68.2% | Register allocation differs

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt gynt changed the title Reimplement 9 Map::Entities functions at 100% from #527 Reimplement 9 Map::Entities functions at 100% from #527, plus 1 with register allocation differences Dec 4, 2026
gynt and others added 2 commits December 4, 2026 17:01
Names the five values of Entity::pathAxisCase, the axis a projectile's Bresenham walk steps along:
none, vertical (dX == 0), horizontal (dY == 0), Y-major and X-major. Used by
initializeProjectileVelocities and handleProjectileWallBounce. PathAxisCaseShort is the sized alias
for the short field; the field itself keeps its generated type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
- PathAxisCase names for pathAxisCase, MC_MAP_XY_LIMIT(_INCLUSIVE) for the map bounds, and the
  Logic1 flags written inline instead of a local enum in IgniteFireAtMiniTile.
- Map::Enums:: qualified now that Map::Entities::Enums exists.
- Dropped redundant casts: whole-expression (short) on stores to short fields, (short)/(double) on
  call arguments, (byte) before & masks, the function-pointer cast on EntityCallbacks, and the
  char-pointer read of the random number's second byte (now >> 8).
- short instead of word for graphicRotationUnk, fireParameter instead of two.
- Removed code comments that explained match-driven forms (moved to the PR thread) and comments
  that restated the code.

All 10 functions measure as before: 9 at 100% (two counted), UpdateFlag_1_2_4_Entity 68.2%.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026
@gynt

gynt commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

These forms are kept because the match needs them. I measured each one with a full build and reccmp, with both resolver sets on.

UpdateEntity_CowFlying

  • short* const counter = &...someCounter_OR_hitGround; (*counter)--; if (*counter <= 0) stays as a pointer local.
  • Decrementing and testing the field directly drops the function from 100% to 86.4%.

calculateEntityDrawOffset

  • The two gotos (rotate, lookup) stay. The original emits the rotate block and the lookup once and jumps into them from the east, west and "any other orientation" paths.
  • I tried three goto-free forms. quick_diff ratio against the current 0.9796:
    • an if/else-if chain with each arm doing its own shifts: 0.6195, 64 instructions instead of 49;
    • flips first, then one if (north || south) for the shifts: 0.7547;
    • a switch with fall-throughs: 0.6170.

handleProjectileWallBounce, spawnProjectileImpactDebris

  • goto straight; stays. When both neighbouring walls are set, each of the 12 diagonal orientation cases jumps into the one shared "keep the target" block. The original has that block once.

Not changed, because a header would have to change (main's generated headers lead):

  • updateProjectileHeightAndCollision(uint, undefined4 heightOffset, int angle, int skipCollision): heightOffset looks like a plain int and skipCollision like a BOOLEnum.

  • Entity types without a name in EntityType:

    • 5 and 6 (the effect entities spawned in updateEntities);
    • 27 (impact debris in spawnProjectileImpactDebris).

    They are written as (EntityType)n until they get upstream names. 26 and 30 already exist, as EntityTypeInt__ET_EXPLOSION and ET_UNKNOWN_30.

  • Entity::pathAxisCase is a plain short. This PR adds Map/Entities/Enums/PathAxisCase.hpp (+Short), which the field could use upstream.

  • Entity::logicalState values (7 = decaying, ...) and orientation 60..75 stay as decimals, as in the sibling Update*Entity functions on main. There is no enum for them yet.

…7-matched

PathAxisCaseShort.hpp is taken from main (main's typedef is int, size 4); nothing uses it. compiler-flags-gl.txt keeps main's UpdateSlinger line and this PR's IgniteFireAtMiniTile line.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026
…BILITY_UNITS on

The default build (visibility units off) gives 94.2 / 90.0: without the wrapper unit the original's reload of BuildingLayer[tile] after getFireEntityIDAtTile becomes a stack spill. With OPENSHC_FUNCTION_VISIBILITY_UNITS on it is 100.0 / 100.0. Same convention as main's other visibility-unit functions. No other function of this PR changes with the option.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt gynt changed the title Reimplement 9 Map::Entities functions at 100% from #527, plus 1 with register allocation differences Reimplement 9 Map::Entities functions at 100% from #527 (one with visibility units), plus 1 with register allocation differences Dec 4, 2026
gynt and others added 3 commits December 4, 2026 22:30
…flow

Flip the sub-tile coordinates in an if/else-if chain, then pick the
shift pair with one test for the south and north orientations. Still
100.0% raw and normalized.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The float constants and CRT helpers now pair through
reccmp/float-literals.csv, so the counted remark is no longer needed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt

gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Goto pass. calculateEntityDrawOffset is now goto-free and still at 100.0 / 100.0 (4666424). The other two functions keep their goto straight form, because no goto-free form I found keeps the match.

The goto straight pattern. In both functions, each of the 12 diagonal cases has an inner switch (wallFlags & mask), and its both-walls case jumps to the shared axis block (cases 60/64/68/72).

  • The original compiles every inner switch to a byte-indexed jump table, and the both-walls entry lands directly on that shared block.
  • The 12 bounce if/else tails are folded into one copy: case 61's copy is kept and the other cases jump back to it.
  • In the goto-free forms, tail merging keeps different copies and places the straight block elsewhere. That is decided after register allocation, so statement order cannot steer it.

Scores are quick_diff ratios (goto form 0.9909 for handleProjectileWallBounce, 0.9919 for spawnProjectileImpactDebris), and reccmp raw / normalized for the best form.

Form tried handleProjectileWallBounce spawnProjectileImpactDebris
bool straight flag, if (!straight) { bounce } else { straight targets } (best) 0.9610; reccmp 93.1 / 79.5 0.9526; reccmp 92.9 / 80.3
same with an int flag, or the flag declared at the top or just before the switch 0.9610 -
flag, if (straight) { straight targets } else if / else bounce 0.9569 0.9469
flag, if (straight) { straight targets; break; }, then the bounce 0.9569 -
flag and the axis case moved after case 75 (three variants) 0.9471-0.9489 -
flag only, straight targets assigned once after the switch (if (straight)) 0.9489 0.9368
flag checked inside the bounce: else if (straight) / else if (!straight) 0.6734-0.7189 -
duplicated tail: the bounce copied into each inner case, the straight targets into the both-walls case 0.5563 -
hoisted condition if (x == y && (wallFlags & m) == m) { straight targets; break; } before the inner switch 0.6564 -

Both functions stay at 100.0 / 100.0 with the gotos. handleProjectileWallBounce now needs no counted remark either, since main's reccmp/float-literals.csv.

Systematic flag pass.

  • Every form below replaces the goto with takeAxisPath = TRUE in the both-walls arm.
  • quick_diff baselines with the goto: 0.9909 and 0.9919.
  • Two forms were also measured with reccmp (raw / normalized).
Dimension Values tried Effect
Flag type BOOLEnum, BOOL, int, bool, char, uchar none: identical code. The flag is threaded away completely.
Declaration function top, just before the switch, inside each case scope none
Initialisation at declaration; = FALSE at the start of each case none
Initialisation = FALSE in every non-jumping arm and on the x != y path worse (0.9154-0.9228)
Test, axis first if (flag) {axis} else {bounce} 0.9569 / 0.9469
Test, bounce first if (!flag) {bounce} else {axis}, or inverted sense (takeBouncePath) 0.9610 / 0.9526; reccmp 93.1 / 79.5 and 92.9 / 80.3
Test merged into an existing condition if (!flag && x > y) … else if (flag) … 0.9235 / 0.9034
Test merged into an existing condition if (x > y) … else if (flag) … 0.7457 / 0.6955
Early exit with the flag if (flag) { axis; break; } before the bounce 0.9569 / 0.9469
Axis block after the switch if (!flag) bounce in each case, one if (flag) {axis} after the switch; axis cases set the flag or keep their own block 0.9489 / 0.9368
Loop exit with the flag do { switch … } while (0); arm sets the flag and continues; if (flag) {axis} after 0.9489 / 0.9368
Loop exit, duplicated tail, no flag do { switch … } while (0); arm assigns the axis targets and continues 0.9569 / 0.9469
Inner arm order (jump arm first, middle, last) all six orders jump arm first or middle is best: the loop-exit, duplicated-tail form reaches 0.9634 / 0.9556, same instruction count as the original; reccmp 93.4 / 77.4 and 93.0 / 77.4
Outer case order axis case last, cases reversed, cases reversed with axis first worse (0.64-0.95)
quick_reorder declaration block of the best form no move improves it

Why none of them match. Every goto-free form compiles to the same instructions as the goto form; only the block layout differs.

  • The original keeps case 61's copy of the bounce tail inline and the other cases jump back to it. The straight block sits at the end, beside the merged else arm.
  • The flag forms come out mirrored: the last case's copy survives, and the straight block lands right after the outer jump table.
  • That choice is made in the post-allocation tail merge, which follows predecessor order, so the source cannot steer it. The gotos stay.

Casts kept after the cast pass (c5794c6; every other cast was removed with identical code and warnings):

  • updateEntities.cpp:24, (int)DAT_CurrentEntityID::instance < this->maxEntityCount: without it the compare is unsigned (jb instead of the original's jl) and C4018 appears. The header declares the global as uint, but the original always compares it signed, so I proposed int for Ghidra (tested locally: all 11 users keep their scores). The cast goes away once the header type is fixed in Ghidra.
  • updateEntities.cpp:223/229 (EntityType)5/(EntityType)6, spawnProjectileImpactDebris.cpp:314 (EntityType)27: needed to compile, because EntityType has no constant for 5, 6 or 27.
  • handleProjectileWallBounce.cpp:372-373 and initializeProjectileVelocities.cpp:168-169, (float) on the vCos/vSin stores: the value is a double, so dropping the cast gives C4244.

gynt and others added 3 commits December 4, 2026 23:06
The 0.75 and 15.0 constants now pair through reccmp/float-literals.csv,
so the counted remark is no longer needed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Each removed cast leaves the generated code and the warnings unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

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.

1 participant