[llvm-commits] [llvm] r46656 - in /llvm/trunk: include/llvm-c/Core.h lib/VMCore/Core.cpp test/Bindings/Ocaml/vmcore.ml

Gordon Henriksen gordonhenriksen at mac.com
Fri Feb 1 17:07:51 PST 2008


Author: gordon
Date: Fri Feb  1 19:07:50 2008
New Revision: 46656

URL: http://llvm.org/viewvc/llvm-project?rev=46656&view=rev
Log:
Fixing a bug creating floating point constants of type other
than double through the C bindings. Thanks to Tomas Lindquist
Olsen for reporting it.

Modified:
    llvm/trunk/include/llvm-c/Core.h
    llvm/trunk/lib/VMCore/Core.cpp
    llvm/trunk/test/Bindings/Ocaml/vmcore.ml

Modified: llvm/trunk/include/llvm-c/Core.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=46656&r1=46655&r2=46656&view=diff

==============================================================================
--- llvm/trunk/include/llvm-c/Core.h (original)
+++ llvm/trunk/include/llvm-c/Core.h Fri Feb  1 19:07:50 2008
@@ -289,6 +289,7 @@
 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,

Modified: llvm/trunk/lib/VMCore/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Core.cpp?rev=46656&r1=46655&r2=46656&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Core.cpp (original)
+++ llvm/trunk/lib/VMCore/Core.cpp Fri Feb  1 19:07:50 2008
@@ -284,8 +284,30 @@
   return wrap(ConstantInt::get(unwrap<IntegerType>(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 ...................................--*/

Modified: llvm/trunk/test/Bindings/Ocaml/vmcore.ml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/Ocaml/vmcore.ml?rev=46656&r1=46655&r2=46656&view=diff

==============================================================================
--- llvm/trunk/test/Bindings/Ocaml/vmcore.ml (original)
+++ llvm/trunk/test/Bindings/Ocaml/vmcore.ml Fri Feb  1 19:07:50 2008
@@ -211,12 +211,18 @@
   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





More information about the llvm-commits mailing list