Compare commits

...

1 Commits

Author SHA1 Message Date
coah
4b4566fb70 decomp(nwc24): getUnScrambleId in C (Petari prior art) (worker 0291759e) 2026-10-08 18:36:48 +00:00
4 changed files with 292 additions and 165 deletions

View File

@@ -1,7 +1,7 @@
# Assembly inventory
4.3U, 2026-10-05. 170 functions contain 172 assembly
bodies/blocks: 162 ORIGINAL functions and 8 PLACEHOLDER functions.
4.3U, 2026-10-05. 167 functions contain 169 assembly
bodies/blocks: 162 ORIGINAL functions and 5 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,
@@ -160,7 +160,6 @@ found. Historical logs absent from this branch have commit IDs and excerpts in
| `libs/RVL_SDK/src/os/__ppc_eabi_init.cpp` | `__init_hardware` | ORIGINAL | Before normal C startup, mfmsr/mtmsr enables the FPU and r31 holds LR across init calls. |
| `libs/RVL_SDK/src/os/__ppc_eabi_init.cpp` | `__flush_cache` | ORIGINAL | dcbst/sync/icbi/isync make freshly initialized code visible to instruction fetch. |
| `libs/RVL_SDK/src/os/__ppc_eabi_init.cpp` | `__init_user` | ORIGINAL | fralloc/bl __init_cpp/frfree/blr matches the public [SDK startup assembly][sdk-init]. |
| `libs/RevoEX/src/nwc24/NWC24UserId.c` | `getUnScrambleId` | PLACEHOLDER | Integer byte permutation; best C 76.27329%, 161/161 instructions, 97 register/scheduling differences, [rx30][rx30]. |
| `libs/Runtime/src/Gecko_setjmp.c` | `__setjmp` | ORIGINAL | Saves or restores caller GPRs/FPRs, LR, CR, and stack state across nonlocal returns. |
| `libs/Runtime/src/Gecko_setjmp.c` | `longjmp` | ORIGINAL | Saves or restores caller GPRs/FPRs, LR, CR, and stack state across nonlocal returns. |
| `libs/Runtime/src/__init_cpp_exceptions.cpp` | `__exception_info_constants` | ORIGINAL | Reads the caller ABI TOC register r2 directly for exception registration. |

View File

