decomp(jpeg): TMCJPEGDEC_err_restart in C (last asm placeholder removed) (worker 25aed309) (#1369)

This commit was merged in pull request #1369.
This commit is contained in:
coah
2026-10-09 14:28:11 -05:00
committed by GitHub
parent 7a44876cbd
commit 04d3d1e87d
3 changed files with 169 additions and 213 deletions

View File

@@ -1,7 +1,7 @@
# Assembly inventory
4.3U, 2026-10-09. 163 functions contain 165 assembly
bodies/blocks: 162 ORIGINAL functions and 1 PLACEHOLDER function.
4.3U, 2026-10-09. 162 functions contain 164 assembly
bodies/blocks: 162 ORIGINAL functions and 0 PLACEHOLDER functions.
Run `python3 tools/check_asm_inventory.py`. Completion requires exit 0, complete
coverage and zero placeholders. The checker also rejects stale/duplicate rows,
@@ -37,7 +37,6 @@ found. Historical logs absent from this branch have commit IDs and excerpts in
| `libs/NW4R/include/nw4r/math/types.h` | `VEC3Scale` | ORIGINAL | psq_l/ps_muls0/psq_st implements paired-single vector/matrix operations. |
| `libs/NW4R/src/math/math_arithmetic.cpp` | `FrSqrt` | ORIGINAL | frsqrte plus fixed refinement matches the public [NW4R arithmetic assembly][arithmetic-cpp]. |
| `libs/NW4R/src/math/math_types.cpp` | `MTX44Identity` | ORIGINAL | psq_st/ps_merge01/ps_merge10 implements paired-single vector/matrix operations. |
| `libs/RVLMiddleware/TMC_JPEG/src/jpegdec/jdec_main.c` | `TMCJPEGDEC_err_restart` | PLACEHOLDER | JPEG marker scan; best C 95.73913%, 114/115 instructions, folded exit branch, [rx19](../tools/decomp-assist/rx19.attempts.md), [rx24][rx24]. |
| `libs/RVL_SDK/include/revolution/os/OSFastCast.h` | `OSInitFastCast` | ORIGINAL | mtspr programs the GQR quantization registers. |
| `libs/RVL_SDK/include/revolution/os/OSFastCast.h` | `__OSu16tof32` | ORIGINAL | psq_l uses a GQR integer conversion format for the fast cast. |
| `libs/RVL_SDK/include/revolution/os/OSFastCast.h` | `__OSf32tou8` | ORIGINAL | psq_st uses a GQR integer conversion format for the fast cast. |
@@ -190,7 +189,6 @@ found. Historical logs absent from this branch have commit IDs and excerpts in
| `src/system/rvl_dec.c` | `Rvl_decode_ash` | ORIGINAL | nofralloc hand-built 0x40 frame saves r21..31 with stmw; work address uses lis @h/ori @l. |
| `src/system/rvl_dec.c` | `Rvl_decode_asr` | ORIGINAL | nofralloc hand-built 0x40 frame saves r21..31 with stmw; work address uses lis @h/ori @l. |
[rx24]: ../tools/decomp-assist/rx36.attempts.md#rx24
[rx36-osinit]: ../tools/decomp-assist/rx36.attempts.md#osinit
[sdk-init]: https://github.com/doldecomp/ogws/blob/master/src/revolution/OS/__ppc_eabi_init.c
[runtime]: https://github.com/doldecomp/ogws/blob/master/src/runtime/runtime.c

View File

@@ -900,237 +900,91 @@ componentFound:
return 0;
}
#ifdef __MWERKS__
asm s32 TMCJPEGDEC_err_restart(register TMCCJPEGDecWork* work) {
nofralloc
static inline s32 restartFromMarker(TMCCJPEGDecWork* work, TMCCJPEGDecState* state, u8 marker) {
u8 skip;
u16 pitch;
u32 pos;
u16 row;
u16 column;
s32 mcuIndex;
u16 y;
u16 x;
s32 result;
stwu r1, -0x20(r1)
mflr r0
stw r0, 0x24(r1)
stw r31, 0x1c(r1)
stw r30, 0x18(r1)
mr r30, r3
stw r29, 0x14(r1)
stw r28, 0x10(r1)
lbz r0, 0x181c(r3)
lwz r31, 0x19e4(r3)
cmplwi r0, 1
bne _err_restart_rewind
li r0, 0
li r3, 0
stw r0, 0x6cc(r31)
b _err_restart_return
_err_restart_rewind:
bl TMCJPEGDEC_rewind_ptr
cmpwi r3, 0
bge _err_restart_load_byte
b _err_restart_return
_err_restart_load_byte:
lwz r3, 0xc(r30)
lbz r0, 0(r3)
stb r0, 8(r1)
b _err_restart_check_byte
_err_restart_read_byte:
mr r4, r30
addi r3, r1, 8
bl TMCJPEGDEC_get_byte
cmpwi r3, 0
bge _err_restart_check_byte
b _err_restart_return
_err_restart_check_byte:
lbz r0, 8(r1)
cmplwi r0, 0xff
bne _err_restart_read_byte
mr r4, r30
addi r3, r1, 8
bl TMCJPEGDEC_get_byte
lbz r6, 8(r1)
cmplwi r6, 0xd9
bne _err_restart_check_result
cmpwi r3, 0
bge _err_restart_eoi
cmpwi r3, -0x90
beq _err_restart_eoi
b _err_restart_return
_err_restart_eoi:
li r3, 0
b _err_restart_return
_err_restart_check_result:
cmpwi r3, 0
bge _err_restart_check_marker
b _err_restart_return
_err_restart_check_marker:
cmplwi r6, 0xd0
blt _err_restart_check_byte
cmplwi r6, 0xd7
ble _err_restart_marker
b _err_restart_check_byte
_err_restart_marker:
lhz r3, 0x52(r30)
addi r0, r6, 8
addi r3, r3, 0xcf
cmpw r6, r3
subf r0, r3, r0
clrlwi r5, r0, 0x18
ble _err_restart_have_diff
subf r0, r3, r6
clrlwi r5, r0, 0x18
_err_restart_have_diff:
lhz r4, 0x181a(r30)
addi r0, r6, 1
clrlwi r3, r0, 0x1d
lhz r7, 0x10(r31)
mullw r5, r5, r4
li r0, 0
lwz r6, 0x54(r30)
sth r3, 0x52(r30)
mr r3, r30
clrlwi r4, r6, 0x18
clrlwi r8, r5, 0x18
srwi r6, r6, 0x10
mullw r5, r4, r7
stw r0, 0x30(r30)
add r4, r8, r6
stw r0, 0x34(r30)
stw r0, 0x38(r30)
add r5, r5, r4
divw r4, r5, r7
stw r0, 0x3c(r30)
sth r0, 0x50(r30)
mullw r0, r4, r7
clrlwi r28, r4, 0x10
subf r4, r0, r5
slwi r0, r4, 0x10
add r0, r0, r28
clrlwi r29, r4, 0x10
stw r0, 0x54(r30)
bl TMCJPEGDEC_init_buff
cmpwi r3, 0
bge _err_restart_store_position
b _err_restart_return
_err_restart_store_position:
sth r29, 0(r31)
mr r3, r30
sth r28, 2(r31)
bl TMCJPEGDEC_get_position
stw r3, 4(r31)
lhz r4, 2(r31)
lhz r3, 0x10(r31)
lwz r0, 0xc(r31)
mullw r3, r4, r3
lhz r4, 0(r31)
subf r0, r3, r0
subf r3, r4, r0
_err_restart_return:
lwz r0, 0x24(r1)
lwz r31, 0x1c(r1)
lwz r30, 0x18(r1)
lwz r29, 0x14(r1)
lwz r28, 0x10(r1)
mtlr r0
addi r1, r1, 0x20
blr
if (marker > work->rstMarkerIdx + 0xCF) {
skip = marker - (work->rstMarkerIdx + 0xCF);
} else {
skip = (marker + 8) - (work->rstMarkerIdx + 0xCF);
}
skip *= work->restartInterval;
pitch = state->maxX;
pos = work->mcuPos;
row = pos & 0xFF;
column = pos >> 16;
mcuIndex = row * pitch + (skip + column);
work->rstMarkerIdx = (marker + 1) & 7;
y = mcuIndex / pitch;
x = mcuIndex % pitch;
work->components.dcPredict[0] = 0;
work->components.dcPredict[1] = 0;
work->components.dcPredict[2] = 0;
work->components.dcPredict[3] = 0;
work->restartCnt = 0;
work->mcuPos = (x << 16) + y;
result = TMCJPEGDEC_init_buff(work);
if (result < 0) {
return result;
}
state->posX = x;
state->posY = y;
state->position = TMCJPEGDEC_get_position(work);
return state->result - state->posY * state->maxX - state->posX;
}
#else
s32 TMCJPEGDEC_err_restart(TMCCJPEGDecWork* work) {
TMCCJPEGDecState* state;
u16 interval;
u16 pitch;
u32 val;
s32 rem;
s32 div;
s32 next;
u8 rstDiff;
TMCCJPEGDecWork* stream;
s32 result;
u8 byte;
s32 r;
u8 marker;
state = work->pState;
if (work->scanCount == 1) {
state->decodeResult = 0;
return 0;
}
r = TMCJPEGDEC_rewind_ptr(work);
if (r < 0) {
return r;
stream = work;
result = TMCJPEGDEC_rewind_ptr(stream);
if (result < 0) {
return result;
}
byte = *work->pBufCur;
while (TRUE) {
byte = *stream->pBufCur;
for (;;) {
while (byte != 0xFF) {
r = TMCJPEGDEC_get_byte(&byte, work);
if (r < 0) {
return r;
result = TMCJPEGDEC_get_byte(&byte, stream);
if (result < 0) {
return result;
}
}
r = TMCJPEGDEC_get_byte(&byte, work);
if (byte == 0xD9) {
if (r < 0 && r != TMCC_ERROR_UNDERFLOW) {
return r;
result = TMCJPEGDEC_get_byte(&byte, stream);
marker = byte;
if (marker == 0xD9) {
if (result < 0 && result != TMCC_ERROR_UNDERFLOW) {
return result;
}
return 0;
}
if (r < 0) {
return r;
if (result < 0) {
return result;
}
if (byte < 0xD0 || byte > 0xD7) {
continue;
if (marker >= 0xD0 && marker <= 0xD7) {
break;
}
if (byte > work->rstMarkerIdx + 0xCF) {
rstDiff = byte - (work->rstMarkerIdx + 0xCF);
} else {
rstDiff = (byte + 0x08) - (work->rstMarkerIdx + 0xCF);
}
work->rstMarkerIdx = (byte + 1) & 7;
interval = work->restartInterval;
pitch = state->maxX;
val = work->mcuPos;
work->components.dcPredict[0] = 0;
work->components.dcPredict[1] = 0;
work->components.dcPredict[2] = 0;
work->components.dcPredict[3] = 0;
work->restartCnt = 0;
next = (u8)(val & 0xFF) * (s32)pitch + (val >> 16) + (s32)(u8)(rstDiff * interval);
div = next / (s32)pitch;
rem = next - div * (s32)pitch;
work->mcuPos = (rem << 16) + (u16)div;
r = TMCJPEGDEC_init_buff(work);
if (r < 0) {
return r;
}
state->posX = rem;
state->posY = div;
state->position = TMCJPEGDEC_get_position(work);
return state->result - (state->posY * state->maxX) - state->posX;
// MWCC needs the stream reset here to keep the retail loop-exit branch layout.
stream = work;
}
return restartFromMarker(work, state, marker);
}
#endif
void TMCJPEGDEC_set_entropytbl(TMCJpegTableInfo* work, s32 idx, u8 data) {
switch (idx) {

View File

@@ -0,0 +1,104 @@
# a2c-jdec2: TMCJPEGDEC_err_restart asm -> C
Branch `agent/w1009/a2c-jdec2`, base `be309e8f7`. Owns `TMCJPEGDEC_err_restart` in
`libs/RVLMiddleware/TMC_JPEG/src/jpegdec/jdec_main.c` and its asm inventory row.
Untracked rx63 files in the worktree are unrelated and left alone.
Read opus-common.md, levers.md (all, esp. 35-38), a2c-jdec, opus-ph, sol-ph2,
jpeg-restart-status-lifetime logs, saved diffs in `_luna-runs/best/`, PR #1355 diff.
Harness: `/tmp/a2c-jdec2/h.py` / `hm.py` splice a candidate body into the unit,
compile with the unit's exact MWCC command (0.07 s per trial), and report
instruction count, positional diffs, register-blind shape diffs, and whether any
of the other 12 functions changed.
## Two separable problems
1. Topology: target has `cmplwi d0; blt top; cmplwi d7; ble +8; b top`. Plain
`continue`/`break`/`goto` forms fold to `bgt top` (114 insns).
2. Finish block registers/schedule (dist/interval/pitch/skip/pos temps).
### Topology findings
- Any front-end IR statement in the out-of-range arm that the backend later
deletes reproduces the target shape. Proof: `#pragma opt_dead_assignments off`
+ dead `result = 0;` in that arm gives 115/115 (28 diffs, all in finish).
The same pragma unit-wide changes parse_sof, scan_varinit, restart_interval,
so the original unit was not compiled that way.
- Front end removes: dead stores to locals, self-referencing dead counters
(`skipCount++`), unreachable-after-loop uses, empty inline calls, `(void)x`.
- Kept and backend-visible: stores (`byte = marker` 116), loads
(`state = work->pState` 116).
- Only a register copy that coalesces works: `scanner = work` (sol-ph2 idea).
Loop-head aliases (block-scoped `TMCCJPEGDecWork* scanner = work;`, for-loop
increment) do not work (114/116): the copy must be inside the arm itself.
- DOL-wide scan: `bc +8; b back` occurs only in NHTTP switch lowering, VF inlined
Advance/MoveTo returns, and this function.
- Switch with `case 0xD9`/default: 115 insns, 23 diffs (signed `cmpwi` for D9).
- Loop-flag forms (found/keepGoing/searching, do/while/for): 118-121 insns.
- Empty `else {}` (get_wbyte idiom) on the range test: 114, folded.
- Inline helper decompositions of the scan (skip-to-FF, readMarker, predicate):
114-119 insns.
### Finish block
Exhaustive enumeration of all 3410 dependency-valid statement orders x 2 dist
forms x 2 next forms x 2 decl orders plus annealing searches: best 6 diffs
(pitch r8/skip r7 swap, `mullw` operand order). Fixes:
- `oy = pos & 0xFF` as a named u16 local makes `mullw` keep source order
(`(u8)pos`, casts, `(u8)` locals are reordered to pitch-first).
- The skip count must stay a named local so pitch is coloured first: reusing
the u8 distance local for the multiplied count (`dist *= interval`) gives
0 diffs. A separate `skipped` local is copy-propagated into a temp that takes r7.
Result with scanner alias stand-in: 115/115, 0 diffs, other 12 functions unchanged.
## Prior art (read-only)
hotlandsoftware/wii-news-channel decompiled an older build of the same TMCC JPEG
library (`src/revolution/TMCC_JPEG/jpgd_dec.c`, `jpgdResync`). Its finish uses
the same `skip = ...; skip *= restartInterval;`, `(x << 16) + y` and `n % pitch`
spellings, passes `&ctx->stream` (a stream sub-struct at offset 0 of the work)
to every stream call, and its notes record the identical unsolved problem:
"the loop exit is `ble; b` instead of `bgt`, plus two register swaps" (98.44%,
400 srcsearch iterations, continue/goto/switch/duplicated-condition forms).
So the unfolded exit comes from the original source in two independent builds.
Their flat finish reproduces here as 11-12 diffs (marker r5, skip r8); the inline
helper boundary fixes both swaps.
## Why a no-op pointer copy is required (not a trick)
- The arm must hold a front-end statement the backend deletes after the PCode
branch fold. Dead stores, self-referencing counters, pure expressions, empty
inline calls, labels, nested blocks, `asm {}` and `#pragma unused` all vanish
in the front end (114, folded).
- Only register copies survive to the allocator and then coalesce. Coalescing
needs no interference, so the destination is live at the loop head yet always
holds the source value. At the loop head only `work` (r30) and `state` (r31)
are live in registers (the inner test reloads the stack byte), so every
matching source has a value-preserving pointer copy on this path. Exact forms
found: an alias reset (`stream = work`, or a `state`/finish alias), a pointer
self-assignment through a cast, a for-loop increment alias. Plain
self-assignments and `+ 0` are folded away.
- Brute force of 46 natural assignments over the function's own variables,
loads and constants in the arm: only the two pointer cast self-assignments
match; loads/stores add instructions, scalar copies fold.
- Compiler sweep GC/3.0a5, 3.0a5.2, Wii 0x4201_127, 1.0RC1-1.7: all fold the plain
form; only GC/3.0a5.x keeps the other 12 functions.
- Chosen form mirrors the original's `&ctx->stream` handle: a `stream` pointer
used for rewind/peek/get_byte, reset at the end of the scan loop, with an
`MWCC needs ...` comment (precedent: `AudioWaveUtility.cpp` local aliases of
`this`, `get_wbyte` empty else branches). Loop uses the prior art's
`if (RST) break;` structure.
## Final
`restartFromMarker` inline helper (locals in computation order: skip, pitch, pos,
row, column, mcuIndex, y, x, result; `row = pos & 0xFF` keeps the multiply
operand order) plus `TMCJPEGDEC_err_restart` with the stream handle.
- odiff 0/115, ctxdiff diffs 0, pool identical (no strings), whole-object .text
byte-identical, other 12 functions unchanged.
- Asm inventory row and `[rx24]` link removed; header now 164 functions,
166 bodies/blocks, 162 ORIGINAL, 2 PLACEHOLDER. Checker FAIL only for the two
remaining unrelated placeholders (CNTCACHEClear, System::warning_run).
- Full build OK, DOL SHA1 26116613f624061ba99c8d1a299aaa6efa85670d, DECOMPLETE_OK.