Skip to content

Commit 9ae5125

Browse files
committed
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.
1 parent 5239a8a commit 9ae5125

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

py/objint.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
311311
}
312312

313313
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
314-
// SMALL_INT accepts only signed numbers, of one bit less size
315-
// then word size, which totals 2 bits less for unsigned numbers.
316-
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
314+
// SMALL_INT accepts only signed numbers, so make sure the input
315+
// value fits completely in the small-int positive range.
316+
if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
317317
return MP_OBJ_NEW_SMALL_INT(value);
318318
}
319319
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));

py/objint_longlong.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ mp_obj_t mp_obj_new_int(mp_int_t value) {
223223
}
224224

225225
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
226-
// SMALL_INT accepts only signed numbers, of one bit less size
227-
// than word size, which totals 2 bits less for unsigned numbers.
228-
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
226+
// SMALL_INT accepts only signed numbers, so make sure the input
227+
// value fits completely in the small-int positive range.
228+
if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
229229
return MP_OBJ_NEW_SMALL_INT(value);
230230
}
231231
return mp_obj_new_int_from_ll(value);

py/objint_mpz.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
357357
}
358358

359359
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
360-
// SMALL_INT accepts only signed numbers, of one bit less size
361-
// than word size, which totals 2 bits less for unsigned numbers.
362-
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
360+
// SMALL_INT accepts only signed numbers, so make sure the input
361+
// value fits completely in the small-int positive range.
362+
if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
363363
return MP_OBJ_NEW_SMALL_INT(value);
364364
}
365365
return mp_obj_new_int_from_ull(value);

0 commit comments

Comments
 (0)