|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -std=c++17 -ffreestanding -verify %s |
| 2 | +// expected-no-diagnostics |
| 3 | + |
| 4 | +#include <limits.h> |
| 5 | + |
| 6 | +template<typename T> |
| 7 | +struct Result { |
| 8 | + T value; |
| 9 | + T carry; |
| 10 | + constexpr bool operator==(const Result<T> &Other) { |
| 11 | + return value == Other.value && carry == Other.carry; |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +template<typename T> |
| 16 | +constexpr Result<T> add(T Lhs, T Rhs, T Carryin) |
| 17 | +{ |
| 18 | + T Carryout = 0; |
| 19 | + if constexpr(__is_same(T, unsigned char)) |
| 20 | + return { __builtin_addcb(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 21 | + else if constexpr(__is_same(T, unsigned short)) |
| 22 | + return { __builtin_addcs(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 23 | + else if constexpr(__is_same(T, unsigned int)) |
| 24 | + return { __builtin_addc(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 25 | + else if constexpr(__is_same(T, unsigned long)) |
| 26 | + return { __builtin_addcl(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 27 | + else if constexpr(__is_same(T, unsigned long long)) |
| 28 | + return { __builtin_addcll(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 29 | +} |
| 30 | + |
| 31 | +static_assert(add<unsigned char>(0, 0, 0) == Result<unsigned char>{0, 0}); |
| 32 | +static_assert(add<unsigned char>(0, 0, 1) == Result<unsigned char>{1, 0}); |
| 33 | +static_assert(add<unsigned char>(UCHAR_MAX - 1, 1, 1) == Result<unsigned char>{0, 1}); |
| 34 | +static_assert(add<unsigned char>(UCHAR_MAX, 1, 0) == Result<unsigned char>{0, 1}); |
| 35 | +static_assert(add<unsigned char>(UCHAR_MAX, 1, 1) == Result<unsigned char>{1, 1}); |
| 36 | + |
| 37 | +static_assert(add<unsigned short>(0, 0, 0) == Result<unsigned short>{0, 0}); |
| 38 | +static_assert(add<unsigned short>(0, 0, 1) == Result<unsigned short>{1, 0}); |
| 39 | +static_assert(add<unsigned short>(USHRT_MAX - 1, 1, 1) == Result<unsigned short>{0, 1}); |
| 40 | +static_assert(add<unsigned short>(USHRT_MAX, 1, 0) == Result<unsigned short>{0, 1}); |
| 41 | +static_assert(add<unsigned short>(USHRT_MAX, 1, 1) == Result<unsigned short>{1, 1}); |
| 42 | + |
| 43 | +static_assert(add<unsigned int>(0, 0, 0) == Result<unsigned int>{0, 0}); |
| 44 | +static_assert(add<unsigned int>(0, 0, 1) == Result<unsigned int>{1, 0}); |
| 45 | +static_assert(add<unsigned int>(UINT_MAX - 1, 1, 1) == Result<unsigned int>{0, 1}); |
| 46 | +static_assert(add<unsigned int>(UINT_MAX, 1, 0) == Result<unsigned int>{0, 1}); |
| 47 | +static_assert(add<unsigned int>(UINT_MAX, 1, 1) == Result<unsigned int>{1, 1}); |
| 48 | + |
| 49 | +static_assert(add<unsigned long>(0, 0, 0) == Result<unsigned long>{0, 0}); |
| 50 | +static_assert(add<unsigned long>(0, 0, 1) == Result<unsigned long>{1, 0}); |
| 51 | +static_assert(add<unsigned long>(ULONG_MAX - 1, 1, 1) == Result<unsigned long>{0, 1}); |
| 52 | +static_assert(add<unsigned long>(ULONG_MAX, 1, 0) == Result<unsigned long>{0, 1}); |
| 53 | +static_assert(add<unsigned long>(ULONG_MAX, 1, 1) == Result<unsigned long>{1, 1}); |
| 54 | + |
| 55 | +static_assert(add<unsigned long long>(0, 0, 0) == Result<unsigned long long>{0, 0}); |
| 56 | +static_assert(add<unsigned long long>(0, 0, 1) == Result<unsigned long long>{1, 0}); |
| 57 | +static_assert(add<unsigned long long>(ULLONG_MAX - 1, 1, 1) == Result<unsigned long long>{0, 1}); |
| 58 | +static_assert(add<unsigned long long>(ULLONG_MAX, 1, 0) == Result<unsigned long long>{0, 1}); |
| 59 | +static_assert(add<unsigned long long>(ULLONG_MAX, 1, 1) == Result<unsigned long long>{1, 1}); |
| 60 | + |
| 61 | +template<typename T> |
| 62 | +constexpr Result<T> sub(T Lhs, T Rhs, T Carryin) |
| 63 | +{ |
| 64 | + T Carryout = 0; |
| 65 | + if constexpr(__is_same(T, unsigned char)) |
| 66 | + return { __builtin_subcb(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 67 | + else if constexpr(__is_same(T, unsigned short)) |
| 68 | + return { __builtin_subcs(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 69 | + else if constexpr(__is_same(T, unsigned int)) |
| 70 | + return { __builtin_subc(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 71 | + else if constexpr(__is_same(T, unsigned long)) |
| 72 | + return { __builtin_subcl(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 73 | + else if constexpr(__is_same(T, unsigned long long)) |
| 74 | + return { __builtin_subcll(Lhs, Rhs, Carryin, &Carryout), Carryout }; |
| 75 | +} |
| 76 | + |
| 77 | +static_assert(sub<unsigned char>(0, 0, 0) == Result<unsigned char>{0, 0}); |
| 78 | +static_assert(sub<unsigned char>(0, 0, 1) == Result<unsigned char>{UCHAR_MAX, 1}); |
| 79 | +static_assert(sub<unsigned char>(0, 1, 0) == Result<unsigned char>{UCHAR_MAX, 1}); |
| 80 | +static_assert(sub<unsigned char>(0, 1, 1) == Result<unsigned char>{UCHAR_MAX - 1, 1}); |
| 81 | +static_assert(sub<unsigned char>(1, 0, 0) == Result<unsigned char>{1, 0}); |
| 82 | + |
| 83 | +static_assert(sub<unsigned short>(0, 0, 0) == Result<unsigned short>{0, 0}); |
| 84 | +static_assert(sub<unsigned short>(0, 0, 1) == Result<unsigned short>{USHRT_MAX, 1}); |
| 85 | +static_assert(sub<unsigned short>(0, 1, 0) == Result<unsigned short>{USHRT_MAX, 1}); |
| 86 | +static_assert(sub<unsigned short>(0, 1, 1) == Result<unsigned short>{USHRT_MAX - 1, 1}); |
| 87 | +static_assert(sub<unsigned short>(1, 0, 0) == Result<unsigned short>{1, 0}); |
| 88 | + |
| 89 | +static_assert(sub<unsigned int>(0, 0, 0) == Result<unsigned int>{0, 0}); |
| 90 | +static_assert(sub<unsigned int>(0, 0, 1) == Result<unsigned int>{UINT_MAX, 1}); |
| 91 | +static_assert(sub<unsigned int>(0, 1, 0) == Result<unsigned int>{UINT_MAX, 1}); |
| 92 | +static_assert(sub<unsigned int>(0, 1, 1) == Result<unsigned int>{UINT_MAX - 1, 1}); |
| 93 | +static_assert(sub<unsigned int>(1, 0, 0) == Result<unsigned int>{1, 0}); |
| 94 | + |
| 95 | +static_assert(sub<unsigned long>(0, 0, 0) == Result<unsigned long>{0, 0}); |
| 96 | +static_assert(sub<unsigned long>(0, 0, 1) == Result<unsigned long>{ULONG_MAX, 1}); |
| 97 | +static_assert(sub<unsigned long>(0, 1, 0) == Result<unsigned long>{ULONG_MAX, 1}); |
| 98 | +static_assert(sub<unsigned long>(0, 1, 1) == Result<unsigned long>{ULONG_MAX - 1, 1}); |
| 99 | +static_assert(sub<unsigned long>(1, 0, 0) == Result<unsigned long>{1, 0}); |
| 100 | + |
| 101 | +static_assert(sub<unsigned long long>(0, 0, 0) == Result<unsigned long long>{0, 0}); |
| 102 | +static_assert(sub<unsigned long long>(0, 0, 1) == Result<unsigned long long>{ULLONG_MAX, 1}); |
| 103 | +static_assert(sub<unsigned long long>(0, 1, 0) == Result<unsigned long long>{ULLONG_MAX, 1}); |
| 104 | +static_assert(sub<unsigned long long>(0, 1, 1) == Result<unsigned long long>{ULLONG_MAX - 1, 1}); |
| 105 | +static_assert(sub<unsigned long long>(1, 0, 0) == Result<unsigned long long>{1, 0}); |
0 commit comments