-
- /// getLargest - Returns the largest finite number in the given
- /// semantics.
- ///
- /// \param Negative - True iff the number should be negative
- static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
-
- /// getSmallest - Returns the smallest (by magnitude) finite number
- /// in the given semantics. Might be denormalized, which implies a
- /// relative loss of precision.
- ///
- /// \param Negative - True iff the number should be negative
- static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
-
- /// getSmallestNormalized - Returns the smallest (by magnitude)
- /// normalized finite number in the given semantics.
- ///
- /// \param Negative - True iff the number should be negative
- static APFloat getSmallestNormalized(const fltSemantics &Sem,
- bool Negative = false);
-
- /// getAllOnesValue - Returns a float which is bitcasted from
- /// an all one value int.
- ///
- /// \param BitWidth - Select float type
- /// \param isIEEE - If 128 bit number, select between PPC and IEEE
- static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
-
- /// Profile - Used to insert APFloat objects, or objects that contain
- /// APFloat objects, into FoldingSets.
- void Profile(FoldingSetNodeID& NID) const;
-
- /// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
- void Emit(Serializer& S) const;
-
- /// @brief Used by the Bitcode deserializer to deserialize APInts.
- static APFloat ReadVal(Deserializer& D);
-
- /* Arithmetic. */
- opStatus add(const APFloat &, roundingMode);
- opStatus subtract(const APFloat &, roundingMode);
- opStatus multiply(const APFloat &, roundingMode);
- opStatus divide(const APFloat &, roundingMode);
- /* IEEE remainder. */
- opStatus remainder(const APFloat &);
- /* C fmod, or llvm frem. */
- opStatus mod(const APFloat &, roundingMode);
- opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
-
- /* Sign operations. */
- void changeSign();
- void clearSign();
- void copySign(const APFloat &);
-
- /* Conversions. */
- opStatus convert(const fltSemantics &, roundingMode, bool *);
- opStatus convertToInteger(integerPart *, unsigned int, bool,
- roundingMode, bool *) const;
- opStatus convertFromAPInt(const APInt &,
- bool, roundingMode);
- opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
- bool, roundingMode);
- opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
- bool, roundingMode);
- opStatus convertFromString(StringRef, roundingMode);
- APInt bitcastToAPInt() const;
- double convertToDouble() const;
- float convertToFloat() const;
-
- /* The definition of equality is not straightforward for floating point,
- so we won't use operator==. Use one of the following, or write
- whatever it is you really mean. */
- // bool operator==(const APFloat &) const; // DO NOT IMPLEMENT
-
- /* IEEE comparison with another floating point number (NaNs
- compare unordered, 0==-0). */
- cmpResult compare(const APFloat &) const;
-
- /* Bitwise comparison for equality (QNaNs compare equal, 0!=-0). */
- bool bitwiseIsEqual(const APFloat &) const;
-
- /* Write out a hexadecimal representation of the floating point
- value to DST, which must be of sufficient size, in the C99 form
- [-]0xh.hhhhp[+-]d. Return the number of characters written,
- excluding the terminating NUL. */
- unsigned int convertToHexString(char *dst, unsigned int hexDigits,
- bool upperCase, roundingMode) const;
-
- /* Simple queries. */
- fltCategory getCategory() const { return category; }
- const fltSemantics &getSemantics() const { return *semantics; }
- bool isZero() const { return category == fcZero; }
- bool isNonZero() const { return category != fcZero; }
- bool isNaN() const { return category == fcNaN; }
- bool isInfinity() const { return category == fcInfinity; }
- bool isNegative() const { return sign; }
- bool isPosZero() const { return isZero() && !isNegative(); }
- bool isNegZero() const { return isZero() && isNegative(); }
-
- APFloat& operator=(const APFloat &);
-
- /* Return an arbitrary integer value usable for hashing. */
- uint32_t getHashValue() const;
-
- /// Converts this value into a decimal string.
- ///
- /// \param FormatPrecision The maximum number of digits of
- /// precision to output. If there are fewer digits available,
- /// zero padding will not be used unless the value is
- /// integral and small enough to be expressed in
- /// FormatPrecision digits. 0 means to use the natural
- /// precision of the number.
- /// \param FormatMaxPadding The maximum number of zeros to
- /// consider inserting before falling back to scientific
- /// notation. 0 means to always use scientific notation.
- ///
- /// Number Precision MaxPadding Result
- /// ------ --------- ---------- ------
- /// 1.01E+4 5 2 10100
- /// 1.01E+4 4 2 1.01E+4
- /// 1.01E+4 5 1 1.01E+4
- /// 1.01E-2 5 2 0.0101
- /// 1.01E-2 4 2 0.0101
- /// 1.01E-2 4 1 1.01E-2
- void toString(SmallVectorImpl<char> &Str,
- unsigned FormatPrecision = 0,
- unsigned FormatMaxPadding = 3) const;
-
- private:
-
- /* Trivial queries. */
- integerPart *significandParts();
- const integerPart *significandParts() const;
- unsigned int partCount() const;
-
- /* Significand operations. */
- integerPart addSignificand(const APFloat &);
- integerPart subtractSignificand(const APFloat &, integerPart);
- lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
- lostFraction multiplySignificand(const APFloat &, const APFloat *);
- lostFraction divideSignificand(const APFloat &);
- void incrementSignificand();
- void initialize(const fltSemantics *);
- void shiftSignificandLeft(unsigned int);
- lostFraction shiftSignificandRight(unsigned int);
- unsigned int significandLSB() const;
- unsigned int significandMSB() const;
- void zeroSignificand();
-
- /* Arithmetic on special values. */
- opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
- opStatus divideSpecials(const APFloat &);
- opStatus multiplySpecials(const APFloat &);
- opStatus modSpecials(const APFloat &);
-
- /* Miscellany. */
- static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
- const APInt *fill);
- void makeNaN(bool SNaN = false, bool Neg = false, const APInt *fill = 0);
- opStatus normalize(roundingMode, lostFraction);
- opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
- cmpResult compareAbsoluteValue(const APFloat &) const;
- opStatus handleOverflow(roundingMode);
- bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
- opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
- roundingMode, bool *) const;
- opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
- roundingMode);
- opStatus convertFromHexadecimalString(StringRef, roundingMode);
- opStatus convertFromDecimalString(StringRef, roundingMode);
- char *convertNormalToHexString(char *, unsigned int, bool,
- roundingMode) const;
- opStatus roundSignificandWithExponent(const integerPart *, unsigned int,
- int, roundingMode);
-
- APInt convertHalfAPFloatToAPInt() const;
- APInt convertFloatAPFloatToAPInt() const;
- APInt convertDoubleAPFloatToAPInt() const;
- APInt convertQuadrupleAPFloatToAPInt() const;
- APInt convertF80LongDoubleAPFloatToAPInt() const;
- APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
- void initFromAPInt(const APInt& api, bool isIEEE = false);
- void initFromHalfAPInt(const APInt& api);
- void initFromFloatAPInt(const APInt& api);
- void initFromDoubleAPInt(const APInt& api);
- void initFromQuadrupleAPInt(const APInt &api);
- void initFromF80LongDoubleAPInt(const APInt& api);
- void initFromPPCDoubleDoubleAPInt(const APInt& api);
-
- void assign(const APFloat &);
- void copySignificand(const APFloat &);
- void freeSignificand();
-
- /* What kind of semantics does this value obey? */
- const fltSemantics *semantics;
-
- /* Significand - the fraction with an explicit integer bit. Must be
- at least one bit wider than the target precision. */
- union Significand
- {
- integerPart part;
- integerPart *parts;
- } significand;
-
- /* The exponent - a signed number. */
- exponent_t exponent;
-
- /* What kind of floating point number this is. */
- /* Only 2 bits are required, but VisualStudio incorrectly sign extends
- it. Using the extra bit keeps it from failing under VisualStudio */
- fltCategory category: 3;
-
- /* The sign bit of this number. */
- unsigned int sign: 1;
-
- /* For PPCDoubleDouble, we have a second exponent and sign (the second
- significand is appended to the first one, although it would be wrong to
- regard these as a single number for arithmetic purposes). These fields
- are not meaningful for any other type. */
- exponent_t exponent2 : 11;
- unsigned int sign2: 1;
+ }
+
+ /// Factory for QNaN values.
+ static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
+ const APInt *payload = nullptr) {
+ return makeNaN(Sem, false, Negative, payload);
+ }
+
+ /// Factory for SNaN values.
+ static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
+ const APInt *payload = nullptr) {
+ return makeNaN(Sem, true, Negative, payload);
+ }
+
+ /// Returns the largest finite number in the given semantics.
+ ///
+ /// \param Negative - True iff the number should be negative
+ static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
+
+ /// Returns the smallest (by magnitude) finite number in the given semantics.
+ /// Might be denormalized, which implies a relative loss of precision.
+ ///
+ /// \param Negative - True iff the number should be negative
+ static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
+
+ /// Returns the smallest (by magnitude) normalized finite number in the given
+ /// semantics.
+ ///
+ /// \param Negative - True iff the number should be negative
+ static APFloat getSmallestNormalized(const fltSemantics &Sem,
+ bool Negative = false);
+
+ /// Returns a float which is bitcasted from an all one value int.
+ ///
+ /// \param BitWidth - Select float type
+ /// \param isIEEE - If 128 bit number, select between PPC and IEEE
+ static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
+
+ /// Returns the size of the floating point number (in bits) in the given
+ /// semantics.
+ static unsigned getSizeInBits(const fltSemantics &Sem);
+
+ /// @}
+
+ /// Used to insert APFloat objects, or objects that contain APFloat objects,
+ /// into FoldingSets.
+ void Profile(FoldingSetNodeID &NID) const;
+
+ /// \name Arithmetic
+ /// @{
+
+ opStatus add(const APFloat &, roundingMode);
+ opStatus subtract(const APFloat &, roundingMode);
+ opStatus multiply(const APFloat &, roundingMode);
+ opStatus divide(const APFloat &, roundingMode);
+ /// IEEE remainder.
+ opStatus remainder(const APFloat &);
+ /// C fmod, or llvm frem.
+ opStatus mod(const APFloat &);
+ opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
+ opStatus roundToIntegral(roundingMode);
+ /// IEEE-754R 5.3.1: nextUp/nextDown.
+ opStatus next(bool nextDown);
+
+ /// \brief Operator+ overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator+(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.add(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// \brief Operator- overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator-(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.subtract(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// \brief Operator* overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator*(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.multiply(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// \brief Operator/ overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator/(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.divide(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// @}
+
+ /// \name Sign operations.
+ /// @{
+
+ void changeSign();
+ void clearSign();
+ void copySign(const APFloat &);
+
+ /// \brief A static helper to produce a copy of an APFloat value with its sign
+ /// copied from some other APFloat.
+ static APFloat copySign(APFloat Value, const APFloat &Sign) {
+ Value.copySign(Sign);
+ return Value;
+ }
+
+ /// @}
+
+ /// \name Conversions
+ /// @{
+
+ opStatus convert(const fltSemantics &, roundingMode, bool *);
+ opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode,
+ bool *) const;
+ opStatus convertToInteger(APSInt &, roundingMode, bool *) const;
+ opStatus convertFromAPInt(const APInt &, bool, roundingMode);
+ opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
+ bool, roundingMode);
+ opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
+ bool, roundingMode);
+ opStatus convertFromString(StringRef, roundingMode);
+ APInt bitcastToAPInt() const;
+ double convertToDouble() const;
+ float convertToFloat() const;
+
+ /// @}
+
+ /// The definition of equality is not straightforward for floating point, so
+ /// we won't use operator==. Use one of the following, or write whatever it
+ /// is you really mean.
+ bool operator==(const APFloat &) const = delete;
+
+ /// IEEE comparison with another floating point number (NaNs compare
+ /// unordered, 0==-0).
+ cmpResult compare(const APFloat &) const;
+
+ /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
+ bool bitwiseIsEqual(const APFloat &) const;
+
+ /// Write out a hexadecimal representation of the floating point value to DST,
+ /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
+ /// Return the number of characters written, excluding the terminating NUL.
+ unsigned int convertToHexString(char *dst, unsigned int hexDigits,
+ bool upperCase, roundingMode) const;
+
+ /// \name IEEE-754R 5.7.2 General operations.
+ /// @{
+
+ /// IEEE-754R isSignMinus: Returns true if and only if the current value is
+ /// negative.
+ ///
+ /// This applies to zeros and NaNs as well.
+ bool isNegative() const { return sign; }
+
+ /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
+ ///
+ /// This implies that the current value of the float is not zero, subnormal,
+ /// infinite, or NaN following the definition of normality from IEEE-754R.
+ bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
+
+ /// Returns true if and only if the current value is zero, subnormal, or
+ /// normal.
+ ///
+ /// This means that the value is not infinite or NaN.
+ bool isFinite() const { return !isNaN() && !isInfinity(); }
+
+ /// Returns true if and only if the float is plus or minus zero.
+ bool isZero() const { return category == fcZero; }
+
+ /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
+ /// denormal.
+ bool isDenormal() const;
+
+ /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
+ bool isInfinity() const { return category == fcInfinity; }
+
+ /// Returns true if and only if the float is a quiet or signaling NaN.
+ bool isNaN() const { return category == fcNaN; }
+
+ /// Returns true if and only if the float is a signaling NaN.
+ bool isSignaling() const;
+
+ /// @}
+
+ /// \name Simple Queries
+ /// @{
+
+ fltCategory getCategory() const { return category; }
+ const fltSemantics &getSemantics() const { return *semantics; }
+ bool isNonZero() const { return category != fcZero; }
+ bool isFiniteNonZero() const { return isFinite() && !isZero(); }
+ bool isPosZero() const { return isZero() && !isNegative(); }
+ bool isNegZero() const { return isZero() && isNegative(); }
+
+ /// Returns true if and only if the number has the smallest possible non-zero
+ /// magnitude in the current semantics.
+ bool isSmallest() const;
+
+ /// Returns true if and only if the number has the largest possible finite
+ /// magnitude in the current semantics.
+ bool isLargest() const;
+
+ /// Returns true if and only if the number is an exact integer.
+ bool isInteger() const;
+
+ /// @}
+
+ APFloat &operator=(const APFloat &);
+ APFloat &operator=(APFloat &&);
+
+ /// \brief Overload to compute a hash code for an APFloat value.
+ ///
+ /// Note that the use of hash codes for floating point values is in general
+ /// frought with peril. Equality is hard to define for these values. For
+ /// example, should negative and positive zero hash to different codes? Are
+ /// they equal or not? This hash value implementation specifically
+ /// emphasizes producing different codes for different inputs in order to
+ /// be used in canonicalization and memoization. As such, equality is
+ /// bitwiseIsEqual, and 0 != -0.
+ friend hash_code hash_value(const APFloat &Arg);
+
+ /// Converts this value into a decimal string.
+ ///
+ /// \param FormatPrecision The maximum number of digits of
+ /// precision to output. If there are fewer digits available,
+ /// zero padding will not be used unless the value is
+ /// integral and small enough to be expressed in
+ /// FormatPrecision digits. 0 means to use the natural
+ /// precision of the number.
+ /// \param FormatMaxPadding The maximum number of zeros to
+ /// consider inserting before falling back to scientific
+ /// notation. 0 means to always use scientific notation.
+ ///
+ /// Number Precision MaxPadding Result
+ /// ------ --------- ---------- ------
+ /// 1.01E+4 5 2 10100
+ /// 1.01E+4 4 2 1.01E+4
+ /// 1.01E+4 5 1 1.01E+4
+ /// 1.01E-2 5 2 0.0101
+ /// 1.01E-2 4 2 0.0101
+ /// 1.01E-2 4 1 1.01E-2
+ void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
+ unsigned FormatMaxPadding = 3) const;
+
+ /// If this value has an exact multiplicative inverse, store it in inv and
+ /// return true.
+ bool getExactInverse(APFloat *inv) const;
+
+ /// \brief Enumeration of \c ilogb error results.
+ enum IlogbErrorKinds {
+ IEK_Zero = INT_MIN+1,
+ IEK_NaN = INT_MIN,
+ IEK_Inf = INT_MAX