LLVM: lib/Support/APFloat.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
23#include "llvm/Config/llvm-config.h"
28#include
29#include <limits.h>
30
31#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
32 do { \
33 if (usesLayout(getSemantics())) \
34 return U.IEEE.METHOD_CALL; \
35 if (usesLayout(getSemantics())) \
36 return U.Double.METHOD_CALL; \
37 llvm_unreachable("Unexpected semantics"); \
38 } while (false)
39
40using namespace llvm;
41
42
43
44
45
46
47
48#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
49
50
51
53
54namespace llvm {
55
56
58
59
60
62
63
64
65
66
67
68
69
71
72
73
75};
76
77
78
79
80
82
83
85
86
87
88
89
90
91
93
94
95
96
97
98
100};
101
102
104
105
107
108
109
111
112
113
115
116
118
120
122
123
125
126
128
129
131};
132
133constexpr fltSemantics APFloatBase::semIEEEhalf = {15, -14, 11, 16};
134constexpr fltSemantics APFloatBase::semBFloat = {127, -126, 8, 16};
135constexpr fltSemantics APFloatBase::semIEEEsingle = {127, -126, 24, 32};
136constexpr fltSemantics APFloatBase::semIEEEdouble = {1023, -1022, 53, 64};
137constexpr fltSemantics APFloatBase::semIEEEquad = {16383, -16382, 113, 128};
138constexpr fltSemantics APFloatBase::semFloat8E5M2 = {15, -14, 3, 8};
139constexpr fltSemantics APFloatBase::semFloat8E5M2FNUZ = {
141constexpr fltSemantics APFloatBase::semFloat8E4M3 = {7, -6, 4, 8};
142constexpr fltSemantics APFloatBase::semFloat8E4M3FN = {
144constexpr fltSemantics APFloatBase::semFloat8E4M3FNUZ = {
146constexpr fltSemantics APFloatBase::semFloat8E4M3B11FNUZ = {
148constexpr fltSemantics APFloatBase::semFloat8E3M4 = {3, -2, 5, 8};
149constexpr fltSemantics APFloatBase::semFloatTF32 = {127, -126, 11, 19};
150constexpr fltSemantics APFloatBase::semFloat8E8M0FNU = {
151 127,
152 -127,
153 1,
154 8,
157 false,
158 false,
159 false};
160
161constexpr fltSemantics APFloatBase::semFloat6E3M2FN = {
163constexpr fltSemantics APFloatBase::semFloat6E2M3FN = {
165constexpr fltSemantics APFloatBase::semFloat4E2M1FN = {
167constexpr fltSemantics APFloatBase::semX87DoubleExtended = {16383, -16382, 64,
168 80};
169constexpr fltSemantics APFloatBase::semBogus = {0, 0, 0, 0};
170constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {-1, 0, 0, 128};
171constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = {
172 1023, -1022 + 53, 53 + 53, 128};
173
175 switch (S) {
216 }
218}
219
265
268 return A.maxExponent <= B.maxExponent && A.minExponent >= B.minExponent &&
269 A.precision <= B.precision;
270}
271
272
273
274
275
276
277
278
279
280
281
282
287 2 +
289
306
307
309
311 ++MinBitWidth;
312 return MinBitWidth;
313}
314
318
322
326
330
335
339
342
343 if (Src.maxExponent >= Dst.maxExponent || Src.minExponent <= Dst.minExponent)
344 return false;
345
346
347
348
349
350
351 return Dst.precision >= Src.precision;
352}
353
357
362
367
378
379
380
384
389
390
391static inline unsigned int
393{
394 return c - '0';
395}
396
397
398
399
400
401
404 bool isNegative;
405 unsigned int absExponent;
406 const unsigned int overlargeExponent = 24000;
408
409
410 if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) {
411 return 0;
412 }
413
414 isNegative = (*p == '-');
415 if (*p == '-' || *p == '+') {
416 p++;
417 if (p == end)
418 return createError("Exponent has no digits");
419 }
420
422 if (absExponent >= 10U)
423 return createError("Invalid character in exponent");
424
425 for (; p != end; ++p) {
426 unsigned int value;
427
429 if (value >= 10U)
430 return createError("Invalid character in exponent");
431
432 absExponent = absExponent * 10U + value;
433 if (absExponent >= overlargeExponent) {
434 absExponent = overlargeExponent;
435 break;
436 }
437 }
438
439 if (isNegative)
440 return -(int) absExponent;
441 else
442 return (int) absExponent;
443}
444
445
446
449 int exponentAdjustment) {
450 int unsignedExponent;
451 bool negative, overflow;
452 int exponent = 0;
453
454 if (p == end)
455 return createError("Exponent has no digits");
456
457 negative = *p == '-';
458 if (*p == '-' || *p == '+') {
459 p++;
460 if (p == end)
461 return createError("Exponent has no digits");
462 }
463
464 unsignedExponent = 0;
465 overflow = false;
466 for (; p != end; ++p) {
467 unsigned int value;
468
470 if (value >= 10U)
471 return createError("Invalid character in exponent");
472
473 unsignedExponent = unsignedExponent * 10 + value;
474 if (unsignedExponent > 32767) {
475 overflow = true;
476 break;
477 }
478 }
479
480 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
481 overflow = true;
482
483 if (!overflow) {
484 exponent = unsignedExponent;
485 if (negative)
486 exponent = -exponent;
487 exponent += exponentAdjustment;
488 if (exponent > 32767 || exponent < -32768)
489 overflow = true;
490 }
491
492 if (overflow)
493 exponent = negative ? -32768: 32767;
494
495 return exponent;
496}
497
502 *dot = end;
503 while (p != end && *p == '0')
504 p++;
505
506 if (p != end && *p == '.') {
507 *dot = p++;
508
509 if (end - begin == 1)
510 return createError("Significand has no digits");
511
512 while (p != end && *p == '0')
513 p++;
514 }
515
516 return p;
517}
518
519
520
521
522
523
524
525
526
527
528
529
530
531
538
542
544 if (!PtrOrErr)
545 return PtrOrErr.takeError();
547
548 D->firstSigDigit = p;
549 D->exponent = 0;
550 D->normalizedExponent = 0;
551
552 for (; p != end; ++p) {
553 if (*p == '.') {
554 if (dot != end)
555 return createError("String contains multiple dots");
556 dot = p++;
557 if (p == end)
558 break;
559 }
561 break;
562 }
563
564 if (p != end) {
565 if (*p != 'e' && *p != 'E')
566 return createError("Invalid character in significand");
567 if (p == begin)
568 return createError("Significand has no digits");
569 if (dot != end && p - begin == 1)
570 return createError("Significand has no digits");
571
572
574 if (!ExpOrErr)
575 return ExpOrErr.takeError();
576 D->exponent = *ExpOrErr;
577
578
579 if (dot == end)
580 dot = p;
581 }
582
583
584 if (p != D->firstSigDigit) {
585
586 if (p != begin) {
587 do
588 do
589 p--;
590 while (p != begin && *p == '0');
591 while (p != begin && *p == '.');
592 }
593
594
596 D->normalizedExponent = (D->exponent +
598 - (dot > D->firstSigDigit && dot < p)));
599 }
600
601 D->lastSigDigit = p;
603}
604
605
606
607
610 unsigned int digitValue) {
611 unsigned int hexDigit;
612
613
614
615 if (digitValue > 8)
617 else if (digitValue < 8 && digitValue > 0)
619
620
621 while (p != end && (*p == '0' || *p == '.'))
622 p++;
623
624 if (p == end)
625 return createError("Invalid trailing hexadecimal fraction!");
626
628
629
630
631 if (hexDigit == UINT_MAX)
633 else
635}
636
637
638
641 unsigned int partCount,
642 unsigned int bits)
643{
644 unsigned int lsb;
645
647
648
649 if (bits <= lsb)
651 if (bits == lsb + 1)
656
658}
659
660
663{
665
667
669
670 return lost_fraction;
671}
672
673
677{
683 }
684
685 return moreSignificant;
686}
687
688
689
690
691
692
693
694
695static unsigned int
696HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
697{
698 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
699
700 if (HUerr1 + HUerr2 == 0)
701 return inexactMultiply * 2;
702 else
703 return inexactMultiply + 2 * (HUerr1 + HUerr2);
704}
705
706
707
708
711 bool isNearest) {
712 unsigned int count, partBits;
714
716
717 bits--;
720
722
723 if (isNearest)
725 else
726 boundary = 0;
727
728 if (count == 0) {
729 if (part - boundary <= boundary - part)
730 return part - boundary;
731 else
732 return boundary - part;
733 }
734
735 if (part == boundary) {
737 if (parts[count])
739
740 return parts[0];
741 } else if (part == boundary - 1) {
743 if (~parts[count])
745
746 return -parts[0];
747 }
748
750}
751
752
753
754static unsigned int
756 static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 };
758 pow5s[0] = 78125 * 5;
759
760 unsigned int partsCount = 1;
762 unsigned int result;
764
765 p1 = dst;
766 p2 = scratch;
767
768 *p1 = firstEightPowers[power & 7];
769 power >>= 3;
770
771 result = 1;
772 pow5 = pow5s;
773
774 for (unsigned int n = 0; power; power >>= 1, n++) {
775
776 if (n != 0) {
778 partsCount, partsCount);
779 partsCount *= 2;
780 if (pow5[partsCount - 1] == 0)
781 partsCount--;
782 }
783
784 if (power & 1) {
786
788 result += partsCount;
789 if (p2[result - 1] == 0)
790 result--;
791
792
793
794 tmp = p1;
795 p1 = p2;
796 p2 = tmp;
797 }
798
799 pow5 += partsCount;
800 }
801
802 if (p1 != dst)
804
805 return result;
806}
807
808
809
814static const char NaNL[] = "nan";
815static const char NaNU[] = "NAN";
816
817
818
819
820static unsigned int
822 const char *hexDigitChars)
823{
824 unsigned int result = count;
825
827
829 while (count--) {
830 dst[count] = hexDigitChars[part & 0xf];
831 part >>= 4;
832 }
833
834 return result;
835}
836
837
838static char *
840{
841 char buff[40], *p;
842
843 p = buff;
844 do
845 *p++ = '0' + n % 10;
846 while (n /= 10);
847
848 do
849 *dst++ = *--p;
850 while (p != buff);
851
852 return dst;
853}
854
855
856static char *
858{
859 if (value < 0) {
860 *dst++ = '-';
862 } else {
864 }
865
866 return dst;
867}
868
869
870
871
874 switch (X.getCategory()) {
882 break;
883 }
884 if (X.isDenormal() || X.isSmallestNormalized())
887 if (X.getExactLog2() != INT_MIN)
888 Exp -= 1;
891}
892
894
895void IEEEFloat::initialize(const fltSemantics *ourSemantics) {
896 unsigned int count;
897
898 semantics = ourSemantics;
899 count = partCount();
902}
903
904void IEEEFloat::freeSignificand() {
906 delete [] significand.parts;
907}
908
909void IEEEFloat::assign(const IEEEFloat &rhs) {
910 assert(semantics == rhs.semantics);
911
912 sign = rhs.sign;
913 category = rhs.category;
914 exponent = rhs.exponent;
916 copySignificand(rhs);
917}
918
919void IEEEFloat::copySignificand(const IEEEFloat &rhs) {
921 assert(rhs.partCount() >= partCount());
922
923 APInt::tcAssign(significandParts(), rhs.significandParts(),
924 partCount());
925}
926
927
928
929
932 llvm_unreachable("This floating point format does not support NaN");
933
934 if (Negative && !semantics->hasSignedRepr)
936 "This floating point format does not support signed values");
937
938 category = fcNaN;
939 sign = Negative;
940 exponent = exponentNaN();
941
942 integerPart *significand = significandParts();
943 unsigned numParts = partCount();
944
945 APInt fill_storage;
947
948
949 SNaN = false;
951 sign = true;
952 fill_storage = APInt::getZero(semantics->precision - 1);
953 } else {
955 }
956 fill = &fill_storage;
957 }
958
959
960 if ( || fill->getNumWords() < numParts)
964 std::min(fill->getNumWords(), numParts));
965
966
967 unsigned bitsToPreserve = semantics->precision - 1;
968 unsigned part = bitsToPreserve / 64;
969 bitsToPreserve %= 64;
970 significand[part] &= ((1ULL << bitsToPreserve) - 1);
971 for (part++; part != numParts; ++part)
972 significand[part] = 0;
973 }
974
975 unsigned QNaNBit =
976 (semantics->precision >= 2) ? (semantics->precision - 2) : 0;
977
978 if (SNaN) {
979
981
982
983
984
988
989
990 } else {
991
993 }
994
995
996
997
998 if (semantics == &APFloatBase::semX87DoubleExtended)
1000}
1001
1003 if (this != &rhs) {
1004 if (semantics != rhs.semantics) {
1005 freeSignificand();
1006 initialize(rhs.semantics);
1007 }
1008 assign(rhs);
1009 }
1010
1011 return *this;
1012}
1013
1015 freeSignificand();
1016
1017 semantics = rhs.semantics;
1018 significand = rhs.significand;
1019 exponent = rhs.exponent;
1020 category = rhs.category;
1021 sign = rhs.sign;
1022
1023 rhs.semantics = &APFloatBase::semBogus;
1024 return *this;
1025}
1026
1028 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
1030 semantics->precision - 1) == 0);
1031}
1032
1034
1035
1036
1037 return isFiniteNonZero() && exponent == semantics->minExponent &&
1038 significandMSB() == 0;
1039}
1040
1042 return getCategory() == fcNormal && exponent == semantics->minExponent &&
1043 isSignificandAllZerosExceptMSB();
1044}
1045
1046unsigned int IEEEFloat::getNumHighBits() const {
1049
1050
1051
1052
1053 const unsigned int NumHighBits = (semantics->precision > 1)
1054 ? (Bits - semantics->precision + 1)
1055 : (Bits - semantics->precision);
1056 return NumHighBits;
1057}
1058
1059bool IEEEFloat::isSignificandAllOnes() const {
1060
1061
1062 const integerPart *Parts = significandParts();
1064 for (unsigned i = 0; i < PartCount - 1; i++)
1065 if (~Parts[i])
1066 return false;
1067
1068
1069 const unsigned NumHighBits = getNumHighBits();
1070 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
1071 "Can not have more high bits to fill than integerPartWidth");
1074 if ((semantics->precision <= 1) || (~(Parts[PartCount - 1] | HighBitFill)))
1075 return false;
1076
1077 return true;
1078}
1079
1080bool IEEEFloat::isSignificandAllOnesExceptLSB() const {
1081
1082
1083 const integerPart *Parts = significandParts();
1084
1085 if (Parts[0] & 1)
1086 return false;
1087
1089 for (unsigned i = 0; i < PartCount - 1; i++) {
1090 if (~Parts[i] & ~unsigned{!i})
1091 return false;
1092 }
1093
1094
1095 const unsigned NumHighBits = getNumHighBits();
1096 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
1097 "Can not have more high bits to fill than integerPartWidth");
1098 const integerPart HighBitFill = ~integerPart(0)
1100 if (~(Parts[PartCount - 1] | HighBitFill | 0x1))
1101 return false;
1102
1103 return true;
1104}
1105
1106bool IEEEFloat::isSignificandAllZeros() const {
1107
1108
1109 const integerPart *Parts = significandParts();
1110 const unsigned PartCount = partCountForBits(semantics->precision);
1111
1112 for (unsigned i = 0; i < PartCount - 1; i++)
1113 if (Parts[i])
1114 return false;
1115
1116
1117 const unsigned NumHighBits = getNumHighBits();
1119 "clear than integerPartWidth");
1120 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
1121
1122 if ((semantics->precision > 1) && (Parts[PartCount - 1] & HighBitMask))
1123 return false;
1124
1125 return true;
1126}
1127
1128bool IEEEFloat::isSignificandAllZerosExceptMSB() const {
1129 const integerPart *Parts = significandParts();
1130 const unsigned PartCount = partCountForBits(semantics->precision);
1131
1132 for (unsigned i = 0; i < PartCount - 1; i++) {
1133 if (Parts[i])
1134 return false;
1135 }
1136
1137 const unsigned NumHighBits = getNumHighBits();
1140 return ((semantics->precision <= 1) || (Parts[PartCount - 1] == MSBMask));
1141}
1142
1144 bool IsMaxExp = isFiniteNonZero() && exponent == semantics->maxExponent;
1147
1148
1149
1151 ? isSignificandAllOnesExceptLSB()
1152 : IsMaxExp;
1153 } else {
1154
1155
1156 return IsMaxExp && isSignificandAllOnes();
1157 }
1158}
1159
1161
1162 if (()) return false;
1166}
1167
1169 if (this == &rhs)
1170 return true;
1171 if (semantics != rhs.semantics ||
1172 category != rhs.category ||
1173 sign != rhs.sign)
1174 return false;
1176 return true;
1177
1179 return false;
1180
1181 return std::equal(significandParts(), significandParts() + partCount(),
1182 rhs.significandParts());
1183}
1184
1186 initialize(&ourSemantics);
1187 sign = 0;
1189 zeroSignificand();
1190 exponent = ourSemantics.precision - 1;
1191 significandParts()[0] = value;
1193}
1194
1196 initialize(&ourSemantics);
1197
1198
1199
1200
1201
1202
1204}
1205
1206
1207
1210
1212 initialize(rhs.semantics);
1213 assign(rhs);
1214}
1215
1217 *this = std::move(rhs);
1218}
1219
1221
1222unsigned int IEEEFloat::partCount() const {
1224}
1225
1227 return const_cast<IEEEFloat *>(this)->significandParts();
1228}
1229
1231 if (partCount() > 1)
1232 return significand.parts;
1233 else
1234 return &significand.part;
1235}
1236
1237void IEEEFloat::zeroSignificand() {
1238 APInt::tcSet(significandParts(), 0, partCount());
1239}
1240
1241
1242void IEEEFloat::incrementSignificand() {
1244
1246
1247
1249 (void)carry;
1250}
1251
1252
1255
1256 parts = significandParts();
1257
1258 assert(semantics == rhs.semantics);
1259 assert(exponent == rhs.exponent);
1260
1261 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
1262}
1263
1264
1265
1269
1270 parts = significandParts();
1271
1272 assert(semantics == rhs.semantics);
1273 assert(exponent == rhs.exponent);
1274
1276 partCount());
1277}
1278
1279
1280
1281
1284 bool ignoreAddend) {
1285 unsigned int omsb;
1286 unsigned int partsCount, newPartsCount, precision;
1291 bool ignored;
1292
1293 assert(semantics == rhs.semantics);
1294
1295 precision = semantics->precision;
1296
1297
1298
1300
1301 if (newPartsCount > 4)
1302 fullSignificand = new integerPart[newPartsCount];
1303 else
1304 fullSignificand = scratch;
1305
1306 lhsSignificand = significandParts();
1307 partsCount = partCount();
1308
1310 rhs.significandParts(), partsCount, partsCount);
1311
1313 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1314 exponent += rhs.exponent;
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 exponent += 2;
1327
1328 if (!ignoreAddend && addend.isNonZero()) {
1329
1330
1331
1332 Significand savedSignificand = significand;
1333 const fltSemantics *savedSemantics = semantics;
1334 fltSemantics extendedSemantics;
1336 unsigned int extendedPrecision;
1337
1338
1339 extendedPrecision = 2 * precision + 1;
1340 if (omsb != extendedPrecision - 1) {
1341 assert(extendedPrecision > omsb);
1343 (extendedPrecision - 1) - omsb);
1344 exponent -= (extendedPrecision - 1) - omsb;
1345 }
1346
1347
1348 extendedSemantics = *semantics;
1349 extendedSemantics.precision = extendedPrecision;
1350
1351 if (newPartsCount == 1)
1352 significand.part = fullSignificand[0];
1353 else
1354 significand.parts = fullSignificand;
1355 semantics = &extendedSemantics;
1356
1357
1358
1359
1360 IEEEFloat extendedAddend(addend);
1362 &ignored);
1364 (void)status;
1365
1366
1367
1368
1369 lost_fraction = extendedAddend.shiftSignificandRight(1);
1371 "Lost precision while shifting addend for fused-multiply-add.");
1372
1373 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
1374
1375
1376 if (newPartsCount == 1)
1377 fullSignificand[0] = significand.part;
1378 significand = savedSignificand;
1379 semantics = savedSemantics;
1380
1381 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1382 }
1383
1384
1385
1386
1387
1388 exponent -= precision + 1;
1389
1390
1391
1392
1393
1394
1395
1396
1397 if (omsb > precision) {
1398 unsigned int bits, significantParts;
1400
1401 bits = omsb - precision;
1403 lf = shiftRight(fullSignificand, significantParts, bits);
1405 exponent += bits;
1406 }
1407
1408 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1409
1410 if (newPartsCount > 4)
1411 delete [] fullSignificand;
1412
1413 return lost_fraction;
1414}
1415
1417
1418
1419
1420
1421
1422 return multiplySignificand(rhs, IEEEFloat(*semantics), !semantics->hasZero);
1423}
1424
1425
1427 unsigned int bit, i, partsCount;
1429 integerPart *lhsSignificand, *dividend, *divisor;
1432
1433 assert(semantics == rhs.semantics);
1434
1435 lhsSignificand = significandParts();
1436 rhsSignificand = rhs.significandParts();
1437 partsCount = partCount();
1438
1439 if (partsCount > 2)
1440 dividend = new integerPart[partsCount * 2];
1441 else
1442 dividend = scratch;
1443
1444 divisor = dividend + partsCount;
1445
1446
1447 for (i = 0; i < partsCount; i++) {
1448 dividend[i] = lhsSignificand[i];
1449 divisor[i] = rhsSignificand[i];
1450 lhsSignificand[i] = 0;
1451 }
1452
1453 exponent -= rhs.exponent;
1454
1455 unsigned int precision = semantics->precision;
1456
1457
1458 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
1459 if (bit) {
1460 exponent += bit;
1462 }
1463
1464
1465 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
1466 if (bit) {
1467 exponent -= bit;
1469 }
1470
1471
1472
1473
1475 exponent--;
1478 }
1479
1480
1481 for (bit = precision; bit; bit -= 1) {
1485 }
1486
1488 }
1489
1490
1492
1493 if (cmp > 0)
1495 else if (cmp == 0)
1499 else
1501
1502 if (partsCount > 2)
1503 delete [] dividend;
1504
1505 return lost_fraction;
1506}
1507
1508unsigned int IEEEFloat::significandMSB() const {
1509 return APInt::tcMSB(significandParts(), partCount());
1510}
1511
1512unsigned int IEEEFloat::significandLSB() const {
1513 return APInt::tcLSB(significandParts(), partCount());
1514}
1515
1516
1517lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) {
1518
1520
1521 exponent += bits;
1522
1523 return shiftRight(significandParts(), partCount(), bits);
1524}
1525
1526
1527void IEEEFloat::shiftSignificandLeft(unsigned int bits) {
1528 assert(bits < semantics->precision ||
1529 (semantics->precision == 1 && bits <= 1));
1530
1531 if (bits) {
1532 unsigned int partsCount = partCount();
1533
1535 exponent -= bits;
1536
1538 }
1539}
1540
1543
1544 assert(semantics == rhs.semantics);
1547
1548 compare = exponent - rhs.exponent;
1549
1550
1551
1554 partCount());
1555
1560 else
1562}
1563
1564
1565
1567 unsigned bits) {
1568 unsigned i = 0;
1572 }
1573
1574 if (bits)
1576
1577 while (i < parts)
1578 dst[i++] = 0;
1579}
1580
1581
1582
1585
1592 else
1595 }
1596 }
1597
1598
1600 exponent = semantics->maxExponent;
1602 semantics->precision);
1606
1608}
1609
1610
1611
1612
1613
1614
1615bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode,
1617 unsigned int bit) const {
1618
1620
1621
1623
1624 switch (rounding_mode) {
1627
1630 return true;
1631
1632
1635
1636 return false;
1637
1639 return false;
1640
1642 return !sign;
1643
1645 return sign;
1646
1647 default:
1648 break;
1649 }
1651}
1652
1655 unsigned int omsb;
1656 int exponentChange;
1657
1659 return opOK;
1660
1661
1662 omsb = significandMSB() + 1;
1663
1664
1666
1667
1668
1669 exponentChange = omsb - semantics->precision;
1670
1671
1672
1673 if (exponent + exponentChange > semantics->maxExponent)
1674 return handleOverflow(rounding_mode);
1675
1676
1677
1678 if (exponent + exponentChange < semantics->minExponent)
1679 exponentChange = semantics->minExponent - exponent;
1680
1681
1682 if (exponentChange < 0) {
1684
1685 shiftSignificandLeft(-exponentChange);
1686
1687 return opOK;
1688 }
1689
1690 if (exponentChange > 0) {
1692
1693
1694 lf = shiftSignificandRight(exponentChange);
1695
1697
1698
1699 if (omsb > (unsigned) exponentChange)
1700 omsb -= exponentChange;
1701 else
1702 omsb = 0;
1703 }
1704 }
1705
1706
1707
1710 exponent == semantics->maxExponent && isSignificandAllOnes())
1711 return handleOverflow(rounding_mode);
1712
1713
1714
1715
1716
1717
1719
1720 if (omsb == 0) {
1723 sign = false;
1724 if (!semantics->hasZero)
1726 }
1727
1728 return opOK;
1729 }
1730
1731
1732 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1733 if (omsb == 0)
1734 exponent = semantics->minExponent;
1735
1736 incrementSignificand();
1737 omsb = significandMSB() + 1;
1738
1739
1740 if (omsb == (unsigned) semantics->precision + 1) {
1741
1742
1743
1744 if (exponent == semantics->maxExponent)
1745
1746
1747
1748
1750
1751 shiftSignificandRight(1);
1752
1754 }
1755
1756
1757
1760 exponent == semantics->maxExponent && isSignificandAllOnes())
1761 return handleOverflow(rounding_mode);
1762 }
1763
1764
1765
1766 if (omsb == semantics->precision)
1768
1769
1770 assert(omsb < semantics->precision);
1771
1772
1773 if (omsb == 0) {
1776 sign = false;
1777
1778
1779
1780 if (!semantics->hasZero)
1782 }
1783
1784
1786}
1787
1789 bool subtract) {
1791 default:
1793
1797 assign(rhs);
1798 [[fallthrough]];
1806 }
1808
1812 return opOK;
1813
1818 return opOK;
1819
1821 assign(rhs);
1823 return opOK;
1824
1826
1827 return opOK;
1828
1830
1831
1832 if (((sign ^ rhs.sign)!=0) != subtract) {
1835 }
1836
1837 return opOK;
1838
1841 }
1842}
1843
1844
1846 bool subtract) {
1849 int bits;
1850
1851
1852
1853 subtract ^= static_cast<bool>(sign ^ rhs.sign);
1854
1855
1856 bits = exponent - rhs.exponent;
1857
1858
1860 if ((bits < 0) && !semantics->hasSignedRepr)
1862 "This floating point format does not support signed values");
1863
1865 bool lost_fraction_is_from_rhs = false;
1866
1867 if (bits == 0)
1869 else if (bits > 0) {
1870 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1871 lost_fraction_is_from_rhs = true;
1872 shiftSignificandLeft(1);
1873 } else {
1874 lost_fraction = shiftSignificandRight(-bits - 1);
1875 temp_rhs.shiftSignificandLeft(1);
1876 }
1877
1878
1881 bool borrow =
1882 lost_fraction != lfExactlyZero && !lost_fraction_is_from_rhs;
1883 if (borrow) {
1884
1885
1890 }
1891 carry = temp_rhs.subtractSignificand(*this, borrow);
1892 copySignificand(temp_rhs);
1893 sign = !sign;
1895 bool borrow = lost_fraction != lfExactlyZero && lost_fraction_is_from_rhs;
1896 if (borrow) {
1897
1898
1903 }
1904 carry = subtractSignificand(temp_rhs, borrow);
1905 } else {
1906 zeroSignificand();
1907 if (lost_fraction != lfExactlyZero && lost_fraction_is_from_rhs) {
1908
1909 sign = !sign;
1910 }
1911 }
1912
1913
1914
1916 (void)carry;
1917 } else {
1918 if (bits > 0) {
1920
1921 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1922 carry = addSignificand(temp_rhs);
1923 } else {
1924 lost_fraction = shiftSignificandRight(-bits);
1925 carry = addSignificand(rhs);
1926 }
1927
1928
1930 (void)carry;
1931 }
1932
1933 return lost_fraction;
1934}
1935
1938 default:
1940
1944 assign(rhs);
1945 sign = false;
1946 [[fallthrough]];
1951 sign ^= rhs.sign;
1955 }
1957
1962 return opOK;
1963
1968 return opOK;
1969
1974
1976 return opOK;
1977 }
1978}
1979
1982 default:
1984
1988 assign(rhs);
1989 sign = false;
1990 [[fallthrough]];
1995 sign ^= rhs.sign;
1999 }
2001
2006 return opOK;
2007
2010 return opOK;
2011
2015 else
2018
2023
2025 return opOK;
2026 }
2027}
2028
2031 default:
2033
2037 assign(rhs);
2038 [[fallthrough]];
2046 }
2048
2052 return opOK;
2053
2061
2063 return opOK;
2064 }
2065}
2066
2069 default:
2071
2075 assign(rhs);
2076 [[fallthrough]];
2084 }
2086
2090 return opOK;
2091
2099
2101 return opDivByZero;
2102 }
2103}
2104
2105
2107
2108
2111 return;
2112
2113 sign = !sign;
2114}
2115
2116
2119 bool subtract) {
2121
2122 fs = addOrSubtractSpecials(rhs, subtract);
2123
2124
2127
2128 lost_fraction = addOrSubtractSignificand(rhs, subtract);
2129 fs = normalize(rounding_mode, lost_fraction);
2130
2131
2133 }
2134
2135
2136
2137
2138 if (category == fcZero) {
2139 if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
2141
2143 sign = false;
2144 }
2145
2146 return fs;
2147}
2148
2149
2152 return addOrSubtract(rhs, rounding_mode, false);
2153}
2154
2155
2158 return addOrSubtract(rhs, rounding_mode, true);
2159}
2160
2161
2165
2166 sign ^= rhs.sign;
2167 fs = multiplySpecials(rhs);
2168
2170 sign = false;
2172 lostFraction lost_fraction = multiplySignificand(rhs);
2173 fs = normalize(rounding_mode, lost_fraction);
2176 }
2177
2178 return fs;
2179}
2180
2181
2185
2186 sign ^= rhs.sign;
2187 fs = divideSpecials(rhs);
2188
2190 sign = false;
2192 lostFraction lost_fraction = divideSignificand(rhs);
2193 fs = normalize(rounding_mode, lost_fraction);
2196 }
2197
2198 return fs;
2199}
2200
2201
2204 unsigned int origSign = sign;
2205
2206
2207 fs = remainderSpecials(rhs);
2209 return fs;
2210
2212
2213
2214
2215
2216
2221 }
2222
2223
2225 P.sign = false;
2226 sign = false;
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262 bool losesInfo;
2263 fltSemantics extendedSemantics = *semantics;
2266 extendedSemantics.precision += 2;
2267
2274
2275
2276
2279
2283
2284
2285
2286
2291
2296 }
2297 }
2298
2300 sign = origSign;
2302
2303 sign = false;
2304 }
2305
2306 else
2307 sign ^= origSign;
2308 return fs;
2309}
2310
2311
2314 fs = modSpecials(rhs);
2315 unsigned int origSign = sign;
2316
2319 int Exp = ilogb(*this) - ilogb(rhs);
2321
2322
2325 V.sign = sign;
2326
2328
2329
2330
2331
2332
2333
2334
2335
2336 if (!semantics->hasZero && this->isSmallest())
2337 break;
2338
2340 }
2342 sign = origSign;
2344 sign = false;
2345 }
2346 return fs;
2347}
2348
2349
2354
2355
2356 sign ^= multiplicand.sign;
2357
2358
2359
2364
2365 lost_fraction = multiplySignificand(multiplicand, addend);
2366 fs = normalize(rounding_mode, lost_fraction);
2369
2370
2371
2372
2376 sign = false;
2377 }
2378 } else {
2379 fs = multiplySpecials(multiplicand);
2380
2381
2382
2383
2384
2385
2386
2387
2389 fs = addOrSubtract(addend, rounding_mode, false);
2390 }
2391
2392 return fs;
2393}
2394
2395
2398
2400
2401
2402
2403
2404
2405
2406
2407 return opOK;
2408
2411
2412
2413
2414
2416
2417
2418
2419
2420
2422 } else {
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432 return opOK;
2433 }
2434 }
2435
2437
2438
2439
2440
2441 return opOK;
2442 }
2443
2444
2445
2446
2448 return opOK;
2449
2450
2451
2452
2453
2454
2455
2457 1);
2459 IEEEFloat MagicConstant(*semantics);
2463 MagicConstant.sign = sign;
2464
2465
2466
2468
2469 fs = add(MagicConstant, rounding_mode);
2470
2471
2472
2473 subtract(MagicConstant, rounding_mode);
2474
2475
2478
2479 return fs;
2480}
2481
2482
2485
2486 assert(semantics == rhs.semantics);
2487
2489 default:
2491
2500
2504 if (sign)
2506 else
2508
2512 if (rhs.sign)
2514 else
2516
2518 if (sign == rhs.sign)
2520 else if (sign)
2522 else
2524
2527
2529 break;
2530 }
2531
2532
2533 if (sign != rhs.sign) {
2534 if (sign)
2536 else
2538 } else {
2539
2541
2542 if (sign) {
2547 }
2548 }
2549
2550 return result;
2551}
2552
2553
2554
2555
2556
2557
2558
2559
2562 bool *losesInfo) {
2564 unsigned int newPartCount, oldPartCount;
2566 int shift;
2567 const fltSemantics &fromSemantics = *semantics;
2569
2572 oldPartCount = partCount();
2574
2575 bool X86SpecialNan = false;
2576 if (&fromSemantics == &APFloatBase::semX87DoubleExtended &&
2577 &toSemantics != &APFloatBase::semX87DoubleExtended && category == fcNaN &&
2578 (!(*significandParts() & 0x8000000000000000ULL) ||
2579 !(*significandParts() & 0x4000000000000000ULL))) {
2580
2581
2582 X86SpecialNan = true;
2583 }
2584
2585
2586
2587
2588
2589
2590
2591
2593 int omsb = significandMSB() + 1;
2594 int exponentChange = omsb - fromSemantics.precision;
2595 if (exponent + exponentChange < toSemantics.minExponent)
2596 exponentChange = toSemantics.minExponent - exponent;
2597 exponentChange = std::max(exponentChange, shift);
2598 if (exponentChange < 0) {
2599 shift -= exponentChange;
2600 exponent += exponentChange;
2601 } else if (omsb <= -shift) {
2602 exponentChange = omsb + shift - 1;
2603 shift -= exponentChange;
2604 exponent += exponentChange;
2605 }
2606 }
2607
2608
2610 (category == fcNaN && semantics->nonFiniteBehavior !=
2613
2614
2615 if (newPartCount > oldPartCount) {
2616
2618 newParts = new integerPart[newPartCount];
2621 APInt::tcAssign(newParts, significandParts(), oldPartCount);
2622 freeSignificand();
2623 significand.parts = newParts;
2624 } else if (newPartCount == 1 && oldPartCount != 1) {
2625
2628 newPart = significandParts()[0];
2629 freeSignificand();
2630 significand.part = newPart;
2631 }
2632
2633
2634 semantics = &toSemantics;
2635
2636
2637
2640
2643 *losesInfo = (fs != opOK);
2644 } else if (category == fcNaN) {
2646 *losesInfo =
2650 }
2651
2652
2653
2657
2659
2660
2661
2662 if (!X86SpecialNan && semantics == &APFloatBase::semX87DoubleExtended)
2663 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2664
2665
2666
2667
2668 if (is_signaling) {
2671 } else {
2673 }
2674 } else if (category == fcInfinity &&
2677 *losesInfo = true;
2679 } else if (category == fcZero &&
2681
2682 *losesInfo =
2685
2686 sign = false;
2687 } else {
2688 *losesInfo = false;
2690 }
2691
2692 if (category == fcZero && !semantics->hasZero)
2694 return fs;
2695}
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2709 roundingMode rounding_mode, bool *isExact) const {
2712 unsigned int dstPartsCount, truncatedBits;
2713
2714 *isExact = false;
2715
2716
2719
2721 assert(dstPartsCount <= parts.size() && "Integer too big");
2722
2723 if (category == fcZero) {
2725
2726 *isExact = !sign;
2727 return opOK;
2728 }
2729
2730 src = significandParts();
2731
2732
2733
2734 if (exponent < 0) {
2735
2737
2738
2739 truncatedBits = semantics->precision -1U - exponent;
2740 } else {
2741
2742
2743 unsigned int bits = exponent + 1U;
2744
2745
2746 if (bits > width)
2748
2749 if (bits < semantics->precision) {
2750
2751 truncatedBits = semantics->precision - bits;
2753 } else {
2754
2756 0);
2758 bits - semantics->precision);
2759 truncatedBits = 0;
2760 }
2761 }
2762
2763
2764
2765 if (truncatedBits) {
2767 truncatedBits);
2769 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2772 }
2773 } else {
2775 }
2776
2777
2778 unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1;
2779
2780 if (sign) {
2782
2783 if (omsb != 0)
2785 } else {
2786
2787
2788
2789 if (omsb == width &&
2792
2793
2794 if (omsb > width)
2796 }
2797
2799 } else {
2800 if (omsb >= width + )
2802 }
2803
2805 *isExact = true;
2806 return opOK;
2807 }
2809}
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2822 unsigned int width, bool isSigned,
2823 roundingMode rounding_mode, bool *isExact) const {
2825
2826 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2827 isExact);
2828
2830 unsigned int bits, dstPartsCount;
2831
2833 assert(dstPartsCount <= parts.size() && "Integer too big");
2834
2835 if (category == fcNaN)
2836 bits = 0;
2837 else if (sign)
2839 else
2841
2845 }
2846
2847 return fs;
2848}
2849
2850
2851
2852
2855 unsigned int omsb, precision, dstCount;
2858
2861 dst = significandParts();
2862 dstCount = partCount();
2863 precision = semantics->precision;
2864
2865
2866
2867 if (precision <= omsb) {
2868 exponent = omsb - 1;
2870 omsb - precision);
2871 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2872 } else {
2873 exponent = precision - 1;
2876 }
2877
2878 return normalize(rounding_mode, lost_fraction);
2879}
2880
2883 unsigned int partCount = Val.getNumWords();
2884 APInt api = Val;
2885
2886 sign = false;
2888 sign = true;
2889 api = -api;
2890 }
2891
2892 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2893}
2894
2896IEEEFloat::convertFromHexadecimalString(StringRef s,
2899
2901 zeroSignificand();
2902 exponent = 0;
2903
2904 integerPart *significand = significandParts();
2905 unsigned partsCount = partCount();
2907 bool computedTrailingFraction = false;
2908
2909
2914 if (!PtrOrErr)
2915 return PtrOrErr.takeError();
2918
2919 while (p != end) {
2921
2922 if (*p == '.') {
2923 if (dot != end)
2924 return createError("String contains multiple dots");
2925 dot = p++;
2926 continue;
2927 }
2928
2930 if (hex_value == UINT_MAX)
2931 break;
2932
2933 p++;
2934
2935
2936 if (bitPos) {
2937 bitPos -= 4;
2940 } else if (!computedTrailingFraction) {
2942 if (!FractOrErr)
2943 return FractOrErr.takeError();
2944 lost_fraction = *FractOrErr;
2945 computedTrailingFraction = true;
2946 }
2947 }
2948
2949
2950 if (p == end)
2951 return createError("Hex strings require an exponent");
2952 if (*p != 'p' && *p != 'P')
2953 return createError("Invalid character in significand");
2954 if (p == begin)
2955 return createError("Significand has no digits");
2956 if (dot != end && p - begin == 1)
2957 return createError("Significand has no digits");
2958
2959
2960 if (p != firstSignificantDigit) {
2961 int expAdjustment;
2962
2963
2964 if (dot == end)
2966
2967
2968
2969 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
2970 if (expAdjustment < 0)
2971 expAdjustment++;
2972 expAdjustment = expAdjustment * 4 - 1;
2973
2974
2975
2976 expAdjustment += semantics->precision;
2978
2979
2980 auto ExpOrErr = totalExponent(p + 1, end, expAdjustment);
2981 if (!ExpOrErr)
2982 return ExpOrErr.takeError();
2983 exponent = *ExpOrErr;
2984 }
2985
2986 return normalize(rounding_mode, lost_fraction);
2987}
2988
2990IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2991 unsigned sigPartCount, int exp,
2993 unsigned int parts, pow5PartCount;
2994 fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
2996 bool isNearest;
2997
3000
3002
3003
3004 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
3005
3006 for (;; parts *= 2) {
3007 opStatus sigStatus, powStatus;
3008 unsigned int excessPrecision, truncatedBits;
3009
3011 excessPrecision = calcSemantics.precision - semantics->precision;
3012 truncatedBits = excessPrecision;
3013
3015 decSig.makeZero(sign);
3017
3018 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
3020 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
3022
3023 decSig.exponent += exp;
3024
3027 unsigned int powHUerr;
3028
3029 if (exp >= 0) {
3030
3031 calcLostFraction = decSig.multiplySignificand(pow5);
3032 powHUerr = powStatus != opOK;
3033 } else {
3034 calcLostFraction = decSig.divideSignificand(pow5);
3035
3036 if (decSig.exponent < semantics->minExponent) {
3037 excessPrecision += (semantics->minExponent - decSig.exponent);
3038 truncatedBits = excessPrecision;
3039 excessPrecision = std::min(excessPrecision, calcSemantics.precision);
3040 }
3041
3042 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
3043 }
3044
3045
3046
3048 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
3049
3051 powHUerr);
3052 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
3053 excessPrecision, isNearest);
3054
3055
3056 if (HUdistance >= HUerr) {
3057 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
3058 calcSemantics.precision - excessPrecision,
3059 excessPrecision);
3060
3061
3062
3063 exponent = (decSig.exponent + semantics->precision
3064 - (calcSemantics.precision - excessPrecision));
3066 decSig.partCount(),
3067 truncatedBits);
3068 return normalize(rounding_mode, calcLostFraction);
3069 }
3070 }
3071}
3072
3073ExpectedAPFloat::opStatus
3074IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) {
3075 decimalInfo D;
3077
3078
3081 return std::move(Err);
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
3111 sign = false;
3112 if (!semantics->hasZero)
3114
3115
3116
3117 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
3118 fs = handleOverflow(rounding_mode);
3119
3120
3121
3122
3123
3124 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
3125 (D.normalizedExponent + 1) * 28738 <=
3126 8651 * (semantics->minExponent - (int) semantics->precision)) {
3127
3129 zeroSignificand();
3131
3132
3133 } else if ((D.normalizedExponent - 1) * 42039
3134 >= 12655 * semantics->maxExponent) {
3135
3136 fs = handleOverflow(rounding_mode);
3137 } else {
3139 unsigned int partCount;
3140
3141
3142
3143
3144
3145 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
3147 decSignificand = new integerPart[partCount + 1];
3148 partCount = 0;
3149
3150
3151
3152
3153
3154 do {
3156
3157 val = 0;
3158 multiplier = 1;
3159
3160 do {
3161 if (*p == '.') {
3162 p++;
3163 if (p == str.end()) {
3164 break;
3165 }
3166 }
3168 if (decValue >= 10U) {
3169 delete[] decSignificand;
3170 return createError("Invalid character in significand");
3171 }
3172 multiplier *= 10;
3173 val = val * 10 + decValue;
3174
3175
3176 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
3177
3178
3180 partCount, partCount + 1, false);
3181
3182
3183
3184 if (decSignificand[partCount])
3185 partCount++;
3186 } while (p <= D.lastSigDigit);
3187
3189 fs = roundSignificandWithExponent(decSignificand, partCount,
3190 D.exponent, rounding_mode);
3191
3192 delete [] decSignificand;
3193 }
3194
3195 return fs;
3196}
3197
3198bool IEEEFloat::convertFromStringSpecials(StringRef str) {
3199 const size_t MIN_NAME_SIZE = 3;
3200
3201 if (str.size() < MIN_NAME_SIZE)
3202 return false;
3203
3204 if (str == "inf" || str == "INFINITY" || str == "+Inf") {
3206 return true;
3207 }
3208
3210 if (IsNegative) {
3211 if (str.size() < MIN_NAME_SIZE)
3212 return false;
3213
3214 if (str == "inf" || str == "INFINITY" || str == "Inf") {
3216 return true;
3217 }
3218 }
3219
3220
3222 if (IsSignaling) {
3223 if (str.size() < MIN_NAME_SIZE)
3224 return false;
3225 }
3226
3228
3229 if (str.empty()) {
3230 makeNaN(IsSignaling, IsNegative);
3231 return true;
3232 }
3233
3234
3235 if (str.front() == '(') {
3236
3237 if (str.size() <= 2 || str.back() != ')')
3238 return false;
3239
3240 str = str.slice(1, str.size() - 1);
3241 }
3242
3243
3244 unsigned Radix = 10;
3245 if (str[0] == '0') {
3246 if (str.size() > 1 && tolower(str[1]) == 'x') {
3248 Radix = 16;
3249 } else {
3250 Radix = 8;
3251 }
3252 }
3253
3254
3255 APInt Payload;
3257 makeNaN(IsSignaling, IsNegative, &Payload);
3258 return true;
3259 }
3260 }
3261
3262 return false;
3263}
3264
3265ExpectedAPFloat::opStatus
3267 if (str.empty())
3268 return createError("Invalid string length");
3269
3270
3271 if (convertFromStringSpecials(str))
3272 return opOK;
3273
3274
3276 size_t slen = str.size();
3277 sign = *p == '-' ? 1 : 0;
3278 if (sign && !semantics->hasSignedRepr)
3280 "This floating point format does not support signed values");
3281
3282 if (*p == '-' || *p == '+') {
3283 p++;
3284 slen--;
3285 if (!slen)
3286 return createError("String has no digits");
3287 }
3288
3289 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3290 if (slen == 2)
3292 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
3293 rounding_mode);
3294 }
3295
3296 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
3297}
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3324 bool upperCase,
3326 char *p;
3327
3328 p = dst;
3329 if (sign)
3330 *dst++ = '-';
3331
3332 switch (category) {
3336 break;
3337
3339 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
3340 dst += sizeof NaNU - 1;
3341 break;
3342
3344 *dst++ = '0';
3345 *dst++ = upperCase ? 'X': 'x';
3346 *dst++ = '0';
3347 if (hexDigits > 1) {
3348 *dst++ = '.';
3349 memset (dst, '0', hexDigits - 1);
3350 dst += hexDigits - 1;
3351 }
3352 *dst++ = upperCase ? 'P': 'p';
3353 *dst++ = '0';
3354 break;
3355
3357 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
3358 break;
3359 }
3360
3361 *dst = 0;
3362
3363 return static_cast<unsigned int>(dst - p);
3364}
3365
3366
3367
3368
3369
3370char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
3371 bool upperCase,
3373 unsigned int count, valueBits, shift, partsCount, outputDigits;
3374 const char *hexDigitChars;
3376 char *p;
3377 bool roundUp;
3378
3379 *dst++ = '0';
3380 *dst++ = upperCase ? 'X': 'x';
3381
3382 roundUp = false;
3384
3385 significand = significandParts();
3386 partsCount = partCount();
3387
3388
3389
3390 valueBits = semantics->precision + 3;
3392
3393
3394
3395 outputDigits = (valueBits - significandLSB () + 3) / 4;
3396
3397
3398
3399
3400 if (hexDigits) {
3401 if (hexDigits < outputDigits) {
3402
3403
3404 unsigned int bits;
3406
3407 bits = valueBits - hexDigits * 4;
3409 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
3410 }
3411 outputDigits = hexDigits;
3412 }
3413
3414
3415
3416
3417 p = ++dst;
3418
3420
3421 while (outputDigits && count) {
3423
3424
3425 if (--count == partsCount)
3426 part = 0;
3427 else
3428 part = significand[count] << shift;
3429
3430 if (count && shift)
3432
3433
3435
3436 curDigits = std::min(curDigits, outputDigits);
3437 dst += partAsHex (dst, part, curDigits, hexDigitChars);
3438 outputDigits -= curDigits;
3439 }
3440
3441 if (roundUp) {
3442 char *q = dst;
3443
3444
3445 do {
3446 q--;
3448 } while (*q == '0');
3450 } else {
3451
3452 memset (dst, '0', outputDigits);
3453 dst += outputDigits;
3454 }
3455
3456
3457
3458
3460 if (dst -1 == p)
3461 dst--;
3462 else
3463 p[0] = '.';
3464
3465
3466 *dst++ = upperCase ? 'P': 'p';
3467
3469}
3470
3474
3477
3478
3480 Arg.semantics->precision, Arg.exponent,
3482 Arg.significandParts(),
3483 Arg.significandParts() + Arg.partCount()));
3484}
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const {
3498 assert(partCount()==2);
3499
3500 uint64_t myexponent, mysignificand;
3501
3503 myexponent = exponent+16383;
3504 mysignificand = significandParts()[0];
3505 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
3506 myexponent = 0;
3507 } else if (category==fcZero) {
3508 myexponent = 0;
3509 mysignificand = 0;
3511 myexponent = 0x7fff;
3512 mysignificand = 0x8000000000000000ULL;
3513 } else {
3514 assert(category == fcNaN && "Unknown category");
3515 myexponent = 0x7fff;
3516 mysignificand = significandParts()[0];
3517 }
3518
3519 uint64_t words[2];
3520 words[0] = mysignificand;
3521 words[1] = ((uint64_t)(sign & 1) << 15) |
3522 (myexponent & 0x7fffLL);
3523 return APInt(80, words);
3524}
3525
3526APInt IEEEFloat::convertPPCDoubleDoubleLegacyAPFloatToAPInt() const {
3528 (const llvm::fltSemantics *)&APFloatBase::semPPCDoubleDoubleLegacy);
3529 assert(partCount()==2);
3530
3531 uint64_t words[2];
3533 bool losesInfo;
3534
3535
3536
3537
3538
3539
3540
3541 fltSemantics extendedSemantics = *semantics;
3544 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
3546 (void)fs;
3547
3549 fs = u.convert(APFloatBase::semIEEEdouble, rmNearestTiesToEven, &losesInfo);
3551 (void)fs;
3552 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
3553
3554
3555
3556
3557
3558 if (u.isFiniteNonZero() && losesInfo) {
3561 (void)fs;
3562
3565 fs = v.convert(APFloatBase::semIEEEdouble, rmNearestTiesToEven, &losesInfo);
3567 (void)fs;
3568 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
3569 } else {
3570 words[1] = 0;
3571 }
3572
3573 return APInt(128, words);
3574}
3575
3576template <const fltSemantics &S>
3577APInt IEEEFloat::convertIEEEFloatToAPInt() const {
3578 assert(semantics == &S);
3579 const int bias = (semantics == &APFloatBase::semFloat8E8M0FNU)
3580 ? -S.minExponent
3581 : -(S.minExponent - 1);
3582 constexpr unsigned int trailing_significand_bits = S.precision - 1;
3583 constexpr int integer_bit_part = trailing_significand_bits / integerPartWidth;
3586 constexpr uint64_t significand_mask = integer_bit - 1;
3587 constexpr unsigned int exponent_bits =
3588 trailing_significand_bits ? (S.sizeInBits - 1 - trailing_significand_bits)
3589 : S.sizeInBits;
3590 static_assert(exponent_bits < 64);
3591 constexpr uint64_t exponent_mask = (uint64_t{1} << exponent_bits) - 1;
3592
3593 uint64_t myexponent;
3595 mysignificand;
3596
3598 myexponent = exponent + bias;
3599 std::copy_n(significandParts(), mysignificand.size(),
3600 mysignificand.begin());
3601 if (myexponent == 1 &&
3602 !(significandParts()[integer_bit_part] & integer_bit))
3603 myexponent = 0;
3604 } else if (category == fcZero) {
3605 if (!S.hasZero)
3607 myexponent = ::exponentZero(S) + bias;
3608 mysignificand.fill(0);
3609 } else if (category == fcInfinity) {
3613 myexponent = ::exponentInf(S) + bias;
3614 mysignificand.fill(0);
3615 } else {
3616 assert(category == fcNaN && "Unknown category!");
3619 myexponent = ::exponentNaN(S) + bias;
3620 std::copy_n(significandParts(), mysignificand.size(),
3621 mysignificand.begin());
3622 }
3623 std::array<uint64_t, (S.sizeInBits + 63) / 64> words;
3624 auto words_iter =
3625 std::copy_n(mysignificand.begin(), mysignificand.size(), words.begin());
3626 if constexpr (significand_mask != 0 || trailing_significand_bits == 0) {
3627
3628 words[mysignificand.size() - 1] &= significand_mask;
3629 }
3630 std::fill(words_iter, words.end(), uint64_t{0});
3631 constexpr size_t last_word = words.size() - 1;
3632 uint64_t shifted_sign = static_cast<uint64_t>(sign & 1)
3633 << ((S.sizeInBits - 1) % 64);
3634 words[last_word] |= shifted_sign;
3635 uint64_t shifted_exponent = (myexponent & exponent_mask)
3636 << (trailing_significand_bits % 64);
3637 words[last_word] |= shifted_exponent;
3638 if constexpr (last_word == 0) {
3639 return APInt(S.sizeInBits, words[0]);
3640 }
3641 return APInt(S.sizeInBits, words);
3642}
3643
3644APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const {
3645 assert(partCount() == 2);
3646 return convertIEEEFloatToAPIntAPFloatBase::semIEEEquad();
3647}
3648
3649APInt IEEEFloat::convertDoubleAPFloatToAPInt() const {
3650 assert(partCount()==1);
3651 return convertIEEEFloatToAPIntAPFloatBase::semIEEEdouble();
3652}
3653
3654APInt IEEEFloat::convertFloatAPFloatToAPInt() const {
3655 assert(partCount()==1);
3656 return convertIEEEFloatToAPIntAPFloatBase::semIEEEsingle();
3657}
3658
3659APInt IEEEFloat::convertBFloatAPFloatToAPInt() const {
3660 assert(partCount() == 1);
3661 return convertIEEEFloatToAPIntAPFloatBase::semBFloat();
3662}
3663
3664APInt IEEEFloat::convertHalfAPFloatToAPInt() const {
3665 assert(partCount()==1);
3666 return convertIEEEFloatToAPIntAPFloatBase::APFloatBase::semIEEEhalf();
3667}
3668
3669APInt IEEEFloat::convertFloat8E5M2APFloatToAPInt() const {
3670 assert(partCount() == 1);
3671 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E5M2();
3672}
3673
3674APInt IEEEFloat::convertFloat8E5M2FNUZAPFloatToAPInt() const {
3675 assert(partCount() == 1);
3676 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E5M2FNUZ();
3677}
3678
3679APInt IEEEFloat::convertFloat8E4M3APFloatToAPInt() const {
3680 assert(partCount() == 1);
3681 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E4M3();
3682}
3683
3684APInt IEEEFloat::convertFloat8E4M3FNAPFloatToAPInt() const {
3685 assert(partCount() == 1);
3686 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E4M3FN();
3687}
3688
3689APInt IEEEFloat::convertFloat8E4M3FNUZAPFloatToAPInt() const {
3690 assert(partCount() == 1);
3691 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E4M3FNUZ();
3692}
3693
3694APInt IEEEFloat::convertFloat8E4M3B11FNUZAPFloatToAPInt() const {
3695 assert(partCount() == 1);
3696 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E4M3B11FNUZ();
3697}
3698
3699APInt IEEEFloat::convertFloat8E3M4APFloatToAPInt() const {
3700 assert(partCount() == 1);
3701 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E3M4();
3702}
3703
3704APInt IEEEFloat::convertFloatTF32APFloatToAPInt() const {
3705 assert(partCount() == 1);
3706 return convertIEEEFloatToAPIntAPFloatBase::semFloatTF32();
3707}
3708
3709APInt IEEEFloat::convertFloat8E8M0FNUAPFloatToAPInt() const {
3710 assert(partCount() == 1);
3711 return convertIEEEFloatToAPIntAPFloatBase::semFloat8E8M0FNU();
3712}
3713
3714APInt IEEEFloat::convertFloat6E3M2FNAPFloatToAPInt() const {
3715 assert(partCount() == 1);
3716 return convertIEEEFloatToAPIntAPFloatBase::semFloat6E3M2FN();
3717}
3718
3719APInt IEEEFloat::convertFloat6E2M3FNAPFloatToAPInt() const {
3720 assert(partCount() == 1);
3721 return convertIEEEFloatToAPIntAPFloatBase::semFloat6E2M3FN();
3722}
3723
3724APInt IEEEFloat::convertFloat4E2M1FNAPFloatToAPInt() const {
3725 assert(partCount() == 1);
3726 return convertIEEEFloatToAPIntAPFloatBase::semFloat4E2M1FN();
3727}
3728
3729
3730
3731
3732
3734 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEhalf)
3735 return convertHalfAPFloatToAPInt();
3736
3737 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semBFloat)
3738 return convertBFloatAPFloatToAPInt();
3739
3740 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEsingle)
3741 return convertFloatAPFloatToAPInt();
3742
3743 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEdouble)
3744 return convertDoubleAPFloatToAPInt();
3745
3746 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEquad)
3747 return convertQuadrupleAPFloatToAPInt();
3748
3749 if (semantics ==
3751 return convertPPCDoubleDoubleLegacyAPFloatToAPInt();
3752
3753 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E5M2)
3754 return convertFloat8E5M2APFloatToAPInt();
3755
3756 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E5M2FNUZ)
3757 return convertFloat8E5M2FNUZAPFloatToAPInt();
3758
3759 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3)
3760 return convertFloat8E4M3APFloatToAPInt();
3761
3762 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3FN)
3763 return convertFloat8E4M3FNAPFloatToAPInt();
3764
3765 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3FNUZ)
3766 return convertFloat8E4M3FNUZAPFloatToAPInt();
3767
3768 if (semantics ==
3770 return convertFloat8E4M3B11FNUZAPFloatToAPInt();
3771
3772 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E3M4)
3773 return convertFloat8E3M4APFloatToAPInt();
3774
3775 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloatTF32)
3776 return convertFloatTF32APFloatToAPInt();
3777
3778 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E8M0FNU)
3779 return convertFloat8E8M0FNUAPFloatToAPInt();
3780
3781 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat6E3M2FN)
3782 return convertFloat6E3M2FNAPFloatToAPInt();
3783
3784 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat6E2M3FN)
3785 return convertFloat6E2M3FNAPFloatToAPInt();
3786
3787 if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat4E2M1FN)
3788 return convertFloat4E2M1FNAPFloatToAPInt();
3789
3792 "unknown format!");
3793 return convertF80LongDoubleAPFloatToAPInt();
3794}
3795
3798 "Float semantics are not IEEEsingle");
3801}
3802
3805 "Float semantics are not IEEEdouble");
3808}
3809
3810#ifdef HAS_IEE754_FLOAT128
3811float128 IEEEFloat::convertToQuad() const {
3813 "Float semantics are not IEEEquads");
3815 return api.bitsToQuad();
3816}
3817#endif
3818
3819
3820
3821
3822
3823
3824
3825
3826void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) {
3829 uint64_t myexponent = (i2 & 0x7fff);
3830 uint64_t mysignificand = i1;
3831 uint8_t myintegerbit = mysignificand >> 63;
3832
3833 initialize(&APFloatBase::semX87DoubleExtended);
3834 assert(partCount()==2);
3835
3836 sign = static_cast<unsigned int>(i2>>15);
3837 if (myexponent == 0 && mysignificand == 0) {
3839 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3841 } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3842 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3843 category = fcNaN;
3844 exponent = exponentNaN();
3845 significandParts()[0] = mysignificand;
3846 significandParts()[1] = 0;
3847 } else {
3849 exponent = myexponent - 16383;
3850 significandParts()[0] = mysignificand;
3851 significandParts()[1] = 0;
3852 if (myexponent==0)
3853 exponent = -16382;
3854 }
3855}
3856
3857void IEEEFloat::initFromPPCDoubleDoubleLegacyAPInt(const APInt &api) {
3861 bool losesInfo;
3862
3863
3864 initFromDoubleAPInt(APInt(64, i1));
3866 &losesInfo);
3868 (void)fs;
3869
3870
3872 IEEEFloat v(APFloatBase::semIEEEdouble, APInt(64, i2));
3874 &losesInfo);
3876 (void)fs;
3877
3879 }
3880}
3881
3882
3883
3884
3885
3886
3887void IEEEFloat::initFromFloat8E8M0FNUAPInt(const APInt &api) {
3888 const uint64_t exponent_mask = 0xff;
3890 uint64_t myexponent = (val & exponent_mask);
3891
3892 initialize(&APFloatBase::semFloat8E8M0FNU);
3893 assert(partCount() == 1);
3894
3895
3896 sign = 0;
3897
3898
3899
3900
3901 uint64_t mysignificand = 1;
3902 significandParts()[0] = mysignificand;
3903
3904
3905
3906 if (val == exponent_mask) {
3907 category = fcNaN;
3908 exponent = exponentNaN();
3909 return;
3910 }
3911
3913 exponent = myexponent - 127;
3914}
3915template <const fltSemantics &S>
3916void IEEEFloat::initFromIEEEAPInt(const APInt &api) {
3920 constexpr uint64_t significand_mask = integer_bit - 1;
3921 constexpr unsigned int trailing_significand_bits = S.precision - 1;
3922 constexpr unsigned int stored_significand_parts =
3924 constexpr unsigned int exponent_bits =
3925 S.sizeInBits - 1 - trailing_significand_bits;
3926 static_assert(exponent_bits < 64);
3927 constexpr uint64_t exponent_mask = (uint64_t{1} << exponent_bits) - 1;
3928 constexpr int bias = -(S.minExponent - 1);
3929
3930
3931
3932 std::array<integerPart, stored_significand_parts> mysignificand;
3933 std::copy_n(api.getRawData(), mysignificand.size(), mysignificand.begin());
3934 if constexpr (significand_mask != 0) {
3935 mysignificand[mysignificand.size() - 1] &= significand_mask;
3936 }
3937
3938
3939
3941 uint64_t myexponent =
3942 (last_word >> (trailing_significand_bits % 64)) & exponent_mask;
3943
3944 initialize(&S);
3945 assert(partCount() == mysignificand.size());
3946
3947 sign = static_cast<unsigned int>(last_word >> ((S.sizeInBits - 1) % 64));
3948
3949 bool all_zero_significand =
3951
3952 bool is_zero = myexponent == 0 && all_zero_significand;
3953
3955 if (myexponent - bias == ::exponentInf(S) && all_zero_significand) {
3957 return;
3958 }
3959 }
3960
3961 bool is_nan = false;
3962
3964 is_nan = myexponent - bias == ::exponentNaN(S) && !all_zero_significand;
3966 bool all_ones_significand =
3967 std::all_of(mysignificand.begin(), mysignificand.end() - 1,
3968 [](integerPart bits) { return bits == ~integerPart{0}; }) &&
3969 (!significand_mask ||
3970 mysignificand[mysignificand.size() - 1] == significand_mask);
3971 is_nan = myexponent - bias == ::exponentNaN(S) && all_ones_significand;
3973 is_nan = is_zero && sign;
3974 }
3975
3976 if (is_nan) {
3977 category = fcNaN;
3979 std::copy_n(mysignificand.begin(), mysignificand.size(),
3980 significandParts());
3981 return;
3982 }
3983
3984 if (is_zero) {
3985 makeZero(sign);
3986 return;
3987 }
3988
3990 exponent = myexponent - bias;
3991 std::copy_n(mysignificand.begin(), mysignificand.size(), significandParts());
3992 if (myexponent == 0)
3993 exponent = S.minExponent;
3994 else
3995 significandParts()[mysignificand.size()-1] |= integer_bit;
3996}
3997
3998void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) {
3999 initFromIEEEAPIntAPFloatBase::semIEEEquad(api);
4000}
4001
4002void IEEEFloat::initFromDoubleAPInt(const APInt &api) {
4003 initFromIEEEAPIntAPFloatBase::semIEEEdouble(api);
4004}
4005
4006void IEEEFloat::initFromFloatAPInt(const APInt &api) {
4007 initFromIEEEAPIntAPFloatBase::semIEEEsingle(api);
4008}
4009
4010void IEEEFloat::initFromBFloatAPInt(const APInt &api) {
4011 initFromIEEEAPIntAPFloatBase::semBFloat(api);
4012}
4013
4014void IEEEFloat::initFromHalfAPInt(const APInt &api) {
4015 initFromIEEEAPIntAPFloatBase::semIEEEhalf(api);
4016}
4017
4018void IEEEFloat::initFromFloat8E5M2APInt(const APInt &api) {
4019 initFromIEEEAPIntAPFloatBase::semFloat8E5M2(api);
4020}
4021
4022void IEEEFloat::initFromFloat8E5M2FNUZAPInt(const APInt &api) {
4023 initFromIEEEAPIntAPFloatBase::semFloat8E5M2FNUZ(api);
4024}
4025
4026void IEEEFloat::initFromFloat8E4M3APInt(const APInt &api) {
4027 initFromIEEEAPIntAPFloatBase::semFloat8E4M3(api);
4028}
4029
4030void IEEEFloat::initFromFloat8E4M3FNAPInt(const APInt &api) {
4031 initFromIEEEAPIntAPFloatBase::semFloat8E4M3FN(api);
4032}
4033
4034void IEEEFloat::initFromFloat8E4M3FNUZAPInt(const APInt &api) {
4035 initFromIEEEAPIntAPFloatBase::semFloat8E4M3FNUZ(api);
4036}
4037
4038void IEEEFloat::initFromFloat8E4M3B11FNUZAPInt(const APInt &api) {
4039 initFromIEEEAPIntAPFloatBase::semFloat8E4M3B11FNUZ(api);
4040}
4041
4042void IEEEFloat::initFromFloat8E3M4APInt(const APInt &api) {
4043 initFromIEEEAPIntAPFloatBase::semFloat8E3M4(api);
4044}
4045
4046void IEEEFloat::initFromFloatTF32APInt(const APInt &api) {
4047 initFromIEEEAPIntAPFloatBase::semFloatTF32(api);
4048}
4049
4050void IEEEFloat::initFromFloat6E3M2FNAPInt(const APInt &api) {
4051 initFromIEEEAPIntAPFloatBase::semFloat6E3M2FN(api);
4052}
4053
4054void IEEEFloat::initFromFloat6E2M3FNAPInt(const APInt &api) {
4055 initFromIEEEAPIntAPFloatBase::semFloat6E2M3FN(api);
4056}
4057
4058void IEEEFloat::initFromFloat4E2M1FNAPInt(const APInt &api) {
4059 initFromIEEEAPIntAPFloatBase::semFloat4E2M1FN(api);
4060}
4061
4062
4063void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) {
4065 if (Sem == &APFloatBase::semIEEEhalf)
4066 return initFromHalfAPInt(api);
4067 if (Sem == &APFloatBase::semBFloat)
4068 return initFromBFloatAPInt(api);
4069 if (Sem == &APFloatBase::semIEEEsingle)
4070 return initFromFloatAPInt(api);
4071 if (Sem == &APFloatBase::semIEEEdouble)
4072 return initFromDoubleAPInt(api);
4073 if (Sem == &APFloatBase::semX87DoubleExtended)
4074 return initFromF80LongDoubleAPInt(api);
4075 if (Sem == &APFloatBase::semIEEEquad)
4076 return initFromQuadrupleAPInt(api);
4077 if (Sem == &APFloatBase::semPPCDoubleDoubleLegacy)
4078 return initFromPPCDoubleDoubleLegacyAPInt(api);
4079 if (Sem == &APFloatBase::semFloat8E5M2)
4080 return initFromFloat8E5M2APInt(api);
4081 if (Sem == &APFloatBase::semFloat8E5M2FNUZ)
4082 return initFromFloat8E5M2FNUZAPInt(api);
4083 if (Sem == &APFloatBase::semFloat8E4M3)
4084 return initFromFloat8E4M3APInt(api);
4085 if (Sem == &APFloatBase::semFloat8E4M3FN)
4086 return initFromFloat8E4M3FNAPInt(api);
4087 if (Sem == &APFloatBase::semFloat8E4M3FNUZ)
4088 return initFromFloat8E4M3FNUZAPInt(api);
4089 if (Sem == &APFloatBase::semFloat8E4M3B11FNUZ)
4090 return initFromFloat8E4M3B11FNUZAPInt(api);
4091 if (Sem == &APFloatBase::semFloat8E3M4)
4092 return initFromFloat8E3M4APInt(api);
4093 if (Sem == &APFloatBase::semFloatTF32)
4094 return initFromFloatTF32APInt(api);
4095 if (Sem == &APFloatBase::semFloat8E8M0FNU)
4096 return initFromFloat8E8M0FNUAPInt(api);
4097 if (Sem == &APFloatBase::semFloat6E3M2FN)
4098 return initFromFloat6E3M2FNAPInt(api);
4099 if (Sem == &APFloatBase::semFloat6E2M3FN)
4100 return initFromFloat6E2M3FNAPInt(api);
4101 if (Sem == &APFloatBase::semFloat4E2M1FN)
4102 return initFromFloat4E2M1FNAPInt(api);
4103
4105}
4106
4107
4108
4110 if (Negative && !semantics->hasSignedRepr)
4112 "This floating point format does not support signed values");
4113
4114
4115
4116
4118 sign = Negative;
4119 exponent = semantics->maxExponent;
4120
4121
4122 integerPart *significand = significandParts();
4123 unsigned PartCount = partCount();
4124 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
4125
4126
4127
4128 const unsigned NumUnusedHighBits =
4130 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
4131 ? (~integerPart(0) >> NumUnusedHighBits)
4132 : 0;
4135 (semantics->precision > 1))
4137}
4138
4139
4140
4142 if (Negative && !semantics->hasSignedRepr)
4144 "This floating point format does not support signed values");
4145
4146
4147
4148
4150 sign = Negative;
4151 exponent = semantics->minExponent;
4152 APInt::tcSet(significandParts(), 1, partCount());
4153}
4154
4156 if (Negative && !semantics->hasSignedRepr)
4158 "This floating point format does not support signed values");
4159
4160
4161
4162
4163
4165 zeroSignificand();
4166 sign = Negative;
4167 exponent = semantics->minExponent;
4168 APInt::tcSetBit(significandParts(), semantics->precision - 1);
4169}
4170
4172 initFromAPInt(&Sem, API);
4173}
4174
4178
4182
4183namespace {
4185 Buffer.append(Str.begin(), Str.end());
4186 }
4187
4188
4189
4190 void AdjustToPrecision(APInt &significand,
4191 int &exp, unsigned FormatPrecision) {
4193
4194
4195 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
4196
4197 if (bits <= bitsRequired) return;
4198
4199 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
4200 if (!tensRemovable) return;
4201
4202 exp += tensRemovable;
4203
4206 while (true) {
4207 if (tensRemovable & 1)
4208 divisor *= powten;
4209 tensRemovable >>= 1;
4210 if (!tensRemovable) break;
4211 powten *= powten;
4212 }
4213
4214 significand = significand.udiv(divisor);
4215
4216
4218 }
4219
4220
4222 int &exp, unsigned FormatPrecision) {
4223 unsigned N = buffer.size();
4224 if (N <= FormatPrecision) return;
4225
4226
4227 unsigned FirstSignificant = N - FormatPrecision;
4228
4229
4230
4231
4232
4233
4234 if (buffer[FirstSignificant - 1] < '5') {
4235 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
4236 FirstSignificant++;
4237
4238 exp += FirstSignificant;
4239 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
4240 return;
4241 }
4242
4243
4244
4245 for (unsigned I = FirstSignificant; I != N; ++I) {
4246 if (buffer[I] == '9') {
4247 FirstSignificant++;
4248 } else {
4249 buffer[I]++;
4250 break;
4251 }
4252 }
4253
4254
4255 if (FirstSignificant == N) {
4256 exp += FirstSignificant;
4259 return;
4260 }
4261
4262 exp += FirstSignificant;
4263 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
4264 }
4265
4267 APInt significand, unsigned FormatPrecision,
4268 unsigned FormatMaxPadding, bool TruncateZero) {
4269 const int semanticsPrecision = significand.getBitWidth();
4270
4272 Str.push_back('-');
4273
4274
4275
4276 if (!FormatPrecision) {
4277
4278
4279
4280
4281
4282
4283
4284 FormatPrecision = 2 + semanticsPrecision * 59 / 196;
4285 }
4286
4287
4288 int trailingZeros = significand.countr_zero();
4289 exp += trailingZeros;
4291
4292
4293 if (exp == 0) {
4294
4295 } else if (exp > 0) {
4296
4297 significand = significand.zext(semanticsPrecision + exp);
4298 significand <<= exp;
4299 exp = 0;
4300 } else {
4301 int texp = -exp;
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312 unsigned precision = semanticsPrecision + (137 * texp + 136) / 59;
4313
4314
4315
4316 significand = significand.zext(precision);
4317 APInt five_to_the_i(precision, 5);
4318 while (true) {
4319 if (texp & 1)
4320 significand *= five_to_the_i;
4321
4322 texp >>= 1;
4323 if (!texp)
4324 break;
4325 five_to_the_i *= five_to_the_i;
4326 }
4327 }
4328
4329 AdjustToPrecision(significand, exp, FormatPrecision);
4330
4332
4333
4334 unsigned precision = significand.getBitWidth();
4335 if (precision < 4) {
4336
4337 precision = 4;
4338 significand = significand.zext(precision);
4339 }
4340 APInt ten(precision, 10);
4341 APInt digit(precision, 0);
4342
4343 bool inTrail = true;
4344 while (significand != 0) {
4345
4346
4347 APInt::udivrem(significand, ten, significand, digit);
4348
4349 unsigned d = digit.getZExtValue();
4350
4351
4352 if (inTrail && !d)
4353 exp++;
4354 else {
4355 buffer.push_back((char) ('0' + d));
4356 inTrail = false;
4357 }
4358 }
4359
4360 assert(!buffer.empty() && "no characters in buffer!");
4361
4362
4363
4364 AdjustToPrecision(buffer, exp, FormatPrecision);
4365
4366 unsigned NDigits = buffer.size();
4367
4368
4369 bool FormatScientific;
4370 if (!FormatMaxPadding)
4371 FormatScientific = true;
4372 else {
4373 if (exp >= 0) {
4374
4375
4376
4377 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
4378 NDigits + (unsigned) exp > FormatPrecision);
4379 } else {
4380
4381 int MSD = exp + (int) (NDigits - 1);
4382 if (MSD >= 0) {
4383
4384 FormatScientific = false;
4385 } else {
4386
4387
4388 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
4389 }
4390 }
4391 }
4392
4393
4394 if (FormatScientific) {
4395 exp += (NDigits - 1);
4396
4397 Str.push_back(buffer[NDigits-1]);
4398 Str.push_back('.');
4399 if (NDigits == 1 && TruncateZero)
4400 Str.push_back('0');
4401 else
4402 for (unsigned I = 1; I != NDigits; ++I)
4403 Str.push_back(buffer[NDigits-1-I]);
4404
4405 if (!TruncateZero && FormatPrecision > NDigits - 1)
4406 Str.append(FormatPrecision - NDigits + 1, '0');
4407
4408 Str.push_back(TruncateZero ? 'E' : 'e');
4409
4410 Str.push_back(exp >= 0 ? '+' : '-');
4411 if (exp < 0)
4412 exp = -exp;
4414 do {
4415 expbuf.push_back((char) ('0' + (exp % 10)));
4416 exp /= 10;
4417 } while (exp);
4418
4419 if (!TruncateZero && expbuf.size() < 2)
4421 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
4422 Str.push_back(expbuf[E-1-I]);
4423 return;
4424 }
4425
4426
4427 if (exp >= 0) {
4428 for (unsigned I = 0; I != NDigits; ++I)
4429 Str.push_back(buffer[NDigits-1-I]);
4430 for (unsigned I = 0; I != (unsigned) exp; ++I)
4431 Str.push_back('0');
4432 return;
4433 }
4434
4435
4436
4437
4438 int NWholeDigits = exp + (int) NDigits;
4439
4440 unsigned I = 0;
4441 if (NWholeDigits > 0) {
4442 for (; I != (unsigned) NWholeDigits; ++I)
4443 Str.push_back(buffer[NDigits-I-1]);
4444 Str.push_back('.');
4445 } else {
4446 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
4447
4448 Str.push_back('0');
4449 Str.push_back('.');
4450 for (unsigned Z = 1; Z != NZeros; ++Z)
4451 Str.push_back('0');
4452 }
4453
4454 for (; I != NDigits; ++I)
4455 Str.push_back(buffer[NDigits-I-1]);
4456
4457 }
4458}
4459
4461 unsigned FormatMaxPadding, bool TruncateZero) const {
4462 switch (category) {
4465 return append(Str, "-Inf");
4466 else
4467 return append(Str, "+Inf");
4468
4469 case fcNaN: return append(Str, "NaN");
4470
4473 Str.push_back('-');
4474
4475 if (!FormatMaxPadding) {
4476 if (TruncateZero)
4477 append(Str, "0.0E+0");
4478 else {
4479 append(Str, "0.0");
4480 if (FormatPrecision > 1)
4481 Str.append(FormatPrecision - 1, '0');
4482 append(Str, "e+00");
4483 }
4484 } else {
4485 Str.push_back('0');
4486 }
4487 return;
4488
4490 break;
4491 }
4492
4493
4494 int exp = exponent - ((int) semantics->precision - 1);
4495 APInt significand(
4496 semantics->precision,
4498
4499 toStringImpl(Str, isNegative(), exp, significand, FormatPrecision,
4500 FormatMaxPadding, TruncateZero);
4501
4502}
4503
4506 return INT_MIN;
4507
4508 const integerPart *Parts = significandParts();
4509 const int PartCount = partCountForBits(semantics->precision);
4510
4511 int PopCount = 0;
4512 for (int i = 0; i < PartCount; ++i) {
4514 if (PopCount > 1)
4515 return INT_MIN;
4516 }
4517
4518 if (exponent != semantics->minExponent)
4519 return exponent;
4520
4521 int CountrParts = 0;
4522 for (int i = 0; i < PartCount;
4524 if (Parts[i] != 0) {
4525 return exponent - semantics->precision + CountrParts +
4527 }
4528 }
4529
4531}
4532
4535 return false;
4538 return false;
4539
4540
4541
4542 return (significandParts(), semantics->precision - 2);
4543}
4544
4545
4546
4547
4548
4550
4551 if (nextDown)
4553
4554
4556
4557
4558 switch (category) {
4560
4562 break;
4563
4565 break;
4567
4568
4569
4572
4574 }
4575 break;
4577
4579 break;
4581
4583 APInt::tcSet(significandParts(), 0, partCount());
4585 exponent = 0;
4587 sign = false;
4588 if (!semantics->hasZero)
4590 break;
4591 }
4592
4595
4597 break;
4598 } else if (semantics->nonFiniteBehavior ==
4600
4601 break;
4602 } else {
4603
4604 APInt::tcSet(significandParts(), 0, partCount());
4606 exponent = semantics->maxExponent + 1;
4607 break;
4608 }
4609 }
4610
4611
4613
4614
4615
4616
4617
4618
4619
4620 bool WillCrossBinadeBoundary =
4621 exponent != semantics->minExponent && isSignificandAllZeros();
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636 integerPart *Parts = significandParts();
4638
4639 if (WillCrossBinadeBoundary) {
4640
4641
4642
4644 exponent--;
4645 }
4646 } else {
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4658 (() && isSignificandAllOnes());
4659
4660 if (WillCrossBinadeBoundary) {
4661 integerPart *Parts = significandParts();
4664 assert(exponent != semantics->maxExponent &&
4665 "We can not increment an exponent beyond the maxExponent allowed"
4666 " by the given floating point semantics.");
4667 exponent++;
4668 } else {
4669 incrementSignificand();
4670 }
4671 }
4672 break;
4673 }
4674
4675
4676 if (nextDown)
4678
4679 return result;
4680}
4681
4683 return ::exponentNaN(*semantics);
4684}
4685
4687 return ::exponentInf(*semantics);
4688}
4689
4691 return ::exponentZero(*semantics);
4692}
4693
4696 llvm_unreachable("This floating point format does not support Inf");
4697
4699
4700 makeNaN(false, Negative);
4701 return;
4702 }
4704 sign = Negative;
4706 APInt::tcSet(significandParts(), 0, partCount());
4707}
4708
4710 if (!semantics->hasZero)
4711 llvm_unreachable("This floating point format does not support Zero");
4712
4714 sign = Negative;
4716
4717 sign = false;
4718 }
4720 APInt::tcSet(significandParts(), 0, partCount());
4721}
4722
4728
4730 if (Arg.isNaN())
4737 return Arg.exponent;
4738
4741
4742 Normalized.exponent += SignificandBits;
4744 return Normalized.exponent - SignificandBits;
4745}
4746
4748 auto MaxExp = X.getSemantics().maxExponent;
4749 auto MinExp = X.getSemantics().minExponent;
4750
4751
4752
4753
4754
4755
4756
4757 int SignificandBits = X.getSemantics().precision - 1;
4758 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
4759
4760
4761 X.exponent += std::clamp(Exp, -MaxIncrement - 1, MaxIncrement);
4763 if (X.isNaN())
4764 X.makeQuiet();
4765 return X;
4766}
4767
4769 Exp = ilogb(Val);
4770
4771
4774 Quiet.makeQuiet();
4776 }
4777
4779 return Val;
4780
4781
4782
4784 return scalbn(Val, -Exp, RM);
4785}
4786
4788 : Semantics(&S),
4789 Floats(new APFloat[2]{APFloat(APFloatBase::semIEEEdouble),
4790 APFloat(APFloatBase::semIEEEdouble)}) {
4791 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4792}
4793
4795 : Semantics(&S), Floats(new APFloat[2]{
4798 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4799}
4800
4802 : Semantics(&S),
4803 Floats(new APFloat[2]{APFloat(APFloatBase::semIEEEdouble, I),
4804 APFloat(APFloatBase::semIEEEdouble)}) {
4805 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4806}
4807
4809 : Semantics(&S),
4811 APFloat(APFloatBase::semIEEEdouble, APInt(64, I.getRawData()[0])),
4812 APFloat(APFloatBase::semIEEEdouble, APInt(64, I.getRawData()[1]))}) {
4813 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4814}
4815
4818 : Semantics(&S),
4819 Floats(new APFloat[2]{std::move(First), std::move(Second)}) {
4820 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4821 assert(&Floats[0].getSemantics() == &APFloatBase::semIEEEdouble);
4822 assert(&Floats[1].getSemantics() == &APFloatBase::semIEEEdouble);
4823}
4824
4826 : Semantics(RHS.Semantics),
4827 Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]),
4829 : nullptr) {
4830 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4831}
4832
4834 : Semantics(RHS.Semantics), Floats(RHS.Floats) {
4835 RHS.Semantics = &APFloatBase::semBogus;
4836 RHS.Floats = nullptr;
4837 assert(Semantics == &APFloatBase::semPPCDoubleDouble);
4838}
4839
4841 if (Semantics == RHS.Semantics && RHS.Floats) {
4842 Floats[0] = RHS.Floats[0];
4843 Floats[1] = RHS.Floats[1];
4844 } else if (this != &RHS) {
4847 }
4848 return *this;
4849}
4850
4851
4852
4853
4854
4855
4856
4858 if (.isFinite())
4864}
4865
4866
4867
4868
4877 Floats[0] = std::move(z);
4878 Floats[1].makeZero( false);
4880 }
4882 auto AComparedToC = a.compareAbsoluteValue(c);
4883 z = cc;
4886
4889 } else {
4890
4891 Status |= z.add(a, RM);
4892 Status |= z.add(c, RM);
4893 }
4895 Floats[0] = std::move(z);
4896 Floats[1].makeZero( false);
4898 }
4899 Floats[0] = z;
4901 Status |= zz.add(cc, RM);
4903
4904 Floats[1] = a;
4905 Status |= Floats[1].subtract(z, RM);
4906 Status |= Floats[1].add(c, RM);
4907 Status |= Floats[1].add(zz, RM);
4908 } else {
4909
4910 Floats[1] = c;
4911 Status |= Floats[1].subtract(z, RM);
4912 Status |= Floats[1].add(a, RM);
4913 Status |= Floats[1].add(zz, RM);
4914 }
4915 } else {
4916
4918 Status |= q.subtract(z, RM);
4919
4920
4921
4922 auto zz = q;
4923 Status |= zz.add(c, RM);
4924 Status |= q.add(z, RM);
4925 Status |= q.subtract(a, RM);
4926 q.changeSign();
4927 Status |= zz.add(q, RM);
4928 Status |= zz.add(aa, RM);
4929 Status |= zz.add(cc, RM);
4931 Floats[0] = std::move(z);
4932 Floats[1].makeZero( false);
4933 return opOK;
4934 }
4935 Floats[0] = z;
4936 Status |= Floats[0].add(zz, RM);
4937 if (!Floats[0].isFinite()) {
4938 Floats[1].makeZero( false);
4940 }
4941 Floats[1] = std::move(z);
4942 Status |= Floats[1].subtract(Floats[0], RM);
4943 Status |= Floats[1].add(zz, RM);
4944 }
4946}
4947
4952 if (LHS.getCategory() == fcNaN) {
4953 Out = LHS;
4954 return opOK;
4955 }
4956 if (RHS.getCategory() == fcNaN) {
4957 Out = RHS;
4958 return opOK;
4959 }
4960 if (LHS.getCategory() == fcZero) {
4961 Out = RHS;
4962 return opOK;
4963 }
4964 if (RHS.getCategory() == fcZero) {
4965 Out = LHS;
4966 return opOK;
4967 }
4969 LHS.isNegative() != RHS.isNegative()) {
4970 Out.makeNaN(false, Out.isNegative(), nullptr);
4972 }
4974 Out = LHS;
4975 return opOK;
4976 }
4978 Out = RHS;
4979 return opOK;
4980 }
4982
4984 CC(RHS.Floats[1]);
4985 assert(&A.getSemantics() == &APFloatBase::semIEEEdouble);
4986 assert(&AA.getSemantics() == &APFloatBase::semIEEEdouble);
4987 assert(&C.getSemantics() == &APFloatBase::semIEEEdouble);
4988 assert(&CC.getSemantics() == &APFloatBase::semIEEEdouble);
4989 assert(&Out.Floats[0].getSemantics() == &APFloatBase::semIEEEdouble);
4990 assert(&Out.Floats[1].getSemantics() == &APFloatBase::semIEEEdouble);
4991 return Out.addImpl(A, AA, C, CC, RM);
4992}
4993
4996 return addWithSpecial(*this, RHS, *this, RM);
4997}
4998
5002 auto Ret = add(RHS, RM);
5004 return Ret;
5005}
5006
5009 const auto &LHS = *this;
5010 auto &Out = *this;
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026 if (LHS.getCategory() == fcNaN) {
5027 Out = LHS;
5028 return opOK;
5029 }
5030 if (RHS.getCategory() == fcNaN) {
5031 Out = RHS;
5032 return opOK;
5033 }
5034 if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) ||
5035 (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) {
5036 Out.makeNaN(false, false, nullptr);
5037 return opOK;
5038 }
5039 if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) {
5040 Out = LHS;
5041 return opOK;
5042 }
5043 if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) {
5044 Out = RHS;
5045 return opOK;
5046 }
5048 "Special cases not handled exhaustively");
5049
5051 APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1];
5052
5054 Status |= T.multiply(C, RM);
5055 if (.isFiniteNonZero()) {
5056 Floats[0] = T;
5057 Floats[1].makeZero( false);
5059 }
5060
5061
5063 T.changeSign();
5065 T.changeSign();
5066 {
5067
5069 Status |= V.multiply(D, RM);
5070
5072 Status |= W.multiply(C, RM);
5073 Status |= V.add(W, RM);
5074
5076 }
5077
5079 Status |= U.add(Tau, RM);
5080
5081 Floats[0] = U;
5082 if (!U.isFinite()) {
5083 Floats[1].makeZero( false);
5084 } else {
5085
5086 Status |= T.subtract(U, RM);
5087 Status |= T.add(Tau, RM);
5088 Floats[1] = T;
5089 }
5091}
5092
5095 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5096 "Unexpected Semantics");
5098 auto Ret = Tmp.divide(
5099 APFloat(APFloatBase::semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM);
5101 return Ret;
5102}
5103
5105 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5106 "Unexpected Semantics");
5109 APFloat(APFloatBase::semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
5111 return Ret;
5112}
5113
5115 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5116 "Unexpected Semantics");
5118 auto Ret = Tmp.mod(
5119 APFloat(APFloatBase::semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
5121 return Ret;
5122}
5123
5128 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5129 "Unexpected Semantics");
5132 APFloat(APFloatBase::semPPCDoubleDoubleLegacy,
5135 RM);
5137 return Ret;
5138}
5139
5141 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5142 "Unexpected Semantics");
5145
5148
5149
5150
5151
5152 if (.isFiniteNonZero() || Lo.isZero()) {
5153 Floats[0] = std::move(RoundedHi);
5154 Floats[1].makeZero(false);
5155 return HiStatus;
5156 }
5157
5158
5159
5160 auto RoundToNearestHelper = [](APFloat ToRound, APFloat Rounded,
5162
5163
5164
5165
5166 const APFloat RoundingError = Rounded - ToRound;
5167 if (TieBreaker.isNonZero() &&
5168 TieBreaker.isNegative() != RoundingError.isNegative() &&
5169 abs(RoundingError).isExactlyValue(0.5))
5170 Rounded.add(
5171 APFloat::getOne(Rounded.getSemantics(), TieBreaker.isNegative()),
5173 return Rounded;
5174 };
5175
5176
5177
5178 if (RoundedHi != Hi) {
5179
5180
5181
5183 RoundedHi = RoundToNearestHelper(Hi, RoundedHi, Lo);
5184
5185 Floats[0] = std::move(RoundedHi);
5186 Floats[1].makeZero(false);
5187 return HiStatus;
5188 }
5189
5190
5191
5194
5195
5196
5198 else
5199 LoRoundingMode = RM;
5200
5204
5205
5206
5207 RoundedLo = RoundToNearestHelper(Lo, RoundedLo, Hi);
5208
5209
5210 std::tie(RoundedHi, RoundedLo) = fastTwoSum(RoundedHi, RoundedLo);
5211
5212 Floats[0] = std::move(RoundedHi);
5213 Floats[1] = std::move(RoundedLo);
5214 return LoStatus;
5215}
5216
5218 Floats[0].changeSign();
5219 Floats[1].changeSign();
5220}
5221
5224
5225 const cmpResult HiPartCmp = Floats[0].compareAbsoluteValue(RHS.Floats[0]);
5227 return HiPartCmp;
5228
5229
5230 if (Floats[1].isZero() && RHS.Floats[1].isZero())
5232
5233
5234
5235
5236 const bool ThisIsSubtractive =
5237 Floats[0].isNegative() != Floats[1].isNegative();
5238 const bool RHSIsSubtractive =
5239 RHS.Floats[0].isNegative() != RHS.Floats[1].isNegative();
5240
5241
5242 if (Floats[1].isZero())
5243
5244
5245
5247
5248
5249 if (RHS.Floats[1].isZero())
5250
5251
5252
5254
5255
5256 if (ThisIsSubtractive != RHSIsSubtractive)
5258
5259
5260
5261 const cmpResult LoPartCmp = Floats[1].compareAbsoluteValue(RHS.Floats[1]);
5262
5263 if (ThisIsSubtractive) {
5264
5269 }
5270
5271
5272
5273 return LoPartCmp;
5274}
5275
5277 return Floats[0].getCategory();
5278}
5279
5281
5283 Floats[0].makeInf(Neg);
5284 Floats[1].makeZero( false);
5285}
5286
5288 Floats[0].makeZero(Neg);
5289 Floats[1].makeZero( false);
5290}
5291
5293 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5294 "Unexpected Semantics");
5295 Floats[0] =
5296 APFloat(APFloatBase::semIEEEdouble, APInt(64, 0x7fefffffffffffffull));
5297 Floats[1] =
5298 APFloat(APFloatBase::semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull));
5299 if (Neg)
5301}
5302
5304 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5305 "Unexpected Semantics");
5306 Floats[0].makeSmallest(Neg);
5307 Floats[1].makeZero( false);
5308}
5309
5311 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5312 "Unexpected Semantics");
5313 Floats[0] =
5314 APFloat(APFloatBase::semIEEEdouble, APInt(64, 0x0360000000000000ull));
5315 if (Neg)
5316 Floats[0].changeSign();
5317 Floats[1].makeZero( false);
5318}
5319
5321 Floats[0].makeNaN(SNaN, Neg, fill);
5322 Floats[1].makeZero( false);
5323}
5324
5326 auto Result = Floats[0].compare(RHS.Floats[0]);
5327
5329 return Floats[1].compare(RHS.Floats[1]);
5330 return Result;
5331}
5332
5334 return Floats[0].bitwiseIsEqual(RHS.Floats[0]) &&
5335 Floats[1].bitwiseIsEqual(RHS.Floats[1]);
5336}
5337
5339 if (Arg.Floats)
5342}
5343
5345 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5346 "Unexpected Semantics");
5348 Floats[0].bitcastToAPInt().getRawData()[0],
5349 Floats[1].bitcastToAPInt().getRawData()[0],
5350 };
5352}
5353
5356 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5357 "Unexpected Semantics");
5358 APFloat Tmp(APFloatBase::semPPCDoubleDoubleLegacy);
5361 return Ret;
5362}
5363
5364
5365
5366
5367
5368
5369
5371 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5372 "Unexpected Semantics");
5373
5374 if (nextDown) {
5378 return Result;
5379 }
5382
5383
5386 return opOK;
5387
5389
5390
5391
5392 if (getFirst().isSignaling()) {
5393
5396 }
5397 return opOK;
5398
5400
5402 return opOK;
5403
5405 break;
5406 }
5407
5410
5412 NextLo.next(false);
5413
5414
5415
5416
5419 };
5420
5421
5422 if (InLattice(HiOld, NextLo)) {
5423
5424 Floats[1] = std::move(NextLo);
5425
5426
5427
5428
5429
5434
5435 return opOK;
5436 }
5437
5438
5439
5440
5442 NextHi.next(false);
5443
5444
5447 return opOK;
5448 }
5449
5450
5451
5452
5453 if (NextHi.isZero()) {
5455 return opOK;
5456 }
5457
5458
5459
5461 if (!InLattice(NextHi, NextLo))
5462
5463 NextLo.next(false);
5464
5465 Floats[0] = std::move(NextHi);
5466 Floats[1] = std::move(NextLo);
5467
5468 return opOK;
5469}
5470
5474 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5475 "Unexpected Semantics");
5476
5477
5478
5481
5482
5483
5484
5485 *IsExact = false;
5492
5493
5494
5495 bool HiIsExact;
5496 if (IntegralHi.isZero() || IntegralLo.isZero()) {
5499
5500
5501
5503 }
5504
5505
5506
5507 if (!IsSigned && IntegralHi.isNegative())
5509
5510
5511
5512
5513
5514
5515 bool LoIsExact;
5517 const unsigned PositiveOverflowWidth = IsSigned ? Width - 1 : Width;
5518 if (HiExactLog2 >= 0 &&
5519 static_cast<unsigned>(HiExactLog2) == PositiveOverflowWidth) {
5520
5521
5524
5525
5526
5527
5529 Input, Width, true, RM, &LoIsExact);
5532
5533
5534
5535
5536
5537
5538
5539
5540 if (IsSigned && !IntegralHi.isNegative())
5542 *IsExact = RoundStatus == opOK;
5543 return RoundStatus;
5544 }
5545
5546
5547
5551 return HiStatus;
5552
5553
5554 APSInt LoResult{Width, !IsSigned};
5558 return LoStatus;
5559
5560
5561
5562
5563
5565
5566 *IsExact = RoundStatus == opOK;
5567 return RoundStatus;
5568}
5569
5572 unsigned int Width, bool IsSigned,
5575 convertToSignExtendedInteger(Input, Width, IsSigned, RM, IsExact);
5576
5579 assert(DstPartsCount <= Input.size() && "Integer too big");
5580
5581 unsigned Bits;
5583 Bits = 0;
5585 Bits = IsSigned;
5586 else
5587 Bits = Width - IsSigned;
5588
5592 }
5593
5594 return FS;
5595}
5596
5598 switch (RM) {
5601 break;
5605 else
5607 break;
5611 else
5613 break;
5617 break;
5618 default:
5620 }
5624 return S;
5625}
5626
5629
5630
5631 const unsigned SrcMSB = APInt::tcMSB(Src, SrcCount);
5632 if (SrcMSB == UINT_MAX) {
5633
5635 return opOK;
5636 }
5637
5638
5639 const unsigned SrcBitWidth = SrcMSB + 1;
5640 APSInt SrcInt{APInt{SrcBitWidth, ArrayRef(Src, SrcCount)},
5641 true};
5642
5643
5644
5645
5646
5649
5650
5651
5652
5653
5654 if (.isFinite())
5655 return handleOverflow(RM);
5656
5657
5658
5659
5660
5661 bool HiAsIntIsExact;
5662
5663
5664
5665
5666 APSInt HiAsInt{static_cast<uint32_t>(ilogb(Hi) + 1), true};
5669
5670
5671
5672
5673
5674
5676
5677
5678 if (Error.isNegative()) {
5680
5681
5682
5683
5684
5685
5686 const unsigned ErrorActiveBits = Error.getSignificantBits() - 1;
5688 if (ErrorActiveBits > LoPrecision) {
5689 const unsigned RoundingBoundary = ErrorActiveBits - LoPrecision;
5690
5691
5692
5693 if (Error.countTrailingZeros() == RoundingBoundary - 1)
5695 }
5697
5698
5699
5700
5701
5703 }
5704 }
5705
5707 opStatus Status = Lo.convertFromAPInt(Error, true, LoRM);
5708
5709
5710
5712 Floats[0] = std::move(Hi);
5713 Floats[1] = std::move(Lo);
5714
5715
5716
5718 return handleOverflow(RM);
5719
5720
5721
5724 Largest.makeLargest(false);
5726 return handleOverflow(RM);
5727 }
5728
5729
5730
5731
5732 return Status;
5733}
5734
5736 bool IsSigned,
5738 const bool NegateInput = IsSigned && Input.isNegative();
5740 if (NegateInput)
5742
5745 if (NegateInput)
5748}
5749
5751 unsigned int HexDigits,
5752 bool UpperCase,
5754 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5755 "Unexpected Semantics");
5758}
5759
5762 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
5763
5764 Floats[0] != Floats[0] + Floats[1]);
5765}
5766
5774
5777 return false;
5778
5782}
5783
5791
5793 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5794 "Unexpected Semantics");
5795 return Floats[0].isInteger() && Floats[1].isInteger();
5796}
5797
5799 unsigned FormatPrecision,
5800 unsigned FormatMaxPadding,
5801 bool TruncateZero) const {
5802 assert(Semantics == &APFloatBase::semPPCDoubleDouble &&
5803 "Unexpected Semantics");
5805 .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
5806}
5807
5809
5810
5811
5813 return INT_MIN;
5815}
5816
5820 int IlogbResult = ilogb(Hi);
5821
5823 return IlogbResult;
5824
5825 if (Lo.isZero() || Hi.isNegative() == Lo.isNegative())
5826 return IlogbResult;
5827 if (Hi.getExactLog2Abs() == INT_MIN)
5828 return IlogbResult;
5829
5830
5831 return IlogbResult - 1;
5832}
5833
5837 "Unexpected Semantics");
5839 scalbn(Arg.Floats[0], Exp, RM),
5840 scalbn(Arg.Floats[1], Exp, RM));
5841}
5842
5846 "Unexpected Semantics");
5847
5848
5849
5850 Exp = ilogb(Arg);
5851
5852
5853
5856 Quiet.getFirst() = Quiet.getFirst().makeQuiet();
5858 }
5859
5860
5862 return Arg;
5863
5864
5866 Exp = 0;
5867 return Arg;
5868 }
5869
5872
5873
5874
5875
5876 ++Exp;
5877
5878 const bool SignsDisagree = Hi.isNegative() != Lo.isNegative();
5882
5883
5886
5887
5888
5889
5890
5891
5894 else
5895 LoRoundingMode = RM;
5896 Second = scalbn(Lo, -Exp, LoRoundingMode);
5897
5898
5899
5900
5902
5904 if (RecomposedLo != Lo) {
5905
5906
5907
5908 const APFloat RoundingError = RecomposedLo - Lo;
5909
5910
5911
5913 const APFloat ScaledUlpOfSecond =
5915 const bool IsMidpoint = abs(RoundingError) == ScaledUlpOfSecond;
5916 const bool RoundedLoAway =
5918
5919
5920
5921 if (IsMidpoint && RoundedLoAway)
5923 }
5924 }
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937 if (Second.isZero() && SignsDisagree && Hi.getExactLog2Abs() != INT_MIN)
5938 ++Exp;
5939 }
5940
5943 std::move(Second));
5944}
5945
5946}
5947
5948APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
5949 if (usesLayout(Semantics)) {
5951 return;
5952 }
5953 if (usesLayout(Semantics)) {
5954 const fltSemantics& S = F.getSemantics();
5957 return;
5958 }
5960}
5961
5966
5968 if (APFloat::usesLayoutdetail::IEEEFloat(Arg.getSemantics()))
5970 if (APFloat::usesLayoutdetail::DoubleAPFloat(Arg.getSemantics()))
5973}
5974
5978 assert(StatusOrErr && "Invalid floating point representation");
5980}
5981
5991 assert(isNaN() && "Other class of FP constant");
5993}
5994
5996
5997
5999 return false;
6000
6001
6002
6003
6005 return false;
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6025 if (Exp == INT_MIN)
6026 return false;
6027
6028
6029
6030 APFloat Reciprocal =
6033
6034
6035
6036
6038 return false;
6039
6040
6041
6043 return false;
6044
6046
6047 if (Inv)
6048 *Inv = std::move(Reciprocal);
6049
6050 return true;
6051}
6052
6056 *losesInfo = false;
6057 return opOK;
6058 }
6059 if (usesLayout(getSemantics()) &&
6060 usesLayout(ToSemantics))
6061 return U.IEEE.convert(ToSemantics, RM, losesInfo);
6062 if (usesLayout(getSemantics()) &&
6063 usesLayout(ToSemantics)) {
6064 assert(&ToSemantics == &APFloatBase::semPPCDoubleDouble);
6065 auto Ret =
6066 U.IEEE.convert(APFloatBase::semPPCDoubleDoubleLegacy, RM, losesInfo);
6067 *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
6068 return Ret;
6069 }
6070 if (usesLayout(getSemantics()) &&
6071 usesLayout(ToSemantics)) {
6072 auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo);
6073 *this = APFloat(std::move(getIEEE()), ToSemantics);
6074 return Ret;
6075 }
6077}
6078
6082
6088
6089#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
6092 dbgs() << '\n';
6093}
6094#endif
6095
6099
6102 bool *isExact) const {
6103 unsigned bitWidth = result.getBitWidth();
6106 rounding_mode, isExact);
6107
6108 result = APInt(bitWidth, parts);
6109 return status;
6110}
6111
6115 return getIEEE().convertToDouble();
6117 "Float semantics is not representable by IEEEdouble");
6118 APFloat Temp = *this;
6119 bool LosesInfo;
6122 assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision");
6123 (void)St;
6125}
6126
6127#ifdef HAS_IEE754_FLOAT128
6128float128 APFloat::convertToQuad() const {
6130 return getIEEE().convertToQuad();
6132 "Float semantics is not representable by IEEEquad");
6134 bool LosesInfo;
6137 assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision");
6138 (void)St;
6139 return Temp.getIEEE().convertToQuad();
6140}
6141#endif
6142
6146 return getIEEE().convertToFloat();
6148 "Float semantics is not representable by IEEEsingle");
6149 APFloat Temp = *this;
6150 bool LosesInfo;
6153 assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision");
6154 (void)St;
6156}
6157
6158APFloat::Storage::~Storage() {
6159 if (usesLayout(*semantics)) {
6160 IEEE.~IEEEFloat();
6161 return;
6162 }
6163 if (usesLayout(*semantics)) {
6165 return;
6166 }
6168}
6169
6170APFloat::Storage::Storage(const APFloat::Storage &RHS) {
6171 if (usesLayout(*RHS.semantics)) {
6173 return;
6174 }
6175 if (usesLayout(*RHS.semantics)) {
6177 return;
6178 }
6180}
6181
6182APFloat::Storage::Storage(APFloat::Storage &&RHS) {
6183 if (usesLayout(*RHS.semantics)) {
6185 return;
6186 }
6187 if (usesLayout(*RHS.semantics)) {
6189 return;
6190 }
6192}
6193
6194APFloat::Storage &APFloat::Storage::operator=(const APFloat::Storage &RHS) {
6195 if (usesLayout(*semantics) &&
6196 usesLayout(*RHS.semantics)) {
6198 } else if (usesLayout(*semantics) &&
6199 usesLayout(*RHS.semantics)) {
6201 } else if (this != &RHS) {
6202 this->~Storage();
6203 new (this) Storage(RHS);
6204 }
6205 return *this;
6206}
6207
6208APFloat::Storage &APFloat::Storage::operator=(APFloat::Storage &&RHS) {
6209 if (usesLayout(*semantics) &&
6210 usesLayout(*RHS.semantics)) {
6211 IEEE = std::move(RHS.IEEE);
6212 } else if (usesLayout(*semantics) &&
6213 usesLayout(*RHS.semantics)) {
6215 } else if (this != &RHS) {
6216 this->~Storage();
6217 new (this) Storage(std::move(RHS));
6218 }
6219 return *this;
6220}
6221
6222}
6223
6224#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)
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.
static bool isNeg(Value *V)
Returns true if the operation is a negation of V, and it works for both integers and floats.
static bool isSigned(unsigned int Opcode)
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)
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.
The Input class is used to parse a yaml document into in-memory structs and vectors.
static const fltSemantics & IEEEsingle()
static const fltSemantics & Float8E4M3FN()
static LLVM_ABI const llvm::fltSemantics & EnumToSemantics(Semantics S)
Definition APFloat.cpp:174
static LLVM_ABI bool semanticsHasInf(const fltSemantics &)
Definition APFloat.cpp:323
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
static constexpr roundingMode rmTowardZero
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:298
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
static const fltSemantics & BFloat()
static const fltSemantics & IEEEquad()
static LLVM_ABI unsigned int semanticsSizeInBits(const fltSemantics &)
Definition APFloat.cpp:301
static const fltSemantics & Float8E8M0FNU()
static LLVM_ABI bool semanticsHasSignedRepr(const fltSemantics &)
Definition APFloat.cpp:319
static const fltSemantics & IEEEdouble()
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:354
static const fltSemantics & x87DoubleExtended()
static constexpr roundingMode rmTowardNegative
static constexpr roundingMode rmNearestTiesToEven
static LLVM_ABI bool hasSignBitInMSB(const fltSemantics &)
Definition APFloat.cpp:336
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:294
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:290
static LLVM_ABI bool semanticsHasNaN(const fltSemantics &)
Definition APFloat.cpp:327
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:221
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
static constexpr unsigned integerPartWidth
static const fltSemantics & PPCDoubleDoubleLegacy()
APInt::WordType integerPart
static LLVM_ABI bool semanticsHasZero(const fltSemantics &)
Definition APFloat.cpp:315
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:340
static const fltSemantics & Float8E5M2FNUZ()
static const fltSemantics & Float8E4M3FNUZ()
static constexpr roundingMode rmTowardPositive
static const fltSemantics & IEEEhalf()
static const fltSemantics & Float4E2M1FN()
static const fltSemantics & Float6E2M3FN()
static const fltSemantics & Float8E4M3()
static const fltSemantics & Float8E4M3B11FNUZ()
static LLVM_ABI bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B)
Definition APFloat.cpp:266
static const fltSemantics & Float8E3M4()
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:331
static const fltSemantics & Float8E5M2()
fltCategory
Category of internally-represented number.
static constexpr roundingMode rmNearestTiesToAway
static const fltSemantics & PPCDoubleDouble()
@ S_PPCDoubleDoubleLegacy
static const fltSemantics & Float6E3M2FN()
opStatus
IEEE-754R 7: Default exception handling.
static const fltSemantics & FloatTF32()
static LLVM_ABI unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
Definition APFloat.cpp:304
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
LLVM_ABI void Profile(FoldingSetNodeID &NID) const
Used to insert APFloat objects, or objects that contain APFloat objects, into FoldingSets.
Definition APFloat.cpp:6096
opStatus divide(const APFloat &RHS, roundingMode RM)
bool isFiniteNonZero() const
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6053
LLVM_READONLY int getExactLog2Abs() const
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:5995
LLVM_ABI double convertToDouble() const
Converts this APFloat to host double value.
Definition APFloat.cpp:6112
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
opStatus add(const APFloat &RHS, roundingMode RM)
static LLVM_ABI APFloat getAllOnesValue(const fltSemantics &Semantics)
Returns a float which is bitcasted from an all one value int.
Definition APFloat.cpp:6079
LLVM_ABI friend hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition APFloat.cpp:5967
const fltSemantics & getSemantics() const
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
LLVM_ABI float convertToFloat() const
Converts this APFloat to host float value.
Definition APFloat.cpp:6143
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
opStatus remainder(const APFloat &RHS)
APInt bitcastToAPInt() const
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
opStatus next(bool nextDown)
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
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.
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition APFloat.cpp:5982
opStatus mod(const APFloat &RHS)
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:5962
LLVM_DUMP_METHOD void dump() const
Definition APFloat.cpp:6090
LLVM_ABI void print(raw_ostream &) const
Definition APFloat.cpp:6083
opStatus roundToIntegral(roundingMode RM)
static bool hasSignificand(const fltSemantics &Sem)
Returns true if the given semantics has actual significand.
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Class for arbitrary precision integers.
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
static LLVM_ABI void tcSetBit(WordType *, unsigned bit)
Set the given bit of a bignum. Zero-based.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
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.
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
static LLVM_ABI int tcExtractBit(const WordType *, unsigned bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
static LLVM_ABI WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
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,...
unsigned getActiveBits() const
Compute the number of active bits in the value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
static LLVM_ABI int tcCompare(const WordType *, const WordType *, unsigned)
Comparison (unsigned) of two bignums.
static APInt floatToBits(float V)
Converts a float to APInt bits.
static LLVM_ABI void tcAssign(WordType *, const WordType *, unsigned)
Assign one bignum to another.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static LLVM_ABI void tcShiftRight(WordType *, unsigned Words, unsigned Count)
Shift a bignum right Count bits.
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.
unsigned getNumWords() const
Get the number of words.
bool isNegative() const
Determine sign of this APInt.
static LLVM_ABI void tcClearBit(WordType *, unsigned bit)
Clear the given bit of a bignum. Zero-based.
void negate()
Negate this APInt in place.
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
unsigned countr_zero() const
Count the number of trailing zero bits.
static LLVM_ABI unsigned tcLSB(const WordType *, unsigned n)
Returns the bit number of the least or most significant set bit of a number.
static LLVM_ABI void tcShiftLeft(WordType *, unsigned Words, unsigned Count)
Shift a bignum left Count bits.
static LLVM_ABI bool tcIsZero(const WordType *, unsigned)
Returns true if a bignum is zero, false otherwise.
static LLVM_ABI unsigned tcMSB(const WordType *parts, unsigned n)
Returns the bit number of the most significant set bit of a number.
float bitsToFloat() const
Converts APInt bits to a float.
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.
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
static LLVM_ABI WordType tcSubtract(WordType *, const WordType *, WordType carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static LLVM_ABI void tcNegate(WordType *, unsigned)
Negate a bignum in-place.
static APInt doubleToBits(double V)
Converts a double to APInt bits.
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
double bitsToDouble() const
Converts APInt bits to a double.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
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.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
constexpr bool empty() const
empty - Check if the string is empty.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
char back() const
back - Get the last character in the string.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
size - Get the string size.
char front() const
front - Get the first character in the string.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI void makeSmallestNormalized(bool Neg)
Definition APFloat.cpp:5310
LLVM_ABI DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
Definition APFloat.cpp:4840
LLVM_ABI void changeSign()
Definition APFloat.cpp:5217
LLVM_ABI bool isLargest() const
Definition APFloat.cpp:5784
LLVM_ABI opStatus remainder(const DoubleAPFloat &RHS)
Definition APFloat.cpp:5104
LLVM_ABI opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5007
LLVM_ABI fltCategory getCategory() const
Definition APFloat.cpp:5276
LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5333
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.cpp:5808
LLVM_ABI opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.cpp:5735
LLVM_ABI APInt bitcastToAPInt() const
Definition APFloat.cpp:5344
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:5354
LLVM_ABI bool isSmallest() const
Definition APFloat.cpp:5767
LLVM_ABI opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:4999
LLVM_ABI friend hash_code hash_value(const DoubleAPFloat &Arg)
Definition APFloat.cpp:5338
LLVM_ABI cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5223
LLVM_ABI bool isDenormal() const
Definition APFloat.cpp:5760
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.cpp:5571
LLVM_ABI void makeSmallest(bool Neg)
Definition APFloat.cpp:5303
LLVM_ABI friend int ilogb(const DoubleAPFloat &X)
Definition APFloat.cpp:5817
LLVM_ABI opStatus next(bool nextDown)
Definition APFloat.cpp:5370
LLVM_ABI void makeInf(bool Neg)
Definition APFloat.cpp:5282
LLVM_ABI bool isInteger() const
Definition APFloat.cpp:5792
LLVM_ABI void makeZero(bool Neg)
Definition APFloat.cpp:5287
LLVM_ABI opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:5093
LLVM_ABI bool isSmallestNormalized() const
Definition APFloat.cpp:5775
LLVM_ABI opStatus mod(const DoubleAPFloat &RHS)
Definition APFloat.cpp:5114
LLVM_ABI DoubleAPFloat(const fltSemantics &S)
Definition APFloat.cpp:4787
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
Definition APFloat.cpp:5798
LLVM_ABI void makeLargest(bool Neg)
Definition APFloat.cpp:5292
LLVM_ABI cmpResult compare(const DoubleAPFloat &RHS) const
Definition APFloat.cpp:5325
LLVM_ABI friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode)
LLVM_ABI opStatus roundToIntegral(roundingMode RM)
Definition APFloat.cpp:5140
LLVM_ABI opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
Definition APFloat.cpp:5125
LLVM_ABI unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
Definition APFloat.cpp:5750
LLVM_ABI bool isNegative() const
Definition APFloat.cpp:5280
LLVM_ABI opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
Definition APFloat.cpp:4994
LLVM_ABI void makeNaN(bool SNaN, bool Neg, const APInt *fill)
Definition APFloat.cpp:5320
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:3323
LLVM_ABI cmpResult compareAbsoluteValue(const IEEEFloat &) const
Definition APFloat.cpp:1541
LLVM_ABI opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
Definition APFloat.cpp:2312
fltCategory getCategory() const
LLVM_ABI opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition APFloat.cpp:2881
bool isFiniteNonZero() const
bool needsCleanup() const
Returns whether this instance allocated memory.
LLVM_ABI void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
Definition APFloat.cpp:4109
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.cpp:4504
LLVM_ABI APInt bitcastToAPInt() const
Definition APFloat.cpp:3733
LLVM_ABI friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition APFloat.cpp:4747
LLVM_ABI cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition APFloat.cpp:2483
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
LLVM_ABI opStatus divide(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2182
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
LLVM_ABI opStatus remainder(const IEEEFloat &)
IEEE remainder.
Definition APFloat.cpp:2202
LLVM_ABI double convertToDouble() const
Definition APFloat.cpp:3803
LLVM_ABI float convertToFloat() const
Definition APFloat.cpp:3796
LLVM_ABI opStatus subtract(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2156
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:4460
LLVM_ABI ~IEEEFloat()
Definition APFloat.cpp:1220
LLVM_ABI void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
Definition APFloat.cpp:4141
LLVM_ABI void makeInf(bool Neg=false)
Definition APFloat.cpp:4694
LLVM_ABI bool isSmallestNormalized() const
Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.cpp:1041
LLVM_ABI void makeQuiet()
Definition APFloat.cpp:4723
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:1143
LLVM_ABI opStatus add(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2150
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
Definition APFloat.cpp:3266
LLVM_ABI void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
Definition APFloat.cpp:930
LLVM_ABI opStatus multiply(const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2162
LLVM_ABI opStatus roundToIntegral(roundingMode)
Definition APFloat.cpp:2396
LLVM_ABI IEEEFloat & operator=(const IEEEFloat &)
Definition APFloat.cpp:1002
LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition APFloat.cpp:1168
LLVM_ABI void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.cpp:4155
LLVM_ABI bool isInteger() const
Returns true if and only if the number is an exact integer.
Definition APFloat.cpp:1160
LLVM_ABI IEEEFloat(const fltSemantics &)
Definition APFloat.cpp:1195
LLVM_ABI opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
Definition APFloat.cpp:2350
LLVM_ABI friend int ilogb(const IEEEFloat &Arg)
Definition APFloat.cpp:4729
LLVM_ABI opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
Definition APFloat.cpp:4549
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
const fltSemantics & getSemantics() const
bool isZero() const
Returns true if and only if the float is plus or minus zero.
LLVM_ABI bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
Definition APFloat.cpp:4533
LLVM_ABI void makeZero(bool Neg=false)
Definition APFloat.cpp:4709
LLVM_ABI opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
Definition APFloat.cpp:2560
LLVM_ABI void changeSign()
Definition APFloat.cpp:2106
LLVM_ABI bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
Definition APFloat.cpp:1027
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
Definition APFloat.cpp:2821
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:1033
An opaque object representing a hash code.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
static constexpr opStatus opInexact
LLVM_ABI SlowDynamicAPInt abs(const SlowDynamicAPInt &X)
Redeclarations of friend declarations above to make it discoverable by lookups.
static constexpr fltCategory fcNaN
static constexpr opStatus opDivByZero
static constexpr opStatus opOverflow
static constexpr cmpResult cmpLessThan
const char unit< Period >::value[]
static void tcSetLeastSignificantBits(APInt::WordType *dst, unsigned parts, unsigned bits)
Definition APFloat.cpp:1566
static constexpr roundingMode rmTowardPositive
static constexpr uninitializedTag uninitialized
static constexpr fltCategory fcZero
static constexpr opStatus opOK
static constexpr cmpResult cmpGreaterThan
static constexpr unsigned integerPartWidth
LLVM_ABI hash_code hash_value(const IEEEFloat &Arg)
Definition APFloat.cpp:3471
APFloatBase::ExponentType ExponentType
static constexpr fltCategory fcNormal
static constexpr opStatus opInvalidOp
APFloatBase::opStatus opStatus
LLVM_ABI IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM)
Definition APFloat.cpp:4768
APFloatBase::uninitializedTag uninitializedTag
static constexpr cmpResult cmpUnordered
static constexpr roundingMode rmTowardNegative
APFloatBase::roundingMode roundingMode
APFloatBase::cmpResult cmpResult
static constexpr fltCategory fcInfinity
static constexpr roundingMode rmNearestTiesToAway
static constexpr roundingMode rmTowardZero
static constexpr opStatus opUnderflow
static constexpr roundingMode rmNearestTiesToEven
LLVM_ABI int ilogb(const IEEEFloat &Arg)
Definition APFloat.cpp:4729
static constexpr cmpResult cmpEqual
LLVM_ABI IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Definition APFloat.cpp:4747
static std::pair< APFloat, APFloat > fastTwoSum(APFloat X, APFloat Y)
Definition APFloat.cpp:4857
APFloatBase::integerPart integerPart
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.
static unsigned int partAsHex(char *dst, APFloatBase::integerPart part, unsigned int count, const char *hexDigitChars)
Definition APFloat.cpp:821
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
static const char infinityL[]
Definition APFloat.cpp:812
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
static constexpr unsigned int partCountForBits(unsigned int bits)
Definition APFloat.cpp:385
static const char NaNU[]
Definition APFloat.cpp:815
static unsigned int HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
Definition APFloat.cpp:696
static unsigned int powerOf5(APFloatBase::integerPart *dst, unsigned int power)
Definition APFloat.cpp:755
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:872
static constexpr APFloatBase::ExponentType exponentZero(const fltSemantics &semantics)
Definition APFloat.cpp:359
static Expected< int > totalExponent(StringRef::iterator p, StringRef::iterator end, int exponentAdjustment)
Definition APFloat.cpp:447
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
const unsigned int maxPowerOfFiveExponent
Definition APFloat.cpp:285
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
static char * writeUnsignedDecimal(char *dst, unsigned int n)
Definition APFloat.cpp:839
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
const unsigned int maxPrecision
Definition APFloat.cpp:284
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
static const char NaNL[]
Definition APFloat.cpp:814
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
static const char infinityU[]
Definition APFloat.cpp:813
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
static Error interpretDecimal(StringRef::iterator begin, StringRef::iterator end, decimalInfo *D)
Definition APFloat.cpp:539
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:286
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static constexpr APFloatBase::ExponentType exponentNaN(const fltSemantics &semantics)
Definition APFloat.cpp:369
static Error createError(const Twine &Err)
Definition APFloat.cpp:381
static lostFraction shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits)
Definition APFloat.cpp:662
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
static const char hexDigitsUpper[]
Definition APFloat.cpp:811
FunctionAddr VTableAddr uintptr_t uintptr_t Data
const unsigned int maxExponent
Definition APFloat.cpp:283
static unsigned int decDigitValue(unsigned int c)
Definition APFloat.cpp:392
fltNonfiniteBehavior
Definition APFloat.cpp:57
@ IEEE754
Definition APFloat.cpp:61
@ NanOnly
Definition APFloat.cpp:70
@ FiniteOnly
Definition APFloat.cpp:74
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...
static lostFraction combineLostFractions(lostFraction moreSignificant, lostFraction lessSignificant)
Definition APFloat.cpp:675
static Expected< StringRef::iterator > skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, StringRef::iterator *dot)
Definition APFloat.cpp:499
RoundingMode
Rounding mode.
ArrayRef(const T &OneElt) -> ArrayRef< T >
static constexpr APFloatBase::ExponentType exponentInf(const fltSemantics &semantics)
Definition APFloat.cpp:364
static lostFraction lostFractionThroughTruncation(const APFloatBase::integerPart *parts, unsigned int partCount, unsigned int bits)
Definition APFloat.cpp:640
APFloat neg(APFloat X)
Returns the negated value of the argument.
static APFloatBase::integerPart ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, bool isNearest)
Definition APFloat.cpp:710
static char * writeSignedDecimal(char *dst, int value)
Definition APFloat.cpp:857
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
static Expected< lostFraction > trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, unsigned int digitValue)
Definition APFloat.cpp:609
void consumeError(Error Err)
Consume a Error without doing anything.
fltNanEncoding
Definition APFloat.cpp:81
@ NegativeZero
Definition APFloat.cpp:99
@ AllOnes
Definition APFloat.cpp:92
@ IEEE
Definition APFloat.cpp:84
static Expected< int > readExponent(StringRef::iterator begin, StringRef::iterator end)
Definition APFloat.cpp:402
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
static const char hexDigitsLower[]
Definition APFloat.cpp:810
Definition APFloat.cpp:532
int normalizedExponent
Definition APFloat.cpp:536
int exponent
Definition APFloat.cpp:535
const char * lastSigDigit
Definition APFloat.cpp:534
const char * firstSigDigit
Definition APFloat.cpp:533
Definition APFloat.cpp:103
bool hasSignBitInMSB
Definition APFloat.cpp:130
APFloatBase::ExponentType maxExponent
Definition APFloat.cpp:106
fltNonfiniteBehavior nonFiniteBehavior
Definition APFloat.cpp:119
APFloatBase::ExponentType minExponent
Definition APFloat.cpp:110
bool hasSignedRepr
Definition APFloat.cpp:127
unsigned int sizeInBits
Definition APFloat.cpp:117
unsigned int precision
Definition APFloat.cpp:114
bool hasZero
Definition APFloat.cpp:124
fltNanEncoding nanEncoding
Definition APFloat.cpp:121