Non-Confidential | ![]() | ARM DUI0472J | ||
| ||||
Home > Compiler Coding Practices > Example of hardware and software support differences for floating-point arithmetic |
This example shows how the compiler deals with floating-point arithmetic for different processors supporting either hardware or software floating-point arithmetic.
The following example shows a function implementing floating-point arithmetic in C code.
float foo(float num1, float num2) { float temp, temp2; temp = num1 + num2; temp2 = num2 * num2; return temp2 - temp; }
When the example C code is compiled with the command-line options
--cpu 5TE
and --fpu softvfp
, the compiler produces
machine code with the disassembly shown below. In this case, floating-point arithmetic is performed in software through calls to
library routines such as __aeabi_fmul
.
||foo|| PROC PUSH {r4-r6, lr} MOV r4, r1 BL __aeabi_fadd MOV r5, r0 MOV r1, r4 MOV r0, r4 BL __aeabi_fmul MOV r1, r5 POP {r4-r6, lr} B __aeabi_fsub ENDP
However, when the example C code is compiled with the command-line
option --fpu vfp
, the compiler produces machine code with the disassembly
shown below. In this case, floating-point
arithmetic is performed in hardware through floating-point arithmetic instructions such as
VMUL.F32
.
||foo|| PROC VADD.F32 s2, s0, s1 VMUL.F32 s0, s1, s1 VSUB.F32 s0, s0, s2 BX lr ENDP