aboutsummaryrefslogtreecommitdiffstats
path: root/sumsq_av.c
blob: 53c6acfd3b6d1493ec01e7940fd5b0d88ff67ee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Compute the sum of the squares of a vector of signed shorts

 * This is the Altivec SIMD version. It's a little hairy because Altivec
 * does not do 64-bit operations directly, so we have to accumulate separate
 * 32-bit sums and carries

 * Copyright 2004 Phil Karn, KA9Q
 * May be used under the terms of the GNU Lesser General Public License (LGPL)
 */

#include "fec.h"

unsigned long long sumsq_av(signed short *in,int cnt){
  long long sum;
  vector signed short x;
  vector unsigned int sums,carries,s1,s2;
  int pad;
  union { vector unsigned char cv; vector unsigned int iv; unsigned int w[4]; unsigned char c[16];} s;

  carries = sums = (vector unsigned int)(0);
  if((pad = (int)in & 15)!=0){
    /* Load unaligned leading word */
    x = vec_perm(vec_ld(0,in),(vector signed short)(0),vec_lvsl(0,in));
    if(cnt < 8){ /* Shift right to chop stuff beyond end of short block */
      s.c[15] = (8-cnt)<<4;
      x = vec_sro(x,s.cv);
    }
    sums = (vector unsigned int)vec_msum(x,x,(vector signed int)(0));
    in += 8-pad/2;
    cnt -= 8-pad/2;
  }
  /* Everything is now aligned, rip through most of the block */
  while(cnt >= 8){
    x = vec_ld(0,in);
    /* A single vec_msum cannot overflow, but we have to sum it with
     * the earlier terms separately to handle the carries
     * The cast to unsigned is OK because squares are always positive
     */
    s1 = (vector unsigned int)vec_msum(x,x,(vector signed int)(0));
    carries = vec_add(carries,vec_addc(sums,s1));
    sums = vec_add(sums,s1);
    in += 8;
    cnt -= 8;
  }
  /* Handle trailing fragment, if any */
  if(cnt > 0){
    x = vec_ld(0,in);
    s.c[15] = (8-cnt)<<4;
    x = vec_sro(x,s.cv);
    s1 = (vector unsigned int)vec_msum(x,x,(vector signed int)(0));
    carries = vec_add(carries,vec_addc(sums,s1));
    sums = vec_add(sums,s1);
  }
  /* Combine 4 sub-sums and carries */
  s.c[15] = 64; /* Shift right two 32-bit words */
  s1 = vec_sro(sums,s.cv);
  s2 = vec_sro(carries,s.cv);
  carries = vec_add(carries,vec_addc(sums,s1));
  sums = vec_add(sums,s1);
  carries = vec_add(carries,s2);

  s.c[15] = 32; /* Shift right one 32-bit word */
  s1 = vec_sro(sums,s.cv);
  s2 = vec_sro(carries,s.cv);
  carries = vec_add(carries,vec_addc(sums,s1));
  sums = vec_add(sums,s1);
  carries = vec_add(carries,s2);

  /* Extract sum and carries from right-hand words and combine into result */
  s.iv = sums;
  sum = s.w[3];

  s.iv = carries;
  sum += (long long)s.w[3] << 32;

  return sum;
}