1f4a2713aSLionel Sambuc //===-- APFloat.cpp - Implement APFloat class -----------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements a class to represent arbitrary precision floating
11f4a2713aSLionel Sambuc // point values and provide a variety of arithmetic operations on them.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "llvm/ADT/APFloat.h"
16f4a2713aSLionel Sambuc #include "llvm/ADT/APSInt.h"
17f4a2713aSLionel Sambuc #include "llvm/ADT/FoldingSet.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/Hashing.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/MathExtras.h"
23f4a2713aSLionel Sambuc #include <cstring>
24f4a2713aSLionel Sambuc #include <limits.h>
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc using namespace llvm;
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc /// A macro used to combine two fcCategory enums into one key which can be used
29f4a2713aSLionel Sambuc /// in a switch statement to classify how the interaction of two APFloat's
30f4a2713aSLionel Sambuc /// categories affects an operation.
31f4a2713aSLionel Sambuc ///
32f4a2713aSLionel Sambuc /// TODO: If clang source code is ever allowed to use constexpr in its own
33f4a2713aSLionel Sambuc /// codebase, change this into a static inline function.
34f4a2713aSLionel Sambuc #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc /* Assumed in hexadecimal significand parsing, and conversion to
37f4a2713aSLionel Sambuc hexadecimal strings. */
38*0a6a1f1dSLionel Sambuc static_assert(integerPartWidth % 4 == 0, "Part width must be divisible by 4!");
39f4a2713aSLionel Sambuc
40f4a2713aSLionel Sambuc namespace llvm {
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc /* Represents floating point arithmetic semantics. */
43f4a2713aSLionel Sambuc struct fltSemantics {
44f4a2713aSLionel Sambuc /* The largest E such that 2^E is representable; this matches the
45f4a2713aSLionel Sambuc definition of IEEE 754. */
46f4a2713aSLionel Sambuc APFloat::ExponentType maxExponent;
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc /* The smallest E such that 2^E is a normalized number; this
49f4a2713aSLionel Sambuc matches the definition of IEEE 754. */
50f4a2713aSLionel Sambuc APFloat::ExponentType minExponent;
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc /* Number of bits in the significand. This includes the integer
53f4a2713aSLionel Sambuc bit. */
54f4a2713aSLionel Sambuc unsigned int precision;
55f4a2713aSLionel Sambuc };
56f4a2713aSLionel Sambuc
57f4a2713aSLionel Sambuc const fltSemantics APFloat::IEEEhalf = { 15, -14, 11 };
58f4a2713aSLionel Sambuc const fltSemantics APFloat::IEEEsingle = { 127, -126, 24 };
59f4a2713aSLionel Sambuc const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53 };
60f4a2713aSLionel Sambuc const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113 };
61f4a2713aSLionel Sambuc const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64 };
62f4a2713aSLionel Sambuc const fltSemantics APFloat::Bogus = { 0, 0, 0 };
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc /* The PowerPC format consists of two doubles. It does not map cleanly
65f4a2713aSLionel Sambuc onto the usual format above. It is approximated using twice the
66f4a2713aSLionel Sambuc mantissa bits. Note that for exponents near the double minimum,
67f4a2713aSLionel Sambuc we no longer can represent the full 106 mantissa bits, so those
68f4a2713aSLionel Sambuc will be treated as denormal numbers.
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc FIXME: While this approximation is equivalent to what GCC uses for
71f4a2713aSLionel Sambuc compile-time arithmetic on PPC double-double numbers, it is not able
72f4a2713aSLionel Sambuc to represent all possible values held by a PPC double-double number,
73f4a2713aSLionel Sambuc for example: (long double) 1.0 + (long double) 0x1p-106
74f4a2713aSLionel Sambuc Should this be replaced by a full emulation of PPC double-double? */
75f4a2713aSLionel Sambuc const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53 };
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc /* A tight upper bound on number of parts required to hold the value
78f4a2713aSLionel Sambuc pow(5, power) is
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc power * 815 / (351 * integerPartWidth) + 1
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc However, whilst the result may require only this many parts,
83f4a2713aSLionel Sambuc because we are multiplying two values to get it, the
84f4a2713aSLionel Sambuc multiplication may require an extra part with the excess part
85f4a2713aSLionel Sambuc being zero (consider the trivial case of 1 * 1, tcFullMultiply
86f4a2713aSLionel Sambuc requires two parts to hold the single-part result). So we add an
87f4a2713aSLionel Sambuc extra one to guarantee enough space whilst multiplying. */
88f4a2713aSLionel Sambuc const unsigned int maxExponent = 16383;
89f4a2713aSLionel Sambuc const unsigned int maxPrecision = 113;
90f4a2713aSLionel Sambuc const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
91f4a2713aSLionel Sambuc const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
92f4a2713aSLionel Sambuc / (351 * integerPartWidth));
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc /* A bunch of private, handy routines. */
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc static inline unsigned int
partCountForBits(unsigned int bits)98f4a2713aSLionel Sambuc partCountForBits(unsigned int bits)
99f4a2713aSLionel Sambuc {
100f4a2713aSLionel Sambuc return ((bits) + integerPartWidth - 1) / integerPartWidth;
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc
103f4a2713aSLionel Sambuc /* Returns 0U-9U. Return values >= 10U are not digits. */
104f4a2713aSLionel Sambuc static inline unsigned int
decDigitValue(unsigned int c)105f4a2713aSLionel Sambuc decDigitValue(unsigned int c)
106f4a2713aSLionel Sambuc {
107f4a2713aSLionel Sambuc return c - '0';
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc /* Return the value of a decimal exponent of the form
111f4a2713aSLionel Sambuc [+-]ddddddd.
112f4a2713aSLionel Sambuc
113f4a2713aSLionel Sambuc If the exponent overflows, returns a large exponent with the
114f4a2713aSLionel Sambuc appropriate sign. */
115f4a2713aSLionel Sambuc static int
readExponent(StringRef::iterator begin,StringRef::iterator end)116f4a2713aSLionel Sambuc readExponent(StringRef::iterator begin, StringRef::iterator end)
117f4a2713aSLionel Sambuc {
118f4a2713aSLionel Sambuc bool isNegative;
119f4a2713aSLionel Sambuc unsigned int absExponent;
120f4a2713aSLionel Sambuc const unsigned int overlargeExponent = 24000; /* FIXME. */
121f4a2713aSLionel Sambuc StringRef::iterator p = begin;
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc assert(p != end && "Exponent has no digits");
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc isNegative = (*p == '-');
126f4a2713aSLionel Sambuc if (*p == '-' || *p == '+') {
127f4a2713aSLionel Sambuc p++;
128f4a2713aSLionel Sambuc assert(p != end && "Exponent has no digits");
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc
131f4a2713aSLionel Sambuc absExponent = decDigitValue(*p++);
132f4a2713aSLionel Sambuc assert(absExponent < 10U && "Invalid character in exponent");
133f4a2713aSLionel Sambuc
134f4a2713aSLionel Sambuc for (; p != end; ++p) {
135f4a2713aSLionel Sambuc unsigned int value;
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc value = decDigitValue(*p);
138f4a2713aSLionel Sambuc assert(value < 10U && "Invalid character in exponent");
139f4a2713aSLionel Sambuc
140f4a2713aSLionel Sambuc value += absExponent * 10;
141f4a2713aSLionel Sambuc if (absExponent >= overlargeExponent) {
142f4a2713aSLionel Sambuc absExponent = overlargeExponent;
143f4a2713aSLionel Sambuc p = end; /* outwit assert below */
144f4a2713aSLionel Sambuc break;
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc absExponent = value;
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc assert(p == end && "Invalid exponent in exponent");
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc if (isNegative)
152f4a2713aSLionel Sambuc return -(int) absExponent;
153f4a2713aSLionel Sambuc else
154f4a2713aSLionel Sambuc return (int) absExponent;
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc
157f4a2713aSLionel Sambuc /* This is ugly and needs cleaning up, but I don't immediately see
158f4a2713aSLionel Sambuc how whilst remaining safe. */
159f4a2713aSLionel Sambuc static int
totalExponent(StringRef::iterator p,StringRef::iterator end,int exponentAdjustment)160f4a2713aSLionel Sambuc totalExponent(StringRef::iterator p, StringRef::iterator end,
161f4a2713aSLionel Sambuc int exponentAdjustment)
162f4a2713aSLionel Sambuc {
163f4a2713aSLionel Sambuc int unsignedExponent;
164f4a2713aSLionel Sambuc bool negative, overflow;
165f4a2713aSLionel Sambuc int exponent = 0;
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc assert(p != end && "Exponent has no digits");
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc negative = *p == '-';
170f4a2713aSLionel Sambuc if (*p == '-' || *p == '+') {
171f4a2713aSLionel Sambuc p++;
172f4a2713aSLionel Sambuc assert(p != end && "Exponent has no digits");
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc
175f4a2713aSLionel Sambuc unsignedExponent = 0;
176f4a2713aSLionel Sambuc overflow = false;
177f4a2713aSLionel Sambuc for (; p != end; ++p) {
178f4a2713aSLionel Sambuc unsigned int value;
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc value = decDigitValue(*p);
181f4a2713aSLionel Sambuc assert(value < 10U && "Invalid character in exponent");
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc unsignedExponent = unsignedExponent * 10 + value;
184f4a2713aSLionel Sambuc if (unsignedExponent > 32767) {
185f4a2713aSLionel Sambuc overflow = true;
186f4a2713aSLionel Sambuc break;
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc
190f4a2713aSLionel Sambuc if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
191f4a2713aSLionel Sambuc overflow = true;
192f4a2713aSLionel Sambuc
193f4a2713aSLionel Sambuc if (!overflow) {
194f4a2713aSLionel Sambuc exponent = unsignedExponent;
195f4a2713aSLionel Sambuc if (negative)
196f4a2713aSLionel Sambuc exponent = -exponent;
197f4a2713aSLionel Sambuc exponent += exponentAdjustment;
198f4a2713aSLionel Sambuc if (exponent > 32767 || exponent < -32768)
199f4a2713aSLionel Sambuc overflow = true;
200f4a2713aSLionel Sambuc }
201f4a2713aSLionel Sambuc
202f4a2713aSLionel Sambuc if (overflow)
203f4a2713aSLionel Sambuc exponent = negative ? -32768: 32767;
204f4a2713aSLionel Sambuc
205f4a2713aSLionel Sambuc return exponent;
206f4a2713aSLionel Sambuc }
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambuc static StringRef::iterator
skipLeadingZeroesAndAnyDot(StringRef::iterator begin,StringRef::iterator end,StringRef::iterator * dot)209f4a2713aSLionel Sambuc skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
210f4a2713aSLionel Sambuc StringRef::iterator *dot)
211f4a2713aSLionel Sambuc {
212f4a2713aSLionel Sambuc StringRef::iterator p = begin;
213f4a2713aSLionel Sambuc *dot = end;
214*0a6a1f1dSLionel Sambuc while (p != end && *p == '0')
215f4a2713aSLionel Sambuc p++;
216f4a2713aSLionel Sambuc
217*0a6a1f1dSLionel Sambuc if (p != end && *p == '.') {
218f4a2713aSLionel Sambuc *dot = p++;
219f4a2713aSLionel Sambuc
220f4a2713aSLionel Sambuc assert(end - begin != 1 && "Significand has no digits");
221f4a2713aSLionel Sambuc
222*0a6a1f1dSLionel Sambuc while (p != end && *p == '0')
223f4a2713aSLionel Sambuc p++;
224f4a2713aSLionel Sambuc }
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc return p;
227f4a2713aSLionel Sambuc }
228f4a2713aSLionel Sambuc
229f4a2713aSLionel Sambuc /* Given a normal decimal floating point number of the form
230f4a2713aSLionel Sambuc
231f4a2713aSLionel Sambuc dddd.dddd[eE][+-]ddd
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc where the decimal point and exponent are optional, fill out the
234f4a2713aSLionel Sambuc structure D. Exponent is appropriate if the significand is
235f4a2713aSLionel Sambuc treated as an integer, and normalizedExponent if the significand
236f4a2713aSLionel Sambuc is taken to have the decimal point after a single leading
237f4a2713aSLionel Sambuc non-zero digit.
238f4a2713aSLionel Sambuc
239f4a2713aSLionel Sambuc If the value is zero, V->firstSigDigit points to a non-digit, and
240f4a2713aSLionel Sambuc the return exponent is zero.
241f4a2713aSLionel Sambuc */
242f4a2713aSLionel Sambuc struct decimalInfo {
243f4a2713aSLionel Sambuc const char *firstSigDigit;
244f4a2713aSLionel Sambuc const char *lastSigDigit;
245f4a2713aSLionel Sambuc int exponent;
246f4a2713aSLionel Sambuc int normalizedExponent;
247f4a2713aSLionel Sambuc };
248f4a2713aSLionel Sambuc
249f4a2713aSLionel Sambuc static void
interpretDecimal(StringRef::iterator begin,StringRef::iterator end,decimalInfo * D)250f4a2713aSLionel Sambuc interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
251f4a2713aSLionel Sambuc decimalInfo *D)
252f4a2713aSLionel Sambuc {
253f4a2713aSLionel Sambuc StringRef::iterator dot = end;
254f4a2713aSLionel Sambuc StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
255f4a2713aSLionel Sambuc
256f4a2713aSLionel Sambuc D->firstSigDigit = p;
257f4a2713aSLionel Sambuc D->exponent = 0;
258f4a2713aSLionel Sambuc D->normalizedExponent = 0;
259f4a2713aSLionel Sambuc
260f4a2713aSLionel Sambuc for (; p != end; ++p) {
261f4a2713aSLionel Sambuc if (*p == '.') {
262f4a2713aSLionel Sambuc assert(dot == end && "String contains multiple dots");
263f4a2713aSLionel Sambuc dot = p++;
264f4a2713aSLionel Sambuc if (p == end)
265f4a2713aSLionel Sambuc break;
266f4a2713aSLionel Sambuc }
267f4a2713aSLionel Sambuc if (decDigitValue(*p) >= 10U)
268f4a2713aSLionel Sambuc break;
269f4a2713aSLionel Sambuc }
270f4a2713aSLionel Sambuc
271f4a2713aSLionel Sambuc if (p != end) {
272f4a2713aSLionel Sambuc assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
273f4a2713aSLionel Sambuc assert(p != begin && "Significand has no digits");
274f4a2713aSLionel Sambuc assert((dot == end || p - begin != 1) && "Significand has no digits");
275f4a2713aSLionel Sambuc
276f4a2713aSLionel Sambuc /* p points to the first non-digit in the string */
277f4a2713aSLionel Sambuc D->exponent = readExponent(p + 1, end);
278f4a2713aSLionel Sambuc
279f4a2713aSLionel Sambuc /* Implied decimal point? */
280f4a2713aSLionel Sambuc if (dot == end)
281f4a2713aSLionel Sambuc dot = p;
282f4a2713aSLionel Sambuc }
283f4a2713aSLionel Sambuc
284f4a2713aSLionel Sambuc /* If number is all zeroes accept any exponent. */
285f4a2713aSLionel Sambuc if (p != D->firstSigDigit) {
286f4a2713aSLionel Sambuc /* Drop insignificant trailing zeroes. */
287f4a2713aSLionel Sambuc if (p != begin) {
288f4a2713aSLionel Sambuc do
289f4a2713aSLionel Sambuc do
290f4a2713aSLionel Sambuc p--;
291f4a2713aSLionel Sambuc while (p != begin && *p == '0');
292f4a2713aSLionel Sambuc while (p != begin && *p == '.');
293f4a2713aSLionel Sambuc }
294f4a2713aSLionel Sambuc
295f4a2713aSLionel Sambuc /* Adjust the exponents for any decimal point. */
296f4a2713aSLionel Sambuc D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
297f4a2713aSLionel Sambuc D->normalizedExponent = (D->exponent +
298f4a2713aSLionel Sambuc static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
299f4a2713aSLionel Sambuc - (dot > D->firstSigDigit && dot < p)));
300f4a2713aSLionel Sambuc }
301f4a2713aSLionel Sambuc
302f4a2713aSLionel Sambuc D->lastSigDigit = p;
303f4a2713aSLionel Sambuc }
304f4a2713aSLionel Sambuc
305f4a2713aSLionel Sambuc /* Return the trailing fraction of a hexadecimal number.
306f4a2713aSLionel Sambuc DIGITVALUE is the first hex digit of the fraction, P points to
307f4a2713aSLionel Sambuc the next digit. */
308f4a2713aSLionel Sambuc static lostFraction
trailingHexadecimalFraction(StringRef::iterator p,StringRef::iterator end,unsigned int digitValue)309f4a2713aSLionel Sambuc trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
310f4a2713aSLionel Sambuc unsigned int digitValue)
311f4a2713aSLionel Sambuc {
312f4a2713aSLionel Sambuc unsigned int hexDigit;
313f4a2713aSLionel Sambuc
314f4a2713aSLionel Sambuc /* If the first trailing digit isn't 0 or 8 we can work out the
315f4a2713aSLionel Sambuc fraction immediately. */
316f4a2713aSLionel Sambuc if (digitValue > 8)
317f4a2713aSLionel Sambuc return lfMoreThanHalf;
318f4a2713aSLionel Sambuc else if (digitValue < 8 && digitValue > 0)
319f4a2713aSLionel Sambuc return lfLessThanHalf;
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc // Otherwise we need to find the first non-zero digit.
322f4a2713aSLionel Sambuc while (p != end && (*p == '0' || *p == '.'))
323f4a2713aSLionel Sambuc p++;
324f4a2713aSLionel Sambuc
325f4a2713aSLionel Sambuc assert(p != end && "Invalid trailing hexadecimal fraction!");
326f4a2713aSLionel Sambuc
327f4a2713aSLionel Sambuc hexDigit = hexDigitValue(*p);
328f4a2713aSLionel Sambuc
329f4a2713aSLionel Sambuc /* If we ran off the end it is exactly zero or one-half, otherwise
330f4a2713aSLionel Sambuc a little more. */
331f4a2713aSLionel Sambuc if (hexDigit == -1U)
332f4a2713aSLionel Sambuc return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
333f4a2713aSLionel Sambuc else
334f4a2713aSLionel Sambuc return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
335f4a2713aSLionel Sambuc }
336f4a2713aSLionel Sambuc
337f4a2713aSLionel Sambuc /* Return the fraction lost were a bignum truncated losing the least
338f4a2713aSLionel Sambuc significant BITS bits. */
339f4a2713aSLionel Sambuc static lostFraction
lostFractionThroughTruncation(const integerPart * parts,unsigned int partCount,unsigned int bits)340f4a2713aSLionel Sambuc lostFractionThroughTruncation(const integerPart *parts,
341f4a2713aSLionel Sambuc unsigned int partCount,
342f4a2713aSLionel Sambuc unsigned int bits)
343f4a2713aSLionel Sambuc {
344f4a2713aSLionel Sambuc unsigned int lsb;
345f4a2713aSLionel Sambuc
346f4a2713aSLionel Sambuc lsb = APInt::tcLSB(parts, partCount);
347f4a2713aSLionel Sambuc
348f4a2713aSLionel Sambuc /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
349f4a2713aSLionel Sambuc if (bits <= lsb)
350f4a2713aSLionel Sambuc return lfExactlyZero;
351f4a2713aSLionel Sambuc if (bits == lsb + 1)
352f4a2713aSLionel Sambuc return lfExactlyHalf;
353f4a2713aSLionel Sambuc if (bits <= partCount * integerPartWidth &&
354f4a2713aSLionel Sambuc APInt::tcExtractBit(parts, bits - 1))
355f4a2713aSLionel Sambuc return lfMoreThanHalf;
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc return lfLessThanHalf;
358f4a2713aSLionel Sambuc }
359f4a2713aSLionel Sambuc
360f4a2713aSLionel Sambuc /* Shift DST right BITS bits noting lost fraction. */
361f4a2713aSLionel Sambuc static lostFraction
shiftRight(integerPart * dst,unsigned int parts,unsigned int bits)362f4a2713aSLionel Sambuc shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
363f4a2713aSLionel Sambuc {
364f4a2713aSLionel Sambuc lostFraction lost_fraction;
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
367f4a2713aSLionel Sambuc
368f4a2713aSLionel Sambuc APInt::tcShiftRight(dst, parts, bits);
369f4a2713aSLionel Sambuc
370f4a2713aSLionel Sambuc return lost_fraction;
371f4a2713aSLionel Sambuc }
372f4a2713aSLionel Sambuc
373f4a2713aSLionel Sambuc /* Combine the effect of two lost fractions. */
374f4a2713aSLionel Sambuc static lostFraction
combineLostFractions(lostFraction moreSignificant,lostFraction lessSignificant)375f4a2713aSLionel Sambuc combineLostFractions(lostFraction moreSignificant,
376f4a2713aSLionel Sambuc lostFraction lessSignificant)
377f4a2713aSLionel Sambuc {
378f4a2713aSLionel Sambuc if (lessSignificant != lfExactlyZero) {
379f4a2713aSLionel Sambuc if (moreSignificant == lfExactlyZero)
380f4a2713aSLionel Sambuc moreSignificant = lfLessThanHalf;
381f4a2713aSLionel Sambuc else if (moreSignificant == lfExactlyHalf)
382f4a2713aSLionel Sambuc moreSignificant = lfMoreThanHalf;
383f4a2713aSLionel Sambuc }
384f4a2713aSLionel Sambuc
385f4a2713aSLionel Sambuc return moreSignificant;
386f4a2713aSLionel Sambuc }
387f4a2713aSLionel Sambuc
388f4a2713aSLionel Sambuc /* The error from the true value, in half-ulps, on multiplying two
389f4a2713aSLionel Sambuc floating point numbers, which differ from the value they
390f4a2713aSLionel Sambuc approximate by at most HUE1 and HUE2 half-ulps, is strictly less
391f4a2713aSLionel Sambuc than the returned value.
392f4a2713aSLionel Sambuc
393f4a2713aSLionel Sambuc See "How to Read Floating Point Numbers Accurately" by William D
394f4a2713aSLionel Sambuc Clinger. */
395f4a2713aSLionel Sambuc static unsigned int
HUerrBound(bool inexactMultiply,unsigned int HUerr1,unsigned int HUerr2)396f4a2713aSLionel Sambuc HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
397f4a2713aSLionel Sambuc {
398f4a2713aSLionel Sambuc assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
399f4a2713aSLionel Sambuc
400f4a2713aSLionel Sambuc if (HUerr1 + HUerr2 == 0)
401f4a2713aSLionel Sambuc return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
402f4a2713aSLionel Sambuc else
403f4a2713aSLionel Sambuc return inexactMultiply + 2 * (HUerr1 + HUerr2);
404f4a2713aSLionel Sambuc }
405f4a2713aSLionel Sambuc
406f4a2713aSLionel Sambuc /* The number of ulps from the boundary (zero, or half if ISNEAREST)
407f4a2713aSLionel Sambuc when the least significant BITS are truncated. BITS cannot be
408f4a2713aSLionel Sambuc zero. */
409f4a2713aSLionel Sambuc static integerPart
ulpsFromBoundary(const integerPart * parts,unsigned int bits,bool isNearest)410f4a2713aSLionel Sambuc ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
411f4a2713aSLionel Sambuc {
412f4a2713aSLionel Sambuc unsigned int count, partBits;
413f4a2713aSLionel Sambuc integerPart part, boundary;
414f4a2713aSLionel Sambuc
415f4a2713aSLionel Sambuc assert(bits != 0);
416f4a2713aSLionel Sambuc
417f4a2713aSLionel Sambuc bits--;
418f4a2713aSLionel Sambuc count = bits / integerPartWidth;
419f4a2713aSLionel Sambuc partBits = bits % integerPartWidth + 1;
420f4a2713aSLionel Sambuc
421f4a2713aSLionel Sambuc part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
422f4a2713aSLionel Sambuc
423f4a2713aSLionel Sambuc if (isNearest)
424f4a2713aSLionel Sambuc boundary = (integerPart) 1 << (partBits - 1);
425f4a2713aSLionel Sambuc else
426f4a2713aSLionel Sambuc boundary = 0;
427f4a2713aSLionel Sambuc
428f4a2713aSLionel Sambuc if (count == 0) {
429f4a2713aSLionel Sambuc if (part - boundary <= boundary - part)
430f4a2713aSLionel Sambuc return part - boundary;
431f4a2713aSLionel Sambuc else
432f4a2713aSLionel Sambuc return boundary - part;
433f4a2713aSLionel Sambuc }
434f4a2713aSLionel Sambuc
435f4a2713aSLionel Sambuc if (part == boundary) {
436f4a2713aSLionel Sambuc while (--count)
437f4a2713aSLionel Sambuc if (parts[count])
438f4a2713aSLionel Sambuc return ~(integerPart) 0; /* A lot. */
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc return parts[0];
441f4a2713aSLionel Sambuc } else if (part == boundary - 1) {
442f4a2713aSLionel Sambuc while (--count)
443f4a2713aSLionel Sambuc if (~parts[count])
444f4a2713aSLionel Sambuc return ~(integerPart) 0; /* A lot. */
445f4a2713aSLionel Sambuc
446f4a2713aSLionel Sambuc return -parts[0];
447f4a2713aSLionel Sambuc }
448f4a2713aSLionel Sambuc
449f4a2713aSLionel Sambuc return ~(integerPart) 0; /* A lot. */
450f4a2713aSLionel Sambuc }
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc /* Place pow(5, power) in DST, and return the number of parts used.
453f4a2713aSLionel Sambuc DST must be at least one part larger than size of the answer. */
454f4a2713aSLionel Sambuc static unsigned int
powerOf5(integerPart * dst,unsigned int power)455f4a2713aSLionel Sambuc powerOf5(integerPart *dst, unsigned int power)
456f4a2713aSLionel Sambuc {
457f4a2713aSLionel Sambuc static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
458f4a2713aSLionel Sambuc 15625, 78125 };
459f4a2713aSLionel Sambuc integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
460f4a2713aSLionel Sambuc pow5s[0] = 78125 * 5;
461f4a2713aSLionel Sambuc
462f4a2713aSLionel Sambuc unsigned int partsCount[16] = { 1 };
463f4a2713aSLionel Sambuc integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
464f4a2713aSLionel Sambuc unsigned int result;
465f4a2713aSLionel Sambuc assert(power <= maxExponent);
466f4a2713aSLionel Sambuc
467f4a2713aSLionel Sambuc p1 = dst;
468f4a2713aSLionel Sambuc p2 = scratch;
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc *p1 = firstEightPowers[power & 7];
471f4a2713aSLionel Sambuc power >>= 3;
472f4a2713aSLionel Sambuc
473f4a2713aSLionel Sambuc result = 1;
474f4a2713aSLionel Sambuc pow5 = pow5s;
475f4a2713aSLionel Sambuc
476f4a2713aSLionel Sambuc for (unsigned int n = 0; power; power >>= 1, n++) {
477f4a2713aSLionel Sambuc unsigned int pc;
478f4a2713aSLionel Sambuc
479f4a2713aSLionel Sambuc pc = partsCount[n];
480f4a2713aSLionel Sambuc
481f4a2713aSLionel Sambuc /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
482f4a2713aSLionel Sambuc if (pc == 0) {
483f4a2713aSLionel Sambuc pc = partsCount[n - 1];
484f4a2713aSLionel Sambuc APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
485f4a2713aSLionel Sambuc pc *= 2;
486f4a2713aSLionel Sambuc if (pow5[pc - 1] == 0)
487f4a2713aSLionel Sambuc pc--;
488f4a2713aSLionel Sambuc partsCount[n] = pc;
489f4a2713aSLionel Sambuc }
490f4a2713aSLionel Sambuc
491f4a2713aSLionel Sambuc if (power & 1) {
492f4a2713aSLionel Sambuc integerPart *tmp;
493f4a2713aSLionel Sambuc
494f4a2713aSLionel Sambuc APInt::tcFullMultiply(p2, p1, pow5, result, pc);
495f4a2713aSLionel Sambuc result += pc;
496f4a2713aSLionel Sambuc if (p2[result - 1] == 0)
497f4a2713aSLionel Sambuc result--;
498f4a2713aSLionel Sambuc
499f4a2713aSLionel Sambuc /* Now result is in p1 with partsCount parts and p2 is scratch
500f4a2713aSLionel Sambuc space. */
501f4a2713aSLionel Sambuc tmp = p1, p1 = p2, p2 = tmp;
502f4a2713aSLionel Sambuc }
503f4a2713aSLionel Sambuc
504f4a2713aSLionel Sambuc pow5 += pc;
505f4a2713aSLionel Sambuc }
506f4a2713aSLionel Sambuc
507f4a2713aSLionel Sambuc if (p1 != dst)
508f4a2713aSLionel Sambuc APInt::tcAssign(dst, p1, result);
509f4a2713aSLionel Sambuc
510f4a2713aSLionel Sambuc return result;
511f4a2713aSLionel Sambuc }
512f4a2713aSLionel Sambuc
513f4a2713aSLionel Sambuc /* Zero at the end to avoid modular arithmetic when adding one; used
514f4a2713aSLionel Sambuc when rounding up during hexadecimal output. */
515f4a2713aSLionel Sambuc static const char hexDigitsLower[] = "0123456789abcdef0";
516f4a2713aSLionel Sambuc static const char hexDigitsUpper[] = "0123456789ABCDEF0";
517f4a2713aSLionel Sambuc static const char infinityL[] = "infinity";
518f4a2713aSLionel Sambuc static const char infinityU[] = "INFINITY";
519f4a2713aSLionel Sambuc static const char NaNL[] = "nan";
520f4a2713aSLionel Sambuc static const char NaNU[] = "NAN";
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc /* Write out an integerPart in hexadecimal, starting with the most
523f4a2713aSLionel Sambuc significant nibble. Write out exactly COUNT hexdigits, return
524f4a2713aSLionel Sambuc COUNT. */
525f4a2713aSLionel Sambuc static unsigned int
partAsHex(char * dst,integerPart part,unsigned int count,const char * hexDigitChars)526f4a2713aSLionel Sambuc partAsHex (char *dst, integerPart part, unsigned int count,
527f4a2713aSLionel Sambuc const char *hexDigitChars)
528f4a2713aSLionel Sambuc {
529f4a2713aSLionel Sambuc unsigned int result = count;
530f4a2713aSLionel Sambuc
531f4a2713aSLionel Sambuc assert(count != 0 && count <= integerPartWidth / 4);
532f4a2713aSLionel Sambuc
533f4a2713aSLionel Sambuc part >>= (integerPartWidth - 4 * count);
534f4a2713aSLionel Sambuc while (count--) {
535f4a2713aSLionel Sambuc dst[count] = hexDigitChars[part & 0xf];
536f4a2713aSLionel Sambuc part >>= 4;
537f4a2713aSLionel Sambuc }
538f4a2713aSLionel Sambuc
539f4a2713aSLionel Sambuc return result;
540f4a2713aSLionel Sambuc }
541f4a2713aSLionel Sambuc
542f4a2713aSLionel Sambuc /* Write out an unsigned decimal integer. */
543f4a2713aSLionel Sambuc static char *
writeUnsignedDecimal(char * dst,unsigned int n)544f4a2713aSLionel Sambuc writeUnsignedDecimal (char *dst, unsigned int n)
545f4a2713aSLionel Sambuc {
546f4a2713aSLionel Sambuc char buff[40], *p;
547f4a2713aSLionel Sambuc
548f4a2713aSLionel Sambuc p = buff;
549f4a2713aSLionel Sambuc do
550f4a2713aSLionel Sambuc *p++ = '0' + n % 10;
551f4a2713aSLionel Sambuc while (n /= 10);
552f4a2713aSLionel Sambuc
553f4a2713aSLionel Sambuc do
554f4a2713aSLionel Sambuc *dst++ = *--p;
555f4a2713aSLionel Sambuc while (p != buff);
556f4a2713aSLionel Sambuc
557f4a2713aSLionel Sambuc return dst;
558f4a2713aSLionel Sambuc }
559f4a2713aSLionel Sambuc
560f4a2713aSLionel Sambuc /* Write out a signed decimal integer. */
561f4a2713aSLionel Sambuc static char *
writeSignedDecimal(char * dst,int value)562f4a2713aSLionel Sambuc writeSignedDecimal (char *dst, int value)
563f4a2713aSLionel Sambuc {
564f4a2713aSLionel Sambuc if (value < 0) {
565f4a2713aSLionel Sambuc *dst++ = '-';
566f4a2713aSLionel Sambuc dst = writeUnsignedDecimal(dst, -(unsigned) value);
567f4a2713aSLionel Sambuc } else
568f4a2713aSLionel Sambuc dst = writeUnsignedDecimal(dst, value);
569f4a2713aSLionel Sambuc
570f4a2713aSLionel Sambuc return dst;
571f4a2713aSLionel Sambuc }
572f4a2713aSLionel Sambuc
573f4a2713aSLionel Sambuc /* Constructors. */
574f4a2713aSLionel Sambuc void
initialize(const fltSemantics * ourSemantics)575f4a2713aSLionel Sambuc APFloat::initialize(const fltSemantics *ourSemantics)
576f4a2713aSLionel Sambuc {
577f4a2713aSLionel Sambuc unsigned int count;
578f4a2713aSLionel Sambuc
579f4a2713aSLionel Sambuc semantics = ourSemantics;
580f4a2713aSLionel Sambuc count = partCount();
581f4a2713aSLionel Sambuc if (count > 1)
582f4a2713aSLionel Sambuc significand.parts = new integerPart[count];
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc
585f4a2713aSLionel Sambuc void
freeSignificand()586f4a2713aSLionel Sambuc APFloat::freeSignificand()
587f4a2713aSLionel Sambuc {
588f4a2713aSLionel Sambuc if (needsCleanup())
589f4a2713aSLionel Sambuc delete [] significand.parts;
590f4a2713aSLionel Sambuc }
591f4a2713aSLionel Sambuc
592f4a2713aSLionel Sambuc void
assign(const APFloat & rhs)593f4a2713aSLionel Sambuc APFloat::assign(const APFloat &rhs)
594f4a2713aSLionel Sambuc {
595f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
596f4a2713aSLionel Sambuc
597f4a2713aSLionel Sambuc sign = rhs.sign;
598f4a2713aSLionel Sambuc category = rhs.category;
599f4a2713aSLionel Sambuc exponent = rhs.exponent;
600f4a2713aSLionel Sambuc if (isFiniteNonZero() || category == fcNaN)
601f4a2713aSLionel Sambuc copySignificand(rhs);
602f4a2713aSLionel Sambuc }
603f4a2713aSLionel Sambuc
604f4a2713aSLionel Sambuc void
copySignificand(const APFloat & rhs)605f4a2713aSLionel Sambuc APFloat::copySignificand(const APFloat &rhs)
606f4a2713aSLionel Sambuc {
607f4a2713aSLionel Sambuc assert(isFiniteNonZero() || category == fcNaN);
608f4a2713aSLionel Sambuc assert(rhs.partCount() >= partCount());
609f4a2713aSLionel Sambuc
610f4a2713aSLionel Sambuc APInt::tcAssign(significandParts(), rhs.significandParts(),
611f4a2713aSLionel Sambuc partCount());
612f4a2713aSLionel Sambuc }
613f4a2713aSLionel Sambuc
614f4a2713aSLionel Sambuc /* Make this number a NaN, with an arbitrary but deterministic value
615f4a2713aSLionel Sambuc for the significand. If double or longer, this is a signalling NaN,
616f4a2713aSLionel Sambuc which may not be ideal. If float, this is QNaN(0). */
makeNaN(bool SNaN,bool Negative,const APInt * fill)617f4a2713aSLionel Sambuc void APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill)
618f4a2713aSLionel Sambuc {
619f4a2713aSLionel Sambuc category = fcNaN;
620f4a2713aSLionel Sambuc sign = Negative;
621f4a2713aSLionel Sambuc
622f4a2713aSLionel Sambuc integerPart *significand = significandParts();
623f4a2713aSLionel Sambuc unsigned numParts = partCount();
624f4a2713aSLionel Sambuc
625f4a2713aSLionel Sambuc // Set the significand bits to the fill.
626f4a2713aSLionel Sambuc if (!fill || fill->getNumWords() < numParts)
627f4a2713aSLionel Sambuc APInt::tcSet(significand, 0, numParts);
628f4a2713aSLionel Sambuc if (fill) {
629f4a2713aSLionel Sambuc APInt::tcAssign(significand, fill->getRawData(),
630f4a2713aSLionel Sambuc std::min(fill->getNumWords(), numParts));
631f4a2713aSLionel Sambuc
632f4a2713aSLionel Sambuc // Zero out the excess bits of the significand.
633f4a2713aSLionel Sambuc unsigned bitsToPreserve = semantics->precision - 1;
634f4a2713aSLionel Sambuc unsigned part = bitsToPreserve / 64;
635f4a2713aSLionel Sambuc bitsToPreserve %= 64;
636f4a2713aSLionel Sambuc significand[part] &= ((1ULL << bitsToPreserve) - 1);
637f4a2713aSLionel Sambuc for (part++; part != numParts; ++part)
638f4a2713aSLionel Sambuc significand[part] = 0;
639f4a2713aSLionel Sambuc }
640f4a2713aSLionel Sambuc
641f4a2713aSLionel Sambuc unsigned QNaNBit = semantics->precision - 2;
642f4a2713aSLionel Sambuc
643f4a2713aSLionel Sambuc if (SNaN) {
644f4a2713aSLionel Sambuc // We always have to clear the QNaN bit to make it an SNaN.
645f4a2713aSLionel Sambuc APInt::tcClearBit(significand, QNaNBit);
646f4a2713aSLionel Sambuc
647f4a2713aSLionel Sambuc // If there are no bits set in the payload, we have to set
648f4a2713aSLionel Sambuc // *something* to make it a NaN instead of an infinity;
649f4a2713aSLionel Sambuc // conventionally, this is the next bit down from the QNaN bit.
650f4a2713aSLionel Sambuc if (APInt::tcIsZero(significand, numParts))
651f4a2713aSLionel Sambuc APInt::tcSetBit(significand, QNaNBit - 1);
652f4a2713aSLionel Sambuc } else {
653f4a2713aSLionel Sambuc // We always have to set the QNaN bit to make it a QNaN.
654f4a2713aSLionel Sambuc APInt::tcSetBit(significand, QNaNBit);
655f4a2713aSLionel Sambuc }
656f4a2713aSLionel Sambuc
657f4a2713aSLionel Sambuc // For x87 extended precision, we want to make a NaN, not a
658f4a2713aSLionel Sambuc // pseudo-NaN. Maybe we should expose the ability to make
659f4a2713aSLionel Sambuc // pseudo-NaNs?
660f4a2713aSLionel Sambuc if (semantics == &APFloat::x87DoubleExtended)
661f4a2713aSLionel Sambuc APInt::tcSetBit(significand, QNaNBit + 1);
662f4a2713aSLionel Sambuc }
663f4a2713aSLionel Sambuc
makeNaN(const fltSemantics & Sem,bool SNaN,bool Negative,const APInt * fill)664f4a2713aSLionel Sambuc APFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
665f4a2713aSLionel Sambuc const APInt *fill) {
666f4a2713aSLionel Sambuc APFloat value(Sem, uninitialized);
667f4a2713aSLionel Sambuc value.makeNaN(SNaN, Negative, fill);
668f4a2713aSLionel Sambuc return value;
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc
671f4a2713aSLionel Sambuc APFloat &
operator =(const APFloat & rhs)672f4a2713aSLionel Sambuc APFloat::operator=(const APFloat &rhs)
673f4a2713aSLionel Sambuc {
674f4a2713aSLionel Sambuc if (this != &rhs) {
675f4a2713aSLionel Sambuc if (semantics != rhs.semantics) {
676f4a2713aSLionel Sambuc freeSignificand();
677f4a2713aSLionel Sambuc initialize(rhs.semantics);
678f4a2713aSLionel Sambuc }
679f4a2713aSLionel Sambuc assign(rhs);
680f4a2713aSLionel Sambuc }
681f4a2713aSLionel Sambuc
682f4a2713aSLionel Sambuc return *this;
683f4a2713aSLionel Sambuc }
684f4a2713aSLionel Sambuc
685*0a6a1f1dSLionel Sambuc APFloat &
operator =(APFloat && rhs)686*0a6a1f1dSLionel Sambuc APFloat::operator=(APFloat &&rhs) {
687*0a6a1f1dSLionel Sambuc freeSignificand();
688*0a6a1f1dSLionel Sambuc
689*0a6a1f1dSLionel Sambuc semantics = rhs.semantics;
690*0a6a1f1dSLionel Sambuc significand = rhs.significand;
691*0a6a1f1dSLionel Sambuc exponent = rhs.exponent;
692*0a6a1f1dSLionel Sambuc category = rhs.category;
693*0a6a1f1dSLionel Sambuc sign = rhs.sign;
694*0a6a1f1dSLionel Sambuc
695*0a6a1f1dSLionel Sambuc rhs.semantics = &Bogus;
696*0a6a1f1dSLionel Sambuc return *this;
697*0a6a1f1dSLionel Sambuc }
698*0a6a1f1dSLionel Sambuc
699f4a2713aSLionel Sambuc bool
isDenormal() const700f4a2713aSLionel Sambuc APFloat::isDenormal() const {
701f4a2713aSLionel Sambuc return isFiniteNonZero() && (exponent == semantics->minExponent) &&
702f4a2713aSLionel Sambuc (APInt::tcExtractBit(significandParts(),
703f4a2713aSLionel Sambuc semantics->precision - 1) == 0);
704f4a2713aSLionel Sambuc }
705f4a2713aSLionel Sambuc
706f4a2713aSLionel Sambuc bool
isSmallest() const707f4a2713aSLionel Sambuc APFloat::isSmallest() const {
708f4a2713aSLionel Sambuc // The smallest number by magnitude in our format will be the smallest
709f4a2713aSLionel Sambuc // denormal, i.e. the floating point number with exponent being minimum
710f4a2713aSLionel Sambuc // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
711f4a2713aSLionel Sambuc return isFiniteNonZero() && exponent == semantics->minExponent &&
712f4a2713aSLionel Sambuc significandMSB() == 0;
713f4a2713aSLionel Sambuc }
714f4a2713aSLionel Sambuc
isSignificandAllOnes() const715f4a2713aSLionel Sambuc bool APFloat::isSignificandAllOnes() const {
716f4a2713aSLionel Sambuc // Test if the significand excluding the integral bit is all ones. This allows
717f4a2713aSLionel Sambuc // us to test for binade boundaries.
718f4a2713aSLionel Sambuc const integerPart *Parts = significandParts();
719f4a2713aSLionel Sambuc const unsigned PartCount = partCount();
720f4a2713aSLionel Sambuc for (unsigned i = 0; i < PartCount - 1; i++)
721f4a2713aSLionel Sambuc if (~Parts[i])
722f4a2713aSLionel Sambuc return false;
723f4a2713aSLionel Sambuc
724f4a2713aSLionel Sambuc // Set the unused high bits to all ones when we compare.
725f4a2713aSLionel Sambuc const unsigned NumHighBits =
726f4a2713aSLionel Sambuc PartCount*integerPartWidth - semantics->precision + 1;
727f4a2713aSLionel Sambuc assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
728f4a2713aSLionel Sambuc "fill than integerPartWidth");
729f4a2713aSLionel Sambuc const integerPart HighBitFill =
730f4a2713aSLionel Sambuc ~integerPart(0) << (integerPartWidth - NumHighBits);
731f4a2713aSLionel Sambuc if (~(Parts[PartCount - 1] | HighBitFill))
732f4a2713aSLionel Sambuc return false;
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc return true;
735f4a2713aSLionel Sambuc }
736f4a2713aSLionel Sambuc
isSignificandAllZeros() const737f4a2713aSLionel Sambuc bool APFloat::isSignificandAllZeros() const {
738f4a2713aSLionel Sambuc // Test if the significand excluding the integral bit is all zeros. This
739f4a2713aSLionel Sambuc // allows us to test for binade boundaries.
740f4a2713aSLionel Sambuc const integerPart *Parts = significandParts();
741f4a2713aSLionel Sambuc const unsigned PartCount = partCount();
742f4a2713aSLionel Sambuc
743f4a2713aSLionel Sambuc for (unsigned i = 0; i < PartCount - 1; i++)
744f4a2713aSLionel Sambuc if (Parts[i])
745f4a2713aSLionel Sambuc return false;
746f4a2713aSLionel Sambuc
747f4a2713aSLionel Sambuc const unsigned NumHighBits =
748f4a2713aSLionel Sambuc PartCount*integerPartWidth - semantics->precision + 1;
749f4a2713aSLionel Sambuc assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
750f4a2713aSLionel Sambuc "clear than integerPartWidth");
751f4a2713aSLionel Sambuc const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
752f4a2713aSLionel Sambuc
753f4a2713aSLionel Sambuc if (Parts[PartCount - 1] & HighBitMask)
754f4a2713aSLionel Sambuc return false;
755f4a2713aSLionel Sambuc
756f4a2713aSLionel Sambuc return true;
757f4a2713aSLionel Sambuc }
758f4a2713aSLionel Sambuc
759f4a2713aSLionel Sambuc bool
isLargest() const760f4a2713aSLionel Sambuc APFloat::isLargest() const {
761f4a2713aSLionel Sambuc // The largest number by magnitude in our format will be the floating point
762f4a2713aSLionel Sambuc // number with maximum exponent and with significand that is all ones.
763f4a2713aSLionel Sambuc return isFiniteNonZero() && exponent == semantics->maxExponent
764f4a2713aSLionel Sambuc && isSignificandAllOnes();
765f4a2713aSLionel Sambuc }
766f4a2713aSLionel Sambuc
767f4a2713aSLionel Sambuc bool
bitwiseIsEqual(const APFloat & rhs) const768f4a2713aSLionel Sambuc APFloat::bitwiseIsEqual(const APFloat &rhs) const {
769f4a2713aSLionel Sambuc if (this == &rhs)
770f4a2713aSLionel Sambuc return true;
771f4a2713aSLionel Sambuc if (semantics != rhs.semantics ||
772f4a2713aSLionel Sambuc category != rhs.category ||
773f4a2713aSLionel Sambuc sign != rhs.sign)
774f4a2713aSLionel Sambuc return false;
775f4a2713aSLionel Sambuc if (category==fcZero || category==fcInfinity)
776f4a2713aSLionel Sambuc return true;
777f4a2713aSLionel Sambuc else if (isFiniteNonZero() && exponent!=rhs.exponent)
778f4a2713aSLionel Sambuc return false;
779f4a2713aSLionel Sambuc else {
780f4a2713aSLionel Sambuc int i= partCount();
781f4a2713aSLionel Sambuc const integerPart* p=significandParts();
782f4a2713aSLionel Sambuc const integerPart* q=rhs.significandParts();
783f4a2713aSLionel Sambuc for (; i>0; i--, p++, q++) {
784f4a2713aSLionel Sambuc if (*p != *q)
785f4a2713aSLionel Sambuc return false;
786f4a2713aSLionel Sambuc }
787f4a2713aSLionel Sambuc return true;
788f4a2713aSLionel Sambuc }
789f4a2713aSLionel Sambuc }
790f4a2713aSLionel Sambuc
APFloat(const fltSemantics & ourSemantics,integerPart value)791f4a2713aSLionel Sambuc APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) {
792f4a2713aSLionel Sambuc initialize(&ourSemantics);
793f4a2713aSLionel Sambuc sign = 0;
794f4a2713aSLionel Sambuc category = fcNormal;
795f4a2713aSLionel Sambuc zeroSignificand();
796f4a2713aSLionel Sambuc exponent = ourSemantics.precision - 1;
797f4a2713aSLionel Sambuc significandParts()[0] = value;
798f4a2713aSLionel Sambuc normalize(rmNearestTiesToEven, lfExactlyZero);
799f4a2713aSLionel Sambuc }
800f4a2713aSLionel Sambuc
APFloat(const fltSemantics & ourSemantics)801f4a2713aSLionel Sambuc APFloat::APFloat(const fltSemantics &ourSemantics) {
802f4a2713aSLionel Sambuc initialize(&ourSemantics);
803f4a2713aSLionel Sambuc category = fcZero;
804f4a2713aSLionel Sambuc sign = false;
805f4a2713aSLionel Sambuc }
806f4a2713aSLionel Sambuc
APFloat(const fltSemantics & ourSemantics,uninitializedTag tag)807f4a2713aSLionel Sambuc APFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
808f4a2713aSLionel Sambuc // Allocates storage if necessary but does not initialize it.
809f4a2713aSLionel Sambuc initialize(&ourSemantics);
810f4a2713aSLionel Sambuc }
811f4a2713aSLionel Sambuc
APFloat(const fltSemantics & ourSemantics,StringRef text)812f4a2713aSLionel Sambuc APFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) {
813f4a2713aSLionel Sambuc initialize(&ourSemantics);
814f4a2713aSLionel Sambuc convertFromString(text, rmNearestTiesToEven);
815f4a2713aSLionel Sambuc }
816f4a2713aSLionel Sambuc
APFloat(const APFloat & rhs)817f4a2713aSLionel Sambuc APFloat::APFloat(const APFloat &rhs) {
818f4a2713aSLionel Sambuc initialize(rhs.semantics);
819f4a2713aSLionel Sambuc assign(rhs);
820f4a2713aSLionel Sambuc }
821f4a2713aSLionel Sambuc
APFloat(APFloat && rhs)822*0a6a1f1dSLionel Sambuc APFloat::APFloat(APFloat &&rhs) : semantics(&Bogus) {
823*0a6a1f1dSLionel Sambuc *this = std::move(rhs);
824*0a6a1f1dSLionel Sambuc }
825*0a6a1f1dSLionel Sambuc
~APFloat()826f4a2713aSLionel Sambuc APFloat::~APFloat()
827f4a2713aSLionel Sambuc {
828f4a2713aSLionel Sambuc freeSignificand();
829f4a2713aSLionel Sambuc }
830f4a2713aSLionel Sambuc
831f4a2713aSLionel Sambuc // Profile - This method 'profiles' an APFloat for use with FoldingSet.
Profile(FoldingSetNodeID & ID) const832f4a2713aSLionel Sambuc void APFloat::Profile(FoldingSetNodeID& ID) const {
833f4a2713aSLionel Sambuc ID.Add(bitcastToAPInt());
834f4a2713aSLionel Sambuc }
835f4a2713aSLionel Sambuc
836f4a2713aSLionel Sambuc unsigned int
partCount() const837f4a2713aSLionel Sambuc APFloat::partCount() const
838f4a2713aSLionel Sambuc {
839f4a2713aSLionel Sambuc return partCountForBits(semantics->precision + 1);
840f4a2713aSLionel Sambuc }
841f4a2713aSLionel Sambuc
842f4a2713aSLionel Sambuc unsigned int
semanticsPrecision(const fltSemantics & semantics)843f4a2713aSLionel Sambuc APFloat::semanticsPrecision(const fltSemantics &semantics)
844f4a2713aSLionel Sambuc {
845f4a2713aSLionel Sambuc return semantics.precision;
846f4a2713aSLionel Sambuc }
847f4a2713aSLionel Sambuc
848f4a2713aSLionel Sambuc const integerPart *
significandParts() const849f4a2713aSLionel Sambuc APFloat::significandParts() const
850f4a2713aSLionel Sambuc {
851f4a2713aSLionel Sambuc return const_cast<APFloat *>(this)->significandParts();
852f4a2713aSLionel Sambuc }
853f4a2713aSLionel Sambuc
854f4a2713aSLionel Sambuc integerPart *
significandParts()855f4a2713aSLionel Sambuc APFloat::significandParts()
856f4a2713aSLionel Sambuc {
857f4a2713aSLionel Sambuc if (partCount() > 1)
858f4a2713aSLionel Sambuc return significand.parts;
859f4a2713aSLionel Sambuc else
860f4a2713aSLionel Sambuc return &significand.part;
861f4a2713aSLionel Sambuc }
862f4a2713aSLionel Sambuc
863f4a2713aSLionel Sambuc void
zeroSignificand()864f4a2713aSLionel Sambuc APFloat::zeroSignificand()
865f4a2713aSLionel Sambuc {
866f4a2713aSLionel Sambuc APInt::tcSet(significandParts(), 0, partCount());
867f4a2713aSLionel Sambuc }
868f4a2713aSLionel Sambuc
869f4a2713aSLionel Sambuc /* Increment an fcNormal floating point number's significand. */
870f4a2713aSLionel Sambuc void
incrementSignificand()871f4a2713aSLionel Sambuc APFloat::incrementSignificand()
872f4a2713aSLionel Sambuc {
873f4a2713aSLionel Sambuc integerPart carry;
874f4a2713aSLionel Sambuc
875f4a2713aSLionel Sambuc carry = APInt::tcIncrement(significandParts(), partCount());
876f4a2713aSLionel Sambuc
877f4a2713aSLionel Sambuc /* Our callers should never cause us to overflow. */
878f4a2713aSLionel Sambuc assert(carry == 0);
879f4a2713aSLionel Sambuc (void)carry;
880f4a2713aSLionel Sambuc }
881f4a2713aSLionel Sambuc
882f4a2713aSLionel Sambuc /* Add the significand of the RHS. Returns the carry flag. */
883f4a2713aSLionel Sambuc integerPart
addSignificand(const APFloat & rhs)884f4a2713aSLionel Sambuc APFloat::addSignificand(const APFloat &rhs)
885f4a2713aSLionel Sambuc {
886f4a2713aSLionel Sambuc integerPart *parts;
887f4a2713aSLionel Sambuc
888f4a2713aSLionel Sambuc parts = significandParts();
889f4a2713aSLionel Sambuc
890f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
891f4a2713aSLionel Sambuc assert(exponent == rhs.exponent);
892f4a2713aSLionel Sambuc
893f4a2713aSLionel Sambuc return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
894f4a2713aSLionel Sambuc }
895f4a2713aSLionel Sambuc
896f4a2713aSLionel Sambuc /* Subtract the significand of the RHS with a borrow flag. Returns
897f4a2713aSLionel Sambuc the borrow flag. */
898f4a2713aSLionel Sambuc integerPart
subtractSignificand(const APFloat & rhs,integerPart borrow)899f4a2713aSLionel Sambuc APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
900f4a2713aSLionel Sambuc {
901f4a2713aSLionel Sambuc integerPart *parts;
902f4a2713aSLionel Sambuc
903f4a2713aSLionel Sambuc parts = significandParts();
904f4a2713aSLionel Sambuc
905f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
906f4a2713aSLionel Sambuc assert(exponent == rhs.exponent);
907f4a2713aSLionel Sambuc
908f4a2713aSLionel Sambuc return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
909f4a2713aSLionel Sambuc partCount());
910f4a2713aSLionel Sambuc }
911f4a2713aSLionel Sambuc
912f4a2713aSLionel Sambuc /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
913f4a2713aSLionel Sambuc on to the full-precision result of the multiplication. Returns the
914f4a2713aSLionel Sambuc lost fraction. */
915f4a2713aSLionel Sambuc lostFraction
multiplySignificand(const APFloat & rhs,const APFloat * addend)916f4a2713aSLionel Sambuc APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
917f4a2713aSLionel Sambuc {
918f4a2713aSLionel Sambuc unsigned int omsb; // One, not zero, based MSB.
919f4a2713aSLionel Sambuc unsigned int partsCount, newPartsCount, precision;
920f4a2713aSLionel Sambuc integerPart *lhsSignificand;
921f4a2713aSLionel Sambuc integerPart scratch[4];
922f4a2713aSLionel Sambuc integerPart *fullSignificand;
923f4a2713aSLionel Sambuc lostFraction lost_fraction;
924f4a2713aSLionel Sambuc bool ignored;
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
927f4a2713aSLionel Sambuc
928f4a2713aSLionel Sambuc precision = semantics->precision;
929*0a6a1f1dSLionel Sambuc
930*0a6a1f1dSLionel Sambuc // Allocate space for twice as many bits as the original significand, plus one
931*0a6a1f1dSLionel Sambuc // extra bit for the addition to overflow into.
932*0a6a1f1dSLionel Sambuc newPartsCount = partCountForBits(precision * 2 + 1);
933f4a2713aSLionel Sambuc
934f4a2713aSLionel Sambuc if (newPartsCount > 4)
935f4a2713aSLionel Sambuc fullSignificand = new integerPart[newPartsCount];
936f4a2713aSLionel Sambuc else
937f4a2713aSLionel Sambuc fullSignificand = scratch;
938f4a2713aSLionel Sambuc
939f4a2713aSLionel Sambuc lhsSignificand = significandParts();
940f4a2713aSLionel Sambuc partsCount = partCount();
941f4a2713aSLionel Sambuc
942f4a2713aSLionel Sambuc APInt::tcFullMultiply(fullSignificand, lhsSignificand,
943f4a2713aSLionel Sambuc rhs.significandParts(), partsCount, partsCount);
944f4a2713aSLionel Sambuc
945f4a2713aSLionel Sambuc lost_fraction = lfExactlyZero;
946f4a2713aSLionel Sambuc omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
947f4a2713aSLionel Sambuc exponent += rhs.exponent;
948f4a2713aSLionel Sambuc
949f4a2713aSLionel Sambuc // Assume the operands involved in the multiplication are single-precision
950f4a2713aSLionel Sambuc // FP, and the two multiplicants are:
951f4a2713aSLionel Sambuc // *this = a23 . a22 ... a0 * 2^e1
952f4a2713aSLionel Sambuc // rhs = b23 . b22 ... b0 * 2^e2
953f4a2713aSLionel Sambuc // the result of multiplication is:
954*0a6a1f1dSLionel Sambuc // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2)
955*0a6a1f1dSLionel Sambuc // Note that there are three significant bits at the left-hand side of the
956*0a6a1f1dSLionel Sambuc // radix point: two for the multiplication, and an overflow bit for the
957*0a6a1f1dSLionel Sambuc // addition (that will always be zero at this point). Move the radix point
958*0a6a1f1dSLionel Sambuc // toward left by two bits, and adjust exponent accordingly.
959*0a6a1f1dSLionel Sambuc exponent += 2;
960f4a2713aSLionel Sambuc
961*0a6a1f1dSLionel Sambuc if (addend && addend->isNonZero()) {
962f4a2713aSLionel Sambuc // The intermediate result of the multiplication has "2 * precision"
963f4a2713aSLionel Sambuc // signicant bit; adjust the addend to be consistent with mul result.
964f4a2713aSLionel Sambuc //
965f4a2713aSLionel Sambuc Significand savedSignificand = significand;
966f4a2713aSLionel Sambuc const fltSemantics *savedSemantics = semantics;
967f4a2713aSLionel Sambuc fltSemantics extendedSemantics;
968f4a2713aSLionel Sambuc opStatus status;
969f4a2713aSLionel Sambuc unsigned int extendedPrecision;
970f4a2713aSLionel Sambuc
971*0a6a1f1dSLionel Sambuc // Normalize our MSB to one below the top bit to allow for overflow.
972*0a6a1f1dSLionel Sambuc extendedPrecision = 2 * precision + 1;
973*0a6a1f1dSLionel Sambuc if (omsb != extendedPrecision - 1) {
974f4a2713aSLionel Sambuc assert(extendedPrecision > omsb);
975f4a2713aSLionel Sambuc APInt::tcShiftLeft(fullSignificand, newPartsCount,
976*0a6a1f1dSLionel Sambuc (extendedPrecision - 1) - omsb);
977*0a6a1f1dSLionel Sambuc exponent -= (extendedPrecision - 1) - omsb;
978f4a2713aSLionel Sambuc }
979f4a2713aSLionel Sambuc
980f4a2713aSLionel Sambuc /* Create new semantics. */
981f4a2713aSLionel Sambuc extendedSemantics = *semantics;
982f4a2713aSLionel Sambuc extendedSemantics.precision = extendedPrecision;
983f4a2713aSLionel Sambuc
984f4a2713aSLionel Sambuc if (newPartsCount == 1)
985f4a2713aSLionel Sambuc significand.part = fullSignificand[0];
986f4a2713aSLionel Sambuc else
987f4a2713aSLionel Sambuc significand.parts = fullSignificand;
988f4a2713aSLionel Sambuc semantics = &extendedSemantics;
989f4a2713aSLionel Sambuc
990f4a2713aSLionel Sambuc APFloat extendedAddend(*addend);
991f4a2713aSLionel Sambuc status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
992f4a2713aSLionel Sambuc assert(status == opOK);
993f4a2713aSLionel Sambuc (void)status;
994*0a6a1f1dSLionel Sambuc
995*0a6a1f1dSLionel Sambuc // Shift the significand of the addend right by one bit. This guarantees
996*0a6a1f1dSLionel Sambuc // that the high bit of the significand is zero (same as fullSignificand),
997*0a6a1f1dSLionel Sambuc // so the addition will overflow (if it does overflow at all) into the top bit.
998*0a6a1f1dSLionel Sambuc lost_fraction = extendedAddend.shiftSignificandRight(1);
999*0a6a1f1dSLionel Sambuc assert(lost_fraction == lfExactlyZero &&
1000*0a6a1f1dSLionel Sambuc "Lost precision while shifting addend for fused-multiply-add.");
1001*0a6a1f1dSLionel Sambuc
1002f4a2713aSLionel Sambuc lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1003f4a2713aSLionel Sambuc
1004f4a2713aSLionel Sambuc /* Restore our state. */
1005f4a2713aSLionel Sambuc if (newPartsCount == 1)
1006f4a2713aSLionel Sambuc fullSignificand[0] = significand.part;
1007f4a2713aSLionel Sambuc significand = savedSignificand;
1008f4a2713aSLionel Sambuc semantics = savedSemantics;
1009f4a2713aSLionel Sambuc
1010f4a2713aSLionel Sambuc omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1011f4a2713aSLionel Sambuc }
1012f4a2713aSLionel Sambuc
1013f4a2713aSLionel Sambuc // Convert the result having "2 * precision" significant-bits back to the one
1014f4a2713aSLionel Sambuc // having "precision" significant-bits. First, move the radix point from
1015f4a2713aSLionel Sambuc // poision "2*precision - 1" to "precision - 1". The exponent need to be
1016f4a2713aSLionel Sambuc // adjusted by "2*precision - 1" - "precision - 1" = "precision".
1017*0a6a1f1dSLionel Sambuc exponent -= precision + 1;
1018f4a2713aSLionel Sambuc
1019f4a2713aSLionel Sambuc // In case MSB resides at the left-hand side of radix point, shift the
1020f4a2713aSLionel Sambuc // mantissa right by some amount to make sure the MSB reside right before
1021f4a2713aSLionel Sambuc // the radix point (i.e. "MSB . rest-significant-bits").
1022f4a2713aSLionel Sambuc //
1023f4a2713aSLionel Sambuc // Note that the result is not normalized when "omsb < precision". So, the
1024f4a2713aSLionel Sambuc // caller needs to call APFloat::normalize() if normalized value is expected.
1025f4a2713aSLionel Sambuc if (omsb > precision) {
1026f4a2713aSLionel Sambuc unsigned int bits, significantParts;
1027f4a2713aSLionel Sambuc lostFraction lf;
1028f4a2713aSLionel Sambuc
1029f4a2713aSLionel Sambuc bits = omsb - precision;
1030f4a2713aSLionel Sambuc significantParts = partCountForBits(omsb);
1031f4a2713aSLionel Sambuc lf = shiftRight(fullSignificand, significantParts, bits);
1032f4a2713aSLionel Sambuc lost_fraction = combineLostFractions(lf, lost_fraction);
1033f4a2713aSLionel Sambuc exponent += bits;
1034f4a2713aSLionel Sambuc }
1035f4a2713aSLionel Sambuc
1036f4a2713aSLionel Sambuc APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1037f4a2713aSLionel Sambuc
1038f4a2713aSLionel Sambuc if (newPartsCount > 4)
1039f4a2713aSLionel Sambuc delete [] fullSignificand;
1040f4a2713aSLionel Sambuc
1041f4a2713aSLionel Sambuc return lost_fraction;
1042f4a2713aSLionel Sambuc }
1043f4a2713aSLionel Sambuc
1044f4a2713aSLionel Sambuc /* Multiply the significands of LHS and RHS to DST. */
1045f4a2713aSLionel Sambuc lostFraction
divideSignificand(const APFloat & rhs)1046f4a2713aSLionel Sambuc APFloat::divideSignificand(const APFloat &rhs)
1047f4a2713aSLionel Sambuc {
1048f4a2713aSLionel Sambuc unsigned int bit, i, partsCount;
1049f4a2713aSLionel Sambuc const integerPart *rhsSignificand;
1050f4a2713aSLionel Sambuc integerPart *lhsSignificand, *dividend, *divisor;
1051f4a2713aSLionel Sambuc integerPart scratch[4];
1052f4a2713aSLionel Sambuc lostFraction lost_fraction;
1053f4a2713aSLionel Sambuc
1054f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
1055f4a2713aSLionel Sambuc
1056f4a2713aSLionel Sambuc lhsSignificand = significandParts();
1057f4a2713aSLionel Sambuc rhsSignificand = rhs.significandParts();
1058f4a2713aSLionel Sambuc partsCount = partCount();
1059f4a2713aSLionel Sambuc
1060f4a2713aSLionel Sambuc if (partsCount > 2)
1061f4a2713aSLionel Sambuc dividend = new integerPart[partsCount * 2];
1062f4a2713aSLionel Sambuc else
1063f4a2713aSLionel Sambuc dividend = scratch;
1064f4a2713aSLionel Sambuc
1065f4a2713aSLionel Sambuc divisor = dividend + partsCount;
1066f4a2713aSLionel Sambuc
1067f4a2713aSLionel Sambuc /* Copy the dividend and divisor as they will be modified in-place. */
1068f4a2713aSLionel Sambuc for (i = 0; i < partsCount; i++) {
1069f4a2713aSLionel Sambuc dividend[i] = lhsSignificand[i];
1070f4a2713aSLionel Sambuc divisor[i] = rhsSignificand[i];
1071f4a2713aSLionel Sambuc lhsSignificand[i] = 0;
1072f4a2713aSLionel Sambuc }
1073f4a2713aSLionel Sambuc
1074f4a2713aSLionel Sambuc exponent -= rhs.exponent;
1075f4a2713aSLionel Sambuc
1076f4a2713aSLionel Sambuc unsigned int precision = semantics->precision;
1077f4a2713aSLionel Sambuc
1078f4a2713aSLionel Sambuc /* Normalize the divisor. */
1079f4a2713aSLionel Sambuc bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
1080f4a2713aSLionel Sambuc if (bit) {
1081f4a2713aSLionel Sambuc exponent += bit;
1082f4a2713aSLionel Sambuc APInt::tcShiftLeft(divisor, partsCount, bit);
1083f4a2713aSLionel Sambuc }
1084f4a2713aSLionel Sambuc
1085f4a2713aSLionel Sambuc /* Normalize the dividend. */
1086f4a2713aSLionel Sambuc bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
1087f4a2713aSLionel Sambuc if (bit) {
1088f4a2713aSLionel Sambuc exponent -= bit;
1089f4a2713aSLionel Sambuc APInt::tcShiftLeft(dividend, partsCount, bit);
1090f4a2713aSLionel Sambuc }
1091f4a2713aSLionel Sambuc
1092f4a2713aSLionel Sambuc /* Ensure the dividend >= divisor initially for the loop below.
1093f4a2713aSLionel Sambuc Incidentally, this means that the division loop below is
1094f4a2713aSLionel Sambuc guaranteed to set the integer bit to one. */
1095f4a2713aSLionel Sambuc if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
1096f4a2713aSLionel Sambuc exponent--;
1097f4a2713aSLionel Sambuc APInt::tcShiftLeft(dividend, partsCount, 1);
1098f4a2713aSLionel Sambuc assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
1099f4a2713aSLionel Sambuc }
1100f4a2713aSLionel Sambuc
1101f4a2713aSLionel Sambuc /* Long division. */
1102f4a2713aSLionel Sambuc for (bit = precision; bit; bit -= 1) {
1103f4a2713aSLionel Sambuc if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
1104f4a2713aSLionel Sambuc APInt::tcSubtract(dividend, divisor, 0, partsCount);
1105f4a2713aSLionel Sambuc APInt::tcSetBit(lhsSignificand, bit - 1);
1106f4a2713aSLionel Sambuc }
1107f4a2713aSLionel Sambuc
1108f4a2713aSLionel Sambuc APInt::tcShiftLeft(dividend, partsCount, 1);
1109f4a2713aSLionel Sambuc }
1110f4a2713aSLionel Sambuc
1111f4a2713aSLionel Sambuc /* Figure out the lost fraction. */
1112f4a2713aSLionel Sambuc int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc if (cmp > 0)
1115f4a2713aSLionel Sambuc lost_fraction = lfMoreThanHalf;
1116f4a2713aSLionel Sambuc else if (cmp == 0)
1117f4a2713aSLionel Sambuc lost_fraction = lfExactlyHalf;
1118f4a2713aSLionel Sambuc else if (APInt::tcIsZero(dividend, partsCount))
1119f4a2713aSLionel Sambuc lost_fraction = lfExactlyZero;
1120f4a2713aSLionel Sambuc else
1121f4a2713aSLionel Sambuc lost_fraction = lfLessThanHalf;
1122f4a2713aSLionel Sambuc
1123f4a2713aSLionel Sambuc if (partsCount > 2)
1124f4a2713aSLionel Sambuc delete [] dividend;
1125f4a2713aSLionel Sambuc
1126f4a2713aSLionel Sambuc return lost_fraction;
1127f4a2713aSLionel Sambuc }
1128f4a2713aSLionel Sambuc
1129f4a2713aSLionel Sambuc unsigned int
significandMSB() const1130f4a2713aSLionel Sambuc APFloat::significandMSB() const
1131f4a2713aSLionel Sambuc {
1132f4a2713aSLionel Sambuc return APInt::tcMSB(significandParts(), partCount());
1133f4a2713aSLionel Sambuc }
1134f4a2713aSLionel Sambuc
1135f4a2713aSLionel Sambuc unsigned int
significandLSB() const1136f4a2713aSLionel Sambuc APFloat::significandLSB() const
1137f4a2713aSLionel Sambuc {
1138f4a2713aSLionel Sambuc return APInt::tcLSB(significandParts(), partCount());
1139f4a2713aSLionel Sambuc }
1140f4a2713aSLionel Sambuc
1141f4a2713aSLionel Sambuc /* Note that a zero result is NOT normalized to fcZero. */
1142f4a2713aSLionel Sambuc lostFraction
shiftSignificandRight(unsigned int bits)1143f4a2713aSLionel Sambuc APFloat::shiftSignificandRight(unsigned int bits)
1144f4a2713aSLionel Sambuc {
1145f4a2713aSLionel Sambuc /* Our exponent should not overflow. */
1146f4a2713aSLionel Sambuc assert((ExponentType) (exponent + bits) >= exponent);
1147f4a2713aSLionel Sambuc
1148f4a2713aSLionel Sambuc exponent += bits;
1149f4a2713aSLionel Sambuc
1150f4a2713aSLionel Sambuc return shiftRight(significandParts(), partCount(), bits);
1151f4a2713aSLionel Sambuc }
1152f4a2713aSLionel Sambuc
1153f4a2713aSLionel Sambuc /* Shift the significand left BITS bits, subtract BITS from its exponent. */
1154f4a2713aSLionel Sambuc void
shiftSignificandLeft(unsigned int bits)1155f4a2713aSLionel Sambuc APFloat::shiftSignificandLeft(unsigned int bits)
1156f4a2713aSLionel Sambuc {
1157f4a2713aSLionel Sambuc assert(bits < semantics->precision);
1158f4a2713aSLionel Sambuc
1159f4a2713aSLionel Sambuc if (bits) {
1160f4a2713aSLionel Sambuc unsigned int partsCount = partCount();
1161f4a2713aSLionel Sambuc
1162f4a2713aSLionel Sambuc APInt::tcShiftLeft(significandParts(), partsCount, bits);
1163f4a2713aSLionel Sambuc exponent -= bits;
1164f4a2713aSLionel Sambuc
1165f4a2713aSLionel Sambuc assert(!APInt::tcIsZero(significandParts(), partsCount));
1166f4a2713aSLionel Sambuc }
1167f4a2713aSLionel Sambuc }
1168f4a2713aSLionel Sambuc
1169f4a2713aSLionel Sambuc APFloat::cmpResult
compareAbsoluteValue(const APFloat & rhs) const1170f4a2713aSLionel Sambuc APFloat::compareAbsoluteValue(const APFloat &rhs) const
1171f4a2713aSLionel Sambuc {
1172f4a2713aSLionel Sambuc int compare;
1173f4a2713aSLionel Sambuc
1174f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
1175f4a2713aSLionel Sambuc assert(isFiniteNonZero());
1176f4a2713aSLionel Sambuc assert(rhs.isFiniteNonZero());
1177f4a2713aSLionel Sambuc
1178f4a2713aSLionel Sambuc compare = exponent - rhs.exponent;
1179f4a2713aSLionel Sambuc
1180f4a2713aSLionel Sambuc /* If exponents are equal, do an unsigned bignum comparison of the
1181f4a2713aSLionel Sambuc significands. */
1182f4a2713aSLionel Sambuc if (compare == 0)
1183f4a2713aSLionel Sambuc compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
1184f4a2713aSLionel Sambuc partCount());
1185f4a2713aSLionel Sambuc
1186f4a2713aSLionel Sambuc if (compare > 0)
1187f4a2713aSLionel Sambuc return cmpGreaterThan;
1188f4a2713aSLionel Sambuc else if (compare < 0)
1189f4a2713aSLionel Sambuc return cmpLessThan;
1190f4a2713aSLionel Sambuc else
1191f4a2713aSLionel Sambuc return cmpEqual;
1192f4a2713aSLionel Sambuc }
1193f4a2713aSLionel Sambuc
1194f4a2713aSLionel Sambuc /* Handle overflow. Sign is preserved. We either become infinity or
1195f4a2713aSLionel Sambuc the largest finite number. */
1196f4a2713aSLionel Sambuc APFloat::opStatus
handleOverflow(roundingMode rounding_mode)1197f4a2713aSLionel Sambuc APFloat::handleOverflow(roundingMode rounding_mode)
1198f4a2713aSLionel Sambuc {
1199f4a2713aSLionel Sambuc /* Infinity? */
1200f4a2713aSLionel Sambuc if (rounding_mode == rmNearestTiesToEven ||
1201f4a2713aSLionel Sambuc rounding_mode == rmNearestTiesToAway ||
1202f4a2713aSLionel Sambuc (rounding_mode == rmTowardPositive && !sign) ||
1203f4a2713aSLionel Sambuc (rounding_mode == rmTowardNegative && sign)) {
1204f4a2713aSLionel Sambuc category = fcInfinity;
1205f4a2713aSLionel Sambuc return (opStatus) (opOverflow | opInexact);
1206f4a2713aSLionel Sambuc }
1207f4a2713aSLionel Sambuc
1208f4a2713aSLionel Sambuc /* Otherwise we become the largest finite number. */
1209f4a2713aSLionel Sambuc category = fcNormal;
1210f4a2713aSLionel Sambuc exponent = semantics->maxExponent;
1211f4a2713aSLionel Sambuc APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
1212f4a2713aSLionel Sambuc semantics->precision);
1213f4a2713aSLionel Sambuc
1214f4a2713aSLionel Sambuc return opInexact;
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc
1217f4a2713aSLionel Sambuc /* Returns TRUE if, when truncating the current number, with BIT the
1218f4a2713aSLionel Sambuc new LSB, with the given lost fraction and rounding mode, the result
1219f4a2713aSLionel Sambuc would need to be rounded away from zero (i.e., by increasing the
1220f4a2713aSLionel Sambuc signficand). This routine must work for fcZero of both signs, and
1221f4a2713aSLionel Sambuc fcNormal numbers. */
1222f4a2713aSLionel Sambuc bool
roundAwayFromZero(roundingMode rounding_mode,lostFraction lost_fraction,unsigned int bit) const1223f4a2713aSLionel Sambuc APFloat::roundAwayFromZero(roundingMode rounding_mode,
1224f4a2713aSLionel Sambuc lostFraction lost_fraction,
1225f4a2713aSLionel Sambuc unsigned int bit) const
1226f4a2713aSLionel Sambuc {
1227f4a2713aSLionel Sambuc /* NaNs and infinities should not have lost fractions. */
1228f4a2713aSLionel Sambuc assert(isFiniteNonZero() || category == fcZero);
1229f4a2713aSLionel Sambuc
1230f4a2713aSLionel Sambuc /* Current callers never pass this so we don't handle it. */
1231f4a2713aSLionel Sambuc assert(lost_fraction != lfExactlyZero);
1232f4a2713aSLionel Sambuc
1233f4a2713aSLionel Sambuc switch (rounding_mode) {
1234f4a2713aSLionel Sambuc case rmNearestTiesToAway:
1235f4a2713aSLionel Sambuc return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1236f4a2713aSLionel Sambuc
1237f4a2713aSLionel Sambuc case rmNearestTiesToEven:
1238f4a2713aSLionel Sambuc if (lost_fraction == lfMoreThanHalf)
1239f4a2713aSLionel Sambuc return true;
1240f4a2713aSLionel Sambuc
1241f4a2713aSLionel Sambuc /* Our zeroes don't have a significand to test. */
1242f4a2713aSLionel Sambuc if (lost_fraction == lfExactlyHalf && category != fcZero)
1243f4a2713aSLionel Sambuc return APInt::tcExtractBit(significandParts(), bit);
1244f4a2713aSLionel Sambuc
1245f4a2713aSLionel Sambuc return false;
1246f4a2713aSLionel Sambuc
1247f4a2713aSLionel Sambuc case rmTowardZero:
1248f4a2713aSLionel Sambuc return false;
1249f4a2713aSLionel Sambuc
1250f4a2713aSLionel Sambuc case rmTowardPositive:
1251f4a2713aSLionel Sambuc return sign == false;
1252f4a2713aSLionel Sambuc
1253f4a2713aSLionel Sambuc case rmTowardNegative:
1254f4a2713aSLionel Sambuc return sign == true;
1255f4a2713aSLionel Sambuc }
1256f4a2713aSLionel Sambuc llvm_unreachable("Invalid rounding mode found");
1257f4a2713aSLionel Sambuc }
1258f4a2713aSLionel Sambuc
1259f4a2713aSLionel Sambuc APFloat::opStatus
normalize(roundingMode rounding_mode,lostFraction lost_fraction)1260f4a2713aSLionel Sambuc APFloat::normalize(roundingMode rounding_mode,
1261f4a2713aSLionel Sambuc lostFraction lost_fraction)
1262f4a2713aSLionel Sambuc {
1263f4a2713aSLionel Sambuc unsigned int omsb; /* One, not zero, based MSB. */
1264f4a2713aSLionel Sambuc int exponentChange;
1265f4a2713aSLionel Sambuc
1266f4a2713aSLionel Sambuc if (!isFiniteNonZero())
1267f4a2713aSLionel Sambuc return opOK;
1268f4a2713aSLionel Sambuc
1269f4a2713aSLionel Sambuc /* Before rounding normalize the exponent of fcNormal numbers. */
1270f4a2713aSLionel Sambuc omsb = significandMSB() + 1;
1271f4a2713aSLionel Sambuc
1272f4a2713aSLionel Sambuc if (omsb) {
1273f4a2713aSLionel Sambuc /* OMSB is numbered from 1. We want to place it in the integer
1274f4a2713aSLionel Sambuc bit numbered PRECISION if possible, with a compensating change in
1275f4a2713aSLionel Sambuc the exponent. */
1276f4a2713aSLionel Sambuc exponentChange = omsb - semantics->precision;
1277f4a2713aSLionel Sambuc
1278f4a2713aSLionel Sambuc /* If the resulting exponent is too high, overflow according to
1279f4a2713aSLionel Sambuc the rounding mode. */
1280f4a2713aSLionel Sambuc if (exponent + exponentChange > semantics->maxExponent)
1281f4a2713aSLionel Sambuc return handleOverflow(rounding_mode);
1282f4a2713aSLionel Sambuc
1283f4a2713aSLionel Sambuc /* Subnormal numbers have exponent minExponent, and their MSB
1284f4a2713aSLionel Sambuc is forced based on that. */
1285f4a2713aSLionel Sambuc if (exponent + exponentChange < semantics->minExponent)
1286f4a2713aSLionel Sambuc exponentChange = semantics->minExponent - exponent;
1287f4a2713aSLionel Sambuc
1288f4a2713aSLionel Sambuc /* Shifting left is easy as we don't lose precision. */
1289f4a2713aSLionel Sambuc if (exponentChange < 0) {
1290f4a2713aSLionel Sambuc assert(lost_fraction == lfExactlyZero);
1291f4a2713aSLionel Sambuc
1292f4a2713aSLionel Sambuc shiftSignificandLeft(-exponentChange);
1293f4a2713aSLionel Sambuc
1294f4a2713aSLionel Sambuc return opOK;
1295f4a2713aSLionel Sambuc }
1296f4a2713aSLionel Sambuc
1297f4a2713aSLionel Sambuc if (exponentChange > 0) {
1298f4a2713aSLionel Sambuc lostFraction lf;
1299f4a2713aSLionel Sambuc
1300f4a2713aSLionel Sambuc /* Shift right and capture any new lost fraction. */
1301f4a2713aSLionel Sambuc lf = shiftSignificandRight(exponentChange);
1302f4a2713aSLionel Sambuc
1303f4a2713aSLionel Sambuc lost_fraction = combineLostFractions(lf, lost_fraction);
1304f4a2713aSLionel Sambuc
1305f4a2713aSLionel Sambuc /* Keep OMSB up-to-date. */
1306f4a2713aSLionel Sambuc if (omsb > (unsigned) exponentChange)
1307f4a2713aSLionel Sambuc omsb -= exponentChange;
1308f4a2713aSLionel Sambuc else
1309f4a2713aSLionel Sambuc omsb = 0;
1310f4a2713aSLionel Sambuc }
1311f4a2713aSLionel Sambuc }
1312f4a2713aSLionel Sambuc
1313f4a2713aSLionel Sambuc /* Now round the number according to rounding_mode given the lost
1314f4a2713aSLionel Sambuc fraction. */
1315f4a2713aSLionel Sambuc
1316f4a2713aSLionel Sambuc /* As specified in IEEE 754, since we do not trap we do not report
1317f4a2713aSLionel Sambuc underflow for exact results. */
1318f4a2713aSLionel Sambuc if (lost_fraction == lfExactlyZero) {
1319f4a2713aSLionel Sambuc /* Canonicalize zeroes. */
1320f4a2713aSLionel Sambuc if (omsb == 0)
1321f4a2713aSLionel Sambuc category = fcZero;
1322f4a2713aSLionel Sambuc
1323f4a2713aSLionel Sambuc return opOK;
1324f4a2713aSLionel Sambuc }
1325f4a2713aSLionel Sambuc
1326f4a2713aSLionel Sambuc /* Increment the significand if we're rounding away from zero. */
1327f4a2713aSLionel Sambuc if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1328f4a2713aSLionel Sambuc if (omsb == 0)
1329f4a2713aSLionel Sambuc exponent = semantics->minExponent;
1330f4a2713aSLionel Sambuc
1331f4a2713aSLionel Sambuc incrementSignificand();
1332f4a2713aSLionel Sambuc omsb = significandMSB() + 1;
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc /* Did the significand increment overflow? */
1335f4a2713aSLionel Sambuc if (omsb == (unsigned) semantics->precision + 1) {
1336f4a2713aSLionel Sambuc /* Renormalize by incrementing the exponent and shifting our
1337f4a2713aSLionel Sambuc significand right one. However if we already have the
1338f4a2713aSLionel Sambuc maximum exponent we overflow to infinity. */
1339f4a2713aSLionel Sambuc if (exponent == semantics->maxExponent) {
1340f4a2713aSLionel Sambuc category = fcInfinity;
1341f4a2713aSLionel Sambuc
1342f4a2713aSLionel Sambuc return (opStatus) (opOverflow | opInexact);
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc shiftSignificandRight(1);
1346f4a2713aSLionel Sambuc
1347f4a2713aSLionel Sambuc return opInexact;
1348f4a2713aSLionel Sambuc }
1349f4a2713aSLionel Sambuc }
1350f4a2713aSLionel Sambuc
1351f4a2713aSLionel Sambuc /* The normal case - we were and are not denormal, and any
1352f4a2713aSLionel Sambuc significand increment above didn't overflow. */
1353f4a2713aSLionel Sambuc if (omsb == semantics->precision)
1354f4a2713aSLionel Sambuc return opInexact;
1355f4a2713aSLionel Sambuc
1356f4a2713aSLionel Sambuc /* We have a non-zero denormal. */
1357f4a2713aSLionel Sambuc assert(omsb < semantics->precision);
1358f4a2713aSLionel Sambuc
1359f4a2713aSLionel Sambuc /* Canonicalize zeroes. */
1360f4a2713aSLionel Sambuc if (omsb == 0)
1361f4a2713aSLionel Sambuc category = fcZero;
1362f4a2713aSLionel Sambuc
1363f4a2713aSLionel Sambuc /* The fcZero case is a denormal that underflowed to zero. */
1364f4a2713aSLionel Sambuc return (opStatus) (opUnderflow | opInexact);
1365f4a2713aSLionel Sambuc }
1366f4a2713aSLionel Sambuc
1367f4a2713aSLionel Sambuc APFloat::opStatus
addOrSubtractSpecials(const APFloat & rhs,bool subtract)1368f4a2713aSLionel Sambuc APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1369f4a2713aSLionel Sambuc {
1370f4a2713aSLionel Sambuc switch (PackCategoriesIntoKey(category, rhs.category)) {
1371f4a2713aSLionel Sambuc default:
1372*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcZero):
1375f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNormal):
1376f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcInfinity):
1377f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNaN):
1378f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcZero):
1379f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNormal):
1380f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcZero):
1381f4a2713aSLionel Sambuc return opOK;
1382f4a2713aSLionel Sambuc
1383f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNaN):
1384f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNaN):
1385f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNaN):
1386*0a6a1f1dSLionel Sambuc // We need to be sure to flip the sign here for subtraction because we
1387*0a6a1f1dSLionel Sambuc // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1388*0a6a1f1dSLionel Sambuc sign = rhs.sign ^ subtract;
1389f4a2713aSLionel Sambuc category = fcNaN;
1390f4a2713aSLionel Sambuc copySignificand(rhs);
1391f4a2713aSLionel Sambuc return opOK;
1392f4a2713aSLionel Sambuc
1393f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcInfinity):
1394f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcInfinity):
1395f4a2713aSLionel Sambuc category = fcInfinity;
1396f4a2713aSLionel Sambuc sign = rhs.sign ^ subtract;
1397f4a2713aSLionel Sambuc return opOK;
1398f4a2713aSLionel Sambuc
1399f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNormal):
1400f4a2713aSLionel Sambuc assign(rhs);
1401f4a2713aSLionel Sambuc sign = rhs.sign ^ subtract;
1402f4a2713aSLionel Sambuc return opOK;
1403f4a2713aSLionel Sambuc
1404f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcZero):
1405f4a2713aSLionel Sambuc /* Sign depends on rounding mode; handled by caller. */
1406f4a2713aSLionel Sambuc return opOK;
1407f4a2713aSLionel Sambuc
1408f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1409f4a2713aSLionel Sambuc /* Differently signed infinities can only be validly
1410f4a2713aSLionel Sambuc subtracted. */
1411f4a2713aSLionel Sambuc if (((sign ^ rhs.sign)!=0) != subtract) {
1412f4a2713aSLionel Sambuc makeNaN();
1413f4a2713aSLionel Sambuc return opInvalidOp;
1414f4a2713aSLionel Sambuc }
1415f4a2713aSLionel Sambuc
1416f4a2713aSLionel Sambuc return opOK;
1417f4a2713aSLionel Sambuc
1418f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNormal):
1419f4a2713aSLionel Sambuc return opDivByZero;
1420f4a2713aSLionel Sambuc }
1421f4a2713aSLionel Sambuc }
1422f4a2713aSLionel Sambuc
1423f4a2713aSLionel Sambuc /* Add or subtract two normal numbers. */
1424f4a2713aSLionel Sambuc lostFraction
addOrSubtractSignificand(const APFloat & rhs,bool subtract)1425f4a2713aSLionel Sambuc APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1426f4a2713aSLionel Sambuc {
1427f4a2713aSLionel Sambuc integerPart carry;
1428f4a2713aSLionel Sambuc lostFraction lost_fraction;
1429f4a2713aSLionel Sambuc int bits;
1430f4a2713aSLionel Sambuc
1431f4a2713aSLionel Sambuc /* Determine if the operation on the absolute values is effectively
1432f4a2713aSLionel Sambuc an addition or subtraction. */
1433f4a2713aSLionel Sambuc subtract ^= (sign ^ rhs.sign) ? true : false;
1434f4a2713aSLionel Sambuc
1435f4a2713aSLionel Sambuc /* Are we bigger exponent-wise than the RHS? */
1436f4a2713aSLionel Sambuc bits = exponent - rhs.exponent;
1437f4a2713aSLionel Sambuc
1438f4a2713aSLionel Sambuc /* Subtraction is more subtle than one might naively expect. */
1439f4a2713aSLionel Sambuc if (subtract) {
1440f4a2713aSLionel Sambuc APFloat temp_rhs(rhs);
1441f4a2713aSLionel Sambuc bool reverse;
1442f4a2713aSLionel Sambuc
1443f4a2713aSLionel Sambuc if (bits == 0) {
1444f4a2713aSLionel Sambuc reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1445f4a2713aSLionel Sambuc lost_fraction = lfExactlyZero;
1446f4a2713aSLionel Sambuc } else if (bits > 0) {
1447f4a2713aSLionel Sambuc lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1448f4a2713aSLionel Sambuc shiftSignificandLeft(1);
1449f4a2713aSLionel Sambuc reverse = false;
1450f4a2713aSLionel Sambuc } else {
1451f4a2713aSLionel Sambuc lost_fraction = shiftSignificandRight(-bits - 1);
1452f4a2713aSLionel Sambuc temp_rhs.shiftSignificandLeft(1);
1453f4a2713aSLionel Sambuc reverse = true;
1454f4a2713aSLionel Sambuc }
1455f4a2713aSLionel Sambuc
1456f4a2713aSLionel Sambuc if (reverse) {
1457f4a2713aSLionel Sambuc carry = temp_rhs.subtractSignificand
1458f4a2713aSLionel Sambuc (*this, lost_fraction != lfExactlyZero);
1459f4a2713aSLionel Sambuc copySignificand(temp_rhs);
1460f4a2713aSLionel Sambuc sign = !sign;
1461f4a2713aSLionel Sambuc } else {
1462f4a2713aSLionel Sambuc carry = subtractSignificand
1463f4a2713aSLionel Sambuc (temp_rhs, lost_fraction != lfExactlyZero);
1464f4a2713aSLionel Sambuc }
1465f4a2713aSLionel Sambuc
1466f4a2713aSLionel Sambuc /* Invert the lost fraction - it was on the RHS and
1467f4a2713aSLionel Sambuc subtracted. */
1468f4a2713aSLionel Sambuc if (lost_fraction == lfLessThanHalf)
1469f4a2713aSLionel Sambuc lost_fraction = lfMoreThanHalf;
1470f4a2713aSLionel Sambuc else if (lost_fraction == lfMoreThanHalf)
1471f4a2713aSLionel Sambuc lost_fraction = lfLessThanHalf;
1472f4a2713aSLionel Sambuc
1473f4a2713aSLionel Sambuc /* The code above is intended to ensure that no borrow is
1474f4a2713aSLionel Sambuc necessary. */
1475f4a2713aSLionel Sambuc assert(!carry);
1476f4a2713aSLionel Sambuc (void)carry;
1477f4a2713aSLionel Sambuc } else {
1478f4a2713aSLionel Sambuc if (bits > 0) {
1479f4a2713aSLionel Sambuc APFloat temp_rhs(rhs);
1480f4a2713aSLionel Sambuc
1481f4a2713aSLionel Sambuc lost_fraction = temp_rhs.shiftSignificandRight(bits);
1482f4a2713aSLionel Sambuc carry = addSignificand(temp_rhs);
1483f4a2713aSLionel Sambuc } else {
1484f4a2713aSLionel Sambuc lost_fraction = shiftSignificandRight(-bits);
1485f4a2713aSLionel Sambuc carry = addSignificand(rhs);
1486f4a2713aSLionel Sambuc }
1487f4a2713aSLionel Sambuc
1488f4a2713aSLionel Sambuc /* We have a guard bit; generating a carry cannot happen. */
1489f4a2713aSLionel Sambuc assert(!carry);
1490f4a2713aSLionel Sambuc (void)carry;
1491f4a2713aSLionel Sambuc }
1492f4a2713aSLionel Sambuc
1493f4a2713aSLionel Sambuc return lost_fraction;
1494f4a2713aSLionel Sambuc }
1495f4a2713aSLionel Sambuc
1496f4a2713aSLionel Sambuc APFloat::opStatus
multiplySpecials(const APFloat & rhs)1497f4a2713aSLionel Sambuc APFloat::multiplySpecials(const APFloat &rhs)
1498f4a2713aSLionel Sambuc {
1499f4a2713aSLionel Sambuc switch (PackCategoriesIntoKey(category, rhs.category)) {
1500f4a2713aSLionel Sambuc default:
1501*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
1502f4a2713aSLionel Sambuc
1503f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcZero):
1504f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNormal):
1505f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcInfinity):
1506f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNaN):
1507f4a2713aSLionel Sambuc sign = false;
1508f4a2713aSLionel Sambuc return opOK;
1509f4a2713aSLionel Sambuc
1510f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNaN):
1511f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNaN):
1512f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNaN):
1513f4a2713aSLionel Sambuc sign = false;
1514f4a2713aSLionel Sambuc category = fcNaN;
1515f4a2713aSLionel Sambuc copySignificand(rhs);
1516f4a2713aSLionel Sambuc return opOK;
1517f4a2713aSLionel Sambuc
1518f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcInfinity):
1519f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNormal):
1520f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1521f4a2713aSLionel Sambuc category = fcInfinity;
1522f4a2713aSLionel Sambuc return opOK;
1523f4a2713aSLionel Sambuc
1524f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNormal):
1525f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcZero):
1526f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcZero):
1527f4a2713aSLionel Sambuc category = fcZero;
1528f4a2713aSLionel Sambuc return opOK;
1529f4a2713aSLionel Sambuc
1530f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcInfinity):
1531f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcZero):
1532f4a2713aSLionel Sambuc makeNaN();
1533f4a2713aSLionel Sambuc return opInvalidOp;
1534f4a2713aSLionel Sambuc
1535f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNormal):
1536f4a2713aSLionel Sambuc return opOK;
1537f4a2713aSLionel Sambuc }
1538f4a2713aSLionel Sambuc }
1539f4a2713aSLionel Sambuc
1540f4a2713aSLionel Sambuc APFloat::opStatus
divideSpecials(const APFloat & rhs)1541f4a2713aSLionel Sambuc APFloat::divideSpecials(const APFloat &rhs)
1542f4a2713aSLionel Sambuc {
1543f4a2713aSLionel Sambuc switch (PackCategoriesIntoKey(category, rhs.category)) {
1544f4a2713aSLionel Sambuc default:
1545*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
1546f4a2713aSLionel Sambuc
1547f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNaN):
1548f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNaN):
1549f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNaN):
1550f4a2713aSLionel Sambuc category = fcNaN;
1551f4a2713aSLionel Sambuc copySignificand(rhs);
1552f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcZero):
1553f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNormal):
1554f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcInfinity):
1555f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNaN):
1556f4a2713aSLionel Sambuc sign = false;
1557f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcZero):
1558f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNormal):
1559f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcInfinity):
1560f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNormal):
1561f4a2713aSLionel Sambuc return opOK;
1562f4a2713aSLionel Sambuc
1563f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcInfinity):
1564f4a2713aSLionel Sambuc category = fcZero;
1565f4a2713aSLionel Sambuc return opOK;
1566f4a2713aSLionel Sambuc
1567f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcZero):
1568f4a2713aSLionel Sambuc category = fcInfinity;
1569f4a2713aSLionel Sambuc return opDivByZero;
1570f4a2713aSLionel Sambuc
1571f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1572f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcZero):
1573f4a2713aSLionel Sambuc makeNaN();
1574f4a2713aSLionel Sambuc return opInvalidOp;
1575f4a2713aSLionel Sambuc
1576f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNormal):
1577f4a2713aSLionel Sambuc return opOK;
1578f4a2713aSLionel Sambuc }
1579f4a2713aSLionel Sambuc }
1580f4a2713aSLionel Sambuc
1581f4a2713aSLionel Sambuc APFloat::opStatus
modSpecials(const APFloat & rhs)1582f4a2713aSLionel Sambuc APFloat::modSpecials(const APFloat &rhs)
1583f4a2713aSLionel Sambuc {
1584f4a2713aSLionel Sambuc switch (PackCategoriesIntoKey(category, rhs.category)) {
1585f4a2713aSLionel Sambuc default:
1586*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
1587f4a2713aSLionel Sambuc
1588f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcZero):
1589f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNormal):
1590f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcInfinity):
1591f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNaN):
1592f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcInfinity):
1593f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNormal):
1594f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcInfinity):
1595f4a2713aSLionel Sambuc return opOK;
1596f4a2713aSLionel Sambuc
1597f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNaN):
1598f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNaN):
1599f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNaN):
1600f4a2713aSLionel Sambuc sign = false;
1601f4a2713aSLionel Sambuc category = fcNaN;
1602f4a2713aSLionel Sambuc copySignificand(rhs);
1603f4a2713aSLionel Sambuc return opOK;
1604f4a2713aSLionel Sambuc
1605f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcZero):
1606f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcZero):
1607f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNormal):
1608f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1609f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcZero):
1610f4a2713aSLionel Sambuc makeNaN();
1611f4a2713aSLionel Sambuc return opInvalidOp;
1612f4a2713aSLionel Sambuc
1613f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNormal):
1614f4a2713aSLionel Sambuc return opOK;
1615f4a2713aSLionel Sambuc }
1616f4a2713aSLionel Sambuc }
1617f4a2713aSLionel Sambuc
1618f4a2713aSLionel Sambuc /* Change sign. */
1619f4a2713aSLionel Sambuc void
changeSign()1620f4a2713aSLionel Sambuc APFloat::changeSign()
1621f4a2713aSLionel Sambuc {
1622f4a2713aSLionel Sambuc /* Look mummy, this one's easy. */
1623f4a2713aSLionel Sambuc sign = !sign;
1624f4a2713aSLionel Sambuc }
1625f4a2713aSLionel Sambuc
1626f4a2713aSLionel Sambuc void
clearSign()1627f4a2713aSLionel Sambuc APFloat::clearSign()
1628f4a2713aSLionel Sambuc {
1629f4a2713aSLionel Sambuc /* So is this one. */
1630f4a2713aSLionel Sambuc sign = 0;
1631f4a2713aSLionel Sambuc }
1632f4a2713aSLionel Sambuc
1633f4a2713aSLionel Sambuc void
copySign(const APFloat & rhs)1634f4a2713aSLionel Sambuc APFloat::copySign(const APFloat &rhs)
1635f4a2713aSLionel Sambuc {
1636f4a2713aSLionel Sambuc /* And this one. */
1637f4a2713aSLionel Sambuc sign = rhs.sign;
1638f4a2713aSLionel Sambuc }
1639f4a2713aSLionel Sambuc
1640f4a2713aSLionel Sambuc /* Normalized addition or subtraction. */
1641f4a2713aSLionel Sambuc APFloat::opStatus
addOrSubtract(const APFloat & rhs,roundingMode rounding_mode,bool subtract)1642f4a2713aSLionel Sambuc APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
1643f4a2713aSLionel Sambuc bool subtract)
1644f4a2713aSLionel Sambuc {
1645f4a2713aSLionel Sambuc opStatus fs;
1646f4a2713aSLionel Sambuc
1647f4a2713aSLionel Sambuc fs = addOrSubtractSpecials(rhs, subtract);
1648f4a2713aSLionel Sambuc
1649f4a2713aSLionel Sambuc /* This return code means it was not a simple case. */
1650f4a2713aSLionel Sambuc if (fs == opDivByZero) {
1651f4a2713aSLionel Sambuc lostFraction lost_fraction;
1652f4a2713aSLionel Sambuc
1653f4a2713aSLionel Sambuc lost_fraction = addOrSubtractSignificand(rhs, subtract);
1654f4a2713aSLionel Sambuc fs = normalize(rounding_mode, lost_fraction);
1655f4a2713aSLionel Sambuc
1656f4a2713aSLionel Sambuc /* Can only be zero if we lost no fraction. */
1657f4a2713aSLionel Sambuc assert(category != fcZero || lost_fraction == lfExactlyZero);
1658f4a2713aSLionel Sambuc }
1659f4a2713aSLionel Sambuc
1660f4a2713aSLionel Sambuc /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1661f4a2713aSLionel Sambuc positive zero unless rounding to minus infinity, except that
1662f4a2713aSLionel Sambuc adding two like-signed zeroes gives that zero. */
1663f4a2713aSLionel Sambuc if (category == fcZero) {
1664f4a2713aSLionel Sambuc if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
1665f4a2713aSLionel Sambuc sign = (rounding_mode == rmTowardNegative);
1666f4a2713aSLionel Sambuc }
1667f4a2713aSLionel Sambuc
1668f4a2713aSLionel Sambuc return fs;
1669f4a2713aSLionel Sambuc }
1670f4a2713aSLionel Sambuc
1671f4a2713aSLionel Sambuc /* Normalized addition. */
1672f4a2713aSLionel Sambuc APFloat::opStatus
add(const APFloat & rhs,roundingMode rounding_mode)1673f4a2713aSLionel Sambuc APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1674f4a2713aSLionel Sambuc {
1675f4a2713aSLionel Sambuc return addOrSubtract(rhs, rounding_mode, false);
1676f4a2713aSLionel Sambuc }
1677f4a2713aSLionel Sambuc
1678f4a2713aSLionel Sambuc /* Normalized subtraction. */
1679f4a2713aSLionel Sambuc APFloat::opStatus
subtract(const APFloat & rhs,roundingMode rounding_mode)1680f4a2713aSLionel Sambuc APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1681f4a2713aSLionel Sambuc {
1682f4a2713aSLionel Sambuc return addOrSubtract(rhs, rounding_mode, true);
1683f4a2713aSLionel Sambuc }
1684f4a2713aSLionel Sambuc
1685f4a2713aSLionel Sambuc /* Normalized multiply. */
1686f4a2713aSLionel Sambuc APFloat::opStatus
multiply(const APFloat & rhs,roundingMode rounding_mode)1687f4a2713aSLionel Sambuc APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1688f4a2713aSLionel Sambuc {
1689f4a2713aSLionel Sambuc opStatus fs;
1690f4a2713aSLionel Sambuc
1691f4a2713aSLionel Sambuc sign ^= rhs.sign;
1692f4a2713aSLionel Sambuc fs = multiplySpecials(rhs);
1693f4a2713aSLionel Sambuc
1694f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
1695*0a6a1f1dSLionel Sambuc lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
1696f4a2713aSLionel Sambuc fs = normalize(rounding_mode, lost_fraction);
1697f4a2713aSLionel Sambuc if (lost_fraction != lfExactlyZero)
1698f4a2713aSLionel Sambuc fs = (opStatus) (fs | opInexact);
1699f4a2713aSLionel Sambuc }
1700f4a2713aSLionel Sambuc
1701f4a2713aSLionel Sambuc return fs;
1702f4a2713aSLionel Sambuc }
1703f4a2713aSLionel Sambuc
1704f4a2713aSLionel Sambuc /* Normalized divide. */
1705f4a2713aSLionel Sambuc APFloat::opStatus
divide(const APFloat & rhs,roundingMode rounding_mode)1706f4a2713aSLionel Sambuc APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1707f4a2713aSLionel Sambuc {
1708f4a2713aSLionel Sambuc opStatus fs;
1709f4a2713aSLionel Sambuc
1710f4a2713aSLionel Sambuc sign ^= rhs.sign;
1711f4a2713aSLionel Sambuc fs = divideSpecials(rhs);
1712f4a2713aSLionel Sambuc
1713f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
1714f4a2713aSLionel Sambuc lostFraction lost_fraction = divideSignificand(rhs);
1715f4a2713aSLionel Sambuc fs = normalize(rounding_mode, lost_fraction);
1716f4a2713aSLionel Sambuc if (lost_fraction != lfExactlyZero)
1717f4a2713aSLionel Sambuc fs = (opStatus) (fs | opInexact);
1718f4a2713aSLionel Sambuc }
1719f4a2713aSLionel Sambuc
1720f4a2713aSLionel Sambuc return fs;
1721f4a2713aSLionel Sambuc }
1722f4a2713aSLionel Sambuc
1723f4a2713aSLionel Sambuc /* Normalized remainder. This is not currently correct in all cases. */
1724f4a2713aSLionel Sambuc APFloat::opStatus
remainder(const APFloat & rhs)1725f4a2713aSLionel Sambuc APFloat::remainder(const APFloat &rhs)
1726f4a2713aSLionel Sambuc {
1727f4a2713aSLionel Sambuc opStatus fs;
1728f4a2713aSLionel Sambuc APFloat V = *this;
1729f4a2713aSLionel Sambuc unsigned int origSign = sign;
1730f4a2713aSLionel Sambuc
1731f4a2713aSLionel Sambuc fs = V.divide(rhs, rmNearestTiesToEven);
1732f4a2713aSLionel Sambuc if (fs == opDivByZero)
1733f4a2713aSLionel Sambuc return fs;
1734f4a2713aSLionel Sambuc
1735f4a2713aSLionel Sambuc int parts = partCount();
1736f4a2713aSLionel Sambuc integerPart *x = new integerPart[parts];
1737f4a2713aSLionel Sambuc bool ignored;
1738f4a2713aSLionel Sambuc fs = V.convertToInteger(x, parts * integerPartWidth, true,
1739f4a2713aSLionel Sambuc rmNearestTiesToEven, &ignored);
1740f4a2713aSLionel Sambuc if (fs==opInvalidOp)
1741f4a2713aSLionel Sambuc return fs;
1742f4a2713aSLionel Sambuc
1743f4a2713aSLionel Sambuc fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1744f4a2713aSLionel Sambuc rmNearestTiesToEven);
1745f4a2713aSLionel Sambuc assert(fs==opOK); // should always work
1746f4a2713aSLionel Sambuc
1747f4a2713aSLionel Sambuc fs = V.multiply(rhs, rmNearestTiesToEven);
1748f4a2713aSLionel Sambuc assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1749f4a2713aSLionel Sambuc
1750f4a2713aSLionel Sambuc fs = subtract(V, rmNearestTiesToEven);
1751f4a2713aSLionel Sambuc assert(fs==opOK || fs==opInexact); // likewise
1752f4a2713aSLionel Sambuc
1753f4a2713aSLionel Sambuc if (isZero())
1754f4a2713aSLionel Sambuc sign = origSign; // IEEE754 requires this
1755f4a2713aSLionel Sambuc delete[] x;
1756f4a2713aSLionel Sambuc return fs;
1757f4a2713aSLionel Sambuc }
1758f4a2713aSLionel Sambuc
1759f4a2713aSLionel Sambuc /* Normalized llvm frem (C fmod).
1760f4a2713aSLionel Sambuc This is not currently correct in all cases. */
1761f4a2713aSLionel Sambuc APFloat::opStatus
mod(const APFloat & rhs,roundingMode rounding_mode)1762f4a2713aSLionel Sambuc APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1763f4a2713aSLionel Sambuc {
1764f4a2713aSLionel Sambuc opStatus fs;
1765f4a2713aSLionel Sambuc fs = modSpecials(rhs);
1766f4a2713aSLionel Sambuc
1767f4a2713aSLionel Sambuc if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
1768f4a2713aSLionel Sambuc APFloat V = *this;
1769f4a2713aSLionel Sambuc unsigned int origSign = sign;
1770f4a2713aSLionel Sambuc
1771f4a2713aSLionel Sambuc fs = V.divide(rhs, rmNearestTiesToEven);
1772f4a2713aSLionel Sambuc if (fs == opDivByZero)
1773f4a2713aSLionel Sambuc return fs;
1774f4a2713aSLionel Sambuc
1775f4a2713aSLionel Sambuc int parts = partCount();
1776f4a2713aSLionel Sambuc integerPart *x = new integerPart[parts];
1777f4a2713aSLionel Sambuc bool ignored;
1778f4a2713aSLionel Sambuc fs = V.convertToInteger(x, parts * integerPartWidth, true,
1779f4a2713aSLionel Sambuc rmTowardZero, &ignored);
1780f4a2713aSLionel Sambuc if (fs==opInvalidOp)
1781f4a2713aSLionel Sambuc return fs;
1782f4a2713aSLionel Sambuc
1783f4a2713aSLionel Sambuc fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1784f4a2713aSLionel Sambuc rmNearestTiesToEven);
1785f4a2713aSLionel Sambuc assert(fs==opOK); // should always work
1786f4a2713aSLionel Sambuc
1787f4a2713aSLionel Sambuc fs = V.multiply(rhs, rounding_mode);
1788f4a2713aSLionel Sambuc assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1789f4a2713aSLionel Sambuc
1790f4a2713aSLionel Sambuc fs = subtract(V, rounding_mode);
1791f4a2713aSLionel Sambuc assert(fs==opOK || fs==opInexact); // likewise
1792f4a2713aSLionel Sambuc
1793f4a2713aSLionel Sambuc if (isZero())
1794f4a2713aSLionel Sambuc sign = origSign; // IEEE754 requires this
1795f4a2713aSLionel Sambuc delete[] x;
1796f4a2713aSLionel Sambuc }
1797f4a2713aSLionel Sambuc return fs;
1798f4a2713aSLionel Sambuc }
1799f4a2713aSLionel Sambuc
1800f4a2713aSLionel Sambuc /* Normalized fused-multiply-add. */
1801f4a2713aSLionel Sambuc APFloat::opStatus
fusedMultiplyAdd(const APFloat & multiplicand,const APFloat & addend,roundingMode rounding_mode)1802f4a2713aSLionel Sambuc APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
1803f4a2713aSLionel Sambuc const APFloat &addend,
1804f4a2713aSLionel Sambuc roundingMode rounding_mode)
1805f4a2713aSLionel Sambuc {
1806f4a2713aSLionel Sambuc opStatus fs;
1807f4a2713aSLionel Sambuc
1808f4a2713aSLionel Sambuc /* Post-multiplication sign, before addition. */
1809f4a2713aSLionel Sambuc sign ^= multiplicand.sign;
1810f4a2713aSLionel Sambuc
1811f4a2713aSLionel Sambuc /* If and only if all arguments are normal do we need to do an
1812f4a2713aSLionel Sambuc extended-precision calculation. */
1813f4a2713aSLionel Sambuc if (isFiniteNonZero() &&
1814f4a2713aSLionel Sambuc multiplicand.isFiniteNonZero() &&
1815*0a6a1f1dSLionel Sambuc addend.isFinite()) {
1816f4a2713aSLionel Sambuc lostFraction lost_fraction;
1817f4a2713aSLionel Sambuc
1818f4a2713aSLionel Sambuc lost_fraction = multiplySignificand(multiplicand, &addend);
1819f4a2713aSLionel Sambuc fs = normalize(rounding_mode, lost_fraction);
1820f4a2713aSLionel Sambuc if (lost_fraction != lfExactlyZero)
1821f4a2713aSLionel Sambuc fs = (opStatus) (fs | opInexact);
1822f4a2713aSLionel Sambuc
1823f4a2713aSLionel Sambuc /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1824f4a2713aSLionel Sambuc positive zero unless rounding to minus infinity, except that
1825f4a2713aSLionel Sambuc adding two like-signed zeroes gives that zero. */
1826*0a6a1f1dSLionel Sambuc if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
1827f4a2713aSLionel Sambuc sign = (rounding_mode == rmTowardNegative);
1828f4a2713aSLionel Sambuc } else {
1829f4a2713aSLionel Sambuc fs = multiplySpecials(multiplicand);
1830f4a2713aSLionel Sambuc
1831f4a2713aSLionel Sambuc /* FS can only be opOK or opInvalidOp. There is no more work
1832f4a2713aSLionel Sambuc to do in the latter case. The IEEE-754R standard says it is
1833f4a2713aSLionel Sambuc implementation-defined in this case whether, if ADDEND is a
1834f4a2713aSLionel Sambuc quiet NaN, we raise invalid op; this implementation does so.
1835f4a2713aSLionel Sambuc
1836f4a2713aSLionel Sambuc If we need to do the addition we can do so with normal
1837f4a2713aSLionel Sambuc precision. */
1838f4a2713aSLionel Sambuc if (fs == opOK)
1839f4a2713aSLionel Sambuc fs = addOrSubtract(addend, rounding_mode, false);
1840f4a2713aSLionel Sambuc }
1841f4a2713aSLionel Sambuc
1842f4a2713aSLionel Sambuc return fs;
1843f4a2713aSLionel Sambuc }
1844f4a2713aSLionel Sambuc
1845f4a2713aSLionel Sambuc /* Rounding-mode corrrect round to integral value. */
roundToIntegral(roundingMode rounding_mode)1846f4a2713aSLionel Sambuc APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
1847f4a2713aSLionel Sambuc opStatus fs;
1848f4a2713aSLionel Sambuc
1849f4a2713aSLionel Sambuc // If the exponent is large enough, we know that this value is already
1850f4a2713aSLionel Sambuc // integral, and the arithmetic below would potentially cause it to saturate
1851f4a2713aSLionel Sambuc // to +/-Inf. Bail out early instead.
1852f4a2713aSLionel Sambuc if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
1853f4a2713aSLionel Sambuc return opOK;
1854f4a2713aSLionel Sambuc
1855f4a2713aSLionel Sambuc // The algorithm here is quite simple: we add 2^(p-1), where p is the
1856f4a2713aSLionel Sambuc // precision of our format, and then subtract it back off again. The choice
1857f4a2713aSLionel Sambuc // of rounding modes for the addition/subtraction determines the rounding mode
1858f4a2713aSLionel Sambuc // for our integral rounding as well.
1859f4a2713aSLionel Sambuc // NOTE: When the input value is negative, we do subtraction followed by
1860f4a2713aSLionel Sambuc // addition instead.
1861f4a2713aSLionel Sambuc APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1862f4a2713aSLionel Sambuc IntegerConstant <<= semanticsPrecision(*semantics)-1;
1863f4a2713aSLionel Sambuc APFloat MagicConstant(*semantics);
1864f4a2713aSLionel Sambuc fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1865f4a2713aSLionel Sambuc rmNearestTiesToEven);
1866f4a2713aSLionel Sambuc MagicConstant.copySign(*this);
1867f4a2713aSLionel Sambuc
1868f4a2713aSLionel Sambuc if (fs != opOK)
1869f4a2713aSLionel Sambuc return fs;
1870f4a2713aSLionel Sambuc
1871f4a2713aSLionel Sambuc // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1872f4a2713aSLionel Sambuc bool inputSign = isNegative();
1873f4a2713aSLionel Sambuc
1874f4a2713aSLionel Sambuc fs = add(MagicConstant, rounding_mode);
1875f4a2713aSLionel Sambuc if (fs != opOK && fs != opInexact)
1876f4a2713aSLionel Sambuc return fs;
1877f4a2713aSLionel Sambuc
1878f4a2713aSLionel Sambuc fs = subtract(MagicConstant, rounding_mode);
1879f4a2713aSLionel Sambuc
1880f4a2713aSLionel Sambuc // Restore the input sign.
1881f4a2713aSLionel Sambuc if (inputSign != isNegative())
1882f4a2713aSLionel Sambuc changeSign();
1883f4a2713aSLionel Sambuc
1884f4a2713aSLionel Sambuc return fs;
1885f4a2713aSLionel Sambuc }
1886f4a2713aSLionel Sambuc
1887f4a2713aSLionel Sambuc
1888f4a2713aSLionel Sambuc /* Comparison requires normalized numbers. */
1889f4a2713aSLionel Sambuc APFloat::cmpResult
compare(const APFloat & rhs) const1890f4a2713aSLionel Sambuc APFloat::compare(const APFloat &rhs) const
1891f4a2713aSLionel Sambuc {
1892f4a2713aSLionel Sambuc cmpResult result;
1893f4a2713aSLionel Sambuc
1894f4a2713aSLionel Sambuc assert(semantics == rhs.semantics);
1895f4a2713aSLionel Sambuc
1896f4a2713aSLionel Sambuc switch (PackCategoriesIntoKey(category, rhs.category)) {
1897f4a2713aSLionel Sambuc default:
1898*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
1899f4a2713aSLionel Sambuc
1900f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcZero):
1901f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNormal):
1902f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcInfinity):
1903f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNaN, fcNaN):
1904f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNaN):
1905f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNaN):
1906f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNaN):
1907f4a2713aSLionel Sambuc return cmpUnordered;
1908f4a2713aSLionel Sambuc
1909f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcNormal):
1910f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcZero):
1911f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcZero):
1912f4a2713aSLionel Sambuc if (sign)
1913f4a2713aSLionel Sambuc return cmpLessThan;
1914f4a2713aSLionel Sambuc else
1915f4a2713aSLionel Sambuc return cmpGreaterThan;
1916f4a2713aSLionel Sambuc
1917f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcInfinity):
1918f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcInfinity):
1919f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcNormal):
1920f4a2713aSLionel Sambuc if (rhs.sign)
1921f4a2713aSLionel Sambuc return cmpGreaterThan;
1922f4a2713aSLionel Sambuc else
1923f4a2713aSLionel Sambuc return cmpLessThan;
1924f4a2713aSLionel Sambuc
1925f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1926f4a2713aSLionel Sambuc if (sign == rhs.sign)
1927f4a2713aSLionel Sambuc return cmpEqual;
1928f4a2713aSLionel Sambuc else if (sign)
1929f4a2713aSLionel Sambuc return cmpLessThan;
1930f4a2713aSLionel Sambuc else
1931f4a2713aSLionel Sambuc return cmpGreaterThan;
1932f4a2713aSLionel Sambuc
1933f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcZero, fcZero):
1934f4a2713aSLionel Sambuc return cmpEqual;
1935f4a2713aSLionel Sambuc
1936f4a2713aSLionel Sambuc case PackCategoriesIntoKey(fcNormal, fcNormal):
1937f4a2713aSLionel Sambuc break;
1938f4a2713aSLionel Sambuc }
1939f4a2713aSLionel Sambuc
1940f4a2713aSLionel Sambuc /* Two normal numbers. Do they have the same sign? */
1941f4a2713aSLionel Sambuc if (sign != rhs.sign) {
1942f4a2713aSLionel Sambuc if (sign)
1943f4a2713aSLionel Sambuc result = cmpLessThan;
1944f4a2713aSLionel Sambuc else
1945f4a2713aSLionel Sambuc result = cmpGreaterThan;
1946f4a2713aSLionel Sambuc } else {
1947f4a2713aSLionel Sambuc /* Compare absolute values; invert result if negative. */
1948f4a2713aSLionel Sambuc result = compareAbsoluteValue(rhs);
1949f4a2713aSLionel Sambuc
1950f4a2713aSLionel Sambuc if (sign) {
1951f4a2713aSLionel Sambuc if (result == cmpLessThan)
1952f4a2713aSLionel Sambuc result = cmpGreaterThan;
1953f4a2713aSLionel Sambuc else if (result == cmpGreaterThan)
1954f4a2713aSLionel Sambuc result = cmpLessThan;
1955f4a2713aSLionel Sambuc }
1956f4a2713aSLionel Sambuc }
1957f4a2713aSLionel Sambuc
1958f4a2713aSLionel Sambuc return result;
1959f4a2713aSLionel Sambuc }
1960f4a2713aSLionel Sambuc
1961f4a2713aSLionel Sambuc /// APFloat::convert - convert a value of one floating point type to another.
1962f4a2713aSLionel Sambuc /// The return value corresponds to the IEEE754 exceptions. *losesInfo
1963f4a2713aSLionel Sambuc /// records whether the transformation lost information, i.e. whether
1964f4a2713aSLionel Sambuc /// converting the result back to the original type will produce the
1965f4a2713aSLionel Sambuc /// original value (this is almost the same as return value==fsOK, but there
1966f4a2713aSLionel Sambuc /// are edge cases where this is not so).
1967f4a2713aSLionel Sambuc
1968f4a2713aSLionel Sambuc APFloat::opStatus
convert(const fltSemantics & toSemantics,roundingMode rounding_mode,bool * losesInfo)1969f4a2713aSLionel Sambuc APFloat::convert(const fltSemantics &toSemantics,
1970f4a2713aSLionel Sambuc roundingMode rounding_mode, bool *losesInfo)
1971f4a2713aSLionel Sambuc {
1972f4a2713aSLionel Sambuc lostFraction lostFraction;
1973f4a2713aSLionel Sambuc unsigned int newPartCount, oldPartCount;
1974f4a2713aSLionel Sambuc opStatus fs;
1975f4a2713aSLionel Sambuc int shift;
1976f4a2713aSLionel Sambuc const fltSemantics &fromSemantics = *semantics;
1977f4a2713aSLionel Sambuc
1978f4a2713aSLionel Sambuc lostFraction = lfExactlyZero;
1979f4a2713aSLionel Sambuc newPartCount = partCountForBits(toSemantics.precision + 1);
1980f4a2713aSLionel Sambuc oldPartCount = partCount();
1981f4a2713aSLionel Sambuc shift = toSemantics.precision - fromSemantics.precision;
1982f4a2713aSLionel Sambuc
1983f4a2713aSLionel Sambuc bool X86SpecialNan = false;
1984f4a2713aSLionel Sambuc if (&fromSemantics == &APFloat::x87DoubleExtended &&
1985f4a2713aSLionel Sambuc &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN &&
1986f4a2713aSLionel Sambuc (!(*significandParts() & 0x8000000000000000ULL) ||
1987f4a2713aSLionel Sambuc !(*significandParts() & 0x4000000000000000ULL))) {
1988f4a2713aSLionel Sambuc // x86 has some unusual NaNs which cannot be represented in any other
1989f4a2713aSLionel Sambuc // format; note them here.
1990f4a2713aSLionel Sambuc X86SpecialNan = true;
1991f4a2713aSLionel Sambuc }
1992f4a2713aSLionel Sambuc
1993f4a2713aSLionel Sambuc // If this is a truncation of a denormal number, and the target semantics
1994f4a2713aSLionel Sambuc // has larger exponent range than the source semantics (this can happen
1995f4a2713aSLionel Sambuc // when truncating from PowerPC double-double to double format), the
1996f4a2713aSLionel Sambuc // right shift could lose result mantissa bits. Adjust exponent instead
1997f4a2713aSLionel Sambuc // of performing excessive shift.
1998f4a2713aSLionel Sambuc if (shift < 0 && isFiniteNonZero()) {
1999f4a2713aSLionel Sambuc int exponentChange = significandMSB() + 1 - fromSemantics.precision;
2000f4a2713aSLionel Sambuc if (exponent + exponentChange < toSemantics.minExponent)
2001f4a2713aSLionel Sambuc exponentChange = toSemantics.minExponent - exponent;
2002f4a2713aSLionel Sambuc if (exponentChange < shift)
2003f4a2713aSLionel Sambuc exponentChange = shift;
2004f4a2713aSLionel Sambuc if (exponentChange < 0) {
2005f4a2713aSLionel Sambuc shift -= exponentChange;
2006f4a2713aSLionel Sambuc exponent += exponentChange;
2007f4a2713aSLionel Sambuc }
2008f4a2713aSLionel Sambuc }
2009f4a2713aSLionel Sambuc
2010f4a2713aSLionel Sambuc // If this is a truncation, perform the shift before we narrow the storage.
2011f4a2713aSLionel Sambuc if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
2012f4a2713aSLionel Sambuc lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
2013f4a2713aSLionel Sambuc
2014f4a2713aSLionel Sambuc // Fix the storage so it can hold to new value.
2015f4a2713aSLionel Sambuc if (newPartCount > oldPartCount) {
2016f4a2713aSLionel Sambuc // The new type requires more storage; make it available.
2017f4a2713aSLionel Sambuc integerPart *newParts;
2018f4a2713aSLionel Sambuc newParts = new integerPart[newPartCount];
2019f4a2713aSLionel Sambuc APInt::tcSet(newParts, 0, newPartCount);
2020f4a2713aSLionel Sambuc if (isFiniteNonZero() || category==fcNaN)
2021f4a2713aSLionel Sambuc APInt::tcAssign(newParts, significandParts(), oldPartCount);
2022f4a2713aSLionel Sambuc freeSignificand();
2023f4a2713aSLionel Sambuc significand.parts = newParts;
2024f4a2713aSLionel Sambuc } else if (newPartCount == 1 && oldPartCount != 1) {
2025f4a2713aSLionel Sambuc // Switch to built-in storage for a single part.
2026f4a2713aSLionel Sambuc integerPart newPart = 0;
2027f4a2713aSLionel Sambuc if (isFiniteNonZero() || category==fcNaN)
2028f4a2713aSLionel Sambuc newPart = significandParts()[0];
2029f4a2713aSLionel Sambuc freeSignificand();
2030f4a2713aSLionel Sambuc significand.part = newPart;
2031f4a2713aSLionel Sambuc }
2032f4a2713aSLionel Sambuc
2033f4a2713aSLionel Sambuc // Now that we have the right storage, switch the semantics.
2034f4a2713aSLionel Sambuc semantics = &toSemantics;
2035f4a2713aSLionel Sambuc
2036f4a2713aSLionel Sambuc // If this is an extension, perform the shift now that the storage is
2037f4a2713aSLionel Sambuc // available.
2038f4a2713aSLionel Sambuc if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
2039f4a2713aSLionel Sambuc APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2040f4a2713aSLionel Sambuc
2041f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
2042f4a2713aSLionel Sambuc fs = normalize(rounding_mode, lostFraction);
2043f4a2713aSLionel Sambuc *losesInfo = (fs != opOK);
2044f4a2713aSLionel Sambuc } else if (category == fcNaN) {
2045f4a2713aSLionel Sambuc *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
2046f4a2713aSLionel Sambuc
2047f4a2713aSLionel Sambuc // For x87 extended precision, we want to make a NaN, not a special NaN if
2048f4a2713aSLionel Sambuc // the input wasn't special either.
2049f4a2713aSLionel Sambuc if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
2050f4a2713aSLionel Sambuc APInt::tcSetBit(significandParts(), semantics->precision - 1);
2051f4a2713aSLionel Sambuc
2052f4a2713aSLionel Sambuc // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2053f4a2713aSLionel Sambuc // does not give you back the same bits. This is dubious, and we
2054f4a2713aSLionel Sambuc // don't currently do it. You're really supposed to get
2055f4a2713aSLionel Sambuc // an invalid operation signal at runtime, but nobody does that.
2056f4a2713aSLionel Sambuc fs = opOK;
2057f4a2713aSLionel Sambuc } else {
2058f4a2713aSLionel Sambuc *losesInfo = false;
2059f4a2713aSLionel Sambuc fs = opOK;
2060f4a2713aSLionel Sambuc }
2061f4a2713aSLionel Sambuc
2062f4a2713aSLionel Sambuc return fs;
2063f4a2713aSLionel Sambuc }
2064f4a2713aSLionel Sambuc
2065f4a2713aSLionel Sambuc /* Convert a floating point number to an integer according to the
2066f4a2713aSLionel Sambuc rounding mode. If the rounded integer value is out of range this
2067f4a2713aSLionel Sambuc returns an invalid operation exception and the contents of the
2068f4a2713aSLionel Sambuc destination parts are unspecified. If the rounded value is in
2069f4a2713aSLionel Sambuc range but the floating point number is not the exact integer, the C
2070f4a2713aSLionel Sambuc standard doesn't require an inexact exception to be raised. IEEE
2071f4a2713aSLionel Sambuc 854 does require it so we do that.
2072f4a2713aSLionel Sambuc
2073f4a2713aSLionel Sambuc Note that for conversions to integer type the C standard requires
2074f4a2713aSLionel Sambuc round-to-zero to always be used. */
2075f4a2713aSLionel Sambuc APFloat::opStatus
convertToSignExtendedInteger(integerPart * parts,unsigned int width,bool isSigned,roundingMode rounding_mode,bool * isExact) const2076f4a2713aSLionel Sambuc APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
2077f4a2713aSLionel Sambuc bool isSigned,
2078f4a2713aSLionel Sambuc roundingMode rounding_mode,
2079f4a2713aSLionel Sambuc bool *isExact) const
2080f4a2713aSLionel Sambuc {
2081f4a2713aSLionel Sambuc lostFraction lost_fraction;
2082f4a2713aSLionel Sambuc const integerPart *src;
2083f4a2713aSLionel Sambuc unsigned int dstPartsCount, truncatedBits;
2084f4a2713aSLionel Sambuc
2085f4a2713aSLionel Sambuc *isExact = false;
2086f4a2713aSLionel Sambuc
2087f4a2713aSLionel Sambuc /* Handle the three special cases first. */
2088f4a2713aSLionel Sambuc if (category == fcInfinity || category == fcNaN)
2089f4a2713aSLionel Sambuc return opInvalidOp;
2090f4a2713aSLionel Sambuc
2091f4a2713aSLionel Sambuc dstPartsCount = partCountForBits(width);
2092f4a2713aSLionel Sambuc
2093f4a2713aSLionel Sambuc if (category == fcZero) {
2094f4a2713aSLionel Sambuc APInt::tcSet(parts, 0, dstPartsCount);
2095f4a2713aSLionel Sambuc // Negative zero can't be represented as an int.
2096f4a2713aSLionel Sambuc *isExact = !sign;
2097f4a2713aSLionel Sambuc return opOK;
2098f4a2713aSLionel Sambuc }
2099f4a2713aSLionel Sambuc
2100f4a2713aSLionel Sambuc src = significandParts();
2101f4a2713aSLionel Sambuc
2102f4a2713aSLionel Sambuc /* Step 1: place our absolute value, with any fraction truncated, in
2103f4a2713aSLionel Sambuc the destination. */
2104f4a2713aSLionel Sambuc if (exponent < 0) {
2105f4a2713aSLionel Sambuc /* Our absolute value is less than one; truncate everything. */
2106f4a2713aSLionel Sambuc APInt::tcSet(parts, 0, dstPartsCount);
2107f4a2713aSLionel Sambuc /* For exponent -1 the integer bit represents .5, look at that.
2108f4a2713aSLionel Sambuc For smaller exponents leftmost truncated bit is 0. */
2109f4a2713aSLionel Sambuc truncatedBits = semantics->precision -1U - exponent;
2110f4a2713aSLionel Sambuc } else {
2111f4a2713aSLionel Sambuc /* We want the most significant (exponent + 1) bits; the rest are
2112f4a2713aSLionel Sambuc truncated. */
2113f4a2713aSLionel Sambuc unsigned int bits = exponent + 1U;
2114f4a2713aSLionel Sambuc
2115f4a2713aSLionel Sambuc /* Hopelessly large in magnitude? */
2116f4a2713aSLionel Sambuc if (bits > width)
2117f4a2713aSLionel Sambuc return opInvalidOp;
2118f4a2713aSLionel Sambuc
2119f4a2713aSLionel Sambuc if (bits < semantics->precision) {
2120f4a2713aSLionel Sambuc /* We truncate (semantics->precision - bits) bits. */
2121f4a2713aSLionel Sambuc truncatedBits = semantics->precision - bits;
2122f4a2713aSLionel Sambuc APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
2123f4a2713aSLionel Sambuc } else {
2124f4a2713aSLionel Sambuc /* We want at least as many bits as are available. */
2125f4a2713aSLionel Sambuc APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
2126f4a2713aSLionel Sambuc APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
2127f4a2713aSLionel Sambuc truncatedBits = 0;
2128f4a2713aSLionel Sambuc }
2129f4a2713aSLionel Sambuc }
2130f4a2713aSLionel Sambuc
2131f4a2713aSLionel Sambuc /* Step 2: work out any lost fraction, and increment the absolute
2132f4a2713aSLionel Sambuc value if we would round away from zero. */
2133f4a2713aSLionel Sambuc if (truncatedBits) {
2134f4a2713aSLionel Sambuc lost_fraction = lostFractionThroughTruncation(src, partCount(),
2135f4a2713aSLionel Sambuc truncatedBits);
2136f4a2713aSLionel Sambuc if (lost_fraction != lfExactlyZero &&
2137f4a2713aSLionel Sambuc roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2138f4a2713aSLionel Sambuc if (APInt::tcIncrement(parts, dstPartsCount))
2139f4a2713aSLionel Sambuc return opInvalidOp; /* Overflow. */
2140f4a2713aSLionel Sambuc }
2141f4a2713aSLionel Sambuc } else {
2142f4a2713aSLionel Sambuc lost_fraction = lfExactlyZero;
2143f4a2713aSLionel Sambuc }
2144f4a2713aSLionel Sambuc
2145f4a2713aSLionel Sambuc /* Step 3: check if we fit in the destination. */
2146f4a2713aSLionel Sambuc unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
2147f4a2713aSLionel Sambuc
2148f4a2713aSLionel Sambuc if (sign) {
2149f4a2713aSLionel Sambuc if (!isSigned) {
2150f4a2713aSLionel Sambuc /* Negative numbers cannot be represented as unsigned. */
2151f4a2713aSLionel Sambuc if (omsb != 0)
2152f4a2713aSLionel Sambuc return opInvalidOp;
2153f4a2713aSLionel Sambuc } else {
2154f4a2713aSLionel Sambuc /* It takes omsb bits to represent the unsigned integer value.
2155f4a2713aSLionel Sambuc We lose a bit for the sign, but care is needed as the
2156f4a2713aSLionel Sambuc maximally negative integer is a special case. */
2157f4a2713aSLionel Sambuc if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2158f4a2713aSLionel Sambuc return opInvalidOp;
2159f4a2713aSLionel Sambuc
2160f4a2713aSLionel Sambuc /* This case can happen because of rounding. */
2161f4a2713aSLionel Sambuc if (omsb > width)
2162f4a2713aSLionel Sambuc return opInvalidOp;
2163f4a2713aSLionel Sambuc }
2164f4a2713aSLionel Sambuc
2165f4a2713aSLionel Sambuc APInt::tcNegate (parts, dstPartsCount);
2166f4a2713aSLionel Sambuc } else {
2167f4a2713aSLionel Sambuc if (omsb >= width + !isSigned)
2168f4a2713aSLionel Sambuc return opInvalidOp;
2169f4a2713aSLionel Sambuc }
2170f4a2713aSLionel Sambuc
2171f4a2713aSLionel Sambuc if (lost_fraction == lfExactlyZero) {
2172f4a2713aSLionel Sambuc *isExact = true;
2173f4a2713aSLionel Sambuc return opOK;
2174f4a2713aSLionel Sambuc } else
2175f4a2713aSLionel Sambuc return opInexact;
2176f4a2713aSLionel Sambuc }
2177f4a2713aSLionel Sambuc
2178f4a2713aSLionel Sambuc /* Same as convertToSignExtendedInteger, except we provide
2179f4a2713aSLionel Sambuc deterministic values in case of an invalid operation exception,
2180f4a2713aSLionel Sambuc namely zero for NaNs and the minimal or maximal value respectively
2181f4a2713aSLionel Sambuc for underflow or overflow.
2182f4a2713aSLionel Sambuc The *isExact output tells whether the result is exact, in the sense
2183f4a2713aSLionel Sambuc that converting it back to the original floating point type produces
2184f4a2713aSLionel Sambuc the original value. This is almost equivalent to result==opOK,
2185f4a2713aSLionel Sambuc except for negative zeroes.
2186f4a2713aSLionel Sambuc */
2187f4a2713aSLionel Sambuc APFloat::opStatus
convertToInteger(integerPart * parts,unsigned int width,bool isSigned,roundingMode rounding_mode,bool * isExact) const2188f4a2713aSLionel Sambuc APFloat::convertToInteger(integerPart *parts, unsigned int width,
2189f4a2713aSLionel Sambuc bool isSigned,
2190f4a2713aSLionel Sambuc roundingMode rounding_mode, bool *isExact) const
2191f4a2713aSLionel Sambuc {
2192f4a2713aSLionel Sambuc opStatus fs;
2193f4a2713aSLionel Sambuc
2194f4a2713aSLionel Sambuc fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2195f4a2713aSLionel Sambuc isExact);
2196f4a2713aSLionel Sambuc
2197f4a2713aSLionel Sambuc if (fs == opInvalidOp) {
2198f4a2713aSLionel Sambuc unsigned int bits, dstPartsCount;
2199f4a2713aSLionel Sambuc
2200f4a2713aSLionel Sambuc dstPartsCount = partCountForBits(width);
2201f4a2713aSLionel Sambuc
2202f4a2713aSLionel Sambuc if (category == fcNaN)
2203f4a2713aSLionel Sambuc bits = 0;
2204f4a2713aSLionel Sambuc else if (sign)
2205f4a2713aSLionel Sambuc bits = isSigned;
2206f4a2713aSLionel Sambuc else
2207f4a2713aSLionel Sambuc bits = width - isSigned;
2208f4a2713aSLionel Sambuc
2209f4a2713aSLionel Sambuc APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2210f4a2713aSLionel Sambuc if (sign && isSigned)
2211f4a2713aSLionel Sambuc APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
2212f4a2713aSLionel Sambuc }
2213f4a2713aSLionel Sambuc
2214f4a2713aSLionel Sambuc return fs;
2215f4a2713aSLionel Sambuc }
2216f4a2713aSLionel Sambuc
2217f4a2713aSLionel Sambuc /* Same as convertToInteger(integerPart*, ...), except the result is returned in
2218f4a2713aSLionel Sambuc an APSInt, whose initial bit-width and signed-ness are used to determine the
2219f4a2713aSLionel Sambuc precision of the conversion.
2220f4a2713aSLionel Sambuc */
2221f4a2713aSLionel Sambuc APFloat::opStatus
convertToInteger(APSInt & result,roundingMode rounding_mode,bool * isExact) const2222f4a2713aSLionel Sambuc APFloat::convertToInteger(APSInt &result,
2223f4a2713aSLionel Sambuc roundingMode rounding_mode, bool *isExact) const
2224f4a2713aSLionel Sambuc {
2225f4a2713aSLionel Sambuc unsigned bitWidth = result.getBitWidth();
2226f4a2713aSLionel Sambuc SmallVector<uint64_t, 4> parts(result.getNumWords());
2227f4a2713aSLionel Sambuc opStatus status = convertToInteger(
2228f4a2713aSLionel Sambuc parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
2229f4a2713aSLionel Sambuc // Keeps the original signed-ness.
2230f4a2713aSLionel Sambuc result = APInt(bitWidth, parts);
2231f4a2713aSLionel Sambuc return status;
2232f4a2713aSLionel Sambuc }
2233f4a2713aSLionel Sambuc
2234f4a2713aSLionel Sambuc /* Convert an unsigned integer SRC to a floating point number,
2235f4a2713aSLionel Sambuc rounding according to ROUNDING_MODE. The sign of the floating
2236f4a2713aSLionel Sambuc point number is not modified. */
2237f4a2713aSLionel Sambuc APFloat::opStatus
convertFromUnsignedParts(const integerPart * src,unsigned int srcCount,roundingMode rounding_mode)2238f4a2713aSLionel Sambuc APFloat::convertFromUnsignedParts(const integerPart *src,
2239f4a2713aSLionel Sambuc unsigned int srcCount,
2240f4a2713aSLionel Sambuc roundingMode rounding_mode)
2241f4a2713aSLionel Sambuc {
2242f4a2713aSLionel Sambuc unsigned int omsb, precision, dstCount;
2243f4a2713aSLionel Sambuc integerPart *dst;
2244f4a2713aSLionel Sambuc lostFraction lost_fraction;
2245f4a2713aSLionel Sambuc
2246f4a2713aSLionel Sambuc category = fcNormal;
2247f4a2713aSLionel Sambuc omsb = APInt::tcMSB(src, srcCount) + 1;
2248f4a2713aSLionel Sambuc dst = significandParts();
2249f4a2713aSLionel Sambuc dstCount = partCount();
2250f4a2713aSLionel Sambuc precision = semantics->precision;
2251f4a2713aSLionel Sambuc
2252f4a2713aSLionel Sambuc /* We want the most significant PRECISION bits of SRC. There may not
2253f4a2713aSLionel Sambuc be that many; extract what we can. */
2254f4a2713aSLionel Sambuc if (precision <= omsb) {
2255f4a2713aSLionel Sambuc exponent = omsb - 1;
2256f4a2713aSLionel Sambuc lost_fraction = lostFractionThroughTruncation(src, srcCount,
2257f4a2713aSLionel Sambuc omsb - precision);
2258f4a2713aSLionel Sambuc APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2259f4a2713aSLionel Sambuc } else {
2260f4a2713aSLionel Sambuc exponent = precision - 1;
2261f4a2713aSLionel Sambuc lost_fraction = lfExactlyZero;
2262f4a2713aSLionel Sambuc APInt::tcExtract(dst, dstCount, src, omsb, 0);
2263f4a2713aSLionel Sambuc }
2264f4a2713aSLionel Sambuc
2265f4a2713aSLionel Sambuc return normalize(rounding_mode, lost_fraction);
2266f4a2713aSLionel Sambuc }
2267f4a2713aSLionel Sambuc
2268f4a2713aSLionel Sambuc APFloat::opStatus
convertFromAPInt(const APInt & Val,bool isSigned,roundingMode rounding_mode)2269f4a2713aSLionel Sambuc APFloat::convertFromAPInt(const APInt &Val,
2270f4a2713aSLionel Sambuc bool isSigned,
2271f4a2713aSLionel Sambuc roundingMode rounding_mode)
2272f4a2713aSLionel Sambuc {
2273f4a2713aSLionel Sambuc unsigned int partCount = Val.getNumWords();
2274f4a2713aSLionel Sambuc APInt api = Val;
2275f4a2713aSLionel Sambuc
2276f4a2713aSLionel Sambuc sign = false;
2277f4a2713aSLionel Sambuc if (isSigned && api.isNegative()) {
2278f4a2713aSLionel Sambuc sign = true;
2279f4a2713aSLionel Sambuc api = -api;
2280f4a2713aSLionel Sambuc }
2281f4a2713aSLionel Sambuc
2282f4a2713aSLionel Sambuc return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2283f4a2713aSLionel Sambuc }
2284f4a2713aSLionel Sambuc
2285f4a2713aSLionel Sambuc /* Convert a two's complement integer SRC to a floating point number,
2286f4a2713aSLionel Sambuc rounding according to ROUNDING_MODE. ISSIGNED is true if the
2287f4a2713aSLionel Sambuc integer is signed, in which case it must be sign-extended. */
2288f4a2713aSLionel Sambuc APFloat::opStatus
convertFromSignExtendedInteger(const integerPart * src,unsigned int srcCount,bool isSigned,roundingMode rounding_mode)2289f4a2713aSLionel Sambuc APFloat::convertFromSignExtendedInteger(const integerPart *src,
2290f4a2713aSLionel Sambuc unsigned int srcCount,
2291f4a2713aSLionel Sambuc bool isSigned,
2292f4a2713aSLionel Sambuc roundingMode rounding_mode)
2293f4a2713aSLionel Sambuc {
2294f4a2713aSLionel Sambuc opStatus status;
2295f4a2713aSLionel Sambuc
2296f4a2713aSLionel Sambuc if (isSigned &&
2297f4a2713aSLionel Sambuc APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2298f4a2713aSLionel Sambuc integerPart *copy;
2299f4a2713aSLionel Sambuc
2300f4a2713aSLionel Sambuc /* If we're signed and negative negate a copy. */
2301f4a2713aSLionel Sambuc sign = true;
2302f4a2713aSLionel Sambuc copy = new integerPart[srcCount];
2303f4a2713aSLionel Sambuc APInt::tcAssign(copy, src, srcCount);
2304f4a2713aSLionel Sambuc APInt::tcNegate(copy, srcCount);
2305f4a2713aSLionel Sambuc status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2306f4a2713aSLionel Sambuc delete [] copy;
2307f4a2713aSLionel Sambuc } else {
2308f4a2713aSLionel Sambuc sign = false;
2309f4a2713aSLionel Sambuc status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2310f4a2713aSLionel Sambuc }
2311f4a2713aSLionel Sambuc
2312f4a2713aSLionel Sambuc return status;
2313f4a2713aSLionel Sambuc }
2314f4a2713aSLionel Sambuc
2315f4a2713aSLionel Sambuc /* FIXME: should this just take a const APInt reference? */
2316f4a2713aSLionel Sambuc APFloat::opStatus
convertFromZeroExtendedInteger(const integerPart * parts,unsigned int width,bool isSigned,roundingMode rounding_mode)2317f4a2713aSLionel Sambuc APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2318f4a2713aSLionel Sambuc unsigned int width, bool isSigned,
2319f4a2713aSLionel Sambuc roundingMode rounding_mode)
2320f4a2713aSLionel Sambuc {
2321f4a2713aSLionel Sambuc unsigned int partCount = partCountForBits(width);
2322f4a2713aSLionel Sambuc APInt api = APInt(width, makeArrayRef(parts, partCount));
2323f4a2713aSLionel Sambuc
2324f4a2713aSLionel Sambuc sign = false;
2325f4a2713aSLionel Sambuc if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
2326f4a2713aSLionel Sambuc sign = true;
2327f4a2713aSLionel Sambuc api = -api;
2328f4a2713aSLionel Sambuc }
2329f4a2713aSLionel Sambuc
2330f4a2713aSLionel Sambuc return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2331f4a2713aSLionel Sambuc }
2332f4a2713aSLionel Sambuc
2333f4a2713aSLionel Sambuc APFloat::opStatus
convertFromHexadecimalString(StringRef s,roundingMode rounding_mode)2334f4a2713aSLionel Sambuc APFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode)
2335f4a2713aSLionel Sambuc {
2336f4a2713aSLionel Sambuc lostFraction lost_fraction = lfExactlyZero;
2337f4a2713aSLionel Sambuc
2338f4a2713aSLionel Sambuc category = fcNormal;
2339f4a2713aSLionel Sambuc zeroSignificand();
2340f4a2713aSLionel Sambuc exponent = 0;
2341f4a2713aSLionel Sambuc
2342f4a2713aSLionel Sambuc integerPart *significand = significandParts();
2343f4a2713aSLionel Sambuc unsigned partsCount = partCount();
2344f4a2713aSLionel Sambuc unsigned bitPos = partsCount * integerPartWidth;
2345f4a2713aSLionel Sambuc bool computedTrailingFraction = false;
2346f4a2713aSLionel Sambuc
2347f4a2713aSLionel Sambuc // Skip leading zeroes and any (hexa)decimal point.
2348f4a2713aSLionel Sambuc StringRef::iterator begin = s.begin();
2349f4a2713aSLionel Sambuc StringRef::iterator end = s.end();
2350f4a2713aSLionel Sambuc StringRef::iterator dot;
2351f4a2713aSLionel Sambuc StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
2352f4a2713aSLionel Sambuc StringRef::iterator firstSignificantDigit = p;
2353f4a2713aSLionel Sambuc
2354f4a2713aSLionel Sambuc while (p != end) {
2355f4a2713aSLionel Sambuc integerPart hex_value;
2356f4a2713aSLionel Sambuc
2357f4a2713aSLionel Sambuc if (*p == '.') {
2358f4a2713aSLionel Sambuc assert(dot == end && "String contains multiple dots");
2359f4a2713aSLionel Sambuc dot = p++;
2360f4a2713aSLionel Sambuc continue;
2361f4a2713aSLionel Sambuc }
2362f4a2713aSLionel Sambuc
2363f4a2713aSLionel Sambuc hex_value = hexDigitValue(*p);
2364f4a2713aSLionel Sambuc if (hex_value == -1U)
2365f4a2713aSLionel Sambuc break;
2366f4a2713aSLionel Sambuc
2367f4a2713aSLionel Sambuc p++;
2368f4a2713aSLionel Sambuc
2369f4a2713aSLionel Sambuc // Store the number while we have space.
2370f4a2713aSLionel Sambuc if (bitPos) {
2371f4a2713aSLionel Sambuc bitPos -= 4;
2372f4a2713aSLionel Sambuc hex_value <<= bitPos % integerPartWidth;
2373f4a2713aSLionel Sambuc significand[bitPos / integerPartWidth] |= hex_value;
2374f4a2713aSLionel Sambuc } else if (!computedTrailingFraction) {
2375f4a2713aSLionel Sambuc lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2376f4a2713aSLionel Sambuc computedTrailingFraction = true;
2377f4a2713aSLionel Sambuc }
2378f4a2713aSLionel Sambuc }
2379f4a2713aSLionel Sambuc
2380f4a2713aSLionel Sambuc /* Hex floats require an exponent but not a hexadecimal point. */
2381f4a2713aSLionel Sambuc assert(p != end && "Hex strings require an exponent");
2382f4a2713aSLionel Sambuc assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2383f4a2713aSLionel Sambuc assert(p != begin && "Significand has no digits");
2384f4a2713aSLionel Sambuc assert((dot == end || p - begin != 1) && "Significand has no digits");
2385f4a2713aSLionel Sambuc
2386f4a2713aSLionel Sambuc /* Ignore the exponent if we are zero. */
2387f4a2713aSLionel Sambuc if (p != firstSignificantDigit) {
2388f4a2713aSLionel Sambuc int expAdjustment;
2389f4a2713aSLionel Sambuc
2390f4a2713aSLionel Sambuc /* Implicit hexadecimal point? */
2391f4a2713aSLionel Sambuc if (dot == end)
2392f4a2713aSLionel Sambuc dot = p;
2393f4a2713aSLionel Sambuc
2394f4a2713aSLionel Sambuc /* Calculate the exponent adjustment implicit in the number of
2395f4a2713aSLionel Sambuc significant digits. */
2396f4a2713aSLionel Sambuc expAdjustment = static_cast<int>(dot - firstSignificantDigit);
2397f4a2713aSLionel Sambuc if (expAdjustment < 0)
2398f4a2713aSLionel Sambuc expAdjustment++;
2399f4a2713aSLionel Sambuc expAdjustment = expAdjustment * 4 - 1;
2400f4a2713aSLionel Sambuc
2401f4a2713aSLionel Sambuc /* Adjust for writing the significand starting at the most
2402f4a2713aSLionel Sambuc significant nibble. */
2403f4a2713aSLionel Sambuc expAdjustment += semantics->precision;
2404f4a2713aSLionel Sambuc expAdjustment -= partsCount * integerPartWidth;
2405f4a2713aSLionel Sambuc
2406f4a2713aSLionel Sambuc /* Adjust for the given exponent. */
2407f4a2713aSLionel Sambuc exponent = totalExponent(p + 1, end, expAdjustment);
2408f4a2713aSLionel Sambuc }
2409f4a2713aSLionel Sambuc
2410f4a2713aSLionel Sambuc return normalize(rounding_mode, lost_fraction);
2411f4a2713aSLionel Sambuc }
2412f4a2713aSLionel Sambuc
2413f4a2713aSLionel Sambuc APFloat::opStatus
roundSignificandWithExponent(const integerPart * decSigParts,unsigned sigPartCount,int exp,roundingMode rounding_mode)2414f4a2713aSLionel Sambuc APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2415f4a2713aSLionel Sambuc unsigned sigPartCount, int exp,
2416f4a2713aSLionel Sambuc roundingMode rounding_mode)
2417f4a2713aSLionel Sambuc {
2418f4a2713aSLionel Sambuc unsigned int parts, pow5PartCount;
2419f4a2713aSLionel Sambuc fltSemantics calcSemantics = { 32767, -32767, 0 };
2420f4a2713aSLionel Sambuc integerPart pow5Parts[maxPowerOfFiveParts];
2421f4a2713aSLionel Sambuc bool isNearest;
2422f4a2713aSLionel Sambuc
2423f4a2713aSLionel Sambuc isNearest = (rounding_mode == rmNearestTiesToEven ||
2424f4a2713aSLionel Sambuc rounding_mode == rmNearestTiesToAway);
2425f4a2713aSLionel Sambuc
2426f4a2713aSLionel Sambuc parts = partCountForBits(semantics->precision + 11);
2427f4a2713aSLionel Sambuc
2428f4a2713aSLionel Sambuc /* Calculate pow(5, abs(exp)). */
2429f4a2713aSLionel Sambuc pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2430f4a2713aSLionel Sambuc
2431f4a2713aSLionel Sambuc for (;; parts *= 2) {
2432f4a2713aSLionel Sambuc opStatus sigStatus, powStatus;
2433f4a2713aSLionel Sambuc unsigned int excessPrecision, truncatedBits;
2434f4a2713aSLionel Sambuc
2435f4a2713aSLionel Sambuc calcSemantics.precision = parts * integerPartWidth - 1;
2436f4a2713aSLionel Sambuc excessPrecision = calcSemantics.precision - semantics->precision;
2437f4a2713aSLionel Sambuc truncatedBits = excessPrecision;
2438f4a2713aSLionel Sambuc
2439f4a2713aSLionel Sambuc APFloat decSig = APFloat::getZero(calcSemantics, sign);
2440f4a2713aSLionel Sambuc APFloat pow5(calcSemantics);
2441f4a2713aSLionel Sambuc
2442f4a2713aSLionel Sambuc sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2443f4a2713aSLionel Sambuc rmNearestTiesToEven);
2444f4a2713aSLionel Sambuc powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2445f4a2713aSLionel Sambuc rmNearestTiesToEven);
2446f4a2713aSLionel Sambuc /* Add exp, as 10^n = 5^n * 2^n. */
2447f4a2713aSLionel Sambuc decSig.exponent += exp;
2448f4a2713aSLionel Sambuc
2449f4a2713aSLionel Sambuc lostFraction calcLostFraction;
2450f4a2713aSLionel Sambuc integerPart HUerr, HUdistance;
2451f4a2713aSLionel Sambuc unsigned int powHUerr;
2452f4a2713aSLionel Sambuc
2453f4a2713aSLionel Sambuc if (exp >= 0) {
2454f4a2713aSLionel Sambuc /* multiplySignificand leaves the precision-th bit set to 1. */
2455*0a6a1f1dSLionel Sambuc calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
2456f4a2713aSLionel Sambuc powHUerr = powStatus != opOK;
2457f4a2713aSLionel Sambuc } else {
2458f4a2713aSLionel Sambuc calcLostFraction = decSig.divideSignificand(pow5);
2459f4a2713aSLionel Sambuc /* Denormal numbers have less precision. */
2460f4a2713aSLionel Sambuc if (decSig.exponent < semantics->minExponent) {
2461f4a2713aSLionel Sambuc excessPrecision += (semantics->minExponent - decSig.exponent);
2462f4a2713aSLionel Sambuc truncatedBits = excessPrecision;
2463f4a2713aSLionel Sambuc if (excessPrecision > calcSemantics.precision)
2464f4a2713aSLionel Sambuc excessPrecision = calcSemantics.precision;
2465f4a2713aSLionel Sambuc }
2466f4a2713aSLionel Sambuc /* Extra half-ulp lost in reciprocal of exponent. */
2467f4a2713aSLionel Sambuc powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
2468f4a2713aSLionel Sambuc }
2469f4a2713aSLionel Sambuc
2470f4a2713aSLionel Sambuc /* Both multiplySignificand and divideSignificand return the
2471f4a2713aSLionel Sambuc result with the integer bit set. */
2472f4a2713aSLionel Sambuc assert(APInt::tcExtractBit
2473f4a2713aSLionel Sambuc (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2474f4a2713aSLionel Sambuc
2475f4a2713aSLionel Sambuc HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2476f4a2713aSLionel Sambuc powHUerr);
2477f4a2713aSLionel Sambuc HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2478f4a2713aSLionel Sambuc excessPrecision, isNearest);
2479f4a2713aSLionel Sambuc
2480f4a2713aSLionel Sambuc /* Are we guaranteed to round correctly if we truncate? */
2481f4a2713aSLionel Sambuc if (HUdistance >= HUerr) {
2482f4a2713aSLionel Sambuc APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2483f4a2713aSLionel Sambuc calcSemantics.precision - excessPrecision,
2484f4a2713aSLionel Sambuc excessPrecision);
2485f4a2713aSLionel Sambuc /* Take the exponent of decSig. If we tcExtract-ed less bits
2486f4a2713aSLionel Sambuc above we must adjust our exponent to compensate for the
2487f4a2713aSLionel Sambuc implicit right shift. */
2488f4a2713aSLionel Sambuc exponent = (decSig.exponent + semantics->precision
2489f4a2713aSLionel Sambuc - (calcSemantics.precision - excessPrecision));
2490f4a2713aSLionel Sambuc calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2491f4a2713aSLionel Sambuc decSig.partCount(),
2492f4a2713aSLionel Sambuc truncatedBits);
2493f4a2713aSLionel Sambuc return normalize(rounding_mode, calcLostFraction);
2494f4a2713aSLionel Sambuc }
2495f4a2713aSLionel Sambuc }
2496f4a2713aSLionel Sambuc }
2497f4a2713aSLionel Sambuc
2498f4a2713aSLionel Sambuc APFloat::opStatus
convertFromDecimalString(StringRef str,roundingMode rounding_mode)2499f4a2713aSLionel Sambuc APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
2500f4a2713aSLionel Sambuc {
2501f4a2713aSLionel Sambuc decimalInfo D;
2502f4a2713aSLionel Sambuc opStatus fs;
2503f4a2713aSLionel Sambuc
2504f4a2713aSLionel Sambuc /* Scan the text. */
2505f4a2713aSLionel Sambuc StringRef::iterator p = str.begin();
2506f4a2713aSLionel Sambuc interpretDecimal(p, str.end(), &D);
2507f4a2713aSLionel Sambuc
2508f4a2713aSLionel Sambuc /* Handle the quick cases. First the case of no significant digits,
2509f4a2713aSLionel Sambuc i.e. zero, and then exponents that are obviously too large or too
2510f4a2713aSLionel Sambuc small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2511f4a2713aSLionel Sambuc definitely overflows if
2512f4a2713aSLionel Sambuc
2513f4a2713aSLionel Sambuc (exp - 1) * L >= maxExponent
2514f4a2713aSLionel Sambuc
2515f4a2713aSLionel Sambuc and definitely underflows to zero where
2516f4a2713aSLionel Sambuc
2517f4a2713aSLionel Sambuc (exp + 1) * L <= minExponent - precision
2518f4a2713aSLionel Sambuc
2519f4a2713aSLionel Sambuc With integer arithmetic the tightest bounds for L are
2520f4a2713aSLionel Sambuc
2521f4a2713aSLionel Sambuc 93/28 < L < 196/59 [ numerator <= 256 ]
2522f4a2713aSLionel Sambuc 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2523f4a2713aSLionel Sambuc */
2524f4a2713aSLionel Sambuc
2525f4a2713aSLionel Sambuc // Test if we have a zero number allowing for strings with no null terminators
2526f4a2713aSLionel Sambuc // and zero decimals with non-zero exponents.
2527f4a2713aSLionel Sambuc //
2528f4a2713aSLionel Sambuc // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2529f4a2713aSLionel Sambuc // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2530f4a2713aSLionel Sambuc // be at most one dot. On the other hand, if we have a zero with a non-zero
2531f4a2713aSLionel Sambuc // exponent, then we know that D.firstSigDigit will be non-numeric.
2532f4a2713aSLionel Sambuc if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
2533f4a2713aSLionel Sambuc category = fcZero;
2534f4a2713aSLionel Sambuc fs = opOK;
2535f4a2713aSLionel Sambuc
2536f4a2713aSLionel Sambuc /* Check whether the normalized exponent is high enough to overflow
2537f4a2713aSLionel Sambuc max during the log-rebasing in the max-exponent check below. */
2538f4a2713aSLionel Sambuc } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
2539f4a2713aSLionel Sambuc fs = handleOverflow(rounding_mode);
2540f4a2713aSLionel Sambuc
2541f4a2713aSLionel Sambuc /* If it wasn't, then it also wasn't high enough to overflow max
2542f4a2713aSLionel Sambuc during the log-rebasing in the min-exponent check. Check that it
2543f4a2713aSLionel Sambuc won't overflow min in either check, then perform the min-exponent
2544f4a2713aSLionel Sambuc check. */
2545f4a2713aSLionel Sambuc } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
2546f4a2713aSLionel Sambuc (D.normalizedExponent + 1) * 28738 <=
2547f4a2713aSLionel Sambuc 8651 * (semantics->minExponent - (int) semantics->precision)) {
2548f4a2713aSLionel Sambuc /* Underflow to zero and round. */
2549f4a2713aSLionel Sambuc category = fcNormal;
2550f4a2713aSLionel Sambuc zeroSignificand();
2551f4a2713aSLionel Sambuc fs = normalize(rounding_mode, lfLessThanHalf);
2552f4a2713aSLionel Sambuc
2553f4a2713aSLionel Sambuc /* We can finally safely perform the max-exponent check. */
2554f4a2713aSLionel Sambuc } else if ((D.normalizedExponent - 1) * 42039
2555f4a2713aSLionel Sambuc >= 12655 * semantics->maxExponent) {
2556f4a2713aSLionel Sambuc /* Overflow and round. */
2557f4a2713aSLionel Sambuc fs = handleOverflow(rounding_mode);
2558f4a2713aSLionel Sambuc } else {
2559f4a2713aSLionel Sambuc integerPart *decSignificand;
2560f4a2713aSLionel Sambuc unsigned int partCount;
2561f4a2713aSLionel Sambuc
2562f4a2713aSLionel Sambuc /* A tight upper bound on number of bits required to hold an
2563f4a2713aSLionel Sambuc N-digit decimal integer is N * 196 / 59. Allocate enough space
2564f4a2713aSLionel Sambuc to hold the full significand, and an extra part required by
2565f4a2713aSLionel Sambuc tcMultiplyPart. */
2566f4a2713aSLionel Sambuc partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
2567f4a2713aSLionel Sambuc partCount = partCountForBits(1 + 196 * partCount / 59);
2568f4a2713aSLionel Sambuc decSignificand = new integerPart[partCount + 1];
2569f4a2713aSLionel Sambuc partCount = 0;
2570f4a2713aSLionel Sambuc
2571f4a2713aSLionel Sambuc /* Convert to binary efficiently - we do almost all multiplication
2572f4a2713aSLionel Sambuc in an integerPart. When this would overflow do we do a single
2573f4a2713aSLionel Sambuc bignum multiplication, and then revert again to multiplication
2574f4a2713aSLionel Sambuc in an integerPart. */
2575f4a2713aSLionel Sambuc do {
2576f4a2713aSLionel Sambuc integerPart decValue, val, multiplier;
2577f4a2713aSLionel Sambuc
2578f4a2713aSLionel Sambuc val = 0;
2579f4a2713aSLionel Sambuc multiplier = 1;
2580f4a2713aSLionel Sambuc
2581f4a2713aSLionel Sambuc do {
2582f4a2713aSLionel Sambuc if (*p == '.') {
2583f4a2713aSLionel Sambuc p++;
2584f4a2713aSLionel Sambuc if (p == str.end()) {
2585f4a2713aSLionel Sambuc break;
2586f4a2713aSLionel Sambuc }
2587f4a2713aSLionel Sambuc }
2588f4a2713aSLionel Sambuc decValue = decDigitValue(*p++);
2589f4a2713aSLionel Sambuc assert(decValue < 10U && "Invalid character in significand");
2590f4a2713aSLionel Sambuc multiplier *= 10;
2591f4a2713aSLionel Sambuc val = val * 10 + decValue;
2592f4a2713aSLionel Sambuc /* The maximum number that can be multiplied by ten with any
2593f4a2713aSLionel Sambuc digit added without overflowing an integerPart. */
2594f4a2713aSLionel Sambuc } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2595f4a2713aSLionel Sambuc
2596f4a2713aSLionel Sambuc /* Multiply out the current part. */
2597f4a2713aSLionel Sambuc APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2598f4a2713aSLionel Sambuc partCount, partCount + 1, false);
2599f4a2713aSLionel Sambuc
2600f4a2713aSLionel Sambuc /* If we used another part (likely but not guaranteed), increase
2601f4a2713aSLionel Sambuc the count. */
2602f4a2713aSLionel Sambuc if (decSignificand[partCount])
2603f4a2713aSLionel Sambuc partCount++;
2604f4a2713aSLionel Sambuc } while (p <= D.lastSigDigit);
2605f4a2713aSLionel Sambuc
2606f4a2713aSLionel Sambuc category = fcNormal;
2607f4a2713aSLionel Sambuc fs = roundSignificandWithExponent(decSignificand, partCount,
2608f4a2713aSLionel Sambuc D.exponent, rounding_mode);
2609f4a2713aSLionel Sambuc
2610f4a2713aSLionel Sambuc delete [] decSignificand;
2611f4a2713aSLionel Sambuc }
2612f4a2713aSLionel Sambuc
2613f4a2713aSLionel Sambuc return fs;
2614f4a2713aSLionel Sambuc }
2615f4a2713aSLionel Sambuc
2616f4a2713aSLionel Sambuc bool
convertFromStringSpecials(StringRef str)2617f4a2713aSLionel Sambuc APFloat::convertFromStringSpecials(StringRef str) {
2618f4a2713aSLionel Sambuc if (str.equals("inf") || str.equals("INFINITY")) {
2619f4a2713aSLionel Sambuc makeInf(false);
2620f4a2713aSLionel Sambuc return true;
2621f4a2713aSLionel Sambuc }
2622f4a2713aSLionel Sambuc
2623f4a2713aSLionel Sambuc if (str.equals("-inf") || str.equals("-INFINITY")) {
2624f4a2713aSLionel Sambuc makeInf(true);
2625f4a2713aSLionel Sambuc return true;
2626f4a2713aSLionel Sambuc }
2627f4a2713aSLionel Sambuc
2628f4a2713aSLionel Sambuc if (str.equals("nan") || str.equals("NaN")) {
2629f4a2713aSLionel Sambuc makeNaN(false, false);
2630f4a2713aSLionel Sambuc return true;
2631f4a2713aSLionel Sambuc }
2632f4a2713aSLionel Sambuc
2633f4a2713aSLionel Sambuc if (str.equals("-nan") || str.equals("-NaN")) {
2634f4a2713aSLionel Sambuc makeNaN(false, true);
2635f4a2713aSLionel Sambuc return true;
2636f4a2713aSLionel Sambuc }
2637f4a2713aSLionel Sambuc
2638f4a2713aSLionel Sambuc return false;
2639f4a2713aSLionel Sambuc }
2640f4a2713aSLionel Sambuc
2641f4a2713aSLionel Sambuc APFloat::opStatus
convertFromString(StringRef str,roundingMode rounding_mode)2642f4a2713aSLionel Sambuc APFloat::convertFromString(StringRef str, roundingMode rounding_mode)
2643f4a2713aSLionel Sambuc {
2644f4a2713aSLionel Sambuc assert(!str.empty() && "Invalid string length");
2645f4a2713aSLionel Sambuc
2646f4a2713aSLionel Sambuc // Handle special cases.
2647f4a2713aSLionel Sambuc if (convertFromStringSpecials(str))
2648f4a2713aSLionel Sambuc return opOK;
2649f4a2713aSLionel Sambuc
2650f4a2713aSLionel Sambuc /* Handle a leading minus sign. */
2651f4a2713aSLionel Sambuc StringRef::iterator p = str.begin();
2652f4a2713aSLionel Sambuc size_t slen = str.size();
2653f4a2713aSLionel Sambuc sign = *p == '-' ? 1 : 0;
2654f4a2713aSLionel Sambuc if (*p == '-' || *p == '+') {
2655f4a2713aSLionel Sambuc p++;
2656f4a2713aSLionel Sambuc slen--;
2657f4a2713aSLionel Sambuc assert(slen && "String has no digits");
2658f4a2713aSLionel Sambuc }
2659f4a2713aSLionel Sambuc
2660f4a2713aSLionel Sambuc if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2661f4a2713aSLionel Sambuc assert(slen - 2 && "Invalid string");
2662f4a2713aSLionel Sambuc return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
2663f4a2713aSLionel Sambuc rounding_mode);
2664f4a2713aSLionel Sambuc }
2665f4a2713aSLionel Sambuc
2666f4a2713aSLionel Sambuc return convertFromDecimalString(StringRef(p, slen), rounding_mode);
2667f4a2713aSLionel Sambuc }
2668f4a2713aSLionel Sambuc
2669f4a2713aSLionel Sambuc /* Write out a hexadecimal representation of the floating point value
2670f4a2713aSLionel Sambuc to DST, which must be of sufficient size, in the C99 form
2671f4a2713aSLionel Sambuc [-]0xh.hhhhp[+-]d. Return the number of characters written,
2672f4a2713aSLionel Sambuc excluding the terminating NUL.
2673f4a2713aSLionel Sambuc
2674f4a2713aSLionel Sambuc If UPPERCASE, the output is in upper case, otherwise in lower case.
2675f4a2713aSLionel Sambuc
2676f4a2713aSLionel Sambuc HEXDIGITS digits appear altogether, rounding the value if
2677f4a2713aSLionel Sambuc necessary. If HEXDIGITS is 0, the minimal precision to display the
2678f4a2713aSLionel Sambuc number precisely is used instead. If nothing would appear after
2679f4a2713aSLionel Sambuc the decimal point it is suppressed.
2680f4a2713aSLionel Sambuc
2681f4a2713aSLionel Sambuc The decimal exponent is always printed and has at least one digit.
2682f4a2713aSLionel Sambuc Zero values display an exponent of zero. Infinities and NaNs
2683f4a2713aSLionel Sambuc appear as "infinity" or "nan" respectively.
2684f4a2713aSLionel Sambuc
2685f4a2713aSLionel Sambuc The above rules are as specified by C99. There is ambiguity about
2686f4a2713aSLionel Sambuc what the leading hexadecimal digit should be. This implementation
2687f4a2713aSLionel Sambuc uses whatever is necessary so that the exponent is displayed as
2688f4a2713aSLionel Sambuc stored. This implies the exponent will fall within the IEEE format
2689f4a2713aSLionel Sambuc range, and the leading hexadecimal digit will be 0 (for denormals),
2690f4a2713aSLionel Sambuc 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2691f4a2713aSLionel Sambuc any other digits zero).
2692f4a2713aSLionel Sambuc */
2693f4a2713aSLionel Sambuc unsigned int
convertToHexString(char * dst,unsigned int hexDigits,bool upperCase,roundingMode rounding_mode) const2694f4a2713aSLionel Sambuc APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2695f4a2713aSLionel Sambuc bool upperCase, roundingMode rounding_mode) const
2696f4a2713aSLionel Sambuc {
2697f4a2713aSLionel Sambuc char *p;
2698f4a2713aSLionel Sambuc
2699f4a2713aSLionel Sambuc p = dst;
2700f4a2713aSLionel Sambuc if (sign)
2701f4a2713aSLionel Sambuc *dst++ = '-';
2702f4a2713aSLionel Sambuc
2703f4a2713aSLionel Sambuc switch (category) {
2704f4a2713aSLionel Sambuc case fcInfinity:
2705f4a2713aSLionel Sambuc memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2706f4a2713aSLionel Sambuc dst += sizeof infinityL - 1;
2707f4a2713aSLionel Sambuc break;
2708f4a2713aSLionel Sambuc
2709f4a2713aSLionel Sambuc case fcNaN:
2710f4a2713aSLionel Sambuc memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2711f4a2713aSLionel Sambuc dst += sizeof NaNU - 1;
2712f4a2713aSLionel Sambuc break;
2713f4a2713aSLionel Sambuc
2714f4a2713aSLionel Sambuc case fcZero:
2715f4a2713aSLionel Sambuc *dst++ = '0';
2716f4a2713aSLionel Sambuc *dst++ = upperCase ? 'X': 'x';
2717f4a2713aSLionel Sambuc *dst++ = '0';
2718f4a2713aSLionel Sambuc if (hexDigits > 1) {
2719f4a2713aSLionel Sambuc *dst++ = '.';
2720f4a2713aSLionel Sambuc memset (dst, '0', hexDigits - 1);
2721f4a2713aSLionel Sambuc dst += hexDigits - 1;
2722f4a2713aSLionel Sambuc }
2723f4a2713aSLionel Sambuc *dst++ = upperCase ? 'P': 'p';
2724f4a2713aSLionel Sambuc *dst++ = '0';
2725f4a2713aSLionel Sambuc break;
2726f4a2713aSLionel Sambuc
2727f4a2713aSLionel Sambuc case fcNormal:
2728f4a2713aSLionel Sambuc dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2729f4a2713aSLionel Sambuc break;
2730f4a2713aSLionel Sambuc }
2731f4a2713aSLionel Sambuc
2732f4a2713aSLionel Sambuc *dst = 0;
2733f4a2713aSLionel Sambuc
2734f4a2713aSLionel Sambuc return static_cast<unsigned int>(dst - p);
2735f4a2713aSLionel Sambuc }
2736f4a2713aSLionel Sambuc
2737f4a2713aSLionel Sambuc /* Does the hard work of outputting the correctly rounded hexadecimal
2738f4a2713aSLionel Sambuc form of a normal floating point number with the specified number of
2739f4a2713aSLionel Sambuc hexadecimal digits. If HEXDIGITS is zero the minimum number of
2740f4a2713aSLionel Sambuc digits necessary to print the value precisely is output. */
2741f4a2713aSLionel Sambuc char *
convertNormalToHexString(char * dst,unsigned int hexDigits,bool upperCase,roundingMode rounding_mode) const2742f4a2713aSLionel Sambuc APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2743f4a2713aSLionel Sambuc bool upperCase,
2744f4a2713aSLionel Sambuc roundingMode rounding_mode) const
2745f4a2713aSLionel Sambuc {
2746f4a2713aSLionel Sambuc unsigned int count, valueBits, shift, partsCount, outputDigits;
2747f4a2713aSLionel Sambuc const char *hexDigitChars;
2748f4a2713aSLionel Sambuc const integerPart *significand;
2749f4a2713aSLionel Sambuc char *p;
2750f4a2713aSLionel Sambuc bool roundUp;
2751f4a2713aSLionel Sambuc
2752f4a2713aSLionel Sambuc *dst++ = '0';
2753f4a2713aSLionel Sambuc *dst++ = upperCase ? 'X': 'x';
2754f4a2713aSLionel Sambuc
2755f4a2713aSLionel Sambuc roundUp = false;
2756f4a2713aSLionel Sambuc hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2757f4a2713aSLionel Sambuc
2758f4a2713aSLionel Sambuc significand = significandParts();
2759f4a2713aSLionel Sambuc partsCount = partCount();
2760f4a2713aSLionel Sambuc
2761f4a2713aSLionel Sambuc /* +3 because the first digit only uses the single integer bit, so
2762f4a2713aSLionel Sambuc we have 3 virtual zero most-significant-bits. */
2763f4a2713aSLionel Sambuc valueBits = semantics->precision + 3;
2764f4a2713aSLionel Sambuc shift = integerPartWidth - valueBits % integerPartWidth;
2765f4a2713aSLionel Sambuc
2766f4a2713aSLionel Sambuc /* The natural number of digits required ignoring trailing
2767f4a2713aSLionel Sambuc insignificant zeroes. */
2768f4a2713aSLionel Sambuc outputDigits = (valueBits - significandLSB () + 3) / 4;
2769f4a2713aSLionel Sambuc
2770f4a2713aSLionel Sambuc /* hexDigits of zero means use the required number for the
2771f4a2713aSLionel Sambuc precision. Otherwise, see if we are truncating. If we are,
2772f4a2713aSLionel Sambuc find out if we need to round away from zero. */
2773f4a2713aSLionel Sambuc if (hexDigits) {
2774f4a2713aSLionel Sambuc if (hexDigits < outputDigits) {
2775f4a2713aSLionel Sambuc /* We are dropping non-zero bits, so need to check how to round.
2776f4a2713aSLionel Sambuc "bits" is the number of dropped bits. */
2777f4a2713aSLionel Sambuc unsigned int bits;
2778f4a2713aSLionel Sambuc lostFraction fraction;
2779f4a2713aSLionel Sambuc
2780f4a2713aSLionel Sambuc bits = valueBits - hexDigits * 4;
2781f4a2713aSLionel Sambuc fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2782f4a2713aSLionel Sambuc roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2783f4a2713aSLionel Sambuc }
2784f4a2713aSLionel Sambuc outputDigits = hexDigits;
2785f4a2713aSLionel Sambuc }
2786f4a2713aSLionel Sambuc
2787f4a2713aSLionel Sambuc /* Write the digits consecutively, and start writing in the location
2788f4a2713aSLionel Sambuc of the hexadecimal point. We move the most significant digit
2789f4a2713aSLionel Sambuc left and add the hexadecimal point later. */
2790f4a2713aSLionel Sambuc p = ++dst;
2791f4a2713aSLionel Sambuc
2792f4a2713aSLionel Sambuc count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2793f4a2713aSLionel Sambuc
2794f4a2713aSLionel Sambuc while (outputDigits && count) {
2795f4a2713aSLionel Sambuc integerPart part;
2796f4a2713aSLionel Sambuc
2797f4a2713aSLionel Sambuc /* Put the most significant integerPartWidth bits in "part". */
2798f4a2713aSLionel Sambuc if (--count == partsCount)
2799f4a2713aSLionel Sambuc part = 0; /* An imaginary higher zero part. */
2800f4a2713aSLionel Sambuc else
2801f4a2713aSLionel Sambuc part = significand[count] << shift;
2802f4a2713aSLionel Sambuc
2803f4a2713aSLionel Sambuc if (count && shift)
2804f4a2713aSLionel Sambuc part |= significand[count - 1] >> (integerPartWidth - shift);
2805f4a2713aSLionel Sambuc
2806f4a2713aSLionel Sambuc /* Convert as much of "part" to hexdigits as we can. */
2807f4a2713aSLionel Sambuc unsigned int curDigits = integerPartWidth / 4;
2808f4a2713aSLionel Sambuc
2809f4a2713aSLionel Sambuc if (curDigits > outputDigits)
2810f4a2713aSLionel Sambuc curDigits = outputDigits;
2811f4a2713aSLionel Sambuc dst += partAsHex (dst, part, curDigits, hexDigitChars);
2812f4a2713aSLionel Sambuc outputDigits -= curDigits;
2813f4a2713aSLionel Sambuc }
2814f4a2713aSLionel Sambuc
2815f4a2713aSLionel Sambuc if (roundUp) {
2816f4a2713aSLionel Sambuc char *q = dst;
2817f4a2713aSLionel Sambuc
2818f4a2713aSLionel Sambuc /* Note that hexDigitChars has a trailing '0'. */
2819f4a2713aSLionel Sambuc do {
2820f4a2713aSLionel Sambuc q--;
2821f4a2713aSLionel Sambuc *q = hexDigitChars[hexDigitValue (*q) + 1];
2822f4a2713aSLionel Sambuc } while (*q == '0');
2823f4a2713aSLionel Sambuc assert(q >= p);
2824f4a2713aSLionel Sambuc } else {
2825f4a2713aSLionel Sambuc /* Add trailing zeroes. */
2826f4a2713aSLionel Sambuc memset (dst, '0', outputDigits);
2827f4a2713aSLionel Sambuc dst += outputDigits;
2828f4a2713aSLionel Sambuc }
2829f4a2713aSLionel Sambuc
2830f4a2713aSLionel Sambuc /* Move the most significant digit to before the point, and if there
2831f4a2713aSLionel Sambuc is something after the decimal point add it. This must come
2832f4a2713aSLionel Sambuc after rounding above. */
2833f4a2713aSLionel Sambuc p[-1] = p[0];
2834f4a2713aSLionel Sambuc if (dst -1 == p)
2835f4a2713aSLionel Sambuc dst--;
2836f4a2713aSLionel Sambuc else
2837f4a2713aSLionel Sambuc p[0] = '.';
2838f4a2713aSLionel Sambuc
2839f4a2713aSLionel Sambuc /* Finally output the exponent. */
2840f4a2713aSLionel Sambuc *dst++ = upperCase ? 'P': 'p';
2841f4a2713aSLionel Sambuc
2842f4a2713aSLionel Sambuc return writeSignedDecimal (dst, exponent);
2843f4a2713aSLionel Sambuc }
2844f4a2713aSLionel Sambuc
hash_value(const APFloat & Arg)2845f4a2713aSLionel Sambuc hash_code llvm::hash_value(const APFloat &Arg) {
2846f4a2713aSLionel Sambuc if (!Arg.isFiniteNonZero())
2847f4a2713aSLionel Sambuc return hash_combine((uint8_t)Arg.category,
2848f4a2713aSLionel Sambuc // NaN has no sign, fix it at zero.
2849f4a2713aSLionel Sambuc Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2850f4a2713aSLionel Sambuc Arg.semantics->precision);
2851f4a2713aSLionel Sambuc
2852f4a2713aSLionel Sambuc // Normal floats need their exponent and significand hashed.
2853f4a2713aSLionel Sambuc return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2854f4a2713aSLionel Sambuc Arg.semantics->precision, Arg.exponent,
2855f4a2713aSLionel Sambuc hash_combine_range(
2856f4a2713aSLionel Sambuc Arg.significandParts(),
2857f4a2713aSLionel Sambuc Arg.significandParts() + Arg.partCount()));
2858f4a2713aSLionel Sambuc }
2859f4a2713aSLionel Sambuc
2860f4a2713aSLionel Sambuc // Conversion from APFloat to/from host float/double. It may eventually be
2861f4a2713aSLionel Sambuc // possible to eliminate these and have everybody deal with APFloats, but that
2862f4a2713aSLionel Sambuc // will take a while. This approach will not easily extend to long double.
2863f4a2713aSLionel Sambuc // Current implementation requires integerPartWidth==64, which is correct at
2864f4a2713aSLionel Sambuc // the moment but could be made more general.
2865f4a2713aSLionel Sambuc
2866f4a2713aSLionel Sambuc // Denormals have exponent minExponent in APFloat, but minExponent-1 in
2867f4a2713aSLionel Sambuc // the actual IEEE respresentations. We compensate for that here.
2868f4a2713aSLionel Sambuc
2869f4a2713aSLionel Sambuc APInt
convertF80LongDoubleAPFloatToAPInt() const2870f4a2713aSLionel Sambuc APFloat::convertF80LongDoubleAPFloatToAPInt() const
2871f4a2713aSLionel Sambuc {
2872f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
2873f4a2713aSLionel Sambuc assert(partCount()==2);
2874f4a2713aSLionel Sambuc
2875f4a2713aSLionel Sambuc uint64_t myexponent, mysignificand;
2876f4a2713aSLionel Sambuc
2877f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
2878f4a2713aSLionel Sambuc myexponent = exponent+16383; //bias
2879f4a2713aSLionel Sambuc mysignificand = significandParts()[0];
2880f4a2713aSLionel Sambuc if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2881f4a2713aSLionel Sambuc myexponent = 0; // denormal
2882f4a2713aSLionel Sambuc } else if (category==fcZero) {
2883f4a2713aSLionel Sambuc myexponent = 0;
2884f4a2713aSLionel Sambuc mysignificand = 0;
2885f4a2713aSLionel Sambuc } else if (category==fcInfinity) {
2886f4a2713aSLionel Sambuc myexponent = 0x7fff;
2887f4a2713aSLionel Sambuc mysignificand = 0x8000000000000000ULL;
2888f4a2713aSLionel Sambuc } else {
2889f4a2713aSLionel Sambuc assert(category == fcNaN && "Unknown category");
2890f4a2713aSLionel Sambuc myexponent = 0x7fff;
2891f4a2713aSLionel Sambuc mysignificand = significandParts()[0];
2892f4a2713aSLionel Sambuc }
2893f4a2713aSLionel Sambuc
2894f4a2713aSLionel Sambuc uint64_t words[2];
2895f4a2713aSLionel Sambuc words[0] = mysignificand;
2896f4a2713aSLionel Sambuc words[1] = ((uint64_t)(sign & 1) << 15) |
2897f4a2713aSLionel Sambuc (myexponent & 0x7fffLL);
2898f4a2713aSLionel Sambuc return APInt(80, words);
2899f4a2713aSLionel Sambuc }
2900f4a2713aSLionel Sambuc
2901f4a2713aSLionel Sambuc APInt
convertPPCDoubleDoubleAPFloatToAPInt() const2902f4a2713aSLionel Sambuc APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2903f4a2713aSLionel Sambuc {
2904f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
2905f4a2713aSLionel Sambuc assert(partCount()==2);
2906f4a2713aSLionel Sambuc
2907f4a2713aSLionel Sambuc uint64_t words[2];
2908f4a2713aSLionel Sambuc opStatus fs;
2909f4a2713aSLionel Sambuc bool losesInfo;
2910f4a2713aSLionel Sambuc
2911f4a2713aSLionel Sambuc // Convert number to double. To avoid spurious underflows, we re-
2912f4a2713aSLionel Sambuc // normalize against the "double" minExponent first, and only *then*
2913f4a2713aSLionel Sambuc // truncate the mantissa. The result of that second conversion
2914f4a2713aSLionel Sambuc // may be inexact, but should never underflow.
2915f4a2713aSLionel Sambuc // Declare fltSemantics before APFloat that uses it (and
2916f4a2713aSLionel Sambuc // saves pointer to it) to ensure correct destruction order.
2917f4a2713aSLionel Sambuc fltSemantics extendedSemantics = *semantics;
2918f4a2713aSLionel Sambuc extendedSemantics.minExponent = IEEEdouble.minExponent;
2919f4a2713aSLionel Sambuc APFloat extended(*this);
2920f4a2713aSLionel Sambuc fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2921f4a2713aSLionel Sambuc assert(fs == opOK && !losesInfo);
2922f4a2713aSLionel Sambuc (void)fs;
2923f4a2713aSLionel Sambuc
2924f4a2713aSLionel Sambuc APFloat u(extended);
2925f4a2713aSLionel Sambuc fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2926f4a2713aSLionel Sambuc assert(fs == opOK || fs == opInexact);
2927f4a2713aSLionel Sambuc (void)fs;
2928f4a2713aSLionel Sambuc words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2929f4a2713aSLionel Sambuc
2930f4a2713aSLionel Sambuc // If conversion was exact or resulted in a special case, we're done;
2931f4a2713aSLionel Sambuc // just set the second double to zero. Otherwise, re-convert back to
2932f4a2713aSLionel Sambuc // the extended format and compute the difference. This now should
2933f4a2713aSLionel Sambuc // convert exactly to double.
2934f4a2713aSLionel Sambuc if (u.isFiniteNonZero() && losesInfo) {
2935f4a2713aSLionel Sambuc fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2936f4a2713aSLionel Sambuc assert(fs == opOK && !losesInfo);
2937f4a2713aSLionel Sambuc (void)fs;
2938f4a2713aSLionel Sambuc
2939f4a2713aSLionel Sambuc APFloat v(extended);
2940f4a2713aSLionel Sambuc v.subtract(u, rmNearestTiesToEven);
2941f4a2713aSLionel Sambuc fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2942f4a2713aSLionel Sambuc assert(fs == opOK && !losesInfo);
2943f4a2713aSLionel Sambuc (void)fs;
2944f4a2713aSLionel Sambuc words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
2945f4a2713aSLionel Sambuc } else {
2946f4a2713aSLionel Sambuc words[1] = 0;
2947f4a2713aSLionel Sambuc }
2948f4a2713aSLionel Sambuc
2949f4a2713aSLionel Sambuc return APInt(128, words);
2950f4a2713aSLionel Sambuc }
2951f4a2713aSLionel Sambuc
2952f4a2713aSLionel Sambuc APInt
convertQuadrupleAPFloatToAPInt() const2953f4a2713aSLionel Sambuc APFloat::convertQuadrupleAPFloatToAPInt() const
2954f4a2713aSLionel Sambuc {
2955f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&IEEEquad);
2956f4a2713aSLionel Sambuc assert(partCount()==2);
2957f4a2713aSLionel Sambuc
2958f4a2713aSLionel Sambuc uint64_t myexponent, mysignificand, mysignificand2;
2959f4a2713aSLionel Sambuc
2960f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
2961f4a2713aSLionel Sambuc myexponent = exponent+16383; //bias
2962f4a2713aSLionel Sambuc mysignificand = significandParts()[0];
2963f4a2713aSLionel Sambuc mysignificand2 = significandParts()[1];
2964f4a2713aSLionel Sambuc if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2965f4a2713aSLionel Sambuc myexponent = 0; // denormal
2966f4a2713aSLionel Sambuc } else if (category==fcZero) {
2967f4a2713aSLionel Sambuc myexponent = 0;
2968f4a2713aSLionel Sambuc mysignificand = mysignificand2 = 0;
2969f4a2713aSLionel Sambuc } else if (category==fcInfinity) {
2970f4a2713aSLionel Sambuc myexponent = 0x7fff;
2971f4a2713aSLionel Sambuc mysignificand = mysignificand2 = 0;
2972f4a2713aSLionel Sambuc } else {
2973f4a2713aSLionel Sambuc assert(category == fcNaN && "Unknown category!");
2974f4a2713aSLionel Sambuc myexponent = 0x7fff;
2975f4a2713aSLionel Sambuc mysignificand = significandParts()[0];
2976f4a2713aSLionel Sambuc mysignificand2 = significandParts()[1];
2977f4a2713aSLionel Sambuc }
2978f4a2713aSLionel Sambuc
2979f4a2713aSLionel Sambuc uint64_t words[2];
2980f4a2713aSLionel Sambuc words[0] = mysignificand;
2981f4a2713aSLionel Sambuc words[1] = ((uint64_t)(sign & 1) << 63) |
2982f4a2713aSLionel Sambuc ((myexponent & 0x7fff) << 48) |
2983f4a2713aSLionel Sambuc (mysignificand2 & 0xffffffffffffLL);
2984f4a2713aSLionel Sambuc
2985f4a2713aSLionel Sambuc return APInt(128, words);
2986f4a2713aSLionel Sambuc }
2987f4a2713aSLionel Sambuc
2988f4a2713aSLionel Sambuc APInt
convertDoubleAPFloatToAPInt() const2989f4a2713aSLionel Sambuc APFloat::convertDoubleAPFloatToAPInt() const
2990f4a2713aSLionel Sambuc {
2991f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
2992f4a2713aSLionel Sambuc assert(partCount()==1);
2993f4a2713aSLionel Sambuc
2994f4a2713aSLionel Sambuc uint64_t myexponent, mysignificand;
2995f4a2713aSLionel Sambuc
2996f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
2997f4a2713aSLionel Sambuc myexponent = exponent+1023; //bias
2998f4a2713aSLionel Sambuc mysignificand = *significandParts();
2999f4a2713aSLionel Sambuc if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
3000f4a2713aSLionel Sambuc myexponent = 0; // denormal
3001f4a2713aSLionel Sambuc } else if (category==fcZero) {
3002f4a2713aSLionel Sambuc myexponent = 0;
3003f4a2713aSLionel Sambuc mysignificand = 0;
3004f4a2713aSLionel Sambuc } else if (category==fcInfinity) {
3005f4a2713aSLionel Sambuc myexponent = 0x7ff;
3006f4a2713aSLionel Sambuc mysignificand = 0;
3007f4a2713aSLionel Sambuc } else {
3008f4a2713aSLionel Sambuc assert(category == fcNaN && "Unknown category!");
3009f4a2713aSLionel Sambuc myexponent = 0x7ff;
3010f4a2713aSLionel Sambuc mysignificand = *significandParts();
3011f4a2713aSLionel Sambuc }
3012f4a2713aSLionel Sambuc
3013f4a2713aSLionel Sambuc return APInt(64, ((((uint64_t)(sign & 1) << 63) |
3014f4a2713aSLionel Sambuc ((myexponent & 0x7ff) << 52) |
3015f4a2713aSLionel Sambuc (mysignificand & 0xfffffffffffffLL))));
3016f4a2713aSLionel Sambuc }
3017f4a2713aSLionel Sambuc
3018f4a2713aSLionel Sambuc APInt
convertFloatAPFloatToAPInt() const3019f4a2713aSLionel Sambuc APFloat::convertFloatAPFloatToAPInt() const
3020f4a2713aSLionel Sambuc {
3021f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
3022f4a2713aSLionel Sambuc assert(partCount()==1);
3023f4a2713aSLionel Sambuc
3024f4a2713aSLionel Sambuc uint32_t myexponent, mysignificand;
3025f4a2713aSLionel Sambuc
3026f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
3027f4a2713aSLionel Sambuc myexponent = exponent+127; //bias
3028f4a2713aSLionel Sambuc mysignificand = (uint32_t)*significandParts();
3029f4a2713aSLionel Sambuc if (myexponent == 1 && !(mysignificand & 0x800000))
3030f4a2713aSLionel Sambuc myexponent = 0; // denormal
3031f4a2713aSLionel Sambuc } else if (category==fcZero) {
3032f4a2713aSLionel Sambuc myexponent = 0;
3033f4a2713aSLionel Sambuc mysignificand = 0;
3034f4a2713aSLionel Sambuc } else if (category==fcInfinity) {
3035f4a2713aSLionel Sambuc myexponent = 0xff;
3036f4a2713aSLionel Sambuc mysignificand = 0;
3037f4a2713aSLionel Sambuc } else {
3038f4a2713aSLionel Sambuc assert(category == fcNaN && "Unknown category!");
3039f4a2713aSLionel Sambuc myexponent = 0xff;
3040f4a2713aSLionel Sambuc mysignificand = (uint32_t)*significandParts();
3041f4a2713aSLionel Sambuc }
3042f4a2713aSLionel Sambuc
3043f4a2713aSLionel Sambuc return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3044f4a2713aSLionel Sambuc (mysignificand & 0x7fffff)));
3045f4a2713aSLionel Sambuc }
3046f4a2713aSLionel Sambuc
3047f4a2713aSLionel Sambuc APInt
convertHalfAPFloatToAPInt() const3048f4a2713aSLionel Sambuc APFloat::convertHalfAPFloatToAPInt() const
3049f4a2713aSLionel Sambuc {
3050f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&IEEEhalf);
3051f4a2713aSLionel Sambuc assert(partCount()==1);
3052f4a2713aSLionel Sambuc
3053f4a2713aSLionel Sambuc uint32_t myexponent, mysignificand;
3054f4a2713aSLionel Sambuc
3055f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
3056f4a2713aSLionel Sambuc myexponent = exponent+15; //bias
3057f4a2713aSLionel Sambuc mysignificand = (uint32_t)*significandParts();
3058f4a2713aSLionel Sambuc if (myexponent == 1 && !(mysignificand & 0x400))
3059f4a2713aSLionel Sambuc myexponent = 0; // denormal
3060f4a2713aSLionel Sambuc } else if (category==fcZero) {
3061f4a2713aSLionel Sambuc myexponent = 0;
3062f4a2713aSLionel Sambuc mysignificand = 0;
3063f4a2713aSLionel Sambuc } else if (category==fcInfinity) {
3064f4a2713aSLionel Sambuc myexponent = 0x1f;
3065f4a2713aSLionel Sambuc mysignificand = 0;
3066f4a2713aSLionel Sambuc } else {
3067f4a2713aSLionel Sambuc assert(category == fcNaN && "Unknown category!");
3068f4a2713aSLionel Sambuc myexponent = 0x1f;
3069f4a2713aSLionel Sambuc mysignificand = (uint32_t)*significandParts();
3070f4a2713aSLionel Sambuc }
3071f4a2713aSLionel Sambuc
3072f4a2713aSLionel Sambuc return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3073f4a2713aSLionel Sambuc (mysignificand & 0x3ff)));
3074f4a2713aSLionel Sambuc }
3075f4a2713aSLionel Sambuc
3076f4a2713aSLionel Sambuc // This function creates an APInt that is just a bit map of the floating
3077f4a2713aSLionel Sambuc // point constant as it would appear in memory. It is not a conversion,
3078f4a2713aSLionel Sambuc // and treating the result as a normal integer is unlikely to be useful.
3079f4a2713aSLionel Sambuc
3080f4a2713aSLionel Sambuc APInt
bitcastToAPInt() const3081f4a2713aSLionel Sambuc APFloat::bitcastToAPInt() const
3082f4a2713aSLionel Sambuc {
3083f4a2713aSLionel Sambuc if (semantics == (const llvm::fltSemantics*)&IEEEhalf)
3084f4a2713aSLionel Sambuc return convertHalfAPFloatToAPInt();
3085f4a2713aSLionel Sambuc
3086f4a2713aSLionel Sambuc if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
3087f4a2713aSLionel Sambuc return convertFloatAPFloatToAPInt();
3088f4a2713aSLionel Sambuc
3089f4a2713aSLionel Sambuc if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
3090f4a2713aSLionel Sambuc return convertDoubleAPFloatToAPInt();
3091f4a2713aSLionel Sambuc
3092f4a2713aSLionel Sambuc if (semantics == (const llvm::fltSemantics*)&IEEEquad)
3093f4a2713aSLionel Sambuc return convertQuadrupleAPFloatToAPInt();
3094f4a2713aSLionel Sambuc
3095f4a2713aSLionel Sambuc if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
3096f4a2713aSLionel Sambuc return convertPPCDoubleDoubleAPFloatToAPInt();
3097f4a2713aSLionel Sambuc
3098f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
3099f4a2713aSLionel Sambuc "unknown format!");
3100f4a2713aSLionel Sambuc return convertF80LongDoubleAPFloatToAPInt();
3101f4a2713aSLionel Sambuc }
3102f4a2713aSLionel Sambuc
3103f4a2713aSLionel Sambuc float
convertToFloat() const3104f4a2713aSLionel Sambuc APFloat::convertToFloat() const
3105f4a2713aSLionel Sambuc {
3106f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&IEEEsingle &&
3107f4a2713aSLionel Sambuc "Float semantics are not IEEEsingle");
3108f4a2713aSLionel Sambuc APInt api = bitcastToAPInt();
3109f4a2713aSLionel Sambuc return api.bitsToFloat();
3110f4a2713aSLionel Sambuc }
3111f4a2713aSLionel Sambuc
3112f4a2713aSLionel Sambuc double
convertToDouble() const3113f4a2713aSLionel Sambuc APFloat::convertToDouble() const
3114f4a2713aSLionel Sambuc {
3115f4a2713aSLionel Sambuc assert(semantics == (const llvm::fltSemantics*)&IEEEdouble &&
3116f4a2713aSLionel Sambuc "Float semantics are not IEEEdouble");
3117f4a2713aSLionel Sambuc APInt api = bitcastToAPInt();
3118f4a2713aSLionel Sambuc return api.bitsToDouble();
3119f4a2713aSLionel Sambuc }
3120f4a2713aSLionel Sambuc
3121f4a2713aSLionel Sambuc /// Integer bit is explicit in this format. Intel hardware (387 and later)
3122f4a2713aSLionel Sambuc /// does not support these bit patterns:
3123f4a2713aSLionel Sambuc /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3124f4a2713aSLionel Sambuc /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3125f4a2713aSLionel Sambuc /// exponent = 0, integer bit 1 ("pseudodenormal")
3126f4a2713aSLionel Sambuc /// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3127f4a2713aSLionel Sambuc /// At the moment, the first two are treated as NaNs, the second two as Normal.
3128f4a2713aSLionel Sambuc void
initFromF80LongDoubleAPInt(const APInt & api)3129f4a2713aSLionel Sambuc APFloat::initFromF80LongDoubleAPInt(const APInt &api)
3130f4a2713aSLionel Sambuc {
3131f4a2713aSLionel Sambuc assert(api.getBitWidth()==80);
3132f4a2713aSLionel Sambuc uint64_t i1 = api.getRawData()[0];
3133f4a2713aSLionel Sambuc uint64_t i2 = api.getRawData()[1];
3134f4a2713aSLionel Sambuc uint64_t myexponent = (i2 & 0x7fff);
3135f4a2713aSLionel Sambuc uint64_t mysignificand = i1;
3136f4a2713aSLionel Sambuc
3137f4a2713aSLionel Sambuc initialize(&APFloat::x87DoubleExtended);
3138f4a2713aSLionel Sambuc assert(partCount()==2);
3139f4a2713aSLionel Sambuc
3140f4a2713aSLionel Sambuc sign = static_cast<unsigned int>(i2>>15);
3141f4a2713aSLionel Sambuc if (myexponent==0 && mysignificand==0) {
3142f4a2713aSLionel Sambuc // exponent, significand meaningless
3143f4a2713aSLionel Sambuc category = fcZero;
3144f4a2713aSLionel Sambuc } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3145f4a2713aSLionel Sambuc // exponent, significand meaningless
3146f4a2713aSLionel Sambuc category = fcInfinity;
3147f4a2713aSLionel Sambuc } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3148f4a2713aSLionel Sambuc // exponent meaningless
3149f4a2713aSLionel Sambuc category = fcNaN;
3150f4a2713aSLionel Sambuc significandParts()[0] = mysignificand;
3151f4a2713aSLionel Sambuc significandParts()[1] = 0;
3152f4a2713aSLionel Sambuc } else {
3153f4a2713aSLionel Sambuc category = fcNormal;
3154f4a2713aSLionel Sambuc exponent = myexponent - 16383;
3155f4a2713aSLionel Sambuc significandParts()[0] = mysignificand;
3156f4a2713aSLionel Sambuc significandParts()[1] = 0;
3157f4a2713aSLionel Sambuc if (myexponent==0) // denormal
3158f4a2713aSLionel Sambuc exponent = -16382;
3159f4a2713aSLionel Sambuc }
3160f4a2713aSLionel Sambuc }
3161f4a2713aSLionel Sambuc
3162f4a2713aSLionel Sambuc void
initFromPPCDoubleDoubleAPInt(const APInt & api)3163f4a2713aSLionel Sambuc APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
3164f4a2713aSLionel Sambuc {
3165f4a2713aSLionel Sambuc assert(api.getBitWidth()==128);
3166f4a2713aSLionel Sambuc uint64_t i1 = api.getRawData()[0];
3167f4a2713aSLionel Sambuc uint64_t i2 = api.getRawData()[1];
3168f4a2713aSLionel Sambuc opStatus fs;
3169f4a2713aSLionel Sambuc bool losesInfo;
3170f4a2713aSLionel Sambuc
3171f4a2713aSLionel Sambuc // Get the first double and convert to our format.
3172f4a2713aSLionel Sambuc initFromDoubleAPInt(APInt(64, i1));
3173f4a2713aSLionel Sambuc fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3174f4a2713aSLionel Sambuc assert(fs == opOK && !losesInfo);
3175f4a2713aSLionel Sambuc (void)fs;
3176f4a2713aSLionel Sambuc
3177f4a2713aSLionel Sambuc // Unless we have a special case, add in second double.
3178f4a2713aSLionel Sambuc if (isFiniteNonZero()) {
3179f4a2713aSLionel Sambuc APFloat v(IEEEdouble, APInt(64, i2));
3180f4a2713aSLionel Sambuc fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3181f4a2713aSLionel Sambuc assert(fs == opOK && !losesInfo);
3182f4a2713aSLionel Sambuc (void)fs;
3183f4a2713aSLionel Sambuc
3184f4a2713aSLionel Sambuc add(v, rmNearestTiesToEven);
3185f4a2713aSLionel Sambuc }
3186f4a2713aSLionel Sambuc }
3187f4a2713aSLionel Sambuc
3188f4a2713aSLionel Sambuc void
initFromQuadrupleAPInt(const APInt & api)3189f4a2713aSLionel Sambuc APFloat::initFromQuadrupleAPInt(const APInt &api)
3190f4a2713aSLionel Sambuc {
3191f4a2713aSLionel Sambuc assert(api.getBitWidth()==128);
3192f4a2713aSLionel Sambuc uint64_t i1 = api.getRawData()[0];
3193f4a2713aSLionel Sambuc uint64_t i2 = api.getRawData()[1];
3194f4a2713aSLionel Sambuc uint64_t myexponent = (i2 >> 48) & 0x7fff;
3195f4a2713aSLionel Sambuc uint64_t mysignificand = i1;
3196f4a2713aSLionel Sambuc uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3197f4a2713aSLionel Sambuc
3198f4a2713aSLionel Sambuc initialize(&APFloat::IEEEquad);
3199f4a2713aSLionel Sambuc assert(partCount()==2);
3200f4a2713aSLionel Sambuc
3201f4a2713aSLionel Sambuc sign = static_cast<unsigned int>(i2>>63);
3202f4a2713aSLionel Sambuc if (myexponent==0 &&
3203f4a2713aSLionel Sambuc (mysignificand==0 && mysignificand2==0)) {
3204f4a2713aSLionel Sambuc // exponent, significand meaningless
3205f4a2713aSLionel Sambuc category = fcZero;
3206f4a2713aSLionel Sambuc } else if (myexponent==0x7fff &&
3207f4a2713aSLionel Sambuc (mysignificand==0 && mysignificand2==0)) {
3208f4a2713aSLionel Sambuc // exponent, significand meaningless
3209f4a2713aSLionel Sambuc category = fcInfinity;
3210f4a2713aSLionel Sambuc } else if (myexponent==0x7fff &&
3211f4a2713aSLionel Sambuc (mysignificand!=0 || mysignificand2 !=0)) {
3212f4a2713aSLionel Sambuc // exponent meaningless
3213f4a2713aSLionel Sambuc category = fcNaN;
3214f4a2713aSLionel Sambuc significandParts()[0] = mysignificand;
3215f4a2713aSLionel Sambuc significandParts()[1] = mysignificand2;
3216f4a2713aSLionel Sambuc } else {
3217f4a2713aSLionel Sambuc category = fcNormal;
3218f4a2713aSLionel Sambuc exponent = myexponent - 16383;
3219f4a2713aSLionel Sambuc significandParts()[0] = mysignificand;
3220f4a2713aSLionel Sambuc significandParts()[1] = mysignificand2;
3221f4a2713aSLionel Sambuc if (myexponent==0) // denormal
3222f4a2713aSLionel Sambuc exponent = -16382;
3223f4a2713aSLionel Sambuc else
3224f4a2713aSLionel Sambuc significandParts()[1] |= 0x1000000000000LL; // integer bit
3225f4a2713aSLionel Sambuc }
3226f4a2713aSLionel Sambuc }
3227f4a2713aSLionel Sambuc
3228f4a2713aSLionel Sambuc void
initFromDoubleAPInt(const APInt & api)3229f4a2713aSLionel Sambuc APFloat::initFromDoubleAPInt(const APInt &api)
3230f4a2713aSLionel Sambuc {
3231f4a2713aSLionel Sambuc assert(api.getBitWidth()==64);
3232f4a2713aSLionel Sambuc uint64_t i = *api.getRawData();
3233f4a2713aSLionel Sambuc uint64_t myexponent = (i >> 52) & 0x7ff;
3234f4a2713aSLionel Sambuc uint64_t mysignificand = i & 0xfffffffffffffLL;
3235f4a2713aSLionel Sambuc
3236f4a2713aSLionel Sambuc initialize(&APFloat::IEEEdouble);
3237f4a2713aSLionel Sambuc assert(partCount()==1);
3238f4a2713aSLionel Sambuc
3239f4a2713aSLionel Sambuc sign = static_cast<unsigned int>(i>>63);
3240f4a2713aSLionel Sambuc if (myexponent==0 && mysignificand==0) {
3241f4a2713aSLionel Sambuc // exponent, significand meaningless
3242f4a2713aSLionel Sambuc category = fcZero;
3243f4a2713aSLionel Sambuc } else if (myexponent==0x7ff && mysignificand==0) {
3244f4a2713aSLionel Sambuc // exponent, significand meaningless
3245f4a2713aSLionel Sambuc category = fcInfinity;
3246f4a2713aSLionel Sambuc } else if (myexponent==0x7ff && mysignificand!=0) {
3247f4a2713aSLionel Sambuc // exponent meaningless
3248f4a2713aSLionel Sambuc category = fcNaN;
3249f4a2713aSLionel Sambuc *significandParts() = mysignificand;
3250f4a2713aSLionel Sambuc } else {
3251f4a2713aSLionel Sambuc category = fcNormal;
3252f4a2713aSLionel Sambuc exponent = myexponent - 1023;
3253f4a2713aSLionel Sambuc *significandParts() = mysignificand;
3254f4a2713aSLionel Sambuc if (myexponent==0) // denormal
3255f4a2713aSLionel Sambuc exponent = -1022;
3256f4a2713aSLionel Sambuc else
3257f4a2713aSLionel Sambuc *significandParts() |= 0x10000000000000LL; // integer bit
3258f4a2713aSLionel Sambuc }
3259f4a2713aSLionel Sambuc }
3260f4a2713aSLionel Sambuc
3261f4a2713aSLionel Sambuc void
initFromFloatAPInt(const APInt & api)3262f4a2713aSLionel Sambuc APFloat::initFromFloatAPInt(const APInt & api)
3263f4a2713aSLionel Sambuc {
3264f4a2713aSLionel Sambuc assert(api.getBitWidth()==32);
3265f4a2713aSLionel Sambuc uint32_t i = (uint32_t)*api.getRawData();
3266f4a2713aSLionel Sambuc uint32_t myexponent = (i >> 23) & 0xff;
3267f4a2713aSLionel Sambuc uint32_t mysignificand = i & 0x7fffff;
3268f4a2713aSLionel Sambuc
3269f4a2713aSLionel Sambuc initialize(&APFloat::IEEEsingle);
3270f4a2713aSLionel Sambuc assert(partCount()==1);
3271f4a2713aSLionel Sambuc
3272f4a2713aSLionel Sambuc sign = i >> 31;
3273f4a2713aSLionel Sambuc if (myexponent==0 && mysignificand==0) {
3274f4a2713aSLionel Sambuc // exponent, significand meaningless
3275f4a2713aSLionel Sambuc category = fcZero;
3276f4a2713aSLionel Sambuc } else if (myexponent==0xff && mysignificand==0) {
3277f4a2713aSLionel Sambuc // exponent, significand meaningless
3278f4a2713aSLionel Sambuc category = fcInfinity;
3279f4a2713aSLionel Sambuc } else if (myexponent==0xff && mysignificand!=0) {
3280f4a2713aSLionel Sambuc // sign, exponent, significand meaningless
3281f4a2713aSLionel Sambuc category = fcNaN;
3282f4a2713aSLionel Sambuc *significandParts() = mysignificand;
3283f4a2713aSLionel Sambuc } else {
3284f4a2713aSLionel Sambuc category = fcNormal;
3285f4a2713aSLionel Sambuc exponent = myexponent - 127; //bias
3286f4a2713aSLionel Sambuc *significandParts() = mysignificand;
3287f4a2713aSLionel Sambuc if (myexponent==0) // denormal
3288f4a2713aSLionel Sambuc exponent = -126;
3289f4a2713aSLionel Sambuc else
3290f4a2713aSLionel Sambuc *significandParts() |= 0x800000; // integer bit
3291f4a2713aSLionel Sambuc }
3292f4a2713aSLionel Sambuc }
3293f4a2713aSLionel Sambuc
3294f4a2713aSLionel Sambuc void
initFromHalfAPInt(const APInt & api)3295f4a2713aSLionel Sambuc APFloat::initFromHalfAPInt(const APInt & api)
3296f4a2713aSLionel Sambuc {
3297f4a2713aSLionel Sambuc assert(api.getBitWidth()==16);
3298f4a2713aSLionel Sambuc uint32_t i = (uint32_t)*api.getRawData();
3299f4a2713aSLionel Sambuc uint32_t myexponent = (i >> 10) & 0x1f;
3300f4a2713aSLionel Sambuc uint32_t mysignificand = i & 0x3ff;
3301f4a2713aSLionel Sambuc
3302f4a2713aSLionel Sambuc initialize(&APFloat::IEEEhalf);
3303f4a2713aSLionel Sambuc assert(partCount()==1);
3304f4a2713aSLionel Sambuc
3305f4a2713aSLionel Sambuc sign = i >> 15;
3306f4a2713aSLionel Sambuc if (myexponent==0 && mysignificand==0) {
3307f4a2713aSLionel Sambuc // exponent, significand meaningless
3308f4a2713aSLionel Sambuc category = fcZero;
3309f4a2713aSLionel Sambuc } else if (myexponent==0x1f && mysignificand==0) {
3310f4a2713aSLionel Sambuc // exponent, significand meaningless
3311f4a2713aSLionel Sambuc category = fcInfinity;
3312f4a2713aSLionel Sambuc } else if (myexponent==0x1f && mysignificand!=0) {
3313f4a2713aSLionel Sambuc // sign, exponent, significand meaningless
3314f4a2713aSLionel Sambuc category = fcNaN;
3315f4a2713aSLionel Sambuc *significandParts() = mysignificand;
3316f4a2713aSLionel Sambuc } else {
3317f4a2713aSLionel Sambuc category = fcNormal;
3318f4a2713aSLionel Sambuc exponent = myexponent - 15; //bias
3319f4a2713aSLionel Sambuc *significandParts() = mysignificand;
3320f4a2713aSLionel Sambuc if (myexponent==0) // denormal
3321f4a2713aSLionel Sambuc exponent = -14;
3322f4a2713aSLionel Sambuc else
3323f4a2713aSLionel Sambuc *significandParts() |= 0x400; // integer bit
3324f4a2713aSLionel Sambuc }
3325f4a2713aSLionel Sambuc }
3326f4a2713aSLionel Sambuc
3327f4a2713aSLionel Sambuc /// Treat api as containing the bits of a floating point number. Currently
3328f4a2713aSLionel Sambuc /// we infer the floating point type from the size of the APInt. The
3329f4a2713aSLionel Sambuc /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3330f4a2713aSLionel Sambuc /// when the size is anything else).
3331f4a2713aSLionel Sambuc void
initFromAPInt(const fltSemantics * Sem,const APInt & api)3332f4a2713aSLionel Sambuc APFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api)
3333f4a2713aSLionel Sambuc {
3334f4a2713aSLionel Sambuc if (Sem == &IEEEhalf)
3335f4a2713aSLionel Sambuc return initFromHalfAPInt(api);
3336f4a2713aSLionel Sambuc if (Sem == &IEEEsingle)
3337f4a2713aSLionel Sambuc return initFromFloatAPInt(api);
3338f4a2713aSLionel Sambuc if (Sem == &IEEEdouble)
3339f4a2713aSLionel Sambuc return initFromDoubleAPInt(api);
3340f4a2713aSLionel Sambuc if (Sem == &x87DoubleExtended)
3341f4a2713aSLionel Sambuc return initFromF80LongDoubleAPInt(api);
3342f4a2713aSLionel Sambuc if (Sem == &IEEEquad)
3343f4a2713aSLionel Sambuc return initFromQuadrupleAPInt(api);
3344f4a2713aSLionel Sambuc if (Sem == &PPCDoubleDouble)
3345f4a2713aSLionel Sambuc return initFromPPCDoubleDoubleAPInt(api);
3346f4a2713aSLionel Sambuc
3347*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
3348f4a2713aSLionel Sambuc }
3349f4a2713aSLionel Sambuc
3350f4a2713aSLionel Sambuc APFloat
getAllOnesValue(unsigned BitWidth,bool isIEEE)3351f4a2713aSLionel Sambuc APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
3352f4a2713aSLionel Sambuc {
3353f4a2713aSLionel Sambuc switch (BitWidth) {
3354f4a2713aSLionel Sambuc case 16:
3355f4a2713aSLionel Sambuc return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth));
3356f4a2713aSLionel Sambuc case 32:
3357f4a2713aSLionel Sambuc return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth));
3358f4a2713aSLionel Sambuc case 64:
3359f4a2713aSLionel Sambuc return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth));
3360f4a2713aSLionel Sambuc case 80:
3361f4a2713aSLionel Sambuc return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth));
3362f4a2713aSLionel Sambuc case 128:
3363f4a2713aSLionel Sambuc if (isIEEE)
3364f4a2713aSLionel Sambuc return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth));
3365f4a2713aSLionel Sambuc return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
3366f4a2713aSLionel Sambuc default:
3367f4a2713aSLionel Sambuc llvm_unreachable("Unknown floating bit width");
3368f4a2713aSLionel Sambuc }
3369f4a2713aSLionel Sambuc }
3370f4a2713aSLionel Sambuc
3371f4a2713aSLionel Sambuc /// Make this number the largest magnitude normal number in the given
3372f4a2713aSLionel Sambuc /// semantics.
makeLargest(bool Negative)3373f4a2713aSLionel Sambuc void APFloat::makeLargest(bool Negative) {
3374f4a2713aSLionel Sambuc // We want (in interchange format):
3375f4a2713aSLionel Sambuc // sign = {Negative}
3376f4a2713aSLionel Sambuc // exponent = 1..10
3377f4a2713aSLionel Sambuc // significand = 1..1
3378f4a2713aSLionel Sambuc category = fcNormal;
3379f4a2713aSLionel Sambuc sign = Negative;
3380f4a2713aSLionel Sambuc exponent = semantics->maxExponent;
3381f4a2713aSLionel Sambuc
3382f4a2713aSLionel Sambuc // Use memset to set all but the highest integerPart to all ones.
3383f4a2713aSLionel Sambuc integerPart *significand = significandParts();
3384f4a2713aSLionel Sambuc unsigned PartCount = partCount();
3385f4a2713aSLionel Sambuc memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
3386f4a2713aSLionel Sambuc
3387f4a2713aSLionel Sambuc // Set the high integerPart especially setting all unused top bits for
3388f4a2713aSLionel Sambuc // internal consistency.
3389f4a2713aSLionel Sambuc const unsigned NumUnusedHighBits =
3390f4a2713aSLionel Sambuc PartCount*integerPartWidth - semantics->precision;
3391*0a6a1f1dSLionel Sambuc significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3392*0a6a1f1dSLionel Sambuc ? (~integerPart(0) >> NumUnusedHighBits)
3393*0a6a1f1dSLionel Sambuc : 0;
3394f4a2713aSLionel Sambuc }
3395f4a2713aSLionel Sambuc
3396f4a2713aSLionel Sambuc /// Make this number the smallest magnitude denormal number in the given
3397f4a2713aSLionel Sambuc /// semantics.
makeSmallest(bool Negative)3398f4a2713aSLionel Sambuc void APFloat::makeSmallest(bool Negative) {
3399f4a2713aSLionel Sambuc // We want (in interchange format):
3400f4a2713aSLionel Sambuc // sign = {Negative}
3401f4a2713aSLionel Sambuc // exponent = 0..0
3402f4a2713aSLionel Sambuc // significand = 0..01
3403f4a2713aSLionel Sambuc category = fcNormal;
3404f4a2713aSLionel Sambuc sign = Negative;
3405f4a2713aSLionel Sambuc exponent = semantics->minExponent;
3406f4a2713aSLionel Sambuc APInt::tcSet(significandParts(), 1, partCount());
3407f4a2713aSLionel Sambuc }
3408f4a2713aSLionel Sambuc
3409f4a2713aSLionel Sambuc
getLargest(const fltSemantics & Sem,bool Negative)3410f4a2713aSLionel Sambuc APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
3411f4a2713aSLionel Sambuc // We want (in interchange format):
3412f4a2713aSLionel Sambuc // sign = {Negative}
3413f4a2713aSLionel Sambuc // exponent = 1..10
3414f4a2713aSLionel Sambuc // significand = 1..1
3415f4a2713aSLionel Sambuc APFloat Val(Sem, uninitialized);
3416f4a2713aSLionel Sambuc Val.makeLargest(Negative);
3417f4a2713aSLionel Sambuc return Val;
3418f4a2713aSLionel Sambuc }
3419f4a2713aSLionel Sambuc
getSmallest(const fltSemantics & Sem,bool Negative)3420f4a2713aSLionel Sambuc APFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) {
3421f4a2713aSLionel Sambuc // We want (in interchange format):
3422f4a2713aSLionel Sambuc // sign = {Negative}
3423f4a2713aSLionel Sambuc // exponent = 0..0
3424f4a2713aSLionel Sambuc // significand = 0..01
3425f4a2713aSLionel Sambuc APFloat Val(Sem, uninitialized);
3426f4a2713aSLionel Sambuc Val.makeSmallest(Negative);
3427f4a2713aSLionel Sambuc return Val;
3428f4a2713aSLionel Sambuc }
3429f4a2713aSLionel Sambuc
getSmallestNormalized(const fltSemantics & Sem,bool Negative)3430f4a2713aSLionel Sambuc APFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) {
3431f4a2713aSLionel Sambuc APFloat Val(Sem, uninitialized);
3432f4a2713aSLionel Sambuc
3433f4a2713aSLionel Sambuc // We want (in interchange format):
3434f4a2713aSLionel Sambuc // sign = {Negative}
3435f4a2713aSLionel Sambuc // exponent = 0..0
3436f4a2713aSLionel Sambuc // significand = 10..0
3437f4a2713aSLionel Sambuc
3438f4a2713aSLionel Sambuc Val.category = fcNormal;
3439f4a2713aSLionel Sambuc Val.zeroSignificand();
3440f4a2713aSLionel Sambuc Val.sign = Negative;
3441f4a2713aSLionel Sambuc Val.exponent = Sem.minExponent;
3442f4a2713aSLionel Sambuc Val.significandParts()[partCountForBits(Sem.precision)-1] |=
3443f4a2713aSLionel Sambuc (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth));
3444f4a2713aSLionel Sambuc
3445f4a2713aSLionel Sambuc return Val;
3446f4a2713aSLionel Sambuc }
3447f4a2713aSLionel Sambuc
APFloat(const fltSemantics & Sem,const APInt & API)3448f4a2713aSLionel Sambuc APFloat::APFloat(const fltSemantics &Sem, const APInt &API) {
3449f4a2713aSLionel Sambuc initFromAPInt(&Sem, API);
3450f4a2713aSLionel Sambuc }
3451f4a2713aSLionel Sambuc
APFloat(float f)3452f4a2713aSLionel Sambuc APFloat::APFloat(float f) {
3453f4a2713aSLionel Sambuc initFromAPInt(&IEEEsingle, APInt::floatToBits(f));
3454f4a2713aSLionel Sambuc }
3455f4a2713aSLionel Sambuc
APFloat(double d)3456f4a2713aSLionel Sambuc APFloat::APFloat(double d) {
3457f4a2713aSLionel Sambuc initFromAPInt(&IEEEdouble, APInt::doubleToBits(d));
3458f4a2713aSLionel Sambuc }
3459f4a2713aSLionel Sambuc
3460f4a2713aSLionel Sambuc namespace {
append(SmallVectorImpl<char> & Buffer,StringRef Str)3461f4a2713aSLionel Sambuc void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3462f4a2713aSLionel Sambuc Buffer.append(Str.begin(), Str.end());
3463f4a2713aSLionel Sambuc }
3464f4a2713aSLionel Sambuc
3465f4a2713aSLionel Sambuc /// Removes data from the given significand until it is no more
3466f4a2713aSLionel Sambuc /// precise than is required for the desired precision.
AdjustToPrecision(APInt & significand,int & exp,unsigned FormatPrecision)3467f4a2713aSLionel Sambuc void AdjustToPrecision(APInt &significand,
3468f4a2713aSLionel Sambuc int &exp, unsigned FormatPrecision) {
3469f4a2713aSLionel Sambuc unsigned bits = significand.getActiveBits();
3470f4a2713aSLionel Sambuc
3471f4a2713aSLionel Sambuc // 196/59 is a very slight overestimate of lg_2(10).
3472f4a2713aSLionel Sambuc unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3473f4a2713aSLionel Sambuc
3474f4a2713aSLionel Sambuc if (bits <= bitsRequired) return;
3475f4a2713aSLionel Sambuc
3476f4a2713aSLionel Sambuc unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3477f4a2713aSLionel Sambuc if (!tensRemovable) return;
3478f4a2713aSLionel Sambuc
3479f4a2713aSLionel Sambuc exp += tensRemovable;
3480f4a2713aSLionel Sambuc
3481f4a2713aSLionel Sambuc APInt divisor(significand.getBitWidth(), 1);
3482f4a2713aSLionel Sambuc APInt powten(significand.getBitWidth(), 10);
3483f4a2713aSLionel Sambuc while (true) {
3484f4a2713aSLionel Sambuc if (tensRemovable & 1)
3485f4a2713aSLionel Sambuc divisor *= powten;
3486f4a2713aSLionel Sambuc tensRemovable >>= 1;
3487f4a2713aSLionel Sambuc if (!tensRemovable) break;
3488f4a2713aSLionel Sambuc powten *= powten;
3489f4a2713aSLionel Sambuc }
3490f4a2713aSLionel Sambuc
3491f4a2713aSLionel Sambuc significand = significand.udiv(divisor);
3492f4a2713aSLionel Sambuc
3493f4a2713aSLionel Sambuc // Truncate the significand down to its active bit count.
3494f4a2713aSLionel Sambuc significand = significand.trunc(significand.getActiveBits());
3495f4a2713aSLionel Sambuc }
3496f4a2713aSLionel Sambuc
3497f4a2713aSLionel Sambuc
AdjustToPrecision(SmallVectorImpl<char> & buffer,int & exp,unsigned FormatPrecision)3498f4a2713aSLionel Sambuc void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3499f4a2713aSLionel Sambuc int &exp, unsigned FormatPrecision) {
3500f4a2713aSLionel Sambuc unsigned N = buffer.size();
3501f4a2713aSLionel Sambuc if (N <= FormatPrecision) return;
3502f4a2713aSLionel Sambuc
3503f4a2713aSLionel Sambuc // The most significant figures are the last ones in the buffer.
3504f4a2713aSLionel Sambuc unsigned FirstSignificant = N - FormatPrecision;
3505f4a2713aSLionel Sambuc
3506f4a2713aSLionel Sambuc // Round.
3507f4a2713aSLionel Sambuc // FIXME: this probably shouldn't use 'round half up'.
3508f4a2713aSLionel Sambuc
3509f4a2713aSLionel Sambuc // Rounding down is just a truncation, except we also want to drop
3510f4a2713aSLionel Sambuc // trailing zeros from the new result.
3511f4a2713aSLionel Sambuc if (buffer[FirstSignificant - 1] < '5') {
3512f4a2713aSLionel Sambuc while (FirstSignificant < N && buffer[FirstSignificant] == '0')
3513f4a2713aSLionel Sambuc FirstSignificant++;
3514f4a2713aSLionel Sambuc
3515f4a2713aSLionel Sambuc exp += FirstSignificant;
3516f4a2713aSLionel Sambuc buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3517f4a2713aSLionel Sambuc return;
3518f4a2713aSLionel Sambuc }
3519f4a2713aSLionel Sambuc
3520f4a2713aSLionel Sambuc // Rounding up requires a decimal add-with-carry. If we continue
3521f4a2713aSLionel Sambuc // the carry, the newly-introduced zeros will just be truncated.
3522f4a2713aSLionel Sambuc for (unsigned I = FirstSignificant; I != N; ++I) {
3523f4a2713aSLionel Sambuc if (buffer[I] == '9') {
3524f4a2713aSLionel Sambuc FirstSignificant++;
3525f4a2713aSLionel Sambuc } else {
3526f4a2713aSLionel Sambuc buffer[I]++;
3527f4a2713aSLionel Sambuc break;
3528f4a2713aSLionel Sambuc }
3529f4a2713aSLionel Sambuc }
3530f4a2713aSLionel Sambuc
3531f4a2713aSLionel Sambuc // If we carried through, we have exactly one digit of precision.
3532f4a2713aSLionel Sambuc if (FirstSignificant == N) {
3533f4a2713aSLionel Sambuc exp += FirstSignificant;
3534f4a2713aSLionel Sambuc buffer.clear();
3535f4a2713aSLionel Sambuc buffer.push_back('1');
3536f4a2713aSLionel Sambuc return;
3537f4a2713aSLionel Sambuc }
3538f4a2713aSLionel Sambuc
3539f4a2713aSLionel Sambuc exp += FirstSignificant;
3540f4a2713aSLionel Sambuc buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3541f4a2713aSLionel Sambuc }
3542f4a2713aSLionel Sambuc }
3543f4a2713aSLionel Sambuc
toString(SmallVectorImpl<char> & Str,unsigned FormatPrecision,unsigned FormatMaxPadding) const3544f4a2713aSLionel Sambuc void APFloat::toString(SmallVectorImpl<char> &Str,
3545f4a2713aSLionel Sambuc unsigned FormatPrecision,
3546f4a2713aSLionel Sambuc unsigned FormatMaxPadding) const {
3547f4a2713aSLionel Sambuc switch (category) {
3548f4a2713aSLionel Sambuc case fcInfinity:
3549f4a2713aSLionel Sambuc if (isNegative())
3550f4a2713aSLionel Sambuc return append(Str, "-Inf");
3551f4a2713aSLionel Sambuc else
3552f4a2713aSLionel Sambuc return append(Str, "+Inf");
3553f4a2713aSLionel Sambuc
3554f4a2713aSLionel Sambuc case fcNaN: return append(Str, "NaN");
3555f4a2713aSLionel Sambuc
3556f4a2713aSLionel Sambuc case fcZero:
3557f4a2713aSLionel Sambuc if (isNegative())
3558f4a2713aSLionel Sambuc Str.push_back('-');
3559f4a2713aSLionel Sambuc
3560f4a2713aSLionel Sambuc if (!FormatMaxPadding)
3561f4a2713aSLionel Sambuc append(Str, "0.0E+0");
3562f4a2713aSLionel Sambuc else
3563f4a2713aSLionel Sambuc Str.push_back('0');
3564f4a2713aSLionel Sambuc return;
3565f4a2713aSLionel Sambuc
3566f4a2713aSLionel Sambuc case fcNormal:
3567f4a2713aSLionel Sambuc break;
3568f4a2713aSLionel Sambuc }
3569f4a2713aSLionel Sambuc
3570f4a2713aSLionel Sambuc if (isNegative())
3571f4a2713aSLionel Sambuc Str.push_back('-');
3572f4a2713aSLionel Sambuc
3573f4a2713aSLionel Sambuc // Decompose the number into an APInt and an exponent.
3574f4a2713aSLionel Sambuc int exp = exponent - ((int) semantics->precision - 1);
3575f4a2713aSLionel Sambuc APInt significand(semantics->precision,
3576f4a2713aSLionel Sambuc makeArrayRef(significandParts(),
3577f4a2713aSLionel Sambuc partCountForBits(semantics->precision)));
3578f4a2713aSLionel Sambuc
3579f4a2713aSLionel Sambuc // Set FormatPrecision if zero. We want to do this before we
3580f4a2713aSLionel Sambuc // truncate trailing zeros, as those are part of the precision.
3581f4a2713aSLionel Sambuc if (!FormatPrecision) {
3582f4a2713aSLionel Sambuc // We use enough digits so the number can be round-tripped back to an
3583f4a2713aSLionel Sambuc // APFloat. The formula comes from "How to Print Floating-Point Numbers
3584f4a2713aSLionel Sambuc // Accurately" by Steele and White.
3585f4a2713aSLionel Sambuc // FIXME: Using a formula based purely on the precision is conservative;
3586f4a2713aSLionel Sambuc // we can print fewer digits depending on the actual value being printed.
3587f4a2713aSLionel Sambuc
3588f4a2713aSLionel Sambuc // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3589f4a2713aSLionel Sambuc FormatPrecision = 2 + semantics->precision * 59 / 196;
3590f4a2713aSLionel Sambuc }
3591f4a2713aSLionel Sambuc
3592f4a2713aSLionel Sambuc // Ignore trailing binary zeros.
3593f4a2713aSLionel Sambuc int trailingZeros = significand.countTrailingZeros();
3594f4a2713aSLionel Sambuc exp += trailingZeros;
3595f4a2713aSLionel Sambuc significand = significand.lshr(trailingZeros);
3596f4a2713aSLionel Sambuc
3597f4a2713aSLionel Sambuc // Change the exponent from 2^e to 10^e.
3598f4a2713aSLionel Sambuc if (exp == 0) {
3599f4a2713aSLionel Sambuc // Nothing to do.
3600f4a2713aSLionel Sambuc } else if (exp > 0) {
3601f4a2713aSLionel Sambuc // Just shift left.
3602f4a2713aSLionel Sambuc significand = significand.zext(semantics->precision + exp);
3603f4a2713aSLionel Sambuc significand <<= exp;
3604f4a2713aSLionel Sambuc exp = 0;
3605f4a2713aSLionel Sambuc } else { /* exp < 0 */
3606f4a2713aSLionel Sambuc int texp = -exp;
3607f4a2713aSLionel Sambuc
3608f4a2713aSLionel Sambuc // We transform this using the identity:
3609f4a2713aSLionel Sambuc // (N)(2^-e) == (N)(5^e)(10^-e)
3610f4a2713aSLionel Sambuc // This means we have to multiply N (the significand) by 5^e.
3611f4a2713aSLionel Sambuc // To avoid overflow, we have to operate on numbers large
3612f4a2713aSLionel Sambuc // enough to store N * 5^e:
3613f4a2713aSLionel Sambuc // log2(N * 5^e) == log2(N) + e * log2(5)
3614f4a2713aSLionel Sambuc // <= semantics->precision + e * 137 / 59
3615f4a2713aSLionel Sambuc // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
3616f4a2713aSLionel Sambuc
3617f4a2713aSLionel Sambuc unsigned precision = semantics->precision + (137 * texp + 136) / 59;
3618f4a2713aSLionel Sambuc
3619f4a2713aSLionel Sambuc // Multiply significand by 5^e.
3620f4a2713aSLionel Sambuc // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
3621f4a2713aSLionel Sambuc significand = significand.zext(precision);
3622f4a2713aSLionel Sambuc APInt five_to_the_i(precision, 5);
3623f4a2713aSLionel Sambuc while (true) {
3624f4a2713aSLionel Sambuc if (texp & 1) significand *= five_to_the_i;
3625f4a2713aSLionel Sambuc
3626f4a2713aSLionel Sambuc texp >>= 1;
3627f4a2713aSLionel Sambuc if (!texp) break;
3628f4a2713aSLionel Sambuc five_to_the_i *= five_to_the_i;
3629f4a2713aSLionel Sambuc }
3630f4a2713aSLionel Sambuc }
3631f4a2713aSLionel Sambuc
3632f4a2713aSLionel Sambuc AdjustToPrecision(significand, exp, FormatPrecision);
3633f4a2713aSLionel Sambuc
3634f4a2713aSLionel Sambuc SmallVector<char, 256> buffer;
3635f4a2713aSLionel Sambuc
3636f4a2713aSLionel Sambuc // Fill the buffer.
3637f4a2713aSLionel Sambuc unsigned precision = significand.getBitWidth();
3638f4a2713aSLionel Sambuc APInt ten(precision, 10);
3639f4a2713aSLionel Sambuc APInt digit(precision, 0);
3640f4a2713aSLionel Sambuc
3641f4a2713aSLionel Sambuc bool inTrail = true;
3642f4a2713aSLionel Sambuc while (significand != 0) {
3643f4a2713aSLionel Sambuc // digit <- significand % 10
3644f4a2713aSLionel Sambuc // significand <- significand / 10
3645f4a2713aSLionel Sambuc APInt::udivrem(significand, ten, significand, digit);
3646f4a2713aSLionel Sambuc
3647f4a2713aSLionel Sambuc unsigned d = digit.getZExtValue();
3648f4a2713aSLionel Sambuc
3649f4a2713aSLionel Sambuc // Drop trailing zeros.
3650f4a2713aSLionel Sambuc if (inTrail && !d) exp++;
3651f4a2713aSLionel Sambuc else {
3652f4a2713aSLionel Sambuc buffer.push_back((char) ('0' + d));
3653f4a2713aSLionel Sambuc inTrail = false;
3654f4a2713aSLionel Sambuc }
3655f4a2713aSLionel Sambuc }
3656f4a2713aSLionel Sambuc
3657f4a2713aSLionel Sambuc assert(!buffer.empty() && "no characters in buffer!");
3658f4a2713aSLionel Sambuc
3659f4a2713aSLionel Sambuc // Drop down to FormatPrecision.
3660f4a2713aSLionel Sambuc // TODO: don't do more precise calculations above than are required.
3661f4a2713aSLionel Sambuc AdjustToPrecision(buffer, exp, FormatPrecision);
3662f4a2713aSLionel Sambuc
3663f4a2713aSLionel Sambuc unsigned NDigits = buffer.size();
3664f4a2713aSLionel Sambuc
3665f4a2713aSLionel Sambuc // Check whether we should use scientific notation.
3666f4a2713aSLionel Sambuc bool FormatScientific;
3667f4a2713aSLionel Sambuc if (!FormatMaxPadding)
3668f4a2713aSLionel Sambuc FormatScientific = true;
3669f4a2713aSLionel Sambuc else {
3670f4a2713aSLionel Sambuc if (exp >= 0) {
3671f4a2713aSLionel Sambuc // 765e3 --> 765000
3672f4a2713aSLionel Sambuc // ^^^
3673f4a2713aSLionel Sambuc // But we shouldn't make the number look more precise than it is.
3674f4a2713aSLionel Sambuc FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3675f4a2713aSLionel Sambuc NDigits + (unsigned) exp > FormatPrecision);
3676f4a2713aSLionel Sambuc } else {
3677f4a2713aSLionel Sambuc // Power of the most significant digit.
3678f4a2713aSLionel Sambuc int MSD = exp + (int) (NDigits - 1);
3679f4a2713aSLionel Sambuc if (MSD >= 0) {
3680f4a2713aSLionel Sambuc // 765e-2 == 7.65
3681f4a2713aSLionel Sambuc FormatScientific = false;
3682f4a2713aSLionel Sambuc } else {
3683f4a2713aSLionel Sambuc // 765e-5 == 0.00765
3684f4a2713aSLionel Sambuc // ^ ^^
3685f4a2713aSLionel Sambuc FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
3686f4a2713aSLionel Sambuc }
3687f4a2713aSLionel Sambuc }
3688f4a2713aSLionel Sambuc }
3689f4a2713aSLionel Sambuc
3690f4a2713aSLionel Sambuc // Scientific formatting is pretty straightforward.
3691f4a2713aSLionel Sambuc if (FormatScientific) {
3692f4a2713aSLionel Sambuc exp += (NDigits - 1);
3693f4a2713aSLionel Sambuc
3694f4a2713aSLionel Sambuc Str.push_back(buffer[NDigits-1]);
3695f4a2713aSLionel Sambuc Str.push_back('.');
3696f4a2713aSLionel Sambuc if (NDigits == 1)
3697f4a2713aSLionel Sambuc Str.push_back('0');
3698f4a2713aSLionel Sambuc else
3699f4a2713aSLionel Sambuc for (unsigned I = 1; I != NDigits; ++I)
3700f4a2713aSLionel Sambuc Str.push_back(buffer[NDigits-1-I]);
3701f4a2713aSLionel Sambuc Str.push_back('E');
3702f4a2713aSLionel Sambuc
3703f4a2713aSLionel Sambuc Str.push_back(exp >= 0 ? '+' : '-');
3704f4a2713aSLionel Sambuc if (exp < 0) exp = -exp;
3705f4a2713aSLionel Sambuc SmallVector<char, 6> expbuf;
3706f4a2713aSLionel Sambuc do {
3707f4a2713aSLionel Sambuc expbuf.push_back((char) ('0' + (exp % 10)));
3708f4a2713aSLionel Sambuc exp /= 10;
3709f4a2713aSLionel Sambuc } while (exp);
3710f4a2713aSLionel Sambuc for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3711f4a2713aSLionel Sambuc Str.push_back(expbuf[E-1-I]);
3712f4a2713aSLionel Sambuc return;
3713f4a2713aSLionel Sambuc }
3714f4a2713aSLionel Sambuc
3715f4a2713aSLionel Sambuc // Non-scientific, positive exponents.
3716f4a2713aSLionel Sambuc if (exp >= 0) {
3717f4a2713aSLionel Sambuc for (unsigned I = 0; I != NDigits; ++I)
3718f4a2713aSLionel Sambuc Str.push_back(buffer[NDigits-1-I]);
3719f4a2713aSLionel Sambuc for (unsigned I = 0; I != (unsigned) exp; ++I)
3720f4a2713aSLionel Sambuc Str.push_back('0');
3721f4a2713aSLionel Sambuc return;
3722f4a2713aSLionel Sambuc }
3723f4a2713aSLionel Sambuc
3724f4a2713aSLionel Sambuc // Non-scientific, negative exponents.
3725f4a2713aSLionel Sambuc
3726f4a2713aSLionel Sambuc // The number of digits to the left of the decimal point.
3727f4a2713aSLionel Sambuc int NWholeDigits = exp + (int) NDigits;
3728f4a2713aSLionel Sambuc
3729f4a2713aSLionel Sambuc unsigned I = 0;
3730f4a2713aSLionel Sambuc if (NWholeDigits > 0) {
3731f4a2713aSLionel Sambuc for (; I != (unsigned) NWholeDigits; ++I)
3732f4a2713aSLionel Sambuc Str.push_back(buffer[NDigits-I-1]);
3733f4a2713aSLionel Sambuc Str.push_back('.');
3734f4a2713aSLionel Sambuc } else {
3735f4a2713aSLionel Sambuc unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3736f4a2713aSLionel Sambuc
3737f4a2713aSLionel Sambuc Str.push_back('0');
3738f4a2713aSLionel Sambuc Str.push_back('.');
3739f4a2713aSLionel Sambuc for (unsigned Z = 1; Z != NZeros; ++Z)
3740f4a2713aSLionel Sambuc Str.push_back('0');
3741f4a2713aSLionel Sambuc }
3742f4a2713aSLionel Sambuc
3743f4a2713aSLionel Sambuc for (; I != NDigits; ++I)
3744f4a2713aSLionel Sambuc Str.push_back(buffer[NDigits-I-1]);
3745f4a2713aSLionel Sambuc }
3746f4a2713aSLionel Sambuc
getExactInverse(APFloat * inv) const3747f4a2713aSLionel Sambuc bool APFloat::getExactInverse(APFloat *inv) const {
3748f4a2713aSLionel Sambuc // Special floats and denormals have no exact inverse.
3749f4a2713aSLionel Sambuc if (!isFiniteNonZero())
3750f4a2713aSLionel Sambuc return false;
3751f4a2713aSLionel Sambuc
3752f4a2713aSLionel Sambuc // Check that the number is a power of two by making sure that only the
3753f4a2713aSLionel Sambuc // integer bit is set in the significand.
3754f4a2713aSLionel Sambuc if (significandLSB() != semantics->precision - 1)
3755f4a2713aSLionel Sambuc return false;
3756f4a2713aSLionel Sambuc
3757f4a2713aSLionel Sambuc // Get the inverse.
3758f4a2713aSLionel Sambuc APFloat reciprocal(*semantics, 1ULL);
3759f4a2713aSLionel Sambuc if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3760f4a2713aSLionel Sambuc return false;
3761f4a2713aSLionel Sambuc
3762f4a2713aSLionel Sambuc // Avoid multiplication with a denormal, it is not safe on all platforms and
3763f4a2713aSLionel Sambuc // may be slower than a normal division.
3764f4a2713aSLionel Sambuc if (reciprocal.isDenormal())
3765f4a2713aSLionel Sambuc return false;
3766f4a2713aSLionel Sambuc
3767f4a2713aSLionel Sambuc assert(reciprocal.isFiniteNonZero() &&
3768f4a2713aSLionel Sambuc reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3769f4a2713aSLionel Sambuc
3770f4a2713aSLionel Sambuc if (inv)
3771f4a2713aSLionel Sambuc *inv = reciprocal;
3772f4a2713aSLionel Sambuc
3773f4a2713aSLionel Sambuc return true;
3774f4a2713aSLionel Sambuc }
3775f4a2713aSLionel Sambuc
isSignaling() const3776f4a2713aSLionel Sambuc bool APFloat::isSignaling() const {
3777f4a2713aSLionel Sambuc if (!isNaN())
3778f4a2713aSLionel Sambuc return false;
3779f4a2713aSLionel Sambuc
3780f4a2713aSLionel Sambuc // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3781f4a2713aSLionel Sambuc // first bit of the trailing significand being 0.
3782f4a2713aSLionel Sambuc return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3783f4a2713aSLionel Sambuc }
3784f4a2713aSLionel Sambuc
3785f4a2713aSLionel Sambuc /// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3786f4a2713aSLionel Sambuc ///
3787f4a2713aSLionel Sambuc /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3788f4a2713aSLionel Sambuc /// appropriate sign switching before/after the computation.
next(bool nextDown)3789f4a2713aSLionel Sambuc APFloat::opStatus APFloat::next(bool nextDown) {
3790f4a2713aSLionel Sambuc // If we are performing nextDown, swap sign so we have -x.
3791f4a2713aSLionel Sambuc if (nextDown)
3792f4a2713aSLionel Sambuc changeSign();
3793f4a2713aSLionel Sambuc
3794f4a2713aSLionel Sambuc // Compute nextUp(x)
3795f4a2713aSLionel Sambuc opStatus result = opOK;
3796f4a2713aSLionel Sambuc
3797f4a2713aSLionel Sambuc // Handle each float category separately.
3798f4a2713aSLionel Sambuc switch (category) {
3799f4a2713aSLionel Sambuc case fcInfinity:
3800f4a2713aSLionel Sambuc // nextUp(+inf) = +inf
3801f4a2713aSLionel Sambuc if (!isNegative())
3802f4a2713aSLionel Sambuc break;
3803f4a2713aSLionel Sambuc // nextUp(-inf) = -getLargest()
3804f4a2713aSLionel Sambuc makeLargest(true);
3805f4a2713aSLionel Sambuc break;
3806f4a2713aSLionel Sambuc case fcNaN:
3807f4a2713aSLionel Sambuc // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3808f4a2713aSLionel Sambuc // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3809f4a2713aSLionel Sambuc // change the payload.
3810f4a2713aSLionel Sambuc if (isSignaling()) {
3811f4a2713aSLionel Sambuc result = opInvalidOp;
3812*0a6a1f1dSLionel Sambuc // For consistency, propagate the sign of the sNaN to the qNaN.
3813*0a6a1f1dSLionel Sambuc makeNaN(false, isNegative(), nullptr);
3814f4a2713aSLionel Sambuc }
3815f4a2713aSLionel Sambuc break;
3816f4a2713aSLionel Sambuc case fcZero:
3817f4a2713aSLionel Sambuc // nextUp(pm 0) = +getSmallest()
3818f4a2713aSLionel Sambuc makeSmallest(false);
3819f4a2713aSLionel Sambuc break;
3820f4a2713aSLionel Sambuc case fcNormal:
3821f4a2713aSLionel Sambuc // nextUp(-getSmallest()) = -0
3822f4a2713aSLionel Sambuc if (isSmallest() && isNegative()) {
3823f4a2713aSLionel Sambuc APInt::tcSet(significandParts(), 0, partCount());
3824f4a2713aSLionel Sambuc category = fcZero;
3825f4a2713aSLionel Sambuc exponent = 0;
3826f4a2713aSLionel Sambuc break;
3827f4a2713aSLionel Sambuc }
3828f4a2713aSLionel Sambuc
3829f4a2713aSLionel Sambuc // nextUp(getLargest()) == INFINITY
3830f4a2713aSLionel Sambuc if (isLargest() && !isNegative()) {
3831f4a2713aSLionel Sambuc APInt::tcSet(significandParts(), 0, partCount());
3832f4a2713aSLionel Sambuc category = fcInfinity;
3833f4a2713aSLionel Sambuc exponent = semantics->maxExponent + 1;
3834f4a2713aSLionel Sambuc break;
3835f4a2713aSLionel Sambuc }
3836f4a2713aSLionel Sambuc
3837f4a2713aSLionel Sambuc // nextUp(normal) == normal + inc.
3838f4a2713aSLionel Sambuc if (isNegative()) {
3839f4a2713aSLionel Sambuc // If we are negative, we need to decrement the significand.
3840f4a2713aSLionel Sambuc
3841f4a2713aSLionel Sambuc // We only cross a binade boundary that requires adjusting the exponent
3842f4a2713aSLionel Sambuc // if:
3843f4a2713aSLionel Sambuc // 1. exponent != semantics->minExponent. This implies we are not in the
3844f4a2713aSLionel Sambuc // smallest binade or are dealing with denormals.
3845f4a2713aSLionel Sambuc // 2. Our significand excluding the integral bit is all zeros.
3846f4a2713aSLionel Sambuc bool WillCrossBinadeBoundary =
3847f4a2713aSLionel Sambuc exponent != semantics->minExponent && isSignificandAllZeros();
3848f4a2713aSLionel Sambuc
3849f4a2713aSLionel Sambuc // Decrement the significand.
3850f4a2713aSLionel Sambuc //
3851f4a2713aSLionel Sambuc // We always do this since:
3852*0a6a1f1dSLionel Sambuc // 1. If we are dealing with a non-binade decrement, by definition we
3853f4a2713aSLionel Sambuc // just decrement the significand.
3854f4a2713aSLionel Sambuc // 2. If we are dealing with a normal -> normal binade decrement, since
3855f4a2713aSLionel Sambuc // we have an explicit integral bit the fact that all bits but the
3856f4a2713aSLionel Sambuc // integral bit are zero implies that subtracting one will yield a
3857f4a2713aSLionel Sambuc // significand with 0 integral bit and 1 in all other spots. Thus we
3858f4a2713aSLionel Sambuc // must just adjust the exponent and set the integral bit to 1.
3859f4a2713aSLionel Sambuc // 3. If we are dealing with a normal -> denormal binade decrement,
3860f4a2713aSLionel Sambuc // since we set the integral bit to 0 when we represent denormals, we
3861f4a2713aSLionel Sambuc // just decrement the significand.
3862f4a2713aSLionel Sambuc integerPart *Parts = significandParts();
3863f4a2713aSLionel Sambuc APInt::tcDecrement(Parts, partCount());
3864f4a2713aSLionel Sambuc
3865f4a2713aSLionel Sambuc if (WillCrossBinadeBoundary) {
3866f4a2713aSLionel Sambuc // Our result is a normal number. Do the following:
3867f4a2713aSLionel Sambuc // 1. Set the integral bit to 1.
3868f4a2713aSLionel Sambuc // 2. Decrement the exponent.
3869f4a2713aSLionel Sambuc APInt::tcSetBit(Parts, semantics->precision - 1);
3870f4a2713aSLionel Sambuc exponent--;
3871f4a2713aSLionel Sambuc }
3872f4a2713aSLionel Sambuc } else {
3873f4a2713aSLionel Sambuc // If we are positive, we need to increment the significand.
3874f4a2713aSLionel Sambuc
3875f4a2713aSLionel Sambuc // We only cross a binade boundary that requires adjusting the exponent if
3876f4a2713aSLionel Sambuc // the input is not a denormal and all of said input's significand bits
3877f4a2713aSLionel Sambuc // are set. If all of said conditions are true: clear the significand, set
3878f4a2713aSLionel Sambuc // the integral bit to 1, and increment the exponent. If we have a
3879f4a2713aSLionel Sambuc // denormal always increment since moving denormals and the numbers in the
3880f4a2713aSLionel Sambuc // smallest normal binade have the same exponent in our representation.
3881f4a2713aSLionel Sambuc bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3882f4a2713aSLionel Sambuc
3883f4a2713aSLionel Sambuc if (WillCrossBinadeBoundary) {
3884f4a2713aSLionel Sambuc integerPart *Parts = significandParts();
3885f4a2713aSLionel Sambuc APInt::tcSet(Parts, 0, partCount());
3886f4a2713aSLionel Sambuc APInt::tcSetBit(Parts, semantics->precision - 1);
3887f4a2713aSLionel Sambuc assert(exponent != semantics->maxExponent &&
3888f4a2713aSLionel Sambuc "We can not increment an exponent beyond the maxExponent allowed"
3889f4a2713aSLionel Sambuc " by the given floating point semantics.");
3890f4a2713aSLionel Sambuc exponent++;
3891f4a2713aSLionel Sambuc } else {
3892f4a2713aSLionel Sambuc incrementSignificand();
3893f4a2713aSLionel Sambuc }
3894f4a2713aSLionel Sambuc }
3895f4a2713aSLionel Sambuc break;
3896f4a2713aSLionel Sambuc }
3897f4a2713aSLionel Sambuc
3898f4a2713aSLionel Sambuc // If we are performing nextDown, swap sign so we have -nextUp(-x)
3899f4a2713aSLionel Sambuc if (nextDown)
3900f4a2713aSLionel Sambuc changeSign();
3901f4a2713aSLionel Sambuc
3902f4a2713aSLionel Sambuc return result;
3903f4a2713aSLionel Sambuc }
3904f4a2713aSLionel Sambuc
3905f4a2713aSLionel Sambuc void
makeInf(bool Negative)3906f4a2713aSLionel Sambuc APFloat::makeInf(bool Negative) {
3907f4a2713aSLionel Sambuc category = fcInfinity;
3908f4a2713aSLionel Sambuc sign = Negative;
3909f4a2713aSLionel Sambuc exponent = semantics->maxExponent + 1;
3910f4a2713aSLionel Sambuc APInt::tcSet(significandParts(), 0, partCount());
3911f4a2713aSLionel Sambuc }
3912f4a2713aSLionel Sambuc
3913f4a2713aSLionel Sambuc void
makeZero(bool Negative)3914f4a2713aSLionel Sambuc APFloat::makeZero(bool Negative) {
3915f4a2713aSLionel Sambuc category = fcZero;
3916f4a2713aSLionel Sambuc sign = Negative;
3917f4a2713aSLionel Sambuc exponent = semantics->minExponent-1;
3918f4a2713aSLionel Sambuc APInt::tcSet(significandParts(), 0, partCount());
3919f4a2713aSLionel Sambuc }
3920*0a6a1f1dSLionel Sambuc
scalbn(APFloat X,int Exp)3921*0a6a1f1dSLionel Sambuc APFloat llvm::scalbn(APFloat X, int Exp) {
3922*0a6a1f1dSLionel Sambuc if (X.isInfinity() || X.isZero() || X.isNaN())
3923*0a6a1f1dSLionel Sambuc return std::move(X);
3924*0a6a1f1dSLionel Sambuc
3925*0a6a1f1dSLionel Sambuc auto MaxExp = X.getSemantics().maxExponent;
3926*0a6a1f1dSLionel Sambuc auto MinExp = X.getSemantics().minExponent;
3927*0a6a1f1dSLionel Sambuc if (Exp > (MaxExp - X.exponent))
3928*0a6a1f1dSLionel Sambuc // Overflow saturates to infinity.
3929*0a6a1f1dSLionel Sambuc return APFloat::getInf(X.getSemantics(), X.isNegative());
3930*0a6a1f1dSLionel Sambuc if (Exp < (MinExp - X.exponent))
3931*0a6a1f1dSLionel Sambuc // Underflow saturates to zero.
3932*0a6a1f1dSLionel Sambuc return APFloat::getZero(X.getSemantics(), X.isNegative());
3933*0a6a1f1dSLionel Sambuc
3934*0a6a1f1dSLionel Sambuc X.exponent += Exp;
3935*0a6a1f1dSLionel Sambuc return std::move(X);
3936*0a6a1f1dSLionel Sambuc }
3937