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
105 #include <bits/uClibc_fpmax.h>
109 #ifdef __UCLIBC_HAS_WCHAR__
112 # include <bits/uClibc_uwchar.h>
115 #ifdef __UCLIBC_HAS_XLOCALE__
116 # include <xlocale.h>
119 /* Handle _STRTOD_HEXADECIMAL_FLOATS via uClibc config now. */
120 #undef _STRTOD_HEXADECIMAL_FLOATS
121 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
122 # define _STRTOD_HEXADECIMAL_FLOATS 1
125 /**********************************************************************/
131 #define NEED_STRTOLD_WRAPPER
132 #define NEED_STRTOD_WRAPPER
133 #define NEED_STRTOF_WRAPPER
135 #elif FPMAX_TYPE == 2
137 #define NEED_STRTOD_WRAPPER
138 #define NEED_STRTOF_WRAPPER
140 #elif FPMAX_TYPE == 1
142 #define NEED_STRTOF_WRAPPER
146 #error unknown FPMAX_TYPE!
150 extern void __fp_range_check(__fpmax_t y, __fpmax_t x) attribute_hidden;
152 /**********************************************************************/
154 #ifdef _STRTOD_RESTRICT_DIGITS
155 #define EXP_DENORM_ADJUST DECIMAL_DIG
156 #define MAX_ALLOWED_EXP (DECIMAL_DIG + EXP_DENORM_ADJUST - FPMAX_MIN_10_EXP)
158 #if MAX_ALLOWED_EXP > INT_MAX
159 #error size assumption violated for MAX_ALLOWED_EXP
162 /* We want some excess if we're not restricting mantissa digits. */
163 #define MAX_ALLOWED_EXP ((20 - FPMAX_MIN_10_EXP) * 2)
167 #if defined(_STRTOD_RESTRICT_DIGITS) || defined(_STRTOD_ENDPTR) || defined(_STRTOD_HEXADECIMAL_FLOATS)
168 #undef _STRTOD_NEED_NUM_DIGITS
169 #define _STRTOD_NEED_NUM_DIGITS 1
172 /**********************************************************************/
173 #if defined(L___strtofpmax) || defined(L___strtofpmax_l) || defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
175 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
177 #define __strtofpmax __wcstofpmax
178 #define __strtofpmax_l __wcstofpmax_l
180 #define Wchar wchar_t
181 #ifdef __UCLIBC_DO_XLOCALE
182 #define ISSPACE(C) iswspace_l((C), locale_arg)
184 #define ISSPACE(C) iswspace((C))
187 #else /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
190 #ifdef __UCLIBC_DO_XLOCALE
191 #define ISSPACE(C) isspace_l((C), locale_arg)
193 #define ISSPACE(C) isspace((C))
196 #endif /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
199 #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
201 __fpmax_t attribute_hidden __strtofpmax(const Wchar *str, Wchar **endptr, int exponent_power)
203 return __strtofpmax_l(str, endptr, exponent_power, __UCLIBC_CURLOCALE);
206 #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
209 __fpmax_t attribute_hidden __XL_NPP(__strtofpmax)(const Wchar *str, Wchar **endptr, int exponent_power
213 __fpmax_t p_base = 10; /* Adjusted to 16 in the hex case. */
215 #ifdef _STRTOD_ENDPTR
218 Wchar *pos = (Wchar *) str;
220 int negative; /* A flag for the number, a multiplier for the exponent. */
221 #ifdef _STRTOD_NEED_NUM_DIGITS
224 #ifdef __UCLIBC_HAS_LOCALE__
225 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
226 wchar_t decpt_wc = __LOCALE_PTR->decimal_point_wc;
228 const char *decpt = __LOCALE_PTR->decimal_point;
229 int decpt_len = __LOCALE_PTR->decimal_point_len;
233 #ifdef _STRTOD_HEXADECIMAL_FLOATS
235 Wchar *poshex = NULL;
236 __uint16_t is_mask = _ISdigit;
237 #define EXPCHAR expchar
238 #define IS_X_DIGIT(C) __isctype((C), is_mask)
239 #else /* _STRTOD_HEXADECIMAL_FLOATS */
241 #define IS_X_DIGIT(C) isdigit((C))
242 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
244 while (ISSPACE(*pos)) { /* Skip leading whitespace. */
249 switch(*pos) { /* Handle optional sign. */
250 case '-': negative = 1; /* Fall through to increment position. */
254 #ifdef _STRTOD_HEXADECIMAL_FLOATS
255 if ((*pos == '0') && (((pos[1])|0x20) == 'x')) {
256 poshex = ++pos; /* Save position of 'x' in case no digits */
257 ++pos; /* and advance past it. */
258 is_mask = _ISxdigit; /* Used by IS_X_DIGIT. */
259 expchar = 'p'; /* Adjust exponent char. */
260 p_base = 16; /* Adjust base multiplier. */
265 #ifdef _STRTOD_NEED_NUM_DIGITS
268 /* exponent_power = 0; */
272 while (IS_X_DIGIT(*pos)) { /* Process string of (hex) digits. */
273 #ifdef _STRTOD_RESTRICT_DIGITS
274 if (num_digits < 0) { /* First time through? */
275 ++num_digits; /* We've now seen a digit. */
277 if (num_digits || (*pos != '0')) { /* Had/have nonzero. */
279 if (num_digits <= DECIMAL_DIG) { /* Is digit significant? */
280 #ifdef _STRTOD_HEXADECIMAL_FLOATS
281 number = number * p_base
284 : (((*pos)|0x20) - ('a' - 10)));
285 #else /* _STRTOD_HEXADECIMAL_FLOATS */
286 number = number * p_base + (*pos - '0');
287 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
290 #else /* _STRTOD_RESTRICT_DIGITS */
291 #ifdef _STRTOD_NEED_NUM_DIGITS
294 #ifdef _STRTOD_HEXADECIMAL_FLOATS
295 number = number * p_base
298 : (((*pos)|0x20) - ('a' - 10)));
299 #else /* _STRTOD_HEXADECIMAL_FLOATS */
300 number = number * p_base + (*pos - '0');
301 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
302 #endif /* _STRTOD_RESTRICT_DIGITS */
306 #ifdef __UCLIBC_HAS_LOCALE__
307 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
308 if (!pos0 && (*pos == decpt_wc)) { /* First decimal point? */
313 if (!pos0 && !memcmp(pos, decpt, decpt_len)) { /* First decimal point? */
314 pos0 = (pos += decpt_len);
318 #else /* __UCLIBC_HAS_LOCALE__ */
319 if ((*pos == '.') && !pos0) { /* First decimal point? */
320 pos0 = ++pos; /* Save position of decimal point */
321 goto LOOP; /* and process rest of digits. */
323 #endif /* __UCLIBC_HAS_LOCALE__ */
325 #ifdef _STRTOD_NEED_NUM_DIGITS
326 if (num_digits<0) { /* Must have at least one digit. */
327 #ifdef _STRTOD_HEXADECIMAL_FLOATS
328 if (poshex) { /* Back up to '0' in '0x' prefix. */
332 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
334 #ifdef _STRTOD_NAN_INF_STRINGS
335 if (!pos0) { /* No decimal point, so check for inf/nan. */
336 /* Note: nan is the first string so 'number = i/0.;' works. */
337 static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0";
341 /* Unfortunately, we have no memcasecmp(). */
343 /* | 0x20 is a cheap lowercasing (valid for ASCII letters and numbers only) */
344 while ((pos[j] | 0x20) == nan_inf_str[i+1+j]) {
346 if (!nan_inf_str[i+1+j]) {
348 if (negative) { /* Correct for sign. */
351 pos += nan_inf_str[i] - 2;
356 } while (nan_inf_str[i]);
359 #endif /* STRTOD_NAN_INF_STRINGS */
360 #ifdef _STRTOD_ENDPTR
365 #endif /* _STRTOD_NEED_NUM_DIGITS */
367 #ifdef _STRTOD_RESTRICT_DIGITS
368 if (num_digits > DECIMAL_DIG) { /* Adjust exponent for skipped digits. */
369 exponent_power += num_digits - DECIMAL_DIG;
374 exponent_power += pos0 - pos; /* Adjust exponent for decimal point. */
377 #ifdef _STRTOD_HEXADECIMAL_FLOATS
379 exponent_power *= 4; /* Above is 2**4, but below is 2. */
382 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
384 if (negative) { /* Correct for sign. */
388 /* process an exponent string */
389 if (((*pos)|0x20) == EXPCHAR) {
390 #ifdef _STRTOD_ENDPTR
394 switch(*++pos) { /* Handle optional sign. */
395 case '-': negative = -1; /* Fall through to increment pos. */
401 while (isdigit(*pos)) { /* Process string of digits. */
402 #ifdef _STRTOD_RESTRICT_EXP
403 if (exponent_temp < MAX_ALLOWED_EXP) { /* Avoid overflow. */
404 exponent_temp = exponent_temp * 10 + (*pos - '0');
407 exponent_temp = exponent_temp * 10 + (*pos - '0');
412 #ifdef _STRTOD_ENDPTR
413 if (pos == pos0) { /* No digits? */
414 pos = pos1; /* Back up to {e|E}/{p|P}. */
418 exponent_power += negative * exponent_temp;
421 #ifdef _STRTOD_ZERO_CHECK
427 /* scale the result */
428 #ifdef _STRTOD_LOG_SCALING
429 exponent_temp = exponent_power;
431 if (exponent_temp < 0) {
432 exponent_temp = -exponent_temp;
435 while (exponent_temp) {
436 if (exponent_temp & 1) {
437 if (exponent_power < 0) {
438 /* Warning... caluclating a factor for the exponent and
439 * then dividing could easily be faster. But doing so
440 * might cause problems when dealing with denormals. */
450 #else /* _STRTOD_LOG_SCALING */
451 while (exponent_power) {
452 if (exponent_power < 0) {
460 #endif /* _STRTOD_LOG_SCALING */
463 if (__FPMAX_ZERO_OR_INF_CHECK(number)) {
469 #ifdef _STRTOD_ENDPTR
478 #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
481 /**********************************************************************/
482 #ifdef L___fp_range_check
483 #if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)
485 void attribute_hidden __fp_range_check(__fpmax_t y, __fpmax_t x)
487 if (__FPMAX_ZERO_OR_INF_CHECK(y) /* y is 0 or +/- infinity */
488 && (y != 0) /* y is not 0 (could have x>0, y==0 if underflow) */
489 && !__FPMAX_ZERO_OR_INF_CHECK(x) /* x is not 0 or +/- infinity */
491 __set_errno(ERANGE); /* Then x is not in y's range. */
497 /**********************************************************************/
498 #if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)
499 #if defined(NEED_STRTOF_WRAPPER)
501 #if defined(L_wcstof) || defined(L_wcstof_l)
502 #define strtof wcstof
503 #define strtof_l wcstof_l
504 #define __strtofpmax __wcstofpmax
505 #define __strtofpmax_l __wcstofpmax_l
506 #define Wchar wchar_t
512 float __XL_NPP(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
515 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
520 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
523 __fp_range_check(y, x);
531 /**********************************************************************/
532 #if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)
533 #if defined(NEED_STRTOD_WRAPPER)
535 #if defined(L_wcstod) || defined(L_wcstod_l)
536 #define strtod wcstod
537 #define strtod_l wcstod_l
538 #define __strtofpmax __wcstofpmax
539 #define __strtofpmax_l __wcstofpmax_l
540 #define Wchar wchar_t
545 double __XL_NPP(strtod)(const Wchar *__restrict str,
546 Wchar **__restrict endptr __LOCALE_PARAM )
549 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
554 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
557 __fp_range_check(y, x);
563 libc_hidden_def(strtod)
568 /**********************************************************************/
569 #if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)
570 #if defined(NEED_STRTOLD_WRAPPER)
572 #if defined(L_wcstold) || defined(L_wcstold_l)
573 #define strtold wcstold
574 #define strtold_l wcstold_l
575 #define __strtofpmax __wcstofpmax
576 #define __strtofpmax_l __wcstofpmax_l
577 #define Wchar wchar_t
582 long double __XL_NPP(strtold) (const Wchar *str, Wchar **endptr __LOCALE_PARAM )
585 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
590 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
593 __fp_range_check(y, x);
601 /**********************************************************************/