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