1 /* K=9 r=1/2 Viterbi decoder for SSE2 2 * Copyright Feb 2004, Phil Karn, KA9Q 3 * May be used under the terms of the GNU Lesser General Public License (LGPL) 4 */ 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <memory.h> 8 #include <emmintrin.h> 9 #include "fec.h" 10 11 typedef union { unsigned char c[256]; __m128i v[16];} metric_t; 12 typedef union { unsigned long w[8]; unsigned char c[32];} decision_t; 13 14 union branchtab29 { unsigned char c[128]; } Branchtab29_sse2[2]; 15 static int Init = 0; 16 17 /* State info for instance of Viterbi decoder 18 * Don't change this without also changing references in sse2bfly29.s! 19 */ 20 struct v29 { 21 metric_t metrics1; /* path metric buffer 1 */ 22 metric_t metrics2; /* path metric buffer 2 */ 23 decision_t *dp; /* Pointer to current decision */ 24 metric_t *old_metrics,*new_metrics; /* Pointers to path metrics, swapped on every bit */ 25 decision_t *decisions; /* Beginning of decisions for block */ 26 }; 27 28 /* Initialize Viterbi decoder for start of new frame */ 29 int init_viterbi29_sse2(void *p,int starting_state){ 30 struct v29 *vp = p; 31 int i; 32 33 for(i=0;i<256;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 & 255] = 0; /* Bias known start state */ 40 return 0; 41 } 42 43 void set_viterbi29_polynomial_sse2(int polys[2]){ 44 int state; 45 46 for(state=0;state < 128;state++){ 47 Branchtab29_sse2[0].c[state] = (polys[0] < 0) ^ parity((2*state) & abs(polys[0])) ? 255 : 0; 48 Branchtab29_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 */ 55 void *create_viterbi29_sse2(int len){ 56 void *p; 57 struct v29 *vp; 58 59 if(!Init){ 60 int polys[2] = {V29POLYA,V29POLYB}; 61 62 set_viterbi29_polynomial(polys); 63 } 64 /* Ordinary malloc() only returns 8-byte alignment, we need 16 */ 65 if(posix_memalign(&p, sizeof(__m128i),sizeof(struct v29))) 66 return NULL; 67 vp = (struct v29 *)p; 68 if((p = malloc((len+8)*sizeof(decision_t))) == NULL){ 69 free(vp); 70 return NULL; 71 } 72 vp->decisions = (decision_t *)p; 73 init_viterbi29_sse2(vp,0); 74 return vp; 75 } 76 77 78 /* Viterbi chainback */ 79 int chainback_viterbi29_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 v29 *vp = p; 85 decision_t *d; 86 87 if(p == NULL) 88 return -1; 89 d = vp->decisions; 90 91 /* Make room beyond the end of the encoder register so we can 92 * accumulate a full byte of decoded data 93 */ 94 endstate %= 256; 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 += 8; /* Look past tail */ 101 while(nbits-- != 0){ 102 int k; 103 104 k = (d[nbits].c[endstate/8] >> (endstate%8)) & 1; 105 data[nbits>>3] = endstate = (endstate >> 1) | (k << 7); 106 } 107 return 0; 108 } 109 110 111 /* Delete instance of a Viterbi decoder */ 112 void delete_viterbi29_sse2(void *p){ 113 struct v29 *vp = p; 114 115 if(vp != NULL){ 116 free(vp->decisions); 117 free(vp); 118 } 119 } 120