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