Arbitrary precision LibBF library port to MicroPython
Posted: Sat Jun 15, 2019 3:24 pm
Hi,
I needed better than single-precision arithmetic support on the STM32F4 microcontrollers.
MPFR and mpdecimal are the silverbacks in MP arithmetic land but are too big for my target.
I tried to port MPMATH, but gave up trying to port that to MicroPython (kept running into MemoryError forever
just to even import the module; it also has too many functions that I don't need).
Then I wrote a Python version based upon a prior Python work -- this works OK , but with my naive first-principles math.
function implementations, runs very slow -- the upper bound on a natural logarithm is around 4 seconds on my
STM32F407VET board running at 168MHz with a precision of 27. Native code emitters helped reduce this time by ~10%.
Clearly, something better was needed, and this was Fabrice Bellard's LibBF
My port is here. I got it working in the Unix port (on WSL Ubuntu) and on the STM32F407VET board. Log with precision set
to 27 now runs in around than a fifth of a second. With precision = 500, time taken is about 0.5 sec.
Precision set to more than 500 either gives a "MemoryError: memory allocation failed" error or the board gets reset.
Any comments or suggestions are welcome.
PP
I needed better than single-precision arithmetic support on the STM32F4 microcontrollers.
MPFR and mpdecimal are the silverbacks in MP arithmetic land but are too big for my target.
I tried to port MPMATH, but gave up trying to port that to MicroPython (kept running into MemoryError forever
just to even import the module; it also has too many functions that I don't need).
Then I wrote a Python version based upon a prior Python work -- this works OK , but with my naive first-principles math.
function implementations, runs very slow -- the upper bound on a natural logarithm is around 4 seconds on my
STM32F407VET board running at 168MHz with a precision of 27. Native code emitters helped reduce this time by ~10%.
Clearly, something better was needed, and this was Fabrice Bellard's LibBF
My port is here. I got it working in the Unix port (on WSL Ubuntu) and on the STM32F407VET board. Log with precision set
to 27 now runs in around than a fifth of a second. With precision = 500, time taken is about 0.5 sec.
Precision set to more than 500 either gives a "MemoryError: memory allocation failed" error or the board gets reset.
Any comments or suggestions are welcome.
PP