FPU float in functions calls
Hello,
I recently started with OpenSTM32 to program my STM32F4 Discovery board. I’ve already programmed it under Windows with CooCox IDE. So I had some program examples to test under Linux and sw4stm32. But, I was surprised that one of them didn’t run correctly and hanged. After spending some time with the debugger, I noticed that the problem was due to float variable declaration and function call with float variables as parameters. The program went in an infinite loop.
First, I took a look at the compiler options in order to verify that the FPU was correctly set, and yes it was. Second, I searched on internet about this problem and found some ideas like changing float to double. So I did it but it wasn’t sufficient : the FPU was in fact not activated !
Here what the original code looks like :
factor(float, float);
int main(void)
{
float a = 10.0f, b = -1.5f;
while(1)
{
factor(a, b);
}
}
This code needs the corrections below to run correctly :
factor(double, double); // float -> double
int main(void)
{
// NOTE: Important: Enable full access to FPU:
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
double a = 10.0f, b = -1.5f; // float -> double
while(1)
{
factor(a, b);
}
}
Another thing to do in factor() is to cast the double variable to float because there is a slowdown in the calculation using double. Why ? Does the FPU only compute float numbers in simple precision ?
So, can someone explain what’s the difference between my original program and the modified one ? Is it a normal behaviour of OpenSTM32 or GCC compiler ?
Thanks