@@ -2,6 +2,7 @@
#include <revolution/nwc24.h>
static const u8 TtableInv[] = {13, 5, 9, 7, 0, 15, 10, 2, 12, 3, 14, 1, 8, 6, 11, 4};
static const u8 ExcTable[] = {1, 5, 0, 4, 2, 3, 6, 7};
extern void _savegpr_27(void);
extern void _restgpr_27(void);
extern void __shr2u(void);
@@ -53,166 +54,34 @@ NWC24Err NWC24iCheckUserIdCRC(NWC24UserId id) {
return NWC24_OK;
}
static asm NWC24UserId getUnScrambleId(register NWC24UserId id) {
li r9,-1
lis r7,0x20
lis r6,0x5e5e
stwu r1,-0x20(r1)
subi r7,r7,1
and r8,r4,r9
addi r0,r6,0x5e5e
stw r31,0x1c(r1)
and r3,r3,r7
lis r5,-1
xor r8,r8,r0
stw r30,0x18(r1)
and r8,r8,r9
xori r3,r3,0x5e5e
rlwinm r0,r8,0x15,0xa,0xa
li r6,-0x100
and r3,r3,r7
rlwinm r8,r8,0x1f,0,0x1f
or r3,r3,r0
stw r29,0x14(r1)
rlwimi r8,r3,0x1f,0,0
lis r4,-0xff
rlwinm r7,r3,0x1f,1,0x1f
addi r5,r5,0xff
rlwinm r30,r8,0x18,0,0x1f
rlwinm r29,r3,0x17,0x18,0x1f
and r12,r7,r9
li r0,0
rlwimi r30,r3,0x17,0,7
and r31,r8,r6
rlwimi r31,r30,0,0x18,0x1f
rlwinm r11,r3,0x1f,0x10,0x17
and r30,r31,r5
and r12,r12,r9
rlwimi r0,r29,8,0x18,0x1f
subi r4,r4,1
or r12,r12,r0
or r11,r30,r11
and r30,r11,r4
rlwinm r31,r8,0,0x18,0x1f
li r0,0
rlwinm r11,r8,0x10,8,0xf
lis r10,0x100
and r12,r12,r9
rlwimi r0,r31,0x10,0x10,0x1f
or r30,r30,r11
or r29,r12,r0
rlwinm r12,r3,0x1f,0x18,0x1f
subi r0,r10,1
li r10,0
rlwimi r10,r12,0x18,8,0x1f
and r11,r29,r9
and r12,r30,r0
rlwinm r3,r3,0x17,0,7
or r3,r12,r3
or r12,r11,r10
and r10,r3,r9
lis r3,TtableInv@ha
and r31,r10,r9
rlwinm r30,r8,0x10,0,0x1f
rlwinm r11,r31,0x1c,0x1c,0x1f
addi r3,r3,TtableInv@l
rlwimi r30,r7,0x10,0,0xf
rlwinm r10,r31,0,0x1c,0x1f
and r29,r12,r6
rlwinm r12,r8,8,0,0x1f
lbzx r11,r3,r11
rlwimi r12,r7,8,0,0x17
lbzx r7,r3,r10
rlwimi r29,r30,0,0x18,0x1f
rlwinm r8,r11,4,0,0x1b
rlwinm r10,r12,8,0x10,0x17
and r11,r29,r5
or r10,r11,r10
or r7,r8,r7
and r11,r31,r6
li r8,0
rlwimi r11,r7,0,0x18,0x1f
and r12,r10,r9
rlwinm r10,r11,0x18,0,0x1f
li r7,0
rlwimi r10,r12,0x18,0,7
and r31,r11,r5
rlwinm r11,r10,0x1c,0x1c,0x1f
and r12,r12,r9
rlwinm r10,r10,0,0x1c,0x1f
lbzx r11,r3,r11
lbzx r10,r3,r10
rlwinm r11,r11,4,0,0x1b
or r10,r11,r10
rlwinm r11,r10,0,0x18,0x1f
rlwinm r10,r10,8,0x10,0x17
rlwimi r8,r11,8,0x18,0x1f
or r10,r31,r10
or r8,r12,r8
rlwinm r12,r10,0x10,0,0x1f
and r11,r10,r4
rlwimi r12,r8,0x10,0,0xf
and r10,r8,r9
rlwinm r8,r12,0x1c,0x1c,0x1f
rlwinm r4,r12,0,0x1c,0x1f
lbzx r8,r3,r8
lbzx r4,r3,r4
rlwinm r8,r8,4,0,0x1b
or r4,r8,r4
rlwinm r8,r4,0,0x18,0x1f
rlwinm r4,r4,0x10,8,0xf
or r29,r11,r4
rlwimi r7,r8,0x10,0x10,0x1f
or r11,r10,r7
lis r4,1
rlwinm r8,r29,8,0,0x1f
and r31,r29,r0
rlwimi r8,r11,8,0,0x17
and r12,r11,r9
rlwinm r10,r8,0x1c,0x1c,0x1f
subi r0,r4,0x4c4d
lbzx r11,r3,r10
rlwinm r8,r8,0,0x1c,0x1f
lbzx r10,r3,r8
li r8,0
rlwinm r11,r11,4,0,0x1b
lis r7,-0x4c4c
or r11,r11,r10
rlwinm r10,r11,0,0x18,0x1f
subi r7,r7,0x4c4d
rlwimi r8,r10,0x18,8,0x1f
rlwinm r4,r11,0x18,0,7
or r10,r12,r8
rlwinm r8,r10,0x1c,0x1c,0x1f
or r12,r31,r4
rlwinm r4,r10,0,0x1c,0x1f
lbzx r8,r3,r8
and r11,r10,r6
lbzx r4,r3,r4
rlwinm r6,r8,4,0,0x1b
and r8,r12,r9
or r4,r6,r4
rlwimi r11,r4,0,0x18,0x1f
and r10,r8,r9
rlwinm r6,r11,0x14,0x1c,0x1f
rlwinm r4,r11,0x18,0x1c,0x1f
lbzx r6,r3,r6
lbzx r3,r3,r4
and r8,r11,r5
rlwinm r5,r6,4,0,0x1b
and r4,r10,r9
or r3,r5,r3
rlwinm r3,r3,8,0x10,0x17
or r3,r8,r3
rlwinm r5,r3,0xa,0xb,0x15
rlwinm r3,r3,0x15,0x16,0x1f
rlwimi r5,r4,0xa,0x16,0x1f
rlwimi r3,r4,0xa,0,0x15
xor r4,r3,r7
xor r3,r5,r0
lwz r31,0x1c(r1)
lwz r30,0x18(r1)
lwz r29,0x14(r1)
addi r1,r1,0x20
blr
static u8 getByte(u64 value, u8 index) {
return (value >> (index * 8)) & 0xFF;
}
static u64 setByte(u64 value, u8 index, u8 byte) {
return (value & ~(0xFFULL << ((u64)8 * index))) | ((u64)byte << ((u64)8 * index));
}
static NWC24UserId getUnScrambleId(NWC24UserId id) {
u64 copy;
u8 i;
id &= 0x001FFFFFFFFFFFFFULL;
id ^= 0x00005E5E5E5E5E5EULL;
id &= 0x001FFFFFFFFFFFFFULL;
id |= (((id & 0xFF) << 5) & 0x20) << 48;
id >>= 1;
copy = id;
for (i = 0; i < 6; ++i) {
id = setByte(id, i, getByte(copy, ExcTable[i]));
}
for (i = 0; i < 6; ++i) {
id = setByte(id, i, (TtableInv[(getByte(id, i) >> 4) & 0xF] << 4) | TtableInv[getByte(id, i) & 0xF]);
}
id = ((id & 0x7FFFFFFFFFFULL) << 10) | ((id >> 43) & 0x3FF);
id ^= 0x0000B3B3B3B3B3B3ULL;
return id;
}

View File

@@ -0,0 +1,116 @@
# opus-ph attempts
Branch agent/w1008/data-d3-c. Measure: `odiff.py <unit> <symbol>` differing count.
## TMCJPEGDEC_err_restart
Start: rx5b finishRestart candidate (114/115 insns, odiff 73 differing because of the 1-insn shift).
Key finding: mwdbg backend-00 shows the marker test is already folded (`bt gt -> loop`) in the
initial PCode, so the target's `ble found; b loop` is decided before the backend. In matched
code (`__OSInitNet`) the same unfolded shape comes from `if (a || b) return;`, i.e. a jump the
IR folder does not treat as a goto.
- v0 rx5b baseline: 73/115 (114 insns).
- A `if (m>=D0 && m<=D7) break;` + finish after loop: 73.
- B `if (m>=D0 && m<=D7) return finish;`: 73.
- C `if (m<D0 || m>D7) continue; return finish;`: 73.
- E nested `if (m>=D0) { if (m<=D7) return finish; }`: 73.
- H `goto found` after loop: 73.
- I empty-then / else return: 73.
- D1 `{ result = 0; continue; }`: 73. D2 else-arm assignment: 73. D3 continue/else break: 73.
- W1 do/while(byte<D0||byte>D7) using byte: 73. W2 byte instead of marker: 73.
- W3 marker as loop-carried while condition: 98/118.
- E1 else-if chain: 73. E2 inverted D9 test: 81. E3 inverted D9 inner test + break: 76.
- G1 `goto retry` label before inner while: 73.
- P1/P2/P3 isRestartMarker inline predicate (3 shapes x 2 uses): 76-78, 118-119 insns.
- S1 `switch (marker) { case 0xD0..0xD7: return finish; }`: 21/115, size equal. Switch
lowering emits the unfolded `bge case; b default`, but as signed `cmpwi d8; bge; cmpwi d0`
instead of target `cmplwi d0; blt; cmplwi d7; ble; b`. Marker lands in r4 not r6.
- S2 switch on (u32): 21. S3 default: continue: 21. S4 default: break: 21.
- S5/S6/S7 marker as u32/s32/u16 with switch: 21 (switch compares stay signed).
Result: not exact. Best is S1 (equal size, 21 differing). Remaining: switch compares are signed
and use a different tree (hi+1 then lo); marker gets r4 instead of r6, which shifts the finish
block's temporaries. Next lever: a construct whose jump the IR folder leaves alone (like the
`return` in `__OSInitNet`) but keeps the unsigned `cmplwi d0 / blt / cmplwi d7 / ble` order.
## getUnScrambleId
Start: rx30 id-08 (loop + inline helpers). That source is now auto-inlined into both C callers
(target keeps it out of line), so loop forms are out; the function must be written unrolled.
Metric below is (insns, exact positional, opcode positional) out of 161.
- u1 unrolled inline get/set/unscramble helpers: 156 differing.
- m1 macros GET_BYTE/SET_BYTE/UNSCRAMBLE, lazy permutation: 159 differing; multiset differs
only by two rlwimi shift-combines (b2/b3 read from rotated hi in target).
- x1 read all six bytes into b0..b5 first, then set: 111 differing.
- x1 extraction orders (4) / declaration order: 111-112.
- xf/xg `(x >> 1) | ((x & 1) << 52)` rotation: b2/b3 match but b1/b4/b5 stop combining; 113.
- greedy over GET/SET/UNSCRAMBLE/rotation/mask/permutation/substitution/final forms (24 builds):
best r2 (`mixId |= (mixId & 1) << 53; mixId >>= 1;`) + t2 (substitution via `b` temp): 69.
- p3 copy + `b = GET_BYTE(copy, k); mixId = SET_BYTE(mixId, i, b);`: 68 (prologue and rotation
now exact; b3 combine matches).
- greedy from p3 over all dims + declaration orders (≈30 builds): no gain.
- SET split into &=/|= (3 variants): 162-166 insns. inline get/set helpers: 161-163, ≤93 exact.
- b as u32/int/u16/s32: 164-175 insns. GET without cast / const shift / u64 cast: unchanged.
- SET mask spellings, (u8) value cast, assignment-macro, block-scoped/const copy: unchanged.
- per-pair inline GET in SET for subsets of the six moves: worse.
- distinct b0..b5 lazily: 156. separate perm/subst temps: 163 insns.
- 160 spellings of the mask/xor/rotate statements: all identical (IR canonicalizes).
Finding: mwdbg backend-00 shows MWCC splits this straight-line function into ~100-instruction
blocks at statement ends (B1 ends after `b = GET_BYTE(copy, 2)` at 103; before it 97). Combines
and scheduling are block-local. The target splits one statement earlier (b2/b3 both read the
uncombined rotated hi), so the original has 1-6 more initial PCode instructions before set 3.
No natural spelling found that adds them without changing final code.
Best: 161/161, 68 differing (93 exact positional), saved in opus-ph.best.c.
## Round 2 (higher effort)
### getUnScrambleId: exact
- u1 substitution as a `for (i = 0; i < 6; i++)` loop over GET/SET: 68 (identical to the unrolled form;
MWCC fully unrolls it, so loop source is fine as long as the function stays out of line).
- final step spellings: `return (rot) ^ B3` 61 (tail exact), operand swap 67, copy temp 68, split 80.
- copies/temps at the start (id_direct, copyfirst, idcopy, block-scoped inst temp): 68, IR propagates them.
- inline u8 getByte for the permutation only: 156 (different prologue allocation).
- permutation statement orders (source order, reversed): 148-159.
- prior art: `gh search code getUnScrambleId` found SMGCommunity/Petari src/RVL_SDK/nwc24/NWC24UserId.c
(same SDK, same 0x284 size in SMG). Its shape: static `getbyte`/`setbyte` helpers (auto-inlined), a
`static const u8 ExcTable[8] = {1, 5, 0, 4, 2, 3, 6, 7}` permutation table read in a `u8 i` loop,
a second loop for the TtableInv substitution, rotation spelled `v |= (((v & 0xFF) << 5) & 0x20) << 48`.
Compiled as is: 0/161. Unrolling the ExcTable loop by hand gives 159/163, so the table loop is what
shapes the PCode blocks. ExcTable lands in .sdata2 of the object (constant-folded after unrolling,
never referenced) and the linker dead-strips it: main.dol SHA1 unchanged, gate data 16/16.
- Final: renamed to repo style (getByte/setByte/copy/id), still 0/161; NWC24CheckUserId and
NWC24iCheckUserIdCRC stay 0. gate.py --quick: GATE PASS.
### TMCJPEGDEC_err_restart: not exact (best stays S1, 21/115, equal size)
Standalone harness (/tmp, same flags, `-ipa file`) to test the range-test shape. Target wants
`cmplwi d0; blt test; cmplwi d7; ble F; b test` (unsigned, unfolded).
- switch lowering is always signed and hi-first (`cmpwi hi+1; bge def; cmpwi lo; bge case; b def`),
for u8, u32, `unsigned long`, `(u32)` casts, `m - 0xD0` with cases 0..7, `(u8)(m - 0xD0)`, and even
case values >= 0x80000000. So the target is not a switch.
- if-forms all fold to `bgt test` (≈45 shapes): &&/|| in either direction, continue/break/goto
found/goto label, do-while with `||` or `!(&&)`, else-if chains, nested ifs, empty then, else
continue, flag loops (`found`, `keepGoing` style as in parse_para), ternary conditions,
`(u32)(m - 0xD0) <= 7`, `(m & 0xF8) == 0xD0`, the whole loop inside a static inline.
- `{ r = 0; continue; }`, `{ m = 0; continue; }`, `{ nop(); continue; }`, `{ (void)0; continue; }`:
still folded (IR removes the dead statement before folding).
- IRO 0 around the function (levers.md 26): no change (21 / 73). Target shows no IRO-off symptoms.
- inline predicates with `return` (`if (m < D0 || m > D7) return 0; return 1;`) give the unfolded
`ble` but materialize a bool (`li r0,0/1; cmpwi; beq`) that the target lacks.
- Scan of all compiled C objects: unfolded backward `bc +8; b back` appears only in switch lowering
(NHTTP) and inline-function exits (VF Advance/MoveTo); in retail, err_restart is the only
`cmplwi lo; blt; cmplwi hi; ble +8; b back` in the DOL.
- Mechanism found (t13): a then-arm with a surviving non-jump statement, e.g.
`if (m < 0xD0 || m > 0xD7) { last = r; continue; }`, gives exactly `blt X; cmplwi d7; ble F;
X: mr; b top`. The target matches this if that statement is a register copy that coalesces away
after the IR fold (same as tolower's empty ternary arm in MSL stricmp). No natural loop-carried
register variable exists in this function to carry such a copy (byte is address-taken, result/
marker are dead on that path), so not adopted.
- upstream koopthekoopa C (read-only) is the same nonmatching shape as our #else branch.
Next lever: find what loop-carried value the original kept in a register on the "other marker" path
(the copy that vanished), e.g. a marker/previous-marker variable or a result carried to the EOI return.

View File

@@ -0,0 +1,143 @@
/* opus-ph best C. Not exact; the retail asm stays in the source. */
/* libs/RevoEX/src/nwc24/NWC24UserId.c getUnScrambleId: 161/161 insns, odiff 68 differing (prior best 97). */
#define GET_BYTE(id, n) ((u8)((id) >> ((n) * 8)))
#define SET_BYTE(id, n, b) (((id) & ~((u64)0xFF << ((n) * 8))) | ((u64)(b) << ((n) * 8)))
#define UNSCRAMBLE(b) ((u8)((TtableInv[(b) >> 4] << 4) | TtableInv[(b) & 0xF]))
static NWC24UserId getUnScrambleId(NWC24UserId id) {
u64 mixId;
u64 copy;
u8 b;
mixId = id;
mixId &= 0x001FFFFFFFFFFFFFULL;
mixId ^= 0x00005E5E5E5E5E5EULL;
mixId &= 0x001FFFFFFFFFFFFFULL;
mixId |= (mixId & 1) << 53;
mixId >>= 1;
copy = mixId;
b = GET_BYTE(copy, 1);
mixId = SET_BYTE(mixId, 0, b);
b = GET_BYTE(copy, 5);
mixId = SET_BYTE(mixId, 1, b);
b = GET_BYTE(copy, 0);
mixId = SET_BYTE(mixId, 2, b);
b = GET_BYTE(copy, 4);
mixId = SET_BYTE(mixId, 3, b);
b = GET_BYTE(copy, 2);
mixId = SET_BYTE(mixId, 4, b);
b = GET_BYTE(copy, 3);
mixId = SET_BYTE(mixId, 5, b);
b = GET_BYTE(mixId, 0);
mixId = SET_BYTE(mixId, 0, UNSCRAMBLE(b));
b = GET_BYTE(mixId, 1);
mixId = SET_BYTE(mixId, 1, UNSCRAMBLE(b));
b = GET_BYTE(mixId, 2);
mixId = SET_BYTE(mixId, 2, UNSCRAMBLE(b));
b = GET_BYTE(mixId, 3);
mixId = SET_BYTE(mixId, 3, UNSCRAMBLE(b));
b = GET_BYTE(mixId, 4);
mixId = SET_BYTE(mixId, 4, UNSCRAMBLE(b));
b = GET_BYTE(mixId, 5);
mixId = SET_BYTE(mixId, 5, UNSCRAMBLE(b));
mixId = ((mixId & 0x7FFFFFFFFFFULL) << 10) | ((mixId >> 43) & 0x3FF);
mixId ^= 0x0000B3B3B3B3B3B3ULL;
return mixId;
}
/* libs/RVLMiddleware/TMC_JPEG/src/jpegdec/jdec_main.c TMCJPEGDEC_err_restart: 115/115 insns, odiff 21 differing (prior 114/115). */
static inline s32 finishRestart(TMCCJPEGDecWork* work, TMCCJPEGDecState* state, u8 marker) {
u8 restartDistance;
s32 result;
s32 next;
u8 skipped;
s32 quotient;
s32 remainder;
u16 y;
u16 x;
u16 pitch;
u16 interval;
u32 position;
restartDistance = marker + 8 - (work->rstMarkerIdx + 0xCF);
if (marker > work->rstMarkerIdx + 0xCF) {
restartDistance = marker - (work->rstMarkerIdx + 0xCF);
}
pitch = state->maxX;
interval = work->restartInterval;
skipped = restartDistance * interval;
work->rstMarkerIdx = (marker + 1) & 7;
position = work->mcuPos;
next = (u8)position * pitch + (skipped + (position >> 16));
quotient = next / pitch;
remainder = next - quotient * pitch;
y = quotient;
work->mcuPos = ((u32)remainder << 16) + y;
x = remainder;
work->components.dcPredict[0] = 0;
work->components.dcPredict[1] = 0;
work->components.dcPredict[2] = 0;
work->components.dcPredict[3] = 0;
work->restartCnt = 0;
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;
}
s32 TMCJPEGDEC_err_restart(TMCCJPEGDecWork* work) {
u8 byte;
TMCCJPEGDecState* state;
s32 result;
u8 marker;
state = work->pState;
if (work->scanCount == 1) {
state->decodeResult = 0;
return 0;
}
result = TMCJPEGDEC_rewind_ptr(work);
if (result < 0) {
return result;
}
byte = *work->pBufCur;
for (;;) {
while (byte != 0xFF) {
result = TMCJPEGDEC_get_byte(&byte, work);
if (result < 0) {
return result;
}
}
result = TMCJPEGDEC_get_byte(&byte, work);
marker = byte;
if (marker == 0xD9) {
if (result < 0 && result != TMCC_ERROR_UNDERFLOW) {
return result;
}
return 0;
}
if (result < 0) {
return result;
}
switch (marker) {
case 0xD0:
case 0xD1:
case 0xD2:
case 0xD3:
case 0xD4:
case 0xD5:
case 0xD6:
case 0xD7:
return finishRestart(work, state, marker);
}
}
}