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 /// @}
285
286 /// IEEE-754R 5.11: Floating Point Comparison Relations.
292 };
293
294 /// IEEE-754R 4.3: Rounding-direction attributes.
296
304
305 /// IEEE-754R 7: Default exception handling.
306 ///
307 /// opUnderflow or opOverflow are always returned or-ed with opInexact.
308 ///
309 /// APFloat models this behavior specified by IEEE-754:
310 /// "For operations producing results in floating-point format, the default
311 /// result of an operation that signals the invalid operation exception
312 /// shall be a quiet NaN."
313 enum opStatus {
314 opOK = 0x00,
319 opInexact = 0x10
320 };
321
322 /// Category of internally-represented number.
327 fcZero
328 };
329
330 /// Convenience enum used to construct an uninitialized APFloat.
333 };
334
335 /// Enumeration of \c ilogb error results.
337 IEK_Zero = INT_MIN + 1,
338 IEK_NaN = INT_MIN,
339 IEK_Inf = INT_MAX
340 };
341
342 static unsigned int semanticsPrecision(const fltSemantics &);
345 static unsigned int semanticsSizeInBits(const fltSemantics &);
346 static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
347 static bool semanticsHasZero(const fltSemantics &);
348 static bool semanticsHasSignedRepr(const fltSemantics &);
349 static bool semanticsHasInf(const fltSemantics &);
350 static bool semanticsHasNaN(const fltSemantics &);
351
352 // Returns true if any number described by \p Src can be precisely represented
353 // by a normal (not subnormal) value in \p Dst.
354 static bool isRepresentableAsNormalIn(const fltSemantics &Src,
355 const fltSemantics &Dst);
356
357 /// Returns the size of the floating point number (in bits) in the given
358 /// semantics.
359 static unsigned getSizeInBits(const fltSemantics &Sem);
360};
361
362namespace detail {
363
384static constexpr opStatus opOK = APFloatBase::opOK;
394
395class IEEEFloat final {
396public:
397 /// \name Constructors
398 /// @{
399
400 IEEEFloat(const fltSemantics &); // Default construct to +0.0
403 IEEEFloat(const fltSemantics &, const APInt &);
404 explicit IEEEFloat(double d);
405 explicit IEEEFloat(float f);
406 IEEEFloat(const IEEEFloat &);
408 ~IEEEFloat();
409
410 /// @}
411
412 /// Returns whether this instance allocated memory.
413 bool needsCleanup() const { return partCount() > 1; }
414
415 /// \name Convenience "constructors"
416 /// @{
417
418 /// @}
419
420 /// \name Arithmetic
421 /// @{
422
427 /// IEEE remainder.
429 /// C fmod, or llvm frem.
430 opStatus mod(const IEEEFloat &);
433 /// IEEE-754R 5.3.1: nextUp/nextDown.
434 opStatus next(bool nextDown);
435
436 /// @}
437
438 /// \name Sign operations.
439 /// @{
440
441 void changeSign();
442
443 /// @}
444
445 /// \name Conversions
446 /// @{
447
448 opStatus convert(const fltSemantics &, roundingMode, bool *);
450 roundingMode, bool *) const;
453 bool, roundingMode);
455 bool, roundingMode);
457 APInt bitcastToAPInt() const;
458 double convertToDouble() const;
459#ifdef HAS_IEE754_FLOAT128
460 float128 convertToQuad() const;
461#endif
462 float convertToFloat() const;
463
464 /// @}
465
466 /// The definition of equality is not straightforward for floating point, so
467 /// we won't use operator==. Use one of the following, or write whatever it
468 /// is you really mean.
469 bool operator==(const IEEEFloat &) const = delete;
470
471 /// IEEE comparison with another floating point number (NaNs compare
472 /// unordered, 0==-0).
473 cmpResult compare(const IEEEFloat &) const;
474
475 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
476 bool bitwiseIsEqual(const IEEEFloat &) const;
477
478 /// Write out a hexadecimal representation of the floating point value to DST,
479 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
480 /// Return the number of characters written, excluding the terminating NUL.
481 unsigned int convertToHexString(char *dst, unsigned int hexDigits,
482 bool upperCase, roundingMode) const;
483
484 /// \name IEEE-754R 5.7.2 General operations.
485 /// @{
486
487 /// IEEE-754R isSignMinus: Returns true if and only if the current value is
488 /// negative.
489 ///
490 /// This applies to zeros and NaNs as well.
491 bool isNegative() const { return sign; }
492
493 /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
494 ///
495 /// This implies that the current value of the float is not zero, subnormal,
496 /// infinite, or NaN following the definition of normality from IEEE-754R.
497 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
498
499 /// Returns true if and only if the current value is zero, subnormal, or
500 /// normal.
501 ///
502 /// This means that the value is not infinite or NaN.
503 bool isFinite() const { return !isNaN() && !isInfinity(); }
504
505 /// Returns true if and only if the float is plus or minus zero.
506 bool isZero() const { return category == fltCategory::fcZero; }
507
508 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
509 /// denormal.
510 bool isDenormal() const;
511
512 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
513 bool isInfinity() const { return category == fcInfinity; }
514
515 /// Returns true if and only if the float is a quiet or signaling NaN.
516 bool isNaN() const { return category == fcNaN; }
517
518 /// Returns true if and only if the float is a signaling NaN.
519 bool isSignaling() const;
520
521 /// @}
522
523 /// \name Simple Queries
524 /// @{
525
526 fltCategory getCategory() const { return category; }
527 const fltSemantics &getSemantics() const { return *semantics; }
528 bool isNonZero() const { return category != fltCategory::fcZero; }
529 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
530 bool isPosZero() const { return isZero() && !isNegative(); }
531 bool isNegZero() const { return isZero() && isNegative(); }
532
533 /// Returns true if and only if the number has the smallest possible non-zero
534 /// magnitude in the current semantics.
535 bool isSmallest() const;
536
537 /// Returns true if this is the smallest (by magnitude) normalized finite
538 /// number in the given semantics.
539 bool isSmallestNormalized() const;
540
541 /// Returns true if and only if the number has the largest possible finite
542 /// magnitude in the current semantics.
543 bool isLargest() const;
544
545 /// Returns true if and only if the number is an exact integer.
546 bool isInteger() const;
547
548 /// @}
549
550 IEEEFloat &operator=(const IEEEFloat &);
552
553 /// Overload to compute a hash code for an APFloat value.
554 ///
555 /// Note that the use of hash codes for floating point values is in general
556 /// frought with peril. Equality is hard to define for these values. For
557 /// example, should negative and positive zero hash to different codes? Are
558 /// they equal or not? This hash value implementation specifically
559 /// emphasizes producing different codes for different inputs in order to
560 /// be used in canonicalization and memoization. As such, equality is
561 /// bitwiseIsEqual, and 0 != -0.
562 friend hash_code hash_value(const IEEEFloat &Arg);
563
564 /// Converts this value into a decimal string.
565 ///
566 /// \param FormatPrecision The maximum number of digits of
567 /// precision to output. If there are fewer digits available,
568 /// zero padding will not be used unless the value is
569 /// integral and small enough to be expressed in
570 /// FormatPrecision digits. 0 means to use the natural
571 /// precision of the number.
572 /// \param FormatMaxPadding The maximum number of zeros to
573 /// consider inserting before falling back to scientific
574 /// notation. 0 means to always use scientific notation.
575 ///
576 /// \param TruncateZero Indicate whether to remove the trailing zero in
577 /// fraction part or not. Also setting this parameter to false forcing
578 /// producing of output more similar to default printf behavior.
579 /// Specifically the lower e is used as exponent delimiter and exponent
580 /// always contains no less than two digits.
581 ///
582 /// Number Precision MaxPadding Result
583 /// ------ --------- ---------- ------
584 /// 1.01E+4 5 2 10100
585 /// 1.01E+4 4 2 1.01E+4
586 /// 1.01E+4 5 1 1.01E+4
587 /// 1.01E-2 5 2 0.0101
588 /// 1.01E-2 4 2 0.0101
589 /// 1.01E-2 4 1 1.01E-2
590 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
591 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const;
592
593 /// If this value has an exact multiplicative inverse, store it in inv and
594 /// return true.
595 bool getExactInverse(APFloat *inv) const;
596
597 // If this is an exact power of two, return the exponent while ignoring the
598 // sign bit. If it's not an exact power of 2, return INT_MIN
600 int getExactLog2Abs() const;
601
602 // If this is an exact power of two, return the exponent. If it's not an exact
603 // power of 2, return INT_MIN
605 int getExactLog2() const {
606 return isNegative() ? INT_MIN : getExactLog2Abs();
607 }
608
609 /// Returns the exponent of the internal representation of the APFloat.
610 ///
611 /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
612 /// For special APFloat values, this returns special error codes:
613 ///
614 /// NaN -> \c IEK_NaN
615 /// 0 -> \c IEK_Zero
616 /// Inf -> \c IEK_Inf
617 ///
618 friend int ilogb(const IEEEFloat &Arg);
619
620 /// Returns: X * 2^Exp for integral exponents.
621 friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
622
623 friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
624
625 /// \name Special value setters.
626 /// @{
627
628 void makeLargest(bool Neg = false);
629 void makeSmallest(bool Neg = false);
630 void makeNaN(bool SNaN = false, bool Neg = false,
631 const APInt *fill = nullptr);
632 void makeInf(bool Neg = false);
633 void makeZero(bool Neg = false);
634 void makeQuiet();
635
636 /// Returns the smallest (by magnitude) normalized finite number in the given
637 /// semantics.
638 ///
639 /// \param Negative - True iff the number should be negative
640 void makeSmallestNormalized(bool Negative = false);
641
642 /// @}
643
645
646private:
647 /// \name Simple Queries
648 /// @{
649
650 integerPart *significandParts();
651 const integerPart *significandParts() const;
652 unsigned int partCount() const;
653
654 /// @}
655
656 /// \name Significand operations.
657 /// @{
658
659 integerPart addSignificand(const IEEEFloat &);
660 integerPart subtractSignificand(const IEEEFloat &, integerPart);
661 lostFraction addOrSubtractSignificand(const IEEEFloat &, bool subtract);
662 lostFraction multiplySignificand(const IEEEFloat &, IEEEFloat,
663 bool ignoreAddend = false);
664 lostFraction multiplySignificand(const IEEEFloat&);
665 lostFraction divideSignificand(const IEEEFloat &);
666 void incrementSignificand();
667 void initialize(const fltSemantics *);
668 void shiftSignificandLeft(unsigned int);
669 lostFraction shiftSignificandRight(unsigned int);
670 unsigned int significandLSB() const;
671 unsigned int significandMSB() const;
672 void zeroSignificand();
673 unsigned int getNumHighBits() const;
674 /// Return true if the significand excluding the integral bit is all ones.
675 bool isSignificandAllOnes() const;
676 bool isSignificandAllOnesExceptLSB() const;
677 /// Return true if the significand excluding the integral bit is all zeros.
678 bool isSignificandAllZeros() const;
679 bool isSignificandAllZerosExceptMSB() const;
680
681 /// @}
682
683 /// \name Arithmetic on special values.
684 /// @{
685
686 opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract);
687 opStatus divideSpecials(const IEEEFloat &);
688 opStatus multiplySpecials(const IEEEFloat &);
689 opStatus modSpecials(const IEEEFloat &);
690 opStatus remainderSpecials(const IEEEFloat&);
691
692 /// @}
693
694 /// \name Miscellany
695 /// @{
696
697 bool convertFromStringSpecials(StringRef str);
699 opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
700 opStatus handleOverflow(roundingMode);
701 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
702 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
703 unsigned int, bool, roundingMode,
704 bool *) const;
705 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
707 Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode);
708 Expected<opStatus> convertFromDecimalString(StringRef, roundingMode);
709 char *convertNormalToHexString(char *, unsigned int, bool,
710 roundingMode) const;
711 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
713 ExponentType exponentNaN() const;
714 ExponentType exponentInf() const;
715 ExponentType exponentZero() const;
716
717 /// @}
718
719 template <const fltSemantics &S> APInt convertIEEEFloatToAPInt() const;
720 APInt convertHalfAPFloatToAPInt() const;
721 APInt convertBFloatAPFloatToAPInt() const;
722 APInt convertFloatAPFloatToAPInt() const;
723 APInt convertDoubleAPFloatToAPInt() const;
724 APInt convertQuadrupleAPFloatToAPInt() const;
725 APInt convertF80LongDoubleAPFloatToAPInt() const;
726 APInt convertPPCDoubleDoubleLegacyAPFloatToAPInt() const;
727 APInt convertFloat8E5M2APFloatToAPInt() const;
728 APInt convertFloat8E5M2FNUZAPFloatToAPInt() const;
729 APInt convertFloat8E4M3APFloatToAPInt() const;
730 APInt convertFloat8E4M3FNAPFloatToAPInt() const;
731 APInt convertFloat8E4M3FNUZAPFloatToAPInt() const;
732 APInt convertFloat8E4M3B11FNUZAPFloatToAPInt() const;
733 APInt convertFloat8E3M4APFloatToAPInt() const;
734 APInt convertFloatTF32APFloatToAPInt() const;
735 APInt convertFloat8E8M0FNUAPFloatToAPInt() const;
736 APInt convertFloat6E3M2FNAPFloatToAPInt() const;
737 APInt convertFloat6E2M3FNAPFloatToAPInt() const;
738 APInt convertFloat4E2M1FNAPFloatToAPInt() const;
739 void initFromAPInt(const fltSemantics *Sem, const APInt &api);
740 template <const fltSemantics &S> void initFromIEEEAPInt(const APInt &api);
741 void initFromHalfAPInt(const APInt &api);
742 void initFromBFloatAPInt(const APInt &api);
743 void initFromFloatAPInt(const APInt &api);
744 void initFromDoubleAPInt(const APInt &api);
745 void initFromQuadrupleAPInt(const APInt &api);
746 void initFromF80LongDoubleAPInt(const APInt &api);
747 void initFromPPCDoubleDoubleLegacyAPInt(const APInt &api);
748 void initFromFloat8E5M2APInt(const APInt &api);
749 void initFromFloat8E5M2FNUZAPInt(const APInt &api);
750 void initFromFloat8E4M3APInt(const APInt &api);
751 void initFromFloat8E4M3FNAPInt(const APInt &api);
752 void initFromFloat8E4M3FNUZAPInt(const APInt &api);
753 void initFromFloat8E4M3B11FNUZAPInt(const APInt &api);
754 void initFromFloat8E3M4APInt(const APInt &api);
755 void initFromFloatTF32APInt(const APInt &api);
756 void initFromFloat8E8M0FNUAPInt(const APInt &api);
757 void initFromFloat6E3M2FNAPInt(const APInt &api);
758 void initFromFloat6E2M3FNAPInt(const APInt &api);
759 void initFromFloat4E2M1FNAPInt(const APInt &api);
760
761 void assign(const IEEEFloat &);
762 void copySignificand(const IEEEFloat &);
763 void freeSignificand();
764
765 /// Note: this must be the first data member.
766 /// The semantics that this value obeys.
767 const fltSemantics *semantics;
768
769 /// A binary fraction with an explicit integer bit.
770 ///
771 /// The significand must be at least one bit wider than the target precision.
772 union Significand {
773 integerPart part;
774 integerPart *parts;
775 } significand;
776
777 /// The signed unbiased exponent of the value.
778 ExponentType exponent;
779
780 /// What kind of floating point number this is.
781 ///
782 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
783 /// Using the extra bit keeps it from failing under VisualStudio.
784 fltCategory category : 3;
785
786 /// Sign bit of the number.
787 unsigned int sign : 1;
788};
789
790hash_code hash_value(const IEEEFloat &Arg);
791int ilogb(const IEEEFloat &Arg);
792IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
793IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM);
794
795// This mode implements more precise float in terms of two APFloats.
796// The interface and layout is designed for arbitrary underlying semantics,
797// though currently only PPCDoubleDouble semantics are supported, whose
798// corresponding underlying semantics are IEEEdouble.
799class DoubleAPFloat final {
800 // Note: this must be the first data member.
801 const fltSemantics *Semantics;
802 std::unique_ptr<APFloat[]> Floats;
803
804 opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c,
805 const APFloat &cc, roundingMode RM);
806
807 opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS,
808 DoubleAPFloat &Out, roundingMode RM);
809
810public:
811 DoubleAPFloat(const fltSemantics &S);
814 DoubleAPFloat(const fltSemantics &S, const APInt &I);
815 DoubleAPFloat(const fltSemantics &S, APFloat &&First, APFloat &&Second);
818
821
822 bool needsCleanup() const { return Floats != nullptr; }
823
824 inline APFloat &getFirst();
825 inline const APFloat &getFirst() const;
826 inline APFloat &getSecond();
827 inline const APFloat &getSecond() const;
828
835 opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
836 const DoubleAPFloat &Addend, roundingMode RM);
838 void changeSign();
840
841 fltCategory getCategory() const;
842 bool isNegative() const;
843
844 void makeInf(bool Neg);
845 void makeZero(bool Neg);
846 void makeLargest(bool Neg);
847 void makeSmallest(bool Neg);
848 void makeSmallestNormalized(bool Neg);
849 void makeNaN(bool SNaN, bool Neg, const APInt *fill);
850
851 cmpResult compare(const DoubleAPFloat &RHS) const;
852 bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
853 APInt bitcastToAPInt() const;
855 opStatus next(bool nextDown);
856
858 unsigned int Width, bool IsSigned, roundingMode RM,
859 bool *IsExact) const;
860 opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM);
862 unsigned int InputSize, bool IsSigned,
863 roundingMode RM);
865 unsigned int InputSize, bool IsSigned,
866 roundingMode RM);
867 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
868 bool UpperCase, roundingMode RM) const;
869
870 bool isDenormal() const;
871 bool isSmallest() const;
872 bool isSmallestNormalized() const;
873 bool isLargest() const;
874 bool isInteger() const;
875
876 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
877 unsigned FormatMaxPadding, bool TruncateZero = true) const;
878
879 bool getExactInverse(APFloat *inv) const;
880
882 int getExactLog2() const;
884 int getExactLog2Abs() const;
885
887 friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode);
888 friend hash_code hash_value(const DoubleAPFloat &Arg);
889};
890
892DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp, roundingMode RM);
894
895} // End detail namespace
896
897// This is a interface class that is currently forwarding functionalities from
898// detail::IEEEFloat.
899class APFloat : public APFloatBase {
902
903 static_assert(std::is_standard_layout<IEEEFloat>::value);
904
905 union Storage {
906 const fltSemantics *semantics;
908 DoubleAPFloat Double;
909
910 explicit Storage(IEEEFloat F, const fltSemantics &S);
911 explicit Storage(DoubleAPFloat F, const fltSemantics &S)
912 : Double(std::move(F)) {
913 assert(&S == &PPCDoubleDouble());
914 }
915
916 template <typename... ArgTypes>
917 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
918 if (usesLayout<IEEEFloat>(Semantics)) {
919 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
920 return;
921 }
922 if (usesLayout<DoubleAPFloat>(Semantics)) {
923 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
924 return;
925 }
926 llvm_unreachable("Unexpected semantics");
927 }
928
929 ~Storage() {
930 if (usesLayout<IEEEFloat>(*semantics)) {
931 IEEE.~IEEEFloat();
932 return;
933 }
934 if (usesLayout<DoubleAPFloat>(*semantics)) {
935 Double.~DoubleAPFloat();
936 return;
937 }
938 llvm_unreachable("Unexpected semantics");
939 }
940
941 Storage(const Storage &RHS) {
942 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
943 new (this) IEEEFloat(RHS.IEEE);
944 return;
945 }
946 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
947 new (this) DoubleAPFloat(RHS.Double);
948 return;
949 }
950 llvm_unreachable("Unexpected semantics");
951 }
952
953 Storage(Storage &&RHS) {
954 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
955 new (this) IEEEFloat(std::move(RHS.IEEE));
956 return;
957 }
958 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
959 new (this) DoubleAPFloat(std::move(RHS.Double));
960 return;
961 }
962 llvm_unreachable("Unexpected semantics");
963 }
964
965 Storage &operator=(const Storage &RHS) {
966 if (usesLayout<IEEEFloat>(*semantics) &&
967 usesLayout<IEEEFloat>(*RHS.semantics)) {
968 IEEE = RHS.IEEE;
969 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
970 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
971 Double = RHS.Double;
972 } else if (this != &RHS) {
973 this->~Storage();
974 new (this) Storage(RHS);
975 }
976 return *this;
977 }
978
979 Storage &operator=(Storage &&RHS) {
980 if (usesLayout<IEEEFloat>(*semantics) &&
981 usesLayout<IEEEFloat>(*RHS.semantics)) {
982 IEEE = std::move(RHS.IEEE);
983 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
984 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
985 Double = std::move(RHS.Double);
986 } else if (this != &RHS) {
987 this->~Storage();
988 new (this) Storage(std::move(RHS));
989 }
990 return *this;
991 }
992 } U;
993
994 template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
995 static_assert(std::is_same<T, IEEEFloat>::value ||
996 std::is_same<T, DoubleAPFloat>::value);
997 if (std::is_same<T, DoubleAPFloat>::value) {
998 return &Semantics == &PPCDoubleDouble();
999 }
1000 return &Semantics != &PPCDoubleDouble();
1001 }
1002
1003 IEEEFloat &getIEEE() {
1004 if (usesLayout<IEEEFloat>(*U.semantics))
1005 return U.IEEE;
1006 if (usesLayout<DoubleAPFloat>(*U.semantics))
1007 return U.Double.getFirst().U.IEEE;
1008 llvm_unreachable("Unexpected semantics");
1009 }
1010
1011 const IEEEFloat &getIEEE() const {
1012 if (usesLayout<IEEEFloat>(*U.semantics))
1013 return U.IEEE;
1014 if (usesLayout<DoubleAPFloat>(*U.semantics))
1015 return U.Double.getFirst().U.IEEE;
1016 llvm_unreachable("Unexpected semantics");
1017 }
1018
1019 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
1020
1021 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
1022
1023 void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
1024 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
1025 }
1026
1027 void makeLargest(bool Neg) {
1028 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
1029 }
1030
1031 void makeSmallest(bool Neg) {
1032 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
1033 }
1034
1035 void makeSmallestNormalized(bool Neg) {
1036 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
1037 }
1038
1039 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
1040 explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
1041 : U(std::move(F), S) {}
1042
1043 cmpResult compareAbsoluteValue(const APFloat &RHS) const {
1044 assert(&getSemantics() == &RHS.getSemantics() &&
1045 "Should only compare APFloats with the same semantics");
1046 if (usesLayout<IEEEFloat>(getSemantics()))
1047 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
1048 if (usesLayout<DoubleAPFloat>(getSemantics()))
1049 return U.Double.compareAbsoluteValue(RHS.U.Double);
1050 llvm_unreachable("Unexpected semantics");
1051 }
1052
1053public:
1057 template <typename T,
1058 typename = std::enable_if_t<std::is_floating_point<T>::value>>
1059 APFloat(const fltSemantics &Semantics, T V) = delete;
1060 // TODO: Remove this constructor. This isn't faster than the first one.
1062 : U(Semantics, uninitialized) {}
1064 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
1065 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
1066 APFloat(const APFloat &RHS) = default;
1067 APFloat(APFloat &&RHS) = default;
1068
1069 ~APFloat() = default;
1070
1072
1073 /// Factory for Positive and Negative Zero.
1074 ///
1075 /// \param Negative True iff the number should be negative.
1076 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
1077 APFloat Val(Sem, uninitialized);
1078 Val.makeZero(Negative);
1079 return Val;
1080 }
1081
1082 /// Factory for Positive and Negative One.
1083 ///
1084 /// \param Negative True iff the number should be negative.
1085 static APFloat getOne(const fltSemantics &Sem, bool Negative = false) {
1086 APFloat Val(Sem, 1U);
1087 if (Negative)
1088 Val.changeSign();
1089 return Val;
1090 }
1091
1092 /// Factory for Positive and Negative Infinity.
1093 ///
1094 /// \param Negative True iff the number should be negative.
1095 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
1096 APFloat Val(Sem, uninitialized);
1097 Val.makeInf(Negative);
1098 return Val;
1099 }
1100
1101 /// Factory for NaN values.
1102 ///
1103 /// \param Negative - True iff the NaN generated should be negative.
1104 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
1105 /// default. The value is truncated as necessary.
1106 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
1107 uint64_t payload = 0) {
1108 if (payload) {
1109 APInt intPayload(64, payload);
1110 return getQNaN(Sem, Negative, &intPayload);
1111 } else {
1112 return getQNaN(Sem, Negative, nullptr);
1113 }
1114 }
1115
1116 /// Factory for QNaN values.
1117 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
1118 const APInt *payload = nullptr) {
1119 APFloat Val(Sem, uninitialized);
1120 Val.makeNaN(false, Negative, payload);
1121 return Val;
1122 }
1123
1124 /// Factory for SNaN values.
1125 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
1126 const APInt *payload = nullptr) {
1127 APFloat Val(Sem, uninitialized);
1128 Val.makeNaN(true, Negative, payload);
1129 return Val;
1130 }
1131
1132 /// Returns the largest finite number in the given semantics.
1133 ///
1134 /// \param Negative - True iff the number should be negative
1135 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
1136 APFloat Val(Sem, uninitialized);
1137 Val.makeLargest(Negative);
1138 return Val;
1139 }
1140
1141 /// Returns the smallest (by magnitude) finite number in the given semantics.
1142 /// Might be denormalized, which implies a relative loss of precision.
1143 ///
1144 /// \param Negative - True iff the number should be negative
1145 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
1146 APFloat Val(Sem, uninitialized);
1147 Val.makeSmallest(Negative);
1148 return Val;
1149 }
1150
1151 /// Returns the smallest (by magnitude) normalized finite number in the given
1152 /// semantics.
1153 ///
1154 /// \param Negative - True iff the number should be negative
1156 bool Negative = false) {
1157 APFloat Val(Sem, uninitialized);
1158 Val.makeSmallestNormalized(Negative);
1159 return Val;
1160 }
1161
1162 /// Returns a float which is bitcasted from an all one value int.
1163 ///
1164 /// \param Semantics - type float semantics
1166
1167 /// Returns true if the given semantics has actual significand.
1168 ///
1169 /// \param Sem - type float semantics
1170 static bool hasSignificand(const fltSemantics &Sem) {
1171 return &Sem != &Float8E8M0FNU();
1172 }
1173
1174 /// Used to insert APFloat objects, or objects that contain APFloat objects,
1175 /// into FoldingSets.
1176 void Profile(FoldingSetNodeID &NID) const;
1177
1179 assert(&getSemantics() == &RHS.getSemantics() &&
1180 "Should only call on two APFloats with the same semantics");
1181 if (usesLayout<IEEEFloat>(getSemantics()))
1182 return U.IEEE.add(RHS.U.IEEE, RM);
1183 if (usesLayout<DoubleAPFloat>(getSemantics()))
1184 return U.Double.add(RHS.U.Double, RM);
1185 llvm_unreachable("Unexpected semantics");
1186 }
1188 assert(&getSemantics() == &RHS.getSemantics() &&
1189 "Should only call on two APFloats with the same semantics");
1190 if (usesLayout<IEEEFloat>(getSemantics()))
1191 return U.IEEE.subtract(RHS.U.IEEE, RM);
1192 if (usesLayout<DoubleAPFloat>(getSemantics()))
1193 return U.Double.subtract(RHS.U.Double, RM);
1194 llvm_unreachable("Unexpected semantics");
1195 }
1197 assert(&getSemantics() == &RHS.getSemantics() &&
1198 "Should only call on two APFloats with the same semantics");
1199 if (usesLayout<IEEEFloat>(getSemantics()))
1200 return U.IEEE.multiply(RHS.U.IEEE, RM);
1201 if (usesLayout<DoubleAPFloat>(getSemantics()))
1202 return U.Double.multiply(RHS.U.Double, RM);
1203 llvm_unreachable("Unexpected semantics");
1204 }
1206 assert(&getSemantics() == &RHS.getSemantics() &&
1207 "Should only call on two APFloats with the same semantics");
1208 if (usesLayout<IEEEFloat>(getSemantics()))
1209 return U.IEEE.divide(RHS.U.IEEE, RM);
1210 if (usesLayout<DoubleAPFloat>(getSemantics()))
1211 return U.Double.divide(RHS.U.Double, RM);
1212 llvm_unreachable("Unexpected semantics");
1213 }
1215 assert(&getSemantics() == &RHS.getSemantics() &&
1216 "Should only call on two APFloats with the same semantics");
1217 if (usesLayout<IEEEFloat>(getSemantics()))
1218 return U.IEEE.remainder(RHS.U.IEEE);
1219 if (usesLayout<DoubleAPFloat>(getSemantics()))
1220 return U.Double.remainder(RHS.U.Double);
1221 llvm_unreachable("Unexpected semantics");
1222 }
1224 assert(&getSemantics() == &RHS.getSemantics() &&
1225 "Should only call on two APFloats with the same semantics");
1226 if (usesLayout<IEEEFloat>(getSemantics()))
1227 return U.IEEE.mod(RHS.U.IEEE);
1228 if (usesLayout<DoubleAPFloat>(getSemantics()))
1229 return U.Double.mod(RHS.U.Double);
1230 llvm_unreachable("Unexpected semantics");
1231 }
1232 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
1233 roundingMode RM) {
1234 assert(&getSemantics() == &Multiplicand.getSemantics() &&
1235 "Should only call on APFloats with the same semantics");
1236 assert(&getSemantics() == &Addend.getSemantics() &&
1237 "Should only call on APFloats with the same semantics");
1238 if (usesLayout<IEEEFloat>(getSemantics()))
1239 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1240 if (usesLayout<DoubleAPFloat>(getSemantics()))
1241 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1242 RM);
1243 llvm_unreachable("Unexpected semantics");
1244 }
1247 }
1248
1249 // TODO: bool parameters are not readable and a source of bugs.
1250 // Do something.
1251 opStatus next(bool nextDown) {
1253 }
1254
1255 /// Negate an APFloat.
1257 APFloat Result(*this);
1258 Result.changeSign();
1259 return Result;
1260 }
1261
1262 /// Add two APFloats, rounding ties to the nearest even.
1263 /// No error checking.
1265 APFloat Result(*this);
1266 (void)Result.add(RHS, rmNearestTiesToEven);
1267 return Result;
1268 }
1269
1270 /// Subtract two APFloats, rounding ties to the nearest even.
1271 /// No error checking.
1273 APFloat Result(*this);
1274 (void)Result.subtract(RHS, rmNearestTiesToEven);
1275 return Result;
1276 }
1277
1278 /// Multiply two APFloats, rounding ties to the nearest even.
1279 /// No error checking.
1281 APFloat Result(*this);
1282 (void)Result.multiply(RHS, rmNearestTiesToEven);
1283 return Result;
1284 }
1285
1286 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1287 /// No error checking.
1289 APFloat Result(*this);
1290 (void)Result.divide(RHS, rmNearestTiesToEven);
1291 return Result;
1292 }
1293
1295 void clearSign() {
1296 if (isNegative())
1297 changeSign();
1298 }
1299 void copySign(const APFloat &RHS) {
1300 if (isNegative() != RHS.isNegative())
1301 changeSign();
1302 }
1303
1304 /// A static helper to produce a copy of an APFloat value with its sign
1305 /// copied from some other APFloat.
1306 static APFloat copySign(APFloat Value, const APFloat &Sign) {
1307 Value.copySign(Sign);
1308 return Value;
1309 }
1310
1311 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
1312 /// This preserves the sign and payload bits.
1314 APFloat Result(*this);
1315 Result.getIEEE().makeQuiet();
1316 return Result;
1317 }
1318
1319 opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
1320 bool *losesInfo);
1322 unsigned int Width, bool IsSigned, roundingMode RM,
1323 bool *IsExact) const {
1325 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1326 }
1328 bool *IsExact) const;
1329 opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
1330 roundingMode RM) {
1331 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input, IsSigned, RM));
1332 }
1334 unsigned int InputSize, bool IsSigned,
1335 roundingMode RM) {
1337 convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM));
1338 }
1340 unsigned int InputSize, bool IsSigned,
1341 roundingMode RM) {
1343 convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
1344 }
1348 }
1349
1350 /// Converts this APFloat to host double value.
1351 ///
1352 /// \pre The APFloat must be built using semantics, that can be represented by
1353 /// the host double type without loss of precision. It can be IEEEdouble and
1354 /// shorter semantics, like IEEEsingle and others.
1355 double convertToDouble() const;
1356
1357 /// Converts this APFloat to host float value.
1358 ///
1359 /// \pre The APFloat must be built using semantics, that can be represented by
1360 /// the host float type without loss of precision. It can be IEEEquad and
1361 /// shorter semantics, like IEEEdouble and others.
1362#ifdef HAS_IEE754_FLOAT128
1363 float128 convertToQuad() const;
1364#endif
1365
1366 /// Converts this APFloat to host float value.
1367 ///
1368 /// \pre The APFloat must be built using semantics, that can be represented by
1369 /// the host float type without loss of precision. It can be IEEEsingle and
1370 /// shorter semantics, like IEEEhalf.
1371 float convertToFloat() const;
1372
1373 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
1374
1375 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; }
1376
1377 bool operator<(const APFloat &RHS) const {
1378 return compare(RHS) == cmpLessThan;
1379 }
1380
1381 bool operator>(const APFloat &RHS) const {
1382 return compare(RHS) == cmpGreaterThan;
1383 }
1384
1385 bool operator<=(const APFloat &RHS) const {
1386 cmpResult Res = compare(RHS);
1387 return Res == cmpLessThan || Res == cmpEqual;
1388 }
1389
1390 bool operator>=(const APFloat &RHS) const {
1391 cmpResult Res = compare(RHS);
1392 return Res == cmpGreaterThan || Res == cmpEqual;
1393 }
1394
1396 assert(&getSemantics() == &RHS.getSemantics() &&
1397 "Should only compare APFloats with the same semantics");
1398 if (usesLayout<IEEEFloat>(getSemantics()))
1399 return U.IEEE.compare(RHS.U.IEEE);
1400 if (usesLayout<DoubleAPFloat>(getSemantics()))
1401 return U.Double.compare(RHS.U.Double);
1402 llvm_unreachable("Unexpected semantics");
1403 }
1404
1405 bool bitwiseIsEqual(const APFloat &RHS) const {
1406 if (&getSemantics() != &RHS.getSemantics())
1407 return false;
1408 if (usesLayout<IEEEFloat>(getSemantics()))
1409 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1410 if (usesLayout<DoubleAPFloat>(getSemantics()))
1411 return U.Double.bitwiseIsEqual(RHS.U.Double);
1412 llvm_unreachable("Unexpected semantics");
1413 }
1414
1415 /// We don't rely on operator== working on double values, as
1416 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1417 /// As such, this method can be used to do an exact bit-for-bit comparison of
1418 /// two floating point values.
1419 ///
1420 /// We leave the version with the double argument here because it's just so
1421 /// convenient to write "2.0" and the like. Without this function we'd
1422 /// have to duplicate its logic everywhere it's called.
1423 bool isExactlyValue(double V) const {
1424 bool ignored;
1425 APFloat Tmp(V);
1427 return bitwiseIsEqual(Tmp);
1428 }
1429
1430 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
1431 bool UpperCase, roundingMode RM) const {
1433 convertToHexString(DST, HexDigits, UpperCase, RM));
1434 }
1435
1436 bool isZero() const { return getCategory() == fcZero; }
1437 bool isInfinity() const { return getCategory() == fcInfinity; }
1438 bool isNaN() const { return getCategory() == fcNaN; }
1439
1440 bool isNegative() const { return getIEEE().isNegative(); }
1442 bool isSignaling() const { return getIEEE().isSignaling(); }
1443
1444 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
1445 bool isFinite() const { return !isNaN() && !isInfinity(); }
1446
1447 fltCategory getCategory() const { return getIEEE().getCategory(); }
1448 const fltSemantics &getSemantics() const { return *U.semantics; }
1449 bool isNonZero() const { return !isZero(); }
1450 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
1451 bool isPosZero() const { return isZero() && !isNegative(); }
1452 bool isNegZero() const { return isZero() && isNegative(); }
1453 bool isPosInfinity() const { return isInfinity() && !isNegative(); }
1454 bool isNegInfinity() const { return isInfinity() && isNegative(); }
1458 bool isIEEE() const { return usesLayout<IEEEFloat>(getSemantics()); }
1459
1462 }
1463
1464 /// Return the FPClassTest which will return true for the value.
1465 FPClassTest classify() const;
1466
1467 APFloat &operator=(const APFloat &RHS) = default;
1469
1470 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
1471 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
1473 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1474 }
1475
1476 void print(raw_ostream &) const;
1477 void dump() const;
1478
1479 bool getExactInverse(APFloat *inv) const {
1481 }
1482
1484 int getExactLog2Abs() const {
1486 }
1487
1489 int getExactLog2() const {
1491 }
1492
1493 friend hash_code hash_value(const APFloat &Arg);
1494 friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); }
1495 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
1496 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
1499};
1500
1501static_assert(sizeof(APFloat) == sizeof(detail::IEEEFloat),
1502 "Empty base class optimization is not performed.");
1503
1504/// See friend declarations above.
1505///
1506/// These additional declarations are required in order to compile LLVM with IBM
1507/// xlC compiler.
1508hash_code hash_value(const APFloat &Arg);
1510 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1511 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
1512 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1513 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
1514 llvm_unreachable("Unexpected semantics");
1515}
1516
1517/// Equivalent of C standard library function.
1518///
1519/// While the C standard says Exp is an unspecified value for infinity and nan,
1520/// this returns INT_MAX for infinities, and INT_MIN for NaNs.
1521inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
1522 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1523 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
1524 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1525 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
1526 llvm_unreachable("Unexpected semantics");
1527}
1528/// Returns the absolute value of the argument.
1530 X.clearSign();
1531 return X;
1532}
1533
1534/// Returns the negated value of the argument.
1536 X.changeSign();
1537 return X;
1538}
1539
1540/// Implements IEEE-754 2019 minimumNumber semantics. Returns the smaller of the
1541/// 2 arguments if both are not NaN. If either argument is a NaN, returns the
1542/// other argument. -0 is treated as ordered less than +0.
1544inline APFloat minnum(const APFloat &A, const APFloat &B) {
1545 if (A.isNaN())
1546 return B;
1547 if (B.isNaN())
1548 return A;
1549 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1550 return A.isNegative() ? A : B;
1551 return B < A ? B : A;
1552}
1553
1554/// Implements IEEE-754 2019 maximumNumber semantics. Returns the larger of the
1555/// 2 arguments if both are not NaN. If either argument is a NaN, returns the
1556/// other argument. +0 is treated as ordered greater than -0.
1558inline APFloat maxnum(const APFloat &A, const APFloat &B) {
1559 if (A.isNaN())
1560 return B;
1561 if (B.isNaN())
1562 return A;
1563 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1564 return A.isNegative() ? B : A;
1565 return A < B ? B : A;
1566}
1567
1568/// Implements IEEE 754-2019 minimum semantics. Returns the smaller of 2
1569/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1570/// as less than +0.
1572inline APFloat minimum(const APFloat &A, const APFloat &B) {
1573 if (A.isNaN())
1574 return A.makeQuiet();
1575 if (B.isNaN())
1576 return B.makeQuiet();
1577 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1578 return A.isNegative() ? A : B;
1579 return B < A ? B : A;
1580}
1581
1582/// Implements IEEE 754-2019 minimumNumber semantics. Returns the smaller
1583/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1585inline APFloat minimumnum(const APFloat &A, const APFloat &B) {
1586 if (A.isNaN())
1587 return B.isNaN() ? B.makeQuiet() : B;
1588 if (B.isNaN())
1589 return A;
1590 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1591 return A.isNegative() ? A : B;
1592 return B < A ? B : A;
1593}
1594
1595/// Implements IEEE 754-2019 maximum semantics. Returns the larger of 2
1596/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1597/// as less than +0.
1599inline APFloat maximum(const APFloat &A, const APFloat &B) {
1600 if (A.isNaN())
1601 return A.makeQuiet();
1602 if (B.isNaN())
1603 return B.makeQuiet();
1604 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1605 return A.isNegative() ? B : A;
1606 return A < B ? B : A;
1607}
1608
1609/// Implements IEEE 754-2019 maximumNumber semantics. Returns the larger
1610/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1612inline APFloat maximumnum(const APFloat &A, const APFloat &B) {
1613 if (A.isNaN())
1614 return B.isNaN() ? B.makeQuiet() : B;
1615 if (B.isNaN())
1616 return A;
1617 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1618 return A.isNegative() ? B : A;
1619 return A < B ? B : A;
1620}
1621
1623 V.print(OS);
1624 return OS;
1625}
1626
1627// We want the following functions to be available in the header for inlining.
1628// We cannot define them inline in the class definition of `DoubleAPFloat`
1629// because doing so would instantiate `std::unique_ptr<APFloat[]>` before
1630// `APFloat` is defined, and that would be undefined behavior.
1631namespace detail {
1632
1634 if (this != &RHS) {
1635 this->~DoubleAPFloat();
1636 new (this) DoubleAPFloat(std::move(RHS));
1637 }
1638 return *this;
1639}
1640
1641APFloat &DoubleAPFloat::getFirst() { return Floats[0]; }
1642const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; }
1643APFloat &DoubleAPFloat::getSecond() { return Floats[1]; }
1644const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; }
1645
1646} // namespace detail
1647
1648} // namespace llvm
1649
1650#undef APFLOAT_DISPATCH_ON_SEMANTICS
1651#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:1117
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition: APFloat.h:1125
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1205
bool getExactInverse(APFloat *inv) const
Definition: APFloat.h:1479
APFloat & operator=(APFloat &&RHS)=default
bool isFiniteNonZero() const
Definition: APFloat.h:1450
APFloat(const APFloat &RHS)=default
void copySign(const APFloat &RHS)
Definition: APFloat.h:1299
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5465
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.h:1484
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1187
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1405
bool isNegative() const
Definition: APFloat.h:1440
~APFloat()=default
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1264
friend DoubleAPFloat
Definition: APFloat.h:1498
double convertToDouble() const
Converts this APFloat to host double value.
Definition: APFloat.cpp:5527
bool isPosInfinity() const
Definition: APFloat.h:1453
APFloat(APFloat &&RHS)=default
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition: APFloat.h:1470
bool isNormal() const
Definition: APFloat.h:1444
bool isDenormal() const
Definition: APFloat.h:1441
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:1423
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1178
LLVM_READONLY int getExactLog2() const
Definition: APFloat.h:1489
APFloat(double d)
Definition: APFloat.h:1064
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:5490
APFloat(const fltSemantics &Semantics, integerPart I)
Definition: APFloat.h:1056
bool operator!=(const APFloat &RHS) const
Definition: APFloat.h:1375
APFloat(const fltSemantics &Semantics, T V)=delete
const fltSemantics & getSemantics() const
Definition: APFloat.h:1448
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1272
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1280
APFloat(const fltSemantics &Semantics)
Definition: APFloat.h:1054
bool isNonZero() const
Definition: APFloat.h:1449
void clearSign()
Definition: APFloat.h:1295
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1333
bool operator<(const APFloat &RHS) const
Definition: APFloat.h:1377
bool isFinite() const
Definition: APFloat.h:1445
APFloat makeQuiet() const
Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
Definition: APFloat.h:1313
bool isNaN() const
Definition: APFloat.h:1438
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1329
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
Definition: APFloat.h:1085
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition: APFloat.h:1430
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1196
float convertToFloat() const
Converts this APFloat to host float value.
Definition: APFloat.cpp:5555
bool isSignaling() const
Definition: APFloat.h:1442
bool operator>(const APFloat &RHS) const
Definition: APFloat.h:1381
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1232
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
Definition: APFloat.h:1288
opStatus remainder(const APFloat &RHS)
Definition: APFloat.h:1214
APFloat operator-() const
Negate an APFloat.
Definition: APFloat.h:1256
bool isZero() const
Definition: APFloat.h:1436
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1155
APInt bitcastToAPInt() const
Definition: APFloat.h:1346
bool isLargest() const
Definition: APFloat.h:1456
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
bool isSmallest() const
Definition: APFloat.h:1455
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1135
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1321
opStatus next(bool nextDown)
Definition: APFloat.h:1251
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1095
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
bool operator>=(const APFloat &RHS) const
Definition: APFloat.h:1390
bool needsCleanup() const
Definition: APFloat.h:1071
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition: APFloat.h:1145
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5452
bool operator==(const APFloat &RHS) const
Definition: APFloat.h:1373
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1223
bool isPosZero() const
Definition: APFloat.h:1451
friend int ilogb(const APFloat &Arg)
Definition: APFloat.h:1494
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:5432
fltCategory getCategory() const
Definition: APFloat.h:1447
APFloat(const fltSemantics &Semantics, uninitializedTag)
Definition: APFloat.h:1061
bool isInteger() const
Definition: APFloat.h:1457
bool isNegInfinity() const
Definition: APFloat.h:1454
friend IEEEFloat
Definition: APFloat.h:1497
void dump() const
Definition: APFloat.cpp:5501
bool isNegZero() const
Definition: APFloat.h:1452
void print(raw_ostream &) const
Definition: APFloat.cpp:5494
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:1306
APFloat(float f)
Definition: APFloat.h:1065
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1245
void changeSign()
Definition: APFloat.h:1294
friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:5437
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1106
static bool hasSignificand(const fltSemantics &Sem)
Returns true if the given semantics has actual significand.
Definition: APFloat.h:1170
bool isIEEE() const
Definition: APFloat.h:1458
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:1076
cmpResult compare(const APFloat &RHS) const
Definition: APFloat.h:1395
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1339
bool isSmallestNormalized() const
Definition: APFloat.h:1460
APFloat(const fltSemantics &Semantics, const APInt &I)
Definition: APFloat.h:1063
bool isInfinity() const
Definition: APFloat.h:1437
bool operator<=(const APFloat &RHS) const
Definition: APFloat.h:1385
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:5223
DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:4881
LLVM_READONLY int getExactLog2() const
Definition: APFloat.cpp:5388
opStatus remainder(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:5129
opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5033
fltCategory getCategory() const
Definition: APFloat.cpp:5193
bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:5244
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.cpp:5393
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.cpp:5290
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.cpp:5312
APInt bitcastToAPInt() const
Definition: APFloat.cpp:5255
bool getExactInverse(APFloat *inv) const
Definition: APFloat.cpp:5377
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:5264
opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5025
cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:5173
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.cpp:5282
void makeSmallest(bool Neg)
Definition: APFloat.cpp:5217
opStatus next(bool nextDown)
Definition: APFloat.cpp:5273
friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode)
opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5119
friend hash_code hash_value(const DoubleAPFloat &Arg)
Definition: APFloat.cpp:5249
bool isSmallestNormalized() const
Definition: APFloat.cpp:5346
opStatus mod(const DoubleAPFloat &RHS)
Definition: APFloat.cpp:5138
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
Definition: APFloat.cpp:5368
void makeLargest(bool Neg)
Definition: APFloat.cpp:5209
cmpResult compare(const DoubleAPFloat &RHS) const
Definition: APFloat.cpp:5236
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.cpp:5159
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.cpp:5301
opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
Definition: APFloat.cpp:5147
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition: APFloat.cpp:5322
bool needsCleanup() const
Definition: APFloat.h:822
opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
Definition: APFloat.cpp:5020
friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode)
void makeNaN(bool SNaN, bool Neg, const APInt *fill)
Definition: APFloat.cpp:5231
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:3346
cmpResult compareAbsoluteValue(const IEEEFloat &) const
Definition: APFloat.cpp:1534
opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
Definition: APFloat.cpp:2285
fltCategory getCategory() const
Definition: APFloat.h:526
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition: APFloat.cpp:2855
bool isNonZero() const
Definition: APFloat.h:528
bool isFiniteNonZero() const
Definition: APFloat.h:529
bool getExactInverse(APFloat *inv) const
If this value has an exact multiplicative inverse, store it in inv and return true.
Definition: APFloat.cpp:4519
bool needsCleanup() const
Returns whether this instance allocated memory.
Definition: APFloat.h:413
void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
Definition: APFloat.cpp:4125
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.cpp:4548
APInt bitcastToAPInt() const
Definition: APFloat.cpp:3754
cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition: APFloat.cpp:2456
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
Definition: APFloat.h:491
opStatus divide(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2155
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
Definition: APFloat.h:516
opStatus remainder(const IEEEFloat &)
IEEE remainder.
Definition: APFloat.cpp:2175
double convertToDouble() const
Definition: APFloat.cpp:3821
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
Definition: APFloat.cpp:2873
float convertToFloat() const
Definition: APFloat.cpp:3814
opStatus subtract(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2129
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:4476
void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
Definition: APFloat.cpp:4157
void makeInf(bool Neg=false)
Definition: APFloat.cpp:4738
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
Definition: APFloat.cpp:2899
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
Definition: APFloat.h:497
friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
Definition: APFloat.cpp:4812
bool isSmallestNormalized() const
Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.cpp:1034
bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
Definition: APFloat.cpp:1136
opStatus add(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2123
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
Definition: APFloat.h:503
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:3289
void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
Definition: APFloat.cpp:923
friend int ilogb(const IEEEFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition: APFloat.cpp:4773
opStatus multiply(const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2135
friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
Definition: APFloat.cpp:3495
opStatus roundToIntegral(roundingMode)
Definition: APFloat.cpp:2369
IEEEFloat & operator=(const IEEEFloat &)
Definition: APFloat.cpp:995
friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Returns: X * 2^Exp for integral exponents.
Definition: APFloat.cpp:4791
bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition: APFloat.cpp:1161
void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.cpp:4171
bool isInteger() const
Returns true if and only if the number is an exact integer.
Definition: APFloat.cpp:1153
bool isPosZero() const
Definition: APFloat.h:530
opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
Definition: APFloat.cpp:2323
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
Definition: APFloat.cpp:4593
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
Definition: APFloat.h:513
const fltSemantics & getSemantics() const
Definition: APFloat.h:527
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition: APFloat.h:506
bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
Definition: APFloat.cpp:4577
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:4753
LLVM_READONLY int getExactLog2() const
Definition: APFloat.h:605
bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
Definition: APFloat.cpp:1020
opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
Definition: APFloat.cpp:2795
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:1026
bool isNegZero() const
Definition: APFloat.h:531
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:389
APFloatBase::roundingMode roundingMode
Definition: APFloat.h:366
static constexpr fltCategory fcNaN
Definition: APFloat.h:391
static constexpr opStatus opDivByZero
Definition: APFloat.h:386
static constexpr opStatus opOverflow
Definition: APFloat.h:387
static constexpr cmpResult cmpLessThan
Definition: APFloat.h:381
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:377
static constexpr uninitializedTag uninitialized
Definition: APFloat.h:371
static constexpr fltCategory fcZero
Definition: APFloat.h:393
static constexpr opStatus opOK
Definition: APFloat.h:384
static constexpr cmpResult cmpGreaterThan
Definition: APFloat.h:382
static constexpr unsigned integerPartWidth
Definition: APFloat.h:379
APFloatBase::ExponentType ExponentType
Definition: APFloat.h:370
hash_code hash_value(const IEEEFloat &Arg)
Definition: APFloat.cpp:3495
static constexpr fltCategory fcNormal
Definition: APFloat.h:392
static constexpr opStatus opInvalidOp
Definition: APFloat.h:385
IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition: APFloat.cpp:4791
APFloatBase::fltCategory fltCategory
Definition: APFloat.h:369
IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM)
Definition: APFloat.cpp:4812
static constexpr cmpResult cmpUnordered
Definition: APFloat.h:383
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:376
static constexpr fltCategory fcInfinity
Definition: APFloat.h:390
static constexpr roundingMode rmNearestTiesToAway
Definition: APFloat.h:374
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:378
static constexpr opStatus opUnderflow
Definition: APFloat.h:388
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:372
int ilogb(const IEEEFloat &Arg)
Definition: APFloat.cpp:4773
static constexpr cmpResult cmpEqual
Definition: APFloat.h:380
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:1529
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1599
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1521
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Definition: APFloat.h:1558
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:1585
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Definition: APFloat.h:1509
@ 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:1544
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:1535
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1572
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition: APFloat.h:1612
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:171
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:265
static bool semanticsHasInf(const fltSemantics &)
Definition: APFloat.cpp:350
static const fltSemantics & Float6E3M2FN() LLVM_READNONE
Definition: APFloat.cpp:285
static constexpr roundingMode rmNearestTiesToAway
Definition: APFloat.h:302
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:287
static const fltSemantics & PPCDoubleDoubleLegacy() LLVM_READNONE
Definition: APFloat.cpp:271
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:300
static ExponentType semanticsMinExponent(const fltSemantics &)
Definition: APFloat.cpp:325
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition: APFloat.h:295
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:297
static unsigned int semanticsSizeInBits(const fltSemantics &)
Definition: APFloat.cpp:328
static bool semanticsHasSignedRepr(const fltSemantics &)
Definition: APFloat.cpp:346
static const fltSemantics & Float8E4M3() LLVM_READNONE
Definition: APFloat.cpp:276
static unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
Definition: APFloat.cpp:372
static const fltSemantics & Float8E4M3FN() LLVM_READNONE
Definition: APFloat.cpp:277
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:268
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:301
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:288
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
Definition: APFloat.h:331
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:267
static const fltSemantics & Float4E2M1FN() LLVM_READNONE
Definition: APFloat.cpp:287
static const fltSemantics & Float8E8M0FNU() LLVM_READNONE
Definition: APFloat.cpp:284
static const fltSemantics & Float8E4M3B11FNUZ() LLVM_READNONE
Definition: APFloat.cpp:279
static const fltSemantics & Bogus() LLVM_READNONE
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real.
Definition: APFloat.cpp:291
static ExponentType semanticsMaxExponent(const fltSemantics &)
Definition: APFloat.cpp:321
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:317
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:266
static bool semanticsHasNaN(const fltSemantics &)
Definition: APFloat.cpp:354
static const fltSemantics & Float8E5M2() LLVM_READNONE
Definition: APFloat.cpp:274
static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition: APFloat.cpp:218
static constexpr unsigned integerPartWidth
Definition: APFloat.h:145
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:263
APInt::WordType integerPart
Definition: APFloat.h:144
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:299
static bool semanticsHasZero(const fltSemantics &)
Definition: APFloat.cpp:342
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition: APFloat.cpp:358
static const fltSemantics & Float8E4M3FNUZ() LLVM_READNONE
Definition: APFloat.cpp:278
IlogbErrorKinds
Enumeration of ilogb error results.
Definition: APFloat.h:336
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:264
static const fltSemantics & FloatTF32() LLVM_READNONE
Definition: APFloat.cpp:283
static const fltSemantics & Float8E5M2FNUZ() LLVM_READNONE
Definition: APFloat.cpp:275
static const fltSemantics & Float6E2M3FN() LLVM_READNONE
Definition: APFloat.cpp:286
fltCategory
Category of internally-represented number.
Definition: APFloat.h:323
@ S_PPCDoubleDoubleLegacy
Definition: APFloat.h:192
static const fltSemantics & Float8E3M4() LLVM_READNONE
Definition: APFloat.cpp:282
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:313
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:331