[llvm-commits] [llvm] r92130 - /llvm/trunk/lib/Support/APFloat.cpp
John McCall
rjmccall at apple.com
Thu Dec 24 04:16:56 PST 2009
Author: rjmccall
Date: Thu Dec 24 06:16:56 2009
New Revision: 92130
URL: http://llvm.org/viewvc/llvm-project?rev=92130&view=rev
Log:
Substantially optimize APFloat::toString() by doing a single large divide to
cut the significand down to the desired precision *before* entering the
core divmod loop. Makes the overall algorithm logarithmic in the exponent.
There's still a lot of room for improvement here, but this gets the
performance back down to acceptable-for-diagnostics levels, even for
long doubles.
negligible, even on long doubles.
Modified:
llvm/trunk/lib/Support/APFloat.cpp
Modified: llvm/trunk/lib/Support/APFloat.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APFloat.cpp?rev=92130&r1=92129&r2=92130&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APFloat.cpp (original)
+++ llvm/trunk/lib/Support/APFloat.cpp Thu Dec 24 06:16:56 2009
@@ -3223,6 +3223,41 @@
append(Buffer, N, Str);
}
+ /// Removes data from the given significand until it is no more
+ /// precise than is required for the desired precision.
+ void AdjustToPrecision(APInt &significand,
+ int &exp, unsigned FormatPrecision) {
+ unsigned bits = significand.getActiveBits();
+
+ // 196/59 is a very slight overestimate of lg_2(10).
+ unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
+
+ if (bits <= bitsRequired) return;
+
+ unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
+ if (!tensRemovable) return;
+
+ exp += tensRemovable;
+
+ APInt divisor(significand.getBitWidth(), 1);
+ APInt powten(significand.getBitWidth(), 10);
+ while (true) {
+ if (tensRemovable & 1)
+ divisor *= powten;
+ tensRemovable >>= 1;
+ if (!tensRemovable) break;
+ powten *= powten;
+ }
+
+ significand = significand.udiv(divisor);
+
+ // Truncate the significand down to its active bit count, but
+ // don't try to drop below 32.
+ unsigned newPrecision = std::min(32U, significand.getActiveBits());
+ significand.trunc(newPrecision);
+ }
+
+
void AdjustToPrecision(SmallVectorImpl<char> &buffer,
int &exp, unsigned FormatPrecision) {
unsigned N = buffer.size();
@@ -3343,6 +3378,8 @@
}
}
+ AdjustToPrecision(significand, exp, FormatPrecision);
+
llvm::SmallVector<char, 256> buffer;
// Fill the buffer.
More information about the llvm-commits
mailing list