summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2011-10-24 13:50:56 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2011-10-24 13:50:56 +0000
commit13b10731abfc006fce790c5c8205cd24c7d67de8 (patch)
treeeb1a73b00a86c6246051239c5e58c7cb28c6b5ac /include
parentde1c9bb45017e25b5fc2b77e15d3c377f6572075 (diff)
downloadllvm-13b10731abfc006fce790c5c8205cd24c7d67de8.tar.gz
llvm-13b10731abfc006fce790c5c8205cd24c7d67de8.tar.bz2
llvm-13b10731abfc006fce790c5c8205cd24c7d67de8.tar.xz
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
Diffstat (limited to 'include')
-rw-r--r--include/llvm/Support/BranchProbability.h28
1 files changed, 18 insertions, 10 deletions
diff --git a/include/llvm/Support/BranchProbability.h b/include/llvm/Support/BranchProbability.h
index 6b83159de6..4b5d904400 100644
--- a/include/llvm/Support/BranchProbability.h
+++ b/include/llvm/Support/BranchProbability.h
@@ -29,10 +29,6 @@ class BranchProbability {
// Denominator
uint32_t D;
- int64_t compare(BranchProbability RHS) const {
- return (uint64_t)N * RHS.D - (uint64_t)D * RHS.N;
- }
-
public:
BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
assert(d > 0 && "Denomiator cannot be 0!");
@@ -54,12 +50,24 @@ public:
void dump() const;
- bool operator==(BranchProbability RHS) const { return compare(RHS) == 0; }
- bool operator!=(BranchProbability RHS) const { return compare(RHS) != 0; }
- bool operator< (BranchProbability RHS) const { return compare(RHS) < 0; }
- bool operator> (BranchProbability RHS) const { return compare(RHS) > 0; }
- bool operator<=(BranchProbability RHS) const { return compare(RHS) <= 0; }
- bool operator>=(BranchProbability RHS) const { return compare(RHS) >= 0; }
+ bool operator==(BranchProbability RHS) const {
+ return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
+ }
+ bool operator!=(BranchProbability RHS) const {
+ return !(*this == RHS);
+ }
+ bool operator<(BranchProbability RHS) const {
+ return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
+ }
+ bool operator>(BranchProbability RHS) const {
+ return RHS < *this;
+ }
+ bool operator<=(BranchProbability RHS) const {
+ return (uint64_t)N * RHS.D <= (uint64_t)D * RHS.N;
+ }
+ bool operator>=(BranchProbability RHS) const {
+ return RHS <= *this;
+ }
};
raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);