[PATCH] D91445: [APInt] Add the truncOrSelf resizing operator to APInt

Kerry McLaughlin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 23 03:40:33 PST 2020


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
kmclaughlin marked 2 inline comments as done.
Closed by commit rGd3a0f9b9ec88: [APInt] Add the truncOrSelf resizing operator to APInt (authored by kmclaughlin).

Changed prior to commit:
  https://reviews.llvm.org/D91445?vs=305212&id=307030#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91445/new/

https://reviews.llvm.org/D91445

Files:
  llvm/include/llvm/ADT/APInt.h
  llvm/lib/Support/APInt.cpp
  llvm/unittests/ADT/APIntTest.cpp


Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -2598,6 +2598,13 @@
   EXPECT_EQ(63U, i32_neg1.countPopulation());
 }
 
+TEST(APIntTest, truncOrSelf) {
+  APInt val(32, 0xFFFFFFFF);
+  EXPECT_EQ(0xFFFF, val.truncOrSelf(16));
+  EXPECT_EQ(0xFFFFFFFF, val.truncOrSelf(32));
+  EXPECT_EQ(0xFFFFFFFF, val.truncOrSelf(64));
+}
+
 TEST(APIntTest, multiply) {
   APInt i64(64, 1234);
 
Index: llvm/lib/Support/APInt.cpp
===================================================================
--- llvm/lib/Support/APInt.cpp
+++ llvm/lib/Support/APInt.cpp
@@ -961,6 +961,12 @@
   return *this;
 }
 
+APInt APInt::truncOrSelf(unsigned width) const {
+  if (BitWidth > width)
+    return trunc(width);
+  return *this;
+}
+
 APInt APInt::zextOrSelf(unsigned width) const {
   if (BitWidth < width)
     return zext(width);
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -1403,6 +1403,12 @@
   /// extended, truncated, or left alone to make it that width.
   APInt zextOrTrunc(unsigned width) const;
 
+  /// Truncate to width
+  ///
+  /// Make this APInt have the bit width given by \p width. The value is
+  /// truncated or left alone to make it that width.
+  APInt truncOrSelf(unsigned width) const;
+
   /// Sign extend or truncate to width
   ///
   /// Make this APInt have the bit width given by \p width. The value is sign


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91445.307030.patch
Type: text/x-patch
Size: 1584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201123/8e50f296/attachment.bin>


More information about the llvm-commits mailing list