136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===- llvm/ADT/APFloat.h - Arbitrary Precision Floating Point ---*- C++ -*-==//
2b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//
3b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//                     The LLVM Compiler Infrastructure
4b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//
8b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//===----------------------------------------------------------------------===//
97f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
107f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// \file
117f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// \brief
127f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// This file declares a class to represent arbitrary precision floating point
137f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// values and provide a variety of arithmetic operations on them.
147f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
15b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//===----------------------------------------------------------------------===//
16b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
17674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_ADT_APFLOAT_H
18674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_ADT_APFLOAT_H
19b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
20b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner#include "llvm/ADT/APInt.h"
21b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
22b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattnernamespace llvm {
23b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
24fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanstruct fltSemantics;
25fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanclass APSInt;
26fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanclass StringRef;
27fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Enum that represents what fraction of the LSB truncated bits of an fp number
297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// represent.
307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
317f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// This essentially combines the roles of guard and sticky bits.
32fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanenum lostFraction { // Example of truncated bits:
33fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lfExactlyZero,    // 000000
34fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lfLessThanHalf,   // 0xxxxx  x's not all zero
35fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lfExactlyHalf,    // 100000
36fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lfMoreThanHalf    // 1xxxxx  x's not all zero
37fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman};
38fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
397f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// \brief A self-contained host- and target-independent arbitrary-precision
407f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// floating-point software implementation.
417f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
427f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// APFloat uses bignum integer arithmetic as provided by static functions in
437f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// the APInt class.  The library will work with bignum integers whose parts are
447f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// any unsigned type at least 16 bits wide, but 64 bits is recommended.
457f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Written for clarity rather than speed, in particular with a view to use in
477f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// the front-end of a cross compiler so that target arithmetic can be correctly
487f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// performed on the host.  Performance should nonetheless be reasonable,
497f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// particularly for its intended use.  It may be useful as a base
507f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// implementation for a run-time library during development of a faster
517f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// target-specific one.
527f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
537f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
547f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// implemented operations.  Currently implemented operations are add, subtract,
557f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// multiply, divide, fused-multiply-add, conversion-to-float,
567f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// conversion-to-integer and conversion-from-integer.  New rounding modes
577f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// (e.g. away from zero) can be added with three or four lines of code.
587f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
597f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Four formats are built-in: IEEE single precision, double precision,
607f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// quadruple precision, and x87 80-bit extended double (when operating with
617f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// full extended precision).  Adding a new format that obeys IEEE semantics
627f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// only requires adding two lines of code: a declaration and definition of the
637f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// format.
647f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
657f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// All operations return the status of that operation as an exception bit-mask,
667f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// so multiple operations can be done consecutively with their results or-ed
677f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// together.  The returned status can be useful for compiler diagnostics; e.g.,
687f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// inexact, underflow and overflow can be easily diagnosed on constant folding,
697f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// and compiler optimizers can determine what exceptions would be raised by
707f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// folding operations and optimize, or perhaps not optimize, accordingly.
717f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
727f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// At present, underflow tininess is detected after rounding; it should be
737f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// straight forward to add support for the before-rounding case too.
747f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// The library reads hexadecimal floating point numbers as per C99, and
767f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// correctly rounds if necessary according to the specified rounding mode.
777f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Syntax is required to have been validated by the caller.  It also converts
787f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// floating point numbers to hexadecimal text as per the C99 %a and %A
797f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// conversions.  The output precision (or alternatively the natural minimal
807f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// precision) can be specified; if the requested precision is less than the
817f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// natural precision the output is correctly rounded for the specified rounding
827f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// mode.
837f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
847f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// It also reads decimal floating point numbers and correctly rounds according
857f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// to the specified rounding mode.
867f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
877f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Conversion to decimal text is not currently implemented.
887f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
897f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
907f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// signed exponent, and the significand as an array of integer parts.  After
917f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// normalization of a number of precision P the exponent is within the range of
927f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// the format, and if the number is not denormal the P-th bit of the
937f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significand is set as an explicit integer bit.  For denormals the most
947f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significant bit is shifted right so that the exponent is maintained at the
957f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// format's minimum, so that the smallest denormal has just the least
967f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significant bit of the significand set.  The sign of zeroes and infinities
977f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// is significant; the exponent and significand of such numbers is not stored,
987f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// but has a known implicit (deterministic) value: 0 for the significands, 0
997f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// for zero exponent, all 1 bits for infinity exponent.  For NaNs the sign and
1007f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significand are deterministic, although not really meaningful, and preserved
1017f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// in non-conversion operations.  The exponent is implicitly all 1 bits.
1027f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1037f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// APFloat does not provide any exception handling beyond default exception
1047f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
1057f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// by encoding Signaling NaNs with the first bit of its trailing significand as
1067f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 0.
1077f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1087f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// TODO
1097f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// ====
1107f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1117f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Some features that may or may not be worth adding:
1127f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1137f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Binary to decimal conversion (hard).
1147f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1157f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Optional ability to detect underflow tininess before rounding.
1167f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1177f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// New formats: x87 in single and double precision mode (IEEE apart from
1187f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// extended exponent range) (hard).
1197f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
1207ffc854002282b0a6fd018d967a6def971dc341eMichael Gottesman/// New operations: sqrt, IEEE remainder, C90 fmod, nexttoward.
1217f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
122fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanclass APFloat {
123fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanpublic:
124fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
125db045ab1532469551a56e5811e684920a6f7a10cMichael Gottesman  /// A signed type to represent a floating point numbers unbiased exponent.
126db045ab1532469551a56e5811e684920a6f7a10cMichael Gottesman  typedef signed short ExponentType;
127db045ab1532469551a56e5811e684920a6f7a10cMichael Gottesman
1287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Floating Point Semantics.
1297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
1307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
131fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics IEEEhalf;
132fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics IEEEsingle;
133fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics IEEEdouble;
134fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics IEEEquad;
135fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics PPCDoubleDouble;
136fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics x87DoubleExtended;
1377f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
1387f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with
1397f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// anything real.
140fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static const fltSemantics Bogus;
141fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
1422e2922cd90922eaf3736421bd0ad4331f89f3e75Michael Gottesman  /// @}
1432e2922cd90922eaf3736421bd0ad4331f89f3e75Michael Gottesman
144fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static unsigned int semanticsPrecision(const fltSemantics &);
145fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
1467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// IEEE-754R 5.11: Floating Point Comparison Relations.
147fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  enum cmpResult {
148fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    cmpLessThan,
149fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    cmpEqual,
150fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    cmpGreaterThan,
151fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    cmpUnordered
152b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  };
153b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
1547f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// IEEE-754R 4.3: Rounding-direction attributes.
155fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  enum roundingMode {
156fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    rmNearestTiesToEven,
157fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    rmTowardPositive,
158fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    rmTowardNegative,
159fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    rmTowardZero,
160fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    rmNearestTiesToAway
161fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  };
162e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
1637f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// IEEE-754R 7: Default exception handling.
1647f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  ///
1657f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// opUnderflow or opOverflow are always returned or-ed with opInexact.
166fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  enum opStatus {
167fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    opOK = 0x00,
168fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    opInvalidOp = 0x01,
169fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    opDivByZero = 0x02,
170fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    opOverflow = 0x04,
171fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    opUnderflow = 0x08,
172fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    opInexact = 0x10
173fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  };
174e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
1757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Category of internally-represented number.
176fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  enum fltCategory {
177fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    fcInfinity,
178fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    fcNaN,
179fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    fcNormal,
180fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    fcZero
181fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  };
1823a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
1837f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Convenience enum used to construct an uninitialized APFloat.
184fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  enum uninitializedTag {
185fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    uninitialized
186b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  };
1873a9b71434cda6f66d65a031effec1bbe58e1dda3Rafael Espindola
1887f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Constructors
1897f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
1907f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
191fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat(const fltSemantics &); // Default construct to 0.0
192fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat(const fltSemantics &, StringRef);
193fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat(const fltSemantics &, integerPart);
194fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat(const fltSemantics &, uninitializedTag);
195fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat(const fltSemantics &, const APInt &);
196fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  explicit APFloat(double d);
197fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  explicit APFloat(float f);
198fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat(const APFloat &);
19936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  APFloat(APFloat &&);
200fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ~APFloat();
201fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2027f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
2037f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
204abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek  /// \brief Returns whether this instance allocated memory.
205abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek  bool needsCleanup() const { return partCount() > 1; }
206abff3aa8217049cffe6da77c91e510c66b8c2313Manuel Klimek
2077f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Convenience "constructors"
2087f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
2097f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
2108a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman  /// Factory for Positive and Negative Zero.
2118a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman  ///
2128a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman  /// \param Negative True iff the number should be negative.
213fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
214fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman    APFloat Val(Sem, uninitialized);
215fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman    Val.makeZero(Negative);
216fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman    return Val;
217fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  }
2188a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman
2198a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman  /// Factory for Positive and Negative Infinity.
2208a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman  ///
2218a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman  /// \param Negative True iff the number should be negative.
222fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
223fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman    APFloat Val(Sem, uninitialized);
224fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman    Val.makeInf(Negative);
225fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman    return Val;
226fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  }
227fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Factory for QNaN values.
229fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
230fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param Negative - True iff the NaN generated should be negative.
231fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param type - The unspecified fill bits for creating the NaN, 0 by
232fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// default.  The value is truncated as necessary.
233fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
234fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                        unsigned type = 0) {
235fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    if (type) {
236fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman      APInt fill(64, type);
237fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman      return getQNaN(Sem, Negative, &fill);
238fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    } else {
239dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return getQNaN(Sem, Negative, nullptr);
240fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    }
241fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  }
242fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2437f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Factory for QNaN values.
244fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false,
245dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         const APInt *payload = nullptr) {
246fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    return makeNaN(Sem, false, Negative, payload);
247fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  }
248fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2497f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Factory for SNaN values.
250fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false,
251dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         const APInt *payload = nullptr) {
252fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    return makeNaN(Sem, true, Negative, payload);
253fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  }
254fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2557f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Returns the largest finite number in the given semantics.
256fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
257fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param Negative - True iff the number should be negative
258fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
259fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2607f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Returns the smallest (by magnitude) finite number in the given semantics.
2617f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Might be denormalized, which implies a relative loss of precision.
262fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
263fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param Negative - True iff the number should be negative
264fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
265fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2667f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Returns the smallest (by magnitude) normalized finite number in the given
2677f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// semantics.
268fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
269fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param Negative - True iff the number should be negative
270fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getSmallestNormalized(const fltSemantics &Sem,
271fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                       bool Negative = false);
272fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2737f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Returns a float which is bitcasted from an all one value int.
274fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
275fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param BitWidth - Select float type
276fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param isIEEE   - If 128 bit number, select between PPC and IEEE
277fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
278fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2797f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
2807f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
2817f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Used to insert APFloat objects, or objects that contain APFloat objects,
2827f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// into FoldingSets.
283fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void Profile(FoldingSetNodeID &NID) const;
284fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
2857f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Arithmetic
2867f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
2877f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
288fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus add(const APFloat &, roundingMode);
289fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus subtract(const APFloat &, roundingMode);
290fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus multiply(const APFloat &, roundingMode);
291fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus divide(const APFloat &, roundingMode);
2927f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// IEEE remainder.
293fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus remainder(const APFloat &);
2947f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// C fmod, or llvm frem.
295fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus mod(const APFloat &, roundingMode);
296fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
297fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus roundToIntegral(roundingMode);
298964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  /// IEEE-754R 5.3.1: nextUp/nextDown.
299964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  opStatus next(bool nextDown);
300fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
30137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Operator+ overload which provides the default
30237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \c nmNearestTiesToEven rounding mode and *no* error checking.
30337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  APFloat operator+(const APFloat &RHS) const {
30437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    APFloat Result = *this;
30537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Result.add(RHS, rmNearestTiesToEven);
30637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Result;
30737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
30837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
30937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Operator- overload which provides the default
31037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \c nmNearestTiesToEven rounding mode and *no* error checking.
31137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  APFloat operator-(const APFloat &RHS) const {
31237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    APFloat Result = *this;
31337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Result.subtract(RHS, rmNearestTiesToEven);
31437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Result;
31537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
31637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
31737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Operator* overload which provides the default
31837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \c nmNearestTiesToEven rounding mode and *no* error checking.
31937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  APFloat operator*(const APFloat &RHS) const {
32037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    APFloat Result = *this;
32137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Result.multiply(RHS, rmNearestTiesToEven);
32237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Result;
32337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
32437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Operator/ overload which provides the default
32637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \c nmNearestTiesToEven rounding mode and *no* error checking.
32737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  APFloat operator/(const APFloat &RHS) const {
32837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    APFloat Result = *this;
32937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Result.divide(RHS, rmNearestTiesToEven);
33037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Result;
33137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
33237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
333d3d03fe758d4ee93ea225c6740742eb56b6dca3bMichael Gottesman  /// @}
334d3d03fe758d4ee93ea225c6740742eb56b6dca3bMichael Gottesman
3357f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Sign operations.
3367f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
3377f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
338fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void changeSign();
339fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void clearSign();
340fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void copySign(const APFloat &);
341fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
34237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief A static helper to produce a copy of an APFloat value with its sign
34337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// copied from some other APFloat.
34437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  static APFloat copySign(APFloat Value, const APFloat &Sign) {
34537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Value.copySign(Sign);
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return std::move(Value);
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
34837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
3497f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
3507f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
3517f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Conversions
3527f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
3537f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
354fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convert(const fltSemantics &, roundingMode, bool *);
355fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode,
356fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                            bool *) const;
357fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertToInteger(APSInt &, roundingMode, bool *) const;
358fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromAPInt(const APInt &, bool, roundingMode);
359fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
360fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                          bool, roundingMode);
361fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
362fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                          bool, roundingMode);
363fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromString(StringRef, roundingMode);
364fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt bitcastToAPInt() const;
365fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  double convertToDouble() const;
366fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  float convertToFloat() const;
367fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
3687f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
3697f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
3707f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// The definition of equality is not straightforward for floating point, so
3717f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// we won't use operator==.  Use one of the following, or write whatever it
3727f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// is you really mean.
373ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool operator==(const APFloat &) const = delete;
374fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
3757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// IEEE comparison with another floating point number (NaNs compare
3767f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// unordered, 0==-0).
377fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  cmpResult compare(const APFloat &) const;
378fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
3797f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
380fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  bool bitwiseIsEqual(const APFloat &) const;
381fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
3827f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Write out a hexadecimal representation of the floating point value to DST,
3837f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d.
3847f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Return the number of characters written, excluding the terminating NUL.
385fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  unsigned int convertToHexString(char *dst, unsigned int hexDigits,
386fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                  bool upperCase, roundingMode) const;
387fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
388b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// \name IEEE-754R 5.7.2 General operations.
389b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// @{
390b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
391b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// IEEE-754R isSignMinus: Returns true if and only if the current value is
392b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// negative.
393b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  ///
394b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// This applies to zeros and NaNs as well.
395b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isNegative() const { return sign; }
396b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
397b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
398b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  ///
399b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// This implies that the current value of the float is not zero, subnormal,
400b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// infinite, or NaN following the definition of normality from IEEE-754R.
401a1694e578492c90c246e59ec861c1a34f8ad7b4dMichael Gottesman  bool isNormal() const { return !isDenormal() && isFiniteNonZero(); }
402b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
403b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// Returns true if and only if the current value is zero, subnormal, or
404b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// normal.
405b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  ///
406b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// This means that the value is not infinite or NaN.
407b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isFinite() const { return !isNaN() && !isInfinity(); }
408b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
409b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// Returns true if and only if the float is plus or minus zero.
410b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isZero() const { return category == fcZero; }
411b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
412b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
413b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// denormal.
414b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isDenormal() const;
415b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
416b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
417b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isInfinity() const { return category == fcInfinity; }
418b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
419b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// Returns true if and only if the float is a quiet or signaling NaN.
420b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isNaN() const { return category == fcNaN; }
421b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
422b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// Returns true if and only if the float is a signaling NaN.
423b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  bool isSignaling() const;
424b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
425b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman  /// @}
426b30718af1ada4b68c7c44e3dcbf4891efd5e3ba1Michael Gottesman
4277f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Simple Queries
4287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
4297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
430fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  fltCategory getCategory() const { return category; }
431fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  const fltSemantics &getSemantics() const { return *semantics; }
432fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  bool isNonZero() const { return category != fcZero; }
4337032c883cdf8da579fbf9bf499d36a711eef676fMichael Gottesman  bool isFiniteNonZero() const { return isFinite() && !isZero(); }
434fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  bool isPosZero() const { return isZero() && !isNegative(); }
435fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  bool isNegZero() const { return isZero() && isNegative(); }
436fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
437c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman  /// Returns true if and only if the number has the smallest possible non-zero
438c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman  /// magnitude in the current semantics.
439c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman  bool isSmallest() const;
440c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman
441c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman  /// Returns true if and only if the number has the largest possible finite
442c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman  /// magnitude in the current semantics.
443c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman  bool isLargest() const;
444c896020dd8d6c2f25f5bf7eb9191fd4e72407015Michael Gottesman
4457f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
4467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
447fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APFloat &operator=(const APFloat &);
44836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  APFloat &operator=(APFloat &&);
449fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
450fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \brief Overload to compute a hash code for an APFloat value.
451fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
452fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// Note that the use of hash codes for floating point values is in general
453fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// frought with peril. Equality is hard to define for these values. For
454fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// example, should negative and positive zero hash to different codes? Are
455fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// they equal or not? This hash value implementation specifically
456fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// emphasizes producing different codes for different inputs in order to
457fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// be used in canonicalization and memoization. As such, equality is
458fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// bitwiseIsEqual, and 0 != -0.
459fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  friend hash_code hash_value(const APFloat &Arg);
460fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
461fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// Converts this value into a decimal string.
462fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
463fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param FormatPrecision The maximum number of digits of
464fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   precision to output.  If there are fewer digits available,
465fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   zero padding will not be used unless the value is
466fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   integral and small enough to be expressed in
467fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   FormatPrecision digits.  0 means to use the natural
468fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   precision of the number.
469fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// \param FormatMaxPadding The maximum number of zeros to
470fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   consider inserting before falling back to scientific
471fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///   notation.  0 means to always use scientific notation.
472fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  ///
473fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// Number       Precision    MaxPadding      Result
474fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// ------       ---------    ----------      ------
475fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// 1.01E+4              5             2       10100
476fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// 1.01E+4              4             2       1.01E+4
477fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// 1.01E+4              5             1       1.01E+4
478fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// 1.01E-2              5             2       0.0101
479fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// 1.01E-2              4             2       0.0101
480fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  /// 1.01E-2              4             1       1.01E-2
481fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0,
482fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                unsigned FormatMaxPadding = 3) const;
483fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
4847f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// If this value has an exact multiplicative inverse, store it in inv and
4857f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// return true.
486fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  bool getExactInverse(APFloat *inv) const;
487fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
48837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Enumeration of \c ilogb error results.
48937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  enum IlogbErrorKinds {
49037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    IEK_Zero = INT_MIN+1,
49137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    IEK_NaN = INT_MIN,
49237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    IEK_Inf = INT_MAX
49337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  };
49437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
49537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Returns the exponent of the internal representation of the APFloat.
49637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
49737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Because the radix of APFloat is 2, this is equivalent to floor(log2(x)).
49837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// For special APFloat values, this returns special error codes:
49937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
50037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///   NaN -> \c IEK_NaN
50137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///   0   -> \c IEK_Zero
50237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///   Inf -> \c IEK_Inf
50337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
50437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  friend int ilogb(const APFloat &Arg) {
50537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (Arg.isNaN())
50637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      return IEK_NaN;
50737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (Arg.isZero())
50837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      return IEK_Zero;
50937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (Arg.isInfinity())
51037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      return IEK_Inf;
51137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
51237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Arg.exponent;
51337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
51437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
51537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Returns: X * 2^Exp for integral exponents.
51637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  friend APFloat scalbn(APFloat X, int Exp);
51737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
518fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanprivate:
519fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
5207f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Simple Queries
5217f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
5227f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
523fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  integerPart *significandParts();
524fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  const integerPart *significandParts() const;
525fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  unsigned int partCount() const;
526fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
5277f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
5287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
5297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Significand operations.
5307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
5317f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
532fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  integerPart addSignificand(const APFloat &);
533fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  integerPart subtractSignificand(const APFloat &, integerPart);
534fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
535fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lostFraction multiplySignificand(const APFloat &, const APFloat *);
536fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lostFraction divideSignificand(const APFloat &);
537fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void incrementSignificand();
538fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initialize(const fltSemantics *);
539fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void shiftSignificandLeft(unsigned int);
540fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  lostFraction shiftSignificandRight(unsigned int);
541fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  unsigned int significandLSB() const;
542fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  unsigned int significandMSB() const;
543fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void zeroSignificand();
544964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  /// Return true if the significand excluding the integral bit is all ones.
545964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  bool isSignificandAllOnes() const;
546964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  /// Return true if the significand excluding the integral bit is all zeros.
547964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  bool isSignificandAllZeros() const;
548fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
5497f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
5507f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
5517f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Arithmetic on special values.
5527f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
5537f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
554fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
555fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus divideSpecials(const APFloat &);
556fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus multiplySpecials(const APFloat &);
557fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus modSpecials(const APFloat &);
558fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
5597f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
5607f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
5617f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Special value setters.
5627f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
5637f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
564964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  void makeLargest(bool Neg = false);
565964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman  void makeSmallest(bool Neg = false);
566dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void makeNaN(bool SNaN = false, bool Neg = false,
567dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines               const APInt *fill = nullptr);
568fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
569fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                         const APInt *fill);
570fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman  void makeInf(bool Neg = false);
571fdec0c7a7302702d08f1221609221018af8085ecMichael Gottesman  void makeZero(bool Neg = false);
572964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman
5737f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
5747f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
5757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// \name Miscellany
5767f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @{
5777f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
578575694b6a7d49004acc93294bc88d0bc337e4d27Michael Gottesman  bool convertFromStringSpecials(StringRef str);
579fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus normalize(roundingMode, lostFraction);
580fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
581fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  cmpResult compareAbsoluteValue(const APFloat &) const;
582fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus handleOverflow(roundingMode);
583fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
584fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
585fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                        roundingMode, bool *) const;
586fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
587fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                    roundingMode);
588fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromHexadecimalString(StringRef, roundingMode);
589fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus convertFromDecimalString(StringRef, roundingMode);
590fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  char *convertNormalToHexString(char *, unsigned int, bool,
591fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                 roundingMode) const;
592fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
593fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman                                        roundingMode);
594fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
5957f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// @}
5967f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman
597fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt convertHalfAPFloatToAPInt() const;
598fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt convertFloatAPFloatToAPInt() const;
599fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt convertDoubleAPFloatToAPInt() const;
600fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt convertQuadrupleAPFloatToAPInt() const;
601fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt convertF80LongDoubleAPFloatToAPInt() const;
602fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
603fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromAPInt(const fltSemantics *Sem, const APInt &api);
604fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromHalfAPInt(const APInt &api);
605fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromFloatAPInt(const APInt &api);
606fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromDoubleAPInt(const APInt &api);
607fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromQuadrupleAPInt(const APInt &api);
608fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromF80LongDoubleAPInt(const APInt &api);
609fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void initFromPPCDoubleDoubleAPInt(const APInt &api);
610fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
611fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void assign(const APFloat &);
612fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void copySignificand(const APFloat &);
613fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  void freeSignificand();
614fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
6157f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// The semantics that this value obeys.
616fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  const fltSemantics *semantics;
617fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
6187f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// A binary fraction with an explicit integer bit.
6197f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  ///
6207f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// The significand must be at least one bit wider than the target precision.
621fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  union Significand {
622fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    integerPart part;
623fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman    integerPart *parts;
624fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  } significand;
625fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
6267f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// The signed unbiased exponent of the value.
627db045ab1532469551a56e5811e684920a6f7a10cMichael Gottesman  ExponentType exponent;
628fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
6297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// What kind of floating point number this is.
6307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  ///
6317f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
6327f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Using the extra bit keeps it from failing under VisualStudio.
633fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  fltCategory category : 3;
634fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
6357f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman  /// Sign bit of the number.
636fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman  unsigned int sign : 1;
637fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman};
638fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman
63937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// See friend declarations above.
6407f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman///
64137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// These additional declarations are required in order to compile LLVM with IBM
6427f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// xlC compiler.
643fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanhash_code hash_value(const APFloat &Arg);
64437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesAPFloat scalbn(APFloat X, int Exp);
64537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
64637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Returns the absolute value of the argument.
64737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesinline APFloat abs(APFloat X) {
64837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  X.clearSign();
64937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return X;
65037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
65137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
65237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
65337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// both are not NaN. If either argument is a NaN, returns the other argument.
65437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesLLVM_READONLY
65537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesinline APFloat minnum(const APFloat &A, const APFloat &B) {
65637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (A.isNaN())
65737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return B;
65837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (B.isNaN())
65937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return A;
66037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return (B.compare(A) == APFloat::cmpLessThan) ? B : A;
66137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
66237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
66337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
66437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// both are not NaN. If either argument is a NaN, returns the other argument.
66537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesLLVM_READONLY
66637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesinline APFloat maxnum(const APFloat &A, const APFloat &B) {
66737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (A.isNaN())
66837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return B;
66937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (B.isNaN())
67037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return A;
67137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return (A.compare(B) == APFloat::cmpLessThan) ? B : A;
67237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
67337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
6747f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman} // namespace llvm
675b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
6767f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman#endif // LLVM_ADT_APFLOAT_H
677