From 94e12317c63bfa0044c7d024d431fcf0507ad13b Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 2 Nov 2026 20:38:48 +1000 Subject: [PATCH] CPU/PGXP: Don't take fraction from the field below when extracting bitfields Syphon Filter 3 extracts y from packed vertices with sll 10; sra 21, which gave the x's top bits as a fraction. --- src/core-tests/cpu_pgxp_tests.cpp | 19 +++++++++++++++++++ src/core/cpu_pgxp.cpp | 16 ++++++++++------ src/core/cpu_pgxp.h | 1 + 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/core-tests/cpu_pgxp_tests.cpp b/src/core-tests/cpu_pgxp_tests.cpp index 152b571a0..2324c54f7 100644 --- a/src/core-tests/cpu_pgxp_tests.cpp +++ b/src/core-tests/cpu_pgxp_tests.cpp @@ -193,6 +193,25 @@ TEST_F(PGXPTest, ShiftDropsFractionOfBitsShiftedOut) EXPECT_NEAR(Get(R_V1).y, 346.5 * 32.0, TOLERANCE); } +TEST_F(PGXPTest, BitfieldExtractDoesNotTakeFractionFromFieldBelow) +{ + // Syphon Filter 3 packs vertices as z:10 y:11 x:11, here x = -50 and y = -19, and extracts y with sll 10; sra 21. + // The top bits of x are below y, and gave it a fraction of 31/32. + Set(R_V0, 0x13FF6FCE, 28622.0f, 5119.0f); + CPU::PGXP::CPU_SLL(R(0, R_V0, R_V1, 10), 0x13FF6FCE); + EXPECT_EQ(Get(R_V1).value, 0xFDBF3800u); + CPU::PGXP::CPU_SRA(R(0, R_V1, R_A0, 21), 0xFDBF3800); + EXPECT_EQ(Get(R_A0).value, 0xFFFFFFEDu); + EXPECT_NEAR(Get(R_A0).x, -19.0, TOLERANCE); + EXPECT_NEAR(Get(R_A0).y, -1.0, TOLERANCE); + EXPECT_FALSE(Get(R_A0).flags & BITFIELD); + + // Without bits shifted out by a left shift, the bits shifted out are a fraction. + Set(R_V1, 0xFDBF3800, 14336.0f, -577.0f); + CPU::PGXP::CPU_SRA(R(0, R_V1, R_A0, 21), 0xFDBF3800); + EXPECT_NEAR(Get(R_A0).x, -577.0 / 32.0, TOLERANCE); +} + ////////////////////////////////////////////////////////////////////////// // Right shifts: sign of an upper half that is only sign bits ////////////////////////////////////////////////////////////////////////// diff --git a/src/core/cpu_pgxp.cpp b/src/core/cpu_pgxp.cpp index af47a76c3..49d4ef85f 100644 --- a/src/core/cpu_pgxp.cpp +++ b/src/core/cpu_pgxp.cpp @@ -1750,14 +1750,13 @@ ALWAYS_INLINE_RELEASE void CPU::PGXP::CPU_SLL(Instruction instr, u32 rtVal, u32 { // When significant bits are shifted out of the top (e.g. extracting a bitfield), the upper half's precise offset // can belong to them, and would land in the integer bits of the field below. Use the native upper half instead. + const bool shifted_out = ((static_cast(rdVal) >> sh) != static_cast(rtVal)); const double x = f16Unsign(prtVal.GetValidX(rtVal)) * static_cast(1 << sh); - const double y_in = ((static_cast(rdVal) >> sh) != static_cast(rtVal)) ? - static_cast(HIWORD_U16(rtVal)) : - f16Unsign(prtVal.GetValidY(rtVal)); + const double y_in = shifted_out ? static_cast(HIWORD_U16(rtVal)) : f16Unsign(prtVal.GetValidY(rtVal)); const double y = (y_in * static_cast(1 << sh)) + f16Overflow(x); prdVal.x = static_cast(f16Sign(x)); prdVal.y = static_cast(f16Sign(y)); - prdVal.flags = (prtVal.flags | VALID_TAINTED_Z); + prdVal.flags = (prtVal.flags & ~BITFIELD) | VALID_TAINTED_Z | (shifted_out ? BITFIELD : 0u); } } @@ -1795,7 +1794,12 @@ ALWAYS_INLINE_RELEASE void CPU::PGXP::CPU_SRx(Instruction instr, u32 rtVal, u32 { // Only the upper half is left, so extract it, using the representation closest to the native value. The upper half // of the result is only sign bits, taken from the precise value so that the halves represent the same value. - const double y = static_cast(sign ? HIWORD_S16(rtVal) : HIWORD_U16(rtVal)) + + // If a left shift dropped significant bits, this is extracting a bitfield, and the native bits shifted out belong + // to the field below, not a fraction of this one (Syphon Filter 3 packs vertices as z:10 y:11 x:11). Only shift the + // precise offset from native then. + const double y = ((prtVal.flags & BITFIELD) ? + (static_cast(static_cast(rdVal)) * static_cast(1 << (sh - 16))) : + static_cast(sign ? HIWORD_S16(rtVal) : HIWORD_U16(rtVal))) + f16Sign(prtVal.GetValidY(rtVal) - HIWORD_S16(rtVal)); // Quantize before splitting, otherwise a tiny negative value has a zero low half and a -1 upper half. const double x = @@ -1804,7 +1808,7 @@ ALWAYS_INLINE_RELEASE void CPU::PGXP::CPU_SRx(Instruction instr, u32 rtVal, u32 prdVal.y = static_cast(std::floor(x / static_cast(USHRT_MAX + 1))); prdVal.z = prtVal.z; prdVal.value = rdVal; - prdVal.flags = (prtVal.flags & ~VALID_X) | ((prtVal.flags & VALID_Y) >> 1) | VALID_TAINTED_Z; + prdVal.flags = (prtVal.flags & ~(VALID_X | BITFIELD)) | ((prtVal.flags & VALID_Y) >> 1) | VALID_TAINTED_Z; return; } diff --git a/src/core/cpu_pgxp.h b/src/core/cpu_pgxp.h index 4230f1cbf..9c84f321e 100644 --- a/src/core/cpu_pgxp.h +++ b/src/core/cpu_pgxp.h @@ -12,6 +12,7 @@ enum : u32 VALID_X = (1u << 0), VALID_Y = (1u << 1), VALID_Z = (1u << 2), + BITFIELD = (1u << 3), // Significant bits were shifted out of the top, lower bits may be another field. VALID_LOWZ = (1u << 16), // Valid Z from the low part of a 32-bit value. VALID_HIGHZ = (1u << 17), // Valid Z from the high part of a 32-bit value. VALID_TAINTED_Z = (1u << 31), // X/Y has been changed, Z may not be accurate.