NEON 向量化无符号字节的乘积之和: (a[i]-int1) * (b[i]-int2)

发布于 2024-12-20 21:43:05 字数 1569 浏览 1 评论 0原文

我需要改进循环,因为我的应用程序调用了数千次。我想我需要用 Neon 来做这件事,但我不知道从哪里开始。

假设/前提条件:

  • w 始终为 320(16/32 的倍数)。
  • papb 是 16 字节对齐的,
  • mamb 是正数。

 int whileInstruction (const unsigned char *pa,const unsigned char *pb,int ma,int mb,int w)
{
    int sum=0;

    do {
        sum += ((*pa++)-ma)*((*pb++)-mb);

    } while(--w);


    return sum;
}

这种矢量化的尝试效果不佳,并且不安全(缺少破坏),但演示了我正在尝试做的事情:

int whileInstruction (const unsigned char *pa,const unsigned char *pb,int ma,int mb,int w)
{

    asm volatile("lsr          %2, %2, #3      \n"
                 ".loop:                       \n"
                 "# load 8 elements:             \n"
                 "vld4.8      {d0-d3}, [%1]!   \n"
                 "vld4.8      {d4-d7}, [%2]!   \n"
                 "# do the operation:     \n"
                 "vaddl.u8    q7, d0, r7       \n"
                 "vaddl.u8    q8, d1, d8       \n"
                 "vmlal.u8    q7, q7, q8       \n"
                 "# Sum the vector a save in sum (this is wrong):\n"
                 "vaddl.u8    q7, d0, r7       \n"
                 "subs        %2, %2, #1       \n" // Decrement iteration count
                 "bne         .loop            \n" // Repeat unil iteration count is not zero
                 :
                 : "r"(pa), "r"(pb), "r"(w),"r"(ma),"r"(mb),"r"(sum)
                 : "r4", "r5", "r6","r7","r8","r9"
                 );

    return sum;
}

I need to improve a loop, because is called by my application thousands of times. I suppose I need to do it with Neon, but I don´t know where to begin.

Assumptions / pre-conditions:

  • w is always 320 (multiple of 16/32).
  • pa and pb are 16-byte aligned
  • ma and mb are positive.
 int whileInstruction (const unsigned char *pa,const unsigned char *pb,int ma,int mb,int w)
{
    int sum=0;

    do {
        sum += ((*pa++)-ma)*((*pb++)-mb);

    } while(--w);


    return sum;
}

This attempt at vectorizing it is not working well, and isn't safe (missing clobbers), but demonstrates what I'm trying to do:

