Shaders: v_cvt_u16_f16 and v_cvt_i16_f16 saturate at 16 bits

They went through a 32-bit conversion, so an f16 past the i16 range
(32768 up to 65504) wrapped, and infinities and NaN were undefined. The
value is now clamped in f32 to the 16-bit range first (every f16 is exact
there) and NaN gives 0, as the hardware's conversions do.
This commit is contained in:
Luiz Neves committed 2026-11-23 23:49:01 -03:00
1 parent d2a4bae1f4
commit 2af66ff1d8
2 files changed
+53 -6

No files matched your search

+13 -6
View File
@@ -4357,15 +4357,22 @@ impl<'a> Lower<'a> {
}
let r = match name {
"v_cvt_u16_f16" | "v_cvt_i16_f16" => {
// shortcut: converted through 32 bits, so out-of-range values
// wrap instead of saturating at 16 bits.
// Toward zero, saturating at the 16-bit range (infinities
// included); NaN gives 0. Clamped in f32 first, where every
// f16 is exact, so the conversion never leaves i32's range.
let a = self.half_fsrc(inst, 0)?;
let op = if name == "v_cvt_u16_f16" {
Cast::FpToUi
let (lo, hi) = if name == "v_cvt_u16_f16" {
(0.0f32, 65535.0f32)
} else {
Cast::FpToSi
(-32768.0, 32767.0)
};
self.s.m.cast(c, op, a, i32t)
let (lo, hi) = (self.s.m.f32(lo.to_bits()), self.s.m.f32(hi.to_bits()));
let t = self.binary(35, a, lo);
let t = self.binary(36, t, hi);
let r = self.s.m.cast(c, Cast::FpToSi, t, i32t);
let nan = self.s.m.cmp(c, pred::UNO, a, a);
let z = self.s.m.i32(0);
self.s.m.select(c, nan, z, r)
}
_ => {
let (mut a, mut b) = (self.half_src(inst, 0)?, self.half_src(inst, 1)?);