From 46b7744cc9c39eb8f8cf06ea2c95e5164abcdf9b Mon Sep 17 00:00:00 2001 From: luckyteagirl <290248872+luckyteagirl@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:09:01 -0800 Subject: [PATCH] Recompiler: preserve zero sign in HLSL f16 multiplies --- crates/xcl/src/gpu/recomp/emit.rs | 20 +++++++++++++++-- crates/xcl/src/gpu/recomp/tests.rs | 35 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043..c61ac9129 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3199,7 +3199,16 @@ impl Em { let curh = if hi { format!("({cur} >> 16)") } else { format!("({cur} & 0xFFFFu)") }; Some(match name { "v_pk_add_f16" => fr(format!("{} + {}", fh(0, hi), fh(1, hi))), - "v_pk_mul_f16" => fr(format!("{} * {}", fh(0, hi), fh(1, hi))), + "v_pk_mul_f16" => { + let r = fr(format!("{} * {}", fh(0, hi), fh(1, hi))); + if clamp { + r + } else { + let neg_bits = if hi { neg_hi } else { neg }; + let flip = if (neg_bits & 1 != 0) ^ (neg_bits & 2 != 0) { "0x8000u" } else { "0u" }; + format!("f16_mul_zero_sign({r}, {a}, {b}, {flip})") + } + } "v_pk_fma_f16" => fr(format!("mad({}, {}, {})", fh(0, hi), fh(1, hi), fh(2, hi))), "v_pk_fmac_f16" => fr(format!("mad({}, {}, f16tof32({curh}))", fh(0, hi), fh(1, hi))), "v_pk_min_f16" => fr(format!("min({}, {})", fh(0, hi), fh(1, hi))), @@ -3389,7 +3398,10 @@ impl Em { "v_add_f16" => fr(format!("{} + {}", fh(0), fh(1))), "v_sub_f16" => fr(format!("{} - {}", fh(0), fh(1))), "v_subrev_f16" => fr(format!("{} - {}", fh(1), fh(0))), - "v_mul_f16" => fr(format!("{} * {}", fh(0), fh(1))), + "v_mul_f16" => { + let r = fr(format!("{} * {}", fh(0), fh(1))); + if m.clamp { r } else { format!("f16_mul_zero_sign({r}, {}, {}, 0u)", hb(0), hb(1)) } + } "v_min_f16" => fr(format!("min({}, {})", fh(0), fh(1))), "v_max_f16" => fr(format!("max({}, {})", fh(0), fh(1))), "v_fma_f16" => fr(format!("mad({}, {}, {})", fh(0), fh(1), fh(2))), @@ -6159,6 +6171,10 @@ uint sat_i32(int64_t r) { return select(r > (int64_t)0x7FFFFFFF, 0x7FFFFFFFu, se float div_fixup(float q, float d, float n) { return select(or(or(isnan(d), isnan(n)), or(d == 0.0f, or(isinf(d), isinf(n)))), n / d, q); } float fma_legacy(float a, float b, float c) { return (a == 0.0f || b == 0.0f) ? c : mad(a, b, c); } float mad_nf(float a, float b, float c) { precise float p = a * b; precise float r = p + c; return r; } +// DXC fast math may erase -0 in an f16 product. Restore its sign from the operand bits only if the rounded result is zero. +uint f16_mul_zero_sign(uint r, uint a, uint b, uint flip) { + return (r & 0x7FFFu) == 0u ? ((a ^ b ^ flip) & 0x8000u) : r; +} // IEEE-754 binary32 -> binary16 under RDNA2 MODE[3:2]: 0 = RNE, 1 = +inf, 2 = -inf, 3 = RTZ. uint f32_to_f16_rm(float f, uint rm) { uint x = asuint(f); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e64..73c2d8113 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3249,6 +3249,41 @@ fn gpu_valu_16_and_64_bit() { assert_eq!(r.sgpr[22] as u64 | (r.sgpr[23] as u64) << 32, carries, "v_mad_u64_u32 carry mask"); } +#[test] +fn gpu_f16_multiply_preserves_zero_sign() { + if !gpu_ready() { + return; + } + let p = prog(&[ + ("V_MUL_F16", "ENC_VOP2", &[("VDST", 2), ("SRC0", V), ("VSRC1", 1)], None), + ("V_PK_MUL_F16", "ENC_VOP3P", &[("VDST", 3), ("SRC0", V), ("SRC1", V + 1), ("OP_SEL_HI", 3), ("OP_SEL_HI_2", 1)], None), + ("V_PK_MUL_F16", "ENC_VOP3P", &[("VDST", 4), ("SRC0", V), ("SRC1", V + 1), ("OP_SEL_HI", 3), ("OP_SEL_HI_2", 1), ("NEG", 1), ("NEG_HI", 2)], None), + ("V_MUL_F16", "ENC_VOP3", &[("VDST", 5), ("SRC0", V), ("SRC1", V + 1), ("NEG", 1)], None), + ]); + let cases: [(u32, u32, u32); 4] = [ + (0x8000_0001, 0x3C00_8000, 0x8000_8000), // +subnormal * -0; -0 * +1 + (0x0000_8000, 0x8000_8000, 0x8000_0000), // -0 * -0; +0 * -0 + (0xBC00_3C00, 0x4000_4000, 0xC000_4000), // ordinary nonzero controls + (0x0001_8001, 0x8000_0000, 0x8000_8000), // -subnormal * +0; +subnormal * -0 + ]; + let mut st = KernelState::default(); + for (lane, regs) in st.vgpr.iter_mut().enumerate() { + let (a, b, _) = cases[lane % cases.len()]; + regs[0] = a; + regs[1] = b; + regs[2] = 0xBEEF_0000; + regs[5] = 0xCAFE_0000; + } + let result = runner::run_program(&p, &st, &[]).unwrap().0; + for (lane, regs) in result.vgpr.iter().enumerate() { + let (_, _, packed) = cases[lane % cases.len()]; + assert_eq!(regs[2], 0xBEEF_0000 | (packed & 0xFFFF), "lane {lane}: scalar mul"); + assert_eq!(regs[3], packed, "lane {lane}: packed mul"); + assert_eq!(regs[4], packed ^ 0x8000_8000, "lane {lane}: packed mul with negated sources"); + assert_eq!(regs[5], 0xCAFE_0000 | ((packed ^ 0x8000) & 0xFFFF), "lane {lane}: scalar mul with negated source"); + } +} + #[test] fn gpu_packed_math_dots_movrel() { if !gpu_ready() {