int whileInstruction (const unsigned char *pa,const unsigned char *pb,int ma,int mb,int w)
{

    asm volatile("lsr          %2, %2, #3      \n"
                 ".loop:                       \n"
                 "# load 8 elements:             \n"
                 "vld4.8      {d0-d3}, [%1]!   \n"
                 "vld4.8      {d4-d7}, [%2]!   \n"
                 "# do the operation:     \n"
                 "vaddl.u8    q7, d0, r7       \n"
                 "vaddl.u8    q8, d1, d8       \n"
                 "vmlal.u8    q7, q7, q8       \n"
                 "# Sum the vector a save in sum (this is wrong):\n"
                 "vaddl.u8    q7, d0, r7       \n"
                 "subs        %2, %2, #1       \n" // Decrement iteration count
                 "bne         .loop            \n" // Repeat unil iteration count is not zero
                 :
                 : "r"(pa), "r"(pb), "r"(w),"r"(ma),"r"(mb),"r"(sum)
                 : "r4", "r5", "r6","r7","r8","r9"
                 );

    return sum;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-12-27 21:43:05

这是一个简单的 NEON 实现。我已经针对标量代码对此进行了测试,以确保它有效。请注意,为了获得最佳性能,papb 都应按 16 字节对齐。

#include <arm_neon.h>

int whileInstruction_neon(const unsigned char *pa, const unsigned char *pb, int ma, int mb, int w)
{
    int sum = 0;

    const int32x4_t vma = { ma, ma, ma, ma };
    const int32x4_t vmb = { mb, mb, mb, mb };

    int32x4_t vsumll = { 0 };
    int32x4_t vsumlh = { 0 };
    int32x4_t vsumhl = { 0 };
    int32x4_t vsumhh = { 0 };
    int32x4_t vsum;

    int i;

    for (i = 0; i <= (w - 16); i += 16)
    {
        uint8x16_t va = vld1q_u8(pa);   // load vector from pa
        uint8x16_t vb = vld1q_u8(pb);   // load vector from pb

        // unpack va into 4 vectors

        int16x8_t val =  (int16x8_t)vmovl_u8(vget_low_u8(va));
        int16x8_t vah =  (int16x8_t)vmovl_u8(vget_high_u8(va));
        int32x4_t vall = vmovl_s16(vget_low_s16(val));
        int32x4_t valh = vmovl_s16(vget_high_s16(val));
        int32x4_t vahl = vmovl_s16(vget_low_s16(vah));
        int32x4_t vahh = vmovl_s16(vget_high_s16(vah));

        // subtract means

        vall = vsubq_s32(vall, vma);
        valh = vsubq_s32(valh, vma);
        vahl = vsubq_s32(vahl, vma);
        vahh = vsubq_s32(vahh, vma);

        // unpack vb into 4 vectors

        int16x8_t vbl =  (int16x8_t)vmovl_u8(vget_low_u8(vb));
        int16x8_t vbh =  (int16x8_t)vmovl_u8(vget_high_u8(vb));
        int32x4_t vbll = vmovl_s16(vget_low_s16(vbl));
        int32x4_t vblh = vmovl_s16(vget_high_s16(vbl));
        int32x4_t vbhl = vmovl_s16(vget_low_s16(vbh));
        int32x4_t vbhh = vmovl_s16(vget_high_s16(vbh));

        // subtract means

        vbll = vsubq_s32(vbll, vmb);
        vblh = vsubq_s32(vblh, vmb);
        vbhl = vsubq_s32(vbhl, vmb);
        vbhh = vsubq_s32(vbhh, vmb);

        // update 4 partial sum of products vectors

        vsumll = vmlaq_s32(vsumll, vall, vbll);
        vsumlh = vmlaq_s32(vsumlh, valh, vblh);
        vsumhl = vmlaq_s32(vsumhl, vahl, vbhl);
        vsumhh = vmlaq_s32(vsumhh, vahh, vbhh);

        pa += 16;
        pb += 16;
    }

    // sum 4 partial sum of product vectors

    vsum = vaddq_s32(vsumll, vsumlh);
    vsum = vaddq_s32(vsum, vsumhl);
    vsum = vaddq_s32(vsum, vsumhh);

    // do scalar horizontal sum across final vector

    sum = vgetq_lane_s32(vsum, 0);
    sum += vgetq_lane_s32(vsum, 1);
    sum += vgetq_lane_s32(vsum, 2);
    sum += vgetq_lane_s32(vsum, 3);

    // handle any residual non-multiple of 16 points

    for ( ; i < w; ++i)
    {
        sum +=  (*pa++ - ma) * (*pb++ - mb);
    }

    return sum;
}

Here is a simple NEON implementation. I have tested this against the scalar code to make sure that it works. Note that for best performance both pa and pb should be 16 byte aligned.

#include <arm_neon.h>

int whileInstruction_neon(const unsigned char *pa, const unsigned char *pb, int ma, int mb, int w)
{
    int sum = 0;

    const int32x4_t vma = { ma, ma, ma, ma };
    const int32x4_t vmb = { mb, mb, mb, mb };

    int32x4_t vsumll = { 0 };
    int32x4_t vsumlh = { 0 };
    int32x4_t vsumhl = { 0 };
    int32x4_t vsumhh = { 0 };
    int32x4_t vsum;

    int i;

    for (i = 0; i <= (w - 16); i += 16)
    {
        uint8x16_t va = vld1q_u8(pa);   // load vector from pa
        uint8x16_t vb = vld1q_u8(pb);   // load vector from pb

        // unpack va into 4 vectors

        int16x8_t val =  (int16x8_t)vmovl_u8(vget_low_u8(va));
        int16x8_t vah =  (int16x8_t)vmovl_u8(vget_high_u8(va));
        int32x4_t vall = vmovl_s16(vget_low_s16(val));
        int32x4_t valh = vmovl_s16(vget_high_s16(val));
        int32x4_t vahl = vmovl_s16(vget_low_s16(vah));
        int32x4_t vahh = vmovl_s16(vget_high_s16(vah));

        // subtract means

        vall = vsubq_s32(vall, vma);
        valh = vsubq_s32(valh, vma);
        vahl = vsubq_s32(vahl, vma);
        vahh = vsubq_s32(vahh, vma);

        // unpack vb into 4 vectors

        int16x8_t vbl =  (int16x8_t)vmovl_u8(vget_low_u8(vb));
        int16x8_t vbh =  (int16x8_t)vmovl_u8(vget_high_u8(vb));
        int32x4_t vbll = vmovl_s16(vget_low_s16(vbl));
        int32x4_t vblh = vmovl_s16(vget_high_s16(vbl));
        int32x4_t vbhl = vmovl_s16(vget_low_s16(vbh));
        int32x4_t vbhh = vmovl_s16(vget_high_s16(vbh));

        // subtract means

        vbll = vsubq_s32(vbll, vmb);
        vblh = vsubq_s32(vblh, vmb);
        vbhl = vsubq_s32(vbhl, vmb);
        vbhh = vsubq_s32(vbhh, vmb);

        // update 4 partial sum of products vectors

        vsumll = vmlaq_s32(vsumll, vall, vbll);
        vsumlh = vmlaq_s32(vsumlh, valh, vblh);
        vsumhl = vmlaq_s32(vsumhl, vahl, vbhl);
        vsumhh = vmlaq_s32(vsumhh, vahh, vbhh);

        pa += 16;
        pb += 16;
    }

    // sum 4 partial sum of product vectors

    vsum = vaddq_s32(vsumll, vsumlh);
    vsum = vaddq_s32(vsum, vsumhl);
    vsum = vaddq_s32(vsum, vsumhh);

    // do scalar horizontal sum across final vector

    sum = vgetq_lane_s32(vsum, 0);
    sum += vgetq_lane_s32(vsum, 1);
    sum += vgetq_lane_s32(vsum, 2);
    sum += vgetq_lane_s32(vsum, 3);

    // handle any residual non-multiple of 16 points

    for ( ; i < w; ++i)
    {
        sum +=  (*pa++ - ma) * (*pb++ - mb);
    }

    return sum;
}
怂人 2024-12-27 21:43:05

好吧,我的问题的另一个解决方案采用了 Paul R 的完美解决方案,在 w 等于 8 的情况下,通常会发生什么,可以使用这个函数:

int whileInstruction8Valors (const unsigned char *pa,const unsigned char *pb,int ma,int mb,int w)
{

int sum=0;
//int 32 bits /4 elementos? 

const int32x4_t vma = { ma, ma, ma, ma };
const int32x4_t vmb = { mb, mb, mb, mb };

int32x4_t vsumll = { 0 };
int32x4_t vsumlh = { 0 };

int32x4_t vsum;

//char 8 bytes / 8 elementos
uint8x8_t  va2= vld1_u8(pa); // VLD1.8 {d0}, [r0]
uint8x8_t  vb2= vld1_u8(pb); // VLD1.8 {d0}, [r0]

//int 16 bytes /8 elementos
int16x8_t val =  (int16x8_t)vmovl_u8(va2);

//int 32 /4 elementos *2 
int32x4_t vall = vmovl_s16(vget_low_s16(val));
int32x4_t valh = vmovl_s16(vget_high_s16(val));

// subtract means
vall = vsubq_s32(vall, vma);
valh = vsubq_s32(valh, vma);

//int 16 bytes /8 elementos
int16x8_t vbl2 =  (int16x8_t)vmovl_u8(vb2);

//int 32 /4 elementos *2 
int32x4_t vbll = vmovl_s16(vget_low_s16(vbl2));
int32x4_t vblh = vmovl_s16(vget_high_s16(vbl2));

// subtract means

vbll = vsubq_s32(vbll, vmb);
vblh = vsubq_s32(vblh, vmb);

// update 4 partial sum of products vectors

vsumll = vmlaq_s32(vsumll, vall, vbll);
vsumlh = vmlaq_s32(vsumlh, valh, vblh);

// sum 4 partial sum of product vectors

vsum = vaddq_s32(vsumll, vsumlh);

// do scalar horizontal sum across final vector

sum = vgetq_lane_s32(vsum, 0);
sum += vgetq_lane_s32(vsum, 1);
sum += vgetq_lane_s32(vsum, 2);
sum += vgetq_lane_s32(vsum, 3);

return sum;
}

也许可以改进它。

Well another solution for my problem taken the perfect solution by Paul R, in the case the w is equal to 8, what happens usually it is possible to use this function:

int whileInstruction8Valors (const unsigned char *pa,const unsigned char *pb,int ma,int mb,int w)
{

int sum=0;
//int 32 bits /4 elementos? 

const int32x4_t vma = { ma, ma, ma, ma };
const int32x4_t vmb = { mb, mb, mb, mb };

int32x4_t vsumll = { 0 };
int32x4_t vsumlh = { 0 };

int32x4_t vsum;

//char 8 bytes / 8 elementos
uint8x8_t  va2= vld1_u8(pa); // VLD1.8 {d0}, [r0]
uint8x8_t  vb2= vld1_u8(pb); // VLD1.8 {d0}, [r0]

//int 16 bytes /8 elementos
int16x8_t val =  (int16x8_t)vmovl_u8(va2);

//int 32 /4 elementos *2 
int32x4_t vall = vmovl_s16(vget_low_s16(val));
int32x4_t valh = vmovl_s16(vget_high_s16(val));

// subtract means
vall = vsubq_s32(vall, vma);
valh = vsubq_s32(valh, vma);

//int 16 bytes /8 elementos
int16x8_t vbl2 =  (int16x8_t)vmovl_u8(vb2);

//int 32 /4 elementos *2 
int32x4_t vbll = vmovl_s16(vget_low_s16(vbl2));
int32x4_t vblh = vmovl_s16(vget_high_s16(vbl2));

// subtract means

vbll = vsubq_s32(vbll, vmb);
vblh = vsubq_s32(vblh, vmb);

// update 4 partial sum of products vectors

vsumll = vmlaq_s32(vsumll, vall, vbll);
vsumlh = vmlaq_s32(vsumlh, valh, vblh);

// sum 4 partial sum of product vectors

vsum = vaddq_s32(vsumll, vsumlh);

// do scalar horizontal sum across final vector

sum = vgetq_lane_s32(vsum, 0);
sum += vgetq_lane_s32(vsum, 1);
sum += vgetq_lane_s32(vsum, 2);
sum += vgetq_lane_s32(vsum, 3);

return sum;
}

Maybe it is possible to improve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文