diff --git a/crates/xcl/src/gpu/dxil/lower_valu.rs b/crates/xcl/src/gpu/dxil/lower_valu.rs index 5e373200d..7598299a6 100644 --- a/crates/xcl/src/gpu/dxil/lower_valu.rs +++ b/crates/xcl/src/gpu/dxil/lower_valu.rs @@ -23,6 +23,21 @@ impl Src { } impl Lw { + /// abs / neg of source `i` on the sign bit (`sign` = 0x8000_0000, or 0x8000 for an f16 in the + /// low half): abs clears it, then neg flips it. The value stays bits, so a NaN keeps its payload + /// and a denormal passes through, as on the hardware for a move or a select. + pub fn sign_mod(&mut self, m: &Mods, i: usize, x: Value, sign: u32) -> Value { + let mut x = x; + if m.abs[i] { + x = self.andc(x, sign - 1); + } + if m.neg[i] { + let k = self.c(sign); + x = self.xor(x, k); + } + x + } + pub fn mods(d: &Decoded) -> Mods { let fv = |e: &Encoding, n: &str| rdna2::field_value(e, &d.words, n).unwrap_or(0); let bit = |e: &Encoding, n: &str| fv(e, n) == 1; @@ -419,7 +434,7 @@ impl Lw { } } let res: Value = match name { - "v_mov_b32" | "v_mov_fed_b32" => a, + "v_mov_b32" | "v_mov_fed_b32" => self.sign_mod(&m, 0, a, 0x8000_0000), "v_not_b32" => self.not(a), "v_bfrev_b32" => self.reversebits(a), "v_ffbh_u32" => self.lzcnt(a), @@ -455,6 +470,7 @@ impl Lw { } "v_cvt_f32_f16" => { let h = if m.opsel & 1 == 1 { self.lshrc(a, 16) } else { self.andc(a, 0xFFFF) }; + let h = self.sign_mod(&m, 0, h, 0x8000); fr!(self.s.f16_to_f32(h)) } "v_cvt_f16_f32" => { @@ -952,6 +968,8 @@ impl Lw { "v_cndmask_b32" => { let Some(Src::Pair(l, h)) = u.get(2).copied() else { return err(d, "mask") }; let bit = self.lane_of(l, h); + let a = self.sign_mod(&m, 0, a, 0x8000_0000); + let b = self.sign_mod(&m, 1, b, 0x8000_0000); self.sel(bit, b, a) } "v_permlane16_b32" | "v_permlanex16_b32" => { diff --git a/crates/xcl/src/gpu/dxil/lower_wide.rs b/crates/xcl/src/gpu/dxil/lower_wide.rs index 3927a9025..42ef8431e 100644 --- a/crates/xcl/src/gpu/dxil/lower_wide.rs +++ b/crates/xcl/src/gpu/dxil/lower_wide.rs @@ -895,7 +895,8 @@ impl Lw { } "v_cvt_f64_f32" => { let t = self.f64ty(); - let x = self.f(lh[0].0); + let x = self.sign_mod(&m, 0, lh[0].0, 0x8000_0000); + let x = self.f(x); self.s.m.cast(CastOp::FPExt, x, t) } "v_cvt_f64_i32" => { diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043..eea6e8fd1 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2978,6 +2978,20 @@ impl Em { s } + /// abs / neg of source `i` on the sign bit (`sign` = 0x80000000u, or 0x8000u for an f16 in + /// the low half): abs clears it, then neg flips it. The value stays bits, so a NaN keeps its + /// payload and a denormal passes through, as on the hardware for a move or a select. + fn sign_mods(m: &Mods, i: usize, u: &str, sign: u32) -> String { + let mut s = u.to_string(); + if m.abs[i] { + s = format!("({s} & {:#X}u)", sign - 1); + } + if m.neg[i] { + s = format!("({s} ^ {sign:#X}u)"); + } + s + } + /// Float result with output modifiers, as u32. fn fres(m: &Mods, e: &str) -> String { format!("asuint({})", Self::out_mods(m, e)) @@ -3558,7 +3572,7 @@ impl Em { "v_min_f64" => format!("min({}, {})", f64v(0), f64v(1)), "v_max_f64" => format!("max({}, {})", f64v(0), f64v(1)), "v_rcp_f64" => format!("rcp_d({})", f64v(0)), - "v_cvt_f64_f32" => format!("(double)asfloat({})", lo_hi[0].0), + "v_cvt_f64_f32" => format!("(double)asfloat({})", Self::sign_mods(&m, 0, &lo_hi[0].0, 0x8000_0000)), "v_cvt_f64_i32" => format!("(double)(int){}", lo_hi[0].0), "v_cvt_f64_u32" => format!("(double){}", lo_hi[0].0), "v_floor_f64" => format!("floor_d({})", f64v(0)), @@ -3681,7 +3695,7 @@ impl Em { let fr = |e: String| Self::fres(&m, &e); let wmask = if self.opt.wave64 { 63 } else { 31 }; let res: String = match name { - "v_mov_b32" | "v_mov_fed_b32" => a.clone(), + "v_mov_b32" | "v_mov_fed_b32" => Self::sign_mods(&m, 0, &a, 0x8000_0000), "v_not_b32" => format!("~{a}"), "v_bfrev_b32" => format!("reversebits({a})"), "v_ffbh_u32" => format!("(({a}) == 0u ? 0xFFFFFFFFu : 31u - firstbithigh({a}))"), @@ -3699,7 +3713,7 @@ impl Em { "v_cvt_f32_ubyte3" => fr(format!("(float)({a} >> 24)")), "v_cvt_f32_f16" => { let h = if m.opsel & 1 == 1 { format!("({a} >> 16)") } else { format!("({a} & 0xFFFFu)") }; - fr(format!("f16tof32({h})")) + fr(format!("f16tof32({})", Self::sign_mods(&m, 0, &h, 0x8000))) } "v_cvt_f16_f32" => { // the modifiers in f32 before the rounding: the same f16 (scaling by a power of @@ -3895,7 +3909,7 @@ impl Em { "v_cndmask_b32" => { let (l, h) = u.get(2).ok_or("mask")?.split_once('|').ok_or("mask")?; let bit = self.lane_of(l, h); - format!("select({bit}, {b}, {a})") + format!("select({bit}, {}, {})", Self::sign_mods(&m, 1, &b, 0x8000_0000), Self::sign_mods(&m, 0, &a, 0x8000_0000)) } "v_permlane16_b32" | "v_permlanex16_b32" => { // lane i of each row reads lane sel[i] of the same / the other row (32-lane halves) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e64..6420fb81f 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1182,6 +1182,59 @@ fn gpu_valu_float_and_int() { } } +/// ABS / NEG source modifiers on the VOP3 forms of the untyped move and select (applied on +/// the sign bit: NaN payloads, -0 and denormals pass bit-exact) and of the f16 → f32 and +/// f32 → f64 conversions (RDNA 2 ISA, V_CNDMASK_B32 / V_MOV_B32: "suitable for negating or +/// taking the absolute value"; LLVM `VOPSelectModsPat`, MC `gfx10_asm_vop2.s` / `gfx10_asm_vop1.s`). +#[test] +fn gpu_abs_neg_on_move_select_and_conversions() { + if !gpu_ready() { + return; + } + let code = prog(&[ + // v_cndmask_b32_e64 v5, -v1, |v2|, vcc + ("V_CNDMASK_B32", "ENC_VOP3", &[("VDST", 5), ("SRC0", V + 1), ("SRC1", V + 2), ("SRC2", 106), ("NEG", 1), ("ABS", 2)], None), + ("V_MOV_B32", "ENC_VOP3", &[("VDST", 6), ("SRC0", V + 1), ("NEG", 1)], None), // -v1 + ("V_MOV_B32", "ENC_VOP3", &[("VDST", 7), ("SRC0", V + 2), ("ABS", 1), ("NEG", 1)], None), // -|v2| + ("V_CVT_F32_F16", "ENC_VOP3", &[("VDST", 8), ("SRC0", V + 3), ("NEG", 1)], None), // -v3.h + ("V_CVT_F32_F16", "ENC_VOP3", &[("VDST", 9), ("SRC0", V + 3), ("ABS", 1)], None), // |v3.h| + ("V_CVT_F64_F32", "ENC_VOP3", &[("VDST", 10), ("SRC0", V + 4), ("NEG", 1)], None), // -v4 + ("V_CVT_F64_F32", "ENC_VOP3", &[("VDST", 12), ("SRC0", V + 4), ("ABS", 1)], None), // |v4| + ]); + // (v1, v2): ordinary, a quiet NaN with a payload against a negative signalling NaN, -0 against + // -0, and denormals + let pairs: [(u32, u32); 4] = + [(f(1.0), f(-2.0)), (0x7FC1_2345, 0xFFA0_0001), (0x8000_0000, 0x8000_0000), (0x0000_0001, 0x8000_0001)]; + // v3 (f16) with -v3 / |v3| as f32 + let halves: [(u32, f32, f32); 4] = [(0x3C00, -1.0, 1.0), (0x8000, 0.0, 0.0), (0xC000, 2.0, 2.0), (0x3800, -0.5, 0.5)]; + let singles: [f32; 4] = [1.0, -0.0, -3.5, 0.25]; + let mut st = KernelState::default(); + st.sgpr[106] = 0x5555_5555; // vcc: even lanes take src1 + st.sgpr[107] = 0x5555_5555; + for (l, r) in st.vgpr.iter_mut().enumerate() { + let k = (l / 2) % 4; // each input in a VCC-set and a VCC-clear lane + (r[1], r[2]) = pairs[k]; + r[3] = 0xDEAD_0000 | halves[k].0; + r[4] = f(singles[k]); + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + for l in 0..64 { + let v = &r.vgpr[l]; + let k = (l / 2) % 4; + let (a, b) = pairs[k]; + let want = if l % 2 == 0 { b & 0x7FFF_FFFF } else { a ^ 0x8000_0000 }; + assert_eq!(v[5], want, "lane {l} v_cndmask_b32 -v1, |v2| ({a:#010X}, {b:#010X})"); + assert_eq!(v[6], a ^ 0x8000_0000, "lane {l} v_mov_b32 -v1 ({a:#010X})"); + assert_eq!(v[7], b | 0x8000_0000, "lane {l} v_mov_b32 -|v2| ({b:#010X})"); + assert_eq!(v[8], f(halves[k].1), "lane {l} v_cvt_f32_f16 -v3 ({:#06X})", halves[k].0); + assert_eq!(v[9], f(halves[k].2), "lane {l} v_cvt_f32_f16 |v3| ({:#06X})", halves[k].0); + let x = singles[k] as f64; + let d = |i: usize| ((v[i + 1] as u64) << 32) | v[i] as u64; + assert_eq!(d(10), (-x).to_bits(), "lane {l} v_cvt_f64_f32 -v4 ({x})"); + assert_eq!(d(12), x.abs().to_bits(), "lane {l} v_cvt_f64_f32 |v4| ({x})"); + } +} + /// Output modifiers each instruction kind applies: omod on f64 (clamp stays in double /// precision) and on the f64 → f32 / f32 → f16 conversions, and the integer clamp (saturation) of the /// unsigned adds, the packed 16-bit adds and the integer dot products.