APFloat.h 24 KB

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