diff --git a/configure.py b/configure.py index f1330924..3af16b9e 100644 --- a/configure.py +++ b/configure.py @@ -1049,7 +1049,7 @@ config.libs = [ Object(Matching, "net/hmac.c"), Object(Matching, "net/neterrorcode.c"), Object(Matching, "net/NETVersion.c"), - Object(NonMatching, "net/aes.c"), + Object(Matching, "net/aes.c"), Object(Matching, "net/wireless_macaddr.c"), ] ), diff --git a/include/global/decomp/ide.h b/include/global/decomp/ide.h index 40bdf89f..b169e003 100644 --- a/include/global/decomp/ide.h +++ b/include/global/decomp/ide.h @@ -12,6 +12,7 @@ extern void __sync(); extern unsigned int __cntlzw(unsigned int value); extern void __memclr(void* buffer, int len); extern unsigned int __rlwimi(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e); +extern unsigned int __rlwinm(unsigned int value, unsigned int shift, unsigned int maskBegin, unsigned int maskEnd); extern void* __alloca(unsigned int blockSize); extern int __abs(int n); 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 159e34e5..f3b93e06 100644 --- a/libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var.c +++ b/libs/RVLMiddleware/TMC_JPEG/src/buffer/idct_block_var.c @@ -33,391 +33,417 @@ static s32 scalingClampU8(s32 value) { void TMCJPEGDEC_IdctBlock_Lumi(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigzag) { s32 workspace[64]; - s32* workspaceCursor; - s32 evenBaseSum; - s32 evenRotation; + s32 pitch4; + s32* dst; + s32* src; + s32 pitch7; + s32 rowCount; + s32 pitch6; + s32 pitch5; + s32 pitch3; + s32 pitch2; + s32 processed; s32 column; - s32 evenOutput3; - s32 coefficient2; - s32 coefficient1; - s32 coefficient4; - s32 coefficient5; - s32 coefficient6; - s32 coefficient7; - s32 oddOutput0; - s32 m; - s32 evenBaseDifference; - s32 processedCoefficients; - s32 z; - s32 evenOutput1; - s32 oddOutput3; - s32 oddLowRotation; - s32 oddHighRotation; - s32 evenRotationSum; - s32 r; - s32 oddOutput1; - s32 oddSumRotation; - s32 oddSumDifference; - s32 oddDifferenceSum; - s32 columnsRemaining; - s32 oddOutput2; - s32 evenOutput0; - s32 evenOutput2; - s32 coefficient3; - r = (zigzag >> 4) * 8; - workspaceCursor = workspace; - processedCoefficients = 0; - { - for (; processedCoefficients < r; processedCoefficients += 8) { - u32 acBits; - coefficient4 = block[4]; - coefficient6 = block[6]; - coefficient2 = block[2]; - coefficient1 = block[1]; - coefficient7 = block[7]; - coefficient5 = block[5]; - coefficient3 = block[3]; - - acBits = (u32)coefficient4; - acBits |= (u32)coefficient6; - acBits |= (u32)coefficient2; - acBits |= (u32)coefficient1; - acBits |= (u32)coefficient7; - acBits |= (u32)coefficient5; - acBits |= (u32)coefficient3; - if (acBits == 0) { - s32 sample = block[0]; - workspaceCursor[7] = sample; - workspaceCursor[6] = sample; - workspaceCursor[5] = sample; - workspaceCursor[4] = sample; - workspaceCursor[3] = sample; - workspaceCursor[2] = sample; - workspaceCursor[1] = sample; - workspaceCursor[0] = sample; - } else { - s32 oddLowSum; - s32 oddHighSum; - s32 oddLowDifference; - s32 oddHighDifference; - s32 dcValue; - - evenRotation = coefficient2 - coefficient6; - oddLowSum = coefficient5 + coefficient3; - oddLowDifference = coefficient5 - coefficient3; - oddHighSum = coefficient1 + coefficient7; - oddHighDifference = coefficient1 - coefficient7; - evenRotationSum = coefficient6 + coefficient2; - evenRotation = evenRotation * 0xB5 >> 8; - oddSumDifference = oddHighSum - oddLowSum; - oddDifferenceSum = oddLowDifference + oddHighDifference; - dcValue = block[0]; - evenBaseDifference = dcValue - coefficient4; - oddSumRotation = oddSumDifference * 0xB5 >> 8; - evenBaseSum = dcValue + coefficient4; - evenRotationSum = evenRotation + evenRotationSum; - evenOutput1 = evenBaseDifference + evenRotation; - z = oddDifferenceSum * 0x62 >> 8; - evenOutput0 = evenBaseSum + evenRotationSum; - evenOutput3 = evenBaseSum - evenRotationSum; - oddHighRotation = oddHighDifference * 0x14E >> 8; - evenOutput2 = evenBaseDifference - evenRotation; - m = oddHighRotation - z; - oddLowSum += m; - oddOutput0 = oddHighSum + oddLowSum; - oddOutput1 = oddSumRotation + m; - workspaceCursor[0] = evenOutput0 + oddOutput0; - oddLowRotation = oddLowDifference * 0x8B; - workspaceCursor[7] = evenOutput0 - oddOutput0; - oddLowRotation >>= 8; - workspaceCursor[1] = evenOutput1 + oddOutput1; - oddOutput3 = z + oddLowRotation; - oddOutput2 = oddOutput3 + oddSumRotation; - workspaceCursor[6] = evenOutput1 - oddOutput1; - workspaceCursor[2] = evenOutput2 + oddOutput2; - workspaceCursor[5] = evenOutput2 - oddOutput2; - workspaceCursor[3] = evenOutput3 + oddOutput3; - workspaceCursor[4] = evenOutput3 - oddOutput3; - } - block += 8; - workspaceCursor += 8; + rowCount = (zigzag >> 4) * 8; + dst = workspace; + for (processed = 0; processed < rowCount; processed += 8) { + s32 c1; + s32 c2; + int acBits; + s32 c7; + s32 c5; + s32 c4; + s32 c3; + s32 c6; + c4 = block[4]; + c6 = block[6]; + c2 = block[2]; + c1 = block[1]; + c7 = block[7]; + c5 = block[5]; + c3 = block[3]; + acBits = c4 | c6; + acBits = c2 | acBits; + acBits = c1 | acBits; + acBits = c7 | acBits; + acBits = c5 | acBits; + acBits = c3 | acBits; + if (acBits == 0) { + s32 dc = block[0]; + dst[7] = dc; + dst[6] = dc; + dst[5] = dc; + dst[4] = dc; + dst[3] = dc; + dst[2] = dc; + dst[1] = dc; + dst[0] = dc; + } else { + s32 dc; + s32 evenSum; + s32 oddSumRotation; + s32 oddOutput0; + s32 evenRotationSum; + s32 evenRotation; + s32 oddLowDifference; + s32 oddDifferenceRotation; + s32 evenOutput3; + s32 oddOutput1; + s32 oddHighSum; + s32 oddOutput2; + s32 evenBaseSum; + s32 evenOutput0; + s32 oddDifferenceSum; + s32 evenOutput2; + s32 oddMix; + s32 oddLowRotation; + s32 oddOutput3; + s32 evenOutput1; + s32 oddHighRotation; + s32 oddLowSum; + s32 oddSumDifference; + s32 evenBaseDifference; + s32 oddHighDifference; + oddHighSum = c1 + c7; + dc = block[0]; + evenSum = c6 + c2; + oddLowDifference = c5 - c3; + oddHighDifference = c1 - c7; + evenRotation = c2 - c6; + c2 = evenRotation * 0xB5 >> 8; + evenBaseDifference = dc - c4; + oddLowSum = c5 + c3; + oddSumDifference = oddHighSum - oddLowSum; + oddSumRotation = oddSumDifference * 0xB5 >> 8; + oddDifferenceSum = oddLowDifference + oddHighDifference; + oddDifferenceRotation = oddDifferenceSum * 0x62 >> 8; + oddLowRotation = oddLowDifference * 0x8B; + oddHighRotation = oddHighDifference * 0x14E >> 8; + evenRotationSum = c2 + evenSum; + evenOutput1 = evenBaseDifference + c2; + oddLowRotation >>= 8; + evenBaseSum = dc + c4; + oddMix = oddHighRotation - oddDifferenceRotation; + evenOutput2 = evenBaseDifference - c2; + oddOutput1 = oddSumRotation + oddMix; + evenOutput3 = evenBaseSum - evenRotationSum; + oddLowSum += oddMix; + evenOutput0 = evenBaseSum + evenRotationSum; + oddOutput0 = oddHighSum + oddLowSum; + oddOutput3 = oddDifferenceRotation + oddLowRotation; + oddOutput2 = oddOutput3 + oddSumRotation; + dst[0] = evenOutput0 + oddOutput0; + dst[7] = evenOutput0 - oddOutput0; + dst[1] = evenOutput1 + oddOutput1; + dst[6] = evenOutput1 - oddOutput1; + dst[2] = evenOutput2 + oddOutput2; + dst[5] = evenOutput2 - oddOutput2; + dst[3] = evenOutput3 + oddOutput3; + dst[4] = evenOutput3 - oddOutput3; } + block += 8; + dst += 8; } - workspaceCursor = workspace + processedCoefficients; - for (; processedCoefficients <= 0x38; processedCoefficients += 8) { - memset(workspaceCursor, 0, 0x20); - workspaceCursor += 8; + dst = workspace + processed; + for (; processed <= 56; processed += 8) { + memset(dst, 0, 32); + dst += 8; } - r = pitch * 8 - pitch; - z = pitch * 4; - m = pitch * 2; - workspaceCursor = workspace + 7; - for (columnsRemaining = 8, column = 7; columnsRemaining > 0; columnsRemaining--, column--) { - u32 acBits; + src = workspace + 7; + pitch4 = pitch * 4; + pitch5 = pitch4 + pitch; + pitch3 = pitch4 - pitch; + pitch2 = pitch * 2; + pitch6 = pitch * 6; + pitch7 = pitch * 8 - pitch; + for (column = 7; column >= 0; column--) { + s32 c1; + s32 c2; + s32 c5; + s32 c3; + s32 c6; + s32 c4; + s32 c7; + int acBits; u8* output; - - coefficient4 = workspaceCursor[0x20]; - coefficient6 = workspaceCursor[0x30]; - coefficient2 = workspaceCursor[0x10]; - coefficient1 = workspaceCursor[8]; - coefficient7 = workspaceCursor[0x38]; - coefficient5 = workspaceCursor[0x28]; - coefficient3 = workspaceCursor[0x18]; - - acBits = (u32)coefficient4; - acBits |= (u32)coefficient6; - acBits |= (u32)coefficient2; - acBits |= (u32)coefficient1; - acBits |= (u32)coefficient7; - acBits |= (u32)coefficient5; - acBits |= (u32)coefficient3; + c4 = src[32]; + c6 = src[48]; + c2 = src[16]; + c1 = src[8]; + c7 = src[56]; + c5 = src[40]; + c3 = src[24]; + acBits = c4 | c6; + acBits = c2 | acBits; + acBits = c1 | acBits; + acBits = c7 | acBits; + acBits = c5 | acBits; + acBits = c3 | acBits; output = conv_row_ptr + column; if (acBits == 0) { - u8 sample = clampU8((*workspaceCursor >> 11) + 0x80); - output[r] = sample; - output[pitch * 6] = sample; - output[z + pitch] = sample; - output[z] = sample; - output[z - pitch] = sample; - output[m] = sample; + u8 sample = clampU8((*src >> 11) + 0x80); + output[pitch7] = sample; + output[pitch6] = sample; + output[pitch5] = sample; + output[pitch4] = sample; + output[pitch3] = sample; + output[pitch2] = sample; output[pitch] = sample; output[0] = sample; } else { + s32 oddDifferenceSum; + s32 oddSumRotation; + s32 evenBaseSum; + s32 evenOutput2; + s32 dc; + s32 evenBaseDifference; + s32 evenOutput0; + s32 evenOutput1; + s32 evenOutput3; + s32 oddHighRotation; + s32 oddMix; + s32 oddHighDifference; s32 oddLowSum; + s32 oddLowRotation; + s32 evenRotation; + s32 oddLowDifference; + s32 oddSumDifference; s32 oddHighSum; - s32 dcValue; - - evenRotation = coefficient2 - coefficient6; - oddLowSum = coefficient5 + coefficient3; - coefficient5 = coefficient5 - coefficient3; - oddHighSum = coefficient1 + coefficient7; - coefficient1 = coefficient1 - coefficient7; - evenRotationSum = coefficient6 + coefficient2; - coefficient2 = evenRotation * 0xB5 >> 8; + s32 evenRotationSum; + s32 oddOutput0; + s32 evenSum; + s32 oddOutput2; + s32 oddDifferenceRotation; + s32 oddOutput1; + s32 oddOutput3; + evenSum = c6 + c2; + evenRotation = c2 - c6; + oddLowSum = c5 + c3; + oddHighDifference = c1 - c7; + oddLowDifference = c5 - c3; + oddHighSum = c1 + c7; + c2 = evenRotation * 0xB5 >> 8; + dc = *src + 0x40000; + oddDifferenceSum = oddLowDifference + oddHighDifference; oddSumDifference = oddHighSum - oddLowSum; - oddDifferenceSum = coefficient5 + coefficient1; - dcValue = *workspaceCursor + 0x40000; - evenBaseDifference = dcValue - coefficient4; oddSumRotation = oddSumDifference * 0xB5 >> 8; - evenBaseSum = dcValue + coefficient4; - evenRotationSum = coefficient2 + evenRotationSum; - evenOutput1 = evenBaseDifference + coefficient2; - oddDifferenceSum = oddDifferenceSum * 0x62 >> 8; + evenBaseSum = dc + c4; + oddDifferenceRotation = oddDifferenceSum * 0x62 >> 8; + oddHighRotation = oddHighDifference * 0x14E >> 8; + evenRotationSum = c2 + evenSum; + oddMix = oddHighRotation - oddDifferenceRotation; + oddLowSum += oddMix; evenOutput0 = evenBaseSum + evenRotationSum; + evenBaseDifference = dc - c4; evenOutput3 = evenBaseSum - evenRotationSum; - oddHighRotation = coefficient1 * 0x14E >> 8; - evenOutput2 = evenBaseDifference - coefficient2; - oddHighRotation = oddHighRotation - oddDifferenceSum; - oddLowSum += oddHighRotation; + evenOutput1 = evenBaseDifference + c2; oddOutput0 = oddHighSum + oddLowSum; - oddOutput1 = oddSumRotation + oddHighRotation; - oddLowRotation = coefficient5 * 0x8B >> 8; - oddOutput3 = oddDifferenceSum + oddLowRotation; + oddLowRotation = oddLowDifference * 0x8B >> 8; + oddOutput3 = oddDifferenceRotation + oddLowRotation; + evenOutput2 = evenBaseDifference - c2; oddOutput2 = oddOutput3 + oddSumRotation; + oddOutput1 = oddSumRotation + oddMix; output[0] = scalingClampU8(evenOutput0 + oddOutput0); - output[r] = scalingClampU8(evenOutput0 - oddOutput0); + output[pitch7] = scalingClampU8(evenOutput0 - oddOutput0); output[pitch] = scalingClampU8(evenOutput1 + oddOutput1); - output[pitch * 6] = scalingClampU8(evenOutput1 - oddOutput1); - output[m] = scalingClampU8(evenOutput2 + oddOutput2); - output[z + pitch] = scalingClampU8(evenOutput2 - oddOutput2); - output[z - pitch] = scalingClampU8(evenOutput3 + oddOutput3); - output[z] = scalingClampU8(evenOutput3 - oddOutput3); + output[pitch6] = scalingClampU8(evenOutput1 - oddOutput1); + output[pitch2] = scalingClampU8(evenOutput2 + oddOutput2); + output[pitch5] = scalingClampU8(evenOutput2 - oddOutput2); + output[pitch3] = scalingClampU8(evenOutput3 + oddOutput3); + output[pitch4] = scalingClampU8(evenOutput3 - oddOutput3); } - workspaceCursor--; + src--; } } void TMCJPEGDEC_IdctBlock_Col(s32* block, u8* conv_row_ptr, u16 pitch, s32 zigzag) { s32 workspace[64]; - s32* workspaceCursor; - s32 evenBaseDifference; - s32 columnsRemaining; - s32 evenBaseSum; - s32 coefficient1; - s32 coefficient2; - s32 coefficient3; - s32 coefficient4; - s32 coefficient5; - s32 coefficient6; - s32 coefficient7; - s32 v; - s32 rowCoefficientCount; - s32 processedCoefficients; + s32* src; s32 column; - s32 m; - s32 n; - s32 oddOutput1; - s32 oddSumRotation; - s32 oddOutput2; - s32 evenOutput2; - s32 u; - s32 evenOutput0; - s32 oddOutput3; - s32 rotation45; - s32 z; - s32 oddSumDifference; - s32 oddDifferenceSum; - s32 oddHighRotation; - s32 oddLowRotation; - s32 evenOutput3; + s32 rowCount; + s32 processed; + s32* dst; if (zigzag == 0x11) { - s32 sample; - sample = clampS8(block[0] >> 11); - memset(conv_row_ptr, (s8)sample, 0x40); + s32 dc = block[0]; + s32 sample = clampS8(dc >> 11); + memset(conv_row_ptr, (s8)sample, 64); return; } - rowCoefficientCount = (zigzag >> 4) * 8; - if ((zigzag & 0xF) > 2) { - goto fullRowTransform; - } - - workspaceCursor = workspace; - processedCoefficients = 0; - for (; processedCoefficients < rowCoefficientCount; processedCoefficients += 8) { - s32 dcValue; - s32 oddCoefficient; - - oddCoefficient = block[1]; - dcValue = block[0]; - rotation45 = oddCoefficient * 0xB5 >> 8; - v = oddCoefficient * 0x62 >> 8; - u = v + rotation45; - z = (oddCoefficient * 0x14E >> 8) - v; - m = oddCoefficient + z; - n = rotation45 + z; - workspaceCursor[0] = dcValue + m; - workspaceCursor[7] = dcValue - m; - workspaceCursor[1] = dcValue + n; - workspaceCursor[6] = dcValue - n; - workspaceCursor[2] = dcValue + u; - workspaceCursor[5] = dcValue - u; - workspaceCursor[3] = dcValue + v; - workspaceCursor[4] = dcValue - v; - workspaceCursor += 8; - block += 8; - } - workspaceCursor = workspace + processedCoefficients; - for (; processedCoefficients <= 0x38; processedCoefficients += 8) { - memset(workspaceCursor, 0, 0x20); - workspaceCursor += 8; - } - goto columnTransform; - -fullRowTransform: - workspaceCursor = workspace; - processedCoefficients = 0; - for (; processedCoefficients < rowCoefficientCount; processedCoefficients += 8) { - u32 acBits; - coefficient4 = block[4]; - coefficient6 = block[6]; - coefficient2 = block[2]; - coefficient1 = block[1]; - coefficient7 = block[7]; - coefficient5 = block[5]; - coefficient3 = block[3]; - - acBits = (u32)coefficient4; - acBits |= (u32)coefficient6; - acBits |= (u32)coefficient2; - acBits |= (u32)coefficient1; - acBits |= (u32)coefficient7; - acBits |= (u32)coefficient5; - acBits |= (u32)coefficient3; + rowCount = (zigzag >> 4) * 8; + if ((zigzag & 0xF) <= 2) { + dst = workspace; + for (processed = 0; processed < rowCount; processed += 8) { + s32 mid; + s32 odd; + s32 outer; + s32 dc; + s32 inner; + s32 rotation; + s32 lowRotation; + s32 mix; + odd = block[1]; + rotation = odd * 0xB5 >> 8; + lowRotation = odd * 0x62 >> 8; + dc = block[0]; + mix = (odd * 0x14E >> 8) - lowRotation; + outer = odd + mix; + dst[0] = dc + outer; + inner = rotation + mix; + dst[7] = dc - outer; + dst[1] = dc + inner; + mid = lowRotation + rotation; + dst[6] = dc - inner; + dst[2] = dc + mid; + dst[5] = dc - mid; + dst[3] = dc + lowRotation; + dst[4] = dc - lowRotation; + dst += 8; + block += 8; + } + dst = workspace + processed; + for (; processed <= 56; processed += 8) { + memset(dst, 0, 32); + dst += 8; + } + } else { + dst = workspace; + for (processed = 0; processed < rowCount; processed += 8) { + s32 c2; + s32 c7; + s32 c3; + s32 c5; + s32 c1; + s32 c6; + int acBits; + s32 c4; + c4 = block[4]; + c6 = block[6]; + c2 = block[2]; + c1 = block[1]; + c7 = block[7]; + c5 = block[5]; + c3 = block[3]; + acBits = c4 | c6; + acBits = c2 | acBits; + acBits = c1 | acBits; + acBits = c7 | acBits; + acBits = c5 | acBits; + acBits = c3 | acBits; if (acBits == 0) { - s32 sample; - sample = block[0]; - workspaceCursor[7] = sample; - workspaceCursor[6] = sample; - workspaceCursor[5] = sample; - workspaceCursor[4] = sample; - workspaceCursor[3] = sample; - workspaceCursor[2] = sample; - workspaceCursor[1] = sample; - workspaceCursor[0] = sample; + s32 dc = block[0]; + dst[7] = dc; + dst[6] = dc; + dst[5] = dc; + dst[4] = dc; + dst[3] = dc; + dst[2] = dc; + dst[1] = dc; + dst[0] = dc; } else { + s32 evenBaseDifference; + s32 oddOutput3; + s32 oddMix; + s32 evenOutput1; + s32 evenOutput2; + s32 oddDifferenceRotation; + s32 oddLowRotation; + s32 oddDifferenceSum; + s32 evenSum; s32 oddLowSum; + s32 dc; + s32 oddSumDifference; s32 oddLowDifference; - s32 oddHighSum; + s32 evenBaseSum; + s32 oddSumRotation; + s32 evenRotationSum; + s32 evenRotation; + s32 evenOutput0; + s32 oddOutput1; s32 oddHighDifference; - s32 dcValue; - - rotation45 = coefficient2 - coefficient6; - oddLowSum = coefficient5 + coefficient3; - oddLowDifference = coefficient5 - coefficient3; - oddHighSum = coefficient1 + coefficient7; - oddHighDifference = coefficient1 - coefficient7; - u = coefficient6 + coefficient2; - rotation45 = rotation45 * 0xB5 >> 8; + s32 oddOutput0; + s32 evenOutput3; + s32 oddHighSum; + s32 oddOutput2; + s32 oddHighRotation; + oddHighDifference = c1 - c7; + oddLowSum = c5 + c3; + evenSum = c6 + c2; + oddHighSum = c1 + c7; + evenRotation = c2 - c6; + dc = block[0]; + evenBaseDifference = dc - c4; + c2 = evenRotation * 0xB5 >> 8; oddSumDifference = oddHighSum - oddLowSum; - oddDifferenceSum = oddLowDifference + oddHighDifference; - dcValue = block[0]; - evenBaseDifference = dcValue - coefficient4; + oddLowDifference = c5 - c3; oddSumRotation = oddSumDifference * 0xB5 >> 8; - evenBaseSum = dcValue + coefficient4; - u = rotation45 + u; - v = evenBaseDifference + rotation45; - z = oddDifferenceSum * 0x62 >> 8; - evenOutput0 = evenBaseSum + u; - evenOutput3 = evenBaseSum - u; + evenBaseSum = dc + c4; oddHighRotation = oddHighDifference * 0x14E >> 8; - evenOutput2 = evenBaseDifference - rotation45; - m = oddHighRotation - z; - oddLowSum += m; - n = oddHighSum + oddLowSum; - oddOutput1 = oddSumRotation + m; - workspaceCursor[0] = evenOutput0 + n; + oddDifferenceSum = oddLowDifference + oddHighDifference; + oddDifferenceRotation = oddDifferenceSum * 0x62 >> 8; + evenRotationSum = c2 + evenSum; + oddMix = oddHighRotation - oddDifferenceRotation; + oddOutput1 = oddSumRotation + oddMix; + evenOutput1 = evenBaseDifference + c2; oddLowRotation = oddLowDifference * 0x8B; - workspaceCursor[7] = evenOutput0 - n; + oddLowSum += oddMix; + evenOutput3 = evenBaseSum - evenRotationSum; + evenOutput0 = evenBaseSum + evenRotationSum; oddLowRotation >>= 8; - workspaceCursor[1] = v + oddOutput1; - oddOutput3 = z + oddLowRotation; + oddOutput3 = oddDifferenceRotation + oddLowRotation; + evenOutput2 = evenBaseDifference - c2; + oddOutput0 = oddHighSum + oddLowSum; + dst[0] = evenOutput0 + oddOutput0; + dst[7] = evenOutput0 - oddOutput0; + dst[1] = evenOutput1 + oddOutput1; oddOutput2 = oddOutput3 + oddSumRotation; - workspaceCursor[6] = v - oddOutput1; - workspaceCursor[2] = evenOutput2 + oddOutput2; - workspaceCursor[5] = evenOutput2 - oddOutput2; - workspaceCursor[3] = evenOutput3 + oddOutput3; - workspaceCursor[4] = evenOutput3 - oddOutput3; + dst[6] = evenOutput1 - oddOutput1; + dst[2] = evenOutput2 + oddOutput2; + dst[5] = evenOutput2 - oddOutput2; + dst[3] = evenOutput3 + oddOutput3; + dst[4] = evenOutput3 - oddOutput3; } block += 8; - workspaceCursor += 8; + dst += 8; + } + dst = workspace + processed; + for (; processed <= 56; processed += 8) { + memset(dst, 0, 32); + dst += 8; + } } - workspaceCursor = workspace + processedCoefficients; - for (; processedCoefficients <= 0x38; processedCoefficients += 8) { - memset(workspaceCursor, 0, 0x20); - workspaceCursor += 8; - } - -columnTransform: - - workspaceCursor = workspace + 7; - for (columnsRemaining = 8, column = 7; columnsRemaining > 0; columnsRemaining--, column--) { - u32 acBits; + src = workspace + 7; + for (column = 7; column >= 0; column--) { + s32 c1; + s32 c3; + int acBits; + s32 c7; + s32 c2; + s32 c5; u8* output; - coefficient4 = workspaceCursor[0x20]; + s32 c6; + s32 c4; + c4 = src[32]; output = conv_row_ptr + column; - coefficient6 = workspaceCursor[0x30]; - coefficient2 = workspaceCursor[0x10]; - coefficient1 = workspaceCursor[8]; - coefficient7 = workspaceCursor[0x38]; - coefficient5 = workspaceCursor[0x28]; - coefficient3 = workspaceCursor[0x18]; - - acBits = (u32)coefficient4; - acBits |= (u32)coefficient6; - acBits |= (u32)coefficient2; - acBits |= (u32)coefficient1; - acBits |= (u32)coefficient7; - acBits |= (u32)coefficient5; - acBits |= (u32)coefficient3; + c6 = src[48]; + c2 = src[16]; + c1 = src[8]; + c7 = src[56]; + c5 = src[40]; + c3 = src[24]; + acBits = c4 | c6; + acBits = c2 | acBits; + acBits = c1 | acBits; + acBits = c7 | acBits; + acBits = c5 | acBits; + acBits = c3 | acBits; if (acBits == 0) { - s32 sample; - sample = clampS8(*workspaceCursor >> 11); + s32 sample = clampS8(*src >> 11); output[56] = (u8)sample; output[48] = (u8)sample; output[40] = (u8)sample; @@ -427,48 +453,68 @@ columnTransform: output[8] = (u8)sample; output[0] = (u8)sample; } else { - s32 oddLowSum; - s32 oddLowDifference; + s32 evenSum; + s32 evenOutput2; + s32 oddOutput0; + s32 oddMix; + s32 evenBaseSum; + s32 oddHighRotation; + s32 evenRotationSum; + s32 oddSumDifference; + s32 evenOutput1; s32 oddHighSum; + s32 evenOutput0; + s32 oddOutput1; + s32 oddDifferenceSum; + s32 oddLowSum; + s32 oddOutput3; + s32 evenBaseDifference; s32 oddHighDifference; - s32 dcValue; - - rotation45 = coefficient2 - coefficient6; - oddLowSum = coefficient5 + coefficient3; - oddLowDifference = coefficient5 - coefficient3; - oddHighSum = coefficient1 + coefficient7; - oddHighDifference = coefficient1 - coefficient7; - u = coefficient6 + coefficient2; - rotation45 = rotation45 * 0xB5 >> 8; + s32 oddOutput2; + s32 dc; + s32 oddLowDifference; + s32 evenOutput3; + s32 oddDifferenceRotation; + s32 oddLowRotation; + s32 evenRotation; + s32 oddSumRotation; + oddHighSum = c1 + c7; + evenRotation = c2 - c6; + oddLowSum = c5 + c3; oddSumDifference = oddHighSum - oddLowSum; - oddDifferenceSum = oddLowDifference + oddHighDifference; - dcValue = *workspaceCursor; - evenBaseDifference = dcValue - coefficient4; + evenSum = c6 + c2; + oddLowDifference = c5 - c3; + c2 = evenRotation * 0xB5 >> 8; + oddHighDifference = c1 - c7; + dc = *src; + evenRotationSum = c2 + evenSum; oddSumRotation = oddSumDifference * 0xB5 >> 8; - evenBaseSum = dcValue + coefficient4; - u = rotation45 + u; - v = evenBaseDifference + rotation45; - z = oddDifferenceSum * 0x62 >> 8; - evenOutput0 = evenBaseSum + u; - evenOutput3 = evenBaseSum - u; + oddDifferenceSum = oddLowDifference + oddHighDifference; + oddDifferenceRotation = oddDifferenceSum * 0x62 >> 8; + evenBaseDifference = dc - c4; + evenBaseSum = dc + c4; oddHighRotation = oddHighDifference * 0x14E >> 8; - evenOutput2 = evenBaseDifference - rotation45; - m = oddHighRotation - z; - oddLowSum += m; - n = oddHighSum + oddLowSum; - oddOutput1 = oddSumRotation + m; + oddMix = oddHighRotation - oddDifferenceRotation; + evenOutput3 = evenBaseSum - evenRotationSum; + evenOutput1 = evenBaseDifference + c2; + evenOutput2 = evenBaseDifference - c2; + evenOutput0 = evenBaseSum + evenRotationSum; oddLowRotation = oddLowDifference * 0x8B >> 8; - oddOutput3 = z + oddLowRotation; + oddOutput3 = oddDifferenceRotation + oddLowRotation; + oddLowSum += oddMix; + oddOutput1 = oddSumRotation + oddMix; + oddOutput0 = oddHighSum + oddLowSum; oddOutput2 = oddOutput3 + oddSumRotation; - output[0] = (u8)clampS8((evenOutput0 + n) >> 11); - output[56] = (u8)clampS8((evenOutput0 - n) >> 11); - output[8] = (u8)clampS8((v + oddOutput1) >> 11); - output[48] = (u8)clampS8((v - oddOutput1) >> 11); + output[0] = (u8)clampS8((evenOutput0 + oddOutput0) >> 11); + output[56] = (u8)clampS8((evenOutput0 - oddOutput0) >> 11); + output[8] = (u8)clampS8((evenOutput1 + oddOutput1) >> 11); + output[48] = (u8)clampS8((evenOutput1 - oddOutput1) >> 11); output[16] = (u8)clampS8((evenOutput2 + oddOutput2) >> 11); output[40] = (u8)clampS8((evenOutput2 - oddOutput2) >> 11); output[24] = (u8)clampS8((evenOutput3 + oddOutput3) >> 11); output[32] = (u8)clampS8((evenOutput3 - oddOutput3) >> 11); } - workspaceCursor--; + src--; } } + diff --git a/libs/RevoEX/src/net/aes.c b/libs/RevoEX/src/net/aes.c index 50ad2509..a0967b1e 100644 --- a/libs/RevoEX/src/net/aes.c +++ b/libs/RevoEX/src/net/aes.c @@ -168,72 +168,86 @@ static const u32 AESiDecryptTable[] = { 0xa8017139, 0xcb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0 }; -#define ROTATE(x,n) (((x) << (n)) | ((x) >> (32 - (n)))) -#define ENCRYPT(state0,state1,state2,state3) (((ROTATE(AESiEncryptTable[((state1)>>16)&255],16) ^ ROTATE(AESiEncryptTable[(state0)>>24],24)) ^ AESiEncryptTable[(state3)&255]) ^ ROTATE(AESiEncryptTable[((state2)>>8)&255],8)) -#define DECRYPT(state0,state1,state2,state3) (((ROTATE(AESiDecryptTable[((state1)>>16)&255],16) ^ AESiDecryptTable[(state3)&255]) ^ ROTATE(AESiDecryptTable[(state0)>>24],24)) ^ ROTATE(AESiDecryptTable[((state2)>>8)&255],8)) -#define SUBSTITUTE(table,state0,state1,state2,state3) ((((u32)table[(state0)>>24]<<24) | table[(state3)&255]) | (((u32)table[((state1)>>16)&255]<<16) | ((u32)table[((state2)>>8)&255]<<8))) - -#define DECRYPT_FINAL(table,state0,state1,state2,state3,key) ((table[(state3)&255] ^ ((u32)table[((state2)>>8)&255]<<8)) ^ (((u32)table[((state1)>>16)&255]<<16) ^ ((key) ^ ((u32)table[(state0)>>24]<<24)))) +#define ROTATE(x, n) __rlwinm((x), (n), 0, 31) +#define ROUND_WORD(table, a, b, c, d) (ROTATE(table[(a) >> 24], 24) ^ ROTATE(table[((b) >> 16) & 255], 16) ^ ROTATE(table[((c) >> 8) & 255], 8) ^ table[(d) & 255]) +#define SUBSTITUTE_WORD(table, a, b, c, d) (((u32)table[(a) >> 24] << 24) | ((u32)table[((b) >> 16) & 255] << 16) | ((u32)table[((c) >> 8) & 255] << 8) | table[(d) & 255]) +#define DOUBLE_BYTES(x) ((((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)) void AESiEncryptBlock(AESContext* context, u32* output, const u32* input) { + const u32* key = context->keys; u32 rounds = context->rounds; - const u32* key = context->keys + 4; - u32 state0=input[0]^context->keys[0], state1=input[1]^context->keys[1], state2=input[2]^context->keys[2], state3=input[3]^context->keys[3]; + u32 state0 = input[0]; + u32 state1 = input[1]; + u32 state2 = input[2]; + u32 state3 = input[3]; + state0 ^= key[0]; + state1 ^= key[1]; + state2 ^= key[2]; + state3 ^= key[3]; + key += 4; while (--rounds != 0) { - u32 nextState0=ENCRYPT(state0,state1,state2,state3); - u32 nextState1=ENCRYPT(state1,state2,state3,state0); - u32 nextState2=ENCRYPT(state2,state3,state0,state1); - u32 nextState3=ENCRYPT(state3,state0,state1,state2); - state0=nextState0^key[0]; state1=nextState1^key[1]; state2=nextState2^key[2]; state3=nextState3^key[3]; + u32 next0 = ROUND_WORD(AESiEncryptTable, state0, state1, state2, state3); + u32 next1 = ROUND_WORD(AESiEncryptTable, state1, state2, state3, state0); + u32 next2 = ROUND_WORD(AESiEncryptTable, state2, state3, state0, state1); + u32 next3 = ROUND_WORD(AESiEncryptTable, state3, state0, state1, state2); + state0 = next0 ^ key[0]; + state1 = next1 ^ key[1]; + state2 = next2 ^ key[2]; + state3 = next3 ^ key[3]; key += 4; } - output[0] = SUBSTITUTE(AESiSubShiftTable,state0,state1,state2,state3)^key[0]; - output[1] = SUBSTITUTE(AESiSubShiftTable,state1,state2,state3,state0)^key[1]; - output[2] = SUBSTITUTE(AESiSubShiftTable,state2,state3,state0,state1)^key[2]; - output[3] = SUBSTITUTE(AESiSubShiftTable,state3,state0,state1,state2)^key[3]; + output[0] = SUBSTITUTE_WORD(AESiSubShiftTable, state0, state1, state2, state3) ^ key[0]; + output[1] = SUBSTITUTE_WORD(AESiSubShiftTable, state1, state2, state3, state0) ^ key[1]; + output[2] = SUBSTITUTE_WORD(AESiSubShiftTable, state2, state3, state0, state1) ^ key[2]; + output[3] = SUBSTITUTE_WORD(AESiSubShiftTable, state3, state0, state1, state2) ^ key[3]; } -#define DOUBLE_BYTES(x) ((((x)&0x7f7f7f7f)<<1) ^ ((((x)&0x80808080)>>7)*0x1b)) +static inline void AESiInvMixColumn(u32* word) { + u32 value = *word; + u32 timesTwo = DOUBLE_BYTES(value); + u32 timesFour = DOUBLE_BYTES(timesTwo); + u32 mixed = DOUBLE_BYTES(timesFour) ^ value; + mixed ^= timesFour ^ ROTATE(mixed, 8); + mixed ^= timesTwo ^ ROTATE(mixed, 8); + mixed ^= value ^ ROTATE(mixed, 8); + *word = mixed; +} void AESiDecryptBlock(AESContext* context, u32* output, const u32* input) { - u32 rounds=context->rounds; - u32* key=context->keys; - u32 state1; - u32 state0; - u32 state2; - u32 state3; - key += rounds*4; + u32* key = context->keys; + u32 rounds = context->rounds; + u32 state0, state1, state2, state3; + key += rounds * 4; if (context->needsTransform) { u32 round; - for (round=1; roundrounds; ++round) { + for (round = 1; round < context->rounds; round++) { u32 word; - for (word=0; word<4; ++word) { - u32 value=context->keys[round*4 + word]; - u32 timesTwo=DOUBLE_BYTES(value); - u32 timesFour=DOUBLE_BYTES(timesTwo); - u32 timesNine=DOUBLE_BYTES(timesFour)^value; - u32 mixed=(ROTATE(timesNine,8) ^ (timesNine ^ timesFour)); - mixed=(ROTATE(mixed,8) ^ (mixed ^ timesTwo)); - mixed=(ROTATE(mixed,8) ^ (mixed ^ value)); - context->keys[round*4 + word]=mixed; + for (word = 0; word < 4; word++) { + AESiInvMixColumn(&context->keys[round * 4 + word]); } } - context->needsTransform=0; + context->needsTransform = 0; } - state0=input[0]^key[0]; state1=input[1]^key[1]; state2=input[2]^key[2]; state3=input[3]^key[3]; - key-=4; + state0 = input[0] ^ key[0]; + state1 = input[1] ^ key[1]; + state2 = input[2] ^ key[2]; + state3 = input[3] ^ key[3]; + key -= 4; while (--rounds != 0) { - u32 nextState0=DECRYPT(state0,state3,state2,state1); - u32 nextState1=DECRYPT(state1,state0,state3,state2); - u32 nextState2=DECRYPT(state2,state1,state0,state3); - u32 nextState3=DECRYPT(state3,state2,state1,state0); - state0=nextState0^key[0]; state1=nextState1^key[1]; state2=nextState2^key[2]; state3=nextState3^key[3]; - key-=4; + u32 next0 = ROUND_WORD(AESiDecryptTable, state0, state3, state2, state1); + u32 next1 = ROUND_WORD(AESiDecryptTable, state1, state0, state3, state2); + u32 next2 = ROUND_WORD(AESiDecryptTable, state2, state1, state0, state3); + u32 next3 = ROUND_WORD(AESiDecryptTable, state3, state2, state1, state0); + state0 = next0 ^ key[0]; + state1 = next1 ^ key[1]; + state2 = next2 ^ key[2]; + state3 = next3 ^ key[3]; + key -= 4; } - output[0]=DECRYPT_FINAL(AESiInvSubShiftTable,state0,state3,state2,state1,key[0]); - output[1]=DECRYPT_FINAL(AESiInvSubShiftTable,state1,state0,state3,state2,key[1]); - output[2]=DECRYPT_FINAL(AESiInvSubShiftTable,state2,state1,state0,state3,key[2]); - output[3]=DECRYPT_FINAL(AESiInvSubShiftTable,state3,state2,state1,state0,key[3]); + output[0] = key[0] ^ ((u32)AESiInvSubShiftTable[state0 >> 24] << 24) ^ ((u32)AESiInvSubShiftTable[(state3 >> 16) & 255] << 16) ^ ((u32)AESiInvSubShiftTable[(state2 >> 8) & 255] << 8) ^ AESiInvSubShiftTable[state1 & 255]; + output[1] = key[1] ^ ((u32)AESiInvSubShiftTable[state1 >> 24] << 24) ^ ((u32)AESiInvSubShiftTable[(state0 >> 16) & 255] << 16) ^ ((u32)AESiInvSubShiftTable[(state3 >> 8) & 255] << 8) ^ AESiInvSubShiftTable[state2 & 255]; + output[2] = key[2] ^ ((u32)AESiInvSubShiftTable[state2 >> 24] << 24) ^ ((u32)AESiInvSubShiftTable[(state1 >> 16) & 255] << 16) ^ ((u32)AESiInvSubShiftTable[(state0 >> 8) & 255] << 8) ^ AESiInvSubShiftTable[state3 & 255]; + output[3] = key[3] ^ ((u32)AESiInvSubShiftTable[state3 >> 24] << 24) ^ ((u32)AESiInvSubShiftTable[(state2 >> 16) & 255] << 16) ^ ((u32)AESiInvSubShiftTable[(state1 >> 8) & 255] << 8) ^ AESiInvSubShiftTable[state0 & 255]; } BOOL NETAESCreateEx(AESContext* context, const void* key, u32 keyLength, const void* iv, const AESBlockMode* mode) { diff --git a/tools/decomp-assist/o2-aes-idct.attempts.md b/tools/decomp-assist/o2-aes-idct.attempts.md new file mode 100644 index 00000000..f33760fe --- /dev/null +++ b/tools/decomp-assist/o2-aes-idct.attempts.md @@ -0,0 +1,75 @@ +# o2-aes-idct attempts + +Worktree data-d11, branch agent/w1009/o2-aes-idct (from origin/main 20cf9f9f). 43U only. + +## Unit-level checks + +- Compiler sweep (all GC and Wii compilers; `-ipa file` dropped for GC < 3.0): aes GC/3.0a3 .. 3.0a5.2 + identical (7/9), older or Wii compilers worse. idct_block_var GC/3.0a3 .. 3.0a5.2 identical (0/2). + Version is not the lever for either unit. +- combosweep (single + pairwise pragmas): aes best 137 -> 135 (opt_propagation off), no lead. + +## net/aes: AESiEncryptBlock 137 -> 0, AESiDecryptBlock 215 -> 0 (unit Matching, DOL OK) + +Tooling: `tree.py` symbolically executes a function and compares the expression tree of every +store against the target, independent of registers and scheduling. That separated "wrong +dataflow" from "wrong allocation", which earlier rounds had mixed together. + +Findings, in order: + +1. The target round is plain rijndael-fst order `hi ^ m16 ^ m8 ^ lo`, then `^ key`. The target + tree is `(lo ^ (m8 ^ (hi ^ m16))) ^ key`. At -O3/-O4 MWCC rebalances any 4-leaf XOR chain + (tree height reduction): every one of the 120 shapes tried compiled to `(x^y)^(z^w)`. At + optimization_level 2 it is not rebalanced, but level 2 cannot unroll the decrypt key loop, + which the target does (mtctr 2), so the unit is O4. +2. With the rotate written as the `__rlwinm(x, n, 0, 31)` intrinsic instead of the shift/or + macro, O4 leaves the chain alone (intrinsic calls are not reassociated), and the natural + expression gives exactly the target tree. It also removes the CSE temporaries the shift/or + macro created for the table loads (x used twice), which is what had kept the allocation off. +3. Final round: encrypt `(hi<<24 | m16<<16 | m8<<8 | lo) ^ key[i]`; decrypt is XOR-based with the + key first, `key[i] ^ hi<<24 ^ m16<<16 ^ m8<<8 ^ lo` (O4 rebalances it the target way). +4. Encrypt init: `state = input[i]; ... state ^= key[i]` with `key = context->keys` walked by + `key += 4` (single-expression init put keys[i] in the state register; 9 diffs). +5. Decrypt: `key = context->keys; key += rounds * 4;` (the one-expression form folds +16 into + the loads). InvMixColumn as `static inline void AESiInvMixColumn(u32* word)` (pointer + parameter) fixes the last r7/r8 swap between value and timesTwo; value-parameter helpers and + all local declaration orders left 26+ diffs. + +Rejected along the way: optimization_level 2 pragma (correct tree, wrong key loop), int/long +casts between XOR stages (blocks rebalancing but unnatural). + +`include/global/decomp/ide.h`: declared `__rlwinm` for non-MWCC tooling (guarded by +`#ifndef __MWERKS__`, no build effect). + +## buffer/idct_block_var: Lumi and Col (in progress) + +Dataflow first (path-based symbolic execution of both objects, `ptree.py`): + +1. Even part: the target computes `rot + (c6 + c2)` into a fresh variable. `x = rot + x` is + rewritten by MWCC to `x += rot` (operand order `sum + rot`), and a single-use `evenSum` + gets forward-substituted and reassociated (`c2 + (rot + c6)`). Fix: `evenSum = c6 + c2; + c2 = evenRotation * 0xB5 >> 8; evenRotationSum = c2 + evenSum;` (the c2 reassignment + blocks the substitution). Same in both passes of both functions. +2. AC test: the target ORs leaf-first (`c2 | acc`). `acc |= c` or `acc = c | acc` with a u32/s32 + accumulator is canonicalised to acc-first, a single 7-way expression is rebalanced at O4. + An `int` accumulator over `s32` coefficients (`acBits = c2 | acBits;`) keeps leaf-first + (the long->int conversion stops the `|=` rewrite). +3. Level 2 or 1 cannot be it: no CTR row loop at level 2 (274 vs 257 insns). + +With 1-2, all store/call trees of Lumi (3 paths) and Col (4 paths) equal the target and the +frame sizes match. Remaining differences are allocation and scheduling only. + +Allocation findings (mwdbg + regsim, `wantmap.py` maps candidate vregs to target registers by +symbolic value): + +- Named locals are numbered per block in reverse declaration order, blocks top-down (function + scope first), forward-substituted locals take no number; split webs and inline locals become + `@` temps numbered after all named locals. +- Column setup now matches exactly: `pitch4/7/6/5/3/2` as named locals assigned before the + loop, a separate column cursor (`src`, not a split web of the row cursor), and + `for (column = 7; column >= 0; column--)` (CTR trip count becomes a temp colored first). +- Declaration-order search inside blocks on the captured graph tops out at 113/153 target + registers, and a greedy order-feasibility check stalls on callee-saved claim order, so the + column AC block still has a different pre-RA schedule (live ranges). +- objdiff fuzzy is the gate metric and disagrees with positional diff counts; searches now + score candidates with objdiff itself (`odfuzzy.py`, one-unit objdiff project per slot).