summaryrefslogtreecommitdiffstats
path: root/lib/Basic/FixedPoint.cpp
diff options
context:
space:
mode:
authorLeonard Chan <leonardchan@google.com>2019-01-16 18:53:05 +0000
committerLeonard Chan <leonardchan@google.com>2019-01-16 18:53:05 +0000
commitbfe5a44dde6243818898e7cfada316d762c59742 (patch)
treeb79a62013085e5940e5d1a745ab1781062d45a82 /lib/Basic/FixedPoint.cpp
parentad18a0d69778c7a02e398967aafd92318fc50a58 (diff)
[Fixed Point Arithmetic] Add APFixedPoint to APValue
This adds APFixedPoint to the union of values that can be represented with an APValue. Differential Revision: https://reviews.llvm.org/D56746 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351368 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Basic/FixedPoint.cpp')
-rw-r--r--lib/Basic/FixedPoint.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/Basic/FixedPoint.cpp b/lib/Basic/FixedPoint.cpp
index 0aaa9af191..e3cffe628f 100644
--- a/lib/Basic/FixedPoint.cpp
+++ b/lib/Basic/FixedPoint.cpp
@@ -137,4 +137,31 @@ FixedPointSemantics FixedPointSemantics::getCommonSemantics(
ResultIsSaturated, ResultHasUnsignedPadding);
}
+void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const {
+ llvm::APSInt Val = getValue();
+ unsigned Scale = getScale();
+
+ if (Val.isSigned() && Val.isNegative() && Val != -Val) {
+ Val = -Val;
+ Str.push_back('-');
+ }
+
+ llvm::APSInt IntPart = Val >> Scale;
+
+ // Add 4 digits to hold the value after multiplying 10 (the radix)
+ unsigned Width = Val.getBitWidth() + 4;
+ llvm::APInt FractPart = Val.zextOrTrunc(Scale).zext(Width);
+ llvm::APInt FractPartMask = llvm::APInt::getAllOnesValue(Scale).zext(Width);
+ llvm::APInt RadixInt = llvm::APInt(Width, 10);
+
+ IntPart.toString(Str, /*radix=*/10);
+ Str.push_back('.');
+ do {
+ (FractPart * RadixInt)
+ .lshr(Scale)
+ .toString(Str, /*radix=*/10, Val.isSigned());
+ FractPart = (FractPart * RadixInt) & FractPartMask;
+ } while (FractPart != 0);
+}
+
} // namespace clang