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
25 changes: 22 additions & 3 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,18 +1485,37 @@ impl<'a> Lower<'a> {
self.s.m.select(c, z, zero, m)
}

/// `a × b + addend` fused, with `0 × anything = 0` as in D3D9
/// (`v_fma_legacy_f32`).
/// Flush f32 subnormals to signed zero independently of the host's
/// floating-point mode. Signed zeros already have the desired bits.
fn flush_f32_subnormal(&mut self, value: V) -> V {
let c = self.cur;
let bits = self.as_int(value);
let exponent_mask = self.s.m.i32(0x7f80_0000);
let exponent = self.s.m.bin(c, Bin::And, bits, exponent_mask);
let zero = self.s.m.i32(0);
let zero_exponent = self.s.m.cmp(c, pred::EQ, exponent, zero);
let sign_mask = self.s.m.i32(0x8000_0000);
let sign = self.s.m.bin(c, Bin::And, bits, sign_mask);
let flushed = self.s.m.select(c, zero_exponent, sign, bits);
self.as_float(flushed)
}

/// `a × b + addend` fused, with `0 × anything = 0` as in D3D9.
/// Both RDNA2 legacy fused opcodes flush subnormal inputs and results.
fn fma_legacy(&mut self, a: V, b: V, addend: V) -> V {
let c = self.cur;
let a = self.flush_f32_subnormal(a);
let b = self.flush_f32_subnormal(b);
let addend = self.flush_f32_subnormal(addend);
let zero = self.s.m.f32(0);
let az = self.s.m.cmp(c, pred::OEQ, a, zero);
let bz = self.s.m.cmp(c, pred::OEQ, b, zero);
let z = self.s.m.bin(c, Bin::Or, az, bz);
let product_zero = self.s.m.bin(c, Bin::Add, zero, addend);
// shortcut: FMad lets the driver fuse, as for `v_fma_f32`.
let fused = self.fmad(a, b, addend);
self.s.m.select(c, z, product_zero, fused)
let value = self.s.m.select(c, z, product_zero, fused);
self.flush_f32_subnormal(value)
}

// ---- lane booleans ----
Expand Down
37 changes: 37 additions & 0 deletions crates/victory-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,43 @@ fn v_fma_legacy_f32_follows_the_d3d9_zero_rule() {
});
}

#[test]
fn legacy_fused_multiply_adds_flush_subnormal_inputs_and_results() {
let f = f32::to_bits;
let subnormal = 0x0000_0100;
let cases = [
// A subnormal factor flushed to zero must take the legacy zero path.
([subnormal, f(f32::INFINITY), f(2.0), 0], f(2.0)),
([f(f32::INFINITY), subnormal, f(2.0), 0], f(2.0)),
// A subnormal addend must also be flushed on the zero-factor path.
([0, f(3.0), subnormal, 0], 0),
// A normal product with subnormal exact result is flushed.
([f(2.0f32.powi(-63)), f(2.0f32.powi(-64)), 0, 0], 0),
([-2.0f32.powi(-63), 2.0f32.powi(-64), 0.0, 0.0].map(f), f(-0.0)),
// The normal values adjoining the subnormal range remain intact.
([f(f32::MIN_POSITIVE), f(1.0), 0, 0], f(f32::MIN_POSITIVE)),
];

let inputs: Vec<_> = cases.iter().map(|&(input, _)| input).collect();
let fma = vop3(0x140, 0, vgpr(1), vgpr(2), vgpr(3));
// VOP2 FMAC reads its addend from the destination register.
let copy_addend = vop1(0x001, 0, vgpr(3));
let fmac = [copy_addend, 0x0c00_0501]; // v_fmac_legacy_f32_e32 v0, v1, v2
for (name, body) in [
("v_fma_legacy_f32", fma.as_slice()),
("v_fmac_legacy_f32", fmac.as_slice()),
] {
let shader = build_valu_cs(body);
run_cases(&shader, &inputs, |backend, i, _, got| {
let want = cases[i].1;
assert_eq!(
got, want,
"{backend}: {name} case {i}: {got:#x}, want {want:#x}"
);
});
}
}

#[test]
fn v_fract_f32_never_returns_one() {
// v_fract_f32 v0, v1 (VOP1 opcode 0x020)
Expand Down