I need to implement the following loop in Neon.
int jump=4,c[8],i; //c[8] may be declared here
int *src,sum=0; //**EDIT:** src points to a 256 element array
for (i = 0; i < 4; i++)
{
sum = src[ i + 0 * jump] * c[0];//1
sum += src[ i + 1 * jump] * c[1];//2
sum += src[ i + 2 * jump] * c[2];//3
sum += src[ i + 3 * jump] * c[3];//4
sum += src[ i + 4 * jump] * c[4];//5
sum += src[ i + 5 * jump] * c[5];//6
sum += src[ i + 6 * jump] * c[6];//7
sum += src[ i + 7 * jump] * c[7];//8
src += 2; //9
}
**EDIT:**
The code can be shortened as-
int jump=4,c[8],i,j; //initialize array c
int *src,sum,a[256];//initialize array a
src=a;
for (i = 0; i < 4; i++)
{
sum=0;
for (j = 0; j < 8; j++)
{
int *p=src+ i + (j * jump);
sum += (*p)* c[j]; //sum += src[ i + j* jump] * c[j]
}
printf("Sum:%d\n",sum);
src += 2;
}
Just need to know a way to implement something like [%0]=[%0]+4 rather than [%0]!
The main optimization will come by running the instructions numbered 1-8 in parallel using VMLA instruction in NEON.
- We may do that by loading array c[8] into registers q0 and q1 and loading array src[256] into registers q2 and q3.After this we use VMLA,VADD,VPADD to get the result in the variable sum.
- The problem being faced is how to load the elements of array src (as src[0],src[4],src[8] and so on)since the only way i know how to load an array is by [%1]! which only loads an array sequentially(as src[0],src[1],src[2] and so on).
Also how may the pointer src be incremented by 2 in instruction 9?
Instead of computing the sums one after another, compute them at the same time. Then you don’t need to skip source values, since every source value (presumably) affects some sum. Basically, what you do is computing (* means dot product).
You can do that instead by doing
You don’t actually need a horizontal summing during the individual steps, you can just use vmul.32 (or maybe it’s accumuated form, if there is one) to compute the dot-products, then sum them horizontally at the end. You’ll just have to figure out a way to compute the coefficient vectors, but since they all have only a single non-zero element, you can simply use vset_lane.
Or, even better, you can exploit that fact that each coefficient vector contains only one non-zero entry, and do one multipyl instead of four, then use vdup_lane extract each lane and add it to the sum. You’ll need to generate the coefficient vectors
which should be easy if your coefficients are already stored consecutively – you just need to fixup the first element.