Skip to content

Commit d4222bf

Browse files
authored
[libc++] Use saturation builtins directly for {add,sub}_sat (#165228)
This doesn't improve performance (except with optimizations disabled), since the compiler is able to fold our current implementation. However, it does significantly reduce the amount of code the compiler has to sift through, reducing compile times a bit.
1 parent c93df83 commit d4222bf

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

libcxx/include/__numeric/saturation_arithmetic.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
3030

3131
template <__signed_or_unsigned_integer _Tp>
3232
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
33+
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
34+
return __builtin_elementwise_add_sat(__x, __y);
35+
# else
3336
if (_Tp __sum; !__builtin_add_overflow(__x, __y, std::addressof(__sum)))
3437
return __sum;
3538
// Handle overflow
@@ -44,10 +47,14 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
4447
// Overflows if (x < 0 && y < 0)
4548
return std::numeric_limits<_Tp>::min();
4649
}
50+
# endif
4751
}
4852

4953
template <__signed_or_unsigned_integer _Tp>
5054
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
55+
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
56+
return __builtin_elementwise_sub_sat(__x, __y);
57+
# else
5158
if (_Tp __sub; !__builtin_sub_overflow(__x, __y, std::addressof(__sub)))
5259
return __sub;
5360
// Handle overflow
@@ -63,6 +70,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
6370
// Overflows if (x < 0 && y > 0)
6471
return std::numeric_limits<_Tp>::min();
6572
}
73+
# endif
6674
}
6775

6876
template <__signed_or_unsigned_integer _Tp>

0 commit comments

Comments
 (0)