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
100674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_ADT_APFLOAT_H
101674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_ADT_APFLOAT_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;
1123d42bfbbdd26ac56ccd706d4ebee984490c72eccJeffrey Yasskin  class APSInt;
113f70bc559a12c410aefab470f1349922ae7b19307Oscar Fuentes  class StringRef;
114b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
115b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  /* When bits of a floating point number are truncated, this enum is
116b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner     used to indicate what fraction of the LSB those bits represented.
117b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner     It essentially combines the roles of guard and sticky bits.  */
118e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling  enum lostFraction {           // Example of truncated bits:
119e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfExactlyZero,              // 000000
120e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfLessThanHalf,             // 0xxxxx  x's not all zero
121e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfExactlyHalf,              // 100000
122e85fe660e4e99d30ca9292b706b8ffe6d0367dcaBill Wendling    lfMoreThanHalf              // 1xxxxx  x's not all zero
123b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  };
124b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
125b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  class APFloat {
126b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  public:
127b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
128b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* We support the following floating point semantics.  */
129cc4287a374a33fb03ef41b92f74783e31ef47650Chris Lattner    static const fltSemantics IEEEhalf;
130b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics IEEEsingle;
131b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics IEEEdouble;
132b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics IEEEquad;
133a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    static const fltSemantics PPCDoubleDouble;
134b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static const fltSemantics x87DoubleExtended;
1357111b02c734c992b8c97d9918118768026dad79eDale Johannesen    /* And this pseudo, used to construct APFloats that cannot
136343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen       conflict with anything real. */
137343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    static const fltSemantics Bogus;
138b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
139b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    static unsigned int semanticsPrecision(const fltSemantics &);
140b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
141b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Floating point numbers have a four-state comparison relation.  */
142b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum cmpResult {
143b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpLessThan,
144b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpEqual,
145b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpGreaterThan,
146b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      cmpUnordered
147b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
148b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
149b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* IEEE-754R gives five rounding modes.  */
150b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum roundingMode {
151b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmNearestTiesToEven,
152b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmTowardPositive,
153b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmTowardNegative,
154b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmTowardZero,
155b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      rmNearestTiesToAway
156b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
157b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
158f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    // Operation status.  opUnderflow or opOverflow are always returned
159f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    // or-ed with opInexact.
160b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum opStatus {
161b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opOK          = 0x00,
162b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opInvalidOp   = 0x01,
163b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opDivByZero   = 0x02,
164b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opOverflow    = 0x04,
165b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opUnderflow   = 0x08,
166b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      opInexact     = 0x10
167b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
168b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
169f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    // Category of internally-represented number.
170b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    enum fltCategory {
171b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      fcInfinity,
172eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen      fcNaN,
173b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      fcNormal,
174b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      fcZero
175b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    };
176b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
177e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    enum uninitializedTag {
178e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall      uninitialized
179e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    };
180e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
181f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    // Constructors.
182d7bd78e36e1d3adfc90a4f95b2cc849d38af1b24Chris Lattner    APFloat(const fltSemantics &); // Default construct to 0.0
18338e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer    APFloat(const fltSemantics &, StringRef);
184b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat(const fltSemantics &, integerPart);
185e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    APFloat(const fltSemantics &, fltCategory, bool negative);
186e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    APFloat(const fltSemantics &, uninitializedTag);
1870a29cb045444c13160e90fe7942a9d7c720185edTim Northover    APFloat(const fltSemantics &, const APInt &);
1888a901985bc599df9dfd2fca0b8dd0da4585df8a7Chris Lattner    explicit APFloat(double d);
1898a901985bc599df9dfd2fca0b8dd0da4585df8a7Chris Lattner    explicit APFloat(float f);
190b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat(const APFloat &);
191b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    ~APFloat();
192f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner
193f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    // Convenience "constructors"
194f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
195f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner      return APFloat(Sem, fcZero, Negative);
196f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    }
197f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
198f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner      return APFloat(Sem, fcInfinity, Negative);
199f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    }
20000e65de9d83f846af732e3ace3f48d43d67b13d1John McCall
2017d36d39e63451a09283225e6507dea730c1efa14Mike Stump    /// getNaN - Factory for QNaN values.
2027d36d39e63451a09283225e6507dea730c1efa14Mike Stump    ///
20394c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru    /// \param Negative - True iff the NaN generated should be negative.
2047d36d39e63451a09283225e6507dea730c1efa14Mike Stump    /// \param type - The unspecified fill bits for creating the NaN, 0 by
2056330084a6623afcacd86156ef505aa11d482e307Mike Stump    /// default.  The value is truncated as necessary.
206c5ca713b8073d9fe95b258d0c01328d020df3357Mike Stump    static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
2077d36d39e63451a09283225e6507dea730c1efa14Mike Stump                          unsigned type = 0) {
208e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall      if (type) {
209e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall        APInt fill(64, type);
210e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall        return getQNaN(Sem, Negative, &fill);
211e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall      } else {
212e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall        return getQNaN(Sem, Negative, 0);
213e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall      }
214e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    }
215e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
216e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    /// getQNan - Factory for QNaN values.
217e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    static APFloat getQNaN(const fltSemantics &Sem,
218e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall                           bool Negative = false,
219e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall                           const APInt *payload = 0) {
220e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall      return makeNaN(Sem, false, Negative, payload);
221e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    }
222e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall
223e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    /// getSNan - Factory for SNaN values.
224e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    static APFloat getSNaN(const fltSemantics &Sem,
225e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall                           bool Negative = false,
226e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall                           const APInt *payload = 0) {
227e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall      return makeNaN(Sem, true, Negative, payload);
228f25381ed35c345c252da8b8a8ae9af41eef6b35cChris Lattner    }
2293a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
23000e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// getLargest - Returns the largest finite number in the given
23100e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// semantics.
23200e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///
23394c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru    /// \param Negative - True iff the number should be negative
23400e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
23500e65de9d83f846af732e3ace3f48d43d67b13d1John McCall
23600e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// getSmallest - Returns the smallest (by magnitude) finite number
23700e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// in the given semantics.  Might be denormalized, which implies a
23800e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// relative loss of precision.
23900e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///
24094c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru    /// \param Negative - True iff the number should be negative
24100e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
24200e65de9d83f846af732e3ace3f48d43d67b13d1John McCall
24300e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// getSmallestNormalized - Returns the smallest (by magnitude)
24400e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// normalized finite number in the given semantics.
24500e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///
24694c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru    /// \param Negative - True iff the number should be negative
24700e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    static APFloat getSmallestNormalized(const fltSemantics &Sem,
24800e65de9d83f846af732e3ace3f48d43d67b13d1John McCall                                         bool Negative = false);
24900e65de9d83f846af732e3ace3f48d43d67b13d1John McCall
250093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem    /// getAllOnesValue - Returns a float which is bitcasted from
251093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem    /// an all one value int.
252093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem    ///
253093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem    /// \param BitWidth - Select float type
254093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem    /// \param isIEEE   - If 128 bit number, select between PPC and IEEE
255093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem    static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
256093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem
2571f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek    /// Profile - Used to insert APFloat objects, or objects that contain
2581f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek    ///  APFloat objects, into FoldingSets.
2591f801fa5ada9cb40fb97ae755c282e91af54a1bcTed Kremenek    void Profile(FoldingSetNodeID& NID) const;
2603a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
261c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    /// @brief Used by the Bitcode serializer to emit APInts to Bitcode.
262c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    void Emit(Serializer& S) const;
2633a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
264c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    /// @brief Used by the Bitcode deserializer to deserialize APInts.
265c47dfdde2458f97d2ef8342b73b526579424c381Ted Kremenek    static APFloat ReadVal(Deserializer& D);
266b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
267b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Arithmetic.  */
268b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus add(const APFloat &, roundingMode);
269b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus subtract(const APFloat &, roundingMode);
270b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus multiply(const APFloat &, roundingMode);
271b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus divide(const APFloat &, roundingMode);
27224b66a83ca8c80417ad7523c35ece709a7e6599cDale Johannesen    /* IEEE remainder. */
27324b66a83ca8c80417ad7523c35ece709a7e6599cDale Johannesen    opStatus remainder(const APFloat &);
27424b66a83ca8c80417ad7523c35ece709a7e6599cDale Johannesen    /* C fmod, or llvm frem. */
275c4dd3c3b519aa2c2ed26ce03a4b1fbb992efeacaDale Johannesen    opStatus mod(const APFloat &, roundingMode);
276b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
2777c626d30974c632ab500171ff185a24bcf2603bfOwen Anderson    opStatus roundToIntegral(roundingMode);
27896c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth
27996c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    /* Sign operations.  */
28096c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    void changeSign();
28196c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    void clearSign();
28296c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    void copySign(const APFloat &);
283b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
284b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Conversions.  */
28523a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    opStatus convert(const fltSemantics &, roundingMode, bool *);
286b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus convertToInteger(integerPart *, unsigned int, bool,
28723a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen                              roundingMode, bool *) const;
2883d42bfbbdd26ac56ccd706d4ebee984490c72eccJeffrey Yasskin    opStatus convertToInteger(APSInt&, roundingMode, bool *) const;
28993c276e1c92da03ce9805fd3f3814b5e9b8cd57cDan Gohman    opStatus convertFromAPInt(const APInt &,
29093c276e1c92da03ce9805fd3f3814b5e9b8cd57cDan Gohman                              bool, roundingMode);
291f16c595252de363e0e1f6895a5a626bc30017053Neil Booth    opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
292f16c595252de363e0e1f6895a5a626bc30017053Neil Booth                                            bool, roundingMode);
293ccf596a53e16ea221a9bf8b3874a7d6afa71f1f4Neil Booth    opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
294ccf596a53e16ea221a9bf8b3874a7d6afa71f1f4Neil Booth                                            bool, roundingMode);
29538e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer    opStatus convertFromString(StringRef, roundingMode);
2967111b02c734c992b8c97d9918118768026dad79eDale Johannesen    APInt bitcastToAPInt() const;
297343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    double convertToDouble() const;
298343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen    float convertToFloat() const;
299b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
30012595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen    /* The definition of equality is not straightforward for floating point,
30112595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen       so we won't use operator==.  Use one of the following, or write
30212595d7b165bf460b18f4ddd395dd29e6e6e68bcDale Johannesen       whatever it is you really mean. */
303a39058aaed4540fc37681cad728b99546595b2e8David Blaikie    bool operator==(const APFloat &) const LLVM_DELETED_FUNCTION;
304117acf9e36a0789d56b52525e031f575f80fe169Neil Booth
305eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen    /* IEEE comparison with another floating point number (NaNs
306343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen       compare unordered, 0==-0). */
307b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    cmpResult compare(const APFloat &) const;
308b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
3097111b02c734c992b8c97d9918118768026dad79eDale Johannesen    /* Bitwise comparison for equality (QNaNs compare equal, 0!=-0). */
3107111b02c734c992b8c97d9918118768026dad79eDale Johannesen    bool bitwiseIsEqual(const APFloat &) const;
3117111b02c734c992b8c97d9918118768026dad79eDale Johannesen
312a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    /* Write out a hexadecimal representation of the floating point
313a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth       value to DST, which must be of sufficient size, in the C99 form
314a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth       [-]0xh.hhhhp[+-]d.  Return the number of characters written,
315a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth       excluding the terminating NUL.  */
316a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    unsigned int convertToHexString(char *dst, unsigned int hexDigits,
317a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth                                    bool upperCase, roundingMode) const;
318a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth
319b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Simple queries.  */
320b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    fltCategory getCategory() const { return category; }
321b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    const fltSemantics &getSemantics() const { return *semantics; }
322b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bool isZero() const { return category == fcZero; }
323b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bool isNonZero() const { return category != fcZero; }
3241fd63df6930a83d9d1378d2c68e19850c652078eDuncan Sands    bool isNormal() const { return category == fcNormal; }
32510c42185d82c121e8f7d261decde798c05567c59Chris Lattner    bool isNaN() const { return category == fcNaN; }
326b8d0b807fc85e5ea5fb3a156fea5f8c2214e1f31Chris Lattner    bool isInfinity() const { return category == fcInfinity; }
327b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    bool isNegative() const { return sign; }
328eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen    bool isPosZero() const { return isZero() && !isNegative(); }
329eaf089430e7681fcddc3465c3b33b9645273ab02Dale Johannesen    bool isNegZero() const { return isZero() && isNegative(); }
3307aa1c321f00d29fdc84e9a03080853aa25dd06fcShuxin Yang    bool isDenormal() const;
331b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
332b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    APFloat& operator=(const APFloat &);
333b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
334ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// \brief Overload to compute a hash code for an APFloat value.
335ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    ///
336ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// Note that the use of hash codes for floating point values is in general
337ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// frought with peril. Equality is hard to define for these values. For
338ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// example, should negative and positive zero hash to different codes? Are
339ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// they equal or not? This hash value implementation specifically
340ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// emphasizes producing different codes for different inputs in order to
341ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// be used in canonicalization and memoization. As such, equality is
342ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    /// bitwiseIsEqual, and 0 != -0.
343ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    friend hash_code hash_value(const APFloat &Arg);
344343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen
34500e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// Converts this value into a decimal string.
34600e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///
34700e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// \param FormatPrecision The maximum number of digits of
34800e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///   precision to output.  If there are fewer digits available,
34900e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///   zero padding will not be used unless the value is
35000e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///   integral and small enough to be expressed in
3516a09affdf62fccbd190ca6e9cac0cb065b5f2677John McCall    ///   FormatPrecision digits.  0 means to use the natural
3526a09affdf62fccbd190ca6e9cac0cb065b5f2677John McCall    ///   precision of the number.
35300e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// \param FormatMaxPadding The maximum number of zeros to
35400e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///   consider inserting before falling back to scientific
35500e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///   notation.  0 means to always use scientific notation.
35600e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    ///
35700e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// Number       Precision    MaxPadding      Result
35800e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// ------       ---------    ----------      ------
35900e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// 1.01E+4              5             2       10100
36000e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// 1.01E+4              4             2       1.01E+4
36100e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// 1.01E+4              5             1       1.01E+4
36200e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// 1.01E-2              5             2       0.0101
3636a09affdf62fccbd190ca6e9cac0cb065b5f2677John McCall    /// 1.01E-2              4             2       0.0101
36400e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    /// 1.01E-2              4             1       1.01E-2
36500e65de9d83f846af732e3ace3f48d43d67b13d1John McCall    void toString(SmallVectorImpl<char> &Str,
3666a09affdf62fccbd190ca6e9cac0cb065b5f2677John McCall                  unsigned FormatPrecision = 0,
3670ddda3b8c6acc85761636ad46981395fdbe8dabaChris Lattner                  unsigned FormatMaxPadding = 3) const;
36800e65de9d83f846af732e3ace3f48d43d67b13d1John McCall
3692746000f4fa54e6ad00cf96910ea5772803624edBenjamin Kramer    /// getExactInverse - If this value has an exact multiplicative inverse,
3702746000f4fa54e6ad00cf96910ea5772803624edBenjamin Kramer    /// store it in inv and return true.
3712746000f4fa54e6ad00cf96910ea5772803624edBenjamin Kramer    bool getExactInverse(APFloat *inv) const;
3722746000f4fa54e6ad00cf96910ea5772803624edBenjamin Kramer
373b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  private:
374b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
375b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Trivial queries.  */
376b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integerPart *significandParts();
377b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    const integerPart *significandParts() const;
378b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int partCount() const;
379b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
380b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Significand operations.  */
381b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integerPart addSignificand(const APFloat &);
382b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    integerPart subtractSignificand(const APFloat &, integerPart);
383b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
384b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction multiplySignificand(const APFloat &, const APFloat *);
385b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction divideSignificand(const APFloat &);
386b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void incrementSignificand();
387b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void initialize(const fltSemantics *);
388b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void shiftSignificandLeft(unsigned int);
389b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    lostFraction shiftSignificandRight(unsigned int);
390b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int significandLSB() const;
391b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int significandMSB() const;
392b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void zeroSignificand();
393b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
394b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Arithmetic on special values.  */
395b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
396b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus divideSpecials(const APFloat &);
397b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus multiplySpecials(const APFloat &);
398ed6af24e146a5d358115123f0d2be694c1fa3a84Dale Johannesen    opStatus modSpecials(const APFloat &);
399b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
400b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Miscellany.  */
401e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
402e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall                           const APInt *fill);
403e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall    void makeNaN(bool SNaN = false, bool Neg = false, const APInt *fill = 0);
404b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus normalize(roundingMode, lostFraction);
405b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
406b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    cmpResult compareAbsoluteValue(const APFloat &) const;
407b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    opStatus handleOverflow(roundingMode);
408b7dea4cb368c7b2e825e6d58b249693736a32e21Neil Booth    bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
409ee7ae384f5d6067f1ca6d475b8630fe91ff2f6b6Neil Booth    opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
41023a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen                                          roundingMode, bool *) const;
411643ce59495702ef29573b725d7431638be1c136aNeil Booth    opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
412643ce59495702ef29573b725d7431638be1c136aNeil Booth                                      roundingMode);
41338e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer    opStatus convertFromHexadecimalString(StringRef, roundingMode);
41438e59891ee4417a9be2f8146ce0ba3269e38ac21Benjamin Kramer    opStatus convertFromDecimalString(StringRef, roundingMode);
415a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth    char *convertNormalToHexString(char *, unsigned int, bool,
416a30b0ee959b53e83ed3697ee0b704a493829dc04Neil Booth                                   roundingMode) const;
41796c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth    opStatus roundSignificandWithExponent(const integerPart *, unsigned int,
41896c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth                                          int, roundingMode);
41996c7471b39dc77d4f29658212e5a72e575b23c39Neil Booth
420cc4287a374a33fb03ef41b92f74783e31ef47650Chris Lattner    APInt convertHalfAPFloatToAPInt() const;
4213f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertFloatAPFloatToAPInt() const;
4223f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertDoubleAPFloatToAPInt() const;
4237e844f128e0be77154aa08dbf2e828fcd4051604Anton Korobeynikov    APInt convertQuadrupleAPFloatToAPInt() const;
4243f6eb7419de437436265831fce92f62498556e08Dale Johannesen    APInt convertF80LongDoubleAPFloatToAPInt() const;
425a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
4260a29cb045444c13160e90fe7942a9d7c720185edTim Northover    void initFromAPInt(const fltSemantics *Sem, const APInt& api);
427cc4287a374a33fb03ef41b92f74783e31ef47650Chris Lattner    void initFromHalfAPInt(const APInt& api);
4283f6eb7419de437436265831fce92f62498556e08Dale Johannesen    void initFromFloatAPInt(const APInt& api);
4293f6eb7419de437436265831fce92f62498556e08Dale Johannesen    void initFromDoubleAPInt(const APInt& api);
4307e844f128e0be77154aa08dbf2e828fcd4051604Anton Korobeynikov    void initFromQuadrupleAPInt(const APInt &api);
4313f6eb7419de437436265831fce92f62498556e08Dale Johannesen    void initFromF80LongDoubleAPInt(const APInt& api);
432a471c2ecda37cd1bae0d94e832f002caa7b63216Dale Johannesen    void initFromPPCDoubleDoubleAPInt(const APInt& api);
433b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
434b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void assign(const APFloat &);
435b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void copySignificand(const APFloat &);
436b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    void freeSignificand();
437b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
438b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* What kind of semantics does this value obey?  */
439b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    const fltSemantics *semantics;
440b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
441b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* Significand - the fraction with an explicit integer bit.  Must be
442b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner       at least one bit wider than the target precision.  */
443b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    union Significand
444b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    {
445b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      integerPart part;
446b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner      integerPart *parts;
447b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    } significand;
448b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
449b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* The exponent - a signed number.  */
450b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    exponent_t exponent;
451b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
452b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* What kind of floating point number this is.  */
453117acf9e36a0789d56b52525e031f575f80fe169Neil Booth    /* Only 2 bits are required, but VisualStudio incorrectly sign extends
454418d360518811121ad9352af57fdd7ba58a4f917Chuck Rose III       it.  Using the extra bit keeps it from failing under VisualStudio */
455418d360518811121ad9352af57fdd7ba58a4f917Chuck Rose III    fltCategory category: 3;
456b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
457b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    /* The sign bit of this number.  */
458b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner    unsigned int sign: 1;
459b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner  };
4603a9b71434cda6f66d65a031effec1bbe58e1dda3Rafael Espindola
4613a9b71434cda6f66d65a031effec1bbe58e1dda3Rafael Espindola  // See friend declaration above. This additional declaration is required in
4623a9b71434cda6f66d65a031effec1bbe58e1dda3Rafael Espindola  // order to compile LLVM with IBM xlC compiler.
4633a9b71434cda6f66d65a031effec1bbe58e1dda3Rafael Espindola  hash_code hash_value(const APFloat &Arg);
464b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner} /* namespace llvm */
465b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner
466674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#endif /* LLVM_ADT_APFLOAT_H */
467