LLVM  4.0.0
APFloat.h
Go to the documentation of this file.
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"
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 {
158  };
159 
160  /// IEEE-754R 4.3: Rounding-direction attributes.
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 {
187  };
188 
189  /// Convenience enum used to construct an uninitialized APFloat.
192  };
193 
194  /// \brief Enumeration of \c ilogb error results.
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 &);
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
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 
249  /// IEEE remainder.
250  opStatus remainder(const IEEEFloat &);
251  /// C fmod, or llvm frem.
252  opStatus mod(const IEEEFloat &);
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;
317  bool, roundingMode);
319  bool, 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 &);
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 
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);
615  DoubleAPFloat(const fltSemantics &S, const APInt &I);
616  DoubleAPFloat(const fltSemantics &S, APFloat &&First, APFloat &&Second);
617  DoubleAPFloat(const DoubleAPFloat &RHS);
619 
621 
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);
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 {
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.
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
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 
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  }
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  }
940  return getIEEE().multiply(RHS.getIEEE(), RM);
941  }
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  }
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  }
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  }
1000  unsigned int InputSize, bool IsSigned,
1001  roundingMode RM) {
1002  return getIEEE().convertFromSignExtendedInteger(Input, InputSize, IsSigned,
1003  RM);
1004  }
1006  unsigned int InputSize, bool IsSigned,
1007  roundingMode RM) {
1008  return getIEEE().convertFromZeroExtendedInteger(Input, InputSize, IsSigned,
1009  RM);
1010  }
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 {
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;
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);
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.
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.
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.
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
friend int ilogb(const APFloat &Arg)
Definition: APFloat.h:1075
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:954
DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:3909
~APFloat()=default
fltCategory
Category of internally-represented number.
Definition: APFloat.h:182
const APFloat & getFirst() const
Definition: APFloat.h:633
void makeNaN(bool SNaN, bool Neg, const APInt *fill)
Definition: APFloat.cpp:4107
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:995
APFloat(const fltSemantics &Semantics)
Definition: APFloat.h:816
bool isNonZero() const
Definition: APFloat.h:1044
void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.cpp:3311
bool isNaN() const
Definition: APFloat.h:1033
float convertToFloat() const
Definition: APFloat.h:1014
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
Definition: APFloat.cpp:2253
APFloat(double d)
Definition: APFloat.h:823
opStatus roundToIntegral(roundingMode)
Definition: APFloat.cpp:1823
fltCategory getCategory() const
Definition: APFloat.h:387
bool isSmallest() const
Returns true if and only if the number has the smallest possible non-zero magnitude in the current se...
Definition: APFloat.cpp:749
bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition: APFloat.cpp:816
void makeInf(bool Neg=false)
Definition: APFloat.cpp:3781
bool isSignaling() const
Definition: APFloat.h:1037
static unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
Definition: APFloat.cpp:151
hash_code hash_value(const IEEEFloat &Arg)
Definition: APFloat.cpp:2800
static const fltSemantics & Bogus()
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real...
Definition: APFloat.cpp:112
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:841
friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
void changeSign()
Definition: APFloat.h:975
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
Definition: APFloat.h:364
bool isFiniteNonZero() const
Definition: APFloat.h:1045
opStatus next(bool nextDown)
Definition: APFloat.h:957
APFloat(const fltSemantics &Semantics, const APInt &I)
Definition: APFloat.h:822
bool isSmallest() const
Definition: APFloat.h:1048
bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
Definition: APFloat.cpp:801
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:999
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:942
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition: APFloat.h:900
const APFloat & getSecond() const
Definition: APFloat.h:635
opStatus convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:4128
static const fltSemantics & x87DoubleExtended()
Definition: APFloat.cpp:109
friend IEEEFloat
Definition: APFloat.h:1078
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition: APFloat.h:161
IEEEFloat(const fltSemantics &)
Definition: APFloat.cpp:843
static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE=false)
Returns a float which is bitcasted from an all one value int.
Definition: APFloat.cpp:4164
void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
Definition: APFloat.cpp:3275
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3) const
Definition: APFloat.h:1055
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
bool isFiniteNonZero() const
Definition: APFloat.h:390
APFloat & operator=(const APFloat &RHS)=default
cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:4076
APInt bitcastToAPInt() const
Definition: APFloat.h:1012
unsigned int convertToHexString(char *dst, unsigned int hexDigits, bool upperCase, roundingMode) const
Write out a hexadecimal representation of the floating point value to DST, which must be of sufficien...
Definition: APFloat.cpp:2651
opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:4062
static ExponentType semanticsMaxExponent(const fltSemantics &)
Definition: APFloat.cpp:140
bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
Definition: APFloat.cpp:3652
void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
Definition: APFloat.cpp:3300
APFloat operator+(const APFloat &RHS) const
Definition: APFloat.h:959
This file implements a class to represent arbitrary precision integral constant values and operations...
void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
Definition: APFloat.cpp:672
opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:4057
IEEEFloat & operator=(const IEEEFloat &)
Definition: APFloat.cpp:718
void Profile(FoldingSetNodeID &NID) const
Used to insert APFloat objects, or objects that contain APFloat objects, into FoldingSets.
Definition: APFloat.cpp:866
#define F(x, y, z)
Definition: MD5.cpp:51
bool isFinite() const
Definition: APFloat.h:1040
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4139
static unsigned int semanticsSizeInBits(const fltSemantics &)
Definition: APFloat.cpp:147
cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition: APFloat.cpp:1866
friend DoubleAPFloat
Definition: APFloat.h:1079
fltCategory getCategory() const
Definition: APFloat.cpp:4096
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition: APFloat.cpp:2235
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
Definition: APFloat.h:358
opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
Definition: APFloat.cpp:1740
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition: APFloat.h:367
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4132
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
Definition: APFloat.cpp:3665
IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM)
Definition: APFloat.cpp:3839
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:932
opStatus convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:2600
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:153
DoubleAPFloat(const fltSemantics &S)
Definition: APFloat.cpp:3858
const APFloat & getSecondFloat() const
Definition: APFloat.h:1069
bool isInteger() const
Returns true if and only if the number is an exact integer.
Definition: APFloat.cpp:808
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
Definition: APFloat.cpp:2279
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:850
static ExponentType semanticsMinExponent(const fltSemantics &)
Definition: APFloat.cpp:144
const fltSemantics & getSemantics() const
Definition: APFloat.h:388
opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, bool *) const
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
Definition: APFloat.h:190
cmpResult compareAbsoluteValue(const IEEEFloat &) const
Definition: APFloat.cpp:1175
IlogbErrorKinds
Enumeration of ilogb error results.
Definition: APFloat.h:195
static APFloat copySign(APFloat Value, const APFloat &Sign)
Definition: APFloat.h:979
static const fltSemantics & IEEEsingle()
Definition: APFloat.cpp:100
bool isDenormal() const
Definition: APFloat.h:1036
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:316
friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4132
bool isNegZero() const
Definition: APFloat.h:1047
void clearSign()
Definition: APFloat.h:976
void Profile(FoldingSetNodeID &NID) const
Definition: APFloat.h:923
APFloat(float f)
Definition: APFloat.h:824
bool operator==(const APFloat &) const =delete
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1022
bool isInteger() const
Definition: APFloat.h:1050
IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode)
Definition: APFloat.cpp:3818
opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
Definition: APFloat.cpp:1943
static const fltSemantics & IEEEhalf()
Definition: APFloat.cpp:97
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Definition: APFloat.h:1087
opStatus remainder(const IEEEFloat &)
IEEE remainder.
Definition: APFloat.cpp:1703
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static IEEEFloat copySign(IEEEFloat Value, const IEEEFloat &Sign)
A static helper to produce a copy of an APFloat value with its sign copied from some other APFloat...
Definition: APFloat.h:301
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:939
APFloat operator/(const APFloat &RHS) const
Definition: APFloat.h:971
opStatus convertToInteger(integerPart *Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:986
fltCategory getCategory() const
Definition: APFloat.h:1042
friend int ilogb(const IEEEFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition: APFloat.cpp:3800
static const fltSemantics & IEEEquad()
Definition: APFloat.cpp:106
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void copySign(const APFloat &RHS)
Definition: APFloat.h:977
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Definition: APFloat.h:1118
bool needsCleanup() const
Definition: APFloat.h:830
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3) const
Converts this value into a decimal string.
Definition: APFloat.cpp:3421
signed short ExponentType
A signed type to represent a floating point numbers unbiased exponent.
Definition: APFloat.h:134
IEEEFloat operator*(const IEEEFloat &RHS) const
Operator* overload which provides the default nmNearestTiesToEven rounding mode and no error checking...
Definition: APFloat.h:276
void makeZero(bool Neg=false)
Definition: APFloat.cpp:3788
bool isPosZero() const
Definition: APFloat.h:1046
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
Definition: APFloat.h:374
DoubleAPFloat & operator=(DoubleAPFloat &&RHS)
Definition: APFloat.h:622
void dump() const
Definition: APFloat.cpp:4192
bool isLargest() const
Definition: APFloat.h:1049
IEEEFloat operator-(const IEEEFloat &RHS) const
Operator- overload which provides the default nmNearestTiesToEven rounding mode and no error checking...
Definition: APFloat.h:268
opStatus add(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:1655
void copySign(const IEEEFloat &)
Definition: APFloat.cpp:1619
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
Definition: APFloat.h:38
bool isNegative() const
Definition: APFloat.h:1035
opStatus subtract(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:1661
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1095
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:136
bool needsCleanup() const
Definition: APFloat.h:630
Class for arbitrary precision integers.
Definition: APInt.h:77
void print(raw_ostream &) const
Definition: APFloat.cpp:4186
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition: APFloat.h:880
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition: APFloat.h:1026
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:948
An opaque object representing a hash code.
Definition: Hashing.h:72
double convertToDouble() const
Definition: APFloat.h:1013
static const fltSemantics & IEEEdouble()
Definition: APFloat.cpp:103
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:925
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative. ...
Definition: APFloat.h:352
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, unsigned type=0)
Factory for NaN values.
Definition: APFloat.h:861
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:172
double convertToDouble() const
Definition: APFloat.cpp:3051
APFloat operator*(const APFloat &RHS) const
Definition: APFloat.h:967
bool needsCleanup() const
Returns whether this instance allocated memory.
Definition: APFloat.h:231
#define I(x, y, z)
Definition: MD5.cpp:54
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1099
opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:1780
static const fltSemantics & PPCDoubleDouble()
Definition: APFloat.cpp:115
IEEEFloat operator+(const IEEEFloat &RHS) const
Operator+ overload which provides the default nmNearestTiesToEven rounding mode and no error checking...
Definition: APFloat.h:260
#define LLVM_READONLY
Definition: Compiler.h:174
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:890
bool isNegZero() const
Definition: APFloat.h:392
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:910
float convertToFloat() const
Definition: APFloat.cpp:3044
opStatus divide(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:1685
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:949
APFloat operator-(const APFloat &RHS) const
Definition: APFloat.h:963
opStatus remainder(const APFloat &RHS)
Definition: APFloat.h:945
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:132
LLVM Value Representation.
Definition: Value.h:71
bool isZero() const
Definition: APFloat.h:1031
APInt bitcastToAPInt() const
Definition: APFloat.cpp:3023
opStatus multiply(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:1667
bool operator==(const IEEEFloat &) const =delete
The definition of equality is not straightforward for floating point, so we won't use operator==...
opStatus convertToInteger(APSInt &Result, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:991
bool isInfinity() const
Definition: APFloat.h:1032
bool getExactInverse(IEEEFloat *inv) const
If this value has an exact multiplicative inverse, store it in inv and return true.
Definition: APFloat.cpp:3623
bool isPosZero() const
Definition: APFloat.h:391
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Returns: X * 2^Exp for integral exponents.
cmpResult compare(const APFloat &RHS) const
Definition: APFloat.h:1018
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1005
bool getExactInverse(APFloat *inv) const
Definition: APFloat.h:1063
IEEEFloat operator/(const IEEEFloat &RHS) const
Operator/ overload which provides the default nmNearestTiesToEven rounding mode and no error checking...
Definition: APFloat.h:284
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
Definition: APFloat.h:377
bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
Definition: APFloat.cpp:743
friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
Definition: APFloat.cpp:2800
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
APFloat(const fltSemantics &Semantics, integerPart I)
Definition: APFloat.h:818
const fltSemantics & getSemantics() const
Definition: APFloat.h:1043
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:872
APFloat(const fltSemantics &Semantics, uninitializedTag)
Definition: APFloat.h:820
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
Definition: APFloat.h:1107
bool isNormal() const
Definition: APFloat.h:1039
int ilogb(const IEEEFloat &Arg)
Definition: APFloat.cpp:3800
bool isNonZero() const
Definition: APFloat.h:389
uint64_t integerPart
Definition: APInt.h:33