[PATCH] D135562: [ADT] Fix Incorrect rounding for APFixedPoint::getIntPart

Tyker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 10 01:38:43 PDT 2022


Tyker created this revision.
Tyker added reviewers: leonardchan, uabelho.
Herald added a project: All.
Tyker requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

https://reviews.llvm.org/D135562

Files:
  llvm/include/llvm/ADT/APFixedPoint.h
  llvm/unittests/ADT/APFixedPointTest.cpp


Index: llvm/unittests/ADT/APFixedPointTest.cpp
===================================================================
--- llvm/unittests/ADT/APFixedPointTest.cpp
+++ llvm/unittests/ADT/APFixedPointTest.cpp
@@ -276,6 +276,11 @@
                                     APSInt::getUnsigned(Expected)) == 0);
 }
 
+void CheckIntPartRes(const FixedPointSemantics &Sema, int64_t Representation, int64_t Result) {
+  APFixedPoint Val(Representation, Sema);
+  ASSERT_EQ(Val.getIntPart().getZExtValue(), Result) ;
+}
+
 TEST(FixedPoint, getIntPart) {
   // Normal values
   CheckIntPart(getSAccumSema(), 2);
@@ -359,6 +364,12 @@
   CheckIntPartMax(getPadUSFractSema(), 0);
   CheckIntPartMax(getPadUFractSema(), 0);
   CheckIntPartMax(getPadULFractSema(), 0);
+
+  // Rounded Towards Zero
+  CheckIntPartRes(getSFractSema(), -127, 0);
+  CheckIntPartRes(getFractSema(), -32767, 0);
+  CheckIntPartRes(getLFractSema(), -2147483647, 0);
+  CheckIntPartRes(getS16Neg18(), -32768, 0);
 }
 
 TEST(FixedPoint, compare) {
Index: llvm/include/llvm/ADT/APFixedPoint.h
===================================================================
--- llvm/include/llvm/ADT/APFixedPoint.h
+++ llvm/include/llvm/ADT/APFixedPoint.h
@@ -214,7 +214,7 @@
     APSInt ExtVal =
         (getLsbWeight() > 0) ? Val.extend(getWidth() + getLsbWeight()) : Val;
     if (Val < 0 && Val != -Val) // Cover the case when we have the min val
-      return -(-ExtVal.relativeShl(getLsbWeight()));
+      return -((-ExtVal).relativeShl(getLsbWeight()));
     return ExtVal.relativeShl(getLsbWeight());
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135562.466450.patch
Type: text/x-patch
Size: 1556 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221010/03e894d7/attachment.bin>


More information about the llvm-commits mailing list