llvm.org GIT mirror llvm / 3f6eb74
Add APInt interfaces to APFloat (allows directly access to bits). Use them in place of float and double interfaces where appropriate. First bits of x86 long double constants handling (untested, probably does not work). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41858 91177308-0d34-0410-b5e6-96231b3b80d8 Dale Johannesen 12 years ago
16 changed file(s) with 313 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
169169 APFloat(const fltSemantics &, fltCategory, bool negative);
170170 APFloat(double d);
171171 APFloat(float f);
172 APFloat(const APInt &);
172173 APFloat(const APFloat &);
173174 ~APFloat();
174175
190191 opStatus convertFromInteger(const integerPart *, unsigned int, bool,
191192 roundingMode);
192193 opStatus convertFromString(const char *, roundingMode);
194 APInt convertToAPInt() const;
193195 double convertToDouble() const;
194196 float convertToFloat() const;
195197
255257 roundingMode);
256258 lostFraction combineLostFractions(lostFraction, lostFraction);
257259 opStatus convertFromHexadecimalString(const char *, roundingMode);
260 APInt convertFloatAPFloatToAPInt() const;
261 APInt convertDoubleAPFloatToAPInt() const;
262 APInt convertF80LongDoubleAPFloatToAPInt() const;
263 void initFromAPInt(const APInt& api);
264 void initFromFloatAPInt(const APInt& api);
265 void initFromDoubleAPInt(const APInt& api);
266 void initFromF80LongDoubleAPInt(const APInt& api);
258267
259268 void assign(const APFloat &);
260269 void copySignificand(const APFloat &);
9090 return Result;
9191 }
9292
93
94 // HexToFP - Convert the ascii string in hexidecimal format to the floating
93 // HexToFP - Convert the ascii string in hexadecimal format to the floating
9594 // point representation of it.
9695 //
9796 static double HexToFP(const char *Buffer) {
9897 return BitsToDouble(HexIntToVal(Buffer)); // Cast Hex constant to double
9998 }
10099
100 static void HexToIntPair(const char *Buffer, uint64_t Pair[2]) {
101 Pair[0] = 0;
102 for (int i=0; i<16; i++, Buffer++) {
103 assert(*Buffer);
104 Pair[0] *= 16;
105 char C = *Buffer;
106 if (C >= '0' && C <= '9')
107 Pair[0] += C-'0';
108 else if (C >= 'A' && C <= 'F')
109 Pair[0] += C-'A'+10;
110 else if (C >= 'a' && C <= 'f')
111 Pair[0] += C-'a'+10;
112 }
113 Pair[1] = 0;
114 for (int i=0; i<16 && *Buffer; i++, Buffer++) {
115 Pair[1] *= 16;
116 char C = *Buffer;
117 if (C >= '0' && C <= '9')
118 Pair[1] += C-'0';
119 else if (C >= 'A' && C <= 'F')
120 Pair[1] += C-'A'+10;
121 else if (C >= 'a' && C <= 'f')
122 Pair[1] += C-'a'+10;
123 }
124 if (*Buffer)
125 GenerateError("constant bigger than 128 bits detected!");
126 }
101127
102128 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
103129 // appropriate character.
162188 PInteger [0-9]+
163189 NInteger -[0-9]+
164190
165 /* FPConstant - A Floating point constant.
191 /* FPConstant - A Floating point constant. Float and double only.
166192 */
167193 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
168194
169195 /* HexFPConstant - Floating point constant represented in IEEE format as a
170196 * hexadecimal number for when exponential notation is not precise enough.
197 * Float and double only.
171198 */
172199 HexFPConstant 0x[0-9A-Fa-f]+
200
201 /* F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
202 */
203 HexFP80Constant 0xK[0-9A-Fa-f]+
204
205 /* F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
206 */
207 HexFP128Constant 0xL[0-9A-Fa-f]+
208
209 /* PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
210 */
211 HexPPC128Constant 0xM[0-9A-Fa-f]+
173212
174213 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
175214 * it to deal with 64 bit numbers.
440479 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext));
441480 return FPVAL;
442481 }
482 {HexFP80Constant} { uint64_t Pair[2];
483 HexToIntPair(yytext, Pair);
484 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
485 return FPVAL;
486 }
487 {HexFP128Constant} { uint64_t Pair[2];
488 HexToIntPair(yytext, Pair);
489 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
490 return FPVAL;
491 }
492 {HexPPC128Constant} { uint64_t Pair[2];
493 HexToIntPair(yytext, Pair);
494 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
495 return FPVAL;
496 }
443497
444498 <> {
445499 /* Make sure to free the internal buffers for flex when we are
415415 GenerateError("FP constant invalid for type");
416416 return 0;
417417 }
418 // Lexer has no type info, so builds all FP constants as double.
419 // Fix this here.
420 if (Ty==Type::FloatTy)
418 // Lexer has no type info, so builds all float and double FP constants
419 // as double. Fix this here. Long double does not need this.
420 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
421 Ty==Type::FloatTy)
421422 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
422423 return ConstantFP::get(Ty, *D.ConstPoolFP);
423424
18671868 | FPType FPVAL { // Float & Double constants
18681869 if (!ConstantFP::isValueValidForType($1, *$2))
18691870 GEN_ERROR("Floating point constant invalid for type");
1870 // Lexer has no type info, so builds all FP constants as double.
1871 // Fix this here.
1872 if ($1==Type::FloatTy)
1871 // Lexer has no type info, so builds all float and double FP constants
1872 // as double. Fix this here. Long double is done right.
1873 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
18731874 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
18741875 $$ = ConstantFP::get($1, *$2);
18751876 delete $2;
621621 NumWords, &Words[0]));
622622 break;
623623 }
624 case bitc::CST_CODE_FLOAT: // FLOAT: [fpval]
624 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
625625 if (Record.empty())
626626 return Error("Invalid FLOAT record");
627627 if (CurTy == Type::FloatTy)
628 V = ConstantFP::get(CurTy, APFloat(BitsToFloat(Record[0])));
628 V = ConstantFP::get(CurTy, APFloat(APInt(32, (uint32_t)Record[0])));
629629 else if (CurTy == Type::DoubleTy)
630 V = ConstantFP::get(CurTy, APFloat(BitsToDouble(Record[0])));
630 V = ConstantFP::get(CurTy, APFloat(APInt(64, Record[0])));
631631 // FIXME: Make long double constants work. BitsToDouble does not make it.
632632 else if (CurTy == Type::X86_FP80Ty)
633 V = ConstantFP::get(CurTy, APFloat(BitsToDouble(Record[0])));
633 V = ConstantFP::get(CurTy, APFloat(APInt(80, 2, &Record[0])));
634634 else if (CurTy == Type::FP128Ty)
635 V = ConstantFP::get(CurTy, APFloat(BitsToDouble(Record[0])));
635 V = ConstantFP::get(CurTy, APFloat(APInt(128, 2, &Record[0])));
636636 else if (CurTy == Type::PPC_FP128Ty)
637637 assert(0 && "PowerPC long double constants not handled yet.");
638638 else
639639 V = UndefValue::get(CurTy);
640640 break;
641 }
641642
642643 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
643644 if (Record.empty())
526526 Code = bitc::CST_CODE_FLOAT;
527527 const Type *Ty = CFP->getType();
528528 if (Ty == Type::FloatTy)
529 Record.push_back(FloatToBits(CFP->getValueAPF().convertToFloat()));
529 Record.push_back((uint32_t)*CFP->getValueAPF().convertToAPInt().
530 getRawData());
530531 else if (Ty == Type::DoubleTy) {
531 Record.push_back(DoubleToBits(CFP->getValueAPF().convertToDouble()));
532 // FIXME: make long double constants work.
533 } else if (Ty == Type::X86_FP80Ty ||
534 Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) {
535 assert (0 && "Long double constants not handled yet.");
532 Record.push_back(*CFP->getValueAPF().convertToAPInt().getRawData());
533 } else if (Ty == Type::X86_FP80Ty) {
534 const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
535 Record.push_back(p[0]);
536 Record.push_back((uint16_t)p[1]);
537 } else if (Ty == Type::FP128Ty) {
538 const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
539 Record.push_back(p[0]);
540 Record.push_back(p[1]);
541 } else if (Ty == Type::PPC_FP128Ty) {
542 assert(0 && "PowerPC long double constants not handled yet.");
536543 } else {
537544 assert (0 && "Unknown FP type!");
538545 }
829829 // FP Constants are printed as integer constants to avoid losing
830830 // precision...
831831 if (CFP->getType() == Type::DoubleTy) {
832 double Val = CFP->getValueAPF().convertToDouble();
832 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
833 uint64_t i = *CFP->getValueAPF().convertToAPInt().getRawData();
833834 if (TAI->getData64bitsDirective())
834 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
835 O << TAI->getData64bitsDirective() << i << "\t"
835836 << TAI->getCommentString() << " double value: " << Val << "\n";
836837 else if (TD->isBigEndian()) {
837 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
838 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
838839 << "\t" << TAI->getCommentString()
839840 << " double most significant word " << Val << "\n";
840 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
841 O << TAI->getData32bitsDirective() << unsigned(i)
841842 << "\t" << TAI->getCommentString()
842843 << " double least significant word " << Val << "\n";
843844 } else {
844 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
845 O << TAI->getData32bitsDirective() << unsigned(i)
845846 << "\t" << TAI->getCommentString()
846847 << " double least significant word " << Val << "\n";
847 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
848 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
848849 << "\t" << TAI->getCommentString()
849850 << " double most significant word " << Val << "\n";
850851 }
851852 return;
852853 } else {
853 float Val = CFP->getValueAPF().convertToFloat();
854 O << TAI->getData32bitsDirective() << FloatToBits(Val)
854 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
855 O << TAI->getData32bitsDirective()
856 << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData()
855857 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
856858 return;
857859 }
860860 break;
861861 }
862862 case Type::FloatTyID: {
863 uint64_t val = FloatToBits(cast(PC)->
864 getValueAPF().convertToFloat());
863 uint32_t val = (uint32_t)*cast(PC)->
864 getValueAPF().convertToAPInt().getRawData();
865865 if (TD->isBigEndian())
866866 val = ByteSwap_32(val);
867867 ptr[0] = val;
871871 break;
872872 }
873873 case Type::DoubleTyID: {
874 uint64_t val = DoubleToBits(cast(PC)->
875 getValueAPF().convertToDouble());
874 uint64_t val = *cast(PC)->getValueAPF().convertToAPInt().
875 getRawData();
876876 if (TD->isBigEndian())
877877 val = ByteSwap_64(val);
878878 ptr[0] = val;
37993799 default: assert(0 && "Unknown FP type");
38003800 case MVT::f32:
38013801 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3802 Tmp = DAG.getConstant(FloatToBits(CFP->getValueAPF().convertToFloat()), MVT::i32);
3802 Tmp = DAG.getConstant((uint32_t)*CFP->getValueAPF().
3803 convertToAPInt().getRawData(), MVT::i32);
38033804 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
38043805 ST->getSrcValueOffset(), ST->isVolatile(),
38053806 ST->getAlignment());
38073808 break;
38083809 case MVT::f64:
38093810 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3810 Tmp = DAG.getConstant(DoubleToBits(CFP->getValueAPF().convertToDouble()), MVT::i64);
3811 Tmp = DAG.getConstant(*CFP->getValueAPF().convertToAPInt().
3812 getRawData(), MVT::i64);
38113813 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
38123814 ST->getSrcValueOffset(), ST->isVolatile(),
38133815 ST->getAlignment());
38153817 // Many FP stores are not make apparent until after legalize, e.g. for
38163818 // argument passing. Since this is so common, custom legalize the
38173819 // 64-bit integer store into two 32-bit stores.
3818 uint64_t Val = DoubleToBits(CFP->getValueAPF().convertToDouble());
3820 uint64_t Val = *CFP->getValueAPF().convertToAPInt().getRawData();
38193821 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
38203822 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
38213823 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
490490 if (!UseCP) {
491491 const APFloat& Val = LLVMC->getValueAPF();
492492 return isDouble
493 ? DAG.getConstant(DoubleToBits(Val.convertToDouble()), MVT::i64)
494 : DAG.getConstant(FloatToBits(Val.convertToFloat()), MVT::i32);
493 ? DAG.getConstant(*Val.convertToAPInt().getRawData(), MVT::i64)
494 : DAG.getConstant((uint32_t )*Val.convertToAPInt().getRawData(),
495 MVT::i32);
495496 }
496497
497498 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
19791980 // together.
19801981 if (ConstantFPSDNode *CFP = dyn_cast(ST->getValue())) {
19811982 if (CFP->getValueType(0) == MVT::f32) {
1982 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValueAPF().
1983 convertToFloat()), MVT::i32);
1983 Tmp3 = DAG.getConstant((uint32_t)*CFP->getValueAPF().
1984 convertToAPInt().getRawData(),
1985 MVT::i32);
19841986 } else {
19851987 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1986 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValueAPF().
1987 convertToDouble()), MVT::i64);
1988 Tmp3 = DAG.getConstant(*CFP->getValueAPF().convertToAPInt().
1989 getRawData(), MVT::i64);
19881990 }
19891991 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
19901992 SVOffset, isVolatile, Alignment);
108108 } else if (isa(NotZero)) {
109109 MVT::ValueType VT = NotZero.getValueType();
110110 if (VT== MVT::f64) {
111 if (DoubleToBits(cast(NotZero)->
112 getValueAPF().convertToDouble()) !=
113 (uint64_t)-1)
111 if (*((cast(NotZero)->getValueAPF().
112 convertToAPInt().getRawData())) != (uint64_t)-1)
114113 return false;
115114 } else {
116 if (FloatToBits(cast(NotZero)->
117 getValueAPF().convertToFloat()) !=
115 if ((uint32_t)*cast(NotZero)->
116 getValueAPF().convertToAPInt().getRawData() !=
118117 (uint32_t)-1)
119118 return false;
120119 }
16971696 }
16981697 case ISD::BIT_CONVERT:
16991698 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1700 return getConstant(FloatToBits(V.convertToFloat()), VT);
1699 return getConstant((uint32_t)*V.convertToAPInt().getRawData(), VT);
17011700 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1702 return getConstant(DoubleToBits(V.convertToDouble()), VT);
1701 return getConstant(*V.convertToAPInt().getRawData(), VT);
17031702 break;
17041703 }
17051704 }
338338 unsigned int
339339 APFloat::partCount() const
340340 {
341 return partCountForBits(semantics->precision + 1);
341 return partCountForBits(semantics->precision +
342 semantics->implicitIntegerBit ? 1 : 0);
342343 }
343344
344345 unsigned int
15921593 // Denormals have exponent minExponent in APFloat, but minExponent-1 in
15931594 // the actual IEEE respresentation. We compensate for that here.
15941595
1595 double
1596 APFloat::convertToDouble() const {
1596 APInt
1597 APFloat::convertF80LongDoubleAPFloatToAPInt() const {
1598 assert(semantics == (const llvm::fltSemantics* const)&x87DoubleExtended);
1599 assert (partCount()==1);
1600
1601 uint64_t myexponent, mysignificand;
1602
1603 if (category==fcNormal) {
1604 myexponent = exponent+16383; //bias
1605 mysignificand = *significandParts();
1606 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
1607 myexponent = 0; // denormal
1608 } else if (category==fcZero) {
1609 myexponent = 0;
1610 mysignificand = 0;
1611 } else if (category==fcInfinity) {
1612 myexponent = 0x7fff;
1613 mysignificand = 0x8000000000000000ULL;
1614 } else if (category==fcNaN) {
1615 myexponent = 0x7fff;
1616 mysignificand = *significandParts();
1617 } else
1618 assert(0);
1619
1620 uint64_t words[2];
1621 words[0] = (((uint64_t)sign & 1) << 63) |
1622 ((myexponent & 0x7fff) << 48) |
1623 ((mysignificand >>16) & 0xffffffffffffLL);
1624 words[1] = mysignificand & 0xffff;
1625 APInt api(80, 2, words);
1626 return api;
1627 }
1628
1629 APInt
1630 APFloat::convertDoubleAPFloatToAPInt() const {
15971631 assert(semantics == (const llvm::fltSemantics* const)&IEEEdouble);
15981632 assert (partCount()==1);
15991633
16161650 } else
16171651 assert(0);
16181652
1619 return BitsToDouble((((uint64_t)sign & 1) << 63) |
1620 ((myexponent & 0x7ff) << 52) |
1621 (mysignificand & 0xfffffffffffffLL));
1622 }
1623
1624 float
1625 APFloat::convertToFloat() const {
1653 APInt api(64, (((((uint64_t)sign & 1) << 63) |
1654 ((myexponent & 0x7ff) << 52) |
1655 (mysignificand & 0xfffffffffffffLL))));
1656 return api;
1657 }
1658
1659 APInt
1660 APFloat::convertFloatAPFloatToAPInt() const {
16261661 assert(semantics == (const llvm::fltSemantics* const)&IEEEsingle);
16271662 assert (partCount()==1);
1628
1663
16291664 uint32_t myexponent, mysignificand;
16301665
16311666 if (category==fcNormal) {
16451680 } else
16461681 assert(0);
16471682
1648 return BitsToFloat(((sign&1) << 31) | ((myexponent&0xff) << 23) |
1649 (mysignificand & 0x7fffff));
1650 }
1651
1652 APFloat::APFloat(double d) {
1653 uint64_t i = DoubleToBits(d);
1683 APInt api(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
1684 (mysignificand & 0x7fffff)));
1685 return api;
1686 }
1687
1688 APInt
1689 APFloat::convertToAPInt() const {
1690 if (semantics == (const llvm::fltSemantics* const)&IEEEsingle)
1691 return convertFloatAPFloatToAPInt();
1692 else if (semantics == (const llvm::fltSemantics* const)&IEEEdouble)
1693 return convertDoubleAPFloatToAPInt();
1694 else if (semantics == (const llvm::fltSemantics* const)&x87DoubleExtended)
1695 return convertF80LongDoubleAPFloatToAPInt();
1696 else
1697 assert(0);
1698 }
1699
1700 float
1701 APFloat::convertToFloat() const {
1702 assert(semantics == (const llvm::fltSemantics* const)&IEEEsingle);
1703 APInt api = convertToAPInt();
1704 return api.bitsToFloat();
1705 }
1706
1707 double
1708 APFloat::convertToDouble() const {
1709 assert(semantics == (const llvm::fltSemantics* const)&IEEEdouble);
1710 APInt api = convertToAPInt();
1711 return api.bitsToDouble();
1712 }
1713
1714 /// Integer bit is explicit in this format. Current Intel book does not
1715 /// define meaning of:
1716 /// exponent = all 1's, integer bit not set.
1717 /// exponent = 0, integer bit set. (formerly "psuedodenormals")
1718 /// exponent!=0 nor all 1's, integer bit not set. (formerly "unnormals")
1719 void
1720 APFloat::initFromF80LongDoubleAPInt(const APInt &api) {
1721 assert(api.getBitWidth()==80);
1722 uint64_t i1 = api.getRawData()[0];
1723 uint64_t i2 = api.getRawData()[1];
1724 uint64_t myexponent = (i1 >> 48) & 0x7fff;
1725 uint64_t mysignificand = ((i1 << 16) & 0xffffffffffff0000ULL) |
1726 (i2 & 0xffff);
1727
1728 initialize(&APFloat::x87DoubleExtended);
1729 assert(partCount()==1);
1730
1731 sign = i1>>63;
1732 if (myexponent==0 && mysignificand==0) {
1733 // exponent, significand meaningless
1734 category = fcZero;
1735 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
1736 // exponent, significand meaningless
1737 category = fcInfinity;
1738 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
1739 // exponent meaningless
1740 category = fcNaN;
1741 *significandParts() = mysignificand;
1742 } else {
1743 category = fcNormal;
1744 exponent = myexponent - 16383;
1745 *significandParts() = mysignificand;
1746 if (myexponent==0) // denormal
1747 exponent = -16382;
1748 }
1749 }
1750
1751 void
1752 APFloat::initFromDoubleAPInt(const APInt &api) {
1753 assert(api.getBitWidth()==64);
1754 uint64_t i = *api.getRawData();
16541755 uint64_t myexponent = (i >> 52) & 0x7ff;
16551756 uint64_t mysignificand = i & 0xfffffffffffffLL;
16561757
16791780 }
16801781 }
16811782
1682 APFloat::APFloat(float f) {
1683 uint32_t i = FloatToBits(f);
1783 void
1784 APFloat::initFromFloatAPInt(const APInt & api) {
1785 assert(api.getBitWidth()==32);
1786 uint32_t i = (uint32_t)*api.getRawData();
16841787 uint32_t myexponent = (i >> 23) & 0xff;
16851788 uint32_t mysignificand = i & 0x7fffff;
16861789
17081811 *significandParts() |= 0x800000; // integer bit
17091812 }
17101813 }
1814
1815 /// Treat api as containing the bits of a floating point number. Currently
1816 /// we infer the floating point type from the size of the APInt. FIXME: This
1817 /// breaks when we get to PPC128 and IEEE128 (but both cannot exist in the
1818 /// same compile...)
1819 void
1820 APFloat::initFromAPInt(const APInt& api) {
1821 if (api.getBitWidth() == 32)
1822 return initFromFloatAPInt(api);
1823 else if (api.getBitWidth()==64)
1824 return initFromDoubleAPInt(api);
1825 else if (api.getBitWidth()==80)
1826 return initFromF80LongDoubleAPInt(api);
1827 else
1828 assert(0);
1829 }
1830
1831 APFloat::APFloat(const APInt& api) {
1832 initFromAPInt(api);
1833 }
1834
1835 APFloat::APFloat(float f) {
1836 APInt api = APInt(32, 0);
1837 initFromAPInt(api.floatToBits(f));
1838 }
1839
1840 APFloat::APFloat(double d) {
1841 APInt api = APInt(64, 0);
1842 initFromAPInt(api.doubleToBits(d));
1843 }
1844
17141714
17151715 if (FPC->getType() == Type::DoubleTy) {
17161716 double Val = FPC->getValueAPF().convertToDouble();
1717 uint64_t i = *FPC->getValueAPF().convertToAPInt().getRawData();
17171718 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
1718 << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
1719 << " = 0x" << std::hex << i << std::dec
17191720 << "ULL; /* " << Val << " */\n";
17201721 } else if (FPC->getType() == Type::FloatTy) {
17211722 float Val = FPC->getValueAPF().convertToFloat();
1723 uint32_t i = (uint32_t)*FPC->getValueAPF().convertToAPInt().
1724 getRawData();
17221725 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
1723 << " = 0x" << std::hex << FloatToBits(Val) << std::dec
1726 << " = 0x" << std::hex << i << std::dec
17241727 << "U; /* " << Val << " */\n";
17251728 } else
17261729 assert(0 && "Unknown float type!");
427427 uint64_t X;
428428 unsigned Size;
429429 if (FP->getType()->getTypeID()==Type::FloatTyID) {
430 X = FloatToBits(FP->getValueAPF().convertToFloat());
430 X = (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData();
431431 Size = 4;
432432 } else {
433 X = DoubleToBits(FP->getValueAPF().convertToDouble());
433 X = *FP->getValueAPF().convertToAPInt().getRawData();
434434 Size = 8;
435435 }
436436 Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')';
14721472 const ConstantFP* FP = cast(C);
14731473 if (Ty->getTypeID() == Type::FloatTyID)
14741474 Out << "int32 (" <<
1475 FloatToBits(FP->getValueAPF().convertToFloat()) << ')';
1475 (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData() << ')';
14761476 else
14771477 Out << "int64 (" <<
1478 DoubleToBits(FP->getValueAPF().convertToDouble()) << ')';
1478 *FP->getValueAPF().convertToAPInt().getRawData() << ')';
14791479 break;
14801480 }
14811481 case Type::ArrayTyID:
34093409 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
34103410 std::vector CV;
34113411 if (EltVT == MVT::f64) {
3412 Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToDouble(~(1ULL << 63))));
3412 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
34133413 CV.push_back(C);
34143414 CV.push_back(C);
34153415 } else {
3416 Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToFloat(~(1U << 31))));
3416 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
34173417 CV.push_back(C);
34183418 CV.push_back(C);
34193419 CV.push_back(C);
34373437 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
34383438 std::vector CV;
34393439 if (EltVT == MVT::f64) {
3440 Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToDouble(1ULL << 63)));
3440 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
34413441 CV.push_back(C);
34423442 CV.push_back(C);
34433443 } else {
3444 Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToFloat(1U << 31)));
3444 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
34453445 CV.push_back(C);
34463446 CV.push_back(C);
34473447 CV.push_back(C);
34783478 // First get the sign bit of second operand.
34793479 std::vector CV;
34803480 if (SrcVT == MVT::f64) {
3481 CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToDouble(1ULL << 63))));
3482 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0)));
3481 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
3482 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
34833483 } else {
3484 CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToFloat(1U << 31))));
3485 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
3486 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
3487 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
3484 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
3485 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3486 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3487 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
34883488 }
34893489 Constant *C = ConstantVector::get(CV);
34903490 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
35063506 // Clear first operand sign bit.
35073507 CV.clear();
35083508 if (VT == MVT::f64) {
3509 CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToDouble(~(1ULL << 63)))));
3510 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0)));
3509 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
3510 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
35113511 } else {
3512 CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToFloat(~(1U << 31)))));
3513 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
3514 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
3515 CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
3512 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
3513 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3514 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3515 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
35163516 }
35173517 C = ConstantVector::get(CV);
35183518 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
8686
8787 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
8888 for (unsigned i = 0; i != SrcNumElts; ++i) {
89 uint64_t V =
90 DoubleToBits(cast(CV->getOperand(i))->
91 getValueAPF().convertToDouble());
89 uint64_t V = *cast(CV->getOperand(i))->
90 getValueAPF().convertToAPInt().getRawData();
9291 Constant *C = ConstantInt::get(Type::Int64Ty, V);
9392 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
9493 }
9796
9897 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
9998 for (unsigned i = 0; i != SrcNumElts; ++i) {
100 uint32_t V = FloatToBits(cast(CV->getOperand(i))->
101 getValueAPF().convertToFloat());
99 uint32_t V = (uint32_t)*cast(CV->getOperand(i))->
100 getValueAPF().convertToAPInt().getRawData();
102101 Constant *C = ConstantInt::get(Type::Int32Ty, V);
103102 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
104103 }
332331
333332 if (DestTy->isFloatingPoint()) {
334333 if (DestTy == Type::FloatTy)
335 return ConstantFP::get(DestTy, APFloat(CI->getValue().bitsToFloat()));
334 return ConstantFP::get(DestTy, APFloat(CI->getValue()));
336335 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
337 return ConstantFP::get(DestTy, APFloat(CI->getValue().bitsToDouble()));
336 return ConstantFP::get(DestTy, APFloat(CI->getValue()));
338337 }
339338 // Otherwise, can't fold this (vector?)
340339 return 0;
344343 if (const ConstantFP *FP = dyn_cast(V)) {
345344 // FP -> Integral.
346345 if (DestTy == Type::Int32Ty) {
347 APInt Val(32, 0);
348 return ConstantInt::get(Val.floatToBits(FP->
349 getValueAPF().convertToFloat()));
346 return ConstantInt::get(FP->getValueAPF().convertToAPInt());
350347 } else {
351348 assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!");
352 APInt Val(64, 0);
353 return ConstantInt::get(Val.doubleToBits(FP->
354 getValueAPF().convertToDouble()));
349 return ConstantInt::get(FP->getValueAPF().convertToAPInt());
355350 }
356351 }
357352 return 0;
249249 }
250250 else if (CFP->getType() == Type::DoubleTy)
251251 Out << "BitsToDouble(0x" << std::hex
252 << DoubleToBits(CFP->getValueAPF().convertToDouble())
252 << *CFP->getValueAPF().convertToAPInt().getRawData()
253253 << std::dec << "ULL) /* " << StrVal << " */";
254254 else
255255 Out << "BitsToFloat(0x" << std::hex
256 << FloatToBits(CFP->getValueAPF().convertToFloat())
256 << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData()
257257 << std::dec << "U) /* " << StrVal << " */";
258258 Out << ")";
259259 #if HAVE_PRINTF_A