From bc43fd6dc4035f3687fe2667024b65e9742442f6 Mon Sep 17 00:00:00 2001 From: VicO <91730360+Victorbriez@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:56:35 +0100 Subject: [PATCH] Recompiler: cube sc / tc negate by the sign bit on both backends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both backends negated with `fsub -0.0, x`: the HLSL helpers through `-x`, the direct lowering through `fneg`. With no denormal option a driver may flush a denormal there. NVIDIA does on the HLSL path (v_cubetc_f32 returned ±0 for a denormal source on an RTX 3070), and RADV under vkd3d-proton does on the direct path (reported on #2348). Both now flip the sign bit, as the direct backend's directed-rounding path already does for source modifiers (`fsrc_bits`). The VALU fuzzer of #2169 found 58 of 256 cases differing between the backends on NVIDIA; with this change it finds none. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/prelude.rs | 15 +++++++--- crates/xcl/src/gpu/recomp/emit.rs | 6 ++-- crates/xcl/src/gpu/recomp/tests.rs | 44 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/prelude.rs b/crates/xcl/src/gpu/dxil/prelude.rs index 082676cc5b..261f4e55de 100644 --- a/crates/xcl/src/gpu/dxil/prelude.rs +++ b/crates/xcl/src/gpu/dxil/prelude.rs @@ -262,6 +262,13 @@ impl Lw { let r = self.fdiv(n, d); self.sel(c, r, q) } + /// An f32 with its sign bit flipped (`fneg` is `fsub -0.0, x`, which lets a driver flush a denormal). + fn fneg_bits(&mut self, a: Value) -> Value { + let b = self.bits(a); + let k = self.c(0x8000_0000); + let n = self.xor(b, k); + self.f(n) + } /// cube_id / cube_sc / cube_tc / cube_ma pub fn cube(&mut self, name: &str, x: Value, y: Value, z: Value) -> Value { let (ax, ay, az) = (self.fabs(x), self.fabs(y), self.fabs(z)); @@ -279,13 +286,13 @@ impl Lw { (self.sel(zneg, f5, f4), self.sel(yneg, f3, f2), self.sel(xneg, f1, f0)) } "v_cubesc_f32" => { - let nx = self.fneg(x); - let nz = self.fneg(z); + let nx = self.fneg_bits(x); + let nz = self.fneg_bits(z); (self.sel(zneg, nx, x), x, self.sel(xneg, z, nz)) } "v_cubetc_f32" => { - let ny = self.fneg(y); - let nz = self.fneg(z); + let ny = self.fneg_bits(y); + let nz = self.fneg_bits(z); (ny, self.sel(yneg, nz, z), ny) } _ => { diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 64acd2d57b..8e86d5711e 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -6279,8 +6279,10 @@ uint sdwa_dst(uint r, uint old, uint sel, uint unused) { return v; } float cube_id(float x, float y, float z) { if (abs(z) >= abs(x) && abs(z) >= abs(y)) return z < 0.0f ? 5.0f : 4.0f; if (abs(y) >= abs(x)) return y < 0.0f ? 3.0f : 2.0f; return x < 0.0f ? 1.0f : 0.0f; } -float cube_sc(float x, float y, float z) { if (abs(z) >= abs(x) && abs(z) >= abs(y)) return z < 0.0f ? -x : x; if (abs(y) >= abs(x)) return x; return x < 0.0f ? z : -z; } -float cube_tc(float x, float y, float z) { if (abs(z) >= abs(x) && abs(z) >= abs(y)) return -y; if (abs(y) >= abs(x)) return y < 0.0f ? -z : z; return -y; } +// the sign bit flipped: `-x` compiles to `fsub -0.0, x`, which lets a driver flush a denormal +float neg_bits(float x) { return asfloat(asuint(x) ^ 0x80000000u); } +float cube_sc(float x, float y, float z) { if (abs(z) >= abs(x) && abs(z) >= abs(y)) return z < 0.0f ? neg_bits(x) : x; if (abs(y) >= abs(x)) return x; return x < 0.0f ? z : neg_bits(z); } +float cube_tc(float x, float y, float z) { if (abs(z) >= abs(x) && abs(z) >= abs(y)) return neg_bits(y); if (abs(y) >= abs(x)) return y < 0.0f ? neg_bits(z) : z; return neg_bits(y); } float cube_ma(float x, float y, float z) { if (abs(z) >= abs(x) && abs(z) >= abs(y)) return 2.0f * z; if (abs(y) >= abs(x)) return 2.0f * y; return 2.0f * x; } // AMD cube coordinates (sc/|2ma| + 1.5, tc/|2ma| + 1.5, face) back to a direction float3 cube_dir(float s, float t, float face) { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 3cfa8e2ec2..3d38b98b9f 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -9845,3 +9845,47 @@ fn gpu_v_dot8_u32_u4_edge_cases() { } } } + +/// v_cubesc_f32 / v_cubetc_f32 negate a source by its sign bit: a denormal comes back negated, +/// not flushed to ±0, on both backends (an `fsub -0.0, x` lets the driver flush it). +#[test] +fn gpu_cube_coordinates_negate_denormals_by_sign() { + if !gpu_ready() { + return; + } + let code = prog(&[ + ("V_CUBESC_F32", "ENC_VOP3", &[("VDST", 3), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2)], None), + ("V_CUBETC_F32", "ENC_VOP3", &[("VDST", 4), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2)], None), + ]); + // (x, y, z) and the expected (sc, tc) + const CASES: [([u32; 3], [u32; 2]); 6] = [ + // z major, z < 0: sc = -x, tc = -y + ([0x0000_FFFF, 0x0000_0001, 0xBF80_0000], [0x8000_FFFF, 0x8000_0001]), + // z major, z > 0: sc = x, tc = -y (a negative denormal y) + ([0x0000_0010, 0x8001_FC00, 0x3F80_0000], [0x0000_0010, 0x0001_FC00]), + // y major, y < 0: sc = x, tc = -z + ([0x0000_0000, 0xBF80_0000, 0x0000_007F], [0x0000_0000, 0x8000_007F]), + // x major, x > 0: sc = -z, tc = -y + ([0x3F80_0000, 0x0000_3800, 0x0000_0010], [0x8000_0010, 0x8000_3800]), + // x major, x < 0: sc = z, tc = -y + ([0xBF80_0000, 0x807F_FFFF, 0x0000_0010], [0x0000_0010, 0x007F_FFFF]), + // normal values (1.0, 0.5, 0.25): x major, sc = -0.25, tc = -0.5 + ([0x3F80_0000, 0x3F00_0000, 0x3E80_0000], [0xBE80_0000, 0xBF00_0000]), + ]; + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[..3].copy_from_slice(&CASES[l % CASES.len()].0); + } + let t = translate(&code, &runner::options(Mode::Kernel)).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported); + let hlsl_dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|x| panic!("{x}\n--- HLSL ---\n{}", t.hlsl)); + let direct_dxil = runner::direct_kernel_full(&code, true).unwrap().dxil; + for (backend, dxil) in [("hlsl", hlsl_dxil), ("direct_dxil", direct_dxil)] { + let r = runner::run_with_statics(&dxil, &st, &[], &[], &[], &[]).unwrap_or_else(|e| panic!("{backend}: {e}")); + for l in 0..64usize { + let (src, want) = CASES[l % CASES.len()]; + let got = [r.vgpr[l][3], r.vgpr[l][4]]; + assert_eq!(got, want, "{backend} lane {l}: (sc, tc) of {src:08x?}"); + } + } +}