开发环境
Stm32f407zgt6
CubeIDE
如何配置
在默认情况下,FPU一般为开启的,
在cubeide中可以在项目属性Properties -> C/C++ Bulid -> Tool Settings -> MCU/MPU Setiing
中看到
Floating-point-unit即为FPU,从下面的Floating-point ABI也可以看出为Hardware implementation,即硬件级别的设施。
注意事项
F4的FPU只支持32位的浮点运算,即单精度浮点single precision float,
在定义浮点数时必须以f为结尾,否则会被默认当作双精度浮点数,
则FPU不会参与工作!
运算对比
这里使用TIM6基本定时器(频率1ns),作为运算时间计时工具。
if(!strncmp((const char*)g_usart_rx_buf, "float\r\n", 7)){
uint16_t i = 5000;
GetTimerStart();
uint32_t startTP = GetTimer();
while(i--){
float b = 9.1331f * 123.245f;
float c = 9.135f / b;
}
uint32_t endTP = GetTimer();
GetTimerEnd();
printf("float compute cost: %ld ns\r\n", endTP-startTP);
}
这里定义了5k个单精度浮点运算,实际运算量为10k浮点运算
使用FPU
不使用FPU
可见,效率提高了4倍左右,FPU在浮点运算时有极大的帮助。