1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
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.
16  *
17  */
18 
19 #include "stlport_prefix.h"
20 
21 #include <limits>
22 #include <locale>
23 #include <istream>
24 
25 #if (defined (__GNUC__) && !defined (__sun) && !defined (__hpux)) || \
26     defined (__DMC__)
27 #  include <stdint.h>
28 #endif
29 
30 #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
31     defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
32 
33 #  if defined (__BORLANDC__)
34 typedef unsigned int uint32_t;
35 typedef unsigned __int64 uint64_t;
36 #  endif
37 
38 union _ll {
39   uint64_t i64;
40   struct {
41 #  if defined (_STLP_BIG_ENDIAN)
42     uint32_t hi;
43     uint32_t lo;
44 #  elif defined (_STLP_LITTLE_ENDIAN)
45     uint32_t lo;
46     uint32_t hi;
47 #  else
48 #    error Unknown endianess
49 #  endif
50   } i32;
51 };
52 
53 #  if defined (__linux__) && !defined (__ANDROID__)
54 #    include <ieee754.h>
55 #  else
56 union ieee854_long_double {
57   long double d;
58 
59   /* This is the IEEE 854 double-extended-precision format.  */
60   struct {
61     unsigned int mantissa1:32;
62     unsigned int mantissa0:32;
63     unsigned int exponent:15;
64     unsigned int negative:1;
65     unsigned int empty:16;
66   } ieee;
67 };
68 
69 #    define IEEE854_LONG_DOUBLE_BIAS 0x3fff
70 #  endif
71 #endif
72 
73 _STLP_BEGIN_NAMESPACE
74 _STLP_MOVE_TO_PRIV_NAMESPACE
75 
76 //----------------------------------------------------------------------
77 // num_get
78 
79 // Helper functions for _M_do_get_float.
80 
81 #if !defined (_STLP_NO_WCHAR_T)
82 void  _STLP_CALL
_Initialize_get_float(const ctype<wchar_t> & ct,wchar_t & Plus,wchar_t & Minus,wchar_t & pow_e,wchar_t & pow_E,wchar_t * digits)83 _Initialize_get_float( const ctype<wchar_t>& ct,
84                        wchar_t& Plus, wchar_t& Minus,
85                        wchar_t& pow_e, wchar_t& pow_E,
86                        wchar_t* digits) {
87   char ndigits[11] = "0123456789";
88   Plus  = ct.widen('+');
89   Minus = ct.widen('-');
90   pow_e = ct.widen('e');
91   pow_E = ct.widen('E');
92   ct.widen(ndigits + 0, ndigits + 10, digits);
93 }
94 #endif /* WCHAR_T */
95 
96 /*
97  * __string_to_double is just lifted from atof, the difference being
98  * that we just use '.' for the decimal point, rather than let it
99  * be taken from the current C locale, which of course is not accessible
100  * to us.
101  */
102 #if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
103 typedef unsigned long uint32;
104 typedef unsigned __int64 uint64;
105 #  define ULL(x) x##Ui64
106 #elif defined (__unix) || defined (__MINGW32__) || \
107       (defined (__DMC__) && (__LONGLONG)) || defined (__WATCOMC__) || \
108       defined (__ANDROID__)
109 typedef uint32_t uint32;
110 typedef uint64_t uint64;
111 #  define ULL(x) x##ULL
112 #else
113 #  error There should be some unsigned 64-bit integer on the system!
114 #endif
115 
116 // Multiplication of two 64-bit integers, giving a 128-bit result.
117 // Taken from Algorithm M in Knuth section 4.3.1, with the loop
118 // hand-unrolled.
_Stl_mult64(const uint64 u,const uint64 v,uint64 & high,uint64 & low)119 static void _Stl_mult64(const uint64 u, const uint64 v,
120                         uint64& high, uint64& low) {
121   const uint64 low_mask = ULL(0xffffffff);
122   const uint64 u0 = u & low_mask;
123   const uint64 u1 = u >> 32;
124   const uint64 v0 = v & low_mask;
125   const uint64 v1 = v >> 32;
126 
127   uint64 t = u0 * v0;
128   low = t & low_mask;
129 
130   t = u1 * v0 + (t >> 32);
131   uint64 w1 = t & low_mask;
132   uint64 w2 = t >> 32;
133 
134   uint64 x = u0 * v1 + w1;
135   low += (x & low_mask) << 32;
136   high = u1 * v1 + w2 + (x >> 32);
137 }
138 
139 #if !defined (__linux__) || defined (__ANDROID__)
140 
141 #  define bit11 ULL(0x7ff)
142 #  define exponent_mask (bit11 << 52)
143 
144 #  if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
145       (!defined (__CYGWIN__) && !defined (__MINGW32__))
146 //Generate bad code when compiled with -O2 option.
147 inline
148 #  endif
_Stl_set_exponent(uint64 & val,uint64 exp)149 void _Stl_set_exponent(uint64 &val, uint64 exp)
150 { val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
151 
152 #endif // __linux__
153 
154 /* Power of ten fractions for tenscale*/
155 /* The constants are factored so that at most two constants
156  * and two multiplies are needed. Furthermore, one of the constants
157  * is represented exactly - 10**n where 1<= n <= 27.
158  */
159 
160 static const uint64 _Stl_tenpow[80] = {
161 ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
162 ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
163 ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
164 ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
165 ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
166 ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
167 ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
168 ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
169 ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
170 ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
171 ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
172 ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
173 ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
174 ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
175 ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
176 ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
177 ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
178 ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
179 ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
180 ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
181 ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
182 ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
183 ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
184 ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
185 ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
186 ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
187 ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
188 
189 ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
190 ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
191 ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
192 ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
193 ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
194 ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
195 ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
196 ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
197 ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
198 ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
199 
200 // /* _Stl_tenpow[36]=(10**335)/(2**) */
201 // /* _Stl_tenpow[36]=(10**335)/(2**) */
202 
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) */
216 };
217 
218 static const short _Stl_twoexp[80] = {
219 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,
220 183,276,369,462,555,648,741,834,927,1020,
221 -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
222 };
223 
224 #define  TEN_1  0           /* offset to 10 **   1 */
225 #define  TEN_27   26        /* offset to 10 **  27 */
226 #define  TEN_M28  37        /* offset to 10 ** -28 */
227 #define  NUM_HI_P 11
228 #define  NUM_HI_N 13
229 
230 #define _Stl_HIBITULL (ULL(1) << 63)
231 
_Stl_norm_and_round(uint64 & p,int & norm,uint64 prodhi,uint64 prodlo)232 static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
233   norm = 0;
234   if ((prodhi & _Stl_HIBITULL) == 0) {
235                                 /* leading bit is a zero
236                                  * may have to normalize
237                                  */
238     if ((prodhi == ~_Stl_HIBITULL) &&
239         ((prodlo >> 62) == 0x3)) {  /* normalization followed by round
240                                      * would cause carry to create
241                                      * extra bit, so don't normalize
242                                      */
243       p = _Stl_HIBITULL;
244       return;
245     }
246     p = (prodhi << 1) | (prodlo >> 63); /* normalize */
247     norm = 1;
248     prodlo <<= 1;
249   }
250   else {
251     p = prodhi;
252   }
253 
254   if ((prodlo & _Stl_HIBITULL) != 0) {     /* first guard bit a one */
255     if (((p & 0x1) != 0) ||
256         prodlo != _Stl_HIBITULL ) {    /* not borderline for round to even */
257       /* round */
258       ++p;
259       if (p == 0)
260         ++p;
261     }
262   }
263 }
264 
265 // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
266 // p:    64-bit fraction
267 // exp:  base-10 exponent
268 // bexp: base-2 exponent (output parameter)
_Stl_tenscale(uint64 & p,int exp,int & bexp)269 static void _Stl_tenscale(uint64& p, int exp, int& bexp) {
270   bexp = 0;
271 
272   if ( exp == 0 ) {              /* no scaling needed */
273     return;
274   }
275 
276   int exp_hi = 0, exp_lo = exp; /* exp = exp_hi*32 + exp_lo */
277   int tlo = TEN_1, thi;         /* offsets in power of ten table */
278   int num_hi;                   /* number of high exponent powers */
279 
280   if (exp > 0) {                /* split exponent */
281     if (exp_lo > 27) {
282       exp_lo++;
283       while (exp_lo > 27) {
284         exp_hi++;
285         exp_lo -= 28;
286       }
287     }
288     thi = TEN_27;
289     num_hi = NUM_HI_P;
290   } else { // exp < 0
291     while (exp_lo < 0) {
292       exp_hi++;
293       exp_lo += 28;
294     }
295     thi = TEN_M28;
296     num_hi = NUM_HI_N;
297   }
298 
299   uint64 prodhi, prodlo;        /* 128b product */
300   int norm;                     /* number of bits of normalization */
301 
302   int hi, lo;                   /* offsets in power of ten table */
303   while (exp_hi) {              /* scale */
304     hi = (min) (exp_hi, num_hi);    /* only a few large powers of 10 */
305     exp_hi -= hi;               /* could iterate in extreme case */
306     hi += thi-1;
307     _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
308     _Stl_norm_and_round(p, norm, prodhi, prodlo);
309     bexp += _Stl_twoexp[hi] - norm;
310   }
311 
312   if (exp_lo) {
313     lo = tlo + exp_lo -1;
314     _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo);
315     _Stl_norm_and_round(p, norm, prodhi, prodlo);
316     bexp += _Stl_twoexp[lo] - norm;
317   }
318 
319   return;
320 }
321 
322 // First argument is a buffer of values from 0 to 9, NOT ascii.
323 // Second argument is number of digits in buffer, 1 <= digits <= 17.
324 // Third argument is base-10 exponent.
325 
326 /* IEEE representation */
327 #if !defined (__linux__) || defined (__ANDROID__)
328 
329 union _Double_rep {
330   uint64 ival;
331   double val;
332 };
333 
_Stl_atod(char * buffer,ptrdiff_t ndigit,int dexp)334 static double _Stl_atod(char *buffer, ptrdiff_t ndigit, int dexp) {
335   typedef numeric_limits<double> limits;
336   _Double_rep drep;
337   uint64 &value = drep.ival;  /* Value develops as follows:
338                                  * 1) decimal digits as an integer
339                                  * 2) left adjusted fraction
340                                  * 3) right adjusted fraction
341                                  * 4) exponent and fraction
342                                  */
343 
344   uint32 guard;         /* First guard bit */
345   uint64 rest;          /* Remaining guard bits */
346 
347   int bexp;             /* binary exponent */
348   int nzero;            /* number of non-zero bits */
349   int sexp;             /* scaling exponent */
350 
351   char *bufferend;              /* pointer to char after last digit */
352 
353   /* Convert the decimal digits to a binary integer. */
354   bufferend = buffer + ndigit;
355   value = 0;
356 
357   while (buffer < bufferend) {
358     value *= 10;
359     value += *buffer++;
360   }
361 
362   /* Check for zero and treat it as a special case */
363   if (value == 0) {
364     return 0.0;
365   }
366 
367   /* Normalize value */
368   bexp = 64;                    /* convert from 64b int to fraction */
369 
370   /* Count number of non-zeroes in value */
371   nzero = 0;
372   if ((value >> 32) != 0) { nzero  = 32; }    //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator
373   if ((value >> (16 + nzero)) != 0) { nzero += 16; }
374   if ((value >> ( 8 + nzero)) != 0) { nzero +=  8; }
375   if ((value >> ( 4 + nzero)) != 0) { nzero +=  4; }
376   if ((value >> ( 2 + nzero)) != 0) { nzero +=  2; }
377   if ((value >> ( 1 + nzero)) != 0) { nzero +=  1; }
378   if ((value >> (     nzero)) != 0) { nzero +=  1; }
379 
380   /* Normalize */
381   value <<= /*(uint64)*/ (64 - nzero);    //*TY 03/25/2000 - removed extraneous cast to uint64
382   bexp -= 64 - nzero;
383 
384   /* At this point we have a 64b fraction and a binary exponent
385    * but have yet to incorporate the decimal exponent.
386    */
387 
388   /* multiply by 10^dexp */
389   _Stl_tenscale(value, dexp, sexp);
390   bexp += sexp;
391 
392   if (bexp <= -1022) {          /* HI denorm or underflow */
393     bexp += 1022;
394     if (bexp < -53) {          /* guaranteed underflow */
395       value = 0;
396     }
397     else {                      /* denorm or possible underflow */
398       int lead0 = 12 - bexp;          /* 12 sign and exponent bits */
399 
400       /* we must special case right shifts of more than 63 */
401       if (lead0 > 64) {
402         rest = value;
403         guard = 0;
404         value = 0;
405       }
406       else if (lead0 == 64) {
407         rest = value & ((ULL(1)<< 63)-1);
408         guard = (uint32) ((value>> 63) & 1 );
409         value = 0;
410       }
411       else {
412         rest = value & (((ULL(1) << lead0)-1)-1);
413         guard = (uint32) (((value>> lead0)-1) & 1);
414         value >>= /*(uint64)*/ lead0; /* exponent is zero */
415       }
416 
417       /* Round */
418       if (guard && ((value & 1) || rest) ) {
419         ++value;
420         if (value == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */
421           value = 0;
422           _Stl_set_exponent(value, 1);
423         }
424       }
425     }
426   }
427   else {                        /* not zero or denorm */
428     /* Round to 53 bits */
429     rest = value & ((1 << 10) - 1);
430     value >>= 10;
431     guard = (uint32) value & 1;
432     value >>= 1;
433 
434     /*  value&1 guard   rest    Action
435      *
436      *  dc      0       dc      none
437      *  1       1       dc      round
438      *  0       1       0       none
439      *  0       1       !=0     round
440      */
441     if (guard) {
442       if (((value&1)!=0) || (rest!=0)) {
443         ++value;                        /* round */
444         if ((value >> 53) != 0) {       /* carry all the way across */
445           value >>= 1;          /* renormalize */
446           ++bexp;
447         }
448       }
449     }
450     /*
451      * Check for overflow
452      * IEEE Double Precision Format
453      * (From Table 7-8 of Kane and Heinrich)
454      *
455      * Fraction bits               52
456      * Emax                     +1023
457      * Emin                     -1022
458      * Exponent bias            +1023
459      * Exponent bits               11
460      * Integer bit             hidden
461      * Total width in bits         64
462      */
463 
464     if (bexp > limits::max_exponent) {          /* overflow */
465       return limits::infinity();
466     }
467     else {                      /* value is normal */
468       value &= ~(ULL(1) << (limits::digits - 1));   /* hide hidden bit */
469       _Stl_set_exponent(value, bexp + 1022); /* add bias */
470     }
471   }
472 
473   _STLP_STATIC_ASSERT(sizeof(uint64) >= sizeof(double))
474   return drep.val;
475 }
476 
477 #endif
478 
479 #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
480     defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
481 
482 template <class D, class IEEE, int M, int BIAS>
_Stl_atodT(char * buffer,ptrdiff_t ndigit,int dexp)483 D _Stl_atodT(char *buffer, ptrdiff_t ndigit, int dexp)
484 {
485   typedef numeric_limits<D> limits;
486 
487   /* Convert the decimal digits to a binary integer. */
488   char *bufferend = buffer + ndigit; /* pointer to char after last digit */
489   _ll vv;
490   vv.i64 = 0L;
491 
492   while ( buffer < bufferend ) {
493     vv.i64 *= 10;
494     vv.i64 += *buffer++;
495   }
496 
497   if ( vv.i64 == ULL(0) ) { /* Check for zero and treat it as a special case */
498     return D(0.0);
499   }
500 
501   /* Normalize value */
502 
503   int bexp = 64; /* convert from 64b int to fraction */
504 
505   /* Count number of non-zeroes in value */
506   int nzero = 0;
507   if ((vv.i64 >> 32) != 0) { nzero = 32; }
508   if ((vv.i64 >> (16 + nzero)) != 0) { nzero += 16; }
509   if ((vv.i64 >> ( 8 + nzero)) != 0) { nzero +=  8; }
510   if ((vv.i64 >> ( 4 + nzero)) != 0) { nzero +=  4; }
511   if ((vv.i64 >> ( 2 + nzero)) != 0) { nzero +=  2; }
512   if ((vv.i64 >> ( 1 + nzero)) != 0) { nzero +=  1; }
513   if ((vv.i64 >> (     nzero)) != 0) { nzero +=  1; }
514 
515   /* Normalize */
516   nzero = 64 - nzero;
517   vv.i64 <<= nzero;    // * TY 03/25/2000 - removed extraneous cast to uint64
518   bexp -= nzero;
519 
520   /* At this point we have a 64b fraction and a binary exponent
521    * but have yet to incorporate the decimal exponent.
522    */
523 
524   /* multiply by 10^dexp */
525   int sexp;
526   _Stl_tenscale(vv.i64, dexp, sexp);
527   bexp += sexp;
528 
529   if ( bexp >= limits::min_exponent ) { /* not zero or denorm */
530     if ( limits::digits < 64 ) {
531       /* Round to (64 - M + 1) bits */
532       uint64_t rest = vv.i64 & ((~ULL(0) / ULL(2)) >> (limits::digits - 1));
533       vv.i64 >>= M - 2;
534       uint32_t guard = (uint32) vv.i64 & 1;
535       vv.i64 >>= 1;
536 
537       /*  value&1 guard   rest    Action
538        *
539        *  dc      0       dc      none
540        *  1       1       dc      round
541        *  0       1       0       none
542        *  0       1       !=0     round
543        */
544 
545       if (guard) {
546         if ( ((vv.i64 & 1) != 0) || (rest != 0) ) {
547           vv.i64++;       /* round */
548           if ( (vv.i64 >> (limits::digits < 64 ? limits::digits : 0)) != 0 ) { /* carry all the way across */
549             vv.i64 >>= 1; /* renormalize */
550             ++bexp;
551           }
552         }
553       }
554 
555       vv.i64 &= ~(ULL(1) << (limits::digits - 1)); /* hide hidden bit */
556     }
557     /*
558      * Check for overflow
559      * IEEE Double Precision Format
560      * (From Table 7-8 of Kane and Heinrich)
561      *
562      * Fraction bits               52
563      * Emax                     +1023
564      * Emin                     -1022
565      * Exponent bias            +1023
566      * Exponent bits               11
567      * Integer bit             hidden
568      * Total width in bits         64
569      */
570 
571     if (bexp > limits::max_exponent) { /* overflow */
572       return limits::infinity();
573     }
574 
575     /* value is normal */
576 
577     IEEE v;
578 
579     v.ieee.mantissa0 = vv.i32.hi;
580     v.ieee.mantissa1 = vv.i32.lo;
581     v.ieee.negative = 0;
582     v.ieee.exponent = bexp + BIAS - 1;
583 
584     return v.d;
585   }
586 
587   /* HI denorm or underflow */
588   bexp += BIAS - 1;
589   if (bexp < -limits::digits) { /* guaranteed underflow */
590     vv.i64 = 0;
591   } else {  /* denorm or possible underflow */
592 
593     /*
594      * Problem point for long double: looks like this code reflect shareing of mantissa
595      * and exponent in 64b int; not so for long double
596      */
597 
598     int lead0 = M - bexp; /* M = 12 sign and exponent bits */
599     uint64_t rest;
600     uint32_t guard;
601 
602     /* we must special case right shifts of more than 63 */
603 
604     if (lead0 > 64) {
605       rest = vv.i64;
606       guard = 0;
607       vv.i64 = 0;
608     } else if (lead0 == 64) {
609       rest = vv.i64 & ((ULL(1) << 63)-1);
610       guard = (uint32) ((vv.i64 >> 63) & 1 );
611       vv.i64 = 0;
612     } else {
613       rest = vv.i64 & (((ULL(1) << lead0)-1)-1);
614       guard = (uint32) (((vv.i64 >> lead0)-1) & 1);
615       vv.i64 >>= /*(uint64)*/ lead0; /* exponent is zero */
616     }
617 
618     /* Round */
619     if (guard && ( (vv.i64 & 1) || rest)) {
620       vv.i64++;
621       if (vv.i64 == (ULL(1) << (limits::digits - 1))) { /* carry created normal number */
622         IEEE v;
623 
624         v.ieee.mantissa0 = 0;
625         v.ieee.mantissa1 = 0;
626         v.ieee.negative = 0;
627         v.ieee.exponent = 1;
628         return v.d;
629       }
630     }
631   }
632 
633   IEEE v;
634 
635   v.ieee.mantissa0 = vv.i32.hi;
636   v.ieee.mantissa1 = vv.i32.lo;
637   v.ieee.negative = 0;
638   v.ieee.exponent = 0;
639 
640   return v.d;
641 }
642 #endif // __linux__
643 
644 #if !defined (__linux__) || defined (__ANDROID__)
_Stl_string_to_double(const char * s)645 static double _Stl_string_to_double(const char *s) {
646   typedef numeric_limits<double> limits;
647   const int max_digits = limits::digits10 + 2;
648   unsigned c;
649   unsigned Negate, decimal_point;
650   char *d;
651   int exp;
652   int dpchar;
653   char digits[max_digits];
654 
655   c = *s++;
656 
657   /* process sign */
658   Negate = 0;
659   if (c == '+') {
660     c = *s++;
661   } else if (c == '-') {
662     Negate = 1;
663     c = *s++;
664   }
665 
666   d = digits;
667   dpchar = '.' - '0';
668   decimal_point = 0;
669   exp = 0;
670 
671   for (;;) {
672     c -= '0';
673     if (c < 10) {
674       if (d == digits + max_digits) {
675         /* ignore more than max_digits digits, but adjust exponent */
676         exp += (decimal_point ^ 1);
677       } else {
678         if (c == 0 && d == digits) {
679           /* ignore leading zeros */
680         } else {
681           *d++ = (char) c;
682         }
683         exp -= decimal_point;
684       }
685     } else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */
686       decimal_point = 1;
687     } else {
688       break;
689     }
690     c = *s++;
691   }
692 
693   /* strtod cant return until it finds the end of the exponent */
694   if (d == digits) {
695     return 0.0;
696   }
697 
698   if (c == 'e' - '0' || c == 'E' - '0') {
699     register unsigned negate_exp = 0;
700     register int e = 0;
701     c = *s++;
702     if (c == '+' || c == ' ') {
703       c = *s++;
704     } else if (c == '-') {
705       negate_exp = 1;
706       c = *s++;
707     }
708     if (c -= '0', c < 10) {
709       do {
710         e = e * 10 + (int)c;
711         c = *s++;
712       } while (c -= '0', c < 10);
713 
714       if (negate_exp) {
715         e = -e;
716       }
717       exp += e;
718     }
719   }
720 
721   double x;
722   ptrdiff_t n = d - digits;
723   if ((exp + n - 1) < limits::min_exponent10) {
724     x = 0;
725   }
726   else if ((exp + n - 1) > limits::max_exponent10) {
727     x = limits::infinity();
728   }
729   else {
730     /* Let _Stl_atod diagnose under- and over-flows.
731      * If the input was == 0.0, we have already returned,
732      * so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW */
733     x = _Stl_atod(digits, n, exp);
734   }
735 
736   if (Negate) {
737     x = -x;
738   }
739 
740   return x;
741 }
742 
743 #endif
744 
745 #if defined (__linux__) || defined (__MINGW32__) || defined (__CYGWIN__) || \
746     defined (__BORLANDC__) || defined (__DMC__) || defined (__HP_aCC)
747 
748 template <class D, class IEEE, int M, int BIAS>
_Stl_string_to_doubleT(const char * s)749 D _Stl_string_to_doubleT(const char *s)
750 {
751   typedef numeric_limits<D> limits;
752   const int max_digits = limits::digits10; /* + 2 17 */;
753   unsigned c;
754   unsigned decimal_point;
755   char *d;
756   int exp;
757   D x;
758   int dpchar;
759   char digits[max_digits];
760 
761   c = *s++;
762 
763   /* process sign */
764   bool Negate = false;
765   if (c == '+') {
766     c = *s++;
767   } else if (c == '-') {
768     Negate = true;
769     c = *s++;
770   }
771 
772   d = digits;
773   dpchar = '.' - '0';
774   decimal_point = 0;
775   exp = 0;
776 
777   for (;;) {
778     c -= '0';
779     if (c < 10) {
780       if (d == digits + max_digits) {
781         /* ignore more than max_digits digits, but adjust exponent */
782         exp += (decimal_point ^ 1);
783       } else {
784         if (c == 0 && d == digits) {
785           /* ignore leading zeros */
786         } else {
787           *d++ = (char) c;
788         }
789         exp -= decimal_point;
790       }
791     } else if (c == (unsigned int) dpchar && !decimal_point) {    /* INTERNATIONAL */
792       decimal_point = 1;
793     } else {
794       break;
795     }
796     c = *s++;
797   }
798   /* strtod cant return until it finds the end of the exponent */
799   if (d == digits) {
800     return D(0.0);
801   }
802 
803   if (c == 'e'-'0' || c == 'E'-'0') {
804     bool negate_exp = false;
805     register int e = 0;
806     c = *s++;
807     if (c == '+' || c == ' ') {
808       c = *s++;
809     } else if (c == '-') {
810       negate_exp = true;
811       c = *s++;
812     }
813     if (c -= '0', c < 10) {
814       do {
815         e = e * 10 + (int)c;
816         c = *s++;
817       } while (c -= '0', c < 10);
818 
819       if (negate_exp) {
820         e = -e;
821       }
822       exp += e;
823     }
824   }
825 
826   ptrdiff_t n = d - digits;
827   if ((exp + n - 1) < limits::min_exponent10) {
828     return D(0.0); // +0.0 is the same as -0.0
829   } else if ((exp + n - 1) > limits::max_exponent10 ) {
830     // not good, because of x = -x below; this may lead to portability problems
831     x = limits::infinity();
832   } else {
833     /* let _Stl_atod diagnose under- and over-flows */
834     /* if the input was == 0.0, we have already returned,
835        so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW
836     */
837     x = _Stl_atodT<D,IEEE,M,BIAS>(digits, n, exp);
838   }
839 
840   return Negate ? -x : x;
841 }
842 
843 #endif // __linux__
844 
845 void _STLP_CALL
__string_to_float(const __iostring & v,float & val)846 __string_to_float(const __iostring& v, float& val)
847 {
848 #if !defined (__linux__) || defined (__ANDROID__)
849   val = (float)_Stl_string_to_double(v.c_str());
850 #else
851   val = (float)_Stl_string_to_doubleT<double,ieee754_double,12,IEEE754_DOUBLE_BIAS>(v.c_str());
852 #endif
853 }
854 
855 void _STLP_CALL
__string_to_float(const __iostring & v,double & val)856 __string_to_float(const __iostring& v, double& val)
857 {
858 #if !defined (__linux__) || defined (__ANDROID__)
859   val = _Stl_string_to_double(v.c_str());
860 #else
861   val = _Stl_string_to_doubleT<double,ieee754_double,12,IEEE754_DOUBLE_BIAS>(v.c_str());
862 #endif
863 }
864 
865 #if !defined (_STLP_NO_LONG_DOUBLE)
866 void _STLP_CALL
__string_to_float(const __iostring & v,long double & val)867 __string_to_float(const __iostring& v, long double& val) {
868 #if !defined (__linux__) && !defined (__MINGW32__) && !defined (__CYGWIN__) && \
869     !defined (__BORLANDC__) && !defined (__DMC__) && !defined (__HP_aCC)
870   //The following function is valid only if long double is an alias for double.
871   _STLP_STATIC_ASSERT( sizeof(long double) <= sizeof(double) )
872   val = _Stl_string_to_double(v.c_str());
873 #else
874   val = _Stl_string_to_doubleT<long double,ieee854_long_double,16,IEEE854_LONG_DOUBLE_BIAS>(v.c_str());
875 #endif
876 }
877 #endif
878 
879 _STLP_MOVE_TO_STD_NAMESPACE
880 _STLP_END_NAMESPACE
881 
882 // Local Variables:
883 // mode:C++
884 // End:
885