Since a Floating Point Unit (FPU) is not present in Arduino Uno, I wanted to know how float operations are performed. If it is simulated as integer operations, I want to know how exactly the simulation is done.
2 Answers
As I said in my comment, all float operations are implemented in software. As a simple test, I compiled the following program:
volatile float x, y, z;
int main(void)
{
z = x + y;
return 0;
}
The compiler translated the sum operation into a call to __addsf3.
This function extends its arguments to flt40_t format (non-standard 40
bits float), calls __addsf3x to do the 40-bit addition, then calls
__fp_round to convert the result into the standard float format.
__addsf3x also makes a few library calls of its own. In the end, the
addition pulled the following functions into the compiled program:
__subsf3, __addsf3, __addsf3x, __fp_inf, __fp_nan,
__fp_pscA, __fp_pscB, __fp_round, __fp_split3, __fp_splitA,
__fp_zero and __fp_szero.
All these functions are provided by the avr-libc. You can find them in the libm/fplib directory. See for example the files:
- 45,094
- 4
- 42
- 81
software emulation. The float is decomposed into sign, exponent and mantissa and then integer operations are used to do the actual math.
Addition and subtraction means shifting the mantissas to a common magnitude, adding/subtracting them and then checking the highest set bit for the new exponent.
Multiplication means adding the exponents and multiplying the mantissas. Division is subtracting.
- 3,267
- 1
- 13
- 12