• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* K=9 r=1/3 Viterbi decoder for x86 SSE
2  * Copyright Aug 2006, Phil Karn, KA9Q
3  * May be used under the terms of the GNU Lesser General Public License (LGPL)
4  */
5 #include <xmmintrin.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <memory.h>
9 #include <limits.h>
10 #include "fec.h"
11 
12 typedef union { unsigned long w[8]; unsigned char c[32];} decision_t;
13 typedef union { signed short s[256]; __m64 v[64];} metric_t;
14 
15 static union branchtab39 { unsigned short s[128]; __m64 v[32];} Branchtab39[3];
16 static int Init = 0;
17 
18 /* State info for instance of Viterbi decoder */
19 struct v39 {
20   metric_t metrics1; /* path metric buffer 1 */
21   metric_t metrics2; /* path metric buffer 2 */
22   void *dp;          /* Pointer to current decision */
23   metric_t *old_metrics,*new_metrics; /* Pointers to path metrics, swapped on every bit */
24   void *decisions;   /* Beginning of decisions for block */
25 };
26 
27 /* Initialize Viterbi decoder for start of new frame */
init_viterbi39_sse(void * p,int starting_state)28 int init_viterbi39_sse(void *p,int starting_state){
29   struct v39 *vp = p;
30   int i;
31 
32   if(p == NULL)
33     return -1;
34   for(i=0;i<256;i++)
35     vp->metrics1.s[i] = (SHRT_MIN+1000);
36 
37   vp->old_metrics = &vp->metrics1;
38   vp->new_metrics = &vp->metrics2;
39   vp->dp = vp->decisions;
40   vp->old_metrics->s[starting_state & 255] = SHRT_MIN; /* Bias known start state */
41   return 0;
42 }
43 
44 /* Create a new instance of a Viterbi decoder */
create_viterbi39_sse(int len)45 void *create_viterbi39_sse(int len){
46   struct v39 *vp;
47 
48   if(!Init){
49     int polys[3] = { V39POLYA, V39POLYB, V39POLYC };
50 
51     set_viterbi39_polynomial_sse(polys);
52   }
53   if((vp = (struct v39 *)malloc(sizeof(struct v39))) == NULL){
54     return NULL;
55   }
56   if((vp->decisions = malloc((len+8)*sizeof(decision_t))) == NULL){
57     free(vp);
58     return NULL;
59   }
60   init_viterbi39_sse(vp,0);
61   return vp;
62 }
63 
set_viterbi39_polynomial_sse(int polys[3])64 void set_viterbi39_polynomial_sse(int polys[3]){
65   int state;
66 
67   for(state=0;state < 128;state++){
68     Branchtab39[0].s[state] = (polys[0] < 0) ^ parity((2*state) & polys[0]) ? 255:0;
69     Branchtab39[1].s[state] = (polys[1] < 0) ^ parity((2*state) & polys[1]) ? 255:0;
70     Branchtab39[2].s[state] = (polys[2] < 0) ^ parity((2*state) & polys[2]) ? 255:0;
71   }
72   Init++;
73 }
74 
75 /* Viterbi chainback */
chainback_viterbi39_sse(void * p,unsigned char * data,unsigned int nbits,unsigned int endstate)76 int chainback_viterbi39_sse(
77       void *p,
78       unsigned char *data, /* Decoded output data */
79       unsigned int nbits, /* Number of data bits */
80       unsigned int endstate){ /* Terminal encoder state */
81   struct v39 *vp = p;
82   decision_t *d;
83   int path_metric;
84 
85   if(p == NULL)
86     return -1;
87   d = (decision_t *)vp->decisions;
88   endstate %= 256;
89 
90   path_metric = vp->old_metrics->s[endstate];
91 
92   /* The store into data[] only needs to be done every 8 bits.
93    * But this avoids a conditional branch, and the writes will
94    * combine in the cache anyway
95    */
96   d += 8; /* Look past tail */
97   while(nbits-- != 0){
98     int k;
99 
100     /*    k = (d[nbits].w[endstate/32] >> (endstate%32)) & 1;*/
101     k = (d[nbits].c[endstate/8] >> (endstate%8)) & 1;
102     endstate = (k << 7) | (endstate >> 1);
103     data[nbits>>3] = endstate;
104   }
105   return path_metric - SHRT_MIN;
106 }
107 
108 /* Delete instance of a Viterbi decoder */
delete_viterbi39_sse(void * p)109 void delete_viterbi39_sse(void *p){
110   struct v39 *vp = p;
111 
112   if(vp != NULL){
113     free(vp->decisions);
114     free(vp);
115   }
116 }
117 
118 
update_viterbi39_blk_sse(void * p,unsigned char * syms,int nbits)119 int update_viterbi39_blk_sse(void *p,unsigned char *syms,int nbits){
120   struct v39 *vp = p;
121   decision_t *d;
122   int path_metric = 0;
123 
124   if(p == NULL)
125     return -1;
126   d = (decision_t *)vp->dp;
127   while(nbits--){
128     __m64 sym0v,sym1v,sym2v;
129     void *tmp;
130     int i;
131 
132     /* Splat the 0th symbol across sym0v, the 1st symbol across sym1v, etc */
133     sym0v = _mm_set1_pi16(syms[0]);
134     sym1v = _mm_set1_pi16(syms[1]);
135     sym2v = _mm_set1_pi16(syms[2]);
136     syms += 3;
137 
138     for(i=0;i<32;i++){
139       __m64 decision0,decision1,metric,m_metric,m0,m1,m2,m3,survivor0,survivor1;
140 
141       /* Form branch metrics
142        * Because Branchtab takes on values 0 and 255, and the values of sym?v are offset binary in the range 0-255,
143        * the XOR operations constitute conditional negation.
144        * metric and m_metric (-metric) are in the range 0-765
145        */
146       m0 = _mm_add_pi16(_mm_xor_si64(Branchtab39[0].v[i],sym0v),_mm_xor_si64(Branchtab39[1].v[i],sym1v));
147       metric = _mm_add_pi16(_mm_xor_si64(Branchtab39[2].v[i],sym2v),m0);
148       m_metric = _mm_sub_pi16(_mm_set1_pi16(765),metric);
149 
150       /* Add branch metrics to path metrics */
151       m0 = _mm_adds_pi16(vp->old_metrics->v[i],metric);
152       m3 = _mm_adds_pi16(vp->old_metrics->v[32+i],metric);
153       m1 = _mm_adds_pi16(vp->old_metrics->v[32+i],m_metric);
154       m2 = _mm_adds_pi16(vp->old_metrics->v[i],m_metric);
155 
156       /* Compare and select */
157       survivor0 = _mm_min_pi16(m0,m1);
158       survivor1 = _mm_min_pi16(m2,m3);
159       decision0 = _mm_cmpeq_pi16(survivor0,m1);
160       decision1 = _mm_cmpeq_pi16(survivor1,m3);
161 
162       /* Pack decisions into 8 bits and store */
163       d->c[i] = _mm_movemask_pi8(_mm_unpacklo_pi8(_mm_packs_pi16(decision0,_mm_setzero_si64()),_mm_packs_pi16(decision1,_mm_setzero_si64())));
164 
165       /* Store surviving metrics */
166       vp->new_metrics->v[2*i] = _mm_unpacklo_pi16(survivor0,survivor1);
167       vp->new_metrics->v[2*i+1] = _mm_unpackhi_pi16(survivor0,survivor1);
168     }
169     /* See if we need to renormalize
170      * Max metric spread for this code with 0-255 branch metrics is 12750
171      */
172     if(vp->new_metrics->s[0] >= SHRT_MAX-5000){
173       int i,adjust;
174       __m64 adjustv;
175       union { __m64 v; signed short w[4]; } t;
176 
177       /* Find smallest metric and set adjustv to bring it down to SHRT_MIN */
178       adjustv = vp->new_metrics->v[0];
179       for(i=1;i<64;i++)
180 	adjustv = _mm_min_pi16(adjustv,vp->new_metrics->v[i]);
181 
182       adjustv = _mm_min_pi16(adjustv,_mm_srli_si64(adjustv,32));
183       adjustv = _mm_min_pi16(adjustv,_mm_srli_si64(adjustv,16));
184       t.v = adjustv;
185       adjust = t.w[0] - SHRT_MIN;
186       path_metric += adjust;
187       adjustv = _mm_set1_pi16(adjust);
188 
189       for(i=0;i<64;i++)
190 	vp->new_metrics->v[i] = _mm_sub_pi16(vp->new_metrics->v[i],adjustv);
191     }
192     d++;
193     /* Swap pointers to old and new metrics */
194     tmp = vp->old_metrics;
195     vp->old_metrics = vp->new_metrics;
196     vp->new_metrics = tmp;
197   }
198   vp->dp = d;
199   _mm_empty();
200   return path_metric;
201 }
202