欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

arm neon 函数详解

程序员文章站 2022-04-19 16:13:12
...

前面一篇博客有介绍arm_neon.h中函数的定义,以及大致的功能和作用,为了进一步加深对这些函数的理解,我们通过运行代码来验证。

在android studio中创建一个android工程,然后在jni代码中调用arm_neon.h中的函数

为了方便大家学习,我上传了一个demo,ARMNeonDemo


下载解压后,导入android studio就可以了。

arm neon 函数详解

打开上面native.cpp这个文件,在如下函数中添加demo code就可以了,例如

static void neon(JNIEnv *env) {
    LOGD("the function for test neon start.");
    int8_t s8[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    int8x16_t a=vld1q_s8(s8);  //赋值,a=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
    int8x16_t b=vld1q_s8(s8);  //赋值,b=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
    //int8x16_t   vaddq_s8(int8x16_t a, int8x16_t b); // VADD.I8 q0,q0,q0
    int8x16_t c=vaddq_s8(a,b); //add 后面的q表示正常指令,c应该是2,4,6,8,10 ...
    int8_t ret[16]={0};
    vst1q_s8(ret,c);   //把c的值赋给ret数组,这样是为了方便打印,验证结果
    for(int i=0;i<16;i++){
        LOGD("ret[%d] is %d ",i,ret[i]);
    }
    LOGD("the function for test neon end.");
}

编译运行后的结果如下

08-18 14:52:24.722 14304-14304/com.hxiong.neondemo I/neon_demo_jni: JNI_OnLoad
08-18 14:52:24.723 14304-14304/com.hxiong.neondemo D/neon_demo_jni: the function for test neon start.
08-18 14:52:24.723 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[0] is 2 
08-18 14:52:24.723 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[1] is 4 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[2] is 6 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[3] is 8 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[4] is 10 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[5] is 12 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[6] is 14 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[7] is 16 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[8] is 18 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[9] is 20 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[10] is 22 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[11] is 24 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[12] is 26 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[13] is 28 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[14] is 30 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: ret[15] is 32 
08-18 14:52:24.724 14304-14304/com.hxiong.neondemo D/neon_demo_jni: the function for test neon end.