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