Skip to content

Commit 7b72eb2

Browse files
committed
[ADT] Add fshl/fshr operations to APInt
1 parent 6b316ec commit 7b72eb2

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

llvm/include/llvm/ADT/APInt.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,35 @@ LLVM_ABI std::optional<unsigned> GetMostSignificantDifferentBit(const APInt &A,
24102410
/// A.getBitwidth() or NewBitWidth must be a whole multiples of the other.
24112411
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth,
24122412
bool MatchAllBits = false);
2413+
2414+
/// Perform a funnel shift left.
2415+
///
2416+
/// Concatenate Hi and Lo (Hi is the most significant bits of the wide value),
2417+
/// the combined value is shifted left by Shift (modulo the bit width of the
2418+
/// original arguments), and the most significant bits are extracted to produce
2419+
/// a result that is the same size as the original arguments.
2420+
///
2421+
/// Examples:
2422+
/// (1) fshl(i8 255, i8 0, i8 15) = 128 (0b10000000)
2423+
/// (2) fshl(i8 15, i8 15, i8 11) = 120 (0b01111000)
2424+
/// (3) fshl(i8 0, i8 255, i8 8) = 0 (0b00000000)
2425+
/// (4) fshl(i8 255, i8 0, i8 15) = fshl(i8 255, i8 0, i8 7) // 15 % 8
2426+
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift);
2427+
2428+
/// Perform a funnel shift right.
2429+
///
2430+
/// Concatenate Hi and Lo (Hi is the most significant bits of the wide value),
2431+
/// the combined value is shifted right by Shift (modulo the bit width of the
2432+
/// original arguments), and the least significant bits are extracted to produce
2433+
/// a result that is the same size as the original arguments.
2434+
///
2435+
/// Examples:
2436+
/// (1) fshr(i8 255, i8 0, i8 15) = 254 (0b11111110)
2437+
/// (2) fshr(i8 15, i8 15, i8 11) = 225 (0b11100001)
2438+
/// (3) fshr(i8 0, i8 255, i8 8) = 255 (0b11111111)
2439+
/// (4) fshr(i8 255, i8 0, i8 9) = fshr(i8 255, i8 0, i8 1) // 9 % 8
2440+
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift);
2441+
24132442
} // namespace APIntOps
24142443

24152444
// See friend declaration above. This additional declaration is required in

llvm/lib/Support/APInt.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,3 +3169,21 @@ APInt APIntOps::pow(const APInt &X, int64_t N) {
31693169
}
31703170
return Acc;
31713171
}
3172+
3173+
APInt llvm::APIntOps::fshl(const APInt &Hi, const APInt &Lo,
3174+
const APInt &Shift) {
3175+
assert(Hi.getBitWidth() == Lo.getBitWidth());
3176+
unsigned ShiftAmt = rotateModulo(Hi.getBitWidth(), Shift);
3177+
if (ShiftAmt == 0)
3178+
return Hi;
3179+
return APInt(Hi.shl(ShiftAmt) | Lo.lshr(Hi.getBitWidth() - ShiftAmt));
3180+
}
3181+
3182+
APInt llvm::APIntOps::fshr(const APInt &Hi, const APInt &Lo,
3183+
const APInt &Shift) {
3184+
assert(Hi.getBitWidth() == Lo.getBitWidth());
3185+
unsigned ShiftAmt = rotateModulo(Hi.getBitWidth(), Shift);
3186+
if (ShiftAmt == 0)
3187+
return Lo;
3188+
return APInt(Hi.shl(Hi.getBitWidth() - ShiftAmt) | Lo.lshr(ShiftAmt));
3189+
}

llvm/unittests/ADT/APIntTest.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,4 +3744,80 @@ TEST(APIntTest, TryExt) {
37443744
ASSERT_EQ(42, APInt(128, -1).trySExtValue().value_or(42));
37453745
}
37463746

