Skip to content

Commit d1e94c4

Browse files
IhorNehrutsaIhorNehrutsa
authored and
IhorNehrutsa
committed
py\obj.c: Get 64-bit integer arg.
Signed-off-by: IhorNehrutsa <[email protected]>
1 parent 4a2e510 commit d1e94c4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

py/obj.c

+30
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,36 @@ bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
336336
return true;
337337
}
338338

339+
int64_t mp_obj_get_ll_int(mp_obj_t arg_in) {
340+
if (arg_in == mp_const_false) {
341+
return 0;
342+
} else if (arg_in == mp_const_true) {
343+
return 1;
344+
} else if (mp_obj_is_small_int(arg_in)) {
345+
return MP_OBJ_SMALL_INT_VALUE(arg_in);
346+
} else if (mp_obj_is_exact_type(arg_in, &mp_type_int)) {
347+
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
348+
mp_obj_int_t *arg = arg_in;
349+
return arg->val;
350+
#elif MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
351+
mp_obj_int_t *arg = arg_in;
352+
int len = arg->mpz.len;
353+
uint64_t res = 0;
354+
for (int i = len - 1; i >= 0; --i) {
355+
res = (res << MPZ_DIG_SIZE) + arg->mpz.dig[i];
356+
}
357+
if (arg->mpz.neg) {
358+
return -res;
359+
}
360+
return res;
361+
#endif
362+
} else {
363+
mp_raise_ValueError(MP_ERROR_TEXT("expected an integer"));
364+
return 0;
365+
}
366+
return 0;
367+
}
368+
339369
#if MICROPY_PY_BUILTINS_FLOAT
340370
bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
341371
mp_float_t val;

py/obj.h

+1
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ static inline bool mp_obj_is_integer(mp_const_obj_t o) {
10161016
return mp_obj_is_int(o) || mp_obj_is_bool(o);
10171017
}
10181018

1019+
int64_t mp_obj_get_ll_int(mp_obj_t arg_in);
10191020
mp_int_t mp_obj_get_int(mp_const_obj_t arg);
10201021
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
10211022
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);

0 commit comments

Comments
 (0)