2014-10-29 20:32:47 -05:00
|
|
|
/********************************************************************
|
|
|
|
* qofmath128.h -- an 128-bit integer library *
|
|
|
|
* Copyright (C) 2004 Linas Vepstas <linas@linas.org> *
|
|
|
|
* Copyright (C) 2014 John Ralls <jralls@ceridwen.us> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
*******************************************************************/
|
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
#ifndef GNCINT128_H
|
|
|
|
#define GNCINT128_H
|
2014-10-29 20:32:47 -05:00
|
|
|
|
2014-12-07 15:57:55 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2014-12-08 15:10:56 -06:00
|
|
|
#ifndef __STDC_LIMIT_MACROS
|
2014-12-07 15:57:55 -06:00
|
|
|
#define __STDC_LIMIT_MACROS 1
|
2015-06-23 19:09:51 -05:00
|
|
|
#endif
|
|
|
|
#ifndef __STDC_CONSTANT_MACROS
|
2014-12-07 15:57:55 -06:00
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
2015-06-23 19:09:51 -05:00
|
|
|
#endif
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
2014-12-07 15:57:55 -06:00
|
|
|
#define __STDC_FORMAT_MACROS 1
|
2014-12-08 15:10:56 -06:00
|
|
|
#endif
|
2014-12-07 15:57:55 -06:00
|
|
|
#include <inttypes.h>
|
|
|
|
}
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <ostream>
|
2015-10-31 17:53:32 -05:00
|
|
|
#include <type_traits>
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
//using std::string;
|
2015-10-31 18:27:51 -05:00
|
|
|
/** @addtogroup GncInt128
|
|
|
|
* @ingroup QOF
|
|
|
|
* @{
|
2017-01-31 16:21:00 -06:00
|
|
|
* @brief provides a 125-bit int as a base class for GncNumeric.
|
2014-10-29 20:32:47 -05:00
|
|
|
*
|
2017-01-31 16:21:00 -06:00
|
|
|
* In order to make space for the status flags the upper leg is limited to
|
|
|
|
* 0x1fffffffffffffff. Attempting to construct a GncInt128 with a larger upper
|
|
|
|
* leg will throw a std::overflow_error.
|
|
|
|
*
|
|
|
|
* All the usual operators are provided. Only the constructors and explicit
|
|
|
|
* integer conversions throw; all other errors are indicated by the overflow and
|
|
|
|
* NaN ("Not a Number") flags. Note that performing any operation on an
|
|
|
|
* overflowed or NaN Gncint128 will yield an overflowed or NaN result, so
|
|
|
|
* calling routines need not check until the end of a chained calculation.
|
2014-12-05 17:46:07 -06:00
|
|
|
* GncInt128 uses implicit copy and move constructors and implicit destructor.
|
2014-10-29 20:32:47 -05:00
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
class GncInt128
|
2014-10-29 20:32:47 -05:00
|
|
|
{
|
|
|
|
uint64_t m_hi;
|
|
|
|
uint64_t m_lo;
|
|
|
|
|
|
|
|
public:
|
2017-04-07 14:29:04 -05:00
|
|
|
static const unsigned int flagbits = 3;
|
2015-10-31 18:27:51 -05:00
|
|
|
static const unsigned int numlegs = 2;
|
|
|
|
static const unsigned int legbits = 64;
|
2017-04-07 14:29:04 -05:00
|
|
|
static const unsigned int maxbits = legbits * numlegs - flagbits;
|
2014-11-09 13:55:59 -06:00
|
|
|
|
2014-10-29 20:32:47 -05:00
|
|
|
enum // Values for m_flags
|
|
|
|
{
|
|
|
|
pos = 0,
|
|
|
|
neg = 1,
|
|
|
|
overflow = 2,
|
|
|
|
NaN = 4
|
|
|
|
};
|
2015-10-31 18:27:51 -05:00
|
|
|
/** @addtogroup Constructors Constructors
|
|
|
|
* Constructors are templated so that a GncInt128 can be constructed from any
|
|
|
|
* arbitrary integer type or pair of integer types.
|
|
|
|
* @note If the two parameters are of differing sign, it's taken to
|
2014-10-29 20:32:47 -05:00
|
|
|
* mean that the lower magnitude is *reducing* the magnitude of the
|
2021-02-05 13:52:33 -06:00
|
|
|
* upper, so the lower magnitude will be subtracted from UINT64_MAX to
|
2014-10-29 20:32:47 -05:00
|
|
|
* obtain the lower limb value.
|
2015-10-31 18:27:51 -05:00
|
|
|
* @{
|
2014-10-29 20:32:47 -05:00
|
|
|
*/
|
2015-10-31 18:27:51 -05:00
|
|
|
/** Default constructor. Makes 0. */
|
2015-10-31 17:53:32 -05:00
|
|
|
GncInt128();
|
|
|
|
template <typename T>
|
2017-01-30 14:55:06 -06:00
|
|
|
GncInt128(T lower) : GncInt128(INT64_C(0), static_cast<int64_t>(lower))
|
2015-10-31 17:53:32 -05:00
|
|
|
{
|
|
|
|
static_assert (std::is_integral<T>(),
|
|
|
|
"GncInt128 can be constructed only with "
|
|
|
|
"integral arguments.");
|
|
|
|
}
|
|
|
|
GncInt128(uint64_t lower) : GncInt128 {UINT64_C(0), lower} {}
|
|
|
|
/** Double-integer constructor template.
|
|
|
|
*/
|
|
|
|
template <typename T, typename U>
|
|
|
|
GncInt128(T upper, U lower, unsigned char flags = '\0') :
|
|
|
|
GncInt128 {static_cast<int64_t>(upper),
|
|
|
|
static_cast<int64_t>(lower), flags}
|
|
|
|
{
|
|
|
|
static_assert (std::is_integral<T>(),
|
|
|
|
"GncInt128 can be constructed only with "
|
|
|
|
"integral arguments.");
|
|
|
|
static_assert (std::is_integral<U>(),
|
|
|
|
"GncInt128 can be constructed only with "
|
|
|
|
"integral arguments.");
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 (int64_t upper, int64_t lower, unsigned char flags = '\0');
|
2015-10-31 17:53:32 -05:00
|
|
|
template <typename T>
|
|
|
|
GncInt128(T upper, uint64_t lower) :
|
|
|
|
GncInt128 {static_cast<int64_t>(upper), lower}
|
|
|
|
{
|
|
|
|
static_assert (std::is_integral<T>(),
|
|
|
|
"GncInt128 can be constructed only with "
|
|
|
|
"integral arguments.");
|
|
|
|
}
|
2014-11-17 14:08:00 -06:00
|
|
|
|
2015-10-31 17:53:32 -05:00
|
|
|
GncInt128 (int64_t upper, uint64_t lower, unsigned char flags = '\0');
|
|
|
|
GncInt128 (uint64_t upper, uint64_t lower, unsigned char flags = '\0');
|
2015-10-31 18:27:51 -05:00
|
|
|
/** @} */
|
2014-11-17 14:08:00 -06:00
|
|
|
/**
|
|
|
|
* Clear the object.
|
|
|
|
*
|
|
|
|
* Sets all member variables to zero.
|
|
|
|
* @return A reference to the object for chaining.
|
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128& zero() noexcept;
|
2014-11-17 14:08:00 -06:00
|
|
|
|
2014-10-29 20:32:47 -05:00
|
|
|
/**
|
|
|
|
* Compare function.
|
|
|
|
*
|
|
|
|
* @return -1 if the object is less than the parameter, 0 if they're
|
|
|
|
* equal, and 1 if the object is greater.
|
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
int cmp (const GncInt128& b) const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
/**
|
2017-01-16 16:03:50 -06:00
|
|
|
* Computes the Greatest Common Divisor between the object and parameter
|
2014-10-29 20:32:47 -05:00
|
|
|
*
|
2014-12-05 17:46:07 -06:00
|
|
|
* @return A GncInt128 having the GCD.
|
2014-10-29 20:32:47 -05:00
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 gcd (GncInt128 b) const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
/**
|
2014-11-17 14:08:00 -06:00
|
|
|
* Computes the Least Common Multiple between the object and parameter
|
2014-10-29 20:32:47 -05:00
|
|
|
*
|
2014-12-05 17:46:07 -06:00
|
|
|
* @return A GncInt128 having the LCM.
|
2014-10-29 20:32:47 -05:00
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 lcm (const GncInt128& b) const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the object raised to the parameter's power
|
|
|
|
*
|
2014-12-05 17:46:07 -06:00
|
|
|
* @param b The power to raise this to. No point in taking a GncInt128, any
|
2014-11-19 15:31:19 -06:00
|
|
|
* value greater than 128 would overflow on any value other than 1.
|
2014-12-05 17:46:07 -06:00
|
|
|
* @return A GncInt128
|
2014-10-29 20:32:47 -05:00
|
|
|
*/
|
2014-12-08 15:09:32 -06:00
|
|
|
GncInt128 pow (unsigned int n) const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
2014-11-17 14:13:34 -06:00
|
|
|
/**
|
|
|
|
* Computes a quotient and a remainder, passed as reference parameters.
|
|
|
|
*
|
|
|
|
* 'this' is the dividend. The quotient and remainder args must be initialized
|
|
|
|
* to zero.
|
|
|
|
* @param d The divisor
|
|
|
|
* @param q The quotient; will be NaN if divisor = 0
|
|
|
|
* @param r The remainder; will be 0 if divisor = 0
|
|
|
|
*/
|
2017-01-30 14:56:24 -06:00
|
|
|
void div (const GncInt128& d, GncInt128& q, GncInt128& r) const noexcept;
|
2014-11-17 14:13:34 -06:00
|
|
|
|
2014-10-29 20:32:47 -05:00
|
|
|
/**
|
|
|
|
* Explicit conversion to int64_t.
|
|
|
|
*
|
|
|
|
* @return A int64_t
|
|
|
|
* @throws std::overflow_error if the object's value is > INT64_MAX or NaN.
|
|
|
|
* @throws std::underflow_error if the object's value is < INT64_MIN
|
|
|
|
*/
|
|
|
|
explicit operator int64_t() const;
|
|
|
|
/**
|
|
|
|
* Explicit conversion to uint64_t.
|
|
|
|
*
|
|
|
|
* @return A uint64_t
|
|
|
|
* @throws std::overflow_error if the object's value is > UINT64_MAX or NaN.
|
|
|
|
* @throws std::underflow_error if the object's value is < 0.
|
|
|
|
*/
|
|
|
|
explicit operator uint64_t() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if the object value is < 0
|
|
|
|
*/
|
|
|
|
bool isNeg () const noexcept;
|
|
|
|
/**
|
|
|
|
* @return true if the object value is > INT64_MAX or < INT64_MIN
|
|
|
|
*/
|
|
|
|
bool isBig () const noexcept;
|
|
|
|
/**
|
|
|
|
* @return true if a calculation has produced a result of larger
|
|
|
|
* magnitude than can be contained in the 128 bits available.
|
|
|
|
*/
|
|
|
|
bool isOverflow () const noexcept;
|
|
|
|
/**
|
2017-01-16 16:03:50 -06:00
|
|
|
* @return true if an illegal calculation has occurred.
|
2014-10-29 20:32:47 -05:00
|
|
|
*/
|
|
|
|
bool isNan () const noexcept;
|
|
|
|
/**
|
|
|
|
* @return true if the object represents 0.
|
|
|
|
*/
|
|
|
|
bool isZero() const noexcept;
|
2017-01-30 17:01:27 -06:00
|
|
|
/**
|
|
|
|
* @return true if neither the overflow nor nan flags are set.
|
|
|
|
*/
|
|
|
|
bool valid() const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
2014-11-17 16:43:23 -06:00
|
|
|
/**
|
|
|
|
* @return the number of bits used to represent the value
|
|
|
|
*/
|
2014-12-08 15:09:32 -06:00
|
|
|
unsigned int bits() const noexcept;
|
2014-11-17 16:43:23 -06:00
|
|
|
|
2014-10-29 20:32:47 -05:00
|
|
|
/**
|
|
|
|
* Fills a supplied buffer with a representation of the number in base 10. If
|
2014-12-05 17:46:07 -06:00
|
|
|
* the GncInt128 is overflowed or NaN it will contain the words "Overflow" or
|
2014-10-29 20:32:47 -05:00
|
|
|
* "NaN" respectively.
|
|
|
|
*
|
|
|
|
* @param buf char[41], 39 digits plus sign and trailing 0.
|
|
|
|
* @return pointer to the buffer for convenience
|
|
|
|
*/
|
|
|
|
char* asCharBufR(char* buf) const noexcept;
|
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 abs() const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 operator-() const noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
explicit operator bool() const noexcept;
|
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128& operator++ () noexcept;
|
|
|
|
GncInt128& operator++ (int) noexcept;
|
|
|
|
GncInt128& operator-- () noexcept;
|
|
|
|
GncInt128& operator-- (int) noexcept;
|
2014-12-08 15:09:32 -06:00
|
|
|
GncInt128& operator<<= (unsigned int i) noexcept;
|
|
|
|
GncInt128& operator>>= (unsigned int i) noexcept;
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128& operator+= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator-= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator*= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator/= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator%= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator&= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator|= (const GncInt128& b) noexcept;
|
|
|
|
GncInt128& operator^= (const GncInt128& b) noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
static const GncInt128 k_gncint128_Max {UINT64_MAX, UINT64_MAX, GncInt128::pos};
|
|
|
|
static const GncInt128 k_gncint128_Min {UINT64_MAX, UINT64_MAX, GncInt128::neg};
|
|
|
|
|
|
|
|
GncInt128 operator+ (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator- (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator* (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator/ (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator% (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator& (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator| (GncInt128 a, const GncInt128& b) noexcept;
|
|
|
|
GncInt128 operator^ (GncInt128 a, const GncInt128& b) noexcept;
|
2014-12-08 15:09:32 -06:00
|
|
|
GncInt128 operator<< (GncInt128 a, unsigned int b) noexcept;
|
|
|
|
GncInt128 operator>> (GncInt128 a, unsigned int b) noexcept;
|
2014-12-05 17:46:07 -06:00
|
|
|
|
|
|
|
bool operator== (const GncInt128& a, const GncInt128& b) noexcept;
|
|
|
|
bool operator!= (const GncInt128& a, const GncInt128& b) noexcept;
|
|
|
|
bool operator<= (const GncInt128& a, const GncInt128& b) noexcept;
|
|
|
|
bool operator>= (const GncInt128& a, const GncInt128& b) noexcept;
|
|
|
|
bool operator< (const GncInt128& a, const GncInt128& b) noexcept;
|
|
|
|
bool operator> (const GncInt128& a, const GncInt128& b) noexcept;
|
|
|
|
|
|
|
|
std::ostream& operator<< (std::ostream&, const GncInt128&) noexcept;
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
/** Compute the greatest common denominator of two integers
|
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 gcd (int64_t a, int64_t b);
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
/** Compute the least common multiple of two integers
|
|
|
|
*/
|
2014-12-05 17:46:07 -06:00
|
|
|
GncInt128 lcm (int64_t a, int64_t b);
|
2017-01-30 14:55:06 -06:00
|
|
|
|
2014-12-05 17:46:07 -06:00
|
|
|
#endif //GNCINT128_H
|
2014-10-29 20:32:47 -05:00
|
|
|
|
|
|
|
/** @} */
|