decomp(jpeg): match IdctBlock_Lumi and IdctBlock_Col, link idct_block_var #1324

Merged
coah80 merged 1 commits from agent/w0929/o3-idct-land into main 2026-10-09 11:05:24 +00:00
3 changed files with 229 additions and 172 deletions

View File

@@ -1202,7 +1202,7 @@ config.libs = [
Object(Matching, "buffer/mkhdec3.c"),
Object(Matching, "buffer/buffer_system.c"),
Object(Matching, "buffer/getcode.c"),
Object(NonMatching, "buffer/idct_block_var.c"),
Object(Matching, "buffer/idct_block_var.c"),
Object(Matching, "api/decapi.c"),

View File

@@ -35,81 +35,80 @@ static s32 clampLumaDc(s32 coefficient) {
return clampU8((coefficient >> 11) + 0x80);
}
#pragma push
#pragma optimization_level 3
void TMCJPEGDEC_IdctBlock_Lumi(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigzag) {
s32 workspace[64];
s32 rowOddOutput3;
s32 columnOddOutput2;
s32 pitch2;
s32* dst;
s32 rowOddOutput1;
int columnAcBits;
s32 column;
s32 rowEvenOutput0;
s32 columnC6;
s32 columnC4;
s32 columnOddSumRotation;
s32 rowDc;
s32 rowEvenOutput1;
s32 columnC1;
s32 columnC5;
s32 columnC7;
s32 columnC4;
s32 columnEvenBaseDifference;
s32 columnC3;
s32 rowC4;
s32 rowC2;
s32 columnEvenOutput0;
s32 columnC2;
s32 columnScaledEvenRotation;
int columnAcBits;
s32 rowOddSumRotation;
s32 rowEvenOutput1;
s32 rowOddOutput1;
s32* dst;
s32 columnEvenOutput0;
s32 columnEvenOutput1;
s32 columnC5;
s32 rowC4;
s32 rowOddSumRotation;
s32 columnEvenOutput3;
s32 columnOddHighDifference;
s32 columnOddLowSum;
s32 rowCount;
s32 columnEvenBaseSum;
s32 rowEvenBaseDifference;
s32 rowOddDifferenceRotation;
s32 columnOddLowDifference;
s32 columnOddHighSum;
s32 columnC7;
s32 columnC3;
s32 columnEvenBaseSum;
s32 rowEvenBaseDifference;
s32 rowEvenOutput2;
s32 columnOddMix;
s32 columnEvenOutput2;
s32 rowEvenOutput0;
s32 rowC1;
s32 columnOddOutput1;
s32 rowC3;
s32 columnDc;
s32 rowEvenSum;
s32 rowEvenRotationSum;
s32 rowSampleDc;
u8* columnOutput;
s32 rowOddHighDifference;
s32 columnEvenRotationSum;
s32 pitch4;
s32 rowOddOutput2;
s32* src;
s32 pitch7;
s32 columnOddOutput0;
s32 rowEvenBaseSum;
s32 rowEvenOutput3;
s32 rowC6;
s32 rowOddLowDifference;
s32 rowOddHighSum;
s32 columnOddDifferenceRotation;
s32 pitch6;
s32 pitch5;
s32 rowOddLowRotation;
s32 rowC5;
s32 columnOddOutput3;
s32 pitch3;
int rowAcBits;
s32 rowOddLowSum;
s32 processed;
u8 columnSample;
s32 pitch2;
s32 rowOddOutput0;
s32 rowC7;
s32 column;
s32 rowC1;
s32 columnOddHighDifference;
s32 columnDc;
s32 columnOddDifferenceRotation;
s32 columnOddOutput1;
s32 rowScaledEvenRotation;
s32 columnEvenOutput2;
s32 columnOddMix;
int rowAcBits;
s32 columnEvenRotationSum;
s32 rowEvenBaseSum;
s32 rowC2;
s32 rowEvenRotationSum;
s32 rowEvenOutput3;
s32 rowOddLowDifference;
s32 columnOddOutput0;
u8* columnOutput;
s32 pitch4;
s32* src;
s32 rowC3;
s32 rowOddHighDifference;
s32 rowOddHighSum;
s32 rowOddOutput3;
s32 pitch7;
s32 pitch6;
s32 rowC6;
s32 columnOddOutput2;
u8 columnSample;
s32 processed;
s32 rowOddLowSum;
s32 rowOddDifferenceRotation;
s32 columnOddOutput3;
s32 rowC5;
s32 rowOddMix;
s32 rowSampleDc;
s32 rowOddOutput2;
s32 rowC7;
s32 rowCount;
s32 pitch5;
s32 pitch3;
s32 workspace[64];
s32 rowEvenSum;
s32 rowOddLowRotation;
s32 rowEvenRotation;
s32 rowOddDifferenceSum;
s32 rowOddHighRotation;
@@ -148,32 +147,31 @@ void TMCJPEGDEC_IdctBlock_Lumi(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigz
dst[1] = rowSampleDc;
dst[0] = rowSampleDc;
} else {
rowOddHighSum = rowC1 + rowC7;
rowDc = block[0];
rowEvenSum = rowC6 + rowC2;
rowOddLowDifference = rowC5 - rowC3;
rowOddHighDifference = rowC1 - rowC7;
rowEvenRotation = rowC2 - rowC6;
rowC2 = rowEvenRotation * 0xB5 >> 8;
rowEvenBaseDifference = rowDc - rowC4;
rowOddLowSum = rowC5 + rowC3;
rowOddSumDifference = rowOddHighSum - rowOddLowSum;
rowOddSumRotation = rowOddSumDifference * 0xB5 >> 8;
rowOddDifferenceSum = rowOddLowDifference + rowOddHighDifference;
rowOddDifferenceRotation = rowOddDifferenceSum * 0x62 >> 8;
rowOddLowRotation = rowOddLowDifference * 0x8B;
rowOddHighRotation = rowOddHighDifference * 0x14E >> 8;
rowEvenRotationSum = rowEvenSum + rowC2;
rowEvenOutput1 = rowEvenBaseDifference + rowC2;
rowOddLowRotation >>= 8;
rowEvenSum = rowC2 + rowC6;
rowEvenBaseSum = rowDc + rowC4;
rowOddMix = rowOddHighRotation - rowOddDifferenceRotation;
rowEvenOutput2 = rowEvenBaseDifference - rowC2;
rowOddOutput1 = rowOddSumRotation + rowOddMix;
rowEvenOutput3 = rowEvenBaseSum - rowEvenRotationSum;
rowOddLowSum += rowOddMix;
rowEvenBaseDifference = rowDc - rowC4;
rowScaledEvenRotation = rowEvenRotation * 0xB5 >> 8;
rowEvenRotationSum = rowScaledEvenRotation + rowEvenSum;
rowEvenOutput0 = rowEvenBaseSum + rowEvenRotationSum;
rowEvenOutput1 = rowEvenBaseDifference + rowScaledEvenRotation;
rowEvenOutput2 = rowEvenBaseDifference - rowScaledEvenRotation;
rowEvenOutput3 = rowEvenBaseSum - rowEvenRotationSum;
rowOddHighSum = rowC1 + rowC7;
rowOddHighDifference = rowC1 - rowC7;
rowOddLowSum = rowC5 + rowC3;
rowOddLowDifference = rowC5 - rowC3;
rowOddSumDifference = rowOddHighSum - rowOddLowSum;
rowOddDifferenceSum = rowOddLowDifference + rowOddHighDifference;
rowOddSumRotation = rowOddSumDifference * 0xB5 >> 8;
rowOddDifferenceRotation = rowOddDifferenceSum * 0x62 >> 8;
rowOddHighRotation = rowOddHighDifference * 0x14E >> 8;
rowOddLowRotation = rowOddLowDifference * 0x8B >> 8;
rowOddMix = rowOddHighRotation - rowOddDifferenceRotation;
rowOddLowSum += rowOddMix;
rowOddOutput0 = rowOddHighSum + rowOddLowSum;
rowOddOutput1 = rowOddSumRotation + rowOddMix;
rowOddOutput3 = rowOddDifferenceRotation + rowOddLowRotation;
rowOddOutput2 = rowOddOutput3 + rowOddSumRotation;
dst[0] = rowEvenOutput0 + rowOddOutput0;
@@ -268,100 +266,99 @@ void TMCJPEGDEC_IdctBlock_Lumi(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigz
}
}
#pragma pop
static s32 clampChromaDc(s32 value) {
return clampS8(value >> 11);
}
void TMCJPEGDEC_IdctBlock_Col(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigzag) {
s32 workspace[64];
s32 rowOuter;
s32 columnOddOutput2;
s32* dst;
s32 columnEvenRotationSum;
s32 rowOddSumRotation;
s32 rowOddOutput1;
s32* src;
s32 rowC3;
s32 rowMid;
s32 columnOddSumRotation;
s32 columnOddOutput3;
s32 columnC1;
s32 columnEvenBaseSum;
s32 sparseDc;
s32 columnEvenBaseDifference;
s32 columnOddDifferenceRotation;
s32 columnC7;
s32 rowOdd;
s32 rowDc;
s32 rowC6;
s32 rowC2;
s32 columnC6;
s32 rowOddOutput3;
s32 columnOddOutput1;
s32 columnScaledEvenRotation;
s32 rowCount;
s32 columnC5;
s32 rowSampleDc;
s32 column;
s32 columnOddOutput0;
s32 rowEvenBaseDifference;
s32 columnEvenOutput0;
s32 columnOddMix;
s32 columnEvenOutput1;
s32 columnDc;
s32 rowRotation;
s32 columnC4;
s32 columnC3;
s32 columnOddSumRotation;
s32 rowOddOutput1;
s32 columnC1;
int rowAcBits;
s32 columnEvenBaseDifference;
s32 columnC7;
s32 rowDc;
s32 rowOddSumRotation;
s32 rowEvenBaseSum;
s32 columnOddDifferenceRotation;
s32 columnEvenRotationSum;
s32* dst;
s32 columnOddOutput2;
s32 sparseDc;
s32 rowOddDifferenceRotation;
s32 columnOddOutput1;
s32 rowOddOutput2;
s32 columnDc;
s32 columnEvenBaseSum;
s32* src;
s32 rowOuter;
s32 processed;
s32 columnC6;
s32 rowOdd;
s32 column;
s32 columnOddOutput3;
s32 rowSampleDc;
s32 rowOddOutput0;
s32 rowC3;
s32 columnScaledEvenRotation;
s32 rowRotation;
s32 rowEvenOutput2;
s32 rowC2;
s32 rowEvenBaseDifference;
s32 columnC5;
s32 rowEvenRotationSum;
s32 columnOddOutput0;
s32 rowC1;
int columnAcBits;
s32 columnEvenOutput3;
s32 rowOddOutput2;
s32 processed;
s32 rowEvenOutput1;
s32 rowOddOutput0;
s32 columnEvenOutput2;
s32 rowEvenBaseSum;
s32 rowC7;
s32 rowC5;
s32 rowOddMix;
s32 rowLowRotation;
s32 rowEvenOutput2;
s32 rowScaledEvenRotation;
s32 columnOddHighDifference;
s32 rowEvenOutput3;
s32 rowEvenOutput0;
s32 rowMix;
s32 rowC4;
s32 columnC4;
s32 columnOddMix;
s32 columnOddLowSum;
s32 rowEvenOutput1;
s32 rowLowRotation;
s32 columnC3;
s32 rowOddHighDifference;
s32 rowC5;
s32 columnEvenOutput0;
s32 rowEvenOutput0;
s32 rowOddMix;
s32 rowEvenOutput3;
s32 rowC4;
s32 rowInner;
s32 rowC6;
s32 columnEvenOutput1;
s32 rowScaledEvenRotation;
s32 rowOddLowDifference;
s32 rowC7;
s32 rowOddOutput3;
s32 columnEvenOutput3;
s32 rowMid;
s32 columnOddHighDifference;
s32 columnEvenOutput2;
s32 columnC2;
s32 columnOddLowDifference;
s32 columnOddHighSum;
s32 rowInner;
s32 rowOddHighSum;
s32 rowOddHighDifference;
int rowAcBits;
s32 rowOddLowSum;
u8* columnOutput;
s32 columnC2;
s32 rowEvenRotationSum;
int columnEvenSum;
s32 columnOddLowRotation;
s32 rowEvenSum;
s32 flatDc;
s32 rowSample;
s32 rowOddLowRotation;
s32 columnEvenRotation;
s32 rowOddDifferenceSum;
s32 rowOddSumDifference;
s32 rowEvenRotation;
s32 rowOddHighRotation;
s32 columnSample;
s32 columnOddDifferenceSum;
int columnEvenSum;
s32 rowOddHighRotation;
s32 workspace[64];
s32 rowOddLowRotation;
s32 rowSample;
s32 rowEvenRotation;
s32 columnOddHighRotation;
s32 columnOddSumDifference;
s32 columnOddDifferenceSum;
s32 columnOddLowRotation;
s32 columnEvenRotation;
s32 rowMix;
if (zigzag == 0x11) {
flatDc = block[0];
@@ -425,37 +422,36 @@ void TMCJPEGDEC_IdctBlock_Col(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigza
dst[1] = rowSampleDc;
dst[0] = rowSampleDc;
} else {
rowOddHighDifference = rowC1 - rowC7;
rowOddLowSum = rowC5 + rowC3;
rowEvenSum = rowC6 + rowC2;
rowOddHighSum = rowC1 + rowC7;
rowEvenRotation = rowC2 - rowC6;
rowDc = block[0];
rowEvenRotation = rowC2 - rowC6;
rowEvenSum = rowC2 + rowC6;
rowEvenBaseSum = rowDc + rowC4;
rowEvenBaseDifference = rowDc - rowC4;
rowScaledEvenRotation = rowEvenRotation * 0xB5 >> 8;
rowOddSumDifference = rowOddHighSum - rowOddLowSum;
rowOddLowDifference = rowC5 - rowC3;
rowOddSumRotation = rowOddSumDifference * 0xB5 >> 8;
rowEvenBaseSum = rowDc + rowC4;
rowOddHighRotation = rowOddHighDifference * 0x14E >> 8;
rowOddDifferenceSum = rowOddLowDifference + rowOddHighDifference;
rowOddDifferenceRotation = rowOddDifferenceSum * 0x62 >> 8;
rowEvenRotationSum = rowScaledEvenRotation + rowEvenSum;
rowOddMix = rowOddHighRotation - rowOddDifferenceRotation;
rowOddOutput1 = rowOddSumRotation + rowOddMix;
rowEvenOutput1 = rowEvenBaseDifference + rowScaledEvenRotation;
rowOddLowRotation = rowOddLowDifference * 0x8B;
rowOddLowSum += rowOddMix;
rowEvenOutput3 = rowEvenBaseSum - rowEvenRotationSum;
rowEvenOutput0 = rowEvenBaseSum + rowEvenRotationSum;
rowOddLowRotation >>= 8;
rowOddOutput3 = rowOddDifferenceRotation + rowOddLowRotation;
rowEvenOutput1 = rowEvenBaseDifference + rowScaledEvenRotation;
rowEvenOutput2 = rowEvenBaseDifference - rowScaledEvenRotation;
rowEvenOutput3 = rowEvenBaseSum - rowEvenRotationSum;
rowOddHighSum = rowC1 + rowC7;
rowOddHighDifference = rowC1 - rowC7;
rowOddLowSum = rowC5 + rowC3;
rowOddLowDifference = rowC5 - rowC3;
rowOddSumDifference = rowOddHighSum - rowOddLowSum;
rowOddDifferenceSum = rowOddLowDifference + rowOddHighDifference;
rowOddSumRotation = rowOddSumDifference * 0xB5 >> 8;
rowOddDifferenceRotation = rowOddDifferenceSum * 0x62 >> 8;
rowOddHighRotation = rowOddHighDifference * 0x14E >> 8;
rowOddLowRotation = rowOddLowDifference * 0x8B >> 8;
rowOddMix = rowOddHighRotation - rowOddDifferenceRotation;
rowOddLowSum += rowOddMix;
rowOddOutput0 = rowOddHighSum + rowOddLowSum;
rowOddOutput1 = rowOddSumRotation + rowOddMix;
rowOddOutput3 = rowOddDifferenceRotation + rowOddLowRotation;
rowOddOutput2 = rowOddOutput3 + rowOddSumRotation;
dst[0] = rowEvenOutput0 + rowOddOutput0;
dst[7] = rowEvenOutput0 - rowOddOutput0;
dst[1] = rowEvenOutput1 + rowOddOutput1;
rowOddOutput2 = rowOddOutput3 + rowOddSumRotation;
dst[6] = rowEvenOutput1 - rowOddOutput1;
dst[2] = rowEvenOutput2 + rowOddOutput2;
dst[5] = rowEvenOutput2 - rowOddOutput2;

View File

@@ -0,0 +1,61 @@
# o3-idct attempts
Worktree data-d11, branch agent/w1009/o3-idct (from origin/main 179cd4e3). 43U only.
Unit: `libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var`.
Start: Lumi 11 diffs (O3 pragma), Col 51 diffs.
## What was wrong (Lumi)
- Post-RA scheduler model (`/tmp/opus-idct-sched/sched.py`, reproduces all 61 captured blocks)
applied to our pre-RA order with only four values recoloured (scaled even rotation and odd
difference rotation to r20, even sum and first even output to r10) gives the target order
exactly. So the 11 diffs were pure colouring; no schedule or dataflow change was needed.
- The retained source made the scaled rotation a split web of `rowC2` (`rowC2 = rot * 0xB5 >> 8`).
MWCC's lifetime splitter (`fn_00459420` in the rayanht GC/1.x decomp) turns every web but the
first-defined one into an `@` temp numbered after all named locals, so it is coloured early and
takes r10. Named locals are numbered in reverse declaration order; single-use locals are forward
substituted and become codegen temps numbered in statement order at their use.
- regsim on that capture: no declaration order and no single renumbering reaches the target.
With a named rotation, the even-sum temp has to be numbered between the rotation temps and the
named locals (it must be created before the odd-part temps).
## The fix (Lumi exact, no pragma)
- Natural row pass in the 4x4 sibling's shape: even part first (`evenRotation = c2 - c6;
evenSum = c2 + c6; ... scaled = evenRotation * 0xB5 >> 8; evenRotationSum = scaled + evenSum;`),
then the odd part. Single-use `evenSum` is substituted and MWCC emits `add c6, c2`, as in the
target; no reassociation (that came from the old statement order, not from the types).
- mwdbg capture of that source (`runs/o3-idct-nat1`), vmap to the target, then an anneal over the
order of named locals with regsim: cost 0. Declaring the locals in the solved order compiles
to an exact Lumi at O3 and at the unit's O4, so the scoped `optimization_level 3` was dropped.
## Col (exact)
- Same natural row pass: 51 -> 28 diffs (dataflow now matches; the old source reassociated the
even sum to `c2 + (rot + c6)`).
- Compiler-in-the-loop declaration climb: 28 -> 13.
- mwdbg capture of the climbed source (`runs/o3-idct-natcol1c1`): regsim reproduced it with only
4 wrong registers; the declaration anneal reached cost 0 on four of five seeds and the solved
order compiles to an exact Col. A duplicated `rowOddOutput2` statement left over from the splice
was removed (still exact).
## Result
- Lumi 11 -> 0 diffs, Col 51 -> 0 diffs; `.text` byte-identical, pool identical, symorder ORDER OK.
- Unit flipped to `Matching`; full build passes, main.dol SHA1 26116613f624061ba99c8d1a299aaa6efa85670d.
- No pragmas left in the file (the scoped `optimization_level 3` from #1322 is gone).
- Gate: `GATE PASS`, code 2844/2844, functions 2/2, linked code 2844, 0 regressions.
## Method worth reusing
1. Make the source natural first and check the dataflow (here: copy the sibling 4x4 butterfly).
2. Capture once with mwdbg, map every vreg to the target register (`/tmp/o3-idct/sched/vmap.py`,
a copy of the opus-idct one with `andi.` added).
3. Anneal the order of named locals with regsim against the full want map
(`/tmp/o3-idct/sim/declsearch2.py <capture> <vmap.json> '{}' <seed> <steps>`), then write the
declarations in that order. If temps are among the mismatches, no declaration order can fix it:
change which values are named or where single-use values are consumed, and recapture.
Tools tried and not needed: per-function pragma sweep (no lead), grouping row/column declarations
(breaks the match; the solved interleaving is required).