|
@@ -4255,6 +4255,8 @@ impl<'a> Lower<'a> {
|
|
|
"v_min_i16",
|
|
"v_min_i16",
|
|
|
"v_cvt_u16_f16",
|
|
"v_cvt_u16_f16",
|
|
|
"v_cvt_i16_f16",
|
|
"v_cvt_i16_f16",
|
|
|
|
|
+ "v_cvt_norm_i16_f16",
|
|
|
|
|
+ "v_cvt_norm_u16_f16",
|
|
|
];
|
|
];
|
|
|
let all = [FLOAT1, FLOAT2, FLOAT3, INT];
|
|
let all = [FLOAT1, FLOAT2, FLOAT3, INT];
|
|
|
if !all.iter().any(|l| l.contains(&name))
|
|
if !all.iter().any(|l| l.contains(&name))
|
|
@@ -4393,6 +4395,13 @@ impl<'a> Lower<'a> {
|
|
|
};
|
|
};
|
|
|
return float_result(self, r);
|
|
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.
|
|
// 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 {
|
|
if v.clamp || v.omod != 0 || v.abs != 0 || v.neg != 0 {
|
|
|
return Err("modifiers on a 16-bit integer operation");
|
|
return Err("modifiers on a 16-bit integer operation");
|
|
@@ -4457,6 +4466,78 @@ impl<'a> Lower<'a> {
|
|
|
self.write_half(inst, r, guard)
|
|
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
|
|
/// 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
|
|
/// 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.
|
|
/// half the high result reads. Inline float constants are f16 values.
|