ImutAVLTree now allocates tree nodes from the BumpPtrAllocator using
[oota-llvm.git] / include / llvm / ADT / APFloat.h
index 7b95392e7ff6ad04f9ec868210509458abccd71d..9037ef5f727337d98e9335a6416b2c5374c5b2ee 100644 (file)
 //===----------------------------------------------------------------------===//
 
 /*  A self-contained host- and target-independent arbitrary-precision
-    floating-point software implementation using bignum integer
-    arithmetic, as provided by static functions in the APInt class.
+    floating-point software implementation.  It 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 64 bits is recommended.
+    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
     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.  The library reads
-    and correctly rounds hexadecimal floating point numbers as per
-    C99; syntax is required to have been validated by the caller.
-    Conversion from decimal is not currently implemented.
+    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
     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
     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
-    indeterminate and meaningless.  For QNaNs the sign bit, as well as
-    the exponent and significand are indeterminate and meaningless.
+    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.
 
     TODO
     ====
 
     Some features that may or may not be worth adding:
 
-    Conversions to and from decimal strings (hard).
-
-    Conversions to hexadecimal string.
-
-    Read and write IEEE-format in-memory representations.
+    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) and IBM two-double extended
-    precision (hard).
+    from extended exponent range) (hard).
 
-    New operations: sqrt, copysign, nextafter, nexttoward.
+    New operations: sqrt, IEEE remainder, C90 fmod, nextafter,
+    nexttoward.
 */
 
 #ifndef LLVM_FLOAT_H
 
 // APInt contains static functions implementing bignum arithmetic.
 #include "llvm/ADT/APInt.h"
+#include "llvm/CodeGen/ValueTypes.h"
 
 namespace llvm {
 
@@ -117,6 +128,7 @@ namespace llvm {
     static const fltSemantics IEEEsingle;
     static const fltSemantics IEEEdouble;
     static const fltSemantics IEEEquad;
+    static const fltSemantics PPCDoubleDouble;
     static const fltSemantics x87DoubleExtended;
     /* And this psuedo, used to construct APFloats that cannot
        conflict with anything real. */
@@ -155,7 +167,7 @@ namespace llvm {
     /* Category of internally-represented number.  */
     enum fltCategory {
       fcInfinity,
-      fcQNaN,
+      fcNaN,
       fcNormal,
       fcZero
     };
@@ -164,8 +176,9 @@ namespace llvm {
     APFloat(const fltSemantics &, const char *);
     APFloat(const fltSemantics &, integerPart);
     APFloat(const fltSemantics &, fltCategory, bool negative);
-    APFloat(double d);
-    APFloat(float f);
+    explicit APFloat(double d);
+    explicit APFloat(float f);
+    explicit APFloat(const APInt &, bool isIEEE = false);
     APFloat(const APFloat &);
     ~APFloat();
 
@@ -174,28 +187,43 @@ namespace llvm {
     opStatus subtract(const APFloat &, roundingMode);
     opStatus multiply(const APFloat &, roundingMode);
     opStatus divide(const APFloat &, roundingMode);
+    opStatus mod(const APFloat &, roundingMode);
     opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
+
+    /* Sign operations.  */
     void changeSign();
+    void clearSign();
+    void copySign(const APFloat &);
 
     /* Conversions.  */
     opStatus convert(const fltSemantics &, roundingMode);
     opStatus convertToInteger(integerPart *, unsigned int, bool,
                              roundingMode) const;
-    opStatus convertFromInteger(const integerPart *, unsigned int, bool,
-                               roundingMode);
+    opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
+                                            bool, roundingMode);
+    opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
+                                            bool, roundingMode);
     opStatus convertFromString(const char *, roundingMode);
+    APInt convertToAPInt() const;
     double convertToDouble() const;
     float convertToFloat() const;
 
     /* 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. */
-    bool operator==(const APFloat &) const;     // DO NOT IMPLEMENT
-    
-    /* IEEE comparison with another floating point number (QNaNs
+    // bool operator==(const APFloat &) const;     // DO NOT IMPLEMENT
+
+    /* IEEE comparison with another floating point number (NaNs
        compare unordered, 0==-0). */
     cmpResult compare(const APFloat &) const;
 
+    /* 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;
+
     /* Bitwise comparison for equality (QNaNs compare equal, 0!=-0). */
     bool bitwiseIsEqual(const APFloat &) const;
 
@@ -205,6 +233,8 @@ namespace llvm {
     bool isZero() const { return category == fcZero; }
     bool isNonZero() const { return category != fcZero; }
     bool isNegative() const { return sign; }
+    bool isPosZero() const { return isZero() && !isNegative(); }
+    bool isNegZero() const { return isZero() && isNegative(); }
 
     APFloat& operator=(const APFloat &);
 
@@ -238,15 +268,30 @@ namespace llvm {
     opStatus multiplySpecials(const APFloat &);
 
     /* Miscellany.  */
+    void makeNaN(void);
     opStatus normalize(roundingMode, lostFraction);
     opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
     cmpResult compareAbsoluteValue(const APFloat &) const;
     opStatus handleOverflow(roundingMode);
-    bool roundAwayFromZero(roundingMode, lostFraction);
-    opStatus convertFromUnsignedInteger(integerPart *, unsigned int,
-                                       roundingMode);
-    lostFraction combineLostFractions(lostFraction, lostFraction);
+    bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
+    opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
+                                      roundingMode);
     opStatus convertFromHexadecimalString(const char *, roundingMode);
+    opStatus convertFromDecimalString (const char *, roundingMode);
+    char *convertNormalToHexString(char *, unsigned int, bool,
+                                   roundingMode) const;
+    opStatus roundSignificandWithExponent(const integerPart *, unsigned int,
+                                          int, roundingMode);
+
+    APInt convertFloatAPFloatToAPInt() const;
+    APInt convertDoubleAPFloatToAPInt() const;
+    APInt convertF80LongDoubleAPFloatToAPInt() const;
+    APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
+    void initFromAPInt(const APInt& api, bool isIEEE = false);
+    void initFromFloatAPInt(const APInt& api);
+    void initFromDoubleAPInt(const APInt& api);
+    void initFromF80LongDoubleAPInt(const APInt& api);
+    void initFromPPCDoubleDoubleAPInt(const APInt& api);
 
     void assign(const APFloat &);
     void copySignificand(const APFloat &);
@@ -267,10 +312,19 @@ namespace llvm {
     exponent_t exponent;
 
     /* What kind of floating point number this is.  */
-    fltCategory category: 2;
+    /* Only 2 bits are required, but VisualStudio incorrectly sign extends
+       it.  Using the extra bit keeps it from failing under VisualStudio */
+    fltCategory category: 3;
 
     /* The sign bit of this number.  */
     unsigned int sign: 1;
+
+    /* 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;
   };
 } /* namespace llvm */