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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-06-01 00:44:05 +00:00
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief
|
|
|
|
/// This file declares a class to represent arbitrary precision floating point
|
|
|
|
/// values and provide a variety of arithmetic operations on them.
|
|
|
|
///
|
2007-08-20 22:49:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ADT_APFLOAT_H
|
|
|
|
#define LLVM_ADT_APFLOAT_H
|
2007-08-20 22:49:32 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// A signed type to represent a floating point numbers unbiased exponent.
|
2013-05-24 22:40:37 +00:00
|
|
|
typedef signed short exponent_t;
|
|
|
|
|
|
|
|
struct fltSemantics;
|
|
|
|
class APSInt;
|
|
|
|
class StringRef;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Enum that represents what fraction of the LSB truncated bits of an fp number
|
|
|
|
/// represent.
|
|
|
|
///
|
|
|
|
/// This essentially combines the roles of guard and sticky bits.
|
2013-05-24 22:40:37 +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
|
|
|
|
};
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \brief A self-contained host- and target-independent arbitrary-precision
|
|
|
|
/// floating-point software implementation.
|
|
|
|
///
|
|
|
|
/// APFloat uses bignum integer arithmetic as provided by static functions in
|
|
|
|
/// the APInt class. The library will work with bignum integers whose parts are
|
|
|
|
/// any unsigned type at least 16 bits wide, but 64 bits is recommended.
|
|
|
|
///
|
|
|
|
/// 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) can be added with three or four lines of code.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// It also reads decimal floating point numbers and correctly rounds according
|
|
|
|
/// to the specified rounding mode.
|
|
|
|
///
|
|
|
|
/// Conversion to decimal text is not currently implemented.
|
|
|
|
///
|
|
|
|
/// 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 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.
|
|
|
|
///
|
|
|
|
/// APFloat does not provide any exception handling beyond default exception
|
|
|
|
/// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
|
|
|
|
/// by encoding Signaling NaNs with the first bit of its trailing significand as
|
|
|
|
/// 0.
|
|
|
|
///
|
|
|
|
/// TODO
|
|
|
|
/// ====
|
|
|
|
///
|
|
|
|
/// Some features that may or may not be worth adding:
|
|
|
|
///
|
|
|
|
/// Binary to decimal conversion (hard).
|
|
|
|
///
|
|
|
|
/// Optional ability to detect underflow tininess before rounding.
|
|
|
|
///
|
|
|
|
/// New formats: x87 in single and double precision mode (IEEE apart from
|
|
|
|
/// extended exponent range) (hard).
|
|
|
|
///
|
|
|
|
/// New operations: sqrt, IEEE remainder, C90 fmod, nextafter, nexttoward.
|
|
|
|
///
|
2013-05-24 22:40:37 +00:00
|
|
|
class APFloat {
|
|
|
|
public:
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Floating Point Semantics.
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
static const fltSemantics IEEEhalf;
|
|
|
|
static const fltSemantics IEEEsingle;
|
|
|
|
static const fltSemantics IEEEdouble;
|
|
|
|
static const fltSemantics IEEEquad;
|
|
|
|
static const fltSemantics PPCDoubleDouble;
|
|
|
|
static const fltSemantics x87DoubleExtended;
|
2013-06-01 00:44:05 +00:00
|
|
|
|
|
|
|
/// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
|
|
|
|
/// anything real.
|
2013-05-24 22:40:37 +00:00
|
|
|
static const fltSemantics Bogus;
|
|
|
|
|
2013-06-01 00:48:24 +00:00
|
|
|
/// @}
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
static unsigned int semanticsPrecision(const fltSemantics &);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// IEEE-754R 5.11: Floating Point Comparison Relations.
|
2013-05-24 22:40:37 +00:00
|
|
|
enum cmpResult {
|
|
|
|
cmpLessThan,
|
|
|
|
cmpEqual,
|
|
|
|
cmpGreaterThan,
|
|
|
|
cmpUnordered
|
2007-08-20 22:49:32 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// IEEE-754R 4.3: Rounding-direction attributes.
|
2013-05-24 22:40:37 +00:00
|
|
|
enum roundingMode {
|
|
|
|
rmNearestTiesToEven,
|
|
|
|
rmTowardPositive,
|
|
|
|
rmTowardNegative,
|
|
|
|
rmTowardZero,
|
|
|
|
rmNearestTiesToAway
|
|
|
|
};
|
2010-02-28 02:51:25 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// IEEE-754R 7: Default exception handling.
|
|
|
|
///
|
|
|
|
/// opUnderflow or opOverflow are always returned or-ed with opInexact.
|
2013-05-24 22:40:37 +00:00
|
|
|
enum opStatus {
|
|
|
|
opOK = 0x00,
|
|
|
|
opInvalidOp = 0x01,
|
|
|
|
opDivByZero = 0x02,
|
|
|
|
opOverflow = 0x04,
|
|
|
|
opUnderflow = 0x08,
|
|
|
|
opInexact = 0x10
|
|
|
|
};
|
2010-02-28 02:51:25 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Category of internally-represented number.
|
2013-05-24 22:40:37 +00:00
|
|
|
enum fltCategory {
|
|
|
|
fcInfinity,
|
|
|
|
fcNaN,
|
|
|
|
fcNormal,
|
|
|
|
fcZero
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Convenience enum used to construct an uninitialized APFloat.
|
2013-05-24 22:40:37 +00:00
|
|
|
enum uninitializedTag {
|
|
|
|
uninitialized
|
2007-08-20 22:49:32 +00:00
|
|
|
};
|
2012-10-31 00:46:18 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Constructors
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
APFloat(const fltSemantics &); // Default construct to 0.0
|
|
|
|
APFloat(const fltSemantics &, StringRef);
|
|
|
|
APFloat(const fltSemantics &, integerPart);
|
|
|
|
APFloat(const fltSemantics &, fltCategory, bool negative);
|
|
|
|
APFloat(const fltSemantics &, uninitializedTag);
|
|
|
|
APFloat(const fltSemantics &, const APInt &);
|
|
|
|
explicit APFloat(double d);
|
|
|
|
explicit APFloat(float f);
|
|
|
|
APFloat(const APFloat &);
|
|
|
|
~APFloat();
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
2013-06-03 13:03:05 +00:00
|
|
|
/// \brief Returns whether this instance allocated memory.
|
|
|
|
bool needsCleanup() const { return partCount() > 1; }
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Convenience "constructors"
|
|
|
|
/// @{
|
|
|
|
|
2013-06-01 00:44:29 +00:00
|
|
|
/// Factory for Positive and Negative Zero.
|
|
|
|
///
|
|
|
|
/// \param Negative True iff the number should be negative.
|
2013-05-24 22:40:37 +00:00
|
|
|
static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
|
|
|
|
return APFloat(Sem, fcZero, Negative);
|
|
|
|
}
|
2013-06-01 00:44:29 +00:00
|
|
|
|
|
|
|
/// Factory for Positive and Negative Infinity.
|
|
|
|
///
|
|
|
|
/// \param Negative True iff the number should be negative.
|
2013-05-24 22:40:37 +00:00
|
|
|
static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
|
|
|
|
return APFloat(Sem, fcInfinity, Negative);
|
|
|
|
}
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Factory for QNaN values.
|
2013-05-24 22:40:37 +00:00
|
|
|
///
|
|
|
|
/// \param Negative - True iff the NaN generated should be negative.
|
|
|
|
/// \param type - The unspecified fill bits for creating the NaN, 0 by
|
|
|
|
/// default. The value is truncated as necessary.
|
|
|
|
static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
|
|
|
|
unsigned type = 0) {
|
|
|
|
if (type) {
|
|
|
|
APInt fill(64, type);
|
|
|
|
return getQNaN(Sem, Negative, &fill);
|
|
|
|
} else {
|
|
|
|
return getQNaN(Sem, Negative, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Factory for QNaN values.
|
2013-05-24 22:40:37 +00:00
|
|
|
static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
|
|
|
|
const APInt *payload = 0) {
|
|
|
|
return makeNaN(Sem, false, Negative, payload);
|
|
|
|
}
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Factory for SNaN values.
|
2013-05-24 22:40:37 +00:00
|
|
|
static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
|
|
|
|
const APInt *payload = 0) {
|
|
|
|
return makeNaN(Sem, true, Negative, payload);
|
|
|
|
}
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Returns the largest finite number in the given semantics.
|
2013-05-24 22:40:37 +00:00
|
|
|
///
|
|
|
|
/// \param Negative - True iff the number should be negative
|
|
|
|
static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Returns the smallest (by magnitude) finite number in the given semantics.
|
|
|
|
/// Might be denormalized, which implies a relative loss of precision.
|
2013-05-24 22:40:37 +00:00
|
|
|
///
|
|
|
|
/// \param Negative - True iff the number should be negative
|
|
|
|
static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Returns the smallest (by magnitude) normalized finite number in the given
|
|
|
|
/// semantics.
|
2013-05-24 22:40:37 +00:00
|
|
|
///
|
|
|
|
/// \param Negative - True iff the number should be negative
|
|
|
|
static APFloat getSmallestNormalized(const fltSemantics &Sem,
|
|
|
|
bool Negative = false);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Returns a float which is bitcasted from an all one value int.
|
2013-05-24 22:40:37 +00:00
|
|
|
///
|
|
|
|
/// \param BitWidth - Select float type
|
|
|
|
/// \param isIEEE - If 128 bit number, select between PPC and IEEE
|
|
|
|
static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Used to insert APFloat objects, or objects that contain APFloat objects,
|
|
|
|
/// into FoldingSets.
|
2013-05-24 22:40:37 +00:00
|
|
|
void Profile(FoldingSetNodeID &NID) const;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \brief Used by the Bitcode serializer to emit APInts to Bitcode.
|
2013-05-24 22:40:37 +00:00
|
|
|
void Emit(Serializer &S) const;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \brief Used by the Bitcode deserializer to deserialize APInts.
|
2013-05-24 22:40:37 +00:00
|
|
|
static APFloat ReadVal(Deserializer &D);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Arithmetic
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
opStatus add(const APFloat &, roundingMode);
|
|
|
|
opStatus subtract(const APFloat &, roundingMode);
|
|
|
|
opStatus multiply(const APFloat &, roundingMode);
|
|
|
|
opStatus divide(const APFloat &, roundingMode);
|
2013-06-01 00:44:05 +00:00
|
|
|
/// IEEE remainder.
|
2013-05-24 22:40:37 +00:00
|
|
|
opStatus remainder(const APFloat &);
|
2013-06-01 00:44:05 +00:00
|
|
|
/// C fmod, or llvm frem.
|
2013-05-24 22:40:37 +00:00
|
|
|
opStatus mod(const APFloat &, roundingMode);
|
|
|
|
opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
|
|
|
|
opStatus roundToIntegral(roundingMode);
|
2013-05-30 18:07:13 +00:00
|
|
|
/// IEEE-754R 5.3.1: nextUp/nextDown.
|
|
|
|
opStatus next(bool nextDown);
|
2013-05-24 22:40:37 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Sign operations.
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
void changeSign();
|
|
|
|
void clearSign();
|
|
|
|
void copySign(const APFloat &);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Conversions
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
opStatus convert(const fltSemantics &, roundingMode, bool *);
|
|
|
|
opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode,
|
|
|
|
bool *) const;
|
|
|
|
opStatus convertToInteger(APSInt &, roundingMode, bool *) const;
|
|
|
|
opStatus convertFromAPInt(const APInt &, bool, roundingMode);
|
|
|
|
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
|
|
|
|
bool, roundingMode);
|
|
|
|
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
|
|
|
|
bool, roundingMode);
|
|
|
|
opStatus convertFromString(StringRef, roundingMode);
|
|
|
|
APInt bitcastToAPInt() const;
|
|
|
|
double convertToDouble() const;
|
|
|
|
float convertToFloat() const;
|
|
|
|
|
2013-06-01 00:44:05 +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.
|
2013-05-24 22:40:37 +00:00
|
|
|
bool operator==(const APFloat &) const LLVM_DELETED_FUNCTION;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// IEEE comparison with another floating point number (NaNs compare
|
|
|
|
/// unordered, 0==-0).
|
2013-05-24 22:40:37 +00:00
|
|
|
cmpResult compare(const APFloat &) const;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
|
2013-05-24 22:40:37 +00:00
|
|
|
bool bitwiseIsEqual(const APFloat &) const;
|
|
|
|
|
2013-06-01 00:44:05 +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.
|
2013-05-24 22:40:37 +00:00
|
|
|
unsigned int convertToHexString(char *dst, unsigned int hexDigits,
|
|
|
|
bool upperCase, roundingMode) const;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Simple Queries
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
fltCategory getCategory() const { return category; }
|
|
|
|
const fltSemantics &getSemantics() const { return *semantics; }
|
|
|
|
bool isZero() const { return category == fcZero; }
|
|
|
|
bool isNonZero() const { return category != fcZero; }
|
|
|
|
bool isNormal() const { return category == fcNormal; }
|
|
|
|
bool isNaN() const { return category == fcNaN; }
|
|
|
|
bool isInfinity() const { return category == fcInfinity; }
|
|
|
|
bool isNegative() const { return sign; }
|
|
|
|
bool isPosZero() const { return isZero() && !isNegative(); }
|
|
|
|
bool isNegZero() const { return isZero() && isNegative(); }
|
|
|
|
bool isDenormal() const;
|
2013-05-30 18:07:13 +00:00
|
|
|
/// IEEE-754R 5.7.2: isSignaling. Returns true if this is a signaling NaN.
|
|
|
|
bool isSignaling() const;
|
2013-05-24 22:40:37 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
APFloat &operator=(const APFloat &);
|
|
|
|
|
|
|
|
/// \brief Overload to compute a hash code for an APFloat value.
|
|
|
|
///
|
|
|
|
/// Note that the use of hash codes for floating point values is in general
|
|
|
|
/// frought with peril. Equality is hard to define for these values. For
|
|
|
|
/// example, should negative and positive zero hash to different codes? Are
|
|
|
|
/// they equal or not? This hash value implementation specifically
|
|
|
|
/// emphasizes producing different codes for different inputs in order to
|
|
|
|
/// be used in canonicalization and memoization. As such, equality is
|
|
|
|
/// bitwiseIsEqual, and 0 != -0.
|
|
|
|
friend hash_code hash_value(const APFloat &Arg);
|
|
|
|
|
|
|
|
/// Converts this value into a decimal string.
|
|
|
|
///
|
|
|
|
/// \param FormatPrecision The maximum number of digits of
|
|
|
|
/// precision to output. If there are fewer digits available,
|
|
|
|
/// zero padding will not be used unless the value is
|
|
|
|
/// integral and small enough to be expressed in
|
|
|
|
/// FormatPrecision digits. 0 means to use the natural
|
|
|
|
/// precision of the number.
|
|
|
|
/// \param FormatMaxPadding The maximum number of zeros to
|
|
|
|
/// consider inserting before falling back to scientific
|
|
|
|
/// notation. 0 means to always use scientific notation.
|
|
|
|
///
|
|
|
|
/// Number Precision MaxPadding Result
|
|
|
|
/// ------ --------- ---------- ------
|
|
|
|
/// 1.01E+4 5 2 10100
|
|
|
|
/// 1.01E+4 4 2 1.01E+4
|
|
|
|
/// 1.01E+4 5 1 1.01E+4
|
|
|
|
/// 1.01E-2 5 2 0.0101
|
|
|
|
/// 1.01E-2 4 2 0.0101
|
|
|
|
/// 1.01E-2 4 1 1.01E-2
|
|
|
|
void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
|
|
|
|
unsigned FormatMaxPadding = 3) const;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// If this value has an exact multiplicative inverse, store it in inv and
|
|
|
|
/// return true.
|
2013-05-24 22:40:37 +00:00
|
|
|
bool getExactInverse(APFloat *inv) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Simple Queries
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
integerPart *significandParts();
|
|
|
|
const integerPart *significandParts() const;
|
|
|
|
unsigned int partCount() const;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Significand operations.
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
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();
|
2013-05-30 18:07:13 +00:00
|
|
|
/// Return true if the significand excluding the integral bit is all ones.
|
|
|
|
bool isSignificandAllOnes() const;
|
|
|
|
/// Return true if the significand excluding the integral bit is all zeros.
|
|
|
|
bool isSignificandAllZeros() const;
|
2013-05-24 22:40:37 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Arithmetic on special values.
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
|
|
|
|
opStatus divideSpecials(const APFloat &);
|
|
|
|
opStatus multiplySpecials(const APFloat &);
|
|
|
|
opStatus modSpecials(const APFloat &);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Special value setters.
|
|
|
|
/// @{
|
|
|
|
|
2013-05-30 18:07:13 +00:00
|
|
|
void makeLargest(bool Neg = false);
|
|
|
|
void makeSmallest(bool Neg = false);
|
|
|
|
void makeNaN(bool SNaN = false, bool Neg = false, const APInt *fill = 0);
|
2013-05-24 22:40:37 +00:00
|
|
|
static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
|
|
|
|
const APInt *fill);
|
2013-05-30 18:07:13 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
2013-05-30 18:07:13 +00:00
|
|
|
/// \name Special value queries only useful internally to APFloat
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// Returns true if and only if the number has the smallest possible non-zero
|
|
|
|
/// magnitude in the current semantics.
|
|
|
|
bool isSmallest() const;
|
|
|
|
/// Returns true if and only if the number has the largest possible finite
|
|
|
|
/// magnitude in the current semantics.
|
|
|
|
bool isLargest() const;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// \name Miscellany
|
|
|
|
/// @{
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
opStatus normalize(roundingMode, lostFraction);
|
|
|
|
opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
|
|
|
|
cmpResult compareAbsoluteValue(const APFloat &) const;
|
|
|
|
opStatus handleOverflow(roundingMode);
|
|
|
|
bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
|
|
|
|
opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
|
|
|
|
roundingMode, bool *) const;
|
|
|
|
opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
|
|
|
|
roundingMode);
|
|
|
|
opStatus convertFromHexadecimalString(StringRef, roundingMode);
|
|
|
|
opStatus convertFromDecimalString(StringRef, roundingMode);
|
|
|
|
char *convertNormalToHexString(char *, unsigned int, bool,
|
|
|
|
roundingMode) const;
|
|
|
|
opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
|
|
|
|
roundingMode);
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// @}
|
|
|
|
|
2013-05-24 22:40:37 +00:00
|
|
|
APInt convertHalfAPFloatToAPInt() const;
|
|
|
|
APInt convertFloatAPFloatToAPInt() const;
|
|
|
|
APInt convertDoubleAPFloatToAPInt() const;
|
|
|
|
APInt convertQuadrupleAPFloatToAPInt() const;
|
|
|
|
APInt convertF80LongDoubleAPFloatToAPInt() const;
|
|
|
|
APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
|
|
|
|
void initFromAPInt(const fltSemantics *Sem, const APInt &api);
|
|
|
|
void initFromHalfAPInt(const APInt &api);
|
|
|
|
void initFromFloatAPInt(const APInt &api);
|
|
|
|
void initFromDoubleAPInt(const APInt &api);
|
|
|
|
void initFromQuadrupleAPInt(const APInt &api);
|
|
|
|
void initFromF80LongDoubleAPInt(const APInt &api);
|
|
|
|
void initFromPPCDoubleDoubleAPInt(const APInt &api);
|
|
|
|
|
|
|
|
void assign(const APFloat &);
|
|
|
|
void copySignificand(const APFloat &);
|
|
|
|
void freeSignificand();
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// The semantics that this value obeys.
|
2013-05-24 22:40:37 +00:00
|
|
|
const fltSemantics *semantics;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// A binary fraction with an explicit integer bit.
|
|
|
|
///
|
|
|
|
/// The significand must be at least one bit wider than the target precision.
|
2013-05-24 22:40:37 +00:00
|
|
|
union Significand {
|
|
|
|
integerPart part;
|
|
|
|
integerPart *parts;
|
|
|
|
} significand;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// The signed unbiased exponent of the value.
|
2013-05-24 22:40:37 +00:00
|
|
|
exponent_t exponent;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// What kind of floating point number this is.
|
|
|
|
///
|
|
|
|
/// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
|
|
|
|
/// Using the extra bit keeps it from failing under VisualStudio.
|
2013-05-24 22:40:37 +00:00
|
|
|
fltCategory category : 3;
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// Sign bit of the number.
|
2013-05-24 22:40:37 +00:00
|
|
|
unsigned int sign : 1;
|
|
|
|
};
|
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
/// See friend declaration above.
|
|
|
|
///
|
|
|
|
/// This additional declaration is required in order to compile LLVM with IBM
|
|
|
|
/// xlC compiler.
|
2013-05-24 22:40:37 +00:00
|
|
|
hash_code hash_value(const APFloat &Arg);
|
2013-06-01 00:44:05 +00:00
|
|
|
} // namespace llvm
|
2007-08-20 22:49:32 +00:00
|
|
|
|
2013-06-01 00:44:05 +00:00
|
|
|
#endif // LLVM_ADT_APFLOAT_H
|