blob: a22ec15512fd0b65bcf0fcf6167a39e1f60b1d32 [file] [log] [blame]
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001//===-- APFloat.cpp - Implement APFloat class -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb39cdde2007-08-20 22:49:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a class to represent arbitrary precision floating
11// point values and provide a variety of arithmetic operations on them.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner36d26c22007-12-08 19:00:03 +000015#include "llvm/ADT/APFloat.h"
Erick Tryzelaara15d8902009-08-16 23:36:19 +000016#include "llvm/ADT/StringRef.h"
Ted Kremenek1f801fa2008-02-11 17:24:50 +000017#include "llvm/ADT/FoldingSet.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Dale Johannesend3b51fd2007-08-24 05:08:11 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000020#include <cstring>
Chris Lattnerb39cdde2007-08-20 22:49:32 +000021
22using namespace llvm;
23
24#define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
25
Neil Bootha30b0ee2007-10-03 22:26:02 +000026/* Assumed in hexadecimal significand parsing, and conversion to
27 hexadecimal strings. */
Chris Lattner9f17eb02008-08-17 04:58:58 +000028#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
Chris Lattnerb39cdde2007-08-20 22:49:32 +000029COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
30
31namespace llvm {
32
33 /* Represents floating point arithmetic semantics. */
34 struct fltSemantics {
35 /* The largest E such that 2^E is representable; this matches the
36 definition of IEEE 754. */
37 exponent_t maxExponent;
38
39 /* The smallest E such that 2^E is a normalized number; this
40 matches the definition of IEEE 754. */
41 exponent_t minExponent;
42
43 /* Number of bits in the significand. This includes the integer
44 bit. */
Neil Booth7a951ca2007-10-12 15:33:27 +000045 unsigned int precision;
Neil Boothcaf19d72007-10-14 10:29:28 +000046
47 /* True if arithmetic is supported. */
48 unsigned int arithmeticOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +000049 };
50
Neil Boothcaf19d72007-10-14 10:29:28 +000051 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
52 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
53 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
54 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, true };
55 const fltSemantics APFloat::Bogus = { 0, 0, 0, true };
Dale Johannesena471c2e2007-10-11 18:07:22 +000056
57 // The PowerPC format consists of two doubles. It does not map cleanly
58 // onto the usual format above. For now only storage of constants of
59 // this type is supported, no arithmetic.
Neil Boothcaf19d72007-10-14 10:29:28 +000060 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
Neil Booth96c74712007-10-12 16:02:31 +000061
62 /* A tight upper bound on number of parts required to hold the value
63 pow(5, power) is
64
Neil Booth686700e2007-10-15 15:00:55 +000065 power * 815 / (351 * integerPartWidth) + 1
Neil Booth96c74712007-10-12 16:02:31 +000066
67 However, whilst the result may require only this many parts,
68 because we are multiplying two values to get it, the
69 multiplication may require an extra part with the excess part
70 being zero (consider the trivial case of 1 * 1, tcFullMultiply
71 requires two parts to hold the single-part result). So we add an
72 extra one to guarantee enough space whilst multiplying. */
73 const unsigned int maxExponent = 16383;
74 const unsigned int maxPrecision = 113;
75 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
Neil Booth686700e2007-10-15 15:00:55 +000076 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
77 / (351 * integerPartWidth));
Chris Lattnerb39cdde2007-08-20 22:49:32 +000078}
79
Chris Lattnere213f3f2009-03-12 23:59:55 +000080/* A bunch of private, handy routines. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +000081
Chris Lattnere213f3f2009-03-12 23:59:55 +000082static inline unsigned int
83partCountForBits(unsigned int bits)
84{
85 return ((bits) + integerPartWidth - 1) / integerPartWidth;
86}
Chris Lattnerb39cdde2007-08-20 22:49:32 +000087
Chris Lattnere213f3f2009-03-12 23:59:55 +000088/* Returns 0U-9U. Return values >= 10U are not digits. */
89static inline unsigned int
90decDigitValue(unsigned int c)
91{
92 return c - '0';
93}
Chris Lattnerb39cdde2007-08-20 22:49:32 +000094
Chris Lattnere213f3f2009-03-12 23:59:55 +000095static unsigned int
96hexDigitValue(unsigned int c)
97{
98 unsigned int r;
Chris Lattnerb39cdde2007-08-20 22:49:32 +000099
Chris Lattnere213f3f2009-03-12 23:59:55 +0000100 r = c - '0';
101 if(r <= 9)
102 return r;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000103
Chris Lattnere213f3f2009-03-12 23:59:55 +0000104 r = c - 'A';
105 if(r <= 5)
106 return r + 10;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000107
Chris Lattnere213f3f2009-03-12 23:59:55 +0000108 r = c - 'a';
109 if(r <= 5)
110 return r + 10;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000111
Chris Lattnere213f3f2009-03-12 23:59:55 +0000112 return -1U;
113}
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000114
Chris Lattnere213f3f2009-03-12 23:59:55 +0000115static inline void
116assertArithmeticOK(const llvm::fltSemantics &semantics) {
117 assert(semantics.arithmeticOK
118 && "Compile-time arithmetic does not support these semantics");
119}
Neil Boothcaf19d72007-10-14 10:29:28 +0000120
Chris Lattnere213f3f2009-03-12 23:59:55 +0000121/* Return the value of a decimal exponent of the form
122 [+-]ddddddd.
Neil Booth1870f292007-10-14 10:16:12 +0000123
Chris Lattnere213f3f2009-03-12 23:59:55 +0000124 If the exponent overflows, returns a large exponent with the
125 appropriate sign. */
126static int
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000127readExponent(StringRef::iterator begin, StringRef::iterator end)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000128{
129 bool isNegative;
130 unsigned int absExponent;
131 const unsigned int overlargeExponent = 24000; /* FIXME. */
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000132 StringRef::iterator p = begin;
133
134 assert(p != end && "Exponent has no digits");
Neil Booth1870f292007-10-14 10:16:12 +0000135
Chris Lattnere213f3f2009-03-12 23:59:55 +0000136 isNegative = (*p == '-');
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000137 if (*p == '-' || *p == '+') {
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000138 p++;
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000139 assert(p != end && "Exponent has no digits");
140 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000141
Chris Lattnere213f3f2009-03-12 23:59:55 +0000142 absExponent = decDigitValue(*p++);
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000143 assert(absExponent < 10U && "Invalid character in exponent");
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000144
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000145 for (; p != end; ++p) {
Chris Lattnere213f3f2009-03-12 23:59:55 +0000146 unsigned int value;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000147
Chris Lattnere213f3f2009-03-12 23:59:55 +0000148 value = decDigitValue(*p);
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000149 assert(value < 10U && "Invalid character in exponent");
Chris Lattnere213f3f2009-03-12 23:59:55 +0000150
Chris Lattnere213f3f2009-03-12 23:59:55 +0000151 value += absExponent * 10;
152 if (absExponent >= overlargeExponent) {
153 absExponent = overlargeExponent;
154 break;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000155 }
Chris Lattnere213f3f2009-03-12 23:59:55 +0000156 absExponent = value;
157 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000158
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000159 assert(p == end && "Invalid exponent in exponent");
160
Chris Lattnere213f3f2009-03-12 23:59:55 +0000161 if (isNegative)
162 return -(int) absExponent;
163 else
164 return (int) absExponent;
165}
166
167/* This is ugly and needs cleaning up, but I don't immediately see
168 how whilst remaining safe. */
169static int
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000170totalExponent(StringRef::iterator p, StringRef::iterator end,
171 int exponentAdjustment)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000172{
173 int unsignedExponent;
174 bool negative, overflow;
175 int exponent;
176
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000177 assert(p != end && "Exponent has no digits");
178
Chris Lattnere213f3f2009-03-12 23:59:55 +0000179 negative = *p == '-';
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000180 if(*p == '-' || *p == '+') {
Chris Lattnere213f3f2009-03-12 23:59:55 +0000181 p++;
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000182 assert(p != end && "Exponent has no digits");
183 }
Chris Lattnere213f3f2009-03-12 23:59:55 +0000184
185 unsignedExponent = 0;
186 overflow = false;
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000187 for(; p != end; ++p) {
Chris Lattnere213f3f2009-03-12 23:59:55 +0000188 unsigned int value;
189
190 value = decDigitValue(*p);
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000191 assert(value < 10U && "Invalid character in exponent");
Chris Lattnere213f3f2009-03-12 23:59:55 +0000192
Chris Lattnere213f3f2009-03-12 23:59:55 +0000193 unsignedExponent = unsignedExponent * 10 + value;
194 if(unsignedExponent > 65535)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000195 overflow = true;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000196 }
197
Chris Lattnere213f3f2009-03-12 23:59:55 +0000198 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
199 overflow = true;
200
201 if(!overflow) {
202 exponent = unsignedExponent;
203 if(negative)
204 exponent = -exponent;
205 exponent += exponentAdjustment;
206 if(exponent > 65535 || exponent < -65536)
207 overflow = true;
208 }
209
210 if(overflow)
211 exponent = negative ? -65536: 65535;
212
213 return exponent;
214}
215
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000216static StringRef::iterator
217skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
218 StringRef::iterator *dot)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000219{
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000220 StringRef::iterator p = begin;
221 *dot = end;
222 while(*p == '0' && p != end)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000223 p++;
224
225 if(*p == '.') {
226 *dot = p++;
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000227
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000228 assert(end - begin != 1 && "Significand has no digits");
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000229
230 while(*p == '0' && p != end)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000231 p++;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000232 }
233
Chris Lattnere213f3f2009-03-12 23:59:55 +0000234 return p;
235}
Neil Booth1870f292007-10-14 10:16:12 +0000236
Chris Lattnere213f3f2009-03-12 23:59:55 +0000237/* Given a normal decimal floating point number of the form
Neil Booth1870f292007-10-14 10:16:12 +0000238
Chris Lattnere213f3f2009-03-12 23:59:55 +0000239 dddd.dddd[eE][+-]ddd
Neil Booth686700e2007-10-15 15:00:55 +0000240
Chris Lattnere213f3f2009-03-12 23:59:55 +0000241 where the decimal point and exponent are optional, fill out the
242 structure D. Exponent is appropriate if the significand is
243 treated as an integer, and normalizedExponent if the significand
244 is taken to have the decimal point after a single leading
245 non-zero digit.
Neil Booth1870f292007-10-14 10:16:12 +0000246
Chris Lattnere213f3f2009-03-12 23:59:55 +0000247 If the value is zero, V->firstSigDigit points to a non-digit, and
248 the return exponent is zero.
249*/
250struct decimalInfo {
251 const char *firstSigDigit;
252 const char *lastSigDigit;
253 int exponent;
254 int normalizedExponent;
255};
Neil Booth1870f292007-10-14 10:16:12 +0000256
Chris Lattnere213f3f2009-03-12 23:59:55 +0000257static void
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000258interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
259 decimalInfo *D)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000260{
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000261 StringRef::iterator dot = end;
262 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
Neil Booth1870f292007-10-14 10:16:12 +0000263
Chris Lattnere213f3f2009-03-12 23:59:55 +0000264 D->firstSigDigit = p;
265 D->exponent = 0;
266 D->normalizedExponent = 0;
267
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000268 for (; p != end; ++p) {
Chris Lattnere213f3f2009-03-12 23:59:55 +0000269 if (*p == '.') {
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000270 assert(dot == end && "String contains multiple dots");
Chris Lattnere213f3f2009-03-12 23:59:55 +0000271 dot = p++;
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000272 if (p == end)
273 break;
Neil Booth1870f292007-10-14 10:16:12 +0000274 }
Chris Lattnere213f3f2009-03-12 23:59:55 +0000275 if (decDigitValue(*p) >= 10U)
276 break;
Chris Lattnere213f3f2009-03-12 23:59:55 +0000277 }
Neil Booth1870f292007-10-14 10:16:12 +0000278
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000279 if (p != end) {
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000280 assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
281 assert(p != begin && "Significand has no digits");
282 assert((dot == end || p - begin != 1) && "Significand has no digits");
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000283
284 /* p points to the first non-digit in the string */
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +0000285 D->exponent = readExponent(p + 1, end);
Neil Booth1870f292007-10-14 10:16:12 +0000286
Chris Lattnere213f3f2009-03-12 23:59:55 +0000287 /* Implied decimal point? */
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000288 if (dot == end)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000289 dot = p;
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000290 }
Neil Booth1870f292007-10-14 10:16:12 +0000291
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000292 /* If number is all zeroes accept any exponent. */
293 if (p != D->firstSigDigit) {
Chris Lattnere213f3f2009-03-12 23:59:55 +0000294 /* Drop insignificant trailing zeroes. */
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000295 if (p != begin) {
Neil Booth1870f292007-10-14 10:16:12 +0000296 do
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000297 do
298 p--;
299 while (p != begin && *p == '0');
300 while (p != begin && *p == '.');
301 }
Neil Booth1870f292007-10-14 10:16:12 +0000302
Chris Lattnere213f3f2009-03-12 23:59:55 +0000303 /* Adjust the exponents for any decimal point. */
304 D->exponent += static_cast<exponent_t>((dot - p) - (dot > p));
305 D->normalizedExponent = (D->exponent +
306 static_cast<exponent_t>((p - D->firstSigDigit)
307 - (dot > D->firstSigDigit && dot < p)));
Neil Booth1870f292007-10-14 10:16:12 +0000308 }
309
Chris Lattnere213f3f2009-03-12 23:59:55 +0000310 D->lastSigDigit = p;
311}
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000312
Chris Lattnere213f3f2009-03-12 23:59:55 +0000313/* Return the trailing fraction of a hexadecimal number.
314 DIGITVALUE is the first hex digit of the fraction, P points to
315 the next digit. */
316static lostFraction
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000317trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
318 unsigned int digitValue)
Chris Lattnere213f3f2009-03-12 23:59:55 +0000319{
320 unsigned int hexDigit;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000321
Chris Lattnere213f3f2009-03-12 23:59:55 +0000322 /* If the first trailing digit isn't 0 or 8 we can work out the
323 fraction immediately. */
324 if(digitValue > 8)
325 return lfMoreThanHalf;
326 else if(digitValue < 8 && digitValue > 0)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000327 return lfLessThanHalf;
Chris Lattnere213f3f2009-03-12 23:59:55 +0000328
329 /* Otherwise we need to find the first non-zero digit. */
330 while(*p == '0')
331 p++;
332
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000333 assert(p != end && "Invalid trailing hexadecimal fraction!");
334
Chris Lattnere213f3f2009-03-12 23:59:55 +0000335 hexDigit = hexDigitValue(*p);
336
337 /* If we ran off the end it is exactly zero or one-half, otherwise
338 a little more. */
339 if(hexDigit == -1U)
340 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
341 else
342 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
343}
344
345/* Return the fraction lost were a bignum truncated losing the least
346 significant BITS bits. */
347static lostFraction
348lostFractionThroughTruncation(const integerPart *parts,
349 unsigned int partCount,
350 unsigned int bits)
351{
352 unsigned int lsb;
353
354 lsb = APInt::tcLSB(parts, partCount);
355
356 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
357 if(bits <= lsb)
358 return lfExactlyZero;
359 if(bits == lsb + 1)
360 return lfExactlyHalf;
361 if(bits <= partCount * integerPartWidth
362 && APInt::tcExtractBit(parts, bits - 1))
363 return lfMoreThanHalf;
364
365 return lfLessThanHalf;
366}
367
368/* Shift DST right BITS bits noting lost fraction. */
369static lostFraction
370shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
371{
372 lostFraction lost_fraction;
373
374 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
375
376 APInt::tcShiftRight(dst, parts, bits);
377
378 return lost_fraction;
379}
380
381/* Combine the effect of two lost fractions. */
382static lostFraction
383combineLostFractions(lostFraction moreSignificant,
384 lostFraction lessSignificant)
385{
386 if(lessSignificant != lfExactlyZero) {
387 if(moreSignificant == lfExactlyZero)
388 moreSignificant = lfLessThanHalf;
389 else if(moreSignificant == lfExactlyHalf)
390 moreSignificant = lfMoreThanHalf;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000391 }
392
Chris Lattnere213f3f2009-03-12 23:59:55 +0000393 return moreSignificant;
394}
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000395
Chris Lattnere213f3f2009-03-12 23:59:55 +0000396/* The error from the true value, in half-ulps, on multiplying two
397 floating point numbers, which differ from the value they
398 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
399 than the returned value.
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000400
Chris Lattnere213f3f2009-03-12 23:59:55 +0000401 See "How to Read Floating Point Numbers Accurately" by William D
402 Clinger. */
403static unsigned int
404HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
405{
406 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000407
Chris Lattnere213f3f2009-03-12 23:59:55 +0000408 if (HUerr1 + HUerr2 == 0)
409 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
410 else
411 return inexactMultiply + 2 * (HUerr1 + HUerr2);
412}
Neil Bootha30b0ee2007-10-03 22:26:02 +0000413
Chris Lattnere213f3f2009-03-12 23:59:55 +0000414/* The number of ulps from the boundary (zero, or half if ISNEAREST)
415 when the least significant BITS are truncated. BITS cannot be
416 zero. */
417static integerPart
418ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
419{
420 unsigned int count, partBits;
421 integerPart part, boundary;
Neil Booth33d4c922007-10-07 08:51:21 +0000422
Chris Lattnere213f3f2009-03-12 23:59:55 +0000423 assert (bits != 0);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000424
Chris Lattnere213f3f2009-03-12 23:59:55 +0000425 bits--;
426 count = bits / integerPartWidth;
427 partBits = bits % integerPartWidth + 1;
Neil Booth96c74712007-10-12 16:02:31 +0000428
Chris Lattnere213f3f2009-03-12 23:59:55 +0000429 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
Neil Booth96c74712007-10-12 16:02:31 +0000430
Chris Lattnere213f3f2009-03-12 23:59:55 +0000431 if (isNearest)
432 boundary = (integerPart) 1 << (partBits - 1);
433 else
434 boundary = 0;
435
436 if (count == 0) {
437 if (part - boundary <= boundary - part)
438 return part - boundary;
Neil Booth96c74712007-10-12 16:02:31 +0000439 else
Chris Lattnere213f3f2009-03-12 23:59:55 +0000440 return boundary - part;
Neil Booth96c74712007-10-12 16:02:31 +0000441 }
442
Chris Lattnere213f3f2009-03-12 23:59:55 +0000443 if (part == boundary) {
444 while (--count)
445 if (parts[count])
446 return ~(integerPart) 0; /* A lot. */
Neil Booth96c74712007-10-12 16:02:31 +0000447
Chris Lattnere213f3f2009-03-12 23:59:55 +0000448 return parts[0];
449 } else if (part == boundary - 1) {
450 while (--count)
451 if (~parts[count])
452 return ~(integerPart) 0; /* A lot. */
Neil Booth96c74712007-10-12 16:02:31 +0000453
Chris Lattnere213f3f2009-03-12 23:59:55 +0000454 return -parts[0];
455 }
Neil Booth96c74712007-10-12 16:02:31 +0000456
Chris Lattnere213f3f2009-03-12 23:59:55 +0000457 return ~(integerPart) 0; /* A lot. */
458}
Neil Booth96c74712007-10-12 16:02:31 +0000459
Chris Lattnere213f3f2009-03-12 23:59:55 +0000460/* Place pow(5, power) in DST, and return the number of parts used.
461 DST must be at least one part larger than size of the answer. */
462static unsigned int
463powerOf5(integerPart *dst, unsigned int power)
464{
465 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
466 15625, 78125 };
Chris Lattneree167a72009-03-13 00:24:01 +0000467 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
468 pow5s[0] = 78125 * 5;
469
Chris Lattner807926a2009-03-13 00:03:51 +0000470 unsigned int partsCount[16] = { 1 };
Chris Lattnere213f3f2009-03-12 23:59:55 +0000471 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
472 unsigned int result;
Chris Lattnere213f3f2009-03-12 23:59:55 +0000473 assert(power <= maxExponent);
474
475 p1 = dst;
476 p2 = scratch;
477
478 *p1 = firstEightPowers[power & 7];
479 power >>= 3;
480
481 result = 1;
482 pow5 = pow5s;
483
484 for (unsigned int n = 0; power; power >>= 1, n++) {
485 unsigned int pc;
486
487 pc = partsCount[n];
488
489 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
490 if (pc == 0) {
491 pc = partsCount[n - 1];
492 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
493 pc *= 2;
494 if (pow5[pc - 1] == 0)
495 pc--;
496 partsCount[n] = pc;
Neil Booth96c74712007-10-12 16:02:31 +0000497 }
498
Chris Lattnere213f3f2009-03-12 23:59:55 +0000499 if (power & 1) {
500 integerPart *tmp;
Neil Booth96c74712007-10-12 16:02:31 +0000501
Chris Lattnere213f3f2009-03-12 23:59:55 +0000502 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
503 result += pc;
504 if (p2[result - 1] == 0)
505 result--;
Neil Booth96c74712007-10-12 16:02:31 +0000506
Chris Lattnere213f3f2009-03-12 23:59:55 +0000507 /* Now result is in p1 with partsCount parts and p2 is scratch
508 space. */
509 tmp = p1, p1 = p2, p2 = tmp;
Neil Booth96c74712007-10-12 16:02:31 +0000510 }
511
Chris Lattnere213f3f2009-03-12 23:59:55 +0000512 pow5 += pc;
Neil Booth96c74712007-10-12 16:02:31 +0000513 }
514
Chris Lattnere213f3f2009-03-12 23:59:55 +0000515 if (p1 != dst)
516 APInt::tcAssign(dst, p1, result);
Neil Booth96c74712007-10-12 16:02:31 +0000517
Chris Lattnere213f3f2009-03-12 23:59:55 +0000518 return result;
519}
Neil Booth96c74712007-10-12 16:02:31 +0000520
Chris Lattnere213f3f2009-03-12 23:59:55 +0000521/* Zero at the end to avoid modular arithmetic when adding one; used
522 when rounding up during hexadecimal output. */
523static const char hexDigitsLower[] = "0123456789abcdef0";
524static const char hexDigitsUpper[] = "0123456789ABCDEF0";
525static const char infinityL[] = "infinity";
526static const char infinityU[] = "INFINITY";
527static const char NaNL[] = "nan";
528static const char NaNU[] = "NAN";
Neil Booth96c74712007-10-12 16:02:31 +0000529
Chris Lattnere213f3f2009-03-12 23:59:55 +0000530/* Write out an integerPart in hexadecimal, starting with the most
531 significant nibble. Write out exactly COUNT hexdigits, return
532 COUNT. */
533static unsigned int
534partAsHex (char *dst, integerPart part, unsigned int count,
535 const char *hexDigitChars)
536{
537 unsigned int result = count;
Neil Booth96c74712007-10-12 16:02:31 +0000538
Chris Lattnere213f3f2009-03-12 23:59:55 +0000539 assert (count != 0 && count <= integerPartWidth / 4);
Neil Booth96c74712007-10-12 16:02:31 +0000540
Chris Lattnere213f3f2009-03-12 23:59:55 +0000541 part >>= (integerPartWidth - 4 * count);
542 while (count--) {
543 dst[count] = hexDigitChars[part & 0xf];
544 part >>= 4;
Neil Booth96c74712007-10-12 16:02:31 +0000545 }
546
Chris Lattnere213f3f2009-03-12 23:59:55 +0000547 return result;
548}
Neil Bootha30b0ee2007-10-03 22:26:02 +0000549
Chris Lattnere213f3f2009-03-12 23:59:55 +0000550/* Write out an unsigned decimal integer. */
551static char *
552writeUnsignedDecimal (char *dst, unsigned int n)
553{
554 char buff[40], *p;
Neil Bootha30b0ee2007-10-03 22:26:02 +0000555
Chris Lattnere213f3f2009-03-12 23:59:55 +0000556 p = buff;
557 do
558 *p++ = '0' + n % 10;
559 while (n /= 10);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000560
Chris Lattnere213f3f2009-03-12 23:59:55 +0000561 do
562 *dst++ = *--p;
563 while (p != buff);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000564
Chris Lattnere213f3f2009-03-12 23:59:55 +0000565 return dst;
566}
Neil Bootha30b0ee2007-10-03 22:26:02 +0000567
Chris Lattnere213f3f2009-03-12 23:59:55 +0000568/* Write out a signed decimal integer. */
569static char *
570writeSignedDecimal (char *dst, int value)
571{
572 if (value < 0) {
573 *dst++ = '-';
574 dst = writeUnsignedDecimal(dst, -(unsigned) value);
575 } else
576 dst = writeUnsignedDecimal(dst, value);
Neil Bootha30b0ee2007-10-03 22:26:02 +0000577
Chris Lattnere213f3f2009-03-12 23:59:55 +0000578 return dst;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000579}
580
581/* Constructors. */
582void
583APFloat::initialize(const fltSemantics *ourSemantics)
584{
585 unsigned int count;
586
587 semantics = ourSemantics;
588 count = partCount();
589 if(count > 1)
590 significand.parts = new integerPart[count];
591}
592
593void
594APFloat::freeSignificand()
595{
596 if(partCount() > 1)
597 delete [] significand.parts;
598}
599
600void
601APFloat::assign(const APFloat &rhs)
602{
603 assert(semantics == rhs.semantics);
604
605 sign = rhs.sign;
606 category = rhs.category;
607 exponent = rhs.exponent;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000608 sign2 = rhs.sign2;
609 exponent2 = rhs.exponent2;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000610 if(category == fcNormal || category == fcNaN)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000611 copySignificand(rhs);
612}
613
614void
615APFloat::copySignificand(const APFloat &rhs)
616{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000617 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000618 assert(rhs.partCount() >= partCount());
619
620 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +0000621 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000622}
623
Neil Boothe5e01942007-10-14 10:39:51 +0000624/* Make this number a NaN, with an arbitrary but deterministic value
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000625 for the significand. If double or longer, this is a signalling NaN,
Mike Stumpc5ca7132009-05-30 03:49:43 +0000626 which may not be ideal. If float, this is QNaN(0). */
Neil Boothe5e01942007-10-14 10:39:51 +0000627void
Mike Stumpc5ca7132009-05-30 03:49:43 +0000628APFloat::makeNaN(unsigned type)
Neil Boothe5e01942007-10-14 10:39:51 +0000629{
630 category = fcNaN;
Mike Stumpc5ca7132009-05-30 03:49:43 +0000631 // FIXME: Add double and long double support for QNaN(0).
632 if (semantics->precision == 24 && semantics->maxExponent == 127) {
633 type |= 0x7fc00000U;
634 type &= ~0x80000000U;
635 } else
636 type = ~0U;
637 APInt::tcSet(significandParts(), type, partCount());
Neil Boothe5e01942007-10-14 10:39:51 +0000638}
639
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000640APFloat &
641APFloat::operator=(const APFloat &rhs)
642{
643 if(this != &rhs) {
644 if(semantics != rhs.semantics) {
645 freeSignificand();
646 initialize(rhs.semantics);
647 }
648 assign(rhs);
649 }
650
651 return *this;
652}
653
Dale Johannesen343e7702007-08-24 00:56:33 +0000654bool
Dale Johannesen12595d72007-08-24 22:09:56 +0000655APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000656 if (this == &rhs)
657 return true;
658 if (semantics != rhs.semantics ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000659 category != rhs.category ||
660 sign != rhs.sign)
Dale Johannesen343e7702007-08-24 00:56:33 +0000661 return false;
Dan Gohmanb10abe12008-01-29 12:08:20 +0000662 if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
Dale Johannesena471c2e2007-10-11 18:07:22 +0000663 sign2 != rhs.sign2)
664 return false;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000665 if (category==fcZero || category==fcInfinity)
Dale Johannesen343e7702007-08-24 00:56:33 +0000666 return true;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000667 else if (category==fcNormal && exponent!=rhs.exponent)
668 return false;
Dan Gohmanb10abe12008-01-29 12:08:20 +0000669 else if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
Dale Johannesena471c2e2007-10-11 18:07:22 +0000670 exponent2!=rhs.exponent2)
671 return false;
Dale Johannesen343e7702007-08-24 00:56:33 +0000672 else {
Dale Johannesen343e7702007-08-24 00:56:33 +0000673 int i= partCount();
674 const integerPart* p=significandParts();
675 const integerPart* q=rhs.significandParts();
676 for (; i>0; i--, p++, q++) {
677 if (*p != *q)
678 return false;
679 }
680 return true;
681 }
682}
683
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000684APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
685{
Neil Boothcaf19d72007-10-14 10:29:28 +0000686 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000687 initialize(&ourSemantics);
688 sign = 0;
689 zeroSignificand();
690 exponent = ourSemantics.precision - 1;
691 significandParts()[0] = value;
692 normalize(rmNearestTiesToEven, lfExactlyZero);
693}
694
695APFloat::APFloat(const fltSemantics &ourSemantics,
Mike Stumpc5ca7132009-05-30 03:49:43 +0000696 fltCategory ourCategory, bool negative, unsigned type)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000697{
Neil Boothcaf19d72007-10-14 10:29:28 +0000698 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000699 initialize(&ourSemantics);
700 category = ourCategory;
701 sign = negative;
Mike Stumpc5ca7132009-05-30 03:49:43 +0000702 if (category == fcNormal)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000703 category = fcZero;
Neil Boothe5e01942007-10-14 10:39:51 +0000704 else if (ourCategory == fcNaN)
Mike Stumpc5ca7132009-05-30 03:49:43 +0000705 makeNaN(type);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000706}
707
Erick Tryzelaara15d8902009-08-16 23:36:19 +0000708APFloat::APFloat(const fltSemantics &ourSemantics, const StringRef& text)
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000709{
Neil Boothcaf19d72007-10-14 10:29:28 +0000710 assertArithmeticOK(ourSemantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000711 initialize(&ourSemantics);
712 convertFromString(text, rmNearestTiesToEven);
713}
714
715APFloat::APFloat(const APFloat &rhs)
716{
717 initialize(rhs.semantics);
718 assign(rhs);
719}
720
721APFloat::~APFloat()
722{
723 freeSignificand();
724}
725
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000726// Profile - This method 'profiles' an APFloat for use with FoldingSet.
727void APFloat::Profile(FoldingSetNodeID& ID) const {
Dale Johannesen7111b022008-10-09 18:53:47 +0000728 ID.Add(bitcastToAPInt());
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000729}
730
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000731unsigned int
732APFloat::partCount() const
733{
Dale Johannesena72a5a02007-09-20 23:47:58 +0000734 return partCountForBits(semantics->precision + 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000735}
736
737unsigned int
738APFloat::semanticsPrecision(const fltSemantics &semantics)
739{
740 return semantics.precision;
741}
742
743const integerPart *
744APFloat::significandParts() const
745{
746 return const_cast<APFloat *>(this)->significandParts();
747}
748
749integerPart *
750APFloat::significandParts()
751{
Dale Johanneseneaf08942007-08-31 04:03:46 +0000752 assert(category == fcNormal || category == fcNaN);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000753
754 if(partCount() > 1)
755 return significand.parts;
756 else
757 return &significand.part;
758}
759
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000760void
761APFloat::zeroSignificand()
762{
763 category = fcNormal;
764 APInt::tcSet(significandParts(), 0, partCount());
765}
766
767/* Increment an fcNormal floating point number's significand. */
768void
769APFloat::incrementSignificand()
770{
771 integerPart carry;
772
773 carry = APInt::tcIncrement(significandParts(), partCount());
774
775 /* Our callers should never cause us to overflow. */
776 assert(carry == 0);
777}
778
779/* Add the significand of the RHS. Returns the carry flag. */
780integerPart
781APFloat::addSignificand(const APFloat &rhs)
782{
783 integerPart *parts;
784
785 parts = significandParts();
786
787 assert(semantics == rhs.semantics);
788 assert(exponent == rhs.exponent);
789
790 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
791}
792
793/* Subtract the significand of the RHS with a borrow flag. Returns
794 the borrow flag. */
795integerPart
796APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
797{
798 integerPart *parts;
799
800 parts = significandParts();
801
802 assert(semantics == rhs.semantics);
803 assert(exponent == rhs.exponent);
804
805 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth4f881702007-09-26 21:33:42 +0000806 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000807}
808
809/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
810 on to the full-precision result of the multiplication. Returns the
811 lost fraction. */
812lostFraction
813APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
814{
Neil Booth4f881702007-09-26 21:33:42 +0000815 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000816 unsigned int partsCount, newPartsCount, precision;
817 integerPart *lhsSignificand;
818 integerPart scratch[4];
819 integerPart *fullSignificand;
820 lostFraction lost_fraction;
Dale Johannesen23a98552008-10-09 23:00:39 +0000821 bool ignored;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000822
823 assert(semantics == rhs.semantics);
824
825 precision = semantics->precision;
826 newPartsCount = partCountForBits(precision * 2);
827
828 if(newPartsCount > 4)
829 fullSignificand = new integerPart[newPartsCount];
830 else
831 fullSignificand = scratch;
832
833 lhsSignificand = significandParts();
834 partsCount = partCount();
835
836 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth978661d2007-10-06 00:24:48 +0000837 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000838
839 lost_fraction = lfExactlyZero;
840 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
841 exponent += rhs.exponent;
842
843 if(addend) {
844 Significand savedSignificand = significand;
845 const fltSemantics *savedSemantics = semantics;
846 fltSemantics extendedSemantics;
847 opStatus status;
848 unsigned int extendedPrecision;
849
850 /* Normalize our MSB. */
851 extendedPrecision = precision + precision - 1;
852 if(omsb != extendedPrecision)
853 {
Neil Booth4f881702007-09-26 21:33:42 +0000854 APInt::tcShiftLeft(fullSignificand, newPartsCount,
855 extendedPrecision - omsb);
856 exponent -= extendedPrecision - omsb;
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000857 }
858
859 /* Create new semantics. */
860 extendedSemantics = *semantics;
861 extendedSemantics.precision = extendedPrecision;
862
863 if(newPartsCount == 1)
864 significand.part = fullSignificand[0];
865 else
866 significand.parts = fullSignificand;
867 semantics = &extendedSemantics;
868
869 APFloat extendedAddend(*addend);
Dale Johannesen23a98552008-10-09 23:00:39 +0000870 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000871 assert(status == opOK);
872 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
873
874 /* Restore our state. */
875 if(newPartsCount == 1)
876 fullSignificand[0] = significand.part;
877 significand = savedSignificand;
878 semantics = savedSemantics;
879
880 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
881 }
882
883 exponent -= (precision - 1);
884
885 if(omsb > precision) {
886 unsigned int bits, significantParts;
887 lostFraction lf;
888
889 bits = omsb - precision;
890 significantParts = partCountForBits(omsb);
891 lf = shiftRight(fullSignificand, significantParts, bits);
892 lost_fraction = combineLostFractions(lf, lost_fraction);
893 exponent += bits;
894 }
895
896 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
897
898 if(newPartsCount > 4)
899 delete [] fullSignificand;
900
901 return lost_fraction;
902}
903
904/* Multiply the significands of LHS and RHS to DST. */
905lostFraction
906APFloat::divideSignificand(const APFloat &rhs)
907{
908 unsigned int bit, i, partsCount;
909 const integerPart *rhsSignificand;
910 integerPart *lhsSignificand, *dividend, *divisor;
911 integerPart scratch[4];
912 lostFraction lost_fraction;
913
914 assert(semantics == rhs.semantics);
915
916 lhsSignificand = significandParts();
917 rhsSignificand = rhs.significandParts();
918 partsCount = partCount();
919
920 if(partsCount > 2)
921 dividend = new integerPart[partsCount * 2];
922 else
923 dividend = scratch;
924
925 divisor = dividend + partsCount;
926
927 /* Copy the dividend and divisor as they will be modified in-place. */
928 for(i = 0; i < partsCount; i++) {
929 dividend[i] = lhsSignificand[i];
930 divisor[i] = rhsSignificand[i];
931 lhsSignificand[i] = 0;
932 }
933
934 exponent -= rhs.exponent;
935
936 unsigned int precision = semantics->precision;
937
938 /* Normalize the divisor. */
939 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
940 if(bit) {
941 exponent += bit;
942 APInt::tcShiftLeft(divisor, partsCount, bit);
943 }
944
945 /* Normalize the dividend. */
946 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
947 if(bit) {
948 exponent -= bit;
949 APInt::tcShiftLeft(dividend, partsCount, bit);
950 }
951
Neil Booth96c74712007-10-12 16:02:31 +0000952 /* Ensure the dividend >= divisor initially for the loop below.
953 Incidentally, this means that the division loop below is
954 guaranteed to set the integer bit to one. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +0000955 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
956 exponent--;
957 APInt::tcShiftLeft(dividend, partsCount, 1);
958 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
959 }
960
961 /* Long division. */
962 for(bit = precision; bit; bit -= 1) {
963 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
964 APInt::tcSubtract(dividend, divisor, 0, partsCount);
965 APInt::tcSetBit(lhsSignificand, bit - 1);
966 }
967
968 APInt::tcShiftLeft(dividend, partsCount, 1);
969 }
970
971 /* Figure out the lost fraction. */
972 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
973
974 if(cmp > 0)
975 lost_fraction = lfMoreThanHalf;
976 else if(cmp == 0)
977 lost_fraction = lfExactlyHalf;
978 else if(APInt::tcIsZero(dividend, partsCount))
979 lost_fraction = lfExactlyZero;
980 else
981 lost_fraction = lfLessThanHalf;
982
983 if(partsCount > 2)
984 delete [] dividend;
985
986 return lost_fraction;
987}
988
989unsigned int
990APFloat::significandMSB() const
991{
992 return APInt::tcMSB(significandParts(), partCount());
993}
994
995unsigned int
996APFloat::significandLSB() const
997{
998 return APInt::tcLSB(significandParts(), partCount());
999}
1000
1001/* Note that a zero result is NOT normalized to fcZero. */
1002lostFraction
1003APFloat::shiftSignificandRight(unsigned int bits)
1004{
1005 /* Our exponent should not overflow. */
1006 assert((exponent_t) (exponent + bits) >= exponent);
1007
1008 exponent += bits;
1009
1010 return shiftRight(significandParts(), partCount(), bits);
1011}
1012
1013/* Shift the significand left BITS bits, subtract BITS from its exponent. */
1014void
1015APFloat::shiftSignificandLeft(unsigned int bits)
1016{
1017 assert(bits < semantics->precision);
1018
1019 if(bits) {
1020 unsigned int partsCount = partCount();
1021
1022 APInt::tcShiftLeft(significandParts(), partsCount, bits);
1023 exponent -= bits;
1024
1025 assert(!APInt::tcIsZero(significandParts(), partsCount));
1026 }
1027}
1028
1029APFloat::cmpResult
1030APFloat::compareAbsoluteValue(const APFloat &rhs) const
1031{
1032 int compare;
1033
1034 assert(semantics == rhs.semantics);
1035 assert(category == fcNormal);
1036 assert(rhs.category == fcNormal);
1037
1038 compare = exponent - rhs.exponent;
1039
1040 /* If exponents are equal, do an unsigned bignum comparison of the
1041 significands. */
1042 if(compare == 0)
1043 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth4f881702007-09-26 21:33:42 +00001044 partCount());
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001045
1046 if(compare > 0)
1047 return cmpGreaterThan;
1048 else if(compare < 0)
1049 return cmpLessThan;
1050 else
1051 return cmpEqual;
1052}
1053
1054/* Handle overflow. Sign is preserved. We either become infinity or
1055 the largest finite number. */
1056APFloat::opStatus
1057APFloat::handleOverflow(roundingMode rounding_mode)
1058{
1059 /* Infinity? */
1060 if(rounding_mode == rmNearestTiesToEven
1061 || rounding_mode == rmNearestTiesToAway
1062 || (rounding_mode == rmTowardPositive && !sign)
1063 || (rounding_mode == rmTowardNegative && sign))
1064 {
1065 category = fcInfinity;
1066 return (opStatus) (opOverflow | opInexact);
1067 }
1068
1069 /* Otherwise we become the largest finite number. */
1070 category = fcNormal;
1071 exponent = semantics->maxExponent;
1072 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth4f881702007-09-26 21:33:42 +00001073 semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001074
1075 return opInexact;
1076}
1077
Neil Boothb7dea4c2007-10-03 15:16:41 +00001078/* Returns TRUE if, when truncating the current number, with BIT the
1079 new LSB, with the given lost fraction and rounding mode, the result
1080 would need to be rounded away from zero (i.e., by increasing the
1081 signficand). This routine must work for fcZero of both signs, and
1082 fcNormal numbers. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001083bool
1084APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Boothb7dea4c2007-10-03 15:16:41 +00001085 lostFraction lost_fraction,
1086 unsigned int bit) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001087{
Dale Johanneseneaf08942007-08-31 04:03:46 +00001088 /* NaNs and infinities should not have lost fractions. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001089 assert(category == fcNormal || category == fcZero);
1090
Neil Boothb7dea4c2007-10-03 15:16:41 +00001091 /* Current callers never pass this so we don't handle it. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001092 assert(lost_fraction != lfExactlyZero);
1093
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001094 switch (rounding_mode) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001095 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001096 llvm_unreachable(0);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001097
1098 case rmNearestTiesToAway:
1099 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1100
1101 case rmNearestTiesToEven:
1102 if(lost_fraction == lfMoreThanHalf)
1103 return true;
1104
1105 /* Our zeroes don't have a significand to test. */
1106 if(lost_fraction == lfExactlyHalf && category != fcZero)
Neil Boothb7dea4c2007-10-03 15:16:41 +00001107 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001108
1109 return false;
1110
1111 case rmTowardZero:
1112 return false;
1113
1114 case rmTowardPositive:
1115 return sign == false;
1116
1117 case rmTowardNegative:
1118 return sign == true;
1119 }
1120}
1121
1122APFloat::opStatus
1123APFloat::normalize(roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001124 lostFraction lost_fraction)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001125{
Neil Booth4f881702007-09-26 21:33:42 +00001126 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001127 int exponentChange;
1128
1129 if(category != fcNormal)
1130 return opOK;
1131
1132 /* Before rounding normalize the exponent of fcNormal numbers. */
1133 omsb = significandMSB() + 1;
1134
1135 if(omsb) {
1136 /* OMSB is numbered from 1. We want to place it in the integer
1137 bit numbered PRECISON if possible, with a compensating change in
1138 the exponent. */
1139 exponentChange = omsb - semantics->precision;
1140
1141 /* If the resulting exponent is too high, overflow according to
1142 the rounding mode. */
1143 if(exponent + exponentChange > semantics->maxExponent)
1144 return handleOverflow(rounding_mode);
1145
1146 /* Subnormal numbers have exponent minExponent, and their MSB
1147 is forced based on that. */
1148 if(exponent + exponentChange < semantics->minExponent)
1149 exponentChange = semantics->minExponent - exponent;
1150
1151 /* Shifting left is easy as we don't lose precision. */
1152 if(exponentChange < 0) {
1153 assert(lost_fraction == lfExactlyZero);
1154
1155 shiftSignificandLeft(-exponentChange);
1156
1157 return opOK;
1158 }
1159
1160 if(exponentChange > 0) {
1161 lostFraction lf;
1162
1163 /* Shift right and capture any new lost fraction. */
1164 lf = shiftSignificandRight(exponentChange);
1165
1166 lost_fraction = combineLostFractions(lf, lost_fraction);
1167
1168 /* Keep OMSB up-to-date. */
1169 if(omsb > (unsigned) exponentChange)
Neil Booth96c74712007-10-12 16:02:31 +00001170 omsb -= exponentChange;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001171 else
Neil Booth4f881702007-09-26 21:33:42 +00001172 omsb = 0;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001173 }
1174 }
1175
1176 /* Now round the number according to rounding_mode given the lost
1177 fraction. */
1178
1179 /* As specified in IEEE 754, since we do not trap we do not report
1180 underflow for exact results. */
1181 if(lost_fraction == lfExactlyZero) {
1182 /* Canonicalize zeroes. */
1183 if(omsb == 0)
1184 category = fcZero;
1185
1186 return opOK;
1187 }
1188
1189 /* Increment the significand if we're rounding away from zero. */
Neil Boothb7dea4c2007-10-03 15:16:41 +00001190 if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001191 if(omsb == 0)
1192 exponent = semantics->minExponent;
1193
1194 incrementSignificand();
1195 omsb = significandMSB() + 1;
1196
1197 /* Did the significand increment overflow? */
1198 if(omsb == (unsigned) semantics->precision + 1) {
1199 /* Renormalize by incrementing the exponent and shifting our
Neil Booth4f881702007-09-26 21:33:42 +00001200 significand right one. However if we already have the
1201 maximum exponent we overflow to infinity. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001202 if(exponent == semantics->maxExponent) {
Neil Booth4f881702007-09-26 21:33:42 +00001203 category = fcInfinity;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001204
Neil Booth4f881702007-09-26 21:33:42 +00001205 return (opStatus) (opOverflow | opInexact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001206 }
1207
1208 shiftSignificandRight(1);
1209
1210 return opInexact;
1211 }
1212 }
1213
1214 /* The normal case - we were and are not denormal, and any
1215 significand increment above didn't overflow. */
1216 if(omsb == semantics->precision)
1217 return opInexact;
1218
1219 /* We have a non-zero denormal. */
1220 assert(omsb < semantics->precision);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001221
1222 /* Canonicalize zeroes. */
1223 if(omsb == 0)
1224 category = fcZero;
1225
1226 /* The fcZero case is a denormal that underflowed to zero. */
1227 return (opStatus) (opUnderflow | opInexact);
1228}
1229
1230APFloat::opStatus
1231APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1232{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001233 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001234 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001235 llvm_unreachable(0);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001236
Dale Johanneseneaf08942007-08-31 04:03:46 +00001237 case convolve(fcNaN, fcZero):
1238 case convolve(fcNaN, fcNormal):
1239 case convolve(fcNaN, fcInfinity):
1240 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001241 case convolve(fcNormal, fcZero):
1242 case convolve(fcInfinity, fcNormal):
1243 case convolve(fcInfinity, fcZero):
1244 return opOK;
1245
Dale Johanneseneaf08942007-08-31 04:03:46 +00001246 case convolve(fcZero, fcNaN):
1247 case convolve(fcNormal, fcNaN):
1248 case convolve(fcInfinity, fcNaN):
1249 category = fcNaN;
1250 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001251 return opOK;
1252
1253 case convolve(fcNormal, fcInfinity):
1254 case convolve(fcZero, fcInfinity):
1255 category = fcInfinity;
1256 sign = rhs.sign ^ subtract;
1257 return opOK;
1258
1259 case convolve(fcZero, fcNormal):
1260 assign(rhs);
1261 sign = rhs.sign ^ subtract;
1262 return opOK;
1263
1264 case convolve(fcZero, fcZero):
1265 /* Sign depends on rounding mode; handled by caller. */
1266 return opOK;
1267
1268 case convolve(fcInfinity, fcInfinity):
1269 /* Differently signed infinities can only be validly
1270 subtracted. */
Cedric Venetaff9c272009-02-14 16:06:42 +00001271 if(((sign ^ rhs.sign)!=0) != subtract) {
Neil Boothe5e01942007-10-14 10:39:51 +00001272 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001273 return opInvalidOp;
1274 }
1275
1276 return opOK;
1277
1278 case convolve(fcNormal, fcNormal):
1279 return opDivByZero;
1280 }
1281}
1282
1283/* Add or subtract two normal numbers. */
1284lostFraction
1285APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1286{
1287 integerPart carry;
1288 lostFraction lost_fraction;
1289 int bits;
1290
1291 /* Determine if the operation on the absolute values is effectively
1292 an addition or subtraction. */
Hartmut Kaiser8df77a92007-10-25 23:15:31 +00001293 subtract ^= (sign ^ rhs.sign) ? true : false;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001294
1295 /* Are we bigger exponent-wise than the RHS? */
1296 bits = exponent - rhs.exponent;
1297
1298 /* Subtraction is more subtle than one might naively expect. */
1299 if(subtract) {
1300 APFloat temp_rhs(rhs);
1301 bool reverse;
1302
Chris Lattnerada530b2007-08-24 03:02:34 +00001303 if (bits == 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001304 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1305 lost_fraction = lfExactlyZero;
Chris Lattnerada530b2007-08-24 03:02:34 +00001306 } else if (bits > 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001307 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1308 shiftSignificandLeft(1);
1309 reverse = false;
Chris Lattnerada530b2007-08-24 03:02:34 +00001310 } else {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001311 lost_fraction = shiftSignificandRight(-bits - 1);
1312 temp_rhs.shiftSignificandLeft(1);
1313 reverse = true;
1314 }
1315
Chris Lattnerada530b2007-08-24 03:02:34 +00001316 if (reverse) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001317 carry = temp_rhs.subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001318 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001319 copySignificand(temp_rhs);
1320 sign = !sign;
1321 } else {
1322 carry = subtractSignificand
Neil Booth4f881702007-09-26 21:33:42 +00001323 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001324 }
1325
1326 /* Invert the lost fraction - it was on the RHS and
1327 subtracted. */
1328 if(lost_fraction == lfLessThanHalf)
1329 lost_fraction = lfMoreThanHalf;
1330 else if(lost_fraction == lfMoreThanHalf)
1331 lost_fraction = lfLessThanHalf;
1332
1333 /* The code above is intended to ensure that no borrow is
1334 necessary. */
1335 assert(!carry);
1336 } else {
1337 if(bits > 0) {
1338 APFloat temp_rhs(rhs);
1339
1340 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1341 carry = addSignificand(temp_rhs);
1342 } else {
1343 lost_fraction = shiftSignificandRight(-bits);
1344 carry = addSignificand(rhs);
1345 }
1346
1347 /* We have a guard bit; generating a carry cannot happen. */
1348 assert(!carry);
1349 }
1350
1351 return lost_fraction;
1352}
1353
1354APFloat::opStatus
1355APFloat::multiplySpecials(const APFloat &rhs)
1356{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001357 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001358 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001359 llvm_unreachable(0);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001360
Dale Johanneseneaf08942007-08-31 04:03:46 +00001361 case convolve(fcNaN, fcZero):
1362 case convolve(fcNaN, fcNormal):
1363 case convolve(fcNaN, fcInfinity):
1364 case convolve(fcNaN, fcNaN):
1365 return opOK;
1366
1367 case convolve(fcZero, fcNaN):
1368 case convolve(fcNormal, fcNaN):
1369 case convolve(fcInfinity, fcNaN):
1370 category = fcNaN;
1371 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001372 return opOK;
1373
1374 case convolve(fcNormal, fcInfinity):
1375 case convolve(fcInfinity, fcNormal):
1376 case convolve(fcInfinity, fcInfinity):
1377 category = fcInfinity;
1378 return opOK;
1379
1380 case convolve(fcZero, fcNormal):
1381 case convolve(fcNormal, fcZero):
1382 case convolve(fcZero, fcZero):
1383 category = fcZero;
1384 return opOK;
1385
1386 case convolve(fcZero, fcInfinity):
1387 case convolve(fcInfinity, fcZero):
Neil Boothe5e01942007-10-14 10:39:51 +00001388 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001389 return opInvalidOp;
1390
1391 case convolve(fcNormal, fcNormal):
1392 return opOK;
1393 }
1394}
1395
1396APFloat::opStatus
1397APFloat::divideSpecials(const APFloat &rhs)
1398{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001399 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001400 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001401 llvm_unreachable(0);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001402
Dale Johanneseneaf08942007-08-31 04:03:46 +00001403 case convolve(fcNaN, fcZero):
1404 case convolve(fcNaN, fcNormal):
1405 case convolve(fcNaN, fcInfinity):
1406 case convolve(fcNaN, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001407 case convolve(fcInfinity, fcZero):
1408 case convolve(fcInfinity, fcNormal):
1409 case convolve(fcZero, fcInfinity):
1410 case convolve(fcZero, fcNormal):
1411 return opOK;
1412
Dale Johanneseneaf08942007-08-31 04:03:46 +00001413 case convolve(fcZero, fcNaN):
1414 case convolve(fcNormal, fcNaN):
1415 case convolve(fcInfinity, fcNaN):
1416 category = fcNaN;
1417 copySignificand(rhs);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001418 return opOK;
1419
1420 case convolve(fcNormal, fcInfinity):
1421 category = fcZero;
1422 return opOK;
1423
1424 case convolve(fcNormal, fcZero):
1425 category = fcInfinity;
1426 return opDivByZero;
1427
1428 case convolve(fcInfinity, fcInfinity):
1429 case convolve(fcZero, fcZero):
Neil Boothe5e01942007-10-14 10:39:51 +00001430 makeNaN();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001431 return opInvalidOp;
1432
1433 case convolve(fcNormal, fcNormal):
1434 return opOK;
1435 }
1436}
1437
Dale Johannesened6af242009-01-21 00:35:19 +00001438APFloat::opStatus
1439APFloat::modSpecials(const APFloat &rhs)
1440{
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001441 switch (convolve(category, rhs.category)) {
Dale Johannesened6af242009-01-21 00:35:19 +00001442 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001443 llvm_unreachable(0);
Dale Johannesened6af242009-01-21 00:35:19 +00001444
1445 case convolve(fcNaN, fcZero):
1446 case convolve(fcNaN, fcNormal):
1447 case convolve(fcNaN, fcInfinity):
1448 case convolve(fcNaN, fcNaN):
1449 case convolve(fcZero, fcInfinity):
1450 case convolve(fcZero, fcNormal):
1451 case convolve(fcNormal, fcInfinity):
1452 return opOK;
1453
1454 case convolve(fcZero, fcNaN):
1455 case convolve(fcNormal, fcNaN):
1456 case convolve(fcInfinity, fcNaN):
1457 category = fcNaN;
1458 copySignificand(rhs);
1459 return opOK;
1460
1461 case convolve(fcNormal, fcZero):
1462 case convolve(fcInfinity, fcZero):
1463 case convolve(fcInfinity, fcNormal):
1464 case convolve(fcInfinity, fcInfinity):
1465 case convolve(fcZero, fcZero):
1466 makeNaN();
1467 return opInvalidOp;
1468
1469 case convolve(fcNormal, fcNormal):
1470 return opOK;
1471 }
1472}
1473
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001474/* Change sign. */
1475void
1476APFloat::changeSign()
1477{
1478 /* Look mummy, this one's easy. */
1479 sign = !sign;
1480}
1481
Dale Johannesene15c2db2007-08-31 23:35:31 +00001482void
1483APFloat::clearSign()
1484{
1485 /* So is this one. */
1486 sign = 0;
1487}
1488
1489void
1490APFloat::copySign(const APFloat &rhs)
1491{
1492 /* And this one. */
1493 sign = rhs.sign;
1494}
1495
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001496/* Normalized addition or subtraction. */
1497APFloat::opStatus
1498APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth4f881702007-09-26 21:33:42 +00001499 bool subtract)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001500{
1501 opStatus fs;
1502
Neil Boothcaf19d72007-10-14 10:29:28 +00001503 assertArithmeticOK(*semantics);
1504
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001505 fs = addOrSubtractSpecials(rhs, subtract);
1506
1507 /* This return code means it was not a simple case. */
1508 if(fs == opDivByZero) {
1509 lostFraction lost_fraction;
1510
1511 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1512 fs = normalize(rounding_mode, lost_fraction);
1513
1514 /* Can only be zero if we lost no fraction. */
1515 assert(category != fcZero || lost_fraction == lfExactlyZero);
1516 }
1517
1518 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1519 positive zero unless rounding to minus infinity, except that
1520 adding two like-signed zeroes gives that zero. */
1521 if(category == fcZero) {
1522 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1523 sign = (rounding_mode == rmTowardNegative);
1524 }
1525
1526 return fs;
1527}
1528
1529/* Normalized addition. */
1530APFloat::opStatus
1531APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1532{
1533 return addOrSubtract(rhs, rounding_mode, false);
1534}
1535
1536/* Normalized subtraction. */
1537APFloat::opStatus
1538APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1539{
1540 return addOrSubtract(rhs, rounding_mode, true);
1541}
1542
1543/* Normalized multiply. */
1544APFloat::opStatus
1545APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1546{
1547 opStatus fs;
1548
Neil Boothcaf19d72007-10-14 10:29:28 +00001549 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001550 sign ^= rhs.sign;
1551 fs = multiplySpecials(rhs);
1552
1553 if(category == fcNormal) {
1554 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1555 fs = normalize(rounding_mode, lost_fraction);
1556 if(lost_fraction != lfExactlyZero)
1557 fs = (opStatus) (fs | opInexact);
1558 }
1559
1560 return fs;
1561}
1562
1563/* Normalized divide. */
1564APFloat::opStatus
1565APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1566{
1567 opStatus fs;
1568
Neil Boothcaf19d72007-10-14 10:29:28 +00001569 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001570 sign ^= rhs.sign;
1571 fs = divideSpecials(rhs);
1572
1573 if(category == fcNormal) {
1574 lostFraction lost_fraction = divideSignificand(rhs);
1575 fs = normalize(rounding_mode, lost_fraction);
1576 if(lost_fraction != lfExactlyZero)
1577 fs = (opStatus) (fs | opInexact);
1578 }
1579
1580 return fs;
1581}
1582
Dale Johannesen24b66a82009-01-20 18:35:05 +00001583/* Normalized remainder. This is not currently correct in all cases. */
1584APFloat::opStatus
1585APFloat::remainder(const APFloat &rhs)
1586{
1587 opStatus fs;
1588 APFloat V = *this;
1589 unsigned int origSign = sign;
1590
1591 assertArithmeticOK(*semantics);
1592 fs = V.divide(rhs, rmNearestTiesToEven);
1593 if (fs == opDivByZero)
1594 return fs;
1595
1596 int parts = partCount();
1597 integerPart *x = new integerPart[parts];
1598 bool ignored;
1599 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1600 rmNearestTiesToEven, &ignored);
1601 if (fs==opInvalidOp)
1602 return fs;
1603
1604 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1605 rmNearestTiesToEven);
1606 assert(fs==opOK); // should always work
1607
1608 fs = V.multiply(rhs, rmNearestTiesToEven);
1609 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1610
1611 fs = subtract(V, rmNearestTiesToEven);
1612 assert(fs==opOK || fs==opInexact); // likewise
1613
1614 if (isZero())
1615 sign = origSign; // IEEE754 requires this
1616 delete[] x;
1617 return fs;
1618}
1619
1620/* Normalized llvm frem (C fmod).
1621 This is not currently correct in all cases. */
Dale Johannesene15c2db2007-08-31 23:35:31 +00001622APFloat::opStatus
1623APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1624{
1625 opStatus fs;
Neil Boothcaf19d72007-10-14 10:29:28 +00001626 assertArithmeticOK(*semantics);
Dale Johannesened6af242009-01-21 00:35:19 +00001627 fs = modSpecials(rhs);
Dale Johannesene15c2db2007-08-31 23:35:31 +00001628
Dale Johannesened6af242009-01-21 00:35:19 +00001629 if (category == fcNormal && rhs.category == fcNormal) {
1630 APFloat V = *this;
1631 unsigned int origSign = sign;
Dale Johannesene15c2db2007-08-31 23:35:31 +00001632
Dale Johannesened6af242009-01-21 00:35:19 +00001633 fs = V.divide(rhs, rmNearestTiesToEven);
1634 if (fs == opDivByZero)
1635 return fs;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001636
Dale Johannesened6af242009-01-21 00:35:19 +00001637 int parts = partCount();
1638 integerPart *x = new integerPart[parts];
1639 bool ignored;
1640 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1641 rmTowardZero, &ignored);
1642 if (fs==opInvalidOp)
1643 return fs;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001644
Dale Johannesened6af242009-01-21 00:35:19 +00001645 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1646 rmNearestTiesToEven);
1647 assert(fs==opOK); // should always work
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00001648
Dale Johannesened6af242009-01-21 00:35:19 +00001649 fs = V.multiply(rhs, rounding_mode);
1650 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1651
1652 fs = subtract(V, rounding_mode);
1653 assert(fs==opOK || fs==opInexact); // likewise
1654
1655 if (isZero())
1656 sign = origSign; // IEEE754 requires this
1657 delete[] x;
1658 }
Dale Johannesene15c2db2007-08-31 23:35:31 +00001659 return fs;
1660}
1661
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001662/* Normalized fused-multiply-add. */
1663APFloat::opStatus
1664APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth4f881702007-09-26 21:33:42 +00001665 const APFloat &addend,
1666 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001667{
1668 opStatus fs;
1669
Neil Boothcaf19d72007-10-14 10:29:28 +00001670 assertArithmeticOK(*semantics);
1671
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001672 /* Post-multiplication sign, before addition. */
1673 sign ^= multiplicand.sign;
1674
1675 /* If and only if all arguments are normal do we need to do an
1676 extended-precision calculation. */
1677 if(category == fcNormal
1678 && multiplicand.category == fcNormal
1679 && addend.category == fcNormal) {
1680 lostFraction lost_fraction;
1681
1682 lost_fraction = multiplySignificand(multiplicand, &addend);
1683 fs = normalize(rounding_mode, lost_fraction);
1684 if(lost_fraction != lfExactlyZero)
1685 fs = (opStatus) (fs | opInexact);
1686
1687 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1688 positive zero unless rounding to minus infinity, except that
1689 adding two like-signed zeroes gives that zero. */
1690 if(category == fcZero && sign != addend.sign)
1691 sign = (rounding_mode == rmTowardNegative);
1692 } else {
1693 fs = multiplySpecials(multiplicand);
1694
1695 /* FS can only be opOK or opInvalidOp. There is no more work
1696 to do in the latter case. The IEEE-754R standard says it is
1697 implementation-defined in this case whether, if ADDEND is a
Dale Johanneseneaf08942007-08-31 04:03:46 +00001698 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001699
1700 If we need to do the addition we can do so with normal
1701 precision. */
1702 if(fs == opOK)
1703 fs = addOrSubtract(addend, rounding_mode, false);
1704 }
1705
1706 return fs;
1707}
1708
1709/* Comparison requires normalized numbers. */
1710APFloat::cmpResult
1711APFloat::compare(const APFloat &rhs) const
1712{
1713 cmpResult result;
1714
Neil Boothcaf19d72007-10-14 10:29:28 +00001715 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001716 assert(semantics == rhs.semantics);
1717
Mike Stumpf3dc0c02009-05-13 23:23:20 +00001718 switch (convolve(category, rhs.category)) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001719 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001720 llvm_unreachable(0);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001721
Dale Johanneseneaf08942007-08-31 04:03:46 +00001722 case convolve(fcNaN, fcZero):
1723 case convolve(fcNaN, fcNormal):
1724 case convolve(fcNaN, fcInfinity):
1725 case convolve(fcNaN, fcNaN):
1726 case convolve(fcZero, fcNaN):
1727 case convolve(fcNormal, fcNaN):
1728 case convolve(fcInfinity, fcNaN):
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001729 return cmpUnordered;
1730
1731 case convolve(fcInfinity, fcNormal):
1732 case convolve(fcInfinity, fcZero):
1733 case convolve(fcNormal, fcZero):
1734 if(sign)
1735 return cmpLessThan;
1736 else
1737 return cmpGreaterThan;
1738
1739 case convolve(fcNormal, fcInfinity):
1740 case convolve(fcZero, fcInfinity):
1741 case convolve(fcZero, fcNormal):
1742 if(rhs.sign)
1743 return cmpGreaterThan;
1744 else
1745 return cmpLessThan;
1746
1747 case convolve(fcInfinity, fcInfinity):
1748 if(sign == rhs.sign)
1749 return cmpEqual;
1750 else if(sign)
1751 return cmpLessThan;
1752 else
1753 return cmpGreaterThan;
1754
1755 case convolve(fcZero, fcZero):
1756 return cmpEqual;
1757
1758 case convolve(fcNormal, fcNormal):
1759 break;
1760 }
1761
1762 /* Two normal numbers. Do they have the same sign? */
1763 if(sign != rhs.sign) {
1764 if(sign)
1765 result = cmpLessThan;
1766 else
1767 result = cmpGreaterThan;
1768 } else {
1769 /* Compare absolute values; invert result if negative. */
1770 result = compareAbsoluteValue(rhs);
1771
1772 if(sign) {
1773 if(result == cmpLessThan)
Neil Booth4f881702007-09-26 21:33:42 +00001774 result = cmpGreaterThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001775 else if(result == cmpGreaterThan)
Neil Booth4f881702007-09-26 21:33:42 +00001776 result = cmpLessThan;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001777 }
1778 }
1779
1780 return result;
1781}
1782
Dale Johannesen23a98552008-10-09 23:00:39 +00001783/// APFloat::convert - convert a value of one floating point type to another.
1784/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1785/// records whether the transformation lost information, i.e. whether
1786/// converting the result back to the original type will produce the
1787/// original value (this is almost the same as return value==fsOK, but there
1788/// are edge cases where this is not so).
1789
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001790APFloat::opStatus
1791APFloat::convert(const fltSemantics &toSemantics,
Dale Johannesen23a98552008-10-09 23:00:39 +00001792 roundingMode rounding_mode, bool *losesInfo)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001793{
Neil Boothc8db43d2007-09-22 02:56:19 +00001794 lostFraction lostFraction;
1795 unsigned int newPartCount, oldPartCount;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001796 opStatus fs;
Neil Booth4f881702007-09-26 21:33:42 +00001797
Neil Boothcaf19d72007-10-14 10:29:28 +00001798 assertArithmeticOK(*semantics);
Dale Johannesen79f82f92008-04-20 01:34:03 +00001799 assertArithmeticOK(toSemantics);
Neil Boothc8db43d2007-09-22 02:56:19 +00001800 lostFraction = lfExactlyZero;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001801 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Boothc8db43d2007-09-22 02:56:19 +00001802 oldPartCount = partCount();
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001803
Neil Boothc8db43d2007-09-22 02:56:19 +00001804 /* Handle storage complications. If our new form is wider,
1805 re-allocate our bit pattern into wider storage. If it is
1806 narrower, we ignore the excess parts, but if narrowing to a
Dale Johannesen902ff942007-09-25 17:25:00 +00001807 single part we need to free the old storage.
1808 Be careful not to reference significandParts for zeroes
1809 and infinities, since it aborts. */
Neil Boothc8db43d2007-09-22 02:56:19 +00001810 if (newPartCount > oldPartCount) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001811 integerPart *newParts;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001812 newParts = new integerPart[newPartCount];
1813 APInt::tcSet(newParts, 0, newPartCount);
Dale Johannesen902ff942007-09-25 17:25:00 +00001814 if (category==fcNormal || category==fcNaN)
1815 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001816 freeSignificand();
1817 significand.parts = newParts;
Neil Boothc8db43d2007-09-22 02:56:19 +00001818 } else if (newPartCount < oldPartCount) {
1819 /* Capture any lost fraction through truncation of parts so we get
1820 correct rounding whilst normalizing. */
Dale Johannesen902ff942007-09-25 17:25:00 +00001821 if (category==fcNormal)
1822 lostFraction = lostFractionThroughTruncation
1823 (significandParts(), oldPartCount, toSemantics.precision);
1824 if (newPartCount == 1) {
1825 integerPart newPart = 0;
Neil Booth4f881702007-09-26 21:33:42 +00001826 if (category==fcNormal || category==fcNaN)
Dale Johannesen902ff942007-09-25 17:25:00 +00001827 newPart = significandParts()[0];
1828 freeSignificand();
1829 significand.part = newPart;
1830 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001831 }
1832
1833 if(category == fcNormal) {
1834 /* Re-interpret our bit-pattern. */
1835 exponent += toSemantics.precision - semantics->precision;
1836 semantics = &toSemantics;
Neil Boothc8db43d2007-09-22 02:56:19 +00001837 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen23a98552008-10-09 23:00:39 +00001838 *losesInfo = (fs != opOK);
Dale Johannesen902ff942007-09-25 17:25:00 +00001839 } else if (category == fcNaN) {
1840 int shift = toSemantics.precision - semantics->precision;
Dale Johannesenb63fa052008-01-31 18:34:01 +00001841 // Do this now so significandParts gets the right answer
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001842 const fltSemantics *oldSemantics = semantics;
Dale Johannesenb63fa052008-01-31 18:34:01 +00001843 semantics = &toSemantics;
Dale Johannesen23a98552008-10-09 23:00:39 +00001844 *losesInfo = false;
Dale Johannesen902ff942007-09-25 17:25:00 +00001845 // No normalization here, just truncate
1846 if (shift>0)
1847 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001848 else if (shift < 0) {
1849 unsigned ushift = -shift;
Dale Johannesen23a98552008-10-09 23:00:39 +00001850 // Figure out if we are losing information. This happens
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001851 // if are shifting out something other than 0s, or if the x87 long
1852 // double input did not have its integer bit set (pseudo-NaN), or if the
1853 // x87 long double input did not have its QNan bit set (because the x87
1854 // hardware sets this bit when converting a lower-precision NaN to
1855 // x87 long double).
1856 if (APInt::tcLSB(significandParts(), newPartCount) < ushift)
Dale Johannesen23a98552008-10-09 23:00:39 +00001857 *losesInfo = true;
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001858 if (oldSemantics == &APFloat::x87DoubleExtended &&
1859 (!(*significandParts() & 0x8000000000000000ULL) ||
1860 !(*significandParts() & 0x4000000000000000ULL)))
Dale Johannesen23a98552008-10-09 23:00:39 +00001861 *losesInfo = true;
Dale Johannesen2df5eec2008-10-06 22:59:10 +00001862 APInt::tcShiftRight(significandParts(), newPartCount, ushift);
1863 }
Dale Johannesen902ff942007-09-25 17:25:00 +00001864 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
1865 // does not give you back the same bits. This is dubious, and we
1866 // don't currently do it. You're really supposed to get
1867 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen23a98552008-10-09 23:00:39 +00001868 fs = opOK;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001869 } else {
1870 semantics = &toSemantics;
1871 fs = opOK;
Dale Johannesen23a98552008-10-09 23:00:39 +00001872 *losesInfo = false;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001873 }
1874
1875 return fs;
1876}
1877
1878/* Convert a floating point number to an integer according to the
1879 rounding mode. If the rounded integer value is out of range this
Neil Boothee7ae382007-11-01 22:43:37 +00001880 returns an invalid operation exception and the contents of the
1881 destination parts are unspecified. If the rounded value is in
Chris Lattnerb39cdde2007-08-20 22:49:32 +00001882 range but the floating point number is not the exact integer, the C
1883 standard doesn't require an inexact exception to be raised. IEEE
1884 854 does require it so we do that.
1885
1886 Note that for conversions to integer type the C standard requires
1887 round-to-zero to always be used. */
1888APFloat::opStatus
Neil Boothee7ae382007-11-01 22:43:37 +00001889APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
1890 bool isSigned,
Dale Johannesen23a98552008-10-09 23:00:39 +00001891 roundingMode rounding_mode,
1892 bool *isExact) const
Neil Boothee7ae382007-11-01 22:43:37 +00001893{
1894 lostFraction lost_fraction;
1895 const integerPart *src;
1896 unsigned int dstPartsCount, truncatedBits;
1897
Evan Cheng794a7db2008-11-26 01:11:57 +00001898 assertArithmeticOK(*semantics);
Neil Boothe3d936a2007-11-02 15:10:05 +00001899
Dale Johannesen23a98552008-10-09 23:00:39 +00001900 *isExact = false;
1901
Neil Boothee7ae382007-11-01 22:43:37 +00001902 /* Handle the three special cases first. */
1903 if(category == fcInfinity || category == fcNaN)
1904 return opInvalidOp;
1905
1906 dstPartsCount = partCountForBits(width);
1907
1908 if(category == fcZero) {
1909 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesene4a42452008-10-07 00:40:01 +00001910 // Negative zero can't be represented as an int.
Dale Johannesen23a98552008-10-09 23:00:39 +00001911 *isExact = !sign;
1912 return opOK;
Neil Boothee7ae382007-11-01 22:43:37 +00001913 }
1914
1915 src = significandParts();
1916
1917 /* Step 1: place our absolute value, with any fraction truncated, in
1918 the destination. */
1919 if (exponent < 0) {
1920 /* Our absolute value is less than one; truncate everything. */
1921 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen1f54f582009-01-19 21:17:05 +00001922 /* For exponent -1 the integer bit represents .5, look at that.
1923 For smaller exponents leftmost truncated bit is 0. */
1924 truncatedBits = semantics->precision -1U - exponent;
Neil Boothee7ae382007-11-01 22:43:37 +00001925 } else {
1926 /* We want the most significant (exponent + 1) bits; the rest are
1927 truncated. */
1928 unsigned int bits = exponent + 1U;
1929
1930 /* Hopelessly large in magnitude? */
1931 if (bits > width)
1932 return opInvalidOp;
1933
1934 if (bits < semantics->precision) {
1935 /* We truncate (semantics->precision - bits) bits. */
1936 truncatedBits = semantics->precision - bits;
1937 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
1938 } else {
1939 /* We want at least as many bits as are available. */
1940 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
1941 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
1942 truncatedBits = 0;
1943 }
1944 }
1945
1946 /* Step 2: work out any lost fraction, and increment the absolute
1947 value if we would round away from zero. */
1948 if (truncatedBits) {
1949 lost_fraction = lostFractionThroughTruncation(src, partCount(),
1950 truncatedBits);
1951 if (lost_fraction != lfExactlyZero
1952 && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
1953 if (APInt::tcIncrement(parts, dstPartsCount))
1954 return opInvalidOp; /* Overflow. */
1955 }
1956 } else {
1957 lost_fraction = lfExactlyZero;
1958 }
1959
1960 /* Step 3: check if we fit in the destination. */
1961 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
1962
1963 if (sign) {
1964 if (!isSigned) {
1965 /* Negative numbers cannot be represented as unsigned. */
1966 if (omsb != 0)
1967 return opInvalidOp;
1968 } else {
1969 /* It takes omsb bits to represent the unsigned integer value.
1970 We lose a bit for the sign, but care is needed as the
1971 maximally negative integer is a special case. */
1972 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
1973 return opInvalidOp;
1974
1975 /* This case can happen because of rounding. */
1976 if (omsb > width)
1977 return opInvalidOp;
1978 }
1979
1980 APInt::tcNegate (parts, dstPartsCount);
1981 } else {
1982 if (omsb >= width + !isSigned)
1983 return opInvalidOp;
1984 }
1985
Dale Johannesen23a98552008-10-09 23:00:39 +00001986 if (lost_fraction == lfExactlyZero) {
1987 *isExact = true;
Neil Boothee7ae382007-11-01 22:43:37 +00001988 return opOK;
Dale Johannesen23a98552008-10-09 23:00:39 +00001989 } else
Neil Boothee7ae382007-11-01 22:43:37 +00001990 return opInexact;
1991}
1992
1993/* Same as convertToSignExtendedInteger, except we provide
1994 deterministic values in case of an invalid operation exception,
1995 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen23a98552008-10-09 23:00:39 +00001996 for underflow or overflow.
1997 The *isExact output tells whether the result is exact, in the sense
1998 that converting it back to the original floating point type produces
1999 the original value. This is almost equivalent to result==opOK,
2000 except for negative zeroes.
2001*/
Neil Boothee7ae382007-11-01 22:43:37 +00002002APFloat::opStatus
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002003APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth4f881702007-09-26 21:33:42 +00002004 bool isSigned,
Dale Johannesen23a98552008-10-09 23:00:39 +00002005 roundingMode rounding_mode, bool *isExact) const
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002006{
Neil Boothee7ae382007-11-01 22:43:37 +00002007 opStatus fs;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002008
Dale Johannesen23a98552008-10-09 23:00:39 +00002009 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2010 isExact);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002011
Neil Boothee7ae382007-11-01 22:43:37 +00002012 if (fs == opInvalidOp) {
2013 unsigned int bits, dstPartsCount;
2014
2015 dstPartsCount = partCountForBits(width);
2016
2017 if (category == fcNaN)
2018 bits = 0;
2019 else if (sign)
2020 bits = isSigned;
2021 else
2022 bits = width - isSigned;
2023
2024 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2025 if (sign && isSigned)
2026 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002027 }
2028
Neil Boothee7ae382007-11-01 22:43:37 +00002029 return fs;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002030}
2031
Neil Booth643ce592007-10-07 12:07:53 +00002032/* Convert an unsigned integer SRC to a floating point number,
2033 rounding according to ROUNDING_MODE. The sign of the floating
2034 point number is not modified. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002035APFloat::opStatus
Neil Booth643ce592007-10-07 12:07:53 +00002036APFloat::convertFromUnsignedParts(const integerPart *src,
2037 unsigned int srcCount,
2038 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002039{
Neil Booth5477f852007-10-08 14:39:42 +00002040 unsigned int omsb, precision, dstCount;
Neil Booth643ce592007-10-07 12:07:53 +00002041 integerPart *dst;
Neil Booth5477f852007-10-08 14:39:42 +00002042 lostFraction lost_fraction;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002043
Neil Boothcaf19d72007-10-14 10:29:28 +00002044 assertArithmeticOK(*semantics);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002045 category = fcNormal;
Neil Booth5477f852007-10-08 14:39:42 +00002046 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth643ce592007-10-07 12:07:53 +00002047 dst = significandParts();
2048 dstCount = partCount();
Neil Booth5477f852007-10-08 14:39:42 +00002049 precision = semantics->precision;
Neil Booth643ce592007-10-07 12:07:53 +00002050
Neil Booth5477f852007-10-08 14:39:42 +00002051 /* We want the most significant PRECISON bits of SRC. There may not
2052 be that many; extract what we can. */
2053 if (precision <= omsb) {
2054 exponent = omsb - 1;
Neil Booth643ce592007-10-07 12:07:53 +00002055 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth5477f852007-10-08 14:39:42 +00002056 omsb - precision);
2057 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2058 } else {
2059 exponent = precision - 1;
2060 lost_fraction = lfExactlyZero;
2061 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth643ce592007-10-07 12:07:53 +00002062 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002063
2064 return normalize(rounding_mode, lost_fraction);
2065}
2066
Dan Gohman93c276e2008-02-29 01:26:11 +00002067APFloat::opStatus
2068APFloat::convertFromAPInt(const APInt &Val,
2069 bool isSigned,
2070 roundingMode rounding_mode)
2071{
2072 unsigned int partCount = Val.getNumWords();
2073 APInt api = Val;
2074
2075 sign = false;
2076 if (isSigned && api.isNegative()) {
2077 sign = true;
2078 api = -api;
2079 }
2080
2081 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2082}
2083
Neil Boothf16c5952007-10-07 12:15:41 +00002084/* Convert a two's complement integer SRC to a floating point number,
2085 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2086 integer is signed, in which case it must be sign-extended. */
2087APFloat::opStatus
2088APFloat::convertFromSignExtendedInteger(const integerPart *src,
2089 unsigned int srcCount,
2090 bool isSigned,
2091 roundingMode rounding_mode)
2092{
2093 opStatus status;
2094
Neil Boothcaf19d72007-10-14 10:29:28 +00002095 assertArithmeticOK(*semantics);
Neil Boothf16c5952007-10-07 12:15:41 +00002096 if (isSigned
2097 && APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2098 integerPart *copy;
2099
2100 /* If we're signed and negative negate a copy. */
2101 sign = true;
2102 copy = new integerPart[srcCount];
2103 APInt::tcAssign(copy, src, srcCount);
2104 APInt::tcNegate(copy, srcCount);
2105 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2106 delete [] copy;
2107 } else {
2108 sign = false;
2109 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2110 }
2111
2112 return status;
2113}
2114
Neil Boothccf596a2007-10-07 11:45:55 +00002115/* FIXME: should this just take a const APInt reference? */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002116APFloat::opStatus
Neil Boothccf596a2007-10-07 11:45:55 +00002117APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2118 unsigned int width, bool isSigned,
2119 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002120{
Dale Johannesen910993e2007-09-21 22:09:37 +00002121 unsigned int partCount = partCountForBits(width);
Dale Johannesen910993e2007-09-21 22:09:37 +00002122 APInt api = APInt(width, partCount, parts);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002123
2124 sign = false;
Dale Johannesencce23a42007-09-30 18:17:01 +00002125 if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
2126 sign = true;
2127 api = -api;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002128 }
2129
Neil Booth7a7bc0f2007-10-07 12:10:57 +00002130 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002131}
2132
2133APFloat::opStatus
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002134APFloat::convertFromHexadecimalString(const StringRef &s,
Neil Booth4f881702007-09-26 21:33:42 +00002135 roundingMode rounding_mode)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002136{
Erick Tryzelaarf8bc8012009-08-18 18:20:37 +00002137 lostFraction lost_fraction = lfExactlyZero;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002138 integerPart *significand;
2139 unsigned int bitPos, partsCount;
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002140 StringRef::iterator dot, firstSignificantDigit;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002141
2142 zeroSignificand();
2143 exponent = 0;
2144 category = fcNormal;
2145
2146 significand = significandParts();
2147 partsCount = partCount();
2148 bitPos = partsCount * integerPartWidth;
2149
Neil Booth33d4c922007-10-07 08:51:21 +00002150 /* Skip leading zeroes and any (hexa)decimal point. */
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002151 StringRef::iterator begin = s.begin();
2152 StringRef::iterator end = s.end();
2153 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002154 firstSignificantDigit = p;
2155
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002156 for(; p != end;) {
Dale Johannesen386f3e92008-05-14 22:53:25 +00002157 integerPart hex_value;
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002158
2159 if(*p == '.') {
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002160 assert(dot == end && "String contains multiple dots");
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002161 dot = p++;
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002162 if (p == end) {
2163 break;
2164 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002165 }
2166
2167 hex_value = hexDigitValue(*p);
2168 if(hex_value == -1U) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002169 break;
2170 }
2171
2172 p++;
2173
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002174 if (p == end) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002175 break;
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002176 } else {
2177 /* Store the number whilst 4-bit nibbles remain. */
2178 if(bitPos) {
2179 bitPos -= 4;
2180 hex_value <<= bitPos % integerPartWidth;
2181 significand[bitPos / integerPartWidth] |= hex_value;
2182 } else {
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002183 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2184 while(p != end && hexDigitValue(*p) != -1U)
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002185 p++;
2186 break;
2187 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002188 }
2189 }
2190
2191 /* Hex floats require an exponent but not a hexadecimal point. */
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002192 assert(p != end && "Hex strings require an exponent");
2193 assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2194 assert(p != begin && "Significand has no digits");
2195 assert((dot == end || p - begin != 1) && "Significand has no digits");
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002196
2197 /* Ignore the exponent if we are zero. */
2198 if(p != firstSignificantDigit) {
2199 int expAdjustment;
2200
2201 /* Implicit hexadecimal point? */
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002202 if (dot == end)
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002203 dot = p;
2204
2205 /* Calculate the exponent adjustment implicit in the number of
2206 significant digits. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002207 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002208 if(expAdjustment < 0)
2209 expAdjustment++;
2210 expAdjustment = expAdjustment * 4 - 1;
2211
2212 /* Adjust for writing the significand starting at the most
2213 significant nibble. */
2214 expAdjustment += semantics->precision;
2215 expAdjustment -= partsCount * integerPartWidth;
2216
2217 /* Adjust for the given exponent. */
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002218 exponent = totalExponent(p + 1, end, expAdjustment);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002219 }
2220
2221 return normalize(rounding_mode, lost_fraction);
2222}
2223
2224APFloat::opStatus
Neil Booth96c74712007-10-12 16:02:31 +00002225APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2226 unsigned sigPartCount, int exp,
2227 roundingMode rounding_mode)
2228{
2229 unsigned int parts, pow5PartCount;
Neil Boothcaf19d72007-10-14 10:29:28 +00002230 fltSemantics calcSemantics = { 32767, -32767, 0, true };
Neil Booth96c74712007-10-12 16:02:31 +00002231 integerPart pow5Parts[maxPowerOfFiveParts];
2232 bool isNearest;
2233
2234 isNearest = (rounding_mode == rmNearestTiesToEven
2235 || rounding_mode == rmNearestTiesToAway);
2236
2237 parts = partCountForBits(semantics->precision + 11);
2238
2239 /* Calculate pow(5, abs(exp)). */
2240 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2241
2242 for (;; parts *= 2) {
2243 opStatus sigStatus, powStatus;
2244 unsigned int excessPrecision, truncatedBits;
2245
2246 calcSemantics.precision = parts * integerPartWidth - 1;
2247 excessPrecision = calcSemantics.precision - semantics->precision;
2248 truncatedBits = excessPrecision;
2249
2250 APFloat decSig(calcSemantics, fcZero, sign);
2251 APFloat pow5(calcSemantics, fcZero, false);
2252
2253 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2254 rmNearestTiesToEven);
2255 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2256 rmNearestTiesToEven);
2257 /* Add exp, as 10^n = 5^n * 2^n. */
2258 decSig.exponent += exp;
2259
2260 lostFraction calcLostFraction;
Evan Cheng48e8c802008-05-02 21:15:08 +00002261 integerPart HUerr, HUdistance;
2262 unsigned int powHUerr;
Neil Booth96c74712007-10-12 16:02:31 +00002263
2264 if (exp >= 0) {
2265 /* multiplySignificand leaves the precision-th bit set to 1. */
2266 calcLostFraction = decSig.multiplySignificand(pow5, NULL);
2267 powHUerr = powStatus != opOK;
2268 } else {
2269 calcLostFraction = decSig.divideSignificand(pow5);
2270 /* Denormal numbers have less precision. */
2271 if (decSig.exponent < semantics->minExponent) {
2272 excessPrecision += (semantics->minExponent - decSig.exponent);
2273 truncatedBits = excessPrecision;
2274 if (excessPrecision > calcSemantics.precision)
2275 excessPrecision = calcSemantics.precision;
2276 }
2277 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002278 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Booth96c74712007-10-12 16:02:31 +00002279 }
2280
2281 /* Both multiplySignificand and divideSignificand return the
2282 result with the integer bit set. */
2283 assert (APInt::tcExtractBit
2284 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2285
2286 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2287 powHUerr);
2288 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2289 excessPrecision, isNearest);
2290
2291 /* Are we guaranteed to round correctly if we truncate? */
2292 if (HUdistance >= HUerr) {
2293 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2294 calcSemantics.precision - excessPrecision,
2295 excessPrecision);
2296 /* Take the exponent of decSig. If we tcExtract-ed less bits
2297 above we must adjust our exponent to compensate for the
2298 implicit right shift. */
2299 exponent = (decSig.exponent + semantics->precision
2300 - (calcSemantics.precision - excessPrecision));
2301 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2302 decSig.partCount(),
2303 truncatedBits);
2304 return normalize(rounding_mode, calcLostFraction);
2305 }
2306 }
2307}
2308
2309APFloat::opStatus
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002310APFloat::convertFromDecimalString(const StringRef &str, roundingMode rounding_mode)
Neil Booth96c74712007-10-12 16:02:31 +00002311{
Neil Booth1870f292007-10-14 10:16:12 +00002312 decimalInfo D;
Neil Booth96c74712007-10-12 16:02:31 +00002313 opStatus fs;
2314
Neil Booth1870f292007-10-14 10:16:12 +00002315 /* Scan the text. */
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002316 StringRef::iterator p = str.begin();
2317 interpretDecimal(p, str.end(), &D);
Neil Booth96c74712007-10-12 16:02:31 +00002318
Neil Booth686700e2007-10-15 15:00:55 +00002319 /* Handle the quick cases. First the case of no significant digits,
2320 i.e. zero, and then exponents that are obviously too large or too
2321 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2322 definitely overflows if
2323
2324 (exp - 1) * L >= maxExponent
2325
2326 and definitely underflows to zero where
2327
2328 (exp + 1) * L <= minExponent - precision
2329
2330 With integer arithmetic the tightest bounds for L are
2331
2332 93/28 < L < 196/59 [ numerator <= 256 ]
2333 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2334 */
2335
Neil Boothcc233592007-12-05 13:06:04 +00002336 if (decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Booth96c74712007-10-12 16:02:31 +00002337 category = fcZero;
2338 fs = opOK;
Neil Booth686700e2007-10-15 15:00:55 +00002339 } else if ((D.normalizedExponent + 1) * 28738
2340 <= 8651 * (semantics->minExponent - (int) semantics->precision)) {
2341 /* Underflow to zero and round. */
2342 zeroSignificand();
2343 fs = normalize(rounding_mode, lfLessThanHalf);
2344 } else if ((D.normalizedExponent - 1) * 42039
2345 >= 12655 * semantics->maxExponent) {
2346 /* Overflow and round. */
2347 fs = handleOverflow(rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002348 } else {
Neil Booth1870f292007-10-14 10:16:12 +00002349 integerPart *decSignificand;
2350 unsigned int partCount;
Neil Booth96c74712007-10-12 16:02:31 +00002351
Neil Booth1870f292007-10-14 10:16:12 +00002352 /* A tight upper bound on number of bits required to hold an
Neil Booth686700e2007-10-15 15:00:55 +00002353 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth1870f292007-10-14 10:16:12 +00002354 to hold the full significand, and an extra part required by
2355 tcMultiplyPart. */
Evan Cheng48e8c802008-05-02 21:15:08 +00002356 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth686700e2007-10-15 15:00:55 +00002357 partCount = partCountForBits(1 + 196 * partCount / 59);
Neil Booth1870f292007-10-14 10:16:12 +00002358 decSignificand = new integerPart[partCount + 1];
2359 partCount = 0;
Neil Booth96c74712007-10-12 16:02:31 +00002360
Neil Booth1870f292007-10-14 10:16:12 +00002361 /* Convert to binary efficiently - we do almost all multiplication
2362 in an integerPart. When this would overflow do we do a single
2363 bignum multiplication, and then revert again to multiplication
2364 in an integerPart. */
2365 do {
2366 integerPart decValue, val, multiplier;
2367
2368 val = 0;
2369 multiplier = 1;
2370
2371 do {
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002372 if (*p == '.') {
Neil Booth1870f292007-10-14 10:16:12 +00002373 p++;
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002374 if (p == str.end()) {
2375 break;
2376 }
2377 }
Neil Booth1870f292007-10-14 10:16:12 +00002378 decValue = decDigitValue(*p++);
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002379 assert(decValue < 10U && "Invalid character in significand");
Neil Booth1870f292007-10-14 10:16:12 +00002380 multiplier *= 10;
2381 val = val * 10 + decValue;
2382 /* The maximum number that can be multiplied by ten with any
2383 digit added without overflowing an integerPart. */
2384 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2385
2386 /* Multiply out the current part. */
2387 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2388 partCount, partCount + 1, false);
2389
2390 /* If we used another part (likely but not guaranteed), increase
2391 the count. */
2392 if (decSignificand[partCount])
2393 partCount++;
2394 } while (p <= D.lastSigDigit);
Neil Booth96c74712007-10-12 16:02:31 +00002395
Neil Booth43a4b282007-11-01 22:51:07 +00002396 category = fcNormal;
Neil Booth96c74712007-10-12 16:02:31 +00002397 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth1870f292007-10-14 10:16:12 +00002398 D.exponent, rounding_mode);
Neil Booth96c74712007-10-12 16:02:31 +00002399
Neil Booth1870f292007-10-14 10:16:12 +00002400 delete [] decSignificand;
2401 }
Neil Booth96c74712007-10-12 16:02:31 +00002402
2403 return fs;
2404}
2405
2406APFloat::opStatus
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002407APFloat::convertFromString(const StringRef &str, roundingMode rounding_mode)
Neil Booth4f881702007-09-26 21:33:42 +00002408{
Neil Boothcaf19d72007-10-14 10:29:28 +00002409 assertArithmeticOK(*semantics);
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002410 assert(!str.empty() && "Invalid string length");
Neil Boothcaf19d72007-10-14 10:29:28 +00002411
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002412 /* Handle a leading minus sign. */
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002413 StringRef::iterator p = str.begin();
2414 size_t slen = str.size();
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002415 sign = *p == '-' ? 1 : 0;
2416 if(*p == '-' || *p == '+') {
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002417 p++;
2418 slen--;
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002419 assert(slen && "String has no digits");
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002420 }
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002421
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002422 if(slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2423 assert(slen - 2 && "Invalid string");
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002424 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002425 rounding_mode);
2426 }
Bill Wendlingb7c0d942008-11-27 08:00:12 +00002427
Erick Tryzelaarc78b33b2009-08-20 23:30:43 +00002428 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002429}
Dale Johannesen343e7702007-08-24 00:56:33 +00002430
Neil Bootha30b0ee2007-10-03 22:26:02 +00002431/* Write out a hexadecimal representation of the floating point value
2432 to DST, which must be of sufficient size, in the C99 form
2433 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2434 excluding the terminating NUL.
2435
2436 If UPPERCASE, the output is in upper case, otherwise in lower case.
2437
2438 HEXDIGITS digits appear altogether, rounding the value if
2439 necessary. If HEXDIGITS is 0, the minimal precision to display the
2440 number precisely is used instead. If nothing would appear after
2441 the decimal point it is suppressed.
2442
2443 The decimal exponent is always printed and has at least one digit.
2444 Zero values display an exponent of zero. Infinities and NaNs
2445 appear as "infinity" or "nan" respectively.
2446
2447 The above rules are as specified by C99. There is ambiguity about
2448 what the leading hexadecimal digit should be. This implementation
2449 uses whatever is necessary so that the exponent is displayed as
2450 stored. This implies the exponent will fall within the IEEE format
2451 range, and the leading hexadecimal digit will be 0 (for denormals),
2452 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2453 any other digits zero).
2454*/
2455unsigned int
2456APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2457 bool upperCase, roundingMode rounding_mode) const
2458{
2459 char *p;
2460
Neil Boothcaf19d72007-10-14 10:29:28 +00002461 assertArithmeticOK(*semantics);
2462
Neil Bootha30b0ee2007-10-03 22:26:02 +00002463 p = dst;
2464 if (sign)
2465 *dst++ = '-';
2466
2467 switch (category) {
2468 case fcInfinity:
2469 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2470 dst += sizeof infinityL - 1;
2471 break;
2472
2473 case fcNaN:
2474 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2475 dst += sizeof NaNU - 1;
2476 break;
2477
2478 case fcZero:
2479 *dst++ = '0';
2480 *dst++ = upperCase ? 'X': 'x';
2481 *dst++ = '0';
2482 if (hexDigits > 1) {
2483 *dst++ = '.';
2484 memset (dst, '0', hexDigits - 1);
2485 dst += hexDigits - 1;
2486 }
2487 *dst++ = upperCase ? 'P': 'p';
2488 *dst++ = '0';
2489 break;
2490
2491 case fcNormal:
2492 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2493 break;
2494 }
2495
2496 *dst = 0;
2497
Evan Cheng48e8c802008-05-02 21:15:08 +00002498 return static_cast<unsigned int>(dst - p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002499}
2500
2501/* Does the hard work of outputting the correctly rounded hexadecimal
2502 form of a normal floating point number with the specified number of
2503 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2504 digits necessary to print the value precisely is output. */
2505char *
2506APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2507 bool upperCase,
2508 roundingMode rounding_mode) const
2509{
2510 unsigned int count, valueBits, shift, partsCount, outputDigits;
2511 const char *hexDigitChars;
2512 const integerPart *significand;
2513 char *p;
2514 bool roundUp;
2515
2516 *dst++ = '0';
2517 *dst++ = upperCase ? 'X': 'x';
2518
2519 roundUp = false;
2520 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2521
2522 significand = significandParts();
2523 partsCount = partCount();
2524
2525 /* +3 because the first digit only uses the single integer bit, so
2526 we have 3 virtual zero most-significant-bits. */
2527 valueBits = semantics->precision + 3;
2528 shift = integerPartWidth - valueBits % integerPartWidth;
2529
2530 /* The natural number of digits required ignoring trailing
2531 insignificant zeroes. */
2532 outputDigits = (valueBits - significandLSB () + 3) / 4;
2533
2534 /* hexDigits of zero means use the required number for the
2535 precision. Otherwise, see if we are truncating. If we are,
Neil Booth978661d2007-10-06 00:24:48 +00002536 find out if we need to round away from zero. */
Neil Bootha30b0ee2007-10-03 22:26:02 +00002537 if (hexDigits) {
2538 if (hexDigits < outputDigits) {
2539 /* We are dropping non-zero bits, so need to check how to round.
2540 "bits" is the number of dropped bits. */
2541 unsigned int bits;
2542 lostFraction fraction;
2543
2544 bits = valueBits - hexDigits * 4;
2545 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2546 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2547 }
2548 outputDigits = hexDigits;
2549 }
2550
2551 /* Write the digits consecutively, and start writing in the location
2552 of the hexadecimal point. We move the most significant digit
2553 left and add the hexadecimal point later. */
2554 p = ++dst;
2555
2556 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2557
2558 while (outputDigits && count) {
2559 integerPart part;
2560
2561 /* Put the most significant integerPartWidth bits in "part". */
2562 if (--count == partsCount)
2563 part = 0; /* An imaginary higher zero part. */
2564 else
2565 part = significand[count] << shift;
2566
2567 if (count && shift)
2568 part |= significand[count - 1] >> (integerPartWidth - shift);
2569
2570 /* Convert as much of "part" to hexdigits as we can. */
2571 unsigned int curDigits = integerPartWidth / 4;
2572
2573 if (curDigits > outputDigits)
2574 curDigits = outputDigits;
2575 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2576 outputDigits -= curDigits;
2577 }
2578
2579 if (roundUp) {
2580 char *q = dst;
2581
2582 /* Note that hexDigitChars has a trailing '0'. */
2583 do {
2584 q--;
2585 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth978661d2007-10-06 00:24:48 +00002586 } while (*q == '0');
2587 assert (q >= p);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002588 } else {
2589 /* Add trailing zeroes. */
2590 memset (dst, '0', outputDigits);
2591 dst += outputDigits;
2592 }
2593
2594 /* Move the most significant digit to before the point, and if there
2595 is something after the decimal point add it. This must come
2596 after rounding above. */
2597 p[-1] = p[0];
2598 if (dst -1 == p)
2599 dst--;
2600 else
2601 p[0] = '.';
2602
2603 /* Finally output the exponent. */
2604 *dst++ = upperCase ? 'P': 'p';
2605
Neil Booth92f7e8d2007-10-06 07:29:25 +00002606 return writeSignedDecimal (dst, exponent);
Neil Bootha30b0ee2007-10-03 22:26:02 +00002607}
2608
Dale Johannesen343e7702007-08-24 00:56:33 +00002609// For good performance it is desirable for different APFloats
2610// to produce different integers.
2611uint32_t
Neil Booth4f881702007-09-26 21:33:42 +00002612APFloat::getHashValue() const
2613{
Dale Johannesen343e7702007-08-24 00:56:33 +00002614 if (category==fcZero) return sign<<8 | semantics->precision ;
2615 else if (category==fcInfinity) return sign<<9 | semantics->precision;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002616 else if (category==fcNaN) return 1<<10 | semantics->precision;
Dale Johannesen343e7702007-08-24 00:56:33 +00002617 else {
2618 uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
2619 const integerPart* p = significandParts();
2620 for (int i=partCount(); i>0; i--, p++)
Evan Cheng48e8c802008-05-02 21:15:08 +00002621 hash ^= ((uint32_t)*p) ^ (uint32_t)((*p)>>32);
Dale Johannesen343e7702007-08-24 00:56:33 +00002622 return hash;
2623 }
2624}
2625
2626// Conversion from APFloat to/from host float/double. It may eventually be
2627// possible to eliminate these and have everybody deal with APFloats, but that
2628// will take a while. This approach will not easily extend to long double.
Dale Johannesena72a5a02007-09-20 23:47:58 +00002629// Current implementation requires integerPartWidth==64, which is correct at
2630// the moment but could be made more general.
Dale Johannesen343e7702007-08-24 00:56:33 +00002631
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002632// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesena72a5a02007-09-20 23:47:58 +00002633// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002634
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002635APInt
Neil Booth4f881702007-09-26 21:33:42 +00002636APFloat::convertF80LongDoubleAPFloatToAPInt() const
2637{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002638 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002639 assert (partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002640
2641 uint64_t myexponent, mysignificand;
2642
2643 if (category==fcNormal) {
2644 myexponent = exponent+16383; //bias
Dale Johannesena72a5a02007-09-20 23:47:58 +00002645 mysignificand = significandParts()[0];
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002646 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2647 myexponent = 0; // denormal
2648 } else if (category==fcZero) {
2649 myexponent = 0;
2650 mysignificand = 0;
2651 } else if (category==fcInfinity) {
2652 myexponent = 0x7fff;
2653 mysignificand = 0x8000000000000000ULL;
Chris Lattnera11ef822007-10-06 06:13:42 +00002654 } else {
2655 assert(category == fcNaN && "Unknown category");
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002656 myexponent = 0x7fff;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002657 mysignificand = significandParts()[0];
Chris Lattnera11ef822007-10-06 06:13:42 +00002658 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002659
2660 uint64_t words[2];
Dale Johannesen1b25cb22009-03-23 21:16:53 +00002661 words[0] = mysignificand;
2662 words[1] = ((uint64_t)(sign & 1) << 15) |
2663 (myexponent & 0x7fffLL);
Chris Lattnera11ef822007-10-06 06:13:42 +00002664 return APInt(80, 2, words);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002665}
2666
2667APInt
Dale Johannesena471c2e2007-10-11 18:07:22 +00002668APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2669{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002670 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002671 assert (partCount()==2);
2672
2673 uint64_t myexponent, mysignificand, myexponent2, mysignificand2;
2674
2675 if (category==fcNormal) {
2676 myexponent = exponent + 1023; //bias
2677 myexponent2 = exponent2 + 1023;
2678 mysignificand = significandParts()[0];
2679 mysignificand2 = significandParts()[1];
2680 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2681 myexponent = 0; // denormal
2682 if (myexponent2==1 && !(mysignificand2 & 0x10000000000000LL))
2683 myexponent2 = 0; // denormal
2684 } else if (category==fcZero) {
2685 myexponent = 0;
2686 mysignificand = 0;
2687 myexponent2 = 0;
2688 mysignificand2 = 0;
2689 } else if (category==fcInfinity) {
2690 myexponent = 0x7ff;
2691 myexponent2 = 0;
2692 mysignificand = 0;
2693 mysignificand2 = 0;
2694 } else {
2695 assert(category == fcNaN && "Unknown category");
2696 myexponent = 0x7ff;
2697 mysignificand = significandParts()[0];
2698 myexponent2 = exponent2;
2699 mysignificand2 = significandParts()[1];
2700 }
2701
2702 uint64_t words[2];
Evan Cheng48e8c802008-05-02 21:15:08 +00002703 words[0] = ((uint64_t)(sign & 1) << 63) |
Dale Johannesena471c2e2007-10-11 18:07:22 +00002704 ((myexponent & 0x7ff) << 52) |
2705 (mysignificand & 0xfffffffffffffLL);
Evan Cheng48e8c802008-05-02 21:15:08 +00002706 words[1] = ((uint64_t)(sign2 & 1) << 63) |
Dale Johannesena471c2e2007-10-11 18:07:22 +00002707 ((myexponent2 & 0x7ff) << 52) |
2708 (mysignificand2 & 0xfffffffffffffLL);
2709 return APInt(128, 2, words);
2710}
2711
2712APInt
Neil Booth4f881702007-09-26 21:33:42 +00002713APFloat::convertDoubleAPFloatToAPInt() const
2714{
Dan Gohmancb648f92007-09-14 20:08:19 +00002715 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002716 assert (partCount()==1);
2717
Dale Johanneseneaf08942007-08-31 04:03:46 +00002718 uint64_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002719
2720 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002721 myexponent = exponent+1023; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002722 mysignificand = *significandParts();
2723 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2724 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002725 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002726 myexponent = 0;
2727 mysignificand = 0;
2728 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002729 myexponent = 0x7ff;
2730 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002731 } else {
2732 assert(category == fcNaN && "Unknown category!");
Dale Johannesen343e7702007-08-24 00:56:33 +00002733 myexponent = 0x7ff;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002734 mysignificand = *significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002735 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002736
Evan Cheng48e8c802008-05-02 21:15:08 +00002737 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattnera11ef822007-10-06 06:13:42 +00002738 ((myexponent & 0x7ff) << 52) |
2739 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesen343e7702007-08-24 00:56:33 +00002740}
2741
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002742APInt
Neil Booth4f881702007-09-26 21:33:42 +00002743APFloat::convertFloatAPFloatToAPInt() const
2744{
Dan Gohmancb648f92007-09-14 20:08:19 +00002745 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002746 assert (partCount()==1);
Neil Booth4f881702007-09-26 21:33:42 +00002747
Dale Johanneseneaf08942007-08-31 04:03:46 +00002748 uint32_t myexponent, mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002749
2750 if (category==fcNormal) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002751 myexponent = exponent+127; //bias
Evan Cheng48e8c802008-05-02 21:15:08 +00002752 mysignificand = (uint32_t)*significandParts();
Dale Johannesend0763b92007-11-17 01:02:27 +00002753 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002754 myexponent = 0; // denormal
Dale Johannesen343e7702007-08-24 00:56:33 +00002755 } else if (category==fcZero) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002756 myexponent = 0;
2757 mysignificand = 0;
2758 } else if (category==fcInfinity) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002759 myexponent = 0xff;
2760 mysignificand = 0;
Chris Lattnera11ef822007-10-06 06:13:42 +00002761 } else {
2762 assert(category == fcNaN && "Unknown category!");
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002763 myexponent = 0xff;
Evan Cheng48e8c802008-05-02 21:15:08 +00002764 mysignificand = (uint32_t)*significandParts();
Chris Lattnera11ef822007-10-06 06:13:42 +00002765 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002766
Chris Lattnera11ef822007-10-06 06:13:42 +00002767 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2768 (mysignificand & 0x7fffff)));
Dale Johannesen343e7702007-08-24 00:56:33 +00002769}
2770
Dale Johannesena471c2e2007-10-11 18:07:22 +00002771// This function creates an APInt that is just a bit map of the floating
2772// point constant as it would appear in memory. It is not a conversion,
2773// and treating the result as a normal integer is unlikely to be useful.
2774
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002775APInt
Dale Johannesen7111b022008-10-09 18:53:47 +00002776APFloat::bitcastToAPInt() const
Neil Booth4f881702007-09-26 21:33:42 +00002777{
Dan Gohmanb10abe12008-01-29 12:08:20 +00002778 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002779 return convertFloatAPFloatToAPInt();
Chris Lattnera11ef822007-10-06 06:13:42 +00002780
Dan Gohmanb10abe12008-01-29 12:08:20 +00002781 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002782 return convertDoubleAPFloatToAPInt();
Neil Booth4f881702007-09-26 21:33:42 +00002783
Dan Gohmanb10abe12008-01-29 12:08:20 +00002784 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
Dale Johannesena471c2e2007-10-11 18:07:22 +00002785 return convertPPCDoubleDoubleAPFloatToAPInt();
2786
Dan Gohmanb10abe12008-01-29 12:08:20 +00002787 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
Chris Lattnera11ef822007-10-06 06:13:42 +00002788 "unknown format!");
2789 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002790}
2791
Neil Booth4f881702007-09-26 21:33:42 +00002792float
2793APFloat::convertToFloat() const
2794{
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002795 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle && "Float semantics are not IEEEsingle");
Dale Johannesen7111b022008-10-09 18:53:47 +00002796 APInt api = bitcastToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002797 return api.bitsToFloat();
2798}
2799
Neil Booth4f881702007-09-26 21:33:42 +00002800double
2801APFloat::convertToDouble() const
2802{
Erick Tryzelaara15d8902009-08-16 23:36:19 +00002803 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble && "Float semantics are not IEEEdouble");
Dale Johannesen7111b022008-10-09 18:53:47 +00002804 APInt api = bitcastToAPInt();
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002805 return api.bitsToDouble();
2806}
2807
Dale Johannesend3d8ce32008-10-06 18:22:29 +00002808/// Integer bit is explicit in this format. Intel hardware (387 and later)
2809/// does not support these bit patterns:
2810/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
2811/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
2812/// exponent = 0, integer bit 1 ("pseudodenormal")
2813/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
2814/// At the moment, the first two are treated as NaNs, the second two as Normal.
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002815void
Neil Booth4f881702007-09-26 21:33:42 +00002816APFloat::initFromF80LongDoubleAPInt(const APInt &api)
2817{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002818 assert(api.getBitWidth()==80);
2819 uint64_t i1 = api.getRawData()[0];
2820 uint64_t i2 = api.getRawData()[1];
Dale Johannesen1b25cb22009-03-23 21:16:53 +00002821 uint64_t myexponent = (i2 & 0x7fff);
2822 uint64_t mysignificand = i1;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002823
2824 initialize(&APFloat::x87DoubleExtended);
Dale Johannesena72a5a02007-09-20 23:47:58 +00002825 assert(partCount()==2);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002826
Dale Johannesen1b25cb22009-03-23 21:16:53 +00002827 sign = static_cast<unsigned int>(i2>>15);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002828 if (myexponent==0 && mysignificand==0) {
2829 // exponent, significand meaningless
2830 category = fcZero;
2831 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
2832 // exponent, significand meaningless
2833 category = fcInfinity;
2834 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
2835 // exponent meaningless
2836 category = fcNaN;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002837 significandParts()[0] = mysignificand;
2838 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002839 } else {
2840 category = fcNormal;
2841 exponent = myexponent - 16383;
Dale Johannesena72a5a02007-09-20 23:47:58 +00002842 significandParts()[0] = mysignificand;
2843 significandParts()[1] = 0;
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002844 if (myexponent==0) // denormal
2845 exponent = -16382;
Neil Booth4f881702007-09-26 21:33:42 +00002846 }
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002847}
2848
2849void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002850APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
2851{
2852 assert(api.getBitWidth()==128);
2853 uint64_t i1 = api.getRawData()[0];
2854 uint64_t i2 = api.getRawData()[1];
2855 uint64_t myexponent = (i1 >> 52) & 0x7ff;
2856 uint64_t mysignificand = i1 & 0xfffffffffffffLL;
2857 uint64_t myexponent2 = (i2 >> 52) & 0x7ff;
2858 uint64_t mysignificand2 = i2 & 0xfffffffffffffLL;
2859
2860 initialize(&APFloat::PPCDoubleDouble);
2861 assert(partCount()==2);
2862
Evan Cheng48e8c802008-05-02 21:15:08 +00002863 sign = static_cast<unsigned int>(i1>>63);
2864 sign2 = static_cast<unsigned int>(i2>>63);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002865 if (myexponent==0 && mysignificand==0) {
2866 // exponent, significand meaningless
2867 // exponent2 and significand2 are required to be 0; we don't check
2868 category = fcZero;
2869 } else if (myexponent==0x7ff && mysignificand==0) {
2870 // exponent, significand meaningless
2871 // exponent2 and significand2 are required to be 0; we don't check
2872 category = fcInfinity;
2873 } else if (myexponent==0x7ff && mysignificand!=0) {
2874 // exponent meaningless. So is the whole second word, but keep it
2875 // for determinism.
2876 category = fcNaN;
2877 exponent2 = myexponent2;
2878 significandParts()[0] = mysignificand;
2879 significandParts()[1] = mysignificand2;
2880 } else {
2881 category = fcNormal;
2882 // Note there is no category2; the second word is treated as if it is
2883 // fcNormal, although it might be something else considered by itself.
2884 exponent = myexponent - 1023;
2885 exponent2 = myexponent2 - 1023;
2886 significandParts()[0] = mysignificand;
2887 significandParts()[1] = mysignificand2;
2888 if (myexponent==0) // denormal
2889 exponent = -1022;
2890 else
2891 significandParts()[0] |= 0x10000000000000LL; // integer bit
2892 if (myexponent2==0)
2893 exponent2 = -1022;
2894 else
2895 significandParts()[1] |= 0x10000000000000LL; // integer bit
2896 }
2897}
2898
2899void
Neil Booth4f881702007-09-26 21:33:42 +00002900APFloat::initFromDoubleAPInt(const APInt &api)
2901{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002902 assert(api.getBitWidth()==64);
2903 uint64_t i = *api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002904 uint64_t myexponent = (i >> 52) & 0x7ff;
2905 uint64_t mysignificand = i & 0xfffffffffffffLL;
2906
Dale Johannesen343e7702007-08-24 00:56:33 +00002907 initialize(&APFloat::IEEEdouble);
Dale Johannesen343e7702007-08-24 00:56:33 +00002908 assert(partCount()==1);
2909
Evan Cheng48e8c802008-05-02 21:15:08 +00002910 sign = static_cast<unsigned int>(i>>63);
Dale Johannesen343e7702007-08-24 00:56:33 +00002911 if (myexponent==0 && mysignificand==0) {
2912 // exponent, significand meaningless
2913 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002914 } else if (myexponent==0x7ff && mysignificand==0) {
2915 // exponent, significand meaningless
2916 category = fcInfinity;
Dale Johanneseneaf08942007-08-31 04:03:46 +00002917 } else if (myexponent==0x7ff && mysignificand!=0) {
2918 // exponent meaningless
2919 category = fcNaN;
2920 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002921 } else {
Dale Johannesen343e7702007-08-24 00:56:33 +00002922 category = fcNormal;
2923 exponent = myexponent - 1023;
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002924 *significandParts() = mysignificand;
2925 if (myexponent==0) // denormal
2926 exponent = -1022;
2927 else
2928 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth4f881702007-09-26 21:33:42 +00002929 }
Dale Johannesen343e7702007-08-24 00:56:33 +00002930}
2931
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002932void
Neil Booth4f881702007-09-26 21:33:42 +00002933APFloat::initFromFloatAPInt(const APInt & api)
2934{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002935 assert(api.getBitWidth()==32);
2936 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesend3b51fd2007-08-24 05:08:11 +00002937 uint32_t myexponent = (i >> 23) & 0xff;
2938 uint32_t mysignificand = i & 0x7fffff;
2939
Dale Johannesen343e7702007-08-24 00:56:33 +00002940 initialize(&APFloat::IEEEsingle);
Dale Johannesen343e7702007-08-24 00:56:33 +00002941 assert(partCount()==1);
2942
Dale Johanneseneaf08942007-08-31 04:03:46 +00002943 sign = i >> 31;
Dale Johannesen343e7702007-08-24 00:56:33 +00002944 if (myexponent==0 && mysignificand==0) {
2945 // exponent, significand meaningless
2946 category = fcZero;
Dale Johannesen343e7702007-08-24 00:56:33 +00002947 } else if (myexponent==0xff && mysignificand==0) {
2948 // exponent, significand meaningless
2949 category = fcInfinity;
Dale Johannesen902ff942007-09-25 17:25:00 +00002950 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesen343e7702007-08-24 00:56:33 +00002951 // sign, exponent, significand meaningless
Dale Johanneseneaf08942007-08-31 04:03:46 +00002952 category = fcNaN;
2953 *significandParts() = mysignificand;
Dale Johannesen343e7702007-08-24 00:56:33 +00002954 } else {
2955 category = fcNormal;
Dale Johannesen343e7702007-08-24 00:56:33 +00002956 exponent = myexponent - 127; //bias
Dale Johannesen58c2e4c2007-09-05 20:39:49 +00002957 *significandParts() = mysignificand;
2958 if (myexponent==0) // denormal
2959 exponent = -126;
2960 else
2961 *significandParts() |= 0x800000; // integer bit
Dale Johannesen343e7702007-08-24 00:56:33 +00002962 }
2963}
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002964
2965/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesena471c2e2007-10-11 18:07:22 +00002966/// we infer the floating point type from the size of the APInt. The
2967/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
2968/// when the size is anything else).
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002969void
Dale Johannesena471c2e2007-10-11 18:07:22 +00002970APFloat::initFromAPInt(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002971{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002972 if (api.getBitWidth() == 32)
2973 return initFromFloatAPInt(api);
2974 else if (api.getBitWidth()==64)
2975 return initFromDoubleAPInt(api);
2976 else if (api.getBitWidth()==80)
2977 return initFromF80LongDoubleAPInt(api);
Dale Johannesena471c2e2007-10-11 18:07:22 +00002978 else if (api.getBitWidth()==128 && !isIEEE)
2979 return initFromPPCDoubleDoubleAPInt(api);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002980 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002981 llvm_unreachable(0);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002982}
2983
Dale Johannesena471c2e2007-10-11 18:07:22 +00002984APFloat::APFloat(const APInt& api, bool isIEEE)
Neil Booth4f881702007-09-26 21:33:42 +00002985{
Dale Johannesena471c2e2007-10-11 18:07:22 +00002986 initFromAPInt(api, isIEEE);
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002987}
2988
Neil Booth4f881702007-09-26 21:33:42 +00002989APFloat::APFloat(float f)
2990{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002991 APInt api = APInt(32, 0);
2992 initFromAPInt(api.floatToBits(f));
2993}
2994
Neil Booth4f881702007-09-26 21:33:42 +00002995APFloat::APFloat(double d)
2996{
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002997 APInt api = APInt(64, 0);
2998 initFromAPInt(api.doubleToBits(d));
2999}