2 * Copyright (C) 2000-2005 Manuel Novoa III
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
9 * The primary objective of this implementation was minimal size and
10 * portablility, while providing robustness and resonable accuracy.
12 * This implementation depends on IEEE floating point behavior and expects
13 * to be able to generate +/- infinity as a result.
15 * There are a number of compile-time options below.
20 * General cleanup and some minor size optimizations.
21 * Change implementation to support __strtofpmax() rather than strtod().
22 * Now all the strto{floating pt}() funcs are implemented in terms of
23 * of the internal __strtofpmax() function.
24 * Support "nan", "inf", and "infinity" strings (case-insensitive).
25 * Support hexadecimal floating point notation.
26 * Support wchar variants.
27 * Support xlocale variants.
31 * Consider accumulating blocks of digits in longs to save floating pt mults.
32 * This would likely be much better on anything that only supported floats
33 * where DECIMAL_DIG == 9. Actually, if floats have FLT_MAX_10_EXP == 38,
34 * we could calculate almost all the exponent multipliers (p_base) in
35 * long arithmetic as well.
38 /**********************************************************************/
40 /**********************************************************************/
42 /* Defined if we want to recognize "nan", "inf", and "infinity". (C99) */
43 #define _STRTOD_NAN_INF_STRINGS 1
45 /* Defined if we want support hexadecimal floating point notation. (C99) */
46 /* Note! Now controlled by uClibc configuration. See below. */
47 #define _STRTOD_HEXADECIMAL_FLOATS 1
49 /* Defined if we want to scale with a O(log2(exp)) multiplications.
50 * This is generally a good thing to do unless you are really tight
51 * on space and do not expect to convert values of large magnitude. */
53 #define _STRTOD_LOG_SCALING 1
55 /* WARNING!!! WARNING!!! WARNING!!! WARNING!!! WARNING!!!
57 * Clearing any of the options below this point is not advised (or tested).
59 * WARNING!!! WARNING!!! WARNING!!! WARNING!!! WARNING!!! */
61 /* Defined if we want strtod to set errno appropriately. */
62 /* NOTE: Implies all options below. */
63 #define _STRTOD_ERRNO 1
65 /* Defined if we want support for the endptr arg. */
66 /* Implied by _STRTOD_ERRNO. */
67 #define _STRTOD_ENDPTR 1
69 /* Defined if we want to prevent overflow in accumulating the exponent. */
70 /* Implied by _STRTOD_ERRNO. */
71 #define _STRTOD_RESTRICT_EXP 1
73 /* Defined if we want to process mantissa digits more intelligently. */
74 /* Implied by _STRTOD_ERRNO. */
75 #define _STRTOD_RESTRICT_DIGITS 1
77 /* Defined if we want to skip scaling 0 for the exponent. */
78 /* Implied by _STRTOD_ERRNO. */
79 #define _STRTOD_ZERO_CHECK 1
81 /**********************************************************************/
82 /* Don't change anything that follows. */
83 /**********************************************************************/
87 #undef _STRTOD_RESTRICT_EXP
88 #undef _STRTOD_RESTRICT_DIGITS
89 #undef _STRTOD_ZERO_CHECK
90 #define _STRTOD_ENDPTR 1
91 #define _STRTOD_RESTRICT_EXP 1
92 #define _STRTOD_RESTRICT_DIGITS 1
93 #define _STRTOD_ZERO_CHECK 1
96 /**********************************************************************/
98 #define _ISOC99_SOURCE 1
106 #include <bits/uClibc_fpmax.h>
110 #ifdef __UCLIBC_HAS_WCHAR__
114 #include <bits/uClibc_uwchar.h>
118 #ifdef __UCLIBC_HAS_XLOCALE__
120 #endif /* __UCLIBC_HAS_XLOCALE__ */
124 /* Handle _STRTOD_HEXADECIMAL_FLOATS via uClibc config now. */
125 #undef _STRTOD_HEXADECIMAL_FLOATS
126 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
127 #define _STRTOD_HEXADECIMAL_FLOATS 1
128 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
130 /**********************************************************************/
136 #define NEED_STRTOLD_WRAPPER
137 #define NEED_STRTOD_WRAPPER
138 #define NEED_STRTOF_WRAPPER
140 #elif FPMAX_TYPE == 2
142 #define NEED_STRTOD_WRAPPER
143 #define NEED_STRTOF_WRAPPER
145 #elif FPMAX_TYPE == 1
147 #define NEED_STRTOF_WRAPPER
151 #error unknown FPMAX_TYPE!
155 extern void __fp_range_check(__fpmax_t y, __fpmax_t x);
157 /**********************************************************************/
159 #ifdef _STRTOD_RESTRICT_DIGITS
160 #define EXP_DENORM_ADJUST DECIMAL_DIG
161 #define MAX_ALLOWED_EXP (DECIMAL_DIG + EXP_DENORM_ADJUST - FPMAX_MIN_10_EXP)
163 #if MAX_ALLOWED_EXP > INT_MAX
164 #error size assumption violated for MAX_ALLOWED_EXP
167 /* We want some excess if we're not restricting mantissa digits. */
168 #define MAX_ALLOWED_EXP ((20 - FPMAX_MIN_10_EXP) * 2)
172 #if defined(_STRTOD_RESTRICT_DIGITS) || defined(_STRTOD_ENDPTR) || defined(_STRTOD_HEXADECIMAL_FLOATS)
173 #undef _STRTOD_NEED_NUM_DIGITS
174 #define _STRTOD_NEED_NUM_DIGITS 1
177 /**********************************************************************/
178 #if defined(L___strtofpmax) || defined(L___strtofpmax_l) || defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
180 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
182 #define __strtofpmax __wcstofpmax
183 #define __strtofpmax_l __wcstofpmax_l
185 #define Wchar wchar_t
186 #ifdef __UCLIBC_DO_XLOCALE
187 #define ISSPACE(C) iswspace_l((C), locale_arg)
189 #define ISSPACE(C) iswspace((C))
192 #else /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
195 #ifdef __UCLIBC_DO_XLOCALE
196 #define ISSPACE(C) isspace_l((C), locale_arg)
198 #define ISSPACE(C) isspace((C))
201 #endif /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
204 #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
206 __fpmax_t __strtofpmax(const Wchar *str, Wchar **endptr, int exponent_power)
208 return __strtofpmax_l(str, endptr, exponent_power, __UCLIBC_CURLOCALE);
211 #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
213 __fpmax_t __XL_NPP(__strtofpmax)(const Wchar *str, Wchar **endptr, int exponent_power
217 __fpmax_t p_base = 10; /* Adjusted to 16 in the hex case. */
219 #ifdef _STRTOD_ENDPTR
222 Wchar *pos = (Wchar *) str;
224 int negative; /* A flag for the number, a multiplier for the exponent. */
225 #ifdef _STRTOD_NEED_NUM_DIGITS
228 #ifdef __UCLIBC_HAS_LOCALE__
229 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
230 wchar_t decpt_wc = __LOCALE_PTR->decimal_point;
232 const char *decpt = __LOCALE_PTR->decimal_point;
233 int decpt_len = __LOCALE_PTR->decimal_point_len;
237 #ifdef _STRTOD_HEXADECIMAL_FLOATS
239 Wchar *poshex = NULL;
240 __uint16_t is_mask = _ISdigit;
241 #define EXPCHAR expchar
242 #define IS_X_DIGIT(C) __isctype((C), is_mask)
243 #else /* _STRTOD_HEXADECIMAL_FLOATS */
245 #define IS_X_DIGIT(C) isdigit((C))
246 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
248 while (ISSPACE(*pos)) { /* Skip leading whitespace. */
253 switch(*pos) { /* Handle optional sign. */
254 case '-': negative = 1; /* Fall through to increment position. */
258 #ifdef _STRTOD_HEXADECIMAL_FLOATS
259 if ((*pos == '0') && (((pos[1])|0x20) == 'x')) {
260 poshex = ++pos; /* Save position of 'x' in case no digits */
261 ++pos; /* and advance past it. */
262 is_mask = _ISxdigit; /* Used by IS_X_DIGIT. */
263 expchar = 'p'; /* Adjust exponent char. */
264 p_base = 16; /* Adjust base multiplier. */
269 #ifdef _STRTOD_NEED_NUM_DIGITS
272 /* exponent_power = 0; */
276 while (IS_X_DIGIT(*pos)) { /* Process string of (hex) digits. */
277 #ifdef _STRTOD_RESTRICT_DIGITS
278 if (num_digits < 0) { /* First time through? */
279 ++num_digits; /* We've now seen a digit. */
281 if (num_digits || (*pos != '0')) { /* Had/have nonzero. */
283 if (num_digits <= DECIMAL_DIG) { /* Is digit significant? */
284 #ifdef _STRTOD_HEXADECIMAL_FLOATS
285 number = number * p_base
288 : (((*pos)|0x20) - ('a' - 10)));
289 #else /* _STRTOD_HEXADECIMAL_FLOATS */
290 number = number * p_base + (*pos - '0');
291 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
294 #else /* _STRTOD_RESTRICT_DIGITS */
295 #ifdef _STRTOD_NEED_NUM_DIGITS
298 #ifdef _STRTOD_HEXADECIMAL_FLOATS
299 number = number * p_base
302 : (((*pos)|0x20) - ('a' - 10)));
303 #else /* _STRTOD_HEXADECIMAL_FLOATS */
304 number = number * p_base + (*pos - '0');
305 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
306 #endif /* _STRTOD_RESTRICT_DIGITS */
310 #ifdef __UCLIBC_HAS_LOCALE__
311 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
312 if (!pos0 && (*pos == decpt_wc)) { /* First decimal point? */
317 if (!pos0 && !memcmp(pos, decpt, decpt_len)) { /* First decimal point? */
318 pos0 = (pos += decpt_len);
322 #else /* __UCLIBC_HAS_LOCALE__ */
323 if ((*pos == '.') && !pos0) { /* First decimal point? */
324 pos0 = ++pos; /* Save position of decimal point */
325 goto LOOP; /* and process rest of digits. */
327 #endif /* __UCLIBC_HAS_LOCALE__ */
329 #ifdef _STRTOD_NEED_NUM_DIGITS
330 if (num_digits<0) { /* Must have at least one digit. */
331 #ifdef _STRTOD_HEXADECIMAL_FLOATS
332 if (poshex) { /* Back up to '0' in '0x' prefix. */
336 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
338 #ifdef _STRTOD_NAN_INF_STRINGS
339 if (!pos0) { /* No decimal point, so check for inf/nan. */
340 /* Note: nan is the first string so 'number = i/0.;' works. */
341 static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0";
344 #ifdef __UCLIBC_HAS_LOCALE__
345 /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
347 #define _tolower(C) ((C)|0x20)
348 #endif /* __UCLIBC_HAS_LOCALE__ */
351 /* Unfortunately, we have no memcasecmp(). */
353 while (_tolower(pos[j]) == nan_inf_str[i+1+j]) {
355 if (!nan_inf_str[i+1+j]) {
357 if (negative) { /* Correct for sign. */
360 pos += nan_inf_str[i] - 2;
365 } while (nan_inf_str[i]);
368 #endif /* STRTOD_NAN_INF_STRINGS */
369 #ifdef _STRTOD_ENDPTR
374 #endif /* _STRTOD_NEED_NUM_DIGITS */
376 #ifdef _STRTOD_RESTRICT_DIGITS
377 if (num_digits > DECIMAL_DIG) { /* Adjust exponent for skipped digits. */
378 exponent_power += num_digits - DECIMAL_DIG;
383 exponent_power += pos0 - pos; /* Adjust exponent for decimal point. */
386 #ifdef _STRTOD_HEXADECIMAL_FLOATS
388 exponent_power *= 4; /* Above is 2**4, but below is 2. */
391 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
393 if (negative) { /* Correct for sign. */
397 /* process an exponent string */
398 if (((*pos)|0x20) == EXPCHAR) {
399 #ifdef _STRTOD_ENDPTR
403 switch(*++pos) { /* Handle optional sign. */
404 case '-': negative = -1; /* Fall through to increment pos. */
410 while (isdigit(*pos)) { /* Process string of digits. */
411 #ifdef _STRTOD_RESTRICT_EXP
412 if (exponent_temp < MAX_ALLOWED_EXP) { /* Avoid overflow. */
413 exponent_temp = exponent_temp * 10 + (*pos - '0');
416 exponent_temp = exponent_temp * 10 + (*pos - '0');
421 #ifdef _STRTOD_ENDPTR
422 if (pos == pos0) { /* No digits? */
423 pos = pos1; /* Back up to {e|E}/{p|P}. */
427 exponent_power += negative * exponent_temp;
430 #ifdef _STRTOD_ZERO_CHECK
436 /* scale the result */
437 #ifdef _STRTOD_LOG_SCALING
438 exponent_temp = exponent_power;
440 if (exponent_temp < 0) {
441 exponent_temp = -exponent_temp;
444 while (exponent_temp) {
445 if (exponent_temp & 1) {
446 if (exponent_power < 0) {
447 /* Warning... caluclating a factor for the exponent and
448 * then dividing could easily be faster. But doing so
449 * might cause problems when dealing with denormals. */
459 #else /* _STRTOD_LOG_SCALING */
460 while (exponent_power) {
461 if (exponent_power < 0) {
469 #endif /* _STRTOD_LOG_SCALING */
472 if (__FPMAX_ZERO_OR_INF_CHECK(number)) {
478 #ifdef _STRTOD_ENDPTR
487 #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
490 /**********************************************************************/
491 #ifdef L___fp_range_check
492 #if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)
494 extern void __fp_range_check(__fpmax_t y, __fpmax_t x)
496 if (__FPMAX_ZERO_OR_INF_CHECK(y) /* y is 0 or +/- infinity */
497 && (y != 0) /* y is not 0 (could have x>0, y==0 if underflow) */
498 && !__FPMAX_ZERO_OR_INF_CHECK(x) /* x is not 0 or +/- infinity */
500 __set_errno(ERANGE); /* Then x is not in y's range. */
506 /**********************************************************************/
507 #if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)
508 #if defined(NEED_STRTOF_WRAPPER)
510 #if defined(L_wcstof) || defined(L_wcstof_l)
511 #define strtof wcstof
512 #define strtof_l wcstof_l
513 #define __strtof __wcstof
514 #define __strtof_l __wcstof_l
515 #define __strtofpmax __wcstofpmax
516 #define __strtofpmax_l __wcstofpmax_l
517 #define Wchar wchar_t
523 float __XL(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
526 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
531 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
534 __fp_range_check(y, x);
544 /**********************************************************************/
545 #if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)
546 #if defined(NEED_STRTOD_WRAPPER)
548 #if defined(L_wcstod) || defined(L_wcstod_l)
549 #define strtod wcstod
550 #define strtod_l wcstod_l
551 #define __strtod __wcstod
552 #define __strtod_l __wcstod_l
553 #define __strtofpmax __wcstofpmax
554 #define __strtofpmax_l __wcstofpmax_l
555 #define Wchar wchar_t
560 double __XL(strtod)(const Wchar *__restrict str,
561 Wchar **__restrict endptr __LOCALE_PARAM )
564 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
569 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
572 __fp_range_check(y, x);
582 /**********************************************************************/
583 #if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)
584 #if defined(NEED_STRTOLD_WRAPPER)
586 #if defined(L_wcstold) || defined(L_wcstold_l)
587 #define strtold wcstold
588 #define strtold_l wcstold_l
589 #define __strtold __wcstold
590 #define __strtold_l __wcstold_l
591 #define __strtofpmax __wcstofpmax
592 #define __strtofpmax_l __wcstofpmax_l
593 #define Wchar wchar_t
598 long double __XL(strtold)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
601 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
606 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
609 __fp_range_check(y, x);
619 /**********************************************************************/