blob: 3b03c54e9764155953d6bbf752170e2c45dcb5b5 [file] [log] [blame]
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001//===-- APFloat.cpp - Implement APFloat class -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb39cdde2007-08-20 22:49:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a class to represent arbitrary precision floating
11// point values and provide a variety of arithmetic operations on them.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner36d26c22007-12-08 19:00:03 +000015#include "llvm/ADT/APFloat.h"
Ted Kremenek1f801fa2008-02-11 17:24:50 +000016#include "llvm/ADT/FoldingSet.h"
Dale Johannesend3b51fd2007-08-24 05:08:11 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000018#include <cstring>
Chris Lattnerb39cdde2007-08-20 22:49:32 +000019
20using namespace llvm;
21
22#define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
23
Neil Bootha30b0ee2007-10-03 22:26:02 +000024/* Assumed in hexadecimal significand parsing, and conversion to
25 hexadecimal strings. */
Chris Lattner9f17eb02008-08-17 04:58:58 +000026#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
Chris Lattnerb39cdde2007-08-20 22:49:32 +000027COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
28
29namespace llvm {
30
31 /* Represents floating point arithmetic semantics. */
32 struct fltSemantics {
33 /* The largest E such that 2^E is representable; this matches the
34 definition of IEEE 754. */
35 exponent_t maxExponent;
36
37 /* The smallest E such that 2^E is a normalized number; this
38 matches the definition of IEEE 754. */
39 exponent_t minExponent;
40
41 /* Number of bits in the significand. This includes the integer
42 bit. */
Neil Booth7a951ca2007-10-12 15:33:27 +000043 unsigned int precision;
Neil Boothcaf19d72007-10-14 10:29:28 +000044
45 /* True if arithmetic is supported. */
46 unsigned int arithmeticOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +000047 };
48
Neil Boothcaf19d72007-10-14 10:29:28 +000049 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
50 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
51 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
52 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, true };
53 const fltSemantics APFloat::Bogus = { 0, 0, 0, true };
Dale Johannesena471c2e2007-10-11 18:07:22 +000054
55 // The PowerPC format consists of two doubles. It does not map cleanly
56 // onto the usual format above. For now only storage of constants of
57 // this type is supported, no arithmetic.
Neil Boothcaf19d72007-10-14 10:29:28 +000058 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
Neil Booth96c74712007-10-12 16:02:31 +000059
60 /* A tight upper bound on number of parts required to hold the value
61 pow(5, power) is
62
Neil Booth686700e2007-10-15 15:00:55 +000063 power * 815 / (351 * integerPartWidth) + 1
Neil Booth96c74712007-10-12 16:02:31 +000064
65 However, whilst the result may require only this many parts,
66 because we are multiplying two values to get it, the
67 multiplication may require an extra part with the excess part
68 being zero (consider the trivial case of 1 * 1, tcFullMultiply
69 requires two parts to hold the single-part result). So we add an
70 extra one to guarantee enough space whilst multiplying. */
71 const unsigned int maxExponent = 16383;
72 const unsigned int maxPrecision = 113;
73 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
Neil Booth686700e2007-10-15 15:00:55 +000074 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
75 / (351 * integerPartWidth));
Chris Lattnerb39cdde2007-08-20 22:49:32 +000076}
77
Chris Lattnere213f3f2009-03-12 23:59:55 +000078/* A bunch of private, handy routines. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +000079
Chris Lattnere213f3f2009-03-12 23:59:55 +000080static inline unsigned int
81partCountForBits(unsigned int bits)
82{
83 return ((bits) + integerPartWidth - 1) / integerPartWidth;
84}
Chris Lattnerb39cdde2007-08-20 22:49:32 +000085
Chris Lattnere213f3f2009-03-12 23:59:55 +000086/* Returns 0U-9U. Return values >= 10U are not digits. */
87static inline unsigned int
88decDigitValue(unsigned int c)
89{
90 return c - '0';
91}
Chris Lattnerb39cdde2007-08-20 22:49:32 +000092
Chris Lattnere213f3f2009-03-12 23:59:55 +000093static unsigned int
94hexDigitValue(unsigned int c)
95{
96 unsigned int r;
Chris Lattnerb39cdde2007-08-20 22:49:32 +000097
Chris Lattnere213f3f2009-03-12 23:59:55 +000098 r = c - '0';
99 if(r <= 9)
100 return r;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000101
Chris Lattnere213f3f2009-03-12 23:59:55 +0000102 r = c - 'A';
103 if(r <= 5)
104 return r + 10;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000105
Chris Lattnere213f3f2009-03-12 23:59:55 +0000106 r = c - 'a';
107 if(r <= 5)
108 return r + 10;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000109
Chris Lattnere213f3f2009-03-12 23:59:55 +0000110 return -1U;
111}
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000112
Chris Lattnere213f3f2009-03-12 23:59:55 +0000113static inline void
114assertArithmeticOK(const llvm::fltSemantics &semantics) {
115 assert(semantics.arithmeticOK
116 && "Compile-time arithmetic does not support these semantics");
117}
Neil Boothcaf19d72007-10-14 10:29:28 +0000118
Chris Lattnere213f3f2009-03-12 23:59:55 +0000119/* Return the value of a decimal exponent of the form
120 [+-]ddddddd.
Neil Booth1870f292007-10-14 10:16:12 +0000121
Chris Lattnere213f3f2009-03-12 23:59:55 +0000122 If the exponent overflows, returns a large exponent with the
123 appropriate sign. */
124static int
125readExponent(const char *p)
126{
127 bool isNegative;
128 unsigned int absExponent;
129 const unsigned int overlargeExponent = 24000; /* FIXME. */
Neil Booth1870f292007-10-14 10:16:12 +0000130
Chris Lattnere213f3f2009-03-12 23:59:55 +0000131 isNegative = (*p == '-');
132 if (*p == '-' || *p == '+')
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000133 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000134
Chris Lattnere213f3f2009-03-12 23:59:55 +0000135 absExponent = decDigitValue(*p++);
136 assert (absExponent < 10U);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000137
Chris Lattnere213f3f2009-03-12 23:59:55 +0000138 for (;;) {
139 unsigned int value;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000140
Chris Lattnere213f3f2009-03-12 23:59:55 +0000141 value = decDigitValue(*p);
142 if (value >= 10U)
143 break;
144
145 p++;
146 value += absExponent * 10;
147 if (absExponent >= overlargeExponent) {
148 absExponent = overlargeExponent;
149 break;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000150 }
Chris Lattnere213f3f2009-03-12 23:59:55 +0000151 absExponent = value;
152 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000153
Chris Lattnere213f3f2009-03-12 23:59:55 +0000154 if (isNegative)
155 return -(int) absExponent;
156 else
157 return (int) absExponent;
158}
159
160/* This is ugly and needs cleaning up, but I don't immediately see
161 how whilst remaining safe. */
162static int
163totalExponent(const char *p, int exponentAdjustment)
164{
165 int unsignedExponent;
166 bool negative, overflow;
167 int exponent;
168
169 /* Move past the exponent letter and sign to the digits. */
170 p++;
171 negative = *p == '-';
172 if(*p == '-' || *p == '+')
173 p++;
174
175 unsignedExponent = 0;
176 overflow = false;
177 for(;;) {
178 unsigned int value;
179
180 value = decDigitValue(*p);
181 if(value >= 10U)
182 break;
183
184 p++;
185 unsignedExponent = unsignedExponent * 10 + value;
186 if(unsignedExponent > 65535)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000187 overflow = true;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000188 }
189
Chris Lattnere213f3f2009-03-12 23:59:55 +0000190 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
191 overflow = true;
192
193 if(!overflow) {
194 exponent = unsignedExponent;
195 if(negative)
196 exponent = -exponent;
197 exponent += exponentAdjustment;
198 if(exponent > 65535 || exponent < -65536)
199 overflow = true;
200 }
201
202 if(overflow)
203 exponent = negative ? -65536: 65535;
204
205 return exponent;
206}
207
208static const char *
209skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
210{
211 *dot = 0;
212 while(*p == '0')
213 p++;
214
215 if(*p == '.') {
216 *dot = p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000217 while(*p == '0')
218 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000219 }
220
Chris Lattnere213f3f2009-03-12 23:59:55 +0000221 return p;
222}
Neil Booth1870f292007-10-14 10:16:12 +0000223
Chris Lattnere213f3f2009-03-12 23:59:55 +0000224/* Given a normal decimal floating point number of the form
Neil Booth1870f292007-10-14 10:16:12 +0000225
Chris Lattnere213f3f2009-03-12 23:59:55 +0000226 dddd.dddd[eE][+-]ddd
Neil Booth686700e2007-10-15 15:00:55 +0000227
Chris Lattnere213f3f2009-03-12 23:59:55 +0000228 where the decimal point and exponent are optional, fill out the
229 structure D. Exponent is appropriate if the significand is
230 treated as an integer, and normalizedExponent if the significand
231 is taken to have the decimal point after a single leading
232 non-zero digit.
Neil Booth1870f292007-10-14 10:16:12 +0000233
Chris Lattnere213f3f2009-03-12 23:59:55 +0000234 If the value is zero, V->firstSigDigit points to a non-digit, and
235 the return exponent is zero.
236*/
237struct decimalInfo {
238 const char *firstSigDigit;
239 const char *lastSigDigit;
240 int exponent;
241 int normalizedExponent;
242};
Neil Booth1870f292007-10-14 10:16:12 +0000243
Chris Lattnere213f3f2009-03-12 23:59:55 +0000244static void
245interpretDecimal(const char *p, decimalInfo *D)
246{
247 const char *dot;
Neil Booth1870f292007-10-14 10:16:12 +0000248
Chris Lattnere213f3f2009-03-12 23:59:55 +0000249 p = skipLeadingZeroesAndAnyDot (p, &dot);
Neil Booth1870f292007-10-14 10:16:12 +0000250
Chris Lattnere213f3f2009-03-12 23:59:55 +0000251 D->firstSigDigit = p;
252 D->exponent = 0;
253 D->normalizedExponent = 0;
254
255 for (;;) {
256 if (*p == '.') {
257 assert(dot == 0);
258 dot = p++;
Neil Booth1870f292007-10-14 10:16:12 +0000259 }
Chris Lattnere213f3f2009-03-12 23:59:55 +0000260 if (decDigitValue(*p) >= 10U)
261 break;
262 p++;
263 }
Neil Booth1870f292007-10-14 10:16:12 +0000264
Chris Lattnere213f3f2009-03-12 23:59:55 +0000265 /* If number is all zerooes accept any exponent. */
266 if (p != D->firstSigDigit) {
267 if (*p == 'e' || *p == 'E')
268 D->exponent = readExponent(p + 1);
Neil Booth1870f292007-10-14 10:16:12 +0000269
Chris Lattnere213f3f2009-03-12 23:59:55 +0000270 /* Implied decimal point? */
271 if (!dot)
272 dot = p;
Neil Booth1870f292007-10-14 10:16:12 +0000273
Chris Lattnere213f3f2009-03-12 23:59:55 +0000274 /* Drop insignificant trailing zeroes. */
275 do
Neil Booth1870f292007-10-14 10:16:12 +0000276 do
Chris Lattnere213f3f2009-03-12 23:59:55 +0000277 p--;
278 while (*p == '0');
279 while (*p == '.');
Neil Booth1870f292007-10-14 10:16:12 +0000280
Chris Lattnere213f3f2009-03-12 23:59:55 +0000281 /* Adjust the exponents for any decimal point. */
282 D->exponent += static_cast<exponent_t>((dot - p) - (dot > p));
283 D->normalizedExponent = (D->exponent +
284 static_cast<exponent_t>((p - D->firstSigDigit)
285 - (dot > D->firstSigDigit && dot < p)));
Neil Booth1870f292007-10-14 10:16:12 +0000286 }
287
Chris Lattnere213f3f2009-03-12 23:59:55 +0000288 D->lastSigDigit = p;
289}
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000290
Chris Lattnere213f3f2009-03-12 23:59:55 +0000291/* Return the trailing fraction of a hexadecimal number.
292 DIGITVALUE is the first hex digit of the fraction, P points to
293 the next digit. */
294static lostFraction
295trailingHexadecimalFraction(const char *p, unsigned int digitValue)
296{
297 unsigned int hexDigit;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000298
Chris Lattnere213f3f2009-03-12 23:59:55 +0000299 /* If the first trailing digit isn't 0 or 8 we can work out the
300 fraction immediately. */
301 if(digitValue > 8)
302 return lfMoreThanHalf;
303 else if(digitValue < 8 && digitValue > 0)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000304 return lfLessThanHalf;
Chris Lattnere213f3f2009-03-12 23:59:55 +0000305
306 /* Otherwise we need to find the first non-zero digit. */
307 while(*p == '0')
308 p++;
309
310 hexDigit = hexDigitValue(*p);
311
312 /* If we ran off the end it is exactly zero or one-half, otherwise
313 a little more. */
314 if(hexDigit == -1U)
315 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
316 else
317 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
318}
319
320/* Return the fraction lost were a bignum truncated losing the least
321 significant BITS bits. */
322static lostFraction
323lostFractionThroughTruncation(const integerPart *parts,
324 unsigned int partCount,
325 unsigned int bits)
326{
327 unsigned int lsb;
328
329 lsb = APInt::tcLSB(parts, partCount);
330
331 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
332 if(bits <= lsb)
333 return lfExactlyZero;
334 if(bits == lsb + 1)
335 return lfExactlyHalf;
336 if(bits <= partCount * integerPartWidth
337 && APInt::tcExtractBit(parts, bits - 1))
338 return lfMoreThanHalf;
339
340 return lfLessThanHalf;
341}
342
343/* Shift DST right BITS bits noting lost fraction. */
344static lostFraction
345shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
346{
347 lostFraction lost_fraction;
348
349 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
350
351 APInt::tcShiftRight(dst, parts, bits);
352
353 return lost_fraction;
354}
355
356/* Combine the effect of two lost fractions. */
357static lostFraction
358combineLostFractions(lostFraction moreSignificant,
359 lostFraction lessSignificant)
360{
361 if(lessSignificant != lfExactlyZero) {
362 if(moreSignificant == lfExactlyZero)
363 moreSignificant = lfLessThanHalf;
364 else if(moreSignificant == lfExactlyHalf)
365 moreSignificant = lfMoreThanHalf;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000366 }
367
Chris Lattnere213f3f2009-03-12 23:59:55 +0000368 return moreSignificant;
369}
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000370
Chris Lattnere213f3f2009-03-12 23:59:55 +0000371/* The error from the true value, in half-ulps, on multiplying two
372 floating point numbers, which differ from the value they
373 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
374 than the returned value.
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000375
Chris Lattnere213f3f2009-03-12 23:59:55 +0000376 See "How to Read Floating Point Numbers Accurately" by William D
377 Clinger. */
378static unsigned int
379HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
380{
381 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000382
Chris Lattnere213f3f2009-03-12 23:59:55 +0000383 if (HUerr1 + HUerr2 == 0)
384 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
385 else
386 return inexactMultiply + 2 * (HUerr1 + HUerr2);
387}
Neil Bootha30b0ee2007-10-03 22:26:02 +0000388
Chris Lattnere213f3f2009-03-12 23:59:55 +0000389/* The number of ulps from the boundary (zero, or half if ISNEAREST)
390 when the least significant BITS are truncated. BITS cannot be
391 zero. */
392static integerPart
393ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
394{
395 unsigned int count, partBits;
396 integerPart part, boundary;
Neil Booth33d4c922007-10-07 08:51:21 +0000397
Chris Lattnere213f3f2009-03-12 23:59:55 +0000398 assert (bits != 0);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000399
Chris Lattnere213f3f2009-03-12 23:59:55 +0000400 bits--;
401 count = bits / integerPartWidth;
402 partBits = bits % integerPartWidth + 1;
Neil Booth96c74712007-10-12 16:02:31 +0000403
Chris Lattnere213f3f2009-03-12 23:59:55 +0000404 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
Neil Booth96c74712007-10-12 16:02:31 +0000405
Chris Lattnere213f3f2009-03-12 23:59:55 +0000406 if (isNearest)
407 boundary = (integerPart) 1 << (partBits - 1);
408 else
409 boundary = 0;
410
411 if (count == 0) {
412 if (part - boundary <= boundary - part)
413 return part - boundary;
Neil Booth96c74712007-10-12 16:02:31 +0000414 else
Chris Lattnere213f3f2009-03-12 23:59:55 +0000415 return boundary - part;
Neil Booth96c74712007-10-12 16:02:31 +0000416 }
417
Chris Lattnere213f3f2009-03-12 23:59:55 +0000418 if (part == boundary) {
419 while (--count)
420 if (parts[count])
421 return ~(integerPart) 0; /* A lot. */
Neil Booth96c74712007-10-12 16:02:31 +0000422
Chris Lattnere213f3f2009-03-12 23:59:55 +0000423 return parts[0];
424 } else if (part == boundary - 1) {
425 while (--count)
426 if (~parts[count])
427 return ~(integerPart) 0; /* A lot. */
Neil Booth96c74712007-10-12 16:02:31 +0000428
Chris Lattnere213f3f2009-03-12 23:59:55 +0000429 return -parts[0];
430 }
Neil Booth96c74712007-10-12 16:02:31 +0000431
Chris Lattnere213f3f2009-03-12 23:59:55 +0000432 return ~(integerPart) 0; /* A lot. */
433}
Neil Booth96c74712007-10-12 16:02:31 +0000434
Chris Lattnere213f3f2009-03-12 23:59:55 +0000435/* Place pow(5, power) in DST, and return the number of parts used.
436 DST must be at least one part larger than size of the answer. */
437static unsigned int
438powerOf5(integerPart *dst, unsigned int power)
439{
440 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
441 15625, 78125 };
Chris Lattneree167a72009-03-13 00:24:01 +0000442 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
443 pow5s[0] = 78125 * 5;
444
Chris Lattner807926a2009-03-13 00:03:51 +0000445 unsigned int partsCount[16] = { 1 };
Chris Lattnere213f3f2009-03-12 23:59:55 +0000446 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
447 unsigned int result;
Chris Lattnere213f3f2009-03-12 23:59:55 +0000448 assert(power <= maxExponent);
449
450 p1 = dst;
451 p2 = scratch;
452
453 *p1 = firstEightPowers[power & 7];
454 power >>= 3;
455
456 result = 1;
457 pow5 = pow5s;
458
459 for (unsigned int n = 0; power; power >>= 1, n++) {
460 unsigned int pc;
461
462 pc = partsCount[n];
463
464 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
465 if (pc == 0) {
466 pc = partsCount[n - 1];
467 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
468 pc *= 2;
469 if (pow5[pc - 1] == 0)
470 pc--;
471 partsCount[n] = pc;
Neil Booth96c74712007-10-12 16:02:31 +0000472 }
473
Chris Lattnere213f3f2009-03-12 23:59:55 +0000474 if (power & 1) {
475 integerPart *tmp;
Neil Booth96c74712007-10-12 16:02:31 +0000476
Chris Lattnere213f3f2009-03-12 23:59:55 +0000477 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
478 result += pc;
479 if (p2[result - 1] == 0)
480 result--;
Neil Booth96c74712007-10-12 16:02:31 +0000481
Chris Lattnere213f3f2009-03-12 23:59:55 +0000482 /* Now result is in p1 with partsCount parts and p2 is scratch
483 space. */
484 tmp = p1, p1 = p2, p2 = tmp;
Neil Booth96c74712007-10-12 16:02:31 +0000485 }
486
Chris Lattnere213f3f2009-03-12 23:59:55 +0000487 pow5 += pc;
Neil Booth96c74712007-10-12 16:02:31 +0000488 }
489
Chris Lattnere213f3f2009-03-12 23:59:55 +0000490 if (p1 != dst)
491 APInt::tcAssign(dst, p1, result);
Neil Booth96c74712007-10-12 16:02:31 +0000492
Chris Lattnere213f3f2009-03-12 23:59:55 +0000493 return result;
494}
Neil Booth96c74712007-10-12 16:02:31 +0000495
Chris Lattnere213f3f2009-03-12 23:59:55 +0000496/* Zero at the end to avoid modular arithmetic when adding one; used
497 when rounding up during hexadecimal output. */
498static const char hexDigitsLower[] = "0123456789abcdef0";
499static const char hexDigitsUpper[] = "0123456789ABCDEF0";
500static const char infinityL[] = "infinity";
501static const char infinityU[] = "INFINITY";
502static const char NaNL[] = "nan";
503static const char NaNU[] = "NAN";
Neil Booth96c74712007-10-12 16:02:31 +0000504
Chris Lattnere213f3f2009-03-12 23:59:55 +0000505/* Write out an integerPart in hexadecimal, starting with the most
506 significant nibble. Write out exactly COUNT hexdigits, return
507 COUNT. */
508static unsigned int
509partAsHex (char *dst, integerPart part, unsigned int count,
510 const char *hexDigitChars)
511{
512 unsigned int result = count;
Neil Booth96c74712007-10-12 16:02:31 +0000513
Chris Lattnere213f3f2009-03-12 23:59:55 +0000514 assert (count != 0 && count <= integerPartWidth / 4);
Neil Booth96c74712007-10-12 16:02:31 +0000515
Chris Lattnere213f3f2009-03-12 23:59:55 +0000516 part >>= (integerPartWidth - 4 * count);
517 while (count--) {
518 dst[count] = hexDigitChars[part & 0xf];
519 part >>= 4;
Neil Booth96c74712007-10-12 16:02:31 +0000520 }
521
Chris Lattnere213f3f2009-03-12 23:59:55 +0000522 return result;
523}
Neil Bootha30b0ee2007-10-03 22:26:02 +0000524
Chris Lattnere213f3f2009-03-12 23:59:55 +0000525/* Write out an unsigned decimal integer. */
526static char *
527writeUnsignedDecimal (char *dst, unsigned int n)
528{
529 char buff[40], *p;
Neil Bootha30b0ee2007-10-03 22:26:02 +0000530
Chris Lattnere213f3f2009-03-12 23:59:55 +0000531 p = buff;
532 do
533 *p++ = '0' + n % 10;
534 while (n /= 10);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000535
Chris Lattnere213f3f2009-03-12 23:59:55 +0000536 do
537 *dst++ = *--p;
538 while (p != buff);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000539
Chris Lattnere213f3f2009-03-12 23:59:55 +0000540 return dst;
541}
Neil Bootha30b0ee2007-10-03 22:26:02 +0000542
Chris Lattnere213f3f2009-03-12 23:59:55 +0000543/* Write out a signed decimal integer. */
544static char *
545writeSignedDecimal (char *dst, int value)
546{
547 if (value < 0) {
548 *dst++ = '-';
549 dst = writeUnsignedDecimal(dst, -(unsigned) value);
550 } else
551 dst = writeUnsignedDecimal(dst, value);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000552
Chris Lattnere213f3f2009-03-12 23:59:55 +0000553 return dst;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000554}
555
556/* Constructors. */
557void
558APFloat::initialize(const fltSemantics *ourSemantics)
559{
560 unsigned int count;
561
562 semantics = ourSemantics;
563 count = partCount();
564 if(count > 1)
565 significand.parts = new integerPart[count];
566}
567
568void
569APFloat::freeSignificand()
570{
571 if(partCount() > 1)
572 delete [] significand.parts;
573}
574
575void
576APFloat::assign(const APFloat &rhs)
577{
578 assert(semantics == rhs.semantics);
579
580 sign = rhs.sign;
581 category = rhs.category;
582 exponent = rhs.exponent;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000583 sign2 = rhs.sign2;
584 exponent2 = rhs.exponent2;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000585 if(category == fcNormal || category == fcNaN)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000586 copySignificand(rhs);
587}
588
589void
590APFloat::copySignificand(const APFloat &rhs)
591{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000592 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000593 assert(rhs.partCount() >= partCount());
594
595 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +0000596 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000597}
598
Neil Boothe5e01942007-10-14 10:39:51 +0000599/* Make this number a NaN, with an arbitrary but deterministic value
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000600 for the significand. If double or longer, this is a signalling NaN,
Mike Stumpc5ca7132009-05-30 03:49:43 +0000601 which may not be ideal. If float, this is QNaN(0). */
Neil Boothe5e01942007-10-14 10:39:51 +0000602void
Mike Stumpc5ca7132009-05-30 03:49:43 +0000603APFloat::makeNaN(unsigned type)
Neil Boothe5e01942007-10-14 10:39:51 +0000604{
605 category = fcNaN;
Mike Stumpc5ca7132009-05-30 03:49:43 +0000606 // FIXME: Add double and long double support for QNaN(0).
607 if (semantics->precision == 24 && semantics->maxExponent == 127) {
608 type |= 0x7fc00000U;
609 type &= ~0x80000000U;
610 } else
611 type = ~0U;
612 APInt::tcSet(significandParts(), type, partCount());
Neil Boothe5e01942007-10-14 10:39:51 +0000613}
614
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000615APFloat &
616APFloat::operator=(const APFloat &rhs)
617{
618 if(this != &rhs) {
619 if(semantics != rhs.semantics) {
620 freeSignificand();
621 initialize(rhs.semantics);
622 }
623 assign(rhs);
624 }
625
626 return *this;
627}
628
Dale Johannesen343e7702007-08-24 00:56:33 +0000629bool
Dale Johannesen12595d72007-08-24 22:09:56 +0000630APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000631 if (this == &rhs)
632 return true;
633 if (semantics != rhs.semantics ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000634 category != rhs.category ||
635 sign != rhs.sign)
Dale Johannesen343e7702007-08-24 00:56:33 +0000636 return false;
Dan Gohmanb10abe12008-01-29 12:08:20 +0000637 if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
Dale Johannesena471c2e2007-10-11 18:07:22 +0000638 sign2 != rhs.sign2)
639 return false;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000640 if (category==fcZero || category==fcInfinity)
Dale Johannesen343e7702007-08-24 00:56:33 +0000641 return true;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000642 else if (category==fcNormal && exponent!=rhs.exponent)
643 return false;
Dan Gohmanb10abe12008-01-29 12:08:20 +0000644 else if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
Dale Johannesena471c2e2007-10-11 18:07:22 +0000645 exponent2!=rhs.exponent2)
646 return false;
Dale Johannesen343e7702007-08-24 00:56:33 +0000647 else {
Dale Johannesen343e7702007-08-24 00:56:33 +0000648 int i= partCount();
649 const integerPart* p=significandParts();
650 const integerPart* q=rhs.significandParts();
651 for (; i>0; i--, p++, q++) {
652 if (*p != *q)
653 return false;
654 }
655 return true;
656 }
657}
658
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000659APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
660{
Neil Boothcaf19d72007-10-14 10:29:28 +0000661 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000662 initialize(&ourSemantics);
663 sign = 0;
664 zeroSignificand();
665 exponent = ourSemantics.precision - 1;
666 significandParts()[0] = value;
667 normalize(rmNearestTiesToEven, lfExactlyZero);
668}
669
670APFloat::APFloat(const fltSemantics &ourSemantics,
Mike Stumpc5ca7132009-05-30 03:49:43 +0000671 fltCategory ourCategory, bool negative, unsigned type)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000672{
Neil Boothcaf19d72007-10-14 10:29:28 +0000673 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000674 initialize(&ourSemantics);
675 category = ourCategory;
676 sign = negative;
Mike Stumpc5ca7132009-05-30 03:49:43 +0000677 if (category == fcNormal)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000678 category = fcZero;
Neil Boothe5e01942007-10-14 10:39:51 +0000679 else if (ourCategory == fcNaN)
Mike Stumpc5ca7132009-05-30 03:49:43 +0000680 makeNaN(type);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000681}
682
683APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
684{
Neil Boothcaf19d72007-10-14 10:29:28 +0000685 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000686 initialize(&ourSemantics);
687 convertFromString(text, rmNearestTiesToEven);
688}
689
690APFloat::APFloat(const APFloat &rhs)
691{
692 initialize(rhs.semantics);
693 assign(rhs);
694}
695
696APFloat::~APFloat()
697{
698 freeSignificand();
699}
700
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000701// Profile - This method 'profiles' an APFloat for use with FoldingSet.
702void APFloat::Profile(FoldingSetNodeID& ID) const {
Dale Johannesen7111b022008-10-09 18:53:47 +0000703 ID.Add(bitcastToAPInt());
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000704}
705
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000706unsigned int
707APFloat::partCount() const
708{
Dale Johannesena72a5a02007-09-20 23:47:58 +0000709 return partCountForBits(semantics->precision + 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000710}
711
712unsigned int
713APFloat::semanticsPrecision(const fltSemantics &semantics)
714{
715 return semantics.precision;
716}
717
718const integerPart *
719APFloat::significandParts() const
720{
721 return const_cast<APFloat *>(this)->significandParts();
722}
723
724integerPart *
725APFloat::significandParts()
726{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000727 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000728
729 if(partCount() > 1)
730 return significand.parts;
731 else
732 return &significand.part;
733}
734
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000735void
736APFloat::zeroSignificand()
737{
738 category = fcNormal;
739 APInt::tcSet(significandParts(), 0, partCount());
740}
741
742/* Increment an fcNormal floating point number's significand. */
743void
744APFloat::incrementSignificand()
745{
746 integerPart carry;
747
748 carry = APInt::tcIncrement(significandParts(), partCount());
749
750 /* Our callers should never cause us to overflow. */
751 assert(carry == 0);
752}
753
754/* Add the significand of the RHS. Returns the carry flag. */
755integerPart
756APFloat::addSignificand(const APFloat &rhs)
757{
758 integerPart *parts;
759
760 parts = significandParts();
761
762 assert(semantics == rhs.semantics);
763 assert(exponent == rhs.exponent);
764
765 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
766}
767
768/* Subtract the significand of the RHS with a borrow flag. Returns
769 the borrow flag. */
770integerPart
771APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
772{
773 integerPart *parts;
774
775 parts = significandParts();
776
777 assert(semantics == rhs.semantics);
778 assert(exponent == rhs.exponent);
779
780 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth4f881702007-09-26 21:33:42 +0000781 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000782}
783
784/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
785 on to the full-precision result of the multiplication. Returns the
786 lost fraction. */
787lostFraction
788APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
789{
Neil Booth4f881702007-09-26 21:33:42 +0000790 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000791 unsigned int partsCount, newPartsCount, precision;
792 integerPart *lhsSignificand;
793 integerPart scratch[4];
794 integerPart *fullSignificand;
795 lostFraction lost_fraction;
Dale Johannesen23a98552008-10-09 23:00:39 +0000796 bool ignored;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000797
798 assert(semantics == rhs.semantics);
799
800 precision = semantics->precision;
801 newPartsCount = partCountForBits(precision * 2);
802
803 if(newPartsCount > 4)
804 fullSignificand = new integerPart[newPartsCount];
805 else
806 fullSignificand = scratch;
807
808 lhsSignificand = significandParts();
809 partsCount = partCount();
810
811 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth978661d2007-10-06 00:24:48 +0000812 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000813
814 lost_fraction = lfExactlyZero;
815 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
816 exponent += rhs.exponent;
817
818 if(addend) {
819 Significand savedSignificand = significand;
820 const fltSemantics *savedSemantics = semantics;
821 fltSemantics extendedSemantics;
822 opStatus status;
823 unsigned int extendedPrecision;
824
825 /* Normalize our MSB. */
826 extendedPrecision = precision + precision - 1;
827 if(omsb != extendedPrecision)
828 {
Neil Booth4f881702007-09-26 21:33:42 +0000829 APInt::tcShiftLeft(fullSignificand, newPartsCount,
830 extendedPrecision - omsb);
831 exponent -= extendedPrecision - omsb;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000832 }
833
834 /* Create new semantics. */
835 extendedSemantics = *semantics;
836 extendedSemantics.precision = extendedPrecision;
837
838 if(newPartsCount == 1)
839 significand.part = fullSignificand[0];
840 else
841 significand.parts = fullSignificand;
842 semantics = &extendedSemantics;
843
844 APFloat extendedAddend(*addend);
Dale Johannesen23a98552008-10-09 23:00:39 +0000845 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000846 assert(status == opOK);
847 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
848
849 /* Restore our state. */
850 if(newPartsCount == 1)
851 fullSignificand[0] = significand.part;
852 significand = savedSignificand;
853 semantics = savedSemantics;
854
855 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
856 }
857
858 exponent -= (precision - 1);
859
860 if(omsb > precision) {
861 unsigned int bits, significantParts;
862 lostFraction lf;
863
864 bits = omsb - precision;
865 significantParts = partCountForBits(omsb);
866 lf = shiftRight(fullSignificand, significantParts, bits);
867 lost_fraction = combineLostFractions(lf, lost_fraction);
868 exponent += bits;
869 }
870
871 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
872
873 if(newPartsCount > 4)
874 delete [] fullSignificand;
875
876 return lost_fraction;
877}
878
879/* Multiply the significands of LHS and RHS to DST. */
880lostFraction
881APFloat::divideSignificand(const APFloat &rhs)
882{
883 unsigned int bit, i, partsCount;
884 const integerPart *rhsSignificand;
885 integerPart *lhsSignificand, *dividend, *divisor;
886 integerPart scratch[4];
887 lostFraction lost_fraction;
888
889 assert(semantics == rhs.semantics);
890
891 lhsSignificand = significandParts();
892 rhsSignificand = rhs.significandParts();
893 partsCount = partCount();
894
895 if(partsCount > 2)
896 dividend = new integerPart[partsCount * 2];
897 else
898 dividend = scratch;
899
900 divisor = dividend + partsCount;
901
902 /* Copy the dividend and divisor as they will be modified in-place. */
903 for(i = 0; i < partsCount; i++) {
904 dividend[i] = lhsSignificand[i];
905 divisor[i] = rhsSignificand[i];
906 lhsSignificand[i] = 0;
907 }
908
909 exponent -= rhs.exponent;
910
911 unsigned int precision = semantics->precision;
912
913 /* Normalize the divisor. */
914 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
915 if(bit) {
916 exponent += bit;
917 APInt::tcShiftLeft(divisor, partsCount, bit);
918 }
919
920 /* Normalize the dividend. */
921 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
922 if(bit) {
923 exponent -= bit;
924 APInt::tcShiftLeft(dividend, partsCount, bit);
925 }
926
Neil Booth96c74712007-10-12 16:02:31 +0000927 /* Ensure the dividend >= divisor initially for the loop below.
928 Incidentally, this means that the division loop below is
929 guaranteed to set the integer bit to one. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000930 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
931 exponent--;
932 APInt::tcShiftLeft(dividend, partsCount, 1);
933 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
934 }
935
936 /* Long division. */
937 for(bit = precision; bit; bit -= 1) {
938 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
939 APInt::tcSubtract(dividend, divisor, 0, partsCount);
940 APInt::tcSetBit(lhsSignificand, bit - 1);
941 }
942
943 APInt::tcShiftLeft(dividend, partsCount, 1);
944 }
945
946 /* Figure out the lost fraction. */
947 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
948
949 if(cmp > 0)
950 lost_fraction = lfMoreThanHalf;
951 else if(cmp == 0)
952 lost_fraction = lfExactlyHalf;
953 else if(APInt::tcIsZero(dividend, partsCount))
954 lost_fraction = lfExactlyZero;
955 else
956 lost_fraction = lfLessThanHalf;
957
958 if(partsCount > 2)
959 delete [] dividend;
960
961 return lost_fraction;
962}
963
964unsigned int
965APFloat::significandMSB() const
966{
967 return APInt::tcMSB(significandParts(), partCount());
968}
969
970unsigned int
971APFloat::significandLSB() const
972{
973 return APInt::tcLSB(significandParts(), partCount());
974}
975
976/* Note that a zero result is NOT normalized to fcZero. */
977lostFraction
978APFloat::shiftSignificandRight(unsigned int bits)
979{
980 /* Our exponent should not overflow. */
981 assert((exponent_t) (exponent + bits) >= exponent);
982
983 exponent += bits;
984
985 return shiftRight(significandParts(), partCount(), bits);
986}
987
988/* Shift the significand left BITS bits, subtract BITS from its exponent. */
989void
990APFloat::shiftSignificandLeft(unsigned int bits)
991{
992 assert(bits < semantics->precision);
993
994 if(bits) {
995 unsigned int partsCount = partCount();
996
997 APInt::tcShiftLeft(significandParts(), partsCount, bits);
998 exponent -= bits;
999
1000 assert(!APInt::tcIsZero(significandParts(), partsCount));
1001 }
1002}
1003
1004APFloat::cmpResult
1005APFloat::compareAbsoluteValue(const APFloat &rhs) const
1006{
1007 int compare;
1008
1009 assert(semantics == rhs.semantics);
1010 assert(category == fcNormal);
1011 assert(rhs.category == fcNormal);
1012
1013 compare = exponent - rhs.exponent;
1014
1015 /* If exponents are equal, do an unsigned bignum comparison of the
1016 significands. */
1017 if(compare == 0)
1018 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +00001019 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001020
1021 if(compare > 0)
1022 return cmpGreaterThan;
1023 else if(compare < 0)
1024 return cmpLessThan;
1025 else
1026 return cmpEqual;
1027}
1028
1029/* Handle overflow. Sign is preserved. We either become infinity or
1030 the largest finite number. */
1031APFloat::opStatus
1032APFloat::handleOverflow(roundingMode rounding_mode)
1033{
1034 /* Infinity? */
1035 if(rounding_mode == rmNearestTiesToEven
1036 || rounding_mode == rmNearestTiesToAway
1037 || (rounding_mode == rmTowardPositive && !sign)
1038 || (rounding_mode == rmTowardNegative && sign))
1039 {
1040 category = fcInfinity;
1041 return (opStatus) (opOverflow | opInexact);
1042 }
1043
1044 /* Otherwise we become the largest finite number. */
1045 category = fcNormal;
1046 exponent = semantics->maxExponent;
1047 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth4f881702007-09-26 21:33:42 +00001048 semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001049
1050 return opInexact;
1051}
1052
Neil Boothb7dea4c2007-10-03 15:16:41 +00001053/* Returns TRUE if, when truncating the current number, with BIT the
1054 new LSB, with the given lost fraction and rounding mode, the result
1055 would need to be rounded away from zero (i.e., by increasing the
1056 signficand). This routine must work for fcZero of both signs, and
1057 fcNormal numbers. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001058bool
1059APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Boothb7dea4c2007-10-03 15:16:41 +00001060 lostFraction lost_fraction,
1061 unsigned int bit) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001062{
Dale Johanneseneaf08942007-08-31 04:03:46 +00001063 /* NaNs and infinities should not have lost fractions. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001064 assert(category == fcNormal || category == fcZero);
1065
Neil Boothb7dea4c2007-10-03 15:16:41 +00001066 /* Current callers never pass this so we don't handle it. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001067 assert(lost_fraction != lfExactlyZero);
1068
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001069 switch (rounding_mode) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001070 default:
1071 assert(0);
1072
1073 case rmNearestTiesToAway:
1074 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1075
1076 case rmNearestTiesToEven:
1077 if(lost_fraction == lfMoreThanHalf)
1078 return true;
1079
1080 /* Our zeroes don't have a significand to test. */
1081 if(lost_fraction == lfExactlyHalf && category != fcZero)
Neil Boothb7dea4c2007-10-03 15:16:41 +00001082 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001083
1084 return false;
1085
1086 case rmTowardZero:
1087 return false;
1088
1089 case rmTowardPositive:
1090 return sign == false;
1091
1092 case rmTowardNegative:
1093 return sign == true;
1094 }
1095}
1096
1097APFloat::opStatus
1098APFloat::normalize(roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001099 lostFraction lost_fraction)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001100{
Neil Booth4f881702007-09-26 21:33:42 +00001101 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001102 int exponentChange;
1103
1104 if(category != fcNormal)
1105 return opOK;
1106
1107 /* Before rounding normalize the exponent of fcNormal numbers. */
1108 omsb = significandMSB() + 1;
1109
1110 if(omsb) {
1111 /* OMSB is numbered from 1. We want to place it in the integer
1112 bit numbered PRECISON if possible, with a compensating change in
1113 the exponent. */
1114 exponentChange = omsb - semantics->precision;
1115
1116 /* If the resulting exponent is too high, overflow according to
1117 the rounding mode. */
1118 if(exponent + exponentChange > semantics->maxExponent)
1119 return handleOverflow(rounding_mode);
1120
1121 /* Subnormal numbers have exponent minExponent, and their MSB
1122 is forced based on that. */
1123 if(exponent + exponentChange < semantics->minExponent)
1124 exponentChange = semantics->minExponent - exponent;
1125
1126 /* Shifting left is easy as we don't lose precision. */
1127 if(exponentChange < 0) {
1128 assert(lost_fraction == lfExactlyZero);
1129
1130 shiftSignificandLeft(-exponentChange);
1131
1132 return opOK;
1133 }
1134
1135 if(exponentChange > 0) {
1136 lostFraction lf;
1137
1138 /* Shift right and capture any new lost fraction. */
1139 lf = shiftSignificandRight(exponentChange);
1140
1141 lost_fraction = combineLostFractions(lf, lost_fraction);
1142
1143 /* Keep OMSB up-to-date. */
1144 if(omsb > (unsigned) exponentChange)
Neil Booth96c74712007-10-12 16:02:31 +00001145 omsb -= exponentChange;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001146 else
Neil Booth4f881702007-09-26 21:33:42 +00001147 omsb = 0;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001148 }
1149 }
1150
1151 /* Now round the number according to rounding_mode given the lost
1152 fraction. */
1153
1154 /* As specified in IEEE 754, since we do not trap we do not report
1155 underflow for exact results. */
1156 if(lost_fraction == lfExactlyZero) {
1157 /* Canonicalize zeroes. */
1158 if(omsb == 0)
1159 category = fcZero;
1160
1161 return opOK;
1162 }
1163
1164 /* Increment the significand if we're rounding away from zero. */
Neil Boothb7dea4c2007-10-03 15:16:41 +00001165 if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001166 if(omsb == 0)
1167 exponent = semantics->minExponent;
1168
1169 incrementSignificand();
1170 omsb = significandMSB() + 1;
1171
1172 /* Did the significand increment overflow? */
1173 if(omsb == (unsigned) semantics->precision + 1) {
1174 /* Renormalize by incrementing the exponent and shifting our
Neil Booth4f881702007-09-26 21:33:42 +00001175 significand right one. However if we already have the
1176 maximum exponent we overflow to infinity. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001177 if(exponent == semantics->maxExponent) {
Neil Booth4f881702007-09-26 21:33:42 +00001178 category = fcInfinity;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001179
Neil Booth4f881702007-09-26 21:33:42 +00001180 return (opStatus) (opOverflow | opInexact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001181 }
1182
1183 shiftSignificandRight(1);
1184
1185 return opInexact;
1186 }
1187 }
1188
1189 /* The normal case - we were and are not denormal, and any
1190 significand increment above didn't overflow. */
1191 if(omsb == semantics->precision)
1192 return opInexact;
1193
1194 /* We have a non-zero denormal. */
1195 assert(omsb < semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001196
1197 /* Canonicalize zeroes. */
1198 if(omsb == 0)
1199 category = fcZero;
1200
1201 /* The fcZero case is a denormal that underflowed to zero. */
1202 return (opStatus) (opUnderflow | opInexact);
1203}
1204
1205APFloat::opStatus
1206APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1207{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001208 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001209 default:
1210 assert(0);
1211
Dale Johanneseneaf08942007-08-31 04:03:46 +00001212 case convolve(fcNaN, fcZero):
1213 case convolve(fcNaN, fcNormal):
1214 case convolve(fcNaN, fcInfinity):
1215 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001216 case convolve(fcNormal, fcZero):
1217 case convolve(fcInfinity, fcNormal):
1218 case convolve(fcInfinity, fcZero):
1219 return opOK;
1220
Dale Johanneseneaf08942007-08-31 04:03:46 +00001221 case convolve(fcZero, fcNaN):
1222 case convolve(fcNormal, fcNaN):
1223 case convolve(fcInfinity, fcNaN):
1224 category = fcNaN;
1225 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001226 return opOK;
1227
1228 case convolve(fcNormal, fcInfinity):
1229 case convolve(fcZero, fcInfinity):
1230 category = fcInfinity;
1231 sign = rhs.sign ^ subtract;
1232 return opOK;
1233
1234 case convolve(fcZero, fcNormal):
1235 assign(rhs);
1236 sign = rhs.sign ^ subtract;
1237 return opOK;
1238
1239 case convolve(fcZero, fcZero):
1240 /* Sign depends on rounding mode; handled by caller. */
1241 return opOK;
1242
1243 case convolve(fcInfinity, fcInfinity):
1244 /* Differently signed infinities can only be validly
1245 subtracted. */
Cedric Venetaff9c272009-02-14 16:06:42 +00001246 if(((sign ^ rhs.sign)!=0) != subtract) {
Neil Boothe5e01942007-10-14 10:39:51 +00001247 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001248 return opInvalidOp;
1249 }
1250
1251 return opOK;
1252
1253 case convolve(fcNormal, fcNormal):
1254 return opDivByZero;
1255 }
1256}
1257
1258/* Add or subtract two normal numbers. */
1259lostFraction
1260APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1261{
1262 integerPart carry;
1263 lostFraction lost_fraction;
1264 int bits;
1265
1266 /* Determine if the operation on the absolute values is effectively
1267 an addition or subtraction. */
Hartmut Kaiser8df77a92007-10-25 23:15:31 +00001268 subtract ^= (sign ^ rhs.sign) ? true : false;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001269
1270 /* Are we bigger exponent-wise than the RHS? */
1271 bits = exponent - rhs.exponent;
1272
1273 /* Subtraction is more subtle than one might naively expect. */
1274 if(subtract) {
1275 APFloat temp_rhs(rhs);
1276 bool reverse;
1277
Chris Lattnerada530b2007-08-24 03:02:34 +00001278 if (bits == 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001279 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1280 lost_fraction = lfExactlyZero;
Chris Lattnerada530b2007-08-24 03:02:34 +00001281 } else if (bits > 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001282 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1283 shiftSignificandLeft(1);
1284 reverse = false;
Chris Lattnerada530b2007-08-24 03:02:34 +00001285 } else {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001286 lost_fraction = shiftSignificandRight(-bits - 1);
1287 temp_rhs.shiftSignificandLeft(1);
1288 reverse = true;
1289 }
1290
Chris Lattnerada530b2007-08-24 03:02:34 +00001291 if (reverse) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001292 carry = temp_rhs.subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001293 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001294 copySignificand(temp_rhs);
1295 sign = !sign;
1296 } else {
1297 carry = subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001298 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001299 }
1300
1301 /* Invert the lost fraction - it was on the RHS and
1302 subtracted. */
1303 if(lost_fraction == lfLessThanHalf)
1304 lost_fraction = lfMoreThanHalf;
1305 else if(lost_fraction == lfMoreThanHalf)
1306 lost_fraction = lfLessThanHalf;
1307
1308 /* The code above is intended to ensure that no borrow is
1309 necessary. */
1310 assert(!carry);
1311 } else {
1312 if(bits > 0) {
1313 APFloat temp_rhs(rhs);
1314
1315 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1316 carry = addSignificand(temp_rhs);
1317 } else {
1318 lost_fraction = shiftSignificandRight(-bits);
1319 carry = addSignificand(rhs);
1320 }
1321
1322 /* We have a guard bit; generating a carry cannot happen. */
1323 assert(!carry);
1324 }
1325
1326 return lost_fraction;
1327}
1328
1329APFloat::opStatus
1330APFloat::multiplySpecials(const APFloat &rhs)
1331{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001332 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001333 default:
1334 assert(0);
1335
Dale Johanneseneaf08942007-08-31 04:03:46 +00001336 case convolve(fcNaN, fcZero):
1337 case convolve(fcNaN, fcNormal):
1338 case convolve(fcNaN, fcInfinity):
1339 case convolve(fcNaN, fcNaN):
1340 return opOK;
1341
1342 case convolve(fcZero, fcNaN):
1343 case convolve(fcNormal, fcNaN):
1344 case convolve(fcInfinity, fcNaN):
1345 category = fcNaN;
1346 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001347 return opOK;
1348
1349 case convolve(fcNormal, fcInfinity):
1350 case convolve(fcInfinity, fcNormal):
1351 case convolve(fcInfinity, fcInfinity):
1352 category = fcInfinity;
1353 return opOK;
1354
1355 case convolve(fcZero, fcNormal):
1356 case convolve(fcNormal, fcZero):
1357 case convolve(fcZero, fcZero):
1358 category = fcZero;
1359 return opOK;
1360
1361 case convolve(fcZero, fcInfinity):
1362 case convolve(fcInfinity, fcZero):
Neil Boothe5e01942007-10-14 10:39:51 +00001363 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001364 return opInvalidOp;
1365
1366 case convolve(fcNormal, fcNormal):
1367 return opOK;
1368 }
1369}
1370
1371APFloat::opStatus
1372APFloat::divideSpecials(const APFloat &rhs)
1373{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001374 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001375 default:
1376 assert(0);
1377
Dale Johanneseneaf08942007-08-31 04:03:46 +00001378 case convolve(fcNaN, fcZero):
1379 case convolve(fcNaN, fcNormal):
1380 case convolve(fcNaN, fcInfinity):
1381 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001382 case convolve(fcInfinity, fcZero):
1383 case convolve(fcInfinity, fcNormal):
1384 case convolve(fcZero, fcInfinity):
1385 case convolve(fcZero, fcNormal):
1386 return opOK;
1387
Dale Johanneseneaf08942007-08-31 04:03:46 +00001388 case convolve(fcZero, fcNaN):
1389 case convolve(fcNormal, fcNaN):
1390 case convolve(fcInfinity, fcNaN):
1391 category = fcNaN;
1392 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001393 return opOK;
1394
1395 case convolve(fcNormal, fcInfinity):
1396 category = fcZero;
1397 return opOK;
1398
1399 case convolve(fcNormal, fcZero):
1400 category = fcInfinity;
1401 return opDivByZero;
1402
1403 case convolve(fcInfinity, fcInfinity):
1404 case convolve(fcZero, fcZero):
Neil Boothe5e01942007-10-14 10:39:51 +00001405 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001406 return opInvalidOp;
1407
1408 case convolve(fcNormal, fcNormal):
1409 return opOK;
1410 }
1411}
1412
Dale Johannesened6af242009-01-21 00:35:19 +00001413APFloat::opStatus
1414APFloat::modSpecials(const APFloat &rhs)
1415{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001416 switch (convolve(category, rhs.category)) {
Dale Johannesened6af242009-01-21 00:35:19 +00001417 default:
1418 assert(0);
1419
1420 case convolve(fcNaN, fcZero):
1421 case convolve(fcNaN, fcNormal):
1422 case convolve(fcNaN, fcInfinity):
1423 case convolve(fcNaN, fcNaN):
1424 case convolve(fcZero, fcInfinity):
1425 case convolve(fcZero, fcNormal):
1426 case convolve(fcNormal, fcInfinity):
1427 return opOK;
1428
1429 case convolve(fcZero, fcNaN):
1430 case convolve(fcNormal, fcNaN):
1431 case convolve(fcInfinity, fcNaN):
1432 category = fcNaN;
1433 copySignificand(rhs);
1434 return opOK;
1435
1436 case convolve(fcNormal, fcZero):
1437 case convolve(fcInfinity, fcZero):
1438 case convolve(fcInfinity, fcNormal):
1439 case convolve(fcInfinity, fcInfinity):
1440 case convolve(fcZero, fcZero):
1441 makeNaN();
1442 return opInvalidOp;
1443
1444 case convolve(fcNormal, fcNormal):
1445 return opOK;
1446 }
1447}
1448
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001449/* Change sign. */
1450void
1451APFloat::changeSign()
1452{
1453 /* Look mummy, this one's easy. */
1454 sign = !sign;
1455}
1456
Dale Johannesene15c2db2007-08-31 23:35:31 +00001457void
1458APFloat::clearSign()
1459{
1460 /* So is this one. */
1461 sign = 0;
1462}
1463
1464void
1465APFloat::copySign(const APFloat &rhs)
1466{
1467 /* And this one. */
1468 sign = rhs.sign;
1469}
1470
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001471/* Normalized addition or subtraction. */
1472APFloat::opStatus
1473APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001474 bool subtract)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001475{
1476 opStatus fs;
1477
Neil Boothcaf19d72007-10-14 10:29:28 +00001478 assertArithmeticOK(*semantics);
1479
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001480 fs = addOrSubtractSpecials(rhs, subtract);
1481
1482 /* This return code means it was not a simple case. */
1483 if(fs == opDivByZero) {
1484 lostFraction lost_fraction;
1485
1486 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1487 fs = normalize(rounding_mode, lost_fraction);
1488
1489 /* Can only be zero if we lost no fraction. */
1490 assert(category != fcZero || lost_fraction == lfExactlyZero);
1491 }
1492
1493 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1494 positive zero unless rounding to minus infinity, except that
1495 adding two like-signed zeroes gives that zero. */
1496 if(category == fcZero) {
1497 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1498 sign = (rounding_mode == rmTowardNegative);
1499 }
1500
1501 return fs;
1502}
1503
1504/* Normalized addition. */
1505APFloat::opStatus
1506APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1507{
1508 return addOrSubtract(rhs, rounding_mode, false);
1509}
1510
1511/* Normalized subtraction. */
1512APFloat::opStatus
1513APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1514{
1515 return addOrSubtract(rhs, rounding_mode, true);
1516}
1517
1518/* Normalized multiply. */
1519APFloat::opStatus
1520APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1521{
1522 opStatus fs;
1523
Neil Boothcaf19d72007-10-14 10:29:28 +00001524 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001525 sign ^= rhs.sign;
1526 fs = multiplySpecials(rhs);
1527
1528 if(category == fcNormal) {
1529 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1530 fs = normalize(rounding_mode, lost_fraction);
1531 if(lost_fraction != lfExactlyZero)
1532 fs = (opStatus) (fs | opInexact);
1533 }
1534
1535 return fs;
1536}
1537
1538/* Normalized divide. */
1539APFloat::opStatus
1540APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1541{
1542 opStatus fs;
1543
Neil Boothcaf19d72007-10-14 10:29:28 +00001544 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001545 sign ^= rhs.sign;
1546 fs = divideSpecials(rhs);
1547
1548 if(category == fcNormal) {
1549 lostFraction lost_fraction = divideSignificand(rhs);
1550 fs = normalize(rounding_mode, lost_fraction);
1551 if(lost_fraction != lfExactlyZero)
1552 fs = (opStatus) (fs | opInexact);
1553 }
1554
1555 return fs;
1556}
1557
Dale Johannesen24b66a82009-01-20 18:35:05 +00001558/* Normalized remainder. This is not currently correct in all cases. */
1559APFloat::opStatus
1560APFloat::remainder(const APFloat &rhs)
1561{
1562 opStatus fs;
1563 APFloat V = *this;
1564 unsigned int origSign = sign;
1565
1566 assertArithmeticOK(*semantics);
1567 fs = V.divide(rhs, rmNearestTiesToEven);
1568 if (fs == opDivByZero)
1569 return fs;
1570
1571 int parts = partCount();
1572 integerPart *x = new integerPart[parts];
1573 bool ignored;
1574 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1575 rmNearestTiesToEven, &ignored);
1576 if (fs==opInvalidOp)
1577 return fs;
1578
1579 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1580 rmNearestTiesToEven);
1581 assert(fs==opOK); // should always work
1582
1583 fs = V.multiply(rhs, rmNearestTiesToEven);
1584 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1585
1586 fs = subtract(V, rmNearestTiesToEven);
1587 assert(fs==opOK || fs==opInexact); // likewise
1588
1589 if (isZero())
1590 sign = origSign; // IEEE754 requires this
1591 delete[] x;
1592 return fs;
1593}
1594
1595/* Normalized llvm frem (C fmod).
1596 This is not currently correct in all cases. */
Dale Johannesene15c2db2007-08-31 23:35:31 +00001597APFloat::opStatus
1598APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1599{
1600 opStatus fs;
Neil Boothcaf19d72007-10-14 10:29:28 +00001601 assertArithmeticOK(*semantics);
Dale Johannesened6af242009-01-21 00:35:19 +00001602 fs = modSpecials(rhs);
Dale Johannesene15c2db2007-08-31 23:35:31 +00001603
Dale Johannesened6af242009-01-21 00:35:19 +00001604 if (category == fcNormal && rhs.category == fcNormal) {
1605 APFloat V = *this;
1606 unsigned int origSign = sign;
Dale Johannesene15c2db2007-08-31 23:35:31 +00001607
Dale Johannesened6af242009-01-21 00:35:19 +00001608 fs = V.divide(rhs, rmNearestTiesToEven);
1609 if (fs == opDivByZero)
1610 return fs;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001611
Dale Johannesened6af242009-01-21 00:35:19 +00001612 int parts = partCount();
1613 integerPart *x = new integerPart[parts];
1614 bool ignored;
1615 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1616 rmTowardZero, &ignored);
1617 if (fs==opInvalidOp)
1618 return fs;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001619
Dale Johannesened6af242009-01-21 00:35:19 +00001620 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1621 rmNearestTiesToEven);
1622 assert(fs==opOK); // should always work
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001623
Dale Johannesened6af242009-01-21 00:35:19 +00001624 fs = V.multiply(rhs, rounding_mode);
1625 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1626
1627 fs = subtract(V, rounding_mode);
1628 assert(fs==opOK || fs==opInexact); // likewise
1629
1630 if (isZero())
1631 sign = origSign; // IEEE754 requires this
1632 delete[] x;
1633 }
Dale Johannesene15c2db2007-08-31 23:35:31 +00001634 return fs;
1635}
1636
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001637/* Normalized fused-multiply-add. */
1638APFloat::opStatus
1639APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth4f881702007-09-26 21:33:42 +00001640 const APFloat &addend,
1641 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001642{
1643 opStatus fs;
1644
Neil Boothcaf19d72007-10-14 10:29:28 +00001645 assertArithmeticOK(*semantics);
1646
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001647 /* Post-multiplication sign, before addition. */
1648 sign ^= multiplicand.sign;
1649
1650 /* If and only if all arguments are normal do we need to do an
1651 extended-precision calculation. */
1652 if(category == fcNormal
1653 && multiplicand.category == fcNormal
1654 && addend.category == fcNormal) {
1655 lostFraction lost_fraction;
1656
1657 lost_fraction = multiplySignificand(multiplicand, &addend);
1658 fs = normalize(rounding_mode, lost_fraction);
1659 if(lost_fraction != lfExactlyZero)
1660 fs = (opStatus) (fs | opInexact);
1661
1662 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1663 positive zero unless rounding to minus infinity, except that
1664 adding two like-signed zeroes gives that zero. */
1665 if(category == fcZero && sign != addend.sign)
1666 sign = (rounding_mode == rmTowardNegative);
1667 } else {
1668 fs = multiplySpecials(multiplicand);
1669
1670 /* FS can only be opOK or opInvalidOp. There is no more work
1671 to do in the latter case. The IEEE-754R standard says it is
1672 implementation-defined in this case whether, if ADDEND is a
Dale Johanneseneaf08942007-08-31 04:03:46 +00001673 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001674
1675 If we need to do the addition we can do so with normal
1676 precision. */
1677 if(fs == opOK)
1678 fs = addOrSubtract(addend, rounding_mode, false);
1679 }
1680
1681 return fs;
1682}
1683
1684/* Comparison requires normalized numbers. */
1685APFloat::cmpResult
1686APFloat::compare(const APFloat &rhs) const
1687{
1688 cmpResult result;
1689
Neil Boothcaf19d72007-10-14 10:29:28 +00001690 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001691 assert(semantics == rhs.semantics);
1692
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001693 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001694 default:
1695 assert(0);
1696
Dale Johanneseneaf08942007-08-31 04:03:46 +00001697 case convolve(fcNaN, fcZero):
1698 case convolve(fcNaN, fcNormal):
1699 case convolve(fcNaN, fcInfinity):
1700 case convolve(fcNaN, fcNaN):
1701 case convolve(fcZero, fcNaN):
1702 case convolve(fcNormal, fcNaN):
1703 case convolve(fcInfinity, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001704 return cmpUnordered;
1705
1706 case convolve(fcInfinity, fcNormal):
1707 case convolve(fcInfinity, fcZero):
1708 case convolve(fcNormal, fcZero):
1709 if(sign)
1710 return cmpLessThan;
1711 else
1712 return cmpGreaterThan;
1713
1714 case convolve(fcNormal, fcInfinity):
1715 case convolve(fcZero, fcInfinity):
1716 case convolve(fcZero, fcNormal):
1717 if(rhs.sign)
1718 return cmpGreaterThan;
1719 else
1720 return cmpLessThan;
1721
1722 case convolve(fcInfinity, fcInfinity):
1723 if(sign == rhs.sign)
1724 return cmpEqual;
1725 else if(sign)
1726 return cmpLessThan;
1727 else
1728 return cmpGreaterThan;
1729
1730 case convolve(fcZero, fcZero):
1731 return cmpEqual;
1732
1733 case convolve(fcNormal, fcNormal):
1734 break;
1735 }
1736
1737 /* Two normal numbers. Do they have the same sign? */
1738 if(sign != rhs.sign) {
1739 if(sign)
1740 result = cmpLessThan;
1741 else
1742 result = cmpGreaterThan;
1743 } else {
1744 /* Compare absolute values; invert result if negative. */
1745 result = compareAbsoluteValue(rhs);
1746
1747 if(sign) {
1748 if(result == cmpLessThan)
Neil Booth4f881702007-09-26 21:33:42 +00001749 result = cmpGreaterThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001750 else if(result == cmpGreaterThan)
Neil Booth4f881702007-09-26 21:33:42 +00001751 result = cmpLessThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001752 }
1753 }
1754
1755 return result;
1756}
1757
Dale Johannesen23a98552008-10-09 23:00:39 +00001758/// APFloat::convert - convert a value of one floating point type to another.
1759/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1760/// records whether the transformation lost information, i.e. whether
1761/// converting the result back to the original type will produce the
1762/// original value (this is almost the same as return value==fsOK, but there
1763/// are edge cases where this is not so).
1764
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001765APFloat::opStatus
1766APFloat::convert(const fltSemantics &toSemantics,
Dale Johannesen23a98552008-10-09 23:00:39 +00001767 roundingMode rounding_mode, bool *losesInfo)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001768{
Neil Boothc8db43d2007-09-22 02:56:19 +00001769 lostFraction lostFraction;
1770 unsigned int newPartCount, oldPartCount;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001771 opStatus fs;
Neil Booth4f881702007-09-26 21:33:42 +00001772
Neil Boothcaf19d72007-10-14 10:29:28 +00001773 assertArithmeticOK(*semantics);
Dale Johannesen79f82f92008-04-20 01:34:03 +00001774 assertArithmeticOK(toSemantics);
Neil Boothc8db43d2007-09-22 02:56:19 +00001775 lostFraction = lfExactlyZero;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001776 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Boothc8db43d2007-09-22 02:56:19 +00001777 oldPartCount = partCount();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001778
Neil Boothc8db43d2007-09-22 02:56:19 +00001779 /* Handle storage complications. If our new form is wider,
1780 re-allocate our bit pattern into wider storage. If it is
1781 narrower, we ignore the excess parts, but if narrowing to a
Dale Johannesen902ff942007-09-25 17:25:00 +00001782 single part we need to free the old storage.
1783 Be careful not to reference significandParts for zeroes
1784 and infinities, since it aborts. */
Neil Boothc8db43d2007-09-22 02:56:19 +00001785 if (newPartCount > oldPartCount) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001786 integerPart *newParts;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001787 newParts = new integerPart[newPartCount];
1788 APInt::tcSet(newParts, 0, newPartCount);
Dale Johannesen902ff942007-09-25 17:25:00 +00001789 if (category==fcNormal || category==fcNaN)
1790 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001791 freeSignificand();
1792 significand.parts = newParts;
Neil Boothc8db43d2007-09-22 02:56:19 +00001793 } else if (newPartCount < oldPartCount) {
1794 /* Capture any lost fraction through truncation of parts so we get
1795 correct rounding whilst normalizing. */
Dale Johannesen902ff942007-09-25 17:25:00 +00001796 if (category==fcNormal)
1797 lostFraction = lostFractionThroughTruncation
1798 (significandParts(), oldPartCount, toSemantics.precision);
1799 if (newPartCount == 1) {
1800 integerPart newPart = 0;
Neil Booth4f881702007-09-26 21:33:42 +00001801 if (category==fcNormal || category==fcNaN)
Dale Johannesen902ff942007-09-25 17:25:00 +00001802 newPart = significandParts()[0];
1803 freeSignificand();
1804 significand.part = newPart;
1805 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001806 }
1807
1808 if(category == fcNormal) {
1809 /* Re-interpret our bit-pattern. */
1810 exponent += toSemantics.precision - semantics->precision;
1811 semantics = &toSemantics;
Neil Boothc8db43d2007-09-22 02:56:19 +00001812 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen23a98552008-10-09 23:00:39 +00001813 *losesInfo = (fs != opOK);
Dale Johannesen902ff942007-09-25 17:25:00 +00001814 } else if (category == fcNaN) {
1815 int shift = toSemantics.precision - semantics->precision;
Dale Johannesenb63fa052008-01-31 18:34:01 +00001816 // Do this now so significandParts gets the right answer
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001817 const fltSemantics *oldSemantics = semantics;
Dale Johannesenb63fa052008-01-31 18:34:01 +00001818 semantics = &toSemantics;
Dale Johannesen23a98552008-10-09 23:00:39 +00001819 *losesInfo = false;
Dale Johannesen902ff942007-09-25 17:25:00 +00001820 // No normalization here, just truncate
1821 if (shift>0)
1822 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001823 else if (shift < 0) {
1824 unsigned ushift = -shift;
Dale Johannesen23a98552008-10-09 23:00:39 +00001825 // Figure out if we are losing information. This happens
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001826 // if are shifting out something other than 0s, or if the x87 long
1827 // double input did not have its integer bit set (pseudo-NaN), or if the
1828 // x87 long double input did not have its QNan bit set (because the x87
1829 // hardware sets this bit when converting a lower-precision NaN to
1830 // x87 long double).
1831 if (APInt::tcLSB(significandParts(), newPartCount) < ushift)
Dale Johannesen23a98552008-10-09 23:00:39 +00001832 *losesInfo = true;
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001833 if (oldSemantics == &APFloat::x87DoubleExtended &&
1834 (!(*significandParts() & 0x8000000000000000ULL) ||
1835 !(*significandParts() & 0x4000000000000000ULL)))
Dale Johannesen23a98552008-10-09 23:00:39 +00001836 *losesInfo = true;
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001837 APInt::tcShiftRight(significandParts(), newPartCount, ushift);
1838 }
Dale Johannesen902ff942007-09-25 17:25:00 +00001839 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
1840 // does not give you back the same bits. This is dubious, and we
1841 // don't currently do it. You're really supposed to get
1842 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen23a98552008-10-09 23:00:39 +00001843 fs = opOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001844 } else {
1845 semantics = &toSemantics;
1846 fs = opOK;
Dale Johannesen23a98552008-10-09 23:00:39 +00001847 *losesInfo = false;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001848 }
1849
1850 return fs;
1851}
1852
1853/* Convert a floating point number to an integer according to the
1854 rounding mode. If the rounded integer value is out of range this
Neil Boothee7ae382007-11-01 22:43:37 +00001855 returns an invalid operation exception and the contents of the
1856 destination parts are unspecified. If the rounded value is in
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001857 range but the floating point number is not the exact integer, the C
1858 standard doesn't require an inexact exception to be raised. IEEE
1859 854 does require it so we do that.
1860
1861 Note that for conversions to integer type the C standard requires
1862 round-to-zero to always be used. */
1863APFloat::opStatus
Neil Boothee7ae382007-11-01 22:43:37 +00001864APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
1865 bool isSigned,
Dale Johannesen23a98552008-10-09 23:00:39 +00001866 roundingMode rounding_mode,
1867 bool *isExact) const
Neil Boothee7ae382007-11-01 22:43:37 +00001868{
1869 lostFraction lost_fraction;
1870 const integerPart *src;
1871 unsigned int dstPartsCount, truncatedBits;
1872
Evan Cheng794a7db2008-11-26 01:11:57 +00001873 assertArithmeticOK(*semantics);
Neil Boothe3d936a2007-11-02 15:10:05 +00001874
Dale Johannesen23a98552008-10-09 23:00:39 +00001875 *isExact = false;
1876
Neil Boothee7ae382007-11-01 22:43:37 +00001877 /* Handle the three special cases first. */
1878 if(category == fcInfinity || category == fcNaN)
1879 return opInvalidOp;
1880
1881 dstPartsCount = partCountForBits(width);
1882
1883 if(category == fcZero) {
1884 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesene4a42452008-10-07 00:40:01 +00001885 // Negative zero can't be represented as an int.
Dale Johannesen23a98552008-10-09 23:00:39 +00001886 *isExact = !sign;
1887 return opOK;
Neil Boothee7ae382007-11-01 22:43:37 +00001888 }
1889
1890 src = significandParts();
1891
1892 /* Step 1: place our absolute value, with any fraction truncated, in
1893 the destination. */
1894 if (exponent < 0) {
1895 /* Our absolute value is less than one; truncate everything. */
1896 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen1f54f582009-01-19 21:17:05 +00001897 /* For exponent -1 the integer bit represents .5, look at that.
1898 For smaller exponents leftmost truncated bit is 0. */
1899 truncatedBits = semantics->precision -1U - exponent;
Neil Boothee7ae382007-11-01 22:43:37 +00001900 } else {
1901 /* We want the most significant (exponent + 1) bits; the rest are
1902 truncated. */
1903 unsigned int bits = exponent + 1U;
1904
1905 /* Hopelessly large in magnitude? */
1906 if (bits > width)
1907 return opInvalidOp;
1908
1909 if (bits < semantics->precision) {
1910 /* We truncate (semantics->precision - bits) bits. */
1911 truncatedBits = semantics->precision - bits;
1912 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
1913 } else {
1914 /* We want at least as many bits as are available. */
1915 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
1916 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
1917 truncatedBits = 0;
1918 }
1919 }
1920
1921 /* Step 2: work out any lost fraction, and increment the absolute
1922 value if we would round away from zero. */
1923 if (truncatedBits) {
1924 lost_fraction = lostFractionThroughTruncation(src, partCount(),
1925 truncatedBits);
1926 if (lost_fraction != lfExactlyZero
1927 && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
1928 if (APInt::tcIncrement(parts, dstPartsCount))
1929 return opInvalidOp; /* Overflow. */
1930 }
1931 } else {
1932 lost_fraction = lfExactlyZero;
1933 }
1934
1935 /* Step 3: check if we fit in the destination. */
1936 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
1937
1938 if (sign) {
1939 if (!isSigned) {
1940 /* Negative numbers cannot be represented as unsigned. */
1941 if (omsb != 0)
1942 return opInvalidOp;
1943 } else {
1944 /* It takes omsb bits to represent the unsigned integer value.
1945 We lose a bit for the sign, but care is needed as the
1946 maximally negative integer is a special case. */
1947 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
1948 return opInvalidOp;
1949
1950 /* This case can happen because of rounding. */
1951 if (omsb > width)
1952 return opInvalidOp;
1953 }
1954
1955 APInt::tcNegate (parts, dstPartsCount);
1956 } else {
1957 if (omsb >= width + !isSigned)
1958 return opInvalidOp;
1959 }
1960
Dale Johannesen23a98552008-10-09 23:00:39 +00001961 if (lost_fraction == lfExactlyZero) {
1962 *isExact = true;
Neil Boothee7ae382007-11-01 22:43:37 +00001963 return opOK;
Dale Johannesen23a98552008-10-09 23:00:39 +00001964 } else
Neil Boothee7ae382007-11-01 22:43:37 +00001965 return opInexact;
1966}
1967
1968/* Same as convertToSignExtendedInteger, except we provide
1969 deterministic values in case of an invalid operation exception,
1970 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen23a98552008-10-09 23:00:39 +00001971 for underflow or overflow.
1972 The *isExact output tells whether the result is exact, in the sense
1973 that converting it back to the original floating point type produces
1974 the original value. This is almost equivalent to result==opOK,
1975 except for negative zeroes.
1976*/
Neil Boothee7ae382007-11-01 22:43:37 +00001977APFloat::opStatus
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001978APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth4f881702007-09-26 21:33:42 +00001979 bool isSigned,
Dale Johannesen23a98552008-10-09 23:00:39 +00001980 roundingMode rounding_mode, bool *isExact) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001981{
Neil Boothee7ae382007-11-01 22:43:37 +00001982 opStatus fs;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001983
Dale Johannesen23a98552008-10-09 23:00:39 +00001984 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
1985 isExact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001986
Neil Boothee7ae382007-11-01 22:43:37 +00001987 if (fs == opInvalidOp) {
1988 unsigned int bits, dstPartsCount;
1989
1990 dstPartsCount = partCountForBits(width);
1991
1992 if (category == fcNaN)
1993 bits = 0;
1994 else if (sign)
1995 bits = isSigned;
1996 else
1997 bits = width - isSigned;
1998
1999 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2000 if (sign && isSigned)
2001 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002002 }
2003
Neil Boothee7ae382007-11-01 22:43:37 +00002004 return fs;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002005}
2006
Neil Booth643ce592007-10-07 12:07:53 +00002007/* Convert an unsigned integer SRC to a floating point number,
2008 rounding according to ROUNDING_MODE. The sign of the floating
2009 point number is not modified. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002010APFloat::opStatus
Neil Booth643ce592007-10-07 12:07:53 +00002011APFloat::convertFromUnsignedParts(const integerPart *src,
2012 unsigned int srcCount,
2013 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002014{
Neil Booth5477f852007-10-08 14:39:42 +00002015 unsigned int omsb, precision, dstCount;
Neil Booth643ce592007-10-07 12:07:53 +00002016 integerPart *dst;
Neil Booth5477f852007-10-08 14:39:42 +00002017 lostFraction lost_fraction;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002018
Neil Boothcaf19d72007-10-14 10:29:28 +00002019 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002020 category = fcNormal;
Neil Booth5477f852007-10-08 14:39:42 +00002021 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth643ce592007-10-07 12:07:53 +00002022 dst = significandParts();
2023 dstCount = partCount();
Neil Booth5477f852007-10-08 14:39:42 +00002024 precision = semantics->precision;
Neil Booth643ce592007-10-07 12:07:53 +00002025
Neil Booth5477f852007-10-08 14:39:42 +00002026 /* We want the most significant PRECISON bits of SRC. There may not
2027 be that many; extract what we can. */
2028 if (precision <= omsb) {
2029 exponent = omsb - 1;
Neil Booth643ce592007-10-07 12:07:53 +00002030 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth5477f852007-10-08 14:39:42 +00002031 omsb - precision);
2032 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2033 } else {
2034 exponent = precision - 1;
2035 lost_fraction = lfExactlyZero;
2036 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth643ce592007-10-07 12:07:53 +00002037 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002038
2039 return normalize(rounding_mode, lost_fraction);
2040}
2041
Dan Gohman93c276e2008-02-29 01:26:11 +00002042APFloat::opStatus
2043APFloat::convertFromAPInt(const APInt &Val,
2044 bool isSigned,
2045 roundingMode rounding_mode)
2046{
2047 unsigned int partCount = Val.getNumWords();
2048 APInt api = Val;
2049
2050 sign = false;
2051 if (isSigned && api.isNegative()) {
2052 sign = true;
2053 api = -api;
2054 }
2055
2056 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2057}
2058
Neil Boothf16c5952007-10-07 12:15:41 +00002059/* Convert a two's complement integer SRC to a floating point number,
2060 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2061 integer is signed, in which case it must be sign-extended. */
2062APFloat::opStatus
2063APFloat::convertFromSignExtendedInteger(const integerPart *src,
2064 unsigned int srcCount,
2065 bool isSigned,
2066 roundingMode rounding_mode)
2067{
2068 opStatus status;
2069
Neil Boothcaf19d72007-10-14 10:29:28 +00002070 assertArithmeticOK(*semantics);
Neil Boothf16c5952007-10-07 12:15:41 +00002071 if (isSigned
2072 && APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2073 integerPart *copy;
2074
2075 /* If we're signed and negative negate a copy. */
2076 sign = true;
2077 copy = new integerPart[srcCount];
2078 APInt::tcAssign(copy, src, srcCount);
2079 APInt::tcNegate(copy, srcCount);
2080 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2081 delete [] copy;
2082 } else {
2083 sign = false;
2084 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2085 }
2086
2087 return status;
2088}
2089
Neil Boothccf596a2007-10-07 11:45:55 +00002090/* FIXME: should this just take a const APInt reference? */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002091APFloat::opStatus
Neil Boothccf596a2007-10-07 11:45:55 +00002092APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2093 unsigned int width, bool isSigned,
2094 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002095{
Dale Johannesen910993e2007-09-21 22:09:37 +00002096 unsigned int partCount = partCountForBits(width);
Dale Johannesen910993e2007-09-21 22:09:37 +00002097 APInt api = APInt(width, partCount, parts);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002098
2099 sign = false;
Dale Johannesencce23a42007-09-30 18:17:01 +00002100 if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
2101 sign = true;
2102 api = -api;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002103 }
2104
Neil Booth7a7bc0f2007-10-07 12:10:57 +00002105 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002106}
2107
2108APFloat::opStatus
2109APFloat::convertFromHexadecimalString(const char *p,
Neil Booth4f881702007-09-26 21:33:42 +00002110 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002111{
2112 lostFraction lost_fraction;
2113 integerPart *significand;
2114 unsigned int bitPos, partsCount;
2115 const char *dot, *firstSignificantDigit;
2116
2117 zeroSignificand();
2118 exponent = 0;
2119 category = fcNormal;
2120
2121 significand = significandParts();
2122 partsCount = partCount();
2123 bitPos = partsCount * integerPartWidth;
2124
Neil Booth33d4c922007-10-07 08:51:21 +00002125 /* Skip leading zeroes and any (hexa)decimal point. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002126 p = skipLeadingZeroesAndAnyDot(p, &dot);
2127 firstSignificantDigit = p;
2128
2129 for(;;) {
Dale Johannesen386f3e92008-05-14 22:53:25 +00002130 integerPart hex_value;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002131
2132 if(*p == '.') {
2133 assert(dot == 0);
2134 dot = p++;
2135 }
2136
2137 hex_value = hexDigitValue(*p);
2138 if(hex_value == -1U) {
2139 lost_fraction = lfExactlyZero;
2140 break;
2141 }
2142
2143 p++;
2144
2145 /* Store the number whilst 4-bit nibbles remain. */
2146 if(bitPos) {
2147 bitPos -= 4;
2148 hex_value <<= bitPos % integerPartWidth;
2149 significand[bitPos / integerPartWidth] |= hex_value;
2150 } else {
2151 lost_fraction = trailingHexadecimalFraction(p, hex_value);
2152 while(hexDigitValue(*p) != -1U)
Neil Booth4f881702007-09-26 21:33:42 +00002153 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002154 break;
2155 }
2156 }
2157
2158 /* Hex floats require an exponent but not a hexadecimal point. */
2159 assert(*p == 'p' || *p == 'P');
2160
2161 /* Ignore the exponent if we are zero. */
2162 if(p != firstSignificantDigit) {
2163 int expAdjustment;
2164
2165 /* Implicit hexadecimal point? */
2166 if(!dot)
2167 dot = p;
2168
2169 /* Calculate the exponent adjustment implicit in the number of
2170 significant digits. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002171 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002172 if(expAdjustment < 0)
2173 expAdjustment++;
2174 expAdjustment = expAdjustment * 4 - 1;
2175
2176 /* Adjust for writing the significand starting at the most
2177 significant nibble. */
2178 expAdjustment += semantics->precision;
2179 expAdjustment -= partsCount * integerPartWidth;
2180
2181 /* Adjust for the given exponent. */
2182 exponent = totalExponent(p, expAdjustment);
2183 }
2184
2185 return normalize(rounding_mode, lost_fraction);
2186}
2187
2188APFloat::opStatus
Neil Booth96c74712007-10-12 16:02:31 +00002189APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2190 unsigned sigPartCount, int exp,
2191 roundingMode rounding_mode)
2192{
2193 unsigned int parts, pow5PartCount;
Neil Boothcaf19d72007-10-14 10:29:28 +00002194 fltSemantics calcSemantics = { 32767, -32767, 0, true };
Neil Booth96c74712007-10-12 16:02:31 +00002195 integerPart pow5Parts[maxPowerOfFiveParts];
2196 bool isNearest;
2197
2198 isNearest = (rounding_mode == rmNearestTiesToEven
2199 || rounding_mode == rmNearestTiesToAway);
2200
2201 parts = partCountForBits(semantics->precision + 11);
2202
2203 /* Calculate pow(5, abs(exp)). */
2204 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2205
2206 for (;; parts *= 2) {
2207 opStatus sigStatus, powStatus;
2208 unsigned int excessPrecision, truncatedBits;
2209
2210 calcSemantics.precision = parts * integerPartWidth - 1;
2211 excessPrecision = calcSemantics.precision - semantics->precision;
2212 truncatedBits = excessPrecision;
2213
2214 APFloat decSig(calcSemantics, fcZero, sign);
2215 APFloat pow5(calcSemantics, fcZero, false);
2216
2217 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2218 rmNearestTiesToEven);
2219 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2220 rmNearestTiesToEven);
2221 /* Add exp, as 10^n = 5^n * 2^n. */
2222 decSig.exponent += exp;
2223
2224 lostFraction calcLostFraction;
Evan Cheng48e8c802008-05-02 21:15:08 +00002225 integerPart HUerr, HUdistance;
2226 unsigned int powHUerr;
Neil Booth96c74712007-10-12 16:02:31 +00002227
2228 if (exp >= 0) {
2229 /* multiplySignificand leaves the precision-th bit set to 1. */
2230 calcLostFraction = decSig.multiplySignificand(pow5, NULL);
2231 powHUerr = powStatus != opOK;
2232 } else {
2233 calcLostFraction = decSig.divideSignificand(pow5);
2234 /* Denormal numbers have less precision. */
2235 if (decSig.exponent < semantics->minExponent) {
2236 excessPrecision += (semantics->minExponent - decSig.exponent);
2237 truncatedBits = excessPrecision;
2238 if (excessPrecision > calcSemantics.precision)
2239 excessPrecision = calcSemantics.precision;
2240 }
2241 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002242 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Booth96c74712007-10-12 16:02:31 +00002243 }
2244
2245 /* Both multiplySignificand and divideSignificand return the
2246 result with the integer bit set. */
2247 assert (APInt::tcExtractBit
2248 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2249
2250 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2251 powHUerr);
2252 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2253 excessPrecision, isNearest);
2254
2255 /* Are we guaranteed to round correctly if we truncate? */
2256 if (HUdistance >= HUerr) {
2257 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2258 calcSemantics.precision - excessPrecision,
2259 excessPrecision);
2260 /* Take the exponent of decSig. If we tcExtract-ed less bits
2261 above we must adjust our exponent to compensate for the
2262 implicit right shift. */
2263 exponent = (decSig.exponent + semantics->precision
2264 - (calcSemantics.precision - excessPrecision));
2265 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2266 decSig.partCount(),
2267 truncatedBits);
2268 return normalize(rounding_mode, calcLostFraction);
2269 }
2270 }
2271}
2272
2273APFloat::opStatus
2274APFloat::convertFromDecimalString(const char *p, roundingMode rounding_mode)
2275{
Neil Booth1870f292007-10-14 10:16:12 +00002276 decimalInfo D;
Neil Booth96c74712007-10-12 16:02:31 +00002277 opStatus fs;
2278
Neil Booth1870f292007-10-14 10:16:12 +00002279 /* Scan the text. */
2280 interpretDecimal(p, &D);
Neil Booth96c74712007-10-12 16:02:31 +00002281
Neil Booth686700e2007-10-15 15:00:55 +00002282 /* Handle the quick cases. First the case of no significant digits,
2283 i.e. zero, and then exponents that are obviously too large or too
2284 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2285 definitely overflows if
2286
2287 (exp - 1) * L >= maxExponent
2288
2289 and definitely underflows to zero where
2290
2291 (exp + 1) * L <= minExponent - precision
2292
2293 With integer arithmetic the tightest bounds for L are
2294
2295 93/28 < L < 196/59 [ numerator <= 256 ]
2296 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2297 */
2298
Neil Boothcc233592007-12-05 13:06:04 +00002299 if (decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Booth96c74712007-10-12 16:02:31 +00002300 category = fcZero;
2301 fs = opOK;
Neil Booth686700e2007-10-15 15:00:55 +00002302 } else if ((D.normalizedExponent + 1) * 28738
2303 <= 8651 * (semantics->minExponent - (int) semantics->precision)) {
2304 /* Underflow to zero and round. */
2305 zeroSignificand();
2306 fs = normalize(rounding_mode, lfLessThanHalf);
2307 } else if ((D.normalizedExponent - 1) * 42039
2308 >= 12655 * semantics->maxExponent) {
2309 /* Overflow and round. */
2310 fs = handleOverflow(rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002311 } else {
Neil Booth1870f292007-10-14 10:16:12 +00002312 integerPart *decSignificand;
2313 unsigned int partCount;
Neil Booth96c74712007-10-12 16:02:31 +00002314
Neil Booth1870f292007-10-14 10:16:12 +00002315 /* A tight upper bound on number of bits required to hold an
Neil Booth686700e2007-10-15 15:00:55 +00002316 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth1870f292007-10-14 10:16:12 +00002317 to hold the full significand, and an extra part required by
2318 tcMultiplyPart. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002319 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth686700e2007-10-15 15:00:55 +00002320 partCount = partCountForBits(1 + 196 * partCount / 59);
Neil Booth1870f292007-10-14 10:16:12 +00002321 decSignificand = new integerPart[partCount + 1];
2322 partCount = 0;
Neil Booth96c74712007-10-12 16:02:31 +00002323
Neil Booth1870f292007-10-14 10:16:12 +00002324 /* Convert to binary efficiently - we do almost all multiplication
2325 in an integerPart. When this would overflow do we do a single
2326 bignum multiplication, and then revert again to multiplication
2327 in an integerPart. */
2328 do {
2329 integerPart decValue, val, multiplier;
2330
2331 val = 0;
2332 multiplier = 1;
2333
2334 do {
2335 if (*p == '.')
2336 p++;
2337
2338 decValue = decDigitValue(*p++);
2339 multiplier *= 10;
2340 val = val * 10 + decValue;
2341 /* The maximum number that can be multiplied by ten with any
2342 digit added without overflowing an integerPart. */
2343 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2344
2345 /* Multiply out the current part. */
2346 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2347 partCount, partCount + 1, false);
2348
2349 /* If we used another part (likely but not guaranteed), increase
2350 the count. */
2351 if (decSignificand[partCount])
2352 partCount++;
2353 } while (p <= D.lastSigDigit);
Neil Booth96c74712007-10-12 16:02:31 +00002354
Neil Booth43a4b282007-11-01 22:51:07 +00002355 category = fcNormal;
Neil Booth96c74712007-10-12 16:02:31 +00002356 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth1870f292007-10-14 10:16:12 +00002357 D.exponent, rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002358
Neil Booth1870f292007-10-14 10:16:12 +00002359 delete [] decSignificand;
2360 }
Neil Booth96c74712007-10-12 16:02:31 +00002361
2362 return fs;
2363}
2364
2365APFloat::opStatus
Neil Booth4f881702007-09-26 21:33:42 +00002366APFloat::convertFromString(const char *p, roundingMode rounding_mode)
2367{
Neil Boothcaf19d72007-10-14 10:29:28 +00002368 assertArithmeticOK(*semantics);
2369
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002370 /* Handle a leading minus sign. */
2371 if(*p == '-')
2372 sign = 1, p++;
2373 else
2374 sign = 0;
2375
2376 if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
2377 return convertFromHexadecimalString(p + 2, rounding_mode);
Bill Wendlingb7c0d942008-11-27 08:00:12 +00002378
2379 return convertFromDecimalString(p, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002380}
Dale Johannesen343e7702007-08-24 00:56:33 +00002381
Neil Bootha30b0ee2007-10-03 22:26:02 +00002382/* Write out a hexadecimal representation of the floating point value
2383 to DST, which must be of sufficient size, in the C99 form
2384 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2385 excluding the terminating NUL.
2386
2387 If UPPERCASE, the output is in upper case, otherwise in lower case.
2388
2389 HEXDIGITS digits appear altogether, rounding the value if
2390 necessary. If HEXDIGITS is 0, the minimal precision to display the
2391 number precisely is used instead. If nothing would appear after
2392 the decimal point it is suppressed.
2393
2394 The decimal exponent is always printed and has at least one digit.
2395 Zero values display an exponent of zero. Infinities and NaNs
2396 appear as "infinity" or "nan" respectively.
2397
2398 The above rules are as specified by C99. There is ambiguity about
2399 what the leading hexadecimal digit should be. This implementation
2400 uses whatever is necessary so that the exponent is displayed as
2401 stored. This implies the exponent will fall within the IEEE format
2402 range, and the leading hexadecimal digit will be 0 (for denormals),
2403 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2404 any other digits zero).
2405*/
2406unsigned int
2407APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2408 bool upperCase, roundingMode rounding_mode) const
2409{
2410 char *p;
2411
Neil Boothcaf19d72007-10-14 10:29:28 +00002412 assertArithmeticOK(*semantics);
2413
Neil Bootha30b0ee2007-10-03 22:26:02 +00002414 p = dst;
2415 if (sign)
2416 *dst++ = '-';
2417
2418 switch (category) {
2419 case fcInfinity:
2420 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2421 dst += sizeof infinityL - 1;
2422 break;
2423
2424 case fcNaN:
2425 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2426 dst += sizeof NaNU - 1;
2427 break;
2428
2429 case fcZero:
2430 *dst++ = '0';
2431 *dst++ = upperCase ? 'X': 'x';
2432 *dst++ = '0';
2433 if (hexDigits > 1) {
2434 *dst++ = '.';
2435 memset (dst, '0', hexDigits - 1);
2436 dst += hexDigits - 1;
2437 }
2438 *dst++ = upperCase ? 'P': 'p';
2439 *dst++ = '0';
2440 break;
2441
2442 case fcNormal:
2443 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2444 break;
2445 }
2446
2447 *dst = 0;
2448
Evan Cheng48e8c802008-05-02 21:15:08 +00002449 return static_cast<unsigned int>(dst - p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002450}
2451
2452/* Does the hard work of outputting the correctly rounded hexadecimal
2453 form of a normal floating point number with the specified number of
2454 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2455 digits necessary to print the value precisely is output. */
2456char *
2457APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2458 bool upperCase,
2459 roundingMode rounding_mode) const
2460{
2461 unsigned int count, valueBits, shift, partsCount, outputDigits;
2462 const char *hexDigitChars;
2463 const integerPart *significand;
2464 char *p;
2465 bool roundUp;
2466
2467 *dst++ = '0';
2468 *dst++ = upperCase ? 'X': 'x';
2469
2470 roundUp = false;
2471 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2472
2473 significand = significandParts();
2474 partsCount = partCount();
2475
2476 /* +3 because the first digit only uses the single integer bit, so
2477 we have 3 virtual zero most-significant-bits. */
2478 valueBits = semantics->precision + 3;
2479 shift = integerPartWidth - valueBits % integerPartWidth;
2480
2481 /* The natural number of digits required ignoring trailing
2482 insignificant zeroes. */
2483 outputDigits = (valueBits - significandLSB () + 3) / 4;
2484
2485 /* hexDigits of zero means use the required number for the
2486 precision. Otherwise, see if we are truncating. If we are,
Neil Booth978661d2007-10-06 00:24:48 +00002487 find out if we need to round away from zero. */
Neil Bootha30b0ee2007-10-03 22:26:02 +00002488 if (hexDigits) {
2489 if (hexDigits < outputDigits) {
2490 /* We are dropping non-zero bits, so need to check how to round.
2491 "bits" is the number of dropped bits. */
2492 unsigned int bits;
2493 lostFraction fraction;
2494
2495 bits = valueBits - hexDigits * 4;
2496 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2497 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2498 }
2499 outputDigits = hexDigits;
2500 }
2501
2502 /* Write the digits consecutively, and start writing in the location
2503 of the hexadecimal point. We move the most significant digit
2504 left and add the hexadecimal point later. */
2505 p = ++dst;
2506
2507 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2508
2509 while (outputDigits && count) {
2510 integerPart part;
2511
2512 /* Put the most significant integerPartWidth bits in "part". */
2513 if (--count == partsCount)
2514 part = 0; /* An imaginary higher zero part. */
2515 else
2516 part = significand[count] << shift;
2517
2518 if (count && shift)
2519 part |= significand[count - 1] >> (integerPartWidth - shift);
2520
2521 /* Convert as much of "part" to hexdigits as we can. */
2522 unsigned int curDigits = integerPartWidth / 4;
2523
2524 if (curDigits > outputDigits)
2525 curDigits = outputDigits;
2526 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2527 outputDigits -= curDigits;
2528 }
2529
2530 if (roundUp) {
2531 char *q = dst;
2532
2533 /* Note that hexDigitChars has a trailing '0'. */
2534 do {
2535 q--;
2536 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth978661d2007-10-06 00:24:48 +00002537 } while (*q == '0');
2538 assert (q >= p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002539 } else {
2540 /* Add trailing zeroes. */
2541 memset (dst, '0', outputDigits);
2542 dst += outputDigits;
2543 }
2544
2545 /* Move the most significant digit to before the point, and if there
2546 is something after the decimal point add it. This must come
2547 after rounding above. */
2548 p[-1] = p[0];
2549 if (dst -1 == p)
2550 dst--;
2551 else
2552 p[0] = '.';
2553
2554 /* Finally output the exponent. */
2555 *dst++ = upperCase ? 'P': 'p';
2556
Neil Booth92f7e8d2007-10-06 07:29:25 +00002557 return writeSignedDecimal (dst, exponent);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002558}
2559
Dale Johannesen343e7702007-08-24 00:56:33 +00002560// For good performance it is desirable for different APFloats
2561// to produce different integers.
2562uint32_t
Neil Booth4f881702007-09-26 21:33:42 +00002563APFloat::getHashValue() const
2564{
Dale Johannesen343e7702007-08-24 00:56:33 +00002565 if (category==fcZero) return sign<<8 | semantics->precision ;
2566 else if (category==fcInfinity) return sign<<9 | semantics->precision;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002567 else if (category==fcNaN) return 1<<10 | semantics->precision;
Dale Johannesen343e7702007-08-24 00:56:33 +00002568 else {
2569 uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
2570 const integerPart* p = significandParts();
2571 for (int i=partCount(); i>0; i--, p++)
Evan Cheng48e8c802008-05-02 21:15:08 +00002572 hash ^= ((uint32_t)*p) ^ (uint32_t)((*p)>>32);
Dale Johannesen343e7702007-08-24 00:56:33 +00002573 return hash;
2574 }
2575}
2576
2577// Conversion from APFloat to/from host float/double. It may eventually be
2578// possible to eliminate these and have everybody deal with APFloats, but that
2579// will take a while. This approach will not easily extend to long double.
Dale Johannesena72a5a02007-09-20 23:47:58 +00002580// Current implementation requires integerPartWidth==64, which is correct at
2581// the moment but could be made more general.
Dale Johannesen343e7702007-08-24 00:56:33 +00002582
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002583// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesena72a5a02007-09-20 23:47:58 +00002584// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002585
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002586APInt
Neil Booth4f881702007-09-26 21:33:42 +00002587APFloat::convertF80LongDoubleAPFloatToAPInt() const
2588{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002589 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002590 assert (partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002591
2592 uint64_t myexponent, mysignificand;
2593
2594 if (category==fcNormal) {
2595 myexponent = exponent+16383; //bias
Dale Johannesena72a5a02007-09-20 23:47:58 +00002596 mysignificand = significandParts()[0];
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002597 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2598 myexponent = 0; // denormal
2599 } else if (category==fcZero) {
2600 myexponent = 0;
2601 mysignificand = 0;
2602 } else if (category==fcInfinity) {
2603 myexponent = 0x7fff;
2604 mysignificand = 0x8000000000000000ULL;
Chris Lattnera11ef822007-10-06 06:13:42 +00002605 } else {
2606 assert(category == fcNaN && "Unknown category");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002607 myexponent = 0x7fff;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002608 mysignificand = significandParts()[0];
Chris Lattnera11ef822007-10-06 06:13:42 +00002609 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002610
2611 uint64_t words[2];
Dale Johannesen1b25cb22009-03-23 21:16:53 +00002612 words[0] = mysignificand;
2613 words[1] = ((uint64_t)(sign & 1) << 15) |
2614 (myexponent & 0x7fffLL);
Chris Lattnera11ef822007-10-06 06:13:42 +00002615 return APInt(80, 2, words);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002616}
2617
2618APInt
Dale Johannesena471c2e2007-10-11 18:07:22 +00002619APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2620{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002621 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002622 assert (partCount()==2);
2623
2624 uint64_t myexponent, mysignificand, myexponent2, mysignificand2;
2625
2626 if (category==fcNormal) {
2627 myexponent = exponent + 1023; //bias
2628 myexponent2 = exponent2 + 1023;
2629 mysignificand = significandParts()[0];
2630 mysignificand2 = significandParts()[1];
2631 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2632 myexponent = 0; // denormal
2633 if (myexponent2==1 && !(mysignificand2 & 0x10000000000000LL))
2634 myexponent2 = 0; // denormal
2635 } else if (category==fcZero) {
2636 myexponent = 0;
2637 mysignificand = 0;
2638 myexponent2 = 0;
2639 mysignificand2 = 0;
2640 } else if (category==fcInfinity) {
2641 myexponent = 0x7ff;
2642 myexponent2 = 0;
2643 mysignificand = 0;
2644 mysignificand2 = 0;
2645 } else {
2646 assert(category == fcNaN && "Unknown category");
2647 myexponent = 0x7ff;
2648 mysignificand = significandParts()[0];
2649 myexponent2 = exponent2;
2650 mysignificand2 = significandParts()[1];
2651 }
2652
2653 uint64_t words[2];
Evan Cheng48e8c802008-05-02 21:15:08 +00002654 words[0] = ((uint64_t)(sign & 1) << 63) |
Dale Johannesena471c2e2007-10-11 18:07:22 +00002655 ((myexponent & 0x7ff) << 52) |
2656 (mysignificand & 0xfffffffffffffLL);
Evan Cheng48e8c802008-05-02 21:15:08 +00002657 words[1] = ((uint64_t)(sign2 & 1) << 63) |
Dale Johannesena471c2e2007-10-11 18:07:22 +00002658 ((myexponent2 & 0x7ff) << 52) |
2659 (mysignificand2 & 0xfffffffffffffLL);
2660 return APInt(128, 2, words);
2661}
2662
2663APInt
Neil Booth4f881702007-09-26 21:33:42 +00002664APFloat::convertDoubleAPFloatToAPInt() const
2665{
Dan Gohmancb648f92007-09-14 20:08:19 +00002666 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002667 assert (partCount()==1);
2668
Dale Johanneseneaf08942007-08-31 04:03:46 +00002669 uint64_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002670
2671 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002672 myexponent = exponent+1023; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002673 mysignificand = *significandParts();
2674 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2675 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002676 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002677 myexponent = 0;
2678 mysignificand = 0;
2679 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002680 myexponent = 0x7ff;
2681 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002682 } else {
2683 assert(category == fcNaN && "Unknown category!");
Dale Johannesen343e7702007-08-24 00:56:33 +00002684 myexponent = 0x7ff;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002685 mysignificand = *significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002686 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002687
Evan Cheng48e8c802008-05-02 21:15:08 +00002688 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattnera11ef822007-10-06 06:13:42 +00002689 ((myexponent & 0x7ff) << 52) |
2690 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesen343e7702007-08-24 00:56:33 +00002691}
2692
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002693APInt
Neil Booth4f881702007-09-26 21:33:42 +00002694APFloat::convertFloatAPFloatToAPInt() const
2695{
Dan Gohmancb648f92007-09-14 20:08:19 +00002696 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002697 assert (partCount()==1);
Neil Booth4f881702007-09-26 21:33:42 +00002698
Dale Johanneseneaf08942007-08-31 04:03:46 +00002699 uint32_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002700
2701 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002702 myexponent = exponent+127; //bias
Evan Cheng48e8c802008-05-02 21:15:08 +00002703 mysignificand = (uint32_t)*significandParts();
Dale Johannesend0763b92007-11-17 01:02:27 +00002704 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002705 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002706 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002707 myexponent = 0;
2708 mysignificand = 0;
2709 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002710 myexponent = 0xff;
2711 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002712 } else {
2713 assert(category == fcNaN && "Unknown category!");
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002714 myexponent = 0xff;
Evan Cheng48e8c802008-05-02 21:15:08 +00002715 mysignificand = (uint32_t)*significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002716 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002717
Chris Lattnera11ef822007-10-06 06:13:42 +00002718 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2719 (mysignificand & 0x7fffff)));
Dale Johannesen343e7702007-08-24 00:56:33 +00002720}
2721
Dale Johannesena471c2e2007-10-11 18:07:22 +00002722// This function creates an APInt that is just a bit map of the floating
2723// point constant as it would appear in memory. It is not a conversion,
2724// and treating the result as a normal integer is unlikely to be useful.
2725
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002726APInt
Dale Johannesen7111b022008-10-09 18:53:47 +00002727APFloat::bitcastToAPInt() const
Neil Booth4f881702007-09-26 21:33:42 +00002728{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002729 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002730 return convertFloatAPFloatToAPInt();
Chris Lattnera11ef822007-10-06 06:13:42 +00002731
Dan Gohmanb10abe12008-01-29 12:08:20 +00002732 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002733 return convertDoubleAPFloatToAPInt();
Neil Booth4f881702007-09-26 21:33:42 +00002734
Dan Gohmanb10abe12008-01-29 12:08:20 +00002735 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
Dale Johannesena471c2e2007-10-11 18:07:22 +00002736 return convertPPCDoubleDoubleAPFloatToAPInt();
2737
Dan Gohmanb10abe12008-01-29 12:08:20 +00002738 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
Chris Lattnera11ef822007-10-06 06:13:42 +00002739 "unknown format!");
2740 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002741}
2742
Neil Booth4f881702007-09-26 21:33:42 +00002743float
2744APFloat::convertToFloat() const
2745{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002746 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Dale Johannesen7111b022008-10-09 18:53:47 +00002747 APInt api = bitcastToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002748 return api.bitsToFloat();
2749}
2750
Neil Booth4f881702007-09-26 21:33:42 +00002751double
2752APFloat::convertToDouble() const
2753{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002754 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Dale Johannesen7111b022008-10-09 18:53:47 +00002755 APInt api = bitcastToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002756 return api.bitsToDouble();
2757}
2758
Dale Johannesend3d8ce32008-10-06 18:22:29 +00002759/// Integer bit is explicit in this format. Intel hardware (387 and later)
2760/// does not support these bit patterns:
2761/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
2762/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
2763/// exponent = 0, integer bit 1 ("pseudodenormal")
2764/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
2765/// At the moment, the first two are treated as NaNs, the second two as Normal.
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002766void
Neil Booth4f881702007-09-26 21:33:42 +00002767APFloat::initFromF80LongDoubleAPInt(const APInt &api)
2768{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002769 assert(api.getBitWidth()==80);
2770 uint64_t i1 = api.getRawData()[0];
2771 uint64_t i2 = api.getRawData()[1];
Dale Johannesen1b25cb22009-03-23 21:16:53 +00002772 uint64_t myexponent = (i2 & 0x7fff);
2773 uint64_t mysignificand = i1;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002774
2775 initialize(&APFloat::x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002776 assert(partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002777
Dale Johannesen1b25cb22009-03-23 21:16:53 +00002778 sign = static_cast<unsigned int>(i2>>15);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002779 if (myexponent==0 && mysignificand==0) {
2780 // exponent, significand meaningless
2781 category = fcZero;
2782 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
2783 // exponent, significand meaningless
2784 category = fcInfinity;
2785 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
2786 // exponent meaningless
2787 category = fcNaN;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002788 significandParts()[0] = mysignificand;
2789 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002790 } else {
2791 category = fcNormal;
2792 exponent = myexponent - 16383;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002793 significandParts()[0] = mysignificand;
2794 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002795 if (myexponent==0) // denormal
2796 exponent = -16382;
Neil Booth4f881702007-09-26 21:33:42 +00002797 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002798}
2799
2800void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002801APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
2802{
2803 assert(api.getBitWidth()==128);
2804 uint64_t i1 = api.getRawData()[0];
2805 uint64_t i2 = api.getRawData()[1];
2806 uint64_t myexponent = (i1 >> 52) & 0x7ff;
2807 uint64_t mysignificand = i1 & 0xfffffffffffffLL;
2808 uint64_t myexponent2 = (i2 >> 52) & 0x7ff;
2809 uint64_t mysignificand2 = i2 & 0xfffffffffffffLL;
2810
2811 initialize(&APFloat::PPCDoubleDouble);
2812 assert(partCount()==2);
2813
Evan Cheng48e8c802008-05-02 21:15:08 +00002814 sign = static_cast<unsigned int>(i1>>63);
2815 sign2 = static_cast<unsigned int>(i2>>63);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002816 if (myexponent==0 && mysignificand==0) {
2817 // exponent, significand meaningless
2818 // exponent2 and significand2 are required to be 0; we don't check
2819 category = fcZero;
2820 } else if (myexponent==0x7ff && mysignificand==0) {
2821 // exponent, significand meaningless
2822 // exponent2 and significand2 are required to be 0; we don't check
2823 category = fcInfinity;
2824 } else if (myexponent==0x7ff && mysignificand!=0) {
2825 // exponent meaningless. So is the whole second word, but keep it
2826 // for determinism.
2827 category = fcNaN;
2828 exponent2 = myexponent2;
2829 significandParts()[0] = mysignificand;
2830 significandParts()[1] = mysignificand2;
2831 } else {
2832 category = fcNormal;
2833 // Note there is no category2; the second word is treated as if it is
2834 // fcNormal, although it might be something else considered by itself.
2835 exponent = myexponent - 1023;
2836 exponent2 = myexponent2 - 1023;
2837 significandParts()[0] = mysignificand;
2838 significandParts()[1] = mysignificand2;
2839 if (myexponent==0) // denormal
2840 exponent = -1022;
2841 else
2842 significandParts()[0] |= 0x10000000000000LL; // integer bit
2843 if (myexponent2==0)
2844 exponent2 = -1022;
2845 else
2846 significandParts()[1] |= 0x10000000000000LL; // integer bit
2847 }
2848}
2849
2850void
Neil Booth4f881702007-09-26 21:33:42 +00002851APFloat::initFromDoubleAPInt(const APInt &api)
2852{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002853 assert(api.getBitWidth()==64);
2854 uint64_t i = *api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002855 uint64_t myexponent = (i >> 52) & 0x7ff;
2856 uint64_t mysignificand = i & 0xfffffffffffffLL;
2857
Dale Johannesen343e7702007-08-24 00:56:33 +00002858 initialize(&APFloat::IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002859 assert(partCount()==1);
2860
Evan Cheng48e8c802008-05-02 21:15:08 +00002861 sign = static_cast<unsigned int>(i>>63);
Dale Johannesen343e7702007-08-24 00:56:33 +00002862 if (myexponent==0 && mysignificand==0) {
2863 // exponent, significand meaningless
2864 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002865 } else if (myexponent==0x7ff && mysignificand==0) {
2866 // exponent, significand meaningless
2867 category = fcInfinity;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002868 } else if (myexponent==0x7ff && mysignificand!=0) {
2869 // exponent meaningless
2870 category = fcNaN;
2871 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002872 } else {
Dale Johannesen343e7702007-08-24 00:56:33 +00002873 category = fcNormal;
2874 exponent = myexponent - 1023;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002875 *significandParts() = mysignificand;
2876 if (myexponent==0) // denormal
2877 exponent = -1022;
2878 else
2879 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth4f881702007-09-26 21:33:42 +00002880 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002881}
2882
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002883void
Neil Booth4f881702007-09-26 21:33:42 +00002884APFloat::initFromFloatAPInt(const APInt & api)
2885{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002886 assert(api.getBitWidth()==32);
2887 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002888 uint32_t myexponent = (i >> 23) & 0xff;
2889 uint32_t mysignificand = i & 0x7fffff;
2890
Dale Johannesen343e7702007-08-24 00:56:33 +00002891 initialize(&APFloat::IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002892 assert(partCount()==1);
2893
Dale Johanneseneaf08942007-08-31 04:03:46 +00002894 sign = i >> 31;
Dale Johannesen343e7702007-08-24 00:56:33 +00002895 if (myexponent==0 && mysignificand==0) {
2896 // exponent, significand meaningless
2897 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002898 } else if (myexponent==0xff && mysignificand==0) {
2899 // exponent, significand meaningless
2900 category = fcInfinity;
Dale Johannesen902ff942007-09-25 17:25:00 +00002901 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002902 // sign, exponent, significand meaningless
Dale Johanneseneaf08942007-08-31 04:03:46 +00002903 category = fcNaN;
2904 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002905 } else {
2906 category = fcNormal;
Dale Johannesen343e7702007-08-24 00:56:33 +00002907 exponent = myexponent - 127; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002908 *significandParts() = mysignificand;
2909 if (myexponent==0) // denormal
2910 exponent = -126;
2911 else
2912 *significandParts() |= 0x800000; // integer bit
Dale Johannesen343e7702007-08-24 00:56:33 +00002913 }
2914}
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002915
2916/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesena471c2e2007-10-11 18:07:22 +00002917/// we infer the floating point type from the size of the APInt. The
2918/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
2919/// when the size is anything else).
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002920void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002921APFloat::initFromAPInt(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002922{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002923 if (api.getBitWidth() == 32)
2924 return initFromFloatAPInt(api);
2925 else if (api.getBitWidth()==64)
2926 return initFromDoubleAPInt(api);
2927 else if (api.getBitWidth()==80)
2928 return initFromF80LongDoubleAPInt(api);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002929 else if (api.getBitWidth()==128 && !isIEEE)
2930 return initFromPPCDoubleDoubleAPInt(api);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002931 else
2932 assert(0);
2933}
2934
Dale Johannesena471c2e2007-10-11 18:07:22 +00002935APFloat::APFloat(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002936{
Dale Johannesena471c2e2007-10-11 18:07:22 +00002937 initFromAPInt(api, isIEEE);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002938}
2939
Neil Booth4f881702007-09-26 21:33:42 +00002940APFloat::APFloat(float f)
2941{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002942 APInt api = APInt(32, 0);
2943 initFromAPInt(api.floatToBits(f));
2944}
2945
Neil Booth4f881702007-09-26 21:33:42 +00002946APFloat::APFloat(double d)
2947{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002948 APInt api = APInt(64, 0);
2949 initFromAPInt(api.doubleToBits(d));
2950}