From 3db7c65e86e1645183feac1fbbe94c161b3d78d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 24 Sep 2026 13:46:06 -0600 Subject: [PATCH] x64 IR JIT: Compile the VFPU pack, unpack and clamp ops vi2uc, vi2c, vi2us, vi2s, vuc2i, vc2i, vus2i and vs2i lowered to IR ops that x64 left to the IR interpreter. They're now SSE2: shifts into place, then PACKSSDW/PACKUSWB for the packs and self-unpacks for the unpacks. An output overlapping its input still goes the slow way. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/MIPS/x86/X64IRCompVec.cpp | 102 ++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 8 deletions(-) diff --git a/Core/MIPS/x86/X64IRCompVec.cpp b/Core/MIPS/x86/X64IRCompVec.cpp index a68939a16d..c448fa5b3a 100644 --- a/Core/MIPS/x86/X64IRCompVec.cpp +++ b/Core/MIPS/x86/X64IRCompVec.cpp @@ -248,8 +248,19 @@ void X64JitBackend::CompIR_VecClamp(IRInst inst) { switch (inst.op) { case IROp::Vec4ClampToZero: case IROp::Vec2ClampToZero: - CompIR_Generic(inst); + { + const int lanes = inst.op == IROp::Vec4ClampToZero ? 4 : 2; + if (inst.dest != inst.src1 && Overlap(inst.dest, lanes, inst.src1, lanes)) { + DISABLE; + } + // Expand the sign bit to a mask, then clear the negative lanes. + X64Reg tempReg = regs_.MapWithFPRTemp(inst); + MOVDQA(tempReg, regs_.F(inst.src1)); + PSRAD(tempReg, 31); + PANDN(tempReg, regs_.F(inst.src1)); + MOVDQA(regs_.FX(inst.dest), R(tempReg)); break; + } default: INVALIDOP; @@ -298,17 +309,92 @@ void X64JitBackend::CompIR_VecHoriz(IRInst inst) { void X64JitBackend::CompIR_VecPack(IRInst inst) { CONDITIONAL_DISABLE; + // Only SSE2 here. A destination overlapping the source goes the slow way, it's rare. switch (inst.op) { - case IROp::Vec2Unpack16To31: case IROp::Vec4Pack32To8: - case IROp::Vec2Pack31To16: - case IROp::Vec4Unpack8To32: - case IROp::Vec2Unpack16To32: - case IROp::Vec4DuplicateUpperBitsAndShift1: case IROp::Vec4Pack31To8: - case IROp::Vec2Pack32To16: - CompIR_Generic(inst); + { + if (Overlap(inst.dest, 1, inst.src1, 4)) { + DISABLE; + } + // The top byte of each lane (for 31, the byte below the sign), packed into one lane. + X64Reg tempReg = regs_.MapWithFPRTemp(inst); + MOVDQA(tempReg, regs_.F(inst.src1)); + if (inst.op == IROp::Vec4Pack31To8) + PSLLD(tempReg, 1); + PSRLD(tempReg, 24); + PACKSSDW(tempReg, R(tempReg)); + PACKUSWB(tempReg, R(tempReg)); + MOVDQA(regs_.FX(inst.dest), R(tempReg)); break; + } + + case IROp::Vec2Pack32To16: + case IROp::Vec2Pack31To16: + { + if (Overlap(inst.dest, 1, inst.src1, 2)) { + DISABLE; + } + // The top 16 bits of each lane (for 31, the 16 below the sign), packed into one lane. The + // arithmetic shift keeps them in PACKSSDW's range, so the bits come through unchanged. + X64Reg tempReg = regs_.MapWithFPRTemp(inst); + MOVDQA(tempReg, regs_.F(inst.src1)); + if (inst.op == IROp::Vec2Pack31To16) + PSLLD(tempReg, 1); + PSRAD(tempReg, 16); + PACKSSDW(tempReg, R(tempReg)); + MOVDQA(regs_.FX(inst.dest), R(tempReg)); + break; + } + + case IROp::Vec4Unpack8To32: + { + if (Overlap(inst.dest, 4, inst.src1, 1)) { + DISABLE; + } + // Each byte into the top of a lane: duplicate bytes, then words, then shift the rest out. + X64Reg tempReg = regs_.MapWithFPRTemp(inst); + MOVDQA(tempReg, regs_.F(inst.src1)); + PUNPCKLBW(tempReg, R(tempReg)); + PUNPCKLWD(tempReg, R(tempReg)); + PSLLD(tempReg, 24); + MOVDQA(regs_.FX(inst.dest), R(tempReg)); + break; + } + + case IROp::Vec2Unpack16To32: + case IROp::Vec2Unpack16To31: + { + if (Overlap(inst.dest, 2, inst.src1, 1)) { + DISABLE; + } + // Each half into the top of a lane, shifted down one more for 31. + X64Reg tempReg = regs_.MapWithFPRTemp(inst); + MOVDQA(tempReg, regs_.F(inst.src1)); + PUNPCKLWD(tempReg, R(tempReg)); + PSLLD(tempReg, 16); + if (inst.op == IROp::Vec2Unpack16To31) + PSRLD(tempReg, 1); + MOVDQA(regs_.FX(inst.dest), R(tempReg)); + break; + } + + case IROp::Vec4DuplicateUpperBitsAndShift1: + { + if (inst.dest != inst.src1 && Overlap(inst.dest, 4, inst.src1, 4)) { + DISABLE; + } + // v |= v >> 8; v |= v >> 16; v >>= 1, which turns 0xFF000000 into 0x7FFFFFFF. + X64Reg tempReg = regs_.MapWithFPRTemp(inst); + MOVDQA(tempReg, regs_.F(inst.src1)); + PSRLD(tempReg, 8); + POR(tempReg, regs_.F(inst.src1)); + MOVDQA(regs_.FX(inst.dest), R(tempReg)); + PSRLD(tempReg, 16); + POR(regs_.FX(inst.dest), R(tempReg)); + PSRLD(regs_.FX(inst.dest), 1); + break; + } default: INVALIDOP;