[llvm] 366ae9c - [APInt] byteSwap - handle any whole byte bitwidth greater than 16-bits

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 15 05:28:09 PST 2020


Author: Simon Pilgrim
Date: 2020-02-15T13:27:06Z
New Revision: 366ae9c90fe6d3a590c0a0d4283c019f11799516

URL: https://github.com/llvm/llvm-project/commit/366ae9c90fe6d3a590c0a0d4283c019f11799516
DIFF: https://github.com/llvm/llvm-project/commit/366ae9c90fe6d3a590c0a0d4283c019f11799516.diff

LOG: [APInt] byteSwap - handle any whole byte bitwidth greater than 16-bits

As noted on D74621, the bswap intrinsic has a self imposed limitation that the type's bitwidth must be divisible by 16, but there's no reason that APInt::byteSwap must have the same limitation, given that it can already handle any byte width.

Added: 
    

Modified: 
    llvm/lib/Support/APInt.cpp
    llvm/unittests/ADT/APIntTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 3a5fada6cf66..faef9a31a0ed 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -670,7 +670,7 @@ bool APInt::isSubsetOfSlowCase(const APInt &RHS) const {
 }
 
 APInt APInt::byteSwap() const {
-  assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
+  assert(BitWidth >= 16 && BitWidth % 8 == 0 && "Cannot byteswap!");
   if (BitWidth == 16)
     return APInt(BitWidth, ByteSwap_16(uint16_t(U.VAL)));
   if (BitWidth == 32)

diff  --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 34efbc524f94..0efb10f9b32d 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1818,11 +1818,15 @@ TEST(APIntTest, SelfMoveAssignment) {
 TEST(APIntTest, byteSwap) {
   EXPECT_EQ(0x00000000, APInt(16, 0x0000).byteSwap());
   EXPECT_EQ(0x0000010f, APInt(16, 0x0f01).byteSwap());
+  EXPECT_EQ(0x00ff8000, APInt(24, 0x0080ff).byteSwap());
   EXPECT_EQ(0x117700ff, APInt(32, 0xff007711).byteSwap());
+  EXPECT_EQ(0x228811aaffULL, APInt(40, 0xffaa118822ULL).byteSwap());
   EXPECT_EQ(0x050403020100ULL, APInt(48, 0x000102030405ULL).byteSwap());
+  EXPECT_EQ(0xff050403020100ULL, APInt(56, 0x000102030405ffULL).byteSwap());
   EXPECT_EQ(0xff050403020100aaULL, APInt(64, 0xaa000102030405ffULL).byteSwap());
 
-  for (unsigned N : {16, 32, 48, 64, 80, 96, 112, 128, 256, 1024, 1040}) {
+  for (unsigned N : {16, 24, 32, 48, 56, 64, 72, 80, 96, 112, 128, 248, 256,
+                     1024, 1032, 1040}) {
     for (unsigned I = 0; I < N; I += 8) {
       APInt X = APInt::getBitsSet(N, I, I + 8);
       APInt Y = APInt::getBitsSet(N, N - I - 8, N - I);


        


More information about the llvm-commits mailing list