Translator tests: a half-float or RG11B10Float texel held as words converts only values in range

A texture buffer of a format a device holds as words (an R32_UINT view whose texels shaders
convert) is written by lower/texture/words.rs's write_texel, whose half-float and RG11B10Float
codes come from small_float_bits. That function computes every lane's denormal code, the lane's
value times 2^(14 + m) converted by OpConvertFToU, before a select keeps it only for a denormal
lane. A normal lane of 2^(18 - m) or more (256 for a half float, 4096 for an 11-bit float, 8192
for a 10-bit one), and every infinity or NaN, reaches that conversion past u32, and SPIR-V leaves
OpConvertFToU of a value its result type cannot hold undefined, whatever a device returns for it.

This adds a no-device test and the hook it needs (texture::written_words, a write_texel of a
format held as words, under cfg(test) alone): lower/arith/tests/color_write.rs
half_and_small_float_words_convert_only_values_in_range writes RG16Float and RG11B10Float texels
of normal, past-largest, infinite and denormal lanes on the interpreter, which traps an
out-of-range conversion, and checks each texel's words against the format's codes.

At this commit the test fails: the interpreter traps ConversionOutOfRange on RG16Float's first
texel (256, -65504).
This commit is contained in:
Anees Iqbal committed 2026-12-03 05:55:05 -08:00
1 parent 7e8d556729
commit 5458510f15
3 files changed
+77

No files matched your search

@@ -674,3 +674,64 @@ fn rgb9e5_words_are_metals_at_either_mode() {
}
}
}
/// A half-float or `RG11B10Float` texel held as words (`texture::written_words`) converts only
/// values in range. A lane's denormal code is its value times `2^(14 + m)` converted to an integer
/// (`words::small_float_bits`), which a normal lane of `2^(18 - m)` or more (256 for a half float,
/// 4096 for an 11-bit float, 8192 for a 10-bit one) takes past `u32` before the select drops it,
/// and `OpConvertFToU` of a value its result type cannot hold is undefined. On the interpreter,
/// which traps such a conversion, the words of normal, past-largest, infinite and denormal lanes
/// are the format's codes: a half float's bits from bit 0 and 16, `RG11B10Float`'s 11-bit floats
/// from bit 0 and 11 and its 10-bit float from bit 22.
#[test]
fn half_and_small_float_words_convert_only_values_in_range() {
use crate::metal::PixelFormat;
let f4 = vec(float(32), 4);
let out = vec(int(32), 2);
let two = |e: i32| 2f32.powi(e);
// Red and green of RG16Float: 256 and -65504; past the largest, rounded toward zero to the
// largest; the infinities; the denormals 16 * 2^-24 and -2^-24.
let half: &[([f32; 4], u32)] = &[
([256.0, -65504.0, 0.0, 0.0], 0xfbff_5c00),
([70000.0, -1.0e9, 0.0, 0.0], 0xfbff_7bff),
([f32::INFINITY, f32::NEG_INFINITY, 0.0, 0.0], 0xfc00_7c00),
([two(-20), -two(-24), 0.0, 0.0], 0x8001_0010),
];
// RG11B10Float: 4096 and each channel's largest (0x6c0, 0x7bf, 0x3df); past the largest, and
// the denormals 32 * 2^-20 and 8 * 2^-19 (0x7bf, 0x020, 0x008).
let small: &[([f32; 4], u32)] = &[
([4096.0, 65024.0, 64512.0, 0.0], 0xf7fd_fec0),
([1.0e6, two(-15), two(-16), 0.0], 0x0201_07bf),
];
for (pf, texels) in [
(PixelFormat::RG16Float, half),
(PixelFormat::RG11B10Float, small),
] {
let what = format!("{pf:?}");
let m = kernel_with(
&[f4],
out,
&|fb, x| {
let (lo, hi) = crate::lower::texture::written_words(fb, pf, false, x[0])?;
let t = crate::lower::arith::ops::ty(fb, out)?;
fb.composite_construct(t, &[lo, hi])
},
true,
false,
)
.unwrap_or_else(|e| panic!("{what}: build: {e:?}"));
let cases: Vec<Case> = texels
.iter()
.map(|(t, _)| vec![t.iter().map(|x| u64::from(x.to_bits())).collect()])
.collect();
let words = run_module(&what, m, &[f4], out, &cases);
for ((t, want), w) in texels.iter().zip(&words) {
assert_eq!(
(w[0] as u32, w[1] as u32),
(*want, 0),
"{what} {t:?}: {:#010x}",
w[0] as u32
);
}
}
}
@@ -188,6 +188,8 @@ pub(crate) use emit::{
pub use emit::{emit, held_texel, to_image};
pub(crate) use slot::depth_comparison;
pub use status::StatusUse;
#[cfg(test)]
pub(crate) use words::written_words;
pub(crate) use words::{gate as words_gate, HANDLE_WORD, HANDLE_WORD_SHIFT};
use crate::lower::access::handle::HandleTarget;
@@ -1038,6 +1038,20 @@ fn read_texel(
}
}
/// For the tests: the words `(lo, hi)` a write of `v` (a `vec4` of `f32`) stores in a texel of
/// `pf`, a format held as words ([`write_texel`]), at `textureWriteRoundingMode` 2 where
/// `half_up`.
#[cfg(test)]
pub(crate) fn written_words(
fb: &mut FunctionBuilder<'_>,
pf: crate::metal::PixelFormat,
half_up: bool,
v: SValue,
) -> BResult<(SValue, SValue)> {
let layout = texel_words(pf).expect("a format held as words").layout;
write_texel(fb, (layout, half_up), v)
}
/// The words `(lo, hi)` of the texel of `layout` that a write of `v` (a `vec4` of `f32` for the
/// float class, of `u32` for an integer one) stores, a narrow texel's bits at the bottom of `lo`;
/// an `RGB9E5Float` word's mantissas rounded half up where `half_up` (`emit::rgb9e5_word`).