Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:13:31+00:00 2026-05-27T14:13:31+00:00

I need to improve a loop, because is called by my application thousands of

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:13:31+00:00Added an answer on May 27, 2026 at 2:13 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to improve the response time for a Grails application, so I need
I have a piece of code that I don't know how to improve it.
Features Only numbers A unique dot (.) But...i need improve to allow user digits
I need to improve memory performance on my application and I could see that
i am using php for a while but....... i think i need to improve
I need to improve my windows form application UI, is there any free plugin
I need to improve on a regular expression I'm using. Currently, here it is:
I have recently started to feel that I need to greatly improve my C++
I need to implement threading to improve load time in a compact framework app.
In order to improve my open source project, I need testers. I have created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.