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