|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // strtod.cxx |
|
|
4 // |
|
|
5 // ANSI String to double-precision floating point conversion |
|
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour |
|
2
|
33 // Contributors: jlarmour |
|
0
|
34 // Date: 1998-02-13 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // Configuration header |
|
|
46 |
|
|
47 // Include the C library? And strtod()? |
|
|
48 #if defined(CYGPKG_LIBC) && defined(CYGFUN_LIBC_strtod) |
|
|
49 |
|
|
50 // INCLUDES |
|
|
51 |
|
|
52 #include <cyg/infra/cyg_type.h> // Common type definitions and support |
|
|
53 #include <cyg/infra/cyg_trac.h> // Tracing support |
|
|
54 #include <cyg/infra/cyg_ass.h> // Assertion support |
|
|
55 #include <stddef.h> // NULL, wchar_t and size_t from compiler |
|
|
56 #include <stdlib.h> // Main header for stdlib functions |
|
|
57 #include <ctype.h> // isspace() and isdigit() |
|
|
58 #include "clibincl/stdlibsupp.hxx" // Support for stdlib functions |
|
|
59 #include <float.h> // DBL_MIN_10_EXP and DBL_MAX_10_EXP |
|
|
60 #include <math.h> // HUGE_VAL |
|
|
61 #include <errno.h> // errno |
|
|
62 |
|
|
63 // EXPORTED SYMBOLS |
|
|
64 |
|
|
65 externC double |
|
|
66 strtod( const char *, char ** ) CYGPRI_LIBC_WEAK_ALIAS("_strtod"); |
|
|
67 |
|
|
68 // CONSTANTS |
|
|
69 |
|
|
70 #define MAXE (DBL_MAX_10_EXP) |
|
|
71 #define MINE (DBL_MIN_10_EXP) |
|
|
72 |
|
|
73 // flags |
|
|
74 #define SIGN 0x01 |
|
|
75 #define ESIGN 0x02 |
|
|
76 #define DECP 0x04 |
|
|
77 |
|
|
78 |
|
|
79 // MACROS |
|
|
80 |
|
|
81 #define Ise(c) ((c == 'e') || (c == 'E') || (c == 'd') || (c == 'D')) |
|
|
82 #define Issign(c) ((c == '-') || (c == '+')) |
|
|
83 #define Val(c) ((c - '0')) |
|
|
84 |
|
|
85 |
|
|
86 // FUNCTIONS |
|
|
87 |
|
|
88 /* |
|
|
89 * [atw] multiply 64 bit accumulator by 10 and add digit. |
|
|
90 * The KA/CA way to do this should be to use |
|
|
91 * a 64-bit integer internally and use "adjust" to |
|
|
92 * convert it to float at the end of processing. |
|
|
93 */ |
|
|
94 static int |
|
|
95 ten_mul(double *acc, int digit) |
|
|
96 { |
|
|
97 /* [atw] Crude, but effective (at least on a KB)... |
|
|
98 */ |
|
|
99 *acc *= 10; |
|
|
100 *acc += digit; |
|
|
101 |
|
|
102 return 0; /* no overflow */ |
|
|
103 } // ten_mul() |
|
|
104 |
|
|
105 |
|
|
106 /* |
|
|
107 * compute 10**x by successive squaring. |
|
|
108 */ |
|
|
109 |
|
|
110 static const double |
|
|
111 exp10(unsigned x) |
|
|
112 { |
|
|
113 static double powtab[] = {1.0, |
|
|
114 10.0, |
|
|
115 100.0, |
|
|
116 1000.0, |
|
|
117 10000.0}; |
|
|
118 |
|
|
119 if (x < (sizeof(powtab)/sizeof(double))) |
|
|
120 return powtab[x]; |
|
|
121 else if (x & 1) |
|
|
122 return 10.0 * exp10(x-1); |
|
|
123 else |
|
|
124 return exp10(x/2) * exp10(x/2); |
|
|
125 } // exp10() |
|
|
126 |
|
|
127 |
|
|
128 /* |
|
|
129 * return (*acc) scaled by 10**dexp. |
|
|
130 */ |
|
|
131 |
|
|
132 static double |
|
|
133 adjust(double *acc, int dexp, int sign) |
|
|
134 /* *acc the 64 bit accumulator */ |
|
|
135 /* dexp decimal exponent */ |
|
|
136 /* sign sign flag */ |
|
|
137 { |
|
|
138 double r; |
|
|
139 |
|
|
140 if (dexp > MAXE) |
|
|
141 { |
|
|
142 errno = ERANGE; |
|
|
143 return (sign) ? -HUGE_VAL : HUGE_VAL; |
|
|
144 } |
|
|
145 else if (dexp < MINE) |
|
|
146 { |
|
|
147 errno = ERANGE; |
|
|
148 return 0.0; |
|
|
149 } |
|
|
150 |
|
|
151 r = *acc; |
|
|
152 if (sign) |
|
|
153 r = -r; |
|
|
154 if (dexp==0) |
|
|
155 return r; |
|
|
156 |
|
|
157 if (dexp < 0) |
|
|
158 return r / exp10(abs(dexp)); |
|
|
159 else |
|
|
160 return r * exp10(dexp); |
|
|
161 } // adjust() |
|
|
162 |
|
|
163 |
|
|
164 externC double |
|
|
165 _strtod( const char *nptr, char **endptr ) |
|
|
166 { |
|
|
167 const char *start=nptr; |
|
|
168 double accum = 0.0; |
|
|
169 int flags = 0; |
|
|
170 int texp = 0; |
|
|
171 int e = 0; |
|
|
172 int conv_done = 0; |
|
|
173 |
|
|
174 double retval; |
|
|
175 |
|
|
176 CYG_REPORT_FUNCNAMETYPE( "_strtod", "returning %f" ); |
|
|
177 |
|
|
178 CYG_CHECK_DATA_PTR( nptr, "nptr is an invalid pointer!" ); |
|
|
179 |
|
|
180 // endptr is allowed to be NULL, but if it isn't, we check it |
|
|
181 if (endptr != NULL) |
|
|
182 CYG_CHECK_DATA_PTR( endptr, "endptr is an invalid pointer!" ); |
|
|
183 |
|
|
184 while(isspace(*nptr)) nptr++; |
|
|
185 if(*nptr == '\0') |
|
|
186 { /* just leading spaces */ |
|
|
187 if(endptr != NULL) *endptr = (char *)start; |
|
|
188 return 0.0; |
|
|
189 } |
|
|
190 |
|
|
191 |
|
|
192 if(Issign(*nptr)) |
|
|
193 { |
|
|
194 if(*nptr == '-') flags = SIGN; |
|
|
195 if(*++nptr == '\0') |
|
|
196 { /* "+|-" : should be an error ? */ |
|
|
197 if(endptr != NULL) *endptr = (char *)start; |
|
|
198 return 0.0; |
|
|
199 } |
|
|
200 } |
|
|
201 |
|
|
202 for(; (isdigit(*nptr) || (*nptr == '.')); nptr++) |
|
|
203 { |
|
|
204 conv_done = 1; |
|
|
205 if(*nptr == '.') |
|
|
206 flags |= DECP; |
|
|
207 else |
|
|
208 { |
|
|
209 if( ten_mul(&accum, Val(*nptr)) ) texp++; |
|
|
210 if(flags & DECP) texp--; |
|
|
211 } |
|
|
212 } |
|
|
213 |
|
|
214 if(Ise(*nptr)) |
|
|
215 { |
|
|
216 conv_done = 1; |
|
|
217 if(*++nptr != '\0') /* skip e|E|d|D */ |
|
|
218 { /* ! ([nptr]xxx[.[yyy]]e) */ |
|
|
219 |
|
|
220 while(isspace(*nptr)) nptr++; /* Ansi allows spaces after e */ |
|
|
221 if(*nptr != '\0') |
|
|
222 { /* ! ([nptr]xxx[.[yyy]]e[space]) */ |
|
|
223 |
|
|
224 if(Issign(*nptr)) |
|
|
225 if(*nptr++ == '-') flags |= ESIGN; |
|
|
226 |
|
|
227 if(*nptr != '\0') |
|
|
228 { /* ! ([nptr]xxx[.[yyy]]e[nptr]) -- error?? */ |
|
|
229 |
|
|
230 for(; isdigit(*nptr); nptr++) |
|
|
231 if (e < MAXE) /* prevent from grossly overflowing */ |
|
|
232 e = e*10 + Val(*nptr); |
|
|
233 |
|
|
234 /* dont care what comes after this */ |
|
|
235 if(flags & ESIGN) |
|
|
236 texp -= e; |
|
|
237 else |
|
|
238 texp += e; |
|
|
239 } |
|
|
240 } |
|
|
241 } |
|
|
242 } |
|
|
243 |
|
|
244 if(endptr != NULL) |
|
|
245 *endptr = (char *)((conv_done) ? nptr : start); |
|
|
246 |
|
|
247 retval = adjust(&accum, (int)texp, (int)(flags & SIGN)); |
|
|
248 |
|
|
249 |
|
|
250 CYG_REPORT_RETVAL( retval ); |
|
|
251 |
|
|
252 return retval; |
|
|
253 } // _strtod() |
|
|
254 |
|
|
255 |
|
|
256 #endif // if defined(CYGPKG_LIBC) && defined(CYGFUN_LIBC_strtod) |
|
|
257 |
|
|
258 // EOF strtod.cxx |