1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

Add - and -= operators to BlockFrequency using saturating arithmetic.

llvm-svn: 250077
This commit is contained in:
Cong Hou 2015-10-12 18:34:00 +00:00
parent 955b43980e
commit 2707f67c87
3 changed files with 25 additions and 0 deletions

View File

@ -49,6 +49,10 @@ public:
BlockFrequency &operator+=(BlockFrequency Freq);
BlockFrequency operator+(BlockFrequency Freq) const;
/// \brief Subtracts another block frequency using saturating arithmetic.
BlockFrequency &operator-=(BlockFrequency Freq);
BlockFrequency operator-(BlockFrequency Freq) const;
/// \brief Shift block frequency to the right by count digits saturating to 1.
BlockFrequency &operator>>=(const unsigned count);

View File

@ -56,6 +56,21 @@ BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const {
return NewFreq;
}
BlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) {
// If underflow, set frequency to 0.
if (Frequency <= Freq.Frequency)
Frequency = 0;
else
Frequency -= Freq.Frequency;
return *this;
}
BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const {
BlockFrequency NewFreq(Frequency);
NewFreq -= Freq;
return NewFreq;
}
BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
// Frequency can never be 0 by design.
assert(Frequency != 0);

View File

@ -88,6 +88,12 @@ TEST(BlockFrequencyTest, MaxToMax) {
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
}
TEST(BlockFrequencyTest, Subtract) {
BlockFrequency Freq1(0), Freq2(1);
EXPECT_EQ((Freq1 - Freq2).getFrequency(), 0u);
EXPECT_EQ((Freq2 - Freq1).getFrequency(), 1u);
}
TEST(BlockFrequency, Divide) {
BlockFrequency Freq(0x3333333333333333ULL);
Freq /= BranchProbability(1, 2);