[APInt] byteSwap - handle any whole byte bitwidth greater than 16-bits
authorSimon Pilgrim <llvm-dev@redking.me.uk>
Sat, 15 Feb 2020 13:27:06 +0000 (13:27 +0000)
committerSimon Pilgrim <llvm-dev@redking.me.uk>
Sat, 15 Feb 2020 13:27:06 +0000 (13:27 +0000)
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.

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

index 3a5fada..faef9a3 100644 (file)
@@ -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)
index 34efbc5..0efb10f 100644 (file)
@@ -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);