blob: a8e14d6c08b5008ee02a05fdd8c3101264817bbe [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>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000024#include <limits.h>
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000025
26using namespace llvm;
27
Michael Gottesman9b877e12013-06-24 09:57:57 +000028/// A macro used to combine two fcCategory enums into one key which can be used
29/// in a switch statement to classify how the interaction of two APFloat's
30/// categories affects an operation.
31///
32/// TODO: If clang source code is ever allowed to use constexpr in its own
33/// codebase, change this into a static inline function.
34#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000035
Neil Booth8f1946f2007-10-03 22:26:02 +000036/* Assumed in hexadecimal significand parsing, and conversion to
37 hexadecimal strings. */
Benjamin Kramer7000ca32014-10-12 17:56:40 +000038static_assert(integerPartWidth % 4 == 0, "Part width must be divisible by 4!");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000039
40namespace llvm {
41
42 /* Represents floating point arithmetic semantics. */
43 struct fltSemantics {
44 /* The largest E such that 2^E is representable; this matches the
45 definition of IEEE 754. */
Michael Gottesman9dc98332013-06-24 04:06:23 +000046 APFloat::ExponentType maxExponent;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000047
48 /* The smallest E such that 2^E is a normalized number; this
49 matches the definition of IEEE 754. */
Michael Gottesman9dc98332013-06-24 04:06:23 +000050 APFloat::ExponentType minExponent;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000051
52 /* Number of bits in the significand. This includes the integer
53 bit. */
Neil Booth146fdb32007-10-12 15:33:27 +000054 unsigned int precision;
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000055
56 /* Number of bits actually used in the semantics. */
57 unsigned int sizeInBits;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000058 };
59
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000060 const fltSemantics APFloat::IEEEhalf = { 15, -14, 11, 16 };
61 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, 32 };
62 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, 64 };
63 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, 128 };
64 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, 80 };
65 const fltSemantics APFloat::Bogus = { 0, 0, 0, 0 };
Dale Johannesen007aa372007-10-11 18:07:22 +000066
Ulrich Weigandd9f7e252012-10-29 18:09:01 +000067 /* The PowerPC format consists of two doubles. It does not map cleanly
68 onto the usual format above. It is approximated using twice the
69 mantissa bits. Note that for exponents near the double minimum,
70 we no longer can represent the full 106 mantissa bits, so those
71 will be treated as denormal numbers.
72
73 FIXME: While this approximation is equivalent to what GCC uses for
74 compile-time arithmetic on PPC double-double numbers, it is not able
75 to represent all possible values held by a PPC double-double number,
76 for example: (long double) 1.0 + (long double) 0x1p-106
77 Should this be replaced by a full emulation of PPC double-double? */
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000078 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53, 128 };
Neil Boothb93d90e2007-10-12 16:02:31 +000079
80 /* A tight upper bound on number of parts required to hold the value
81 pow(5, power) is
82
Neil Booth91305512007-10-15 15:00:55 +000083 power * 815 / (351 * integerPartWidth) + 1
Dan Gohmanb452d4e2010-03-24 19:38:02 +000084
Neil Boothb93d90e2007-10-12 16:02:31 +000085 However, whilst the result may require only this many parts,
86 because we are multiplying two values to get it, the
87 multiplication may require an extra part with the excess part
88 being zero (consider the trivial case of 1 * 1, tcFullMultiply
89 requires two parts to hold the single-part result). So we add an
90 extra one to guarantee enough space whilst multiplying. */
91 const unsigned int maxExponent = 16383;
92 const unsigned int maxPrecision = 113;
93 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
Neil Booth91305512007-10-15 15:00:55 +000094 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
95 / (351 * integerPartWidth));
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000096}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000097
Chris Lattner91702092009-03-12 23:59:55 +000098/* A bunch of private, handy routines. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000099
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000100static inline unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000101partCountForBits(unsigned int bits)
102{
103 return ((bits) + integerPartWidth - 1) / integerPartWidth;
104}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000105
Chris Lattner91702092009-03-12 23:59:55 +0000106/* Returns 0U-9U. Return values >= 10U are not digits. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000107static inline unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000108decDigitValue(unsigned int c)
109{
110 return c - '0';
111}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000112
Chris Lattner91702092009-03-12 23:59:55 +0000113/* Return the value of a decimal exponent of the form
114 [+-]ddddddd.
Neil Booth4ed401b2007-10-14 10:16:12 +0000115
Chris Lattner91702092009-03-12 23:59:55 +0000116 If the exponent overflows, returns a large exponent with the
117 appropriate sign. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000118static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000119readExponent(StringRef::iterator begin, StringRef::iterator end)
Chris Lattner91702092009-03-12 23:59:55 +0000120{
121 bool isNegative;
122 unsigned int absExponent;
123 const unsigned int overlargeExponent = 24000; /* FIXME. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000124 StringRef::iterator p = begin;
125
126 assert(p != end && "Exponent has no digits");
Neil Booth4ed401b2007-10-14 10:16:12 +0000127
Chris Lattner91702092009-03-12 23:59:55 +0000128 isNegative = (*p == '-');
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000129 if (*p == '-' || *p == '+') {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000130 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000131 assert(p != end && "Exponent has no digits");
132 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000133
Chris Lattner91702092009-03-12 23:59:55 +0000134 absExponent = decDigitValue(*p++);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000135 assert(absExponent < 10U && "Invalid character in exponent");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000136
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000137 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000138 unsigned int value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000139
Chris Lattner91702092009-03-12 23:59:55 +0000140 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000141 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000142
Chris Lattner91702092009-03-12 23:59:55 +0000143 value += absExponent * 10;
144 if (absExponent >= overlargeExponent) {
145 absExponent = overlargeExponent;
Dale Johannesen370c77c2010-08-19 17:58:35 +0000146 p = end; /* outwit assert below */
Chris Lattner91702092009-03-12 23:59:55 +0000147 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000148 }
Chris Lattner91702092009-03-12 23:59:55 +0000149 absExponent = value;
150 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000151
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000152 assert(p == end && "Invalid exponent in exponent");
153
Chris Lattner91702092009-03-12 23:59:55 +0000154 if (isNegative)
155 return -(int) absExponent;
156 else
157 return (int) absExponent;
158}
159
160/* This is ugly and needs cleaning up, but I don't immediately see
161 how whilst remaining safe. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000162static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000163totalExponent(StringRef::iterator p, StringRef::iterator end,
164 int exponentAdjustment)
Chris Lattner91702092009-03-12 23:59:55 +0000165{
166 int unsignedExponent;
167 bool negative, overflow;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000168 int exponent = 0;
Chris Lattner91702092009-03-12 23:59:55 +0000169
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000170 assert(p != end && "Exponent has no digits");
171
Chris Lattner91702092009-03-12 23:59:55 +0000172 negative = *p == '-';
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000173 if (*p == '-' || *p == '+') {
Chris Lattner91702092009-03-12 23:59:55 +0000174 p++;
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000175 assert(p != end && "Exponent has no digits");
176 }
Chris Lattner91702092009-03-12 23:59:55 +0000177
178 unsignedExponent = 0;
179 overflow = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000180 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000181 unsigned int value;
182
183 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000184 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000185
Chris Lattner91702092009-03-12 23:59:55 +0000186 unsignedExponent = unsignedExponent * 10 + value;
Richard Smith156d9202012-08-24 00:01:19 +0000187 if (unsignedExponent > 32767) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000188 overflow = true;
Richard Smith156d9202012-08-24 00:01:19 +0000189 break;
190 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000191 }
192
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000193 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000194 overflow = true;
195
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000196 if (!overflow) {
Chris Lattner91702092009-03-12 23:59:55 +0000197 exponent = unsignedExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000198 if (negative)
Chris Lattner91702092009-03-12 23:59:55 +0000199 exponent = -exponent;
200 exponent += exponentAdjustment;
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000201 if (exponent > 32767 || exponent < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000202 overflow = true;
203 }
204
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000205 if (overflow)
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000206 exponent = negative ? -32768: 32767;
Chris Lattner91702092009-03-12 23:59:55 +0000207
208 return exponent;
209}
210
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000211static StringRef::iterator
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000212skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
213 StringRef::iterator *dot)
Chris Lattner91702092009-03-12 23:59:55 +0000214{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000215 StringRef::iterator p = begin;
216 *dot = end;
Nick Lewycky095b92e2014-09-06 01:16:42 +0000217 while (p != end && *p == '0')
Chris Lattner91702092009-03-12 23:59:55 +0000218 p++;
219
Nick Lewycky095b92e2014-09-06 01:16:42 +0000220 if (p != end && *p == '.') {
Chris Lattner91702092009-03-12 23:59:55 +0000221 *dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000222
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000223 assert(end - begin != 1 && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000224
Nick Lewycky095b92e2014-09-06 01:16:42 +0000225 while (p != end && *p == '0')
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000226 p++;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000227 }
228
Chris Lattner91702092009-03-12 23:59:55 +0000229 return p;
230}
Neil Booth4ed401b2007-10-14 10:16:12 +0000231
Chris Lattner91702092009-03-12 23:59:55 +0000232/* Given a normal decimal floating point number of the form
Neil Booth4ed401b2007-10-14 10:16:12 +0000233
Chris Lattner91702092009-03-12 23:59:55 +0000234 dddd.dddd[eE][+-]ddd
Neil Booth91305512007-10-15 15:00:55 +0000235
Chris Lattner91702092009-03-12 23:59:55 +0000236 where the decimal point and exponent are optional, fill out the
237 structure D. Exponent is appropriate if the significand is
238 treated as an integer, and normalizedExponent if the significand
239 is taken to have the decimal point after a single leading
240 non-zero digit.
Neil Booth4ed401b2007-10-14 10:16:12 +0000241
Chris Lattner91702092009-03-12 23:59:55 +0000242 If the value is zero, V->firstSigDigit points to a non-digit, and
243 the return exponent is zero.
244*/
245struct decimalInfo {
246 const char *firstSigDigit;
247 const char *lastSigDigit;
248 int exponent;
249 int normalizedExponent;
250};
Neil Booth4ed401b2007-10-14 10:16:12 +0000251
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000252static void
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000253interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
254 decimalInfo *D)
Chris Lattner91702092009-03-12 23:59:55 +0000255{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000256 StringRef::iterator dot = end;
257 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
Neil Booth4ed401b2007-10-14 10:16:12 +0000258
Chris Lattner91702092009-03-12 23:59:55 +0000259 D->firstSigDigit = p;
260 D->exponent = 0;
261 D->normalizedExponent = 0;
262
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000263 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000264 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000265 assert(dot == end && "String contains multiple dots");
Chris Lattner91702092009-03-12 23:59:55 +0000266 dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000267 if (p == end)
268 break;
Neil Booth4ed401b2007-10-14 10:16:12 +0000269 }
Chris Lattner91702092009-03-12 23:59:55 +0000270 if (decDigitValue(*p) >= 10U)
271 break;
Chris Lattner91702092009-03-12 23:59:55 +0000272 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000273
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000274 if (p != end) {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000275 assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
276 assert(p != begin && "Significand has no digits");
277 assert((dot == end || p - begin != 1) && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000278
279 /* p points to the first non-digit in the string */
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000280 D->exponent = readExponent(p + 1, end);
Neil Booth4ed401b2007-10-14 10:16:12 +0000281
Chris Lattner91702092009-03-12 23:59:55 +0000282 /* Implied decimal point? */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000283 if (dot == end)
Chris Lattner91702092009-03-12 23:59:55 +0000284 dot = p;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000285 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000286
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000287 /* If number is all zeroes accept any exponent. */
288 if (p != D->firstSigDigit) {
Chris Lattner91702092009-03-12 23:59:55 +0000289 /* Drop insignificant trailing zeroes. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000290 if (p != begin) {
Neil Booth4ed401b2007-10-14 10:16:12 +0000291 do
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000292 do
293 p--;
294 while (p != begin && *p == '0');
295 while (p != begin && *p == '.');
296 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000297
Chris Lattner91702092009-03-12 23:59:55 +0000298 /* Adjust the exponents for any decimal point. */
Michael Gottesman9dc98332013-06-24 04:06:23 +0000299 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
Chris Lattner91702092009-03-12 23:59:55 +0000300 D->normalizedExponent = (D->exponent +
Michael Gottesman9dc98332013-06-24 04:06:23 +0000301 static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
Chris Lattner91702092009-03-12 23:59:55 +0000302 - (dot > D->firstSigDigit && dot < p)));
Neil Booth4ed401b2007-10-14 10:16:12 +0000303 }
304
Chris Lattner91702092009-03-12 23:59:55 +0000305 D->lastSigDigit = p;
306}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000307
Chris Lattner91702092009-03-12 23:59:55 +0000308/* Return the trailing fraction of a hexadecimal number.
309 DIGITVALUE is the first hex digit of the fraction, P points to
310 the next digit. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000311static lostFraction
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000312trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
313 unsigned int digitValue)
Chris Lattner91702092009-03-12 23:59:55 +0000314{
315 unsigned int hexDigit;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000316
Chris Lattner91702092009-03-12 23:59:55 +0000317 /* If the first trailing digit isn't 0 or 8 we can work out the
318 fraction immediately. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000319 if (digitValue > 8)
Chris Lattner91702092009-03-12 23:59:55 +0000320 return lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000321 else if (digitValue < 8 && digitValue > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000322 return lfLessThanHalf;
Chris Lattner91702092009-03-12 23:59:55 +0000323
Eli Friedmand2eb07a2013-07-17 22:17:29 +0000324 // Otherwise we need to find the first non-zero digit.
325 while (p != end && (*p == '0' || *p == '.'))
Chris Lattner91702092009-03-12 23:59:55 +0000326 p++;
327
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000328 assert(p != end && "Invalid trailing hexadecimal fraction!");
329
Chris Lattner91702092009-03-12 23:59:55 +0000330 hexDigit = hexDigitValue(*p);
331
332 /* If we ran off the end it is exactly zero or one-half, otherwise
333 a little more. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000334 if (hexDigit == -1U)
Chris Lattner91702092009-03-12 23:59:55 +0000335 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
336 else
337 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
338}
339
340/* Return the fraction lost were a bignum truncated losing the least
341 significant BITS bits. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000342static lostFraction
Chris Lattner91702092009-03-12 23:59:55 +0000343lostFractionThroughTruncation(const integerPart *parts,
344 unsigned int partCount,
345 unsigned int bits)
346{
347 unsigned int lsb;
348
349 lsb = APInt::tcLSB(parts, partCount);
350
351 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000352 if (bits <= lsb)
Chris Lattner91702092009-03-12 23:59:55 +0000353 return lfExactlyZero;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000354 if (bits == lsb + 1)
Chris Lattner91702092009-03-12 23:59:55 +0000355 return lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000356 if (bits <= partCount * integerPartWidth &&
357 APInt::tcExtractBit(parts, bits - 1))
Chris Lattner91702092009-03-12 23:59:55 +0000358 return lfMoreThanHalf;
359
360 return lfLessThanHalf;
361}
362
363/* Shift DST right BITS bits noting lost fraction. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000364static lostFraction
Chris Lattner91702092009-03-12 23:59:55 +0000365shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
366{
367 lostFraction lost_fraction;
368
369 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
370
371 APInt::tcShiftRight(dst, parts, bits);
372
373 return lost_fraction;
374}
375
376/* Combine the effect of two lost fractions. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000377static lostFraction
Chris Lattner91702092009-03-12 23:59:55 +0000378combineLostFractions(lostFraction moreSignificant,
379 lostFraction lessSignificant)
380{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000381 if (lessSignificant != lfExactlyZero) {
382 if (moreSignificant == lfExactlyZero)
Chris Lattner91702092009-03-12 23:59:55 +0000383 moreSignificant = lfLessThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000384 else if (moreSignificant == lfExactlyHalf)
Chris Lattner91702092009-03-12 23:59:55 +0000385 moreSignificant = lfMoreThanHalf;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000386 }
387
Chris Lattner91702092009-03-12 23:59:55 +0000388 return moreSignificant;
389}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000390
Chris Lattner91702092009-03-12 23:59:55 +0000391/* The error from the true value, in half-ulps, on multiplying two
392 floating point numbers, which differ from the value they
393 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
394 than the returned value.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000395
Chris Lattner91702092009-03-12 23:59:55 +0000396 See "How to Read Floating Point Numbers Accurately" by William D
397 Clinger. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000398static unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000399HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
400{
401 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000402
Chris Lattner91702092009-03-12 23:59:55 +0000403 if (HUerr1 + HUerr2 == 0)
404 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
405 else
406 return inexactMultiply + 2 * (HUerr1 + HUerr2);
407}
Neil Booth8f1946f2007-10-03 22:26:02 +0000408
Chris Lattner91702092009-03-12 23:59:55 +0000409/* The number of ulps from the boundary (zero, or half if ISNEAREST)
410 when the least significant BITS are truncated. BITS cannot be
411 zero. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000412static integerPart
Chris Lattner91702092009-03-12 23:59:55 +0000413ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
414{
415 unsigned int count, partBits;
416 integerPart part, boundary;
Neil Boothd3985922007-10-07 08:51:21 +0000417
Evan Cheng67c90212009-10-27 21:35:42 +0000418 assert(bits != 0);
Neil Booth8f1946f2007-10-03 22:26:02 +0000419
Chris Lattner91702092009-03-12 23:59:55 +0000420 bits--;
421 count = bits / integerPartWidth;
422 partBits = bits % integerPartWidth + 1;
Neil Boothb93d90e2007-10-12 16:02:31 +0000423
Chris Lattner91702092009-03-12 23:59:55 +0000424 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
Neil Boothb93d90e2007-10-12 16:02:31 +0000425
Chris Lattner91702092009-03-12 23:59:55 +0000426 if (isNearest)
427 boundary = (integerPart) 1 << (partBits - 1);
428 else
429 boundary = 0;
430
431 if (count == 0) {
432 if (part - boundary <= boundary - part)
433 return part - boundary;
Neil Boothb93d90e2007-10-12 16:02:31 +0000434 else
Chris Lattner91702092009-03-12 23:59:55 +0000435 return boundary - part;
Neil Boothb93d90e2007-10-12 16:02:31 +0000436 }
437
Chris Lattner91702092009-03-12 23:59:55 +0000438 if (part == boundary) {
439 while (--count)
440 if (parts[count])
441 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000442
Chris Lattner91702092009-03-12 23:59:55 +0000443 return parts[0];
444 } else if (part == boundary - 1) {
445 while (--count)
446 if (~parts[count])
447 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000448
Chris Lattner91702092009-03-12 23:59:55 +0000449 return -parts[0];
450 }
Neil Boothb93d90e2007-10-12 16:02:31 +0000451
Chris Lattner91702092009-03-12 23:59:55 +0000452 return ~(integerPart) 0; /* A lot. */
453}
Neil Boothb93d90e2007-10-12 16:02:31 +0000454
Chris Lattner91702092009-03-12 23:59:55 +0000455/* Place pow(5, power) in DST, and return the number of parts used.
456 DST must be at least one part larger than size of the answer. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000457static unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000458powerOf5(integerPart *dst, unsigned int power)
459{
460 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
461 15625, 78125 };
Chris Lattnerb858c0e2009-03-13 00:24:01 +0000462 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
463 pow5s[0] = 78125 * 5;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000464
Chris Lattner0bf18692009-03-13 00:03:51 +0000465 unsigned int partsCount[16] = { 1 };
Chris Lattner91702092009-03-12 23:59:55 +0000466 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
467 unsigned int result;
Chris Lattner91702092009-03-12 23:59:55 +0000468 assert(power <= maxExponent);
469
470 p1 = dst;
471 p2 = scratch;
472
473 *p1 = firstEightPowers[power & 7];
474 power >>= 3;
475
476 result = 1;
477 pow5 = pow5s;
478
479 for (unsigned int n = 0; power; power >>= 1, n++) {
480 unsigned int pc;
481
482 pc = partsCount[n];
483
484 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
485 if (pc == 0) {
486 pc = partsCount[n - 1];
487 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
488 pc *= 2;
489 if (pow5[pc - 1] == 0)
490 pc--;
491 partsCount[n] = pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000492 }
493
Chris Lattner91702092009-03-12 23:59:55 +0000494 if (power & 1) {
495 integerPart *tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000496
Chris Lattner91702092009-03-12 23:59:55 +0000497 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
498 result += pc;
499 if (p2[result - 1] == 0)
500 result--;
Neil Boothb93d90e2007-10-12 16:02:31 +0000501
Chris Lattner91702092009-03-12 23:59:55 +0000502 /* Now result is in p1 with partsCount parts and p2 is scratch
503 space. */
Richard Trieu7a083812016-02-18 22:09:30 +0000504 tmp = p1;
505 p1 = p2;
506 p2 = tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000507 }
508
Chris Lattner91702092009-03-12 23:59:55 +0000509 pow5 += pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000510 }
511
Chris Lattner91702092009-03-12 23:59:55 +0000512 if (p1 != dst)
513 APInt::tcAssign(dst, p1, result);
Neil Boothb93d90e2007-10-12 16:02:31 +0000514
Chris Lattner91702092009-03-12 23:59:55 +0000515 return result;
516}
Neil Boothb93d90e2007-10-12 16:02:31 +0000517
Chris Lattner91702092009-03-12 23:59:55 +0000518/* Zero at the end to avoid modular arithmetic when adding one; used
519 when rounding up during hexadecimal output. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000520static const char hexDigitsLower[] = "0123456789abcdef0";
521static const char hexDigitsUpper[] = "0123456789ABCDEF0";
522static const char infinityL[] = "infinity";
523static const char infinityU[] = "INFINITY";
524static const char NaNL[] = "nan";
525static const char NaNU[] = "NAN";
Neil Boothb93d90e2007-10-12 16:02:31 +0000526
Chris Lattner91702092009-03-12 23:59:55 +0000527/* Write out an integerPart in hexadecimal, starting with the most
528 significant nibble. Write out exactly COUNT hexdigits, return
529 COUNT. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000530static unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000531partAsHex (char *dst, integerPart part, unsigned int count,
532 const char *hexDigitChars)
533{
534 unsigned int result = count;
Neil Boothb93d90e2007-10-12 16:02:31 +0000535
Evan Cheng67c90212009-10-27 21:35:42 +0000536 assert(count != 0 && count <= integerPartWidth / 4);
Neil Boothb93d90e2007-10-12 16:02:31 +0000537
Chris Lattner91702092009-03-12 23:59:55 +0000538 part >>= (integerPartWidth - 4 * count);
539 while (count--) {
540 dst[count] = hexDigitChars[part & 0xf];
541 part >>= 4;
Neil Boothb93d90e2007-10-12 16:02:31 +0000542 }
543
Chris Lattner91702092009-03-12 23:59:55 +0000544 return result;
545}
Neil Booth8f1946f2007-10-03 22:26:02 +0000546
Chris Lattner91702092009-03-12 23:59:55 +0000547/* Write out an unsigned decimal integer. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000548static char *
Chris Lattner91702092009-03-12 23:59:55 +0000549writeUnsignedDecimal (char *dst, unsigned int n)
550{
551 char buff[40], *p;
Neil Booth8f1946f2007-10-03 22:26:02 +0000552
Chris Lattner91702092009-03-12 23:59:55 +0000553 p = buff;
554 do
555 *p++ = '0' + n % 10;
556 while (n /= 10);
Neil Booth8f1946f2007-10-03 22:26:02 +0000557
Chris Lattner91702092009-03-12 23:59:55 +0000558 do
559 *dst++ = *--p;
560 while (p != buff);
Neil Booth8f1946f2007-10-03 22:26:02 +0000561
Chris Lattner91702092009-03-12 23:59:55 +0000562 return dst;
563}
Neil Booth8f1946f2007-10-03 22:26:02 +0000564
Chris Lattner91702092009-03-12 23:59:55 +0000565/* Write out a signed decimal integer. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000566static char *
Chris Lattner91702092009-03-12 23:59:55 +0000567writeSignedDecimal (char *dst, int value)
568{
569 if (value < 0) {
570 *dst++ = '-';
571 dst = writeUnsignedDecimal(dst, -(unsigned) value);
572 } else
573 dst = writeUnsignedDecimal(dst, value);
Neil Booth8f1946f2007-10-03 22:26:02 +0000574
Chris Lattner91702092009-03-12 23:59:55 +0000575 return dst;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000576}
577
578/* Constructors. */
579void
580APFloat::initialize(const fltSemantics *ourSemantics)
581{
582 unsigned int count;
583
584 semantics = ourSemantics;
585 count = partCount();
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000586 if (count > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000587 significand.parts = new integerPart[count];
588}
589
590void
591APFloat::freeSignificand()
592{
Manuel Klimekd0cf5b22013-06-03 13:03:05 +0000593 if (needsCleanup())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000594 delete [] significand.parts;
595}
596
597void
598APFloat::assign(const APFloat &rhs)
599{
600 assert(semantics == rhs.semantics);
601
602 sign = rhs.sign;
603 category = rhs.category;
604 exponent = rhs.exponent;
Michael Gottesman8136c382013-06-26 23:17:28 +0000605 if (isFiniteNonZero() || category == fcNaN)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000606 copySignificand(rhs);
607}
608
609void
610APFloat::copySignificand(const APFloat &rhs)
611{
Michael Gottesman8136c382013-06-26 23:17:28 +0000612 assert(isFiniteNonZero() || category == fcNaN);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000613 assert(rhs.partCount() >= partCount());
614
615 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +0000616 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000617}
618
Neil Booth5fe658b2007-10-14 10:39:51 +0000619/* Make this number a NaN, with an arbitrary but deterministic value
Dale Johannesen1f864982009-01-21 20:32:55 +0000620 for the significand. If double or longer, this is a signalling NaN,
Mike Stump799bf582009-05-30 03:49:43 +0000621 which may not be ideal. If float, this is QNaN(0). */
John McCalldcb9a7a2010-02-28 02:51:25 +0000622void APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill)
Neil Booth5fe658b2007-10-14 10:39:51 +0000623{
624 category = fcNaN;
John McCalldcb9a7a2010-02-28 02:51:25 +0000625 sign = Negative;
626
John McCallc12b1332010-02-28 12:49:50 +0000627 integerPart *significand = significandParts();
628 unsigned numParts = partCount();
629
John McCalldcb9a7a2010-02-28 02:51:25 +0000630 // Set the significand bits to the fill.
John McCallc12b1332010-02-28 12:49:50 +0000631 if (!fill || fill->getNumWords() < numParts)
632 APInt::tcSet(significand, 0, numParts);
633 if (fill) {
John McCallc6dbe302010-03-01 18:38:45 +0000634 APInt::tcAssign(significand, fill->getRawData(),
635 std::min(fill->getNumWords(), numParts));
John McCallc12b1332010-02-28 12:49:50 +0000636
637 // Zero out the excess bits of the significand.
638 unsigned bitsToPreserve = semantics->precision - 1;
639 unsigned part = bitsToPreserve / 64;
640 bitsToPreserve %= 64;
641 significand[part] &= ((1ULL << bitsToPreserve) - 1);
642 for (part++; part != numParts; ++part)
643 significand[part] = 0;
644 }
645
646 unsigned QNaNBit = semantics->precision - 2;
John McCalldcb9a7a2010-02-28 02:51:25 +0000647
648 if (SNaN) {
649 // We always have to clear the QNaN bit to make it an SNaN.
John McCallc12b1332010-02-28 12:49:50 +0000650 APInt::tcClearBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000651
652 // If there are no bits set in the payload, we have to set
653 // *something* to make it a NaN instead of an infinity;
654 // conventionally, this is the next bit down from the QNaN bit.
John McCallc12b1332010-02-28 12:49:50 +0000655 if (APInt::tcIsZero(significand, numParts))
656 APInt::tcSetBit(significand, QNaNBit - 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000657 } else {
658 // We always have to set the QNaN bit to make it a QNaN.
John McCallc12b1332010-02-28 12:49:50 +0000659 APInt::tcSetBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000660 }
John McCallc12b1332010-02-28 12:49:50 +0000661
662 // For x87 extended precision, we want to make a NaN, not a
663 // pseudo-NaN. Maybe we should expose the ability to make
664 // pseudo-NaNs?
665 if (semantics == &APFloat::x87DoubleExtended)
666 APInt::tcSetBit(significand, QNaNBit + 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000667}
668
669APFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
670 const APInt *fill) {
671 APFloat value(Sem, uninitialized);
672 value.makeNaN(SNaN, Negative, fill);
673 return value;
Neil Booth5fe658b2007-10-14 10:39:51 +0000674}
675
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000676APFloat &
677APFloat::operator=(const APFloat &rhs)
678{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000679 if (this != &rhs) {
680 if (semantics != rhs.semantics) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000681 freeSignificand();
682 initialize(rhs.semantics);
683 }
684 assign(rhs);
685 }
686
687 return *this;
688}
689
Benjamin Kramer06f47782014-03-04 20:26:51 +0000690APFloat &
691APFloat::operator=(APFloat &&rhs) {
692 freeSignificand();
693
694 semantics = rhs.semantics;
695 significand = rhs.significand;
696 exponent = rhs.exponent;
697 category = rhs.category;
698 sign = rhs.sign;
699
700 rhs.semantics = &Bogus;
701 return *this;
702}
703
Dale Johannesena719a602007-08-24 00:56:33 +0000704bool
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000705APFloat::isDenormal() const {
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000706 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000707 (APInt::tcExtractBit(significandParts(),
708 semantics->precision - 1) == 0);
709}
710
711bool
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000712APFloat::isSmallest() const {
713 // The smallest number by magnitude in our format will be the smallest
Michael Gottesmana7cc1242013-06-19 07:34:21 +0000714 // denormal, i.e. the floating point number with exponent being minimum
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000715 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000716 return isFiniteNonZero() && exponent == semantics->minExponent &&
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000717 significandMSB() == 0;
718}
719
720bool APFloat::isSignificandAllOnes() const {
721 // Test if the significand excluding the integral bit is all ones. This allows
722 // us to test for binade boundaries.
723 const integerPart *Parts = significandParts();
724 const unsigned PartCount = partCount();
725 for (unsigned i = 0; i < PartCount - 1; i++)
726 if (~Parts[i])
727 return false;
728
729 // Set the unused high bits to all ones when we compare.
730 const unsigned NumHighBits =
731 PartCount*integerPartWidth - semantics->precision + 1;
732 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
733 "fill than integerPartWidth");
734 const integerPart HighBitFill =
735 ~integerPart(0) << (integerPartWidth - NumHighBits);
736 if (~(Parts[PartCount - 1] | HighBitFill))
737 return false;
738
739 return true;
740}
741
742bool APFloat::isSignificandAllZeros() const {
743 // Test if the significand excluding the integral bit is all zeros. This
744 // allows us to test for binade boundaries.
745 const integerPart *Parts = significandParts();
746 const unsigned PartCount = partCount();
747
748 for (unsigned i = 0; i < PartCount - 1; i++)
749 if (Parts[i])
750 return false;
751
752 const unsigned NumHighBits =
753 PartCount*integerPartWidth - semantics->precision + 1;
754 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
755 "clear than integerPartWidth");
756 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
757
758 if (Parts[PartCount - 1] & HighBitMask)
759 return false;
760
761 return true;
762}
763
764bool
765APFloat::isLargest() const {
766 // The largest number by magnitude in our format will be the floating point
767 // number with maximum exponent and with significand that is all ones.
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000768 return isFiniteNonZero() && exponent == semantics->maxExponent
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000769 && isSignificandAllOnes();
770}
771
772bool
Stephen Canon1bfc89b2015-11-16 21:52:48 +0000773APFloat::isInteger() const {
774 // This could be made more efficient; I'm going for obviously correct.
775 if (!isFinite()) return false;
776 APFloat truncated = *this;
777 truncated.roundToIntegral(rmTowardZero);
778 return compare(truncated) == cmpEqual;
779}
780
781bool
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000782APFloat::bitwiseIsEqual(const APFloat &rhs) const {
Dale Johannesena719a602007-08-24 00:56:33 +0000783 if (this == &rhs)
784 return true;
785 if (semantics != rhs.semantics ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000786 category != rhs.category ||
787 sign != rhs.sign)
Dale Johannesena719a602007-08-24 00:56:33 +0000788 return false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000789 if (category==fcZero || category==fcInfinity)
Dale Johannesena719a602007-08-24 00:56:33 +0000790 return true;
Benjamin Kramer103fc942015-08-21 16:44:52 +0000791
792 if (isFiniteNonZero() && exponent != rhs.exponent)
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000793 return false;
Benjamin Kramer103fc942015-08-21 16:44:52 +0000794
795 return std::equal(significandParts(), significandParts() + partCount(),
796 rhs.significandParts());
Dale Johannesena719a602007-08-24 00:56:33 +0000797}
798
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000799APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000800 initialize(&ourSemantics);
801 sign = 0;
Michael Gottesman30a90eb2013-07-27 21:49:21 +0000802 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000803 zeroSignificand();
804 exponent = ourSemantics.precision - 1;
805 significandParts()[0] = value;
806 normalize(rmNearestTiesToEven, lfExactlyZero);
807}
808
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000809APFloat::APFloat(const fltSemantics &ourSemantics) {
Chris Lattnerac6271e2009-09-17 01:08:43 +0000810 initialize(&ourSemantics);
811 category = fcZero;
812 sign = false;
813}
814
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000815APFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
John McCalldcb9a7a2010-02-28 02:51:25 +0000816 // Allocates storage if necessary but does not initialize it.
817 initialize(&ourSemantics);
818}
Chris Lattnerac6271e2009-09-17 01:08:43 +0000819
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000820APFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000821 initialize(&ourSemantics);
822 convertFromString(text, rmNearestTiesToEven);
823}
824
Ulrich Weigande1d62f92012-10-29 18:17:42 +0000825APFloat::APFloat(const APFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000826 initialize(rhs.semantics);
827 assign(rhs);
828}
829
Benjamin Kramer06f47782014-03-04 20:26:51 +0000830APFloat::APFloat(APFloat &&rhs) : semantics(&Bogus) {
831 *this = std::move(rhs);
832}
833
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000834APFloat::~APFloat()
835{
836 freeSignificand();
837}
838
Ted Kremenek6f30a072008-02-11 17:24:50 +0000839// Profile - This method 'profiles' an APFloat for use with FoldingSet.
840void APFloat::Profile(FoldingSetNodeID& ID) const {
Dale Johannesen54306fe2008-10-09 18:53:47 +0000841 ID.Add(bitcastToAPInt());
Ted Kremenek6f30a072008-02-11 17:24:50 +0000842}
843
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000844unsigned int
845APFloat::partCount() const
846{
Dale Johannesen146a0ea2007-09-20 23:47:58 +0000847 return partCountForBits(semantics->precision + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000848}
849
850unsigned int
851APFloat::semanticsPrecision(const fltSemantics &semantics)
852{
853 return semantics.precision;
854}
JF Bastiena1d3c242015-08-26 02:32:45 +0000855APFloat::ExponentType
856APFloat::semanticsMaxExponent(const fltSemantics &semantics)
857{
858 return semantics.maxExponent;
859}
860APFloat::ExponentType
861APFloat::semanticsMinExponent(const fltSemantics &semantics)
862{
863 return semantics.minExponent;
864}
865unsigned int
866APFloat::semanticsSizeInBits(const fltSemantics &semantics)
867{
868 return semantics.sizeInBits;
869}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000870
871const integerPart *
872APFloat::significandParts() const
873{
874 return const_cast<APFloat *>(this)->significandParts();
875}
876
877integerPart *
878APFloat::significandParts()
879{
Evan Cheng67c90212009-10-27 21:35:42 +0000880 if (partCount() > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000881 return significand.parts;
882 else
883 return &significand.part;
884}
885
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000886void
887APFloat::zeroSignificand()
888{
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000889 APInt::tcSet(significandParts(), 0, partCount());
890}
891
892/* Increment an fcNormal floating point number's significand. */
893void
894APFloat::incrementSignificand()
895{
896 integerPart carry;
897
898 carry = APInt::tcIncrement(significandParts(), partCount());
899
900 /* Our callers should never cause us to overflow. */
901 assert(carry == 0);
Duncan Sandsa41634e2011-08-12 14:54:45 +0000902 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000903}
904
905/* Add the significand of the RHS. Returns the carry flag. */
906integerPart
907APFloat::addSignificand(const APFloat &rhs)
908{
909 integerPart *parts;
910
911 parts = significandParts();
912
913 assert(semantics == rhs.semantics);
914 assert(exponent == rhs.exponent);
915
916 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
917}
918
919/* Subtract the significand of the RHS with a borrow flag. Returns
920 the borrow flag. */
921integerPart
922APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
923{
924 integerPart *parts;
925
926 parts = significandParts();
927
928 assert(semantics == rhs.semantics);
929 assert(exponent == rhs.exponent);
930
931 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth9acbf5a2007-09-26 21:33:42 +0000932 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000933}
934
935/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
936 on to the full-precision result of the multiplication. Returns the
937 lost fraction. */
938lostFraction
939APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
940{
Neil Booth9acbf5a2007-09-26 21:33:42 +0000941 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000942 unsigned int partsCount, newPartsCount, precision;
943 integerPart *lhsSignificand;
944 integerPart scratch[4];
945 integerPart *fullSignificand;
946 lostFraction lost_fraction;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000947 bool ignored;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000948
949 assert(semantics == rhs.semantics);
950
951 precision = semantics->precision;
Lang Hames56c0eb22014-11-19 19:15:41 +0000952
953 // Allocate space for twice as many bits as the original significand, plus one
954 // extra bit for the addition to overflow into.
955 newPartsCount = partCountForBits(precision * 2 + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000956
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000957 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000958 fullSignificand = new integerPart[newPartsCount];
959 else
960 fullSignificand = scratch;
961
962 lhsSignificand = significandParts();
963 partsCount = partCount();
964
965 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth0ea72a92007-10-06 00:24:48 +0000966 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000967
968 lost_fraction = lfExactlyZero;
969 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
970 exponent += rhs.exponent;
971
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000972 // Assume the operands involved in the multiplication are single-precision
973 // FP, and the two multiplicants are:
974 // *this = a23 . a22 ... a0 * 2^e1
975 // rhs = b23 . b22 ... b0 * 2^e2
976 // the result of multiplication is:
Lang Hames56c0eb22014-11-19 19:15:41 +0000977 // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2)
978 // Note that there are three significant bits at the left-hand side of the
979 // radix point: two for the multiplication, and an overflow bit for the
980 // addition (that will always be zero at this point). Move the radix point
981 // toward left by two bits, and adjust exponent accordingly.
982 exponent += 2;
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000983
Hal Finkel171c2ec2014-10-14 19:23:07 +0000984 if (addend && addend->isNonZero()) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000985 // The intermediate result of the multiplication has "2 * precision"
986 // signicant bit; adjust the addend to be consistent with mul result.
987 //
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000988 Significand savedSignificand = significand;
989 const fltSemantics *savedSemantics = semantics;
990 fltSemantics extendedSemantics;
991 opStatus status;
992 unsigned int extendedPrecision;
993
Lang Hames56c0eb22014-11-19 19:15:41 +0000994 // Normalize our MSB to one below the top bit to allow for overflow.
995 extendedPrecision = 2 * precision + 1;
996 if (omsb != extendedPrecision - 1) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000997 assert(extendedPrecision > omsb);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000998 APInt::tcShiftLeft(fullSignificand, newPartsCount,
Lang Hames56c0eb22014-11-19 19:15:41 +0000999 (extendedPrecision - 1) - omsb);
1000 exponent -= (extendedPrecision - 1) - omsb;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001001 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001002
1003 /* Create new semantics. */
1004 extendedSemantics = *semantics;
1005 extendedSemantics.precision = extendedPrecision;
1006
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001007 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001008 significand.part = fullSignificand[0];
1009 else
1010 significand.parts = fullSignificand;
1011 semantics = &extendedSemantics;
1012
1013 APFloat extendedAddend(*addend);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001014 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001015 assert(status == opOK);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001016 (void)status;
Lang Hames56c0eb22014-11-19 19:15:41 +00001017
1018 // Shift the significand of the addend right by one bit. This guarantees
1019 // that the high bit of the significand is zero (same as fullSignificand),
1020 // so the addition will overflow (if it does overflow at all) into the top bit.
1021 lost_fraction = extendedAddend.shiftSignificandRight(1);
1022 assert(lost_fraction == lfExactlyZero &&
1023 "Lost precision while shifting addend for fused-multiply-add.");
1024
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001025 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1026
1027 /* Restore our state. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001028 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001029 fullSignificand[0] = significand.part;
1030 significand = savedSignificand;
1031 semantics = savedSemantics;
1032
1033 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1034 }
1035
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001036 // Convert the result having "2 * precision" significant-bits back to the one
1037 // having "precision" significant-bits. First, move the radix point from
1038 // poision "2*precision - 1" to "precision - 1". The exponent need to be
1039 // adjusted by "2*precision - 1" - "precision - 1" = "precision".
Lang Hames56c0eb22014-11-19 19:15:41 +00001040 exponent -= precision + 1;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001041
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001042 // In case MSB resides at the left-hand side of radix point, shift the
1043 // mantissa right by some amount to make sure the MSB reside right before
1044 // the radix point (i.e. "MSB . rest-significant-bits").
1045 //
1046 // Note that the result is not normalized when "omsb < precision". So, the
1047 // caller needs to call APFloat::normalize() if normalized value is expected.
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001048 if (omsb > precision) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001049 unsigned int bits, significantParts;
1050 lostFraction lf;
1051
1052 bits = omsb - precision;
1053 significantParts = partCountForBits(omsb);
1054 lf = shiftRight(fullSignificand, significantParts, bits);
1055 lost_fraction = combineLostFractions(lf, lost_fraction);
1056 exponent += bits;
1057 }
1058
1059 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1060
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001061 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001062 delete [] fullSignificand;
1063
1064 return lost_fraction;
1065}
1066
1067/* Multiply the significands of LHS and RHS to DST. */
1068lostFraction
1069APFloat::divideSignificand(const APFloat &rhs)
1070{
1071 unsigned int bit, i, partsCount;
1072 const integerPart *rhsSignificand;
1073 integerPart *lhsSignificand, *dividend, *divisor;
1074 integerPart scratch[4];
1075 lostFraction lost_fraction;
1076
1077 assert(semantics == rhs.semantics);
1078
1079 lhsSignificand = significandParts();
1080 rhsSignificand = rhs.significandParts();
1081 partsCount = partCount();
1082
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001083 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001084 dividend = new integerPart[partsCount * 2];
1085 else
1086 dividend = scratch;
1087
1088 divisor = dividend + partsCount;
1089
1090 /* Copy the dividend and divisor as they will be modified in-place. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001091 for (i = 0; i < partsCount; i++) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001092 dividend[i] = lhsSignificand[i];
1093 divisor[i] = rhsSignificand[i];
1094 lhsSignificand[i] = 0;
1095 }
1096
1097 exponent -= rhs.exponent;
1098
1099 unsigned int precision = semantics->precision;
1100
1101 /* Normalize the divisor. */
1102 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001103 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001104 exponent += bit;
1105 APInt::tcShiftLeft(divisor, partsCount, bit);
1106 }
1107
1108 /* Normalize the dividend. */
1109 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001110 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001111 exponent -= bit;
1112 APInt::tcShiftLeft(dividend, partsCount, bit);
1113 }
1114
Neil Boothb93d90e2007-10-12 16:02:31 +00001115 /* Ensure the dividend >= divisor initially for the loop below.
1116 Incidentally, this means that the division loop below is
1117 guaranteed to set the integer bit to one. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001118 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001119 exponent--;
1120 APInt::tcShiftLeft(dividend, partsCount, 1);
1121 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
1122 }
1123
1124 /* Long division. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001125 for (bit = precision; bit; bit -= 1) {
1126 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001127 APInt::tcSubtract(dividend, divisor, 0, partsCount);
1128 APInt::tcSetBit(lhsSignificand, bit - 1);
1129 }
1130
1131 APInt::tcShiftLeft(dividend, partsCount, 1);
1132 }
1133
1134 /* Figure out the lost fraction. */
1135 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1136
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001137 if (cmp > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001138 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001139 else if (cmp == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001140 lost_fraction = lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001141 else if (APInt::tcIsZero(dividend, partsCount))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001142 lost_fraction = lfExactlyZero;
1143 else
1144 lost_fraction = lfLessThanHalf;
1145
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001146 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001147 delete [] dividend;
1148
1149 return lost_fraction;
1150}
1151
1152unsigned int
1153APFloat::significandMSB() const
1154{
1155 return APInt::tcMSB(significandParts(), partCount());
1156}
1157
1158unsigned int
1159APFloat::significandLSB() const
1160{
1161 return APInt::tcLSB(significandParts(), partCount());
1162}
1163
1164/* Note that a zero result is NOT normalized to fcZero. */
1165lostFraction
1166APFloat::shiftSignificandRight(unsigned int bits)
1167{
1168 /* Our exponent should not overflow. */
Michael Gottesman9dc98332013-06-24 04:06:23 +00001169 assert((ExponentType) (exponent + bits) >= exponent);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001170
1171 exponent += bits;
1172
1173 return shiftRight(significandParts(), partCount(), bits);
1174}
1175
1176/* Shift the significand left BITS bits, subtract BITS from its exponent. */
1177void
1178APFloat::shiftSignificandLeft(unsigned int bits)
1179{
1180 assert(bits < semantics->precision);
1181
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001182 if (bits) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001183 unsigned int partsCount = partCount();
1184
1185 APInt::tcShiftLeft(significandParts(), partsCount, bits);
1186 exponent -= bits;
1187
1188 assert(!APInt::tcIsZero(significandParts(), partsCount));
1189 }
1190}
1191
1192APFloat::cmpResult
1193APFloat::compareAbsoluteValue(const APFloat &rhs) const
1194{
1195 int compare;
1196
1197 assert(semantics == rhs.semantics);
Michael Gottesman8136c382013-06-26 23:17:28 +00001198 assert(isFiniteNonZero());
1199 assert(rhs.isFiniteNonZero());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001200
1201 compare = exponent - rhs.exponent;
1202
1203 /* If exponents are equal, do an unsigned bignum comparison of the
1204 significands. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001205 if (compare == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001206 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001207 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001208
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001209 if (compare > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001210 return cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001211 else if (compare < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001212 return cmpLessThan;
1213 else
1214 return cmpEqual;
1215}
1216
1217/* Handle overflow. Sign is preserved. We either become infinity or
1218 the largest finite number. */
1219APFloat::opStatus
1220APFloat::handleOverflow(roundingMode rounding_mode)
1221{
1222 /* Infinity? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001223 if (rounding_mode == rmNearestTiesToEven ||
1224 rounding_mode == rmNearestTiesToAway ||
1225 (rounding_mode == rmTowardPositive && !sign) ||
1226 (rounding_mode == rmTowardNegative && sign)) {
1227 category = fcInfinity;
1228 return (opStatus) (opOverflow | opInexact);
1229 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001230
1231 /* Otherwise we become the largest finite number. */
1232 category = fcNormal;
1233 exponent = semantics->maxExponent;
1234 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001235 semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001236
1237 return opInexact;
1238}
1239
Neil Booth1ca1f802007-10-03 15:16:41 +00001240/* Returns TRUE if, when truncating the current number, with BIT the
1241 new LSB, with the given lost fraction and rounding mode, the result
1242 would need to be rounded away from zero (i.e., by increasing the
1243 signficand). This routine must work for fcZero of both signs, and
1244 fcNormal numbers. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001245bool
1246APFloat::roundAwayFromZero(roundingMode rounding_mode,
Neil Booth1ca1f802007-10-03 15:16:41 +00001247 lostFraction lost_fraction,
1248 unsigned int bit) const
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001249{
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001250 /* NaNs and infinities should not have lost fractions. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001251 assert(isFiniteNonZero() || category == fcZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001252
Neil Booth1ca1f802007-10-03 15:16:41 +00001253 /* Current callers never pass this so we don't handle it. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001254 assert(lost_fraction != lfExactlyZero);
1255
Mike Stump889285d2009-05-13 23:23:20 +00001256 switch (rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001257 case rmNearestTiesToAway:
1258 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1259
1260 case rmNearestTiesToEven:
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001261 if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001262 return true;
1263
1264 /* Our zeroes don't have a significand to test. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001265 if (lost_fraction == lfExactlyHalf && category != fcZero)
Neil Booth1ca1f802007-10-03 15:16:41 +00001266 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001267
1268 return false;
1269
1270 case rmTowardZero:
1271 return false;
1272
1273 case rmTowardPositive:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001274 return !sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001275
1276 case rmTowardNegative:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001277 return sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001278 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00001279 llvm_unreachable("Invalid rounding mode found");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001280}
1281
1282APFloat::opStatus
1283APFloat::normalize(roundingMode rounding_mode,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001284 lostFraction lost_fraction)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001285{
Neil Booth9acbf5a2007-09-26 21:33:42 +00001286 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001287 int exponentChange;
1288
Michael Gottesman8136c382013-06-26 23:17:28 +00001289 if (!isFiniteNonZero())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001290 return opOK;
1291
1292 /* Before rounding normalize the exponent of fcNormal numbers. */
1293 omsb = significandMSB() + 1;
1294
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001295 if (omsb) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001296 /* OMSB is numbered from 1. We want to place it in the integer
Nick Lewyckyf66daac2011-10-03 21:30:08 +00001297 bit numbered PRECISION if possible, with a compensating change in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001298 the exponent. */
1299 exponentChange = omsb - semantics->precision;
1300
1301 /* If the resulting exponent is too high, overflow according to
1302 the rounding mode. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001303 if (exponent + exponentChange > semantics->maxExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001304 return handleOverflow(rounding_mode);
1305
1306 /* Subnormal numbers have exponent minExponent, and their MSB
1307 is forced based on that. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001308 if (exponent + exponentChange < semantics->minExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001309 exponentChange = semantics->minExponent - exponent;
1310
1311 /* Shifting left is easy as we don't lose precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001312 if (exponentChange < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001313 assert(lost_fraction == lfExactlyZero);
1314
1315 shiftSignificandLeft(-exponentChange);
1316
1317 return opOK;
1318 }
1319
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001320 if (exponentChange > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001321 lostFraction lf;
1322
1323 /* Shift right and capture any new lost fraction. */
1324 lf = shiftSignificandRight(exponentChange);
1325
1326 lost_fraction = combineLostFractions(lf, lost_fraction);
1327
1328 /* Keep OMSB up-to-date. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001329 if (omsb > (unsigned) exponentChange)
Neil Boothb93d90e2007-10-12 16:02:31 +00001330 omsb -= exponentChange;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001331 else
Neil Booth9acbf5a2007-09-26 21:33:42 +00001332 omsb = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001333 }
1334 }
1335
1336 /* Now round the number according to rounding_mode given the lost
1337 fraction. */
1338
1339 /* As specified in IEEE 754, since we do not trap we do not report
1340 underflow for exact results. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001341 if (lost_fraction == lfExactlyZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001342 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001343 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001344 category = fcZero;
1345
1346 return opOK;
1347 }
1348
1349 /* Increment the significand if we're rounding away from zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001350 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1351 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001352 exponent = semantics->minExponent;
1353
1354 incrementSignificand();
1355 omsb = significandMSB() + 1;
1356
1357 /* Did the significand increment overflow? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001358 if (omsb == (unsigned) semantics->precision + 1) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001359 /* Renormalize by incrementing the exponent and shifting our
Neil Booth9acbf5a2007-09-26 21:33:42 +00001360 significand right one. However if we already have the
1361 maximum exponent we overflow to infinity. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001362 if (exponent == semantics->maxExponent) {
Neil Booth9acbf5a2007-09-26 21:33:42 +00001363 category = fcInfinity;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001364
Neil Booth9acbf5a2007-09-26 21:33:42 +00001365 return (opStatus) (opOverflow | opInexact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001366 }
1367
1368 shiftSignificandRight(1);
1369
1370 return opInexact;
1371 }
1372 }
1373
1374 /* The normal case - we were and are not denormal, and any
1375 significand increment above didn't overflow. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001376 if (omsb == semantics->precision)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001377 return opInexact;
1378
1379 /* We have a non-zero denormal. */
1380 assert(omsb < semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001381
1382 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001383 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001384 category = fcZero;
1385
1386 /* The fcZero case is a denormal that underflowed to zero. */
1387 return (opStatus) (opUnderflow | opInexact);
1388}
1389
1390APFloat::opStatus
1391APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1392{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001393 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001394 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001395 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001396
Michael Gottesman9b877e12013-06-24 09:57:57 +00001397 case PackCategoriesIntoKey(fcNaN, fcZero):
1398 case PackCategoriesIntoKey(fcNaN, fcNormal):
1399 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1400 case PackCategoriesIntoKey(fcNaN, fcNaN):
1401 case PackCategoriesIntoKey(fcNormal, fcZero):
1402 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1403 case PackCategoriesIntoKey(fcInfinity, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001404 return opOK;
1405
Michael Gottesman9b877e12013-06-24 09:57:57 +00001406 case PackCategoriesIntoKey(fcZero, fcNaN):
1407 case PackCategoriesIntoKey(fcNormal, fcNaN):
1408 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Stephen Canond3278282014-06-08 16:53:31 +00001409 // We need to be sure to flip the sign here for subtraction because we
1410 // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1411 sign = rhs.sign ^ subtract;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001412 category = fcNaN;
1413 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001414 return opOK;
1415
Michael Gottesman9b877e12013-06-24 09:57:57 +00001416 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1417 case PackCategoriesIntoKey(fcZero, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001418 category = fcInfinity;
1419 sign = rhs.sign ^ subtract;
1420 return opOK;
1421
Michael Gottesman9b877e12013-06-24 09:57:57 +00001422 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001423 assign(rhs);
1424 sign = rhs.sign ^ subtract;
1425 return opOK;
1426
Michael Gottesman9b877e12013-06-24 09:57:57 +00001427 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001428 /* Sign depends on rounding mode; handled by caller. */
1429 return opOK;
1430
Michael Gottesman9b877e12013-06-24 09:57:57 +00001431 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001432 /* Differently signed infinities can only be validly
1433 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001434 if (((sign ^ rhs.sign)!=0) != subtract) {
Neil Booth5fe658b2007-10-14 10:39:51 +00001435 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001436 return opInvalidOp;
1437 }
1438
1439 return opOK;
1440
Michael Gottesman9b877e12013-06-24 09:57:57 +00001441 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001442 return opDivByZero;
1443 }
1444}
1445
1446/* Add or subtract two normal numbers. */
1447lostFraction
1448APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1449{
1450 integerPart carry;
1451 lostFraction lost_fraction;
1452 int bits;
1453
1454 /* Determine if the operation on the absolute values is effectively
1455 an addition or subtraction. */
Aaron Ballmand5cc45f2015-03-24 12:47:51 +00001456 subtract ^= static_cast<bool>(sign ^ rhs.sign);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001457
1458 /* Are we bigger exponent-wise than the RHS? */
1459 bits = exponent - rhs.exponent;
1460
1461 /* Subtraction is more subtle than one might naively expect. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001462 if (subtract) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001463 APFloat temp_rhs(rhs);
1464 bool reverse;
1465
Chris Lattner3da18eb2007-08-24 03:02:34 +00001466 if (bits == 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001467 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1468 lost_fraction = lfExactlyZero;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001469 } else if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001470 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1471 shiftSignificandLeft(1);
1472 reverse = false;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001473 } else {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001474 lost_fraction = shiftSignificandRight(-bits - 1);
1475 temp_rhs.shiftSignificandLeft(1);
1476 reverse = true;
1477 }
1478
Chris Lattner3da18eb2007-08-24 03:02:34 +00001479 if (reverse) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001480 carry = temp_rhs.subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001481 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001482 copySignificand(temp_rhs);
1483 sign = !sign;
1484 } else {
1485 carry = subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001486 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001487 }
1488
1489 /* Invert the lost fraction - it was on the RHS and
1490 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001491 if (lost_fraction == lfLessThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001492 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001493 else if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001494 lost_fraction = lfLessThanHalf;
1495
1496 /* The code above is intended to ensure that no borrow is
1497 necessary. */
1498 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001499 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001500 } else {
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001501 if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001502 APFloat temp_rhs(rhs);
1503
1504 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1505 carry = addSignificand(temp_rhs);
1506 } else {
1507 lost_fraction = shiftSignificandRight(-bits);
1508 carry = addSignificand(rhs);
1509 }
1510
1511 /* We have a guard bit; generating a carry cannot happen. */
1512 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001513 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001514 }
1515
1516 return lost_fraction;
1517}
1518
1519APFloat::opStatus
1520APFloat::multiplySpecials(const APFloat &rhs)
1521{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001522 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001523 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001524 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001525
Michael Gottesman9b877e12013-06-24 09:57:57 +00001526 case PackCategoriesIntoKey(fcNaN, fcZero):
1527 case PackCategoriesIntoKey(fcNaN, fcNormal):
1528 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1529 case PackCategoriesIntoKey(fcNaN, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001530 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001531 return opOK;
1532
Michael Gottesman9b877e12013-06-24 09:57:57 +00001533 case PackCategoriesIntoKey(fcZero, fcNaN):
1534 case PackCategoriesIntoKey(fcNormal, fcNaN):
1535 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001536 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001537 category = fcNaN;
1538 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001539 return opOK;
1540
Michael Gottesman9b877e12013-06-24 09:57:57 +00001541 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1542 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1543 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001544 category = fcInfinity;
1545 return opOK;
1546
Michael Gottesman9b877e12013-06-24 09:57:57 +00001547 case PackCategoriesIntoKey(fcZero, fcNormal):
1548 case PackCategoriesIntoKey(fcNormal, fcZero):
1549 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001550 category = fcZero;
1551 return opOK;
1552
Michael Gottesman9b877e12013-06-24 09:57:57 +00001553 case PackCategoriesIntoKey(fcZero, fcInfinity):
1554 case PackCategoriesIntoKey(fcInfinity, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001555 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001556 return opInvalidOp;
1557
Michael Gottesman9b877e12013-06-24 09:57:57 +00001558 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001559 return opOK;
1560 }
1561}
1562
1563APFloat::opStatus
1564APFloat::divideSpecials(const APFloat &rhs)
1565{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001566 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001567 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001568 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001569
Michael Gottesman9b877e12013-06-24 09:57:57 +00001570 case PackCategoriesIntoKey(fcZero, fcNaN):
1571 case PackCategoriesIntoKey(fcNormal, fcNaN):
1572 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001573 category = fcNaN;
1574 copySignificand(rhs);
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001575 case PackCategoriesIntoKey(fcNaN, fcZero):
1576 case PackCategoriesIntoKey(fcNaN, fcNormal):
1577 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1578 case PackCategoriesIntoKey(fcNaN, fcNaN):
1579 sign = false;
1580 case PackCategoriesIntoKey(fcInfinity, fcZero):
1581 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1582 case PackCategoriesIntoKey(fcZero, fcInfinity):
1583 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001584 return opOK;
1585
Michael Gottesman9b877e12013-06-24 09:57:57 +00001586 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001587 category = fcZero;
1588 return opOK;
1589
Michael Gottesman9b877e12013-06-24 09:57:57 +00001590 case PackCategoriesIntoKey(fcNormal, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001591 category = fcInfinity;
1592 return opDivByZero;
1593
Michael Gottesman9b877e12013-06-24 09:57:57 +00001594 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1595 case PackCategoriesIntoKey(fcZero, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001596 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001597 return opInvalidOp;
1598
Michael Gottesman9b877e12013-06-24 09:57:57 +00001599 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001600 return opOK;
1601 }
1602}
1603
Dale Johannesenb5721632009-01-21 00:35:19 +00001604APFloat::opStatus
1605APFloat::modSpecials(const APFloat &rhs)
1606{
Michael Gottesman9b877e12013-06-24 09:57:57 +00001607 switch (PackCategoriesIntoKey(category, rhs.category)) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001608 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001609 llvm_unreachable(nullptr);
Dale Johannesenb5721632009-01-21 00:35:19 +00001610
Michael Gottesman9b877e12013-06-24 09:57:57 +00001611 case PackCategoriesIntoKey(fcNaN, fcZero):
1612 case PackCategoriesIntoKey(fcNaN, fcNormal):
1613 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1614 case PackCategoriesIntoKey(fcNaN, fcNaN):
1615 case PackCategoriesIntoKey(fcZero, fcInfinity):
1616 case PackCategoriesIntoKey(fcZero, fcNormal):
1617 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Dale Johannesenb5721632009-01-21 00:35:19 +00001618 return opOK;
1619
Michael Gottesman9b877e12013-06-24 09:57:57 +00001620 case PackCategoriesIntoKey(fcZero, fcNaN):
1621 case PackCategoriesIntoKey(fcNormal, fcNaN):
1622 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001623 sign = false;
Dale Johannesenb5721632009-01-21 00:35:19 +00001624 category = fcNaN;
1625 copySignificand(rhs);
1626 return opOK;
1627
Michael Gottesman9b877e12013-06-24 09:57:57 +00001628 case PackCategoriesIntoKey(fcNormal, fcZero):
1629 case PackCategoriesIntoKey(fcInfinity, fcZero):
1630 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1631 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1632 case PackCategoriesIntoKey(fcZero, fcZero):
Dale Johannesenb5721632009-01-21 00:35:19 +00001633 makeNaN();
1634 return opInvalidOp;
1635
Michael Gottesman9b877e12013-06-24 09:57:57 +00001636 case PackCategoriesIntoKey(fcNormal, fcNormal):
Dale Johannesenb5721632009-01-21 00:35:19 +00001637 return opOK;
1638 }
1639}
1640
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001641/* Change sign. */
1642void
1643APFloat::changeSign()
1644{
1645 /* Look mummy, this one's easy. */
1646 sign = !sign;
1647}
1648
Dale Johannesen689d17d2007-08-31 23:35:31 +00001649void
1650APFloat::clearSign()
1651{
1652 /* So is this one. */
1653 sign = 0;
1654}
1655
1656void
1657APFloat::copySign(const APFloat &rhs)
1658{
1659 /* And this one. */
1660 sign = rhs.sign;
1661}
1662
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001663/* Normalized addition or subtraction. */
1664APFloat::opStatus
1665APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001666 bool subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001667{
1668 opStatus fs;
1669
1670 fs = addOrSubtractSpecials(rhs, subtract);
1671
1672 /* This return code means it was not a simple case. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001673 if (fs == opDivByZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001674 lostFraction lost_fraction;
1675
1676 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1677 fs = normalize(rounding_mode, lost_fraction);
1678
1679 /* Can only be zero if we lost no fraction. */
1680 assert(category != fcZero || lost_fraction == lfExactlyZero);
1681 }
1682
1683 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1684 positive zero unless rounding to minus infinity, except that
1685 adding two like-signed zeroes gives that zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001686 if (category == fcZero) {
1687 if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001688 sign = (rounding_mode == rmTowardNegative);
1689 }
1690
1691 return fs;
1692}
1693
1694/* Normalized addition. */
1695APFloat::opStatus
1696APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1697{
1698 return addOrSubtract(rhs, rounding_mode, false);
1699}
1700
1701/* Normalized subtraction. */
1702APFloat::opStatus
1703APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1704{
1705 return addOrSubtract(rhs, rounding_mode, true);
1706}
1707
1708/* Normalized multiply. */
1709APFloat::opStatus
1710APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1711{
1712 opStatus fs;
1713
1714 sign ^= rhs.sign;
1715 fs = multiplySpecials(rhs);
1716
Michael Gottesman8136c382013-06-26 23:17:28 +00001717 if (isFiniteNonZero()) {
Craig Topperc10719f2014-04-07 04:17:22 +00001718 lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001719 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001720 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001721 fs = (opStatus) (fs | opInexact);
1722 }
1723
1724 return fs;
1725}
1726
1727/* Normalized divide. */
1728APFloat::opStatus
1729APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1730{
1731 opStatus fs;
1732
1733 sign ^= rhs.sign;
1734 fs = divideSpecials(rhs);
1735
Michael Gottesman8136c382013-06-26 23:17:28 +00001736 if (isFiniteNonZero()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001737 lostFraction lost_fraction = divideSignificand(rhs);
1738 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001739 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001740 fs = (opStatus) (fs | opInexact);
1741 }
1742
1743 return fs;
1744}
1745
Dale Johannesenfe750172009-01-20 18:35:05 +00001746/* Normalized remainder. This is not currently correct in all cases. */
1747APFloat::opStatus
1748APFloat::remainder(const APFloat &rhs)
1749{
1750 opStatus fs;
1751 APFloat V = *this;
1752 unsigned int origSign = sign;
1753
Dale Johannesenfe750172009-01-20 18:35:05 +00001754 fs = V.divide(rhs, rmNearestTiesToEven);
1755 if (fs == opDivByZero)
1756 return fs;
1757
1758 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001759 integerPart *x = new integerPart[parts];
Dale Johannesenfe750172009-01-20 18:35:05 +00001760 bool ignored;
1761 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1762 rmNearestTiesToEven, &ignored);
1763 if (fs==opInvalidOp)
1764 return fs;
1765
1766 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1767 rmNearestTiesToEven);
1768 assert(fs==opOK); // should always work
1769
1770 fs = V.multiply(rhs, rmNearestTiesToEven);
1771 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1772
1773 fs = subtract(V, rmNearestTiesToEven);
1774 assert(fs==opOK || fs==opInexact); // likewise
1775
1776 if (isZero())
1777 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001778 delete[] x;
Dale Johannesenfe750172009-01-20 18:35:05 +00001779 return fs;
1780}
1781
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001782/* Normalized llvm frem (C fmod).
Dale Johannesenfe750172009-01-20 18:35:05 +00001783 This is not currently correct in all cases. */
Dale Johannesen689d17d2007-08-31 23:35:31 +00001784APFloat::opStatus
Stephen Canonb12db0e2015-09-21 19:29:25 +00001785APFloat::mod(const APFloat &rhs)
Dale Johannesen689d17d2007-08-31 23:35:31 +00001786{
1787 opStatus fs;
Dale Johannesenb5721632009-01-21 00:35:19 +00001788 fs = modSpecials(rhs);
Dale Johannesen689d17d2007-08-31 23:35:31 +00001789
Michael Gottesman8136c382013-06-26 23:17:28 +00001790 if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001791 APFloat V = *this;
1792 unsigned int origSign = sign;
Dale Johannesen689d17d2007-08-31 23:35:31 +00001793
Dale Johannesenb5721632009-01-21 00:35:19 +00001794 fs = V.divide(rhs, rmNearestTiesToEven);
1795 if (fs == opDivByZero)
1796 return fs;
Dale Johannesen728687c2007-09-05 20:39:49 +00001797
Dale Johannesenb5721632009-01-21 00:35:19 +00001798 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001799 integerPart *x = new integerPart[parts];
Dale Johannesenb5721632009-01-21 00:35:19 +00001800 bool ignored;
1801 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1802 rmTowardZero, &ignored);
1803 if (fs==opInvalidOp)
1804 return fs;
Dale Johannesen728687c2007-09-05 20:39:49 +00001805
Dale Johannesenb5721632009-01-21 00:35:19 +00001806 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1807 rmNearestTiesToEven);
1808 assert(fs==opOK); // should always work
Dale Johannesen728687c2007-09-05 20:39:49 +00001809
Stephen Canonb12db0e2015-09-21 19:29:25 +00001810 fs = V.multiply(rhs, rmNearestTiesToEven);
Dale Johannesenb5721632009-01-21 00:35:19 +00001811 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1812
Stephen Canonb12db0e2015-09-21 19:29:25 +00001813 fs = subtract(V, rmNearestTiesToEven);
Dale Johannesenb5721632009-01-21 00:35:19 +00001814 assert(fs==opOK || fs==opInexact); // likewise
1815
1816 if (isZero())
1817 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001818 delete[] x;
Dale Johannesenb5721632009-01-21 00:35:19 +00001819 }
Dale Johannesen689d17d2007-08-31 23:35:31 +00001820 return fs;
1821}
1822
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001823/* Normalized fused-multiply-add. */
1824APFloat::opStatus
1825APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
Neil Booth9acbf5a2007-09-26 21:33:42 +00001826 const APFloat &addend,
1827 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001828{
1829 opStatus fs;
1830
1831 /* Post-multiplication sign, before addition. */
1832 sign ^= multiplicand.sign;
1833
1834 /* If and only if all arguments are normal do we need to do an
1835 extended-precision calculation. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001836 if (isFiniteNonZero() &&
1837 multiplicand.isFiniteNonZero() &&
Hal Finkel171c2ec2014-10-14 19:23:07 +00001838 addend.isFinite()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001839 lostFraction lost_fraction;
1840
1841 lost_fraction = multiplySignificand(multiplicand, &addend);
1842 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001843 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001844 fs = (opStatus) (fs | opInexact);
1845
1846 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1847 positive zero unless rounding to minus infinity, except that
1848 adding two like-signed zeroes gives that zero. */
Lang Hames12b12e82015-01-04 01:20:55 +00001849 if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001850 sign = (rounding_mode == rmTowardNegative);
1851 } else {
1852 fs = multiplySpecials(multiplicand);
1853
1854 /* FS can only be opOK or opInvalidOp. There is no more work
1855 to do in the latter case. The IEEE-754R standard says it is
1856 implementation-defined in this case whether, if ADDEND is a
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001857 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001858
1859 If we need to do the addition we can do so with normal
1860 precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001861 if (fs == opOK)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001862 fs = addOrSubtract(addend, rounding_mode, false);
1863 }
1864
1865 return fs;
1866}
1867
Owen Andersona40319b2012-08-13 23:32:49 +00001868/* Rounding-mode corrrect round to integral value. */
1869APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
1870 opStatus fs;
Owen Andersona40319b2012-08-13 23:32:49 +00001871
Owen Anderson352dfff2012-08-15 18:28:45 +00001872 // If the exponent is large enough, we know that this value is already
1873 // integral, and the arithmetic below would potentially cause it to saturate
1874 // to +/-Inf. Bail out early instead.
Michael Gottesman8136c382013-06-26 23:17:28 +00001875 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
Owen Anderson352dfff2012-08-15 18:28:45 +00001876 return opOK;
1877
Owen Andersona40319b2012-08-13 23:32:49 +00001878 // The algorithm here is quite simple: we add 2^(p-1), where p is the
1879 // precision of our format, and then subtract it back off again. The choice
1880 // of rounding modes for the addition/subtraction determines the rounding mode
1881 // for our integral rounding as well.
Owen Andersonbe7e2972012-08-15 16:42:53 +00001882 // NOTE: When the input value is negative, we do subtraction followed by
Owen Anderson1ff74b02012-08-15 05:39:46 +00001883 // addition instead.
Owen Anderson0b357222012-08-14 18:51:15 +00001884 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1885 IntegerConstant <<= semanticsPrecision(*semantics)-1;
Owen Andersona40319b2012-08-13 23:32:49 +00001886 APFloat MagicConstant(*semantics);
1887 fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1888 rmNearestTiesToEven);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001889 MagicConstant.copySign(*this);
1890
Owen Andersona40319b2012-08-13 23:32:49 +00001891 if (fs != opOK)
1892 return fs;
1893
Owen Anderson1ff74b02012-08-15 05:39:46 +00001894 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1895 bool inputSign = isNegative();
1896
Owen Andersona40319b2012-08-13 23:32:49 +00001897 fs = add(MagicConstant, rounding_mode);
1898 if (fs != opOK && fs != opInexact)
1899 return fs;
1900
1901 fs = subtract(MagicConstant, rounding_mode);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001902
1903 // Restore the input sign.
1904 if (inputSign != isNegative())
1905 changeSign();
1906
Owen Andersona40319b2012-08-13 23:32:49 +00001907 return fs;
1908}
1909
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00001910
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001911/* Comparison requires normalized numbers. */
1912APFloat::cmpResult
1913APFloat::compare(const APFloat &rhs) const
1914{
1915 cmpResult result;
1916
1917 assert(semantics == rhs.semantics);
1918
Michael Gottesman9b877e12013-06-24 09:57:57 +00001919 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001920 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001921 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001922
Michael Gottesman9b877e12013-06-24 09:57:57 +00001923 case PackCategoriesIntoKey(fcNaN, fcZero):
1924 case PackCategoriesIntoKey(fcNaN, fcNormal):
1925 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1926 case PackCategoriesIntoKey(fcNaN, fcNaN):
1927 case PackCategoriesIntoKey(fcZero, fcNaN):
1928 case PackCategoriesIntoKey(fcNormal, fcNaN):
1929 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001930 return cmpUnordered;
1931
Michael Gottesman9b877e12013-06-24 09:57:57 +00001932 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1933 case PackCategoriesIntoKey(fcInfinity, fcZero):
1934 case PackCategoriesIntoKey(fcNormal, fcZero):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001935 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001936 return cmpLessThan;
1937 else
1938 return cmpGreaterThan;
1939
Michael Gottesman9b877e12013-06-24 09:57:57 +00001940 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1941 case PackCategoriesIntoKey(fcZero, fcInfinity):
1942 case PackCategoriesIntoKey(fcZero, fcNormal):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001943 if (rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001944 return cmpGreaterThan;
1945 else
1946 return cmpLessThan;
1947
Michael Gottesman9b877e12013-06-24 09:57:57 +00001948 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001949 if (sign == rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001950 return cmpEqual;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001951 else if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001952 return cmpLessThan;
1953 else
1954 return cmpGreaterThan;
1955
Michael Gottesman9b877e12013-06-24 09:57:57 +00001956 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001957 return cmpEqual;
1958
Michael Gottesman9b877e12013-06-24 09:57:57 +00001959 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001960 break;
1961 }
1962
1963 /* Two normal numbers. Do they have the same sign? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001964 if (sign != rhs.sign) {
1965 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001966 result = cmpLessThan;
1967 else
1968 result = cmpGreaterThan;
1969 } else {
1970 /* Compare absolute values; invert result if negative. */
1971 result = compareAbsoluteValue(rhs);
1972
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001973 if (sign) {
1974 if (result == cmpLessThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001975 result = cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001976 else if (result == cmpGreaterThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001977 result = cmpLessThan;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001978 }
1979 }
1980
1981 return result;
1982}
1983
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001984/// APFloat::convert - convert a value of one floating point type to another.
1985/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1986/// records whether the transformation lost information, i.e. whether
1987/// converting the result back to the original type will produce the
1988/// original value (this is almost the same as return value==fsOK, but there
1989/// are edge cases where this is not so).
1990
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001991APFloat::opStatus
1992APFloat::convert(const fltSemantics &toSemantics,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001993 roundingMode rounding_mode, bool *losesInfo)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001994{
Neil Bootha8d72692007-09-22 02:56:19 +00001995 lostFraction lostFraction;
1996 unsigned int newPartCount, oldPartCount;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001997 opStatus fs;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001998 int shift;
1999 const fltSemantics &fromSemantics = *semantics;
Neil Booth9acbf5a2007-09-26 21:33:42 +00002000
Neil Bootha8d72692007-09-22 02:56:19 +00002001 lostFraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002002 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Bootha8d72692007-09-22 02:56:19 +00002003 oldPartCount = partCount();
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002004 shift = toSemantics.precision - fromSemantics.precision;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002005
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002006 bool X86SpecialNan = false;
2007 if (&fromSemantics == &APFloat::x87DoubleExtended &&
2008 &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN &&
2009 (!(*significandParts() & 0x8000000000000000ULL) ||
2010 !(*significandParts() & 0x4000000000000000ULL))) {
2011 // x86 has some unusual NaNs which cannot be represented in any other
2012 // format; note them here.
2013 X86SpecialNan = true;
2014 }
2015
Ulrich Weigand1d4dbda2013-07-16 13:03:25 +00002016 // If this is a truncation of a denormal number, and the target semantics
2017 // has larger exponent range than the source semantics (this can happen
2018 // when truncating from PowerPC double-double to double format), the
2019 // right shift could lose result mantissa bits. Adjust exponent instead
2020 // of performing excessive shift.
2021 if (shift < 0 && isFiniteNonZero()) {
2022 int exponentChange = significandMSB() + 1 - fromSemantics.precision;
2023 if (exponent + exponentChange < toSemantics.minExponent)
2024 exponentChange = toSemantics.minExponent - exponent;
2025 if (exponentChange < shift)
2026 exponentChange = shift;
2027 if (exponentChange < 0) {
2028 shift -= exponentChange;
2029 exponent += exponentChange;
2030 }
2031 }
2032
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002033 // If this is a truncation, perform the shift before we narrow the storage.
Michael Gottesman8136c382013-06-26 23:17:28 +00002034 if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002035 lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
2036
2037 // Fix the storage so it can hold to new value.
Neil Bootha8d72692007-09-22 02:56:19 +00002038 if (newPartCount > oldPartCount) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002039 // The new type requires more storage; make it available.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002040 integerPart *newParts;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002041 newParts = new integerPart[newPartCount];
2042 APInt::tcSet(newParts, 0, newPartCount);
Michael Gottesman8136c382013-06-26 23:17:28 +00002043 if (isFiniteNonZero() || category==fcNaN)
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002044 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002045 freeSignificand();
2046 significand.parts = newParts;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002047 } else if (newPartCount == 1 && oldPartCount != 1) {
2048 // Switch to built-in storage for a single part.
2049 integerPart newPart = 0;
Michael Gottesman8136c382013-06-26 23:17:28 +00002050 if (isFiniteNonZero() || category==fcNaN)
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002051 newPart = significandParts()[0];
2052 freeSignificand();
2053 significand.part = newPart;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002054 }
2055
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002056 // Now that we have the right storage, switch the semantics.
2057 semantics = &toSemantics;
2058
2059 // If this is an extension, perform the shift now that the storage is
2060 // available.
Michael Gottesman8136c382013-06-26 23:17:28 +00002061 if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002062 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2063
Michael Gottesman8136c382013-06-26 23:17:28 +00002064 if (isFiniteNonZero()) {
Neil Bootha8d72692007-09-22 02:56:19 +00002065 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002066 *losesInfo = (fs != opOK);
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002067 } else if (category == fcNaN) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002068 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
Benjamin Kramerb361adb2013-01-25 17:01:00 +00002069
2070 // For x87 extended precision, we want to make a NaN, not a special NaN if
2071 // the input wasn't special either.
2072 if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
2073 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2074
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002075 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2076 // does not give you back the same bits. This is dubious, and we
2077 // don't currently do it. You're really supposed to get
2078 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002079 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002080 } else {
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002081 *losesInfo = false;
Eli Friedman31f01162011-11-28 18:50:37 +00002082 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002083 }
2084
2085 return fs;
2086}
2087
2088/* Convert a floating point number to an integer according to the
2089 rounding mode. If the rounded integer value is out of range this
Neil Booth618d0fc2007-11-01 22:43:37 +00002090 returns an invalid operation exception and the contents of the
2091 destination parts are unspecified. If the rounded value is in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002092 range but the floating point number is not the exact integer, the C
2093 standard doesn't require an inexact exception to be raised. IEEE
2094 854 does require it so we do that.
2095
2096 Note that for conversions to integer type the C standard requires
2097 round-to-zero to always be used. */
2098APFloat::opStatus
Neil Booth618d0fc2007-11-01 22:43:37 +00002099APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
2100 bool isSigned,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002101 roundingMode rounding_mode,
2102 bool *isExact) const
Neil Booth618d0fc2007-11-01 22:43:37 +00002103{
2104 lostFraction lost_fraction;
2105 const integerPart *src;
2106 unsigned int dstPartsCount, truncatedBits;
2107
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002108 *isExact = false;
2109
Neil Booth618d0fc2007-11-01 22:43:37 +00002110 /* Handle the three special cases first. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002111 if (category == fcInfinity || category == fcNaN)
Neil Booth618d0fc2007-11-01 22:43:37 +00002112 return opInvalidOp;
2113
2114 dstPartsCount = partCountForBits(width);
2115
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002116 if (category == fcZero) {
Neil Booth618d0fc2007-11-01 22:43:37 +00002117 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen7221af32008-10-07 00:40:01 +00002118 // Negative zero can't be represented as an int.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002119 *isExact = !sign;
2120 return opOK;
Neil Booth618d0fc2007-11-01 22:43:37 +00002121 }
2122
2123 src = significandParts();
2124
2125 /* Step 1: place our absolute value, with any fraction truncated, in
2126 the destination. */
2127 if (exponent < 0) {
2128 /* Our absolute value is less than one; truncate everything. */
2129 APInt::tcSet(parts, 0, dstPartsCount);
Dale Johannesen740e9872009-01-19 21:17:05 +00002130 /* For exponent -1 the integer bit represents .5, look at that.
2131 For smaller exponents leftmost truncated bit is 0. */
2132 truncatedBits = semantics->precision -1U - exponent;
Neil Booth618d0fc2007-11-01 22:43:37 +00002133 } else {
2134 /* We want the most significant (exponent + 1) bits; the rest are
2135 truncated. */
2136 unsigned int bits = exponent + 1U;
2137
2138 /* Hopelessly large in magnitude? */
2139 if (bits > width)
2140 return opInvalidOp;
2141
2142 if (bits < semantics->precision) {
2143 /* We truncate (semantics->precision - bits) bits. */
2144 truncatedBits = semantics->precision - bits;
2145 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
2146 } else {
2147 /* We want at least as many bits as are available. */
2148 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
2149 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
2150 truncatedBits = 0;
2151 }
2152 }
2153
2154 /* Step 2: work out any lost fraction, and increment the absolute
2155 value if we would round away from zero. */
2156 if (truncatedBits) {
2157 lost_fraction = lostFractionThroughTruncation(src, partCount(),
2158 truncatedBits);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002159 if (lost_fraction != lfExactlyZero &&
2160 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
Neil Booth618d0fc2007-11-01 22:43:37 +00002161 if (APInt::tcIncrement(parts, dstPartsCount))
2162 return opInvalidOp; /* Overflow. */
2163 }
2164 } else {
2165 lost_fraction = lfExactlyZero;
2166 }
2167
2168 /* Step 3: check if we fit in the destination. */
2169 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
2170
2171 if (sign) {
2172 if (!isSigned) {
2173 /* Negative numbers cannot be represented as unsigned. */
2174 if (omsb != 0)
2175 return opInvalidOp;
2176 } else {
2177 /* It takes omsb bits to represent the unsigned integer value.
2178 We lose a bit for the sign, but care is needed as the
2179 maximally negative integer is a special case. */
2180 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2181 return opInvalidOp;
2182
2183 /* This case can happen because of rounding. */
2184 if (omsb > width)
2185 return opInvalidOp;
2186 }
2187
2188 APInt::tcNegate (parts, dstPartsCount);
2189 } else {
2190 if (omsb >= width + !isSigned)
2191 return opInvalidOp;
2192 }
2193
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002194 if (lost_fraction == lfExactlyZero) {
2195 *isExact = true;
Neil Booth618d0fc2007-11-01 22:43:37 +00002196 return opOK;
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002197 } else
Neil Booth618d0fc2007-11-01 22:43:37 +00002198 return opInexact;
2199}
2200
2201/* Same as convertToSignExtendedInteger, except we provide
2202 deterministic values in case of an invalid operation exception,
2203 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002204 for underflow or overflow.
2205 The *isExact output tells whether the result is exact, in the sense
2206 that converting it back to the original floating point type produces
2207 the original value. This is almost equivalent to result==opOK,
2208 except for negative zeroes.
2209*/
Neil Booth618d0fc2007-11-01 22:43:37 +00002210APFloat::opStatus
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002211APFloat::convertToInteger(integerPart *parts, unsigned int width,
Neil Booth9acbf5a2007-09-26 21:33:42 +00002212 bool isSigned,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002213 roundingMode rounding_mode, bool *isExact) const
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002214{
Neil Booth618d0fc2007-11-01 22:43:37 +00002215 opStatus fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002216
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002217 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002218 isExact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002219
Neil Booth618d0fc2007-11-01 22:43:37 +00002220 if (fs == opInvalidOp) {
2221 unsigned int bits, dstPartsCount;
2222
2223 dstPartsCount = partCountForBits(width);
2224
2225 if (category == fcNaN)
2226 bits = 0;
2227 else if (sign)
2228 bits = isSigned;
2229 else
2230 bits = width - isSigned;
2231
2232 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2233 if (sign && isSigned)
2234 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002235 }
2236
Neil Booth618d0fc2007-11-01 22:43:37 +00002237 return fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002238}
2239
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +00002240/* Same as convertToInteger(integerPart*, ...), except the result is returned in
2241 an APSInt, whose initial bit-width and signed-ness are used to determine the
2242 precision of the conversion.
2243 */
2244APFloat::opStatus
2245APFloat::convertToInteger(APSInt &result,
2246 roundingMode rounding_mode, bool *isExact) const
2247{
2248 unsigned bitWidth = result.getBitWidth();
2249 SmallVector<uint64_t, 4> parts(result.getNumWords());
2250 opStatus status = convertToInteger(
2251 parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
2252 // Keeps the original signed-ness.
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002253 result = APInt(bitWidth, parts);
Jeffrey Yasskin03b81a22011-07-15 07:04:56 +00002254 return status;
2255}
2256
Neil Booth6c1c8582007-10-07 12:07:53 +00002257/* Convert an unsigned integer SRC to a floating point number,
2258 rounding according to ROUNDING_MODE. The sign of the floating
2259 point number is not modified. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002260APFloat::opStatus
Neil Booth6c1c8582007-10-07 12:07:53 +00002261APFloat::convertFromUnsignedParts(const integerPart *src,
2262 unsigned int srcCount,
2263 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002264{
Neil Booth49c6aab2007-10-08 14:39:42 +00002265 unsigned int omsb, precision, dstCount;
Neil Booth6c1c8582007-10-07 12:07:53 +00002266 integerPart *dst;
Neil Booth49c6aab2007-10-08 14:39:42 +00002267 lostFraction lost_fraction;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002268
2269 category = fcNormal;
Neil Booth49c6aab2007-10-08 14:39:42 +00002270 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002271 dst = significandParts();
2272 dstCount = partCount();
Neil Booth49c6aab2007-10-08 14:39:42 +00002273 precision = semantics->precision;
Neil Booth6c1c8582007-10-07 12:07:53 +00002274
Nick Lewyckyf66daac2011-10-03 21:30:08 +00002275 /* We want the most significant PRECISION bits of SRC. There may not
Neil Booth49c6aab2007-10-08 14:39:42 +00002276 be that many; extract what we can. */
2277 if (precision <= omsb) {
2278 exponent = omsb - 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002279 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth49c6aab2007-10-08 14:39:42 +00002280 omsb - precision);
2281 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2282 } else {
2283 exponent = precision - 1;
2284 lost_fraction = lfExactlyZero;
2285 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth6c1c8582007-10-07 12:07:53 +00002286 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002287
2288 return normalize(rounding_mode, lost_fraction);
2289}
2290
Dan Gohman35723eb2008-02-29 01:26:11 +00002291APFloat::opStatus
2292APFloat::convertFromAPInt(const APInt &Val,
2293 bool isSigned,
2294 roundingMode rounding_mode)
2295{
2296 unsigned int partCount = Val.getNumWords();
2297 APInt api = Val;
2298
2299 sign = false;
2300 if (isSigned && api.isNegative()) {
2301 sign = true;
2302 api = -api;
2303 }
2304
2305 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2306}
2307
Neil Booth03f58ab2007-10-07 12:15:41 +00002308/* Convert a two's complement integer SRC to a floating point number,
2309 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2310 integer is signed, in which case it must be sign-extended. */
2311APFloat::opStatus
2312APFloat::convertFromSignExtendedInteger(const integerPart *src,
2313 unsigned int srcCount,
2314 bool isSigned,
2315 roundingMode rounding_mode)
2316{
2317 opStatus status;
2318
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002319 if (isSigned &&
2320 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002321 integerPart *copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002322
2323 /* If we're signed and negative negate a copy. */
2324 sign = true;
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002325 copy = new integerPart[srcCount];
Neil Booth03f58ab2007-10-07 12:15:41 +00002326 APInt::tcAssign(copy, src, srcCount);
2327 APInt::tcNegate(copy, srcCount);
2328 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002329 delete [] copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002330 } else {
2331 sign = false;
2332 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2333 }
2334
2335 return status;
2336}
2337
Neil Booth5f009732007-10-07 11:45:55 +00002338/* FIXME: should this just take a const APInt reference? */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002339APFloat::opStatus
Neil Booth5f009732007-10-07 11:45:55 +00002340APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2341 unsigned int width, bool isSigned,
2342 roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002343{
Dale Johannesen42305122007-09-21 22:09:37 +00002344 unsigned int partCount = partCountForBits(width);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002345 APInt api = APInt(width, makeArrayRef(parts, partCount));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002346
2347 sign = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002348 if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
Dale Johannesen28a2c4a2007-09-30 18:17:01 +00002349 sign = true;
2350 api = -api;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002351 }
2352
Neil Boothba205222007-10-07 12:10:57 +00002353 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002354}
2355
2356APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002357APFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002358{
Erick Tryzelaara9680df2009-08-18 18:20:37 +00002359 lostFraction lost_fraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002360
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002361 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002362 zeroSignificand();
2363 exponent = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002364
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002365 integerPart *significand = significandParts();
2366 unsigned partsCount = partCount();
2367 unsigned bitPos = partsCount * integerPartWidth;
2368 bool computedTrailingFraction = false;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002369
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002370 // Skip leading zeroes and any (hexa)decimal point.
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002371 StringRef::iterator begin = s.begin();
2372 StringRef::iterator end = s.end();
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002373 StringRef::iterator dot;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002374 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002375 StringRef::iterator firstSignificantDigit = p;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002376
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002377 while (p != end) {
Dale Johannesenfa483722008-05-14 22:53:25 +00002378 integerPart hex_value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002379
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002380 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002381 assert(dot == end && "String contains multiple dots");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002382 dot = p++;
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002383 continue;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002384 }
2385
2386 hex_value = hexDigitValue(*p);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002387 if (hex_value == -1U)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002388 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002389
2390 p++;
2391
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002392 // Store the number while we have space.
2393 if (bitPos) {
2394 bitPos -= 4;
2395 hex_value <<= bitPos % integerPartWidth;
2396 significand[bitPos / integerPartWidth] |= hex_value;
2397 } else if (!computedTrailingFraction) {
2398 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2399 computedTrailingFraction = true;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002400 }
2401 }
2402
2403 /* Hex floats require an exponent but not a hexadecimal point. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002404 assert(p != end && "Hex strings require an exponent");
2405 assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2406 assert(p != begin && "Significand has no digits");
2407 assert((dot == end || p - begin != 1) && "Significand has no digits");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002408
2409 /* Ignore the exponent if we are zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002410 if (p != firstSignificantDigit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002411 int expAdjustment;
2412
2413 /* Implicit hexadecimal point? */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002414 if (dot == end)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002415 dot = p;
2416
2417 /* Calculate the exponent adjustment implicit in the number of
2418 significant digits. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002419 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002420 if (expAdjustment < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002421 expAdjustment++;
2422 expAdjustment = expAdjustment * 4 - 1;
2423
2424 /* Adjust for writing the significand starting at the most
2425 significant nibble. */
2426 expAdjustment += semantics->precision;
2427 expAdjustment -= partsCount * integerPartWidth;
2428
2429 /* Adjust for the given exponent. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002430 exponent = totalExponent(p + 1, end, expAdjustment);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002431 }
2432
2433 return normalize(rounding_mode, lost_fraction);
2434}
2435
2436APFloat::opStatus
Neil Boothb93d90e2007-10-12 16:02:31 +00002437APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2438 unsigned sigPartCount, int exp,
2439 roundingMode rounding_mode)
2440{
2441 unsigned int parts, pow5PartCount;
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +00002442 fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
Neil Boothb93d90e2007-10-12 16:02:31 +00002443 integerPart pow5Parts[maxPowerOfFiveParts];
2444 bool isNearest;
2445
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002446 isNearest = (rounding_mode == rmNearestTiesToEven ||
2447 rounding_mode == rmNearestTiesToAway);
Neil Boothb93d90e2007-10-12 16:02:31 +00002448
2449 parts = partCountForBits(semantics->precision + 11);
2450
2451 /* Calculate pow(5, abs(exp)). */
2452 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2453
2454 for (;; parts *= 2) {
2455 opStatus sigStatus, powStatus;
2456 unsigned int excessPrecision, truncatedBits;
2457
2458 calcSemantics.precision = parts * integerPartWidth - 1;
2459 excessPrecision = calcSemantics.precision - semantics->precision;
2460 truncatedBits = excessPrecision;
2461
Michael Gottesman79b09672013-06-27 21:58:19 +00002462 APFloat decSig = APFloat::getZero(calcSemantics, sign);
2463 APFloat pow5(calcSemantics);
Neil Boothb93d90e2007-10-12 16:02:31 +00002464
2465 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2466 rmNearestTiesToEven);
2467 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2468 rmNearestTiesToEven);
2469 /* Add exp, as 10^n = 5^n * 2^n. */
2470 decSig.exponent += exp;
2471
2472 lostFraction calcLostFraction;
Evan Cheng82b9e962008-05-02 21:15:08 +00002473 integerPart HUerr, HUdistance;
2474 unsigned int powHUerr;
Neil Boothb93d90e2007-10-12 16:02:31 +00002475
2476 if (exp >= 0) {
2477 /* multiplySignificand leaves the precision-th bit set to 1. */
Craig Topperc10719f2014-04-07 04:17:22 +00002478 calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
Neil Boothb93d90e2007-10-12 16:02:31 +00002479 powHUerr = powStatus != opOK;
2480 } else {
2481 calcLostFraction = decSig.divideSignificand(pow5);
2482 /* Denormal numbers have less precision. */
2483 if (decSig.exponent < semantics->minExponent) {
2484 excessPrecision += (semantics->minExponent - decSig.exponent);
2485 truncatedBits = excessPrecision;
2486 if (excessPrecision > calcSemantics.precision)
2487 excessPrecision = calcSemantics.precision;
2488 }
2489 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002490 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Boothb93d90e2007-10-12 16:02:31 +00002491 }
2492
2493 /* Both multiplySignificand and divideSignificand return the
2494 result with the integer bit set. */
Evan Cheng67c90212009-10-27 21:35:42 +00002495 assert(APInt::tcExtractBit
2496 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
Neil Boothb93d90e2007-10-12 16:02:31 +00002497
2498 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2499 powHUerr);
2500 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2501 excessPrecision, isNearest);
2502
2503 /* Are we guaranteed to round correctly if we truncate? */
2504 if (HUdistance >= HUerr) {
2505 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2506 calcSemantics.precision - excessPrecision,
2507 excessPrecision);
2508 /* Take the exponent of decSig. If we tcExtract-ed less bits
2509 above we must adjust our exponent to compensate for the
2510 implicit right shift. */
2511 exponent = (decSig.exponent + semantics->precision
2512 - (calcSemantics.precision - excessPrecision));
2513 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2514 decSig.partCount(),
2515 truncatedBits);
2516 return normalize(rounding_mode, calcLostFraction);
2517 }
2518 }
2519}
2520
2521APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002522APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
Neil Boothb93d90e2007-10-12 16:02:31 +00002523{
Neil Booth4ed401b2007-10-14 10:16:12 +00002524 decimalInfo D;
Neil Boothb93d90e2007-10-12 16:02:31 +00002525 opStatus fs;
2526
Neil Booth4ed401b2007-10-14 10:16:12 +00002527 /* Scan the text. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002528 StringRef::iterator p = str.begin();
2529 interpretDecimal(p, str.end(), &D);
Neil Boothb93d90e2007-10-12 16:02:31 +00002530
Neil Booth91305512007-10-15 15:00:55 +00002531 /* Handle the quick cases. First the case of no significant digits,
2532 i.e. zero, and then exponents that are obviously too large or too
2533 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2534 definitely overflows if
2535
2536 (exp - 1) * L >= maxExponent
2537
2538 and definitely underflows to zero where
2539
2540 (exp + 1) * L <= minExponent - precision
2541
2542 With integer arithmetic the tightest bounds for L are
2543
2544 93/28 < L < 196/59 [ numerator <= 256 ]
2545 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2546 */
2547
Michael Gottesman228156c2013-07-01 23:54:08 +00002548 // Test if we have a zero number allowing for strings with no null terminators
2549 // and zero decimals with non-zero exponents.
2550 //
2551 // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2552 // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2553 // be at most one dot. On the other hand, if we have a zero with a non-zero
2554 // exponent, then we know that D.firstSigDigit will be non-numeric.
Michael Gottesman94d61952013-07-02 15:50:05 +00002555 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Boothb93d90e2007-10-12 16:02:31 +00002556 category = fcZero;
2557 fs = opOK;
John McCallb42cc682010-02-26 22:20:41 +00002558
2559 /* Check whether the normalized exponent is high enough to overflow
2560 max during the log-rebasing in the max-exponent check below. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00002561 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
John McCallb42cc682010-02-26 22:20:41 +00002562 fs = handleOverflow(rounding_mode);
2563
2564 /* If it wasn't, then it also wasn't high enough to overflow max
2565 during the log-rebasing in the min-exponent check. Check that it
2566 won't overflow min in either check, then perform the min-exponent
2567 check. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00002568 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
John McCallb42cc682010-02-26 22:20:41 +00002569 (D.normalizedExponent + 1) * 28738 <=
2570 8651 * (semantics->minExponent - (int) semantics->precision)) {
Neil Booth91305512007-10-15 15:00:55 +00002571 /* Underflow to zero and round. */
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002572 category = fcNormal;
Neil Booth91305512007-10-15 15:00:55 +00002573 zeroSignificand();
2574 fs = normalize(rounding_mode, lfLessThanHalf);
John McCallb42cc682010-02-26 22:20:41 +00002575
2576 /* We can finally safely perform the max-exponent check. */
Neil Booth91305512007-10-15 15:00:55 +00002577 } else if ((D.normalizedExponent - 1) * 42039
2578 >= 12655 * semantics->maxExponent) {
2579 /* Overflow and round. */
2580 fs = handleOverflow(rounding_mode);
Neil Boothb93d90e2007-10-12 16:02:31 +00002581 } else {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002582 integerPart *decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002583 unsigned int partCount;
Neil Boothb93d90e2007-10-12 16:02:31 +00002584
Neil Booth4ed401b2007-10-14 10:16:12 +00002585 /* A tight upper bound on number of bits required to hold an
Neil Booth91305512007-10-15 15:00:55 +00002586 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth4ed401b2007-10-14 10:16:12 +00002587 to hold the full significand, and an extra part required by
2588 tcMultiplyPart. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002589 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth91305512007-10-15 15:00:55 +00002590 partCount = partCountForBits(1 + 196 * partCount / 59);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002591 decSignificand = new integerPart[partCount + 1];
Neil Booth4ed401b2007-10-14 10:16:12 +00002592 partCount = 0;
Neil Boothb93d90e2007-10-12 16:02:31 +00002593
Neil Booth4ed401b2007-10-14 10:16:12 +00002594 /* Convert to binary efficiently - we do almost all multiplication
2595 in an integerPart. When this would overflow do we do a single
2596 bignum multiplication, and then revert again to multiplication
2597 in an integerPart. */
2598 do {
2599 integerPart decValue, val, multiplier;
2600
2601 val = 0;
2602 multiplier = 1;
2603
2604 do {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002605 if (*p == '.') {
Neil Booth4ed401b2007-10-14 10:16:12 +00002606 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002607 if (p == str.end()) {
2608 break;
2609 }
2610 }
Neil Booth4ed401b2007-10-14 10:16:12 +00002611 decValue = decDigitValue(*p++);
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002612 assert(decValue < 10U && "Invalid character in significand");
Neil Booth4ed401b2007-10-14 10:16:12 +00002613 multiplier *= 10;
2614 val = val * 10 + decValue;
2615 /* The maximum number that can be multiplied by ten with any
2616 digit added without overflowing an integerPart. */
2617 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2618
2619 /* Multiply out the current part. */
2620 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2621 partCount, partCount + 1, false);
2622
2623 /* If we used another part (likely but not guaranteed), increase
2624 the count. */
2625 if (decSignificand[partCount])
2626 partCount++;
2627 } while (p <= D.lastSigDigit);
Neil Boothb93d90e2007-10-12 16:02:31 +00002628
Neil Boothae077d22007-11-01 22:51:07 +00002629 category = fcNormal;
Neil Boothb93d90e2007-10-12 16:02:31 +00002630 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth4ed401b2007-10-14 10:16:12 +00002631 D.exponent, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002632
2633 delete [] decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002634 }
Neil Boothb93d90e2007-10-12 16:02:31 +00002635
2636 return fs;
2637}
2638
Michael Gottesman40e8a182013-06-24 09:58:05 +00002639bool
2640APFloat::convertFromStringSpecials(StringRef str) {
2641 if (str.equals("inf") || str.equals("INFINITY")) {
2642 makeInf(false);
2643 return true;
2644 }
2645
2646 if (str.equals("-inf") || str.equals("-INFINITY")) {
2647 makeInf(true);
2648 return true;
2649 }
2650
2651 if (str.equals("nan") || str.equals("NaN")) {
2652 makeNaN(false, false);
2653 return true;
2654 }
2655
2656 if (str.equals("-nan") || str.equals("-NaN")) {
2657 makeNaN(false, true);
2658 return true;
2659 }
2660
2661 return false;
2662}
2663
Neil Boothb93d90e2007-10-12 16:02:31 +00002664APFloat::opStatus
Benjamin Kramer92d89982010-07-14 22:38:02 +00002665APFloat::convertFromString(StringRef str, roundingMode rounding_mode)
Neil Booth9acbf5a2007-09-26 21:33:42 +00002666{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002667 assert(!str.empty() && "Invalid string length");
Neil Booth06077e72007-10-14 10:29:28 +00002668
Michael Gottesman40e8a182013-06-24 09:58:05 +00002669 // Handle special cases.
2670 if (convertFromStringSpecials(str))
2671 return opOK;
2672
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002673 /* Handle a leading minus sign. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002674 StringRef::iterator p = str.begin();
2675 size_t slen = str.size();
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002676 sign = *p == '-' ? 1 : 0;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002677 if (*p == '-' || *p == '+') {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002678 p++;
2679 slen--;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002680 assert(slen && "String has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002681 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002682
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002683 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002684 assert(slen - 2 && "Invalid string");
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002685 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002686 rounding_mode);
2687 }
Bill Wendlingc6075402008-11-27 08:00:12 +00002688
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002689 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002690}
Dale Johannesena719a602007-08-24 00:56:33 +00002691
Neil Booth8f1946f2007-10-03 22:26:02 +00002692/* Write out a hexadecimal representation of the floating point value
2693 to DST, which must be of sufficient size, in the C99 form
2694 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2695 excluding the terminating NUL.
2696
2697 If UPPERCASE, the output is in upper case, otherwise in lower case.
2698
2699 HEXDIGITS digits appear altogether, rounding the value if
2700 necessary. If HEXDIGITS is 0, the minimal precision to display the
2701 number precisely is used instead. If nothing would appear after
2702 the decimal point it is suppressed.
2703
2704 The decimal exponent is always printed and has at least one digit.
2705 Zero values display an exponent of zero. Infinities and NaNs
2706 appear as "infinity" or "nan" respectively.
2707
2708 The above rules are as specified by C99. There is ambiguity about
2709 what the leading hexadecimal digit should be. This implementation
2710 uses whatever is necessary so that the exponent is displayed as
2711 stored. This implies the exponent will fall within the IEEE format
2712 range, and the leading hexadecimal digit will be 0 (for denormals),
2713 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2714 any other digits zero).
2715*/
2716unsigned int
2717APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2718 bool upperCase, roundingMode rounding_mode) const
2719{
2720 char *p;
2721
2722 p = dst;
2723 if (sign)
2724 *dst++ = '-';
2725
2726 switch (category) {
2727 case fcInfinity:
2728 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2729 dst += sizeof infinityL - 1;
2730 break;
2731
2732 case fcNaN:
2733 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2734 dst += sizeof NaNU - 1;
2735 break;
2736
2737 case fcZero:
2738 *dst++ = '0';
2739 *dst++ = upperCase ? 'X': 'x';
2740 *dst++ = '0';
2741 if (hexDigits > 1) {
2742 *dst++ = '.';
2743 memset (dst, '0', hexDigits - 1);
2744 dst += hexDigits - 1;
2745 }
2746 *dst++ = upperCase ? 'P': 'p';
2747 *dst++ = '0';
2748 break;
2749
2750 case fcNormal:
2751 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2752 break;
2753 }
2754
2755 *dst = 0;
2756
Evan Cheng82b9e962008-05-02 21:15:08 +00002757 return static_cast<unsigned int>(dst - p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002758}
2759
2760/* Does the hard work of outputting the correctly rounded hexadecimal
2761 form of a normal floating point number with the specified number of
2762 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2763 digits necessary to print the value precisely is output. */
2764char *
2765APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2766 bool upperCase,
2767 roundingMode rounding_mode) const
2768{
2769 unsigned int count, valueBits, shift, partsCount, outputDigits;
2770 const char *hexDigitChars;
2771 const integerPart *significand;
2772 char *p;
2773 bool roundUp;
2774
2775 *dst++ = '0';
2776 *dst++ = upperCase ? 'X': 'x';
2777
2778 roundUp = false;
2779 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2780
2781 significand = significandParts();
2782 partsCount = partCount();
2783
2784 /* +3 because the first digit only uses the single integer bit, so
2785 we have 3 virtual zero most-significant-bits. */
2786 valueBits = semantics->precision + 3;
2787 shift = integerPartWidth - valueBits % integerPartWidth;
2788
2789 /* The natural number of digits required ignoring trailing
2790 insignificant zeroes. */
2791 outputDigits = (valueBits - significandLSB () + 3) / 4;
2792
2793 /* hexDigits of zero means use the required number for the
2794 precision. Otherwise, see if we are truncating. If we are,
Neil Booth0ea72a92007-10-06 00:24:48 +00002795 find out if we need to round away from zero. */
Neil Booth8f1946f2007-10-03 22:26:02 +00002796 if (hexDigits) {
2797 if (hexDigits < outputDigits) {
2798 /* We are dropping non-zero bits, so need to check how to round.
2799 "bits" is the number of dropped bits. */
2800 unsigned int bits;
2801 lostFraction fraction;
2802
2803 bits = valueBits - hexDigits * 4;
2804 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2805 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2806 }
2807 outputDigits = hexDigits;
2808 }
2809
2810 /* Write the digits consecutively, and start writing in the location
2811 of the hexadecimal point. We move the most significant digit
2812 left and add the hexadecimal point later. */
2813 p = ++dst;
2814
2815 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2816
2817 while (outputDigits && count) {
2818 integerPart part;
2819
2820 /* Put the most significant integerPartWidth bits in "part". */
2821 if (--count == partsCount)
2822 part = 0; /* An imaginary higher zero part. */
2823 else
2824 part = significand[count] << shift;
2825
2826 if (count && shift)
2827 part |= significand[count - 1] >> (integerPartWidth - shift);
2828
2829 /* Convert as much of "part" to hexdigits as we can. */
2830 unsigned int curDigits = integerPartWidth / 4;
2831
2832 if (curDigits > outputDigits)
2833 curDigits = outputDigits;
2834 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2835 outputDigits -= curDigits;
2836 }
2837
2838 if (roundUp) {
2839 char *q = dst;
2840
2841 /* Note that hexDigitChars has a trailing '0'. */
2842 do {
2843 q--;
2844 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth0ea72a92007-10-06 00:24:48 +00002845 } while (*q == '0');
Evan Cheng67c90212009-10-27 21:35:42 +00002846 assert(q >= p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002847 } else {
2848 /* Add trailing zeroes. */
2849 memset (dst, '0', outputDigits);
2850 dst += outputDigits;
2851 }
2852
2853 /* Move the most significant digit to before the point, and if there
2854 is something after the decimal point add it. This must come
2855 after rounding above. */
2856 p[-1] = p[0];
2857 if (dst -1 == p)
2858 dst--;
2859 else
2860 p[0] = '.';
2861
2862 /* Finally output the exponent. */
2863 *dst++ = upperCase ? 'P': 'p';
2864
Neil Booth32897f52007-10-06 07:29:25 +00002865 return writeSignedDecimal (dst, exponent);
Neil Booth8f1946f2007-10-03 22:26:02 +00002866}
2867
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002868hash_code llvm::hash_value(const APFloat &Arg) {
Michael Gottesman8136c382013-06-26 23:17:28 +00002869 if (!Arg.isFiniteNonZero())
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002870 return hash_combine((uint8_t)Arg.category,
2871 // NaN has no sign, fix it at zero.
2872 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2873 Arg.semantics->precision);
2874
2875 // Normal floats need their exponent and significand hashed.
2876 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2877 Arg.semantics->precision, Arg.exponent,
2878 hash_combine_range(
2879 Arg.significandParts(),
2880 Arg.significandParts() + Arg.partCount()));
Dale Johannesena719a602007-08-24 00:56:33 +00002881}
2882
2883// Conversion from APFloat to/from host float/double. It may eventually be
2884// possible to eliminate these and have everybody deal with APFloats, but that
2885// will take a while. This approach will not easily extend to long double.
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002886// Current implementation requires integerPartWidth==64, which is correct at
2887// the moment but could be made more general.
Dale Johannesena719a602007-08-24 00:56:33 +00002888
Dale Johannesen728687c2007-09-05 20:39:49 +00002889// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002890// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen728687c2007-09-05 20:39:49 +00002891
Dale Johannesen245dceb2007-09-11 18:32:33 +00002892APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00002893APFloat::convertF80LongDoubleAPFloatToAPInt() const
2894{
Dan Gohmanb456a152008-01-29 12:08:20 +00002895 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
Evan Cheng67c90212009-10-27 21:35:42 +00002896 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002897
2898 uint64_t myexponent, mysignificand;
2899
Michael Gottesman8136c382013-06-26 23:17:28 +00002900 if (isFiniteNonZero()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00002901 myexponent = exponent+16383; //bias
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002902 mysignificand = significandParts()[0];
Dale Johannesen245dceb2007-09-11 18:32:33 +00002903 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2904 myexponent = 0; // denormal
2905 } else if (category==fcZero) {
2906 myexponent = 0;
2907 mysignificand = 0;
2908 } else if (category==fcInfinity) {
2909 myexponent = 0x7fff;
2910 mysignificand = 0x8000000000000000ULL;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002911 } else {
2912 assert(category == fcNaN && "Unknown category");
Dale Johannesen245dceb2007-09-11 18:32:33 +00002913 myexponent = 0x7fff;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002914 mysignificand = significandParts()[0];
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002915 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00002916
2917 uint64_t words[2];
Dale Johannesen93eefa02009-03-23 21:16:53 +00002918 words[0] = mysignificand;
2919 words[1] = ((uint64_t)(sign & 1) << 15) |
2920 (myexponent & 0x7fffLL);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002921 return APInt(80, words);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002922}
2923
2924APInt
Dale Johannesen007aa372007-10-11 18:07:22 +00002925APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2926{
Dan Gohmanb456a152008-01-29 12:08:20 +00002927 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
Evan Cheng67c90212009-10-27 21:35:42 +00002928 assert(partCount()==2);
Dale Johannesen007aa372007-10-11 18:07:22 +00002929
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002930 uint64_t words[2];
2931 opStatus fs;
2932 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00002933
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002934 // Convert number to double. To avoid spurious underflows, we re-
2935 // normalize against the "double" minExponent first, and only *then*
2936 // truncate the mantissa. The result of that second conversion
2937 // may be inexact, but should never underflow.
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002938 // Declare fltSemantics before APFloat that uses it (and
2939 // saves pointer to it) to ensure correct destruction order.
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002940 fltSemantics extendedSemantics = *semantics;
2941 extendedSemantics.minExponent = IEEEdouble.minExponent;
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002942 APFloat extended(*this);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002943 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2944 assert(fs == opOK && !losesInfo);
2945 (void)fs;
2946
2947 APFloat u(extended);
2948 fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2949 assert(fs == opOK || fs == opInexact);
2950 (void)fs;
2951 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2952
2953 // If conversion was exact or resulted in a special case, we're done;
2954 // just set the second double to zero. Otherwise, re-convert back to
2955 // the extended format and compute the difference. This now should
2956 // convert exactly to double.
Michael Gottesman8136c382013-06-26 23:17:28 +00002957 if (u.isFiniteNonZero() && losesInfo) {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002958 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2959 assert(fs == opOK && !losesInfo);
2960 (void)fs;
2961
2962 APFloat v(extended);
2963 v.subtract(u, rmNearestTiesToEven);
2964 fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2965 assert(fs == opOK && !losesInfo);
2966 (void)fs;
2967 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
Dale Johannesen007aa372007-10-11 18:07:22 +00002968 } else {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002969 words[1] = 0;
Dale Johannesen007aa372007-10-11 18:07:22 +00002970 }
2971
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002972 return APInt(128, words);
Dale Johannesen007aa372007-10-11 18:07:22 +00002973}
2974
2975APInt
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002976APFloat::convertQuadrupleAPFloatToAPInt() const
2977{
2978 assert(semantics == (const llvm::fltSemantics*)&IEEEquad);
Evan Cheng67c90212009-10-27 21:35:42 +00002979 assert(partCount()==2);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002980
2981 uint64_t myexponent, mysignificand, mysignificand2;
2982
Michael Gottesman8136c382013-06-26 23:17:28 +00002983 if (isFiniteNonZero()) {
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002984 myexponent = exponent+16383; //bias
2985 mysignificand = significandParts()[0];
2986 mysignificand2 = significandParts()[1];
2987 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2988 myexponent = 0; // denormal
2989 } else if (category==fcZero) {
2990 myexponent = 0;
2991 mysignificand = mysignificand2 = 0;
2992 } else if (category==fcInfinity) {
2993 myexponent = 0x7fff;
2994 mysignificand = mysignificand2 = 0;
2995 } else {
2996 assert(category == fcNaN && "Unknown category!");
2997 myexponent = 0x7fff;
2998 mysignificand = significandParts()[0];
2999 mysignificand2 = significandParts()[1];
3000 }
3001
3002 uint64_t words[2];
3003 words[0] = mysignificand;
3004 words[1] = ((uint64_t)(sign & 1) << 63) |
3005 ((myexponent & 0x7fff) << 48) |
Anton Korobeynikov876955c2009-08-21 23:09:47 +00003006 (mysignificand2 & 0xffffffffffffLL);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003007
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00003008 return APInt(128, words);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003009}
3010
3011APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00003012APFloat::convertDoubleAPFloatToAPInt() const
3013{
Dan Gohman58c468f2007-09-14 20:08:19 +00003014 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
Evan Cheng67c90212009-10-27 21:35:42 +00003015 assert(partCount()==1);
Dale Johannesena719a602007-08-24 00:56:33 +00003016
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003017 uint64_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003018
Michael Gottesman8136c382013-06-26 23:17:28 +00003019 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00003020 myexponent = exponent+1023; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00003021 mysignificand = *significandParts();
3022 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
3023 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00003024 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00003025 myexponent = 0;
3026 mysignificand = 0;
3027 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00003028 myexponent = 0x7ff;
3029 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003030 } else {
3031 assert(category == fcNaN && "Unknown category!");
Dale Johannesena719a602007-08-24 00:56:33 +00003032 myexponent = 0x7ff;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003033 mysignificand = *significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003034 }
Dale Johannesena719a602007-08-24 00:56:33 +00003035
Evan Cheng82b9e962008-05-02 21:15:08 +00003036 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003037 ((myexponent & 0x7ff) << 52) |
3038 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesena719a602007-08-24 00:56:33 +00003039}
3040
Dale Johannesen245dceb2007-09-11 18:32:33 +00003041APInt
Neil Booth9acbf5a2007-09-26 21:33:42 +00003042APFloat::convertFloatAPFloatToAPInt() const
3043{
Dan Gohman58c468f2007-09-14 20:08:19 +00003044 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
Evan Cheng67c90212009-10-27 21:35:42 +00003045 assert(partCount()==1);
Neil Booth9acbf5a2007-09-26 21:33:42 +00003046
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003047 uint32_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003048
Michael Gottesman8136c382013-06-26 23:17:28 +00003049 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00003050 myexponent = exponent+127; //bias
Evan Cheng82b9e962008-05-02 21:15:08 +00003051 mysignificand = (uint32_t)*significandParts();
Dale Johannesen06a10df2007-11-17 01:02:27 +00003052 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen728687c2007-09-05 20:39:49 +00003053 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00003054 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00003055 myexponent = 0;
3056 mysignificand = 0;
3057 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00003058 myexponent = 0xff;
3059 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003060 } else {
3061 assert(category == fcNaN && "Unknown category!");
Dale Johannesen728687c2007-09-05 20:39:49 +00003062 myexponent = 0xff;
Evan Cheng82b9e962008-05-02 21:15:08 +00003063 mysignificand = (uint32_t)*significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003064 }
Dale Johannesena719a602007-08-24 00:56:33 +00003065
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003066 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3067 (mysignificand & 0x7fffff)));
Dale Johannesena719a602007-08-24 00:56:33 +00003068}
3069
Chris Lattner4794b2b2009-10-16 02:13:51 +00003070APInt
3071APFloat::convertHalfAPFloatToAPInt() const
3072{
3073 assert(semantics == (const llvm::fltSemantics*)&IEEEhalf);
Evan Cheng67c90212009-10-27 21:35:42 +00003074 assert(partCount()==1);
Chris Lattner4794b2b2009-10-16 02:13:51 +00003075
3076 uint32_t myexponent, mysignificand;
3077
Michael Gottesman8136c382013-06-26 23:17:28 +00003078 if (isFiniteNonZero()) {
Chris Lattner4794b2b2009-10-16 02:13:51 +00003079 myexponent = exponent+15; //bias
3080 mysignificand = (uint32_t)*significandParts();
3081 if (myexponent == 1 && !(mysignificand & 0x400))
3082 myexponent = 0; // denormal
3083 } else if (category==fcZero) {
3084 myexponent = 0;
3085 mysignificand = 0;
3086 } else if (category==fcInfinity) {
Dale Johannesen0d670b52009-10-23 04:02:51 +00003087 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003088 mysignificand = 0;
3089 } else {
3090 assert(category == fcNaN && "Unknown category!");
Dale Johannesen0d670b52009-10-23 04:02:51 +00003091 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003092 mysignificand = (uint32_t)*significandParts();
3093 }
3094
3095 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3096 (mysignificand & 0x3ff)));
3097}
3098
Dale Johannesen007aa372007-10-11 18:07:22 +00003099// This function creates an APInt that is just a bit map of the floating
3100// point constant as it would appear in memory. It is not a conversion,
3101// and treating the result as a normal integer is unlikely to be useful.
3102
Dale Johannesen245dceb2007-09-11 18:32:33 +00003103APInt
Dale Johannesen54306fe2008-10-09 18:53:47 +00003104APFloat::bitcastToAPInt() const
Neil Booth9acbf5a2007-09-26 21:33:42 +00003105{
Chris Lattner4794b2b2009-10-16 02:13:51 +00003106 if (semantics == (const llvm::fltSemantics*)&IEEEhalf)
3107 return convertHalfAPFloatToAPInt();
3108
Dan Gohmanb456a152008-01-29 12:08:20 +00003109 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003110 return convertFloatAPFloatToAPInt();
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003111
Dan Gohmanb456a152008-01-29 12:08:20 +00003112 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003113 return convertDoubleAPFloatToAPInt();
Neil Booth9acbf5a2007-09-26 21:33:42 +00003114
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003115 if (semantics == (const llvm::fltSemantics*)&IEEEquad)
3116 return convertQuadrupleAPFloatToAPInt();
3117
Dan Gohmanb456a152008-01-29 12:08:20 +00003118 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
Dale Johannesen007aa372007-10-11 18:07:22 +00003119 return convertPPCDoubleDoubleAPFloatToAPInt();
3120
Dan Gohmanb456a152008-01-29 12:08:20 +00003121 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003122 "unknown format!");
3123 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003124}
3125
Neil Booth9acbf5a2007-09-26 21:33:42 +00003126float
3127APFloat::convertToFloat() const
3128{
Chris Lattner688f9912009-09-24 21:44:20 +00003129 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle &&
3130 "Float semantics are not IEEEsingle");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003131 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003132 return api.bitsToFloat();
3133}
3134
Neil Booth9acbf5a2007-09-26 21:33:42 +00003135double
3136APFloat::convertToDouble() const
3137{
Chris Lattner688f9912009-09-24 21:44:20 +00003138 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble &&
3139 "Float semantics are not IEEEdouble");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003140 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003141 return api.bitsToDouble();
3142}
3143
Dale Johannesenfff29952008-10-06 18:22:29 +00003144/// Integer bit is explicit in this format. Intel hardware (387 and later)
3145/// does not support these bit patterns:
3146/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3147/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3148/// exponent = 0, integer bit 1 ("pseudodenormal")
3149/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3150/// At the moment, the first two are treated as NaNs, the second two as Normal.
Dale Johannesen245dceb2007-09-11 18:32:33 +00003151void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003152APFloat::initFromF80LongDoubleAPInt(const APInt &api)
3153{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003154 assert(api.getBitWidth()==80);
3155 uint64_t i1 = api.getRawData()[0];
3156 uint64_t i2 = api.getRawData()[1];
Dale Johannesen93eefa02009-03-23 21:16:53 +00003157 uint64_t myexponent = (i2 & 0x7fff);
3158 uint64_t mysignificand = i1;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003159
3160 initialize(&APFloat::x87DoubleExtended);
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003161 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003162
Dale Johannesen93eefa02009-03-23 21:16:53 +00003163 sign = static_cast<unsigned int>(i2>>15);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003164 if (myexponent==0 && mysignificand==0) {
3165 // exponent, significand meaningless
3166 category = fcZero;
3167 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3168 // exponent, significand meaningless
3169 category = fcInfinity;
3170 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3171 // exponent meaningless
3172 category = fcNaN;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003173 significandParts()[0] = mysignificand;
3174 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003175 } else {
3176 category = fcNormal;
3177 exponent = myexponent - 16383;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003178 significandParts()[0] = mysignificand;
3179 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003180 if (myexponent==0) // denormal
3181 exponent = -16382;
Neil Booth9acbf5a2007-09-26 21:33:42 +00003182 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00003183}
3184
3185void
Dale Johannesen007aa372007-10-11 18:07:22 +00003186APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
3187{
3188 assert(api.getBitWidth()==128);
3189 uint64_t i1 = api.getRawData()[0];
3190 uint64_t i2 = api.getRawData()[1];
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003191 opStatus fs;
3192 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00003193
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003194 // Get the first double and convert to our format.
3195 initFromDoubleAPInt(APInt(64, i1));
3196 fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3197 assert(fs == opOK && !losesInfo);
3198 (void)fs;
Dale Johannesen007aa372007-10-11 18:07:22 +00003199
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003200 // Unless we have a special case, add in second double.
Michael Gottesman8136c382013-06-26 23:17:28 +00003201 if (isFiniteNonZero()) {
Tim Northover29178a32013-01-22 09:46:31 +00003202 APFloat v(IEEEdouble, APInt(64, i2));
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003203 fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3204 assert(fs == opOK && !losesInfo);
3205 (void)fs;
3206
3207 add(v, rmNearestTiesToEven);
Dale Johannesen007aa372007-10-11 18:07:22 +00003208 }
3209}
3210
3211void
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003212APFloat::initFromQuadrupleAPInt(const APInt &api)
3213{
3214 assert(api.getBitWidth()==128);
3215 uint64_t i1 = api.getRawData()[0];
3216 uint64_t i2 = api.getRawData()[1];
3217 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3218 uint64_t mysignificand = i1;
3219 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3220
3221 initialize(&APFloat::IEEEquad);
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00003222 assert(partCount()==2);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003223
3224 sign = static_cast<unsigned int>(i2>>63);
3225 if (myexponent==0 &&
3226 (mysignificand==0 && mysignificand2==0)) {
3227 // exponent, significand meaningless
3228 category = fcZero;
3229 } else if (myexponent==0x7fff &&
3230 (mysignificand==0 && mysignificand2==0)) {
3231 // exponent, significand meaningless
3232 category = fcInfinity;
3233 } else if (myexponent==0x7fff &&
3234 (mysignificand!=0 || mysignificand2 !=0)) {
3235 // exponent meaningless
3236 category = fcNaN;
3237 significandParts()[0] = mysignificand;
3238 significandParts()[1] = mysignificand2;
3239 } else {
3240 category = fcNormal;
3241 exponent = myexponent - 16383;
3242 significandParts()[0] = mysignificand;
3243 significandParts()[1] = mysignificand2;
3244 if (myexponent==0) // denormal
3245 exponent = -16382;
3246 else
3247 significandParts()[1] |= 0x1000000000000LL; // integer bit
3248 }
3249}
3250
3251void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003252APFloat::initFromDoubleAPInt(const APInt &api)
3253{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003254 assert(api.getBitWidth()==64);
3255 uint64_t i = *api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003256 uint64_t myexponent = (i >> 52) & 0x7ff;
3257 uint64_t mysignificand = i & 0xfffffffffffffLL;
3258
Dale Johannesena719a602007-08-24 00:56:33 +00003259 initialize(&APFloat::IEEEdouble);
Dale Johannesena719a602007-08-24 00:56:33 +00003260 assert(partCount()==1);
3261
Evan Cheng82b9e962008-05-02 21:15:08 +00003262 sign = static_cast<unsigned int>(i>>63);
Dale Johannesena719a602007-08-24 00:56:33 +00003263 if (myexponent==0 && mysignificand==0) {
3264 // exponent, significand meaningless
3265 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003266 } else if (myexponent==0x7ff && mysignificand==0) {
3267 // exponent, significand meaningless
3268 category = fcInfinity;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003269 } else if (myexponent==0x7ff && mysignificand!=0) {
3270 // exponent meaningless
3271 category = fcNaN;
3272 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003273 } else {
Dale Johannesena719a602007-08-24 00:56:33 +00003274 category = fcNormal;
3275 exponent = myexponent - 1023;
Dale Johannesen728687c2007-09-05 20:39:49 +00003276 *significandParts() = mysignificand;
3277 if (myexponent==0) // denormal
3278 exponent = -1022;
3279 else
3280 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth9acbf5a2007-09-26 21:33:42 +00003281 }
Dale Johannesena719a602007-08-24 00:56:33 +00003282}
3283
Dale Johannesen245dceb2007-09-11 18:32:33 +00003284void
Neil Booth9acbf5a2007-09-26 21:33:42 +00003285APFloat::initFromFloatAPInt(const APInt & api)
3286{
Dale Johannesen245dceb2007-09-11 18:32:33 +00003287 assert(api.getBitWidth()==32);
3288 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003289 uint32_t myexponent = (i >> 23) & 0xff;
3290 uint32_t mysignificand = i & 0x7fffff;
3291
Dale Johannesena719a602007-08-24 00:56:33 +00003292 initialize(&APFloat::IEEEsingle);
Dale Johannesena719a602007-08-24 00:56:33 +00003293 assert(partCount()==1);
3294
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003295 sign = i >> 31;
Dale Johannesena719a602007-08-24 00:56:33 +00003296 if (myexponent==0 && mysignificand==0) {
3297 // exponent, significand meaningless
3298 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003299 } else if (myexponent==0xff && mysignificand==0) {
3300 // exponent, significand meaningless
3301 category = fcInfinity;
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00003302 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesena719a602007-08-24 00:56:33 +00003303 // sign, exponent, significand meaningless
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003304 category = fcNaN;
3305 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003306 } else {
3307 category = fcNormal;
Dale Johannesena719a602007-08-24 00:56:33 +00003308 exponent = myexponent - 127; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00003309 *significandParts() = mysignificand;
3310 if (myexponent==0) // denormal
3311 exponent = -126;
3312 else
3313 *significandParts() |= 0x800000; // integer bit
Dale Johannesena719a602007-08-24 00:56:33 +00003314 }
3315}
Dale Johannesen245dceb2007-09-11 18:32:33 +00003316
Chris Lattner4794b2b2009-10-16 02:13:51 +00003317void
3318APFloat::initFromHalfAPInt(const APInt & api)
3319{
3320 assert(api.getBitWidth()==16);
3321 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen0d670b52009-10-23 04:02:51 +00003322 uint32_t myexponent = (i >> 10) & 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003323 uint32_t mysignificand = i & 0x3ff;
3324
3325 initialize(&APFloat::IEEEhalf);
3326 assert(partCount()==1);
3327
3328 sign = i >> 15;
3329 if (myexponent==0 && mysignificand==0) {
3330 // exponent, significand meaningless
3331 category = fcZero;
3332 } else if (myexponent==0x1f && mysignificand==0) {
3333 // exponent, significand meaningless
3334 category = fcInfinity;
3335 } else if (myexponent==0x1f && mysignificand!=0) {
3336 // sign, exponent, significand meaningless
3337 category = fcNaN;
3338 *significandParts() = mysignificand;
3339 } else {
3340 category = fcNormal;
3341 exponent = myexponent - 15; //bias
3342 *significandParts() = mysignificand;
3343 if (myexponent==0) // denormal
3344 exponent = -14;
3345 else
3346 *significandParts() |= 0x400; // integer bit
3347 }
3348}
3349
Dale Johannesen245dceb2007-09-11 18:32:33 +00003350/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesen007aa372007-10-11 18:07:22 +00003351/// we infer the floating point type from the size of the APInt. The
3352/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3353/// when the size is anything else).
Dale Johannesen245dceb2007-09-11 18:32:33 +00003354void
Tim Northover29178a32013-01-22 09:46:31 +00003355APFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api)
Neil Booth9acbf5a2007-09-26 21:33:42 +00003356{
Tim Northover29178a32013-01-22 09:46:31 +00003357 if (Sem == &IEEEhalf)
Chris Lattner4794b2b2009-10-16 02:13:51 +00003358 return initFromHalfAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003359 if (Sem == &IEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003360 return initFromFloatAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003361 if (Sem == &IEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003362 return initFromDoubleAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003363 if (Sem == &x87DoubleExtended)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003364 return initFromF80LongDoubleAPInt(api);
Tim Northover29178a32013-01-22 09:46:31 +00003365 if (Sem == &IEEEquad)
3366 return initFromQuadrupleAPInt(api);
3367 if (Sem == &PPCDoubleDouble)
3368 return initFromPPCDoubleDoubleAPInt(api);
3369
Craig Topper2617dcc2014-04-15 06:32:26 +00003370 llvm_unreachable(nullptr);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003371}
3372
Nadav Rotem7cc6d122011-02-17 21:22:27 +00003373APFloat
3374APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
3375{
Tim Northover29178a32013-01-22 09:46:31 +00003376 switch (BitWidth) {
3377 case 16:
3378 return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth));
3379 case 32:
3380 return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth));
3381 case 64:
3382 return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth));
3383 case 80:
3384 return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth));
3385 case 128:
3386 if (isIEEE)
3387 return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth));
3388 return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
3389 default:
3390 llvm_unreachable("Unknown floating bit width");
3391 }
Nadav Rotem7cc6d122011-02-17 21:22:27 +00003392}
3393
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +00003394unsigned APFloat::getSizeInBits(const fltSemantics &Sem) {
3395 return Sem.sizeInBits;
3396}
3397
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003398/// Make this number the largest magnitude normal number in the given
3399/// semantics.
3400void APFloat::makeLargest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003401 // We want (in interchange format):
3402 // sign = {Negative}
3403 // exponent = 1..10
3404 // significand = 1..1
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003405 category = fcNormal;
3406 sign = Negative;
3407 exponent = semantics->maxExponent;
John McCall29b5c282009-12-24 08:56:26 +00003408
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003409 // Use memset to set all but the highest integerPart to all ones.
3410 integerPart *significand = significandParts();
3411 unsigned PartCount = partCount();
3412 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
John McCall29b5c282009-12-24 08:56:26 +00003413
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003414 // Set the high integerPart especially setting all unused top bits for
3415 // internal consistency.
3416 const unsigned NumUnusedHighBits =
3417 PartCount*integerPartWidth - semantics->precision;
Alexey Samsonovba1ecbc2014-09-06 00:41:19 +00003418 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3419 ? (~integerPart(0) >> NumUnusedHighBits)
3420 : 0;
John McCall29b5c282009-12-24 08:56:26 +00003421}
3422
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003423/// Make this number the smallest magnitude denormal number in the given
3424/// semantics.
3425void APFloat::makeSmallest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003426 // We want (in interchange format):
3427 // sign = {Negative}
3428 // exponent = 0..0
3429 // significand = 0..01
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003430 category = fcNormal;
3431 sign = Negative;
3432 exponent = semantics->minExponent;
3433 APInt::tcSet(significandParts(), 1, partCount());
3434}
John McCall29b5c282009-12-24 08:56:26 +00003435
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003436
3437APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
3438 // We want (in interchange format):
3439 // sign = {Negative}
3440 // exponent = 1..10
3441 // significand = 1..1
3442 APFloat Val(Sem, uninitialized);
3443 Val.makeLargest(Negative);
3444 return Val;
3445}
3446
3447APFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) {
3448 // We want (in interchange format):
3449 // sign = {Negative}
3450 // exponent = 0..0
3451 // significand = 0..01
3452 APFloat Val(Sem, uninitialized);
3453 Val.makeSmallest(Negative);
John McCall29b5c282009-12-24 08:56:26 +00003454 return Val;
3455}
3456
3457APFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) {
Michael Gottesman79b09672013-06-27 21:58:19 +00003458 APFloat Val(Sem, uninitialized);
John McCall29b5c282009-12-24 08:56:26 +00003459
3460 // We want (in interchange format):
3461 // sign = {Negative}
3462 // exponent = 0..0
3463 // significand = 10..0
3464
Michael Gottesman30a90eb2013-07-27 21:49:21 +00003465 Val.category = fcNormal;
Michael Gottesmanccaf3322013-06-27 20:40:11 +00003466 Val.zeroSignificand();
Michael Gottesman79b09672013-06-27 21:58:19 +00003467 Val.sign = Negative;
3468 Val.exponent = Sem.minExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003469 Val.significandParts()[partCountForBits(Sem.precision)-1] |=
Eli Friedmand4330422011-10-12 21:56:19 +00003470 (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth));
John McCall29b5c282009-12-24 08:56:26 +00003471
3472 return Val;
3473}
3474
Tim Northover29178a32013-01-22 09:46:31 +00003475APFloat::APFloat(const fltSemantics &Sem, const APInt &API) {
3476 initFromAPInt(&Sem, API);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003477}
3478
Ulrich Weigande1d62f92012-10-29 18:17:42 +00003479APFloat::APFloat(float f) {
Tim Northover29178a32013-01-22 09:46:31 +00003480 initFromAPInt(&IEEEsingle, APInt::floatToBits(f));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003481}
3482
Ulrich Weigande1d62f92012-10-29 18:17:42 +00003483APFloat::APFloat(double d) {
Tim Northover29178a32013-01-22 09:46:31 +00003484 initFromAPInt(&IEEEdouble, APInt::doubleToBits(d));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003485}
John McCall29b5c282009-12-24 08:56:26 +00003486
3487namespace {
David Blaikie70fdf722012-07-25 18:04:24 +00003488 void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3489 Buffer.append(Str.begin(), Str.end());
John McCall29b5c282009-12-24 08:56:26 +00003490 }
3491
John McCalle6212ace2009-12-24 12:16:56 +00003492 /// Removes data from the given significand until it is no more
3493 /// precise than is required for the desired precision.
3494 void AdjustToPrecision(APInt &significand,
3495 int &exp, unsigned FormatPrecision) {
3496 unsigned bits = significand.getActiveBits();
3497
3498 // 196/59 is a very slight overestimate of lg_2(10).
3499 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3500
3501 if (bits <= bitsRequired) return;
3502
3503 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3504 if (!tensRemovable) return;
3505
3506 exp += tensRemovable;
3507
3508 APInt divisor(significand.getBitWidth(), 1);
3509 APInt powten(significand.getBitWidth(), 10);
3510 while (true) {
3511 if (tensRemovable & 1)
3512 divisor *= powten;
3513 tensRemovable >>= 1;
3514 if (!tensRemovable) break;
3515 powten *= powten;
3516 }
3517
3518 significand = significand.udiv(divisor);
3519
Hao Liube99cc32013-03-20 01:46:36 +00003520 // Truncate the significand down to its active bit count.
3521 significand = significand.trunc(significand.getActiveBits());
John McCalle6212ace2009-12-24 12:16:56 +00003522 }
3523
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00003524
John McCall29b5c282009-12-24 08:56:26 +00003525 void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3526 int &exp, unsigned FormatPrecision) {
3527 unsigned N = buffer.size();
3528 if (N <= FormatPrecision) return;
3529
3530 // The most significant figures are the last ones in the buffer.
3531 unsigned FirstSignificant = N - FormatPrecision;
3532
3533 // Round.
3534 // FIXME: this probably shouldn't use 'round half up'.
3535
3536 // Rounding down is just a truncation, except we also want to drop
3537 // trailing zeros from the new result.
3538 if (buffer[FirstSignificant - 1] < '5') {
NAKAMURA Takumi5adeb932012-02-19 03:18:29 +00003539 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
John McCall29b5c282009-12-24 08:56:26 +00003540 FirstSignificant++;
3541
3542 exp += FirstSignificant;
3543 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3544 return;
3545 }
3546
3547 // Rounding up requires a decimal add-with-carry. If we continue
3548 // the carry, the newly-introduced zeros will just be truncated.
3549 for (unsigned I = FirstSignificant; I != N; ++I) {
3550 if (buffer[I] == '9') {
3551 FirstSignificant++;
3552 } else {
3553 buffer[I]++;
3554 break;
3555 }
3556 }
3557
3558 // If we carried through, we have exactly one digit of precision.
3559 if (FirstSignificant == N) {
3560 exp += FirstSignificant;
3561 buffer.clear();
3562 buffer.push_back('1');
3563 return;
3564 }
3565
3566 exp += FirstSignificant;
3567 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3568 }
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00003569}
John McCall29b5c282009-12-24 08:56:26 +00003570
3571void APFloat::toString(SmallVectorImpl<char> &Str,
3572 unsigned FormatPrecision,
Chris Lattner4c1e4db2010-03-06 19:20:13 +00003573 unsigned FormatMaxPadding) const {
John McCall29b5c282009-12-24 08:56:26 +00003574 switch (category) {
3575 case fcInfinity:
3576 if (isNegative())
3577 return append(Str, "-Inf");
3578 else
3579 return append(Str, "+Inf");
3580
3581 case fcNaN: return append(Str, "NaN");
3582
3583 case fcZero:
3584 if (isNegative())
3585 Str.push_back('-');
3586
3587 if (!FormatMaxPadding)
3588 append(Str, "0.0E+0");
3589 else
3590 Str.push_back('0');
3591 return;
3592
3593 case fcNormal:
3594 break;
3595 }
3596
3597 if (isNegative())
3598 Str.push_back('-');
3599
3600 // Decompose the number into an APInt and an exponent.
3601 int exp = exponent - ((int) semantics->precision - 1);
3602 APInt significand(semantics->precision,
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00003603 makeArrayRef(significandParts(),
3604 partCountForBits(semantics->precision)));
John McCall29b5c282009-12-24 08:56:26 +00003605
John McCalldd5044a2009-12-24 23:18:09 +00003606 // Set FormatPrecision if zero. We want to do this before we
3607 // truncate trailing zeros, as those are part of the precision.
3608 if (!FormatPrecision) {
Eli Friedmane72f1322013-08-29 23:44:34 +00003609 // We use enough digits so the number can be round-tripped back to an
3610 // APFloat. The formula comes from "How to Print Floating-Point Numbers
3611 // Accurately" by Steele and White.
3612 // FIXME: Using a formula based purely on the precision is conservative;
3613 // we can print fewer digits depending on the actual value being printed.
John McCalldd5044a2009-12-24 23:18:09 +00003614
Eli Friedmane72f1322013-08-29 23:44:34 +00003615 // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3616 FormatPrecision = 2 + semantics->precision * 59 / 196;
John McCalldd5044a2009-12-24 23:18:09 +00003617 }
3618
John McCall29b5c282009-12-24 08:56:26 +00003619 // Ignore trailing binary zeros.
3620 int trailingZeros = significand.countTrailingZeros();
3621 exp += trailingZeros;
3622 significand = significand.lshr(trailingZeros);
3623
3624 // Change the exponent from 2^e to 10^e.
3625 if (exp == 0) {
3626 // Nothing to do.
3627 } else if (exp > 0) {
3628 // Just shift left.
Jay Foad583abbc2010-12-07 08:25:19 +00003629 significand = significand.zext(semantics->precision + exp);
John McCall29b5c282009-12-24 08:56:26 +00003630 significand <<= exp;
3631 exp = 0;
3632 } else { /* exp < 0 */
3633 int texp = -exp;
3634
3635 // We transform this using the identity:
3636 // (N)(2^-e) == (N)(5^e)(10^-e)
3637 // This means we have to multiply N (the significand) by 5^e.
3638 // To avoid overflow, we have to operate on numbers large
3639 // enough to store N * 5^e:
3640 // log2(N * 5^e) == log2(N) + e * log2(5)
John McCalldd5044a2009-12-24 23:18:09 +00003641 // <= semantics->precision + e * 137 / 59
3642 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003643
Eli Friedman19546412011-10-07 23:40:49 +00003644 unsigned precision = semantics->precision + (137 * texp + 136) / 59;
John McCall29b5c282009-12-24 08:56:26 +00003645
3646 // Multiply significand by 5^e.
3647 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
Jay Foad583abbc2010-12-07 08:25:19 +00003648 significand = significand.zext(precision);
John McCall29b5c282009-12-24 08:56:26 +00003649 APInt five_to_the_i(precision, 5);
3650 while (true) {
3651 if (texp & 1) significand *= five_to_the_i;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003652
John McCall29b5c282009-12-24 08:56:26 +00003653 texp >>= 1;
3654 if (!texp) break;
3655 five_to_the_i *= five_to_the_i;
3656 }
3657 }
3658
John McCalle6212ace2009-12-24 12:16:56 +00003659 AdjustToPrecision(significand, exp, FormatPrecision);
3660
Dmitri Gribenko226fea52013-01-13 16:01:15 +00003661 SmallVector<char, 256> buffer;
John McCall29b5c282009-12-24 08:56:26 +00003662
3663 // Fill the buffer.
3664 unsigned precision = significand.getBitWidth();
3665 APInt ten(precision, 10);
3666 APInt digit(precision, 0);
3667
3668 bool inTrail = true;
3669 while (significand != 0) {
3670 // digit <- significand % 10
3671 // significand <- significand / 10
3672 APInt::udivrem(significand, ten, significand, digit);
3673
3674 unsigned d = digit.getZExtValue();
3675
3676 // Drop trailing zeros.
3677 if (inTrail && !d) exp++;
3678 else {
3679 buffer.push_back((char) ('0' + d));
3680 inTrail = false;
3681 }
3682 }
3683
3684 assert(!buffer.empty() && "no characters in buffer!");
3685
3686 // Drop down to FormatPrecision.
3687 // TODO: don't do more precise calculations above than are required.
3688 AdjustToPrecision(buffer, exp, FormatPrecision);
3689
3690 unsigned NDigits = buffer.size();
3691
John McCalldd5044a2009-12-24 23:18:09 +00003692 // Check whether we should use scientific notation.
John McCall29b5c282009-12-24 08:56:26 +00003693 bool FormatScientific;
3694 if (!FormatMaxPadding)
3695 FormatScientific = true;
3696 else {
John McCall29b5c282009-12-24 08:56:26 +00003697 if (exp >= 0) {
John McCalldd5044a2009-12-24 23:18:09 +00003698 // 765e3 --> 765000
3699 // ^^^
3700 // But we shouldn't make the number look more precise than it is.
3701 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3702 NDigits + (unsigned) exp > FormatPrecision);
John McCall29b5c282009-12-24 08:56:26 +00003703 } else {
John McCalldd5044a2009-12-24 23:18:09 +00003704 // Power of the most significant digit.
3705 int MSD = exp + (int) (NDigits - 1);
3706 if (MSD >= 0) {
John McCall29b5c282009-12-24 08:56:26 +00003707 // 765e-2 == 7.65
John McCalldd5044a2009-12-24 23:18:09 +00003708 FormatScientific = false;
John McCall29b5c282009-12-24 08:56:26 +00003709 } else {
3710 // 765e-5 == 0.00765
3711 // ^ ^^
John McCalldd5044a2009-12-24 23:18:09 +00003712 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
John McCall29b5c282009-12-24 08:56:26 +00003713 }
3714 }
John McCall29b5c282009-12-24 08:56:26 +00003715 }
3716
3717 // Scientific formatting is pretty straightforward.
3718 if (FormatScientific) {
3719 exp += (NDigits - 1);
3720
3721 Str.push_back(buffer[NDigits-1]);
3722 Str.push_back('.');
3723 if (NDigits == 1)
3724 Str.push_back('0');
3725 else
3726 for (unsigned I = 1; I != NDigits; ++I)
3727 Str.push_back(buffer[NDigits-1-I]);
3728 Str.push_back('E');
3729
3730 Str.push_back(exp >= 0 ? '+' : '-');
3731 if (exp < 0) exp = -exp;
3732 SmallVector<char, 6> expbuf;
3733 do {
3734 expbuf.push_back((char) ('0' + (exp % 10)));
3735 exp /= 10;
3736 } while (exp);
3737 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3738 Str.push_back(expbuf[E-1-I]);
3739 return;
3740 }
3741
3742 // Non-scientific, positive exponents.
3743 if (exp >= 0) {
3744 for (unsigned I = 0; I != NDigits; ++I)
3745 Str.push_back(buffer[NDigits-1-I]);
3746 for (unsigned I = 0; I != (unsigned) exp; ++I)
3747 Str.push_back('0');
3748 return;
3749 }
3750
3751 // Non-scientific, negative exponents.
3752
3753 // The number of digits to the left of the decimal point.
3754 int NWholeDigits = exp + (int) NDigits;
3755
3756 unsigned I = 0;
3757 if (NWholeDigits > 0) {
3758 for (; I != (unsigned) NWholeDigits; ++I)
3759 Str.push_back(buffer[NDigits-I-1]);
3760 Str.push_back('.');
3761 } else {
3762 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3763
3764 Str.push_back('0');
3765 Str.push_back('.');
3766 for (unsigned Z = 1; Z != NZeros; ++Z)
3767 Str.push_back('0');
3768 }
3769
3770 for (; I != NDigits; ++I)
3771 Str.push_back(buffer[NDigits-I-1]);
3772}
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003773
3774bool APFloat::getExactInverse(APFloat *inv) const {
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003775 // Special floats and denormals have no exact inverse.
Michael Gottesman8136c382013-06-26 23:17:28 +00003776 if (!isFiniteNonZero())
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003777 return false;
3778
3779 // Check that the number is a power of two by making sure that only the
3780 // integer bit is set in the significand.
3781 if (significandLSB() != semantics->precision - 1)
3782 return false;
3783
3784 // Get the inverse.
3785 APFloat reciprocal(*semantics, 1ULL);
3786 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3787 return false;
3788
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003789 // Avoid multiplication with a denormal, it is not safe on all platforms and
3790 // may be slower than a normal division.
Benjamin Kramer6bef24f2013-06-01 11:26:33 +00003791 if (reciprocal.isDenormal())
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003792 return false;
3793
Michael Gottesman8136c382013-06-26 23:17:28 +00003794 assert(reciprocal.isFiniteNonZero() &&
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003795 reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3796
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003797 if (inv)
3798 *inv = reciprocal;
3799
3800 return true;
3801}
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003802
3803bool APFloat::isSignaling() const {
3804 if (!isNaN())
3805 return false;
3806
3807 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3808 // first bit of the trailing significand being 0.
3809 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3810}
3811
3812/// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3813///
3814/// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3815/// appropriate sign switching before/after the computation.
3816APFloat::opStatus APFloat::next(bool nextDown) {
3817 // If we are performing nextDown, swap sign so we have -x.
3818 if (nextDown)
3819 changeSign();
3820
3821 // Compute nextUp(x)
3822 opStatus result = opOK;
3823
3824 // Handle each float category separately.
3825 switch (category) {
3826 case fcInfinity:
3827 // nextUp(+inf) = +inf
3828 if (!isNegative())
3829 break;
3830 // nextUp(-inf) = -getLargest()
3831 makeLargest(true);
3832 break;
3833 case fcNaN:
3834 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3835 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3836 // change the payload.
3837 if (isSignaling()) {
3838 result = opInvalidOp;
Alp Tokercb402912014-01-24 17:20:08 +00003839 // For consistency, propagate the sign of the sNaN to the qNaN.
Craig Topperc10719f2014-04-07 04:17:22 +00003840 makeNaN(false, isNegative(), nullptr);
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003841 }
3842 break;
3843 case fcZero:
3844 // nextUp(pm 0) = +getSmallest()
3845 makeSmallest(false);
3846 break;
3847 case fcNormal:
3848 // nextUp(-getSmallest()) = -0
3849 if (isSmallest() && isNegative()) {
3850 APInt::tcSet(significandParts(), 0, partCount());
3851 category = fcZero;
3852 exponent = 0;
3853 break;
3854 }
3855
3856 // nextUp(getLargest()) == INFINITY
3857 if (isLargest() && !isNegative()) {
3858 APInt::tcSet(significandParts(), 0, partCount());
3859 category = fcInfinity;
3860 exponent = semantics->maxExponent + 1;
3861 break;
3862 }
3863
3864 // nextUp(normal) == normal + inc.
3865 if (isNegative()) {
3866 // If we are negative, we need to decrement the significand.
3867
3868 // We only cross a binade boundary that requires adjusting the exponent
3869 // if:
3870 // 1. exponent != semantics->minExponent. This implies we are not in the
3871 // smallest binade or are dealing with denormals.
3872 // 2. Our significand excluding the integral bit is all zeros.
3873 bool WillCrossBinadeBoundary =
3874 exponent != semantics->minExponent && isSignificandAllZeros();
3875
3876 // Decrement the significand.
3877 //
3878 // We always do this since:
Alp Tokerf907b892013-12-05 05:44:44 +00003879 // 1. If we are dealing with a non-binade decrement, by definition we
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003880 // just decrement the significand.
3881 // 2. If we are dealing with a normal -> normal binade decrement, since
3882 // we have an explicit integral bit the fact that all bits but the
3883 // integral bit are zero implies that subtracting one will yield a
3884 // significand with 0 integral bit and 1 in all other spots. Thus we
3885 // must just adjust the exponent and set the integral bit to 1.
3886 // 3. If we are dealing with a normal -> denormal binade decrement,
3887 // since we set the integral bit to 0 when we represent denormals, we
3888 // just decrement the significand.
3889 integerPart *Parts = significandParts();
3890 APInt::tcDecrement(Parts, partCount());
3891
3892 if (WillCrossBinadeBoundary) {
3893 // Our result is a normal number. Do the following:
3894 // 1. Set the integral bit to 1.
3895 // 2. Decrement the exponent.
3896 APInt::tcSetBit(Parts, semantics->precision - 1);
3897 exponent--;
3898 }
3899 } else {
3900 // If we are positive, we need to increment the significand.
3901
3902 // We only cross a binade boundary that requires adjusting the exponent if
3903 // the input is not a denormal and all of said input's significand bits
3904 // are set. If all of said conditions are true: clear the significand, set
3905 // the integral bit to 1, and increment the exponent. If we have a
3906 // denormal always increment since moving denormals and the numbers in the
3907 // smallest normal binade have the same exponent in our representation.
3908 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3909
3910 if (WillCrossBinadeBoundary) {
3911 integerPart *Parts = significandParts();
3912 APInt::tcSet(Parts, 0, partCount());
3913 APInt::tcSetBit(Parts, semantics->precision - 1);
3914 assert(exponent != semantics->maxExponent &&
3915 "We can not increment an exponent beyond the maxExponent allowed"
3916 " by the given floating point semantics.");
3917 exponent++;
3918 } else {
3919 incrementSignificand();
3920 }
3921 }
3922 break;
3923 }
3924
3925 // If we are performing nextDown, swap sign so we have -nextUp(-x)
3926 if (nextDown)
3927 changeSign();
3928
3929 return result;
3930}
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003931
3932void
3933APFloat::makeInf(bool Negative) {
3934 category = fcInfinity;
3935 sign = Negative;
3936 exponent = semantics->maxExponent + 1;
3937 APInt::tcSet(significandParts(), 0, partCount());
3938}
3939
3940void
3941APFloat::makeZero(bool Negative) {
3942 category = fcZero;
3943 sign = Negative;
3944 exponent = semantics->minExponent-1;
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003945 APInt::tcSet(significandParts(), 0, partCount());
3946}
3947
3948void APFloat::makeQuiet() {
3949 assert(isNaN());
3950 APInt::tcSetBit(significandParts(), semantics->precision - 2);
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003951}
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003952
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003953int llvm::ilogb(const APFloat &Arg) {
3954 if (Arg.isNaN())
3955 return APFloat::IEK_NaN;
3956 if (Arg.isZero())
3957 return APFloat::IEK_Zero;
3958 if (Arg.isInfinity())
3959 return APFloat::IEK_Inf;
3960 if (!Arg.isDenormal())
3961 return Arg.exponent;
3962
3963 APFloat Normalized(Arg);
3964 int SignificandBits = Arg.getSemantics().precision - 1;
3965
3966 Normalized.exponent += SignificandBits;
3967 Normalized.normalize(APFloat::rmNearestTiesToEven, lfExactlyZero);
3968 return Normalized.exponent - SignificandBits;
3969}
3970
Matt Arsenaultafa31cf2016-03-13 05:11:51 +00003971APFloat llvm::scalbn(APFloat X, int Exp, APFloat::roundingMode RoundingMode) {
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003972 auto MaxExp = X.getSemantics().maxExponent;
3973 auto MinExp = X.getSemantics().minExponent;
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003974
Matt Arsenaultafa31cf2016-03-13 05:11:51 +00003975 // If Exp is wildly out-of-scale, simply adding it to X.exponent will
3976 // overflow; clamp it to a safe range before adding, but ensure that the range
3977 // is large enough that the clamp does not change the result. The range we
3978 // need to support is the difference between the largest possible exponent and
3979 // the normalized exponent of half the smallest denormal.
3980
3981 int SignificandBits = X.getSemantics().precision - 1;
3982 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
3983
3984 // Clamp to one past the range ends to let normalize handle overlflow.
3985 X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement);
3986 X.normalize(RoundingMode, lfExactlyZero);
Matt Arsenaultea00b492016-03-23 23:51:45 +00003987 if (X.isNaN())
3988 X.makeQuiet();
Richard Trieu6ae37962015-04-30 23:07:00 +00003989 return X;
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003990}
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003991
3992APFloat llvm::frexp(const APFloat &Val, int &Exp, APFloat::roundingMode RM) {
3993 Exp = ilogb(Val);
3994
3995 // Quiet signalling nans.
3996 if (Exp == APFloat::IEK_NaN) {
3997 APFloat Quiet(Val);
3998 Quiet.makeQuiet();
3999 return Quiet;
4000 }
4001
4002 if (Exp == APFloat::IEK_Inf)
4003 return Val;
4004
4005 // 1 is added because frexp is defined to return a normalized fraction in
4006 // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0).
4007 Exp = Exp == APFloat::IEK_Zero ? 0 : Exp + 1;
4008 return scalbn(Val, -Exp, RM);
4009}