Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 63 additions & 9 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7207,15 +7207,65 @@ impl<'a> Lower<'a> {
})
}

/// Raw binary64 source words. Class tests keep NaNs and denormals in
/// their integer representation and only modify the sign bit.
fn class_f64_words(&mut self, inst: &Inst) -> Result<(V, V), Why> {
let Fields::Valu(v) = &inst.fields else {
unreachable!()
};
if v.sdwa.is_some() || v.dpp.is_some() {
return Err("64-bit floating operands with SDWA or DPP");
}
let operand = *v.src.first().ok_or("a missing operand")?;
let pair = |l: &mut Self, lo: Reg, hi: Reg| -> Result<(V, V), Why> {
Ok((
l.i32_of(inst.read(lo).ok_or("an unread operand")?)?,
l.i32_of(inst.read(hi).ok_or("an unread operand")?)?,
))
};
let (lo, mut hi) = match operand {
Operand::Vector { reg, .. } => {
let next = reg.checked_add(1).ok_or("a missing high word")?;
pair(self, Reg::V(reg), Reg::V(next))?
}
Operand::Scalar { code, .. } if code != NULL => {
let next = code.checked_add(1).ok_or("a missing high word")?;
pair(self, Reg::S(code), Reg::S(next))?
}
Operand::Literal(high) => (self.s.m.i32(0), self.s.m.i32(high)),
Operand::Const(code @ (128..=208 | 240..=248)) => {
let value = crate::resource::inline(code).ok_or("an unknown inline constant")?;
let bits = match code {
128..=208 => i64::from(value as i32) as u64,
248 => 0x3fc4_5f30_6dc9_c882,
_ => f64::from(f32::from_bits(value)).to_bits(),
};
(self.s.m.i32(bits as u32), self.s.m.i32((bits >> 32) as u32))
}
_ => (self.operand(inst, &operand)?, self.s.m.i32(0)),
};
if v.abs & 1 != 0 {
let mask = self.s.m.i32(0x7fff_ffff);
hi = self.s.m.bin(self.cur, Bin::And, hi, mask);
}
if v.neg & 1 != 0 {
let sign = self.s.m.i32(0x8000_0000);
hi = self.s.m.bin(self.cur, Bin::Xor, hi, sign);
}
Ok((lo, hi))
}

