Skip to content

[clang:frontend] Add __builtin_elementwise_saturating_cast intrinsic #221132

Description

@eisenwave

template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturating_cast(_Tp __x) noexcept {
// Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
// optimized out by the compiler.
// Handle overflow
if (std::cmp_less(__x, std::numeric_limits<_Rp>::min()))
return std::numeric_limits<_Rp>::min();
if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max()))
return std::numeric_limits<_Rp>::max();
// No overflow
return static_cast<_Rp>(__x);
}

Currently, we just have this software implementation, but future developments will necessitate an intrinsic:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions