[Mlir-commits] [clang] [llvm] [mlir] [clang][APFloat] Print floating-point literals in the shortest round-trip form (PR #218471)

Emery Conrad llvmlistbot at llvm.org
Mon Aug 24 11:50:03 PDT 2026


================
@@ -5973,6 +5974,43 @@ APFloat APFloat::getAllOnesValue(const fltSemantics &Semantics) {
   return APFloat(Semantics, APInt::getAllOnes(Semantics.sizeInBits));
 }
 
+bool APFloat::toStringRoundTrip(SmallVectorImpl<char> &Str,
+                                unsigned FormatPrecision,
+                                unsigned FormatMaxPadding,
+                                bool TruncateZero) const {
+  SmallString<32> Buf;
+  toString(Buf, FormatPrecision, FormatMaxPadding, TruncateZero);
+  Str.append(Buf.begin(), Buf.end());
+  APFloat Parsed(getSemantics());
+  Expected<opStatus> Status =
+      Parsed.convertFromString(Buf, rmNearestTiesToEven);
+  if (!Status) {
+    consumeError(Status.takeError());
+    return false;
+  }
+  return Parsed.bitwiseIsEqual(*this);
+}
+
+void APFloat::toStringShortest(SmallVectorImpl<char> &Str,
+                               unsigned FormatMaxPadding,
+                               bool TruncateZero) const {
+  SmallString<32> Best;
+  toString(Best, /*FormatPrecision=*/0, FormatMaxPadding, TruncateZero);
+  if (isFiniteNonZero()) {
+    // Probe below the conservative natural precision (toStringImpl's FIXME).
----------------
conrade-ctc wrote:

looking at paper now, maybe worth updating since it's in a core util that might be re-used :)

https://github.com/llvm/llvm-project/pull/218471


More information about the Mlir-commits mailing list