Page 1 of 1

Arbitrary precision LibBF library port to MicroPython

Posted: Sat Jun 15, 2019 3:24 pm
by ProudPagan
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 :D

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

Re: Arbitrary precision LibBF library port to MicroPython

Posted: Sun Jun 16, 2019 7:48 am
by pythoncoder
Impressive! Out of interest can you envisage applications for running this on microcontrollers?

Re: Arbitrary precision LibBF library port to MicroPython

Posted: Sun Jun 16, 2019 2:36 pm
by ProudPagan
My own use is to build a calculator that specializes in calculating lunar phases - probably double precision (53 bits) would be sufficient there, but higher precision can't hurt. I think it would eventually morph into a homebrew DM42.

Other uses of higher-than-single-precision could be:
- accurate GPS positioning e.g., in trackers
- navigation instrumentation
- IEEE PTP timekeeping (typically h/w clock precision is 64-bit).


Thanks!

Re: Arbitrary precision LibBF library port to MicroPython

Posted: Mon Jun 17, 2019 8:45 am
by pythoncoder
Interesting. I have a lunar clock which I built many years ago. It has a servo-driven pointer indicating the lunar phase. It can display and predict sun and moon rise and set times and lunar quarters. It uses double precision FP (in C).

Note that the Pyboard D SF6W supports DP in hardware. Other ports can be compiled to support DP in software.

For astronomy I recommend "Astronomy on the personal computer" by Montenbruck and Pfleger. It details the calculations involved (in C++). As I understand it all their work uses double variables even for the really hard stuff like predicting the path of solar eclipses. Some of that stuff is pretty hardcore...

I don't know about your other applications but evidently there are requirements for >DP support as that calculator suggests. And it uses RPN: the proper way to do the job. I use an HP35s for the same reason. ;)