Fix private constructor for ScaledNumber.

Summary:
The private constructor for ScaledNumber was using uint64_t instead of
DigitsT. This was preventing instantiations of ScaledNumber with
anything other than uint64_t types.

In implementing the tests, I ran into another issue. Operators >>= and
<<= did not have variants for accepting other ScaledNumber as the shift
argument. This is expected by the SCALED_NUMBER_BOP.

It makes no sense to allow shifting a ScaledNumber by another
ScaledNumber, so the patch includes two new templates for shifting
ScaledNumbers.

Reviewers: dexonsmith

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D9350

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236232 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Diego Novillo 2015-04-30 13:22:48 +00:00
parent c846f35a04
commit 156f9e8a04
2 changed files with 37 additions and 3 deletions

View File

@ -514,7 +514,7 @@ public:
: Digits(Digits), Scale(Scale) {}
private:
ScaledNumber(const std::pair<uint64_t, int16_t> &X)
ScaledNumber(const std::pair<DigitsT, int16_t> &X)
: Digits(X.first), Scale(X.second) {}
public:
@ -732,10 +732,20 @@ SCALED_NUMBER_BOP(+, += )
SCALED_NUMBER_BOP(-, -= )
SCALED_NUMBER_BOP(*, *= )
SCALED_NUMBER_BOP(/, /= )
SCALED_NUMBER_BOP(<<, <<= )
SCALED_NUMBER_BOP(>>, >>= )
#undef SCALED_NUMBER_BOP
template <class DigitsT>
ScaledNumber<DigitsT> operator<<(const ScaledNumber<DigitsT> &L,
int16_t Shift) {
return ScaledNumber<DigitsT>(L) <<= Shift;
}
template <class DigitsT>
ScaledNumber<DigitsT> operator>>(const ScaledNumber<DigitsT> &L,
int16_t Shift) {
return ScaledNumber<DigitsT>(L) >>= Shift;
}
template <class DigitsT>
raw_ostream &operator<<(raw_ostream &OS, const ScaledNumber<DigitsT> &X) {
return X.print(OS, 10);

View File

@ -532,4 +532,28 @@ TEST(ScaledNumberHelpersTest, getDifference) {
EXPECT_EQ(SP64(0, 0), getDifference64(1, -64, 1, -1));
}
TEST(ScaledNumberHelpersTest, arithmeticOperators) {
EXPECT_EQ(ScaledNumber<uint32_t>(10, 0),
ScaledNumber<uint32_t>(1, 3) + ScaledNumber<uint32_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint32_t>(6, 0),
ScaledNumber<uint32_t>(1, 3) - ScaledNumber<uint32_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint32_t>(2, 3),
ScaledNumber<uint32_t>(1, 3) * ScaledNumber<uint32_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint32_t>(1, 2),
ScaledNumber<uint32_t>(1, 3) / ScaledNumber<uint32_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint32_t>(1, 2), ScaledNumber<uint32_t>(1, 3) >> 1);
EXPECT_EQ(ScaledNumber<uint32_t>(1, 4), ScaledNumber<uint32_t>(1, 3) << 1);
EXPECT_EQ(ScaledNumber<uint64_t>(10, 0),
ScaledNumber<uint64_t>(1, 3) + ScaledNumber<uint64_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint64_t>(6, 0),
ScaledNumber<uint64_t>(1, 3) - ScaledNumber<uint64_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint64_t>(2, 3),
ScaledNumber<uint64_t>(1, 3) * ScaledNumber<uint64_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint64_t>(1, 2),
ScaledNumber<uint64_t>(1, 3) / ScaledNumber<uint64_t>(1, 1));
EXPECT_EQ(ScaledNumber<uint64_t>(1, 2), ScaledNumber<uint64_t>(1, 3) >> 1);
EXPECT_EQ(ScaledNumber<uint64_t>(1, 4), ScaledNumber<uint64_t>(1, 3) << 1);
}
} // end namespace