2007-08-20 22:49:32 +00:00
|
|
|
//== llvm/Support/APFloat.h - Arbitrary Precision Floating Point -*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-20 22:49:32 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares a class to represent arbitrary precision floating
|
|
|
|
// point values and provide a variety of arithmetic operations on them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/* A self-contained host- and target-independent arbitrary-precision
|
2007-10-03 22:26:02 +00:00
|
|
|
floating-point software implementation. It uses bignum integer
|
|
|
|
arithmetic as provided by static functions in the APInt class.
|
2007-08-20 22:49:32 +00:00
|
|
|
The library will work with bignum integers whose parts are any
|
2007-10-03 22:26:02 +00:00
|
|
|
unsigned type at least 16 bits wide, but 64 bits is recommended.
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
Written for clarity rather than speed, in particular with a view
|
|
|
|
to use in the front-end of a cross compiler so that target
|
|
|
|
arithmetic can be correctly performed on the host. Performance
|
|
|
|
should nonetheless be reasonable, particularly for its intended
|
|
|
|
use. It may be useful as a base implementation for a run-time
|
|
|
|
library during development of a faster target-specific one.
|
|
|
|
|
|
|
|
All 5 rounding modes in the IEEE-754R draft are handled correctly
|
|
|
|
for all implemented operations. Currently implemented operations
|
|
|
|
are add, subtract, multiply, divide, fused-multiply-add,
|
|
|
|
conversion-to-float, conversion-to-integer and
|
|
|
|
conversion-from-integer. New rounding modes (e.g. away from zero)
|
2007-10-03 22:26:02 +00:00
|
|
|
can be added with three or four lines of code.
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
Four formats are built-in: IEEE single precision, double
|
|
|
|
precision, quadruple precision, and x87 80-bit extended double
|
|
|
|
(when operating with full extended precision). Adding a new
|
|
|
|
format that obeys IEEE semantics only requires adding two lines of
|
|
|
|
code: a declaration and definition of the format.
|
|
|
|
|
|
|
|
All operations return the status of that operation as an exception
|
|
|
|
bit-mask, so multiple operations can be done consecutively with
|
|
|
|
their results or-ed together. The returned status can be useful
|
|
|
|
for compiler diagnostics; e.g., inexact, underflow and overflow
|
|
|
|
can be easily diagnosed on constant folding, and compiler
|
|
|
|
optimizers can determine what exceptions would be raised by
|
|
|
|
folding operations and optimize, or perhaps not optimize,
|
|
|
|
accordingly.
|
|
|
|
|
|
|
|
At present, underflow tininess is detected after rounding; it
|
|
|
|
should be straight forward to add support for the before-rounding
|
|
|
|
case too.
|
|
|
|
|
2007-10-03 22:26:02 +00:00
|
|
|
The library reads hexadecimal floating point numbers as per C99,
|
|
|
|
and correctly rounds if necessary according to the specified
|
|
|
|
rounding mode. Syntax is required to have been validated by the
|
|
|
|
caller. It also converts floating point numbers to hexadecimal
|
|
|
|
text as per the C99 %a and %A conversions. The output precision
|
|
|
|
(or alternatively the natural minimal precision) can be specified;
|
|
|
|
if the requested precision is less than the natural precision the
|
|
|
|
output is correctly rounded for the specified rounding mode.
|
|
|
|
|
2007-10-12 16:02:31 +00:00
|
|
|
It also reads decimal floating point numbers and correctly rounds
|
|
|
|
according to the specified rounding mode.
|
|
|
|
|
|
|
|
Conversion to decimal text is not currently implemented.
|
2007-10-03 22:26:02 +00:00
|
|
|
|
2007-08-20 22:49:32 +00:00
|
|
|
Non-zero finite numbers are represented internally as a sign bit,
|
|
|
|
a 16-bit signed exponent, and the significand as an array of
|
|
|
|
integer parts. After normalization of a number of precision P the
|
|
|
|
exponent is within the range of the format, and if the number is
|
|
|
|
not denormal the P-th bit of the significand is set as an explicit
|
|
|
|
integer bit. For denormals the most significant bit is shifted
|
|
|
|
right so that the exponent is maintained at the format's minimum,
|
|
|
|
so that the smallest denormal has just the least significant bit
|
|
|
|
of the significand set. The sign of zeroes and infinities is
|
2007-09-26 21:35:05 +00:00
|
|
|
significant; the exponent and significand of such numbers is not
|
|
|
|
stored, but has a known implicit (deterministic) value: 0 for the
|
|
|
|
significands, 0 for zero exponent, all 1 bits for infinity
|
|
|
|
exponent. For NaNs the sign and significand are deterministic,
|
|
|
|
although not really meaningful, and preserved in non-conversion
|
|
|
|
operations. The exponent is implicitly all 1 bits.
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
TODO
|
|
|
|
====
|
|
|
|
|
|
|
|
Some features that may or may not be worth adding:
|
|
|
|
|
2007-10-12 16:02:31 +00:00
|
|
|
Binary to decimal conversion (hard).
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
Optional ability to detect underflow tininess before rounding.
|
|
|
|
|
|
|
|
New formats: x87 in single and double precision mode (IEEE apart
|
2007-10-12 16:02:31 +00:00
|
|
|
from extended exponent range) (hard).
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2007-10-03 22:26:02 +00:00
|
|
|
New operations: sqrt, IEEE remainder, C90 fmod, nextafter,
|
|
|
|
nexttoward.
|
2007-08-20 22:49:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LLVM_FLOAT_H
|
|
|
|
#define LLVM_FLOAT_H
|
|
|
|
|
|
|
|
// APInt contains static functions implementing bignum arithmetic.
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/* Exponents are stored as signed numbers. */
|
|
|
|
typedef signed short exponent_t;
|
|
|
|
|
|
|
|
struct fltSemantics;
|
|
|
|
|
|
|
|
/* When bits of a floating point number are truncated, this enum is
|
|
|
|
used to indicate what fraction of the LSB those bits represented.
|
|
|
|
It essentially combines the roles of guard and sticky bits. */
|
2008-02-26 10:49:39 +00:00
|
|
|
enum lostFraction { // Example of truncated bits:
|
|
|
|
lfExactlyZero, // 000000
|
|
|
|
lfLessThanHalf, // 0xxxxx x's not all zero
|
|
|
|
lfExactlyHalf, // 100000
|
|
|
|
lfMoreThanHalf // 1xxxxx x's not all zero
|
2007-08-20 22:49:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class APFloat {
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* We support the following floating point semantics. */
|
|
|
|
static const fltSemantics IEEEsingle;
|
|
|
|
static const fltSemantics IEEEdouble;
|
|
|
|
static const fltSemantics IEEEquad;
|
2007-10-11 18:07:22 +00:00
|
|
|
static const fltSemantics PPCDoubleDouble;
|
2007-08-20 22:49:32 +00:00
|
|
|
static const fltSemantics x87DoubleExtended;
|
2007-08-24 00:56:33 +00:00
|
|
|
/* And this psuedo, used to construct APFloats that cannot
|
|
|
|
conflict with anything real. */
|
|
|
|
static const fltSemantics Bogus;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
static unsigned int semanticsPrecision(const fltSemantics &);
|
|
|
|
|
|
|
|
/* Floating point numbers have a four-state comparison relation. */
|
|
|
|
enum cmpResult {
|
|
|
|
cmpLessThan,
|
|
|
|
cmpEqual,
|
|
|
|
cmpGreaterThan,
|
|
|
|
cmpUnordered
|
|
|
|
};
|
|
|
|
|
|
|
|
/* IEEE-754R gives five rounding modes. */
|
|
|
|
enum roundingMode {
|
|
|
|
rmNearestTiesToEven,
|
|
|
|
rmTowardPositive,
|
|
|
|
rmTowardNegative,
|
|
|
|
rmTowardZero,
|
|
|
|
rmNearestTiesToAway
|
|
|
|
};
|
|
|
|
|
2008-06-30 18:02:44 +00:00
|
|
|
// Operation status. opUnderflow or opOverflow are always returned
|
|
|
|
// or-ed with opInexact.
|
2007-08-20 22:49:32 +00:00
|
|
|
enum opStatus {
|
|
|
|
opOK = 0x00,
|
|
|
|
opInvalidOp = 0x01,
|
|
|
|
opDivByZero = 0x02,
|
|
|
|
opOverflow = 0x04,
|
|
|
|
opUnderflow = 0x08,
|
|
|
|
opInexact = 0x10
|
|
|
|
};
|
|
|
|
|
2008-06-30 18:02:44 +00:00
|
|
|
// Category of internally-represented number.
|
2007-08-20 22:49:32 +00:00
|
|
|
enum fltCategory {
|
|
|
|
fcInfinity,
|
2007-08-31 04:03:46 +00:00
|
|
|
fcNaN,
|
2007-08-20 22:49:32 +00:00
|
|
|
fcNormal,
|
|
|
|
fcZero
|
|
|
|
};
|
|
|
|
|
2008-06-30 18:02:44 +00:00
|
|
|
// Constructors.
|
2007-08-20 22:49:32 +00:00
|
|
|
APFloat(const fltSemantics &, const char *);
|
|
|
|
APFloat(const fltSemantics &, integerPart);
|
|
|
|
APFloat(const fltSemantics &, fltCategory, bool negative);
|
2007-09-13 06:15:57 +00:00
|
|
|
explicit APFloat(double d);
|
|
|
|
explicit APFloat(float f);
|
2007-10-11 18:07:22 +00:00
|
|
|
explicit APFloat(const APInt &, bool isIEEE = false);
|
2007-08-20 22:49:32 +00:00
|
|
|
APFloat(const APFloat &);
|
|
|
|
~APFloat();
|
2008-06-30 18:02:44 +00:00
|
|
|
|
|
|
|
// Convenience "constructors"
|
|
|
|
static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
|
|
|
|
return APFloat(Sem, fcZero, Negative);
|
|
|
|
}
|
|
|
|
static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
|
|
|
|
return APFloat(Sem, fcInfinity, Negative);
|
|
|
|
}
|
|
|
|
static APFloat getNaN(const fltSemantics &Sem, bool Negative = false) {
|
|
|
|
return APFloat(Sem, fcNaN, Negative);
|
|
|
|
}
|
2007-11-07 18:39:22 +00:00
|
|
|
|
2008-02-11 17:24:50 +00:00
|
|
|
/// Profile - Used to insert APFloat objects, or objects that contain
|
|
|
|
/// APFloat objects, into FoldingSets.
|
|
|
|
void Profile(FoldingSetNodeID& NID) const;
|
|
|
|
|
2007-11-07 18:39:22 +00:00
|
|
|
/// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
|
|
|
|
void Emit(Serializer& S) const;
|
|
|
|
|
|
|
|
/// @brief Used by the Bitcode deserializer to deserialize APInts.
|
|
|
|
static APFloat ReadVal(Deserializer& D);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
/* Arithmetic. */
|
|
|
|
opStatus add(const APFloat &, roundingMode);
|
|
|
|
opStatus subtract(const APFloat &, roundingMode);
|
|
|
|
opStatus multiply(const APFloat &, roundingMode);
|
|
|
|
opStatus divide(const APFloat &, roundingMode);
|
2007-08-31 23:34:27 +00:00
|
|
|
opStatus mod(const APFloat &, roundingMode);
|
2007-08-20 22:49:32 +00:00
|
|
|
opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
|
2007-10-12 16:02:31 +00:00
|
|
|
|
|
|
|
/* Sign operations. */
|
|
|
|
void changeSign();
|
|
|
|
void clearSign();
|
|
|
|
void copySign(const APFloat &);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
/* Conversions. */
|
|
|
|
opStatus convert(const fltSemantics &, roundingMode);
|
|
|
|
opStatus convertToInteger(integerPart *, unsigned int, bool,
|
2008-02-26 10:49:39 +00:00
|
|
|
roundingMode) const;
|
2008-02-29 01:26:11 +00:00
|
|
|
opStatus convertFromAPInt(const APInt &,
|
|
|
|
bool, roundingMode);
|
2007-10-07 12:15:41 +00:00
|
|
|
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
|
|
|
|
bool, roundingMode);
|
2007-10-07 11:45:55 +00:00
|
|
|
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
|
|
|
|
bool, roundingMode);
|
2007-08-20 22:49:32 +00:00
|
|
|
opStatus convertFromString(const char *, roundingMode);
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt convertToAPInt() const;
|
2007-08-24 00:56:33 +00:00
|
|
|
double convertToDouble() const;
|
|
|
|
float convertToFloat() const;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2007-08-24 22:09:56 +00:00
|
|
|
/* The definition of equality is not straightforward for floating point,
|
|
|
|
so we won't use operator==. Use one of the following, or write
|
|
|
|
whatever it is you really mean. */
|
2007-08-25 00:40:41 +00:00
|
|
|
// bool operator==(const APFloat &) const; // DO NOT IMPLEMENT
|
2007-09-26 21:35:05 +00:00
|
|
|
|
2007-08-31 04:03:46 +00:00
|
|
|
/* IEEE comparison with another floating point number (NaNs
|
2007-08-24 00:56:33 +00:00
|
|
|
compare unordered, 0==-0). */
|
2007-08-20 22:49:32 +00:00
|
|
|
cmpResult compare(const APFloat &) const;
|
|
|
|
|
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. */
|
|
|
|
unsigned int convertToHexString(char *dst, unsigned int hexDigits,
|
|
|
|
bool upperCase, roundingMode) const;
|
|
|
|
|
2007-08-24 00:56:33 +00:00
|
|
|
/* Bitwise comparison for equality (QNaNs compare equal, 0!=-0). */
|
2007-08-24 22:09:56 +00:00
|
|
|
bool bitwiseIsEqual(const APFloat &) const;
|
2007-08-24 05:08:11 +00:00
|
|
|
|
2007-08-20 22:49:32 +00:00
|
|
|
/* Simple queries. */
|
|
|
|
fltCategory getCategory() const { return category; }
|
|
|
|
const fltSemantics &getSemantics() const { return *semantics; }
|
|
|
|
bool isZero() const { return category == fcZero; }
|
|
|
|
bool isNonZero() const { return category != fcZero; }
|
2007-10-24 18:54:28 +00:00
|
|
|
bool isNaN() const { return category == fcNaN; }
|
2008-06-01 18:09:04 +00:00
|
|
|
bool isInfinity() const { return category == fcInfinity; }
|
2007-08-20 22:49:32 +00:00
|
|
|
bool isNegative() const { return sign; }
|
2007-08-31 04:03:46 +00:00
|
|
|
bool isPosZero() const { return isZero() && !isNegative(); }
|
|
|
|
bool isNegZero() const { return isZero() && isNegative(); }
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
APFloat& operator=(const APFloat &);
|
|
|
|
|
2007-08-24 00:56:33 +00:00
|
|
|
/* Return an arbitrary integer value usable for hashing. */
|
|
|
|
uint32_t getHashValue() const;
|
|
|
|
|
2007-08-20 22:49:32 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/* Trivial queries. */
|
|
|
|
integerPart *significandParts();
|
|
|
|
const integerPart *significandParts() const;
|
|
|
|
unsigned int partCount() const;
|
|
|
|
|
|
|
|
/* Significand operations. */
|
|
|
|
integerPart addSignificand(const APFloat &);
|
|
|
|
integerPart subtractSignificand(const APFloat &, integerPart);
|
|
|
|
lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
|
|
|
|
lostFraction multiplySignificand(const APFloat &, const APFloat *);
|
|
|
|
lostFraction divideSignificand(const APFloat &);
|
|
|
|
void incrementSignificand();
|
|
|
|
void initialize(const fltSemantics *);
|
|
|
|
void shiftSignificandLeft(unsigned int);
|
|
|
|
lostFraction shiftSignificandRight(unsigned int);
|
|
|
|
unsigned int significandLSB() const;
|
|
|
|
unsigned int significandMSB() const;
|
|
|
|
void zeroSignificand();
|
|
|
|
|
|
|
|
/* Arithmetic on special values. */
|
|
|
|
opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
|
|
|
|
opStatus divideSpecials(const APFloat &);
|
|
|
|
opStatus multiplySpecials(const APFloat &);
|
|
|
|
|
|
|
|
/* Miscellany. */
|
2007-10-14 10:39:51 +00:00
|
|
|
void makeNaN(void);
|
2007-08-20 22:49:32 +00:00
|
|
|
opStatus normalize(roundingMode, lostFraction);
|
|
|
|
opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
|
|
|
|
cmpResult compareAbsoluteValue(const APFloat &) const;
|
|
|
|
opStatus handleOverflow(roundingMode);
|
2007-10-03 15:16:41 +00:00
|
|
|
bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
|
2007-11-01 22:43:37 +00:00
|
|
|
opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
|
|
|
|
roundingMode) const;
|
2007-10-07 12:07:53 +00:00
|
|
|
opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
|
|
|
|
roundingMode);
|
2007-08-20 22:49:32 +00:00
|
|
|
opStatus convertFromHexadecimalString(const char *, roundingMode);
|
2007-10-12 16:02:31 +00:00
|
|
|
opStatus convertFromDecimalString (const char *, roundingMode);
|
2007-10-03 22:26:02 +00:00
|
|
|
char *convertNormalToHexString(char *, unsigned int, bool,
|
|
|
|
roundingMode) const;
|
2007-10-12 16:02:31 +00:00
|
|
|
opStatus roundSignificandWithExponent(const integerPart *, unsigned int,
|
|
|
|
int, roundingMode);
|
|
|
|
|
2007-09-11 18:32:33 +00:00
|
|
|
APInt convertFloatAPFloatToAPInt() const;
|
|
|
|
APInt convertDoubleAPFloatToAPInt() const;
|
|
|
|
APInt convertF80LongDoubleAPFloatToAPInt() const;
|
2007-10-11 18:07:22 +00:00
|
|
|
APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
|
|
|
|
void initFromAPInt(const APInt& api, bool isIEEE = false);
|
2007-09-11 18:32:33 +00:00
|
|
|
void initFromFloatAPInt(const APInt& api);
|
|
|
|
void initFromDoubleAPInt(const APInt& api);
|
|
|
|
void initFromF80LongDoubleAPInt(const APInt& api);
|
2007-10-11 18:07:22 +00:00
|
|
|
void initFromPPCDoubleDoubleAPInt(const APInt& api);
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
void assign(const APFloat &);
|
|
|
|
void copySignificand(const APFloat &);
|
|
|
|
void freeSignificand();
|
|
|
|
|
|
|
|
/* What kind of semantics does this value obey? */
|
|
|
|
const fltSemantics *semantics;
|
|
|
|
|
|
|
|
/* Significand - the fraction with an explicit integer bit. Must be
|
|
|
|
at least one bit wider than the target precision. */
|
|
|
|
union Significand
|
|
|
|
{
|
|
|
|
integerPart part;
|
|
|
|
integerPart *parts;
|
|
|
|
} significand;
|
|
|
|
|
|
|
|
/* The exponent - a signed number. */
|
|
|
|
exponent_t exponent;
|
|
|
|
|
|
|
|
/* What kind of floating point number this is. */
|
2007-09-26 21:35:05 +00:00
|
|
|
/* Only 2 bits are required, but VisualStudio incorrectly sign extends
|
2007-09-08 04:17:08 +00:00
|
|
|
it. Using the extra bit keeps it from failing under VisualStudio */
|
|
|
|
fltCategory category: 3;
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
/* The sign bit of this number. */
|
|
|
|
unsigned int sign: 1;
|
2007-10-11 18:07:22 +00:00
|
|
|
|
|
|
|
/* For PPCDoubleDouble, we have a second exponent and sign (the second
|
|
|
|
significand is appended to the first one, although it would be wrong to
|
|
|
|
regard these as a single number for arithmetic purposes). These fields
|
|
|
|
are not meaningful for any other type. */
|
|
|
|
exponent_t exponent2 : 11;
|
|
|
|
unsigned int sign2: 1;
|
2007-08-20 22:49:32 +00:00
|
|
|
};
|
|
|
|
} /* namespace llvm */
|
|
|
|
|
|
|
|
#endif /* LLVM_FLOAT_H */
|