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.
This commit is contained in:
Stenzek committed 2026-11-02 20:53:00 +10:00
1 parent 586deb5927
commit 94e12317c6
3 files changed
+30 -6

No files matched your search

+19
View File
@@ -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
//////////////////////////////////////////////////////////////////////////
+10 -6
View File
@@ -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<s32>(rdVal) >> sh) != static_cast<s32>(rtVal));
const double x = f16Unsign(prtVal.GetValidX(rtVal)) * static_cast<double>(1 << sh);
const double y_in = ((static_cast<s32>(rdVal) >> sh) != static_cast<s32>(rtVal)) ?
static_cast<double>(HIWORD_U16(rtVal)) :
f16Unsign(prtVal.GetValidY(rtVal));
const double y_in = shifted_out ? static_cast<double>(HIWORD_U16(rtVal)) : f16Unsign(prtVal.GetValidY(rtVal));
const double y = (y_in * static_cast<double>(1 << sh)) + f16Overflow(x);
prdVal.x = static_cast<float>(f16Sign(x));
prdVal.y = static_cast<float>(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<double>(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<double>(static_cast<s32>(rdVal)) * static_cast<double>(1 << (sh - 16))) :
static_cast<double>(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<float>(std::floor(x / static_cast<double>(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;
}
+1
View File
@@ -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.