Skip to content

Commit 3cc200f

Browse files
authored
DynamicAPInt: Support APInt constructor. (#146301)
This PR introduces a constructor for `DynamicAPInt` that takes an `APInt` object as input to simplifies the creation of a large numbers.
1 parent 65cb0ea commit 3cc200f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

llvm/include/llvm/ADT/DynamicAPInt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef LLVM_ADT_DYNAMICAPINT_H
1717
#define LLVM_ADT_DYNAMICAPINT_H
1818

19+
#include "llvm/ADT/APInt.h"
1920
#include "llvm/ADT/SlowDynamicAPInt.h"
2021
#include "llvm/Support/Compiler.h"
2122
#include "llvm/Support/MathExtras.h"
@@ -116,6 +117,14 @@ class DynamicAPInt {
116117
: ValSmall(Val) {
117118
ValLarge.Val.BitWidth = 0;
118119
}
120+
LLVM_ATTRIBUTE_ALWAYS_INLINE explicit DynamicAPInt(const APInt &Val) {
121+
if (Val.getBitWidth() <= 64) {
122+
ValSmall = Val.getSExtValue();
123+
ValLarge.Val.BitWidth = 0;
124+
} else {
125+
new (&ValLarge) detail::SlowDynamicAPInt(Val);
126+
}
127+
}
119128
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt() : DynamicAPInt(0) {}
120129
LLVM_ATTRIBUTE_ALWAYS_INLINE ~DynamicAPInt() {
121130
if (LLVM_UNLIKELY(isLarge()))

llvm/unittests/ADT/DynamicAPIntTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class TypeNames {
3131
};
3232
TYPED_TEST_SUITE(IntTest, TypeList, TypeNames);
3333

34+
TYPED_TEST(IntTest, ValueInit) {
35+
APInt Large(65, 0, true);
36+
Large.setBit(64);
37+
TypeParam DynLarge(1ll << 63);
38+
EXPECT_EQ(TypeParam(Large), DynLarge + DynLarge);
39+
APInt Small(64, -1, true);
40+
TypeParam DynSmall(Small.getSExtValue());
41+
EXPECT_EQ(TypeParam(Small), DynSmall);
42+
}
43+
3444
TYPED_TEST(IntTest, ops) {
3545
TypeParam Two(2), Five(5), Seven(7), Ten(10);
3646
EXPECT_EQ(Five + Five, Ten);

0 commit comments

Comments
 (0)