Skip to content

Commit

Permalink
Ensure that the sign is correctly preserved and transferred.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Dec 4, 2014
1 parent 9e37ad2 commit f4c69db
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/libqof/qof/qofint128.cpp
Expand Up @@ -170,7 +170,7 @@ QofInt128
QofInt128::lcm(const QofInt128& b) const noexcept
{
auto common = gcd(b);
return *this / common * b;
return *this / common * b.abs(); //Preserve our sign, discard the other's.
}

/* Knuth section 4.6.3 */
Expand Down Expand Up @@ -351,7 +351,7 @@ QofInt128::operator-= (const QofInt128& b) noexcept

if ((!isNeg() && b.isNeg()) || (isNeg() && !b.isNeg()))
return this->operator+= (-b);
bool operand_bigger {cmp (b) < 0};
bool operand_bigger {abs().cmp (b.abs()) < 0};
if (operand_bigger)
{
m_flags ^= neg; // ^= flips the bit
Expand Down Expand Up @@ -494,6 +494,7 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, QofInt128& q, QofIn
uint64_t qv[sublegs] {};
uint64_t d {(UINT64_C(1) << sublegbits)/(v[n - 1] + UINT64_C(1))};
uint64_t carry {UINT64_C(0)};
bool negative {q.isNeg()};
for (auto i = 0; i < m; ++i)
{
u[i] = u[i] * d + carry;
Expand Down Expand Up @@ -580,13 +581,15 @@ div_multi_leg (uint64_t* u, size_t m, uint64_t* v, size_t n, QofInt128& q, QofIn
q = QofInt128 ((qv[3] << sublegbits) + qv[2], (qv[1] << sublegbits) + qv[0]);
r = QofInt128 ((u[3] << sublegbits) + u[2], (u[1] << sublegbits) + u[0]);
r /= d;
if (negative) q = -q;
}

void
div_single_leg (uint64_t* u, size_t m, uint64_t v, QofInt128& q, QofInt128& r) noexcept
{
uint64_t qv[sublegs] {};
uint64_t carry {};
bool negative {q.isNeg()};
for (int i = m - 1; i >= 0; --i)
{
qv[i] = u[i] / v;
Expand All @@ -601,6 +604,7 @@ div_single_leg (uint64_t* u, size_t m, uint64_t v, QofInt128& q, QofInt128& r) n

q = QofInt128 ((qv[3] << sublegbits) + qv[2], (qv[1] << sublegbits) + qv[0]);
r = QofInt128 ((u[3] << sublegbits) + u[2], (u[1] << sublegbits) + u[0]);
if (negative) q = -q;
}

}// namespace
Expand Down Expand Up @@ -629,6 +633,10 @@ QofInt128::div (const QofInt128& b, QofInt128& q, QofInt128& r) noexcept
r.m_flags |= NaN;
return;
}

if (isNeg())
q.m_flags |= neg;

if (b.isNeg())
q.m_flags ^= neg;

Expand Down

0 comments on commit f4c69db

Please sign in to comment.