Skip to content

[clang][Interp] Implement IntegralAP::mul() #72491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2023
Merged

Conversation

tbaederr
Copy link
Contributor

No description provided.

@tbaederr tbaederr added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 16, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 16, 2023

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/72491.diff

2 Files Affected:

  • (modified) clang/lib/AST/Interp/IntegralAP.h (+5-7)
  • (modified) clang/test/AST/Interp/intap.cpp (+16)
diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index b8e37878ce2f848..b9d7eab331d27bd 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) {
@@ -264,8 +262,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 c3cae9a64780d5c..f04a82af2736ca5 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}} \

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tbaederr tbaederr merged commit 4cf996d into llvm:main Nov 16, 2023
sr-tream pushed a commit to sr-tream/llvm-project that referenced this pull request Nov 20, 2023
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants