Shaders: maximums and minimums order -0 below +0, as RDNA2 does

v_max_f32, v_min_f32, their three-operand and f16 forms and the packed
v_pk_max_f16/v_pk_min_f16 were lowered to DXIL's FMax and FMin. DXIL
specifies those as a compare and select, so with a -0 and a +0 either
zero can come out, depending on the operand order and the host; RDNA2
orders -0 below +0, so max(-0, +0) is +0 and min(-0, +0) is -0 in
either order. A -0 that should have been +0 then turns, say, a
following reciprocal square root's +inf into -inf.

A new helper keeps FMax/FMin for everything but two zeros, which it
resolves by their sign bits.
This commit is contained in:
Duff committed 2026-11-24 04:25:55 +01:00
1 parent d2a4bae1f4
commit bf84134b2b
2 files changed
+59 -14

No files matched your search

@@ -279,6 +279,32 @@ fn v_med3_f32_matches_rdna2_with_nan() {
});
}
/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and
/// FMin compare and select, which can give either zero.
#[test]
fn max_and_min_order_negative_zero_below_positive_zero() {
let (pz, nz) = (0_u32, 0x8000_0000_u32);
// Each case has a -0 and a +0 among its first two operands.
let cases = [
[nz, pz, nz, 0],
[pz, nz, nz, 0],
[nz, pz, pz, 0],
[pz, nz, pz, 0],
];
let ops = [
("v_max_f32", 0x110, pz),
("v_min_f32", 0x10F, nz),
("v_max3_f32", 0x154, pz),
("v_min3_f32", 0x151, nz),
];
for (name, op, want) in ops {
let shader = build_valu_cs(&vop3(op, 0, vgpr(1), vgpr(2), vgpr(3)));
run_cases(&shader, &cases, |backend, _, inp, got| {
assert_eq!(got, want, "{backend}: {name} {inp:x?}");
});
}
}
#[test]
fn v_med3_f16_matches_rdna2_with_nan() {
// v_med3_f16 v0, v1, v2, v3 (VOP3 opcode 0x357)