|
1 /* |
|
2 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. |
|
3 * Copyright (c) 1999 |
|
4 * Silicon Graphics Computer Systems, Inc. |
|
5 * |
|
6 * Copyright (c) 1999 |
|
7 * Boris Fomitchev |
|
8 * |
|
9 * This material is provided "as is", with absolutely no warranty expressed |
|
10 * or implied. Any use is at your own risk. |
|
11 * |
|
12 * Permission to use or copy this software for any purpose is hereby granted |
|
13 * without fee, provided the above notices are retained on all copies. |
|
14 * Permission to modify the code and to distribute modified code is granted, |
|
15 * provided the above notices are retained, and a notice that the code was |
|
16 * modified is included with the above copyright notice. |
|
17 * |
|
18 */ |
|
19 |
|
20 # include "stlport_prefix.h" |
|
21 #include <stl/_limits.h> |
|
22 #include <stl/_num_get.h> |
|
23 #include <stl/_istream.h> |
|
24 #ifdef __SYMBIAN32__ |
|
25 #include <stdlib.h> |
|
26 #include <errno.h> |
|
27 #endif |
|
28 |
|
29 _STLP_BEGIN_NAMESPACE |
|
30 |
|
31 //---------------------------------------------------------------------- |
|
32 // num_get |
|
33 |
|
34 |
|
35 /* |
|
36 * __string_to_double is just lifted from atof, the difference being |
|
37 * that we just use '.' for the decimal point, rather than let it |
|
38 * be taken from the current C locale, which of course is not accessible |
|
39 * to us. |
|
40 */ |
|
41 |
|
42 typedef unsigned int uint32; |
|
43 # if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL) |
|
44 # define ULL(x) x##Ui64 |
|
45 typedef unsigned _STLP_LONG_LONG uint64; |
|
46 # elif defined (_STLP_LONG_LONG) |
|
47 typedef unsigned _STLP_LONG_LONG uint64; |
|
48 # define ULL(x) x##ULL |
|
49 # elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers |
|
50 # include "uint64.h" //*TY 03/25/2000 - added 64bit math type definition |
|
51 # else |
|
52 # error "there should be some long long type on the system!" |
|
53 # define NUMERIC_NO_64 1 |
|
54 # endif |
|
55 |
|
56 // Multiplication of two 64-bit integers, giving a 128-bit result. |
|
57 // Taken from Algorithm M in Knuth section 4.3.1, with the loop |
|
58 // hand-unrolled. |
|
59 void _Stl_mult64(const uint64 u, const uint64 v, |
|
60 uint64& high, uint64& low) |
|
61 { |
|
62 const uint64 low_mask = ULL(0xffffffff); |
|
63 const uint64 u0 = u & low_mask; |
|
64 const uint64 u1 = u >> 32; |
|
65 const uint64 v0 = v & low_mask; |
|
66 const uint64 v1 = v >> 32; |
|
67 |
|
68 uint64 t = u0 * v0; |
|
69 low = t & low_mask; |
|
70 |
|
71 t = u1 * v0 + (t >> 32); |
|
72 uint64 w1 = t & low_mask; |
|
73 uint64 w2 = t >> 32; |
|
74 |
|
75 uint64 x = u0 * v1 + w1; |
|
76 low += (x & low_mask) << 32; |
|
77 high = u1 * v1 + w2 + (x >> 32); |
|
78 } |
|
79 |
|
80 # define bit11 ULL(0x7ff) |
|
81 # define exponent_mask (bit11 << 52) |
|
82 |
|
83 inline void _Stl_set_exponent(uint64& val, uint64 exp) |
|
84 { |
|
85 val = (val & ~exponent_mask) | ((exp & bit11) << 52); |
|
86 } |
|
87 |
|
88 /* Power of ten fractions for tenscale*/ |
|
89 /* The constants are factored so that at most two constants |
|
90 * and two multiplies are needed. Furthermore, one of the constants |
|
91 * is represented exactly - 10**n where 1<= n <= 27. |
|
92 */ |
|
93 |
|
94 #if !defined(__SC__) //*TY 03/25/2000 - no native 64bit integer under SCpp |
|
95 static const uint64 _Stl_tenpow[80] = { |
|
96 ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */ |
|
97 ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */ |
|
98 ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */ |
|
99 ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */ |
|
100 ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */ |
|
101 ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */ |
|
102 ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */ |
|
103 ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */ |
|
104 ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */ |
|
105 ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */ |
|
106 ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */ |
|
107 ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */ |
|
108 ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */ |
|
109 ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */ |
|
110 ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */ |
|
111 ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */ |
|
112 ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */ |
|
113 ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */ |
|
114 ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */ |
|
115 ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */ |
|
116 ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */ |
|
117 ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */ |
|
118 ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */ |
|
119 ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */ |
|
120 ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */ |
|
121 ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */ |
|
122 ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */ |
|
123 |
|
124 ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */ |
|
125 ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */ |
|
126 ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */ |
|
127 ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */ |
|
128 ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */ |
|
129 ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */ |
|
130 ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */ |
|
131 ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */ |
|
132 ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */ |
|
133 ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */ |
|
134 |
|
135 ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */ |
|
136 ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */ |
|
137 ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */ |
|
138 ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */ |
|
139 ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */ |
|
140 ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */ |
|
141 ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */ |
|
142 ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */ |
|
143 ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */ |
|
144 ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */ |
|
145 ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */ |
|
146 ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */ |
|
147 ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */ |
|
148 |
|
149 #else //*TY 03/20/2000 - added support for SCpp which lacks native 64bit integer type |
|
150 static const UnsignedWide _Stl_tenpow[80] = { |
|
151 ULL2(0xa0000000,0x00000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */ |
|
152 ULL2(0xc8000000,0x00000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */ |
|
153 ULL2(0xfa000000,0x00000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */ |
|
154 ULL2(0x9c400000,0x00000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */ |
|
155 ULL2(0xc3500000,0x00000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */ |
|
156 ULL2(0xf4240000,0x00000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */ |
|
157 ULL2(0x98968000,0x00000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */ |
|
158 ULL2(0xbebc2000,0x00000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */ |
|
159 ULL2(0xee6b2800,0x00000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */ |
|
160 ULL2(0x9502f900,0x00000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */ |
|
161 ULL2(0xba43b740,0x00000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */ |
|
162 ULL2(0xe8d4a510,0x00000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */ |
|
163 ULL2(0x9184e72a,0x00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */ |
|
164 ULL2(0xb5e620f4,0x80000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */ |
|
165 ULL2(0xe35fa931,0xa0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */ |
|
166 ULL2(0x8e1bc9bf,0x04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */ |
|
167 ULL2(0xb1a2bc2e,0xc5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */ |
|
168 ULL2(0xde0b6b3a,0x76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */ |
|
169 ULL2(0x8ac72304,0x89e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */ |
|
170 ULL2(0xad78ebc5,0xac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */ |
|
171 ULL2(0xd8d726b7,0x177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */ |
|
172 ULL2(0x87867832,0x6eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */ |
|
173 ULL2(0xa968163f,0x0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */ |
|
174 ULL2(0xd3c21bce,0xcceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */ |
|
175 ULL2(0x84595161,0x401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */ |
|
176 ULL2(0xa56fa5b9,0x9019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */ |
|
177 ULL2(0xcecb8f27,0xf4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */ |
|
178 |
|
179 ULL2(0xd0cf4b50,0xcfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */ |
|
180 ULL2(0xd2d80db0,0x2aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */ |
|
181 ULL2(0xd4e5e2cd,0xc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */ |
|
182 ULL2(0xd6f8d750,0x9292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */ |
|
183 ULL2(0xd910f7ff,0x28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */ |
|
184 ULL2(0xdb2e51bf,0xe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */ |
|
185 ULL2(0xdd50f199,0x6b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */ |
|
186 ULL2(0xdf78e4b2,0xbd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */ |
|
187 ULL2(0xe1a63853,0xbbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */ |
|
188 ULL2(0xe3d8f9e5,0x63a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */ |
|
189 |
|
190 ULL2(0xfd87b5f2,0x8300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */ |
|
191 ULL2(0xfb158592,0xbe068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */ |
|
192 ULL2(0xf8a95fcf,0x88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */ |
|
193 ULL2(0xf64335bc,0xf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */ |
|
194 ULL2(0xf3e2f893,0xdec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */ |
|
195 ULL2(0xf18899b1,0xbc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */ |
|
196 ULL2(0xef340a98,0x172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */ |
|
197 ULL2(0xece53cec,0x4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */ |
|
198 ULL2(0xea9c2277,0x23ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */ |
|
199 ULL2(0xe858ad24,0x8f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */ |
|
200 ULL2(0xe61acf03,0x3d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */ |
|
201 ULL2(0xe3e27a44,0x4d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */ |
|
202 ULL2(0xe1afa13a,0xfbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */ |
|
203 #endif |
|
204 }; |
|
205 |
|
206 static const short _Stl_twoexp[80] = { |
|
207 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, |
|
208 183,276,369,462,555,648,741,834,927,1020, |
|
209 -93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209 |
|
210 }; |
|
211 |
|
212 # define TEN_1 0 /* offset to 10 ** 1 */ |
|
213 # define TEN_27 26 /* offset to 10 ** 27 */ |
|
214 # define TEN_M28 37 /* offset to 10 ** -28 */ |
|
215 # define NUM_HI_P 11 |
|
216 # define NUM_HI_N 13 |
|
217 |
|
218 # define _Stl_HIBITULL (ULL(1) << 63) |
|
219 |
|
220 void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) |
|
221 { |
|
222 norm = 0; |
|
223 if( ! (prodhi & _Stl_HIBITULL) ) { |
|
224 /* leading bit is a zero |
|
225 * may have to normalize |
|
226 */ |
|
227 if(( prodhi == ~_Stl_HIBITULL) && |
|
228 ((prodlo >> 62) == 0x3) ) { /* normalization followed by round |
|
229 * would cause carry to create |
|
230 * extra bit, so don't normalize |
|
231 */ |
|
232 p = _Stl_HIBITULL; |
|
233 return; |
|
234 } |
|
235 p = (prodhi<<1) | (prodlo>>63); /* normalize */ |
|
236 norm=1; |
|
237 prodlo <<= 1; |
|
238 } |
|
239 else { |
|
240 p = prodhi; |
|
241 } |
|
242 |
|
243 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 |
|
244 #if !defined(__SC__) //*TY 03/25/2000 - |
|
245 if( ((p & 0x1) != 0) || |
|
246 prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */ |
|
247 #else //*TY 03/25/2000 - added workaround for SCpp compiler |
|
248 bool b1 = ((p & 0x1) != 0); |
|
249 if( b1 || prodlo != _Stl_HIBITULL ) { //*TY 03/25/2000 - SCpp confuses on this particular original boolean expression |
|
250 #endif //*TY 03/25/2000 - |
|
251 /* round */ |
|
252 p++; |
|
253 if(p==0) |
|
254 p++; |
|
255 } |
|
256 } |
|
257 |
|
258 return; |
|
259 } |
|
260 |
|
261 // Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp. |
|
262 // p: 64-bit fraction |
|
263 // exp: base-10 exponent |
|
264 // bexp: base-2 exponent (output parameter) |
|
265 |
|
266 void _Stl_tenscale(uint64& p, int exp, int& bexp) |
|
267 { |
|
268 uint64 prodhi, prodlo; /* 128b product */ |
|
269 int exp_hi, exp_lo; /* exp = exp_hi*32 + exp_lo */ |
|
270 int hi, lo, tlo, thi; /* offsets in power of ten table */ |
|
271 int norm; /* number of bits of normalization */ |
|
272 int num_hi; /* number of high exponent powers */ |
|
273 |
|
274 bexp = 0; |
|
275 if(exp > 0) { /* split exponent */ |
|
276 exp_lo = exp; |
|
277 exp_hi = 0; |
|
278 if(exp_lo>27) { |
|
279 exp_lo++; |
|
280 while(exp_lo>27) { |
|
281 exp_hi++; |
|
282 exp_lo-=28; |
|
283 } |
|
284 } |
|
285 tlo = TEN_1; |
|
286 thi = TEN_27; |
|
287 num_hi = NUM_HI_P; |
|
288 } |
|
289 else if(exp < 0) { |
|
290 exp_lo = exp; |
|
291 exp_hi = 0; |
|
292 while(exp_lo<0) { |
|
293 exp_hi++; |
|
294 exp_lo+=28; |
|
295 } |
|
296 tlo = TEN_1; |
|
297 thi = TEN_M28; |
|
298 num_hi = NUM_HI_N; |
|
299 } |
|
300 else { /* no scaling needed */ |
|
301 return; |
|
302 } |
|
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 if(exp_lo) { |
|
312 lo = tlo + exp_lo -1; |
|
313 _Stl_mult64(p, _Stl_tenpow[lo], prodhi, prodlo); |
|
314 _Stl_norm_and_round(p, norm, prodhi, prodlo); |
|
315 bexp += _Stl_twoexp[lo] - norm; |
|
316 } |
|
317 |
|
318 return; |
|
319 } |
|
320 |
|
321 // First argument is a buffer of values from 0 to 9, NOT ascii. |
|
322 // Second argument is number of digits in buffer, 1 <= digits <= 17. |
|
323 // Third argument is base-10 exponent. |
|
324 |
|
325 #if defined(__SC__) || defined(__MRC__) |
|
326 |
|
327 //*TY 04/06/2000 - powermac's 68K emulator utilizes apple's SANE floating point, which is not compatible with IEEE format. |
|
328 _STLP_END_NAMESPACE |
|
329 # include <fp.h> |
|
330 _STLP_BEGIN_NAMESPACE |
|
331 inline double _Stl_atod(char *buffer, int ndigit, int dexp) |
|
332 { |
|
333 decimal d; // ref. inside macintosh powerpc numerics p.9-13 |
|
334 |
|
335 d.sgn = 0; |
|
336 d.exp = dexp; |
|
337 d.sig.length = ndigit; |
|
338 for( int i = 0; i < ndigit; ++i ) |
|
339 { |
|
340 d.sig.text[i] = buffer[i] + '0'; |
|
341 } |
|
342 return dec2num( &d ); |
|
343 } |
|
344 |
|
345 #else /* IEEE representation */ |
|
346 |
|
347 #if 0 // def __ICL |
|
348 // turn off optimization here |
|
349 # pragma optimize "off" |
|
350 #endif |
|
351 |
|
352 double _Stl_atod(char *buffer, int ndigit, int dexp) |
|
353 { |
|
354 |
|
355 uint64 value; /* Value develops as follows: |
|
356 * 1) decimal digits as an integer |
|
357 * 2) left adjusted fraction |
|
358 * 3) right adjusted fraction |
|
359 * 4) exponent and fraction |
|
360 */ |
|
361 |
|
362 uint32 guard; /* First guard bit */ |
|
363 uint64 rest; /* Remaining guard bits */ |
|
364 |
|
365 int bexp; /* binary exponent */ |
|
366 int nzero; /* number of non-zero bits */ |
|
367 int sexp; /* scaling exponent */ |
|
368 |
|
369 char *bufferend; /* pointer to char after last digit */ |
|
370 |
|
371 /* Check for zero and treat it as a special case */ |
|
372 |
|
373 if (buffer == 0){ |
|
374 return 0.0; |
|
375 } |
|
376 |
|
377 /* Convert the decimal digits to a binary integer. */ |
|
378 |
|
379 bufferend = buffer + ndigit; |
|
380 value = 0; |
|
381 |
|
382 while( buffer < bufferend ) { |
|
383 value *= 10; |
|
384 value += *buffer++; |
|
385 } |
|
386 |
|
387 /* Check for zero and treat it as a special case */ |
|
388 |
|
389 if (value == 0){ |
|
390 return 0.0; |
|
391 } |
|
392 |
|
393 /* Normalize value */ |
|
394 |
|
395 bexp = 64; /* convert from 64b int to fraction */ |
|
396 |
|
397 /* Count number of non-zeroes in value */ |
|
398 nzero = 0; |
|
399 if ( (value >> 32) !=0 ){ nzero = 32; } //*TY 03/25/2000 - added explicit comparison to zero to avoid uint64 to bool conversion operator |
|
400 if ( (value >> (16 + nzero)) !=0 ){ nzero += 16; } |
|
401 if ( (value >> ( 8 + nzero)) !=0 ){ nzero += 8; } |
|
402 if ( (value >> ( 4 + nzero)) !=0 ){ nzero += 4; } |
|
403 if ( (value >> ( 2 + nzero)) !=0 ){ nzero += 2; } |
|
404 if ( (value >> ( 1 + nzero)) !=0 ){ nzero += 1; } |
|
405 if ( (value >> ( nzero)) !=0 ){ nzero += 1; } |
|
406 |
|
407 /* Normalize */ |
|
408 value <<= /*(uint64)*/ (64-nzero); //*TY 03/25/2000 - removed extraneous cast to uint64 |
|
409 bexp -= 64-nzero; |
|
410 |
|
411 /* At this point we have a 64b fraction and a binary exponent |
|
412 * but have yet to incorporate the decimal exponent. |
|
413 */ |
|
414 |
|
415 /* multiply by 10^dexp */ |
|
416 |
|
417 _Stl_tenscale(value, dexp, sexp); |
|
418 bexp += sexp; |
|
419 |
|
420 if (bexp <= -1022) { /* HI denorm or underflow */ |
|
421 bexp += 1022; |
|
422 if( bexp < -53 ) { /* guaranteed underflow */ |
|
423 value = 0; |
|
424 } |
|
425 else { /* denorm or possible underflow */ |
|
426 int lead0; |
|
427 |
|
428 lead0 = 12-bexp; /* 12 sign and exponent bits */ |
|
429 |
|
430 /* we must special case right shifts of more than 63 */ |
|
431 |
|
432 if ( lead0 > 64 ) |
|
433 { |
|
434 rest = value; |
|
435 guard = 0; |
|
436 value = 0; |
|
437 } |
|
438 else if ( lead0 == 64 ) |
|
439 { |
|
440 rest = value & ((ULL(1)<< 63)-1); |
|
441 #if !defined(__SC__) |
|
442 guard = (uint32) ((value>> 63) & 1 ); |
|
443 #else |
|
444 guard = to_ulong((value>> 63) & 1 ); //*TY 03/25/2000 - use member function instead of problematic conversion operator utilization |
|
445 #endif |
|
446 value = 0; |
|
447 } |
|
448 else |
|
449 { |
|
450 rest = value & (((ULL(1) << lead0)-1)-1); |
|
451 #if !defined(__SC__) |
|
452 guard = (uint32) (((value>> lead0)-1) & 1); |
|
453 #else //*TY 03/25/2000 - |
|
454 guard = to_ulong(((value>> lead0)-1) & 1); |
|
455 #endif //*TY 03/25/2000 - |
|
456 value >>= /*(uint64)*/ lead0; /* exponent is zero */ |
|
457 } |
|
458 |
|
459 /* Round */ |
|
460 if ( guard && ( (value&1) || rest) ) { |
|
461 value++; |
|
462 if( value == (ULL(1) << 52) ) { /* carry created normal number */ |
|
463 value = 0; |
|
464 _Stl_set_exponent(value, 1); |
|
465 } |
|
466 } |
|
467 } |
|
468 |
|
469 } |
|
470 else { /* not zero or denorm */ |
|
471 /* Round to 53 bits */ |
|
472 |
|
473 rest = value & (1<<10)-1; |
|
474 value >>= 10; |
|
475 #if !defined(__SC__) |
|
476 guard = (uint32) value & 1; |
|
477 #else //*TY 03/25/2000 - |
|
478 guard = to_ulong(value & 1); |
|
479 #endif |
|
480 value >>= 1; |
|
481 |
|
482 /* value&1 guard rest Action |
|
483 * |
|
484 * dc 0 dc none |
|
485 * 1 1 dc round |
|
486 * 0 1 0 none |
|
487 * 0 1 !=0 round |
|
488 */ |
|
489 if(guard) { |
|
490 if(((value&1)!=0) || (rest!=0)) { |
|
491 value++; /* round */ |
|
492 if((value>>53)!=0) { /* carry all the way across */ |
|
493 value >>= 1; /* renormalize */ |
|
494 bexp ++; |
|
495 } |
|
496 } |
|
497 } |
|
498 /* |
|
499 * Check for overflow |
|
500 * IEEE Double Precision Format |
|
501 * (From Table 7-8 of Kane and Heinrich) |
|
502 * |
|
503 * Fraction bits 52 |
|
504 * Emax +1023 |
|
505 * Emin -1022 |
|
506 * Exponent bias +1023 |
|
507 * Exponent bits 11 |
|
508 * Integer bit hidden |
|
509 * Total width in bits 64 |
|
510 */ |
|
511 |
|
512 if (bexp > 1024) { /* overflow */ |
|
513 return numeric_limits<double>::infinity(); |
|
514 } |
|
515 else { /* value is normal */ |
|
516 value &= ~(ULL(1) << 52); /* hide hidden bit */ |
|
517 _Stl_set_exponent(value, bexp + 1022); /* add bias */ |
|
518 } |
|
519 } |
|
520 |
|
521 return *((double *) &value); |
|
522 } |
|
523 |
|
524 #endif |
|
525 |
|
526 double _Stl_string_to_double(const char * s) { |
|
527 const int max_digits = 17; |
|
528 unsigned c; |
|
529 unsigned Negate, decimal_point; |
|
530 char *d; |
|
531 int exp; |
|
532 double x; |
|
533 int dpchar; |
|
534 char digits[max_digits]; |
|
535 |
|
536 // Skip leading whitespace, if any. |
|
537 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); |
|
538 while (c = *s++, ct.is(ctype_base::space, char(c))) |
|
539 ; |
|
540 |
|
541 /* process sign */ |
|
542 Negate = 0; |
|
543 if (c == '+') { |
|
544 c = *s++; |
|
545 } |
|
546 else if (c == '-') { |
|
547 Negate = 1; |
|
548 c = *s++; |
|
549 } |
|
550 d = digits; |
|
551 dpchar = '.' - '0'; |
|
552 decimal_point = 0; |
|
553 exp = 0; |
|
554 for (;;) { |
|
555 c -= '0'; |
|
556 if (c < 10) { |
|
557 if (d == digits+max_digits) { |
|
558 /* ignore more than 17 digits, but adjust exponent */ |
|
559 exp += (decimal_point ^ 1); |
|
560 } |
|
561 else { |
|
562 if (c == 0 && d == digits) { |
|
563 /* ignore leading zeros */ |
|
564 } |
|
565 else { |
|
566 *d++ = (char) c; |
|
567 } |
|
568 exp -= decimal_point; |
|
569 } |
|
570 } |
|
571 else if (c == (unsigned int) dpchar && !decimal_point) { /* INTERNATIONAL */ |
|
572 decimal_point = 1; |
|
573 } |
|
574 else { |
|
575 break; |
|
576 } |
|
577 c = *s++; |
|
578 } |
|
579 /* strtod cant return until it finds the end of the exponent */ |
|
580 if (d == digits) { |
|
581 return 0.0; |
|
582 } |
|
583 if (c == 'e'-'0' || c == 'E'-'0') { |
|
584 register unsigned negate_exp = 0; |
|
585 register int e = 0; |
|
586 c = *s++; |
|
587 if (c == '+' || c == ' ') { |
|
588 c = *s++; |
|
589 } |
|
590 else if (c == '-') { |
|
591 negate_exp = 1; |
|
592 c = *s++; |
|
593 } |
|
594 if (c -= '0', c < 10) { |
|
595 do { |
|
596 if (e <= 340) |
|
597 e = e * 10 + (int)c; |
|
598 else break; |
|
599 c = *s++; |
|
600 } |
|
601 while (c -= '0', c < 10); |
|
602 if (negate_exp) { |
|
603 e = -e; |
|
604 } |
|
605 if (e < -340 || e > 340) |
|
606 exp = e; |
|
607 else |
|
608 exp += e; |
|
609 } |
|
610 } |
|
611 |
|
612 if (exp < -340) { |
|
613 x = 0; |
|
614 } |
|
615 else if (exp > 308) { |
|
616 x = numeric_limits<double>::infinity(); |
|
617 } |
|
618 else { |
|
619 /* let _Stl_atod diagnose under- and over-flows */ |
|
620 /* if the input was == 0.0, we have already returned, |
|
621 so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW |
|
622 */ |
|
623 x = _Stl_atod (digits, (int)(d - digits), exp); |
|
624 } |
|
625 if (Negate) { |
|
626 x = -x; |
|
627 } |
|
628 return x; |
|
629 } |
|
630 |
|
631 |
|
632 #ifndef _STLP_NO_LONG_DOUBLE |
|
633 /* |
|
634 * __string_to_long_double is just lifted from atold, the difference being |
|
635 * that we just use '.' for the decimal point, rather than let it |
|
636 * be taken from the current C locale, which of course is not accessible |
|
637 * to us. |
|
638 */ |
|
639 |
|
640 long double |
|
641 _Stl_string_to_long_double(const char * s) { |
|
642 const int max_digits = 34; |
|
643 register unsigned c; |
|
644 register unsigned Negate, decimal_point; |
|
645 register char *d; |
|
646 register int exp; |
|
647 long double x; |
|
648 register int dpchar; |
|
649 char digits[max_digits]; |
|
650 |
|
651 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); |
|
652 while (c = *s++, ct.is(ctype_base::space, char(c))) |
|
653 ; |
|
654 |
|
655 /* process sign */ |
|
656 Negate = 0; |
|
657 if (c == '+') { |
|
658 c = *s++; |
|
659 } |
|
660 else if (c == '-') { |
|
661 Negate = 1; |
|
662 c = *s++; |
|
663 } |
|
664 |
|
665 d = digits; |
|
666 dpchar = '.' -'0'; |
|
667 decimal_point = 0; |
|
668 exp = 0; |
|
669 |
|
670 for (;;) { |
|
671 c -= '0'; |
|
672 if (c < 10) { |
|
673 if (d == digits+max_digits) { |
|
674 /* ignore more than 34 digits, but adjust exponent */ |
|
675 exp += (decimal_point ^ 1); |
|
676 } |
|
677 else { |
|
678 if (c == 0 && d == digits) { |
|
679 /* ignore leading zeros */ |
|
680 ; |
|
681 } |
|
682 else { |
|
683 *d++ = c; |
|
684 } |
|
685 exp -= decimal_point; |
|
686 } |
|
687 } |
|
688 else if (c == dpchar && !decimal_point) { /* INTERNATIONAL */ |
|
689 decimal_point = 1; |
|
690 } |
|
691 else { |
|
692 break; |
|
693 } |
|
694 c = *s++; |
|
695 } /* for */ |
|
696 |
|
697 if (d == digits) { |
|
698 return 0.0L; |
|
699 } |
|
700 if (c == 'e'-'0' || c == 'E'-'0') { |
|
701 register unsigned negate_exp = 0; |
|
702 register int e = 0; |
|
703 c = *s++; |
|
704 if (c == '+' || c == ' ') { |
|
705 c = *s++; |
|
706 } |
|
707 else if (c == '-') { |
|
708 negate_exp = 1; |
|
709 c = *s++; |
|
710 } |
|
711 if (c -= '0', c < 10) { |
|
712 do { |
|
713 if (e <= 340) |
|
714 e = e * 10 + c; |
|
715 else break; |
|
716 c = *s++; |
|
717 } |
|
718 while (c -= '0', c < 10); |
|
719 if (negate_exp) { |
|
720 e = -e; |
|
721 } |
|
722 if (e < -(323+max_digits) || e > 308) |
|
723 exp = e; |
|
724 else |
|
725 exp += e; |
|
726 } |
|
727 } |
|
728 |
|
729 |
|
730 if (exp < -(324+max_digits)) { |
|
731 x = 0; |
|
732 } |
|
733 else if (exp > 308) { |
|
734 x = numeric_limits<long double>::infinity(); |
|
735 } |
|
736 else { |
|
737 /* let _Stl_atod diagnose under- and over-flows */ |
|
738 /* if the input was == 0.0, we have already returned, |
|
739 so retval of +-Inf signals OVERFLOW, 0.0 UNDERFLOW |
|
740 */ |
|
741 |
|
742 // x = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1 |
|
743 double tmp = _Stl_atod (digits, (int)(d - digits), exp); // TEMPORARY!!:1 |
|
744 x = tmp == numeric_limits<double>::infinity() |
|
745 ? numeric_limits<long double>::infinity() |
|
746 : tmp; |
|
747 } |
|
748 |
|
749 if (Negate) { |
|
750 x = -x; |
|
751 } |
|
752 |
|
753 return x; |
|
754 } |
|
755 #endif |
|
756 |
|
757 _STLP_EXP_DECLSPEC int _STLP_CALL |
|
758 __string_to_float(const string& v, float& val) { |
|
759 #ifdef __SYMBIAN32__ |
|
760 char *endpt = NULL; |
|
761 unsigned c; |
|
762 const char *s = v.data(); |
|
763 // Skip leading whitespace, if any. |
|
764 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); |
|
765 while (c = *s, ct.is(ctype_base::space, char(c))) |
|
766 s++; |
|
767 int prv_error = errno; |
|
768 errno = 0; |
|
769 val = strtof(s, &endpt); |
|
770 bool __ok = (errno == 0); |
|
771 int tt = errno; |
|
772 errno= prv_error; |
|
773 return (__ok); |
|
774 #else |
|
775 val = _Stl_string_to_double(v.data()); |
|
776 return 0; |
|
777 #endif |
|
778 } |
|
779 |
|
780 _STLP_EXP_DECLSPEC int _STLP_CALL |
|
781 __string_to_float(const string& v, double& val) { |
|
782 #ifdef __SYMBIAN32__ |
|
783 char *endpt = NULL; |
|
784 unsigned c; |
|
785 const char *s = v.data(); |
|
786 // Skip leading whitespace, if any. |
|
787 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); |
|
788 while (c = *s, ct.is(ctype_base::space, char(c))) |
|
789 s++; |
|
790 int prv_error = errno; |
|
791 errno = 0; |
|
792 val = strtod(s, &endpt); |
|
793 bool __ok = (errno == 0); |
|
794 errno = prv_error; |
|
795 return (__ok); |
|
796 #else |
|
797 val = _Stl_string_to_double(v.data()); |
|
798 return 0; |
|
799 #endif |
|
800 } |
|
801 |
|
802 #ifndef _STLP_NO_LONG_DOUBLE |
|
803 _STLP_EXP_DECLSPEC int _STLP_CALL |
|
804 __string_to_float(const string& v, long double& val) { |
|
805 #ifdef __SYMBIAN32__ |
|
806 char *endpt = NULL; |
|
807 unsigned c; |
|
808 const char *s = v.data(); |
|
809 // Skip leading whitespace, if any. |
|
810 const ctype<char>& ct = use_facet<ctype<char> >(locale::classic()); |
|
811 while (c = *s, ct.is(ctype_base::space, char(c))) |
|
812 s++; |
|
813 int prv_error = errno; |
|
814 errno = 0; |
|
815 val = strtold(s, &endpt); |
|
816 bool __ok = (errno == 0); |
|
817 errno = prv_error; |
|
818 return (__ok); |
|
819 #else |
|
820 val = _Stl_string_to_long_double(v.data()); |
|
821 return 0; |
|
822 #endif |
|
823 } |
|
824 #endif |
|
825 |
|
826 _STLP_END_NAMESPACE |
|
827 |
|
828 // Local Variables: |
|
829 // mode:C++ |
|
830 // End: |