MDK对浮点支持有bug吗?

2020-01-11 18:09发布

最近调试stm32f103平台上调试pid,发现函数参或者返回值若使用float型,则总会出问题,发现传进去的参数或者返回来的的值并非本来赋的那个值,百思不得其解.但若使用指针传递则完全正常。难道使用浮点有什么特殊操作吗?
举个例子.
float pid_cal(float in)
{
float ret=0.012;
……
printf("in=%f ",in);
return ret;
}

调用函数pid_cal(2.5); 函数打印出in=xxxx.xxxx。是一个很大的值,并非2.5
同理printf("pid_val=%f ",pid_cal(3.0));打印出来也不是期望的0.012

如改成指针形式则正常如
void pid_cal(float *in,float *out)
{
float ret=0.012;
……
printf("in=%f ",*in);
*out=ret;
}

float t=2.5;
float out_val;
pid_cal(&t,&out_val);
这样使用就不会有问题.

我觉得MDK不至于这么弱智吧?有人碰到过吗?
还是我的MDK破解有问题?
之前用的MDK4.6就有这个问题,后来还特地升级到4.73,发现问题依旧
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
22条回答
szyusong@163
2020-01-14 17:29
Compiler eight-byte alignment features

The compiler has the following eight-byte alignment features:

The Procedure Call Standard for the ARM Architecture (AAPCS) requires that the stack is eight-byte aligned at all external interfaces. The compiler and C libraries preserve the eight-byte alignment of the stack. In addition, the default C library memory model maintains eight-byte alignment of the heap.
Code is compiled in a way that requires and preserves the eight-byte alignment constraints at external interfaces.
If you have assembly language files, or legacy objects, or libraries in your project, it is your responsibility to check that they preserve eight-byte stack alignment, and correct them if required.
In RVCT v2.0 and later, and in ARM Compiler 4.1 and later, double and long long data types are eight-byte aligned for compliance with the Application Binary Interface for the ARM Architecture (AEABI). This enables efficient use of the LDRD and STRD instructions in ARMv5TE and later.
The default implementations of malloc(), realloc(), and calloc() maintain an eight-byte aligned heap.
The default implementation of alloca() returns an eight-byte aligned block of memory.

一周热门 更多>