APFloat.h revision e85fe660e4e99d30ca9292b706b8ffe6d0367dca
1b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//== llvm/Support/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//===----------------------------------------------------------------------===//
9b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//
10b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// This file declares a class to represent arbitrary precision floating
11b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// point values and provide a variety of arithmetic operations on them.
12b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//
13b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//===----------------------------------------------------------------------===//
14b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
15b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner/*  A self-contained host- and target-independent arbitrary-precision
16a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    floating-point software implementation.  It uses bignum integer
17a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    arithmetic as provided by static functions in the APInt class.
18b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    The library will work with bignum integers whose parts are any
19a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    unsigned type at least 16 bits wide, but 64 bits is recommended.
20b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
21b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    Written for clarity rather than speed, in particular with a view
22b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    to use in the front-end of a cross compiler so that target
23b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    arithmetic can be correctly performed on the host.  Performance
24b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    should nonetheless be reasonable, particularly for its intended
25b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    use.  It may be useful as a base implementation for a run-time
26b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    library during development of a faster target-specific one.
27b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
28b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    All 5 rounding modes in the IEEE-754R draft are handled correctly
29b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    for all implemented operations.  Currently implemented operations
30b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    are add, subtract, multiply, divide, fused-multiply-add,
31b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    conversion-to-float, conversion-to-integer and
32b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    conversion-from-integer.  New rounding modes (e.g. away from zero)
33a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    can be added with three or four lines of code.
34b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
35b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    Four formats are built-in: IEEE single precision, double
36b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    precision, quadruple precision, and x87 80-bit extended double
37b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    (when operating with full extended precision).  Adding a new
38b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    format that obeys IEEE semantics only requires adding two lines of
39b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    code: a declaration and definition of the format.
40b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
41b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    All operations return the status of that operation as an exception
42b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bit-mask, so multiple operations can be done consecutively with
43b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    their results or-ed together.  The returned status can be useful
44b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    for compiler diagnostics; e.g., inexact, underflow and overflow
45b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    can be easily diagnosed on constant folding, and compiler
46b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    optimizers can determine what exceptions would be raised by
47b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    folding operations and optimize, or perhaps not optimize,
48b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    accordingly.
49b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
50b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    At present, underflow tininess is detected after rounding; it
51b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    should be straight forward to add support for the before-rounding
52b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    case too.
53b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
54a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    The library reads hexadecimal floating point numbers as per C99,
55a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    and correctly rounds if necessary according to the specified
56a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    rounding mode.  Syntax is required to have been validated by the
57a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    caller.  It also converts floating point numbers to hexadecimal
58a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    text as per the C99 %a and %A conversions.  The output precision
59a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    (or alternatively the natural minimal precision) can be specified;
60a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    if the requested precision is less than the natural precision the
61a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    output is correctly rounded for the specified rounding mode.
62a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth
6396c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    It also reads decimal floating point numbers and correctly rounds
6496c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    according to the specified rounding mode.
6596c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth
6696c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    Conversion to decimal text is not currently implemented.
67a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth
68b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    Non-zero finite numbers are represented internally as a sign bit,
69b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    a 16-bit signed exponent, and the significand as an array of
70b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integer parts.  After normalization of a number of precision P the
71b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    exponent is within the range of the format, and if the number is
72b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    not denormal the P-th bit of the significand is set as an explicit
73b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integer bit.  For denormals the most significant bit is shifted
74b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    right so that the exponent is maintained at the format's minimum,
75b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    so that the smallest denormal has just the least significant bit
76b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    of the significand set.  The sign of zeroes and infinities is
77117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    significant; the exponent and significand of such numbers is not
78117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    stored, but has a known implicit (deterministic) value: 0 for the
79117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    significands, 0 for zero exponent, all 1 bits for infinity
80117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    exponent.  For NaNs the sign and significand are deterministic,
81117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    although not really meaningful, and preserved in non-conversion
82117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    operations.  The exponent is implicitly all 1 bits.
83b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
84b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    TODO
85b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    ====
86b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
87b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    Some features that may or may not be worth adding:
88b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
8996c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    Binary to decimal conversion (hard).
90b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
91b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    Optional ability to detect underflow tininess before rounding.
92b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
93b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    New formats: x87 in single and double precision mode (IEEE apart
9496c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    from extended exponent range) (hard).
95b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
96a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    New operations: sqrt, IEEE remainder, C90 fmod, nextafter,
97a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    nexttoward.
98b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner*/
99b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
100b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner#ifndef LLVM_FLOAT_H
101b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner#define LLVM_FLOAT_H
102b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
103b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// APInt contains static functions implementing bignum arithmetic.
104b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner#include "llvm/ADT/APInt.h"
105b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
106b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattnernamespace llvm {
107b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
108b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  /* Exponents are stored as signed numbers.  */
109b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  typedef signed short exponent_t;
110b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
111b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  struct fltSemantics;
112b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
113b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  /* When bits of a floating point number are truncated, this enum is
114b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner     used to indicate what fraction of the LSB those bits represented.
115b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner     It essentially combines the roles of guard and sticky bits.  */
116e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling  enum lostFraction {           // Example of truncated bits:
117e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfExactlyZero,              // 000000
118e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfLessThanHalf,             // 0xxxxx  x's not all zero
119e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfExactlyHalf,              // 100000
120e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfMoreThanHalf              // 1xxxxx  x's not all zero
121b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  };
122b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
123b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  class APFloat {
124b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  public:
125b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
126b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* We support the following floating point semantics.  */
127b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics IEEEsingle;
128b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics IEEEdouble;
129b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics IEEEquad;
130a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    static const fltSemantics PPCDoubleDouble;
131b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics x87DoubleExtended;
132343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    /* And this psuedo, used to construct APFloats that cannot
133343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen       conflict with anything real. */
134343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    static const fltSemantics Bogus;
135b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
136b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static unsigned int semanticsPrecision(const fltSemantics &);
137b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
138b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Floating point numbers have a four-state comparison relation.  */
139b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum cmpResult {
140b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpLessThan,
141b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpEqual,
142b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpGreaterThan,
143b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpUnordered
144b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
145b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
146b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* IEEE-754R gives five rounding modes.  */
147b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum roundingMode {
148b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmNearestTiesToEven,
149b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmTowardPositive,
150b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmTowardNegative,
151b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmTowardZero,
152b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmNearestTiesToAway
153b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
154b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
155b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Operation status.  opUnderflow or opOverflow are always returned
156b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner       or-ed with opInexact.  */
157b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum opStatus {
158b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opOK          = 0x00,
159b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opInvalidOp   = 0x01,
160b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opDivByZero   = 0x02,
161b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opOverflow    = 0x04,
162b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opUnderflow   = 0x08,
163b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opInexact     = 0x10
164b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
165b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
166b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Category of internally-represented number.  */
167b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum fltCategory {
168b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      fcInfinity,
169eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen      fcNaN,
170b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      fcNormal,
171b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      fcZero
172b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
173b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
174b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Constructors.  */
175b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat(const fltSemantics &, const char *);
176b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat(const fltSemantics &, integerPart);
177b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat(const fltSemantics &, fltCategory, bool negative);
1788a901985bc599df9dfd2fca0b8dd0da4585df8a7Chris Lattner    explicit APFloat(double d);
1798a901985bc599df9dfd2fca0b8dd0da4585df8a7Chris Lattner    explicit APFloat(float f);
180a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    explicit APFloat(const APInt &, bool isIEEE = false);
181b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat(const APFloat &);
182b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    ~APFloat();
183c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek
1841f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek    /// Profile - Used to insert APFloat objects, or objects that contain
1851f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek    ///  APFloat objects, into FoldingSets.
1861f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek    void Profile(FoldingSetNodeID& NID) const;
1871f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek
188c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    /// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
189c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    void Emit(Serializer& S) const;
190c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek
191c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    /// @brief Used by the Bitcode deserializer to deserialize APInts.
192c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    static APFloat ReadVal(Deserializer& D);
193b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
194b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Arithmetic.  */
195b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus add(const APFloat &, roundingMode);
196b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus subtract(const APFloat &, roundingMode);
197b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus multiply(const APFloat &, roundingMode);
198b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus divide(const APFloat &, roundingMode);
199c4dd3c3b519aa2c2ed26ce03a4b1fbb992efeacaDale Johannesen    opStatus mod(const APFloat &, roundingMode);
200b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
20196c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth
20296c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    /* Sign operations.  */
20396c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    void changeSign();
20496c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    void clearSign();
20596c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    void copySign(const APFloat &);
206b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
207b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Conversions.  */
208b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus convert(const fltSemantics &, roundingMode);
209b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus convertToInteger(integerPart *, unsigned int, bool,
210e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling                              roundingMode) const;
211f16c595252de363e0e1f6895a5a626bc30017053Neil Booth    opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
212f16c595252de363e0e1f6895a5a626bc30017053Neil Booth                                            bool, roundingMode);
213ccf596a53e16ea221a9bf8b3874a7d6afa71f1f4Neil Booth    opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
214ccf596a53e16ea221a9bf8b3874a7d6afa71f1f4Neil Booth                                            bool, roundingMode);
215b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus convertFromString(const char *, roundingMode);
2163f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertToAPInt() const;
217343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    double convertToDouble() const;
218343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    float convertToFloat() const;
219b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
22012595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen    /* The definition of equality is not straightforward for floating point,
22112595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen       so we won't use operator==.  Use one of the following, or write
22212595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen       whatever it is you really mean. */
22321dcae17f580678c452eac73e01424c924d2f994Dale Johannesen    // bool operator==(const APFloat &) const;     // DO NOT IMPLEMENT
224117acf9e36a0789d56b52525e031f575f80fe169Neil Booth
225eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen    /* IEEE comparison with another floating point number (NaNs
226343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen       compare unordered, 0==-0). */
227b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    cmpResult compare(const APFloat &) const;
228b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
229a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    /* Write out a hexadecimal representation of the floating point
230a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth       value to DST, which must be of sufficient size, in the C99 form
231a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth       [-]0xh.hhhhp[+-]d.  Return the number of characters written,
232a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth       excluding the terminating NUL.  */
233a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    unsigned int convertToHexString(char *dst, unsigned int hexDigits,
234a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth                                    bool upperCase, roundingMode) const;
235a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth
236343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    /* Bitwise comparison for equality (QNaNs compare equal, 0!=-0). */
23712595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen    bool bitwiseIsEqual(const APFloat &) const;
238d3b51fd17024569cc53ae02b9a4f80857930e08bDale Johannesen
239b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Simple queries.  */
240b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    fltCategory getCategory() const { return category; }
241b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    const fltSemantics &getSemantics() const { return *semantics; }
242b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bool isZero() const { return category == fcZero; }
243b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bool isNonZero() const { return category != fcZero; }
24410c42185d82c121e8f7d261decde798c05567c59Chris Lattner    bool isNaN() const { return category == fcNaN; }
245b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bool isNegative() const { return sign; }
246eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen    bool isPosZero() const { return isZero() && !isNegative(); }
247eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen    bool isNegZero() const { return isZero() && isNegative(); }
248b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
249b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat& operator=(const APFloat &);
250b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
251343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    /* Return an arbitrary integer value usable for hashing. */
252343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    uint32_t getHashValue() const;
253343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen
254b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  private:
255b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
256b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Trivial queries.  */
257b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integerPart *significandParts();
258b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    const integerPart *significandParts() const;
259b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int partCount() const;
260b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
261b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Significand operations.  */
262b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integerPart addSignificand(const APFloat &);
263b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integerPart subtractSignificand(const APFloat &, integerPart);
264b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
265b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction multiplySignificand(const APFloat &, const APFloat *);
266b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction divideSignificand(const APFloat &);
267b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void incrementSignificand();
268b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void initialize(const fltSemantics *);
269b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void shiftSignificandLeft(unsigned int);
270b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction shiftSignificandRight(unsigned int);
271b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int significandLSB() const;
272b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int significandMSB() const;
273b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void zeroSignificand();
274b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
275b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Arithmetic on special values.  */
276b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
277b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus divideSpecials(const APFloat &);
278b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus multiplySpecials(const APFloat &);
279b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
280b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Miscellany.  */
281e5e0194583c9d95cae685da139531624e050d39eNeil Booth    void makeNaN(void);
282b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus normalize(roundingMode, lostFraction);
283b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
284b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    cmpResult compareAbsoluteValue(const APFloat &) const;
285b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus handleOverflow(roundingMode);
286b7dea4cb368c7b2e825e6d58b249693736a32e21Neil Booth    bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
287ee7ae384f5d6067f1ca6d475b8630fe91ff2f6b6Neil Booth    opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
288ee7ae384f5d6067f1ca6d475b8630fe91ff2f6b6Neil Booth                                          roundingMode) const;
289643ce59495702ef29573b725d7431638be1c136aNeil Booth    opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
290643ce59495702ef29573b725d7431638be1c136aNeil Booth                                      roundingMode);
291b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus convertFromHexadecimalString(const char *, roundingMode);
29296c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    opStatus convertFromDecimalString (const char *, roundingMode);
293a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    char *convertNormalToHexString(char *, unsigned int, bool,
294a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth                                   roundingMode) const;
29596c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    opStatus roundSignificandWithExponent(const integerPart *, unsigned int,
29696c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth                                          int, roundingMode);
29796c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth
2983f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertFloatAPFloatToAPInt() const;
2993f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertDoubleAPFloatToAPInt() const;
3003f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertF80LongDoubleAPFloatToAPInt() const;
301a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
302a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    void initFromAPInt(const APInt& api, bool isIEEE = false);
3033f6eb7419de437436265831fce92f62498556e08Dale Johannesen    void initFromFloatAPInt(const APInt& api);
3043f6eb7419de437436265831fce92f62498556e08Dale Johannesen    void initFromDoubleAPInt(const APInt& api);
3053f6eb7419de437436265831fce92f62498556e08Dale Johannesen    void initFromF80LongDoubleAPInt(const APInt& api);
306a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    void initFromPPCDoubleDoubleAPInt(const APInt& api);
307b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
308b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void assign(const APFloat &);
309b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void copySignificand(const APFloat &);
310b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void freeSignificand();
311b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
312b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* What kind of semantics does this value obey?  */
313b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    const fltSemantics *semantics;
314b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
315b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Significand - the fraction with an explicit integer bit.  Must be
316b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner       at least one bit wider than the target precision.  */
317b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    union Significand
318b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    {
319b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      integerPart part;
320b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      integerPart *parts;
321b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    } significand;
322b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
323b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* The exponent - a signed number.  */
324b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    exponent_t exponent;
325b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
326b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* What kind of floating point number this is.  */
327117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    /* Only 2 bits are required, but VisualStudio incorrectly sign extends
328418d360518811121ad9352af57fdd7ba58a4f917Chuck Rose III       it.  Using the extra bit keeps it from failing under VisualStudio */
329418d360518811121ad9352af57fdd7ba58a4f917Chuck Rose III    fltCategory category: 3;
330b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
331b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* The sign bit of this number.  */
332b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int sign: 1;
333a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen
334a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    /* For PPCDoubleDouble, we have a second exponent and sign (the second
335a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen       significand is appended to the first one, although it would be wrong to
336a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen       regard these as a single number for arithmetic purposes).  These fields
337a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen       are not meaningful for any other type. */
338a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    exponent_t exponent2 : 11;
339a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    unsigned int sign2: 1;
340b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  };
341b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner} /* namespace llvm */
342b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
343b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner#endif /* LLVM_FLOAT_H */
344