diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 4e380d9bd5969..52bb03a605999 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -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. * diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 27d8294b01264..de00a4f9c2a39 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -1573,6 +1573,15 @@ 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); + unsigned SB = T->getScalarSizeInBits(); + assert(SB == 128 && "Ty size should be 128"); + APInt AI(SB, ArrayRef(N, divideCeil(SB, 64))); + APFloat Quad(T->getFltSemantics(), AI); + return wrap(ConstantFP::get(T, Quad)); +} + unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { return unwrap(ConstantVal)->getZExtValue(); } diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp index 6376165cbe766..4fb847de228cc 100644 --- a/llvm/unittests/IR/ConstantsTest.cpp +++ b/llvm/unittests/IR/ConstantsTest.cpp @@ -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