LLVM 20.0.0git
|
A self-contained host- and target-independent arbitrary-precision floating-point software implementation. More...
#include "llvm/ADT/APFloat.h"
Public Types | |
enum | cmpResult { cmpLessThan , cmpEqual , cmpGreaterThan , cmpUnordered } |
IEEE-754R 5.11: Floating Point Comparison Relations. More... | |
enum | opStatus { opOK = 0x00 , opInvalidOp = 0x01 , opDivByZero = 0x02 , opOverflow = 0x04 , opUnderflow = 0x08 , opInexact = 0x10 } |
IEEE-754R 7: Default exception handling. More... | |
enum | fltCategory { fcInfinity , fcNaN , fcNormal , fcZero } |
Category of internally-represented number. More... | |
enum | uninitializedTag { uninitialized } |
Convenience enum used to construct an uninitialized APFloat. More... | |
enum | IlogbErrorKinds { IEK_Zero = INT_MIN + 1 , IEK_NaN = INT_MIN , IEK_Inf = INT_MAX } |
Enumeration of ilogb error results. More... | |
typedef APInt::WordType | integerPart |
typedef int32_t | ExponentType |
A signed type to represent a floating point numbers unbiased exponent. | |
using | roundingMode = llvm::RoundingMode |
IEEE-754R 4.3: Rounding-direction attributes. | |
Static Public Member Functions | |
static unsigned int | semanticsPrecision (const fltSemantics &) |
static ExponentType | semanticsMinExponent (const fltSemantics &) |
static ExponentType | semanticsMaxExponent (const fltSemantics &) |
static unsigned int | semanticsSizeInBits (const fltSemantics &) |
static unsigned int | semanticsIntSizeInBits (const fltSemantics &, bool) |
static bool | semanticsHasZero (const fltSemantics &) |
static bool | semanticsHasSignedRepr (const fltSemantics &) |
static bool | semanticsHasInf (const fltSemantics &) |
static bool | semanticsHasNaN (const fltSemantics &) |
static bool | isRepresentableAsNormalIn (const fltSemantics &Src, const fltSemantics &Dst) |
static unsigned | getSizeInBits (const fltSemantics &Sem) |
Returns the size of the floating point number (in bits) in the given semantics. | |
Static Public Attributes | |
static constexpr unsigned | integerPartWidth = APInt::APINT_BITS_PER_WORD |
static constexpr roundingMode | rmNearestTiesToEven |
static constexpr roundingMode | rmTowardPositive = RoundingMode::TowardPositive |
static constexpr roundingMode | rmTowardNegative = RoundingMode::TowardNegative |
static constexpr roundingMode | rmTowardZero = RoundingMode::TowardZero |
static constexpr roundingMode | rmNearestTiesToAway |
A self-contained host- and target-independent arbitrary-precision floating-point software implementation.
APFloat uses bignum integer arithmetic as provided by static functions in the APInt class. The library will work with bignum integers whose parts are any unsigned type at least 16 bits wide, but 64 bits is recommended.
Written for clarity rather than speed, in particular with a view to use in the front-end of a cross compiler so that target arithmetic can be correctly performed on the host. Performance should nonetheless be reasonable, particularly for its intended use. It may be useful as a base implementation for a run-time library during development of a faster target-specific one.
All 5 rounding modes in the IEEE-754R draft are handled correctly for all implemented operations. Currently implemented operations are add, subtract, multiply, divide, fused-multiply-add, conversion-to-float, conversion-to-integer and conversion-from-integer. New rounding modes (e.g. away from zero) can be added with three or four lines of code.
Four formats are built-in: IEEE single precision, double precision, quadruple precision, and x87 80-bit extended double (when operating with full extended precision). Adding a new format that obeys IEEE semantics only requires adding two lines of code: a declaration and definition of the format.
All operations return the status of that operation as an exception bit-mask, so multiple operations can be done consecutively with their results or-ed together. The returned status can be useful for compiler diagnostics; e.g., inexact, underflow and overflow can be easily diagnosed on constant folding, and compiler optimizers can determine what exceptions would be raised by folding operations and optimize, or perhaps not optimize, accordingly.
At present, underflow tininess is detected after rounding; it should be straight forward to add support for the before-rounding case too.
The library reads hexadecimal floating point numbers as per C99, and correctly rounds if necessary according to the specified rounding mode. Syntax is required to have been validated by the caller. It also converts floating point numbers to hexadecimal text as per the C99 a and A conversions. The output precision (or alternatively the natural minimal precision) can be specified; if the requested precision is less than the natural precision the output is correctly rounded for the specified rounding mode.
It also reads decimal floating point numbers and correctly rounds according to the specified rounding mode.
Conversion to decimal text is not currently implemented.
Non-zero finite numbers are represented internally as a sign bit, a 16-bit signed exponent, and the significand as an array of integer parts. After normalization of a number of precision P the exponent is within the range of the format, and if the number is not denormal the P-th bit of the significand is set as an explicit integer bit. For denormals the most significant bit is shifted right so that the exponent is maintained at the format's minimum, so that the smallest denormal has just the least significant bit of the significand set. The sign of zeroes and infinities is significant; the exponent and significand of such numbers is not stored, but has a known implicit (deterministic) value: 0 for the significands, 0 for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and significand are deterministic, although not really meaningful, and preserved in non-conversion operations. The exponent is implicitly all 1 bits.
APFloat does not provide any exception handling beyond default exception handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause by encoding Signaling NaNs with the first bit of its trailing significand as 0.
Some features that may or may not be worth adding:
Binary to decimal conversion (hard).
Optional ability to detect underflow tininess before rounding.
New formats: x87 in single and double precision mode (IEEE apart from extended exponent range) (hard).
New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
typedef int32_t llvm::APFloatBase::ExponentType |
IEEE-754R 7: Default exception handling.
opUnderflow or opOverflow are always returned or-ed with opInexact.
APFloat models this behavior specified by IEEE-754: "For operations producing results in floating-point format, the default result of an operation that signals the invalid operation exception shall be a quiet NaN."
Enumerator | |
---|---|
opOK | |
opInvalidOp | |
opDivByZero | |
opOverflow | |
opUnderflow | |
opInexact |
|
static |
Definition at line 264 of file APFloat.cpp.
References llvm::semBFloat.
Referenced by EnumToSemantics(), llvm::SelectionDAG::FoldConstantArithmetic(), llvm::ConstantDataSequential::getElementAsAPFloat(), llvm::MVT::getFltSemantics(), llvm::ConstantFP::isValueValidForType(), llvm::NVPTXFloatMCExpr::printImpl(), llvm::APFixedPoint::promoteFloatSemantics(), SemanticsToEnum(), shrinkFPConstant(), WriteAPFloatInternal(), and llvm::X86TargetLowering::X86TargetLowering().
|
static |
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real.
Definition at line 291 of file APFloat.cpp.
References llvm::semBogus.
Referenced by llvm::DenseMapInfo< APFloat >::getEmptyKey(), and llvm::DenseMapInfo< APFloat >::getTombstoneKey().
|
static |
Definition at line 171 of file APFloat.cpp.
References BFloat(), Float4E2M1FN(), Float6E2M3FN(), Float6E3M2FN(), Float8E3M4(), Float8E4M3(), Float8E4M3B11FNUZ(), Float8E4M3FN(), Float8E4M3FNUZ(), Float8E5M2(), Float8E5M2FNUZ(), Float8E8M0FNU(), FloatTF32(), IEEEdouble(), IEEEhalf(), IEEEquad(), IEEEsingle(), llvm_unreachable, PPCDoubleDouble(), PPCDoubleDoubleLegacy(), S_BFloat, S_Float4E2M1FN, S_Float6E2M3FN, S_Float6E3M2FN, S_Float8E3M4, S_Float8E4M3, S_Float8E4M3B11FNUZ, S_Float8E4M3FN, S_Float8E4M3FNUZ, S_Float8E5M2, S_Float8E5M2FNUZ, S_Float8E8M0FNU, S_FloatTF32, S_IEEEdouble, S_IEEEhalf, S_IEEEquad, S_IEEEsingle, S_PPCDoubleDouble, S_PPCDoubleDoubleLegacy, S_x87DoubleExtended, and x87DoubleExtended().
|
static |
Definition at line 287 of file APFloat.cpp.
References llvm::semFloat4E2M1FN.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 286 of file APFloat.cpp.
References llvm::semFloat6E2M3FN.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 285 of file APFloat.cpp.
References llvm::semFloat6E3M2FN.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 282 of file APFloat.cpp.
References llvm::semFloat8E3M4.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 276 of file APFloat.cpp.
References llvm::semFloat8E4M3.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 279 of file APFloat.cpp.
References llvm::semFloat8E4M3B11FNUZ.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 277 of file APFloat.cpp.
References llvm::semFloat8E4M3FN.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 278 of file APFloat.cpp.
References llvm::semFloat8E4M3FNUZ.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 274 of file APFloat.cpp.
References llvm::semFloat8E5M2.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 275 of file APFloat.cpp.
References llvm::semFloat8E5M2FNUZ.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 284 of file APFloat.cpp.
References llvm::semFloat8E8M0FNU.
Referenced by EnumToSemantics(), llvm::APFloat::hasSignificand(), and SemanticsToEnum().
|
static |
Definition at line 283 of file APFloat.cpp.
References llvm::semFloatTF32.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Returns the size of the floating point number (in bits) in the given semantics.
Definition at line 372 of file APFloat.cpp.
References llvm::fltSemantics::sizeInBits.
Referenced by llvm::MachineIRBuilder::buildFConstant().
|
static |
Definition at line 266 of file APFloat.cpp.
References llvm::semIEEEdouble.
Referenced by combineBitcast(), constantFoldFpUnary(), emitDebugValueComment(), EnumToSemantics(), fixFuncEntryCount(), llvm::SelectionDAG::FoldConstantArithmetic(), getConstantVector(), getConstVector(), llvm::ConstantDataSequential::getElementAsAPFloat(), llvm::MVT::getFltSemantics(), llvm::RISCVLoadFPImm::getLoadFPImm(), isInv2Pi(), llvm::ConstantFP::isValueValidForType(), llvm::AMDGPULegalizerInfo::legalizeFroundeven(), llvm::AMDGPULegalizerInfo::legalizeRsqClampIntrinsic(), llvm::LegalizerHelper::lowerFPTOUI(), llvm::AMDGPUTargetLowering::LowerFROUNDEVEN(), llvm::ARMAsmPrinter::lowerOperand(), LowerUINT_TO_FP_i64(), lowerUINT_TO_FP_vXi32(), llvm::SDNode::print_details(), llvm::NVPTXFloatMCExpr::printImpl(), llvm::WebAssemblyInstPrinter::printOperand(), llvm::APFixedPoint::promoteFloatSemantics(), SemanticsToEnum(), shrinkFPConstant(), and WriteAPFloatInternal().
|
static |
Definition at line 263 of file APFloat.cpp.
References llvm::semIEEEhalf.
Referenced by convertNvvmIntrinsicToLlvm(), EnumToSemantics(), llvm::SelectionDAG::FoldConstantArithmetic(), getConstantVector(), llvm::ConstantDataSequential::getElementAsAPFloat(), llvm::MVT::getFltSemantics(), llvm::RISCVLoadFPImm::getLoadFPImm(), llvm::GCNTTIImpl::instCombineIntrinsic(), isFPExtFromF16OrConst(), isInv2Pi(), llvm::ConstantFP::isValueValidForType(), matchFPExtFromF16(), llvm::NVPTXFloatMCExpr::printImpl(), llvm::APFixedPoint::promoteFloatSemantics(), SemanticsToEnum(), shrinkFPConstant(), strictFPExtFromF16(), WriteAPFloatInternal(), and llvm::X86TargetLowering::X86TargetLowering().
|
static |
Definition at line 267 of file APFloat.cpp.
References llvm::semIEEEquad.
Referenced by EnumToSemantics(), llvm::SelectionDAG::FoldConstantArithmetic(), llvm::MVT::getFltSemantics(), llvm::ConstantFP::isValueValidForType(), llvm::APFixedPoint::promoteFloatSemantics(), SemanticsToEnum(), llvm::SystemZVectorConstantInfo::SystemZVectorConstantInfo(), WriteAPFloatInternal(), and llvm::X86TargetLowering::X86TargetLowering().
|
static |
Definition at line 265 of file APFloat.cpp.
References llvm::semIEEEsingle.
Referenced by llvm::checkConvertToNonDenormSingle(), convertNvvmIntrinsicToLlvm(), llvm::convertToNonDenormSingle(), EnumToSemantics(), llvm::SelectionDAG::FoldConstantArithmetic(), llvm::generateReadImageInst(), getConstantVector(), getConstVector(), llvm::ConstantDataSequential::getElementAsAPFloat(), getF32Constant(), llvm::MVT::getFltSemantics(), llvm::RISCVLoadFPImm::getLoadFPImm(), llvm::AMDGPULegalizerInfo::getScaledLogInput(), llvm::AMDGPUTargetLowering::getScaledLogInput(), llvm::Attributor::identifyDefaultAbstractAttributes(), isInv2Pi(), llvm::ConstantFP::isValueValidForType(), llvm::AMDGPULegalizerInfo::legalizeFExp(), llvm::AMDGPULegalizerInfo::legalizeFlogCommon(), llvm::AMDGPULegalizerInfo::legalizeRsqClampIntrinsic(), llvm::AMDGPUTargetLowering::lowerFEXP(), llvm::LegalizerHelper::lowerFPTOUI(), lowerUINT_TO_FP_vXi32(), needsDenormHandlingF32(), llvm::AMDGPUTargetLowering::needsDenormHandlingF32(), llvm::SDNode::print_details(), llvm::NVPTXFloatMCExpr::printImpl(), llvm::WebAssemblyInstPrinter::printOperand(), llvm::APFixedPoint::promoteFloatSemantics(), SemanticsToEnum(), shrinkFPConstant(), llvm::pdb::Variant::toAPFloat(), llvm::NVPTXTargetLowering::useF32FTZ(), valueHasFloatPrecision(), and WriteAPFloatInternal().
|
static |
Definition at line 358 of file APFloat.cpp.
Referenced by computeKnownFPClass().
|
static |
Definition at line 268 of file APFloat.cpp.
References llvm::semPPCDoubleDouble.
Referenced by EnumToSemantics(), llvm::MVT::getFltSemantics(), llvm::ConstantFP::isValueValidForType(), SemanticsToEnum(), and WriteAPFloatInternal().
|
static |
Definition at line 271 of file APFloat.cpp.
References llvm::semPPCDoubleDoubleLegacy.
Referenced by EnumToSemantics(), and SemanticsToEnum().
|
static |
Definition at line 350 of file APFloat.cpp.
References llvm::IEEE754, and llvm::fltSemantics::nonFiniteBehavior.
|
static |
Definition at line 354 of file APFloat.cpp.
References llvm::FiniteOnly, and llvm::fltSemantics::nonFiniteBehavior.
|
static |
Definition at line 346 of file APFloat.cpp.
References llvm::fltSemantics::hasSignedRepr.
|
static |
Definition at line 342 of file APFloat.cpp.
References llvm::fltSemantics::hasZero.
|
static |
Definition at line 331 of file APFloat.cpp.
References isSigned(), and semanticsMaxExponent().
Referenced by canEvaluateTruncated(), and isSaturatingMinMax().
|
static |
Definition at line 321 of file APFloat.cpp.
References llvm::fltSemantics::maxExponent.
Referenced by llvm::FunctionComparator::cmpAPFloats(), and semanticsIntSizeInBits().
|
static |
Definition at line 325 of file APFloat.cpp.
References llvm::fltSemantics::minExponent.
Referenced by llvm::FunctionComparator::cmpAPFloats().
|
static |
Definition at line 317 of file APFloat.cpp.
References llvm::fltSemantics::precision.
Referenced by CastIntSETCCtoFP(), llvm::FunctionComparator::cmpAPFloats(), computeKnownFPClass(), FoldIntToFPToInt(), getEstimate(), lowerFTRUNC_FCEIL_FFLOOR_FROUND(), lowerVectorFTRUNC_FCEIL_FFLOOR_FROUND(), lowerVectorStrictFTRUNC_FCEIL_FFLOOR_FROUND(), and llvm::detail::IEEEFloat::roundToIntegral().
|
static |
Definition at line 328 of file APFloat.cpp.
References llvm::fltSemantics::sizeInBits.
Referenced by llvm::FunctionComparator::cmpAPFloats().
|
static |
Definition at line 218 of file APFloat.cpp.
References BFloat(), Float4E2M1FN(), Float6E2M3FN(), Float6E3M2FN(), Float8E3M4(), Float8E4M3(), Float8E4M3B11FNUZ(), Float8E4M3FN(), Float8E4M3FNUZ(), Float8E5M2(), Float8E5M2FNUZ(), Float8E8M0FNU(), FloatTF32(), IEEEdouble(), IEEEhalf(), IEEEquad(), IEEEsingle(), llvm_unreachable, PPCDoubleDouble(), PPCDoubleDoubleLegacy(), S_BFloat, S_Float4E2M1FN, S_Float6E2M3FN, S_Float6E3M2FN, S_Float8E3M4, S_Float8E4M3, S_Float8E4M3B11FNUZ, S_Float8E4M3FN, S_Float8E4M3FNUZ, S_Float8E5M2, S_Float8E5M2FNUZ, S_Float8E8M0FNU, S_FloatTF32, S_IEEEdouble, S_IEEEhalf, S_IEEEquad, S_IEEEsingle, S_PPCDoubleDouble, S_PPCDoubleDoubleLegacy, S_x87DoubleExtended, and x87DoubleExtended().
Referenced by llvm::SIInstrInfo::isInlineConstant().
|
static |
Definition at line 288 of file APFloat.cpp.
References llvm::semX87DoubleExtended.
Referenced by EnumToSemantics(), llvm::ExecutionEngine::getConstantValue(), llvm::MVT::getFltSemantics(), llvm::ConstantFP::isValueValidForType(), SemanticsToEnum(), WriteAPFloatInternal(), and llvm::X86TargetLowering::X86TargetLowering().
|
staticconstexpr |
Definition at line 145 of file APFloat.h.
Referenced by llvm::lostFractionThroughTruncation(), llvm::partAsHex(), llvm::partCountForBits(), and llvm::ulpsFromBoundary().
|
staticconstexpr |
Definition at line 302 of file APFloat.h.
Referenced by llvm::FixedPointSemantics::fitsInFloatSemantics().
|
staticconstexpr |
Definition at line 297 of file APFloat.h.
Referenced by llvm::APFloat::APFloat(), llvm::checkConvertToNonDenormSingle(), constantFoldFpUnary(), llvm::APFloat::convertToDouble(), llvm::APFloat::convertToFloat(), llvm::APFixedPoint::convertToFloat(), llvm::convertToNonDenormSingle(), emitDebugValueComment(), llvm::TargetLowering::expandFP_TO_UINT(), fitsInFPType(), fixFuncEntryCount(), llvm::SelectionDAG::FoldConstantArithmetic(), llvm::SelectionDAG::foldConstantFPMath(), llvm::InstCombinerImpl::foldFCmpIntToFPConst(), llvm::ConstantFP::get(), llvm::StringRef::getAsDouble(), llvm::SelectionDAG::getConstantFP(), llvm::ExecutionEngine::getConstantValue(), llvm::RISCVLoadFPImm::getLoadFPImm(), llvm::SelectionDAG::getNode(), llvm::GCNTTIImpl::instCombineIntrinsic(), llvm::APFloat::isExactlyValue(), llvm::ConstantFP::isExactlyValue(), isFPExtFromF16OrConst(), llvm::ConstantFPSDNode::isValueValidForType(), llvm::ConstantFP::isValueValidForType(), llvm::LegalizerHelper::lowerFPTOUI(), LowerFROUND(), lowerFTRUNC_FCEIL_FFLOOR_FROUND(), lowerVectorFTRUNC_FCEIL_FFLOOR_FROUND(), lowerVectorStrictFTRUNC_FCEIL_FFLOOR_FROUND(), llvm::CombinerHelper::matchConstantFoldFMA(), matchFPExtFromF16(), llvm::APFloat::operator*(), llvm::APFloat::operator+(), llvm::APFloat::operator-(), llvm::APFloat::operator/(), llvm::AMDGPUTargetLowering::PerformDAGCombine(), llvm::NVPTXFloatMCExpr::printImpl(), strictFPExtFromF16(), toString(), valueHasFloatPrecision(), llvm::InstCombinerImpl::visitFCmpInst(), WriteAPFloatInternal(), and llvm::X86TargetLowering::X86TargetLowering().
|
staticconstexpr |
Definition at line 300 of file APFloat.h.
Referenced by llvm::SelectionDAG::FoldConstantArithmetic().
|
staticconstexpr |
Definition at line 299 of file APFloat.h.
Referenced by llvm::SelectionDAG::FoldConstantArithmetic().
|
staticconstexpr |
Definition at line 301 of file APFloat.h.
Referenced by llvm::APFixedPoint::convertToFloat(), llvm::TargetLowering::expandFP_TO_INT_SAT(), llvm::SelectionDAG::FoldConstantArithmetic(), llvm::InstCombinerImpl::foldFCmpIntToFPConst(), llvm::BuildVectorSDNode::getConstantFPSplatPow2ToLog2Int(), llvm::ExecutionEngine::getConstantValue(), llvm::APFixedPoint::getFromFloatValue(), llvm::GCNTTIImpl::instCombineIntrinsic(), llvm::PPCTargetLowering::isFPImmLegal(), llvm::LegalizerHelper::lowerFPTOINT_SAT(), llvm::ARMAsmPrinter::lowerOperand(), and PerformVMulVCTPCombine().