Skip to content

WIP: BIFs: bnot: add support to boxed integers #1743

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

Draft
wants to merge 1 commit into
base: release-0.6
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.6.7] - Unreleased

### Fixed

- bnot not supports 64-bit integers

## [0.6.6] - 2025-06-23

### Added
Expand Down
60 changes: 59 additions & 1 deletion src/libAtomVM/bif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,64 @@ term bif_erlang_bsr_2(Context *ctx, uint32_t fail_label, int live, term arg1, te
return bitshift_helper(ctx, fail_label, live, arg1, arg2, bsr);
}

static term bnot_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, term arg1)
{
if (term_is_boxed_integer(arg1)) {
switch (term_boxed_size(arg1)) {
case 0:
//BUG
AVM_ABORT();

case 1: {
avm_int_t val = term_unbox_int(arg1);
if (val >= 0) {
return arg1;
}

if (val == AVM_INT_MIN) {
#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2
return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) val));

#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1
TRACE("overflow: val: " AVM_INT_FMT "\n", val);
RAISE_ERROR_BIF(fail_label, OVERFLOW_ATOM);

#else
#error "Unsupported configuration."
#endif

} else {
return make_boxed_int(ctx, fail_label, live, -val);
}
}

#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2
case 2: {
avm_int64_t val = term_unbox_int64(arg1);
if (val >= 0) {
return arg1;
}

if (val == INT64_MIN) {
TRACE("overflow: val:" AVM_INT64_FMT "\n", val);
RAISE_ERROR_BIF(fail_label, OVERFLOW_ATOM);

} else {
return make_boxed_int64(ctx, fail_label, live, -val);
}
}
#endif
default:
RAISE_ERROR_BIF(fail_label, OVERFLOW_ATOM);
}
} else {
TRACE("error: arg1: 0x%lx\n", arg1);
RAISE_ERROR_BIF(fail_label, BADARG_ATOM);
}
}



term bif_erlang_bnot_1(Context *ctx, uint32_t fail_label, int live, term arg1)
{
UNUSED(live);
Expand All @@ -1380,7 +1438,7 @@ term bif_erlang_bnot_1(Context *ctx, uint32_t fail_label, int live, term arg1)
return ~arg1 | TERM_INTEGER_TAG;

} else {
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
return bnot_boxed_helper(ctx, fail_label, live, arg1);
}
}

Expand Down
Loading