123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- #define _STRTOD_NAN_INF_STRINGS 1
- #define _STRTOD_HEXADECIMAL_FLOATS 1
- #define _STRTOD_LOG_SCALING 1
- #define _STRTOD_ERRNO 1
- #define _STRTOD_ENDPTR 1
- #define _STRTOD_RESTRICT_EXP 1
- #define _STRTOD_RESTRICT_DIGITS 1
- #define _STRTOD_ZERO_CHECK 1
- #ifdef _STRTOD_ERRNO
- #undef _STRTOD_ENDPTR
- #undef _STRTOD_RESTRICT_EXP
- #undef _STRTOD_RESTRICT_DIGITS
- #undef _STRTOD_ZERO_CHECK
- #define _STRTOD_ENDPTR 1
- #define _STRTOD_RESTRICT_EXP 1
- #define _STRTOD_RESTRICT_DIGITS 1
- #define _STRTOD_ZERO_CHECK 1
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #include <limits.h>
- #include <float.h>
- #include <bits/uClibc_fpmax.h>
- #include <locale.h>
- #ifdef __UCLIBC_HAS_WCHAR__
- # include <wchar.h>
- # include <wctype.h>
- # include <bits/uClibc_uwchar.h>
- #endif
- #undef _STRTOD_HEXADECIMAL_FLOATS
- #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
- # define _STRTOD_HEXADECIMAL_FLOATS 1
- #endif
- #undef _STRTOD_FPMAX
- #if FPMAX_TYPE == 3
- #define NEED_STRTOLD_WRAPPER
- #define NEED_STRTOD_WRAPPER
- #define NEED_STRTOF_WRAPPER
- #elif FPMAX_TYPE == 2
- #define NEED_STRTOD_WRAPPER
- #define NEED_STRTOF_WRAPPER
- #elif FPMAX_TYPE == 1
- #define NEED_STRTOF_WRAPPER
- #else
- #error unknown FPMAX_TYPE!
- #endif
- extern void __fp_range_check(__fpmax_t y, __fpmax_t x) attribute_hidden;
- #ifdef _STRTOD_RESTRICT_DIGITS
- #define EXP_DENORM_ADJUST DECIMAL_DIG
- #define MAX_ALLOWED_EXP (DECIMAL_DIG + EXP_DENORM_ADJUST - FPMAX_MIN_10_EXP)
- #if MAX_ALLOWED_EXP > INT_MAX
- #error size assumption violated for MAX_ALLOWED_EXP
- #endif
- #else
- #define MAX_ALLOWED_EXP ((20 - FPMAX_MIN_10_EXP) * 2)
- #endif
- #if defined(_STRTOD_RESTRICT_DIGITS) || defined(_STRTOD_ENDPTR) || defined(_STRTOD_HEXADECIMAL_FLOATS)
- #undef _STRTOD_NEED_NUM_DIGITS
- #define _STRTOD_NEED_NUM_DIGITS 1
- #endif
- #if defined(L___strtofpmax) || defined(L___strtofpmax_l) || defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
- #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
- #define __strtofpmax __wcstofpmax
- #define __strtofpmax_l __wcstofpmax_l
- #define Wchar wchar_t
- #ifdef __UCLIBC_DO_XLOCALE
- #define ISSPACE(C) iswspace_l((C), locale_arg)
- #else
- #define ISSPACE(C) iswspace((C))
- #endif
- #else
- #define Wchar char
- #ifdef __UCLIBC_DO_XLOCALE
- #define ISSPACE(C) isspace_l((C), locale_arg)
- #else
- #define ISSPACE(C) isspace((C))
- #endif
- #endif
- #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
- __fpmax_t attribute_hidden __strtofpmax(const Wchar *str, Wchar **endptr, int exponent_power)
- {
- return __strtofpmax_l(str, endptr, exponent_power, __UCLIBC_CURLOCALE);
- }
- #else
- __fpmax_t attribute_hidden __XL_NPP(__strtofpmax)(const Wchar *str, Wchar **endptr, int exponent_power
- __LOCALE_PARAM )
- {
- __fpmax_t number;
- __fpmax_t p_base = 10;
- Wchar *pos0;
- #ifdef _STRTOD_ENDPTR
- Wchar *pos1;
- #endif
- Wchar *pos = (Wchar *) str;
- int exponent_temp;
- int negative;
- #ifdef _STRTOD_NEED_NUM_DIGITS
- int num_digits;
- #endif
- #ifdef __UCLIBC_HAS_LOCALE__
- #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
- wchar_t decpt_wc = __LOCALE_PTR->decimal_point_wc;
- #else
- const char *decpt = __LOCALE_PTR->decimal_point;
- int decpt_len = __LOCALE_PTR->decimal_point_len;
- #endif
- #endif
- #ifdef _STRTOD_HEXADECIMAL_FLOATS
- Wchar expchar = 'e';
- Wchar *poshex = NULL;
- __uint16_t is_mask = _ISdigit;
- #define EXPCHAR expchar
- #define IS_X_DIGIT(C) __isctype((C), is_mask)
- #else
- #define EXPCHAR 'e'
- #define IS_X_DIGIT(C) isdigit((C))
- #endif
- while (ISSPACE(*pos)) {
- ++pos;
- }
- negative = 0;
- switch(*pos) {
- case '-': negative = 1;
- case '+': ++pos;
- }
- #ifdef _STRTOD_HEXADECIMAL_FLOATS
- if ((*pos == '0') && (((pos[1])|0x20) == 'x')) {
- poshex = ++pos;
- ++pos;
- is_mask = _ISxdigit;
- expchar = 'p';
- p_base = 16;
- }
- #endif
- number = 0.;
- #ifdef _STRTOD_NEED_NUM_DIGITS
- num_digits = -1;
- #endif
- pos0 = NULL;
- LOOP:
- while (IS_X_DIGIT(*pos)) {
- #ifdef _STRTOD_RESTRICT_DIGITS
- if (num_digits < 0) {
- ++num_digits;
- }
- if (num_digits || (*pos != '0')) {
- ++num_digits;
- if (num_digits <= DECIMAL_DIG) {
- #ifdef _STRTOD_HEXADECIMAL_FLOATS
- number = number * p_base
- + (isdigit(*pos)
- ? (*pos - '0')
- : (((*pos)|0x20) - ('a' - 10)));
- #else
- number = number * p_base + (*pos - '0');
- #endif
- }
- }
- #else
- #ifdef _STRTOD_NEED_NUM_DIGITS
- ++num_digits;
- #endif
- #ifdef _STRTOD_HEXADECIMAL_FLOATS
- number = number * p_base
- + (isdigit(*pos)
- ? (*pos - '0')
- : (((*pos)|0x20) - ('a' - 10)));
- #else
- number = number * p_base + (*pos - '0');
- #endif
- #endif
- ++pos;
- }
- #ifdef __UCLIBC_HAS_LOCALE__
- #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
- if (!pos0 && (*pos == decpt_wc)) {
- pos0 = ++pos;
- goto LOOP;
- }
- #else
- if (!pos0 && !memcmp(pos, decpt, decpt_len)) {
- pos0 = (pos += decpt_len);
- goto LOOP;
- }
- #endif
- #else
- if ((*pos == '.') && !pos0) {
- pos0 = ++pos;
- goto LOOP;
- }
- #endif
- #ifdef _STRTOD_NEED_NUM_DIGITS
- if (num_digits<0) {
- #ifdef _STRTOD_HEXADECIMAL_FLOATS
- if (poshex) {
- pos = poshex;
- goto DONE;
- }
- #endif
- #ifdef _STRTOD_NAN_INF_STRINGS
- if (!pos0) {
-
- static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0";
- int i = 0;
- do {
-
- int j = 0;
-
- while ((pos[j] | 0x20) == nan_inf_str[i+1+j]) {
- ++j;
- if (!nan_inf_str[i+1+j]) {
- number = i / 0.;
- if (negative) {
- number = -number;
- }
- pos += nan_inf_str[i] - 2;
- goto DONE;
- }
- }
- i += nan_inf_str[i];
- } while (nan_inf_str[i]);
- }
- #endif
- #ifdef _STRTOD_ENDPTR
- pos = (Wchar *) str;
- #endif
- goto DONE;
- }
- #endif
- #ifdef _STRTOD_RESTRICT_DIGITS
- if (num_digits > DECIMAL_DIG) {
- exponent_power += num_digits - DECIMAL_DIG;
- }
- #endif
- if (pos0) {
- exponent_power += pos0 - pos;
- }
- #ifdef _STRTOD_HEXADECIMAL_FLOATS
- if (poshex) {
- exponent_power *= 4;
- p_base = 2;
- }
- #endif
- if (negative) {
- number = -number;
- }
-
- if (((*pos)|0x20) == EXPCHAR) {
- #ifdef _STRTOD_ENDPTR
- pos1 = pos;
- #endif
- negative = 1;
- switch(*++pos) {
- case '-': negative = -1;
- case '+': ++pos;
- }
- pos0 = pos;
- exponent_temp = 0;
- while (isdigit(*pos)) {
- #ifdef _STRTOD_RESTRICT_EXP
- if (exponent_temp < MAX_ALLOWED_EXP) {
- exponent_temp = exponent_temp * 10 + (*pos - '0');
- }
- #else
- exponent_temp = exponent_temp * 10 + (*pos - '0');
- #endif
- ++pos;
- }
- #ifdef _STRTOD_ENDPTR
- if (pos == pos0) {
- pos = pos1;
- }
- #endif
- exponent_power += negative * exponent_temp;
- }
- #ifdef _STRTOD_ZERO_CHECK
- if (number == 0.) {
- goto DONE;
- }
- #endif
-
- #ifdef _STRTOD_LOG_SCALING
- exponent_temp = exponent_power;
- if (exponent_temp < 0) {
- exponent_temp = -exponent_temp;
- }
- while (exponent_temp) {
- if (exponent_temp & 1) {
- if (exponent_power < 0) {
-
- number /= p_base;
- } else {
- number *= p_base;
- }
- }
- exponent_temp >>= 1;
- p_base *= p_base;
- }
- #else
- while (exponent_power) {
- if (exponent_power < 0) {
- number /= p_base;
- exponent_power++;
- } else {
- number *= p_base;
- exponent_power--;
- }
- }
- #endif
- #ifdef _STRTOD_ERRNO
- if (__FPMAX_ZERO_OR_INF_CHECK(number)) {
- __set_errno(ERANGE);
- }
- #endif
- DONE:
- #ifdef _STRTOD_ENDPTR
- if (endptr) {
- *endptr = pos;
- }
- #endif
- return number;
- }
- #endif
- #endif
- #ifdef L___fp_range_check
- #if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)
- void attribute_hidden __fp_range_check(__fpmax_t y, __fpmax_t x)
- {
- if (__FPMAX_ZERO_OR_INF_CHECK(y)
- && (y != 0)
- && !__FPMAX_ZERO_OR_INF_CHECK(x)
- ) {
- __set_errno(ERANGE);
- }
- }
- #endif
- #endif
- #if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)
- #if defined(NEED_STRTOF_WRAPPER)
- #if defined(L_wcstof) || defined(L_wcstof_l)
- #define strtof wcstof
- #define strtof_l wcstof_l
- #define __strtofpmax __wcstofpmax
- #define __strtofpmax_l __wcstofpmax_l
- #define Wchar wchar_t
- #else
- #define Wchar char
- #endif
- float __XL_NPP(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
- {
- #if FPMAX_TYPE == 1
- return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
- #else
- __fpmax_t x;
- float y;
- x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
- y = (float) x;
- __fp_range_check(y, x);
- return y;
- #endif
- }
- #endif
- #endif
- #if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)
- #if defined(NEED_STRTOD_WRAPPER)
- #if defined(L_wcstod) || defined(L_wcstod_l)
- #define strtod wcstod
- #define strtod_l wcstod_l
- #define __strtofpmax __wcstofpmax
- #define __strtofpmax_l __wcstofpmax_l
- #define Wchar wchar_t
- #else
- #define Wchar char
- #endif
- double __XL_NPP(strtod)(const Wchar *__restrict str,
- Wchar **__restrict endptr __LOCALE_PARAM )
- {
- #if FPMAX_TYPE == 2
- return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
- #else
- __fpmax_t x;
- double y;
- x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
- y = (double) x;
- __fp_range_check(y, x);
- return y;
- #endif
- }
- #ifdef L_strtod
- libc_hidden_def(strtod)
- #endif
- #endif
- #endif
- #if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)
- #if defined(NEED_STRTOLD_WRAPPER)
- #if defined(L_wcstold) || defined(L_wcstold_l)
- #define strtold wcstold
- #define strtold_l wcstold_l
- #define __strtofpmax __wcstofpmax
- #define __strtofpmax_l __wcstofpmax_l
- #define Wchar wchar_t
- #else
- #define Wchar char
- #endif
- long double __XL_NPP(strtold) (const Wchar *str, Wchar **endptr __LOCALE_PARAM )
- {
- #if FPMAX_TYPE == 3
- return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
- #else
- __fpmax_t x;
- long double y;
- x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
- y = (long double) x;
- __fp_range_check(y, x);
- return y;
- #endif
- }
- #endif
- #endif
|