llvm.org GIT mirror llvm / 13b1073
Implement comparison operators for BranchProbability in a way that can't overflow INT64_MAX. Add a test case for the edge case that triggers this. Thanks to Chandler for bringing this to my attention. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142794 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 8 years ago
2 changed file(s) with 27 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
2828 // Denominator
2929 uint32_t D;
3030
31 int64_t compare(BranchProbability RHS) const {
32 return (uint64_t)N * RHS.D - (uint64_t)D * RHS.N;
33 }
34
3531 public:
3632 BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
3733 assert(d > 0 && "Denomiator cannot be 0!");
5349
5450 void dump() const;
5551
56 bool operator==(BranchProbability RHS) const { return compare(RHS) == 0; }
57 bool operator!=(BranchProbability RHS) const { return compare(RHS) != 0; }
58 bool operator< (BranchProbability RHS) const { return compare(RHS) < 0; }
59 bool operator> (BranchProbability RHS) const { return compare(RHS) > 0; }
60 bool operator<=(BranchProbability RHS) const { return compare(RHS) <= 0; }
61 bool operator>=(BranchProbability RHS) const { return compare(RHS) >= 0; }
52 bool operator==(BranchProbability RHS) const {
53 return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
54 }
55 bool operator!=(BranchProbability RHS) const {
56 return !(*this == RHS);
57 }
58 bool operator<(BranchProbability RHS) const {
59 return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
60 }
61 bool operator>(BranchProbability RHS) const {
62 return RHS < *this;
63 }
64 bool operator<=(BranchProbability RHS) const {
65 return (uint64_t)N * RHS.D <= (uint64_t)D * RHS.N;
66 }
67 bool operator>=(BranchProbability RHS) const {
68 return RHS <= *this;
69 }
6270 };
6371
6472 raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
7070 EXPECT_TRUE(B > C);
7171 EXPECT_FALSE(B <= C);
7272 EXPECT_TRUE(B >= C);
73
74 BranchProbability BigZero(0, UINT32_MAX);
75 BranchProbability BigOne(UINT32_MAX, UINT32_MAX);
76 EXPECT_FALSE(BigZero == BigOne);
77 EXPECT_TRUE(BigZero != BigOne);
78 EXPECT_TRUE(BigZero < BigOne);
79 EXPECT_FALSE(BigZero > BigOne);
80 EXPECT_TRUE(BigZero <= BigOne);
81 EXPECT_FALSE(BigZero >= BigOne);
7382 }
7483
7584 }