diff --git a/crates/xcl/src/gpu/dxil/prelude.rs b/crates/xcl/src/gpu/dxil/prelude.rs index a171fd099..26ab39359 100644 --- a/crates/xcl/src/gpu/dxil/prelude.rs +++ b/crates/xcl/src/gpu/dxil/prelude.rs @@ -186,11 +186,20 @@ impl Lw { let m = self.fmad(a, b, c); self.sel(cnd, c, m) } + /// With a NaN operand the hardware gives v_min(v_min(a, b), c): the smaller of the other + /// two (as #686 does on the HLSL path). pub fn med3f(&mut self, a: Value, b: Value, c: Value) -> Value { let x = self.fmin(a, b); let y = self.fmax(a, b); let y = self.fmin(y, c); - self.fmax(x, y) + let med = self.fmax(x, y); + let na = self.isnan(a); + let nb = self.isnan(b); + let nc = self.isnan(c); + let n = self.lor(na, nb); + let n = self.lor(n, nc); + let lo = self.fmin(x, c); + self.sel(n, lo, med) } /// `div_fixup` pub fn div_fixup(&mut self, q: Value, d: Value, n: Value) -> Value { diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index bfdb47201..fa28b8b40 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -6106,7 +6106,8 @@ uint f32_to_f16_rm(float f, uint rm) { return sign | (h + (uint)inc); } float mad_nf_f16(float a, float b, float c, uint rm) { precise float p = a * b; float p16 = f16tof32(f32_to_f16_rm(p, rm)); precise float r = p16 + c; return r; } -float med3f(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); } +// with a NaN operand the hardware gives v_min(v_min(a, b), c): the smaller of the other two +float med3f(float a, float b, float c) { return (isnan(a) || isnan(b) || isnan(c)) ? min(min(a, b), c) : max(min(a, b), min(max(a, b), c)); } uint med3u(uint a, uint b, uint c) { return max(min(a, b), min(max(a, b), c)); } int med3i(int a, int b, int c) { return max(min(a, b), min(max(a, b), c)); } // x - floor(x) below 1.0 (RDNA: a NaN stays NaN and ±inf gives NaN – min() would return the diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index 44dfbb039..aaab886d4 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -2732,7 +2732,8 @@ pub(crate) mod tests { } pub(crate) fn check_f32_three_source(_: &[u32], v: &[Vec]) { - let med3 = |a: f32, b: f32, c: f32| a.min(b).max(a.max(b).min(c)); + // with a NaN operand v_med3_f32 gives the smaller of the other two (#686) + let med3 = |a: f32, b: f32, c: f32| if a.is_nan() || b.is_nan() || c.is_nan() { a.min(b).min(c) } else { a.min(b).max(a.max(b).min(c)) }; for (l, r) in v.iter().enumerate() { let f = |i: usize| f32::from_bits(r[i]); let (x, y) = (l as f32, l as f32 - 16.0); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index b40953c60..58965cc4f 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2742,6 +2742,46 @@ fn gpu_images_a16_d16() { } } +/// v_med3_f32 / v_med3_f16 with a quiet NaN operand: the hardware returns +/// v_min(v_min(s0, s1), s2), the smaller of the other two (LLVM's amdgcn_fmed3 folding, +/// AMDGPUInstCombineIntrinsic.cpp), wherever the NaN is; without a NaN, the median (#678). +#[test] +fn gpu_med3_with_a_nan_operand_is_the_min_of_the_others() { + for wave64 in gpu_waves() { + let p = prog(&[ + ("V_MED3_F32", "ENC_VOP3", &[("VDST", 10), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2)], None), + ("V_MED3_F16", "ENC_VOP3", &[("VDST", 11), ("SRC0", V + 3), ("SRC1", V + 4), ("SRC2", V + 5)], None), + ]); + let (dxil, mem) = kernel_wave(&p, wave64); + let n = f32::NAN; + let h = |x: f32| if x.is_nan() { 0x7E00 } else { half_bits(x) }; + // (s0, s1, s2, want) + let cases: [(f32, f32, f32, f32); 10] = [ + (n, 2.0, 1.0, 1.0), (n, 1.0, 2.0, 1.0), (2.0, n, 1.0, 1.0), (1.0, n, 2.0, 1.0), + (2.0, 1.0, n, 1.0), (1.0, 2.0, n, 1.0), (n, 0.0, 1.0, 0.0), (-3.0, n, 5.0, -3.0), + (3.0, 1.0, 2.0, 2.0), (n, n, 4.0, 4.0), + ]; + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let (a, b, c, _) = cases[l % cases.len()]; + (r[0], r[1], r[2]) = (a.to_bits(), b.to_bits(), c.to_bits()); + (r[3], r[4], r[5]) = (h(a), h(b), h(c)); + } + let r = runner::run(&dxil, &st, &mem).unwrap(); + let mut bad = Vec::new(); + for (l, (a, b, c, want)) in cases.iter().enumerate() { + let (g32, g16) = (r.vgpr[l][10], r.vgpr[l][11] & 0xFFFF); + if g32 != want.to_bits() { + bad.push(format!("med3_f32({a}, {b}, {c}) = {}, want {want}", f32::from_bits(g32))); + } + if g16 != h(*want) { + bad.push(format!("med3_f16({a}, {b}, {c}) = {g16:#06x}, want {:#06x}", h(*want))); + } + } + assert!(bad.is_empty(), "wave{}: {} wrong\n{}", if wave64 { 64 } else { 32 }, bad.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 {