LLVM 20.0.0git
APFloat.h
Go to the documentation of this file.
1//===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file declares a class to represent arbitrary precision floating point
11/// values and provide a variety of arithmetic operations on them.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_APFLOAT_H
16#define LLVM_ADT_APFLOAT_H
17
18#include "llvm/ADT/APInt.h"
19#include "llvm/ADT/ArrayRef.h"
23#include <memory>
24
25#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
26 do { \
27 if (usesLayout<IEEEFloat>(getSemantics())) \
28 return U.IEEE.METHOD_CALL; \
29 if (usesLayout<DoubleAPFloat>(getSemantics())) \
30 return U.Double.METHOD_CALL; \
31 llvm_unreachable("Unexpected semantics"); \
32 } while (false)
33
34namespace llvm {
35
36struct fltSemantics;
37class APSInt;
38class StringRef;
39class APFloat;
40class raw_ostream;
41
42template <typename T> class Expected;
43template <typename T> class SmallVectorImpl;
44
45/// Enum that represents what fraction of the LSB truncated bits of an fp number
46/// represent.
47///
48/// This essentially combines the roles of guard and sticky bits.
49enum lostFraction { // Example of truncated bits:
50 lfExactlyZero, // 000000
51 lfLessThanHalf, // 0xxxxx x's not all zero
52 lfExactlyHalf, // 100000
53 lfMoreThanHalf // 1xxxxx x's not all zero
54};
55
56/// A self-contained host- and target-independent arbitrary-precision
57/// floating-point software implementation.
58///
59/// APFloat uses bignum integer arithmetic as provided by static functions in
60/// the APInt class. The library will work with bignum integers whose parts are
61/// any unsigned type at least 16 bits wide, but 64 bits is recommended.
62///
63/// Written for clarity rather than speed, in particular with a view to use in
64/// the front-end of a cross compiler so that target arithmetic can be correctly
65/// performed on the host. Performance should nonetheless be reasonable,
66/// particularly for its intended use. It may be useful as a base
67/// implementation for a run-time library during development of a faster
68/// target-specific one.
69///
70/// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
71/// implemented operations. Currently implemented operations are add, subtract,
72/// multiply, divide, fused-multiply-add, conversion-to-float,
73/// conversion-to-integer and conversion-from-integer. New rounding modes
74/// (e.g. away from zero) can be added with three or four lines of code.
75///
76/// Four formats are built-in: IEEE single precision, double precision,
77/// quadruple precision, and x87 80-bit extended double (when operating with
78/// full extended precision). Adding a new format that obeys IEEE semantics
79/// only requires adding two lines of code: a declaration and definition of the
80/// format.
81///
82/// All operations return the status of that operation as an exception bit-mask,
83/// so multiple operations can be done consecutively with their results or-ed
84/// together. The returned status can be useful for compiler diagnostics; e.g.,
85/// inexact, underflow and overflow can be easily diagnosed on constant folding,
86/// and compiler optimizers can determine what exceptions would be raised by
87/// folding operations and optimize, or perhaps not optimize, accordingly.
88///
89/// At present, underflow tininess is detected after rounding; it should be
90/// straight forward to add support for the before-rounding case too.
91///
92/// The library reads hexadecimal floating point numbers as per C99, and
93/// correctly rounds if necessary according to the specified rounding mode.
94/// Syntax is required to have been validated by the caller. It also converts
95/// floating point numbers to hexadecimal text as per the C99 %a and %A
96/// conversions. The output precision (or alternatively the natural minimal
97/// precision) can be specified; if the requested precision is less than the
98/// natural precision the output is correctly rounded for the specified rounding
99/// mode.
100///
101/// It also reads decimal floating point numbers and correctly rounds according
102/// to the specified rounding mode.
103///
104/// Conversion to decimal text is not currently implemented.
105///
106/// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
107/// signed exponent, and the significand as an array of integer parts. After
108/// normalization of a number of precision P the exponent is within the range of
109/// the format, and if the number is not denormal the P-th bit of the
110/// significand is set as an explicit integer bit. For denormals the most
111/// significant bit is shifted right so that the exponent is maintained at the
112/// format's minimum, so that the smallest denormal has just the least
113/// significant bit of the significand set. The sign of zeroes and infinities
114/// is significant; the exponent and significand of such numbers is not stored,
115/// but has a known implicit (deterministic) value: 0 for the significands, 0
116/// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
117/// significand are deterministic, although not really meaningful, and preserved
118/// in non-conversion operations. The exponent is implicitly all 1 bits.
119///
120/// APFloat does not provide any exception handling beyond default exception
121/// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
122/// by encoding Signaling NaNs with the first bit of its trailing significand as
123/// 0.
124///
125/// TODO
126/// ====
127///
128/// Some features that may or may not be worth adding:
129///
130/// Binary to decimal conversion (hard).
131///
132/// Optional ability to detect underflow tininess before rounding.
133///
134/// New formats: x87 in single and double precision mode (IEEE apart from
135/// extended exponent range) (hard).
136///
137/// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
138///
139
140// This is the common type definitions shared by APFloat and its internal
141// implementation classes. This struct should not define any non-static data
142// members.
145 static constexpr unsigned integerPartWidth = APInt::APINT_BITS_PER_WORD;
146
147 /// A signed type to represent a floating point numbers unbiased exponent.
148 typedef int32_t ExponentType;
149
150 /// \name Floating Point Semantics.
151 /// @{
158 // The IBM double-double semantics. Such a number consists of a pair of
159 // IEEE 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
160 // (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
161 // Therefore it has two 53-bit mantissa parts that aren't necessarily
162 // adjacent to each other, and two 11-bit exponents.
163 //
164 // Note: we need to make the value different from semBogus as otherwise
165 // an unsafe optimization may collapse both values to a single address,
166 // and we heavily rely on them having distinct addresses.
168 // These are legacy semantics for the fallback, inaccurate implementation
169 // of IBM double-double, if the accurate semPPCDoubleDouble doesn't handle
170 // the operation. It's equivalent to having an IEEE number with consecutive
171 // 106 bits of mantissa and 11 bits of exponent.
172 //
173 // It's not equivalent to IBM double-double. For example, a legit IBM
174 // double-double, 1 + epsilon:
175 //
176 // 1 + epsilon = 1 + (1 >> 1076)
177 //
178 // is not representable by a consecutive 106 bits of mantissa.
179 //
180 // Currently, these semantics are used in the following way:
181 //
182 // semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
183 // (64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
184 // semPPCDoubleDoubleLegacy -> IEEE operations
185 //
186 // We use bitcastToAPInt() to get the bit representation (in APInt) of the
187 // underlying IEEEdouble, then use the APInt constructor to construct the
188 // legacy IEEE float.
189 //
190 // TODO: Implement all operations in semPPCDoubleDouble, and delete these
191 // semantics.
193 // 8-bit floating point number following IEEE-754 conventions with bit
194 // layout S1E5M2 as described in https://arxiv.org/abs/2209.05433.
196 // 8-bit floating point number mostly following IEEE-754 conventions
197 // and bit layout S1E5M2 described in https://arxiv.org/abs/2206.02915,
198 // with expanded range and with no infinity or signed zero.
199 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
200 // This format's exponent bias is 16, instead of the 15 (2 ** (5 - 1) - 1)
201 // that IEEE precedent would imply.
203 // 8-bit floating point number following IEEE-754 conventions with bit
204 // layout S1E4M3.
206 // 8-bit floating point number mostly following IEEE-754 conventions with
207 // bit layout S1E4M3 as described in https://arxiv.org/abs/2209.05433.
208 // Unlike IEEE-754 types, there are no infinity values, and NaN is
209 // represented with the exponent and mantissa bits set to all 1s.
211 // 8-bit floating point number mostly following IEEE-754 conventions
212 // and bit layout S1E4M3 described in https://arxiv.org/abs/2206.02915,
213 // with expanded range and with no infinity or signed zero.
214 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
215 // This format's exponent bias is 8, instead of the 7 (2 ** (4 - 1) - 1)
216 // that IEEE precedent would imply.
218 // 8-bit floating point number mostly following IEEE-754 conventions
219 // and bit layout S1E4M3 with expanded range and with no infinity or signed
220 // zero.
221 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
222 // This format's exponent bias is 11, instead of the 7 (2 ** (4 - 1) - 1)
223 // that IEEE precedent would imply.
225 // 8-bit floating point number following IEEE-754 conventions with bit
226 // layout S1E3M4.
228 // Floating point number that occupies 32 bits or less of storage, providing
229 // improved range compared to half (16-bit) formats, at (potentially)
230 // greater throughput than single precision (32-bit) formats.
232 // 8-bit floating point number with (all the) 8 bits for the exponent
233 // like in FP32. There are no zeroes, no infinities, and no denormal values.
234 // This format has unsigned representation only. (U -> Unsigned only).
235 // NaN is represented with all bits set to 1. Bias is 127.
236 // This format represents the scale data type in the MX specification from:
237 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
239 // 6-bit floating point number with bit layout S1E3M2. Unlike IEEE-754
240 // types, there are no infinity or NaN values. The format is detailed in
241 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
243 // 6-bit floating point number with bit layout S1E2M3. Unlike IEEE-754
244 // types, there are no infinity or NaN values. The format is detailed in
245 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
247 // 4-bit floating point number with bit layout S1E2M1. Unlike IEEE-754
248 // types, there are no infinity or NaN values. The format is detailed in
249 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
251 // TODO: Documentation is missing.
254 };
255
258
259 static const fltSemantics &IEEEhalf() LLVM_READNONE;
279
280 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
281 /// anything real.
283
284 // Returns true if any number described by this semantics can be precisely
285 // represented by the specified semantics. Does not take into account
286 // the value of fltNonfiniteBehavior, hasZero, hasSignedRepr.
288
289 /// @}
290
291 /// IEEE-754R 5.11: Floating Point Comparison Relations.
297 };
298
299 /// IEEE-754R 4.3: Rounding-direction attributes.
301
309
310 /// IEEE-754R 7: Default exception handling.
311 ///
312 /// opUnderflow or opOverflow are always returned or-ed with opInexact.
313 ///
314 /// APFloat models this behavior specified by IEEE-754:
315 /// "For operations producing results in floating-point format, the default
316 /// result of an operation that signals the invalid operation exception
317 /// shall be a quiet NaN."
318 enum opStatus {
319 opOK = 0x00,
324 opInexact = 0x10
325 };
326
327 /// Category of internally-represented number.
332 fcZero
333 };
334
335 /// Convenience enum used to construct an uninitialized APFloat.
338 };
339
340 /// Enumeration of \c ilogb error results.
342 IEK_Zero = INT_MIN + 1,
343 IEK_NaN = INT_MIN,
344 IEK_Inf = INT_MAX
345 };
346
347 static unsigned int semanticsPrecision(const fltSemantics &);
350 static unsigned int semanticsSizeInBits(const fltSemantics &);
351 static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
352 static bool semanticsHasZero(const fltSemantics &);
353 static bool semanticsHasSignedRepr(const fltSemantics &);
354 static bool semanticsHasInf(const fltSemantics &);
355 static bool semanticsHasNaN(const fltSemantics &);
356
357 // Returns true if any number described by \p Src can be precisely represented
358 // by a normal (not subnormal) value in \p Dst.
359 static bool isRepresentableAsNormalIn(const fltSemantics &Src,
360 const fltSemantics &Dst);
361
362 /// Returns the size of the floating point number (in bits) in the given
363 /// semantics.
364 static unsigned getSizeInBits(const fltSemantics &Sem);
365};
366
367namespace detail {
368
389static constexpr opStatus opOK = APFloatBase::opOK;
399
400class IEEEFloat final {
401public:
402 /// \name Constructors
403 /// @{
404
405 IEEEFloat(const fltSemantics &); // Default construct to +0.0
408 IEEEFloat(const fltSemantics &, const APInt &);
409 explicit IEEEFloat(double d);
410 explicit IEEEFloat(float f);
411 IEEEFloat(const IEEEFloat &);
413 ~IEEEFloat();
414
415 /// @}
416
417 /// Returns whether this instance allocated memory.
418 bool needsCleanup() const { return partCount() > 1; }
419
420 /// \name Convenience "constructors"
421 /// @{
422
423 /// @}
424
425 /// \name Arithmetic
426 /// @{
427
432 /// IEEE remainder.
434 /// C fmod, or llvm frem.
435 opStatus mod(const IEEEFloat &);
438 /// IEEE-754R 5.3.1: nextUp/nextDown.
439 opStatus next(bool nextDown);
440
441 /// @}
442
443 /// \name Sign operations.
444 /// @{
445
446 void changeSign();
447
448 /// @}
449
450 /// \name Conversions
451 /// @{
452
453 opStatus convert(const fltSemantics &, roundingMode, bool *);
455 roundingMode, bool *) const;
458 bool, roundingMode);
460 bool, roundingMode);
462 APInt bitcastToAPInt() const;
463 double convertToDouble() const;
464#ifdef HAS_IEE754_FLOAT128
465 float128 convertToQuad() const;
466#endif
467 float convertToFloat() const;
468
469 /// @}
470
471 /// The definition of equality is not straightforward for floating point, so
472 /// we won't use operator==. Use one of the following, or write whatever it
473 /// is you really mean.
474 bool operator==(const IEEEFloat &) const = delete;
475
476 /// IEEE comparison with another floating point number (NaNs compare
477 /// unordered, 0==-0).
478 cmpResult compare(const IEEEFloat &) const;
479
480 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
481 bool bitwiseIsEqual(const IEEEFloat &) const;
482
483 /// Write out a hexadecimal representation of the floating point value to DST,
484 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
485 /// Return the number of characters written, excluding the terminating NUL.
486 unsigned int convertToHexString(char *dst, unsigned int hexDigits,
487 bool upperCase, roundingMode) const;
488
489 /// \name IEEE-754R 5.7.2 General operations.
490 /// @{
491
492 /// IEEE-754R isSignMinus: Returns true if and only if the current value is
493 /// negative.
494 ///
495 /// This applies to zeros and NaNs as well.
496 bool isNegative() const { return sign; }
497
498 /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
499 ///
500 /// This implies that the current value of the float is not zero, subnormal,
501 /// infinite, or NaN following the definition of normality from IEEE-754R.
502 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
503
504 /// Returns true if and only if the current value is zero, subnormal, or
505 /// normal.
506 ///
507 /// This means that the value is not infinite or NaN.
508 bool isFinite() const { return !isNaN() && !isInfinity(); }
509
510 /// Returns true if and only if the float is plus or minus zero.
511 bool isZero() const { return category == fltCategory::fcZero; }
512
513 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
514 /// denormal.
515 bool isDenormal() const;
516
517 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
518 bool isInfinity() const { return category == fcInfinity; }
519
520 /// Returns true if and only if the float is a quiet or signaling NaN.
521 bool isNaN() const { return category == fcNaN; }
522
523 /// Returns true if and only if the float is a signaling NaN.
524 bool isSignaling() const;
525
526 /// @}
527
528 /// \name Simple Queries
529 /// @{
530
531 fltCategory getCategory() const { return category; }
532 const fltSemantics &getSemantics() const { return *semantics; }
533 bool isNonZero() const { return category != fltCategory::fcZero; }
534 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
535 bool isPosZero() const { return isZero() && !isNegative(); }
536 bool isNegZero() const { return isZero() && isNegative(); }
537
538 /// Returns true if and only if the number has the smallest possible non-zero
539 /// magnitude in the current semantics.
540 bool isSmallest() const;
541
542 /// Returns true if this is the smallest (by magnitude) normalized finite
543 /// number in the given semantics.
544 bool isSmallestNormalized() const;
545
546 /// Returns true if and only if the number has the largest possible finite
547 /// magnitude in the current semantics.
548 bool isLargest() const;
549
550 /// Returns true if and only if the number is an exact integer.
551 bool isInteger() const;
552
553 /// @}
554
555 IEEEFloat &operator=(const IEEEFloat &);
557
558 /// Overload to compute a hash code for an APFloat value.
559 ///
560 /// Note that the use of hash codes for floating point values is in general
561 /// frought with peril. Equality is hard to define for these values. For
562 /// example, should negative and positive zero hash to different codes? Are
563 /// they equal or not? This hash value implementation specifically
564 /// emphasizes producing different codes for different inputs in order to
565 /// be used in canonicalization and memoization. As such, equality is
566 /// bitwiseIsEqual, and 0 != -0.
567 friend hash_code hash_value(const IEEEFloat &Arg);
568
569 /// Converts this value into a decimal string.
570 ///
571 /// \param FormatPrecision The maximum number of digits of
572 /// precision to output. If there are fewer digits available,
573 /// zero padding will not be used unless the value is
574 /// integral and small enough to be expressed in
575 /// FormatPrecision digits. 0 means to use the natural
576 /// precision of the number.
577 /// \param FormatMaxPadding The maximum number of zeros to
578 /// consider inserting before falling back to scientific
579 /// notation. 0 means to always use scientific notation.
580 ///
581 /// \param TruncateZero Indicate whether to remove the trailing zero in
582 /// fraction part or not. Also setting this parameter to false forcing
583 /// producing of output more similar to default printf behavior.
584 /// Specifically the lower e is used as exponent delimiter and exponent
585 /// always contains no less than two digits.
586 ///
587 /// Number Precision MaxPadding Result
588 /// ------ --------- ---------- ------
589 /// 1.01E+4 5 2 10100
590 /// 1.01E+4 4 2 1.01E+4
591 /// 1.01E+4 5 1 1.01E+4
592 /// 1.01E-2 5 2 0.0101
593 /// 1.01E-2 4 2 0.0101
594 /// 1.01E-2 4 1 1.01E-2
595 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
596 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const;
597
598 /// If this value has an exact multiplicative inverse, store it in inv and
599 /// return true.
600 bool getExactInverse(APFloat *inv) const;
601
602 // If this is an exact power of two, return the exponent while ignoring the
603 // sign bit. If it's not an exact power of 2, return INT_MIN
605 int getExactLog2Abs() const;
606
607 // If this is an exact power of two, return the exponent. If it's not an exact
608 // power of 2, return INT_MIN
610 int getExactLog2() const {
611 return isNegative() ? INT_MIN : getExactLog2Abs();
612 }
613
614 /// Returns the exponent of the internal representation of the APFloat.
615 ///
616 /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
617 /// For special APFloat values, this returns special error codes:
618 ///
619 /// NaN -> \c IEK_NaN
620 /// 0 -> \c IEK_Zero
621 /// Inf -> \c IEK_Inf
622 ///
623 friend int ilogb(const IEEEFloat &Arg);
624
625 /// Returns: X * 2^Exp for integral exponents.
626 friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
627
628 friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
629
630 /// \name Special value setters.
631 /// @{
632
633 void makeLargest(bool Neg = false);
634 void makeSmallest(bool Neg = false);
635 void makeNaN(bool SNaN = false, bool Neg = false,
636 const APInt *fill = nullptr);
637 void makeInf(bool Neg = false);
638 void makeZero(bool Neg = false);
639 void makeQuiet();
640
641 /// Returns the smallest (by magnitude) normalized finite number in the given
642 /// semantics.
643 ///
644 /// \param Negative - True iff the number should be negative
645 void makeSmallestNormalized(bool Negative = false);
646
647 /// @}
648
650
651private:
652 /// \name Simple Queries
653 /// @{
654
655 integerPart *significandParts();
656 const integerPart *significandParts() const;
657 unsigned int partCount() const;
658
659 /// @}
660
661 /// \name Significand operations.
662 /// @{
663
664 integerPart addSignificand(const IEEEFloat &);
665 integerPart subtractSignificand(const IEEEFloat &, integerPart);
666 lostFraction addOrSubtractSignificand(const IEEEFloat &, bool subtract);
667 lostFraction multiplySignificand(const IEEEFloat &, IEEEFloat,
668 bool ignoreAddend = false);
669 lostFraction multiplySignificand(const IEEEFloat&);
670 lostFraction divideSignificand(const IEEEFloat &);
671 void incrementSignificand();
672 void initialize(const fltSemantics *);
673 void shiftSignificandLeft(unsigned int);
674 lostFraction shiftSignificandRight(unsigned int);
675 unsigned int significandLSB() const;
676 unsigned int significandMSB() const;
677 void zeroSignificand();
678 unsigned int getNumHighBits() const;
679 /// Return true if the significand excluding the integral bit is all ones.
680 bool isSignificandAllOnes() const;
681 bool isSignificandAllOnesExceptLSB() const;
682 /// Return true if the significand excluding the integral bit is all zeros.
683 bool isSignificandAllZeros() const;
684 bool isSignificandAllZerosExceptMSB() const;
685
686 /// @}
687
688 /// \name Arithmetic on special values.
689 /// @{
690
691 opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract);
692 opStatus divideSpecials(const IEEEFloat &);
693 opStatus multiplySpecials(const IEEEFloat &);
694 opStatus modSpecials(const IEEEFloat &);
695 opStatus remainderSpecials(const IEEEFloat&);
696
697 /// @}
698
699 /// \name Miscellany
700 /// @{
701
702 bool convertFromStringSpecials(StringRef str);
704 opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
705 opStatus handleOverflow(roundingMode);
706 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
707 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
708 unsigned int, bool, roundingMode,
709 bool *) const;
710 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
712 Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode);
713 Expected<opStatus> convertFromDecimalString(StringRef, roundingMode);
714 char *convertNormalToHexString(char *, unsigned int, bool,
715 roundingMode) const;
716 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
718 ExponentType exponentNaN() const;
719 ExponentType exponentInf() const;
720 ExponentType exponentZero() const;
721
722 /// @}
723
724 template <const fltSemantics &S> APInt convertIEEEFloatToAPInt() const;
725 APInt convertHalfAPFloatToAPInt() const;
726 APInt convertBFloatAPFloatToAPInt() const;
727 APInt convertFloatAPFloatToAPInt() const;
728 APInt convertDoubleAPFloatToAPInt() const;
729 APInt convertQuadrupleAPFloatToAPInt() const;
730 APInt convertF80LongDoubleAPFloatToAPInt() const;
731 APInt convertPPCDoubleDoubleLegacyAPFloatToAPInt() const;
732 APInt convertFloat8E5M2APFloatToAPInt() const;
733 APInt convertFloat8E5M2FNUZAPFloatToAPInt() const;
734 APInt convertFloat8E4M3APFloatToAPInt() const;
735 APInt convertFloat8E4M3FNAPFloatToAPInt() const;
736 APInt convertFloat8E4M3FNUZAPFloatToAPInt() const;
737 APInt convertFloat8E4M3B11FNUZAPFloatToAPInt() const;
738 APInt convertFloat8E3M4APFloatToAPInt() const;
739 APInt convertFloatTF32APFloatToAPInt() const;
740 APInt convertFloat8E8M0FNUAPFloatToAPInt() const;
741 APInt convertFloat6E3M2FNAPFloatToAPInt() const;
742 APInt convertFloat6E2M3FNAPFloatToAPInt() const;
743 APInt convertFloat4E2M1FNAPFloatToAPInt() const;
744 void initFromAPInt(const fltSemantics *Sem, const APInt &api);
745 template <const fltSemantics &S> void initFromIEEEAPInt(const APInt &api);
746 void initFromHalfAPInt(const APInt &api);
747 void initFromBFloatAPInt(const APInt &api);
748 void initFromFloatAPInt(const APInt &api);
749 void initFromDoubleAPInt(const APInt &api);
750 void initFromQuadrupleAPInt(const APInt &api);
751 void initFromF80LongDoubleAPInt(const APInt &api);
752 void initFromPPCDoubleDoubleLegacyAPInt(const APInt &api);
753 void initFromFloat8E5M2APInt(const APInt &api);
754 void initFromFloat8E5M2FNUZAPInt(const APInt &api);
755 void initFromFloat8E4M3APInt(const APInt &api);
756 void initFromFloat8E4M3FNAPInt(const APInt &api);
757 void initFromFloat8E4M3FNUZAPInt(const APInt &api);
758 void initFromFloat8E4M3B11FNUZAPInt(const APInt &api);
759 void initFromFloat8E3M4APInt(const APInt &api);
760 void initFromFloatTF32APInt(const APInt &api);
761 void initFromFloat8E8M0FNUAPInt(const APInt &api);
762 void initFromFloat6E3M2FNAPInt(const APInt &api);
763 void initFromFloat6E2M3FNAPInt(const APInt &api);
764 void initFromFloat4E2M1FNAPInt(const APInt &api);
765
766 void assign(const IEEEFloat &);
767 void copySignificand(const IEEEFloat &);
768 void freeSignificand();
769
770 /// Note: this must be the first data member.
771 /// The semantics that this value obeys.
772 const fltSemantics *semantics;
773
774 /// A binary fraction with an explicit integer bit.
775 ///
776 /// The significand must be at least one bit wider than the target precision.
777 union Significand {
778 integerPart part;
779 integerPart *parts;
780 } significand;
781
782 /// The signed unbiased exponent of the value.
783 ExponentType exponent;
784
785 /// What kind of floating point number this is.
786 ///
787 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
788 /// Using the extra bit keeps it from failing under VisualStudio.
789 fltCategory category : 3;
790
791 /// Sign bit of the number.
792 unsigned int sign : 1;
793};
794
795hash_code hash_value(const IEEEFloat &Arg);
796int ilogb(const IEEEFloat &Arg);
797IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
798IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM);
799
800// This mode implements more precise float in terms of two APFloats.
801// The interface and layout is designed for arbitrary underlying semantics,
802// though currently only PPCDoubleDouble semantics are supported, whose
803// corresponding underlying semantics are IEEEdouble.
804class DoubleAPFloat final {
805 // Note: this must be the first data member.
806 const fltSemantics *Semantics;
807 std::unique_ptr<APFloat[]> Floats;
808
809 opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c,
810 const APFloat &cc, roundingMode RM);
811
812 opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS,
813 DoubleAPFloat &Out, roundingMode RM);
814
815public:
816 DoubleAPFloat(const fltSemantics &S);
819 DoubleAPFloat(const fltSemantics &S, const APInt &I);
820 DoubleAPFloat(const fltSemantics &S, APFloat &&First, APFloat &&Second);
823
826
827 bool needsCleanup() const { return Floats != nullptr; }
828
829 inline APFloat &getFirst();
830 inline const APFloat &getFirst() const;
831 inline APFloat &getSecond();
832 inline const APFloat &getSecond() const;
833
840 opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
841 const DoubleAPFloat &Addend, roundingMode RM);
843 void changeSign();
845
846 fltCategory getCategory() const;
847 bool isNegative() const;
848
849 void makeInf(bool Neg);
850 void makeZero(bool Neg);
851 void makeLargest(bool Neg);
852 void makeSmallest(bool Neg);
853 void makeSmallestNormalized(bool Neg);
854 void makeNaN(bool SNaN, bool Neg, const APInt *fill);
855
856 cmpResult compare(const DoubleAPFloat &RHS) const;
857 bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
858 APInt bitcastToAPInt() const;
860 opStatus next(bool nextDown);
861
863 unsigned int Width, bool IsSigned, roundingMode RM,
864 bool *IsExact) const;
865 opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM);
867 unsigned int InputSize, bool IsSigned,
868 roundingMode RM);
870 unsigned int InputSize, bool IsSigned,
871 roundingMode RM);
872 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
873 bool UpperCase, roundingMode RM) const;
874
875 bool isDenormal() const;
876 bool isSmallest() const;
877 bool isSmallestNormalized() const;
878 bool isLargest() const;
879 bool isInteger() const;
880
881 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
882 unsigned FormatMaxPadding, bool TruncateZero = true) const;
883
884 bool getExactInverse(APFloat *inv) const;
885
887 int getExactLog2() const;
889 int getExactLog2Abs() const;
890
892 friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode);
893 friend hash_code hash_value(const DoubleAPFloat &Arg);
894};
895
897DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp, roundingMode RM);
899
900} // End detail namespace
901
902// This is a interface class that is currently forwarding functionalities from
903// detail::IEEEFloat.
904class APFloat : public APFloatBase {
907
908 static_assert(std::is_standard_layout<IEEEFloat>::value);
909
910 union Storage {
911 const fltSemantics *semantics;
913 DoubleAPFloat Double;
914
915 explicit Storage(IEEEFloat F, const fltSemantics &S);
916 explicit Storage(DoubleAPFloat F, const fltSemantics &S)
917 : Double(std::move(F)) {
918 assert(&S == &PPCDoubleDouble());
919 }
920
921 template <typename... ArgTypes>
922 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
923 if (usesLayout<IEEEFloat>(Semantics)) {
924 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
925 return;
926 }
927 if (usesLayout<DoubleAPFloat>(Semantics)) {
928 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
929 return;
930 }
931 llvm_unreachable("Unexpected semantics");
932 }
933
934 ~Storage() {
935 if (usesLayout<IEEEFloat>(*semantics)) {
936 IEEE.~IEEEFloat();
937 return;
938 }
939 if (usesLayout<DoubleAPFloat>(*semantics)) {
940 Double.~DoubleAPFloat();
941 return;
942 }
943 llvm_unreachable("Unexpected semantics");
944 }
945
946 Storage(const Storage &RHS) {
947 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
948 new (this) IEEEFloat(RHS.IEEE);
949 return;
950 }
951 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
952 new (this) DoubleAPFloat(RHS.Double);
953 return;
954 }
955 llvm_unreachable("Unexpected semantics");
956 }
957
958 Storage(Storage &&RHS) {
959 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
960 new (this) IEEEFloat(std::move(RHS.IEEE));
961 return;
962 }
963 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
964 new (this) DoubleAPFloat(std::move(RHS.Double));
965 return;
966 }
967 llvm_unreachable("Unexpected semantics");
968 }
969
970 Storage &operator=(const Storage &RHS) {
971 if (usesLayout<IEEEFloat>(*semantics) &&
972 usesLayout<IEEEFloat>(*RHS.semantics)) {
973 IEEE = RHS.IEEE;
974 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
975 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
976 Double = RHS.Double;
977 } else if (this != &RHS) {
978 this->~Storage();
979 new (this) Storage(RHS);
980 }
981 return *this;
982 }
983
984 Storage &operator=(Storage &&RHS) {
985 if (usesLayout<IEEEFloat>(*semantics) &&
986 usesLayout<IEEEFloat>(*RHS.semantics)) {
987 IEEE = std::move(RHS.IEEE);
988 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
989 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
990 Double = std::move(RHS.Double);
991 } else if (this != &RHS) {
992 this->~Storage();
993 new (this) Storage(std::move(RHS));
994 }
995 return *this;
996 }
997 } U;
998
999 template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
1000 static_assert(std::is_same<T, IEEEFloat>::value ||
1001 std::is_same<T, DoubleAPFloat>::value);
1002 if (std::is_same<T, DoubleAPFloat>::value) {
1003 return &Semantics == &PPCDoubleDouble();
1004 }
1005 return &Semantics != &PPCDoubleDouble();
1006 }
1007
1008 IEEEFloat &getIEEE() {
1009 if (usesLayout<IEEEFloat>(*U.semantics))
1010 return U.IEEE;
1011 if (usesLayout<DoubleAPFloat>(*U.semantics))
1012 return U.Double.getFirst().U.IEEE;
1013 llvm_unreachable("Unexpected semantics");
1014 }
1015
1016 const IEEEFloat &getIEEE() const {
1017 if (usesLayout<IEEEFloat>(*U.semantics))
1018 return U.IEEE;
1019 if (usesLayout<DoubleAPFloat>(*U.semantics))
1020 return U.Double.getFirst().U.IEEE;
1021 llvm_unreachable("Unexpected semantics");
1022 }
1023
1024 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
1025
1026 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
1027
1028 void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
1029 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
1030 }
1031
1032 void makeLargest(bool Neg) {
1033 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
1034 }
1035
1036 void makeSmallest(bool Neg) {
1037 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
1038 }
1039
1040 void makeSmallestNormalized(bool Neg) {
1041 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
1042 }
1043
1044 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
1045 explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
1046 : U(std::move(F), S) {}
1047
1048 cmpResult compareAbsoluteValue(const APFloat &RHS) const {
1049 assert(&getSemantics() == &RHS.getSemantics() &&
1050 "Should only compare APFloats with the same semantics");
1051 if (usesLayout<IEEEFloat>(getSemantics()))
1052 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
1053 if (usesLayout<DoubleAPFloat>(getSemantics()))
1054 return U.Double.compareAbsoluteValue(RHS.U.Double);
1055 llvm_unreachable("Unexpected semantics");
1056 }
1057
1058public:
1062 template <typename T,
1063 typename = std::enable_if_t<std::is_floating_point<T>::value>>
1064 APFloat(const fltSemantics &Semantics, T V) = delete;
1065 // TODO: Remove this constructor. This isn't faster than the first one.
1067 : U(Semantics, uninitialized) {}
1069 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
1070 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
1071 APFloat(const APFloat &RHS) = default;
1072 APFloat(APFloat &&RHS) = default;
1073
1074 ~APFloat() = default;
1075
1077
1078 /// Factory for Positive and Negative Zero.
1079 ///
1080 /// \param Negative True iff the number should be negative.
1081 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
1082 APFloat Val(Sem, uninitialized);
1083 Val.makeZero(Negative);
1084 return Val;
1085 }
1086
1087 /// Factory for Positive and Negative One.
1088 ///
1089 /// \param Negative True iff the number should be negative.
1090 static APFloat getOne(const fltSemantics &Sem, bool Negative = false) {
1091 APFloat Val(Sem, 1U);
1092 if (Negative)
1093 Val.changeSign();
1094 return Val;
1095 }
1096
1097 /// Factory for Positive and Negative Infinity.
1098 ///
1099 /// \param Negative True iff the number should be negative.
1100 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
1101 APFloat Val(Sem, uninitialized);
1102 Val.makeInf(Negative);
1103 return Val;
1104 }
1105
1106 /// Factory for NaN values.
1107 ///
1108 /// \param Negative - True iff the NaN generated should be negative.
1109 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
1110 /// default. The value is truncated as necessary.
1111 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
1112 uint64_t payload = 0) {
1113 if (payload) {
1114 APInt intPayload(64, payload);
1115 return getQNaN(Sem, Negative, &intPayload);
1116 } else {
1117 return getQNaN(Sem, Negative, nullptr);
1118 }
1119 }
1120
1121 /// Factory for QNaN values.
1122 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
1123 const APInt *payload = nullptr) {
1124 APFloat Val(Sem, uninitialized);
1125 Val.makeNaN(false, Negative, payload);
1126 return Val;
1127 }
1128
1129 /// Factory for SNaN values.
1130 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
1131 const APInt *payload = nullptr) {
1132 APFloat Val(Sem, uninitialized);
1133 Val.makeNaN(true, Negative, payload);
1134 return Val;
1135 }
1136
1137 /// Returns the largest finite number in the given semantics.
1138 ///
1139 /// \param Negative - True iff the number should be negative
1140 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
1141 APFloat Val(Sem, uninitialized);
1142 Val.makeLargest(Negative);
1143 return Val;
1144 }
1145
1146 /// Returns the smallest (by magnitude) finite number in the given semantics.
1147 /// Might be denormalized, which implies a relative loss of precision.
1148 ///
1149 /// \param Negative - True iff the number should be negative
1150 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
1151 APFloat Val(Sem, uninitialized);
1152 Val.makeSmallest(Negative);
1153 return Val;
1154 }
1155
1156 /// Returns the smallest (by magnitude) normalized finite number in the given
1157 /// semantics.
1158 ///
1159 /// \param Negative - True iff the number should be negative
1161 bool Negative = false) {
1162 APFloat Val(Sem, uninitialized);
1163 Val.makeSmallestNormalized(Negative);
1164 return Val;
1165 }
1166
1167 /// Returns a float which is bitcasted from an all one value int.
1168 ///
1169 /// \param Semantics - type float semantics
1171
1172 /// Returns true if the given semantics has actual significand.
1173 ///
1174 /// \param Sem - type float semantics
1175 static bool hasSignificand(const fltSemantics &Sem) {
1176 return &Sem != &Float8E8M0FNU();
1177 }
1178
1179 /// Used to insert APFloat objects, or objects that contain APFloat objects,
1180 /// into FoldingSets.
1181 void Profile(FoldingSetNodeID &NID) const;
1182
1184 assert(&getSemantics() == &RHS.getSemantics() &&
1185 "Should only call on two APFloats with the same semantics");
1186 if (usesLayout<IEEEFloat>(getSemantics()))
1187 return U.IEEE.add(RHS.U.IEEE, RM);
1188 if (usesLayout<DoubleAPFloat>(getSemantics()))
1189 return U.Double.add(RHS.U.Double, RM);
1190 llvm_unreachable("Unexpected semantics");
1191 }
1193 assert(&getSemantics() == &RHS.getSemantics() &&
1194 "Should only call on two APFloats with the same semantics");
1195 if (usesLayout<IEEEFloat>(getSemantics()))
1196 return U.IEEE.subtract(RHS.U.IEEE, RM);
1197 if (usesLayout<DoubleAPFloat>(getSemantics()))
1198 return U.Double.subtract(RHS.U.Double, RM);
1199 llvm_unreachable("Unexpected semantics");
1200 }
1202 assert(&getSemantics() == &RHS.getSemantics() &&
1203 "Should only call on two APFloats with the same semantics");
1204 if (usesLayout<IEEEFloat>(getSemantics()))
1205 return U.IEEE.multiply(RHS.U.IEEE, RM);
1206 if (usesLayout<DoubleAPFloat>(getSemantics()))
1207 return U.Double.multiply(RHS.U.Double, RM);
1208 llvm_unreachable("Unexpected semantics");
1209 }
1211 assert(&getSemantics() == &RHS.getSemantics() &&
1212 "Should only call on two APFloats with the same semantics");
1213 if (usesLayout<IEEEFloat>(getSemantics()))
1214 return U.IEEE.divide(RHS.U.IEEE, RM);
1215 if (usesLayout<DoubleAPFloat>(getSemantics()))
1216 return U.Double.divide(RHS.U.Double, RM);
1217 llvm_unreachable("Unexpected semantics");
1218 }
1220 assert(&getSemantics() == &RHS.getSemantics() &&
1221 "Should only call on two APFloats with the same semantics");
1222 if (usesLayout<IEEEFloat>(getSemantics()))
1223 return U.IEEE.remainder(RHS.U.IEEE);
1224 if (usesLayout<DoubleAPFloat>(getSemantics()))
1225 return U.Double.remainder(RHS.U.Double);
1226 llvm_unreachable("Unexpected semantics");
1227 }
1229 assert(&getSemantics() == &RHS.getSemantics() &&
1230 "Should only call on two APFloats with the same semantics");
1231 if (usesLayout<IEEEFloat>(getSemantics()))
1232 return U.IEEE.mod(RHS.U.IEEE);
1233 if (usesLayout<DoubleAPFloat>(getSemantics()))
1234 return U.Double.mod(RHS.U.Double);
1235 llvm_unreachable("Unexpected semantics");
1236 }
1237 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
1238 roundingMode RM) {
1239 assert(&getSemantics() == &Multiplicand.getSemantics() &&
1240 "Should only call on APFloats with the same semantics");
1241 assert(&getSemantics() == &Addend.getSemantics() &&
1242 "Should only call on APFloats with the same semantics");
1243 if (usesLayout<IEEEFloat>(getSemantics()))
1244 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1245 if (usesLayout<DoubleAPFloat>(getSemantics()))
1246 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1247 RM);
1248 llvm_unreachable("Unexpected semantics");
1249 }
1252 }
1253
1254 // TODO: bool parameters are not readable and a source of bugs.
1255 // Do something.
1256 opStatus next(bool nextDown) {
1258 }
1259
1260 /// Negate an APFloat.
1262 APFloat Result(*this);
1263 Result.changeSign();
1264 return Result;
1265 }
1266
1267 /// Add two APFloats, rounding ties to the nearest even.
1268 /// No error checking.
1270 APFloat Result(*this);
1271 (void)Result.add(RHS, rmNearestTiesToEven);
1272 return Result;
1273 }
1274
1275 /// Subtract two APFloats, rounding ties to the nearest even.
1276 /// No error checking.
1278 APFloat Result(*this);
1279 (void)Result.subtract(RHS, rmNearestTiesToEven);
1280 return Result;
1281 }
1282
1283 /// Multiply two APFloats, rounding ties to the nearest even.
1284 /// No error checking.
1286 APFloat Result(*this);
1287 (void)Result.multiply(RHS, rmNearestTiesToEven);
1288 return Result;
1289 }
1290
1291 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1292 /// No error checking.
1294 APFloat Result(*this);
1295 (void)Result.divide(RHS, rmNearestTiesToEven);
1296 return Result;
1297 }
1298
1300 void clearSign() {
1301 if (isNegative())
1302 changeSign();
1303 }
1304 void copySign(const APFloat &RHS) {
1305 if (isNegative() != RHS.isNegative())
1306 changeSign();
1307 }
1308
1309 /// A static helper to produce a copy of an APFloat value with its sign
1310 /// copied from some other APFloat.
1311 static APFloat copySign(APFloat Value, const APFloat &Sign) {
1312 Value.copySign(Sign);
1313 return Value;
1314 }
1315
1316 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
1317 /// This preserves the sign and payload bits.
1319 APFloat Result(*this);
1320 Result.getIEEE().makeQuiet();
1321 return Result;
1322 }
1323
1324 opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
1325 bool *losesInfo);
1327 unsigned int Width, bool IsSigned, roundingMode RM,
1328 bool *IsExact) const {
1330 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1331 }
1333 bool *IsExact) const;
1334 opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
1335 roundingMode RM) {
1336 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input, IsSigned, RM));
1337 }
1339 unsigned int InputSize, bool IsSigned,
1340 roundingMode RM) {
1342 convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM));
1343 }
1345 unsigned int InputSize, bool IsSigned,
1346 roundingMode RM) {
1348 convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
1349 }
1353 }
1354
1355 /// Converts this APFloat to host double value.
1356 ///
1357 /// \pre The APFloat must be built using semantics, that can be represented by
1358 /// the host double type without loss of precision. It can be IEEEdouble and
1359 /// shorter semantics, like IEEEsingle and others.
1360 double convertToDouble() const;
1361
1362 /// Converts this APFloat to host float value.
1363 ///
1364 /// \pre The APFloat must be built using semantics, that can be represented by
1365 /// the host float type without loss of precision. It can be IEEEquad and
1366 /// shorter semantics, like IEEEdouble and others.
1367#ifdef HAS_IEE754_FLOAT128
1368 float128 convertToQuad() const;
1369#endif
1370
1371 /// Converts this APFloat to host float value.
1372 ///
1373 /// \pre The APFloat must be built using semantics, that can be represented by
1374 /// the host float type without loss of precision. It can be IEEEsingle and
1375 /// shorter semantics, like IEEEhalf.
1376 float convertToFloat() const;
1377
1378 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
1379
1380 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; }
1381
1382 bool operator<(const APFloat &RHS) const {
1383 return compare(RHS) == cmpLessThan;
1384 }
1385
1386 bool operator>(const APFloat &RHS) const {
1387 return compare(RHS) == cmpGreaterThan;
1388 }
1389
1390 bool operator<=(const APFloat &RHS) const {
1391 cmpResult Res = compare(RHS);
1392 return Res == cmpLessThan || Res == cmpEqual;
1393 }
1394
1395 bool operator>=(const APFloat &RHS) const {
1396 cmpResult Res = compare(RHS);
1397 return Res == cmpGreaterThan || Res == cmpEqual;
1398 }
1399
1401 assert(&getSemantics() == &RHS.getSemantics() &&
1402 "Should only compare APFloats with the same semantics");
1403 if (usesLayout<IEEEFloat>(getSemantics()))
1404 return U.IEEE.compare(RHS.U.IEEE);
1405 if (usesLayout<DoubleAPFloat>(getSemantics()))
1406 return U.Double.compare(RHS.U.Double);
1407 llvm_unreachable("Unexpected semantics");
1408 }
1409
1410 bool bitwiseIsEqual(const APFloat &RHS) const {
1411 if (&getSemantics() != &RHS.getSemantics())
1412 return false;
1413 if (usesLayout<IEEEFloat>(getSemantics()))
1414 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1415 if (usesLayout<DoubleAPFloat>(getSemantics()))
1416 return U.Double.bitwiseIsEqual(RHS.U.Double);
1417 llvm_unreachable("Unexpected semantics");
1418 }
1419
1420 /// We don't rely on operator== working on double values, as
1421 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1422 /// As such, this method can be used to do an exact bit-for-bit comparison of
1423 /// two floating point values.
1424 ///
1425 /// We leave the version with the double argument here because it's just so
1426 /// convenient to write "2.0" and the like. Without this function we'd
1427 /// have to duplicate its logic everywhere it's called.
1428 bool isExactlyValue(double V) const {
1429 bool ignored;
1430 APFloat Tmp(V);
1432 return bitwiseIsEqual(Tmp);
1433 }
1434
1435 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
1436 bool UpperCase, roundingMode RM) const {
1438 convertToHexString(DST, HexDigits, UpperCase, RM));
1439 }
1440
1441 bool isZero() const { return getCategory() == fcZero; }
1442 bool isInfinity() const { return getCategory() == fcInfinity; }
1443 bool isNaN() const { return getCategory() == fcNaN; }
1444
1445 bool isNegative() const { return getIEEE().isNegative(); }
1447 bool isSignaling() const { return getIEEE().isSignaling(); }
1448
1449 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
1450 bool isFinite() const { return !isNaN() && !isInfinity(); }
1451
1452 fltCategory getCategory() const { return getIEEE().getCategory(); }
1453 const fltSemantics &getSemantics() const { return *U.semantics; }
1454 bool isNonZero() const { return !isZero(); }
1455 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
1456 bool isPosZero() const { return isZero() && !isNegative(); }
1457 bool isNegZero() const { return isZero() && isNegative(); }
1458 bool isPosInfinity() const { return isInfinity() && !isNegative(); }
1459 bool isNegInfinity() const { return isInfinity() && isNegative(); }
1463 bool isIEEE() const { return usesLayout<IEEEFloat>(getSemantics()); }
1464
1467 }
1468
1469 /// Return the FPClassTest which will return true for the value.
1470 FPClassTest classify() const;
1471
1472 APFloat &operator=(const APFloat &RHS) = default;
1474
1475 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
1476 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
1478 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1479 }
1480
1481 void print(raw_ostream &) const;
1482 void dump() const;
1483
1484 bool getExactInverse(APFloat *inv) const {
1486 }
1487
1489 int getExactLog2Abs() const {
1491 }
1492
1494 int getExactLog2() const {
1496 }
1497
1498 friend hash_code hash_value(const APFloat &Arg);
1499 friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); }
1500 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
1501 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
1504};
1505
1506static_assert(sizeof(APFloat) == sizeof(detail::IEEEFloat),
1507 "Empty base class optimization is not performed.");
1508
1509/// See friend declarations above.
1510///
1511/// These additional declarations are required in order to compile LLVM with IBM
1512/// xlC compiler.
1513hash_code hash_value(const APFloat &Arg);
1515 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1516 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
1517 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1518 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
1519 llvm_unreachable("Unexpected semantics");
1520}
1521
1522/// Equivalent of C standard library function.
1523///
1524/// While the C standard says Exp is an unspecified value for infinity and nan,
1525/// this returns INT_MAX for infinities, and INT_MIN for NaNs.
1526inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
1527 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1528 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
1529 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1530 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
1531 llvm_unreachable("Unexpected semantics");
1532}
1533/// Returns the absolute value of the argument.
1535 X.clearSign();
1536 return X;
1537}
1538
1539/// Returns the negated value of the argument.
1541 X.changeSign();
1542 return X;
1543}
1544
1545/// Implements IEEE-754 2019 minimumNumber semantics. Returns the smaller of the
1546/// 2 arguments if both are not NaN. If either argument is a NaN, returns the
1547/// other argument. -0 is treated as ordered less than +0.
1549inline APFloat minnum(const APFloat &A, const APFloat &B) {
1550 if (A.isNaN())
1551 return B;
1552 if (B.isNaN())
1553 return A;
1554 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1555 return A.isNegative() ? A : B;
1556 return B < A ? B : A;
1557}
1558
1559/// Implements IEEE-754 2019 maximumNumber semantics. Returns the larger of the
1560/// 2 arguments if both are not NaN. If either argument is a NaN, returns the
1561/// other argument. +0 is treated as ordered greater than -0.
1563inline APFloat maxnum(const APFloat &A, const APFloat &B) {
1564 if (A.isNaN())
1565 return B;
1566 if (B.isNaN())
1567 return A;
1568 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1569 return A.isNegative() ? B : A;
1570 return A < B ? B : A;
1571}
1572
1573/// Implements IEEE 754-2019 minimum semantics. Returns the smaller of 2
1574/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1575/// as less than +0.
1577inline APFloat minimum(const APFloat &A, const APFloat &B) {
1578 if (A.isNaN())
1579 return A.makeQuiet();
1580 if (B.isNaN())
1581 return B.makeQuiet();
1582 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1583 return A.isNegative() ? A : B;
1584 return B < A ? B : A;
1585}
1586
1587/// Implements IEEE 754-2019 minimumNumber semantics. Returns the smaller
1588/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1590inline APFloat minimumnum(const APFloat &A, const APFloat &B) {
1591 if (A.isNaN())
1592 return B.isNaN() ? B.makeQuiet() : B;
1593 if (B.isNaN())
1594 return A;
1595 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1596 return A.isNegative() ? A : B;
1597 return B < A ? B : A;
1598}
1599
1600/// Implements IEEE 754-2019 maximum semantics. Returns the larger of 2
1601/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1602/// as less than +0.
1604inline APFloat maximum(const APFloat &A, const APFloat &B) {
1605 if (A.isNaN())
1606 return A.makeQuiet();
1607 if (B.isNaN())
1608 return B.makeQuiet();
1609 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1610 return A.isNegative() ? B : A;
1611 return A < B ? B : A;
1612}
1613
1614/// Implements IEEE 754-2019 maximumNumber semantics. Returns the larger
1615/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1617inline APFloat maximumnum(const APFloat &A, const APFloat &B) {
1618 if (A.isNaN())
1619 return B.isNaN() ? B.makeQuiet() : B;
1620 if (B.isNaN())
1621 return A;
1622 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1623 return A.isNegative() ? B : A;
1624 return A < B ? B : A;
1625}
1626
1628 V.print(OS);
1629 return OS;
1630}
1631
1632// We want the following functions to be available in the header for inlining.
1633// We cannot define them inline in the class definition of `DoubleAPFloat`
1634// because doing so would instantiate `std::unique_ptr<APFloat[]>` before
1635// `APFloat` is defined, and that would be undefined behavior.
1636namespace detail {
1637
1639 if (this != &RHS) {
1640 this->~DoubleAPFloat();
1641 new (this) DoubleAPFloat(std::move(RHS));
1642 }
1643 return *this;
1644}
1645
1646APFloat &DoubleAPFloat::getFirst() { return Floats[0]; }
1647const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; }
1648APFloat &DoubleAPFloat::getSecond() { return Floats[1]; }
1649const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; }
1650
1651} // namespace detail
1652
1653} // namespace llvm
1654
1655#undef APFLOAT_DISPATCH_ON_SEMANTICS
1656#endif // LLVM_ADT_APFLOAT_H
aarch64 promote const
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)
Definition: APFloat.h:25
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_READNONE
Definition: Compiler.h:299
#define LLVM_READONLY
Definition: Compiler.h:306
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
expand large fp convert
Utilities for dealing with flags related to floating point properties and mode controls.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Load MIR Sample Profile
#define T
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
Value * LHS
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:1122
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition: APFloat.h:1130
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1210
bool getExactInverse(APFloat *inv) const
Definition: APFloat.h:1484
APFloat & operator=(APFloat &&RHS)=default
bool isFiniteNonZero() const
Definition: APFloat.h:1455
APFloat(const APFloat &RHS)=default
void copySign(const APFloat &RHS)
Definition: APFloat.h:1304
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5463
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.h:1489
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1192
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1410
bool isNegative() const
Definition: APFloat.h:1445
~APFloat()=default
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1269
friend DoubleAPFloat
Definition: APFloat.h:1503
double convertToDouble() const
Converts this APFloat to host double value.
Definition: APFloat.cpp:5525
bool isPosInfinity() const
Definition: APFloat.h:1458
APFloat(APFloat &&RHS)=default
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition: APFloat.h:1475
bool isNormal() const
Definition: APFloat.h:1449
bool isDenormal() const
Definition: APFloat.h:1446
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition: APFloat.h:1428
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1183
LLVM_READONLY int getExactLog2() const
Definition: APFloat.h:1494
APFloat(double d)
Definition: APFloat.h:1069
APFloat & operator=(const APFloat &RHS)=default
static APFloat getAllOnesValue(const fltSemantics &Semantics)
Returns a float which is bitcasted from an all one value int.
Definition: APFloat.cpp:5488
APFloat(const fltSemantics &Semantics, integerPart I)
Definition: APFloat.h:1061
bool operator!=(const APFloat &RHS) const
Definition: APFloat.h:1380
APFloat(const fltSemantics &Semantics, T V)=delete
const fltSemantics & getSemantics() const
Definition: APFloat.h:1453
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1277
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1285
APFloat(const fltSemantics &Semantics)
Definition: APFloat.h:1059
bool isNonZero() const
Definition: APFloat.h:1454
void clearSign()
Definition: APFloat.h:1300
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1338
bool operator<(const APFloat &RHS) const
Definition: APFloat.h:1382
bool isFinite() const
Definition: APFloat.h:1450
APFloat makeQuiet() const
Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
Definition: APFloat.h:1318
bool isNaN() const
Definition: APFloat.h:1443
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1334
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
Definition: APFloat.h:1090
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition: APFloat.h:1435
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1201
float convertToFloat() const
Converts this APFloat to host float value.
Definition: APFloat.cpp:5553
bool isSignaling() const
Definition: APFloat.h:1447
bool operator>(const APFloat &RHS) const
Definition: APFloat.h:1386
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1237
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
Definition: APFloat.h:1293
opStatus remainder(const APFloat &RHS)
Definition: APFloat.h:1219
APFloat operator-() const
Negate an APFloat.
Definition: APFloat.h:1261
bool isZero() const
Definition: APFloat.h:1441
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1160
APInt bitcastToAPInt() const
Definition: APFloat.h:1351
bool isLargest() const
Definition: APFloat.h:1461
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
bool isSmallest() const
Definition: APFloat.h:1460
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1140
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1326
opStatus next(bool nextDown)
Definition: APFloat.h:1256
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1100
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
bool operator>=(const APFloat &RHS) const
Definition: APFloat.h:1395
bool needsCleanup() const
Definition: APFloat.h:1076
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition: APFloat.h:1150
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5450
bool operator==(const APFloat &RHS) const
Definition: APFloat.h:1378
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1228
bool isPosZero() const
Definition: APFloat.h:1456
friend int ilogb(const APFloat &Arg)
Definition: APFloat.h:1499
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:5430
fltCategory getCategory() const
Definition: APFloat.h:1452
APFloat(const fltSemantics &Semantics, uninitializedTag)
Definition: APFloat.h:1066
bool isInteger() const
Definition: APFloat.h:1462
bool isNegInfinity() const
Definition: APFloat.h:1459
friend IEEEFloat
Definition: APFloat.h:1502
void dump() const
Definition: APFloat.cpp:5499
bool isNegZero() const
Definition: APFloat.h:1457
void print(raw_ostream &) const
Definition: APFloat.cpp:5492
static APFloat copySign(APFloat Value, const APFloat &Sign)
A static helper to produce a copy of an APFloat value with its sign copied from some other APFloat.
Definition: APFloat.h:1311
APFloat(float f)
Definition: APFloat.h:1070
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1250
void changeSign()
Definition: APFloat.h:1299
friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:5435
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1111
static bool hasSignificand(const fltSemantics &Sem)
Returns true if the given semantics has actual significand.
Definition: APFloat.h:1175
bool isIEEE() const
Definition: APFloat.h:1463
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:1081
cmpResult compare(const APFloat &RHS) const
Definition: APFloat.h:1400
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1344
bool isSmallestNormalized() const
Definition: APFloat.h:1465
APFloat(const fltSemantics &Semantics, const APInt &I)
Definition: APFloat.h:1068
bool isInfinity() const
Definition: APFloat.h:1442
bool operator<=(const APFloat &RHS) const
Definition: APFloat.h:1390
Class for arbitrary precision integers.
Definition: APInt.h:78
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
Definition: APInt.h:86
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
Tagged union holding either a T or a Error.
Definition: Error.h:481
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:327
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:310
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
void makeSmallestNormalized(bool Neg)
Definition: APFloat.cpp:5221
DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:4879
LLVM_READONLY int getExactLog2() const
Definition: APFloat.cpp:5386
opStatus remainder(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:5127
opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5031
fltCategory getCategory() const
Definition: APFloat.cpp:5191
bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:5242
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.cpp:5391
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.cpp:5288
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.cpp:5310
APInt bitcastToAPInt() const
Definition: APFloat.cpp:5253
bool getExactInverse(APFloat *inv) const
Definition: APFloat.cpp:5375
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:5262
opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5023
cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:5171
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.cpp:5280
void makeSmallest(bool Neg)
Definition: APFloat.cpp:5215
opStatus next(bool nextDown)
Definition: APFloat.cpp:5271
friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode)
opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5117
friend hash_code hash_value(const DoubleAPFloat &Arg)
Definition: APFloat.cpp:5247
bool isSmallestNormalized() const
Definition: APFloat.cpp:5344
opStatus mod(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:5136
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
Definition: APFloat.cpp:5366
void makeLargest(bool Neg)
Definition: APFloat.cpp:5207
cmpResult compare(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:5234
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.cpp:5157
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.cpp:5299
opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
Definition: APFloat.cpp:5145
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition: APFloat.cpp:5320
bool needsCleanup() const
Definition: APFloat.h:827
opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5018
friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode)
void makeNaN(bool SNaN, bool Neg, const APInt *fill)
Definition: APFloat.cpp:5229
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:3344
cmpResult compareAbsoluteValue(const IEEEFloat &) const
Definition: APFloat.cpp:1532
opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
Definition: APFloat.cpp:2283
fltCategory getCategory() const
Definition: APFloat.h:531
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition: APFloat.cpp:2853
bool isNonZero() const
Definition: APFloat.h:533
bool isFiniteNonZero() const
Definition: APFloat.h:534
bool getExactInverse(APFloat *inv) const
If this value has an exact multiplicative inverse, store it in inv and return true.
Definition: APFloat.cpp:4517
bool needsCleanup() const
Returns whether this instance allocated memory.
Definition: APFloat.h:418
void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
Definition: APFloat.cpp:4123
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.cpp:4546
APInt bitcastToAPInt() const
Definition: APFloat.cpp:3752
cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition: APFloat.cpp:2454
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
Definition: APFloat.h:496
opStatus divide(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2153
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
Definition: APFloat.h:521
opStatus remainder(const IEEEFloat &)
IEEE remainder.
Definition: APFloat.cpp:2173
double convertToDouble() const
Definition: APFloat.cpp:3819
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
Definition: APFloat.cpp:2871
float convertToFloat() const
Definition: APFloat.cpp:3812
opStatus subtract(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2127
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Converts this value into a decimal string.
Definition: APFloat.cpp:4474
void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
Definition: APFloat.cpp:4155
void makeInf(bool Neg=false)
Definition: APFloat.cpp:4736
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
Definition: APFloat.cpp:2897
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
Definition: APFloat.h:502
friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
Definition: APFloat.cpp:4810
bool isSmallestNormalized() const
Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.cpp:1032
bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
Definition: APFloat.cpp:1134
opStatus add(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2121
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
Definition: APFloat.h:508
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:3287
void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
Definition: APFloat.cpp:921
friend int ilogb(const IEEEFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition: APFloat.cpp:4771
opStatus multiply(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2133
friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
Definition: APFloat.cpp:3493
opStatus roundToIntegral(roundingMode)
Definition: APFloat.cpp:2367
IEEEFloat & operator=(const IEEEFloat &)
Definition: APFloat.cpp:993
friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Returns: X * 2^Exp for integral exponents.
Definition: APFloat.cpp:4789
bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition: APFloat.cpp:1159
void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.cpp:4169
bool isInteger() const
Returns true if and only if the number is an exact integer.
Definition: APFloat.cpp:1151
bool isPosZero() const
Definition: APFloat.h:535
opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2321
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
Definition: APFloat.cpp:4591
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
Definition: APFloat.h:518
const fltSemantics & getSemantics() const
Definition: APFloat.h:532
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition: APFloat.h:511
bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
Definition: APFloat.cpp:4575
bool operator==(const IEEEFloat &) const =delete
The definition of equality is not straightforward for floating point, so we won't use operator==.
void makeZero(bool Neg=false)
Definition: APFloat.cpp:4751
LLVM_READONLY int getExactLog2() const
Definition: APFloat.h:610
bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
Definition: APFloat.cpp:1018
opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
Definition: APFloat.cpp:2793
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:1024
bool isNegZero() const
Definition: APFloat.h:536
An opaque object representing a hash code.
Definition: Hashing.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static constexpr opStatus opInexact
Definition: APFloat.h:394
APFloatBase::roundingMode roundingMode
Definition: APFloat.h:371
static constexpr fltCategory fcNaN
Definition: APFloat.h:396
static constexpr opStatus opDivByZero
Definition: APFloat.h:391
static constexpr opStatus opOverflow
Definition: APFloat.h:392
static constexpr cmpResult cmpLessThan
Definition: APFloat.h:386
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:382
static constexpr uninitializedTag uninitialized
Definition: APFloat.h:376
static constexpr fltCategory fcZero
Definition: APFloat.h:398
static constexpr opStatus opOK
Definition: APFloat.h:389
static constexpr cmpResult cmpGreaterThan
Definition: APFloat.h:387
static constexpr unsigned integerPartWidth
Definition: APFloat.h:384
APFloatBase::ExponentType ExponentType
Definition: APFloat.h:375
hash_code hash_value(const IEEEFloat &Arg)
Definition: APFloat.cpp:3493
static constexpr fltCategory fcNormal
Definition: APFloat.h:397
static constexpr opStatus opInvalidOp
Definition: APFloat.h:390
IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition: APFloat.cpp:4789
APFloatBase::fltCategory fltCategory
Definition: APFloat.h:374
IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM)
Definition: APFloat.cpp:4810
static constexpr cmpResult cmpUnordered
Definition: APFloat.h:388
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:381
static constexpr fltCategory fcInfinity
Definition: APFloat.h:395
static constexpr roundingMode rmNearestTiesToAway
Definition: APFloat.h:379
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:383
static constexpr opStatus opUnderflow
Definition: APFloat.h:393
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:377
int ilogb(const IEEEFloat &Arg)
Definition: APFloat.cpp:4771
static constexpr cmpResult cmpEqual
Definition: APFloat.h:385
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:136
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1534
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1604
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1526
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Definition: APFloat.h:1563
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
Definition: APFloat.h:49
@ lfMoreThanHalf
Definition: APFloat.h:53
@ lfLessThanHalf
Definition: APFloat.h:51
@ lfExactlyHalf
Definition: APFloat.h:52
@ lfExactlyZero
Definition: APFloat.h:50
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition: APFloat.h:1590
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Definition: APFloat.h:1514
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
Definition: APFloat.h:1549
RoundingMode
Rounding mode.
@ TowardZero
roundTowardZero.
@ NearestTiesToEven
roundTiesToEven.
@ TowardPositive
roundTowardPositive.
@ NearestTiesToAway
roundTiesToAway.
@ TowardNegative
roundTowardNegative.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:303
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition: APFloat.h:1540
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1577
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition: APFloat.h:1617
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:143
static const llvm::fltSemantics & EnumToSemantics(Semantics S)
Definition: APFloat.cpp:163
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:257
static bool semanticsHasInf(const fltSemantics &)
Definition: APFloat.cpp:348
static const fltSemantics & Float6E3M2FN() LLVM_READNONE
Definition: APFloat.cpp:277
static constexpr roundingMode rmNearestTiesToAway
Definition: APFloat.h:307
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:292
static const fltSemantics & PPCDoubleDoubleLegacy() LLVM_READNONE
Definition: APFloat.cpp:263
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:305
static ExponentType semanticsMinExponent(const fltSemantics &)
Definition: APFloat.cpp:323
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition: APFloat.h:300
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:302
static unsigned int semanticsSizeInBits(const fltSemantics &)
Definition: APFloat.cpp:326
static bool semanticsHasSignedRepr(const fltSemantics &)
Definition: APFloat.cpp:344
static const fltSemantics & Float8E4M3() LLVM_READNONE
Definition: APFloat.cpp:268
static unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
Definition: APFloat.cpp:370
static const fltSemantics & Float8E4M3FN() LLVM_READNONE
Definition: APFloat.cpp:269
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:260
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:306
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:280
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
Definition: APFloat.h:336
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:259
static const fltSemantics & Float4E2M1FN() LLVM_READNONE
Definition: APFloat.cpp:279
static const fltSemantics & Float8E8M0FNU() LLVM_READNONE
Definition: APFloat.cpp:276
static const fltSemantics & Float8E4M3B11FNUZ() LLVM_READNONE
Definition: APFloat.cpp:271
static const fltSemantics & Bogus() LLVM_READNONE
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real.
Definition: APFloat.cpp:283
static ExponentType semanticsMaxExponent(const fltSemantics &)
Definition: APFloat.cpp:319
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:315
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:258
static bool semanticsHasNaN(const fltSemantics &)
Definition: APFloat.cpp:352
static const fltSemantics & Float8E5M2() LLVM_READNONE
Definition: APFloat.cpp:266
static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition: APFloat.cpp:210
static constexpr unsigned integerPartWidth
Definition: APFloat.h:145
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:255
APInt::WordType integerPart
Definition: APFloat.h:144
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:304
static bool semanticsHasZero(const fltSemantics &)
Definition: APFloat.cpp:340
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition: APFloat.cpp:356
static const fltSemantics & Float8E4M3FNUZ() LLVM_READNONE
Definition: APFloat.cpp:270
IlogbErrorKinds
Enumeration of ilogb error results.
Definition: APFloat.h:341
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:256
static const fltSemantics & FloatTF32() LLVM_READNONE
Definition: APFloat.cpp:275
static bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B)
Definition: APFloat.cpp:285
static const fltSemantics & Float8E5M2FNUZ() LLVM_READNONE
Definition: APFloat.cpp:267
static const fltSemantics & Float6E2M3FN() LLVM_READNONE
Definition: APFloat.cpp:278
fltCategory
Category of internally-represented number.
Definition: APFloat.h:328
@ S_PPCDoubleDoubleLegacy
Definition: APFloat.h:192
static const fltSemantics & Float8E3M4() LLVM_READNONE
Definition: APFloat.cpp:274
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:318
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
Definition: APFloat.h:148
static unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
Definition: APFloat.cpp:329