blob: e6f6b504e7a4f33c223e2a80f3e4a19596a4cd97 [file] [log] [blame]
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001//===-- APFloat.cpp - Implement APFloat class -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Neil Booth and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
15#include <cassert>
Neil Bootha30b0ee2007-10-03 22:26:02 +000016#include <cstring>
Chris Lattnerb39cdde2007-08-20 22:49:32 +000017#include "llvm/ADT/APFloat.h"
Dale Johannesend3b51fd2007-08-24 05:08:11 +000018#include "llvm/Support/MathExtras.h"
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 Lattnerb39cdde2007-08-20 22:49:32 +000026COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
27
28namespace llvm {
29
30 /* Represents floating point arithmetic semantics. */
31 struct fltSemantics {
32 /* The largest E such that 2^E is representable; this matches the
33 definition of IEEE 754. */
34 exponent_t maxExponent;
35
36 /* The smallest E such that 2^E is a normalized number; this
37 matches the definition of IEEE 754. */
38 exponent_t minExponent;
39
40 /* Number of bits in the significand. This includes the integer
41 bit. */
Neil Booth7a951ca2007-10-12 15:33:27 +000042 unsigned int precision;
Neil Boothcaf19d72007-10-14 10:29:28 +000043
44 /* True if arithmetic is supported. */
45 unsigned int arithmeticOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +000046 };
47
Neil Boothcaf19d72007-10-14 10:29:28 +000048 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
49 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
50 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
51 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, true };
52 const fltSemantics APFloat::Bogus = { 0, 0, 0, true };
Dale Johannesena471c2e2007-10-11 18:07:22 +000053
54 // The PowerPC format consists of two doubles. It does not map cleanly
55 // onto the usual format above. For now only storage of constants of
56 // this type is supported, no arithmetic.
Neil Boothcaf19d72007-10-14 10:29:28 +000057 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
Neil Booth96c74712007-10-12 16:02:31 +000058
59 /* A tight upper bound on number of parts required to hold the value
60 pow(5, power) is
61
62 power * 1024 / (441 * integerPartWidth) + 1
63
64 However, whilst the result may require only this many parts,
65 because we are multiplying two values to get it, the
66 multiplication may require an extra part with the excess part
67 being zero (consider the trivial case of 1 * 1, tcFullMultiply
68 requires two parts to hold the single-part result). So we add an
69 extra one to guarantee enough space whilst multiplying. */
70 const unsigned int maxExponent = 16383;
71 const unsigned int maxPrecision = 113;
72 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
73 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 1024)
74 / (441 * integerPartWidth));
Chris Lattnerb39cdde2007-08-20 22:49:32 +000075}
76
77/* Put a bunch of private, handy routines in an anonymous namespace. */
78namespace {
79
80 inline unsigned int
81 partCountForBits(unsigned int bits)
82 {
83 return ((bits) + integerPartWidth - 1) / integerPartWidth;
84 }
85
Neil Booth1870f292007-10-14 10:16:12 +000086 /* Returns 0U-9U. Return values >= 10U are not digits. */
87 inline unsigned int
88 decDigitValue(unsigned int c)
Chris Lattnerb39cdde2007-08-20 22:49:32 +000089 {
Neil Booth1870f292007-10-14 10:16:12 +000090 return c - '0';
Chris Lattnerb39cdde2007-08-20 22:49:32 +000091 }
92
93 unsigned int
Neil Booth96c74712007-10-12 16:02:31 +000094 hexDigitValue(unsigned int c)
Chris Lattnerb39cdde2007-08-20 22:49:32 +000095 {
96 unsigned int r;
97
98 r = c - '0';
99 if(r <= 9)
100 return r;
101
102 r = c - 'A';
103 if(r <= 5)
104 return r + 10;
105
106 r = c - 'a';
107 if(r <= 5)
108 return r + 10;
109
110 return -1U;
111 }
112
Neil Boothcaf19d72007-10-14 10:29:28 +0000113 inline void
114 assertArithmeticOK(const llvm::fltSemantics &semantics) {
115 assert(semantics.arithmeticOK
116 && "Compile-time arithmetic does not support these semantics");
117 }
118
Neil Booth1870f292007-10-14 10:16:12 +0000119 /* Return the value of a decimal exponent of the form
120 [+-]ddddddd.
121
122 If the exponent overflows, returns a large exponent with the
123 appropriate sign. */
124 static int
125 readExponent(const char *p)
126 {
127 bool isNegative;
128 unsigned int absExponent;
129 const unsigned int overlargeExponent = 24000; /* FIXME. */
130
131 isNegative = (*p == '-');
132 if (*p == '-' || *p == '+')
133 p++;
134
135 absExponent = decDigitValue(*p++);
136 assert (absExponent < 10U);
137
138 for (;;) {
139 unsigned int value;
140
141 value = decDigitValue(*p);
142 if (value >= 10U)
143 break;
144
145 p++;
146 value += absExponent * 10;
147 if (absExponent >= overlargeExponent) {
148 absExponent = overlargeExponent;
149 break;
150 }
151 absExponent = value;
152 }
153
154 if (isNegative)
155 return -(int) absExponent;
156 else
157 return (int) absExponent;
158 }
159
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000160 /* This is ugly and needs cleaning up, but I don't immediately see
161 how whilst remaining safe. */
162 static int
163 totalExponent(const char *p, int exponentAdjustment)
164 {
165 integerPart unsignedExponent;
166 bool negative, overflow;
167 long exponent;
168
169 /* Move past the exponent letter and sign to the digits. */
170 p++;
171 negative = *p == '-';
172 if(*p == '-' || *p == '+')
173 p++;
174
175 unsignedExponent = 0;
176 overflow = false;
177 for(;;) {
178 unsigned int value;
179
Neil Booth1870f292007-10-14 10:16:12 +0000180 value = decDigitValue(*p);
181 if(value >= 10U)
Neil Booth4f881702007-09-26 21:33:42 +0000182 break;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000183
184 p++;
185 unsignedExponent = unsignedExponent * 10 + value;
186 if(unsignedExponent > 65535)
Neil Booth4f881702007-09-26 21:33:42 +0000187 overflow = true;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000188 }
189
190 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
191 overflow = true;
192
193 if(!overflow) {
194 exponent = unsignedExponent;
195 if(negative)
Neil Booth4f881702007-09-26 21:33:42 +0000196 exponent = -exponent;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000197 exponent += exponentAdjustment;
198 if(exponent > 65535 || exponent < -65536)
Neil Booth4f881702007-09-26 21:33:42 +0000199 overflow = true;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000200 }
201
202 if(overflow)
203 exponent = negative ? -65536: 65535;
204
205 return exponent;
206 }
207
208 const char *
209 skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
210 {
211 *dot = 0;
212 while(*p == '0')
213 p++;
214
215 if(*p == '.') {
216 *dot = p++;
217 while(*p == '0')
Neil Booth4f881702007-09-26 21:33:42 +0000218 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000219 }
220
221 return p;
222 }
223
Neil Booth1870f292007-10-14 10:16:12 +0000224 /* Given a normal decimal floating point number of the form
225
226 dddd.dddd[eE][+-]ddd
227
228 where the decimal point and exponent are optional, fill out the
229 structure D. If the value is zero, V->firstSigDigit
230 points to a zero, and the return exponent is zero. */
231 struct decimalInfo {
232 const char *firstSigDigit;
233 const char *lastSigDigit;
234 int exponent;
235 };
236
237 void
238 interpretDecimal(const char *p, decimalInfo *D)
239 {
240 const char *dot;
241
242 p = skipLeadingZeroesAndAnyDot (p, &dot);
243
244 D->firstSigDigit = p;
245 D->exponent = 0;
246
247 for (;;) {
248 if (*p == '.') {
249 assert(dot == 0);
250 dot = p++;
251 }
252 if (decDigitValue(*p) >= 10U)
253 break;
254 p++;
255 }
256
257 /* If number is all zerooes accept any exponent. */
258 if (p != D->firstSigDigit) {
259 if (*p == 'e' || *p == 'E')
260 D->exponent = readExponent(p + 1);
261
262 /* Implied decimal point? */
263 if (!dot)
264 dot = p;
265
266 /* Drop insignificant trailing zeroes. */
267 do
268 do
269 p--;
270 while (*p == '0');
271 while (*p == '.');
272
273 /* Adjust the specified exponent for any decimal point. */
274 D->exponent += (dot - p) - (dot > p);
275 }
276
277 D->lastSigDigit = p;
278 }
279
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000280 /* Return the trailing fraction of a hexadecimal number.
281 DIGITVALUE is the first hex digit of the fraction, P points to
282 the next digit. */
283 lostFraction
284 trailingHexadecimalFraction(const char *p, unsigned int digitValue)
285 {
286 unsigned int hexDigit;
287
288 /* If the first trailing digit isn't 0 or 8 we can work out the
289 fraction immediately. */
290 if(digitValue > 8)
291 return lfMoreThanHalf;
292 else if(digitValue < 8 && digitValue > 0)
293 return lfLessThanHalf;
294
295 /* Otherwise we need to find the first non-zero digit. */
296 while(*p == '0')
297 p++;
298
299 hexDigit = hexDigitValue(*p);
300
301 /* If we ran off the end it is exactly zero or one-half, otherwise
302 a little more. */
303 if(hexDigit == -1U)
304 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
305 else
306 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
307 }
308
Neil Boothb7dea4c2007-10-03 15:16:41 +0000309 /* Return the fraction lost were a bignum truncated losing the least
310 significant BITS bits. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000311 lostFraction
Neil Bootha30b0ee2007-10-03 22:26:02 +0000312 lostFractionThroughTruncation(const integerPart *parts,
Neil Booth4f881702007-09-26 21:33:42 +0000313 unsigned int partCount,
314 unsigned int bits)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000315 {
316 unsigned int lsb;
317
318 lsb = APInt::tcLSB(parts, partCount);
319
320 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
321 if(bits <= lsb)
322 return lfExactlyZero;
323 if(bits == lsb + 1)
324 return lfExactlyHalf;
325 if(bits <= partCount * integerPartWidth
326 && APInt::tcExtractBit(parts, bits - 1))
327 return lfMoreThanHalf;
328
329 return lfLessThanHalf;
330 }
331
332 /* Shift DST right BITS bits noting lost fraction. */
333 lostFraction
334 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
335 {
336 lostFraction lost_fraction;
337
338 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
339
340 APInt::tcShiftRight(dst, parts, bits);
341
342 return lost_fraction;
343 }
Neil Bootha30b0ee2007-10-03 22:26:02 +0000344
Neil Booth33d4c922007-10-07 08:51:21 +0000345 /* Combine the effect of two lost fractions. */
346 lostFraction
347 combineLostFractions(lostFraction moreSignificant,
348 lostFraction lessSignificant)
349 {
350 if(lessSignificant != lfExactlyZero) {
351 if(moreSignificant == lfExactlyZero)
352 moreSignificant = lfLessThanHalf;
353 else if(moreSignificant == lfExactlyHalf)
354 moreSignificant = lfMoreThanHalf;
355 }
356
357 return moreSignificant;
358 }
Neil Bootha30b0ee2007-10-03 22:26:02 +0000359
Neil Booth96c74712007-10-12 16:02:31 +0000360 /* The error from the true value, in half-ulps, on multiplying two
361 floating point numbers, which differ from the value they
362 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
363 than the returned value.
364
365 See "How to Read Floating Point Numbers Accurately" by William D
366 Clinger. */
367 unsigned int
368 HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
369 {
370 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
371
372 if (HUerr1 + HUerr2 == 0)
373 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
374 else
375 return inexactMultiply + 2 * (HUerr1 + HUerr2);
376 }
377
378 /* The number of ulps from the boundary (zero, or half if ISNEAREST)
379 when the least significant BITS are truncated. BITS cannot be
380 zero. */
381 integerPart
382 ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
383 {
384 unsigned int count, partBits;
385 integerPart part, boundary;
386
387 assert (bits != 0);
388
389 bits--;
390 count = bits / integerPartWidth;
391 partBits = bits % integerPartWidth + 1;
392
393 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
394
395 if (isNearest)
396 boundary = (integerPart) 1 << (partBits - 1);
397 else
398 boundary = 0;
399
400 if (count == 0) {
401 if (part - boundary <= boundary - part)
402 return part - boundary;
403 else
404 return boundary - part;
405 }
406
407 if (part == boundary) {
408 while (--count)
409 if (parts[count])
410 return ~(integerPart) 0; /* A lot. */
411
412 return parts[0];
413 } else if (part == boundary - 1) {
414 while (--count)
415 if (~parts[count])
416 return ~(integerPart) 0; /* A lot. */
417
418 return -parts[0];
419 }
420
421 return ~(integerPart) 0; /* A lot. */
422 }
423
424 /* Place pow(5, power) in DST, and return the number of parts used.
425 DST must be at least one part larger than size of the answer. */
426 static unsigned int
427 powerOf5(integerPart *dst, unsigned int power)
428 {
Neil Booth96c74712007-10-12 16:02:31 +0000429 static integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
430 15625, 78125 };
431 static integerPart pow5s[maxPowerOfFiveParts * 2 + 5] = { 78125 * 5 };
432 static unsigned int partsCount[16] = { 1 };
433
434 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
435 unsigned int result;
436
437 assert(power <= maxExponent);
438
439 p1 = dst;
440 p2 = scratch;
441
442 *p1 = firstEightPowers[power & 7];
443 power >>= 3;
444
445 result = 1;
446 pow5 = pow5s;
447
448 for (unsigned int n = 0; power; power >>= 1, n++) {
449 unsigned int pc;
450
451 pc = partsCount[n];
452
453 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
454 if (pc == 0) {
455 pc = partsCount[n - 1];
456 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
457 pc *= 2;
458 if (pow5[pc - 1] == 0)
459 pc--;
460 partsCount[n] = pc;
461 }
462
463 if (power & 1) {
464 integerPart *tmp;
465
466 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
467 result += pc;
468 if (p2[result - 1] == 0)
469 result--;
470
471 /* Now result is in p1 with partsCount parts and p2 is scratch
472 space. */
473 tmp = p1, p1 = p2, p2 = tmp;
474 }
475
476 pow5 += pc;
477 }
478
479 if (p1 != dst)
480 APInt::tcAssign(dst, p1, result);
481
482 return result;
483 }
484
Neil Bootha30b0ee2007-10-03 22:26:02 +0000485 /* Zero at the end to avoid modular arithmetic when adding one; used
486 when rounding up during hexadecimal output. */
487 static const char hexDigitsLower[] = "0123456789abcdef0";
488 static const char hexDigitsUpper[] = "0123456789ABCDEF0";
489 static const char infinityL[] = "infinity";
490 static const char infinityU[] = "INFINITY";
491 static const char NaNL[] = "nan";
492 static const char NaNU[] = "NAN";
493
494 /* Write out an integerPart in hexadecimal, starting with the most
495 significant nibble. Write out exactly COUNT hexdigits, return
496 COUNT. */
497 static unsigned int
498 partAsHex (char *dst, integerPart part, unsigned int count,
499 const char *hexDigitChars)
500 {
501 unsigned int result = count;
502
503 assert (count != 0 && count <= integerPartWidth / 4);
504
505 part >>= (integerPartWidth - 4 * count);
506 while (count--) {
507 dst[count] = hexDigitChars[part & 0xf];
508 part >>= 4;
509 }
510
511 return result;
512 }
513
Neil Booth92f7e8d2007-10-06 07:29:25 +0000514 /* Write out an unsigned decimal integer. */
Neil Bootha30b0ee2007-10-03 22:26:02 +0000515 static char *
Neil Booth92f7e8d2007-10-06 07:29:25 +0000516 writeUnsignedDecimal (char *dst, unsigned int n)
Neil Bootha30b0ee2007-10-03 22:26:02 +0000517 {
Neil Booth92f7e8d2007-10-06 07:29:25 +0000518 char buff[40], *p;
Neil Bootha30b0ee2007-10-03 22:26:02 +0000519
Neil Booth92f7e8d2007-10-06 07:29:25 +0000520 p = buff;
521 do
522 *p++ = '0' + n % 10;
523 while (n /= 10);
524
525 do
526 *dst++ = *--p;
527 while (p != buff);
528
529 return dst;
530 }
531
532 /* Write out a signed decimal integer. */
533 static char *
534 writeSignedDecimal (char *dst, int value)
535 {
536 if (value < 0) {
Neil Bootha30b0ee2007-10-03 22:26:02 +0000537 *dst++ = '-';
Neil Booth92f7e8d2007-10-06 07:29:25 +0000538 dst = writeUnsignedDecimal(dst, -(unsigned) value);
539 } else
540 dst = writeUnsignedDecimal(dst, value);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000541
542 return dst;
543 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000544}
545
546/* Constructors. */
547void
548APFloat::initialize(const fltSemantics *ourSemantics)
549{
550 unsigned int count;
551
552 semantics = ourSemantics;
553 count = partCount();
554 if(count > 1)
555 significand.parts = new integerPart[count];
556}
557
558void
559APFloat::freeSignificand()
560{
561 if(partCount() > 1)
562 delete [] significand.parts;
563}
564
565void
566APFloat::assign(const APFloat &rhs)
567{
568 assert(semantics == rhs.semantics);
569
570 sign = rhs.sign;
571 category = rhs.category;
572 exponent = rhs.exponent;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000573 sign2 = rhs.sign2;
574 exponent2 = rhs.exponent2;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000575 if(category == fcNormal || category == fcNaN)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000576 copySignificand(rhs);
577}
578
579void
580APFloat::copySignificand(const APFloat &rhs)
581{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000582 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000583 assert(rhs.partCount() >= partCount());
584
585 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +0000586 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000587}
588
589APFloat &
590APFloat::operator=(const APFloat &rhs)
591{
592 if(this != &rhs) {
593 if(semantics != rhs.semantics) {
594 freeSignificand();
595 initialize(rhs.semantics);
596 }
597 assign(rhs);
598 }
599
600 return *this;
601}
602
Dale Johannesen343e7702007-08-24 00:56:33 +0000603bool
Dale Johannesen12595d72007-08-24 22:09:56 +0000604APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000605 if (this == &rhs)
606 return true;
607 if (semantics != rhs.semantics ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000608 category != rhs.category ||
609 sign != rhs.sign)
Dale Johannesen343e7702007-08-24 00:56:33 +0000610 return false;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000611 if (semantics==(const llvm::fltSemantics* const)&PPCDoubleDouble &&
612 sign2 != rhs.sign2)
613 return false;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000614 if (category==fcZero || category==fcInfinity)
Dale Johannesen343e7702007-08-24 00:56:33 +0000615 return true;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000616 else if (category==fcNormal && exponent!=rhs.exponent)
617 return false;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000618 else if (semantics==(const llvm::fltSemantics* const)&PPCDoubleDouble &&
619 exponent2!=rhs.exponent2)
620 return false;
Dale Johannesen343e7702007-08-24 00:56:33 +0000621 else {
Dale Johannesen343e7702007-08-24 00:56:33 +0000622 int i= partCount();
623 const integerPart* p=significandParts();
624 const integerPart* q=rhs.significandParts();
625 for (; i>0; i--, p++, q++) {
626 if (*p != *q)
627 return false;
628 }
629 return true;
630 }
631}
632
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000633APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
634{
Neil Boothcaf19d72007-10-14 10:29:28 +0000635 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000636 initialize(&ourSemantics);
637 sign = 0;
638 zeroSignificand();
639 exponent = ourSemantics.precision - 1;
640 significandParts()[0] = value;
641 normalize(rmNearestTiesToEven, lfExactlyZero);
642}
643
644APFloat::APFloat(const fltSemantics &ourSemantics,
Neil Booth4f881702007-09-26 21:33:42 +0000645 fltCategory ourCategory, bool negative)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000646{
Neil Boothcaf19d72007-10-14 10:29:28 +0000647 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000648 initialize(&ourSemantics);
649 category = ourCategory;
650 sign = negative;
651 if(category == fcNormal)
652 category = fcZero;
653}
654
655APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
656{
Neil Boothcaf19d72007-10-14 10:29:28 +0000657 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000658 initialize(&ourSemantics);
659 convertFromString(text, rmNearestTiesToEven);
660}
661
662APFloat::APFloat(const APFloat &rhs)
663{
664 initialize(rhs.semantics);
665 assign(rhs);
666}
667
668APFloat::~APFloat()
669{
670 freeSignificand();
671}
672
673unsigned int
674APFloat::partCount() const
675{
Dale Johannesena72a5a02007-09-20 23:47:58 +0000676 return partCountForBits(semantics->precision + 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000677}
678
679unsigned int
680APFloat::semanticsPrecision(const fltSemantics &semantics)
681{
682 return semantics.precision;
683}
684
685const integerPart *
686APFloat::significandParts() const
687{
688 return const_cast<APFloat *>(this)->significandParts();
689}
690
691integerPart *
692APFloat::significandParts()
693{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000694 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000695
696 if(partCount() > 1)
697 return significand.parts;
698 else
699 return &significand.part;
700}
701
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000702void
703APFloat::zeroSignificand()
704{
705 category = fcNormal;
706 APInt::tcSet(significandParts(), 0, partCount());
707}
708
709/* Increment an fcNormal floating point number's significand. */
710void
711APFloat::incrementSignificand()
712{
713 integerPart carry;
714
715 carry = APInt::tcIncrement(significandParts(), partCount());
716
717 /* Our callers should never cause us to overflow. */
718 assert(carry == 0);
719}
720
721/* Add the significand of the RHS. Returns the carry flag. */
722integerPart
723APFloat::addSignificand(const APFloat &rhs)
724{
725 integerPart *parts;
726
727 parts = significandParts();
728
729 assert(semantics == rhs.semantics);
730 assert(exponent == rhs.exponent);
731
732 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
733}
734
735/* Subtract the significand of the RHS with a borrow flag. Returns
736 the borrow flag. */
737integerPart
738APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
739{
740 integerPart *parts;
741
742 parts = significandParts();
743
744 assert(semantics == rhs.semantics);
745 assert(exponent == rhs.exponent);
746
747 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth4f881702007-09-26 21:33:42 +0000748 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000749}
750
751/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
752 on to the full-precision result of the multiplication. Returns the
753 lost fraction. */
754lostFraction
755APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
756{
Neil Booth4f881702007-09-26 21:33:42 +0000757 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000758 unsigned int partsCount, newPartsCount, precision;
759 integerPart *lhsSignificand;
760 integerPart scratch[4];
761 integerPart *fullSignificand;
762 lostFraction lost_fraction;
763
764 assert(semantics == rhs.semantics);
765
766 precision = semantics->precision;
767 newPartsCount = partCountForBits(precision * 2);
768
769 if(newPartsCount > 4)
770 fullSignificand = new integerPart[newPartsCount];
771 else
772 fullSignificand = scratch;
773
774 lhsSignificand = significandParts();
775 partsCount = partCount();
776
777 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth978661d2007-10-06 00:24:48 +0000778 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000779
780 lost_fraction = lfExactlyZero;
781 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
782 exponent += rhs.exponent;
783
784 if(addend) {
785 Significand savedSignificand = significand;
786 const fltSemantics *savedSemantics = semantics;
787 fltSemantics extendedSemantics;
788 opStatus status;
789 unsigned int extendedPrecision;
790
791 /* Normalize our MSB. */
792 extendedPrecision = precision + precision - 1;
793 if(omsb != extendedPrecision)
794 {
Neil Booth4f881702007-09-26 21:33:42 +0000795 APInt::tcShiftLeft(fullSignificand, newPartsCount,
796 extendedPrecision - omsb);
797 exponent -= extendedPrecision - omsb;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000798 }
799
800 /* Create new semantics. */
801 extendedSemantics = *semantics;
802 extendedSemantics.precision = extendedPrecision;
803
804 if(newPartsCount == 1)
805 significand.part = fullSignificand[0];
806 else
807 significand.parts = fullSignificand;
808 semantics = &extendedSemantics;
809
810 APFloat extendedAddend(*addend);
811 status = extendedAddend.convert(extendedSemantics, rmTowardZero);
812 assert(status == opOK);
813 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
814
815 /* Restore our state. */
816 if(newPartsCount == 1)
817 fullSignificand[0] = significand.part;
818 significand = savedSignificand;
819 semantics = savedSemantics;
820
821 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
822 }
823
824 exponent -= (precision - 1);
825
826 if(omsb > precision) {
827 unsigned int bits, significantParts;
828 lostFraction lf;
829
830 bits = omsb - precision;
831 significantParts = partCountForBits(omsb);
832 lf = shiftRight(fullSignificand, significantParts, bits);
833 lost_fraction = combineLostFractions(lf, lost_fraction);
834 exponent += bits;
835 }
836
837 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
838
839 if(newPartsCount > 4)
840 delete [] fullSignificand;
841
842 return lost_fraction;
843}
844
845/* Multiply the significands of LHS and RHS to DST. */
846lostFraction
847APFloat::divideSignificand(const APFloat &rhs)
848{
849 unsigned int bit, i, partsCount;
850 const integerPart *rhsSignificand;
851 integerPart *lhsSignificand, *dividend, *divisor;
852 integerPart scratch[4];
853 lostFraction lost_fraction;
854
855 assert(semantics == rhs.semantics);
856
857 lhsSignificand = significandParts();
858 rhsSignificand = rhs.significandParts();
859 partsCount = partCount();
860
861 if(partsCount > 2)
862 dividend = new integerPart[partsCount * 2];
863 else
864 dividend = scratch;
865
866 divisor = dividend + partsCount;
867
868 /* Copy the dividend and divisor as they will be modified in-place. */
869 for(i = 0; i < partsCount; i++) {
870 dividend[i] = lhsSignificand[i];
871 divisor[i] = rhsSignificand[i];
872 lhsSignificand[i] = 0;
873 }
874
875 exponent -= rhs.exponent;
876
877 unsigned int precision = semantics->precision;
878
879 /* Normalize the divisor. */
880 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
881 if(bit) {
882 exponent += bit;
883 APInt::tcShiftLeft(divisor, partsCount, bit);
884 }
885
886 /* Normalize the dividend. */
887 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
888 if(bit) {
889 exponent -= bit;
890 APInt::tcShiftLeft(dividend, partsCount, bit);
891 }
892
Neil Booth96c74712007-10-12 16:02:31 +0000893 /* Ensure the dividend >= divisor initially for the loop below.
894 Incidentally, this means that the division loop below is
895 guaranteed to set the integer bit to one. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000896 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
897 exponent--;
898 APInt::tcShiftLeft(dividend, partsCount, 1);
899 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
900 }
901
902 /* Long division. */
903 for(bit = precision; bit; bit -= 1) {
904 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
905 APInt::tcSubtract(dividend, divisor, 0, partsCount);
906 APInt::tcSetBit(lhsSignificand, bit - 1);
907 }
908
909 APInt::tcShiftLeft(dividend, partsCount, 1);
910 }
911
912 /* Figure out the lost fraction. */
913 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
914
915 if(cmp > 0)
916 lost_fraction = lfMoreThanHalf;
917 else if(cmp == 0)
918 lost_fraction = lfExactlyHalf;
919 else if(APInt::tcIsZero(dividend, partsCount))
920 lost_fraction = lfExactlyZero;
921 else
922 lost_fraction = lfLessThanHalf;
923
924 if(partsCount > 2)
925 delete [] dividend;
926
927 return lost_fraction;
928}
929
930unsigned int
931APFloat::significandMSB() const
932{
933 return APInt::tcMSB(significandParts(), partCount());
934}
935
936unsigned int
937APFloat::significandLSB() const
938{
939 return APInt::tcLSB(significandParts(), partCount());
940}
941
942/* Note that a zero result is NOT normalized to fcZero. */
943lostFraction
944APFloat::shiftSignificandRight(unsigned int bits)
945{
946 /* Our exponent should not overflow. */
947 assert((exponent_t) (exponent + bits) >= exponent);
948
949 exponent += bits;
950
951 return shiftRight(significandParts(), partCount(), bits);
952}
953
954/* Shift the significand left BITS bits, subtract BITS from its exponent. */
955void
956APFloat::shiftSignificandLeft(unsigned int bits)
957{
958 assert(bits < semantics->precision);
959
960 if(bits) {
961 unsigned int partsCount = partCount();
962
963 APInt::tcShiftLeft(significandParts(), partsCount, bits);
964 exponent -= bits;
965
966 assert(!APInt::tcIsZero(significandParts(), partsCount));
967 }
968}
969
970APFloat::cmpResult
971APFloat::compareAbsoluteValue(const APFloat &rhs) const
972{
973 int compare;
974
975 assert(semantics == rhs.semantics);
976 assert(category == fcNormal);
977 assert(rhs.category == fcNormal);
978
979 compare = exponent - rhs.exponent;
980
981 /* If exponents are equal, do an unsigned bignum comparison of the
982 significands. */
983 if(compare == 0)
984 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +0000985 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000986
987 if(compare > 0)
988 return cmpGreaterThan;
989 else if(compare < 0)
990 return cmpLessThan;
991 else
992 return cmpEqual;
993}
994
995/* Handle overflow. Sign is preserved. We either become infinity or
996 the largest finite number. */
997APFloat::opStatus
998APFloat::handleOverflow(roundingMode rounding_mode)
999{
1000 /* Infinity? */
1001 if(rounding_mode == rmNearestTiesToEven
1002 || rounding_mode == rmNearestTiesToAway
1003 || (rounding_mode == rmTowardPositive && !sign)
1004 || (rounding_mode == rmTowardNegative && sign))
1005 {
1006 category = fcInfinity;
1007 return (opStatus) (opOverflow | opInexact);
1008 }
1009
1010 /* Otherwise we become the largest finite number. */
1011 category = fcNormal;
1012 exponent = semantics->maxExponent;
1013 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth4f881702007-09-26 21:33:42 +00001014 semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001015
1016 return opInexact;
1017}
1018
Neil Boothb7dea4c2007-10-03 15:16:41 +00001019/* Returns TRUE if, when truncating the current number, with BIT the
1020 new LSB, with the given lost fraction and rounding mode, the result
1021 would need to be rounded away from zero (i.e., by increasing the
1022 signficand). This routine must work for fcZero of both signs, and
1023 fcNormal numbers. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001024bool
1025APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Boothb7dea4c2007-10-03 15:16:41 +00001026 lostFraction lost_fraction,
1027 unsigned int bit) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001028{
Dale Johanneseneaf08942007-08-31 04:03:46 +00001029 /* NaNs and infinities should not have lost fractions. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001030 assert(category == fcNormal || category == fcZero);
1031
Neil Boothb7dea4c2007-10-03 15:16:41 +00001032 /* Current callers never pass this so we don't handle it. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001033 assert(lost_fraction != lfExactlyZero);
1034
1035 switch(rounding_mode) {
1036 default:
1037 assert(0);
1038
1039 case rmNearestTiesToAway:
1040 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1041
1042 case rmNearestTiesToEven:
1043 if(lost_fraction == lfMoreThanHalf)
1044 return true;
1045
1046 /* Our zeroes don't have a significand to test. */
1047 if(lost_fraction == lfExactlyHalf && category != fcZero)
Neil Boothb7dea4c2007-10-03 15:16:41 +00001048 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001049
1050 return false;
1051
1052 case rmTowardZero:
1053 return false;
1054
1055 case rmTowardPositive:
1056 return sign == false;
1057
1058 case rmTowardNegative:
1059 return sign == true;
1060 }
1061}
1062
1063APFloat::opStatus
1064APFloat::normalize(roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001065 lostFraction lost_fraction)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001066{
Neil Booth4f881702007-09-26 21:33:42 +00001067 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001068 int exponentChange;
1069
1070 if(category != fcNormal)
1071 return opOK;
1072
1073 /* Before rounding normalize the exponent of fcNormal numbers. */
1074 omsb = significandMSB() + 1;
1075
1076 if(omsb) {
1077 /* OMSB is numbered from 1. We want to place it in the integer
1078 bit numbered PRECISON if possible, with a compensating change in
1079 the exponent. */
1080 exponentChange = omsb - semantics->precision;
1081
1082 /* If the resulting exponent is too high, overflow according to
1083 the rounding mode. */
1084 if(exponent + exponentChange > semantics->maxExponent)
1085 return handleOverflow(rounding_mode);
1086
1087 /* Subnormal numbers have exponent minExponent, and their MSB
1088 is forced based on that. */
1089 if(exponent + exponentChange < semantics->minExponent)
1090 exponentChange = semantics->minExponent - exponent;
1091
1092 /* Shifting left is easy as we don't lose precision. */
1093 if(exponentChange < 0) {
1094 assert(lost_fraction == lfExactlyZero);
1095
1096 shiftSignificandLeft(-exponentChange);
1097
1098 return opOK;
1099 }
1100
1101 if(exponentChange > 0) {
1102 lostFraction lf;
1103
1104 /* Shift right and capture any new lost fraction. */
1105 lf = shiftSignificandRight(exponentChange);
1106
1107 lost_fraction = combineLostFractions(lf, lost_fraction);
1108
1109 /* Keep OMSB up-to-date. */
1110 if(omsb > (unsigned) exponentChange)
Neil Booth96c74712007-10-12 16:02:31 +00001111 omsb -= exponentChange;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001112 else
Neil Booth4f881702007-09-26 21:33:42 +00001113 omsb = 0;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001114 }
1115 }
1116
1117 /* Now round the number according to rounding_mode given the lost
1118 fraction. */
1119
1120 /* As specified in IEEE 754, since we do not trap we do not report
1121 underflow for exact results. */
1122 if(lost_fraction == lfExactlyZero) {
1123 /* Canonicalize zeroes. */
1124 if(omsb == 0)
1125 category = fcZero;
1126
1127 return opOK;
1128 }
1129
1130 /* Increment the significand if we're rounding away from zero. */
Neil Boothb7dea4c2007-10-03 15:16:41 +00001131 if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001132 if(omsb == 0)
1133 exponent = semantics->minExponent;
1134
1135 incrementSignificand();
1136 omsb = significandMSB() + 1;
1137
1138 /* Did the significand increment overflow? */
1139 if(omsb == (unsigned) semantics->precision + 1) {
1140 /* Renormalize by incrementing the exponent and shifting our
Neil Booth4f881702007-09-26 21:33:42 +00001141 significand right one. However if we already have the
1142 maximum exponent we overflow to infinity. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001143 if(exponent == semantics->maxExponent) {
Neil Booth4f881702007-09-26 21:33:42 +00001144 category = fcInfinity;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001145
Neil Booth4f881702007-09-26 21:33:42 +00001146 return (opStatus) (opOverflow | opInexact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001147 }
1148
1149 shiftSignificandRight(1);
1150
1151 return opInexact;
1152 }
1153 }
1154
1155 /* The normal case - we were and are not denormal, and any
1156 significand increment above didn't overflow. */
1157 if(omsb == semantics->precision)
1158 return opInexact;
1159
1160 /* We have a non-zero denormal. */
1161 assert(omsb < semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001162
1163 /* Canonicalize zeroes. */
1164 if(omsb == 0)
1165 category = fcZero;
1166
1167 /* The fcZero case is a denormal that underflowed to zero. */
1168 return (opStatus) (opUnderflow | opInexact);
1169}
1170
1171APFloat::opStatus
1172APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1173{
1174 switch(convolve(category, rhs.category)) {
1175 default:
1176 assert(0);
1177
Dale Johanneseneaf08942007-08-31 04:03:46 +00001178 case convolve(fcNaN, fcZero):
1179 case convolve(fcNaN, fcNormal):
1180 case convolve(fcNaN, fcInfinity):
1181 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001182 case convolve(fcNormal, fcZero):
1183 case convolve(fcInfinity, fcNormal):
1184 case convolve(fcInfinity, fcZero):
1185 return opOK;
1186
Dale Johanneseneaf08942007-08-31 04:03:46 +00001187 case convolve(fcZero, fcNaN):
1188 case convolve(fcNormal, fcNaN):
1189 case convolve(fcInfinity, fcNaN):
1190 category = fcNaN;
1191 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001192 return opOK;
1193
1194 case convolve(fcNormal, fcInfinity):
1195 case convolve(fcZero, fcInfinity):
1196 category = fcInfinity;
1197 sign = rhs.sign ^ subtract;
1198 return opOK;
1199
1200 case convolve(fcZero, fcNormal):
1201 assign(rhs);
1202 sign = rhs.sign ^ subtract;
1203 return opOK;
1204
1205 case convolve(fcZero, fcZero):
1206 /* Sign depends on rounding mode; handled by caller. */
1207 return opOK;
1208
1209 case convolve(fcInfinity, fcInfinity):
1210 /* Differently signed infinities can only be validly
1211 subtracted. */
1212 if(sign ^ rhs.sign != subtract) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001213 category = fcNaN;
1214 // Arbitrary but deterministic value for significand
1215 APInt::tcSet(significandParts(), ~0U, partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001216 return opInvalidOp;
1217 }
1218
1219 return opOK;
1220
1221 case convolve(fcNormal, fcNormal):
1222 return opDivByZero;
1223 }
1224}
1225
1226/* Add or subtract two normal numbers. */
1227lostFraction
1228APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1229{
1230 integerPart carry;
1231 lostFraction lost_fraction;
1232 int bits;
1233
1234 /* Determine if the operation on the absolute values is effectively
1235 an addition or subtraction. */
1236 subtract ^= (sign ^ rhs.sign);
1237
1238 /* Are we bigger exponent-wise than the RHS? */
1239 bits = exponent - rhs.exponent;
1240
1241 /* Subtraction is more subtle than one might naively expect. */
1242 if(subtract) {
1243 APFloat temp_rhs(rhs);
1244 bool reverse;
1245
Chris Lattnerada530b2007-08-24 03:02:34 +00001246 if (bits == 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001247 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1248 lost_fraction = lfExactlyZero;
Chris Lattnerada530b2007-08-24 03:02:34 +00001249 } else if (bits > 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001250 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1251 shiftSignificandLeft(1);
1252 reverse = false;
Chris Lattnerada530b2007-08-24 03:02:34 +00001253 } else {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001254 lost_fraction = shiftSignificandRight(-bits - 1);
1255 temp_rhs.shiftSignificandLeft(1);
1256 reverse = true;
1257 }
1258
Chris Lattnerada530b2007-08-24 03:02:34 +00001259 if (reverse) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001260 carry = temp_rhs.subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001261 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001262 copySignificand(temp_rhs);
1263 sign = !sign;
1264 } else {
1265 carry = subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001266 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001267 }
1268
1269 /* Invert the lost fraction - it was on the RHS and
1270 subtracted. */
1271 if(lost_fraction == lfLessThanHalf)
1272 lost_fraction = lfMoreThanHalf;
1273 else if(lost_fraction == lfMoreThanHalf)
1274 lost_fraction = lfLessThanHalf;
1275
1276 /* The code above is intended to ensure that no borrow is
1277 necessary. */
1278 assert(!carry);
1279 } else {
1280 if(bits > 0) {
1281 APFloat temp_rhs(rhs);
1282
1283 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1284 carry = addSignificand(temp_rhs);
1285 } else {
1286 lost_fraction = shiftSignificandRight(-bits);
1287 carry = addSignificand(rhs);
1288 }
1289
1290 /* We have a guard bit; generating a carry cannot happen. */
1291 assert(!carry);
1292 }
1293
1294 return lost_fraction;
1295}
1296
1297APFloat::opStatus
1298APFloat::multiplySpecials(const APFloat &rhs)
1299{
1300 switch(convolve(category, rhs.category)) {
1301 default:
1302 assert(0);
1303
Dale Johanneseneaf08942007-08-31 04:03:46 +00001304 case convolve(fcNaN, fcZero):
1305 case convolve(fcNaN, fcNormal):
1306 case convolve(fcNaN, fcInfinity):
1307 case convolve(fcNaN, fcNaN):
1308 return opOK;
1309
1310 case convolve(fcZero, fcNaN):
1311 case convolve(fcNormal, fcNaN):
1312 case convolve(fcInfinity, fcNaN):
1313 category = fcNaN;
1314 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001315 return opOK;
1316
1317 case convolve(fcNormal, fcInfinity):
1318 case convolve(fcInfinity, fcNormal):
1319 case convolve(fcInfinity, fcInfinity):
1320 category = fcInfinity;
1321 return opOK;
1322
1323 case convolve(fcZero, fcNormal):
1324 case convolve(fcNormal, fcZero):
1325 case convolve(fcZero, fcZero):
1326 category = fcZero;
1327 return opOK;
1328
1329 case convolve(fcZero, fcInfinity):
1330 case convolve(fcInfinity, fcZero):
Dale Johanneseneaf08942007-08-31 04:03:46 +00001331 category = fcNaN;
1332 // Arbitrary but deterministic value for significand
1333 APInt::tcSet(significandParts(), ~0U, partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001334 return opInvalidOp;
1335
1336 case convolve(fcNormal, fcNormal):
1337 return opOK;
1338 }
1339}
1340
1341APFloat::opStatus
1342APFloat::divideSpecials(const APFloat &rhs)
1343{
1344 switch(convolve(category, rhs.category)) {
1345 default:
1346 assert(0);
1347
Dale Johanneseneaf08942007-08-31 04:03:46 +00001348 case convolve(fcNaN, fcZero):
1349 case convolve(fcNaN, fcNormal):
1350 case convolve(fcNaN, fcInfinity):
1351 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001352 case convolve(fcInfinity, fcZero):
1353 case convolve(fcInfinity, fcNormal):
1354 case convolve(fcZero, fcInfinity):
1355 case convolve(fcZero, fcNormal):
1356 return opOK;
1357
Dale Johanneseneaf08942007-08-31 04:03:46 +00001358 case convolve(fcZero, fcNaN):
1359 case convolve(fcNormal, fcNaN):
1360 case convolve(fcInfinity, fcNaN):
1361 category = fcNaN;
1362 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001363 return opOK;
1364
1365 case convolve(fcNormal, fcInfinity):
1366 category = fcZero;
1367 return opOK;
1368
1369 case convolve(fcNormal, fcZero):
1370 category = fcInfinity;
1371 return opDivByZero;
1372
1373 case convolve(fcInfinity, fcInfinity):
1374 case convolve(fcZero, fcZero):
Dale Johanneseneaf08942007-08-31 04:03:46 +00001375 category = fcNaN;
1376 // Arbitrary but deterministic value for significand
1377 APInt::tcSet(significandParts(), ~0U, partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001378 return opInvalidOp;
1379
1380 case convolve(fcNormal, fcNormal):
1381 return opOK;
1382 }
1383}
1384
1385/* Change sign. */
1386void
1387APFloat::changeSign()
1388{
1389 /* Look mummy, this one's easy. */
1390 sign = !sign;
1391}
1392
Dale Johannesene15c2db2007-08-31 23:35:31 +00001393void
1394APFloat::clearSign()
1395{
1396 /* So is this one. */
1397 sign = 0;
1398}
1399
1400void
1401APFloat::copySign(const APFloat &rhs)
1402{
1403 /* And this one. */
1404 sign = rhs.sign;
1405}
1406
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001407/* Normalized addition or subtraction. */
1408APFloat::opStatus
1409APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001410 bool subtract)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001411{
1412 opStatus fs;
1413
Neil Boothcaf19d72007-10-14 10:29:28 +00001414 assertArithmeticOK(*semantics);
1415
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001416 fs = addOrSubtractSpecials(rhs, subtract);
1417
1418 /* This return code means it was not a simple case. */
1419 if(fs == opDivByZero) {
1420 lostFraction lost_fraction;
1421
1422 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1423 fs = normalize(rounding_mode, lost_fraction);
1424
1425 /* Can only be zero if we lost no fraction. */
1426 assert(category != fcZero || lost_fraction == lfExactlyZero);
1427 }
1428
1429 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1430 positive zero unless rounding to minus infinity, except that
1431 adding two like-signed zeroes gives that zero. */
1432 if(category == fcZero) {
1433 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1434 sign = (rounding_mode == rmTowardNegative);
1435 }
1436
1437 return fs;
1438}
1439
1440/* Normalized addition. */
1441APFloat::opStatus
1442APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1443{
1444 return addOrSubtract(rhs, rounding_mode, false);
1445}
1446
1447/* Normalized subtraction. */
1448APFloat::opStatus
1449APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1450{
1451 return addOrSubtract(rhs, rounding_mode, true);
1452}
1453
1454/* Normalized multiply. */
1455APFloat::opStatus
1456APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1457{
1458 opStatus fs;
1459
Neil Boothcaf19d72007-10-14 10:29:28 +00001460 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001461 sign ^= rhs.sign;
1462 fs = multiplySpecials(rhs);
1463
1464 if(category == fcNormal) {
1465 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1466 fs = normalize(rounding_mode, lost_fraction);
1467 if(lost_fraction != lfExactlyZero)
1468 fs = (opStatus) (fs | opInexact);
1469 }
1470
1471 return fs;
1472}
1473
1474/* Normalized divide. */
1475APFloat::opStatus
1476APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1477{
1478 opStatus fs;
1479
Neil Boothcaf19d72007-10-14 10:29:28 +00001480 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001481 sign ^= rhs.sign;
1482 fs = divideSpecials(rhs);
1483
1484 if(category == fcNormal) {
1485 lostFraction lost_fraction = divideSignificand(rhs);
1486 fs = normalize(rounding_mode, lost_fraction);
1487 if(lost_fraction != lfExactlyZero)
1488 fs = (opStatus) (fs | opInexact);
1489 }
1490
1491 return fs;
1492}
1493
Neil Bootha30b0ee2007-10-03 22:26:02 +00001494/* Normalized remainder. This is not currently doing TRT. */
Dale Johannesene15c2db2007-08-31 23:35:31 +00001495APFloat::opStatus
1496APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1497{
1498 opStatus fs;
1499 APFloat V = *this;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001500 unsigned int origSign = sign;
Neil Boothcaf19d72007-10-14 10:29:28 +00001501
1502 assertArithmeticOK(*semantics);
Dale Johannesene15c2db2007-08-31 23:35:31 +00001503 fs = V.divide(rhs, rmNearestTiesToEven);
1504 if (fs == opDivByZero)
1505 return fs;
1506
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001507 int parts = partCount();
1508 integerPart *x = new integerPart[parts];
Neil Booth4f881702007-09-26 21:33:42 +00001509 fs = V.convertToInteger(x, parts * integerPartWidth, true,
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001510 rmNearestTiesToEven);
Dale Johannesene15c2db2007-08-31 23:35:31 +00001511 if (fs==opInvalidOp)
1512 return fs;
1513
Neil Boothccf596a2007-10-07 11:45:55 +00001514 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1515 rmNearestTiesToEven);
Dale Johannesene15c2db2007-08-31 23:35:31 +00001516 assert(fs==opOK); // should always work
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001517
Dale Johannesene15c2db2007-08-31 23:35:31 +00001518 fs = V.multiply(rhs, rounding_mode);
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001519 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1520
Dale Johannesene15c2db2007-08-31 23:35:31 +00001521 fs = subtract(V, rounding_mode);
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001522 assert(fs==opOK || fs==opInexact); // likewise
1523
1524 if (isZero())
1525 sign = origSign; // IEEE754 requires this
1526 delete[] x;
Dale Johannesene15c2db2007-08-31 23:35:31 +00001527 return fs;
1528}
1529
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001530/* Normalized fused-multiply-add. */
1531APFloat::opStatus
1532APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth4f881702007-09-26 21:33:42 +00001533 const APFloat &addend,
1534 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001535{
1536 opStatus fs;
1537
Neil Boothcaf19d72007-10-14 10:29:28 +00001538 assertArithmeticOK(*semantics);
1539
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001540 /* Post-multiplication sign, before addition. */
1541 sign ^= multiplicand.sign;
1542
1543 /* If and only if all arguments are normal do we need to do an
1544 extended-precision calculation. */
1545 if(category == fcNormal
1546 && multiplicand.category == fcNormal
1547 && addend.category == fcNormal) {
1548 lostFraction lost_fraction;
1549
1550 lost_fraction = multiplySignificand(multiplicand, &addend);
1551 fs = normalize(rounding_mode, lost_fraction);
1552 if(lost_fraction != lfExactlyZero)
1553 fs = (opStatus) (fs | opInexact);
1554
1555 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1556 positive zero unless rounding to minus infinity, except that
1557 adding two like-signed zeroes gives that zero. */
1558 if(category == fcZero && sign != addend.sign)
1559 sign = (rounding_mode == rmTowardNegative);
1560 } else {
1561 fs = multiplySpecials(multiplicand);
1562
1563 /* FS can only be opOK or opInvalidOp. There is no more work
1564 to do in the latter case. The IEEE-754R standard says it is
1565 implementation-defined in this case whether, if ADDEND is a
Dale Johanneseneaf08942007-08-31 04:03:46 +00001566 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001567
1568 If we need to do the addition we can do so with normal
1569 precision. */
1570 if(fs == opOK)
1571 fs = addOrSubtract(addend, rounding_mode, false);
1572 }
1573
1574 return fs;
1575}
1576
1577/* Comparison requires normalized numbers. */
1578APFloat::cmpResult
1579APFloat::compare(const APFloat &rhs) const
1580{
1581 cmpResult result;
1582
Neil Boothcaf19d72007-10-14 10:29:28 +00001583 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001584 assert(semantics == rhs.semantics);
1585
1586 switch(convolve(category, rhs.category)) {
1587 default:
1588 assert(0);
1589
Dale Johanneseneaf08942007-08-31 04:03:46 +00001590 case convolve(fcNaN, fcZero):
1591 case convolve(fcNaN, fcNormal):
1592 case convolve(fcNaN, fcInfinity):
1593 case convolve(fcNaN, fcNaN):
1594 case convolve(fcZero, fcNaN):
1595 case convolve(fcNormal, fcNaN):
1596 case convolve(fcInfinity, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001597 return cmpUnordered;
1598
1599 case convolve(fcInfinity, fcNormal):
1600 case convolve(fcInfinity, fcZero):
1601 case convolve(fcNormal, fcZero):
1602 if(sign)
1603 return cmpLessThan;
1604 else
1605 return cmpGreaterThan;
1606
1607 case convolve(fcNormal, fcInfinity):
1608 case convolve(fcZero, fcInfinity):
1609 case convolve(fcZero, fcNormal):
1610 if(rhs.sign)
1611 return cmpGreaterThan;
1612 else
1613 return cmpLessThan;
1614
1615 case convolve(fcInfinity, fcInfinity):
1616 if(sign == rhs.sign)
1617 return cmpEqual;
1618 else if(sign)
1619 return cmpLessThan;
1620 else
1621 return cmpGreaterThan;
1622
1623 case convolve(fcZero, fcZero):
1624 return cmpEqual;
1625
1626 case convolve(fcNormal, fcNormal):
1627 break;
1628 }
1629
1630 /* Two normal numbers. Do they have the same sign? */
1631 if(sign != rhs.sign) {
1632 if(sign)
1633 result = cmpLessThan;
1634 else
1635 result = cmpGreaterThan;
1636 } else {
1637 /* Compare absolute values; invert result if negative. */
1638 result = compareAbsoluteValue(rhs);
1639
1640 if(sign) {
1641 if(result == cmpLessThan)
Neil Booth4f881702007-09-26 21:33:42 +00001642 result = cmpGreaterThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001643 else if(result == cmpGreaterThan)
Neil Booth4f881702007-09-26 21:33:42 +00001644 result = cmpLessThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001645 }
1646 }
1647
1648 return result;
1649}
1650
1651APFloat::opStatus
1652APFloat::convert(const fltSemantics &toSemantics,
Neil Booth4f881702007-09-26 21:33:42 +00001653 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001654{
Neil Boothc8db43d2007-09-22 02:56:19 +00001655 lostFraction lostFraction;
1656 unsigned int newPartCount, oldPartCount;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001657 opStatus fs;
Neil Booth4f881702007-09-26 21:33:42 +00001658
Neil Boothcaf19d72007-10-14 10:29:28 +00001659 assertArithmeticOK(*semantics);
Neil Boothc8db43d2007-09-22 02:56:19 +00001660 lostFraction = lfExactlyZero;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001661 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Boothc8db43d2007-09-22 02:56:19 +00001662 oldPartCount = partCount();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001663
Neil Boothc8db43d2007-09-22 02:56:19 +00001664 /* Handle storage complications. If our new form is wider,
1665 re-allocate our bit pattern into wider storage. If it is
1666 narrower, we ignore the excess parts, but if narrowing to a
Dale Johannesen902ff942007-09-25 17:25:00 +00001667 single part we need to free the old storage.
1668 Be careful not to reference significandParts for zeroes
1669 and infinities, since it aborts. */
Neil Boothc8db43d2007-09-22 02:56:19 +00001670 if (newPartCount > oldPartCount) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001671 integerPart *newParts;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001672 newParts = new integerPart[newPartCount];
1673 APInt::tcSet(newParts, 0, newPartCount);
Dale Johannesen902ff942007-09-25 17:25:00 +00001674 if (category==fcNormal || category==fcNaN)
1675 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001676 freeSignificand();
1677 significand.parts = newParts;
Neil Boothc8db43d2007-09-22 02:56:19 +00001678 } else if (newPartCount < oldPartCount) {
1679 /* Capture any lost fraction through truncation of parts so we get
1680 correct rounding whilst normalizing. */
Dale Johannesen902ff942007-09-25 17:25:00 +00001681 if (category==fcNormal)
1682 lostFraction = lostFractionThroughTruncation
1683 (significandParts(), oldPartCount, toSemantics.precision);
1684 if (newPartCount == 1) {
1685 integerPart newPart = 0;
Neil Booth4f881702007-09-26 21:33:42 +00001686 if (category==fcNormal || category==fcNaN)
Dale Johannesen902ff942007-09-25 17:25:00 +00001687 newPart = significandParts()[0];
1688 freeSignificand();
1689 significand.part = newPart;
1690 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001691 }
1692
1693 if(category == fcNormal) {
1694 /* Re-interpret our bit-pattern. */
1695 exponent += toSemantics.precision - semantics->precision;
1696 semantics = &toSemantics;
Neil Boothc8db43d2007-09-22 02:56:19 +00001697 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen902ff942007-09-25 17:25:00 +00001698 } else if (category == fcNaN) {
1699 int shift = toSemantics.precision - semantics->precision;
1700 // No normalization here, just truncate
1701 if (shift>0)
1702 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
1703 else if (shift < 0)
1704 APInt::tcShiftRight(significandParts(), newPartCount, -shift);
1705 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
1706 // does not give you back the same bits. This is dubious, and we
1707 // don't currently do it. You're really supposed to get
1708 // an invalid operation signal at runtime, but nobody does that.
1709 semantics = &toSemantics;
1710 fs = opOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001711 } else {
1712 semantics = &toSemantics;
1713 fs = opOK;
1714 }
1715
1716 return fs;
1717}
1718
1719/* Convert a floating point number to an integer according to the
1720 rounding mode. If the rounded integer value is out of range this
1721 returns an invalid operation exception. If the rounded value is in
1722 range but the floating point number is not the exact integer, the C
1723 standard doesn't require an inexact exception to be raised. IEEE
1724 854 does require it so we do that.
1725
1726 Note that for conversions to integer type the C standard requires
1727 round-to-zero to always be used. */
1728APFloat::opStatus
1729APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth4f881702007-09-26 21:33:42 +00001730 bool isSigned,
1731 roundingMode rounding_mode) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001732{
1733 lostFraction lost_fraction;
1734 unsigned int msb, partsCount;
1735 int bits;
1736
Neil Boothcaf19d72007-10-14 10:29:28 +00001737 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001738 partsCount = partCountForBits(width);
1739
Dale Johannesen0edc47a2007-09-25 23:07:07 +00001740 /* Handle the three special cases first. We produce
1741 a deterministic result even for the Invalid cases. */
1742 if (category == fcNaN) {
1743 // Neither sign nor isSigned affects this.
1744 APInt::tcSet(parts, 0, partsCount);
1745 return opInvalidOp;
1746 }
1747 if (category == fcInfinity) {
1748 if (!sign && isSigned)
1749 APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
1750 else if (!sign && !isSigned)
1751 APInt::tcSetLeastSignificantBits(parts, partsCount, width);
1752 else if (sign && isSigned) {
1753 APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
1754 APInt::tcShiftLeft(parts, partsCount, width-1);
1755 } else // sign && !isSigned
1756 APInt::tcSet(parts, 0, partsCount);
1757 return opInvalidOp;
1758 }
1759 if (category == fcZero) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001760 APInt::tcSet(parts, 0, partsCount);
1761 return opOK;
1762 }
1763
1764 /* Shift the bit pattern so the fraction is lost. */
1765 APFloat tmp(*this);
1766
1767 bits = (int) semantics->precision - 1 - exponent;
1768
1769 if(bits > 0) {
1770 lost_fraction = tmp.shiftSignificandRight(bits);
1771 } else {
Dale Johannesen0edc47a2007-09-25 23:07:07 +00001772 if (-bits >= semantics->precision) {
1773 // Unrepresentably large.
1774 if (!sign && isSigned)
1775 APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
1776 else if (!sign && !isSigned)
1777 APInt::tcSetLeastSignificantBits(parts, partsCount, width);
1778 else if (sign && isSigned) {
1779 APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
1780 APInt::tcShiftLeft(parts, partsCount, width-1);
1781 } else // sign && !isSigned
1782 APInt::tcSet(parts, 0, partsCount);
1783 return (opStatus)(opOverflow | opInexact);
1784 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001785 tmp.shiftSignificandLeft(-bits);
1786 lost_fraction = lfExactlyZero;
1787 }
1788
1789 if(lost_fraction != lfExactlyZero
Neil Boothb7dea4c2007-10-03 15:16:41 +00001790 && tmp.roundAwayFromZero(rounding_mode, lost_fraction, 0))
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001791 tmp.incrementSignificand();
1792
1793 msb = tmp.significandMSB();
1794
1795 /* Negative numbers cannot be represented as unsigned. */
1796 if(!isSigned && tmp.sign && msb != -1U)
1797 return opInvalidOp;
1798
1799 /* It takes exponent + 1 bits to represent the truncated floating
1800 point number without its sign. We lose a bit for the sign, but
1801 the maximally negative integer is a special case. */
Neil Booth4f881702007-09-26 21:33:42 +00001802 if(msb + 1 > width) /* !! Not same as msb >= width !! */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001803 return opInvalidOp;
1804
1805 if(isSigned && msb + 1 == width
1806 && (!tmp.sign || tmp.significandLSB() != msb))
1807 return opInvalidOp;
1808
1809 APInt::tcAssign(parts, tmp.significandParts(), partsCount);
1810
1811 if(tmp.sign)
1812 APInt::tcNegate(parts, partsCount);
1813
1814 if(lost_fraction == lfExactlyZero)
1815 return opOK;
1816 else
1817 return opInexact;
1818}
1819
Neil Booth643ce592007-10-07 12:07:53 +00001820/* Convert an unsigned integer SRC to a floating point number,
1821 rounding according to ROUNDING_MODE. The sign of the floating
1822 point number is not modified. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001823APFloat::opStatus
Neil Booth643ce592007-10-07 12:07:53 +00001824APFloat::convertFromUnsignedParts(const integerPart *src,
1825 unsigned int srcCount,
1826 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001827{
Neil Booth5477f852007-10-08 14:39:42 +00001828 unsigned int omsb, precision, dstCount;
Neil Booth643ce592007-10-07 12:07:53 +00001829 integerPart *dst;
Neil Booth5477f852007-10-08 14:39:42 +00001830 lostFraction lost_fraction;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001831
Neil Boothcaf19d72007-10-14 10:29:28 +00001832 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001833 category = fcNormal;
Neil Booth5477f852007-10-08 14:39:42 +00001834 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth643ce592007-10-07 12:07:53 +00001835 dst = significandParts();
1836 dstCount = partCount();
Neil Booth5477f852007-10-08 14:39:42 +00001837 precision = semantics->precision;
Neil Booth643ce592007-10-07 12:07:53 +00001838
Neil Booth5477f852007-10-08 14:39:42 +00001839 /* We want the most significant PRECISON bits of SRC. There may not
1840 be that many; extract what we can. */
1841 if (precision <= omsb) {
1842 exponent = omsb - 1;
Neil Booth643ce592007-10-07 12:07:53 +00001843 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth5477f852007-10-08 14:39:42 +00001844 omsb - precision);
1845 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
1846 } else {
1847 exponent = precision - 1;
1848 lost_fraction = lfExactlyZero;
1849 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth643ce592007-10-07 12:07:53 +00001850 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001851
1852 return normalize(rounding_mode, lost_fraction);
1853}
1854
Neil Boothf16c5952007-10-07 12:15:41 +00001855/* Convert a two's complement integer SRC to a floating point number,
1856 rounding according to ROUNDING_MODE. ISSIGNED is true if the
1857 integer is signed, in which case it must be sign-extended. */
1858APFloat::opStatus
1859APFloat::convertFromSignExtendedInteger(const integerPart *src,
1860 unsigned int srcCount,
1861 bool isSigned,
1862 roundingMode rounding_mode)
1863{
1864 opStatus status;
1865
Neil Boothcaf19d72007-10-14 10:29:28 +00001866 assertArithmeticOK(*semantics);
Neil Boothf16c5952007-10-07 12:15:41 +00001867 if (isSigned
1868 && APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
1869 integerPart *copy;
1870
1871 /* If we're signed and negative negate a copy. */
1872 sign = true;
1873 copy = new integerPart[srcCount];
1874 APInt::tcAssign(copy, src, srcCount);
1875 APInt::tcNegate(copy, srcCount);
1876 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
1877 delete [] copy;
1878 } else {
1879 sign = false;
1880 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
1881 }
1882
1883 return status;
1884}
1885
Neil Boothccf596a2007-10-07 11:45:55 +00001886/* FIXME: should this just take a const APInt reference? */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001887APFloat::opStatus
Neil Boothccf596a2007-10-07 11:45:55 +00001888APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
1889 unsigned int width, bool isSigned,
1890 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001891{
Dale Johannesen910993e2007-09-21 22:09:37 +00001892 unsigned int partCount = partCountForBits(width);
Dale Johannesen910993e2007-09-21 22:09:37 +00001893 APInt api = APInt(width, partCount, parts);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001894
1895 sign = false;
Dale Johannesencce23a42007-09-30 18:17:01 +00001896 if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
1897 sign = true;
1898 api = -api;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001899 }
1900
Neil Booth7a7bc0f2007-10-07 12:10:57 +00001901 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001902}
1903
1904APFloat::opStatus
1905APFloat::convertFromHexadecimalString(const char *p,
Neil Booth4f881702007-09-26 21:33:42 +00001906 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001907{
1908 lostFraction lost_fraction;
1909 integerPart *significand;
1910 unsigned int bitPos, partsCount;
1911 const char *dot, *firstSignificantDigit;
1912
1913 zeroSignificand();
1914 exponent = 0;
1915 category = fcNormal;
1916
1917 significand = significandParts();
1918 partsCount = partCount();
1919 bitPos = partsCount * integerPartWidth;
1920
Neil Booth33d4c922007-10-07 08:51:21 +00001921 /* Skip leading zeroes and any (hexa)decimal point. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001922 p = skipLeadingZeroesAndAnyDot(p, &dot);
1923 firstSignificantDigit = p;
1924
1925 for(;;) {
1926 integerPart hex_value;
1927
1928 if(*p == '.') {
1929 assert(dot == 0);
1930 dot = p++;
1931 }
1932
1933 hex_value = hexDigitValue(*p);
1934 if(hex_value == -1U) {
1935 lost_fraction = lfExactlyZero;
1936 break;
1937 }
1938
1939 p++;
1940
1941 /* Store the number whilst 4-bit nibbles remain. */
1942 if(bitPos) {
1943 bitPos -= 4;
1944 hex_value <<= bitPos % integerPartWidth;
1945 significand[bitPos / integerPartWidth] |= hex_value;
1946 } else {
1947 lost_fraction = trailingHexadecimalFraction(p, hex_value);
1948 while(hexDigitValue(*p) != -1U)
Neil Booth4f881702007-09-26 21:33:42 +00001949 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001950 break;
1951 }
1952 }
1953
1954 /* Hex floats require an exponent but not a hexadecimal point. */
1955 assert(*p == 'p' || *p == 'P');
1956
1957 /* Ignore the exponent if we are zero. */
1958 if(p != firstSignificantDigit) {
1959 int expAdjustment;
1960
1961 /* Implicit hexadecimal point? */
1962 if(!dot)
1963 dot = p;
1964
1965 /* Calculate the exponent adjustment implicit in the number of
1966 significant digits. */
1967 expAdjustment = dot - firstSignificantDigit;
1968 if(expAdjustment < 0)
1969 expAdjustment++;
1970 expAdjustment = expAdjustment * 4 - 1;
1971
1972 /* Adjust for writing the significand starting at the most
1973 significant nibble. */
1974 expAdjustment += semantics->precision;
1975 expAdjustment -= partsCount * integerPartWidth;
1976
1977 /* Adjust for the given exponent. */
1978 exponent = totalExponent(p, expAdjustment);
1979 }
1980
1981 return normalize(rounding_mode, lost_fraction);
1982}
1983
1984APFloat::opStatus
Neil Booth96c74712007-10-12 16:02:31 +00001985APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
1986 unsigned sigPartCount, int exp,
1987 roundingMode rounding_mode)
1988{
1989 unsigned int parts, pow5PartCount;
Neil Boothcaf19d72007-10-14 10:29:28 +00001990 fltSemantics calcSemantics = { 32767, -32767, 0, true };
Neil Booth96c74712007-10-12 16:02:31 +00001991 integerPart pow5Parts[maxPowerOfFiveParts];
1992 bool isNearest;
1993
1994 isNearest = (rounding_mode == rmNearestTiesToEven
1995 || rounding_mode == rmNearestTiesToAway);
1996
1997 parts = partCountForBits(semantics->precision + 11);
1998
1999 /* Calculate pow(5, abs(exp)). */
2000 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2001
2002 for (;; parts *= 2) {
2003 opStatus sigStatus, powStatus;
2004 unsigned int excessPrecision, truncatedBits;
2005
2006 calcSemantics.precision = parts * integerPartWidth - 1;
2007 excessPrecision = calcSemantics.precision - semantics->precision;
2008 truncatedBits = excessPrecision;
2009
2010 APFloat decSig(calcSemantics, fcZero, sign);
2011 APFloat pow5(calcSemantics, fcZero, false);
2012
2013 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2014 rmNearestTiesToEven);
2015 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2016 rmNearestTiesToEven);
2017 /* Add exp, as 10^n = 5^n * 2^n. */
2018 decSig.exponent += exp;
2019
2020 lostFraction calcLostFraction;
2021 integerPart HUerr, HUdistance, powHUerr;
2022
2023 if (exp >= 0) {
2024 /* multiplySignificand leaves the precision-th bit set to 1. */
2025 calcLostFraction = decSig.multiplySignificand(pow5, NULL);
2026 powHUerr = powStatus != opOK;
2027 } else {
2028 calcLostFraction = decSig.divideSignificand(pow5);
2029 /* Denormal numbers have less precision. */
2030 if (decSig.exponent < semantics->minExponent) {
2031 excessPrecision += (semantics->minExponent - decSig.exponent);
2032 truncatedBits = excessPrecision;
2033 if (excessPrecision > calcSemantics.precision)
2034 excessPrecision = calcSemantics.precision;
2035 }
2036 /* Extra half-ulp lost in reciprocal of exponent. */
Neil Boothd1a23d52007-10-13 03:34:08 +00002037 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0: 2;
Neil Booth96c74712007-10-12 16:02:31 +00002038 }
2039
2040 /* Both multiplySignificand and divideSignificand return the
2041 result with the integer bit set. */
2042 assert (APInt::tcExtractBit
2043 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2044
2045 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2046 powHUerr);
2047 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2048 excessPrecision, isNearest);
2049
2050 /* Are we guaranteed to round correctly if we truncate? */
2051 if (HUdistance >= HUerr) {
2052 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2053 calcSemantics.precision - excessPrecision,
2054 excessPrecision);
2055 /* Take the exponent of decSig. If we tcExtract-ed less bits
2056 above we must adjust our exponent to compensate for the
2057 implicit right shift. */
2058 exponent = (decSig.exponent + semantics->precision
2059 - (calcSemantics.precision - excessPrecision));
2060 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2061 decSig.partCount(),
2062 truncatedBits);
2063 return normalize(rounding_mode, calcLostFraction);
2064 }
2065 }
2066}
2067
2068APFloat::opStatus
2069APFloat::convertFromDecimalString(const char *p, roundingMode rounding_mode)
2070{
Neil Booth1870f292007-10-14 10:16:12 +00002071 decimalInfo D;
Neil Booth96c74712007-10-12 16:02:31 +00002072 opStatus fs;
2073
Neil Booth1870f292007-10-14 10:16:12 +00002074 /* Scan the text. */
2075 interpretDecimal(p, &D);
Neil Booth96c74712007-10-12 16:02:31 +00002076
Neil Booth1870f292007-10-14 10:16:12 +00002077 if (*D.firstSigDigit == '0') {
Neil Booth96c74712007-10-12 16:02:31 +00002078 category = fcZero;
2079 fs = opOK;
2080 } else {
Neil Booth1870f292007-10-14 10:16:12 +00002081 integerPart *decSignificand;
2082 unsigned int partCount;
Neil Booth96c74712007-10-12 16:02:31 +00002083
Neil Booth1870f292007-10-14 10:16:12 +00002084 /* A tight upper bound on number of bits required to hold an
2085 N-digit decimal integer is N * 256 / 77. Allocate enough space
2086 to hold the full significand, and an extra part required by
2087 tcMultiplyPart. */
2088 partCount = (D.lastSigDigit - D.firstSigDigit) + 1;
2089 partCount = partCountForBits(1 + 256 * partCount / 77);
2090 decSignificand = new integerPart[partCount + 1];
2091 partCount = 0;
Neil Booth96c74712007-10-12 16:02:31 +00002092
Neil Booth1870f292007-10-14 10:16:12 +00002093 /* Convert to binary efficiently - we do almost all multiplication
2094 in an integerPart. When this would overflow do we do a single
2095 bignum multiplication, and then revert again to multiplication
2096 in an integerPart. */
2097 do {
2098 integerPart decValue, val, multiplier;
2099
2100 val = 0;
2101 multiplier = 1;
2102
2103 do {
2104 if (*p == '.')
2105 p++;
2106
2107 decValue = decDigitValue(*p++);
2108 multiplier *= 10;
2109 val = val * 10 + decValue;
2110 /* The maximum number that can be multiplied by ten with any
2111 digit added without overflowing an integerPart. */
2112 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2113
2114 /* Multiply out the current part. */
2115 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2116 partCount, partCount + 1, false);
2117
2118 /* If we used another part (likely but not guaranteed), increase
2119 the count. */
2120 if (decSignificand[partCount])
2121 partCount++;
2122 } while (p <= D.lastSigDigit);
Neil Booth96c74712007-10-12 16:02:31 +00002123
2124 category = fcNormal;
2125 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth1870f292007-10-14 10:16:12 +00002126 D.exponent, rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002127
Neil Booth1870f292007-10-14 10:16:12 +00002128 delete [] decSignificand;
2129 }
Neil Booth96c74712007-10-12 16:02:31 +00002130
2131 return fs;
2132}
2133
2134APFloat::opStatus
Neil Booth4f881702007-09-26 21:33:42 +00002135APFloat::convertFromString(const char *p, roundingMode rounding_mode)
2136{
Neil Boothcaf19d72007-10-14 10:29:28 +00002137 assertArithmeticOK(*semantics);
2138
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002139 /* Handle a leading minus sign. */
2140 if(*p == '-')
2141 sign = 1, p++;
2142 else
2143 sign = 0;
2144
2145 if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
2146 return convertFromHexadecimalString(p + 2, rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002147 else
2148 return convertFromDecimalString(p, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002149}
Dale Johannesen343e7702007-08-24 00:56:33 +00002150
Neil Bootha30b0ee2007-10-03 22:26:02 +00002151/* Write out a hexadecimal representation of the floating point value
2152 to DST, which must be of sufficient size, in the C99 form
2153 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2154 excluding the terminating NUL.
2155
2156 If UPPERCASE, the output is in upper case, otherwise in lower case.
2157
2158 HEXDIGITS digits appear altogether, rounding the value if
2159 necessary. If HEXDIGITS is 0, the minimal precision to display the
2160 number precisely is used instead. If nothing would appear after
2161 the decimal point it is suppressed.
2162
2163 The decimal exponent is always printed and has at least one digit.
2164 Zero values display an exponent of zero. Infinities and NaNs
2165 appear as "infinity" or "nan" respectively.
2166
2167 The above rules are as specified by C99. There is ambiguity about
2168 what the leading hexadecimal digit should be. This implementation
2169 uses whatever is necessary so that the exponent is displayed as
2170 stored. This implies the exponent will fall within the IEEE format
2171 range, and the leading hexadecimal digit will be 0 (for denormals),
2172 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2173 any other digits zero).
2174*/
2175unsigned int
2176APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2177 bool upperCase, roundingMode rounding_mode) const
2178{
2179 char *p;
2180
Neil Boothcaf19d72007-10-14 10:29:28 +00002181 assertArithmeticOK(*semantics);
2182
Neil Bootha30b0ee2007-10-03 22:26:02 +00002183 p = dst;
2184 if (sign)
2185 *dst++ = '-';
2186
2187 switch (category) {
2188 case fcInfinity:
2189 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2190 dst += sizeof infinityL - 1;
2191 break;
2192
2193 case fcNaN:
2194 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2195 dst += sizeof NaNU - 1;
2196 break;
2197
2198 case fcZero:
2199 *dst++ = '0';
2200 *dst++ = upperCase ? 'X': 'x';
2201 *dst++ = '0';
2202 if (hexDigits > 1) {
2203 *dst++ = '.';
2204 memset (dst, '0', hexDigits - 1);
2205 dst += hexDigits - 1;
2206 }
2207 *dst++ = upperCase ? 'P': 'p';
2208 *dst++ = '0';
2209 break;
2210
2211 case fcNormal:
2212 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2213 break;
2214 }
2215
2216 *dst = 0;
2217
2218 return dst - p;
2219}
2220
2221/* Does the hard work of outputting the correctly rounded hexadecimal
2222 form of a normal floating point number with the specified number of
2223 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2224 digits necessary to print the value precisely is output. */
2225char *
2226APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2227 bool upperCase,
2228 roundingMode rounding_mode) const
2229{
2230 unsigned int count, valueBits, shift, partsCount, outputDigits;
2231 const char *hexDigitChars;
2232 const integerPart *significand;
2233 char *p;
2234 bool roundUp;
2235
2236 *dst++ = '0';
2237 *dst++ = upperCase ? 'X': 'x';
2238
2239 roundUp = false;
2240 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2241
2242 significand = significandParts();
2243 partsCount = partCount();
2244
2245 /* +3 because the first digit only uses the single integer bit, so
2246 we have 3 virtual zero most-significant-bits. */
2247 valueBits = semantics->precision + 3;
2248 shift = integerPartWidth - valueBits % integerPartWidth;
2249
2250 /* The natural number of digits required ignoring trailing
2251 insignificant zeroes. */
2252 outputDigits = (valueBits - significandLSB () + 3) / 4;
2253
2254 /* hexDigits of zero means use the required number for the
2255 precision. Otherwise, see if we are truncating. If we are,
Neil Booth978661d2007-10-06 00:24:48 +00002256 find out if we need to round away from zero. */
Neil Bootha30b0ee2007-10-03 22:26:02 +00002257 if (hexDigits) {
2258 if (hexDigits < outputDigits) {
2259 /* We are dropping non-zero bits, so need to check how to round.
2260 "bits" is the number of dropped bits. */
2261 unsigned int bits;
2262 lostFraction fraction;
2263
2264 bits = valueBits - hexDigits * 4;
2265 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2266 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2267 }
2268 outputDigits = hexDigits;
2269 }
2270
2271 /* Write the digits consecutively, and start writing in the location
2272 of the hexadecimal point. We move the most significant digit
2273 left and add the hexadecimal point later. */
2274 p = ++dst;
2275
2276 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2277
2278 while (outputDigits && count) {
2279 integerPart part;
2280
2281 /* Put the most significant integerPartWidth bits in "part". */
2282 if (--count == partsCount)
2283 part = 0; /* An imaginary higher zero part. */
2284 else
2285 part = significand[count] << shift;
2286
2287 if (count && shift)
2288 part |= significand[count - 1] >> (integerPartWidth - shift);
2289
2290 /* Convert as much of "part" to hexdigits as we can. */
2291 unsigned int curDigits = integerPartWidth / 4;
2292
2293 if (curDigits > outputDigits)
2294 curDigits = outputDigits;
2295 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2296 outputDigits -= curDigits;
2297 }
2298
2299 if (roundUp) {
2300 char *q = dst;
2301
2302 /* Note that hexDigitChars has a trailing '0'. */
2303 do {
2304 q--;
2305 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth978661d2007-10-06 00:24:48 +00002306 } while (*q == '0');
2307 assert (q >= p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002308 } else {
2309 /* Add trailing zeroes. */
2310 memset (dst, '0', outputDigits);
2311 dst += outputDigits;
2312 }
2313
2314 /* Move the most significant digit to before the point, and if there
2315 is something after the decimal point add it. This must come
2316 after rounding above. */
2317 p[-1] = p[0];
2318 if (dst -1 == p)
2319 dst--;
2320 else
2321 p[0] = '.';
2322
2323 /* Finally output the exponent. */
2324 *dst++ = upperCase ? 'P': 'p';
2325
Neil Booth92f7e8d2007-10-06 07:29:25 +00002326 return writeSignedDecimal (dst, exponent);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002327}
2328
Dale Johannesen343e7702007-08-24 00:56:33 +00002329// For good performance it is desirable for different APFloats
2330// to produce different integers.
2331uint32_t
Neil Booth4f881702007-09-26 21:33:42 +00002332APFloat::getHashValue() const
2333{
Dale Johannesen343e7702007-08-24 00:56:33 +00002334 if (category==fcZero) return sign<<8 | semantics->precision ;
2335 else if (category==fcInfinity) return sign<<9 | semantics->precision;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002336 else if (category==fcNaN) return 1<<10 | semantics->precision;
Dale Johannesen343e7702007-08-24 00:56:33 +00002337 else {
2338 uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
2339 const integerPart* p = significandParts();
2340 for (int i=partCount(); i>0; i--, p++)
2341 hash ^= ((uint32_t)*p) ^ (*p)>>32;
2342 return hash;
2343 }
2344}
2345
2346// Conversion from APFloat to/from host float/double. It may eventually be
2347// possible to eliminate these and have everybody deal with APFloats, but that
2348// will take a while. This approach will not easily extend to long double.
Dale Johannesena72a5a02007-09-20 23:47:58 +00002349// Current implementation requires integerPartWidth==64, which is correct at
2350// the moment but could be made more general.
Dale Johannesen343e7702007-08-24 00:56:33 +00002351
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002352// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesena72a5a02007-09-20 23:47:58 +00002353// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002354
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002355APInt
Neil Booth4f881702007-09-26 21:33:42 +00002356APFloat::convertF80LongDoubleAPFloatToAPInt() const
2357{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002358 assert(semantics == (const llvm::fltSemantics* const)&x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002359 assert (partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002360
2361 uint64_t myexponent, mysignificand;
2362
2363 if (category==fcNormal) {
2364 myexponent = exponent+16383; //bias
Dale Johannesena72a5a02007-09-20 23:47:58 +00002365 mysignificand = significandParts()[0];
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002366 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2367 myexponent = 0; // denormal
2368 } else if (category==fcZero) {
2369 myexponent = 0;
2370 mysignificand = 0;
2371 } else if (category==fcInfinity) {
2372 myexponent = 0x7fff;
2373 mysignificand = 0x8000000000000000ULL;
Chris Lattnera11ef822007-10-06 06:13:42 +00002374 } else {
2375 assert(category == fcNaN && "Unknown category");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002376 myexponent = 0x7fff;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002377 mysignificand = significandParts()[0];
Chris Lattnera11ef822007-10-06 06:13:42 +00002378 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002379
2380 uint64_t words[2];
Neil Booth4f881702007-09-26 21:33:42 +00002381 words[0] = (((uint64_t)sign & 1) << 63) |
2382 ((myexponent & 0x7fff) << 48) |
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002383 ((mysignificand >>16) & 0xffffffffffffLL);
2384 words[1] = mysignificand & 0xffff;
Chris Lattnera11ef822007-10-06 06:13:42 +00002385 return APInt(80, 2, words);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002386}
2387
2388APInt
Dale Johannesena471c2e2007-10-11 18:07:22 +00002389APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2390{
2391 assert(semantics == (const llvm::fltSemantics* const)&PPCDoubleDouble);
2392 assert (partCount()==2);
2393
2394 uint64_t myexponent, mysignificand, myexponent2, mysignificand2;
2395
2396 if (category==fcNormal) {
2397 myexponent = exponent + 1023; //bias
2398 myexponent2 = exponent2 + 1023;
2399 mysignificand = significandParts()[0];
2400 mysignificand2 = significandParts()[1];
2401 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2402 myexponent = 0; // denormal
2403 if (myexponent2==1 && !(mysignificand2 & 0x10000000000000LL))
2404 myexponent2 = 0; // denormal
2405 } else if (category==fcZero) {
2406 myexponent = 0;
2407 mysignificand = 0;
2408 myexponent2 = 0;
2409 mysignificand2 = 0;
2410 } else if (category==fcInfinity) {
2411 myexponent = 0x7ff;
2412 myexponent2 = 0;
2413 mysignificand = 0;
2414 mysignificand2 = 0;
2415 } else {
2416 assert(category == fcNaN && "Unknown category");
2417 myexponent = 0x7ff;
2418 mysignificand = significandParts()[0];
2419 myexponent2 = exponent2;
2420 mysignificand2 = significandParts()[1];
2421 }
2422
2423 uint64_t words[2];
2424 words[0] = (((uint64_t)sign & 1) << 63) |
2425 ((myexponent & 0x7ff) << 52) |
2426 (mysignificand & 0xfffffffffffffLL);
2427 words[1] = (((uint64_t)sign2 & 1) << 63) |
2428 ((myexponent2 & 0x7ff) << 52) |
2429 (mysignificand2 & 0xfffffffffffffLL);
2430 return APInt(128, 2, words);
2431}
2432
2433APInt
Neil Booth4f881702007-09-26 21:33:42 +00002434APFloat::convertDoubleAPFloatToAPInt() const
2435{
Dan Gohmancb648f92007-09-14 20:08:19 +00002436 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002437 assert (partCount()==1);
2438
Dale Johanneseneaf08942007-08-31 04:03:46 +00002439 uint64_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002440
2441 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002442 myexponent = exponent+1023; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002443 mysignificand = *significandParts();
2444 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2445 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002446 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002447 myexponent = 0;
2448 mysignificand = 0;
2449 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002450 myexponent = 0x7ff;
2451 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002452 } else {
2453 assert(category == fcNaN && "Unknown category!");
Dale Johannesen343e7702007-08-24 00:56:33 +00002454 myexponent = 0x7ff;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002455 mysignificand = *significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002456 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002457
Chris Lattnera11ef822007-10-06 06:13:42 +00002458 return APInt(64, (((((uint64_t)sign & 1) << 63) |
2459 ((myexponent & 0x7ff) << 52) |
2460 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesen343e7702007-08-24 00:56:33 +00002461}
2462
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002463APInt
Neil Booth4f881702007-09-26 21:33:42 +00002464APFloat::convertFloatAPFloatToAPInt() const
2465{
Dan Gohmancb648f92007-09-14 20:08:19 +00002466 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002467 assert (partCount()==1);
Neil Booth4f881702007-09-26 21:33:42 +00002468
Dale Johanneseneaf08942007-08-31 04:03:46 +00002469 uint32_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002470
2471 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002472 myexponent = exponent+127; //bias
2473 mysignificand = *significandParts();
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002474 if (myexponent == 1 && !(mysignificand & 0x400000))
2475 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002476 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002477 myexponent = 0;
2478 mysignificand = 0;
2479 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002480 myexponent = 0xff;
2481 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002482 } else {
2483 assert(category == fcNaN && "Unknown category!");
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002484 myexponent = 0xff;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002485 mysignificand = *significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002486 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002487
Chris Lattnera11ef822007-10-06 06:13:42 +00002488 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2489 (mysignificand & 0x7fffff)));
Dale Johannesen343e7702007-08-24 00:56:33 +00002490}
2491
Dale Johannesena471c2e2007-10-11 18:07:22 +00002492// This function creates an APInt that is just a bit map of the floating
2493// point constant as it would appear in memory. It is not a conversion,
2494// and treating the result as a normal integer is unlikely to be useful.
2495
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002496APInt
Neil Booth4f881702007-09-26 21:33:42 +00002497APFloat::convertToAPInt() const
2498{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002499 if (semantics == (const llvm::fltSemantics* const)&IEEEsingle)
2500 return convertFloatAPFloatToAPInt();
Chris Lattnera11ef822007-10-06 06:13:42 +00002501
2502 if (semantics == (const llvm::fltSemantics* const)&IEEEdouble)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002503 return convertDoubleAPFloatToAPInt();
Neil Booth4f881702007-09-26 21:33:42 +00002504
Dale Johannesena471c2e2007-10-11 18:07:22 +00002505 if (semantics == (const llvm::fltSemantics* const)&PPCDoubleDouble)
2506 return convertPPCDoubleDoubleAPFloatToAPInt();
2507
Chris Lattnera11ef822007-10-06 06:13:42 +00002508 assert(semantics == (const llvm::fltSemantics* const)&x87DoubleExtended &&
2509 "unknown format!");
2510 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002511}
2512
Neil Booth4f881702007-09-26 21:33:42 +00002513float
2514APFloat::convertToFloat() const
2515{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002516 assert(semantics == (const llvm::fltSemantics* const)&IEEEsingle);
2517 APInt api = convertToAPInt();
2518 return api.bitsToFloat();
2519}
2520
Neil Booth4f881702007-09-26 21:33:42 +00002521double
2522APFloat::convertToDouble() const
2523{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002524 assert(semantics == (const llvm::fltSemantics* const)&IEEEdouble);
2525 APInt api = convertToAPInt();
2526 return api.bitsToDouble();
2527}
2528
2529/// Integer bit is explicit in this format. Current Intel book does not
2530/// define meaning of:
2531/// exponent = all 1's, integer bit not set.
2532/// exponent = 0, integer bit set. (formerly "psuedodenormals")
2533/// exponent!=0 nor all 1's, integer bit not set. (formerly "unnormals")
2534void
Neil Booth4f881702007-09-26 21:33:42 +00002535APFloat::initFromF80LongDoubleAPInt(const APInt &api)
2536{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002537 assert(api.getBitWidth()==80);
2538 uint64_t i1 = api.getRawData()[0];
2539 uint64_t i2 = api.getRawData()[1];
2540 uint64_t myexponent = (i1 >> 48) & 0x7fff;
2541 uint64_t mysignificand = ((i1 << 16) & 0xffffffffffff0000ULL) |
2542 (i2 & 0xffff);
2543
2544 initialize(&APFloat::x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002545 assert(partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002546
2547 sign = i1>>63;
2548 if (myexponent==0 && mysignificand==0) {
2549 // exponent, significand meaningless
2550 category = fcZero;
2551 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
2552 // exponent, significand meaningless
2553 category = fcInfinity;
2554 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
2555 // exponent meaningless
2556 category = fcNaN;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002557 significandParts()[0] = mysignificand;
2558 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002559 } else {
2560 category = fcNormal;
2561 exponent = myexponent - 16383;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002562 significandParts()[0] = mysignificand;
2563 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002564 if (myexponent==0) // denormal
2565 exponent = -16382;
Neil Booth4f881702007-09-26 21:33:42 +00002566 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002567}
2568
2569void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002570APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
2571{
2572 assert(api.getBitWidth()==128);
2573 uint64_t i1 = api.getRawData()[0];
2574 uint64_t i2 = api.getRawData()[1];
2575 uint64_t myexponent = (i1 >> 52) & 0x7ff;
2576 uint64_t mysignificand = i1 & 0xfffffffffffffLL;
2577 uint64_t myexponent2 = (i2 >> 52) & 0x7ff;
2578 uint64_t mysignificand2 = i2 & 0xfffffffffffffLL;
2579
2580 initialize(&APFloat::PPCDoubleDouble);
2581 assert(partCount()==2);
2582
2583 sign = i1>>63;
2584 sign2 = i2>>63;
2585 if (myexponent==0 && mysignificand==0) {
2586 // exponent, significand meaningless
2587 // exponent2 and significand2 are required to be 0; we don't check
2588 category = fcZero;
2589 } else if (myexponent==0x7ff && mysignificand==0) {
2590 // exponent, significand meaningless
2591 // exponent2 and significand2 are required to be 0; we don't check
2592 category = fcInfinity;
2593 } else if (myexponent==0x7ff && mysignificand!=0) {
2594 // exponent meaningless. So is the whole second word, but keep it
2595 // for determinism.
2596 category = fcNaN;
2597 exponent2 = myexponent2;
2598 significandParts()[0] = mysignificand;
2599 significandParts()[1] = mysignificand2;
2600 } else {
2601 category = fcNormal;
2602 // Note there is no category2; the second word is treated as if it is
2603 // fcNormal, although it might be something else considered by itself.
2604 exponent = myexponent - 1023;
2605 exponent2 = myexponent2 - 1023;
2606 significandParts()[0] = mysignificand;
2607 significandParts()[1] = mysignificand2;
2608 if (myexponent==0) // denormal
2609 exponent = -1022;
2610 else
2611 significandParts()[0] |= 0x10000000000000LL; // integer bit
2612 if (myexponent2==0)
2613 exponent2 = -1022;
2614 else
2615 significandParts()[1] |= 0x10000000000000LL; // integer bit
2616 }
2617}
2618
2619void
Neil Booth4f881702007-09-26 21:33:42 +00002620APFloat::initFromDoubleAPInt(const APInt &api)
2621{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002622 assert(api.getBitWidth()==64);
2623 uint64_t i = *api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002624 uint64_t myexponent = (i >> 52) & 0x7ff;
2625 uint64_t mysignificand = i & 0xfffffffffffffLL;
2626
Dale Johannesen343e7702007-08-24 00:56:33 +00002627 initialize(&APFloat::IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002628 assert(partCount()==1);
2629
Dale Johanneseneaf08942007-08-31 04:03:46 +00002630 sign = i>>63;
Dale Johannesen343e7702007-08-24 00:56:33 +00002631 if (myexponent==0 && mysignificand==0) {
2632 // exponent, significand meaningless
2633 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002634 } else if (myexponent==0x7ff && mysignificand==0) {
2635 // exponent, significand meaningless
2636 category = fcInfinity;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002637 } else if (myexponent==0x7ff && mysignificand!=0) {
2638 // exponent meaningless
2639 category = fcNaN;
2640 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002641 } else {
Dale Johannesen343e7702007-08-24 00:56:33 +00002642 category = fcNormal;
2643 exponent = myexponent - 1023;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002644 *significandParts() = mysignificand;
2645 if (myexponent==0) // denormal
2646 exponent = -1022;
2647 else
2648 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth4f881702007-09-26 21:33:42 +00002649 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002650}
2651
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002652void
Neil Booth4f881702007-09-26 21:33:42 +00002653APFloat::initFromFloatAPInt(const APInt & api)
2654{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002655 assert(api.getBitWidth()==32);
2656 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002657 uint32_t myexponent = (i >> 23) & 0xff;
2658 uint32_t mysignificand = i & 0x7fffff;
2659
Dale Johannesen343e7702007-08-24 00:56:33 +00002660 initialize(&APFloat::IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002661 assert(partCount()==1);
2662
Dale Johanneseneaf08942007-08-31 04:03:46 +00002663 sign = i >> 31;
Dale Johannesen343e7702007-08-24 00:56:33 +00002664 if (myexponent==0 && mysignificand==0) {
2665 // exponent, significand meaningless
2666 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002667 } else if (myexponent==0xff && mysignificand==0) {
2668 // exponent, significand meaningless
2669 category = fcInfinity;
Dale Johannesen902ff942007-09-25 17:25:00 +00002670 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002671 // sign, exponent, significand meaningless
Dale Johanneseneaf08942007-08-31 04:03:46 +00002672 category = fcNaN;
2673 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002674 } else {
2675 category = fcNormal;
Dale Johannesen343e7702007-08-24 00:56:33 +00002676 exponent = myexponent - 127; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002677 *significandParts() = mysignificand;
2678 if (myexponent==0) // denormal
2679 exponent = -126;
2680 else
2681 *significandParts() |= 0x800000; // integer bit
Dale Johannesen343e7702007-08-24 00:56:33 +00002682 }
2683}
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002684
2685/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesena471c2e2007-10-11 18:07:22 +00002686/// we infer the floating point type from the size of the APInt. The
2687/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
2688/// when the size is anything else).
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002689void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002690APFloat::initFromAPInt(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002691{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002692 if (api.getBitWidth() == 32)
2693 return initFromFloatAPInt(api);
2694 else if (api.getBitWidth()==64)
2695 return initFromDoubleAPInt(api);
2696 else if (api.getBitWidth()==80)
2697 return initFromF80LongDoubleAPInt(api);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002698 else if (api.getBitWidth()==128 && !isIEEE)
2699 return initFromPPCDoubleDoubleAPInt(api);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002700 else
2701 assert(0);
2702}
2703
Dale Johannesena471c2e2007-10-11 18:07:22 +00002704APFloat::APFloat(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002705{
Dale Johannesena471c2e2007-10-11 18:07:22 +00002706 initFromAPInt(api, isIEEE);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002707}
2708
Neil Booth4f881702007-09-26 21:33:42 +00002709APFloat::APFloat(float f)
2710{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002711 APInt api = APInt(32, 0);
2712 initFromAPInt(api.floatToBits(f));
2713}
2714
Neil Booth4f881702007-09-26 21:33:42 +00002715APFloat::APFloat(double d)
2716{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002717 APInt api = APInt(64, 0);
2718 initFromAPInt(api.doubleToBits(d));
2719}