1 /* K=15 r=1/6 Viterbi decoder for x86 SSE2
2 * Copyright Mar 2004, Phil Karn, KA9Q
3 * May be used under the terms of the GNU Lesser General Public License (LGPL)
4 */
5 #include <emmintrin.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[512]; unsigned short s[1024];} decision_t;
13 typedef union { signed short s[16384]; __m128i v[2048];} metric_t;
14
15 static union branchtab615 { unsigned short s[8192]; __m128i v[1024];} Branchtab615[6];
16 static int Init = 0;
17
18 /* State info for instance of Viterbi decoder */
19 struct v615 {
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_viterbi615_sse2(void * p,int starting_state)28 int init_viterbi615_sse2(void *p,int starting_state){
29 struct v615 *vp = p;
30 int i;
31
32 if(p == NULL)
33 return -1;
34 for(i=0;i<16384;i++)
35 vp->metrics1.s[i] = (SHRT_MIN+5000);
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 & 16383] = SHRT_MIN; /* Bias known start state */
41 return 0;
42 }
43
44 /* Create a new instance of a Viterbi decoder */
create_viterbi615_sse2(int len)45 void *create_viterbi615_sse2(int len){
46 void *p;
47 struct v615 *vp;
48
49 if(!Init){
50 int polys[6] = { V615POLYA,V615POLYB,V615POLYC,V615POLYD,V615POLYE,V615POLYF };
51 set_viterbi615_polynomial_sse2(polys);
52 }
53
54 /* Ordinary malloc() only returns 8-byte alignment, we need 16 */
55 if(posix_memalign(&p, sizeof(__m128i),sizeof(struct v615)))
56 return NULL;
57
58 vp = (struct v615 *)p;
59 if((p = malloc((len+14)*sizeof(decision_t))) == NULL){
60 free(vp);
61 return NULL;
62 }
63 vp->decisions = (decision_t *)p;
64 init_viterbi615_sse2(vp,0);
65 return vp;
66 }
67
set_viterbi615_polynomial_sse2(int polys[6])68 void set_viterbi615_polynomial_sse2(int polys[6]){
69 int state;
70 int i;
71
72 for(state=0;state < 8192;state++){
73 for(i=0;i<6;i++)
74 Branchtab615[i].s[state] = (polys[i] < 0) ^ parity((2*state) & abs(polys[i])) ? 255 : 0;
75 }
76 Init++;
77 }
78
79 /* Viterbi chainback */
chainback_viterbi615_sse2(void * p,unsigned char * data,unsigned int nbits,unsigned int endstate)80 int chainback_viterbi615_sse2(
81 void *p,
82 unsigned char *data, /* Decoded output data */
83 unsigned int nbits, /* Number of data bits */
84 unsigned int endstate){ /* Terminal encoder state */
85 struct v615 *vp = p;
86 decision_t *d = (decision_t *)vp->decisions;
87
88 endstate %= 16384;
89
90 /* The store into data[] only needs to be done every 8 bits.
91 * But this avoids a conditional branch, and the writes will
92 * combine in the cache anyway
93 */
94 d += 14; /* Look past tail */
95 while(nbits-- != 0){
96 int k;
97
98 k = (d[nbits].w[endstate/32] >> (endstate%32)) & 1;
99 endstate = (k << 13) | (endstate >> 1);
100 data[nbits>>3] = endstate >> 6;
101 }
102 return 0;
103 }
104
105 /* Delete instance of a Viterbi decoder */
delete_viterbi615_sse2(void * p)106 void delete_viterbi615_sse2(void *p){
107 struct v615 *vp = p;
108
109 if(vp != NULL){
110 free(vp->decisions);
111 free(vp);
112 }
113 }
114
115
update_viterbi615_blk_sse2(void * p,unsigned char * syms,int nbits)116 int update_viterbi615_blk_sse2(void *p,unsigned char *syms,int nbits){
117 struct v615 *vp = p;
118 decision_t *d = (decision_t *)vp->dp;
119
120 while(nbits--){
121 __m128i sym0v,sym1v,sym2v,sym3v,sym4v,sym5v;
122 void *tmp;
123 int i;
124
125 /* Splat the 0th symbol across sym0v, the 1st symbol across sym1v, etc */
126 sym0v = _mm_set1_epi16(syms[0]);
127 sym1v = _mm_set1_epi16(syms[1]);
128 sym2v = _mm_set1_epi16(syms[2]);
129 sym3v = _mm_set1_epi16(syms[3]);
130 sym4v = _mm_set1_epi16(syms[4]);
131 sym5v = _mm_set1_epi16(syms[5]);
132 syms += 6;
133
134 /* SSE2 doesn't support saturated adds on unsigned shorts, so we have to use signed shorts */
135 for(i=0;i<1024;i++){
136 __m128i decision0,decision1,metric,m_metric,m0,m1,m2,m3,survivor0,survivor1;
137
138 /* Form branch metrics
139 * Because Branchtab takes on values 0 and 255, and the values of sym?v are offset binary in the range 0-255,
140 * the XOR operations constitute conditional negation.
141 * metric and m_metric (-metric) are in the range 0-1530
142 */
143 m0 = _mm_add_epi16(_mm_xor_si128(Branchtab615[0].v[i],sym0v),_mm_xor_si128(Branchtab615[1].v[i],sym1v));
144 m1 = _mm_add_epi16(_mm_xor_si128(Branchtab615[2].v[i],sym2v),_mm_xor_si128(Branchtab615[3].v[i],sym3v));
145 m2 = _mm_add_epi16(_mm_xor_si128(Branchtab615[4].v[i],sym4v),_mm_xor_si128(Branchtab615[5].v[i],sym5v));
146 metric = _mm_add_epi16(m0,_mm_add_epi16(m1,m2));
147 m_metric = _mm_sub_epi16(_mm_set1_epi16(1530),metric);
148
149 /* Add branch metrics to path metrics */
150 m0 = _mm_adds_epi16(vp->old_metrics->v[i],metric);
151 m3 = _mm_adds_epi16(vp->old_metrics->v[1024+i],metric);
152 m1 = _mm_adds_epi16(vp->old_metrics->v[1024+i],m_metric);
153 m2 = _mm_adds_epi16(vp->old_metrics->v[i],m_metric);
154
155 /* Compare and select */
156 survivor0 = _mm_min_epi16(m0,m1);
157 survivor1 = _mm_min_epi16(m2,m3);
158 decision0 = _mm_cmpeq_epi16(survivor0,m1);
159 decision1 = _mm_cmpeq_epi16(survivor1,m3);
160
161 /* Pack each set of decisions into 8 8-bit bytes, then interleave them and compress into 16 bits */
162 d->s[i] = _mm_movemask_epi8(_mm_unpacklo_epi8(_mm_packs_epi16(decision0,_mm_setzero_si128()),_mm_packs_epi16(decision1,_mm_setzero_si128())));
163
164 /* Store surviving metrics */
165 vp->new_metrics->v[2*i] = _mm_unpacklo_epi16(survivor0,survivor1);
166 vp->new_metrics->v[2*i+1] = _mm_unpackhi_epi16(survivor0,survivor1);
167 }
168 /* See if we need to renormalize
169 * Max metric spread for this code with 0-90 branch metrics is 405
170 */
171 if(vp->new_metrics->s[0] >= SHRT_MAX-12750){
172 int i,adjust;
173 __m128i adjustv;
174 union { __m128i v; signed short w[8]; } t;
175
176 /* Find smallest metric and set adjustv to bring it down to SHRT_MIN */
177 adjustv = vp->new_metrics->v[0];
178 for(i=1;i<2048;i++)
179 adjustv = _mm_min_epi16(adjustv,vp->new_metrics->v[i]);
180
181 adjustv = _mm_min_epi16(adjustv,_mm_srli_si128(adjustv,8));
182 adjustv = _mm_min_epi16(adjustv,_mm_srli_si128(adjustv,4));
183 adjustv = _mm_min_epi16(adjustv,_mm_srli_si128(adjustv,2));
184 t.v = adjustv;
185 adjust = t.w[0] - SHRT_MIN;
186 adjustv = _mm_set1_epi16(adjust);
187
188 /* We cannot use a saturated subtract, because we often have to adjust by more than SHRT_MAX
189 * This is okay since it can't overflow anyway
190 */
191 for(i=0;i<2048;i++)
192 vp->new_metrics->v[i] = _mm_sub_epi16(vp->new_metrics->v[i],adjustv);
193 }
194 d++;
195 /* Swap pointers to old and new metrics */
196 tmp = vp->old_metrics;
197 vp->old_metrics = vp->new_metrics;
198 vp->new_metrics = tmp;
199 }
200 vp->dp = d;
201 return 0;
202 }
203
204
205