[PATCH] D32334: [BPI] Add multiplication by scalar operators to BranchProbability
Serguei Katkov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 20 19:50:26 PDT 2017
skatkov created this revision.
This patch just adds two operators to BranchProbability class:
(BP * scalar) and (BP *= scalar).
https://reviews.llvm.org/D32334
Files:
include/llvm/Support/BranchProbability.h
unittests/Support/BranchProbabilityTest.cpp
Index: unittests/Support/BranchProbabilityTest.cpp
===================================================================
--- unittests/Support/BranchProbabilityTest.cpp
+++ unittests/Support/BranchProbabilityTest.cpp
@@ -115,6 +115,54 @@
EXPECT_FALSE(BigZero >= BigOne);
}
+TEST(BranchProbabilityTest, ArithmeticOperators) {
+ BP Z(0, 1);
+ BP O(1, 1);
+ BP H(1, 2);
+ BP Q(1, 4);
+ BP Q3(3, 4);
+
+ EXPECT_EQ(Z + O, O);
+ EXPECT_EQ(H + Z, H);
+ EXPECT_EQ(H + H, O);
+ EXPECT_EQ(Q + H, Q3);
+ EXPECT_EQ(Q + Q3, O);
+ EXPECT_EQ(H + Q3, O);
+ EXPECT_EQ(Q3 + Q3, O);
+
+ EXPECT_EQ(Z - O, Z);
+ EXPECT_EQ(O - Z, O);
+ EXPECT_EQ(O - H, H);
+ EXPECT_EQ(O - Q, Q3);
+ EXPECT_EQ(Q3 - H, Q);
+ EXPECT_EQ(Q - H, Z);
+ EXPECT_EQ(Q - Q3, Z);
+
+ EXPECT_EQ(Z * O, Z);
+ EXPECT_EQ(H * H, Q);
+ EXPECT_EQ(Q * O, Q);
+ EXPECT_EQ(O * O, O);
+ EXPECT_EQ(Z * Z, Z);
+
+ EXPECT_EQ(Z * 3, Z);
+ EXPECT_EQ(Q * 3, Q3);
+ EXPECT_EQ(H * 3, O);
+ EXPECT_EQ(Q3 * 2, O);
+ EXPECT_EQ(O * UINT32_MAX, O);
+
+ EXPECT_EQ(Z / 4, Z);
+ EXPECT_EQ(O / 4, Q);
+ EXPECT_EQ(Q3 / 3, Q);
+ EXPECT_EQ(H / 2, Q);
+ EXPECT_EQ(O / 2, H);
+ EXPECT_EQ(H / UINT32_MAX, Z);
+
+ BP MIN(1, 1u << 31);
+
+ EXPECT_EQ(O / UINT32_MAX, Z);
+ EXPECT_EQ(MIN * UINT32_MAX, O);
+}
+
TEST(BranchProbabilityTest, getCompl) {
EXPECT_EQ(BP(5, 7), BP(2, 7).getCompl());
EXPECT_EQ(BP(2, 7), BP(5, 7).getCompl());
Index: include/llvm/Support/BranchProbability.h
===================================================================
--- include/llvm/Support/BranchProbability.h
+++ include/llvm/Support/BranchProbability.h
@@ -112,6 +112,13 @@
return *this;
}
+ BranchProbability &operator*=(uint32_t RHS) {
+ assert(N != UnknownN &&
+ "Unknown probability cannot participate in arithmetics.");
+ N = (uint64_t(N) * RHS > D) ? D : N * RHS;
+ return *this;
+ }
+
BranchProbability &operator/=(uint32_t RHS) {
assert(N != UnknownN &&
"Unknown probability cannot participate in arithmetics.");
@@ -135,6 +142,11 @@
return Prob *= RHS;
}
+ BranchProbability operator*(uint32_t RHS) const {
+ BranchProbability Prob(*this);
+ return Prob *= RHS;
+ }
+
BranchProbability operator/(uint32_t RHS) const {
BranchProbability Prob(*this);
return Prob /= RHS;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32334.96083.patch
Type: text/x-patch
Size: 2309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170421/5311a818/attachment.bin>
More information about the llvm-commits
mailing list