3747+
TEST(APIntTest, Fshl) {
3748+
EXPECT_EQ(
3749+
APIntOps::fshl(APInt(8, 0), APInt(8, 255), APInt(8, 8)).getZExtValue(),
3750+
0);
3751+
EXPECT_EQ(
3752+
APIntOps::fshl(APInt(8, 255), APInt(8, 0), APInt(8, 8)).getZExtValue(),
3753+
255);
3754+
EXPECT_EQ(
3755+
APIntOps::fshl(APInt(8, 255), APInt(8, 0), APInt(8, 15)).getZExtValue(),
3756+
128);
3757+
EXPECT_EQ(
3758+
APIntOps::fshl(APInt(8, 15), APInt(8, 15), APInt(8, 11)).getZExtValue(),
3759+
120);
3760+
EXPECT_EQ(
3761+
APIntOps::fshl(APInt(8, 2), APInt(8, 1), APInt(8, 3)).getZExtValue(), 16);
3762+
EXPECT_EQ(
3763+
APIntOps::fshl(APInt(8, 2), APInt(8, 1), APInt(8, 1)).getZExtValue(),
3764+
APIntOps::fshl(APInt(8, 2), APInt(8, 1), APInt(8, 9)).getZExtValue());
3765+
EXPECT_EQ(
3766+
APIntOps::fshl(APInt(8, 2), APInt(8, 1), APInt(8, 7)).getZExtValue(),
3767+
APIntOps::fshl(APInt(8, 2), APInt(8, 1), APInt(8, 15)).getZExtValue());
3768+
EXPECT_EQ(APIntOps::fshl(APInt(32, 0, /*isSigned*/ true),
3769+
APInt(32, 2147483647, /*isSigned*/ true),
3770+
APInt(32, 32, /*isSigned*/ true))
3771+
.getSExtValue(),
3772+
0);
3773+
EXPECT_EQ(APIntOps::fshl(APInt(64, 1, /*isSigned*/ true),
3774+
APInt(64, 2, /*isSigned*/ true),
3775+
APInt(64, 3, /*isSigned*/ true))
3776+
.getSExtValue(),
3777+
8);
3778+
EXPECT_EQ(APIntOps::fshl(APInt(16, -2, /*isSigned*/ true),
3779+
APInt(16, -1, /*isSigned*/ true),
3780+
APInt(16, 3, /*isSigned*/ true))
3781+
.getSExtValue(),
3782+
-9);
3783+
}
3784+
3785+
TEST(APIntTest, Fshr) {
3786+
EXPECT_EQ(
3787+
APIntOps::fshr(APInt(8, 0), APInt(8, 255), APInt(8, 8)).getZExtValue(),
3788+
255);
3789+
EXPECT_EQ(
3790+
APIntOps::fshr(APInt(8, 255), APInt(8, 0), APInt(8, 8)).getZExtValue(),
3791+
0);
3792+
EXPECT_EQ(
3793+
APIntOps::fshr(APInt(8, 255), APInt(8, 0), APInt(8, 15)).getZExtValue(),
3794+
254);
3795+
EXPECT_EQ(
3796+
APIntOps::fshr(APInt(8, 15), APInt(8, 15), APInt(8, 11)).getZExtValue(),
3797+
225);
3798+
EXPECT_EQ(
3799+
APIntOps::fshr(APInt(8, 1), APInt(8, 2), APInt(8, 3)).getZExtValue(), 32);
3800+
EXPECT_EQ(
3801+
APIntOps::fshr(APInt(8, 1), APInt(8, 2), APInt(8, 1)).getZExtValue(),
3802+
APIntOps::fshr(APInt(8, 1), APInt(8, 2), APInt(8, 9)).getZExtValue());
3803+
EXPECT_EQ(
3804+
APIntOps::fshr(APInt(8, 1), APInt(8, 2), APInt(8, 7)).getZExtValue(),
3805+
APIntOps::fshr(APInt(8, 1), APInt(8, 2), APInt(8, 15)).getZExtValue());
3806+
EXPECT_EQ(APIntOps::fshr(APInt(64, 0, /*isSigned*/ true),
3807+
APInt(64, 9223372036854775807, /*isSigned*/ true),
3808+
APInt(64, 64, /*isSigned*/ true))
3809+
.getSExtValue(),
3810+
9223372036854775807);
3811+
EXPECT_EQ(APIntOps::fshr(APInt(64, 1, /*isSigned*/ true),
3812+
APInt(64, 2, /*isSigned*/ true),
3813+
APInt(64, 3, /*isSigned*/ true))
3814+
.getSExtValue(),
3815+
2305843009213693952);
3816+
EXPECT_EQ(APIntOps::fshr(APInt(16, -2, /*isSigned*/ true),
3817+
APInt(16, -1, /*isSigned*/ true),
3818+
APInt(16, 3, /*isSigned*/ true))
3819+
.getSExtValue(),
3820+
-8193);
3821+
}
3822+
37473823
} // end anonymous namespace

0 commit comments

Comments
 (0)