[PATCH] D145277: [support] Support printing floats in ScopedPrinter

Paul Kirth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 3 15:27:47 PST 2023


paulkirth created this revision.
paulkirth added a reviewer: jhenderson.
Herald added a project: All.
paulkirth requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

llvm-readobj will need the ability to print floats for use in
HashHistograms. This adds that functionality to the ScopedPrinter and
JSONScopedPrinter.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145277

Files:
  llvm/include/llvm/Support/ScopedPrinter.h
  llvm/unittests/Support/ScopedPrinterTest.cpp


Index: llvm/unittests/Support/ScopedPrinterTest.cpp
===================================================================
--- llvm/unittests/Support/ScopedPrinterTest.cpp
+++ llvm/unittests/Support/ScopedPrinterTest.cpp
@@ -10,6 +10,7 @@
 #include "llvm/ADT/APSInt.h"
 #include "gtest/gtest.h"
 #include <vector>
+#include <cmath>
 
 using namespace llvm;
 
@@ -553,6 +554,15 @@
     W.printNumber("apsint", LargeNum);
 
     W.printNumber("label", "value", 0);
+
+    float MaxFloat = std::numeric_limits<float>::max();
+    float MinFloat = std::numeric_limits<float>::min();
+    float NaN = std::nanf("1");
+    W.printNumber("float-max", MaxFloat);
+    W.printNumber("float-min", MinFloat);
+    W.printNumber("float-nan", NaN);
+    W.printNumber("float-42.0", 42.0f);
+    W.printNumber("float-42.5625", 42.5625f);
   };
 
   const char *ExpectedOut = R"(uint64_t-max: 18446744073709551615
@@ -573,6 +583,11 @@
 int8_t-min: -128
 apsint: 9999999999999999999999
 label: value (0)
+float-max: 340282346638528859811704183484516925440.0
+float-min:   0.0
+float-nan:   nan
+float-42.0:  42.0
+float-42.5625:  42.6
 )";
 
   const char *JSONExpectedOut = R"({
@@ -596,7 +611,12 @@
   "label": {
     "Name": "value",
     "Value": 0
-  }
+  },
+  "float-max": 3.4028234663852886e+38,
+  "float-min": 1.1754943508222875e-38,
+  "float-nan": nan,
+  "float-42.0": 42,
+  "float-42.5625": 42.5625
 })";
   verifyAll(ExpectedOut, JSONExpectedOut, PrintFunc);
 }
Index: llvm/include/llvm/Support/ScopedPrinter.h
===================================================================
--- llvm/include/llvm/Support/ScopedPrinter.h
+++ llvm/include/llvm/Support/ScopedPrinter.h
@@ -234,6 +234,10 @@
     startLine() << Label << ": " << Value << "\n";
   }
 
+  virtual void printNumber(StringRef Label, float Value) {
+    startLine() << Label << ": " << format("%5.1f", Value) << "\n";
+  }
+
   template <typename T>
   void printNumber(StringRef Label, StringRef Str, T Value) {
     printNumberImpl(Label, Str, to_string(Value));
@@ -586,6 +590,10 @@
     JOS.attribute(Label, Value);
   }
 
+  void printNumber(StringRef Label, float Value) override {
+    JOS.attribute(Label, Value);
+  }
+
   void printNumber(StringRef Label, const APSInt &Value) override {
     JOS.attributeBegin(Label);
     printAPSInt(Value);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145277.502276.patch
Type: text/x-patch
Size: 2314 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230303/1574e267/attachment.bin>


More information about the llvm-commits mailing list