Commit d49182c1 authored by HuangRui's avatar HuangRui
Browse files

Move powersOf10 array to global.

parent db43b1e0
...@@ -12,8 +12,10 @@ const char *lua_init_value = "@init.lua"; ...@@ -12,8 +12,10 @@ const char *lua_init_value = "@init.lua";
// } // }
// 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){ {
if (c_strcmp(__string, "LUA_INIT") == 0)
{
return lua_init_value; return lua_init_value;
} }
return NULL; return NULL;
...@@ -55,14 +57,8 @@ const char *c_getenv(const char *__string){ ...@@ -55,14 +57,8 @@ const char *c_getenv(const char *__string){
#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
* 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 */ 10., /* is 10^2^i. Used to convert decimal */
100., /* exponents into floating-point numbers. */ 100., /* exponents into floating-point numbers. */
1.0e4, 1.0e4,
...@@ -72,7 +68,16 @@ double c_strtod(const char *string, char **endPtr) ...@@ -72,7 +68,16 @@ 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;
...@@ -98,14 +103,19 @@ double c_strtod(const char *string, char **endPtr) ...@@ -98,14 +103,19 @@ double c_strtod(const char *string, char **endPtr)
*/ */
p = string; p = string;
while (isspace((unsigned char)(*p))) { while (isspace((unsigned char)(*p)))
{
p += 1; p += 1;
} }
if (*p == '-') { if (*p == '-')
{
sign = TRUE; sign = TRUE;
p += 1; p += 1;
} else { }
if (*p == '+') { else
{
if (*p == '+')
{
p += 1; p += 1;
} }
sign = FALSE; sign = FALSE;
...@@ -120,8 +130,10 @@ double c_strtod(const char *string, char **endPtr) ...@@ -120,8 +130,10 @@ double c_strtod(const char *string, char **endPtr)
for (mantSize = 0; ; mantSize += 1) for (mantSize = 0; ; mantSize += 1)
{ {
c = *p; c = *p;
if (!isdigit(c)) { if (!isdigit(c))
if ((c != '.') || (decPt >= 0)) { {
if ((c != '.') || (decPt >= 0))
{
break; break;
} }
decPt = mantSize; decPt = mantSize;
...@@ -138,44 +150,55 @@ double c_strtod(const char *string, char **endPtr) ...@@ -138,44 +150,55 @@ double c_strtod(const char *string, char **endPtr)
pExp = p; pExp = p;
p -= mantSize; p -= mantSize;
if (decPt < 0) { if (decPt < 0)
{
decPt = mantSize; decPt = mantSize;
} else { }
else
{
mantSize -= 1; /* One of the digits was the point. */ mantSize -= 1; /* One of the digits was the point. */
} }
if (mantSize > 18) { if (mantSize > 18)
{
fracExp = decPt - 18; fracExp = decPt - 18;
mantSize = 18; mantSize = 18;
} else { }
else
{
fracExp = decPt - mantSize; fracExp = decPt - mantSize;
} }
if (mantSize == 0) { if (mantSize == 0)
{
fraction = 0.0; fraction = 0.0;
p = string; p = string;
goto done; goto done;
} else { }
else
{
int frac1, frac2; int frac1, frac2;
frac1 = 0; frac1 = 0;
for ( ; mantSize > 9; mantSize -= 1) for ( ; mantSize > 9; mantSize -= 1)
{ {
c = *p; c = *p;
p += 1; p += 1;
if (c == '.') { if (c == '.')
{
c = *p; c = *p;
p += 1; p += 1;
} }
frac1 = 10*frac1 + (c - '0'); frac1 = 10 * frac1 + (c - '0');
} }
frac2 = 0; frac2 = 0;
for (; mantSize > 0; mantSize -= 1) for (; mantSize > 0; mantSize -= 1)
{ {
c = *p; c = *p;
p += 1; p += 1;
if (c == '.') { if (c == '.')
{
c = *p; c = *p;
p += 1; p += 1;
} }
frac2 = 10*frac2 + (c - '0'); frac2 = 10 * frac2 + (c - '0');
} }
fraction = (1.0e9 * frac1) + frac2; fraction = (1.0e9 * frac1) + frac2;
} }
...@@ -185,29 +208,39 @@ double c_strtod(const char *string, char **endPtr) ...@@ -185,29 +208,39 @@ double c_strtod(const char *string, char **endPtr)
*/ */
p = pExp; p = pExp;
if ((*p == 'E') || (*p == 'e')) { if ((*p == 'E') || (*p == 'e'))
{
p += 1; p += 1;
if (*p == '-') { if (*p == '-')
{
expSign = TRUE; expSign = TRUE;
p += 1; p += 1;
} else { }
if (*p == '+') { else
{
if (*p == '+')
{
p += 1; p += 1;
} }
expSign = FALSE; expSign = FALSE;
} }
if (!isdigit((unsigned char)(*p))) { if (!isdigit((unsigned char)(*p)))
{
p = pExp; p = pExp;
goto done; goto done;
} }
while (isdigit((unsigned char)(*p))) { while (isdigit((unsigned char)(*p)))
{
exp = exp * 10 + (*p - '0'); exp = exp * 10 + (*p - '0');
p += 1; p += 1;
} }
} }
if (expSign) { if (expSign)
{
exp = fracExp - exp; exp = fracExp - exp;
} else { }
else
{
exp = fracExp + exp; exp = fracExp + exp;
} }
...@@ -218,34 +251,45 @@ double c_strtod(const char *string, char **endPtr) ...@@ -218,34 +251,45 @@ double c_strtod(const char *string, char **endPtr)
* fraction. * fraction.
*/ */
if (exp < 0) { if (exp < 0)
{
expSign = TRUE; expSign = TRUE;
exp = -exp; exp = -exp;
} else { }
else
{
expSign = FALSE; expSign = FALSE;
} }
if (exp > maxExponent) { if (exp > maxExponent)
{
exp = maxExponent; exp = maxExponent;
// errno = ERANGE; // 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) { {
if (exp & 01)
{
dblExp *= *d; dblExp *= *d;
} }
} }
if (expSign) { if (expSign)
{
fraction /= dblExp; fraction /= dblExp;
} else { }
else
{
fraction *= dblExp; 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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment