Update contrib.
2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
21 #include "stlport_prefix.h"
27 #if defined (__GNUC__) && !defined (__sun) || \
32 #if defined (__linux__)
38 # if defined (_STLP_BIG_ENDIAN)
41 # elif defined (_STLP_LITTLE_ENDIAN)
45 # error Unknown endianess
51 #if defined (N_PLAT_NLM)
52 # include <nlm/nwintxx.h>
55 typedef unsigned INT64 uint64_t;
57 // #error "Can't find INT64"
58 // 64-bit int really not defined in headers
59 // (_INTEGRAL_MAX_BITS < 64 in any case?), but compiler indeed know __int64
61 typedef unsigned __int64 uint64_t;
65 typedef unsigned INT32 uint32_t;
67 # error Can not find INT32
80 _STLP_MOVE_TO_PRIV_NAMESPACE
82 //----------------------------------------------------------------------
85 // Helper functions for _M_do_get_float.
87 #if !defined (_STLP_NO_WCHAR_T)
88 void _STLP_DECLSPEC _STLP_CALL
89 _Initialize_get_float( const ctype<wchar_t>& ct,
90 wchar_t& Plus, wchar_t& Minus,
91 wchar_t& pow_e, wchar_t& pow_E,
93 char ndigits[11] = "0123456789";
95 Minus = ct.widen('-');
96 pow_e = ct.widen('e');
97 pow_E = ct.widen('E');
98 ct.widen(ndigits + 0, ndigits + 10, digits);
103 * __string_to_double is just lifted from atof, the difference being
104 * that we just use '.' for the decimal point, rather than let it
105 * be taken from the current C locale, which of course is not accessible
108 #if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
109 typedef unsigned long uint32;
110 typedef unsigned __int64 uint64;
111 # define ULL(x) x##Ui64
112 #elif defined (__MRC__) || defined (__SC__)
113 typedef unsigned long uint32;
114 # include "uint64.h" //*TY 03/25/2000 - added 64bit math type definition
115 #elif defined (__unix) || defined (__MINGW32__) || defined (N_PLAT_NLM) || \
116 (defined (__DMC__) && (__LONGLONG)) || defined(__SYMBIAN32__)
117 #if defined(__SYMBIAN32__)
118 # include <sys/types.h>
120 typedef uint32_t uint32;
121 typedef uint64_t uint64;
122 # define ULL(x) x##ULL
124 # error There should be some unsigned 64-bit integer on the system!
127 // Multiplication of two 64-bit integers, giving a 128-bit result.
128 // Taken from Algorithm M in Knuth section 4.3.1, with the loop
130 static void _Stl_mult64(const uint64 u, const uint64 v,
131 uint64& high, uint64& low) {
132 const uint64 low_mask = ULL(0xffffffff);
133 const uint64 u0 = u & low_mask;
134 const uint64 u1 = u >> 32;
135 const uint64 v0 = v & low_mask;
136 const uint64 v1 = v >> 32;
141 t = u1 * v0 + (t >> 32);
142 uint64 w1 = t & low_mask;
145 uint64 x = u0 * v1 + w1;
146 low += (x & low_mask) << 32;
147 high = u1 * v1 + w2 + (x >> 32);
150 #define bit11 ULL(0x7ff)
151 #define exponent_mask (bit11 << 52)
153 #if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
154 (!defined (__CYGWIN__) && !defined (__MINGW32__))
155 //Generate bad code when compiled with -O2 option.
158 void _Stl_set_exponent(uint64 &val, uint64 exp)
159 { val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
161 /* Power of ten fractions for tenscale*/
162 /* The constants are factored so that at most two constants
163 * and two multiplies are needed. Furthermore, one of the constants
164 * is represented exactly - 10**n where 1<= n <= 27.
167 #if !defined (__SC__) //*TY 03/25/2000 - no native 64bit integer under SCpp
168 static const uint64 _Stl_tenpow[80] = {
169 ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
170 ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
171 ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
172 ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
173 ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
174 ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
175 ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
176 ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
177 ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
178 ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
179 ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
180 ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
181 ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
182 ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
183 ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
184 ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
185 ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
186 ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
187 ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
188 ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
189 ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
190 ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
191 ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
192 ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
193 ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
194 ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
195 ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
197 ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
198 ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
199 ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
200 ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
201 ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
202 ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
203 ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
204 ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
205 ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
206 ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
208 ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
209 ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
210 ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
211 ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
212 ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
213 ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
214 ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
215 ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
216 ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
217 ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
218 ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
219 ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
220 ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
222 #else //*TY 03/20/2000 - added support for SCpp which lacks native 64bit integer type
223 static const UnsignedWide _Stl_tenpow[80] = {
224 ULL2(0xa0000000,0x00000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
225 ULL2(0xc8000000,0x00000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
226 ULL2(0xfa000000,0x00000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
227 ULL2(0x9c400000,0x00000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
228 ULL2(0xc3500000,0x00000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
229 ULL2(0xf4240000,0x00000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
230 ULL2(0x98968000,0x00000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
231 ULL2(0xbebc2000,0x00000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
232 ULL2(0xee6b2800,0x00000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
233 ULL2(0x9502f900,0x00000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
234 ULL2(0xba43b740,0x00000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
235 ULL2(0xe8d4a510,0x00000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
236 ULL2(0x9184e72a,0x00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
237 ULL2(0xb5e620f4,0x80000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
238 ULL2(0xe35fa931,0xa0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
239 ULL2(0x8e1bc9bf,0x04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
240 ULL2(0xb1a2bc2e,0xc5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
241 ULL2(0xde0b6b3a,0x76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
242 ULL2(0x8ac72304,0x89e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
243 ULL2(0xad78ebc5,0xac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
244 ULL2(0xd8d726b7,0x177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
245 ULL2(0x87867832,0x6eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
246 ULL2(0xa968163f,0x0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
247 ULL2(0xd3c21bce,0xcceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
248 ULL2(0x84595161,0x401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
249 ULL2(0xa56fa5b9,0x9019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
250 ULL2(0xcecb8f27,0xf4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
252 ULL2(0xd0cf4b50,0xcfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
253 ULL2(0xd2d80db0,0x2aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
254 ULL2(0xd4e5e2cd,0xc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
255 ULL2(0xd6f8d750,0x9292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
256 ULL2(0xd910f7ff,0x28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
257 ULL2(0xdb2e51bf,0xe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
258 ULL2(0xdd50f199,0x6b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
259 ULL2(0xdf78e4b2,0xbd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
260 ULL2(0xe1a63853,0xbbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
261 ULL2(0xe3d8f9e5,0x63a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
263 ULL2(0xfd87b5f2,0x8300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
264 ULL2(0xfb158592,0xbe068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
265 ULL2(0xf8a95fcf,0x88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
266 ULL2(0xf64335bc,0xf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
267 ULL2(0xf3e2f893,0xdec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
268 ULL2(0xf18899b1,0xbc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
269 ULL2(0xef340a98,0x172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
270 ULL2(0xece53cec,0x4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
271 ULL2(0xea9c2277,0x23ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
272 ULL2(0xe858ad24,0x8f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
273 ULL2(0xe61acf03,0x3d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
274 ULL2(0xe3e27a44,0x4d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
275 ULL2(0xe1afa13a,0xfbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
279 static const short _Stl_twoexp[80] = {
280 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,
281 183,276,369,462,555,648,741,834,927,1020,
282 -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
285 #define TEN_1 0 /* offset to 10 ** 1 */
286 #define TEN_27 26 /* offset to 10 ** 27 */
287 #define TEN_M28 37 /* offset to 10 ** -28 */
291 #define _Stl_HIBITULL (ULL(1) << 63)
293 static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
295 if ((prodhi & _Stl_HIBITULL) == 0) {
296 /* leading bit is a zero
297 * may have to normalize
299 if ((prodhi == ~_Stl_HIBITULL) &&
300 ((prodlo >> 62) == 0x3)) { /* normalization followed by round
301 * would cause carry to create
302 * extra bit, so don't normalize
307 p = (prodhi << 1) | (prodlo >> 63); /* normalize */
315 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
316 #if !defined (__SC__) //*TY 03/25/2000 -
317 if (((p & 0x1) != 0) ||
318 prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */
319 #else //*TY 03/25/2000 - added workaround for SCpp compiler
320 bool b1 = ((p & 0x1) != 0);
321 if (b1 || prodlo != _Stl_HIBITULL) { //*TY 03/25/2000 - SCpp confuses on this particular original boolean expression
322 #endif //*TY 03/25/2000 -
333 // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
334 // p: 64-bit fraction
335 // exp: base-10 exponent
336 // bexp: base-2 exponent (output parameter)
337 static void _Stl_tenscale(uint64& p, int exp, int& bexp) {
338 uint64 prodhi, prodlo; /* 128b product */
339 int exp_hi, exp_lo; /* exp = exp_hi*32 + exp_lo */
340 int hi, lo, tlo, thi; /* offsets in power of ten table */
341 int norm; /* number of bits of normalization */
342 int num_hi; /* number of high exponent powers */
345 if (exp > 0) { /* split exponent */
350 while (exp_lo > 27) {
370 else { /* no scaling needed */
373 while (exp_hi) { /* scale */
374 hi = (min) (exp_hi, num_hi); /* only a few large powers of 10 */
375 exp_hi -= hi; /* could iterate in extreme case */
377 _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
378 _Stl_norm_and_round(p, norm, prodhi, prodlo);
379 bexp += _Stl_twoexp[hi] - norm;
382 lo = tlo + exp_lo -1;
383 _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo);
384 _Stl_norm_and_round(p, norm, prodhi, prodlo);
385 bexp += _Stl_twoexp[lo] - norm;
391 // First argument is a buffer of values from 0 to 9, NOT ascii.
392 // Second argument is number of digits in buffer, 1 <= digits <= 17.
393 // Third argument is base-10 exponent.
395 #if defined (__SC__) || defined (__MRC__)
397 //*TY 04/06/2000 - powermac's 68K emulator utilizes apple's SANE floating point, which is not compatible with IEEE format.
398 _STLP_MOVE_TO_STD_NAMESPACE
403 _STLP_BEGIN_NAMESPACE
404 _STLP_MOVE_TO_PRIV_NAMESPACE
406 static inline double _Stl_atod(char *buffer, int ndigit, int dexp) {
407 decimal d; // ref. inside macintosh powerpc numerics p.9-13
411 d.sig.length = ndigit;
412 for (int i = 0; i < ndigit; ++i) {
413 d.sig.text[i] = buffer[i] + '0';
418 #else /* IEEE representation */
420 # if !defined (__linux__)
421 static double _Stl_atod(char *buffer, int ndigit, int dexp) {
422 uint64 value; /* Value develops as follows:
423 * 1) decimal digits as an integer
424 * 2) left adjusted fraction
425 * 3) right adjusted fraction
426 * 4) exponent and fraction
429 uint32 guard; /* First guard bit */
430 uint64 rest; /* Remaining guard bits */
432 int bexp; /* binary exponent */
433 int nzero; /* number of non-zero bits */
434 int sexp; /* scaling exponent */
436 char *bufferend; /* pointer to char after last digit */
438 /* Check for zero and treat it as a special case */
443 /* Convert the decimal digits to a binary integer. */
445 bufferend = buffer + ndigit;
448 while (buffer < bufferend) {
453 /* Check for zero and treat it as a special case */
458 /* Normalize value */
459 bexp = 64; /* convert from 64b int to fraction */
461 /* Count number of non-zeroes in value */
463 if ((value >> 32) != 0) { nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
464 if ((value >> (16 + nzero)) != 0) { nzero += 16; }
465 if ((value >> ( 8 + nzero)) != 0) { nzero += 8; }
466 if ((value >> ( 4 + nzero)) != 0) { nzero += 4; }
467 if ((value >> ( 2 + nzero)) != 0) { nzero += 2; }
468 if ((value >> ( 1 + nzero)) != 0) { nzero += 1; }
469 if ((value >> ( nzero)) != 0) { nzero += 1; }
472 value <<= /*(uint64)*/ (64 - nzero); //*TY 03/25/2000 - removed extraneous cast to uint64
475 /* At this point we have a 64b fraction and a binary exponent
476 * but have yet to incorporate the decimal exponent.
479 /* multiply by 10^dexp */
480 _Stl_tenscale(value, dexp, sexp);
483 if (bexp <= -1022) { /* HI denorm or underflow */
485 if (bexp < -53) { /* guaranteed underflow */
488 else { /* denorm or possible underflow */
489 int lead0 = 12 - bexp; /* 12 sign and exponent bits */
491 /* we must special case right shifts of more than 63 */
497 else if (lead0 == 64) {
498 rest = value & ((ULL(1)<< 63)-1);
500 guard = (uint32) ((value>> 63) & 1 );
502 guard = to_ulong((value>> 63) & 1 ); //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
507 rest = value & (((ULL(1) << lead0)-1)-1);
509 guard = (uint32) (((value>> lead0)-1) & 1);
510 #else //*TY 03/25/2000 -
511 guard = to_ulong(((value>> lead0)-1) & 1);
512 #endif //*TY 03/25/2000 -
513 value >>= /*(uint64)*/ lead0; /* exponent is zero */
517 if (guard && ((value & 1) || rest) ) {
519 if (value == (ULL(1) << 52)) { /* carry created normal number */
521 _Stl_set_exponent(value, 1);
526 else { /* not zero or denorm */
527 /* Round to 53 bits */
528 rest = value & (1<<10)-1;
531 guard = (uint32) value & 1;
532 #else //*TY 03/25/2000 -
533 guard = to_ulong(value & 1);
537 /* value&1 guard rest Action
545 if (((value&1)!=0) || (rest!=0)) {
547 if ((value >> 53) != 0) { /* carry all the way across */
548 value >>= 1; /* renormalize */
555 * IEEE Double Precision Format
556 * (From Table 7-8 of Kane and Heinrich)
561 * Exponent bias +1023
564 * Total width in bits 64
567 if (bexp > 1024) { /* overflow */
568 return numeric_limits<double>::infinity();
570 else { /* value is normal */
571 value &= ~(ULL(1) << 52); /* hide hidden bit */
572 _Stl_set_exponent(value, bexp + 1022); /* add bias */
576 _STLP_STATIC_ASSERT(sizeof(value) == sizeof(double))
577 return *((double *) &value);
582 static double _Stl_atod(char *buffer, int ndigit, int dexp) {
585 char *bufferend; /* pointer to char after last digit */
587 /* Check for zero and treat it as a special case */
593 /* Convert the decimal digits to a binary integer. */
595 bufferend = buffer + ndigit;
599 while (buffer < bufferend) {
604 /* Check for zero and treat it as a special case */
609 /* Normalize value */
610 int bexp = 64; /* convert from 64b int to fraction */
612 /* Count number of non-zeroes in value */
614 if ((vv.i64 >> 32) !=0 ) { nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
615 if ((vv.i64 >> (16 + nzero)) != 0) { nzero += 16; }
616 if ((vv.i64 >> ( 8 + nzero)) != 0) { nzero += 8; }
617 if ((vv.i64 >> ( 4 + nzero)) != 0) { nzero += 4; }
618 if ((vv.i64 >> ( 2 + nzero)) != 0) { nzero += 2; }
619 if ((vv.i64 >> ( 1 + nzero)) != 0) { nzero += 1; }
620 if ((vv.i64 >> ( nzero)) != 0) { nzero += 1; }
624 vv.i64 <<= nzero; //*TY 03/25/2000 - removed extraneous cast to uint64
627 /* At this point we have a 64b fraction and a binary exponent
628 * but have yet to incorporate the decimal exponent.
631 /* multiply by 10^dexp */
633 _Stl_tenscale(vv.i64, dexp, sexp);
636 if (bexp <= -1022) { /* HI denorm or underflow */
638 if (bexp < -53) { /* guaranteed underflow */
641 else { /* denorm or possible underflow */
646 lead0 = 12-bexp; /* 12 sign and exponent bits */
648 /* we must special case right shifts of more than 63 */
654 else if (lead0 == 64) {
655 rest = vv.i64 & ((ULL(1) << 63)-1);
657 guard = (uint32) ((vv.i64 >> 63) & 1 );
659 guard = to_ulong((vv.i64 >> 63) & 1 ); //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
664 rest = vv.i64 & (((ULL(1) << lead0)-1)-1);
666 guard = (uint32) (((vv.i64 >> lead0)-1) & 1);
667 #else //*TY 03/25/2000 -
668 guard = to_ulong(((vv.i64 >> lead0)-1) & 1);
669 #endif //*TY 03/25/2000 -
670 vv.i64 >>= /*(uint64)*/ lead0; /* exponent is zero */
674 if (guard && ( (vv.i64 & 1) || rest)) {
676 if (vv.i64 == (ULL(1) << 52)) { /* carry created normal number */
677 v.ieee.mantissa0 = 0;
678 v.ieee.mantissa1 = 0;
686 else { /* not zero or denorm */
687 /* Round to 53 bits */
688 uint64_t rest = vv.i64 & (1<<10)-1;
691 uint32_t guard = (uint32) vv.i64 & 1;
692 #else //*TY 03/25/2000 -
693 uint32_t guard = to_ulong(vv.i64 & 1);
697 /* value&1 guard rest Action
705 if (((vv.i64&1)!=0) || (rest!=0)) {
706 vv.i64++; /* round */
707 if ((vv.i64>>53)!=0) { /* carry all the way across */
708 vv.i64 >>= 1; /* renormalize */
715 * IEEE Double Precision Format
716 * (From Table 7-8 of Kane and Heinrich)
721 * Exponent bias +1023
724 * Total width in bits 64
727 if (bexp > 1024) { /* overflow */
728 return numeric_limits<double>::infinity();
730 else { /* value is normal */
731 vv.i64 &= ~(ULL(1) << 52); /* hide hidden bit */
732 v.ieee.mantissa0 = vv.i32.hi;
733 v.ieee.mantissa1 = vv.i32.lo;
735 v.ieee.exponent = bexp + 1022;
740 v.ieee.mantissa0 = vv.i32.hi;
741 v.ieee.mantissa1 = vv.i32.lo;
751 static double _Stl_string_to_double(const char *s) {
752 const int max_digits = 17;
754 unsigned Negate, decimal_point;
759 char digits[max_digits];
761 // Skip leading whitespace, if any.
762 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
763 while (c = *s++, ct.is(ctype_base::space, char(c))) {}
781 if (d == digits + max_digits) {
782 /* ignore more than 17 digits, but adjust exponent */
783 exp += (decimal_point ^ 1);
786 if (c == 0 && d == digits) {
787 /* ignore leading zeros */
792 exp -= decimal_point;
795 else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */
803 /* strtod cant return until it finds the end of the exponent */
808 if (c == 'e'-'0' || c == 'E'-'0') {
809 register unsigned negate_exp = 0;
812 if (c == '+' || c == ' ') {
819 if (c -= '0', c < 10) {
826 while (c -= '0', c < 10);
830 if (e < -340 || e > 340)
840 else if (exp > 308) {
841 x = numeric_limits<double>::infinity();
844 /* let _Stl_atod diagnose under- and over-flows */
845 /* if the input was == 0.0, we have already returned,
846 so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
848 x = _Stl_atod(digits, (int)(d - digits), exp);
857 #if !defined (_STLP_NO_LONG_DOUBLE)
859 * __string_to_long_double is just lifted from atold, the difference being
860 * that we just use '.' for the decimal point, rather than let it
861 * be taken from the current C locale, which of course is not accessible
866 _Stl_string_to_long_double(const char * s) {
867 const int max_digits = 34;
869 register unsigned Negate, decimal_point;
874 char digits[max_digits];
876 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
877 while (c = *s++, ct.is(ctype_base::space, char(c)))
898 if (d == digits+max_digits) {
899 /* ignore more than 34 digits, but adjust exponent */
900 exp += (decimal_point ^ 1);
903 if (c == 0 && d == digits) {
904 /* ignore leading zeros */
910 exp -= decimal_point;
913 else if ((char)c == dpchar && !decimal_point) { /* INTERNATIONAL */
925 if (c == 'e'-'0' || c == 'E'-'0') {
926 register unsigned negate_exp = 0;
929 if (c == '+' || c == ' ') {
936 if (c -= '0', c < 10) {
943 while (c -= '0', c < 10);
947 if (e < -(323+max_digits) || e > 308)
954 if (exp < -(324+max_digits)) {
957 else if (exp > 308) {
958 x = numeric_limits<long double>::infinity();
961 /* let _Stl_atod diagnose under- and over-flows */
962 /* if the input was == 0.0, we have already returned,
963 so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
966 // x = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1
967 double tmp = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1
968 x = tmp == numeric_limits<double>::infinity()
969 ? numeric_limits<long double>::infinity()
981 void _STLP_DECLSPEC _STLP_CALL
982 __string_to_float(const __iostring& v, float& val)
983 { val = (float)_Stl_string_to_double(v.c_str()); }
985 void _STLP_DECLSPEC _STLP_CALL
986 __string_to_float(const __iostring& v, double& val)
987 { val = _Stl_string_to_double(v.c_str()); }
989 #if !defined (_STLP_NO_LONG_DOUBLE)
990 void _STLP_DECLSPEC _STLP_CALL
991 __string_to_float(const __iostring& v, long double& val)
992 { val = _Stl_string_to_long_double(v.c_str()); }
995 _STLP_MOVE_TO_STD_NAMESPACE