October 4th, 2024

Fixed-point arithmetic as a replacement for soft floats

Fixed-point arithmetic offers advantages over floating-point in embedded systems, improving performance and reducing binary size. It enables efficient calculations on platforms without floating-point hardware support, enhancing precision and speed.

Read original articleLink Icon
Fixed-point arithmetic as a replacement for soft floats

The blog post discusses the advantages of using fixed-point arithmetic as a substitute for floating-point arithmetic in embedded systems, particularly on platforms like the Cortex M0+, which lack hardware support for floating-point operations. Fixed-point types, defined in ISO TR 18037, allow for efficient representation of fractional values using scaled integers, enabling arithmetic operations to be performed using standard integer instructions. This approach has demonstrated a significant performance improvement—approximately double the speed in classification algorithms—while also reducing binary size without compromising accuracy. The author highlights the limitations of floating-point arithmetic, especially in embedded contexts where software emulation can be costly. The blog also details the implementation of fixed-point types in a Google project, including the adaptation of mathematical functions like expf and sqrtf to work with fixed-point types. The author notes that careful normalization techniques can prevent overflow and maintain precision, ultimately enhancing performance. The transition to fixed-point arithmetic not only streamlines operations but also aligns with the constraints of embedded systems, making it a viable alternative to traditional floating-point methods.

- Fixed-point arithmetic can replace floating-point arithmetic in embedded systems for better performance.

- Using fixed-point types can lead to significant speed improvements and reduced binary size.

- The implementation of mathematical functions for fixed-point types is crucial for successful integration.

- Normalization techniques help prevent overflow and maintain precision in calculations.

- Fixed-point types are particularly beneficial for platforms lacking hardware support for floating-point operations.

Link Icon 5 comments
By @gatane - 7 months
You can also use a library for it:

https://github.com/PetteriAimonen/libfixmath

I saw this getting forked for a custom SDK for the PS1 (which doesnt have a FPU).

By @phkahler - 7 months
I just converted a big fixed point algorithm to float on Cortex-M4f. It runs very close to the same speed, but significantly more readable.

In the fixed code I used straight c but wrote functions for sin, cos, and reciprocal square root.

I can't see getting just a 2x improvement over soft float, while using llvm specific features and modifying libraries just eliminates portability.

By @User23 - 7 months
My first exposure to fixed-point arithmetic was early cpu based 3d rendering. It’s a really neat technique.
By @mikequinlan - 7 months
What is the advantage of fixed point arithmetic over rational arithmetic? Rational arithmetic seems to include fixed point arithmetic as a subset (in fixed point arithmetic the denominator is always a power of the base).
By @librasteve - 7 months
the Raku language https://raku.org defaults to bigint (Int)and rational (Rat) numerics since that gives fewer unexpected results with eg decimals (0.1+0.2==0.3) … it’s easy enough to reach for floating point (Num), but this is deliberately the non default so that you the coder need to know that is what you want to trade performance vs precision

all irrational operations (sqrt, sin, cos and so on) return Num in a consistent way