Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,13 @@ LLVM_C_ABI LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy,
const char *Text,
unsigned SLen);

/**
* Obtain a constant for a floating point FP128 value from 2 64 bit values.
* Only the LLVMFP128Type or LLVMPPCFP128Type are accepted.
*/

LLVM_C_ABI LLVMValueRef LLVMConstFP128(LLVMTypeRef Ty, const uint64_t N[2]);

/**
* Obtain the zero extended value for an integer constant value.
*
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,14 @@ LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
}

LLVMValueRef LLVMConstFP128(LLVMTypeRef Ty, const uint64_t N[2]) {
Type *T = unwrap(Ty);
assert(T->getPrimitiveSizeInBits() == 128 && "Ty size should be 128");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert(T->getPrimitiveSizeInBits() == 128 && "Ty size should be 128");
assert(T->getScalarSizeInBits() == 128 && "Ty size should be 128");

To allow vector of float.

APInt AI(128, ArrayRef<uint64_t>(N, 2));
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than hardcoding 128 here, we can use the size of the float (e.g. via getScalarSizeInBits). The corresponding size of the array would be divideCeil(SizeInBits, 64). Then this would work for all sizes of floats.

APFloat Quad(T->getFltSemantics(), AI);
return wrap(ConstantFP::get(T, Quad));
}

unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
}
Expand Down
14 changes: 14 additions & 0 deletions llvm/unittests/IR/ConstantsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,5 +835,19 @@ TEST(ConstantsTest, BlockAddressCAPITest) {
EXPECT_EQ(&BB, OutBB);
}

TEST(ConstantsTest, Float128Test) {
LLVMTypeRef Ty128 = LLVMFP128TypeInContext(LLVMGetGlobalContext());
LLVMTypeRef TyPPC128 = LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
LLVMBuilderRef Builder = LLVMCreateBuilder();
uint64_t n[2] = {0x4000000000000000, 0x0}; //+2
uint64_t m[2] = {0xC000000000000000, 0x0}; //-2
LLVMValueRef val1 = LLVMConstFP128(Ty128, n);
LLVMValueRef val2 = LLVMConstFP128(Ty128, m);
LLVMValueRef val3 = LLVMBuildFAdd(Builder, val1, val2, "test");
EXPECT_TRUE(val3 != nullptr);
LLVMValueRef val4 = LLVMConstFP128(TyPPC128, n);
EXPECT_TRUE(val4 != nullptr);
}

} // end anonymous namespace
} // end namespace llvm