Skip to content

.from_bytes() implementation for long MPZ ints #2791

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

Closed
wants to merge 7 commits into from
Closed
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
31 changes: 31 additions & 0 deletions py/mpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,37 @@ mp_uint_t mpz_set_from_str(mpz_t *z, const char *str, mp_uint_t len, bool neg, m
return cur - str;
}

void mpz_set_from_bytes(mpz_t *z, bool big_endian, mp_uint_t len, const byte *buf) {
int delta = 1;
if (big_endian) {
buf += len - 1;
delta = -1;
}

mpz_need_dig(z, (len * 8 + DIG_SIZE - 1) / DIG_SIZE);

mpz_dig_t d = 0;
int num_bits = 0;
z->neg = 0;
z->len = 0;
while (len) {
while (len && num_bits < DIG_SIZE) {
d |= *buf << num_bits;
num_bits += 8;
buf += delta;
len--;
}
z->dig[z->len++] = d & DIG_MASK;
// Need this #if because it's C undefined behavior to do: uint32_t >> 32
#if DIG_SIZE != 8 && DIG_SIZE != 16 && DIG_SIZE != 32
d >>= DIG_SIZE;
#else
d = 0;
#endif
num_bits -= DIG_SIZE;
}
}

bool mpz_is_zero(const mpz_t *z) {
return z->len == 0;
}
Expand Down
1 change: 1 addition & 0 deletions py/mpz.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void mpz_set_from_ll(mpz_t *z, long long i, bool is_signed);
void mpz_set_from_float(mpz_t *z, mp_float_t src);
#endif
mp_uint_t mpz_set_from_str(mpz_t *z, const char *str, mp_uint_t len, bool neg, mp_uint_t base);
void mpz_set_from_bytes(mpz_t *z, bool big_endian, mp_uint_t len, const byte *buf);

bool mpz_is_zero(const mpz_t *z);
int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
Expand Down
31 changes: 19 additions & 12 deletions py/objint.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,26 +374,33 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_

// this is a classmethod
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
// TODO: Support long ints
// TODO: Support byteorder param
// TODO: Support signed param (assumes signed=False at the moment)
(void)n_args;

if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
mp_not_implemented("");
}

// get the buffer info
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);

// convert the bytes to an integer
mp_uint_t value = 0;
for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
value = (value << 8) | *buf;
}
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
// If result guaranteedly fits in small int, use that
if (!MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
} else
#endif
{
const byte* buf = (const byte*)bufinfo.buf;
int delta = 1;
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
buf += bufinfo.len - 1;
delta = -1;
}

return mp_obj_new_int_from_uint(value);
mp_uint_t value = 0;
for (; bufinfo.len--; buf += delta) {
value = (value << 8) | *buf;
}
return mp_obj_new_int_from_uint(value);
}
}

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
Expand Down
1 change: 1 addition & 0 deletions py/objint.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co
char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma);
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf);
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf);
int mp_obj_int_sign(mp_obj_t self_in);
mp_obj_t mp_obj_int_abs(mp_obj_t self_in);
Expand Down
4 changes: 4 additions & 0 deletions py/objint_longlong.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
#endif

mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
mp_not_implemented("");
}

void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
mp_obj_int_t *self = self_in;
Expand Down
6 changes: 6 additions & 0 deletions py/objint_mpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size,
return str;
}

mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
mp_obj_int_t *o = mp_obj_int_new_mpz();
mpz_set_from_bytes(&o->mpz, big_endian, len, buf);
return MP_OBJ_FROM_PTR(o);
}

void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Expand Down
7 changes: 7 additions & 0 deletions tests/basics/int_bytes_long.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
b = bytes(range(20))

il = int.from_bytes(b, "little")
ib = int.from_bytes(b, "big")
print(il)
print(ib)
print(il.to_bytes(20, "little"))
5 changes: 0 additions & 5 deletions tests/basics/int_bytes_notimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@
print((10).to_bytes(1, "big"))
except Exception as e:
print(type(e))

try:
print(int.from_bytes(b"\0", "big"))
except Exception as e:
print(type(e))
1 change: 0 additions & 1 deletion tests/basics/int_bytes_notimpl.py.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<class 'NotImplementedError'>
<class 'NotImplementedError'>
8 changes: 8 additions & 0 deletions tests/micropython/heapalloc_int_from_bytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Test that int.from_bytes() for small number of bytes generates
# small int.
import micropython

micropython.heap_lock()
print(int.from_bytes(b"1", "little"))
print(int.from_bytes(b"12", "little"))
micropython.heap_unlock()
2 changes: 2 additions & 0 deletions tests/micropython/heapalloc_int_from_bytes.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
49
12849
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy