16 #ifndef LLVM_ADT_APFLOAT_H 17 #define LLVM_ADT_APFLOAT_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"); \ 304 APInt bitcastToAPInt()
const;
305 double convertToDouble()
const;
306 float convertToFloat()
const;
320 bool bitwiseIsEqual(
const IEEEFloat &)
const;
325 unsigned int convertToHexString(
char *dst,
unsigned int hexDigits,
341 bool isNormal()
const {
return !isDenormal() && isFiniteNonZero(); }
347 bool isFinite()
const {
return !isNaN() && !isInfinity(); }
354 bool isDenormal()
const;
363 bool isSignaling()
const;
379 bool isSmallest()
const;
383 bool isLargest()
const;
386 bool isInteger()
const;
431 unsigned FormatMaxPadding = 3,
bool TruncateZero =
true)
const;
435 bool getExactInverse(
APFloat *inv)
const;
456 void makeLargest(
bool Neg =
false);
457 void makeSmallest(
bool Neg =
false);
458 void makeNaN(
bool SNaN =
false,
bool Neg =
false,
459 const APInt *fill =
nullptr);
460 void makeInf(
bool Neg =
false);
461 void makeZero(
bool Neg =
false);
468 void makeSmallestNormalized(
bool Negative =
false);
480 unsigned int partCount()
const;
493 void incrementSignificand();
495 void shiftSignificandLeft(
unsigned int);
497 unsigned int significandLSB()
const;
498 unsigned int significandMSB()
const;
499 void zeroSignificand();
501 bool isSignificandAllOnes()
const;
503 bool isSignificandAllZeros()
const;
520 bool convertFromStringSpecials(
StringRef str);
532 char *convertNormalToHexString(
char *,
unsigned int,
bool,
539 APInt convertHalfAPFloatToAPInt()
const;
540 APInt convertFloatAPFloatToAPInt()
const;
541 APInt convertDoubleAPFloatToAPInt()
const;
542 APInt convertQuadrupleAPFloatToAPInt()
const;
543 APInt convertF80LongDoubleAPFloatToAPInt()
const;
544 APInt convertPPCDoubleDoubleAPFloatToAPInt()
const;
546 void initFromHalfAPInt(
const APInt &api);
547 void initFromFloatAPInt(
const APInt &api);
548 void initFromDoubleAPInt(
const APInt &api);
549 void initFromQuadrupleAPInt(
const APInt &api);
550 void initFromF80LongDoubleAPInt(
const APInt &api);
551 void initFromPPCDoubleDoubleAPInt(
const APInt &api);
555 void freeSignificand();
579 unsigned int sign : 1;
594 std::unique_ptr<APFloat[]> Floats;
641 bool isNegative()
const;
643 void makeInf(
bool Neg);
644 void makeZero(
bool Neg);
645 void makeLargest(
bool Neg);
646 void makeSmallest(
bool Neg);
647 void makeSmallestNormalized(
bool Neg);
648 void makeNaN(
bool SNaN,
bool Neg,
const APInt *fill);
652 APInt bitcastToAPInt()
const;
658 bool *IsExact)
const;
661 unsigned int InputSize,
bool IsSigned,
664 unsigned int InputSize,
bool IsSigned,
666 unsigned int convertToHexString(
char *DST,
unsigned int HexDigits,
669 bool isDenormal()
const;
670 bool isSmallest()
const;
671 bool isLargest()
const;
672 bool isInteger()
const;
675 unsigned FormatMaxPadding,
bool TruncateZero =
true)
const;
677 bool getExactInverse(
APFloat *inv)
const;
695 static_assert(std::is_standard_layout<IEEEFloat>::value,
"");
700 DoubleAPFloat Double;
703 explicit Storage(DoubleAPFloat F,
const fltSemantics &S)
704 : Double(std::move(F)) {
708 template <
typename... ArgTypes>
710 if (usesLayout<IEEEFloat>(Semantics)) {
711 new (&
IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(
Args)...);
714 if (usesLayout<DoubleAPFloat>(Semantics)) {
715 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(
Args)...);
722 if (usesLayout<IEEEFloat>(*semantics)) {
726 if (usesLayout<DoubleAPFloat>(*semantics)) {
727 Double.~DoubleAPFloat();
733 Storage(
const Storage &RHS) {
734 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
735 new (
this) IEEEFloat(RHS.IEEE);
738 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
739 new (
this) DoubleAPFloat(RHS.Double);
745 Storage(Storage &&RHS) {
746 if (usesLayout<IEEEFloat>(*RHS.semantics)) {
747 new (
this) IEEEFloat(std::move(RHS.IEEE));
750 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) {
751 new (
this) DoubleAPFloat(std::move(RHS.Double));
757 Storage &operator=(
const Storage &RHS) {
758 if (usesLayout<IEEEFloat>(*semantics) &&
759 usesLayout<IEEEFloat>(*RHS.semantics)) {
761 }
else if (usesLayout<DoubleAPFloat>(*semantics) &&
762 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
764 }
else if (
this != &RHS) {
766 new (
this) Storage(RHS);
771 Storage &operator=(Storage &&RHS) {
772 if (usesLayout<IEEEFloat>(*semantics) &&
773 usesLayout<IEEEFloat>(*RHS.semantics)) {
774 IEEE = std::move(RHS.IEEE);
775 }
else if (usesLayout<DoubleAPFloat>(*semantics) &&
776 usesLayout<DoubleAPFloat>(*RHS.semantics)) {
777 Double = std::move(RHS.Double);
778 }
else if (
this != &RHS) {
780 new (
this) Storage(std::move(RHS));
787 static_assert(std::is_same<T, IEEEFloat>::value ||
788 std::is_same<T, DoubleAPFloat>::value,
"");
789 if (std::is_same<T, DoubleAPFloat>::value) {
795 IEEEFloat &getIEEE() {
796 if (usesLayout<IEEEFloat>(*U.semantics))
798 if (usesLayout<DoubleAPFloat>(*U.semantics))
799 return U.Double.getFirst().U.IEEE;
803 const IEEEFloat &getIEEE()
const {
804 if (usesLayout<IEEEFloat>(*U.semantics))
806 if (usesLayout<DoubleAPFloat>(*U.semantics))
807 return U.Double.getFirst().U.IEEE;
815 void makeNaN(
bool SNaN,
bool Neg,
const APInt *fill) {
819 void makeLargest(
bool Neg) {
823 void makeSmallest(
bool Neg) {
827 void makeSmallestNormalized(
bool Neg) {
840 : U(std::move(F), S) {}
844 "Should only compare APFloats with the same semantics");
845 if (usesLayout<IEEEFloat>(getSemantics()))
846 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE);
847 if (usesLayout<DoubleAPFloat>(getSemantics()))
848 return U.Double.compareAbsoluteValue(RHS.U.Double);
856 template <
typename T,
typename =
typename std::enable_if<
857 std::is_floating_point<T>::value>
::type>
877 Val.makeZero(Negative);
886 Val.makeInf(Negative);
896 uint64_t payload = 0) {
898 APInt intPayload(64, payload);
899 return getQNaN(Sem, Negative, &intPayload);
901 return getQNaN(Sem, Negative,
nullptr);
907 const APInt *payload =
nullptr) {
909 Val.makeNaN(
false, Negative, payload);
915 const APInt *payload =
nullptr) {
917 Val.makeNaN(
true, Negative, payload);
926 Val.makeLargest(Negative);
936 Val.makeSmallest(Negative);
945 bool Negative =
false) {
947 Val.makeSmallestNormalized(Negative);
955 static APFloat getAllOnesValue(
unsigned BitWidth,
bool isIEEE =
false);
963 "Should only call on two APFloats with the same semantics");
964 if (usesLayout<IEEEFloat>(getSemantics()))
965 return U.IEEE.add(RHS.U.IEEE, RM);
966 if (usesLayout<DoubleAPFloat>(getSemantics()))
967 return U.Double.add(RHS.U.Double, RM);
972 "Should only call on two APFloats with the same semantics");
973 if (usesLayout<IEEEFloat>(getSemantics()))
974 return U.IEEE.subtract(RHS.U.IEEE, RM);
975 if (usesLayout<DoubleAPFloat>(getSemantics()))
976 return U.Double.subtract(RHS.U.Double, RM);
981 "Should only call on two APFloats with the same semantics");
982 if (usesLayout<IEEEFloat>(getSemantics()))
983 return U.IEEE.multiply(RHS.U.IEEE, RM);
984 if (usesLayout<DoubleAPFloat>(getSemantics()))
985 return U.Double.multiply(RHS.U.Double, RM);
990 "Should only call on two APFloats with the same semantics");
991 if (usesLayout<IEEEFloat>(getSemantics()))
992 return U.IEEE.divide(RHS.U.IEEE, RM);
993 if (usesLayout<DoubleAPFloat>(getSemantics()))
994 return U.Double.divide(RHS.U.Double, RM);
999 "Should only call on two APFloats with the same semantics");
1000 if (usesLayout<IEEEFloat>(getSemantics()))
1001 return U.IEEE.remainder(RHS.U.IEEE);
1002 if (usesLayout<DoubleAPFloat>(getSemantics()))
1003 return U.Double.remainder(RHS.U.Double);
1008 "Should only call on two APFloats with the same semantics");
1009 if (usesLayout<IEEEFloat>(getSemantics()))
1010 return U.IEEE.mod(RHS.U.IEEE);
1011 if (usesLayout<DoubleAPFloat>(getSemantics()))
1012 return U.Double.mod(RHS.U.Double);
1018 "Should only call on APFloats with the same semantics");
1020 "Should only call on APFloats with the same semantics");
1021 if (usesLayout<IEEEFloat>(getSemantics()))
1022 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM);
1023 if (usesLayout<DoubleAPFloat>(getSemantics()))
1024 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double,
1091 bool *IsExact)
const {
1093 convertToInteger(Input, Width, IsSigned, RM, IsExact));
1096 bool *IsExact)
const;
1102 unsigned int InputSize,
bool IsSigned,
1105 convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM));
1108 unsigned int InputSize,
bool IsSigned,
1111 convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
1124 "Should only compare APFloats with the same semantics");
1125 if (usesLayout<IEEEFloat>(getSemantics()))
1126 return U.IEEE.compare(RHS.U.IEEE);
1127 if (usesLayout<DoubleAPFloat>(getSemantics()))
1128 return U.Double.compare(RHS.U.Double);
1135 if (usesLayout<IEEEFloat>(getSemantics()))
1136 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE);
1137 if (usesLayout<DoubleAPFloat>(getSemantics()))
1138 return U.Double.bitwiseIsEqual(RHS.U.Double);
1154 return bitwiseIsEqual(Tmp);
1160 convertToHexString(DST, HexDigits, UpperCase, RM));
1171 bool isNormal()
const {
return !isDenormal() && isFiniteNonZero(); }
1172 bool isFinite()
const {
return !isNaN() && !isInfinity(); }
1188 unsigned FormatMaxPadding = 3,
bool TruncateZero =
true)
const {
1190 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
1214 if (APFloat::usesLayout<detail::IEEEFloat>(X.
getSemantics()))
1216 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.
getSemantics()))
1226 if (APFloat::usesLayout<detail::IEEEFloat>(X.
getSemantics()))
1228 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.
getSemantics()))
1294 #undef APFLOAT_DISPATCH_ON_SEMANTICS 1295 #endif // LLVM_ADT_APFLOAT_H friend int ilogb(const APFloat &Arg)
opStatus roundToIntegral(roundingMode RM)
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
static const fltSemantics & IEEEquad() LLVM_READNONE
fltCategory
Category of internally-represented number.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
APFloat(const fltSemantics &Semantics)
This class represents lattice values for constants.
fltCategory getCategory() const
float convertToFloat() const
static unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
const fltSemantics & getSemantics() const
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
static const llvm::fltSemantics & EnumToSemantics(Semantics S)
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
opStatus next(bool nextDown)
APFloat(const fltSemantics &Semantics, const APInt &I)
static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative. ...
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, 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.
std::string toString(Error E)
Write all error messages (if any) in E to a string.
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
APFloat operator*(const APFloat &RHS) const
Multiply two APFloats, rounding ties to the nearest even.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Tagged union holding either a T or a Error.
APFloat operator-(const APFloat &RHS) const
Subtract two APFloats, rounding ties to the nearest even.
static ExponentType semanticsMaxExponent(const fltSemantics &)
This file implements a class to represent arbitrary precision integral constant values and operations...
div rem Hoist decompose integer division and remainder
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
static unsigned int semanticsSizeInBits(const fltSemantics &)
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
opStatus subtract(const APFloat &RHS, roundingMode RM)
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
static const fltSemantics & IEEEdouble() LLVM_READNONE
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
static ExponentType semanticsMinExponent(const fltSemantics &)
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
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.
fltCategory getCategory() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
double convertToDouble() const
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
opStatus multiply(const APFloat &RHS, roundingMode RM)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
APFloat operator+(const APFloat &RHS) const
Add two APFloats, rounding ties to the nearest even.
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
APFloat operator/(const APFloat &RHS) const
Divide the first APFloat by the second, rounding ties to the nearest even.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void copySign(const APFloat &RHS)
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
bool needsCleanup() const
static const fltSemantics & IEEEsingle() LLVM_READNONE
DoubleAPFloat & operator=(DoubleAPFloat &&RHS)
static const fltSemantics & IEEEhalf() LLVM_READNONE
bool isFiniteNonZero() const
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
bool isFiniteNonZero() const
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)
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.
bool needsCleanup() const
static unsigned int semanticsPrecision(const fltSemantics &)
const fltSemantics & getSemantics() const
APInt::WordType integerPart
bool getExactInverse(APFloat *inv) const
Class for arbitrary precision integers.
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
const APFloat & getFirst() const
bool isZero() const
Returns true if and only if the float is plus or minus zero.
opStatus mod(const APFloat &RHS)
An opaque object representing a hash code.
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
opStatus add(const APFloat &RHS, roundingMode RM)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
opStatus
IEEE-754R 7: Default exception handling.
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...
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.
const APFloat & getSecond() const
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
opStatus remainder(const APFloat &RHS)
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
LLVM Value Representation.
bool needsCleanup() const
Returns whether this instance allocated memory.
bool bitwiseIsEqual(const APFloat &RHS) const
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
APInt bitcastToAPInt() const
bool operator==(uint64_t V1, const APInt &V2)
APFloat(const fltSemantics &Semantics, integerPart I)
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
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.
int ilogb(const IEEEFloat &Arg)
cmpResult compare(const APFloat &RHS) const
IEEE-754 denormal numbers preserved.
bool isNormal() const
IEEE-754R isNormal: Returns true if and only if the current value is normal.