Merge pull request #376 from LouisRiverstone/shader-cvt-norm-f16
Shaders: lower v_cvt_norm_i16_f16 and v_cvt_norm_u16_f16
This commit is contained in:
2 files changed
+127
No files matched your search
@@ -4255,6 +4255,8 @@ impl<'a> Lower<'a> {
|
||||
"v_min_i16",
|
||||
"v_cvt_u16_f16",
|
||||
"v_cvt_i16_f16",
|
||||
"v_cvt_norm_i16_f16",
|
||||
"v_cvt_norm_u16_f16",
|
||||
];
|
||||
let all = [FLOAT1, FLOAT2, FLOAT3, INT];
|
||||
if !all.iter().any(|l| l.contains(&name))
|
||||
@@ -4393,6 +4395,13 @@ impl<'a> Lower<'a> {
|
||||
};
|
||||
return float_result(self, r);
|
||||
}
|
||||
if matches!(name, "v_cvt_norm_i16_f16" | "v_cvt_norm_u16_f16") {
|
||||
if v.clamp || v.omod != 0 {
|
||||
return Err("modifiers on a 16-bit integer operation");
|
||||
}
|
||||
let r = self.cvt_norm16(inst, name == "v_cvt_norm_i16_f16")?;
|
||||
return self.write_half(inst, r, guard);
|
||||
}
|
||||
// 16-bit integers: computed on the low halves, the low 16 bits kept.
|
||||
if v.clamp || v.omod != 0 || v.abs != 0 || v.neg != 0 {
|
||||
return Err("modifiers on a 16-bit integer operation");
|
||||
@@ -4457,6 +4466,78 @@ impl<'a> Lower<'a> {
|
||||
self.write_half(inst, r, guard)
|
||||
}
|
||||
|
||||
/// `v_cvt_norm_i16_f16` / `v_cvt_norm_u16_f16`: the f16 source (with its
|
||||
/// abs/neg modifiers) clamped to [-1, 1] or [0, 1], times 32767 or 65535,
|
||||
/// rounded to nearest even once from the exact product; NaN gives 0.
|
||||
/// Worked on the f16's bits in integers, because in f32 the product
|
||||
/// (up to 26 significant bits) would round before the rounding to an
|
||||
/// integer.
|
||||
fn cvt_norm16(&mut self, inst: &Inst, signed: bool) -> Result<V, Why> {
|
||||
let Fields::Valu(v) = &inst.fields else {
|
||||
unreachable!()
|
||||
};
|
||||
let c = self.cur;
|
||||
let k = |l: &mut Self, x: u32| l.s.m.i32(x);
|
||||
let mut h = self.half_src(inst, 0)?;
|
||||
if v.abs & 1 != 0 {
|
||||
let m = k(self, 0x7FFF);
|
||||
h = self.s.m.bin(c, Bin::And, h, m);
|
||||
}
|
||||
if v.neg & 1 != 0 {
|
||||
let m = k(self, 0x8000);
|
||||
h = self.s.m.bin(c, Bin::Xor, h, m);
|
||||
}
|
||||
let scale = if signed { 0x7FFF } else { 0xFFFF };
|
||||
let (k0, k1, k10) = (k(self, 0), k(self, 1), k(self, 10));
|
||||
let mag_mask = k(self, 0x7FFF);
|
||||
let mag = self.s.m.bin(c, Bin::And, h, mag_mask);
|
||||
let k15 = k(self, 15);
|
||||
let neg = self.s.m.bin(c, Bin::LShr, h, k15);
|
||||
let neg = self.s.m.cmp(c, pred::NE, neg, k0);
|
||||
// |x| < 1: x = m * 2^-s with m < 2^11 and 11 <= s <= 24.
|
||||
let exp = self.s.m.bin(c, Bin::LShr, mag, k10);
|
||||
let man_mask = k(self, 0x3FF);
|
||||
let man = self.s.m.bin(c, Bin::And, mag, man_mask);
|
||||
let implicit = k(self, 0x400);
|
||||
let normal = self.s.m.bin(c, Bin::Or, man, implicit);
|
||||
let sub = self.s.m.cmp(c, pred::EQ, exp, k0);
|
||||
let m = self.s.m.select(c, sub, man, normal);
|
||||
let k25 = k(self, 25);
|
||||
let s_norm = self.s.m.bin(c, Bin::Sub, k25, exp);
|
||||
let k24 = k(self, 24);
|
||||
let sh = self.s.m.select(c, sub, k24, s_norm);
|
||||
let ks = k(self, scale);
|
||||
let p = self.s.m.bin(c, Bin::Mul, m, ks);
|
||||
let q = self.s.m.bin(c, Bin::LShr, p, sh);
|
||||
let one_s = self.s.m.bin(c, Bin::Shl, k1, sh);
|
||||
let low_mask = self.s.m.bin(c, Bin::Sub, one_s, k1);
|
||||
let rem = self.s.m.bin(c, Bin::And, p, low_mask);
|
||||
let half = self.s.m.bin(c, Bin::LShr, one_s, k1);
|
||||
let above = self.s.m.cmp(c, pred::IUGT, rem, half);
|
||||
let tie = self.s.m.cmp(c, pred::EQ, rem, half);
|
||||
let odd = self.s.m.bin(c, Bin::And, q, k1);
|
||||
let odd = self.s.m.cmp(c, pred::NE, odd, k0);
|
||||
let tie_up = self.s.m.bin(c, Bin::And, tie, odd);
|
||||
let up = self.s.m.bin(c, Bin::Or, above, tie_up);
|
||||
let r = self.s.m.select(c, up, k1, k0);
|
||||
let r = self.s.m.bin(c, Bin::Add, q, r);
|
||||
// |x| >= 1 (and infinity) saturates; NaN gives 0.
|
||||
let one_h = k(self, 0x3C00);
|
||||
let big = self.s.m.cmp(c, pred::IUGE, mag, one_h);
|
||||
let r = self.s.m.select(c, big, ks, r);
|
||||
let inf = k(self, 0x7C00);
|
||||
let nan = self.s.m.cmp(c, pred::IUGT, mag, inf);
|
||||
let r = self.s.m.select(c, nan, k0, r);
|
||||
let r = if signed {
|
||||
let n = self.s.m.bin(c, Bin::Sub, k0, r);
|
||||
self.s.m.select(c, neg, n, r)
|
||||
} else {
|
||||
self.s.m.select(c, neg, k0, r)
|
||||
};
|
||||
let m16 = k(self, 0xFFFF);
|
||||
Ok(self.s.m.bin(c, Bin::And, r, m16))
|
||||
}
|
||||
|
||||
/// Half `high` of source `k` of a packed (VOP3P) operation, in the low
|
||||
/// 16 bits: op_sel picks the half the low result reads, op_sel_hi the
|
||||
/// half the high result reads. Inline float constants are f16 values.
|
||||
|
||||
@@ -420,6 +420,52 @@ fn v_fract_f16_never_returns_one() {
|
||||
});
|
||||
}
|
||||
|
||||
/// RDNA2's flt16_to_snorm16 / flt16_to_unorm16: clamp, scale by 32767 or
|
||||
/// 65535, round the exact product to nearest even; NaN gives 0.
|
||||
fn cvt_norm16_reference(bits: u16, signed: bool) -> u16 {
|
||||
let x = f16_to_f32(bits);
|
||||
if x.is_nan() {
|
||||
return 0;
|
||||
}
|
||||
let (lo, scale) = if signed {
|
||||
(-1.0, 32767.0)
|
||||
} else {
|
||||
(0.0, 65535.0)
|
||||
};
|
||||
// f64 holds the product of an f16 and a 16-bit scale exactly.
|
||||
let p = f64::from(x.clamp(lo, 1.0)) * scale;
|
||||
p.round_ties_even() as i32 as u16
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_cvt_norm_i16_and_u16_f16_round_the_exact_product() {
|
||||
// Every 251st f16 pattern, plus the values where an f32 product would
|
||||
// round before the rounding to an integer, ties, and the edges.
|
||||
let mut bits: Vec<u16> = (0..=u16::MAX).step_by(251).collect();
|
||||
bits.extend([
|
||||
0x0000, 0x8000, 0x0001, 0x8001, 0x03FF, 0x0400, 0x3800, 0xB800, 0x3801, 0xB801, 0x37FF,
|
||||
0x3BFF, 0xBBFF, 0x3C00, 0xBC00, 0x3C01, 0x7BFF, 0x7C00, 0xFC00, 0x7E00, 0xFE00, 0x7C01,
|
||||
0x3A9B, 0xBA9B, 0x2E66, 0x1400,
|
||||
]);
|
||||
for (op, signed, what) in [
|
||||
(0x063u16, true, "v_cvt_norm_i16_f16"),
|
||||
(0x064, false, "v_cvt_norm_u16_f16"),
|
||||
] {
|
||||
// v_cvt_norm_*16_f16 v0, v1 (VOP1); v0's high half starts at 0.
|
||||
let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
|
||||
let inputs: Vec<[u32; 4]> = bits.iter().map(|&b| [u32::from(b), 0, 0, 0]).collect();
|
||||
run_cases(&shader, &inputs, |backend, i, _, got| {
|
||||
let want = cvt_norm16_reference(bits[i], signed);
|
||||
assert_eq!(
|
||||
got,
|
||||
u32::from(want),
|
||||
"{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
|
||||
bits[i],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_sin_and_v_cos_f32_reduce_large_turns() {
|
||||
let sin_shader = build_valu_cs(&[vop1(0x035, 0, vgpr(1))]);
|
||||
|
||||
Reference in new issue
Block a user