Skip to content

Square root register order (ZSqrt)

Symptom

In several functions the original computes a vector length like this:

mula.s  $f2, $f2
madd.s  $f12, $f13, $f13
sqrt.s  $f0, $f12
swc1    $f0, 0x1c($s0)
abs.s   $f1, $f0
mov.s   $f12, $f0
...
div.s   $f13, $f13, $f12
div.s   $f12, $f2, $f12

The sum is built in $f12, the root lands in $f0, the store and the abs.s read $f0, and a mov.s $f12,$f0 copies the root back into $f12 for the two divisions before atan2f. Every source form written against the old ZSqrt gave the reverse: the sum in $f0, sqrt.s $f12,$f0, and no copy. This blocked ZHitman3ControlConsole::Update (best score 54), ZHitman3ControlMouse::Update (63) and ZHitman3ControlConsoleStrafe::Update (not attempted) in zhitman3movementcontrol.cpp.

Diagnosis

The old helper was:

inline float ZSqrt(float value) {
    float root;
    __asm__ __volatile__("...sqrt.s %0,%1..." : "=&f"(root) : "f"(value));
    return root;
}

With two pseudos (value and root), the only way to get the original's two simultaneous copies of the root ($f0 and $f12) is a source-level copy that the allocator fails to coalesce. No source form produced it.

Use tools/lines.py to read the original source line for each instruction. In ZHitman3ControlConsole::Update:

offset instruction line
+0x54..+0x60 loads, mula.s, madd.s zmath.h 238
+0x64 sqrt.s $f0,$f12 zmath.h 239
+0x70 abs.s $f1,$f0 zmath.h 264
+0x74 mov.s $f12,$f0 zmath.h 239

The copy back into $f12 belongs to line 239, the same line as the sqrt.s, and the lexical block around it holds a single variable, float value in $f12. There is no separate result local in any of the inlined sqrt scopes, here or in the other functions that use the helper (ParametricCurve::CalcLen, PF4::ZDynamicObstacle::FindShortestRouteToTarget). A copy emitted on the asm's own line is what GCC's reload produces for an asm output operand: IOI's zmath.h square root wrote the result back into its own parameter, with an early-clobber output.

__asm__ __volatile__("...sqrt.s %0,%1..." : "=&f"(value) : "f"(value));
return value;

Input and output are then the same pseudo, and the early clobber forbids the asm from using one register for both. Reload keeps value in its allocated register ($f12) for the input, writes the output into a reload register ($f0), and emits value = $f0 after the asm. Uses of the result that come before $f0 is clobbered are rewritten to read $f0 (the store, the abs.s). The divisions sit in an out-of-line block after $f0 has been reloaded with the dead-zone constant, so they read value in $f12. The global-alloc dump confirms it (compile_unit.py --dump g, insn 116):

Reload 0: reload_out (SF) = (reg/v:SF 44 $f12 [168])
    FP_REGS, RELOAD_OTHER (opnum = 0)
    reload_reg_rtx: (reg:SF 32 $f0)

Where the result has no later use in the original value register, the copy is dead and deleted, which gives the plain madd.s $f1 ...; sqrt.s $f0,$f1 shape seen elsewhere.

Change

src/engine/system/sqrt.h now writes the root back into value:

inline float ZSqrt(float value) {
#if defined(OPENBLOOD_HOST_TEST)
    return __builtin_sqrtf(value);
#else
    __asm__ __volatile__(".set push\n\t.set noreorder\n\tsqrt.s %0,%1\n\t.set pop" : "=&f"(value) : "f"(value));
    return value;
#endif
}

ZAbs needed no change. Its debug scope already shows a separate fAbs local, as the current helper has.

Variants tried against Console::Update, CalcLen and FindShortestRouteToTarget together (score 0 is exact):

helper Console::Update CalcLen FindShortest
old (root, =&f, volatile) 54 0 0
in place, =&f, volatile 17 76 43
in place, =&f, not volatile 53 219 291
in place, =f 53 77 87
root, =f 54 77 86
root, =&f, not volatile 62 219 283

The remaining 17 in Console::Update came from the speed clamp and a store order, not from the sqrt block (below). With the new helper, two previously exact functions broke at call sites of the form x += ZSqrt(...) and (a + ZSqrt(...)), where the sum and root registers came out swapped. Assigning the root to a local that already existed, then using the local, matches the original in both:

  • PF4::ZDynamicObstacle::FindShortestRouteToTarget (pf4runtime.cpp): the two closing LengthLeft += ZSqrt(...) and LengthRight += ZSqrt(...) became segmentLength = ZSqrt(...); LengthLeft += segmentLength;.
  • ParametricCurve::CalcLen (parametriccurve.cpp): rewritten with the debug record's names. fa and fb are function-scope locals, fb is assigned in the loop and again for the final segment, fa = fb carries it.

No other matched function changed.

Other changes in zhitman3movementcontrol.cpp

ZHitman3ControlConsole::Update also needed the speed clamp written the way the debug record shows: an inline ZHitman3ControlBase::SetMovementSpeedPrc(float fVal) (zhitman3movementcontrol.h line 139 in the original) that forwards to ZHM3MovementControl::SetMovementSpeedPrc(float fPrc) (line 90), which clamps its own parameter before storing it:

void SetMovementSpeedPrc(float fPrc) {
    fPrc = fPrc >= 0.0f ? ZMIN(fPrc, 1.0f) : 0.0f;
    m_fMovementSpeedPrc = fPrc;
}

Assigning the ternary straight to the member leaves the result in the zero register; clamping the parameter first gives the original's separate result register (mov.s $f2,$f0 in the branch delay slot, then min.s $f2,$f1,$f3). The same setter fixed the clamp in ZHitman3ControlConsoleStrafe::UpdateTurn, whose last difference was a literal: the original's 2.356194f (0x4016cbe2), not 2.3561945f. The ConsoleStrafe code also needed friend class ZHitman3ControlConsoleStrafe; in CConfiguration, as the other control classes already have.

Functions recovered

New exact C++ bodies:

  • ZHitman3ControlConsole::Update (0x00364c98, 280 bytes)
  • ZHitman3ControlMouse::Update (0x00363e74, 368 bytes)
  • ZHitman3ControlConsoleStrafe::Update (0x00364644, 640 bytes)
  • ZHitman3ControlConsoleStrafe::UpdateTurn (0x003648c4, 232 bytes, draft score 39)

Existing drafts that the new helper alone made exact (scores with the old helper in parentheses):

  • ZHumanState::Reset (0x0022f4f0, animation.cpp, 73)
  • ZHumanBoid::MoveTracker (0x0022b354, human.cpp, 20)
  • Locomotion::ZTransition::Init (0x001a7e84, zactor.cpp, 29). tools/tests/zactor_behavior.cpp stubbed Init for the host build; it now stubs addSubTransition, which Init calls and which is still assembly on the host.

ZHitman3ControlBase::SetTurn (0x003631b8) was already exact as a draft before this change; the recorded score of 55 was stale. It is promoted here too.