本帖最后由 sonicll 于 2011-11-24 13:55 编辑
使用环境为 STM32F4DISCOVERY + MDK4.22
因为看到MDK的options for target选项里,可以选择使用或不使用FPU,想看看使用FPU和不使用FPU在浮点计算上有多大差距,就写了个简单的程序,加减乘除计算,看看每秒钟能计算多少次,把次数和计算结果从串口输出来,代码如下
#include "stm32f4xx.h"
#include <stdio.h>
#include <string.h>
void InitTIM(void);
void InitUart(void);
void UartSendStr(char buf[],int length);
char TIM2_Flag = 0;
int main(void)
{
unsigned long count = 0;
float i = 0;
float j = 0.134245223423424556;
float k = 0.253252534536366666;
char buffer[128];
SystemInit();
InitTIM();
InitUart();
while(1)
{
i = (j+5123)*821/(k-456);
count++;
if (TIM2_Flag) //TIM2的中断处理函数里把TIM2_Flag置1,周期1秒
{
TIM2_Flag = 0;
sprintf(buffer, "%d,%f\n",count,i);
UartSendStr(buffer,strlen(buffer));
count = 0;
}
}
}
void UartSendStr(char buf[],int length)
{
int i;
for(i=0;i<length;i++)
{
USART_SendData(USART3, buf);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
}
void InitUart(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
void InitTIM(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 41999;
TIM_TimeBaseStructure.TIM_Prescaler = 1999;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_PrescalerConfig(TIM2, TIM_TimeBaseStructure.TIM_Prescaler, TIM_PSCReloadMode_Immediate);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
MDK的选项里,target选项卡里只选择是否使用FPU,其他选项不选;C/C++选项卡里,优化等级选为0,其他选项都不选
观察结果为:
使用FPU,count为4657245
不使用FPU,count为916266
区别还是比较明显;
接下来是问题,当我把i、j、k三个变量的类型设成double后,结果变成了这样:
使用FPU,count为284680
不使用FPU,count为530620
我知道F4的FPU是单精度的,但是也不应该出现double类型使用FPU,速度反而下降的现象啊,请大家帮忙看看,是程序问题还是MDK有什么设置不对,工程在附件里 |