Skip to content

Commit 003255a

Browse files
committed
WIP: refactor bitwise ops
Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 515ec03 commit 003255a

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/libAtomVM/bif.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,23 +1430,28 @@ term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1)
14301430

14311431
typedef int64_t (*bitwise_op)(int64_t a, int64_t b);
14321432

1433-
static inline term bitwise_helper(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op)
1433+
static inline term bitwise_helper(
1434+
Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op)
14341435
{
1435-
UNUSED(live);
1436+
if (LIKELY(term_is_any_integer(arg1) && term_is_any_integer(arg2))) {
1437+
size_t arg1_size = term_is_integer(arg1) ? 0 : term_boxed_size(arg1);
1438+
size_t arg2_size = term_is_integer(arg2) ? 0 : term_boxed_size(arg2);
1439+
if (MAX(arg1_size, arg2_size) <= BOXED_TERMS_REQUIRED_FOR_INT64) {
1440+
int64_t a = term_maybe_unbox_int64(arg1);
1441+
int64_t b = term_maybe_unbox_int64(arg2);
1442+
int64_t result = op(a, b);
14361443

1437-
if (UNLIKELY(!term_is_any_integer(arg1) || !term_is_any_integer(arg2))) {
1444+
#if BOXED_TERMS_REQUIRED_FOR_INT64 > 1
1445+
return make_maybe_boxed_int64(ctx, fail_label, live, result);
1446+
#else
1447+
return make_maybe_boxed_int(ctx, fail_label, live, result);
1448+
#endif
1449+
} else {
1450+
abort();
1451+
}
1452+
} else {
14381453
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
14391454
}
1440-
1441-
int64_t a = term_maybe_unbox_int64(arg1);
1442-
int64_t b = term_maybe_unbox_int64(arg2);
1443-
int64_t result = op(a, b);
1444-
1445-
#if BOXED_TERMS_REQUIRED_FOR_INT64 > 1
1446-
return make_maybe_boxed_int64(ctx, fail_label, live, result);
1447-
#else
1448-
return make_maybe_boxed_int(ctx, fail_label, live, result);
1449-
#endif
14501455
}
14511456

14521457
static inline int64_t bor(int64_t a, int64_t b)

0 commit comments

Comments
 (0)