Coversion of a mp_obj to uint64_t

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Coversion of a mp_obj to uint64_t

Post by Roberthh » Fri Jan 05, 2018 11:16 am

Is there a function of macro to convert a MP object holding a >32 bit integer to a C uint64_t?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Coversion of a mp_obj to uint64_t

Post by stijn » Fri Jan 05, 2018 1:18 pm

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Coversion of a mp_obj to uint64_t

Post by dhylands » Fri Jan 05, 2018 3:21 pm

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).

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Coversion of a mp_obj to uint64_t

Post by Roberthh » Fri Jan 05, 2018 3:52 pm

@stijn, @dhylands, thank you very much for your hints,

Post Reply