From ebbc191fe18bf1d0c8e6a588a92880af38b0ca71 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 23 May 2017 10:54:17 +0200 Subject: [PATCH] Add fast integer special case in jsV_numbertostring. Also fix js_itoa to support signed integers. --- jsi.h | 1 + jsvalue.c | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/jsi.h b/jsi.h index 10aa582..ffbdf51 100644 --- a/jsi.h +++ b/jsi.h @@ -11,6 +11,7 @@ #include #include #include +#include /* Microsoft Visual C */ #ifdef _MSC_VER diff --git a/jsvalue.c b/jsvalue.c index 51daf9b..7bc19d8 100644 --- a/jsvalue.c +++ b/jsvalue.c @@ -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;