diff --git a/libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var.c b/libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var.c index 02d0e046..e808fa22 100644 --- a/libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var.c +++ b/libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var.c @@ -92,18 +92,20 @@ void TMCJPEGDEC_IdctBlock_Lumi(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigz dst[0] = val; } else { s32 oddLowSum; + s32 oddLowDifference; s32 oddHighSum; + s32 oddHighDifference; s32 dcValue; t = b2 - b6; oddLowSum = b5 + b3; - b5 = b5 - b3; + oddLowDifference = b5 - b3; oddHighSum = b1 + b7; - b1 = b1 - b7; + oddHighDifference = b1 - b7; u = b6 + b2; t = t * 0xB5 >> 8; x_factor = oddHighSum - oddLowSum; - z_factor = b5 + b1; + z_factor = oddLowDifference + oddHighDifference; dcValue = block[0]; a = dcValue - b4; x = x_factor * 0xB5 >> 8; @@ -113,14 +115,14 @@ void TMCJPEGDEC_IdctBlock_Lumi(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigz z = z_factor * 0x62 >> 8; w = d + u; y = d - u; - m_part = b1 * 0x14E >> 8; + m_part = oddHighDifference * 0x14E >> 8; e = a - t; m = m_part - z; oddLowSum += m; n = oddHighSum + oddLowSum; o = x + m; dst[0] = w + n; - p_part = b5 * 0x8B; + p_part = oddLowDifference * 0x8B; dst[7] = w - n; p_part >>= 8; dst[1] = v + o; diff --git a/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGB565.c b/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGB565.c index c27f82e0..2d0a8e61 100644 --- a/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGB565.c +++ b/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGB565.c @@ -816,7 +816,6 @@ static void TMCJPEGDEC_converterYUV444toRGB565(TMCCJPEGDecWork* work, s32 x, s32 u8* luminance; u8* cb; u8* cr; - s32 value; s32 column; s32 xEnd; s32 cbValue; @@ -844,17 +843,17 @@ static void TMCJPEGDEC_converterYUV444toRGB565(TMCCJPEGDecWork* work, s32 x, s32 for (column = x; column < xEnd; column++) { cbValue = (s8)*cb++; crValue = (s8)*cr++; - value = *luminance++; - green = value + (-(cbValue * 88 + crValue * 183) >> 8); - red = value + ((crValue * 359) >> 8); - blue = value + ((cbValue * 454) >> 8); + red = *luminance++; + green = red + (-(cbValue * 88 + crValue * 183) >> 8); + blue = red + ((cbValue * 454) >> 8); + red = red + ((crValue * 359) >> 8); if ((blue | green | red) >> 8) { blue = blue > 255 ? 255 : blue < 0 ? 0 : blue; green = green > 255 ? 255 : green < 0 ? 0 : green; red = red > 255 ? 255 : red < 0 ? 0 : red; } output[(column & 3) + (((column >> 2) + tileRow) << 4)] = - ((blue & 0xF8) >> 3) + ((red & 0xF8) << 8) + ((green & 0xFC) << 3); + ((blue & 0xF8) >> 3) + (((red & 0xF8) << 8) + ((green & 0xFC) << 3)); } luminance += rowSkip; cb += rowSkip; diff --git a/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGBA8.c b/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGBA8.c index 69d38cb7..993d7389 100644 --- a/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGBA8.c +++ b/libs/RVLMiddleware/TMC_JPEG/src/texturecvtr/Texture_MCUtoRGBA8.c @@ -656,10 +656,10 @@ static void TMCJPEGDEC_converterYUV420toRGBA8edge(TMCCJPEGDecWork* work, s32 x, blueOffset = (cbValue * 454) >> 8; } { - s32 value = *luminance++; - s32 green = value + greenOffset; - s32 red = value + redOffset; - s32 blue = value + blueOffset; + s32 red = *luminance++; + s32 green = red + greenOffset; + s32 blue = red + blueOffset; + red = red + redOffset; if ((green | red | blue) >> 8) { blue = blue > 255 ? 255 : blue < 0 ? 0 : blue; green = green > 255 ? 255 : green < 0 ? 0 : green; diff --git a/tools/decomp-assist/jpeg-remainder-attempts.md b/tools/decomp-assist/jpeg-remainder-attempts.md new file mode 100644 index 00000000..acc9c0c6 --- /dev/null +++ b/tools/decomp-assist/jpeg-remainder-attempts.md @@ -0,0 +1,276 @@ +# JPEG remainder: semantic audit and source-shape reconstruction + +Baseline: `449529d9`, branch `agent/bittle/allhands-jpeg-decompile`, 4.3U only. +No remote writes, header changes, configuration changes, linking changes, assembly, +volatile forcing, padding, or edits to the separately owned entropy decoder. + +## Result + +All 28 owned functions were already implemented. No semantic mismatch was found +in a bounded differential comparison of their current compiled PPC against their +original ELF function bodies. This is additional test evidence, not a formal +proof of equivalence and not a Wii runtime test. + +Three natural source-shape changes improved the report without a function-score +regression: + +- Luminance IDCT now uses explicit odd-coefficient sum/difference temporaries, + matching the existing chroma first-pass butterfly. Function fuzzy + 82.31518 -> 83.05058; 257/257 instructions, 187 ctxdiff differences remain +- RGB444 consumes the luminance temporary as the red channel and uses the + existing exact edge sibling's red/green packing association. Function fuzzy + 97.96703 -> 98.71429; 91/91 instructions, 13 ctxdiff differences remain +- RGBA420 edge consumes the luminance temporary as red after calculating the + other channels. Function fuzzy 87.4 -> 87.458336; 120/120 instructions, + 77 ctxdiff differences remain + +No new exact matches. This candidate is not an exact-matching or linking result. + +| Unit | Exact before -> after | Fuzzy before -> after | Allocated data | +| --- | --- | --- | --- | +| Texture_MCUtoRGBA8 | 2/13 -> 2/13 | 90.318985 -> 90.32323 | 0 bytes; 100% | +| Texture_MCUtoRGB565 | 6/13 -> 6/13 | 96.40103 -> 96.44502 | 0 bytes; 100% | +| idct_block_var | 0/2 -> 0/2 | 88.80309 -> 89.06892 | 0 bytes; 100% | + +All eight previously exact functions still have exact-name objdiff 100% and +ctxdiff zero differences. Each setter remains 169/170 instructions, with one +folded buffer-base address operation; the other 26 functions retain their +original instruction counts. Every owned function and both input objects were +checked for allocated non-code sections: none. Pool checks were run before tuning and after the final build; all three were +identical, with zero strings. + +The baseline comparison was regenerated by rebuilding saved unmodified versions +of the three owned sources, reporting them, then restoring/rebuilding the final +sources. All 28 function scores were compared; zero regressions. + +Full build: PASS. `git diff --check`: PASS. +DOL SHA1: `26116613f624061ba99c8d1a299aaa6efa85670d`. + +Commands used: + +```sh +python3 configure.py --version 43U --wrapper ../toolchain/wibo-build/wibo +WIBO_SJIS_MISSING_IMPORTS=1 ../.venv/bin/ninja +build/tools/objdiff-cli report generate -p . -o build/43U/report.json -f json +PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/pool_diff.py +PYTHONPATH=../local-tools ../.venv/bin/python tools/decomp-assist/ctxdiff.py +../.venv/bin/python tools/decomp-assist/jpeg_differential_audit.py +sha1sum build/43U/main.dol +``` + +## Differential test scope and limitations + +The committed `jpeg_differential_audit.py` is a bounded integer PPC interpreter +and deterministic test generator. It reads the locally available source/original +ELF objects and does not distribute original executable bytes. Capstone and +pyelftools are required. Run it from the repository root after building the +three objects; do not run Python with assertions disabled. + +- 9,456 paired calls across all 28 functions, seed 449529 +- Normal and edge converters: scales 1, 2, 4, 8; multiple MCU origins; both + edge-dimension branches; zero, partial and full extents; constant and random + luminance/chroma bytes, including negative signed chroma and clipping +- Setters: all 256 component selectors and four supported IDCT sizes; + randomized 16-bit image dimensions; return values, converter identities and + non-stack side effects compared +- IDCT: all row extents 1..8 and column-mode representatives 1, 2, 3, 8; + DC-only, sparse and dense coefficients; positive/negative coefficients and + clipping-boundary DC values; pitches 8, 16, 32 +- Every non-stack byte is compared, including guard regions, along with setter + return values. Stack pointer, LR and GPR14..GPR31 restoration are checked +- Full instruction-address coverage was reached for all 20 non-grayscale + converter bodies and both IDCT functions. This does not mean exhaustive + branch/path/input coverage. Four grayscale bodies retain 19 unvisited + instructions each in saturation paths that follow an unsigned byte load; + each setter has one unvisited negative-selector branch instruction +- Function address relocations are normalized to deterministic symbol tokens, + with collision checks. Real linked addresses and integration are not tested +- Only the observed integer instruction subset is implemented. Unsupported + instructions/relocations/callees fail closed. No FPU, exceptions, cache/MMIO, + timing, alignment faults, XER behavior or complete condition-register model +- `memset` is modeled by its memory effects and ABI-permitted volatile-register + clobbers; its actual library machine code is not executed +- Inputs are bounded samples. Invalid pointers, zero scale, arbitrary invalid + IDCT descriptors, all 32-bit coefficient patterns, concurrency, real JPEG + streams and other units are not covered +- This custom interpreter is itself test tooling rather than a formally verified + CPU implementation. Official objdiff, ctxdiff and full-DOL checks remain the + matching/integration authority + +Exact run output is preserved in `jpeg-remainder-differential-results.txt`. + +## Attempts + +Only strictly improved candidates without other function regressions were kept. +No declaration-order permutation search was performed. Failed/equal candidates +were restored. The one temporary compile error in the luminance-consumption +script was a duplicate local declaration; it was corrected and no broken +candidate was retained. + +### type-trials + +```text +TMCJPEGDEC_converterYUV411toRGBA8 signed-chroma-pointers: 82.603300 -> 82.500000; restore +TMCJPEGDEC_converterYUV411toRGBA8 signed-chroma-coefficients: 82.603300 -> 82.603300; restore +TMCJPEGDEC_converterYUV411toRGBA8 signed-chroma-domain: 82.603300 -> 81.570250; restore +TMCJPEGDEC_converterYUV411toRGBA8edge signed-chroma-pointers: 93.883930 -> 91.964290; restore +TMCJPEGDEC_converterYUV411toRGBA8edge signed-chroma-coefficients: 93.883930 -> 93.750000; restore +TMCJPEGDEC_converterYUV411toRGBA8edge signed-chroma-domain: 93.883930 -> 92.187500; restore +TMCJPEGDEC_converterYUV422toRGBA8 signed-chroma-pointers: 87.722980 -> 87.722980; restore +TMCJPEGDEC_converterYUV422toRGBA8 signed-chroma-coefficients: 87.722980 -> 87.722980; restore +TMCJPEGDEC_converterYUV422toRGBA8 signed-chroma-domain: 87.722980 -> 87.722980; restore +TMCJPEGDEC_converterYUV422toRGBA8 byte-luminance: 87.722980 -> 87.689186; restore +TMCJPEGDEC_converterYUV422toRGBA8edge signed-chroma-pointers: 90.088490 -> 90.088490; restore +TMCJPEGDEC_converterYUV422toRGBA8edge signed-chroma-coefficients: 90.088490 -> 90.088490; restore +TMCJPEGDEC_converterYUV422toRGBA8edge signed-chroma-domain: 90.088490 -> 88.628320; restore +TMCJPEGDEC_converterYUV422toRGBA8edge byte-luminance: 90.088490 -> 90.088490; restore +TMCJPEGDEC_converterYUV420toRGBA8 signed-chroma-pointers: 89.405230 -> 89.176470; restore +TMCJPEGDEC_converterYUV420toRGBA8 signed-chroma-coefficients: 89.405230 -> 89.307190; restore +TMCJPEGDEC_converterYUV420toRGBA8 signed-chroma-domain: 89.405230 -> 86.464050; restore +TMCJPEGDEC_converterYUV420toRGBA8 byte-luminance: 89.405230 -> 87.379080; restore +TMCJPEGDEC_converterYUV420toRGBA8edge signed-chroma-pointers: 87.400000 -> 80.666664; restore +TMCJPEGDEC_converterYUV420toRGBA8edge signed-chroma-domain: 87.400000 -> 80.666664; restore +TMCJPEGDEC_converterYUV211toRGBA8 signed-chroma-pointers: 91.153850 -> 91.153850; restore +TMCJPEGDEC_converterYUV211toRGBA8 signed-chroma-coefficients: 91.153850 -> 91.153850; restore +TMCJPEGDEC_converterYUV211toRGBA8 signed-chroma-domain: 91.153850 -> 91.153850; restore +TMCJPEGDEC_converterYUV211toRGBA8 byte-luminance: 91.153850 -> 91.153850; restore +TMCJPEGDEC_converterYUV211toRGBA8edge signed-chroma-pointers: 92.000000 -> 91.869570; restore +TMCJPEGDEC_converterYUV211toRGBA8edge signed-chroma-coefficients: 92.000000 -> 91.869570; restore +TMCJPEGDEC_converterYUV211toRGBA8edge signed-chroma-domain: 92.000000 -> 91.869570; restore +TMCJPEGDEC_converterYUV444toRGBA8 signed-chroma-pointers: 90.670105 -> 90.515465; restore +TMCJPEGDEC_converterYUV444toRGBA8 signed-chroma-coefficients: 90.670105 -> 90.515465; restore +TMCJPEGDEC_converterYUV444toRGBA8 signed-chroma-domain: 90.670105 -> 90.618550; restore +TMCJPEGDEC_converterYUV444toRGBA8 byte-luminance: 90.670105 -> 90.670105; restore +TMCJPEGDEC_converterYUV444toRGBA8edge signed-chroma-pointers: 88.909090 -> 88.909090; restore +TMCJPEGDEC_converterYUV444toRGBA8edge signed-chroma-coefficients: 88.909090 -> 88.909090; restore +TMCJPEGDEC_converterYUV444toRGBA8edge signed-chroma-domain: 88.909090 -> 87.645454; restore +TMCJPEGDEC_converterYUV444toRGBA8edge byte-luminance: 88.909090 -> 88.909090; restore +TMCJPEGDEC_converterYUV411toRGB565 signed-chroma-pointers: 90.599075 -> 90.599075; restore +TMCJPEGDEC_converterYUV411toRGB565 signed-chroma-coefficients: 90.599075 -> 90.599075; restore +TMCJPEGDEC_converterYUV411toRGB565 signed-chroma-domain: 90.599075 -> 90.599075; restore +TMCJPEGDEC_converterYUV422toRGB565 signed-chroma-pointers: 96.691180 -> 96.691180; restore +TMCJPEGDEC_converterYUV422toRGB565 signed-chroma-coefficients: 96.691180 -> 96.691180; restore +TMCJPEGDEC_converterYUV422toRGB565 signed-chroma-domain: 96.691180 -> 96.691180; restore +TMCJPEGDEC_converterYUV420toRGB565 signed-chroma-pointers: 94.609930 -> 94.609930; restore +TMCJPEGDEC_converterYUV420toRGB565 signed-chroma-coefficients: 94.609930 -> 94.609930; restore +TMCJPEGDEC_converterYUV420toRGB565 signed-chroma-domain: 94.609930 -> 94.609930; restore +TMCJPEGDEC_converterYUV420toRGB565 byte-luminance: 94.609930 -> 92.907800; restore +TMCJPEGDEC_converterYUV444toRGB565 signed-chroma-pointers: 97.967030 -> 97.362640; restore +TMCJPEGDEC_converterYUV444toRGB565 signed-chroma-coefficients: 97.967030 -> 97.802200; restore +TMCJPEGDEC_converterYUV444toRGB565 signed-chroma-domain: 97.967030 -> 97.340660; restore +TMCJPEGDEC_converterYUV444toRGB565 byte-luminance: 97.967030 -> 91.373630; restore +``` + +### idct-trials + +```text +TMCJPEGDEC_IdctBlock_Lumi nested-odd-output-sum: 82.315180 -> 79.708170; restore +TMCJPEGDEC_IdctBlock_Lumi explicit-ac-or-tree: 82.315180 -> 74.984436; restore +TMCJPEGDEC_IdctBlock_Lumi ac-coefficients-direct-condition: 82.315180 -> 74.984436; restore +TMCJPEGDEC_IdctBlock_Lumi nested-even-output-sum: 82.315180 -> 82.315180; restore +TMCJPEGDEC_IdctBlock_Lumi direct-product-shifts: 82.315180 -> 82.315180; restore +TMCJPEGDEC_IdctBlock_Col nested-odd-output-sum: 92.475770 -> 87.907486; restore +TMCJPEGDEC_IdctBlock_Col explicit-ac-or-tree: 92.475770 -> 90.651985; restore +TMCJPEGDEC_IdctBlock_Col ac-coefficients-direct-condition: 92.475770 -> 90.651985; restore +TMCJPEGDEC_IdctBlock_Col nested-even-output-sum: 92.475770 -> 92.475770; restore +TMCJPEGDEC_IdctBlock_Col direct-product-shifts: 92.475770 -> 92.475770; restore +``` + +### packing-trials + +```text +TMCJPEGDEC_converterYUV411toRGBA8 explicit-channel-clamp-branches: 82.603300 -> 61.962810; restore +TMCJPEGDEC_converterYUV411toRGBA8 structured-nonnegative-clamp: 82.603300 -> 68.442150; restore +TMCJPEGDEC_converterYUV411toRGBA8edge explicit-channel-clamp-branches: 93.883930 -> 80.008930; restore +TMCJPEGDEC_converterYUV411toRGBA8edge structured-nonnegative-clamp: 93.883930 -> 85.714290; restore +TMCJPEGDEC_converterYUV422toRGBA8 explicit-channel-clamp-branches: 87.722980 -> 71.222980; restore +TMCJPEGDEC_converterYUV422toRGBA8 structured-nonnegative-clamp: 87.722980 -> 75.533780; restore +TMCJPEGDEC_converterYUV422toRGBA8edge explicit-channel-clamp-branches: 90.088490 -> 76.097340; restore +TMCJPEGDEC_converterYUV422toRGBA8edge structured-nonnegative-clamp: 90.088490 -> 82.123890; restore +TMCJPEGDEC_converterYUV420toRGBA8 explicit-channel-clamp-branches: 89.405230 -> 73.372550; restore +TMCJPEGDEC_converterYUV420toRGBA8 structured-nonnegative-clamp: 89.405230 -> 76.797386; restore +TMCJPEGDEC_converterYUV420toRGBA8edge explicit-channel-clamp-branches: 87.400000 -> 77.841670; restore +TMCJPEGDEC_converterYUV420toRGBA8edge structured-nonnegative-clamp: 87.400000 -> 82.400000; restore +TMCJPEGDEC_converterYUV211toRGBA8 explicit-channel-clamp-branches: 91.153850 -> 74.701920; restore +TMCJPEGDEC_converterYUV211toRGBA8 structured-nonnegative-clamp: 91.153850 -> 82.211540; restore +TMCJPEGDEC_converterYUV211toRGBA8edge explicit-channel-clamp-branches: 92.000000 -> 77.730440; restore +TMCJPEGDEC_converterYUV211toRGBA8edge structured-nonnegative-clamp: 92.000000 -> 84.043480; restore +TMCJPEGDEC_converterYUV444toRGBA8 explicit-channel-clamp-branches: 90.670105 -> 73.030930; restore +TMCJPEGDEC_converterYUV444toRGBA8 structured-nonnegative-clamp: 90.670105 -> 81.082470; restore +TMCJPEGDEC_converterYUV444toRGBA8edge explicit-channel-clamp-branches: 88.909090 -> 75.463640; restore +TMCJPEGDEC_converterYUV444toRGBA8edge structured-nonnegative-clamp: 88.909090 -> 81.000000; restore +TMCJPEGDEC_converterYUV411toRGB565 normalize-packed-channels: 90.599075 -> 87.004610; restore +TMCJPEGDEC_converterYUV411toRGB565 group-red-green-packing: 90.599075 -> 88.755760; restore +TMCJPEGDEC_converterYUV411toRGB565 pack-before-output-address: 90.599075 -> 90.599075; restore +TMCJPEGDEC_converterYUV411toRGB565 explicit-channel-clamp-branches: 90.599075 -> 59.511520; restore +TMCJPEGDEC_converterYUV411toRGB565 structured-nonnegative-clamp: 90.599075 -> 71.640560; restore +TMCJPEGDEC_converterYUV411toRGB565edge normalize-packed-channels: 95.377360 -> 93.820755; restore +TMCJPEGDEC_converterYUV411toRGB565edge group-red-green-packing: 95.377360 -> 93.613205; restore +TMCJPEGDEC_converterYUV411toRGB565edge pack-before-output-address: 95.377360 -> 95.377360; restore +TMCJPEGDEC_converterYUV411toRGB565edge explicit-channel-clamp-branches: 95.377360 -> 79.801890; restore +TMCJPEGDEC_converterYUV411toRGB565edge structured-nonnegative-clamp: 95.377360 -> 86.179245; restore +TMCJPEGDEC_converterYUV422toRGB565 explicit-channel-clamp-branches: 96.691180 -> 78.507355; restore +TMCJPEGDEC_converterYUV422toRGB565 structured-nonnegative-clamp: 96.691180 -> 79.044120; restore +TMCJPEGDEC_converterYUV420toRGB565 normalize-packed-channels: 94.609930 -> 90.248230; restore +TMCJPEGDEC_converterYUV420toRGB565 group-red-green-packing: 94.609930 -> 91.560290; restore +TMCJPEGDEC_converterYUV420toRGB565 pack-before-output-address: 94.609930 -> 94.609930; restore +TMCJPEGDEC_converterYUV420toRGB565 explicit-channel-clamp-branches: 94.609930 -> 73.127660; restore +TMCJPEGDEC_converterYUV420toRGB565 structured-nonnegative-clamp: 94.609930 -> 76.751770; restore +TMCJPEGDEC_converterYUV420toRGB565edge normalize-packed-channels: 94.692986 -> 89.912280; restore +TMCJPEGDEC_converterYUV420toRGB565edge group-red-green-packing: 94.692986 -> 91.798250; restore +TMCJPEGDEC_converterYUV420toRGB565edge pack-before-output-address: 94.692986 -> 94.692986; restore +TMCJPEGDEC_converterYUV420toRGB565edge explicit-channel-clamp-branches: 94.692986 -> 81.894740; restore +TMCJPEGDEC_converterYUV420toRGB565edge structured-nonnegative-clamp: 94.692986 -> 86.315790; restore +TMCJPEGDEC_converterYUV444toRGB565 normalize-packed-channels: 97.967030 -> 96.648350; restore +TMCJPEGDEC_converterYUV444toRGB565 group-red-green-packing: 97.967030 -> 98.681320; RETAIN +TMCJPEGDEC_converterYUV444toRGB565 explicit-channel-clamp-branches: 98.681320 -> 85.120880; restore +TMCJPEGDEC_converterYUV444toRGB565 structured-nonnegative-clamp: 98.681320 -> 82.802200; restore +``` + +### control-trials + +```text +structured-col-low-mode-first: {'TMCJPEGDEC_IdctBlock_Lumi': 82.31518, 'TMCJPEGDEC_IdctBlock_Col': 92.47577} -> {'TMCJPEGDEC_IdctBlock_Lumi': 82.31518, 'TMCJPEGDEC_IdctBlock_Col': 92.47577}; restore +structured-col-high-mode-first: {'TMCJPEGDEC_IdctBlock_Lumi': 82.31518, 'TMCJPEGDEC_IdctBlock_Col': 92.47577} -> {'TMCJPEGDEC_IdctBlock_Lumi': 82.31518, 'TMCJPEGDEC_IdctBlock_Col': 50.55066}; restore +shared-full-row-butterfly: {'TMCJPEGDEC_IdctBlock_Lumi': 82.31518, 'TMCJPEGDEC_IdctBlock_Col': 92.47577} -> {'TMCJPEGDEC_IdctBlock_Lumi': 83.05058, 'TMCJPEGDEC_IdctBlock_Col': 92.47577}; RETAIN +``` + +### luma-reuse-trials + +```text +TMCJPEGDEC_converterYUV422toRGBA8edge consume-luminance-as-blue: 90.088490 -> 90.088490; restore +TMCJPEGDEC_converterYUV422toRGBA8edge consume-luminance-as-red: 90.088490 -> 89.955750; restore +TMCJPEGDEC_converterYUV422toRGBA8edge consume-luminance-as-green: 90.088490 -> 90.088490; restore +TMCJPEGDEC_converterYUV420toRGBA8edge consume-luminance-as-blue: 87.400000 -> 87.400000; restore +TMCJPEGDEC_converterYUV420toRGBA8edge consume-luminance-as-red: 87.400000 -> 87.458336; RETAIN +TMCJPEGDEC_converterYUV211toRGBA8 consume-luminance-as-blue: 91.153850 -> 91.153850; restore +TMCJPEGDEC_converterYUV211toRGBA8 consume-luminance-as-red: 91.153850 -> 86.067310; restore +TMCJPEGDEC_converterYUV211toRGBA8 consume-luminance-as-green: 91.153850 -> 78.307690; restore +TMCJPEGDEC_converterYUV211toRGBA8edge consume-luminance-as-blue: 92.000000 -> 91.565216; restore +TMCJPEGDEC_converterYUV211toRGBA8edge consume-luminance-as-red: 92.000000 -> 87.617390; restore +TMCJPEGDEC_converterYUV211toRGBA8edge consume-luminance-as-green: 92.000000 -> 79.547830; restore +TMCJPEGDEC_converterYUV444toRGBA8 consume-luminance-as-blue: 90.670105 -> 90.670105; restore +TMCJPEGDEC_converterYUV444toRGBA8 consume-luminance-as-red: 90.670105 -> 85.216490; restore +TMCJPEGDEC_converterYUV444toRGBA8 consume-luminance-as-green: 90.670105 -> 74.577320; restore +TMCJPEGDEC_converterYUV444toRGBA8edge consume-luminance-as-blue: 88.909090 -> 88.909090; restore +TMCJPEGDEC_converterYUV444toRGBA8edge consume-luminance-as-red: 88.909090 -> 85.645454; restore +TMCJPEGDEC_converterYUV444toRGBA8edge consume-luminance-as-green: 88.909090 -> 78.772730; restore +TMCJPEGDEC_converterYUV411toRGB565edge consume-luminance-as-blue: 95.377360 -> 91.396225; restore +TMCJPEGDEC_converterYUV411toRGB565edge consume-luminance-as-red: 95.377360 -> 93.707550; restore +TMCJPEGDEC_converterYUV411toRGB565edge consume-luminance-as-green: 95.377360 -> 95.330185; restore +TMCJPEGDEC_converterYUV444toRGB565 consume-luminance-as-blue: 98.681320 -> 98.681320; restore +TMCJPEGDEC_converterYUV444toRGB565 consume-luminance-as-red: 98.681320 -> 98.714290; RETAIN +``` + +### rgb444-trials + +```text +TMCJPEGDEC_converterYUV444toRGB565 reused-luminance-green-red-group: 98.714290 -> 89.923080; restore +TMCJPEGDEC_converterYUV444toRGB565 reused-luminance-blue-green-group: 98.714290 -> 98.549450; restore +TMCJPEGDEC_converterYUV444toRGB565 reused-luminance-red-blue-group: 98.714290 -> 90.945050; restore +TMCJPEGDEC_converterYUV444toRGB565 reused-luminance-red-green-group: 98.714290 -> 98.714290; restore +TMCJPEGDEC_converterYUV444toRGB565 separate-luminance-green-red-group: 98.714290 -> 98.131870; restore +TMCJPEGDEC_converterYUV444toRGB565 separate-luminance-blue-green-group: 98.714290 -> 98.516490; restore +TMCJPEGDEC_converterYUV444toRGB565 separate-luminance-red-blue-group: 98.714290 -> 91.406590; restore +TMCJPEGDEC_converterYUV444toRGB565 separate-luminance-red-green-group: 98.714290 -> 98.681320; restore +``` diff --git a/tools/decomp-assist/jpeg-remainder-differential-results.txt b/tools/decomp-assist/jpeg-remainder-differential-results.txt new file mode 100644 index 00000000..6e79cbe9 --- /dev/null +++ b/tools/decomp-assist/jpeg-remainder-differential-results.txt @@ -0,0 +1,29 @@ +TMCJPEGDEC_converterYUV411toRGBA8: 100 cases PASS; instruction coverage source 242/242, original 242/242 +TMCJPEGDEC_converterYUV411toRGBA8edge: 400 cases PASS; instruction coverage source 112/112, original 112/112 +TMCJPEGDEC_converterYUV422toRGBA8: 100 cases PASS; instruction coverage source 148/148, original 148/148 +TMCJPEGDEC_converterYUV422toRGBA8edge: 400 cases PASS; instruction coverage source 113/113, original 113/113 +TMCJPEGDEC_converterYUV420toRGBA8: 100 cases PASS; instruction coverage source 153/153, original 153/153 +TMCJPEGDEC_converterYUV420toRGBA8edge: 400 cases PASS; instruction coverage source 120/120, original 120/120 +TMCJPEGDEC_converterYUV211toRGBA8: 100 cases PASS; instruction coverage source 104/104, original 104/104 +TMCJPEGDEC_converterYUV211toRGBA8edge: 400 cases PASS; instruction coverage source 115/115, original 115/115 +TMCJPEGDEC_converterYUV444toRGBA8: 100 cases PASS; instruction coverage source 97/97, original 97/97 +TMCJPEGDEC_converterYUV444toRGBA8edge: 400 cases PASS; instruction coverage source 110/110, original 110/110 +TMCJPEGDEC_converterYUV400toRGBA8: 100 cases PASS; instruction coverage source 57/76, original 57/76 +TMCJPEGDEC_converterYUV400toRGBA8edge: 400 cases PASS; instruction coverage source 70/89, original 70/89 +TMCJPEGDEC_set_converterRGBA8: 1024 cases PASS; instruction coverage source 168/169, original 169/170 +TMCJPEGDEC_converterYUV411toRGB565: 100 cases PASS; instruction coverage source 217/217, original 217/217 +TMCJPEGDEC_converterYUV411toRGB565edge: 400 cases PASS; instruction coverage source 106/106, original 106/106 +TMCJPEGDEC_converterYUV422toRGB565: 100 cases PASS; instruction coverage source 136/136, original 136/136 +TMCJPEGDEC_converterYUV422toRGB565edge: 400 cases PASS; instruction coverage source 107/107, original 107/107 +TMCJPEGDEC_converterYUV420toRGB565: 100 cases PASS; instruction coverage source 141/141, original 141/141 +TMCJPEGDEC_converterYUV420toRGB565edge: 400 cases PASS; instruction coverage source 114/114, original 114/114 +TMCJPEGDEC_converterYUV211toRGB565: 100 cases PASS; instruction coverage source 98/98, original 98/98 +TMCJPEGDEC_converterYUV211toRGB565edge: 400 cases PASS; instruction coverage source 109/109, original 109/109 +TMCJPEGDEC_converterYUV444toRGB565: 100 cases PASS; instruction coverage source 91/91, original 91/91 +TMCJPEGDEC_converterYUV444toRGB565edge: 400 cases PASS; instruction coverage source 104/104, original 104/104 +TMCJPEGDEC_converterYUV400toRGB565: 100 cases PASS; instruction coverage source 51/70, original 51/70 +TMCJPEGDEC_converterYUV400toRGB565edge: 400 cases PASS; instruction coverage source 64/83, original 64/83 +TMCJPEGDEC_set_converterRGB565: 1024 cases PASS; instruction coverage source 168/169, original 169/170 +TMCJPEGDEC_IdctBlock_Lumi: 704 cases PASS; instruction coverage source 257/257, original 257/257 +TMCJPEGDEC_IdctBlock_Col: 704 cases PASS; instruction coverage source 454/454, original 454/454 +TOTAL: 9456 differential cases PASS; GPR14-GPR31, stack pointer and LR preserved diff --git a/tools/decomp-assist/jpeg_differential_audit.py b/tools/decomp-assist/jpeg_differential_audit.py new file mode 100644 index 00000000..1e12c6f6 --- /dev/null +++ b/tools/decomp-assist/jpeg_differential_audit.py @@ -0,0 +1,319 @@ +#!/usr/bin/env python3 +"""Read-only, bounded PPC integer interpreter for differential JPEG checks. +Loads source and original ELF .text; never copies original code into sources. +Only memset is external. No FPU/system instructions are supported. +""" +import random +import re +import struct +import zlib + +import capstone +from elftools.elf.elffile import ELFFile +U32 = 0xFFFFFFFF + +def signed(x): + x &= U32 + return x - 0x100000000 if x & 0x80000000 else x + +def rotate(x, n): + return (x << n | x >> (32 - n if n else 32)) & U32 + +def mask(b, e): + return sum((1 << 31 - i for i in range(32) if b <= i <= e or (b > e and (i >= b or i <= e)))) + +def load_object(path): + with open(path, 'rb') as f: + e = ELFFile(f) + section = e.get_section_by_name('.text') + data = bytearray(section.data()) + syms = e.get_section_by_name('.symtab') + calls = {} + symbol_addresses = {} + rel = e.get_section_by_name('.rela.text') + if rel: + for r in rel.iter_relocations(): + name = syms.get_symbol(r['r_info_sym']).name + off = r['r_offset'] + typ = r['r_info_type'] + addr = 0x800000 + (zlib.crc32(name.encode()) & 0x7FFF) * 4 + r['r_addend'] + assert addr not in symbol_addresses or symbol_addresses[addr] == name + symbol_addresses[addr] = name + if typ == 10: + calls[off] = name + elif typ == 4: + data[off:off + 2] = struct.pack('>H', addr & 65535) + elif typ == 6: + data[off:off + 2] = struct.pack('>H', addr + 0x8000 >> 16 & 65535) + else: + raise ValueError(('relocation', typ)) + dis = capstone.Cs(capstone.CS_ARCH_PPC, capstone.CS_MODE_32 | capstone.CS_MODE_BIG_ENDIAN) + funcs = {} + for sym in syms.iter_symbols(): + if sym['st_info']['type'] != 'STT_FUNC' or not sym['st_size']: + continue + start = sym['st_value'] + size = sym['st_size'] + inst = {} + for i in dis.disasm(bytes(data[start:start + size]), start): + ops = [] + for o in i.op_str.split(', '): + if not o: + continue + if o.startswith('cr'): + ops.append(('cr', int(o[2:]))) + elif o.startswith('r'): + ops.append(int(o[1:])) + elif '(' in o: + off, reg = o.split('(') + ops.append((int(off, 0), int(reg[1:-1]))) + else: + ops.append(int(o, 0)) + inst[i.address] = (i.mnemonic, ops) + funcs[sym.name] = (start, inst, calls) + return funcs + +def run(fn, initial, args): + pc, ins, calls = fn + mem = bytearray(initial) + r = [i * 1122867 & U32 for i in range(32)] + r[1] = 0x3F000 + r[3:3 + len(args)] = args + saved_registers = r[14:].copy() + cr = [0] * 8 + ctr = 0 + lr = 0 + steps = 0 + coverage = set() + + def ld(a, n): + assert 0 <= a <= len(mem) - n, hex(a) + return int.from_bytes(mem[a:a + n], 'big') + + def st(a, v, n): + assert 0 <= a <= len(mem) - n, hex(a) + mem[a:a + n] = (v & (1 << 8 * n) - 1).to_bytes(n, 'big') + while True: + steps += 1 + if steps > 100000: + raise RuntimeError('step bound') + coverage.add(pc) + op, a = ins[pc] + pc += 4 + dot = op.endswith('.') + op = op.rstrip('.') + val = None + if op == 'blr': + break + elif op == 'bl': + assert calls.get(pc - 4) == 'memset', calls.get(pc - 4) + assert r[3] + r[5] <= len(mem) + mem[r[3]:r[3] + r[5]] = bytes([r[4] & 255]) * r[5] + # Model ABI-permitted call clobbers rather than preserving temporaries. + for reg in [0] + list(range(4, 13)): + r[reg] = (0xBAD00000 + reg) & U32 + for field in [0, 1, 5, 6, 7]: + cr[field] = -1 + lr = pc + elif op == 'b': + pc = a[0] + elif op == 'bdnz': + ctr = ctr - 1 & U32 + if ctr: + pc = a[0] + elif op in ['beq', 'bne', 'ble', 'bge', 'blt', 'bgt']: + c = cr[a[0][1]] if len(a) > 1 else cr[0] + if {'beq': c == 0, 'bne': c != 0, 'ble': c <= 0, 'bge': c >= 0, 'blt': c < 0, 'bgt': c > 0}[op]: + pc = a[-1] + elif op in ['cmpwi', 'cmpw', 'cmplw']: + c = a[0][1] if isinstance(a[0], tuple) else 0 + a = ins[pc - 4][1] + if isinstance(a[0], tuple): + a = a[1:] + x = r[a[0]] + y = a[1] if op == 'cmpwi' else r[a[1]] + if op != 'cmplw': + x = signed(x) + y = signed(y) + cr[c] = (x > y) - (x < y) + elif op in ['lbz', 'lhz', 'lwz', 'lmw']: + off, reg = a[1] + addr = (r[reg] if reg else 0) + off & U32 + if op == 'lmw': + for j in range(a[0], 32): + r[j] = ld(addr + (j - a[0]) * 4, 4) + else: + r[a[0]] = ld(addr, {'lbz': 1, 'lhz': 2, 'lwz': 4}[op]) + elif op in ['stb', 'sth', 'stw', 'stwu', 'stmw']: + off, reg = a[1] + addr = (r[reg] if reg else 0) + off & U32 + if op == 'stmw': + for j in range(a[0], 32): + st(addr + (j - a[0]) * 4, r[j], 4) + else: + st(addr, r[a[0]], {'stb': 1, 'sth': 2, 'stw': 4, 'stwu': 4}[op]) + if op == 'stwu': + r[reg] = addr + elif op in ['stbx', 'sthx']: + st((r[a[1]] if a[1] else 0) + r[a[2]] & U32, r[a[0]], 1 if op == 'stbx' else 2) + elif op == 'mflr': + r[a[0]] = lr + elif op == 'mtlr': + lr = r[a[0]] + elif op == 'mtctr': + ctr = r[a[0]] + else: + d = a[0] + if op == 'li': + val = a[1] + elif op == 'lis': + val = a[1] << 16 + elif op == 'mr': + val = r[a[1]] + elif op == 'add': + val = r[a[1]] + r[a[2]] + elif op == 'addi': + val = (r[a[1]] if a[1] else 0) + a[2] + elif op == 'addis': + val = (r[a[1]] if a[1] else 0) + (a[2] << 16) + elif op == 'subf': + val = r[a[2]] - r[a[1]] + elif op == 'subfic': + val = a[2] - r[a[1]] + elif op == 'mulli': + val = r[a[1]] * a[2] + elif op == 'mullw': + val = r[a[1]] * r[a[2]] + elif op == 'divw': + x = signed(r[a[1]]) + y = signed(r[a[2]]) + val = abs(x) // abs(y) * (-1 if (x < 0) != (y < 0) else 1) + elif op == 'or': + val = r[a[1]] | r[a[2]] + elif op == 'andc': + val = r[a[1]] & ~r[a[2]] + elif op == 'andi': + val = r[a[1]] & a[2] + elif op == 'neg': + val = -r[a[1]] + elif op == 'extsb': + val = (r[a[1]] & 255) - (256 if r[a[1]] & 128 else 0) + elif op == 'srawi': + val = signed(r[a[1]]) >> a[2] + elif op == 'srwi': + val = r[a[1]] >> a[2] + elif op == 'slwi': + val = r[a[1]] << a[2] + elif op == 'clrlwi': + val = r[a[1]] & (1 << 32 - a[2]) - 1 + elif op == 'rotlwi': + val = rotate(r[a[1]], a[2]) + elif op == 'rlwinm': + val = rotate(r[a[1]], a[2]) & mask(a[3], a[4]) + else: + raise ValueError((hex(pc - 4), op, a)) + r[d] = val & U32 + if dot: + cr[0] = (signed(val) > 0) - (signed(val) < 0) + assert r[1] == 0x3F000, 'Stack pointer not restored' + assert lr == 0, 'Link register not restored' + assert r[14:] == saved_registers, 'Nonvolatile registers not restored' + return (mem, r[3], coverage) + +def put(m, a, v, n=4): + m[a:a + n] = (v & (1 << 8 * n) - 1).to_bytes(n, 'big') + +def compare(src, orig, name, m, args, case, returns=False): + a, ra, ca = run(src[name], m, args) + b, rb, cb = run(orig[name], m, args) + # The stack can differ because each implementation owns its frame. + if a[:0x3E000] != b[:0x3E000]: + diff = [hex(i) for i in range(0x3E000) if a[i] != b[i]][:12] + raise AssertionError((name, case, 'memory mismatch', diff)) + if returns and ra != rb: + raise AssertionError((name, case, 'return', ra, rb)) + return (ca, cb) + +def main(): + total_cases = 0 + rng = random.Random(449529) + root = 'build/43U/{}/libs/RVLMiddleware/TMC_JPEG/src/{}' + for unit in ['texturecvtr/Texture_MCUtoRGBA8', 'texturecvtr/Texture_MCUtoRGB565', 'buffer/idct_block_var']: + src = load_object(root.format('src', unit) + '.o') + orig = load_object(root.format('obj', unit) + '.o') + assert src.keys() == orig.keys(), (unit, 'Function sets differ') + assert len(src) == (2 if unit.startswith('buffer/') else 13) + for name in src: + n = 0 + sa = set() + sb = set() + if 'IdctBlock' in name: + for rows in range(1, 9): + for cols in [1, 2, 3, 8]: + for pattern in range(22): + m = bytearray([0xA5]) * 0x40000 + block = [0] * 64 + for y in range(rows): + for x in range(cols): + if pattern < 6: + block[y * 8 + x] = [0, 1, -1, 0x3FFFF, 0x40000, -0x40000][pattern] if x == 0 and y == 0 else 0 + elif pattern < 14: + block[y * 8 + x] = rng.randrange(-10000, 10001) if rng.randrange(4) == 0 else 0 + else: + block[y * 8 + x] = rng.randrange(-150000, 150001) + for i, v in enumerate(block): + put(m, 0x1000 + i * 4, v) + pitch = [8, 16, 32][pattern % 3] + args = [0x1000, 0x2000, pitch, rows << 4 | cols] + ca, cb = compare(src, orig, name, m, args, (rows, cols, pattern)) + sa |= ca + sb |= cb + n += 1 + elif 'set_converter' in name: + for component in range(256): + for mode in [1, 2, 4, 8]: + m = bytearray([0xA5]) * 0x40000 + work = 0x1000 + state = 0x4000 + put(m, work + 0x17FC, component, 1) + put(m, work + 0x19DC, mode, 1) + put(m, work + 0x19E4, state) + put(m, state + 0x24, rng.randrange(65536), 2) + put(m, state + 0x26, rng.randrange(65536), 2) + ca, cb = compare(src, orig, name, m, [work], (component, mode), True) + sa |= ca + sb |= cb + n += 1 + else: + sampling = re.search('YUV(\\d+)to', name)[1] + w, h = {'411': (32, 8), '422': (16, 8), '420': (16, 16), '211': (8, 16), '444': (8, 8), '400': (8, 8)}[sampling] + for scale in [1, 2, 4, 8]: + for pattern in range(25): + for border in range(4 if name.endswith('edge') else 1): + m = bytearray([0xA5]) * 0x40000 + work = 0x1000 + state = 0x4000 + tex = 0x8000 + x = 32 * (pattern % 2) + y = 16 * (pattern % 3) + ww = w // scale + hh = h // scale + put(m, work + 0x19E4, state) + put(m, state + 0x20, scale, 1) + put(m, state + 0x2C, 96) + put(m, state + 0x48, tex) + put(m, state + 0x18, x if border & 1 else x + 1) + put(m, state + 0x1C, y if border & 2 else y + 1) + put(m, state + 0x16, pattern % (ww + 1), 1) + put(m, state + 0x17, pattern % (hh + 1), 1) + for i in range(0x184): + m[work + 0x1858 + i] = [0, 127, 128, 255][pattern % 4] if pattern < 4 else rng.randrange(256) + ca, cb = compare(src, orig, name, m, [work, x, y], (scale, pattern, border)) + sa |= ca + sb |= cb + n += 1 + total_cases += n + print(f'{name}: {n} cases PASS; instruction coverage source {len(sa)}/{len(src[name][1])}, original {len(sb)}/{len(orig[name][1])}', flush=True) + print(f'TOTAL: {total_cases} differential cases PASS; GPR14-GPR31, stack pointer and LR preserved') +if __name__ == '__main__': + main()