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
141namespace detail {
142class IEEEFloat;
143class DoubleAPFloat;
144} // namespace detail
145
146// This is the common type definitions shared by APFloat and its internal
147// implementation classes. This struct should not define any non-static data
148// members.
150public:
152 static constexpr unsigned integerPartWidth = APInt::APINT_BITS_PER_WORD;
153
154 /// A signed type to represent a floating point numbers unbiased exponent.
155 typedef int32_t ExponentType;
156
157 /// \name Floating Point Semantics.
158 /// @{
165 // The IBM double-double semantics. Such a number consists of a pair of
166 // IEEE 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
167 // (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
168 // Therefore it has two 53-bit mantissa parts that aren't necessarily
169 // adjacent to each other, and two 11-bit exponents.
170 //
171 // Note: we need to make the value different from semBogus as otherwise
172 // an unsafe optimization may collapse both values to a single address,
173 // and we heavily rely on them having distinct addresses.
175 // These are legacy semantics for the fallback, inaccurate implementation
176 // of IBM double-double, if the accurate semPPCDoubleDouble doesn't handle
177 // the operation. It's equivalent to having an IEEE number with consecutive
178 // 106 bits of mantissa and 11 bits of exponent.
179 //
180 // It's not equivalent to IBM double-double. For example, a legit IBM
181 // double-double, 1 + epsilon:
182 //
183 // 1 + epsilon = 1 + (1 >> 1076)
184 //
185 // is not representable by a consecutive 106 bits of mantissa.
186 //
187 // Currently, these semantics are used in the following way:
188 //
189 // semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
190 // (64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
191 // semPPCDoubleDoubleLegacy -> IEEE operations
192 //
193 // We use bitcastToAPInt() to get the bit representation (in APInt) of the
194 // underlying IEEEdouble, then use the APInt constructor to construct the
195 // legacy IEEE float.
196 //
197 // TODO: Implement all operations in semPPCDoubleDouble, and delete these
198 // semantics.
200 // 8-bit floating point number following IEEE-754 conventions with bit
201 // layout S1E5M2 as described in https://arxiv.org/abs/2209.05433.
203 // 8-bit floating point number mostly following IEEE-754 conventions
204 // and bit layout S1E5M2 described in https://arxiv.org/abs/2206.02915,
205 // with expanded range and with no infinity or signed zero.
206 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
207 // This format's exponent bias is 16, instead of the 15 (2 ** (5 - 1) - 1)
208 // that IEEE precedent would imply.
210 // 8-bit floating point number following IEEE-754 conventions with bit
211 // layout S1E4M3.
213 // 8-bit floating point number mostly following IEEE-754 conventions with
214 // bit layout S1E4M3 as described in https://arxiv.org/abs/2209.05433.
215 // Unlike IEEE-754 types, there are no infinity values, and NaN is
216 // represented with the exponent and mantissa bits set to all 1s.
218 // 8-bit floating point number mostly following IEEE-754 conventions
219 // and bit layout S1E4M3 described in https://arxiv.org/abs/2206.02915,
220 // with expanded range and with no infinity or signed zero.
221 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
222 // This format's exponent bias is 8, instead of the 7 (2 ** (4 - 1) - 1)
223 // that IEEE precedent would imply.
225 // 8-bit floating point number mostly following IEEE-754 conventions
226 // and bit layout S1E4M3 with expanded range and with no infinity or signed
227 // zero.
228 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero).
229 // This format's exponent bias is 11, instead of the 7 (2 ** (4 - 1) - 1)
230 // that IEEE precedent would imply.
232 // 8-bit floating point number following IEEE-754 conventions with bit
233 // layout S1E3M4.
235 // Floating point number that occupies 32 bits or less of storage, providing
236 // improved range compared to half (16-bit) formats, at (potentially)
237 // greater throughput than single precision (32-bit) formats.
239 // 8-bit floating point number with (all the) 8 bits for the exponent
240 // like in FP32. There are no zeroes, no infinities, and no denormal values.
241 // This format has unsigned representation only. (U -> Unsigned only).
242 // NaN is represented with all bits set to 1. Bias is 127.
243 // This format represents the scale data type in the MX specification from:
244 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
246 // 6-bit floating point number with bit layout S1E3M2. Unlike IEEE-754
247 // types, there are no infinity or NaN values. The format is detailed in
248 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
250 // 6-bit floating point number with bit layout S1E2M3. Unlike IEEE-754
251 // types, there are no infinity or NaN values. The format is detailed in
252 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
254 // 4-bit floating point number with bit layout S1E2M1. Unlike IEEE-754
255 // types, there are no infinity or NaN values. The format is detailed in
256 // https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
258 // TODO: Documentation is missing.
261 };
262
265
266private:
267 LLVM_ABI static const fltSemantics semIEEEhalf;
268 LLVM_ABI static const fltSemantics semBFloat;
269 LLVM_ABI static const fltSemantics semIEEEsingle;
270 LLVM_ABI static const fltSemantics semIEEEdouble;
271 LLVM_ABI static const fltSemantics semIEEEquad;
272 LLVM_ABI static const fltSemantics semFloat8E5M2;
273 LLVM_ABI static const fltSemantics semFloat8E5M2FNUZ;
274 LLVM_ABI static const fltSemantics semFloat8E4M3;
275 LLVM_ABI static const fltSemantics semFloat8E4M3FN;
276 LLVM_ABI static const fltSemantics semFloat8E4M3FNUZ;
277 LLVM_ABI static const fltSemantics semFloat8E4M3B11FNUZ;
278 LLVM_ABI static const fltSemantics semFloat8E3M4;
279 LLVM_ABI static const fltSemantics semFloatTF32;
280 LLVM_ABI static const fltSemantics semFloat8E8M0FNU;
281 LLVM_ABI static const fltSemantics semFloat6E3M2FN;
282 LLVM_ABI static const fltSemantics semFloat6E2M3FN;
283 LLVM_ABI static const fltSemantics semFloat4E2M1FN;
284 LLVM_ABI static const fltSemantics semX87DoubleExtended;
285 LLVM_ABI static const fltSemantics semBogus;
286 LLVM_ABI static const fltSemantics semPPCDoubleDouble;
287 LLVM_ABI static const fltSemantics semPPCDoubleDoubleLegacy;
288
289 friend class detail::IEEEFloat;
291 friend class APFloat;
292
293public:
294 static const fltSemantics &IEEEhalf() { return semIEEEhalf; }
295 static const fltSemantics &BFloat() { return semBFloat; }
296 static const fltSemantics &IEEEsingle() { return semIEEEsingle; }
297 static const fltSemantics &IEEEdouble() { return semIEEEdouble; }
298 static const fltSemantics &IEEEquad() { return semIEEEquad; }
299 static const fltSemantics &PPCDoubleDouble() { return semPPCDoubleDouble; }
301 return semPPCDoubleDoubleLegacy;
302 }
303 static const fltSemantics &Float8E5M2() { return semFloat8E5M2; }
304 static const fltSemantics &Float8E5M2FNUZ() { return semFloat8E5M2FNUZ; }
305 static const fltSemantics &Float8E4M3() { return semFloat8E4M3; }
306 static const fltSemantics &Float8E4M3FN() { return semFloat8E4M3FN; }
307 static const fltSemantics &Float8E4M3FNUZ() { return semFloat8E4M3FNUZ; }
309 return semFloat8E4M3B11FNUZ;
310 }
311 static const fltSemantics &Float8E3M4() { return semFloat8E3M4; }
312 static const fltSemantics &FloatTF32() { return semFloatTF32; }
313 static const fltSemantics &Float8E8M0FNU() { return semFloat8E8M0FNU; }
314 static const fltSemantics &Float6E3M2FN() { return semFloat6E3M2FN; }
315 static const fltSemantics &Float6E2M3FN() { return semFloat6E2M3FN; }
316 static const fltSemantics &Float4E2M1FN() { return semFloat4E2M1FN; }
318 return semX87DoubleExtended;
319 }
320
321 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
322 /// anything real.
323 static const fltSemantics &Bogus() { return semBogus; }
324
325 // Returns true if any number described by this semantics can be precisely
326 // represented by the specified semantics. Does not take into account
327 // the value of fltNonfiniteBehavior, hasZero, hasSignedRepr.
328 LLVM_ABI static bool isRepresentableBy(const fltSemantics &A,
329 const fltSemantics &B);
330
331 /// @}
332
333 /// IEEE-754R 5.11: Floating Point Comparison Relations.
340
341 /// IEEE-754R 4.3: Rounding-direction attributes.
343
351
352 /// IEEE-754R 7: Default exception handling.
353 ///
354 /// opUnderflow or opOverflow are always returned or-ed with opInexact.
355 ///
356 /// APFloat models this behavior specified by IEEE-754:
357 /// "For operations producing results in floating-point format, the default
358 /// result of an operation that signals the invalid operation exception
359 /// shall be a quiet NaN."
360 enum opStatus {
361 opOK = 0x00,
367 };
368
369 /// Category of internally-represented number.
376
377 /// Convenience enum used to construct an uninitialized APFloat.
381
382 /// Enumeration of \c ilogb error results.
384 IEK_Zero = INT_MIN + 1,
385 IEK_NaN = INT_MIN,
386 IEK_Inf = INT_MAX
387 };
388
389 LLVM_ABI static unsigned int semanticsPrecision(const fltSemantics &);
392 LLVM_ABI static unsigned int semanticsSizeInBits(const fltSemantics &);
393 LLVM_ABI static unsigned int semanticsIntSizeInBits(const fltSemantics &,
394 bool);
395 LLVM_ABI static bool semanticsHasZero(const fltSemantics &);
396 LLVM_ABI static bool semanticsHasSignedRepr(const fltSemantics &);
397 LLVM_ABI static bool semanticsHasInf(const fltSemantics &);
398 LLVM_ABI static bool semanticsHasNaN(const fltSemantics &);
399 LLVM_ABI static bool isIEEELikeFP(const fltSemantics &);
400 LLVM_ABI static bool hasSignBitInMSB(const fltSemantics &);
401
402 // Returns true if any number described by \p Src can be precisely represented
403 // by a normal (not subnormal) value in \p Dst.
404 LLVM_ABI static bool isRepresentableAsNormalIn(const fltSemantics &Src,
405 const fltSemantics &Dst);
406
407 /// Returns the size of the floating point number (in bits) in the given
408 /// semantics.
409 LLVM_ABI static unsigned getSizeInBits(const fltSemantics &Sem);
410};
411
412namespace detail {
413
434static constexpr opStatus opOK = APFloatBase::opOK;
444
445class IEEEFloat final {
446public:
447 /// \name Constructors
448 /// @{
449
450 LLVM_ABI IEEEFloat(const fltSemantics &); // Default construct to +0.0
453 LLVM_ABI IEEEFloat(const fltSemantics &, const APInt &);
454 LLVM_ABI explicit IEEEFloat(double d);
455 LLVM_ABI explicit IEEEFloat(float f);
459
460 /// @}
461
462 /// Returns whether this instance allocated memory.
463 bool needsCleanup() const { return partCount() > 1; }
464
465 /// \name Convenience "constructors"
466 /// @{
467
468 /// @}
469
470 /// \name Arithmetic
471 /// @{
472
477 /// IEEE remainder.
479 /// C fmod, or llvm frem.
484 /// IEEE-754R 5.3.1: nextUp/nextDown.
485 LLVM_ABI opStatus next(bool nextDown);
486
487 /// @}
488
489 /// \name Sign operations.
490 /// @{
491
492 LLVM_ABI void changeSign();
493
494 /// @}
495
496 /// \name Conversions
497 /// @{
498
501 bool, roundingMode, bool *) const;
505 LLVM_ABI double convertToDouble() const;
506#ifdef HAS_IEE754_FLOAT128
507 LLVM_ABI float128 convertToQuad() const;
508#endif
509 LLVM_ABI float convertToFloat() const;
510
511 /// @}
512
513 /// The definition of equality is not straightforward for floating point, so
514 /// we won't use operator==. Use one of the following, or write whatever it
515 /// is you really mean.
516 bool operator==(const IEEEFloat &) const = delete;
517
518 /// IEEE comparison with another floating point number (NaNs compare
519 /// unordered, 0==-0).
520 LLVM_ABI cmpResult compare(const IEEEFloat &) const;
521
522 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
523 LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const;
524
525 /// Write out a hexadecimal representation of the floating point value to DST,
526 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
527 /// Return the number of characters written, excluding the terminating NUL.
528 LLVM_ABI unsigned int convertToHexString(char *dst, unsigned int hexDigits,
529 bool upperCase, roundingMode) const;
530
531 /// \name IEEE-754R 5.7.2 General operations.
532 /// @{
533
534 /// IEEE-754R isSignMinus: Returns true if and only if the current value is
535 /// negative.
536 ///
537 /// This applies to zeros and NaNs as well.
538 bool isNegative() const { return sign; }
539
540 /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
541 ///
542 /// This implies that the current value of the float is not zero, subnormal,
543 /// infinite, or NaN following the definition of normality from IEEE-754R.
544 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
545
546 /// Returns true if and only if the current value is zero, subnormal, or
547 /// normal.
548 ///
549 /// This means that the value is not infinite or NaN.
550 bool isFinite() const { return !isNaN() && !isInfinity(); }
551
552 /// Returns true if and only if the float is plus or minus zero.
553 bool isZero() const { return category == fltCategory::fcZero; }
554
555 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
556 /// denormal.
557 LLVM_ABI bool isDenormal() const;
558
559 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
560 bool isInfinity() const { return category == fcInfinity; }
561
562 /// Returns true if and only if the float is a quiet or signaling NaN.
563 bool isNaN() const { return category == fcNaN; }
564
565 /// Returns true if and only if the float is a signaling NaN.
566 LLVM_ABI bool isSignaling() const;
567
568 /// @}
569
570 /// \name Simple Queries
571 /// @{
572
573 fltCategory getCategory() const { return category; }
574 const fltSemantics &getSemantics() const { return *semantics; }
575 bool isNonZero() const { return category != fltCategory::fcZero; }
576 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
577 bool isPosZero() const { return isZero() && !isNegative(); }
578 bool isNegZero() const { return isZero() && isNegative(); }
579
580 /// Returns true if and only if the number has the smallest possible non-zero
581 /// magnitude in the current semantics.
582 LLVM_ABI bool isSmallest() const;
583
584 /// Returns true if this is the smallest (by magnitude) normalized finite
585 /// number in the given semantics.
586 LLVM_ABI bool isSmallestNormalized() const;
587
588 /// Returns true if and only if the number has the largest possible finite
589 /// magnitude in the current semantics.
590 LLVM_ABI bool isLargest() const;
591
592 /// Returns true if and only if the number is an exact integer.
593 LLVM_ABI bool isInteger() const;
594
595 /// @}
596
599
600 /// Overload to compute a hash code for an APFloat value.
601 ///
602 /// Note that the use of hash codes for floating point values is in general
603 /// frought with peril. Equality is hard to define for these values. For
604 /// example, should negative and positive zero hash to different codes? Are
605 /// they equal or not? This hash value implementation specifically
606 /// emphasizes producing different codes for different inputs in order to
607 /// be used in canonicalization and memoization. As such, equality is
608 /// bitwiseIsEqual, and 0 != -0.
609 LLVM_ABI friend hash_code hash_value(const IEEEFloat &Arg);
610
611 /// Converts this value into a decimal string.
612 ///
613 /// \param FormatPrecision The maximum number of digits of
614 /// precision to output. If there are fewer digits available,
615 /// zero padding will not be used unless the value is
616 /// integral and small enough to be expressed in
617 /// FormatPrecision digits. 0 means to use the natural
618 /// precision of the number.
619 /// \param FormatMaxPadding The maximum number of zeros to
620 /// consider inserting before falling back to scientific
621 /// notation. 0 means to always use scientific notation.
622 ///
623 /// \param TruncateZero Indicate whether to remove the trailing zero in
624 /// fraction part or not. Also setting this parameter to false forcing
625 /// producing of output more similar to default printf behavior.
626 /// Specifically the lower e is used as exponent delimiter and exponent
627 /// always contains no less than two digits.
628 ///
629 /// Number Precision MaxPadding Result
630 /// ------ --------- ---------- ------
631 /// 1.01E+4 5 2 10100
632 /// 1.01E+4 4 2 1.01E+4
633 /// 1.01E+4 5 1 1.01E+4
634 /// 1.01E-2 5 2 0.0101
635 /// 1.01E-2 4 2 0.0101
636 /// 1.01E-2 4 1 1.01E-2
638 unsigned FormatPrecision = 0,
639 unsigned FormatMaxPadding = 3,
640 bool TruncateZero = true) const;
641
643
644 LLVM_ABI friend int ilogb(const IEEEFloat &Arg);
645
647
648 LLVM_ABI friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
649
650 /// \name Special value setters.
651 /// @{
652
653 LLVM_ABI void makeLargest(bool Neg = false);
654 LLVM_ABI void makeSmallest(bool Neg = false);
655 LLVM_ABI void makeNaN(bool SNaN = false, bool Neg = false,
656 const APInt *fill = nullptr);
657 LLVM_ABI void makeInf(bool Neg = false);
658 LLVM_ABI void makeZero(bool Neg = false);
659 LLVM_ABI void makeQuiet();
660
661 /// Returns the smallest (by magnitude) normalized finite number in the given
662 /// semantics.
663 ///
664 /// \param Negative - True iff the number should be negative
665 LLVM_ABI void makeSmallestNormalized(bool Negative = false);
666
667 /// @}
668
670
671private:
672 /// \name Simple Queries
673 /// @{
674
675 integerPart *significandParts();
676 const integerPart *significandParts() const;
677 LLVM_ABI unsigned int partCount() const;
678
679 /// @}
680
681 /// \name Significand operations.
682 /// @{
683
684 integerPart addSignificand(const IEEEFloat &);
685 integerPart subtractSignificand(const IEEEFloat &, integerPart);
686 // Exported for IEEEFloatUnitTestHelper.
687 LLVM_ABI lostFraction addOrSubtractSignificand(const IEEEFloat &,
688 bool subtract);
689 lostFraction multiplySignificand(const IEEEFloat &, IEEEFloat,
690 bool ignoreAddend = false);
691 lostFraction multiplySignificand(const IEEEFloat&);
692 lostFraction divideSignificand(const IEEEFloat &);
693 void incrementSignificand();
694 void initialize(const fltSemantics *);
695 void shiftSignificandLeft(unsigned int);
696 lostFraction shiftSignificandRight(unsigned int);
697 unsigned int significandLSB() const;
698 unsigned int significandMSB() const;
699 void zeroSignificand();
700 unsigned int getNumHighBits() const;
701 /// Return true if the significand excluding the integral bit is all ones.
702 bool isSignificandAllOnes() const;
703 bool isSignificandAllOnesExceptLSB() const;
704 /// Return true if the significand excluding the integral bit is all zeros.
705 bool isSignificandAllZeros() const;
706 bool isSignificandAllZerosExceptMSB() const;
707
708 /// @}
709
710 /// \name Arithmetic on special values.
711 /// @{
712
713 opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract);
714 opStatus divideSpecials(const IEEEFloat &);
715 opStatus multiplySpecials(const IEEEFloat &);
716 opStatus modSpecials(const IEEEFloat &);
717 opStatus remainderSpecials(const IEEEFloat&);
718
719 /// @}
720
721 /// \name Miscellany
722 /// @{
723
724 bool convertFromStringSpecials(StringRef str);
726 opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract);
727 opStatus handleOverflow(roundingMode);
728 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
729 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>,
730 unsigned int, bool, roundingMode,
731 bool *) const;
732 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
734 Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode);
735 Expected<opStatus> convertFromDecimalString(StringRef, roundingMode);
736 char *convertNormalToHexString(char *, unsigned int, bool,
737 roundingMode) const;
738 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
743
744 /// @}
745
746 template <const fltSemantics &S> APInt convertIEEEFloatToAPInt() const;
747 APInt convertHalfAPFloatToAPInt() const;
748 APInt convertBFloatAPFloatToAPInt() const;
749 APInt convertFloatAPFloatToAPInt() const;
750 APInt convertDoubleAPFloatToAPInt() const;
751 APInt convertQuadrupleAPFloatToAPInt() const;
752 APInt convertF80LongDoubleAPFloatToAPInt() const;
753 APInt convertPPCDoubleDoubleLegacyAPFloatToAPInt() const;
754 APInt convertFloat8E5M2APFloatToAPInt() const;
755 APInt convertFloat8E5M2FNUZAPFloatToAPInt() const;
756 APInt convertFloat8E4M3APFloatToAPInt() const;
757 APInt convertFloat8E4M3FNAPFloatToAPInt() const;
758 APInt convertFloat8E4M3FNUZAPFloatToAPInt() const;
759 APInt convertFloat8E4M3B11FNUZAPFloatToAPInt() const;
760 APInt convertFloat8E3M4APFloatToAPInt() const;
761 APInt convertFloatTF32APFloatToAPInt() const;
762 APInt convertFloat8E8M0FNUAPFloatToAPInt() const;
763 APInt convertFloat6E3M2FNAPFloatToAPInt() const;
764 APInt convertFloat6E2M3FNAPFloatToAPInt() const;
765 APInt convertFloat4E2M1FNAPFloatToAPInt() const;
766 void initFromAPInt(const fltSemantics *Sem, const APInt &api);
767 template <const fltSemantics &S> void initFromIEEEAPInt(const APInt &api);
768 void initFromHalfAPInt(const APInt &api);
769 void initFromBFloatAPInt(const APInt &api);
770 void initFromFloatAPInt(const APInt &api);
771 void initFromDoubleAPInt(const APInt &api);
772 void initFromQuadrupleAPInt(const APInt &api);
773 void initFromF80LongDoubleAPInt(const APInt &api);
774 void initFromPPCDoubleDoubleLegacyAPInt(const APInt &api);
775 void initFromFloat8E5M2APInt(const APInt &api);
776 void initFromFloat8E5M2FNUZAPInt(const APInt &api);
777 void initFromFloat8E4M3APInt(const APInt &api);
778 void initFromFloat8E4M3FNAPInt(const APInt &api);
779 void initFromFloat8E4M3FNUZAPInt(const APInt &api);
780 void initFromFloat8E4M3B11FNUZAPInt(const APInt &api);
781 void initFromFloat8E3M4APInt(const APInt &api);
782 void initFromFloatTF32APInt(const APInt &api);
783 void initFromFloat8E8M0FNUAPInt(const APInt &api);
784 void initFromFloat6E3M2FNAPInt(const APInt &api);
785 void initFromFloat6E2M3FNAPInt(const APInt &api);
786 void initFromFloat4E2M1FNAPInt(const APInt &api);
787
788 void assign(const IEEEFloat &);
789 void copySignificand(const IEEEFloat &);
790 void freeSignificand();
791
792 /// Note: this must be the first data member.
793 /// The semantics that this value obeys.
794 const fltSemantics *semantics;
795
796 /// A binary fraction with an explicit integer bit.
797 ///
798 /// The significand must be at least one bit wider than the target precision.
799 union Significand {
800 integerPart part;
801 integerPart *parts;
802 } significand;
803
804 /// The signed unbiased exponent of the value.
805 ExponentType exponent;
806
807 /// What kind of floating point number this is.
808 ///
809 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
810 /// Using the extra bit keeps it from failing under VisualStudio.
811 fltCategory category : 3;
812
813 /// Sign bit of the number.
814 unsigned int sign : 1;
815
817};
818
820LLVM_ABI int ilogb(const IEEEFloat &Arg);
822LLVM_ABI IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM);
823
824// This mode implements more precise float in terms of two APFloats.
825// The interface and layout is designed for arbitrary underlying semantics,
826// though currently only PPCDoubleDouble semantics are supported, whose
827// corresponding underlying semantics are IEEEdouble.
828class DoubleAPFloat final {
829 // Note: this must be the first data member.
830 const fltSemantics *Semantics;
831 APFloat *Floats;
832
833 opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c,
834 const APFloat &cc, roundingMode RM);
835
836 opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS,
837 DoubleAPFloat &Out, roundingMode RM);
838 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart> Input,
839 unsigned int Width, bool IsSigned,
840 roundingMode RM, bool *IsExact) const;
841
842 // Convert an unsigned integer Src to a floating point number,
843 // rounding according to RM. The sign of the floating point number is not
844 // modified.
845 opStatus convertFromUnsignedParts(const integerPart *Src,
846 unsigned int SrcCount, roundingMode RM);
847
848 // Handle overflow. Sign is preserved. We either become infinity or
849 // the largest finite number.
850 opStatus handleOverflow(roundingMode RM);
851
852public:
856 LLVM_ABI DoubleAPFloat(const fltSemantics &S, const APInt &I);
858 APFloat &&Second);
862
865
866 bool needsCleanup() const { return Floats != nullptr; }
867
868 inline APFloat &getFirst();
869 inline const APFloat &getFirst() const;
870 inline APFloat &getSecond();
871 inline const APFloat &getSecond() const;
872
880 const DoubleAPFloat &Addend,
881 roundingMode RM);
883 LLVM_ABI void changeSign();
885
887 LLVM_ABI bool isNegative() const;
888
889 LLVM_ABI void makeInf(bool Neg);
890 LLVM_ABI void makeZero(bool Neg);
891 LLVM_ABI void makeLargest(bool Neg);
892 LLVM_ABI void makeSmallest(bool Neg);
893 LLVM_ABI void makeSmallestNormalized(bool Neg);
894 LLVM_ABI void makeNaN(bool SNaN, bool Neg, const APInt *fill);
895
897 LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
900 LLVM_ABI opStatus next(bool nextDown);
901
903 unsigned int Width, bool IsSigned,
904 roundingMode RM, bool *IsExact) const;
905 LLVM_ABI opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
906 roundingMode RM);
907 LLVM_ABI unsigned int convertToHexString(char *DST, unsigned int HexDigits,
908 bool UpperCase,
909 roundingMode RM) const;
910
911 LLVM_ABI bool isDenormal() const;
912 LLVM_ABI bool isSmallest() const;
913 LLVM_ABI bool isSmallestNormalized() const;
914 LLVM_ABI bool isLargest() const;
915 LLVM_ABI bool isInteger() const;
916
917 LLVM_ABI void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
918 unsigned FormatMaxPadding,
919 bool TruncateZero = true) const;
920
922
923 LLVM_ABI friend int ilogb(const DoubleAPFloat &X);
926 LLVM_ABI friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp,
928 LLVM_ABI friend hash_code hash_value(const DoubleAPFloat &Arg);
929};
930
932LLVM_ABI DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp,
933 roundingMode RM);
935
936} // End detail namespace
937
938// This is a interface class that is currently forwarding functionalities from
939// detail::IEEEFloat.
940class APFloat : public APFloatBase {
941 typedef detail::IEEEFloat IEEEFloat;
942 typedef detail::DoubleAPFloat DoubleAPFloat;
943
944 static_assert(std::is_standard_layout<IEEEFloat>::value);
945
946 union Storage {
947 const fltSemantics *semantics;
948 IEEEFloat IEEE;
949 DoubleAPFloat Double;
950
951 LLVM_ABI explicit Storage(IEEEFloat F, const fltSemantics &S);
952 explicit Storage(DoubleAPFloat F, const fltSemantics &S)
953 : Double(std::move(F)) {
954 assert(&S == &PPCDoubleDouble());
955 }
956
957 template <typename... ArgTypes>
958 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
959 if (usesLayout<IEEEFloat>(Semantics)) {
960 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
961 return;
962 }
963 if (usesLayout<DoubleAPFloat>(Semantics)) {
964 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
965 return;
966 }
967 llvm_unreachable("Unexpected semantics");
968 }
969
970 LLVM_ABI ~Storage();
971 LLVM_ABI Storage(const Storage &RHS);
972 LLVM_ABI Storage(Storage &&RHS);
973 LLVM_ABI Storage &operator=(const Storage &RHS);
974 LLVM_ABI Storage &operator=(Storage &&RHS);
975 } U;
976
977 template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
978 static_assert(std::is_same<T, IEEEFloat>::value ||
979 std::is_same<T, DoubleAPFloat>::value);
980 if (std::is_same<T, DoubleAPFloat>::value) {
981 return &Semantics == &PPCDoubleDouble();
982 }
983 return &Semantics != &PPCDoubleDouble();
984 }
985
986 IEEEFloat &getIEEE() {
987 if (usesLayout<IEEEFloat>(*U.semantics))
988 return U.IEEE;
989 if (usesLayout<DoubleAPFloat>(*U.semantics))
990 return U.Double.getFirst().U.IEEE;
991 llvm_unreachable("Unexpected semantics");
992 }
993
994 const IEEEFloat &getIEEE() const {
995 if (usesLayout<IEEEFloat>(*U.semantics))
996 return U.IEEE;
997 if (usesLayout<DoubleAPFloat>(*U.semantics))
998 return U.Double.getFirst().U.IEEE;
999 llvm_unreachable("Unexpected semantics");
1000 }
1001
1002 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
1003
1004 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
1005
1006 void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
1007 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
1008 }
1009
1010 void makeLargest(bool Neg) {
1011 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
1012 }
1013
1014 void makeSmallest(bool Neg) {
1015 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
1016 }
1017
1018 void makeSmallestNormalized(bool Neg) {
1019 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
1020 }
1021
1022 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
1023 explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
1024 : U(std::move(F), S) {}
1025
1026 // Compares the absolute value of this APFloat with another. Both operands
1027 // must be finite non-zero.
1028 cmpResult compareAbsoluteValue(const APFloat &RHS) const {
1029 assert(&getSemantics() == &RHS.getSemantics() &&
1030 "Should only compare APFloats with the same semantics");
1031 if (usesLayout<IEEEFloat>(getSemantics()))
1032 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
1033 if (usesLayout<DoubleAPFloat>(getSemantics()))
1034 return U.Double.compareAbsoluteValue(RHS.U.Double);
1035 llvm_unreachable("Unexpected semantics");
1036 }
1037
1038public:
1042 template <typename T,
1043 typename = std::enable_if_t<std::is_floating_point<T>::value>>
1044 APFloat(const fltSemantics &Semantics, T V) = delete;
1045 // TODO: Remove this constructor. This isn't faster than the first one.
1049 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
1050 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
1051 APFloat(const APFloat &RHS) = default;
1052 APFloat(APFloat &&RHS) = default;
1053
1054 ~APFloat() = default;
1055
1057
1058 /// Factory for Positive and Negative Zero.
1059 ///
1060 /// \param Negative True iff the number should be negative.
1061 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
1062 APFloat Val(Sem, uninitialized);
1063 Val.makeZero(Negative);
1064 return Val;
1065 }
1066
1067 /// Factory for Positive and Negative One.
1068 ///
1069 /// \param Negative True iff the number should be negative.
1070 static APFloat getOne(const fltSemantics &Sem, bool Negative = false) {
1071 APFloat Val(Sem, 1U);
1072 if (Negative)
1073 Val.changeSign();
1074 return Val;
1075 }
1076
1077 /// Factory for Positive and Negative Infinity.
1078 ///
1079 /// \param Negative True iff the number should be negative.
1080 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
1081 APFloat Val(Sem, uninitialized);
1082 Val.makeInf(Negative);
1083 return Val;
1084 }
1085
1086 /// Factory for NaN values.
1087 ///
1088 /// \param Negative - True iff the NaN generated should be negative.
1089 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
1090 /// default. The value is truncated as necessary.
1091 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
1092 uint64_t payload = 0) {
1093 if (payload) {
1094 APInt intPayload(64, payload);
1095 return getQNaN(Sem, Negative, &intPayload);
1096 } else {
1097 return getQNaN(Sem, Negative, nullptr);
1098 }
1099 }
1100
1101 /// Factory for QNaN values.
1102 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
1103 const APInt *payload = nullptr) {
1104 APFloat Val(Sem, uninitialized);
1105 Val.makeNaN(false, Negative, payload);
1106 return Val;
1107 }
1108
1109 /// Factory for SNaN values.
1110 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
1111 const APInt *payload = nullptr) {
1112 APFloat Val(Sem, uninitialized);
1113 Val.makeNaN(true, Negative, payload);
1114 return Val;
1115 }
1116
1117 /// Returns the largest finite number in the given semantics.
1118 ///
1119 /// \param Negative - True iff the number should be negative
1120 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
1121 APFloat Val(Sem, uninitialized);
1122 Val.makeLargest(Negative);
1123 return Val;
1124 }
1125
1126 /// Returns the smallest (by magnitude) finite number in the given semantics.
1127 /// Might be denormalized, which implies a relative loss of precision.
1128 ///
1129 /// \param Negative - True iff the number should be negative
1130 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
1131 APFloat Val(Sem, uninitialized);
1132 Val.makeSmallest(Negative);
1133 return Val;
1134 }
1135
1136 /// Returns the smallest (by magnitude) normalized finite number in the given
1137 /// semantics.
1138 ///
1139 /// \param Negative - True iff the number should be negative
1140 static APFloat getSmallestNormalized(const fltSemantics &Sem,
1141 bool Negative = false) {
1142 APFloat Val(Sem, uninitialized);
1143 Val.makeSmallestNormalized(Negative);
1144 return Val;
1145 }
1146
1147 /// Returns a float which is bitcasted from an all one value int.
1148 ///
1149 /// \param Semantics - type float semantics
1151
1152 /// Returns true if the given semantics has actual significand.
1153 ///
1154 /// \param Sem - type float semantics
1155 static bool hasSignificand(const fltSemantics &Sem) {
1156 return &Sem != &Float8E8M0FNU();
1157 }
1158
1159 /// Used to insert APFloat objects, or objects that contain APFloat objects,
1160 /// into FoldingSets.
1161 LLVM_ABI void Profile(FoldingSetNodeID &NID) const;
1162
1163 opStatus add(const APFloat &RHS, roundingMode RM) {
1164 assert(&getSemantics() == &RHS.getSemantics() &&
1165 "Should only call on two APFloats with the same semantics");
1166 if (usesLayout<IEEEFloat>(getSemantics()))
1167 return U.IEEE.add(RHS.U.IEEE, RM);
1168 if (usesLayout<DoubleAPFloat>(getSemantics()))
1169 return U.Double.add(RHS.U.Double, RM);
1170 llvm_unreachable("Unexpected semantics");
1171 }
1172 opStatus subtract(const APFloat &RHS, roundingMode RM) {
1173 assert(&getSemantics() == &RHS.getSemantics() &&
1174 "Should only call on two APFloats with the same semantics");
1175 if (usesLayout<IEEEFloat>(getSemantics()))
1176 return U.IEEE.subtract(RHS.U.IEEE, RM);
1177 if (usesLayout<DoubleAPFloat>(getSemantics()))
1178 return U.Double.subtract(RHS.U.Double, RM);
1179 llvm_unreachable("Unexpected semantics");
1180 }
1181 opStatus multiply(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.multiply(RHS.U.IEEE, RM);
1186 if (usesLayout<DoubleAPFloat>(getSemantics()))
1187 return U.Double.multiply(RHS.U.Double, RM);
1188 llvm_unreachable("Unexpected semantics");
1189 }
1190 opStatus divide(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.divide(RHS.U.IEEE, RM);
1195 if (usesLayout<DoubleAPFloat>(getSemantics()))
1196 return U.Double.divide(RHS.U.Double, RM);
1197 llvm_unreachable("Unexpected semantics");
1198 }
1199 opStatus remainder(const APFloat &RHS) {
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.remainder(RHS.U.IEEE);
1204 if (usesLayout<DoubleAPFloat>(getSemantics()))
1205 return U.Double.remainder(RHS.U.Double);
1206 llvm_unreachable("Unexpected semantics");
1207 }
1208 opStatus mod(const APFloat &RHS) {
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.mod(RHS.U.IEEE);
1213 if (usesLayout<DoubleAPFloat>(getSemantics()))
1214 return U.Double.mod(RHS.U.Double);
1215 llvm_unreachable("Unexpected semantics");
1216 }
1217 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
1218 roundingMode RM) {
1219 assert(&getSemantics() == &Multiplicand.getSemantics() &&
1220 "Should only call on APFloats with the same semantics");
1221 assert(&getSemantics() == &Addend.getSemantics() &&
1222 "Should only call on APFloats with the same semantics");
1223 if (usesLayout<IEEEFloat>(getSemantics()))
1224 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1225 if (usesLayout<DoubleAPFloat>(getSemantics()))
1226 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1227 RM);
1228 llvm_unreachable("Unexpected semantics");
1229 }
1233
1234 // TODO: bool parameters are not readable and a source of bugs.
1235 // Do something.
1236 opStatus next(bool nextDown) {
1238 }
1239
1240 /// Negate an APFloat.
1241 APFloat operator-() const {
1242 APFloat Result(*this);
1243 Result.changeSign();
1244 return Result;
1245 }
1246
1247 /// Add two APFloats, rounding ties to the nearest even.
1248 /// No error checking.
1249 APFloat operator+(const APFloat &RHS) const {
1250 APFloat Result(*this);
1251 (void)Result.add(RHS, rmNearestTiesToEven);
1252 return Result;
1253 }
1254
1255 /// Subtract two APFloats, rounding ties to the nearest even.
1256 /// No error checking.
1257 APFloat operator-(const APFloat &RHS) const {
1258 APFloat Result(*this);
1259 (void)Result.subtract(RHS, rmNearestTiesToEven);
1260 return Result;
1261 }
1262
1263 /// Multiply two APFloats, rounding ties to the nearest even.
1264 /// No error checking.
1265 APFloat operator*(const APFloat &RHS) const {
1266 APFloat Result(*this);
1267 (void)Result.multiply(RHS, rmNearestTiesToEven);
1268 return Result;
1269 }
1270
1271 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1272 /// No error checking.
1273 APFloat operator/(const APFloat &RHS) const {
1274 APFloat Result(*this);
1275 (void)Result.divide(RHS, rmNearestTiesToEven);
1276 return Result;
1277 }
1278
1280 void clearSign() {
1281 if (isNegative())
1282 changeSign();
1283 }
1284 void copySign(const APFloat &RHS) {
1285 if (isNegative() != RHS.isNegative())
1286 changeSign();
1287 }
1288
1289 /// A static helper to produce a copy of an APFloat value with its sign
1290 /// copied from some other APFloat.
1291 static APFloat copySign(APFloat Value, const APFloat &Sign) {
1292 Value.copySign(Sign);
1293 return Value;
1294 }
1295
1296 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
1297 /// This preserves the sign and payload bits.
1298 [[nodiscard]] APFloat makeQuiet() const {
1299 APFloat Result(*this);
1300 Result.getIEEE().makeQuiet();
1301 return Result;
1302 }
1303
1304 LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
1305 bool *losesInfo);
1306 // Convert a floating point number to an integer according to the
1307 // rounding mode. We provide deterministic values in case of an invalid
1308 // operation exception, namely zero for NaNs and the minimal or maximal value
1309 // respectively for underflow or overflow.
1310 // The *IsExact output tells whether the result is exact, in the sense that
1311 // converting it back to the original floating point type produces the
1312 // original value. This is almost equivalent to result==opOK, except for
1313 // negative zeroes.
1315 unsigned int Width, bool IsSigned, roundingMode RM,
1316 bool *IsExact) const {
1318 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1319 }
1320 // Same as convertToInteger(integerPart*, ...), except the result is returned
1321 // in an APSInt, whose initial bit-width and signed-ness are used to determine
1322 // the precision of the conversion.
1324 bool *IsExact) const;
1325
1326 // Convert a two's complement integer Input to a floating point number,
1327 // rounding according to RM. IsSigned is true if the integer is signed,
1328 // in which case it must be sign-extended.
1329 opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
1330 roundingMode RM) {
1332 }
1333
1338
1339 /// Converts this APFloat to host double value.
1340 ///
1341 /// \pre The APFloat must be built using semantics, that can be represented by
1342 /// the host double type without loss of precision. It can be IEEEdouble and
1343 /// shorter semantics, like IEEEsingle and others.
1344 LLVM_ABI double convertToDouble() const;
1345
1346 /// Converts this APFloat to host float value.
1347 ///
1348 /// \pre The APFloat must be built using semantics, that can be represented by
1349 /// the host float type without loss of precision. It can be IEEEquad and
1350 /// shorter semantics, like IEEEdouble and others.
1351#ifdef HAS_IEE754_FLOAT128
1352 LLVM_ABI float128 convertToQuad() const;
1353#endif
1354
1355 /// Converts this APFloat to host float value.
1356 ///
1357 /// \pre The APFloat must be built using semantics, that can be represented by
1358 /// the host float type without loss of precision. It can be IEEEsingle and
1359 /// shorter semantics, like IEEEhalf.
1360 LLVM_ABI float convertToFloat() const;
1361
1362 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
1363
1364 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; }
1365
1366 bool operator<(const APFloat &RHS) const {
1367 return compare(RHS) == cmpLessThan;
1368 }
1369
1370 bool operator>(const APFloat &RHS) const {
1371 return compare(RHS) == cmpGreaterThan;
1372 }
1373
1374 bool operator<=(const APFloat &RHS) const {
1375 cmpResult Res = compare(RHS);
1376 return Res == cmpLessThan || Res == cmpEqual;
1377 }
1378
1379 bool operator>=(const APFloat &RHS) const {
1380 cmpResult Res = compare(RHS);
1381 return Res == cmpGreaterThan || Res == cmpEqual;
1382 }
1383
1384 // IEEE comparison with another floating point number (NaNs compare unordered,
1385 // 0==-0).
1386 cmpResult compare(const APFloat &RHS) const {
1387 assert(&getSemantics() == &RHS.getSemantics() &&
1388 "Should only compare APFloats with the same semantics");
1389 if (usesLayout<IEEEFloat>(getSemantics()))
1390 return U.IEEE.compare(RHS.U.IEEE);
1391 if (usesLayout<DoubleAPFloat>(getSemantics()))
1392 return U.Double.compare(RHS.U.Double);
1393 llvm_unreachable("Unexpected semantics");
1394 }
1395
1396 bool bitwiseIsEqual(const APFloat &RHS) const {
1397 if (&getSemantics() != &RHS.getSemantics())
1398 return false;
1399 if (usesLayout<IEEEFloat>(getSemantics()))
1400 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1401 if (usesLayout<DoubleAPFloat>(getSemantics()))
1402 return U.Double.bitwiseIsEqual(RHS.U.Double);
1403 llvm_unreachable("Unexpected semantics");
1404 }
1405
1406 /// We don't rely on operator== working on double values, as
1407 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1408 /// As such, this method can be used to do an exact bit-for-bit comparison of
1409 /// two floating point values.
1410 ///
1411 /// We leave the version with the double argument here because it's just so
1412 /// convenient to write "2.0" and the like. Without this function we'd
1413 /// have to duplicate its logic everywhere it's called.
1414 bool isExactlyValue(double V) const {
1415 bool ignored;
1416 APFloat Tmp(V);
1418 return bitwiseIsEqual(Tmp);
1419 }
1420
1421 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
1422 bool UpperCase, roundingMode RM) const {
1424 convertToHexString(DST, HexDigits, UpperCase, RM));
1425 }
1426
1427 bool isZero() const { return getCategory() == fcZero; }
1428 bool isInfinity() const { return getCategory() == fcInfinity; }
1429 bool isNaN() const { return getCategory() == fcNaN; }
1430
1431 bool isNegative() const { return getIEEE().isNegative(); }
1433 bool isSignaling() const { return getIEEE().isSignaling(); }
1434
1435 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
1436 bool isFinite() const { return !isNaN() && !isInfinity(); }
1437
1438 fltCategory getCategory() const { return getIEEE().getCategory(); }
1439 const fltSemantics &getSemantics() const { return *U.semantics; }
1440 bool isNonZero() const { return !isZero(); }
1441 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
1442 bool isPosZero() const { return isZero() && !isNegative(); }
1443 bool isNegZero() const { return isZero() && isNegative(); }
1444 bool isPosInfinity() const { return isInfinity() && !isNegative(); }
1445 bool isNegInfinity() const { return isInfinity() && isNegative(); }
1449
1453
1454 /// Return the FPClassTest which will return true for the value.
1456
1457 APFloat &operator=(const APFloat &RHS) = default;
1458 APFloat &operator=(APFloat &&RHS) = default;
1459
1460 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
1461 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
1463 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1464 }
1465
1466 LLVM_ABI void print(raw_ostream &) const;
1467
1468#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1469 LLVM_DUMP_METHOD void dump() const;
1470#endif
1471
1472 /// If this value is normal and has an exact, normal, multiplicative inverse,
1473 /// store it in inv and return true.
1474 bool getExactInverse(APFloat *Inv) const;
1475
1476 // If this is an exact power of two, return the exponent while ignoring the
1477 // sign bit. If it's not an exact power of 2, return INT_MIN
1482
1483 // If this is an exact power of two, return the exponent. If it's not an exact
1484 // power of 2, return INT_MIN
1486 int getExactLog2() const {
1487 return isNegative() ? INT_MIN : getExactLog2Abs();
1488 }
1489
1490 LLVM_ABI friend hash_code hash_value(const APFloat &Arg);
1491 friend int ilogb(const APFloat &Arg);
1492 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
1493 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
1494 friend IEEEFloat;
1495 friend DoubleAPFloat;
1496};
1497
1498static_assert(sizeof(APFloat) == sizeof(detail::IEEEFloat),
1499 "Empty base class optimization is not performed.");
1500
1501/// See friend declarations above.
1502///
1503/// These additional declarations are required in order to compile LLVM with IBM
1504/// xlC compiler.
1506
1507/// Returns the exponent of the internal representation of the APFloat.
1508///
1509/// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
1510/// For special APFloat values, this returns special error codes:
1511///
1512/// NaN -> \c IEK_NaN
1513/// 0 -> \c IEK_Zero
1514/// Inf -> \c IEK_Inf
1515///
1516inline int ilogb(const APFloat &Arg) {
1517 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics()))
1518 return ilogb(Arg.U.IEEE);
1519 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics()))
1520 return ilogb(Arg.U.Double);
1521 llvm_unreachable("Unexpected semantics");
1522}
1523
1524/// Returns: X * 2^Exp for integral exponents.
1526 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1527 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
1528 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1529 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
1530 llvm_unreachable("Unexpected semantics");
1531}
1532
1533/// Equivalent of C standard library function.
1534///
1535/// While the C standard says Exp is an unspecified value for infinity and nan,
1536/// this returns INT_MAX for infinities, and INT_MIN for NaNs.
1537inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
1538 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1539 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
1540 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1541 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
1542 llvm_unreachable("Unexpected semantics");
1543}
1544/// Returns the absolute value of the argument.
1546 X.clearSign();
1547 return X;
1548}
1549
1550/// Returns the negated value of the argument.
1552 X.changeSign();
1553 return X;
1554}
1555
1556/// Implements IEEE-754 2008 minNum semantics. Returns the smaller of the
1557/// 2 arguments if both are not NaN. If either argument is a qNaN, returns the
1558/// other argument. If either argument is sNaN, return a qNaN.
1559/// -0 is treated as ordered less than +0.
1561inline APFloat minnum(const APFloat &A, const APFloat &B) {
1562 if (A.isSignaling())
1563 return A.makeQuiet();
1564 if (B.isSignaling())
1565 return B.makeQuiet();
1566 if (A.isNaN())
1567 return B;
1568 if (B.isNaN())
1569 return A;
1570 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1571 return A.isNegative() ? A : B;
1572 return B < A ? B : A;
1573}
1574
1575/// Implements IEEE-754 2008 maxNum semantics. Returns the larger of the
1576/// 2 arguments if both are not NaN. If either argument is a qNaN, returns the
1577/// other argument. If either argument is sNaN, return a qNaN.
1578/// +0 is treated as ordered greater than -0.
1580inline APFloat maxnum(const APFloat &A, const APFloat &B) {
1581 if (A.isSignaling())
1582 return A.makeQuiet();
1583 if (B.isSignaling())
1584 return B.makeQuiet();
1585 if (A.isNaN())
1586 return B;
1587 if (B.isNaN())
1588 return A;
1589 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1590 return A.isNegative() ? B : A;
1591 return A < B ? B : A;
1592}
1593
1594/// Implements IEEE 754-2019 minimum semantics. Returns the smaller of 2
1595/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1596/// as less than +0.
1598inline APFloat minimum(const APFloat &A, const APFloat &B) {
1599 if (A.isNaN())
1600 return A.makeQuiet();
1601 if (B.isNaN())
1602 return B.makeQuiet();
1603 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1604 return A.isNegative() ? A : B;
1605 return B < A ? B : A;
1606}
1607
1608/// Implements IEEE 754-2019 minimumNumber semantics. Returns the smaller
1609/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1611inline APFloat minimumnum(const APFloat &A, const APFloat &B) {
1612 if (A.isNaN())
1613 return B.isNaN() ? B.makeQuiet() : B;
1614 if (B.isNaN())
1615 return A;
1616 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1617 return A.isNegative() ? A : B;
1618 return B < A ? B : A;
1619}
1620
1621/// Implements IEEE 754-2019 maximum semantics. Returns the larger of 2
1622/// arguments, returning a quiet NaN if an argument is a NaN and treating -0
1623/// as less than +0.
1625inline APFloat maximum(const APFloat &A, const APFloat &B) {
1626 if (A.isNaN())
1627 return A.makeQuiet();
1628 if (B.isNaN())
1629 return B.makeQuiet();
1630 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1631 return A.isNegative() ? B : A;
1632 return A < B ? B : A;
1633}
1634
1635/// Implements IEEE 754-2019 maximumNumber semantics. Returns the larger
1636/// of 2 arguments, not propagating NaNs and treating -0 as less than +0.
1638inline APFloat maximumnum(const APFloat &A, const APFloat &B) {
1639 if (A.isNaN())
1640 return B.isNaN() ? B.makeQuiet() : B;
1641 if (B.isNaN())
1642 return A;
1643 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1644 return A.isNegative() ? B : A;
1645 return A < B ? B : A;
1646}
1647
1649 V.print(OS);
1650 return OS;
1651}
1652
1653// We want the following functions to be available in the header for inlining.
1654// We cannot define them inline in the class definition of `DoubleAPFloat`
1655// because doing so would instantiate `std::unique_ptr<APFloat[]>` before
1656// `APFloat` is defined, and that would be undefined behavior.
1657namespace detail {
1658
1660 if (this != &RHS) {
1661 this->~DoubleAPFloat();
1662 new (this) DoubleAPFloat(std::move(RHS));
1663 }
1664 return *this;
1665}
1666
1667APFloat &DoubleAPFloat::getFirst() { return Floats[0]; }
1668const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; }
1669APFloat &DoubleAPFloat::getSecond() { return Floats[1]; }
1670const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; }
1671
1672inline DoubleAPFloat::~DoubleAPFloat() { delete[] Floats; }
1673
1674} // namespace detail
1675
1676} // namespace llvm
1677
1678#undef APFLOAT_DISPATCH_ON_SEMANTICS
1679#endif // LLVM_ADT_APFLOAT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#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_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 const fltSemantics & IEEEsingle()
Definition APFloat.h:296
static const fltSemantics & Float8E4M3FN()
Definition APFloat.h:306
static LLVM_ABI const llvm::fltSemantics & EnumToSemantics(Semantics S)
Definition APFloat.cpp:174
static LLVM_ABI bool semanticsHasInf(const fltSemantics &)
Definition APFloat.cpp:329
static constexpr roundingMode rmNearestTiesToAway
Definition APFloat.h:349
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:304
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition APFloat.h:342
static const fltSemantics & BFloat()
Definition APFloat.h:295
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static LLVM_ABI unsigned int semanticsSizeInBits(const fltSemantics &)
Definition APFloat.cpp:307
static const fltSemantics & Float8E8M0FNU()
Definition APFloat.h:313
static LLVM_ABI bool semanticsHasSignedRepr(const fltSemantics &)
Definition APFloat.cpp:325
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
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:360
static const fltSemantics & x87DoubleExtended()
Definition APFloat.h:317
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
Definition APFloat.h:378
static LLVM_ABI bool hasSignBitInMSB(const fltSemantics &)
Definition APFloat.cpp:342
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:300
friend class APFloat
Definition APFloat.h:291
static const fltSemantics & Bogus()
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real.
Definition APFloat.h:323
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:296
static LLVM_ABI bool semanticsHasNaN(const fltSemantics &)
Definition APFloat.cpp:333
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:221
static constexpr unsigned integerPartWidth
Definition APFloat.h:152
static const fltSemantics & PPCDoubleDoubleLegacy()
Definition APFloat.h:300
APInt::WordType integerPart
Definition APFloat.h:151
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static LLVM_ABI bool semanticsHasZero(const fltSemantics &)
Definition APFloat.cpp:321
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:346
static const fltSemantics & Float8E5M2FNUZ()
Definition APFloat.h:304
static const fltSemantics & Float8E4M3FNUZ()
Definition APFloat.h:307
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
static const fltSemantics & Float4E2M1FN()
Definition APFloat.h:316
static const fltSemantics & Float6E2M3FN()
Definition APFloat.h:315
IlogbErrorKinds
Enumeration of ilogb error results.
Definition APFloat.h:383
static const fltSemantics & Float8E4M3()
Definition APFloat.h:305
static const fltSemantics & Float8E4M3B11FNUZ()
Definition APFloat.h:308
static LLVM_ABI bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B)
Definition APFloat.cpp:266
static const fltSemantics & Float8E3M4()
Definition APFloat.h:311
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:337
static const fltSemantics & Float8E5M2()
Definition APFloat.h:303
fltCategory
Category of internally-represented number.
Definition APFloat.h:370
static const fltSemantics & PPCDoubleDouble()
Definition APFloat.h:299
static const fltSemantics & Float6E3M2FN()
Definition APFloat.h:314
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static const fltSemantics & FloatTF32()
Definition APFloat.h:312
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
Definition APFloat.h:155
static LLVM_ABI unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
Definition APFloat.cpp:310
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1102
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition APFloat.h:1110
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
APFloat & operator=(APFloat &&RHS)=default
bool isFiniteNonZero() const
Definition APFloat.h:1441
APFloat(const APFloat &RHS)=default
void copySign(const APFloat &RHS)
Definition APFloat.h:1284
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6060
LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.h:1479
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1172
bool bitwiseIsEqual(const APFloat &RHS) const
Definition APFloat.h:1396
bool isNegative() const
Definition APFloat.h:1431
~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:6002
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
Definition APFloat.h:1249
friend DoubleAPFloat
Definition APFloat.h:1495
LLVM_ABI double convertToDouble() const
Converts this APFloat to host double value.
Definition APFloat.cpp:6119
bool isPosInfinity() const
Definition APFloat.h:1444
APFloat(APFloat &&RHS)=default
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition APFloat.h:1460
bool isNormal() const
Definition APFloat.h:1435
bool isDenormal() const
Definition APFloat.h:1432
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:1414
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1163
LLVM_READONLY int getExactLog2() const
Definition APFloat.h:1486
APFloat(double d)
Definition APFloat.h:1049
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:6086
LLVM_ABI friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition APFloat.cpp:5974
APFloat(const fltSemantics &Semantics, integerPart I)
Definition APFloat.h:1041
bool operator!=(const APFloat &RHS) const
Definition APFloat.h:1364
APFloat(const fltSemantics &Semantics, T V)=delete
const fltSemantics & getSemantics() const
Definition APFloat.h:1439
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
Definition APFloat.h:1257
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
Definition APFloat.h:1265
APFloat(const fltSemantics &Semantics)
Definition APFloat.h:1039
bool isNonZero() const
Definition APFloat.h:1440
void clearSign()
Definition APFloat.h:1280
bool operator<(const APFloat &RHS) const
Definition APFloat.h:1366
bool isFinite() const
Definition APFloat.h:1436
APFloat makeQuiet() const
Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
Definition APFloat.h:1298
bool isNaN() const
Definition APFloat.h:1429
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:1070
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition APFloat.h:1421
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
LLVM_ABI float convertToFloat() const
Converts this APFloat to host float value.
Definition APFloat.cpp:6150
bool isSignaling() const
Definition APFloat.h:1433
bool operator>(const APFloat &RHS) const
Definition APFloat.h:1370
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1217
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
Definition APFloat.h:1273
opStatus remainder(const APFloat &RHS)
Definition APFloat.h:1199
APFloat operator-() const
Negate an APFloat.
Definition APFloat.h:1241
bool isZero() const
Definition APFloat.h:1427
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.h:1140
APInt bitcastToAPInt() const
Definition APFloat.h:1335
bool isLargest() const
Definition APFloat.h:1447
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
bool isSmallest() const
Definition APFloat.h:1446
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1314
opStatus next(bool nextDown)
Definition APFloat.h:1236
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
bool operator>=(const APFloat &RHS) const
Definition APFloat.h:1379
bool needsCleanup() const
Definition APFloat.h:1056
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition APFloat.h:1130
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition APFloat.cpp:5989
bool operator==(const APFloat &RHS) const
Definition APFloat.h:1362
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1208
bool isPosZero() const
Definition APFloat.h:1442
friend int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1516
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:5969
fltCategory getCategory() const
Definition APFloat.h:1438
APFloat(const fltSemantics &Semantics, uninitializedTag)
Definition APFloat.h:1046
bool isInteger() const
Definition APFloat.h:1448
bool isNegInfinity() const
Definition APFloat.h:1445
friend IEEEFloat
Definition APFloat.h:1494
LLVM_DUMP_METHOD void dump() const
Definition APFloat.cpp:6097
bool isNegZero() const
Definition APFloat.h:1443
LLVM_ABI void print(raw_ostream &) const
Definition APFloat.cpp:6090
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:1291
APFloat(float f)
Definition APFloat.h:1050
opStatus roundToIntegral(roundingMode RM)
Definition APFloat.h:1230
void changeSign()
Definition APFloat.h:1279
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1091
static bool hasSignificand(const fltSemantics &Sem)
Returns true if the given semantics has actual significand.
Definition APFloat.h:1155
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1061
cmpResult compare(const APFloat &RHS) const
Definition APFloat.h:1386
bool isSmallestNormalized() const
Definition APFloat.h:1450
APFloat(const fltSemantics &Semantics, const APInt &I)
Definition APFloat.h:1048
bool isInfinity() const
Definition APFloat.h:1428
bool operator<=(const APFloat &RHS) const
Definition APFloat.h:1374
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:5316
LLVM_ABI DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
Definition APFloat.cpp:4846
LLVM_ABI void changeSign()
Definition APFloat.cpp:5223
LLVM_ABI bool isLargest() const
Definition APFloat.cpp:5791
LLVM_ABI opStatus remainder(const DoubleAPFloat &RHS)
Definition APFloat.cpp:5110
LLVM_ABI opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5013
LLVM_ABI fltCategory getCategory() const
Definition APFloat.cpp:5282
LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5339
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:5742
LLVM_ABI APInt bitcastToAPInt() const
Definition APFloat.cpp:5350
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:5360
LLVM_ABI bool isSmallest() const
Definition APFloat.cpp:5774
LLVM_ABI opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5005
LLVM_ABI friend hash_code hash_value(const DoubleAPFloat &Arg)
Definition APFloat.cpp:5344
LLVM_ABI cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5229
LLVM_ABI bool isDenormal() const
Definition APFloat.cpp:5767
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.cpp:5577
LLVM_ABI void makeSmallest(bool Neg)
Definition APFloat.cpp:5309
LLVM_ABI friend int ilogb(const DoubleAPFloat &X)
Definition APFloat.cpp:5824
LLVM_ABI opStatus next(bool nextDown)
Definition APFloat.cpp:5376
LLVM_ABI void makeInf(bool Neg)
Definition APFloat.cpp:5288
LLVM_ABI bool isInteger() const
Definition APFloat.cpp:5799
LLVM_ABI void makeZero(bool Neg)
Definition APFloat.cpp:5293
LLVM_ABI opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5099
LLVM_ABI bool isSmallestNormalized() const
Definition APFloat.cpp:5782
LLVM_ABI opStatus mod(const DoubleAPFloat &RHS)
Definition APFloat.cpp:5120
LLVM_ABI DoubleAPFloat(const fltSemantics &S)
Definition APFloat.cpp:4793
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
Definition APFloat.cpp:5805
LLVM_ABI void makeLargest(bool Neg)
Definition APFloat.cpp:5298
LLVM_ABI cmpResult compare(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5331
LLVM_ABI friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode)
LLVM_ABI opStatus roundToIntegral(roundingMode RM)
Definition APFloat.cpp:5146
LLVM_ABI opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
Definition APFloat.cpp:5131
LLVM_ABI unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition APFloat.cpp:5757
bool needsCleanup() const
Definition APFloat.h:866
LLVM_ABI bool isNegative() const
Definition APFloat.cpp:5286
LLVM_ABI opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5000
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:5326
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:3329
LLVM_ABI cmpResult compareAbsoluteValue(const IEEEFloat &) const
Definition APFloat.cpp:1547
LLVM_ABI opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
Definition APFloat.cpp:2318
fltCategory getCategory() const
Definition APFloat.h:573
LLVM_ABI opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition APFloat.cpp:2887
bool isNonZero() const
Definition APFloat.h:575
bool isFiniteNonZero() const
Definition APFloat.h:576
bool needsCleanup() const
Returns whether this instance allocated memory.
Definition APFloat.h:463
LLVM_ABI void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
Definition APFloat.cpp:4115
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.cpp:4510
LLVM_ABI APInt bitcastToAPInt() const
Definition APFloat.cpp:3739
LLVM_ABI friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition APFloat.cpp:4753
LLVM_ABI cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition APFloat.cpp:2489
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
Definition APFloat.h:538
LLVM_ABI opStatus divide(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2188
LLVM_ABI friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
Definition APFloat.cpp:3477
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
Definition APFloat.h:563
LLVM_ABI opStatus remainder(const IEEEFloat &)
IEEE remainder.
Definition APFloat.cpp:2208
LLVM_ABI double convertToDouble() const
Definition APFloat.cpp:3809
LLVM_ABI float convertToFloat() const
Definition APFloat.cpp:3802
LLVM_ABI opStatus subtract(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2162
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:4466
LLVM_ABI void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
Definition APFloat.cpp:4147
LLVM_ABI void makeInf(bool Neg=false)
Definition APFloat.cpp:4700
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
Definition APFloat.h:544
LLVM_ABI bool isSmallestNormalized() const
Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.cpp:1047
friend class IEEEFloatUnitTestHelper
Definition APFloat.h:816
LLVM_ABI void makeQuiet()
Definition APFloat.cpp:4729
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:1149
LLVM_ABI opStatus add(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2156
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
Definition APFloat.h:550
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:3272
LLVM_ABI void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
Definition APFloat.cpp:936
LLVM_ABI opStatus multiply(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2168
LLVM_ABI opStatus roundToIntegral(roundingMode)
Definition APFloat.cpp:2402
LLVM_ABI IEEEFloat & operator=(const IEEEFloat &)
Definition APFloat.cpp:1008
LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition APFloat.cpp:1174
LLVM_ABI void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.cpp:4161
LLVM_ABI bool isInteger() const
Returns true if and only if the number is an exact integer.
Definition APFloat.cpp:1166
bool isPosZero() const
Definition APFloat.h:577
LLVM_ABI IEEEFloat(const fltSemantics &)
Definition APFloat.cpp:1201
LLVM_ABI opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2356
LLVM_ABI friend int ilogb(const IEEEFloat &Arg)
Definition APFloat.cpp:4735
LLVM_ABI opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
Definition APFloat.cpp:4555
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
Definition APFloat.h:560
const fltSemantics & getSemantics() const
Definition APFloat.h:574
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition APFloat.h:553
LLVM_ABI bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
Definition APFloat.cpp:4539
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:4715
LLVM_ABI opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
Definition APFloat.cpp:2566
LLVM_ABI void changeSign()
Definition APFloat.cpp:2112
LLVM_ABI bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
Definition APFloat.cpp:1033
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
Definition APFloat.cpp:2827
LLVM_ABI friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
Definition APFloat.cpp:4774
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:1039
bool isNegZero() const
Definition APFloat.h:578
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:439
static constexpr fltCategory fcNaN
Definition APFloat.h:441
static constexpr opStatus opDivByZero
Definition APFloat.h:436
static constexpr opStatus opOverflow
Definition APFloat.h:437
static constexpr cmpResult cmpLessThan
Definition APFloat.h:431
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:427
static constexpr uninitializedTag uninitialized
Definition APFloat.h:421
static constexpr fltCategory fcZero
Definition APFloat.h:443
static constexpr opStatus opOK
Definition APFloat.h:434
static constexpr cmpResult cmpGreaterThan
Definition APFloat.h:432
static constexpr unsigned integerPartWidth
Definition APFloat.h:429
LLVM_ABI hash_code hash_value(const IEEEFloat &Arg)
Definition APFloat.cpp:3477
APFloatBase::ExponentType ExponentType
Definition APFloat.h:420
APFloatBase::fltCategory fltCategory
Definition APFloat.h:419
static constexpr fltCategory fcNormal
Definition APFloat.h:442
static constexpr opStatus opInvalidOp
Definition APFloat.h:435
APFloatBase::opStatus opStatus
Definition APFloat.h:417
LLVM_ABI IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM)
Definition APFloat.cpp:4774
APFloatBase::uninitializedTag uninitializedTag
Definition APFloat.h:415
static constexpr cmpResult cmpUnordered
Definition APFloat.h:433
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:426
APFloatBase::roundingMode roundingMode
Definition APFloat.h:416
APFloatBase::cmpResult cmpResult
Definition APFloat.h:418
static constexpr fltCategory fcInfinity
Definition APFloat.h:440
static constexpr roundingMode rmNearestTiesToAway
Definition APFloat.h:424
static constexpr roundingMode rmTowardZero
Definition APFloat.h:428
static constexpr opStatus opUnderflow
Definition APFloat.h:438
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:422
LLVM_ABI int ilogb(const IEEEFloat &Arg)
Definition APFloat.cpp:4735
static constexpr cmpResult cmpEqual
Definition APFloat.h:430
LLVM_ABI IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition APFloat.cpp:4753
APFloatBase::integerPart integerPart
Definition APFloat.h:414
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:1745
hash_code hash_value(const FixedPointSemantics &Val)
static constexpr APFloatBase::ExponentType exponentZero(const fltSemantics &semantics)
Definition APFloat.cpp:365
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition APFloat.h:1545
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1625
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1516
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1537
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1580
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:1611
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:1525
static constexpr APFloatBase::ExponentType exponentNaN(const fltSemantics &semantics)
Definition APFloat.cpp:375
@ 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:1561
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:370
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition APFloat.h:1551
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1598
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1638