r339026 - [Fixed Point Arithmetic] Fix for FixedPointValueToString

Leonard Chan via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 6 09:05:08 PDT 2018


Author: leonardchan
Date: Mon Aug  6 09:05:08 2018
New Revision: 339026

URL: http://llvm.org/viewvc/llvm-project?rev=339026&view=rev
Log:
[Fixed Point Arithmetic] Fix for FixedPointValueToString

- 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

Added:
    cfe/trunk/test/Frontend/fixed_point_to_string.c
    cfe/trunk/unittests/Frontend/FixedPointString.cpp
Modified:
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/lib/AST/Type.cpp
    cfe/trunk/unittests/Frontend/CMakeLists.txt

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=339026&r1=339025&r2=339026&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Mon Aug  6 09:05:08 2018
@@ -6604,9 +6604,8 @@ QualType DecayedType::getPointeeType() c
 
 // 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
 

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=339026&r1=339025&r2=339026&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Aug  6 09:05:08 2018
@@ -785,7 +785,7 @@ std::string FixedPointLiteral::getValueA
   // 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();
 }
 

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=339026&r1=339025&r2=339026&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Aug  6 09:05:08 2018
@@ -9698,8 +9698,7 @@ bool FixedPointExprEvaluator::VisitUnary
       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;
       }

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=339026&r1=339025&r2=339026&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Mon Aug  6 09:05:08 2018
@@ -4032,17 +4032,26 @@ CXXRecordDecl *MemberPointerType::getMos
 }
 
 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);
+                                    llvm::APSInt Val, unsigned Scale) {
+  if (Val.isSigned() && Val.isNegative() && Val != -Val) {
+    Val = -Val;
+    Str.push_back('-');
+  }
 
-  IntPart.toString(Str, Radix);
+  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);
 }

Added: cfe/trunk/test/Frontend/fixed_point_to_string.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/fixed_point_to_string.c?rev=339026&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/fixed_point_to_string.c (added)
+++ cfe/trunk/test/Frontend/fixed_point_to_string.c Mon Aug  6 09:05:08 2018
@@ -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

Modified: cfe/trunk/unittests/Frontend/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Frontend/CMakeLists.txt?rev=339026&r1=339025&r2=339026&view=diff
==============================================================================
--- cfe/trunk/unittests/Frontend/CMakeLists.txt (original)
+++ cfe/trunk/unittests/Frontend/CMakeLists.txt Mon Aug  6 09:05:08 2018
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
 add_clang_unittest(FrontendTests
   ASTUnitTest.cpp
   CompilerInstanceTest.cpp
+  FixedPointString.cpp
   FrontendActionTest.cpp
   CodeGenActionTest.cpp
   ParsedSourceLocationTest.cpp

Added: cfe/trunk/unittests/Frontend/FixedPointString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Frontend/FixedPointString.cpp?rev=339026&view=auto
==============================================================================
--- cfe/trunk/unittests/Frontend/FixedPointString.cpp (added)
+++ cfe/trunk/unittests/Frontend/FixedPointString.cpp Mon Aug  6 09:05:08 2018
@@ -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




More information about the cfe-commits mailing list