Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | --- gdtoa-strtopdd.c.orig 2007-04-03 12:19:28.000000000 -0700 +++ gdtoa-strtopdd.c 2007-04-06 12:53:25.000000000 -0700 @@ -29,19 +29,31 @@ /* Please send bug reports to David M. Gay (dmg at acm dot org, * with " at " changed at "@" and " dot " changed to "."). */ +#include "xlocale_private.h" + #include "gdtoaimp.h" +#ifdef __APPLE__ +/* + * IEEE specifies that the most significant (head) double is required to + * be equal to the long double rounded to the nearest double, so that means + * the tail double might be the opposite sign as the head. We can do this + * adding (long double)0 to the number, which will fix it up. + */ +#define fixLDBL(x) ((x) += 0.L) +#endif /* __APPLE__ */ + int #ifdef KR_headers -strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd; +strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd; locale_t loc; #else -strtopdd(CONST char *s, char **sp, double *dd) +strtopdd(CONST char *s, char **sp, double *dd, locale_t loc) #endif { #ifdef Sudden_Underflow - static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; + static FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; #else - static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; + static FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; #endif ULong bits[4]; Long exp; @@ -49,13 +61,30 @@ typedef union { double d[2]; ULong L[4]; +#ifdef __APPLE__ + long double ld; +#endif /* __APPLE__ */ } U; U *u; + FPI *fpi = &fpi0, fpi1; +#ifdef Honor_FLT_ROUNDS + int rounding = Flt_Rounds; +#endif - rv = strtodg(s, sp, &fpi, &exp, bits); +#ifdef Honor_FLT_ROUNDS + if (rounding != fpi0.rounding) { + fpi1 = fpi0; /* for thread safety */ + fpi1.rounding = rounding; + fpi = &fpi1; + } +#endif /* Honor_FLT_ROUNDS */ + rv = strtodg(s, sp, fpi, &exp, bits, loc); u = (U*)dd; switch(rv & STRTOG_Retmask) { case STRTOG_NoNumber: + u->d[0] = u->d[1] = 0.; + return rv; // avoid setting sign + case STRTOG_Zero: u->d[0] = u->d[1] = 0.; break; @@ -101,6 +130,9 @@ } u->L[2+_1] = bits[0]; u->L[2+_0] = bits[1] & 0xfffff | exp << 20; +#ifdef __APPLE__ + fixLDBL(u->ld); +#endif /* __APPLE__ */ break; case STRTOG_Denormal: @@ -124,6 +156,9 @@ u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; u->L[2+_0] = bits[1] & (1L << j) - 1; u->L[2+_1] = bits[0]; +#ifdef __APPLE__ + fixLDBL(u->ld); +#endif /* __APPLE__ */ break; partly_normal: @@ -135,6 +170,9 @@ u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; u->L[2+_0] = bits[1] & (1L << j) - 1; u->L[2+_1] = bits[0]; +#ifdef __APPLE__ + fixLDBL(u->ld); +#endif /* __APPLE__ */ break; } if (i == 0) { @@ -142,6 +180,9 @@ u->L[_1] = bits[1]; u->L[2+_0] = 0; u->L[2+_1] = bits[0]; +#ifdef __APPLE__ + fixLDBL(u->ld); +#endif /* __APPLE__ */ break; } j = 32 - i; @@ -150,6 +191,9 @@ u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; u->L[2+_0] = 0; u->L[2+_1] = bits[0] & (1L << j) - 1; +#ifdef __APPLE__ + fixLDBL(u->ld); +#endif /* __APPLE__ */ break; hardly_normal: @@ -159,20 +203,44 @@ u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; u->L[2+_0] = 0; u->L[2+_1] = bits[0] & (1L << j) - 1; +#ifdef __APPLE__ + fixLDBL(u->ld); +#endif /* __APPLE__ */ break; case STRTOG_Infinite: +#ifdef __APPLE__ + u->L[_0] = 0x7ff00000; + u->L[_1] = u->L[2+_0] = u->L[2+_1] = 0; +#else /* __APPLE__ */ u->L[_0] = u->L[2+_0] = 0x7ff00000; u->L[_1] = u->L[2+_1] = 0; +#endif /* __APPLE__ */ break; case STRTOG_NaN: +#ifdef __APPLE__ + u->L[0] = d_QNAN0; + u->L[1] = d_QNAN1; + u->L[2] = u->L[3] = 0; +#else /* __APPLE__ */ u->L[0] = u->L[2] = d_QNAN0; u->L[1] = u->L[3] = d_QNAN1; +#endif /* __APPLE__ */ +#ifdef __APPLE__ + case STRTOG_NaNbits: + u->L[0] = d_QNAN0 | ((bits[2] >> 20 | bits[3] << 12) & 0xfffff); + u->L[1] = d_QNAN1 | bits[1] >> 20 | bits[2] << 12; + u->L[2] = u->L[3] = 0; +#endif /* __APPLE__ */ } if (rv & STRTOG_Neg) { u->L[ _0] |= 0x80000000L; +#ifdef __APPLE__ + u->L[2+_0] ^= 0x80000000L; +#else /* __APPLE__ */ u->L[2+_0] |= 0x80000000L; +#endif /* __APPLE__ */ } return rv; } |