]> granicus.if.org Git - clang/commitdiff
Add support for generating MIPS legacy NaN
authorPetar Jovanovic <petar.jovanovic@imgtec.com>
Thu, 26 Feb 2015 18:19:22 +0000 (18:19 +0000)
committerPetar Jovanovic <petar.jovanovic@imgtec.com>
Thu, 26 Feb 2015 18:19:22 +0000 (18:19 +0000)
Currently, the NaN values emitted for MIPS architectures do not cover
non-IEEE754-2008 compliant case. This change fixes the issue.

Patch by Vladimir Radosavljevic.

Differential Revision: http://reviews.llvm.org/D7882

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@230653 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/TargetInfo.h
lib/AST/ExprConstant.cpp
lib/Basic/Targets.cpp
test/CodeGen/builtin-nan-legacy.c [new file with mode: 0644]

index 69a54044680d0db3488d690fda002e476195e6ab..e00e0f5a58294505626fe2d46f7475029b159027 100644 (file)
@@ -638,6 +638,12 @@ public:
     return std::string(1, *Constraint);
   }
 
+  /// \brief Returns true if NaN encoding is IEEE 754-2008.
+  /// Only MIPS allows a different encoding.
+  virtual bool isNan2008() const {
+    return true;
+  }
+
   /// \brief Returns a string of target-specific clobbers, in LLVM format.
   virtual const char *getClobbers() const = 0;
 
index 6569626a357dc7f6329408a0236a60e5f28ae7c1..083572bc3e1439c030bbc33f269e1f7f0a96806a 100644 (file)
@@ -7590,10 +7590,23 @@ static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
   else if (S->getString().getAsInteger(0, fill))
     return false;
 
-  if (SNaN)
-    Result = llvm::APFloat::getSNaN(Sem, false, &fill);
-  else
-    Result = llvm::APFloat::getQNaN(Sem, false, &fill);
+  if (Context.getTargetInfo().isNan2008()) {
+    if (SNaN)
+      Result = llvm::APFloat::getSNaN(Sem, false, &fill);
+    else
+      Result = llvm::APFloat::getQNaN(Sem, false, &fill);
+  } else {
+    // Prior to IEEE 754-2008, architectures were allowed to choose whether
+    // the first bit of their significand was set for qNaN or sNaN. MIPS chose
+    // a different encoding to what became a standard in 2008, and for pre-
+    // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
+    // sNaN. This is now known as "legacy NaN" encoding.
+    if (SNaN)
+      Result = llvm::APFloat::getQNaN(Sem, false, &fill);
+    else
+      Result = llvm::APFloat::getSNaN(Sem, false, &fill);
+  }
+
   return true;
 }
 
index a848d4ce2e525477bb25c4745c171638f5a3635f..03cd853d66ea89b5dd8bbbbce6b487e3969944da 100644 (file)
@@ -5674,6 +5674,10 @@ public:
     return CPU == "mips32r6" || ABI == "n32" || ABI == "n64" || ABI == "64";
   }
 
+  bool isNan2008() const override {
+    return IsNan2008;
+  }
+
   StringRef getABI() const override { return ABI; }
   bool setCPU(const std::string &Name) override {
     bool IsMips32 = getTriple().getArch() == llvm::Triple::mips ||
diff --git a/test/CodeGen/builtin-nan-legacy.c b/test/CodeGen/builtin-nan-legacy.c
new file mode 100644 (file)
index 0000000..cd0f0fd
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clang -target mipsel-unknown-linux -mnan=legacy -emit-llvm -S %s -o - | FileCheck %s
+// CHECK: float 0x7FF4000000000000, float 0x7FF8000000000000
+// CHECK: double 0x7FF4000000000000, double 0x7FF8000000000000
+
+float f[] = {
+  __builtin_nan(""),
+  __builtin_nans(""),
+};
+
+double d[] = {
+  __builtin_nan(""),
+  __builtin_nans(""),
+};