[clang] 9a0164a - [clang][Interp] Fix comparing nan/inf floating point values

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


Author: Timm Bäder
Date: 2023-07-28T11:12:25+02:00
New Revision: 9a0164a0c66c28363fbc9410edd5656b28811427

URL: https://github.com/llvm/llvm-project/commit/9a0164a0c66c28363fbc9410edd5656b28811427
DIFF: https://github.com/llvm/llvm-project/commit/9a0164a0c66c28363fbc9410edd5656b28811427.diff

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

Differential Revision: https://reviews.llvm.org/D155410

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Floating.h b/clang/lib/AST/Interp/Floating.h
index 85876236a9998e..447361a43696b0 100644
--- a/clang/lib/AST/Interp/Floating.h
+++ b/clang/lib/AST/Interp/Floating.h
@@ -90,7 +90,18 @@ class Floating final {
   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,

diff  --git a/clang/test/AST/Interp/floats.cpp b/clang/test/AST/Interp/floats.cpp
index fb9f1ac24bc75f..4ec00a2d677ede 100644
--- a/clang/test/AST/Interp/floats.cpp
+++ b/clang/test/AST/Interp/floats.cpp
@@ -145,3 +145,12 @@ namespace ZeroInit {
 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), "");
+}


        


More information about the cfe-commits mailing list