diff --git a/py/obj.c b/py/obj.c index 5e01198b6f..ad8ee80412 100644 --- a/py/obj.c +++ b/py/obj.c @@ -336,6 +336,35 @@ bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) { return true; } +int64_t mp_obj_get_ll_int(mp_obj_t arg) { + if (arg == mp_const_false) { + return 0; + } else if (arg == mp_const_true) { + return 1; + } else if (mp_obj_is_small_int(arg)) { + return MP_OBJ_SMALL_INT_VALUE(arg); + } else if (mp_obj_is_exact_type(arg, &mp_type_int)) { + #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG + mp_obj_int_t *a = arg; + return a->val; + #elif MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ + mp_obj_int_t *a = MP_OBJ_TO_PTR(arg); + int len = a->mpz.len; + uint64_t res = 0; + for (int i = len - 1; i >= 0; --i) { + res = (res << MPZ_DIG_SIZE) + a->mpz.dig[i]; + } + if (a->mpz.neg) { + return -res; + } + return res; + #endif + } else { + mp_raise_ValueError(MP_ERROR_TEXT("expected an integer")); + } + return 0; +} + #if MICROPY_PY_BUILTINS_FLOAT bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) { mp_float_t val; diff --git a/py/obj.h b/py/obj.h index c7b7db0c38..1e4ebf44b9 100644 --- a/py/obj.h +++ b/py/obj.h @@ -1016,6 +1016,7 @@ static inline bool mp_obj_is_integer(mp_const_obj_t o) { return mp_obj_is_int(o) || mp_obj_is_bool(o); } +int64_t mp_obj_get_ll_int(mp_obj_t arg); mp_int_t mp_obj_get_int(mp_const_obj_t arg); mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg); bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);