60 return (negA && !negB) || (!negA && negB);
68 uint128_t result = Umul (a, b);
69 _v = negative ? -result : result;
73 int64x64_t::Umul (
const uint128_t a,
const uint128_t b)
75 uint128_t aL = a & HP_MASK_LO;
76 uint128_t bL = b & HP_MASK_LO;
77 uint128_t aH = (a >> 64) & HP_MASK_LO;
78 uint128_t bH = (b >> 64) & HP_MASK_LO;
81 uint128_t hiPart, loPart, midPart;
90 midPart = aL * bH + aH * bL;
95 "High precision 128 bits multiplication error: multiplication overflow.");
99 res2 = midPart & HP_MASK_LO;
100 result = res1 + res2;
102 res1 = midPart >> 64;
103 res2 = hiPart & HP_MASK_LO;
117 int128_t result = Udiv (a, b);
118 _v = negative ? -result : result;
122 int64x64_t::Udiv (
const uint128_t a,
const uint128_t b)
127 uint128_t quo = rem / den;
129 uint128_t result = quo;
132 const uint64_t DIGITS = 64;
133 const uint128_t ZERO = 0;
136 "Remainder not less than divisor");
142 while ( (shift < DIGITS) && !(den & 0x1))
148 while ( (digis < DIGITS) && (rem != ZERO) )
151 while ( (digis + shift < DIGITS) &&
152 !(rem & HP128_MASK_HI_BIT))
162 while ( (digis + shift < DIGITS) &&
163 ( !(den & 0x1) || (rem < den) ) )
183 shift = DIGITS - digis;
191 int64x64_t::MulByInvert (
const int64x64_t & o)
193 bool negResult = _v < 0;
194 uint128_t a = negResult ? -_v : _v;
195 uint128_t result = UmulByInvert (a, o._v);
197 _v = negResult ? -result : result;
201 int64x64_t::UmulByInvert (
const uint128_t a,
const uint128_t b)
203 uint128_t result, ah, bh, al, bl;
210 mid = ah * bl + al * bh;
217 int64x64_t::Invert (
const uint64_t v)
224 result._v = Udiv (a, v);
226 tmp.MulByInvert (result);
227 if (tmp.GetHigh () != 1)
High precision numerical type, implementing Q64.64 fixed precision.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
NS_ABORT_x macro definitions.
static bool output_sign(const int128_t sa, const int128_t sb, uint128_t &ua, uint128_t &ub)
Compute the sign of the result of multiplying or dividing Q64.64 fixed precision operands.