blob: 5a402bb280453da5172ae6a58bc5f39d2d0f8175 [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;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000055 };
56
Ulrich Weigand908c9362012-10-29 18:18:44 +000057 const fltSemantics APFloat::IEEEhalf = { 15, -14, 11 };
58 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24 };
59 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53 };
60 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113 };
61 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64 };
62 const fltSemantics APFloat::Bogus = { 0, 0, 0 };
Dale Johannesen007aa372007-10-11 18:07:22 +000063
Ulrich Weigandd9f7e252012-10-29 18:09:01 +000064 /* The PowerPC format consists of two doubles. It does not map cleanly
65 onto the usual format above. It is approximated using twice the
66 mantissa bits. Note that for exponents near the double minimum,
67 we no longer can represent the full 106 mantissa bits, so those
68 will be treated as denormal numbers.
69
70 FIXME: While this approximation is equivalent to what GCC uses for
71 compile-time arithmetic on PPC double-double numbers, it is not able
72 to represent all possible values held by a PPC double-double number,
73 for example: (long double) 1.0 + (long double) 0x1p-106
74 Should this be replaced by a full emulation of PPC double-double? */
Ulrich Weigand908c9362012-10-29 18:18:44 +000075 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53 };
Neil Boothb93d90e2007-10-12 16:02:31 +000076
77 /* A tight upper bound on number of parts required to hold the value
78 pow(5, power) is
79
Neil Booth91305512007-10-15 15:00:55 +000080 power * 815 / (351 * integerPartWidth) + 1
Dan Gohmanb452d4e2010-03-24 19:38:02 +000081
Neil Boothb93d90e2007-10-12 16:02:31 +000082 However, whilst the result may require only this many parts,
83 because we are multiplying two values to get it, the
84 multiplication may require an extra part with the excess part
85 being zero (consider the trivial case of 1 * 1, tcFullMultiply
86 requires two parts to hold the single-part result). So we add an
87 extra one to guarantee enough space whilst multiplying. */
88 const unsigned int maxExponent = 16383;
89 const unsigned int maxPrecision = 113;
90 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
Neil Booth91305512007-10-15 15:00:55 +000091 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
92 / (351 * integerPartWidth));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000093}
94
Chris Lattner91702092009-03-12 23:59:55 +000095/* A bunch of private, handy routines. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000096
Chris Lattner91702092009-03-12 23:59:55 +000097static inline unsigned int
98partCountForBits(unsigned int bits)
99{
100 return ((bits) + integerPartWidth - 1) / integerPartWidth;
101}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000102
Chris Lattner91702092009-03-12 23:59:55 +0000103/* Returns 0U-9U. Return values >= 10U are not digits. */
104static inline unsigned int
105decDigitValue(unsigned int c)
106{
107 return c - '0';
108}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000109
Chris Lattner91702092009-03-12 23:59:55 +0000110/* Return the value of a decimal exponent of the form
111 [+-]ddddddd.
Neil Booth4ed401b2007-10-14 10:16:12 +0000112
Chris Lattner91702092009-03-12 23:59:55 +0000113 If the exponent overflows, returns a large exponent with the
114 appropriate sign. */
115static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000116readExponent(StringRef::iterator begin, StringRef::iterator end)
Chris Lattner91702092009-03-12 23:59:55 +0000117{
118 bool isNegative;
119 unsigned int absExponent;
120 const unsigned int overlargeExponent = 24000; /* FIXME. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000121 StringRef::iterator p = begin;
122
123 assert(p != end && "Exponent has no digits");
Neil Booth4ed401b2007-10-14 10:16:12 +0000124
Chris Lattner91702092009-03-12 23:59:55 +0000125 isNegative = (*p == '-');
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000126 if (*p == '-' || *p == '+') {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000127 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000128 assert(p != end && "Exponent has no digits");
129 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000130
Chris Lattner91702092009-03-12 23:59:55 +0000131 absExponent = decDigitValue(*p++);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000132 assert(absExponent < 10U && "Invalid character in exponent");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000133
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000134 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000135 unsigned int value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000136
Chris Lattner91702092009-03-12 23:59:55 +0000137 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000138 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000139
Chris Lattner91702092009-03-12 23:59:55 +0000140 value += absExponent * 10;
141 if (absExponent >= overlargeExponent) {
142 absExponent = overlargeExponent;
Dale Johannesen370c77c2010-08-19 17:58:35 +0000143 p = end; /* outwit assert below */
Chris Lattner91702092009-03-12 23:59:55 +0000144 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000145 }
Chris Lattner91702092009-03-12 23:59:55 +0000146 absExponent = value;
147 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000148
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000149 assert(p == end && "Invalid exponent in exponent");
150
Chris Lattner91702092009-03-12 23:59:55 +0000151 if (isNegative)
152 return -(int) absExponent;
153 else
154 return (int) absExponent;
155}
156
157/* This is ugly and needs cleaning up, but I don't immediately see
158 how whilst remaining safe. */
159static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000160totalExponent(StringRef::iterator p, StringRef::iterator end,
161 int exponentAdjustment)
Chris Lattner91702092009-03-12 23:59:55 +0000162{
163 int unsignedExponent;
164 bool negative, overflow;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000165 int exponent = 0;
Chris Lattner91702092009-03-12 23:59:55 +0000166
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000167 assert(p != end && "Exponent has no digits");
168
Chris Lattner91702092009-03-12 23:59:55 +0000169 negative = *p == '-';
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000170 if (*p == '-' || *p == '+') {
Chris Lattner91702092009-03-12 23:59:55 +0000171 p++;
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000172 assert(p != end && "Exponent has no digits");
173 }
Chris Lattner91702092009-03-12 23:59:55 +0000174
175 unsignedExponent = 0;
176 overflow = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000177 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000178 unsigned int value;
179
180 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000181 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000182
Chris Lattner91702092009-03-12 23:59:55 +0000183 unsignedExponent = unsignedExponent * 10 + value;
Richard Smith156d9202012-08-24 00:01:19 +0000184 if (unsignedExponent > 32767) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000185 overflow = true;
Richard Smith156d9202012-08-24 00:01:19 +0000186 break;
187 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000188 }
189
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000190 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000191 overflow = true;
192
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000193 if (!overflow) {
Chris Lattner91702092009-03-12 23:59:55 +0000194 exponent = unsignedExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000195 if (negative)
Chris Lattner91702092009-03-12 23:59:55 +0000196 exponent = -exponent;
197 exponent += exponentAdjustment;
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000198 if (exponent > 32767 || exponent < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000199 overflow = true;
200 }
201
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000202 if (overflow)
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000203 exponent = negative ? -32768: 32767;
Chris Lattner91702092009-03-12 23:59:55 +0000204
205 return exponent;
206}
207
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000208static StringRef::iterator
209skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
210 StringRef::iterator *dot)
Chris Lattner91702092009-03-12 23:59:55 +0000211{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000212 StringRef::iterator p = begin;
213 *dot = end;
Nick Lewycky095b92e2014-09-06 01:16:42 +0000214 while (p != end && *p == '0')
Chris Lattner91702092009-03-12 23:59:55 +0000215 p++;
216
Nick Lewycky095b92e2014-09-06 01:16:42 +0000217 if (p != end && *p == '.') {
Chris Lattner91702092009-03-12 23:59:55 +0000218 *dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000219
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000220 assert(end - begin != 1 && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000221
Nick Lewycky095b92e2014-09-06 01:16:42 +0000222 while (p != end && *p == '0')
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000223 p++;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000224 }
225
Chris Lattner91702092009-03-12 23:59:55 +0000226 return p;
227}
Neil Booth4ed401b2007-10-14 10:16:12 +0000228
Chris Lattner91702092009-03-12 23:59:55 +0000229/* Given a normal decimal floating point number of the form
Neil Booth4ed401b2007-10-14 10:16:12 +0000230
Chris Lattner91702092009-03-12 23:59:55 +0000231 dddd.dddd[eE][+-]ddd
Neil Booth91305512007-10-15 15:00:55 +0000232
Chris Lattner91702092009-03-12 23:59:55 +0000233 where the decimal point and exponent are optional, fill out the
234 structure D. Exponent is appropriate if the significand is
235 treated as an integer, and normalizedExponent if the significand
236 is taken to have the decimal point after a single leading
237 non-zero digit.
Neil Booth4ed401b2007-10-14 10:16:12 +0000238
Chris Lattner91702092009-03-12 23:59:55 +0000239 If the value is zero, V->firstSigDigit points to a non-digit, and
240 the return exponent is zero.
241*/
242struct decimalInfo {
243 const char *firstSigDigit;
244 const char *lastSigDigit;
245 int exponent;
246 int normalizedExponent;
247};
Neil Booth4ed401b2007-10-14 10:16:12 +0000248
Chris Lattner91702092009-03-12 23:59:55 +0000249static void
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000250interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
251 decimalInfo *D)
Chris Lattner91702092009-03-12 23:59:55 +0000252{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000253 StringRef::iterator dot = end;
254 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
Neil Booth4ed401b2007-10-14 10:16:12 +0000255
Chris Lattner91702092009-03-12 23:59:55 +0000256 D->firstSigDigit = p;
257 D->exponent = 0;
258 D->normalizedExponent = 0;
259
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000260 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000261 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000262 assert(dot == end && "String contains multiple dots");
Chris Lattner91702092009-03-12 23:59:55 +0000263 dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000264 if (p == end)
265 break;
Neil Booth4ed401b2007-10-14 10:16:12 +0000266 }
Chris Lattner91702092009-03-12 23:59:55 +0000267 if (decDigitValue(*p) >= 10U)
268 break;
Chris Lattner91702092009-03-12 23:59:55 +0000269 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000270
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000271 if (p != end) {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000272 assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
273 assert(p != begin && "Significand has no digits");
274 assert((dot == end || p - begin != 1) && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000275
276 /* p points to the first non-digit in the string */
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000277 D->exponent = readExponent(p + 1, end);
Neil Booth4ed401b2007-10-14 10:16:12 +0000278
Chris Lattner91702092009-03-12 23:59:55 +0000279 /* Implied decimal point? */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000280 if (dot == end)
Chris Lattner91702092009-03-12 23:59:55 +0000281 dot = p;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000282 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000283
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000284 /* If number is all zeroes accept any exponent. */
285 if (p != D->firstSigDigit) {
Chris Lattner91702092009-03-12 23:59:55 +0000286 /* Drop insignificant trailing zeroes. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000287 if (p != begin) {
Neil Booth4ed401b2007-10-14 10:16:12 +0000288 do
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000289 do
290 p--;
291 while (p != begin && *p == '0');
292 while (p != begin && *p == '.');
293 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000294
Chris Lattner91702092009-03-12 23:59:55 +0000295 /* Adjust the exponents for any decimal point. */
Michael Gottesman9dc98332013-06-24 04:06:23 +0000296 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
Chris Lattner91702092009-03-12 23:59:55 +0000297 D->normalizedExponent = (D->exponent +
Michael Gottesman9dc98332013-06-24 04:06:23 +0000298 static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
Chris Lattner91702092009-03-12 23:59:55 +0000299 - (dot > D->firstSigDigit && dot < p)));
Neil Booth4ed401b2007-10-14 10:16:12 +0000300 }
301
Chris Lattner91702092009-03-12 23:59:55 +0000302 D->lastSigDigit = p;
303}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000304
Chris Lattner91702092009-03-12 23:59:55 +0000305/* Return the trailing fraction of a hexadecimal number.
306 DIGITVALUE is the first hex digit of the fraction, P points to
307 the next digit. */
308static lostFraction
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000309trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
310 unsigned int digitValue)
Chris Lattner91702092009-03-12 23:59:55 +0000311{
312 unsigned int hexDigit;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000313
Chris Lattner91702092009-03-12 23:59:55 +0000314 /* If the first trailing digit isn't 0 or 8 we can work out the
315 fraction immediately. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000316 if (digitValue > 8)
Chris Lattner91702092009-03-12 23:59:55 +0000317 return lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000318 else if (digitValue < 8 && digitValue > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000319 return lfLessThanHalf;
Chris Lattner91702092009-03-12 23:59:55 +0000320
Eli Friedmand2eb07a2013-07-17 22:17:29 +0000321 // Otherwise we need to find the first non-zero digit.
322 while (p != end && (*p == '0' || *p == '.'))
Chris Lattner91702092009-03-12 23:59:55 +0000323 p++;
324
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000325 assert(p != end && "Invalid trailing hexadecimal fraction!");
326
Chris Lattner91702092009-03-12 23:59:55 +0000327 hexDigit = hexDigitValue(*p);
328
329 /* If we ran off the end it is exactly zero or one-half, otherwise
330 a little more. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000331 if (hexDigit == -1U)
Chris Lattner91702092009-03-12 23:59:55 +0000332 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
333 else
334 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
335}
336
337/* Return the fraction lost were a bignum truncated losing the least
338 significant BITS bits. */
339static lostFraction
340lostFractionThroughTruncation(const integerPart *parts,
341 unsigned int partCount,
342 unsigned int bits)
343{
344 unsigned int lsb;
345
346 lsb = APInt::tcLSB(parts, partCount);
347
348 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000349 if (bits <= lsb)
Chris Lattner91702092009-03-12 23:59:55 +0000350 return lfExactlyZero;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000351 if (bits == lsb + 1)
Chris Lattner91702092009-03-12 23:59:55 +0000352 return lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000353 if (bits <= partCount * integerPartWidth &&
354 APInt::tcExtractBit(parts, bits - 1))
Chris Lattner91702092009-03-12 23:59:55 +0000355 return lfMoreThanHalf;
356
357 return lfLessThanHalf;
358}
359
360/* Shift DST right BITS bits noting lost fraction. */
361static lostFraction
362shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
363{
364 lostFraction lost_fraction;
365
366 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
367
368 APInt::tcShiftRight(dst, parts, bits);
369
370 return lost_fraction;
371}
372
373/* Combine the effect of two lost fractions. */
374static lostFraction
375combineLostFractions(lostFraction moreSignificant,
376 lostFraction lessSignificant)
377{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000378 if (lessSignificant != lfExactlyZero) {
379 if (moreSignificant == lfExactlyZero)
Chris Lattner91702092009-03-12 23:59:55 +0000380 moreSignificant = lfLessThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000381 else if (moreSignificant == lfExactlyHalf)
Chris Lattner91702092009-03-12 23:59:55 +0000382 moreSignificant = lfMoreThanHalf;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000383 }
384
Chris Lattner91702092009-03-12 23:59:55 +0000385 return moreSignificant;
386}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000387
Chris Lattner91702092009-03-12 23:59:55 +0000388/* The error from the true value, in half-ulps, on multiplying two
389 floating point numbers, which differ from the value they
390 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
391 than the returned value.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000392
Chris Lattner91702092009-03-12 23:59:55 +0000393 See "How to Read Floating Point Numbers Accurately" by William D
394 Clinger. */
395static unsigned int
396HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
397{
398 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000399
Chris Lattner91702092009-03-12 23:59:55 +0000400 if (HUerr1 + HUerr2 == 0)
401 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
402 else
403 return inexactMultiply + 2 * (HUerr1 + HUerr2);
404}
Neil Booth8f1946f2007-10-03 22:26:02 +0000405
Chris Lattner91702092009-03-12 23:59:55 +0000406/* The number of ulps from the boundary (zero, or half if ISNEAREST)
407 when the least significant BITS are truncated. BITS cannot be
408 zero. */
409static integerPart
410ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
411{
412 unsigned int count, partBits;
413 integerPart part, boundary;
Neil Boothd3985922007-10-07 08:51:21 +0000414
Evan Cheng67c90212009-10-27 21:35:42 +0000415 assert(bits != 0);
Neil Booth8f1946f2007-10-03 22:26:02 +0000416
Chris Lattner91702092009-03-12 23:59:55 +0000417 bits--;
418 count = bits / integerPartWidth;
419 partBits = bits % integerPartWidth + 1;
Neil Boothb93d90e2007-10-12 16:02:31 +0000420
Chris Lattner91702092009-03-12 23:59:55 +0000421 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
Neil Boothb93d90e2007-10-12 16:02:31 +0000422
Chris Lattner91702092009-03-12 23:59:55 +0000423 if (isNearest)
424 boundary = (integerPart) 1 << (partBits - 1);
425 else
426 boundary = 0;
427
428 if (count == 0) {
429 if (part - boundary <= boundary - part)
430 return part - boundary;
Neil Boothb93d90e2007-10-12 16:02:31 +0000431 else
Chris Lattner91702092009-03-12 23:59:55 +0000432 return boundary - part;
Neil Boothb93d90e2007-10-12 16:02:31 +0000433 }
434
Chris Lattner91702092009-03-12 23:59:55 +0000435 if (part == boundary) {
436 while (--count)
437 if (parts[count])
438 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000439
Chris Lattner91702092009-03-12 23:59:55 +0000440 return parts[0];
441 } else if (part == boundary - 1) {
442 while (--count)
443 if (~parts[count])
444 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000445
Chris Lattner91702092009-03-12 23:59:55 +0000446 return -parts[0];
447 }
Neil Boothb93d90e2007-10-12 16:02:31 +0000448
Chris Lattner91702092009-03-12 23:59:55 +0000449 return ~(integerPart) 0; /* A lot. */
450}
Neil Boothb93d90e2007-10-12 16:02:31 +0000451
Chris Lattner91702092009-03-12 23:59:55 +0000452/* Place pow(5, power) in DST, and return the number of parts used.
453 DST must be at least one part larger than size of the answer. */
454static unsigned int
455powerOf5(integerPart *dst, unsigned int power)
456{
457 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
458 15625, 78125 };
Chris Lattnerb858c0e2009-03-13 00:24:01 +0000459 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
460 pow5s[0] = 78125 * 5;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000461
Chris Lattner0bf18692009-03-13 00:03:51 +0000462 unsigned int partsCount[16] = { 1 };
Chris Lattner91702092009-03-12 23:59:55 +0000463 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
464 unsigned int result;
Chris Lattner91702092009-03-12 23:59:55 +0000465 assert(power <= maxExponent);
466
467 p1 = dst;
468 p2 = scratch;
469
470 *p1 = firstEightPowers[power & 7];
471 power >>= 3;
472
473 result = 1;
474 pow5 = pow5s;
475
476 for (unsigned int n = 0; power; power >>= 1, n++) {
477 unsigned int pc;
478
479 pc = partsCount[n];
480
481 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
482 if (pc == 0) {
483 pc = partsCount[n - 1];
484 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
485 pc *= 2;
486 if (pow5[pc - 1] == 0)
487 pc--;
488 partsCount[n] = pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000489 }
490
Chris Lattner91702092009-03-12 23:59:55 +0000491 if (power & 1) {
492 integerPart *tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000493
Chris Lattner91702092009-03-12 23:59:55 +0000494 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
495 result += pc;
496 if (p2[result - 1] == 0)
497 result--;
Neil Boothb93d90e2007-10-12 16:02:31 +0000498
Chris Lattner91702092009-03-12 23:59:55 +0000499 /* Now result is in p1 with partsCount parts and p2 is scratch
500 space. */
501 tmp = p1, p1 = p2, p2 = tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000502 }
503
Chris Lattner91702092009-03-12 23:59:55 +0000504 pow5 += pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000505 }
506
Chris Lattner91702092009-03-12 23:59:55 +0000507 if (p1 != dst)
508 APInt::tcAssign(dst, p1, result);
Neil Boothb93d90e2007-10-12 16:02:31 +0000509
Chris Lattner91702092009-03-12 23:59:55 +0000510 return result;
511}
Neil Boothb93d90e2007-10-12 16:02:31 +0000512
Chris Lattner91702092009-03-12 23:59:55 +0000513/* Zero at the end to avoid modular arithmetic when adding one; used
514 when rounding up during hexadecimal output. */
515static const char hexDigitsLower[] = "0123456789abcdef0";
516static const char hexDigitsUpper[] = "0123456789ABCDEF0";
517static const char infinityL[] = "infinity";
518static const char infinityU[] = "INFINITY";
519static const char NaNL[] = "nan";
520static const char NaNU[] = "NAN";
Neil Boothb93d90e2007-10-12 16:02:31 +0000521
Chris Lattner91702092009-03-12 23:59:55 +0000522/* Write out an integerPart in hexadecimal, starting with the most
523 significant nibble. Write out exactly COUNT hexdigits, return
524 COUNT. */
525static unsigned int
526partAsHex (char *dst, integerPart part, unsigned int count,
527 const char *hexDigitChars)
528{
529 unsigned int result = count;
Neil Boothb93d90e2007-10-12 16:02:31 +0000530
Evan Cheng67c90212009-10-27 21:35:42 +0000531 assert(count != 0 && count <= integerPartWidth / 4);
Neil Boothb93d90e2007-10-12 16:02:31 +0000532
Chris Lattner91702092009-03-12 23:59:55 +0000533 part >>= (integerPartWidth - 4 * count);
534 while (count--) {
535 dst[count] = hexDigitChars[part & 0xf];
536 part >>= 4;
Neil Boothb93d90e2007-10-12 16:02:31 +0000537 }
538
Chris Lattner91702092009-03-12 23:59:55 +0000539 return result;
540}
Neil Booth8f1946f2007-10-03 22:26:02 +0000541
Chris Lattner91702092009-03-12 23:59:55 +0000542/* Write out an unsigned decimal integer. */
543static char *
544writeUnsignedDecimal (char *dst, unsigned int n)
545{
546 char buff[40], *p;
Neil Booth8f1946f2007-10-03 22:26:02 +0000547
Chris Lattner91702092009-03-12 23:59:55 +0000548 p = buff;
549 do
550 *p++ = '0' + n % 10;
551 while (n /= 10);
Neil Booth8f1946f2007-10-03 22:26:02 +0000552
Chris Lattner91702092009-03-12 23:59:55 +0000553 do
554 *dst++ = *--p;
555 while (p != buff);
Neil Booth8f1946f2007-10-03 22:26:02 +0000556
Chris Lattner91702092009-03-12 23:59:55 +0000557 return dst;
558}
Neil Booth8f1946f2007-10-03 22:26:02 +0000559
Chris Lattner91702092009-03-12 23:59:55 +0000560/* Write out a signed decimal integer. */
561static char *
562writeSignedDecimal (char *dst, int value)
563{
564 if (value < 0) {
565 *dst++ = '-';
566 dst = writeUnsignedDecimal(dst, -(unsigned) value);
567 } else
568 dst = writeUnsignedDecimal(dst, value);
Neil Booth8f1946f2007-10-03 22:26:02 +0000569
Chris Lattner91702092009-03-12 23:59:55 +0000570 return dst;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000571}
572
573/* Constructors. */
574void
575APFloat::initialize(const fltSemantics *ourSemantics)
576{
577 unsigned int count;
578
579 semantics = ourSemantics;
580 count = partCount();
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000581 if (count > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000582 significand.parts = new integerPart[count];
583}
584
585void
586APFloat::freeSignificand()
587{
Manuel Klimekd0cf5b22013-06-03 13:03:05 +0000588 if (needsCleanup())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000589 delete [] significand.parts;
590}
591
592void
593APFloat::assign(const APFloat &rhs)
594{
595 assert(semantics == rhs.semantics);
596
597 sign = rhs.sign;
598 category = rhs.category;
599 exponent = rhs.exponent;
Michael Gottesman8136c382013-06-26 23:17:28 +0000600 if (isFiniteNonZero() || category == fcNaN)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000601 copySignificand(rhs);
602}
603
604void
605APFloat::copySignificand(const APFloat &rhs)
606{
Michael Gottesman8136c382013-06-26 23:17:28 +0000607 assert(isFiniteNonZero() || category == fcNaN);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000608 assert(rhs.partCount() >= partCount());
609
610 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +0000611 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000612}
613
Neil Booth5fe658b2007-10-14 10:39:51 +0000614/* Make this number a NaN, with an arbitrary but deterministic value
Dale Johannesen1f864982009-01-21 20:32:55 +0000615 for the significand. If double or longer, this is a signalling NaN,
Mike Stump799bf582009-05-30 03:49:43 +0000616 which may not be ideal. If float, this is QNaN(0). */
John McCalldcb9a7a2010-02-28 02:51:25 +0000617void APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill)
Neil Booth5fe658b2007-10-14 10:39:51 +0000618{
619 category = fcNaN;
John McCalldcb9a7a2010-02-28 02:51:25 +0000620 sign = Negative;
621
John McCallc12b1332010-02-28 12:49:50 +0000622 integerPart *significand = significandParts();
623 unsigned numParts = partCount();
624
John McCalldcb9a7a2010-02-28 02:51:25 +0000625 // Set the significand bits to the fill.
John McCallc12b1332010-02-28 12:49:50 +0000626 if (!fill || fill->getNumWords() < numParts)
627 APInt::tcSet(significand, 0, numParts);
628 if (fill) {
John McCallc6dbe302010-03-01 18:38:45 +0000629 APInt::tcAssign(significand, fill->getRawData(),
630 std::min(fill->getNumWords(), numParts));
John McCallc12b1332010-02-28 12:49:50 +0000631
632 // Zero out the excess bits of the significand.
633 unsigned bitsToPreserve = semantics->precision - 1;
634 unsigned part = bitsToPreserve / 64;
635 bitsToPreserve %= 64;
636 significand[part] &= ((1ULL << bitsToPreserve) - 1);
637 for (part++; part != numParts; ++part)
638 significand[part] = 0;
639 }
640
641 unsigned QNaNBit = semantics->precision - 2;
John McCalldcb9a7a2010-02-28 02:51:25 +0000642
643 if (SNaN) {
644 // We always have to clear the QNaN bit to make it an SNaN.
John McCallc12b1332010-02-28 12:49:50 +0000645 APInt::tcClearBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000646
647 // If there are no bits set in the payload, we have to set
648 // *something* to make it a NaN instead of an infinity;
649 // conventionally, this is the next bit down from the QNaN bit.
John McCallc12b1332010-02-28 12:49:50 +0000650 if (APInt::tcIsZero(significand, numParts))
651 APInt::tcSetBit(significand, QNaNBit - 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000652 } else {
653 // We always have to set the QNaN bit to make it a QNaN.
John McCallc12b1332010-02-28 12:49:50 +0000654 APInt::tcSetBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000655 }
John McCallc12b1332010-02-28 12:49:50 +0000656
657 // For x87 extended precision, we want to make a NaN, not a
658 // pseudo-NaN. Maybe we should expose the ability to make
659 // pseudo-NaNs?
660 if (semantics == &APFloat::x87DoubleExtended)
661 APInt::tcSetBit(significand, QNaNBit + 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000662}
663
664APFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
665 const APInt *fill) {
666 APFloat value(Sem, uninitialized);
667 value.makeNaN(SNaN, Negative, fill);
668 return value;
Neil Booth5fe658b2007-10-14 10:39:51 +0000669}
670
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000671APFloat &
672APFloat::operator=(const APFloat &rhs)
673{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000674 if (this != &rhs) {
675 if (semantics != rhs.semantics) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000676 freeSignificand();
677 initialize(rhs.semantics);
678 }
679 assign(rhs);
680 }
681
682 return *this;
683}
684
Benjamin Kramer06f47782014-03-04 20:26:51 +0000685APFloat &
686APFloat::operator=(APFloat &&rhs) {
687 freeSignificand();
688
689 semantics = rhs.semantics;
690 significand = rhs.significand;
691 exponent = rhs.exponent;
692 category = rhs.category;
693 sign = rhs.sign;
694
695 rhs.semantics = &Bogus;
696 return *this;
697}
698
Dale Johannesena719a602007-08-24 00:56:33 +0000699bool
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000700APFloat::isDenormal() const {
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000701 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000702 (APInt::tcExtractBit(significandParts(),
703 semantics->precision - 1) == 0);
704}
705
706bool
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000707APFloat::isSmallest() const {
708 // The smallest number by magnitude in our format will be the smallest
Michael Gottesmana7cc1242013-06-19 07:34:21 +0000709 // denormal, i.e. the floating point number with exponent being minimum
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000710 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000711 return isFiniteNonZero() && exponent == semantics->minExponent &&
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000712 significandMSB() == 0;
713}
714
715bool APFloat::isSignificandAllOnes() const {
716 // Test if the significand excluding the integral bit is all ones. This allows
717 // us to test for binade boundaries.
718 const integerPart *Parts = significandParts();
719 const unsigned PartCount = partCount();
720 for (unsigned i = 0; i < PartCount - 1; i++)
721 if (~Parts[i])
722 return false;
723
724 // Set the unused high bits to all ones when we compare.
725 const unsigned NumHighBits =
726 PartCount*integerPartWidth - semantics->precision + 1;
727 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
728 "fill than integerPartWidth");
729 const integerPart HighBitFill =
730 ~integerPart(0) << (integerPartWidth - NumHighBits);
731 if (~(Parts[PartCount - 1] | HighBitFill))
732 return false;
733
734 return true;
735}
736
737bool APFloat::isSignificandAllZeros() const {
738 // Test if the significand excluding the integral bit is all zeros. This
739 // allows us to test for binade boundaries.
740 const integerPart *Parts = significandParts();
741 const unsigned PartCount = partCount();
742
743 for (unsigned i = 0; i < PartCount - 1; i++)
744 if (Parts[i])
745 return false;
746
747 const unsigned NumHighBits =
748 PartCount*integerPartWidth - semantics->precision + 1;
749 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
750 "clear than integerPartWidth");
751 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
752
753 if (Parts[PartCount - 1] & HighBitMask)
754 return false;
755
756 return true;
757}
758
759bool
760APFloat::isLargest() const {
761 // The largest number by magnitude in our format will be the floating point
762 // number with maximum exponent and with significand that is all ones.
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000763 return isFiniteNonZero() && exponent == semantics->maxExponent
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000764 && isSignificandAllOnes();
765}
766
767bool
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000768APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesena719a602007-08-24 00:56:33 +0000769 if (this == &rhs)
770 return true;
771 if (semantics != rhs.semantics ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000772 category != rhs.category ||
773 sign != rhs.sign)
Dale Johannesena719a602007-08-24 00:56:33 +0000774 return false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000775 if (category==fcZero || category==fcInfinity)
Dale Johannesena719a602007-08-24 00:56:33 +0000776 return true;
Michael Gottesman8136c382013-06-26 23:17:28 +0000777 else if (isFiniteNonZero() && exponent!=rhs.exponent)
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000778 return false;
Dale Johannesena719a602007-08-24 00:56:33 +0000779 else {
Dale Johannesena719a602007-08-24 00:56:33 +0000780 int i= partCount();
781 const integerPart* p=significandParts();
782 const integerPart* q=rhs.significandParts();
783 for (; i>0; i--, p++, q++) {
784 if (*p != *q)
785 return false;
786 }
787 return true;
788 }
789}
790
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000791APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000792 initialize(&ourSemantics);
793 sign = 0;
Michael Gottesman30a90eb2013-07-27 21:49:21 +0000794 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000795 zeroSignificand();
796 exponent = ourSemantics.precision - 1;
797 significandParts()[0] = value;
798 normalize(rmNearestTiesToEven, lfExactlyZero);
799}
800
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000801APFloat::APFloat(const fltSemantics &ourSemantics) {
Chris Lattnerac6271e2009-09-17 01:08:43 +0000802 initialize(&ourSemantics);
803 category = fcZero;
804 sign = false;
805}
806
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000807APFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
John McCalldcb9a7a2010-02-28 02:51:25 +0000808 // Allocates storage if necessary but does not initialize it.
809 initialize(&ourSemantics);
810}
Chris Lattnerac6271e2009-09-17 01:08:43 +0000811
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000812APFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000813 initialize(&ourSemantics);
814 convertFromString(text, rmNearestTiesToEven);
815}
816
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000817APFloat::APFloat(const APFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000818 initialize(rhs.semantics);
819 assign(rhs);
820}
821
Benjamin Kramer06f47782014-03-04 20:26:51 +0000822APFloat::APFloat(APFloat &&rhs) : semantics(&Bogus) {
823 *this = std::move(rhs);
824}
825
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000826APFloat::~APFloat()
827{
828 freeSignificand();
829}
830
Ted Kremenek6f30a072008-02-11 17:24:50 +0000831// Profile - This method 'profiles' an APFloat for use with FoldingSet.
832void APFloat::Profile(FoldingSetNodeID& ID) const {
Dale Johannesen54306fe2008-10-09 18:53:47 +0000833 ID.Add(bitcastToAPInt());
Ted Kremenek6f30a072008-02-11 17:24:50 +0000834}
835
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000836unsigned int
837APFloat::partCount() const
838{
Dale Johannesen146a0ea2007-09-20 23:47:58 +0000839 return partCountForBits(semantics->precision + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000840}
841
842unsigned int
843APFloat::semanticsPrecision(const fltSemantics &semantics)
844{
845 return semantics.precision;
846}
847
848const integerPart *
849APFloat::significandParts() const
850{
851 return const_cast<APFloat *>(this)->significandParts();
852}
853
854integerPart *
855APFloat::significandParts()
856{
Evan Cheng67c90212009-10-27 21:35:42 +0000857 if (partCount() > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000858 return significand.parts;
859 else
860 return &significand.part;
861}
862
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000863void
864APFloat::zeroSignificand()
865{
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000866 APInt::tcSet(significandParts(), 0, partCount());
867}
868
869/* Increment an fcNormal floating point number's significand. */
870void
871APFloat::incrementSignificand()
872{
873 integerPart carry;
874
875 carry = APInt::tcIncrement(significandParts(), partCount());
876
877 /* Our callers should never cause us to overflow. */
878 assert(carry == 0);
Duncan Sandsa41634e2011-08-12 14:54:45 +0000879 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000880}
881
882/* Add the significand of the RHS. Returns the carry flag. */
883integerPart
884APFloat::addSignificand(const APFloat &rhs)
885{
886 integerPart *parts;
887
888 parts = significandParts();
889
890 assert(semantics == rhs.semantics);
891 assert(exponent == rhs.exponent);
892
893 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
894}
895
896/* Subtract the significand of the RHS with a borrow flag. Returns
897 the borrow flag. */
898integerPart
899APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
900{
901 integerPart *parts;
902
903 parts = significandParts();
904
905 assert(semantics == rhs.semantics);
906 assert(exponent == rhs.exponent);
907
908 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth9acbf5a2007-09-26 21:33:42 +0000909 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000910}
911
912/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
913 on to the full-precision result of the multiplication. Returns the
914 lost fraction. */
915lostFraction
916APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
917{
Neil Booth9acbf5a2007-09-26 21:33:42 +0000918 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000919 unsigned int partsCount, newPartsCount, precision;
920 integerPart *lhsSignificand;
921 integerPart scratch[4];
922 integerPart *fullSignificand;
923 lostFraction lost_fraction;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000924 bool ignored;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000925
926 assert(semantics == rhs.semantics);
927
928 precision = semantics->precision;
Lang Hames56c0eb22014-11-19 19:15:41 +0000929
930 // Allocate space for twice as many bits as the original significand, plus one
931 // extra bit for the addition to overflow into.
932 newPartsCount = partCountForBits(precision * 2 + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000933
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000934 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000935 fullSignificand = new integerPart[newPartsCount];
936 else
937 fullSignificand = scratch;
938
939 lhsSignificand = significandParts();
940 partsCount = partCount();
941
942 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth0ea72a92007-10-06 00:24:48 +0000943 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000944
945 lost_fraction = lfExactlyZero;
946 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
947 exponent += rhs.exponent;
948
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000949 // Assume the operands involved in the multiplication are single-precision
950 // FP, and the two multiplicants are:
951 // *this = a23 . a22 ... a0 * 2^e1
952 // rhs = b23 . b22 ... b0 * 2^e2
953 // the result of multiplication is:
Lang Hames56c0eb22014-11-19 19:15:41 +0000954 // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2)
955 // Note that there are three significant bits at the left-hand side of the
956 // radix point: two for the multiplication, and an overflow bit for the
957 // addition (that will always be zero at this point). Move the radix point
958 // toward left by two bits, and adjust exponent accordingly.
959 exponent += 2;
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000960
Hal Finkel171c2ec2014-10-14 19:23:07 +0000961 if (addend && addend->isNonZero()) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000962 // The intermediate result of the multiplication has "2 * precision"
963 // signicant bit; adjust the addend to be consistent with mul result.
964 //
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000965 Significand savedSignificand = significand;
966 const fltSemantics *savedSemantics = semantics;
967 fltSemantics extendedSemantics;
968 opStatus status;
969 unsigned int extendedPrecision;
970
Lang Hames56c0eb22014-11-19 19:15:41 +0000971 // Normalize our MSB to one below the top bit to allow for overflow.
972 extendedPrecision = 2 * precision + 1;
973 if (omsb != extendedPrecision - 1) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000974 assert(extendedPrecision > omsb);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000975 APInt::tcShiftLeft(fullSignificand, newPartsCount,
Lang Hames56c0eb22014-11-19 19:15:41 +0000976 (extendedPrecision - 1) - omsb);
977 exponent -= (extendedPrecision - 1) - omsb;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000978 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000979
980 /* Create new semantics. */
981 extendedSemantics = *semantics;
982 extendedSemantics.precision = extendedPrecision;
983
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000984 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000985 significand.part = fullSignificand[0];
986 else
987 significand.parts = fullSignificand;
988 semantics = &extendedSemantics;
989
990 APFloat extendedAddend(*addend);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000991 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000992 assert(status == opOK);
Duncan Sandsa41634e2011-08-12 14:54:45 +0000993 (void)status;
Lang Hames56c0eb22014-11-19 19:15:41 +0000994
995 // Shift the significand of the addend right by one bit. This guarantees
996 // that the high bit of the significand is zero (same as fullSignificand),
997 // so the addition will overflow (if it does overflow at all) into the top bit.
998 lost_fraction = extendedAddend.shiftSignificandRight(1);
999 assert(lost_fraction == lfExactlyZero &&
1000 "Lost precision while shifting addend for fused-multiply-add.");
1001
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001002 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1003
1004 /* Restore our state. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001005 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001006 fullSignificand[0] = significand.part;
1007 significand = savedSignificand;
1008 semantics = savedSemantics;
1009
1010 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1011 }
1012
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001013 // Convert the result having "2 * precision" significant-bits back to the one
1014 // having "precision" significant-bits. First, move the radix point from
1015 // poision "2*precision - 1" to "precision - 1". The exponent need to be
1016 // adjusted by "2*precision - 1" - "precision - 1" = "precision".
Lang Hames56c0eb22014-11-19 19:15:41 +00001017 exponent -= precision + 1;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001018
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001019 // In case MSB resides at the left-hand side of radix point, shift the
1020 // mantissa right by some amount to make sure the MSB reside right before
1021 // the radix point (i.e. "MSB . rest-significant-bits").
1022 //
1023 // Note that the result is not normalized when "omsb < precision". So, the
1024 // caller needs to call APFloat::normalize() if normalized value is expected.
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001025 if (omsb > precision) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001026 unsigned int bits, significantParts;
1027 lostFraction lf;
1028
1029 bits = omsb - precision;
1030 significantParts = partCountForBits(omsb);
1031 lf = shiftRight(fullSignificand, significantParts, bits);
1032 lost_fraction = combineLostFractions(lf, lost_fraction);
1033 exponent += bits;
1034 }
1035
1036 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1037
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001038 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001039 delete [] fullSignificand;
1040
1041 return lost_fraction;
1042}
1043
1044/* Multiply the significands of LHS and RHS to DST. */
1045lostFraction
1046APFloat::divideSignificand(const APFloat &rhs)
1047{
1048 unsigned int bit, i, partsCount;
1049 const integerPart *rhsSignificand;
1050 integerPart *lhsSignificand, *dividend, *divisor;
1051 integerPart scratch[4];
1052 lostFraction lost_fraction;
1053
1054 assert(semantics == rhs.semantics);
1055
1056 lhsSignificand = significandParts();
1057 rhsSignificand = rhs.significandParts();
1058 partsCount = partCount();
1059
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001060 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001061 dividend = new integerPart[partsCount * 2];
1062 else
1063 dividend = scratch;
1064
1065 divisor = dividend + partsCount;
1066
1067 /* Copy the dividend and divisor as they will be modified in-place. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001068 for (i = 0; i < partsCount; i++) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001069 dividend[i] = lhsSignificand[i];
1070 divisor[i] = rhsSignificand[i];
1071 lhsSignificand[i] = 0;
1072 }
1073
1074 exponent -= rhs.exponent;
1075
1076 unsigned int precision = semantics->precision;
1077
1078 /* Normalize the divisor. */
1079 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001080 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001081 exponent += bit;
1082 APInt::tcShiftLeft(divisor, partsCount, bit);
1083 }
1084
1085 /* Normalize the dividend. */
1086 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001087 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001088 exponent -= bit;
1089 APInt::tcShiftLeft(dividend, partsCount, bit);
1090 }
1091
Neil Boothb93d90e2007-10-12 16:02:31 +00001092 /* Ensure the dividend >= divisor initially for the loop below.
1093 Incidentally, this means that the division loop below is
1094 guaranteed to set the integer bit to one. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001095 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001096 exponent--;
1097 APInt::tcShiftLeft(dividend, partsCount, 1);
1098 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
1099 }
1100
1101 /* Long division. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001102 for (bit = precision; bit; bit -= 1) {
1103 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001104 APInt::tcSubtract(dividend, divisor, 0, partsCount);
1105 APInt::tcSetBit(lhsSignificand, bit - 1);
1106 }
1107
1108 APInt::tcShiftLeft(dividend, partsCount, 1);
1109 }
1110
1111 /* Figure out the lost fraction. */
1112 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1113
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001114 if (cmp > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001115 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001116 else if (cmp == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001117 lost_fraction = lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001118 else if (APInt::tcIsZero(dividend, partsCount))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001119 lost_fraction = lfExactlyZero;
1120 else
1121 lost_fraction = lfLessThanHalf;
1122
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001123 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001124 delete [] dividend;
1125
1126 return lost_fraction;
1127}
1128
1129unsigned int
1130APFloat::significandMSB() const
1131{
1132 return APInt::tcMSB(significandParts(), partCount());
1133}
1134
1135unsigned int
1136APFloat::significandLSB() const
1137{
1138 return APInt::tcLSB(significandParts(), partCount());
1139}
1140
1141/* Note that a zero result is NOT normalized to fcZero. */
1142lostFraction
1143APFloat::shiftSignificandRight(unsigned int bits)
1144{
1145 /* Our exponent should not overflow. */
Michael Gottesman9dc98332013-06-24 04:06:23 +00001146 assert((ExponentType) (exponent + bits) >= exponent);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001147
1148 exponent += bits;
1149
1150 return shiftRight(significandParts(), partCount(), bits);
1151}
1152
1153/* Shift the significand left BITS bits, subtract BITS from its exponent. */
1154void
1155APFloat::shiftSignificandLeft(unsigned int bits)
1156{
1157 assert(bits < semantics->precision);
1158
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001159 if (bits) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001160 unsigned int partsCount = partCount();
1161
1162 APInt::tcShiftLeft(significandParts(), partsCount, bits);
1163 exponent -= bits;
1164
1165 assert(!APInt::tcIsZero(significandParts(), partsCount));
1166 }
1167}
1168
1169APFloat::cmpResult
1170APFloat::compareAbsoluteValue(const APFloat &rhs) const
1171{
1172 int compare;
1173
1174 assert(semantics == rhs.semantics);
Michael Gottesman8136c382013-06-26 23:17:28 +00001175 assert(isFiniteNonZero());
1176 assert(rhs.isFiniteNonZero());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001177
1178 compare = exponent - rhs.exponent;
1179
1180 /* If exponents are equal, do an unsigned bignum comparison of the
1181 significands. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001182 if (compare == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001183 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001184 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001185
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001186 if (compare > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001187 return cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001188 else if (compare < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001189 return cmpLessThan;
1190 else
1191 return cmpEqual;
1192}
1193
1194/* Handle overflow. Sign is preserved. We either become infinity or
1195 the largest finite number. */
1196APFloat::opStatus
1197APFloat::handleOverflow(roundingMode rounding_mode)
1198{
1199 /* Infinity? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001200 if (rounding_mode == rmNearestTiesToEven ||
1201 rounding_mode == rmNearestTiesToAway ||
1202 (rounding_mode == rmTowardPositive && !sign) ||
1203 (rounding_mode == rmTowardNegative && sign)) {
1204 category = fcInfinity;
1205 return (opStatus) (opOverflow | opInexact);
1206 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001207
1208 /* Otherwise we become the largest finite number. */
1209 category = fcNormal;
1210 exponent = semantics->maxExponent;
1211 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001212 semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001213
1214 return opInexact;
1215}
1216
Neil Booth1ca1f802007-10-03 15:16:41 +00001217/* Returns TRUE if, when truncating the current number, with BIT the
1218 new LSB, with the given lost fraction and rounding mode, the result
1219 would need to be rounded away from zero (i.e., by increasing the
1220 signficand). This routine must work for fcZero of both signs, and
1221 fcNormal numbers. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001222bool
1223APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Booth1ca1f802007-10-03 15:16:41 +00001224 lostFraction lost_fraction,
1225 unsigned int bit) const
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001226{
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001227 /* NaNs and infinities should not have lost fractions. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001228 assert(isFiniteNonZero() || category == fcZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001229
Neil Booth1ca1f802007-10-03 15:16:41 +00001230 /* Current callers never pass this so we don't handle it. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001231 assert(lost_fraction != lfExactlyZero);
1232
Mike Stump889285d2009-05-13 23:23:20 +00001233 switch (rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001234 case rmNearestTiesToAway:
1235 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1236
1237 case rmNearestTiesToEven:
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001238 if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001239 return true;
1240
1241 /* Our zeroes don't have a significand to test. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001242 if (lost_fraction == lfExactlyHalf && category != fcZero)
Neil Booth1ca1f802007-10-03 15:16:41 +00001243 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001244
1245 return false;
1246
1247 case rmTowardZero:
1248 return false;
1249
1250 case rmTowardPositive:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001251 return !sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001252
1253 case rmTowardNegative:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001254 return sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001255 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00001256 llvm_unreachable("Invalid rounding mode found");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001257}
1258
1259APFloat::opStatus
1260APFloat::normalize(roundingMode rounding_mode,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001261 lostFraction lost_fraction)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001262{
Neil Booth9acbf5a2007-09-26 21:33:42 +00001263 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001264 int exponentChange;
1265
Michael Gottesman8136c382013-06-26 23:17:28 +00001266 if (!isFiniteNonZero())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001267 return opOK;
1268
1269 /* Before rounding normalize the exponent of fcNormal numbers. */
1270 omsb = significandMSB() + 1;
1271
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001272 if (omsb) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001273 /* OMSB is numbered from 1. We want to place it in the integer
Nick Lewyckyf66daac2011-10-03 21:30:08 +00001274 bit numbered PRECISION if possible, with a compensating change in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001275 the exponent. */
1276 exponentChange = omsb - semantics->precision;
1277
1278 /* If the resulting exponent is too high, overflow according to
1279 the rounding mode. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001280 if (exponent + exponentChange > semantics->maxExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001281 return handleOverflow(rounding_mode);
1282
1283 /* Subnormal numbers have exponent minExponent, and their MSB
1284 is forced based on that. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001285 if (exponent + exponentChange < semantics->minExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001286 exponentChange = semantics->minExponent - exponent;
1287
1288 /* Shifting left is easy as we don't lose precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001289 if (exponentChange < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001290 assert(lost_fraction == lfExactlyZero);
1291
1292 shiftSignificandLeft(-exponentChange);
1293
1294 return opOK;
1295 }
1296
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001297 if (exponentChange > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001298 lostFraction lf;
1299
1300 /* Shift right and capture any new lost fraction. */
1301 lf = shiftSignificandRight(exponentChange);
1302
1303 lost_fraction = combineLostFractions(lf, lost_fraction);
1304
1305 /* Keep OMSB up-to-date. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001306 if (omsb > (unsigned) exponentChange)
Neil Boothb93d90e2007-10-12 16:02:31 +00001307 omsb -= exponentChange;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001308 else
Neil Booth9acbf5a2007-09-26 21:33:42 +00001309 omsb = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001310 }
1311 }
1312
1313 /* Now round the number according to rounding_mode given the lost
1314 fraction. */
1315
1316 /* As specified in IEEE 754, since we do not trap we do not report
1317 underflow for exact results. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001318 if (lost_fraction == lfExactlyZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001319 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001320 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001321 category = fcZero;
1322
1323 return opOK;
1324 }
1325
1326 /* Increment the significand if we're rounding away from zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001327 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1328 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001329 exponent = semantics->minExponent;
1330
1331 incrementSignificand();
1332 omsb = significandMSB() + 1;
1333
1334 /* Did the significand increment overflow? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001335 if (omsb == (unsigned) semantics->precision + 1) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001336 /* Renormalize by incrementing the exponent and shifting our
Neil Booth9acbf5a2007-09-26 21:33:42 +00001337 significand right one. However if we already have the
1338 maximum exponent we overflow to infinity. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001339 if (exponent == semantics->maxExponent) {
Neil Booth9acbf5a2007-09-26 21:33:42 +00001340 category = fcInfinity;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001341
Neil Booth9acbf5a2007-09-26 21:33:42 +00001342 return (opStatus) (opOverflow | opInexact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001343 }
1344
1345 shiftSignificandRight(1);
1346
1347 return opInexact;
1348 }
1349 }
1350
1351 /* The normal case - we were and are not denormal, and any
1352 significand increment above didn't overflow. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001353 if (omsb == semantics->precision)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001354 return opInexact;
1355
1356 /* We have a non-zero denormal. */
1357 assert(omsb < semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001358
1359 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001360 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001361 category = fcZero;
1362
1363 /* The fcZero case is a denormal that underflowed to zero. */
1364 return (opStatus) (opUnderflow | opInexact);
1365}
1366
1367APFloat::opStatus
1368APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1369{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001370 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001371 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001372 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001373
Michael Gottesman9b877e12013-06-24 09:57:57 +00001374 case PackCategoriesIntoKey(fcNaN, fcZero):
1375 case PackCategoriesIntoKey(fcNaN, fcNormal):
1376 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1377 case PackCategoriesIntoKey(fcNaN, fcNaN):
1378 case PackCategoriesIntoKey(fcNormal, fcZero):
1379 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1380 case PackCategoriesIntoKey(fcInfinity, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001381 return opOK;
1382
Michael Gottesman9b877e12013-06-24 09:57:57 +00001383 case PackCategoriesIntoKey(fcZero, fcNaN):
1384 case PackCategoriesIntoKey(fcNormal, fcNaN):
1385 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Stephen Canond3278282014-06-08 16:53:31 +00001386 // We need to be sure to flip the sign here for subtraction because we
1387 // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1388 sign = rhs.sign ^ subtract;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001389 category = fcNaN;
1390 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001391 return opOK;
1392
Michael Gottesman9b877e12013-06-24 09:57:57 +00001393 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1394 case PackCategoriesIntoKey(fcZero, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001395 category = fcInfinity;
1396 sign = rhs.sign ^ subtract;
1397 return opOK;
1398
Michael Gottesman9b877e12013-06-24 09:57:57 +00001399 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001400 assign(rhs);
1401 sign = rhs.sign ^ subtract;
1402 return opOK;
1403
Michael Gottesman9b877e12013-06-24 09:57:57 +00001404 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001405 /* Sign depends on rounding mode; handled by caller. */
1406 return opOK;
1407
Michael Gottesman9b877e12013-06-24 09:57:57 +00001408 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001409 /* Differently signed infinities can only be validly
1410 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001411 if (((sign ^ rhs.sign)!=0) != subtract) {
Neil Booth5fe658b2007-10-14 10:39:51 +00001412 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001413 return opInvalidOp;
1414 }
1415
1416 return opOK;
1417
Michael Gottesman9b877e12013-06-24 09:57:57 +00001418 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001419 return opDivByZero;
1420 }
1421}
1422
1423/* Add or subtract two normal numbers. */
1424lostFraction
1425APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1426{
1427 integerPart carry;
1428 lostFraction lost_fraction;
1429 int bits;
1430
1431 /* Determine if the operation on the absolute values is effectively
1432 an addition or subtraction. */
Aaron Ballmand5cc45f2015-03-24 12:47:51 +00001433 subtract ^= static_cast<bool>(sign ^ rhs.sign);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001434
1435 /* Are we bigger exponent-wise than the RHS? */
1436 bits = exponent - rhs.exponent;
1437
1438 /* Subtraction is more subtle than one might naively expect. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001439 if (subtract) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001440 APFloat temp_rhs(rhs);
1441 bool reverse;
1442
Chris Lattner3da18eb2007-08-24 03:02:34 +00001443 if (bits == 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001444 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1445 lost_fraction = lfExactlyZero;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001446 } else if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001447 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1448 shiftSignificandLeft(1);
1449 reverse = false;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001450 } else {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001451 lost_fraction = shiftSignificandRight(-bits - 1);
1452 temp_rhs.shiftSignificandLeft(1);
1453 reverse = true;
1454 }
1455
Chris Lattner3da18eb2007-08-24 03:02:34 +00001456 if (reverse) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001457 carry = temp_rhs.subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001458 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001459 copySignificand(temp_rhs);
1460 sign = !sign;
1461 } else {
1462 carry = subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001463 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001464 }
1465
1466 /* Invert the lost fraction - it was on the RHS and
1467 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001468 if (lost_fraction == lfLessThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001469 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001470 else if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001471 lost_fraction = lfLessThanHalf;
1472
1473 /* The code above is intended to ensure that no borrow is
1474 necessary. */
1475 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001476 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001477 } else {
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001478 if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001479 APFloat temp_rhs(rhs);
1480
1481 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1482 carry = addSignificand(temp_rhs);
1483 } else {
1484 lost_fraction = shiftSignificandRight(-bits);
1485 carry = addSignificand(rhs);
1486 }
1487
1488 /* We have a guard bit; generating a carry cannot happen. */
1489 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001490 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001491 }
1492
1493 return lost_fraction;
1494}
1495
1496APFloat::opStatus
1497APFloat::multiplySpecials(const APFloat &rhs)
1498{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001499 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001500 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001501 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001502
Michael Gottesman9b877e12013-06-24 09:57:57 +00001503 case PackCategoriesIntoKey(fcNaN, fcZero):
1504 case PackCategoriesIntoKey(fcNaN, fcNormal):
1505 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1506 case PackCategoriesIntoKey(fcNaN, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001507 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001508 return opOK;
1509
Michael Gottesman9b877e12013-06-24 09:57:57 +00001510 case PackCategoriesIntoKey(fcZero, fcNaN):
1511 case PackCategoriesIntoKey(fcNormal, fcNaN):
1512 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001513 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001514 category = fcNaN;
1515 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001516 return opOK;
1517
Michael Gottesman9b877e12013-06-24 09:57:57 +00001518 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1519 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1520 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001521 category = fcInfinity;
1522 return opOK;
1523
Michael Gottesman9b877e12013-06-24 09:57:57 +00001524 case PackCategoriesIntoKey(fcZero, fcNormal):
1525 case PackCategoriesIntoKey(fcNormal, fcZero):
1526 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001527 category = fcZero;
1528 return opOK;
1529
Michael Gottesman9b877e12013-06-24 09:57:57 +00001530 case PackCategoriesIntoKey(fcZero, fcInfinity):
1531 case PackCategoriesIntoKey(fcInfinity, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001532 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001533 return opInvalidOp;
1534
Michael Gottesman9b877e12013-06-24 09:57:57 +00001535 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001536 return opOK;
1537 }
1538}
1539
1540APFloat::opStatus
1541APFloat::divideSpecials(const APFloat &rhs)
1542{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001543 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001544 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001545 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001546
Michael Gottesman9b877e12013-06-24 09:57:57 +00001547 case PackCategoriesIntoKey(fcZero, fcNaN):
1548 case PackCategoriesIntoKey(fcNormal, fcNaN):
1549 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001550 category = fcNaN;
1551 copySignificand(rhs);
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001552 case PackCategoriesIntoKey(fcNaN, fcZero):
1553 case PackCategoriesIntoKey(fcNaN, fcNormal):
1554 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1555 case PackCategoriesIntoKey(fcNaN, fcNaN):
1556 sign = false;
1557 case PackCategoriesIntoKey(fcInfinity, fcZero):
1558 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1559 case PackCategoriesIntoKey(fcZero, fcInfinity):
1560 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001561 return opOK;
1562
Michael Gottesman9b877e12013-06-24 09:57:57 +00001563 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001564 category = fcZero;
1565 return opOK;
1566
Michael Gottesman9b877e12013-06-24 09:57:57 +00001567 case PackCategoriesIntoKey(fcNormal, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001568 category = fcInfinity;
1569 return opDivByZero;
1570
Michael Gottesman9b877e12013-06-24 09:57:57 +00001571 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1572 case PackCategoriesIntoKey(fcZero, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001573 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001574 return opInvalidOp;
1575
Michael Gottesman9b877e12013-06-24 09:57:57 +00001576 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001577 return opOK;
1578 }
1579}
1580
Dale Johannesenb5721632009-01-21 00:35:19 +00001581APFloat::opStatus
1582APFloat::modSpecials(const APFloat &rhs)
1583{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001584 switch (PackCategoriesIntoKey(category, rhs.category)) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001585 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001586 llvm_unreachable(nullptr);
Dale Johannesenb5721632009-01-21 00:35:19 +00001587
Michael Gottesman9b877e12013-06-24 09:57:57 +00001588 case PackCategoriesIntoKey(fcNaN, fcZero):
1589 case PackCategoriesIntoKey(fcNaN, fcNormal):
1590 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1591 case PackCategoriesIntoKey(fcNaN, fcNaN):
1592 case PackCategoriesIntoKey(fcZero, fcInfinity):
1593 case PackCategoriesIntoKey(fcZero, fcNormal):
1594 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Dale Johannesenb5721632009-01-21 00:35:19 +00001595 return opOK;
1596
Michael Gottesman9b877e12013-06-24 09:57:57 +00001597 case PackCategoriesIntoKey(fcZero, fcNaN):
1598 case PackCategoriesIntoKey(fcNormal, fcNaN):
1599 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001600 sign = false;
Dale Johannesenb5721632009-01-21 00:35:19 +00001601 category = fcNaN;
1602 copySignificand(rhs);
1603 return opOK;
1604
Michael Gottesman9b877e12013-06-24 09:57:57 +00001605 case PackCategoriesIntoKey(fcNormal, fcZero):
1606 case PackCategoriesIntoKey(fcInfinity, fcZero):
1607 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1608 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1609 case PackCategoriesIntoKey(fcZero, fcZero):
Dale Johannesenb5721632009-01-21 00:35:19 +00001610 makeNaN();
1611 return opInvalidOp;
1612
Michael Gottesman9b877e12013-06-24 09:57:57 +00001613 case PackCategoriesIntoKey(fcNormal, fcNormal):
Dale Johannesenb5721632009-01-21 00:35:19 +00001614 return opOK;
1615 }
1616}
1617
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001618/* Change sign. */
1619void
1620APFloat::changeSign()
1621{
1622 /* Look mummy, this one's easy. */
1623 sign = !sign;
1624}
1625
Dale Johannesen689d17d2007-08-31 23:35:31 +00001626void
1627APFloat::clearSign()
1628{
1629 /* So is this one. */
1630 sign = 0;
1631}
1632
1633void
1634APFloat::copySign(const APFloat &rhs)
1635{
1636 /* And this one. */
1637 sign = rhs.sign;
1638}
1639
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001640/* Normalized addition or subtraction. */
1641APFloat::opStatus
1642APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001643 bool subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001644{
1645 opStatus fs;
1646
1647 fs = addOrSubtractSpecials(rhs, subtract);
1648
1649 /* This return code means it was not a simple case. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001650 if (fs == opDivByZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001651 lostFraction lost_fraction;
1652
1653 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1654 fs = normalize(rounding_mode, lost_fraction);
1655
1656 /* Can only be zero if we lost no fraction. */
1657 assert(category != fcZero || lost_fraction == lfExactlyZero);
1658 }
1659
1660 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1661 positive zero unless rounding to minus infinity, except that
1662 adding two like-signed zeroes gives that zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001663 if (category == fcZero) {
1664 if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001665 sign = (rounding_mode == rmTowardNegative);
1666 }
1667
1668 return fs;
1669}
1670
1671/* Normalized addition. */
1672APFloat::opStatus
1673APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1674{
1675 return addOrSubtract(rhs, rounding_mode, false);
1676}
1677
1678/* Normalized subtraction. */
1679APFloat::opStatus
1680APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1681{
1682 return addOrSubtract(rhs, rounding_mode, true);
1683}
1684
1685/* Normalized multiply. */
1686APFloat::opStatus
1687APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1688{
1689 opStatus fs;
1690
1691 sign ^= rhs.sign;
1692 fs = multiplySpecials(rhs);
1693
Michael Gottesman8136c382013-06-26 23:17:28 +00001694 if (isFiniteNonZero()) {
Craig Topperc10719f2014-04-07 04:17:22 +00001695 lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001696 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001697 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001698 fs = (opStatus) (fs | opInexact);
1699 }
1700
1701 return fs;
1702}
1703
1704/* Normalized divide. */
1705APFloat::opStatus
1706APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1707{
1708 opStatus fs;
1709
1710 sign ^= rhs.sign;
1711 fs = divideSpecials(rhs);
1712
Michael Gottesman8136c382013-06-26 23:17:28 +00001713 if (isFiniteNonZero()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001714 lostFraction lost_fraction = divideSignificand(rhs);
1715 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001716 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001717 fs = (opStatus) (fs | opInexact);
1718 }
1719
1720 return fs;
1721}
1722
Dale Johannesenfe750172009-01-20 18:35:05 +00001723/* Normalized remainder. This is not currently correct in all cases. */
1724APFloat::opStatus
1725APFloat::remainder(const APFloat &rhs)
1726{
1727 opStatus fs;
1728 APFloat V = *this;
1729 unsigned int origSign = sign;
1730
Dale Johannesenfe750172009-01-20 18:35:05 +00001731 fs = V.divide(rhs, rmNearestTiesToEven);
1732 if (fs == opDivByZero)
1733 return fs;
1734
1735 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001736 integerPart *x = new integerPart[parts];
Dale Johannesenfe750172009-01-20 18:35:05 +00001737 bool ignored;
1738 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1739 rmNearestTiesToEven, &ignored);
1740 if (fs==opInvalidOp)
1741 return fs;
1742
1743 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1744 rmNearestTiesToEven);
1745 assert(fs==opOK); // should always work
1746
1747 fs = V.multiply(rhs, rmNearestTiesToEven);
1748 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1749
1750 fs = subtract(V, rmNearestTiesToEven);
1751 assert(fs==opOK || fs==opInexact); // likewise
1752
1753 if (isZero())
1754 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001755 delete[] x;
Dale Johannesenfe750172009-01-20 18:35:05 +00001756 return fs;
1757}
1758
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001759/* Normalized llvm frem (C fmod).
Dale Johannesenfe750172009-01-20 18:35:05 +00001760 This is not currently correct in all cases. */
Dale Johannesen689d17d2007-08-31 23:35:31 +00001761APFloat::opStatus
1762APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1763{
1764 opStatus fs;
Dale Johannesenb5721632009-01-21 00:35:19 +00001765 fs = modSpecials(rhs);
Dale Johannesen689d17d2007-08-31 23:35:31 +00001766
Michael Gottesman8136c382013-06-26 23:17:28 +00001767 if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001768 APFloat V = *this;
1769 unsigned int origSign = sign;
Dale Johannesen689d17d2007-08-31 23:35:31 +00001770
Dale Johannesenb5721632009-01-21 00:35:19 +00001771 fs = V.divide(rhs, rmNearestTiesToEven);
1772 if (fs == opDivByZero)
1773 return fs;
Dale Johannesen728687c2007-09-05 20:39:49 +00001774
Dale Johannesenb5721632009-01-21 00:35:19 +00001775 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001776 integerPart *x = new integerPart[parts];
Dale Johannesenb5721632009-01-21 00:35:19 +00001777 bool ignored;
1778 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1779 rmTowardZero, &ignored);
1780 if (fs==opInvalidOp)
1781 return fs;
Dale Johannesen728687c2007-09-05 20:39:49 +00001782
Dale Johannesenb5721632009-01-21 00:35:19 +00001783 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1784 rmNearestTiesToEven);
1785 assert(fs==opOK); // should always work
Dale Johannesen728687c2007-09-05 20:39:49 +00001786
Dale Johannesenb5721632009-01-21 00:35:19 +00001787 fs = V.multiply(rhs, rounding_mode);
1788 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1789
1790 fs = subtract(V, rounding_mode);
1791 assert(fs==opOK || fs==opInexact); // likewise
1792
1793 if (isZero())
1794 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001795 delete[] x;
Dale Johannesenb5721632009-01-21 00:35:19 +00001796 }
Dale Johannesen689d17d2007-08-31 23:35:31 +00001797 return fs;
1798}
1799
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001800/* Normalized fused-multiply-add. */
1801APFloat::opStatus
1802APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001803 const APFloat &addend,
1804 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001805{
1806 opStatus fs;
1807
1808 /* Post-multiplication sign, before addition. */
1809 sign ^= multiplicand.sign;
1810
1811 /* If and only if all arguments are normal do we need to do an
1812 extended-precision calculation. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001813 if (isFiniteNonZero() &&
1814 multiplicand.isFiniteNonZero() &&
Hal Finkel171c2ec2014-10-14 19:23:07 +00001815 addend.isFinite()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001816 lostFraction lost_fraction;
1817
1818 lost_fraction = multiplySignificand(multiplicand, &addend);
1819 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001820 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001821 fs = (opStatus) (fs | opInexact);
1822
1823 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1824 positive zero unless rounding to minus infinity, except that
1825 adding two like-signed zeroes gives that zero. */
Lang Hames12b12e82015-01-04 01:20:55 +00001826 if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001827 sign = (rounding_mode == rmTowardNegative);
1828 } else {
1829 fs = multiplySpecials(multiplicand);
1830
1831 /* FS can only be opOK or opInvalidOp. There is no more work
1832 to do in the latter case. The IEEE-754R standard says it is
1833 implementation-defined in this case whether, if ADDEND is a
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001834 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001835
1836 If we need to do the addition we can do so with normal
1837 precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001838 if (fs == opOK)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001839 fs = addOrSubtract(addend, rounding_mode, false);
1840 }
1841
1842 return fs;
1843}
1844
Owen Andersona40319b2012-08-13 23:32:49 +00001845/* Rounding-mode corrrect round to integral value. */
1846APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
1847 opStatus fs;
Owen Andersona40319b2012-08-13 23:32:49 +00001848
Owen Anderson352dfff2012-08-15 18:28:45 +00001849 // If the exponent is large enough, we know that this value is already
1850 // integral, and the arithmetic below would potentially cause it to saturate
1851 // to +/-Inf. Bail out early instead.
Michael Gottesman8136c382013-06-26 23:17:28 +00001852 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
Owen Anderson352dfff2012-08-15 18:28:45 +00001853 return opOK;
1854
Owen Andersona40319b2012-08-13 23:32:49 +00001855 // The algorithm here is quite simple: we add 2^(p-1), where p is the
1856 // precision of our format, and then subtract it back off again. The choice
1857 // of rounding modes for the addition/subtraction determines the rounding mode
1858 // for our integral rounding as well.
Owen Andersonbe7e2972012-08-15 16:42:53 +00001859 // NOTE: When the input value is negative, we do subtraction followed by
Owen Anderson1ff74b02012-08-15 05:39:46 +00001860 // addition instead.
Owen Anderson0b357222012-08-14 18:51:15 +00001861 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1862 IntegerConstant <<= semanticsPrecision(*semantics)-1;
Owen Andersona40319b2012-08-13 23:32:49 +00001863 APFloat MagicConstant(*semantics);
1864 fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1865 rmNearestTiesToEven);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001866 MagicConstant.copySign(*this);
1867
Owen Andersona40319b2012-08-13 23:32:49 +00001868 if (fs != opOK)
1869 return fs;
1870
Owen Anderson1ff74b02012-08-15 05:39:46 +00001871 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1872 bool inputSign = isNegative();
1873
Owen Andersona40319b2012-08-13 23:32:49 +00001874 fs = add(MagicConstant, rounding_mode);
1875 if (fs != opOK && fs != opInexact)
1876 return fs;
1877
1878 fs = subtract(MagicConstant, rounding_mode);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001879
1880 // Restore the input sign.
1881 if (inputSign != isNegative())
1882 changeSign();
1883
Owen Andersona40319b2012-08-13 23:32:49 +00001884 return fs;
1885}
1886
1887
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001888/* Comparison requires normalized numbers. */
1889APFloat::cmpResult
1890APFloat::compare(const APFloat &rhs) const
1891{
1892 cmpResult result;
1893
1894 assert(semantics == rhs.semantics);
1895
Michael Gottesman9b877e12013-06-24 09:57:57 +00001896 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001897 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001898 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001899
Michael Gottesman9b877e12013-06-24 09:57:57 +00001900 case PackCategoriesIntoKey(fcNaN, fcZero):
1901 case PackCategoriesIntoKey(fcNaN, fcNormal):
1902 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1903 case PackCategoriesIntoKey(fcNaN, fcNaN):
1904 case PackCategoriesIntoKey(fcZero, fcNaN):
1905 case PackCategoriesIntoKey(fcNormal, fcNaN):
1906 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001907 return cmpUnordered;
1908
Michael Gottesman9b877e12013-06-24 09:57:57 +00001909 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1910 case PackCategoriesIntoKey(fcInfinity, fcZero):
1911 case PackCategoriesIntoKey(fcNormal, fcZero):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001912 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001913 return cmpLessThan;
1914 else
1915 return cmpGreaterThan;
1916
Michael Gottesman9b877e12013-06-24 09:57:57 +00001917 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1918 case PackCategoriesIntoKey(fcZero, fcInfinity):
1919 case PackCategoriesIntoKey(fcZero, fcNormal):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001920 if (rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001921 return cmpGreaterThan;
1922 else
1923 return cmpLessThan;
1924
Michael Gottesman9b877e12013-06-24 09:57:57 +00001925 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001926 if (sign == rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001927 return cmpEqual;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001928 else if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001929 return cmpLessThan;
1930 else
1931 return cmpGreaterThan;
1932
Michael Gottesman9b877e12013-06-24 09:57:57 +00001933 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001934 return cmpEqual;
1935
Michael Gottesman9b877e12013-06-24 09:57:57 +00001936 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001937 break;
1938 }
1939
1940 /* Two normal numbers. Do they have the same sign? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001941 if (sign != rhs.sign) {
1942 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001943 result = cmpLessThan;
1944 else
1945 result = cmpGreaterThan;
1946 } else {
1947 /* Compare absolute values; invert result if negative. */
1948 result = compareAbsoluteValue(rhs);
1949
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001950 if (sign) {
1951 if (result == cmpLessThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001952 result = cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001953 else if (result == cmpGreaterThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001954 result = cmpLessThan;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001955 }
1956 }
1957
1958 return result;
1959}
1960
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001961/// APFloat::convert - convert a value of one floating point type to another.
1962/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1963/// records whether the transformation lost information, i.e. whether
1964/// converting the result back to the original type will produce the
1965/// original value (this is almost the same as return value==fsOK, but there
1966/// are edge cases where this is not so).
1967
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001968APFloat::opStatus
1969APFloat::convert(const fltSemantics &toSemantics,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001970 roundingMode rounding_mode, bool *losesInfo)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001971{
Neil Bootha8d72692007-09-22 02:56:19 +00001972 lostFraction lostFraction;
1973 unsigned int newPartCount, oldPartCount;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001974 opStatus fs;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001975 int shift;
1976 const fltSemantics &fromSemantics = *semantics;
Neil Booth9acbf5a2007-09-26 21:33:42 +00001977
Neil Bootha8d72692007-09-22 02:56:19 +00001978 lostFraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001979 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Bootha8d72692007-09-22 02:56:19 +00001980 oldPartCount = partCount();
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001981 shift = toSemantics.precision - fromSemantics.precision;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001982
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001983 bool X86SpecialNan = false;
1984 if (&fromSemantics == &APFloat::x87DoubleExtended &&
1985 &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN &&
1986 (!(*significandParts() & 0x8000000000000000ULL) ||
1987 !(*significandParts() & 0x4000000000000000ULL))) {
1988 // x86 has some unusual NaNs which cannot be represented in any other
1989 // format; note them here.
1990 X86SpecialNan = true;
1991 }
1992
Ulrich Weigand1d4dbda2013-07-16 13:03:25 +00001993 // If this is a truncation of a denormal number, and the target semantics
1994 // has larger exponent range than the source semantics (this can happen
1995 // when truncating from PowerPC double-double to double format), the
1996 // right shift could lose result mantissa bits. Adjust exponent instead
1997 // of performing excessive shift.
1998 if (shift < 0 && isFiniteNonZero()) {
1999 int exponentChange = significandMSB() + 1 - fromSemantics.precision;
2000 if (exponent + exponentChange < toSemantics.minExponent)
2001 exponentChange = toSemantics.minExponent - exponent;
2002 if (exponentChange < shift)
2003 exponentChange = shift;
2004 if (exponentChange < 0) {
2005 shift -= exponentChange;
2006 exponent += exponentChange;
2007 }
2008 }
2009
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002010 // If this is a truncation, perform the shift before we narrow the storage.
Michael Gottesman8136c382013-06-26 23:17:28 +00002011 if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002012 lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
2013
2014 // Fix the storage so it can hold to new value.
Neil Bootha8d72692007-09-22 02:56:19 +00002015 if (newPartCount > oldPartCount) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002016 // The new type requires more storage; make it available.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002017 integerPart *newParts;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002018 newParts = new integerPart[newPartCount];
2019 APInt::tcSet(newParts, 0, newPartCount);
Michael Gottesman8136c382013-06-26 23:17:28 +00002020 if (isFiniteNonZero() || category==fcNaN)
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002021 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002022 freeSignificand();
2023 significand.parts = newParts;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002024 } else if (newPartCount == 1 && oldPartCount != 1) {
2025 // Switch to built-in storage for a single part.
2026 integerPart newPart = 0;
Michael Gottesman8136c382013-06-26 23:17:28 +00002027 if (isFiniteNonZero() || category==fcNaN)
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002028 newPart = significandParts()[0];
2029 freeSignificand();
2030 significand.part = newPart;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002031 }
2032
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002033 // Now that we have the right storage, switch the semantics.
2034 semantics = &toSemantics;
2035
2036 // If this is an extension, perform the shift now that the storage is
2037 // available.
Michael Gottesman8136c382013-06-26 23:17:28 +00002038 if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002039 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2040
Michael Gottesman8136c382013-06-26 23:17:28 +00002041 if (isFiniteNonZero()) {
Neil Bootha8d72692007-09-22 02:56:19 +00002042 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002043 *losesInfo = (fs != opOK);
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002044 } else if (category == fcNaN) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002045 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
Benjamin Kramerb361adb2013-01-25 17:01:00 +00002046
2047 // For x87 extended precision, we want to make a NaN, not a special NaN if
2048 // the input wasn't special either.
2049 if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
2050 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2051
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002052 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2053 // does not give you back the same bits. This is dubious, and we
2054 // don't currently do it. You're really supposed to get
2055 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002056 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002057 } else {
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002058 *losesInfo = false;
Eli Friedman31f01162011-11-28 18:50:37 +00002059 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002060 }
2061
2062 return fs;
2063}
2064
2065/* Convert a floating point number to an integer according to the
2066 rounding mode. If the rounded integer value is out of range this
Neil Booth618d0fc2007-11-01 22:43:37 +00002067 returns an invalid operation exception and the contents of the
2068 destination parts are unspecified. If the rounded value is in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002069 range but the floating point number is not the exact integer, the C
2070 standard doesn't require an inexact exception to be raised. IEEE
2071 854 does require it so we do that.
2072
2073 Note that for conversions to integer type the C standard requires
2074 round-to-zero to always be used. */
2075APFloat::opStatus
Neil Booth618d0fc2007-11-01 22:43:37 +00002076APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
2077 bool isSigned,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002078 roundingMode rounding_mode,
2079 bool *isExact) const
Neil Booth618d0fc2007-11-01 22:43:37 +00002080{
2081 lostFraction lost_fraction;
2082 const integerPart *src;
2083 unsigned int dstPartsCount, truncatedBits;
2084
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002085 *isExact = false;
2086
Neil Booth618d0fc2007-11-01 22:43:37 +00002087 /* Handle the three special cases first. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002088 if (category == fcInfinity || category == fcNaN)
Neil Booth618d0fc2007-11-01 22:43:37 +00002089 return opInvalidOp;
2090
2091 dstPartsCount = partCountForBits(width);
2092
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002093 if (category == fcZero) {
Neil Booth618d0fc2007-11-01 22:43:37 +00002094 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen7221af32008-10-07 00:40:01 +00002095 // Negative zero can't be represented as an int.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002096 *isExact = !sign;
2097 return opOK;
Neil Booth618d0fc2007-11-01 22:43:37 +00002098 }
2099
2100 src = significandParts();
2101
2102 /* Step 1: place our absolute value, with any fraction truncated, in
2103 the destination. */
2104 if (exponent < 0) {
2105 /* Our absolute value is less than one; truncate everything. */
2106 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen740e9872009-01-19 21:17:05 +00002107 /* For exponent -1 the integer bit represents .5, look at that.
2108 For smaller exponents leftmost truncated bit is 0. */
2109 truncatedBits = semantics->precision -1U - exponent;
Neil Booth618d0fc2007-11-01 22:43:37 +00002110 } else {
2111 /* We want the most significant (exponent + 1) bits; the rest are
2112 truncated. */
2113 unsigned int bits = exponent + 1U;
2114
2115 /* Hopelessly large in magnitude? */
2116 if (bits > width)
2117 return opInvalidOp;
2118
2119 if (bits < semantics->precision) {
2120 /* We truncate (semantics->precision - bits) bits. */
2121 truncatedBits = semantics->precision - bits;
2122 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
2123 } else {
2124 /* We want at least as many bits as are available. */
2125 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
2126 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
2127 truncatedBits = 0;
2128 }
2129 }
2130
2131 /* Step 2: work out any lost fraction, and increment the absolute
2132 value if we would round away from zero. */
2133 if (truncatedBits) {
2134 lost_fraction = lostFractionThroughTruncation(src, partCount(),
2135 truncatedBits);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002136 if (lost_fraction != lfExactlyZero &&
2137 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
Neil Booth618d0fc2007-11-01 22:43:37 +00002138 if (APInt::tcIncrement(parts, dstPartsCount))
2139 return opInvalidOp; /* Overflow. */
2140 }
2141 } else {
2142 lost_fraction = lfExactlyZero;
2143 }
2144
2145 /* Step 3: check if we fit in the destination. */
2146 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
2147
2148 if (sign) {
2149 if (!isSigned) {
2150 /* Negative numbers cannot be represented as unsigned. */
2151 if (omsb != 0)
2152 return opInvalidOp;
2153 } else {
2154 /* It takes omsb bits to represent the unsigned integer value.
2155 We lose a bit for the sign, but care is needed as the
2156 maximally negative integer is a special case. */
2157 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2158 return opInvalidOp;
2159
2160 /* This case can happen because of rounding. */
2161 if (omsb > width)
2162 return opInvalidOp;
2163 }
2164
2165 APInt::tcNegate (parts, dstPartsCount);
2166 } else {
2167 if (omsb >= width + !isSigned)
2168 return opInvalidOp;
2169 }
2170
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002171 if (lost_fraction == lfExactlyZero) {
2172 *isExact = true;
Neil Booth618d0fc2007-11-01 22:43:37 +00002173 return opOK;
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002174 } else
Neil Booth618d0fc2007-11-01 22:43:37 +00002175 return opInexact;
2176}
2177
2178/* Same as convertToSignExtendedInteger, except we provide
2179 deterministic values in case of an invalid operation exception,
2180 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002181 for underflow or overflow.
2182 The *isExact output tells whether the result is exact, in the sense
2183 that converting it back to the original floating point type produces
2184 the original value. This is almost equivalent to result==opOK,
2185 except for negative zeroes.
2186*/
Neil Booth618d0fc2007-11-01 22:43:37 +00002187APFloat::opStatus
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002188APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth9acbf5a2007-09-26 21:33:42 +00002189 bool isSigned,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002190 roundingMode rounding_mode, bool *isExact) const
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002191{
Neil Booth618d0fc2007-11-01 22:43:37 +00002192 opStatus fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002193
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002194 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002195 isExact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002196
Neil Booth618d0fc2007-11-01 22:43:37 +00002197 if (fs == opInvalidOp) {
2198 unsigned int bits, dstPartsCount;
2199
2200 dstPartsCount = partCountForBits(width);
2201
2202 if (category == fcNaN)
2203 bits = 0;
2204 else if (sign)
2205 bits = isSigned;
2206 else
2207 bits = width - isSigned;
2208
2209 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2210 if (sign && isSigned)
2211 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002212 }
2213
Neil Booth618d0fc2007-11-01 22:43:37 +00002214 return fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002215}
2216
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +00002217/* Same as convertToInteger(integerPart*, ...), except the result is returned in
2218 an APSInt, whose initial bit-width and signed-ness are used to determine the
2219 precision of the conversion.
2220 */
2221APFloat::opStatus
2222APFloat::convertToInteger(APSInt &result,
2223 roundingMode rounding_mode, bool *isExact) const
2224{
2225 unsigned bitWidth = result.getBitWidth();
2226 SmallVector<uint64_t, 4> parts(result.getNumWords());
2227 opStatus status = convertToInteger(
2228 parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
2229 // Keeps the original signed-ness.
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002230 result = APInt(bitWidth, parts);
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +00002231 return status;
2232}
2233
Neil Booth6c1c8582007-10-07 12:07:53 +00002234/* Convert an unsigned integer SRC to a floating point number,
2235 rounding according to ROUNDING_MODE. The sign of the floating
2236 point number is not modified. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002237APFloat::opStatus
Neil Booth6c1c8582007-10-07 12:07:53 +00002238APFloat::convertFromUnsignedParts(const integerPart *src,
2239 unsigned int srcCount,
2240 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002241{
Neil Booth49c6aab2007-10-08 14:39:42 +00002242 unsigned int omsb, precision, dstCount;
Neil Booth6c1c8582007-10-07 12:07:53 +00002243 integerPart *dst;
Neil Booth49c6aab2007-10-08 14:39:42 +00002244 lostFraction lost_fraction;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002245
2246 category = fcNormal;
Neil Booth49c6aab2007-10-08 14:39:42 +00002247 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002248 dst = significandParts();
2249 dstCount = partCount();
Neil Booth49c6aab2007-10-08 14:39:42 +00002250 precision = semantics->precision;
Neil Booth6c1c8582007-10-07 12:07:53 +00002251
Nick Lewyckyf66daac2011-10-03 21:30:08 +00002252 /* We want the most significant PRECISION bits of SRC. There may not
Neil Booth49c6aab2007-10-08 14:39:42 +00002253 be that many; extract what we can. */
2254 if (precision <= omsb) {
2255 exponent = omsb - 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002256 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth49c6aab2007-10-08 14:39:42 +00002257 omsb - precision);
2258 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2259 } else {
2260 exponent = precision - 1;
2261 lost_fraction = lfExactlyZero;
2262 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth6c1c8582007-10-07 12:07:53 +00002263 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002264
2265 return normalize(rounding_mode, lost_fraction);
2266}
2267
Dan Gohman35723eb2008-02-29 01:26:11 +00002268APFloat::opStatus
2269APFloat::convertFromAPInt(const APInt &Val,
2270 bool isSigned,
2271 roundingMode rounding_mode)
2272{
2273 unsigned int partCount = Val.getNumWords();
2274 APInt api = Val;
2275
2276 sign = false;
2277 if (isSigned && api.isNegative()) {
2278 sign = true;
2279 api = -api;
2280 }
2281
2282 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2283}
2284
Neil Booth03f58ab2007-10-07 12:15:41 +00002285/* Convert a two's complement integer SRC to a floating point number,
2286 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2287 integer is signed, in which case it must be sign-extended. */
2288APFloat::opStatus
2289APFloat::convertFromSignExtendedInteger(const integerPart *src,
2290 unsigned int srcCount,
2291 bool isSigned,
2292 roundingMode rounding_mode)
2293{
2294 opStatus status;
2295
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002296 if (isSigned &&
2297 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002298 integerPart *copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002299
2300 /* If we're signed and negative negate a copy. */
2301 sign = true;
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002302 copy = new integerPart[srcCount];
Neil Booth03f58ab2007-10-07 12:15:41 +00002303 APInt::tcAssign(copy, src, srcCount);
2304 APInt::tcNegate(copy, srcCount);
2305 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002306 delete [] copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002307 } else {
2308 sign = false;
2309 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2310 }
2311
2312 return status;
2313}
2314
Neil Booth5f009732007-10-07 11:45:55 +00002315/* FIXME: should this just take a const APInt reference? */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002316APFloat::opStatus
Neil Booth5f009732007-10-07 11:45:55 +00002317APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2318 unsigned int width, bool isSigned,
2319 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002320{
Dale Johannesen42305122007-09-21 22:09:37 +00002321 unsigned int partCount = partCountForBits(width);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002322 APInt api = APInt(width, makeArrayRef(parts, partCount));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002323
2324 sign = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002325 if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
Dale Johannesen28a2c4a2007-09-30 18:17:01 +00002326 sign = true;
2327 api = -api;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002328 }
2329
Neil Boothba205222007-10-07 12:10:57 +00002330 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002331}
2332
2333APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002334APFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002335{
Erick Tryzelaara9680df2009-08-18 18:20:37 +00002336 lostFraction lost_fraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002337
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002338 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002339 zeroSignificand();
2340 exponent = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002341
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002342 integerPart *significand = significandParts();
2343 unsigned partsCount = partCount();
2344 unsigned bitPos = partsCount * integerPartWidth;
2345 bool computedTrailingFraction = false;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002346
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002347 // Skip leading zeroes and any (hexa)decimal point.
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002348 StringRef::iterator begin = s.begin();
2349 StringRef::iterator end = s.end();
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002350 StringRef::iterator dot;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002351 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002352 StringRef::iterator firstSignificantDigit = p;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002353
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002354 while (p != end) {
Dale Johannesenfa483722008-05-14 22:53:25 +00002355 integerPart hex_value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002356
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002357 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002358 assert(dot == end && "String contains multiple dots");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002359 dot = p++;
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002360 continue;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002361 }
2362
2363 hex_value = hexDigitValue(*p);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002364 if (hex_value == -1U)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002365 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002366
2367 p++;
2368
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002369 // Store the number while we have space.
2370 if (bitPos) {
2371 bitPos -= 4;
2372 hex_value <<= bitPos % integerPartWidth;
2373 significand[bitPos / integerPartWidth] |= hex_value;
2374 } else if (!computedTrailingFraction) {
2375 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2376 computedTrailingFraction = true;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002377 }
2378 }
2379
2380 /* Hex floats require an exponent but not a hexadecimal point. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002381 assert(p != end && "Hex strings require an exponent");
2382 assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2383 assert(p != begin && "Significand has no digits");
2384 assert((dot == end || p - begin != 1) && "Significand has no digits");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002385
2386 /* Ignore the exponent if we are zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002387 if (p != firstSignificantDigit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002388 int expAdjustment;
2389
2390 /* Implicit hexadecimal point? */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002391 if (dot == end)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002392 dot = p;
2393
2394 /* Calculate the exponent adjustment implicit in the number of
2395 significant digits. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002396 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002397 if (expAdjustment < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002398 expAdjustment++;
2399 expAdjustment = expAdjustment * 4 - 1;
2400
2401 /* Adjust for writing the significand starting at the most
2402 significant nibble. */
2403 expAdjustment += semantics->precision;
2404 expAdjustment -= partsCount * integerPartWidth;
2405
2406 /* Adjust for the given exponent. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002407 exponent = totalExponent(p + 1, end, expAdjustment);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002408 }
2409
2410 return normalize(rounding_mode, lost_fraction);
2411}
2412
2413APFloat::opStatus
Neil Boothb93d90e2007-10-12 16:02:31 +00002414APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2415 unsigned sigPartCount, int exp,
2416 roundingMode rounding_mode)
2417{
2418 unsigned int parts, pow5PartCount;
Ulrich Weigand908c9362012-10-29 18:18:44 +00002419 fltSemantics calcSemantics = { 32767, -32767, 0 };
Neil Boothb93d90e2007-10-12 16:02:31 +00002420 integerPart pow5Parts[maxPowerOfFiveParts];
2421 bool isNearest;
2422
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002423 isNearest = (rounding_mode == rmNearestTiesToEven ||
2424 rounding_mode == rmNearestTiesToAway);
Neil Boothb93d90e2007-10-12 16:02:31 +00002425
2426 parts = partCountForBits(semantics->precision + 11);
2427
2428 /* Calculate pow(5, abs(exp)). */
2429 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2430
2431 for (;; parts *= 2) {
2432 opStatus sigStatus, powStatus;
2433 unsigned int excessPrecision, truncatedBits;
2434
2435 calcSemantics.precision = parts * integerPartWidth - 1;
2436 excessPrecision = calcSemantics.precision - semantics->precision;
2437 truncatedBits = excessPrecision;
2438
Michael Gottesman79b09672013-06-27 21:58:19 +00002439 APFloat decSig = APFloat::getZero(calcSemantics, sign);
2440 APFloat pow5(calcSemantics);
Neil Boothb93d90e2007-10-12 16:02:31 +00002441
2442 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2443 rmNearestTiesToEven);
2444 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2445 rmNearestTiesToEven);
2446 /* Add exp, as 10^n = 5^n * 2^n. */
2447 decSig.exponent += exp;
2448
2449 lostFraction calcLostFraction;
Evan Cheng82b9e962008-05-02 21:15:08 +00002450 integerPart HUerr, HUdistance;
2451 unsigned int powHUerr;
Neil Boothb93d90e2007-10-12 16:02:31 +00002452
2453 if (exp >= 0) {
2454 /* multiplySignificand leaves the precision-th bit set to 1. */
Craig Topperc10719f2014-04-07 04:17:22 +00002455 calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
Neil Boothb93d90e2007-10-12 16:02:31 +00002456 powHUerr = powStatus != opOK;
2457 } else {
2458 calcLostFraction = decSig.divideSignificand(pow5);
2459 /* Denormal numbers have less precision. */
2460 if (decSig.exponent < semantics->minExponent) {
2461 excessPrecision += (semantics->minExponent - decSig.exponent);
2462 truncatedBits = excessPrecision;
2463 if (excessPrecision > calcSemantics.precision)
2464 excessPrecision = calcSemantics.precision;
2465 }
2466 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002467 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Boothb93d90e2007-10-12 16:02:31 +00002468 }
2469
2470 /* Both multiplySignificand and divideSignificand return the
2471 result with the integer bit set. */
Evan Cheng67c90212009-10-27 21:35:42 +00002472 assert(APInt::tcExtractBit
2473 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
Neil Boothb93d90e2007-10-12 16:02:31 +00002474
2475 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2476 powHUerr);
2477 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2478 excessPrecision, isNearest);
2479
2480 /* Are we guaranteed to round correctly if we truncate? */
2481 if (HUdistance >= HUerr) {
2482 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2483 calcSemantics.precision - excessPrecision,
2484 excessPrecision);
2485 /* Take the exponent of decSig. If we tcExtract-ed less bits
2486 above we must adjust our exponent to compensate for the
2487 implicit right shift. */
2488 exponent = (decSig.exponent + semantics->precision
2489 - (calcSemantics.precision - excessPrecision));
2490 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2491 decSig.partCount(),
2492 truncatedBits);
2493 return normalize(rounding_mode, calcLostFraction);
2494 }
2495 }
2496}
2497
2498APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002499APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
Neil Boothb93d90e2007-10-12 16:02:31 +00002500{
Neil Booth4ed401b2007-10-14 10:16:12 +00002501 decimalInfo D;
Neil Boothb93d90e2007-10-12 16:02:31 +00002502 opStatus fs;
2503
Neil Booth4ed401b2007-10-14 10:16:12 +00002504 /* Scan the text. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002505 StringRef::iterator p = str.begin();
2506 interpretDecimal(p, str.end(), &D);
Neil Boothb93d90e2007-10-12 16:02:31 +00002507
Neil Booth91305512007-10-15 15:00:55 +00002508 /* Handle the quick cases. First the case of no significant digits,
2509 i.e. zero, and then exponents that are obviously too large or too
2510 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2511 definitely overflows if
2512
2513 (exp - 1) * L >= maxExponent
2514
2515 and definitely underflows to zero where
2516
2517 (exp + 1) * L <= minExponent - precision
2518
2519 With integer arithmetic the tightest bounds for L are
2520
2521 93/28 < L < 196/59 [ numerator <= 256 ]
2522 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2523 */
2524
Michael Gottesman228156c2013-07-01 23:54:08 +00002525 // Test if we have a zero number allowing for strings with no null terminators
2526 // and zero decimals with non-zero exponents.
2527 //
2528 // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2529 // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2530 // be at most one dot. On the other hand, if we have a zero with a non-zero
2531 // exponent, then we know that D.firstSigDigit will be non-numeric.
Michael Gottesman94d61952013-07-02 15:50:05 +00002532 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Boothb93d90e2007-10-12 16:02:31 +00002533 category = fcZero;
2534 fs = opOK;
John McCallb42cc682010-02-26 22:20:41 +00002535
2536 /* Check whether the normalized exponent is high enough to overflow
2537 max during the log-rebasing in the max-exponent check below. */
2538 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
2539 fs = handleOverflow(rounding_mode);
2540
2541 /* If it wasn't, then it also wasn't high enough to overflow max
2542 during the log-rebasing in the min-exponent check. Check that it
2543 won't overflow min in either check, then perform the min-exponent
2544 check. */
2545 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
2546 (D.normalizedExponent + 1) * 28738 <=
2547 8651 * (semantics->minExponent - (int) semantics->precision)) {
Neil Booth91305512007-10-15 15:00:55 +00002548 /* Underflow to zero and round. */
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002549 category = fcNormal;
Neil Booth91305512007-10-15 15:00:55 +00002550 zeroSignificand();
2551 fs = normalize(rounding_mode, lfLessThanHalf);
John McCallb42cc682010-02-26 22:20:41 +00002552
2553 /* We can finally safely perform the max-exponent check. */
Neil Booth91305512007-10-15 15:00:55 +00002554 } else if ((D.normalizedExponent - 1) * 42039
2555 >= 12655 * semantics->maxExponent) {
2556 /* Overflow and round. */
2557 fs = handleOverflow(rounding_mode);
Neil Boothb93d90e2007-10-12 16:02:31 +00002558 } else {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002559 integerPart *decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002560 unsigned int partCount;
Neil Boothb93d90e2007-10-12 16:02:31 +00002561
Neil Booth4ed401b2007-10-14 10:16:12 +00002562 /* A tight upper bound on number of bits required to hold an
Neil Booth91305512007-10-15 15:00:55 +00002563 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth4ed401b2007-10-14 10:16:12 +00002564 to hold the full significand, and an extra part required by
2565 tcMultiplyPart. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002566 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth91305512007-10-15 15:00:55 +00002567 partCount = partCountForBits(1 + 196 * partCount / 59);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002568 decSignificand = new integerPart[partCount + 1];
Neil Booth4ed401b2007-10-14 10:16:12 +00002569 partCount = 0;
Neil Boothb93d90e2007-10-12 16:02:31 +00002570
Neil Booth4ed401b2007-10-14 10:16:12 +00002571 /* Convert to binary efficiently - we do almost all multiplication
2572 in an integerPart. When this would overflow do we do a single
2573 bignum multiplication, and then revert again to multiplication
2574 in an integerPart. */
2575 do {
2576 integerPart decValue, val, multiplier;
2577
2578 val = 0;
2579 multiplier = 1;
2580
2581 do {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002582 if (*p == '.') {
Neil Booth4ed401b2007-10-14 10:16:12 +00002583 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002584 if (p == str.end()) {
2585 break;
2586 }
2587 }
Neil Booth4ed401b2007-10-14 10:16:12 +00002588 decValue = decDigitValue(*p++);
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002589 assert(decValue < 10U && "Invalid character in significand");
Neil Booth4ed401b2007-10-14 10:16:12 +00002590 multiplier *= 10;
2591 val = val * 10 + decValue;
2592 /* The maximum number that can be multiplied by ten with any
2593 digit added without overflowing an integerPart. */
2594 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2595
2596 /* Multiply out the current part. */
2597 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2598 partCount, partCount + 1, false);
2599
2600 /* If we used another part (likely but not guaranteed), increase
2601 the count. */
2602 if (decSignificand[partCount])
2603 partCount++;
2604 } while (p <= D.lastSigDigit);
Neil Boothb93d90e2007-10-12 16:02:31 +00002605
Neil Boothae077d22007-11-01 22:51:07 +00002606 category = fcNormal;
Neil Boothb93d90e2007-10-12 16:02:31 +00002607 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth4ed401b2007-10-14 10:16:12 +00002608 D.exponent, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002609
2610 delete [] decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002611 }
Neil Boothb93d90e2007-10-12 16:02:31 +00002612
2613 return fs;
2614}
2615
Michael Gottesman40e8a182013-06-24 09:58:05 +00002616bool
2617APFloat::convertFromStringSpecials(StringRef str) {
2618 if (str.equals("inf") || str.equals("INFINITY")) {
2619 makeInf(false);
2620 return true;
2621 }
2622
2623 if (str.equals("-inf") || str.equals("-INFINITY")) {
2624 makeInf(true);
2625 return true;
2626 }
2627
2628 if (str.equals("nan") || str.equals("NaN")) {
2629 makeNaN(false, false);
2630 return true;
2631 }
2632
2633 if (str.equals("-nan") || str.equals("-NaN")) {
2634 makeNaN(false, true);
2635 return true;
2636 }
2637
2638 return false;
2639}
2640
Neil Boothb93d90e2007-10-12 16:02:31 +00002641APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002642APFloat::convertFromString(StringRef str, roundingMode rounding_mode)
Neil Booth9acbf5a2007-09-26 21:33:42 +00002643{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002644 assert(!str.empty() && "Invalid string length");
Neil Booth06077e72007-10-14 10:29:28 +00002645
Michael Gottesman40e8a182013-06-24 09:58:05 +00002646 // Handle special cases.
2647 if (convertFromStringSpecials(str))
2648 return opOK;
2649
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002650 /* Handle a leading minus sign. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002651 StringRef::iterator p = str.begin();
2652 size_t slen = str.size();
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002653 sign = *p == '-' ? 1 : 0;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002654 if (*p == '-' || *p == '+') {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002655 p++;
2656 slen--;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002657 assert(slen && "String has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002658 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002659
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002660 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002661 assert(slen - 2 && "Invalid string");
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002662 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002663 rounding_mode);
2664 }
Bill Wendlingc6075402008-11-27 08:00:12 +00002665
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002666 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002667}
Dale Johannesena719a602007-08-24 00:56:33 +00002668
Neil Booth8f1946f2007-10-03 22:26:02 +00002669/* Write out a hexadecimal representation of the floating point value
2670 to DST, which must be of sufficient size, in the C99 form
2671 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2672 excluding the terminating NUL.
2673
2674 If UPPERCASE, the output is in upper case, otherwise in lower case.
2675
2676 HEXDIGITS digits appear altogether, rounding the value if
2677 necessary. If HEXDIGITS is 0, the minimal precision to display the
2678 number precisely is used instead. If nothing would appear after
2679 the decimal point it is suppressed.
2680
2681 The decimal exponent is always printed and has at least one digit.
2682 Zero values display an exponent of zero. Infinities and NaNs
2683 appear as "infinity" or "nan" respectively.
2684
2685 The above rules are as specified by C99. There is ambiguity about
2686 what the leading hexadecimal digit should be. This implementation
2687 uses whatever is necessary so that the exponent is displayed as
2688 stored. This implies the exponent will fall within the IEEE format
2689 range, and the leading hexadecimal digit will be 0 (for denormals),
2690 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2691 any other digits zero).
2692*/
2693unsigned int
2694APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2695 bool upperCase, roundingMode rounding_mode) const
2696{
2697 char *p;
2698
2699 p = dst;
2700 if (sign)
2701 *dst++ = '-';
2702
2703 switch (category) {
2704 case fcInfinity:
2705 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2706 dst += sizeof infinityL - 1;
2707 break;
2708
2709 case fcNaN:
2710 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2711 dst += sizeof NaNU - 1;
2712 break;
2713
2714 case fcZero:
2715 *dst++ = '0';
2716 *dst++ = upperCase ? 'X': 'x';
2717 *dst++ = '0';
2718 if (hexDigits > 1) {
2719 *dst++ = '.';
2720 memset (dst, '0', hexDigits - 1);
2721 dst += hexDigits - 1;
2722 }
2723 *dst++ = upperCase ? 'P': 'p';
2724 *dst++ = '0';
2725 break;
2726
2727 case fcNormal:
2728 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2729 break;
2730 }
2731
2732 *dst = 0;
2733
Evan Cheng82b9e962008-05-02 21:15:08 +00002734 return static_cast<unsigned int>(dst - p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002735}
2736
2737/* Does the hard work of outputting the correctly rounded hexadecimal
2738 form of a normal floating point number with the specified number of
2739 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2740 digits necessary to print the value precisely is output. */
2741char *
2742APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2743 bool upperCase,
2744 roundingMode rounding_mode) const
2745{
2746 unsigned int count, valueBits, shift, partsCount, outputDigits;
2747 const char *hexDigitChars;
2748 const integerPart *significand;
2749 char *p;
2750 bool roundUp;
2751
2752 *dst++ = '0';
2753 *dst++ = upperCase ? 'X': 'x';
2754
2755 roundUp = false;
2756 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2757
2758 significand = significandParts();
2759 partsCount = partCount();
2760
2761 /* +3 because the first digit only uses the single integer bit, so
2762 we have 3 virtual zero most-significant-bits. */
2763 valueBits = semantics->precision + 3;
2764 shift = integerPartWidth - valueBits % integerPartWidth;
2765
2766 /* The natural number of digits required ignoring trailing
2767 insignificant zeroes. */
2768 outputDigits = (valueBits - significandLSB () + 3) / 4;
2769
2770 /* hexDigits of zero means use the required number for the
2771 precision. Otherwise, see if we are truncating. If we are,
Neil Booth0ea72a92007-10-06 00:24:48 +00002772 find out if we need to round away from zero. */
Neil Booth8f1946f2007-10-03 22:26:02 +00002773 if (hexDigits) {
2774 if (hexDigits < outputDigits) {
2775 /* We are dropping non-zero bits, so need to check how to round.
2776 "bits" is the number of dropped bits. */
2777 unsigned int bits;
2778 lostFraction fraction;
2779
2780 bits = valueBits - hexDigits * 4;
2781 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2782 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2783 }
2784 outputDigits = hexDigits;
2785 }
2786
2787 /* Write the digits consecutively, and start writing in the location
2788 of the hexadecimal point. We move the most significant digit
2789 left and add the hexadecimal point later. */
2790 p = ++dst;
2791
2792 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2793
2794 while (outputDigits && count) {
2795 integerPart part;
2796
2797 /* Put the most significant integerPartWidth bits in "part". */
2798 if (--count == partsCount)
2799 part = 0; /* An imaginary higher zero part. */
2800 else
2801 part = significand[count] << shift;
2802
2803 if (count && shift)
2804 part |= significand[count - 1] >> (integerPartWidth - shift);
2805
2806 /* Convert as much of "part" to hexdigits as we can. */
2807 unsigned int curDigits = integerPartWidth / 4;
2808
2809 if (curDigits > outputDigits)
2810 curDigits = outputDigits;
2811 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2812 outputDigits -= curDigits;
2813 }
2814
2815 if (roundUp) {
2816 char *q = dst;
2817
2818 /* Note that hexDigitChars has a trailing '0'. */
2819 do {
2820 q--;
2821 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth0ea72a92007-10-06 00:24:48 +00002822 } while (*q == '0');
Evan Cheng67c90212009-10-27 21:35:42 +00002823 assert(q >= p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002824 } else {
2825 /* Add trailing zeroes. */
2826 memset (dst, '0', outputDigits);
2827 dst += outputDigits;
2828 }
2829
2830 /* Move the most significant digit to before the point, and if there
2831 is something after the decimal point add it. This must come
2832 after rounding above. */
2833 p[-1] = p[0];
2834 if (dst -1 == p)
2835 dst--;
2836 else
2837 p[0] = '.';
2838
2839 /* Finally output the exponent. */
2840 *dst++ = upperCase ? 'P': 'p';
2841
Neil Booth32897f52007-10-06 07:29:25 +00002842 return writeSignedDecimal (dst, exponent);
Neil Booth8f1946f2007-10-03 22:26:02 +00002843}
2844
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002845hash_code llvm::hash_value(const APFloat &Arg) {
Michael Gottesman8136c382013-06-26 23:17:28 +00002846 if (!Arg.isFiniteNonZero())
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002847 return hash_combine((uint8_t)Arg.category,
2848 // NaN has no sign, fix it at zero.
2849 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2850 Arg.semantics->precision);
2851
2852 // Normal floats need their exponent and significand hashed.
2853 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2854 Arg.semantics->precision, Arg.exponent,
2855 hash_combine_range(
2856 Arg.significandParts(),
2857 Arg.significandParts() + Arg.partCount()));
Dale Johannesena719a602007-08-24 00:56:33 +00002858}
2859
2860// Conversion from APFloat to/from host float/double. It may eventually be
2861// possible to eliminate these and have everybody deal with APFloats, but that
2862// will take a while. This approach will not easily extend to long double.
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002863// Current implementation requires integerPartWidth==64, which is correct at
2864// the moment but could be made more general.
Dale Johannesena719a602007-08-24 00:56:33 +00002865
Dale Johannesen728687c2007-09-05 20:39:49 +00002866// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002867// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen728687c2007-09-05 20:39:49 +00002868
Dale Johannesen245dceb2007-09-11 18:32:33 +00002869APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00002870APFloat::convertF80LongDoubleAPFloatToAPInt() const
2871{
Dan Gohmanb456a152008-01-29 12:08:20 +00002872 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
Evan Cheng67c90212009-10-27 21:35:42 +00002873 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002874
2875 uint64_t myexponent, mysignificand;
2876
Michael Gottesman8136c382013-06-26 23:17:28 +00002877 if (isFiniteNonZero()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00002878 myexponent = exponent+16383; //bias
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002879 mysignificand = significandParts()[0];
Dale Johannesen245dceb2007-09-11 18:32:33 +00002880 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2881 myexponent = 0; // denormal
2882 } else if (category==fcZero) {
2883 myexponent = 0;
2884 mysignificand = 0;
2885 } else if (category==fcInfinity) {
2886 myexponent = 0x7fff;
2887 mysignificand = 0x8000000000000000ULL;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002888 } else {
2889 assert(category == fcNaN && "Unknown category");
Dale Johannesen245dceb2007-09-11 18:32:33 +00002890 myexponent = 0x7fff;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002891 mysignificand = significandParts()[0];
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002892 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00002893
2894 uint64_t words[2];
Dale Johannesen93eefa02009-03-23 21:16:53 +00002895 words[0] = mysignificand;
2896 words[1] = ((uint64_t)(sign & 1) << 15) |
2897 (myexponent & 0x7fffLL);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002898 return APInt(80, words);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002899}
2900
2901APInt
Dale Johannesen007aa372007-10-11 18:07:22 +00002902APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2903{
Dan Gohmanb456a152008-01-29 12:08:20 +00002904 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
Evan Cheng67c90212009-10-27 21:35:42 +00002905 assert(partCount()==2);
Dale Johannesen007aa372007-10-11 18:07:22 +00002906
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002907 uint64_t words[2];
2908 opStatus fs;
2909 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00002910
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002911 // Convert number to double. To avoid spurious underflows, we re-
2912 // normalize against the "double" minExponent first, and only *then*
2913 // truncate the mantissa. The result of that second conversion
2914 // may be inexact, but should never underflow.
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002915 // Declare fltSemantics before APFloat that uses it (and
2916 // saves pointer to it) to ensure correct destruction order.
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002917 fltSemantics extendedSemantics = *semantics;
2918 extendedSemantics.minExponent = IEEEdouble.minExponent;
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002919 APFloat extended(*this);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002920 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2921 assert(fs == opOK && !losesInfo);
2922 (void)fs;
2923
2924 APFloat u(extended);
2925 fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2926 assert(fs == opOK || fs == opInexact);
2927 (void)fs;
2928 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2929
2930 // If conversion was exact or resulted in a special case, we're done;
2931 // just set the second double to zero. Otherwise, re-convert back to
2932 // the extended format and compute the difference. This now should
2933 // convert exactly to double.
Michael Gottesman8136c382013-06-26 23:17:28 +00002934 if (u.isFiniteNonZero() && losesInfo) {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002935 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2936 assert(fs == opOK && !losesInfo);
2937 (void)fs;
2938
2939 APFloat v(extended);
2940 v.subtract(u, rmNearestTiesToEven);
2941 fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2942 assert(fs == opOK && !losesInfo);
2943 (void)fs;
2944 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
Dale Johannesen007aa372007-10-11 18:07:22 +00002945 } else {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002946 words[1] = 0;
Dale Johannesen007aa372007-10-11 18:07:22 +00002947 }
2948
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002949 return APInt(128, words);
Dale Johannesen007aa372007-10-11 18:07:22 +00002950}
2951
2952APInt
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002953APFloat::convertQuadrupleAPFloatToAPInt() const
2954{
2955 assert(semantics == (const llvm::fltSemantics*)&IEEEquad);
Evan Cheng67c90212009-10-27 21:35:42 +00002956 assert(partCount()==2);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002957
2958 uint64_t myexponent, mysignificand, mysignificand2;
2959
Michael Gottesman8136c382013-06-26 23:17:28 +00002960 if (isFiniteNonZero()) {
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002961 myexponent = exponent+16383; //bias
2962 mysignificand = significandParts()[0];
2963 mysignificand2 = significandParts()[1];
2964 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2965 myexponent = 0; // denormal
2966 } else if (category==fcZero) {
2967 myexponent = 0;
2968 mysignificand = mysignificand2 = 0;
2969 } else if (category==fcInfinity) {
2970 myexponent = 0x7fff;
2971 mysignificand = mysignificand2 = 0;
2972 } else {
2973 assert(category == fcNaN && "Unknown category!");
2974 myexponent = 0x7fff;
2975 mysignificand = significandParts()[0];
2976 mysignificand2 = significandParts()[1];
2977 }
2978
2979 uint64_t words[2];
2980 words[0] = mysignificand;
2981 words[1] = ((uint64_t)(sign & 1) << 63) |
2982 ((myexponent & 0x7fff) << 48) |
Anton Korobeynikov876955c2009-08-21 23:09:47 +00002983 (mysignificand2 & 0xffffffffffffLL);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002984
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002985 return APInt(128, words);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002986}
2987
2988APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00002989APFloat::convertDoubleAPFloatToAPInt() const
2990{
Dan Gohman58c468f2007-09-14 20:08:19 +00002991 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Evan Cheng67c90212009-10-27 21:35:42 +00002992 assert(partCount()==1);
Dale Johannesena719a602007-08-24 00:56:33 +00002993
Dale Johannesen3cf889f2007-08-31 04:03:46 +00002994 uint64_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00002995
Michael Gottesman8136c382013-06-26 23:17:28 +00002996 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00002997 myexponent = exponent+1023; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00002998 mysignificand = *significandParts();
2999 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
3000 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00003001 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00003002 myexponent = 0;
3003 mysignificand = 0;
3004 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00003005 myexponent = 0x7ff;
3006 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003007 } else {
3008 assert(category == fcNaN && "Unknown category!");
Dale Johannesena719a602007-08-24 00:56:33 +00003009 myexponent = 0x7ff;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003010 mysignificand = *significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003011 }
Dale Johannesena719a602007-08-24 00:56:33 +00003012
Evan Cheng82b9e962008-05-02 21:15:08 +00003013 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003014 ((myexponent & 0x7ff) << 52) |
3015 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesena719a602007-08-24 00:56:33 +00003016}
3017
Dale Johannesen245dceb2007-09-11 18:32:33 +00003018APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00003019APFloat::convertFloatAPFloatToAPInt() const
3020{
Dan Gohman58c468f2007-09-14 20:08:19 +00003021 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Evan Cheng67c90212009-10-27 21:35:42 +00003022 assert(partCount()==1);
Neil Booth9acbf5a2007-09-26 21:33:42 +00003023
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003024 uint32_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003025
Michael Gottesman8136c382013-06-26 23:17:28 +00003026 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00003027 myexponent = exponent+127; //bias
Evan Cheng82b9e962008-05-02 21:15:08 +00003028 mysignificand = (uint32_t)*significandParts();
Dale Johannesen06a10df2007-11-17 01:02:27 +00003029 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen728687c2007-09-05 20:39:49 +00003030 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00003031 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00003032 myexponent = 0;
3033 mysignificand = 0;
3034 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00003035 myexponent = 0xff;
3036 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003037 } else {
3038 assert(category == fcNaN && "Unknown category!");
Dale Johannesen728687c2007-09-05 20:39:49 +00003039 myexponent = 0xff;
Evan Cheng82b9e962008-05-02 21:15:08 +00003040 mysignificand = (uint32_t)*significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003041 }
Dale Johannesena719a602007-08-24 00:56:33 +00003042
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003043 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3044 (mysignificand & 0x7fffff)));
Dale Johannesena719a602007-08-24 00:56:33 +00003045}
3046
Chris Lattner4794b2b2009-10-16 02:13:51 +00003047APInt
3048APFloat::convertHalfAPFloatToAPInt() const
3049{
3050 assert(semantics == (const llvm::fltSemantics*)&IEEEhalf);
Evan Cheng67c90212009-10-27 21:35:42 +00003051 assert(partCount()==1);
Chris Lattner4794b2b2009-10-16 02:13:51 +00003052
3053 uint32_t myexponent, mysignificand;
3054
Michael Gottesman8136c382013-06-26 23:17:28 +00003055 if (isFiniteNonZero()) {
Chris Lattner4794b2b2009-10-16 02:13:51 +00003056 myexponent = exponent+15; //bias
3057 mysignificand = (uint32_t)*significandParts();
3058 if (myexponent == 1 && !(mysignificand & 0x400))
3059 myexponent = 0; // denormal
3060 } else if (category==fcZero) {
3061 myexponent = 0;
3062 mysignificand = 0;
3063 } else if (category==fcInfinity) {
Dale Johannesen0d670b52009-10-23 04:02:51 +00003064 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003065 mysignificand = 0;
3066 } else {
3067 assert(category == fcNaN && "Unknown category!");
Dale Johannesen0d670b52009-10-23 04:02:51 +00003068 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003069 mysignificand = (uint32_t)*significandParts();
3070 }
3071
3072 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3073 (mysignificand & 0x3ff)));
3074}
3075
Dale Johannesen007aa372007-10-11 18:07:22 +00003076// This function creates an APInt that is just a bit map of the floating
3077// point constant as it would appear in memory. It is not a conversion,
3078// and treating the result as a normal integer is unlikely to be useful.
3079
Dale Johannesen245dceb2007-09-11 18:32:33 +00003080APInt
Dale Johannesen54306fe2008-10-09 18:53:47 +00003081APFloat::bitcastToAPInt() const
Neil Booth9acbf5a2007-09-26 21:33:42 +00003082{
Chris Lattner4794b2b2009-10-16 02:13:51 +00003083 if (semantics == (const llvm::fltSemantics*)&IEEEhalf)
3084 return convertHalfAPFloatToAPInt();
3085
Dan Gohmanb456a152008-01-29 12:08:20 +00003086 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003087 return convertFloatAPFloatToAPInt();
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003088
Dan Gohmanb456a152008-01-29 12:08:20 +00003089 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003090 return convertDoubleAPFloatToAPInt();
Neil Booth9acbf5a2007-09-26 21:33:42 +00003091
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003092 if (semantics == (const llvm::fltSemantics*)&IEEEquad)
3093 return convertQuadrupleAPFloatToAPInt();
3094
Dan Gohmanb456a152008-01-29 12:08:20 +00003095 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
Dale Johannesen007aa372007-10-11 18:07:22 +00003096 return convertPPCDoubleDoubleAPFloatToAPInt();
3097
Dan Gohmanb456a152008-01-29 12:08:20 +00003098 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003099 "unknown format!");
3100 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003101}
3102
Neil Booth9acbf5a2007-09-26 21:33:42 +00003103float
3104APFloat::convertToFloat() const
3105{
Chris Lattner688f9912009-09-24 21:44:20 +00003106 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle &&
3107 "Float semantics are not IEEEsingle");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003108 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003109 return api.bitsToFloat();
3110}
3111
Neil Booth9acbf5a2007-09-26 21:33:42 +00003112double
3113APFloat::convertToDouble() const
3114{
Chris Lattner688f9912009-09-24 21:44:20 +00003115 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble &&
3116 "Float semantics are not IEEEdouble");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003117 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003118 return api.bitsToDouble();
3119}
3120
Dale Johannesenfff29952008-10-06 18:22:29 +00003121/// Integer bit is explicit in this format. Intel hardware (387 and later)
3122/// does not support these bit patterns:
3123/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3124/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3125/// exponent = 0, integer bit 1 ("pseudodenormal")
3126/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3127/// At the moment, the first two are treated as NaNs, the second two as Normal.
Dale Johannesen245dceb2007-09-11 18:32:33 +00003128void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003129APFloat::initFromF80LongDoubleAPInt(const APInt &api)
3130{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003131 assert(api.getBitWidth()==80);
3132 uint64_t i1 = api.getRawData()[0];
3133 uint64_t i2 = api.getRawData()[1];
Dale Johannesen93eefa02009-03-23 21:16:53 +00003134 uint64_t myexponent = (i2 & 0x7fff);
3135 uint64_t mysignificand = i1;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003136
3137 initialize(&APFloat::x87DoubleExtended);
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003138 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003139
Dale Johannesen93eefa02009-03-23 21:16:53 +00003140 sign = static_cast<unsigned int>(i2>>15);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003141 if (myexponent==0 && mysignificand==0) {
3142 // exponent, significand meaningless
3143 category = fcZero;
3144 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3145 // exponent, significand meaningless
3146 category = fcInfinity;
3147 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3148 // exponent meaningless
3149 category = fcNaN;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003150 significandParts()[0] = mysignificand;
3151 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003152 } else {
3153 category = fcNormal;
3154 exponent = myexponent - 16383;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003155 significandParts()[0] = mysignificand;
3156 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003157 if (myexponent==0) // denormal
3158 exponent = -16382;
Neil Booth9acbf5a2007-09-26 21:33:42 +00003159 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00003160}
3161
3162void
Dale Johannesen007aa372007-10-11 18:07:22 +00003163APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
3164{
3165 assert(api.getBitWidth()==128);
3166 uint64_t i1 = api.getRawData()[0];
3167 uint64_t i2 = api.getRawData()[1];
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003168 opStatus fs;
3169 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00003170
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003171 // Get the first double and convert to our format.
3172 initFromDoubleAPInt(APInt(64, i1));
3173 fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3174 assert(fs == opOK && !losesInfo);
3175 (void)fs;
Dale Johannesen007aa372007-10-11 18:07:22 +00003176
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003177 // Unless we have a special case, add in second double.
Michael Gottesman8136c382013-06-26 23:17:28 +00003178 if (isFiniteNonZero()) {
Tim Northover29178a32013-01-22 09:46:31 +00003179 APFloat v(IEEEdouble, APInt(64, i2));
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003180 fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3181 assert(fs == opOK && !losesInfo);
3182 (void)fs;
3183
3184 add(v, rmNearestTiesToEven);
Dale Johannesen007aa372007-10-11 18:07:22 +00003185 }
3186}
3187
3188void
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003189APFloat::initFromQuadrupleAPInt(const APInt &api)
3190{
3191 assert(api.getBitWidth()==128);
3192 uint64_t i1 = api.getRawData()[0];
3193 uint64_t i2 = api.getRawData()[1];
3194 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3195 uint64_t mysignificand = i1;
3196 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3197
3198 initialize(&APFloat::IEEEquad);
3199 assert(partCount()==2);
3200
3201 sign = static_cast<unsigned int>(i2>>63);
3202 if (myexponent==0 &&
3203 (mysignificand==0 && mysignificand2==0)) {
3204 // exponent, significand meaningless
3205 category = fcZero;
3206 } else if (myexponent==0x7fff &&
3207 (mysignificand==0 && mysignificand2==0)) {
3208 // exponent, significand meaningless
3209 category = fcInfinity;
3210 } else if (myexponent==0x7fff &&
3211 (mysignificand!=0 || mysignificand2 !=0)) {
3212 // exponent meaningless
3213 category = fcNaN;
3214 significandParts()[0] = mysignificand;
3215 significandParts()[1] = mysignificand2;
3216 } else {
3217 category = fcNormal;
3218 exponent = myexponent - 16383;
3219 significandParts()[0] = mysignificand;
3220 significandParts()[1] = mysignificand2;
3221 if (myexponent==0) // denormal
3222 exponent = -16382;
3223 else
3224 significandParts()[1] |= 0x1000000000000LL; // integer bit
3225 }
3226}
3227
3228void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003229APFloat::initFromDoubleAPInt(const APInt &api)
3230{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003231 assert(api.getBitWidth()==64);
3232 uint64_t i = *api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003233 uint64_t myexponent = (i >> 52) & 0x7ff;
3234 uint64_t mysignificand = i & 0xfffffffffffffLL;
3235
Dale Johannesena719a602007-08-24 00:56:33 +00003236 initialize(&APFloat::IEEEdouble);
Dale Johannesena719a602007-08-24 00:56:33 +00003237 assert(partCount()==1);
3238
Evan Cheng82b9e962008-05-02 21:15:08 +00003239 sign = static_cast<unsigned int>(i>>63);
Dale Johannesena719a602007-08-24 00:56:33 +00003240 if (myexponent==0 && mysignificand==0) {
3241 // exponent, significand meaningless
3242 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003243 } else if (myexponent==0x7ff && mysignificand==0) {
3244 // exponent, significand meaningless
3245 category = fcInfinity;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003246 } else if (myexponent==0x7ff && mysignificand!=0) {
3247 // exponent meaningless
3248 category = fcNaN;
3249 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003250 } else {
Dale Johannesena719a602007-08-24 00:56:33 +00003251 category = fcNormal;
3252 exponent = myexponent - 1023;
Dale Johannesen728687c2007-09-05 20:39:49 +00003253 *significandParts() = mysignificand;
3254 if (myexponent==0) // denormal
3255 exponent = -1022;
3256 else
3257 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth9acbf5a2007-09-26 21:33:42 +00003258 }
Dale Johannesena719a602007-08-24 00:56:33 +00003259}
3260
Dale Johannesen245dceb2007-09-11 18:32:33 +00003261void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003262APFloat::initFromFloatAPInt(const APInt & api)
3263{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003264 assert(api.getBitWidth()==32);
3265 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003266 uint32_t myexponent = (i >> 23) & 0xff;
3267 uint32_t mysignificand = i & 0x7fffff;
3268
Dale Johannesena719a602007-08-24 00:56:33 +00003269 initialize(&APFloat::IEEEsingle);
Dale Johannesena719a602007-08-24 00:56:33 +00003270 assert(partCount()==1);
3271
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003272 sign = i >> 31;
Dale Johannesena719a602007-08-24 00:56:33 +00003273 if (myexponent==0 && mysignificand==0) {
3274 // exponent, significand meaningless
3275 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003276 } else if (myexponent==0xff && mysignificand==0) {
3277 // exponent, significand meaningless
3278 category = fcInfinity;
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00003279 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesena719a602007-08-24 00:56:33 +00003280 // sign, exponent, significand meaningless
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003281 category = fcNaN;
3282 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003283 } else {
3284 category = fcNormal;
Dale Johannesena719a602007-08-24 00:56:33 +00003285 exponent = myexponent - 127; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00003286 *significandParts() = mysignificand;
3287 if (myexponent==0) // denormal
3288 exponent = -126;
3289 else
3290 *significandParts() |= 0x800000; // integer bit
Dale Johannesena719a602007-08-24 00:56:33 +00003291 }
3292}
Dale Johannesen245dceb2007-09-11 18:32:33 +00003293
Chris Lattner4794b2b2009-10-16 02:13:51 +00003294void
3295APFloat::initFromHalfAPInt(const APInt & api)
3296{
3297 assert(api.getBitWidth()==16);
3298 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen0d670b52009-10-23 04:02:51 +00003299 uint32_t myexponent = (i >> 10) & 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003300 uint32_t mysignificand = i & 0x3ff;
3301
3302 initialize(&APFloat::IEEEhalf);
3303 assert(partCount()==1);
3304
3305 sign = i >> 15;
3306 if (myexponent==0 && mysignificand==0) {
3307 // exponent, significand meaningless
3308 category = fcZero;
3309 } else if (myexponent==0x1f && mysignificand==0) {
3310 // exponent, significand meaningless
3311 category = fcInfinity;
3312 } else if (myexponent==0x1f && mysignificand!=0) {
3313 // sign, exponent, significand meaningless
3314 category = fcNaN;
3315 *significandParts() = mysignificand;
3316 } else {
3317 category = fcNormal;
3318 exponent = myexponent - 15; //bias
3319 *significandParts() = mysignificand;
3320 if (myexponent==0) // denormal
3321 exponent = -14;
3322 else
3323 *significandParts() |= 0x400; // integer bit
3324 }
3325}
3326
Dale Johannesen245dceb2007-09-11 18:32:33 +00003327/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesen007aa372007-10-11 18:07:22 +00003328/// we infer the floating point type from the size of the APInt. The
3329/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3330/// when the size is anything else).
Dale Johannesen245dceb2007-09-11 18:32:33 +00003331void
Tim Northover29178a32013-01-22 09:46:31 +00003332APFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api)
Neil Booth9acbf5a2007-09-26 21:33:42 +00003333{
Tim Northover29178a32013-01-22 09:46:31 +00003334 if (Sem == &IEEEhalf)
Chris Lattner4794b2b2009-10-16 02:13:51 +00003335 return initFromHalfAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003336 if (Sem == &IEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003337 return initFromFloatAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003338 if (Sem == &IEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003339 return initFromDoubleAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003340 if (Sem == &x87DoubleExtended)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003341 return initFromF80LongDoubleAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003342 if (Sem == &IEEEquad)
3343 return initFromQuadrupleAPInt(api);
3344 if (Sem == &PPCDoubleDouble)
3345 return initFromPPCDoubleDoubleAPInt(api);
3346
Craig Topper2617dcc2014-04-15 06:32:26 +00003347 llvm_unreachable(nullptr);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003348}
3349
Nadav Rotem7cc6d122011-02-17 21:22:27 +00003350APFloat
3351APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
3352{
Tim Northover29178a32013-01-22 09:46:31 +00003353 switch (BitWidth) {
3354 case 16:
3355 return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth));
3356 case 32:
3357 return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth));
3358 case 64:
3359 return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth));
3360 case 80:
3361 return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth));
3362 case 128:
3363 if (isIEEE)
3364 return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth));
3365 return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
3366 default:
3367 llvm_unreachable("Unknown floating bit width");
3368 }
Nadav Rotem7cc6d122011-02-17 21:22:27 +00003369}
3370
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003371/// Make this number the largest magnitude normal number in the given
3372/// semantics.
3373void APFloat::makeLargest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003374 // We want (in interchange format):
3375 // sign = {Negative}
3376 // exponent = 1..10
3377 // significand = 1..1
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003378 category = fcNormal;
3379 sign = Negative;
3380 exponent = semantics->maxExponent;
John McCall29b5c282009-12-24 08:56:26 +00003381
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003382 // Use memset to set all but the highest integerPart to all ones.
3383 integerPart *significand = significandParts();
3384 unsigned PartCount = partCount();
3385 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
John McCall29b5c282009-12-24 08:56:26 +00003386
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003387 // Set the high integerPart especially setting all unused top bits for
3388 // internal consistency.
3389 const unsigned NumUnusedHighBits =
3390 PartCount*integerPartWidth - semantics->precision;
Alexey Samsonovba1ecbc2014-09-06 00:41:19 +00003391 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3392 ? (~integerPart(0) >> NumUnusedHighBits)
3393 : 0;
John McCall29b5c282009-12-24 08:56:26 +00003394}
3395
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003396/// Make this number the smallest magnitude denormal number in the given
3397/// semantics.
3398void APFloat::makeSmallest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003399 // We want (in interchange format):
3400 // sign = {Negative}
3401 // exponent = 0..0
3402 // significand = 0..01
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003403 category = fcNormal;
3404 sign = Negative;
3405 exponent = semantics->minExponent;
3406 APInt::tcSet(significandParts(), 1, partCount());
3407}
John McCall29b5c282009-12-24 08:56:26 +00003408
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003409
3410APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
3411 // We want (in interchange format):
3412 // sign = {Negative}
3413 // exponent = 1..10
3414 // significand = 1..1
3415 APFloat Val(Sem, uninitialized);
3416 Val.makeLargest(Negative);
3417 return Val;
3418}
3419
3420APFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) {
3421 // We want (in interchange format):
3422 // sign = {Negative}
3423 // exponent = 0..0
3424 // significand = 0..01
3425 APFloat Val(Sem, uninitialized);
3426 Val.makeSmallest(Negative);
John McCall29b5c282009-12-24 08:56:26 +00003427 return Val;
3428}
3429
3430APFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) {
Michael Gottesman79b09672013-06-27 21:58:19 +00003431 APFloat Val(Sem, uninitialized);
John McCall29b5c282009-12-24 08:56:26 +00003432
3433 // We want (in interchange format):
3434 // sign = {Negative}
3435 // exponent = 0..0
3436 // significand = 10..0
3437
Michael Gottesman30a90eb2013-07-27 21:49:21 +00003438 Val.category = fcNormal;
Michael Gottesmanccaf3322013-06-27 20:40:11 +00003439 Val.zeroSignificand();
Michael Gottesman79b09672013-06-27 21:58:19 +00003440 Val.sign = Negative;
3441 Val.exponent = Sem.minExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003442 Val.significandParts()[partCountForBits(Sem.precision)-1] |=
Eli Friedmand4330422011-10-12 21:56:19 +00003443 (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth));
John McCall29b5c282009-12-24 08:56:26 +00003444
3445 return Val;
3446}
3447
Tim Northover29178a32013-01-22 09:46:31 +00003448APFloat::APFloat(const fltSemantics &Sem, const APInt &API) {
3449 initFromAPInt(&Sem, API);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003450}
3451
Ulrich Weigande1d62f92012-10-29 18:17:42 +00003452APFloat::APFloat(float f) {
Tim Northover29178a32013-01-22 09:46:31 +00003453 initFromAPInt(&IEEEsingle, APInt::floatToBits(f));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003454}
3455
Ulrich Weigande1d62f92012-10-29 18:17:42 +00003456APFloat::APFloat(double d) {
Tim Northover29178a32013-01-22 09:46:31 +00003457 initFromAPInt(&IEEEdouble, APInt::doubleToBits(d));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003458}
John McCall29b5c282009-12-24 08:56:26 +00003459
3460namespace {
David Blaikie70fdf722012-07-25 18:04:24 +00003461 void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3462 Buffer.append(Str.begin(), Str.end());
John McCall29b5c282009-12-24 08:56:26 +00003463 }
3464
John McCalle6212ace2009-12-24 12:16:56 +00003465 /// Removes data from the given significand until it is no more
3466 /// precise than is required for the desired precision.
3467 void AdjustToPrecision(APInt &significand,
3468 int &exp, unsigned FormatPrecision) {
3469 unsigned bits = significand.getActiveBits();
3470
3471 // 196/59 is a very slight overestimate of lg_2(10).
3472 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3473
3474 if (bits <= bitsRequired) return;
3475
3476 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3477 if (!tensRemovable) return;
3478
3479 exp += tensRemovable;
3480
3481 APInt divisor(significand.getBitWidth(), 1);
3482 APInt powten(significand.getBitWidth(), 10);
3483 while (true) {
3484 if (tensRemovable & 1)
3485 divisor *= powten;
3486 tensRemovable >>= 1;
3487 if (!tensRemovable) break;
3488 powten *= powten;
3489 }
3490
3491 significand = significand.udiv(divisor);
3492
Hao Liube99cc32013-03-20 01:46:36 +00003493 // Truncate the significand down to its active bit count.
3494 significand = significand.trunc(significand.getActiveBits());
John McCalle6212ace2009-12-24 12:16:56 +00003495 }
3496
3497
John McCall29b5c282009-12-24 08:56:26 +00003498 void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3499 int &exp, unsigned FormatPrecision) {
3500 unsigned N = buffer.size();
3501 if (N <= FormatPrecision) return;
3502
3503 // The most significant figures are the last ones in the buffer.
3504 unsigned FirstSignificant = N - FormatPrecision;
3505
3506 // Round.
3507 // FIXME: this probably shouldn't use 'round half up'.
3508
3509 // Rounding down is just a truncation, except we also want to drop
3510 // trailing zeros from the new result.
3511 if (buffer[FirstSignificant - 1] < '5') {
NAKAMURA Takumi5adeb932012-02-19 03:18:29 +00003512 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
John McCall29b5c282009-12-24 08:56:26 +00003513 FirstSignificant++;
3514
3515 exp += FirstSignificant;
3516 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3517 return;
3518 }
3519
3520 // Rounding up requires a decimal add-with-carry. If we continue
3521 // the carry, the newly-introduced zeros will just be truncated.
3522 for (unsigned I = FirstSignificant; I != N; ++I) {
3523 if (buffer[I] == '9') {
3524 FirstSignificant++;
3525 } else {
3526 buffer[I]++;
3527 break;
3528 }
3529 }
3530
3531 // If we carried through, we have exactly one digit of precision.
3532 if (FirstSignificant == N) {
3533 exp += FirstSignificant;
3534 buffer.clear();
3535 buffer.push_back('1');
3536 return;
3537 }
3538
3539 exp += FirstSignificant;
3540 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3541 }
3542}
3543
3544void APFloat::toString(SmallVectorImpl<char> &Str,
3545 unsigned FormatPrecision,
Chris Lattner4c1e4db2010-03-06 19:20:13 +00003546 unsigned FormatMaxPadding) const {
John McCall29b5c282009-12-24 08:56:26 +00003547 switch (category) {
3548 case fcInfinity:
3549 if (isNegative())
3550 return append(Str, "-Inf");
3551 else
3552 return append(Str, "+Inf");
3553
3554 case fcNaN: return append(Str, "NaN");
3555
3556 case fcZero:
3557 if (isNegative())
3558 Str.push_back('-');
3559
3560 if (!FormatMaxPadding)
3561 append(Str, "0.0E+0");
3562 else
3563 Str.push_back('0');
3564 return;
3565
3566 case fcNormal:
3567 break;
3568 }
3569
3570 if (isNegative())
3571 Str.push_back('-');
3572
3573 // Decompose the number into an APInt and an exponent.
3574 int exp = exponent - ((int) semantics->precision - 1);
3575 APInt significand(semantics->precision,
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00003576 makeArrayRef(significandParts(),
3577 partCountForBits(semantics->precision)));
John McCall29b5c282009-12-24 08:56:26 +00003578
John McCalldd5044a2009-12-24 23:18:09 +00003579 // Set FormatPrecision if zero. We want to do this before we
3580 // truncate trailing zeros, as those are part of the precision.
3581 if (!FormatPrecision) {
Eli Friedmane72f1322013-08-29 23:44:34 +00003582 // We use enough digits so the number can be round-tripped back to an
3583 // APFloat. The formula comes from "How to Print Floating-Point Numbers
3584 // Accurately" by Steele and White.
3585 // FIXME: Using a formula based purely on the precision is conservative;
3586 // we can print fewer digits depending on the actual value being printed.
John McCalldd5044a2009-12-24 23:18:09 +00003587
Eli Friedmane72f1322013-08-29 23:44:34 +00003588 // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3589 FormatPrecision = 2 + semantics->precision * 59 / 196;
John McCalldd5044a2009-12-24 23:18:09 +00003590 }
3591
John McCall29b5c282009-12-24 08:56:26 +00003592 // Ignore trailing binary zeros.
3593 int trailingZeros = significand.countTrailingZeros();
3594 exp += trailingZeros;
3595 significand = significand.lshr(trailingZeros);
3596
3597 // Change the exponent from 2^e to 10^e.
3598 if (exp == 0) {
3599 // Nothing to do.
3600 } else if (exp > 0) {
3601 // Just shift left.
Jay Foad583abbc2010-12-07 08:25:19 +00003602 significand = significand.zext(semantics->precision + exp);
John McCall29b5c282009-12-24 08:56:26 +00003603 significand <<= exp;
3604 exp = 0;
3605 } else { /* exp < 0 */
3606 int texp = -exp;
3607
3608 // We transform this using the identity:
3609 // (N)(2^-e) == (N)(5^e)(10^-e)
3610 // This means we have to multiply N (the significand) by 5^e.
3611 // To avoid overflow, we have to operate on numbers large
3612 // enough to store N * 5^e:
3613 // log2(N * 5^e) == log2(N) + e * log2(5)
John McCalldd5044a2009-12-24 23:18:09 +00003614 // <= semantics->precision + e * 137 / 59
3615 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003616
Eli Friedman19546412011-10-07 23:40:49 +00003617 unsigned precision = semantics->precision + (137 * texp + 136) / 59;
John McCall29b5c282009-12-24 08:56:26 +00003618
3619 // Multiply significand by 5^e.
3620 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
Jay Foad583abbc2010-12-07 08:25:19 +00003621 significand = significand.zext(precision);
John McCall29b5c282009-12-24 08:56:26 +00003622 APInt five_to_the_i(precision, 5);
3623 while (true) {
3624 if (texp & 1) significand *= five_to_the_i;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003625
John McCall29b5c282009-12-24 08:56:26 +00003626 texp >>= 1;
3627 if (!texp) break;
3628 five_to_the_i *= five_to_the_i;
3629 }
3630 }
3631
John McCalle6212ace2009-12-24 12:16:56 +00003632 AdjustToPrecision(significand, exp, FormatPrecision);
3633
Dmitri Gribenko226fea52013-01-13 16:01:15 +00003634 SmallVector<char, 256> buffer;
John McCall29b5c282009-12-24 08:56:26 +00003635
3636 // Fill the buffer.
3637 unsigned precision = significand.getBitWidth();
3638 APInt ten(precision, 10);
3639 APInt digit(precision, 0);
3640
3641 bool inTrail = true;
3642 while (significand != 0) {
3643 // digit <- significand % 10
3644 // significand <- significand / 10
3645 APInt::udivrem(significand, ten, significand, digit);
3646
3647 unsigned d = digit.getZExtValue();
3648
3649 // Drop trailing zeros.
3650 if (inTrail && !d) exp++;
3651 else {
3652 buffer.push_back((char) ('0' + d));
3653 inTrail = false;
3654 }
3655 }
3656
3657 assert(!buffer.empty() && "no characters in buffer!");
3658
3659 // Drop down to FormatPrecision.
3660 // TODO: don't do more precise calculations above than are required.
3661 AdjustToPrecision(buffer, exp, FormatPrecision);
3662
3663 unsigned NDigits = buffer.size();
3664
John McCalldd5044a2009-12-24 23:18:09 +00003665 // Check whether we should use scientific notation.
John McCall29b5c282009-12-24 08:56:26 +00003666 bool FormatScientific;
3667 if (!FormatMaxPadding)
3668 FormatScientific = true;
3669 else {
John McCall29b5c282009-12-24 08:56:26 +00003670 if (exp >= 0) {
John McCalldd5044a2009-12-24 23:18:09 +00003671 // 765e3 --> 765000
3672 // ^^^
3673 // But we shouldn't make the number look more precise than it is.
3674 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3675 NDigits + (unsigned) exp > FormatPrecision);
John McCall29b5c282009-12-24 08:56:26 +00003676 } else {
John McCalldd5044a2009-12-24 23:18:09 +00003677 // Power of the most significant digit.
3678 int MSD = exp + (int) (NDigits - 1);
3679 if (MSD >= 0) {
John McCall29b5c282009-12-24 08:56:26 +00003680 // 765e-2 == 7.65
John McCalldd5044a2009-12-24 23:18:09 +00003681 FormatScientific = false;
John McCall29b5c282009-12-24 08:56:26 +00003682 } else {
3683 // 765e-5 == 0.00765
3684 // ^ ^^
John McCalldd5044a2009-12-24 23:18:09 +00003685 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
John McCall29b5c282009-12-24 08:56:26 +00003686 }
3687 }
John McCall29b5c282009-12-24 08:56:26 +00003688 }
3689
3690 // Scientific formatting is pretty straightforward.
3691 if (FormatScientific) {
3692 exp += (NDigits - 1);
3693
3694 Str.push_back(buffer[NDigits-1]);
3695 Str.push_back('.');
3696 if (NDigits == 1)
3697 Str.push_back('0');
3698 else
3699 for (unsigned I = 1; I != NDigits; ++I)
3700 Str.push_back(buffer[NDigits-1-I]);
3701 Str.push_back('E');
3702
3703 Str.push_back(exp >= 0 ? '+' : '-');
3704 if (exp < 0) exp = -exp;
3705 SmallVector<char, 6> expbuf;
3706 do {
3707 expbuf.push_back((char) ('0' + (exp % 10)));
3708 exp /= 10;
3709 } while (exp);
3710 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3711 Str.push_back(expbuf[E-1-I]);
3712 return;
3713 }
3714
3715 // Non-scientific, positive exponents.
3716 if (exp >= 0) {
3717 for (unsigned I = 0; I != NDigits; ++I)
3718 Str.push_back(buffer[NDigits-1-I]);
3719 for (unsigned I = 0; I != (unsigned) exp; ++I)
3720 Str.push_back('0');
3721 return;
3722 }
3723
3724 // Non-scientific, negative exponents.
3725
3726 // The number of digits to the left of the decimal point.
3727 int NWholeDigits = exp + (int) NDigits;
3728
3729 unsigned I = 0;
3730 if (NWholeDigits > 0) {
3731 for (; I != (unsigned) NWholeDigits; ++I)
3732 Str.push_back(buffer[NDigits-I-1]);
3733 Str.push_back('.');
3734 } else {
3735 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3736
3737 Str.push_back('0');
3738 Str.push_back('.');
3739 for (unsigned Z = 1; Z != NZeros; ++Z)
3740 Str.push_back('0');
3741 }
3742
3743 for (; I != NDigits; ++I)
3744 Str.push_back(buffer[NDigits-I-1]);
3745}
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003746
3747bool APFloat::getExactInverse(APFloat *inv) const {
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003748 // Special floats and denormals have no exact inverse.
Michael Gottesman8136c382013-06-26 23:17:28 +00003749 if (!isFiniteNonZero())
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003750 return false;
3751
3752 // Check that the number is a power of two by making sure that only the
3753 // integer bit is set in the significand.
3754 if (significandLSB() != semantics->precision - 1)
3755 return false;
3756
3757 // Get the inverse.
3758 APFloat reciprocal(*semantics, 1ULL);
3759 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3760 return false;
3761
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003762 // Avoid multiplication with a denormal, it is not safe on all platforms and
3763 // may be slower than a normal division.
Benjamin Kramer6bef24f2013-06-01 11:26:33 +00003764 if (reciprocal.isDenormal())
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003765 return false;
3766
Michael Gottesman8136c382013-06-26 23:17:28 +00003767 assert(reciprocal.isFiniteNonZero() &&
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003768 reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3769
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003770 if (inv)
3771 *inv = reciprocal;
3772
3773 return true;
3774}
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003775
3776bool APFloat::isSignaling() const {
3777 if (!isNaN())
3778 return false;
3779
3780 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3781 // first bit of the trailing significand being 0.
3782 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3783}
3784
3785/// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3786///
3787/// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3788/// appropriate sign switching before/after the computation.
3789APFloat::opStatus APFloat::next(bool nextDown) {
3790 // If we are performing nextDown, swap sign so we have -x.
3791 if (nextDown)
3792 changeSign();
3793
3794 // Compute nextUp(x)
3795 opStatus result = opOK;
3796
3797 // Handle each float category separately.
3798 switch (category) {
3799 case fcInfinity:
3800 // nextUp(+inf) = +inf
3801 if (!isNegative())
3802 break;
3803 // nextUp(-inf) = -getLargest()
3804 makeLargest(true);
3805 break;
3806 case fcNaN:
3807 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3808 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3809 // change the payload.
3810 if (isSignaling()) {
3811 result = opInvalidOp;
Alp Tokercb402912014-01-24 17:20:08 +00003812 // For consistency, propagate the sign of the sNaN to the qNaN.
Craig Topperc10719f2014-04-07 04:17:22 +00003813 makeNaN(false, isNegative(), nullptr);
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003814 }
3815 break;
3816 case fcZero:
3817 // nextUp(pm 0) = +getSmallest()
3818 makeSmallest(false);
3819 break;
3820 case fcNormal:
3821 // nextUp(-getSmallest()) = -0
3822 if (isSmallest() && isNegative()) {
3823 APInt::tcSet(significandParts(), 0, partCount());
3824 category = fcZero;
3825 exponent = 0;
3826 break;
3827 }
3828
3829 // nextUp(getLargest()) == INFINITY
3830 if (isLargest() && !isNegative()) {
3831 APInt::tcSet(significandParts(), 0, partCount());
3832 category = fcInfinity;
3833 exponent = semantics->maxExponent + 1;
3834 break;
3835 }
3836
3837 // nextUp(normal) == normal + inc.
3838 if (isNegative()) {
3839 // If we are negative, we need to decrement the significand.
3840
3841 // We only cross a binade boundary that requires adjusting the exponent
3842 // if:
3843 // 1. exponent != semantics->minExponent. This implies we are not in the
3844 // smallest binade or are dealing with denormals.
3845 // 2. Our significand excluding the integral bit is all zeros.
3846 bool WillCrossBinadeBoundary =
3847 exponent != semantics->minExponent && isSignificandAllZeros();
3848
3849 // Decrement the significand.
3850 //
3851 // We always do this since:
Alp Tokerf907b892013-12-05 05:44:44 +00003852 // 1. If we are dealing with a non-binade decrement, by definition we
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003853 // just decrement the significand.
3854 // 2. If we are dealing with a normal -> normal binade decrement, since
3855 // we have an explicit integral bit the fact that all bits but the
3856 // integral bit are zero implies that subtracting one will yield a
3857 // significand with 0 integral bit and 1 in all other spots. Thus we
3858 // must just adjust the exponent and set the integral bit to 1.
3859 // 3. If we are dealing with a normal -> denormal binade decrement,
3860 // since we set the integral bit to 0 when we represent denormals, we
3861 // just decrement the significand.
3862 integerPart *Parts = significandParts();
3863 APInt::tcDecrement(Parts, partCount());
3864
3865 if (WillCrossBinadeBoundary) {
3866 // Our result is a normal number. Do the following:
3867 // 1. Set the integral bit to 1.
3868 // 2. Decrement the exponent.
3869 APInt::tcSetBit(Parts, semantics->precision - 1);
3870 exponent--;
3871 }
3872 } else {
3873 // If we are positive, we need to increment the significand.
3874
3875 // We only cross a binade boundary that requires adjusting the exponent if
3876 // the input is not a denormal and all of said input's significand bits
3877 // are set. If all of said conditions are true: clear the significand, set
3878 // the integral bit to 1, and increment the exponent. If we have a
3879 // denormal always increment since moving denormals and the numbers in the
3880 // smallest normal binade have the same exponent in our representation.
3881 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3882
3883 if (WillCrossBinadeBoundary) {
3884 integerPart *Parts = significandParts();
3885 APInt::tcSet(Parts, 0, partCount());
3886 APInt::tcSetBit(Parts, semantics->precision - 1);
3887 assert(exponent != semantics->maxExponent &&
3888 "We can not increment an exponent beyond the maxExponent allowed"
3889 " by the given floating point semantics.");
3890 exponent++;
3891 } else {
3892 incrementSignificand();
3893 }
3894 }
3895 break;
3896 }
3897
3898 // If we are performing nextDown, swap sign so we have -nextUp(-x)
3899 if (nextDown)
3900 changeSign();
3901
3902 return result;
3903}
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003904
3905void
3906APFloat::makeInf(bool Negative) {
3907 category = fcInfinity;
3908 sign = Negative;
3909 exponent = semantics->maxExponent + 1;
3910 APInt::tcSet(significandParts(), 0, partCount());
3911}
3912
3913void
3914APFloat::makeZero(bool Negative) {
3915 category = fcZero;
3916 sign = Negative;
3917 exponent = semantics->minExponent-1;
3918 APInt::tcSet(significandParts(), 0, partCount());
3919}
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003920
3921APFloat llvm::scalbn(APFloat X, int Exp) {
3922 if (X.isInfinity() || X.isZero() || X.isNaN())
3923 return std::move(X);
3924
3925 auto MaxExp = X.getSemantics().maxExponent;
3926 auto MinExp = X.getSemantics().minExponent;
3927 if (Exp > (MaxExp - X.exponent))
3928 // Overflow saturates to infinity.
3929 return APFloat::getInf(X.getSemantics(), X.isNegative());
3930 if (Exp < (MinExp - X.exponent))
3931 // Underflow saturates to zero.
3932 return APFloat::getZero(X.getSemantics(), X.isNegative());
3933
3934 X.exponent += Exp;
3935 return std::move(X);
3936}