Home | History | Annotate | Download | only in ADT
      1 //===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// \brief
     12 /// This file declares a class to represent arbitrary precision floating point
     13 /// values and provide a variety of arithmetic operations on them.
     14 ///
     15 //===----------------------------------------------------------------------===//
     16 
     17 #ifndef LLVM_ADT_APFLOAT_H
     18 #define LLVM_ADT_APFLOAT_H
     19 
     20 #include "llvm/ADT/APInt.h"
     21 
     22 namespace llvm {
     23 
     24 struct fltSemantics;
     25 class APSInt;
     26 class StringRef;
     27 
     28 /// Enum that represents what fraction of the LSB truncated bits of an fp number
     29 /// represent.
     30 ///
     31 /// This essentially combines the roles of guard and sticky bits.
     32 enum lostFraction { // Example of truncated bits:
     33   lfExactlyZero,    // 000000
     34   lfLessThanHalf,   // 0xxxxx  x's not all zero
     35   lfExactlyHalf,    // 100000
     36   lfMoreThanHalf    // 1xxxxx  x's not all zero
     37 };
     38 
     39 /// \brief A self-contained host- and target-independent arbitrary-precision
     40 /// floating-point software implementation.
     41 ///
     42 /// APFloat uses bignum integer arithmetic as provided by static functions in
     43 /// the APInt class.  The library will work with bignum integers whose parts are
     44 /// any unsigned type at least 16 bits wide, but 64 bits is recommended.
     45 ///
     46 /// Written for clarity rather than speed, in particular with a view to use in
     47 /// the front-end of a cross compiler so that target arithmetic can be correctly
     48 /// performed on the host.  Performance should nonetheless be reasonable,
     49 /// particularly for its intended use.  It may be useful as a base
     50 /// implementation for a run-time library during development of a faster
     51 /// target-specific one.
     52 ///
     53 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
     54 /// implemented operations.  Currently implemented operations are add, subtract,
     55 /// multiply, divide, fused-multiply-add, conversion-to-float,
     56 /// conversion-to-integer and conversion-from-integer.  New rounding modes
     57 /// (e.g. away from zero) can be added with three or four lines of code.
     58 ///
     59 /// Four formats are built-in: IEEE single precision, double precision,
     60 /// quadruple precision, and x87 80-bit extended double (when operating with
     61 /// full extended precision).  Adding a new format that obeys IEEE semantics
     62 /// only requires adding two lines of code: a declaration and definition of the
     63 /// format.
     64 ///
     65 /// All operations return the status of that operation as an exception bit-mask,
     66 /// so multiple operations can be done consecutively with their results or-ed
     67 /// together.  The returned status can be useful for compiler diagnostics; e.g.,
     68 /// inexact, underflow and overflow can be easily diagnosed on constant folding,
     69 /// and compiler optimizers can determine what exceptions would be raised by
     70 /// folding operations and optimize, or perhaps not optimize, accordingly.
     71 ///
     72 /// At present, underflow tininess is detected after rounding; it should be
     73 /// straight forward to add support for the before-rounding case too.
     74 ///
     75 /// The library reads hexadecimal floating point numbers as per C99, and
     76 /// correctly rounds if necessary according to the specified rounding mode.
     77 /// Syntax is required to have been validated by the caller.  It also converts
     78 /// floating point numbers to hexadecimal text as per the C99 %a and %A
     79 /// conversions.  The output precision (or alternatively the natural minimal
     80 /// precision) can be specified; if the requested precision is less than the
     81 /// natural precision the output is correctly rounded for the specified rounding
     82 /// mode.
     83 ///
     84 /// It also reads decimal floating point numbers and correctly rounds according
     85 /// to the specified rounding mode.
     86 ///
     87 /// Conversion to decimal text is not currently implemented.
     88 ///
     89 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
     90 /// signed exponent, and the significand as an array of integer parts.  After
     91 /// normalization of a number of precision P the exponent is within the range of
     92 /// the format, and if the number is not denormal the P-th bit of the
     93 /// significand is set as an explicit integer bit.  For denormals the most
     94 /// significant bit is shifted right so that the exponent is maintained at the
     95 /// format's minimum, so that the smallest denormal has just the least
     96 /// significant bit of the significand set.  The sign of zeroes and infinities
     97 /// is significant; the exponent and significand of such numbers is not stored,
     98 /// but has a known implicit (deterministic) value: 0 for the significands, 0
     99 /// for zero exponent, all 1 bits for infinity exponent.  For NaNs the sign and
    100 /// significand are deterministic, although not really meaningful, and preserved
    101 /// in non-conversion operations.  The exponent is implicitly all 1 bits.
    102 ///
    103 /// APFloat does not provide any exception handling beyond default exception
    104 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
    105 /// by encoding Signaling NaNs with the first bit of its trailing significand as
    106 /// 0.
    107 ///
    108 /// TODO
    109 /// ====
    110 ///
    111 /// Some features that may or may not be worth adding:
    112 ///
    113 /// Binary to decimal conversion (hard).
    114 ///
    115 /// Optional ability to detect underflow tininess before rounding.
    116 ///
    117 /// New formats: x87 in single and double precision mode (IEEE apart from
    118 /// extended exponent range) (hard).
    119 ///
    120 /// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
    121 ///
    122 class APFloat {
    123 public:
    124 
    125   /// A signed type to represent a floating point numbers unbiased exponent.
    126   typedef signed short ExponentType;
    127 
    128   /// \name Floating Point Semantics.
    129   /// @{
    130 
    131   static const fltSemantics IEEEhalf;
    132   static const fltSemantics IEEEsingle;
    133   static const fltSemantics IEEEdouble;
    134   static const fltSemantics IEEEquad;
    135   static const fltSemantics PPCDoubleDouble;
    136   static const fltSemantics x87DoubleExtended;
    137 
    138   /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
    139   /// anything real.
    140   static const fltSemantics Bogus;
    141 
    142   /// @}
    143 
    144   static unsigned int semanticsPrecision(const fltSemantics &);
    145   static ExponentType semanticsMinExponent(const fltSemantics &);
    146   static ExponentType semanticsMaxExponent(const fltSemantics &);
    147   static unsigned int semanticsSizeInBits(const fltSemantics &);
    148 
    149   /// IEEE-754R 5.11: Floating Point Comparison Relations.
    150   enum cmpResult {
    151     cmpLessThan,
    152     cmpEqual,
    153     cmpGreaterThan,
    154     cmpUnordered
    155   };
    156 
    157   /// IEEE-754R 4.3: Rounding-direction attributes.
    158   enum roundingMode {
    159     rmNearestTiesToEven,
    160     rmTowardPositive,
    161     rmTowardNegative,
    162     rmTowardZero,
    163     rmNearestTiesToAway
    164   };
    165 
    166   /// IEEE-754R 7: Default exception handling.
    167   ///
    168   /// opUnderflow or opOverflow are always returned or-ed with opInexact.
    169   enum opStatus {
    170     opOK = 0x00,
    171     opInvalidOp = 0x01,
    172     opDivByZero = 0x02,
    173     opOverflow = 0x04,
    174     opUnderflow = 0x08,
    175     opInexact = 0x10
    176   };
    177 
    178   /// Category of internally-represented number.
    179   enum fltCategory {
    180     fcInfinity,
    181     fcNaN,
    182     fcNormal,
    183     fcZero
    184   };
    185 
    186   /// Convenience enum used to construct an uninitialized APFloat.
    187   enum uninitializedTag {
    188     uninitialized
    189   };
    190 
    191   /// \name Constructors
    192   /// @{
    193 
    194   APFloat(const fltSemantics &); // Default construct to 0.0
    195   APFloat(const fltSemantics &, StringRef);
    196   APFloat(const fltSemantics &, integerPart);
    197   APFloat(const fltSemantics &, uninitializedTag);
    198   APFloat(const fltSemantics &, const APInt &);
    199   explicit APFloat(double d);
    200   explicit APFloat(float f);
    201   APFloat(const APFloat &);
    202   APFloat(APFloat &&);
    203   ~APFloat();
    204 
    205   /// @}
    206 
    207   /// \brief Returns whether this instance allocated memory.
    208   bool needsCleanup() const { return partCount() > 1; }
    209 
    210   /// \name Convenience "constructors"
    211   /// @{
    212 
    213   /// Factory for Positive and Negative Zero.
    214   ///
    215   /// \param Negative True iff the number should be negative.
    216   static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
    217     APFloat Val(Sem, uninitialized);
    218     Val.makeZero(Negative);
    219     return Val;
    220   }
    221 
    222   /// Factory for Positive and Negative Infinity.
    223   ///
    224   /// \param Negative True iff the number should be negative.
    225   static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
    226     APFloat Val(Sem, uninitialized);
    227     Val.makeInf(Negative);
    228     return Val;
    229   }
    230 
    231   /// Factory for QNaN values.
    232   ///
    233   /// \param Negative - True iff the NaN generated should be negative.
    234   /// \param type - The unspecified fill bits for creating the NaN, 0 by
    235   /// default.  The value is truncated as necessary.
    236   static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
    237                         unsigned type = 0) {
    238     if (type) {
    239       APInt fill(64, type);
    240       return getQNaN(Sem, Negative, &fill);
    241     } else {
    242       return getQNaN(Sem, Negative, nullptr);
    243     }
    244   }
    245 
    246   /// Factory for QNaN values.
    247   static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
    248                          const APInt *payload = nullptr) {
    249     return makeNaN(Sem, false, Negative, payload);
    250   }
    251 
    252   /// Factory for SNaN values.
    253   static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
    254                          const APInt *payload = nullptr) {
    255     return makeNaN(Sem, true, Negative, payload);
    256   }
    257 
    258   /// Returns the largest finite number in the given semantics.
    259   ///
    260   /// \param Negative - True iff the number should be negative
    261   static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
    262 
    263   /// Returns the smallest (by magnitude) finite number in the given semantics.
    264   /// Might be denormalized, which implies a relative loss of precision.
    265   ///
    266   /// \param Negative - True iff the number should be negative
    267   static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
    268 
    269   /// Returns the smallest (by magnitude) normalized finite number in the given
    270   /// semantics.
    271   ///
    272   /// \param Negative - True iff the number should be negative
    273   static APFloat getSmallestNormalized(const fltSemantics &Sem,
    274                                        bool Negative = false);
    275 
    276   /// Returns a float which is bitcasted from an all one value int.
    277   ///
    278   /// \param BitWidth - Select float type
    279   /// \param isIEEE   - If 128 bit number, select between PPC and IEEE
    280   static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
    281 
    282   /// Returns the size of the floating point number (in bits) in the given
    283   /// semantics.
    284   static unsigned getSizeInBits(const fltSemantics &Sem);
    285 
    286   /// @}
    287 
    288   /// Used to insert APFloat objects, or objects that contain APFloat objects,
    289   /// into FoldingSets.
    290   void Profile(FoldingSetNodeID &NID) const;
    291 
    292   /// \name Arithmetic
    293   /// @{
    294 
    295   opStatus add(const APFloat &, roundingMode);
    296   opStatus subtract(const APFloat &, roundingMode);
    297   opStatus multiply(const APFloat &, roundingMode);
    298   opStatus divide(const APFloat &, roundingMode);
    299   /// IEEE remainder.
    300   opStatus remainder(const APFloat &);
    301   /// C fmod, or llvm frem.
    302   opStatus mod(const APFloat &);
    303   opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
    304   opStatus roundToIntegral(roundingMode);
    305   /// IEEE-754R 5.3.1: nextUp/nextDown.
    306   opStatus next(bool nextDown);
    307 
    308   /// \brief Operator+ overload which provides the default
    309   /// \c nmNearestTiesToEven rounding mode and *no* error checking.
    310   APFloat operator+(const APFloat &RHS) const {
    311     APFloat Result = *this;
    312     Result.add(RHS, rmNearestTiesToEven);
    313     return Result;
    314   }
    315 
    316   /// \brief Operator- overload which provides the default
    317   /// \c nmNearestTiesToEven rounding mode and *no* error checking.
    318   APFloat operator-(const APFloat &RHS) const {
    319     APFloat Result = *this;
    320     Result.subtract(RHS, rmNearestTiesToEven);
    321     return Result;
    322   }
    323 
    324   /// \brief Operator* overload which provides the default
    325   /// \c nmNearestTiesToEven rounding mode and *no* error checking.
    326   APFloat operator*(const APFloat &RHS) const {
    327     APFloat Result = *this;
    328     Result.multiply(RHS, rmNearestTiesToEven);
    329     return Result;
    330   }
    331 
    332   /// \brief Operator/ overload which provides the default
    333   /// \c nmNearestTiesToEven rounding mode and *no* error checking.
    334   APFloat operator/(const APFloat &RHS) const {
    335     APFloat Result = *this;
    336     Result.divide(RHS, rmNearestTiesToEven);
    337     return Result;
    338   }
    339 
    340   /// @}
    341 
    342   /// \name Sign operations.
    343   /// @{
    344 
    345   void changeSign();
    346   void clearSign();
    347   void copySign(const APFloat &);
    348 
    349   /// \brief A static helper to produce a copy of an APFloat value with its sign
    350   /// copied from some other APFloat.
    351   static APFloat copySign(APFloat Value, const APFloat &Sign) {
    352     Value.copySign(Sign);
    353     return Value;
    354   }
    355 
    356   /// @}
    357 
    358   /// \name Conversions
    359   /// @{
    360 
    361   opStatus convert(const fltSemantics &, roundingMode, bool *);
    362   opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode,
    363                             bool *) const;
    364   opStatus convertToInteger(APSInt &, roundingMode, bool *) const;
    365   opStatus convertFromAPInt(const APInt &, bool, roundingMode);
    366   opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
    367                                           bool, roundingMode);
    368   opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
    369                                           bool, roundingMode);
    370   opStatus convertFromString(StringRef, roundingMode);
    371   APInt bitcastToAPInt() const;
    372   double convertToDouble() const;
    373   float convertToFloat() const;
    374 
    375   /// @}
    376 
    377   /// The definition of equality is not straightforward for floating point, so
    378   /// we won't use operator==.  Use one of the following, or write whatever it
    379   /// is you really mean.
    380   bool operator==(const APFloat &) const = delete;
    381 
    382   /// IEEE comparison with another floating point number (NaNs compare
    383   /// unordered, 0==-0).
    384   cmpResult compare(const APFloat &) const;
    385 
    386   /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
    387   bool bitwiseIsEqual(const APFloat &) const;
    388 
    389   /// Write out a hexadecimal representation of the floating point value to DST,
    390   /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
    391   /// Return the number of characters written, excluding the terminating NUL.
    392   unsigned int convertToHexString(char *dst, unsigned int hexDigits,
    393                                   bool upperCase, roundingMode) const;
    394 
    395   /// \name IEEE-754R 5.7.2 General operations.
    396   /// @{
    397 
    398   /// IEEE-754R isSignMinus: Returns true if and only if the current value is
    399   /// negative.
    400   ///
    401   /// This applies to zeros and NaNs as well.
    402   bool isNegative() const { return sign; }
    403 
    404   /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
    405   ///
    406   /// This implies that the current value of the float is not zero, subnormal,
    407   /// infinite, or NaN following the definition of normality from IEEE-754R.
    408   bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
    409 
    410   /// Returns true if and only if the current value is zero, subnormal, or
    411   /// normal.
    412   ///
    413   /// This means that the value is not infinite or NaN.
    414   bool isFinite() const { return !isNaN() && !isInfinity(); }
    415 
    416   /// Returns true if and only if the float is plus or minus zero.
    417   bool isZero() const { return category == fcZero; }
    418 
    419   /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
    420   /// denormal.
    421   bool isDenormal() const;
    422 
    423   /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
    424   bool isInfinity() const { return category == fcInfinity; }
    425 
    426   /// Returns true if and only if the float is a quiet or signaling NaN.
    427   bool isNaN() const { return category == fcNaN; }
    428 
    429   /// Returns true if and only if the float is a signaling NaN.
    430   bool isSignaling() const;
    431 
    432   /// @}
    433 
    434   /// \name Simple Queries
    435   /// @{
    436 
    437   fltCategory getCategory() const { return category; }
    438   const fltSemantics &getSemantics() const { return *semantics; }
    439   bool isNonZero() const { return category != fcZero; }
    440   bool isFiniteNonZero() const { return isFinite() && !isZero(); }
    441   bool isPosZero() const { return isZero() && !isNegative(); }
    442   bool isNegZero() const { return isZero() && isNegative(); }
    443 
    444   /// Returns true if and only if the number has the smallest possible non-zero
    445   /// magnitude in the current semantics.
    446   bool isSmallest() const;
    447 
    448   /// Returns true if and only if the number has the largest possible finite
    449   /// magnitude in the current semantics.
    450   bool isLargest() const;
    451 
    452   /// Returns true if and only if the number is an exact integer.
    453   bool isInteger() const;
    454 
    455   /// @}
    456 
    457   APFloat &operator=(const APFloat &);
    458   APFloat &operator=(APFloat &&);
    459 
    460   /// \brief Overload to compute a hash code for an APFloat value.
    461   ///
    462   /// Note that the use of hash codes for floating point values is in general
    463   /// frought with peril. Equality is hard to define for these values. For
    464   /// example, should negative and positive zero hash to different codes? Are
    465   /// they equal or not? This hash value implementation specifically
    466   /// emphasizes producing different codes for different inputs in order to
    467   /// be used in canonicalization and memoization. As such, equality is
    468   /// bitwiseIsEqual, and 0 != -0.
    469   friend hash_code hash_value(const APFloat &Arg);
    470 
    471   /// Converts this value into a decimal string.
    472   ///
    473   /// \param FormatPrecision The maximum number of digits of
    474   ///   precision to output.  If there are fewer digits available,
    475   ///   zero padding will not be used unless the value is
    476   ///   integral and small enough to be expressed in
    477   ///   FormatPrecision digits.  0 means to use the natural
    478   ///   precision of the number.
    479   /// \param FormatMaxPadding The maximum number of zeros to
    480   ///   consider inserting before falling back to scientific
    481   ///   notation.  0 means to always use scientific notation.
    482   ///
    483   /// Number       Precision    MaxPadding      Result
    484   /// ------       ---------    ----------      ------
    485   /// 1.01E+4              5             2       10100
    486   /// 1.01E+4              4             2       1.01E+4
    487   /// 1.01E+4              5             1       1.01E+4
    488   /// 1.01E-2              5             2       0.0101
    489   /// 1.01E-2              4             2       0.0101
    490   /// 1.01E-2              4             1       1.01E-2
    491   void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
    492                 unsigned FormatMaxPadding = 3) const;
    493 
    494   /// If this value has an exact multiplicative inverse, store it in inv and
    495   /// return true.
    496   bool getExactInverse(APFloat *inv) const;
    497 
    498   /// \brief Enumeration of \c ilogb error results.
    499   enum IlogbErrorKinds {
    500     IEK_Zero = INT_MIN+1,
    501     IEK_NaN = INT_MIN,
    502     IEK_Inf = INT_MAX
    503   };
    504 
    505   /// \brief Returns the exponent of the internal representation of the APFloat.
    506   ///
    507   /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
    508   /// For special APFloat values, this returns special error codes:
    509   ///
    510   ///   NaN -> \c IEK_NaN
    511   ///   0   -> \c IEK_Zero
    512   ///   Inf -> \c IEK_Inf
    513   ///
    514   friend int ilogb(const APFloat &Arg) {
    515     if (Arg.isNaN())
    516       return IEK_NaN;
    517     if (Arg.isZero())
    518       return IEK_Zero;
    519     if (Arg.isInfinity())
    520       return IEK_Inf;
    521 
    522     return Arg.exponent;
    523   }
    524 
    525   /// \brief Returns: X * 2^Exp for integral exponents.
    526   friend APFloat scalbn(APFloat X, int Exp);
    527 
    528 private:
    529 
    530   /// \name Simple Queries
    531   /// @{
    532 
    533   integerPart *significandParts();
    534   const integerPart *significandParts() const;
    535   unsigned int partCount() const;
    536 
    537   /// @}
    538 
    539   /// \name Significand operations.
    540   /// @{
    541 
    542   integerPart addSignificand(const APFloat &);
    543   integerPart subtractSignificand(const APFloat &, integerPart);
    544   lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
    545   lostFraction multiplySignificand(const APFloat &, const APFloat *);
    546   lostFraction divideSignificand(const APFloat &);
    547   void incrementSignificand();
    548   void initialize(const fltSemantics *);
    549   void shiftSignificandLeft(unsigned int);
    550   lostFraction shiftSignificandRight(unsigned int);
    551   unsigned int significandLSB() const;
    552   unsigned int significandMSB() const;
    553   void zeroSignificand();
    554   /// Return true if the significand excluding the integral bit is all ones.
    555   bool isSignificandAllOnes() const;
    556   /// Return true if the significand excluding the integral bit is all zeros.
    557   bool isSignificandAllZeros() const;
    558 
    559   /// @}
    560 
    561   /// \name Arithmetic on special values.
    562   /// @{
    563 
    564   opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
    565   opStatus divideSpecials(const APFloat &);
    566   opStatus multiplySpecials(const APFloat &);
    567   opStatus modSpecials(const APFloat &);
    568 
    569   /// @}
    570 
    571   /// \name Special value setters.
    572   /// @{
    573 
    574   void makeLargest(bool Neg = false);
    575   void makeSmallest(bool Neg = false);
    576   void makeNaN(bool SNaN = false, bool Neg = false,
    577                const APInt *fill = nullptr);
    578   static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
    579                          const APInt *fill);
    580   void makeInf(bool Neg = false);
    581   void makeZero(bool Neg = false);
    582 
    583   /// @}
    584 
    585   /// \name Miscellany
    586   /// @{
    587 
    588   bool convertFromStringSpecials(StringRef str);
    589   opStatus normalize(roundingMode, lostFraction);
    590   opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
    591   cmpResult compareAbsoluteValue(const APFloat &) const;
    592   opStatus handleOverflow(roundingMode);
    593   bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
    594   opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
    595                                         roundingMode, bool *) const;
    596   opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
    597                                     roundingMode);
    598   opStatus convertFromHexadecimalString(StringRef, roundingMode);
    599   opStatus convertFromDecimalString(StringRef, roundingMode);
    600   char *convertNormalToHexString(char *, unsigned int, bool,
    601                                  roundingMode) const;
    602   opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
    603                                         roundingMode);
    604 
    605   /// @}
    606 
    607   APInt convertHalfAPFloatToAPInt() const;
    608   APInt convertFloatAPFloatToAPInt() const;
    609   APInt convertDoubleAPFloatToAPInt() const;
    610   APInt convertQuadrupleAPFloatToAPInt() const;
    611   APInt convertF80LongDoubleAPFloatToAPInt() const;
    612   APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
    613   void initFromAPInt(const fltSemantics *Sem, const APInt &api);
    614   void initFromHalfAPInt(const APInt &api);
    615   void initFromFloatAPInt(const APInt &api);
    616   void initFromDoubleAPInt(const APInt &api);
    617   void initFromQuadrupleAPInt(const APInt &api);
    618   void initFromF80LongDoubleAPInt(const APInt &api);
    619   void initFromPPCDoubleDoubleAPInt(const APInt &api);
    620 
    621   void assign(const APFloat &);
    622   void copySignificand(const APFloat &);
    623   void freeSignificand();
    624 
    625   /// The semantics that this value obeys.
    626   const fltSemantics *semantics;
    627 
    628   /// A binary fraction with an explicit integer bit.
    629   ///
    630   /// The significand must be at least one bit wider than the target precision.
    631   union Significand {
    632     integerPart part;
    633     integerPart *parts;
    634   } significand;
    635 
    636   /// The signed unbiased exponent of the value.
    637   ExponentType exponent;
    638 
    639   /// What kind of floating point number this is.
    640   ///
    641   /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
    642   /// Using the extra bit keeps it from failing under VisualStudio.
    643   fltCategory category : 3;
    644 
    645   /// Sign bit of the number.
    646   unsigned int sign : 1;
    647 };
    648 
    649 /// See friend declarations above.
    650 ///
    651 /// These additional declarations are required in order to compile LLVM with IBM
    652 /// xlC compiler.
    653 hash_code hash_value(const APFloat &Arg);
    654 APFloat scalbn(APFloat X, int Exp);
    655 
    656 /// \brief Returns the absolute value of the argument.
    657 inline APFloat abs(APFloat X) {
    658   X.clearSign();
    659   return X;
    660 }
    661 
    662 /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
    663 /// both are not NaN. If either argument is a NaN, returns the other argument.
    664 LLVM_READONLY
    665 inline APFloat minnum(const APFloat &A, const APFloat &B) {
    666   if (A.isNaN())
    667     return B;
    668   if (B.isNaN())
    669     return A;
    670   return (B.compare(A) == APFloat::cmpLessThan) ? B : A;
    671 }
    672 
    673 /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
    674 /// both are not NaN. If either argument is a NaN, returns the other argument.
    675 LLVM_READONLY
    676 inline APFloat maxnum(const APFloat &A, const APFloat &B) {
    677   if (A.isNaN())
    678     return B;
    679   if (B.isNaN())
    680     return A;
    681   return (A.compare(B) == APFloat::cmpLessThan) ? B : A;
    682 }
    683 
    684 } // namespace llvm
    685 
    686 #endif // LLVM_ADT_APFLOAT_H
    687