LLVM 18.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
773
774} // End detail namespace
775
776// This is a interface class that is currently forwarding functionalities from
777// detail::IEEEFloat.
778class APFloat : public APFloatBase {
781
782 static_assert(std::is_standard_layout<IEEEFloat>::value);
783
784 union Storage {
785 const fltSemantics *semantics;
787 DoubleAPFloat Double;
788
789 explicit Storage(IEEEFloat F, const fltSemantics &S);
790 explicit Storage(DoubleAPFloat F, const fltSemantics &S)
791 : Double(std::move(F)) {
792 assert(&S == &PPCDoubleDouble());
793 }
794
795 template <typename... ArgTypes>
796 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) {
797 if (usesLayout<IEEEFloat>(Semantics)) {
798 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...);
799 return;
800 }
801 if (usesLayout<DoubleAPFloat>(Semantics)) {
802 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...);
803 return;
804 }
805 llvm_unreachable("Unexpected semantics");
806 }
807
808 ~Storage() {
809 if (usesLayout<IEEEFloat>(*semantics)) {
810 IEEE.~IEEEFloat();
811 return;
812 }
813 if (usesLayout<DoubleAPFloat>(*semantics)) {
814 Double.~DoubleAPFloat();
815 return;
816 }
817 llvm_unreachable("Unexpected semantics");
818 }
819
820 Storage(const Storage &RHS) {
821 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
822 new (this) IEEEFloat(RHS.IEEE);
823 return;
824 }
825 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
826 new (this) DoubleAPFloat(RHS.Double);
827 return;
828 }
829 llvm_unreachable("Unexpected semantics");
830 }
831
832 Storage(Storage &&RHS) {
833 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
834 new (this) IEEEFloat(std::move(RHS.IEEE));
835 return;
836 }
837 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
838 new (this) DoubleAPFloat(std::move(RHS.Double));
839 return;
840 }
841 llvm_unreachable("Unexpected semantics");
842 }
843
844 Storage &operator=(const Storage &RHS) {
845 if (usesLayout<IEEEFloat>(*semantics) &&
846 usesLayout<IEEEFloat>(*RHS.semantics)) {
847 IEEE = RHS.IEEE;
848 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
849 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
850 Double = RHS.Double;
851 } else if (this != &RHS) {
852 this->~Storage();
853 new (this) Storage(RHS);
854 }
855 return *this;
856 }
857
858 Storage &operator=(Storage &&RHS) {
859 if (usesLayout<IEEEFloat>(*semantics) &&
860 usesLayout<IEEEFloat>(*RHS.semantics)) {
861 IEEE = std::move(RHS.IEEE);
862 } else if (usesLayout<DoubleAPFloat>(*semantics) &&
863 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
864 Double = std::move(RHS.Double);
865 } else if (this != &RHS) {
866 this->~Storage();
867 new (this) Storage(std::move(RHS));
868 }
869 return *this;
870 }
871 } U;
872
873 template <typename T> static bool usesLayout(const fltSemantics &Semantics) {
874 static_assert(std::is_same<T, IEEEFloat>::value ||
875 std::is_same<T, DoubleAPFloat>::value);
876 if (std::is_same<T, DoubleAPFloat>::value) {
877 return &Semantics == &PPCDoubleDouble();
878 }
879 return &Semantics != &PPCDoubleDouble();
880 }
881
882 IEEEFloat &getIEEE() {
883 if (usesLayout<IEEEFloat>(*U.semantics))
884 return U.IEEE;
885 if (usesLayout<DoubleAPFloat>(*U.semantics))
886 return U.Double.getFirst().U.IEEE;
887 llvm_unreachable("Unexpected semantics");
888 }
889
890 const IEEEFloat &getIEEE() const {
891 if (usesLayout<IEEEFloat>(*U.semantics))
892 return U.IEEE;
893 if (usesLayout<DoubleAPFloat>(*U.semantics))
894 return U.Double.getFirst().U.IEEE;
895 llvm_unreachable("Unexpected semantics");
896 }
897
898 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); }
899
900 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); }
901
902 void makeNaN(bool SNaN, bool Neg, const APInt *fill) {
903 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill));
904 }
905
906 void makeLargest(bool Neg) {
907 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg));
908 }
909
910 void makeSmallest(bool Neg) {
911 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg));
912 }
913
914 void makeSmallestNormalized(bool Neg) {
915 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg));
916 }
917
918 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {}
919 explicit APFloat(DoubleAPFloat F, const fltSemantics &S)
920 : U(std::move(F), S) {}
921
922 cmpResult compareAbsoluteValue(const APFloat &RHS) const {
923 assert(&getSemantics() == &RHS.getSemantics() &&
924 "Should only compare APFloats with the same semantics");
925 if (usesLayout<IEEEFloat>(getSemantics()))
926 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
927 if (usesLayout<DoubleAPFloat>(getSemantics()))
928 return U.Double.compareAbsoluteValue(RHS.U.Double);
929 llvm_unreachable("Unexpected semantics");
930 }
931
932public:
936 template <typename T,
937 typename = std::enable_if_t<std::is_floating_point<T>::value>>
938 APFloat(const fltSemantics &Semantics, T V) = delete;
939 // TODO: Remove this constructor. This isn't faster than the first one.
941 : U(Semantics, uninitialized) {}
942 APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {}
943 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
944 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
945 APFloat(const APFloat &RHS) = default;
946 APFloat(APFloat &&RHS) = default;
947
948 ~APFloat() = default;
949
951
952 /// Factory for Positive and Negative Zero.
953 ///
954 /// \param Negative True iff the number should be negative.
955 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
956 APFloat Val(Sem, uninitialized);
957 Val.makeZero(Negative);
958 return Val;
959 }
960
961 /// Factory for Positive and Negative Infinity.
962 ///
963 /// \param Negative True iff the number should be negative.
964 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
965 APFloat Val(Sem, uninitialized);
966 Val.makeInf(Negative);
967 return Val;
968 }
969
970 /// Factory for NaN values.
971 ///
972 /// \param Negative - True iff the NaN generated should be negative.
973 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
974 /// default. The value is truncated as necessary.
975 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
976 uint64_t payload = 0) {
977 if (payload) {
978 APInt intPayload(64, payload);
979 return getQNaN(Sem, Negative, &intPayload);
980 } else {
981 return getQNaN(Sem, Negative, nullptr);
982 }
983 }
984
985 /// Factory for QNaN values.
986 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
987 const APInt *payload = nullptr) {
988 APFloat Val(Sem, uninitialized);
989 Val.makeNaN(false, Negative, payload);
990 return Val;
991 }
992
993 /// Factory for SNaN values.
994 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
995 const APInt *payload = nullptr) {
996 APFloat Val(Sem, uninitialized);
997 Val.makeNaN(true, Negative, payload);
998 return Val;
999 }
1000
1001 /// Returns the largest finite number in the given semantics.
1002 ///
1003 /// \param Negative - True iff the number should be negative
1004 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) {
1005 APFloat Val(Sem, uninitialized);
1006 Val.makeLargest(Negative);
1007 return Val;
1008 }
1009
1010 /// Returns the smallest (by magnitude) finite number in the given semantics.
1011 /// Might be denormalized, which implies a relative loss of precision.
1012 ///
1013 /// \param Negative - True iff the number should be negative
1014 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) {
1015 APFloat Val(Sem, uninitialized);
1016 Val.makeSmallest(Negative);
1017 return Val;
1018 }
1019
1020 /// Returns the smallest (by magnitude) normalized finite number in the given
1021 /// semantics.
1022 ///
1023 /// \param Negative - True iff the number should be negative
1025 bool Negative = false) {
1026 APFloat Val(Sem, uninitialized);
1027 Val.makeSmallestNormalized(Negative);
1028 return Val;
1029 }
1030
1031 /// Returns a float which is bitcasted from an all one value int.
1032 ///
1033 /// \param Semantics - type float semantics
1035
1036 /// Used to insert APFloat objects, or objects that contain APFloat objects,
1037 /// into FoldingSets.
1038 void Profile(FoldingSetNodeID &NID) const;
1039
1041 assert(&getSemantics() == &RHS.getSemantics() &&
1042 "Should only call on two APFloats with the same semantics");
1043 if (usesLayout<IEEEFloat>(getSemantics()))
1044 return U.IEEE.add(RHS.U.IEEE, RM);
1045 if (usesLayout<DoubleAPFloat>(getSemantics()))
1046 return U.Double.add(RHS.U.Double, RM);
1047 llvm_unreachable("Unexpected semantics");
1048 }
1050 assert(&getSemantics() == &RHS.getSemantics() &&
1051 "Should only call on two APFloats with the same semantics");
1052 if (usesLayout<IEEEFloat>(getSemantics()))
1053 return U.IEEE.subtract(RHS.U.IEEE, RM);
1054 if (usesLayout<DoubleAPFloat>(getSemantics()))
1055 return U.Double.subtract(RHS.U.Double, RM);
1056 llvm_unreachable("Unexpected semantics");
1057 }
1059 assert(&getSemantics() == &RHS.getSemantics() &&
1060 "Should only call on two APFloats with the same semantics");
1061 if (usesLayout<IEEEFloat>(getSemantics()))
1062 return U.IEEE.multiply(RHS.U.IEEE, RM);
1063 if (usesLayout<DoubleAPFloat>(getSemantics()))
1064 return U.Double.multiply(RHS.U.Double, RM);
1065 llvm_unreachable("Unexpected semantics");
1066 }
1068 assert(&getSemantics() == &RHS.getSemantics() &&
1069 "Should only call on two APFloats with the same semantics");
1070 if (usesLayout<IEEEFloat>(getSemantics()))
1071 return U.IEEE.divide(RHS.U.IEEE, RM);
1072 if (usesLayout<DoubleAPFloat>(getSemantics()))
1073 return U.Double.divide(RHS.U.Double, RM);
1074 llvm_unreachable("Unexpected semantics");
1075 }
1077 assert(&getSemantics() == &RHS.getSemantics() &&
1078 "Should only call on two APFloats with the same semantics");
1079 if (usesLayout<IEEEFloat>(getSemantics()))
1080 return U.IEEE.remainder(RHS.U.IEEE);
1081 if (usesLayout<DoubleAPFloat>(getSemantics()))
1082 return U.Double.remainder(RHS.U.Double);
1083 llvm_unreachable("Unexpected semantics");
1084 }
1086 assert(&getSemantics() == &RHS.getSemantics() &&
1087 "Should only call on two APFloats with the same semantics");
1088 if (usesLayout<IEEEFloat>(getSemantics()))
1089 return U.IEEE.mod(RHS.U.IEEE);
1090 if (usesLayout<DoubleAPFloat>(getSemantics()))
1091 return U.Double.mod(RHS.U.Double);
1092 llvm_unreachable("Unexpected semantics");
1093 }
1094 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend,
1095 roundingMode RM) {
1096 assert(&getSemantics() == &Multiplicand.getSemantics() &&
1097 "Should only call on APFloats with the same semantics");
1098 assert(&getSemantics() == &Addend.getSemantics() &&
1099 "Should only call on APFloats with the same semantics");
1100 if (usesLayout<IEEEFloat>(getSemantics()))
1101 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1102 if (usesLayout<DoubleAPFloat>(getSemantics()))
1103 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1104 RM);
1105 llvm_unreachable("Unexpected semantics");
1106 }
1109 }
1110
1111 // TODO: bool parameters are not readable and a source of bugs.
1112 // Do something.
1113 opStatus next(bool nextDown) {
1115 }
1116
1117 /// Negate an APFloat.
1119 APFloat Result(*this);
1120 Result.changeSign();
1121 return Result;
1122 }
1123
1124 /// Add two APFloats, rounding ties to the nearest even.
1125 /// No error checking.
1127 APFloat Result(*this);
1128 (void)Result.add(RHS, rmNearestTiesToEven);
1129 return Result;
1130 }
1131
1132 /// Subtract two APFloats, rounding ties to the nearest even.
1133 /// No error checking.
1135 APFloat Result(*this);
1136 (void)Result.subtract(RHS, rmNearestTiesToEven);
1137 return Result;
1138 }
1139
1140 /// Multiply two APFloats, rounding ties to the nearest even.
1141 /// No error checking.
1143 APFloat Result(*this);
1144 (void)Result.multiply(RHS, rmNearestTiesToEven);
1145 return Result;
1146 }
1147
1148 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1149 /// No error checking.
1151 APFloat Result(*this);
1152 (void)Result.divide(RHS, rmNearestTiesToEven);
1153 return Result;
1154 }
1155
1157 void clearSign() {
1158 if (isNegative())
1159 changeSign();
1160 }
1161 void copySign(const APFloat &RHS) {
1162 if (isNegative() != RHS.isNegative())
1163 changeSign();
1164 }
1165
1166 /// A static helper to produce a copy of an APFloat value with its sign
1167 /// copied from some other APFloat.
1168 static APFloat copySign(APFloat Value, const APFloat &Sign) {
1169 Value.copySign(Sign);
1170 return Value;
1171 }
1172
1173 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
1174 /// This preserves the sign and payload bits.
1176 APFloat Result(*this);
1177 Result.getIEEE().makeQuiet();
1178 return Result;
1179 }
1180
1181 opStatus convert(const fltSemantics &ToSemantics, roundingMode RM,
1182 bool *losesInfo);
1184 unsigned int Width, bool IsSigned, roundingMode RM,
1185 bool *IsExact) const {
1187 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1188 }
1190 bool *IsExact) const;
1191 opStatus convertFromAPInt(const APInt &Input, bool IsSigned,
1192 roundingMode RM) {
1193 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input, IsSigned, RM));
1194 }
1196 unsigned int InputSize, bool IsSigned,
1197 roundingMode RM) {
1199 convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM));
1200 }
1202 unsigned int InputSize, bool IsSigned,
1203 roundingMode RM) {
1205 convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
1206 }
1210 }
1211
1212 /// Converts this APFloat to host double value.
1213 ///
1214 /// \pre The APFloat must be built using semantics, that can be represented by
1215 /// the host double type without loss of precision. It can be IEEEdouble and
1216 /// shorter semantics, like IEEEsingle and others.
1217 double convertToDouble() const;
1218
1219 /// Converts this APFloat to host float value.
1220 ///
1221 /// \pre The APFloat must be built using semantics, that can be represented by
1222 /// the host float type without loss of precision. It can be IEEEsingle and
1223 /// shorter semantics, like IEEEhalf.
1224 float convertToFloat() const;
1225
1226 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; }
1227
1228 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; }
1229
1230 bool operator<(const APFloat &RHS) const {
1231 return compare(RHS) == cmpLessThan;
1232 }
1233
1234 bool operator>(const APFloat &RHS) const {
1235 return compare(RHS) == cmpGreaterThan;
1236 }
1237
1238 bool operator<=(const APFloat &RHS) const {
1239 cmpResult Res = compare(RHS);
1240 return Res == cmpLessThan || Res == cmpEqual;
1241 }
1242
1243 bool operator>=(const APFloat &RHS) const {
1244 cmpResult Res = compare(RHS);
1245 return Res == cmpGreaterThan || Res == cmpEqual;
1246 }
1247
1249 assert(&getSemantics() == &RHS.getSemantics() &&
1250 "Should only compare APFloats with the same semantics");
1251 if (usesLayout<IEEEFloat>(getSemantics()))
1252 return U.IEEE.compare(RHS.U.IEEE);
1253 if (usesLayout<DoubleAPFloat>(getSemantics()))
1254 return U.Double.compare(RHS.U.Double);
1255 llvm_unreachable("Unexpected semantics");
1256 }
1257
1258 bool bitwiseIsEqual(const APFloat &RHS) const {
1259 if (&getSemantics() != &RHS.getSemantics())
1260 return false;
1261 if (usesLayout<IEEEFloat>(getSemantics()))
1262 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1263 if (usesLayout<DoubleAPFloat>(getSemantics()))
1264 return U.Double.bitwiseIsEqual(RHS.U.Double);
1265 llvm_unreachable("Unexpected semantics");
1266 }
1267
1268 /// We don't rely on operator== working on double values, as
1269 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1270 /// As such, this method can be used to do an exact bit-for-bit comparison of
1271 /// two floating point values.
1272 ///
1273 /// We leave the version with the double argument here because it's just so
1274 /// convenient to write "2.0" and the like. Without this function we'd
1275 /// have to duplicate its logic everywhere it's called.
1276 bool isExactlyValue(double V) const {
1277 bool ignored;
1278 APFloat Tmp(V);
1280 return bitwiseIsEqual(Tmp);
1281 }
1282
1283 unsigned int convertToHexString(char *DST, unsigned int HexDigits,
1284 bool UpperCase, roundingMode RM) const {
1286 convertToHexString(DST, HexDigits, UpperCase, RM));
1287 }
1288
1289 bool isZero() const { return getCategory() == fcZero; }
1290 bool isInfinity() const { return getCategory() == fcInfinity; }
1291 bool isNaN() const { return getCategory() == fcNaN; }
1292
1293 bool isNegative() const { return getIEEE().isNegative(); }
1295 bool isSignaling() const { return getIEEE().isSignaling(); }
1296
1297 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
1298 bool isFinite() const { return !isNaN() && !isInfinity(); }
1299
1300 fltCategory getCategory() const { return getIEEE().getCategory(); }
1301 const fltSemantics &getSemantics() const { return *U.semantics; }
1302 bool isNonZero() const { return !isZero(); }
1303 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
1304 bool isPosZero() const { return isZero() && !isNegative(); }
1305 bool isNegZero() const { return isZero() && isNegative(); }
1306 bool isPosInfinity() const { return isInfinity() && !isNegative(); }
1307 bool isNegInfinity() const { return isInfinity() && isNegative(); }
1311 bool isIEEE() const { return usesLayout<IEEEFloat>(getSemantics()); }
1312
1315 }
1316
1317 /// Return the FPClassTest which will return true for the value.
1318 FPClassTest classify() const;
1319
1320 APFloat &operator=(const APFloat &RHS) = default;
1322
1323 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
1324 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const {
1326 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1327 }
1328
1329 void print(raw_ostream &) const;
1330 void dump() const;
1331
1332 bool getExactInverse(APFloat *inv) const {
1334 }
1335
1337 int getExactLog2Abs() const {
1339 }
1340
1342 int getExactLog2() const {
1344 }
1345
1346 friend hash_code hash_value(const APFloat &Arg);
1347 friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); }
1348 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
1349 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);
1352};
1353
1354/// See friend declarations above.
1355///
1356/// These additional declarations are required in order to compile LLVM with IBM
1357/// xlC compiler.
1358hash_code hash_value(const APFloat &Arg);
1360 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1361 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics());
1362 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1363 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics());
1364 llvm_unreachable("Unexpected semantics");
1365}
1366
1367/// Equivalent of C standard library function.
1368///
1369/// While the C standard says Exp is an unspecified value for infinity and nan,
1370/// this returns INT_MAX for infinities, and INT_MIN for NaNs.
1371inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) {
1372 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics()))
1373 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics());
1374 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics()))
1375 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics());
1376 llvm_unreachable("Unexpected semantics");
1377}
1378/// Returns the absolute value of the argument.
1380 X.clearSign();
1381 return X;
1382}
1383
1384/// Returns the negated value of the argument.
1386 X.changeSign();
1387 return X;
1388}
1389
1390/// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
1391/// both are not NaN. If either argument is a NaN, returns the other argument.
1393inline APFloat minnum(const APFloat &A, const APFloat &B) {
1394 if (A.isNaN())
1395 return B;
1396 if (B.isNaN())
1397 return A;
1398 return B < A ? B : A;
1399}
1400
1401/// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
1402/// both are not NaN. If either argument is a NaN, returns the other argument.
1404inline APFloat maxnum(const APFloat &A, const APFloat &B) {
1405 if (A.isNaN())
1406 return B;
1407 if (B.isNaN())
1408 return A;
1409 return A < B ? B : A;
1410}
1411
1412/// Implements IEEE 754-2018 minimum semantics. Returns the smaller of 2
1413/// arguments, propagating NaNs and treating -0 as less than +0.
1415inline APFloat minimum(const APFloat &A, const APFloat &B) {
1416 if (A.isNaN())
1417 return A;
1418 if (B.isNaN())
1419 return B;
1420 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1421 return A.isNegative() ? A : B;
1422 return B < A ? B : A;
1423}
1424
1425/// Implements IEEE 754-2018 maximum semantics. Returns the larger of 2
1426/// arguments, propagating NaNs and treating -0 as less than +0.
1428inline APFloat maximum(const APFloat &A, const APFloat &B) {
1429 if (A.isNaN())
1430 return A;
1431 if (B.isNaN())
1432 return B;
1433 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative()))
1434 return A.isNegative() ? B : A;
1435 return A < B ? B : A;
1436}
1437
1438// We want the following functions to be available in the header for inlining.
1439// We cannot define them inline in the class definition of `DoubleAPFloat`
1440// because doing so would instantiate `std::unique_ptr<APFloat[]>` before
1441// `APFloat` is defined, and that would be undefined behavior.
1442namespace detail {
1443
1445 if (this != &RHS) {
1446 this->~DoubleAPFloat();
1447 new (this) DoubleAPFloat(std::move(RHS));
1448 }
1449 return *this;
1450}
1451
1452APFloat &DoubleAPFloat::getFirst() { return Floats[0]; }
1453const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; }
1454APFloat &DoubleAPFloat::getSecond() { return Floats[1]; }
1455const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; }
1456
1457} // namespace detail
1458
1459} // namespace llvm
1460
1461#undef APFLOAT_DISPATCH_ON_SEMANTICS
1462#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:201
#define LLVM_READONLY
Definition: Compiler.h:208
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:986
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition: APFloat.h:994
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1067
bool getExactInverse(APFloat *inv) const
Definition: APFloat.h:1332
APFloat & operator=(APFloat &&RHS)=default
bool isFiniteNonZero() const
Definition: APFloat.h:1303
APFloat(const APFloat &RHS)=default
void copySign(const APFloat &RHS)
Definition: APFloat.h:1161
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
LLVM_READONLY int getExactLog2Abs() const
Definition: APFloat.h:1337
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1049
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1258
bool isNegative() const
Definition: APFloat.h:1293
~APFloat()=default
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1126
friend DoubleAPFloat
Definition: APFloat.h:1351
double convertToDouble() const
Converts this APFloat to host double value.
Definition: APFloat.cpp:5255
bool isPosInfinity() const
Definition: APFloat.h:1306
APFloat(APFloat &&RHS)=default
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition: APFloat.h:1323
bool isNormal() const
Definition: APFloat.h:1297
bool isDenormal() const
Definition: APFloat.h:1294
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:1276
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1040
LLVM_READONLY int getExactLog2() const
Definition: APFloat.h:1342
APFloat(double d)
Definition: APFloat.h:943
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:935
bool operator!=(const APFloat &RHS) const
Definition: APFloat.h:1228
APFloat(const fltSemantics &Semantics, T V)=delete
const fltSemantics & getSemantics() const
Definition: APFloat.h:1301
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1134
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
Definition: APFloat.h:1142
APFloat(const fltSemantics &Semantics)
Definition: APFloat.h:933
bool isNonZero() const
Definition: APFloat.h:1302
void clearSign()
Definition: APFloat.h:1157
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1195
bool operator<(const APFloat &RHS) const
Definition: APFloat.h:1230
bool isFinite() const
Definition: APFloat.h:1298
APFloat makeQuiet() const
Assuming this is an IEEE-754 NaN value, quiet its signaling bit.
Definition: APFloat.h:1175
bool isNaN() const
Definition: APFloat.h:1291
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1191
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition: APFloat.h:1283
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1058
float convertToFloat() const
Converts this APFloat to host float value.
Definition: APFloat.cpp:5268
bool isSignaling() const
Definition: APFloat.h:1295
bool operator>(const APFloat &RHS) const
Definition: APFloat.h:1234
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1094
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
Definition: APFloat.h:1150
opStatus remainder(const APFloat &RHS)
Definition: APFloat.h:1076
APFloat operator-() const
Negate an APFloat.
Definition: APFloat.h:1118
bool isZero() const
Definition: APFloat.h:1289
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1024
APInt bitcastToAPInt() const
Definition: APFloat.h:1208
bool isLargest() const
Definition: APFloat.h:1309
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
bool isSmallest() const
Definition: APFloat.h:1308
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1004
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1183
opStatus next(bool nextDown)
Definition: APFloat.h:1113
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:964
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
bool operator>=(const APFloat &RHS) const
Definition: APFloat.h:1243
bool needsCleanup() const
Definition: APFloat.h:950
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition: APFloat.h:1014
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:1226
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1085
bool isPosZero() const
Definition: APFloat.h:1304
friend int ilogb(const APFloat &Arg)
Definition: APFloat.h:1347
Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition: APFloat.cpp:5163
fltCategory getCategory() const
Definition: APFloat.h:1300
APFloat(const fltSemantics &Semantics, uninitializedTag)
Definition: APFloat.h:940
bool isInteger() const
Definition: APFloat.h:1310
bool isNegInfinity() const
Definition: APFloat.h:1307
friend IEEEFloat
Definition: APFloat.h:1350
void dump() const
Definition: APFloat.cpp:5232
bool isNegZero() const
Definition: APFloat.h:1305
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:1168
APFloat(float f)
Definition: APFloat.h:944
opStatus roundToIntegral(roundingMode RM)
Definition: APFloat.h:1107
void changeSign()
Definition: APFloat.h:1156
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:975
bool isIEEE() const
Definition: APFloat.h:1311
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:955
cmpResult compare(const APFloat &RHS) const
Definition: APFloat.h:1248
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1201
bool isSmallestNormalized() const
Definition: APFloat.h:1313
APFloat(const fltSemantics &Semantics, const APInt &I)
Definition: APFloat.h:942
bool isInfinity() const
Definition: APFloat.h:1290
bool operator<=(const APFloat &RHS) const
Definition: APFloat.h:1238
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:318
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:577
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:1379
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
Definition: APFloat.h:1428
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1371
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Definition: APFloat.h:1404
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:1359
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
Definition: APFloat.h:1393
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:1385
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Definition: APFloat.h:1415
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