From d49182c1abd3d6cfc5e7af16c094191270bad344 Mon Sep 17 00:00:00 2001 From: HuangRui Date: Wed, 11 Feb 2015 23:05:46 +0800 Subject: [PATCH] Move powersOf10 array to global. --- app/libc/c_stdlib.c | 340 +++++++++++++++++++++++++------------------- 1 file changed, 192 insertions(+), 148 deletions(-) diff --git a/app/libc/c_stdlib.c b/app/libc/c_stdlib.c index fffd47b5..b01e938e 100644 --- a/app/libc/c_stdlib.c +++ b/app/libc/c_stdlib.c @@ -8,63 +8,59 @@ const char *lua_init_value = "@init.lua"; // int c_abs(int x){ -// return x>0?x:0-x; +// return x>0?x:0-x; // } // void c_exit(int e){ // } -const char *c_getenv(const char *__string){ - if (c_strcmp(__string, "LUA_INIT") == 0){ - return lua_init_value; - } - return NULL; +const char *c_getenv(const char *__string) +{ + if (c_strcmp(__string, "LUA_INIT") == 0) + { + return lua_init_value; + } + return NULL; } // make sure there is enough memory before real malloc, otherwise malloc will panic and reset // void *c_malloc(size_t __size){ -// if(__size>system_get_free_heap_size()){ -// NODE_ERR("malloc: not enough memory\n"); -// return NULL; -// } -// return (void *)os_malloc(__size); +// if(__size>system_get_free_heap_size()){ +// NODE_ERR("malloc: not enough memory\n"); +// return NULL; +// } +// return (void *)os_malloc(__size); // } // void *c_zalloc(size_t __size){ -// if(__size>system_get_free_heap_size()){ -// NODE_ERR("zalloc: not enough memory\n"); -// return NULL; -// } -// return (void *)os_zalloc(__size); +// if(__size>system_get_free_heap_size()){ +// NODE_ERR("zalloc: not enough memory\n"); +// return NULL; +// } +// return (void *)os_zalloc(__size); // } // void c_free(void *p){ -// // NODE_ERR("free1: %d\n", system_get_free_heap_size()); -// os_free(p); -// // NODE_ERR("-free1: %d\n", system_get_free_heap_size()); +// // NODE_ERR("free1: %d\n", system_get_free_heap_size()); +// os_free(p); +// // NODE_ERR("-free1: %d\n", system_get_free_heap_size()); // } -// int c_rand(void){ +// int c_rand(void){ // } // void c_srand(unsigned int __seed){ // } -// int c_atoi(const char *__nptr){ +// int c_atoi(const char *__nptr){ // } #include <_ansi.h> //#include #include //#include "mprec.h" -double c_strtod(const char *string, char **endPtr) +double powersOf10[] = /* Table giving binary powers of 10. Entry */ { - int maxExponent = 511; /* Largest possible base 10 exponent. Any - * exponent larger than this will already - * produce underflow or overflow, so there's - * no need to worry about additional digits. - */ - double powersOf10[] = { /* Table giving binary powers of 10. Entry */ - 10., /* is 10^2^i. Used to convert decimal */ - 100., /* exponents into floating-point numbers. */ + 10., /* is 10^2^i. Used to convert decimal */ + 100., /* exponents into floating-point numbers. */ 1.0e4, 1.0e8, 1.0e16, @@ -72,43 +68,57 @@ double c_strtod(const char *string, char **endPtr) 1.0e64, 1.0e128, 1.0e256 - }; +}; + +double c_strtod(const char *string, char **endPtr) +{ + int maxExponent = 511; /* Largest possible base 10 exponent. Any + * exponent larger than this will already + * produce underflow or overflow, so there's + * no need to worry about additional digits. + */ + int sign, expSign = FALSE; double fraction, dblExp, *d; register const char *p; register int c; - int exp = 0; /* Exponent read from "EX" field. */ - int fracExp = 0; /* Exponent that derives from the fractional - * part. Under normal circumstatnces, it is - * the negative of the number of digits in F. - * However, if I is very long, the last digits - * of I get dropped (otherwise a long I with a - * large negative exponent could cause an - * unnecessary overflow on I alone). In this - * case, fracExp is incremented one for each - * dropped digit. */ - int mantSize; /* Number of digits in mantissa. */ - int decPt; /* Number of mantissa digits BEFORE decimal - * point. */ - const char *pExp; /* Temporarily holds location of exponent - * in string. */ + int exp = 0; /* Exponent read from "EX" field. */ + int fracExp = 0; /* Exponent that derives from the fractional + * part. Under normal circumstatnces, it is + * the negative of the number of digits in F. + * However, if I is very long, the last digits + * of I get dropped (otherwise a long I with a + * large negative exponent could cause an + * unnecessary overflow on I alone). In this + * case, fracExp is incremented one for each + * dropped digit. */ + int mantSize; /* Number of digits in mantissa. */ + int decPt; /* Number of mantissa digits BEFORE decimal + * point. */ + const char *pExp; /* Temporarily holds location of exponent + * in string. */ /* * Strip off leading blanks and check for a sign. */ p = string; - while (isspace((unsigned char)(*p))) { - p += 1; + while (isspace((unsigned char)(*p))) + { + p += 1; } - if (*p == '-') { - sign = TRUE; - p += 1; - } else { - if (*p == '+') { - p += 1; - } - sign = FALSE; + if (*p == '-') + { + sign = TRUE; + p += 1; + } + else + { + if (*p == '+') + { + p += 1; + } + sign = FALSE; } /* @@ -119,14 +129,16 @@ double c_strtod(const char *string, char **endPtr) decPt = -1; for (mantSize = 0; ; mantSize += 1) { - c = *p; - if (!isdigit(c)) { - if ((c != '.') || (decPt >= 0)) { - break; - } - decPt = mantSize; - } - p += 1; + c = *p; + if (!isdigit(c)) + { + if ((c != '.') || (decPt >= 0)) + { + break; + } + decPt = mantSize; + } + p += 1; } /* @@ -135,49 +147,60 @@ double c_strtod(const char *string, char **endPtr) * If the mantissa has more than 18 digits, ignore the extras, since * they can't affect the value anyway. */ - + pExp = p; p -= mantSize; - if (decPt < 0) { - decPt = mantSize; - } else { - mantSize -= 1; /* One of the digits was the point. */ + if (decPt < 0) + { + decPt = mantSize; } - if (mantSize > 18) { - fracExp = decPt - 18; - mantSize = 18; - } else { - fracExp = decPt - mantSize; + else + { + mantSize -= 1; /* One of the digits was the point. */ } - if (mantSize == 0) { - fraction = 0.0; - p = string; - goto done; - } else { - int frac1, frac2; - frac1 = 0; - for ( ; mantSize > 9; mantSize -= 1) - { - c = *p; - p += 1; - if (c == '.') { - c = *p; - p += 1; - } - frac1 = 10*frac1 + (c - '0'); - } - frac2 = 0; - for (; mantSize > 0; mantSize -= 1) - { - c = *p; - p += 1; - if (c == '.') { - c = *p; - p += 1; - } - frac2 = 10*frac2 + (c - '0'); - } - fraction = (1.0e9 * frac1) + frac2; + if (mantSize > 18) + { + fracExp = decPt - 18; + mantSize = 18; + } + else + { + fracExp = decPt - mantSize; + } + if (mantSize == 0) + { + fraction = 0.0; + p = string; + goto done; + } + else + { + int frac1, frac2; + frac1 = 0; + for ( ; mantSize > 9; mantSize -= 1) + { + c = *p; + p += 1; + if (c == '.') + { + c = *p; + p += 1; + } + frac1 = 10 * frac1 + (c - '0'); + } + frac2 = 0; + for (; mantSize > 0; mantSize -= 1) + { + c = *p; + p += 1; + if (c == '.') + { + c = *p; + p += 1; + } + frac2 = 10 * frac2 + (c - '0'); + } + fraction = (1.0e9 * frac1) + frac2; } /* @@ -185,30 +208,40 @@ double c_strtod(const char *string, char **endPtr) */ p = pExp; - if ((*p == 'E') || (*p == 'e')) { - p += 1; - if (*p == '-') { - expSign = TRUE; - p += 1; - } else { - if (*p == '+') { - p += 1; - } - expSign = FALSE; - } - if (!isdigit((unsigned char)(*p))) { - p = pExp; - goto done; - } - while (isdigit((unsigned char)(*p))) { - exp = exp * 10 + (*p - '0'); - p += 1; - } + if ((*p == 'E') || (*p == 'e')) + { + p += 1; + if (*p == '-') + { + expSign = TRUE; + p += 1; + } + else + { + if (*p == '+') + { + p += 1; + } + expSign = FALSE; + } + if (!isdigit((unsigned char)(*p))) + { + p = pExp; + goto done; + } + while (isdigit((unsigned char)(*p))) + { + exp = exp * 10 + (*p - '0'); + p += 1; + } } - if (expSign) { - exp = fracExp - exp; - } else { - exp = fracExp + exp; + if (expSign) + { + exp = fracExp - exp; + } + else + { + exp = fracExp + exp; } /* @@ -217,41 +250,52 @@ double c_strtod(const char *string, char **endPtr) * many powers of 2 of 10. Then combine the exponent with the * fraction. */ - - if (exp < 0) { - expSign = TRUE; - exp = -exp; - } else { - expSign = FALSE; + + if (exp < 0) + { + expSign = TRUE; + exp = -exp; } - if (exp > maxExponent) { - exp = maxExponent; - // errno = ERANGE; + else + { + expSign = FALSE; + } + if (exp > maxExponent) + { + exp = maxExponent; + // errno = ERANGE; } dblExp = 1.0; - for (d = powersOf10; exp != 0; exp >>= 1, d += 1) { - if (exp & 01) { - dblExp *= *d; - } + for (d = powersOf10; exp != 0; exp >>= 1, d += 1) + { + if (exp & 01) + { + dblExp *= *d; + } } - if (expSign) { - fraction /= dblExp; - } else { - fraction *= dblExp; + if (expSign) + { + fraction /= dblExp; + } + else + { + fraction *= dblExp; } done: - if (endPtr != NULL) { - *endPtr = (char *) p; + if (endPtr != NULL) + { + *endPtr = (char *) p; } - if (sign) { - return -fraction; + if (sign) + { + return -fraction; } return fraction; } -// long c_strtol(const char *__n, char **__end_PTR, int __base){ +// long c_strtol(const char *__n, char **__end_PTR, int __base){ // } // unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base){ // }