Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/CppCore.Test/Math/BigInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ namespace CppCore { namespace Test { namespace Math
}
INLINE static bool todouble128()
{
/*
// test low 64-bit ones that can be tested with double chast
for (uint64_t i = 0; i <= 0x000000000000FFFFULL; i++)
{
Expand Down Expand Up @@ -244,7 +245,7 @@ namespace CppCore { namespace Test { namespace Math

if (!specials128)
return false;

*/
return true;
}

Expand Down
12 changes: 12 additions & 0 deletions include/CppCore.Test/Math/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,18 @@ namespace CppCore { namespace Test { namespace Math
if (!specials)
return false;

// test some other special cases
uint64_t x = 0U;
for (size_t i = 0; i < 64U; i++)
{
x <<= 1;
x |= 1;
double d1 = CppCore::todouble(x);
double d2 = (double)x;
if (d1 != d2)
return false;
}

// test some random ones
CppCore::Random::Default64 rnd;
for (size_t i = 0; i < 100000; i++)
Expand Down
7 changes: 4 additions & 3 deletions include/CppCore/Math/BigInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2575,8 +2575,9 @@ namespace CppCore

/// <summary>
/// Convert to IEEE-754 Double Precision
/// TODO: This has bugs and should not be used
/// </summary>
constexpr INLINE explicit operator double () const
/*constexpr INLINE explicit operator double() const
{
constexpr uint32_t BITS = N8 * 8U;
constexpr uint32_t LOSS = BITS - 52U;
Expand All @@ -2602,10 +2603,10 @@ namespace CppCore
los = man & LMSK;
man >>= LOSS;
man += ((los > LCMP) || ((los == LCMP) && ((uint32_t)man & 1U)));
exp |= (uint64_t)man;
exp |= (uint64_t)man; // this needs refactoring, see CppCore::todouble()

return reinterpret_cast<double&>(exp);
}
}*/

/// <summary>
/// Convert to Decimal String
Expand Down
17 changes: 8 additions & 9 deletions include/CppCore/Math/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ namespace CppCore
// build the zero (positive) sign bit and the exponent bits
// exp is neg for 0-1022, zero for 1023 and pos above.
// shift the exponent in place (52 bits mantissa)
const uint64_t exp = (uint64_t)(IH + 1023U) << 52;
uint64_t exp = (uint64_t)(IH + 1023U) << 52;

// start mantissa building by left-shifting the high bit out of x.
// this removes it and creates a fixed offset to the hidden highbit position in IEEE-754.
Expand Down Expand Up @@ -1572,18 +1572,17 @@ namespace CppCore
case FE_TONEAREST:
// round up for upper range (break tie by only rounding odd up)
man += ((lost > 2048U) | ((lost == 2048U) & man & 1U));

//if (_bittest((long*)&lost, 11))
// man += ((lost & 2047) | (man & 1)) != 0;

// if rounding overflowed into exp bits, increment exponent and remove overflow bit.
// disabled because just ORing the overflow into exp below gives same result here.
//if (man & MASKEXP) { exp++; man &= MASKMAN; }
break;
}

// if rounding overflowed into exp bits,
// increment exponent and remove overflow bit from mantissa
if (man & MASKEXP) {
exp += MASKHHI;
man &= MASKMAN;
}

// combine the exponent bits with the mantissa bits
// OR a possible overflow bit of mantissa into exponent
const uint64_t ri = exp | man;
return reinterpret_cast<const double&>(ri);
}
Expand Down