Skip to content

Commit

Permalink
Rework convert_sat builtins to handle infinity values (recommit)
Browse files Browse the repository at this point in the history
This is a plain recommit of 7930522.

The previous implementation of saturated conversion functions was based on the
assumption that if the destination integer type limits included the normal
number limits of the source FP type (e.g. `half` to `ulong` conversion), simple
checks for NaN values would suffice. Meanwhile, positive/negative infinity
values of any FP type would break this assumption, and we would run into
undefined behaviour as if performing a non-saturated conversion.

Additionally, saturation logic was duplicated for many FP/integer type pairs,
some of the implementations contradicting each other.

Unify all of the type-specific implementations through common helpers that
handle out-of-bounds clamping/NaN saturation logic. Account for INF values by
clamping to the destination type's min/max value depending on the INF sign (1).
Additionally, remove the old i64 emulation workaround which is no longer needed
with the proper source-level limit checks.

This commit is a rework of 2b1593e: compared to the original approach, the
infinity checks are restrained to int <-> fp type pairs that satisfy:
```
intty_min < fpty_normal_min & fpty_normalmax < intty_max
```
Naturally, with unsigned types, this logic only involves the upper limit checks.
Compared to the first attempt, test coverage is also improved.

(1) Per OpenCL C 3.0 spec 6.4.3.3, "Out-of-Range Behavior and Saturated
Conversions",
> When in saturated mode, values that are outside the representable range shall
clamp to the nearest representable value in the destination format.

https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_C.html#out-of-range-behavior
  • Loading branch information
AGindinson authored and igcbot committed Jun 20, 2023
1 parent 097633c commit cb90788
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 353 deletions.
Loading

0 comments on commit cb90788

Please sign in to comment.