Skip to content
Open
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
10 changes: 9 additions & 1 deletion crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6880,11 +6880,19 @@ impl<'a> Lower<'a> {
let b = self.s.m.cast(c, Cast::ZExt, b, i64t);
let product = self.s.m.bin(c, Bin::Mul, a, b);
let r = self.s.m.bin(c, Bin::Add, product, x);
let carry = self.s.m.cmp(c, pred::IULT, r, x);
let Some(Operand::Vector { reg, .. }) = v.dst else {
return Err("a 64-bit result");
};
let (lo, hi) = self.split64(r);
// The carry is `r < c` (unsigned), on the dwords: as one i64
// compare, AMD's D3D12 compiler answers wrong where c shares a
// register with the second factor (RX 7900 XT); WARP is right.
let (xlo, xhi) = self.split64(x);
let hi_lt = self.s.m.cmp(c, pred::IULT, hi, xhi);
let hi_eq = self.s.m.cmp(c, pred::EQ, hi, xhi);
let lo_lt = self.s.m.cmp(c, pred::IULT, lo, xlo);
let lo_lt = self.s.m.bin(c, Bin::And, hi_eq, lo_lt);
let carry = self.s.m.bin(c, Bin::Or, hi_lt, lo_lt);
self.write(inst, Reg::V(reg), lo, guard);
self.write(inst, Reg::V(reg + 1), hi, guard);
if let Some(Operand::Scalar { code, .. }) = v.carry
Expand Down
82 changes: 82 additions & 0 deletions crates/victory-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,88 @@ fn cvt_16bit_int_from_f16_saturates_and_honors_modifiers() {
);
}

/// `v_mad_u64_u32` gives `{carry, D} = a * b + c` (65 bits): the two dwords
/// of the result and the carry out, with the addend in a register pair of
/// its own and sharing a register with the second factor. The addends give
/// carries decided by the high dwords and by the low ones. Every case runs;
/// the mismatches are counted by device, form and output before the test
/// fails.
#[test]
fn v_mad_u64_u32_matches_its_definition() {
let factors: [u32; 6] = [0, 1, 2, 0xFFFF, 0x8000_0000, 0xFFFF_FFFF];
let addends: [u64; 9] = [
0,
1,
0xFFFF_FFFF,
0x1_0000_0000,
0x1_FFFF_FFFF,
0x7FFF_FFFF_FFFF_FFFF,
0xFFFF_FFFE_0000_0002,
0xFFFF_FFFF_0000_0000,
0xFFFF_FFFF_FFFF_FFFF,
];
let mut inputs: Vec<[u32; 4]> = Vec::new();
for &a in &factors {
for &b in &factors {
for &c in &addends {
inputs.push([a, b, c as u32, (c >> 32) as u32]);
}
}
}
let mut counts: std::collections::BTreeMap<(String, &str, &str), usize> = Default::default();
let mut first: Vec<String> = Vec::new();
// v_mad_u64_u32 v[5:6], s20, v1, v2, v[3:4] and, sharing v2,
// v_mad_u64_u32 v[5:6], s20, v1, v2, v[2:3] (VOP3B: s20 in bits 14:8);
// then v0 = the carry (v_cndmask_b32_e64 v0, 0, 1, s20), v5 or v6.
for (third, form) in [(3u8, "v[3:4]"), (2, "v[2:3]")] {
for output in ["carry", "low", "high"] {
let mut body = vec![
0x35 << 26 | 0x176 << 16 | 20 << 8 | 5,
vgpr(1) as u32 | (vgpr(2) as u32) << 9 | (vgpr(third) as u32) << 18,
];
match output {
"carry" => body.extend_from_slice(&vop3(0x101, 0, 0x80, 0x81, sgpr(20))),
"low" => body.push(vop1(0x001, 0, vgpr(5))),
_ => body.push(vop1(0x001, 0, vgpr(6))),
}
let shader = build_valu_cs(&body);
let at = usize::from(third) - 1;
run_cases(&shader, &inputs, |backend, _, inp, got| {
let (a, b) = (inp[0], inp[1]);
let c = u64::from(inp[at]) | u64::from(inp[at + 1]) << 32;
let sum = u128::from(a) * u128::from(b) + u128::from(c);
let want = match output {
"carry" => (sum >> 64) as u32,
"low" => sum as u32,
_ => (sum >> 32) as u32,
};
if got != want {
*counts
.entry((backend.to_owned(), form, output))
.or_default() += 1;
if first.len() < 12 {
first.push(format!(
"{backend} {form} {output}: v_mad_u64_u32({a:#010x}, {b:#010x}, \
{c:#018x}) = {got:#010x}, want {want:#010x}"
));
}
}
});
}
}
for ((backend, form, output), n) in &counts {
eprintln!(
"v_mad_u64_u32 {backend} {form} {output}: {n} of {} wrong",
inputs.len()
);
}
assert!(
counts.is_empty(),
"v_mad_u64_u32 mismatches: {counts:?}\n{}",
first.join("\n")
);
}

const S_BARRIER: u32 = 0xBF8A_0000;

/// VOP2 `op vdst, src0, vsrc1`.
Expand Down