Skip to content

ZBBox reset and expand in pf4runtime.cpp

Symptom

PF4RunTime::ZData::CloseLinks (ob_0016ff38, 660 bytes) and OpenLinks (ob_001701cc, 656 bytes) could not be recovered. src/engine/pathfinder4/bbox.h declared ZBBox as two float[3] arrays with reset() and expand(const float *) written as for (i != 3) loops. Those loops compile rolled, and the best body scored 785. The original is fully unrolled. It builds the box through ZVector3f temporaries copied with lw/sw pairs, and it adds a margin taken from the literal pool (.rodata_00589e68, .rodata_00589e6c). Earlier workers concluded that bbox.h needed a different API.

Two other functions in the unit were also blocked: PF4RunTime::ZData::PushOutOfObstacles (ob_00173104, best 44, frame 0x110 against 0x100) and PF4RunTime::ZGraph::GetClosestComponent (ob_0016c9e0, best 36).

Diagnosis

The debug record in analysis/recovered-types.h declares ZBBox as ZVector3f m_Min; ZVector3f m_Max; with a default constructor, reset(), expand(const ZVector3f &), Inside(const float *), Inside(const float *, float) and InsideXZ(float, float). tools/debug_context.py ob_001701cc shows the function starting in STLport's _limits.h line 488 (numeric_limits<float>::max()), with code alternating between bbox.h and ZVector3.h. The locals are pLink, count, hits, pos, bbox and iDestNode, all at function scope.

Reading the reference instructions gives four min/max triples:

  • m_Min = min(m_Min, bboxMin - m)
  • m_Max = max(m_Max, bboxMin + m)
  • m_Min = min(m_Min, bboxMax - m)
  • m_Max = max(m_Max, bboxMax + m)

Here m = (1, 100, 1). Each group is written to a stack temporary and copied into the box word by word. The function does not have eight triples, which rules out four calls to a plain point expand, and only the four expected stores reach memory. ZVector3f(bboxMax) is stored to the stack and never read, so it is an argument bound to a const ZVector3f &. The margin is never stored, so it is not a ZVector3f passed by reference. The source is two calls, bbox.expand(ZVector3f(bboxMin)) and bbox.expand(ZVector3f(bboxMax)), with the margin applied inside expand.

The reset constant is the word 0x7fffffff, the largest R5900 single-precision value (the EE FPU has no infinities). 6.80564694e+38f compiles to that word. The _limits.h line record shows it was written as std::numeric_limits<float>::max().

GCC 3.2 allocates named locals as temp slots at the point of declaration. The original's two 16-byte temp slots and the position of pos (sp+0x0 in OpenLinks, sp+0x20 in CloseLinks) therefore follow the declaration order the stabs record: pos before bbox in OpenLinks, after it in CloseLinks.

ZVector3::operator+ and operator- must stay out of line. FindNodes, MapInside, FindWalls and GetClosestComponent call the copies at 0x438c74 and 0x438cb4. Defining them in the class body broke FindNodes, and expand does not need them.

For PushOutOfObstacles the debug record shows an inline function at ZVector2.h line 41 with a single const float fS parameter that calls the out-of-line ZVector2::operator*(float). That is a scalar-first operator*(fS, v), and its by-value return under -fno-elide-constructors is the extra copy the earlier drafts were missing. ZVector2.h line 54 is ptr(). The obstacle position copy is the zmath.h v3copy with tmp0/tmp1/tmp2 locals (zmath.h lines 558-563). engine/pathfinder4/bsptree/zbsptree.h line 19 is not a helper function. GCC synthesized ZVector2's implicit operator= at that line, and the same line covers the plain p = p1 copies in GetClosestComponent.

Change

  • src/engine/pathfinder4/bbox.h: ZBBox now holds ZVector3f m_Min, m_Max. It has a constructor that calls reset(). reset() assigns ZVector3f(max, max, max) and its negation. expand(const ZVector3f &v) assigns ZVector3f(ZMIN(v.x - 1.0f, m_Min.x), ...) and ZVector3f(ZMAX(v.x + 1.0f, m_Max.x), ...) with the 1/100/1 margin. Inside(const float *) reads .x/.y/.z. Inside(const float *, float) and InsideXZ(float, float) were file-static helpers in pf4runtime.cpp and are now members, as the debug record has them.
  • src/libraries/STLport-4.6.1/stlport/stl/_limits.h: std::numeric_limits<float>::max() returning 6.80564694e+38f.
  • src/common/basic/zvector3.h: inline ZVector3(const T *) constructor. ZVector3 keeps the implicit copy assignment, which produces the original's lw/sw box copies.
  • src/common/basic/zvector2.h: ptr() and the scalar-first operator*(const T fS, const ZVector2<T> &v).
  • src/engine/pathfinder4/pf4runtime.cpp: CloseLinks, OpenLinks and PushOutOfObstacles use the shared v3copy inline from zmath.h.

Using .x/.y/.z in Inside matters. m_Min[0] through an operator[] folds the member offset into each load, but the original keeps &m_BBox in a register, and with that form GetGraphAndComponent and GetClosestGraphAndComponent no longer match.

Functions now matching

  • ob_0016ff38 PF4RunTime::ZData::CloseLinks
  • ob_001701cc PF4RunTime::ZData::OpenLinks
  • ob_00173104 PF4RunTime::ZData::PushOutOfObstacles

This change brought the unit from 51 to 54 matching functions at the time.