Add zero and abs functions to QofInt128.

This commit is contained in:
John Ralls 2014-11-17 12:08:00 -08:00
parent 1e6855efe5
commit c7752d5d3c
2 changed files with 30 additions and 1 deletions

View File

@ -64,6 +64,14 @@ QofInt128::QofInt128 (int64_t upper, int64_t lower, unsigned char flags) :
m_flags {flags}, m_hi {upper}, m_flags {flags}, m_hi {upper},
m_lo {lower} {} m_lo {lower} {}
QofInt128&
QofInt128::zero () noexcept
{
m_flags = 0;
m_lo = m_hi = UINT64_C(0);
return *this;
}
QofInt128::operator int64_t() const QofInt128::operator int64_t() const
{ {
if ((m_flags & neg) && isBig()) if ((m_flags & neg) && isBig())
@ -138,6 +146,16 @@ QofInt128::isZero() const noexcept
return ((m_flags & (overflow | NaN)) == 0 && m_hi == 0 && m_lo == 0); return ((m_flags & (overflow | NaN)) == 0 && m_hi == 0 && m_lo == 0);
} }
QofInt128
QofInt128::abs() const noexcept
{
if (isNeg())
return operator-();
return *this;
}
QofInt128 QofInt128
QofInt128::operator-() const noexcept QofInt128::operator-() const noexcept
{ {

View File

@ -42,6 +42,7 @@
* on an overflowed or NaN QofInt128 will yield an overflowed or NaN * on an overflowed or NaN QofInt128 will yield an overflowed or NaN
* result, so calling routines need not check until the end of a * result, so calling routines need not check until the end of a
* chained calculation. * chained calculation.
* QofInt128 uses implicit copy and move constructors and implicit destructor.
*/ */
class QofInt128 class QofInt128
{ {
@ -75,6 +76,15 @@ enum // Values for m_flags
*/ */
QofInt128 (int64_t upper, int64_t lower, unsigned char flags = '\0'); QofInt128 (int64_t upper, int64_t lower, unsigned char flags = '\0');
QofInt128 (uint64_t upper, uint64_t lower, unsigned char flags = '\0'); QofInt128 (uint64_t upper, uint64_t lower, unsigned char flags = '\0');
/**
* Clear the object.
*
* Sets all member variables to zero.
* @return A reference to the object for chaining.
*/
QofInt128& zero() noexcept;
/** /**
* Compare function. * Compare function.
* *
@ -90,7 +100,7 @@ enum // Values for m_flags
*/ */
QofInt128 gcd (const QofInt128& b) const noexcept; QofInt128 gcd (const QofInt128& b) const noexcept;
/** /**
* Computes the Least Common Multiple between the object and paramter * Computes the Least Common Multiple between the object and parameter
* *
* @return A QofInt128 having the LCM. * @return A QofInt128 having the LCM.
*/ */
@ -152,6 +162,7 @@ enum // Values for m_flags
*/ */
char* asCharBufR(char* buf) const noexcept; char* asCharBufR(char* buf) const noexcept;
QofInt128 abs() const noexcept;
QofInt128 operator-() const noexcept; QofInt128 operator-() const noexcept;
explicit operator bool() const noexcept; explicit operator bool() const noexcept;