From e62a8a353c3b21b551c00b9025800d3352e5349e Mon Sep 17 00:00:00 2001 From: Gordon Henriksen Date: Sat, 2 Feb 2008 01:07:50 +0000 Subject: [PATCH] Fixing a bug creating floating point constants of type other than double through the C bindings. Thanks to Tomas Lindquist Olsen for reporting it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46656 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm-c/Core.h | 1 + lib/VMCore/Core.cpp | 24 +++++++++++++++++++++++- test/Bindings/Ocaml/vmcore.ml | 16 +++++++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 9def5158299..9b11df51931 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -289,6 +289,7 @@ int LLVMIsUndef(LLVMValueRef Val); LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, int SignExtend); LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N); +LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text); /* Operations on composite constants */ LLVMValueRef LLVMConstString(const char *Str, unsigned Length, diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index 8722785bd39..0c913ffbf73 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -284,8 +284,30 @@ LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, return wrap(ConstantInt::get(unwrap(IntTy), N, SignExtend != 0)); } +static const fltSemantics &SemanticsForType(Type *Ty) { + assert(Ty->isFloatingPoint() && "Type is not floating point!"); + if (Ty == Type::FloatTy) + return APFloat::IEEEsingle; + if (Ty == Type::DoubleTy) + return APFloat::IEEEdouble; + if (Ty == Type::X86_FP80Ty) + return APFloat::x87DoubleExtended; + if (Ty == Type::FP128Ty) + return APFloat::IEEEquad; + if (Ty == Type::PPC_FP128Ty) + return APFloat::PPCDoubleDouble; + return APFloat::Bogus; +} + LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { - return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N))); + APFloat APN(N); + APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven); + return wrap(ConstantFP::get(unwrap(RealTy), APN)); +} + +LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { + return wrap(ConstantFP::get(unwrap(RealTy), + APFloat(SemanticsForType(unwrap(RealTy)), Text))); } /*--.. Operations on composite constants ...................................--*/ diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml index 457e7664efa..cfa22d503e5 100644 --- a/test/Bindings/Ocaml/vmcore.ml +++ b/test/Bindings/Ocaml/vmcore.ml @@ -211,12 +211,18 @@ let test_constants () = ignore (define_global "Const05" c m); insist ((array_type i8_type 9) = type_of c); - (* RUN: grep {Const06.*3.1459} < %t.ll + (* RUN: grep {ConstSingle.*2.75} < %t.ll + * RUN: grep {ConstDouble.*3.1459} < %t.ll *) - group "real"; - let c = const_float double_type 3.1459 in - ignore (define_global "Const06" c m); - insist (double_type = type_of c); + begin group "real"; + let cs = const_float float_type 2.75 in + ignore (define_global "ConstSingle" cs m); + insist (float_type = type_of cs); + + let cd = const_float double_type 3.1459 in + ignore (define_global "ConstDouble" cd m); + insist (double_type = type_of cd) + end; let one = const_int i16_type 1 in let two = const_int i16_type 2 in