[Mlir-commits] [clang] [llvm] [mlir] [clang][APFloat] Print floating-point literals in the shortest round-trip form (PR #218471)
Joshua Cranmer
llvmlistbot at llvm.org
Mon Aug 24 11:55:55 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).
----------------
jcranmer-intel wrote:
I do agree that this is a terribly inefficient implementation; the best algorithm right now I think is Dragonbox: https://github.com/jk-jeon/dragonbox/blob/master/other_files/Dragonbox.pdf
(see also https://onlinelibrary.wiley.com/doi/epdf/10.1002/spe.70056 for empirical testing).
https://github.com/llvm/llvm-project/pull/218471
More information about the Mlir-commits
mailing list