Skip to content

Commit 4cf996d

Browse files
authored
[clang][Interp] Implement IntegralAP::mul() (#72491)
1 parent 545f4d9 commit 4cf996d

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

clang/lib/AST/Interp/IntegralAP.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,15 @@ template <bool Signed> class IntegralAP final {
191191
}
192192

193193
static bool add(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
194-
return CheckAddSubUB<std::plus>(A, B, OpBits, R);
194+
return CheckAddSubMulUB<std::plus>(A, B, OpBits, R);
195195
}
196196

197197
static bool sub(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
198-
return CheckAddSubUB<std::minus>(A, B, OpBits, R);
198+
return CheckAddSubMulUB<std::minus>(A, B, OpBits, R);
199199
}
200200

201201
static bool mul(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
202-
// FIXME: Implement.
203-
assert(false);
204-
return false;
202+
return CheckAddSubMulUB<std::multiplies>(A, B, OpBits, R);
205203
}
206204

207205
static bool rem(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
@@ -262,8 +260,8 @@ template <bool Signed> class IntegralAP final {
262260

263261
private:
264262
template <template <typename T> class Op>
265-
static bool CheckAddSubUB(const IntegralAP &A, const IntegralAP &B,
266-
unsigned BitWidth, IntegralAP *R) {
263+
static bool CheckAddSubMulUB(const IntegralAP &A, const IntegralAP &B,
264+
unsigned BitWidth, IntegralAP *R) {
267265
if constexpr (!Signed) {
268266
R->V = Op<APInt>{}(A.V, B.V);
269267
return false;

clang/test/AST/Interp/intap.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ static_assert(UBitIntZero1 == 0, "");
3535
constexpr unsigned _BitInt(2) BI1 = 3u;
3636
static_assert(BI1 == 3, "");
3737

38+
constexpr _BitInt(4) MulA = 5;
39+
constexpr _BitInt(4) MulB = 7;
40+
static_assert(MulA * MulB == 50, ""); // ref-error {{not an integral constant expression}} \
41+
// ref-note {{value 35 is outside the range of representable values of type '_BitInt(4)'}} \
42+
// expected-error {{not an integral constant expression}} \
43+
// expected-note {{value 35 is outside the range of representable values of type '_BitInt(4)'}}
44+
static_assert(MulA * 5 == 25, "");
45+
static_assert(-1 * MulB == -7, "");
46+
3847
namespace APCast {
3948
constexpr _BitInt(10) A = 1;
4049
constexpr _BitInt(11) B = A;
@@ -66,13 +75,20 @@ namespace i128 {
6675
// ref-error {{static assertion failed}} \
6776
// ref-note {{'340282366920938463463374607431768211455 == 1'}}
6877

78+
constexpr uint128_t TooMuch = UINT128_MAX * 2;
79+
6980
static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
7081
static_assert(INT128_MAX != 0, "");
7182
static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \
7283
// expected-note {{evaluates to '170141183460469231731687303715884105727 == 0'}} \
7384
// ref-error {{failed}} \
7485
// ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
7586

87+
constexpr int128_t TooMuch2 = INT128_MAX * INT128_MAX; // ref-error {{must be initialized by a constant expression}} \
88+
// ref-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}} \
89+
// expected-error {{must be initialized by a constant expression}} \
90+
// expected-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}}
91+
7692
static const __int128_t INT128_MIN = -INT128_MAX - 1;
7793
constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \
7894
// expected-note {{value 170141183460469231731687303715884105728 is outside the range}} \

0 commit comments

Comments
 (0)