LCOV - code coverage report
Current view: top level - include/llvm/ADT - APFloat.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 418 454 92.1 %
Date: 2018-10-20 13:21:21 Functions: 68 73 93.2 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include "llvm/ADT/ArrayRef.h"
      22             : #include "llvm/Support/ErrorHandling.h"
      23             : #include <memory>
      24             : 
      25             : #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)                             \
      26             :   do {                                                                         \
      27             :     if (usesLayout<IEEEFloat>(getSemantics()))                                 \
      28             :       return U.IEEE.METHOD_CALL;                                               \
      29             :     if (usesLayout<DoubleAPFloat>(getSemantics()))                             \
      30             :       return U.Double.METHOD_CALL;                                             \
      31             :     llvm_unreachable("Unexpected semantics");                                  \
      32             :   } while (false)
      33             : 
      34             : namespace llvm {
      35             : 
      36             : struct fltSemantics;
      37             : class APSInt;
      38             : class StringRef;
      39             : class APFloat;
      40             : class raw_ostream;
      41             : 
      42             : template <typename T> class SmallVectorImpl;
      43             : 
      44             : /// Enum that represents what fraction of the LSB truncated bits of an fp number
      45             : /// represent.
      46             : ///
      47             : /// This essentially combines the roles of guard and sticky bits.
      48             : enum lostFraction { // Example of truncated bits:
      49             :   lfExactlyZero,    // 000000
      50             :   lfLessThanHalf,   // 0xxxxx  x's not all zero
      51             :   lfExactlyHalf,    // 100000
      52             :   lfMoreThanHalf    // 1xxxxx  x's not all zero
      53             : };
      54             : 
      55             : /// A self-contained host- and target-independent arbitrary-precision
      56             : /// floating-point software implementation.
      57             : ///
      58             : /// APFloat uses bignum integer arithmetic as provided by static functions in
      59             : /// the APInt class.  The library will work with bignum integers whose parts are
      60             : /// any unsigned type at least 16 bits wide, but 64 bits is recommended.
      61             : ///
      62             : /// Written for clarity rather than speed, in particular with a view to use in
      63             : /// the front-end of a cross compiler so that target arithmetic can be correctly
      64             : /// performed on the host.  Performance should nonetheless be reasonable,
      65             : /// particularly for its intended use.  It may be useful as a base
      66             : /// implementation for a run-time library during development of a faster
      67             : /// target-specific one.
      68             : ///
      69             : /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
      70             : /// implemented operations.  Currently implemented operations are add, subtract,
      71             : /// multiply, divide, fused-multiply-add, conversion-to-float,
      72             : /// conversion-to-integer and conversion-from-integer.  New rounding modes
      73             : /// (e.g. away from zero) can be added with three or four lines of code.
      74             : ///
      75             : /// Four formats are built-in: IEEE single precision, double precision,
      76             : /// quadruple precision, and x87 80-bit extended double (when operating with
      77             : /// full extended precision).  Adding a new format that obeys IEEE semantics
      78             : /// only requires adding two lines of code: a declaration and definition of the
      79             : /// format.
      80             : ///
      81             : /// All operations return the status of that operation as an exception bit-mask,
      82             : /// so multiple operations can be done consecutively with their results or-ed
      83             : /// together.  The returned status can be useful for compiler diagnostics; e.g.,
      84             : /// inexact, underflow and overflow can be easily diagnosed on constant folding,
      85             : /// and compiler optimizers can determine what exceptions would be raised by
      86             : /// folding operations and optimize, or perhaps not optimize, accordingly.
      87             : ///
      88             : /// At present, underflow tininess is detected after rounding; it should be
      89             : /// straight forward to add support for the before-rounding case too.
      90             : ///
      91             : /// The library reads hexadecimal floating point numbers as per C99, and
      92             : /// correctly rounds if necessary according to the specified rounding mode.
      93             : /// Syntax is required to have been validated by the caller.  It also converts
      94             : /// floating point numbers to hexadecimal text as per the C99 %a and %A
      95             : /// conversions.  The output precision (or alternatively the natural minimal
      96             : /// precision) can be specified; if the requested precision is less than the
      97             : /// natural precision the output is correctly rounded for the specified rounding
      98             : /// mode.
      99             : ///
     100             : /// It also reads decimal floating point numbers and correctly rounds according
     101             : /// to the specified rounding mode.
     102             : ///
     103             : /// Conversion to decimal text is not currently implemented.
     104             : ///
     105             : /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
     106             : /// signed exponent, and the significand as an array of integer parts.  After
     107             : /// normalization of a number of precision P the exponent is within the range of
     108             : /// the format, and if the number is not denormal the P-th bit of the
     109             : /// significand is set as an explicit integer bit.  For denormals the most
     110             : /// significant bit is shifted right so that the exponent is maintained at the
     111             : /// format's minimum, so that the smallest denormal has just the least
     112             : /// significant bit of the significand set.  The sign of zeroes and infinities
     113             : /// is significant; the exponent and significand of such numbers is not stored,
     114             : /// but has a known implicit (deterministic) value: 0 for the significands, 0
     115             : /// for zero exponent, all 1 bits for infinity exponent.  For NaNs the sign and
     116             : /// significand are deterministic, although not really meaningful, and preserved
     117             : /// in non-conversion operations.  The exponent is implicitly all 1 bits.
     118             : ///
     119             : /// APFloat does not provide any exception handling beyond default exception
     120             : /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
     121             : /// by encoding Signaling NaNs with the first bit of its trailing significand as
     122             : /// 0.
     123             : ///
     124             : /// TODO
     125             : /// ====
     126             : ///
     127             : /// Some features that may or may not be worth adding:
     128             : ///
     129             : /// Binary to decimal conversion (hard).
     130             : ///
     131             : /// Optional ability to detect underflow tininess before rounding.
     132             : ///
     133             : /// New formats: x87 in single and double precision mode (IEEE apart from
     134             : /// extended exponent range) (hard).
     135             : ///
     136             : /// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
     137             : ///
     138             : 
     139             : // This is the common type definitions shared by APFloat and its internal
     140             : // implementation classes. This struct should not define any non-static data
     141             : // members.
     142             : struct APFloatBase {
     143             :   typedef APInt::WordType integerPart;
     144             :   static const unsigned integerPartWidth = APInt::APINT_BITS_PER_WORD;
     145             : 
     146             :   /// A signed type to represent a floating point numbers unbiased exponent.
     147             :   typedef signed short ExponentType;
     148             : 
     149             :   /// \name Floating Point Semantics.
     150             :   /// @{
     151             : 
     152             :   static const fltSemantics &IEEEhalf() LLVM_READNONE;
     153             :   static const fltSemantics &IEEEsingle() LLVM_READNONE;
     154             :   static const fltSemantics &IEEEdouble() LLVM_READNONE;
     155             :   static const fltSemantics &IEEEquad() LLVM_READNONE;
     156             :   static const fltSemantics &PPCDoubleDouble() LLVM_READNONE;
     157             :   static const fltSemantics &x87DoubleExtended() LLVM_READNONE;
     158             : 
     159             :   /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
     160             :   /// anything real.
     161             :   static const fltSemantics &Bogus() LLVM_READNONE;
     162             : 
     163             :   /// @}
     164             : 
     165             :   /// IEEE-754R 5.11: Floating Point Comparison Relations.
     166             :   enum cmpResult {
     167             :     cmpLessThan,
     168             :     cmpEqual,
     169             :     cmpGreaterThan,
     170             :     cmpUnordered
     171             :   };
     172             : 
     173             :   /// IEEE-754R 4.3: Rounding-direction attributes.
     174             :   enum roundingMode {
     175             :     rmNearestTiesToEven,
     176             :     rmTowardPositive,
     177             :     rmTowardNegative,
     178             :     rmTowardZero,
     179             :     rmNearestTiesToAway
     180             :   };
     181             : 
     182             :   /// IEEE-754R 7: Default exception handling.
     183             :   ///
     184             :   /// opUnderflow or opOverflow are always returned or-ed with opInexact.
     185             :   enum opStatus {
     186             :     opOK = 0x00,
     187             :     opInvalidOp = 0x01,
     188             :     opDivByZero = 0x02,
     189             :     opOverflow = 0x04,
     190             :     opUnderflow = 0x08,
     191             :     opInexact = 0x10
     192             :   };
     193             : 
     194             :   /// Category of internally-represented number.
     195             :   enum fltCategory {
     196             :     fcInfinity,
     197             :     fcNaN,
     198             :     fcNormal,
     199             :     fcZero
     200             :   };
     201             : 
     202             :   /// Convenience enum used to construct an uninitialized APFloat.
     203             :   enum uninitializedTag {
     204             :     uninitialized
     205             :   };
     206             : 
     207             :   /// Enumeration of \c ilogb error results.
     208             :   enum IlogbErrorKinds {
     209             :     IEK_Zero = INT_MIN + 1,
     210             :     IEK_NaN = INT_MIN,
     211             :     IEK_Inf = INT_MAX
     212             :   };
     213             : 
     214             :   static unsigned int semanticsPrecision(const fltSemantics &);
     215             :   static ExponentType semanticsMinExponent(const fltSemantics &);
     216             :   static ExponentType semanticsMaxExponent(const fltSemantics &);
     217             :   static unsigned int semanticsSizeInBits(const fltSemantics &);
     218             : 
     219             :   /// Returns the size of the floating point number (in bits) in the given
     220             :   /// semantics.
     221             :   static unsigned getSizeInBits(const fltSemantics &Sem);
     222             : };
     223             : 
     224             : namespace detail {
     225             : 
     226             : class IEEEFloat final : public APFloatBase {
     227             : public:
     228             :   /// \name Constructors
     229             :   /// @{
     230             : 
     231             :   IEEEFloat(const fltSemantics &); // Default construct to 0.0
     232             :   IEEEFloat(const fltSemantics &, integerPart);
     233             :   IEEEFloat(const fltSemantics &, uninitializedTag);
     234             :   IEEEFloat(const fltSemantics &, const APInt &);
     235             :   explicit IEEEFloat(double d);
     236             :   explicit IEEEFloat(float f);
     237             :   IEEEFloat(const IEEEFloat &);
     238             :   IEEEFloat(IEEEFloat &&);
     239             :   ~IEEEFloat();
     240             : 
     241             :   /// @}
     242             : 
     243             :   /// Returns whether this instance allocated memory.
     244    17923853 :   bool needsCleanup() const { return partCount() > 1; }
     245             : 
     246             :   /// \name Convenience "constructors"
     247             :   /// @{
     248             : 
     249             :   /// @}
     250             : 
     251             :   /// \name Arithmetic
     252             :   /// @{
     253             : 
     254             :   opStatus add(const IEEEFloat &, roundingMode);
     255             :   opStatus subtract(const IEEEFloat &, roundingMode);
     256             :   opStatus multiply(const IEEEFloat &, roundingMode);
     257             :   opStatus divide(const IEEEFloat &, roundingMode);
     258             :   /// IEEE remainder.
     259             :   opStatus remainder(const IEEEFloat &);
     260             :   /// C fmod, or llvm frem.
     261             :   opStatus mod(const IEEEFloat &);
     262             :   opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode);
     263             :   opStatus roundToIntegral(roundingMode);
     264             :   /// IEEE-754R 5.3.1: nextUp/nextDown.
     265             :   opStatus next(bool nextDown);
     266             : 
     267             :   /// @}
     268             : 
     269             :   /// \name Sign operations.
     270             :   /// @{
     271             : 
     272             :   void changeSign();
     273             : 
     274             :   /// @}
     275             : 
     276             :   /// \name Conversions
     277             :   /// @{
     278             : 
     279             :   opStatus convert(const fltSemantics &, roundingMode, bool *);
     280             :   opStatus convertToInteger(MutableArrayRef<integerPart>, unsigned int, bool,
     281             :                             roundingMode, bool *) const;
     282             :   opStatus convertFromAPInt(const APInt &, bool, roundingMode);
     283             :   opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
     284             :                                           bool, roundingMode);
     285             :   opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
     286             :                                           bool, roundingMode);
     287             :   opStatus convertFromString(StringRef, roundingMode);
     288             :   APInt bitcastToAPInt() const;
     289             :   double convertToDouble() const;
     290             :   float convertToFloat() const;
     291             : 
     292             :   /// @}
     293             : 
     294             :   /// The definition of equality is not straightforward for floating point, so
     295             :   /// we won't use operator==.  Use one of the following, or write whatever it
     296             :   /// is you really mean.
     297             :   bool operator==(const IEEEFloat &) const = delete;
     298             : 
     299             :   /// IEEE comparison with another floating point number (NaNs compare
     300             :   /// unordered, 0==-0).
     301             :   cmpResult compare(const IEEEFloat &) const;
     302             : 
     303             :   /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
     304             :   bool bitwiseIsEqual(const IEEEFloat &) const;
     305             : 
     306             :   /// Write out a hexadecimal representation of the floating point value to DST,
     307             :   /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
     308             :   /// Return the number of characters written, excluding the terminating NUL.
     309             :   unsigned int convertToHexString(char *dst, unsigned int hexDigits,
     310             :                                   bool upperCase, roundingMode) const;
     311             : 
     312             :   /// \name IEEE-754R 5.7.2 General operations.
     313             :   /// @{
     314             : 
     315             :   /// IEEE-754R isSignMinus: Returns true if and only if the current value is
     316             :   /// negative.
     317             :   ///
     318             :   /// This applies to zeros and NaNs as well.
     319      104706 :   bool isNegative() const { return sign; }
     320             : 
     321             :   /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
     322             :   ///
     323             :   /// This implies that the current value of the float is not zero, subnormal,
     324             :   /// infinite, or NaN following the definition of normality from IEEE-754R.
     325             :   bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
     326             : 
     327             :   /// Returns true if and only if the current value is zero, subnormal, or
     328             :   /// normal.
     329             :   ///
     330             :   /// This means that the value is not infinite or NaN.
     331     4600204 :   bool isFinite() const { return !isNaN() && !isInfinity(); }
     332             : 
     333             :   /// Returns true if and only if the float is plus or minus zero.
     334         111 :   bool isZero() const { return category == fcZero; }
     335             : 
     336             :   /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
     337             :   /// denormal.
     338             :   bool isDenormal() const;
     339             : 
     340             :   /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
     341             :   bool isInfinity() const { return category == fcInfinity; }
     342             : 
     343             :   /// Returns true if and only if the float is a quiet or signaling NaN.
     344     3680711 :   bool isNaN() const { return category == fcNaN; }
     345             : 
     346             :   /// Returns true if and only if the float is a signaling NaN.
     347             :   bool isSignaling() const;
     348             : 
     349             :   /// @}
     350             : 
     351             :   /// \name Simple Queries
     352             :   /// @{
     353             : 
     354      204927 :   fltCategory getCategory() const { return category; }
     355           0 :   const fltSemantics &getSemantics() const { return *semantics; }
     356          44 :   bool isNonZero() const { return category != fcZero; }
     357     4526121 :   bool isFiniteNonZero() const { return isFinite() && !isZero(); }
     358             :   bool isPosZero() const { return isZero() && !isNegative(); }
     359             :   bool isNegZero() const { return isZero() && isNegative(); }
     360             : 
     361             :   /// Returns true if and only if the number has the smallest possible non-zero
     362             :   /// magnitude in the current semantics.
     363             :   bool isSmallest() const;
     364             : 
     365             :   /// Returns true if and only if the number has the largest possible finite
     366             :   /// magnitude in the current semantics.
     367             :   bool isLargest() const;
     368             : 
     369             :   /// Returns true if and only if the number is an exact integer.
     370             :   bool isInteger() const;
     371             : 
     372             :   /// @}
     373             : 
     374             :   IEEEFloat &operator=(const IEEEFloat &);
     375             :   IEEEFloat &operator=(IEEEFloat &&);
     376             : 
     377             :   /// Overload to compute a hash code for an APFloat value.
     378             :   ///
     379             :   /// Note that the use of hash codes for floating point values is in general
     380             :   /// frought with peril. Equality is hard to define for these values. For
     381             :   /// example, should negative and positive zero hash to different codes? Are
     382             :   /// they equal or not? This hash value implementation specifically
     383             :   /// emphasizes producing different codes for different inputs in order to
     384             :   /// be used in canonicalization and memoization. As such, equality is
     385             :   /// bitwiseIsEqual, and 0 != -0.
     386             :   friend hash_code hash_value(const IEEEFloat &Arg);
     387             : 
     388             :   /// Converts this value into a decimal string.
     389             :   ///
     390             :   /// \param FormatPrecision The maximum number of digits of
     391             :   ///   precision to output.  If there are fewer digits available,
     392             :   ///   zero padding will not be used unless the value is
     393             :   ///   integral and small enough to be expressed in
     394             :   ///   FormatPrecision digits.  0 means to use the natural
     395             :   ///   precision of the number.
     396             :   /// \param FormatMaxPadding The maximum number of zeros to
     397             :   ///   consider inserting before falling back to scientific
     398             :   ///   notation.  0 means to always use scientific notation.
     399             :   ///
     400             :   /// \param TruncateZero Indicate whether to remove the trailing zero in
     401             :   ///   fraction part or not. Also setting this parameter to false forcing
     402             :   ///   producing of output more similar to default printf behavior.
     403             :   ///   Specifically the lower e is used as exponent delimiter and exponent
     404             :   ///   always contains no less than two digits.
     405             :   ///
     406             :   /// Number       Precision    MaxPadding      Result
     407             :   /// ------       ---------    ----------      ------
     408             :   /// 1.01E+4              5             2       10100
     409             :   /// 1.01E+4              4             2       1.01E+4
     410             :   /// 1.01E+4              5             1       1.01E+4
     411             :   /// 1.01E-2              5             2       0.0101
     412             :   /// 1.01E-2              4             2       0.0101
     413             :   /// 1.01E-2              4             1       1.01E-2
     414             :   void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
     415             :                 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const;
     416             : 
     417             :   /// If this value has an exact multiplicative inverse, store it in inv and
     418             :   /// return true.
     419             :   bool getExactInverse(APFloat *inv) const;
     420             : 
     421             :   /// Returns the exponent of the internal representation of the APFloat.
     422             :   ///
     423             :   /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
     424             :   /// For special APFloat values, this returns special error codes:
     425             :   ///
     426             :   ///   NaN -> \c IEK_NaN
     427             :   ///   0   -> \c IEK_Zero
     428             :   ///   Inf -> \c IEK_Inf
     429             :   ///
     430             :   friend int ilogb(const IEEEFloat &Arg);
     431             : 
     432             :   /// Returns: X * 2^Exp for integral exponents.
     433             :   friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
     434             : 
     435             :   friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
     436             : 
     437             :   /// \name Special value setters.
     438             :   /// @{
     439             : 
     440             :   void makeLargest(bool Neg = false);
     441             :   void makeSmallest(bool Neg = false);
     442             :   void makeNaN(bool SNaN = false, bool Neg = false,
     443             :                const APInt *fill = nullptr);
     444             :   void makeInf(bool Neg = false);
     445             :   void makeZero(bool Neg = false);
     446             :   void makeQuiet();
     447             : 
     448             :   /// Returns the smallest (by magnitude) normalized finite number in the given
     449             :   /// semantics.
     450             :   ///
     451             :   /// \param Negative - True iff the number should be negative
     452             :   void makeSmallestNormalized(bool Negative = false);
     453             : 
     454             :   /// @}
     455             : 
     456             :   cmpResult compareAbsoluteValue(const IEEEFloat &) const;
     457             : 
     458             : private:
     459             :   /// \name Simple Queries
     460             :   /// @{
     461             : 
     462             :   integerPart *significandParts();
     463             :   const integerPart *significandParts() const;
     464             :   unsigned int partCount() const;
     465             : 
     466             :   /// @}
     467             : 
     468             :   /// \name Significand operations.
     469             :   /// @{
     470             : 
     471             :   integerPart addSignificand(const IEEEFloat &);
     472             :   integerPart subtractSignificand(const IEEEFloat &, integerPart);
     473             :   lostFraction addOrSubtractSignificand(const IEEEFloat &, bool subtract);
     474             :   lostFraction multiplySignificand(const IEEEFloat &, const IEEEFloat *);
     475             :   lostFraction divideSignificand(const IEEEFloat &);
     476             :   void incrementSignificand();
     477             :   void initialize(const fltSemantics *);
     478             :   void shiftSignificandLeft(unsigned int);
     479             :   lostFraction shiftSignificandRight(unsigned int);
     480             :   unsigned int significandLSB() const;
     481             :   unsigned int significandMSB() const;
     482             :   void zeroSignificand();
     483             :   /// Return true if the significand excluding the integral bit is all ones.
     484             :   bool isSignificandAllOnes() const;
     485             :   /// Return true if the significand excluding the integral bit is all zeros.
     486             :   bool isSignificandAllZeros() const;
     487             : 
     488             :   /// @}
     489             : 
     490             :   /// \name Arithmetic on special values.
     491             :   /// @{
     492             : 
     493             :   opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract);
     494             :   opStatus divideSpecials(const IEEEFloat &);
     495             :   opStatus multiplySpecials(const IEEEFloat &);
     496             :   opStatus modSpecials(const IEEEFloat &);
     497             : 
     498             :   /// @}
     499             : 
     500             :   /// \name Miscellany
     501             :   /// @{
     502             : 
     503             :   bool convertFromStringSpecials(StringRef str);
     504             :   opStatus normalize(roundingMode, lostFraction);
     505             :   opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
     506             :   opStatus handleOverflow(roundingMode);
     507             :   bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
     508             :   opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
     509             :                                         unsigned int, bool, roundingMode,
     510             :                                         bool *) const;
     511             :   opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
     512             :                                     roundingMode);
     513             :   opStatus convertFromHexadecimalString(StringRef, roundingMode);
     514             :   opStatus convertFromDecimalString(StringRef, roundingMode);
     515             :   char *convertNormalToHexString(char *, unsigned int, bool,
     516             :                                  roundingMode) const;
     517             :   opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
     518             :                                         roundingMode);
     519             : 
     520             :   /// @}
     521             : 
     522             :   APInt convertHalfAPFloatToAPInt() const;
     523             :   APInt convertFloatAPFloatToAPInt() const;
     524             :   APInt convertDoubleAPFloatToAPInt() const;
     525             :   APInt convertQuadrupleAPFloatToAPInt() const;
     526             :   APInt convertF80LongDoubleAPFloatToAPInt() const;
     527             :   APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
     528             :   void initFromAPInt(const fltSemantics *Sem, const APInt &api);
     529             :   void initFromHalfAPInt(const APInt &api);
     530             :   void initFromFloatAPInt(const APInt &api);
     531             :   void initFromDoubleAPInt(const APInt &api);
     532             :   void initFromQuadrupleAPInt(const APInt &api);
     533             :   void initFromF80LongDoubleAPInt(const APInt &api);
     534             :   void initFromPPCDoubleDoubleAPInt(const APInt &api);
     535             : 
     536             :   void assign(const IEEEFloat &);
     537             :   void copySignificand(const IEEEFloat &);
     538             :   void freeSignificand();
     539             : 
     540             :   /// Note: this must be the first data member.
     541             :   /// The semantics that this value obeys.
     542             :   const fltSemantics *semantics;
     543             : 
     544             :   /// A binary fraction with an explicit integer bit.
     545             :   ///
     546             :   /// The significand must be at least one bit wider than the target precision.
     547             :   union Significand {
     548             :     integerPart part;
     549             :     integerPart *parts;
     550             :   } significand;
     551             : 
     552             :   /// The signed unbiased exponent of the value.
     553             :   ExponentType exponent;
     554             : 
     555             :   /// What kind of floating point number this is.
     556             :   ///
     557             :   /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
     558             :   /// Using the extra bit keeps it from failing under VisualStudio.
     559             :   fltCategory category : 3;
     560             : 
     561             :   /// Sign bit of the number.
     562             :   unsigned int sign : 1;
     563             : };
     564             : 
     565             : hash_code hash_value(const IEEEFloat &Arg);
     566             : int ilogb(const IEEEFloat &Arg);
     567             : IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode);
     568             : IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM);
     569             : 
     570             : // This mode implements more precise float in terms of two APFloats.
     571             : // The interface and layout is designed for arbitray underlying semantics,
     572             : // though currently only PPCDoubleDouble semantics are supported, whose
     573             : // corresponding underlying semantics are IEEEdouble.
     574          42 : class DoubleAPFloat final : public APFloatBase {
     575             :   // Note: this must be the first data member.
     576             :   const fltSemantics *Semantics;
     577             :   std::unique_ptr<APFloat[]> Floats;
     578             : 
     579             :   opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c,
     580             :                    const APFloat &cc, roundingMode RM);
     581             : 
     582             :   opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS,
     583             :                           DoubleAPFloat &Out, roundingMode RM);
     584             : 
     585             : public:
     586             :   DoubleAPFloat(const fltSemantics &S);
     587             :   DoubleAPFloat(const fltSemantics &S, uninitializedTag);
     588             :   DoubleAPFloat(const fltSemantics &S, integerPart);
     589             :   DoubleAPFloat(const fltSemantics &S, const APInt &I);
     590             :   DoubleAPFloat(const fltSemantics &S, APFloat &&First, APFloat &&Second);
     591             :   DoubleAPFloat(const DoubleAPFloat &RHS);
     592             :   DoubleAPFloat(DoubleAPFloat &&RHS);
     593             : 
     594             :   DoubleAPFloat &operator=(const DoubleAPFloat &RHS);
     595             : 
     596          90 :   DoubleAPFloat &operator=(DoubleAPFloat &&RHS) {
     597          90 :     if (this != &RHS) {
     598             :       this->~DoubleAPFloat();
     599          90 :       new (this) DoubleAPFloat(std::move(RHS));
     600             :     }
     601          90 :     return *this;
     602             :   }
     603             : 
     604             :   bool needsCleanup() const { return Floats != nullptr; }
     605             : 
     606             :   APFloat &getFirst() { return Floats[0]; }
     607             :   const APFloat &getFirst() const { return Floats[0]; }
     608             :   APFloat &getSecond() { return Floats[1]; }
     609             :   const APFloat &getSecond() const { return Floats[1]; }
     610             : 
     611             :   opStatus add(const DoubleAPFloat &RHS, roundingMode RM);
     612             :   opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM);
     613             :   opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM);
     614             :   opStatus divide(const DoubleAPFloat &RHS, roundingMode RM);
     615             :   opStatus remainder(const DoubleAPFloat &RHS);
     616             :   opStatus mod(const DoubleAPFloat &RHS);
     617             :   opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
     618             :                             const DoubleAPFloat &Addend, roundingMode RM);
     619             :   opStatus roundToIntegral(roundingMode RM);
     620             :   void changeSign();
     621             :   cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const;
     622             : 
     623             :   fltCategory getCategory() const;
     624             :   bool isNegative() const;
     625             : 
     626             :   void makeInf(bool Neg);
     627             :   void makeZero(bool Neg);
     628             :   void makeLargest(bool Neg);
     629             :   void makeSmallest(bool Neg);
     630             :   void makeSmallestNormalized(bool Neg);
     631             :   void makeNaN(bool SNaN, bool Neg, const APInt *fill);
     632             : 
     633             :   cmpResult compare(const DoubleAPFloat &RHS) const;
     634             :   bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
     635             :   APInt bitcastToAPInt() const;
     636             :   opStatus convertFromString(StringRef, roundingMode);
     637             :   opStatus next(bool nextDown);
     638             : 
     639             :   opStatus convertToInteger(MutableArrayRef<integerPart> Input,
     640             :                             unsigned int Width, bool IsSigned, roundingMode RM,
     641             :                             bool *IsExact) const;
     642             :   opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM);
     643             :   opStatus convertFromSignExtendedInteger(const integerPart *Input,
     644             :                                           unsigned int InputSize, bool IsSigned,
     645             :                                           roundingMode RM);
     646             :   opStatus convertFromZeroExtendedInteger(const integerPart *Input,
     647             :                                           unsigned int InputSize, bool IsSigned,
     648             :                                           roundingMode RM);
     649             :   unsigned int convertToHexString(char *DST, unsigned int HexDigits,
     650             :                                   bool UpperCase, roundingMode RM) const;
     651             : 
     652             :   bool isDenormal() const;
     653             :   bool isSmallest() const;
     654             :   bool isLargest() const;
     655             :   bool isInteger() const;
     656             : 
     657             :   void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
     658             :                 unsigned FormatMaxPadding, bool TruncateZero = true) const;
     659             : 
     660             :   bool getExactInverse(APFloat *inv) const;
     661             : 
     662             :   friend int ilogb(const DoubleAPFloat &Arg);
     663             :   friend DoubleAPFloat scalbn(DoubleAPFloat X, int Exp, roundingMode);
     664             :   friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode);
     665             :   friend hash_code hash_value(const DoubleAPFloat &Arg);
     666             : };
     667             : 
     668             : hash_code hash_value(const DoubleAPFloat &Arg);
     669             : 
     670             : } // End detail namespace
     671             : 
     672             : // This is a interface class that is currently forwarding functionalities from
     673             : // detail::IEEEFloat.
     674      439225 : class APFloat : public APFloatBase {
     675             :   typedef detail::IEEEFloat IEEEFloat;
     676             :   typedef detail::DoubleAPFloat DoubleAPFloat;
     677             : 
     678             :   static_assert(std::is_standard_layout<IEEEFloat>::value, "");
     679             : 
     680             :   union Storage {
     681             :     const fltSemantics *semantics;
     682             :     IEEEFloat IEEE;
     683             :     DoubleAPFloat Double;
     684             : 
     685             :     explicit Storage(IEEEFloat F, const fltSemantics &S);
     686             :     explicit Storage(DoubleAPFloat F, const fltSemantics &S)
     687           4 :         : Double(std::move(F)) {
     688             :       assert(&S == &PPCDoubleDouble());
     689             :     }
     690             : 
     691             :     template <typename... ArgTypes>
     692     1104810 :     Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
     693     1104810 :       if (usesLayout<IEEEFloat>(Semantics)) {
     694     1104436 :         new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
     695     1104436 :         return;
     696             :       }
     697             :       if (usesLayout<DoubleAPFloat>(Semantics)) {
     698         374 :         new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
     699         374 :         return;
     700             :       }
     701             :       llvm_unreachable("Unexpected semantics");
     702             :     }
     703      233333 : 
     704    11781555 :     ~Storage() {
     705    11781332 :       if (usesLayout<IEEEFloat>(*semantics)) {
     706     6007062 :         IEEE.~IEEEFloat();
     707     5773952 :         return;
     708             :       }
     709         223 :       if (usesLayout<DoubleAPFloat>(*semantics)) {
     710         223 :         Double.~DoubleAPFloat();
     711         159 :         return;
     712             :       }
     713             :       llvm_unreachable("Unexpected semantics");
     714     6026170 :     }
     715      252059 : 
     716      323557 :     Storage(const Storage &RHS) {
     717      395100 :       if (usesLayout<IEEEFloat>(*RHS.semantics)) {
     718       71462 :         new (this) IEEEFloat(RHS.IEEE);
     719       71462 :         return;
     720          45 :       }
     721          45 :       if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
     722          81 :         new (this) DoubleAPFloat(RHS.Double);
     723          81 :         return;
     724             :       }
     725      449214 :       llvm_unreachable("Unexpected semantics");
     726     1076546 :     }
     727     1076546 : 
     728      763030 :     Storage(Storage &&RHS) {
     729      313990 :       if (usesLayout<IEEEFloat>(*RHS.semantics)) {
     730         138 :         new (this) IEEEFloat(std::move(RHS.IEEE));
     731         138 :         return;
     732           0 :       }
     733          24 :       if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
     734          36 :         new (this) DoubleAPFloat(std::move(RHS.Double));
     735          36 :         return;
     736      402533 :       }
     737       88867 :       llvm_unreachable("Unexpected semantics");
     738      334632 :     }
     739      580426 : 
     740      328927 :     Storage &operator=(const Storage &RHS) {
     741      495200 :       if (usesLayout<IEEEFloat>(*semantics) &&
     742       83128 :           usesLayout<IEEEFloat>(*RHS.semantics)) {
     743       83060 :         IEEE = RHS.IEEE;
     744         184 :       } else if (usesLayout<DoubleAPFloat>(*semantics) &&
     745         116 :                  usesLayout<DoubleAPFloat>(*RHS.semantics)) {
     746           0 :         Double = RHS.Double;
     747         143 :       } else if (this != &RHS) {
     748     2042335 :         this->~Storage();
     749     2647594 :         new (this) Storage(RHS);
     750     2231314 :       }
     751     1709208 :       return *this;
     752      605238 :     }
     753         209 : 
     754       88073 :     Storage &operator=(Storage &&RHS) {
     755      264919 :       if (usesLayout<IEEEFloat>(*semantics) &&
     756       88264 :           usesLayout<IEEEFloat>(*RHS.semantics)) {
     757       87998 :         IEEE = std::move(RHS.IEEE);
     758     1021171 :       } else if (usesLayout<DoubleAPFloat>(*semantics) &&
     759          39 :                  usesLayout<DoubleAPFloat>(*RHS.semantics)) {
     760      202328 :         Double = std::move(RHS.Double);
     761      404663 :       } else if (this != &RHS) {
     762      202379 :         this->~Storage();
     763      202323 :         new (this) Storage(std::move(RHS));
     764           0 :       }
     765       88073 :       return *this;
     766          16 :     }
     767          44 :   } U;
     768          28 : 
     769           0 :   template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
     770           0 :     static_assert(std::is_same<T, IEEEFloat>::value ||
     771           0 :                   std::is_same<T, DoubleAPFloat>::value, "");
     772      168246 :     if (std::is_same<T, DoubleAPFloat>::value) {
     773      367959 :       return &Semantics == &PPCDoubleDouble();
     774      262609 :     }
     775     6318427 :     return &Semantics != &PPCDoubleDouble();
     776       31392 :   }
     777          99 : 
     778          44 :   IEEEFloat &getIEEE() {
     779          44 :     if (usesLayout<IEEEFloat>(*U.semantics))
     780          81 :       return U.IEEE;
     781          81 :     if (usesLayout<DoubleAPFloat>(*U.semantics))
     782          81 :       return U.Double.getFirst().U.IEEE;
     783           0 :     llvm_unreachable("Unexpected semantics");
     784       47237 :   }
     785       47308 : 
     786       15768 :   const IEEEFloat &getIEEE() const {
     787      287404 :     if (usesLayout<IEEEFloat>(*U.semantics))
     788      688910 :       return U.IEEE;
     789      191450 :     if (usesLayout<DoubleAPFloat>(*U.semantics))
     790      191563 :       return U.Double.getFirst().U.IEEE;
     791          28 :     llvm_unreachable("Unexpected semantics");
     792           0 :   }
     793           0 : 
     794          64 :   void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
     795       15798 : 
     796          31 :   void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
     797      758269 : 
     798      391418 :   void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
     799      599710 :     APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
     800      199865 :   }
     801      199839 : 
     802         264 :   void makeLargest(bool Neg) {
     803         238 :     APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
     804           7 :   }
     805          29 : 
     806          40 :   void makeSmallest(bool Neg) {
     807          40 :     APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
     808     1340476 :   }
     809      315955 : 
     810      109423 :   void makeSmallestNormalized(bool Neg) {
     811          90 :     APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
     812          54 :   }
     813             : 
     814             :   // FIXME: This is due to clang 3.3 (or older version) always checks for the
     815             :   // default constructor in an array aggregate initialization, even if no
     816       16253 :   // elements in the array is default initialized.
     817             :   APFloat() : U(IEEEdouble()) {
     818           4 :     llvm_unreachable("This is a workaround for old clang.");
     819     7382504 :   }
     820       50320 : 
     821       50704 :   explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
     822           0 :   explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
     823          52 :       : U(std::move(F), S) {}
     824          10 : 
     825          10 :   cmpResult compareAbsoluteValue(const APFloat &RHS) const {
     826           4 :     assert(&getSemantics() == &RHS.getSemantics() &&
     827       15026 :            "Should only compare APFloats with the same semantics");
     828           4 :     if (usesLayout<IEEEFloat>(getSemantics()))
     829         257 :       return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
     830             :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     831        5411 :       return U.Double.compareAbsoluteValue(RHS.U.Double);
     832        4958 :     llvm_unreachable("Unexpected semantics");
     833          13 :   }
     834          34 : 
     835           8 : public:
     836        7388 :   APFloat(const fltSemantics &Semantics) : U(Semantics) {}
     837             :   APFloat(const fltSemantics &Semantics, StringRef S);
     838       16217 :   APFloat(const fltSemantics &Semantics, integerPart I) : U(Semantics, I) {}
     839             :   // TODO: Remove this constructor. This isn't faster than the first one.
     840       17692 :   APFloat(const fltSemantics &Semantics, uninitializedTag)
     841         249 :       : U(Semantics, uninitialized) {}
     842       79011 :   APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {}
     843     4214087 :   explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
     844       20882 :   explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
     845       71486 :   APFloat(const APFloat &RHS) = default;
     846         163 :   APFloat(APFloat &&RHS) = default;
     847          32 : 
     848      930071 :   ~APFloat() = default;
     849             : 
     850         717 :   bool needsCleanup() const { APFLOAT_DISPATCH_ON_SEMANTICS(needsCleanup()); }
     851          35 : 
     852             :   /// Factory for Positive and Negative Zero.
     853             :   ///
     854          23 :   /// \param Negative True iff the number should be negative.
     855          23 :   static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
     856             :     APFloat Val(Sem, uninitialized);
     857          36 :     Val.makeZero(Negative);
     858      166156 :     return Val;
     859             :   }
     860          54 : 
     861             :   /// Factory for Positive and Negative Infinity.
     862             :   ///
     863       18828 :   /// \param Negative True iff the number should be negative.
     864        7044 :   static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
     865       18752 :     APFloat Val(Sem, uninitialized);
     866        4794 :     Val.makeInf(Negative);
     867      250686 :     return Val;
     868         209 :   }
     869           6 : 
     870      417397 :   /// Factory for NaN values.
     871      229723 :   ///
     872           6 :   /// \param Negative - True iff the NaN generated should be negative.
     873           6 :   /// \param type - The unspecified fill bits for creating the NaN, 0 by
     874       15303 :   /// default.  The value is truncated as necessary.
     875       76444 :   static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
     876        1484 :                         unsigned type = 0) {
     877         135 :     if (type) {
     878      605178 :       APInt fill(64, type);
     879       16253 :       return getQNaN(Sem, Negative, &fill);
     880       85237 :     } else {
     881      362744 :       return getQNaN(Sem, Negative, nullptr);
     882          63 :     }
     883             :   }
     884          56 : 
     885       50259 :   /// Factory for QNaN values.
     886      139668 :   static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
     887      616829 :                          const APInt *payload = nullptr) {
     888          21 :     APFloat Val(Sem, uninitialized);
     889      170206 :     Val.makeNaN(false, Negative, payload);
     890      183018 :     return Val;
     891             :   }
     892      604060 : 
     893             :   /// Factory for SNaN values.
     894             :   static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
     895             :                          const APInt *payload = nullptr) {
     896             :     APFloat Val(Sem, uninitialized);
     897        1274 :     Val.makeNaN(true, Negative, payload);
     898             :     return Val;
     899        1527 :   }
     900           2 : 
     901         327 :   /// Returns the largest finite number in the given semantics.
     902             :   ///
     903        1272 :   /// \param Negative - True iff the number should be negative
     904             :   static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
     905             :     APFloat Val(Sem, uninitialized);
     906          14 :     Val.makeLargest(Negative);
     907             :     return Val;
     908         543 :   }
     909             : 
     910       18235 :   /// Returns the smallest (by magnitude) finite number in the given semantics.
     911        1274 :   /// Might be denormalized, which implies a relative loss of precision.
     912           0 :   ///
     913             :   /// \param Negative - True iff the number should be negative
     914         543 :   static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
     915             :     APFloat Val(Sem, uninitialized);
     916          11 :     Val.makeSmallest(Negative);
     917             :     return Val;
     918             :   }
     919          27 : 
     920             :   /// Returns the smallest (by magnitude) normalized finite number in the given
     921          27 :   /// semantics.
     922         543 :   ///
     923           0 :   /// \param Negative - True iff the number should be negative
     924             :   static APFloat getSmallestNormalized(const fltSemantics &Sem,
     925          27 :                                        bool Negative = false) {
     926             :     APFloat Val(Sem, uninitialized);
     927          90 :     Val.makeSmallestNormalized(Negative);
     928          10 :     return Val;
     929             :   }
     930             : 
     931             :   /// Returns a float which is bitcasted from an all one value int.
     932             :   ///
     933       16574 :   /// \param BitWidth - Select float type
     934             :   /// \param isIEEE   - If 128 bit number, select between PPC and IEEE
     935             :   static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
     936             : 
     937             :   /// Used to insert APFloat objects, or objects that contain APFloat objects,
     938           4 :   /// into FoldingSets.
     939           8 :   void Profile(FoldingSetNodeID &NID) const;
     940             : 
     941       15986 :   opStatus add(const APFloat &RHS, roundingMode RM) {
     942             :     assert(&getSemantics() == &RHS.getSemantics() &&
     943             :            "Should only call on two APFloats with the same semantics");
     944         432 :     if (usesLayout<IEEEFloat>(getSemantics()))
     945         432 :       return U.IEEE.add(RHS.U.IEEE, RM);
     946             :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     947           0 :       return U.Double.add(RHS.U.Double, RM);
     948             :     llvm_unreachable("Unexpected semantics");
     949          13 :   }
     950         778 :   opStatus subtract(const APFloat &RHS, roundingMode RM) {
     951             :     assert(&getSemantics() == &RHS.getSemantics() &&
     952             :            "Should only call on two APFloats with the same semantics");
     953         746 :     if (usesLayout<IEEEFloat>(getSemantics()))
     954         741 :       return U.IEEE.subtract(RHS.U.IEEE, RM);
     955             :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     956           5 :       return U.Double.subtract(RHS.U.Double, RM);
     957             :     llvm_unreachable("Unexpected semantics");
     958             :   }
     959         284 :   opStatus multiply(const APFloat &RHS, roundingMode RM) {
     960          30 :     assert(&getSemantics() == &RHS.getSemantics() &&
     961             :            "Should only call on two APFloats with the same semantics");
     962         284 :     if (usesLayout<IEEEFloat>(getSemantics()))
     963         788 :       return U.IEEE.multiply(RHS.U.IEEE, RM);
     964             :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     965           3 :       return U.Double.multiply(RHS.U.Double, RM);
     966         507 :     llvm_unreachable("Unexpected semantics");
     967         507 :   }
     968         269 :   opStatus divide(const APFloat &RHS, roundingMode RM) {
     969           0 :     assert(&getSemantics() == &RHS.getSemantics() &&
     970             :            "Should only call on two APFloats with the same semantics");
     971         292 :     if (usesLayout<IEEEFloat>(getSemantics()))
     972         344 :       return U.IEEE.divide(RHS.U.IEEE, RM);
     973             :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     974           8 :       return U.Double.divide(RHS.U.Double, RM);
     975          75 :     llvm_unreachable("Unexpected semantics");
     976          75 :   }
     977           8 :   opStatus remainder(const APFloat &RHS) {
     978           8 :     assert(&getSemantics() == &RHS.getSemantics() &&
     979             :            "Should only call on two APFloats with the same semantics");
     980           0 :     if (usesLayout<IEEEFloat>(getSemantics()))
     981         590 :       return U.IEEE.remainder(RHS.U.IEEE);
     982             :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     983           8 :       return U.Double.remainder(RHS.U.Double);
     984         590 :     llvm_unreachable("Unexpected semantics");
     985        2354 :   }
     986          69 :   opStatus mod(const APFloat &RHS) {
     987           8 :     assert(&getSemantics() == &RHS.getSemantics() &&
     988        1764 :            "Should only call on two APFloats with the same semantics");
     989        1802 :     if (usesLayout<IEEEFloat>(getSemantics()))
     990         169 :       return U.IEEE.mod(RHS.U.IEEE);
     991          23 :     if (usesLayout<DoubleAPFloat>(getSemantics()))
     992           0 :       return U.Double.mod(RHS.U.Double);
     993         108 :     llvm_unreachable("Unexpected semantics");
     994         660 :   }
     995          22 :   opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
     996           0 :                             roundingMode RM) {
     997         552 :     assert(&getSemantics() == &Multiplicand.getSemantics() &&
     998         547 :            "Should only call on APFloats with the same semantics");
     999             :     assert(&getSemantics() == &Addend.getSemantics() &&
    1000           5 :            "Should only call on APFloats with the same semantics");
    1001          22 :     if (usesLayout<IEEEFloat>(getSemantics()))
    1002          22 :       return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
    1003        1106 :     if (usesLayout<DoubleAPFloat>(getSemantics()))
    1004           0 :       return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
    1005           0 :                                        RM);
    1006        1106 :     llvm_unreachable("Unexpected semantics");
    1007        1069 :   }
    1008         115 :   opStatus roundToIntegral(roundingMode RM) {
    1009         117 :     APFLOAT_DISPATCH_ON_SEMANTICS(roundToIntegral(RM));
    1010             :   }
    1011          35 : 
    1012        1831 :   // TODO: bool parameters are not readable and a source of bugs.
    1013             :   // Do something.
    1014           3 :   opStatus next(bool nextDown) {
    1015        1799 :     APFLOAT_DISPATCH_ON_SEMANTICS(next(nextDown));
    1016        1794 :   }
    1017           8 : 
    1018           2 :   /// Add two APFloats, rounding ties to the nearest even.
    1019             :   /// No error checking.
    1020             :   APFloat operator+(const APFloat &RHS) const {
    1021           4 :     APFloat Result(*this);
    1022             :     (void)Result.add(RHS, rmNearestTiesToEven);
    1023           8 :     return Result;
    1024          12 :   }
    1025           2 : 
    1026           0 :   /// Subtract two APFloats, rounding ties to the nearest even.
    1027           2 :   /// No error checking.
    1028             :   APFloat operator-(const APFloat &RHS) const {
    1029             :     APFloat Result(*this);
    1030         186 :     (void)Result.subtract(RHS, rmNearestTiesToEven);
    1031         171 :     return Result;
    1032             :   }
    1033          15 : 
    1034          13 :   /// Multiply two APFloats, rounding ties to the nearest even.
    1035             :   /// No error checking.
    1036           2 :   APFloat operator*(const APFloat &RHS) const {
    1037             :     APFloat Result(*this);
    1038             :     (void)Result.multiply(RHS, rmNearestTiesToEven);
    1039          28 :     return Result;
    1040             :   }
    1041             : 
    1042             :   /// Divide the first APFloat by the second, rounding ties to the nearest even.
    1043             :   /// No error checking.
    1044             :   APFloat operator/(const APFloat &RHS) const {
    1045          28 :     APFloat Result(*this);
    1046         111 :     (void)Result.divide(RHS, rmNearestTiesToEven);
    1047             :     return Result;
    1048           1 :   }
    1049           1 : 
    1050        6081 :   void changeSign() { APFLOAT_DISPATCH_ON_SEMANTICS(changeSign()); }
    1051          27 :   void clearSign() {
    1052          48 :     if (isNegative())
    1053          30 :       changeSign();
    1054          27 :   }
    1055           6 :   void copySign(const APFloat &RHS) {
    1056           6 :     if (isNegative() != RHS.isNegative())
    1057           4 :       changeSign();
    1058          50 :   }
    1059          44 : 
    1060             :   /// A static helper to produce a copy of an APFloat value with its sign
    1061             :   /// copied from some other APFloat.
    1062             :   static APFloat copySign(APFloat Value, const APFloat &Sign) {
    1063             :     Value.copySign(Sign);
    1064             :     return Value;
    1065             :   }
    1066          93 : 
    1067             :   opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
    1068             :                    bool *losesInfo);
    1069         176 :   opStatus convertToInteger(MutableArrayRef<integerPart> Input,
    1070             :                             unsigned int Width, bool IsSigned, roundingMode RM,
    1071             :                             bool *IsExact) const {
    1072        2230 :     APFLOAT_DISPATCH_ON_SEMANTICS(
    1073         127 :         convertToInteger(Input, Width, IsSigned, RM, IsExact));
    1074         197 :   }
    1075          10 :   opStatus convertToInteger(APSInt &Result, roundingMode RM,
    1076         127 :                             bool *IsExact) const;
    1077       16315 :   opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
    1078           4 :                             roundingMode RM) {
    1079       16315 :     APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input, IsSigned, RM));
    1080           4 :   }
    1081             :   opStatus convertFromSignExtendedInteger(const integerPart *Input,
    1082         500 :                                           unsigned int InputSize, bool IsSigned,
    1083             :                                           roundingMode RM) {
    1084             :     APFLOAT_DISPATCH_ON_SEMANTICS(
    1085             :         convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM));
    1086             :   }
    1087             :   opStatus convertFromZeroExtendedInteger(const integerPart *Input,
    1088             :                                           unsigned int InputSize, bool IsSigned,
    1089             :                                           roundingMode RM) {
    1090          74 :     APFLOAT_DISPATCH_ON_SEMANTICS(
    1091           0 :         convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
    1092             :   }
    1093             :   opStatus convertFromString(StringRef, roundingMode);
    1094      285115 :   APInt bitcastToAPInt() const {
    1095      247009 :     APFLOAT_DISPATCH_ON_SEMANTICS(bitcastToAPInt());
    1096          56 :   }
    1097       48822 :   double convertToDouble() const { return getIEEE().convertToDouble(); }
    1098       22568 :   float convertToFloat() const { return getIEEE().convertToFloat(); }
    1099         650 : 
    1100          61 :   bool operator==(const APFloat &) const = delete;
    1101         597 : 
    1102        1046 :   cmpResult compare(const APFloat &RHS) const {
    1103             :     assert(&getSemantics() == &RHS.getSemantics() &&
    1104             :            "Should only compare APFloats with the same semantics");
    1105         985 :     if (usesLayout<IEEEFloat>(getSemantics()))
    1106         983 :       return U.IEEE.compare(RHS.U.IEEE);
    1107          56 :     if (usesLayout<DoubleAPFloat>(getSemantics()))
    1108           2 :       return U.Double.compare(RHS.U.Double);
    1109             :     llvm_unreachable("Unexpected semantics");
    1110          16 :   }
    1111             : 
    1112      896812 :   bool bitwiseIsEqual(const APFloat &RHS) const {
    1113      907719 :     if (&getSemantics() != &RHS.getSemantics())
    1114             :       return false;
    1115      847994 :     if (usesLayout<IEEEFloat>(getSemantics()))
    1116      976751 :       return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
    1117      117834 :     if (usesLayout<DoubleAPFloat>(getSemantics()))
    1118           0 :       return U.Double.bitwiseIsEqual(RHS.U.Double);
    1119         100 :     llvm_unreachable("Unexpected semantics");
    1120          74 :   }
    1121       29151 : 
    1122             :   /// We don't rely on operator== working on double values, as
    1123       29151 :   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
    1124         377 :   /// As such, this method can be used to do an exact bit-for-bit comparison of
    1125           0 :   /// two floating point values.
    1126             :   ///
    1127         377 :   /// We leave the version with the double argument here because it's just so
    1128         377 :   /// convenient to write "2.0" and the like.  Without this function we'd
    1129             :   /// have to duplicate its logic everywhere it's called.
    1130        1913 :   bool isExactlyValue(double V) const {
    1131           0 :     bool ignored;
    1132        1913 :     APFloat Tmp(V);
    1133        1913 :     Tmp.convert(getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
    1134       59718 :     return bitwiseIsEqual(Tmp);
    1135       57805 :   }
    1136             : 
    1137       29559 :   unsigned int convertToHexString(char *DST, unsigned int HexDigits,
    1138       97611 :                                   bool UpperCase, roundingMode RM) const {
    1139       68475 :     APFLOAT_DISPATCH_ON_SEMANTICS(
    1140           7 :         convertToHexString(DST, HexDigits, UpperCase, RM));
    1141         534 :   }
    1142          14 : 
    1143        3692 :   bool isZero() const { return getCategory() == fcZero; }
    1144           0 :   bool isInfinity() const { return getCategory() == fcInfinity; }
    1145      385309 :   bool isNaN() const { return getCategory() == fcNaN; }
    1146      365666 : 
    1147             :   bool isNegative() const { return getIEEE().isNegative(); }
    1148      253252 :   bool isDenormal() const { APFLOAT_DISPATCH_ON_SEMANTICS(isDenormal()); }
    1149      269641 :   bool isSignaling() const { return getIEEE().isSignaling(); }
    1150        1922 : 
    1151          65 :   bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
    1152       16604 :   bool isFinite() const { return !isNaN() && !isInfinity(); }
    1153             : 
    1154       16091 :   fltCategory getCategory() const { return getIEEE().getCategory(); }
    1155       16091 :   const fltSemantics &getSemantics() const { return *U.semantics; }
    1156       17070 :   bool isNonZero() const { return !isZero(); }
    1157        1772 :   bool isFiniteNonZero() const { return isFinite() && !isZero(); }
    1158       13390 :   bool isPosZero() const { return isZero() && !isNegative(); }
    1159        4615 :   bool isNegZero() const { return isZero() && isNegative(); }
    1160         971 :   bool isSmallest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isSmallest()); }
    1161             :   bool isLargest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isLargest()); }
    1162         211 :   bool isInteger() const { APFLOAT_DISPATCH_ON_SEMANTICS(isInteger()); }
    1163             : 
    1164             :   APFloat &operator=(const APFloat &RHS) = default;
    1165          22 :   APFloat &operator=(APFloat &&RHS) = default;
    1166             : 
    1167       35239 :   void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
    1168             :                 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
    1169       35239 :     APFLOAT_DISPATCH_ON_SEMANTICS(
    1170           4 :         toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
    1171           0 :   }
    1172             : 
    1173           4 :   void print(raw_ostream &) const;
    1174           2 :   void dump() const;
    1175             : 
    1176         473 :   bool getExactInverse(APFloat *inv) const {
    1177           0 :     APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv));
    1178          92 :   }
    1179           4 : 
    1180        6764 :   friend hash_code hash_value(const APFloat &Arg);
    1181       23910 :   friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); }
    1182             :   friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
    1183           0 :   friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
    1184          84 :   friend IEEEFloat;
    1185         439 :   friend DoubleAPFloat;
    1186             : };
    1187          12 : 
    1188          11 : /// See friend declarations above.
    1189          34 : ///
    1190         875 : /// These additional declarations are required in order to compile LLVM with IBM
    1191          11 : /// xlC compiler.
    1192         189 : hash_code hash_value(const APFloat &Arg);
    1193          26 : inline APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM) {
    1194             :   if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
    1195          46 :     return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
    1196         179 :   if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
    1197             :     return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
    1198             :   llvm_unreachable("Unexpected semantics");
    1199           0 : }
    1200             : 
    1201         127 : /// Equivalent of C standard library function.
    1202          54 : ///
    1203          98 : /// While the C standard says Exp is an unspecified value for infinity and nan,
    1204           1 : /// this returns INT_MAX for infinities, and INT_MIN for NaNs.
    1205          39 : inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
    1206          49 :   if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
    1207          38 :     return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
    1208             :   if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
    1209         117 :     return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
    1210         117 :   llvm_unreachable("Unexpected semantics");
    1211          45 : }
    1212             : /// Returns the absolute value of the argument.
    1213          45 : inline APFloat abs(APFloat X) {
    1214          24 :   X.clearSign();
    1215             :   return X;
    1216             : }
    1217             : 
    1218             : /// Returns the negated value of the argument.
    1219             : inline APFloat neg(APFloat X) {
    1220          10 :   X.changeSign();
    1221          10 :   return X;
    1222             : }
    1223             : 
    1224             : /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
    1225          88 : /// both are not NaN. If either argument is a NaN, returns the other argument.
    1226             : LLVM_READONLY
    1227          31 : inline APFloat minnum(const APFloat &A, const APFloat &B) {
    1228          31 :   if (A.isNaN())
    1229             :     return B;
    1230          26 :   if (B.isNaN())
    1231             :     return A;
    1232          24 :   return (B.compare(A) == APFloat::cmpLessThan) ? B : A;
    1233             : }
    1234             : 
    1235             : /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
    1236             : /// both are not NaN. If either argument is a NaN, returns the other argument.
    1237         144 : LLVM_READONLY
    1238         190 : inline APFloat maxnum(const APFloat &A, const APFloat &B) {
    1239         188 :   if (A.isNaN())
    1240             :     return B;
    1241          48 :   if (B.isNaN())
    1242         209 :     return A;
    1243          40 :   return (A.compare(B) == APFloat::cmpLessThan) ? B : A;
    1244             : }
    1245             : 
    1246             : /// Implements IEEE 754-2018 minimum semantics. Returns the smaller of 2
    1247             : /// arguments, propagating NaNs and treating -0 as less than +0.
    1248             : LLVM_READONLY
    1249          23 : inline APFloat minimum(const APFloat &A, const APFloat &B) {
    1250          23 :   if (A.isNaN())
    1251          21 :     return A;
    1252           0 :   if (B.isNaN())
    1253           4 :     return B;
    1254           0 :   if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
    1255             :     return A.isNegative() ? A : B;
    1256             :   return (B.compare(A) == APFloat::cmpLessThan) ? B : A;
    1257             : }
    1258          56 : 
    1259             : /// Implements IEEE 754-2018 maximum semantics. Returns the larger of 2
    1260           0 : /// arguments, propagating NaNs and treating -0 as less than +0.
    1261           0 : LLVM_READONLY
    1262             : inline APFloat maximum(const APFloat &A, const APFloat &B) {
    1263           0 :   if (A.isNaN())
    1264          10 :     return A;
    1265           0 :   if (B.isNaN())
    1266             :     return B;
    1267             :   if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
    1268             :     return A.isNegative() ? B : A;
    1269             :   return (A.compare(B) == APFloat::cmpLessThan) ? B : A;
    1270             : }
    1271           4 : 
    1272           4 : } // namespace llvm
    1273             : 
    1274           3 : #undef APFLOAT_DISPATCH_ON_SEMANTICS
    1275             : #endif // LLVM_ADT_APFLOAT_H

Generated by: LCOV version 1.13