High precision numerical type, implementing Q64.64 fixed precision. More...
#include <int64x64-128.h>
Public Types | |
enum | impl_type { int128_impl, cairo_impl, ld_impl } |
Type tag for the underlying implementation. More... | |
Public Member Functions | |
int64x64_t () | |
Default constructor. More... | |
int64x64_t (const int64_t hi, const uint64_t lo) | |
Construct from explicit high and low values. More... | |
int64x64_t (const int64x64_t &o) | |
Copy constructor. More... | |
double | GetDouble (void) const |
Get this value as a double. More... | |
int64_t | GetHigh (void) const |
Get the integer portion. More... | |
uint64_t | GetLow (void) const |
Get the fractional portion of this value, unscaled. More... | |
void | MulByInvert (const int64x64_t &o) |
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division operation. More... | |
int64x64_t & | operator= (const int64x64_t &o) |
Assignment. More... | |
int64x64_t (const double value) | |
Construct from a floating point value. More... | |
int64x64_t (const long double value) | |
Construct from a floating point value. More... | |
int64x64_t (const int v) | |
Construct from an integral type. More... | |
int64x64_t (const long int v) | |
Construct from an integral type. More... | |
int64x64_t (const long long int v) | |
Construct from an integral type. More... | |
int64x64_t (const unsigned int v) | |
Construct from an integral type. More... | |
int64x64_t (const unsigned long int v) | |
Construct from an integral type. More... | |
int64x64_t (const unsigned long long int v) | |
Construct from an integral type. More... | |
Static Public Member Functions | |
static int64x64_t | Invert (const uint64_t v) |
Compute the inverse of an integer value. More... | |
Static Public Attributes | |
static enum impl_type | implementation = int128_impl |
Type tag for this implementation. More... | |
Private Member Functions | |
int64x64_t (const int128_t v) | |
Construct from an integral type. More... | |
void | Div (const int64x64_t &o) |
Implement /= . More... | |
void | Mul (const int64x64_t &o) |
Implement *= . More... | |
Static Private Member Functions | |
static uint128_t | Udiv (const uint128_t a, const uint128_t b) |
Unsigned division of Q64.64 values. More... | |
static uint128_t | Umul (const uint128_t a, const uint128_t b) |
Unsigned multiplication of Q64.64 values. More... | |
static uint128_t | UmulByInvert (const uint128_t a, const uint128_t b) |
Unsigned multiplication of Q64.64 and Q0.128 values. More... | |
Private Attributes | |
int128_t | _v |
The Q64.64 value. More... | |
Static Private Attributes | |
static const uint128_t | HP128_MASK_HI_BIT = (((int128_t)1)<<127) |
uint128_t high bit (sign bit) More... | |
static const uint64_t | HP_MASK_HI = ~HP_MASK_LO |
Mask for sign + integer part. More... | |
static const uint64_t | HP_MASK_LO = 0xffffffffffffffffULL |
Mask for fraction part. More... | |
Friends | |
int64x64_t | operator! (const int64x64_t &lhs) |
Logical not operator. More... | |
int64x64_t & | operator*= (int64x64_t &lhs, const int64x64_t &rhs) |
Compound multiplication operator. More... | |
int64x64_t & | operator+= (int64x64_t &lhs, const int64x64_t &rhs) |
Compound addition operator. More... | |
int64x64_t | operator- (const int64x64_t &lhs) |
Unary negation operator (change sign operator) More... | |
int64x64_t & | operator-= (int64x64_t &lhs, const int64x64_t &rhs) |
Compound subtraction operator. More... | |
int64x64_t & | operator/= (int64x64_t &lhs, const int64x64_t &rhs) |
Compound division operator. More... | |
bool | operator< (const int64x64_t &lhs, const int64x64_t &rhs) |
Less than operator. More... | |
bool | operator== (const int64x64_t &lhs, const int64x64_t &rhs) |
Equality operator. More... | |
bool | operator> (const int64x64_t &lhs, const int64x64_t &rhs) |
Greater operator. More... | |
High precision numerical type, implementing Q64.64 fixed precision.
A Q64.64 fixed precision number consists of:
Bits | Function |
---|---|
1 | Sign bit |
63 | Integer portion |
64 | Fractional portion |
The high
word consists of the sign bit and integer value; the low
word is the fractional part, unscaled.
All standard arithmetic operations are supported:
Category | Operators |
---|---|
Computation | + , += , - , -= , * , *= , / , /= |
Comparison | == , != , < , <= , > , >= |
Unary | + , - , ! |
Definition at line 20 of file int64x64-128.h.
Type tag for the underlying implementation.
A few testcases are are sensitive to implementation, specifically the double implementation. To handle this, we expose the underlying implementation type here.
Enumerator | |
---|---|
int128_impl |
Native int128_t implementation. |
cairo_impl |
cairo wideint implementation |
ld_impl |
long double implementation |
Definition at line 50 of file int64x64-128.h.
|
inline |
Default constructor.
Definition at line 60 of file int64x64-128.h.
Referenced by Invert().
|
inline |
Construct from a floating point value.
[in] | value | floating value to represent |
Definition at line 68 of file int64x64-128.h.
References _v.
|
inline |
Construct from a floating point value.
[in] | value | floating value to represent |
Definition at line 73 of file int64x64-128.h.
|
inline |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 109 of file int64x64-128.h.
References _v.
|
inline |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 114 of file int64x64-128.h.
References _v.
|
inline |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 119 of file int64x64-128.h.
References _v.
|
inline |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 124 of file int64x64-128.h.
References _v.
|
inline |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 129 of file int64x64-128.h.
References _v.
|
inline |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 134 of file int64x64-128.h.
References _v.
|
inlineexplicit |
Construct from explicit high and low values.
[in] | hi | Integer portion. |
[in] | lo | Fractional portion, already scaled to HP_MAX_64. |
Definition at line 146 of file int64x64-128.h.
References _v.
|
inline |
|
inlineprivate |
Construct from an integral type.
[in] | v | integer value to represent |
Definition at line 307 of file int64x64-128.h.
|
private |
Implement /=
.
[in] | o | The divisor. |
Definition at line 78 of file int64x64-128.cc.
References _v, ns3::output_sign(), and Udiv().
Referenced by ns3::operator/=().
|
inline |
Get this value as a double.
Definition at line 175 of file int64x64-128.h.
References _v, HP_MASK_LO, and HP_MAX_64.
Referenced by ns3::int64x64::test::Int64x64Bug455TestCase::DoRun(), ns3::int64x64::test::Int64x64Bug863TestCase::DoRun(), and ns3::Time::ToDouble().
|
inline |
Get the integer portion.
Definition at line 191 of file int64x64-128.h.
References _v.
Referenced by ns3::OnOffApplication::CancelEvents(), ns3::int64x64::test::Int64x64HiLoTestCase::Check(), ns3::DelayJitterEstimation::GetLastJitter(), Invert(), and ns3::operator<<().
|
inline |
Get the fractional portion of this value, unscaled.
Definition at line 201 of file int64x64-128.h.
References _v, and HP_MASK_LO.
Referenced by ns3::int64x64::test::Int64x64HiLoTestCase::Check(), ns3::int64x64::test::Int64x64InputTestCase::Check(), ns3::operator<<(), and ns3::ReadLoDigits().
|
static |
Compute the inverse of an integer value.
Ordinary division by an integer would be limited to 64 bits of precsion. Instead, we multiply by the 128-bit inverse of the divisor. This function computes the inverse to 128-bit precision. MulByInvert() then completes the division.
(Really this should be a separate type representing Q0.128.)
[in] | v | The value to compute the inverse of. |
Definition at line 182 of file int64x64-128.cc.
References _v, GetHigh(), int64x64_t(), MulByInvert(), NS_ASSERT, and Udiv().
Referenced by ns3::int64x64::test::Int64x64InvertTestCase::Check(), and ns3::Time::SetResolution().
|
private |
Implement *=
.
[in] | o | The other factor. |
Definition at line 29 of file int64x64-128.cc.
References _v, ns3::output_sign(), and Umul().
Referenced by ns3::operator*=().
void ns3::int64x64_t::MulByInvert | ( | const int64x64_t & | o | ) |
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division operation.
[in] | o | The inverse operand. |
Definition at line 156 of file int64x64-128.cc.
References _v, and UmulByInvert().
Referenced by ns3::int64x64::test::Int64x64InvertTestCase::Check(), ns3::Time::From(), Invert(), and ns3::Time::To().
|
inline |
Assignment.
[in] | o | Value to assign to this int64x64_t. |
Definition at line 164 of file int64x64-128.h.
References _v.
Unsigned division of Q64.64 values.
[in] | a | Numerator. |
[in] | b | Denominator. |
a / b
Definition at line 87 of file int64x64-128.cc.
References HP128_MASK_HI_BIT, and NS_ASSERT_MSG.
Referenced by Div(), and Invert().
Unsigned multiplication of Q64.64 values.
Mathematically this should produce a Q128.128 value; we keep the central 128 bits, representing the Q64.64 result. We assert on integer overflow beyond the 64-bit integer portion.
[in] | a | First factor. |
[in] | b | Second factor. |
It might be tempting to just use a *
b and be done with it, but it's not that simple. With a and b as 128-bit integers, a *
b mathematically produces a 256-bit result, which the computer truncates to the lowest 128 bits. In our case, where a and b are interpreted as Q64.64 fixed point numbers, the multiplication mathematically produces a Q128.128 fixed point number. We want the middle 128 bits from the result, truncating both the high and low 64 bits. To achieve this, we carry out the multiplication explicitly with 64-bit operands and 128-bit intermediate results.
Definition at line 38 of file int64x64-128.cc.
References HP_MASK_HI, HP_MASK_LO, and NS_ABORT_MSG_IF.
Referenced by Mul().
|
staticprivate |
Unsigned multiplication of Q64.64 and Q0.128 values.
[in] | a | The numerator, a Q64.64 value. |
[in] | b | The inverse of the denominator, a Q0.128 value |
a * b
, representing the ration a / b^-1
Definition at line 166 of file int64x64-128.cc.
References HP_MASK_LO.
Referenced by MulByInvert().
|
friend |
Logical not operator.
Definition at line 397 of file int64x64-128.h.
|
friend |
Compound multiplication operator.
Definition at line 362 of file int64x64-128.h.
|
friend |
Compound addition operator.
Definition at line 344 of file int64x64-128.h.
|
friend |
Unary negation operator (change sign operator)
Definition at line 389 of file int64x64-128.h.
|
friend |
Compound subtraction operator.
Definition at line 353 of file int64x64-128.h.
|
friend |
Compound division operator.
Definition at line 371 of file int64x64-128.h.
|
friend |
Less than operator.
Definition at line 327 of file int64x64-128.h.
|
friend |
Equality operator.
Definition at line 319 of file int64x64-128.h.
|
friend |
Greater operator.
Definition at line 335 of file int64x64-128.h.
|
private |
The Q64.64 value.
Definition at line 310 of file int64x64-128.h.
Referenced by Div(), GetDouble(), GetHigh(), GetLow(), int64x64_t(), Invert(), Mul(), MulByInvert(), ns3::operator!(), ns3::operator+=(), ns3::operator-(), ns3::operator-=(), ns3::operator<(), operator=(), ns3::operator==(), and ns3::operator>().
|
staticprivate |
|
staticprivate |
Mask for fraction part.
Definition at line 25 of file int64x64-128.h.
Referenced by GetDouble(), GetLow(), Umul(), and UmulByInvert().
|
static |
Type tag for this implementation.
Definition at line 57 of file int64x64-128.h.
Referenced by ns3::int64x64::test::Int64x64HiLoTestCase::Check(), ns3::int64x64::test::Int64x64InvertTestCase::Check(), ns3::int64x64::test::Int64x64DoubleTestCase::Check(), ns3::int64x64::test::Int64x64HiLoTestCase::DoRun(), ns3::int64x64::test::Int64x64InputTestCase::DoRun(), ns3::int64x64::test::Int64x64InputOutputTestCase::DoRun(), ns3::int64x64::test::Int64x64Bug1786TestCase::DoRun(), and ns3::int64x64::test::Int64x64ImplTestCase::DoRun().