llvm.org GIT mirror llvm / 69c9c8c
Implement arithmetic on APFloat with PPCDoubleDouble semantics by treating it as if it were an IEEE floating-point type with 106-bit mantissa. This makes compile-time arithmetic on "long double" for PowerPC in clang (in particular parsing of floating point constants) work, and fixes all "long double" related failures in the test suite. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166951 91177308-0d34-0410-b5e6-96231b3b80d8 Ulrich Weigand 7 years ago
2 changed file(s) with 106 addition(s) and 81 deletion(s). Raw diff Collapse all Expand all
5757 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, true };
5858 const fltSemantics APFloat::Bogus = { 0, 0, 0, true };
5959
60 // The PowerPC format consists of two doubles. It does not map cleanly
61 // onto the usual format above. For now only storage of constants of
62 // this type is supported, no arithmetic.
63 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
60 /* The PowerPC format consists of two doubles. It does not map cleanly
61 onto the usual format above. It is approximated using twice the
62 mantissa bits. Note that for exponents near the double minimum,
63 we no longer can represent the full 106 mantissa bits, so those
64 will be treated as denormal numbers.
65
66 FIXME: While this approximation is equivalent to what GCC uses for
67 compile-time arithmetic on PPC double-double numbers, it is not able
68 to represent all possible values held by a PPC double-double number,
69 for example: (long double) 1.0 + (long double) 0x1p-106
70 Should this be replaced by a full emulation of PPC double-double? */
71 const fltSemantics APFloat::PPCDoubleDouble =
72 { 1023, -1022 + 53, 53 + 53, true };
6473
6574 /* A tight upper bound on number of parts required to hold the value
6675 pow(5, power) is
27892798 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
27902799 assert(partCount()==2);
27912800
2792 uint64_t myexponent, mysignificand, myexponent2, mysignificand2;
2793
2794 if (category==fcNormal) {
2795 myexponent = exponent + 1023; //bias
2796 myexponent2 = exponent2 + 1023;
2797 mysignificand = significandParts()[0];
2798 mysignificand2 = significandParts()[1];
2799 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2800 myexponent = 0; // denormal
2801 if (myexponent2==1 && !(mysignificand2 & 0x10000000000000LL))
2802 myexponent2 = 0; // denormal
2803 } else if (category==fcZero) {
2804 myexponent = 0;
2805 mysignificand = 0;
2806 myexponent2 = 0;
2807 mysignificand2 = 0;
2808 } else if (category==fcInfinity) {
2809 myexponent = 0x7ff;
2810 myexponent2 = 0;
2811 mysignificand = 0;
2812 mysignificand2 = 0;
2801 uint64_t words[2];
2802 opStatus fs;
2803 bool losesInfo;
2804
2805 // Convert number to double. To avoid spurious underflows, we re-
2806 // normalize against the "double" minExponent first, and only *then*
2807 // truncate the mantissa. The result of that second conversion
2808 // may be inexact, but should never underflow.
2809 APFloat extended(*this);
2810 fltSemantics extendedSemantics = *semantics;
2811 extendedSemantics.minExponent = IEEEdouble.minExponent;
2812 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2813 assert(fs == opOK && !losesInfo);
2814 (void)fs;
2815
2816 APFloat u(extended);
2817 fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2818 assert(fs == opOK || fs == opInexact);
2819 (void)fs;
2820 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2821
2822 // If conversion was exact or resulted in a special case, we're done;
2823 // just set the second double to zero. Otherwise, re-convert back to
2824 // the extended format and compute the difference. This now should
2825 // convert exactly to double.
2826 if (u.category == fcNormal && losesInfo) {
2827 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2828 assert(fs == opOK && !losesInfo);
2829 (void)fs;
2830
2831 APFloat v(extended);
2832 v.subtract(u, rmNearestTiesToEven);
2833 fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2834 assert(fs == opOK && !losesInfo);
2835 (void)fs;
2836 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
28132837 } else {
2814 assert(category == fcNaN && "Unknown category");
2815 myexponent = 0x7ff;
2816 mysignificand = significandParts()[0];
2817 myexponent2 = exponent2;
2818 mysignificand2 = significandParts()[1];
2819 }
2820
2821 uint64_t words[2];
2822 words[0] = ((uint64_t)(sign & 1) << 63) |
2823 ((myexponent & 0x7ff) << 52) |
2824 (mysignificand & 0xfffffffffffffLL);
2825 words[1] = ((uint64_t)(sign2 & 1) << 63) |
2826 ((myexponent2 & 0x7ff) << 52) |
2827 (mysignificand2 & 0xfffffffffffffLL);
2838 words[1] = 0;
2839 }
2840
28282841 return APInt(128, words);
28292842 }
28302843
30443057 assert(api.getBitWidth()==128);
30453058 uint64_t i1 = api.getRawData()[0];
30463059 uint64_t i2 = api.getRawData()[1];
3047 uint64_t myexponent = (i1 >> 52) & 0x7ff;
3048 uint64_t mysignificand = i1 & 0xfffffffffffffLL;
3049 uint64_t myexponent2 = (i2 >> 52) & 0x7ff;
3050 uint64_t mysignificand2 = i2 & 0xfffffffffffffLL;
3051
3052 initialize(&APFloat::PPCDoubleDouble);
3053 assert(partCount()==2);
3054
3055 sign = static_cast(i1>>63);
3056 sign2 = static_cast(i2>>63);
3057 if (myexponent==0 && mysignificand==0) {
3058 // exponent, significand meaningless
3059 // exponent2 and significand2 are required to be 0; we don't check
3060 category = fcZero;
3061 } else if (myexponent==0x7ff && mysignificand==0) {
3062 // exponent, significand meaningless
3063 // exponent2 and significand2 are required to be 0; we don't check
3064 category = fcInfinity;
3065 } else if (myexponent==0x7ff && mysignificand!=0) {
3066 // exponent meaningless. So is the whole second word, but keep it
3067 // for determinism.
3068 category = fcNaN;
3069 exponent2 = myexponent2;
3070 significandParts()[0] = mysignificand;
3071 significandParts()[1] = mysignificand2;
3072 } else {
3073 category = fcNormal;
3074 // Note there is no category2; the second word is treated as if it is
3075 // fcNormal, although it might be something else considered by itself.
3076 exponent = myexponent - 1023;
3077 exponent2 = myexponent2 - 1023;
3078 significandParts()[0] = mysignificand;
3079 significandParts()[1] = mysignificand2;
3080 if (myexponent==0) // denormal
3081 exponent = -1022;
3082 else
3083 significandParts()[0] |= 0x10000000000000LL; // integer bit
3084 if (myexponent2==0)
3085 exponent2 = -1022;
3086 else
3087 significandParts()[1] |= 0x10000000000000LL; // integer bit
3060 opStatus fs;
3061 bool losesInfo;
3062
3063 // Get the first double and convert to our format.
3064 initFromDoubleAPInt(APInt(64, i1));
3065 fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3066 assert(fs == opOK && !losesInfo);
3067 (void)fs;
3068
3069 // Unless we have a special case, add in second double.
3070 if (category == fcNormal) {
3071 APFloat v(APInt(64, i2));
3072 fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3073 assert(fs == opOK && !losesInfo);
3074 (void)fs;
3075
3076 add(v, rmNearestTiesToEven);
30883077 }
30893078 }
30903079
736736 EXPECT_EQ(4294967295.0, test.convertToDouble());
737737 EXPECT_FALSE(losesInfo);
738738 }
739 }
739
740 TEST(APFloatTest, PPCDoubleDouble) {
741 APFloat test(APFloat::PPCDoubleDouble, "1.0");
742 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
743 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
744
745 test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven);
746 EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]);
747 EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]);
748
749 // LDBL_MAX
750 test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308");
751 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
752 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
753
754 // LDBL_MIN
755 test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292");
756 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
757 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
758
759 test = APFloat(APFloat::PPCDoubleDouble, "1.0");
760 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven);
761 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
762 EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]);
763
764 test = APFloat(APFloat::PPCDoubleDouble, "1.0");
765 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven);
766 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
767 #if 0 // XFAIL
768 // This is what we would expect with a true double-double implementation
769 EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]);
770 #else
771 // This is what we get with our 106-bit mantissa approximation
772 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
773 #endif
774 }
775 }