[clang:frontend] Add `__builtin_elementwise_saturating_cast` intrinsic
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
https://github.com/llvm/llvm-project/blob/f6f14162f59d360509ad79ae96336223b724f44d/libcxx/include/__numeric/saturation_arithmetic.h#L105-L117
Currently, we just have this software implementation, but future developments will necessitate an intrinsic:
- [P2956R3: Allow `std::simd` overloads for saturating operations](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p2956r3.html) wants a SIMD version of the feature, and I'm not sure how easily the current implementation could be turned into `vpmovsdw` on x86.
- [P4355R0: Floating-point `std::saturating_cast`](https://isocpp.org/files/papers/P4355R0.html) (not yet mailed) extends `std::saturating_cast` with the ability to cast to `float`, which is exactly the existing behavior of [llvm.fptoui.sat.*](https://llvm.org/docs/LangRef.html#llvm-fptoui-sat-intrinsic), except that it doesn't require the [-fno-strict-float-cast-overflow](https://clang.llvm.org/docs/UsersManual.html#cmdoption-f-no-strict-float-cast-overflow) flag to be specified. Irrespective of whether the proposal makes it in, it would be nice to have a way to emit `fptoui.sat` without needing to set a global flag.
Contributor guide
Research direction
Start with libcxx/include/__numeric/saturation_arithmetic.h:105-117 and the LLVM LangRef entry for llvm.fptoui.sat. Review the requested Clang frontend intrinsic against the SIMD and floating-point use cases described in P2956R3 and P4355R0. Done means the intrinsic provides the requested saturating-cast behavior without requiring the global flag.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100