Author: Timm Baeder Date: 2023-11-16T15:06:11+01:00 New Revision: 4cf996d6a20d8fd0817c953fd383d261409e6d04
URL: https://github.com/llvm/llvm-project/commit/4cf996d6a20d8fd0817c953fd383d261409e6d04 DIFF: https://github.com/llvm/llvm-project/commit/4cf996d6a20d8fd0817c953fd383d261409e6d04.diff LOG: [clang][Interp] Implement IntegralAP::mul() (#72491) Added: Modified: clang/lib/AST/Interp/IntegralAP.h clang/test/AST/Interp/intap.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h index d85904a8f9572eb..bd665959cf3dcc4 100644 --- a/clang/lib/AST/Interp/IntegralAP.h +++ b/clang/lib/AST/Interp/IntegralAP.h @@ -191,17 +191,15 @@ template <bool Signed> class IntegralAP final { } static bool add(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { - return CheckAddSubUB<std::plus>(A, B, OpBits, R); + return CheckAddSubMulUB<std::plus>(A, B, OpBits, R); } static bool sub(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { - return CheckAddSubUB<std::minus>(A, B, OpBits, R); + return CheckAddSubMulUB<std::minus>(A, B, OpBits, R); } static bool mul(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { - // FIXME: Implement. - assert(false); - return false; + return CheckAddSubMulUB<std::multiplies>(A, B, OpBits, R); } static bool rem(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) { @@ -262,8 +260,8 @@ template <bool Signed> class IntegralAP final { private: template <template <typename T> class Op> - static bool CheckAddSubUB(const IntegralAP &A, const IntegralAP &B, - unsigned BitWidth, IntegralAP *R) { + static bool CheckAddSubMulUB(const IntegralAP &A, const IntegralAP &B, + unsigned BitWidth, IntegralAP *R) { if constexpr (!Signed) { R->V = Op<APInt>{}(A.V, B.V); return false; diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp index 4a18f1f9459e301..c93ec331296647b 100644 --- a/clang/test/AST/Interp/intap.cpp +++ b/clang/test/AST/Interp/intap.cpp @@ -35,6 +35,15 @@ static_assert(UBitIntZero1 == 0, ""); constexpr unsigned _BitInt(2) BI1 = 3u; static_assert(BI1 == 3, ""); +constexpr _BitInt(4) MulA = 5; +constexpr _BitInt(4) MulB = 7; +static_assert(MulA * MulB == 50, ""); // ref-error {{not an integral constant expression}} \ + // ref-note {{value 35 is outside the range of representable values of type '_BitInt(4)'}} \ + // expected-error {{not an integral constant expression}} \ + // expected-note {{value 35 is outside the range of representable values of type '_BitInt(4)'}} +static_assert(MulA * 5 == 25, ""); +static_assert(-1 * MulB == -7, ""); + namespace APCast { constexpr _BitInt(10) A = 1; constexpr _BitInt(11) B = A; @@ -66,6 +75,8 @@ namespace i128 { // ref-error {{static assertion failed}} \ // ref-note {{'340282366920938463463374607431768211455 == 1'}} + constexpr uint128_t TooMuch = UINT128_MAX * 2; + static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1; static_assert(INT128_MAX != 0, ""); static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \ @@ -73,6 +84,11 @@ namespace i128 { // ref-error {{failed}} \ // ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}} + constexpr int128_t TooMuch2 = INT128_MAX * INT128_MAX; // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}} \ + // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}} + static const __int128_t INT128_MIN = -INT128_MAX - 1; constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \ // expected-note {{value 170141183460469231731687303715884105728 is outside the range}} \ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits