Long numbers in frozen modules

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Pastor
Posts: 7
Joined: Thu Nov 02, 2017 10:16 am

Long numbers in frozen modules

Post by Pastor » Tue Jan 16, 2018 8:18 am

Hello,
I am currently working with frozen modules of Micropython, in particular, I am freezing some python drivers in the Micropython environment.
Yesterday I wanted to start working with long numbers, putting:
- in the configuration header:

Code: Select all

#define MICROPY_LONGINT_IMPL                        ( MICROPY_LONGINT_IMPL_LONGLONG )
- as option for mpy-tool.py script call:

Code: Select all

-mlongint-impl=longlong
I encountered a problem trying to freeze a python code containing long numbers (apparently, int numbers must be written within 28 bits.
Looking at the mpy-tool.py script, I noticed that the longlong option could not be implemented.
Do you have any idea about how to freeze long numbers?

Thank you

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Long numbers in frozen modules

Post by pythoncoder » Tue Jan 16, 2018 10:07 am

I'm not sure if I understand your problem. Python (and MicroPython) support integers of arbitrary precision. In MicroPython integers which can be represented in 30 bits are stored as a single machine word. Larger integers are stored in as many machine words as required. This process is transparent to the user, and works with frozen bytecode. I created a file `bignum.py` consisting of

Code: Select all

a = 1099511627776  # 2**40
After compiling and flashing to a Pyboard, I did the following

Code: Select all

MicroPython v1.9.3-240-ga275cb0 on 2018-01-16; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> 
>>> import bignum
>>> bignum.a
1099511627776
>>> bignum.a - 2**40
0
>>> 
I made no changes to the standard build environment.
Peter Hinch
Index to my micropython libraries.

Pastor
Posts: 7
Joined: Thu Nov 02, 2017 10:16 am

Re: Long numbers in frozen modules

Post by Pastor » Tue Jan 16, 2018 11:24 am

I tryed to freeze the same number, but the error persists. Maybe, I'm missing something in the freezing process.
To freeze a component, I call the mpy-cross.exe executable (compiled for windows with cygwin tool) on the .py file:

Code: Select all

mpy-cross.exe test.py
The output .mpy file seems to be ok. Then, I call the mpy-tool.py as follows:

Code: Select all

mpy-tool.py -f -q qstrdefs.preprocessed.h -mlongint-impl=longlong test.mpy > frozen_modules.c
Here I get the following error message:

Code: Select all

error while freezing qstr(str='test.py', qstr_esc='test_dot_py', qstr_id='MP_QSTR_test_dot_py'): freezing int to long-long is not implemented
As you said, the limit is 30 bits (not 28), indeed the number 0x3fffffff works, while 0x40000000 don't.
In the mpy-tool.py file, there is the code (line 303):

Code: Select all

elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
	# TODO
And this makes me think that the longlong option is not implemented for frozen codes (at least using the 'longlong' option).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Long numbers in frozen modules

Post by pythoncoder » Tue Jan 16, 2018 11:31 am

To freeze a module put it in ports/stm32/modules. Then build the firmware and flash it to the board. Build instructions may be found here.
Peter Hinch
Index to my micropython libraries.

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

Re: Long numbers in frozen modules

Post by Roberthh » Tue Jan 16, 2018 11:36 am

Your approach looks too complicated for me. If you want to freeze a Python module, just put that into the "modules" subdirectory of the respective port (stm32, esp8266, esp32) and create a new image by just issueing the command "make". The Makefile will do all the magic you need. And, as Peter said, Micropython supports arbitrary long numbers by default.
But maybe Peter and me do not understand your intention.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Long numbers in frozen modules

Post by pfalcon » Tue Jan 16, 2018 4:32 pm

Pastor, you're right, MICROPY_LONGINT_IMPL_LONGLONG support not implemented for frozen bytecode. Just hit the same issue for Zephyr port. Patches welcome ;-).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Pastor
Posts: 7
Joined: Thu Nov 02, 2017 10:16 am

Re: Long numbers in frozen modules

Post by Pastor » Tue Jan 16, 2018 4:49 pm

First of all, thank you all for replies!

I need to use this complicated approach because I'm trying to integrate micropython in an already existing environment. In this way, I can't use those makefiles, but I need to execute "manually" each Makefile step.

@pfalcon mmm ok. I would like to add it, but honestly I don't know how to do it :oops:

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Long numbers in frozen modules

Post by pfalcon » Tue Jan 16, 2018 11:04 pm

Hey, me too! All that stuff was done by @dpgeorge, and I never looked into it. Amazing feeling, isn't it? All people envy us - we're about to learn something new, while they just go thru their boring routine.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply