[PATCH] D147906: [clang-tidy] Avoid float compare in bugprone-incorrect-roundings

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 9 23:07:53 PDT 2023


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147906.512076.patch
Type: text/x-patch
Size: 1177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230410/c0360dc7/attachment.bin>


More information about the cfe-commits mailing list