blob: c296770385f9f71cbb3f114774d2add3ea8eea20 [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
78/* Put a bunch of private, handy routines in an anonymous namespace. */
79namespace {
80
Dan Gohman3bd659b2008-04-10 21:11:47 +000081 static inline unsigned int
Chris Lattnerb39cdde2007-08-20 22:49:32 +000082 partCountForBits(unsigned int bits)
83 {
84 return ((bits) + integerPartWidth - 1) / integerPartWidth;
85 }
86
Neil Booth1870f292007-10-14 10:16:12 +000087 /* Returns 0U-9U. Return values >= 10U are not digits. */
Dan Gohman3bd659b2008-04-10 21:11:47 +000088 static inline unsigned int
Neil Booth1870f292007-10-14 10:16:12 +000089 decDigitValue(unsigned int c)
Chris Lattnerb39cdde2007-08-20 22:49:32 +000090 {
Neil Booth1870f292007-10-14 10:16:12 +000091 return c - '0';
Chris Lattnerb39cdde2007-08-20 22:49:32 +000092 }
93
Dan Gohman3bd659b2008-04-10 21:11:47 +000094 static unsigned int
Neil Booth96c74712007-10-12 16:02:31 +000095 hexDigitValue(unsigned int c)
Chris Lattnerb39cdde2007-08-20 22:49:32 +000096 {
97 unsigned int r;
98
99 r = c - '0';
100 if(r <= 9)
101 return r;
102
103 r = c - 'A';
104 if(r <= 5)
105 return r + 10;
106
107 r = c - 'a';
108 if(r <= 5)
109 return r + 10;
110
111 return -1U;
112 }
113
Dan Gohman3bd659b2008-04-10 21:11:47 +0000114 static inline void
Neil Boothcaf19d72007-10-14 10:29:28 +0000115 assertArithmeticOK(const llvm::fltSemantics &semantics) {
116 assert(semantics.arithmeticOK
117 && "Compile-time arithmetic does not support these semantics");
118 }
119
Neil Booth1870f292007-10-14 10:16:12 +0000120 /* Return the value of a decimal exponent of the form
121 [+-]ddddddd.
122
123 If the exponent overflows, returns a large exponent with the
124 appropriate sign. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000125 static int
Neil Booth1870f292007-10-14 10:16:12 +0000126 readExponent(const char *p)
127 {
128 bool isNegative;
129 unsigned int absExponent;
130 const unsigned int overlargeExponent = 24000; /* FIXME. */
131
132 isNegative = (*p == '-');
133 if (*p == '-' || *p == '+')
134 p++;
135
136 absExponent = decDigitValue(*p++);
137 assert (absExponent < 10U);
138
139 for (;;) {
140 unsigned int value;
141
142 value = decDigitValue(*p);
143 if (value >= 10U)
144 break;
145
146 p++;
147 value += absExponent * 10;
148 if (absExponent >= overlargeExponent) {
149 absExponent = overlargeExponent;
150 break;
151 }
152 absExponent = value;
153 }
154
155 if (isNegative)
156 return -(int) absExponent;
157 else
158 return (int) absExponent;
159 }
160
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000161 /* This is ugly and needs cleaning up, but I don't immediately see
162 how whilst remaining safe. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000163 static int
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000164 totalExponent(const char *p, int exponentAdjustment)
165 {
Evan Cheng48e8c802008-05-02 21:15:08 +0000166 int unsignedExponent;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000167 bool negative, overflow;
Evan Cheng48e8c802008-05-02 21:15:08 +0000168 int exponent;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000169
170 /* Move past the exponent letter and sign to the digits. */
171 p++;
172 negative = *p == '-';
173 if(*p == '-' || *p == '+')
174 p++;
175
176 unsignedExponent = 0;
177 overflow = false;
178 for(;;) {
179 unsigned int value;
180
Neil Booth1870f292007-10-14 10:16:12 +0000181 value = decDigitValue(*p);
182 if(value >= 10U)
Neil Booth4f881702007-09-26 21:33:42 +0000183 break;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000184
185 p++;
186 unsignedExponent = unsignedExponent * 10 + value;
187 if(unsignedExponent > 65535)
Neil Booth4f881702007-09-26 21:33:42 +0000188 overflow = true;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000189 }
190
191 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
192 overflow = true;
193
194 if(!overflow) {
195 exponent = unsignedExponent;
196 if(negative)
Neil Booth4f881702007-09-26 21:33:42 +0000197 exponent = -exponent;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000198 exponent += exponentAdjustment;
199 if(exponent > 65535 || exponent < -65536)
Neil Booth4f881702007-09-26 21:33:42 +0000200 overflow = true;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000201 }
202
203 if(overflow)
204 exponent = negative ? -65536: 65535;
205
206 return exponent;
207 }
208
Dan Gohman3bd659b2008-04-10 21:11:47 +0000209 static const char *
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000210 skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
211 {
212 *dot = 0;
213 while(*p == '0')
214 p++;
215
216 if(*p == '.') {
217 *dot = p++;
218 while(*p == '0')
Neil Booth4f881702007-09-26 21:33:42 +0000219 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000220 }
221
222 return p;
223 }
224
Neil Booth1870f292007-10-14 10:16:12 +0000225 /* Given a normal decimal floating point number of the form
226
227 dddd.dddd[eE][+-]ddd
228
229 where the decimal point and exponent are optional, fill out the
Neil Booth686700e2007-10-15 15:00:55 +0000230 structure D. Exponent is appropriate if the significand is
231 treated as an integer, and normalizedExponent if the significand
232 is taken to have the decimal point after a single leading
233 non-zero digit.
234
Neil Bootha89e45f2007-12-05 13:01:24 +0000235 If the value is zero, V->firstSigDigit points to a non-digit, and
236 the return exponent is zero.
Neil Booth686700e2007-10-15 15:00:55 +0000237 */
Neil Booth1870f292007-10-14 10:16:12 +0000238 struct decimalInfo {
239 const char *firstSigDigit;
240 const char *lastSigDigit;
241 int exponent;
Neil Booth686700e2007-10-15 15:00:55 +0000242 int normalizedExponent;
Neil Booth1870f292007-10-14 10:16:12 +0000243 };
244
Dan Gohman3bd659b2008-04-10 21:11:47 +0000245 static void
Neil Booth1870f292007-10-14 10:16:12 +0000246 interpretDecimal(const char *p, decimalInfo *D)
247 {
248 const char *dot;
249
250 p = skipLeadingZeroesAndAnyDot (p, &dot);
251
252 D->firstSigDigit = p;
253 D->exponent = 0;
Neil Booth686700e2007-10-15 15:00:55 +0000254 D->normalizedExponent = 0;
Neil Booth1870f292007-10-14 10:16:12 +0000255
256 for (;;) {
257 if (*p == '.') {
258 assert(dot == 0);
259 dot = p++;
260 }
261 if (decDigitValue(*p) >= 10U)
262 break;
263 p++;
264 }
265
266 /* If number is all zerooes accept any exponent. */
Neil Boothcc233592007-12-05 13:06:04 +0000267 if (p != D->firstSigDigit) {
Neil Booth1870f292007-10-14 10:16:12 +0000268 if (*p == 'e' || *p == 'E')
269 D->exponent = readExponent(p + 1);
270
271 /* Implied decimal point? */
272 if (!dot)
273 dot = p;
274
275 /* Drop insignificant trailing zeroes. */
276 do
277 do
278 p--;
279 while (*p == '0');
280 while (*p == '.');
281
Neil Booth686700e2007-10-15 15:00:55 +0000282 /* Adjust the exponents for any decimal point. */
Evan Cheng48e8c802008-05-02 21:15:08 +0000283 D->exponent += static_cast<exponent_t>((dot - p) - (dot > p));
284 D->normalizedExponent = (D->exponent +
285 static_cast<exponent_t>((p - D->firstSigDigit)
286 - (dot > D->firstSigDigit && dot < p)));
Neil Booth1870f292007-10-14 10:16:12 +0000287 }
288
289 D->lastSigDigit = p;
290 }
291
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000292 /* Return the trailing fraction of a hexadecimal number.
293 DIGITVALUE is the first hex digit of the fraction, P points to
294 the next digit. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000295 static lostFraction
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000296 trailingHexadecimalFraction(const char *p, unsigned int digitValue)
297 {
298 unsigned int hexDigit;
299
300 /* If the first trailing digit isn't 0 or 8 we can work out the
301 fraction immediately. */
302 if(digitValue > 8)
303 return lfMoreThanHalf;
304 else if(digitValue < 8 && digitValue > 0)
305 return lfLessThanHalf;
306
307 /* Otherwise we need to find the first non-zero digit. */
308 while(*p == '0')
309 p++;
310
311 hexDigit = hexDigitValue(*p);
312
313 /* If we ran off the end it is exactly zero or one-half, otherwise
314 a little more. */
315 if(hexDigit == -1U)
316 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
317 else
318 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
319 }
320
Neil Boothb7dea4c2007-10-03 15:16:41 +0000321 /* Return the fraction lost were a bignum truncated losing the least
322 significant BITS bits. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000323 static lostFraction
Neil Bootha30b0ee2007-10-03 22:26:02 +0000324 lostFractionThroughTruncation(const integerPart *parts,
Neil Booth4f881702007-09-26 21:33:42 +0000325 unsigned int partCount,
326 unsigned int bits)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000327 {
328 unsigned int lsb;
329
330 lsb = APInt::tcLSB(parts, partCount);
331
332 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
333 if(bits <= lsb)
334 return lfExactlyZero;
335 if(bits == lsb + 1)
336 return lfExactlyHalf;
337 if(bits <= partCount * integerPartWidth
338 && APInt::tcExtractBit(parts, bits - 1))
339 return lfMoreThanHalf;
340
341 return lfLessThanHalf;
342 }
343
344 /* Shift DST right BITS bits noting lost fraction. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000345 static lostFraction
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000346 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
347 {
348 lostFraction lost_fraction;
349
350 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
351
352 APInt::tcShiftRight(dst, parts, bits);
353
354 return lost_fraction;
355 }
Neil Bootha30b0ee2007-10-03 22:26:02 +0000356
Neil Booth33d4c922007-10-07 08:51:21 +0000357 /* Combine the effect of two lost fractions. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000358 static lostFraction
Neil Booth33d4c922007-10-07 08:51:21 +0000359 combineLostFractions(lostFraction moreSignificant,
360 lostFraction lessSignificant)
361 {
362 if(lessSignificant != lfExactlyZero) {
363 if(moreSignificant == lfExactlyZero)
364 moreSignificant = lfLessThanHalf;
365 else if(moreSignificant == lfExactlyHalf)
366 moreSignificant = lfMoreThanHalf;
367 }
368
369 return moreSignificant;
370 }
Neil Bootha30b0ee2007-10-03 22:26:02 +0000371
Neil Booth96c74712007-10-12 16:02:31 +0000372 /* The error from the true value, in half-ulps, on multiplying two
373 floating point numbers, which differ from the value they
374 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
375 than the returned value.
376
377 See "How to Read Floating Point Numbers Accurately" by William D
378 Clinger. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000379 static unsigned int
Neil Booth96c74712007-10-12 16:02:31 +0000380 HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
381 {
382 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
383
384 if (HUerr1 + HUerr2 == 0)
385 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
386 else
387 return inexactMultiply + 2 * (HUerr1 + HUerr2);
388 }
389
390 /* The number of ulps from the boundary (zero, or half if ISNEAREST)
391 when the least significant BITS are truncated. BITS cannot be
392 zero. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000393 static integerPart
Neil Booth96c74712007-10-12 16:02:31 +0000394 ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
395 {
396 unsigned int count, partBits;
397 integerPart part, boundary;
398
399 assert (bits != 0);
400
401 bits--;
402 count = bits / integerPartWidth;
403 partBits = bits % integerPartWidth + 1;
404
405 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
406
407 if (isNearest)
408 boundary = (integerPart) 1 << (partBits - 1);
409 else
410 boundary = 0;
411
412 if (count == 0) {
413 if (part - boundary <= boundary - part)
414 return part - boundary;
415 else
416 return boundary - part;
417 }
418
419 if (part == boundary) {
420 while (--count)
421 if (parts[count])
422 return ~(integerPart) 0; /* A lot. */
423
424 return parts[0];
425 } else if (part == boundary - 1) {
426 while (--count)
427 if (~parts[count])
428 return ~(integerPart) 0; /* A lot. */
429
430 return -parts[0];
431 }
432
433 return ~(integerPart) 0; /* A lot. */
434 }
435
436 /* Place pow(5, power) in DST, and return the number of parts used.
437 DST must be at least one part larger than size of the answer. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000438 static unsigned int
Neil Booth96c74712007-10-12 16:02:31 +0000439 powerOf5(integerPart *dst, unsigned int power)
440 {
Dan Gohman7c2e4f22008-05-12 16:38:14 +0000441 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
442 15625, 78125 };
Neil Booth96c74712007-10-12 16:02:31 +0000443 static integerPart pow5s[maxPowerOfFiveParts * 2 + 5] = { 78125 * 5 };
444 static unsigned int partsCount[16] = { 1 };
445
446 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
447 unsigned int result;
448
449 assert(power <= maxExponent);
450
451 p1 = dst;
452 p2 = scratch;
453
454 *p1 = firstEightPowers[power & 7];
455 power >>= 3;
456
457 result = 1;
458 pow5 = pow5s;
459
460 for (unsigned int n = 0; power; power >>= 1, n++) {
461 unsigned int pc;
462
463 pc = partsCount[n];
464
465 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
466 if (pc == 0) {
467 pc = partsCount[n - 1];
468 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
469 pc *= 2;
470 if (pow5[pc - 1] == 0)
471 pc--;
472 partsCount[n] = pc;
473 }
474
475 if (power & 1) {
476 integerPart *tmp;
477
478 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
479 result += pc;
480 if (p2[result - 1] == 0)
481 result--;
482
483 /* Now result is in p1 with partsCount parts and p2 is scratch
484 space. */
485 tmp = p1, p1 = p2, p2 = tmp;
486 }
487
488 pow5 += pc;
489 }
490
491 if (p1 != dst)
492 APInt::tcAssign(dst, p1, result);
493
494 return result;
495 }
496
Neil Bootha30b0ee2007-10-03 22:26:02 +0000497 /* Zero at the end to avoid modular arithmetic when adding one; used
498 when rounding up during hexadecimal output. */
499 static const char hexDigitsLower[] = "0123456789abcdef0";
500 static const char hexDigitsUpper[] = "0123456789ABCDEF0";
501 static const char infinityL[] = "infinity";
502 static const char infinityU[] = "INFINITY";
503 static const char NaNL[] = "nan";
504 static const char NaNU[] = "NAN";
505
506 /* Write out an integerPart in hexadecimal, starting with the most
507 significant nibble. Write out exactly COUNT hexdigits, return
508 COUNT. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000509 static unsigned int
Neil Bootha30b0ee2007-10-03 22:26:02 +0000510 partAsHex (char *dst, integerPart part, unsigned int count,
511 const char *hexDigitChars)
512 {
513 unsigned int result = count;
514
515 assert (count != 0 && count <= integerPartWidth / 4);
516
517 part >>= (integerPartWidth - 4 * count);
518 while (count--) {
519 dst[count] = hexDigitChars[part & 0xf];
520 part >>= 4;
521 }
522
523 return result;
524 }
525
Neil Booth92f7e8d2007-10-06 07:29:25 +0000526 /* Write out an unsigned decimal integer. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000527 static char *
Neil Booth92f7e8d2007-10-06 07:29:25 +0000528 writeUnsignedDecimal (char *dst, unsigned int n)
Neil Bootha30b0ee2007-10-03 22:26:02 +0000529 {
Neil Booth92f7e8d2007-10-06 07:29:25 +0000530 char buff[40], *p;
Neil Bootha30b0ee2007-10-03 22:26:02 +0000531
Neil Booth92f7e8d2007-10-06 07:29:25 +0000532 p = buff;
533 do
534 *p++ = '0' + n % 10;
535 while (n /= 10);
536
537 do
538 *dst++ = *--p;
539 while (p != buff);
540
541 return dst;
542 }
543
544 /* Write out a signed decimal integer. */
Dan Gohman3bd659b2008-04-10 21:11:47 +0000545 static char *
Neil Booth92f7e8d2007-10-06 07:29:25 +0000546 writeSignedDecimal (char *dst, int value)
547 {
548 if (value < 0) {
Neil Bootha30b0ee2007-10-03 22:26:02 +0000549 *dst++ = '-';
Neil Booth92f7e8d2007-10-06 07:29:25 +0000550 dst = writeUnsignedDecimal(dst, -(unsigned) value);
551 } else
552 dst = writeUnsignedDecimal(dst, value);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000553
554 return dst;
555 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000556}
557
558/* Constructors. */
559void
560APFloat::initialize(const fltSemantics *ourSemantics)
561{
562 unsigned int count;
563
564 semantics = ourSemantics;
565 count = partCount();
566 if(count > 1)
567 significand.parts = new integerPart[count];
568}
569
570void
571APFloat::freeSignificand()
572{
573 if(partCount() > 1)
574 delete [] significand.parts;
575}
576
577void
578APFloat::assign(const APFloat &rhs)
579{
580 assert(semantics == rhs.semantics);
581
582 sign = rhs.sign;
583 category = rhs.category;
584 exponent = rhs.exponent;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000585 sign2 = rhs.sign2;
586 exponent2 = rhs.exponent2;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000587 if(category == fcNormal || category == fcNaN)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000588 copySignificand(rhs);
589}
590
591void
592APFloat::copySignificand(const APFloat &rhs)
593{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000594 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000595 assert(rhs.partCount() >= partCount());
596
597 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +0000598 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000599}
600
Neil Boothe5e01942007-10-14 10:39:51 +0000601/* Make this number a NaN, with an arbitrary but deterministic value
602 for the significand. */
603void
604APFloat::makeNaN(void)
605{
606 category = fcNaN;
607 APInt::tcSet(significandParts(), ~0U, partCount());
608}
609
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000610APFloat &
611APFloat::operator=(const APFloat &rhs)
612{
613 if(this != &rhs) {
614 if(semantics != rhs.semantics) {
615 freeSignificand();
616 initialize(rhs.semantics);
617 }
618 assign(rhs);
619 }
620
621 return *this;
622}
623
Dale Johannesen343e7702007-08-24 00:56:33 +0000624bool
Dale Johannesen12595d72007-08-24 22:09:56 +0000625APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000626 if (this == &rhs)
627 return true;
628 if (semantics != rhs.semantics ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000629 category != rhs.category ||
630 sign != rhs.sign)
Dale Johannesen343e7702007-08-24 00:56:33 +0000631 return false;
Dan Gohmanb10abe12008-01-29 12:08:20 +0000632 if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
Dale Johannesena471c2e2007-10-11 18:07:22 +0000633 sign2 != rhs.sign2)
634 return false;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000635 if (category==fcZero || category==fcInfinity)
Dale Johannesen343e7702007-08-24 00:56:33 +0000636 return true;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000637 else if (category==fcNormal && exponent!=rhs.exponent)
638 return false;
Dan Gohmanb10abe12008-01-29 12:08:20 +0000639 else if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
Dale Johannesena471c2e2007-10-11 18:07:22 +0000640 exponent2!=rhs.exponent2)
641 return false;
Dale Johannesen343e7702007-08-24 00:56:33 +0000642 else {
Dale Johannesen343e7702007-08-24 00:56:33 +0000643 int i= partCount();
644 const integerPart* p=significandParts();
645 const integerPart* q=rhs.significandParts();
646 for (; i>0; i--, p++, q++) {
647 if (*p != *q)
648 return false;
649 }
650 return true;
651 }
652}
653
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000654APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
655{
Neil Boothcaf19d72007-10-14 10:29:28 +0000656 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000657 initialize(&ourSemantics);
658 sign = 0;
659 zeroSignificand();
660 exponent = ourSemantics.precision - 1;
661 significandParts()[0] = value;
662 normalize(rmNearestTiesToEven, lfExactlyZero);
663}
664
665APFloat::APFloat(const fltSemantics &ourSemantics,
Neil Booth4f881702007-09-26 21:33:42 +0000666 fltCategory ourCategory, bool negative)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000667{
Neil Boothcaf19d72007-10-14 10:29:28 +0000668 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000669 initialize(&ourSemantics);
670 category = ourCategory;
671 sign = negative;
672 if(category == fcNormal)
673 category = fcZero;
Neil Boothe5e01942007-10-14 10:39:51 +0000674 else if (ourCategory == fcNaN)
675 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000676}
677
678APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
679{
Neil Boothcaf19d72007-10-14 10:29:28 +0000680 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000681 initialize(&ourSemantics);
682 convertFromString(text, rmNearestTiesToEven);
683}
684
685APFloat::APFloat(const APFloat &rhs)
686{
687 initialize(rhs.semantics);
688 assign(rhs);
689}
690
691APFloat::~APFloat()
692{
693 freeSignificand();
694}
695
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000696// Profile - This method 'profiles' an APFloat for use with FoldingSet.
697void APFloat::Profile(FoldingSetNodeID& ID) const {
Dale Johannesen7111b022008-10-09 18:53:47 +0000698 ID.Add(bitcastToAPInt());
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000699}
700
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000701unsigned int
702APFloat::partCount() const
703{
Dale Johannesena72a5a02007-09-20 23:47:58 +0000704 return partCountForBits(semantics->precision + 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000705}
706
707unsigned int
708APFloat::semanticsPrecision(const fltSemantics &semantics)
709{
710 return semantics.precision;
711}
712
713const integerPart *
714APFloat::significandParts() const
715{
716 return const_cast<APFloat *>(this)->significandParts();
717}
718
719integerPart *
720APFloat::significandParts()
721{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000722 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000723
724 if(partCount() > 1)
725 return significand.parts;
726 else
727 return &significand.part;
728}
729
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000730void
731APFloat::zeroSignificand()
732{
733 category = fcNormal;
734 APInt::tcSet(significandParts(), 0, partCount());
735}
736
737/* Increment an fcNormal floating point number's significand. */
738void
739APFloat::incrementSignificand()
740{
741 integerPart carry;
742
743 carry = APInt::tcIncrement(significandParts(), partCount());
744
745 /* Our callers should never cause us to overflow. */
746 assert(carry == 0);
747}
748
749/* Add the significand of the RHS. Returns the carry flag. */
750integerPart
751APFloat::addSignificand(const APFloat &rhs)
752{
753 integerPart *parts;
754
755 parts = significandParts();
756
757 assert(semantics == rhs.semantics);
758 assert(exponent == rhs.exponent);
759
760 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
761}
762
763/* Subtract the significand of the RHS with a borrow flag. Returns
764 the borrow flag. */
765integerPart
766APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
767{
768 integerPart *parts;
769
770 parts = significandParts();
771
772 assert(semantics == rhs.semantics);
773 assert(exponent == rhs.exponent);
774
775 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth4f881702007-09-26 21:33:42 +0000776 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000777}
778
779/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
780 on to the full-precision result of the multiplication. Returns the
781 lost fraction. */
782lostFraction
783APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
784{
Neil Booth4f881702007-09-26 21:33:42 +0000785 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000786 unsigned int partsCount, newPartsCount, precision;
787 integerPart *lhsSignificand;
788 integerPart scratch[4];
789 integerPart *fullSignificand;
790 lostFraction lost_fraction;
Dale Johannesen23a98552008-10-09 23:00:39 +0000791 bool ignored;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000792
793 assert(semantics == rhs.semantics);
794
795 precision = semantics->precision;
796 newPartsCount = partCountForBits(precision * 2);
797
798 if(newPartsCount > 4)
799 fullSignificand = new integerPart[newPartsCount];
800 else
801 fullSignificand = scratch;
802
803 lhsSignificand = significandParts();
804 partsCount = partCount();
805
806 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth978661d2007-10-06 00:24:48 +0000807 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000808
809 lost_fraction = lfExactlyZero;
810 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
811 exponent += rhs.exponent;
812
813 if(addend) {
814 Significand savedSignificand = significand;
815 const fltSemantics *savedSemantics = semantics;
816 fltSemantics extendedSemantics;
817 opStatus status;
818 unsigned int extendedPrecision;
819
820 /* Normalize our MSB. */
821 extendedPrecision = precision + precision - 1;
822 if(omsb != extendedPrecision)
823 {
Neil Booth4f881702007-09-26 21:33:42 +0000824 APInt::tcShiftLeft(fullSignificand, newPartsCount,
825 extendedPrecision - omsb);
826 exponent -= extendedPrecision - omsb;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000827 }
828
829 /* Create new semantics. */
830 extendedSemantics = *semantics;
831 extendedSemantics.precision = extendedPrecision;
832
833 if(newPartsCount == 1)
834 significand.part = fullSignificand[0];
835 else
836 significand.parts = fullSignificand;
837 semantics = &extendedSemantics;
838
839 APFloat extendedAddend(*addend);
Dale Johannesen23a98552008-10-09 23:00:39 +0000840 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000841 assert(status == opOK);
842 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
843
844 /* Restore our state. */
845 if(newPartsCount == 1)
846 fullSignificand[0] = significand.part;
847 significand = savedSignificand;
848 semantics = savedSemantics;
849
850 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
851 }
852
853 exponent -= (precision - 1);
854
855 if(omsb > precision) {
856 unsigned int bits, significantParts;
857 lostFraction lf;
858
859 bits = omsb - precision;
860 significantParts = partCountForBits(omsb);
861 lf = shiftRight(fullSignificand, significantParts, bits);
862 lost_fraction = combineLostFractions(lf, lost_fraction);
863 exponent += bits;
864 }
865
866 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
867
868 if(newPartsCount > 4)
869 delete [] fullSignificand;
870
871 return lost_fraction;
872}
873
874/* Multiply the significands of LHS and RHS to DST. */
875lostFraction
876APFloat::divideSignificand(const APFloat &rhs)
877{
878 unsigned int bit, i, partsCount;
879 const integerPart *rhsSignificand;
880 integerPart *lhsSignificand, *dividend, *divisor;
881 integerPart scratch[4];
882 lostFraction lost_fraction;
883
884 assert(semantics == rhs.semantics);
885
886 lhsSignificand = significandParts();
887 rhsSignificand = rhs.significandParts();
888 partsCount = partCount();
889
890 if(partsCount > 2)
891 dividend = new integerPart[partsCount * 2];
892 else
893 dividend = scratch;
894
895 divisor = dividend + partsCount;
896
897 /* Copy the dividend and divisor as they will be modified in-place. */
898 for(i = 0; i < partsCount; i++) {
899 dividend[i] = lhsSignificand[i];
900 divisor[i] = rhsSignificand[i];
901 lhsSignificand[i] = 0;
902 }
903
904 exponent -= rhs.exponent;
905
906 unsigned int precision = semantics->precision;
907
908 /* Normalize the divisor. */
909 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
910 if(bit) {
911 exponent += bit;
912 APInt::tcShiftLeft(divisor, partsCount, bit);
913 }
914
915 /* Normalize the dividend. */
916 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
917 if(bit) {
918 exponent -= bit;
919 APInt::tcShiftLeft(dividend, partsCount, bit);
920 }
921
Neil Booth96c74712007-10-12 16:02:31 +0000922 /* Ensure the dividend >= divisor initially for the loop below.
923 Incidentally, this means that the division loop below is
924 guaranteed to set the integer bit to one. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000925 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
926 exponent--;
927 APInt::tcShiftLeft(dividend, partsCount, 1);
928 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
929 }
930
931 /* Long division. */
932 for(bit = precision; bit; bit -= 1) {
933 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
934 APInt::tcSubtract(dividend, divisor, 0, partsCount);
935 APInt::tcSetBit(lhsSignificand, bit - 1);
936 }
937
938 APInt::tcShiftLeft(dividend, partsCount, 1);
939 }
940
941 /* Figure out the lost fraction. */
942 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
943
944 if(cmp > 0)
945 lost_fraction = lfMoreThanHalf;
946 else if(cmp == 0)
947 lost_fraction = lfExactlyHalf;
948 else if(APInt::tcIsZero(dividend, partsCount))
949 lost_fraction = lfExactlyZero;
950 else
951 lost_fraction = lfLessThanHalf;
952
953 if(partsCount > 2)
954 delete [] dividend;
955
956 return lost_fraction;
957}
958
959unsigned int
960APFloat::significandMSB() const
961{
962 return APInt::tcMSB(significandParts(), partCount());
963}
964
965unsigned int
966APFloat::significandLSB() const
967{
968 return APInt::tcLSB(significandParts(), partCount());
969}
970
971/* Note that a zero result is NOT normalized to fcZero. */
972lostFraction
973APFloat::shiftSignificandRight(unsigned int bits)
974{
975 /* Our exponent should not overflow. */
976 assert((exponent_t) (exponent + bits) >= exponent);
977
978 exponent += bits;
979
980 return shiftRight(significandParts(), partCount(), bits);
981}
982
983/* Shift the significand left BITS bits, subtract BITS from its exponent. */
984void
985APFloat::shiftSignificandLeft(unsigned int bits)
986{
987 assert(bits < semantics->precision);
988
989 if(bits) {
990 unsigned int partsCount = partCount();
991
992 APInt::tcShiftLeft(significandParts(), partsCount, bits);
993 exponent -= bits;
994
995 assert(!APInt::tcIsZero(significandParts(), partsCount));
996 }
997}
998
999APFloat::cmpResult
1000APFloat::compareAbsoluteValue(const APFloat &rhs) const
1001{
1002 int compare;
1003
1004 assert(semantics == rhs.semantics);
1005 assert(category == fcNormal);
1006 assert(rhs.category == fcNormal);
1007
1008 compare = exponent - rhs.exponent;
1009
1010 /* If exponents are equal, do an unsigned bignum comparison of the
1011 significands. */
1012 if(compare == 0)
1013 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +00001014 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001015
1016 if(compare > 0)
1017 return cmpGreaterThan;
1018 else if(compare < 0)
1019 return cmpLessThan;
1020 else
1021 return cmpEqual;
1022}
1023
1024/* Handle overflow. Sign is preserved. We either become infinity or
1025 the largest finite number. */
1026APFloat::opStatus
1027APFloat::handleOverflow(roundingMode rounding_mode)
1028{
1029 /* Infinity? */
1030 if(rounding_mode == rmNearestTiesToEven
1031 || rounding_mode == rmNearestTiesToAway
1032 || (rounding_mode == rmTowardPositive && !sign)
1033 || (rounding_mode == rmTowardNegative && sign))
1034 {
1035 category = fcInfinity;
1036 return (opStatus) (opOverflow | opInexact);
1037 }
1038
1039 /* Otherwise we become the largest finite number. */
1040 category = fcNormal;
1041 exponent = semantics->maxExponent;
1042 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth4f881702007-09-26 21:33:42 +00001043 semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001044
1045 return opInexact;
1046}
1047
Neil Boothb7dea4c2007-10-03 15:16:41 +00001048/* Returns TRUE if, when truncating the current number, with BIT the
1049 new LSB, with the given lost fraction and rounding mode, the result
1050 would need to be rounded away from zero (i.e., by increasing the
1051 signficand). This routine must work for fcZero of both signs, and
1052 fcNormal numbers. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001053bool
1054APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Boothb7dea4c2007-10-03 15:16:41 +00001055 lostFraction lost_fraction,
1056 unsigned int bit) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001057{
Dale Johanneseneaf08942007-08-31 04:03:46 +00001058 /* NaNs and infinities should not have lost fractions. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001059 assert(category == fcNormal || category == fcZero);
1060
Neil Boothb7dea4c2007-10-03 15:16:41 +00001061 /* Current callers never pass this so we don't handle it. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001062 assert(lost_fraction != lfExactlyZero);
1063
1064 switch(rounding_mode) {
1065 default:
1066 assert(0);
1067
1068 case rmNearestTiesToAway:
1069 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1070
1071 case rmNearestTiesToEven:
1072 if(lost_fraction == lfMoreThanHalf)
1073 return true;
1074
1075 /* Our zeroes don't have a significand to test. */
1076 if(lost_fraction == lfExactlyHalf && category != fcZero)
Neil Boothb7dea4c2007-10-03 15:16:41 +00001077 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001078
1079 return false;
1080
1081 case rmTowardZero:
1082 return false;
1083
1084 case rmTowardPositive:
1085 return sign == false;
1086
1087 case rmTowardNegative:
1088 return sign == true;
1089 }
1090}
1091
1092APFloat::opStatus
1093APFloat::normalize(roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001094 lostFraction lost_fraction)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001095{
Neil Booth4f881702007-09-26 21:33:42 +00001096 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001097 int exponentChange;
1098
1099 if(category != fcNormal)
1100 return opOK;
1101
1102 /* Before rounding normalize the exponent of fcNormal numbers. */
1103 omsb = significandMSB() + 1;
1104
1105 if(omsb) {
1106 /* OMSB is numbered from 1. We want to place it in the integer
1107 bit numbered PRECISON if possible, with a compensating change in
1108 the exponent. */
1109 exponentChange = omsb - semantics->precision;
1110
1111 /* If the resulting exponent is too high, overflow according to
1112 the rounding mode. */
1113 if(exponent + exponentChange > semantics->maxExponent)
1114 return handleOverflow(rounding_mode);
1115
1116 /* Subnormal numbers have exponent minExponent, and their MSB
1117 is forced based on that. */
1118 if(exponent + exponentChange < semantics->minExponent)
1119 exponentChange = semantics->minExponent - exponent;
1120
1121 /* Shifting left is easy as we don't lose precision. */
1122 if(exponentChange < 0) {
1123 assert(lost_fraction == lfExactlyZero);
1124
1125 shiftSignificandLeft(-exponentChange);
1126
1127 return opOK;
1128 }
1129
1130 if(exponentChange > 0) {
1131 lostFraction lf;
1132
1133 /* Shift right and capture any new lost fraction. */
1134 lf = shiftSignificandRight(exponentChange);
1135
1136 lost_fraction = combineLostFractions(lf, lost_fraction);
1137
1138 /* Keep OMSB up-to-date. */
1139 if(omsb > (unsigned) exponentChange)
Neil Booth96c74712007-10-12 16:02:31 +00001140 omsb -= exponentChange;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001141 else
Neil Booth4f881702007-09-26 21:33:42 +00001142 omsb = 0;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001143 }
1144 }
1145
1146 /* Now round the number according to rounding_mode given the lost
1147 fraction. */
1148
1149 /* As specified in IEEE 754, since we do not trap we do not report
1150 underflow for exact results. */
1151 if(lost_fraction == lfExactlyZero) {
1152 /* Canonicalize zeroes. */
1153 if(omsb == 0)
1154 category = fcZero;
1155
1156 return opOK;
1157 }
1158
1159 /* Increment the significand if we're rounding away from zero. */
Neil Boothb7dea4c2007-10-03 15:16:41 +00001160 if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001161 if(omsb == 0)
1162 exponent = semantics->minExponent;
1163
1164 incrementSignificand();
1165 omsb = significandMSB() + 1;
1166
1167 /* Did the significand increment overflow? */
1168 if(omsb == (unsigned) semantics->precision + 1) {
1169 /* Renormalize by incrementing the exponent and shifting our
Neil Booth4f881702007-09-26 21:33:42 +00001170 significand right one. However if we already have the
1171 maximum exponent we overflow to infinity. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001172 if(exponent == semantics->maxExponent) {
Neil Booth4f881702007-09-26 21:33:42 +00001173 category = fcInfinity;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001174
Neil Booth4f881702007-09-26 21:33:42 +00001175 return (opStatus) (opOverflow | opInexact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001176 }
1177
1178 shiftSignificandRight(1);
1179
1180 return opInexact;
1181 }
1182 }
1183
1184 /* The normal case - we were and are not denormal, and any
1185 significand increment above didn't overflow. */
1186 if(omsb == semantics->precision)
1187 return opInexact;
1188
1189 /* We have a non-zero denormal. */
1190 assert(omsb < semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001191
1192 /* Canonicalize zeroes. */
1193 if(omsb == 0)
1194 category = fcZero;
1195
1196 /* The fcZero case is a denormal that underflowed to zero. */
1197 return (opStatus) (opUnderflow | opInexact);
1198}
1199
1200APFloat::opStatus
1201APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1202{
1203 switch(convolve(category, rhs.category)) {
1204 default:
1205 assert(0);
1206
Dale Johanneseneaf08942007-08-31 04:03:46 +00001207 case convolve(fcNaN, fcZero):
1208 case convolve(fcNaN, fcNormal):
1209 case convolve(fcNaN, fcInfinity):
1210 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001211 case convolve(fcNormal, fcZero):
1212 case convolve(fcInfinity, fcNormal):
1213 case convolve(fcInfinity, fcZero):
1214 return opOK;
1215
Dale Johanneseneaf08942007-08-31 04:03:46 +00001216 case convolve(fcZero, fcNaN):
1217 case convolve(fcNormal, fcNaN):
1218 case convolve(fcInfinity, fcNaN):
1219 category = fcNaN;
1220 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001221 return opOK;
1222
1223 case convolve(fcNormal, fcInfinity):
1224 case convolve(fcZero, fcInfinity):
1225 category = fcInfinity;
1226 sign = rhs.sign ^ subtract;
1227 return opOK;
1228
1229 case convolve(fcZero, fcNormal):
1230 assign(rhs);
1231 sign = rhs.sign ^ subtract;
1232 return opOK;
1233
1234 case convolve(fcZero, fcZero):
1235 /* Sign depends on rounding mode; handled by caller. */
1236 return opOK;
1237
1238 case convolve(fcInfinity, fcInfinity):
1239 /* Differently signed infinities can only be validly
1240 subtracted. */
Hartmut Kaiser8df77a92007-10-25 23:15:31 +00001241 if((sign ^ rhs.sign) != subtract) {
Neil Boothe5e01942007-10-14 10:39:51 +00001242 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001243 return opInvalidOp;
1244 }
1245
1246 return opOK;
1247
1248 case convolve(fcNormal, fcNormal):
1249 return opDivByZero;
1250 }
1251}
1252
1253/* Add or subtract two normal numbers. */
1254lostFraction
1255APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1256{
1257 integerPart carry;
1258 lostFraction lost_fraction;
1259 int bits;
1260
1261 /* Determine if the operation on the absolute values is effectively
1262 an addition or subtraction. */
Hartmut Kaiser8df77a92007-10-25 23:15:31 +00001263 subtract ^= (sign ^ rhs.sign) ? true : false;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001264
1265 /* Are we bigger exponent-wise than the RHS? */
1266 bits = exponent - rhs.exponent;
1267
1268 /* Subtraction is more subtle than one might naively expect. */
1269 if(subtract) {
1270 APFloat temp_rhs(rhs);
1271 bool reverse;
1272
Chris Lattnerada530b2007-08-24 03:02:34 +00001273 if (bits == 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001274 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1275 lost_fraction = lfExactlyZero;
Chris Lattnerada530b2007-08-24 03:02:34 +00001276 } else if (bits > 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001277 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1278 shiftSignificandLeft(1);
1279 reverse = false;
Chris Lattnerada530b2007-08-24 03:02:34 +00001280 } else {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001281 lost_fraction = shiftSignificandRight(-bits - 1);
1282 temp_rhs.shiftSignificandLeft(1);
1283 reverse = true;
1284 }
1285
Chris Lattnerada530b2007-08-24 03:02:34 +00001286 if (reverse) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001287 carry = temp_rhs.subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001288 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001289 copySignificand(temp_rhs);
1290 sign = !sign;
1291 } else {
1292 carry = subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001293 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001294 }
1295
1296 /* Invert the lost fraction - it was on the RHS and
1297 subtracted. */
1298 if(lost_fraction == lfLessThanHalf)
1299 lost_fraction = lfMoreThanHalf;
1300 else if(lost_fraction == lfMoreThanHalf)
1301 lost_fraction = lfLessThanHalf;
1302
1303 /* The code above is intended to ensure that no borrow is
1304 necessary. */
1305 assert(!carry);
1306 } else {
1307 if(bits > 0) {
1308 APFloat temp_rhs(rhs);
1309
1310 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1311 carry = addSignificand(temp_rhs);
1312 } else {
1313 lost_fraction = shiftSignificandRight(-bits);
1314 carry = addSignificand(rhs);
1315 }
1316
1317 /* We have a guard bit; generating a carry cannot happen. */
1318 assert(!carry);
1319 }
1320
1321 return lost_fraction;
1322}
1323
1324APFloat::opStatus
1325APFloat::multiplySpecials(const APFloat &rhs)
1326{
1327 switch(convolve(category, rhs.category)) {
1328 default:
1329 assert(0);
1330
Dale Johanneseneaf08942007-08-31 04:03:46 +00001331 case convolve(fcNaN, fcZero):
1332 case convolve(fcNaN, fcNormal):
1333 case convolve(fcNaN, fcInfinity):
1334 case convolve(fcNaN, fcNaN):
1335 return opOK;
1336
1337 case convolve(fcZero, fcNaN):
1338 case convolve(fcNormal, fcNaN):
1339 case convolve(fcInfinity, fcNaN):
1340 category = fcNaN;
1341 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001342 return opOK;
1343
1344 case convolve(fcNormal, fcInfinity):
1345 case convolve(fcInfinity, fcNormal):
1346 case convolve(fcInfinity, fcInfinity):
1347 category = fcInfinity;
1348 return opOK;
1349
1350 case convolve(fcZero, fcNormal):
1351 case convolve(fcNormal, fcZero):
1352 case convolve(fcZero, fcZero):
1353 category = fcZero;
1354 return opOK;
1355
1356 case convolve(fcZero, fcInfinity):
1357 case convolve(fcInfinity, fcZero):
Neil Boothe5e01942007-10-14 10:39:51 +00001358 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001359 return opInvalidOp;
1360
1361 case convolve(fcNormal, fcNormal):
1362 return opOK;
1363 }
1364}
1365
1366APFloat::opStatus
1367APFloat::divideSpecials(const APFloat &rhs)
1368{
1369 switch(convolve(category, rhs.category)) {
1370 default:
1371 assert(0);
1372
Dale Johanneseneaf08942007-08-31 04:03:46 +00001373 case convolve(fcNaN, fcZero):
1374 case convolve(fcNaN, fcNormal):
1375 case convolve(fcNaN, fcInfinity):
1376 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001377 case convolve(fcInfinity, fcZero):
1378 case convolve(fcInfinity, fcNormal):
1379 case convolve(fcZero, fcInfinity):
1380 case convolve(fcZero, fcNormal):
1381 return opOK;
1382
Dale Johanneseneaf08942007-08-31 04:03:46 +00001383 case convolve(fcZero, fcNaN):
1384 case convolve(fcNormal, fcNaN):
1385 case convolve(fcInfinity, fcNaN):
1386 category = fcNaN;
1387 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001388 return opOK;
1389
1390 case convolve(fcNormal, fcInfinity):
1391 category = fcZero;
1392 return opOK;
1393
1394 case convolve(fcNormal, fcZero):
1395 category = fcInfinity;
1396 return opDivByZero;
1397
1398 case convolve(fcInfinity, fcInfinity):
1399 case convolve(fcZero, fcZero):
Neil Boothe5e01942007-10-14 10:39:51 +00001400 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001401 return opInvalidOp;
1402
1403 case convolve(fcNormal, fcNormal):
1404 return opOK;
1405 }
1406}
1407
Dale Johannesened6af242009-01-21 00:35:19 +00001408APFloat::opStatus
1409APFloat::modSpecials(const APFloat &rhs)
1410{
1411 switch(convolve(category, rhs.category)) {
1412 default:
1413 assert(0);
1414
1415 case convolve(fcNaN, fcZero):
1416 case convolve(fcNaN, fcNormal):
1417 case convolve(fcNaN, fcInfinity):
1418 case convolve(fcNaN, fcNaN):
1419 case convolve(fcZero, fcInfinity):
1420 case convolve(fcZero, fcNormal):
1421 case convolve(fcNormal, fcInfinity):
1422 return opOK;
1423
1424 case convolve(fcZero, fcNaN):
1425 case convolve(fcNormal, fcNaN):
1426 case convolve(fcInfinity, fcNaN):
1427 category = fcNaN;
1428 copySignificand(rhs);
1429 return opOK;
1430
1431 case convolve(fcNormal, fcZero):
1432 case convolve(fcInfinity, fcZero):
1433 case convolve(fcInfinity, fcNormal):
1434 case convolve(fcInfinity, fcInfinity):
1435 case convolve(fcZero, fcZero):
1436 makeNaN();
1437 return opInvalidOp;
1438
1439 case convolve(fcNormal, fcNormal):
1440 return opOK;
1441 }
1442}
1443
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001444/* Change sign. */
1445void
1446APFloat::changeSign()
1447{
1448 /* Look mummy, this one's easy. */
1449 sign = !sign;
1450}
1451
Dale Johannesene15c2db2007-08-31 23:35:31 +00001452void
1453APFloat::clearSign()
1454{
1455 /* So is this one. */
1456 sign = 0;
1457}
1458
1459void
1460APFloat::copySign(const APFloat &rhs)
1461{
1462 /* And this one. */
1463 sign = rhs.sign;
1464}
1465
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001466/* Normalized addition or subtraction. */
1467APFloat::opStatus
1468APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001469 bool subtract)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001470{
1471 opStatus fs;
1472
Neil Boothcaf19d72007-10-14 10:29:28 +00001473 assertArithmeticOK(*semantics);
1474
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001475 fs = addOrSubtractSpecials(rhs, subtract);
1476
1477 /* This return code means it was not a simple case. */
1478 if(fs == opDivByZero) {
1479 lostFraction lost_fraction;
1480
1481 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1482 fs = normalize(rounding_mode, lost_fraction);
1483
1484 /* Can only be zero if we lost no fraction. */
1485 assert(category != fcZero || lost_fraction == lfExactlyZero);
1486 }
1487
1488 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1489 positive zero unless rounding to minus infinity, except that
1490 adding two like-signed zeroes gives that zero. */
1491 if(category == fcZero) {
1492 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1493 sign = (rounding_mode == rmTowardNegative);
1494 }
1495
1496 return fs;
1497}
1498
1499/* Normalized addition. */
1500APFloat::opStatus
1501APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1502{
1503 return addOrSubtract(rhs, rounding_mode, false);
1504}
1505
1506/* Normalized subtraction. */
1507APFloat::opStatus
1508APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1509{
1510 return addOrSubtract(rhs, rounding_mode, true);
1511}
1512
1513/* Normalized multiply. */
1514APFloat::opStatus
1515APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1516{
1517 opStatus fs;
1518
Neil Boothcaf19d72007-10-14 10:29:28 +00001519 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001520 sign ^= rhs.sign;
1521 fs = multiplySpecials(rhs);
1522
1523 if(category == fcNormal) {
1524 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1525 fs = normalize(rounding_mode, lost_fraction);
1526 if(lost_fraction != lfExactlyZero)
1527 fs = (opStatus) (fs | opInexact);
1528 }
1529
1530 return fs;
1531}
1532
1533/* Normalized divide. */
1534APFloat::opStatus
1535APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1536{
1537 opStatus fs;
1538
Neil Boothcaf19d72007-10-14 10:29:28 +00001539 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001540 sign ^= rhs.sign;
1541 fs = divideSpecials(rhs);
1542
1543 if(category == fcNormal) {
1544 lostFraction lost_fraction = divideSignificand(rhs);
1545 fs = normalize(rounding_mode, lost_fraction);
1546 if(lost_fraction != lfExactlyZero)
1547 fs = (opStatus) (fs | opInexact);
1548 }
1549
1550 return fs;
1551}
1552
Dale Johannesen24b66a82009-01-20 18:35:05 +00001553/* Normalized remainder. This is not currently correct in all cases. */
1554APFloat::opStatus
1555APFloat::remainder(const APFloat &rhs)
1556{
1557 opStatus fs;
1558 APFloat V = *this;
1559 unsigned int origSign = sign;
1560
1561 assertArithmeticOK(*semantics);
1562 fs = V.divide(rhs, rmNearestTiesToEven);
1563 if (fs == opDivByZero)
1564 return fs;
1565
1566 int parts = partCount();
1567 integerPart *x = new integerPart[parts];
1568 bool ignored;
1569 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1570 rmNearestTiesToEven, &ignored);
1571 if (fs==opInvalidOp)
1572 return fs;
1573
1574 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1575 rmNearestTiesToEven);
1576 assert(fs==opOK); // should always work
1577
1578 fs = V.multiply(rhs, rmNearestTiesToEven);
1579 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1580
1581 fs = subtract(V, rmNearestTiesToEven);
1582 assert(fs==opOK || fs==opInexact); // likewise
1583
1584 if (isZero())
1585 sign = origSign; // IEEE754 requires this
1586 delete[] x;
1587 return fs;
1588}
1589
1590/* Normalized llvm frem (C fmod).
1591 This is not currently correct in all cases. */
Dale Johannesene15c2db2007-08-31 23:35:31 +00001592APFloat::opStatus
1593APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1594{
1595 opStatus fs;
Neil Boothcaf19d72007-10-14 10:29:28 +00001596 assertArithmeticOK(*semantics);
Dale Johannesened6af242009-01-21 00:35:19 +00001597 fs = modSpecials(rhs);
Dale Johannesene15c2db2007-08-31 23:35:31 +00001598
Dale Johannesened6af242009-01-21 00:35:19 +00001599 if (category == fcNormal && rhs.category == fcNormal) {
1600 APFloat V = *this;
1601 unsigned int origSign = sign;
Dale Johannesene15c2db2007-08-31 23:35:31 +00001602
Dale Johannesened6af242009-01-21 00:35:19 +00001603 fs = V.divide(rhs, rmNearestTiesToEven);
1604 if (fs == opDivByZero)
1605 return fs;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001606
Dale Johannesened6af242009-01-21 00:35:19 +00001607 int parts = partCount();
1608 integerPart *x = new integerPart[parts];
1609 bool ignored;
1610 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1611 rmTowardZero, &ignored);
1612 if (fs==opInvalidOp)
1613 return fs;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001614
Dale Johannesened6af242009-01-21 00:35:19 +00001615 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1616 rmNearestTiesToEven);
1617 assert(fs==opOK); // should always work
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001618
Dale Johannesened6af242009-01-21 00:35:19 +00001619 fs = V.multiply(rhs, rounding_mode);
1620 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1621
1622 fs = subtract(V, rounding_mode);
1623 assert(fs==opOK || fs==opInexact); // likewise
1624
1625 if (isZero())
1626 sign = origSign; // IEEE754 requires this
1627 delete[] x;
1628 }
Dale Johannesene15c2db2007-08-31 23:35:31 +00001629 return fs;
1630}
1631
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001632/* Normalized fused-multiply-add. */
1633APFloat::opStatus
1634APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth4f881702007-09-26 21:33:42 +00001635 const APFloat &addend,
1636 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001637{
1638 opStatus fs;
1639
Neil Boothcaf19d72007-10-14 10:29:28 +00001640 assertArithmeticOK(*semantics);
1641
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001642 /* Post-multiplication sign, before addition. */
1643 sign ^= multiplicand.sign;
1644
1645 /* If and only if all arguments are normal do we need to do an
1646 extended-precision calculation. */
1647 if(category == fcNormal
1648 && multiplicand.category == fcNormal
1649 && addend.category == fcNormal) {
1650 lostFraction lost_fraction;
1651
1652 lost_fraction = multiplySignificand(multiplicand, &addend);
1653 fs = normalize(rounding_mode, lost_fraction);
1654 if(lost_fraction != lfExactlyZero)
1655 fs = (opStatus) (fs | opInexact);
1656
1657 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1658 positive zero unless rounding to minus infinity, except that
1659 adding two like-signed zeroes gives that zero. */
1660 if(category == fcZero && sign != addend.sign)
1661 sign = (rounding_mode == rmTowardNegative);
1662 } else {
1663 fs = multiplySpecials(multiplicand);
1664
1665 /* FS can only be opOK or opInvalidOp. There is no more work
1666 to do in the latter case. The IEEE-754R standard says it is
1667 implementation-defined in this case whether, if ADDEND is a
Dale Johanneseneaf08942007-08-31 04:03:46 +00001668 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001669
1670 If we need to do the addition we can do so with normal
1671 precision. */
1672 if(fs == opOK)
1673 fs = addOrSubtract(addend, rounding_mode, false);
1674 }
1675
1676 return fs;
1677}
1678
1679/* Comparison requires normalized numbers. */
1680APFloat::cmpResult
1681APFloat::compare(const APFloat &rhs) const
1682{
1683 cmpResult result;
1684
Neil Boothcaf19d72007-10-14 10:29:28 +00001685 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001686 assert(semantics == rhs.semantics);
1687
1688 switch(convolve(category, rhs.category)) {
1689 default:
1690 assert(0);
1691
Dale Johanneseneaf08942007-08-31 04:03:46 +00001692 case convolve(fcNaN, fcZero):
1693 case convolve(fcNaN, fcNormal):
1694 case convolve(fcNaN, fcInfinity):
1695 case convolve(fcNaN, fcNaN):
1696 case convolve(fcZero, fcNaN):
1697 case convolve(fcNormal, fcNaN):
1698 case convolve(fcInfinity, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001699 return cmpUnordered;
1700
1701 case convolve(fcInfinity, fcNormal):
1702 case convolve(fcInfinity, fcZero):
1703 case convolve(fcNormal, fcZero):
1704 if(sign)
1705 return cmpLessThan;
1706 else
1707 return cmpGreaterThan;
1708
1709 case convolve(fcNormal, fcInfinity):
1710 case convolve(fcZero, fcInfinity):
1711 case convolve(fcZero, fcNormal):
1712 if(rhs.sign)
1713 return cmpGreaterThan;
1714 else
1715 return cmpLessThan;
1716
1717 case convolve(fcInfinity, fcInfinity):
1718 if(sign == rhs.sign)
1719 return cmpEqual;
1720 else if(sign)
1721 return cmpLessThan;
1722 else
1723 return cmpGreaterThan;
1724
1725 case convolve(fcZero, fcZero):
1726 return cmpEqual;
1727
1728 case convolve(fcNormal, fcNormal):
1729 break;
1730 }
1731
1732 /* Two normal numbers. Do they have the same sign? */
1733 if(sign != rhs.sign) {
1734 if(sign)
1735 result = cmpLessThan;
1736 else
1737 result = cmpGreaterThan;
1738 } else {
1739 /* Compare absolute values; invert result if negative. */
1740 result = compareAbsoluteValue(rhs);
1741
1742 if(sign) {
1743 if(result == cmpLessThan)
Neil Booth4f881702007-09-26 21:33:42 +00001744 result = cmpGreaterThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001745 else if(result == cmpGreaterThan)
Neil Booth4f881702007-09-26 21:33:42 +00001746 result = cmpLessThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001747 }
1748 }
1749
1750 return result;
1751}
1752
Dale Johannesen23a98552008-10-09 23:00:39 +00001753/// APFloat::convert - convert a value of one floating point type to another.
1754/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1755/// records whether the transformation lost information, i.e. whether
1756/// converting the result back to the original type will produce the
1757/// original value (this is almost the same as return value==fsOK, but there
1758/// are edge cases where this is not so).
1759
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001760APFloat::opStatus
1761APFloat::convert(const fltSemantics &toSemantics,
Dale Johannesen23a98552008-10-09 23:00:39 +00001762 roundingMode rounding_mode, bool *losesInfo)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001763{
Neil Boothc8db43d2007-09-22 02:56:19 +00001764 lostFraction lostFraction;
1765 unsigned int newPartCount, oldPartCount;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001766 opStatus fs;
Neil Booth4f881702007-09-26 21:33:42 +00001767
Neil Boothcaf19d72007-10-14 10:29:28 +00001768 assertArithmeticOK(*semantics);
Dale Johannesen79f82f92008-04-20 01:34:03 +00001769 assertArithmeticOK(toSemantics);
Neil Boothc8db43d2007-09-22 02:56:19 +00001770 lostFraction = lfExactlyZero;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001771 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Boothc8db43d2007-09-22 02:56:19 +00001772 oldPartCount = partCount();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001773
Neil Boothc8db43d2007-09-22 02:56:19 +00001774 /* Handle storage complications. If our new form is wider,
1775 re-allocate our bit pattern into wider storage. If it is
1776 narrower, we ignore the excess parts, but if narrowing to a
Dale Johannesen902ff942007-09-25 17:25:00 +00001777 single part we need to free the old storage.
1778 Be careful not to reference significandParts for zeroes
1779 and infinities, since it aborts. */
Neil Boothc8db43d2007-09-22 02:56:19 +00001780 if (newPartCount > oldPartCount) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001781 integerPart *newParts;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001782 newParts = new integerPart[newPartCount];
1783 APInt::tcSet(newParts, 0, newPartCount);
Dale Johannesen902ff942007-09-25 17:25:00 +00001784 if (category==fcNormal || category==fcNaN)
1785 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001786 freeSignificand();
1787 significand.parts = newParts;
Neil Boothc8db43d2007-09-22 02:56:19 +00001788 } else if (newPartCount < oldPartCount) {
1789 /* Capture any lost fraction through truncation of parts so we get
1790 correct rounding whilst normalizing. */
Dale Johannesen902ff942007-09-25 17:25:00 +00001791 if (category==fcNormal)
1792 lostFraction = lostFractionThroughTruncation
1793 (significandParts(), oldPartCount, toSemantics.precision);
1794 if (newPartCount == 1) {
1795 integerPart newPart = 0;
Neil Booth4f881702007-09-26 21:33:42 +00001796 if (category==fcNormal || category==fcNaN)
Dale Johannesen902ff942007-09-25 17:25:00 +00001797 newPart = significandParts()[0];
1798 freeSignificand();
1799 significand.part = newPart;
1800 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001801 }
1802
1803 if(category == fcNormal) {
1804 /* Re-interpret our bit-pattern. */
1805 exponent += toSemantics.precision - semantics->precision;
1806 semantics = &toSemantics;
Neil Boothc8db43d2007-09-22 02:56:19 +00001807 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen23a98552008-10-09 23:00:39 +00001808 *losesInfo = (fs != opOK);
Dale Johannesen902ff942007-09-25 17:25:00 +00001809 } else if (category == fcNaN) {
1810 int shift = toSemantics.precision - semantics->precision;
Dale Johannesenb63fa052008-01-31 18:34:01 +00001811 // Do this now so significandParts gets the right answer
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001812 const fltSemantics *oldSemantics = semantics;
Dale Johannesenb63fa052008-01-31 18:34:01 +00001813 semantics = &toSemantics;
Dale Johannesen23a98552008-10-09 23:00:39 +00001814 *losesInfo = false;
Dale Johannesen902ff942007-09-25 17:25:00 +00001815 // No normalization here, just truncate
1816 if (shift>0)
1817 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001818 else if (shift < 0) {
1819 unsigned ushift = -shift;
Dale Johannesen23a98552008-10-09 23:00:39 +00001820 // Figure out if we are losing information. This happens
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001821 // if are shifting out something other than 0s, or if the x87 long
1822 // double input did not have its integer bit set (pseudo-NaN), or if the
1823 // x87 long double input did not have its QNan bit set (because the x87
1824 // hardware sets this bit when converting a lower-precision NaN to
1825 // x87 long double).
1826 if (APInt::tcLSB(significandParts(), newPartCount) < ushift)
Dale Johannesen23a98552008-10-09 23:00:39 +00001827 *losesInfo = true;
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001828 if (oldSemantics == &APFloat::x87DoubleExtended &&
1829 (!(*significandParts() & 0x8000000000000000ULL) ||
1830 !(*significandParts() & 0x4000000000000000ULL)))
Dale Johannesen23a98552008-10-09 23:00:39 +00001831 *losesInfo = true;
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001832 APInt::tcShiftRight(significandParts(), newPartCount, ushift);
1833 }
Dale Johannesen902ff942007-09-25 17:25:00 +00001834 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
1835 // does not give you back the same bits. This is dubious, and we
1836 // don't currently do it. You're really supposed to get
1837 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen23a98552008-10-09 23:00:39 +00001838 fs = opOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001839 } else {
1840 semantics = &toSemantics;
1841 fs = opOK;
Dale Johannesen23a98552008-10-09 23:00:39 +00001842 *losesInfo = false;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001843 }
1844
1845 return fs;
1846}
1847
1848/* Convert a floating point number to an integer according to the
1849 rounding mode. If the rounded integer value is out of range this
Neil Boothee7ae382007-11-01 22:43:37 +00001850 returns an invalid operation exception and the contents of the
1851 destination parts are unspecified. If the rounded value is in
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001852 range but the floating point number is not the exact integer, the C
1853 standard doesn't require an inexact exception to be raised. IEEE
1854 854 does require it so we do that.
1855
1856 Note that for conversions to integer type the C standard requires
1857 round-to-zero to always be used. */
1858APFloat::opStatus
Neil Boothee7ae382007-11-01 22:43:37 +00001859APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
1860 bool isSigned,
Dale Johannesen23a98552008-10-09 23:00:39 +00001861 roundingMode rounding_mode,
1862 bool *isExact) const
Neil Boothee7ae382007-11-01 22:43:37 +00001863{
1864 lostFraction lost_fraction;
1865 const integerPart *src;
1866 unsigned int dstPartsCount, truncatedBits;
1867
Evan Cheng794a7db2008-11-26 01:11:57 +00001868 assertArithmeticOK(*semantics);
Neil Boothe3d936a2007-11-02 15:10:05 +00001869
Dale Johannesen23a98552008-10-09 23:00:39 +00001870 *isExact = false;
1871
Neil Boothee7ae382007-11-01 22:43:37 +00001872 /* Handle the three special cases first. */
1873 if(category == fcInfinity || category == fcNaN)
1874 return opInvalidOp;
1875
1876 dstPartsCount = partCountForBits(width);
1877
1878 if(category == fcZero) {
1879 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesene4a42452008-10-07 00:40:01 +00001880 // Negative zero can't be represented as an int.
Dale Johannesen23a98552008-10-09 23:00:39 +00001881 *isExact = !sign;
1882 return opOK;
Neil Boothee7ae382007-11-01 22:43:37 +00001883 }
1884
1885 src = significandParts();
1886
1887 /* Step 1: place our absolute value, with any fraction truncated, in
1888 the destination. */
1889 if (exponent < 0) {
1890 /* Our absolute value is less than one; truncate everything. */
1891 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen1f54f582009-01-19 21:17:05 +00001892 /* For exponent -1 the integer bit represents .5, look at that.
1893 For smaller exponents leftmost truncated bit is 0. */
1894 truncatedBits = semantics->precision -1U - exponent;
Neil Boothee7ae382007-11-01 22:43:37 +00001895 } else {
1896 /* We want the most significant (exponent + 1) bits; the rest are
1897 truncated. */
1898 unsigned int bits = exponent + 1U;
1899
1900 /* Hopelessly large in magnitude? */
1901 if (bits > width)
1902 return opInvalidOp;
1903
1904 if (bits < semantics->precision) {
1905 /* We truncate (semantics->precision - bits) bits. */
1906 truncatedBits = semantics->precision - bits;
1907 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
1908 } else {
1909 /* We want at least as many bits as are available. */
1910 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
1911 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
1912 truncatedBits = 0;
1913 }
1914 }
1915
1916 /* Step 2: work out any lost fraction, and increment the absolute
1917 value if we would round away from zero. */
1918 if (truncatedBits) {
1919 lost_fraction = lostFractionThroughTruncation(src, partCount(),
1920 truncatedBits);
1921 if (lost_fraction != lfExactlyZero
1922 && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
1923 if (APInt::tcIncrement(parts, dstPartsCount))
1924 return opInvalidOp; /* Overflow. */
1925 }
1926 } else {
1927 lost_fraction = lfExactlyZero;
1928 }
1929
1930 /* Step 3: check if we fit in the destination. */
1931 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
1932
1933 if (sign) {
1934 if (!isSigned) {
1935 /* Negative numbers cannot be represented as unsigned. */
1936 if (omsb != 0)
1937 return opInvalidOp;
1938 } else {
1939 /* It takes omsb bits to represent the unsigned integer value.
1940 We lose a bit for the sign, but care is needed as the
1941 maximally negative integer is a special case. */
1942 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
1943 return opInvalidOp;
1944
1945 /* This case can happen because of rounding. */
1946 if (omsb > width)
1947 return opInvalidOp;
1948 }
1949
1950 APInt::tcNegate (parts, dstPartsCount);
1951 } else {
1952 if (omsb >= width + !isSigned)
1953 return opInvalidOp;
1954 }
1955
Dale Johannesen23a98552008-10-09 23:00:39 +00001956 if (lost_fraction == lfExactlyZero) {
1957 *isExact = true;
Neil Boothee7ae382007-11-01 22:43:37 +00001958 return opOK;
Dale Johannesen23a98552008-10-09 23:00:39 +00001959 } else
Neil Boothee7ae382007-11-01 22:43:37 +00001960 return opInexact;
1961}
1962
1963/* Same as convertToSignExtendedInteger, except we provide
1964 deterministic values in case of an invalid operation exception,
1965 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen23a98552008-10-09 23:00:39 +00001966 for underflow or overflow.
1967 The *isExact output tells whether the result is exact, in the sense
1968 that converting it back to the original floating point type produces
1969 the original value. This is almost equivalent to result==opOK,
1970 except for negative zeroes.
1971*/
Neil Boothee7ae382007-11-01 22:43:37 +00001972APFloat::opStatus
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001973APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth4f881702007-09-26 21:33:42 +00001974 bool isSigned,
Dale Johannesen23a98552008-10-09 23:00:39 +00001975 roundingMode rounding_mode, bool *isExact) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001976{
Neil Boothee7ae382007-11-01 22:43:37 +00001977 opStatus fs;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001978
Dale Johannesen23a98552008-10-09 23:00:39 +00001979 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
1980 isExact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001981
Neil Boothee7ae382007-11-01 22:43:37 +00001982 if (fs == opInvalidOp) {
1983 unsigned int bits, dstPartsCount;
1984
1985 dstPartsCount = partCountForBits(width);
1986
1987 if (category == fcNaN)
1988 bits = 0;
1989 else if (sign)
1990 bits = isSigned;
1991 else
1992 bits = width - isSigned;
1993
1994 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
1995 if (sign && isSigned)
1996 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001997 }
1998
Neil Boothee7ae382007-11-01 22:43:37 +00001999 return fs;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002000}
2001
Neil Booth643ce592007-10-07 12:07:53 +00002002/* Convert an unsigned integer SRC to a floating point number,
2003 rounding according to ROUNDING_MODE. The sign of the floating
2004 point number is not modified. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002005APFloat::opStatus
Neil Booth643ce592007-10-07 12:07:53 +00002006APFloat::convertFromUnsignedParts(const integerPart *src,
2007 unsigned int srcCount,
2008 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002009{
Neil Booth5477f852007-10-08 14:39:42 +00002010 unsigned int omsb, precision, dstCount;
Neil Booth643ce592007-10-07 12:07:53 +00002011 integerPart *dst;
Neil Booth5477f852007-10-08 14:39:42 +00002012 lostFraction lost_fraction;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002013
Neil Boothcaf19d72007-10-14 10:29:28 +00002014 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002015 category = fcNormal;
Neil Booth5477f852007-10-08 14:39:42 +00002016 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth643ce592007-10-07 12:07:53 +00002017 dst = significandParts();
2018 dstCount = partCount();
Neil Booth5477f852007-10-08 14:39:42 +00002019 precision = semantics->precision;
Neil Booth643ce592007-10-07 12:07:53 +00002020
Neil Booth5477f852007-10-08 14:39:42 +00002021 /* We want the most significant PRECISON bits of SRC. There may not
2022 be that many; extract what we can. */
2023 if (precision <= omsb) {
2024 exponent = omsb - 1;
Neil Booth643ce592007-10-07 12:07:53 +00002025 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth5477f852007-10-08 14:39:42 +00002026 omsb - precision);
2027 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2028 } else {
2029 exponent = precision - 1;
2030 lost_fraction = lfExactlyZero;
2031 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth643ce592007-10-07 12:07:53 +00002032 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002033
2034 return normalize(rounding_mode, lost_fraction);
2035}
2036
Dan Gohman93c276e2008-02-29 01:26:11 +00002037APFloat::opStatus
2038APFloat::convertFromAPInt(const APInt &Val,
2039 bool isSigned,
2040 roundingMode rounding_mode)
2041{
2042 unsigned int partCount = Val.getNumWords();
2043 APInt api = Val;
2044
2045 sign = false;
2046 if (isSigned && api.isNegative()) {
2047 sign = true;
2048 api = -api;
2049 }
2050
2051 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2052}
2053
Neil Boothf16c5952007-10-07 12:15:41 +00002054/* Convert a two's complement integer SRC to a floating point number,
2055 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2056 integer is signed, in which case it must be sign-extended. */
2057APFloat::opStatus
2058APFloat::convertFromSignExtendedInteger(const integerPart *src,
2059 unsigned int srcCount,
2060 bool isSigned,
2061 roundingMode rounding_mode)
2062{
2063 opStatus status;
2064
Neil Boothcaf19d72007-10-14 10:29:28 +00002065 assertArithmeticOK(*semantics);
Neil Boothf16c5952007-10-07 12:15:41 +00002066 if (isSigned
2067 && APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2068 integerPart *copy;
2069
2070 /* If we're signed and negative negate a copy. */
2071 sign = true;
2072 copy = new integerPart[srcCount];
2073 APInt::tcAssign(copy, src, srcCount);
2074 APInt::tcNegate(copy, srcCount);
2075 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2076 delete [] copy;
2077 } else {
2078 sign = false;
2079 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2080 }
2081
2082 return status;
2083}
2084
Neil Boothccf596a2007-10-07 11:45:55 +00002085/* FIXME: should this just take a const APInt reference? */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002086APFloat::opStatus
Neil Boothccf596a2007-10-07 11:45:55 +00002087APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2088 unsigned int width, bool isSigned,
2089 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002090{
Dale Johannesen910993e2007-09-21 22:09:37 +00002091 unsigned int partCount = partCountForBits(width);
Dale Johannesen910993e2007-09-21 22:09:37 +00002092 APInt api = APInt(width, partCount, parts);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002093
2094 sign = false;
Dale Johannesencce23a42007-09-30 18:17:01 +00002095 if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
2096 sign = true;
2097 api = -api;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002098 }
2099
Neil Booth7a7bc0f2007-10-07 12:10:57 +00002100 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002101}
2102
2103APFloat::opStatus
2104APFloat::convertFromHexadecimalString(const char *p,
Neil Booth4f881702007-09-26 21:33:42 +00002105 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002106{
2107 lostFraction lost_fraction;
2108 integerPart *significand;
2109 unsigned int bitPos, partsCount;
2110 const char *dot, *firstSignificantDigit;
2111
2112 zeroSignificand();
2113 exponent = 0;
2114 category = fcNormal;
2115
2116 significand = significandParts();
2117 partsCount = partCount();
2118 bitPos = partsCount * integerPartWidth;
2119
Neil Booth33d4c922007-10-07 08:51:21 +00002120 /* Skip leading zeroes and any (hexa)decimal point. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002121 p = skipLeadingZeroesAndAnyDot(p, &dot);
2122 firstSignificantDigit = p;
2123
2124 for(;;) {
Dale Johannesen386f3e92008-05-14 22:53:25 +00002125 integerPart hex_value;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002126
2127 if(*p == '.') {
2128 assert(dot == 0);
2129 dot = p++;
2130 }
2131
2132 hex_value = hexDigitValue(*p);
2133 if(hex_value == -1U) {
2134 lost_fraction = lfExactlyZero;
2135 break;
2136 }
2137
2138 p++;
2139
2140 /* Store the number whilst 4-bit nibbles remain. */
2141 if(bitPos) {
2142 bitPos -= 4;
2143 hex_value <<= bitPos % integerPartWidth;
2144 significand[bitPos / integerPartWidth] |= hex_value;
2145 } else {
2146 lost_fraction = trailingHexadecimalFraction(p, hex_value);
2147 while(hexDigitValue(*p) != -1U)
Neil Booth4f881702007-09-26 21:33:42 +00002148 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002149 break;
2150 }
2151 }
2152
2153 /* Hex floats require an exponent but not a hexadecimal point. */
2154 assert(*p == 'p' || *p == 'P');
2155
2156 /* Ignore the exponent if we are zero. */
2157 if(p != firstSignificantDigit) {
2158 int expAdjustment;
2159
2160 /* Implicit hexadecimal point? */
2161 if(!dot)
2162 dot = p;
2163
2164 /* Calculate the exponent adjustment implicit in the number of
2165 significant digits. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002166 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002167 if(expAdjustment < 0)
2168 expAdjustment++;
2169 expAdjustment = expAdjustment * 4 - 1;
2170
2171 /* Adjust for writing the significand starting at the most
2172 significant nibble. */
2173 expAdjustment += semantics->precision;
2174 expAdjustment -= partsCount * integerPartWidth;
2175
2176 /* Adjust for the given exponent. */
2177 exponent = totalExponent(p, expAdjustment);
2178 }
2179
2180 return normalize(rounding_mode, lost_fraction);
2181}
2182
2183APFloat::opStatus
Neil Booth96c74712007-10-12 16:02:31 +00002184APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2185 unsigned sigPartCount, int exp,
2186 roundingMode rounding_mode)
2187{
2188 unsigned int parts, pow5PartCount;
Neil Boothcaf19d72007-10-14 10:29:28 +00002189 fltSemantics calcSemantics = { 32767, -32767, 0, true };
Neil Booth96c74712007-10-12 16:02:31 +00002190 integerPart pow5Parts[maxPowerOfFiveParts];
2191 bool isNearest;
2192
2193 isNearest = (rounding_mode == rmNearestTiesToEven
2194 || rounding_mode == rmNearestTiesToAway);
2195
2196 parts = partCountForBits(semantics->precision + 11);
2197
2198 /* Calculate pow(5, abs(exp)). */
2199 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2200
2201 for (;; parts *= 2) {
2202 opStatus sigStatus, powStatus;
2203 unsigned int excessPrecision, truncatedBits;
2204
2205 calcSemantics.precision = parts * integerPartWidth - 1;
2206 excessPrecision = calcSemantics.precision - semantics->precision;
2207 truncatedBits = excessPrecision;
2208
2209 APFloat decSig(calcSemantics, fcZero, sign);
2210 APFloat pow5(calcSemantics, fcZero, false);
2211
2212 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2213 rmNearestTiesToEven);
2214 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2215 rmNearestTiesToEven);
2216 /* Add exp, as 10^n = 5^n * 2^n. */
2217 decSig.exponent += exp;
2218
2219 lostFraction calcLostFraction;
Evan Cheng48e8c802008-05-02 21:15:08 +00002220 integerPart HUerr, HUdistance;
2221 unsigned int powHUerr;
Neil Booth96c74712007-10-12 16:02:31 +00002222
2223 if (exp >= 0) {
2224 /* multiplySignificand leaves the precision-th bit set to 1. */
2225 calcLostFraction = decSig.multiplySignificand(pow5, NULL);
2226 powHUerr = powStatus != opOK;
2227 } else {
2228 calcLostFraction = decSig.divideSignificand(pow5);
2229 /* Denormal numbers have less precision. */
2230 if (decSig.exponent < semantics->minExponent) {
2231 excessPrecision += (semantics->minExponent - decSig.exponent);
2232 truncatedBits = excessPrecision;
2233 if (excessPrecision > calcSemantics.precision)
2234 excessPrecision = calcSemantics.precision;
2235 }
2236 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002237 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Booth96c74712007-10-12 16:02:31 +00002238 }
2239
2240 /* Both multiplySignificand and divideSignificand return the
2241 result with the integer bit set. */
2242 assert (APInt::tcExtractBit
2243 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2244
2245 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2246 powHUerr);
2247 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2248 excessPrecision, isNearest);
2249
2250 /* Are we guaranteed to round correctly if we truncate? */
2251 if (HUdistance >= HUerr) {
2252 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2253 calcSemantics.precision - excessPrecision,
2254 excessPrecision);
2255 /* Take the exponent of decSig. If we tcExtract-ed less bits
2256 above we must adjust our exponent to compensate for the
2257 implicit right shift. */
2258 exponent = (decSig.exponent + semantics->precision
2259 - (calcSemantics.precision - excessPrecision));
2260 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2261 decSig.partCount(),
2262 truncatedBits);
2263 return normalize(rounding_mode, calcLostFraction);
2264 }
2265 }
2266}
2267
2268APFloat::opStatus
2269APFloat::convertFromDecimalString(const char *p, roundingMode rounding_mode)
2270{
Neil Booth1870f292007-10-14 10:16:12 +00002271 decimalInfo D;
Neil Booth96c74712007-10-12 16:02:31 +00002272 opStatus fs;
2273
Neil Booth1870f292007-10-14 10:16:12 +00002274 /* Scan the text. */
2275 interpretDecimal(p, &D);
Neil Booth96c74712007-10-12 16:02:31 +00002276
Neil Booth686700e2007-10-15 15:00:55 +00002277 /* Handle the quick cases. First the case of no significant digits,
2278 i.e. zero, and then exponents that are obviously too large or too
2279 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2280 definitely overflows if
2281
2282 (exp - 1) * L >= maxExponent
2283
2284 and definitely underflows to zero where
2285
2286 (exp + 1) * L <= minExponent - precision
2287
2288 With integer arithmetic the tightest bounds for L are
2289
2290 93/28 < L < 196/59 [ numerator <= 256 ]
2291 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2292 */
2293
Neil Boothcc233592007-12-05 13:06:04 +00002294 if (decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Booth96c74712007-10-12 16:02:31 +00002295 category = fcZero;
2296 fs = opOK;
Neil Booth686700e2007-10-15 15:00:55 +00002297 } else if ((D.normalizedExponent + 1) * 28738
2298 <= 8651 * (semantics->minExponent - (int) semantics->precision)) {
2299 /* Underflow to zero and round. */
2300 zeroSignificand();
2301 fs = normalize(rounding_mode, lfLessThanHalf);
2302 } else if ((D.normalizedExponent - 1) * 42039
2303 >= 12655 * semantics->maxExponent) {
2304 /* Overflow and round. */
2305 fs = handleOverflow(rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002306 } else {
Neil Booth1870f292007-10-14 10:16:12 +00002307 integerPart *decSignificand;
2308 unsigned int partCount;
Neil Booth96c74712007-10-12 16:02:31 +00002309
Neil Booth1870f292007-10-14 10:16:12 +00002310 /* A tight upper bound on number of bits required to hold an
Neil Booth686700e2007-10-15 15:00:55 +00002311 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth1870f292007-10-14 10:16:12 +00002312 to hold the full significand, and an extra part required by
2313 tcMultiplyPart. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002314 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth686700e2007-10-15 15:00:55 +00002315 partCount = partCountForBits(1 + 196 * partCount / 59);
Neil Booth1870f292007-10-14 10:16:12 +00002316 decSignificand = new integerPart[partCount + 1];
2317 partCount = 0;
Neil Booth96c74712007-10-12 16:02:31 +00002318
Neil Booth1870f292007-10-14 10:16:12 +00002319 /* Convert to binary efficiently - we do almost all multiplication
2320 in an integerPart. When this would overflow do we do a single
2321 bignum multiplication, and then revert again to multiplication
2322 in an integerPart. */
2323 do {
2324 integerPart decValue, val, multiplier;
2325
2326 val = 0;
2327 multiplier = 1;
2328
2329 do {
2330 if (*p == '.')
2331 p++;
2332
2333 decValue = decDigitValue(*p++);
2334 multiplier *= 10;
2335 val = val * 10 + decValue;
2336 /* The maximum number that can be multiplied by ten with any
2337 digit added without overflowing an integerPart. */
2338 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2339
2340 /* Multiply out the current part. */
2341 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2342 partCount, partCount + 1, false);
2343
2344 /* If we used another part (likely but not guaranteed), increase
2345 the count. */
2346 if (decSignificand[partCount])
2347 partCount++;
2348 } while (p <= D.lastSigDigit);
Neil Booth96c74712007-10-12 16:02:31 +00002349
Neil Booth43a4b282007-11-01 22:51:07 +00002350 category = fcNormal;
Neil Booth96c74712007-10-12 16:02:31 +00002351 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth1870f292007-10-14 10:16:12 +00002352 D.exponent, rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002353
Neil Booth1870f292007-10-14 10:16:12 +00002354 delete [] decSignificand;
2355 }
Neil Booth96c74712007-10-12 16:02:31 +00002356
2357 return fs;
2358}
2359
2360APFloat::opStatus
Neil Booth4f881702007-09-26 21:33:42 +00002361APFloat::convertFromString(const char *p, roundingMode rounding_mode)
2362{
Neil Boothcaf19d72007-10-14 10:29:28 +00002363 assertArithmeticOK(*semantics);
2364
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002365 /* Handle a leading minus sign. */
2366 if(*p == '-')
2367 sign = 1, p++;
2368 else
2369 sign = 0;
2370
2371 if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
2372 return convertFromHexadecimalString(p + 2, rounding_mode);
Bill Wendlingb7c0d942008-11-27 08:00:12 +00002373
2374 return convertFromDecimalString(p, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002375}
Dale Johannesen343e7702007-08-24 00:56:33 +00002376
Neil Bootha30b0ee2007-10-03 22:26:02 +00002377/* Write out a hexadecimal representation of the floating point value
2378 to DST, which must be of sufficient size, in the C99 form
2379 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2380 excluding the terminating NUL.
2381
2382 If UPPERCASE, the output is in upper case, otherwise in lower case.
2383
2384 HEXDIGITS digits appear altogether, rounding the value if
2385 necessary. If HEXDIGITS is 0, the minimal precision to display the
2386 number precisely is used instead. If nothing would appear after
2387 the decimal point it is suppressed.
2388
2389 The decimal exponent is always printed and has at least one digit.
2390 Zero values display an exponent of zero. Infinities and NaNs
2391 appear as "infinity" or "nan" respectively.
2392
2393 The above rules are as specified by C99. There is ambiguity about
2394 what the leading hexadecimal digit should be. This implementation
2395 uses whatever is necessary so that the exponent is displayed as
2396 stored. This implies the exponent will fall within the IEEE format
2397 range, and the leading hexadecimal digit will be 0 (for denormals),
2398 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2399 any other digits zero).
2400*/
2401unsigned int
2402APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2403 bool upperCase, roundingMode rounding_mode) const
2404{
2405 char *p;
2406
Neil Boothcaf19d72007-10-14 10:29:28 +00002407 assertArithmeticOK(*semantics);
2408
Neil Bootha30b0ee2007-10-03 22:26:02 +00002409 p = dst;
2410 if (sign)
2411 *dst++ = '-';
2412
2413 switch (category) {
2414 case fcInfinity:
2415 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2416 dst += sizeof infinityL - 1;
2417 break;
2418
2419 case fcNaN:
2420 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2421 dst += sizeof NaNU - 1;
2422 break;
2423
2424 case fcZero:
2425 *dst++ = '0';
2426 *dst++ = upperCase ? 'X': 'x';
2427 *dst++ = '0';
2428 if (hexDigits > 1) {
2429 *dst++ = '.';
2430 memset (dst, '0', hexDigits - 1);
2431 dst += hexDigits - 1;
2432 }
2433 *dst++ = upperCase ? 'P': 'p';
2434 *dst++ = '0';
2435 break;
2436
2437 case fcNormal:
2438 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2439 break;
2440 }
2441
2442 *dst = 0;
2443
Evan Cheng48e8c802008-05-02 21:15:08 +00002444 return static_cast<unsigned int>(dst - p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002445}
2446
2447/* Does the hard work of outputting the correctly rounded hexadecimal
2448 form of a normal floating point number with the specified number of
2449 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2450 digits necessary to print the value precisely is output. */
2451char *
2452APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2453 bool upperCase,
2454 roundingMode rounding_mode) const
2455{
2456 unsigned int count, valueBits, shift, partsCount, outputDigits;
2457 const char *hexDigitChars;
2458 const integerPart *significand;
2459 char *p;
2460 bool roundUp;
2461
2462 *dst++ = '0';
2463 *dst++ = upperCase ? 'X': 'x';
2464
2465 roundUp = false;
2466 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2467
2468 significand = significandParts();
2469 partsCount = partCount();
2470
2471 /* +3 because the first digit only uses the single integer bit, so
2472 we have 3 virtual zero most-significant-bits. */
2473 valueBits = semantics->precision + 3;
2474 shift = integerPartWidth - valueBits % integerPartWidth;
2475
2476 /* The natural number of digits required ignoring trailing
2477 insignificant zeroes. */
2478 outputDigits = (valueBits - significandLSB () + 3) / 4;
2479
2480 /* hexDigits of zero means use the required number for the
2481 precision. Otherwise, see if we are truncating. If we are,
Neil Booth978661d2007-10-06 00:24:48 +00002482 find out if we need to round away from zero. */
Neil Bootha30b0ee2007-10-03 22:26:02 +00002483 if (hexDigits) {
2484 if (hexDigits < outputDigits) {
2485 /* We are dropping non-zero bits, so need to check how to round.
2486 "bits" is the number of dropped bits. */
2487 unsigned int bits;
2488 lostFraction fraction;
2489
2490 bits = valueBits - hexDigits * 4;
2491 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2492 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2493 }
2494 outputDigits = hexDigits;
2495 }
2496
2497 /* Write the digits consecutively, and start writing in the location
2498 of the hexadecimal point. We move the most significant digit
2499 left and add the hexadecimal point later. */
2500 p = ++dst;
2501
2502 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2503
2504 while (outputDigits && count) {
2505 integerPart part;
2506
2507 /* Put the most significant integerPartWidth bits in "part". */
2508 if (--count == partsCount)
2509 part = 0; /* An imaginary higher zero part. */
2510 else
2511 part = significand[count] << shift;
2512
2513 if (count && shift)
2514 part |= significand[count - 1] >> (integerPartWidth - shift);
2515
2516 /* Convert as much of "part" to hexdigits as we can. */
2517 unsigned int curDigits = integerPartWidth / 4;
2518
2519 if (curDigits > outputDigits)
2520 curDigits = outputDigits;
2521 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2522 outputDigits -= curDigits;
2523 }
2524
2525 if (roundUp) {
2526 char *q = dst;
2527
2528 /* Note that hexDigitChars has a trailing '0'. */
2529 do {
2530 q--;
2531 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth978661d2007-10-06 00:24:48 +00002532 } while (*q == '0');
2533 assert (q >= p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002534 } else {
2535 /* Add trailing zeroes. */
2536 memset (dst, '0', outputDigits);
2537 dst += outputDigits;
2538 }
2539
2540 /* Move the most significant digit to before the point, and if there
2541 is something after the decimal point add it. This must come
2542 after rounding above. */
2543 p[-1] = p[0];
2544 if (dst -1 == p)
2545 dst--;
2546 else
2547 p[0] = '.';
2548
2549 /* Finally output the exponent. */
2550 *dst++ = upperCase ? 'P': 'p';
2551
Neil Booth92f7e8d2007-10-06 07:29:25 +00002552 return writeSignedDecimal (dst, exponent);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002553}
2554
Dale Johannesen343e7702007-08-24 00:56:33 +00002555// For good performance it is desirable for different APFloats
2556// to produce different integers.
2557uint32_t
Neil Booth4f881702007-09-26 21:33:42 +00002558APFloat::getHashValue() const
2559{
Dale Johannesen343e7702007-08-24 00:56:33 +00002560 if (category==fcZero) return sign<<8 | semantics->precision ;
2561 else if (category==fcInfinity) return sign<<9 | semantics->precision;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002562 else if (category==fcNaN) return 1<<10 | semantics->precision;
Dale Johannesen343e7702007-08-24 00:56:33 +00002563 else {
2564 uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
2565 const integerPart* p = significandParts();
2566 for (int i=partCount(); i>0; i--, p++)
Evan Cheng48e8c802008-05-02 21:15:08 +00002567 hash ^= ((uint32_t)*p) ^ (uint32_t)((*p)>>32);
Dale Johannesen343e7702007-08-24 00:56:33 +00002568 return hash;
2569 }
2570}
2571
2572// Conversion from APFloat to/from host float/double. It may eventually be
2573// possible to eliminate these and have everybody deal with APFloats, but that
2574// will take a while. This approach will not easily extend to long double.
Dale Johannesena72a5a02007-09-20 23:47:58 +00002575// Current implementation requires integerPartWidth==64, which is correct at
2576// the moment but could be made more general.
Dale Johannesen343e7702007-08-24 00:56:33 +00002577
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002578// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesena72a5a02007-09-20 23:47:58 +00002579// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002580
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002581APInt
Neil Booth4f881702007-09-26 21:33:42 +00002582APFloat::convertF80LongDoubleAPFloatToAPInt() const
2583{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002584 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002585 assert (partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002586
2587 uint64_t myexponent, mysignificand;
2588
2589 if (category==fcNormal) {
2590 myexponent = exponent+16383; //bias
Dale Johannesena72a5a02007-09-20 23:47:58 +00002591 mysignificand = significandParts()[0];
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002592 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2593 myexponent = 0; // denormal
2594 } else if (category==fcZero) {
2595 myexponent = 0;
2596 mysignificand = 0;
2597 } else if (category==fcInfinity) {
2598 myexponent = 0x7fff;
2599 mysignificand = 0x8000000000000000ULL;
Chris Lattnera11ef822007-10-06 06:13:42 +00002600 } else {
2601 assert(category == fcNaN && "Unknown category");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002602 myexponent = 0x7fff;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002603 mysignificand = significandParts()[0];
Chris Lattnera11ef822007-10-06 06:13:42 +00002604 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002605
2606 uint64_t words[2];
Evan Cheng48e8c802008-05-02 21:15:08 +00002607 words[0] = ((uint64_t)(sign & 1) << 63) |
2608 ((myexponent & 0x7fffLL) << 48) |
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002609 ((mysignificand >>16) & 0xffffffffffffLL);
2610 words[1] = mysignificand & 0xffff;
Chris Lattnera11ef822007-10-06 06:13:42 +00002611 return APInt(80, 2, words);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002612}
2613
2614APInt
Dale Johannesena471c2e2007-10-11 18:07:22 +00002615APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2616{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002617 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002618 assert (partCount()==2);
2619
2620 uint64_t myexponent, mysignificand, myexponent2, mysignificand2;
2621
2622 if (category==fcNormal) {
2623 myexponent = exponent + 1023; //bias
2624 myexponent2 = exponent2 + 1023;
2625 mysignificand = significandParts()[0];
2626 mysignificand2 = significandParts()[1];
2627 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2628 myexponent = 0; // denormal
2629 if (myexponent2==1 && !(mysignificand2 & 0x10000000000000LL))
2630 myexponent2 = 0; // denormal
2631 } else if (category==fcZero) {
2632 myexponent = 0;
2633 mysignificand = 0;
2634 myexponent2 = 0;
2635 mysignificand2 = 0;
2636 } else if (category==fcInfinity) {
2637 myexponent = 0x7ff;
2638 myexponent2 = 0;
2639 mysignificand = 0;
2640 mysignificand2 = 0;
2641 } else {
2642 assert(category == fcNaN && "Unknown category");
2643 myexponent = 0x7ff;
2644 mysignificand = significandParts()[0];
2645 myexponent2 = exponent2;
2646 mysignificand2 = significandParts()[1];
2647 }
2648
2649 uint64_t words[2];
Evan Cheng48e8c802008-05-02 21:15:08 +00002650 words[0] = ((uint64_t)(sign & 1) << 63) |
Dale Johannesena471c2e2007-10-11 18:07:22 +00002651 ((myexponent & 0x7ff) << 52) |
2652 (mysignificand & 0xfffffffffffffLL);
Evan Cheng48e8c802008-05-02 21:15:08 +00002653 words[1] = ((uint64_t)(sign2 & 1) << 63) |
Dale Johannesena471c2e2007-10-11 18:07:22 +00002654 ((myexponent2 & 0x7ff) << 52) |
2655 (mysignificand2 & 0xfffffffffffffLL);
2656 return APInt(128, 2, words);
2657}
2658
2659APInt
Neil Booth4f881702007-09-26 21:33:42 +00002660APFloat::convertDoubleAPFloatToAPInt() const
2661{
Dan Gohmancb648f92007-09-14 20:08:19 +00002662 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002663 assert (partCount()==1);
2664
Dale Johanneseneaf08942007-08-31 04:03:46 +00002665 uint64_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002666
2667 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002668 myexponent = exponent+1023; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002669 mysignificand = *significandParts();
2670 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2671 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002672 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002673 myexponent = 0;
2674 mysignificand = 0;
2675 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002676 myexponent = 0x7ff;
2677 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002678 } else {
2679 assert(category == fcNaN && "Unknown category!");
Dale Johannesen343e7702007-08-24 00:56:33 +00002680 myexponent = 0x7ff;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002681 mysignificand = *significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002682 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002683
Evan Cheng48e8c802008-05-02 21:15:08 +00002684 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattnera11ef822007-10-06 06:13:42 +00002685 ((myexponent & 0x7ff) << 52) |
2686 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesen343e7702007-08-24 00:56:33 +00002687}
2688
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002689APInt
Neil Booth4f881702007-09-26 21:33:42 +00002690APFloat::convertFloatAPFloatToAPInt() const
2691{
Dan Gohmancb648f92007-09-14 20:08:19 +00002692 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002693 assert (partCount()==1);
Neil Booth4f881702007-09-26 21:33:42 +00002694
Dale Johanneseneaf08942007-08-31 04:03:46 +00002695 uint32_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002696
2697 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002698 myexponent = exponent+127; //bias
Evan Cheng48e8c802008-05-02 21:15:08 +00002699 mysignificand = (uint32_t)*significandParts();
Dale Johannesend0763b92007-11-17 01:02:27 +00002700 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002701 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002702 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002703 myexponent = 0;
2704 mysignificand = 0;
2705 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002706 myexponent = 0xff;
2707 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002708 } else {
2709 assert(category == fcNaN && "Unknown category!");
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002710 myexponent = 0xff;
Evan Cheng48e8c802008-05-02 21:15:08 +00002711 mysignificand = (uint32_t)*significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002712 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002713
Chris Lattnera11ef822007-10-06 06:13:42 +00002714 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2715 (mysignificand & 0x7fffff)));
Dale Johannesen343e7702007-08-24 00:56:33 +00002716}
2717
Dale Johannesena471c2e2007-10-11 18:07:22 +00002718// This function creates an APInt that is just a bit map of the floating
2719// point constant as it would appear in memory. It is not a conversion,
2720// and treating the result as a normal integer is unlikely to be useful.
2721
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002722APInt
Dale Johannesen7111b022008-10-09 18:53:47 +00002723APFloat::bitcastToAPInt() const
Neil Booth4f881702007-09-26 21:33:42 +00002724{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002725 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002726 return convertFloatAPFloatToAPInt();
Chris Lattnera11ef822007-10-06 06:13:42 +00002727
Dan Gohmanb10abe12008-01-29 12:08:20 +00002728 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002729 return convertDoubleAPFloatToAPInt();
Neil Booth4f881702007-09-26 21:33:42 +00002730
Dan Gohmanb10abe12008-01-29 12:08:20 +00002731 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
Dale Johannesena471c2e2007-10-11 18:07:22 +00002732 return convertPPCDoubleDoubleAPFloatToAPInt();
2733
Dan Gohmanb10abe12008-01-29 12:08:20 +00002734 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
Chris Lattnera11ef822007-10-06 06:13:42 +00002735 "unknown format!");
2736 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002737}
2738
Neil Booth4f881702007-09-26 21:33:42 +00002739float
2740APFloat::convertToFloat() const
2741{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002742 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Dale Johannesen7111b022008-10-09 18:53:47 +00002743 APInt api = bitcastToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002744 return api.bitsToFloat();
2745}
2746
Neil Booth4f881702007-09-26 21:33:42 +00002747double
2748APFloat::convertToDouble() const
2749{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002750 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Dale Johannesen7111b022008-10-09 18:53:47 +00002751 APInt api = bitcastToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002752 return api.bitsToDouble();
2753}
2754
Dale Johannesend3d8ce32008-10-06 18:22:29 +00002755/// Integer bit is explicit in this format. Intel hardware (387 and later)
2756/// does not support these bit patterns:
2757/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
2758/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
2759/// exponent = 0, integer bit 1 ("pseudodenormal")
2760/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
2761/// At the moment, the first two are treated as NaNs, the second two as Normal.
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002762void
Neil Booth4f881702007-09-26 21:33:42 +00002763APFloat::initFromF80LongDoubleAPInt(const APInt &api)
2764{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002765 assert(api.getBitWidth()==80);
2766 uint64_t i1 = api.getRawData()[0];
2767 uint64_t i2 = api.getRawData()[1];
2768 uint64_t myexponent = (i1 >> 48) & 0x7fff;
2769 uint64_t mysignificand = ((i1 << 16) & 0xffffffffffff0000ULL) |
2770 (i2 & 0xffff);
2771
2772 initialize(&APFloat::x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002773 assert(partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002774
Evan Cheng48e8c802008-05-02 21:15:08 +00002775 sign = static_cast<unsigned int>(i1>>63);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002776 if (myexponent==0 && mysignificand==0) {
2777 // exponent, significand meaningless
2778 category = fcZero;
2779 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
2780 // exponent, significand meaningless
2781 category = fcInfinity;
2782 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
2783 // exponent meaningless
2784 category = fcNaN;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002785 significandParts()[0] = mysignificand;
2786 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002787 } else {
2788 category = fcNormal;
2789 exponent = myexponent - 16383;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002790 significandParts()[0] = mysignificand;
2791 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002792 if (myexponent==0) // denormal
2793 exponent = -16382;
Neil Booth4f881702007-09-26 21:33:42 +00002794 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002795}
2796
2797void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002798APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
2799{
2800 assert(api.getBitWidth()==128);
2801 uint64_t i1 = api.getRawData()[0];
2802 uint64_t i2 = api.getRawData()[1];
2803 uint64_t myexponent = (i1 >> 52) & 0x7ff;
2804 uint64_t mysignificand = i1 & 0xfffffffffffffLL;
2805 uint64_t myexponent2 = (i2 >> 52) & 0x7ff;
2806 uint64_t mysignificand2 = i2 & 0xfffffffffffffLL;
2807
2808 initialize(&APFloat::PPCDoubleDouble);
2809 assert(partCount()==2);
2810
Evan Cheng48e8c802008-05-02 21:15:08 +00002811 sign = static_cast<unsigned int>(i1>>63);
2812 sign2 = static_cast<unsigned int>(i2>>63);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002813 if (myexponent==0 && mysignificand==0) {
2814 // exponent, significand meaningless
2815 // exponent2 and significand2 are required to be 0; we don't check
2816 category = fcZero;
2817 } else if (myexponent==0x7ff && mysignificand==0) {
2818 // exponent, significand meaningless
2819 // exponent2 and significand2 are required to be 0; we don't check
2820 category = fcInfinity;
2821 } else if (myexponent==0x7ff && mysignificand!=0) {
2822 // exponent meaningless. So is the whole second word, but keep it
2823 // for determinism.
2824 category = fcNaN;
2825 exponent2 = myexponent2;
2826 significandParts()[0] = mysignificand;
2827 significandParts()[1] = mysignificand2;
2828 } else {
2829 category = fcNormal;
2830 // Note there is no category2; the second word is treated as if it is
2831 // fcNormal, although it might be something else considered by itself.
2832 exponent = myexponent - 1023;
2833 exponent2 = myexponent2 - 1023;
2834 significandParts()[0] = mysignificand;
2835 significandParts()[1] = mysignificand2;
2836 if (myexponent==0) // denormal
2837 exponent = -1022;
2838 else
2839 significandParts()[0] |= 0x10000000000000LL; // integer bit
2840 if (myexponent2==0)
2841 exponent2 = -1022;
2842 else
2843 significandParts()[1] |= 0x10000000000000LL; // integer bit
2844 }
2845}
2846
2847void
Neil Booth4f881702007-09-26 21:33:42 +00002848APFloat::initFromDoubleAPInt(const APInt &api)
2849{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002850 assert(api.getBitWidth()==64);
2851 uint64_t i = *api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002852 uint64_t myexponent = (i >> 52) & 0x7ff;
2853 uint64_t mysignificand = i & 0xfffffffffffffLL;
2854
Dale Johannesen343e7702007-08-24 00:56:33 +00002855 initialize(&APFloat::IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002856 assert(partCount()==1);
2857
Evan Cheng48e8c802008-05-02 21:15:08 +00002858 sign = static_cast<unsigned int>(i>>63);
Dale Johannesen343e7702007-08-24 00:56:33 +00002859 if (myexponent==0 && mysignificand==0) {
2860 // exponent, significand meaningless
2861 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002862 } else if (myexponent==0x7ff && mysignificand==0) {
2863 // exponent, significand meaningless
2864 category = fcInfinity;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002865 } else if (myexponent==0x7ff && mysignificand!=0) {
2866 // exponent meaningless
2867 category = fcNaN;
2868 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002869 } else {
Dale Johannesen343e7702007-08-24 00:56:33 +00002870 category = fcNormal;
2871 exponent = myexponent - 1023;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002872 *significandParts() = mysignificand;
2873 if (myexponent==0) // denormal
2874 exponent = -1022;
2875 else
2876 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth4f881702007-09-26 21:33:42 +00002877 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002878}
2879
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002880void
Neil Booth4f881702007-09-26 21:33:42 +00002881APFloat::initFromFloatAPInt(const APInt & api)
2882{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002883 assert(api.getBitWidth()==32);
2884 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002885 uint32_t myexponent = (i >> 23) & 0xff;
2886 uint32_t mysignificand = i & 0x7fffff;
2887
Dale Johannesen343e7702007-08-24 00:56:33 +00002888 initialize(&APFloat::IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002889 assert(partCount()==1);
2890
Dale Johanneseneaf08942007-08-31 04:03:46 +00002891 sign = i >> 31;
Dale Johannesen343e7702007-08-24 00:56:33 +00002892 if (myexponent==0 && mysignificand==0) {
2893 // exponent, significand meaningless
2894 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002895 } else if (myexponent==0xff && mysignificand==0) {
2896 // exponent, significand meaningless
2897 category = fcInfinity;
Dale Johannesen902ff942007-09-25 17:25:00 +00002898 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002899 // sign, exponent, significand meaningless
Dale Johanneseneaf08942007-08-31 04:03:46 +00002900 category = fcNaN;
2901 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002902 } else {
2903 category = fcNormal;
Dale Johannesen343e7702007-08-24 00:56:33 +00002904 exponent = myexponent - 127; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002905 *significandParts() = mysignificand;
2906 if (myexponent==0) // denormal
2907 exponent = -126;
2908 else
2909 *significandParts() |= 0x800000; // integer bit
Dale Johannesen343e7702007-08-24 00:56:33 +00002910 }
2911}
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002912
2913/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesena471c2e2007-10-11 18:07:22 +00002914/// we infer the floating point type from the size of the APInt. The
2915/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
2916/// when the size is anything else).
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002917void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002918APFloat::initFromAPInt(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002919{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002920 if (api.getBitWidth() == 32)
2921 return initFromFloatAPInt(api);
2922 else if (api.getBitWidth()==64)
2923 return initFromDoubleAPInt(api);
2924 else if (api.getBitWidth()==80)
2925 return initFromF80LongDoubleAPInt(api);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002926 else if (api.getBitWidth()==128 && !isIEEE)
2927 return initFromPPCDoubleDoubleAPInt(api);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002928 else
2929 assert(0);
2930}
2931
Dale Johannesena471c2e2007-10-11 18:07:22 +00002932APFloat::APFloat(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002933{
Dale Johannesena471c2e2007-10-11 18:07:22 +00002934 initFromAPInt(api, isIEEE);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002935}
2936
Neil Booth4f881702007-09-26 21:33:42 +00002937APFloat::APFloat(float f)
2938{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002939 APInt api = APInt(32, 0);
2940 initFromAPInt(api.floatToBits(f));
2941}
2942
Neil Booth4f881702007-09-26 21:33:42 +00002943APFloat::APFloat(double d)
2944{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002945 APInt api = APInt(64, 0);
2946 initFromAPInt(api.doubleToBits(d));
2947}