PiotrZSL created this revision.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Using APFloat to compare floating numbers instead of float/double.

Fixes: 46424


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147906

Files:
  clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp


Index: clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp
@@ -17,13 +17,21 @@
 
 namespace clang::tidy::bugprone {
 
+static llvm::APFloat getHalf(const llvm::fltSemantics &Semantics) {
+  return llvm::APFloat(Semantics, 1U) / llvm::APFloat(Semantics, 2U);
+}
+
 namespace {
 AST_MATCHER(FloatingLiteral, floatHalf) {
   const auto &Literal = Node.getValue();
-  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle())
-    return Literal.convertToFloat() == 0.5f;
-  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble())
-    return Literal.convertToDouble() == 0.5;
+  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle()) {
+    static llvm::APFloat Half = getHalf(llvm::APFloat::IEEEsingle());
+    return Literal == Half;
+  }
+  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble()) {
+    static llvm::APFloat Half = getHalf(llvm::APFloat::IEEEdouble());
+    return Literal == Half;
+  }
   return false;
 }
 } // namespace


Index: clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp
@@ -17,13 +17,21 @@
 
 namespace clang::tidy::bugprone {
 
+static llvm::APFloat getHalf(const llvm::fltSemantics &Semantics) {
+  return llvm::APFloat(Semantics, 1U) / llvm::APFloat(Semantics, 2U);
+}
+
 namespace {
 AST_MATCHER(FloatingLiteral, floatHalf) {
   const auto &Literal = Node.getValue();
-  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle())
-    return Literal.convertToFloat() == 0.5f;
-  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble())
-    return Literal.convertToDouble() == 0.5;
+  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle()) {
+    static llvm::APFloat Half = getHalf(llvm::APFloat::IEEEsingle());
+    return Literal == Half;
+  }
+  if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble()) {
+    static llvm::APFloat Half = getHalf(llvm::APFloat::IEEEdouble());
+    return Literal == Half;
+  }
   return false;
 }
 } // namespace
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to