Skip to content

Commit 9a184e0

Browse files
committed
Merge pull request #1922 from bettio/bigint-clarifications
Add clarifications to bigint code Make existing code more understandable These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents f657254 + 8040db1 commit 9a184e0

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

doc/src/programmers-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ The following Erlang type specification enumerates this type:
740740
Erlang/OTP uses the Christian epoch to count time units from year 0 in the Gregorian calendar. The, for example, the value 0 in Gregorian seconds represents the date Jan 1, year 0, and midnight (UTC), or in Erlang terms, `{{0, 1, 1}, {0, 0, 0}}`.
741741

742742
```{attention}
743-
AtomVM is currently limited to representing integers in at most 64 bits, with one bit representing the sign bit.
743+
AtomVM is currently limited to representing time in at most 64 bits, with one bit representing the sign bit.
744744
However, even with this limitation, AtomVM is able to resolve microsecond values in the Gregorian calendar for over
745745
292,000 years, likely well past the likely lifetime of an AtomVM application (unless perhaps launched on a deep
746746
space probe).

src/libAtomVM/bif.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ term bif_erlang_fdiv_2(Context *ctx, uint32_t fail_label, int live, term arg1, t
10261026

10271027
static term div_maybe_bigint(Context *ctx, uint32_t fail_label, uint32_t live, term arg1, term arg2)
10281028
{
1029+
// 0 is always normalized to `term_from_int(0)`, so we can do this
10291030
if (UNLIKELY(arg2 == term_from_int(0))) {
10301031
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
10311032
}
@@ -1346,6 +1347,7 @@ term bif_erlang_abs_1(Context *ctx, uint32_t fail_label, int live, term arg1)
13461347

13471348
static term rem_maybe_bigint(Context *ctx, uint32_t fail_label, uint32_t live, term arg1, term arg2)
13481349
{
1350+
// 0 is always normalized to `term_from_int(0)`, so we can do this
13491351
if (UNLIKELY(arg2 == term_from_int(0))) {
13501352
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
13511353
}

src/libAtomVM/nifs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,8 @@ static term nif_erlang_binary_to_atom_1(Context *ctx, int argc, term argv[])
20512051
return result;
20522052
}
20532053

2054+
// This make_bigint version doesn't do any overflow check and it doesn't normalize integers
2055+
// These contraints are ok for parse_integer, since it checks this before calling make_bigint
20542056
static term make_bigint(Context *ctx, const intn_digit_t bigres[], size_t bigres_len, intn_integer_sign_t sign)
20552057
{
20562058
size_t intn_data_size;

src/libAtomVM/opcodesswitch.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,8 @@ static size_t decode_nbits_integer(Context *ctx, const uint8_t *encoded, term *o
15571557
static term large_integer_to_term(Context *ctx, int num_bytes, const uint8_t **encoded)
15581558
{
15591559
const uint8_t *compact_term = *encoded;
1560+
// num_bytes is decoded from a 3 bits value and incremented by 2,
1561+
// meaning that minimum value is 0 and maximum value is 7
15601562
switch (num_bytes) {
15611563
case 0:
15621564
case 1:
@@ -1815,8 +1817,11 @@ static bool maybe_call_native(Context *ctx, atom_index_t module_name, atom_index
18151817
static size_t decode_nbits_integer(Context *ctx, const uint8_t *encoded, term *out_term)
18161818
{
18171819
const uint8_t *new_encoded = encoded;
1818-
unsigned int len;
1820+
uint32_t len;
18191821
DECODE_LITERAL(len, new_encoded);
1822+
// TODO: check this: actually should be enough: len = *(new_encoded)++ >> 4;
1823+
// it seems that likely range is something like from 9 (9 + 0) to 24 (9 + 15)
1824+
// that is 192 bits integer
18201825

18211826
len += 9;
18221827

0 commit comments

Comments
 (0)