Skip to content

Commit

Permalink
Update ARM_NEON_CNN编程.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Ewenwan authored May 10, 2019
1 parent cfceff8 commit b07269e
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions CNN/HighPerformanceComputing/ARM_NEON_CNN编程.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Intrinsics(内联函数)是使用C语言的方式对NEON寄存器进行操作,

NEON C内联函数(intrinsics)是由ARM定义的一组全新的数据类型和内联函数,便于使用C语言直接访问NEON单元。在C/C++程序中,内联函数就同普通函数一样,但在编译时,这些内联函数会直接映射为NEON提供的向量指令。当前GCC编译器和ARM编译器都支持相同的NEON内联语法,只需在程序中添加“arm_neon.h”头文件,就可以使用NEON内联函数。

[ARM NEON常用 intrinsics 函数总结 !!!!](https://blog.csdn.net/may0324/article/details/72847800)

**优势**:使用内联函数进行优化,开发人员无需关注寄存器分配和互锁等问题,这些都交由编译器处理,而且编写程序比较容易,优化后的性能相对较高。

**不足**:目前内联函数所提供的功能和灵活性仍远远比不上汇编指令,并且经过编译器编译后,会反复加载/存取寄存器数据,导致系统时钟的浪费。 
Expand Down Expand Up @@ -1128,8 +1130,108 @@ uint64x2_t vabdl_u32(uint32x2_t a, uint32x2_t b); // VABDL.U32 q0,d0,d0
>**加载并存储单个向量 加载并存储某类型的单个向量。vld1q_type**
```c
```
### 实例0:数组元素求和
```c
// c版本=======================
#include <iostream>
using namespace std;

float sum_array(float *arr, int len)
{
if(NULL == arr || len < 1)
{
cout<<"input error\n";
return 0;
}
float sum(0.0);
for(int i=0; i<len; ++i)
{
sum += *arr++;
}
return sum;
}


// arm intrinsics==============
#include <iostream>
#include <arm_neon.h> //需包含的头文件
using namespace std;

float sum_array(float *arr, int len)
{
if(NULL == arr || len < 1)
{
cout<<"input error\n";
return 0;
}

int dim4 = len >> 2; // 数组长度除4整数
int left4 = len & 3; // 数组长度除4余数,不够4的剩下的

float32x4_t sum_vec = vdupq_n_f32(0.0);//定义用于暂存累加结果的寄存器且初始化为0
for (; dim4>0; dim4--, arr+=4) //每次同时访问4个数组元素
{
float32x4_t data_vec = vld1q_f32(arr); //依次取4个元素存入寄存器vec
sum_vec = vaddq_f32(sum_vec, data_vec);//ri = ai + bi 计算两组寄存器对应元素之和并存放到相应结果
}
float sum = vgetq_lane_f32(sum_vec, 0)+vgetq_lane_f32(sum_vec, 1)+vgetq_lane_f32(sum_vec, 2)+vgetq_lane_f32(sum_vec, 3);//将累加结果寄存器中的所有元素相加得到最终累加值
for (; left4>0; left4--, arr++)
sum += (*arr) ; //对于剩下的少于4的数字,依次计算累加即可
return sum;
}
```
上述算法的时间复杂度时O(N/4)
从上面的例子看出,使用NEON函数很简单,只需要将依次处理,变为批处理(如上面的每次处理4个)。
上面用到的函数有:
float32x4_t vdupq_n_f32 (float32_t value)
将value复制4分存到返回的寄存器中
float32x4_t vld1q_f32 (float32_t const * ptr)
从数组中依次Load4个元素存到寄存器中
相应的 有void vst1q_f32 (float32_t * ptr, float32x4_t val)
将寄存器中的值写入数组中
float32x4_t vaddq_f32 (float32x4_t a, float32x4_t b)
返回两个寄存器对应元素之和 r = a+b
相应的 有float32x4_t vsubq_f32 (float32x4_t a, float32x4_t b)
返回两个寄存器对应元素之差 r = a-b
float32_t vgetq_lane_f32 (float32x4_t v, const int lane)
返回寄存器某一lane的值
其他常用的函数还有:
float32x4_t vmulq_f32 (float32x4_t a, float32x4_t b)
返回两个寄存器对应元素之积 r = a*b
float32x4_t vmlaq_f32 (float32x4_t a, float32x4_t b, float32x4_t c)
乘加 r = a +b*c
float32x4_t vextq_f32 (float32x4_t a, float32x4_t b, const int n)
拼接两个寄存器并返回从第n位开始的大小为4的寄存器 0<=n<=3
例如
a: 1 2 3 4
b: 5 6 7 8
vextq_f32(a,b,1) -> r: 2 3 4 5
vextq_f32(a,b,2) -> r: 3 4 5 6
vextq_f32(a,b,3) -> r: 4 5 6 7
```c
float32x4_t sum = vdupq_n_f32(0);
float _a[] = {1,2,3,4}, _b[] = {5,6,7,8} ;
float32x4_t a = vld1q_f32(_a), b = vld1q_f32(_b) ;
float32x4_t sum1 = vfmaq_laneq_f32(sum, a, b, 0);
float32x4_t sum2 = vfmaq_laneq_f32(sum1, a, b, 1);
float32x4_t sum3 = vfmaq_laneq_f32(sum2, a, b, 2);
```

[官方文档 其他常用函数](https://developer.arm.com/architectures/instruction-sets/simd-isas/neon/intrinsics)

### 示例1:向量加法**
```c
Expand Down

0 comments on commit b07269e

Please sign in to comment.