#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/FoldingSet.h"
-#include <cassert>
-#include <cstring>
#include "llvm/Support/MathExtras.h"
+#include <cstring>
using namespace llvm;
/* Assumed in hexadecimal significand parsing, and conversion to
hexadecimal strings. */
+#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
namespace llvm {
// Profile - This method 'profiles' an APFloat for use with FoldingSet.
void APFloat::Profile(FoldingSetNodeID& ID) const {
- ID.Add(convertToAPInt());
+ ID.Add(bitcastToAPInt());
}
unsigned int
integerPart scratch[4];
integerPart *fullSignificand;
lostFraction lost_fraction;
+ bool ignored;
assert(semantics == rhs.semantics);
semantics = &extendedSemantics;
APFloat extendedAddend(*addend);
- status = extendedAddend.convert(extendedSemantics, rmTowardZero);
+ status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
assert(status == opOK);
lost_fraction = addOrSubtractSignificand(extendedAddend, false);
}
}
+APFloat::opStatus
+APFloat::modSpecials(const APFloat &rhs)
+{
+ switch(convolve(category, rhs.category)) {
+ default:
+ assert(0);
+
+ case convolve(fcNaN, fcZero):
+ case convolve(fcNaN, fcNormal):
+ case convolve(fcNaN, fcInfinity):
+ case convolve(fcNaN, fcNaN):
+ case convolve(fcZero, fcInfinity):
+ case convolve(fcZero, fcNormal):
+ case convolve(fcNormal, fcInfinity):
+ return opOK;
+
+ case convolve(fcZero, fcNaN):
+ case convolve(fcNormal, fcNaN):
+ case convolve(fcInfinity, fcNaN):
+ category = fcNaN;
+ copySignificand(rhs);
+ return opOK;
+
+ case convolve(fcNormal, fcZero):
+ case convolve(fcInfinity, fcZero):
+ case convolve(fcInfinity, fcNormal):
+ case convolve(fcInfinity, fcInfinity):
+ case convolve(fcZero, fcZero):
+ makeNaN();
+ return opInvalidOp;
+
+ case convolve(fcNormal, fcNormal):
+ return opOK;
+ }
+}
+
/* Change sign. */
void
APFloat::changeSign()
return fs;
}
-/* Normalized remainder. This is not currently doing TRT. */
+/* Normalized remainder. This is not currently correct in all cases. */
APFloat::opStatus
-APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
+APFloat::remainder(const APFloat &rhs)
{
opStatus fs;
APFloat V = *this;
int parts = partCount();
integerPart *x = new integerPart[parts];
+ bool ignored;
fs = V.convertToInteger(x, parts * integerPartWidth, true,
- rmNearestTiesToEven);
+ rmNearestTiesToEven, &ignored);
if (fs==opInvalidOp)
return fs;
rmNearestTiesToEven);
assert(fs==opOK); // should always work
- fs = V.multiply(rhs, rounding_mode);
+ fs = V.multiply(rhs, rmNearestTiesToEven);
assert(fs==opOK || fs==opInexact); // should not overflow or underflow
- fs = subtract(V, rounding_mode);
+ fs = subtract(V, rmNearestTiesToEven);
assert(fs==opOK || fs==opInexact); // likewise
if (isZero())
return fs;
}
+/* Normalized llvm frem (C fmod).
+ This is not currently correct in all cases. */
+APFloat::opStatus
+APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
+{
+ opStatus fs;
+ assertArithmeticOK(*semantics);
+ fs = modSpecials(rhs);
+
+ if (category == fcNormal && rhs.category == fcNormal) {
+ APFloat V = *this;
+ unsigned int origSign = sign;
+
+ fs = V.divide(rhs, rmNearestTiesToEven);
+ if (fs == opDivByZero)
+ return fs;
+
+ int parts = partCount();
+ integerPart *x = new integerPart[parts];
+ bool ignored;
+ fs = V.convertToInteger(x, parts * integerPartWidth, true,
+ rmTowardZero, &ignored);
+ if (fs==opInvalidOp)
+ return fs;
+
+ fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
+ rmNearestTiesToEven);
+ assert(fs==opOK); // should always work
+
+ fs = V.multiply(rhs, rounding_mode);
+ assert(fs==opOK || fs==opInexact); // should not overflow or underflow
+
+ fs = subtract(V, rounding_mode);
+ assert(fs==opOK || fs==opInexact); // likewise
+
+ if (isZero())
+ sign = origSign; // IEEE754 requires this
+ delete[] x;
+ }
+ return fs;
+}
+
/* Normalized fused-multiply-add. */
APFloat::opStatus
APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
return result;
}
+/// APFloat::convert - convert a value of one floating point type to another.
+/// The return value corresponds to the IEEE754 exceptions. *losesInfo
+/// records whether the transformation lost information, i.e. whether
+/// converting the result back to the original type will produce the
+/// original value (this is almost the same as return value==fsOK, but there
+/// are edge cases where this is not so).
+
APFloat::opStatus
APFloat::convert(const fltSemantics &toSemantics,
- roundingMode rounding_mode)
+ roundingMode rounding_mode, bool *losesInfo)
{
lostFraction lostFraction;
unsigned int newPartCount, oldPartCount;
exponent += toSemantics.precision - semantics->precision;
semantics = &toSemantics;
fs = normalize(rounding_mode, lostFraction);
+ *losesInfo = (fs != opOK);
} else if (category == fcNaN) {
int shift = toSemantics.precision - semantics->precision;
// Do this now so significandParts gets the right answer
+ const fltSemantics *oldSemantics = semantics;
semantics = &toSemantics;
+ *losesInfo = false;
// No normalization here, just truncate
if (shift>0)
APInt::tcShiftLeft(significandParts(), newPartCount, shift);
- else if (shift < 0)
- APInt::tcShiftRight(significandParts(), newPartCount, -shift);
+ else if (shift < 0) {
+ unsigned ushift = -shift;
+ // Figure out if we are losing information. This happens
+ // if are shifting out something other than 0s, or if the x87 long
+ // double input did not have its integer bit set (pseudo-NaN), or if the
+ // x87 long double input did not have its QNan bit set (because the x87
+ // hardware sets this bit when converting a lower-precision NaN to
+ // x87 long double).
+ if (APInt::tcLSB(significandParts(), newPartCount) < ushift)
+ *losesInfo = true;
+ if (oldSemantics == &APFloat::x87DoubleExtended &&
+ (!(*significandParts() & 0x8000000000000000ULL) ||
+ !(*significandParts() & 0x4000000000000000ULL)))
+ *losesInfo = true;
+ APInt::tcShiftRight(significandParts(), newPartCount, ushift);
+ }
// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
// does not give you back the same bits. This is dubious, and we
// don't currently do it. You're really supposed to get
} else {
semantics = &toSemantics;
fs = opOK;
+ *losesInfo = false;
}
return fs;
APFloat::opStatus
APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
bool isSigned,
- roundingMode rounding_mode) const
+ roundingMode rounding_mode,
+ bool *isExact) const
{
lostFraction lost_fraction;
const integerPart *src;
assertArithmeticOK(*semantics);
+ *isExact = false;
+
/* Handle the three special cases first. */
if(category == fcInfinity || category == fcNaN)
return opInvalidOp;
if(category == fcZero) {
APInt::tcSet(parts, 0, dstPartsCount);
+ // Negative zero can't be represented as an int.
+ *isExact = !sign;
return opOK;
}
if (exponent < 0) {
/* Our absolute value is less than one; truncate everything. */
APInt::tcSet(parts, 0, dstPartsCount);
- truncatedBits = semantics->precision;
+ /* For exponent -1 the integer bit represents .5, look at that.
+ For smaller exponents leftmost truncated bit is 0. */
+ truncatedBits = semantics->precision -1U - exponent;
} else {
/* We want the most significant (exponent + 1) bits; the rest are
truncated. */
return opInvalidOp;
}
- if (lost_fraction == lfExactlyZero)
+ if (lost_fraction == lfExactlyZero) {
+ *isExact = true;
return opOK;
- else
+ } else
return opInexact;
}
/* Same as convertToSignExtendedInteger, except we provide
deterministic values in case of an invalid operation exception,
namely zero for NaNs and the minimal or maximal value respectively
- for underflow or overflow. */
+ for underflow or overflow.
+ The *isExact output tells whether the result is exact, in the sense
+ that converting it back to the original floating point type produces
+ the original value. This is almost equivalent to result==opOK,
+ except for negative zeroes.
+*/
APFloat::opStatus
APFloat::convertToInteger(integerPart *parts, unsigned int width,
bool isSigned,
- roundingMode rounding_mode) const
+ roundingMode rounding_mode, bool *isExact) const
{
opStatus fs;
- fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode);
+ fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
+ isExact);
if (fs == opInvalidOp) {
unsigned int bits, dstPartsCount;
firstSignificantDigit = p;
for(;;) {
- uint64_t hex_value;
+ integerPart hex_value;
if(*p == '.') {
assert(dot == 0);
if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
return convertFromHexadecimalString(p + 2, rounding_mode);
- else
- return convertFromDecimalString(p, rounding_mode);
+
+ return convertFromDecimalString(p, rounding_mode);
}
/* Write out a hexadecimal representation of the floating point value
// and treating the result as a normal integer is unlikely to be useful.
APInt
-APFloat::convertToAPInt() const
+APFloat::bitcastToAPInt() const
{
if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
return convertFloatAPFloatToAPInt();
APFloat::convertToFloat() const
{
assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
- APInt api = convertToAPInt();
+ APInt api = bitcastToAPInt();
return api.bitsToFloat();
}
APFloat::convertToDouble() const
{
assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
- APInt api = convertToAPInt();
+ APInt api = bitcastToAPInt();
return api.bitsToDouble();
}
-/// Integer bit is explicit in this format. Current Intel book does not
-/// define meaning of:
-/// exponent = all 1's, integer bit not set.
-/// exponent = 0, integer bit set. (formerly "psuedodenormals")
-/// exponent!=0 nor all 1's, integer bit not set. (formerly "unnormals")
+/// Integer bit is explicit in this format. Intel hardware (387 and later)
+/// does not support these bit patterns:
+/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
+/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
+/// exponent = 0, integer bit 1 ("pseudodenormal")
+/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
+/// At the moment, the first two are treated as NaNs, the second two as Normal.
void
APFloat::initFromF80LongDoubleAPInt(const APInt &api)
{