[cfe-commits] r72607 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/CodeGen/builtin-nanf.c
Mike Stump
mrs at apple.com
Fri May 29 20:56:50 PDT 2009
Author: mrs
Date: Fri May 29 22:56:50 2009
New Revision: 72607
URL: http://llvm.org/viewvc/llvm-project?rev=72607&view=rev
Log:
Improve __builtin_nanf support; we now can deal with them as constants.
Added:
cfe/trunk/test/CodeGen/builtin-nanf.c
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=72607&r1=72606&r2=72607&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Fri May 29 22:56:50 2009
@@ -18,6 +18,8 @@
#include "clang/AST/ASTDiagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/Support/Compiler.h"
+#include <cstring>
+
using namespace clang;
using llvm::APSInt;
using llvm::APFloat;
@@ -1311,14 +1313,23 @@
case Builtin::BI__builtin_nan:
case Builtin::BI__builtin_nanf:
case Builtin::BI__builtin_nanl:
- // If this is __builtin_nan("") turn this into a simple nan, otherwise we
+ // If this is __builtin_nan() turn this into a nan, otherwise we
// can't constant fold it.
if (const StringLiteral *S =
dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
- if (!S->isWide() && S->getByteLength() == 0) { // empty string.
+ if (!S->isWide()) {
const llvm::fltSemantics &Sem =
Info.Ctx.getFloatTypeSemantics(E->getType());
- Result = llvm::APFloat::getNaN(Sem);
+ char *s = (char *)malloc (S->getByteLength()+1);
+ memcpy(s, S->getStrData(), S->getByteLength());
+ s[S->getByteLength()] = 0;
+ long l;
+ char *endp;
+ l = strtol(S->getStrData(), &endp, 0);
+ if (endp != (S->getStrData() + S->getByteLength()))
+ return false;
+ unsigned type = (unsigned int)l;;
+ Result = llvm::APFloat::getNaN(Sem, false, type);
return true;
}
}
Added: cfe/trunk/test/CodeGen/builtin-nanf.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtin-nanf.c?rev=72607&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/builtin-nanf.c (added)
+++ cfe/trunk/test/CodeGen/builtin-nanf.c Fri May 29 22:56:50 2009
@@ -0,0 +1,15 @@
+// RUN: clang-cc -triple x86_64-apple-darwin9 -emit-llvm -o %t %s &&
+// RUN: grep 'float 0x7FF8000000000000, float 0x7FF8000000000000, float 0x7FF8000020000000, float 0x7FF8000000000000, float 0x7FF80001E0000000, float 0x7FF8001E00000000, float 0x7FF801E000000000, float 0x7FF81E0000000000, float 0x7FF9E00000000000, float 0x7FFFFFFFE0000000' %t
+
+float n[] = {
+ __builtin_nanf("0"),
+ __builtin_nanf(""),
+ __builtin_nanf("1"),
+ __builtin_nanf("0x7fc00000"),
+ __builtin_nanf("0x7fc0000f"),
+ __builtin_nanf("0x7fc000f0"),
+ __builtin_nanf("0x7fc00f00"),
+ __builtin_nanf("0x7fc0f000"),
+ __builtin_nanf("0x7fcf0000"),
+ __builtin_nanf("0xffffffff"),
+};
More information about the cfe-commits
mailing list