3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #include "stlport_prefix.h"
25 #if defined (__GNUC__) && !defined (__sun) || \
30 #if defined (__linux__)
36 # if defined (_STLP_BIG_ENDIAN)
39 # elif defined (_STLP_LITTLE_ENDIAN)
43 # error Unknown endianess
49 #if defined (N_PLAT_NLM)
50 # include <nlm/nwintxx.h>
53 typedef unsigned INT64 uint64_t;
55 // #error "Can't find INT64"
56 // 64-bit int really not defined in headers
57 // (_INTEGRAL_MAX_BITS < 64 in any case?), but compiler indeed know __int64
59 typedef unsigned __int64 uint64_t;
63 typedef unsigned INT32 uint32_t;
65 # error Can not find INT32
78 _STLP_MOVE_TO_PRIV_NAMESPACE
80 //----------------------------------------------------------------------
83 // Helper functions for _M_do_get_float.
85 #if !defined (_STLP_NO_WCHAR_T)
87 _Initialize_get_float( const ctype<wchar_t>& ct,
88 wchar_t& Plus, wchar_t& Minus,
89 wchar_t& pow_e, wchar_t& pow_E,
91 char ndigits[11] = "0123456789";
93 Minus = ct.widen('-');
94 pow_e = ct.widen('e');
95 pow_E = ct.widen('E');
96 ct.widen(ndigits + 0, ndigits + 10, digits);
101 * __string_to_double is just lifted from atof, the difference being
102 * that we just use '.' for the decimal point, rather than let it
103 * be taken from the current C locale, which of course is not accessible
106 #if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
107 typedef unsigned long uint32;
108 typedef unsigned __int64 uint64;
109 # define ULL(x) x##Ui64
110 #elif defined (__MRC__) || defined (__SC__)
111 typedef unsigned long uint32;
112 # include "uint64.h" //*TY 03/25/2000 - added 64bit math type definition
113 #elif defined (__unix) || defined (__MINGW32__) || defined (N_PLAT_NLM) || \
114 (defined (__DMC__) && (__LONGLONG))
115 typedef uint32_t uint32;
116 typedef uint64_t uint64;
117 # define ULL(x) x##ULL
119 # error There should be some unsigned 64-bit integer on the system!
122 // Multiplication of two 64-bit integers, giving a 128-bit result.
123 // Taken from Algorithm M in Knuth section 4.3.1, with the loop
125 static void _Stl_mult64(const uint64 u, const uint64 v,
126 uint64& high, uint64& low) {
127 const uint64 low_mask = ULL(0xffffffff);
128 const uint64 u0 = u & low_mask;
129 const uint64 u1 = u >> 32;
130 const uint64 v0 = v & low_mask;
131 const uint64 v1 = v >> 32;
136 t = u1 * v0 + (t >> 32);
137 uint64 w1 = t & low_mask;
140 uint64 x = u0 * v1 + w1;
141 low += (x & low_mask) << 32;
142 high = u1 * v1 + w2 + (x >> 32);
145 #define bit11 ULL(0x7ff)
146 #define exponent_mask (bit11 << 52)
148 #if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
149 (!defined (__CYGWIN__) && !defined (__MINGW32__))
150 //Generate bad code when compiled with -O2 option.
153 void _Stl_set_exponent(uint64 &val, uint64 exp)
154 { val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
156 /* Power of ten fractions for tenscale*/
157 /* The constants are factored so that at most two constants
158 * and two multiplies are needed. Furthermore, one of the constants
159 * is represented exactly - 10**n where 1<= n <= 27.
162 #if !defined (__SC__) //*TY 03/25/2000 - no native 64bit integer under SCpp
163 static const uint64 _Stl_tenpow[80] = {
164 ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
165 ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
166 ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
167 ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
168 ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
169 ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
170 ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
171 ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
172 ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
173 ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
174 ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
175 ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
176 ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
177 ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
178 ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
179 ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
180 ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
181 ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
182 ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
183 ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
184 ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
185 ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
186 ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
187 ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
188 ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
189 ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
190 ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
192 ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
193 ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
194 ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
195 ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
196 ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
197 ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
198 ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
199 ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
200 ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
201 ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
203 ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
204 ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
205 ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
206 ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
207 ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
208 ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
209 ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
210 ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
211 ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
212 ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
213 ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
214 ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
215 ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
217 #else //*TY 03/20/2000 - added support for SCpp which lacks native 64bit integer type
218 static const UnsignedWide _Stl_tenpow[80] = {
219 ULL2(0xa0000000,0x00000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
220 ULL2(0xc8000000,0x00000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
221 ULL2(0xfa000000,0x00000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
222 ULL2(0x9c400000,0x00000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
223 ULL2(0xc3500000,0x00000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
224 ULL2(0xf4240000,0x00000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
225 ULL2(0x98968000,0x00000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
226 ULL2(0xbebc2000,0x00000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
227 ULL2(0xee6b2800,0x00000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
228 ULL2(0x9502f900,0x00000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
229 ULL2(0xba43b740,0x00000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
230 ULL2(0xe8d4a510,0x00000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
231 ULL2(0x9184e72a,0x00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
232 ULL2(0xb5e620f4,0x80000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
233 ULL2(0xe35fa931,0xa0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
234 ULL2(0x8e1bc9bf,0x04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
235 ULL2(0xb1a2bc2e,0xc5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
236 ULL2(0xde0b6b3a,0x76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
237 ULL2(0x8ac72304,0x89e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
238 ULL2(0xad78ebc5,0xac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
239 ULL2(0xd8d726b7,0x177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
240 ULL2(0x87867832,0x6eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
241 ULL2(0xa968163f,0x0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
242 ULL2(0xd3c21bce,0xcceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
243 ULL2(0x84595161,0x401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
244 ULL2(0xa56fa5b9,0x9019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
245 ULL2(0xcecb8f27,0xf4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
247 ULL2(0xd0cf4b50,0xcfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
248 ULL2(0xd2d80db0,0x2aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
249 ULL2(0xd4e5e2cd,0xc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
250 ULL2(0xd6f8d750,0x9292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
251 ULL2(0xd910f7ff,0x28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
252 ULL2(0xdb2e51bf,0xe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
253 ULL2(0xdd50f199,0x6b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
254 ULL2(0xdf78e4b2,0xbd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
255 ULL2(0xe1a63853,0xbbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
256 ULL2(0xe3d8f9e5,0x63a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
258 ULL2(0xfd87b5f2,0x8300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
259 ULL2(0xfb158592,0xbe068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
260 ULL2(0xf8a95fcf,0x88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
261 ULL2(0xf64335bc,0xf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
262 ULL2(0xf3e2f893,0xdec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
263 ULL2(0xf18899b1,0xbc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
264 ULL2(0xef340a98,0x172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
265 ULL2(0xece53cec,0x4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
266 ULL2(0xea9c2277,0x23ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
267 ULL2(0xe858ad24,0x8f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
268 ULL2(0xe61acf03,0x3d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
269 ULL2(0xe3e27a44,0x4d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
270 ULL2(0xe1afa13a,0xfbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
274 static const short _Stl_twoexp[80] = {
275 4,7,10,14,17,20,24,27,30,34,37,40,44,47,50,54,57,60,64,67,70,74,77,80,84,87,90,
276 183,276,369,462,555,648,741,834,927,1020,
277 -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
280 #define TEN_1 0 /* offset to 10 ** 1 */
281 #define TEN_27 26 /* offset to 10 ** 27 */
282 #define TEN_M28 37 /* offset to 10 ** -28 */
286 #define _Stl_HIBITULL (ULL(1) << 63)
288 static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
290 if ((prodhi & _Stl_HIBITULL) == 0) {
291 /* leading bit is a zero
292 * may have to normalize
294 if ((prodhi == ~_Stl_HIBITULL) &&
295 ((prodlo >> 62) == 0x3)) { /* normalization followed by round
296 * would cause carry to create
297 * extra bit, so don't normalize
302 p = (prodhi << 1) | (prodlo >> 63); /* normalize */
310 if ((prodlo & _Stl_HIBITULL) != 0) { /* first guard bit a one */ //*TY 03/25/2000 - added explicit comparison to zero to avoid reliance to the implicit conversion from uint64 to bool
311 #if !defined (__SC__) //*TY 03/25/2000 -
312 if (((p & 0x1) != 0) ||
313 prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */
314 #else //*TY 03/25/2000 - added workaround for SCpp compiler
315 bool b1 = ((p & 0x1) != 0);
316 if (b1 || prodlo != _Stl_HIBITULL) { //*TY 03/25/2000 - SCpp confuses on this particular original boolean expression
317 #endif //*TY 03/25/2000 -
328 // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
329 // p: 64-bit fraction
330 // exp: base-10 exponent
331 // bexp: base-2 exponent (output parameter)
332 static void _Stl_tenscale(uint64& p, int exp, int& bexp) {
333 uint64 prodhi, prodlo; /* 128b product */
334 int exp_hi, exp_lo; /* exp = exp_hi*32 + exp_lo */
335 int hi, lo, tlo, thi; /* offsets in power of ten table */
336 int norm; /* number of bits of normalization */
337 int num_hi; /* number of high exponent powers */
340 if (exp > 0) { /* split exponent */
345 while (exp_lo > 27) {
365 else { /* no scaling needed */
368 while (exp_hi) { /* scale */
369 hi = (min) (exp_hi, num_hi); /* only a few large powers of 10 */
370 exp_hi -= hi; /* could iterate in extreme case */
372 _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
373 _Stl_norm_and_round(p, norm, prodhi, prodlo);
374 bexp += _Stl_twoexp[hi] - norm;
377 lo = tlo + exp_lo -1;
378 _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo);
379 _Stl_norm_and_round(p, norm, prodhi, prodlo);
380 bexp += _Stl_twoexp[lo] - norm;
386 // First argument is a buffer of values from 0 to 9, NOT ascii.
387 // Second argument is number of digits in buffer, 1 <= digits <= 17.
388 // Third argument is base-10 exponent.
390 #if defined (__SC__) || defined (__MRC__)
392 //*TY 04/06/2000 - powermac's 68K emulator utilizes apple's SANE floating point, which is not compatible with IEEE format.
393 _STLP_MOVE_TO_STD_NAMESPACE
398 _STLP_BEGIN_NAMESPACE
399 _STLP_MOVE_TO_PRIV_NAMESPACE
401 static inline double _Stl_atod(char *buffer, int ndigit, int dexp) {
402 decimal d; // ref. inside macintosh powerpc numerics p.9-13
406 d.sig.length = ndigit;
407 for (int i = 0; i < ndigit; ++i) {
408 d.sig.text[i] = buffer[i] + '0';
413 #else /* IEEE representation */
415 # if !defined (__linux__)
416 static double _Stl_atod(char *buffer, int ndigit, int dexp) {
417 uint64 value; /* Value develops as follows:
418 * 1) decimal digits as an integer
419 * 2) left adjusted fraction
420 * 3) right adjusted fraction
421 * 4) exponent and fraction
424 uint32 guard; /* First guard bit */
425 uint64 rest; /* Remaining guard bits */
427 int bexp; /* binary exponent */
428 int nzero; /* number of non-zero bits */
429 int sexp; /* scaling exponent */
431 char *bufferend; /* pointer to char after last digit */
433 /* Check for zero and treat it as a special case */
438 /* Convert the decimal digits to a binary integer. */
440 bufferend = buffer + ndigit;
443 while (buffer < bufferend) {
448 /* Check for zero and treat it as a special case */
453 /* Normalize value */
454 bexp = 64; /* convert from 64b int to fraction */
456 /* Count number of non-zeroes in value */
458 if ((value >> 32) != 0) { nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
459 if ((value >> (16 + nzero)) != 0) { nzero += 16; }
460 if ((value >> ( 8 + nzero)) != 0) { nzero += 8; }
461 if ((value >> ( 4 + nzero)) != 0) { nzero += 4; }
462 if ((value >> ( 2 + nzero)) != 0) { nzero += 2; }
463 if ((value >> ( 1 + nzero)) != 0) { nzero += 1; }
464 if ((value >> ( nzero)) != 0) { nzero += 1; }
467 value <<= /*(uint64)*/ (64 - nzero); //*TY 03/25/2000 - removed extraneous cast to uint64
470 /* At this point we have a 64b fraction and a binary exponent
471 * but have yet to incorporate the decimal exponent.
474 /* multiply by 10^dexp */
475 _Stl_tenscale(value, dexp, sexp);
478 if (bexp <= -1022) { /* HI denorm or underflow */
480 if (bexp < -53) { /* guaranteed underflow */
483 else { /* denorm or possible underflow */
484 int lead0 = 12 - bexp; /* 12 sign and exponent bits */
486 /* we must special case right shifts of more than 63 */
492 else if (lead0 == 64) {
493 rest = value & ((ULL(1)<< 63)-1);
495 guard = (uint32) ((value>> 63) & 1 );
497 guard = to_ulong((value>> 63) & 1 ); //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
502 rest = value & (((ULL(1) << lead0)-1)-1);
504 guard = (uint32) (((value>> lead0)-1) & 1);
505 #else //*TY 03/25/2000 -
506 guard = to_ulong(((value>> lead0)-1) & 1);
507 #endif //*TY 03/25/2000 -
508 value >>= /*(uint64)*/ lead0; /* exponent is zero */
512 if (guard && ((value & 1) || rest) ) {
514 if (value == (ULL(1) << 52)) { /* carry created normal number */
516 _Stl_set_exponent(value, 1);
521 else { /* not zero or denorm */
522 /* Round to 53 bits */
523 rest = value & (1<<10)-1;
526 guard = (uint32) value & 1;
527 #else //*TY 03/25/2000 -
528 guard = to_ulong(value & 1);
532 /* value&1 guard rest Action
540 if (((value&1)!=0) || (rest!=0)) {
542 if ((value >> 53) != 0) { /* carry all the way across */
543 value >>= 1; /* renormalize */
550 * IEEE Double Precision Format
551 * (From Table 7-8 of Kane and Heinrich)
556 * Exponent bias +1023
559 * Total width in bits 64
562 if (bexp > 1024) { /* overflow */
563 return numeric_limits<double>::infinity();
565 else { /* value is normal */
566 value &= ~(ULL(1) << 52); /* hide hidden bit */
567 _Stl_set_exponent(value, bexp + 1022); /* add bias */
571 _STLP_STATIC_ASSERT(sizeof(value) == sizeof(double))
572 return *((double *) &value);
577 static double _Stl_atod(char *buffer, int ndigit, int dexp) {
580 char *bufferend; /* pointer to char after last digit */
582 /* Check for zero and treat it as a special case */
588 /* Convert the decimal digits to a binary integer. */
590 bufferend = buffer + ndigit;
594 while (buffer < bufferend) {
599 /* Check for zero and treat it as a special case */
604 /* Normalize value */
605 int bexp = 64; /* convert from 64b int to fraction */
607 /* Count number of non-zeroes in value */
609 if ((vv.i64 >> 32) !=0 ) { nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
610 if ((vv.i64 >> (16 + nzero)) != 0) { nzero += 16; }
611 if ((vv.i64 >> ( 8 + nzero)) != 0) { nzero += 8; }
612 if ((vv.i64 >> ( 4 + nzero)) != 0) { nzero += 4; }
613 if ((vv.i64 >> ( 2 + nzero)) != 0) { nzero += 2; }
614 if ((vv.i64 >> ( 1 + nzero)) != 0) { nzero += 1; }
615 if ((vv.i64 >> ( nzero)) != 0) { nzero += 1; }
619 vv.i64 <<= nzero; //*TY 03/25/2000 - removed extraneous cast to uint64
622 /* At this point we have a 64b fraction and a binary exponent
623 * but have yet to incorporate the decimal exponent.
626 /* multiply by 10^dexp */
628 _Stl_tenscale(vv.i64, dexp, sexp);
631 if (bexp <= -1022) { /* HI denorm or underflow */
633 if (bexp < -53) { /* guaranteed underflow */
636 else { /* denorm or possible underflow */
641 lead0 = 12-bexp; /* 12 sign and exponent bits */
643 /* we must special case right shifts of more than 63 */
649 else if (lead0 == 64) {
650 rest = vv.i64 & ((ULL(1) << 63)-1);
652 guard = (uint32) ((vv.i64 >> 63) & 1 );
654 guard = to_ulong((vv.i64 >> 63) & 1 ); //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
659 rest = vv.i64 & (((ULL(1) << lead0)-1)-1);
661 guard = (uint32) (((vv.i64 >> lead0)-1) & 1);
662 #else //*TY 03/25/2000 -
663 guard = to_ulong(((vv.i64 >> lead0)-1) & 1);
664 #endif //*TY 03/25/2000 -
665 vv.i64 >>= /*(uint64)*/ lead0; /* exponent is zero */
669 if (guard && ( (vv.i64 & 1) || rest)) {
671 if (vv.i64 == (ULL(1) << 52)) { /* carry created normal number */
672 v.ieee.mantissa0 = 0;
673 v.ieee.mantissa1 = 0;
681 else { /* not zero or denorm */
682 /* Round to 53 bits */
683 uint64_t rest = vv.i64 & (1<<10)-1;
686 uint32_t guard = (uint32) vv.i64 & 1;
687 #else //*TY 03/25/2000 -
688 uint32_t guard = to_ulong(vv.i64 & 1);
692 /* value&1 guard rest Action
700 if (((vv.i64&1)!=0) || (rest!=0)) {
701 vv.i64++; /* round */
702 if ((vv.i64>>53)!=0) { /* carry all the way across */
703 vv.i64 >>= 1; /* renormalize */
710 * IEEE Double Precision Format
711 * (From Table 7-8 of Kane and Heinrich)
716 * Exponent bias +1023
719 * Total width in bits 64
722 if (bexp > 1024) { /* overflow */
723 return numeric_limits<double>::infinity();
725 else { /* value is normal */
726 vv.i64 &= ~(ULL(1) << 52); /* hide hidden bit */
727 v.ieee.mantissa0 = vv.i32.hi;
728 v.ieee.mantissa1 = vv.i32.lo;
730 v.ieee.exponent = bexp + 1022;
735 v.ieee.mantissa0 = vv.i32.hi;
736 v.ieee.mantissa1 = vv.i32.lo;
746 static double _Stl_string_to_double(const char *s) {
747 const int max_digits = 17;
749 unsigned Negate, decimal_point;
754 char digits[max_digits];
756 // Skip leading whitespace, if any.
757 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
758 while (c = *s++, ct.is(ctype_base::space, char(c))) {}
776 if (d == digits + max_digits) {
777 /* ignore more than 17 digits, but adjust exponent */
778 exp += (decimal_point ^ 1);
781 if (c == 0 && d == digits) {
782 /* ignore leading zeros */
787 exp -= decimal_point;
790 else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */
798 /* strtod cant return until it finds the end of the exponent */
803 if (c == 'e'-'0' || c == 'E'-'0') {
804 register unsigned negate_exp = 0;
807 if (c == '+' || c == ' ') {
814 if (c -= '0', c < 10) {
821 while (c -= '0', c < 10);
825 if (e < -340 || e > 340)
835 else if (exp > 308) {
836 x = numeric_limits<double>::infinity();
839 /* let _Stl_atod diagnose under- and over-flows */
840 /* if the input was == 0.0, we have already returned,
841 so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
843 x = _Stl_atod(digits, (int)(d - digits), exp);
852 #if !defined (_STLP_NO_LONG_DOUBLE)
854 * __string_to_long_double is just lifted from atold, the difference being
855 * that we just use '.' for the decimal point, rather than let it
856 * be taken from the current C locale, which of course is not accessible
861 _Stl_string_to_long_double(const char * s) {
862 const int max_digits = 34;
864 register unsigned Negate, decimal_point;
869 char digits[max_digits];
871 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
872 while (c = *s++, ct.is(ctype_base::space, char(c)))
893 if (d == digits+max_digits) {
894 /* ignore more than 34 digits, but adjust exponent */
895 exp += (decimal_point ^ 1);
898 if (c == 0 && d == digits) {
899 /* ignore leading zeros */
905 exp -= decimal_point;
908 else if ((char)c == dpchar && !decimal_point) { /* INTERNATIONAL */
920 if (c == 'e'-'0' || c == 'E'-'0') {
921 register unsigned negate_exp = 0;
924 if (c == '+' || c == ' ') {
931 if (c -= '0', c < 10) {
938 while (c -= '0', c < 10);
942 if (e < -(323+max_digits) || e > 308)
949 if (exp < -(324+max_digits)) {
952 else if (exp > 308) {
953 x = numeric_limits<long double>::infinity();
956 /* let _Stl_atod diagnose under- and over-flows */
957 /* if the input was == 0.0, we have already returned,
958 so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
961 // x = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1
962 double tmp = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1
963 x = tmp == numeric_limits<double>::infinity()
964 ? numeric_limits<long double>::infinity()
977 __string_to_float(const __iostring& v, float& val)
978 { val = (float)_Stl_string_to_double(v.c_str()); }
981 __string_to_float(const __iostring& v, double& val)
982 { val = _Stl_string_to_double(v.c_str()); }
984 #if !defined (_STLP_NO_LONG_DOUBLE)
986 __string_to_float(const __iostring& v, long double& val)
987 { val = _Stl_string_to_long_double(v.c_str()); }
990 _STLP_MOVE_TO_STD_NAMESPACE