From 840e19aa59f76e12a85fbc31b1fa81c57bd9b8d4 Mon Sep 17 00:00:00 2001 From: joeypercia <81598667+joeypercia@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:11:58 -0800 Subject: [PATCH 1/2] =?UTF-8?q?Recompiler:=20v=5Fldexp=5Ff16=20keeps=20?= =?UTF-8?q?=C2=B10=20and=20=C2=B1inf=20for=20any=20exponent=20(#658)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v_ldexp_f16 was emitted as HLSL ldexp(x, (float)n), which DXC computes as x * exp2(n). For |n| beyond the f32 exponent range exp2(n) is inf or 0, so ±0 with a large positive n and ±inf with a large negative n gave 0 * inf = NaN (0x7fff on an RTX 4070 Ti, 0xfe00 on an RDNA2 iGPU); the ISA keeps ±0 and ±inf. v_ldexp_f16 now uses the exact ldexp_f helper that v_ldexp_f32 already uses. Fixes #658 Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/gpu/recomp/emit.rs | 2 +- crates/xcl/src/gpu/recomp/tests.rs | 33 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index d8244a0d5..f2d6ab192 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3411,7 +3411,7 @@ impl Em { "v_fmac_f16" => fr(format!("mad({}, {}, f16tof32({curh}))", fh(0), fh(1))), "v_fmamk_f16" => fr(format!("mad({}, f16tof32({} & 0xFFFFu), {})", fh(0), u[1], fh(2))), "v_fmaak_f16" => fr(format!("mad({}, {}, f16tof32({} & 0xFFFFu))", fh(0), fh(1), u[2])), - "v_ldexp_f16" => fr(format!("ldexp({}, (float){})", fh(0), si(1))), + "v_ldexp_f16" => fr(format!("ldexp_f({}, {})", fh(0), si(1))), "v_rcp_f16" => fr(format!("rcp({})", fh(0))), "v_rsq_f16" => fr(format!("rsqrt({})", fh(0))), "v_sqrt_f16" => fr(format!("sqrt({})", fh(0))), diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 1c9e1e809..9f6cedf0f 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3007,6 +3007,39 @@ fn gpu_images_a16_d16() { } } +/// v_ldexp_f16 at the edges: ±0 and ±inf keep their value for any exponent (no 0·inf NaN from a +/// 2^n that overflows f32), NaN stays NaN, underflow lands on 0 and exact scalings stay exact +/// (#658). Overflow of a finite value is left out: it depends on the f16 rounding mode (#111). +#[test] +fn gpu_ldexp_f16_zero_inf_and_large_exponents() { + for wave64 in gpu_waves() { + let p = prog(&[("V_LDEXP_F16", "ENC_VOP2", &[("VDST", 10), ("SRC0", V), ("VSRC1", 1)], None)]); + let (dxil, mem) = kernel_wave(&p, wave64); + // (x, n, want) + let cases: [(u16, i16, u16); 11] = [ + (0x0000, 200, 0x0000), (0x8000, 300, 0x8000), (0x0000, 32767, 0x0000), (0x8000, -32768, 0x8000), + (0x7C00, -200, 0x7C00), (0xFC00, -300, 0xFC00), (0x7C00, -32768, 0x7C00), (0x7E00, 5, 0x7E00), + (0x3C00, -30, 0x0000), (0x0001, 10, 0x0400), (0x3C00, 3, 0x4800), + ]; + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let (x, n, _) = cases[l % cases.len()]; + (r[0], r[1], r[10]) = (x as u32, n as u16 as u32, 0); + } + let r = runner::run(&dxil, &st, &mem).unwrap(); + let mut bad = Vec::new(); + for (l, (x, n, want)) in cases.iter().enumerate() { + let got = (r.vgpr[l][10] & 0xFFFF) as u16; + // any NaN encoding counts as NaN + let ok = got == *want || (*want & 0x7C00 == 0x7C00 && *want & 0x3FF != 0 && got & 0x7C00 == 0x7C00 && got & 0x3FF != 0); + if !ok { + bad.push(format!("ldexp_f16({x:#06x}, {n}) = {got:#06x}, want {want:#06x}")); + } + } + assert!(bad.is_empty(), "wave{}: {} of {} wrong\n{}", if wave64 { 64 } else { 32 }, bad.len(), cases.len(), bad.join("\n")); + } +} + /// f32 → IEEE half bits (exact for the small values used in tests). fn half_bits(x: f32) -> u32 { if x == 0.0 { From 32bcaef6fca398125b5abcacaeb3aadcc8418e08 Mon Sep 17 00:00:00 2001 From: joeypercia <81598667+joeypercia@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:48:46 -0800 Subject: [PATCH 2/2] =?UTF-8?q?Direct=20DXIL:=20v=5Fldexp=5Ff16=20keeps=20?= =?UTF-8?q?=C2=B10=20and=20=C2=B1inf=20for=20any=20exponent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The direct backend computed x * exp2(n) in f32, so 0 with an exponent that overflows exp2 gave 0 * inf = NaN, and inf with a very negative one gave inf * 0 = NaN (both 0x7fff after the f16 conversion). It now uses the exact ldexp_f helper that v_ldexp_f32 uses, like the HLSL path in the previous commit. gpu_ldexp_f16_zero_inf_and_large_exponents with XCL_DIRECT_DXIL=1: 6 of 11 cases wrong before, all pass after, on an RTX 4070 Ti and an RDNA2 iGPU. Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/gpu/dxil/lower_wide.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_wide.rs b/crates/xcl/src/gpu/dxil/lower_wide.rs index 9176356ac..f0bb19dde 100644 --- a/crates/xcl/src/gpu/dxil/lower_wide.rs +++ b/crates/xcl/src/gpu/dxil/lower_wide.rs @@ -530,11 +530,10 @@ impl Lw { fr(self, e) } "v_ldexp_f16" => { + // exact on the bits, like v_ldexp_f32: x * exp2(n) gives NaN for 0 * inf and inf * 0 let x = fh(self, 0); let n = si(self, 1); - let nf = self.sitof(n); - let p = self.un(op::EXP, nf); - let e = self.fmul(x, p); + let e = self.ldexp_f(x, n); fr(self, e) } "v_rcp_f16" => f1!(|s: &mut Self, x| {