1/* K=7 r=1/2 Viterbi decoder for SSE2
2 * Feb 2004, Phil Karn, KA9Q
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <memory.h>
7#include <xmmintrin.h>
8#include "fec.h"
9
10typedef union { unsigned char c[64]; __m128i v[4]; } metric_t;
11typedef union { unsigned long w[2]; unsigned char c[8]; unsigned short s[4]; __m64 v[1];} decision_t;
12union branchtab27 { unsigned char c[32]; __m128i v[2];} Branchtab27_sse2[2];
13static int Init = 0;
14
15/* State info for instance of Viterbi decoder
16 * Don't change this without also changing references in sse2bfly27.s!
17 */
18struct v27 {
19  metric_t metrics1; /* path metric buffer 1 */
20  metric_t metrics2; /* path metric buffer 2 */
21  decision_t *dp;          /* Pointer to current decision */
22  metric_t *old_metrics,*new_metrics; /* Pointers to path metrics, swapped on every bit */
23  decision_t *decisions;   /* Beginning of decisions for block */
24};
25
26/* Initialize Viterbi decoder for start of new frame */
27int init_viterbi27_sse2(void *p,int starting_state){
28  struct v27 *vp = p;
29  int i;
30
31  if(p == NULL)
32    return -1;
33  for(i=0;i<64;i++)
34    vp->metrics1.c[i] = 63;
35
36  vp->old_metrics = &vp->metrics1;
37  vp->new_metrics = &vp->metrics2;
38  vp->dp = vp->decisions;
39  vp->old_metrics->c[starting_state & 63] = 0; /* Bias known start state */
40  return 0;
41}
42
43void set_viterbi27_polynomial_sse2(int polys[2]){
44  int state;
45
46  for(state=0;state < 32;state++){
47    Branchtab27_sse2[0].c[state] = (polys[0] < 0) ^ parity((2*state) & abs(polys[0])) ? 255 : 0;
48    Branchtab27_sse2[1].c[state] = (polys[1] < 0) ^ parity((2*state) & abs(polys[1])) ? 255 : 0;
49  }
50  Init++;
51}
52
53
54/* Create a new instance of a Viterbi decoder */
55void *create_viterbi27_sse2(int len){
56  void *p;
57  struct v27 *vp;
58
59  if(!Init){
60    int polys[2] = { V27POLYA, V27POLYB };
61    set_viterbi27_polynomial_sse2(polys);
62  }
63  /* Ordinary malloc() only returns 8-byte alignment, we need 16 */
64  if(posix_memalign(&p, sizeof(__m128i),sizeof(struct v27)))
65    return NULL;
66  vp = (struct v27 *)p;
67
68  if((p = malloc((len+6)*sizeof(decision_t))) == NULL){
69    free(vp);
70    return NULL;
71  }
72  vp->decisions = (decision_t *)p;
73  init_viterbi27_sse2(vp,0);
74
75  return vp;
76}
77
78/* Viterbi chainback */
79int chainback_viterbi27_sse2(
80      void *p,
81      unsigned char *data, /* Decoded output data */
82      unsigned int nbits, /* Number of data bits */
83      unsigned int endstate){ /* Terminal encoder state */
84  struct v27 *vp = p;
85  decision_t *d;
86
87  if(p == NULL)
88    return -1;
89  d = vp->decisions;
90  /* Make room beyond the end of the encoder register so we can
91   * accumulate a full byte of decoded data
92   */
93  endstate %= 64;
94  endstate <<= 2;
95
96  /* The store into data[] only needs to be done every 8 bits.
97   * But this avoids a conditional branch, and the writes will
98   * combine in the cache anyway
99   */
100  d += 6; /* Look past tail */
101  while(nbits-- != 0){
102    int k;
103
104    k = (d[nbits].c[(endstate>>2)/8] >> ((endstate>>2)%8)) & 1;
105    data[nbits>>3] = endstate = (endstate >> 1) | (k << 7);
106  }
107  return 0;
108}
109
110/* Delete instance of a Viterbi decoder */
111void delete_viterbi27_sse2(void *p){
112  struct v27 *vp = p;
113
114  if(vp != NULL){
115    free(vp->decisions);
116    free(vp);
117  }
118}
119
120
121#if 0
122/* This code is turned off because it's slower than my hand-crafted assembler in sse2bfly27.s. But it does work. */
123void update_viterbi27_blk_sse2(void *p,unsigned char *syms,int nbits){
124  struct v27 *vp = p;
125  decision_t *d;
126
127  if(p == NULL)
128    return;
129  d = (decision_t *)vp->dp;
130  while(nbits--){
131    __m128i sym0v,sym1v;
132    void *tmp;
133    int i;
134
135    /* Splat the 0th symbol across sym0v, the 1st symbol across sym1v, etc */
136    sym0v = _mm_set1_epi8(syms[0]);
137    sym1v = _mm_set1_epi8(syms[1]);
138    syms += 2;
139
140    for(i=0;i<2;i++){
141      __m128i decision0,decision1,metric,m_metric,m0,m1,m2,m3,survivor0,survivor1;
142
143      /* Form branch metrics */
144      metric = _mm_avg_epu8(_mm_xor_si128(Branchtab27_sse2[0].v[i],sym0v),_mm_xor_si128(Branchtab27_sse2[1].v[i],sym1v));
145      /* There's no packed bytes right shift in SSE2, so we use the word version and mask
146       * (I'm *really* starting to like Altivec...)
147       */
148      metric = _mm_srli_epi16(metric,3);
149      metric = _mm_and_si128(metric,_mm_set1_epi8(31));
150      m_metric = _mm_sub_epi8(_mm_set1_epi8(31),metric);
151
152      /* Add branch metrics to path metrics */
153      m0 = _mm_add_epi8(vp->old_metrics->v[i],metric);
154      m3 = _mm_add_epi8(vp->old_metrics->v[2+i],metric);
155      m1 = _mm_add_epi8(vp->old_metrics->v[2+i],m_metric);
156      m2 = _mm_add_epi8(vp->old_metrics->v[i],m_metric);
157
158      /* Compare and select, using modulo arithmetic */
159      decision0 = _mm_cmpgt_epi8(_mm_sub_epi8(m0,m1),_mm_setzero_si128());
160      decision1 = _mm_cmpgt_epi8(_mm_sub_epi8(m2,m3),_mm_setzero_si128());
161      survivor0 = _mm_or_si128(_mm_and_si128(decision0,m1),_mm_andnot_si128(decision0,m0));
162      survivor1 = _mm_or_si128(_mm_and_si128(decision1,m3),_mm_andnot_si128(decision1,m2));
163
164      /* Pack each set of decisions into 16 bits */
165      d->s[2*i] = _mm_movemask_epi8(_mm_unpacklo_epi8(decision0,decision1));
166      d->s[2*i+1] = _mm_movemask_epi8(_mm_unpackhi_epi8(decision0,decision1));
167
168      /* Store surviving metrics */
169      vp->new_metrics->v[2*i] = _mm_unpacklo_epi8(survivor0,survivor1);
170      vp->new_metrics->v[2*i+1] = _mm_unpackhi_epi8(survivor0,survivor1);
171    }
172    d++;
173    /* Swap pointers to old and new metrics */
174    tmp = vp->old_metrics;
175    vp->old_metrics = vp->new_metrics;
176    vp->new_metrics = tmp;
177  }
178  vp->dp = d;
179}
180#endif
181