// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s // RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s -fexperimental-new-constant-interpreter // expected-no-diagnostics #include #include int a() { const int x = 3; static int z; constexpr int *y = &z; return []() { return __builtin_sub_overflow((int)x, (int)x, (int *)y); }(); } int a2() { const int x = 3; static int z; constexpr int *y = &z; return []() { return __builtin_sub_overflow(x, x, y); }(); } template struct Result { bool B; T Value; constexpr bool operator==(const Result &Other) { return B == Other.B && Value == Other.Value; } }; template constexpr Result add(LHS &&lhs, RHS &&rhs) { RET sum{}; return {__builtin_add_overflow(lhs, rhs, &sum), sum}; } static_assert(add(static_cast(120), static_cast(10)) == Result{false, 130}); static_assert(add(static_cast(120), static_cast(10)) == Result{true, -126}); static_assert(add(INT_MAX, INT_MAX) == Result{false, static_cast(INT_MAX) * 2u}); static_assert(add(static_cast(INT_MAX), 1u) == Result{true, INT_MIN}); static_assert(add(17, 22) == Result{false, 39}); static_assert(add(INT_MAX - 22, 24) == Result{true, INT_MIN + 1}); static_assert(add(INT_MIN + 22, -23) == Result{true, INT_MAX}); template constexpr Result sub(LHS &&lhs, RHS &&rhs) { RET sum{}; return {__builtin_sub_overflow(lhs, rhs, &sum), sum}; } static_assert(sub(static_cast(0),static_cast(1)) == Result{true, UCHAR_MAX}); static_assert(sub(static_cast(0),static_cast(1)) == Result{false, -1}); static_assert(sub(static_cast(0),static_cast(1)) == Result{true, USHRT_MAX}); static_assert(sub(static_cast(255),static_cast(100)) == Result{false, 155}); static_assert(sub(17,22) == Result{false, -5}); static_assert(sub(INT_MAX - 22, -23) == Result{true, INT_MIN}); static_assert(sub(INT_MIN + 22, 23) == Result{true, INT_MAX}); template constexpr Result mul(LHS &&lhs, RHS &&rhs) { RET sum{}; return {__builtin_mul_overflow(lhs, rhs, &sum), sum}; } static_assert(mul(17,22) == Result{false, 374}); static_assert(mul(INT_MAX / 22, 23) == Result{true, -2049870757}); static_assert(mul(INT_MIN / 22, -23) == Result{true, -2049870757}); constexpr Result sadd(int lhs, int rhs) { int sum{}; return {__builtin_sadd_overflow(lhs, rhs, &sum), sum}; } static_assert(sadd(17,22) == Result{false, 39}); static_assert(sadd(INT_MAX - 22, 23) == Result{true, INT_MIN}); static_assert(sadd(INT_MIN + 22, -23) == Result{true, INT_MAX}); constexpr Result ssub(int lhs, int rhs) { int sum{}; return {__builtin_ssub_overflow(lhs, rhs, &sum), sum}; } static_assert(ssub(17,22) == Result{false, -5}); static_assert(ssub(INT_MAX - 22, -23) == Result{true, INT_MIN}); static_assert(ssub(INT_MIN + 22, 23) == Result{true, INT_MAX}); constexpr Result smul(int lhs, int rhs) { int sum{}; return {__builtin_smul_overflow(lhs, rhs, &sum), sum}; } static_assert(smul(17,22) == Result{false, 374}); static_assert(smul(INT_MAX / 22, 23) == Result{true, -2049870757}); static_assert(smul(INT_MIN / 22, -23) == Result{true, -2049870757}); template struct CarryResult { T CarryOut; T Value; constexpr bool operator==(const CarryResult &Other) { return CarryOut == Other.CarryOut && Value == Other.Value; } }; constexpr CarryResult addcb(unsigned char lhs, unsigned char rhs, unsigned char carry) { unsigned char carry_out{}; unsigned char sum{}; sum = __builtin_addcb(lhs, rhs, carry, &carry_out); return {carry_out, sum}; } static_assert(addcb(120, 10, 0) == CarryResult{0, 130}); static_assert(addcb(250, 10, 0) == CarryResult{1, 4}); static_assert(addcb(255, 255, 0) == CarryResult{1, 254}); static_assert(addcb(255, 255, 1) == CarryResult{1, 255}); static_assert(addcb(255, 0, 1) == CarryResult{1, 0}); static_assert(addcb(255, 1, 0) == CarryResult{1, 0}); static_assert(addcb(255, 1, 1) == CarryResult{1, 1}); // This is currently supported with the carry still producing a value of 1. // If support for carry outside of 0-1 is removed, change this test to check // that it is not supported. static_assert(addcb(255, 255, 2) == CarryResult{1, 0}); constexpr CarryResult subcb(unsigned char lhs, unsigned char rhs, unsigned char carry) { unsigned char carry_out{}; unsigned char sum{}; sum = __builtin_subcb(lhs, rhs, carry, &carry_out); return {carry_out, sum}; } static_assert(subcb(20, 10, 0) == CarryResult{0, 10}); static_assert(subcb(10, 10, 0) == CarryResult{0, 0}); static_assert(subcb(10, 15, 0) == CarryResult{1, 251}); // The carry is subtracted from the result static_assert(subcb(10, 15, 1) == CarryResult{1, 250}); static_assert(subcb(0, 0, 1) == CarryResult{1, 255}); static_assert(subcb(0, 1, 0) == CarryResult{1, 255}); static_assert(subcb(0, 1, 1) == CarryResult{1, 254}); static_assert(subcb(0, 255, 0) == CarryResult{1, 1}); static_assert(subcb(0, 255, 1) == CarryResult{1, 0}); // This is currently supported with the carry still producing a value of 1. // If support for carry outside of 0-1 is removed, change this test to check // that it is not supported. static_assert(subcb(0, 255, 2) == CarryResult{1, 255});