Skip to content

Fix vector float-to-int and signed integer conversions - #16

Merged
maleadt merged 2 commits into
mainfrom
tb/sat-convert-vector
Oct 9, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/sat-convert-vector

Conversation

@maleadt

@maleadt maleadt commented Oct 9, 2026

Copy link
Copy Markdown
Member

Saturating OpConvertFToS/OpConvertFToU on vectors emitted C that clang rejects. There were three separate problems:

  • The NaN guard used isnan(x) directly as the select condition. For a vector, that mask is as wide as x's elements, not the result's (long4 vs int4 for double4 → uint4). It now goes through relational_mask and a new expression-level select_mask, which is factored out of select_condition.
  • The guard selected an untyped 0, which outranks narrow element types (short4). It now selects the result type's null constant.
  • OpConvertFToS assigned a signed convert_intN result to the unsigned uintN result. Scalars convert implicitly, but OpenCL C has no implicit conversion between vector types, so even non-saturating vector conversions failed. The result is now reinterpreted with as_type, as other signed operations already do.
// before
uint4 v1 = isnan(x) ? 0 : convert_int4_sat_rtz(x);
// after
uint4 v1 = convert_int4(isnan(x)) ? (uint4)(0) : as_uint4(convert_int4_sat_rtz(x));

The second commit fixes the same signedness bug in OpSConvert and OpSatConvertUToS (ulong4 = convert_long4(...), ushort4 = convert_short4_sat(...)).

The emitted expressions give the expected results (NaN → 0, saturation at the type limits, rounding toward zero) on Intel OpenCL, PoCL and NVIDIA.

Fixes #10.

OpConvertFToS produced a signed convert_intN value for an unsigned uintN
result. Scalars convert implicitly, but OpenCL C has no implicit
conversion between vector types, so reinterpret the result with as_type
as other signed operations do.

The NaN guard of saturating conversions spliced isnan(x) in as the
condition of a select, which for a vector is a mask as wide as x's
elements rather than the result's, and selected an untyped 0 that
outranks narrow element types. Bring the mask to the result's width
through relational_mask and a new expression-level select_mask, factored
out of select_condition, and select the result type's null constant.

Fixes #10.
OpSConvert and OpSatConvertUToS convert to the signed spelling of their
result type, but the result is declared with the unsigned spelling.
That converts implicitly for scalars but is an error for vectors, so
reinterpret the result with as_type like OpConvertFToS.
@maleadt
maleadt merged commit a914d8e into main Oct 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saturating float-to-int vector conversions emit a mismatched-width select

1 participant