| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // REQUIRES: std-at-least-c++26 |
| 10 | |
| 11 | // The test uses "Placeholder variables with no name" |
| 12 | // UNSUPPORTED: apple-clang-15, apple-clang-16 |
| 13 | |
| 14 | // <numeric> |
| 15 | |
| 16 | // template<class T> |
| 17 | // constexpr T sub_sat(T x, T y) noexcept; // freestanding |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <concepts> |
| 21 | #include <limits> |
| 22 | #include <numeric> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | template <typename IntegerT> |
| 27 | constexpr bool test_signed() { |
| 28 | constexpr auto minVal = std::numeric_limits<IntegerT>::min(); |
| 29 | constexpr auto maxVal = std::numeric_limits<IntegerT>::max(); |
| 30 | |
| 31 | std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal); |
| 32 | |
| 33 | static_assert(noexcept(std::sub_sat(minVal, maxVal))); |
| 34 | |
| 35 | // clang-format off |
| 36 | |
| 37 | // Limit values (-1, 0, 1, min, max) |
| 38 | |
| 39 | assert(std::sub_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 0}); |
| 40 | assert(std::sub_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1}); |
| 41 | assert(std::sub_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-2}); |
| 42 | assert(std::sub_sat(IntegerT{-1}, minVal) == IntegerT{-1} - minVal); |
| 43 | assert(std::sub_sat(IntegerT{-1}, maxVal) == IntegerT{-1} - maxVal); |
| 44 | |
| 45 | assert(std::sub_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 1}); |
| 46 | assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0}); |
| 47 | assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{-1}); |
| 48 | assert(std::sub_sat(IntegerT{ 0}, minVal) == maxVal); // saturated |
| 49 | assert(std::sub_sat(IntegerT{ 0}, maxVal) == -maxVal); |
| 50 | |
| 51 | assert(std::sub_sat( minVal, IntegerT{-1}) == minVal - IntegerT{-1}); |
| 52 | assert(std::sub_sat( minVal, IntegerT{ 0}) == minVal); |
| 53 | assert(std::sub_sat( minVal, IntegerT{ 1}) == minVal); // saturated |
| 54 | assert(std::sub_sat( minVal, minVal) == IntegerT{0}); |
| 55 | assert(std::sub_sat( minVal, maxVal) == minVal); // saturated |
| 56 | |
| 57 | assert(std::sub_sat( maxVal, IntegerT{-1}) == maxVal); // saturated |
| 58 | assert(std::sub_sat( maxVal, IntegerT{ 0}) == maxVal); |
| 59 | assert(std::sub_sat( maxVal, IntegerT{ 1}) == maxVal - IntegerT{ 1}); |
| 60 | assert(std::sub_sat( maxVal, minVal) == maxVal); // saturated |
| 61 | assert(std::sub_sat( maxVal, maxVal) == IntegerT{0}); |
| 62 | |
| 63 | // No saturation (no limit values) |
| 64 | |
| 65 | assert(std::sub_sat(IntegerT{ 27}, IntegerT{-28}) == 55); |
| 66 | assert(std::sub_sat(IntegerT{ 27}, IntegerT{ 28}) == -1); |
| 67 | assert(std::sub_sat(IntegerT{-27}, IntegerT{ 28}) == -55); |
| 68 | assert(std::sub_sat(IntegerT{-27}, IntegerT{-28}) == 1); |
| 69 | |
| 70 | // Saturation (no limit values) |
| 71 | |
| 72 | { |
| 73 | constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27}; |
| 74 | constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28}; |
| 75 | assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated |
| 76 | } |
| 77 | { |
| 78 | constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28}; |
| 79 | constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27}; |
| 80 | assert(std::sub_sat(biggerVal, lesserVal) == maxVal); // saturated |
| 81 | } |
| 82 | |
| 83 | // clang-format on |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | template <typename IntegerT> |
| 89 | constexpr bool test_unsigned() { |
| 90 | constexpr auto minVal = std::numeric_limits<IntegerT>::min(); |
| 91 | constexpr auto maxVal = std::numeric_limits<IntegerT>::max(); |
| 92 | |
| 93 | std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal); |
| 94 | |
| 95 | static_assert(noexcept(std::sub_sat(minVal, maxVal))); |
| 96 | |
| 97 | // clang-format off |
| 98 | |
| 99 | // Limit values (0, 1, min, max) |
| 100 | |
| 101 | assert(std::sub_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0}); |
| 102 | assert(std::sub_sat(IntegerT{0}, IntegerT{1}) == minVal); // saturated |
| 103 | assert(std::sub_sat(IntegerT{0}, minVal) == minVal); |
| 104 | assert(std::sub_sat(IntegerT{0}, maxVal) == minVal); // saturated |
| 105 | |
| 106 | assert(std::sub_sat(IntegerT{1}, IntegerT{0}) == IntegerT{1}); |
| 107 | assert(std::sub_sat(IntegerT{1}, IntegerT{1}) == IntegerT{0}); |
| 108 | assert(std::sub_sat(IntegerT{1}, minVal) == IntegerT{1}); |
| 109 | assert(std::sub_sat(IntegerT{1}, maxVal) == minVal); // saturated |
| 110 | |
| 111 | assert(std::sub_sat( minVal, IntegerT{0}) == IntegerT{0}); |
| 112 | assert(std::sub_sat( minVal, IntegerT{1}) == minVal); |
| 113 | assert(std::sub_sat( minVal, maxVal) == minVal); |
| 114 | assert(std::sub_sat( minVal, maxVal) == minVal); |
| 115 | |
| 116 | assert(std::sub_sat( maxVal, IntegerT{0}) == maxVal); |
| 117 | assert(std::sub_sat( maxVal, IntegerT{1}) == maxVal - IntegerT{1}); |
| 118 | assert(std::sub_sat( maxVal, minVal) == maxVal); |
| 119 | assert(std::sub_sat( maxVal, maxVal) == IntegerT{0}); |
| 120 | |
| 121 | // Saturation (no limit values) |
| 122 | |
| 123 | { |
| 124 | constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27}; |
| 125 | constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28}; |
| 126 | assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated |
| 127 | } |
| 128 | |
| 129 | // clang-format on |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | constexpr bool test() { |
| 135 | // Signed |
| 136 | test_signed<signed char>(); |
| 137 | test_signed<short int>(); |
| 138 | test_signed<int>(); |
| 139 | test_signed<long int>(); |
| 140 | test_signed<long long int>(); |
| 141 | #ifndef TEST_HAS_NO_INT128 |
| 142 | test_signed<__int128_t>(); |
| 143 | #endif |
| 144 | // Unsigned |
| 145 | test_unsigned<unsigned char>(); |
| 146 | test_unsigned<unsigned short int>(); |
| 147 | test_unsigned<unsigned int>(); |
| 148 | test_unsigned<unsigned long int>(); |
| 149 | test_unsigned<unsigned long long int>(); |
| 150 | #ifndef TEST_HAS_NO_INT128 |
| 151 | test_unsigned<__uint128_t>(); |
| 152 | #endif |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | int main(int, char**) { |
| 158 | test(); |
| 159 | static_assert(test()); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |