Skip to content

Commit 3c91418

Browse files
committed
WIP bigint bitwise bif
Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent eb7b64b commit 3c91418

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/libAtomVM/bif.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,13 @@ term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1)
14291429
}
14301430

14311431
typedef int64_t (*bitwise_op)(int64_t a, int64_t b);
1432+
typedef size_t (*bitwise_big_op)(
1433+
const intn_digit_t m[], size_t m_len, intn_integer_sign_t m_sign,
1434+
const intn_digit_t n[], size_t n_len, intn_integer_sign_t n_sign,
1435+
intn_digit_t out[], intn_integer_sign_t *out_sign);
14321436

14331437
static inline term bitwise_helper(
1434-
Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op)
1438+
Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op, bitwise_big_op big_op)
14351439
{
14361440
if (LIKELY(term_is_any_integer(arg1) && term_is_any_integer(arg2))) {
14371441
size_t arg1_size = term_is_integer(arg1) ? 0 : term_boxed_size(arg1);
@@ -1447,7 +1451,21 @@ static inline term bitwise_helper(
14471451
return make_maybe_boxed_int(ctx, fail_label, live, result);
14481452
#endif
14491453
} else {
1450-
abort();
1454+
intn_digit_t tmp_buf1[INTN_INT64_LEN];
1455+
intn_digit_t tmp_buf2[INTN_INT64_LEN];
1456+
intn_digit_t *m;
1457+
size_t m_len;
1458+
intn_integer_sign_t m_sign;
1459+
intn_digit_t *n;
1460+
size_t n_len;
1461+
intn_integer_sign_t n_sign;
1462+
args_to_bigint(arg1, arg2, tmp_buf1, tmp_buf2, &m, &m_len, &m_sign, &n, &n_len, &n_sign);
1463+
1464+
intn_digit_t bigres[INTN_MAX_RES_LEN];
1465+
intn_integer_sign_t bigres_sign;
1466+
size_t bigres_len = big_op(m, m_len, m_sign, n, n_len, n_sign, bigres, &bigres_sign);
1467+
1468+
return make_bigint(ctx, fail_label, live, bigres, bigres_len, bigres_sign);
14511469
}
14521470
} else {
14531471
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
@@ -1464,7 +1482,7 @@ term bif_erlang_bor_2(Context *ctx, uint32_t fail_label, int live, term arg1, te
14641482
if (LIKELY(term_is_integer(arg1) && term_is_integer(arg2))) {
14651483
return arg1 | arg2;
14661484
} else {
1467-
return bitwise_helper(ctx, fail_label, live, arg1, arg2, bor);
1485+
return bitwise_helper(ctx, fail_label, live, arg1, arg2, bor, intn_bormn);
14681486
}
14691487
}
14701488

@@ -1478,7 +1496,7 @@ term bif_erlang_band_2(Context *ctx, uint32_t fail_label, int live, term arg1, t
14781496
if (LIKELY(term_is_integer(arg1) && term_is_integer(arg2))) {
14791497
return arg1 & arg2;
14801498
} else {
1481-
return bitwise_helper(ctx, fail_label, live, arg1, arg2, band);
1499+
return bitwise_helper(ctx, fail_label, live, arg1, arg2, band, NULL);
14821500
}
14831501
}
14841502

@@ -1492,7 +1510,7 @@ term bif_erlang_bxor_2(Context *ctx, uint32_t fail_label, int live, term arg1, t
14921510
if (LIKELY(term_is_integer(arg1) && term_is_integer(arg2))) {
14931511
return (arg1 ^ arg2) | TERM_INTEGER_TAG;
14941512
} else {
1495-
return bitwise_helper(ctx, fail_label, live, arg1, arg2, bxor);
1513+
return bitwise_helper(ctx, fail_label, live, arg1, arg2, bxor, NULL);
14961514
}
14971515
}
14981516

0 commit comments

Comments
 (0)