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