wgpu/naga/tests/in/wgsl/conversion-float-to-int-no-f64.toml
Jamie Nicol 479173197e [naga hlsl-out msl-out spv-out] Avoid undefined behaviour when casting floats to integers
Currently we generate code to convert floating point values to integers
using constructor-style casts in HLSL, static_cast in MSL, and
OpConvertFToS/OpConvertFToU instructions in SPV. Unfortunately the
behaviour of these operations is undefined when the original value is
outside of the range of the target type.

This patch avoids undefined behaviour by first clamping the value to
be inside the target type's range, then performing the cast.
Additionally, we specifically clamp to the minimum and maximum values
that are exactly representable in both the original and the target
type, as per the WGSL spec[1]. Note that these may not be the same as
the minimum and maximum values of the target type.

We additionally must ensure we clamp in the same manner for
conversions during const evaluation. Lastly, although not part of the
WGSL spec, we do the same for casting from F64 and/or to I64 or U64.

[1] https://www.w3.org/TR/WGSL/#floating-point-conversion
2025-03-27 15:41:27 +00:00

5 lines
130 B
TOML

# Other backends are tested by conversion-float-to-int.wgsl which is a
# superset of this test.
targets = "METAL"
god_mode = true