]> granicus.if.org Git - clang/commitdiff
[Fixed Point Arithmetic] Fix for FixedPointValueToString
authorLeonard Chan <leonardchan@google.com>
Mon, 6 Aug 2018 16:05:08 +0000 (16:05 +0000)
committerLeonard Chan <leonardchan@google.com>
Mon, 6 Aug 2018 16:05:08 +0000 (16:05 +0000)
- Print negative numbers correctly
- Handle APInts of different sizes
- Add formal unit tests for FixedPointValueToString
- Add tests for checking correct printing when padding is set
- Restrict to printing in radix 10 since that's all we need for now

Differential Revision: https://reviews.llvm.org/D49945

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@339026 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Type.h
lib/AST/Expr.cpp
lib/AST/ExprConstant.cpp
lib/AST/Type.cpp
test/Frontend/fixed_point_to_string.c [new file with mode: 0644]
unittests/Frontend/CMakeLists.txt
unittests/Frontend/FixedPointString.cpp [new file with mode: 0644]

index e89aefb8771170692b1a00c367851f40a34dd5bf..cda84b19fa9bbd72cb3e00442ebb46afedd67100 100644 (file)
@@ -6604,9 +6604,8 @@ QualType DecayedType::getPointeeType() const {
 
 // Get the decimal string representation of a fixed point type, represented
 // as a scaled integer.
-void FixedPointValueToString(SmallVectorImpl<char> &Str,
-                             const llvm::APSInt &Val,
-                             unsigned Scale, unsigned Radix);
+void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
+                             unsigned Scale);
 
 } // namespace clang
 
index 7556c76c38bda829ca70a7bf0ca1edcbc28786ab..69adcbc0d5577be22db65bd49054d5b240cd0fdc 100644 (file)
@@ -785,7 +785,7 @@ std::string FixedPointLiteral::getValueAsString(unsigned Radix) const {
   // which is 43 characters.
   SmallString<64> S;
   FixedPointValueToString(
-      S, llvm::APSInt::getUnsigned(getValue().getZExtValue()), Scale, Radix);
+      S, llvm::APSInt::getUnsigned(getValue().getZExtValue()), Scale);
   return S.str();
 }
 
index 222ff74aaacfcefb80f71e4230f531046582389f..7e596d0715c313048ce1b3e76f4fdf0a7b16d337 100644 (file)
@@ -9698,8 +9698,7 @@ bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
       if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow()) {
         SmallString<64> S;
         FixedPointValueToString(S, Value,
-                                Info.Ctx.getTypeInfo(E->getType()).Width,
-                                /*Radix=*/10);
+                                Info.Ctx.getTypeInfo(E->getType()).Width);
         Info.CCEDiag(E, diag::note_constexpr_overflow) << S << E->getType();
         if (Info.noteUndefinedBehavior()) return false;
       }
index f79a59712a4100fc43accdce3d29bbd74a8edb16..315844fa42489779a57c56b3ebbedc3cf8d4322b 100644 (file)
@@ -4032,17 +4032,26 @@ CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const {
 }
 
 void clang::FixedPointValueToString(SmallVectorImpl<char> &Str,
-                                    const llvm::APSInt &Val, unsigned Scale,
-                                    unsigned Radix) {
-  llvm::APSInt ScaleVal = llvm::APSInt::getUnsigned(1ULL << Scale);
-  llvm::APSInt IntPart = Val / ScaleVal;
-  llvm::APSInt FractPart = Val % ScaleVal;
-  llvm::APSInt RadixInt = llvm::APSInt::getUnsigned(Radix);
-
-  IntPart.toString(Str, Radix);
+                                    llvm::APSInt Val, unsigned Scale) {
+  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 / ScaleVal).toString(Str, Radix);
-    FractPart = (FractPart * RadixInt) % ScaleVal;
-  } while (FractPart.getExtValue());
+    (FractPart * RadixInt)
+        .lshr(Scale)
+        .toString(Str, /*radix=*/10, Val.isSigned());
+    FractPart = (FractPart * RadixInt) & FractPartMask;
+  } while (FractPart != 0);
 }
