1 //== llvm/Support/APFloat.h - Arbitrary Precision Floating Point -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Neil Booth and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares a class to represent arbitrary precision floating
11 // point values and provide a variety of arithmetic operations on them.
13 //===----------------------------------------------------------------------===//
15 /* A self-contained host- and target-independent arbitrary-precision
16 floating-point software implementation using bignum integer
17 arithmetic, as provided by static functions in the APInt class.
18 The library will work with bignum integers whose parts are any
19 unsigned type at least 16 bits wide. 64 bits is recommended.
21 Written for clarity rather than speed, in particular with a view
22 to use in the front-end of a cross compiler so that target
23 arithmetic can be correctly performed on the host. Performance
24 should nonetheless be reasonable, particularly for its intended
25 use. It may be useful as a base implementation for a run-time
26 library during development of a faster target-specific one.
28 All 5 rounding modes in the IEEE-754R draft are handled correctly
29 for all implemented operations. Currently implemented operations
30 are add, subtract, multiply, divide, fused-multiply-add,
31 conversion-to-float, conversion-to-integer and
32 conversion-from-integer. New rounding modes (e.g. away from zero)
33 can be added with three or four lines of code. The library reads
34 and correctly rounds hexadecimal floating point numbers as per
35 C99; syntax is required to have been validated by the caller.
36 Conversion from decimal is not currently implemented.
38 Four formats are built-in: IEEE single precision, double
39 precision, quadruple precision, and x87 80-bit extended double
40 (when operating with full extended precision). Adding a new
41 format that obeys IEEE semantics only requires adding two lines of
42 code: a declaration and definition of the format.
44 All operations return the status of that operation as an exception
45 bit-mask, so multiple operations can be done consecutively with
46 their results or-ed together. The returned status can be useful
47 for compiler diagnostics; e.g., inexact, underflow and overflow
48 can be easily diagnosed on constant folding, and compiler
49 optimizers can determine what exceptions would be raised by
50 folding operations and optimize, or perhaps not optimize,
53 At present, underflow tininess is detected after rounding; it
54 should be straight forward to add support for the before-rounding
57 Non-zero finite numbers are represented internally as a sign bit,
58 a 16-bit signed exponent, and the significand as an array of
59 integer parts. After normalization of a number of precision P the
60 exponent is within the range of the format, and if the number is
61 not denormal the P-th bit of the significand is set as an explicit
62 integer bit. For denormals the most significant bit is shifted
63 right so that the exponent is maintained at the format's minimum,
64 so that the smallest denormal has just the least significant bit
65 of the significand set. The sign of zeroes and infinities is
66 significant; the exponent and significand of such numbers is
67 not stored, but has a known implicit (deterministic) value:
68 0 for the significands, 0 for zero exponent, all 1 bits for
69 infinity exponent. For NaNs the sign and significand are
70 deterministic, although not really meaningful; the exponent is
71 implicitly all 1 bits.
76 Some features that may or may not be worth adding:
78 Conversions to and from decimal strings (hard).
80 Conversions to hexadecimal string.
82 Read and write IEEE-format in-memory representations.
84 Optional ability to detect underflow tininess before rounding.
86 New formats: x87 in single and double precision mode (IEEE apart
87 from extended exponent range) and IBM two-double extended
90 New operations: sqrt, copysign, nextafter, nexttoward.
96 // APInt contains static functions implementing bignum arithmetic.
97 #include "llvm/ADT/APInt.h"
98 #include "llvm/CodeGen/ValueTypes.h"
102 /* Exponents are stored as signed numbers. */
103 typedef signed short exponent_t;
107 /* When bits of a floating point number are truncated, this enum is
108 used to indicate what fraction of the LSB those bits represented.
109 It essentially combines the roles of guard and sticky bits. */
110 enum lostFraction { // Example of truncated bits:
111 lfExactlyZero, // 000000
112 lfLessThanHalf, // 0xxxxx x's not all zero
113 lfExactlyHalf, // 100000
114 lfMoreThanHalf // 1xxxxx x's not all zero
120 /* We support the following floating point semantics. */
121 static const fltSemantics IEEEsingle;
122 static const fltSemantics IEEEdouble;
123 static const fltSemantics IEEEquad;
124 static const fltSemantics x87DoubleExtended;
125 /* And this psuedo, used to construct APFloats that cannot
126 conflict with anything real. */
127 static const fltSemantics Bogus;
129 static unsigned int semanticsPrecision(const fltSemantics &);
131 /* Floating point numbers have a four-state comparison relation. */
139 /* IEEE-754R gives five rounding modes. */
148 /* Operation status. opUnderflow or opOverflow are always returned
149 or-ed with opInexact. */
159 /* Category of internally-represented number. */
168 APFloat(const fltSemantics &, const char *);
169 APFloat(const fltSemantics &, integerPart);
170 APFloat(const fltSemantics &, fltCategory, bool negative);
171 explicit APFloat(double d);
172 explicit APFloat(float f);
173 explicit APFloat(const APInt &);
174 APFloat(const APFloat &);
178 opStatus add(const APFloat &, roundingMode);
179 opStatus subtract(const APFloat &, roundingMode);
180 opStatus multiply(const APFloat &, roundingMode);
181 opStatus divide(const APFloat &, roundingMode);
182 opStatus mod(const APFloat &, roundingMode);
183 void copySign(const APFloat &);
184 opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
185 void changeSign(); // neg
186 void clearSign(); // abs
189 opStatus convert(const fltSemantics &, roundingMode);
190 opStatus convertToInteger(integerPart *, unsigned int, bool,
192 opStatus convertFromInteger(const integerPart *, unsigned int, bool,
194 opStatus convertFromString(const char *, roundingMode);
195 APInt convertToAPInt() const;
196 double convertToDouble() const;
197 float convertToFloat() const;
199 /* The definition of equality is not straightforward for floating point,
200 so we won't use operator==. Use one of the following, or write
201 whatever it is you really mean. */
202 // bool operator==(const APFloat &) const; // DO NOT IMPLEMENT
204 /* IEEE comparison with another floating point number (NaNs
205 compare unordered, 0==-0). */
206 cmpResult compare(const APFloat &) const;
208 /* Bitwise comparison for equality (QNaNs compare equal, 0!=-0). */
209 bool bitwiseIsEqual(const APFloat &) const;
211 /* Simple queries. */
212 fltCategory getCategory() const { return category; }
213 const fltSemantics &getSemantics() const { return *semantics; }
214 bool isZero() const { return category == fcZero; }
215 bool isNonZero() const { return category != fcZero; }
216 bool isNegative() const { return sign; }
217 bool isPosZero() const { return isZero() && !isNegative(); }
218 bool isNegZero() const { return isZero() && isNegative(); }
220 APFloat& operator=(const APFloat &);
222 /* Return an arbitrary integer value usable for hashing. */
223 uint32_t getHashValue() const;
227 /* Trivial queries. */
228 integerPart *significandParts();
229 const integerPart *significandParts() const;
230 unsigned int partCount() const;
232 /* Significand operations. */
233 integerPart addSignificand(const APFloat &);
234 integerPart subtractSignificand(const APFloat &, integerPart);
235 lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
236 lostFraction multiplySignificand(const APFloat &, const APFloat *);
237 lostFraction divideSignificand(const APFloat &);
238 void incrementSignificand();
239 void initialize(const fltSemantics *);
240 void shiftSignificandLeft(unsigned int);
241 lostFraction shiftSignificandRight(unsigned int);
242 unsigned int significandLSB() const;
243 unsigned int significandMSB() const;
244 void zeroSignificand();
246 /* Arithmetic on special values. */
247 opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
248 opStatus divideSpecials(const APFloat &);
249 opStatus multiplySpecials(const APFloat &);
252 opStatus normalize(roundingMode, lostFraction);
253 opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
254 cmpResult compareAbsoluteValue(const APFloat &) const;
255 opStatus handleOverflow(roundingMode);
256 bool roundAwayFromZero(roundingMode, lostFraction);
257 opStatus convertFromUnsignedInteger(integerPart *, unsigned int,
259 lostFraction combineLostFractions(lostFraction, lostFraction);
260 opStatus convertFromHexadecimalString(const char *, roundingMode);
261 APInt convertFloatAPFloatToAPInt() const;
262 APInt convertDoubleAPFloatToAPInt() const;
263 APInt convertF80LongDoubleAPFloatToAPInt() const;
264 void initFromAPInt(const APInt& api);
265 void initFromFloatAPInt(const APInt& api);
266 void initFromDoubleAPInt(const APInt& api);
267 void initFromF80LongDoubleAPInt(const APInt& api);
269 void assign(const APFloat &);
270 void copySignificand(const APFloat &);
271 void freeSignificand();
273 /* What kind of semantics does this value obey? */
274 const fltSemantics *semantics;
276 /* Significand - the fraction with an explicit integer bit. Must be
277 at least one bit wider than the target precision. */
284 /* The exponent - a signed number. */
287 /* What kind of floating point number this is. */
288 /* Only 2 bits are required, but VisualStudio incorrectly sign extends
289 it. Using the extra bit keeps it from failing under VisualStudio */
290 fltCategory category: 3;
292 /* The sign bit of this number. */
293 unsigned int sign: 1;
295 } /* namespace llvm */
297 #endif /* LLVM_FLOAT_H */