Move powersOf10 array to global.

This commit is contained in:
HuangRui 2015-02-11 23:05:46 +08:00
parent db43b1e005
commit d49182c1ab
1 changed files with 192 additions and 148 deletions

View File

@ -8,63 +8,59 @@
const char *lua_init_value = "@init.lua"; const char *lua_init_value = "@init.lua";
// int c_abs(int x){ // int c_abs(int x){
// return x>0?x:0-x; // return x>0?x:0-x;
// } // }
// void c_exit(int e){ // void c_exit(int e){
// } // }
const char *c_getenv(const char *__string){ const char *c_getenv(const char *__string)
if (c_strcmp(__string, "LUA_INIT") == 0){ {
return lua_init_value; if (c_strcmp(__string, "LUA_INIT") == 0)
} {
return NULL; return lua_init_value;
}
return NULL;
} }
// make sure there is enough memory before real malloc, otherwise malloc will panic and reset // make sure there is enough memory before real malloc, otherwise malloc will panic and reset
// void *c_malloc(size_t __size){ // void *c_malloc(size_t __size){
// if(__size>system_get_free_heap_size()){ // if(__size>system_get_free_heap_size()){
// NODE_ERR("malloc: not enough memory\n"); // NODE_ERR("malloc: not enough memory\n");
// return NULL; // return NULL;
// } // }
// return (void *)os_malloc(__size); // return (void *)os_malloc(__size);
// } // }
// void *c_zalloc(size_t __size){ // void *c_zalloc(size_t __size){
// if(__size>system_get_free_heap_size()){ // if(__size>system_get_free_heap_size()){
// NODE_ERR("zalloc: not enough memory\n"); // NODE_ERR("zalloc: not enough memory\n");
// return NULL; // return NULL;
// } // }
// return (void *)os_zalloc(__size); // return (void *)os_zalloc(__size);
// } // }
// void c_free(void *p){ // void c_free(void *p){
// // NODE_ERR("free1: %d\n", system_get_free_heap_size()); // // NODE_ERR("free1: %d\n", system_get_free_heap_size());
// os_free(p); // os_free(p);
// // NODE_ERR("-free1: %d\n", system_get_free_heap_size()); // // NODE_ERR("-free1: %d\n", system_get_free_heap_size());
// } // }
// int c_rand(void){ // int c_rand(void){
// } // }
// void c_srand(unsigned int __seed){ // void c_srand(unsigned int __seed){
// } // }
// int c_atoi(const char *__nptr){ // int c_atoi(const char *__nptr){
// } // }
#include <_ansi.h> #include <_ansi.h>
//#include <reent.h> //#include <reent.h>
#include <string.h> #include <string.h>
//#include "mprec.h" //#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 10., /* is 10^2^i. Used to convert decimal */
* exponent larger than this will already 100., /* exponents into floating-point numbers. */
* 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. */
1.0e4, 1.0e4,
1.0e8, 1.0e8,
1.0e16, 1.0e16,
@ -72,43 +68,57 @@ double c_strtod(const char *string, char **endPtr)
1.0e64, 1.0e64,
1.0e128, 1.0e128,
1.0e256 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; int sign, expSign = FALSE;
double fraction, dblExp, *d; double fraction, dblExp, *d;
register const char *p; register const char *p;
register int c; register int c;
int exp = 0; /* Exponent read from "EX" field. */ int exp = 0; /* Exponent read from "EX" field. */
int fracExp = 0; /* Exponent that derives from the fractional int fracExp = 0; /* Exponent that derives from the fractional
* part. Under normal circumstatnces, it is * part. Under normal circumstatnces, it is
* the negative of the number of digits in F. * the negative of the number of digits in F.
* However, if I is very long, the last digits * However, if I is very long, the last digits
* of I get dropped (otherwise a long I with a * of I get dropped (otherwise a long I with a
* large negative exponent could cause an * large negative exponent could cause an
* unnecessary overflow on I alone). In this * unnecessary overflow on I alone). In this
* case, fracExp is incremented one for each * case, fracExp is incremented one for each
* dropped digit. */ * dropped digit. */
int mantSize; /* Number of digits in mantissa. */ int mantSize; /* Number of digits in mantissa. */
int decPt; /* Number of mantissa digits BEFORE decimal int decPt; /* Number of mantissa digits BEFORE decimal
* point. */ * point. */
const char *pExp; /* Temporarily holds location of exponent const char *pExp; /* Temporarily holds location of exponent
* in string. */ * in string. */
/* /*
* Strip off leading blanks and check for a sign. * Strip off leading blanks and check for a sign.
*/ */
p = string; p = string;
while (isspace((unsigned char)(*p))) { while (isspace((unsigned char)(*p)))
p += 1; {
p += 1;
} }
if (*p == '-') { if (*p == '-')
sign = TRUE; {
p += 1; sign = TRUE;
} else { p += 1;
if (*p == '+') { }
p += 1; else
} {
sign = FALSE; if (*p == '+')
{
p += 1;
}
sign = FALSE;
} }
/* /*
@ -119,14 +129,16 @@ double c_strtod(const char *string, char **endPtr)
decPt = -1; decPt = -1;
for (mantSize = 0; ; mantSize += 1) for (mantSize = 0; ; mantSize += 1)
{ {
c = *p; c = *p;
if (!isdigit(c)) { if (!isdigit(c))
if ((c != '.') || (decPt >= 0)) { {
break; if ((c != '.') || (decPt >= 0))
} {
decPt = mantSize; break;
} }
p += 1; 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 * If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway. * they can't affect the value anyway.
*/ */
pExp = p; pExp = p;
p -= mantSize; p -= mantSize;
if (decPt < 0) { if (decPt < 0)
decPt = mantSize; {
} else { decPt = mantSize;
mantSize -= 1; /* One of the digits was the point. */
} }
if (mantSize > 18) { else
fracExp = decPt - 18; {
mantSize = 18; mantSize -= 1; /* One of the digits was the point. */
} else {
fracExp = decPt - mantSize;
} }
if (mantSize == 0) { if (mantSize > 18)
fraction = 0.0; {
p = string; fracExp = decPt - 18;
goto done; mantSize = 18;
} else { }
int frac1, frac2; else
frac1 = 0; {
for ( ; mantSize > 9; mantSize -= 1) fracExp = decPt - mantSize;
{ }
c = *p; if (mantSize == 0)
p += 1; {
if (c == '.') { fraction = 0.0;
c = *p; p = string;
p += 1; goto done;
} }
frac1 = 10*frac1 + (c - '0'); else
} {
frac2 = 0; int frac1, frac2;
for (; mantSize > 0; mantSize -= 1) frac1 = 0;
{ for ( ; mantSize > 9; mantSize -= 1)
c = *p; {
p += 1; c = *p;
if (c == '.') { p += 1;
c = *p; if (c == '.')
p += 1; {
} c = *p;
frac2 = 10*frac2 + (c - '0'); p += 1;
} }
fraction = (1.0e9 * frac1) + frac2; 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; p = pExp;
if ((*p == 'E') || (*p == 'e')) { if ((*p == 'E') || (*p == 'e'))
p += 1; {
if (*p == '-') { p += 1;
expSign = TRUE; if (*p == '-')
p += 1; {
} else { expSign = TRUE;
if (*p == '+') { p += 1;
p += 1; }
} else
expSign = FALSE; {
} if (*p == '+')
if (!isdigit((unsigned char)(*p))) { {
p = pExp; p += 1;
goto done; }
} expSign = FALSE;
while (isdigit((unsigned char)(*p))) { }
exp = exp * 10 + (*p - '0'); if (!isdigit((unsigned char)(*p)))
p += 1; {
} p = pExp;
goto done;
}
while (isdigit((unsigned char)(*p)))
{
exp = exp * 10 + (*p - '0');
p += 1;
}
} }
if (expSign) { if (expSign)
exp = fracExp - exp; {
} else { exp = fracExp - exp;
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 * many powers of 2 of 10. Then combine the exponent with the
* fraction. * fraction.
*/ */
if (exp < 0) { if (exp < 0)
expSign = TRUE; {
exp = -exp; expSign = TRUE;
} else { exp = -exp;
expSign = FALSE;
} }
if (exp > maxExponent) { else
exp = maxExponent; {
// errno = ERANGE; expSign = FALSE;
}
if (exp > maxExponent)
{
exp = maxExponent;
// errno = ERANGE;
} }
dblExp = 1.0; dblExp = 1.0;
for (d = powersOf10; exp != 0; exp >>= 1, d += 1) { for (d = powersOf10; exp != 0; exp >>= 1, d += 1)
if (exp & 01) { {
dblExp *= *d; if (exp & 01)
} {
dblExp *= *d;
}
} }
if (expSign) { if (expSign)
fraction /= dblExp; {
} else { fraction /= dblExp;
fraction *= dblExp; }
else
{
fraction *= dblExp;
} }
done: done:
if (endPtr != NULL) { if (endPtr != NULL)
*endPtr = (char *) p; {
*endPtr = (char *) p;
} }
if (sign) { if (sign)
return -fraction; {
return -fraction;
} }
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){ // unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base){
// } // }