fn compare(&mut self, inst: &Inst, rest: &str) -> Result<(), Why> {
let (op, ty) = rest.rsplit_once('_').ok_or("a compare")?;
let c = self.cur;
let r = match ty {
"f32" | "f16" if op == "class" => {
"f32" | "f16" | "f64" if op == "class" => {
// Bit k of src1 tests class k: signalling NaN, quiet NaN,
// -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf.
let f16 = ty == "f16";
let x = if f16 {
let (x, low) = if ty == "f64" {
let (low, high) = self.class_f64_words(inst)?;
(high, Some(low))
} else if ty == "f16" {
let Fields::Valu(v) = &inst.fields else {
unreachable!()
};
Expand All @@ -7228,17 +7278,17 @@ impl<'a> Lower<'a> {
let s = self.s.m.i32(0x8000);
h = self.s.m.bin(c, Bin::Xor, h, s);
}
h
(h, None)
} else {
let x = self.fsrc(inst, 0)?;
self.as_int(x)
(self.as_int(x), None)
};
let mask = self.src(inst, 1)?;
let k = |l: &mut Self, v: u32| l.s.m.i32(v);
let (exp_sh, sign_sh, exp_mask, mant_mask, quiet_bit) = if f16 {
(10, 15, 0x1F, 0x03FF, 0x0200)
} else {
(23, 31, 0xFF, 0x7F_FFFF, 0x40_0000)
let (exp_sh, sign_sh, exp_mask, mant_mask, quiet_bit) = match ty {
"f64" => (20, 31, 0x7FF, 0xF_FFFF, 0x8_0000),
"f16" => (10, 15, 0x1F, 0x03FF, 0x0200),
_ => (23, 31, 0xFF, 0x7F_FFFF, 0x40_0000),
};
let (k_exp, k_sign, k_emax, mant, quiet) = (
k(self, exp_sh),
Expand All @@ -7250,6 +7300,10 @@ impl<'a> Lower<'a> {
let e = self.s.m.bin(c, Bin::LShr, x, k_exp);
let e = self.s.m.bin(c, Bin::And, e, k_emax);
let m = self.s.m.bin(c, Bin::And, x, mant);
let m = match low {
Some(low) => self.s.m.bin(c, Bin::Or, m, low),
None => m,
};
let neg = self.s.m.bin(c, Bin::LShr, x, k_sign);
let zero = k(self, 0);
let (e0, emax) = (
Expand Down
80 changes: 80 additions & 0 deletions crates/victory-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,86 @@ fn fspx_programs_run_with_the_hardwares_dot_products() {
}
}

#[test]
fn v_cmp_class_f64_matches_rdna2() {
let select = vop3(0x101, 0, 128, 129, sgpr(6)); // v_cndmask_b32_e64 v0, 0, 1, s6
let compare = |src0, modifiers: [u32; 2], literal| {
let mut words = vop3(0x0a8, 6, src0, vgpr(3), 0); // v_cmp_class_f64_e64 s6, src0, v3
words[0] |= modifiers[0];
words[1] |= modifiers[1];
let mut body = Vec::from(words);
body.extend(literal);
body.extend(select);
build_valu_cs(&body)
};
let cases = [
(0x7ff0_0000_0000_0001u64, 0), // sNaN with only a low-word payload
(0xfff8_0000_0000_0000, 1), // negative qNaN
(0xfff0_0000_0000_0000, 2), // -inf
(0xbff0_0000_0000_0000, 3), // -1.0
(0x8000_0000_0000_0001, 4), // smallest negative subnormal
(0x8000_0000_0000_0000, 5), // -0
(0x0000_0000_0000_0000, 6), // +0
(0x0000_0000_0000_0001, 7), // smallest positive subnormal
(0x3ff0_0000_0000_0000, 8), // +1.0
(0x7ff0_0000_0000_0000, 9), // +inf
];
let cases: Vec<_> = cases
.into_iter()
.flat_map(|(bits, class)| {
let bit = 1 << class;
[
[bits as u32, (bits >> 32) as u32, bit, 1],
[bits as u32, (bits >> 32) as u32, 0x3ff ^ bit, 0],
]
})
.collect();
let check = |label: &str, _, input: [u32; 4], actual| {
assert_eq!(actual, input[3], "{label}: {input:08x?}");
};
for source in [vgpr(1), sgpr(0)] {
let shader = compare(source, [0, 0], None);
let container = crate::Container::parse(&shader.dxil).unwrap();
let features = container.part(crate::FourCC::SFI0).unwrap();
let flags = u64::from_le_bytes(features.data.try_into().unwrap());
assert_eq!(flags & super::shader::flags::INT64, 0);
run_cases(&shader, &cases, check);
}
// ABS and NEG change only the sign bit; the low-word payload stays intact.
run_cases(
&compare(vgpr(1), [1 << 8, 1 << 29], None),
&[[1, 0, 1 << 4, 1], [1, 0, 1 << 7, 0], [0, 0, 1 << 5, 1]],
check,
);
// A 32-bit literal occupies the high word of a binary64 source.
run_cases(
&compare(255, [0, 0], Some(0x7ff0_0000)),
&[[0, 0, 1 << 9, 1]],
check,
);
// Integer inline constants supply raw bits; floating inline constants use binary64.
for (source, class) in [(129, 7), (193, 1), (243, 3), (248, 8)] {
run_cases(
&compare(source, [0, 0], None),
&[[0, 0, 1 << class, 1]],
check,
);
}
}

#[test]
fn v_cmpx_class_f64_filters_stores() {
let shader = build_valu_cs(&[
0x7c00_0000 | 0x0b8 << 17 | 3 << 9 | vgpr(1) as u32, // v_cmpx_class_f64 v[1:2], v3
vop1(1, 0, 129), // v_mov_b32 v0, 1
]);
run_cases(
&shader,
&[[1, 0, 1 << 7, 1], [1, 0, 1 << 6, 0]],
|label, _, input, actual| assert_eq!(actual, input[3], "{label}: {input:08x?}"),
);
}

/// `v_pk_fmac_f16` (VOP2): each half of the destination plus the product
/// of the sources' halves.
#[test]
Expand Down