Pigweed Blog #4: Fixed-point arithmetic as a replacement for soft floats#

submited by
Style Pass
2024-10-05 00:00:06

Fixed-point arithmetic is an alternative to floating-point arithmetic for representing fractional data values (0.5, -1.125, 10.75, etc.). Fixed-point types can be represented as scaled integers . The advantage here is many arithmetic operations (+, -, *, /, etc.) can be implemented as normal integral instructions. This can be useful for embedded systems or other applications where floating-point operations can be too expensive or not available. Clang has support for fixed-point types according to ISO TR 18037. I work on a Google project that uses Pigweed. Recently, we replaced usage of floats in an embedded project running on Cortex M0+. This MCU doesn’t provide any hardware support for floating-point operations, so floating-point operations are implemented in software. We’ve observed a ~2x speed improvement in classification algorithms and a small decrease in binary size using fixed-point without sacrificing correctness.

All Clang users can benefit from this by adding -ffixed-point as a compilation flag. All of the types and semantics described in ISO 18037 are supported.

Leave a Comment