py\obj.c: Get 64-bit integer arg.

Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
pull/8089/head
IhorNehrutsa 2021-12-15 21:29:51 +02:00
rodzic 4a2e510a87
commit efbdedf516
2 zmienionych plików z 30 dodań i 0 usunięć

Wyświetl plik

@ -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;

Wyświetl plik

@ -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);