[PATCH] D155410: [clang][Interp] Fix comparing nan/inf floating point values

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 28 02:12:51 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9a0164a0c66c: [clang][Interp] Fix comparing nan/inf floating point values (authored by tbaeder).

Changed prior to commit:
  https://reviews.llvm.org/D155410?vs=540832&id=545072#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155410/new/

https://reviews.llvm.org/D155410

Files:
  clang/lib/AST/Interp/Floating.h
  clang/test/AST/Interp/floats.cpp


Index: clang/test/AST/Interp/floats.cpp
===================================================================
--- clang/test/AST/Interp/floats.cpp
+++ clang/test/AST/Interp/floats.cpp
@@ -145,3 +145,12 @@
 namespace LongDouble {
   constexpr long double ld = 3.1425926539;
 }
+
+namespace Compare {
+  constexpr float nan = __builtin_nan("");
+  constexpr float inf = __builtin_inf();
+  static_assert(!(nan == nan), "");
+  static_assert(nan != nan, "");
+  static_assert(!(inf < nan), "");
+  static_assert(!(inf > nan), "");
+}
Index: clang/lib/AST/Interp/Floating.h
===================================================================
--- clang/lib/AST/Interp/Floating.h
+++ clang/lib/AST/Interp/Floating.h
@@ -90,7 +90,18 @@
   bool isFinite() const { return F.isFinite(); }
 
   ComparisonCategoryResult compare(const Floating &RHS) const {
-    return Compare(F, RHS.F);
+    llvm::APFloatBase::cmpResult CmpRes = F.compare(RHS.F);
+    switch (CmpRes) {
+    case llvm::APFloatBase::cmpLessThan:
+      return ComparisonCategoryResult::Less;
+    case llvm::APFloatBase::cmpEqual:
+      return ComparisonCategoryResult::Equal;
+    case llvm::APFloatBase::cmpGreaterThan:
+      return ComparisonCategoryResult::Greater;
+    case llvm::APFloatBase::cmpUnordered:
+      return ComparisonCategoryResult::Unordered;
+    }
+    llvm_unreachable("Inavlid cmpResult value");
   }
 
   static APFloat::opStatus fromIntegral(APSInt Val,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155410.545072.patch
Type: text/x-patch
Size: 1438 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230728/ae9ae864/attachment.bin>


More information about the cfe-commits mailing list