Fix GncInt128 maxbits to account for the flag bits.

This commit is contained in:
John Ralls 2017-04-07 12:29:04 -07:00
parent de1c56b53d
commit a467d0d397
2 changed files with 8 additions and 2 deletions

View File

@ -501,7 +501,12 @@ GncInt128::operator*= (const GncInt128& b) noexcept
}
unsigned int abits {bits()}, bbits {b.bits()};
if (abits + bbits > maxbits)
/* If the product of the high bytes < 7fff then the result will have abits +
* bbits -1 bits and won't actually overflow. It's not worth the effort to
* work that out for this corner-case, so we'll take the risk and calculate
* the product and catch the overflow later.
*/
if (abits + bbits - 1 > maxbits)
{
flags |= overflow;
m_hi = set_flags(m_hi, flags);

View File

@ -67,9 +67,10 @@ class GncInt128
uint64_t m_lo;
public:
static const unsigned int flagbits = 3;
static const unsigned int numlegs = 2;
static const unsigned int legbits = 64;
static const unsigned int maxbits = legbits * numlegs;
static const unsigned int maxbits = legbits * numlegs - flagbits;
enum // Values for m_flags
{