Bug Summary

File:build/source/llvm/lib/Support/APFloat.cpp
Warning:line 638, column 24
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

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