From 68cdcba6c5cbb0a075a5a4e0e95e34ae639a7f2c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 26 Mar 2019 22:06:39 -0700 Subject: [PATCH 1/5] interp: Don't write lane 2 on single colorconv. Not that it's valid to use the op with that size anyway. --- Core/MIPS/IR/IRCompVFPU.cpp | 3 ++- Core/MIPS/MIPSIntVFPU.cpp | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp index c13ab20c72..7240b5e8bb 100644 --- a/Core/MIPS/IR/IRCompVFPU.cpp +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -1953,8 +1953,9 @@ namespace MIPSComp { void IRFrontend::Comp_ColorConv(MIPSOpcode op) { CONDITIONAL_DISABLE(VFPU_VEC); - if (js.HasUnknownPrefix()) + if (js.HasUnknownPrefix() || !IsPrefixWithinSize(js.prefixS, op) || js.HasTPrefix()) { DISABLE; + } // Vector color conversion // d[N] = ConvertTo16(s[N*2]) | (ConvertTo16(s[N*2+1]) << 16) diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index b9bb5e2d2e..8de28d7e87 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -983,10 +983,13 @@ namespace MIPSInt int vd = _VD; int vs = _VS; u32 s[4]; + VectorSize isz = GetVecSize(op); VectorSize sz = V_Quad; ReadVector(reinterpret_cast(s), sz, vs); ApplySwizzleS(reinterpret_cast(s), sz); u16 colors[4]; + // TODO: Invalid swizzle values almost seem to use the last value converted in a + // previous execution of these ops. It's a bit odd. for (int i = 0; i < 4; i++) { u32 in = s[i]; @@ -1024,7 +1027,7 @@ namespace MIPSInt } u32 ov[2] = {(u32)colors[0] | (colors[1] << 16), (u32)colors[2] | (colors[3] << 16)}; ApplyPrefixD(reinterpret_cast(ov), V_Pair); - WriteVector((const float *)ov, V_Pair, vd); + WriteVector((const float *)ov, isz == V_Single ? V_Single : V_Pair, vd); PC += 4; EatPrefixes(); } From b75690787e3d7b1f0b5ee0fe525455b622f6f4ce Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 30 Mar 2019 10:55:29 -0700 Subject: [PATCH 2/5] interp: Correct swizzle on vx2i ops. --- Core/MIPS/IR/IRCompVFPU.cpp | 3 +-- Core/MIPS/MIPSIntVFPU.cpp | 32 +++++++++++++++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp index 7240b5e8bb..9939822e99 100644 --- a/Core/MIPS/IR/IRCompVFPU.cpp +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -1541,8 +1541,7 @@ namespace MIPSComp { void IRFrontend::Comp_Vx2i(MIPSOpcode op) { CONDITIONAL_DISABLE(VFPU_VEC); - - if (js.HasUnknownPrefix()) + if (js.HasUnknownPrefix() || js.HasSPrefix()) DISABLE; int bits = ((op >> 16) & 2) == 0 ? 8 : 16; // vuc2i/vc2i (0/1), vus2i/vs2i (2/3) diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index 8de28d7e87..573275e123 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -798,17 +798,16 @@ namespace MIPSInt EatPrefixes(); } - void Int_Vx2i(MIPSOpcode op) - { - u32 s[4]; - u32 d[4] = {0}; + void Int_Vx2i(MIPSOpcode op) { + u32 s[4], d[4]{}; int vd = _VD; int vs = _VS; VectorSize sz = GetVecSize(op); VectorSize oz = sz; ReadVector(reinterpret_cast(s), sz, vs); - // ForbidVPFXS + ApplySwizzleS(reinterpret_cast(s), sz); + // TODO: Similar to colorconv, invalid swizzle seems to reuse last output. switch ((op >> 16) & 3) { case 0: // vuc2i // Quad is the only option. @@ -819,7 +818,7 @@ namespace MIPSInt { u32 value = s[0]; for (int i = 0; i < 4; i++) { - d[i] = (u32)((value & 0xFF) * 0x01010101) >> 1; + d[i] = (u32)((u32)(value & 0xFF) * 0x01010101UL) >> 1; value >>= 8; } oz = V_Quad; @@ -840,8 +839,11 @@ namespace MIPSInt case 2: // vus2i oz = V_Pair; - switch (sz) - { + switch (sz) { + case V_Quad: + case V_Triple: + sz = V_Pair; + // Intentional fallthrough. case V_Pair: oz = V_Quad; // Intentional fallthrough. @@ -861,8 +863,11 @@ namespace MIPSInt case 3: // vs2i oz = V_Pair; - switch (sz) - { + switch (sz) { + case V_Quad: + case V_Triple: + sz = V_Pair; + // Intentional fallthrough. case V_Pair: oz = V_Quad; // Intentional fallthrough. @@ -884,9 +889,10 @@ namespace MIPSInt _dbg_assert_msg_(CPU,0,"Trying to interpret instruction that can't be interpreted"); break; } - - ApplyPrefixD(reinterpret_cast(d),oz, true); // Only write mask - WriteVector(reinterpret_cast(d),oz,vd); + + // Saturation does in fact apply. + ApplyPrefixD(reinterpret_cast(d),oz); + WriteVector(reinterpret_cast(d), oz, vd); PC += 4; EatPrefixes(); } From 4a2f8a74dc4a6a76e97d8f9e01a6c64d1475b41b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 30 Mar 2019 11:23:11 -0700 Subject: [PATCH 3/5] interp: Correct size handling for vi2x ops. --- Core/MIPS/IR/IRCompVFPU.cpp | 3 +- Core/MIPS/MIPSIntVFPU.cpp | 116 +++++++++++++++++------------------- 2 files changed, 57 insertions(+), 62 deletions(-) diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp index 9939822e99..c964628c72 100644 --- a/Core/MIPS/IR/IRCompVFPU.cpp +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -1456,8 +1456,7 @@ namespace MIPSComp { void IRFrontend::Comp_Vi2x(MIPSOpcode op) { CONDITIONAL_DISABLE(VFPU_VEC); - - if (js.HasUnknownPrefix()) + if (js.HasUnknownPrefix() || js.HasSPrefix()) DISABLE; int bits = ((op >> 16) & 2) == 0 ? 8 : 16; // vi2uc/vi2c (0/1), vi2us/vi2s (2/3) diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index 573275e123..d6e0230b67 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -897,89 +897,85 @@ namespace MIPSInt EatPrefixes(); } - void Int_Vi2x(MIPSOpcode op) - { - int s[4]; - u32 d[2] = {0}; + void Int_Vi2x(MIPSOpcode op) { + int s[4]{}; + u32 d[2]{}; int vd = _VD; int vs = _VS; VectorSize sz = GetVecSize(op); VectorSize oz; ReadVector(reinterpret_cast(s), sz, vs); - ApplySwizzleS(reinterpret_cast(s), sz); //TODO: and the mask to kill everything but swizzle - switch ((op >> 16)&3) - { + // Negate, const, etc. apply as expected. + ApplySwizzleS(reinterpret_cast(s), V_Quad); + + // TODO: Similar to colorconv, invalid swizzle seems to reuse last output. + switch ((op >> 16) & 3) { case 0: //vi2uc - { - for (int i = 0; i < 4; i++) - { - int v = s[i]; - if (v < 0) v = 0; - v >>= 23; - d[0] |= ((u32)v & 0xFF) << (i * 8); - } - oz = V_Single; + for (int i = 0; i < 4; i++) { + int v = s[i]; + if (v < 0) v = 0; + v >>= 23; + d[0] |= ((u32)v & 0xFF) << (i * 8); } + oz = V_Single; break; case 1: //vi2c - { - for (int i = 0; i < 4; i++) - { - u32 v = s[i]; - d[0] |= (v >> 24) << (i * 8); - } - oz = V_Single; + for (int i = 0; i < 4; i++) { + u32 v = s[i]; + d[0] |= (v >> 24) << (i * 8); } + oz = V_Single; break; case 2: //vi2us - { - for (int i = 0; i < GetNumVectorElements(sz) / 2; i++) { - int low = s[i * 2]; - int high = s[i * 2 + 1]; - if (low < 0) low = 0; - if (high < 0) high = 0; - low >>= 15; - high >>= 15; - d[i] = low | (high << 16); - } - switch (sz) { - case V_Quad: oz = V_Pair; break; - case V_Pair: oz = V_Single; break; - default: - _dbg_assert_msg_(CPU,0,"Trying to interpret instruction that can't be interpreted"); - oz = V_Single; - break; - } + for (int i = 0; i < (GetNumVectorElements(sz) + 1) / 2; i++) { + int low = s[i * 2]; + int high = s[i * 2 + 1]; + if (low < 0) low = 0; + if (high < 0) high = 0; + low >>= 15; + high >>= 15; + d[i] = low | (high << 16); + } + switch (sz) { + case V_Quad: oz = V_Pair; break; + case V_Triple: oz = V_Pair; break; + case V_Pair: oz = V_Single; break; + case V_Single: oz = V_Single; break; + default: + _dbg_assert_msg_(CPU, 0, "Trying to interpret instruction that can't be interpreted"); + oz = V_Single; + break; } break; case 3: //vi2s - { - for (int i = 0; i < GetNumVectorElements(sz) / 2; i++) { - u32 low = s[i * 2]; - u32 high = s[i * 2 + 1]; - low >>= 16; - high >>= 16; - d[i] = low | (high << 16); - } - switch (sz) { - case V_Quad: oz = V_Pair; break; - case V_Pair: oz = V_Single; break; - default: - _dbg_assert_msg_(CPU,0,"Trying to interpret instruction that can't be interpreted"); - oz = V_Single; - break; - } + for (int i = 0; i < (GetNumVectorElements(sz) + 1) / 2; i++) { + u32 low = s[i * 2]; + u32 high = s[i * 2 + 1]; + low >>= 16; + high >>= 16; + d[i] = low | (high << 16); + } + switch (sz) { + case V_Quad: oz = V_Pair; break; + case V_Triple: oz = V_Pair; break; + case V_Pair: oz = V_Single; break; + case V_Single: oz = V_Single; break; + default: + _dbg_assert_msg_(CPU, 0, "Trying to interpret instruction that can't be interpreted"); + oz = V_Single; + break; } break; default: - _dbg_assert_msg_(CPU,0,"Trying to interpret instruction that can't be interpreted"); + _dbg_assert_msg_(CPU, 0, "Trying to interpret instruction that can't be interpreted"); oz = V_Single; break; } - ApplyPrefixD(reinterpret_cast(d),oz); - WriteVector(reinterpret_cast(d),oz,vd); + // D prefix applies as expected. + ApplyPrefixD(reinterpret_cast(d), oz); + WriteVector(reinterpret_cast(d), oz, vd); PC += 4; EatPrefixes(); } From 175ceef58326213d83239de6fd49450989a2b392 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 30 Mar 2019 10:23:37 -0700 Subject: [PATCH 4/5] interp: Cleanup vsocp size handling. --- Core/MIPS/IR/IRCompVFPU.cpp | 2 +- Core/MIPS/MIPSIntVFPU.cpp | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp index c964628c72..eb73cb27fa 100644 --- a/Core/MIPS/IR/IRCompVFPU.cpp +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -1905,7 +1905,7 @@ namespace MIPSComp { void IRFrontend::Comp_Vocp(MIPSOpcode op) { CONDITIONAL_DISABLE(VFPU_VEC); - if (js.HasUnknownPrefix()) { + if (js.HasUnknownPrefix() || !IsPrefixWithinSize(js.prefixS, op) || js.HasTPrefix() || (js.prefixS & VFPU_NEGATE(1, 1, 1, 1)) != 0) { DISABLE; } diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index d6e0230b67..0b8be5894d 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -579,8 +579,7 @@ namespace MIPSInt EatPrefixes(); } - void Int_Vocp(MIPSOpcode op) - { + void Int_Vocp(MIPSOpcode op) { float s[4], t[4], d[4]; int vd = _VD; int vs = _VS; @@ -601,19 +600,21 @@ namespace MIPSInt // Always positive NaN. Note that s is always negated from the registers. d[i] = my_isnan(s[i]) ? fabsf(s[i]) : t[i] + s[i]; } + RetainInvalidSwizzleST(d, sz); ApplyPrefixD(d, sz); WriteVector(d, sz, vd); PC += 4; EatPrefixes(); } - void Int_Vsocp(MIPSOpcode op) - { + void Int_Vsocp(MIPSOpcode op) { float s[4], t[4], d[4]; int vd = _VD; int vs = _VS; VectorSize sz = GetVecSize(op); - VectorSize outSize = GetDoubleVectorSize(sz); + VectorSize outSize = GetDoubleVectorSizeSafe(sz); + if (outSize == V_Invalid) + outSize = V_Quad; ReadVector(s, sz, vs); // S prefix forces negate in even/odd and xxyy swizzle. @@ -636,7 +637,7 @@ namespace MIPSInt d[2] = nanclamp(t[2] + s[2], 0.0f, 1.0f); d[3] = nanclamp(t[3] + s[3], 0.0f, 1.0f); } - ApplyPrefixD(d, sz); + ApplyPrefixD(d, sz, true); WriteVector(d, outSize, vd); PC += 4; EatPrefixes(); From f2be0cb0836081e44eab3e245c728bb8d5c9add4 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 16 Mar 2019 10:05:48 -0700 Subject: [PATCH 5/5] interp: Correct prefixes for vsbn/vsbz. --- Core/MIPS/MIPSIntVFPU.cpp | 61 +++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index 0b8be5894d..90c4a2b71c 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -2000,64 +2000,61 @@ namespace MIPSInt EatPrefixes(); } - void Int_Vsbn(MIPSOpcode op) - { + void Int_Vsbn(MIPSOpcode op) { + FloatBits d, s, t; int vd = _VD; int vs = _VS; int vt = _VT; VectorSize sz = GetVecSize(op); - FloatBits d; - FloatBits s; - u8 exp = (u8)(127 + VI(vt)); - ReadVector(s.f, sz, vs); - // TODO: Test swizzle, t? ApplySwizzleS(s.f, sz); + ReadVector(t.f, sz, vs); + ApplySwizzleT(t.f, sz); + // Swizzle does apply to the value read as an integer. + u8 exp = (u8)(127 + t.i[0]); - if (sz != V_Single) { - ERROR_LOG_REPORT(CPU, "vsbn not implemented for size %d", GetNumVectorElements(sz)); + // Simply replace the exponent bits. + u32 prev = s.u[0] & 0x7F800000; + if (prev != 0 && prev != 0x7F800000) { + d.u[0] = (s.u[0] & ~0x7F800000) | (exp << 23); + } else { + d.u[0] = s.u[0]; } - for (int i = 0; i < GetNumVectorElements(sz); ++i) { - // Simply replace the exponent bits. - u32 prev = s.u[i] & 0x7F800000; - if (prev != 0 && prev != 0x7F800000) { - d.u[i] = (s.u[i] & ~0x7F800000) | (exp << 23); - } else { - d.u[i] = s.u[i]; - } + + // If sz is greater than V_Single, the rest are unchanged. + for (int i = 1; i < GetNumVectorElements(sz); ++i) { + d.u[i] = s.u[i]; } + ApplyPrefixD(d.f, sz); WriteVector(d.f, sz, vd); PC += 4; EatPrefixes(); } - void Int_Vsbz(MIPSOpcode op) - { + void Int_Vsbz(MIPSOpcode op) { // Vector scale by zero (set exp to 0 to extract mantissa) + FloatBits d, s; int vd = _VD; int vs = _VS; VectorSize sz = GetVecSize(op); - FloatBits d; - FloatBits s; - ReadVector(s.f, sz, vs); - // TODO: Test swizzle, t? ApplySwizzleS(s.f, sz); - if (sz != V_Single) { - ERROR_LOG_REPORT(CPU, "vsbz not implemented for size %d", GetNumVectorElements(sz)); + // NAN and denormals pass through. + if (my_isnan(s.f[0]) || (s.u[0] & 0x7F800000) == 0) { + d.u[0] = s.u[0]; + } else { + d.u[0] = (127 << 23) | (s.u[0] & 0x007FFFFF); } - for (int i = 0; i < GetNumVectorElements(sz); ++i) { - // NAN and denormals pass through. - if (my_isnan(s.f[i]) || (s.u[i] & 0x7F800000) == 0) { - d.u[i] = s.u[i]; - } else { - d.u[i] = (127 << 23) | (s.u[i] & 0x007FFFFF); - } + + // If sz is greater than V_Single, the rest are unchanged. + for (int i = 1; i < GetNumVectorElements(sz); ++i) { + d.u[i] = s.u[i]; } + ApplyPrefixD(d.f, sz); WriteVector(d.f, sz, vd); PC += 4;