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

@ -12,8 +12,10 @@ const char *lua_init_value = "@init.lua";
// }
// void c_exit(int e){
// }
const char *c_getenv(const char *__string){
if (c_strcmp(__string, "LUA_INIT") == 0){
const char *c_getenv(const char *__string)
{
if (c_strcmp(__string, "LUA_INIT") == 0)
{
return lua_init_value;
}
return NULL;
@ -55,14 +57,8 @@ const char *c_getenv(const char *__string){
#include <string.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 */
100., /* exponents into floating-point numbers. */
1.0e4,
@ -73,6 +69,15 @@ double c_strtod(const char *string, char **endPtr)
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;
@ -98,14 +103,19 @@ double c_strtod(const char *string, char **endPtr)
*/
p = string;
while (isspace((unsigned char)(*p))) {
while (isspace((unsigned char)(*p)))
{
p += 1;
}
if (*p == '-') {
if (*p == '-')
{
sign = TRUE;
p += 1;
} else {
if (*p == '+') {
}
else
{
if (*p == '+')
{
p += 1;
}
sign = FALSE;
@ -120,8 +130,10 @@ double c_strtod(const char *string, char **endPtr)
for (mantSize = 0; ; mantSize += 1)
{
c = *p;
if (!isdigit(c)) {
if ((c != '.') || (decPt >= 0)) {
if (!isdigit(c))
{
if ((c != '.') || (decPt >= 0))
{
break;
}
decPt = mantSize;
@ -138,29 +150,39 @@ double c_strtod(const char *string, char **endPtr)
pExp = p;
p -= mantSize;
if (decPt < 0) {
if (decPt < 0)
{
decPt = mantSize;
} else {
}
else
{
mantSize -= 1; /* One of the digits was the point. */
}
if (mantSize > 18) {
if (mantSize > 18)
{
fracExp = decPt - 18;
mantSize = 18;
} else {
}
else
{
fracExp = decPt - mantSize;
}
if (mantSize == 0) {
if (mantSize == 0)
{
fraction = 0.0;
p = string;
goto done;
} else {
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mantSize > 9; mantSize -= 1)
{
c = *p;
p += 1;
if (c == '.') {
if (c == '.')
{
c = *p;
p += 1;
}
@ -171,7 +193,8 @@ double c_strtod(const char *string, char **endPtr)
{
c = *p;
p += 1;
if (c == '.') {
if (c == '.')
{
c = *p;
p += 1;
}
@ -185,29 +208,39 @@ double c_strtod(const char *string, char **endPtr)
*/
p = pExp;
if ((*p == 'E') || (*p == 'e')) {
if ((*p == 'E') || (*p == 'e'))
{
p += 1;
if (*p == '-') {
if (*p == '-')
{
expSign = TRUE;
p += 1;
} else {
if (*p == '+') {
}
else
{
if (*p == '+')
{
p += 1;
}
expSign = FALSE;
}
if (!isdigit((unsigned char)(*p))) {
if (!isdigit((unsigned char)(*p)))
{
p = pExp;
goto done;
}
while (isdigit((unsigned char)(*p))) {
while (isdigit((unsigned char)(*p)))
{
exp = exp * 10 + (*p - '0');
p += 1;
}
}
if (expSign) {
if (expSign)
{
exp = fracExp - exp;
} else {
}
else
{
exp = fracExp + exp;
}
@ -218,34 +251,45 @@ double c_strtod(const char *string, char **endPtr)
* fraction.
*/
if (exp < 0) {
if (exp < 0)
{
expSign = TRUE;
exp = -exp;
} else {
}
else
{
expSign = FALSE;
}
if (exp > maxExponent) {
if (exp > maxExponent)
{
exp = maxExponent;
// errno = ERANGE;
}
dblExp = 1.0;
for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
if (exp & 01) {
for (d = powersOf10; exp != 0; exp >>= 1, d += 1)
{
if (exp & 01)
{
dblExp *= *d;
}
}
if (expSign) {
if (expSign)
{
fraction /= dblExp;
} else {
}
else
{
fraction *= dblExp;
}
done:
if (endPtr != NULL) {
if (endPtr != NULL)
{
*endPtr = (char *) p;
}
if (sign) {
if (sign)
{
return -fraction;
}
return fraction;