blob: 89a56f76efe10fa335e13fbb8377e2ddc0e9dc82 [file] [log] [blame]
Bill Yi4e213d52015-06-23 13:53:11 -07001/* K=15 r=1/6 Viterbi decoder for x86 MMX
2 * Mar 2004, Phil Karn, KA9Q
3 * May be used under the terms of the GNU Lesser General Public License (LGPL)
4 */
5#include <mmintrin.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <memory.h>
9#include "fec.h"
10
11typedef union { unsigned char c[16384]; __m64 v[2048];} decision_t;
12typedef union { unsigned short s[16384]; __m64 v[4096];} metric_t;
13
14static union branchtab615 { unsigned short s[8192]; __m64 v[2048];} Branchtab615[6];
15static int Init = 0;
16
17/* State info for instance of Viterbi decoder */
18struct v615 {
19 metric_t metrics1; /* path metric buffer 1 */
20 metric_t metrics2; /* path metric buffer 2 */
21 void *dp; /* Pointer to current decision */
22 metric_t *old_metrics,*new_metrics; /* Pointers to path metrics, swapped on every bit */
23 void *decisions; /* Beginning of decisions for block */
24};
25
26/* Initialize Viterbi decoder for start of new frame */
27int init_viterbi615_mmx(void *p,int starting_state){
28 struct v615 *vp = p;
29 int i;
30
31 if(p == NULL)
32 return -1;
33 for(i=0;i<16384;i++)
34 vp->metrics1.s[i] = 5000;
35
36 vp->old_metrics = &vp->metrics1;
37 vp->new_metrics = &vp->metrics2;
38 vp->dp = vp->decisions;
39 vp->old_metrics->s[starting_state & 16383] = 0; /* Bias known start state */
40 return 0;
41}
42
43/* Create a new instance of a Viterbi decoder */
44void *create_viterbi615_mmx(int len){
45 struct v615 *vp;
46
47 if(!Init){
48 int polys[6] = { V615POLYA,V615POLYB,V615POLYC,V615POLYD,V615POLYE,V615POLYF };
49 set_viterbi615_polynomial_mmx(polys);
50 }
51
52 if((vp = (struct v615 *)malloc(sizeof(struct v615))) == NULL)
53 return NULL;
54 if((vp->decisions = malloc((len+14)*sizeof(decision_t))) == NULL){
55 free(vp);
56 return NULL;
57 }
58 init_viterbi615_mmx(vp,0);
59 return vp;
60}
61
62void set_viterbi615_polynomial_mmx(int polys[6]){
63 int state;
64 int i;
65
66 for(state=0;state < 8192;state++){
67 for(i=0;i<6;i++)
68 Branchtab615[i].s[state] = (polys[i] < 0) ^ parity((2*state) & abs(polys[i])) ? 255 : 0;
69 }
70 Init++;
71}
72
73/* Viterbi chainback */
74int chainback_viterbi615_mmx(
75 void *p,
76 unsigned char *data, /* Decoded output data */
77 unsigned int nbits, /* Number of data bits */
78 unsigned int endstate){ /* Terminal encoder state */
79 struct v615 *vp = p;
80 decision_t *d;
81
82 if(p == NULL)
83 return -1;
84
85 d = (decision_t *)vp->decisions;
86
87 endstate %= 16384;
88
89 /* The store into data[] only needs to be done every 8 bits.
90 * But this avoids a conditional branch, and the writes will
91 * combine in the cache anyway
92 */
93 d += 14; /* Look past tail */
94 while(nbits-- != 0){
95 int k;
96
97 k = d[nbits].c[endstate] & 1;
98 endstate = (k << 13) | (endstate >> 1);
99 data[nbits>>3] = endstate >> 6;
100 }
101 return 0;
102}
103
104/* Delete instance of a Viterbi decoder */
105void delete_viterbi615_mmx(void *p){
106 struct v615 *vp = p;
107
108 if(vp != NULL){
109 free(vp->decisions);
110 free(vp);
111 }
112}
113
114
115int update_viterbi615_blk_mmx(void *p,unsigned char *syms,int nbits){
116 struct v615 *vp = p;
117 decision_t *d;
118
119 if(p == NULL)
120 return -1;
121
122 d = (decision_t *)vp->dp;
123
124 while(nbits--){
125 __m64 sym0v,sym1v,sym2v,sym3v,sym4v,sym5v;
126 void *tmp;
127 int i;
128
129 /* Splat the 0th symbol across sym0v, the 1st symbol across sym1v, etc */
130 sym0v = _mm_set1_pi16(syms[0]);
131 sym1v = _mm_set1_pi16(syms[1]);
132 sym2v = _mm_set1_pi16(syms[2]);
133 sym3v = _mm_set1_pi16(syms[3]);
134 sym4v = _mm_set1_pi16(syms[4]);
135 sym5v = _mm_set1_pi16(syms[5]);
136 syms += 6;
137
138 for(i=0;i<2048;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-1530
145 */
146 m0 = _mm_add_pi16(_mm_xor_si64(Branchtab615[0].v[i],sym0v),_mm_xor_si64(Branchtab615[1].v[i],sym1v));
147 m1 = _mm_add_pi16(_mm_xor_si64(Branchtab615[2].v[i],sym2v),_mm_xor_si64(Branchtab615[3].v[i],sym3v));
148 m2 = _mm_add_pi16(_mm_xor_si64(Branchtab615[4].v[i],sym4v),_mm_xor_si64(Branchtab615[5].v[i],sym5v));
149 metric = _mm_add_pi16(m0,_mm_add_pi16(m1,m2));
150 m_metric = _mm_sub_pi16(_mm_set1_pi16(1530),metric);
151
152 /* Add branch metrics to path metrics */
153 m0 = _mm_add_pi16(vp->old_metrics->v[i],metric);
154 m3 = _mm_add_pi16(vp->old_metrics->v[2048+i],metric);
155 m1 = _mm_add_pi16(vp->old_metrics->v[2048+i],m_metric);
156 m2 = _mm_add_pi16(vp->old_metrics->v[i],m_metric);
157
158 /* Compare and select
159 * There's no packed min instruction in MMX, so we use modulo arithmetic
160 * to form the decisions and then do the select the hard way
161 */
162 decision0 = _mm_cmpgt_pi16(_mm_sub_pi16(m0,m1),_mm_setzero_si64());
163 decision1 = _mm_cmpgt_pi16(_mm_sub_pi16(m2,m3),_mm_setzero_si64());
164 survivor0 = _mm_or_si64(_mm_and_si64(decision0,m1),_mm_andnot_si64(decision0,m0));
165 survivor1 = _mm_or_si64(_mm_and_si64(decision1,m3),_mm_andnot_si64(decision1,m2));
166
167 /* Merge decisions and store as bytes */
168 d->v[i] = _mm_unpacklo_pi8(_mm_packs_pi16(decision0,_mm_setzero_si64()),_mm_packs_pi16(decision1,_mm_setzero_si64()));
169
170 /* Store surviving metrics */
171 vp->new_metrics->v[2*i] = _mm_unpacklo_pi16(survivor0,survivor1);
172 vp->new_metrics->v[2*i+1] = _mm_unpackhi_pi16(survivor0,survivor1);
173 }
174 d++;
175 /* Swap pointers to old and new metrics */
176 tmp = vp->old_metrics;
177 vp->old_metrics = vp->new_metrics;
178 vp->new_metrics = tmp;
179 }
180 vp->dp = d;
181 _mm_empty();
182 return 0;
183}