Add fast integer special case in jsV_numbertostring.

Also fix js_itoa to support signed integers.
This commit is contained in:
Tor Andersson
2017-05-23 13:15:11 +02:00
parent 3f5f3034d2
commit ebbc191fe1
2 changed files with 19 additions and 2 deletions
+1
View File
@@ -11,6 +11,7 @@
#include <setjmp.h>
#include <math.h>
#include <float.h>
#include <limits.h>
/* Microsoft Visual C */
#ifdef _MSC_VER
+18 -2
View File
@@ -128,10 +128,17 @@ int jsV_toboolean(js_State *J, js_Value *v)
}
}
const char *js_itoa(char *out, int a)
const char *js_itoa(char *out, int v)
{
char buf[32], *s = out;
unsigned int a;
int i = 0;
if (v < 0) {
a = -v;
*s++ = '-';
} else {
a = v;
}
while (a) {
buf[i++] = (a % 10) + '0';
a /= 10;
@@ -222,9 +229,18 @@ const char *jsV_numbertostring(js_State *J, char buf[32], double f)
char digits[32], *p = buf, *s = digits;
int exp, ndigits, point;
if (f == 0) return "0";
if (isnan(f)) return "NaN";
if (isinf(f)) return f < 0 ? "-Infinity" : "Infinity";
if (f == 0) return "0";
/* Fast case for integers. This only works assuming all integers can be
* exactly represented by a float. This is true for 32-bit integers and
* 64-bit floats. */
if (f >= INT_MIN && f <= INT_MAX) {
int i = (int)f;
if ((double)i == f)
return js_itoa(buf, i);
}
ndigits = js_grisu2(f, digits, &exp);
point = ndigits + exp;