diff --git a/clang/include/clang/Basic/Builtins.def b/clang/include/clang/Basic/Builtins.def --- a/clang/include/clang/Basic/Builtins.def +++ b/clang/include/clang/Basic/Builtins.def @@ -1611,16 +1611,16 @@ BUILTIN(__builtin_assume_separate_storage, "vvCD*vCD*", "nE") // Multiprecision Arithmetic Builtins. -BUILTIN(__builtin_addcb, "UcUcCUcCUcCUc*", "n") -BUILTIN(__builtin_addcs, "UsUsCUsCUsCUs*", "n") -BUILTIN(__builtin_addc, "UiUiCUiCUiCUi*", "n") -BUILTIN(__builtin_addcl, "ULiULiCULiCULiCULi*", "n") -BUILTIN(__builtin_addcll, "ULLiULLiCULLiCULLiCULLi*", "n") -BUILTIN(__builtin_subcb, "UcUcCUcCUcCUc*", "n") -BUILTIN(__builtin_subcs, "UsUsCUsCUsCUs*", "n") -BUILTIN(__builtin_subc, "UiUiCUiCUiCUi*", "n") -BUILTIN(__builtin_subcl, "ULiULiCULiCULiCULi*", "n") -BUILTIN(__builtin_subcll, "ULLiULLiCULLiCULLiCULLi*", "n") +BUILTIN(__builtin_addcb, "UcUcCUcCUcCUc*", "nE") +BUILTIN(__builtin_addcs, "UsUsCUsCUsCUs*", "nE") +BUILTIN(__builtin_addc, "UiUiCUiCUiCUi*", "nE") +BUILTIN(__builtin_addcl, "ULiULiCULiCULiCULi*", "nE") +BUILTIN(__builtin_addcll, "ULLiULLiCULLiCULLiCULLi*", "nE") +BUILTIN(__builtin_subcb, "UcUcCUcCUcCUc*", "nE") +BUILTIN(__builtin_subcs, "UsUsCUsCUsCUs*", "nE") +BUILTIN(__builtin_subc, "UiUiCUiCUiCUi*", "nE") +BUILTIN(__builtin_subcl, "ULiULiCULiCULiCULi*", "nE") +BUILTIN(__builtin_subcll, "ULLiULLiCULLiCULLiCULLi*", "nE") // Checked Arithmetic Builtins for Security. BUILTIN(__builtin_add_overflow, "b.", "ntE") diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12551,6 +12551,56 @@ return false; return Success(DidOverflow, E); } + case Builtin::BI__builtin_addcb: + case Builtin::BI__builtin_addcs: + case Builtin::BI__builtin_addc: + case Builtin::BI__builtin_addcl: + case Builtin::BI__builtin_addcll: + case Builtin::BI__builtin_subcb: + case Builtin::BI__builtin_subcs: + case Builtin::BI__builtin_subc: + case Builtin::BI__builtin_subcl: + case Builtin::BI__builtin_subcll: { + APSInt X, Y, CarryIn; + LValue CarryOut; + + QualType ResultType = E->getArg(3)->getType()->getPointeeType(); + if (!EvaluateInteger(E->getArg(0), X, Info) || + !EvaluateInteger(E->getArg(1), Y, Info) || + !EvaluateInteger(E->getArg(2), CarryIn, Info) || + !EvaluatePointer(E->getArg(3), CarryOut, Info)) + return false; + + APInt Result; + bool DidOverflow1 = false; + bool DidOverflow2 = false; + + switch (BuiltinOp) { + default: + llvm_unreachable("Invalid value for BuiltinOp"); + case Builtin::BI__builtin_addcb: + case Builtin::BI__builtin_addcs: + case Builtin::BI__builtin_addc: + case Builtin::BI__builtin_addcl: + case Builtin::BI__builtin_addcll: + Result = X.uadd_ov(Y, DidOverflow1).uadd_ov(CarryIn, DidOverflow2); + break; + case Builtin::BI__builtin_subcb: + case Builtin::BI__builtin_subcs: + case Builtin::BI__builtin_subc: + case Builtin::BI__builtin_subcl: + case Builtin::BI__builtin_subcll: + Result = X.usub_ov(Y, DidOverflow1).usub_ov(CarryIn, DidOverflow2); + break; + } + + APSInt DidOverflow( + APInt(Result.getBitWidth(), DidOverflow1 || DidOverflow2)); + APValue DidOverflowVal{DidOverflow}; + if (!handleAssignment(Info, E, CarryOut, ResultType, DidOverflowVal)) + return false; + return Success(Result, E); + } } } diff --git a/clang/test/SemaCXX/builtins-multiprecision.cpp b/clang/test/SemaCXX/builtins-multiprecision.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/builtins-multiprecision.cpp @@ -0,0 +1,105 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s +// expected-no-diagnostics + +#include + +template +struct Result { + T value; + T carry; + constexpr bool operator==(const Result &Other) { + return value == Other.value && carry == Other.carry; + } +}; + +template +constexpr Result add(T Lhs, T Rhs, T Carryin) +{ + T Carryout = 0; + if constexpr(__is_same(T, unsigned char)) + return { __builtin_addcb(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned short)) + return { __builtin_addcs(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned int)) + return { __builtin_addc(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned long)) + return { __builtin_addcl(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned long long)) + return { __builtin_addcll(Lhs, Rhs, Carryin, &Carryout), Carryout }; +} + +static_assert(add(0, 0, 0) == Result{0, 0}); +static_assert(add(0, 0, 1) == Result{1, 0}); +static_assert(add(UCHAR_MAX - 1, 1, 1) == Result{0, 1}); +static_assert(add(UCHAR_MAX, 1, 0) == Result{0, 1}); +static_assert(add(UCHAR_MAX, 1, 1) == Result{1, 1}); + +static_assert(add(0, 0, 0) == Result{0, 0}); +static_assert(add(0, 0, 1) == Result{1, 0}); +static_assert(add(USHRT_MAX - 1, 1, 1) == Result{0, 1}); +static_assert(add(USHRT_MAX, 1, 0) == Result{0, 1}); +static_assert(add(USHRT_MAX, 1, 1) == Result{1, 1}); + +static_assert(add(0, 0, 0) == Result{0, 0}); +static_assert(add(0, 0, 1) == Result{1, 0}); +static_assert(add(UINT_MAX - 1, 1, 1) == Result{0, 1}); +static_assert(add(UINT_MAX, 1, 0) == Result{0, 1}); +static_assert(add(UINT_MAX, 1, 1) == Result{1, 1}); + +static_assert(add(0, 0, 0) == Result{0, 0}); +static_assert(add(0, 0, 1) == Result{1, 0}); +static_assert(add(ULONG_MAX - 1, 1, 1) == Result{0, 1}); +static_assert(add(ULONG_MAX, 1, 0) == Result{0, 1}); +static_assert(add(ULONG_MAX, 1, 1) == Result{1, 1}); + +static_assert(add(0, 0, 0) == Result{0, 0}); +static_assert(add(0, 0, 1) == Result{1, 0}); +static_assert(add(ULLONG_MAX - 1, 1, 1) == Result{0, 1}); +static_assert(add(ULLONG_MAX, 1, 0) == Result{0, 1}); +static_assert(add(ULLONG_MAX, 1, 1) == Result{1, 1}); + +template +constexpr Result sub(T Lhs, T Rhs, T Carryin) +{ + T Carryout = 0; + if constexpr(__is_same(T, unsigned char)) + return { __builtin_subcb(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned short)) + return { __builtin_subcs(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned int)) + return { __builtin_subc(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned long)) + return { __builtin_subcl(Lhs, Rhs, Carryin, &Carryout), Carryout }; + else if constexpr(__is_same(T, unsigned long long)) + return { __builtin_subcll(Lhs, Rhs, Carryin, &Carryout), Carryout }; +} + +static_assert(sub(0, 0, 0) == Result{0, 0}); +static_assert(sub(0, 0, 1) == Result{UCHAR_MAX, 1}); +static_assert(sub(0, 1, 0) == Result{UCHAR_MAX, 1}); +static_assert(sub(0, 1, 1) == Result{UCHAR_MAX - 1, 1}); +static_assert(sub(1, 0, 0) == Result{1, 0}); + +static_assert(sub(0, 0, 0) == Result{0, 0}); +static_assert(sub(0, 0, 1) == Result{USHRT_MAX, 1}); +static_assert(sub(0, 1, 0) == Result{USHRT_MAX, 1}); +static_assert(sub(0, 1, 1) == Result{USHRT_MAX - 1, 1}); +static_assert(sub(1, 0, 0) == Result{1, 0}); + +static_assert(sub(0, 0, 0) == Result{0, 0}); +static_assert(sub(0, 0, 1) == Result{UINT_MAX, 1}); +static_assert(sub(0, 1, 0) == Result{UINT_MAX, 1}); +static_assert(sub(0, 1, 1) == Result{UINT_MAX - 1, 1}); +static_assert(sub(1, 0, 0) == Result{1, 0}); + +static_assert(sub(0, 0, 0) == Result{0, 0}); +static_assert(sub(0, 0, 1) == Result{ULONG_MAX, 1}); +static_assert(sub(0, 1, 0) == Result{ULONG_MAX, 1}); +static_assert(sub(0, 1, 1) == Result{ULONG_MAX - 1, 1}); +static_assert(sub(1, 0, 0) == Result{1, 0}); + +static_assert(sub(0, 0, 0) == Result{0, 0}); +static_assert(sub(0, 0, 1) == Result{ULLONG_MAX, 1}); +static_assert(sub(0, 1, 0) == Result{ULLONG_MAX, 1}); +static_assert(sub(0, 1, 1) == Result{ULLONG_MAX - 1, 1}); +static_assert(sub(1, 0, 0) == Result{1, 0});