| 1 |
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s -fexperimental-new-constant-interpreter |
| 3 |
// expected-no-diagnostics |
| 4 |
|
| 5 |
#include <limits.h> |
| 6 |
#include <stdint.h> |
| 7 |
|
| 8 |
int a() { |
| 9 |
const int x = 3; |
| 10 |
static int z; |
| 11 |
constexpr int *y = &z; |
| 12 |
return []() { return __builtin_sub_overflow((int)x, (int)x, (int *)y); }(); |
| 13 |
} |
| 14 |
int a2() { |
| 15 |
const int x = 3; |
| 16 |
static int z; |
| 17 |
constexpr int *y = &z; |
| 18 |
return []() { return __builtin_sub_overflow(x, x, y); }(); |
| 19 |
} |
| 20 |
|
| 21 |
template<typename T> |
| 22 |
struct Result { |
| 23 |
bool B; |
| 24 |
T Value; |
| 25 |
constexpr bool operator==(const Result<T> &Other) { |
| 26 |
return B == Other.B && Value == Other.Value; |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
template <typename RET, typename LHS, typename RHS> |
| 32 |
constexpr Result<RET> add(LHS &&lhs, RHS &&rhs) { |
| 33 |
RET sum{}; |
| 34 |
return {__builtin_add_overflow(lhs, rhs, &sum), sum}; |
| 35 |
} |
| 36 |
|
| 37 |
static_assert(add<short>(static_cast<char>(120), static_cast<char>(10)) == Result<short>{false, 130}); |
| 38 |
static_assert(add<char>(static_cast<short>(120), static_cast<short>(10)) == Result<char>{true, -126}); |
| 39 |
static_assert(add<unsigned int>(INT_MAX, INT_MAX) == Result<unsigned int>{false, static_cast<unsigned int>(INT_MAX) * 2u}); |
| 40 |
static_assert(add<int>(static_cast<unsigned int>(INT_MAX), 1u) == Result<int>{true, INT_MIN}); |
| 41 |
|
| 42 |
static_assert(add<int>(17, 22) == Result<int>{false, 39}); |
| 43 |
static_assert(add<int>(INT_MAX - 22, 24) == Result<int>{true, INT_MIN + 1}); |
| 44 |
static_assert(add<int>(INT_MIN + 22, -23) == Result<int>{true, INT_MAX}); |
| 45 |
|
| 46 |
template <typename RET, typename LHS, typename RHS> |
| 47 |
constexpr Result<RET> sub(LHS &&lhs, RHS &&rhs) { |
| 48 |
RET sum{}; |
| 49 |
return {__builtin_sub_overflow(lhs, rhs, &sum), sum}; |
| 50 |
} |
| 51 |
|
| 52 |
static_assert(sub<unsigned char>(static_cast<char>(0),static_cast<char>(1)) == Result<unsigned char>{true, UCHAR_MAX}); |
| 53 |
static_assert(sub<char>(static_cast<unsigned char>(0),static_cast<unsigned char>(1)) == Result<char>{false, -1}); |
| 54 |
static_assert(sub<unsigned short>(static_cast<short>(0),static_cast<short>(1)) == Result<unsigned short>{true, USHRT_MAX}); |
| 55 |
static_assert(sub<uint8_t>(static_cast<uint8_t>(255),static_cast<int>(100)) == Result<uint8_t>{false, 155}); |
| 56 |
|
| 57 |
static_assert(sub<int>(17,22) == Result<int>{false, -5}); |
| 58 |
static_assert(sub<int>(INT_MAX - 22, -23) == Result<int>{true, INT_MIN}); |
| 59 |
static_assert(sub<int>(INT_MIN + 22, 23) == Result<int>{true, INT_MAX}); |
| 60 |
|
| 61 |
template <typename RET, typename LHS, typename RHS> |
| 62 |
constexpr Result<RET> mul(LHS &&lhs, RHS &&rhs) { |
| 63 |
RET sum{}; |
| 64 |
return {__builtin_mul_overflow(lhs, rhs, &sum), sum}; |
| 65 |
} |
| 66 |
|
| 67 |
static_assert(mul<int>(17,22) == Result<int>{false, 374}); |
| 68 |
static_assert(mul<int>(INT_MAX / 22, 23) == Result<int>{true, -2049870757}); |
| 69 |
static_assert(mul<int>(INT_MIN / 22, -23) == Result<int>{true, -2049870757}); |
| 70 |
|
| 71 |
constexpr Result<int> sadd(int lhs, int rhs) { |
| 72 |
int sum{}; |
| 73 |
return {__builtin_sadd_overflow(lhs, rhs, &sum), sum}; |
| 74 |
} |
| 75 |
|
| 76 |
static_assert(sadd(17,22) == Result<int>{false, 39}); |
| 77 |
static_assert(sadd(INT_MAX - 22, 23) == Result<int>{true, INT_MIN}); |
| 78 |
static_assert(sadd(INT_MIN + 22, -23) == Result<int>{true, INT_MAX}); |
| 79 |
|
| 80 |
constexpr Result<int> ssub(int lhs, int rhs) { |
| 81 |
int sum{}; |
| 82 |
return {__builtin_ssub_overflow(lhs, rhs, &sum), sum}; |
| 83 |
} |
| 84 |
|
| 85 |
static_assert(ssub(17,22) == Result<int>{false, -5}); |
| 86 |
static_assert(ssub(INT_MAX - 22, -23) == Result<int>{true, INT_MIN}); |
| 87 |
static_assert(ssub(INT_MIN + 22, 23) == Result<int>{true, INT_MAX}); |
| 88 |
|
| 89 |
constexpr Result<int> smul(int lhs, int rhs) { |
| 90 |
int sum{}; |
| 91 |
return {__builtin_smul_overflow(lhs, rhs, &sum), sum}; |
| 92 |
} |
| 93 |
|
| 94 |
static_assert(smul(17,22) == Result<int>{false, 374}); |
| 95 |
static_assert(smul(INT_MAX / 22, 23) == Result<int>{true, -2049870757}); |
| 96 |
static_assert(smul(INT_MIN / 22, -23) == Result<int>{true, -2049870757}); |
| 97 |
|
| 98 |
template<typename T> |
| 99 |
struct CarryResult { |
| 100 |
T CarryOut; |
| 101 |
T Value; |
| 102 |
constexpr bool operator==(const CarryResult<T> &Other) { |
| 103 |
return CarryOut == Other.CarryOut && Value == Other.Value; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
constexpr CarryResult<unsigned char> addcb(unsigned char lhs, unsigned char rhs, unsigned char carry) { |
| 108 |
unsigned char carry_out{}; |
| 109 |
unsigned char sum{}; |
| 110 |
sum = __builtin_addcb(lhs, rhs, carry, &carry_out); |
| 111 |
return {carry_out, sum}; |
| 112 |
} |
| 113 |
|
| 114 |
static_assert(addcb(120, 10, 0) == CarryResult<unsigned char>{0, 130}); |
| 115 |
static_assert(addcb(250, 10, 0) == CarryResult<unsigned char>{1, 4}); |
| 116 |
static_assert(addcb(255, 255, 0) == CarryResult<unsigned char>{1, 254}); |
| 117 |
static_assert(addcb(255, 255, 1) == CarryResult<unsigned char>{1, 255}); |
| 118 |
static_assert(addcb(255, 0, 1) == CarryResult<unsigned char>{1, 0}); |
| 119 |
static_assert(addcb(255, 1, 0) == CarryResult<unsigned char>{1, 0}); |
| 120 |
static_assert(addcb(255, 1, 1) == CarryResult<unsigned char>{1, 1}); |
| 121 |
// This is currently supported with the carry still producing a value of 1. |
| 122 |
// If support for carry outside of 0-1 is removed, change this test to check |
| 123 |
// that it is not supported. |
| 124 |
static_assert(addcb(255, 255, 2) == CarryResult<unsigned char>{1, 0}); |
| 125 |
|
| 126 |
constexpr CarryResult<unsigned char> subcb(unsigned char lhs, unsigned char rhs, unsigned char carry) { |
| 127 |
unsigned char carry_out{}; |
| 128 |
unsigned char sum{}; |
| 129 |
sum = __builtin_subcb(lhs, rhs, carry, &carry_out); |
| 130 |
return {carry_out, sum}; |
| 131 |
} |
| 132 |
|
| 133 |
static_assert(subcb(20, 10, 0) == CarryResult<unsigned char>{0, 10}); |
| 134 |
static_assert(subcb(10, 10, 0) == CarryResult<unsigned char>{0, 0}); |
| 135 |
static_assert(subcb(10, 15, 0) == CarryResult<unsigned char>{1, 251}); |
| 136 |
// The carry is subtracted from the result |
| 137 |
static_assert(subcb(10, 15, 1) == CarryResult<unsigned char>{1, 250}); |
| 138 |
static_assert(subcb(0, 0, 1) == CarryResult<unsigned char>{1, 255}); |
| 139 |
static_assert(subcb(0, 1, 0) == CarryResult<unsigned char>{1, 255}); |
| 140 |
static_assert(subcb(0, 1, 1) == CarryResult<unsigned char>{1, 254}); |
| 141 |
static_assert(subcb(0, 255, 0) == CarryResult<unsigned char>{1, 1}); |
| 142 |
static_assert(subcb(0, 255, 1) == CarryResult<unsigned char>{1, 0}); |
| 143 |
// This is currently supported with the carry still producing a value of 1. |
| 144 |
// If support for carry outside of 0-1 is removed, change this test to check |
| 145 |
// that it is not supported. |
| 146 |
static_assert(subcb(0, 255, 2) == CarryResult<unsigned char>{1, 255}); |