1 //===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief 12 /// This file declares a class to represent arbitrary precision floating point 13 /// values and provide a variety of arithmetic operations on them. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_ADT_APFLOAT_H 18 #define LLVM_ADT_APFLOAT_H 19 20 #include "llvm/ADT/APInt.h" 21 22 namespace llvm { 23 24 struct fltSemantics; 25 class APSInt; 26 class StringRef; 27 28 template <typename T> class SmallVectorImpl; 29 30 /// Enum that represents what fraction of the LSB truncated bits of an fp number 31 /// represent. 32 /// 33 /// This essentially combines the roles of guard and sticky bits. 34 enum lostFraction { // Example of truncated bits: 35 lfExactlyZero, // 000000 36 lfLessThanHalf, // 0xxxxx x's not all zero 37 lfExactlyHalf, // 100000 38 lfMoreThanHalf // 1xxxxx x's not all zero 39 }; 40 41 /// \brief A self-contained host- and target-independent arbitrary-precision 42 /// floating-point software implementation. 43 /// 44 /// APFloat uses bignum integer arithmetic as provided by static functions in 45 /// the APInt class. The library will work with bignum integers whose parts are 46 /// any unsigned type at least 16 bits wide, but 64 bits is recommended. 47 /// 48 /// Written for clarity rather than speed, in particular with a view to use in 49 /// the front-end of a cross compiler so that target arithmetic can be correctly 50 /// performed on the host. Performance should nonetheless be reasonable, 51 /// particularly for its intended use. It may be useful as a base 52 /// implementation for a run-time library during development of a faster 53 /// target-specific one. 54 /// 55 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all 56 /// implemented operations. Currently implemented operations are add, subtract, 57 /// multiply, divide, fused-multiply-add, conversion-to-float, 58 /// conversion-to-integer and conversion-from-integer. New rounding modes 59 /// (e.g. away from zero) can be added with three or four lines of code. 60 /// 61 /// Four formats are built-in: IEEE single precision, double precision, 62 /// quadruple precision, and x87 80-bit extended double (when operating with 63 /// full extended precision). Adding a new format that obeys IEEE semantics 64 /// only requires adding two lines of code: a declaration and definition of the 65 /// format. 66 /// 67 /// All operations return the status of that operation as an exception bit-mask, 68 /// so multiple operations can be done consecutively with their results or-ed 69 /// together. The returned status can be useful for compiler diagnostics; e.g., 70 /// inexact, underflow and overflow can be easily diagnosed on constant folding, 71 /// and compiler optimizers can determine what exceptions would be raised by 72 /// folding operations and optimize, or perhaps not optimize, accordingly. 73 /// 74 /// At present, underflow tininess is detected after rounding; it should be 75 /// straight forward to add support for the before-rounding case too. 76 /// 77 /// The library reads hexadecimal floating point numbers as per C99, and 78 /// correctly rounds if necessary according to the specified rounding mode. 79 /// Syntax is required to have been validated by the caller. It also converts 80 /// floating point numbers to hexadecimal text as per the C99 %a and %A 81 /// conversions. The output precision (or alternatively the natural minimal 82 /// precision) can be specified; if the requested precision is less than the 83 /// natural precision the output is correctly rounded for the specified rounding 84 /// mode. 85 /// 86 /// It also reads decimal floating point numbers and correctly rounds according 87 /// to the specified rounding mode. 88 /// 89 /// Conversion to decimal text is not currently implemented. 90 /// 91 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit 92 /// signed exponent, and the significand as an array of integer parts. After 93 /// normalization of a number of precision P the exponent is within the range of 94 /// the format, and if the number is not denormal the P-th bit of the 95 /// significand is set as an explicit integer bit. For denormals the most 96 /// significant bit is shifted right so that the exponent is maintained at the 97 /// format's minimum, so that the smallest denormal has just the least 98 /// significant bit of the significand set. The sign of zeroes and infinities 99 /// is significant; the exponent and significand of such numbers is not stored, 100 /// but has a known implicit (deterministic) value: 0 for the significands, 0 101 /// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and 102 /// significand are deterministic, although not really meaningful, and preserved 103 /// in non-conversion operations. The exponent is implicitly all 1 bits. 104 /// 105 /// APFloat does not provide any exception handling beyond default exception 106 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause 107 /// by encoding Signaling NaNs with the first bit of its trailing significand as 108 /// 0. 109 /// 110 /// TODO 111 /// ==== 112 /// 113 /// Some features that may or may not be worth adding: 114 /// 115 /// Binary to decimal conversion (hard). 116 /// 117 /// Optional ability to detect underflow tininess before rounding. 118 /// 119 /// New formats: x87 in single and double precision mode (IEEE apart from 120 /// extended exponent range) (hard). 121 /// 122 /// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward. 123 /// 124 class APFloat { 125 public: 126 127 /// A signed type to represent a floating point numbers unbiased exponent. 128 typedef signed short ExponentType; 129 130 /// \name Floating Point Semantics. 131 /// @{ 132 133 static const fltSemantics IEEEhalf; 134 static const fltSemantics IEEEsingle; 135 static const fltSemantics IEEEdouble; 136 static const fltSemantics IEEEquad; 137 static const fltSemantics PPCDoubleDouble; 138 static const fltSemantics x87DoubleExtended; 139 140 /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with 141 /// anything real. 142 static const fltSemantics Bogus; 143 144 /// @} 145 146 static unsigned int semanticsPrecision(const fltSemantics &); 147 static ExponentType semanticsMinExponent(const fltSemantics &); 148 static ExponentType semanticsMaxExponent(const fltSemantics &); 149 static unsigned int semanticsSizeInBits(const fltSemantics &); 150 151 /// IEEE-754R 5.11: Floating Point Comparison Relations. 152 enum cmpResult { 153 cmpLessThan, 154 cmpEqual, 155 cmpGreaterThan, 156 cmpUnordered 157 }; 158 159 /// IEEE-754R 4.3: Rounding-direction attributes. 160 enum roundingMode { 161 rmNearestTiesToEven, 162 rmTowardPositive, 163 rmTowardNegative, 164 rmTowardZero, 165 rmNearestTiesToAway 166 }; 167 168 /// IEEE-754R 7: Default exception handling. 169 /// 170 /// opUnderflow or opOverflow are always returned or-ed with opInexact. 171 enum opStatus { 172 opOK = 0x00, 173 opInvalidOp = 0x01, 174 opDivByZero = 0x02, 175 opOverflow = 0x04, 176 opUnderflow = 0x08, 177 opInexact = 0x10 178 }; 179 180 /// Category of internally-represented number. 181 enum fltCategory { 182 fcInfinity, 183 fcNaN, 184 fcNormal, 185 fcZero 186 }; 187 188 /// Convenience enum used to construct an uninitialized APFloat. 189 enum uninitializedTag { 190 uninitialized 191 }; 192 193 /// \name Constructors 194 /// @{ 195 196 APFloat(const fltSemantics &); // Default construct to 0.0 197 APFloat(const fltSemantics &, StringRef); 198 APFloat(const fltSemantics &, integerPart); 199 APFloat(const fltSemantics &, uninitializedTag); 200 APFloat(const fltSemantics &, const APInt &); 201 explicit APFloat(double d); 202 explicit APFloat(float f); 203 APFloat(const APFloat &); 204 APFloat(APFloat &&); 205 ~APFloat(); 206 207 /// @} 208 209 /// \brief Returns whether this instance allocated memory. 210 bool needsCleanup() const { return partCount() > 1; } 211 212 /// \name Convenience "constructors" 213 /// @{ 214 215 /// Factory for Positive and Negative Zero. 216 /// 217 /// \param Negative True iff the number should be negative. 218 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) { 219 APFloat Val(Sem, uninitialized); 220 Val.makeZero(Negative); 221 return Val; 222 } 223 224 /// Factory for Positive and Negative Infinity. 225 /// 226 /// \param Negative True iff the number should be negative. 227 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) { 228 APFloat Val(Sem, uninitialized); 229 Val.makeInf(Negative); 230 return Val; 231 } 232 233 /// Factory for QNaN values. 234 /// 235 /// \param Negative - True iff the NaN generated should be negative. 236 /// \param type - The unspecified fill bits for creating the NaN, 0 by 237 /// default. The value is truncated as necessary. 238 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false, 239 unsigned type = 0) { 240 if (type) { 241 APInt fill(64, type); 242 return getQNaN(Sem, Negative, &fill); 243 } else { 244 return getQNaN(Sem, Negative, nullptr); 245 } 246 } 247 248 /// Factory for QNaN values. 249 static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false, 250 const APInt *payload = nullptr) { 251 return makeNaN(Sem, false, Negative, payload); 252 } 253 254 /// Factory for SNaN values. 255 static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false, 256 const APInt *payload = nullptr) { 257 return makeNaN(Sem, true, Negative, payload); 258 } 259 260 /// Returns the largest finite number in the given semantics. 261 /// 262 /// \param Negative - True iff the number should be negative 263 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false); 264 265 /// Returns the smallest (by magnitude) finite number in the given semantics. 266 /// Might be denormalized, which implies a relative loss of precision. 267 /// 268 /// \param Negative - True iff the number should be negative 269 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false); 270 271 /// Returns the smallest (by magnitude) normalized finite number in the given 272 /// semantics. 273 /// 274 /// \param Negative - True iff the number should be negative 275 static APFloat getSmallestNormalized(const fltSemantics &Sem, 276 bool Negative = false); 277 278 /// Returns a float which is bitcasted from an all one value int. 279 /// 280 /// \param BitWidth - Select float type 281 /// \param isIEEE - If 128 bit number, select between PPC and IEEE 282 static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false); 283 284 /// Returns the size of the floating point number (in bits) in the given 285 /// semantics. 286 static unsigned getSizeInBits(const fltSemantics &Sem); 287 288 /// @} 289 290 /// Used to insert APFloat objects, or objects that contain APFloat objects, 291 /// into FoldingSets. 292 void Profile(FoldingSetNodeID &NID) const; 293 294 /// \name Arithmetic 295 /// @{ 296 297 opStatus add(const APFloat &, roundingMode); 298 opStatus subtract(const APFloat &, roundingMode); 299 opStatus multiply(const APFloat &, roundingMode); 300 opStatus divide(const APFloat &, roundingMode); 301 /// IEEE remainder. 302 opStatus remainder(const APFloat &); 303 /// C fmod, or llvm frem. 304 opStatus mod(const APFloat &); 305 opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode); 306 opStatus roundToIntegral(roundingMode); 307 /// IEEE-754R 5.3.1: nextUp/nextDown. 308 opStatus next(bool nextDown); 309 310 /// \brief Operator+ overload which provides the default 311 /// \c nmNearestTiesToEven rounding mode and *no* error checking. 312 APFloat operator+(const APFloat &RHS) const { 313 APFloat Result = *this; 314 Result.add(RHS, rmNearestTiesToEven); 315 return Result; 316 } 317 318 /// \brief Operator- overload which provides the default 319 /// \c nmNearestTiesToEven rounding mode and *no* error checking. 320 APFloat operator-(const APFloat &RHS) const { 321 APFloat Result = *this; 322 Result.subtract(RHS, rmNearestTiesToEven); 323 return Result; 324 } 325 326 /// \brief Operator* overload which provides the default 327 /// \c nmNearestTiesToEven rounding mode and *no* error checking. 328 APFloat operator*(const APFloat &RHS) const { 329 APFloat Result = *this; 330 Result.multiply(RHS, rmNearestTiesToEven); 331 return Result; 332 } 333 334 /// \brief Operator/ overload which provides the default 335 /// \c nmNearestTiesToEven rounding mode and *no* error checking. 336 APFloat operator/(const APFloat &RHS) const { 337 APFloat Result = *this; 338 Result.divide(RHS, rmNearestTiesToEven); 339 return Result; 340 } 341 342 /// @} 343 344 /// \name Sign operations. 345 /// @{ 346 347 void changeSign(); 348 void clearSign(); 349 void copySign(const APFloat &); 350 351 /// \brief A static helper to produce a copy of an APFloat value with its sign 352 /// copied from some other APFloat. 353 static APFloat copySign(APFloat Value, const APFloat &Sign) { 354 Value.copySign(Sign); 355 return Value; 356 } 357 358 /// @} 359 360 /// \name Conversions 361 /// @{ 362 363 opStatus convert(const fltSemantics &, roundingMode, bool *); 364 opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, 365 bool *) const; 366 opStatus convertToInteger(APSInt &, roundingMode, bool *) const; 367 opStatus convertFromAPInt(const APInt &, bool, roundingMode); 368 opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, 369 bool, roundingMode); 370 opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, 371 bool, roundingMode); 372 opStatus convertFromString(StringRef, roundingMode); 373 APInt bitcastToAPInt() const; 374 double convertToDouble() const; 375 float convertToFloat() const; 376 377 /// @} 378 379 /// The definition of equality is not straightforward for floating point, so 380 /// we won't use operator==. Use one of the following, or write whatever it 381 /// is you really mean. 382 bool operator==(const APFloat &) const = delete; 383 384 /// IEEE comparison with another floating point number (NaNs compare 385 /// unordered, 0==-0). 386 cmpResult compare(const APFloat &) const; 387 388 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0). 389 bool bitwiseIsEqual(const APFloat &) const; 390 391 /// Write out a hexadecimal representation of the floating point value to DST, 392 /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d. 393 /// Return the number of characters written, excluding the terminating NUL. 394 unsigned int convertToHexString(char *dst, unsigned int hexDigits, 395 bool upperCase, roundingMode) const; 396 397 /// \name IEEE-754R 5.7.2 General operations. 398 /// @{ 399 400 /// IEEE-754R isSignMinus: Returns true if and only if the current value is 401 /// negative. 402 /// 403 /// This applies to zeros and NaNs as well. 404 bool isNegative() const { return sign; } 405 406 /// IEEE-754R isNormal: Returns true if and only if the current value is normal. 407 /// 408 /// This implies that the current value of the float is not zero, subnormal, 409 /// infinite, or NaN following the definition of normality from IEEE-754R. 410 bool isNormal() const { return !isDenormal() && isFiniteNonZero(); } 411 412 /// Returns true if and only if the current value is zero, subnormal, or 413 /// normal. 414 /// 415 /// This means that the value is not infinite or NaN. 416 bool isFinite() const { return !isNaN() && !isInfinity(); } 417 418 /// Returns true if and only if the float is plus or minus zero. 419 bool isZero() const { return category == fcZero; } 420 421 /// IEEE-754R isSubnormal(): Returns true if and only if the float is a 422 /// denormal. 423 bool isDenormal() const; 424 425 /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity. 426 bool isInfinity() const { return category == fcInfinity; } 427 428 /// Returns true if and only if the float is a quiet or signaling NaN. 429 bool isNaN() const { return category == fcNaN; } 430 431 /// Returns true if and only if the float is a signaling NaN. 432 bool isSignaling() const; 433 434 /// @} 435 436 /// \name Simple Queries 437 /// @{ 438 439 fltCategory getCategory() const { return category; } 440 const fltSemantics &getSemantics() const { return *semantics; } 441 bool isNonZero() const { return category != fcZero; } 442 bool isFiniteNonZero() const { return isFinite() && !isZero(); } 443 bool isPosZero() const { return isZero() && !isNegative(); } 444 bool isNegZero() const { return isZero() && isNegative(); } 445 446 /// Returns true if and only if the number has the smallest possible non-zero 447 /// magnitude in the current semantics. 448 bool isSmallest() const; 449 450 /// Returns true if and only if the number has the largest possible finite 451 /// magnitude in the current semantics. 452 bool isLargest() const; 453 454 /// Returns true if and only if the number is an exact integer. 455 bool isInteger() const; 456 457 /// @} 458 459 APFloat &operator=(const APFloat &); 460 APFloat &operator=(APFloat &&); 461 462 /// \brief Overload to compute a hash code for an APFloat value. 463 /// 464 /// Note that the use of hash codes for floating point values is in general 465 /// frought with peril. Equality is hard to define for these values. For 466 /// example, should negative and positive zero hash to different codes? Are 467 /// they equal or not? This hash value implementation specifically 468 /// emphasizes producing different codes for different inputs in order to 469 /// be used in canonicalization and memoization. As such, equality is 470 /// bitwiseIsEqual, and 0 != -0. 471 friend hash_code hash_value(const APFloat &Arg); 472 473 /// Converts this value into a decimal string. 474 /// 475 /// \param FormatPrecision The maximum number of digits of 476 /// precision to output. If there are fewer digits available, 477 /// zero padding will not be used unless the value is 478 /// integral and small enough to be expressed in 479 /// FormatPrecision digits. 0 means to use the natural 480 /// precision of the number. 481 /// \param FormatMaxPadding The maximum number of zeros to 482 /// consider inserting before falling back to scientific 483 /// notation. 0 means to always use scientific notation. 484 /// 485 /// Number Precision MaxPadding Result 486 /// ------ --------- ---------- ------ 487 /// 1.01E+4 5 2 10100 488 /// 1.01E+4 4 2 1.01E+4 489 /// 1.01E+4 5 1 1.01E+4 490 /// 1.01E-2 5 2 0.0101 491 /// 1.01E-2 4 2 0.0101 492 /// 1.01E-2 4 1 1.01E-2 493 void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0, 494 unsigned FormatMaxPadding = 3) const; 495 496 /// If this value has an exact multiplicative inverse, store it in inv and 497 /// return true. 498 bool getExactInverse(APFloat *inv) const; 499 500 /// \brief Enumeration of \c ilogb error results. 501 enum IlogbErrorKinds { 502 IEK_Zero = INT_MIN+1, 503 IEK_NaN = INT_MIN, 504 IEK_Inf = INT_MAX 505 }; 506 507 /// \brief Returns the exponent of the internal representation of the APFloat. 508 /// 509 /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)). 510 /// For special APFloat values, this returns special error codes: 511 /// 512 /// NaN -> \c IEK_NaN 513 /// 0 -> \c IEK_Zero 514 /// Inf -> \c IEK_Inf 515 /// 516 friend int ilogb(const APFloat &Arg); 517 518 /// \brief Returns: X * 2^Exp for integral exponents. 519 friend APFloat scalbn(APFloat X, int Exp, roundingMode); 520 521 friend APFloat frexp(const APFloat &X, int &Exp, roundingMode); 522 523 private: 524 525 /// \name Simple Queries 526 /// @{ 527 528 integerPart *significandParts(); 529 const integerPart *significandParts() const; 530 unsigned int partCount() const; 531 532 /// @} 533 534 /// \name Significand operations. 535 /// @{ 536 537 integerPart addSignificand(const APFloat &); 538 integerPart subtractSignificand(const APFloat &, integerPart); 539 lostFraction addOrSubtractSignificand(const APFloat &, bool subtract); 540 lostFraction multiplySignificand(const APFloat &, const APFloat *); 541 lostFraction divideSignificand(const APFloat &); 542 void incrementSignificand(); 543 void initialize(const fltSemantics *); 544 void shiftSignificandLeft(unsigned int); 545 lostFraction shiftSignificandRight(unsigned int); 546 unsigned int significandLSB() const; 547 unsigned int significandMSB() const; 548 void zeroSignificand(); 549 /// Return true if the significand excluding the integral bit is all ones. 550 bool isSignificandAllOnes() const; 551 /// Return true if the significand excluding the integral bit is all zeros. 552 bool isSignificandAllZeros() const; 553 554 /// @} 555 556 /// \name Arithmetic on special values. 557 /// @{ 558 559 opStatus addOrSubtractSpecials(const APFloat &, bool subtract); 560 opStatus divideSpecials(const APFloat &); 561 opStatus multiplySpecials(const APFloat &); 562 opStatus modSpecials(const APFloat &); 563 564 /// @} 565 566 /// \name Special value setters. 567 /// @{ 568 569 void makeLargest(bool Neg = false); 570 void makeSmallest(bool Neg = false); 571 void makeNaN(bool SNaN = false, bool Neg = false, 572 const APInt *fill = nullptr); 573 static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative, 574 const APInt *fill); 575 void makeInf(bool Neg = false); 576 void makeZero(bool Neg = false); 577 void makeQuiet(); 578 579 /// @} 580 581 /// \name Miscellany 582 /// @{ 583 584 bool convertFromStringSpecials(StringRef str); 585 opStatus normalize(roundingMode, lostFraction); 586 opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract); 587 cmpResult compareAbsoluteValue(const APFloat &) const; 588 opStatus handleOverflow(roundingMode); 589 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const; 590 opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool, 591 roundingMode, bool *) const; 592 opStatus convertFromUnsignedParts(const integerPart *, unsigned int, 593 roundingMode); 594 opStatus convertFromHexadecimalString(StringRef, roundingMode); 595 opStatus convertFromDecimalString(StringRef, roundingMode); 596 char *convertNormalToHexString(char *, unsigned int, bool, 597 roundingMode) const; 598 opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int, 599 roundingMode); 600 601 /// @} 602 603 APInt convertHalfAPFloatToAPInt() const; 604 APInt convertFloatAPFloatToAPInt() const; 605 APInt convertDoubleAPFloatToAPInt() const; 606 APInt convertQuadrupleAPFloatToAPInt() const; 607 APInt convertF80LongDoubleAPFloatToAPInt() const; 608 APInt convertPPCDoubleDoubleAPFloatToAPInt() const; 609 void initFromAPInt(const fltSemantics *Sem, const APInt &api); 610 void initFromHalfAPInt(const APInt &api); 611 void initFromFloatAPInt(const APInt &api); 612 void initFromDoubleAPInt(const APInt &api); 613 void initFromQuadrupleAPInt(const APInt &api); 614 void initFromF80LongDoubleAPInt(const APInt &api); 615 void initFromPPCDoubleDoubleAPInt(const APInt &api); 616 617 void assign(const APFloat &); 618 void copySignificand(const APFloat &); 619 void freeSignificand(); 620 621 /// The semantics that this value obeys. 622 const fltSemantics *semantics; 623 624 /// A binary fraction with an explicit integer bit. 625 /// 626 /// The significand must be at least one bit wider than the target precision. 627 union Significand { 628 integerPart part; 629 integerPart *parts; 630 } significand; 631 632 /// The signed unbiased exponent of the value. 633 ExponentType exponent; 634 635 /// What kind of floating point number this is. 636 /// 637 /// Only 2 bits are required, but VisualStudio incorrectly sign extends it. 638 /// Using the extra bit keeps it from failing under VisualStudio. 639 fltCategory category : 3; 640 641 /// Sign bit of the number. 642 unsigned int sign : 1; 643 }; 644 645 /// See friend declarations above. 646 /// 647 /// These additional declarations are required in order to compile LLVM with IBM 648 /// xlC compiler. 649 hash_code hash_value(const APFloat &Arg); 650 int ilogb(const APFloat &Arg); 651 APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode); 652 653 /// \brief Equivalent of C standard library function. 654 /// 655 /// While the C standard says Exp is an unspecified value for infinity and nan, 656 /// this returns INT_MAX for infinities, and INT_MIN for NaNs. 657 APFloat frexp(const APFloat &Val, int &Exp, APFloat::roundingMode RM); 658 659 /// \brief Returns the absolute value of the argument. 660 inline APFloat abs(APFloat X) { 661 X.clearSign(); 662 return X; 663 } 664 665 /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if 666 /// both are not NaN. If either argument is a NaN, returns the other argument. 667 LLVM_READONLY 668 inline APFloat minnum(const APFloat &A, const APFloat &B) { 669 if (A.isNaN()) 670 return B; 671 if (B.isNaN()) 672 return A; 673 return (B.compare(A) == APFloat::cmpLessThan) ? B : A; 674 } 675 676 /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if 677 /// both are not NaN. If either argument is a NaN, returns the other argument. 678 LLVM_READONLY 679 inline APFloat maxnum(const APFloat &A, const APFloat &B) { 680 if (A.isNaN()) 681 return B; 682 if (B.isNaN()) 683 return A; 684 return (A.compare(B) == APFloat::cmpLessThan) ? B : A; 685 } 686 687 } // namespace llvm 688 689 #endif // LLVM_ADT_APFLOAT_H 690