[llvm] [ADT] Add implementations for avgFloor and avgCeil to APInt (PR #84431)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 11 22:40:56 PDT 2024
================
@@ -2877,6 +2878,73 @@ TEST(APIntTest, RoundingSDiv) {
}
}
+TEST(APIntTest, Average) {
+ APInt Ap0(32, 0);
+ APInt A2(32, 2);
+ APInt A100(32, 100);
+ APInt A101(32, 101);
+ APInt A200(32, 200, false);
+ APInt ApUMax(32, UINT_MAX, false);
+
+ EXPECT_EQ(APInt(32, 150), APIntOps::avgFloorU(A100, A200));
+ EXPECT_EQ(APIntOps::RoundingUDiv(A100 + A200, A2, APInt::Rounding::DOWN),
+ APIntOps::avgFloorU(A100, A200));
+ EXPECT_EQ(APIntOps::RoundingUDiv(A100 + A200, A2, APInt::Rounding::UP),
+ APIntOps::avgCeilU(A100, A200));
+ EXPECT_EQ(APIntOps::RoundingUDiv(A100 + A101, A2, APInt::Rounding::DOWN),
+ APIntOps::avgFloorU(A100, A101));
+ EXPECT_EQ(APIntOps::RoundingUDiv(A100 + A101, A2, APInt::Rounding::UP),
+ APIntOps::avgCeilU(A100, A101));
+ EXPECT_EQ(Ap0, APIntOps::avgFloorU(Ap0, Ap0));
+ EXPECT_EQ(Ap0, APIntOps::avgCeilU(Ap0, Ap0));
+ EXPECT_EQ(ApUMax, APIntOps::avgFloorU(ApUMax, ApUMax));
+ EXPECT_EQ(ApUMax, APIntOps::avgCeilU(ApUMax, ApUMax));
+
+ APInt Ap100(32, +100);
+ APInt Ap101(32, +101);
+ APInt Ap200(32, +200);
+ APInt Am100(32, -100);
+ APInt Am101(32, -101);
+ APInt Am200(32, -200);
+ APInt AmSMin(32, INT_MIN);
+ APInt ApSMax(32, INT_MAX);
+
+ EXPECT_EQ(APInt(32, +150), APIntOps::avgFloorS(Ap100, Ap200));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Ap100 + Ap200, A2, APInt::Rounding::DOWN),
+ APIntOps::avgFloorS(Ap100, Ap200));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Ap100 + Ap200, A2, APInt::Rounding::UP),
+ APIntOps::avgCeilS(Ap100, Ap200));
+
+ EXPECT_EQ(APInt(32, -150), APIntOps::avgFloorS(Am100, Am200));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Am100 + Am200, A2, APInt::Rounding::DOWN),
+ APIntOps::avgFloorS(Am100, Am200));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Am100 + Am200, A2, APInt::Rounding::UP),
+ APIntOps::avgCeilS(Am100, Am200));
+
+ EXPECT_EQ(APInt(32, +100), APIntOps::avgFloorS(Ap100, Ap101));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Ap100 + Ap101, A2, APInt::Rounding::DOWN),
+ APIntOps::avgFloorS(Ap100, Ap101));
+ EXPECT_EQ(APInt(32, +101), APIntOps::avgCeilS(Ap100, Ap101));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Ap100 + Ap101, A2, APInt::Rounding::UP),
+ APIntOps::avgCeilS(Ap100, Ap101));
+
+ EXPECT_EQ(APInt(32, -101), APIntOps::avgFloorS(Am100, Am101));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Am100 + Am101, A2, APInt::Rounding::DOWN),
+ APIntOps::avgFloorS(Am100, Am101));
+ EXPECT_EQ(APInt(32, -100), APIntOps::avgCeilS(Am100, Am101));
+ EXPECT_EQ(APIntOps::RoundingSDiv(Am100 + Am101, A2, APInt::Rounding::UP),
+ APIntOps::avgCeilS(Am100, Am101));
+
+ EXPECT_EQ(AmSMin, APIntOps::avgFloorS(AmSMin, AmSMin));
+ EXPECT_EQ(AmSMin, APIntOps::avgCeilS(AmSMin, AmSMin));
----------------
arsenm wrote:
min / max / 0 cases?
https://github.com/llvm/llvm-project/pull/84431
More information about the llvm-commits
mailing list