Skip to content

Commit 3d0cc29

Browse files
author
Fabrice Bellard
committed
optimized add/sub int32 overflow
1 parent 125b012 commit 3d0cc29

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

quickjs.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19197,9 +19197,11 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1919719197
if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
1919819198
int64_t r;
1919919199
r = (int64_t)JS_VALUE_GET_INT(op1) + JS_VALUE_GET_INT(op2);
19200-
if (unlikely((int)r != r))
19201-
goto add_slow;
19202-
sp[-2] = JS_NewInt32(ctx, r);
19200+
if (unlikely((int)r != r)) {
19201+
sp[-2] = __JS_NewFloat64(ctx, (double)r);
19202+
} else {
19203+
sp[-2] = JS_NewInt32(ctx, r);
19204+
}
1920319205
sp--;
1920419206
} else if (JS_VALUE_IS_BOTH_FLOAT(op1, op2)) {
1920519207
sp[-2] = __JS_NewFloat64(ctx, JS_VALUE_GET_FLOAT64(op1) +
@@ -19211,7 +19213,6 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1921119213
if (JS_IsException(sp[-1]))
1921219214
goto exception;
1921319215
} else {
19214-
add_slow:
1921519216
sf->cur_pc = pc;
1921619217
if (js_add_slow(ctx, sp))
1921719218
goto exception;
@@ -19232,9 +19233,11 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1923219233
if (likely(JS_VALUE_IS_BOTH_INT(*pv, op2))) {
1923319234
int64_t r;
1923419235
r = (int64_t)JS_VALUE_GET_INT(*pv) + JS_VALUE_GET_INT(op2);
19235-
if (unlikely((int)r != r))
19236-
goto add_loc_slow;
19237-
*pv = JS_NewInt32(ctx, r);
19236+
if (unlikely((int)r != r)) {
19237+
*pv = __JS_NewFloat64(ctx, (double)r);
19238+
} else {
19239+
*pv = JS_NewInt32(ctx, r);
19240+
}
1923819241
sp--;
1923919242
} else if (JS_VALUE_IS_BOTH_FLOAT(*pv, op2)) {
1924019243
*pv = __JS_NewFloat64(ctx, JS_VALUE_GET_FLOAT64(*pv) +
@@ -19254,7 +19257,6 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1925419257
}
1925519258
} else {
1925619259
JSValue ops[2];
19257-
add_loc_slow:
1925819260
/* In case of exception, js_add_slow frees ops[0]
1925919261
and ops[1], so we must duplicate *pv */
1926019262
sf->cur_pc = pc;
@@ -19275,9 +19277,11 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1927519277
if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
1927619278
int64_t r;
1927719279
r = (int64_t)JS_VALUE_GET_INT(op1) - JS_VALUE_GET_INT(op2);
19278-
if (unlikely((int)r != r))
19279-
goto binary_arith_slow;
19280-
sp[-2] = JS_NewInt32(ctx, r);
19280+
if (unlikely((int)r != r)) {
19281+
sp[-2] = __JS_NewFloat64(ctx, (double)r);
19282+
} else {
19283+
sp[-2] = JS_NewInt32(ctx, r);
19284+
}
1928119285
sp--;
1928219286
} else if (JS_VALUE_IS_BOTH_FLOAT(op1, op2)) {
1928319287
sp[-2] = __JS_NewFloat64(ctx, JS_VALUE_GET_FLOAT64(op1) -
@@ -19373,6 +19377,8 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1937319377
op1 = sp[-1];
1937419378
tag = JS_VALUE_GET_TAG(op1);
1937519379
if (tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag)) {
19380+
} else if (tag == JS_TAG_NULL || tag == JS_TAG_BOOL) {
19381+
sp[-1] = JS_NewInt32(ctx, JS_VALUE_GET_INT(op1));
1937619382
} else {
1937719383
sf->cur_pc = pc;
1937819384
if (js_unary_arith_slow(ctx, sp, opcode))
@@ -19388,7 +19394,9 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1938819394
double d;
1938919395
op1 = sp[-1];
1939019396
tag = JS_VALUE_GET_TAG(op1);
19391-
if (tag == JS_TAG_INT) {
19397+
if (tag == JS_TAG_INT ||
19398+
tag == JS_TAG_BOOL ||
19399+
tag == JS_TAG_NULL) {
1939219400
val = JS_VALUE_GET_INT(op1);
1939319401
/* Note: -0 cannot be expressed as integer */
1939419402
if (unlikely(val == 0)) {

0 commit comments

Comments
 (0)