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