What is an FPU?
An FPU, or Floating-Point Unit, is a dedicated hardware circuit within a microprocessor or microcontroller designed specifically to perform mathematical operations on floating-point numbers.
- Floating-Point Numbers: These are numbers with a decimal point (e.g., 3.14159, -0.0012, 6.02e23). They are used to represent a wide range of values, both very large and very small, with a certain precision.
- The Alternative: Software Emulation (Library): If a microcontroller does not have an FPU, the compiler must generate code that uses a software library to perform floating-point math. This involves breaking down the operation into a series of simpler integer instructions. This process is much slower and consumes more CPU cycles and memory.
The Analogy: Math Co-processor
Think of the FPU as a specialized math co-processor sitting alongside the main CPU core (the ALU - Arithmetic Logic Unit).
- The Main CPU (ALU) is a generalist, great at handling integers and logical operations.
- The FPU is a specialist, hired exclusively to do complex decimal math very quickly and efficiently.
When the main core encounters a line of code like float x = a * b + c;, it can "offload" this calculation directly to the FPU, which computes the result in hardware, often in a single clock cycle or just a few.
Why is an FPU Important?
- Performance (Speed): This is the primary reason. An FPU can perform floating-point operations tens to hundreds of times faster than software emulation. For example, a single-instruction FMUL (Floating Multiply) on an FPU might replace hundreds of integer instructions from a software library.
- Determinism: Hardware operations have predictable timing. Software libraries can have variable execution times depending on the values being processed, which is problematic for real-time control systems.
- Reduced Code Size: The compiler doesn't need to link in the large, bulky software floating-point library, freeing up precious program memory (Flash).
- Ease of Development: It allows engineers to use floating-point arithmetic in their code (float, double) without worrying about severe performance penalties, making algorithms for digital signal processing (DSP), control theory, and data analysis much easier to implement.
Types of FPU in Microcontrollers
It's crucial to distinguish between two common types:
- Single-Precision FPU: Handles 32-bit floating-point numbers (the float type in C/C++). This is the most common type found in mainstream microcontrollers.
- Double-Precision FPU: Handles 64-bit floating-point numbers (the double type in C/C++). This is less common and typically found in higher-performance processors (like application processors in the Raspberry Pi) or specialized microcontrollers.
Key Point: A microcontroller advertised as having an "FPU" almost always means a single-precision FPU. If you use double on such a chip, the operations will typically fall back to slow software emulation.
Examples of Microcontrollers with an FPU
Here are examples from various major vendors and architectures:
1. ARM Cortex-M Series (The most common category)
The FPU is an optional core component that chip manufacturers can license and include.
- Cortex-M4 / Cortex-M7 / Cortex-M33 / Cortex-M55: These cores have an optional single-precision FPU. It's so common that most mid-to-high-end chips based on these cores include it.
- Cortex-M0 / Cortex-M0+ / Cortex-M3: These cores do not have an optional FPU. Any floating-point math must be done in software.
Example Chips:
- STMicroelectronics STM32F4 Series (e.g., STM32F407): A classic example of a Cortex-M4 with a single-precision FPU.
- STMicroelectronics STM32H7 Series (e.g., STM32H743): A high-performance Cortex-M7 that often includes a double-precision FPU as well.
- Microchip (Atmel) SAMD51 (e.g., ATSAMD51): A Cortex-M4 with FPU, popular in hobbyist boards like the Arduino Zero and Adafruit ItsyBitsy M4.
- Nordic nRF52840: A Cortex-M4 with FPU, widely used in Bluetooth Low Energy applications.
- Espressif ESP32-S3: Its Xtensa LX7 cores include a single-precision FPU.
- Raspberry Pi RP2040: Notably, this popular microcontroller (used in the Raspberry Pi Pico) has a dual-core Cortex-M0+, which does not have a hardware FPU. All floating-point is done in software.
2. Other Architectures
- Microchip PIC32MZ: A MIPS-based microcontroller family that includes a core with a single-precision FPU.
- Texas Instruments Tiva C Series (ARM Cortex-M based): Many members, like the TM4C129x, use a Cortex-M4F core (the 'F' signifies the included FPU).
How to Check and Use the FPU in Your Code
- Compiler Settings: You must tell your compiler to generate hardware FPU instructions. In GCC/ARM, this is typically done with the -mfpu=fpv4-sp-d16 -mfloat-abi=hard flags.
- -mfpu: Specifies which FPU hardware you have.
- -mfloat-abi=hard: Tells the compiler to use the FPU for all float operations and to use the FPU's registers for passing floating-point arguments. This is the most efficient option.
- In the Code: You don't need to do anything special. Just use float variables and operations as you normally would. The compiler will automatically use the hardware instructions (VADD.F32, VMUL.F32, etc.) if it's configured correctly.
c #include <math.h> int main() { float sensor_value = 3.14f; float gain = 1.5f; float offset = 10.0f; // If the FPU is enabled, this calculation is done in hardware // in just a few clock cycles. float result = sensor_value * gain + offset; // Functions from math.h (like sinf) also benefit hugely from an FPU. float sine_val = sinf(sensor_value); while(1); return 0; } Summary
In short, an FPU is a critical feature for microcontrollers running algorithms that require intensive decimal math, such as digital signal processing (audio, filters), advanced motor control, complex sensor fusion, and scientific calculations. Its presence significantly boosts performance and responsiveness for these tasks.


Top comments (0)