-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++][numeric] P0543R3: Saturation arithmetic #77967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zingam
merged 71 commits into
llvm:main
from
H-G-Hristov:hgh/libcxx/P0543R3-Saturation-arithmetic
Jan 22, 2024
Merged
Changes from all commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
48c4463
[libc++][numeric] P0543R3: Saturation arithmetic
Zingam 2c939cc
Cleanup
Zingam f5db95e
Fixed "code formatter" error
Zingam db3e72e
Cleanup
Zingam 880ca3a
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam 30a1593
WIP: prepare to address comments
Zingam 2793acb
WIP: Cleaned up tests
Zingam 540cfec
WIP: Test `constexpr`
Zingam 83fff64
WIP: Optimized `saturate_cast`
Zingam 2cab21a
Renamed test
Zingam 68cbc9f
WIP: `add_sat` - edge case tests
Zingam e284255
WIP: Added extended integers types to tests
Zingam fb43978
WIP: Added `check_constexpr.h`
Zingam 39837d6
WIP: Fixed code formatter error
Zingam e8e21ce
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
H-G-Hristov 3fbec01
WIP: Addtional tests `sat_sub`
Zingam ea68a59
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam a77d603
WIP: `add_sat` - additional test cases
Zingam faf1173
Completed SFINAE tests
Zingam b66f034
WIP: Temporary static tests
Zingam 248531a
WIP: Cleanup
Zingam 14fb0cf
Updated tests: `add_sat`
Zingam 179ebaa
Updated tests: `div_sat`
Zingam 191970d
Cleanup
Zingam 6b661d6
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam b24b4a6
Updated tests: `mul_sat`
Zingam e108b3f
Fix CI
Zingam 1517f67
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam 3b8f712
Fix CI
Zingam 9bf023c
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam 2289b24
Move release notes entry to avoid merge conflict.
Zingam ed4e571
Addressed some comments - `TEST_HAS_NO_INT128`
Zingam eb6b908
Updated `add_sat.pass`
Zingam 20de72c
Updated tests `div_sat.pass`
Zingam 3dfb7a0
Updated test `mul_sat.pass`
Zingam a6435a9
Updated test `sub_sat.pass`
Zingam 6312da8
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam 80483e7
Updated test `saturate_cast.pass`
Zingam 1c2316b
Updated test `saturate_cast.pass`
Zingam fdff569
Updated test: `saturate_cast.pass`
Zingam 38f4157
Updated test `saturation_cast.pass`
Zingam 2f76f02
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam 453e681
Minor tweak
Zingam 8261422
More tweaks
Zingam e930967
More tweaks
Zingam d97311b
More tweaks
Zingam ddfd2fc
More tweaks
Zingam deb78c6
Unsupport Clang-17
Zingam b6c2924
Fixed formatting
Zingam 34bd4ba
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
H-G-Hristov cdd5992
Added `"test_macros.h` to fix 32-bit CI
Zingam 9487d18
Improved implementation of `mul_sat`
Zingam 8a67964
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam bcf6e7e
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam e2eaf0c
Reformatted `saturate_cast`
Zingam a79ca6c
Updated test `add_sat`
Zingam 82ef906
Updated test `div_sat`
Zingam 5d1af8c
Updated test `sub_sat`
Zingam fc15282
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
H-G-Hristov efb7355
Minor tweaks to comments
Zingam 75c08b7
Re-introduced `[[maybe_unused]]` and re-enabled the tests
Zingam 8ce388c
Updated `saturate_cast.pass`
Zingam 51166ff
Cleanup
Zingam 43a9b27
Final update and cleanup
Zingam eaa0168
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
Zingam b06e016
DEBUG
Zingam 71a10a0
Debug 32-bit
Zingam 71c1cb5
Try again
Zingam 0066189
Try to fix CI
Zingam 618d6e4
Merge branch 'main' into hgh/libcxx/P0543R3-Saturation-arithmetic
H-G-Hristov fc9af61
Update libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_…
H-G-Hristov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// -*- C++ -*- | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H | ||
#define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H | ||
|
||
#include <__concepts/arithmetic.h> | ||
#include <__config> | ||
#include <__utility/cmp.h> | ||
#include <limits> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
#if _LIBCPP_STD_VER >= 26 | ||
|
||
template <__libcpp_integer _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept { | ||
if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum)) | ||
return __sum; | ||
// Handle overflow | ||
if constexpr (__libcpp_unsigned_integer<_Tp>) { | ||
return std::numeric_limits<_Tp>::max(); | ||
} else { | ||
// Signed addition overflow | ||
if (__x > 0) | ||
// Overflows if (x > 0 && y > 0) | ||
return std::numeric_limits<_Tp>::max(); | ||
else | ||
// Overflows if (x < 0 && y < 0) | ||
return std::numeric_limits<_Tp>::min(); | ||
} | ||
} | ||
|
||
template <__libcpp_integer _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept { | ||
if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub)) | ||
return __sub; | ||
// Handle overflow | ||
if constexpr (__libcpp_unsigned_integer<_Tp>) { | ||
// Overflows if (x < y) | ||
return std::numeric_limits<_Tp>::min(); | ||
} else { | ||
// Signed subtration overflow | ||
if (__x >= 0) | ||
// Overflows if (x >= 0 && y < 0) | ||
return std::numeric_limits<_Tp>::max(); | ||
else | ||
// Overflows if (x < 0 && y > 0) | ||
return std::numeric_limits<_Tp>::min(); | ||
} | ||
} | ||
|
||
template <__libcpp_integer _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept { | ||
if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul)) | ||
return __mul; | ||
// Handle overflow | ||
if constexpr (__libcpp_unsigned_integer<_Tp>) { | ||
return std::numeric_limits<_Tp>::max(); | ||
} else { | ||
// Signed multiplication overflow | ||
if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0)) | ||
return std::numeric_limits<_Tp>::max(); | ||
// Overflows if (x < 0 && y > 0) || (x > 0 && y < 0) | ||
return std::numeric_limits<_Tp>::min(); | ||
} | ||
} | ||
|
||
template <__libcpp_integer _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined"); | ||
if constexpr (__libcpp_unsigned_integer<_Tp>) { | ||
return __x / __y; | ||
} else { | ||
// Handle signed division overflow | ||
if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1}) | ||
return std::numeric_limits<_Tp>::max(); | ||
return __x / __y; | ||
} | ||
} | ||
|
||
template <__libcpp_integer _Rp, __libcpp_integer _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept { | ||
H-G-Hristov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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); | ||
} | ||
|
||
#endif // _LIBCPP_STD_VER >= 26 | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 | ||
|
||
// <numeric> | ||
|
||
// template<class T> | ||
// constexpr T add_sat(T x, T y) noexcept; // freestanding | ||
|
||
#include <concepts> | ||
#include <numeric> | ||
|
||
#include "test_macros.h" | ||
|
||
template <typename T, typename U> | ||
concept CanDo = requires(T x, U y) { | ||
{ std::add_sat(x, y) } -> std::same_as<T>; | ||
}; | ||
|
||
template <typename T, typename U> | ||
constexpr void test_constraint_success() { | ||
static_assert(CanDo<T, T>); | ||
static_assert(!CanDo<U, T>); | ||
static_assert(!CanDo<T, U>); | ||
} | ||
|
||
template <typename T> | ||
constexpr void test_constraint_fail() { | ||
using I = int; | ||
static_assert(!CanDo<T, T>); | ||
static_assert(!CanDo<I, T>); | ||
static_assert(!CanDo<T, I>); | ||
} | ||
|
||
constexpr void test() { | ||
// Contraint success - Signed | ||
using SI = long long int; | ||
test_constraint_success<signed char, SI>(); | ||
test_constraint_success<short int, SI>(); | ||
test_constraint_success<signed char, SI>(); | ||
test_constraint_success<short int, SI>(); | ||
test_constraint_success<int, SI>(); | ||
test_constraint_success<long int, SI>(); | ||
test_constraint_success<long long int, int>(); | ||
#ifndef TEST_HAS_NO_INT128 | ||
test_constraint_success<__int128_t, SI>(); | ||
#endif | ||
// Contraint success - Unsigned | ||
using UI = unsigned long long int; | ||
test_constraint_success<unsigned char, UI>(); | ||
test_constraint_success<unsigned short int, UI>(); | ||
test_constraint_success<unsigned int, UI>(); | ||
test_constraint_success<unsigned long int, UI>(); | ||
test_constraint_success<unsigned long long int, unsigned int>(); | ||
#ifndef TEST_HAS_NO_INT128 | ||
test_constraint_success<__uint128_t, UI>(); | ||
#endif | ||
|
||
// Contraint failure | ||
test_constraint_fail<bool>(); | ||
test_constraint_fail<char>(); | ||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
test_constraint_fail<wchar_t>(); | ||
#endif | ||
test_constraint_fail<char8_t>(); | ||
test_constraint_fail<char16_t>(); | ||
test_constraint_fail<char32_t>(); | ||
test_constraint_fail<float>(); | ||
test_constraint_fail<double>(); | ||
test_constraint_fail<long double>(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.