From c2887bbee96db50a6d08cc13df35a4b76652554e Mon Sep 17 00:00:00 2001 From: gefydev Date: Sun, 22 Nov 2026 21:28:01 -0300 Subject: [PATCH] recomp: f64 abs / neg modifiers on the sign bit (#72) The f64 NEG source modifier was emitted as -x and ABS as abs(x). DXIL has no fneg: DXC writes -x as fsub -0.0, x, and IEEE leaves the sign of a NaN result open, so a negated NaN kept its sign on NVIDIA (v_frexp_mant_f64 with NEG on 0x7FF8000000000001). The hardware modifiers only flip or clear the sign bit; they are now that, on the high dword. Cherry-picked from #72 (ec446ec) without #71, which #67 superseded; the test gates on gpu_ready (#67: wave64 programs run split on 32-lane GPUs). --- crates/xcl/src/gpu/recomp/emit.rs | 10 ++++++---- crates/xcl/src/gpu/recomp/tests.rs | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 28ed46e..b9269d3 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2534,15 +2534,17 @@ impl Em { } } let u64v = |i: usize| format!("(((uint64_t){} << 32) | (uint64_t){})", lo_hi[i].1, lo_hi[i].0); + // abs / neg on the sign bit, as the hardware applies them: `-x` is `fsub -0.0, x` in + // DXIL, which leaves the sign of a NaN to the driver let f64v = |i: usize| { - let mut s = format!("asdouble({}, {})", lo_hi[i].0, lo_hi[i].1); + let mut hi = lo_hi[i].1.clone(); if m.abs[i.min(2)] { - s = format!("abs({s})"); + hi = format!("({hi} & 0x7FFFFFFFu)"); } if m.neg[i.min(2)] { - s = format!("(-{s})"); + hi = format!("({hi} ^ 0x80000000u)"); } - s + format!("asdouble({}, {hi})", lo_hi[i].0) }; let dst = outs.first().map(|o| Self::dst_of(d, o)).unwrap_or(Dst::Null); let t = self.t(); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 3e350ba..ba39561 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -468,8 +468,8 @@ fn f64_class_compare_vopc_and_vop3() { // SRC0 is the 64-bit f64 operand (VGPR/SGPR pair), SRC1 stays a plain 32-bit mask; // abs/neg act on the double operand only assert!(t.hlsl.contains("(v12) >> fclass64(asdouble(v10, v11))"), "{}", t.hlsl); - assert!(t.hlsl.contains("(v12) >> fclass64(abs(asdouble(v10, v11)))"), "{}", t.hlsl); - assert!(t.hlsl.contains("(v13) >> fclass64((-asdouble(v10, v11)))"), "{}", t.hlsl); + assert!(t.hlsl.contains("(v12) >> fclass64(asdouble(v10, (v11 & 0x7FFFFFFFu)))"), "{}", t.hlsl); + assert!(t.hlsl.contains("(v13) >> fclass64(asdouble(v10, (v11 ^ 0x80000000u)))"), "{}", t.hlsl); // mask destinations go through the shared compare/write-mask path: vcc, an SGPR pair, exec assert!(t.hlsl.contains("vcc_lo = t") && t.hlsl.contains("vcc_hi = t"), "{}", t.hlsl); assert!(t.hlsl.contains("s20 = t") && t.hlsl.contains("s21 = t"), "{}", t.hlsl); @@ -567,8 +567,8 @@ fn frexp_f64_instructions() { // the source pair becomes a double; the mantissa stays f64, the exponent is one dword assert!(t.hlsl.contains("frexp_mant_d(asdouble(v10, v11))"), "{}", t.hlsl); assert!(t.hlsl.contains("(uint)frexp_exp_d(asdouble(v10, v11))"), "{}", t.hlsl); - assert!(t.hlsl.contains("frexp_mant_d((-asdouble(v10, v11)))"), "{}", t.hlsl); - assert!(t.hlsl.contains("frexp_exp_d(abs(asdouble(v10, v11)))"), "{}", t.hlsl); + assert!(t.hlsl.contains("frexp_mant_d(asdouble(v10, (v11 ^ 0x80000000u)))"), "{}", t.hlsl); + assert!(t.hlsl.contains("frexp_exp_d(asdouble(v10, (v11 & 0x7FFFFFFFu)))"), "{}", t.hlsl); // the f64 pair of inline −1.0 = 0xBFF0000000000000 assert!(t.hlsl.contains("frexp_mant_d(asdouble(0x00000000u, 0xBFF00000u))"), "{}", t.hlsl); if super::dxc::location().is_ok() { @@ -585,7 +585,7 @@ fn rndne_f64_instructions() { let t = translate(&code, &Options::default()).unwrap(); assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); assert!(t.hlsl.contains("round_ne_d(asdouble(v10, v11))"), "{}", t.hlsl); - assert!(t.hlsl.contains("round_ne_d((-asdouble(v10, v11)))"), "{}", t.hlsl); + assert!(t.hlsl.contains("round_ne_d(asdouble(v10, (v11 ^ 0x80000000u)))"), "{}", t.hlsl); if super::dxc::location().is_ok() { super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); }