]> git.buserror.net Git - polintos/scott/priv.git/blob - lib/c++/stlport/num_get_float.cpp
Add STLport 5.1.4
[polintos/scott/priv.git] / lib / c++ / stlport / num_get_float.cpp
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) || \
26     defined (__DMC__)
27 #  include <stdint.h>
28 #endif
29
30 #if defined (__linux__)
31 #  include <ieee754.h>
32
33 union _ll {
34   uint64_t i64;
35   struct {
36 #  if defined (_STLP_BIG_ENDIAN)
37     uint32_t hi;
38     uint32_t lo;
39 #  elif defined (_STLP_LITTLE_ENDIAN)
40     uint32_t lo;
41     uint32_t hi;
42 #  else
43 #    error Unknown endianess
44 #  endif
45   } i32;
46 };
47 #endif
48
49 #if defined (N_PLAT_NLM)
50 #  include <nlm/nwintxx.h>
51
52 #  if defined (INT64)
53 typedef unsigned INT64 uint64_t;
54 #  else
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
58 //        - ptr, 2005-05-06
59 typedef unsigned __int64 uint64_t;
60 #  endif
61
62 #  if defined (INT32)
63 typedef unsigned INT32 uint32_t;
64 #  else
65 #    error Can not find INT32
66 #  endif
67
68 union _ll {
69   uint64_t i64;
70   struct {
71     uint32_t lo;
72     uint32_t hi;
73   } i32;
74 };
75 #endif
76
77 _STLP_BEGIN_NAMESPACE
78 _STLP_MOVE_TO_PRIV_NAMESPACE
79
80 //----------------------------------------------------------------------
81 // num_get
82
83 // Helper functions for _M_do_get_float.
84
85 #if !defined (_STLP_NO_WCHAR_T)
86 void  _STLP_CALL
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,
90                        wchar_t* digits) {
91   char ndigits[11] = "0123456789";
92   Plus  = ct.widen('+');
93   Minus = ct.widen('-');
94   pow_e = ct.widen('e');
95   pow_E = ct.widen('E');
96   ct.widen(ndigits + 0, ndigits + 10, digits);
97 }
98 #endif /* WCHAR_T */
99
100 /*
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
104  * to us.
105  */
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
118 #else
119 #  error There should be some unsigned 64-bit integer on the system!
120 #endif
121
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
124 // hand-unrolled.
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;
132
133   uint64 t = u0 * v0;
134   low = t & low_mask;
135
136   t = u1 * v0 + (t >> 32);
137   uint64 w1 = t & low_mask;
138   uint64 w2 = t >> 32;
139
140   uint64 x = u0 * v1 + w1;
141   low += (x & low_mask) << 32;
142   high = u1 * v1 + w2 + (x >> 32);
143 }
144
145 #define bit11 ULL(0x7ff)
146 #define exponent_mask (bit11 << 52)
147
148 #if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
149     (!defined (__CYGWIN__) && !defined (__MINGW32__))
150 //Generate bad code when compiled with -O2 option.
151 inline
152 #endif
153 void _Stl_set_exponent(uint64 &val, uint64 exp)
154 { val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
155
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.
160  */
161
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) */
191
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) */
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 #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) */
246
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) */
257
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) */
271 #endif
272 };
273
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
278 };
279
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 */
283 #define  NUM_HI_P 11
284 #define  NUM_HI_N 13
285
286 #define _Stl_HIBITULL (ULL(1) << 63)
287
288 static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
289   norm = 0;
290   if ((prodhi & _Stl_HIBITULL) == 0) {
291                                 /* leading bit is a zero
292                                  * may have to normalize
293                                  */
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
298                                      */
299       p = _Stl_HIBITULL;
300       return;
301     }
302     p = (prodhi << 1) | (prodlo >> 63); /* normalize */
303     norm = 1;
304     prodlo <<= 1;
305   }
306   else {
307     p = prodhi;
308   }
309
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 -
318       /* round */
319       ++p;
320       if (p == 0)
321         ++p;
322     }
323   }
324
325   return;
326 }
327
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 */
338
339   bexp = 0;
340   if (exp > 0) {                 /* split exponent */
341     exp_lo = exp;
342     exp_hi = 0;
343     if (exp_lo > 27) {
344       exp_lo++;
345       while (exp_lo > 27) {
346         exp_hi++;
347         exp_lo -= 28;
348       }
349     }
350     tlo = TEN_1;
351     thi = TEN_27;
352     num_hi = NUM_HI_P;
353   }
354   else if (exp < 0) {
355     exp_lo = exp;
356     exp_hi = 0;
357     while (exp_lo < 0) {
358       exp_hi++;
359       exp_lo += 28;
360     }
361     tlo = TEN_1;
362     thi = TEN_M28;
363     num_hi = NUM_HI_N;
364   }
365   else {                        /* no scaling needed */
366     return;
367   }
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 */
371     hi += thi-1;
372     _Stl_mult64(p, _Stl_tenpow[hi], prodhi, prodlo);
373     _Stl_norm_and_round(p, norm, prodhi, prodlo);
374     bexp += _Stl_twoexp[hi] - norm;
375   }
376   if (exp_lo) {
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;
381   }
382
383   return;
384 }
385
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.
389
390 #if defined (__SC__) || defined (__MRC__)
391
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
394 _STLP_END_NAMESPACE
395
396 #  include <fp.h>
397
398 _STLP_BEGIN_NAMESPACE
399 _STLP_MOVE_TO_PRIV_NAMESPACE
400
401 static inline double _Stl_atod(char *buffer, int ndigit, int dexp) {
402   decimal d;  // ref. inside macintosh powerpc numerics p.9-13
403
404   d.sgn = 0;
405   d.exp = dexp;
406   d.sig.length = ndigit;
407   for (int i = 0; i < ndigit; ++i) {
408     d.sig.text[i] = buffer[i] + '0';
409   }
410   return dec2num(&d);
411 }
412
413 #else  /* IEEE representation */
414
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
422                                  */
423
424   uint32 guard;         /* First guard bit */
425   uint64 rest;          /* Remaining guard bits */
426
427   int bexp;             /* binary exponent */
428   int nzero;            /* number of non-zero bits */
429   int sexp;             /* scaling exponent */
430
431   char *bufferend;              /* pointer to char after last digit */
432
433   /* Check for zero and treat it as a special case */
434   if (buffer == 0){
435     return 0.0;
436   }
437
438   /* Convert the decimal digits to a binary integer. */
439
440   bufferend = buffer + ndigit;
441   value = 0;
442
443   while (buffer < bufferend) {
444     value *= 10;
445     value += *buffer++;
446   }
447
448   /* Check for zero and treat it as a special case */
449   if (value == 0) {
450     return 0.0;
451   }
452
453   /* Normalize value */
454   bexp = 64;                    /* convert from 64b int to fraction */
455
456   /* Count number of non-zeroes in value */
457   nzero = 0;
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; }
465
466   /* Normalize */
467   value <<= /*(uint64)*/ (64 - nzero);    //*TY 03/25/2000 - removed extraneous cast to uint64
468   bexp -= 64 - nzero;
469
470   /* At this point we have a 64b fraction and a binary exponent
471    * but have yet to incorporate the decimal exponent.
472    */
473
474   /* multiply by 10^dexp */
475   _Stl_tenscale(value, dexp, sexp);
476   bexp += sexp;
477
478   if (bexp <= -1022) {          /* HI denorm or underflow */
479     bexp += 1022;
480     if (bexp < -53) {          /* guaranteed underflow */
481       value = 0;
482     }
483     else {                      /* denorm or possible underflow */
484       int lead0 = 12 - bexp;          /* 12 sign and exponent bits */
485
486       /* we must special case right shifts of more than 63 */
487       if (lead0 > 64) {
488         rest = value;
489         guard = 0;
490         value = 0;
491       }
492       else if (lead0 == 64) {
493         rest = value & ((ULL(1)<< 63)-1);
494 #if !defined(__SC__)
495         guard = (uint32) ((value>> 63) & 1 );
496 #else
497         guard = to_ulong((value>> 63) & 1 );   //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
498 #endif
499         value = 0;
500       }
501       else {
502         rest = value & (((ULL(1) << lead0)-1)-1);
503 #if !defined(__SC__)
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 */
509       }
510
511       /* Round */
512       if (guard && ((value & 1) || rest) ) {
513         ++value;
514         if (value == (ULL(1) << 52)) { /* carry created normal number */
515           value = 0;
516           _Stl_set_exponent(value, 1);
517         }
518       }
519     }
520   }
521   else {                        /* not zero or denorm */
522     /* Round to 53 bits */
523     rest = value & (1<<10)-1;
524     value >>= 10;
525 #if !defined(__SC__)
526     guard = (uint32) value & 1;
527 #else    //*TY 03/25/2000 -
528     guard = to_ulong(value & 1);
529 #endif
530     value >>= 1;
531
532     /*  value&1 guard   rest    Action
533      *
534      *  dc      0       dc      none
535      *  1       1       dc      round
536      *  0       1       0       none
537      *  0       1       !=0     round
538      */
539     if (guard) {
540       if (((value&1)!=0) || (rest!=0)) {
541         ++value;                        /* round */
542         if ((value >> 53) != 0) {       /* carry all the way across */
543           value >>= 1;          /* renormalize */
544           ++bexp;
545         }
546       }
547     }
548     /*
549      * Check for overflow
550      * IEEE Double Precision Format
551      * (From Table 7-8 of Kane and Heinrich)
552      *
553      * Fraction bits               52
554      * Emax                     +1023
555      * Emin                     -1022
556      * Exponent bias            +1023
557      * Exponent bits               11
558      * Integer bit             hidden
559      * Total width in bits         64
560      */
561
562     if (bexp > 1024) {          /* overflow */
563       return numeric_limits<double>::infinity();
564     }
565     else {                      /* value is normal */
566       value &= ~(ULL(1) << 52);   /* hide hidden bit */
567       _Stl_set_exponent(value, bexp + 1022); /* add bias */
568     }
569   }
570
571   _STLP_STATIC_ASSERT(sizeof(value) == sizeof(double))
572   return *((double *) &value);
573 }
574
575 #  else // __linux__
576
577 static double _Stl_atod(char *buffer, int ndigit, int dexp) {
578   ieee754_double v;
579
580   char *bufferend;              /* pointer to char after last digit */
581
582   /* Check for zero and treat it as a special case */
583
584   if (buffer == 0) {
585     return 0.0;
586   }
587
588   /* Convert the decimal digits to a binary integer. */
589
590   bufferend = buffer + ndigit;
591   _ll vv;
592   vv.i64 = 0L;
593
594   while (buffer < bufferend) {
595     vv.i64 *= 10;
596     vv.i64 += *buffer++;
597   }
598
599   /* Check for zero and treat it as a special case */
600   if (vv.i64 == 0){
601     return 0.0;
602   }
603
604   /* Normalize value */
605   int bexp = 64;                    /* convert from 64b int to fraction */
606
607   /* Count number of non-zeroes in value */
608   int nzero = 0;
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; }
616
617   /* Normalize */
618   nzero = 64 - nzero;
619   vv.i64 <<= nzero;    //*TY 03/25/2000 - removed extraneous cast to uint64
620   bexp -= nzero;
621
622   /* At this point we have a 64b fraction and a binary exponent
623    * but have yet to incorporate the decimal exponent.
624    */
625
626   /* multiply by 10^dexp */
627   int sexp;
628   _Stl_tenscale(vv.i64, dexp, sexp);
629   bexp += sexp;
630
631   if (bexp <= -1022) {          /* HI denorm or underflow */
632     bexp += 1022;
633     if (bexp < -53) {           /* guaranteed underflow */
634       vv.i64 = 0;
635     }
636     else {                      /* denorm or possible underflow */
637       int lead0;
638       uint64_t rest;
639       uint32_t guard;
640
641       lead0 = 12-bexp;          /* 12 sign and exponent bits */
642
643       /* we must special case right shifts of more than 63 */
644       if (lead0 > 64) {
645         rest = vv.i64;
646         guard = 0;
647         vv.i64 = 0;
648       }
649       else if (lead0 == 64) {
650         rest = vv.i64 & ((ULL(1) << 63)-1);
651 #if !defined(__SC__)
652         guard = (uint32) ((vv.i64 >> 63) & 1 );
653 #else
654         guard = to_ulong((vv.i64 >> 63) & 1 );   //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization
655 #endif
656         vv.i64 = 0;
657       }
658       else {
659         rest = vv.i64 & (((ULL(1) << lead0)-1)-1);
660 #if !defined(__SC__)
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 */
666       }
667
668       /* Round */
669       if (guard && ( (vv.i64 & 1) || rest)) {
670         vv.i64++;
671         if (vv.i64 == (ULL(1) << 52)) { /* carry created normal number */
672           v.ieee.mantissa0 = 0;
673           v.ieee.mantissa1 = 0;
674           v.ieee.negative = 0;
675           v.ieee.exponent = 1;
676           return v.d;
677         }
678       }
679     }
680   }
681   else {                        /* not zero or denorm */
682     /* Round to 53 bits */
683     uint64_t rest = vv.i64 & (1<<10)-1;
684     vv.i64 >>= 10;
685 #if !defined(__SC__)
686     uint32_t guard = (uint32) vv.i64 & 1;
687 #else    //*TY 03/25/2000 -
688     uint32_t guard = to_ulong(vv.i64 & 1);
689 #endif
690     vv.i64 >>= 1;
691
692     /*  value&1 guard   rest    Action
693      *
694      *  dc      0       dc      none
695      *  1       1       dc      round
696      *  0       1       0       none
697      *  0       1       !=0     round
698      */
699     if (guard) {
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 */
704           ++bexp;
705         }
706       }
707     }
708     /*
709      * Check for overflow
710      * IEEE Double Precision Format
711      * (From Table 7-8 of Kane and Heinrich)
712      *
713      * Fraction bits               52
714      * Emax                     +1023
715      * Emin                     -1022
716      * Exponent bias            +1023
717      * Exponent bits               11
718      * Integer bit             hidden
719      * Total width in bits         64
720      */
721
722     if (bexp > 1024) {          /* overflow */
723       return numeric_limits<double>::infinity();
724     }
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;
729       v.ieee.negative = 0;
730       v.ieee.exponent = bexp + 1022;
731       return v.d;
732     }
733   }
734
735   v.ieee.mantissa0 = vv.i32.hi;
736   v.ieee.mantissa1 = vv.i32.lo;
737   v.ieee.negative = 0;
738   v.ieee.exponent = 0;
739
740   return v.d;
741 }
742 #  endif // __linux__
743
744 #endif
745
746 static double _Stl_string_to_double(const char *s) {
747   const int max_digits = 17;
748   unsigned c;
749   unsigned Negate, decimal_point;
750   char *d;
751   int exp;
752   double x;
753   int dpchar;
754   char digits[max_digits];
755
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))) {}
759
760   /* process sign */
761   Negate = 0;
762   if (c == '+') {
763     c = *s++;
764   }
765   else if (c == '-') {
766     Negate = 1;
767     c = *s++;
768   }
769   d = digits;
770   dpchar = '.' - '0';
771   decimal_point = 0;
772   exp = 0;
773   for (;;) {
774     c -= '0';
775     if (c < 10) {
776       if (d == digits + max_digits) {
777         /* ignore more than 17 digits, but adjust exponent */
778         exp += (decimal_point ^ 1);
779       }
780       else {
781         if (c == 0 && d == digits) {
782           /* ignore leading zeros */
783         }
784         else {
785           *d++ = (char) c;
786         }
787         exp -= decimal_point;
788       }
789     }
790     else if (c == (unsigned int) dpchar && !decimal_point) {    /* INTERNATIONAL */
791       decimal_point = 1;
792     }
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 0.0;
801   }
802
803   if (c == 'e'-'0' || c == 'E'-'0') {
804     register unsigned negate_exp = 0;
805     register int e = 0;
806     c = *s++;
807     if (c == '+' || c == ' ') {
808       c = *s++;
809     }
810     else if (c == '-') {
811       negate_exp = 1;
812       c = *s++;
813     }
814     if (c -= '0', c < 10) {
815       do {
816         if (e <= 340)
817           e = e * 10 + (int)c;
818         else break;
819         c = *s++;
820       }
821       while (c -= '0', c < 10);
822       if (negate_exp) {
823         e = -e;
824       }
825       if (e < -340 || e > 340)
826         exp = e;
827       else
828         exp += e;
829     }
830   }
831
832   if (exp < -340) {
833     x = 0;
834   }
835   else if (exp > 308) {
836     x = numeric_limits<double>::infinity();
837   }
838   else {
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
842     */
843     x = _Stl_atod(digits, (int)(d - digits), exp);
844   }
845   if (Negate) {
846     x = -x;
847   }
848   return x;
849 }
850
851
852 #if !defined (_STLP_NO_LONG_DOUBLE)
853 /*
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
857  * to us.
858  */
859
860 static long double
861 _Stl_string_to_long_double(const char * s) {
862   const int max_digits = 34;
863   register unsigned c;
864   register unsigned Negate, decimal_point;
865   register char *d;
866   register int exp;
867   long double x;
868   register int dpchar;
869   char digits[max_digits];
870
871   const ctype<char>& ct = use_facet<ctype<char> >(locale::classic());
872   while (c = *s++, ct.is(ctype_base::space, char(c)))
873     ;
874
875   /* process sign */
876   Negate = 0;
877   if (c == '+') {
878     c = *s++;
879   }
880   else if (c == '-') {
881     Negate = 1;
882     c = *s++;
883   }
884
885   d = digits;
886   dpchar = '.' - '0';
887   decimal_point = 0;
888   exp = 0;
889
890   for (;;) {
891     c -= '0';
892     if (c < 10) {
893       if (d == digits+max_digits) {
894         /* ignore more than 34 digits, but adjust exponent */
895         exp += (decimal_point ^ 1);
896       }
897       else {
898         if (c == 0 && d == digits) {
899           /* ignore leading zeros */
900           ;
901         }
902         else {
903           *d++ = (char)c;
904         }
905         exp -= decimal_point;
906       }
907     }
908     else if ((char)c == dpchar && !decimal_point) {    /* INTERNATIONAL */
909       decimal_point = 1;
910     }
911     else {
912       break;
913     }
914     c = *s++;
915   } /* for */
916
917   if (d == digits) {
918     return 0.0L;
919   }
920   if (c == 'e'-'0' || c == 'E'-'0') {
921     register unsigned negate_exp = 0;
922     register int e = 0;
923     c = *s++;
924     if (c == '+' || c == ' ') {
925       c = *s++;
926     }
927     else if (c == '-') {
928       negate_exp = 1;
929       c = *s++;
930     }
931     if (c -= '0', c < 10) {
932       do {
933         if (e <= 340)
934           e = e * 10 + c;
935         else break;
936         c = *s++;
937       }
938       while (c -= '0', c < 10);
939       if (negate_exp) {
940         e = -e;
941       }
942       if (e < -(323+max_digits) || e > 308)
943         exp = e;
944       else
945         exp += e;
946     }
947   }
948
949   if (exp < -(324+max_digits)) {
950     x = 0;
951   }
952   else if (exp > 308) {
953     x =  numeric_limits<long double>::infinity();
954   }
955   else {
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
959         */
960
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()
965       : tmp;
966   }
967
968   if (Negate) {
969     x = -x;
970   }
971
972   return x;
973 }
974 #endif
975
976 void _STLP_CALL
977 __string_to_float(const __iostring& v, float& val)
978 { val = (float)_Stl_string_to_double(v.c_str()); }
979
980 void _STLP_CALL
981 __string_to_float(const __iostring& v, double& val)
982 { val = _Stl_string_to_double(v.c_str()); }
983
984 #if !defined (_STLP_NO_LONG_DOUBLE)
985 void _STLP_CALL
986 __string_to_float(const __iostring& v, long double& val)
987 { val = _Stl_string_to_long_double(v.c_str()); }
988 #endif
989
990 _STLP_MOVE_TO_STD_NAMESPACE
991 _STLP_END_NAMESPACE
992
993 // Local Variables:
994 // mode:C++
995 // End: