blob: 19b8221b60cb55389d7c9858fe93d0595c5e2c54 [file] [log] [blame]
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001//===-- APFloat.cpp - Implement APFloat class -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnerfe02c1f2007-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 Lattnere5256752007-12-08 19:00:03 +000015#include "llvm/ADT/APFloat.h"
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +000016#include "llvm/ADT/APSInt.h"
Ted Kremenek6f30a072008-02-11 17:24:50 +000017#include "llvm/ADT/FoldingSet.h"
Chandler Carruth71bd7d12012-03-04 12:02:57 +000018#include "llvm/ADT/Hashing.h"
Jordan Rosee1f76582013-01-18 21:45:30 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth71bd7d12012-03-04 12:02:57 +000020#include "llvm/ADT/StringRef.h"
Torok Edwin56d06592009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Dale Johannesen918c33c2007-08-24 05:08:11 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner17f71652008-08-17 07:19:36 +000023#include <cstring>
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include <limits.h>
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000025
26using namespace llvm;
27
Michael Gottesman9b877e12013-06-24 09:57:57 +000028/// A macro used to combine two fcCategory enums into one key which can be used
29/// in a switch statement to classify how the interaction of two APFloat's
30/// categories affects an operation.
31///
32/// TODO: If clang source code is ever allowed to use constexpr in its own
33/// codebase, change this into a static inline function.
34#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000035
Neil Booth8f1946f2007-10-03 22:26:02 +000036/* Assumed in hexadecimal significand parsing, and conversion to
37 hexadecimal strings. */
Benjamin Kramer7000ca32014-10-12 17:56:40 +000038static_assert(integerPartWidth % 4 == 0, "Part width must be divisible by 4!");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000039
40namespace llvm {
41
42 /* Represents floating point arithmetic semantics. */
43 struct fltSemantics {
44 /* The largest E such that 2^E is representable; this matches the
45 definition of IEEE 754. */
Michael Gottesman9dc98332013-06-24 04:06:23 +000046 APFloat::ExponentType maxExponent;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000047
48 /* The smallest E such that 2^E is a normalized number; this
49 matches the definition of IEEE 754. */
Michael Gottesman9dc98332013-06-24 04:06:23 +000050 APFloat::ExponentType minExponent;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000051
52 /* Number of bits in the significand. This includes the integer
53 bit. */
Neil Booth146fdb32007-10-12 15:33:27 +000054 unsigned int precision;
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000055
56 /* Number of bits actually used in the semantics. */
57 unsigned int sizeInBits;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000058 };
59
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000060 const fltSemantics APFloat::IEEEhalf = { 15, -14, 11, 16 };
61 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, 32 };
62 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, 64 };
63 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, 128 };
64 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, 80 };
65 const fltSemantics APFloat::Bogus = { 0, 0, 0, 0 };
Dale Johannesen007aa372007-10-11 18:07:22 +000066
Ulrich Weigandd9f7e252012-10-29 18:09:01 +000067 /* The PowerPC format consists of two doubles. It does not map cleanly
68 onto the usual format above. It is approximated using twice the
69 mantissa bits. Note that for exponents near the double minimum,
70 we no longer can represent the full 106 mantissa bits, so those
71 will be treated as denormal numbers.
72
73 FIXME: While this approximation is equivalent to what GCC uses for
74 compile-time arithmetic on PPC double-double numbers, it is not able
75 to represent all possible values held by a PPC double-double number,
76 for example: (long double) 1.0 + (long double) 0x1p-106
77 Should this be replaced by a full emulation of PPC double-double? */
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000078 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53, 128 };
Neil Boothb93d90e2007-10-12 16:02:31 +000079
80 /* A tight upper bound on number of parts required to hold the value
81 pow(5, power) is
82
Neil Booth91305512007-10-15 15:00:55 +000083 power * 815 / (351 * integerPartWidth) + 1
Dan Gohmanb452d4e2010-03-24 19:38:02 +000084
Neil Boothb93d90e2007-10-12 16:02:31 +000085 However, whilst the result may require only this many parts,
86 because we are multiplying two values to get it, the
87 multiplication may require an extra part with the excess part
88 being zero (consider the trivial case of 1 * 1, tcFullMultiply
89 requires two parts to hold the single-part result). So we add an
90 extra one to guarantee enough space whilst multiplying. */
91 const unsigned int maxExponent = 16383;
92 const unsigned int maxPrecision = 113;
93 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
Neil Booth91305512007-10-15 15:00:55 +000094 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
95 / (351 * integerPartWidth));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000096}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000097
Chris Lattner91702092009-03-12 23:59:55 +000098/* A bunch of private, handy routines. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000099
Chris Lattner91702092009-03-12 23:59:55 +0000100static inline unsigned int
101partCountForBits(unsigned int bits)
102{
103 return ((bits) + integerPartWidth - 1) / integerPartWidth;
104}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000105
Chris Lattner91702092009-03-12 23:59:55 +0000106/* Returns 0U-9U. Return values >= 10U are not digits. */
107static inline unsigned int
108decDigitValue(unsigned int c)
109{
110 return c - '0';
111}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000112
Chris Lattner91702092009-03-12 23:59:55 +0000113/* Return the value of a decimal exponent of the form
114 [+-]ddddddd.
Neil Booth4ed401b2007-10-14 10:16:12 +0000115
Chris Lattner91702092009-03-12 23:59:55 +0000116 If the exponent overflows, returns a large exponent with the
117 appropriate sign. */
118static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000119readExponent(StringRef::iterator begin, StringRef::iterator end)
Chris Lattner91702092009-03-12 23:59:55 +0000120{
121 bool isNegative;
122 unsigned int absExponent;
123 const unsigned int overlargeExponent = 24000; /* FIXME. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000124 StringRef::iterator p = begin;
125
126 assert(p != end && "Exponent has no digits");
Neil Booth4ed401b2007-10-14 10:16:12 +0000127
Chris Lattner91702092009-03-12 23:59:55 +0000128 isNegative = (*p == '-');
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000129 if (*p == '-' || *p == '+') {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000130 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000131 assert(p != end && "Exponent has no digits");
132 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000133
Chris Lattner91702092009-03-12 23:59:55 +0000134 absExponent = decDigitValue(*p++);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000135 assert(absExponent < 10U && "Invalid character in exponent");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000136
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000137 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000138 unsigned int value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000139
Chris Lattner91702092009-03-12 23:59:55 +0000140 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000141 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000142
Chris Lattner91702092009-03-12 23:59:55 +0000143 value += absExponent * 10;
144 if (absExponent >= overlargeExponent) {
145 absExponent = overlargeExponent;
Dale Johannesen370c77c2010-08-19 17:58:35 +0000146 p = end; /* outwit assert below */
Chris Lattner91702092009-03-12 23:59:55 +0000147 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000148 }
Chris Lattner91702092009-03-12 23:59:55 +0000149 absExponent = value;
150 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000151
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000152 assert(p == end && "Invalid exponent in exponent");
153
Chris Lattner91702092009-03-12 23:59:55 +0000154 if (isNegative)
155 return -(int) absExponent;
156 else
157 return (int) absExponent;
158}
159
160/* This is ugly and needs cleaning up, but I don't immediately see
161 how whilst remaining safe. */
162static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000163totalExponent(StringRef::iterator p, StringRef::iterator end,
164 int exponentAdjustment)
Chris Lattner91702092009-03-12 23:59:55 +0000165{
166 int unsignedExponent;
167 bool negative, overflow;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000168 int exponent = 0;
Chris Lattner91702092009-03-12 23:59:55 +0000169
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000170 assert(p != end && "Exponent has no digits");
171
Chris Lattner91702092009-03-12 23:59:55 +0000172 negative = *p == '-';
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000173 if (*p == '-' || *p == '+') {
Chris Lattner91702092009-03-12 23:59:55 +0000174 p++;
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000175 assert(p != end && "Exponent has no digits");
176 }
Chris Lattner91702092009-03-12 23:59:55 +0000177
178 unsignedExponent = 0;
179 overflow = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000180 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000181 unsigned int value;
182
183 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000184 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000185
Chris Lattner91702092009-03-12 23:59:55 +0000186 unsignedExponent = unsignedExponent * 10 + value;
Richard Smith156d9202012-08-24 00:01:19 +0000187 if (unsignedExponent > 32767) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000188 overflow = true;
Richard Smith156d9202012-08-24 00:01:19 +0000189 break;
190 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000191 }
192
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000193 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000194 overflow = true;
195
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000196 if (!overflow) {
Chris Lattner91702092009-03-12 23:59:55 +0000197 exponent = unsignedExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000198 if (negative)
Chris Lattner91702092009-03-12 23:59:55 +0000199 exponent = -exponent;
200 exponent += exponentAdjustment;
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000201 if (exponent > 32767 || exponent < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000202 overflow = true;
203 }
204
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000205 if (overflow)
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000206 exponent = negative ? -32768: 32767;
Chris Lattner91702092009-03-12 23:59:55 +0000207
208 return exponent;
209}
210
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000211static StringRef::iterator
212skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
213 StringRef::iterator *dot)
Chris Lattner91702092009-03-12 23:59:55 +0000214{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000215 StringRef::iterator p = begin;
216 *dot = end;
Nick Lewycky095b92e2014-09-06 01:16:42 +0000217 while (p != end && *p == '0')
Chris Lattner91702092009-03-12 23:59:55 +0000218 p++;
219
Nick Lewycky095b92e2014-09-06 01:16:42 +0000220 if (p != end && *p == '.') {
Chris Lattner91702092009-03-12 23:59:55 +0000221 *dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000222
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000223 assert(end - begin != 1 && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000224
Nick Lewycky095b92e2014-09-06 01:16:42 +0000225 while (p != end && *p == '0')
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000226 p++;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000227 }
228
Chris Lattner91702092009-03-12 23:59:55 +0000229 return p;
230}
Neil Booth4ed401b2007-10-14 10:16:12 +0000231
Chris Lattner91702092009-03-12 23:59:55 +0000232/* Given a normal decimal floating point number of the form
Neil Booth4ed401b2007-10-14 10:16:12 +0000233
Chris Lattner91702092009-03-12 23:59:55 +0000234 dddd.dddd[eE][+-]ddd
Neil Booth91305512007-10-15 15:00:55 +0000235
Chris Lattner91702092009-03-12 23:59:55 +0000236 where the decimal point and exponent are optional, fill out the
237 structure D. Exponent is appropriate if the significand is
238 treated as an integer, and normalizedExponent if the significand
239 is taken to have the decimal point after a single leading
240 non-zero digit.
Neil Booth4ed401b2007-10-14 10:16:12 +0000241
Chris Lattner91702092009-03-12 23:59:55 +0000242 If the value is zero, V->firstSigDigit points to a non-digit, and
243 the return exponent is zero.
244*/
245struct decimalInfo {
246 const char *firstSigDigit;
247 const char *lastSigDigit;
248 int exponent;
249 int normalizedExponent;
250};
Neil Booth4ed401b2007-10-14 10:16:12 +0000251
Chris Lattner91702092009-03-12 23:59:55 +0000252static void
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000253interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
254 decimalInfo *D)
Chris Lattner91702092009-03-12 23:59:55 +0000255{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000256 StringRef::iterator dot = end;
257 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
Neil Booth4ed401b2007-10-14 10:16:12 +0000258
Chris Lattner91702092009-03-12 23:59:55 +0000259 D->firstSigDigit = p;
260 D->exponent = 0;
261 D->normalizedExponent = 0;
262
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000263 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000264 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000265 assert(dot == end && "String contains multiple dots");
Chris Lattner91702092009-03-12 23:59:55 +0000266 dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000267 if (p == end)
268 break;
Neil Booth4ed401b2007-10-14 10:16:12 +0000269 }
Chris Lattner91702092009-03-12 23:59:55 +0000270 if (decDigitValue(*p) >= 10U)
271 break;
Chris Lattner91702092009-03-12 23:59:55 +0000272 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000273
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000274 if (p != end) {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000275 assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
276 assert(p != begin && "Significand has no digits");
277 assert((dot == end || p - begin != 1) && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000278
279 /* p points to the first non-digit in the string */
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000280 D->exponent = readExponent(p + 1, end);
Neil Booth4ed401b2007-10-14 10:16:12 +0000281
Chris Lattner91702092009-03-12 23:59:55 +0000282 /* Implied decimal point? */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000283 if (dot == end)
Chris Lattner91702092009-03-12 23:59:55 +0000284 dot = p;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000285 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000286
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000287 /* If number is all zeroes accept any exponent. */
288 if (p != D->firstSigDigit) {
Chris Lattner91702092009-03-12 23:59:55 +0000289 /* Drop insignificant trailing zeroes. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000290 if (p != begin) {
Neil Booth4ed401b2007-10-14 10:16:12 +0000291 do
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000292 do
293 p--;
294 while (p != begin && *p == '0');
295 while (p != begin && *p == '.');
296 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000297
Chris Lattner91702092009-03-12 23:59:55 +0000298 /* Adjust the exponents for any decimal point. */
Michael Gottesman9dc98332013-06-24 04:06:23 +0000299 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
Chris Lattner91702092009-03-12 23:59:55 +0000300 D->normalizedExponent = (D->exponent +
Michael Gottesman9dc98332013-06-24 04:06:23 +0000301 static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
Chris Lattner91702092009-03-12 23:59:55 +0000302 - (dot > D->firstSigDigit && dot < p)));
Neil Booth4ed401b2007-10-14 10:16:12 +0000303 }
304
Chris Lattner91702092009-03-12 23:59:55 +0000305 D->lastSigDigit = p;
306}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000307
Chris Lattner91702092009-03-12 23:59:55 +0000308/* Return the trailing fraction of a hexadecimal number.
309 DIGITVALUE is the first hex digit of the fraction, P points to
310 the next digit. */
311static lostFraction
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000312trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
313 unsigned int digitValue)
Chris Lattner91702092009-03-12 23:59:55 +0000314{
315 unsigned int hexDigit;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000316
Chris Lattner91702092009-03-12 23:59:55 +0000317 /* If the first trailing digit isn't 0 or 8 we can work out the
318 fraction immediately. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000319 if (digitValue > 8)
Chris Lattner91702092009-03-12 23:59:55 +0000320 return lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000321 else if (digitValue < 8 && digitValue > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000322 return lfLessThanHalf;
Chris Lattner91702092009-03-12 23:59:55 +0000323
Eli Friedmand2eb07a2013-07-17 22:17:29 +0000324 // Otherwise we need to find the first non-zero digit.
325 while (p != end && (*p == '0' || *p == '.'))
Chris Lattner91702092009-03-12 23:59:55 +0000326 p++;
327
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000328 assert(p != end && "Invalid trailing hexadecimal fraction!");
329
Chris Lattner91702092009-03-12 23:59:55 +0000330 hexDigit = hexDigitValue(*p);
331
332 /* If we ran off the end it is exactly zero or one-half, otherwise
333 a little more. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000334 if (hexDigit == -1U)
Chris Lattner91702092009-03-12 23:59:55 +0000335 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
336 else
337 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
338}
339
340/* Return the fraction lost were a bignum truncated losing the least
341 significant BITS bits. */
342static lostFraction
343lostFractionThroughTruncation(const integerPart *parts,
344 unsigned int partCount,
345 unsigned int bits)
346{
347 unsigned int lsb;
348
349 lsb = APInt::tcLSB(parts, partCount);
350
351 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000352 if (bits <= lsb)
Chris Lattner91702092009-03-12 23:59:55 +0000353 return lfExactlyZero;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000354 if (bits == lsb + 1)
Chris Lattner91702092009-03-12 23:59:55 +0000355 return lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000356 if (bits <= partCount * integerPartWidth &&
357 APInt::tcExtractBit(parts, bits - 1))
Chris Lattner91702092009-03-12 23:59:55 +0000358 return lfMoreThanHalf;
359
360 return lfLessThanHalf;
361}
362
363/* Shift DST right BITS bits noting lost fraction. */
364static lostFraction
365shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
366{
367 lostFraction lost_fraction;
368
369 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
370
371 APInt::tcShiftRight(dst, parts, bits);
372
373 return lost_fraction;
374}
375
376/* Combine the effect of two lost fractions. */
377static lostFraction
378combineLostFractions(lostFraction moreSignificant,
379 lostFraction lessSignificant)
380{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000381 if (lessSignificant != lfExactlyZero) {
382 if (moreSignificant == lfExactlyZero)
Chris Lattner91702092009-03-12 23:59:55 +0000383 moreSignificant = lfLessThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000384 else if (moreSignificant == lfExactlyHalf)
Chris Lattner91702092009-03-12 23:59:55 +0000385 moreSignificant = lfMoreThanHalf;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000386 }
387
Chris Lattner91702092009-03-12 23:59:55 +0000388 return moreSignificant;
389}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000390
Chris Lattner91702092009-03-12 23:59:55 +0000391/* The error from the true value, in half-ulps, on multiplying two
392 floating point numbers, which differ from the value they
393 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
394 than the returned value.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000395
Chris Lattner91702092009-03-12 23:59:55 +0000396 See "How to Read Floating Point Numbers Accurately" by William D
397 Clinger. */
398static unsigned int
399HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
400{
401 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000402
Chris Lattner91702092009-03-12 23:59:55 +0000403 if (HUerr1 + HUerr2 == 0)
404 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
405 else
406 return inexactMultiply + 2 * (HUerr1 + HUerr2);
407}
Neil Booth8f1946f2007-10-03 22:26:02 +0000408
Chris Lattner91702092009-03-12 23:59:55 +0000409/* The number of ulps from the boundary (zero, or half if ISNEAREST)
410 when the least significant BITS are truncated. BITS cannot be
411 zero. */
412static integerPart
413ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
414{
415 unsigned int count, partBits;
416 integerPart part, boundary;
Neil Boothd3985922007-10-07 08:51:21 +0000417
Evan Cheng67c90212009-10-27 21:35:42 +0000418 assert(bits != 0);
Neil Booth8f1946f2007-10-03 22:26:02 +0000419
Chris Lattner91702092009-03-12 23:59:55 +0000420 bits--;
421 count = bits / integerPartWidth;
422 partBits = bits % integerPartWidth + 1;
Neil Boothb93d90e2007-10-12 16:02:31 +0000423
Chris Lattner91702092009-03-12 23:59:55 +0000424 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
Neil Boothb93d90e2007-10-12 16:02:31 +0000425
Chris Lattner91702092009-03-12 23:59:55 +0000426 if (isNearest)
427 boundary = (integerPart) 1 << (partBits - 1);
428 else
429 boundary = 0;
430
431 if (count == 0) {
432 if (part - boundary <= boundary - part)
433 return part - boundary;
Neil Boothb93d90e2007-10-12 16:02:31 +0000434 else
Chris Lattner91702092009-03-12 23:59:55 +0000435 return boundary - part;
Neil Boothb93d90e2007-10-12 16:02:31 +0000436 }
437
Chris Lattner91702092009-03-12 23:59:55 +0000438 if (part == boundary) {
439 while (--count)
440 if (parts[count])
441 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000442
Chris Lattner91702092009-03-12 23:59:55 +0000443 return parts[0];
444 } else if (part == boundary - 1) {
445 while (--count)
446 if (~parts[count])
447 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000448
Chris Lattner91702092009-03-12 23:59:55 +0000449 return -parts[0];
450 }
Neil Boothb93d90e2007-10-12 16:02:31 +0000451
Chris Lattner91702092009-03-12 23:59:55 +0000452 return ~(integerPart) 0; /* A lot. */
453}
Neil Boothb93d90e2007-10-12 16:02:31 +0000454
Chris Lattner91702092009-03-12 23:59:55 +0000455/* Place pow(5, power) in DST, and return the number of parts used.
456 DST must be at least one part larger than size of the answer. */
457static unsigned int
458powerOf5(integerPart *dst, unsigned int power)
459{
460 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
461 15625, 78125 };
Chris Lattnerb858c0e2009-03-13 00:24:01 +0000462 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
463 pow5s[0] = 78125 * 5;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000464
Chris Lattner0bf18692009-03-13 00:03:51 +0000465 unsigned int partsCount[16] = { 1 };
Chris Lattner91702092009-03-12 23:59:55 +0000466 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
467 unsigned int result;
Chris Lattner91702092009-03-12 23:59:55 +0000468 assert(power <= maxExponent);
469
470 p1 = dst;
471 p2 = scratch;
472
473 *p1 = firstEightPowers[power & 7];
474 power >>= 3;
475
476 result = 1;
477 pow5 = pow5s;
478
479 for (unsigned int n = 0; power; power >>= 1, n++) {
480 unsigned int pc;
481
482 pc = partsCount[n];
483
484 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
485 if (pc == 0) {
486 pc = partsCount[n - 1];
487 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
488 pc *= 2;
489 if (pow5[pc - 1] == 0)
490 pc--;
491 partsCount[n] = pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000492 }
493
Chris Lattner91702092009-03-12 23:59:55 +0000494 if (power & 1) {
495 integerPart *tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000496
Chris Lattner91702092009-03-12 23:59:55 +0000497 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
498 result += pc;
499 if (p2[result - 1] == 0)
500 result--;
Neil Boothb93d90e2007-10-12 16:02:31 +0000501
Chris Lattner91702092009-03-12 23:59:55 +0000502 /* Now result is in p1 with partsCount parts and p2 is scratch
503 space. */
504 tmp = p1, p1 = p2, p2 = tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000505 }
506
Chris Lattner91702092009-03-12 23:59:55 +0000507 pow5 += pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000508 }
509
Chris Lattner91702092009-03-12 23:59:55 +0000510 if (p1 != dst)
511 APInt::tcAssign(dst, p1, result);
Neil Boothb93d90e2007-10-12 16:02:31 +0000512
Chris Lattner91702092009-03-12 23:59:55 +0000513 return result;
514}
Neil Boothb93d90e2007-10-12 16:02:31 +0000515
Chris Lattner91702092009-03-12 23:59:55 +0000516/* Zero at the end to avoid modular arithmetic when adding one; used
517 when rounding up during hexadecimal output. */
518static const char hexDigitsLower[] = "0123456789abcdef0";
519static const char hexDigitsUpper[] = "0123456789ABCDEF0";
520static const char infinityL[] = "infinity";
521static const char infinityU[] = "INFINITY";
522static const char NaNL[] = "nan";
523static const char NaNU[] = "NAN";
Neil Boothb93d90e2007-10-12 16:02:31 +0000524
Chris Lattner91702092009-03-12 23:59:55 +0000525/* Write out an integerPart in hexadecimal, starting with the most
526 significant nibble. Write out exactly COUNT hexdigits, return
527 COUNT. */
528static unsigned int
529partAsHex (char *dst, integerPart part, unsigned int count,
530 const char *hexDigitChars)
531{
532 unsigned int result = count;
Neil Boothb93d90e2007-10-12 16:02:31 +0000533
Evan Cheng67c90212009-10-27 21:35:42 +0000534 assert(count != 0 && count <= integerPartWidth / 4);
Neil Boothb93d90e2007-10-12 16:02:31 +0000535
Chris Lattner91702092009-03-12 23:59:55 +0000536 part >>= (integerPartWidth - 4 * count);
537 while (count--) {
538 dst[count] = hexDigitChars[part & 0xf];
539 part >>= 4;
Neil Boothb93d90e2007-10-12 16:02:31 +0000540 }
541
Chris Lattner91702092009-03-12 23:59:55 +0000542 return result;
543}
Neil Booth8f1946f2007-10-03 22:26:02 +0000544
Chris Lattner91702092009-03-12 23:59:55 +0000545/* Write out an unsigned decimal integer. */
546static char *
547writeUnsignedDecimal (char *dst, unsigned int n)
548{
549 char buff[40], *p;
Neil Booth8f1946f2007-10-03 22:26:02 +0000550
Chris Lattner91702092009-03-12 23:59:55 +0000551 p = buff;
552 do
553 *p++ = '0' + n % 10;
554 while (n /= 10);
Neil Booth8f1946f2007-10-03 22:26:02 +0000555
Chris Lattner91702092009-03-12 23:59:55 +0000556 do
557 *dst++ = *--p;
558 while (p != buff);
Neil Booth8f1946f2007-10-03 22:26:02 +0000559
Chris Lattner91702092009-03-12 23:59:55 +0000560 return dst;
561}
Neil Booth8f1946f2007-10-03 22:26:02 +0000562
Chris Lattner91702092009-03-12 23:59:55 +0000563/* Write out a signed decimal integer. */
564static char *
565writeSignedDecimal (char *dst, int value)
566{
567 if (value < 0) {
568 *dst++ = '-';
569 dst = writeUnsignedDecimal(dst, -(unsigned) value);
570 } else
571 dst = writeUnsignedDecimal(dst, value);
Neil Booth8f1946f2007-10-03 22:26:02 +0000572
Chris Lattner91702092009-03-12 23:59:55 +0000573 return dst;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000574}
575
576/* Constructors. */
577void
578APFloat::initialize(const fltSemantics *ourSemantics)
579{
580 unsigned int count;
581
582 semantics = ourSemantics;
583 count = partCount();
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000584 if (count > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000585 significand.parts = new integerPart[count];
586}
587
588void
589APFloat::freeSignificand()
590{
Manuel Klimekd0cf5b22013-06-03 13:03:05 +0000591 if (needsCleanup())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000592 delete [] significand.parts;
593}
594
595void
596APFloat::assign(const APFloat &rhs)
597{
598 assert(semantics == rhs.semantics);
599
600 sign = rhs.sign;
601 category = rhs.category;
602 exponent = rhs.exponent;
Michael Gottesman8136c382013-06-26 23:17:28 +0000603 if (isFiniteNonZero() || category == fcNaN)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000604 copySignificand(rhs);
605}
606
607void
608APFloat::copySignificand(const APFloat &rhs)
609{
Michael Gottesman8136c382013-06-26 23:17:28 +0000610 assert(isFiniteNonZero() || category == fcNaN);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000611 assert(rhs.partCount() >= partCount());
612
613 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +0000614 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000615}
616
Neil Booth5fe658b2007-10-14 10:39:51 +0000617/* Make this number a NaN, with an arbitrary but deterministic value
Dale Johannesen1f864982009-01-21 20:32:55 +0000618 for the significand. If double or longer, this is a signalling NaN,
Mike Stump799bf582009-05-30 03:49:43 +0000619 which may not be ideal. If float, this is QNaN(0). */
John McCalldcb9a7a2010-02-28 02:51:25 +0000620void APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill)
Neil Booth5fe658b2007-10-14 10:39:51 +0000621{
622 category = fcNaN;
John McCalldcb9a7a2010-02-28 02:51:25 +0000623 sign = Negative;
624
John McCallc12b1332010-02-28 12:49:50 +0000625 integerPart *significand = significandParts();
626 unsigned numParts = partCount();
627
John McCalldcb9a7a2010-02-28 02:51:25 +0000628 // Set the significand bits to the fill.
John McCallc12b1332010-02-28 12:49:50 +0000629 if (!fill || fill->getNumWords() < numParts)
630 APInt::tcSet(significand, 0, numParts);
631 if (fill) {
John McCallc6dbe302010-03-01 18:38:45 +0000632 APInt::tcAssign(significand, fill->getRawData(),
633 std::min(fill->getNumWords(), numParts));
John McCallc12b1332010-02-28 12:49:50 +0000634
635 // Zero out the excess bits of the significand.
636 unsigned bitsToPreserve = semantics->precision - 1;
637 unsigned part = bitsToPreserve / 64;
638 bitsToPreserve %= 64;
639 significand[part] &= ((1ULL << bitsToPreserve) - 1);
640 for (part++; part != numParts; ++part)
641 significand[part] = 0;
642 }
643
644 unsigned QNaNBit = semantics->precision - 2;
John McCalldcb9a7a2010-02-28 02:51:25 +0000645
646 if (SNaN) {
647 // We always have to clear the QNaN bit to make it an SNaN.
John McCallc12b1332010-02-28 12:49:50 +0000648 APInt::tcClearBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000649
650 // If there are no bits set in the payload, we have to set
651 // *something* to make it a NaN instead of an infinity;
652 // conventionally, this is the next bit down from the QNaN bit.
John McCallc12b1332010-02-28 12:49:50 +0000653 if (APInt::tcIsZero(significand, numParts))
654 APInt::tcSetBit(significand, QNaNBit - 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000655 } else {
656 // We always have to set the QNaN bit to make it a QNaN.
John McCallc12b1332010-02-28 12:49:50 +0000657 APInt::tcSetBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000658 }
John McCallc12b1332010-02-28 12:49:50 +0000659
660 // For x87 extended precision, we want to make a NaN, not a
661 // pseudo-NaN. Maybe we should expose the ability to make
662 // pseudo-NaNs?
663 if (semantics == &APFloat::x87DoubleExtended)
664 APInt::tcSetBit(significand, QNaNBit + 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000665}
666
667APFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
668 const APInt *fill) {
669 APFloat value(Sem, uninitialized);
670 value.makeNaN(SNaN, Negative, fill);
671 return value;
Neil Booth5fe658b2007-10-14 10:39:51 +0000672}
673
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000674APFloat &
675APFloat::operator=(const APFloat &rhs)
676{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000677 if (this != &rhs) {
678 if (semantics != rhs.semantics) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000679 freeSignificand();
680 initialize(rhs.semantics);
681 }
682 assign(rhs);
683 }
684
685 return *this;
686}
687
Benjamin Kramer06f47782014-03-04 20:26:51 +0000688APFloat &
689APFloat::operator=(APFloat &&rhs) {
690 freeSignificand();
691
692 semantics = rhs.semantics;
693 significand = rhs.significand;
694 exponent = rhs.exponent;
695 category = rhs.category;
696 sign = rhs.sign;
697
698 rhs.semantics = &Bogus;
699 return *this;
700}
701
Dale Johannesena719a602007-08-24 00:56:33 +0000702bool
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000703APFloat::isDenormal() const {
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000704 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000705 (APInt::tcExtractBit(significandParts(),
706 semantics->precision - 1) == 0);
707}
708
709bool
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000710APFloat::isSmallest() const {
711 // The smallest number by magnitude in our format will be the smallest
Michael Gottesmana7cc1242013-06-19 07:34:21 +0000712 // denormal, i.e. the floating point number with exponent being minimum
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000713 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000714 return isFiniteNonZero() && exponent == semantics->minExponent &&
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000715 significandMSB() == 0;
716}
717
718bool APFloat::isSignificandAllOnes() const {
719 // Test if the significand excluding the integral bit is all ones. This allows
720 // us to test for binade boundaries.
721 const integerPart *Parts = significandParts();
722 const unsigned PartCount = partCount();
723 for (unsigned i = 0; i < PartCount - 1; i++)
724 if (~Parts[i])
725 return false;
726
727 // Set the unused high bits to all ones when we compare.
728 const unsigned NumHighBits =
729 PartCount*integerPartWidth - semantics->precision + 1;
730 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
731 "fill than integerPartWidth");
732 const integerPart HighBitFill =
733 ~integerPart(0) << (integerPartWidth - NumHighBits);
734 if (~(Parts[PartCount - 1] | HighBitFill))
735 return false;
736
737 return true;
738}
739
740bool APFloat::isSignificandAllZeros() const {
741 // Test if the significand excluding the integral bit is all zeros. This
742 // allows us to test for binade boundaries.
743 const integerPart *Parts = significandParts();
744 const unsigned PartCount = partCount();
745
746 for (unsigned i = 0; i < PartCount - 1; i++)
747 if (Parts[i])
748 return false;
749
750 const unsigned NumHighBits =
751 PartCount*integerPartWidth - semantics->precision + 1;
752 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
753 "clear than integerPartWidth");
754 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
755
756 if (Parts[PartCount - 1] & HighBitMask)
757 return false;
758
759 return true;
760}
761
762bool
763APFloat::isLargest() const {
764 // The largest number by magnitude in our format will be the floating point
765 // number with maximum exponent and with significand that is all ones.
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000766 return isFiniteNonZero() && exponent == semantics->maxExponent
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000767 && isSignificandAllOnes();
768}
769
770bool
Stephen Canon1bfc89b2015-11-16 21:52:48 +0000771APFloat::isInteger() const {
772 // This could be made more efficient; I'm going for obviously correct.
773 if (!isFinite()) return false;
774 APFloat truncated = *this;
775 truncated.roundToIntegral(rmTowardZero);
776 return compare(truncated) == cmpEqual;
777}
778
779bool
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000780APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesena719a602007-08-24 00:56:33 +0000781 if (this == &rhs)
782 return true;
783 if (semantics != rhs.semantics ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000784 category != rhs.category ||
785 sign != rhs.sign)
Dale Johannesena719a602007-08-24 00:56:33 +0000786 return false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000787 if (category==fcZero || category==fcInfinity)
Dale Johannesena719a602007-08-24 00:56:33 +0000788 return true;
Benjamin Kramer103fc942015-08-21 16:44:52 +0000789
790 if (isFiniteNonZero() && exponent != rhs.exponent)
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000791 return false;
Benjamin Kramer103fc942015-08-21 16:44:52 +0000792
793 return std::equal(significandParts(), significandParts() + partCount(),
794 rhs.significandParts());
Dale Johannesena719a602007-08-24 00:56:33 +0000795}
796
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000797APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000798 initialize(&ourSemantics);
799 sign = 0;
Michael Gottesman30a90eb2013-07-27 21:49:21 +0000800 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000801 zeroSignificand();
802 exponent = ourSemantics.precision - 1;
803 significandParts()[0] = value;
804 normalize(rmNearestTiesToEven, lfExactlyZero);
805}
806
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000807APFloat::APFloat(const fltSemantics &ourSemantics) {
Chris Lattnerac6271e2009-09-17 01:08:43 +0000808 initialize(&ourSemantics);
809 category = fcZero;
810 sign = false;
811}
812
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000813APFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
John McCalldcb9a7a2010-02-28 02:51:25 +0000814 // Allocates storage if necessary but does not initialize it.
815 initialize(&ourSemantics);
816}
Chris Lattnerac6271e2009-09-17 01:08:43 +0000817
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000818APFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000819 initialize(&ourSemantics);
820 convertFromString(text, rmNearestTiesToEven);
821}
822
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000823APFloat::APFloat(const APFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000824 initialize(rhs.semantics);
825 assign(rhs);
826}
827
Benjamin Kramer06f47782014-03-04 20:26:51 +0000828APFloat::APFloat(APFloat &&rhs) : semantics(&Bogus) {
829 *this = std::move(rhs);
830}
831
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000832APFloat::~APFloat()
833{
834 freeSignificand();
835}
836
Ted Kremenek6f30a072008-02-11 17:24:50 +0000837// Profile - This method 'profiles' an APFloat for use with FoldingSet.
838void APFloat::Profile(FoldingSetNodeID& ID) const {
Dale Johannesen54306fe2008-10-09 18:53:47 +0000839 ID.Add(bitcastToAPInt());
Ted Kremenek6f30a072008-02-11 17:24:50 +0000840}
841
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000842unsigned int
843APFloat::partCount() const
844{
Dale Johannesen146a0ea2007-09-20 23:47:58 +0000845 return partCountForBits(semantics->precision + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000846}
847
848unsigned int
849APFloat::semanticsPrecision(const fltSemantics &semantics)
850{
851 return semantics.precision;
852}
JF Bastiena1d3c242015-08-26 02:32:45 +0000853APFloat::ExponentType
854APFloat::semanticsMaxExponent(const fltSemantics &semantics)
855{
856 return semantics.maxExponent;
857}
858APFloat::ExponentType
859APFloat::semanticsMinExponent(const fltSemantics &semantics)
860{
861 return semantics.minExponent;
862}
863unsigned int
864APFloat::semanticsSizeInBits(const fltSemantics &semantics)
865{
866 return semantics.sizeInBits;
867}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000868
869const integerPart *
870APFloat::significandParts() const
871{
872 return const_cast<APFloat *>(this)->significandParts();
873}
874
875integerPart *
876APFloat::significandParts()
877{
Evan Cheng67c90212009-10-27 21:35:42 +0000878 if (partCount() > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000879 return significand.parts;
880 else
881 return &significand.part;
882}
883
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000884void
885APFloat::zeroSignificand()
886{
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000887 APInt::tcSet(significandParts(), 0, partCount());
888}
889
890/* Increment an fcNormal floating point number's significand. */
891void
892APFloat::incrementSignificand()
893{
894 integerPart carry;
895
896 carry = APInt::tcIncrement(significandParts(), partCount());
897
898 /* Our callers should never cause us to overflow. */
899 assert(carry == 0);
Duncan Sandsa41634e2011-08-12 14:54:45 +0000900 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000901}
902
903/* Add the significand of the RHS. Returns the carry flag. */
904integerPart
905APFloat::addSignificand(const APFloat &rhs)
906{
907 integerPart *parts;
908
909 parts = significandParts();
910
911 assert(semantics == rhs.semantics);
912 assert(exponent == rhs.exponent);
913
914 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
915}
916
917/* Subtract the significand of the RHS with a borrow flag. Returns
918 the borrow flag. */
919integerPart
920APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
921{
922 integerPart *parts;
923
924 parts = significandParts();
925
926 assert(semantics == rhs.semantics);
927 assert(exponent == rhs.exponent);
928
929 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth9acbf5a2007-09-26 21:33:42 +0000930 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000931}
932
933/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
934 on to the full-precision result of the multiplication. Returns the
935 lost fraction. */
936lostFraction
937APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
938{
Neil Booth9acbf5a2007-09-26 21:33:42 +0000939 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000940 unsigned int partsCount, newPartsCount, precision;
941 integerPart *lhsSignificand;
942 integerPart scratch[4];
943 integerPart *fullSignificand;
944 lostFraction lost_fraction;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000945 bool ignored;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000946
947 assert(semantics == rhs.semantics);
948
949 precision = semantics->precision;
Lang Hames56c0eb22014-11-19 19:15:41 +0000950
951 // Allocate space for twice as many bits as the original significand, plus one
952 // extra bit for the addition to overflow into.
953 newPartsCount = partCountForBits(precision * 2 + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000954
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000955 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000956 fullSignificand = new integerPart[newPartsCount];
957 else
958 fullSignificand = scratch;
959
960 lhsSignificand = significandParts();
961 partsCount = partCount();
962
963 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth0ea72a92007-10-06 00:24:48 +0000964 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000965
966 lost_fraction = lfExactlyZero;
967 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
968 exponent += rhs.exponent;
969
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000970 // Assume the operands involved in the multiplication are single-precision
971 // FP, and the two multiplicants are:
972 // *this = a23 . a22 ... a0 * 2^e1
973 // rhs = b23 . b22 ... b0 * 2^e2
974 // the result of multiplication is:
Lang Hames56c0eb22014-11-19 19:15:41 +0000975 // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2)
976 // Note that there are three significant bits at the left-hand side of the
977 // radix point: two for the multiplication, and an overflow bit for the
978 // addition (that will always be zero at this point). Move the radix point
979 // toward left by two bits, and adjust exponent accordingly.
980 exponent += 2;
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000981
Hal Finkel171c2ec2014-10-14 19:23:07 +0000982 if (addend && addend->isNonZero()) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000983 // The intermediate result of the multiplication has "2 * precision"
984 // signicant bit; adjust the addend to be consistent with mul result.
985 //
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000986 Significand savedSignificand = significand;
987 const fltSemantics *savedSemantics = semantics;
988 fltSemantics extendedSemantics;
989 opStatus status;
990 unsigned int extendedPrecision;
991
Lang Hames56c0eb22014-11-19 19:15:41 +0000992 // Normalize our MSB to one below the top bit to allow for overflow.
993 extendedPrecision = 2 * precision + 1;
994 if (omsb != extendedPrecision - 1) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000995 assert(extendedPrecision > omsb);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000996 APInt::tcShiftLeft(fullSignificand, newPartsCount,
Lang Hames56c0eb22014-11-19 19:15:41 +0000997 (extendedPrecision - 1) - omsb);
998 exponent -= (extendedPrecision - 1) - omsb;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000999 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001000
1001 /* Create new semantics. */
1002 extendedSemantics = *semantics;
1003 extendedSemantics.precision = extendedPrecision;
1004
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001005 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001006 significand.part = fullSignificand[0];
1007 else
1008 significand.parts = fullSignificand;
1009 semantics = &extendedSemantics;
1010
1011 APFloat extendedAddend(*addend);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001012 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001013 assert(status == opOK);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001014 (void)status;
Lang Hames56c0eb22014-11-19 19:15:41 +00001015
1016 // Shift the significand of the addend right by one bit. This guarantees
1017 // that the high bit of the significand is zero (same as fullSignificand),
1018 // so the addition will overflow (if it does overflow at all) into the top bit.
1019 lost_fraction = extendedAddend.shiftSignificandRight(1);
1020 assert(lost_fraction == lfExactlyZero &&
1021 "Lost precision while shifting addend for fused-multiply-add.");
1022
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001023 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1024
1025 /* Restore our state. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001026 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001027 fullSignificand[0] = significand.part;
1028 significand = savedSignificand;
1029 semantics = savedSemantics;
1030
1031 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1032 }
1033
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001034 // Convert the result having "2 * precision" significant-bits back to the one
1035 // having "precision" significant-bits. First, move the radix point from
1036 // poision "2*precision - 1" to "precision - 1". The exponent need to be
1037 // adjusted by "2*precision - 1" - "precision - 1" = "precision".
Lang Hames56c0eb22014-11-19 19:15:41 +00001038 exponent -= precision + 1;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001039
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001040 // In case MSB resides at the left-hand side of radix point, shift the
1041 // mantissa right by some amount to make sure the MSB reside right before
1042 // the radix point (i.e. "MSB . rest-significant-bits").
1043 //
1044 // Note that the result is not normalized when "omsb < precision". So, the
1045 // caller needs to call APFloat::normalize() if normalized value is expected.
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001046 if (omsb > precision) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001047 unsigned int bits, significantParts;
1048 lostFraction lf;
1049
1050 bits = omsb - precision;
1051 significantParts = partCountForBits(omsb);
1052 lf = shiftRight(fullSignificand, significantParts, bits);
1053 lost_fraction = combineLostFractions(lf, lost_fraction);
1054 exponent += bits;
1055 }
1056
1057 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1058
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001059 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001060 delete [] fullSignificand;
1061
1062 return lost_fraction;
1063}
1064
1065/* Multiply the significands of LHS and RHS to DST. */
1066lostFraction
1067APFloat::divideSignificand(const APFloat &rhs)
1068{
1069 unsigned int bit, i, partsCount;
1070 const integerPart *rhsSignificand;
1071 integerPart *lhsSignificand, *dividend, *divisor;
1072 integerPart scratch[4];
1073 lostFraction lost_fraction;
1074
1075 assert(semantics == rhs.semantics);
1076
1077 lhsSignificand = significandParts();
1078 rhsSignificand = rhs.significandParts();
1079 partsCount = partCount();
1080
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001081 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001082 dividend = new integerPart[partsCount * 2];
1083 else
1084 dividend = scratch;
1085
1086 divisor = dividend + partsCount;
1087
1088 /* Copy the dividend and divisor as they will be modified in-place. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001089 for (i = 0; i < partsCount; i++) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001090 dividend[i] = lhsSignificand[i];
1091 divisor[i] = rhsSignificand[i];
1092 lhsSignificand[i] = 0;
1093 }
1094
1095 exponent -= rhs.exponent;
1096
1097 unsigned int precision = semantics->precision;
1098
1099 /* Normalize the divisor. */
1100 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001101 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001102 exponent += bit;
1103 APInt::tcShiftLeft(divisor, partsCount, bit);
1104 }
1105
1106 /* Normalize the dividend. */
1107 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001108 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001109 exponent -= bit;
1110 APInt::tcShiftLeft(dividend, partsCount, bit);
1111 }
1112
Neil Boothb93d90e2007-10-12 16:02:31 +00001113 /* Ensure the dividend >= divisor initially for the loop below.
1114 Incidentally, this means that the division loop below is
1115 guaranteed to set the integer bit to one. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001116 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001117 exponent--;
1118 APInt::tcShiftLeft(dividend, partsCount, 1);
1119 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
1120 }
1121
1122 /* Long division. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001123 for (bit = precision; bit; bit -= 1) {
1124 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001125 APInt::tcSubtract(dividend, divisor, 0, partsCount);
1126 APInt::tcSetBit(lhsSignificand, bit - 1);
1127 }
1128
1129 APInt::tcShiftLeft(dividend, partsCount, 1);
1130 }
1131
1132 /* Figure out the lost fraction. */
1133 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1134
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001135 if (cmp > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001136 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001137 else if (cmp == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001138 lost_fraction = lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001139 else if (APInt::tcIsZero(dividend, partsCount))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001140 lost_fraction = lfExactlyZero;
1141 else
1142 lost_fraction = lfLessThanHalf;
1143
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001144 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001145 delete [] dividend;
1146
1147 return lost_fraction;
1148}
1149
1150unsigned int
1151APFloat::significandMSB() const
1152{
1153 return APInt::tcMSB(significandParts(), partCount());
1154}
1155
1156unsigned int
1157APFloat::significandLSB() const
1158{
1159 return APInt::tcLSB(significandParts(), partCount());
1160}
1161
1162/* Note that a zero result is NOT normalized to fcZero. */
1163lostFraction
1164APFloat::shiftSignificandRight(unsigned int bits)
1165{
1166 /* Our exponent should not overflow. */
Michael Gottesman9dc98332013-06-24 04:06:23 +00001167 assert((ExponentType) (exponent + bits) >= exponent);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001168
1169 exponent += bits;
1170
1171 return shiftRight(significandParts(), partCount(), bits);
1172}
1173
1174/* Shift the significand left BITS bits, subtract BITS from its exponent. */
1175void
1176APFloat::shiftSignificandLeft(unsigned int bits)
1177{
1178 assert(bits < semantics->precision);
1179
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001180 if (bits) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001181 unsigned int partsCount = partCount();
1182
1183 APInt::tcShiftLeft(significandParts(), partsCount, bits);
1184 exponent -= bits;
1185
1186 assert(!APInt::tcIsZero(significandParts(), partsCount));
1187 }
1188}
1189
1190APFloat::cmpResult
1191APFloat::compareAbsoluteValue(const APFloat &rhs) const
1192{
1193 int compare;
1194
1195 assert(semantics == rhs.semantics);
Michael Gottesman8136c382013-06-26 23:17:28 +00001196 assert(isFiniteNonZero());
1197 assert(rhs.isFiniteNonZero());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001198
1199 compare = exponent - rhs.exponent;
1200
1201 /* If exponents are equal, do an unsigned bignum comparison of the
1202 significands. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001203 if (compare == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001204 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001205 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001206
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001207 if (compare > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001208 return cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001209 else if (compare < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001210 return cmpLessThan;
1211 else
1212 return cmpEqual;
1213}
1214
1215/* Handle overflow. Sign is preserved. We either become infinity or
1216 the largest finite number. */
1217APFloat::opStatus
1218APFloat::handleOverflow(roundingMode rounding_mode)
1219{
1220 /* Infinity? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001221 if (rounding_mode == rmNearestTiesToEven ||
1222 rounding_mode == rmNearestTiesToAway ||
1223 (rounding_mode == rmTowardPositive && !sign) ||
1224 (rounding_mode == rmTowardNegative && sign)) {
1225 category = fcInfinity;
1226 return (opStatus) (opOverflow | opInexact);
1227 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001228
1229 /* Otherwise we become the largest finite number. */
1230 category = fcNormal;
1231 exponent = semantics->maxExponent;
1232 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001233 semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001234
1235 return opInexact;
1236}
1237
Neil Booth1ca1f802007-10-03 15:16:41 +00001238/* Returns TRUE if, when truncating the current number, with BIT the
1239 new LSB, with the given lost fraction and rounding mode, the result
1240 would need to be rounded away from zero (i.e., by increasing the
1241 signficand). This routine must work for fcZero of both signs, and
1242 fcNormal numbers. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001243bool
1244APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Booth1ca1f802007-10-03 15:16:41 +00001245 lostFraction lost_fraction,
1246 unsigned int bit) const
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001247{
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001248 /* NaNs and infinities should not have lost fractions. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001249 assert(isFiniteNonZero() || category == fcZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001250
Neil Booth1ca1f802007-10-03 15:16:41 +00001251 /* Current callers never pass this so we don't handle it. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001252 assert(lost_fraction != lfExactlyZero);
1253
Mike Stump889285d2009-05-13 23:23:20 +00001254 switch (rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001255 case rmNearestTiesToAway:
1256 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1257
1258 case rmNearestTiesToEven:
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001259 if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001260 return true;
1261
1262 /* Our zeroes don't have a significand to test. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001263 if (lost_fraction == lfExactlyHalf && category != fcZero)
Neil Booth1ca1f802007-10-03 15:16:41 +00001264 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001265
1266 return false;
1267
1268 case rmTowardZero:
1269 return false;
1270
1271 case rmTowardPositive:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001272 return !sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001273
1274 case rmTowardNegative:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001275 return sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001276 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00001277 llvm_unreachable("Invalid rounding mode found");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001278}
1279
1280APFloat::opStatus
1281APFloat::normalize(roundingMode rounding_mode,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001282 lostFraction lost_fraction)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001283{
Neil Booth9acbf5a2007-09-26 21:33:42 +00001284 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001285 int exponentChange;
1286
Michael Gottesman8136c382013-06-26 23:17:28 +00001287 if (!isFiniteNonZero())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001288 return opOK;
1289
1290 /* Before rounding normalize the exponent of fcNormal numbers. */
1291 omsb = significandMSB() + 1;
1292
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001293 if (omsb) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001294 /* OMSB is numbered from 1. We want to place it in the integer
Nick Lewyckyf66daac2011-10-03 21:30:08 +00001295 bit numbered PRECISION if possible, with a compensating change in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001296 the exponent. */
1297 exponentChange = omsb - semantics->precision;
1298
1299 /* If the resulting exponent is too high, overflow according to
1300 the rounding mode. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001301 if (exponent + exponentChange > semantics->maxExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001302 return handleOverflow(rounding_mode);
1303
1304 /* Subnormal numbers have exponent minExponent, and their MSB
1305 is forced based on that. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001306 if (exponent + exponentChange < semantics->minExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001307 exponentChange = semantics->minExponent - exponent;
1308
1309 /* Shifting left is easy as we don't lose precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001310 if (exponentChange < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001311 assert(lost_fraction == lfExactlyZero);
1312
1313 shiftSignificandLeft(-exponentChange);
1314
1315 return opOK;
1316 }
1317
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001318 if (exponentChange > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001319 lostFraction lf;
1320
1321 /* Shift right and capture any new lost fraction. */
1322 lf = shiftSignificandRight(exponentChange);
1323
1324 lost_fraction = combineLostFractions(lf, lost_fraction);
1325
1326 /* Keep OMSB up-to-date. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001327 if (omsb > (unsigned) exponentChange)
Neil Boothb93d90e2007-10-12 16:02:31 +00001328 omsb -= exponentChange;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001329 else
Neil Booth9acbf5a2007-09-26 21:33:42 +00001330 omsb = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001331 }
1332 }
1333
1334 /* Now round the number according to rounding_mode given the lost
1335 fraction. */
1336
1337 /* As specified in IEEE 754, since we do not trap we do not report
1338 underflow for exact results. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001339 if (lost_fraction == lfExactlyZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001340 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001341 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001342 category = fcZero;
1343
1344 return opOK;
1345 }
1346
1347 /* Increment the significand if we're rounding away from zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001348 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1349 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001350 exponent = semantics->minExponent;
1351
1352 incrementSignificand();
1353 omsb = significandMSB() + 1;
1354
1355 /* Did the significand increment overflow? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001356 if (omsb == (unsigned) semantics->precision + 1) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001357 /* Renormalize by incrementing the exponent and shifting our
Neil Booth9acbf5a2007-09-26 21:33:42 +00001358 significand right one. However if we already have the
1359 maximum exponent we overflow to infinity. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001360 if (exponent == semantics->maxExponent) {
Neil Booth9acbf5a2007-09-26 21:33:42 +00001361 category = fcInfinity;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001362
Neil Booth9acbf5a2007-09-26 21:33:42 +00001363 return (opStatus) (opOverflow | opInexact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001364 }
1365
1366 shiftSignificandRight(1);
1367
1368 return opInexact;
1369 }
1370 }
1371
1372 /* The normal case - we were and are not denormal, and any
1373 significand increment above didn't overflow. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001374 if (omsb == semantics->precision)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001375 return opInexact;
1376
1377 /* We have a non-zero denormal. */
1378 assert(omsb < semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001379
1380 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001381 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001382 category = fcZero;
1383
1384 /* The fcZero case is a denormal that underflowed to zero. */
1385 return (opStatus) (opUnderflow | opInexact);
1386}
1387
1388APFloat::opStatus
1389APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1390{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001391 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001392 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001393 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001394
Michael Gottesman9b877e12013-06-24 09:57:57 +00001395 case PackCategoriesIntoKey(fcNaN, fcZero):
1396 case PackCategoriesIntoKey(fcNaN, fcNormal):
1397 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1398 case PackCategoriesIntoKey(fcNaN, fcNaN):
1399 case PackCategoriesIntoKey(fcNormal, fcZero):
1400 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1401 case PackCategoriesIntoKey(fcInfinity, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001402 return opOK;
1403
Michael Gottesman9b877e12013-06-24 09:57:57 +00001404 case PackCategoriesIntoKey(fcZero, fcNaN):
1405 case PackCategoriesIntoKey(fcNormal, fcNaN):
1406 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Stephen Canond3278282014-06-08 16:53:31 +00001407 // We need to be sure to flip the sign here for subtraction because we
1408 // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1409 sign = rhs.sign ^ subtract;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001410 category = fcNaN;
1411 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001412 return opOK;
1413
Michael Gottesman9b877e12013-06-24 09:57:57 +00001414 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1415 case PackCategoriesIntoKey(fcZero, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001416 category = fcInfinity;
1417 sign = rhs.sign ^ subtract;
1418 return opOK;
1419
Michael Gottesman9b877e12013-06-24 09:57:57 +00001420 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001421 assign(rhs);
1422 sign = rhs.sign ^ subtract;
1423 return opOK;
1424
Michael Gottesman9b877e12013-06-24 09:57:57 +00001425 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001426 /* Sign depends on rounding mode; handled by caller. */
1427 return opOK;
1428
Michael Gottesman9b877e12013-06-24 09:57:57 +00001429 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001430 /* Differently signed infinities can only be validly
1431 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001432 if (((sign ^ rhs.sign)!=0) != subtract) {
Neil Booth5fe658b2007-10-14 10:39:51 +00001433 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001434 return opInvalidOp;
1435 }
1436
1437 return opOK;
1438
Michael Gottesman9b877e12013-06-24 09:57:57 +00001439 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001440 return opDivByZero;
1441 }
1442}
1443
1444/* Add or subtract two normal numbers. */
1445lostFraction
1446APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1447{
1448 integerPart carry;
1449 lostFraction lost_fraction;
1450 int bits;
1451
1452 /* Determine if the operation on the absolute values is effectively
1453 an addition or subtraction. */
Aaron Ballmand5cc45f2015-03-24 12:47:51 +00001454 subtract ^= static_cast<bool>(sign ^ rhs.sign);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001455
1456 /* Are we bigger exponent-wise than the RHS? */
1457 bits = exponent - rhs.exponent;
1458
1459 /* Subtraction is more subtle than one might naively expect. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001460 if (subtract) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001461 APFloat temp_rhs(rhs);
1462 bool reverse;
1463
Chris Lattner3da18eb2007-08-24 03:02:34 +00001464 if (bits == 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001465 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1466 lost_fraction = lfExactlyZero;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001467 } else if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001468 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1469 shiftSignificandLeft(1);
1470 reverse = false;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001471 } else {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001472 lost_fraction = shiftSignificandRight(-bits - 1);
1473 temp_rhs.shiftSignificandLeft(1);
1474 reverse = true;
1475 }
1476
Chris Lattner3da18eb2007-08-24 03:02:34 +00001477 if (reverse) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001478 carry = temp_rhs.subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001479 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001480 copySignificand(temp_rhs);
1481 sign = !sign;
1482 } else {
1483 carry = subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001484 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001485 }
1486
1487 /* Invert the lost fraction - it was on the RHS and
1488 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001489 if (lost_fraction == lfLessThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001490 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001491 else if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001492 lost_fraction = lfLessThanHalf;
1493
1494 /* The code above is intended to ensure that no borrow is
1495 necessary. */
1496 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001497 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001498 } else {
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001499 if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001500 APFloat temp_rhs(rhs);
1501
1502 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1503 carry = addSignificand(temp_rhs);
1504 } else {
1505 lost_fraction = shiftSignificandRight(-bits);
1506 carry = addSignificand(rhs);
1507 }
1508
1509 /* We have a guard bit; generating a carry cannot happen. */
1510 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001511 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001512 }
1513
1514 return lost_fraction;
1515}
1516
1517APFloat::opStatus
1518APFloat::multiplySpecials(const APFloat &rhs)
1519{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001520 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001521 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001522 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001523
Michael Gottesman9b877e12013-06-24 09:57:57 +00001524 case PackCategoriesIntoKey(fcNaN, fcZero):
1525 case PackCategoriesIntoKey(fcNaN, fcNormal):
1526 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1527 case PackCategoriesIntoKey(fcNaN, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001528 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001529 return opOK;
1530
Michael Gottesman9b877e12013-06-24 09:57:57 +00001531 case PackCategoriesIntoKey(fcZero, fcNaN):
1532 case PackCategoriesIntoKey(fcNormal, fcNaN):
1533 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001534 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001535 category = fcNaN;
1536 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001537 return opOK;
1538
Michael Gottesman9b877e12013-06-24 09:57:57 +00001539 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1540 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1541 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001542 category = fcInfinity;
1543 return opOK;
1544
Michael Gottesman9b877e12013-06-24 09:57:57 +00001545 case PackCategoriesIntoKey(fcZero, fcNormal):
1546 case PackCategoriesIntoKey(fcNormal, fcZero):
1547 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001548 category = fcZero;
1549 return opOK;
1550
Michael Gottesman9b877e12013-06-24 09:57:57 +00001551 case PackCategoriesIntoKey(fcZero, fcInfinity):
1552 case PackCategoriesIntoKey(fcInfinity, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001553 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001554 return opInvalidOp;
1555
Michael Gottesman9b877e12013-06-24 09:57:57 +00001556 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001557 return opOK;
1558 }
1559}
1560
1561APFloat::opStatus
1562APFloat::divideSpecials(const APFloat &rhs)
1563{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001564 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001565 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001566 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001567
Michael Gottesman9b877e12013-06-24 09:57:57 +00001568 case PackCategoriesIntoKey(fcZero, fcNaN):
1569 case PackCategoriesIntoKey(fcNormal, fcNaN):
1570 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001571 category = fcNaN;
1572 copySignificand(rhs);
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001573 case PackCategoriesIntoKey(fcNaN, fcZero):
1574 case PackCategoriesIntoKey(fcNaN, fcNormal):
1575 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1576 case PackCategoriesIntoKey(fcNaN, fcNaN):
1577 sign = false;
1578 case PackCategoriesIntoKey(fcInfinity, fcZero):
1579 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1580 case PackCategoriesIntoKey(fcZero, fcInfinity):
1581 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001582 return opOK;
1583
Michael Gottesman9b877e12013-06-24 09:57:57 +00001584 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001585 category = fcZero;
1586 return opOK;
1587
Michael Gottesman9b877e12013-06-24 09:57:57 +00001588 case PackCategoriesIntoKey(fcNormal, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001589 category = fcInfinity;
1590 return opDivByZero;
1591
Michael Gottesman9b877e12013-06-24 09:57:57 +00001592 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1593 case PackCategoriesIntoKey(fcZero, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001594 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001595 return opInvalidOp;
1596
Michael Gottesman9b877e12013-06-24 09:57:57 +00001597 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001598 return opOK;
1599 }
1600}
1601
Dale Johannesenb5721632009-01-21 00:35:19 +00001602APFloat::opStatus
1603APFloat::modSpecials(const APFloat &rhs)
1604{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001605 switch (PackCategoriesIntoKey(category, rhs.category)) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001606 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001607 llvm_unreachable(nullptr);
Dale Johannesenb5721632009-01-21 00:35:19 +00001608
Michael Gottesman9b877e12013-06-24 09:57:57 +00001609 case PackCategoriesIntoKey(fcNaN, fcZero):
1610 case PackCategoriesIntoKey(fcNaN, fcNormal):
1611 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1612 case PackCategoriesIntoKey(fcNaN, fcNaN):
1613 case PackCategoriesIntoKey(fcZero, fcInfinity):
1614 case PackCategoriesIntoKey(fcZero, fcNormal):
1615 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Dale Johannesenb5721632009-01-21 00:35:19 +00001616 return opOK;
1617
Michael Gottesman9b877e12013-06-24 09:57:57 +00001618 case PackCategoriesIntoKey(fcZero, fcNaN):
1619 case PackCategoriesIntoKey(fcNormal, fcNaN):
1620 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001621 sign = false;
Dale Johannesenb5721632009-01-21 00:35:19 +00001622 category = fcNaN;
1623 copySignificand(rhs);
1624 return opOK;
1625
Michael Gottesman9b877e12013-06-24 09:57:57 +00001626 case PackCategoriesIntoKey(fcNormal, fcZero):
1627 case PackCategoriesIntoKey(fcInfinity, fcZero):
1628 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1629 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1630 case PackCategoriesIntoKey(fcZero, fcZero):
Dale Johannesenb5721632009-01-21 00:35:19 +00001631 makeNaN();
1632 return opInvalidOp;
1633
Michael Gottesman9b877e12013-06-24 09:57:57 +00001634 case PackCategoriesIntoKey(fcNormal, fcNormal):
Dale Johannesenb5721632009-01-21 00:35:19 +00001635 return opOK;
1636 }
1637}
1638
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001639/* Change sign. */
1640void
1641APFloat::changeSign()
1642{
1643 /* Look mummy, this one's easy. */
1644 sign = !sign;
1645}
1646
Dale Johannesen689d17d2007-08-31 23:35:31 +00001647void
1648APFloat::clearSign()
1649{
1650 /* So is this one. */
1651 sign = 0;
1652}
1653
1654void
1655APFloat::copySign(const APFloat &rhs)
1656{
1657 /* And this one. */
1658 sign = rhs.sign;
1659}
1660
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001661/* Normalized addition or subtraction. */
1662APFloat::opStatus
1663APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001664 bool subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001665{
1666 opStatus fs;
1667
1668 fs = addOrSubtractSpecials(rhs, subtract);
1669
1670 /* This return code means it was not a simple case. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001671 if (fs == opDivByZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001672 lostFraction lost_fraction;
1673
1674 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1675 fs = normalize(rounding_mode, lost_fraction);
1676
1677 /* Can only be zero if we lost no fraction. */
1678 assert(category != fcZero || lost_fraction == lfExactlyZero);
1679 }
1680
1681 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1682 positive zero unless rounding to minus infinity, except that
1683 adding two like-signed zeroes gives that zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001684 if (category == fcZero) {
1685 if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001686 sign = (rounding_mode == rmTowardNegative);
1687 }
1688
1689 return fs;
1690}
1691
1692/* Normalized addition. */
1693APFloat::opStatus
1694APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1695{
1696 return addOrSubtract(rhs, rounding_mode, false);
1697}
1698
1699/* Normalized subtraction. */
1700APFloat::opStatus
1701APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1702{
1703 return addOrSubtract(rhs, rounding_mode, true);
1704}
1705
1706/* Normalized multiply. */
1707APFloat::opStatus
1708APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1709{
1710 opStatus fs;
1711
1712 sign ^= rhs.sign;
1713 fs = multiplySpecials(rhs);
1714
Michael Gottesman8136c382013-06-26 23:17:28 +00001715 if (isFiniteNonZero()) {
Craig Topperc10719f2014-04-07 04:17:22 +00001716 lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001717 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001718 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001719 fs = (opStatus) (fs | opInexact);
1720 }
1721
1722 return fs;
1723}
1724
1725/* Normalized divide. */
1726APFloat::opStatus
1727APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1728{
1729 opStatus fs;
1730
1731 sign ^= rhs.sign;
1732 fs = divideSpecials(rhs);
1733
Michael Gottesman8136c382013-06-26 23:17:28 +00001734 if (isFiniteNonZero()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001735 lostFraction lost_fraction = divideSignificand(rhs);
1736 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001737 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001738 fs = (opStatus) (fs | opInexact);
1739 }
1740
1741 return fs;
1742}
1743
Dale Johannesenfe750172009-01-20 18:35:05 +00001744/* Normalized remainder. This is not currently correct in all cases. */
1745APFloat::opStatus
1746APFloat::remainder(const APFloat &rhs)
1747{
1748 opStatus fs;
1749 APFloat V = *this;
1750 unsigned int origSign = sign;
1751
Dale Johannesenfe750172009-01-20 18:35:05 +00001752 fs = V.divide(rhs, rmNearestTiesToEven);
1753 if (fs == opDivByZero)
1754 return fs;
1755
1756 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001757 integerPart *x = new integerPart[parts];
Dale Johannesenfe750172009-01-20 18:35:05 +00001758 bool ignored;
1759 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1760 rmNearestTiesToEven, &ignored);
1761 if (fs==opInvalidOp)
1762 return fs;
1763
1764 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1765 rmNearestTiesToEven);
1766 assert(fs==opOK); // should always work
1767
1768 fs = V.multiply(rhs, rmNearestTiesToEven);
1769 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1770
1771 fs = subtract(V, rmNearestTiesToEven);
1772 assert(fs==opOK || fs==opInexact); // likewise
1773
1774 if (isZero())
1775 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001776 delete[] x;
Dale Johannesenfe750172009-01-20 18:35:05 +00001777 return fs;
1778}
1779
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001780/* Normalized llvm frem (C fmod).
Dale Johannesenfe750172009-01-20 18:35:05 +00001781 This is not currently correct in all cases. */
Dale Johannesen689d17d2007-08-31 23:35:31 +00001782APFloat::opStatus
Stephen Canonb12db0e2015-09-21 19:29:25 +00001783APFloat::mod(const APFloat &rhs)
Dale Johannesen689d17d2007-08-31 23:35:31 +00001784{
1785 opStatus fs;
Dale Johannesenb5721632009-01-21 00:35:19 +00001786 fs = modSpecials(rhs);
Dale Johannesen689d17d2007-08-31 23:35:31 +00001787
Michael Gottesman8136c382013-06-26 23:17:28 +00001788 if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001789 APFloat V = *this;
1790 unsigned int origSign = sign;
Dale Johannesen689d17d2007-08-31 23:35:31 +00001791
Dale Johannesenb5721632009-01-21 00:35:19 +00001792 fs = V.divide(rhs, rmNearestTiesToEven);
1793 if (fs == opDivByZero)
1794 return fs;
Dale Johannesen728687c2007-09-05 20:39:49 +00001795
Dale Johannesenb5721632009-01-21 00:35:19 +00001796 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001797 integerPart *x = new integerPart[parts];
Dale Johannesenb5721632009-01-21 00:35:19 +00001798 bool ignored;
1799 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1800 rmTowardZero, &ignored);
1801 if (fs==opInvalidOp)
1802 return fs;
Dale Johannesen728687c2007-09-05 20:39:49 +00001803
Dale Johannesenb5721632009-01-21 00:35:19 +00001804 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1805 rmNearestTiesToEven);
1806 assert(fs==opOK); // should always work
Dale Johannesen728687c2007-09-05 20:39:49 +00001807
Stephen Canonb12db0e2015-09-21 19:29:25 +00001808 fs = V.multiply(rhs, rmNearestTiesToEven);
Dale Johannesenb5721632009-01-21 00:35:19 +00001809 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1810
Stephen Canonb12db0e2015-09-21 19:29:25 +00001811 fs = subtract(V, rmNearestTiesToEven);
Dale Johannesenb5721632009-01-21 00:35:19 +00001812 assert(fs==opOK || fs==opInexact); // likewise
1813
1814 if (isZero())
1815 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001816 delete[] x;
Dale Johannesenb5721632009-01-21 00:35:19 +00001817 }
Dale Johannesen689d17d2007-08-31 23:35:31 +00001818 return fs;
1819}
1820
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001821/* Normalized fused-multiply-add. */
1822APFloat::opStatus
1823APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001824 const APFloat &addend,
1825 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001826{
1827 opStatus fs;
1828
1829 /* Post-multiplication sign, before addition. */
1830 sign ^= multiplicand.sign;
1831
1832 /* If and only if all arguments are normal do we need to do an
1833 extended-precision calculation. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001834 if (isFiniteNonZero() &&
1835 multiplicand.isFiniteNonZero() &&
Hal Finkel171c2ec2014-10-14 19:23:07 +00001836 addend.isFinite()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001837 lostFraction lost_fraction;
1838
1839 lost_fraction = multiplySignificand(multiplicand, &addend);
1840 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001841 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001842 fs = (opStatus) (fs | opInexact);
1843
1844 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1845 positive zero unless rounding to minus infinity, except that
1846 adding two like-signed zeroes gives that zero. */
Lang Hames12b12e82015-01-04 01:20:55 +00001847 if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001848 sign = (rounding_mode == rmTowardNegative);
1849 } else {
1850 fs = multiplySpecials(multiplicand);
1851
1852 /* FS can only be opOK or opInvalidOp. There is no more work
1853 to do in the latter case. The IEEE-754R standard says it is
1854 implementation-defined in this case whether, if ADDEND is a
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001855 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001856
1857 If we need to do the addition we can do so with normal
1858 precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001859 if (fs == opOK)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001860 fs = addOrSubtract(addend, rounding_mode, false);
1861 }
1862
1863 return fs;
1864}
1865
Owen Andersona40319b2012-08-13 23:32:49 +00001866/* Rounding-mode corrrect round to integral value. */
1867APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
1868 opStatus fs;
Owen Andersona40319b2012-08-13 23:32:49 +00001869
Owen Anderson352dfff2012-08-15 18:28:45 +00001870 // If the exponent is large enough, we know that this value is already
1871 // integral, and the arithmetic below would potentially cause it to saturate
1872 // to +/-Inf. Bail out early instead.
Michael Gottesman8136c382013-06-26 23:17:28 +00001873 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
Owen Anderson352dfff2012-08-15 18:28:45 +00001874 return opOK;
1875
Owen Andersona40319b2012-08-13 23:32:49 +00001876 // The algorithm here is quite simple: we add 2^(p-1), where p is the
1877 // precision of our format, and then subtract it back off again. The choice
1878 // of rounding modes for the addition/subtraction determines the rounding mode
1879 // for our integral rounding as well.
Owen Andersonbe7e2972012-08-15 16:42:53 +00001880 // NOTE: When the input value is negative, we do subtraction followed by
Owen Anderson1ff74b02012-08-15 05:39:46 +00001881 // addition instead.
Owen Anderson0b357222012-08-14 18:51:15 +00001882 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1883 IntegerConstant <<= semanticsPrecision(*semantics)-1;
Owen Andersona40319b2012-08-13 23:32:49 +00001884 APFloat MagicConstant(*semantics);
1885 fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1886 rmNearestTiesToEven);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001887 MagicConstant.copySign(*this);
1888
Owen Andersona40319b2012-08-13 23:32:49 +00001889 if (fs != opOK)
1890 return fs;
1891
Owen Anderson1ff74b02012-08-15 05:39:46 +00001892 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1893 bool inputSign = isNegative();
1894
Owen Andersona40319b2012-08-13 23:32:49 +00001895 fs = add(MagicConstant, rounding_mode);
1896 if (fs != opOK && fs != opInexact)
1897 return fs;
1898
1899 fs = subtract(MagicConstant, rounding_mode);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001900
1901 // Restore the input sign.
1902 if (inputSign != isNegative())
1903 changeSign();
1904
Owen Andersona40319b2012-08-13 23:32:49 +00001905 return fs;
1906}
1907
1908
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001909/* Comparison requires normalized numbers. */
1910APFloat::cmpResult
1911APFloat::compare(const APFloat &rhs) const
1912{
1913 cmpResult result;
1914
1915 assert(semantics == rhs.semantics);
1916
Michael Gottesman9b877e12013-06-24 09:57:57 +00001917 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001918 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001919 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001920
Michael Gottesman9b877e12013-06-24 09:57:57 +00001921 case PackCategoriesIntoKey(fcNaN, fcZero):
1922 case PackCategoriesIntoKey(fcNaN, fcNormal):
1923 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1924 case PackCategoriesIntoKey(fcNaN, fcNaN):
1925 case PackCategoriesIntoKey(fcZero, fcNaN):
1926 case PackCategoriesIntoKey(fcNormal, fcNaN):
1927 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001928 return cmpUnordered;
1929
Michael Gottesman9b877e12013-06-24 09:57:57 +00001930 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1931 case PackCategoriesIntoKey(fcInfinity, fcZero):
1932 case PackCategoriesIntoKey(fcNormal, fcZero):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001933 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001934 return cmpLessThan;
1935 else
1936 return cmpGreaterThan;
1937
Michael Gottesman9b877e12013-06-24 09:57:57 +00001938 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1939 case PackCategoriesIntoKey(fcZero, fcInfinity):
1940 case PackCategoriesIntoKey(fcZero, fcNormal):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001941 if (rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001942 return cmpGreaterThan;
1943 else
1944 return cmpLessThan;
1945
Michael Gottesman9b877e12013-06-24 09:57:57 +00001946 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001947 if (sign == rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001948 return cmpEqual;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001949 else if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001950 return cmpLessThan;
1951 else
1952 return cmpGreaterThan;
1953
Michael Gottesman9b877e12013-06-24 09:57:57 +00001954 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001955 return cmpEqual;
1956
Michael Gottesman9b877e12013-06-24 09:57:57 +00001957 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001958 break;
1959 }
1960
1961 /* Two normal numbers. Do they have the same sign? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001962 if (sign != rhs.sign) {
1963 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001964 result = cmpLessThan;
1965 else
1966 result = cmpGreaterThan;
1967 } else {
1968 /* Compare absolute values; invert result if negative. */
1969 result = compareAbsoluteValue(rhs);
1970
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001971 if (sign) {
1972 if (result == cmpLessThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001973 result = cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001974 else if (result == cmpGreaterThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001975 result = cmpLessThan;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001976 }
1977 }
1978
1979 return result;
1980}
1981
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001982/// APFloat::convert - convert a value of one floating point type to another.
1983/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1984/// records whether the transformation lost information, i.e. whether
1985/// converting the result back to the original type will produce the
1986/// original value (this is almost the same as return value==fsOK, but there
1987/// are edge cases where this is not so).
1988
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001989APFloat::opStatus
1990APFloat::convert(const fltSemantics &toSemantics,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001991 roundingMode rounding_mode, bool *losesInfo)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001992{
Neil Bootha8d72692007-09-22 02:56:19 +00001993 lostFraction lostFraction;
1994 unsigned int newPartCount, oldPartCount;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001995 opStatus fs;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001996 int shift;
1997 const fltSemantics &fromSemantics = *semantics;
Neil Booth9acbf5a2007-09-26 21:33:42 +00001998
Neil Bootha8d72692007-09-22 02:56:19 +00001999 lostFraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002000 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Bootha8d72692007-09-22 02:56:19 +00002001 oldPartCount = partCount();
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002002 shift = toSemantics.precision - fromSemantics.precision;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002003
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002004 bool X86SpecialNan = false;
2005 if (&fromSemantics == &APFloat::x87DoubleExtended &&
2006 &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN &&
2007 (!(*significandParts() & 0x8000000000000000ULL) ||
2008 !(*significandParts() & 0x4000000000000000ULL))) {
2009 // x86 has some unusual NaNs which cannot be represented in any other
2010 // format; note them here.
2011 X86SpecialNan = true;
2012 }
2013
Ulrich Weigand1d4dbda2013-07-16 13:03:25 +00002014 // If this is a truncation of a denormal number, and the target semantics
2015 // has larger exponent range than the source semantics (this can happen
2016 // when truncating from PowerPC double-double to double format), the
2017 // right shift could lose result mantissa bits. Adjust exponent instead
2018 // of performing excessive shift.
2019 if (shift < 0 && isFiniteNonZero()) {
2020 int exponentChange = significandMSB() + 1 - fromSemantics.precision;
2021 if (exponent + exponentChange < toSemantics.minExponent)
2022 exponentChange = toSemantics.minExponent - exponent;
2023 if (exponentChange < shift)
2024 exponentChange = shift;
2025 if (exponentChange < 0) {
2026 shift -= exponentChange;
2027 exponent += exponentChange;
2028 }
2029 }
2030
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002031 // If this is a truncation, perform the shift before we narrow the storage.
Michael Gottesman8136c382013-06-26 23:17:28 +00002032 if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002033 lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
2034
2035 // Fix the storage so it can hold to new value.
Neil Bootha8d72692007-09-22 02:56:19 +00002036 if (newPartCount > oldPartCount) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002037 // The new type requires more storage; make it available.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002038 integerPart *newParts;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002039 newParts = new integerPart[newPartCount];
2040 APInt::tcSet(newParts, 0, newPartCount);
Michael Gottesman8136c382013-06-26 23:17:28 +00002041 if (isFiniteNonZero() || category==fcNaN)
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002042 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002043 freeSignificand();
2044 significand.parts = newParts;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002045 } else if (newPartCount == 1 && oldPartCount != 1) {
2046 // Switch to built-in storage for a single part.
2047 integerPart newPart = 0;
Michael Gottesman8136c382013-06-26 23:17:28 +00002048 if (isFiniteNonZero() || category==fcNaN)
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002049 newPart = significandParts()[0];
2050 freeSignificand();
2051 significand.part = newPart;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002052 }
2053
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002054 // Now that we have the right storage, switch the semantics.
2055 semantics = &toSemantics;
2056
2057 // If this is an extension, perform the shift now that the storage is
2058 // available.
Michael Gottesman8136c382013-06-26 23:17:28 +00002059 if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002060 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2061
Michael Gottesman8136c382013-06-26 23:17:28 +00002062 if (isFiniteNonZero()) {
Neil Bootha8d72692007-09-22 02:56:19 +00002063 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002064 *losesInfo = (fs != opOK);
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002065 } else if (category == fcNaN) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002066 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
Benjamin Kramerb361adb2013-01-25 17:01:00 +00002067
2068 // For x87 extended precision, we want to make a NaN, not a special NaN if
2069 // the input wasn't special either.
2070 if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
2071 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2072
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002073 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2074 // does not give you back the same bits. This is dubious, and we
2075 // don't currently do it. You're really supposed to get
2076 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002077 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002078 } else {
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002079 *losesInfo = false;
Eli Friedman31f01162011-11-28 18:50:37 +00002080 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002081 }
2082
2083 return fs;
2084}
2085
2086/* Convert a floating point number to an integer according to the
2087 rounding mode. If the rounded integer value is out of range this
Neil Booth618d0fc2007-11-01 22:43:37 +00002088 returns an invalid operation exception and the contents of the
2089 destination parts are unspecified. If the rounded value is in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002090 range but the floating point number is not the exact integer, the C
2091 standard doesn't require an inexact exception to be raised. IEEE
2092 854 does require it so we do that.
2093
2094 Note that for conversions to integer type the C standard requires
2095 round-to-zero to always be used. */
2096APFloat::opStatus
Neil Booth618d0fc2007-11-01 22:43:37 +00002097APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
2098 bool isSigned,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002099 roundingMode rounding_mode,
2100 bool *isExact) const
Neil Booth618d0fc2007-11-01 22:43:37 +00002101{
2102 lostFraction lost_fraction;
2103 const integerPart *src;
2104 unsigned int dstPartsCount, truncatedBits;
2105
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002106 *isExact = false;
2107
Neil Booth618d0fc2007-11-01 22:43:37 +00002108 /* Handle the three special cases first. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002109 if (category == fcInfinity || category == fcNaN)
Neil Booth618d0fc2007-11-01 22:43:37 +00002110 return opInvalidOp;
2111
2112 dstPartsCount = partCountForBits(width);
2113
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002114 if (category == fcZero) {
Neil Booth618d0fc2007-11-01 22:43:37 +00002115 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen7221af32008-10-07 00:40:01 +00002116 // Negative zero can't be represented as an int.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002117 *isExact = !sign;
2118 return opOK;
Neil Booth618d0fc2007-11-01 22:43:37 +00002119 }
2120
2121 src = significandParts();
2122
2123 /* Step 1: place our absolute value, with any fraction truncated, in
2124 the destination. */
2125 if (exponent < 0) {
2126 /* Our absolute value is less than one; truncate everything. */
2127 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen740e9872009-01-19 21:17:05 +00002128 /* For exponent -1 the integer bit represents .5, look at that.
2129 For smaller exponents leftmost truncated bit is 0. */
2130 truncatedBits = semantics->precision -1U - exponent;
Neil Booth618d0fc2007-11-01 22:43:37 +00002131 } else {
2132 /* We want the most significant (exponent + 1) bits; the rest are
2133 truncated. */
2134 unsigned int bits = exponent + 1U;
2135
2136 /* Hopelessly large in magnitude? */
2137 if (bits > width)
2138 return opInvalidOp;
2139
2140 if (bits < semantics->precision) {
2141 /* We truncate (semantics->precision - bits) bits. */
2142 truncatedBits = semantics->precision - bits;
2143 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
2144 } else {
2145 /* We want at least as many bits as are available. */
2146 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
2147 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
2148 truncatedBits = 0;
2149 }
2150 }
2151
2152 /* Step 2: work out any lost fraction, and increment the absolute
2153 value if we would round away from zero. */
2154 if (truncatedBits) {
2155 lost_fraction = lostFractionThroughTruncation(src, partCount(),
2156 truncatedBits);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002157 if (lost_fraction != lfExactlyZero &&
2158 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
Neil Booth618d0fc2007-11-01 22:43:37 +00002159 if (APInt::tcIncrement(parts, dstPartsCount))
2160 return opInvalidOp; /* Overflow. */
2161 }
2162 } else {
2163 lost_fraction = lfExactlyZero;
2164 }
2165
2166 /* Step 3: check if we fit in the destination. */
2167 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
2168
2169 if (sign) {
2170 if (!isSigned) {
2171 /* Negative numbers cannot be represented as unsigned. */
2172 if (omsb != 0)
2173 return opInvalidOp;
2174 } else {
2175 /* It takes omsb bits to represent the unsigned integer value.
2176 We lose a bit for the sign, but care is needed as the
2177 maximally negative integer is a special case. */
2178 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2179 return opInvalidOp;
2180
2181 /* This case can happen because of rounding. */
2182 if (omsb > width)
2183 return opInvalidOp;
2184 }
2185
2186 APInt::tcNegate (parts, dstPartsCount);
2187 } else {
2188 if (omsb >= width + !isSigned)
2189 return opInvalidOp;
2190 }
2191
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002192 if (lost_fraction == lfExactlyZero) {
2193 *isExact = true;
Neil Booth618d0fc2007-11-01 22:43:37 +00002194 return opOK;
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002195 } else
Neil Booth618d0fc2007-11-01 22:43:37 +00002196 return opInexact;
2197}
2198
2199/* Same as convertToSignExtendedInteger, except we provide
2200 deterministic values in case of an invalid operation exception,
2201 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002202 for underflow or overflow.
2203 The *isExact output tells whether the result is exact, in the sense
2204 that converting it back to the original floating point type produces
2205 the original value. This is almost equivalent to result==opOK,
2206 except for negative zeroes.
2207*/
Neil Booth618d0fc2007-11-01 22:43:37 +00002208APFloat::opStatus
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002209APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth9acbf5a2007-09-26 21:33:42 +00002210 bool isSigned,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002211 roundingMode rounding_mode, bool *isExact) const
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002212{
Neil Booth618d0fc2007-11-01 22:43:37 +00002213 opStatus fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002214
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002215 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002216 isExact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002217
Neil Booth618d0fc2007-11-01 22:43:37 +00002218 if (fs == opInvalidOp) {
2219 unsigned int bits, dstPartsCount;
2220
2221 dstPartsCount = partCountForBits(width);
2222
2223 if (category == fcNaN)
2224 bits = 0;
2225 else if (sign)
2226 bits = isSigned;
2227 else
2228 bits = width - isSigned;
2229
2230 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2231 if (sign && isSigned)
2232 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002233 }
2234
Neil Booth618d0fc2007-11-01 22:43:37 +00002235 return fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002236}
2237
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +00002238/* Same as convertToInteger(integerPart*, ...), except the result is returned in
2239 an APSInt, whose initial bit-width and signed-ness are used to determine the
2240 precision of the conversion.
2241 */
2242APFloat::opStatus
2243APFloat::convertToInteger(APSInt &result,
2244 roundingMode rounding_mode, bool *isExact) const
2245{
2246 unsigned bitWidth = result.getBitWidth();
2247 SmallVector<uint64_t, 4> parts(result.getNumWords());
2248 opStatus status = convertToInteger(
2249 parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
2250 // Keeps the original signed-ness.
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002251 result = APInt(bitWidth, parts);
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +00002252 return status;
2253}
2254
Neil Booth6c1c8582007-10-07 12:07:53 +00002255/* Convert an unsigned integer SRC to a floating point number,
2256 rounding according to ROUNDING_MODE. The sign of the floating
2257 point number is not modified. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002258APFloat::opStatus
Neil Booth6c1c8582007-10-07 12:07:53 +00002259APFloat::convertFromUnsignedParts(const integerPart *src,
2260 unsigned int srcCount,
2261 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002262{
Neil Booth49c6aab2007-10-08 14:39:42 +00002263 unsigned int omsb, precision, dstCount;
Neil Booth6c1c8582007-10-07 12:07:53 +00002264 integerPart *dst;
Neil Booth49c6aab2007-10-08 14:39:42 +00002265 lostFraction lost_fraction;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002266
2267 category = fcNormal;
Neil Booth49c6aab2007-10-08 14:39:42 +00002268 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002269 dst = significandParts();
2270 dstCount = partCount();
Neil Booth49c6aab2007-10-08 14:39:42 +00002271 precision = semantics->precision;
Neil Booth6c1c8582007-10-07 12:07:53 +00002272
Nick Lewyckyf66daac2011-10-03 21:30:08 +00002273 /* We want the most significant PRECISION bits of SRC. There may not
Neil Booth49c6aab2007-10-08 14:39:42 +00002274 be that many; extract what we can. */
2275 if (precision <= omsb) {
2276 exponent = omsb - 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002277 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth49c6aab2007-10-08 14:39:42 +00002278 omsb - precision);
2279 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2280 } else {
2281 exponent = precision - 1;
2282 lost_fraction = lfExactlyZero;
2283 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth6c1c8582007-10-07 12:07:53 +00002284 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002285
2286 return normalize(rounding_mode, lost_fraction);
2287}
2288
Dan Gohman35723eb2008-02-29 01:26:11 +00002289APFloat::opStatus
2290APFloat::convertFromAPInt(const APInt &Val,
2291 bool isSigned,
2292 roundingMode rounding_mode)
2293{
2294 unsigned int partCount = Val.getNumWords();
2295 APInt api = Val;
2296
2297 sign = false;
2298 if (isSigned && api.isNegative()) {
2299 sign = true;
2300 api = -api;
2301 }
2302
2303 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2304}
2305
Neil Booth03f58ab2007-10-07 12:15:41 +00002306/* Convert a two's complement integer SRC to a floating point number,
2307 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2308 integer is signed, in which case it must be sign-extended. */
2309APFloat::opStatus
2310APFloat::convertFromSignExtendedInteger(const integerPart *src,
2311 unsigned int srcCount,
2312 bool isSigned,
2313 roundingMode rounding_mode)
2314{
2315 opStatus status;
2316
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002317 if (isSigned &&
2318 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002319 integerPart *copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002320
2321 /* If we're signed and negative negate a copy. */
2322 sign = true;
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002323 copy = new integerPart[srcCount];
Neil Booth03f58ab2007-10-07 12:15:41 +00002324 APInt::tcAssign(copy, src, srcCount);
2325 APInt::tcNegate(copy, srcCount);
2326 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002327 delete [] copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002328 } else {
2329 sign = false;
2330 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2331 }
2332
2333 return status;
2334}
2335
Neil Booth5f009732007-10-07 11:45:55 +00002336/* FIXME: should this just take a const APInt reference? */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002337APFloat::opStatus
Neil Booth5f009732007-10-07 11:45:55 +00002338APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2339 unsigned int width, bool isSigned,
2340 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002341{
Dale Johannesen42305122007-09-21 22:09:37 +00002342 unsigned int partCount = partCountForBits(width);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002343 APInt api = APInt(width, makeArrayRef(parts, partCount));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002344
2345 sign = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002346 if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
Dale Johannesen28a2c4a2007-09-30 18:17:01 +00002347 sign = true;
2348 api = -api;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002349 }
2350
Neil Boothba205222007-10-07 12:10:57 +00002351 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002352}
2353
2354APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002355APFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002356{
Erick Tryzelaara9680df2009-08-18 18:20:37 +00002357 lostFraction lost_fraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002358
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002359 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002360 zeroSignificand();
2361 exponent = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002362
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002363 integerPart *significand = significandParts();
2364 unsigned partsCount = partCount();
2365 unsigned bitPos = partsCount * integerPartWidth;
2366 bool computedTrailingFraction = false;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002367
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002368 // Skip leading zeroes and any (hexa)decimal point.
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002369 StringRef::iterator begin = s.begin();
2370 StringRef::iterator end = s.end();
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002371 StringRef::iterator dot;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002372 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002373 StringRef::iterator firstSignificantDigit = p;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002374
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002375 while (p != end) {
Dale Johannesenfa483722008-05-14 22:53:25 +00002376 integerPart hex_value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002377
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002378 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002379 assert(dot == end && "String contains multiple dots");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002380 dot = p++;
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002381 continue;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002382 }
2383
2384 hex_value = hexDigitValue(*p);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002385 if (hex_value == -1U)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002386 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002387
2388 p++;
2389
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002390 // Store the number while we have space.
2391 if (bitPos) {
2392 bitPos -= 4;
2393 hex_value <<= bitPos % integerPartWidth;
2394 significand[bitPos / integerPartWidth] |= hex_value;
2395 } else if (!computedTrailingFraction) {
2396 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2397 computedTrailingFraction = true;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002398 }
2399 }
2400
2401 /* Hex floats require an exponent but not a hexadecimal point. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002402 assert(p != end && "Hex strings require an exponent");
2403 assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2404 assert(p != begin && "Significand has no digits");
2405 assert((dot == end || p - begin != 1) && "Significand has no digits");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002406
2407 /* Ignore the exponent if we are zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002408 if (p != firstSignificantDigit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002409 int expAdjustment;
2410
2411 /* Implicit hexadecimal point? */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002412 if (dot == end)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002413 dot = p;
2414
2415 /* Calculate the exponent adjustment implicit in the number of
2416 significant digits. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002417 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002418 if (expAdjustment < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002419 expAdjustment++;
2420 expAdjustment = expAdjustment * 4 - 1;
2421
2422 /* Adjust for writing the significand starting at the most
2423 significant nibble. */
2424 expAdjustment += semantics->precision;
2425 expAdjustment -= partsCount * integerPartWidth;
2426
2427 /* Adjust for the given exponent. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002428 exponent = totalExponent(p + 1, end, expAdjustment);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002429 }
2430
2431 return normalize(rounding_mode, lost_fraction);
2432}
2433
2434APFloat::opStatus
Neil Boothb93d90e2007-10-12 16:02:31 +00002435APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2436 unsigned sigPartCount, int exp,
2437 roundingMode rounding_mode)
2438{
2439 unsigned int parts, pow5PartCount;
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +00002440 fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
Neil Boothb93d90e2007-10-12 16:02:31 +00002441 integerPart pow5Parts[maxPowerOfFiveParts];
2442 bool isNearest;
2443
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002444 isNearest = (rounding_mode == rmNearestTiesToEven ||
2445 rounding_mode == rmNearestTiesToAway);
Neil Boothb93d90e2007-10-12 16:02:31 +00002446
2447 parts = partCountForBits(semantics->precision + 11);
2448
2449 /* Calculate pow(5, abs(exp)). */
2450 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2451
2452 for (;; parts *= 2) {
2453 opStatus sigStatus, powStatus;
2454 unsigned int excessPrecision, truncatedBits;
2455
2456 calcSemantics.precision = parts * integerPartWidth - 1;
2457 excessPrecision = calcSemantics.precision - semantics->precision;
2458 truncatedBits = excessPrecision;
2459
Michael Gottesman79b09672013-06-27 21:58:19 +00002460 APFloat decSig = APFloat::getZero(calcSemantics, sign);
2461 APFloat pow5(calcSemantics);
Neil Boothb93d90e2007-10-12 16:02:31 +00002462
2463 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2464 rmNearestTiesToEven);
2465 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2466 rmNearestTiesToEven);
2467 /* Add exp, as 10^n = 5^n * 2^n. */
2468 decSig.exponent += exp;
2469
2470 lostFraction calcLostFraction;
Evan Cheng82b9e962008-05-02 21:15:08 +00002471 integerPart HUerr, HUdistance;
2472 unsigned int powHUerr;
Neil Boothb93d90e2007-10-12 16:02:31 +00002473
2474 if (exp >= 0) {
2475 /* multiplySignificand leaves the precision-th bit set to 1. */
Craig Topperc10719f2014-04-07 04:17:22 +00002476 calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
Neil Boothb93d90e2007-10-12 16:02:31 +00002477 powHUerr = powStatus != opOK;
2478 } else {
2479 calcLostFraction = decSig.divideSignificand(pow5);
2480 /* Denormal numbers have less precision. */
2481 if (decSig.exponent < semantics->minExponent) {
2482 excessPrecision += (semantics->minExponent - decSig.exponent);
2483 truncatedBits = excessPrecision;
2484 if (excessPrecision > calcSemantics.precision)
2485 excessPrecision = calcSemantics.precision;
2486 }
2487 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002488 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Boothb93d90e2007-10-12 16:02:31 +00002489 }
2490
2491 /* Both multiplySignificand and divideSignificand return the
2492 result with the integer bit set. */
Evan Cheng67c90212009-10-27 21:35:42 +00002493 assert(APInt::tcExtractBit
2494 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
Neil Boothb93d90e2007-10-12 16:02:31 +00002495
2496 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2497 powHUerr);
2498 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2499 excessPrecision, isNearest);
2500
2501 /* Are we guaranteed to round correctly if we truncate? */
2502 if (HUdistance >= HUerr) {
2503 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2504 calcSemantics.precision - excessPrecision,
2505 excessPrecision);
2506 /* Take the exponent of decSig. If we tcExtract-ed less bits
2507 above we must adjust our exponent to compensate for the
2508 implicit right shift. */
2509 exponent = (decSig.exponent + semantics->precision
2510 - (calcSemantics.precision - excessPrecision));
2511 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2512 decSig.partCount(),
2513 truncatedBits);
2514 return normalize(rounding_mode, calcLostFraction);
2515 }
2516 }
2517}
2518
2519APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002520APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
Neil Boothb93d90e2007-10-12 16:02:31 +00002521{
Neil Booth4ed401b2007-10-14 10:16:12 +00002522 decimalInfo D;
Neil Boothb93d90e2007-10-12 16:02:31 +00002523 opStatus fs;
2524
Neil Booth4ed401b2007-10-14 10:16:12 +00002525 /* Scan the text. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002526 StringRef::iterator p = str.begin();
2527 interpretDecimal(p, str.end(), &D);
Neil Boothb93d90e2007-10-12 16:02:31 +00002528
Neil Booth91305512007-10-15 15:00:55 +00002529 /* Handle the quick cases. First the case of no significant digits,
2530 i.e. zero, and then exponents that are obviously too large or too
2531 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2532 definitely overflows if
2533
2534 (exp - 1) * L >= maxExponent
2535
2536 and definitely underflows to zero where
2537
2538 (exp + 1) * L <= minExponent - precision
2539
2540 With integer arithmetic the tightest bounds for L are
2541
2542 93/28 < L < 196/59 [ numerator <= 256 ]
2543 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2544 */
2545
Michael Gottesman228156c2013-07-01 23:54:08 +00002546 // Test if we have a zero number allowing for strings with no null terminators
2547 // and zero decimals with non-zero exponents.
2548 //
2549 // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2550 // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2551 // be at most one dot. On the other hand, if we have a zero with a non-zero
2552 // exponent, then we know that D.firstSigDigit will be non-numeric.
Michael Gottesman94d61952013-07-02 15:50:05 +00002553 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Boothb93d90e2007-10-12 16:02:31 +00002554 category = fcZero;
2555 fs = opOK;
John McCallb42cc682010-02-26 22:20:41 +00002556
2557 /* Check whether the normalized exponent is high enough to overflow
2558 max during the log-rebasing in the max-exponent check below. */
2559 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
2560 fs = handleOverflow(rounding_mode);
2561
2562 /* If it wasn't, then it also wasn't high enough to overflow max
2563 during the log-rebasing in the min-exponent check. Check that it
2564 won't overflow min in either check, then perform the min-exponent
2565 check. */
2566 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
2567 (D.normalizedExponent + 1) * 28738 <=
2568 8651 * (semantics->minExponent - (int) semantics->precision)) {
Neil Booth91305512007-10-15 15:00:55 +00002569 /* Underflow to zero and round. */
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002570 category = fcNormal;
Neil Booth91305512007-10-15 15:00:55 +00002571 zeroSignificand();
2572 fs = normalize(rounding_mode, lfLessThanHalf);
John McCallb42cc682010-02-26 22:20:41 +00002573
2574 /* We can finally safely perform the max-exponent check. */
Neil Booth91305512007-10-15 15:00:55 +00002575 } else if ((D.normalizedExponent - 1) * 42039
2576 >= 12655 * semantics->maxExponent) {
2577 /* Overflow and round. */
2578 fs = handleOverflow(rounding_mode);
Neil Boothb93d90e2007-10-12 16:02:31 +00002579 } else {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002580 integerPart *decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002581 unsigned int partCount;
Neil Boothb93d90e2007-10-12 16:02:31 +00002582
Neil Booth4ed401b2007-10-14 10:16:12 +00002583 /* A tight upper bound on number of bits required to hold an
Neil Booth91305512007-10-15 15:00:55 +00002584 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth4ed401b2007-10-14 10:16:12 +00002585 to hold the full significand, and an extra part required by
2586 tcMultiplyPart. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002587 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth91305512007-10-15 15:00:55 +00002588 partCount = partCountForBits(1 + 196 * partCount / 59);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002589 decSignificand = new integerPart[partCount + 1];
Neil Booth4ed401b2007-10-14 10:16:12 +00002590 partCount = 0;
Neil Boothb93d90e2007-10-12 16:02:31 +00002591
Neil Booth4ed401b2007-10-14 10:16:12 +00002592 /* Convert to binary efficiently - we do almost all multiplication
2593 in an integerPart. When this would overflow do we do a single
2594 bignum multiplication, and then revert again to multiplication
2595 in an integerPart. */
2596 do {
2597 integerPart decValue, val, multiplier;
2598
2599 val = 0;
2600 multiplier = 1;
2601
2602 do {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002603 if (*p == '.') {
Neil Booth4ed401b2007-10-14 10:16:12 +00002604 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002605 if (p == str.end()) {
2606 break;
2607 }
2608 }
Neil Booth4ed401b2007-10-14 10:16:12 +00002609 decValue = decDigitValue(*p++);
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002610 assert(decValue < 10U && "Invalid character in significand");
Neil Booth4ed401b2007-10-14 10:16:12 +00002611 multiplier *= 10;
2612 val = val * 10 + decValue;
2613 /* The maximum number that can be multiplied by ten with any
2614 digit added without overflowing an integerPart. */
2615 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2616
2617 /* Multiply out the current part. */
2618 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2619 partCount, partCount + 1, false);
2620
2621 /* If we used another part (likely but not guaranteed), increase
2622 the count. */
2623 if (decSignificand[partCount])
2624 partCount++;
2625 } while (p <= D.lastSigDigit);
Neil Boothb93d90e2007-10-12 16:02:31 +00002626
Neil Boothae077d22007-11-01 22:51:07 +00002627 category = fcNormal;
Neil Boothb93d90e2007-10-12 16:02:31 +00002628 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth4ed401b2007-10-14 10:16:12 +00002629 D.exponent, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002630
2631 delete [] decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002632 }
Neil Boothb93d90e2007-10-12 16:02:31 +00002633
2634 return fs;
2635}
2636
Michael Gottesman40e8a182013-06-24 09:58:05 +00002637bool
2638APFloat::convertFromStringSpecials(StringRef str) {
2639 if (str.equals("inf") || str.equals("INFINITY")) {
2640 makeInf(false);
2641 return true;
2642 }
2643
2644 if (str.equals("-inf") || str.equals("-INFINITY")) {
2645 makeInf(true);
2646 return true;
2647 }
2648
2649 if (str.equals("nan") || str.equals("NaN")) {
2650 makeNaN(false, false);
2651 return true;
2652 }
2653
2654 if (str.equals("-nan") || str.equals("-NaN")) {
2655 makeNaN(false, true);
2656 return true;
2657 }
2658
2659 return false;
2660}
2661
Neil Boothb93d90e2007-10-12 16:02:31 +00002662APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002663APFloat::convertFromString(StringRef str, roundingMode rounding_mode)
Neil Booth9acbf5a2007-09-26 21:33:42 +00002664{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002665 assert(!str.empty() && "Invalid string length");
Neil Booth06077e72007-10-14 10:29:28 +00002666
Michael Gottesman40e8a182013-06-24 09:58:05 +00002667 // Handle special cases.
2668 if (convertFromStringSpecials(str))
2669 return opOK;
2670
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002671 /* Handle a leading minus sign. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002672 StringRef::iterator p = str.begin();
2673 size_t slen = str.size();
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002674 sign = *p == '-' ? 1 : 0;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002675 if (*p == '-' || *p == '+') {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002676 p++;
2677 slen--;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002678 assert(slen && "String has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002679 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002680
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002681 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002682 assert(slen - 2 && "Invalid string");
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002683 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002684 rounding_mode);
2685 }
Bill Wendlingc6075402008-11-27 08:00:12 +00002686
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002687 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002688}
Dale Johannesena719a602007-08-24 00:56:33 +00002689
Neil Booth8f1946f2007-10-03 22:26:02 +00002690/* Write out a hexadecimal representation of the floating point value
2691 to DST, which must be of sufficient size, in the C99 form
2692 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2693 excluding the terminating NUL.
2694
2695 If UPPERCASE, the output is in upper case, otherwise in lower case.
2696
2697 HEXDIGITS digits appear altogether, rounding the value if
2698 necessary. If HEXDIGITS is 0, the minimal precision to display the
2699 number precisely is used instead. If nothing would appear after
2700 the decimal point it is suppressed.
2701
2702 The decimal exponent is always printed and has at least one digit.
2703 Zero values display an exponent of zero. Infinities and NaNs
2704 appear as "infinity" or "nan" respectively.
2705
2706 The above rules are as specified by C99. There is ambiguity about
2707 what the leading hexadecimal digit should be. This implementation
2708 uses whatever is necessary so that the exponent is displayed as
2709 stored. This implies the exponent will fall within the IEEE format
2710 range, and the leading hexadecimal digit will be 0 (for denormals),
2711 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2712 any other digits zero).
2713*/
2714unsigned int
2715APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2716 bool upperCase, roundingMode rounding_mode) const
2717{
2718 char *p;
2719
2720 p = dst;
2721 if (sign)
2722 *dst++ = '-';
2723
2724 switch (category) {
2725 case fcInfinity:
2726 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2727 dst += sizeof infinityL - 1;
2728 break;
2729
2730 case fcNaN:
2731 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2732 dst += sizeof NaNU - 1;
2733 break;
2734
2735 case fcZero:
2736 *dst++ = '0';
2737 *dst++ = upperCase ? 'X': 'x';
2738 *dst++ = '0';
2739 if (hexDigits > 1) {
2740 *dst++ = '.';
2741 memset (dst, '0', hexDigits - 1);
2742 dst += hexDigits - 1;
2743 }
2744 *dst++ = upperCase ? 'P': 'p';
2745 *dst++ = '0';
2746 break;
2747
2748 case fcNormal:
2749 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2750 break;
2751 }
2752
2753 *dst = 0;
2754
Evan Cheng82b9e962008-05-02 21:15:08 +00002755 return static_cast<unsigned int>(dst - p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002756}
2757
2758/* Does the hard work of outputting the correctly rounded hexadecimal
2759 form of a normal floating point number with the specified number of
2760 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2761 digits necessary to print the value precisely is output. */
2762char *
2763APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2764 bool upperCase,
2765 roundingMode rounding_mode) const
2766{
2767 unsigned int count, valueBits, shift, partsCount, outputDigits;
2768 const char *hexDigitChars;
2769 const integerPart *significand;
2770 char *p;
2771 bool roundUp;
2772
2773 *dst++ = '0';
2774 *dst++ = upperCase ? 'X': 'x';
2775
2776 roundUp = false;
2777 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2778
2779 significand = significandParts();
2780 partsCount = partCount();
2781
2782 /* +3 because the first digit only uses the single integer bit, so
2783 we have 3 virtual zero most-significant-bits. */
2784 valueBits = semantics->precision + 3;
2785 shift = integerPartWidth - valueBits % integerPartWidth;
2786
2787 /* The natural number of digits required ignoring trailing
2788 insignificant zeroes. */
2789 outputDigits = (valueBits - significandLSB () + 3) / 4;
2790
2791 /* hexDigits of zero means use the required number for the
2792 precision. Otherwise, see if we are truncating. If we are,
Neil Booth0ea72a92007-10-06 00:24:48 +00002793 find out if we need to round away from zero. */
Neil Booth8f1946f2007-10-03 22:26:02 +00002794 if (hexDigits) {
2795 if (hexDigits < outputDigits) {
2796 /* We are dropping non-zero bits, so need to check how to round.
2797 "bits" is the number of dropped bits. */
2798 unsigned int bits;
2799 lostFraction fraction;
2800
2801 bits = valueBits - hexDigits * 4;
2802 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2803 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2804 }
2805 outputDigits = hexDigits;
2806 }
2807
2808 /* Write the digits consecutively, and start writing in the location
2809 of the hexadecimal point. We move the most significant digit
2810 left and add the hexadecimal point later. */
2811 p = ++dst;
2812
2813 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2814
2815 while (outputDigits && count) {
2816 integerPart part;
2817
2818 /* Put the most significant integerPartWidth bits in "part". */
2819 if (--count == partsCount)
2820 part = 0; /* An imaginary higher zero part. */
2821 else
2822 part = significand[count] << shift;
2823
2824 if (count && shift)
2825 part |= significand[count - 1] >> (integerPartWidth - shift);
2826
2827 /* Convert as much of "part" to hexdigits as we can. */
2828 unsigned int curDigits = integerPartWidth / 4;
2829
2830 if (curDigits > outputDigits)
2831 curDigits = outputDigits;
2832 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2833 outputDigits -= curDigits;
2834 }
2835
2836 if (roundUp) {
2837 char *q = dst;
2838
2839 /* Note that hexDigitChars has a trailing '0'. */
2840 do {
2841 q--;
2842 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth0ea72a92007-10-06 00:24:48 +00002843 } while (*q == '0');
Evan Cheng67c90212009-10-27 21:35:42 +00002844 assert(q >= p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002845 } else {
2846 /* Add trailing zeroes. */
2847 memset (dst, '0', outputDigits);
2848 dst += outputDigits;
2849 }
2850
2851 /* Move the most significant digit to before the point, and if there
2852 is something after the decimal point add it. This must come
2853 after rounding above. */
2854 p[-1] = p[0];
2855 if (dst -1 == p)
2856 dst--;
2857 else
2858 p[0] = '.';
2859
2860 /* Finally output the exponent. */
2861 *dst++ = upperCase ? 'P': 'p';
2862
Neil Booth32897f52007-10-06 07:29:25 +00002863 return writeSignedDecimal (dst, exponent);
Neil Booth8f1946f2007-10-03 22:26:02 +00002864}
2865
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002866hash_code llvm::hash_value(const APFloat &Arg) {
Michael Gottesman8136c382013-06-26 23:17:28 +00002867 if (!Arg.isFiniteNonZero())
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002868 return hash_combine((uint8_t)Arg.category,
2869 // NaN has no sign, fix it at zero.
2870 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2871 Arg.semantics->precision);
2872
2873 // Normal floats need their exponent and significand hashed.
2874 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2875 Arg.semantics->precision, Arg.exponent,
2876 hash_combine_range(
2877 Arg.significandParts(),
2878 Arg.significandParts() + Arg.partCount()));
Dale Johannesena719a602007-08-24 00:56:33 +00002879}
2880
2881// Conversion from APFloat to/from host float/double. It may eventually be
2882// possible to eliminate these and have everybody deal with APFloats, but that
2883// will take a while. This approach will not easily extend to long double.
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002884// Current implementation requires integerPartWidth==64, which is correct at
2885// the moment but could be made more general.
Dale Johannesena719a602007-08-24 00:56:33 +00002886
Dale Johannesen728687c2007-09-05 20:39:49 +00002887// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002888// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen728687c2007-09-05 20:39:49 +00002889
Dale Johannesen245dceb2007-09-11 18:32:33 +00002890APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00002891APFloat::convertF80LongDoubleAPFloatToAPInt() const
2892{
Dan Gohmanb456a152008-01-29 12:08:20 +00002893 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
Evan Cheng67c90212009-10-27 21:35:42 +00002894 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002895
2896 uint64_t myexponent, mysignificand;
2897
Michael Gottesman8136c382013-06-26 23:17:28 +00002898 if (isFiniteNonZero()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00002899 myexponent = exponent+16383; //bias
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002900 mysignificand = significandParts()[0];
Dale Johannesen245dceb2007-09-11 18:32:33 +00002901 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2902 myexponent = 0; // denormal
2903 } else if (category==fcZero) {
2904 myexponent = 0;
2905 mysignificand = 0;
2906 } else if (category==fcInfinity) {
2907 myexponent = 0x7fff;
2908 mysignificand = 0x8000000000000000ULL;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002909 } else {
2910 assert(category == fcNaN && "Unknown category");
Dale Johannesen245dceb2007-09-11 18:32:33 +00002911 myexponent = 0x7fff;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002912 mysignificand = significandParts()[0];
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002913 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00002914
2915 uint64_t words[2];
Dale Johannesen93eefa02009-03-23 21:16:53 +00002916 words[0] = mysignificand;
2917 words[1] = ((uint64_t)(sign & 1) << 15) |
2918 (myexponent & 0x7fffLL);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002919 return APInt(80, words);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002920}
2921
2922APInt
Dale Johannesen007aa372007-10-11 18:07:22 +00002923APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2924{
Dan Gohmanb456a152008-01-29 12:08:20 +00002925 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
Evan Cheng67c90212009-10-27 21:35:42 +00002926 assert(partCount()==2);
Dale Johannesen007aa372007-10-11 18:07:22 +00002927
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002928 uint64_t words[2];
2929 opStatus fs;
2930 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00002931
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002932 // Convert number to double. To avoid spurious underflows, we re-
2933 // normalize against the "double" minExponent first, and only *then*
2934 // truncate the mantissa. The result of that second conversion
2935 // may be inexact, but should never underflow.
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002936 // Declare fltSemantics before APFloat that uses it (and
2937 // saves pointer to it) to ensure correct destruction order.
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002938 fltSemantics extendedSemantics = *semantics;
2939 extendedSemantics.minExponent = IEEEdouble.minExponent;
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002940 APFloat extended(*this);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002941 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2942 assert(fs == opOK && !losesInfo);
2943 (void)fs;
2944
2945 APFloat u(extended);
2946 fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2947 assert(fs == opOK || fs == opInexact);
2948 (void)fs;
2949 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2950
2951 // If conversion was exact or resulted in a special case, we're done;
2952 // just set the second double to zero. Otherwise, re-convert back to
2953 // the extended format and compute the difference. This now should
2954 // convert exactly to double.
Michael Gottesman8136c382013-06-26 23:17:28 +00002955 if (u.isFiniteNonZero() && losesInfo) {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002956 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2957 assert(fs == opOK && !losesInfo);
2958 (void)fs;
2959
2960 APFloat v(extended);
2961 v.subtract(u, rmNearestTiesToEven);
2962 fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2963 assert(fs == opOK && !losesInfo);
2964 (void)fs;
2965 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
Dale Johannesen007aa372007-10-11 18:07:22 +00002966 } else {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002967 words[1] = 0;
Dale Johannesen007aa372007-10-11 18:07:22 +00002968 }
2969
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002970 return APInt(128, words);
Dale Johannesen007aa372007-10-11 18:07:22 +00002971}
2972
2973APInt
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002974APFloat::convertQuadrupleAPFloatToAPInt() const
2975{
2976 assert(semantics == (const llvm::fltSemantics*)&IEEEquad);
Evan Cheng67c90212009-10-27 21:35:42 +00002977 assert(partCount()==2);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002978
2979 uint64_t myexponent, mysignificand, mysignificand2;
2980
Michael Gottesman8136c382013-06-26 23:17:28 +00002981 if (isFiniteNonZero()) {
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002982 myexponent = exponent+16383; //bias
2983 mysignificand = significandParts()[0];
2984 mysignificand2 = significandParts()[1];
2985 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2986 myexponent = 0; // denormal
2987 } else if (category==fcZero) {
2988 myexponent = 0;
2989 mysignificand = mysignificand2 = 0;
2990 } else if (category==fcInfinity) {
2991 myexponent = 0x7fff;
2992 mysignificand = mysignificand2 = 0;
2993 } else {
2994 assert(category == fcNaN && "Unknown category!");
2995 myexponent = 0x7fff;
2996 mysignificand = significandParts()[0];
2997 mysignificand2 = significandParts()[1];
2998 }
2999
3000 uint64_t words[2];
3001 words[0] = mysignificand;
3002 words[1] = ((uint64_t)(sign & 1) << 63) |
3003 ((myexponent & 0x7fff) << 48) |
Anton Korobeynikov876955c2009-08-21 23:09:47 +00003004 (mysignificand2 & 0xffffffffffffLL);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003005
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00003006 return APInt(128, words);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003007}
3008
3009APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00003010APFloat::convertDoubleAPFloatToAPInt() const
3011{
Dan Gohman58c468f2007-09-14 20:08:19 +00003012 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Evan Cheng67c90212009-10-27 21:35:42 +00003013 assert(partCount()==1);
Dale Johannesena719a602007-08-24 00:56:33 +00003014
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003015 uint64_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003016
Michael Gottesman8136c382013-06-26 23:17:28 +00003017 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00003018 myexponent = exponent+1023; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00003019 mysignificand = *significandParts();
3020 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
3021 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00003022 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00003023 myexponent = 0;
3024 mysignificand = 0;
3025 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00003026 myexponent = 0x7ff;
3027 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003028 } else {
3029 assert(category == fcNaN && "Unknown category!");
Dale Johannesena719a602007-08-24 00:56:33 +00003030 myexponent = 0x7ff;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003031 mysignificand = *significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003032 }
Dale Johannesena719a602007-08-24 00:56:33 +00003033
Evan Cheng82b9e962008-05-02 21:15:08 +00003034 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003035 ((myexponent & 0x7ff) << 52) |
3036 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesena719a602007-08-24 00:56:33 +00003037}
3038
Dale Johannesen245dceb2007-09-11 18:32:33 +00003039APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00003040APFloat::convertFloatAPFloatToAPInt() const
3041{
Dan Gohman58c468f2007-09-14 20:08:19 +00003042 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Evan Cheng67c90212009-10-27 21:35:42 +00003043 assert(partCount()==1);
Neil Booth9acbf5a2007-09-26 21:33:42 +00003044
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003045 uint32_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003046
Michael Gottesman8136c382013-06-26 23:17:28 +00003047 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00003048 myexponent = exponent+127; //bias
Evan Cheng82b9e962008-05-02 21:15:08 +00003049 mysignificand = (uint32_t)*significandParts();
Dale Johannesen06a10df2007-11-17 01:02:27 +00003050 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen728687c2007-09-05 20:39:49 +00003051 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00003052 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00003053 myexponent = 0;
3054 mysignificand = 0;
3055 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00003056 myexponent = 0xff;
3057 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003058 } else {
3059 assert(category == fcNaN && "Unknown category!");
Dale Johannesen728687c2007-09-05 20:39:49 +00003060 myexponent = 0xff;
Evan Cheng82b9e962008-05-02 21:15:08 +00003061 mysignificand = (uint32_t)*significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003062 }
Dale Johannesena719a602007-08-24 00:56:33 +00003063
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003064 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3065 (mysignificand & 0x7fffff)));
Dale Johannesena719a602007-08-24 00:56:33 +00003066}
3067
Chris Lattner4794b2b2009-10-16 02:13:51 +00003068APInt
3069APFloat::convertHalfAPFloatToAPInt() const
3070{
3071 assert(semantics == (const llvm::fltSemantics*)&IEEEhalf);
Evan Cheng67c90212009-10-27 21:35:42 +00003072 assert(partCount()==1);
Chris Lattner4794b2b2009-10-16 02:13:51 +00003073
3074 uint32_t myexponent, mysignificand;
3075
Michael Gottesman8136c382013-06-26 23:17:28 +00003076 if (isFiniteNonZero()) {
Chris Lattner4794b2b2009-10-16 02:13:51 +00003077 myexponent = exponent+15; //bias
3078 mysignificand = (uint32_t)*significandParts();
3079 if (myexponent == 1 && !(mysignificand & 0x400))
3080 myexponent = 0; // denormal
3081 } else if (category==fcZero) {
3082 myexponent = 0;
3083 mysignificand = 0;
3084 } else if (category==fcInfinity) {
Dale Johannesen0d670b52009-10-23 04:02:51 +00003085 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003086 mysignificand = 0;
3087 } else {
3088 assert(category == fcNaN && "Unknown category!");
Dale Johannesen0d670b52009-10-23 04:02:51 +00003089 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003090 mysignificand = (uint32_t)*significandParts();
3091 }
3092
3093 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3094 (mysignificand & 0x3ff)));
3095}
3096
Dale Johannesen007aa372007-10-11 18:07:22 +00003097// This function creates an APInt that is just a bit map of the floating
3098// point constant as it would appear in memory. It is not a conversion,
3099// and treating the result as a normal integer is unlikely to be useful.
3100
Dale Johannesen245dceb2007-09-11 18:32:33 +00003101APInt
Dale Johannesen54306fe2008-10-09 18:53:47 +00003102APFloat::bitcastToAPInt() const
Neil Booth9acbf5a2007-09-26 21:33:42 +00003103{
Chris Lattner4794b2b2009-10-16 02:13:51 +00003104 if (semantics == (const llvm::fltSemantics*)&IEEEhalf)
3105 return convertHalfAPFloatToAPInt();
3106
Dan Gohmanb456a152008-01-29 12:08:20 +00003107 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003108 return convertFloatAPFloatToAPInt();
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003109
Dan Gohmanb456a152008-01-29 12:08:20 +00003110 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003111 return convertDoubleAPFloatToAPInt();
Neil Booth9acbf5a2007-09-26 21:33:42 +00003112
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003113 if (semantics == (const llvm::fltSemantics*)&IEEEquad)
3114 return convertQuadrupleAPFloatToAPInt();
3115
Dan Gohmanb456a152008-01-29 12:08:20 +00003116 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
Dale Johannesen007aa372007-10-11 18:07:22 +00003117 return convertPPCDoubleDoubleAPFloatToAPInt();
3118
Dan Gohmanb456a152008-01-29 12:08:20 +00003119 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003120 "unknown format!");
3121 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003122}
3123
Neil Booth9acbf5a2007-09-26 21:33:42 +00003124float
3125APFloat::convertToFloat() const
3126{
Chris Lattner688f9912009-09-24 21:44:20 +00003127 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle &&
3128 "Float semantics are not IEEEsingle");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003129 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003130 return api.bitsToFloat();
3131}
3132
Neil Booth9acbf5a2007-09-26 21:33:42 +00003133double
3134APFloat::convertToDouble() const
3135{
Chris Lattner688f9912009-09-24 21:44:20 +00003136 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble &&
3137 "Float semantics are not IEEEdouble");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003138 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003139 return api.bitsToDouble();
3140}
3141
Dale Johannesenfff29952008-10-06 18:22:29 +00003142/// Integer bit is explicit in this format. Intel hardware (387 and later)
3143/// does not support these bit patterns:
3144/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3145/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3146/// exponent = 0, integer bit 1 ("pseudodenormal")
3147/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3148/// At the moment, the first two are treated as NaNs, the second two as Normal.
Dale Johannesen245dceb2007-09-11 18:32:33 +00003149void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003150APFloat::initFromF80LongDoubleAPInt(const APInt &api)
3151{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003152 assert(api.getBitWidth()==80);
3153 uint64_t i1 = api.getRawData()[0];
3154 uint64_t i2 = api.getRawData()[1];
Dale Johannesen93eefa02009-03-23 21:16:53 +00003155 uint64_t myexponent = (i2 & 0x7fff);
3156 uint64_t mysignificand = i1;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003157
3158 initialize(&APFloat::x87DoubleExtended);
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003159 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003160
Dale Johannesen93eefa02009-03-23 21:16:53 +00003161 sign = static_cast<unsigned int>(i2>>15);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003162 if (myexponent==0 && mysignificand==0) {
3163 // exponent, significand meaningless
3164 category = fcZero;
3165 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3166 // exponent, significand meaningless
3167 category = fcInfinity;
3168 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3169 // exponent meaningless
3170 category = fcNaN;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003171 significandParts()[0] = mysignificand;
3172 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003173 } else {
3174 category = fcNormal;
3175 exponent = myexponent - 16383;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003176 significandParts()[0] = mysignificand;
3177 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003178 if (myexponent==0) // denormal
3179 exponent = -16382;
Neil Booth9acbf5a2007-09-26 21:33:42 +00003180 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00003181}
3182
3183void
Dale Johannesen007aa372007-10-11 18:07:22 +00003184APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
3185{
3186 assert(api.getBitWidth()==128);
3187 uint64_t i1 = api.getRawData()[0];
3188 uint64_t i2 = api.getRawData()[1];
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003189 opStatus fs;
3190 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00003191
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003192 // Get the first double and convert to our format.
3193 initFromDoubleAPInt(APInt(64, i1));
3194 fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3195 assert(fs == opOK && !losesInfo);
3196 (void)fs;
Dale Johannesen007aa372007-10-11 18:07:22 +00003197
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003198 // Unless we have a special case, add in second double.
Michael Gottesman8136c382013-06-26 23:17:28 +00003199 if (isFiniteNonZero()) {
Tim Northover29178a32013-01-22 09:46:31 +00003200 APFloat v(IEEEdouble, APInt(64, i2));
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003201 fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3202 assert(fs == opOK && !losesInfo);
3203 (void)fs;
3204
3205 add(v, rmNearestTiesToEven);
Dale Johannesen007aa372007-10-11 18:07:22 +00003206 }
3207}
3208
3209void
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003210APFloat::initFromQuadrupleAPInt(const APInt &api)
3211{
3212 assert(api.getBitWidth()==128);
3213 uint64_t i1 = api.getRawData()[0];
3214 uint64_t i2 = api.getRawData()[1];
3215 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3216 uint64_t mysignificand = i1;
3217 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3218
3219 initialize(&APFloat::IEEEquad);
3220 assert(partCount()==2);
3221
3222 sign = static_cast<unsigned int>(i2>>63);
3223 if (myexponent==0 &&
3224 (mysignificand==0 && mysignificand2==0)) {
3225 // exponent, significand meaningless
3226 category = fcZero;
3227 } else if (myexponent==0x7fff &&
3228 (mysignificand==0 && mysignificand2==0)) {
3229 // exponent, significand meaningless
3230 category = fcInfinity;
3231 } else if (myexponent==0x7fff &&
3232 (mysignificand!=0 || mysignificand2 !=0)) {
3233 // exponent meaningless
3234 category = fcNaN;
3235 significandParts()[0] = mysignificand;
3236 significandParts()[1] = mysignificand2;
3237 } else {
3238 category = fcNormal;
3239 exponent = myexponent - 16383;
3240 significandParts()[0] = mysignificand;
3241 significandParts()[1] = mysignificand2;
3242 if (myexponent==0) // denormal
3243 exponent = -16382;
3244 else
3245 significandParts()[1] |= 0x1000000000000LL; // integer bit
3246 }
3247}
3248
3249void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003250APFloat::initFromDoubleAPInt(const APInt &api)
3251{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003252 assert(api.getBitWidth()==64);
3253 uint64_t i = *api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003254 uint64_t myexponent = (i >> 52) & 0x7ff;
3255 uint64_t mysignificand = i & 0xfffffffffffffLL;
3256
Dale Johannesena719a602007-08-24 00:56:33 +00003257 initialize(&APFloat::IEEEdouble);
Dale Johannesena719a602007-08-24 00:56:33 +00003258 assert(partCount()==1);
3259
Evan Cheng82b9e962008-05-02 21:15:08 +00003260 sign = static_cast<unsigned int>(i>>63);
Dale Johannesena719a602007-08-24 00:56:33 +00003261 if (myexponent==0 && mysignificand==0) {
3262 // exponent, significand meaningless
3263 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003264 } else if (myexponent==0x7ff && mysignificand==0) {
3265 // exponent, significand meaningless
3266 category = fcInfinity;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003267 } else if (myexponent==0x7ff && mysignificand!=0) {
3268 // exponent meaningless
3269 category = fcNaN;
3270 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003271 } else {
Dale Johannesena719a602007-08-24 00:56:33 +00003272 category = fcNormal;
3273 exponent = myexponent - 1023;
Dale Johannesen728687c2007-09-05 20:39:49 +00003274 *significandParts() = mysignificand;
3275 if (myexponent==0) // denormal
3276 exponent = -1022;
3277 else
3278 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth9acbf5a2007-09-26 21:33:42 +00003279 }
Dale Johannesena719a602007-08-24 00:56:33 +00003280}
3281
Dale Johannesen245dceb2007-09-11 18:32:33 +00003282void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003283APFloat::initFromFloatAPInt(const APInt & api)
3284{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003285 assert(api.getBitWidth()==32);
3286 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003287 uint32_t myexponent = (i >> 23) & 0xff;
3288 uint32_t mysignificand = i & 0x7fffff;
3289
Dale Johannesena719a602007-08-24 00:56:33 +00003290 initialize(&APFloat::IEEEsingle);
Dale Johannesena719a602007-08-24 00:56:33 +00003291 assert(partCount()==1);
3292
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003293 sign = i >> 31;
Dale Johannesena719a602007-08-24 00:56:33 +00003294 if (myexponent==0 && mysignificand==0) {
3295 // exponent, significand meaningless
3296 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003297 } else if (myexponent==0xff && mysignificand==0) {
3298 // exponent, significand meaningless
3299 category = fcInfinity;
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00003300 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesena719a602007-08-24 00:56:33 +00003301 // sign, exponent, significand meaningless
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003302 category = fcNaN;
3303 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003304 } else {
3305 category = fcNormal;
Dale Johannesena719a602007-08-24 00:56:33 +00003306 exponent = myexponent - 127; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00003307 *significandParts() = mysignificand;
3308 if (myexponent==0) // denormal
3309 exponent = -126;
3310 else
3311 *significandParts() |= 0x800000; // integer bit
Dale Johannesena719a602007-08-24 00:56:33 +00003312 }
3313}
Dale Johannesen245dceb2007-09-11 18:32:33 +00003314
Chris Lattner4794b2b2009-10-16 02:13:51 +00003315void
3316APFloat::initFromHalfAPInt(const APInt & api)
3317{
3318 assert(api.getBitWidth()==16);
3319 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen0d670b52009-10-23 04:02:51 +00003320 uint32_t myexponent = (i >> 10) & 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003321 uint32_t mysignificand = i & 0x3ff;
3322
3323 initialize(&APFloat::IEEEhalf);
3324 assert(partCount()==1);
3325
3326 sign = i >> 15;
3327 if (myexponent==0 && mysignificand==0) {
3328 // exponent, significand meaningless
3329 category = fcZero;
3330 } else if (myexponent==0x1f && mysignificand==0) {
3331 // exponent, significand meaningless
3332 category = fcInfinity;
3333 } else if (myexponent==0x1f && mysignificand!=0) {
3334 // sign, exponent, significand meaningless
3335 category = fcNaN;
3336 *significandParts() = mysignificand;
3337 } else {
3338 category = fcNormal;
3339 exponent = myexponent - 15; //bias
3340 *significandParts() = mysignificand;
3341 if (myexponent==0) // denormal
3342 exponent = -14;
3343 else
3344 *significandParts() |= 0x400; // integer bit
3345 }
3346}
3347
Dale Johannesen245dceb2007-09-11 18:32:33 +00003348/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesen007aa372007-10-11 18:07:22 +00003349/// we infer the floating point type from the size of the APInt. The
3350/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3351/// when the size is anything else).
Dale Johannesen245dceb2007-09-11 18:32:33 +00003352void
Tim Northover29178a32013-01-22 09:46:31 +00003353APFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api)
Neil Booth9acbf5a2007-09-26 21:33:42 +00003354{
Tim Northover29178a32013-01-22 09:46:31 +00003355 if (Sem == &IEEEhalf)
Chris Lattner4794b2b2009-10-16 02:13:51 +00003356 return initFromHalfAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003357 if (Sem == &IEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003358 return initFromFloatAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003359 if (Sem == &IEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003360 return initFromDoubleAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003361 if (Sem == &x87DoubleExtended)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003362 return initFromF80LongDoubleAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003363 if (Sem == &IEEEquad)
3364 return initFromQuadrupleAPInt(api);
3365 if (Sem == &PPCDoubleDouble)
3366 return initFromPPCDoubleDoubleAPInt(api);
3367
Craig Topper2617dcc2014-04-15 06:32:26 +00003368 llvm_unreachable(nullptr);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003369}
3370
Nadav Rotem7cc6d122011-02-17 21:22:27 +00003371APFloat
3372APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
3373{
Tim Northover29178a32013-01-22 09:46:31 +00003374 switch (BitWidth) {
3375 case 16:
3376 return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth));
3377 case 32:
3378 return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth));
3379 case 64:
3380 return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth));
3381 case 80:
3382 return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth));
3383 case 128:
3384 if (isIEEE)
3385 return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth));
3386 return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
3387 default:
3388 llvm_unreachable("Unknown floating bit width");
3389 }
Nadav Rotem7cc6d122011-02-17 21:22:27 +00003390}
3391
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +00003392unsigned APFloat::getSizeInBits(const fltSemantics &Sem) {
3393 return Sem.sizeInBits;
3394}
3395
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003396/// Make this number the largest magnitude normal number in the given
3397/// semantics.
3398void APFloat::makeLargest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003399 // We want (in interchange format):
3400 // sign = {Negative}
3401 // exponent = 1..10
3402 // significand = 1..1
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003403 category = fcNormal;
3404 sign = Negative;
3405 exponent = semantics->maxExponent;
John McCall29b5c282009-12-24 08:56:26 +00003406
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003407 // Use memset to set all but the highest integerPart to all ones.
3408 integerPart *significand = significandParts();
3409 unsigned PartCount = partCount();
3410 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
John McCall29b5c282009-12-24 08:56:26 +00003411
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003412 // Set the high integerPart especially setting all unused top bits for
3413 // internal consistency.
3414 const unsigned NumUnusedHighBits =
3415 PartCount*integerPartWidth - semantics->precision;
Alexey Samsonovba1ecbc2014-09-06 00:41:19 +00003416 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3417 ? (~integerPart(0) >> NumUnusedHighBits)
3418 : 0;
John McCall29b5c282009-12-24 08:56:26 +00003419}
3420
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003421/// Make this number the smallest magnitude denormal number in the given
3422/// semantics.
3423void APFloat::makeSmallest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003424 // We want (in interchange format):
3425 // sign = {Negative}
3426 // exponent = 0..0
3427 // significand = 0..01
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003428 category = fcNormal;
3429 sign = Negative;
3430 exponent = semantics->minExponent;
3431 APInt::tcSet(significandParts(), 1, partCount());
3432}
John McCall29b5c282009-12-24 08:56:26 +00003433
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003434
3435APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
3436 // We want (in interchange format):
3437 // sign = {Negative}
3438 // exponent = 1..10
3439 // significand = 1..1
3440 APFloat Val(Sem, uninitialized);
3441 Val.makeLargest(Negative);
3442 return Val;
3443}
3444
3445APFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) {
3446 // We want (in interchange format):
3447 // sign = {Negative}
3448 // exponent = 0..0
3449 // significand = 0..01
3450 APFloat Val(Sem, uninitialized);
3451 Val.makeSmallest(Negative);
John McCall29b5c282009-12-24 08:56:26 +00003452 return Val;
3453}
3454
3455APFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) {
Michael Gottesman79b09672013-06-27 21:58:19 +00003456 APFloat Val(Sem, uninitialized);
John McCall29b5c282009-12-24 08:56:26 +00003457
3458 // We want (in interchange format):
3459 // sign = {Negative}
3460 // exponent = 0..0
3461 // significand = 10..0
3462
Michael Gottesman30a90eb2013-07-27 21:49:21 +00003463 Val.category = fcNormal;
Michael Gottesmanccaf3322013-06-27 20:40:11 +00003464 Val.zeroSignificand();
Michael Gottesman79b09672013-06-27 21:58:19 +00003465 Val.sign = Negative;
3466 Val.exponent = Sem.minExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003467 Val.significandParts()[partCountForBits(Sem.precision)-1] |=
Eli Friedmand4330422011-10-12 21:56:19 +00003468 (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth));
John McCall29b5c282009-12-24 08:56:26 +00003469
3470 return Val;
3471}
3472
Tim Northover29178a32013-01-22 09:46:31 +00003473APFloat::APFloat(const fltSemantics &Sem, const APInt &API) {
3474 initFromAPInt(&Sem, API);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003475}
3476
Ulrich Weigande1d62f92012-10-29 18:17:42 +00003477APFloat::APFloat(float f) {
Tim Northover29178a32013-01-22 09:46:31 +00003478 initFromAPInt(&IEEEsingle, APInt::floatToBits(f));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003479}
3480
Ulrich Weigande1d62f92012-10-29 18:17:42 +00003481APFloat::APFloat(double d) {
Tim Northover29178a32013-01-22 09:46:31 +00003482 initFromAPInt(&IEEEdouble, APInt::doubleToBits(d));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003483}
John McCall29b5c282009-12-24 08:56:26 +00003484
3485namespace {
David Blaikie70fdf722012-07-25 18:04:24 +00003486 void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3487 Buffer.append(Str.begin(), Str.end());
John McCall29b5c282009-12-24 08:56:26 +00003488 }
3489
John McCalle6212ace2009-12-24 12:16:56 +00003490 /// Removes data from the given significand until it is no more
3491 /// precise than is required for the desired precision.
3492 void AdjustToPrecision(APInt &significand,
3493 int &exp, unsigned FormatPrecision) {
3494 unsigned bits = significand.getActiveBits();
3495
3496 // 196/59 is a very slight overestimate of lg_2(10).
3497 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3498
3499 if (bits <= bitsRequired) return;
3500
3501 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3502 if (!tensRemovable) return;
3503
3504 exp += tensRemovable;
3505
3506 APInt divisor(significand.getBitWidth(), 1);
3507 APInt powten(significand.getBitWidth(), 10);
3508 while (true) {
3509 if (tensRemovable & 1)
3510 divisor *= powten;
3511 tensRemovable >>= 1;
3512 if (!tensRemovable) break;
3513 powten *= powten;
3514 }
3515
3516 significand = significand.udiv(divisor);
3517
Hao Liube99cc32013-03-20 01:46:36 +00003518 // Truncate the significand down to its active bit count.
3519 significand = significand.trunc(significand.getActiveBits());
John McCalle6212ace2009-12-24 12:16:56 +00003520 }
3521
3522
John McCall29b5c282009-12-24 08:56:26 +00003523 void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3524 int &exp, unsigned FormatPrecision) {
3525 unsigned N = buffer.size();
3526 if (N <= FormatPrecision) return;
3527
3528 // The most significant figures are the last ones in the buffer.
3529 unsigned FirstSignificant = N - FormatPrecision;
3530
3531 // Round.
3532 // FIXME: this probably shouldn't use 'round half up'.
3533
3534 // Rounding down is just a truncation, except we also want to drop
3535 // trailing zeros from the new result.
3536 if (buffer[FirstSignificant - 1] < '5') {
NAKAMURA Takumi5adeb932012-02-19 03:18:29 +00003537 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
John McCall29b5c282009-12-24 08:56:26 +00003538 FirstSignificant++;
3539
3540 exp += FirstSignificant;
3541 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3542 return;
3543 }
3544
3545 // Rounding up requires a decimal add-with-carry. If we continue
3546 // the carry, the newly-introduced zeros will just be truncated.
3547 for (unsigned I = FirstSignificant; I != N; ++I) {
3548 if (buffer[I] == '9') {
3549 FirstSignificant++;
3550 } else {
3551 buffer[I]++;
3552 break;
3553 }
3554 }
3555
3556 // If we carried through, we have exactly one digit of precision.
3557 if (FirstSignificant == N) {
3558 exp += FirstSignificant;
3559 buffer.clear();
3560 buffer.push_back('1');
3561 return;
3562 }
3563
3564 exp += FirstSignificant;
3565 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3566 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003567}
John McCall29b5c282009-12-24 08:56:26 +00003568
3569void APFloat::toString(SmallVectorImpl<char> &Str,
3570 unsigned FormatPrecision,
Chris Lattner4c1e4db2010-03-06 19:20:13 +00003571 unsigned FormatMaxPadding) const {
John McCall29b5c282009-12-24 08:56:26 +00003572 switch (category) {
3573 case fcInfinity:
3574 if (isNegative())
3575 return append(Str, "-Inf");
3576 else
3577 return append(Str, "+Inf");
3578
3579 case fcNaN: return append(Str, "NaN");
3580
3581 case fcZero:
3582 if (isNegative())
3583 Str.push_back('-');
3584
3585 if (!FormatMaxPadding)
3586 append(Str, "0.0E+0");
3587 else
3588 Str.push_back('0');
3589 return;
3590
3591 case fcNormal:
3592 break;
3593 }
3594
3595 if (isNegative())
3596 Str.push_back('-');
3597
3598 // Decompose the number into an APInt and an exponent.
3599 int exp = exponent - ((int) semantics->precision - 1);
3600 APInt significand(semantics->precision,
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00003601 makeArrayRef(significandParts(),
3602 partCountForBits(semantics->precision)));
John McCall29b5c282009-12-24 08:56:26 +00003603
John McCalldd5044a2009-12-24 23:18:09 +00003604 // Set FormatPrecision if zero. We want to do this before we
3605 // truncate trailing zeros, as those are part of the precision.
3606 if (!FormatPrecision) {
Eli Friedmane72f1322013-08-29 23:44:34 +00003607 // We use enough digits so the number can be round-tripped back to an
3608 // APFloat. The formula comes from "How to Print Floating-Point Numbers
3609 // Accurately" by Steele and White.
3610 // FIXME: Using a formula based purely on the precision is conservative;
3611 // we can print fewer digits depending on the actual value being printed.
John McCalldd5044a2009-12-24 23:18:09 +00003612
Eli Friedmane72f1322013-08-29 23:44:34 +00003613 // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3614 FormatPrecision = 2 + semantics->precision * 59 / 196;
John McCalldd5044a2009-12-24 23:18:09 +00003615 }
3616
John McCall29b5c282009-12-24 08:56:26 +00003617 // Ignore trailing binary zeros.
3618 int trailingZeros = significand.countTrailingZeros();
3619 exp += trailingZeros;
3620 significand = significand.lshr(trailingZeros);
3621
3622 // Change the exponent from 2^e to 10^e.
3623 if (exp == 0) {
3624 // Nothing to do.
3625 } else if (exp > 0) {
3626 // Just shift left.
Jay Foad583abbc2010-12-07 08:25:19 +00003627 significand = significand.zext(semantics->precision + exp);
John McCall29b5c282009-12-24 08:56:26 +00003628 significand <<= exp;
3629 exp = 0;
3630 } else { /* exp < 0 */
3631 int texp = -exp;
3632
3633 // We transform this using the identity:
3634 // (N)(2^-e) == (N)(5^e)(10^-e)
3635 // This means we have to multiply N (the significand) by 5^e.
3636 // To avoid overflow, we have to operate on numbers large
3637 // enough to store N * 5^e:
3638 // log2(N * 5^e) == log2(N) + e * log2(5)
John McCalldd5044a2009-12-24 23:18:09 +00003639 // <= semantics->precision + e * 137 / 59
3640 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003641
Eli Friedman19546412011-10-07 23:40:49 +00003642 unsigned precision = semantics->precision + (137 * texp + 136) / 59;
John McCall29b5c282009-12-24 08:56:26 +00003643
3644 // Multiply significand by 5^e.
3645 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
Jay Foad583abbc2010-12-07 08:25:19 +00003646 significand = significand.zext(precision);
John McCall29b5c282009-12-24 08:56:26 +00003647 APInt five_to_the_i(precision, 5);
3648 while (true) {
3649 if (texp & 1) significand *= five_to_the_i;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003650
John McCall29b5c282009-12-24 08:56:26 +00003651 texp >>= 1;
3652 if (!texp) break;
3653 five_to_the_i *= five_to_the_i;
3654 }
3655 }
3656
John McCalle6212ace2009-12-24 12:16:56 +00003657 AdjustToPrecision(significand, exp, FormatPrecision);
3658
Dmitri Gribenko226fea52013-01-13 16:01:15 +00003659 SmallVector<char, 256> buffer;
John McCall29b5c282009-12-24 08:56:26 +00003660
3661 // Fill the buffer.
3662 unsigned precision = significand.getBitWidth();
3663 APInt ten(precision, 10);
3664 APInt digit(precision, 0);
3665
3666 bool inTrail = true;
3667 while (significand != 0) {
3668 // digit <- significand % 10
3669 // significand <- significand / 10
3670 APInt::udivrem(significand, ten, significand, digit);
3671
3672 unsigned d = digit.getZExtValue();
3673
3674 // Drop trailing zeros.
3675 if (inTrail && !d) exp++;
3676 else {
3677 buffer.push_back((char) ('0' + d));
3678 inTrail = false;
3679 }
3680 }
3681
3682 assert(!buffer.empty() && "no characters in buffer!");
3683
3684 // Drop down to FormatPrecision.
3685 // TODO: don't do more precise calculations above than are required.
3686 AdjustToPrecision(buffer, exp, FormatPrecision);
3687
3688 unsigned NDigits = buffer.size();
3689
John McCalldd5044a2009-12-24 23:18:09 +00003690 // Check whether we should use scientific notation.
John McCall29b5c282009-12-24 08:56:26 +00003691 bool FormatScientific;
3692 if (!FormatMaxPadding)
3693 FormatScientific = true;
3694 else {
John McCall29b5c282009-12-24 08:56:26 +00003695 if (exp >= 0) {
John McCalldd5044a2009-12-24 23:18:09 +00003696 // 765e3 --> 765000
3697 // ^^^
3698 // But we shouldn't make the number look more precise than it is.
3699 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3700 NDigits + (unsigned) exp > FormatPrecision);
John McCall29b5c282009-12-24 08:56:26 +00003701 } else {
John McCalldd5044a2009-12-24 23:18:09 +00003702 // Power of the most significant digit.
3703 int MSD = exp + (int) (NDigits - 1);
3704 if (MSD >= 0) {
John McCall29b5c282009-12-24 08:56:26 +00003705 // 765e-2 == 7.65
John McCalldd5044a2009-12-24 23:18:09 +00003706 FormatScientific = false;
John McCall29b5c282009-12-24 08:56:26 +00003707 } else {
3708 // 765e-5 == 0.00765
3709 // ^ ^^
John McCalldd5044a2009-12-24 23:18:09 +00003710 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
John McCall29b5c282009-12-24 08:56:26 +00003711 }
3712 }
John McCall29b5c282009-12-24 08:56:26 +00003713 }
3714
3715 // Scientific formatting is pretty straightforward.
3716 if (FormatScientific) {
3717 exp += (NDigits - 1);
3718
3719 Str.push_back(buffer[NDigits-1]);
3720 Str.push_back('.');
3721 if (NDigits == 1)
3722 Str.push_back('0');
3723 else
3724 for (unsigned I = 1; I != NDigits; ++I)
3725 Str.push_back(buffer[NDigits-1-I]);
3726 Str.push_back('E');
3727
3728 Str.push_back(exp >= 0 ? '+' : '-');
3729 if (exp < 0) exp = -exp;
3730 SmallVector<char, 6> expbuf;
3731 do {
3732 expbuf.push_back((char) ('0' + (exp % 10)));
3733 exp /= 10;
3734 } while (exp);
3735 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3736 Str.push_back(expbuf[E-1-I]);
3737 return;
3738 }
3739
3740 // Non-scientific, positive exponents.
3741 if (exp >= 0) {
3742 for (unsigned I = 0; I != NDigits; ++I)
3743 Str.push_back(buffer[NDigits-1-I]);
3744 for (unsigned I = 0; I != (unsigned) exp; ++I)
3745 Str.push_back('0');
3746 return;
3747 }
3748
3749 // Non-scientific, negative exponents.
3750
3751 // The number of digits to the left of the decimal point.
3752 int NWholeDigits = exp + (int) NDigits;
3753
3754 unsigned I = 0;
3755 if (NWholeDigits > 0) {
3756 for (; I != (unsigned) NWholeDigits; ++I)
3757 Str.push_back(buffer[NDigits-I-1]);
3758 Str.push_back('.');
3759 } else {
3760 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3761
3762 Str.push_back('0');
3763 Str.push_back('.');
3764 for (unsigned Z = 1; Z != NZeros; ++Z)
3765 Str.push_back('0');
3766 }
3767
3768 for (; I != NDigits; ++I)
3769 Str.push_back(buffer[NDigits-I-1]);
3770}
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003771
3772bool APFloat::getExactInverse(APFloat *inv) const {
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003773 // Special floats and denormals have no exact inverse.
Michael Gottesman8136c382013-06-26 23:17:28 +00003774 if (!isFiniteNonZero())
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003775 return false;
3776
3777 // Check that the number is a power of two by making sure that only the
3778 // integer bit is set in the significand.
3779 if (significandLSB() != semantics->precision - 1)
3780 return false;
3781
3782 // Get the inverse.
3783 APFloat reciprocal(*semantics, 1ULL);
3784 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3785 return false;
3786
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003787 // Avoid multiplication with a denormal, it is not safe on all platforms and
3788 // may be slower than a normal division.
Benjamin Kramer6bef24f2013-06-01 11:26:33 +00003789 if (reciprocal.isDenormal())
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003790 return false;
3791
Michael Gottesman8136c382013-06-26 23:17:28 +00003792 assert(reciprocal.isFiniteNonZero() &&
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003793 reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3794
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003795 if (inv)
3796 *inv = reciprocal;
3797
3798 return true;
3799}
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003800
3801bool APFloat::isSignaling() const {
3802 if (!isNaN())
3803 return false;
3804
3805 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3806 // first bit of the trailing significand being 0.
3807 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3808}
3809
3810/// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3811///
3812/// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3813/// appropriate sign switching before/after the computation.
3814APFloat::opStatus APFloat::next(bool nextDown) {
3815 // If we are performing nextDown, swap sign so we have -x.
3816 if (nextDown)
3817 changeSign();
3818
3819 // Compute nextUp(x)
3820 opStatus result = opOK;
3821
3822 // Handle each float category separately.
3823 switch (category) {
3824 case fcInfinity:
3825 // nextUp(+inf) = +inf
3826 if (!isNegative())
3827 break;
3828 // nextUp(-inf) = -getLargest()
3829 makeLargest(true);
3830 break;
3831 case fcNaN:
3832 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3833 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3834 // change the payload.
3835 if (isSignaling()) {
3836 result = opInvalidOp;
Alp Tokercb402912014-01-24 17:20:08 +00003837 // For consistency, propagate the sign of the sNaN to the qNaN.
Craig Topperc10719f2014-04-07 04:17:22 +00003838 makeNaN(false, isNegative(), nullptr);
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003839 }
3840 break;
3841 case fcZero:
3842 // nextUp(pm 0) = +getSmallest()
3843 makeSmallest(false);
3844 break;
3845 case fcNormal:
3846 // nextUp(-getSmallest()) = -0
3847 if (isSmallest() && isNegative()) {
3848 APInt::tcSet(significandParts(), 0, partCount());
3849 category = fcZero;
3850 exponent = 0;
3851 break;
3852 }
3853
3854 // nextUp(getLargest()) == INFINITY
3855 if (isLargest() && !isNegative()) {
3856 APInt::tcSet(significandParts(), 0, partCount());
3857 category = fcInfinity;
3858 exponent = semantics->maxExponent + 1;
3859 break;
3860 }
3861
3862 // nextUp(normal) == normal + inc.
3863 if (isNegative()) {
3864 // If we are negative, we need to decrement the significand.
3865
3866 // We only cross a binade boundary that requires adjusting the exponent
3867 // if:
3868 // 1. exponent != semantics->minExponent. This implies we are not in the
3869 // smallest binade or are dealing with denormals.
3870 // 2. Our significand excluding the integral bit is all zeros.
3871 bool WillCrossBinadeBoundary =
3872 exponent != semantics->minExponent && isSignificandAllZeros();
3873
3874 // Decrement the significand.
3875 //
3876 // We always do this since:
Alp Tokerf907b892013-12-05 05:44:44 +00003877 // 1. If we are dealing with a non-binade decrement, by definition we
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003878 // just decrement the significand.
3879 // 2. If we are dealing with a normal -> normal binade decrement, since
3880 // we have an explicit integral bit the fact that all bits but the
3881 // integral bit are zero implies that subtracting one will yield a
3882 // significand with 0 integral bit and 1 in all other spots. Thus we
3883 // must just adjust the exponent and set the integral bit to 1.
3884 // 3. If we are dealing with a normal -> denormal binade decrement,
3885 // since we set the integral bit to 0 when we represent denormals, we
3886 // just decrement the significand.
3887 integerPart *Parts = significandParts();
3888 APInt::tcDecrement(Parts, partCount());
3889
3890 if (WillCrossBinadeBoundary) {
3891 // Our result is a normal number. Do the following:
3892 // 1. Set the integral bit to 1.
3893 // 2. Decrement the exponent.
3894 APInt::tcSetBit(Parts, semantics->precision - 1);
3895 exponent--;
3896 }
3897 } else {
3898 // If we are positive, we need to increment the significand.
3899
3900 // We only cross a binade boundary that requires adjusting the exponent if
3901 // the input is not a denormal and all of said input's significand bits
3902 // are set. If all of said conditions are true: clear the significand, set
3903 // the integral bit to 1, and increment the exponent. If we have a
3904 // denormal always increment since moving denormals and the numbers in the
3905 // smallest normal binade have the same exponent in our representation.
3906 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3907
3908 if (WillCrossBinadeBoundary) {
3909 integerPart *Parts = significandParts();
3910 APInt::tcSet(Parts, 0, partCount());
3911 APInt::tcSetBit(Parts, semantics->precision - 1);
3912 assert(exponent != semantics->maxExponent &&
3913 "We can not increment an exponent beyond the maxExponent allowed"
3914 " by the given floating point semantics.");
3915 exponent++;
3916 } else {
3917 incrementSignificand();
3918 }
3919 }
3920 break;
3921 }
3922
3923 // If we are performing nextDown, swap sign so we have -nextUp(-x)
3924 if (nextDown)
3925 changeSign();
3926
3927 return result;
3928}
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003929
3930void
3931APFloat::makeInf(bool Negative) {
3932 category = fcInfinity;
3933 sign = Negative;
3934 exponent = semantics->maxExponent + 1;
3935 APInt::tcSet(significandParts(), 0, partCount());
3936}
3937
3938void
3939APFloat::makeZero(bool Negative) {
3940 category = fcZero;
3941 sign = Negative;
3942 exponent = semantics->minExponent-1;
3943 APInt::tcSet(significandParts(), 0, partCount());
3944}
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003945
3946APFloat llvm::scalbn(APFloat X, int Exp) {
3947 if (X.isInfinity() || X.isZero() || X.isNaN())
Richard Trieu6ae37962015-04-30 23:07:00 +00003948 return X;
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003949
3950 auto MaxExp = X.getSemantics().maxExponent;
3951 auto MinExp = X.getSemantics().minExponent;
3952 if (Exp > (MaxExp - X.exponent))
3953 // Overflow saturates to infinity.
3954 return APFloat::getInf(X.getSemantics(), X.isNegative());
3955 if (Exp < (MinExp - X.exponent))
3956 // Underflow saturates to zero.
3957 return APFloat::getZero(X.getSemantics(), X.isNegative());
3958
3959 X.exponent += Exp;
Richard Trieu6ae37962015-04-30 23:07:00 +00003960 return X;
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003961}