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:
1 parent
d2a4bae1f4
commit
bf84134b2b
2 files changed
+59
-14
No files matched your search
@@ -1337,6 +1337,25 @@ impl<'a> Lower<'a> {
|
||||
)
|
||||
}
|
||||
|
||||
/// The hardware's maximum (`max`) or minimum of two floats: as DXIL's
|
||||
/// FMax/FMin, but -0 orders below +0 (RDNA2 ISA, `v_max_f32`), where
|
||||
/// the compare and select DXIL specifies gives either zero.
|
||||
fn max_min(&mut self, max: bool, a: V, b: V) -> V {
|
||||
let c = self.cur;
|
||||
let r = self.binary(if max { 35 } else { 36 }, a, b);
|
||||
let (ia, ib) = (self.as_int(a), self.as_int(b));
|
||||
let either = self.s.m.bin(c, Bin::Or, ia, ib);
|
||||
let magnitude = self.s.m.i32(0x7FFF_FFFF);
|
||||
let either = self.s.m.bin(c, Bin::And, either, magnitude);
|
||||
let zero = self.s.m.i32(0);
|
||||
let zeros = self.s.m.cmp(c, pred::EQ, either, zero);
|
||||
// Of two zeros, +0 unless both are -0 (max), -0 unless neither (min).
|
||||
let op = if max { Bin::And } else { Bin::Or };
|
||||
let sign = self.s.m.bin(c, op, ia, ib);
|
||||
let signed = self.as_float(sign);
|
||||
self.s.m.select(c, zeros, signed, r)
|
||||
}
|
||||
|
||||
fn fmad(&mut self, a: V, b: V, c: V) -> V {
|
||||
self.s.op(
|
||||
self.cur,
|
||||
@@ -3673,8 +3692,8 @@ impl<'a> Lower<'a> {
|
||||
"v_subrev_f32" => self.s.m.bin(c, Bin::Sub, bb, a),
|
||||
"v_mul_f32" => self.s.m.bin(c, Bin::Mul, a, bb),
|
||||
"v_mul_legacy_f32" => self.legacy_mul(a, bb),
|
||||
"v_max_f32" => self.binary(35, a, bb),
|
||||
_ => self.binary(36, a, bb),
|
||||
"v_max_f32" => self.max_min(true, a, bb),
|
||||
_ => self.max_min(false, a, bb),
|
||||
};
|
||||
float(self, r)
|
||||
}
|
||||
@@ -3724,12 +3743,12 @@ impl<'a> Lower<'a> {
|
||||
let (a, bb, cc) = (f(self, 0)?, f(self, 1)?, f(self, 2)?);
|
||||
let r = match name {
|
||||
"v_max3_f32" => {
|
||||
let t = self.binary(35, a, bb);
|
||||
self.binary(35, t, cc)
|
||||
let t = self.max_min(true, a, bb);
|
||||
self.max_min(true, t, cc)
|
||||
}
|
||||
"v_min3_f32" => {
|
||||
let t = self.binary(36, a, bb);
|
||||
self.binary(36, t, cc)
|
||||
let t = self.max_min(false, a, bb);
|
||||
self.max_min(false, t, cc)
|
||||
}
|
||||
_ => self.med3(a, bb, cc),
|
||||
};
|
||||
@@ -4265,8 +4284,8 @@ impl<'a> Lower<'a> {
|
||||
"v_sub_f16" => self.s.m.bin(c, Bin::Sub, a, b),
|
||||
"v_subrev_f16" => self.s.m.bin(c, Bin::Sub, b, a),
|
||||
"v_mul_f16" => self.s.m.bin(c, Bin::Mul, a, b),
|
||||
"v_max_f16" => self.binary(35, a, b),
|
||||
_ => self.binary(36, a, b),
|
||||
"v_max_f16" => self.max_min(true, a, b),
|
||||
_ => self.max_min(false, a, b),
|
||||
};
|
||||
return float_result(self, r);
|
||||
}
|
||||
@@ -4296,12 +4315,12 @@ impl<'a> Lower<'a> {
|
||||
// product is exact; the sum rounds twice).
|
||||
"v_fma_f16" | "v_fmac_f16" | "v_mad_f16" | "v_mac_f16" => self.fmad(a, b, acc),
|
||||
"v_max3_f16" => {
|
||||
let t = self.binary(35, a, b);
|
||||
self.binary(35, t, acc)
|
||||
let t = self.max_min(true, a, b);
|
||||
self.max_min(true, t, acc)
|
||||
}
|
||||
"v_min3_f16" => {
|
||||
let t = self.binary(36, a, b);
|
||||
self.binary(36, t, acc)
|
||||
let t = self.max_min(false, a, b);
|
||||
self.max_min(false, t, acc)
|
||||
}
|
||||
_ => self.med3(a, b, acc),
|
||||
};
|
||||
@@ -4487,8 +4506,8 @@ impl<'a> Lower<'a> {
|
||||
let mut f = match op {
|
||||
"add_f16" => self.s.m.bin(c, Bin::Add, xs[0], xs[1]),
|
||||
"mul_f16" => self.s.m.bin(c, Bin::Mul, xs[0], xs[1]),
|
||||
"min_f16" => self.binary(36, xs[0], xs[1]),
|
||||
"max_f16" => self.binary(35, xs[0], xs[1]),
|
||||
"min_f16" => self.max_min(false, xs[0], xs[1]),
|
||||
"max_f16" => self.max_min(true, xs[0], xs[1]),
|
||||
// shortcut: FMad in f32, as v_fma_f16.
|
||||
_ => self.fmad(xs[0], xs[1], xs[2]),
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user