Page 1 of 1

Coversion of a mp_obj to uint64_t

Posted: Fri Jan 05, 2018 11:16 am
by Roberthh
Is there a function of macro to convert a MP object holding a >32 bit integer to a C uint64_t?

Re: Coversion of a mp_obj to uint64_t

Posted: Fri Jan 05, 2018 1:18 pm
by stijn
Don't think so, at least last time I checked I didn't find any built-in way covering all cases. And it's also not exactly trivial as it's different for 32bit/64bit builds and depends on how the integer is stored (small int vs mpz etc) and you should deal with overflowing. Start reading here: https://github.com/stinos/micropython-w ... obj.h#L203

Re: Coversion of a mp_obj to uint64_t

Posted: Fri Jan 05, 2018 3:21 pm
by dhylands
You could create your own version by using mp_obj_get_int_truncated (and explicitly masking the result with 0xffffffff) and then calling mp_obj_int_binary_op to right shift by 32 bits and then repeat the mp_obj_get_int_truncated (or mp_obj_get_int_checked).

Re: Coversion of a mp_obj to uint64_t

Posted: Fri Jan 05, 2018 3:52 pm
by Roberthh
@stijn, @dhylands, thank you very much for your hints,