2007-08-20 22:49:32 +00:00
|
|
|
//===-- APFloat.cpp - Implement APFloat class -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Neil Booth and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a class to represent arbitrary precision floating
|
|
|
|
// point values and provide a variety of arithmetic operations on them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include <cassert>
|
2007-10-03 22:26:02 +00:00
|
|
|
#include <cstring>
|
2007-08-20 22:49:32 +00:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2007-08-24 05:08:11 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
|
|
|
|
|
2007-10-03 22:26:02 +00:00
|
|
|
/* Assumed in hexadecimal significand parsing, and conversion to
|
|
|
|
hexadecimal strings. */
|
2007-08-20 22:49:32 +00:00
|
|
|
COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/* Represents floating point arithmetic semantics. */
|
|
|
|
struct fltSemantics {
|
|
|
|
/* The largest E such that 2^E is representable; this matches the
|
|
|
|
definition of IEEE 754. */
|
|
|
|
exponent_t maxExponent;
|
|
|
|
|
|
|
|
/* The smallest E such that 2^E is a normalized number; this
|
|
|
|
matches the definition of IEEE 754. */
|
|
|
|
exponent_t minExponent;
|
|
|
|
|
|
|
|
/* Number of bits in the significand. This includes the integer
|
|
|
|
bit. */
|
|
|
|
unsigned char precision;
|
|
|
|
|
|
|
|
/* If the target format has an implicit integer bit. */
|
|
|
|
bool implicitIntegerBit;
|
|
|
|
};
|
|
|
|
|
|
|
|
const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
|
|
|
|
const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
|
|
|
|
const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
|
|
|
|
const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, false };
|
2007-09-20 23:47:58 +00:00
|
|
|
const fltSemantics APFloat::Bogus = { 0, 0, 0, false };
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a bunch of private, handy routines in an anonymous namespace. */
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline unsigned int
|
|
|
|
partCountForBits(unsigned int bits)
|
|
|
|
{
|
|
|
|
return ((bits) + integerPartWidth - 1) / integerPartWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
digitValue(unsigned int c)
|
|
|
|
{
|
|
|
|
unsigned int r;
|
|
|
|
|
|
|
|
r = c - '0';
|
|
|
|
if(r <= 9)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return -1U;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
hexDigitValue (unsigned int c)
|
|
|
|
{
|
|
|
|
unsigned int r;
|
|
|
|
|
|
|
|
r = c - '0';
|
|
|
|
if(r <= 9)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = c - 'A';
|
|
|
|
if(r <= 5)
|
|
|
|
return r + 10;
|
|
|
|
|
|
|
|
r = c - 'a';
|
|
|
|
if(r <= 5)
|
|
|
|
return r + 10;
|
|
|
|
|
|
|
|
return -1U;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is ugly and needs cleaning up, but I don't immediately see
|
|
|
|
how whilst remaining safe. */
|
|
|
|
static int
|
|
|
|
totalExponent(const char *p, int exponentAdjustment)
|
|
|
|
{
|
|
|
|
integerPart unsignedExponent;
|
|
|
|
bool negative, overflow;
|
|
|
|
long exponent;
|
|
|
|
|
|
|
|
/* Move past the exponent letter and sign to the digits. */
|
|
|
|
p++;
|
|
|
|
negative = *p == '-';
|
|
|
|
if(*p == '-' || *p == '+')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
unsignedExponent = 0;
|
|
|
|
overflow = false;
|
|
|
|
for(;;) {
|
|
|
|
unsigned int value;
|
|
|
|
|
|
|
|
value = digitValue(*p);
|
|
|
|
if(value == -1U)
|
2007-09-26 21:33:42 +00:00
|
|
|
break;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
p++;
|
|
|
|
unsignedExponent = unsignedExponent * 10 + value;
|
|
|
|
if(unsignedExponent > 65535)
|
2007-09-26 21:33:42 +00:00
|
|
|
overflow = true;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
|
|
|
|
overflow = true;
|
|
|
|
|
|
|
|
if(!overflow) {
|
|
|
|
exponent = unsignedExponent;
|
|
|
|
if(negative)
|
2007-09-26 21:33:42 +00:00
|
|
|
exponent = -exponent;
|
2007-08-20 22:49:32 +00:00
|
|
|
exponent += exponentAdjustment;
|
|
|
|
if(exponent > 65535 || exponent < -65536)
|
2007-09-26 21:33:42 +00:00
|
|
|
overflow = true;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(overflow)
|
|
|
|
exponent = negative ? -65536: 65535;
|
|
|
|
|
|
|
|
return exponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
|
|
|
|
{
|
|
|
|
*dot = 0;
|
|
|
|
while(*p == '0')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if(*p == '.') {
|
|
|
|
*dot = p++;
|
|
|
|
while(*p == '0')
|
2007-09-26 21:33:42 +00:00
|
|
|
p++;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the trailing fraction of a hexadecimal number.
|
|
|
|
DIGITVALUE is the first hex digit of the fraction, P points to
|
|
|
|
the next digit. */
|
|
|
|
lostFraction
|
|
|
|
trailingHexadecimalFraction(const char *p, unsigned int digitValue)
|
|
|
|
{
|
|
|
|
unsigned int hexDigit;
|
|
|
|
|
|
|
|
/* If the first trailing digit isn't 0 or 8 we can work out the
|
|
|
|
fraction immediately. */
|
|
|
|
if(digitValue > 8)
|
|
|
|
return lfMoreThanHalf;
|
|
|
|
else if(digitValue < 8 && digitValue > 0)
|
|
|
|
return lfLessThanHalf;
|
|
|
|
|
|
|
|
/* Otherwise we need to find the first non-zero digit. */
|
|
|
|
while(*p == '0')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
hexDigit = hexDigitValue(*p);
|
|
|
|
|
|
|
|
/* If we ran off the end it is exactly zero or one-half, otherwise
|
|
|
|
a little more. */
|
|
|
|
if(hexDigit == -1U)
|
|
|
|
return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
|
|
|
|
else
|
|
|
|
return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
|
|
|
|
}
|
|
|
|
|
2007-10-03 15:16:41 +00:00
|
|
|
/* Return the fraction lost were a bignum truncated losing the least
|
|
|
|
significant BITS bits. */
|
2007-08-20 22:49:32 +00:00
|
|
|
lostFraction
|
2007-10-03 22:26:02 +00:00
|
|
|
lostFractionThroughTruncation(const integerPart *parts,
|
2007-09-26 21:33:42 +00:00
|
|
|
unsigned int partCount,
|
|
|
|
unsigned int bits)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
unsigned int lsb;
|
|
|
|
|
|
|
|
lsb = APInt::tcLSB(parts, partCount);
|
|
|
|
|
|
|
|
/* Note this is guaranteed true if bits == 0, or LSB == -1U. */
|
|
|
|
if(bits <= lsb)
|
|
|
|
return lfExactlyZero;
|
|
|
|
if(bits == lsb + 1)
|
|
|
|
return lfExactlyHalf;
|
|
|
|
if(bits <= partCount * integerPartWidth
|
|
|
|
&& APInt::tcExtractBit(parts, bits - 1))
|
|
|
|
return lfMoreThanHalf;
|
|
|
|
|
|
|
|
return lfLessThanHalf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shift DST right BITS bits noting lost fraction. */
|
|
|
|
lostFraction
|
|
|
|
shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
|
|
|
|
{
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
|
|
|
|
lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
|
|
|
|
|
|
|
|
APInt::tcShiftRight(dst, parts, bits);
|
|
|
|
|
|
|
|
return lost_fraction;
|
|
|
|
}
|
2007-10-03 22:26:02 +00:00
|
|
|
|
2007-10-07 08:51:21 +00:00
|
|
|
/* Combine the effect of two lost fractions. */
|
|
|
|
lostFraction
|
|
|
|
combineLostFractions(lostFraction moreSignificant,
|
|
|
|
lostFraction lessSignificant)
|
|
|
|
{
|
|
|
|
if(lessSignificant != lfExactlyZero) {
|
|
|
|
if(moreSignificant == lfExactlyZero)
|
|
|
|
moreSignificant = lfLessThanHalf;
|
|
|
|
else if(moreSignificant == lfExactlyHalf)
|
|
|
|
moreSignificant = lfMoreThanHalf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return moreSignificant;
|
|
|
|
}
|
2007-10-03 22:26:02 +00:00
|
|
|
|
|
|
|
/* Zero at the end to avoid modular arithmetic when adding one; used
|
|
|
|
when rounding up during hexadecimal output. */
|
|
|
|
static const char hexDigitsLower[] = "0123456789abcdef0";
|
|
|
|
static const char hexDigitsUpper[] = "0123456789ABCDEF0";
|
|
|
|
static const char infinityL[] = "infinity";
|
|
|
|
static const char infinityU[] = "INFINITY";
|
|
|
|
static const char NaNL[] = "nan";
|
|
|
|
static const char NaNU[] = "NAN";
|
|
|
|
|
|
|
|
/* Write out an integerPart in hexadecimal, starting with the most
|
|
|
|
significant nibble. Write out exactly COUNT hexdigits, return
|
|
|
|
COUNT. */
|
|
|
|
static unsigned int
|
|
|
|
partAsHex (char *dst, integerPart part, unsigned int count,
|
|
|
|
const char *hexDigitChars)
|
|
|
|
{
|
|
|
|
unsigned int result = count;
|
|
|
|
|
|
|
|
assert (count != 0 && count <= integerPartWidth / 4);
|
|
|
|
|
|
|
|
part >>= (integerPartWidth - 4 * count);
|
|
|
|
while (count--) {
|
|
|
|
dst[count] = hexDigitChars[part & 0xf];
|
|
|
|
part >>= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-10-06 07:29:25 +00:00
|
|
|
/* Write out an unsigned decimal integer. */
|
2007-10-03 22:26:02 +00:00
|
|
|
static char *
|
2007-10-06 07:29:25 +00:00
|
|
|
writeUnsignedDecimal (char *dst, unsigned int n)
|
2007-10-03 22:26:02 +00:00
|
|
|
{
|
2007-10-06 07:29:25 +00:00
|
|
|
char buff[40], *p;
|
2007-10-03 22:26:02 +00:00
|
|
|
|
2007-10-06 07:29:25 +00:00
|
|
|
p = buff;
|
|
|
|
do
|
|
|
|
*p++ = '0' + n % 10;
|
|
|
|
while (n /= 10);
|
2007-10-03 22:26:02 +00:00
|
|
|
|
2007-10-06 07:29:25 +00:00
|
|
|
do
|
|
|
|
*dst++ = *--p;
|
|
|
|
while (p != buff);
|
2007-10-03 22:26:02 +00:00
|
|
|
|
2007-10-06 07:29:25 +00:00
|
|
|
return dst;
|
|
|
|
}
|
2007-10-03 22:26:02 +00:00
|
|
|
|
2007-10-06 07:29:25 +00:00
|
|
|
/* Write out a signed decimal integer. */
|
|
|
|
static char *
|
|
|
|
writeSignedDecimal (char *dst, int value)
|
|
|
|
{
|
|
|
|
if (value < 0) {
|
|
|
|
*dst++ = '-';
|
|
|
|
dst = writeUnsignedDecimal(dst, -(unsigned) value);
|
|
|
|
} else
|
|
|
|
dst = writeUnsignedDecimal(dst, value);
|
2007-10-03 22:26:02 +00:00
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Constructors. */
|
|
|
|
void
|
|
|
|
APFloat::initialize(const fltSemantics *ourSemantics)
|
|
|
|
{
|
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
semantics = ourSemantics;
|
|
|
|
count = partCount();
|
|
|
|
if(count > 1)
|
|
|
|
significand.parts = new integerPart[count];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APFloat::freeSignificand()
|
|
|
|
{
|
|
|
|
if(partCount() > 1)
|
|
|
|
delete [] significand.parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APFloat::assign(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
|
|
|
|
sign = rhs.sign;
|
|
|
|
category = rhs.category;
|
|
|
|
exponent = rhs.exponent;
|
2007-08-31 04:03:46 +00:00
|
|
|
if(category == fcNormal || category == fcNaN)
|
2007-08-20 22:49:32 +00:00
|
|
|
copySignificand(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APFloat::copySignificand(const APFloat &rhs)
|
|
|
|
{
|
2007-08-31 04:03:46 +00:00
|
|
|
assert(category == fcNormal || category == fcNaN);
|
2007-08-20 22:49:32 +00:00
|
|
|
assert(rhs.partCount() >= partCount());
|
|
|
|
|
|
|
|
APInt::tcAssign(significandParts(), rhs.significandParts(),
|
2007-09-26 21:33:42 +00:00
|
|
|
partCount());
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
APFloat &
|
|
|
|
APFloat::operator=(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
if(this != &rhs) {
|
|
|
|
if(semantics != rhs.semantics) {
|
|
|
|
freeSignificand();
|
|
|
|
initialize(rhs.semantics);
|
|
|
|
}
|
|
|
|
assign(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-08-24 00:56:33 +00:00
|
|
|
bool
|
2007-08-24 22:09:56 +00:00
|
|
|
APFloat::bitwiseIsEqual(const APFloat &rhs) const {
|
2007-08-24 00:56:33 +00:00
|
|
|
if (this == &rhs)
|
|
|
|
return true;
|
|
|
|
if (semantics != rhs.semantics ||
|
2007-08-31 04:03:46 +00:00
|
|
|
category != rhs.category ||
|
|
|
|
sign != rhs.sign)
|
2007-08-24 00:56:33 +00:00
|
|
|
return false;
|
2007-08-31 04:03:46 +00:00
|
|
|
if (category==fcZero || category==fcInfinity)
|
2007-08-24 00:56:33 +00:00
|
|
|
return true;
|
2007-08-31 04:03:46 +00:00
|
|
|
else if (category==fcNormal && exponent!=rhs.exponent)
|
|
|
|
return false;
|
2007-08-24 00:56:33 +00:00
|
|
|
else {
|
|
|
|
int i= partCount();
|
|
|
|
const integerPart* p=significandParts();
|
|
|
|
const integerPart* q=rhs.significandParts();
|
|
|
|
for (; i>0; i--, p++, q++) {
|
|
|
|
if (*p != *q)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-20 22:49:32 +00:00
|
|
|
APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
|
|
|
|
{
|
|
|
|
initialize(&ourSemantics);
|
|
|
|
sign = 0;
|
|
|
|
zeroSignificand();
|
|
|
|
exponent = ourSemantics.precision - 1;
|
|
|
|
significandParts()[0] = value;
|
|
|
|
normalize(rmNearestTiesToEven, lfExactlyZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::APFloat(const fltSemantics &ourSemantics,
|
2007-09-26 21:33:42 +00:00
|
|
|
fltCategory ourCategory, bool negative)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
initialize(&ourSemantics);
|
|
|
|
category = ourCategory;
|
|
|
|
sign = negative;
|
|
|
|
if(category == fcNormal)
|
|
|
|
category = fcZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
|
|
|
|
{
|
|
|
|
initialize(&ourSemantics);
|
|
|
|
convertFromString(text, rmNearestTiesToEven);
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::APFloat(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
initialize(rhs.semantics);
|
|
|
|
assign(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::~APFloat()
|
|
|
|
{
|
|
|
|
freeSignificand();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
APFloat::partCount() const
|
|
|
|
{
|
2007-09-20 23:47:58 +00:00
|
|
|
return partCountForBits(semantics->precision + 1);
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
APFloat::semanticsPrecision(const fltSemantics &semantics)
|
|
|
|
{
|
|
|
|
return semantics.precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
const integerPart *
|
|
|
|
APFloat::significandParts() const
|
|
|
|
{
|
|
|
|
return const_cast<APFloat *>(this)->significandParts();
|
|
|
|
}
|
|
|
|
|
|
|
|
integerPart *
|
|
|
|
APFloat::significandParts()
|
|
|
|
{
|
2007-08-31 04:03:46 +00:00
|
|
|
assert(category == fcNormal || category == fcNaN);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
if(partCount() > 1)
|
|
|
|
return significand.parts;
|
|
|
|
else
|
|
|
|
return &significand.part;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APFloat::zeroSignificand()
|
|
|
|
{
|
|
|
|
category = fcNormal;
|
|
|
|
APInt::tcSet(significandParts(), 0, partCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment an fcNormal floating point number's significand. */
|
|
|
|
void
|
|
|
|
APFloat::incrementSignificand()
|
|
|
|
{
|
|
|
|
integerPart carry;
|
|
|
|
|
|
|
|
carry = APInt::tcIncrement(significandParts(), partCount());
|
|
|
|
|
|
|
|
/* Our callers should never cause us to overflow. */
|
|
|
|
assert(carry == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the significand of the RHS. Returns the carry flag. */
|
|
|
|
integerPart
|
|
|
|
APFloat::addSignificand(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
integerPart *parts;
|
|
|
|
|
|
|
|
parts = significandParts();
|
|
|
|
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
assert(exponent == rhs.exponent);
|
|
|
|
|
|
|
|
return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Subtract the significand of the RHS with a borrow flag. Returns
|
|
|
|
the borrow flag. */
|
|
|
|
integerPart
|
|
|
|
APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
|
|
|
|
{
|
|
|
|
integerPart *parts;
|
|
|
|
|
|
|
|
parts = significandParts();
|
|
|
|
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
assert(exponent == rhs.exponent);
|
|
|
|
|
|
|
|
return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
|
2007-09-26 21:33:42 +00:00
|
|
|
partCount());
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
|
|
|
|
on to the full-precision result of the multiplication. Returns the
|
|
|
|
lost fraction. */
|
|
|
|
lostFraction
|
|
|
|
APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
|
|
|
|
{
|
2007-09-26 21:33:42 +00:00
|
|
|
unsigned int omsb; // One, not zero, based MSB.
|
2007-08-20 22:49:32 +00:00
|
|
|
unsigned int partsCount, newPartsCount, precision;
|
|
|
|
integerPart *lhsSignificand;
|
|
|
|
integerPart scratch[4];
|
|
|
|
integerPart *fullSignificand;
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
|
|
|
|
precision = semantics->precision;
|
|
|
|
newPartsCount = partCountForBits(precision * 2);
|
|
|
|
|
|
|
|
if(newPartsCount > 4)
|
|
|
|
fullSignificand = new integerPart[newPartsCount];
|
|
|
|
else
|
|
|
|
fullSignificand = scratch;
|
|
|
|
|
|
|
|
lhsSignificand = significandParts();
|
|
|
|
partsCount = partCount();
|
|
|
|
|
|
|
|
APInt::tcFullMultiply(fullSignificand, lhsSignificand,
|
2007-10-06 00:24:48 +00:00
|
|
|
rhs.significandParts(), partsCount, partsCount);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
lost_fraction = lfExactlyZero;
|
|
|
|
omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
|
|
|
|
exponent += rhs.exponent;
|
|
|
|
|
|
|
|
if(addend) {
|
|
|
|
Significand savedSignificand = significand;
|
|
|
|
const fltSemantics *savedSemantics = semantics;
|
|
|
|
fltSemantics extendedSemantics;
|
|
|
|
opStatus status;
|
|
|
|
unsigned int extendedPrecision;
|
|
|
|
|
|
|
|
/* Normalize our MSB. */
|
|
|
|
extendedPrecision = precision + precision - 1;
|
|
|
|
if(omsb != extendedPrecision)
|
|
|
|
{
|
2007-09-26 21:33:42 +00:00
|
|
|
APInt::tcShiftLeft(fullSignificand, newPartsCount,
|
|
|
|
extendedPrecision - omsb);
|
|
|
|
exponent -= extendedPrecision - omsb;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new semantics. */
|
|
|
|
extendedSemantics = *semantics;
|
|
|
|
extendedSemantics.precision = extendedPrecision;
|
|
|
|
|
|
|
|
if(newPartsCount == 1)
|
|
|
|
significand.part = fullSignificand[0];
|
|
|
|
else
|
|
|
|
significand.parts = fullSignificand;
|
|
|
|
semantics = &extendedSemantics;
|
|
|
|
|
|
|
|
APFloat extendedAddend(*addend);
|
|
|
|
status = extendedAddend.convert(extendedSemantics, rmTowardZero);
|
|
|
|
assert(status == opOK);
|
|
|
|
lost_fraction = addOrSubtractSignificand(extendedAddend, false);
|
|
|
|
|
|
|
|
/* Restore our state. */
|
|
|
|
if(newPartsCount == 1)
|
|
|
|
fullSignificand[0] = significand.part;
|
|
|
|
significand = savedSignificand;
|
|
|
|
semantics = savedSemantics;
|
|
|
|
|
|
|
|
omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
exponent -= (precision - 1);
|
|
|
|
|
|
|
|
if(omsb > precision) {
|
|
|
|
unsigned int bits, significantParts;
|
|
|
|
lostFraction lf;
|
|
|
|
|
|
|
|
bits = omsb - precision;
|
|
|
|
significantParts = partCountForBits(omsb);
|
|
|
|
lf = shiftRight(fullSignificand, significantParts, bits);
|
|
|
|
lost_fraction = combineLostFractions(lf, lost_fraction);
|
|
|
|
exponent += bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
|
|
|
|
|
|
|
|
if(newPartsCount > 4)
|
|
|
|
delete [] fullSignificand;
|
|
|
|
|
|
|
|
return lost_fraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Multiply the significands of LHS and RHS to DST. */
|
|
|
|
lostFraction
|
|
|
|
APFloat::divideSignificand(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
unsigned int bit, i, partsCount;
|
|
|
|
const integerPart *rhsSignificand;
|
|
|
|
integerPart *lhsSignificand, *dividend, *divisor;
|
|
|
|
integerPart scratch[4];
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
|
|
|
|
lhsSignificand = significandParts();
|
|
|
|
rhsSignificand = rhs.significandParts();
|
|
|
|
partsCount = partCount();
|
|
|
|
|
|
|
|
if(partsCount > 2)
|
|
|
|
dividend = new integerPart[partsCount * 2];
|
|
|
|
else
|
|
|
|
dividend = scratch;
|
|
|
|
|
|
|
|
divisor = dividend + partsCount;
|
|
|
|
|
|
|
|
/* Copy the dividend and divisor as they will be modified in-place. */
|
|
|
|
for(i = 0; i < partsCount; i++) {
|
|
|
|
dividend[i] = lhsSignificand[i];
|
|
|
|
divisor[i] = rhsSignificand[i];
|
|
|
|
lhsSignificand[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
exponent -= rhs.exponent;
|
|
|
|
|
|
|
|
unsigned int precision = semantics->precision;
|
|
|
|
|
|
|
|
/* Normalize the divisor. */
|
|
|
|
bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
|
|
|
|
if(bit) {
|
|
|
|
exponent += bit;
|
|
|
|
APInt::tcShiftLeft(divisor, partsCount, bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normalize the dividend. */
|
|
|
|
bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
|
|
|
|
if(bit) {
|
|
|
|
exponent -= bit;
|
|
|
|
APInt::tcShiftLeft(dividend, partsCount, bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
|
|
|
|
exponent--;
|
|
|
|
APInt::tcShiftLeft(dividend, partsCount, 1);
|
|
|
|
assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Long division. */
|
|
|
|
for(bit = precision; bit; bit -= 1) {
|
|
|
|
if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
|
|
|
|
APInt::tcSubtract(dividend, divisor, 0, partsCount);
|
|
|
|
APInt::tcSetBit(lhsSignificand, bit - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt::tcShiftLeft(dividend, partsCount, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Figure out the lost fraction. */
|
|
|
|
int cmp = APInt::tcCompare(dividend, divisor, partsCount);
|
|
|
|
|
|
|
|
if(cmp > 0)
|
|
|
|
lost_fraction = lfMoreThanHalf;
|
|
|
|
else if(cmp == 0)
|
|
|
|
lost_fraction = lfExactlyHalf;
|
|
|
|
else if(APInt::tcIsZero(dividend, partsCount))
|
|
|
|
lost_fraction = lfExactlyZero;
|
|
|
|
else
|
|
|
|
lost_fraction = lfLessThanHalf;
|
|
|
|
|
|
|
|
if(partsCount > 2)
|
|
|
|
delete [] dividend;
|
|
|
|
|
|
|
|
return lost_fraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
APFloat::significandMSB() const
|
|
|
|
{
|
|
|
|
return APInt::tcMSB(significandParts(), partCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
APFloat::significandLSB() const
|
|
|
|
{
|
|
|
|
return APInt::tcLSB(significandParts(), partCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that a zero result is NOT normalized to fcZero. */
|
|
|
|
lostFraction
|
|
|
|
APFloat::shiftSignificandRight(unsigned int bits)
|
|
|
|
{
|
|
|
|
/* Our exponent should not overflow. */
|
|
|
|
assert((exponent_t) (exponent + bits) >= exponent);
|
|
|
|
|
|
|
|
exponent += bits;
|
|
|
|
|
|
|
|
return shiftRight(significandParts(), partCount(), bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shift the significand left BITS bits, subtract BITS from its exponent. */
|
|
|
|
void
|
|
|
|
APFloat::shiftSignificandLeft(unsigned int bits)
|
|
|
|
{
|
|
|
|
assert(bits < semantics->precision);
|
|
|
|
|
|
|
|
if(bits) {
|
|
|
|
unsigned int partsCount = partCount();
|
|
|
|
|
|
|
|
APInt::tcShiftLeft(significandParts(), partsCount, bits);
|
|
|
|
exponent -= bits;
|
|
|
|
|
|
|
|
assert(!APInt::tcIsZero(significandParts(), partsCount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::cmpResult
|
|
|
|
APFloat::compareAbsoluteValue(const APFloat &rhs) const
|
|
|
|
{
|
|
|
|
int compare;
|
|
|
|
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
assert(category == fcNormal);
|
|
|
|
assert(rhs.category == fcNormal);
|
|
|
|
|
|
|
|
compare = exponent - rhs.exponent;
|
|
|
|
|
|
|
|
/* If exponents are equal, do an unsigned bignum comparison of the
|
|
|
|
significands. */
|
|
|
|
if(compare == 0)
|
|
|
|
compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
|
2007-09-26 21:33:42 +00:00
|
|
|
partCount());
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
if(compare > 0)
|
|
|
|
return cmpGreaterThan;
|
|
|
|
else if(compare < 0)
|
|
|
|
return cmpLessThan;
|
|
|
|
else
|
|
|
|
return cmpEqual;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle overflow. Sign is preserved. We either become infinity or
|
|
|
|
the largest finite number. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::handleOverflow(roundingMode rounding_mode)
|
|
|
|
{
|
|
|
|
/* Infinity? */
|
|
|
|
if(rounding_mode == rmNearestTiesToEven
|
|
|
|
|| rounding_mode == rmNearestTiesToAway
|
|
|
|
|| (rounding_mode == rmTowardPositive && !sign)
|
|
|
|
|| (rounding_mode == rmTowardNegative && sign))
|
|
|
|
{
|
|
|
|
category = fcInfinity;
|
|
|
|
return (opStatus) (opOverflow | opInexact);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise we become the largest finite number. */
|
|
|
|
category = fcNormal;
|
|
|
|
exponent = semantics->maxExponent;
|
|
|
|
APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
|
2007-09-26 21:33:42 +00:00
|
|
|
semantics->precision);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
return opInexact;
|
|
|
|
}
|
|
|
|
|
2007-10-03 15:16:41 +00:00
|
|
|
/* Returns TRUE if, when truncating the current number, with BIT the
|
|
|
|
new LSB, with the given lost fraction and rounding mode, the result
|
|
|
|
would need to be rounded away from zero (i.e., by increasing the
|
|
|
|
signficand). This routine must work for fcZero of both signs, and
|
|
|
|
fcNormal numbers. */
|
2007-08-20 22:49:32 +00:00
|
|
|
bool
|
|
|
|
APFloat::roundAwayFromZero(roundingMode rounding_mode,
|
2007-10-03 15:16:41 +00:00
|
|
|
lostFraction lost_fraction,
|
|
|
|
unsigned int bit) const
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
2007-08-31 04:03:46 +00:00
|
|
|
/* NaNs and infinities should not have lost fractions. */
|
2007-08-20 22:49:32 +00:00
|
|
|
assert(category == fcNormal || category == fcZero);
|
|
|
|
|
2007-10-03 15:16:41 +00:00
|
|
|
/* Current callers never pass this so we don't handle it. */
|
2007-08-20 22:49:32 +00:00
|
|
|
assert(lost_fraction != lfExactlyZero);
|
|
|
|
|
|
|
|
switch(rounding_mode) {
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
case rmNearestTiesToAway:
|
|
|
|
return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
|
|
|
|
|
|
|
|
case rmNearestTiesToEven:
|
|
|
|
if(lost_fraction == lfMoreThanHalf)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Our zeroes don't have a significand to test. */
|
|
|
|
if(lost_fraction == lfExactlyHalf && category != fcZero)
|
2007-10-03 15:16:41 +00:00
|
|
|
return APInt::tcExtractBit(significandParts(), bit);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case rmTowardZero:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case rmTowardPositive:
|
|
|
|
return sign == false;
|
|
|
|
|
|
|
|
case rmTowardNegative:
|
|
|
|
return sign == true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::normalize(roundingMode rounding_mode,
|
2007-09-26 21:33:42 +00:00
|
|
|
lostFraction lost_fraction)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
2007-09-26 21:33:42 +00:00
|
|
|
unsigned int omsb; /* One, not zero, based MSB. */
|
2007-08-20 22:49:32 +00:00
|
|
|
int exponentChange;
|
|
|
|
|
|
|
|
if(category != fcNormal)
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
/* Before rounding normalize the exponent of fcNormal numbers. */
|
|
|
|
omsb = significandMSB() + 1;
|
|
|
|
|
|
|
|
if(omsb) {
|
|
|
|
/* OMSB is numbered from 1. We want to place it in the integer
|
|
|
|
bit numbered PRECISON if possible, with a compensating change in
|
|
|
|
the exponent. */
|
|
|
|
exponentChange = omsb - semantics->precision;
|
|
|
|
|
|
|
|
/* If the resulting exponent is too high, overflow according to
|
|
|
|
the rounding mode. */
|
|
|
|
if(exponent + exponentChange > semantics->maxExponent)
|
|
|
|
return handleOverflow(rounding_mode);
|
|
|
|
|
|
|
|
/* Subnormal numbers have exponent minExponent, and their MSB
|
|
|
|
is forced based on that. */
|
|
|
|
if(exponent + exponentChange < semantics->minExponent)
|
|
|
|
exponentChange = semantics->minExponent - exponent;
|
|
|
|
|
|
|
|
/* Shifting left is easy as we don't lose precision. */
|
|
|
|
if(exponentChange < 0) {
|
|
|
|
assert(lost_fraction == lfExactlyZero);
|
|
|
|
|
|
|
|
shiftSignificandLeft(-exponentChange);
|
|
|
|
|
|
|
|
return opOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(exponentChange > 0) {
|
|
|
|
lostFraction lf;
|
|
|
|
|
|
|
|
/* Shift right and capture any new lost fraction. */
|
|
|
|
lf = shiftSignificandRight(exponentChange);
|
|
|
|
|
|
|
|
lost_fraction = combineLostFractions(lf, lost_fraction);
|
|
|
|
|
|
|
|
/* Keep OMSB up-to-date. */
|
|
|
|
if(omsb > (unsigned) exponentChange)
|
2007-09-26 21:33:42 +00:00
|
|
|
omsb -= (unsigned) exponentChange;
|
2007-08-20 22:49:32 +00:00
|
|
|
else
|
2007-09-26 21:33:42 +00:00
|
|
|
omsb = 0;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now round the number according to rounding_mode given the lost
|
|
|
|
fraction. */
|
|
|
|
|
|
|
|
/* As specified in IEEE 754, since we do not trap we do not report
|
|
|
|
underflow for exact results. */
|
|
|
|
if(lost_fraction == lfExactlyZero) {
|
|
|
|
/* Canonicalize zeroes. */
|
|
|
|
if(omsb == 0)
|
|
|
|
category = fcZero;
|
|
|
|
|
|
|
|
return opOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the significand if we're rounding away from zero. */
|
2007-10-03 15:16:41 +00:00
|
|
|
if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
|
2007-08-20 22:49:32 +00:00
|
|
|
if(omsb == 0)
|
|
|
|
exponent = semantics->minExponent;
|
|
|
|
|
|
|
|
incrementSignificand();
|
|
|
|
omsb = significandMSB() + 1;
|
|
|
|
|
|
|
|
/* Did the significand increment overflow? */
|
|
|
|
if(omsb == (unsigned) semantics->precision + 1) {
|
|
|
|
/* Renormalize by incrementing the exponent and shifting our
|
2007-09-26 21:33:42 +00:00
|
|
|
significand right one. However if we already have the
|
|
|
|
maximum exponent we overflow to infinity. */
|
2007-08-20 22:49:32 +00:00
|
|
|
if(exponent == semantics->maxExponent) {
|
2007-09-26 21:33:42 +00:00
|
|
|
category = fcInfinity;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
return (opStatus) (opOverflow | opInexact);
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shiftSignificandRight(1);
|
|
|
|
|
|
|
|
return opInexact;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The normal case - we were and are not denormal, and any
|
|
|
|
significand increment above didn't overflow. */
|
|
|
|
if(omsb == semantics->precision)
|
|
|
|
return opInexact;
|
|
|
|
|
|
|
|
/* We have a non-zero denormal. */
|
|
|
|
assert(omsb < semantics->precision);
|
|
|
|
assert(exponent == semantics->minExponent);
|
|
|
|
|
|
|
|
/* Canonicalize zeroes. */
|
|
|
|
if(omsb == 0)
|
|
|
|
category = fcZero;
|
|
|
|
|
|
|
|
/* The fcZero case is a denormal that underflowed to zero. */
|
|
|
|
return (opStatus) (opUnderflow | opInexact);
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
|
|
|
|
{
|
|
|
|
switch(convolve(category, rhs.category)) {
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
case convolve(fcNaN, fcZero):
|
|
|
|
case convolve(fcNaN, fcNormal):
|
|
|
|
case convolve(fcNaN, fcInfinity):
|
|
|
|
case convolve(fcNaN, fcNaN):
|
2007-08-20 22:49:32 +00:00
|
|
|
case convolve(fcNormal, fcZero):
|
|
|
|
case convolve(fcInfinity, fcNormal):
|
|
|
|
case convolve(fcInfinity, fcZero):
|
|
|
|
return opOK;
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
case convolve(fcZero, fcNaN):
|
|
|
|
case convolve(fcNormal, fcNaN):
|
|
|
|
case convolve(fcInfinity, fcNaN):
|
|
|
|
category = fcNaN;
|
|
|
|
copySignificand(rhs);
|
2007-08-20 22:49:32 +00:00
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcInfinity):
|
|
|
|
case convolve(fcZero, fcInfinity):
|
|
|
|
category = fcInfinity;
|
|
|
|
sign = rhs.sign ^ subtract;
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcZero, fcNormal):
|
|
|
|
assign(rhs);
|
|
|
|
sign = rhs.sign ^ subtract;
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcZero, fcZero):
|
|
|
|
/* Sign depends on rounding mode; handled by caller. */
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcInfinity, fcInfinity):
|
|
|
|
/* Differently signed infinities can only be validly
|
|
|
|
subtracted. */
|
|
|
|
if(sign ^ rhs.sign != subtract) {
|
2007-08-31 04:03:46 +00:00
|
|
|
category = fcNaN;
|
|
|
|
// Arbitrary but deterministic value for significand
|
|
|
|
APInt::tcSet(significandParts(), ~0U, partCount());
|
2007-08-20 22:49:32 +00:00
|
|
|
return opInvalidOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcNormal):
|
|
|
|
return opDivByZero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add or subtract two normal numbers. */
|
|
|
|
lostFraction
|
|
|
|
APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
|
|
|
|
{
|
|
|
|
integerPart carry;
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
int bits;
|
|
|
|
|
|
|
|
/* Determine if the operation on the absolute values is effectively
|
|
|
|
an addition or subtraction. */
|
|
|
|
subtract ^= (sign ^ rhs.sign);
|
|
|
|
|
|
|
|
/* Are we bigger exponent-wise than the RHS? */
|
|
|
|
bits = exponent - rhs.exponent;
|
|
|
|
|
|
|
|
/* Subtraction is more subtle than one might naively expect. */
|
|
|
|
if(subtract) {
|
|
|
|
APFloat temp_rhs(rhs);
|
|
|
|
bool reverse;
|
|
|
|
|
2007-08-24 03:02:34 +00:00
|
|
|
if (bits == 0) {
|
2007-08-20 22:49:32 +00:00
|
|
|
reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
|
|
|
|
lost_fraction = lfExactlyZero;
|
2007-08-24 03:02:34 +00:00
|
|
|
} else if (bits > 0) {
|
2007-08-20 22:49:32 +00:00
|
|
|
lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
|
|
|
|
shiftSignificandLeft(1);
|
|
|
|
reverse = false;
|
2007-08-24 03:02:34 +00:00
|
|
|
} else {
|
2007-08-20 22:49:32 +00:00
|
|
|
lost_fraction = shiftSignificandRight(-bits - 1);
|
|
|
|
temp_rhs.shiftSignificandLeft(1);
|
|
|
|
reverse = true;
|
|
|
|
}
|
|
|
|
|
2007-08-24 03:02:34 +00:00
|
|
|
if (reverse) {
|
2007-08-20 22:49:32 +00:00
|
|
|
carry = temp_rhs.subtractSignificand
|
2007-09-26 21:33:42 +00:00
|
|
|
(*this, lost_fraction != lfExactlyZero);
|
2007-08-20 22:49:32 +00:00
|
|
|
copySignificand(temp_rhs);
|
|
|
|
sign = !sign;
|
|
|
|
} else {
|
|
|
|
carry = subtractSignificand
|
2007-09-26 21:33:42 +00:00
|
|
|
(temp_rhs, lost_fraction != lfExactlyZero);
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Invert the lost fraction - it was on the RHS and
|
|
|
|
subtracted. */
|
|
|
|
if(lost_fraction == lfLessThanHalf)
|
|
|
|
lost_fraction = lfMoreThanHalf;
|
|
|
|
else if(lost_fraction == lfMoreThanHalf)
|
|
|
|
lost_fraction = lfLessThanHalf;
|
|
|
|
|
|
|
|
/* The code above is intended to ensure that no borrow is
|
|
|
|
necessary. */
|
|
|
|
assert(!carry);
|
|
|
|
} else {
|
|
|
|
if(bits > 0) {
|
|
|
|
APFloat temp_rhs(rhs);
|
|
|
|
|
|
|
|
lost_fraction = temp_rhs.shiftSignificandRight(bits);
|
|
|
|
carry = addSignificand(temp_rhs);
|
|
|
|
} else {
|
|
|
|
lost_fraction = shiftSignificandRight(-bits);
|
|
|
|
carry = addSignificand(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a guard bit; generating a carry cannot happen. */
|
|
|
|
assert(!carry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lost_fraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::multiplySpecials(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
switch(convolve(category, rhs.category)) {
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
case convolve(fcNaN, fcZero):
|
|
|
|
case convolve(fcNaN, fcNormal):
|
|
|
|
case convolve(fcNaN, fcInfinity):
|
|
|
|
case convolve(fcNaN, fcNaN):
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcZero, fcNaN):
|
|
|
|
case convolve(fcNormal, fcNaN):
|
|
|
|
case convolve(fcInfinity, fcNaN):
|
|
|
|
category = fcNaN;
|
|
|
|
copySignificand(rhs);
|
2007-08-20 22:49:32 +00:00
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcInfinity):
|
|
|
|
case convolve(fcInfinity, fcNormal):
|
|
|
|
case convolve(fcInfinity, fcInfinity):
|
|
|
|
category = fcInfinity;
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcZero, fcNormal):
|
|
|
|
case convolve(fcNormal, fcZero):
|
|
|
|
case convolve(fcZero, fcZero):
|
|
|
|
category = fcZero;
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcZero, fcInfinity):
|
|
|
|
case convolve(fcInfinity, fcZero):
|
2007-08-31 04:03:46 +00:00
|
|
|
category = fcNaN;
|
|
|
|
// Arbitrary but deterministic value for significand
|
|
|
|
APInt::tcSet(significandParts(), ~0U, partCount());
|
2007-08-20 22:49:32 +00:00
|
|
|
return opInvalidOp;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcNormal):
|
|
|
|
return opOK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::divideSpecials(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
switch(convolve(category, rhs.category)) {
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
case convolve(fcNaN, fcZero):
|
|
|
|
case convolve(fcNaN, fcNormal):
|
|
|
|
case convolve(fcNaN, fcInfinity):
|
|
|
|
case convolve(fcNaN, fcNaN):
|
2007-08-20 22:49:32 +00:00
|
|
|
case convolve(fcInfinity, fcZero):
|
|
|
|
case convolve(fcInfinity, fcNormal):
|
|
|
|
case convolve(fcZero, fcInfinity):
|
|
|
|
case convolve(fcZero, fcNormal):
|
|
|
|
return opOK;
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
case convolve(fcZero, fcNaN):
|
|
|
|
case convolve(fcNormal, fcNaN):
|
|
|
|
case convolve(fcInfinity, fcNaN):
|
|
|
|
category = fcNaN;
|
|
|
|
copySignificand(rhs);
|
2007-08-20 22:49:32 +00:00
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcInfinity):
|
|
|
|
category = fcZero;
|
|
|
|
return opOK;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcZero):
|
|
|
|
category = fcInfinity;
|
|
|
|
return opDivByZero;
|
|
|
|
|
|
|
|
case convolve(fcInfinity, fcInfinity):
|
|
|
|
case convolve(fcZero, fcZero):
|
2007-08-31 04:03:46 +00:00
|
|
|
category = fcNaN;
|
|
|
|
// Arbitrary but deterministic value for significand
|
|
|
|
APInt::tcSet(significandParts(), ~0U, partCount());
|
2007-08-20 22:49:32 +00:00
|
|
|
return opInvalidOp;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcNormal):
|
|
|
|
return opOK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change sign. */
|
|
|
|
void
|
|
|
|
APFloat::changeSign()
|
|
|
|
{
|
|
|
|
/* Look mummy, this one's easy. */
|
|
|
|
sign = !sign;
|
|
|
|
}
|
|
|
|
|
2007-08-31 23:35:31 +00:00
|
|
|
void
|
|
|
|
APFloat::clearSign()
|
|
|
|
{
|
|
|
|
/* So is this one. */
|
|
|
|
sign = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APFloat::copySign(const APFloat &rhs)
|
|
|
|
{
|
|
|
|
/* And this one. */
|
|
|
|
sign = rhs.sign;
|
|
|
|
}
|
|
|
|
|
2007-08-20 22:49:32 +00:00
|
|
|
/* Normalized addition or subtraction. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
|
2007-09-26 21:33:42 +00:00
|
|
|
bool subtract)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
opStatus fs;
|
|
|
|
|
|
|
|
fs = addOrSubtractSpecials(rhs, subtract);
|
|
|
|
|
|
|
|
/* This return code means it was not a simple case. */
|
|
|
|
if(fs == opDivByZero) {
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
|
|
|
|
lost_fraction = addOrSubtractSignificand(rhs, subtract);
|
|
|
|
fs = normalize(rounding_mode, lost_fraction);
|
|
|
|
|
|
|
|
/* Can only be zero if we lost no fraction. */
|
|
|
|
assert(category != fcZero || lost_fraction == lfExactlyZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
|
|
|
|
positive zero unless rounding to minus infinity, except that
|
|
|
|
adding two like-signed zeroes gives that zero. */
|
|
|
|
if(category == fcZero) {
|
|
|
|
if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
|
|
|
|
sign = (rounding_mode == rmTowardNegative);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normalized addition. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
|
|
|
|
{
|
|
|
|
return addOrSubtract(rhs, rounding_mode, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normalized subtraction. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
|
|
|
|
{
|
|
|
|
return addOrSubtract(rhs, rounding_mode, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normalized multiply. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
|
|
|
|
{
|
|
|
|
opStatus fs;
|
|
|
|
|
|
|
|
sign ^= rhs.sign;
|
|
|
|
fs = multiplySpecials(rhs);
|
|
|
|
|
|
|
|
if(category == fcNormal) {
|
|
|
|
lostFraction lost_fraction = multiplySignificand(rhs, 0);
|
|
|
|
fs = normalize(rounding_mode, lost_fraction);
|
|
|
|
if(lost_fraction != lfExactlyZero)
|
|
|
|
fs = (opStatus) (fs | opInexact);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normalized divide. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
|
|
|
|
{
|
|
|
|
opStatus fs;
|
|
|
|
|
|
|
|
sign ^= rhs.sign;
|
|
|
|
fs = divideSpecials(rhs);
|
|
|
|
|
|
|
|
if(category == fcNormal) {
|
|
|
|
lostFraction lost_fraction = divideSignificand(rhs);
|
|
|
|
fs = normalize(rounding_mode, lost_fraction);
|
|
|
|
if(lost_fraction != lfExactlyZero)
|
|
|
|
fs = (opStatus) (fs | opInexact);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
2007-10-03 22:26:02 +00:00
|
|
|
/* Normalized remainder. This is not currently doing TRT. */
|
2007-08-31 23:35:31 +00:00
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
|
|
|
|
{
|
|
|
|
opStatus fs;
|
|
|
|
APFloat V = *this;
|
2007-09-05 20:39:49 +00:00
|
|
|
unsigned int origSign = sign;
|
2007-08-31 23:35:31 +00:00
|
|
|
fs = V.divide(rhs, rmNearestTiesToEven);
|
|
|
|
if (fs == opDivByZero)
|
|
|
|
return fs;
|
|
|
|
|
2007-09-05 20:39:49 +00:00
|
|
|
int parts = partCount();
|
|
|
|
integerPart *x = new integerPart[parts];
|
2007-09-26 21:33:42 +00:00
|
|
|
fs = V.convertToInteger(x, parts * integerPartWidth, true,
|
2007-09-05 20:39:49 +00:00
|
|
|
rmNearestTiesToEven);
|
2007-08-31 23:35:31 +00:00
|
|
|
if (fs==opInvalidOp)
|
|
|
|
return fs;
|
|
|
|
|
2007-10-07 11:45:55 +00:00
|
|
|
fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
|
|
|
|
rmNearestTiesToEven);
|
2007-08-31 23:35:31 +00:00
|
|
|
assert(fs==opOK); // should always work
|
2007-09-05 20:39:49 +00:00
|
|
|
|
2007-08-31 23:35:31 +00:00
|
|
|
fs = V.multiply(rhs, rounding_mode);
|
2007-09-05 20:39:49 +00:00
|
|
|
assert(fs==opOK || fs==opInexact); // should not overflow or underflow
|
|
|
|
|
2007-08-31 23:35:31 +00:00
|
|
|
fs = subtract(V, rounding_mode);
|
2007-09-05 20:39:49 +00:00
|
|
|
assert(fs==opOK || fs==opInexact); // likewise
|
|
|
|
|
|
|
|
if (isZero())
|
|
|
|
sign = origSign; // IEEE754 requires this
|
|
|
|
delete[] x;
|
2007-08-31 23:35:31 +00:00
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
2007-08-20 22:49:32 +00:00
|
|
|
/* Normalized fused-multiply-add. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
|
2007-09-26 21:33:42 +00:00
|
|
|
const APFloat &addend,
|
|
|
|
roundingMode rounding_mode)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
opStatus fs;
|
|
|
|
|
|
|
|
/* Post-multiplication sign, before addition. */
|
|
|
|
sign ^= multiplicand.sign;
|
|
|
|
|
|
|
|
/* If and only if all arguments are normal do we need to do an
|
|
|
|
extended-precision calculation. */
|
|
|
|
if(category == fcNormal
|
|
|
|
&& multiplicand.category == fcNormal
|
|
|
|
&& addend.category == fcNormal) {
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
|
|
|
|
lost_fraction = multiplySignificand(multiplicand, &addend);
|
|
|
|
fs = normalize(rounding_mode, lost_fraction);
|
|
|
|
if(lost_fraction != lfExactlyZero)
|
|
|
|
fs = (opStatus) (fs | opInexact);
|
|
|
|
|
|
|
|
/* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
|
|
|
|
positive zero unless rounding to minus infinity, except that
|
|
|
|
adding two like-signed zeroes gives that zero. */
|
|
|
|
if(category == fcZero && sign != addend.sign)
|
|
|
|
sign = (rounding_mode == rmTowardNegative);
|
|
|
|
} else {
|
|
|
|
fs = multiplySpecials(multiplicand);
|
|
|
|
|
|
|
|
/* FS can only be opOK or opInvalidOp. There is no more work
|
|
|
|
to do in the latter case. The IEEE-754R standard says it is
|
|
|
|
implementation-defined in this case whether, if ADDEND is a
|
2007-08-31 04:03:46 +00:00
|
|
|
quiet NaN, we raise invalid op; this implementation does so.
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
If we need to do the addition we can do so with normal
|
|
|
|
precision. */
|
|
|
|
if(fs == opOK)
|
|
|
|
fs = addOrSubtract(addend, rounding_mode, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comparison requires normalized numbers. */
|
|
|
|
APFloat::cmpResult
|
|
|
|
APFloat::compare(const APFloat &rhs) const
|
|
|
|
{
|
|
|
|
cmpResult result;
|
|
|
|
|
|
|
|
assert(semantics == rhs.semantics);
|
|
|
|
|
|
|
|
switch(convolve(category, rhs.category)) {
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
case convolve(fcNaN, fcZero):
|
|
|
|
case convolve(fcNaN, fcNormal):
|
|
|
|
case convolve(fcNaN, fcInfinity):
|
|
|
|
case convolve(fcNaN, fcNaN):
|
|
|
|
case convolve(fcZero, fcNaN):
|
|
|
|
case convolve(fcNormal, fcNaN):
|
|
|
|
case convolve(fcInfinity, fcNaN):
|
2007-08-20 22:49:32 +00:00
|
|
|
return cmpUnordered;
|
|
|
|
|
|
|
|
case convolve(fcInfinity, fcNormal):
|
|
|
|
case convolve(fcInfinity, fcZero):
|
|
|
|
case convolve(fcNormal, fcZero):
|
|
|
|
if(sign)
|
|
|
|
return cmpLessThan;
|
|
|
|
else
|
|
|
|
return cmpGreaterThan;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcInfinity):
|
|
|
|
case convolve(fcZero, fcInfinity):
|
|
|
|
case convolve(fcZero, fcNormal):
|
|
|
|
if(rhs.sign)
|
|
|
|
return cmpGreaterThan;
|
|
|
|
else
|
|
|
|
return cmpLessThan;
|
|
|
|
|
|
|
|
case convolve(fcInfinity, fcInfinity):
|
|
|
|
if(sign == rhs.sign)
|
|
|
|
return cmpEqual;
|
|
|
|
else if(sign)
|
|
|
|
return cmpLessThan;
|
|
|
|
else
|
|
|
|
return cmpGreaterThan;
|
|
|
|
|
|
|
|
case convolve(fcZero, fcZero):
|
|
|
|
return cmpEqual;
|
|
|
|
|
|
|
|
case convolve(fcNormal, fcNormal):
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Two normal numbers. Do they have the same sign? */
|
|
|
|
if(sign != rhs.sign) {
|
|
|
|
if(sign)
|
|
|
|
result = cmpLessThan;
|
|
|
|
else
|
|
|
|
result = cmpGreaterThan;
|
|
|
|
} else {
|
|
|
|
/* Compare absolute values; invert result if negative. */
|
|
|
|
result = compareAbsoluteValue(rhs);
|
|
|
|
|
|
|
|
if(sign) {
|
|
|
|
if(result == cmpLessThan)
|
2007-09-26 21:33:42 +00:00
|
|
|
result = cmpGreaterThan;
|
2007-08-20 22:49:32 +00:00
|
|
|
else if(result == cmpGreaterThan)
|
2007-09-26 21:33:42 +00:00
|
|
|
result = cmpLessThan;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::convert(const fltSemantics &toSemantics,
|
2007-09-26 21:33:42 +00:00
|
|
|
roundingMode rounding_mode)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
2007-09-22 02:56:19 +00:00
|
|
|
lostFraction lostFraction;
|
|
|
|
unsigned int newPartCount, oldPartCount;
|
2007-08-20 22:49:32 +00:00
|
|
|
opStatus fs;
|
2007-09-26 21:33:42 +00:00
|
|
|
|
2007-09-22 02:56:19 +00:00
|
|
|
lostFraction = lfExactlyZero;
|
2007-08-20 22:49:32 +00:00
|
|
|
newPartCount = partCountForBits(toSemantics.precision + 1);
|
2007-09-22 02:56:19 +00:00
|
|
|
oldPartCount = partCount();
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2007-09-22 02:56:19 +00:00
|
|
|
/* Handle storage complications. If our new form is wider,
|
|
|
|
re-allocate our bit pattern into wider storage. If it is
|
|
|
|
narrower, we ignore the excess parts, but if narrowing to a
|
2007-09-25 17:25:00 +00:00
|
|
|
single part we need to free the old storage.
|
|
|
|
Be careful not to reference significandParts for zeroes
|
|
|
|
and infinities, since it aborts. */
|
2007-09-22 02:56:19 +00:00
|
|
|
if (newPartCount > oldPartCount) {
|
2007-08-20 22:49:32 +00:00
|
|
|
integerPart *newParts;
|
|
|
|
newParts = new integerPart[newPartCount];
|
|
|
|
APInt::tcSet(newParts, 0, newPartCount);
|
2007-09-25 17:25:00 +00:00
|
|
|
if (category==fcNormal || category==fcNaN)
|
|
|
|
APInt::tcAssign(newParts, significandParts(), oldPartCount);
|
2007-08-20 22:49:32 +00:00
|
|
|
freeSignificand();
|
|
|
|
significand.parts = newParts;
|
2007-09-22 02:56:19 +00:00
|
|
|
} else if (newPartCount < oldPartCount) {
|
|
|
|
/* Capture any lost fraction through truncation of parts so we get
|
|
|
|
correct rounding whilst normalizing. */
|
2007-09-25 17:25:00 +00:00
|
|
|
if (category==fcNormal)
|
|
|
|
lostFraction = lostFractionThroughTruncation
|
|
|
|
(significandParts(), oldPartCount, toSemantics.precision);
|
|
|
|
if (newPartCount == 1) {
|
|
|
|
integerPart newPart = 0;
|
2007-09-26 21:33:42 +00:00
|
|
|
if (category==fcNormal || category==fcNaN)
|
2007-09-25 17:25:00 +00:00
|
|
|
newPart = significandParts()[0];
|
|
|
|
freeSignificand();
|
|
|
|
significand.part = newPart;
|
|
|
|
}
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(category == fcNormal) {
|
|
|
|
/* Re-interpret our bit-pattern. */
|
|
|
|
exponent += toSemantics.precision - semantics->precision;
|
|
|
|
semantics = &toSemantics;
|
2007-09-22 02:56:19 +00:00
|
|
|
fs = normalize(rounding_mode, lostFraction);
|
2007-09-25 17:25:00 +00:00
|
|
|
} else if (category == fcNaN) {
|
|
|
|
int shift = toSemantics.precision - semantics->precision;
|
|
|
|
// No normalization here, just truncate
|
|
|
|
if (shift>0)
|
|
|
|
APInt::tcShiftLeft(significandParts(), newPartCount, shift);
|
|
|
|
else if (shift < 0)
|
|
|
|
APInt::tcShiftRight(significandParts(), newPartCount, -shift);
|
|
|
|
// 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
|
|
|
|
// an invalid operation signal at runtime, but nobody does that.
|
|
|
|
semantics = &toSemantics;
|
|
|
|
fs = opOK;
|
2007-08-20 22:49:32 +00:00
|
|
|
} else {
|
|
|
|
semantics = &toSemantics;
|
|
|
|
fs = opOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a floating point number to an integer according to the
|
|
|
|
rounding mode. If the rounded integer value is out of range this
|
|
|
|
returns an invalid operation exception. If the rounded value is in
|
|
|
|
range but the floating point number is not the exact integer, the C
|
|
|
|
standard doesn't require an inexact exception to be raised. IEEE
|
|
|
|
854 does require it so we do that.
|
|
|
|
|
|
|
|
Note that for conversions to integer type the C standard requires
|
|
|
|
round-to-zero to always be used. */
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::convertToInteger(integerPart *parts, unsigned int width,
|
2007-09-26 21:33:42 +00:00
|
|
|
bool isSigned,
|
|
|
|
roundingMode rounding_mode) const
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
unsigned int msb, partsCount;
|
|
|
|
int bits;
|
|
|
|
|
|
|
|
partsCount = partCountForBits(width);
|
|
|
|
|
2007-09-25 23:07:07 +00:00
|
|
|
/* Handle the three special cases first. We produce
|
|
|
|
a deterministic result even for the Invalid cases. */
|
|
|
|
if (category == fcNaN) {
|
|
|
|
// Neither sign nor isSigned affects this.
|
|
|
|
APInt::tcSet(parts, 0, partsCount);
|
|
|
|
return opInvalidOp;
|
|
|
|
}
|
|
|
|
if (category == fcInfinity) {
|
|
|
|
if (!sign && isSigned)
|
|
|
|
APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
|
|
|
|
else if (!sign && !isSigned)
|
|
|
|
APInt::tcSetLeastSignificantBits(parts, partsCount, width);
|
|
|
|
else if (sign && isSigned) {
|
|
|
|
APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
|
|
|
|
APInt::tcShiftLeft(parts, partsCount, width-1);
|
|
|
|
} else // sign && !isSigned
|
|
|
|
APInt::tcSet(parts, 0, partsCount);
|
|
|
|
return opInvalidOp;
|
|
|
|
}
|
|
|
|
if (category == fcZero) {
|
2007-08-20 22:49:32 +00:00
|
|
|
APInt::tcSet(parts, 0, partsCount);
|
|
|
|
return opOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shift the bit pattern so the fraction is lost. */
|
|
|
|
APFloat tmp(*this);
|
|
|
|
|
|
|
|
bits = (int) semantics->precision - 1 - exponent;
|
|
|
|
|
|
|
|
if(bits > 0) {
|
|
|
|
lost_fraction = tmp.shiftSignificandRight(bits);
|
|
|
|
} else {
|
2007-09-25 23:07:07 +00:00
|
|
|
if (-bits >= semantics->precision) {
|
|
|
|
// Unrepresentably large.
|
|
|
|
if (!sign && isSigned)
|
|
|
|
APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
|
|
|
|
else if (!sign && !isSigned)
|
|
|
|
APInt::tcSetLeastSignificantBits(parts, partsCount, width);
|
|
|
|
else if (sign && isSigned) {
|
|
|
|
APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
|
|
|
|
APInt::tcShiftLeft(parts, partsCount, width-1);
|
|
|
|
} else // sign && !isSigned
|
|
|
|
APInt::tcSet(parts, 0, partsCount);
|
|
|
|
return (opStatus)(opOverflow | opInexact);
|
|
|
|
}
|
2007-08-20 22:49:32 +00:00
|
|
|
tmp.shiftSignificandLeft(-bits);
|
|
|
|
lost_fraction = lfExactlyZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lost_fraction != lfExactlyZero
|
2007-10-03 15:16:41 +00:00
|
|
|
&& tmp.roundAwayFromZero(rounding_mode, lost_fraction, 0))
|
2007-08-20 22:49:32 +00:00
|
|
|
tmp.incrementSignificand();
|
|
|
|
|
|
|
|
msb = tmp.significandMSB();
|
|
|
|
|
|
|
|
/* Negative numbers cannot be represented as unsigned. */
|
|
|
|
if(!isSigned && tmp.sign && msb != -1U)
|
|
|
|
return opInvalidOp;
|
|
|
|
|
|
|
|
/* It takes exponent + 1 bits to represent the truncated floating
|
|
|
|
point number without its sign. We lose a bit for the sign, but
|
|
|
|
the maximally negative integer is a special case. */
|
2007-09-26 21:33:42 +00:00
|
|
|
if(msb + 1 > width) /* !! Not same as msb >= width !! */
|
2007-08-20 22:49:32 +00:00
|
|
|
return opInvalidOp;
|
|
|
|
|
|
|
|
if(isSigned && msb + 1 == width
|
|
|
|
&& (!tmp.sign || tmp.significandLSB() != msb))
|
|
|
|
return opInvalidOp;
|
|
|
|
|
|
|
|
APInt::tcAssign(parts, tmp.significandParts(), partsCount);
|
|
|
|
|
|
|
|
if(tmp.sign)
|
|
|
|
APInt::tcNegate(parts, partsCount);
|
|
|
|
|
|
|
|
if(lost_fraction == lfExactlyZero)
|
|
|
|
return opOK;
|
|
|
|
else
|
|
|
|
return opInexact;
|
|
|
|
}
|
|
|
|
|
2007-10-07 12:07:53 +00:00
|
|
|
/* Convert an unsigned integer SRC to a floating point number,
|
|
|
|
rounding according to ROUNDING_MODE. The sign of the floating
|
|
|
|
point number is not modified. */
|
2007-08-20 22:49:32 +00:00
|
|
|
APFloat::opStatus
|
2007-10-07 12:07:53 +00:00
|
|
|
APFloat::convertFromUnsignedParts(const integerPart *src,
|
|
|
|
unsigned int srcCount,
|
|
|
|
roundingMode rounding_mode)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
2007-10-07 12:07:53 +00:00
|
|
|
unsigned int dstCount;
|
2007-08-20 22:49:32 +00:00
|
|
|
lostFraction lost_fraction;
|
2007-10-07 12:07:53 +00:00
|
|
|
integerPart *dst;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
category = fcNormal;
|
2007-10-07 12:07:53 +00:00
|
|
|
exponent = semantics->precision - 1;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2007-10-07 12:07:53 +00:00
|
|
|
dst = significandParts();
|
|
|
|
dstCount = partCount();
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2007-10-07 12:07:53 +00:00
|
|
|
/* We need to capture the non-zero most significant parts. */
|
|
|
|
while (srcCount > dstCount && src[srcCount - 1] == 0)
|
|
|
|
srcCount--;
|
|
|
|
|
|
|
|
/* Copy the bit image of as many parts as we can. If we are wider,
|
|
|
|
zero-out remaining parts. */
|
|
|
|
if (dstCount >= srcCount) {
|
|
|
|
APInt::tcAssign(dst, src, srcCount);
|
|
|
|
while (srcCount < dstCount)
|
|
|
|
dst[srcCount++] = 0;
|
|
|
|
lost_fraction = lfExactlyZero;
|
|
|
|
} else {
|
|
|
|
exponent += (srcCount - dstCount) * integerPartWidth;
|
|
|
|
APInt::tcAssign(dst, src + (srcCount - dstCount), dstCount);
|
|
|
|
lost_fraction = lostFractionThroughTruncation(src, srcCount,
|
|
|
|
dstCount * integerPartWidth);
|
|
|
|
}
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
return normalize(rounding_mode, lost_fraction);
|
|
|
|
}
|
|
|
|
|
2007-10-07 11:45:55 +00:00
|
|
|
/* FIXME: should this just take a const APInt reference? */
|
2007-08-20 22:49:32 +00:00
|
|
|
APFloat::opStatus
|
2007-10-07 11:45:55 +00:00
|
|
|
APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
|
|
|
|
unsigned int width, bool isSigned,
|
|
|
|
roundingMode rounding_mode)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
2007-09-21 22:09:37 +00:00
|
|
|
unsigned int partCount = partCountForBits(width);
|
|
|
|
APInt api = APInt(width, partCount, parts);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
sign = false;
|
2007-09-30 18:17:01 +00:00
|
|
|
if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
|
|
|
|
sign = true;
|
|
|
|
api = -api;
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
2007-10-07 12:10:57 +00:00
|
|
|
return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::convertFromHexadecimalString(const char *p,
|
2007-09-26 21:33:42 +00:00
|
|
|
roundingMode rounding_mode)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
integerPart *significand;
|
|
|
|
unsigned int bitPos, partsCount;
|
|
|
|
const char *dot, *firstSignificantDigit;
|
|
|
|
|
|
|
|
zeroSignificand();
|
|
|
|
exponent = 0;
|
|
|
|
category = fcNormal;
|
|
|
|
|
|
|
|
significand = significandParts();
|
|
|
|
partsCount = partCount();
|
|
|
|
bitPos = partsCount * integerPartWidth;
|
|
|
|
|
2007-10-07 08:51:21 +00:00
|
|
|
/* Skip leading zeroes and any (hexa)decimal point. */
|
2007-08-20 22:49:32 +00:00
|
|
|
p = skipLeadingZeroesAndAnyDot(p, &dot);
|
|
|
|
firstSignificantDigit = p;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
integerPart hex_value;
|
|
|
|
|
|
|
|
if(*p == '.') {
|
|
|
|
assert(dot == 0);
|
|
|
|
dot = p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
hex_value = hexDigitValue(*p);
|
|
|
|
if(hex_value == -1U) {
|
|
|
|
lost_fraction = lfExactlyZero;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Store the number whilst 4-bit nibbles remain. */
|
|
|
|
if(bitPos) {
|
|
|
|
bitPos -= 4;
|
|
|
|
hex_value <<= bitPos % integerPartWidth;
|
|
|
|
significand[bitPos / integerPartWidth] |= hex_value;
|
|
|
|
} else {
|
|
|
|
lost_fraction = trailingHexadecimalFraction(p, hex_value);
|
|
|
|
while(hexDigitValue(*p) != -1U)
|
2007-09-26 21:33:42 +00:00
|
|
|
p++;
|
2007-08-20 22:49:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hex floats require an exponent but not a hexadecimal point. */
|
|
|
|
assert(*p == 'p' || *p == 'P');
|
|
|
|
|
|
|
|
/* Ignore the exponent if we are zero. */
|
|
|
|
if(p != firstSignificantDigit) {
|
|
|
|
int expAdjustment;
|
|
|
|
|
|
|
|
/* Implicit hexadecimal point? */
|
|
|
|
if(!dot)
|
|
|
|
dot = p;
|
|
|
|
|
|
|
|
/* Calculate the exponent adjustment implicit in the number of
|
|
|
|
significant digits. */
|
|
|
|
expAdjustment = dot - firstSignificantDigit;
|
|
|
|
if(expAdjustment < 0)
|
|
|
|
expAdjustment++;
|
|
|
|
expAdjustment = expAdjustment * 4 - 1;
|
|
|
|
|
|
|
|
/* Adjust for writing the significand starting at the most
|
|
|
|
significant nibble. */
|
|
|
|
expAdjustment += semantics->precision;
|
|
|
|
expAdjustment -= partsCount * integerPartWidth;
|
|
|
|
|
|
|
|
/* Adjust for the given exponent. */
|
|
|
|
exponent = totalExponent(p, expAdjustment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalize(rounding_mode, lost_fraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::convertFromString(const char *p, roundingMode rounding_mode)
|
|
|
|
{
|
2007-08-20 22:49:32 +00:00
|
|
|
/* Handle a leading minus sign. */
|
|
|
|
if(*p == '-')
|
|
|
|
sign = 1, p++;
|
|
|
|
else
|
|
|
|
sign = 0;
|
|
|
|
|
|
|
|
if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
|
|
|
|
return convertFromHexadecimalString(p + 2, rounding_mode);
|
2007-08-24 03:02:34 +00:00
|
|
|
|
|
|
|
assert(0 && "Decimal to binary conversions not yet implemented");
|
|
|
|
abort();
|
2007-08-20 22:49:32 +00:00
|
|
|
}
|
2007-08-24 00:56:33 +00:00
|
|
|
|
2007-10-03 22:26:02 +00:00
|
|
|
/* Write out a hexadecimal representation of the floating point value
|
|
|
|
to DST, which must be of sufficient size, in the C99 form
|
|
|
|
[-]0xh.hhhhp[+-]d. Return the number of characters written,
|
|
|
|
excluding the terminating NUL.
|
|
|
|
|
|
|
|
If UPPERCASE, the output is in upper case, otherwise in lower case.
|
|
|
|
|
|
|
|
HEXDIGITS digits appear altogether, rounding the value if
|
|
|
|
necessary. If HEXDIGITS is 0, the minimal precision to display the
|
|
|
|
number precisely is used instead. If nothing would appear after
|
|
|
|
the decimal point it is suppressed.
|
|
|
|
|
|
|
|
The decimal exponent is always printed and has at least one digit.
|
|
|
|
Zero values display an exponent of zero. Infinities and NaNs
|
|
|
|
appear as "infinity" or "nan" respectively.
|
|
|
|
|
|
|
|
The above rules are as specified by C99. There is ambiguity about
|
|
|
|
what the leading hexadecimal digit should be. This implementation
|
|
|
|
uses whatever is necessary so that the exponent is displayed as
|
|
|
|
stored. This implies the exponent will fall within the IEEE format
|
|
|
|
range, and the leading hexadecimal digit will be 0 (for denormals),
|
|
|
|
1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
|
|
|
|
any other digits zero).
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
APFloat::convertToHexString(char *dst, unsigned int hexDigits,
|
|
|
|
bool upperCase, roundingMode rounding_mode) const
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = dst;
|
|
|
|
if (sign)
|
|
|
|
*dst++ = '-';
|
|
|
|
|
|
|
|
switch (category) {
|
|
|
|
case fcInfinity:
|
|
|
|
memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
|
|
|
|
dst += sizeof infinityL - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case fcNaN:
|
|
|
|
memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
|
|
|
|
dst += sizeof NaNU - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case fcZero:
|
|
|
|
*dst++ = '0';
|
|
|
|
*dst++ = upperCase ? 'X': 'x';
|
|
|
|
*dst++ = '0';
|
|
|
|
if (hexDigits > 1) {
|
|
|
|
*dst++ = '.';
|
|
|
|
memset (dst, '0', hexDigits - 1);
|
|
|
|
dst += hexDigits - 1;
|
|
|
|
}
|
|
|
|
*dst++ = upperCase ? 'P': 'p';
|
|
|
|
*dst++ = '0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case fcNormal:
|
|
|
|
dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = 0;
|
|
|
|
|
|
|
|
return dst - p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the hard work of outputting the correctly rounded hexadecimal
|
|
|
|
form of a normal floating point number with the specified number of
|
|
|
|
hexadecimal digits. If HEXDIGITS is zero the minimum number of
|
|
|
|
digits necessary to print the value precisely is output. */
|
|
|
|
char *
|
|
|
|
APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
|
|
|
|
bool upperCase,
|
|
|
|
roundingMode rounding_mode) const
|
|
|
|
{
|
|
|
|
unsigned int count, valueBits, shift, partsCount, outputDigits;
|
|
|
|
const char *hexDigitChars;
|
|
|
|
const integerPart *significand;
|
|
|
|
char *p;
|
|
|
|
bool roundUp;
|
|
|
|
|
|
|
|
*dst++ = '0';
|
|
|
|
*dst++ = upperCase ? 'X': 'x';
|
|
|
|
|
|
|
|
roundUp = false;
|
|
|
|
hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
|
|
|
|
|
|
|
|
significand = significandParts();
|
|
|
|
partsCount = partCount();
|
|
|
|
|
|
|
|
/* +3 because the first digit only uses the single integer bit, so
|
|
|
|
we have 3 virtual zero most-significant-bits. */
|
|
|
|
valueBits = semantics->precision + 3;
|
|
|
|
shift = integerPartWidth - valueBits % integerPartWidth;
|
|
|
|
|
|
|
|
/* The natural number of digits required ignoring trailing
|
|
|
|
insignificant zeroes. */
|
|
|
|
outputDigits = (valueBits - significandLSB () + 3) / 4;
|
|
|
|
|
|
|
|
/* hexDigits of zero means use the required number for the
|
|
|
|
precision. Otherwise, see if we are truncating. If we are,
|
2007-10-06 00:24:48 +00:00
|
|
|
find out if we need to round away from zero. */
|
2007-10-03 22:26:02 +00:00
|
|
|
if (hexDigits) {
|
|
|
|
if (hexDigits < outputDigits) {
|
|
|
|
/* We are dropping non-zero bits, so need to check how to round.
|
|
|
|
"bits" is the number of dropped bits. */
|
|
|
|
unsigned int bits;
|
|
|
|
lostFraction fraction;
|
|
|
|
|
|
|
|
bits = valueBits - hexDigits * 4;
|
|
|
|
fraction = lostFractionThroughTruncation (significand, partsCount, bits);
|
|
|
|
roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
|
|
|
|
}
|
|
|
|
outputDigits = hexDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the digits consecutively, and start writing in the location
|
|
|
|
of the hexadecimal point. We move the most significant digit
|
|
|
|
left and add the hexadecimal point later. */
|
|
|
|
p = ++dst;
|
|
|
|
|
|
|
|
count = (valueBits + integerPartWidth - 1) / integerPartWidth;
|
|
|
|
|
|
|
|
while (outputDigits && count) {
|
|
|
|
integerPart part;
|
|
|
|
|
|
|
|
/* Put the most significant integerPartWidth bits in "part". */
|
|
|
|
if (--count == partsCount)
|
|
|
|
part = 0; /* An imaginary higher zero part. */
|
|
|
|
else
|
|
|
|
part = significand[count] << shift;
|
|
|
|
|
|
|
|
if (count && shift)
|
|
|
|
part |= significand[count - 1] >> (integerPartWidth - shift);
|
|
|
|
|
|
|
|
/* Convert as much of "part" to hexdigits as we can. */
|
|
|
|
unsigned int curDigits = integerPartWidth / 4;
|
|
|
|
|
|
|
|
if (curDigits > outputDigits)
|
|
|
|
curDigits = outputDigits;
|
|
|
|
dst += partAsHex (dst, part, curDigits, hexDigitChars);
|
|
|
|
outputDigits -= curDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (roundUp) {
|
|
|
|
char *q = dst;
|
|
|
|
|
|
|
|
/* Note that hexDigitChars has a trailing '0'. */
|
|
|
|
do {
|
|
|
|
q--;
|
|
|
|
*q = hexDigitChars[hexDigitValue (*q) + 1];
|
2007-10-06 00:24:48 +00:00
|
|
|
} while (*q == '0');
|
|
|
|
assert (q >= p);
|
2007-10-03 22:26:02 +00:00
|
|
|
} else {
|
|
|
|
/* Add trailing zeroes. */
|
|
|
|
memset (dst, '0', outputDigits);
|
|
|
|
dst += outputDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move the most significant digit to before the point, and if there
|
|
|
|
is something after the decimal point add it. This must come
|
|
|
|
after rounding above. */
|
|
|
|
p[-1] = p[0];
|
|
|
|
if (dst -1 == p)
|
|
|
|
dst--;
|
|
|
|
else
|
|
|
|
p[0] = '.';
|
|
|
|
|
|
|
|
/* Finally output the exponent. */
|
|
|
|
*dst++ = upperCase ? 'P': 'p';
|
|
|
|
|
2007-10-06 07:29:25 +00:00
|
|
|
return writeSignedDecimal (dst, exponent);
|
2007-10-03 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 00:56:33 +00:00
|
|
|
// For good performance it is desirable for different APFloats
|
|
|
|
// to produce different integers.
|
|
|
|
uint32_t
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::getHashValue() const
|
|
|
|
{
|
2007-08-24 00:56:33 +00:00
|
|
|
if (category==fcZero) return sign<<8 | semantics->precision ;
|
|
|
|
else if (category==fcInfinity) return sign<<9 | semantics->precision;
|
2007-08-31 04:03:46 +00:00
|
|
|
else if (category==fcNaN) return 1<<10 | semantics->precision;
|
2007-08-24 00:56:33 +00:00
|
|
|
else {
|
|
|
|
uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
|
|
|
|
const integerPart* p = significandParts();
|
|
|
|
for (int i=partCount(); i>0; i--, p++)
|
|
|
|
hash ^= ((uint32_t)*p) ^ (*p)>>32;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion from APFloat to/from host float/double. It may eventually be
|
|
|
|
// possible to eliminate these and have everybody deal with APFloats, but that
|
|
|
|
// will take a while. This approach will not easily extend to long double.
|
2007-09-20 23:47:58 +00:00
|
|
|
// Current implementation requires integerPartWidth==64, which is correct at
|
|
|
|
// the moment but could be made more general.
|
2007-08-24 00:56:33 +00:00
|
|
|
|
2007-09-05 20:39:49 +00:00
|
|
|
// Denormals have exponent minExponent in APFloat, but minExponent-1 in
|
2007-09-20 23:47:58 +00:00
|
|
|
// the actual IEEE respresentations. We compensate for that here.
|
2007-09-05 20:39:49 +00:00
|
|
|
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::convertF80LongDoubleAPFloatToAPInt() const
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
assert(semantics == (const llvm::fltSemantics* const)&x87DoubleExtended);
|
2007-09-20 23:47:58 +00:00
|
|
|
assert (partCount()==2);
|
2007-09-11 18:32:33 +00:00
|
|
|
|
|
|
|
uint64_t myexponent, mysignificand;
|
|
|
|
|
|
|
|
if (category==fcNormal) {
|
|
|
|
myexponent = exponent+16383; //bias
|
2007-09-20 23:47:58 +00:00
|
|
|
mysignificand = significandParts()[0];
|
2007-09-11 18:32:33 +00:00
|
|
|
if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
|
|
|
|
myexponent = 0; // denormal
|
|
|
|
} else if (category==fcZero) {
|
|
|
|
myexponent = 0;
|
|
|
|
mysignificand = 0;
|
|
|
|
} else if (category==fcInfinity) {
|
|
|
|
myexponent = 0x7fff;
|
|
|
|
mysignificand = 0x8000000000000000ULL;
|
2007-10-06 06:13:42 +00:00
|
|
|
} else {
|
|
|
|
assert(category == fcNaN && "Unknown category");
|
2007-09-11 18:32:33 +00:00
|
|
|
myexponent = 0x7fff;
|
2007-09-20 23:47:58 +00:00
|
|
|
mysignificand = significandParts()[0];
|
2007-10-06 06:13:42 +00:00
|
|
|
}
|
2007-09-11 18:32:33 +00:00
|
|
|
|
|
|
|
uint64_t words[2];
|
2007-09-26 21:33:42 +00:00
|
|
|
words[0] = (((uint64_t)sign & 1) << 63) |
|
|
|
|
((myexponent & 0x7fff) << 48) |
|
2007-09-11 18:32:33 +00:00
|
|
|
((mysignificand >>16) & 0xffffffffffffLL);
|
|
|
|
words[1] = mysignificand & 0xffff;
|
2007-10-06 06:13:42 +00:00
|
|
|
return APInt(80, 2, words);
|
2007-09-11 18:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
APInt
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::convertDoubleAPFloatToAPInt() const
|
|
|
|
{
|
2007-09-14 20:08:19 +00:00
|
|
|
assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
|
2007-08-24 00:56:33 +00:00
|
|
|
assert (partCount()==1);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
uint64_t myexponent, mysignificand;
|
2007-08-24 00:56:33 +00:00
|
|
|
|
|
|
|
if (category==fcNormal) {
|
|
|
|
myexponent = exponent+1023; //bias
|
2007-09-05 20:39:49 +00:00
|
|
|
mysignificand = *significandParts();
|
|
|
|
if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
|
|
|
|
myexponent = 0; // denormal
|
2007-08-24 00:56:33 +00:00
|
|
|
} else if (category==fcZero) {
|
|
|
|
myexponent = 0;
|
|
|
|
mysignificand = 0;
|
|
|
|
} else if (category==fcInfinity) {
|
|
|
|
myexponent = 0x7ff;
|
|
|
|
mysignificand = 0;
|
2007-10-06 06:13:42 +00:00
|
|
|
} else {
|
|
|
|
assert(category == fcNaN && "Unknown category!");
|
2007-08-24 00:56:33 +00:00
|
|
|
myexponent = 0x7ff;
|
2007-08-31 04:03:46 +00:00
|
|
|
mysignificand = *significandParts();
|
2007-10-06 06:13:42 +00:00
|
|
|
}
|
2007-08-24 00:56:33 +00:00
|
|
|
|
2007-10-06 06:13:42 +00:00
|
|
|
return APInt(64, (((((uint64_t)sign & 1) << 63) |
|
|
|
|
((myexponent & 0x7ff) << 52) |
|
|
|
|
(mysignificand & 0xfffffffffffffLL))));
|
2007-08-24 00:56:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::convertFloatAPFloatToAPInt() const
|
|
|
|
{
|
2007-09-14 20:08:19 +00:00
|
|
|
assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
|
2007-08-24 00:56:33 +00:00
|
|
|
assert (partCount()==1);
|
2007-09-26 21:33:42 +00:00
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
uint32_t myexponent, mysignificand;
|
2007-08-24 00:56:33 +00:00
|
|
|
|
|
|
|
if (category==fcNormal) {
|
|
|
|
myexponent = exponent+127; //bias
|
|
|
|
mysignificand = *significandParts();
|
2007-09-05 20:39:49 +00:00
|
|
|
if (myexponent == 1 && !(mysignificand & 0x400000))
|
|
|
|
myexponent = 0; // denormal
|
2007-08-24 00:56:33 +00:00
|
|
|
} else if (category==fcZero) {
|
|
|
|
myexponent = 0;
|
|
|
|
mysignificand = 0;
|
|
|
|
} else if (category==fcInfinity) {
|
|
|
|
myexponent = 0xff;
|
|
|
|
mysignificand = 0;
|
2007-10-06 06:13:42 +00:00
|
|
|
} else {
|
|
|
|
assert(category == fcNaN && "Unknown category!");
|
2007-09-05 20:39:49 +00:00
|
|
|
myexponent = 0xff;
|
2007-08-31 04:03:46 +00:00
|
|
|
mysignificand = *significandParts();
|
2007-10-06 06:13:42 +00:00
|
|
|
}
|
2007-08-24 00:56:33 +00:00
|
|
|
|
2007-10-06 06:13:42 +00:00
|
|
|
return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
|
|
|
|
(mysignificand & 0x7fffff)));
|
2007-08-24 00:56:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::convertToAPInt() const
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
if (semantics == (const llvm::fltSemantics* const)&IEEEsingle)
|
|
|
|
return convertFloatAPFloatToAPInt();
|
2007-10-06 06:13:42 +00:00
|
|
|
|
|
|
|
if (semantics == (const llvm::fltSemantics* const)&IEEEdouble)
|
2007-09-11 18:32:33 +00:00
|
|
|
return convertDoubleAPFloatToAPInt();
|
2007-09-26 21:33:42 +00:00
|
|
|
|
2007-10-06 06:13:42 +00:00
|
|
|
assert(semantics == (const llvm::fltSemantics* const)&x87DoubleExtended &&
|
|
|
|
"unknown format!");
|
|
|
|
return convertF80LongDoubleAPFloatToAPInt();
|
2007-09-11 18:32:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
float
|
|
|
|
APFloat::convertToFloat() const
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
assert(semantics == (const llvm::fltSemantics* const)&IEEEsingle);
|
|
|
|
APInt api = convertToAPInt();
|
|
|
|
return api.bitsToFloat();
|
|
|
|
}
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
double
|
|
|
|
APFloat::convertToDouble() const
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
assert(semantics == (const llvm::fltSemantics* const)&IEEEdouble);
|
|
|
|
APInt api = convertToAPInt();
|
|
|
|
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")
|
|
|
|
void
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::initFromF80LongDoubleAPInt(const APInt &api)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
assert(api.getBitWidth()==80);
|
|
|
|
uint64_t i1 = api.getRawData()[0];
|
|
|
|
uint64_t i2 = api.getRawData()[1];
|
|
|
|
uint64_t myexponent = (i1 >> 48) & 0x7fff;
|
|
|
|
uint64_t mysignificand = ((i1 << 16) & 0xffffffffffff0000ULL) |
|
|
|
|
(i2 & 0xffff);
|
|
|
|
|
|
|
|
initialize(&APFloat::x87DoubleExtended);
|
2007-09-20 23:47:58 +00:00
|
|
|
assert(partCount()==2);
|
2007-09-11 18:32:33 +00:00
|
|
|
|
|
|
|
sign = i1>>63;
|
|
|
|
if (myexponent==0 && mysignificand==0) {
|
|
|
|
// exponent, significand meaningless
|
|
|
|
category = fcZero;
|
|
|
|
} else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
|
|
|
|
// exponent, significand meaningless
|
|
|
|
category = fcInfinity;
|
|
|
|
} else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
|
|
|
|
// exponent meaningless
|
|
|
|
category = fcNaN;
|
2007-09-20 23:47:58 +00:00
|
|
|
significandParts()[0] = mysignificand;
|
|
|
|
significandParts()[1] = 0;
|
2007-09-11 18:32:33 +00:00
|
|
|
} else {
|
|
|
|
category = fcNormal;
|
|
|
|
exponent = myexponent - 16383;
|
2007-09-20 23:47:58 +00:00
|
|
|
significandParts()[0] = mysignificand;
|
|
|
|
significandParts()[1] = 0;
|
2007-09-11 18:32:33 +00:00
|
|
|
if (myexponent==0) // denormal
|
|
|
|
exponent = -16382;
|
2007-09-26 21:33:42 +00:00
|
|
|
}
|
2007-09-11 18:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::initFromDoubleAPInt(const APInt &api)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
assert(api.getBitWidth()==64);
|
|
|
|
uint64_t i = *api.getRawData();
|
2007-08-24 05:08:11 +00:00
|
|
|
uint64_t myexponent = (i >> 52) & 0x7ff;
|
|
|
|
uint64_t mysignificand = i & 0xfffffffffffffLL;
|
|
|
|
|
2007-08-24 00:56:33 +00:00
|
|
|
initialize(&APFloat::IEEEdouble);
|
|
|
|
assert(partCount()==1);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
sign = i>>63;
|
2007-08-24 00:56:33 +00:00
|
|
|
if (myexponent==0 && mysignificand==0) {
|
|
|
|
// exponent, significand meaningless
|
|
|
|
category = fcZero;
|
|
|
|
} else if (myexponent==0x7ff && mysignificand==0) {
|
|
|
|
// exponent, significand meaningless
|
|
|
|
category = fcInfinity;
|
2007-08-31 04:03:46 +00:00
|
|
|
} else if (myexponent==0x7ff && mysignificand!=0) {
|
|
|
|
// exponent meaningless
|
|
|
|
category = fcNaN;
|
|
|
|
*significandParts() = mysignificand;
|
2007-08-24 00:56:33 +00:00
|
|
|
} else {
|
|
|
|
category = fcNormal;
|
|
|
|
exponent = myexponent - 1023;
|
2007-09-05 20:39:49 +00:00
|
|
|
*significandParts() = mysignificand;
|
|
|
|
if (myexponent==0) // denormal
|
|
|
|
exponent = -1022;
|
|
|
|
else
|
|
|
|
*significandParts() |= 0x10000000000000LL; // integer bit
|
2007-09-26 21:33:42 +00:00
|
|
|
}
|
2007-08-24 00:56:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 18:32:33 +00:00
|
|
|
void
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::initFromFloatAPInt(const APInt & api)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
assert(api.getBitWidth()==32);
|
|
|
|
uint32_t i = (uint32_t)*api.getRawData();
|
2007-08-24 05:08:11 +00:00
|
|
|
uint32_t myexponent = (i >> 23) & 0xff;
|
|
|
|
uint32_t mysignificand = i & 0x7fffff;
|
|
|
|
|
2007-08-24 00:56:33 +00:00
|
|
|
initialize(&APFloat::IEEEsingle);
|
|
|
|
assert(partCount()==1);
|
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
sign = i >> 31;
|
2007-08-24 00:56:33 +00:00
|
|
|
if (myexponent==0 && mysignificand==0) {
|
|
|
|
// exponent, significand meaningless
|
|
|
|
category = fcZero;
|
|
|
|
} else if (myexponent==0xff && mysignificand==0) {
|
|
|
|
// exponent, significand meaningless
|
|
|
|
category = fcInfinity;
|
2007-09-25 17:25:00 +00:00
|
|
|
} else if (myexponent==0xff && mysignificand!=0) {
|
2007-08-24 00:56:33 +00:00
|
|
|
// sign, exponent, significand meaningless
|
2007-08-31 04:03:46 +00:00
|
|
|
category = fcNaN;
|
|
|
|
*significandParts() = mysignificand;
|
2007-08-24 00:56:33 +00:00
|
|
|
} else {
|
|
|
|
category = fcNormal;
|
|
|
|
exponent = myexponent - 127; //bias
|
2007-09-05 20:39:49 +00:00
|
|
|
*significandParts() = mysignificand;
|
|
|
|
if (myexponent==0) // denormal
|
|
|
|
exponent = -126;
|
|
|
|
else
|
|
|
|
*significandParts() |= 0x800000; // integer bit
|
2007-08-24 00:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-11 18:32:33 +00:00
|
|
|
|
|
|
|
/// Treat api as containing the bits of a floating point number. Currently
|
|
|
|
/// we infer the floating point type from the size of the APInt. FIXME: This
|
|
|
|
/// breaks when we get to PPC128 and IEEE128 (but both cannot exist in the
|
|
|
|
/// same compile...)
|
|
|
|
void
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::initFromAPInt(const APInt& api)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
if (api.getBitWidth() == 32)
|
|
|
|
return initFromFloatAPInt(api);
|
|
|
|
else if (api.getBitWidth()==64)
|
|
|
|
return initFromDoubleAPInt(api);
|
|
|
|
else if (api.getBitWidth()==80)
|
|
|
|
return initFromF80LongDoubleAPInt(api);
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::APFloat(const APInt& api)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
initFromAPInt(api);
|
|
|
|
}
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::APFloat(float f)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt api = APInt(32, 0);
|
|
|
|
initFromAPInt(api.floatToBits(f));
|
|
|
|
}
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::APFloat(double d)
|
|
|
|
{
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt api = APInt(64, 0);
|
|
|
|
initFromAPInt(api.doubleToBits(d));
|
|
|
|
}
|