我也是纯新手,看了这篇博客http://blog.youkuaiyun.com/may0324/article/details/72847800,感觉对上手很有帮助。本文也是由这篇博客给的例子出发,探讨一些问题。
1、编译
要让我们的代码在板子上跑,必然是要用交叉工具链来编译,我自己用的是arm-linux-g++。在编译neon优化的代码时,要加入 -mfloat-abi=softfp -mfpu=neon -ffast-math。-mfloat-abi有三个可选项:可选soft/softfp/hardfp: soft和后两者的区别是编译器不会生成浮点指令,浮点操作完全由软件实现;相比之下,hardfp走了另一个极端,浮点操作完全由硬件实现,效率最高;softfp则是走中间路线,它有着和hardfp完全不一样的calling convention——hardfp通过VFP来传浮点参数,而softfp还是通过整型寄存器来传参,和soft是一致的。如果选择soft,那么就不支持neon了。需要-mfpu=neon来指定FPU(Floating-Point Unit)是NEON Unit;-ffast-math是浮点优化选项,可以极大提高浮点运算速度。
此外,使用neon指令需要<arm-neon.h>头文件。
2、代码部分
还是先贴一下上述博客中的代码(稍作修改)。代码中使用的各函数作用可参见上述博客。
#include<iostream>
#include<time.h>
using namespace std;
float sum_array(float* arr,int len);
int main()
{
long l=100000;
float array[l];
for(long i=0;i<l;i++)
{
array[i]=i/1000;
}
clock_t starttime=clock();
for(int round=1;round<=1000;round++)
{
float Sum=sum_array(array,l);
//cout<<Sum<<endl;
}
clock_t endtime=clock();
cout<<(double)(endtime-starttime)/CLOCKS_PER_SEC<<"ms"<<endl;
return 0;
}
float sum_array(float* arr,int len)
{
if(NULL== arr||len<1)
{
cout<<"input error\n";
return 0;
}
float sum(0.0);
for(long i=0;i<len;++i)