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

llvm-svn: 236232
This commit is contained in:
Diego Novillo 2015-04-30 13:22:48 +00:00
parent d8c1475f46
commit 95c93fc6d2
2 changed files with 37 additions and 3 deletions

View File

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

View File

@ -532,4 +532,28 @@ TEST(ScaledNumberHelpersTest, getDifference) {
EXPECT_EQ(SP64(0, 0), getDifference64(1, -64, 1, -1)); 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 } // end namespace