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>
|
|
|
|
#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))
|
|
|
|
|
|
|
|
/* Assumed in hexadecimal significand parsing. */
|
|
|
|
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
|
|
|
|
lostFractionThroughTruncation(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Combine the effect of two lost fractions. */
|
|
|
|
lostFraction
|
|
|
|
APFloat::combineLostFractions(lostFraction moreSignificant,
|
2007-09-26 21:33:42 +00:00
|
|
|
lostFraction lessSignificant)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
if(lessSignificant != lfExactlyZero) {
|
|
|
|
if(moreSignificant == lfExactlyZero)
|
|
|
|
moreSignificant = lfLessThanHalf;
|
|
|
|
else if(moreSignificant == lfExactlyHalf)
|
|
|
|
moreSignificant = lfMoreThanHalf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return moreSignificant;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-26 21:33:42 +00:00
|
|
|
rhs.significandParts(), 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-08-31 23:35:31 +00:00
|
|
|
/* Normalized remainder. */
|
|
|
|
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-09-26 21:33:42 +00:00
|
|
|
fs = V.convertFromInteger(x, parts * integerPartWidth, true,
|
2007-09-21 22:09:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
|
|
|
APFloat::convertFromUnsignedInteger(integerPart *parts,
|
2007-09-26 21:33:42 +00:00
|
|
|
unsigned int partCount,
|
|
|
|
roundingMode rounding_mode)
|
2007-08-20 22:49:32 +00:00
|
|
|
{
|
|
|
|
unsigned int msb, precision;
|
|
|
|
lostFraction lost_fraction;
|
|
|
|
|
|
|
|
msb = APInt::tcMSB(parts, partCount) + 1;
|
|
|
|
precision = semantics->precision;
|
|
|
|
|
|
|
|
category = fcNormal;
|
|
|
|
exponent = precision - 1;
|
|
|
|
|
|
|
|
if(msb > precision) {
|
|
|
|
exponent += (msb - precision);
|
|
|
|
lost_fraction = shiftRight(parts, partCount, msb - precision);
|
|
|
|
msb = precision;
|
|
|
|
} else
|
|
|
|
lost_fraction = lfExactlyZero;
|
|
|
|
|
|
|
|
/* Copy the bit image. */
|
|
|
|
zeroSignificand();
|
|
|
|
APInt::tcAssign(significandParts(), parts, partCountForBits(msb));
|
|
|
|
|
|
|
|
return normalize(rounding_mode, lost_fraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat::opStatus
|
2007-09-26 21:33:42 +00:00
|
|
|
APFloat::convertFromInteger(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);
|
2007-08-20 22:49:32 +00:00
|
|
|
opStatus status;
|
2007-09-21 22:09:37 +00:00
|
|
|
APInt api = APInt(width, partCount, parts);
|
|
|
|
integerPart *copy = new integerPart[partCount];
|
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-09-21 22:09:37 +00:00
|
|
|
APInt::tcAssign(copy, api.getRawData(), partCount);
|
2007-08-20 22:49:32 +00:00
|
|
|
status = convertFromUnsignedInteger(copy, partCount, rounding_mode);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Skip leading zeroes and any(hexa)decimal point. */
|
|
|
|
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
|
|
|
|
|
|
|
// 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;
|
|
|
|
} else if (category==fcNaN) {
|
|
|
|
myexponent = 0x7fff;
|
2007-09-20 23:47:58 +00:00
|
|
|
mysignificand = significandParts()[0];
|
2007-09-11 18:32:33 +00:00
|
|
|
} else
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
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;
|
|
|
|
APInt api(80, 2, words);
|
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-31 04:03:46 +00:00
|
|
|
} else if (category==fcNaN) {
|
2007-08-24 00:56:33 +00:00
|
|
|
myexponent = 0x7ff;
|
2007-08-31 04:03:46 +00:00
|
|
|
mysignificand = *significandParts();
|
2007-08-24 00:56:33 +00:00
|
|
|
} else
|
|
|
|
assert(0);
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
APInt api(64, (((((uint64_t)sign & 1) << 63) |
|
|
|
|
((myexponent & 0x7ff) << 52) |
|
2007-09-11 18:32:33 +00:00
|
|
|
(mysignificand & 0xfffffffffffffLL))));
|
|
|
|
return api;
|
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-08-31 04:03:46 +00:00
|
|
|
} else if (category==fcNaN) {
|
2007-09-05 20:39:49 +00:00
|
|
|
myexponent = 0xff;
|
2007-08-31 04:03:46 +00:00
|
|
|
mysignificand = *significandParts();
|
2007-08-24 00:56:33 +00:00
|
|
|
} else
|
|
|
|
assert(0);
|
|
|
|
|
2007-09-26 21:33:42 +00:00
|
|
|
APInt api(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
|
2007-09-11 18:32:33 +00:00
|
|
|
(mysignificand & 0x7fffff)));
|
|
|
|
return api;
|
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();
|
|
|
|
else if (semantics == (const llvm::fltSemantics* const)&IEEEdouble)
|
|
|
|
return convertDoubleAPFloatToAPInt();
|
|
|
|
else if (semantics == (const llvm::fltSemantics* const)&x87DoubleExtended)
|
|
|
|
return convertF80LongDoubleAPFloatToAPInt();
|
2007-09-26 21:33:42 +00:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
abort();
|
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));
|
|
|
|
}
|