[clang] [clang][Interp] Implement IntegralAP::{div, rem} (PR #72614)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 16 23:12:39 PST 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/72614

None

>From 5fe32770c2c95cb8c7604983edc264f16edf5821 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 16 Nov 2023 18:05:17 +0100
Subject: [PATCH] [clang][Interp] Implement IntegralAP::{div, rem}

---
 clang/lib/AST/Interp/IntegralAP.h | 12 ++++++++----
 clang/test/AST/Interp/intap.cpp   | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index 9019f32e6cef2a3..17fc0695c98bb70 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -208,14 +208,18 @@ template <bool Signed> class IntegralAP final {
   }
 
   static bool rem(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
-    // FIXME: Implement.
-    assert(false);
+    if constexpr (Signed)
+      *R = IntegralAP(A.V.srem(B.V));
+    else
+      *R = IntegralAP(A.V.urem(B.V));
     return false;
   }
 
   static bool div(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
-    // FIXME: Implement.
-    assert(false);
+    if constexpr (Signed)
+      *R = IntegralAP(A.V.sdiv(B.V));
+    else
+      *R = IntegralAP(A.V.udiv(B.V));
     return false;
   }
 
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index c93ec331296647b..30cff785fb34cf7 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -44,6 +44,24 @@ static_assert(MulA * MulB == 50, ""); // ref-error {{not an integral constant ex
 static_assert(MulA * 5 == 25, "");
 static_assert(-1 * MulB == -7, "");
 
+
+constexpr _BitInt(4) DivA = 2;
+constexpr _BitInt(2) DivB = 1;
+static_assert(DivA / DivB == 2, "");
+
+constexpr _BitInt(4) DivC = DivA / 0; // ref-error {{must be initialized by a constant expression}} \
+                                      // ref-note {{division by zero}} \
+                                      // expected-error {{must be initialized by a constant expression}} \
+                                      // expected-note {{division by zero}}
+
+constexpr _BitInt(7) RemA = 47;
+constexpr _BitInt(6) RemB = 9;
+static_assert(RemA % RemB == 2, "");
+static_assert(RemA % 0 == 1, ""); // ref-error {{not an integral constant expression}} \
+                                  // ref-note {{division by zero}} \
+                                  // expected-error {{not an integral constant expression}} \
+                                  // expected-note {{division by zero}}
+
 namespace APCast {
   constexpr _BitInt(10) A = 1;
   constexpr _BitInt(11) B = A;



More information about the cfe-commits mailing list