1 //===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
11 /// This file declares a class to represent arbitrary precision floating point
12 /// values and provide a variety of arithmetic operations on them.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_APFLOAT_H
17 #define LLVM_ADT_APFLOAT_H
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/Support/ErrorHandling.h"
24 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
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"); \
41 template <typename T
> class SmallVectorImpl
;
43 /// Enum that represents what fraction of the LSB truncated bits of an fp number
46 /// This essentially combines the roles of guard and sticky bits.
47 enum lostFraction
{ // Example of truncated bits:
48 lfExactlyZero
, // 000000
49 lfLessThanHalf
, // 0xxxxx x's not all zero
50 lfExactlyHalf
, // 100000
51 lfMoreThanHalf
// 1xxxxx x's not all zero
54 /// A self-contained host- and target-independent arbitrary-precision
55 /// floating-point software implementation.
57 /// APFloat uses bignum integer arithmetic as provided by static functions in
58 /// the APInt class. The library will work with bignum integers whose parts are
59 /// any unsigned type at least 16 bits wide, but 64 bits is recommended.
61 /// Written for clarity rather than speed, in particular with a view to use in
62 /// the front-end of a cross compiler so that target arithmetic can be correctly
63 /// performed on the host. Performance should nonetheless be reasonable,
64 /// particularly for its intended use. It may be useful as a base
65 /// implementation for a run-time library during development of a faster
66 /// target-specific one.
68 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
69 /// implemented operations. Currently implemented operations are add, subtract,
70 /// multiply, divide, fused-multiply-add, conversion-to-float,
71 /// conversion-to-integer and conversion-from-integer. New rounding modes
72 /// (e.g. away from zero) can be added with three or four lines of code.
74 /// Four formats are built-in: IEEE single precision, double precision,
75 /// quadruple precision, and x87 80-bit extended double (when operating with
76 /// full extended precision). Adding a new format that obeys IEEE semantics
77 /// only requires adding two lines of code: a declaration and definition of the
80 /// All operations return the status of that operation as an exception bit-mask,
81 /// so multiple operations can be done consecutively with their results or-ed
82 /// together. The returned status can be useful for compiler diagnostics; e.g.,
83 /// inexact, underflow and overflow can be easily diagnosed on constant folding,
84 /// and compiler optimizers can determine what exceptions would be raised by
85 /// folding operations and optimize, or perhaps not optimize, accordingly.
87 /// At present, underflow tininess is detected after rounding; it should be
88 /// straight forward to add support for the before-rounding case too.
90 /// The library reads hexadecimal floating point numbers as per C99, and
91 /// correctly rounds if necessary according to the specified rounding mode.
92 /// Syntax is required to have been validated by the caller. It also converts
93 /// floating point numbers to hexadecimal text as per the C99 %a and %A
94 /// conversions. The output precision (or alternatively the natural minimal
95 /// precision) can be specified; if the requested precision is less than the
96 /// natural precision the output is correctly rounded for the specified rounding
99 /// It also reads decimal floating point numbers and correctly rounds according
100 /// to the specified rounding mode.
102 /// Conversion to decimal text is not currently implemented.
104 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
105 /// signed exponent, and the significand as an array of integer parts. After
106 /// normalization of a number of precision P the exponent is within the range of
107 /// the format, and if the number is not denormal the P-th bit of the
108 /// significand is set as an explicit integer bit. For denormals the most
109 /// significant bit is shifted right so that the exponent is maintained at the
110 /// format's minimum, so that the smallest denormal has just the least
111 /// significant bit of the significand set. The sign of zeroes and infinities
112 /// is significant; the exponent and significand of such numbers is not stored,
113 /// but has a known implicit (deterministic) value: 0 for the significands, 0
114 /// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
115 /// significand are deterministic, although not really meaningful, and preserved
116 /// in non-conversion operations. The exponent is implicitly all 1 bits.
118 /// APFloat does not provide any exception handling beyond default exception
119 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
120 /// by encoding Signaling NaNs with the first bit of its trailing significand as
126 /// Some features that may or may not be worth adding:
128 /// Binary to decimal conversion (hard).
130 /// Optional ability to detect underflow tininess before rounding.
132 /// New formats: x87 in single and double precision mode (IEEE apart from
133 /// extended exponent range) (hard).
135 /// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
138 // This is the common type definitions shared by APFloat and its internal
139 // implementation classes. This struct should not define any non-static data
142 typedef APInt::WordType integerPart
;
143 static const unsigned integerPartWidth
= APInt::APINT_BITS_PER_WORD
;
145 /// A signed type to represent a floating point numbers unbiased exponent.
146 typedef signed short ExponentType
;
148 /// \name Floating Point Semantics.
159 static const llvm::fltSemantics
&EnumToSemantics(Semantics S
);
160 static Semantics
SemanticsToEnum(const llvm::fltSemantics
&Sem
);
162 static const fltSemantics
&IEEEhalf() LLVM_READNONE
;
163 static const fltSemantics
&IEEEsingle() LLVM_READNONE
;
164 static const fltSemantics
&IEEEdouble() LLVM_READNONE
;
165 static const fltSemantics
&IEEEquad() LLVM_READNONE
;
166 static const fltSemantics
&PPCDoubleDouble() LLVM_READNONE
;
167 static const fltSemantics
&x87DoubleExtended() LLVM_READNONE
;
169 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
171 static const fltSemantics
&Bogus() LLVM_READNONE
;
175 /// IEEE-754R 5.11: Floating Point Comparison Relations.
183 /// IEEE-754R 4.3: Rounding-direction attributes.
192 /// IEEE-754R 7: Default exception handling.
194 /// opUnderflow or opOverflow are always returned or-ed with opInexact.
196 /// APFloat models this behavior specified by IEEE-754:
197 /// "For operations producing results in floating-point format, the default
198 /// result of an operation that signals the invalid operation exception
199 /// shall be a quiet NaN."
209 /// Category of internally-represented number.
217 /// Convenience enum used to construct an uninitialized APFloat.
218 enum uninitializedTag
{
222 /// Enumeration of \c ilogb error results.
223 enum IlogbErrorKinds
{
224 IEK_Zero
= INT_MIN
+ 1,
229 static unsigned int semanticsPrecision(const fltSemantics
&);
230 static ExponentType
semanticsMinExponent(const fltSemantics
&);
231 static ExponentType
semanticsMaxExponent(const fltSemantics
&);
232 static unsigned int semanticsSizeInBits(const fltSemantics
&);
234 /// Returns the size of the floating point number (in bits) in the given
236 static unsigned getSizeInBits(const fltSemantics
&Sem
);
241 class IEEEFloat final
: public APFloatBase
{
243 /// \name Constructors
246 IEEEFloat(const fltSemantics
&); // Default construct to 0.0
247 IEEEFloat(const fltSemantics
&, integerPart
);
248 IEEEFloat(const fltSemantics
&, uninitializedTag
);
249 IEEEFloat(const fltSemantics
&, const APInt
&);
250 explicit IEEEFloat(double d
);
251 explicit IEEEFloat(float f
);
252 IEEEFloat(const IEEEFloat
&);
253 IEEEFloat(IEEEFloat
&&);
258 /// Returns whether this instance allocated memory.
259 bool needsCleanup() const { return partCount() > 1; }
261 /// \name Convenience "constructors"
269 opStatus
add(const IEEEFloat
&, roundingMode
);
270 opStatus
subtract(const IEEEFloat
&, roundingMode
);
271 opStatus
multiply(const IEEEFloat
&, roundingMode
);
272 opStatus
divide(const IEEEFloat
&, roundingMode
);
274 opStatus
remainder(const IEEEFloat
&);
275 /// C fmod, or llvm frem.
276 opStatus
mod(const IEEEFloat
&);
277 opStatus
fusedMultiplyAdd(const IEEEFloat
&, const IEEEFloat
&, roundingMode
);
278 opStatus
roundToIntegral(roundingMode
);
279 /// IEEE-754R 5.3.1: nextUp/nextDown.
280 opStatus
next(bool nextDown
);
284 /// \name Sign operations.
291 /// \name Conversions
294 opStatus
convert(const fltSemantics
&, roundingMode
, bool *);
295 opStatus
convertToInteger(MutableArrayRef
<integerPart
>, unsigned int, bool,
296 roundingMode
, bool *) const;
297 opStatus
convertFromAPInt(const APInt
&, bool, roundingMode
);
298 opStatus
convertFromSignExtendedInteger(const integerPart
*, unsigned int,
300 opStatus
convertFromZeroExtendedInteger(const integerPart
*, unsigned int,
302 opStatus
convertFromString(StringRef
, roundingMode
);
303 APInt
bitcastToAPInt() const;
304 double convertToDouble() const;
305 float convertToFloat() const;
309 /// The definition of equality is not straightforward for floating point, so
310 /// we won't use operator==. Use one of the following, or write whatever it
311 /// is you really mean.
312 bool operator==(const IEEEFloat
&) const = delete;
314 /// IEEE comparison with another floating point number (NaNs compare
315 /// unordered, 0==-0).
316 cmpResult
compare(const IEEEFloat
&) const;
318 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
319 bool bitwiseIsEqual(const IEEEFloat
&) const;
321 /// Write out a hexadecimal representation of the floating point value to DST,
322 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
323 /// Return the number of characters written, excluding the terminating NUL.
324 unsigned int convertToHexString(char *dst
, unsigned int hexDigits
,
325 bool upperCase
, roundingMode
) const;
327 /// \name IEEE-754R 5.7.2 General operations.
330 /// IEEE-754R isSignMinus: Returns true if and only if the current value is
333 /// This applies to zeros and NaNs as well.
334 bool isNegative() const { return sign
; }
336 /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
338 /// This implies that the current value of the float is not zero, subnormal,
339 /// infinite, or NaN following the definition of normality from IEEE-754R.
340 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
342 /// Returns true if and only if the current value is zero, subnormal, or
345 /// This means that the value is not infinite or NaN.
346 bool isFinite() const { return !isNaN() && !isInfinity(); }
348 /// Returns true if and only if the float is plus or minus zero.
349 bool isZero() const { return category
== fcZero
; }
351 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
353 bool isDenormal() const;
355 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
356 bool isInfinity() const { return category
== fcInfinity
; }
358 /// Returns true if and only if the float is a quiet or signaling NaN.
359 bool isNaN() const { return category
== fcNaN
; }
361 /// Returns true if and only if the float is a signaling NaN.
362 bool isSignaling() const;
366 /// \name Simple Queries
369 fltCategory
getCategory() const { return category
; }
370 const fltSemantics
&getSemantics() const { return *semantics
; }
371 bool isNonZero() const { return category
!= fcZero
; }
372 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
373 bool isPosZero() const { return isZero() && !isNegative(); }
374 bool isNegZero() const { return isZero() && isNegative(); }
376 /// Returns true if and only if the number has the smallest possible non-zero
377 /// magnitude in the current semantics.
378 bool isSmallest() const;
380 /// Returns true if and only if the number has the largest possible finite
381 /// magnitude in the current semantics.
382 bool isLargest() const;
384 /// Returns true if and only if the number is an exact integer.
385 bool isInteger() const;
389 IEEEFloat
&operator=(const IEEEFloat
&);
390 IEEEFloat
&operator=(IEEEFloat
&&);
392 /// Overload to compute a hash code for an APFloat value.
394 /// Note that the use of hash codes for floating point values is in general
395 /// frought with peril. Equality is hard to define for these values. For
396 /// example, should negative and positive zero hash to different codes? Are
397 /// they equal or not? This hash value implementation specifically
398 /// emphasizes producing different codes for different inputs in order to
399 /// be used in canonicalization and memoization. As such, equality is
400 /// bitwiseIsEqual, and 0 != -0.
401 friend hash_code
hash_value(const IEEEFloat
&Arg
);
403 /// Converts this value into a decimal string.
405 /// \param FormatPrecision The maximum number of digits of
406 /// precision to output. If there are fewer digits available,
407 /// zero padding will not be used unless the value is
408 /// integral and small enough to be expressed in
409 /// FormatPrecision digits. 0 means to use the natural
410 /// precision of the number.
411 /// \param FormatMaxPadding The maximum number of zeros to
412 /// consider inserting before falling back to scientific
413 /// notation. 0 means to always use scientific notation.
415 /// \param TruncateZero Indicate whether to remove the trailing zero in
416 /// fraction part or not. Also setting this parameter to false forcing
417 /// producing of output more similar to default printf behavior.
418 /// Specifically the lower e is used as exponent delimiter and exponent
419 /// always contains no less than two digits.
421 /// Number Precision MaxPadding Result
422 /// ------ --------- ---------- ------
423 /// 1.01E+4 5 2 10100
424 /// 1.01E+4 4 2 1.01E+4
425 /// 1.01E+4 5 1 1.01E+4
426 /// 1.01E-2 5 2 0.0101
427 /// 1.01E-2 4 2 0.0101
428 /// 1.01E-2 4 1 1.01E-2
429 void toString(SmallVectorImpl
<char> &Str
, unsigned FormatPrecision
= 0,
430 unsigned FormatMaxPadding
= 3, bool TruncateZero
= true) const;
432 /// If this value has an exact multiplicative inverse, store it in inv and
434 bool getExactInverse(APFloat
*inv
) const;
436 /// Returns the exponent of the internal representation of the APFloat.
438 /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
439 /// For special APFloat values, this returns special error codes:
441 /// NaN -> \c IEK_NaN
443 /// Inf -> \c IEK_Inf
445 friend int ilogb(const IEEEFloat
&Arg
);
447 /// Returns: X * 2^Exp for integral exponents.
448 friend IEEEFloat
scalbn(IEEEFloat X
, int Exp
, roundingMode
);
450 friend IEEEFloat
frexp(const IEEEFloat
&X
, int &Exp
, roundingMode
);
452 /// \name Special value setters.
455 void makeLargest(bool Neg
= false);
456 void makeSmallest(bool Neg
= false);
457 void makeNaN(bool SNaN
= false, bool Neg
= false,
458 const APInt
*fill
= nullptr);
459 void makeInf(bool Neg
= false);
460 void makeZero(bool Neg
= false);
463 /// Returns the smallest (by magnitude) normalized finite number in the given
466 /// \param Negative - True iff the number should be negative
467 void makeSmallestNormalized(bool Negative
= false);
471 cmpResult
compareAbsoluteValue(const IEEEFloat
&) const;
474 /// \name Simple Queries
477 integerPart
*significandParts();
478 const integerPart
*significandParts() const;
479 unsigned int partCount() const;
483 /// \name Significand operations.
486 integerPart
addSignificand(const IEEEFloat
&);
487 integerPart
subtractSignificand(const IEEEFloat
&, integerPart
);
488 lostFraction
addOrSubtractSignificand(const IEEEFloat
&, bool subtract
);
489 lostFraction
multiplySignificand(const IEEEFloat
&, const IEEEFloat
*);
490 lostFraction
divideSignificand(const IEEEFloat
&);
491 void incrementSignificand();
492 void initialize(const fltSemantics
*);
493 void shiftSignificandLeft(unsigned int);
494 lostFraction
shiftSignificandRight(unsigned int);
495 unsigned int significandLSB() const;
496 unsigned int significandMSB() const;
497 void zeroSignificand();
498 /// Return true if the significand excluding the integral bit is all ones.
499 bool isSignificandAllOnes() const;
500 /// Return true if the significand excluding the integral bit is all zeros.
501 bool isSignificandAllZeros() const;
505 /// \name Arithmetic on special values.
508 opStatus
addOrSubtractSpecials(const IEEEFloat
&, bool subtract
);
509 opStatus
divideSpecials(const IEEEFloat
&);
510 opStatus
multiplySpecials(const IEEEFloat
&);
511 opStatus
modSpecials(const IEEEFloat
&);
518 bool convertFromStringSpecials(StringRef str
);
519 opStatus
normalize(roundingMode
, lostFraction
);
520 opStatus
addOrSubtract(const IEEEFloat
&, roundingMode
, bool subtract
);
521 opStatus
handleOverflow(roundingMode
);
522 bool roundAwayFromZero(roundingMode
, lostFraction
, unsigned int) const;
523 opStatus
convertToSignExtendedInteger(MutableArrayRef
<integerPart
>,
524 unsigned int, bool, roundingMode
,
526 opStatus
convertFromUnsignedParts(const integerPart
*, unsigned int,
528 opStatus
convertFromHexadecimalString(StringRef
, roundingMode
);
529 opStatus
convertFromDecimalString(StringRef
, roundingMode
);
530 char *convertNormalToHexString(char *, unsigned int, bool,
532 opStatus
roundSignificandWithExponent(const integerPart
*, unsigned int, int,
537 APInt
convertHalfAPFloatToAPInt() const;
538 APInt
convertFloatAPFloatToAPInt() const;
539 APInt
convertDoubleAPFloatToAPInt() const;
540 APInt
convertQuadrupleAPFloatToAPInt() const;
541 APInt
convertF80LongDoubleAPFloatToAPInt() const;
542 APInt
convertPPCDoubleDoubleAPFloatToAPInt() const;
543 void initFromAPInt(const fltSemantics
*Sem
, const APInt
&api
);
544 void initFromHalfAPInt(const APInt
&api
);
545 void initFromFloatAPInt(const APInt
&api
);
546 void initFromDoubleAPInt(const APInt
&api
);
547 void initFromQuadrupleAPInt(const APInt
&api
);
548 void initFromF80LongDoubleAPInt(const APInt
&api
);
549 void initFromPPCDoubleDoubleAPInt(const APInt
&api
);
551 void assign(const IEEEFloat
&);
552 void copySignificand(const IEEEFloat
&);
553 void freeSignificand();
555 /// Note: this must be the first data member.
556 /// The semantics that this value obeys.
557 const fltSemantics
*semantics
;
559 /// A binary fraction with an explicit integer bit.
561 /// The significand must be at least one bit wider than the target precision.
567 /// The signed unbiased exponent of the value.
568 ExponentType exponent
;
570 /// What kind of floating point number this is.
572 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
573 /// Using the extra bit keeps it from failing under VisualStudio.
574 fltCategory category
: 3;
576 /// Sign bit of the number.
577 unsigned int sign
: 1;
580 hash_code
hash_value(const IEEEFloat
&Arg
);
581 int ilogb(const IEEEFloat
&Arg
);
582 IEEEFloat
scalbn(IEEEFloat X
, int Exp
, IEEEFloat::roundingMode
);
583 IEEEFloat
frexp(const IEEEFloat
&Val
, int &Exp
, IEEEFloat::roundingMode RM
);
585 // This mode implements more precise float in terms of two APFloats.
586 // The interface and layout is designed for arbitray underlying semantics,
587 // though currently only PPCDoubleDouble semantics are supported, whose
588 // corresponding underlying semantics are IEEEdouble.
589 class DoubleAPFloat final
: public APFloatBase
{
590 // Note: this must be the first data member.
591 const fltSemantics
*Semantics
;
592 std::unique_ptr
<APFloat
[]> Floats
;
594 opStatus
addImpl(const APFloat
&a
, const APFloat
&aa
, const APFloat
&c
,
595 const APFloat
&cc
, roundingMode RM
);
597 opStatus
addWithSpecial(const DoubleAPFloat
&LHS
, const DoubleAPFloat
&RHS
,
598 DoubleAPFloat
&Out
, roundingMode RM
);
601 DoubleAPFloat(const fltSemantics
&S
);
602 DoubleAPFloat(const fltSemantics
&S
, uninitializedTag
);
603 DoubleAPFloat(const fltSemantics
&S
, integerPart
);
604 DoubleAPFloat(const fltSemantics
&S
, const APInt
&I
);
605 DoubleAPFloat(const fltSemantics
&S
, APFloat
&&First
, APFloat
&&Second
);
606 DoubleAPFloat(const DoubleAPFloat
&RHS
);
607 DoubleAPFloat(DoubleAPFloat
&&RHS
);
609 DoubleAPFloat
&operator=(const DoubleAPFloat
&RHS
);
611 DoubleAPFloat
&operator=(DoubleAPFloat
&&RHS
) {
613 this->~DoubleAPFloat();
614 new (this) DoubleAPFloat(std::move(RHS
));
619 bool needsCleanup() const { return Floats
!= nullptr; }
621 APFloat
&getFirst() { return Floats
[0]; }
622 const APFloat
&getFirst() const { return Floats
[0]; }
623 APFloat
&getSecond() { return Floats
[1]; }
624 const APFloat
&getSecond() const { return Floats
[1]; }
626 opStatus
add(const DoubleAPFloat
&RHS
, roundingMode RM
);
627 opStatus
subtract(const DoubleAPFloat
&RHS
, roundingMode RM
);
628 opStatus
multiply(const DoubleAPFloat
&RHS
, roundingMode RM
);
629 opStatus
divide(const DoubleAPFloat
&RHS
, roundingMode RM
);
630 opStatus
remainder(const DoubleAPFloat
&RHS
);
631 opStatus
mod(const DoubleAPFloat
&RHS
);
632 opStatus
fusedMultiplyAdd(const DoubleAPFloat
&Multiplicand
,
633 const DoubleAPFloat
&Addend
, roundingMode RM
);
634 opStatus
roundToIntegral(roundingMode RM
);
636 cmpResult
compareAbsoluteValue(const DoubleAPFloat
&RHS
) const;
638 fltCategory
getCategory() const;
639 bool isNegative() const;
641 void makeInf(bool Neg
);
642 void makeZero(bool Neg
);
643 void makeLargest(bool Neg
);
644 void makeSmallest(bool Neg
);
645 void makeSmallestNormalized(bool Neg
);
646 void makeNaN(bool SNaN
, bool Neg
, const APInt
*fill
);
648 cmpResult
compare(const DoubleAPFloat
&RHS
) const;
649 bool bitwiseIsEqual(const DoubleAPFloat
&RHS
) const;
650 APInt
bitcastToAPInt() const;
651 opStatus
convertFromString(StringRef
, roundingMode
);
652 opStatus
next(bool nextDown
);
654 opStatus
convertToInteger(MutableArrayRef
<integerPart
> Input
,
655 unsigned int Width
, bool IsSigned
, roundingMode RM
,
656 bool *IsExact
) const;
657 opStatus
convertFromAPInt(const APInt
&Input
, bool IsSigned
, roundingMode RM
);
658 opStatus
convertFromSignExtendedInteger(const integerPart
*Input
,
659 unsigned int InputSize
, bool IsSigned
,
661 opStatus
convertFromZeroExtendedInteger(const integerPart
*Input
,
662 unsigned int InputSize
, bool IsSigned
,
664 unsigned int convertToHexString(char *DST
, unsigned int HexDigits
,
665 bool UpperCase
, roundingMode RM
) const;
667 bool isDenormal() const;
668 bool isSmallest() const;
669 bool isLargest() const;
670 bool isInteger() const;
672 void toString(SmallVectorImpl
<char> &Str
, unsigned FormatPrecision
,
673 unsigned FormatMaxPadding
, bool TruncateZero
= true) const;
675 bool getExactInverse(APFloat
*inv
) const;
677 friend int ilogb(const DoubleAPFloat
&Arg
);
678 friend DoubleAPFloat
scalbn(DoubleAPFloat X
, int Exp
, roundingMode
);
679 friend DoubleAPFloat
frexp(const DoubleAPFloat
&X
, int &Exp
, roundingMode
);
680 friend hash_code
hash_value(const DoubleAPFloat
&Arg
);
683 hash_code
hash_value(const DoubleAPFloat
&Arg
);
685 } // End detail namespace
687 // This is a interface class that is currently forwarding functionalities from
688 // detail::IEEEFloat.
689 class APFloat
: public APFloatBase
{
690 typedef detail::IEEEFloat IEEEFloat
;
691 typedef detail::DoubleAPFloat DoubleAPFloat
;
693 static_assert(std::is_standard_layout
<IEEEFloat
>::value
, "");
696 const fltSemantics
*semantics
;
698 DoubleAPFloat Double
;
700 explicit Storage(IEEEFloat F
, const fltSemantics
&S
);
701 explicit Storage(DoubleAPFloat F
, const fltSemantics
&S
)
702 : Double(std::move(F
)) {
703 assert(&S
== &PPCDoubleDouble());
706 template <typename
... ArgTypes
>
707 Storage(const fltSemantics
&Semantics
, ArgTypes
&&... Args
) {
708 if (usesLayout
<IEEEFloat
>(Semantics
)) {
709 new (&IEEE
) IEEEFloat(Semantics
, std::forward
<ArgTypes
>(Args
)...);
712 if (usesLayout
<DoubleAPFloat
>(Semantics
)) {
713 new (&Double
) DoubleAPFloat(Semantics
, std::forward
<ArgTypes
>(Args
)...);
716 llvm_unreachable("Unexpected semantics");
720 if (usesLayout
<IEEEFloat
>(*semantics
)) {
724 if (usesLayout
<DoubleAPFloat
>(*semantics
)) {
725 Double
.~DoubleAPFloat();
728 llvm_unreachable("Unexpected semantics");
731 Storage(const Storage
&RHS
) {
732 if (usesLayout
<IEEEFloat
>(*RHS
.semantics
)) {
733 new (this) IEEEFloat(RHS
.IEEE
);
736 if (usesLayout
<DoubleAPFloat
>(*RHS
.semantics
)) {
737 new (this) DoubleAPFloat(RHS
.Double
);
740 llvm_unreachable("Unexpected semantics");
743 Storage(Storage
&&RHS
) {
744 if (usesLayout
<IEEEFloat
>(*RHS
.semantics
)) {
745 new (this) IEEEFloat(std::move(RHS
.IEEE
));
748 if (usesLayout
<DoubleAPFloat
>(*RHS
.semantics
)) {
749 new (this) DoubleAPFloat(std::move(RHS
.Double
));
752 llvm_unreachable("Unexpected semantics");
755 Storage
&operator=(const Storage
&RHS
) {
756 if (usesLayout
<IEEEFloat
>(*semantics
) &&
757 usesLayout
<IEEEFloat
>(*RHS
.semantics
)) {
759 } else if (usesLayout
<DoubleAPFloat
>(*semantics
) &&
760 usesLayout
<DoubleAPFloat
>(*RHS
.semantics
)) {
762 } else if (this != &RHS
) {
764 new (this) Storage(RHS
);
769 Storage
&operator=(Storage
&&RHS
) {
770 if (usesLayout
<IEEEFloat
>(*semantics
) &&
771 usesLayout
<IEEEFloat
>(*RHS
.semantics
)) {
772 IEEE
= std::move(RHS
.IEEE
);
773 } else if (usesLayout
<DoubleAPFloat
>(*semantics
) &&
774 usesLayout
<DoubleAPFloat
>(*RHS
.semantics
)) {
775 Double
= std::move(RHS
.Double
);
776 } else if (this != &RHS
) {
778 new (this) Storage(std::move(RHS
));
784 template <typename T
> static bool usesLayout(const fltSemantics
&Semantics
) {
785 static_assert(std::is_same
<T
, IEEEFloat
>::value
||
786 std::is_same
<T
, DoubleAPFloat
>::value
, "");
787 if (std::is_same
<T
, DoubleAPFloat
>::value
) {
788 return &Semantics
== &PPCDoubleDouble();
790 return &Semantics
!= &PPCDoubleDouble();
793 IEEEFloat
&getIEEE() {
794 if (usesLayout
<IEEEFloat
>(*U
.semantics
))
796 if (usesLayout
<DoubleAPFloat
>(*U
.semantics
))
797 return U
.Double
.getFirst().U
.IEEE
;
798 llvm_unreachable("Unexpected semantics");
801 const IEEEFloat
&getIEEE() const {
802 if (usesLayout
<IEEEFloat
>(*U
.semantics
))
804 if (usesLayout
<DoubleAPFloat
>(*U
.semantics
))
805 return U
.Double
.getFirst().U
.IEEE
;
806 llvm_unreachable("Unexpected semantics");
809 void makeZero(bool Neg
) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg
)); }
811 void makeInf(bool Neg
) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg
)); }
813 void makeNaN(bool SNaN
, bool Neg
, const APInt
*fill
) {
814 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN
, Neg
, fill
));
817 void makeLargest(bool Neg
) {
818 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg
));
821 void makeSmallest(bool Neg
) {
822 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg
));
825 void makeSmallestNormalized(bool Neg
) {
826 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg
));
829 // FIXME: This is due to clang 3.3 (or older version) always checks for the
830 // default constructor in an array aggregate initialization, even if no
831 // elements in the array is default initialized.
832 APFloat() : U(IEEEdouble()) {
833 llvm_unreachable("This is a workaround for old clang.");
836 explicit APFloat(IEEEFloat F
, const fltSemantics
&S
) : U(std::move(F
), S
) {}
837 explicit APFloat(DoubleAPFloat F
, const fltSemantics
&S
)
838 : U(std::move(F
), S
) {}
840 cmpResult
compareAbsoluteValue(const APFloat
&RHS
) const {
841 assert(&getSemantics() == &RHS
.getSemantics() &&
842 "Should only compare APFloats with the same semantics");
843 if (usesLayout
<IEEEFloat
>(getSemantics()))
844 return U
.IEEE
.compareAbsoluteValue(RHS
.U
.IEEE
);
845 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
846 return U
.Double
.compareAbsoluteValue(RHS
.U
.Double
);
847 llvm_unreachable("Unexpected semantics");
851 APFloat(const fltSemantics
&Semantics
) : U(Semantics
) {}
852 APFloat(const fltSemantics
&Semantics
, StringRef S
);
853 APFloat(const fltSemantics
&Semantics
, integerPart I
) : U(Semantics
, I
) {}
854 // TODO: Remove this constructor. This isn't faster than the first one.
855 APFloat(const fltSemantics
&Semantics
, uninitializedTag
)
856 : U(Semantics
, uninitialized
) {}
857 APFloat(const fltSemantics
&Semantics
, const APInt
&I
) : U(Semantics
, I
) {}
858 explicit APFloat(double d
) : U(IEEEFloat(d
), IEEEdouble()) {}
859 explicit APFloat(float f
) : U(IEEEFloat(f
), IEEEsingle()) {}
860 APFloat(const APFloat
&RHS
) = default;
861 APFloat(APFloat
&&RHS
) = default;
863 ~APFloat() = default;
865 bool needsCleanup() const { APFLOAT_DISPATCH_ON_SEMANTICS(needsCleanup()); }
867 /// Factory for Positive and Negative Zero.
869 /// \param Negative True iff the number should be negative.
870 static APFloat
getZero(const fltSemantics
&Sem
, bool Negative
= false) {
871 APFloat
Val(Sem
, uninitialized
);
872 Val
.makeZero(Negative
);
876 /// Factory for Positive and Negative Infinity.
878 /// \param Negative True iff the number should be negative.
879 static APFloat
getInf(const fltSemantics
&Sem
, bool Negative
= false) {
880 APFloat
Val(Sem
, uninitialized
);
881 Val
.makeInf(Negative
);
885 /// Factory for NaN values.
887 /// \param Negative - True iff the NaN generated should be negative.
888 /// \param payload - The unspecified fill bits for creating the NaN, 0 by
889 /// default. The value is truncated as necessary.
890 static APFloat
getNaN(const fltSemantics
&Sem
, bool Negative
= false,
891 uint64_t payload
= 0) {
893 APInt
intPayload(64, payload
);
894 return getQNaN(Sem
, Negative
, &intPayload
);
896 return getQNaN(Sem
, Negative
, nullptr);
900 /// Factory for QNaN values.
901 static APFloat
getQNaN(const fltSemantics
&Sem
, bool Negative
= false,
902 const APInt
*payload
= nullptr) {
903 APFloat
Val(Sem
, uninitialized
);
904 Val
.makeNaN(false, Negative
, payload
);
908 /// Factory for SNaN values.
909 static APFloat
getSNaN(const fltSemantics
&Sem
, bool Negative
= false,
910 const APInt
*payload
= nullptr) {
911 APFloat
Val(Sem
, uninitialized
);
912 Val
.makeNaN(true, Negative
, payload
);
916 /// Returns the largest finite number in the given semantics.
918 /// \param Negative - True iff the number should be negative
919 static APFloat
getLargest(const fltSemantics
&Sem
, bool Negative
= false) {
920 APFloat
Val(Sem
, uninitialized
);
921 Val
.makeLargest(Negative
);
925 /// Returns the smallest (by magnitude) finite number in the given semantics.
926 /// Might be denormalized, which implies a relative loss of precision.
928 /// \param Negative - True iff the number should be negative
929 static APFloat
getSmallest(const fltSemantics
&Sem
, bool Negative
= false) {
930 APFloat
Val(Sem
, uninitialized
);
931 Val
.makeSmallest(Negative
);
935 /// Returns the smallest (by magnitude) normalized finite number in the given
938 /// \param Negative - True iff the number should be negative
939 static APFloat
getSmallestNormalized(const fltSemantics
&Sem
,
940 bool Negative
= false) {
941 APFloat
Val(Sem
, uninitialized
);
942 Val
.makeSmallestNormalized(Negative
);
946 /// Returns a float which is bitcasted from an all one value int.
948 /// \param BitWidth - Select float type
949 /// \param isIEEE - If 128 bit number, select between PPC and IEEE
950 static APFloat
getAllOnesValue(unsigned BitWidth
, bool isIEEE
= false);
952 /// Used to insert APFloat objects, or objects that contain APFloat objects,
953 /// into FoldingSets.
954 void Profile(FoldingSetNodeID
&NID
) const;
956 opStatus
add(const APFloat
&RHS
, roundingMode RM
) {
957 assert(&getSemantics() == &RHS
.getSemantics() &&
958 "Should only call on two APFloats with the same semantics");
959 if (usesLayout
<IEEEFloat
>(getSemantics()))
960 return U
.IEEE
.add(RHS
.U
.IEEE
, RM
);
961 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
962 return U
.Double
.add(RHS
.U
.Double
, RM
);
963 llvm_unreachable("Unexpected semantics");
965 opStatus
subtract(const APFloat
&RHS
, roundingMode RM
) {
966 assert(&getSemantics() == &RHS
.getSemantics() &&
967 "Should only call on two APFloats with the same semantics");
968 if (usesLayout
<IEEEFloat
>(getSemantics()))
969 return U
.IEEE
.subtract(RHS
.U
.IEEE
, RM
);
970 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
971 return U
.Double
.subtract(RHS
.U
.Double
, RM
);
972 llvm_unreachable("Unexpected semantics");
974 opStatus
multiply(const APFloat
&RHS
, roundingMode RM
) {
975 assert(&getSemantics() == &RHS
.getSemantics() &&
976 "Should only call on two APFloats with the same semantics");
977 if (usesLayout
<IEEEFloat
>(getSemantics()))
978 return U
.IEEE
.multiply(RHS
.U
.IEEE
, RM
);
979 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
980 return U
.Double
.multiply(RHS
.U
.Double
, RM
);
981 llvm_unreachable("Unexpected semantics");
983 opStatus
divide(const APFloat
&RHS
, roundingMode RM
) {
984 assert(&getSemantics() == &RHS
.getSemantics() &&
985 "Should only call on two APFloats with the same semantics");
986 if (usesLayout
<IEEEFloat
>(getSemantics()))
987 return U
.IEEE
.divide(RHS
.U
.IEEE
, RM
);
988 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
989 return U
.Double
.divide(RHS
.U
.Double
, RM
);
990 llvm_unreachable("Unexpected semantics");
992 opStatus
remainder(const APFloat
&RHS
) {
993 assert(&getSemantics() == &RHS
.getSemantics() &&
994 "Should only call on two APFloats with the same semantics");
995 if (usesLayout
<IEEEFloat
>(getSemantics()))
996 return U
.IEEE
.remainder(RHS
.U
.IEEE
);
997 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
998 return U
.Double
.remainder(RHS
.U
.Double
);
999 llvm_unreachable("Unexpected semantics");
1001 opStatus
mod(const APFloat
&RHS
) {
1002 assert(&getSemantics() == &RHS
.getSemantics() &&
1003 "Should only call on two APFloats with the same semantics");
1004 if (usesLayout
<IEEEFloat
>(getSemantics()))
1005 return U
.IEEE
.mod(RHS
.U
.IEEE
);
1006 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
1007 return U
.Double
.mod(RHS
.U
.Double
);
1008 llvm_unreachable("Unexpected semantics");
1010 opStatus
fusedMultiplyAdd(const APFloat
&Multiplicand
, const APFloat
&Addend
,
1012 assert(&getSemantics() == &Multiplicand
.getSemantics() &&
1013 "Should only call on APFloats with the same semantics");
1014 assert(&getSemantics() == &Addend
.getSemantics() &&
1015 "Should only call on APFloats with the same semantics");
1016 if (usesLayout
<IEEEFloat
>(getSemantics()))
1017 return U
.IEEE
.fusedMultiplyAdd(Multiplicand
.U
.IEEE
, Addend
.U
.IEEE
, RM
);
1018 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
1019 return U
.Double
.fusedMultiplyAdd(Multiplicand
.U
.Double
, Addend
.U
.Double
,
1021 llvm_unreachable("Unexpected semantics");
1023 opStatus
roundToIntegral(roundingMode RM
) {
1024 APFLOAT_DISPATCH_ON_SEMANTICS(roundToIntegral(RM
));
1027 // TODO: bool parameters are not readable and a source of bugs.
1029 opStatus
next(bool nextDown
) {
1030 APFLOAT_DISPATCH_ON_SEMANTICS(next(nextDown
));
1033 /// Add two APFloats, rounding ties to the nearest even.
1034 /// No error checking.
1035 APFloat
operator+(const APFloat
&RHS
) const {
1036 APFloat
Result(*this);
1037 (void)Result
.add(RHS
, rmNearestTiesToEven
);
1041 /// Subtract two APFloats, rounding ties to the nearest even.
1042 /// No error checking.
1043 APFloat
operator-(const APFloat
&RHS
) const {
1044 APFloat
Result(*this);
1045 (void)Result
.subtract(RHS
, rmNearestTiesToEven
);
1049 /// Multiply two APFloats, rounding ties to the nearest even.
1050 /// No error checking.
1051 APFloat
operator*(const APFloat
&RHS
) const {
1052 APFloat
Result(*this);
1053 (void)Result
.multiply(RHS
, rmNearestTiesToEven
);
1057 /// Divide the first APFloat by the second, rounding ties to the nearest even.
1058 /// No error checking.
1059 APFloat
operator/(const APFloat
&RHS
) const {
1060 APFloat
Result(*this);
1061 (void)Result
.divide(RHS
, rmNearestTiesToEven
);
1065 void changeSign() { APFLOAT_DISPATCH_ON_SEMANTICS(changeSign()); }
1070 void copySign(const APFloat
&RHS
) {
1071 if (isNegative() != RHS
.isNegative())
1075 /// A static helper to produce a copy of an APFloat value with its sign
1076 /// copied from some other APFloat.
1077 static APFloat
copySign(APFloat Value
, const APFloat
&Sign
) {
1078 Value
.copySign(Sign
);
1082 opStatus
convert(const fltSemantics
&ToSemantics
, roundingMode RM
,
1084 opStatus
convertToInteger(MutableArrayRef
<integerPart
> Input
,
1085 unsigned int Width
, bool IsSigned
, roundingMode RM
,
1086 bool *IsExact
) const {
1087 APFLOAT_DISPATCH_ON_SEMANTICS(
1088 convertToInteger(Input
, Width
, IsSigned
, RM
, IsExact
));
1090 opStatus
convertToInteger(APSInt
&Result
, roundingMode RM
,
1091 bool *IsExact
) const;
1092 opStatus
convertFromAPInt(const APInt
&Input
, bool IsSigned
,
1094 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input
, IsSigned
, RM
));
1096 opStatus
convertFromSignExtendedInteger(const integerPart
*Input
,
1097 unsigned int InputSize
, bool IsSigned
,
1099 APFLOAT_DISPATCH_ON_SEMANTICS(
1100 convertFromSignExtendedInteger(Input
, InputSize
, IsSigned
, RM
));
1102 opStatus
convertFromZeroExtendedInteger(const integerPart
*Input
,
1103 unsigned int InputSize
, bool IsSigned
,
1105 APFLOAT_DISPATCH_ON_SEMANTICS(
1106 convertFromZeroExtendedInteger(Input
, InputSize
, IsSigned
, RM
));
1108 opStatus
convertFromString(StringRef
, roundingMode
);
1109 APInt
bitcastToAPInt() const {
1110 APFLOAT_DISPATCH_ON_SEMANTICS(bitcastToAPInt());
1112 double convertToDouble() const { return getIEEE().convertToDouble(); }
1113 float convertToFloat() const { return getIEEE().convertToFloat(); }
1115 bool operator==(const APFloat
&) const = delete;
1117 cmpResult
compare(const APFloat
&RHS
) const {
1118 assert(&getSemantics() == &RHS
.getSemantics() &&
1119 "Should only compare APFloats with the same semantics");
1120 if (usesLayout
<IEEEFloat
>(getSemantics()))
1121 return U
.IEEE
.compare(RHS
.U
.IEEE
);
1122 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
1123 return U
.Double
.compare(RHS
.U
.Double
);
1124 llvm_unreachable("Unexpected semantics");
1127 bool bitwiseIsEqual(const APFloat
&RHS
) const {
1128 if (&getSemantics() != &RHS
.getSemantics())
1130 if (usesLayout
<IEEEFloat
>(getSemantics()))
1131 return U
.IEEE
.bitwiseIsEqual(RHS
.U
.IEEE
);
1132 if (usesLayout
<DoubleAPFloat
>(getSemantics()))
1133 return U
.Double
.bitwiseIsEqual(RHS
.U
.Double
);
1134 llvm_unreachable("Unexpected semantics");
1137 /// We don't rely on operator== working on double values, as
1138 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1139 /// As such, this method can be used to do an exact bit-for-bit comparison of
1140 /// two floating point values.
1142 /// We leave the version with the double argument here because it's just so
1143 /// convenient to write "2.0" and the like. Without this function we'd
1144 /// have to duplicate its logic everywhere it's called.
1145 bool isExactlyValue(double V
) const {
1148 Tmp
.convert(getSemantics(), APFloat::rmNearestTiesToEven
, &ignored
);
1149 return bitwiseIsEqual(Tmp
);
1152 unsigned int convertToHexString(char *DST
, unsigned int HexDigits
,
1153 bool UpperCase
, roundingMode RM
) const {
1154 APFLOAT_DISPATCH_ON_SEMANTICS(
1155 convertToHexString(DST
, HexDigits
, UpperCase
, RM
));
1158 bool isZero() const { return getCategory() == fcZero
; }
1159 bool isInfinity() const { return getCategory() == fcInfinity
; }
1160 bool isNaN() const { return getCategory() == fcNaN
; }
1162 bool isNegative() const { return getIEEE().isNegative(); }
1163 bool isDenormal() const { APFLOAT_DISPATCH_ON_SEMANTICS(isDenormal()); }
1164 bool isSignaling() const { return getIEEE().isSignaling(); }
1166 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
1167 bool isFinite() const { return !isNaN() && !isInfinity(); }
1169 fltCategory
getCategory() const { return getIEEE().getCategory(); }
1170 const fltSemantics
&getSemantics() const { return *U
.semantics
; }
1171 bool isNonZero() const { return !isZero(); }
1172 bool isFiniteNonZero() const { return isFinite() && !isZero(); }
1173 bool isPosZero() const { return isZero() && !isNegative(); }
1174 bool isNegZero() const { return isZero() && isNegative(); }
1175 bool isSmallest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isSmallest()); }
1176 bool isLargest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isLargest()); }
1177 bool isInteger() const { APFLOAT_DISPATCH_ON_SEMANTICS(isInteger()); }
1179 APFloat
&operator=(const APFloat
&RHS
) = default;
1180 APFloat
&operator=(APFloat
&&RHS
) = default;
1182 void toString(SmallVectorImpl
<char> &Str
, unsigned FormatPrecision
= 0,
1183 unsigned FormatMaxPadding
= 3, bool TruncateZero
= true) const {
1184 APFLOAT_DISPATCH_ON_SEMANTICS(
1185 toString(Str
, FormatPrecision
, FormatMaxPadding
, TruncateZero
));
1188 void print(raw_ostream
&) const;
1191 bool getExactInverse(APFloat
*inv
) const {
1192 APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv
));
1195 friend hash_code
hash_value(const APFloat
&Arg
);
1196 friend int ilogb(const APFloat
&Arg
) { return ilogb(Arg
.getIEEE()); }
1197 friend APFloat
scalbn(APFloat X
, int Exp
, roundingMode RM
);
1198 friend APFloat
frexp(const APFloat
&X
, int &Exp
, roundingMode RM
);
1200 friend DoubleAPFloat
;
1203 /// See friend declarations above.
1205 /// These additional declarations are required in order to compile LLVM with IBM
1207 hash_code
hash_value(const APFloat
&Arg
);
1208 inline APFloat
scalbn(APFloat X
, int Exp
, APFloat::roundingMode RM
) {
1209 if (APFloat::usesLayout
<detail::IEEEFloat
>(X
.getSemantics()))
1210 return APFloat(scalbn(X
.U
.IEEE
, Exp
, RM
), X
.getSemantics());
1211 if (APFloat::usesLayout
<detail::DoubleAPFloat
>(X
.getSemantics()))
1212 return APFloat(scalbn(X
.U
.Double
, Exp
, RM
), X
.getSemantics());
1213 llvm_unreachable("Unexpected semantics");
1216 /// Equivalent of C standard library function.
1218 /// While the C standard says Exp is an unspecified value for infinity and nan,
1219 /// this returns INT_MAX for infinities, and INT_MIN for NaNs.
1220 inline APFloat
frexp(const APFloat
&X
, int &Exp
, APFloat::roundingMode RM
) {
1221 if (APFloat::usesLayout
<detail::IEEEFloat
>(X
.getSemantics()))
1222 return APFloat(frexp(X
.U
.IEEE
, Exp
, RM
), X
.getSemantics());
1223 if (APFloat::usesLayout
<detail::DoubleAPFloat
>(X
.getSemantics()))
1224 return APFloat(frexp(X
.U
.Double
, Exp
, RM
), X
.getSemantics());
1225 llvm_unreachable("Unexpected semantics");
1227 /// Returns the absolute value of the argument.
1228 inline APFloat
abs(APFloat X
) {
1233 /// Returns the negated value of the argument.
1234 inline APFloat
neg(APFloat X
) {
1239 /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
1240 /// both are not NaN. If either argument is a NaN, returns the other argument.
1242 inline APFloat
minnum(const APFloat
&A
, const APFloat
&B
) {
1247 return (B
.compare(A
) == APFloat::cmpLessThan
) ? B
: A
;
1250 /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
1251 /// both are not NaN. If either argument is a NaN, returns the other argument.
1253 inline APFloat
maxnum(const APFloat
&A
, const APFloat
&B
) {
1258 return (A
.compare(B
) == APFloat::cmpLessThan
) ? B
: A
;
1261 /// Implements IEEE 754-2018 minimum semantics. Returns the smaller of 2
1262 /// arguments, propagating NaNs and treating -0 as less than +0.
1264 inline APFloat
minimum(const APFloat
&A
, const APFloat
&B
) {
1269 if (A
.isZero() && B
.isZero() && (A
.isNegative() != B
.isNegative()))
1270 return A
.isNegative() ? A
: B
;
1271 return (B
.compare(A
) == APFloat::cmpLessThan
) ? B
: A
;
1274 /// Implements IEEE 754-2018 maximum semantics. Returns the larger of 2
1275 /// arguments, propagating NaNs and treating -0 as less than +0.
1277 inline APFloat
maximum(const APFloat
&A
, const APFloat
&B
) {
1282 if (A
.isZero() && B
.isZero() && (A
.isNegative() != B
.isNegative()))
1283 return A
.isNegative() ? B
: A
;
1284 return (A
.compare(B
) == APFloat::cmpLessThan
) ? B
: A
;
1289 #undef APFLOAT_DISPATCH_ON_SEMANTICS
1290 #endif // LLVM_ADT_APFLOAT_H