xref: /aosp_15_r20/external/fec/dotprod_mmx.c (revision 638691a093b4f9473cd6ee8f3e0139deef159a86)
1*638691a0SAndroid Build Coastguard Worker /* 16-bit signed integer dot product
2*638691a0SAndroid Build Coastguard Worker  * MMX assisted version; also for SSE
3*638691a0SAndroid Build Coastguard Worker  *
4*638691a0SAndroid Build Coastguard Worker  * Copyright 2004 Phil Karn
5*638691a0SAndroid Build Coastguard Worker  * May be used under the terms of the GNU Lesser General Public License (LGPL)
6*638691a0SAndroid Build Coastguard Worker  */
7*638691a0SAndroid Build Coastguard Worker #include <stdlib.h>
8*638691a0SAndroid Build Coastguard Worker #include "fec.h"
9*638691a0SAndroid Build Coastguard Worker 
10*638691a0SAndroid Build Coastguard Worker struct dotprod {
11*638691a0SAndroid Build Coastguard Worker   int len; /* Number of coefficients */
12*638691a0SAndroid Build Coastguard Worker 
13*638691a0SAndroid Build Coastguard Worker   /* On a MMX or SSE machine, these hold 4 copies of the coefficients,
14*638691a0SAndroid Build Coastguard Worker    * preshifted by 0,1,2,3 words to meet all possible input data
15*638691a0SAndroid Build Coastguard Worker    * alignments (see Intel ap559 on MMX dot products).
16*638691a0SAndroid Build Coastguard Worker    */
17*638691a0SAndroid Build Coastguard Worker   signed short *coeffs[4];
18*638691a0SAndroid Build Coastguard Worker };
19*638691a0SAndroid Build Coastguard Worker long dotprod_mmx_assist(signed short *a,signed short *b,int cnt);
20*638691a0SAndroid Build Coastguard Worker 
21*638691a0SAndroid Build Coastguard Worker /* Create and return a descriptor for use with the dot product function */
initdp_mmx(signed short coeffs[],int len)22*638691a0SAndroid Build Coastguard Worker void *initdp_mmx(signed short coeffs[],int len){
23*638691a0SAndroid Build Coastguard Worker   struct dotprod *dp;
24*638691a0SAndroid Build Coastguard Worker   int i,j;
25*638691a0SAndroid Build Coastguard Worker 
26*638691a0SAndroid Build Coastguard Worker 
27*638691a0SAndroid Build Coastguard Worker   if(len == 0)
28*638691a0SAndroid Build Coastguard Worker     return NULL;
29*638691a0SAndroid Build Coastguard Worker 
30*638691a0SAndroid Build Coastguard Worker   dp = (struct dotprod *)calloc(1,sizeof(struct dotprod));
31*638691a0SAndroid Build Coastguard Worker   dp->len = len;
32*638691a0SAndroid Build Coastguard Worker 
33*638691a0SAndroid Build Coastguard Worker   /* Make 4 copies of coefficients, one for each data alignment */
34*638691a0SAndroid Build Coastguard Worker   for(i=0;i<4;i++){
35*638691a0SAndroid Build Coastguard Worker     dp->coeffs[i] = (signed short *)calloc(1+(len+i-1)/4,
36*638691a0SAndroid Build Coastguard Worker 					   4*sizeof(signed short));
37*638691a0SAndroid Build Coastguard Worker     for(j=0;j<len;j++)
38*638691a0SAndroid Build Coastguard Worker       dp->coeffs[i][j+i] = coeffs[j];
39*638691a0SAndroid Build Coastguard Worker   }
40*638691a0SAndroid Build Coastguard Worker   return (void *)dp;
41*638691a0SAndroid Build Coastguard Worker }
42*638691a0SAndroid Build Coastguard Worker 
43*638691a0SAndroid Build Coastguard Worker 
44*638691a0SAndroid Build Coastguard Worker /* Free a dot product descriptor created earlier */
freedp_mmx(void * p)45*638691a0SAndroid Build Coastguard Worker void freedp_mmx(void *p){
46*638691a0SAndroid Build Coastguard Worker   struct dotprod *dp = (struct dotprod *)p;
47*638691a0SAndroid Build Coastguard Worker   int i;
48*638691a0SAndroid Build Coastguard Worker 
49*638691a0SAndroid Build Coastguard Worker   for(i=0;i<4;i++)
50*638691a0SAndroid Build Coastguard Worker     if(dp->coeffs[i] != NULL)
51*638691a0SAndroid Build Coastguard Worker       free(dp->coeffs[i]);
52*638691a0SAndroid Build Coastguard Worker   free(dp);
53*638691a0SAndroid Build Coastguard Worker }
54*638691a0SAndroid Build Coastguard Worker 
55*638691a0SAndroid Build Coastguard Worker /* Compute a dot product given a descriptor and an input array
56*638691a0SAndroid Build Coastguard Worker  * The length is taken from the descriptor
57*638691a0SAndroid Build Coastguard Worker  */
dotprod_mmx(void * p,signed short a[])58*638691a0SAndroid Build Coastguard Worker long dotprod_mmx(void *p,signed short a[]){
59*638691a0SAndroid Build Coastguard Worker   struct dotprod *dp = (struct dotprod *)p;
60*638691a0SAndroid Build Coastguard Worker   int al;
61*638691a0SAndroid Build Coastguard Worker   signed short *ar;
62*638691a0SAndroid Build Coastguard Worker 
63*638691a0SAndroid Build Coastguard Worker   /* Round input data address down to 8 byte boundary
64*638691a0SAndroid Build Coastguard Worker    * NB: depending on the alignment of a[], memory
65*638691a0SAndroid Build Coastguard Worker    * before a[] will be accessed. The contents don't matter since they'll
66*638691a0SAndroid Build Coastguard Worker    * be multiplied by zero coefficients. I can't conceive of any
67*638691a0SAndroid Build Coastguard Worker    * situation where this could cause a segfault since memory protection
68*638691a0SAndroid Build Coastguard Worker    * in the x86 machines is done on much larger boundaries
69*638691a0SAndroid Build Coastguard Worker    */
70*638691a0SAndroid Build Coastguard Worker   ar = (signed short *)((int)a & ~7);
71*638691a0SAndroid Build Coastguard Worker 
72*638691a0SAndroid Build Coastguard Worker   /* Choose one of 4 sets of pre-shifted coefficients. al is both the
73*638691a0SAndroid Build Coastguard Worker    * index into dp->coeffs[] and the number of 0 words padded onto
74*638691a0SAndroid Build Coastguard Worker    * that coefficients array for alignment purposes
75*638691a0SAndroid Build Coastguard Worker    */
76*638691a0SAndroid Build Coastguard Worker   al = a - ar;
77*638691a0SAndroid Build Coastguard Worker 
78*638691a0SAndroid Build Coastguard Worker   /* Call assembler routine to do the work, passing number of 4-word blocks */
79*638691a0SAndroid Build Coastguard Worker   return dotprod_mmx_assist(ar,dp->coeffs[al],(dp->len+al-1)/4+1);
80*638691a0SAndroid Build Coastguard Worker }
81*638691a0SAndroid Build Coastguard Worker 
82