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