Skip to content

Remove non-standard BigInt methods #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 0 additions & 142 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -46605,135 +46605,6 @@ static JSValue js_bigint_valueOf(JSContext *ctx, JSValueConst this_val,
return js_thisBigIntValue(ctx, this_val);
}

static JSValue js_bigint_div(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
bf_t a_s, b_s, *a, *b, *r, *q;
int status;
JSValue q_val, r_val;

q_val = JS_NewBigInt(ctx);
if (JS_IsException(q_val))
return JS_EXCEPTION;
r_val = JS_NewBigInt(ctx);
if (JS_IsException(r_val))
goto fail;
b = NULL;
a = JS_ToBigInt(ctx, &a_s, argv[0]);
if (!a)
goto fail;
b = JS_ToBigInt(ctx, &b_s, argv[1]);
if (!b) {
JS_FreeBigInt(ctx, a, &a_s);
goto fail;
}
q = JS_GetBigInt(q_val);
r = JS_GetBigInt(r_val);
status = bf_divrem(q, r, a, b, BF_PREC_INF, BF_RNDZ, magic & 0xf);
JS_FreeBigInt(ctx, a, &a_s);
JS_FreeBigInt(ctx, b, &b_s);
if (unlikely(status)) {
throw_bf_exception(ctx, status);
goto fail;
}
q_val = JS_CompactBigInt(ctx, q_val);
if (magic & 0x10) {
JSValue ret;
ret = JS_NewArray(ctx);
if (JS_IsException(ret))
goto fail;
JS_SetPropertyUint32(ctx, ret, 0, q_val);
JS_SetPropertyUint32(ctx, ret, 1, JS_CompactBigInt(ctx, r_val));
return ret;
} else {
JS_FreeValue(ctx, r_val);
return q_val;
}
fail:
JS_FreeValue(ctx, q_val);
JS_FreeValue(ctx, r_val);
return JS_EXCEPTION;
}

static JSValue js_bigint_sqrt(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
bf_t a_s, *a, *r, *rem;
int status;
JSValue r_val, rem_val;

r_val = JS_NewBigInt(ctx);
if (JS_IsException(r_val))
return JS_EXCEPTION;
rem_val = JS_NewBigInt(ctx);
if (JS_IsException(rem_val))
return JS_EXCEPTION;
r = JS_GetBigInt(r_val);
rem = JS_GetBigInt(rem_val);

a = JS_ToBigInt(ctx, &a_s, argv[0]);
if (!a)
goto fail;
status = bf_sqrtrem(r, rem, a);
JS_FreeBigInt(ctx, a, &a_s);
if (unlikely(status & ~BF_ST_INEXACT)) {
throw_bf_exception(ctx, status);
goto fail;
}
r_val = JS_CompactBigInt(ctx, r_val);
if (magic) {
JSValue ret;
ret = JS_NewArray(ctx);
if (JS_IsException(ret))
goto fail;
JS_SetPropertyUint32(ctx, ret, 0, r_val);
JS_SetPropertyUint32(ctx, ret, 1, JS_CompactBigInt(ctx, rem_val));
return ret;
} else {
JS_FreeValue(ctx, rem_val);
return r_val;
}
fail:
JS_FreeValue(ctx, r_val);
JS_FreeValue(ctx, rem_val);
return JS_EXCEPTION;
}

static JSValue js_bigint_op1(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv,
int magic)
{
bf_t a_s, *a;
int64_t res;

a = JS_ToBigInt(ctx, &a_s, argv[0]);
if (!a)
return JS_EXCEPTION;
switch(magic) {
case 0: /* floorLog2 */
if (a->sign || a->expn <= 0) {
res = -1;
} else {
res = a->expn - 1;
}
break;
case 1: /* ctz */
if (bf_is_zero(a)) {
res = -1;
} else {
res = bf_get_exp_min(a);
}
break;
default:
abort();
}
JS_FreeBigInt(ctx, a, &a_s);
return JS_NewBigInt64(ctx, res);
}

static JSValue js_bigint_asUintN(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv, int asIntN)
Expand Down Expand Up @@ -46777,19 +46648,6 @@ static JSValue js_bigint_asUintN(JSContext *ctx,
static const JSCFunctionListEntry js_bigint_funcs[] = {
JS_CFUNC_MAGIC_DEF("asUintN", 2, js_bigint_asUintN, 0 ),
JS_CFUNC_MAGIC_DEF("asIntN", 2, js_bigint_asUintN, 1 ),
/* QuickJS extensions */
JS_CFUNC_MAGIC_DEF("tdiv", 2, js_bigint_div, BF_RNDZ ),
JS_CFUNC_MAGIC_DEF("fdiv", 2, js_bigint_div, BF_RNDD ),
JS_CFUNC_MAGIC_DEF("cdiv", 2, js_bigint_div, BF_RNDU ),
JS_CFUNC_MAGIC_DEF("ediv", 2, js_bigint_div, BF_DIVREM_EUCLIDIAN ),
JS_CFUNC_MAGIC_DEF("tdivrem", 2, js_bigint_div, BF_RNDZ | 0x10 ),
JS_CFUNC_MAGIC_DEF("fdivrem", 2, js_bigint_div, BF_RNDD | 0x10 ),
JS_CFUNC_MAGIC_DEF("cdivrem", 2, js_bigint_div, BF_RNDU | 0x10 ),
JS_CFUNC_MAGIC_DEF("edivrem", 2, js_bigint_div, BF_DIVREM_EUCLIDIAN | 0x10 ),
JS_CFUNC_MAGIC_DEF("sqrt", 1, js_bigint_sqrt, 0 ),
JS_CFUNC_MAGIC_DEF("sqrtrem", 1, js_bigint_sqrt, 1 ),
JS_CFUNC_MAGIC_DEF("floorLog2", 1, js_bigint_op1, 0 ),
JS_CFUNC_MAGIC_DEF("ctz", 1, js_bigint_op1, 1 ),
};

static const JSCFunctionListEntry js_bigint_proto_funcs[] = {
Expand Down
38 changes: 0 additions & 38 deletions tests/test_bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,43 +110,5 @@ function test_bigint2()
assertThrows(SyntaxError, () => { BigInt(" 123 r") } );
}

function test_divrem(div1, a, b, q)
{
var div, divrem, t;
div = BigInt[div1];
divrem = BigInt[div1 + "rem"];
assert(div(a, b) == q);
t = divrem(a, b);
assert(t[0] == q);
assert(a == b * q + t[1]);
}

function test_idiv1(div, a, b, r)
{
test_divrem(div, a, b, r[0]);
test_divrem(div, -a, b, r[1]);
test_divrem(div, a, -b, r[2]);
test_divrem(div, -a, -b, r[3]);
}

/* QuickJS BigInt extensions */
function test_bigint_ext()
{
var r;
assert(BigInt.floorLog2(0n) === -1n);
assert(BigInt.floorLog2(7n) === 2n);

assert(BigInt.sqrt(0xffffffc000000000000000n) === 17592185913343n);
r = BigInt.sqrtrem(0xffffffc000000000000000n);
assert(r[0] === 17592185913343n);
assert(r[1] === 35167191957503n);

test_idiv1("tdiv", 3n, 2n, [1n, -1n, -1n, 1n]);
test_idiv1("fdiv", 3n, 2n, [1n, -2n, -2n, 1n]);
test_idiv1("cdiv", 3n, 2n, [2n, -1n, -1n, 2n]);
test_idiv1("ediv", 3n, 2n, [1n, -2n, -1n, 2n]);
}

test_bigint1();
test_bigint2();
test_bigint_ext();