17 #ifndef LLVM_ADT_APFLOAT_H
18 #define LLVM_ADT_APFLOAT_H
25 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
27 if (usesLayout<IEEEFloat>(getSemantics())) \
28 return U.IEEE.METHOD_CALL; \
29 if (usesLayout<DoubleAPFloat>(getSemantics())) \
30 return U.Double.METHOD_CALL; \
31 llvm_unreachable("Unexpected semantics"); \
415 unsigned FormatMaxPadding = 3,
bool TruncateZero =
true)
const;
442 void makeNaN(
bool SNaN =
false,
bool Neg =
false,
443 const APInt *fill =
nullptr);
444 void makeInf(
bool Neg =
false);
464 unsigned int partCount()
const;
476 void incrementSignificand();
478 void shiftSignificandLeft(
unsigned int);
480 unsigned int significandLSB()
const;
481 unsigned int significandMSB()
const;
482 void zeroSignificand();
484 bool isSignificandAllOnes()
const;
486 bool isSignificandAllZeros()
const;
503 bool convertFromStringSpecials(
StringRef str);
515 char *convertNormalToHexString(
char *,
unsigned int,
bool,
522 APInt convertHalfAPFloatToAPInt()
const;
523 APInt convertFloatAPFloatToAPInt()
const;
524 APInt convertDoubleAPFloatToAPInt()
const;
525 APInt convertQuadrupleAPFloatToAPInt()
const;
526 APInt convertF80LongDoubleAPFloatToAPInt()
const;
527 APInt convertPPCDoubleDoubleAPFloatToAPInt()
const;
529 void initFromHalfAPInt(
const APInt &api);
530 void initFromFloatAPInt(
const APInt &api);
531 void initFromDoubleAPInt(
const APInt &api);
532 void initFromQuadrupleAPInt(
const APInt &api);
533 void initFromF80LongDoubleAPInt(
const APInt &api);
534 void initFromPPCDoubleDoubleAPInt(
const APInt &api);
538 void freeSignificand();
562 unsigned int sign : 1;
566 int ilogb(
const IEEEFloat &Arg);
577 std::unique_ptr<APFloat[]> Floats;
641 bool *IsExact)
const;
644 unsigned int InputSize,
bool IsSigned,
647 unsigned int InputSize,
bool IsSigned,
658 unsigned FormatMaxPadding,
bool TruncateZero =
true)
const;
678 static_assert(std::is_standard_layout<IEEEFloat>::value,
"");
687 : Double(std::move(F)) {
691 template <
typename... ArgTypes>
693 if (usesLayout<IEEEFloat>(Semantics)) {
697 if (usesLayout<DoubleAPFloat>(Semantics)) {
705 if (usesLayout<IEEEFloat>(*semantics)) {
709 if (usesLayout<DoubleAPFloat>(*semantics)) {
710 Double.~DoubleAPFloat();
716 Storage(
const Storage &RHS) {
717 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
721 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
728 Storage(Storage &&RHS) {
729 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
730 new (
this)
IEEEFloat(std::move(RHS.IEEE));
733 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
741 if (usesLayout<IEEEFloat>(*semantics) &&
742 usesLayout<IEEEFloat>(*RHS.semantics)) {
744 }
else if (usesLayout<DoubleAPFloat>(*semantics) &&
745 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
747 }
else if (
this != &RHS) {
749 new (
this) Storage(RHS);
755 if (usesLayout<IEEEFloat>(*semantics) &&
756 usesLayout<IEEEFloat>(*RHS.semantics)) {
757 IEEE = std::move(RHS.IEEE);
758 }
else if (usesLayout<DoubleAPFloat>(*semantics) &&
759 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
760 Double = std::move(RHS.Double);
761 }
else if (
this != &RHS) {
763 new (
this) Storage(std::move(RHS));
769 template <
typename T>
static bool usesLayout(
const fltSemantics &Semantics) {
770 static_assert(std::is_same<T, IEEEFloat>::value ||
771 std::is_same<T, DoubleAPFloat>::value,
"");
772 if (std::is_same<T, DoubleAPFloat>::value) {
779 if (usesLayout<IEEEFloat>(*U.semantics))
781 if (usesLayout<DoubleAPFloat>(*U.semantics))
782 return U.Double.getFirst().U.IEEE;
787 if (usesLayout<IEEEFloat>(*U.semantics))
789 if (usesLayout<DoubleAPFloat>(*U.semantics))
790 return U.Double.getFirst().U.IEEE;
798 void makeNaN(
bool SNaN,
bool Neg,
const APInt *fill) {
802 void makeLargest(
bool Neg) {
806 void makeSmallest(
bool Neg) {
810 void makeSmallestNormalized(
bool Neg) {
823 : U(std::move(F), S) {}
827 "Should only compare APFloats with the same semantics");
829 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
831 return U.Double.compareAbsoluteValue(RHS.U.Double);
857 Val.makeZero(Negative);
866 Val.makeInf(Negative);
878 APInt fill(64, type);
879 return getQNaN(Sem, Negative, &fill);
881 return getQNaN(Sem, Negative,
nullptr);
887 const APInt *payload =
nullptr) {
889 Val.makeNaN(
false, Negative, payload);
895 const APInt *payload =
nullptr) {
897 Val.makeNaN(
true, Negative, payload);
906 Val.makeLargest(Negative);
916 Val.makeSmallest(Negative);
925 bool Negative =
false) {
927 Val.makeSmallestNormalized(Negative);
943 "Should only call on two APFloats with the same semantics");
945 return U.IEEE.add(RHS.U.IEEE, RM);
947 return U.Double.add(RHS.U.Double, RM);
952 "Should only call on two APFloats with the same semantics");
954 return U.IEEE.subtract(RHS.U.IEEE, RM);
956 return U.Double.subtract(RHS.U.Double, RM);
961 "Should only call on two APFloats with the same semantics");
963 return U.IEEE.multiply(RHS.U.IEEE, RM);
965 return U.Double.multiply(RHS.U.Double, RM);
970 "Should only call on two APFloats with the same semantics");
972 return U.IEEE.divide(RHS.U.IEEE, RM);
974 return U.Double.divide(RHS.U.Double, RM);
979 "Should only call on two APFloats with the same semantics");
981 return U.IEEE.remainder(RHS.U.IEEE);
983 return U.Double.remainder(RHS.U.Double);
988 "Should only call on two APFloats with the same semantics");
990 return U.IEEE.mod(RHS.U.IEEE);
992 return U.Double.mod(RHS.U.Double);
998 "Should only call on APFloats with the same semantics");
1000 "Should only call on APFloats with the same semantics");
1002 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1004 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1071 bool *IsExact)
const {
1076 bool *IsExact)
const;
1082 unsigned int InputSize,
bool IsSigned,
1088 unsigned int InputSize,
bool IsSigned,
1104 "Should only compare APFloats with the same semantics");
1106 return U.IEEE.compare(RHS.U.IEEE);
1108 return U.Double.compare(RHS.U.Double);
1116 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1118 return U.Double.bitwiseIsEqual(RHS.U.Double);
1153 unsigned FormatMaxPadding = 3,
bool TruncateZero =
true)
const {
1155 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1179 if (APFloat::usesLayout<detail::IEEEFloat>(X.
getSemantics()))
1181 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.
getSemantics()))
1191 if (APFloat::usesLayout<detail::IEEEFloat>(X.
getSemantics()))
1193 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.
getSemantics()))
1233 #undef APFLOAT_DISPATCH_ON_SEMANTICS
1234 #endif // LLVM_ADT_APFLOAT_H
friend int ilogb(const APFloat &Arg)
opStatus roundToIntegral(roundingMode RM)
DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
static const fltSemantics & IEEEquad() LLVM_READNONE
fltCategory
Category of internally-represented number.
const APFloat & getFirst() const
void makeNaN(bool SNaN, bool Neg, const APInt *fill)
friend DoubleAPFloat scalbn(DoubleAPFloat X, int Exp, roundingMode)
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
APFloat(const fltSemantics &Semantics)
void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
friend int ilogb(const DoubleAPFloat &Arg)
void makeSmallest(bool Neg)
float convertToFloat() const
opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
friend hash_code hash_value(const DoubleAPFloat &Arg)
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Converts this value into a decimal string.
opStatus roundToIntegral(roundingMode)
fltCategory getCategory() const
opStatus remainder(const DoubleAPFloat &RHS)
bool isSmallest() const
Returns true if and only if the number has the smallest possible non-zero magnitude in the current se...
bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
void makeInf(bool Neg=false)
APInt bitcastToAPInt() const
static unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
hash_code hash_value(const IEEEFloat &Arg)
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
bool getExactInverse(APFloat *inv) const
opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
bool isFiniteNonZero() const
opStatus next(bool nextDown)
APFloat(const fltSemantics &Semantics, const APInt &I)
bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM)
opStatus divide(const APFloat &RHS, roundingMode RM)
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
const APFloat & getSecond() const
opStatus convertFromString(StringRef, roundingMode)
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
IEEEFloat(const fltSemantics &)
static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE=false)
Returns a float which is bitcasted from an all one value int.
void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool isFiniteNonZero() const
APFloat & operator=(const APFloat &RHS)=default
cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
APInt bitcastToAPInt() const
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...
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
void makeSmallestNormalized(bool Neg)
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
static ExponentType semanticsMaxExponent(const fltSemantics &)
bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
This file implements a class to represent arbitrary precision integral constant values and operations...
void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
IEEEFloat & operator=(const IEEEFloat &)
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
static unsigned int semanticsSizeInBits(const fltSemantics &)
cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
fltCategory getCategory() const
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.
opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
bool isZero() const
Returns true if and only if the float is plus or minus zero.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM)
opStatus subtract(const APFloat &RHS, roundingMode RM)
opStatus convertFromString(StringRef, roundingMode)
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
static const fltSemantics & IEEEdouble() LLVM_READNONE
DoubleAPFloat(const fltSemantics &S)
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
bool isInteger() const
Returns true if and only if the number is an exact integer.
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
static ExponentType semanticsMinExponent(const fltSemantics &)
const fltSemantics & getSemantics() const
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
cmpResult compareAbsoluteValue(const IEEEFloat &) const
static const unsigned integerPartWidth
IlogbErrorKinds
Enumeration of ilogb error results.
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...
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
bool getExactInverse(APFloat *inv) const
If this value has an exact multiplicative inverse, store it in inv and return true.
cmpResult compare(const DoubleAPFloat &RHS) const
friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
void Profile(FoldingSetNodeID &NID) const
Used to insert APFloat objects, or objects that contain APFloat objects, into FoldingSets.
bool operator==(const APFloat &) const =delete
opStatus mod(const DoubleAPFloat &RHS)
bool bitwiseIsEqual(const APFloat &RHS) const
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode)
opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
opStatus remainder(const IEEEFloat &)
IEEE remainder.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
opStatus multiply(const APFloat &RHS, roundingMode RM)
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
opStatus roundToIntegral(roundingMode RM)
fltCategory getCategory() const
friend int ilogb(const IEEEFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
void copySign(const APFloat &RHS)
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
bool needsCleanup() const
void makeLargest(bool Neg)
friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode)
signed short ExponentType
A signed type to represent a floating point numbers unbiased exponent.
void makeZero(bool Neg=false)
static const fltSemantics & IEEEsingle() LLVM_READNONE
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
DoubleAPFloat & operator=(DoubleAPFloat &&RHS)
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
static const fltSemantics & IEEEhalf() LLVM_READNONE
opStatus add(const IEEEFloat &, roundingMode)
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
opStatus next(bool nextDown)
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)
opStatus subtract(const IEEEFloat &, roundingMode)
APFloat neg(APFloat X)
Returns the negated value of the argument.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
static unsigned int semanticsPrecision(const fltSemantics &)
bool needsCleanup() const
APInt::WordType integerPart
Class for arbitrary precision integers.
void print(raw_ostream &) const
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
opStatus mod(const APFloat &RHS)
An opaque object representing a hash code.
double convertToDouble() const
llvm_unreachable("Unknown RegBank!")
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
opStatus add(const APFloat &RHS, roundingMode RM)
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative. ...
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, unsigned type=0)
Factory for NaN values.
opStatus
IEEE-754R 7: Default exception handling.
double convertToDouble() const
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
bool needsCleanup() const
Returns whether this instance allocated memory.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
static const fltSemantics & Bogus() LLVM_READNONE
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real...
opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
float convertToFloat() const
opStatus divide(const IEEEFloat &, roundingMode)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
opStatus remainder(const APFloat &RHS)
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
LLVM Value Representation.
APInt bitcastToAPInt() const
opStatus multiply(const IEEEFloat &, roundingMode)
bool operator==(const IEEEFloat &) const =delete
The definition of equality is not straightforward for floating point, so we won't use operator==...
This class implements an extremely fast bulk output stream that can only output to a stream...
friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Returns: X * 2^Exp for integral exponents.
cmpResult compare(const APFloat &RHS) const
StringRef - Represent a constant reference to a string, i.e.
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
bool getExactInverse(APFloat *inv) const
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
APFloat(const fltSemantics &Semantics, integerPart I)
const fltSemantics & getSemantics() const
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
APFloat(const fltSemantics &Semantics, uninitializedTag)
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
opStatus convertFromString(StringRef, roundingMode)
int ilogb(const IEEEFloat &Arg)