| 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 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 |
| 10 |
// The test uses "Placeholder variables with no name" |
| 11 |
|
| 12 |
// <numeric> |
| 13 |
|
| 14 |
// template<class T> |
| 15 |
// constexpr T mul_sat(T x, T y) noexcept; // freestanding |
| 16 |
|
| 17 |
#include <cassert> |
| 18 |
#include <concepts> |
| 19 |
#include <limits> |
| 20 |
#include <numeric> |
| 21 |
|
| 22 |
#include "test_macros.h" |
| 23 |
|
| 24 |
template <typename IntegerT> |
| 25 |
constexpr bool test_signed() { |
| 26 |
constexpr auto minVal = std::numeric_limits<IntegerT>::min(); |
| 27 |
constexpr auto maxVal = std::numeric_limits<IntegerT>::max(); |
| 28 |
|
| 29 |
// TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" |
| 30 |
[[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::mul_sat(minVal, maxVal); |
| 31 |
|
| 32 |
static_assert(noexcept(std::mul_sat(minVal, maxVal))); |
| 33 |
|
| 34 |
// clang-format off |
| 35 |
|
| 36 |
// Limit values (-1, 0, 1, min, max) |
| 37 |
|
| 38 |
assert(std::mul_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1}); |
| 39 |
assert(std::mul_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{ 0}); |
| 40 |
assert(std::mul_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1}); |
| 41 |
assert(std::mul_sat(IntegerT{-1}, minVal) == maxVal); // saturated |
| 42 |
assert(std::mul_sat(IntegerT{-1}, maxVal) == -maxVal); |
| 43 |
|
| 44 |
assert(std::mul_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0}); |
| 45 |
assert(std::mul_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0}); |
| 46 |
assert(std::mul_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0}); |
| 47 |
assert(std::mul_sat(IntegerT{ 0}, minVal) == IntegerT{ 0}); |
| 48 |
assert(std::mul_sat(IntegerT{ 0}, maxVal) == IntegerT{ 0}); |
| 49 |
|
| 50 |
assert(std::mul_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1}); |
| 51 |
assert(std::mul_sat(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 0}); |
| 52 |
assert(std::mul_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1}); |
| 53 |
assert(std::mul_sat(IntegerT{ 1}, minVal) == minVal); |
| 54 |
assert(std::mul_sat(IntegerT{ 1}, maxVal) == maxVal); |
| 55 |
|
| 56 |
assert(std::mul_sat( minVal, IntegerT{-1}) == maxVal); // saturated |
| 57 |
assert(std::mul_sat( minVal, IntegerT{ 0}) == IntegerT{ 0}); |
| 58 |
assert(std::mul_sat( minVal, IntegerT{ 1}) == minVal); |
| 59 |
assert(std::mul_sat( minVal, minVal) == maxVal); // saturated |
| 60 |
assert(std::mul_sat( minVal, maxVal) == minVal); // saturated |
| 61 |
|
| 62 |
assert(std::mul_sat( maxVal, IntegerT{-1}) == -maxVal); |
| 63 |
assert(std::mul_sat( maxVal, IntegerT{ 0}) == IntegerT{ 0}); |
| 64 |
assert(std::mul_sat( maxVal, IntegerT{ 1}) == maxVal); // saturated |
| 65 |
assert(std::mul_sat( maxVal, minVal) == minVal); // saturated |
| 66 |
assert(std::mul_sat( maxVal, maxVal) == maxVal); // saturated |
| 67 |
|
| 68 |
// No saturation (no limit values) |
| 69 |
|
| 70 |
assert(std::mul_sat(IntegerT{27}, IntegerT{ 2}) == IntegerT{54}); |
| 71 |
assert(std::mul_sat(IntegerT{ 2}, IntegerT{28}) == IntegerT{56}); |
| 72 |
|
| 73 |
// Saturation (no limit values) |
| 74 |
|
| 75 |
{ |
| 76 |
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27}; |
| 77 |
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28}; |
| 78 |
assert(std::mul_sat(x, y) == maxVal); // saturated |
| 79 |
} |
| 80 |
{ |
| 81 |
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27}; |
| 82 |
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28}; |
| 83 |
assert(std::mul_sat(x, y) == minVal); // saturated |
| 84 |
} |
| 85 |
{ |
| 86 |
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27}; |
| 87 |
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28}; |
| 88 |
assert(std::mul_sat(x, y) == minVal); // saturated |
| 89 |
} |
| 90 |
{ |
| 91 |
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27}; |
| 92 |
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28}; |
| 93 |
assert(std::mul_sat(x, y) == maxVal); // saturated |
| 94 |
} |
| 95 |
|
| 96 |
// clang-format on |
| 97 |
|
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
template <typename IntegerT> |
| 102 |
constexpr bool test_unsigned() { |
| 103 |
constexpr auto minVal = std::numeric_limits<IntegerT>::min(); |
| 104 |
constexpr auto maxVal = std::numeric_limits<IntegerT>::max(); |
| 105 |
|
| 106 |
// TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" |
| 107 |
[[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::mul_sat(minVal, maxVal); |
| 108 |
|
| 109 |
static_assert(noexcept(std::mul_sat(minVal, maxVal))); |
| 110 |
|
| 111 |
// clang-format off |
| 112 |
|
| 113 |
// No saturation (0, 1) |
| 114 |
|
| 115 |
assert(std::mul_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0}); |
| 116 |
assert(std::mul_sat(IntegerT{0}, IntegerT{1}) == IntegerT{0}); |
| 117 |
assert(std::mul_sat(IntegerT{0}, minVal) == IntegerT{0}); |
| 118 |
assert(std::mul_sat(IntegerT{0}, maxVal) == IntegerT{0}); |
| 119 |
|
| 120 |
assert(std::mul_sat(IntegerT{1}, IntegerT{0}) == IntegerT{0}); |
| 121 |
assert(std::mul_sat(IntegerT{1}, IntegerT{1}) == IntegerT{1}); |
| 122 |
assert(std::mul_sat(IntegerT{1}, minVal) == minVal); |
| 123 |
assert(std::mul_sat(IntegerT{1}, maxVal) == maxVal); |
| 124 |
|
| 125 |
assert(std::mul_sat( minVal, IntegerT{0}) == IntegerT{0}); |
| 126 |
assert(std::mul_sat( minVal, IntegerT{1}) == minVal); |
| 127 |
assert(std::mul_sat( minVal, maxVal) == minVal); |
| 128 |
assert(std::mul_sat( minVal, maxVal) == minVal); |
| 129 |
|
| 130 |
assert(std::mul_sat( maxVal, IntegerT{0}) == IntegerT{0}); |
| 131 |
assert(std::mul_sat( maxVal, IntegerT{1}) == maxVal); |
| 132 |
assert(std::mul_sat( maxVal, minVal) == IntegerT{0}); |
| 133 |
assert(std::mul_sat( maxVal, maxVal) == maxVal); // saturated |
| 134 |
|
| 135 |
// No saturation (no limit values) |
| 136 |
|
| 137 |
assert(std::mul_sat(IntegerT{28}, IntegerT{2}) == IntegerT{56}); |
| 138 |
|
| 139 |
// Saturation (no limit values |
| 140 |
|
| 141 |
{ |
| 142 |
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27}; |
| 143 |
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28}; |
| 144 |
assert(std::mul_sat(x, y) == maxVal); // saturated |
| 145 |
} |
| 146 |
|
| 147 |
// clang-format on |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
constexpr bool test() { |
| 153 |
// Signed |
| 154 |
test_signed<signed char>(); |
| 155 |
test_signed<short int>(); |
| 156 |
test_signed<int>(); |
| 157 |
test_signed<long int>(); |
| 158 |
test_signed<long long int>(); |
| 159 |
#ifndef TEST_HAS_NO_INT128 |
| 160 |
test_signed<__int128_t>(); |
| 161 |
#endif |
| 162 |
// Unsigned |
| 163 |
test_unsigned<unsigned char>(); |
| 164 |
test_unsigned<unsigned short int>(); |
| 165 |
test_unsigned<unsigned int>(); |
| 166 |
test_unsigned<unsigned long int>(); |
| 167 |
test_unsigned<unsigned long long int>(); |
| 168 |
#ifndef TEST_HAS_NO_INT128 |
| 169 |
test_unsigned<__uint128_t>(); |
| 170 |
#endif |
| 171 |
|
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
int main(int, char**) { |
| 176 |
test(); |
| 177 |
static_assert(test()); |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |