Author: Timm Baeder Date: 2024-01-15T09:36:19+01:00 New Revision: c3ced6ac62cdd7c50f2429beb8ab5fc69a1a1a75
URL: https://github.com/llvm/llvm-project/commit/c3ced6ac62cdd7c50f2429beb8ab5fc69a1a1a75 DIFF: https://github.com/llvm/llvm-project/commit/c3ced6ac62cdd7c50f2429beb8ab5fc69a1a1a75.diff LOG: [clang][Interp] Implement IntegralAP::{div, rem} (#72614) 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 d5f46409d231d4..55e29caa1cd747 100644 --- a/clang/lib/AST/Interp/IntegralAP.h +++ b/clang/lib/AST/Interp/IntegralAP.h @@ -204,14 +204,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 b99422dc8f9312..118dc21b67e875 100644 --- a/clang/test/AST/Interp/intap.cpp +++ b/clang/test/AST/Interp/intap.cpp @@ -5,6 +5,7 @@ using MaxBitInt = _BitInt(128); +#define INT_MIN (~__INT_MAX__) constexpr _BitInt(2) A = 0; constexpr _BitInt(2) B = A + 1; @@ -44,6 +45,35 @@ 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}} + +constexpr _BitInt(32) bottom = -1; +constexpr _BitInt(32) top = INT_MIN; +constexpr _BitInt(32) nope = top / bottom; // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{value 2147483648 is outside the range}} \ + // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{value 2147483648 is outside the range}} +constexpr _BitInt(32) noooo = top % bottom; // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{value 2147483648 is outside the range}} \ + // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{value 2147483648 is outside the range}} + namespace APCast { constexpr _BitInt(10) A = 1; constexpr _BitInt(11) B = A; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits