llvm.org GIT mirror llvm / e62a8a3
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 Gordon Henriksen 12 years ago
3 changed file(s) with 36 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
288288 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
289289 int SignExtend);
290290 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N);
291 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text);
291292
292293 /* Operations on composite constants */
293294 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
283283 return wrap(ConstantInt::get(unwrap(IntTy), N, SignExtend != 0));
284284 }
285285
286 static const fltSemantics &SemanticsForType(Type *Ty) {
287 assert(Ty->isFloatingPoint() && "Type is not floating point!");
288 if (Ty == Type::FloatTy)
289 return APFloat::IEEEsingle;
290 if (Ty == Type::DoubleTy)
291 return APFloat::IEEEdouble;
292 if (Ty == Type::X86_FP80Ty)
293 return APFloat::x87DoubleExtended;
294 if (Ty == Type::FP128Ty)
295 return APFloat::IEEEquad;
296 if (Ty == Type::PPC_FP128Ty)
297 return APFloat::PPCDoubleDouble;
298 return APFloat::Bogus;
299 }
300
286301 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
287 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
302 APFloat APN(N);
303 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
304 return wrap(ConstantFP::get(unwrap(RealTy), APN));
305 }
306
307 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
308 return wrap(ConstantFP::get(unwrap(RealTy),
309 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
288310 }
289311
290312 /*--.. Operations on composite constants ...................................--*/
210210 ignore (define_global "Const05" c m);
211211 insist ((array_type i8_type 9) = type_of c);
212212
213 (* RUN: grep {Const06.*3.1459} < %t.ll
214 *)
215 group "real";
216 let c = const_float double_type 3.1459 in
217 ignore (define_global "Const06" c m);
218 insist (double_type = type_of c);
213 (* RUN: grep {ConstSingle.*2.75} < %t.ll
214 * RUN: grep {ConstDouble.*3.1459} < %t.ll
215 *)
216 begin group "real";
217 let cs = const_float float_type 2.75 in
218 ignore (define_global "ConstSingle" cs m);
219 insist (float_type = type_of cs);
220
221 let cd = const_float double_type 3.1459 in
222 ignore (define_global "ConstDouble" cd m);
223 insist (double_type = type_of cd)
224 end;
219225
220226 let one = const_int i16_type 1 in
221227 let two = const_int i16_type 2 in