ckormanyos/pi-crunch-metal
computes up to a million digits of
This fascinating, educational and fun project combines the domain of high-performance numerical computing with the raw simplicity of bare-metal embedded microcontroller systems.
ckormanyos/pi-crunch-metal
exihbits the utmost in portability
and is realized primarily through modern, header-only C++14, 17, 20, 23 (and beyond)
template code.
The application software is intended to run out of the box on both PC/host systems as well as selected bare-metal microcontroller targets.
The wide-decimal multiprecision float back end provides the big-number engine for pi-crunch-metal. Computation progress can be displayed on either the console (for PC systems) or on a simple industry-standard LCD character display.
The backbone real-time operating system is taken directly from the OSEK-like OS implemented in Chalandi/OSEK_Raspberry_Pi_Zero
In this variation of the project, up to
Multiplication is the hot-spot of this program. The multiplication implementation uses a combination of school multiplication for low precision and Karatsuba multiplication for medium percision, switching directly over to an FFT multiplication scheme for higher precision.
The microcontroller boots and performs static initialization via self-written
startup code. Hardware setup including clock initialization,
FPU enable, instruction caching, etc. is carried out with self-written
hybrid assembly/C++ code shortly after reaching main()
.
Compact code size is in focus and the entire project easily fits within 40k of program code, with slight variations depending on the optimization settings, compiler and target system selected. The calculation does, however, require ample RAM of about 16 Mbyte.
GNU/GCC gcc-arm-non-eabi
is used for the target system.
Cross development with Win*
and/or *nix
host is
supported. Build tools, the build system and the compilers
are essentially the same as those used in the
real-time-cpp
project. These can also be found within a coherent collection
of toolchains in the
real-time-cpp-toolchains
repository.
In addition to the AGM algorithm mentioned above, a slower
quadratic pi-spigot algorithm of order
The spigot calculation
is slower than the AGM calculation and requires sightly
more time to compute a count of
This repo features a fully-worked-out pi-crunch-metal prototype example project running on a RaspberryPi(R)-Zero. This powerful single-board computer is driven in OS-less, bare-metal mode directly out-of-the-box.
The build system is set up to use GCC, making use of the arm-none-eabi
compiler taken directly from the
real-time-cpp-toolchains
repository.
The default optimization setting is -Os
and the million-and-one
decimal digit pi calculation takes slightly under 30 minutes
on this target system with the above-mentioned compiler.
The hardware setup is pictured in the image below. In this image, the system has successfully completed one full mega-digit pi calculation and is almost done with a second one.
Traditional wire-wrapping techniques connect the pins on a self-made
breakout board to a solderless prototyping breadboard.
Double and quadruple strands of skinny wire are used on the
power and ground pins, as these might typically carry up to
Bit banging is used to implement an all-software SPI-compatible driver which controls a port expander chip. The port expander chip is used to control the pins on an industry-standard LCD character display.
A classic logic AND
-gate converts
The output pin connections from the Rpi-Zero to the logic-AND gate are shown in the table below.
Rpi-Zero PIN | AND-gate PINS | SPI Function |
---|---|---|
GPIO16/H36 | in 10/9, out 8 | CS |
GPIO18/H12 | in 1/2, out 3 | SCK |
GPIO19/H35 | in 4/5, out 6 | MOSI |
The LCD pin connections and the input/output connections of the port expander chip are clearly identifiable in the image. The port expander chip uses hardware addressing and is hard-wired to address 2.