Bug Summary

File:lib/Support/APFloat.cpp
Warning:line 4422, column 56
Method called on moved-from object 'F'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name APFloat.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/lib/Support -I /build/llvm-toolchain-snapshot-9~svn362543/lib/Support -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn362543/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/lib/Support -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn362543=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-06-05-060531-1271-1 -x c++ /build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp -faddrsig
1//===-- APFloat.cpp - Implement APFloat class -----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a class to represent arbitrary precision floating
10// point values and provide a variety of arithmetic operations on them.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/APFloat.h"
15#include "llvm/ADT/APSInt.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/Hashing.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Config/llvm-config.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/raw_ostream.h"
26#include <cstring>
27#include <limits.h>
28
29#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")::llvm::llvm_unreachable_internal("Unexpected semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 35)
; \
36 } while (false)
37
38using namespace llvm;
39
40/// A macro used to combine two fcCategory enums into one key which can be used
41/// in a switch statement to classify how the interaction of two APFloat's
42/// categories affects an operation.
43///
44/// TODO: If clang source code is ever allowed to use constexpr in its own
45/// codebase, change this into a static inline function.
46#define PackCategoriesIntoKey(_lhs, _rhs)((_lhs) * 4 + (_rhs)) ((_lhs) * 4 + (_rhs))
47
48/* Assumed in hexadecimal significand parsing, and conversion to
49 hexadecimal strings. */
50static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part width must be divisible by 4!");
51
52namespace llvm {
53 /* Represents floating point arithmetic semantics. */
54 struct fltSemantics {
55 /* The largest E such that 2^E is representable; this matches the
56 definition of IEEE 754. */
57 APFloatBase::ExponentType maxExponent;
58
59 /* The smallest E such that 2^E is a normalized number; this
60 matches the definition of IEEE 754. */
61 APFloatBase::ExponentType minExponent;
62
63 /* Number of bits in the significand. This includes the integer
64 bit. */
65 unsigned int precision;
66
67 /* Number of bits actually used in the semantics. */
68 unsigned int sizeInBits;
69 };
70
71 static const fltSemantics semIEEEhalf = {15, -14, 11, 16};
72 static const fltSemantics semIEEEsingle = {127, -126, 24, 32};
73 static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64};
74 static const fltSemantics semIEEEquad = {16383, -16382, 113, 128};
75 static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80};
76 static const fltSemantics semBogus = {0, 0, 0, 0};
77
78 /* The IBM double-double semantics. Such a number consists of a pair of IEEE
79 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
80 (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
81 Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent
82 to each other, and two 11-bit exponents.
83
84 Note: we need to make the value different from semBogus as otherwise
85 an unsafe optimization may collapse both values to a single address,
86 and we heavily rely on them having distinct addresses. */
87 static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0};
88
89 /* These are legacy semantics for the fallback, inaccrurate implementation of
90 IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the
91 operation. It's equivalent to having an IEEE number with consecutive 106
92 bits of mantissa and 11 bits of exponent.
93
94 It's not equivalent to IBM double-double. For example, a legit IBM
95 double-double, 1 + epsilon:
96
97 1 + epsilon = 1 + (1 >> 1076)
98
99 is not representable by a consecutive 106 bits of mantissa.
100
101 Currently, these semantics are used in the following way:
102
103 semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
104 (64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
105 semPPCDoubleDoubleLegacy -> IEEE operations
106
107 We use bitcastToAPInt() to get the bit representation (in APInt) of the
108 underlying IEEEdouble, then use the APInt constructor to construct the
109 legacy IEEE float.
110
111 TODO: Implement all operations in semPPCDoubleDouble, and delete these
112 semantics. */
113 static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53,
114 53 + 53, 128};
115
116 const fltSemantics &APFloatBase::IEEEhalf() {
117 return semIEEEhalf;
118 }
119 const fltSemantics &APFloatBase::IEEEsingle() {
120 return semIEEEsingle;
121 }
122 const fltSemantics &APFloatBase::IEEEdouble() {
123 return semIEEEdouble;
124 }
125 const fltSemantics &APFloatBase::IEEEquad() {
126 return semIEEEquad;
127 }
128 const fltSemantics &APFloatBase::x87DoubleExtended() {
129 return semX87DoubleExtended;
130 }
131 const fltSemantics &APFloatBase::Bogus() {
132 return semBogus;
133 }
134 const fltSemantics &APFloatBase::PPCDoubleDouble() {
135 return semPPCDoubleDouble;
136 }
137
138 /* A tight upper bound on number of parts required to hold the value
139 pow(5, power) is
140
141 power * 815 / (351 * integerPartWidth) + 1
142
143 However, whilst the result may require only this many parts,
144 because we are multiplying two values to get it, the
145 multiplication may require an extra part with the excess part
146 being zero (consider the trivial case of 1 * 1, tcFullMultiply
147 requires two parts to hold the single-part result). So we add an
148 extra one to guarantee enough space whilst multiplying. */
149 const unsigned int maxExponent = 16383;
150 const unsigned int maxPrecision = 113;
151 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
152 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth));
153
154 unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) {
155 return semantics.precision;
156 }
157 APFloatBase::ExponentType
158 APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) {
159 return semantics.maxExponent;
160 }
161 APFloatBase::ExponentType
162 APFloatBase::semanticsMinExponent(const fltSemantics &semantics) {
163 return semantics.minExponent;
164 }
165 unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) {
166 return semantics.sizeInBits;
167 }
168
169 unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) {
170 return Sem.sizeInBits;
171}
172
173/* A bunch of private, handy routines. */
174
175static inline unsigned int
176partCountForBits(unsigned int bits)
177{
178 return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth;
179}
180
181/* Returns 0U-9U. Return values >= 10U are not digits. */
182static inline unsigned int
183decDigitValue(unsigned int c)
184{
185 return c - '0';
186}
187
188/* Return the value of a decimal exponent of the form
189 [+-]ddddddd.
190
191 If the exponent overflows, returns a large exponent with the
192 appropriate sign. */
193static int
194readExponent(StringRef::iterator begin, StringRef::iterator end)
195{
196 bool isNegative;
197 unsigned int absExponent;
198 const unsigned int overlargeExponent = 24000; /* FIXME. */
199 StringRef::iterator p = begin;
200
201 // Treat no exponent as 0 to match binutils
202 if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) {
203 return 0;
204 }
205
206 isNegative = (*p == '-');
207 if (*p == '-' || *p == '+') {
208 p++;
209 assert(p != end && "Exponent has no digits")((p != end && "Exponent has no digits") ? static_cast
<void> (0) : __assert_fail ("p != end && \"Exponent has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 209, __PRETTY_FUNCTION__))
;
210 }
211
212 absExponent = decDigitValue(*p++);
213 assert(absExponent < 10U && "Invalid character in exponent")((absExponent < 10U && "Invalid character in exponent"
) ? static_cast<void> (0) : __assert_fail ("absExponent < 10U && \"Invalid character in exponent\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 213, __PRETTY_FUNCTION__))
;
214
215 for (; p != end; ++p) {
216 unsigned int value;
217
218 value = decDigitValue(*p);
219 assert(value < 10U && "Invalid character in exponent")((value < 10U && "Invalid character in exponent") ?
static_cast<void> (0) : __assert_fail ("value < 10U && \"Invalid character in exponent\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 219, __PRETTY_FUNCTION__))
;
220
221 value += absExponent * 10;
222 if (absExponent >= overlargeExponent) {
223 absExponent = overlargeExponent;
224 p = end; /* outwit assert below */
225 break;
226 }
227 absExponent = value;
228 }
229
230 assert(p == end && "Invalid exponent in exponent")((p == end && "Invalid exponent in exponent") ? static_cast
<void> (0) : __assert_fail ("p == end && \"Invalid exponent in exponent\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 230, __PRETTY_FUNCTION__))
;
231
232 if (isNegative)
233 return -(int) absExponent;
234 else
235 return (int) absExponent;
236}
237
238/* This is ugly and needs cleaning up, but I don't immediately see
239 how whilst remaining safe. */
240static int
241totalExponent(StringRef::iterator p, StringRef::iterator end,
242 int exponentAdjustment)
243{
244 int unsignedExponent;
245 bool negative, overflow;
246 int exponent = 0;
247
248 assert(p != end && "Exponent has no digits")((p != end && "Exponent has no digits") ? static_cast
<void> (0) : __assert_fail ("p != end && \"Exponent has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 248, __PRETTY_FUNCTION__))
;
249
250 negative = *p == '-';
251 if (*p == '-' || *p == '+') {
252 p++;
253 assert(p != end && "Exponent has no digits")((p != end && "Exponent has no digits") ? static_cast
<void> (0) : __assert_fail ("p != end && \"Exponent has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 253, __PRETTY_FUNCTION__))
;
254 }
255
256 unsignedExponent = 0;
257 overflow = false;
258 for (; p != end; ++p) {
259 unsigned int value;
260
261 value = decDigitValue(*p);
262 assert(value < 10U && "Invalid character in exponent")((value < 10U && "Invalid character in exponent") ?
static_cast<void> (0) : __assert_fail ("value < 10U && \"Invalid character in exponent\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 262, __PRETTY_FUNCTION__))
;
263
264 unsignedExponent = unsignedExponent * 10 + value;
265 if (unsignedExponent > 32767) {
266 overflow = true;
267 break;
268 }
269 }
270
271 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
272 overflow = true;
273
274 if (!overflow) {
275 exponent = unsignedExponent;
276 if (negative)
277 exponent = -exponent;
278 exponent += exponentAdjustment;
279 if (exponent > 32767 || exponent < -32768)
280 overflow = true;
281 }
282
283 if (overflow)
284 exponent = negative ? -32768: 32767;
285
286 return exponent;
287}
288
289static StringRef::iterator
290skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
291 StringRef::iterator *dot)
292{
293 StringRef::iterator p = begin;
294 *dot = end;
295 while (p != end && *p == '0')
296 p++;
297
298 if (p != end && *p == '.') {
299 *dot = p++;
300
301 assert(end - begin != 1 && "Significand has no digits")((end - begin != 1 && "Significand has no digits") ? static_cast
<void> (0) : __assert_fail ("end - begin != 1 && \"Significand has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 301, __PRETTY_FUNCTION__))
;
302
303 while (p != end && *p == '0')
304 p++;
305 }
306
307 return p;
308}
309
310/* Given a normal decimal floating point number of the form
311
312 dddd.dddd[eE][+-]ddd
313
314 where the decimal point and exponent are optional, fill out the
315 structure D. Exponent is appropriate if the significand is
316 treated as an integer, and normalizedExponent if the significand
317 is taken to have the decimal point after a single leading
318 non-zero digit.
319
320 If the value is zero, V->firstSigDigit points to a non-digit, and
321 the return exponent is zero.
322*/
323struct decimalInfo {
324 const char *firstSigDigit;
325 const char *lastSigDigit;
326 int exponent;
327 int normalizedExponent;
328};
329
330static void
331interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
332 decimalInfo *D)
333{
334 StringRef::iterator dot = end;
335 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
336
337 D->firstSigDigit = p;
338 D->exponent = 0;
339 D->normalizedExponent = 0;
340
341 for (; p != end; ++p) {
342 if (*p == '.') {
343 assert(dot == end && "String contains multiple dots")((dot == end && "String contains multiple dots") ? static_cast
<void> (0) : __assert_fail ("dot == end && \"String contains multiple dots\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 343, __PRETTY_FUNCTION__))
;
344 dot = p++;
345 if (p == end)
346 break;
347 }
348 if (decDigitValue(*p) >= 10U)
349 break;
350 }
351
352 if (p != end) {
353 assert((*p == 'e' || *p == 'E') && "Invalid character in significand")(((*p == 'e' || *p == 'E') && "Invalid character in significand"
) ? static_cast<void> (0) : __assert_fail ("(*p == 'e' || *p == 'E') && \"Invalid character in significand\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 353, __PRETTY_FUNCTION__))
;
354 assert(p != begin && "Significand has no digits")((p != begin && "Significand has no digits") ? static_cast
<void> (0) : __assert_fail ("p != begin && \"Significand has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 354, __PRETTY_FUNCTION__))
;
355 assert((dot == end || p - begin != 1) && "Significand has no digits")(((dot == end || p - begin != 1) && "Significand has no digits"
) ? static_cast<void> (0) : __assert_fail ("(dot == end || p - begin != 1) && \"Significand has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 355, __PRETTY_FUNCTION__))
;
356
357 /* p points to the first non-digit in the string */
358 D->exponent = readExponent(p + 1, end);
359
360 /* Implied decimal point? */
361 if (dot == end)
362 dot = p;
363 }
364
365 /* If number is all zeroes accept any exponent. */
366 if (p != D->firstSigDigit) {
367 /* Drop insignificant trailing zeroes. */
368 if (p != begin) {
369 do
370 do
371 p--;
372 while (p != begin && *p == '0');
373 while (p != begin && *p == '.');
374 }
375
376 /* Adjust the exponents for any decimal point. */
377 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
378 D->normalizedExponent = (D->exponent +
379 static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
380 - (dot > D->firstSigDigit && dot < p)));
381 }
382
383 D->lastSigDigit = p;
384}
385
386/* Return the trailing fraction of a hexadecimal number.
387 DIGITVALUE is the first hex digit of the fraction, P points to
388 the next digit. */
389static lostFraction
390trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
391 unsigned int digitValue)
392{
393 unsigned int hexDigit;
394
395 /* If the first trailing digit isn't 0 or 8 we can work out the
396 fraction immediately. */
397 if (digitValue > 8)
398 return lfMoreThanHalf;
399 else if (digitValue < 8 && digitValue > 0)
400 return lfLessThanHalf;
401
402 // Otherwise we need to find the first non-zero digit.
403 while (p != end && (*p == '0' || *p == '.'))
404 p++;
405
406 assert(p != end && "Invalid trailing hexadecimal fraction!")((p != end && "Invalid trailing hexadecimal fraction!"
) ? static_cast<void> (0) : __assert_fail ("p != end && \"Invalid trailing hexadecimal fraction!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 406, __PRETTY_FUNCTION__))
;
407
408 hexDigit = hexDigitValue(*p);
409
410 /* If we ran off the end it is exactly zero or one-half, otherwise
411 a little more. */
412 if (hexDigit == -1U)
413 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
414 else
415 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
416}
417
418/* Return the fraction lost were a bignum truncated losing the least
419 significant BITS bits. */
420static lostFraction
421lostFractionThroughTruncation(const APFloatBase::integerPart *parts,
422 unsigned int partCount,
423 unsigned int bits)
424{
425 unsigned int lsb;
426
427 lsb = APInt::tcLSB(parts, partCount);
428
429 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
430 if (bits <= lsb)
431 return lfExactlyZero;
432 if (bits == lsb + 1)
433 return lfExactlyHalf;
434 if (bits <= partCount * APFloatBase::integerPartWidth &&
435 APInt::tcExtractBit(parts, bits - 1))
436 return lfMoreThanHalf;
437
438 return lfLessThanHalf;
439}
440
441/* Shift DST right BITS bits noting lost fraction. */
442static lostFraction
443shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits)
444{
445 lostFraction lost_fraction;
446
447 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
448
449 APInt::tcShiftRight(dst, parts, bits);
450
451 return lost_fraction;
452}
453
454/* Combine the effect of two lost fractions. */
455static lostFraction
456combineLostFractions(lostFraction moreSignificant,
457 lostFraction lessSignificant)
458{
459 if (lessSignificant != lfExactlyZero) {
460 if (moreSignificant == lfExactlyZero)
461 moreSignificant = lfLessThanHalf;
462 else if (moreSignificant == lfExactlyHalf)
463 moreSignificant = lfMoreThanHalf;
464 }
465
466 return moreSignificant;
467}
468
469/* The error from the true value, in half-ulps, on multiplying two
470 floating point numbers, which differ from the value they
471 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
472 than the returned value.
473
474 See "How to Read Floating Point Numbers Accurately" by William D
475 Clinger. */
476static unsigned int
477HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
478{
479 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8))((HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8))
? static_cast<void> (0) : __assert_fail ("HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 479, __PRETTY_FUNCTION__))
;
480
481 if (HUerr1 + HUerr2 == 0)
482 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
483 else
484 return inexactMultiply + 2 * (HUerr1 + HUerr2);
485}
486
487/* The number of ulps from the boundary (zero, or half if ISNEAREST)
488 when the least significant BITS are truncated. BITS cannot be
489 zero. */
490static APFloatBase::integerPart
491ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits,
492 bool isNearest) {
493 unsigned int count, partBits;
494 APFloatBase::integerPart part, boundary;
495
496 assert(bits != 0)((bits != 0) ? static_cast<void> (0) : __assert_fail ("bits != 0"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 496, __PRETTY_FUNCTION__))
;
497
498 bits--;
499 count = bits / APFloatBase::integerPartWidth;
500 partBits = bits % APFloatBase::integerPartWidth + 1;
501
502 part = parts[count] & (~(APFloatBase::integerPart) 0 >> (APFloatBase::integerPartWidth - partBits));
503
504 if (isNearest)
505 boundary = (APFloatBase::integerPart) 1 << (partBits - 1);
506 else
507 boundary = 0;
508
509 if (count == 0) {
510 if (part - boundary <= boundary - part)
511 return part - boundary;
512 else
513 return boundary - part;
514 }
515
516 if (part == boundary) {
517 while (--count)
518 if (parts[count])
519 return ~(APFloatBase::integerPart) 0; /* A lot. */
520
521 return parts[0];
522 } else if (part == boundary - 1) {
523 while (--count)
524 if (~parts[count])
525 return ~(APFloatBase::integerPart) 0; /* A lot. */
526
527 return -parts[0];
528 }
529
530 return ~(APFloatBase::integerPart) 0; /* A lot. */
531}
532
533/* Place pow(5, power) in DST, and return the number of parts used.
534 DST must be at least one part larger than size of the answer. */
535static unsigned int
536powerOf5(APFloatBase::integerPart *dst, unsigned int power) {
537 static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 };
538 APFloatBase::integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
539 pow5s[0] = 78125 * 5;
540
541 unsigned int partsCount[16] = { 1 };
542 APFloatBase::integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
543 unsigned int result;
544 assert(power <= maxExponent)((power <= maxExponent) ? static_cast<void> (0) : __assert_fail
("power <= maxExponent", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 544, __PRETTY_FUNCTION__))
;
545
546 p1 = dst;
547 p2 = scratch;
548
549 *p1 = firstEightPowers[power & 7];
550 power >>= 3;
551
552 result = 1;
553 pow5 = pow5s;
554
555 for (unsigned int n = 0; power; power >>= 1, n++) {
556 unsigned int pc;
557
558 pc = partsCount[n];
559
560 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
561 if (pc == 0) {
562 pc = partsCount[n - 1];
563 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
564 pc *= 2;
565 if (pow5[pc - 1] == 0)
566 pc--;
567 partsCount[n] = pc;
568 }
569
570 if (power & 1) {
571 APFloatBase::integerPart *tmp;
572
573 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
574 result += pc;
575 if (p2[result - 1] == 0)
576 result--;
577
578 /* Now result is in p1 with partsCount parts and p2 is scratch
579 space. */
580 tmp = p1;
581 p1 = p2;
582 p2 = tmp;
583 }
584
585 pow5 += pc;
586 }
587
588 if (p1 != dst)
589 APInt::tcAssign(dst, p1, result);
590
591 return result;
592}
593
594/* Zero at the end to avoid modular arithmetic when adding one; used
595 when rounding up during hexadecimal output. */
596static const char hexDigitsLower[] = "0123456789abcdef0";
597static const char hexDigitsUpper[] = "0123456789ABCDEF0";
598static const char infinityL[] = "infinity";
599static const char infinityU[] = "INFINITY";
600static const char NaNL[] = "nan";
601static const char NaNU[] = "NAN";
602
603/* Write out an integerPart in hexadecimal, starting with the most
604 significant nibble. Write out exactly COUNT hexdigits, return
605 COUNT. */
606static unsigned int
607partAsHex (char *dst, APFloatBase::integerPart part, unsigned int count,
608 const char *hexDigitChars)
609{
610 unsigned int result = count;
611
612 assert(count != 0 && count <= APFloatBase::integerPartWidth / 4)((count != 0 && count <= APFloatBase::integerPartWidth
/ 4) ? static_cast<void> (0) : __assert_fail ("count != 0 && count <= APFloatBase::integerPartWidth / 4"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 612, __PRETTY_FUNCTION__))
;
613
614 part >>= (APFloatBase::integerPartWidth - 4 * count);
615 while (count--) {
616 dst[count] = hexDigitChars[part & 0xf];
617 part >>= 4;
618 }
619
620 return result;
621}
622
623/* Write out an unsigned decimal integer. */
624static char *
625writeUnsignedDecimal (char *dst, unsigned int n)
626{
627 char buff[40], *p;
628
629 p = buff;
630 do
631 *p++ = '0' + n % 10;
632 while (n /= 10);
633
634 do
635 *dst++ = *--p;
636 while (p != buff);
637
638 return dst;
639}
640
641/* Write out a signed decimal integer. */
642static char *
643writeSignedDecimal (char *dst, int value)
644{
645 if (value < 0) {
646 *dst++ = '-';
647 dst = writeUnsignedDecimal(dst, -(unsigned) value);
648 } else
649 dst = writeUnsignedDecimal(dst, value);
650
651 return dst;
652}
653
654namespace detail {
655/* Constructors. */
656void IEEEFloat::initialize(const fltSemantics *ourSemantics) {
657 unsigned int count;
658
659 semantics = ourSemantics;
660 count = partCount();
661 if (count > 1)
662 significand.parts = new integerPart[count];
663}
664
665void IEEEFloat::freeSignificand() {
666 if (needsCleanup())
667 delete [] significand.parts;
668}
669
670void IEEEFloat::assign(const IEEEFloat &rhs) {
671 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 671, __PRETTY_FUNCTION__))
;
672
673 sign = rhs.sign;
674 category = rhs.category;
675 exponent = rhs.exponent;
676 if (isFiniteNonZero() || category == fcNaN)
677 copySignificand(rhs);
678}
679
680void IEEEFloat::copySignificand(const IEEEFloat &rhs) {
681 assert(isFiniteNonZero() || category == fcNaN)((isFiniteNonZero() || category == fcNaN) ? static_cast<void
> (0) : __assert_fail ("isFiniteNonZero() || category == fcNaN"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 681, __PRETTY_FUNCTION__))
;
682 assert(rhs.partCount() >= partCount())((rhs.partCount() >= partCount()) ? static_cast<void>
(0) : __assert_fail ("rhs.partCount() >= partCount()", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 682, __PRETTY_FUNCTION__))
;
683
684 APInt::tcAssign(significandParts(), rhs.significandParts(),
685 partCount());
686}
687
688/* Make this number a NaN, with an arbitrary but deterministic value
689 for the significand. If double or longer, this is a signalling NaN,
690 which may not be ideal. If float, this is QNaN(0). */
691void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) {
692 category = fcNaN;
693 sign = Negative;
694
695 integerPart *significand = significandParts();
696 unsigned numParts = partCount();
697
698 // Set the significand bits to the fill.
699 if (!fill || fill->getNumWords() < numParts)
700 APInt::tcSet(significand, 0, numParts);
701 if (fill) {
702 APInt::tcAssign(significand, fill->getRawData(),
703 std::min(fill->getNumWords(), numParts));
704
705 // Zero out the excess bits of the significand.
706 unsigned bitsToPreserve = semantics->precision - 1;
707 unsigned part = bitsToPreserve / 64;
708 bitsToPreserve %= 64;
709 significand[part] &= ((1ULL << bitsToPreserve) - 1);
710 for (part++; part != numParts; ++part)
711 significand[part] = 0;
712 }
713
714 unsigned QNaNBit = semantics->precision - 2;
715
716 if (SNaN) {
717 // We always have to clear the QNaN bit to make it an SNaN.
718 APInt::tcClearBit(significand, QNaNBit);
719
720 // If there are no bits set in the payload, we have to set
721 // *something* to make it a NaN instead of an infinity;
722 // conventionally, this is the next bit down from the QNaN bit.
723 if (APInt::tcIsZero(significand, numParts))
724 APInt::tcSetBit(significand, QNaNBit - 1);
725 } else {
726 // We always have to set the QNaN bit to make it a QNaN.
727 APInt::tcSetBit(significand, QNaNBit);
728 }
729
730 // For x87 extended precision, we want to make a NaN, not a
731 // pseudo-NaN. Maybe we should expose the ability to make
732 // pseudo-NaNs?
733 if (semantics == &semX87DoubleExtended)
734 APInt::tcSetBit(significand, QNaNBit + 1);
735}
736
737IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) {
738 if (this != &rhs) {
739 if (semantics != rhs.semantics) {
740 freeSignificand();
741 initialize(rhs.semantics);
742 }
743 assign(rhs);
744 }
745
746 return *this;
747}
748
749IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) {
750 freeSignificand();
751
752 semantics = rhs.semantics;
753 significand = rhs.significand;
754 exponent = rhs.exponent;
755 category = rhs.category;
756 sign = rhs.sign;
757
758 rhs.semantics = &semBogus;
759 return *this;
760}
761
762bool IEEEFloat::isDenormal() const {
763 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
764 (APInt::tcExtractBit(significandParts(),
765 semantics->precision - 1) == 0);
766}
767
768bool IEEEFloat::isSmallest() const {
769 // The smallest number by magnitude in our format will be the smallest
770 // denormal, i.e. the floating point number with exponent being minimum
771 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
772 return isFiniteNonZero() && exponent == semantics->minExponent &&
773 significandMSB() == 0;
774}
775
776bool IEEEFloat::isSignificandAllOnes() const {
777 // Test if the significand excluding the integral bit is all ones. This allows
778 // us to test for binade boundaries.
779 const integerPart *Parts = significandParts();
780 const unsigned PartCount = partCount();
781 for (unsigned i = 0; i < PartCount - 1; i++)
782 if (~Parts[i])
783 return false;
784
785 // Set the unused high bits to all ones when we compare.
786 const unsigned NumHighBits =
787 PartCount*integerPartWidth - semantics->precision + 1;
788 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "((NumHighBits <= integerPartWidth && "Can not have more high bits to "
"fill than integerPartWidth") ? static_cast<void> (0) :
__assert_fail ("NumHighBits <= integerPartWidth && \"Can not have more high bits to \" \"fill than integerPartWidth\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 789, __PRETTY_FUNCTION__))
789 "fill than integerPartWidth")((NumHighBits <= integerPartWidth && "Can not have more high bits to "
"fill than integerPartWidth") ? static_cast<void> (0) :
__assert_fail ("NumHighBits <= integerPartWidth && \"Can not have more high bits to \" \"fill than integerPartWidth\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 789, __PRETTY_FUNCTION__))
;
790 const integerPart HighBitFill =
791 ~integerPart(0) << (integerPartWidth - NumHighBits);
792 if (~(Parts[PartCount - 1] | HighBitFill))
793 return false;
794
795 return true;
796}
797
798bool IEEEFloat::isSignificandAllZeros() const {
799 // Test if the significand excluding the integral bit is all zeros. This
800 // allows us to test for binade boundaries.
801 const integerPart *Parts = significandParts();
802 const unsigned PartCount = partCount();
803
804 for (unsigned i = 0; i < PartCount - 1; i++)
805 if (Parts[i])
806 return false;
807
808 const unsigned NumHighBits =
809 PartCount*integerPartWidth - semantics->precision + 1;
810 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "((NumHighBits <= integerPartWidth && "Can not have more high bits to "
"clear than integerPartWidth") ? static_cast<void> (0)
: __assert_fail ("NumHighBits <= integerPartWidth && \"Can not have more high bits to \" \"clear than integerPartWidth\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 811, __PRETTY_FUNCTION__))
811 "clear than integerPartWidth")((NumHighBits <= integerPartWidth && "Can not have more high bits to "
"clear than integerPartWidth") ? static_cast<void> (0)
: __assert_fail ("NumHighBits <= integerPartWidth && \"Can not have more high bits to \" \"clear than integerPartWidth\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 811, __PRETTY_FUNCTION__))
;
812 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
813
814 if (Parts[PartCount - 1] & HighBitMask)
815 return false;
816
817 return true;
818}
819
820bool IEEEFloat::isLargest() const {
821 // The largest number by magnitude in our format will be the floating point
822 // number with maximum exponent and with significand that is all ones.
823 return isFiniteNonZero() && exponent == semantics->maxExponent
824 && isSignificandAllOnes();
825}
826
827bool IEEEFloat::isInteger() const {
828 // This could be made more efficient; I'm going for obviously correct.
829 if (!isFinite()) return false;
830 IEEEFloat truncated = *this;
831 truncated.roundToIntegral(rmTowardZero);
832 return compare(truncated) == cmpEqual;
833}
834
835bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const {
836 if (this == &rhs)
837 return true;
838 if (semantics != rhs.semantics ||
839 category != rhs.category ||
840 sign != rhs.sign)
841 return false;
842 if (category==fcZero || category==fcInfinity)
843 return true;
844
845 if (isFiniteNonZero() && exponent != rhs.exponent)
846 return false;
847
848 return std::equal(significandParts(), significandParts() + partCount(),
849 rhs.significandParts());
850}
851
852IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) {
853 initialize(&ourSemantics);
854 sign = 0;
855 category = fcNormal;
856 zeroSignificand();
857 exponent = ourSemantics.precision - 1;
858 significandParts()[0] = value;
859 normalize(rmNearestTiesToEven, lfExactlyZero);
860}
861
862IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) {
863 initialize(&ourSemantics);
864 category = fcZero;
865 sign = false;
866}
867
868// Delegate to the previous constructor, because later copy constructor may
869// actually inspects category, which can't be garbage.
870IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag)
871 : IEEEFloat(ourSemantics) {}
872
873IEEEFloat::IEEEFloat(const IEEEFloat &rhs) {
874 initialize(rhs.semantics);
875 assign(rhs);
876}
877
878IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&semBogus) {
879 *this = std::move(rhs);
6
Object 'F' is moved
880}
881
882IEEEFloat::~IEEEFloat() { freeSignificand(); }
883
884unsigned int IEEEFloat::partCount() const {
885 return partCountForBits(semantics->precision + 1);
886}
887
888const IEEEFloat::integerPart *IEEEFloat::significandParts() const {
889 return const_cast<IEEEFloat *>(this)->significandParts();
890}
891
892IEEEFloat::integerPart *IEEEFloat::significandParts() {
893 if (partCount() > 1)
894 return significand.parts;
895 else
896 return &significand.part;
897}
898
899void IEEEFloat::zeroSignificand() {
900 APInt::tcSet(significandParts(), 0, partCount());
901}
902
903/* Increment an fcNormal floating point number's significand. */
904void IEEEFloat::incrementSignificand() {
905 integerPart carry;
906
907 carry = APInt::tcIncrement(significandParts(), partCount());
908
909 /* Our callers should never cause us to overflow. */
910 assert(carry == 0)((carry == 0) ? static_cast<void> (0) : __assert_fail (
"carry == 0", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 910, __PRETTY_FUNCTION__))
;
911 (void)carry;
912}
913
914/* Add the significand of the RHS. Returns the carry flag. */
915IEEEFloat::integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) {
916 integerPart *parts;
917
918 parts = significandParts();
919
920 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 920, __PRETTY_FUNCTION__))
;
921 assert(exponent == rhs.exponent)((exponent == rhs.exponent) ? static_cast<void> (0) : __assert_fail
("exponent == rhs.exponent", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 921, __PRETTY_FUNCTION__))
;
922
923 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
924}
925
926/* Subtract the significand of the RHS with a borrow flag. Returns
927 the borrow flag. */
928IEEEFloat::integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs,
929 integerPart borrow) {
930 integerPart *parts;
931
932 parts = significandParts();
933
934 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 934, __PRETTY_FUNCTION__))
;
935 assert(exponent == rhs.exponent)((exponent == rhs.exponent) ? static_cast<void> (0) : __assert_fail
("exponent == rhs.exponent", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 935, __PRETTY_FUNCTION__))
;
936
937 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
938 partCount());
939}
940
941/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
942 on to the full-precision result of the multiplication. Returns the
943 lost fraction. */
944lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs,
945 const IEEEFloat *addend) {
946 unsigned int omsb; // One, not zero, based MSB.
947 unsigned int partsCount, newPartsCount, precision;
948 integerPart *lhsSignificand;
949 integerPart scratch[4];
950 integerPart *fullSignificand;
951 lostFraction lost_fraction;
952 bool ignored;
953
954 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 954, __PRETTY_FUNCTION__))
;
955
956 precision = semantics->precision;
957
958 // Allocate space for twice as many bits as the original significand, plus one
959 // extra bit for the addition to overflow into.
960 newPartsCount = partCountForBits(precision * 2 + 1);
961
962 if (newPartsCount > 4)
963 fullSignificand = new integerPart[newPartsCount];
964 else
965 fullSignificand = scratch;
966
967 lhsSignificand = significandParts();
968 partsCount = partCount();
969
970 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
971 rhs.significandParts(), partsCount, partsCount);
972
973 lost_fraction = lfExactlyZero;
974 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
975 exponent += rhs.exponent;
976
977 // Assume the operands involved in the multiplication are single-precision
978 // FP, and the two multiplicants are:
979 // *this = a23 . a22 ... a0 * 2^e1
980 // rhs = b23 . b22 ... b0 * 2^e2
981 // the result of multiplication is:
982 // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2)
983 // Note that there are three significant bits at the left-hand side of the
984 // radix point: two for the multiplication, and an overflow bit for the
985 // addition (that will always be zero at this point). Move the radix point
986 // toward left by two bits, and adjust exponent accordingly.
987 exponent += 2;
988
989 if (addend && addend->isNonZero()) {
990 // The intermediate result of the multiplication has "2 * precision"
991 // signicant bit; adjust the addend to be consistent with mul result.
992 //
993 Significand savedSignificand = significand;
994 const fltSemantics *savedSemantics = semantics;
995 fltSemantics extendedSemantics;
996 opStatus status;
997 unsigned int extendedPrecision;
998
999 // Normalize our MSB to one below the top bit to allow for overflow.
1000 extendedPrecision = 2 * precision + 1;
1001 if (omsb != extendedPrecision - 1) {
1002 assert(extendedPrecision > omsb)((extendedPrecision > omsb) ? static_cast<void> (0) :
__assert_fail ("extendedPrecision > omsb", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1002, __PRETTY_FUNCTION__))
;
1003 APInt::tcShiftLeft(fullSignificand, newPartsCount,
1004 (extendedPrecision - 1) - omsb);
1005 exponent -= (extendedPrecision - 1) - omsb;
1006 }
1007
1008 /* Create new semantics. */
1009 extendedSemantics = *semantics;
1010 extendedSemantics.precision = extendedPrecision;
1011
1012 if (newPartsCount == 1)
1013 significand.part = fullSignificand[0];
1014 else
1015 significand.parts = fullSignificand;
1016 semantics = &extendedSemantics;
1017
1018 IEEEFloat extendedAddend(*addend);
1019 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
1020 assert(status == opOK)((status == opOK) ? static_cast<void> (0) : __assert_fail
("status == opOK", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1020, __PRETTY_FUNCTION__))
;
1021 (void)status;
1022
1023 // Shift the significand of the addend right by one bit. This guarantees
1024 // that the high bit of the significand is zero (same as fullSignificand),
1025 // so the addition will overflow (if it does overflow at all) into the top bit.
1026 lost_fraction = extendedAddend.shiftSignificandRight(1);
1027 assert(lost_fraction == lfExactlyZero &&((lost_fraction == lfExactlyZero && "Lost precision while shifting addend for fused-multiply-add."
) ? static_cast<void> (0) : __assert_fail ("lost_fraction == lfExactlyZero && \"Lost precision while shifting addend for fused-multiply-add.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1028, __PRETTY_FUNCTION__))
1028 "Lost precision while shifting addend for fused-multiply-add.")((lost_fraction == lfExactlyZero && "Lost precision while shifting addend for fused-multiply-add."
) ? static_cast<void> (0) : __assert_fail ("lost_fraction == lfExactlyZero && \"Lost precision while shifting addend for fused-multiply-add.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1028, __PRETTY_FUNCTION__))
;
1029
1030 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1031
1032 /* Restore our state. */
1033 if (newPartsCount == 1)
1034 fullSignificand[0] = significand.part;
1035 significand = savedSignificand;
1036 semantics = savedSemantics;
1037
1038 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1039 }
1040
1041 // Convert the result having "2 * precision" significant-bits back to the one
1042 // having "precision" significant-bits. First, move the radix point from
1043 // poision "2*precision - 1" to "precision - 1". The exponent need to be
1044 // adjusted by "2*precision - 1" - "precision - 1" = "precision".
1045 exponent -= precision + 1;
1046
1047 // In case MSB resides at the left-hand side of radix point, shift the
1048 // mantissa right by some amount to make sure the MSB reside right before
1049 // the radix point (i.e. "MSB . rest-significant-bits").
1050 //
1051 // Note that the result is not normalized when "omsb < precision". So, the
1052 // caller needs to call IEEEFloat::normalize() if normalized value is
1053 // expected.
1054 if (omsb > precision) {
1055 unsigned int bits, significantParts;
1056 lostFraction lf;
1057
1058 bits = omsb - precision;
1059 significantParts = partCountForBits(omsb);
1060 lf = shiftRight(fullSignificand, significantParts, bits);
1061 lost_fraction = combineLostFractions(lf, lost_fraction);
1062 exponent += bits;
1063 }
1064
1065 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1066
1067 if (newPartsCount > 4)
1068 delete [] fullSignificand;
1069
1070 return lost_fraction;
1071}
1072
1073/* Multiply the significands of LHS and RHS to DST. */
1074lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) {
1075 unsigned int bit, i, partsCount;
1076 const integerPart *rhsSignificand;
1077 integerPart *lhsSignificand, *dividend, *divisor;
1078 integerPart scratch[4];
1079 lostFraction lost_fraction;
1080
1081 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1081, __PRETTY_FUNCTION__))
;
1082
1083 lhsSignificand = significandParts();
1084 rhsSignificand = rhs.significandParts();
1085 partsCount = partCount();
1086
1087 if (partsCount > 2)
1088 dividend = new integerPart[partsCount * 2];
1089 else
1090 dividend = scratch;
1091
1092 divisor = dividend + partsCount;
1093
1094 /* Copy the dividend and divisor as they will be modified in-place. */
1095 for (i = 0; i < partsCount; i++) {
1096 dividend[i] = lhsSignificand[i];
1097 divisor[i] = rhsSignificand[i];
1098 lhsSignificand[i] = 0;
1099 }
1100
1101 exponent -= rhs.exponent;
1102
1103 unsigned int precision = semantics->precision;
1104
1105 /* Normalize the divisor. */
1106 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
1107 if (bit) {
1108 exponent += bit;
1109 APInt::tcShiftLeft(divisor, partsCount, bit);
1110 }
1111
1112 /* Normalize the dividend. */
1113 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
1114 if (bit) {
1115 exponent -= bit;
1116 APInt::tcShiftLeft(dividend, partsCount, bit);
1117 }
1118
1119 /* Ensure the dividend >= divisor initially for the loop below.
1120 Incidentally, this means that the division loop below is
1121 guaranteed to set the integer bit to one. */
1122 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
1123 exponent--;
1124 APInt::tcShiftLeft(dividend, partsCount, 1);
1125 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0)((APInt::tcCompare(dividend, divisor, partsCount) >= 0) ? static_cast
<void> (0) : __assert_fail ("APInt::tcCompare(dividend, divisor, partsCount) >= 0"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1125, __PRETTY_FUNCTION__))
;
1126 }
1127
1128 /* Long division. */
1129 for (bit = precision; bit; bit -= 1) {
1130 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
1131 APInt::tcSubtract(dividend, divisor, 0, partsCount);
1132 APInt::tcSetBit(lhsSignificand, bit - 1);
1133 }
1134
1135 APInt::tcShiftLeft(dividend, partsCount, 1);
1136 }
1137
1138 /* Figure out the lost fraction. */
1139 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1140
1141 if (cmp > 0)
1142 lost_fraction = lfMoreThanHalf;
1143 else if (cmp == 0)
1144 lost_fraction = lfExactlyHalf;
1145 else if (APInt::tcIsZero(dividend, partsCount))
1146 lost_fraction = lfExactlyZero;
1147 else
1148 lost_fraction = lfLessThanHalf;
1149
1150 if (partsCount > 2)
1151 delete [] dividend;
1152
1153 return lost_fraction;
1154}
1155
1156unsigned int IEEEFloat::significandMSB() const {
1157 return APInt::tcMSB(significandParts(), partCount());
1158}
1159
1160unsigned int IEEEFloat::significandLSB() const {
1161 return APInt::tcLSB(significandParts(), partCount());
1162}
1163
1164/* Note that a zero result is NOT normalized to fcZero. */
1165lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) {
1166 /* Our exponent should not overflow. */
1167 assert((ExponentType) (exponent + bits) >= exponent)(((ExponentType) (exponent + bits) >= exponent) ? static_cast
<void> (0) : __assert_fail ("(ExponentType) (exponent + bits) >= exponent"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1167, __PRETTY_FUNCTION__))
;
1168
1169 exponent += bits;
1170
1171 return shiftRight(significandParts(), partCount(), bits);
1172}
1173
1174/* Shift the significand left BITS bits, subtract BITS from its exponent. */
1175void IEEEFloat::shiftSignificandLeft(unsigned int bits) {
1176 assert(bits < semantics->precision)((bits < semantics->precision) ? static_cast<void>
(0) : __assert_fail ("bits < semantics->precision", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1176, __PRETTY_FUNCTION__))
;
1177
1178 if (bits) {
1179 unsigned int partsCount = partCount();
1180
1181 APInt::tcShiftLeft(significandParts(), partsCount, bits);
1182 exponent -= bits;
1183
1184 assert(!APInt::tcIsZero(significandParts(), partsCount))((!APInt::tcIsZero(significandParts(), partsCount)) ? static_cast
<void> (0) : __assert_fail ("!APInt::tcIsZero(significandParts(), partsCount)"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1184, __PRETTY_FUNCTION__))
;
1185 }
1186}
1187
1188IEEEFloat::cmpResult
1189IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const {
1190 int compare;
1191
1192 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1192, __PRETTY_FUNCTION__))
;
1193 assert(isFiniteNonZero())((isFiniteNonZero()) ? static_cast<void> (0) : __assert_fail
("isFiniteNonZero()", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1193, __PRETTY_FUNCTION__))
;
1194 assert(rhs.isFiniteNonZero())((rhs.isFiniteNonZero()) ? static_cast<void> (0) : __assert_fail
("rhs.isFiniteNonZero()", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1194, __PRETTY_FUNCTION__))
;
1195
1196 compare = exponent - rhs.exponent;
1197
1198 /* If exponents are equal, do an unsigned bignum comparison of the
1199 significands. */
1200 if (compare == 0)
1201 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
1202 partCount());
1203
1204 if (compare > 0)
1205 return cmpGreaterThan;
1206 else if (compare < 0)
1207 return cmpLessThan;
1208 else
1209 return cmpEqual;
1210}
1211
1212/* Handle overflow. Sign is preserved. We either become infinity or
1213 the largest finite number. */
1214IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) {
1215 /* Infinity? */
1216 if (rounding_mode == rmNearestTiesToEven ||
1217 rounding_mode == rmNearestTiesToAway ||
1218 (rounding_mode == rmTowardPositive && !sign) ||
1219 (rounding_mode == rmTowardNegative && sign)) {
1220 category = fcInfinity;
1221 return (opStatus) (opOverflow | opInexact);
1222 }
1223
1224 /* Otherwise we become the largest finite number. */
1225 category = fcNormal;
1226 exponent = semantics->maxExponent;
1227 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
1228 semantics->precision);
1229
1230 return opInexact;
1231}
1232
1233/* Returns TRUE if, when truncating the current number, with BIT the
1234 new LSB, with the given lost fraction and rounding mode, the result
1235 would need to be rounded away from zero (i.e., by increasing the
1236 signficand). This routine must work for fcZero of both signs, and
1237 fcNormal numbers. */
1238bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode,
1239 lostFraction lost_fraction,
1240 unsigned int bit) const {
1241 /* NaNs and infinities should not have lost fractions. */
1242 assert(isFiniteNonZero() || category == fcZero)((isFiniteNonZero() || category == fcZero) ? static_cast<void
> (0) : __assert_fail ("isFiniteNonZero() || category == fcZero"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1242, __PRETTY_FUNCTION__))
;
1243
1244 /* Current callers never pass this so we don't handle it. */
1245 assert(lost_fraction != lfExactlyZero)((lost_fraction != lfExactlyZero) ? static_cast<void> (
0) : __assert_fail ("lost_fraction != lfExactlyZero", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1245, __PRETTY_FUNCTION__))
;
1246
1247 switch (rounding_mode) {
1248 case rmNearestTiesToAway:
1249 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1250
1251 case rmNearestTiesToEven:
1252 if (lost_fraction == lfMoreThanHalf)
1253 return true;
1254
1255 /* Our zeroes don't have a significand to test. */
1256 if (lost_fraction == lfExactlyHalf && category != fcZero)
1257 return APInt::tcExtractBit(significandParts(), bit);
1258
1259 return false;
1260
1261 case rmTowardZero:
1262 return false;
1263
1264 case rmTowardPositive:
1265 return !sign;
1266
1267 case rmTowardNegative:
1268 return sign;
1269 }
1270 llvm_unreachable("Invalid rounding mode found")::llvm::llvm_unreachable_internal("Invalid rounding mode found"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1270)
;
1271}
1272
1273IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode,
1274 lostFraction lost_fraction) {
1275 unsigned int omsb; /* One, not zero, based MSB. */
1276 int exponentChange;
1277
1278 if (!isFiniteNonZero())
1279 return opOK;
1280
1281 /* Before rounding normalize the exponent of fcNormal numbers. */
1282 omsb = significandMSB() + 1;
1283
1284 if (omsb) {
1285 /* OMSB is numbered from 1. We want to place it in the integer
1286 bit numbered PRECISION if possible, with a compensating change in
1287 the exponent. */
1288 exponentChange = omsb - semantics->precision;
1289
1290 /* If the resulting exponent is too high, overflow according to
1291 the rounding mode. */
1292 if (exponent + exponentChange > semantics->maxExponent)
1293 return handleOverflow(rounding_mode);
1294
1295 /* Subnormal numbers have exponent minExponent, and their MSB
1296 is forced based on that. */
1297 if (exponent + exponentChange < semantics->minExponent)
1298 exponentChange = semantics->minExponent - exponent;
1299
1300 /* Shifting left is easy as we don't lose precision. */
1301 if (exponentChange < 0) {
1302 assert(lost_fraction == lfExactlyZero)((lost_fraction == lfExactlyZero) ? static_cast<void> (
0) : __assert_fail ("lost_fraction == lfExactlyZero", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1302, __PRETTY_FUNCTION__))
;
1303
1304 shiftSignificandLeft(-exponentChange);
1305
1306 return opOK;
1307 }
1308
1309 if (exponentChange > 0) {
1310 lostFraction lf;
1311
1312 /* Shift right and capture any new lost fraction. */
1313 lf = shiftSignificandRight(exponentChange);
1314
1315 lost_fraction = combineLostFractions(lf, lost_fraction);
1316
1317 /* Keep OMSB up-to-date. */
1318 if (omsb > (unsigned) exponentChange)
1319 omsb -= exponentChange;
1320 else
1321 omsb = 0;
1322 }
1323 }
1324
1325 /* Now round the number according to rounding_mode given the lost
1326 fraction. */
1327
1328 /* As specified in IEEE 754, since we do not trap we do not report
1329 underflow for exact results. */
1330 if (lost_fraction == lfExactlyZero) {
1331 /* Canonicalize zeroes. */
1332 if (omsb == 0)
1333 category = fcZero;
1334
1335 return opOK;
1336 }
1337
1338 /* Increment the significand if we're rounding away from zero. */
1339 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1340 if (omsb == 0)
1341 exponent = semantics->minExponent;
1342
1343 incrementSignificand();
1344 omsb = significandMSB() + 1;
1345
1346 /* Did the significand increment overflow? */
1347 if (omsb == (unsigned) semantics->precision + 1) {
1348 /* Renormalize by incrementing the exponent and shifting our
1349 significand right one. However if we already have the
1350 maximum exponent we overflow to infinity. */
1351 if (exponent == semantics->maxExponent) {
1352 category = fcInfinity;
1353
1354 return (opStatus) (opOverflow | opInexact);
1355 }
1356
1357 shiftSignificandRight(1);
1358
1359 return opInexact;
1360 }
1361 }
1362
1363 /* The normal case - we were and are not denormal, and any
1364 significand increment above didn't overflow. */
1365 if (omsb == semantics->precision)
1366 return opInexact;
1367
1368 /* We have a non-zero denormal. */
1369 assert(omsb < semantics->precision)((omsb < semantics->precision) ? static_cast<void>
(0) : __assert_fail ("omsb < semantics->precision", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1369, __PRETTY_FUNCTION__))
;
1370
1371 /* Canonicalize zeroes. */
1372 if (omsb == 0)
1373 category = fcZero;
1374
1375 /* The fcZero case is a denormal that underflowed to zero. */
1376 return (opStatus) (opUnderflow | opInexact);
1377}
1378
1379IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs,
1380 bool subtract) {
1381 switch (PackCategoriesIntoKey(category, rhs.category)((category) * 4 + (rhs.category))) {
1382 default:
1383 llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1383)
;
1384
1385 case PackCategoriesIntoKey(fcNaN, fcZero)((fcNaN) * 4 + (fcZero)):
1386 case PackCategoriesIntoKey(fcNaN, fcNormal)((fcNaN) * 4 + (fcNormal)):
1387 case PackCategoriesIntoKey(fcNaN, fcInfinity)((fcNaN) * 4 + (fcInfinity)):
1388 case PackCategoriesIntoKey(fcNaN, fcNaN)((fcNaN) * 4 + (fcNaN)):
1389 case PackCategoriesIntoKey(fcNormal, fcZero)((fcNormal) * 4 + (fcZero)):
1390 case PackCategoriesIntoKey(fcInfinity, fcNormal)((fcInfinity) * 4 + (fcNormal)):
1391 case PackCategoriesIntoKey(fcInfinity, fcZero)((fcInfinity) * 4 + (fcZero)):
1392 return opOK;
1393
1394 case PackCategoriesIntoKey(fcZero, fcNaN)((fcZero) * 4 + (fcNaN)):
1395 case PackCategoriesIntoKey(fcNormal, fcNaN)((fcNormal) * 4 + (fcNaN)):
1396 case PackCategoriesIntoKey(fcInfinity, fcNaN)((fcInfinity) * 4 + (fcNaN)):
1397 // We need to be sure to flip the sign here for subtraction because we
1398 // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1399 sign = rhs.sign ^ subtract;
1400 category = fcNaN;
1401 copySignificand(rhs);
1402 return opOK;
1403
1404 case PackCategoriesIntoKey(fcNormal, fcInfinity)((fcNormal) * 4 + (fcInfinity)):
1405 case PackCategoriesIntoKey(fcZero, fcInfinity)((fcZero) * 4 + (fcInfinity)):
1406 category = fcInfinity;
1407 sign = rhs.sign ^ subtract;
1408 return opOK;
1409
1410 case PackCategoriesIntoKey(fcZero, fcNormal)((fcZero) * 4 + (fcNormal)):
1411 assign(rhs);
1412 sign = rhs.sign ^ subtract;
1413 return opOK;
1414
1415 case PackCategoriesIntoKey(fcZero, fcZero)((fcZero) * 4 + (fcZero)):
1416 /* Sign depends on rounding mode; handled by caller. */
1417 return opOK;
1418
1419 case PackCategoriesIntoKey(fcInfinity, fcInfinity)((fcInfinity) * 4 + (fcInfinity)):
1420 /* Differently signed infinities can only be validly
1421 subtracted. */
1422 if (((sign ^ rhs.sign)!=0) != subtract) {
1423 makeNaN();
1424 return opInvalidOp;
1425 }
1426
1427 return opOK;
1428
1429 case PackCategoriesIntoKey(fcNormal, fcNormal)((fcNormal) * 4 + (fcNormal)):
1430 return opDivByZero;
1431 }
1432}
1433
1434/* Add or subtract two normal numbers. */
1435lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs,
1436 bool subtract) {
1437 integerPart carry;
1438 lostFraction lost_fraction;
1439 int bits;
1440
1441 /* Determine if the operation on the absolute values is effectively
1442 an addition or subtraction. */
1443 subtract ^= static_cast<bool>(sign ^ rhs.sign);
1444
1445 /* Are we bigger exponent-wise than the RHS? */
1446 bits = exponent - rhs.exponent;
1447
1448 /* Subtraction is more subtle than one might naively expect. */
1449 if (subtract) {
1450 IEEEFloat temp_rhs(rhs);
1451 bool reverse;
1452
1453 if (bits == 0) {
1454 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1455 lost_fraction = lfExactlyZero;
1456 } else if (bits > 0) {
1457 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1458 shiftSignificandLeft(1);
1459 reverse = false;
1460 } else {
1461 lost_fraction = shiftSignificandRight(-bits - 1);
1462 temp_rhs.shiftSignificandLeft(1);
1463 reverse = true;
1464 }
1465
1466 if (reverse) {
1467 carry = temp_rhs.subtractSignificand
1468 (*this, lost_fraction != lfExactlyZero);
1469 copySignificand(temp_rhs);
1470 sign = !sign;
1471 } else {
1472 carry = subtractSignificand
1473 (temp_rhs, lost_fraction != lfExactlyZero);
1474 }
1475
1476 /* Invert the lost fraction - it was on the RHS and
1477 subtracted. */
1478 if (lost_fraction == lfLessThanHalf)
1479 lost_fraction = lfMoreThanHalf;
1480 else if (lost_fraction == lfMoreThanHalf)
1481 lost_fraction = lfLessThanHalf;
1482
1483 /* The code above is intended to ensure that no borrow is
1484 necessary. */
1485 assert(!carry)((!carry) ? static_cast<void> (0) : __assert_fail ("!carry"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1485, __PRETTY_FUNCTION__))
;
1486 (void)carry;
1487 } else {
1488 if (bits > 0) {
1489 IEEEFloat temp_rhs(rhs);
1490
1491 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1492 carry = addSignificand(temp_rhs);
1493 } else {
1494 lost_fraction = shiftSignificandRight(-bits);
1495 carry = addSignificand(rhs);
1496 }
1497
1498 /* We have a guard bit; generating a carry cannot happen. */
1499 assert(!carry)((!carry) ? static_cast<void> (0) : __assert_fail ("!carry"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1499, __PRETTY_FUNCTION__))
;
1500 (void)carry;
1501 }
1502
1503 return lost_fraction;
1504}
1505
1506IEEEFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) {
1507 switch (PackCategoriesIntoKey(category, rhs.category)((category) * 4 + (rhs.category))) {
1508 default:
1509 llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1509)
;
1510
1511 case PackCategoriesIntoKey(fcNaN, fcZero)((fcNaN) * 4 + (fcZero)):
1512 case PackCategoriesIntoKey(fcNaN, fcNormal)((fcNaN) * 4 + (fcNormal)):
1513 case PackCategoriesIntoKey(fcNaN, fcInfinity)((fcNaN) * 4 + (fcInfinity)):
1514 case PackCategoriesIntoKey(fcNaN, fcNaN)((fcNaN) * 4 + (fcNaN)):
1515 sign = false;
1516 return opOK;
1517
1518 case PackCategoriesIntoKey(fcZero, fcNaN)((fcZero) * 4 + (fcNaN)):
1519 case PackCategoriesIntoKey(fcNormal, fcNaN)((fcNormal) * 4 + (fcNaN)):
1520 case PackCategoriesIntoKey(fcInfinity, fcNaN)((fcInfinity) * 4 + (fcNaN)):
1521 sign = false;
1522 category = fcNaN;
1523 copySignificand(rhs);
1524 return opOK;
1525
1526 case PackCategoriesIntoKey(fcNormal, fcInfinity)((fcNormal) * 4 + (fcInfinity)):
1527 case PackCategoriesIntoKey(fcInfinity, fcNormal)((fcInfinity) * 4 + (fcNormal)):
1528 case PackCategoriesIntoKey(fcInfinity, fcInfinity)((fcInfinity) * 4 + (fcInfinity)):
1529 category = fcInfinity;
1530 return opOK;
1531
1532 case PackCategoriesIntoKey(fcZero, fcNormal)((fcZero) * 4 + (fcNormal)):
1533 case PackCategoriesIntoKey(fcNormal, fcZero)((fcNormal) * 4 + (fcZero)):
1534 case PackCategoriesIntoKey(fcZero, fcZero)((fcZero) * 4 + (fcZero)):
1535 category = fcZero;
1536 return opOK;
1537
1538 case PackCategoriesIntoKey(fcZero, fcInfinity)((fcZero) * 4 + (fcInfinity)):
1539 case PackCategoriesIntoKey(fcInfinity, fcZero)((fcInfinity) * 4 + (fcZero)):
1540 makeNaN();
1541 return opInvalidOp;
1542
1543 case PackCategoriesIntoKey(fcNormal, fcNormal)((fcNormal) * 4 + (fcNormal)):
1544 return opOK;
1545 }
1546}
1547
1548IEEEFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) {
1549 switch (PackCategoriesIntoKey(category, rhs.category)((category) * 4 + (rhs.category))) {
1550 default:
1551 llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1551)
;
1552
1553 case PackCategoriesIntoKey(fcZero, fcNaN)((fcZero) * 4 + (fcNaN)):
1554 case PackCategoriesIntoKey(fcNormal, fcNaN)((fcNormal) * 4 + (fcNaN)):
1555 case PackCategoriesIntoKey(fcInfinity, fcNaN)((fcInfinity) * 4 + (fcNaN)):
1556 category = fcNaN;
1557 copySignificand(rhs);
1558 LLVM_FALLTHROUGH[[clang::fallthrough]];
1559 case PackCategoriesIntoKey(fcNaN, fcZero)((fcNaN) * 4 + (fcZero)):
1560 case PackCategoriesIntoKey(fcNaN, fcNormal)((fcNaN) * 4 + (fcNormal)):
1561 case PackCategoriesIntoKey(fcNaN, fcInfinity)((fcNaN) * 4 + (fcInfinity)):
1562 case PackCategoriesIntoKey(fcNaN, fcNaN)((fcNaN) * 4 + (fcNaN)):
1563 sign = false;
1564 LLVM_FALLTHROUGH[[clang::fallthrough]];
1565 case PackCategoriesIntoKey(fcInfinity, fcZero)((fcInfinity) * 4 + (fcZero)):
1566 case PackCategoriesIntoKey(fcInfinity, fcNormal)((fcInfinity) * 4 + (fcNormal)):
1567 case PackCategoriesIntoKey(fcZero, fcInfinity)((fcZero) * 4 + (fcInfinity)):
1568 case PackCategoriesIntoKey(fcZero, fcNormal)((fcZero) * 4 + (fcNormal)):
1569 return opOK;
1570
1571 case PackCategoriesIntoKey(fcNormal, fcInfinity)((fcNormal) * 4 + (fcInfinity)):
1572 category = fcZero;
1573 return opOK;
1574
1575 case PackCategoriesIntoKey(fcNormal, fcZero)((fcNormal) * 4 + (fcZero)):
1576 category = fcInfinity;
1577 return opDivByZero;
1578
1579 case PackCategoriesIntoKey(fcInfinity, fcInfinity)((fcInfinity) * 4 + (fcInfinity)):
1580 case PackCategoriesIntoKey(fcZero, fcZero)((fcZero) * 4 + (fcZero)):
1581 makeNaN();
1582 return opInvalidOp;
1583
1584 case PackCategoriesIntoKey(fcNormal, fcNormal)((fcNormal) * 4 + (fcNormal)):
1585 return opOK;
1586 }
1587}
1588
1589IEEEFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) {
1590 switch (PackCategoriesIntoKey(category, rhs.category)((category) * 4 + (rhs.category))) {
1591 default:
1592 llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1592)
;
1593
1594 case PackCategoriesIntoKey(fcNaN, fcZero)((fcNaN) * 4 + (fcZero)):
1595 case PackCategoriesIntoKey(fcNaN, fcNormal)((fcNaN) * 4 + (fcNormal)):
1596 case PackCategoriesIntoKey(fcNaN, fcInfinity)((fcNaN) * 4 + (fcInfinity)):
1597 case PackCategoriesIntoKey(fcNaN, fcNaN)((fcNaN) * 4 + (fcNaN)):
1598 case PackCategoriesIntoKey(fcZero, fcInfinity)((fcZero) * 4 + (fcInfinity)):
1599 case PackCategoriesIntoKey(fcZero, fcNormal)((fcZero) * 4 + (fcNormal)):
1600 case PackCategoriesIntoKey(fcNormal, fcInfinity)((fcNormal) * 4 + (fcInfinity)):
1601 return opOK;
1602
1603 case PackCategoriesIntoKey(fcZero, fcNaN)((fcZero) * 4 + (fcNaN)):
1604 case PackCategoriesIntoKey(fcNormal, fcNaN)((fcNormal) * 4 + (fcNaN)):
1605 case PackCategoriesIntoKey(fcInfinity, fcNaN)((fcInfinity) * 4 + (fcNaN)):
1606 sign = false;
1607 category = fcNaN;
1608 copySignificand(rhs);
1609 return opOK;
1610
1611 case PackCategoriesIntoKey(fcNormal, fcZero)((fcNormal) * 4 + (fcZero)):
1612 case PackCategoriesIntoKey(fcInfinity, fcZero)((fcInfinity) * 4 + (fcZero)):
1613 case PackCategoriesIntoKey(fcInfinity, fcNormal)((fcInfinity) * 4 + (fcNormal)):
1614 case PackCategoriesIntoKey(fcInfinity, fcInfinity)((fcInfinity) * 4 + (fcInfinity)):
1615 case PackCategoriesIntoKey(fcZero, fcZero)((fcZero) * 4 + (fcZero)):
1616 makeNaN();
1617 return opInvalidOp;
1618
1619 case PackCategoriesIntoKey(fcNormal, fcNormal)((fcNormal) * 4 + (fcNormal)):
1620 return opOK;
1621 }
1622}
1623
1624/* Change sign. */
1625void IEEEFloat::changeSign() {
1626 /* Look mummy, this one's easy. */
1627 sign = !sign;
1628}
1629
1630/* Normalized addition or subtraction. */
1631IEEEFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs,
1632 roundingMode rounding_mode,
1633 bool subtract) {
1634 opStatus fs;
1635
1636 fs = addOrSubtractSpecials(rhs, subtract);
1637
1638 /* This return code means it was not a simple case. */
1639 if (fs == opDivByZero) {
1640 lostFraction lost_fraction;
1641
1642 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1643 fs = normalize(rounding_mode, lost_fraction);
1644
1645 /* Can only be zero if we lost no fraction. */
1646 assert(category != fcZero || lost_fraction == lfExactlyZero)((category != fcZero || lost_fraction == lfExactlyZero) ? static_cast
<void> (0) : __assert_fail ("category != fcZero || lost_fraction == lfExactlyZero"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1646, __PRETTY_FUNCTION__))
;
1647 }
1648
1649 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1650 positive zero unless rounding to minus infinity, except that
1651 adding two like-signed zeroes gives that zero. */
1652 if (category == fcZero) {
1653 if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
1654 sign = (rounding_mode == rmTowardNegative);
1655 }
1656
1657 return fs;
1658}
1659
1660/* Normalized addition. */
1661IEEEFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs,
1662 roundingMode rounding_mode) {
1663 return addOrSubtract(rhs, rounding_mode, false);
1664}
1665
1666/* Normalized subtraction. */
1667IEEEFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs,
1668 roundingMode rounding_mode) {
1669 return addOrSubtract(rhs, rounding_mode, true);
1670}
1671
1672/* Normalized multiply. */
1673IEEEFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs,
1674 roundingMode rounding_mode) {
1675 opStatus fs;
1676
1677 sign ^= rhs.sign;
1678 fs = multiplySpecials(rhs);
1679
1680 if (isFiniteNonZero()) {
1681 lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
1682 fs = normalize(rounding_mode, lost_fraction);
1683 if (lost_fraction != lfExactlyZero)
1684 fs = (opStatus) (fs | opInexact);
1685 }
1686
1687 return fs;
1688}
1689
1690/* Normalized divide. */
1691IEEEFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs,
1692 roundingMode rounding_mode) {
1693 opStatus fs;
1694
1695 sign ^= rhs.sign;
1696 fs = divideSpecials(rhs);
1697
1698 if (isFiniteNonZero()) {
1699 lostFraction lost_fraction = divideSignificand(rhs);
1700 fs = normalize(rounding_mode, lost_fraction);
1701 if (lost_fraction != lfExactlyZero)
1702 fs = (opStatus) (fs | opInexact);
1703 }
1704
1705 return fs;
1706}
1707
1708/* Normalized remainder. This is not currently correct in all cases. */
1709IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) {
1710 opStatus fs;
1711 IEEEFloat V = *this;
1712 unsigned int origSign = sign;
1713
1714 fs = V.divide(rhs, rmNearestTiesToEven);
1715 if (fs == opDivByZero)
1716 return fs;
1717
1718 int parts = partCount();
1719 integerPart *x = new integerPart[parts];
1720 bool ignored;
1721 fs = V.convertToInteger(makeMutableArrayRef(x, parts),
1722 parts * integerPartWidth, true, rmNearestTiesToEven,
1723 &ignored);
1724 if (fs == opInvalidOp) {
1725 delete[] x;
1726 return fs;
1727 }
1728
1729 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1730 rmNearestTiesToEven);
1731 assert(fs==opOK)((fs==opOK) ? static_cast<void> (0) : __assert_fail ("fs==opOK"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1731, __PRETTY_FUNCTION__))
; // should always work
1732
1733 fs = V.multiply(rhs, rmNearestTiesToEven);
1734 assert(fs==opOK || fs==opInexact)((fs==opOK || fs==opInexact) ? static_cast<void> (0) : __assert_fail
("fs==opOK || fs==opInexact", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1734, __PRETTY_FUNCTION__))
; // should not overflow or underflow
1735
1736 fs = subtract(V, rmNearestTiesToEven);
1737 assert(fs==opOK || fs==opInexact)((fs==opOK || fs==opInexact) ? static_cast<void> (0) : __assert_fail
("fs==opOK || fs==opInexact", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1737, __PRETTY_FUNCTION__))
; // likewise
1738
1739 if (isZero())
1740 sign = origSign; // IEEE754 requires this
1741 delete[] x;
1742 return fs;
1743}
1744
1745/* Normalized llvm frem (C fmod). */
1746IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) {
1747 opStatus fs;
1748 fs = modSpecials(rhs);
1749 unsigned int origSign = sign;
1750
1751 while (isFiniteNonZero() && rhs.isFiniteNonZero() &&
1752 compareAbsoluteValue(rhs) != cmpLessThan) {
1753 IEEEFloat V = scalbn(rhs, ilogb(*this) - ilogb(rhs), rmNearestTiesToEven);
1754 if (compareAbsoluteValue(V) == cmpLessThan)
1755 V = scalbn(V, -1, rmNearestTiesToEven);
1756 V.sign = sign;
1757
1758 fs = subtract(V, rmNearestTiesToEven);
1759 assert(fs==opOK)((fs==opOK) ? static_cast<void> (0) : __assert_fail ("fs==opOK"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1759, __PRETTY_FUNCTION__))
;
1760 }
1761 if (isZero())
1762 sign = origSign; // fmod requires this
1763 return fs;
1764}
1765
1766/* Normalized fused-multiply-add. */
1767IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand,
1768 const IEEEFloat &addend,
1769 roundingMode rounding_mode) {
1770 opStatus fs;
1771
1772 /* Post-multiplication sign, before addition. */
1773 sign ^= multiplicand.sign;
1774
1775 /* If and only if all arguments are normal do we need to do an
1776 extended-precision calculation. */
1777 if (isFiniteNonZero() &&
1778 multiplicand.isFiniteNonZero() &&
1779 addend.isFinite()) {
1780 lostFraction lost_fraction;
1781
1782 lost_fraction = multiplySignificand(multiplicand, &addend);
1783 fs = normalize(rounding_mode, lost_fraction);
1784 if (lost_fraction != lfExactlyZero)
1785 fs = (opStatus) (fs | opInexact);
1786
1787 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1788 positive zero unless rounding to minus infinity, except that
1789 adding two like-signed zeroes gives that zero. */
1790 if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
1791 sign = (rounding_mode == rmTowardNegative);
1792 } else {
1793 fs = multiplySpecials(multiplicand);
1794
1795 /* FS can only be opOK or opInvalidOp. There is no more work
1796 to do in the latter case. The IEEE-754R standard says it is
1797 implementation-defined in this case whether, if ADDEND is a
1798 quiet NaN, we raise invalid op; this implementation does so.
1799
1800 If we need to do the addition we can do so with normal
1801 precision. */
1802 if (fs == opOK)
1803 fs = addOrSubtract(addend, rounding_mode, false);
1804 }
1805
1806 return fs;
1807}
1808
1809/* Rounding-mode corrrect round to integral value. */
1810IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) {
1811 opStatus fs;
1812
1813 // If the exponent is large enough, we know that this value is already
1814 // integral, and the arithmetic below would potentially cause it to saturate
1815 // to +/-Inf. Bail out early instead.
1816 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
1817 return opOK;
1818
1819 // The algorithm here is quite simple: we add 2^(p-1), where p is the
1820 // precision of our format, and then subtract it back off again. The choice
1821 // of rounding modes for the addition/subtraction determines the rounding mode
1822 // for our integral rounding as well.
1823 // NOTE: When the input value is negative, we do subtraction followed by
1824 // addition instead.
1825 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1826 IntegerConstant <<= semanticsPrecision(*semantics)-1;
1827 IEEEFloat MagicConstant(*semantics);
1828 fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1829 rmNearestTiesToEven);
1830 MagicConstant.sign = sign;
1831
1832 if (fs != opOK)
1833 return fs;
1834
1835 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1836 bool inputSign = isNegative();
1837
1838 fs = add(MagicConstant, rounding_mode);
1839 if (fs != opOK && fs != opInexact)
1840 return fs;
1841
1842 fs = subtract(MagicConstant, rounding_mode);
1843
1844 // Restore the input sign.
1845 if (inputSign != isNegative())
1846 changeSign();
1847
1848 return fs;
1849}
1850
1851
1852/* Comparison requires normalized numbers. */
1853IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const {
1854 cmpResult result;
1855
1856 assert(semantics == rhs.semantics)((semantics == rhs.semantics) ? static_cast<void> (0) :
__assert_fail ("semantics == rhs.semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1856, __PRETTY_FUNCTION__))
;
1857
1858 switch (PackCategoriesIntoKey(category, rhs.category)((category) * 4 + (rhs.category))) {
1859 default:
1860 llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 1860)
;
1861
1862 case PackCategoriesIntoKey(fcNaN, fcZero)((fcNaN) * 4 + (fcZero)):
1863 case PackCategoriesIntoKey(fcNaN, fcNormal)((fcNaN) * 4 + (fcNormal)):
1864 case PackCategoriesIntoKey(fcNaN, fcInfinity)((fcNaN) * 4 + (fcInfinity)):
1865 case PackCategoriesIntoKey(fcNaN, fcNaN)((fcNaN) * 4 + (fcNaN)):
1866 case PackCategoriesIntoKey(fcZero, fcNaN)((fcZero) * 4 + (fcNaN)):
1867 case PackCategoriesIntoKey(fcNormal, fcNaN)((fcNormal) * 4 + (fcNaN)):
1868 case PackCategoriesIntoKey(fcInfinity, fcNaN)((fcInfinity) * 4 + (fcNaN)):
1869 return cmpUnordered;
1870
1871 case PackCategoriesIntoKey(fcInfinity, fcNormal)((fcInfinity) * 4 + (fcNormal)):
1872 case PackCategoriesIntoKey(fcInfinity, fcZero)((fcInfinity) * 4 + (fcZero)):
1873 case PackCategoriesIntoKey(fcNormal, fcZero)((fcNormal) * 4 + (fcZero)):
1874 if (sign)
1875 return cmpLessThan;
1876 else
1877 return cmpGreaterThan;
1878
1879 case PackCategoriesIntoKey(fcNormal, fcInfinity)((fcNormal) * 4 + (fcInfinity)):
1880 case PackCategoriesIntoKey(fcZero, fcInfinity)((fcZero) * 4 + (fcInfinity)):
1881 case PackCategoriesIntoKey(fcZero, fcNormal)((fcZero) * 4 + (fcNormal)):
1882 if (rhs.sign)
1883 return cmpGreaterThan;
1884 else
1885 return cmpLessThan;
1886
1887 case PackCategoriesIntoKey(fcInfinity, fcInfinity)((fcInfinity) * 4 + (fcInfinity)):
1888 if (sign == rhs.sign)
1889 return cmpEqual;
1890 else if (sign)
1891 return cmpLessThan;
1892 else
1893 return cmpGreaterThan;
1894
1895 case PackCategoriesIntoKey(fcZero, fcZero)((fcZero) * 4 + (fcZero)):
1896 return cmpEqual;
1897
1898 case PackCategoriesIntoKey(fcNormal, fcNormal)((fcNormal) * 4 + (fcNormal)):
1899 break;
1900 }
1901
1902 /* Two normal numbers. Do they have the same sign? */
1903 if (sign != rhs.sign) {
1904 if (sign)
1905 result = cmpLessThan;
1906 else
1907 result = cmpGreaterThan;
1908 } else {
1909 /* Compare absolute values; invert result if negative. */
1910 result = compareAbsoluteValue(rhs);
1911
1912 if (sign) {
1913 if (result == cmpLessThan)
1914 result = cmpGreaterThan;
1915 else if (result == cmpGreaterThan)
1916 result = cmpLessThan;
1917 }
1918 }
1919
1920 return result;
1921}
1922
1923/// IEEEFloat::convert - convert a value of one floating point type to another.
1924/// The return value corresponds to the IEEE754 exceptions. *losesInfo
1925/// records whether the transformation lost information, i.e. whether
1926/// converting the result back to the original type will produce the
1927/// original value (this is almost the same as return value==fsOK, but there
1928/// are edge cases where this is not so).
1929
1930IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics,
1931 roundingMode rounding_mode,
1932 bool *losesInfo) {
1933 lostFraction lostFraction;
1934 unsigned int newPartCount, oldPartCount;
1935 opStatus fs;
1936 int shift;
1937 const fltSemantics &fromSemantics = *semantics;
1938
1939 lostFraction = lfExactlyZero;
1940 newPartCount = partCountForBits(toSemantics.precision + 1);
1941 oldPartCount = partCount();
1942 shift = toSemantics.precision - fromSemantics.precision;
1943
1944 bool X86SpecialNan = false;
1945 if (&fromSemantics == &semX87DoubleExtended &&
1946 &toSemantics != &semX87DoubleExtended && category == fcNaN &&
1947 (!(*significandParts() & 0x8000000000000000ULL) ||
1948 !(*significandParts() & 0x4000000000000000ULL))) {
1949 // x86 has some unusual NaNs which cannot be represented in any other
1950 // format; note them here.
1951 X86SpecialNan = true;
1952 }
1953
1954 // If this is a truncation of a denormal number, and the target semantics
1955 // has larger exponent range than the source semantics (this can happen
1956 // when truncating from PowerPC double-double to double format), the
1957 // right shift could lose result mantissa bits. Adjust exponent instead
1958 // of performing excessive shift.
1959 if (shift < 0 && isFiniteNonZero()) {
1960 int exponentChange = significandMSB() + 1 - fromSemantics.precision;
1961 if (exponent + exponentChange < toSemantics.minExponent)
1962 exponentChange = toSemantics.minExponent - exponent;
1963 if (exponentChange < shift)
1964 exponentChange = shift;
1965 if (exponentChange < 0) {
1966 shift -= exponentChange;
1967 exponent += exponentChange;
1968 }
1969 }
1970
1971 // If this is a truncation, perform the shift before we narrow the storage.
1972 if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
1973 lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
1974
1975 // Fix the storage so it can hold to new value.
1976 if (newPartCount > oldPartCount) {
1977 // The new type requires more storage; make it available.
1978 integerPart *newParts;
1979 newParts = new integerPart[newPartCount];
1980 APInt::tcSet(newParts, 0, newPartCount);
1981 if (isFiniteNonZero() || category==fcNaN)
1982 APInt::tcAssign(newParts, significandParts(), oldPartCount);
1983 freeSignificand();
1984 significand.parts = newParts;
1985 } else if (newPartCount == 1 && oldPartCount != 1) {
1986 // Switch to built-in storage for a single part.
1987 integerPart newPart = 0;
1988 if (isFiniteNonZero() || category==fcNaN)
1989 newPart = significandParts()[0];
1990 freeSignificand();
1991 significand.part = newPart;
1992 }
1993
1994 // Now that we have the right storage, switch the semantics.
1995 semantics = &toSemantics;
1996
1997 // If this is an extension, perform the shift now that the storage is
1998 // available.
1999 if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
2000 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2001
2002 if (isFiniteNonZero()) {
2003 fs = normalize(rounding_mode, lostFraction);
2004 *losesInfo = (fs != opOK);
2005 } else if (category == fcNaN) {
2006 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
2007
2008 // For x87 extended precision, we want to make a NaN, not a special NaN if
2009 // the input wasn't special either.
2010 if (!X86SpecialNan && semantics == &semX87DoubleExtended)
2011 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2012
2013 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2014 // does not give you back the same bits. This is dubious, and we
2015 // don't currently do it. You're really supposed to get
2016 // an invalid operation signal at runtime, but nobody does that.
2017 fs = opOK;
2018 } else {
2019 *losesInfo = false;
2020 fs = opOK;
2021 }
2022
2023 return fs;
2024}
2025
2026/* Convert a floating point number to an integer according to the
2027 rounding mode. If the rounded integer value is out of range this
2028 returns an invalid operation exception and the contents of the
2029 destination parts are unspecified. If the rounded value is in
2030 range but the floating point number is not the exact integer, the C
2031 standard doesn't require an inexact exception to be raised. IEEE
2032 854 does require it so we do that.
2033
2034 Note that for conversions to integer type the C standard requires
2035 round-to-zero to always be used. */
2036IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger(
2037 MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned,
2038 roundingMode rounding_mode, bool *isExact) const {
2039 lostFraction lost_fraction;
2040 const integerPart *src;
2041 unsigned int dstPartsCount, truncatedBits;
2042
2043 *isExact = false;
2044
2045 /* Handle the three special cases first. */
2046 if (category == fcInfinity || category == fcNaN)
2047 return opInvalidOp;
2048
2049 dstPartsCount = partCountForBits(width);
2050 assert(dstPartsCount <= parts.size() && "Integer too big")((dstPartsCount <= parts.size() && "Integer too big"
) ? static_cast<void> (0) : __assert_fail ("dstPartsCount <= parts.size() && \"Integer too big\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2050, __PRETTY_FUNCTION__))
;
2051
2052 if (category == fcZero) {
2053 APInt::tcSet(parts.data(), 0, dstPartsCount);
2054 // Negative zero can't be represented as an int.
2055 *isExact = !sign;
2056 return opOK;
2057 }
2058
2059 src = significandParts();
2060
2061 /* Step 1: place our absolute value, with any fraction truncated, in
2062 the destination. */
2063 if (exponent < 0) {
2064 /* Our absolute value is less than one; truncate everything. */
2065 APInt::tcSet(parts.data(), 0, dstPartsCount);
2066 /* For exponent -1 the integer bit represents .5, look at that.
2067 For smaller exponents leftmost truncated bit is 0. */
2068 truncatedBits = semantics->precision -1U - exponent;
2069 } else {
2070 /* We want the most significant (exponent + 1) bits; the rest are
2071 truncated. */
2072 unsigned int bits = exponent + 1U;
2073
2074 /* Hopelessly large in magnitude? */
2075 if (bits > width)
2076 return opInvalidOp;
2077
2078 if (bits < semantics->precision) {
2079 /* We truncate (semantics->precision - bits) bits. */
2080 truncatedBits = semantics->precision - bits;
2081 APInt::tcExtract(parts.data(), dstPartsCount, src, bits, truncatedBits);
2082 } else {
2083 /* We want at least as many bits as are available. */
2084 APInt::tcExtract(parts.data(), dstPartsCount, src, semantics->precision,
2085 0);
2086 APInt::tcShiftLeft(parts.data(), dstPartsCount,
2087 bits - semantics->precision);
2088 truncatedBits = 0;
2089 }
2090 }
2091
2092 /* Step 2: work out any lost fraction, and increment the absolute
2093 value if we would round away from zero. */
2094 if (truncatedBits) {
2095 lost_fraction = lostFractionThroughTruncation(src, partCount(),
2096 truncatedBits);
2097 if (lost_fraction != lfExactlyZero &&
2098 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2099 if (APInt::tcIncrement(parts.data(), dstPartsCount))
2100 return opInvalidOp; /* Overflow. */
2101 }
2102 } else {
2103 lost_fraction = lfExactlyZero;
2104 }
2105
2106 /* Step 3: check if we fit in the destination. */
2107 unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1;
2108
2109 if (sign) {
2110 if (!isSigned) {
2111 /* Negative numbers cannot be represented as unsigned. */
2112 if (omsb != 0)
2113 return opInvalidOp;
2114 } else {
2115 /* It takes omsb bits to represent the unsigned integer value.
2116 We lose a bit for the sign, but care is needed as the
2117 maximally negative integer is a special case. */
2118 if (omsb == width &&
2119 APInt::tcLSB(parts.data(), dstPartsCount) + 1 != omsb)
2120 return opInvalidOp;
2121
2122 /* This case can happen because of rounding. */
2123 if (omsb > width)
2124 return opInvalidOp;
2125 }
2126
2127 APInt::tcNegate (parts.data(), dstPartsCount);
2128 } else {
2129 if (omsb >= width + !isSigned)
2130 return opInvalidOp;
2131 }
2132
2133 if (lost_fraction == lfExactlyZero) {
2134 *isExact = true;
2135 return opOK;
2136 } else
2137 return opInexact;
2138}
2139
2140/* Same as convertToSignExtendedInteger, except we provide
2141 deterministic values in case of an invalid operation exception,
2142 namely zero for NaNs and the minimal or maximal value respectively
2143 for underflow or overflow.
2144 The *isExact output tells whether the result is exact, in the sense
2145 that converting it back to the original floating point type produces
2146 the original value. This is almost equivalent to result==opOK,
2147 except for negative zeroes.
2148*/
2149IEEEFloat::opStatus
2150IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts,
2151 unsigned int width, bool isSigned,
2152 roundingMode rounding_mode, bool *isExact) const {
2153 opStatus fs;
2154
2155 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2156 isExact);
2157
2158 if (fs == opInvalidOp) {
2159 unsigned int bits, dstPartsCount;
2160
2161 dstPartsCount = partCountForBits(width);
2162 assert(dstPartsCount <= parts.size() && "Integer too big")((dstPartsCount <= parts.size() && "Integer too big"
) ? static_cast<void> (0) : __assert_fail ("dstPartsCount <= parts.size() && \"Integer too big\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2162, __PRETTY_FUNCTION__))
;
2163
2164 if (category == fcNaN)
2165 bits = 0;
2166 else if (sign)
2167 bits = isSigned;
2168 else
2169 bits = width - isSigned;
2170
2171 APInt::tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits);
2172 if (sign && isSigned)
2173 APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1);
2174 }
2175
2176 return fs;
2177}
2178
2179/* Convert an unsigned integer SRC to a floating point number,
2180 rounding according to ROUNDING_MODE. The sign of the floating
2181 point number is not modified. */
2182IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts(
2183 const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) {
2184 unsigned int omsb, precision, dstCount;
2185 integerPart *dst;
2186 lostFraction lost_fraction;
2187
2188 category = fcNormal;
2189 omsb = APInt::tcMSB(src, srcCount) + 1;
2190 dst = significandParts();
2191 dstCount = partCount();
2192 precision = semantics->precision;
2193
2194 /* We want the most significant PRECISION bits of SRC. There may not
2195 be that many; extract what we can. */
2196 if (precision <= omsb) {
2197 exponent = omsb - 1;
2198 lost_fraction = lostFractionThroughTruncation(src, srcCount,
2199 omsb - precision);
2200 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2201 } else {
2202 exponent = precision - 1;
2203 lost_fraction = lfExactlyZero;
2204 APInt::tcExtract(dst, dstCount, src, omsb, 0);
2205 }
2206
2207 return normalize(rounding_mode, lost_fraction);
2208}
2209
2210IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned,
2211 roundingMode rounding_mode) {
2212 unsigned int partCount = Val.getNumWords();
2213 APInt api = Val;
2214
2215 sign = false;
2216 if (isSigned && api.isNegative()) {
2217 sign = true;
2218 api = -api;
2219 }
2220
2221 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2222}
2223
2224/* Convert a two's complement integer SRC to a floating point number,
2225 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2226 integer is signed, in which case it must be sign-extended. */
2227IEEEFloat::opStatus
2228IEEEFloat::convertFromSignExtendedInteger(const integerPart *src,
2229 unsigned int srcCount, bool isSigned,
2230 roundingMode rounding_mode) {
2231 opStatus status;
2232
2233 if (isSigned &&
2234 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2235 integerPart *copy;
2236
2237 /* If we're signed and negative negate a copy. */
2238 sign = true;
2239 copy = new integerPart[srcCount];
2240 APInt::tcAssign(copy, src, srcCount);
2241 APInt::tcNegate(copy, srcCount);
2242 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2243 delete [] copy;
2244 } else {
2245 sign = false;
2246 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2247 }
2248
2249 return status;
2250}
2251
2252/* FIXME: should this just take a const APInt reference? */
2253IEEEFloat::opStatus
2254IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2255 unsigned int width, bool isSigned,
2256 roundingMode rounding_mode) {
2257 unsigned int partCount = partCountForBits(width);
2258 APInt api = APInt(width, makeArrayRef(parts, partCount));
2259
2260 sign = false;
2261 if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
2262 sign = true;
2263 api = -api;
2264 }
2265
2266 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2267}
2268
2269IEEEFloat::opStatus
2270IEEEFloat::convertFromHexadecimalString(StringRef s,
2271 roundingMode rounding_mode) {
2272 lostFraction lost_fraction = lfExactlyZero;
2273
2274 category = fcNormal;
2275 zeroSignificand();
2276 exponent = 0;
2277
2278 integerPart *significand = significandParts();
2279 unsigned partsCount = partCount();
2280 unsigned bitPos = partsCount * integerPartWidth;
2281 bool computedTrailingFraction = false;
2282
2283 // Skip leading zeroes and any (hexa)decimal point.
2284 StringRef::iterator begin = s.begin();
2285 StringRef::iterator end = s.end();
2286 StringRef::iterator dot;
2287 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
2288 StringRef::iterator firstSignificantDigit = p;
2289
2290 while (p != end) {
2291 integerPart hex_value;
2292
2293 if (*p == '.') {
2294 assert(dot == end && "String contains multiple dots")((dot == end && "String contains multiple dots") ? static_cast
<void> (0) : __assert_fail ("dot == end && \"String contains multiple dots\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2294, __PRETTY_FUNCTION__))
;
2295 dot = p++;
2296 continue;
2297 }
2298
2299 hex_value = hexDigitValue(*p);
2300 if (hex_value == -1U)
2301 break;
2302
2303 p++;
2304
2305 // Store the number while we have space.
2306 if (bitPos) {
2307 bitPos -= 4;
2308 hex_value <<= bitPos % integerPartWidth;
2309 significand[bitPos / integerPartWidth] |= hex_value;
2310 } else if (!computedTrailingFraction) {
2311 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2312 computedTrailingFraction = true;
2313 }
2314 }
2315
2316 /* Hex floats require an exponent but not a hexadecimal point. */
2317 assert(p != end && "Hex strings require an exponent")((p != end && "Hex strings require an exponent") ? static_cast
<void> (0) : __assert_fail ("p != end && \"Hex strings require an exponent\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2317, __PRETTY_FUNCTION__))
;
2318 assert((*p == 'p' || *p == 'P') && "Invalid character in significand")(((*p == 'p' || *p == 'P') && "Invalid character in significand"
) ? static_cast<void> (0) : __assert_fail ("(*p == 'p' || *p == 'P') && \"Invalid character in significand\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2318, __PRETTY_FUNCTION__))
;
2319 assert(p != begin && "Significand has no digits")((p != begin && "Significand has no digits") ? static_cast
<void> (0) : __assert_fail ("p != begin && \"Significand has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2319, __PRETTY_FUNCTION__))
;
2320 assert((dot == end || p - begin != 1) && "Significand has no digits")(((dot == end || p - begin != 1) && "Significand has no digits"
) ? static_cast<void> (0) : __assert_fail ("(dot == end || p - begin != 1) && \"Significand has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2320, __PRETTY_FUNCTION__))
;
2321
2322 /* Ignore the exponent if we are zero. */
2323 if (p != firstSignificantDigit) {
2324 int expAdjustment;
2325
2326 /* Implicit hexadecimal point? */
2327 if (dot == end)
2328 dot = p;
2329
2330 /* Calculate the exponent adjustment implicit in the number of
2331 significant digits. */
2332 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
2333 if (expAdjustment < 0)
2334 expAdjustment++;
2335 expAdjustment = expAdjustment * 4 - 1;
2336
2337 /* Adjust for writing the significand starting at the most
2338 significant nibble. */
2339 expAdjustment += semantics->precision;
2340 expAdjustment -= partsCount * integerPartWidth;
2341
2342 /* Adjust for the given exponent. */
2343 exponent = totalExponent(p + 1, end, expAdjustment);
2344 }
2345
2346 return normalize(rounding_mode, lost_fraction);
2347}
2348
2349IEEEFloat::opStatus
2350IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2351 unsigned sigPartCount, int exp,
2352 roundingMode rounding_mode) {
2353 unsigned int parts, pow5PartCount;
2354 fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
2355 integerPart pow5Parts[maxPowerOfFiveParts];
2356 bool isNearest;
2357
2358 isNearest = (rounding_mode == rmNearestTiesToEven ||
2359 rounding_mode == rmNearestTiesToAway);
2360
2361 parts = partCountForBits(semantics->precision + 11);
2362
2363 /* Calculate pow(5, abs(exp)). */
2364 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2365
2366 for (;; parts *= 2) {
2367 opStatus sigStatus, powStatus;
2368 unsigned int excessPrecision, truncatedBits;
2369
2370 calcSemantics.precision = parts * integerPartWidth - 1;
2371 excessPrecision = calcSemantics.precision - semantics->precision;
2372 truncatedBits = excessPrecision;
2373
2374 IEEEFloat decSig(calcSemantics, uninitialized);
2375 decSig.makeZero(sign);
2376 IEEEFloat pow5(calcSemantics);
2377
2378 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2379 rmNearestTiesToEven);
2380 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2381 rmNearestTiesToEven);
2382 /* Add exp, as 10^n = 5^n * 2^n. */
2383 decSig.exponent += exp;
2384
2385 lostFraction calcLostFraction;
2386 integerPart HUerr, HUdistance;
2387 unsigned int powHUerr;
2388
2389 if (exp >= 0) {
2390 /* multiplySignificand leaves the precision-th bit set to 1. */
2391 calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
2392 powHUerr = powStatus != opOK;
2393 } else {
2394 calcLostFraction = decSig.divideSignificand(pow5);
2395 /* Denormal numbers have less precision. */
2396 if (decSig.exponent < semantics->minExponent) {
2397 excessPrecision += (semantics->minExponent - decSig.exponent);
2398 truncatedBits = excessPrecision;
2399 if (excessPrecision > calcSemantics.precision)
2400 excessPrecision = calcSemantics.precision;
2401 }
2402 /* Extra half-ulp lost in reciprocal of exponent. */
2403 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
2404 }
2405
2406 /* Both multiplySignificand and divideSignificand return the
2407 result with the integer bit set. */
2408 assert(APInt::tcExtractBit((APInt::tcExtractBit (decSig.significandParts(), calcSemantics
.precision - 1) == 1) ? static_cast<void> (0) : __assert_fail
("APInt::tcExtractBit (decSig.significandParts(), calcSemantics.precision - 1) == 1"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2409, __PRETTY_FUNCTION__))
2409 (decSig.significandParts(), calcSemantics.precision - 1) == 1)((APInt::tcExtractBit (decSig.significandParts(), calcSemantics
.precision - 1) == 1) ? static_cast<void> (0) : __assert_fail
("APInt::tcExtractBit (decSig.significandParts(), calcSemantics.precision - 1) == 1"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2409, __PRETTY_FUNCTION__))
;
2410
2411 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2412 powHUerr);
2413 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2414 excessPrecision, isNearest);
2415
2416 /* Are we guaranteed to round correctly if we truncate? */
2417 if (HUdistance >= HUerr) {
2418 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2419 calcSemantics.precision - excessPrecision,
2420 excessPrecision);
2421 /* Take the exponent of decSig. If we tcExtract-ed less bits
2422 above we must adjust our exponent to compensate for the
2423 implicit right shift. */
2424 exponent = (decSig.exponent + semantics->precision
2425 - (calcSemantics.precision - excessPrecision));
2426 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2427 decSig.partCount(),
2428 truncatedBits);
2429 return normalize(rounding_mode, calcLostFraction);
2430 }
2431 }
2432}
2433
2434IEEEFloat::opStatus
2435IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) {
2436 decimalInfo D;
2437 opStatus fs;
2438
2439 /* Scan the text. */
2440 StringRef::iterator p = str.begin();
2441 interpretDecimal(p, str.end(), &D);
2442
2443 /* Handle the quick cases. First the case of no significant digits,
2444 i.e. zero, and then exponents that are obviously too large or too
2445 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2446 definitely overflows if
2447
2448 (exp - 1) * L >= maxExponent
2449
2450 and definitely underflows to zero where
2451
2452 (exp + 1) * L <= minExponent - precision
2453
2454 With integer arithmetic the tightest bounds for L are
2455
2456 93/28 < L < 196/59 [ numerator <= 256 ]
2457 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2458 */
2459
2460 // Test if we have a zero number allowing for strings with no null terminators
2461 // and zero decimals with non-zero exponents.
2462 //
2463 // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2464 // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2465 // be at most one dot. On the other hand, if we have a zero with a non-zero
2466 // exponent, then we know that D.firstSigDigit will be non-numeric.
2467 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
2468 category = fcZero;
2469 fs = opOK;
2470
2471 /* Check whether the normalized exponent is high enough to overflow
2472 max during the log-rebasing in the max-exponent check below. */
2473 } else if (D.normalizedExponent - 1 > INT_MAX2147483647 / 42039) {
2474 fs = handleOverflow(rounding_mode);
2475
2476 /* If it wasn't, then it also wasn't high enough to overflow max
2477 during the log-rebasing in the min-exponent check. Check that it
2478 won't overflow min in either check, then perform the min-exponent
2479 check. */
2480 } else if (D.normalizedExponent - 1 < INT_MIN(-2147483647 -1) / 42039 ||
2481 (D.normalizedExponent + 1) * 28738 <=
2482 8651 * (semantics->minExponent - (int) semantics->precision)) {
2483 /* Underflow to zero and round. */
2484 category = fcNormal;
2485 zeroSignificand();
2486 fs = normalize(rounding_mode, lfLessThanHalf);
2487
2488 /* We can finally safely perform the max-exponent check. */
2489 } else if ((D.normalizedExponent - 1) * 42039
2490 >= 12655 * semantics->maxExponent) {
2491 /* Overflow and round. */
2492 fs = handleOverflow(rounding_mode);
2493 } else {
2494 integerPart *decSignificand;
2495 unsigned int partCount;
2496
2497 /* A tight upper bound on number of bits required to hold an
2498 N-digit decimal integer is N * 196 / 59. Allocate enough space
2499 to hold the full significand, and an extra part required by
2500 tcMultiplyPart. */
2501 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
2502 partCount = partCountForBits(1 + 196 * partCount / 59);
2503 decSignificand = new integerPart[partCount + 1];
2504 partCount = 0;
2505
2506 /* Convert to binary efficiently - we do almost all multiplication
2507 in an integerPart. When this would overflow do we do a single
2508 bignum multiplication, and then revert again to multiplication
2509 in an integerPart. */
2510 do {
2511 integerPart decValue, val, multiplier;
2512
2513 val = 0;
2514 multiplier = 1;
2515
2516 do {
2517 if (*p == '.') {
2518 p++;
2519 if (p == str.end()) {
2520 break;
2521 }
2522 }
2523 decValue = decDigitValue(*p++);
2524 assert(decValue < 10U && "Invalid character in significand")((decValue < 10U && "Invalid character in significand"
) ? static_cast<void> (0) : __assert_fail ("decValue < 10U && \"Invalid character in significand\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2524, __PRETTY_FUNCTION__))
;
2525 multiplier *= 10;
2526 val = val * 10 + decValue;
2527 /* The maximum number that can be multiplied by ten with any
2528 digit added without overflowing an integerPart. */
2529 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2530
2531 /* Multiply out the current part. */
2532 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2533 partCount, partCount + 1, false);
2534
2535 /* If we used another part (likely but not guaranteed), increase
2536 the count. */
2537 if (decSignificand[partCount])
2538 partCount++;
2539 } while (p <= D.lastSigDigit);
2540
2541 category = fcNormal;
2542 fs = roundSignificandWithExponent(decSignificand, partCount,
2543 D.exponent, rounding_mode);
2544
2545 delete [] decSignificand;
2546 }
2547
2548 return fs;
2549}
2550
2551bool IEEEFloat::convertFromStringSpecials(StringRef str) {
2552 if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) {
2553 makeInf(false);
2554 return true;
2555 }
2556
2557 if (str.equals("-inf") || str.equals("-INFINITY") || str.equals("-Inf")) {
2558 makeInf(true);
2559 return true;
2560 }
2561
2562 if (str.equals("nan") || str.equals("NaN")) {
2563 makeNaN(false, false);
2564 return true;
2565 }
2566
2567 if (str.equals("-nan") || str.equals("-NaN")) {
2568 makeNaN(false, true);
2569 return true;
2570 }
2571
2572 return false;
2573}
2574
2575IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str,
2576 roundingMode rounding_mode) {
2577 assert(!str.empty() && "Invalid string length")((!str.empty() && "Invalid string length") ? static_cast
<void> (0) : __assert_fail ("!str.empty() && \"Invalid string length\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2577, __PRETTY_FUNCTION__))
;
2578
2579 // Handle special cases.
2580 if (convertFromStringSpecials(str))
2581 return opOK;
2582
2583 /* Handle a leading minus sign. */
2584 StringRef::iterator p = str.begin();
2585 size_t slen = str.size();
2586 sign = *p == '-' ? 1 : 0;
2587 if (*p == '-' || *p == '+') {
2588 p++;
2589 slen--;
2590 assert(slen && "String has no digits")((slen && "String has no digits") ? static_cast<void
> (0) : __assert_fail ("slen && \"String has no digits\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2590, __PRETTY_FUNCTION__))
;
2591 }
2592
2593 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2594 assert(slen - 2 && "Invalid string")((slen - 2 && "Invalid string") ? static_cast<void
> (0) : __assert_fail ("slen - 2 && \"Invalid string\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2594, __PRETTY_FUNCTION__))
;
2595 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
2596 rounding_mode);
2597 }
2598
2599 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
2600}
2601
2602/* Write out a hexadecimal representation of the floating point value
2603 to DST, which must be of sufficient size, in the C99 form
2604 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2605 excluding the terminating NUL.
2606
2607 If UPPERCASE, the output is in upper case, otherwise in lower case.
2608
2609 HEXDIGITS digits appear altogether, rounding the value if
2610 necessary. If HEXDIGITS is 0, the minimal precision to display the
2611 number precisely is used instead. If nothing would appear after
2612 the decimal point it is suppressed.
2613
2614 The decimal exponent is always printed and has at least one digit.
2615 Zero values display an exponent of zero. Infinities and NaNs
2616 appear as "infinity" or "nan" respectively.
2617
2618 The above rules are as specified by C99. There is ambiguity about
2619 what the leading hexadecimal digit should be. This implementation
2620 uses whatever is necessary so that the exponent is displayed as
2621 stored. This implies the exponent will fall within the IEEE format
2622 range, and the leading hexadecimal digit will be 0 (for denormals),
2623 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2624 any other digits zero).
2625*/
2626unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits,
2627 bool upperCase,
2628 roundingMode rounding_mode) const {
2629 char *p;
2630
2631 p = dst;
2632 if (sign)
2633 *dst++ = '-';
2634
2635 switch (category) {
2636 case fcInfinity:
2637 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2638 dst += sizeof infinityL - 1;
2639 break;
2640
2641 case fcNaN:
2642 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2643 dst += sizeof NaNU - 1;
2644 break;
2645
2646 case fcZero:
2647 *dst++ = '0';
2648 *dst++ = upperCase ? 'X': 'x';
2649 *dst++ = '0';
2650 if (hexDigits > 1) {
2651 *dst++ = '.';
2652 memset (dst, '0', hexDigits - 1);
2653 dst += hexDigits - 1;
2654 }
2655 *dst++ = upperCase ? 'P': 'p';
2656 *dst++ = '0';
2657 break;
2658
2659 case fcNormal:
2660 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2661 break;
2662 }
2663
2664 *dst = 0;
2665
2666 return static_cast<unsigned int>(dst - p);
2667}
2668
2669/* Does the hard work of outputting the correctly rounded hexadecimal
2670 form of a normal floating point number with the specified number of
2671 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2672 digits necessary to print the value precisely is output. */
2673char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2674 bool upperCase,
2675 roundingMode rounding_mode) const {
2676 unsigned int count, valueBits, shift, partsCount, outputDigits;
2677 const char *hexDigitChars;
2678 const integerPart *significand;
2679 char *p;
2680 bool roundUp;
2681
2682 *dst++ = '0';
2683 *dst++ = upperCase ? 'X': 'x';
2684
2685 roundUp = false;
2686 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2687
2688 significand = significandParts();
2689 partsCount = partCount();
2690
2691 /* +3 because the first digit only uses the single integer bit, so
2692 we have 3 virtual zero most-significant-bits. */
2693 valueBits = semantics->precision + 3;
2694 shift = integerPartWidth - valueBits % integerPartWidth;
2695
2696 /* The natural number of digits required ignoring trailing
2697 insignificant zeroes. */
2698 outputDigits = (valueBits - significandLSB () + 3) / 4;
2699
2700 /* hexDigits of zero means use the required number for the
2701 precision. Otherwise, see if we are truncating. If we are,
2702 find out if we need to round away from zero. */
2703 if (hexDigits) {
2704 if (hexDigits < outputDigits) {
2705 /* We are dropping non-zero bits, so need to check how to round.
2706 "bits" is the number of dropped bits. */
2707 unsigned int bits;
2708 lostFraction fraction;
2709
2710 bits = valueBits - hexDigits * 4;
2711 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2712 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2713 }
2714 outputDigits = hexDigits;
2715 }
2716
2717 /* Write the digits consecutively, and start writing in the location
2718 of the hexadecimal point. We move the most significant digit
2719 left and add the hexadecimal point later. */
2720 p = ++dst;
2721
2722 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2723
2724 while (outputDigits && count) {
2725 integerPart part;
2726
2727 /* Put the most significant integerPartWidth bits in "part". */
2728 if (--count == partsCount)
2729 part = 0; /* An imaginary higher zero part. */
2730 else
2731 part = significand[count] << shift;
2732
2733 if (count && shift)
2734 part |= significand[count - 1] >> (integerPartWidth - shift);
2735
2736 /* Convert as much of "part" to hexdigits as we can. */
2737 unsigned int curDigits = integerPartWidth / 4;
2738
2739 if (curDigits > outputDigits)
2740 curDigits = outputDigits;
2741 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2742 outputDigits -= curDigits;
2743 }
2744
2745 if (roundUp) {
2746 char *q = dst;
2747
2748 /* Note that hexDigitChars has a trailing '0'. */
2749 do {
2750 q--;
2751 *q = hexDigitChars[hexDigitValue (*q) + 1];
2752 } while (*q == '0');
2753 assert(q >= p)((q >= p) ? static_cast<void> (0) : __assert_fail ("q >= p"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2753, __PRETTY_FUNCTION__))
;
2754 } else {
2755 /* Add trailing zeroes. */
2756 memset (dst, '0', outputDigits);
2757 dst += outputDigits;
2758 }
2759
2760 /* Move the most significant digit to before the point, and if there
2761 is something after the decimal point add it. This must come
2762 after rounding above. */
2763 p[-1] = p[0];
2764 if (dst -1 == p)
2765 dst--;
2766 else
2767 p[0] = '.';
2768
2769 /* Finally output the exponent. */
2770 *dst++ = upperCase ? 'P': 'p';
2771
2772 return writeSignedDecimal (dst, exponent);
2773}
2774
2775hash_code hash_value(const IEEEFloat &Arg) {
2776 if (!Arg.isFiniteNonZero())
2777 return hash_combine((uint8_t)Arg.category,
2778 // NaN has no sign, fix it at zero.
2779 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2780 Arg.semantics->precision);
2781
2782 // Normal floats need their exponent and significand hashed.
2783 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2784 Arg.semantics->precision, Arg.exponent,
2785 hash_combine_range(
2786 Arg.significandParts(),
2787 Arg.significandParts() + Arg.partCount()));
2788}
2789
2790// Conversion from APFloat to/from host float/double. It may eventually be
2791// possible to eliminate these and have everybody deal with APFloats, but that
2792// will take a while. This approach will not easily extend to long double.
2793// Current implementation requires integerPartWidth==64, which is correct at
2794// the moment but could be made more general.
2795
2796// Denormals have exponent minExponent in APFloat, but minExponent-1 in
2797// the actual IEEE respresentations. We compensate for that here.
2798
2799APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const {
2800 assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended)((semantics == (const llvm::fltSemantics*)&semX87DoubleExtended
) ? static_cast<void> (0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semX87DoubleExtended"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2800, __PRETTY_FUNCTION__))
;
2801 assert(partCount()==2)((partCount()==2) ? static_cast<void> (0) : __assert_fail
("partCount()==2", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2801, __PRETTY_FUNCTION__))
;
2802
2803 uint64_t myexponent, mysignificand;
2804
2805 if (isFiniteNonZero()) {
2806 myexponent = exponent+16383; //bias
2807 mysignificand = significandParts()[0];
2808 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2809 myexponent = 0; // denormal
2810 } else if (category==fcZero) {
2811 myexponent = 0;
2812 mysignificand = 0;
2813 } else if (category==fcInfinity) {
2814 myexponent = 0x7fff;
2815 mysignificand = 0x8000000000000000ULL;
2816 } else {
2817 assert(category == fcNaN && "Unknown category")((category == fcNaN && "Unknown category") ? static_cast
<void> (0) : __assert_fail ("category == fcNaN && \"Unknown category\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2817, __PRETTY_FUNCTION__))
;
2818 myexponent = 0x7fff;
2819 mysignificand = significandParts()[0];
2820 }
2821
2822 uint64_t words[2];
2823 words[0] = mysignificand;
2824 words[1] = ((uint64_t)(sign & 1) << 15) |
2825 (myexponent & 0x7fffLL);
2826 return APInt(80, words);
2827}
2828
2829APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const {
2830 assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy)((semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy
) ? static_cast<void> (0) : __assert_fail ("semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2830, __PRETTY_FUNCTION__))
;
2831 assert(partCount()==2)((partCount()==2) ? static_cast<void> (0) : __assert_fail
("partCount()==2", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2831, __PRETTY_FUNCTION__))
;
2832
2833 uint64_t words[2];
2834 opStatus fs;
2835 bool losesInfo;
2836
2837 // Convert number to double. To avoid spurious underflows, we re-
2838 // normalize against the "double" minExponent first, and only *then*
2839 // truncate the mantissa. The result of that second conversion
2840 // may be inexact, but should never underflow.
2841 // Declare fltSemantics before APFloat that uses it (and
2842 // saves pointer to it) to ensure correct destruction order.
2843 fltSemantics extendedSemantics = *semantics;
2844 extendedSemantics.minExponent = semIEEEdouble.minExponent;
2845 IEEEFloat extended(*this);
2846 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2847 assert(fs == opOK && !losesInfo)((fs == opOK && !losesInfo) ? static_cast<void>
(0) : __assert_fail ("fs == opOK && !losesInfo", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2847, __PRETTY_FUNCTION__))
;
2848 (void)fs;
2849
2850 IEEEFloat u(extended);
2851 fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo);
2852 assert(fs == opOK || fs == opInexact)((fs == opOK || fs == opInexact) ? static_cast<void> (0
) : __assert_fail ("fs == opOK || fs == opInexact", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2852, __PRETTY_FUNCTION__))
;
2853 (void)fs;
2854 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2855
2856 // If conversion was exact or resulted in a special case, we're done;
2857 // just set the second double to zero. Otherwise, re-convert back to
2858 // the extended format and compute the difference. This now should
2859 // convert exactly to double.
2860 if (u.isFiniteNonZero() && losesInfo) {
2861 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2862 assert(fs == opOK && !losesInfo)((fs == opOK && !losesInfo) ? static_cast<void>
(0) : __assert_fail ("fs == opOK && !losesInfo", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2862, __PRETTY_FUNCTION__))
;
2863 (void)fs;
2864
2865 IEEEFloat v(extended);
2866 v.subtract(u, rmNearestTiesToEven);
2867 fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo);
2868 assert(fs == opOK && !losesInfo)((fs == opOK && !losesInfo) ? static_cast<void>
(0) : __assert_fail ("fs == opOK && !losesInfo", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2868, __PRETTY_FUNCTION__))
;
2869 (void)fs;
2870 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
2871 } else {
2872 words[1] = 0;
2873 }
2874
2875 return APInt(128, words);
2876}
2877
2878APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const {
2879 assert(semantics == (const llvm::fltSemantics*)&semIEEEquad)((semantics == (const llvm::fltSemantics*)&semIEEEquad) ?
static_cast<void> (0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEquad"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2879, __PRETTY_FUNCTION__))
;
2880 assert(partCount()==2)((partCount()==2) ? static_cast<void> (0) : __assert_fail
("partCount()==2", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2880, __PRETTY_FUNCTION__))
;
2881
2882 uint64_t myexponent, mysignificand, mysignificand2;
2883
2884 if (isFiniteNonZero()) {
2885 myexponent = exponent+16383; //bias
2886 mysignificand = significandParts()[0];
2887 mysignificand2 = significandParts()[1];
2888 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2889 myexponent = 0; // denormal
2890 } else if (category==fcZero) {
2891 myexponent = 0;
2892 mysignificand = mysignificand2 = 0;
2893 } else if (category==fcInfinity) {
2894 myexponent = 0x7fff;
2895 mysignificand = mysignificand2 = 0;
2896 } else {
2897 assert(category == fcNaN && "Unknown category!")((category == fcNaN && "Unknown category!") ? static_cast
<void> (0) : __assert_fail ("category == fcNaN && \"Unknown category!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2897, __PRETTY_FUNCTION__))
;
2898 myexponent = 0x7fff;
2899 mysignificand = significandParts()[0];
2900 mysignificand2 = significandParts()[1];
2901 }
2902
2903 uint64_t words[2];
2904 words[0] = mysignificand;
2905 words[1] = ((uint64_t)(sign & 1) << 63) |
2906 ((myexponent & 0x7fff) << 48) |
2907 (mysignificand2 & 0xffffffffffffLL);
2908
2909 return APInt(128, words);
2910}
2911
2912APInt IEEEFloat::convertDoubleAPFloatToAPInt() const {
2913 assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble)((semantics == (const llvm::fltSemantics*)&semIEEEdouble)
? static_cast<void> (0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2913, __PRETTY_FUNCTION__))
;
2914 assert(partCount()==1)((partCount()==1) ? static_cast<void> (0) : __assert_fail
("partCount()==1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2914, __PRETTY_FUNCTION__))
;
2915
2916 uint64_t myexponent, mysignificand;
2917
2918 if (isFiniteNonZero()) {
2919 myexponent = exponent+1023; //bias
2920 mysignificand = *significandParts();
2921 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2922 myexponent = 0; // denormal
2923 } else if (category==fcZero) {
2924 myexponent = 0;
2925 mysignificand = 0;
2926 } else if (category==fcInfinity) {
2927 myexponent = 0x7ff;
2928 mysignificand = 0;
2929 } else {
2930 assert(category == fcNaN && "Unknown category!")((category == fcNaN && "Unknown category!") ? static_cast
<void> (0) : __assert_fail ("category == fcNaN && \"Unknown category!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2930, __PRETTY_FUNCTION__))
;
2931 myexponent = 0x7ff;
2932 mysignificand = *significandParts();
2933 }
2934
2935 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
2936 ((myexponent & 0x7ff) << 52) |
2937 (mysignificand & 0xfffffffffffffLL))));
2938}
2939
2940APInt IEEEFloat::convertFloatAPFloatToAPInt() const {
2941 assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle)((semantics == (const llvm::fltSemantics*)&semIEEEsingle)
? static_cast<void> (0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEsingle"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2941, __PRETTY_FUNCTION__))
;
2942 assert(partCount()==1)((partCount()==1) ? static_cast<void> (0) : __assert_fail
("partCount()==1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2942, __PRETTY_FUNCTION__))
;
2943
2944 uint32_t myexponent, mysignificand;
2945
2946 if (isFiniteNonZero()) {
2947 myexponent = exponent+127; //bias
2948 mysignificand = (uint32_t)*significandParts();
2949 if (myexponent == 1 && !(mysignificand & 0x800000))
2950 myexponent = 0; // denormal
2951 } else if (category==fcZero) {
2952 myexponent = 0;
2953 mysignificand = 0;
2954 } else if (category==fcInfinity) {
2955 myexponent = 0xff;
2956 mysignificand = 0;
2957 } else {
2958 assert(category == fcNaN && "Unknown category!")((category == fcNaN && "Unknown category!") ? static_cast
<void> (0) : __assert_fail ("category == fcNaN && \"Unknown category!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2958, __PRETTY_FUNCTION__))
;
2959 myexponent = 0xff;
2960 mysignificand = (uint32_t)*significandParts();
2961 }
2962
2963 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2964 (mysignificand & 0x7fffff)));
2965}
2966
2967APInt IEEEFloat::convertHalfAPFloatToAPInt() const {
2968 assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf)((semantics == (const llvm::fltSemantics*)&semIEEEhalf) ?
static_cast<void> (0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEhalf"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2968, __PRETTY_FUNCTION__))
;
2969 assert(partCount()==1)((partCount()==1) ? static_cast<void> (0) : __assert_fail
("partCount()==1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2969, __PRETTY_FUNCTION__))
;
2970
2971 uint32_t myexponent, mysignificand;
2972
2973 if (isFiniteNonZero()) {
2974 myexponent = exponent+15; //bias
2975 mysignificand = (uint32_t)*significandParts();
2976 if (myexponent == 1 && !(mysignificand & 0x400))
2977 myexponent = 0; // denormal
2978 } else if (category==fcZero) {
2979 myexponent = 0;
2980 mysignificand = 0;
2981 } else if (category==fcInfinity) {
2982 myexponent = 0x1f;
2983 mysignificand = 0;
2984 } else {
2985 assert(category == fcNaN && "Unknown category!")((category == fcNaN && "Unknown category!") ? static_cast
<void> (0) : __assert_fail ("category == fcNaN && \"Unknown category!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 2985, __PRETTY_FUNCTION__))
;
2986 myexponent = 0x1f;
2987 mysignificand = (uint32_t)*significandParts();
2988 }
2989
2990 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
2991 (mysignificand & 0x3ff)));
2992}
2993
2994// This function creates an APInt that is just a bit map of the floating
2995// point constant as it would appear in memory. It is not a conversion,
2996// and treating the result as a normal integer is unlikely to be useful.
2997
2998APInt IEEEFloat::bitcastToAPInt() const {
2999 if (semantics == (const llvm::fltSemantics*)&semIEEEhalf)
3000 return convertHalfAPFloatToAPInt();
3001
3002 if (semantics == (const llvm::fltSemantics*)&semIEEEsingle)
3003 return convertFloatAPFloatToAPInt();
3004
3005 if (semantics == (const llvm::fltSemantics*)&semIEEEdouble)
3006 return convertDoubleAPFloatToAPInt();
3007
3008 if (semantics == (const llvm::fltSemantics*)&semIEEEquad)
3009 return convertQuadrupleAPFloatToAPInt();
3010
3011 if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy)
3012 return convertPPCDoubleDoubleAPFloatToAPInt();
3013
3014 assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended &&((semantics == (const llvm::fltSemantics*)&semX87DoubleExtended
&& "unknown format!") ? static_cast<void> (0) :
__assert_fail ("semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && \"unknown format!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3015, __PRETTY_FUNCTION__))
3015 "unknown format!")((semantics == (const llvm::fltSemantics*)&semX87DoubleExtended
&& "unknown format!") ? static_cast<void> (0) :
__assert_fail ("semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && \"unknown format!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3015, __PRETTY_FUNCTION__))
;
3016 return convertF80LongDoubleAPFloatToAPInt();
3017}
3018
3019float IEEEFloat::convertToFloat() const {
3020 assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle &&((semantics == (const llvm::fltSemantics*)&semIEEEsingle &&
"Float semantics are not IEEEsingle") ? static_cast<void>
(0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEsingle && \"Float semantics are not IEEEsingle\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3021, __PRETTY_FUNCTION__))
3021 "Float semantics are not IEEEsingle")((semantics == (const llvm::fltSemantics*)&semIEEEsingle &&
"Float semantics are not IEEEsingle") ? static_cast<void>
(0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEsingle && \"Float semantics are not IEEEsingle\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3021, __PRETTY_FUNCTION__))
;
3022 APInt api = bitcastToAPInt();
3023 return api.bitsToFloat();
3024}
3025
3026double IEEEFloat::convertToDouble() const {
3027 assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble &&((semantics == (const llvm::fltSemantics*)&semIEEEdouble &&
"Float semantics are not IEEEdouble") ? static_cast<void>
(0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEdouble && \"Float semantics are not IEEEdouble\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3028, __PRETTY_FUNCTION__))
3028 "Float semantics are not IEEEdouble")((semantics == (const llvm::fltSemantics*)&semIEEEdouble &&
"Float semantics are not IEEEdouble") ? static_cast<void>
(0) : __assert_fail ("semantics == (const llvm::fltSemantics*)&semIEEEdouble && \"Float semantics are not IEEEdouble\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3028, __PRETTY_FUNCTION__))
;
3029 APInt api = bitcastToAPInt();
3030 return api.bitsToDouble();
3031}
3032
3033/// Integer bit is explicit in this format. Intel hardware (387 and later)
3034/// does not support these bit patterns:
3035/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3036/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3037/// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3038/// exponent = 0, integer bit 1 ("pseudodenormal")
3039/// At the moment, the first three are treated as NaNs, the last one as Normal.
3040void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) {
3041 assert(api.getBitWidth()==80)((api.getBitWidth()==80) ? static_cast<void> (0) : __assert_fail
("api.getBitWidth()==80", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3041, __PRETTY_FUNCTION__))
;
3042 uint64_t i1 = api.getRawData()[0];
3043 uint64_t i2 = api.getRawData()[1];
3044 uint64_t myexponent = (i2 & 0x7fff);
3045 uint64_t mysignificand = i1;
3046 uint8_t myintegerbit = mysignificand >> 63;
3047
3048 initialize(&semX87DoubleExtended);
3049 assert(partCount()==2)((partCount()==2) ? static_cast<void> (0) : __assert_fail
("partCount()==2", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3049, __PRETTY_FUNCTION__))
;
3050
3051 sign = static_cast<unsigned int>(i2>>15);
3052 if (myexponent == 0 && mysignificand == 0) {
3053 // exponent, significand meaningless
3054 category = fcZero;
3055 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3056 // exponent, significand meaningless
3057 category = fcInfinity;
3058 } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3059 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3060 // exponent meaningless
3061 category = fcNaN;
3062 significandParts()[0] = mysignificand;
3063 significandParts()[1] = 0;
3064 } else {
3065 category = fcNormal;
3066 exponent = myexponent - 16383;
3067 significandParts()[0] = mysignificand;
3068 significandParts()[1] = 0;
3069 if (myexponent==0) // denormal
3070 exponent = -16382;
3071 }
3072}
3073
3074void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) {
3075 assert(api.getBitWidth()==128)((api.getBitWidth()==128) ? static_cast<void> (0) : __assert_fail
("api.getBitWidth()==128", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3075, __PRETTY_FUNCTION__))
;
3076 uint64_t i1 = api.getRawData()[0];
3077 uint64_t i2 = api.getRawData()[1];
3078 opStatus fs;
3079 bool losesInfo;
3080
3081 // Get the first double and convert to our format.
3082 initFromDoubleAPInt(APInt(64, i1));
3083 fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo);
3084 assert(fs == opOK && !losesInfo)((fs == opOK && !losesInfo) ? static_cast<void>
(0) : __assert_fail ("fs == opOK && !losesInfo", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3084, __PRETTY_FUNCTION__))
;
3085 (void)fs;
3086
3087 // Unless we have a special case, add in second double.
3088 if (isFiniteNonZero()) {
3089 IEEEFloat v(semIEEEdouble, APInt(64, i2));
3090 fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo);
3091 assert(fs == opOK && !losesInfo)((fs == opOK && !losesInfo) ? static_cast<void>
(0) : __assert_fail ("fs == opOK && !losesInfo", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3091, __PRETTY_FUNCTION__))
;
3092 (void)fs;
3093
3094 add(v, rmNearestTiesToEven);
3095 }
3096}
3097
3098void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) {
3099 assert(api.getBitWidth()==128)((api.getBitWidth()==128) ? static_cast<void> (0) : __assert_fail
("api.getBitWidth()==128", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3099, __PRETTY_FUNCTION__))
;
3100 uint64_t i1 = api.getRawData()[0];
3101 uint64_t i2 = api.getRawData()[1];
3102 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3103 uint64_t mysignificand = i1;
3104 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3105
3106 initialize(&semIEEEquad);
3107 assert(partCount()==2)((partCount()==2) ? static_cast<void> (0) : __assert_fail
("partCount()==2", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3107, __PRETTY_FUNCTION__))
;
3108
3109 sign = static_cast<unsigned int>(i2>>63);
3110 if (myexponent==0 &&
3111 (mysignificand==0 && mysignificand2==0)) {
3112 // exponent, significand meaningless
3113 category = fcZero;
3114 } else if (myexponent==0x7fff &&
3115 (mysignificand==0 && mysignificand2==0)) {
3116 // exponent, significand meaningless
3117 category = fcInfinity;
3118 } else if (myexponent==0x7fff &&
3119 (mysignificand!=0 || mysignificand2 !=0)) {
3120 // exponent meaningless
3121 category = fcNaN;
3122 significandParts()[0] = mysignificand;
3123 significandParts()[1] = mysignificand2;
3124 } else {
3125 category = fcNormal;
3126 exponent = myexponent - 16383;
3127 significandParts()[0] = mysignificand;
3128 significandParts()[1] = mysignificand2;
3129 if (myexponent==0) // denormal
3130 exponent = -16382;
3131 else
3132 significandParts()[1] |= 0x1000000000000LL; // integer bit
3133 }
3134}
3135
3136void IEEEFloat::initFromDoubleAPInt(const APInt &api) {
3137 assert(api.getBitWidth()==64)((api.getBitWidth()==64) ? static_cast<void> (0) : __assert_fail
("api.getBitWidth()==64", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3137, __PRETTY_FUNCTION__))
;
3138 uint64_t i = *api.getRawData();
3139 uint64_t myexponent = (i >> 52) & 0x7ff;
3140 uint64_t mysignificand = i & 0xfffffffffffffLL;
3141
3142 initialize(&semIEEEdouble);
3143 assert(partCount()==1)((partCount()==1) ? static_cast<void> (0) : __assert_fail
("partCount()==1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3143, __PRETTY_FUNCTION__))
;
3144
3145 sign = static_cast<unsigned int>(i>>63);
3146 if (myexponent==0 && mysignificand==0) {
3147 // exponent, significand meaningless
3148 category = fcZero;
3149 } else if (myexponent==0x7ff && mysignificand==0) {
3150 // exponent, significand meaningless
3151 category = fcInfinity;
3152 } else if (myexponent==0x7ff && mysignificand!=0) {
3153 // exponent meaningless
3154 category = fcNaN;
3155 *significandParts() = mysignificand;
3156 } else {
3157 category = fcNormal;
3158 exponent = myexponent - 1023;
3159 *significandParts() = mysignificand;
3160 if (myexponent==0) // denormal
3161 exponent = -1022;
3162 else
3163 *significandParts() |= 0x10000000000000LL; // integer bit
3164 }
3165}
3166
3167void IEEEFloat::initFromFloatAPInt(const APInt &api) {
3168 assert(api.getBitWidth()==32)((api.getBitWidth()==32) ? static_cast<void> (0) : __assert_fail
("api.getBitWidth()==32", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3168, __PRETTY_FUNCTION__))
;
3169 uint32_t i = (uint32_t)*api.getRawData();
3170 uint32_t myexponent = (i >> 23) & 0xff;
3171 uint32_t mysignificand = i & 0x7fffff;
3172
3173 initialize(&semIEEEsingle);
3174 assert(partCount()==1)((partCount()==1) ? static_cast<void> (0) : __assert_fail
("partCount()==1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3174, __PRETTY_FUNCTION__))
;
3175
3176 sign = i >> 31;
3177 if (myexponent==0 && mysignificand==0) {
3178 // exponent, significand meaningless
3179 category = fcZero;
3180 } else if (myexponent==0xff && mysignificand==0) {
3181 // exponent, significand meaningless
3182 category = fcInfinity;
3183 } else if (myexponent==0xff && mysignificand!=0) {
3184 // sign, exponent, significand meaningless
3185 category = fcNaN;
3186 *significandParts() = mysignificand;
3187 } else {
3188 category = fcNormal;
3189 exponent = myexponent - 127; //bias
3190 *significandParts() = mysignificand;
3191 if (myexponent==0) // denormal
3192 exponent = -126;
3193 else
3194 *significandParts() |= 0x800000; // integer bit
3195 }
3196}
3197
3198void IEEEFloat::initFromHalfAPInt(const APInt &api) {
3199 assert(api.getBitWidth()==16)((api.getBitWidth()==16) ? static_cast<void> (0) : __assert_fail
("api.getBitWidth()==16", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3199, __PRETTY_FUNCTION__))
;
3200 uint32_t i = (uint32_t)*api.getRawData();
3201 uint32_t myexponent = (i >> 10) & 0x1f;
3202 uint32_t mysignificand = i & 0x3ff;
3203
3204 initialize(&semIEEEhalf);
3205 assert(partCount()==1)((partCount()==1) ? static_cast<void> (0) : __assert_fail
("partCount()==1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3205, __PRETTY_FUNCTION__))
;
3206
3207 sign = i >> 15;
3208 if (myexponent==0 && mysignificand==0) {
3209 // exponent, significand meaningless
3210 category = fcZero;
3211 } else if (myexponent==0x1f && mysignificand==0) {
3212 // exponent, significand meaningless
3213 category = fcInfinity;
3214 } else if (myexponent==0x1f && mysignificand!=0) {
3215 // sign, exponent, significand meaningless
3216 category = fcNaN;
3217 *significandParts() = mysignificand;
3218 } else {
3219 category = fcNormal;
3220 exponent = myexponent - 15; //bias
3221 *significandParts() = mysignificand;
3222 if (myexponent==0) // denormal
3223 exponent = -14;
3224 else
3225 *significandParts() |= 0x400; // integer bit
3226 }
3227}
3228
3229/// Treat api as containing the bits of a floating point number. Currently
3230/// we infer the floating point type from the size of the APInt. The
3231/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3232/// when the size is anything else).
3233void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) {
3234 if (Sem == &semIEEEhalf)
3235 return initFromHalfAPInt(api);
3236 if (Sem == &semIEEEsingle)
3237 return initFromFloatAPInt(api);
3238 if (Sem == &semIEEEdouble)
3239 return initFromDoubleAPInt(api);
3240 if (Sem == &semX87DoubleExtended)
3241 return initFromF80LongDoubleAPInt(api);
3242 if (Sem == &semIEEEquad)
3243 return initFromQuadrupleAPInt(api);
3244 if (Sem == &semPPCDoubleDoubleLegacy)
3245 return initFromPPCDoubleDoubleAPInt(api);
3246
3247 llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3247)
;
3248}
3249
3250/// Make this number the largest magnitude normal number in the given
3251/// semantics.
3252void IEEEFloat::makeLargest(bool Negative) {
3253 // We want (in interchange format):
3254 // sign = {Negative}
3255 // exponent = 1..10
3256 // significand = 1..1
3257 category = fcNormal;
3258 sign = Negative;
3259 exponent = semantics->maxExponent;
3260
3261 // Use memset to set all but the highest integerPart to all ones.
3262 integerPart *significand = significandParts();
3263 unsigned PartCount = partCount();
3264 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
3265
3266 // Set the high integerPart especially setting all unused top bits for
3267 // internal consistency.
3268 const unsigned NumUnusedHighBits =
3269 PartCount*integerPartWidth - semantics->precision;
3270 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3271 ? (~integerPart(0) >> NumUnusedHighBits)
3272 : 0;
3273}
3274
3275/// Make this number the smallest magnitude denormal number in the given
3276/// semantics.
3277void IEEEFloat::makeSmallest(bool Negative) {
3278 // We want (in interchange format):
3279 // sign = {Negative}
3280 // exponent = 0..0
3281 // significand = 0..01
3282 category = fcNormal;
3283 sign = Negative;
3284 exponent = semantics->minExponent;
3285 APInt::tcSet(significandParts(), 1, partCount());
3286}
3287
3288void IEEEFloat::makeSmallestNormalized(bool Negative) {
3289 // We want (in interchange format):
3290 // sign = {Negative}
3291 // exponent = 0..0
3292 // significand = 10..0
3293
3294 category = fcNormal;
3295 zeroSignificand();
3296 sign = Negative;
3297 exponent = semantics->minExponent;
3298 significandParts()[partCountForBits(semantics->precision) - 1] |=
3299 (((integerPart)1) << ((semantics->precision - 1) % integerPartWidth));
3300}
3301
3302IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) {
3303 initFromAPInt(&Sem, API);
3304}
3305
3306IEEEFloat::IEEEFloat(float f) {
3307 initFromAPInt(&semIEEEsingle, APInt::floatToBits(f));
3308}
3309
3310IEEEFloat::IEEEFloat(double d) {
3311 initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d));
3312}
3313
3314namespace {
3315 void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3316 Buffer.append(Str.begin(), Str.end());
3317 }
3318
3319 /// Removes data from the given significand until it is no more
3320 /// precise than is required for the desired precision.
3321 void AdjustToPrecision(APInt &significand,
3322 int &exp, unsigned FormatPrecision) {
3323 unsigned bits = significand.getActiveBits();
3324
3325 // 196/59 is a very slight overestimate of lg_2(10).
3326 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3327
3328 if (bits <= bitsRequired) return;
3329
3330 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3331 if (!tensRemovable) return;
3332
3333 exp += tensRemovable;
3334
3335 APInt divisor(significand.getBitWidth(), 1);
3336 APInt powten(significand.getBitWidth(), 10);
3337 while (true) {
3338 if (tensRemovable & 1)
3339 divisor *= powten;
3340 tensRemovable >>= 1;
3341 if (!tensRemovable) break;
3342 powten *= powten;
3343 }
3344
3345 significand = significand.udiv(divisor);
3346
3347 // Truncate the significand down to its active bit count.
3348 significand = significand.trunc(significand.getActiveBits());
3349 }
3350
3351
3352 void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3353 int &exp, unsigned FormatPrecision) {
3354 unsigned N = buffer.size();
3355 if (N <= FormatPrecision) return;
3356
3357 // The most significant figures are the last ones in the buffer.
3358 unsigned FirstSignificant = N - FormatPrecision;
3359
3360 // Round.
3361 // FIXME: this probably shouldn't use 'round half up'.
3362
3363 // Rounding down is just a truncation, except we also want to drop
3364 // trailing zeros from the new result.
3365 if (buffer[FirstSignificant - 1] < '5') {
3366 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
3367 FirstSignificant++;
3368
3369 exp += FirstSignificant;
3370 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3371 return;
3372 }
3373
3374 // Rounding up requires a decimal add-with-carry. If we continue
3375 // the carry, the newly-introduced zeros will just be truncated.
3376 for (unsigned I = FirstSignificant; I != N; ++I) {
3377 if (buffer[I] == '9') {
3378 FirstSignificant++;
3379 } else {
3380 buffer[I]++;
3381 break;
3382 }
3383 }
3384
3385 // If we carried through, we have exactly one digit of precision.
3386 if (FirstSignificant == N) {
3387 exp += FirstSignificant;
3388 buffer.clear();
3389 buffer.push_back('1');
3390 return;
3391 }
3392
3393 exp += FirstSignificant;
3394 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3395 }
3396}
3397
3398void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
3399 unsigned FormatMaxPadding, bool TruncateZero) const {
3400 switch (category) {
3401 case fcInfinity:
3402 if (isNegative())
3403 return append(Str, "-Inf");
3404 else
3405 return append(Str, "+Inf");
3406
3407 case fcNaN: return append(Str, "NaN");
3408
3409 case fcZero:
3410 if (isNegative())
3411 Str.push_back('-');
3412
3413 if (!FormatMaxPadding) {
3414 if (TruncateZero)
3415 append(Str, "0.0E+0");
3416 else {
3417 append(Str, "0.0");
3418 if (FormatPrecision > 1)
3419 Str.append(FormatPrecision - 1, '0');
3420 append(Str, "e+00");
3421 }
3422 } else
3423 Str.push_back('0');
3424 return;
3425
3426 case fcNormal:
3427 break;
3428 }
3429
3430 if (isNegative())
3431 Str.push_back('-');
3432
3433 // Decompose the number into an APInt and an exponent.
3434 int exp = exponent - ((int) semantics->precision - 1);
3435 APInt significand(semantics->precision,
3436 makeArrayRef(significandParts(),
3437 partCountForBits(semantics->precision)));
3438
3439 // Set FormatPrecision if zero. We want to do this before we
3440 // truncate trailing zeros, as those are part of the precision.
3441 if (!FormatPrecision) {
3442 // We use enough digits so the number can be round-tripped back to an
3443 // APFloat. The formula comes from "How to Print Floating-Point Numbers
3444 // Accurately" by Steele and White.
3445 // FIXME: Using a formula based purely on the precision is conservative;
3446 // we can print fewer digits depending on the actual value being printed.
3447
3448 // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3449 FormatPrecision = 2 + semantics->precision * 59 / 196;
3450 }
3451
3452 // Ignore trailing binary zeros.
3453 int trailingZeros = significand.countTrailingZeros();
3454 exp += trailingZeros;
3455 significand.lshrInPlace(trailingZeros);
3456
3457 // Change the exponent from 2^e to 10^e.
3458 if (exp == 0) {
3459 // Nothing to do.
3460 } else if (exp > 0) {
3461 // Just shift left.
3462 significand = significand.zext(semantics->precision + exp);
3463 significand <<= exp;
3464 exp = 0;
3465 } else { /* exp < 0 */
3466 int texp = -exp;
3467
3468 // We transform this using the identity:
3469 // (N)(2^-e) == (N)(5^e)(10^-e)
3470 // This means we have to multiply N (the significand) by 5^e.
3471 // To avoid overflow, we have to operate on numbers large
3472 // enough to store N * 5^e:
3473 // log2(N * 5^e) == log2(N) + e * log2(5)
3474 // <= semantics->precision + e * 137 / 59
3475 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
3476
3477 unsigned precision = semantics->precision + (137 * texp + 136) / 59;
3478
3479 // Multiply significand by 5^e.
3480 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
3481 significand = significand.zext(precision);
3482 APInt five_to_the_i(precision, 5);
3483 while (true) {
3484 if (texp & 1) significand *= five_to_the_i;
3485
3486 texp >>= 1;
3487 if (!texp) break;
3488 five_to_the_i *= five_to_the_i;
3489 }
3490 }
3491
3492 AdjustToPrecision(significand, exp, FormatPrecision);
3493
3494 SmallVector<char, 256> buffer;
3495
3496 // Fill the buffer.
3497 unsigned precision = significand.getBitWidth();
3498 APInt ten(precision, 10);
3499 APInt digit(precision, 0);
3500
3501 bool inTrail = true;
3502 while (significand != 0) {
3503 // digit <- significand % 10
3504 // significand <- significand / 10
3505 APInt::udivrem(significand, ten, significand, digit);
3506
3507 unsigned d = digit.getZExtValue();
3508
3509 // Drop trailing zeros.
3510 if (inTrail && !d) exp++;
3511 else {
3512 buffer.push_back((char) ('0' + d));
3513 inTrail = false;
3514 }
3515 }
3516
3517 assert(!buffer.empty() && "no characters in buffer!")((!buffer.empty() && "no characters in buffer!") ? static_cast
<void> (0) : __assert_fail ("!buffer.empty() && \"no characters in buffer!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3517, __PRETTY_FUNCTION__))
;
3518
3519 // Drop down to FormatPrecision.
3520 // TODO: don't do more precise calculations above than are required.
3521 AdjustToPrecision(buffer, exp, FormatPrecision);
3522
3523 unsigned NDigits = buffer.size();
3524
3525 // Check whether we should use scientific notation.
3526 bool FormatScientific;
3527 if (!FormatMaxPadding)
3528 FormatScientific = true;
3529 else {
3530 if (exp >= 0) {
3531 // 765e3 --> 765000
3532 // ^^^
3533 // But we shouldn't make the number look more precise than it is.
3534 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3535 NDigits + (unsigned) exp > FormatPrecision);
3536 } else {
3537 // Power of the most significant digit.
3538 int MSD = exp + (int) (NDigits - 1);
3539 if (MSD >= 0) {
3540 // 765e-2 == 7.65
3541 FormatScientific = false;
3542 } else {
3543 // 765e-5 == 0.00765
3544 // ^ ^^
3545 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
3546 }
3547 }
3548 }
3549
3550 // Scientific formatting is pretty straightforward.
3551 if (FormatScientific) {
3552 exp += (NDigits - 1);
3553
3554 Str.push_back(buffer[NDigits-1]);
3555 Str.push_back('.');
3556 if (NDigits == 1 && TruncateZero)
3557 Str.push_back('0');
3558 else
3559 for (unsigned I = 1; I != NDigits; ++I)
3560 Str.push_back(buffer[NDigits-1-I]);
3561 // Fill with zeros up to FormatPrecision.
3562 if (!TruncateZero && FormatPrecision > NDigits - 1)
3563 Str.append(FormatPrecision - NDigits + 1, '0');
3564 // For !TruncateZero we use lower 'e'.
3565 Str.push_back(TruncateZero ? 'E' : 'e');
3566
3567 Str.push_back(exp >= 0 ? '+' : '-');
3568 if (exp < 0) exp = -exp;
3569 SmallVector<char, 6> expbuf;
3570 do {
3571 expbuf.push_back((char) ('0' + (exp % 10)));
3572 exp /= 10;
3573 } while (exp);
3574 // Exponent always at least two digits if we do not truncate zeros.
3575 if (!TruncateZero && expbuf.size() < 2)
3576 expbuf.push_back('0');
3577 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3578 Str.push_back(expbuf[E-1-I]);
3579 return;
3580 }
3581
3582 // Non-scientific, positive exponents.
3583 if (exp >= 0) {
3584 for (unsigned I = 0; I != NDigits; ++I)
3585 Str.push_back(buffer[NDigits-1-I]);
3586 for (unsigned I = 0; I != (unsigned) exp; ++I)
3587 Str.push_back('0');
3588 return;
3589 }
3590
3591 // Non-scientific, negative exponents.
3592
3593 // The number of digits to the left of the decimal point.
3594 int NWholeDigits = exp + (int) NDigits;
3595
3596 unsigned I = 0;
3597 if (NWholeDigits > 0) {
3598 for (; I != (unsigned) NWholeDigits; ++I)
3599 Str.push_back(buffer[NDigits-I-1]);
3600 Str.push_back('.');
3601 } else {
3602 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3603
3604 Str.push_back('0');
3605 Str.push_back('.');
3606 for (unsigned Z = 1; Z != NZeros; ++Z)
3607 Str.push_back('0');
3608 }
3609
3610 for (; I != NDigits; ++I)
3611 Str.push_back(buffer[NDigits-I-1]);
3612}
3613
3614bool IEEEFloat::getExactInverse(APFloat *inv) const {
3615 // Special floats and denormals have no exact inverse.
3616 if (!isFiniteNonZero())
3617 return false;
3618
3619 // Check that the number is a power of two by making sure that only the
3620 // integer bit is set in the significand.
3621 if (significandLSB() != semantics->precision - 1)
3622 return false;
3623
3624 // Get the inverse.
3625 IEEEFloat reciprocal(*semantics, 1ULL);
3626 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3627 return false;
3628
3629 // Avoid multiplication with a denormal, it is not safe on all platforms and
3630 // may be slower than a normal division.
3631 if (reciprocal.isDenormal())
3632 return false;
3633
3634 assert(reciprocal.isFiniteNonZero() &&((reciprocal.isFiniteNonZero() && reciprocal.significandLSB
() == reciprocal.semantics->precision - 1) ? static_cast<
void> (0) : __assert_fail ("reciprocal.isFiniteNonZero() && reciprocal.significandLSB() == reciprocal.semantics->precision - 1"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3635, __PRETTY_FUNCTION__))
3635 reciprocal.significandLSB() == reciprocal.semantics->precision - 1)((reciprocal.isFiniteNonZero() && reciprocal.significandLSB
() == reciprocal.semantics->precision - 1) ? static_cast<
void> (0) : __assert_fail ("reciprocal.isFiniteNonZero() && reciprocal.significandLSB() == reciprocal.semantics->precision - 1"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3635, __PRETTY_FUNCTION__))
;
3636
3637 if (inv)
3638 *inv = APFloat(reciprocal, *semantics);
3639
3640 return true;
3641}
3642
3643bool IEEEFloat::isSignaling() const {
3644 if (!isNaN())
3645 return false;
3646
3647 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3648 // first bit of the trailing significand being 0.
3649 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3650}
3651
3652/// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3653///
3654/// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3655/// appropriate sign switching before/after the computation.
3656IEEEFloat::opStatus IEEEFloat::next(bool nextDown) {
3657 // If we are performing nextDown, swap sign so we have -x.
3658 if (nextDown)
3659 changeSign();
3660
3661 // Compute nextUp(x)
3662 opStatus result = opOK;
3663
3664 // Handle each float category separately.
3665 switch (category) {
3666 case fcInfinity:
3667 // nextUp(+inf) = +inf
3668 if (!isNegative())
3669 break;
3670 // nextUp(-inf) = -getLargest()
3671 makeLargest(true);
3672 break;
3673 case fcNaN:
3674 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3675 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3676 // change the payload.
3677 if (isSignaling()) {
3678 result = opInvalidOp;
3679 // For consistency, propagate the sign of the sNaN to the qNaN.
3680 makeNaN(false, isNegative(), nullptr);
3681 }
3682 break;
3683 case fcZero:
3684 // nextUp(pm 0) = +getSmallest()
3685 makeSmallest(false);
3686 break;
3687 case fcNormal:
3688 // nextUp(-getSmallest()) = -0
3689 if (isSmallest() && isNegative()) {
3690 APInt::tcSet(significandParts(), 0, partCount());
3691 category = fcZero;
3692 exponent = 0;
3693 break;
3694 }
3695
3696 // nextUp(getLargest()) == INFINITY
3697 if (isLargest() && !isNegative()) {
3698 APInt::tcSet(significandParts(), 0, partCount());
3699 category = fcInfinity;
3700 exponent = semantics->maxExponent + 1;
3701 break;
3702 }
3703
3704 // nextUp(normal) == normal + inc.
3705 if (isNegative()) {
3706 // If we are negative, we need to decrement the significand.
3707
3708 // We only cross a binade boundary that requires adjusting the exponent
3709 // if:
3710 // 1. exponent != semantics->minExponent. This implies we are not in the
3711 // smallest binade or are dealing with denormals.
3712 // 2. Our significand excluding the integral bit is all zeros.
3713 bool WillCrossBinadeBoundary =
3714 exponent != semantics->minExponent && isSignificandAllZeros();
3715
3716 // Decrement the significand.
3717 //
3718 // We always do this since:
3719 // 1. If we are dealing with a non-binade decrement, by definition we
3720 // just decrement the significand.
3721 // 2. If we are dealing with a normal -> normal binade decrement, since
3722 // we have an explicit integral bit the fact that all bits but the
3723 // integral bit are zero implies that subtracting one will yield a
3724 // significand with 0 integral bit and 1 in all other spots. Thus we
3725 // must just adjust the exponent and set the integral bit to 1.
3726 // 3. If we are dealing with a normal -> denormal binade decrement,
3727 // since we set the integral bit to 0 when we represent denormals, we
3728 // just decrement the significand.
3729 integerPart *Parts = significandParts();
3730 APInt::tcDecrement(Parts, partCount());
3731
3732 if (WillCrossBinadeBoundary) {
3733 // Our result is a normal number. Do the following:
3734 // 1. Set the integral bit to 1.
3735 // 2. Decrement the exponent.
3736 APInt::tcSetBit(Parts, semantics->precision - 1);
3737 exponent--;
3738 }
3739 } else {
3740 // If we are positive, we need to increment the significand.
3741
3742 // We only cross a binade boundary that requires adjusting the exponent if
3743 // the input is not a denormal and all of said input's significand bits
3744 // are set. If all of said conditions are true: clear the significand, set
3745 // the integral bit to 1, and increment the exponent. If we have a
3746 // denormal always increment since moving denormals and the numbers in the
3747 // smallest normal binade have the same exponent in our representation.
3748 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3749
3750 if (WillCrossBinadeBoundary) {
3751 integerPart *Parts = significandParts();
3752 APInt::tcSet(Parts, 0, partCount());
3753 APInt::tcSetBit(Parts, semantics->precision - 1);
3754 assert(exponent != semantics->maxExponent &&((exponent != semantics->maxExponent && "We can not increment an exponent beyond the maxExponent allowed"
" by the given floating point semantics.") ? static_cast<
void> (0) : __assert_fail ("exponent != semantics->maxExponent && \"We can not increment an exponent beyond the maxExponent allowed\" \" by the given floating point semantics.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3756, __PRETTY_FUNCTION__))
3755 "We can not increment an exponent beyond the maxExponent allowed"((exponent != semantics->maxExponent && "We can not increment an exponent beyond the maxExponent allowed"
" by the given floating point semantics.") ? static_cast<
void> (0) : __assert_fail ("exponent != semantics->maxExponent && \"We can not increment an exponent beyond the maxExponent allowed\" \" by the given floating point semantics.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3756, __PRETTY_FUNCTION__))
3756 " by the given floating point semantics.")((exponent != semantics->maxExponent && "We can not increment an exponent beyond the maxExponent allowed"
" by the given floating point semantics.") ? static_cast<
void> (0) : __assert_fail ("exponent != semantics->maxExponent && \"We can not increment an exponent beyond the maxExponent allowed\" \" by the given floating point semantics.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3756, __PRETTY_FUNCTION__))
;
3757 exponent++;
3758 } else {
3759 incrementSignificand();
3760 }
3761 }
3762 break;
3763 }
3764
3765 // If we are performing nextDown, swap sign so we have -nextUp(-x)
3766 if (nextDown)
3767 changeSign();
3768
3769 return result;
3770}
3771
3772void IEEEFloat::makeInf(bool Negative) {
3773 category = fcInfinity;
3774 sign = Negative;
3775 exponent = semantics->maxExponent + 1;
3776 APInt::tcSet(significandParts(), 0, partCount());
3777}
3778
3779void IEEEFloat::makeZero(bool Negative) {
3780 category = fcZero;
3781 sign = Negative;
3782 exponent = semantics->minExponent-1;
3783 APInt::tcSet(significandParts(), 0, partCount());
3784}
3785
3786void IEEEFloat::makeQuiet() {
3787 assert(isNaN())((isNaN()) ? static_cast<void> (0) : __assert_fail ("isNaN()"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3787, __PRETTY_FUNCTION__))
;
3788 APInt::tcSetBit(significandParts(), semantics->precision - 2);
3789}
3790
3791int ilogb(const IEEEFloat &Arg) {
3792 if (Arg.isNaN())
3793 return IEEEFloat::IEK_NaN;
3794 if (Arg.isZero())
3795 return IEEEFloat::IEK_Zero;
3796 if (Arg.isInfinity())
3797 return IEEEFloat::IEK_Inf;
3798 if (!Arg.isDenormal())
3799 return Arg.exponent;
3800
3801 IEEEFloat Normalized(Arg);
3802 int SignificandBits = Arg.getSemantics().precision - 1;
3803
3804 Normalized.exponent += SignificandBits;
3805 Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero);
3806 return Normalized.exponent - SignificandBits;
3807}
3808
3809IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) {
3810 auto MaxExp = X.getSemantics().maxExponent;
3811 auto MinExp = X.getSemantics().minExponent;
3812
3813 // If Exp is wildly out-of-scale, simply adding it to X.exponent will
3814 // overflow; clamp it to a safe range before adding, but ensure that the range
3815 // is large enough that the clamp does not change the result. The range we
3816 // need to support is the difference between the largest possible exponent and
3817 // the normalized exponent of half the smallest denormal.
3818
3819 int SignificandBits = X.getSemantics().precision - 1;
3820 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
3821
3822 // Clamp to one past the range ends to let normalize handle overlflow.
3823 X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement);
3824 X.normalize(RoundingMode, lfExactlyZero);
3825 if (X.isNaN())
3826 X.makeQuiet();
3827 return X;
3828}
3829
3830IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) {
3831 Exp = ilogb(Val);
3832
3833 // Quiet signalling nans.
3834 if (Exp == IEEEFloat::IEK_NaN) {
3835 IEEEFloat Quiet(Val);
3836 Quiet.makeQuiet();
3837 return Quiet;
3838 }
3839
3840 if (Exp == IEEEFloat::IEK_Inf)
3841 return Val;
3842
3843 // 1 is added because frexp is defined to return a normalized fraction in
3844 // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0).
3845 Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1;
3846 return scalbn(Val, -Exp, RM);
3847}
3848
3849DoubleAPFloat::DoubleAPFloat(const fltSemantics &S)
3850 : Semantics(&S),
3851 Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) {
3852 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3852, __PRETTY_FUNCTION__))
;
3853}
3854
3855DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag)
3856 : Semantics(&S),
3857 Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized),
3858 APFloat(semIEEEdouble, uninitialized)}) {
3859 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3859, __PRETTY_FUNCTION__))
;
3860}
3861
3862DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I)
3863 : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I),
3864 APFloat(semIEEEdouble)}) {
3865 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3865, __PRETTY_FUNCTION__))
;
3866}
3867
3868DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I)
3869 : Semantics(&S),
3870 Floats(new APFloat[2]{
3871 APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])),
3872 APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) {
3873 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3873, __PRETTY_FUNCTION__))
;
3874}
3875
3876DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First,
3877 APFloat &&Second)
3878 : Semantics(&S),
3879 Floats(new APFloat[2]{std::move(First), std::move(Second)}) {
3880 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3880, __PRETTY_FUNCTION__))
;
3881 assert(&Floats[0].getSemantics() == &semIEEEdouble)((&Floats[0].getSemantics() == &semIEEEdouble) ? static_cast
<void> (0) : __assert_fail ("&Floats[0].getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3881, __PRETTY_FUNCTION__))
;
3882 assert(&Floats[1].getSemantics() == &semIEEEdouble)((&Floats[1].getSemantics() == &semIEEEdouble) ? static_cast
<void> (0) : __assert_fail ("&Floats[1].getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3882, __PRETTY_FUNCTION__))
;
3883}
3884
3885DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS)
3886 : Semantics(RHS.Semantics),
3887 Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]),
3888 APFloat(RHS.Floats[1])}
3889 : nullptr) {
3890 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3890, __PRETTY_FUNCTION__))
;
3891}
3892
3893DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS)
3894 : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) {
3895 RHS.Semantics = &semBogus;
3896 assert(Semantics == &semPPCDoubleDouble)((Semantics == &semPPCDoubleDouble) ? static_cast<void
> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 3896, __PRETTY_FUNCTION__))
;
3897}
3898
3899DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) {
3900 if (Semantics == RHS.Semantics && RHS.Floats) {
3901 Floats[0] = RHS.Floats[0];
3902 Floats[1] = RHS.Floats[1];
3903 } else if (this != &RHS) {
3904 this->~DoubleAPFloat();
3905 new (this) DoubleAPFloat(RHS);
3906 }
3907 return *this;
3908}
3909
3910// Implement addition, subtraction, multiplication and division based on:
3911// "Software for Doubled-Precision Floating-Point Computations",
3912// by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283.
3913APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa,
3914 const APFloat &c, const APFloat &cc,
3915 roundingMode RM) {
3916 int Status = opOK;
3917 APFloat z = a;
3918 Status |= z.add(c, RM);
3919 if (!z.isFinite()) {
3920 if (!z.isInfinity()) {
3921 Floats[0] = std::move(z);
3922 Floats[1].makeZero(/* Neg = */ false);
3923 return (opStatus)Status;
3924 }
3925 Status = opOK;
3926 auto AComparedToC = a.compareAbsoluteValue(c);
3927 z = cc;
3928 Status |= z.add(aa, RM);
3929 if (AComparedToC == APFloat::cmpGreaterThan) {
3930 // z = cc + aa + c + a;
3931 Status |= z.add(c, RM);
3932 Status |= z.add(a, RM);
3933 } else {
3934 // z = cc + aa + a + c;
3935 Status |= z.add(a, RM);
3936 Status |= z.add(c, RM);
3937 }
3938 if (!z.isFinite()) {
3939 Floats[0] = std::move(z);
3940 Floats[1].makeZero(/* Neg = */ false);
3941 return (opStatus)Status;
3942 }
3943 Floats[0] = z;
3944 APFloat zz = aa;
3945 Status |= zz.add(cc, RM);
3946 if (AComparedToC == APFloat::cmpGreaterThan) {
3947 // Floats[1] = a - z + c + zz;
3948 Floats[1] = a;
3949 Status |= Floats[1].subtract(z, RM);
3950 Status |= Floats[1].add(c, RM);
3951 Status |= Floats[1].add(zz, RM);
3952 } else {
3953 // Floats[1] = c - z + a + zz;
3954 Floats[1] = c;
3955 Status |= Floats[1].subtract(z, RM);
3956 Status |= Floats[1].add(a, RM);
3957 Status |= Floats[1].add(zz, RM);
3958 }
3959 } else {
3960 // q = a - z;
3961 APFloat q = a;
3962 Status |= q.subtract(z, RM);
3963
3964 // zz = q + c + (a - (q + z)) + aa + cc;
3965 // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies.
3966 auto zz = q;
3967 Status |= zz.add(c, RM);
3968 Status |= q.add(z, RM);
3969 Status |= q.subtract(a, RM);
3970 q.changeSign();
3971 Status |= zz.add(q, RM);
3972 Status |= zz.add(aa, RM);
3973 Status |= zz.add(cc, RM);
3974 if (zz.isZero() && !zz.isNegative()) {
3975 Floats[0] = std::move(z);
3976 Floats[1].makeZero(/* Neg = */ false);
3977 return opOK;
3978 }
3979 Floats[0] = z;
3980 Status |= Floats[0].add(zz, RM);
3981 if (!Floats[0].isFinite()) {
3982 Floats[1].makeZero(/* Neg = */ false);
3983 return (opStatus)Status;
3984 }
3985 Floats[1] = std::move(z);
3986 Status |= Floats[1].subtract(Floats[0], RM);
3987 Status |= Floats[1].add(zz, RM);
3988 }
3989 return (opStatus)Status;
3990}
3991
3992APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS,
3993 const DoubleAPFloat &RHS,
3994 DoubleAPFloat &Out,
3995 roundingMode RM) {
3996 if (LHS.getCategory() == fcNaN) {
3997 Out = LHS;
3998 return opOK;
3999 }
4000 if (RHS.getCategory() == fcNaN) {
4001 Out = RHS;
4002 return opOK;
4003 }
4004 if (LHS.getCategory() == fcZero) {
4005 Out = RHS;
4006 return opOK;
4007 }
4008 if (RHS.getCategory() == fcZero) {
4009 Out = LHS;
4010 return opOK;
4011 }
4012 if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity &&
4013 LHS.isNegative() != RHS.isNegative()) {
4014 Out.makeNaN(false, Out.isNegative(), nullptr);
4015 return opInvalidOp;
4016 }
4017 if (LHS.getCategory() == fcInfinity) {
4018 Out = LHS;
4019 return opOK;
4020 }
4021 if (RHS.getCategory() == fcInfinity) {
4022 Out = RHS;
4023 return opOK;
4024 }
4025 assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal)((LHS.getCategory() == fcNormal && RHS.getCategory() ==
fcNormal) ? static_cast<void> (0) : __assert_fail ("LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4025, __PRETTY_FUNCTION__))
;
4026
4027 APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]),
4028 CC(RHS.Floats[1]);
4029 assert(&A.getSemantics() == &semIEEEdouble)((&A.getSemantics() == &semIEEEdouble) ? static_cast<
void> (0) : __assert_fail ("&A.getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4029, __PRETTY_FUNCTION__))
;
4030 assert(&AA.getSemantics() == &semIEEEdouble)((&AA.getSemantics() == &semIEEEdouble) ? static_cast
<void> (0) : __assert_fail ("&AA.getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4030, __PRETTY_FUNCTION__))
;
4031 assert(&C.getSemantics() == &semIEEEdouble)((&C.getSemantics() == &semIEEEdouble) ? static_cast<
void> (0) : __assert_fail ("&C.getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4031, __PRETTY_FUNCTION__))
;
4032 assert(&CC.getSemantics() == &semIEEEdouble)((&CC.getSemantics() == &semIEEEdouble) ? static_cast
<void> (0) : __assert_fail ("&CC.getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4032, __PRETTY_FUNCTION__))
;
4033 assert(&Out.Floats[0].getSemantics() == &semIEEEdouble)((&Out.Floats[0].getSemantics() == &semIEEEdouble) ? static_cast
<void> (0) : __assert_fail ("&Out.Floats[0].getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4033, __PRETTY_FUNCTION__))
;
4034 assert(&Out.Floats[1].getSemantics() == &semIEEEdouble)((&Out.Floats[1].getSemantics() == &semIEEEdouble) ? static_cast
<void> (0) : __assert_fail ("&Out.Floats[1].getSemantics() == &semIEEEdouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4034, __PRETTY_FUNCTION__))
;
4035 return Out.addImpl(A, AA, C, CC, RM);
4036}
4037
4038APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS,
4039 roundingMode RM) {
4040 return addWithSpecial(*this, RHS, *this, RM);
4041}
4042
4043APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS,
4044 roundingMode RM) {
4045 changeSign();
4046 auto Ret = add(RHS, RM);
4047 changeSign();
4048 return Ret;
4049}
4050
4051APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS,
4052 APFloat::roundingMode RM) {
4053 const auto &LHS = *this;
4054 auto &Out = *this;
4055 /* Interesting observation: For special categories, finding the lowest
4056 common ancestor of the following layered graph gives the correct
4057 return category:
4058
4059 NaN
4060 / \
4061 Zero Inf
4062 \ /
4063 Normal
4064
4065 e.g. NaN * NaN = NaN
4066 Zero * Inf = NaN
4067 Normal * Zero = Zero
4068 Normal * Inf = Inf
4069 */
4070 if (LHS.getCategory() == fcNaN) {
4071 Out = LHS;
4072 return opOK;
4073 }
4074 if (RHS.getCategory() == fcNaN) {
4075 Out = RHS;
4076 return opOK;
4077 }
4078 if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) ||
4079 (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) {
4080 Out.makeNaN(false, false, nullptr);
4081 return opOK;
4082 }
4083 if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) {
4084 Out = LHS;
4085 return opOK;
4086 }
4087 if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) {
4088 Out = RHS;
4089 return opOK;
4090 }
4091 assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal &&((LHS.getCategory() == fcNormal && RHS.getCategory() ==
fcNormal && "Special cases not handled exhaustively"
) ? static_cast<void> (0) : __assert_fail ("LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && \"Special cases not handled exhaustively\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4092, __PRETTY_FUNCTION__))
4092 "Special cases not handled exhaustively")((LHS.getCategory() == fcNormal && RHS.getCategory() ==
fcNormal && "Special cases not handled exhaustively"
) ? static_cast<void> (0) : __assert_fail ("LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && \"Special cases not handled exhaustively\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4092, __PRETTY_FUNCTION__))
;
4093
4094 int Status = opOK;
4095 APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1];
4096 // t = a * c
4097 APFloat T = A;
4098 Status |= T.multiply(C, RM);
4099 if (!T.isFiniteNonZero()) {
4100 Floats[0] = T;
4101 Floats[1].makeZero(/* Neg = */ false);
4102 return (opStatus)Status;
4103 }
4104
4105 // tau = fmsub(a, c, t), that is -fmadd(-a, c, t).
4106 APFloat Tau = A;
4107 T.changeSign();
4108 Status |= Tau.fusedMultiplyAdd(C, T, RM);
4109 T.changeSign();
4110 {
4111 // v = a * d
4112 APFloat V = A;
4113 Status |= V.multiply(D, RM);
4114 // w = b * c
4115 APFloat W = B;
4116 Status |= W.multiply(C, RM);
4117 Status |= V.add(W, RM);
4118 // tau += v + w
4119 Status |= Tau.add(V, RM);
4120 }
4121 // u = t + tau
4122 APFloat U = T;
4123 Status |= U.add(Tau, RM);
4124
4125 Floats[0] = U;
4126 if (!U.isFinite()) {
4127 Floats[1].makeZero(/* Neg = */ false);
4128 } else {
4129 // Floats[1] = (t - u) + tau
4130 Status |= T.subtract(U, RM);
4131 Status |= T.add(Tau, RM);
4132 Floats[1] = T;
4133 }
4134 return (opStatus)Status;
4135}
4136
4137APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS,
4138 APFloat::roundingMode RM) {
4139 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4139, __PRETTY_FUNCTION__))
;
4140 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4141 auto Ret =
4142 Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM);
4143 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4144 return Ret;
4145}
4146
4147APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) {
4148 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4148, __PRETTY_FUNCTION__))
;
4149 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4150 auto Ret =
4151 Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
4152 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4153 return Ret;
4154}
4155
4156APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) {
4157 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4157, __PRETTY_FUNCTION__))
;
4158 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4159 auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
4160 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4161 return Ret;
4162}
4163
4164APFloat::opStatus
4165DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
4166 const DoubleAPFloat &Addend,
4167 APFloat::roundingMode RM) {
4168 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4168, __PRETTY_FUNCTION__))
;
4169 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4170 auto Ret = Tmp.fusedMultiplyAdd(
4171 APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()),
4172 APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM);
4173 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4174 return Ret;
4175}
4176
4177APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) {
4178 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4178, __PRETTY_FUNCTION__))
;
4179 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4180 auto Ret = Tmp.roundToIntegral(RM);
4181 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4182 return Ret;
4183}
4184
4185void DoubleAPFloat::changeSign() {
4186 Floats[0].changeSign();
4187 Floats[1].changeSign();
4188}
4189
4190APFloat::cmpResult
4191DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const {
4192 auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]);
4193 if (Result != cmpEqual)
4194 return Result;
4195 Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]);
4196 if (Result == cmpLessThan || Result == cmpGreaterThan) {
4197 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4198 auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative();
4199 if (Against && !RHSAgainst)
4200 return cmpLessThan;
4201 if (!Against && RHSAgainst)
4202 return cmpGreaterThan;
4203 if (!Against && !RHSAgainst)
4204 return Result;
4205 if (Against && RHSAgainst)
4206 return (cmpResult)(cmpLessThan + cmpGreaterThan - Result);
4207 }
4208 return Result;
4209}
4210
4211APFloat::fltCategory DoubleAPFloat::getCategory() const {
4212 return Floats[0].getCategory();
4213}
4214
4215bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); }
4216
4217void DoubleAPFloat::makeInf(bool Neg) {
4218 Floats[0].makeInf(Neg);
4219 Floats[1].makeZero(/* Neg = */ false);
4220}
4221
4222void DoubleAPFloat::makeZero(bool Neg) {
4223 Floats[0].makeZero(Neg);
4224 Floats[1].makeZero(/* Neg = */ false);
4225}
4226
4227void DoubleAPFloat::makeLargest(bool Neg) {
4228 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4228, __PRETTY_FUNCTION__))
;
4229 Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull));
4230 Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull));
4231 if (Neg)
4232 changeSign();
4233}
4234
4235void DoubleAPFloat::makeSmallest(bool Neg) {
4236 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4236, __PRETTY_FUNCTION__))
;
4237 Floats[0].makeSmallest(Neg);
4238 Floats[1].makeZero(/* Neg = */ false);
4239}
4240
4241void DoubleAPFloat::makeSmallestNormalized(bool Neg) {
4242 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4242, __PRETTY_FUNCTION__))
;
4243 Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull));
4244 if (Neg)
4245 Floats[0].changeSign();
4246 Floats[1].makeZero(/* Neg = */ false);
4247}
4248
4249void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) {
4250 Floats[0].makeNaN(SNaN, Neg, fill);
4251 Floats[1].makeZero(/* Neg = */ false);
4252}
4253
4254APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const {
4255 auto Result = Floats[0].compare(RHS.Floats[0]);
4256 // |Float[0]| > |Float[1]|
4257 if (Result == APFloat::cmpEqual)
4258 return Floats[1].compare(RHS.Floats[1]);
4259 return Result;
4260}
4261
4262bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const {
4263 return Floats[0].bitwiseIsEqual(RHS.Floats[0]) &&
4264 Floats[1].bitwiseIsEqual(RHS.Floats[1]);
4265}
4266
4267hash_code hash_value(const DoubleAPFloat &Arg) {
4268 if (Arg.Floats)
4269 return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1]));
4270 return hash_combine(Arg.Semantics);
4271}
4272
4273APInt DoubleAPFloat::bitcastToAPInt() const {
4274 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4274, __PRETTY_FUNCTION__))
;
4275 uint64_t Data[] = {
4276 Floats[0].bitcastToAPInt().getRawData()[0],
4277 Floats[1].bitcastToAPInt().getRawData()[0],
4278 };
4279 return APInt(128, 2, Data);
4280}
4281
4282APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S,
4283 roundingMode RM) {
4284 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4284, __PRETTY_FUNCTION__))
;
4285 APFloat Tmp(semPPCDoubleDoubleLegacy);
4286 auto Ret = Tmp.convertFromString(S, RM);
4287 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4288 return Ret;
4289}
4290
4291APFloat::opStatus DoubleAPFloat::next(bool nextDown) {
4292 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4292, __PRETTY_FUNCTION__))
;
4293 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4294 auto Ret = Tmp.next(nextDown);
4295 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4296 return Ret;
4297}
4298
4299APFloat::opStatus
4300DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input,
4301 unsigned int Width, bool IsSigned,
4302 roundingMode RM, bool *IsExact) const {
4303 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4303, __PRETTY_FUNCTION__))
;
4304 return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4305 .convertToInteger(Input, Width, IsSigned, RM, IsExact);
4306}
4307
4308APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input,
4309 bool IsSigned,
4310 roundingMode RM) {
4311 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4311, __PRETTY_FUNCTION__))
;
4312 APFloat Tmp(semPPCDoubleDoubleLegacy);
4313 auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM);
4314 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4315 return Ret;
4316}
4317
4318APFloat::opStatus
4319DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input,
4320 unsigned int InputSize,
4321 bool IsSigned, roundingMode RM) {
4322 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4322, __PRETTY_FUNCTION__))
;
4323 APFloat Tmp(semPPCDoubleDoubleLegacy);
4324 auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM);
4325 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4326 return Ret;
4327}
4328
4329APFloat::opStatus
4330DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input,
4331 unsigned int InputSize,
4332 bool IsSigned, roundingMode RM) {
4333 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4333, __PRETTY_FUNCTION__))
;
4334 APFloat Tmp(semPPCDoubleDoubleLegacy);
4335 auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM);
4336 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4337 return Ret;
4338}
4339
4340unsigned int DoubleAPFloat::convertToHexString(char *DST,
4341 unsigned int HexDigits,
4342 bool UpperCase,
4343 roundingMode RM) const {
4344 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4344, __PRETTY_FUNCTION__))
;
4345 return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4346 .convertToHexString(DST, HexDigits, UpperCase, RM);
4347}
4348
4349bool DoubleAPFloat::isDenormal() const {
4350 return getCategory() == fcNormal &&
4351 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
4352 // (double)(Hi + Lo) == Hi defines a normal number.
4353 Floats[0].compare(Floats[0] + Floats[1]) != cmpEqual);
4354}
4355
4356bool DoubleAPFloat::isSmallest() const {
4357 if (getCategory() != fcNormal)
4358 return false;
4359 DoubleAPFloat Tmp(*this);
4360 Tmp.makeSmallest(this->isNegative());
4361 return Tmp.compare(*this) == cmpEqual;
4362}
4363
4364bool DoubleAPFloat::isLargest() const {
4365 if (getCategory() != fcNormal)
4366 return false;
4367 DoubleAPFloat Tmp(*this);
4368 Tmp.makeLargest(this->isNegative());
4369 return Tmp.compare(*this) == cmpEqual;
4370}
4371
4372bool DoubleAPFloat::isInteger() const {
4373 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4373, __PRETTY_FUNCTION__))
;
4374 return Floats[0].isInteger() && Floats[1].isInteger();
4375}
4376
4377void DoubleAPFloat::toString(SmallVectorImpl<char> &Str,
4378 unsigned FormatPrecision,
4379 unsigned FormatMaxPadding,
4380 bool TruncateZero) const {
4381 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4381, __PRETTY_FUNCTION__))
;
4382 APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4383 .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
4384}
4385
4386bool DoubleAPFloat::getExactInverse(APFloat *inv) const {
4387 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4387, __PRETTY_FUNCTION__))
;
4388 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4389 if (!inv)
4390 return Tmp.getExactInverse(nullptr);
4391 APFloat Inv(semPPCDoubleDoubleLegacy);
4392 auto Ret = Tmp.getExactInverse(&Inv);
4393 *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt());
4394 return Ret;
4395}
4396
4397DoubleAPFloat scalbn(DoubleAPFloat Arg, int Exp, APFloat::roundingMode RM) {
4398 assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Arg.Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4398, __PRETTY_FUNCTION__))
;
4399 return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM),
4400 scalbn(Arg.Floats[1], Exp, RM));
4401}
4402
4403DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp,
4404 APFloat::roundingMode RM) {
4405 assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics")((Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"
) ? static_cast<void> (0) : __assert_fail ("Arg.Semantics == &semPPCDoubleDouble && \"Unexpected Semantics\""
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4405, __PRETTY_FUNCTION__))
;
4406 APFloat First = frexp(Arg.Floats[0], Exp, RM);
4407 APFloat Second = Arg.Floats[1];
4408 if (Arg.getCategory() == APFloat::fcNormal)
4409 Second = scalbn(Second, -Exp, RM);
4410 return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second));
4411}
4412
4413} // End detail namespace
4414
4415APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
4416 if (usesLayout<IEEEFloat>(Semantics)) {
1
Assuming the condition is false
2
Taking false branch
4417 new (&IEEE) IEEEFloat(std::move(F));
4418 return;
4419 }
4420 if (usesLayout<DoubleAPFloat>(Semantics)) {
3
Assuming the condition is true
4
Taking true branch
4421 new (&Double)
4422 DoubleAPFloat(Semantics, APFloat(std::move(F), F.getSemantics()),
5
Calling move constructor for 'IEEEFloat'
7
Returning from move constructor for 'IEEEFloat'
8
Method called on moved-from object 'F'
4423 APFloat(semIEEEdouble));
4424 return;
4425 }
4426 llvm_unreachable("Unexpected semantics")::llvm::llvm_unreachable_internal("Unexpected semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4426)
;
4427}
4428
4429APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) {
4430 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
4431}
4432
4433hash_code hash_value(const APFloat &Arg) {
4434 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics()))
4435 return hash_value(Arg.U.IEEE);
4436 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics()))
4437 return hash_value(Arg.U.Double);
4438 llvm_unreachable("Unexpected semantics")::llvm::llvm_unreachable_internal("Unexpected semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4438)
;
4439}
4440
4441APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
4442 : APFloat(Semantics) {
4443 convertFromString(S, rmNearestTiesToEven);
4444}
4445
4446APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics,
4447 roundingMode RM, bool *losesInfo) {
4448 if (&getSemantics() == &ToSemantics) {
4449 *losesInfo = false;
4450 return opOK;
4451 }
4452 if (usesLayout<IEEEFloat>(getSemantics()) &&
4453 usesLayout<IEEEFloat>(ToSemantics))
4454 return U.IEEE.convert(ToSemantics, RM, losesInfo);
4455 if (usesLayout<IEEEFloat>(getSemantics()) &&
4456 usesLayout<DoubleAPFloat>(ToSemantics)) {
4457 assert(&ToSemantics == &semPPCDoubleDouble)((&ToSemantics == &semPPCDoubleDouble) ? static_cast<
void> (0) : __assert_fail ("&ToSemantics == &semPPCDoubleDouble"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4457, __PRETTY_FUNCTION__))
;
4458 auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo);
4459 *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
4460 return Ret;
4461 }
4462 if (usesLayout<DoubleAPFloat>(getSemantics()) &&
4463 usesLayout<IEEEFloat>(ToSemantics)) {
4464 auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo);
4465 *this = APFloat(std::move(getIEEE()), ToSemantics);
4466 return Ret;
4467 }
4468 llvm_unreachable("Unexpected semantics")::llvm::llvm_unreachable_internal("Unexpected semantics", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4468)
;
4469}
4470
4471APFloat APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) {
4472 if (isIEEE) {
4473 switch (BitWidth) {
4474 case 16:
4475 return APFloat(semIEEEhalf, APInt::getAllOnesValue(BitWidth));
4476 case 32:
4477 return APFloat(semIEEEsingle, APInt::getAllOnesValue(BitWidth));
4478 case 64:
4479 return APFloat(semIEEEdouble, APInt::getAllOnesValue(BitWidth));
4480 case 80:
4481 return APFloat(semX87DoubleExtended, APInt::getAllOnesValue(BitWidth));
4482 case 128:
4483 return APFloat(semIEEEquad, APInt::getAllOnesValue(BitWidth));
4484 default:
4485 llvm_unreachable("Unknown floating bit width")::llvm::llvm_unreachable_internal("Unknown floating bit width"
, "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4485)
;
4486 }
4487 } else {
4488 assert(BitWidth == 128)((BitWidth == 128) ? static_cast<void> (0) : __assert_fail
("BitWidth == 128", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Support/APFloat.cpp"
, 4488, __PRETTY_FUNCTION__))
;
4489 return APFloat(semPPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
4490 }
4491}
4492
4493void APFloat::print(raw_ostream &OS) const {
4494 SmallVector<char, 16> Buffer;
4495 toString(Buffer);
4496 OS << Buffer << "\n";
4497}
4498
4499#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4500LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void APFloat::dump() const { print(dbgs()); }
4501#endif
4502
4503void APFloat::Profile(FoldingSetNodeID &NID) const {
4504 NID.Add(bitcastToAPInt());
4505}
4506
4507/* Same as convertToInteger(integerPart*, ...), except the result is returned in
4508 an APSInt, whose initial bit-width and signed-ness are used to determine the
4509 precision of the conversion.
4510 */
4511APFloat::opStatus APFloat::convertToInteger(APSInt &result,
4512 roundingMode rounding_mode,
4513 bool *isExact) const {
4514 unsigned bitWidth = result.getBitWidth();
4515 SmallVector<uint64_t, 4> parts(result.getNumWords());
4516 opStatus status = convertToInteger(parts, bitWidth, result.isSigned(),
4517 rounding_mode, isExact);
4518 // Keeps the original signed-ness.
4519 result = APInt(bitWidth, parts);
4520 return status;
4521}
4522
4523} // End llvm namespace
4524
4525#undef APFLOAT_DISPATCH_ON_SEMANTICS