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 /**********************************************************************/
104 #include <bits/uClibc_fpmax.h>
108 #ifdef __UCLIBC_HAS_WCHAR__
111 # include <bits/uClibc_uwchar.h>
114 /* Handle _STRTOD_HEXADECIMAL_FLOATS via uClibc config now. */
115 #undef _STRTOD_HEXADECIMAL_FLOATS
116 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
117 # define _STRTOD_HEXADECIMAL_FLOATS 1
120 /**********************************************************************/
126 #define NEED_STRTOLD_WRAPPER
127 #define NEED_STRTOD_WRAPPER
128 #define NEED_STRTOF_WRAPPER
130 #elif FPMAX_TYPE == 2
132 #define NEED_STRTOD_WRAPPER
133 #define NEED_STRTOF_WRAPPER
135 #elif FPMAX_TYPE == 1
137 #define NEED_STRTOF_WRAPPER
141 #error unknown FPMAX_TYPE!
145 extern void __fp_range_check(__fpmax_t y, __fpmax_t x) attribute_hidden;
147 /**********************************************************************/
149 #ifdef _STRTOD_RESTRICT_DIGITS
150 #define EXP_DENORM_ADJUST DECIMAL_DIG
151 #define MAX_ALLOWED_EXP (DECIMAL_DIG + EXP_DENORM_ADJUST - FPMAX_MIN_10_EXP)
153 #if MAX_ALLOWED_EXP > INT_MAX
154 #error size assumption violated for MAX_ALLOWED_EXP
157 /* We want some excess if we're not restricting mantissa digits. */
158 #define MAX_ALLOWED_EXP ((20 - FPMAX_MIN_10_EXP) * 2)
162 #if defined(_STRTOD_RESTRICT_DIGITS) || defined(_STRTOD_ENDPTR) || defined(_STRTOD_HEXADECIMAL_FLOATS)
163 #undef _STRTOD_NEED_NUM_DIGITS
164 #define _STRTOD_NEED_NUM_DIGITS 1
167 /**********************************************************************/
168 #if defined(L___strtofpmax) || defined(L___strtofpmax_l) || defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
170 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
172 #define __strtofpmax __wcstofpmax
173 #define __strtofpmax_l __wcstofpmax_l
175 #define Wchar wchar_t
176 #ifdef __UCLIBC_DO_XLOCALE
177 #define ISSPACE(C) iswspace_l((C), locale_arg)
179 #define ISSPACE(C) iswspace((C))
182 #else /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
185 #ifdef __UCLIBC_DO_XLOCALE
186 #define ISSPACE(C) isspace_l((C), locale_arg)
188 #define ISSPACE(C) isspace((C))
191 #endif /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
194 #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
196 __fpmax_t attribute_hidden __strtofpmax(const Wchar *str, Wchar **endptr, int exponent_power)
198 return __strtofpmax_l(str, endptr, exponent_power, __UCLIBC_CURLOCALE);
201 #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
204 __fpmax_t attribute_hidden __XL_NPP(__strtofpmax)(const Wchar *str, Wchar **endptr, int exponent_power
208 __fpmax_t p_base = 10; /* Adjusted to 16 in the hex case. */
210 #ifdef _STRTOD_ENDPTR
213 Wchar *pos = (Wchar *) str;
215 int negative; /* A flag for the number, a multiplier for the exponent. */
216 #ifdef _STRTOD_NEED_NUM_DIGITS
219 #ifdef __UCLIBC_HAS_LOCALE__
220 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
221 wchar_t decpt_wc = __LOCALE_PTR->decimal_point_wc;
223 const char *decpt = __LOCALE_PTR->decimal_point;
224 int decpt_len = __LOCALE_PTR->decimal_point_len;
228 #ifdef _STRTOD_HEXADECIMAL_FLOATS
230 Wchar *poshex = NULL;
231 __uint16_t is_mask = _ISdigit;
232 #define EXPCHAR expchar
233 #define IS_X_DIGIT(C) __isctype((C), is_mask)
234 #else /* _STRTOD_HEXADECIMAL_FLOATS */
236 #define IS_X_DIGIT(C) isdigit((C))
237 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
239 while (ISSPACE(*pos)) { /* Skip leading whitespace. */
244 switch(*pos) { /* Handle optional sign. */
245 case '-': negative = 1; /* Fall through to increment position. */
249 #ifdef _STRTOD_HEXADECIMAL_FLOATS
250 if ((*pos == '0') && (((pos[1])|0x20) == 'x')) {
251 poshex = ++pos; /* Save position of 'x' in case no digits */
252 ++pos; /* and advance past it. */
253 is_mask = _ISxdigit; /* Used by IS_X_DIGIT. */
254 expchar = 'p'; /* Adjust exponent char. */
255 p_base = 16; /* Adjust base multiplier. */
260 #ifdef _STRTOD_NEED_NUM_DIGITS
263 /* exponent_power = 0; */
267 while (IS_X_DIGIT(*pos)) { /* Process string of (hex) digits. */
268 #ifdef _STRTOD_RESTRICT_DIGITS
269 if (num_digits < 0) { /* First time through? */
270 ++num_digits; /* We've now seen a digit. */
272 if (num_digits || (*pos != '0')) { /* Had/have nonzero. */
274 if (num_digits <= DECIMAL_DIG) { /* Is digit significant? */
275 #ifdef _STRTOD_HEXADECIMAL_FLOATS
276 number = number * p_base
279 : (((*pos)|0x20) - ('a' - 10)));
280 #else /* _STRTOD_HEXADECIMAL_FLOATS */
281 number = number * p_base + (*pos - '0');
282 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
285 #else /* _STRTOD_RESTRICT_DIGITS */
286 #ifdef _STRTOD_NEED_NUM_DIGITS
289 #ifdef _STRTOD_HEXADECIMAL_FLOATS
290 number = number * p_base
293 : (((*pos)|0x20) - ('a' - 10)));
294 #else /* _STRTOD_HEXADECIMAL_FLOATS */
295 number = number * p_base + (*pos - '0');
296 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
297 #endif /* _STRTOD_RESTRICT_DIGITS */
301 #ifdef __UCLIBC_HAS_LOCALE__
302 #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
303 if (!pos0 && (*pos == decpt_wc)) { /* First decimal point? */
308 if (!pos0 && !memcmp(pos, decpt, decpt_len)) { /* First decimal point? */
309 pos0 = (pos += decpt_len);
313 #else /* __UCLIBC_HAS_LOCALE__ */
314 if ((*pos == '.') && !pos0) { /* First decimal point? */
315 pos0 = ++pos; /* Save position of decimal point */
316 goto LOOP; /* and process rest of digits. */
318 #endif /* __UCLIBC_HAS_LOCALE__ */
320 #ifdef _STRTOD_NEED_NUM_DIGITS
321 if (num_digits<0) { /* Must have at least one digit. */
322 #ifdef _STRTOD_HEXADECIMAL_FLOATS
323 if (poshex) { /* Back up to '0' in '0x' prefix. */
327 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
329 #ifdef _STRTOD_NAN_INF_STRINGS
330 if (!pos0) { /* No decimal point, so check for inf/nan. */
331 /* Note: nan is the first string so 'number = i/0.;' works. */
332 static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0";
336 /* Unfortunately, we have no memcasecmp(). */
338 /* | 0x20 is a cheap lowercasing (valid for ASCII letters and numbers only) */
339 while ((pos[j] | 0x20) == nan_inf_str[i+1+j]) {
341 if (!nan_inf_str[i+1+j]) {
343 if (negative) { /* Correct for sign. */
346 pos += nan_inf_str[i] - 2;
351 } while (nan_inf_str[i]);
354 #endif /* STRTOD_NAN_INF_STRINGS */
355 #ifdef _STRTOD_ENDPTR
360 #endif /* _STRTOD_NEED_NUM_DIGITS */
362 #ifdef _STRTOD_RESTRICT_DIGITS
363 if (num_digits > DECIMAL_DIG) { /* Adjust exponent for skipped digits. */
364 exponent_power += num_digits - DECIMAL_DIG;
369 exponent_power += pos0 - pos; /* Adjust exponent for decimal point. */
372 #ifdef _STRTOD_HEXADECIMAL_FLOATS
374 exponent_power *= 4; /* Above is 2**4, but below is 2. */
377 #endif /* _STRTOD_HEXADECIMAL_FLOATS */
379 if (negative) { /* Correct for sign. */
383 /* process an exponent string */
384 if (((*pos)|0x20) == EXPCHAR) {
385 #ifdef _STRTOD_ENDPTR
389 switch(*++pos) { /* Handle optional sign. */
390 case '-': negative = -1; /* Fall through to increment pos. */
396 while (isdigit(*pos)) { /* Process string of digits. */
397 #ifdef _STRTOD_RESTRICT_EXP
398 if (exponent_temp < MAX_ALLOWED_EXP) { /* Avoid overflow. */
399 exponent_temp = exponent_temp * 10 + (*pos - '0');
402 exponent_temp = exponent_temp * 10 + (*pos - '0');
407 #ifdef _STRTOD_ENDPTR
408 if (pos == pos0) { /* No digits? */
409 pos = pos1; /* Back up to {e|E}/{p|P}. */
413 exponent_power += negative * exponent_temp;
416 #ifdef _STRTOD_ZERO_CHECK
422 /* scale the result */
423 #ifdef _STRTOD_LOG_SCALING
424 exponent_temp = exponent_power;
426 if (exponent_temp < 0) {
427 exponent_temp = -exponent_temp;
430 while (exponent_temp) {
431 if (exponent_temp & 1) {
432 if (exponent_power < 0) {
433 /* Warning... caluclating a factor for the exponent and
434 * then dividing could easily be faster. But doing so
435 * might cause problems when dealing with denormals. */
445 #else /* _STRTOD_LOG_SCALING */
446 while (exponent_power) {
447 if (exponent_power < 0) {
455 #endif /* _STRTOD_LOG_SCALING */
458 if (__FPMAX_ZERO_OR_INF_CHECK(number)) {
464 #ifdef _STRTOD_ENDPTR
473 #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
476 /**********************************************************************/
477 #ifdef L___fp_range_check
478 #if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)
480 void attribute_hidden __fp_range_check(__fpmax_t y, __fpmax_t x)
482 if (__FPMAX_ZERO_OR_INF_CHECK(y) /* y is 0 or +/- infinity */
483 && (y != 0) /* y is not 0 (could have x>0, y==0 if underflow) */
484 && !__FPMAX_ZERO_OR_INF_CHECK(x) /* x is not 0 or +/- infinity */
486 __set_errno(ERANGE); /* Then x is not in y's range. */
492 /**********************************************************************/
493 #if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)
494 #if defined(NEED_STRTOF_WRAPPER)
496 #if defined(L_wcstof) || defined(L_wcstof_l)
497 #define strtof wcstof
498 #define strtof_l wcstof_l
499 #define __strtofpmax __wcstofpmax
500 #define __strtofpmax_l __wcstofpmax_l
501 #define Wchar wchar_t
507 float __XL_NPP(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
510 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
515 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
518 __fp_range_check(y, x);
526 /**********************************************************************/
527 #if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)
528 #if defined(NEED_STRTOD_WRAPPER)
530 #if defined(L_wcstod) || defined(L_wcstod_l)
531 #define strtod wcstod
532 #define strtod_l wcstod_l
533 #define __strtofpmax __wcstofpmax
534 #define __strtofpmax_l __wcstofpmax_l
535 #define Wchar wchar_t
540 double __XL_NPP(strtod)(const Wchar *__restrict str,
541 Wchar **__restrict endptr __LOCALE_PARAM )
544 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
549 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
552 __fp_range_check(y, x);
558 libc_hidden_def(strtod)
563 /**********************************************************************/
564 #if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)
565 #if defined(NEED_STRTOLD_WRAPPER)
567 #if defined(L_wcstold) || defined(L_wcstold_l)
568 #define strtold wcstold
569 #define strtold_l wcstold_l
570 #define __strtofpmax __wcstofpmax
571 #define __strtofpmax_l __wcstofpmax_l
572 #define Wchar wchar_t
577 long double __XL_NPP(strtold) (const Wchar *str, Wchar **endptr __LOCALE_PARAM )
580 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
585 x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
588 __fp_range_check(y, x);
596 /**********************************************************************/