1 //===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file declares a class to represent arbitrary precision floating point 11 /// values and provide a variety of arithmetic operations on them. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_ADT_APFLOAT_H 16 #define LLVM_ADT_APFLOAT_H 17 18 #include "llvm/ADT/APInt.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/FloatingPointMode.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include <memory> 23 24 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ 25 do { \ 26 if (usesLayout<IEEEFloat>(getSemantics())) \ 27 return U.IEEE.METHOD_CALL; \ 28 if (usesLayout<DoubleAPFloat>(getSemantics())) \ 29 return U.Double.METHOD_CALL; \ 30 llvm_unreachable("Unexpected semantics"); \ 31 } while (false) 32 33 namespace llvm { 34 35 struct fltSemantics; 36 class APSInt; 37 class StringRef; 38 class APFloat; 39 class raw_ostream; 40 41 template <typename T> class Expected; 42 template <typename T> class SmallVectorImpl; 43 44 /// Enum that represents what fraction of the LSB truncated bits of an fp number 45 /// represent. 46 /// 47 /// This essentially combines the roles of guard and sticky bits. 48 enum lostFraction { // Example of truncated bits: 49 lfExactlyZero, // 000000 50 lfLessThanHalf, // 0xxxxx x's not all zero 51 lfExactlyHalf, // 100000 52 lfMoreThanHalf // 1xxxxx x's not all zero 53 }; 54 55 /// A self-contained host- and target-independent arbitrary-precision 56 /// floating-point software implementation. 57 /// 58 /// APFloat uses bignum integer arithmetic as provided by static functions in 59 /// the APInt class. The library will work with bignum integers whose parts are 60 /// any unsigned type at least 16 bits wide, but 64 bits is recommended. 61 /// 62 /// Written for clarity rather than speed, in particular with a view to use in 63 /// the front-end of a cross compiler so that target arithmetic can be correctly 64 /// performed on the host. Performance should nonetheless be reasonable, 65 /// particularly for its intended use. It may be useful as a base 66 /// implementation for a run-time library during development of a faster 67 /// target-specific one. 68 /// 69 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all 70 /// implemented operations. Currently implemented operations are add, subtract, 71 /// multiply, divide, fused-multiply-add, conversion-to-float, 72 /// conversion-to-integer and conversion-from-integer. New rounding modes 73 /// (e.g. away from zero) can be added with three or four lines of code. 74 /// 75 /// Four formats are built-in: IEEE single precision, double precision, 76 /// quadruple precision, and x87 80-bit extended double (when operating with 77 /// full extended precision). Adding a new format that obeys IEEE semantics 78 /// only requires adding two lines of code: a declaration and definition of the 79 /// format. 80 /// 81 /// All operations return the status of that operation as an exception bit-mask, 82 /// so multiple operations can be done consecutively with their results or-ed 83 /// together. The returned status can be useful for compiler diagnostics; e.g., 84 /// inexact, underflow and overflow can be easily diagnosed on constant folding, 85 /// and compiler optimizers can determine what exceptions would be raised by 86 /// folding operations and optimize, or perhaps not optimize, accordingly. 87 /// 88 /// At present, underflow tininess is detected after rounding; it should be 89 /// straight forward to add support for the before-rounding case too. 90 /// 91 /// The library reads hexadecimal floating point numbers as per C99, and 92 /// correctly rounds if necessary according to the specified rounding mode. 93 /// Syntax is required to have been validated by the caller. It also converts 94 /// floating point numbers to hexadecimal text as per the C99 %a and %A 95 /// conversions. The output precision (or alternatively the natural minimal 96 /// precision) can be specified; if the requested precision is less than the 97 /// natural precision the output is correctly rounded for the specified rounding 98 /// mode. 99 /// 100 /// It also reads decimal floating point numbers and correctly rounds according 101 /// to the specified rounding mode. 102 /// 103 /// Conversion to decimal text is not currently implemented. 104 /// 105 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit 106 /// signed exponent, and the significand as an array of integer parts. After 107 /// normalization of a number of precision P the exponent is within the range of 108 /// the format, and if the number is not denormal the P-th bit of the 109 /// significand is set as an explicit integer bit. For denormals the most 110 /// significant bit is shifted right so that the exponent is maintained at the 111 /// format's minimum, so that the smallest denormal has just the least 112 /// significant bit of the significand set. The sign of zeroes and infinities 113 /// is significant; the exponent and significand of such numbers is not stored, 114 /// but has a known implicit (deterministic) value: 0 for the significands, 0 115 /// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and 116 /// significand are deterministic, although not really meaningful, and preserved 117 /// in non-conversion operations. The exponent is implicitly all 1 bits. 118 /// 119 /// APFloat does not provide any exception handling beyond default exception 120 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause 121 /// by encoding Signaling NaNs with the first bit of its trailing significand as 122 /// 0. 123 /// 124 /// TODO 125 /// ==== 126 /// 127 /// Some features that may or may not be worth adding: 128 /// 129 /// Binary to decimal conversion (hard). 130 /// 131 /// Optional ability to detect underflow tininess before rounding. 132 /// 133 /// New formats: x87 in single and double precision mode (IEEE apart from 134 /// extended exponent range) (hard). 135 /// 136 /// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward. 137 /// 138 139 // This is the common type definitions shared by APFloat and its internal 140 // implementation classes. This struct should not define any non-static data 141 // members. 142 struct APFloatBase { 143 typedef APInt::WordType integerPart; 144 static constexpr unsigned integerPartWidth = APInt::APINT_BITS_PER_WORD; 145 146 /// A signed type to represent a floating point numbers unbiased exponent. 147 typedef int32_t ExponentType; 148 149 /// \name Floating Point Semantics. 150 /// @{ 151 enum Semantics { 152 S_IEEEhalf, 153 S_BFloat, 154 S_IEEEsingle, 155 S_IEEEdouble, 156 S_IEEEquad, 157 S_PPCDoubleDouble, 158 // 8-bit floating point number following IEEE-754 conventions with bit 159 // layout S1E5M2 as described in https://arxiv.org/abs/2209.05433. 160 S_Float8E5M2, 161 // 8-bit floating point number mostly following IEEE-754 conventions 162 // and bit layout S1E5M2 described in https://arxiv.org/abs/2206.02915, 163 // with expanded range and with no infinity or signed zero. 164 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero). 165 // This format's exponent bias is 16, instead of the 15 (2 ** (5 - 1) - 1) 166 // that IEEE precedent would imply. 167 S_Float8E5M2FNUZ, 168 // 8-bit floating point number mostly following IEEE-754 conventions with 169 // bit layout S1E4M3 as described in https://arxiv.org/abs/2209.05433. 170 // Unlike IEEE-754 types, there are no infinity values, and NaN is 171 // represented with the exponent and mantissa bits set to all 1s. 172 S_Float8E4M3FN, 173 // 8-bit floating point number mostly following IEEE-754 conventions 174 // and bit layout S1E4M3 described in https://arxiv.org/abs/2206.02915, 175 // with expanded range and with no infinity or signed zero. 176 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero). 177 // This format's exponent bias is 8, instead of the 7 (2 ** (4 - 1) - 1) 178 // that IEEE precedent would imply. 179 S_Float8E4M3FNUZ, 180 // 8-bit floating point number mostly following IEEE-754 conventions 181 // and bit layout S1E4M3 with expanded range and with no infinity or signed 182 // zero. 183 // NaN is represented as negative zero. (FN -> Finite, UZ -> unsigned zero). 184 // This format's exponent bias is 11, instead of the 7 (2 ** (4 - 1) - 1) 185 // that IEEE precedent would imply. 186 S_Float8E4M3B11FNUZ, 187 // Floating point number that occupies 32 bits or less of storage, providing 188 // improved range compared to half (16-bit) formats, at (potentially) 189 // greater throughput than single precision (32-bit) formats. 190 S_FloatTF32, 191 192 S_x87DoubleExtended, 193 S_MaxSemantics = S_x87DoubleExtended, 194 }; 195 196 static const llvm::fltSemantics &EnumToSemantics(Semantics S); 197 static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem); 198 199 static const fltSemantics &IEEEhalf() LLVM_READNONE; 200 static const fltSemantics &BFloat() LLVM_READNONE; 201 static const fltSemantics &IEEEsingle() LLVM_READNONE; 202 static const fltSemantics &IEEEdouble() LLVM_READNONE; 203 static const fltSemantics &IEEEquad() LLVM_READNONE; 204 static const fltSemantics &PPCDoubleDouble() LLVM_READNONE; 205 static const fltSemantics &Float8E5M2() LLVM_READNONE; 206 static const fltSemantics &Float8E5M2FNUZ() LLVM_READNONE; 207 static const fltSemantics &Float8E4M3FN() LLVM_READNONE; 208 static const fltSemantics &Float8E4M3FNUZ() LLVM_READNONE; 209 static const fltSemantics &Float8E4M3B11FNUZ() LLVM_READNONE; 210 static const fltSemantics &FloatTF32() LLVM_READNONE; 211 static const fltSemantics &x87DoubleExtended() LLVM_READNONE; 212 213 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with 214 /// anything real. 215 static const fltSemantics &Bogus() LLVM_READNONE; 216 217 /// @} 218 219 /// IEEE-754R 5.11: Floating Point Comparison Relations. 220 enum cmpResult { 221 cmpLessThan, 222 cmpEqual, 223 cmpGreaterThan, 224 cmpUnordered 225 }; 226 227 /// IEEE-754R 4.3: Rounding-direction attributes. 228 using roundingMode = llvm::RoundingMode; 229 230 static constexpr roundingMode rmNearestTiesToEven = 231 RoundingMode::NearestTiesToEven; 232 static constexpr roundingMode rmTowardPositive = RoundingMode::TowardPositive; 233 static constexpr roundingMode rmTowardNegative = RoundingMode::TowardNegative; 234 static constexpr roundingMode rmTowardZero = RoundingMode::TowardZero; 235 static constexpr roundingMode rmNearestTiesToAway = 236 RoundingMode::NearestTiesToAway; 237 238 /// IEEE-754R 7: Default exception handling. 239 /// 240 /// opUnderflow or opOverflow are always returned or-ed with opInexact. 241 /// 242 /// APFloat models this behavior specified by IEEE-754: 243 /// "For operations producing results in floating-point format, the default 244 /// result of an operation that signals the invalid operation exception 245 /// shall be a quiet NaN." 246 enum opStatus { 247 opOK = 0x00, 248 opInvalidOp = 0x01, 249 opDivByZero = 0x02, 250 opOverflow = 0x04, 251 opUnderflow = 0x08, 252 opInexact = 0x10 253 }; 254 255 /// Category of internally-represented number. 256 enum fltCategory { 257 fcInfinity, 258 fcNaN, 259 fcNormal, 260 fcZero 261 }; 262 263 /// Convenience enum used to construct an uninitialized APFloat. 264 enum uninitializedTag { 265 uninitialized 266 }; 267 268 /// Enumeration of \c ilogb error results. 269 enum IlogbErrorKinds { 270 IEK_Zero = INT_MIN + 1, 271 IEK_NaN = INT_MIN, 272 IEK_Inf = INT_MAX 273 }; 274 275 static unsigned int semanticsPrecision(const fltSemantics &); 276 static ExponentType semanticsMinExponent(const fltSemantics &); 277 static ExponentType semanticsMaxExponent(const fltSemantics &); 278 static unsigned int semanticsSizeInBits(const fltSemantics &); 279 static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool); 280 281 // Returns true if any number described by \p Src can be precisely represented 282 // by a normal (not subnormal) value in \p Dst. 283 static bool isRepresentableAsNormalIn(const fltSemantics &Src, 284 const fltSemantics &Dst); 285 286 /// Returns the size of the floating point number (in bits) in the given 287 /// semantics. 288 static unsigned getSizeInBits(const fltSemantics &Sem); 289 }; 290 291 namespace detail { 292 293 class IEEEFloat final : public APFloatBase { 294 public: 295 /// \name Constructors 296 /// @{ 297 298 IEEEFloat(const fltSemantics &); // Default construct to +0.0 299 IEEEFloat(const fltSemantics &, integerPart); 300 IEEEFloat(const fltSemantics &, uninitializedTag); 301 IEEEFloat(const fltSemantics &, const APInt &); 302 explicit IEEEFloat(double d); 303 explicit IEEEFloat(float f); 304 IEEEFloat(const IEEEFloat &); 305 IEEEFloat(IEEEFloat &&); 306 ~IEEEFloat(); 307 308 /// @} 309 310 /// Returns whether this instance allocated memory. 311 bool needsCleanup() const { return partCount() > 1; } 312 313 /// \name Convenience "constructors" 314 /// @{ 315 316 /// @} 317 318 /// \name Arithmetic 319 /// @{ 320 321 opStatus add(const IEEEFloat &, roundingMode); 322 opStatus subtract(const IEEEFloat &, roundingMode); 323 opStatus multiply(const IEEEFloat &, roundingMode); 324 opStatus divide(const IEEEFloat &, roundingMode); 325 /// IEEE remainder. 326 opStatus remainder(const IEEEFloat &); 327 /// C fmod, or llvm frem. 328 opStatus mod(const IEEEFloat &); 329 opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode); 330 opStatus roundToIntegral(roundingMode); 331 /// IEEE-754R 5.3.1: nextUp/nextDown. 332 opStatus next(bool nextDown); 333 334 /// @} 335 336 /// \name Sign operations. 337 /// @{ 338 339 void changeSign(); 340 341 /// @} 342 343 /// \name Conversions 344 /// @{ 345 346 opStatus convert(const fltSemantics &, roundingMode, bool *); 347 opStatus convertToInteger(MutableArrayRef<integerPart>, unsigned int, bool, 348 roundingMode, bool *) const; 349 opStatus convertFromAPInt(const APInt &, bool, roundingMode); 350 opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, 351 bool, roundingMode); 352 opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, 353 bool, roundingMode); 354 Expected<opStatus> convertFromString(StringRef, roundingMode); 355 APInt bitcastToAPInt() const; 356 double convertToDouble() const; 357 float convertToFloat() const; 358 359 /// @} 360 361 /// The definition of equality is not straightforward for floating point, so 362 /// we won't use operator==. Use one of the following, or write whatever it 363 /// is you really mean. 364 bool operator==(const IEEEFloat &) const = delete; 365 366 /// IEEE comparison with another floating point number (NaNs compare 367 /// unordered, 0==-0). 368 cmpResult compare(const IEEEFloat &) const; 369 370 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0). 371 bool bitwiseIsEqual(const IEEEFloat &) const; 372 373 /// Write out a hexadecimal representation of the floating point value to DST, 374 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d. 375 /// Return the number of characters written, excluding the terminating NUL. 376 unsigned int convertToHexString(char *dst, unsigned int hexDigits, 377 bool upperCase, roundingMode) const; 378 379 /// \name IEEE-754R 5.7.2 General operations. 380 /// @{ 381 382 /// IEEE-754R isSignMinus: Returns true if and only if the current value is 383 /// negative. 384 /// 385 /// This applies to zeros and NaNs as well. 386 bool isNegative() const { return sign; } 387 388 /// IEEE-754R isNormal: Returns true if and only if the current value is normal. 389 /// 390 /// This implies that the current value of the float is not zero, subnormal, 391 /// infinite, or NaN following the definition of normality from IEEE-754R. 392 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); } 393 394 /// Returns true if and only if the current value is zero, subnormal, or 395 /// normal. 396 /// 397 /// This means that the value is not infinite or NaN. 398 bool isFinite() const { return !isNaN() && !isInfinity(); } 399 400 /// Returns true if and only if the float is plus or minus zero. 401 bool isZero() const { return category == fcZero; } 402 403 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a 404 /// denormal. 405 bool isDenormal() const; 406 407 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity. 408 bool isInfinity() const { return category == fcInfinity; } 409 410 /// Returns true if and only if the float is a quiet or signaling NaN. 411 bool isNaN() const { return category == fcNaN; } 412 413 /// Returns true if and only if the float is a signaling NaN. 414 bool isSignaling() const; 415 416 /// @} 417 418 /// \name Simple Queries 419 /// @{ 420 421 fltCategory getCategory() const { return category; } 422 const fltSemantics &getSemantics() const { return *semantics; } 423 bool isNonZero() const { return category != fcZero; } 424 bool isFiniteNonZero() const { return isFinite() && !isZero(); } 425 bool isPosZero() const { return isZero() && !isNegative(); } 426 bool isNegZero() const { return isZero() && isNegative(); } 427 428 /// Returns true if and only if the number has the smallest possible non-zero 429 /// magnitude in the current semantics. 430 bool isSmallest() const; 431 432 /// Returns true if this is the smallest (by magnitude) normalized finite 433 /// number in the given semantics. 434 bool isSmallestNormalized() const; 435 436 /// Returns true if and only if the number has the largest possible finite 437 /// magnitude in the current semantics. 438 bool isLargest() const; 439 440 /// Returns true if and only if the number is an exact integer. 441 bool isInteger() const; 442 443 /// @} 444 445 IEEEFloat &operator=(const IEEEFloat &); 446 IEEEFloat &operator=(IEEEFloat &&); 447 448 /// Overload to compute a hash code for an APFloat value. 449 /// 450 /// Note that the use of hash codes for floating point values is in general 451 /// frought with peril. Equality is hard to define for these values. For 452 /// example, should negative and positive zero hash to different codes? Are 453 /// they equal or not? This hash value implementation specifically 454 /// emphasizes producing different codes for different inputs in order to 455 /// be used in canonicalization and memoization. As such, equality is 456 /// bitwiseIsEqual, and 0 != -0. 457 friend hash_code hash_value(const IEEEFloat &Arg); 458 459 /// Converts this value into a decimal string. 460 /// 461 /// \param FormatPrecision The maximum number of digits of 462 /// precision to output. If there are fewer digits available, 463 /// zero padding will not be used unless the value is 464 /// integral and small enough to be expressed in 465 /// FormatPrecision digits. 0 means to use the natural 466 /// precision of the number. 467 /// \param FormatMaxPadding The maximum number of zeros to 468 /// consider inserting before falling back to scientific 469 /// notation. 0 means to always use scientific notation. 470 /// 471 /// \param TruncateZero Indicate whether to remove the trailing zero in 472 /// fraction part or not. Also setting this parameter to false forcing 473 /// producing of output more similar to default printf behavior. 474 /// Specifically the lower e is used as exponent delimiter and exponent 475 /// always contains no less than two digits. 476 /// 477 /// Number Precision MaxPadding Result 478 /// ------ --------- ---------- ------ 479 /// 1.01E+4 5 2 10100 480 /// 1.01E+4 4 2 1.01E+4 481 /// 1.01E+4 5 1 1.01E+4 482 /// 1.01E-2 5 2 0.0101 483 /// 1.01E-2 4 2 0.0101 484 /// 1.01E-2 4 1 1.01E-2 485 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0, 486 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const; 487 488 /// If this value has an exact multiplicative inverse, store it in inv and 489 /// return true. 490 bool getExactInverse(APFloat *inv) const; 491 492 /// Returns the exponent of the internal representation of the APFloat. 493 /// 494 /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)). 495 /// For special APFloat values, this returns special error codes: 496 /// 497 /// NaN -> \c IEK_NaN 498 /// 0 -> \c IEK_Zero 499 /// Inf -> \c IEK_Inf 500 /// 501 friend int ilogb(const IEEEFloat &Arg); 502 503 /// Returns: X * 2^Exp for integral exponents. 504 friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode); 505 506 friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode); 507 508 /// \name Special value setters. 509 /// @{ 510 511 void makeLargest(bool Neg = false); 512 void makeSmallest(bool Neg = false); 513 void makeNaN(bool SNaN = false, bool Neg = false, 514 const APInt *fill = nullptr); 515 void makeInf(bool Neg = false); 516 void makeZero(bool Neg = false); 517 void makeQuiet(); 518 519 /// Returns the smallest (by magnitude) normalized finite number in the given 520 /// semantics. 521 /// 522 /// \param Negative - True iff the number should be negative 523 void makeSmallestNormalized(bool Negative = false); 524 525 /// @} 526 527 cmpResult compareAbsoluteValue(const IEEEFloat &) const; 528 529 private: 530 /// \name Simple Queries 531 /// @{ 532 533 integerPart *significandParts(); 534 const integerPart *significandParts() const; 535 unsigned int partCount() const; 536 537 /// @} 538 539 /// \name Significand operations. 540 /// @{ 541 542 integerPart addSignificand(const IEEEFloat &); 543 integerPart subtractSignificand(const IEEEFloat &, integerPart); 544 lostFraction addOrSubtractSignificand(const IEEEFloat &, bool subtract); 545 lostFraction multiplySignificand(const IEEEFloat &, IEEEFloat); 546 lostFraction multiplySignificand(const IEEEFloat&); 547 lostFraction divideSignificand(const IEEEFloat &); 548 void incrementSignificand(); 549 void initialize(const fltSemantics *); 550 void shiftSignificandLeft(unsigned int); 551 lostFraction shiftSignificandRight(unsigned int); 552 unsigned int significandLSB() const; 553 unsigned int significandMSB() const; 554 void zeroSignificand(); 555 /// Return true if the significand excluding the integral bit is all ones. 556 bool isSignificandAllOnes() const; 557 bool isSignificandAllOnesExceptLSB() const; 558 /// Return true if the significand excluding the integral bit is all zeros. 559 bool isSignificandAllZeros() const; 560 bool isSignificandAllZerosExceptMSB() const; 561 562 /// @} 563 564 /// \name Arithmetic on special values. 565 /// @{ 566 567 opStatus addOrSubtractSpecials(const IEEEFloat &, bool subtract); 568 opStatus divideSpecials(const IEEEFloat &); 569 opStatus multiplySpecials(const IEEEFloat &); 570 opStatus modSpecials(const IEEEFloat &); 571 opStatus remainderSpecials(const IEEEFloat&); 572 573 /// @} 574 575 /// \name Miscellany 576 /// @{ 577 578 bool convertFromStringSpecials(StringRef str); 579 opStatus normalize(roundingMode, lostFraction); 580 opStatus addOrSubtract(const IEEEFloat &, roundingMode, bool subtract); 581 opStatus handleOverflow(roundingMode); 582 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const; 583 opStatus convertToSignExtendedInteger(MutableArrayRef<integerPart>, 584 unsigned int, bool, roundingMode, 585 bool *) const; 586 opStatus convertFromUnsignedParts(const integerPart *, unsigned int, 587 roundingMode); 588 Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode); 589 Expected<opStatus> convertFromDecimalString(StringRef, roundingMode); 590 char *convertNormalToHexString(char *, unsigned int, bool, 591 roundingMode) const; 592 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int, 593 roundingMode); 594 ExponentType exponentNaN() const; 595 ExponentType exponentInf() const; 596 ExponentType exponentZero() const; 597 598 /// @} 599 600 template <const fltSemantics &S> APInt convertIEEEFloatToAPInt() const; 601 APInt convertHalfAPFloatToAPInt() const; 602 APInt convertBFloatAPFloatToAPInt() const; 603 APInt convertFloatAPFloatToAPInt() const; 604 APInt convertDoubleAPFloatToAPInt() const; 605 APInt convertQuadrupleAPFloatToAPInt() const; 606 APInt convertF80LongDoubleAPFloatToAPInt() const; 607 APInt convertPPCDoubleDoubleAPFloatToAPInt() const; 608 APInt convertFloat8E5M2APFloatToAPInt() const; 609 APInt convertFloat8E5M2FNUZAPFloatToAPInt() const; 610 APInt convertFloat8E4M3FNAPFloatToAPInt() const; 611 APInt convertFloat8E4M3FNUZAPFloatToAPInt() const; 612 APInt convertFloat8E4M3B11FNUZAPFloatToAPInt() const; 613 APInt convertFloatTF32APFloatToAPInt() const; 614 void initFromAPInt(const fltSemantics *Sem, const APInt &api); 615 template <const fltSemantics &S> void initFromIEEEAPInt(const APInt &api); 616 void initFromHalfAPInt(const APInt &api); 617 void initFromBFloatAPInt(const APInt &api); 618 void initFromFloatAPInt(const APInt &api); 619 void initFromDoubleAPInt(const APInt &api); 620 void initFromQuadrupleAPInt(const APInt &api); 621 void initFromF80LongDoubleAPInt(const APInt &api); 622 void initFromPPCDoubleDoubleAPInt(const APInt &api); 623 void initFromFloat8E5M2APInt(const APInt &api); 624 void initFromFloat8E5M2FNUZAPInt(const APInt &api); 625 void initFromFloat8E4M3FNAPInt(const APInt &api); 626 void initFromFloat8E4M3FNUZAPInt(const APInt &api); 627 void initFromFloat8E4M3B11FNUZAPInt(const APInt &api); 628 void initFromFloatTF32APInt(const APInt &api); 629 630 void assign(const IEEEFloat &); 631 void copySignificand(const IEEEFloat &); 632 void freeSignificand(); 633 634 /// Note: this must be the first data member. 635 /// The semantics that this value obeys. 636 const fltSemantics *semantics; 637 638 /// A binary fraction with an explicit integer bit. 639 /// 640 /// The significand must be at least one bit wider than the target precision. 641 union Significand { 642 integerPart part; 643 integerPart *parts; 644 } significand; 645 646 /// The signed unbiased exponent of the value. 647 ExponentType exponent; 648 649 /// What kind of floating point number this is. 650 /// 651 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it. 652 /// Using the extra bit keeps it from failing under VisualStudio. 653 fltCategory category : 3; 654 655 /// Sign bit of the number. 656 unsigned int sign : 1; 657 }; 658 659 hash_code hash_value(const IEEEFloat &Arg); 660 int ilogb(const IEEEFloat &Arg); 661 IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode); 662 IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM); 663 664 // This mode implements more precise float in terms of two APFloats. 665 // The interface and layout is designed for arbitrary underlying semantics, 666 // though currently only PPCDoubleDouble semantics are supported, whose 667 // corresponding underlying semantics are IEEEdouble. 668 class DoubleAPFloat final : public APFloatBase { 669 // Note: this must be the first data member. 670 const fltSemantics *Semantics; 671 std::unique_ptr<APFloat[]> Floats; 672 673 opStatus addImpl(const APFloat &a, const APFloat &aa, const APFloat &c, 674 const APFloat &cc, roundingMode RM); 675 676 opStatus addWithSpecial(const DoubleAPFloat &LHS, const DoubleAPFloat &RHS, 677 DoubleAPFloat &Out, roundingMode RM); 678 679 public: 680 DoubleAPFloat(const fltSemantics &S); 681 DoubleAPFloat(const fltSemantics &S, uninitializedTag); 682 DoubleAPFloat(const fltSemantics &S, integerPart); 683 DoubleAPFloat(const fltSemantics &S, const APInt &I); 684 DoubleAPFloat(const fltSemantics &S, APFloat &&First, APFloat &&Second); 685 DoubleAPFloat(const DoubleAPFloat &RHS); 686 DoubleAPFloat(DoubleAPFloat &&RHS); 687 688 DoubleAPFloat &operator=(const DoubleAPFloat &RHS); 689 inline DoubleAPFloat &operator=(DoubleAPFloat &&RHS); 690 691 bool needsCleanup() const { return Floats != nullptr; } 692 693 inline APFloat &getFirst(); 694 inline const APFloat &getFirst() const; 695 inline APFloat &getSecond(); 696 inline const APFloat &getSecond() const; 697 698 opStatus add(const DoubleAPFloat &RHS, roundingMode RM); 699 opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM); 700 opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM); 701 opStatus divide(const DoubleAPFloat &RHS, roundingMode RM); 702 opStatus remainder(const DoubleAPFloat &RHS); 703 opStatus mod(const DoubleAPFloat &RHS); 704 opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, 705 const DoubleAPFloat &Addend, roundingMode RM); 706 opStatus roundToIntegral(roundingMode RM); 707 void changeSign(); 708 cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const; 709 710 fltCategory getCategory() const; 711 bool isNegative() const; 712 713 void makeInf(bool Neg); 714 void makeZero(bool Neg); 715 void makeLargest(bool Neg); 716 void makeSmallest(bool Neg); 717 void makeSmallestNormalized(bool Neg); 718 void makeNaN(bool SNaN, bool Neg, const APInt *fill); 719 720 cmpResult compare(const DoubleAPFloat &RHS) const; 721 bool bitwiseIsEqual(const DoubleAPFloat &RHS) const; 722 APInt bitcastToAPInt() const; 723 Expected<opStatus> convertFromString(StringRef, roundingMode); 724 opStatus next(bool nextDown); 725 726 opStatus convertToInteger(MutableArrayRef<integerPart> Input, 727 unsigned int Width, bool IsSigned, roundingMode RM, 728 bool *IsExact) const; 729 opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM); 730 opStatus convertFromSignExtendedInteger(const integerPart *Input, 731 unsigned int InputSize, bool IsSigned, 732 roundingMode RM); 733 opStatus convertFromZeroExtendedInteger(const integerPart *Input, 734 unsigned int InputSize, bool IsSigned, 735 roundingMode RM); 736 unsigned int convertToHexString(char *DST, unsigned int HexDigits, 737 bool UpperCase, roundingMode RM) const; 738 739 bool isDenormal() const; 740 bool isSmallest() const; 741 bool isSmallestNormalized() const; 742 bool isLargest() const; 743 bool isInteger() const; 744 745 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision, 746 unsigned FormatMaxPadding, bool TruncateZero = true) const; 747 748 bool getExactInverse(APFloat *inv) const; 749 750 friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode); 751 friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode); 752 friend hash_code hash_value(const DoubleAPFloat &Arg); 753 }; 754 755 hash_code hash_value(const DoubleAPFloat &Arg); 756 757 } // End detail namespace 758 759 // This is a interface class that is currently forwarding functionalities from 760 // detail::IEEEFloat. 761 class APFloat : public APFloatBase { 762 typedef detail::IEEEFloat IEEEFloat; 763 typedef detail::DoubleAPFloat DoubleAPFloat; 764 765 static_assert(std::is_standard_layout<IEEEFloat>::value); 766 767 union Storage { 768 const fltSemantics *semantics; 769 IEEEFloat IEEE; 770 DoubleAPFloat Double; 771 772 explicit Storage(IEEEFloat F, const fltSemantics &S); 773 explicit Storage(DoubleAPFloat F, const fltSemantics &S) 774 : Double(std::move(F)) { 775 assert(&S == &PPCDoubleDouble()); 776 } 777 778 template <typename... ArgTypes> 779 Storage(const fltSemantics &Semantics, ArgTypes &&... Args) { 780 if (usesLayout<IEEEFloat>(Semantics)) { 781 new (&IEEE) IEEEFloat(Semantics, std::forward<ArgTypes>(Args)...); 782 return; 783 } 784 if (usesLayout<DoubleAPFloat>(Semantics)) { 785 new (&Double) DoubleAPFloat(Semantics, std::forward<ArgTypes>(Args)...); 786 return; 787 } 788 llvm_unreachable("Unexpected semantics"); 789 } 790 791 ~Storage() { 792 if (usesLayout<IEEEFloat>(*semantics)) { 793 IEEE.~IEEEFloat(); 794 return; 795 } 796 if (usesLayout<DoubleAPFloat>(*semantics)) { 797 Double.~DoubleAPFloat(); 798 return; 799 } 800 llvm_unreachable("Unexpected semantics"); 801 } 802 803 Storage(const Storage &RHS) { 804 if (usesLayout<IEEEFloat>(*RHS.semantics)) { 805 new (this) IEEEFloat(RHS.IEEE); 806 return; 807 } 808 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) { 809 new (this) DoubleAPFloat(RHS.Double); 810 return; 811 } 812 llvm_unreachable("Unexpected semantics"); 813 } 814 815 Storage(Storage &&RHS) { 816 if (usesLayout<IEEEFloat>(*RHS.semantics)) { 817 new (this) IEEEFloat(std::move(RHS.IEEE)); 818 return; 819 } 820 if (usesLayout<DoubleAPFloat>(*RHS.semantics)) { 821 new (this) DoubleAPFloat(std::move(RHS.Double)); 822 return; 823 } 824 llvm_unreachable("Unexpected semantics"); 825 } 826 827 Storage &operator=(const Storage &RHS) { 828 if (usesLayout<IEEEFloat>(*semantics) && 829 usesLayout<IEEEFloat>(*RHS.semantics)) { 830 IEEE = RHS.IEEE; 831 } else if (usesLayout<DoubleAPFloat>(*semantics) && 832 usesLayout<DoubleAPFloat>(*RHS.semantics)) { 833 Double = RHS.Double; 834 } else if (this != &RHS) { 835 this->~Storage(); 836 new (this) Storage(RHS); 837 } 838 return *this; 839 } 840 841 Storage &operator=(Storage &&RHS) { 842 if (usesLayout<IEEEFloat>(*semantics) && 843 usesLayout<IEEEFloat>(*RHS.semantics)) { 844 IEEE = std::move(RHS.IEEE); 845 } else if (usesLayout<DoubleAPFloat>(*semantics) && 846 usesLayout<DoubleAPFloat>(*RHS.semantics)) { 847 Double = std::move(RHS.Double); 848 } else if (this != &RHS) { 849 this->~Storage(); 850 new (this) Storage(std::move(RHS)); 851 } 852 return *this; 853 } 854 } U; 855 856 template <typename T> static bool usesLayout(const fltSemantics &Semantics) { 857 static_assert(std::is_same<T, IEEEFloat>::value || 858 std::is_same<T, DoubleAPFloat>::value); 859 if (std::is_same<T, DoubleAPFloat>::value) { 860 return &Semantics == &PPCDoubleDouble(); 861 } 862 return &Semantics != &PPCDoubleDouble(); 863 } 864 865 IEEEFloat &getIEEE() { 866 if (usesLayout<IEEEFloat>(*U.semantics)) 867 return U.IEEE; 868 if (usesLayout<DoubleAPFloat>(*U.semantics)) 869 return U.Double.getFirst().U.IEEE; 870 llvm_unreachable("Unexpected semantics"); 871 } 872 873 const IEEEFloat &getIEEE() const { 874 if (usesLayout<IEEEFloat>(*U.semantics)) 875 return U.IEEE; 876 if (usesLayout<DoubleAPFloat>(*U.semantics)) 877 return U.Double.getFirst().U.IEEE; 878 llvm_unreachable("Unexpected semantics"); 879 } 880 881 void makeZero(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeZero(Neg)); } 882 883 void makeInf(bool Neg) { APFLOAT_DISPATCH_ON_SEMANTICS(makeInf(Neg)); } 884 885 void makeNaN(bool SNaN, bool Neg, const APInt *fill) { 886 APFLOAT_DISPATCH_ON_SEMANTICS(makeNaN(SNaN, Neg, fill)); 887 } 888 889 void makeLargest(bool Neg) { 890 APFLOAT_DISPATCH_ON_SEMANTICS(makeLargest(Neg)); 891 } 892 893 void makeSmallest(bool Neg) { 894 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallest(Neg)); 895 } 896 897 void makeSmallestNormalized(bool Neg) { 898 APFLOAT_DISPATCH_ON_SEMANTICS(makeSmallestNormalized(Neg)); 899 } 900 901 explicit APFloat(IEEEFloat F, const fltSemantics &S) : U(std::move(F), S) {} 902 explicit APFloat(DoubleAPFloat F, const fltSemantics &S) 903 : U(std::move(F), S) {} 904 905 cmpResult compareAbsoluteValue(const APFloat &RHS) const { 906 assert(&getSemantics() == &RHS.getSemantics() && 907 "Should only compare APFloats with the same semantics"); 908 if (usesLayout<IEEEFloat>(getSemantics())) 909 return U.IEEE.compareAbsoluteValue(RHS.U.IEEE); 910 if (usesLayout<DoubleAPFloat>(getSemantics())) 911 return U.Double.compareAbsoluteValue(RHS.U.Double); 912 llvm_unreachable("Unexpected semantics"); 913 } 914 915 public: 916 APFloat(const fltSemantics &Semantics) : U(Semantics) {} 917 APFloat(const fltSemantics &Semantics, StringRef S); 918 APFloat(const fltSemantics &Semantics, integerPart I) : U(Semantics, I) {} 919 template <typename T, 920 typename = std::enable_if_t<std::is_floating_point<T>::value>> 921 APFloat(const fltSemantics &Semantics, T V) = delete; 922 // TODO: Remove this constructor. This isn't faster than the first one. 923 APFloat(const fltSemantics &Semantics, uninitializedTag) 924 : U(Semantics, uninitialized) {} 925 APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {} 926 explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {} 927 explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {} 928 APFloat(const APFloat &RHS) = default; 929 APFloat(APFloat &&RHS) = default; 930 931 ~APFloat() = default; 932 933 bool needsCleanup() const { APFLOAT_DISPATCH_ON_SEMANTICS(needsCleanup()); } 934 935 /// Factory for Positive and Negative Zero. 936 /// 937 /// \param Negative True iff the number should be negative. 938 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) { 939 APFloat Val(Sem, uninitialized); 940 Val.makeZero(Negative); 941 return Val; 942 } 943 944 /// Factory for Positive and Negative Infinity. 945 /// 946 /// \param Negative True iff the number should be negative. 947 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) { 948 APFloat Val(Sem, uninitialized); 949 Val.makeInf(Negative); 950 return Val; 951 } 952 953 /// Factory for NaN values. 954 /// 955 /// \param Negative - True iff the NaN generated should be negative. 956 /// \param payload - The unspecified fill bits for creating the NaN, 0 by 957 /// default. The value is truncated as necessary. 958 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false, 959 uint64_t payload = 0) { 960 if (payload) { 961 APInt intPayload(64, payload); 962 return getQNaN(Sem, Negative, &intPayload); 963 } else { 964 return getQNaN(Sem, Negative, nullptr); 965 } 966 } 967 968 /// Factory for QNaN values. 969 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false, 970 const APInt *payload = nullptr) { 971 APFloat Val(Sem, uninitialized); 972 Val.makeNaN(false, Negative, payload); 973 return Val; 974 } 975 976 /// Factory for SNaN values. 977 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false, 978 const APInt *payload = nullptr) { 979 APFloat Val(Sem, uninitialized); 980 Val.makeNaN(true, Negative, payload); 981 return Val; 982 } 983 984 /// Returns the largest finite number in the given semantics. 985 /// 986 /// \param Negative - True iff the number should be negative 987 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false) { 988 APFloat Val(Sem, uninitialized); 989 Val.makeLargest(Negative); 990 return Val; 991 } 992 993 /// Returns the smallest (by magnitude) finite number in the given semantics. 994 /// Might be denormalized, which implies a relative loss of precision. 995 /// 996 /// \param Negative - True iff the number should be negative 997 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false) { 998 APFloat Val(Sem, uninitialized); 999 Val.makeSmallest(Negative); 1000 return Val; 1001 } 1002 1003 /// Returns the smallest (by magnitude) normalized finite number in the given 1004 /// semantics. 1005 /// 1006 /// \param Negative - True iff the number should be negative 1007 static APFloat getSmallestNormalized(const fltSemantics &Sem, 1008 bool Negative = false) { 1009 APFloat Val(Sem, uninitialized); 1010 Val.makeSmallestNormalized(Negative); 1011 return Val; 1012 } 1013 1014 /// Returns a float which is bitcasted from an all one value int. 1015 /// 1016 /// \param Semantics - type float semantics 1017 static APFloat getAllOnesValue(const fltSemantics &Semantics); 1018 1019 /// Used to insert APFloat objects, or objects that contain APFloat objects, 1020 /// into FoldingSets. 1021 void Profile(FoldingSetNodeID &NID) const; 1022 1023 opStatus add(const APFloat &RHS, roundingMode RM) { 1024 assert(&getSemantics() == &RHS.getSemantics() && 1025 "Should only call on two APFloats with the same semantics"); 1026 if (usesLayout<IEEEFloat>(getSemantics())) 1027 return U.IEEE.add(RHS.U.IEEE, RM); 1028 if (usesLayout<DoubleAPFloat>(getSemantics())) 1029 return U.Double.add(RHS.U.Double, RM); 1030 llvm_unreachable("Unexpected semantics"); 1031 } 1032 opStatus subtract(const APFloat &RHS, roundingMode RM) { 1033 assert(&getSemantics() == &RHS.getSemantics() && 1034 "Should only call on two APFloats with the same semantics"); 1035 if (usesLayout<IEEEFloat>(getSemantics())) 1036 return U.IEEE.subtract(RHS.U.IEEE, RM); 1037 if (usesLayout<DoubleAPFloat>(getSemantics())) 1038 return U.Double.subtract(RHS.U.Double, RM); 1039 llvm_unreachable("Unexpected semantics"); 1040 } 1041 opStatus multiply(const APFloat &RHS, roundingMode RM) { 1042 assert(&getSemantics() == &RHS.getSemantics() && 1043 "Should only call on two APFloats with the same semantics"); 1044 if (usesLayout<IEEEFloat>(getSemantics())) 1045 return U.IEEE.multiply(RHS.U.IEEE, RM); 1046 if (usesLayout<DoubleAPFloat>(getSemantics())) 1047 return U.Double.multiply(RHS.U.Double, RM); 1048 llvm_unreachable("Unexpected semantics"); 1049 } 1050 opStatus divide(const APFloat &RHS, roundingMode RM) { 1051 assert(&getSemantics() == &RHS.getSemantics() && 1052 "Should only call on two APFloats with the same semantics"); 1053 if (usesLayout<IEEEFloat>(getSemantics())) 1054 return U.IEEE.divide(RHS.U.IEEE, RM); 1055 if (usesLayout<DoubleAPFloat>(getSemantics())) 1056 return U.Double.divide(RHS.U.Double, RM); 1057 llvm_unreachable("Unexpected semantics"); 1058 } 1059 opStatus remainder(const APFloat &RHS) { 1060 assert(&getSemantics() == &RHS.getSemantics() && 1061 "Should only call on two APFloats with the same semantics"); 1062 if (usesLayout<IEEEFloat>(getSemantics())) 1063 return U.IEEE.remainder(RHS.U.IEEE); 1064 if (usesLayout<DoubleAPFloat>(getSemantics())) 1065 return U.Double.remainder(RHS.U.Double); 1066 llvm_unreachable("Unexpected semantics"); 1067 } 1068 opStatus mod(const APFloat &RHS) { 1069 assert(&getSemantics() == &RHS.getSemantics() && 1070 "Should only call on two APFloats with the same semantics"); 1071 if (usesLayout<IEEEFloat>(getSemantics())) 1072 return U.IEEE.mod(RHS.U.IEEE); 1073 if (usesLayout<DoubleAPFloat>(getSemantics())) 1074 return U.Double.mod(RHS.U.Double); 1075 llvm_unreachable("Unexpected semantics"); 1076 } 1077 opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, 1078 roundingMode RM) { 1079 assert(&getSemantics() == &Multiplicand.getSemantics() && 1080 "Should only call on APFloats with the same semantics"); 1081 assert(&getSemantics() == &Addend.getSemantics() && 1082 "Should only call on APFloats with the same semantics"); 1083 if (usesLayout<IEEEFloat>(getSemantics())) 1084 return U.IEEE.fusedMultiplyAdd(Multiplicand.U.IEEE, Addend.U.IEEE, RM); 1085 if (usesLayout<DoubleAPFloat>(getSemantics())) 1086 return U.Double.fusedMultiplyAdd(Multiplicand.U.Double, Addend.U.Double, 1087 RM); 1088 llvm_unreachable("Unexpected semantics"); 1089 } 1090 opStatus roundToIntegral(roundingMode RM) { 1091 APFLOAT_DISPATCH_ON_SEMANTICS(roundToIntegral(RM)); 1092 } 1093 1094 // TODO: bool parameters are not readable and a source of bugs. 1095 // Do something. 1096 opStatus next(bool nextDown) { 1097 APFLOAT_DISPATCH_ON_SEMANTICS(next(nextDown)); 1098 } 1099 1100 /// Negate an APFloat. 1101 APFloat operator-() const { 1102 APFloat Result(*this); 1103 Result.changeSign(); 1104 return Result; 1105 } 1106 1107 /// Add two APFloats, rounding ties to the nearest even. 1108 /// No error checking. 1109 APFloat operator+(const APFloat &RHS) const { 1110 APFloat Result(*this); 1111 (void)Result.add(RHS, rmNearestTiesToEven); 1112 return Result; 1113 } 1114 1115 /// Subtract two APFloats, rounding ties to the nearest even. 1116 /// No error checking. 1117 APFloat operator-(const APFloat &RHS) const { 1118 APFloat Result(*this); 1119 (void)Result.subtract(RHS, rmNearestTiesToEven); 1120 return Result; 1121 } 1122 1123 /// Multiply two APFloats, rounding ties to the nearest even. 1124 /// No error checking. 1125 APFloat operator*(const APFloat &RHS) const { 1126 APFloat Result(*this); 1127 (void)Result.multiply(RHS, rmNearestTiesToEven); 1128 return Result; 1129 } 1130 1131 /// Divide the first APFloat by the second, rounding ties to the nearest even. 1132 /// No error checking. 1133 APFloat operator/(const APFloat &RHS) const { 1134 APFloat Result(*this); 1135 (void)Result.divide(RHS, rmNearestTiesToEven); 1136 return Result; 1137 } 1138 1139 void changeSign() { APFLOAT_DISPATCH_ON_SEMANTICS(changeSign()); } 1140 void clearSign() { 1141 if (isNegative()) 1142 changeSign(); 1143 } 1144 void copySign(const APFloat &RHS) { 1145 if (isNegative() != RHS.isNegative()) 1146 changeSign(); 1147 } 1148 1149 /// A static helper to produce a copy of an APFloat value with its sign 1150 /// copied from some other APFloat. 1151 static APFloat copySign(APFloat Value, const APFloat &Sign) { 1152 Value.copySign(Sign); 1153 return Value; 1154 } 1155 1156 /// Assuming this is an IEEE-754 NaN value, quiet its signaling bit. 1157 /// This preserves the sign and payload bits. 1158 APFloat makeQuiet() const { 1159 APFloat Result(*this); 1160 Result.getIEEE().makeQuiet(); 1161 return Result; 1162 } 1163 1164 opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, 1165 bool *losesInfo); 1166 opStatus convertToInteger(MutableArrayRef<integerPart> Input, 1167 unsigned int Width, bool IsSigned, roundingMode RM, 1168 bool *IsExact) const { 1169 APFLOAT_DISPATCH_ON_SEMANTICS( 1170 convertToInteger(Input, Width, IsSigned, RM, IsExact)); 1171 } 1172 opStatus convertToInteger(APSInt &Result, roundingMode RM, 1173 bool *IsExact) const; 1174 opStatus convertFromAPInt(const APInt &Input, bool IsSigned, 1175 roundingMode RM) { 1176 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromAPInt(Input, IsSigned, RM)); 1177 } 1178 opStatus convertFromSignExtendedInteger(const integerPart *Input, 1179 unsigned int InputSize, bool IsSigned, 1180 roundingMode RM) { 1181 APFLOAT_DISPATCH_ON_SEMANTICS( 1182 convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM)); 1183 } 1184 opStatus convertFromZeroExtendedInteger(const integerPart *Input, 1185 unsigned int InputSize, bool IsSigned, 1186 roundingMode RM) { 1187 APFLOAT_DISPATCH_ON_SEMANTICS( 1188 convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM)); 1189 } 1190 Expected<opStatus> convertFromString(StringRef, roundingMode); 1191 APInt bitcastToAPInt() const { 1192 APFLOAT_DISPATCH_ON_SEMANTICS(bitcastToAPInt()); 1193 } 1194 1195 /// Converts this APFloat to host double value. 1196 /// 1197 /// \pre The APFloat must be built using semantics, that can be represented by 1198 /// the host double type without loss of precision. It can be IEEEdouble and 1199 /// shorter semantics, like IEEEsingle and others. 1200 double convertToDouble() const; 1201 1202 /// Converts this APFloat to host float value. 1203 /// 1204 /// \pre The APFloat must be built using semantics, that can be represented by 1205 /// the host float type without loss of precision. It can be IEEEsingle and 1206 /// shorter semantics, like IEEEhalf. 1207 float convertToFloat() const; 1208 1209 bool operator==(const APFloat &RHS) const { return compare(RHS) == cmpEqual; } 1210 1211 bool operator!=(const APFloat &RHS) const { return compare(RHS) != cmpEqual; } 1212 1213 bool operator<(const APFloat &RHS) const { 1214 return compare(RHS) == cmpLessThan; 1215 } 1216 1217 bool operator>(const APFloat &RHS) const { 1218 return compare(RHS) == cmpGreaterThan; 1219 } 1220 1221 bool operator<=(const APFloat &RHS) const { 1222 cmpResult Res = compare(RHS); 1223 return Res == cmpLessThan || Res == cmpEqual; 1224 } 1225 1226 bool operator>=(const APFloat &RHS) const { 1227 cmpResult Res = compare(RHS); 1228 return Res == cmpGreaterThan || Res == cmpEqual; 1229 } 1230 1231 cmpResult compare(const APFloat &RHS) const { 1232 assert(&getSemantics() == &RHS.getSemantics() && 1233 "Should only compare APFloats with the same semantics"); 1234 if (usesLayout<IEEEFloat>(getSemantics())) 1235 return U.IEEE.compare(RHS.U.IEEE); 1236 if (usesLayout<DoubleAPFloat>(getSemantics())) 1237 return U.Double.compare(RHS.U.Double); 1238 llvm_unreachable("Unexpected semantics"); 1239 } 1240 1241 bool bitwiseIsEqual(const APFloat &RHS) const { 1242 if (&getSemantics() != &RHS.getSemantics()) 1243 return false; 1244 if (usesLayout<IEEEFloat>(getSemantics())) 1245 return U.IEEE.bitwiseIsEqual(RHS.U.IEEE); 1246 if (usesLayout<DoubleAPFloat>(getSemantics())) 1247 return U.Double.bitwiseIsEqual(RHS.U.Double); 1248 llvm_unreachable("Unexpected semantics"); 1249 } 1250 1251 /// We don't rely on operator== working on double values, as 1252 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 1253 /// As such, this method can be used to do an exact bit-for-bit comparison of 1254 /// two floating point values. 1255 /// 1256 /// We leave the version with the double argument here because it's just so 1257 /// convenient to write "2.0" and the like. Without this function we'd 1258 /// have to duplicate its logic everywhere it's called. 1259 bool isExactlyValue(double V) const { 1260 bool ignored; 1261 APFloat Tmp(V); 1262 Tmp.convert(getSemantics(), APFloat::rmNearestTiesToEven, &ignored); 1263 return bitwiseIsEqual(Tmp); 1264 } 1265 1266 unsigned int convertToHexString(char *DST, unsigned int HexDigits, 1267 bool UpperCase, roundingMode RM) const { 1268 APFLOAT_DISPATCH_ON_SEMANTICS( 1269 convertToHexString(DST, HexDigits, UpperCase, RM)); 1270 } 1271 1272 bool isZero() const { return getCategory() == fcZero; } 1273 bool isInfinity() const { return getCategory() == fcInfinity; } 1274 bool isNaN() const { return getCategory() == fcNaN; } 1275 1276 bool isNegative() const { return getIEEE().isNegative(); } 1277 bool isDenormal() const { APFLOAT_DISPATCH_ON_SEMANTICS(isDenormal()); } 1278 bool isSignaling() const { return getIEEE().isSignaling(); } 1279 1280 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); } 1281 bool isFinite() const { return !isNaN() && !isInfinity(); } 1282 1283 fltCategory getCategory() const { return getIEEE().getCategory(); } 1284 const fltSemantics &getSemantics() const { return *U.semantics; } 1285 bool isNonZero() const { return !isZero(); } 1286 bool isFiniteNonZero() const { return isFinite() && !isZero(); } 1287 bool isPosZero() const { return isZero() && !isNegative(); } 1288 bool isNegZero() const { return isZero() && isNegative(); } 1289 bool isPosInfinity() const { return isInfinity() && !isNegative(); } 1290 bool isNegInfinity() const { return isInfinity() && isNegative(); } 1291 bool isSmallest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isSmallest()); } 1292 bool isLargest() const { APFLOAT_DISPATCH_ON_SEMANTICS(isLargest()); } 1293 bool isInteger() const { APFLOAT_DISPATCH_ON_SEMANTICS(isInteger()); } 1294 bool isIEEE() const { return usesLayout<IEEEFloat>(getSemantics()); } 1295 1296 bool isSmallestNormalized() const { 1297 APFLOAT_DISPATCH_ON_SEMANTICS(isSmallestNormalized()); 1298 } 1299 1300 /// Return the FPClassTest which will return true for the value. 1301 FPClassTest classify() const; 1302 1303 APFloat &operator=(const APFloat &RHS) = default; 1304 APFloat &operator=(APFloat &&RHS) = default; 1305 1306 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0, 1307 unsigned FormatMaxPadding = 3, bool TruncateZero = true) const { 1308 APFLOAT_DISPATCH_ON_SEMANTICS( 1309 toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero)); 1310 } 1311 1312 void print(raw_ostream &) const; 1313 void dump() const; 1314 1315 bool getExactInverse(APFloat *inv) const { 1316 APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv)); 1317 } 1318 1319 friend hash_code hash_value(const APFloat &Arg); 1320 friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); } 1321 friend APFloat scalbn(APFloat X, int Exp, roundingMode RM); 1322 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM); 1323 friend IEEEFloat; 1324 friend DoubleAPFloat; 1325 }; 1326 1327 /// See friend declarations above. 1328 /// 1329 /// These additional declarations are required in order to compile LLVM with IBM 1330 /// xlC compiler. 1331 hash_code hash_value(const APFloat &Arg); 1332 inline APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM) { 1333 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics())) 1334 return APFloat(scalbn(X.U.IEEE, Exp, RM), X.getSemantics()); 1335 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics())) 1336 return APFloat(scalbn(X.U.Double, Exp, RM), X.getSemantics()); 1337 llvm_unreachable("Unexpected semantics"); 1338 } 1339 1340 /// Equivalent of C standard library function. 1341 /// 1342 /// While the C standard says Exp is an unspecified value for infinity and nan, 1343 /// this returns INT_MAX for infinities, and INT_MIN for NaNs. 1344 inline APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM) { 1345 if (APFloat::usesLayout<detail::IEEEFloat>(X.getSemantics())) 1346 return APFloat(frexp(X.U.IEEE, Exp, RM), X.getSemantics()); 1347 if (APFloat::usesLayout<detail::DoubleAPFloat>(X.getSemantics())) 1348 return APFloat(frexp(X.U.Double, Exp, RM), X.getSemantics()); 1349 llvm_unreachable("Unexpected semantics"); 1350 } 1351 /// Returns the absolute value of the argument. 1352 inline APFloat abs(APFloat X) { 1353 X.clearSign(); 1354 return X; 1355 } 1356 1357 /// Returns the negated value of the argument. 1358 inline APFloat neg(APFloat X) { 1359 X.changeSign(); 1360 return X; 1361 } 1362 1363 /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if 1364 /// both are not NaN. If either argument is a NaN, returns the other argument. 1365 LLVM_READONLY 1366 inline APFloat minnum(const APFloat &A, const APFloat &B) { 1367 if (A.isNaN()) 1368 return B; 1369 if (B.isNaN()) 1370 return A; 1371 return B < A ? B : A; 1372 } 1373 1374 /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if 1375 /// both are not NaN. If either argument is a NaN, returns the other argument. 1376 LLVM_READONLY 1377 inline APFloat maxnum(const APFloat &A, const APFloat &B) { 1378 if (A.isNaN()) 1379 return B; 1380 if (B.isNaN()) 1381 return A; 1382 return A < B ? B : A; 1383 } 1384 1385 /// Implements IEEE 754-2018 minimum semantics. Returns the smaller of 2 1386 /// arguments, propagating NaNs and treating -0 as less than +0. 1387 LLVM_READONLY 1388 inline APFloat minimum(const APFloat &A, const APFloat &B) { 1389 if (A.isNaN()) 1390 return A; 1391 if (B.isNaN()) 1392 return B; 1393 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative())) 1394 return A.isNegative() ? A : B; 1395 return B < A ? B : A; 1396 } 1397 1398 /// Implements IEEE 754-2018 maximum semantics. Returns the larger of 2 1399 /// arguments, propagating NaNs and treating -0 as less than +0. 1400 LLVM_READONLY 1401 inline APFloat maximum(const APFloat &A, const APFloat &B) { 1402 if (A.isNaN()) 1403 return A; 1404 if (B.isNaN()) 1405 return B; 1406 if (A.isZero() && B.isZero() && (A.isNegative() != B.isNegative())) 1407 return A.isNegative() ? B : A; 1408 return A < B ? B : A; 1409 } 1410 1411 // We want the following functions to be available in the header for inlining. 1412 // We cannot define them inline in the class definition of `DoubleAPFloat` 1413 // because doing so would instantiate `std::unique_ptr<APFloat[]>` before 1414 // `APFloat` is defined, and that would be undefined behavior. 1415 namespace detail { 1416 1417 DoubleAPFloat &DoubleAPFloat::operator=(DoubleAPFloat &&RHS) { 1418 if (this != &RHS) { 1419 this->~DoubleAPFloat(); 1420 new (this) DoubleAPFloat(std::move(RHS)); 1421 } 1422 return *this; 1423 } 1424 1425 APFloat &DoubleAPFloat::getFirst() { return Floats[0]; } 1426 const APFloat &DoubleAPFloat::getFirst() const { return Floats[0]; } 1427 APFloat &DoubleAPFloat::getSecond() { return Floats[1]; } 1428 const APFloat &DoubleAPFloat::getSecond() const { return Floats[1]; } 1429 1430 } // namespace detail 1431 1432 } // namespace llvm 1433 1434 #undef APFLOAT_DISPATCH_ON_SEMANTICS 1435 #endif // LLVM_ADT_APFLOAT_H 1436