转换为int和int与NEON的总和

时间:2012-01-26 12:12:44

标签: c arm neon

我想将下一个函数转换为NEON:

int  dot4_c(unsigned char v0[4], unsigned char v1[4]){
      int r=0;
      r = v0[0]*v1[0];
      r += v0[1]*v1[1];
      r += v0[2]*v1[2];  
      r += v0[3]*v1[3];
return r;
}

我想我差不多了,但是因为效果不好而有错误

int dot4_neon_hfp(unsigned char v0[4], unsigned char v1[4])
{

asm volatile (
              "vld1.16      {d2, d3}, [%0]          \n\t"   //d2={x0,y0}, d3={z0, w0}
              "vld1.16      {d4, d5}, [%1]          \n\t"   //d4={x1,y1}, d5={z1, w1}
              "vcvt.32.u16      d2, d2                  \n\t" //conversion
              "vcvt.32.u16      d3, d3                  \n\t"
              "vcvt.32.u16      d4, d4                  \n\t"
              "vcvt.32.u16      d5, d5                  \n\t"

              "vmul.32      d0, d2, d4              \n\t"   //d0= d2*d4
              "vmla.32      d0, d3, d5              \n\t"   //d0 = d0 + d3*d5 
              "vpadd.32         d0, d0              \n\t"   //d0 = d[0] + d[1]
              :: "r"(v0), "r"(v1) : 
              );    

}

我怎样才能使这个工作?

1 个答案:

答案 0 :(得分:2)

如上所述,您必须使用NEON一次加载至少8个字节。只要加载不超过缓冲区的末尾,就可以忽略额外的字节。以下是使用内在函数的方法:

uint8x8_t v0_vec, v1_vec;
uint16x8_t vproduct;
uint32x2_t vsum32;

v0_vec = vld1_u8(v0); // extra bytes will be ignored as long as you can safely read them
v1_vec = vld1_u8(v1);
// you didn't specify if the product of your vector fits in 8-bits, so I assume it needs to be widened to 16-bits
vproduct = vmull_u8(v0_vec, v1_vec);
vsum32 = vpaddl_u16(vget_low_u16(vproduct)); // pairwise add lower half (first 4 u16's)
return vsum32.val[0] + vsum32.val[1];

如果您绝对无法从源指针加载8个字节,则可以手动将32位值加载到NEON寄存器(4个字节)中,然后将其转换为正确的内部类型。