diff --git a/test/Frontend/fixed_point_to_string.c b/test/Frontend/fixed_point_to_string.c
new file mode 100644 (file)
index 0000000..ad71d10
--- /dev/null
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -ast-dump -ffixed-point %s | FileCheck %s
+// RUN: %clang_cc1 -ast-dump -ffixed-point -fpadding-on-unsigned-fixed-point %s | FileCheck %s
+
+/**
+ * Check the same values are printed in the AST regardless of if unsigned types
+ * have the same number of fractional bits as signed types.
+ */
+
+unsigned short _Accum u_short_accum = 0.5uhk;
+unsigned _Accum u_accum = 0.5uk;
+unsigned long _Accum u_long_accum = 0.5ulk;
+unsigned short _Fract u_short_fract = 0.5uhr;
+unsigned _Fract u_fract = 0.5ur;
+unsigned long _Fract u_long_fract = 0.5ulr;
+
+//CHECK: FixedPointLiteral {{.*}} 'unsigned short _Accum' 0.5
+//CHECK: FixedPointLiteral {{.*}} 'unsigned _Accum' 0.5
+//CHECK: FixedPointLiteral {{.*}} 'unsigned long _Accum' 0.5
+//CHECK: FixedPointLiteral {{.*}} 'unsigned short _Fract' 0.5
+//CHECK: FixedPointLiteral {{.*}} 'unsigned _Fract' 0.5
+//CHECK: FixedPointLiteral {{.*}} 'unsigned long _Fract' 0.5
index c764fb9e2a0f2d3201207455aae4efbb4e241d58..e36570a88bffcd15bbf3b98b5e21cb4180acbc0d 100644 (file)
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
 add_clang_unittest(FrontendTests
   ASTUnitTest.cpp
   CompilerInstanceTest.cpp
+  FixedPointString.cpp
   FrontendActionTest.cpp
   CodeGenActionTest.cpp
   ParsedSourceLocationTest.cpp
diff --git a/unittests/Frontend/FixedPointString.cpp b/unittests/Frontend/FixedPointString.cpp
new file mode 100644 (file)
index 0000000..755efd7
--- /dev/null
@@ -0,0 +1,107 @@
+#include "clang/AST/Type.h"
+#include "llvm/ADT/APSInt.h"
+#include "llvm/ADT/SmallString.h"
+#include "gtest/gtest.h"
+
+using clang::FixedPointValueToString;
+using llvm::APSInt;
+using llvm::SmallString;
+
+namespace {
+
+TEST(FixedPointString, DifferentTypes) {
+  SmallString<64> S;
+  FixedPointValueToString(S, APSInt::get(320), 7);
+  ASSERT_STREQ(S.c_str(), "2.5");
+
+  S.clear();
+  FixedPointValueToString(S, APSInt::get(0), 7);
+  ASSERT_STREQ(S.c_str(), "0.0");
+
+  // signed short _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(16, /*Unsigned=*/false), 7);
+  ASSERT_STREQ(S.c_str(), "255.9921875");
+
+  // signed _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(32, /*Unsigned=*/false), 15);
+  ASSERT_STREQ(S.c_str(), "65535.999969482421875");
+
+  // signed long _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(64, /*Unsigned=*/false), 31);
+  ASSERT_STREQ(S.c_str(), "4294967295.9999999995343387126922607421875");
+
+  // unsigned short _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(16, /*Unsigned=*/true), 8);
+  ASSERT_STREQ(S.c_str(), "255.99609375");
+
+  // unsigned _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(32, /*Unsigned=*/true), 16);
+  ASSERT_STREQ(S.c_str(), "65535.9999847412109375");
+
+  // unsigned long _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(64, /*Unsigned=*/true), 32);
+  ASSERT_STREQ(S.c_str(), "4294967295.99999999976716935634613037109375");
+
+  // signed short _Fract
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(8, /*Unsigned=*/false), 7);
+  ASSERT_STREQ(S.c_str(), "0.9921875");
+
+  // signed _Fract
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(16, /*Unsigned=*/false), 15);
+  ASSERT_STREQ(S.c_str(), "0.999969482421875");
+
+  // signed long _Fract
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(32, /*Unsigned=*/false), 31);
+  ASSERT_STREQ(S.c_str(), "0.9999999995343387126922607421875");
+
+  // unsigned short _Fract
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(8, /*Unsigned=*/true), 8);
+  ASSERT_STREQ(S.c_str(), "0.99609375");
+
+  // unsigned _Fract
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(16, /*Unsigned=*/true), 16);
+  ASSERT_STREQ(S.c_str(), "0.9999847412109375");
+
+  // unsigned long _Fract
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMaxValue(32, /*Unsigned=*/true), 32);
+  ASSERT_STREQ(S.c_str(), "0.99999999976716935634613037109375");
+}
+
+TEST(FixedPointString, Negative) {
+  SmallString<64> S;
+  FixedPointValueToString(S, APSInt::get(-320), 7);
+  ASSERT_STREQ(S.c_str(), "-2.5");
+
+  S.clear();
+  FixedPointValueToString(S, APSInt::get(-64), 7);
+  ASSERT_STREQ(S.c_str(), "-0.5");
+
+  // signed short _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMinValue(16, /*Unsigned=*/false), 7);
+  ASSERT_STREQ(S.c_str(), "-256.0");
+
+  // signed _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMinValue(32, /*Unsigned=*/false), 15);
+  ASSERT_STREQ(S.c_str(), "-65536.0");
+
+  // signed long _Accum
+  S.clear();
+  FixedPointValueToString(S, APSInt::getMinValue(64, /*Unsigned=*/false), 31);
+  ASSERT_STREQ(S.c_str(), "-4294967296.0");
+}
+
+} // namespace