blob: c4c892f0352a046de9d625b313b7898e8639bd32 [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"
Mehdi Amini47b292d2016-04-16 07:51:28 +000017#include "llvm/ADT/ArrayRef.h"
Ted Kremenek6f30a072008-02-11 17:24:50 +000018#include "llvm/ADT/FoldingSet.h"
Chandler Carruth71bd7d12012-03-04 12:02:57 +000019#include "llvm/ADT/Hashing.h"
Jordan Rosee1f76582013-01-18 21:45:30 +000020#include "llvm/ADT/StringExtras.h"
Chandler Carruth71bd7d12012-03-04 12:02:57 +000021#include "llvm/ADT/StringRef.h"
Tim Shen44bde892016-12-12 21:59:30 +000022#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Dale Johannesen918c33c2007-08-24 05:08:11 +000024#include "llvm/Support/MathExtras.h"
Tim Shen44bde892016-12-12 21:59:30 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner17f71652008-08-17 07:19:36 +000026#include <cstring>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000027#include <limits.h>
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000028
Tim Shen601ba8c2017-01-27 02:11:07 +000029#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
30 do { \
31 if (usesLayout<IEEEFloat>(getSemantics())) \
32 return U.IEEE.METHOD_CALL; \
33 if (usesLayout<DoubleAPFloat>(getSemantics())) \
34 return U.Double.METHOD_CALL; \
35 llvm_unreachable("Unexpected semantics"); \
36 } while (false)
37
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000038using namespace llvm;
39
Craig Topper55229b72017-04-02 19:17:22 +000040// TODO: Remove these and use APInt qualified types directly.
41typedef APInt::WordType integerPart;
42const unsigned int integerPartWidth = APInt::APINT_BITS_PER_WORD;
43
Michael Gottesman9b877e12013-06-24 09:57:57 +000044/// A macro used to combine two fcCategory enums into one key which can be used
45/// in a switch statement to classify how the interaction of two APFloat's
46/// categories affects an operation.
47///
48/// TODO: If clang source code is ever allowed to use constexpr in its own
49/// codebase, change this into a static inline function.
50#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000051
Neil Booth8f1946f2007-10-03 22:26:02 +000052/* Assumed in hexadecimal significand parsing, and conversion to
53 hexadecimal strings. */
Benjamin Kramer7000ca32014-10-12 17:56:40 +000054static_assert(integerPartWidth % 4 == 0, "Part width must be divisible by 4!");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000055
56namespace llvm {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000057 /* Represents floating point arithmetic semantics. */
58 struct fltSemantics {
59 /* The largest E such that 2^E is representable; this matches the
60 definition of IEEE 754. */
Tim Shen85de51d2016-10-25 19:55:59 +000061 APFloatBase::ExponentType maxExponent;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000062
63 /* The smallest E such that 2^E is a normalized number; this
64 matches the definition of IEEE 754. */
Tim Shen85de51d2016-10-25 19:55:59 +000065 APFloatBase::ExponentType minExponent;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000066
67 /* Number of bits in the significand. This includes the integer
68 bit. */
Neil Booth146fdb32007-10-12 15:33:27 +000069 unsigned int precision;
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +000070
71 /* Number of bits actually used in the semantics. */
72 unsigned int sizeInBits;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +000073 };
74
Stephan Bergmann17c7f702016-12-14 11:57:17 +000075 static const fltSemantics semIEEEhalf = {15, -14, 11, 16};
76 static const fltSemantics semIEEEsingle = {127, -126, 24, 32};
77 static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64};
78 static const fltSemantics semIEEEquad = {16383, -16382, 113, 128};
79 static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80};
80 static const fltSemantics semBogus = {0, 0, 0, 0};
Dale Johannesen007aa372007-10-11 18:07:22 +000081
Tim Shen7117e692017-01-26 00:11:07 +000082 /* The IBM double-double semantics. Such a number consists of a pair of IEEE
83 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
84 (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
85 Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent
86 to each other, and two 11-bit exponents.
Michal Gorny21c12042017-01-03 16:33:50 +000087
88 Note: we need to make the value different from semBogus as otherwise
89 an unsafe optimization may collapse both values to a single address,
90 and we heavily rely on them having distinct addresses. */
91 static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0};
Tim Shen139a58f2016-10-27 22:52:40 +000092
Tim Shenfd1e5aa2017-01-23 22:39:35 +000093 /* These are legacy semantics for the fallback, inaccrurate implementation of
94 IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the
Tim Shen7117e692017-01-26 00:11:07 +000095 operation. It's equivalent to having an IEEE number with consecutive 106
96 bits of mantissa and 11 bits of exponent.
97
98 It's not equivalent to IBM double-double. For example, a legit IBM
99 double-double, 1 + epsilon:
100
101 1 + epsilon = 1 + (1 >> 1076)
102
103 is not representable by a consecutive 106 bits of mantissa.
Tim Shen139a58f2016-10-27 22:52:40 +0000104
Tim Shenfd1e5aa2017-01-23 22:39:35 +0000105 Currently, these semantics are used in the following way:
106
Tim Shen7117e692017-01-26 00:11:07 +0000107 semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
108 (64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
109 semPPCDoubleDoubleLegacy -> IEEE operations
Tim Shenfd1e5aa2017-01-23 22:39:35 +0000110
111 We use bitcastToAPInt() to get the bit representation (in APInt) of the
112 underlying IEEEdouble, then use the APInt constructor to construct the
113 legacy IEEE float.
114
115 TODO: Implement all operations in semPPCDoubleDouble, and delete these
116 semantics. */
117 static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53,
118 53 + 53, 128};
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000119
120 const fltSemantics &APFloatBase::IEEEhalf() {
121 return semIEEEhalf;
122 }
123 const fltSemantics &APFloatBase::IEEEsingle() {
124 return semIEEEsingle;
125 }
126 const fltSemantics &APFloatBase::IEEEdouble() {
127 return semIEEEdouble;
128 }
129 const fltSemantics &APFloatBase::IEEEquad() {
130 return semIEEEquad;
131 }
132 const fltSemantics &APFloatBase::x87DoubleExtended() {
133 return semX87DoubleExtended;
134 }
135 const fltSemantics &APFloatBase::Bogus() {
136 return semBogus;
137 }
138 const fltSemantics &APFloatBase::PPCDoubleDouble() {
139 return semPPCDoubleDouble;
140 }
Neil Boothb93d90e2007-10-12 16:02:31 +0000141
142 /* A tight upper bound on number of parts required to hold the value
143 pow(5, power) is
144
Neil Booth91305512007-10-15 15:00:55 +0000145 power * 815 / (351 * integerPartWidth) + 1
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000146
Neil Boothb93d90e2007-10-12 16:02:31 +0000147 However, whilst the result may require only this many parts,
148 because we are multiplying two values to get it, the
149 multiplication may require an extra part with the excess part
150 being zero (consider the trivial case of 1 * 1, tcFullMultiply
151 requires two parts to hold the single-part result). So we add an
152 extra one to guarantee enough space whilst multiplying. */
153 const unsigned int maxExponent = 16383;
154 const unsigned int maxPrecision = 113;
155 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
Neil Booth91305512007-10-15 15:00:55 +0000156 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
157 / (351 * integerPartWidth));
Tim Shen85de51d2016-10-25 19:55:59 +0000158
159 unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) {
160 return semantics.precision;
161 }
162 APFloatBase::ExponentType
163 APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) {
164 return semantics.maxExponent;
165 }
166 APFloatBase::ExponentType
167 APFloatBase::semanticsMinExponent(const fltSemantics &semantics) {
168 return semantics.minExponent;
169 }
170 unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) {
171 return semantics.sizeInBits;
172 }
173
174 unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) {
175 return Sem.sizeInBits;
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000176}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000177
Chris Lattner91702092009-03-12 23:59:55 +0000178/* A bunch of private, handy routines. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000179
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000180static inline unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000181partCountForBits(unsigned int bits)
182{
183 return ((bits) + integerPartWidth - 1) / integerPartWidth;
184}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000185
Chris Lattner91702092009-03-12 23:59:55 +0000186/* Returns 0U-9U. Return values >= 10U are not digits. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000187static inline unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000188decDigitValue(unsigned int c)
189{
190 return c - '0';
191}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000192
Chris Lattner91702092009-03-12 23:59:55 +0000193/* Return the value of a decimal exponent of the form
194 [+-]ddddddd.
Neil Booth4ed401b2007-10-14 10:16:12 +0000195
Chris Lattner91702092009-03-12 23:59:55 +0000196 If the exponent overflows, returns a large exponent with the
197 appropriate sign. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000198static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000199readExponent(StringRef::iterator begin, StringRef::iterator end)
Chris Lattner91702092009-03-12 23:59:55 +0000200{
201 bool isNegative;
202 unsigned int absExponent;
203 const unsigned int overlargeExponent = 24000; /* FIXME. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000204 StringRef::iterator p = begin;
205
206 assert(p != end && "Exponent has no digits");
Neil Booth4ed401b2007-10-14 10:16:12 +0000207
Chris Lattner91702092009-03-12 23:59:55 +0000208 isNegative = (*p == '-');
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000209 if (*p == '-' || *p == '+') {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000210 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000211 assert(p != end && "Exponent has no digits");
212 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000213
Chris Lattner91702092009-03-12 23:59:55 +0000214 absExponent = decDigitValue(*p++);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000215 assert(absExponent < 10U && "Invalid character in exponent");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000216
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000217 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000218 unsigned int value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000219
Chris Lattner91702092009-03-12 23:59:55 +0000220 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000221 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000222
Chris Lattner91702092009-03-12 23:59:55 +0000223 value += absExponent * 10;
224 if (absExponent >= overlargeExponent) {
225 absExponent = overlargeExponent;
Dale Johannesen370c77c2010-08-19 17:58:35 +0000226 p = end; /* outwit assert below */
Chris Lattner91702092009-03-12 23:59:55 +0000227 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000228 }
Chris Lattner91702092009-03-12 23:59:55 +0000229 absExponent = value;
230 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000231
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000232 assert(p == end && "Invalid exponent in exponent");
233
Chris Lattner91702092009-03-12 23:59:55 +0000234 if (isNegative)
235 return -(int) absExponent;
236 else
237 return (int) absExponent;
238}
239
240/* This is ugly and needs cleaning up, but I don't immediately see
241 how whilst remaining safe. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000242static int
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000243totalExponent(StringRef::iterator p, StringRef::iterator end,
244 int exponentAdjustment)
Chris Lattner91702092009-03-12 23:59:55 +0000245{
246 int unsignedExponent;
247 bool negative, overflow;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000248 int exponent = 0;
Chris Lattner91702092009-03-12 23:59:55 +0000249
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000250 assert(p != end && "Exponent has no digits");
251
Chris Lattner91702092009-03-12 23:59:55 +0000252 negative = *p == '-';
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000253 if (*p == '-' || *p == '+') {
Chris Lattner91702092009-03-12 23:59:55 +0000254 p++;
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000255 assert(p != end && "Exponent has no digits");
256 }
Chris Lattner91702092009-03-12 23:59:55 +0000257
258 unsignedExponent = 0;
259 overflow = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000260 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000261 unsigned int value;
262
263 value = decDigitValue(*p);
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000264 assert(value < 10U && "Invalid character in exponent");
Chris Lattner91702092009-03-12 23:59:55 +0000265
Chris Lattner91702092009-03-12 23:59:55 +0000266 unsignedExponent = unsignedExponent * 10 + value;
Richard Smith156d9202012-08-24 00:01:19 +0000267 if (unsignedExponent > 32767) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000268 overflow = true;
Richard Smith156d9202012-08-24 00:01:19 +0000269 break;
270 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000271 }
272
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000273 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000274 overflow = true;
275
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000276 if (!overflow) {
Chris Lattner91702092009-03-12 23:59:55 +0000277 exponent = unsignedExponent;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000278 if (negative)
Chris Lattner91702092009-03-12 23:59:55 +0000279 exponent = -exponent;
280 exponent += exponentAdjustment;
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000281 if (exponent > 32767 || exponent < -32768)
Chris Lattner91702092009-03-12 23:59:55 +0000282 overflow = true;
283 }
284
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000285 if (overflow)
Abramo Bagnaraa41d7ae2011-01-06 16:55:14 +0000286 exponent = negative ? -32768: 32767;
Chris Lattner91702092009-03-12 23:59:55 +0000287
288 return exponent;
289}
290
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000291static StringRef::iterator
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000292skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
293 StringRef::iterator *dot)
Chris Lattner91702092009-03-12 23:59:55 +0000294{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000295 StringRef::iterator p = begin;
296 *dot = end;
Nick Lewycky095b92e2014-09-06 01:16:42 +0000297 while (p != end && *p == '0')
Chris Lattner91702092009-03-12 23:59:55 +0000298 p++;
299
Nick Lewycky095b92e2014-09-06 01:16:42 +0000300 if (p != end && *p == '.') {
Chris Lattner91702092009-03-12 23:59:55 +0000301 *dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000302
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000303 assert(end - begin != 1 && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000304
Nick Lewycky095b92e2014-09-06 01:16:42 +0000305 while (p != end && *p == '0')
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000306 p++;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000307 }
308
Chris Lattner91702092009-03-12 23:59:55 +0000309 return p;
310}
Neil Booth4ed401b2007-10-14 10:16:12 +0000311
Chris Lattner91702092009-03-12 23:59:55 +0000312/* Given a normal decimal floating point number of the form
Neil Booth4ed401b2007-10-14 10:16:12 +0000313
Chris Lattner91702092009-03-12 23:59:55 +0000314 dddd.dddd[eE][+-]ddd
Neil Booth91305512007-10-15 15:00:55 +0000315
Chris Lattner91702092009-03-12 23:59:55 +0000316 where the decimal point and exponent are optional, fill out the
317 structure D. Exponent is appropriate if the significand is
318 treated as an integer, and normalizedExponent if the significand
319 is taken to have the decimal point after a single leading
320 non-zero digit.
Neil Booth4ed401b2007-10-14 10:16:12 +0000321
Chris Lattner91702092009-03-12 23:59:55 +0000322 If the value is zero, V->firstSigDigit points to a non-digit, and
323 the return exponent is zero.
324*/
325struct decimalInfo {
326 const char *firstSigDigit;
327 const char *lastSigDigit;
328 int exponent;
329 int normalizedExponent;
330};
Neil Booth4ed401b2007-10-14 10:16:12 +0000331
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000332static void
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000333interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
334 decimalInfo *D)
Chris Lattner91702092009-03-12 23:59:55 +0000335{
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000336 StringRef::iterator dot = end;
337 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
Neil Booth4ed401b2007-10-14 10:16:12 +0000338
Chris Lattner91702092009-03-12 23:59:55 +0000339 D->firstSigDigit = p;
340 D->exponent = 0;
341 D->normalizedExponent = 0;
342
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000343 for (; p != end; ++p) {
Chris Lattner91702092009-03-12 23:59:55 +0000344 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000345 assert(dot == end && "String contains multiple dots");
Chris Lattner91702092009-03-12 23:59:55 +0000346 dot = p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000347 if (p == end)
348 break;
Neil Booth4ed401b2007-10-14 10:16:12 +0000349 }
Chris Lattner91702092009-03-12 23:59:55 +0000350 if (decDigitValue(*p) >= 10U)
351 break;
Chris Lattner91702092009-03-12 23:59:55 +0000352 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000353
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000354 if (p != end) {
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000355 assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
356 assert(p != begin && "Significand has no digits");
357 assert((dot == end || p - begin != 1) && "Significand has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000358
359 /* p points to the first non-digit in the string */
Erick Tryzelaarda666c82009-08-20 23:30:43 +0000360 D->exponent = readExponent(p + 1, end);
Neil Booth4ed401b2007-10-14 10:16:12 +0000361
Chris Lattner91702092009-03-12 23:59:55 +0000362 /* Implied decimal point? */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000363 if (dot == end)
Chris Lattner91702092009-03-12 23:59:55 +0000364 dot = p;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000365 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000366
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000367 /* If number is all zeroes accept any exponent. */
368 if (p != D->firstSigDigit) {
Chris Lattner91702092009-03-12 23:59:55 +0000369 /* Drop insignificant trailing zeroes. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000370 if (p != begin) {
Neil Booth4ed401b2007-10-14 10:16:12 +0000371 do
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000372 do
373 p--;
374 while (p != begin && *p == '0');
375 while (p != begin && *p == '.');
376 }
Neil Booth4ed401b2007-10-14 10:16:12 +0000377
Chris Lattner91702092009-03-12 23:59:55 +0000378 /* Adjust the exponents for any decimal point. */
Michael Gottesman9dc98332013-06-24 04:06:23 +0000379 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
Chris Lattner91702092009-03-12 23:59:55 +0000380 D->normalizedExponent = (D->exponent +
Michael Gottesman9dc98332013-06-24 04:06:23 +0000381 static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
Chris Lattner91702092009-03-12 23:59:55 +0000382 - (dot > D->firstSigDigit && dot < p)));
Neil Booth4ed401b2007-10-14 10:16:12 +0000383 }
384
Chris Lattner91702092009-03-12 23:59:55 +0000385 D->lastSigDigit = p;
386}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000387
Chris Lattner91702092009-03-12 23:59:55 +0000388/* Return the trailing fraction of a hexadecimal number.
389 DIGITVALUE is the first hex digit of the fraction, P points to
390 the next digit. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000391static lostFraction
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000392trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
393 unsigned int digitValue)
Chris Lattner91702092009-03-12 23:59:55 +0000394{
395 unsigned int hexDigit;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000396
Chris Lattner91702092009-03-12 23:59:55 +0000397 /* If the first trailing digit isn't 0 or 8 we can work out the
398 fraction immediately. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000399 if (digitValue > 8)
Chris Lattner91702092009-03-12 23:59:55 +0000400 return lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000401 else if (digitValue < 8 && digitValue > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000402 return lfLessThanHalf;
Chris Lattner91702092009-03-12 23:59:55 +0000403
Eli Friedmand2eb07a2013-07-17 22:17:29 +0000404 // Otherwise we need to find the first non-zero digit.
405 while (p != end && (*p == '0' || *p == '.'))
Chris Lattner91702092009-03-12 23:59:55 +0000406 p++;
407
Erick Tryzelaar19f63b22009-08-16 23:36:19 +0000408 assert(p != end && "Invalid trailing hexadecimal fraction!");
409
Chris Lattner91702092009-03-12 23:59:55 +0000410 hexDigit = hexDigitValue(*p);
411
412 /* If we ran off the end it is exactly zero or one-half, otherwise
413 a little more. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000414 if (hexDigit == -1U)
Chris Lattner91702092009-03-12 23:59:55 +0000415 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
416 else
417 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
418}
419
420/* Return the fraction lost were a bignum truncated losing the least
421 significant BITS bits. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000422static lostFraction
Chris Lattner91702092009-03-12 23:59:55 +0000423lostFractionThroughTruncation(const integerPart *parts,
424 unsigned int partCount,
425 unsigned int bits)
426{
427 unsigned int lsb;
428
429 lsb = APInt::tcLSB(parts, partCount);
430
431 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000432 if (bits <= lsb)
Chris Lattner91702092009-03-12 23:59:55 +0000433 return lfExactlyZero;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000434 if (bits == lsb + 1)
Chris Lattner91702092009-03-12 23:59:55 +0000435 return lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000436 if (bits <= partCount * integerPartWidth &&
437 APInt::tcExtractBit(parts, bits - 1))
Chris Lattner91702092009-03-12 23:59:55 +0000438 return lfMoreThanHalf;
439
440 return lfLessThanHalf;
441}
442
443/* Shift DST right BITS bits noting lost fraction. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000444static lostFraction
Chris Lattner91702092009-03-12 23:59:55 +0000445shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
446{
447 lostFraction lost_fraction;
448
449 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
450
451 APInt::tcShiftRight(dst, parts, bits);
452
453 return lost_fraction;
454}
455
456/* Combine the effect of two lost fractions. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000457static lostFraction
Chris Lattner91702092009-03-12 23:59:55 +0000458combineLostFractions(lostFraction moreSignificant,
459 lostFraction lessSignificant)
460{
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000461 if (lessSignificant != lfExactlyZero) {
462 if (moreSignificant == lfExactlyZero)
Chris Lattner91702092009-03-12 23:59:55 +0000463 moreSignificant = lfLessThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000464 else if (moreSignificant == lfExactlyHalf)
Chris Lattner91702092009-03-12 23:59:55 +0000465 moreSignificant = lfMoreThanHalf;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000466 }
467
Chris Lattner91702092009-03-12 23:59:55 +0000468 return moreSignificant;
469}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000470
Chris Lattner91702092009-03-12 23:59:55 +0000471/* The error from the true value, in half-ulps, on multiplying two
472 floating point numbers, which differ from the value they
473 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
474 than the returned value.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000475
Chris Lattner91702092009-03-12 23:59:55 +0000476 See "How to Read Floating Point Numbers Accurately" by William D
477 Clinger. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000478static unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000479HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
480{
481 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000482
Chris Lattner91702092009-03-12 23:59:55 +0000483 if (HUerr1 + HUerr2 == 0)
484 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
485 else
486 return inexactMultiply + 2 * (HUerr1 + HUerr2);
487}
Neil Booth8f1946f2007-10-03 22:26:02 +0000488
Chris Lattner91702092009-03-12 23:59:55 +0000489/* The number of ulps from the boundary (zero, or half if ISNEAREST)
490 when the least significant BITS are truncated. BITS cannot be
491 zero. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000492static integerPart
Chris Lattner91702092009-03-12 23:59:55 +0000493ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
494{
495 unsigned int count, partBits;
496 integerPart part, boundary;
Neil Boothd3985922007-10-07 08:51:21 +0000497
Evan Cheng67c90212009-10-27 21:35:42 +0000498 assert(bits != 0);
Neil Booth8f1946f2007-10-03 22:26:02 +0000499
Chris Lattner91702092009-03-12 23:59:55 +0000500 bits--;
501 count = bits / integerPartWidth;
502 partBits = bits % integerPartWidth + 1;
Neil Boothb93d90e2007-10-12 16:02:31 +0000503
Chris Lattner91702092009-03-12 23:59:55 +0000504 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
Neil Boothb93d90e2007-10-12 16:02:31 +0000505
Chris Lattner91702092009-03-12 23:59:55 +0000506 if (isNearest)
507 boundary = (integerPart) 1 << (partBits - 1);
508 else
509 boundary = 0;
510
511 if (count == 0) {
512 if (part - boundary <= boundary - part)
513 return part - boundary;
Neil Boothb93d90e2007-10-12 16:02:31 +0000514 else
Chris Lattner91702092009-03-12 23:59:55 +0000515 return boundary - part;
Neil Boothb93d90e2007-10-12 16:02:31 +0000516 }
517
Chris Lattner91702092009-03-12 23:59:55 +0000518 if (part == boundary) {
519 while (--count)
520 if (parts[count])
521 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000522
Chris Lattner91702092009-03-12 23:59:55 +0000523 return parts[0];
524 } else if (part == boundary - 1) {
525 while (--count)
526 if (~parts[count])
527 return ~(integerPart) 0; /* A lot. */
Neil Boothb93d90e2007-10-12 16:02:31 +0000528
Chris Lattner91702092009-03-12 23:59:55 +0000529 return -parts[0];
530 }
Neil Boothb93d90e2007-10-12 16:02:31 +0000531
Chris Lattner91702092009-03-12 23:59:55 +0000532 return ~(integerPart) 0; /* A lot. */
533}
Neil Boothb93d90e2007-10-12 16:02:31 +0000534
Chris Lattner91702092009-03-12 23:59:55 +0000535/* Place pow(5, power) in DST, and return the number of parts used.
536 DST must be at least one part larger than size of the answer. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000537static unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000538powerOf5(integerPart *dst, unsigned int power)
539{
540 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
541 15625, 78125 };
Chris Lattnerb858c0e2009-03-13 00:24:01 +0000542 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
543 pow5s[0] = 78125 * 5;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000544
Chris Lattner0bf18692009-03-13 00:03:51 +0000545 unsigned int partsCount[16] = { 1 };
Chris Lattner91702092009-03-12 23:59:55 +0000546 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
547 unsigned int result;
Chris Lattner91702092009-03-12 23:59:55 +0000548 assert(power <= maxExponent);
549
550 p1 = dst;
551 p2 = scratch;
552
553 *p1 = firstEightPowers[power & 7];
554 power >>= 3;
555
556 result = 1;
557 pow5 = pow5s;
558
559 for (unsigned int n = 0; power; power >>= 1, n++) {
560 unsigned int pc;
561
562 pc = partsCount[n];
563
564 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
565 if (pc == 0) {
566 pc = partsCount[n - 1];
567 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
568 pc *= 2;
569 if (pow5[pc - 1] == 0)
570 pc--;
571 partsCount[n] = pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000572 }
573
Chris Lattner91702092009-03-12 23:59:55 +0000574 if (power & 1) {
575 integerPart *tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000576
Chris Lattner91702092009-03-12 23:59:55 +0000577 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
578 result += pc;
579 if (p2[result - 1] == 0)
580 result--;
Neil Boothb93d90e2007-10-12 16:02:31 +0000581
Chris Lattner91702092009-03-12 23:59:55 +0000582 /* Now result is in p1 with partsCount parts and p2 is scratch
583 space. */
Richard Trieu7a083812016-02-18 22:09:30 +0000584 tmp = p1;
585 p1 = p2;
586 p2 = tmp;
Neil Boothb93d90e2007-10-12 16:02:31 +0000587 }
588
Chris Lattner91702092009-03-12 23:59:55 +0000589 pow5 += pc;
Neil Boothb93d90e2007-10-12 16:02:31 +0000590 }
591
Chris Lattner91702092009-03-12 23:59:55 +0000592 if (p1 != dst)
593 APInt::tcAssign(dst, p1, result);
Neil Boothb93d90e2007-10-12 16:02:31 +0000594
Chris Lattner91702092009-03-12 23:59:55 +0000595 return result;
596}
Neil Boothb93d90e2007-10-12 16:02:31 +0000597
Chris Lattner91702092009-03-12 23:59:55 +0000598/* Zero at the end to avoid modular arithmetic when adding one; used
599 when rounding up during hexadecimal output. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000600static const char hexDigitsLower[] = "0123456789abcdef0";
601static const char hexDigitsUpper[] = "0123456789ABCDEF0";
602static const char infinityL[] = "infinity";
603static const char infinityU[] = "INFINITY";
604static const char NaNL[] = "nan";
605static const char NaNU[] = "NAN";
Neil Boothb93d90e2007-10-12 16:02:31 +0000606
Chris Lattner91702092009-03-12 23:59:55 +0000607/* Write out an integerPart in hexadecimal, starting with the most
608 significant nibble. Write out exactly COUNT hexdigits, return
609 COUNT. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000610static unsigned int
Chris Lattner91702092009-03-12 23:59:55 +0000611partAsHex (char *dst, integerPart part, unsigned int count,
612 const char *hexDigitChars)
613{
614 unsigned int result = count;
Neil Boothb93d90e2007-10-12 16:02:31 +0000615
Evan Cheng67c90212009-10-27 21:35:42 +0000616 assert(count != 0 && count <= integerPartWidth / 4);
Neil Boothb93d90e2007-10-12 16:02:31 +0000617
Chris Lattner91702092009-03-12 23:59:55 +0000618 part >>= (integerPartWidth - 4 * count);
619 while (count--) {
620 dst[count] = hexDigitChars[part & 0xf];
621 part >>= 4;
Neil Boothb93d90e2007-10-12 16:02:31 +0000622 }
623
Chris Lattner91702092009-03-12 23:59:55 +0000624 return result;
625}
Neil Booth8f1946f2007-10-03 22:26:02 +0000626
Chris Lattner91702092009-03-12 23:59:55 +0000627/* Write out an unsigned decimal integer. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000628static char *
Chris Lattner91702092009-03-12 23:59:55 +0000629writeUnsignedDecimal (char *dst, unsigned int n)
630{
631 char buff[40], *p;
Neil Booth8f1946f2007-10-03 22:26:02 +0000632
Chris Lattner91702092009-03-12 23:59:55 +0000633 p = buff;
634 do
635 *p++ = '0' + n % 10;
636 while (n /= 10);
Neil Booth8f1946f2007-10-03 22:26:02 +0000637
Chris Lattner91702092009-03-12 23:59:55 +0000638 do
639 *dst++ = *--p;
640 while (p != buff);
Neil Booth8f1946f2007-10-03 22:26:02 +0000641
Chris Lattner91702092009-03-12 23:59:55 +0000642 return dst;
643}
Neil Booth8f1946f2007-10-03 22:26:02 +0000644
Chris Lattner91702092009-03-12 23:59:55 +0000645/* Write out a signed decimal integer. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000646static char *
Chris Lattner91702092009-03-12 23:59:55 +0000647writeSignedDecimal (char *dst, int value)
648{
649 if (value < 0) {
650 *dst++ = '-';
651 dst = writeUnsignedDecimal(dst, -(unsigned) value);
652 } else
653 dst = writeUnsignedDecimal(dst, value);
Neil Booth8f1946f2007-10-03 22:26:02 +0000654
Chris Lattner91702092009-03-12 23:59:55 +0000655 return dst;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000656}
657
Tim Shen85de51d2016-10-25 19:55:59 +0000658namespace detail {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000659/* Constructors. */
Tim Shen85de51d2016-10-25 19:55:59 +0000660void IEEEFloat::initialize(const fltSemantics *ourSemantics) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000661 unsigned int count;
662
663 semantics = ourSemantics;
664 count = partCount();
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000665 if (count > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000666 significand.parts = new integerPart[count];
667}
668
Tim Shen85de51d2016-10-25 19:55:59 +0000669void IEEEFloat::freeSignificand() {
Manuel Klimekd0cf5b22013-06-03 13:03:05 +0000670 if (needsCleanup())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000671 delete [] significand.parts;
672}
673
Tim Shen85de51d2016-10-25 19:55:59 +0000674void IEEEFloat::assign(const IEEEFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000675 assert(semantics == rhs.semantics);
676
677 sign = rhs.sign;
678 category = rhs.category;
679 exponent = rhs.exponent;
Michael Gottesman8136c382013-06-26 23:17:28 +0000680 if (isFiniteNonZero() || category == fcNaN)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000681 copySignificand(rhs);
682}
683
Tim Shen85de51d2016-10-25 19:55:59 +0000684void IEEEFloat::copySignificand(const IEEEFloat &rhs) {
Michael Gottesman8136c382013-06-26 23:17:28 +0000685 assert(isFiniteNonZero() || category == fcNaN);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000686 assert(rhs.partCount() >= partCount());
687
688 APInt::tcAssign(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +0000689 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000690}
691
Neil Booth5fe658b2007-10-14 10:39:51 +0000692/* Make this number a NaN, with an arbitrary but deterministic value
Dale Johannesen1f864982009-01-21 20:32:55 +0000693 for the significand. If double or longer, this is a signalling NaN,
Mike Stump799bf582009-05-30 03:49:43 +0000694 which may not be ideal. If float, this is QNaN(0). */
Tim Shen85de51d2016-10-25 19:55:59 +0000695void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) {
Neil Booth5fe658b2007-10-14 10:39:51 +0000696 category = fcNaN;
John McCalldcb9a7a2010-02-28 02:51:25 +0000697 sign = Negative;
698
John McCallc12b1332010-02-28 12:49:50 +0000699 integerPart *significand = significandParts();
700 unsigned numParts = partCount();
701
John McCalldcb9a7a2010-02-28 02:51:25 +0000702 // Set the significand bits to the fill.
John McCallc12b1332010-02-28 12:49:50 +0000703 if (!fill || fill->getNumWords() < numParts)
704 APInt::tcSet(significand, 0, numParts);
705 if (fill) {
John McCallc6dbe302010-03-01 18:38:45 +0000706 APInt::tcAssign(significand, fill->getRawData(),
707 std::min(fill->getNumWords(), numParts));
John McCallc12b1332010-02-28 12:49:50 +0000708
709 // Zero out the excess bits of the significand.
710 unsigned bitsToPreserve = semantics->precision - 1;
711 unsigned part = bitsToPreserve / 64;
712 bitsToPreserve %= 64;
713 significand[part] &= ((1ULL << bitsToPreserve) - 1);
714 for (part++; part != numParts; ++part)
715 significand[part] = 0;
716 }
717
718 unsigned QNaNBit = semantics->precision - 2;
John McCalldcb9a7a2010-02-28 02:51:25 +0000719
720 if (SNaN) {
721 // We always have to clear the QNaN bit to make it an SNaN.
John McCallc12b1332010-02-28 12:49:50 +0000722 APInt::tcClearBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000723
724 // If there are no bits set in the payload, we have to set
725 // *something* to make it a NaN instead of an infinity;
726 // conventionally, this is the next bit down from the QNaN bit.
John McCallc12b1332010-02-28 12:49:50 +0000727 if (APInt::tcIsZero(significand, numParts))
728 APInt::tcSetBit(significand, QNaNBit - 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000729 } else {
730 // We always have to set the QNaN bit to make it a QNaN.
John McCallc12b1332010-02-28 12:49:50 +0000731 APInt::tcSetBit(significand, QNaNBit);
John McCalldcb9a7a2010-02-28 02:51:25 +0000732 }
John McCallc12b1332010-02-28 12:49:50 +0000733
734 // For x87 extended precision, we want to make a NaN, not a
735 // pseudo-NaN. Maybe we should expose the ability to make
736 // pseudo-NaNs?
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000737 if (semantics == &semX87DoubleExtended)
John McCallc12b1332010-02-28 12:49:50 +0000738 APInt::tcSetBit(significand, QNaNBit + 1);
John McCalldcb9a7a2010-02-28 02:51:25 +0000739}
740
Tim Shen85de51d2016-10-25 19:55:59 +0000741IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) {
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000742 if (this != &rhs) {
743 if (semantics != rhs.semantics) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000744 freeSignificand();
745 initialize(rhs.semantics);
746 }
747 assign(rhs);
748 }
749
750 return *this;
751}
752
Tim Shen85de51d2016-10-25 19:55:59 +0000753IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) {
Benjamin Kramer06f47782014-03-04 20:26:51 +0000754 freeSignificand();
755
756 semantics = rhs.semantics;
757 significand = rhs.significand;
758 exponent = rhs.exponent;
759 category = rhs.category;
760 sign = rhs.sign;
761
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000762 rhs.semantics = &semBogus;
Benjamin Kramer06f47782014-03-04 20:26:51 +0000763 return *this;
764}
765
Tim Shen85de51d2016-10-25 19:55:59 +0000766bool IEEEFloat::isDenormal() const {
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000767 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
Simon Pilgrima2849592017-03-20 13:53:59 +0000768 (APInt::tcExtractBit(significandParts(),
Shuxin Yang4fb504f2013-01-07 18:59:35 +0000769 semantics->precision - 1) == 0);
770}
771
Tim Shen85de51d2016-10-25 19:55:59 +0000772bool IEEEFloat::isSmallest() const {
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000773 // The smallest number by magnitude in our format will be the smallest
Michael Gottesmana7cc1242013-06-19 07:34:21 +0000774 // denormal, i.e. the floating point number with exponent being minimum
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000775 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000776 return isFiniteNonZero() && exponent == semantics->minExponent &&
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000777 significandMSB() == 0;
778}
779
Tim Shen85de51d2016-10-25 19:55:59 +0000780bool IEEEFloat::isSignificandAllOnes() const {
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000781 // Test if the significand excluding the integral bit is all ones. This allows
782 // us to test for binade boundaries.
783 const integerPart *Parts = significandParts();
784 const unsigned PartCount = partCount();
785 for (unsigned i = 0; i < PartCount - 1; i++)
786 if (~Parts[i])
787 return false;
788
789 // Set the unused high bits to all ones when we compare.
790 const unsigned NumHighBits =
791 PartCount*integerPartWidth - semantics->precision + 1;
792 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
793 "fill than integerPartWidth");
794 const integerPart HighBitFill =
795 ~integerPart(0) << (integerPartWidth - NumHighBits);
796 if (~(Parts[PartCount - 1] | HighBitFill))
797 return false;
798
799 return true;
800}
801
Tim Shen85de51d2016-10-25 19:55:59 +0000802bool IEEEFloat::isSignificandAllZeros() const {
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000803 // Test if the significand excluding the integral bit is all zeros. This
804 // allows us to test for binade boundaries.
805 const integerPart *Parts = significandParts();
806 const unsigned PartCount = partCount();
807
808 for (unsigned i = 0; i < PartCount - 1; i++)
809 if (Parts[i])
810 return false;
811
812 const unsigned NumHighBits =
813 PartCount*integerPartWidth - semantics->precision + 1;
814 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
815 "clear than integerPartWidth");
816 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
817
818 if (Parts[PartCount - 1] & HighBitMask)
819 return false;
820
821 return true;
822}
823
Tim Shen85de51d2016-10-25 19:55:59 +0000824bool IEEEFloat::isLargest() const {
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000825 // The largest number by magnitude in our format will be the floating point
826 // number with maximum exponent and with significand that is all ones.
Michael Gottesman3cb77ab2013-06-19 21:23:18 +0000827 return isFiniteNonZero() && exponent == semantics->maxExponent
Michael Gottesman0c622ea2013-05-30 18:07:13 +0000828 && isSignificandAllOnes();
829}
830
Tim Shen85de51d2016-10-25 19:55:59 +0000831bool IEEEFloat::isInteger() const {
Stephen Canon1bfc89b2015-11-16 21:52:48 +0000832 // This could be made more efficient; I'm going for obviously correct.
833 if (!isFinite()) return false;
Tim Shen85de51d2016-10-25 19:55:59 +0000834 IEEEFloat truncated = *this;
Stephen Canon1bfc89b2015-11-16 21:52:48 +0000835 truncated.roundToIntegral(rmTowardZero);
836 return compare(truncated) == cmpEqual;
837}
838
Tim Shen85de51d2016-10-25 19:55:59 +0000839bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const {
Dale Johannesena719a602007-08-24 00:56:33 +0000840 if (this == &rhs)
841 return true;
842 if (semantics != rhs.semantics ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000843 category != rhs.category ||
844 sign != rhs.sign)
Dale Johannesena719a602007-08-24 00:56:33 +0000845 return false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000846 if (category==fcZero || category==fcInfinity)
Dale Johannesena719a602007-08-24 00:56:33 +0000847 return true;
Benjamin Kramer103fc942015-08-21 16:44:52 +0000848
849 if (isFiniteNonZero() && exponent != rhs.exponent)
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000850 return false;
Benjamin Kramer103fc942015-08-21 16:44:52 +0000851
852 return std::equal(significandParts(), significandParts() + partCount(),
853 rhs.significandParts());
Dale Johannesena719a602007-08-24 00:56:33 +0000854}
855
Tim Shen85de51d2016-10-25 19:55:59 +0000856IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000857 initialize(&ourSemantics);
858 sign = 0;
Michael Gottesman30a90eb2013-07-27 21:49:21 +0000859 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000860 zeroSignificand();
861 exponent = ourSemantics.precision - 1;
862 significandParts()[0] = value;
863 normalize(rmNearestTiesToEven, lfExactlyZero);
864}
865
Tim Shen85de51d2016-10-25 19:55:59 +0000866IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) {
Chris Lattnerac6271e2009-09-17 01:08:43 +0000867 initialize(&ourSemantics);
868 category = fcZero;
869 sign = false;
870}
871
Tim Shenb49915482016-10-28 22:45:33 +0000872// Delegate to the previous constructor, because later copy constructor may
873// actually inspects category, which can't be garbage.
874IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag)
Tim Shen1bab9cf2016-10-29 00:51:41 +0000875 : IEEEFloat(ourSemantics) {}
Chris Lattnerac6271e2009-09-17 01:08:43 +0000876
Tim Shen85de51d2016-10-25 19:55:59 +0000877IEEEFloat::IEEEFloat(const IEEEFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000878 initialize(rhs.semantics);
879 assign(rhs);
880}
881
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000882IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&semBogus) {
Benjamin Kramer06f47782014-03-04 20:26:51 +0000883 *this = std::move(rhs);
884}
885
Tim Shen85de51d2016-10-25 19:55:59 +0000886IEEEFloat::~IEEEFloat() { freeSignificand(); }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000887
Tim Shen85de51d2016-10-25 19:55:59 +0000888unsigned int IEEEFloat::partCount() const {
Dale Johannesen146a0ea2007-09-20 23:47:58 +0000889 return partCountForBits(semantics->precision + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000890}
891
Tim Shen85de51d2016-10-25 19:55:59 +0000892const integerPart *IEEEFloat::significandParts() const {
893 return const_cast<IEEEFloat *>(this)->significandParts();
JF Bastiena1d3c242015-08-26 02:32:45 +0000894}
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000895
Tim Shen85de51d2016-10-25 19:55:59 +0000896integerPart *IEEEFloat::significandParts() {
Evan Cheng67c90212009-10-27 21:35:42 +0000897 if (partCount() > 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000898 return significand.parts;
899 else
900 return &significand.part;
901}
902
Tim Shen85de51d2016-10-25 19:55:59 +0000903void IEEEFloat::zeroSignificand() {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000904 APInt::tcSet(significandParts(), 0, partCount());
905}
906
907/* Increment an fcNormal floating point number's significand. */
Tim Shen85de51d2016-10-25 19:55:59 +0000908void IEEEFloat::incrementSignificand() {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000909 integerPart carry;
910
911 carry = APInt::tcIncrement(significandParts(), partCount());
912
913 /* Our callers should never cause us to overflow. */
914 assert(carry == 0);
Duncan Sandsa41634e2011-08-12 14:54:45 +0000915 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000916}
917
918/* Add the significand of the RHS. Returns the carry flag. */
Tim Shen85de51d2016-10-25 19:55:59 +0000919integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000920 integerPart *parts;
921
922 parts = significandParts();
923
924 assert(semantics == rhs.semantics);
925 assert(exponent == rhs.exponent);
926
927 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
928}
929
930/* Subtract the significand of the RHS with a borrow flag. Returns
931 the borrow flag. */
Tim Shen85de51d2016-10-25 19:55:59 +0000932integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs,
933 integerPart borrow) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000934 integerPart *parts;
935
936 parts = significandParts();
937
938 assert(semantics == rhs.semantics);
939 assert(exponent == rhs.exponent);
940
941 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
Neil Booth9acbf5a2007-09-26 21:33:42 +0000942 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000943}
944
945/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
946 on to the full-precision result of the multiplication. Returns the
947 lost fraction. */
Tim Shen85de51d2016-10-25 19:55:59 +0000948lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs,
949 const IEEEFloat *addend) {
Neil Booth9acbf5a2007-09-26 21:33:42 +0000950 unsigned int omsb; // One, not zero, based MSB.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000951 unsigned int partsCount, newPartsCount, precision;
952 integerPart *lhsSignificand;
953 integerPart scratch[4];
954 integerPart *fullSignificand;
955 lostFraction lost_fraction;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000956 bool ignored;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000957
958 assert(semantics == rhs.semantics);
959
960 precision = semantics->precision;
Lang Hames56c0eb22014-11-19 19:15:41 +0000961
962 // Allocate space for twice as many bits as the original significand, plus one
963 // extra bit for the addition to overflow into.
964 newPartsCount = partCountForBits(precision * 2 + 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000965
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000966 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000967 fullSignificand = new integerPart[newPartsCount];
968 else
969 fullSignificand = scratch;
970
971 lhsSignificand = significandParts();
972 partsCount = partCount();
973
974 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
Neil Booth0ea72a92007-10-06 00:24:48 +0000975 rhs.significandParts(), partsCount, partsCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000976
977 lost_fraction = lfExactlyZero;
978 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
979 exponent += rhs.exponent;
980
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000981 // Assume the operands involved in the multiplication are single-precision
982 // FP, and the two multiplicants are:
983 // *this = a23 . a22 ... a0 * 2^e1
984 // rhs = b23 . b22 ... b0 * 2^e2
985 // the result of multiplication is:
Lang Hames56c0eb22014-11-19 19:15:41 +0000986 // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2)
Simon Pilgrima2849592017-03-20 13:53:59 +0000987 // Note that there are three significant bits at the left-hand side of the
Lang Hames56c0eb22014-11-19 19:15:41 +0000988 // radix point: two for the multiplication, and an overflow bit for the
989 // addition (that will always be zero at this point). Move the radix point
990 // toward left by two bits, and adjust exponent accordingly.
991 exponent += 2;
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000992
Hal Finkel171c2ec2014-10-14 19:23:07 +0000993 if (addend && addend->isNonZero()) {
Simon Pilgrima2849592017-03-20 13:53:59 +0000994 // The intermediate result of the multiplication has "2 * precision"
Shuxin Yangbbddbac2013-05-13 18:03:12 +0000995 // signicant bit; adjust the addend to be consistent with mul result.
996 //
Chris Lattnerfe02c1f2007-08-20 22:49:32 +0000997 Significand savedSignificand = significand;
998 const fltSemantics *savedSemantics = semantics;
999 fltSemantics extendedSemantics;
1000 opStatus status;
1001 unsigned int extendedPrecision;
1002
Lang Hames56c0eb22014-11-19 19:15:41 +00001003 // Normalize our MSB to one below the top bit to allow for overflow.
1004 extendedPrecision = 2 * precision + 1;
1005 if (omsb != extendedPrecision - 1) {
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001006 assert(extendedPrecision > omsb);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001007 APInt::tcShiftLeft(fullSignificand, newPartsCount,
Lang Hames56c0eb22014-11-19 19:15:41 +00001008 (extendedPrecision - 1) - omsb);
1009 exponent -= (extendedPrecision - 1) - omsb;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001010 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001011
1012 /* Create new semantics. */
1013 extendedSemantics = *semantics;
1014 extendedSemantics.precision = extendedPrecision;
1015
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001016 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001017 significand.part = fullSignificand[0];
1018 else
1019 significand.parts = fullSignificand;
1020 semantics = &extendedSemantics;
1021
Tim Shen85de51d2016-10-25 19:55:59 +00001022 IEEEFloat extendedAddend(*addend);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001023 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001024 assert(status == opOK);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001025 (void)status;
Lang Hames56c0eb22014-11-19 19:15:41 +00001026
1027 // Shift the significand of the addend right by one bit. This guarantees
1028 // that the high bit of the significand is zero (same as fullSignificand),
1029 // so the addition will overflow (if it does overflow at all) into the top bit.
1030 lost_fraction = extendedAddend.shiftSignificandRight(1);
1031 assert(lost_fraction == lfExactlyZero &&
1032 "Lost precision while shifting addend for fused-multiply-add.");
1033
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001034 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1035
1036 /* Restore our state. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001037 if (newPartsCount == 1)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001038 fullSignificand[0] = significand.part;
1039 significand = savedSignificand;
1040 semantics = savedSemantics;
1041
1042 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1043 }
1044
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001045 // Convert the result having "2 * precision" significant-bits back to the one
Simon Pilgrima2849592017-03-20 13:53:59 +00001046 // having "precision" significant-bits. First, move the radix point from
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001047 // poision "2*precision - 1" to "precision - 1". The exponent need to be
1048 // adjusted by "2*precision - 1" - "precision - 1" = "precision".
Lang Hames56c0eb22014-11-19 19:15:41 +00001049 exponent -= precision + 1;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001050
Shuxin Yangbbddbac2013-05-13 18:03:12 +00001051 // In case MSB resides at the left-hand side of radix point, shift the
1052 // mantissa right by some amount to make sure the MSB reside right before
1053 // the radix point (i.e. "MSB . rest-significant-bits").
1054 //
1055 // Note that the result is not normalized when "omsb < precision". So, the
Tim Shen85de51d2016-10-25 19:55:59 +00001056 // caller needs to call IEEEFloat::normalize() if normalized value is
1057 // expected.
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001058 if (omsb > precision) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001059 unsigned int bits, significantParts;
1060 lostFraction lf;
1061
1062 bits = omsb - precision;
1063 significantParts = partCountForBits(omsb);
1064 lf = shiftRight(fullSignificand, significantParts, bits);
1065 lost_fraction = combineLostFractions(lf, lost_fraction);
1066 exponent += bits;
1067 }
1068
1069 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1070
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001071 if (newPartsCount > 4)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001072 delete [] fullSignificand;
1073
1074 return lost_fraction;
1075}
1076
1077/* Multiply the significands of LHS and RHS to DST. */
Tim Shen85de51d2016-10-25 19:55:59 +00001078lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001079 unsigned int bit, i, partsCount;
1080 const integerPart *rhsSignificand;
1081 integerPart *lhsSignificand, *dividend, *divisor;
1082 integerPart scratch[4];
1083 lostFraction lost_fraction;
1084
1085 assert(semantics == rhs.semantics);
1086
1087 lhsSignificand = significandParts();
1088 rhsSignificand = rhs.significandParts();
1089 partsCount = partCount();
1090
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001091 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001092 dividend = new integerPart[partsCount * 2];
1093 else
1094 dividend = scratch;
1095
1096 divisor = dividend + partsCount;
1097
1098 /* Copy the dividend and divisor as they will be modified in-place. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001099 for (i = 0; i < partsCount; i++) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001100 dividend[i] = lhsSignificand[i];
1101 divisor[i] = rhsSignificand[i];
1102 lhsSignificand[i] = 0;
1103 }
1104
1105 exponent -= rhs.exponent;
1106
1107 unsigned int precision = semantics->precision;
1108
1109 /* Normalize the divisor. */
1110 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001111 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001112 exponent += bit;
1113 APInt::tcShiftLeft(divisor, partsCount, bit);
1114 }
1115
1116 /* Normalize the dividend. */
1117 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001118 if (bit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001119 exponent -= bit;
1120 APInt::tcShiftLeft(dividend, partsCount, bit);
1121 }
1122
Neil Boothb93d90e2007-10-12 16:02:31 +00001123 /* Ensure the dividend >= divisor initially for the loop below.
1124 Incidentally, this means that the division loop below is
1125 guaranteed to set the integer bit to one. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001126 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001127 exponent--;
1128 APInt::tcShiftLeft(dividend, partsCount, 1);
1129 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
1130 }
1131
1132 /* Long division. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001133 for (bit = precision; bit; bit -= 1) {
1134 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001135 APInt::tcSubtract(dividend, divisor, 0, partsCount);
1136 APInt::tcSetBit(lhsSignificand, bit - 1);
1137 }
1138
1139 APInt::tcShiftLeft(dividend, partsCount, 1);
1140 }
1141
1142 /* Figure out the lost fraction. */
1143 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1144
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001145 if (cmp > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001146 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001147 else if (cmp == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001148 lost_fraction = lfExactlyHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001149 else if (APInt::tcIsZero(dividend, partsCount))
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001150 lost_fraction = lfExactlyZero;
1151 else
1152 lost_fraction = lfLessThanHalf;
1153
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001154 if (partsCount > 2)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001155 delete [] dividend;
1156
1157 return lost_fraction;
1158}
1159
Tim Shen85de51d2016-10-25 19:55:59 +00001160unsigned int IEEEFloat::significandMSB() const {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001161 return APInt::tcMSB(significandParts(), partCount());
1162}
1163
Tim Shen85de51d2016-10-25 19:55:59 +00001164unsigned int IEEEFloat::significandLSB() const {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001165 return APInt::tcLSB(significandParts(), partCount());
1166}
1167
1168/* Note that a zero result is NOT normalized to fcZero. */
Tim Shen85de51d2016-10-25 19:55:59 +00001169lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001170 /* Our exponent should not overflow. */
Michael Gottesman9dc98332013-06-24 04:06:23 +00001171 assert((ExponentType) (exponent + bits) >= exponent);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001172
1173 exponent += bits;
1174
1175 return shiftRight(significandParts(), partCount(), bits);
1176}
1177
1178/* Shift the significand left BITS bits, subtract BITS from its exponent. */
Tim Shen85de51d2016-10-25 19:55:59 +00001179void IEEEFloat::shiftSignificandLeft(unsigned int bits) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001180 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
Tim Shen85de51d2016-10-25 19:55:59 +00001192IEEEFloat::cmpResult
1193IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001194 int compare;
1195
1196 assert(semantics == rhs.semantics);
Michael Gottesman8136c382013-06-26 23:17:28 +00001197 assert(isFiniteNonZero());
1198 assert(rhs.isFiniteNonZero());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001199
1200 compare = exponent - rhs.exponent;
1201
1202 /* If exponents are equal, do an unsigned bignum comparison of the
1203 significands. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001204 if (compare == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001205 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001206 partCount());
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001207
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001208 if (compare > 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001209 return cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001210 else if (compare < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001211 return cmpLessThan;
1212 else
1213 return cmpEqual;
1214}
1215
1216/* Handle overflow. Sign is preserved. We either become infinity or
1217 the largest finite number. */
Tim Shen85de51d2016-10-25 19:55:59 +00001218IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001219 /* Infinity? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001220 if (rounding_mode == rmNearestTiesToEven ||
1221 rounding_mode == rmNearestTiesToAway ||
1222 (rounding_mode == rmTowardPositive && !sign) ||
1223 (rounding_mode == rmTowardNegative && sign)) {
1224 category = fcInfinity;
1225 return (opStatus) (opOverflow | opInexact);
1226 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001227
1228 /* Otherwise we become the largest finite number. */
1229 category = fcNormal;
1230 exponent = semantics->maxExponent;
1231 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
Neil Booth9acbf5a2007-09-26 21:33:42 +00001232 semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001233
1234 return opInexact;
1235}
1236
Neil Booth1ca1f802007-10-03 15:16:41 +00001237/* Returns TRUE if, when truncating the current number, with BIT the
1238 new LSB, with the given lost fraction and rounding mode, the result
1239 would need to be rounded away from zero (i.e., by increasing the
1240 signficand). This routine must work for fcZero of both signs, and
1241 fcNormal numbers. */
Tim Shen85de51d2016-10-25 19:55:59 +00001242bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode,
1243 lostFraction lost_fraction,
1244 unsigned int bit) const {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001245 /* NaNs and infinities should not have lost fractions. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001246 assert(isFiniteNonZero() || category == fcZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001247
Neil Booth1ca1f802007-10-03 15:16:41 +00001248 /* Current callers never pass this so we don't handle it. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001249 assert(lost_fraction != lfExactlyZero);
1250
Mike Stump889285d2009-05-13 23:23:20 +00001251 switch (rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001252 case rmNearestTiesToAway:
1253 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1254
1255 case rmNearestTiesToEven:
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001256 if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001257 return true;
1258
1259 /* Our zeroes don't have a significand to test. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001260 if (lost_fraction == lfExactlyHalf && category != fcZero)
Neil Booth1ca1f802007-10-03 15:16:41 +00001261 return APInt::tcExtractBit(significandParts(), bit);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001262
1263 return false;
1264
1265 case rmTowardZero:
1266 return false;
1267
1268 case rmTowardPositive:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001269 return !sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001270
1271 case rmTowardNegative:
David Blaikiedc3f01e2015-03-09 01:57:13 +00001272 return sign;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001273 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00001274 llvm_unreachable("Invalid rounding mode found");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001275}
1276
Tim Shen85de51d2016-10-25 19:55:59 +00001277IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode,
1278 lostFraction lost_fraction) {
Neil Booth9acbf5a2007-09-26 21:33:42 +00001279 unsigned int omsb; /* One, not zero, based MSB. */
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001280 int exponentChange;
1281
Michael Gottesman8136c382013-06-26 23:17:28 +00001282 if (!isFiniteNonZero())
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001283 return opOK;
1284
1285 /* Before rounding normalize the exponent of fcNormal numbers. */
1286 omsb = significandMSB() + 1;
1287
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001288 if (omsb) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001289 /* OMSB is numbered from 1. We want to place it in the integer
Nick Lewyckyf66daac2011-10-03 21:30:08 +00001290 bit numbered PRECISION if possible, with a compensating change in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001291 the exponent. */
1292 exponentChange = omsb - semantics->precision;
1293
1294 /* If the resulting exponent is too high, overflow according to
1295 the rounding mode. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001296 if (exponent + exponentChange > semantics->maxExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001297 return handleOverflow(rounding_mode);
1298
1299 /* Subnormal numbers have exponent minExponent, and their MSB
1300 is forced based on that. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001301 if (exponent + exponentChange < semantics->minExponent)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001302 exponentChange = semantics->minExponent - exponent;
1303
1304 /* Shifting left is easy as we don't lose precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001305 if (exponentChange < 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001306 assert(lost_fraction == lfExactlyZero);
1307
1308 shiftSignificandLeft(-exponentChange);
1309
1310 return opOK;
1311 }
1312
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001313 if (exponentChange > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001314 lostFraction lf;
1315
1316 /* Shift right and capture any new lost fraction. */
1317 lf = shiftSignificandRight(exponentChange);
1318
1319 lost_fraction = combineLostFractions(lf, lost_fraction);
1320
1321 /* Keep OMSB up-to-date. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001322 if (omsb > (unsigned) exponentChange)
Neil Boothb93d90e2007-10-12 16:02:31 +00001323 omsb -= exponentChange;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001324 else
Neil Booth9acbf5a2007-09-26 21:33:42 +00001325 omsb = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001326 }
1327 }
1328
1329 /* Now round the number according to rounding_mode given the lost
1330 fraction. */
1331
1332 /* As specified in IEEE 754, since we do not trap we do not report
1333 underflow for exact results. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001334 if (lost_fraction == lfExactlyZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001335 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001336 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001337 category = fcZero;
1338
1339 return opOK;
1340 }
1341
1342 /* Increment the significand if we're rounding away from zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001343 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1344 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001345 exponent = semantics->minExponent;
1346
1347 incrementSignificand();
1348 omsb = significandMSB() + 1;
1349
1350 /* Did the significand increment overflow? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001351 if (omsb == (unsigned) semantics->precision + 1) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001352 /* Renormalize by incrementing the exponent and shifting our
Neil Booth9acbf5a2007-09-26 21:33:42 +00001353 significand right one. However if we already have the
1354 maximum exponent we overflow to infinity. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001355 if (exponent == semantics->maxExponent) {
Neil Booth9acbf5a2007-09-26 21:33:42 +00001356 category = fcInfinity;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001357
Neil Booth9acbf5a2007-09-26 21:33:42 +00001358 return (opStatus) (opOverflow | opInexact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001359 }
1360
1361 shiftSignificandRight(1);
1362
1363 return opInexact;
1364 }
1365 }
1366
1367 /* The normal case - we were and are not denormal, and any
1368 significand increment above didn't overflow. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001369 if (omsb == semantics->precision)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001370 return opInexact;
1371
1372 /* We have a non-zero denormal. */
1373 assert(omsb < semantics->precision);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001374
1375 /* Canonicalize zeroes. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001376 if (omsb == 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001377 category = fcZero;
1378
1379 /* The fcZero case is a denormal that underflowed to zero. */
1380 return (opStatus) (opUnderflow | opInexact);
1381}
1382
Tim Shen85de51d2016-10-25 19:55:59 +00001383IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs,
1384 bool subtract) {
Michael Gottesman9b877e12013-06-24 09:57:57 +00001385 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001386 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001387 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001388
Michael Gottesman9b877e12013-06-24 09:57:57 +00001389 case PackCategoriesIntoKey(fcNaN, fcZero):
1390 case PackCategoriesIntoKey(fcNaN, fcNormal):
1391 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1392 case PackCategoriesIntoKey(fcNaN, fcNaN):
1393 case PackCategoriesIntoKey(fcNormal, fcZero):
1394 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1395 case PackCategoriesIntoKey(fcInfinity, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001396 return opOK;
1397
Michael Gottesman9b877e12013-06-24 09:57:57 +00001398 case PackCategoriesIntoKey(fcZero, fcNaN):
1399 case PackCategoriesIntoKey(fcNormal, fcNaN):
1400 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Stephen Canond3278282014-06-08 16:53:31 +00001401 // We need to be sure to flip the sign here for subtraction because we
1402 // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1403 sign = rhs.sign ^ subtract;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001404 category = fcNaN;
1405 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001406 return opOK;
1407
Michael Gottesman9b877e12013-06-24 09:57:57 +00001408 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1409 case PackCategoriesIntoKey(fcZero, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001410 category = fcInfinity;
1411 sign = rhs.sign ^ subtract;
1412 return opOK;
1413
Michael Gottesman9b877e12013-06-24 09:57:57 +00001414 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001415 assign(rhs);
1416 sign = rhs.sign ^ subtract;
1417 return opOK;
1418
Michael Gottesman9b877e12013-06-24 09:57:57 +00001419 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001420 /* Sign depends on rounding mode; handled by caller. */
1421 return opOK;
1422
Michael Gottesman9b877e12013-06-24 09:57:57 +00001423 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001424 /* Differently signed infinities can only be validly
1425 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001426 if (((sign ^ rhs.sign)!=0) != subtract) {
Neil Booth5fe658b2007-10-14 10:39:51 +00001427 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001428 return opInvalidOp;
1429 }
1430
1431 return opOK;
1432
Michael Gottesman9b877e12013-06-24 09:57:57 +00001433 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001434 return opDivByZero;
1435 }
1436}
1437
1438/* Add or subtract two normal numbers. */
Tim Shen85de51d2016-10-25 19:55:59 +00001439lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs,
1440 bool subtract) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001441 integerPart carry;
1442 lostFraction lost_fraction;
1443 int bits;
1444
1445 /* Determine if the operation on the absolute values is effectively
1446 an addition or subtraction. */
Aaron Ballmand5cc45f2015-03-24 12:47:51 +00001447 subtract ^= static_cast<bool>(sign ^ rhs.sign);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001448
1449 /* Are we bigger exponent-wise than the RHS? */
1450 bits = exponent - rhs.exponent;
1451
1452 /* Subtraction is more subtle than one might naively expect. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001453 if (subtract) {
Tim Shen85de51d2016-10-25 19:55:59 +00001454 IEEEFloat temp_rhs(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001455 bool reverse;
1456
Chris Lattner3da18eb2007-08-24 03:02:34 +00001457 if (bits == 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001458 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1459 lost_fraction = lfExactlyZero;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001460 } else if (bits > 0) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001461 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1462 shiftSignificandLeft(1);
1463 reverse = false;
Chris Lattner3da18eb2007-08-24 03:02:34 +00001464 } else {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001465 lost_fraction = shiftSignificandRight(-bits - 1);
1466 temp_rhs.shiftSignificandLeft(1);
1467 reverse = true;
1468 }
1469
Chris Lattner3da18eb2007-08-24 03:02:34 +00001470 if (reverse) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001471 carry = temp_rhs.subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001472 (*this, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001473 copySignificand(temp_rhs);
1474 sign = !sign;
1475 } else {
1476 carry = subtractSignificand
Neil Booth9acbf5a2007-09-26 21:33:42 +00001477 (temp_rhs, lost_fraction != lfExactlyZero);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001478 }
1479
1480 /* Invert the lost fraction - it was on the RHS and
1481 subtracted. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001482 if (lost_fraction == lfLessThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001483 lost_fraction = lfMoreThanHalf;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001484 else if (lost_fraction == lfMoreThanHalf)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001485 lost_fraction = lfLessThanHalf;
1486
1487 /* The code above is intended to ensure that no borrow is
1488 necessary. */
1489 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001490 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001491 } else {
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001492 if (bits > 0) {
Tim Shen85de51d2016-10-25 19:55:59 +00001493 IEEEFloat temp_rhs(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001494
1495 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1496 carry = addSignificand(temp_rhs);
1497 } else {
1498 lost_fraction = shiftSignificandRight(-bits);
1499 carry = addSignificand(rhs);
1500 }
1501
1502 /* We have a guard bit; generating a carry cannot happen. */
1503 assert(!carry);
Duncan Sandsa41634e2011-08-12 14:54:45 +00001504 (void)carry;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001505 }
1506
1507 return lost_fraction;
1508}
1509
Tim Shen85de51d2016-10-25 19:55:59 +00001510IEEEFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) {
Michael Gottesman9b877e12013-06-24 09:57:57 +00001511 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001512 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001513 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001514
Michael Gottesman9b877e12013-06-24 09:57:57 +00001515 case PackCategoriesIntoKey(fcNaN, fcZero):
1516 case PackCategoriesIntoKey(fcNaN, fcNormal):
1517 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1518 case PackCategoriesIntoKey(fcNaN, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001519 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001520 return opOK;
1521
Michael Gottesman9b877e12013-06-24 09:57:57 +00001522 case PackCategoriesIntoKey(fcZero, fcNaN):
1523 case PackCategoriesIntoKey(fcNormal, fcNaN):
1524 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001525 sign = false;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001526 category = fcNaN;
1527 copySignificand(rhs);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001528 return opOK;
1529
Michael Gottesman9b877e12013-06-24 09:57:57 +00001530 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1531 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1532 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001533 category = fcInfinity;
1534 return opOK;
1535
Michael Gottesman9b877e12013-06-24 09:57:57 +00001536 case PackCategoriesIntoKey(fcZero, fcNormal):
1537 case PackCategoriesIntoKey(fcNormal, fcZero):
1538 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001539 category = fcZero;
1540 return opOK;
1541
Michael Gottesman9b877e12013-06-24 09:57:57 +00001542 case PackCategoriesIntoKey(fcZero, fcInfinity):
1543 case PackCategoriesIntoKey(fcInfinity, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001544 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001545 return opInvalidOp;
1546
Michael Gottesman9b877e12013-06-24 09:57:57 +00001547 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001548 return opOK;
1549 }
1550}
1551
Tim Shen85de51d2016-10-25 19:55:59 +00001552IEEEFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) {
Michael Gottesman9b877e12013-06-24 09:57:57 +00001553 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001554 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001555 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001556
Michael Gottesman9b877e12013-06-24 09:57:57 +00001557 case PackCategoriesIntoKey(fcZero, fcNaN):
1558 case PackCategoriesIntoKey(fcNormal, fcNaN):
1559 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001560 category = fcNaN;
1561 copySignificand(rhs);
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001562 case PackCategoriesIntoKey(fcNaN, fcZero):
1563 case PackCategoriesIntoKey(fcNaN, fcNormal):
1564 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1565 case PackCategoriesIntoKey(fcNaN, fcNaN):
1566 sign = false;
1567 case PackCategoriesIntoKey(fcInfinity, fcZero):
1568 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1569 case PackCategoriesIntoKey(fcZero, fcInfinity):
1570 case PackCategoriesIntoKey(fcZero, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001571 return opOK;
1572
Michael Gottesman9b877e12013-06-24 09:57:57 +00001573 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001574 category = fcZero;
1575 return opOK;
1576
Michael Gottesman9b877e12013-06-24 09:57:57 +00001577 case PackCategoriesIntoKey(fcNormal, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001578 category = fcInfinity;
1579 return opDivByZero;
1580
Michael Gottesman9b877e12013-06-24 09:57:57 +00001581 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1582 case PackCategoriesIntoKey(fcZero, fcZero):
Neil Booth5fe658b2007-10-14 10:39:51 +00001583 makeNaN();
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001584 return opInvalidOp;
1585
Michael Gottesman9b877e12013-06-24 09:57:57 +00001586 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001587 return opOK;
1588 }
1589}
1590
Tim Shen85de51d2016-10-25 19:55:59 +00001591IEEEFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) {
Michael Gottesman9b877e12013-06-24 09:57:57 +00001592 switch (PackCategoriesIntoKey(category, rhs.category)) {
Dale Johannesenb5721632009-01-21 00:35:19 +00001593 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001594 llvm_unreachable(nullptr);
Dale Johannesenb5721632009-01-21 00:35:19 +00001595
Michael Gottesman9b877e12013-06-24 09:57:57 +00001596 case PackCategoriesIntoKey(fcNaN, fcZero):
1597 case PackCategoriesIntoKey(fcNaN, fcNormal):
1598 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1599 case PackCategoriesIntoKey(fcNaN, fcNaN):
1600 case PackCategoriesIntoKey(fcZero, fcInfinity):
1601 case PackCategoriesIntoKey(fcZero, fcNormal):
1602 case PackCategoriesIntoKey(fcNormal, fcInfinity):
Dale Johannesenb5721632009-01-21 00:35:19 +00001603 return opOK;
1604
Michael Gottesman9b877e12013-06-24 09:57:57 +00001605 case PackCategoriesIntoKey(fcZero, fcNaN):
1606 case PackCategoriesIntoKey(fcNormal, fcNaN):
1607 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Michael Gottesmanb0e688e2013-07-27 21:49:25 +00001608 sign = false;
Dale Johannesenb5721632009-01-21 00:35:19 +00001609 category = fcNaN;
1610 copySignificand(rhs);
1611 return opOK;
1612
Michael Gottesman9b877e12013-06-24 09:57:57 +00001613 case PackCategoriesIntoKey(fcNormal, fcZero):
1614 case PackCategoriesIntoKey(fcInfinity, fcZero):
1615 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1616 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1617 case PackCategoriesIntoKey(fcZero, fcZero):
Dale Johannesenb5721632009-01-21 00:35:19 +00001618 makeNaN();
1619 return opInvalidOp;
1620
Michael Gottesman9b877e12013-06-24 09:57:57 +00001621 case PackCategoriesIntoKey(fcNormal, fcNormal):
Dale Johannesenb5721632009-01-21 00:35:19 +00001622 return opOK;
1623 }
1624}
1625
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001626/* Change sign. */
Tim Shen85de51d2016-10-25 19:55:59 +00001627void IEEEFloat::changeSign() {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001628 /* Look mummy, this one's easy. */
1629 sign = !sign;
1630}
1631
1632/* Normalized addition or subtraction. */
Tim Shen85de51d2016-10-25 19:55:59 +00001633IEEEFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs,
1634 roundingMode rounding_mode,
1635 bool subtract) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001636 opStatus fs;
1637
1638 fs = addOrSubtractSpecials(rhs, subtract);
1639
1640 /* This return code means it was not a simple case. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001641 if (fs == opDivByZero) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001642 lostFraction lost_fraction;
1643
1644 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1645 fs = normalize(rounding_mode, lost_fraction);
1646
1647 /* Can only be zero if we lost no fraction. */
1648 assert(category != fcZero || lost_fraction == lfExactlyZero);
1649 }
1650
1651 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1652 positive zero unless rounding to minus infinity, except that
1653 adding two like-signed zeroes gives that zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001654 if (category == fcZero) {
1655 if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001656 sign = (rounding_mode == rmTowardNegative);
1657 }
1658
1659 return fs;
1660}
1661
1662/* Normalized addition. */
Tim Shen85de51d2016-10-25 19:55:59 +00001663IEEEFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs,
1664 roundingMode rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001665 return addOrSubtract(rhs, rounding_mode, false);
1666}
1667
1668/* Normalized subtraction. */
Tim Shen85de51d2016-10-25 19:55:59 +00001669IEEEFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs,
1670 roundingMode rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001671 return addOrSubtract(rhs, rounding_mode, true);
1672}
1673
1674/* Normalized multiply. */
Tim Shen85de51d2016-10-25 19:55:59 +00001675IEEEFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs,
1676 roundingMode rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001677 opStatus fs;
1678
1679 sign ^= rhs.sign;
1680 fs = multiplySpecials(rhs);
1681
Michael Gottesman8136c382013-06-26 23:17:28 +00001682 if (isFiniteNonZero()) {
Craig Topperc10719f2014-04-07 04:17:22 +00001683 lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001684 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001685 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001686 fs = (opStatus) (fs | opInexact);
1687 }
1688
1689 return fs;
1690}
1691
1692/* Normalized divide. */
Tim Shen85de51d2016-10-25 19:55:59 +00001693IEEEFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs,
1694 roundingMode rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001695 opStatus fs;
1696
1697 sign ^= rhs.sign;
1698 fs = divideSpecials(rhs);
1699
Michael Gottesman8136c382013-06-26 23:17:28 +00001700 if (isFiniteNonZero()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001701 lostFraction lost_fraction = divideSignificand(rhs);
1702 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001703 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001704 fs = (opStatus) (fs | opInexact);
1705 }
1706
1707 return fs;
1708}
1709
Dale Johannesenfe750172009-01-20 18:35:05 +00001710/* Normalized remainder. This is not currently correct in all cases. */
Tim Shen85de51d2016-10-25 19:55:59 +00001711IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) {
Dale Johannesenfe750172009-01-20 18:35:05 +00001712 opStatus fs;
Tim Shen85de51d2016-10-25 19:55:59 +00001713 IEEEFloat V = *this;
Dale Johannesenfe750172009-01-20 18:35:05 +00001714 unsigned int origSign = sign;
1715
Dale Johannesenfe750172009-01-20 18:35:05 +00001716 fs = V.divide(rhs, rmNearestTiesToEven);
1717 if (fs == opDivByZero)
1718 return fs;
1719
1720 int parts = partCount();
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001721 integerPart *x = new integerPart[parts];
Dale Johannesenfe750172009-01-20 18:35:05 +00001722 bool ignored;
Simon Pilgrim00b34992017-03-20 14:40:12 +00001723 fs = V.convertToInteger(makeMutableArrayRef(x, parts),
1724 parts * integerPartWidth, true, rmNearestTiesToEven,
1725 &ignored);
1726 if (fs == opInvalidOp) {
Sylvestre Ledru3ce346d2016-11-08 10:00:45 +00001727 delete[] x;
Dale Johannesenfe750172009-01-20 18:35:05 +00001728 return fs;
Sylvestre Ledru3ce346d2016-11-08 10:00:45 +00001729 }
Dale Johannesenfe750172009-01-20 18:35:05 +00001730
1731 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1732 rmNearestTiesToEven);
1733 assert(fs==opOK); // should always work
1734
1735 fs = V.multiply(rhs, rmNearestTiesToEven);
1736 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1737
1738 fs = subtract(V, rmNearestTiesToEven);
1739 assert(fs==opOK || fs==opInexact); // likewise
1740
1741 if (isZero())
1742 sign = origSign; // IEEE754 requires this
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00001743 delete[] x;
Dale Johannesenfe750172009-01-20 18:35:05 +00001744 return fs;
1745}
1746
Stephen Canon157c8692017-03-31 20:31:33 +00001747/* Normalized llvm frem (C fmod). */
Tim Shen85de51d2016-10-25 19:55:59 +00001748IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) {
Dale Johannesen689d17d2007-08-31 23:35:31 +00001749 opStatus fs;
Dale Johannesenb5721632009-01-21 00:35:19 +00001750 fs = modSpecials(rhs);
Dale Johannesen689d17d2007-08-31 23:35:31 +00001751
Stephen Canon157c8692017-03-31 20:31:33 +00001752 while (isFiniteNonZero() && rhs.isFiniteNonZero() &&
1753 compareAbsoluteValue(rhs) != cmpLessThan) {
1754 IEEEFloat V = scalbn(rhs, ilogb(*this) - ilogb(rhs), rmNearestTiesToEven);
1755 if (compareAbsoluteValue(V) == cmpLessThan)
1756 V = scalbn(V, -1, rmNearestTiesToEven);
1757 V.sign = sign;
1758
Stephen Canonb12db0e2015-09-21 19:29:25 +00001759 fs = subtract(V, rmNearestTiesToEven);
Stephen Canon157c8692017-03-31 20:31:33 +00001760 assert(fs==opOK);
Dale Johannesenb5721632009-01-21 00:35:19 +00001761 }
Dale Johannesen689d17d2007-08-31 23:35:31 +00001762 return fs;
1763}
1764
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001765/* Normalized fused-multiply-add. */
Tim Shen85de51d2016-10-25 19:55:59 +00001766IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand,
1767 const IEEEFloat &addend,
1768 roundingMode rounding_mode) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001769 opStatus fs;
1770
1771 /* Post-multiplication sign, before addition. */
1772 sign ^= multiplicand.sign;
1773
1774 /* If and only if all arguments are normal do we need to do an
1775 extended-precision calculation. */
Michael Gottesman8136c382013-06-26 23:17:28 +00001776 if (isFiniteNonZero() &&
1777 multiplicand.isFiniteNonZero() &&
Hal Finkel171c2ec2014-10-14 19:23:07 +00001778 addend.isFinite()) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001779 lostFraction lost_fraction;
1780
1781 lost_fraction = multiplySignificand(multiplicand, &addend);
1782 fs = normalize(rounding_mode, lost_fraction);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001783 if (lost_fraction != lfExactlyZero)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001784 fs = (opStatus) (fs | opInexact);
1785
1786 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1787 positive zero unless rounding to minus infinity, except that
1788 adding two like-signed zeroes gives that zero. */
Lang Hames12b12e82015-01-04 01:20:55 +00001789 if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001790 sign = (rounding_mode == rmTowardNegative);
1791 } else {
1792 fs = multiplySpecials(multiplicand);
1793
1794 /* FS can only be opOK or opInvalidOp. There is no more work
1795 to do in the latter case. The IEEE-754R standard says it is
1796 implementation-defined in this case whether, if ADDEND is a
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001797 quiet NaN, we raise invalid op; this implementation does so.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001798
1799 If we need to do the addition we can do so with normal
1800 precision. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001801 if (fs == opOK)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001802 fs = addOrSubtract(addend, rounding_mode, false);
1803 }
1804
1805 return fs;
1806}
1807
Owen Andersona40319b2012-08-13 23:32:49 +00001808/* Rounding-mode corrrect round to integral value. */
Tim Shen85de51d2016-10-25 19:55:59 +00001809IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) {
Owen Andersona40319b2012-08-13 23:32:49 +00001810 opStatus fs;
Owen Andersona40319b2012-08-13 23:32:49 +00001811
Owen Anderson352dfff2012-08-15 18:28:45 +00001812 // If the exponent is large enough, we know that this value is already
1813 // integral, and the arithmetic below would potentially cause it to saturate
1814 // to +/-Inf. Bail out early instead.
Michael Gottesman8136c382013-06-26 23:17:28 +00001815 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
Owen Anderson352dfff2012-08-15 18:28:45 +00001816 return opOK;
1817
Owen Andersona40319b2012-08-13 23:32:49 +00001818 // The algorithm here is quite simple: we add 2^(p-1), where p is the
1819 // precision of our format, and then subtract it back off again. The choice
1820 // of rounding modes for the addition/subtraction determines the rounding mode
1821 // for our integral rounding as well.
Owen Andersonbe7e2972012-08-15 16:42:53 +00001822 // NOTE: When the input value is negative, we do subtraction followed by
Owen Anderson1ff74b02012-08-15 05:39:46 +00001823 // addition instead.
Owen Anderson0b357222012-08-14 18:51:15 +00001824 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1825 IntegerConstant <<= semanticsPrecision(*semantics)-1;
Tim Shen85de51d2016-10-25 19:55:59 +00001826 IEEEFloat MagicConstant(*semantics);
Owen Andersona40319b2012-08-13 23:32:49 +00001827 fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1828 rmNearestTiesToEven);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00001829 MagicConstant.sign = sign;
Owen Anderson1ff74b02012-08-15 05:39:46 +00001830
Owen Andersona40319b2012-08-13 23:32:49 +00001831 if (fs != opOK)
1832 return fs;
1833
Owen Anderson1ff74b02012-08-15 05:39:46 +00001834 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1835 bool inputSign = isNegative();
1836
Owen Andersona40319b2012-08-13 23:32:49 +00001837 fs = add(MagicConstant, rounding_mode);
1838 if (fs != opOK && fs != opInexact)
1839 return fs;
1840
1841 fs = subtract(MagicConstant, rounding_mode);
Owen Anderson1ff74b02012-08-15 05:39:46 +00001842
1843 // Restore the input sign.
1844 if (inputSign != isNegative())
1845 changeSign();
1846
Owen Andersona40319b2012-08-13 23:32:49 +00001847 return fs;
1848}
1849
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00001850
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001851/* Comparison requires normalized numbers. */
Tim Shen85de51d2016-10-25 19:55:59 +00001852IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001853 cmpResult result;
1854
1855 assert(semantics == rhs.semantics);
1856
Michael Gottesman9b877e12013-06-24 09:57:57 +00001857 switch (PackCategoriesIntoKey(category, rhs.category)) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001858 default:
Craig Topper2617dcc2014-04-15 06:32:26 +00001859 llvm_unreachable(nullptr);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001860
Michael Gottesman9b877e12013-06-24 09:57:57 +00001861 case PackCategoriesIntoKey(fcNaN, fcZero):
1862 case PackCategoriesIntoKey(fcNaN, fcNormal):
1863 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1864 case PackCategoriesIntoKey(fcNaN, fcNaN):
1865 case PackCategoriesIntoKey(fcZero, fcNaN):
1866 case PackCategoriesIntoKey(fcNormal, fcNaN):
1867 case PackCategoriesIntoKey(fcInfinity, fcNaN):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001868 return cmpUnordered;
1869
Michael Gottesman9b877e12013-06-24 09:57:57 +00001870 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1871 case PackCategoriesIntoKey(fcInfinity, fcZero):
1872 case PackCategoriesIntoKey(fcNormal, fcZero):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001873 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001874 return cmpLessThan;
1875 else
1876 return cmpGreaterThan;
1877
Michael Gottesman9b877e12013-06-24 09:57:57 +00001878 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1879 case PackCategoriesIntoKey(fcZero, fcInfinity):
1880 case PackCategoriesIntoKey(fcZero, fcNormal):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001881 if (rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001882 return cmpGreaterThan;
1883 else
1884 return cmpLessThan;
1885
Michael Gottesman9b877e12013-06-24 09:57:57 +00001886 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001887 if (sign == rhs.sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001888 return cmpEqual;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001889 else if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001890 return cmpLessThan;
1891 else
1892 return cmpGreaterThan;
1893
Michael Gottesman9b877e12013-06-24 09:57:57 +00001894 case PackCategoriesIntoKey(fcZero, fcZero):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001895 return cmpEqual;
1896
Michael Gottesman9b877e12013-06-24 09:57:57 +00001897 case PackCategoriesIntoKey(fcNormal, fcNormal):
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001898 break;
1899 }
1900
1901 /* Two normal numbers. Do they have the same sign? */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001902 if (sign != rhs.sign) {
1903 if (sign)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001904 result = cmpLessThan;
1905 else
1906 result = cmpGreaterThan;
1907 } else {
1908 /* Compare absolute values; invert result if negative. */
1909 result = compareAbsoluteValue(rhs);
1910
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001911 if (sign) {
1912 if (result == cmpLessThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001913 result = cmpGreaterThan;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00001914 else if (result == cmpGreaterThan)
Neil Booth9acbf5a2007-09-26 21:33:42 +00001915 result = cmpLessThan;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001916 }
1917 }
1918
1919 return result;
1920}
1921
Tim Shen85de51d2016-10-25 19:55:59 +00001922/// IEEEFloat::convert - convert a value of one floating point type to another.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001923/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1924/// records whether the transformation lost information, i.e. whether
1925/// converting the result back to the original type will produce the
1926/// original value (this is almost the same as return value==fsOK, but there
1927/// are edge cases where this is not so).
1928
Tim Shen85de51d2016-10-25 19:55:59 +00001929IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics,
1930 roundingMode rounding_mode,
1931 bool *losesInfo) {
Neil Bootha8d72692007-09-22 02:56:19 +00001932 lostFraction lostFraction;
1933 unsigned int newPartCount, oldPartCount;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001934 opStatus fs;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001935 int shift;
1936 const fltSemantics &fromSemantics = *semantics;
Neil Booth9acbf5a2007-09-26 21:33:42 +00001937
Neil Bootha8d72692007-09-22 02:56:19 +00001938 lostFraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001939 newPartCount = partCountForBits(toSemantics.precision + 1);
Neil Bootha8d72692007-09-22 02:56:19 +00001940 oldPartCount = partCount();
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001941 shift = toSemantics.precision - fromSemantics.precision;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001942
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001943 bool X86SpecialNan = false;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001944 if (&fromSemantics == &semX87DoubleExtended &&
1945 &toSemantics != &semX87DoubleExtended && category == fcNaN &&
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001946 (!(*significandParts() & 0x8000000000000000ULL) ||
1947 !(*significandParts() & 0x4000000000000000ULL))) {
1948 // x86 has some unusual NaNs which cannot be represented in any other
1949 // format; note them here.
1950 X86SpecialNan = true;
1951 }
1952
Ulrich Weigand1d4dbda2013-07-16 13:03:25 +00001953 // If this is a truncation of a denormal number, and the target semantics
1954 // has larger exponent range than the source semantics (this can happen
1955 // when truncating from PowerPC double-double to double format), the
1956 // right shift could lose result mantissa bits. Adjust exponent instead
1957 // of performing excessive shift.
1958 if (shift < 0 && isFiniteNonZero()) {
1959 int exponentChange = significandMSB() + 1 - fromSemantics.precision;
1960 if (exponent + exponentChange < toSemantics.minExponent)
1961 exponentChange = toSemantics.minExponent - exponent;
1962 if (exponentChange < shift)
1963 exponentChange = shift;
1964 if (exponentChange < 0) {
1965 shift -= exponentChange;
1966 exponent += exponentChange;
1967 }
1968 }
1969
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001970 // If this is a truncation, perform the shift before we narrow the storage.
Michael Gottesman8136c382013-06-26 23:17:28 +00001971 if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001972 lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
1973
1974 // Fix the storage so it can hold to new value.
Neil Bootha8d72692007-09-22 02:56:19 +00001975 if (newPartCount > oldPartCount) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001976 // The new type requires more storage; make it available.
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001977 integerPart *newParts;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001978 newParts = new integerPart[newPartCount];
1979 APInt::tcSet(newParts, 0, newPartCount);
Michael Gottesman8136c382013-06-26 23:17:28 +00001980 if (isFiniteNonZero() || category==fcNaN)
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00001981 APInt::tcAssign(newParts, significandParts(), oldPartCount);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001982 freeSignificand();
1983 significand.parts = newParts;
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001984 } else if (newPartCount == 1 && oldPartCount != 1) {
1985 // Switch to built-in storage for a single part.
1986 integerPart newPart = 0;
Michael Gottesman8136c382013-06-26 23:17:28 +00001987 if (isFiniteNonZero() || category==fcNaN)
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001988 newPart = significandParts()[0];
1989 freeSignificand();
1990 significand.part = newPart;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00001991 }
1992
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001993 // Now that we have the right storage, switch the semantics.
1994 semantics = &toSemantics;
1995
1996 // If this is an extension, perform the shift now that the storage is
1997 // available.
Michael Gottesman8136c382013-06-26 23:17:28 +00001998 if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
Eli Friedmana84ad7d2011-11-26 03:38:02 +00001999 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2000
Michael Gottesman8136c382013-06-26 23:17:28 +00002001 if (isFiniteNonZero()) {
Neil Bootha8d72692007-09-22 02:56:19 +00002002 fs = normalize(rounding_mode, lostFraction);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002003 *losesInfo = (fs != opOK);
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002004 } else if (category == fcNaN) {
Eli Friedmana84ad7d2011-11-26 03:38:02 +00002005 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
Benjamin Kramerb361adb2013-01-25 17:01:00 +00002006
2007 // For x87 extended precision, we want to make a NaN, not a special NaN if
2008 // the input wasn't special either.
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002009 if (!X86SpecialNan && semantics == &semX87DoubleExtended)
Benjamin Kramerb361adb2013-01-25 17:01:00 +00002010 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2011
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00002012 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2013 // does not give you back the same bits. This is dubious, and we
2014 // don't currently do it. You're really supposed to get
2015 // an invalid operation signal at runtime, but nobody does that.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002016 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002017 } else {
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002018 *losesInfo = false;
Eli Friedman31f01162011-11-28 18:50:37 +00002019 fs = opOK;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002020 }
2021
2022 return fs;
2023}
2024
2025/* Convert a floating point number to an integer according to the
2026 rounding mode. If the rounded integer value is out of range this
Neil Booth618d0fc2007-11-01 22:43:37 +00002027 returns an invalid operation exception and the contents of the
2028 destination parts are unspecified. If the rounded value is in
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002029 range but the floating point number is not the exact integer, the C
2030 standard doesn't require an inexact exception to be raised. IEEE
2031 854 does require it so we do that.
2032
2033 Note that for conversions to integer type the C standard requires
2034 round-to-zero to always be used. */
Tim Shen85de51d2016-10-25 19:55:59 +00002035IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger(
Simon Pilgrim00b34992017-03-20 14:40:12 +00002036 MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned,
Tim Shen85de51d2016-10-25 19:55:59 +00002037 roundingMode rounding_mode, bool *isExact) const {
Neil Booth618d0fc2007-11-01 22:43:37 +00002038 lostFraction lost_fraction;
2039 const integerPart *src;
2040 unsigned int dstPartsCount, truncatedBits;
2041
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002042 *isExact = false;
2043
Neil Booth618d0fc2007-11-01 22:43:37 +00002044 /* Handle the three special cases first. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002045 if (category == fcInfinity || category == fcNaN)
Neil Booth618d0fc2007-11-01 22:43:37 +00002046 return opInvalidOp;
2047
2048 dstPartsCount = partCountForBits(width);
Simon Pilgrim00b34992017-03-20 14:40:12 +00002049 assert(dstPartsCount <= parts.size() && "Integer too big");
Neil Booth618d0fc2007-11-01 22:43:37 +00002050
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002051 if (category == fcZero) {
Simon Pilgrim00b34992017-03-20 14:40:12 +00002052 APInt::tcSet(parts.data(), 0, dstPartsCount);
Dale Johannesen7221af32008-10-07 00:40:01 +00002053 // Negative zero can't be represented as an int.
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002054 *isExact = !sign;
2055 return opOK;
Neil Booth618d0fc2007-11-01 22:43:37 +00002056 }
2057
2058 src = significandParts();
2059
2060 /* Step 1: place our absolute value, with any fraction truncated, in
2061 the destination. */
2062 if (exponent < 0) {
2063 /* Our absolute value is less than one; truncate everything. */
Simon Pilgrim00b34992017-03-20 14:40:12 +00002064 APInt::tcSet(parts.data(), 0, dstPartsCount);
Dale Johannesen740e9872009-01-19 21:17:05 +00002065 /* For exponent -1 the integer bit represents .5, look at that.
2066 For smaller exponents leftmost truncated bit is 0. */
2067 truncatedBits = semantics->precision -1U - exponent;
Neil Booth618d0fc2007-11-01 22:43:37 +00002068 } else {
2069 /* We want the most significant (exponent + 1) bits; the rest are
2070 truncated. */
2071 unsigned int bits = exponent + 1U;
2072
2073 /* Hopelessly large in magnitude? */
2074 if (bits > width)
2075 return opInvalidOp;
2076
2077 if (bits < semantics->precision) {
2078 /* We truncate (semantics->precision - bits) bits. */
2079 truncatedBits = semantics->precision - bits;
Simon Pilgrim00b34992017-03-20 14:40:12 +00002080 APInt::tcExtract(parts.data(), dstPartsCount, src, bits, truncatedBits);
Neil Booth618d0fc2007-11-01 22:43:37 +00002081 } else {
2082 /* We want at least as many bits as are available. */
Simon Pilgrim00b34992017-03-20 14:40:12 +00002083 APInt::tcExtract(parts.data(), dstPartsCount, src, semantics->precision,
2084 0);
2085 APInt::tcShiftLeft(parts.data(), dstPartsCount,
2086 bits - semantics->precision);
Neil Booth618d0fc2007-11-01 22:43:37 +00002087 truncatedBits = 0;
2088 }
2089 }
2090
2091 /* Step 2: work out any lost fraction, and increment the absolute
2092 value if we would round away from zero. */
2093 if (truncatedBits) {
2094 lost_fraction = lostFractionThroughTruncation(src, partCount(),
2095 truncatedBits);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002096 if (lost_fraction != lfExactlyZero &&
2097 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
Simon Pilgrim00b34992017-03-20 14:40:12 +00002098 if (APInt::tcIncrement(parts.data(), dstPartsCount))
Neil Booth618d0fc2007-11-01 22:43:37 +00002099 return opInvalidOp; /* Overflow. */
2100 }
2101 } else {
2102 lost_fraction = lfExactlyZero;
2103 }
2104
2105 /* Step 3: check if we fit in the destination. */
Simon Pilgrim00b34992017-03-20 14:40:12 +00002106 unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1;
Neil Booth618d0fc2007-11-01 22:43:37 +00002107
2108 if (sign) {
2109 if (!isSigned) {
2110 /* Negative numbers cannot be represented as unsigned. */
2111 if (omsb != 0)
2112 return opInvalidOp;
2113 } else {
2114 /* It takes omsb bits to represent the unsigned integer value.
2115 We lose a bit for the sign, but care is needed as the
2116 maximally negative integer is a special case. */
Simon Pilgrim00b34992017-03-20 14:40:12 +00002117 if (omsb == width &&
2118 APInt::tcLSB(parts.data(), dstPartsCount) + 1 != omsb)
Neil Booth618d0fc2007-11-01 22:43:37 +00002119 return opInvalidOp;
2120
2121 /* This case can happen because of rounding. */
2122 if (omsb > width)
2123 return opInvalidOp;
2124 }
2125
Simon Pilgrim00b34992017-03-20 14:40:12 +00002126 APInt::tcNegate (parts.data(), dstPartsCount);
Neil Booth618d0fc2007-11-01 22:43:37 +00002127 } else {
2128 if (omsb >= width + !isSigned)
2129 return opInvalidOp;
2130 }
2131
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002132 if (lost_fraction == lfExactlyZero) {
2133 *isExact = true;
Neil Booth618d0fc2007-11-01 22:43:37 +00002134 return opOK;
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002135 } else
Neil Booth618d0fc2007-11-01 22:43:37 +00002136 return opInexact;
2137}
2138
2139/* Same as convertToSignExtendedInteger, except we provide
2140 deterministic values in case of an invalid operation exception,
2141 namely zero for NaNs and the minimal or maximal value respectively
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002142 for underflow or overflow.
2143 The *isExact output tells whether the result is exact, in the sense
2144 that converting it back to the original floating point type produces
2145 the original value. This is almost equivalent to result==opOK,
2146 except for negative zeroes.
2147*/
Simon Pilgrim00b34992017-03-20 14:40:12 +00002148IEEEFloat::opStatus
2149IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts,
2150 unsigned int width, bool isSigned,
2151 roundingMode rounding_mode, bool *isExact) const {
Neil Booth618d0fc2007-11-01 22:43:37 +00002152 opStatus fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002153
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002154 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
Dale Johannesen4f0bd682008-10-09 23:00:39 +00002155 isExact);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002156
Neil Booth618d0fc2007-11-01 22:43:37 +00002157 if (fs == opInvalidOp) {
2158 unsigned int bits, dstPartsCount;
2159
2160 dstPartsCount = partCountForBits(width);
Simon Pilgrim00b34992017-03-20 14:40:12 +00002161 assert(dstPartsCount <= parts.size() && "Integer too big");
Neil Booth618d0fc2007-11-01 22:43:37 +00002162
2163 if (category == fcNaN)
2164 bits = 0;
2165 else if (sign)
2166 bits = isSigned;
2167 else
2168 bits = width - isSigned;
2169
Simon Pilgrim00b34992017-03-20 14:40:12 +00002170 APInt::tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits);
Neil Booth618d0fc2007-11-01 22:43:37 +00002171 if (sign && isSigned)
Simon Pilgrim00b34992017-03-20 14:40:12 +00002172 APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002173 }
2174
Neil Booth618d0fc2007-11-01 22:43:37 +00002175 return fs;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002176}
2177
Neil Booth6c1c8582007-10-07 12:07:53 +00002178/* Convert an unsigned integer SRC to a floating point number,
2179 rounding according to ROUNDING_MODE. The sign of the floating
2180 point number is not modified. */
Tim Shen85de51d2016-10-25 19:55:59 +00002181IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts(
2182 const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) {
Neil Booth49c6aab2007-10-08 14:39:42 +00002183 unsigned int omsb, precision, dstCount;
Neil Booth6c1c8582007-10-07 12:07:53 +00002184 integerPart *dst;
Neil Booth49c6aab2007-10-08 14:39:42 +00002185 lostFraction lost_fraction;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002186
2187 category = fcNormal;
Neil Booth49c6aab2007-10-08 14:39:42 +00002188 omsb = APInt::tcMSB(src, srcCount) + 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002189 dst = significandParts();
2190 dstCount = partCount();
Neil Booth49c6aab2007-10-08 14:39:42 +00002191 precision = semantics->precision;
Neil Booth6c1c8582007-10-07 12:07:53 +00002192
Nick Lewyckyf66daac2011-10-03 21:30:08 +00002193 /* We want the most significant PRECISION bits of SRC. There may not
Neil Booth49c6aab2007-10-08 14:39:42 +00002194 be that many; extract what we can. */
2195 if (precision <= omsb) {
2196 exponent = omsb - 1;
Neil Booth6c1c8582007-10-07 12:07:53 +00002197 lost_fraction = lostFractionThroughTruncation(src, srcCount,
Neil Booth49c6aab2007-10-08 14:39:42 +00002198 omsb - precision);
2199 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2200 } else {
2201 exponent = precision - 1;
2202 lost_fraction = lfExactlyZero;
2203 APInt::tcExtract(dst, dstCount, src, omsb, 0);
Neil Booth6c1c8582007-10-07 12:07:53 +00002204 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002205
2206 return normalize(rounding_mode, lost_fraction);
2207}
2208
Tim Shen85de51d2016-10-25 19:55:59 +00002209IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned,
2210 roundingMode rounding_mode) {
Dan Gohman35723eb2008-02-29 01:26:11 +00002211 unsigned int partCount = Val.getNumWords();
2212 APInt api = Val;
2213
2214 sign = false;
2215 if (isSigned && api.isNegative()) {
2216 sign = true;
2217 api = -api;
2218 }
2219
2220 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2221}
2222
Neil Booth03f58ab2007-10-07 12:15:41 +00002223/* Convert a two's complement integer SRC to a floating point number,
2224 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2225 integer is signed, in which case it must be sign-extended. */
Tim Shen85de51d2016-10-25 19:55:59 +00002226IEEEFloat::opStatus
2227IEEEFloat::convertFromSignExtendedInteger(const integerPart *src,
2228 unsigned int srcCount, bool isSigned,
2229 roundingMode rounding_mode) {
Neil Booth03f58ab2007-10-07 12:15:41 +00002230 opStatus status;
2231
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002232 if (isSigned &&
2233 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002234 integerPart *copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002235
2236 /* If we're signed and negative negate a copy. */
2237 sign = true;
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002238 copy = new integerPart[srcCount];
Neil Booth03f58ab2007-10-07 12:15:41 +00002239 APInt::tcAssign(copy, src, srcCount);
2240 APInt::tcNegate(copy, srcCount);
2241 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002242 delete [] copy;
Neil Booth03f58ab2007-10-07 12:15:41 +00002243 } else {
2244 sign = false;
2245 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2246 }
2247
2248 return status;
2249}
2250
Neil Booth5f009732007-10-07 11:45:55 +00002251/* FIXME: should this just take a const APInt reference? */
Tim Shen85de51d2016-10-25 19:55:59 +00002252IEEEFloat::opStatus
2253IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2254 unsigned int width, bool isSigned,
2255 roundingMode rounding_mode) {
Dale Johannesen42305122007-09-21 22:09:37 +00002256 unsigned int partCount = partCountForBits(width);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002257 APInt api = APInt(width, makeArrayRef(parts, partCount));
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002258
2259 sign = false;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002260 if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
Dale Johannesen28a2c4a2007-09-30 18:17:01 +00002261 sign = true;
2262 api = -api;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002263 }
2264
Neil Boothba205222007-10-07 12:10:57 +00002265 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002266}
2267
Tim Shen85de51d2016-10-25 19:55:59 +00002268IEEEFloat::opStatus
2269IEEEFloat::convertFromHexadecimalString(StringRef s,
2270 roundingMode rounding_mode) {
Erick Tryzelaara9680df2009-08-18 18:20:37 +00002271 lostFraction lost_fraction = lfExactlyZero;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002272
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002273 category = fcNormal;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002274 zeroSignificand();
2275 exponent = 0;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002276
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002277 integerPart *significand = significandParts();
2278 unsigned partsCount = partCount();
2279 unsigned bitPos = partsCount * integerPartWidth;
2280 bool computedTrailingFraction = false;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002281
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002282 // Skip leading zeroes and any (hexa)decimal point.
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002283 StringRef::iterator begin = s.begin();
2284 StringRef::iterator end = s.end();
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002285 StringRef::iterator dot;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002286 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002287 StringRef::iterator firstSignificantDigit = p;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002288
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002289 while (p != end) {
Dale Johannesenfa483722008-05-14 22:53:25 +00002290 integerPart hex_value;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002291
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002292 if (*p == '.') {
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002293 assert(dot == end && "String contains multiple dots");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002294 dot = p++;
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002295 continue;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002296 }
2297
2298 hex_value = hexDigitValue(*p);
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002299 if (hex_value == -1U)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002300 break;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002301
2302 p++;
2303
Eli Friedmand2eb07a2013-07-17 22:17:29 +00002304 // Store the number while we have space.
2305 if (bitPos) {
2306 bitPos -= 4;
2307 hex_value <<= bitPos % integerPartWidth;
2308 significand[bitPos / integerPartWidth] |= hex_value;
2309 } else if (!computedTrailingFraction) {
2310 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2311 computedTrailingFraction = true;
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002312 }
2313 }
2314
2315 /* Hex floats require an exponent but not a hexadecimal point. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002316 assert(p != end && "Hex strings require an exponent");
2317 assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2318 assert(p != begin && "Significand has no digits");
2319 assert((dot == end || p - begin != 1) && "Significand has no digits");
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002320
2321 /* Ignore the exponent if we are zero. */
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002322 if (p != firstSignificantDigit) {
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002323 int expAdjustment;
2324
2325 /* Implicit hexadecimal point? */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002326 if (dot == end)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002327 dot = p;
2328
2329 /* Calculate the exponent adjustment implicit in the number of
2330 significant digits. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002331 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002332 if (expAdjustment < 0)
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002333 expAdjustment++;
2334 expAdjustment = expAdjustment * 4 - 1;
2335
2336 /* Adjust for writing the significand starting at the most
2337 significant nibble. */
2338 expAdjustment += semantics->precision;
2339 expAdjustment -= partsCount * integerPartWidth;
2340
2341 /* Adjust for the given exponent. */
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002342 exponent = totalExponent(p + 1, end, expAdjustment);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002343 }
2344
2345 return normalize(rounding_mode, lost_fraction);
2346}
2347
Tim Shen85de51d2016-10-25 19:55:59 +00002348IEEEFloat::opStatus
2349IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2350 unsigned sigPartCount, int exp,
2351 roundingMode rounding_mode) {
Neil Boothb93d90e2007-10-12 16:02:31 +00002352 unsigned int parts, pow5PartCount;
Tamas Berghammerb6b0ddf2015-07-09 10:13:39 +00002353 fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
Neil Boothb93d90e2007-10-12 16:02:31 +00002354 integerPart pow5Parts[maxPowerOfFiveParts];
2355 bool isNearest;
2356
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002357 isNearest = (rounding_mode == rmNearestTiesToEven ||
2358 rounding_mode == rmNearestTiesToAway);
Neil Boothb93d90e2007-10-12 16:02:31 +00002359
2360 parts = partCountForBits(semantics->precision + 11);
2361
2362 /* Calculate pow(5, abs(exp)). */
2363 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2364
2365 for (;; parts *= 2) {
2366 opStatus sigStatus, powStatus;
2367 unsigned int excessPrecision, truncatedBits;
2368
2369 calcSemantics.precision = parts * integerPartWidth - 1;
2370 excessPrecision = calcSemantics.precision - semantics->precision;
2371 truncatedBits = excessPrecision;
2372
Tim Shen139a58f2016-10-27 22:52:40 +00002373 IEEEFloat decSig(calcSemantics, uninitialized);
2374 decSig.makeZero(sign);
Tim Shen85de51d2016-10-25 19:55:59 +00002375 IEEEFloat pow5(calcSemantics);
Neil Boothb93d90e2007-10-12 16:02:31 +00002376
2377 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2378 rmNearestTiesToEven);
2379 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2380 rmNearestTiesToEven);
2381 /* Add exp, as 10^n = 5^n * 2^n. */
2382 decSig.exponent += exp;
2383
2384 lostFraction calcLostFraction;
Evan Cheng82b9e962008-05-02 21:15:08 +00002385 integerPart HUerr, HUdistance;
2386 unsigned int powHUerr;
Neil Boothb93d90e2007-10-12 16:02:31 +00002387
2388 if (exp >= 0) {
2389 /* multiplySignificand leaves the precision-th bit set to 1. */
Craig Topperc10719f2014-04-07 04:17:22 +00002390 calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
Neil Boothb93d90e2007-10-12 16:02:31 +00002391 powHUerr = powStatus != opOK;
2392 } else {
2393 calcLostFraction = decSig.divideSignificand(pow5);
2394 /* Denormal numbers have less precision. */
2395 if (decSig.exponent < semantics->minExponent) {
2396 excessPrecision += (semantics->minExponent - decSig.exponent);
2397 truncatedBits = excessPrecision;
2398 if (excessPrecision > calcSemantics.precision)
2399 excessPrecision = calcSemantics.precision;
2400 }
2401 /* Extra half-ulp lost in reciprocal of exponent. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002402 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
Neil Boothb93d90e2007-10-12 16:02:31 +00002403 }
2404
2405 /* Both multiplySignificand and divideSignificand return the
2406 result with the integer bit set. */
Evan Cheng67c90212009-10-27 21:35:42 +00002407 assert(APInt::tcExtractBit
2408 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
Neil Boothb93d90e2007-10-12 16:02:31 +00002409
2410 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2411 powHUerr);
2412 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2413 excessPrecision, isNearest);
2414
2415 /* Are we guaranteed to round correctly if we truncate? */
2416 if (HUdistance >= HUerr) {
2417 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2418 calcSemantics.precision - excessPrecision,
2419 excessPrecision);
2420 /* Take the exponent of decSig. If we tcExtract-ed less bits
2421 above we must adjust our exponent to compensate for the
2422 implicit right shift. */
2423 exponent = (decSig.exponent + semantics->precision
2424 - (calcSemantics.precision - excessPrecision));
2425 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2426 decSig.partCount(),
2427 truncatedBits);
2428 return normalize(rounding_mode, calcLostFraction);
2429 }
2430 }
2431}
2432
Tim Shen85de51d2016-10-25 19:55:59 +00002433IEEEFloat::opStatus
2434IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) {
Neil Booth4ed401b2007-10-14 10:16:12 +00002435 decimalInfo D;
Neil Boothb93d90e2007-10-12 16:02:31 +00002436 opStatus fs;
2437
Neil Booth4ed401b2007-10-14 10:16:12 +00002438 /* Scan the text. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002439 StringRef::iterator p = str.begin();
2440 interpretDecimal(p, str.end(), &D);
Neil Boothb93d90e2007-10-12 16:02:31 +00002441
Neil Booth91305512007-10-15 15:00:55 +00002442 /* Handle the quick cases. First the case of no significant digits,
2443 i.e. zero, and then exponents that are obviously too large or too
2444 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2445 definitely overflows if
2446
2447 (exp - 1) * L >= maxExponent
2448
2449 and definitely underflows to zero where
2450
2451 (exp + 1) * L <= minExponent - precision
2452
2453 With integer arithmetic the tightest bounds for L are
2454
2455 93/28 < L < 196/59 [ numerator <= 256 ]
2456 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2457 */
2458
Michael Gottesman228156c2013-07-01 23:54:08 +00002459 // Test if we have a zero number allowing for strings with no null terminators
2460 // and zero decimals with non-zero exponents.
Simon Pilgrima2849592017-03-20 13:53:59 +00002461 //
Michael Gottesman228156c2013-07-01 23:54:08 +00002462 // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2463 // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2464 // be at most one dot. On the other hand, if we have a zero with a non-zero
2465 // exponent, then we know that D.firstSigDigit will be non-numeric.
Michael Gottesman94d61952013-07-02 15:50:05 +00002466 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
Neil Boothb93d90e2007-10-12 16:02:31 +00002467 category = fcZero;
2468 fs = opOK;
John McCallb42cc682010-02-26 22:20:41 +00002469
2470 /* Check whether the normalized exponent is high enough to overflow
2471 max during the log-rebasing in the max-exponent check below. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00002472 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
John McCallb42cc682010-02-26 22:20:41 +00002473 fs = handleOverflow(rounding_mode);
2474
2475 /* If it wasn't, then it also wasn't high enough to overflow max
2476 during the log-rebasing in the min-exponent check. Check that it
2477 won't overflow min in either check, then perform the min-exponent
2478 check. */
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00002479 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
John McCallb42cc682010-02-26 22:20:41 +00002480 (D.normalizedExponent + 1) * 28738 <=
2481 8651 * (semantics->minExponent - (int) semantics->precision)) {
Neil Booth91305512007-10-15 15:00:55 +00002482 /* Underflow to zero and round. */
Michael Gottesman30a90eb2013-07-27 21:49:21 +00002483 category = fcNormal;
Neil Booth91305512007-10-15 15:00:55 +00002484 zeroSignificand();
2485 fs = normalize(rounding_mode, lfLessThanHalf);
John McCallb42cc682010-02-26 22:20:41 +00002486
2487 /* We can finally safely perform the max-exponent check. */
Neil Booth91305512007-10-15 15:00:55 +00002488 } else if ((D.normalizedExponent - 1) * 42039
2489 >= 12655 * semantics->maxExponent) {
2490 /* Overflow and round. */
2491 fs = handleOverflow(rounding_mode);
Neil Boothb93d90e2007-10-12 16:02:31 +00002492 } else {
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002493 integerPart *decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002494 unsigned int partCount;
Neil Boothb93d90e2007-10-12 16:02:31 +00002495
Neil Booth4ed401b2007-10-14 10:16:12 +00002496 /* A tight upper bound on number of bits required to hold an
Neil Booth91305512007-10-15 15:00:55 +00002497 N-digit decimal integer is N * 196 / 59. Allocate enough space
Neil Booth4ed401b2007-10-14 10:16:12 +00002498 to hold the full significand, and an extra part required by
2499 tcMultiplyPart. */
Evan Cheng82b9e962008-05-02 21:15:08 +00002500 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
Neil Booth91305512007-10-15 15:00:55 +00002501 partCount = partCountForBits(1 + 196 * partCount / 59);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002502 decSignificand = new integerPart[partCount + 1];
Neil Booth4ed401b2007-10-14 10:16:12 +00002503 partCount = 0;
Neil Boothb93d90e2007-10-12 16:02:31 +00002504
Neil Booth4ed401b2007-10-14 10:16:12 +00002505 /* Convert to binary efficiently - we do almost all multiplication
2506 in an integerPart. When this would overflow do we do a single
2507 bignum multiplication, and then revert again to multiplication
2508 in an integerPart. */
2509 do {
2510 integerPart decValue, val, multiplier;
2511
2512 val = 0;
2513 multiplier = 1;
2514
2515 do {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002516 if (*p == '.') {
Neil Booth4ed401b2007-10-14 10:16:12 +00002517 p++;
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002518 if (p == str.end()) {
2519 break;
2520 }
2521 }
Neil Booth4ed401b2007-10-14 10:16:12 +00002522 decValue = decDigitValue(*p++);
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002523 assert(decValue < 10U && "Invalid character in significand");
Neil Booth4ed401b2007-10-14 10:16:12 +00002524 multiplier *= 10;
2525 val = val * 10 + decValue;
2526 /* The maximum number that can be multiplied by ten with any
2527 digit added without overflowing an integerPart. */
2528 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2529
2530 /* Multiply out the current part. */
2531 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2532 partCount, partCount + 1, false);
2533
2534 /* If we used another part (likely but not guaranteed), increase
2535 the count. */
2536 if (decSignificand[partCount])
2537 partCount++;
2538 } while (p <= D.lastSigDigit);
Neil Boothb93d90e2007-10-12 16:02:31 +00002539
Neil Boothae077d22007-11-01 22:51:07 +00002540 category = fcNormal;
Neil Boothb93d90e2007-10-12 16:02:31 +00002541 fs = roundSignificandWithExponent(decSignificand, partCount,
Neil Booth4ed401b2007-10-14 10:16:12 +00002542 D.exponent, rounding_mode);
Dylan Noblesmith4e69e292014-08-26 02:03:33 +00002543
2544 delete [] decSignificand;
Neil Booth4ed401b2007-10-14 10:16:12 +00002545 }
Neil Boothb93d90e2007-10-12 16:02:31 +00002546
2547 return fs;
2548}
2549
Tim Shen85de51d2016-10-25 19:55:59 +00002550bool IEEEFloat::convertFromStringSpecials(StringRef str) {
Michael Gottesman40e8a182013-06-24 09:58:05 +00002551 if (str.equals("inf") || str.equals("INFINITY")) {
2552 makeInf(false);
2553 return true;
2554 }
2555
2556 if (str.equals("-inf") || str.equals("-INFINITY")) {
2557 makeInf(true);
2558 return true;
2559 }
2560
2561 if (str.equals("nan") || str.equals("NaN")) {
2562 makeNaN(false, false);
2563 return true;
2564 }
2565
2566 if (str.equals("-nan") || str.equals("-NaN")) {
2567 makeNaN(false, true);
2568 return true;
2569 }
2570
2571 return false;
2572}
2573
Tim Shen85de51d2016-10-25 19:55:59 +00002574IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str,
2575 roundingMode rounding_mode) {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002576 assert(!str.empty() && "Invalid string length");
Neil Booth06077e72007-10-14 10:29:28 +00002577
Michael Gottesman40e8a182013-06-24 09:58:05 +00002578 // Handle special cases.
2579 if (convertFromStringSpecials(str))
2580 return opOK;
2581
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002582 /* Handle a leading minus sign. */
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002583 StringRef::iterator p = str.begin();
2584 size_t slen = str.size();
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002585 sign = *p == '-' ? 1 : 0;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002586 if (*p == '-' || *p == '+') {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002587 p++;
2588 slen--;
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002589 assert(slen && "String has no digits");
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002590 }
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002591
Dan Gohmanb452d4e2010-03-24 19:38:02 +00002592 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002593 assert(slen - 2 && "Invalid string");
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002594 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
Erick Tryzelaar19f63b22009-08-16 23:36:19 +00002595 rounding_mode);
2596 }
Bill Wendlingc6075402008-11-27 08:00:12 +00002597
Erick Tryzelaarda666c82009-08-20 23:30:43 +00002598 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
Chris Lattnerfe02c1f2007-08-20 22:49:32 +00002599}
Dale Johannesena719a602007-08-24 00:56:33 +00002600
Neil Booth8f1946f2007-10-03 22:26:02 +00002601/* Write out a hexadecimal representation of the floating point value
2602 to DST, which must be of sufficient size, in the C99 form
2603 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2604 excluding the terminating NUL.
2605
2606 If UPPERCASE, the output is in upper case, otherwise in lower case.
2607
2608 HEXDIGITS digits appear altogether, rounding the value if
2609 necessary. If HEXDIGITS is 0, the minimal precision to display the
2610 number precisely is used instead. If nothing would appear after
2611 the decimal point it is suppressed.
2612
2613 The decimal exponent is always printed and has at least one digit.
2614 Zero values display an exponent of zero. Infinities and NaNs
2615 appear as "infinity" or "nan" respectively.
2616
2617 The above rules are as specified by C99. There is ambiguity about
2618 what the leading hexadecimal digit should be. This implementation
2619 uses whatever is necessary so that the exponent is displayed as
2620 stored. This implies the exponent will fall within the IEEE format
2621 range, and the leading hexadecimal digit will be 0 (for denormals),
2622 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2623 any other digits zero).
2624*/
Tim Shen85de51d2016-10-25 19:55:59 +00002625unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits,
2626 bool upperCase,
2627 roundingMode rounding_mode) const {
Neil Booth8f1946f2007-10-03 22:26:02 +00002628 char *p;
2629
2630 p = dst;
2631 if (sign)
2632 *dst++ = '-';
2633
2634 switch (category) {
2635 case fcInfinity:
2636 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2637 dst += sizeof infinityL - 1;
2638 break;
2639
2640 case fcNaN:
2641 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2642 dst += sizeof NaNU - 1;
2643 break;
2644
2645 case fcZero:
2646 *dst++ = '0';
2647 *dst++ = upperCase ? 'X': 'x';
2648 *dst++ = '0';
2649 if (hexDigits > 1) {
2650 *dst++ = '.';
2651 memset (dst, '0', hexDigits - 1);
2652 dst += hexDigits - 1;
2653 }
2654 *dst++ = upperCase ? 'P': 'p';
2655 *dst++ = '0';
2656 break;
2657
2658 case fcNormal:
2659 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2660 break;
2661 }
2662
2663 *dst = 0;
2664
Evan Cheng82b9e962008-05-02 21:15:08 +00002665 return static_cast<unsigned int>(dst - p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002666}
2667
2668/* Does the hard work of outputting the correctly rounded hexadecimal
2669 form of a normal floating point number with the specified number of
2670 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2671 digits necessary to print the value precisely is output. */
Tim Shen85de51d2016-10-25 19:55:59 +00002672char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2673 bool upperCase,
2674 roundingMode rounding_mode) const {
Neil Booth8f1946f2007-10-03 22:26:02 +00002675 unsigned int count, valueBits, shift, partsCount, outputDigits;
2676 const char *hexDigitChars;
2677 const integerPart *significand;
2678 char *p;
2679 bool roundUp;
2680
2681 *dst++ = '0';
2682 *dst++ = upperCase ? 'X': 'x';
2683
2684 roundUp = false;
2685 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2686
2687 significand = significandParts();
2688 partsCount = partCount();
2689
2690 /* +3 because the first digit only uses the single integer bit, so
2691 we have 3 virtual zero most-significant-bits. */
2692 valueBits = semantics->precision + 3;
2693 shift = integerPartWidth - valueBits % integerPartWidth;
2694
2695 /* The natural number of digits required ignoring trailing
2696 insignificant zeroes. */
2697 outputDigits = (valueBits - significandLSB () + 3) / 4;
2698
2699 /* hexDigits of zero means use the required number for the
2700 precision. Otherwise, see if we are truncating. If we are,
Neil Booth0ea72a92007-10-06 00:24:48 +00002701 find out if we need to round away from zero. */
Neil Booth8f1946f2007-10-03 22:26:02 +00002702 if (hexDigits) {
2703 if (hexDigits < outputDigits) {
2704 /* We are dropping non-zero bits, so need to check how to round.
2705 "bits" is the number of dropped bits. */
2706 unsigned int bits;
2707 lostFraction fraction;
2708
2709 bits = valueBits - hexDigits * 4;
2710 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2711 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2712 }
2713 outputDigits = hexDigits;
2714 }
2715
2716 /* Write the digits consecutively, and start writing in the location
2717 of the hexadecimal point. We move the most significant digit
2718 left and add the hexadecimal point later. */
2719 p = ++dst;
2720
2721 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2722
2723 while (outputDigits && count) {
2724 integerPart part;
2725
2726 /* Put the most significant integerPartWidth bits in "part". */
2727 if (--count == partsCount)
2728 part = 0; /* An imaginary higher zero part. */
2729 else
2730 part = significand[count] << shift;
2731
2732 if (count && shift)
2733 part |= significand[count - 1] >> (integerPartWidth - shift);
2734
2735 /* Convert as much of "part" to hexdigits as we can. */
2736 unsigned int curDigits = integerPartWidth / 4;
2737
2738 if (curDigits > outputDigits)
2739 curDigits = outputDigits;
2740 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2741 outputDigits -= curDigits;
2742 }
2743
2744 if (roundUp) {
2745 char *q = dst;
2746
2747 /* Note that hexDigitChars has a trailing '0'. */
2748 do {
2749 q--;
2750 *q = hexDigitChars[hexDigitValue (*q) + 1];
Neil Booth0ea72a92007-10-06 00:24:48 +00002751 } while (*q == '0');
Evan Cheng67c90212009-10-27 21:35:42 +00002752 assert(q >= p);
Neil Booth8f1946f2007-10-03 22:26:02 +00002753 } else {
2754 /* Add trailing zeroes. */
2755 memset (dst, '0', outputDigits);
2756 dst += outputDigits;
2757 }
2758
2759 /* Move the most significant digit to before the point, and if there
2760 is something after the decimal point add it. This must come
2761 after rounding above. */
2762 p[-1] = p[0];
2763 if (dst -1 == p)
2764 dst--;
2765 else
2766 p[0] = '.';
2767
2768 /* Finally output the exponent. */
2769 *dst++ = upperCase ? 'P': 'p';
2770
Neil Booth32897f52007-10-06 07:29:25 +00002771 return writeSignedDecimal (dst, exponent);
Neil Booth8f1946f2007-10-03 22:26:02 +00002772}
2773
Tim Shen85de51d2016-10-25 19:55:59 +00002774hash_code hash_value(const IEEEFloat &Arg) {
Michael Gottesman8136c382013-06-26 23:17:28 +00002775 if (!Arg.isFiniteNonZero())
Chandler Carruth71bd7d12012-03-04 12:02:57 +00002776 return hash_combine((uint8_t)Arg.category,
2777 // NaN has no sign, fix it at zero.
2778 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2779 Arg.semantics->precision);
2780
2781 // Normal floats need their exponent and significand hashed.
2782 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2783 Arg.semantics->precision, Arg.exponent,
2784 hash_combine_range(
2785 Arg.significandParts(),
2786 Arg.significandParts() + Arg.partCount()));
Dale Johannesena719a602007-08-24 00:56:33 +00002787}
2788
2789// Conversion from APFloat to/from host float/double. It may eventually be
2790// possible to eliminate these and have everybody deal with APFloats, but that
2791// will take a while. This approach will not easily extend to long double.
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002792// Current implementation requires integerPartWidth==64, which is correct at
2793// the moment but could be made more general.
Dale Johannesena719a602007-08-24 00:56:33 +00002794
Dale Johannesen728687c2007-09-05 20:39:49 +00002795// Denormals have exponent minExponent in APFloat, but minExponent-1 in
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002796// the actual IEEE respresentations. We compensate for that here.
Dale Johannesen728687c2007-09-05 20:39:49 +00002797
Tim Shen85de51d2016-10-25 19:55:59 +00002798APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002799 assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended);
Evan Cheng67c90212009-10-27 21:35:42 +00002800 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002801
2802 uint64_t myexponent, mysignificand;
2803
Michael Gottesman8136c382013-06-26 23:17:28 +00002804 if (isFiniteNonZero()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00002805 myexponent = exponent+16383; //bias
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002806 mysignificand = significandParts()[0];
Dale Johannesen245dceb2007-09-11 18:32:33 +00002807 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2808 myexponent = 0; // denormal
2809 } else if (category==fcZero) {
2810 myexponent = 0;
2811 mysignificand = 0;
2812 } else if (category==fcInfinity) {
2813 myexponent = 0x7fff;
2814 mysignificand = 0x8000000000000000ULL;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002815 } else {
2816 assert(category == fcNaN && "Unknown category");
Dale Johannesen245dceb2007-09-11 18:32:33 +00002817 myexponent = 0x7fff;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00002818 mysignificand = significandParts()[0];
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002819 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00002820
2821 uint64_t words[2];
Dale Johannesen93eefa02009-03-23 21:16:53 +00002822 words[0] = mysignificand;
2823 words[1] = ((uint64_t)(sign & 1) << 15) |
2824 (myexponent & 0x7fffLL);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002825 return APInt(80, words);
Dale Johannesen245dceb2007-09-11 18:32:33 +00002826}
2827
Tim Shen85de51d2016-10-25 19:55:59 +00002828APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const {
Tim Shenfd1e5aa2017-01-23 22:39:35 +00002829 assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy);
Evan Cheng67c90212009-10-27 21:35:42 +00002830 assert(partCount()==2);
Dale Johannesen007aa372007-10-11 18:07:22 +00002831
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002832 uint64_t words[2];
2833 opStatus fs;
2834 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00002835
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002836 // Convert number to double. To avoid spurious underflows, we re-
2837 // normalize against the "double" minExponent first, and only *then*
2838 // truncate the mantissa. The result of that second conversion
2839 // may be inexact, but should never underflow.
Alexey Samsonov2b431d92012-11-30 22:27:54 +00002840 // Declare fltSemantics before APFloat that uses it (and
2841 // saves pointer to it) to ensure correct destruction order.
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002842 fltSemantics extendedSemantics = *semantics;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002843 extendedSemantics.minExponent = semIEEEdouble.minExponent;
Tim Shen85de51d2016-10-25 19:55:59 +00002844 IEEEFloat extended(*this);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002845 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2846 assert(fs == opOK && !losesInfo);
2847 (void)fs;
2848
Tim Shen85de51d2016-10-25 19:55:59 +00002849 IEEEFloat u(extended);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002850 fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002851 assert(fs == opOK || fs == opInexact);
2852 (void)fs;
2853 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2854
2855 // If conversion was exact or resulted in a special case, we're done;
2856 // just set the second double to zero. Otherwise, re-convert back to
2857 // the extended format and compute the difference. This now should
2858 // convert exactly to double.
Michael Gottesman8136c382013-06-26 23:17:28 +00002859 if (u.isFiniteNonZero() && losesInfo) {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002860 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2861 assert(fs == opOK && !losesInfo);
2862 (void)fs;
2863
Tim Shen85de51d2016-10-25 19:55:59 +00002864 IEEEFloat v(extended);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002865 v.subtract(u, rmNearestTiesToEven);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002866 fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002867 assert(fs == opOK && !losesInfo);
2868 (void)fs;
2869 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
Dale Johannesen007aa372007-10-11 18:07:22 +00002870 } else {
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00002871 words[1] = 0;
Dale Johannesen007aa372007-10-11 18:07:22 +00002872 }
2873
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002874 return APInt(128, words);
Dale Johannesen007aa372007-10-11 18:07:22 +00002875}
2876
Tim Shen85de51d2016-10-25 19:55:59 +00002877APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002878 assert(semantics == (const llvm::fltSemantics*)&semIEEEquad);
Evan Cheng67c90212009-10-27 21:35:42 +00002879 assert(partCount()==2);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002880
2881 uint64_t myexponent, mysignificand, mysignificand2;
2882
Michael Gottesman8136c382013-06-26 23:17:28 +00002883 if (isFiniteNonZero()) {
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002884 myexponent = exponent+16383; //bias
2885 mysignificand = significandParts()[0];
2886 mysignificand2 = significandParts()[1];
2887 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2888 myexponent = 0; // denormal
2889 } else if (category==fcZero) {
2890 myexponent = 0;
2891 mysignificand = mysignificand2 = 0;
2892 } else if (category==fcInfinity) {
2893 myexponent = 0x7fff;
2894 mysignificand = mysignificand2 = 0;
2895 } else {
2896 assert(category == fcNaN && "Unknown category!");
2897 myexponent = 0x7fff;
2898 mysignificand = significandParts()[0];
2899 mysignificand2 = significandParts()[1];
2900 }
2901
2902 uint64_t words[2];
2903 words[0] = mysignificand;
2904 words[1] = ((uint64_t)(sign & 1) << 63) |
2905 ((myexponent & 0x7fff) << 48) |
Anton Korobeynikov876955c2009-08-21 23:09:47 +00002906 (mysignificand2 & 0xffffffffffffLL);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002907
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00002908 return APInt(128, words);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00002909}
2910
Tim Shen85de51d2016-10-25 19:55:59 +00002911APInt IEEEFloat::convertDoubleAPFloatToAPInt() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002912 assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble);
Evan Cheng67c90212009-10-27 21:35:42 +00002913 assert(partCount()==1);
Dale Johannesena719a602007-08-24 00:56:33 +00002914
Dale Johannesen3cf889f2007-08-31 04:03:46 +00002915 uint64_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00002916
Michael Gottesman8136c382013-06-26 23:17:28 +00002917 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00002918 myexponent = exponent+1023; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00002919 mysignificand = *significandParts();
2920 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2921 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00002922 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00002923 myexponent = 0;
2924 mysignificand = 0;
2925 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00002926 myexponent = 0x7ff;
2927 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002928 } else {
2929 assert(category == fcNaN && "Unknown category!");
Dale Johannesena719a602007-08-24 00:56:33 +00002930 myexponent = 0x7ff;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00002931 mysignificand = *significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002932 }
Dale Johannesena719a602007-08-24 00:56:33 +00002933
Evan Cheng82b9e962008-05-02 21:15:08 +00002934 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002935 ((myexponent & 0x7ff) << 52) |
2936 (mysignificand & 0xfffffffffffffLL))));
Dale Johannesena719a602007-08-24 00:56:33 +00002937}
2938
Tim Shen85de51d2016-10-25 19:55:59 +00002939APInt IEEEFloat::convertFloatAPFloatToAPInt() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002940 assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle);
Evan Cheng67c90212009-10-27 21:35:42 +00002941 assert(partCount()==1);
Neil Booth9acbf5a2007-09-26 21:33:42 +00002942
Dale Johannesen3cf889f2007-08-31 04:03:46 +00002943 uint32_t myexponent, mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00002944
Michael Gottesman8136c382013-06-26 23:17:28 +00002945 if (isFiniteNonZero()) {
Dale Johannesena719a602007-08-24 00:56:33 +00002946 myexponent = exponent+127; //bias
Evan Cheng82b9e962008-05-02 21:15:08 +00002947 mysignificand = (uint32_t)*significandParts();
Dale Johannesen06a10df2007-11-17 01:02:27 +00002948 if (myexponent == 1 && !(mysignificand & 0x800000))
Dale Johannesen728687c2007-09-05 20:39:49 +00002949 myexponent = 0; // denormal
Dale Johannesena719a602007-08-24 00:56:33 +00002950 } else if (category==fcZero) {
Dale Johannesena719a602007-08-24 00:56:33 +00002951 myexponent = 0;
2952 mysignificand = 0;
2953 } else if (category==fcInfinity) {
Dale Johannesena719a602007-08-24 00:56:33 +00002954 myexponent = 0xff;
2955 mysignificand = 0;
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002956 } else {
2957 assert(category == fcNaN && "Unknown category!");
Dale Johannesen728687c2007-09-05 20:39:49 +00002958 myexponent = 0xff;
Evan Cheng82b9e962008-05-02 21:15:08 +00002959 mysignificand = (uint32_t)*significandParts();
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002960 }
Dale Johannesena719a602007-08-24 00:56:33 +00002961
Chris Lattner2a9bcb92007-10-06 06:13:42 +00002962 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2963 (mysignificand & 0x7fffff)));
Dale Johannesena719a602007-08-24 00:56:33 +00002964}
2965
Tim Shen85de51d2016-10-25 19:55:59 +00002966APInt IEEEFloat::convertHalfAPFloatToAPInt() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002967 assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf);
Evan Cheng67c90212009-10-27 21:35:42 +00002968 assert(partCount()==1);
Chris Lattner4794b2b2009-10-16 02:13:51 +00002969
2970 uint32_t myexponent, mysignificand;
2971
Michael Gottesman8136c382013-06-26 23:17:28 +00002972 if (isFiniteNonZero()) {
Chris Lattner4794b2b2009-10-16 02:13:51 +00002973 myexponent = exponent+15; //bias
2974 mysignificand = (uint32_t)*significandParts();
2975 if (myexponent == 1 && !(mysignificand & 0x400))
2976 myexponent = 0; // denormal
2977 } else if (category==fcZero) {
2978 myexponent = 0;
2979 mysignificand = 0;
2980 } else if (category==fcInfinity) {
Dale Johannesen0d670b52009-10-23 04:02:51 +00002981 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00002982 mysignificand = 0;
2983 } else {
2984 assert(category == fcNaN && "Unknown category!");
Dale Johannesen0d670b52009-10-23 04:02:51 +00002985 myexponent = 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00002986 mysignificand = (uint32_t)*significandParts();
2987 }
2988
2989 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
2990 (mysignificand & 0x3ff)));
2991}
2992
Dale Johannesen007aa372007-10-11 18:07:22 +00002993// This function creates an APInt that is just a bit map of the floating
2994// point constant as it would appear in memory. It is not a conversion,
2995// and treating the result as a normal integer is unlikely to be useful.
2996
Tim Shen85de51d2016-10-25 19:55:59 +00002997APInt IEEEFloat::bitcastToAPInt() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002998 if (semantics == (const llvm::fltSemantics*)&semIEEEhalf)
Chris Lattner4794b2b2009-10-16 02:13:51 +00002999 return convertHalfAPFloatToAPInt();
3000
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003001 if (semantics == (const llvm::fltSemantics*)&semIEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003002 return convertFloatAPFloatToAPInt();
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003003
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003004 if (semantics == (const llvm::fltSemantics*)&semIEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003005 return convertDoubleAPFloatToAPInt();
Neil Booth9acbf5a2007-09-26 21:33:42 +00003006
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003007 if (semantics == (const llvm::fltSemantics*)&semIEEEquad)
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003008 return convertQuadrupleAPFloatToAPInt();
3009
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003010 if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy)
Dale Johannesen007aa372007-10-11 18:07:22 +00003011 return convertPPCDoubleDoubleAPFloatToAPInt();
3012
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003013 assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended &&
Chris Lattner2a9bcb92007-10-06 06:13:42 +00003014 "unknown format!");
3015 return convertF80LongDoubleAPFloatToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003016}
3017
Tim Shen85de51d2016-10-25 19:55:59 +00003018float IEEEFloat::convertToFloat() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003019 assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle &&
Chris Lattner688f9912009-09-24 21:44:20 +00003020 "Float semantics are not IEEEsingle");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003021 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003022 return api.bitsToFloat();
3023}
3024
Tim Shen85de51d2016-10-25 19:55:59 +00003025double IEEEFloat::convertToDouble() const {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003026 assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble &&
Chris Lattner688f9912009-09-24 21:44:20 +00003027 "Float semantics are not IEEEdouble");
Dale Johannesen54306fe2008-10-09 18:53:47 +00003028 APInt api = bitcastToAPInt();
Dale Johannesen245dceb2007-09-11 18:32:33 +00003029 return api.bitsToDouble();
3030}
3031
Dale Johannesenfff29952008-10-06 18:22:29 +00003032/// Integer bit is explicit in this format. Intel hardware (387 and later)
3033/// does not support these bit patterns:
3034/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3035/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3036/// exponent = 0, integer bit 1 ("pseudodenormal")
3037/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3038/// At the moment, the first two are treated as NaNs, the second two as Normal.
Tim Shen85de51d2016-10-25 19:55:59 +00003039void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00003040 assert(api.getBitWidth()==80);
3041 uint64_t i1 = api.getRawData()[0];
3042 uint64_t i2 = api.getRawData()[1];
Dale Johannesen93eefa02009-03-23 21:16:53 +00003043 uint64_t myexponent = (i2 & 0x7fff);
3044 uint64_t mysignificand = i1;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003045
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003046 initialize(&semX87DoubleExtended);
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003047 assert(partCount()==2);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003048
Dale Johannesen93eefa02009-03-23 21:16:53 +00003049 sign = static_cast<unsigned int>(i2>>15);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003050 if (myexponent==0 && mysignificand==0) {
3051 // exponent, significand meaningless
3052 category = fcZero;
3053 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3054 // exponent, significand meaningless
3055 category = fcInfinity;
3056 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3057 // exponent meaningless
3058 category = fcNaN;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003059 significandParts()[0] = mysignificand;
3060 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003061 } else {
3062 category = fcNormal;
3063 exponent = myexponent - 16383;
Dale Johannesen146a0ea2007-09-20 23:47:58 +00003064 significandParts()[0] = mysignificand;
3065 significandParts()[1] = 0;
Dale Johannesen245dceb2007-09-11 18:32:33 +00003066 if (myexponent==0) // denormal
3067 exponent = -16382;
Neil Booth9acbf5a2007-09-26 21:33:42 +00003068 }
Dale Johannesen245dceb2007-09-11 18:32:33 +00003069}
3070
Tim Shen85de51d2016-10-25 19:55:59 +00003071void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) {
Dale Johannesen007aa372007-10-11 18:07:22 +00003072 assert(api.getBitWidth()==128);
3073 uint64_t i1 = api.getRawData()[0];
3074 uint64_t i2 = api.getRawData()[1];
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003075 opStatus fs;
3076 bool losesInfo;
Dale Johannesen007aa372007-10-11 18:07:22 +00003077
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003078 // Get the first double and convert to our format.
3079 initFromDoubleAPInt(APInt(64, i1));
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003080 fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003081 assert(fs == opOK && !losesInfo);
3082 (void)fs;
Dale Johannesen007aa372007-10-11 18:07:22 +00003083
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003084 // Unless we have a special case, add in second double.
Michael Gottesman8136c382013-06-26 23:17:28 +00003085 if (isFiniteNonZero()) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003086 IEEEFloat v(semIEEEdouble, APInt(64, i2));
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003087 fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo);
Ulrich Weigandd9f7e252012-10-29 18:09:01 +00003088 assert(fs == opOK && !losesInfo);
3089 (void)fs;
3090
3091 add(v, rmNearestTiesToEven);
Dale Johannesen007aa372007-10-11 18:07:22 +00003092 }
3093}
3094
Tim Shen85de51d2016-10-25 19:55:59 +00003095void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) {
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003096 assert(api.getBitWidth()==128);
3097 uint64_t i1 = api.getRawData()[0];
3098 uint64_t i2 = api.getRawData()[1];
3099 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3100 uint64_t mysignificand = i1;
3101 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3102
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003103 initialize(&semIEEEquad);
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00003104 assert(partCount()==2);
Anton Korobeynikov13e8c7e2009-08-21 22:10:30 +00003105
3106 sign = static_cast<unsigned int>(i2>>63);
3107 if (myexponent==0 &&
3108 (mysignificand==0 && mysignificand2==0)) {
3109 // exponent, significand meaningless
3110 category = fcZero;
3111 } else if (myexponent==0x7fff &&
3112 (mysignificand==0 && mysignificand2==0)) {
3113 // exponent, significand meaningless
3114 category = fcInfinity;
3115 } else if (myexponent==0x7fff &&
3116 (mysignificand!=0 || mysignificand2 !=0)) {
3117 // exponent meaningless
3118 category = fcNaN;
3119 significandParts()[0] = mysignificand;
3120 significandParts()[1] = mysignificand2;
3121 } else {
3122 category = fcNormal;
3123 exponent = myexponent - 16383;
3124 significandParts()[0] = mysignificand;
3125 significandParts()[1] = mysignificand2;
3126 if (myexponent==0) // denormal
3127 exponent = -16382;
3128 else
3129 significandParts()[1] |= 0x1000000000000LL; // integer bit
3130 }
3131}
3132
Tim Shen85de51d2016-10-25 19:55:59 +00003133void IEEEFloat::initFromDoubleAPInt(const APInt &api) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00003134 assert(api.getBitWidth()==64);
3135 uint64_t i = *api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003136 uint64_t myexponent = (i >> 52) & 0x7ff;
3137 uint64_t mysignificand = i & 0xfffffffffffffLL;
3138
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003139 initialize(&semIEEEdouble);
Dale Johannesena719a602007-08-24 00:56:33 +00003140 assert(partCount()==1);
3141
Evan Cheng82b9e962008-05-02 21:15:08 +00003142 sign = static_cast<unsigned int>(i>>63);
Dale Johannesena719a602007-08-24 00:56:33 +00003143 if (myexponent==0 && mysignificand==0) {
3144 // exponent, significand meaningless
3145 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003146 } else if (myexponent==0x7ff && mysignificand==0) {
3147 // exponent, significand meaningless
3148 category = fcInfinity;
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003149 } else if (myexponent==0x7ff && mysignificand!=0) {
3150 // exponent meaningless
3151 category = fcNaN;
3152 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003153 } else {
Dale Johannesena719a602007-08-24 00:56:33 +00003154 category = fcNormal;
3155 exponent = myexponent - 1023;
Dale Johannesen728687c2007-09-05 20:39:49 +00003156 *significandParts() = mysignificand;
3157 if (myexponent==0) // denormal
3158 exponent = -1022;
3159 else
3160 *significandParts() |= 0x10000000000000LL; // integer bit
Neil Booth9acbf5a2007-09-26 21:33:42 +00003161 }
Dale Johannesena719a602007-08-24 00:56:33 +00003162}
3163
Tim Shen85de51d2016-10-25 19:55:59 +00003164void IEEEFloat::initFromFloatAPInt(const APInt &api) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00003165 assert(api.getBitWidth()==32);
3166 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen918c33c2007-08-24 05:08:11 +00003167 uint32_t myexponent = (i >> 23) & 0xff;
3168 uint32_t mysignificand = i & 0x7fffff;
3169
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003170 initialize(&semIEEEsingle);
Dale Johannesena719a602007-08-24 00:56:33 +00003171 assert(partCount()==1);
3172
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003173 sign = i >> 31;
Dale Johannesena719a602007-08-24 00:56:33 +00003174 if (myexponent==0 && mysignificand==0) {
3175 // exponent, significand meaningless
3176 category = fcZero;
Dale Johannesena719a602007-08-24 00:56:33 +00003177 } else if (myexponent==0xff && mysignificand==0) {
3178 // exponent, significand meaningless
3179 category = fcInfinity;
Dale Johannesen4f55d9f2007-09-25 17:25:00 +00003180 } else if (myexponent==0xff && mysignificand!=0) {
Dale Johannesena719a602007-08-24 00:56:33 +00003181 // sign, exponent, significand meaningless
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003182 category = fcNaN;
3183 *significandParts() = mysignificand;
Dale Johannesena719a602007-08-24 00:56:33 +00003184 } else {
3185 category = fcNormal;
Dale Johannesena719a602007-08-24 00:56:33 +00003186 exponent = myexponent - 127; //bias
Dale Johannesen728687c2007-09-05 20:39:49 +00003187 *significandParts() = mysignificand;
3188 if (myexponent==0) // denormal
3189 exponent = -126;
3190 else
3191 *significandParts() |= 0x800000; // integer bit
Dale Johannesena719a602007-08-24 00:56:33 +00003192 }
3193}
Dale Johannesen245dceb2007-09-11 18:32:33 +00003194
Tim Shen85de51d2016-10-25 19:55:59 +00003195void IEEEFloat::initFromHalfAPInt(const APInt &api) {
Chris Lattner4794b2b2009-10-16 02:13:51 +00003196 assert(api.getBitWidth()==16);
3197 uint32_t i = (uint32_t)*api.getRawData();
Dale Johannesen0d670b52009-10-23 04:02:51 +00003198 uint32_t myexponent = (i >> 10) & 0x1f;
Chris Lattner4794b2b2009-10-16 02:13:51 +00003199 uint32_t mysignificand = i & 0x3ff;
3200
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003201 initialize(&semIEEEhalf);
Chris Lattner4794b2b2009-10-16 02:13:51 +00003202 assert(partCount()==1);
3203
3204 sign = i >> 15;
3205 if (myexponent==0 && mysignificand==0) {
3206 // exponent, significand meaningless
3207 category = fcZero;
3208 } else if (myexponent==0x1f && mysignificand==0) {
3209 // exponent, significand meaningless
3210 category = fcInfinity;
3211 } else if (myexponent==0x1f && mysignificand!=0) {
3212 // sign, exponent, significand meaningless
3213 category = fcNaN;
3214 *significandParts() = mysignificand;
3215 } else {
3216 category = fcNormal;
3217 exponent = myexponent - 15; //bias
3218 *significandParts() = mysignificand;
3219 if (myexponent==0) // denormal
3220 exponent = -14;
3221 else
3222 *significandParts() |= 0x400; // integer bit
3223 }
3224}
3225
Dale Johannesen245dceb2007-09-11 18:32:33 +00003226/// Treat api as containing the bits of a floating point number. Currently
Dale Johannesen007aa372007-10-11 18:07:22 +00003227/// we infer the floating point type from the size of the APInt. The
3228/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3229/// when the size is anything else).
Tim Shen85de51d2016-10-25 19:55:59 +00003230void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003231 if (Sem == &semIEEEhalf)
Chris Lattner4794b2b2009-10-16 02:13:51 +00003232 return initFromHalfAPInt(api);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003233 if (Sem == &semIEEEsingle)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003234 return initFromFloatAPInt(api);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003235 if (Sem == &semIEEEdouble)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003236 return initFromDoubleAPInt(api);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003237 if (Sem == &semX87DoubleExtended)
Dale Johannesen245dceb2007-09-11 18:32:33 +00003238 return initFromF80LongDoubleAPInt(api);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003239 if (Sem == &semIEEEquad)
Tim Northover29178a32013-01-22 09:46:31 +00003240 return initFromQuadrupleAPInt(api);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003241 if (Sem == &semPPCDoubleDoubleLegacy)
Tim Northover29178a32013-01-22 09:46:31 +00003242 return initFromPPCDoubleDoubleAPInt(api);
3243
Craig Topper2617dcc2014-04-15 06:32:26 +00003244 llvm_unreachable(nullptr);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003245}
3246
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003247/// Make this number the largest magnitude normal number in the given
3248/// semantics.
Tim Shen85de51d2016-10-25 19:55:59 +00003249void IEEEFloat::makeLargest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003250 // We want (in interchange format):
3251 // sign = {Negative}
3252 // exponent = 1..10
3253 // significand = 1..1
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003254 category = fcNormal;
3255 sign = Negative;
3256 exponent = semantics->maxExponent;
John McCall29b5c282009-12-24 08:56:26 +00003257
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003258 // Use memset to set all but the highest integerPart to all ones.
3259 integerPart *significand = significandParts();
3260 unsigned PartCount = partCount();
3261 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
John McCall29b5c282009-12-24 08:56:26 +00003262
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003263 // Set the high integerPart especially setting all unused top bits for
3264 // internal consistency.
3265 const unsigned NumUnusedHighBits =
3266 PartCount*integerPartWidth - semantics->precision;
Alexey Samsonovba1ecbc2014-09-06 00:41:19 +00003267 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3268 ? (~integerPart(0) >> NumUnusedHighBits)
3269 : 0;
John McCall29b5c282009-12-24 08:56:26 +00003270}
3271
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003272/// Make this number the smallest magnitude denormal number in the given
3273/// semantics.
Tim Shen85de51d2016-10-25 19:55:59 +00003274void IEEEFloat::makeSmallest(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003275 // We want (in interchange format):
3276 // sign = {Negative}
3277 // exponent = 0..0
3278 // significand = 0..01
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003279 category = fcNormal;
3280 sign = Negative;
3281 exponent = semantics->minExponent;
3282 APInt::tcSet(significandParts(), 1, partCount());
3283}
John McCall29b5c282009-12-24 08:56:26 +00003284
Tim Shen139a58f2016-10-27 22:52:40 +00003285void IEEEFloat::makeSmallestNormalized(bool Negative) {
John McCall29b5c282009-12-24 08:56:26 +00003286 // We want (in interchange format):
3287 // sign = {Negative}
3288 // exponent = 0..0
3289 // significand = 10..0
3290
Tim Shen139a58f2016-10-27 22:52:40 +00003291 category = fcNormal;
3292 zeroSignificand();
3293 sign = Negative;
3294 exponent = semantics->minExponent;
3295 significandParts()[partCountForBits(semantics->precision) - 1] |=
3296 (((integerPart)1) << ((semantics->precision - 1) % integerPartWidth));
John McCall29b5c282009-12-24 08:56:26 +00003297}
3298
Tim Shen85de51d2016-10-25 19:55:59 +00003299IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) {
Tim Northover29178a32013-01-22 09:46:31 +00003300 initFromAPInt(&Sem, API);
Dale Johannesen245dceb2007-09-11 18:32:33 +00003301}
3302
Tim Shen85de51d2016-10-25 19:55:59 +00003303IEEEFloat::IEEEFloat(float f) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003304 initFromAPInt(&semIEEEsingle, APInt::floatToBits(f));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003305}
3306
Tim Shen85de51d2016-10-25 19:55:59 +00003307IEEEFloat::IEEEFloat(double d) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003308 initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d));
Dale Johannesen245dceb2007-09-11 18:32:33 +00003309}
John McCall29b5c282009-12-24 08:56:26 +00003310
3311namespace {
David Blaikie70fdf722012-07-25 18:04:24 +00003312 void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3313 Buffer.append(Str.begin(), Str.end());
John McCall29b5c282009-12-24 08:56:26 +00003314 }
3315
John McCalle6212ace2009-12-24 12:16:56 +00003316 /// Removes data from the given significand until it is no more
3317 /// precise than is required for the desired precision.
3318 void AdjustToPrecision(APInt &significand,
3319 int &exp, unsigned FormatPrecision) {
3320 unsigned bits = significand.getActiveBits();
3321
3322 // 196/59 is a very slight overestimate of lg_2(10).
3323 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3324
3325 if (bits <= bitsRequired) return;
3326
3327 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3328 if (!tensRemovable) return;
3329
3330 exp += tensRemovable;
3331
3332 APInt divisor(significand.getBitWidth(), 1);
3333 APInt powten(significand.getBitWidth(), 10);
3334 while (true) {
3335 if (tensRemovable & 1)
3336 divisor *= powten;
3337 tensRemovable >>= 1;
3338 if (!tensRemovable) break;
3339 powten *= powten;
3340 }
3341
3342 significand = significand.udiv(divisor);
3343
Hao Liube99cc32013-03-20 01:46:36 +00003344 // Truncate the significand down to its active bit count.
3345 significand = significand.trunc(significand.getActiveBits());
John McCalle6212ace2009-12-24 12:16:56 +00003346 }
3347
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00003348
John McCall29b5c282009-12-24 08:56:26 +00003349 void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3350 int &exp, unsigned FormatPrecision) {
3351 unsigned N = buffer.size();
3352 if (N <= FormatPrecision) return;
3353
3354 // The most significant figures are the last ones in the buffer.
3355 unsigned FirstSignificant = N - FormatPrecision;
3356
3357 // Round.
3358 // FIXME: this probably shouldn't use 'round half up'.
3359
3360 // Rounding down is just a truncation, except we also want to drop
3361 // trailing zeros from the new result.
3362 if (buffer[FirstSignificant - 1] < '5') {
NAKAMURA Takumi5adeb932012-02-19 03:18:29 +00003363 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
John McCall29b5c282009-12-24 08:56:26 +00003364 FirstSignificant++;
3365
3366 exp += FirstSignificant;
3367 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3368 return;
3369 }
3370
3371 // Rounding up requires a decimal add-with-carry. If we continue
3372 // the carry, the newly-introduced zeros will just be truncated.
3373 for (unsigned I = FirstSignificant; I != N; ++I) {
3374 if (buffer[I] == '9') {
3375 FirstSignificant++;
3376 } else {
3377 buffer[I]++;
3378 break;
3379 }
3380 }
3381
3382 // If we carried through, we have exactly one digit of precision.
3383 if (FirstSignificant == N) {
3384 exp += FirstSignificant;
3385 buffer.clear();
3386 buffer.push_back('1');
3387 return;
3388 }
3389
3390 exp += FirstSignificant;
3391 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3392 }
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00003393}
John McCall29b5c282009-12-24 08:56:26 +00003394
Tim Shen85de51d2016-10-25 19:55:59 +00003395void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
3396 unsigned FormatMaxPadding) const {
John McCall29b5c282009-12-24 08:56:26 +00003397 switch (category) {
3398 case fcInfinity:
3399 if (isNegative())
3400 return append(Str, "-Inf");
3401 else
3402 return append(Str, "+Inf");
3403
3404 case fcNaN: return append(Str, "NaN");
3405
3406 case fcZero:
3407 if (isNegative())
3408 Str.push_back('-');
3409
3410 if (!FormatMaxPadding)
3411 append(Str, "0.0E+0");
3412 else
3413 Str.push_back('0');
3414 return;
3415
3416 case fcNormal:
3417 break;
3418 }
3419
3420 if (isNegative())
3421 Str.push_back('-');
3422
3423 // Decompose the number into an APInt and an exponent.
3424 int exp = exponent - ((int) semantics->precision - 1);
3425 APInt significand(semantics->precision,
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00003426 makeArrayRef(significandParts(),
3427 partCountForBits(semantics->precision)));
John McCall29b5c282009-12-24 08:56:26 +00003428
John McCalldd5044a2009-12-24 23:18:09 +00003429 // Set FormatPrecision if zero. We want to do this before we
3430 // truncate trailing zeros, as those are part of the precision.
3431 if (!FormatPrecision) {
Eli Friedmane72f1322013-08-29 23:44:34 +00003432 // We use enough digits so the number can be round-tripped back to an
3433 // APFloat. The formula comes from "How to Print Floating-Point Numbers
3434 // Accurately" by Steele and White.
3435 // FIXME: Using a formula based purely on the precision is conservative;
3436 // we can print fewer digits depending on the actual value being printed.
John McCalldd5044a2009-12-24 23:18:09 +00003437
Eli Friedmane72f1322013-08-29 23:44:34 +00003438 // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3439 FormatPrecision = 2 + semantics->precision * 59 / 196;
John McCalldd5044a2009-12-24 23:18:09 +00003440 }
3441
John McCall29b5c282009-12-24 08:56:26 +00003442 // Ignore trailing binary zeros.
3443 int trailingZeros = significand.countTrailingZeros();
3444 exp += trailingZeros;
Craig Topperfc947bc2017-04-18 17:14:21 +00003445 significand.lshrInPlace(trailingZeros);
John McCall29b5c282009-12-24 08:56:26 +00003446
3447 // Change the exponent from 2^e to 10^e.
3448 if (exp == 0) {
3449 // Nothing to do.
3450 } else if (exp > 0) {
3451 // Just shift left.
Jay Foad583abbc2010-12-07 08:25:19 +00003452 significand = significand.zext(semantics->precision + exp);
John McCall29b5c282009-12-24 08:56:26 +00003453 significand <<= exp;
3454 exp = 0;
3455 } else { /* exp < 0 */
3456 int texp = -exp;
3457
3458 // We transform this using the identity:
3459 // (N)(2^-e) == (N)(5^e)(10^-e)
3460 // This means we have to multiply N (the significand) by 5^e.
3461 // To avoid overflow, we have to operate on numbers large
3462 // enough to store N * 5^e:
3463 // log2(N * 5^e) == log2(N) + e * log2(5)
John McCalldd5044a2009-12-24 23:18:09 +00003464 // <= semantics->precision + e * 137 / 59
3465 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003466
Eli Friedman19546412011-10-07 23:40:49 +00003467 unsigned precision = semantics->precision + (137 * texp + 136) / 59;
John McCall29b5c282009-12-24 08:56:26 +00003468
3469 // Multiply significand by 5^e.
3470 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
Jay Foad583abbc2010-12-07 08:25:19 +00003471 significand = significand.zext(precision);
John McCall29b5c282009-12-24 08:56:26 +00003472 APInt five_to_the_i(precision, 5);
3473 while (true) {
3474 if (texp & 1) significand *= five_to_the_i;
Dan Gohmanb452d4e2010-03-24 19:38:02 +00003475
John McCall29b5c282009-12-24 08:56:26 +00003476 texp >>= 1;
3477 if (!texp) break;
3478 five_to_the_i *= five_to_the_i;
3479 }
3480 }
3481
John McCalle6212ace2009-12-24 12:16:56 +00003482 AdjustToPrecision(significand, exp, FormatPrecision);
3483
Dmitri Gribenko226fea52013-01-13 16:01:15 +00003484 SmallVector<char, 256> buffer;
John McCall29b5c282009-12-24 08:56:26 +00003485
3486 // Fill the buffer.
3487 unsigned precision = significand.getBitWidth();
3488 APInt ten(precision, 10);
3489 APInt digit(precision, 0);
3490
3491 bool inTrail = true;
3492 while (significand != 0) {
3493 // digit <- significand % 10
3494 // significand <- significand / 10
3495 APInt::udivrem(significand, ten, significand, digit);
3496
3497 unsigned d = digit.getZExtValue();
3498
3499 // Drop trailing zeros.
3500 if (inTrail && !d) exp++;
3501 else {
3502 buffer.push_back((char) ('0' + d));
3503 inTrail = false;
3504 }
3505 }
3506
3507 assert(!buffer.empty() && "no characters in buffer!");
3508
3509 // Drop down to FormatPrecision.
3510 // TODO: don't do more precise calculations above than are required.
3511 AdjustToPrecision(buffer, exp, FormatPrecision);
3512
3513 unsigned NDigits = buffer.size();
3514
John McCalldd5044a2009-12-24 23:18:09 +00003515 // Check whether we should use scientific notation.
John McCall29b5c282009-12-24 08:56:26 +00003516 bool FormatScientific;
3517 if (!FormatMaxPadding)
3518 FormatScientific = true;
3519 else {
John McCall29b5c282009-12-24 08:56:26 +00003520 if (exp >= 0) {
John McCalldd5044a2009-12-24 23:18:09 +00003521 // 765e3 --> 765000
3522 // ^^^
3523 // But we shouldn't make the number look more precise than it is.
3524 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3525 NDigits + (unsigned) exp > FormatPrecision);
John McCall29b5c282009-12-24 08:56:26 +00003526 } else {
John McCalldd5044a2009-12-24 23:18:09 +00003527 // Power of the most significant digit.
3528 int MSD = exp + (int) (NDigits - 1);
3529 if (MSD >= 0) {
John McCall29b5c282009-12-24 08:56:26 +00003530 // 765e-2 == 7.65
John McCalldd5044a2009-12-24 23:18:09 +00003531 FormatScientific = false;
John McCall29b5c282009-12-24 08:56:26 +00003532 } else {
3533 // 765e-5 == 0.00765
3534 // ^ ^^
John McCalldd5044a2009-12-24 23:18:09 +00003535 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
John McCall29b5c282009-12-24 08:56:26 +00003536 }
3537 }
John McCall29b5c282009-12-24 08:56:26 +00003538 }
3539
3540 // Scientific formatting is pretty straightforward.
3541 if (FormatScientific) {
3542 exp += (NDigits - 1);
3543
3544 Str.push_back(buffer[NDigits-1]);
3545 Str.push_back('.');
3546 if (NDigits == 1)
3547 Str.push_back('0');
3548 else
3549 for (unsigned I = 1; I != NDigits; ++I)
3550 Str.push_back(buffer[NDigits-1-I]);
3551 Str.push_back('E');
3552
3553 Str.push_back(exp >= 0 ? '+' : '-');
3554 if (exp < 0) exp = -exp;
3555 SmallVector<char, 6> expbuf;
3556 do {
3557 expbuf.push_back((char) ('0' + (exp % 10)));
3558 exp /= 10;
3559 } while (exp);
3560 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3561 Str.push_back(expbuf[E-1-I]);
3562 return;
3563 }
3564
3565 // Non-scientific, positive exponents.
3566 if (exp >= 0) {
3567 for (unsigned I = 0; I != NDigits; ++I)
3568 Str.push_back(buffer[NDigits-1-I]);
3569 for (unsigned I = 0; I != (unsigned) exp; ++I)
3570 Str.push_back('0');
3571 return;
3572 }
3573
3574 // Non-scientific, negative exponents.
3575
3576 // The number of digits to the left of the decimal point.
3577 int NWholeDigits = exp + (int) NDigits;
3578
3579 unsigned I = 0;
3580 if (NWholeDigits > 0) {
3581 for (; I != (unsigned) NWholeDigits; ++I)
3582 Str.push_back(buffer[NDigits-I-1]);
3583 Str.push_back('.');
3584 } else {
3585 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3586
3587 Str.push_back('0');
3588 Str.push_back('.');
3589 for (unsigned Z = 1; Z != NZeros; ++Z)
3590 Str.push_back('0');
3591 }
3592
3593 for (; I != NDigits; ++I)
3594 Str.push_back(buffer[NDigits-I-1]);
3595}
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003596
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003597bool IEEEFloat::getExactInverse(APFloat *inv) const {
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003598 // Special floats and denormals have no exact inverse.
Michael Gottesman8136c382013-06-26 23:17:28 +00003599 if (!isFiniteNonZero())
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003600 return false;
3601
3602 // Check that the number is a power of two by making sure that only the
3603 // integer bit is set in the significand.
3604 if (significandLSB() != semantics->precision - 1)
3605 return false;
3606
3607 // Get the inverse.
Tim Shen85de51d2016-10-25 19:55:59 +00003608 IEEEFloat reciprocal(*semantics, 1ULL);
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003609 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3610 return false;
3611
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003612 // Avoid multiplication with a denormal, it is not safe on all platforms and
3613 // may be slower than a normal division.
Benjamin Kramer6bef24f2013-06-01 11:26:33 +00003614 if (reciprocal.isDenormal())
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003615 return false;
3616
Michael Gottesman8136c382013-06-26 23:17:28 +00003617 assert(reciprocal.isFiniteNonZero() &&
Benjamin Krameraf0ed952011-03-30 17:02:54 +00003618 reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3619
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003620 if (inv)
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003621 *inv = APFloat(reciprocal, *semantics);
Benjamin Kramer03fd6722011-03-30 15:42:27 +00003622
3623 return true;
3624}
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003625
Tim Shen85de51d2016-10-25 19:55:59 +00003626bool IEEEFloat::isSignaling() const {
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003627 if (!isNaN())
3628 return false;
3629
3630 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3631 // first bit of the trailing significand being 0.
3632 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3633}
3634
3635/// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3636///
3637/// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3638/// appropriate sign switching before/after the computation.
Tim Shen85de51d2016-10-25 19:55:59 +00003639IEEEFloat::opStatus IEEEFloat::next(bool nextDown) {
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003640 // If we are performing nextDown, swap sign so we have -x.
3641 if (nextDown)
3642 changeSign();
3643
3644 // Compute nextUp(x)
3645 opStatus result = opOK;
3646
3647 // Handle each float category separately.
3648 switch (category) {
3649 case fcInfinity:
3650 // nextUp(+inf) = +inf
3651 if (!isNegative())
3652 break;
3653 // nextUp(-inf) = -getLargest()
3654 makeLargest(true);
3655 break;
3656 case fcNaN:
3657 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3658 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3659 // change the payload.
3660 if (isSignaling()) {
3661 result = opInvalidOp;
Alp Tokercb402912014-01-24 17:20:08 +00003662 // For consistency, propagate the sign of the sNaN to the qNaN.
Craig Topperc10719f2014-04-07 04:17:22 +00003663 makeNaN(false, isNegative(), nullptr);
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003664 }
3665 break;
3666 case fcZero:
3667 // nextUp(pm 0) = +getSmallest()
3668 makeSmallest(false);
3669 break;
3670 case fcNormal:
3671 // nextUp(-getSmallest()) = -0
3672 if (isSmallest() && isNegative()) {
3673 APInt::tcSet(significandParts(), 0, partCount());
3674 category = fcZero;
3675 exponent = 0;
3676 break;
3677 }
3678
3679 // nextUp(getLargest()) == INFINITY
3680 if (isLargest() && !isNegative()) {
3681 APInt::tcSet(significandParts(), 0, partCount());
3682 category = fcInfinity;
3683 exponent = semantics->maxExponent + 1;
3684 break;
3685 }
3686
3687 // nextUp(normal) == normal + inc.
3688 if (isNegative()) {
3689 // If we are negative, we need to decrement the significand.
3690
3691 // We only cross a binade boundary that requires adjusting the exponent
3692 // if:
3693 // 1. exponent != semantics->minExponent. This implies we are not in the
3694 // smallest binade or are dealing with denormals.
3695 // 2. Our significand excluding the integral bit is all zeros.
3696 bool WillCrossBinadeBoundary =
3697 exponent != semantics->minExponent && isSignificandAllZeros();
3698
3699 // Decrement the significand.
3700 //
3701 // We always do this since:
Alp Tokerf907b892013-12-05 05:44:44 +00003702 // 1. If we are dealing with a non-binade decrement, by definition we
Michael Gottesman0c622ea2013-05-30 18:07:13 +00003703 // just decrement the significand.
3704 // 2. If we are dealing with a normal -> normal binade decrement, since
3705 // we have an explicit integral bit the fact that all bits but the
3706 // integral bit are zero implies that subtracting one will yield a
3707 // significand with 0 integral bit and 1 in all other spots. Thus we
3708 // must just adjust the exponent and set the integral bit to 1.
3709 // 3. If we are dealing with a normal -> denormal binade decrement,
3710 // since we set the integral bit to 0 when we represent denormals, we
3711 // just decrement the significand.
3712 integerPart *Parts = significandParts();
3713 APInt::tcDecrement(Parts, partCount());
3714
3715 if (WillCrossBinadeBoundary) {
3716 // Our result is a normal number. Do the following:
3717 // 1. Set the integral bit to 1.
3718 // 2. Decrement the exponent.
3719 APInt::tcSetBit(Parts, semantics->precision - 1);
3720 exponent--;
3721 }
3722 } else {
3723 // If we are positive, we need to increment the significand.
3724
3725 // We only cross a binade boundary that requires adjusting the exponent if
3726 // the input is not a denormal and all of said input's significand bits
3727 // are set. If all of said conditions are true: clear the significand, set
3728 // the integral bit to 1, and increment the exponent. If we have a
3729 // denormal always increment since moving denormals and the numbers in the
3730 // smallest normal binade have the same exponent in our representation.
3731 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3732
3733 if (WillCrossBinadeBoundary) {
3734 integerPart *Parts = significandParts();
3735 APInt::tcSet(Parts, 0, partCount());
3736 APInt::tcSetBit(Parts, semantics->precision - 1);
3737 assert(exponent != semantics->maxExponent &&
3738 "We can not increment an exponent beyond the maxExponent allowed"
3739 " by the given floating point semantics.");
3740 exponent++;
3741 } else {
3742 incrementSignificand();
3743 }
3744 }
3745 break;
3746 }
3747
3748 // If we are performing nextDown, swap sign so we have -nextUp(-x)
3749 if (nextDown)
3750 changeSign();
3751
3752 return result;
3753}
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003754
Tim Shen85de51d2016-10-25 19:55:59 +00003755void IEEEFloat::makeInf(bool Negative) {
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003756 category = fcInfinity;
3757 sign = Negative;
3758 exponent = semantics->maxExponent + 1;
3759 APInt::tcSet(significandParts(), 0, partCount());
3760}
3761
Tim Shen85de51d2016-10-25 19:55:59 +00003762void IEEEFloat::makeZero(bool Negative) {
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003763 category = fcZero;
3764 sign = Negative;
3765 exponent = semantics->minExponent-1;
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003766 APInt::tcSet(significandParts(), 0, partCount());
3767}
3768
Tim Shen85de51d2016-10-25 19:55:59 +00003769void IEEEFloat::makeQuiet() {
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003770 assert(isNaN());
3771 APInt::tcSetBit(significandParts(), semantics->precision - 2);
Michael Gottesmanc4facdf2013-06-24 09:58:02 +00003772}
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003773
Tim Shen85de51d2016-10-25 19:55:59 +00003774int ilogb(const IEEEFloat &Arg) {
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003775 if (Arg.isNaN())
Tim Shen85de51d2016-10-25 19:55:59 +00003776 return IEEEFloat::IEK_NaN;
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003777 if (Arg.isZero())
Tim Shen85de51d2016-10-25 19:55:59 +00003778 return IEEEFloat::IEK_Zero;
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003779 if (Arg.isInfinity())
Tim Shen85de51d2016-10-25 19:55:59 +00003780 return IEEEFloat::IEK_Inf;
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003781 if (!Arg.isDenormal())
3782 return Arg.exponent;
3783
Tim Shen85de51d2016-10-25 19:55:59 +00003784 IEEEFloat Normalized(Arg);
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003785 int SignificandBits = Arg.getSemantics().precision - 1;
3786
3787 Normalized.exponent += SignificandBits;
Tim Shen85de51d2016-10-25 19:55:59 +00003788 Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero);
Matt Arsenault69fdf9b2016-03-13 05:12:32 +00003789 return Normalized.exponent - SignificandBits;
3790}
3791
Tim Shen85de51d2016-10-25 19:55:59 +00003792IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) {
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003793 auto MaxExp = X.getSemantics().maxExponent;
3794 auto MinExp = X.getSemantics().minExponent;
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003795
Matt Arsenaultafa31cf2016-03-13 05:11:51 +00003796 // If Exp is wildly out-of-scale, simply adding it to X.exponent will
3797 // overflow; clamp it to a safe range before adding, but ensure that the range
3798 // is large enough that the clamp does not change the result. The range we
3799 // need to support is the difference between the largest possible exponent and
3800 // the normalized exponent of half the smallest denormal.
3801
3802 int SignificandBits = X.getSemantics().precision - 1;
3803 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
3804
3805 // Clamp to one past the range ends to let normalize handle overlflow.
3806 X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement);
3807 X.normalize(RoundingMode, lfExactlyZero);
Matt Arsenaultea00b492016-03-23 23:51:45 +00003808 if (X.isNaN())
3809 X.makeQuiet();
Richard Trieu6ae37962015-04-30 23:07:00 +00003810 return X;
Chandler Carruthd9edd1e2014-10-10 04:54:30 +00003811}
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003812
Tim Shen85de51d2016-10-25 19:55:59 +00003813IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) {
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003814 Exp = ilogb(Val);
3815
3816 // Quiet signalling nans.
Tim Shen85de51d2016-10-25 19:55:59 +00003817 if (Exp == IEEEFloat::IEK_NaN) {
3818 IEEEFloat Quiet(Val);
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003819 Quiet.makeQuiet();
3820 return Quiet;
3821 }
3822
Tim Shen85de51d2016-10-25 19:55:59 +00003823 if (Exp == IEEEFloat::IEK_Inf)
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003824 return Val;
3825
3826 // 1 is added because frexp is defined to return a normalized fraction in
3827 // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0).
Tim Shen85de51d2016-10-25 19:55:59 +00003828 Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1;
Matt Arsenaultc25a7112016-03-21 16:49:16 +00003829 return scalbn(Val, -Exp, RM);
3830}
Tim Shen85de51d2016-10-25 19:55:59 +00003831
Tim Shen139a58f2016-10-27 22:52:40 +00003832DoubleAPFloat::DoubleAPFloat(const fltSemantics &S)
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003833 : Semantics(&S),
3834 Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003835 assert(Semantics == &semPPCDoubleDouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003836}
3837
3838DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag)
3839 : Semantics(&S),
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003840 Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized),
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003841 APFloat(semIEEEdouble, uninitialized)}) {
3842 assert(Semantics == &semPPCDoubleDouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003843}
3844
3845DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I)
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003846 : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I),
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003847 APFloat(semIEEEdouble)}) {
3848 assert(Semantics == &semPPCDoubleDouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003849}
3850
3851DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I)
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003852 : Semantics(&S),
3853 Floats(new APFloat[2]{
3854 APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])),
3855 APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003856 assert(Semantics == &semPPCDoubleDouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003857}
3858
3859DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First,
3860 APFloat &&Second)
3861 : Semantics(&S),
3862 Floats(new APFloat[2]{std::move(First), std::move(Second)}) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003863 assert(Semantics == &semPPCDoubleDouble);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00003864 assert(&Floats[0].getSemantics() == &semIEEEdouble);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003865 assert(&Floats[1].getSemantics() == &semIEEEdouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003866}
3867
3868DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS)
3869 : Semantics(RHS.Semantics),
Tim Shenb49915482016-10-28 22:45:33 +00003870 Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]),
3871 APFloat(RHS.Floats[1])}
3872 : nullptr) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003873 assert(Semantics == &semPPCDoubleDouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003874}
3875
3876DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS)
3877 : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003878 RHS.Semantics = &semBogus;
3879 assert(Semantics == &semPPCDoubleDouble);
Tim Shen139a58f2016-10-27 22:52:40 +00003880}
3881
3882DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) {
Tim Shenb49915482016-10-28 22:45:33 +00003883 if (Semantics == RHS.Semantics && RHS.Floats) {
Tim Shen139a58f2016-10-27 22:52:40 +00003884 Floats[0] = RHS.Floats[0];
3885 Floats[1] = RHS.Floats[1];
3886 } else if (this != &RHS) {
3887 this->~DoubleAPFloat();
3888 new (this) DoubleAPFloat(RHS);
3889 }
3890 return *this;
3891}
3892
Tim Shen7f127622017-01-24 00:19:45 +00003893// Implement addition, subtraction, multiplication and division based on:
Tim Shen44bde892016-12-12 21:59:30 +00003894// "Software for Doubled-Precision Floating-Point Computations",
3895// by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283.
3896APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa,
3897 const APFloat &c, const APFloat &cc,
3898 roundingMode RM) {
3899 int Status = opOK;
3900 APFloat z = a;
3901 Status |= z.add(c, RM);
3902 if (!z.isFinite()) {
3903 if (!z.isInfinity()) {
3904 Floats[0] = std::move(z);
Tim Shen7117e692017-01-26 00:11:07 +00003905 Floats[1].makeZero(/* Neg = */ false);
Tim Shen44bde892016-12-12 21:59:30 +00003906 return (opStatus)Status;
3907 }
3908 Status = opOK;
3909 auto AComparedToC = a.compareAbsoluteValue(c);
3910 z = cc;
3911 Status |= z.add(aa, RM);
3912 if (AComparedToC == APFloat::cmpGreaterThan) {
3913 // z = cc + aa + c + a;
3914 Status |= z.add(c, RM);
3915 Status |= z.add(a, RM);
3916 } else {
3917 // z = cc + aa + a + c;
3918 Status |= z.add(a, RM);
3919 Status |= z.add(c, RM);
3920 }
3921 if (!z.isFinite()) {
3922 Floats[0] = std::move(z);
Tim Shen7117e692017-01-26 00:11:07 +00003923 Floats[1].makeZero(/* Neg = */ false);
Tim Shen44bde892016-12-12 21:59:30 +00003924 return (opStatus)Status;
3925 }
3926 Floats[0] = z;
3927 APFloat zz = aa;
3928 Status |= zz.add(cc, RM);
3929 if (AComparedToC == APFloat::cmpGreaterThan) {
3930 // Floats[1] = a - z + c + zz;
3931 Floats[1] = a;
3932 Status |= Floats[1].subtract(z, RM);
3933 Status |= Floats[1].add(c, RM);
3934 Status |= Floats[1].add(zz, RM);
3935 } else {
3936 // Floats[1] = c - z + a + zz;
3937 Floats[1] = c;
3938 Status |= Floats[1].subtract(z, RM);
3939 Status |= Floats[1].add(a, RM);
3940 Status |= Floats[1].add(zz, RM);
3941 }
3942 } else {
3943 // q = a - z;
3944 APFloat q = a;
3945 Status |= q.subtract(z, RM);
3946
3947 // zz = q + c + (a - (q + z)) + aa + cc;
3948 // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies.
3949 auto zz = q;
3950 Status |= zz.add(c, RM);
3951 Status |= q.add(z, RM);
3952 Status |= q.subtract(a, RM);
3953 q.changeSign();
3954 Status |= zz.add(q, RM);
3955 Status |= zz.add(aa, RM);
3956 Status |= zz.add(cc, RM);
3957 if (zz.isZero() && !zz.isNegative()) {
3958 Floats[0] = std::move(z);
Tim Shen7117e692017-01-26 00:11:07 +00003959 Floats[1].makeZero(/* Neg = */ false);
Tim Shen44bde892016-12-12 21:59:30 +00003960 return opOK;
3961 }
3962 Floats[0] = z;
3963 Status |= Floats[0].add(zz, RM);
3964 if (!Floats[0].isFinite()) {
Tim Shen7117e692017-01-26 00:11:07 +00003965 Floats[1].makeZero(/* Neg = */ false);
Tim Shen44bde892016-12-12 21:59:30 +00003966 return (opStatus)Status;
3967 }
3968 Floats[1] = std::move(z);
3969 Status |= Floats[1].subtract(Floats[0], RM);
3970 Status |= Floats[1].add(zz, RM);
3971 }
3972 return (opStatus)Status;
3973}
3974
3975APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS,
3976 const DoubleAPFloat &RHS,
3977 DoubleAPFloat &Out,
3978 roundingMode RM) {
3979 if (LHS.getCategory() == fcNaN) {
3980 Out = LHS;
3981 return opOK;
3982 }
3983 if (RHS.getCategory() == fcNaN) {
3984 Out = RHS;
3985 return opOK;
3986 }
3987 if (LHS.getCategory() == fcZero) {
3988 Out = RHS;
3989 return opOK;
3990 }
3991 if (RHS.getCategory() == fcZero) {
3992 Out = LHS;
3993 return opOK;
3994 }
3995 if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity &&
3996 LHS.isNegative() != RHS.isNegative()) {
3997 Out.makeNaN(false, Out.isNegative(), nullptr);
3998 return opInvalidOp;
3999 }
4000 if (LHS.getCategory() == fcInfinity) {
4001 Out = LHS;
4002 return opOK;
4003 }
4004 if (RHS.getCategory() == fcInfinity) {
4005 Out = RHS;
4006 return opOK;
4007 }
4008 assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal);
4009
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004010 APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]),
Tim Shen44bde892016-12-12 21:59:30 +00004011 CC(RHS.Floats[1]);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004012 assert(&A.getSemantics() == &semIEEEdouble);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004013 assert(&AA.getSemantics() == &semIEEEdouble);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004014 assert(&C.getSemantics() == &semIEEEdouble);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004015 assert(&CC.getSemantics() == &semIEEEdouble);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004016 assert(&Out.Floats[0].getSemantics() == &semIEEEdouble);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004017 assert(&Out.Floats[1].getSemantics() == &semIEEEdouble);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004018 return Out.addImpl(A, AA, C, CC, RM);
Tim Shen44bde892016-12-12 21:59:30 +00004019}
4020
4021APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS,
4022 roundingMode RM) {
4023 return addWithSpecial(*this, RHS, *this, RM);
4024}
4025
4026APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS,
4027 roundingMode RM) {
4028 changeSign();
4029 auto Ret = add(RHS, RM);
4030 changeSign();
4031 return Ret;
4032}
4033
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004034APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS,
4035 APFloat::roundingMode RM) {
Tim Shen7f127622017-01-24 00:19:45 +00004036 const auto &LHS = *this;
4037 auto &Out = *this;
4038 /* Interesting observation: For special categories, finding the lowest
4039 common ancestor of the following layered graph gives the correct
4040 return category:
4041
4042 NaN
4043 / \
4044 Zero Inf
4045 \ /
4046 Normal
4047
4048 e.g. NaN * NaN = NaN
4049 Zero * Inf = NaN
4050 Normal * Zero = Zero
4051 Normal * Inf = Inf
4052 */
4053 if (LHS.getCategory() == fcNaN) {
4054 Out = LHS;
4055 return opOK;
4056 }
4057 if (RHS.getCategory() == fcNaN) {
4058 Out = RHS;
4059 return opOK;
4060 }
4061 if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) ||
4062 (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) {
4063 Out.makeNaN(false, false, nullptr);
4064 return opOK;
4065 }
4066 if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) {
4067 Out = LHS;
4068 return opOK;
4069 }
4070 if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) {
4071 Out = RHS;
4072 return opOK;
4073 }
4074 assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal &&
4075 "Special cases not handled exhaustively");
4076
4077 int Status = opOK;
4078 APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1];
4079 // t = a * c
4080 APFloat T = A;
4081 Status |= T.multiply(C, RM);
4082 if (!T.isFiniteNonZero()) {
4083 Floats[0] = T;
Tim Shen7117e692017-01-26 00:11:07 +00004084 Floats[1].makeZero(/* Neg = */ false);
Tim Shen7f127622017-01-24 00:19:45 +00004085 return (opStatus)Status;
4086 }
4087
4088 // tau = fmsub(a, c, t), that is -fmadd(-a, c, t).
4089 APFloat Tau = A;
4090 T.changeSign();
4091 Status |= Tau.fusedMultiplyAdd(C, T, RM);
4092 T.changeSign();
4093 {
4094 // v = a * d
4095 APFloat V = A;
4096 Status |= V.multiply(D, RM);
4097 // w = b * c
4098 APFloat W = B;
4099 Status |= W.multiply(C, RM);
4100 Status |= V.add(W, RM);
4101 // tau += v + w
4102 Status |= Tau.add(V, RM);
4103 }
4104 // u = t + tau
4105 APFloat U = T;
4106 Status |= U.add(Tau, RM);
4107
4108 Floats[0] = U;
4109 if (!U.isFinite()) {
Tim Shen7117e692017-01-26 00:11:07 +00004110 Floats[1].makeZero(/* Neg = */ false);
Tim Shen7f127622017-01-24 00:19:45 +00004111 } else {
4112 // Floats[1] = (t - u) + tau
4113 Status |= T.subtract(U, RM);
4114 Status |= T.add(Tau, RM);
4115 Floats[1] = T;
4116 }
4117 return (opStatus)Status;
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004118}
4119
4120APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS,
4121 APFloat::roundingMode RM) {
4122 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4123 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4124 auto Ret =
4125 Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM);
4126 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4127 return Ret;
4128}
4129
4130APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) {
4131 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4132 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4133 auto Ret =
4134 Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
4135 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4136 return Ret;
4137}
4138
4139APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) {
4140 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4141 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4142 auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
4143 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4144 return Ret;
4145}
4146
4147APFloat::opStatus
4148DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
4149 const DoubleAPFloat &Addend,
4150 APFloat::roundingMode RM) {
4151 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4152 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4153 auto Ret = Tmp.fusedMultiplyAdd(
4154 APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()),
4155 APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM);
4156 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4157 return Ret;
4158}
4159
4160APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) {
4161 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4162 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4163 auto Ret = Tmp.roundToIntegral(RM);
4164 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4165 return Ret;
4166}
4167
Tim Shen44bde892016-12-12 21:59:30 +00004168void DoubleAPFloat::changeSign() {
4169 Floats[0].changeSign();
4170 Floats[1].changeSign();
4171}
4172
4173APFloat::cmpResult
4174DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const {
4175 auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]);
4176 if (Result != cmpEqual)
4177 return Result;
4178 Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]);
4179 if (Result == cmpLessThan || Result == cmpGreaterThan) {
4180 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4181 auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative();
4182 if (Against && !RHSAgainst)
4183 return cmpLessThan;
4184 if (!Against && RHSAgainst)
4185 return cmpGreaterThan;
4186 if (!Against && !RHSAgainst)
4187 return Result;
4188 if (Against && RHSAgainst)
4189 return (cmpResult)(cmpLessThan + cmpGreaterThan - Result);
4190 }
4191 return Result;
4192}
4193
4194APFloat::fltCategory DoubleAPFloat::getCategory() const {
4195 return Floats[0].getCategory();
4196}
4197
4198bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); }
4199
4200void DoubleAPFloat::makeInf(bool Neg) {
4201 Floats[0].makeInf(Neg);
Tim Shen7117e692017-01-26 00:11:07 +00004202 Floats[1].makeZero(/* Neg = */ false);
Tim Shen44bde892016-12-12 21:59:30 +00004203}
4204
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004205void DoubleAPFloat::makeZero(bool Neg) {
4206 Floats[0].makeZero(Neg);
Tim Shen7117e692017-01-26 00:11:07 +00004207 Floats[1].makeZero(/* Neg = */ false);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004208}
4209
4210void DoubleAPFloat::makeLargest(bool Neg) {
4211 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4212 Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull));
4213 Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull));
4214 if (Neg)
4215 changeSign();
4216}
4217
4218void DoubleAPFloat::makeSmallest(bool Neg) {
4219 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4220 Floats[0].makeSmallest(Neg);
Tim Shen7117e692017-01-26 00:11:07 +00004221 Floats[1].makeZero(/* Neg = */ false);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004222}
4223
4224void DoubleAPFloat::makeSmallestNormalized(bool Neg) {
4225 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4226 Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull));
4227 if (Neg)
4228 Floats[0].changeSign();
Tim Shen7117e692017-01-26 00:11:07 +00004229 Floats[1].makeZero(/* Neg = */ false);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004230}
4231
Tim Shen44bde892016-12-12 21:59:30 +00004232void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) {
4233 Floats[0].makeNaN(SNaN, Neg, fill);
Tim Shen7117e692017-01-26 00:11:07 +00004234 Floats[1].makeZero(/* Neg = */ false);
Tim Shen44bde892016-12-12 21:59:30 +00004235}
4236
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004237APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const {
4238 auto Result = Floats[0].compare(RHS.Floats[0]);
Tim Shen7117e692017-01-26 00:11:07 +00004239 // |Float[0]| > |Float[1]|
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004240 if (Result == APFloat::cmpEqual)
4241 return Floats[1].compare(RHS.Floats[1]);
4242 return Result;
4243}
4244
4245bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const {
4246 return Floats[0].bitwiseIsEqual(RHS.Floats[0]) &&
4247 Floats[1].bitwiseIsEqual(RHS.Floats[1]);
4248}
4249
4250hash_code hash_value(const DoubleAPFloat &Arg) {
4251 if (Arg.Floats)
4252 return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1]));
4253 return hash_combine(Arg.Semantics);
4254}
4255
4256APInt DoubleAPFloat::bitcastToAPInt() const {
4257 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4258 uint64_t Data[] = {
4259 Floats[0].bitcastToAPInt().getRawData()[0],
4260 Floats[1].bitcastToAPInt().getRawData()[0],
4261 };
4262 return APInt(128, 2, Data);
4263}
4264
4265APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S,
4266 roundingMode RM) {
4267 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4268 APFloat Tmp(semPPCDoubleDoubleLegacy);
4269 auto Ret = Tmp.convertFromString(S, RM);
4270 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4271 return Ret;
4272}
4273
4274APFloat::opStatus DoubleAPFloat::next(bool nextDown) {
4275 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4276 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4277 auto Ret = Tmp.next(nextDown);
4278 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4279 return Ret;
4280}
4281
Simon Pilgrim00b34992017-03-20 14:40:12 +00004282APFloat::opStatus
4283DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input,
4284 unsigned int Width, bool IsSigned,
4285 roundingMode RM, bool *IsExact) const {
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004286 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4287 return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4288 .convertToInteger(Input, Width, IsSigned, RM, IsExact);
4289}
4290
4291APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input,
4292 bool IsSigned,
4293 roundingMode RM) {
4294 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4295 APFloat Tmp(semPPCDoubleDoubleLegacy);
4296 auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM);
4297 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4298 return Ret;
4299}
4300
4301APFloat::opStatus
4302DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input,
4303 unsigned int InputSize,
4304 bool IsSigned, roundingMode RM) {
4305 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4306 APFloat Tmp(semPPCDoubleDoubleLegacy);
4307 auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM);
4308 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4309 return Ret;
4310}
4311
4312APFloat::opStatus
4313DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input,
4314 unsigned int InputSize,
4315 bool IsSigned, roundingMode RM) {
4316 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4317 APFloat Tmp(semPPCDoubleDoubleLegacy);
4318 auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM);
4319 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4320 return Ret;
4321}
4322
4323unsigned int DoubleAPFloat::convertToHexString(char *DST,
4324 unsigned int HexDigits,
4325 bool UpperCase,
4326 roundingMode RM) const {
4327 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4328 return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4329 .convertToHexString(DST, HexDigits, UpperCase, RM);
4330}
4331
4332bool DoubleAPFloat::isDenormal() const {
4333 return getCategory() == fcNormal &&
4334 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
Tim Shen7117e692017-01-26 00:11:07 +00004335 // (double)(Hi + Lo) == Hi defines a normal number.
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004336 Floats[0].compare(Floats[0] + Floats[1]) != cmpEqual);
4337}
4338
4339bool DoubleAPFloat::isSmallest() const {
4340 if (getCategory() != fcNormal)
4341 return false;
4342 DoubleAPFloat Tmp(*this);
4343 Tmp.makeSmallest(this->isNegative());
4344 return Tmp.compare(*this) == cmpEqual;
4345}
4346
4347bool DoubleAPFloat::isLargest() const {
4348 if (getCategory() != fcNormal)
4349 return false;
4350 DoubleAPFloat Tmp(*this);
4351 Tmp.makeLargest(this->isNegative());
4352 return Tmp.compare(*this) == cmpEqual;
4353}
4354
4355bool DoubleAPFloat::isInteger() const {
4356 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4357 APFloat Tmp(semPPCDoubleDoubleLegacy);
4358 (void)Tmp.add(Floats[0], rmNearestTiesToEven);
4359 (void)Tmp.add(Floats[1], rmNearestTiesToEven);
4360 return Tmp.isInteger();
4361}
4362
4363void DoubleAPFloat::toString(SmallVectorImpl<char> &Str,
4364 unsigned FormatPrecision,
4365 unsigned FormatMaxPadding) const {
4366 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4367 APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4368 .toString(Str, FormatPrecision, FormatMaxPadding);
4369}
4370
4371bool DoubleAPFloat::getExactInverse(APFloat *inv) const {
4372 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4373 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4374 if (!inv)
4375 return Tmp.getExactInverse(nullptr);
4376 APFloat Inv(semPPCDoubleDoubleLegacy);
4377 auto Ret = Tmp.getExactInverse(&Inv);
4378 *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt());
4379 return Ret;
4380}
4381
4382DoubleAPFloat scalbn(DoubleAPFloat Arg, int Exp, APFloat::roundingMode RM) {
4383 assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4384 return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM),
4385 scalbn(Arg.Floats[1], Exp, RM));
4386}
4387
4388DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp,
4389 APFloat::roundingMode RM) {
4390 assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4391 APFloat First = frexp(Arg.Floats[0], Exp, RM);
4392 APFloat Second = Arg.Floats[1];
4393 if (Arg.getCategory() == APFloat::fcNormal)
4394 Second = scalbn(Second, -Exp, RM);
4395 return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second));
4396}
4397
Tim Shen85de51d2016-10-25 19:55:59 +00004398} // End detail namespace
4399
Tim Shen398f90f2016-11-06 07:38:37 +00004400APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
4401 if (usesLayout<IEEEFloat>(Semantics)) {
4402 new (&IEEE) IEEEFloat(std::move(F));
Tim Shen7b57ac42016-12-21 02:39:21 +00004403 return;
4404 }
4405 if (usesLayout<DoubleAPFloat>(Semantics)) {
Tim Shen398f90f2016-11-06 07:38:37 +00004406 new (&Double)
4407 DoubleAPFloat(Semantics, APFloat(std::move(F), F.getSemantics()),
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004408 APFloat(semIEEEdouble));
Tim Shen7b57ac42016-12-21 02:39:21 +00004409 return;
Tim Shen398f90f2016-11-06 07:38:37 +00004410 }
Tim Shen7b57ac42016-12-21 02:39:21 +00004411 llvm_unreachable("Unexpected semantics");
Tim Shen398f90f2016-11-06 07:38:37 +00004412}
4413
Tim Shen85de51d2016-10-25 19:55:59 +00004414APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) {
Tim Shen601ba8c2017-01-27 02:11:07 +00004415 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
Tim Shen85de51d2016-10-25 19:55:59 +00004416}
4417
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004418hash_code hash_value(const APFloat &Arg) {
4419 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics()))
4420 return hash_value(Arg.U.IEEE);
4421 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics()))
4422 return hash_value(Arg.U.Double);
4423 llvm_unreachable("Unexpected semantics");
4424}
Tim Shen85de51d2016-10-25 19:55:59 +00004425
4426APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
Tim Shen139a58f2016-10-27 22:52:40 +00004427 : APFloat(Semantics) {
4428 convertFromString(S, rmNearestTiesToEven);
4429}
4430
4431APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics,
4432 roundingMode RM, bool *losesInfo) {
4433 if (&getSemantics() == &ToSemantics)
4434 return opOK;
4435 if (usesLayout<IEEEFloat>(getSemantics()) &&
Tim Shen7b57ac42016-12-21 02:39:21 +00004436 usesLayout<IEEEFloat>(ToSemantics))
Tim Shen139a58f2016-10-27 22:52:40 +00004437 return U.IEEE.convert(ToSemantics, RM, losesInfo);
Tim Shen7b57ac42016-12-21 02:39:21 +00004438 if (usesLayout<IEEEFloat>(getSemantics()) &&
4439 usesLayout<DoubleAPFloat>(ToSemantics)) {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004440 assert(&ToSemantics == &semPPCDoubleDouble);
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004441 auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo);
4442 *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
Tim Shen139a58f2016-10-27 22:52:40 +00004443 return Ret;
Tim Shen7b57ac42016-12-21 02:39:21 +00004444 }
4445 if (usesLayout<DoubleAPFloat>(getSemantics()) &&
4446 usesLayout<IEEEFloat>(ToSemantics)) {
Tim Shen139a58f2016-10-27 22:52:40 +00004447 auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo);
Tim Shen398f90f2016-11-06 07:38:37 +00004448 *this = APFloat(std::move(getIEEE()), ToSemantics);
Tim Shen139a58f2016-10-27 22:52:40 +00004449 return Ret;
Tim Shen139a58f2016-10-27 22:52:40 +00004450 }
Tim Shen7b57ac42016-12-21 02:39:21 +00004451 llvm_unreachable("Unexpected semantics");
Tim Shen139a58f2016-10-27 22:52:40 +00004452}
Tim Shen85de51d2016-10-25 19:55:59 +00004453
Tim Shen398f90f2016-11-06 07:38:37 +00004454APFloat APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) {
4455 if (isIEEE) {
4456 switch (BitWidth) {
4457 case 16:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004458 return APFloat(semIEEEhalf, APInt::getAllOnesValue(BitWidth));
Tim Shen398f90f2016-11-06 07:38:37 +00004459 case 32:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004460 return APFloat(semIEEEsingle, APInt::getAllOnesValue(BitWidth));
Tim Shen398f90f2016-11-06 07:38:37 +00004461 case 64:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004462 return APFloat(semIEEEdouble, APInt::getAllOnesValue(BitWidth));
Tim Shen398f90f2016-11-06 07:38:37 +00004463 case 80:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004464 return APFloat(semX87DoubleExtended, APInt::getAllOnesValue(BitWidth));
Tim Shen398f90f2016-11-06 07:38:37 +00004465 case 128:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004466 return APFloat(semIEEEquad, APInt::getAllOnesValue(BitWidth));
Tim Shen398f90f2016-11-06 07:38:37 +00004467 default:
4468 llvm_unreachable("Unknown floating bit width");
4469 }
4470 } else {
4471 assert(BitWidth == 128);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004472 return APFloat(semPPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
Tim Shen398f90f2016-11-06 07:38:37 +00004473 }
4474}
4475
Tim Shen44bde892016-12-12 21:59:30 +00004476void APFloat::print(raw_ostream &OS) const {
4477 SmallVector<char, 16> Buffer;
4478 toString(Buffer);
4479 OS << Buffer << "\n";
4480}
4481
Matthias Braun8c209aa2017-01-28 02:02:38 +00004482#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4483LLVM_DUMP_METHOD void APFloat::dump() const { print(dbgs()); }
4484#endif
Tim Shen44bde892016-12-12 21:59:30 +00004485
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004486void APFloat::Profile(FoldingSetNodeID &NID) const {
4487 NID.Add(bitcastToAPInt());
4488}
4489
4490/* Same as convertToInteger(integerPart*, ...), except the result is returned in
4491 an APSInt, whose initial bit-width and signed-ness are used to determine the
4492 precision of the conversion.
4493 */
4494APFloat::opStatus APFloat::convertToInteger(APSInt &result,
4495 roundingMode rounding_mode,
4496 bool *isExact) const {
4497 unsigned bitWidth = result.getBitWidth();
4498 SmallVector<uint64_t, 4> parts(result.getNumWords());
Simon Pilgrim00b34992017-03-20 14:40:12 +00004499 opStatus status = convertToInteger(parts, bitWidth, result.isSigned(),
Tim Shenfd1e5aa2017-01-23 22:39:35 +00004500 rounding_mode, isExact);
4501 // Keeps the original signed-ness.
4502 result = APInt(bitWidth, parts);
4503 return status;
4504}
4505
Tim Shen85de51d2016-10-25 19:55:59 +00004506} // End llvm namespace
Tim Shen601ba8c2017-01-27 02:11:07 +00004507
4508#undef APFLOAT_DISPATCH_ON_SEMANTICS