0

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.

  1. 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.
  2. 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?

rnjai
  • 1,065
  • 2
  • 17
  • 32
  • You're really not very likely to see a significant benefit using NEON for this (ditto for any other SIMD architecture that does not have gathered loads). Better to try and re-architect your data structures for contiguous access first. – Paul R Jun 27 '12 at 11:37
  • Could you please give any suggestions for the Data Structure? – rnjai Jun 27 '12 at 11:50
  • For SIMD you ideally want to organise your data so that it's contiguous, and of course suitably aligned (16 byte aligned base address) – Paul R Jun 27 '12 at 11:55
  • I basically need to implement something like [%0]=[%0]+4 if possible? – rnjai Jun 27 '12 at 12:12
  • Another option is to use the VLD4 instruction and load lots of unused data. Then you can use the pairwise add a few times to sum up your products. As mentioned above, the best way to make this run fast is to have your data laid out contiguously in memory. – BitBank Jun 28 '12 at 20:58
  • Ok will try to implement a data structure. – rnjai Jun 30 '12 at 04:16

1 Answers1

1

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).

sum0 = source * (c0 0 0  0 c1 0  0  0 c2 0  0  0 ... c7  0 0  0 )
sum1 = source * ( 0 0 0 c0  0 0  0 c1  0 0  0 c2 ...  0  0 0 c7 )
sum2 = source * ( 0 0 0  0  0 0 c0  0  0 0 c1  0 ...  0  0 c6 0 )
sum3 = source * ( 0 0 0  0  0 0  0  0  0 c0 0  0 ...  0 c5  0 0 )

You can do that instead by doing

sum0 = sum1 = sum2 = sum3 = 0

sum0 += (s0 s1 s2 s3) * (c0  0  0  0)
sum1 += (s0 s1 s2 s3) * ( 0  0  0 c0)
sum2 += (s0 s1 s2 s3) * ( 0  0  0  0)
sum3 += (s0 s1 s2 s3) * ( 0  0  0  0)

sum0 += (s0 s1 s2 s3) * (c1  0  0  0)
sum1 += (s0 s1 s2 s3) * ( 0  0  0 c1)
sum2 += (s0 s1 s2 s3) * ( 0  0 c0  0)
sum3 += (s0 s1 s2 s3) * ( 0  0  0  0)

...

sum0 += (s0 s1 s2 s3) * (c7  0  0  0)
sum1 += (s0 s1 s2 s3) * ( 0  0  0 c7)
sum2 += (s0 s1 s2 s3) * ( 0  0 c6  0)
sum3 += (s0 s1 s2 s3) * ( 0 c5  0  0)

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

(c0  0  0  0)
(c1  0 c0 c1)
(c2 c0 c1 c2)
...
(c7 c5 c6 c7)

which should be easy if your coefficients are already stored consecutively - you just need to fixup the first element.

fgp
  • 8,126
  • 1
  • 17
  • 18