Skip to content
Snippets Groups Projects
Commit 9ae51257 authored by Damien George's avatar Damien George
Browse files

py: Use MP_SMALL_INT_POSITIVE_MASK to check if uint fits in a small int.

Using the original WORD_MSBIT_HIGH-logic resulted in errors when the
object model is not REPR_A or REPR_C.
parent 5239a8a8
No related branches found
No related tags found
No related merge requests found
...@@ -311,9 +311,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) { ...@@ -311,9 +311,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
} }
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
// SMALL_INT accepts only signed numbers, of one bit less size // SMALL_INT accepts only signed numbers, so make sure the input
// then word size, which totals 2 bits less for unsigned numbers. // value fits completely in the small-int positive range.
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) { if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
return MP_OBJ_NEW_SMALL_INT(value); return MP_OBJ_NEW_SMALL_INT(value);
} }
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow")); nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
......
...@@ -223,9 +223,9 @@ mp_obj_t mp_obj_new_int(mp_int_t value) { ...@@ -223,9 +223,9 @@ mp_obj_t mp_obj_new_int(mp_int_t value) {
} }
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
// SMALL_INT accepts only signed numbers, of one bit less size // SMALL_INT accepts only signed numbers, so make sure the input
// than word size, which totals 2 bits less for unsigned numbers. // value fits completely in the small-int positive range.
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) { if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
return MP_OBJ_NEW_SMALL_INT(value); return MP_OBJ_NEW_SMALL_INT(value);
} }
return mp_obj_new_int_from_ll(value); return mp_obj_new_int_from_ll(value);
......
...@@ -357,9 +357,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) { ...@@ -357,9 +357,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
} }
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
// SMALL_INT accepts only signed numbers, of one bit less size // SMALL_INT accepts only signed numbers, so make sure the input
// than word size, which totals 2 bits less for unsigned numbers. // value fits completely in the small-int positive range.
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) { if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
return MP_OBJ_NEW_SMALL_INT(value); return MP_OBJ_NEW_SMALL_INT(value);
} }
return mp_obj_new_int_from_ull(value); return mp_obj_new_int_from_ull(value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment