llvm.org GIT mirror llvm / 8f1b0a7
[BPI] Add multiplication by scalar operators to BranchProbability This patch just adds two operators to BranchProbability class: (BP * scalar) and (BP *= scalar). Reviewers: junbuml, chandlerc, sanjoy, vsk Reviewed By: chandlerc Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D32334 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300945 91177308-0d34-0410-b5e6-96231b3b80d8 Serguei Katkov 3 years ago
2 changed file(s) with 60 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
111111 return *this;
112112 }
113113
114 BranchProbability &operator*=(uint32_t RHS) {
115 assert(N != UnknownN &&
116 "Unknown probability cannot participate in arithmetics.");
117 N = (uint64_t(N) * RHS > D) ? D : N * RHS;
118 return *this;
119 }
120
114121 BranchProbability &operator/=(uint32_t RHS) {
115122 assert(N != UnknownN &&
116123 "Unknown probability cannot participate in arithmetics.");
130137 }
131138
132139 BranchProbability operator*(BranchProbability RHS) const {
140 BranchProbability Prob(*this);
141 return Prob *= RHS;
142 }
143
144 BranchProbability operator*(uint32_t RHS) const {
133145 BranchProbability Prob(*this);
134146 return Prob *= RHS;
135147 }
112112 EXPECT_FALSE(BigZero > BigOne);
113113 EXPECT_TRUE(BigZero <= BigOne);
114114 EXPECT_FALSE(BigZero >= BigOne);
115 }
116
117 TEST(BranchProbabilityTest, ArithmeticOperators) {
118 BP Z(0, 1);
119 BP O(1, 1);
120 BP H(1, 2);
121 BP Q(1, 4);
122 BP Q3(3, 4);
123
124 EXPECT_EQ(Z + O, O);
125 EXPECT_EQ(H + Z, H);
126 EXPECT_EQ(H + H, O);
127 EXPECT_EQ(Q + H, Q3);
128 EXPECT_EQ(Q + Q3, O);
129 EXPECT_EQ(H + Q3, O);
130 EXPECT_EQ(Q3 + Q3, O);
131
132 EXPECT_EQ(Z - O, Z);
133 EXPECT_EQ(O - Z, O);
134 EXPECT_EQ(O - H, H);
135 EXPECT_EQ(O - Q, Q3);
136 EXPECT_EQ(Q3 - H, Q);
137 EXPECT_EQ(Q - H, Z);
138 EXPECT_EQ(Q - Q3, Z);
139
140 EXPECT_EQ(Z * O, Z);
141 EXPECT_EQ(H * H, Q);
142 EXPECT_EQ(Q * O, Q);
143 EXPECT_EQ(O * O, O);
144 EXPECT_EQ(Z * Z, Z);
145
146 EXPECT_EQ(Z * 3, Z);
147 EXPECT_EQ(Q * 3, Q3);
148 EXPECT_EQ(H * 3, O);
149 EXPECT_EQ(Q3 * 2, O);
150 EXPECT_EQ(O * UINT32_MAX, O);
151
152 EXPECT_EQ(Z / 4, Z);
153 EXPECT_EQ(O / 4, Q);
154 EXPECT_EQ(Q3 / 3, Q);
155 EXPECT_EQ(H / 2, Q);
156 EXPECT_EQ(O / 2, H);
157 EXPECT_EQ(H / UINT32_MAX, Z);
158
159 BP Min(1, 1u << 31);
160
161 EXPECT_EQ(O / UINT32_MAX, Z);
162 EXPECT_EQ(Min * UINT32_MAX, O);
115163 }
116164
117165 TEST(BranchProbabilityTest, getCompl) {