1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
//===========================================================================
//
// strtod.cxx
//
// ISO String to double-precision floating point conversion
//
//===========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later
// version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eCos; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, if other files instantiate templates or use
// macros or inline functions from this file, or you compile this file
// and link it with other works to produce a work based on this file,
// this file does not by itself cause the resulting work to be covered by
// the GNU General Public License. However the source code for this file
// must still be made available in accordance with section (3) of the GNU
// General Public License v2.
//
// This exception does not invalidate any other reasons why a work based
// on this file might be covered by the GNU General Public License.
// -------------------------------------------
// ####ECOSGPLCOPYRIGHTEND####
//===========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): jlarmour
// Contributors:
// Date: 2000-04-30
// Purpose:
// Description:
// Usage:
//
//####DESCRIPTIONEND####
//
//===========================================================================
// CONFIGURATION
#include <pkgconf/libc_stdlib.h> // Configuration header
// Include strtod()?
#if defined(CYGFUN_LIBC_strtod)
// INCLUDES
#include <cyg/infra/cyg_type.h> // Common type definitions and support
#include <cyg/infra/cyg_trac.h> // Tracing support
#include <cyg/infra/cyg_ass.h> // Assertion support
#include <stddef.h> // NULL, wchar_t and size_t from compiler
#include <stdlib.h> // Main header for stdlib functions
#include <ctype.h> // isspace() and isdigit()
#include <float.h> // DBL_MIN_10_EXP and DBL_MAX_10_EXP
#include <math.h> // HUGE_VAL
#include <errno.h> // errno
// CONSTANTS
#define MAXE (DBL_MAX_10_EXP)
#define MINE (DBL_MIN_10_EXP)
// flags
#define SIGN 0x01
#define ESIGN 0x02
#define DECP 0x04
// MACROS
#define Ise(c) ((c == 'e') || (c == 'E'))
#define Issign(c) ((c == '-') || (c == '+'))
#define Val(c) ((c - '0'))
// FUNCTIONS
/*
* [atw] multiply 64 bit accumulator by 10 and add digit.
* The KA/CA way to do this should be to use
* a 64-bit integer internally and use "adjust" to
* convert it to float at the end of processing.
*/
static int
ten_mul(double *acc, int digit)
{
/* [atw] Crude, but effective (at least on a KB)...
*/
*acc *= 10;
*acc += digit;
return 0; /* no overflow */
} // ten_mul()
/*
* compute 10**x by successive squaring.
*/
static const double
exp10(unsigned x)
{
static double powtab[] = {1.0,
10.0,
100.0,
1000.0,
10000.0};
if (x < (sizeof(powtab)/sizeof(double)))
return powtab[x];
else if (x & 1)
return 10.0 * exp10(x-1);
else
return exp10(x/2) * exp10(x/2);
} // exp10()
/*
* return (*acc) scaled by 10**dexp.
*/
static double
adjust(double *acc, int dexp, int sign)
/* *acc the 64 bit accumulator */
/* dexp decimal exponent */
/* sign sign flag */
{
double r;
if (dexp > MAXE)
{
errno = ERANGE;
return (sign) ? -HUGE_VAL : HUGE_VAL;
}
else if (dexp < MINE)
{
errno = ERANGE;
return 0.0;
}
r = *acc;
if (sign)
r = -r;
if (dexp==0)
return r;
if (dexp < 0)
return r / exp10(abs(dexp));
else
return r * exp10(dexp);
} // adjust()
externC double
strtod( const char *nptr, char **endptr )
{
const char *start=nptr;
double accum = 0.0;
int flags = 0;
int texp = 0;
int e = 0;
int conv_done = 0;
double retval;
CYG_REPORT_FUNCNAMETYPE( "strtod", "returning %f" );
CYG_CHECK_DATA_PTR( nptr, "nptr is an invalid pointer!" );
// endptr is allowed to be NULL, but if it isn't, we check it
if (endptr != NULL)
CYG_CHECK_DATA_PTR( endptr, "endptr is an invalid pointer!" );
while(isspace(*nptr)) nptr++;
if(*nptr == '\0')
{ /* just leading spaces */
if(endptr != NULL) *endptr = (char *)start;
return 0.0;
}
if(Issign(*nptr))
{
if(*nptr == '-') flags = SIGN;
if(*++nptr == '\0')
{ /* "+|-" : should be an error ? */
if(endptr != NULL) *endptr = (char *)start;
return 0.0;
}
}
for(; (isdigit(*nptr) || (*nptr == '.')); nptr++)
{
conv_done = 1;
if(*nptr == '.')
flags |= DECP;
else
{
if( ten_mul(&accum, Val(*nptr)) ) texp++;
if(flags & DECP) texp--;
}
}
if(Ise(*nptr))
{
conv_done = 1;
if(*++nptr != '\0') /* skip e|E */
{ /* ! ([nptr]xxx[.[yyy]]e) */
while(isspace(*nptr)) nptr++; /* Ansi allows spaces after e */
if(*nptr != '\0')
{ /* ! ([nptr]xxx[.[yyy]]e[space]) */
if(Issign(*nptr))
if(*nptr++ == '-') flags |= ESIGN;
if(*nptr != '\0')
{ /* ! ([nptr]xxx[.[yyy]]e[nptr]) -- error?? */
for(; isdigit(*nptr); nptr++)
if (e < MAXE) /* prevent from grossly overflowing */
e = e*10 + Val(*nptr);
/* dont care what comes after this */
if(flags & ESIGN)
texp -= e;
else
texp += e;
}
}
}
}
if(endptr != NULL)
*endptr = (char *)((conv_done) ? nptr : start);
retval = adjust(&accum, (int)texp, (int)(flags & SIGN));
CYG_REPORT_RETVAL( retval );
return retval;
} // strtod()
#endif // if defined(CYGFUN_LIBC_strtod)
// EOF strtod.cxx
|