blob: 308cfe0706cfc244f756ef9179ad9b9ba8f424f2 [file] [log] [blame]
Tom Tsou35536802016-11-24 19:24:32 +07001/*
2 * Viterbi decoder
3 *
4 * Copyright (C) 2013, 2014 Thomas Tsou <tom@tsou.cc>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26
Tom Tsou35536802016-11-24 19:24:32 +070027#include "config.h"
28
Tom Tsou34e228a2017-04-29 00:16:43 +070029#include <osmocom/core/conv.h>
30
Tom Tsou35536802016-11-24 19:24:32 +070031#define BIT2NRZ(REG,N) (((REG >> N) & 0x01) * 2 - 1) * -1
32#define NUM_STATES(K) (K == 7 ? 64 : 16)
Tom Tsou35536802016-11-24 19:24:32 +070033
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +070034#define INIT_POINTERS(simd) \
35{ \
36 osmo_conv_metrics_k5_n2 = osmo_conv_##simd##_metrics_k5_n2; \
37 osmo_conv_metrics_k5_n3 = osmo_conv_##simd##_metrics_k5_n3; \
38 osmo_conv_metrics_k5_n4 = osmo_conv_##simd##_metrics_k5_n4; \
39 osmo_conv_metrics_k7_n2 = osmo_conv_##simd##_metrics_k7_n2; \
40 osmo_conv_metrics_k7_n3 = osmo_conv_##simd##_metrics_k7_n3; \
41 osmo_conv_metrics_k7_n4 = osmo_conv_##simd##_metrics_k7_n4; \
42 vdec_malloc = &osmo_conv_##simd##_vdec_malloc; \
43 vdec_free = &osmo_conv_##simd##_vdec_free; \
44}
45
Tom Tsou34e228a2017-04-29 00:16:43 +070046static int init_complete = 0;
47
48__attribute__ ((visibility("hidden"))) int avx2_supported = 0;
49__attribute__ ((visibility("hidden"))) int sse3_supported = 0;
50__attribute__ ((visibility("hidden"))) int sse41_supported = 0;
51
52/**
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +070053 * These pointers are being initialized at runtime by the
54 * osmo_conv_init() depending on supported SIMD extensions.
Tom Tsou34e228a2017-04-29 00:16:43 +070055 */
56static int16_t *(*vdec_malloc)(size_t n);
57static void (*vdec_free)(int16_t *ptr);
58
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +070059void (*osmo_conv_metrics_k5_n2)(const int8_t *seq,
60 const int16_t *out, int16_t *sums, int16_t *paths, int norm);
61void (*osmo_conv_metrics_k5_n3)(const int8_t *seq,
62 const int16_t *out, int16_t *sums, int16_t *paths, int norm);
63void (*osmo_conv_metrics_k5_n4)(const int8_t *seq,
64 const int16_t *out, int16_t *sums, int16_t *paths, int norm);
65void (*osmo_conv_metrics_k7_n2)(const int8_t *seq,
66 const int16_t *out, int16_t *sums, int16_t *paths, int norm);
67void (*osmo_conv_metrics_k7_n3)(const int8_t *seq,
68 const int16_t *out, int16_t *sums, int16_t *paths, int norm);
69void (*osmo_conv_metrics_k7_n4)(const int8_t *seq,
70 const int16_t *out, int16_t *sums, int16_t *paths, int norm);
Tom Tsou34e228a2017-04-29 00:16:43 +070071
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +070072/* Forward malloc wrappers */
73int16_t *osmo_conv_gen_vdec_malloc(size_t n);
74void osmo_conv_gen_vdec_free(int16_t *ptr);
75
76#if defined(HAVE_SSE3)
77int16_t *osmo_conv_sse_vdec_malloc(size_t n);
78void osmo_conv_sse_vdec_free(int16_t *ptr);
79#endif
80
81#if defined(HAVE_SSE3) && defined(HAVE_AVX2)
82int16_t *osmo_conv_sse_avx_vdec_malloc(size_t n);
83void osmo_conv_sse_avx_vdec_free(int16_t *ptr);
Tom Tsou34e228a2017-04-29 00:16:43 +070084#endif
85
Tom Tsou35536802016-11-24 19:24:32 +070086/* Forward Metric Units */
87void osmo_conv_gen_metrics_k5_n2(const int8_t *seq, const int16_t *out,
88 int16_t *sums, int16_t *paths, int norm);
89void osmo_conv_gen_metrics_k5_n3(const int8_t *seq, const int16_t *out,
90 int16_t *sums, int16_t *paths, int norm);
91void osmo_conv_gen_metrics_k5_n4(const int8_t *seq, const int16_t *out,
92 int16_t *sums, int16_t *paths, int norm);
93void osmo_conv_gen_metrics_k7_n2(const int8_t *seq, const int16_t *out,
94 int16_t *sums, int16_t *paths, int norm);
95void osmo_conv_gen_metrics_k7_n3(const int8_t *seq, const int16_t *out,
96 int16_t *sums, int16_t *paths, int norm);
97void osmo_conv_gen_metrics_k7_n4(const int8_t *seq, const int16_t *out,
98 int16_t *sums, int16_t *paths, int norm);
99
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700100#if defined(HAVE_SSE3)
101void osmo_conv_sse_metrics_k5_n2(const int8_t *seq, const int16_t *out,
Tom Tsou34e228a2017-04-29 00:16:43 +0700102 int16_t *sums, int16_t *paths, int norm);
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700103void osmo_conv_sse_metrics_k5_n3(const int8_t *seq, const int16_t *out,
Tom Tsou34e228a2017-04-29 00:16:43 +0700104 int16_t *sums, int16_t *paths, int norm);
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700105void osmo_conv_sse_metrics_k5_n4(const int8_t *seq, const int16_t *out,
Tom Tsou34e228a2017-04-29 00:16:43 +0700106 int16_t *sums, int16_t *paths, int norm);
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700107void osmo_conv_sse_metrics_k7_n2(const int8_t *seq, const int16_t *out,
Tom Tsou34e228a2017-04-29 00:16:43 +0700108 int16_t *sums, int16_t *paths, int norm);
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700109void osmo_conv_sse_metrics_k7_n3(const int8_t *seq, const int16_t *out,
Tom Tsou34e228a2017-04-29 00:16:43 +0700110 int16_t *sums, int16_t *paths, int norm);
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700111void osmo_conv_sse_metrics_k7_n4(const int8_t *seq, const int16_t *out,
112 int16_t *sums, int16_t *paths, int norm);
113#endif
114
115#if defined(HAVE_SSE3) && defined(HAVE_AVX2)
116void osmo_conv_sse_avx_metrics_k5_n2(const int8_t *seq, const int16_t *out,
117 int16_t *sums, int16_t *paths, int norm);
118void osmo_conv_sse_avx_metrics_k5_n3(const int8_t *seq, const int16_t *out,
119 int16_t *sums, int16_t *paths, int norm);
120void osmo_conv_sse_avx_metrics_k5_n4(const int8_t *seq, const int16_t *out,
121 int16_t *sums, int16_t *paths, int norm);
122void osmo_conv_sse_avx_metrics_k7_n2(const int8_t *seq, const int16_t *out,
123 int16_t *sums, int16_t *paths, int norm);
124void osmo_conv_sse_avx_metrics_k7_n3(const int8_t *seq, const int16_t *out,
125 int16_t *sums, int16_t *paths, int norm);
126void osmo_conv_sse_avx_metrics_k7_n4(const int8_t *seq, const int16_t *out,
Tom Tsou34e228a2017-04-29 00:16:43 +0700127 int16_t *sums, int16_t *paths, int norm);
128#endif
129
Tom Tsou35536802016-11-24 19:24:32 +0700130/* Trellis State
131 * state - Internal lshift register value
132 * prev - Register values of previous 0 and 1 states
133 */
134struct vstate {
135 unsigned state;
136 unsigned prev[2];
137};
138
139/* Trellis Object
140 * num_states - Number of states in the trellis
141 * sums - Accumulated path metrics
142 * outputs - Trellis output values
143 * vals - Input value that led to each state
144 */
145struct vtrellis {
146 int num_states;
147 int16_t *sums;
148 int16_t *outputs;
149 uint8_t *vals;
150};
151
152/* Viterbi Decoder
153 * n - Code order
154 * k - Constraint length
155 * len - Horizontal length of trellis
156 * recursive - Set to '1' if the code is recursive
157 * intrvl - Normalization interval
158 * trellis - Trellis object
159 * punc - Puncturing sequence
160 * paths - Trellis paths
161 */
162struct vdecoder {
163 int n;
164 int k;
165 int len;
166 int recursive;
167 int intrvl;
168 struct vtrellis *trellis;
169 int *punc;
170 int16_t **paths;
171
172 void (*metric_func)(const int8_t *, const int16_t *,
173 int16_t *, int16_t *, int);
174};
175
Tom Tsou35536802016-11-24 19:24:32 +0700176/* Accessor calls */
177static inline int conv_code_recursive(const struct osmo_conv_code *code)
178{
179 return code->next_term_output ? 1 : 0;
180}
181
182/* Left shift and mask for finding the previous state */
183static unsigned vstate_lshift(unsigned reg, int k, int val)
184{
185 unsigned mask;
186
187 if (k == 5)
188 mask = 0x0e;
189 else if (k == 7)
190 mask = 0x3e;
191 else
192 mask = 0;
193
194 return ((reg << 1) & mask) | val;
195}
196
197/* Bit endian manipulators */
198static inline unsigned bitswap2(unsigned v)
199{
200 return ((v & 0x02) >> 1) | ((v & 0x01) << 1);
201}
202
203static inline unsigned bitswap3(unsigned v)
204{
205 return ((v & 0x04) >> 2) | ((v & 0x02) >> 0) |
206 ((v & 0x01) << 2);
207}
208
209static inline unsigned bitswap4(unsigned v)
210{
211 return ((v & 0x08) >> 3) | ((v & 0x04) >> 1) |
212 ((v & 0x02) << 1) | ((v & 0x01) << 3);
213}
214
215static inline unsigned bitswap5(unsigned v)
216{
217 return ((v & 0x10) >> 4) | ((v & 0x08) >> 2) | ((v & 0x04) >> 0) |
218 ((v & 0x02) << 2) | ((v & 0x01) << 4);
219}
220
221static inline unsigned bitswap6(unsigned v)
222{
223 return ((v & 0x20) >> 5) | ((v & 0x10) >> 3) | ((v & 0x08) >> 1) |
224 ((v & 0x04) << 1) | ((v & 0x02) << 3) | ((v & 0x01) << 5);
225}
226
227static unsigned bitswap(unsigned v, unsigned n)
228{
229 switch (n) {
230 case 1:
231 return v;
232 case 2:
233 return bitswap2(v);
234 case 3:
235 return bitswap3(v);
236 case 4:
237 return bitswap4(v);
238 case 5:
239 return bitswap5(v);
240 case 6:
241 return bitswap6(v);
242 default:
243 return 0;
244 }
245}
246
247/* Generate non-recursive state output from generator state table
248 * Note that the shift register moves right (i.e. the most recent bit is
249 * shifted into the register at k-1 bit of the register), which is typical
250 * textbook representation. The API transition table expects the most recent
251 * bit in the low order bit, or left shift. A bitswap operation is required
252 * to accommodate the difference.
253 */
254static unsigned gen_output(struct vstate *state, int val,
255 const struct osmo_conv_code *code)
256{
257 unsigned out, prev;
258
259 prev = bitswap(state->prev[0], code->K - 1);
260 out = code->next_output[prev][val];
261 out = bitswap(out, code->N);
262
263 return out;
264}
265
266/* Populate non-recursive trellis state
267 * For a given state defined by the k-1 length shift register, find the
268 * value of the input bit that drove the trellis to that state. Also
269 * generate the N outputs of the generator polynomial at that state.
270 */
271static int gen_state_info(uint8_t *val, unsigned reg,
272 int16_t *output, const struct osmo_conv_code *code)
273{
274 int i;
275 unsigned out;
276 struct vstate state;
277
278 /* Previous '0' state */
279 state.state = reg;
280 state.prev[0] = vstate_lshift(reg, code->K, 0);
281 state.prev[1] = vstate_lshift(reg, code->K, 1);
282
283 *val = (reg >> (code->K - 2)) & 0x01;
284
285 /* Transition output */
286 out = gen_output(&state, *val, code);
287
288 /* Unpack to NRZ */
289 for (i = 0; i < code->N; i++)
290 output[i] = BIT2NRZ(out, i);
291
292 return 0;
293}
294
295/* Generate recursive state output from generator state table */
296static unsigned gen_recursive_output(struct vstate *state,
297 uint8_t *val, unsigned reg,
298 const struct osmo_conv_code *code, int pos)
299{
300 int val0, val1;
301 unsigned out, prev;
302
303 /* Previous '0' state */
304 prev = vstate_lshift(reg, code->K, 0);
305 prev = bitswap(prev, code->K - 1);
306
307 /* Input value */
308 val0 = (reg >> (code->K - 2)) & 0x01;
309 val1 = (code->next_term_output[prev] >> pos) & 0x01;
310 *val = val0 == val1 ? 0 : 1;
311
312 /* Wrapper for osmocom state access */
313 prev = bitswap(state->prev[0], code->K - 1);
314
315 /* Compute the transition output */
316 out = code->next_output[prev][*val];
317 out = bitswap(out, code->N);
318
319 return out;
320}
321
322/* Populate recursive trellis state
323 * The bit position of the systematic bit is not explicitly marked by the
324 * API, so it must be extracted from the generator table. Otherwise,
325 * populate the trellis similar to the non-recursive version.
326 * Non-systematic recursive codes are not supported.
327 */
328static int gen_recursive_state_info(uint8_t *val,
329 unsigned reg, int16_t *output, const struct osmo_conv_code *code)
330{
331 int i, j, pos = -1;
332 int ns = NUM_STATES(code->K);
333 unsigned out;
334 struct vstate state;
335
336 /* Previous '0' and '1' states */
337 state.state = reg;
338 state.prev[0] = vstate_lshift(reg, code->K, 0);
339 state.prev[1] = vstate_lshift(reg, code->K, 1);
340
341 /* Find recursive bit location */
342 for (i = 0; i < code->N; i++) {
343 for (j = 0; j < ns; j++) {
344 if ((code->next_output[j][0] >> i) & 0x01)
345 break;
346 }
347
348 if (j == ns) {
349 pos = i;
350 break;
351 }
352 }
353
354 /* Non-systematic recursive code not supported */
355 if (pos < 0)
356 return -EPROTO;
357
358 /* Transition output */
359 out = gen_recursive_output(&state, val, reg, code, pos);
360
361 /* Unpack to NRZ */
362 for (i = 0; i < code->N; i++)
363 output[i] = BIT2NRZ(out, i);
364
365 return 0;
366}
367
368/* Release the trellis */
369static void free_trellis(struct vtrellis *trellis)
370{
371 if (!trellis)
372 return;
373
Tom Tsou34e228a2017-04-29 00:16:43 +0700374 vdec_free(trellis->outputs);
375 vdec_free(trellis->sums);
Tom Tsou35536802016-11-24 19:24:32 +0700376 free(trellis->vals);
Tom Tsou35536802016-11-24 19:24:32 +0700377 free(trellis);
378}
379
380/* Allocate and initialize the trellis object
381 * Initialization consists of generating the outputs and output value of a
382 * given state. Due to trellis symmetry and anti-symmetry, only one of the
383 * transition paths is utilized by the butterfly operation in the forward
384 * recursion, so only one set of N outputs is required per state variable.
385 */
386static struct vtrellis *generate_trellis(const struct osmo_conv_code *code)
387{
388 int i, rc = -1;
389 struct vtrellis *trellis;
390 int16_t *outputs;
391
392 int ns = NUM_STATES(code->K);
393 int recursive = conv_code_recursive(code);
394 int olen = (code->N == 2) ? 2 : 4;
395
396 trellis = (struct vtrellis *) calloc(1, sizeof(struct vtrellis));
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700397 if (!trellis)
398 goto fail;
399
Tom Tsou35536802016-11-24 19:24:32 +0700400 trellis->num_states = ns;
401 trellis->sums = vdec_malloc(ns);
402 trellis->outputs = vdec_malloc(ns * olen);
403 trellis->vals = (uint8_t *) malloc(ns * sizeof(uint8_t));
404
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700405 if (!trellis->sums || !trellis->outputs || !trellis->vals)
Tom Tsou35536802016-11-24 19:24:32 +0700406 goto fail;
407
408 /* Populate the trellis state objects */
409 for (i = 0; i < ns; i++) {
410 outputs = &trellis->outputs[olen * i];
411 if (recursive) {
412 rc = gen_recursive_state_info(&trellis->vals[i],
413 i, outputs, code);
414 } else {
415 rc = gen_state_info(&trellis->vals[i],
416 i, outputs, code);
417 }
418 }
419
420 if (rc < 0)
421 goto fail;
422
423 return trellis;
424
425fail:
426 free_trellis(trellis);
427 return NULL;
428}
429
430/* Reset decoder
431 * Set accumulated path metrics to zero. For termination other than
432 * tail-biting, initialize the zero state as the encoder starting state.
433 * Initialize with the maximum accumulated sum at length equal to the
434 * constraint length.
435 */
436static void reset_decoder(struct vdecoder *dec, int term)
437{
438 int ns = dec->trellis->num_states;
439
440 memset(dec->trellis->sums, 0, sizeof(int16_t) * ns);
441
442 if (term != CONV_TERM_TAIL_BITING)
443 dec->trellis->sums[0] = INT8_MAX * dec->n * dec->k;
444}
445
446static void _traceback(struct vdecoder *dec,
447 unsigned state, uint8_t *out, int len)
448{
449 int i;
450 unsigned path;
451
452 for (i = len - 1; i >= 0; i--) {
453 path = dec->paths[i][state] + 1;
454 out[i] = dec->trellis->vals[state];
455 state = vstate_lshift(state, dec->k, path);
456 }
457}
458
459static void _traceback_rec(struct vdecoder *dec,
460 unsigned state, uint8_t *out, int len)
461{
462 int i;
463 unsigned path;
464
465 for (i = len - 1; i >= 0; i--) {
466 path = dec->paths[i][state] + 1;
467 out[i] = path ^ dec->trellis->vals[state];
468 state = vstate_lshift(state, dec->k, path);
469 }
470}
471
472/* Traceback and generate decoded output
473 * Find the largest accumulated path metric at the final state except for
474 * the zero terminated case, where we assume the final state is always zero.
475 */
476static int traceback(struct vdecoder *dec, uint8_t *out, int term, int len)
477{
478 int i, sum, max = -1;
479 unsigned path, state = 0;
480
481 if (term != CONV_TERM_FLUSH) {
482 for (i = 0; i < dec->trellis->num_states; i++) {
483 sum = dec->trellis->sums[i];
484 if (sum > max) {
485 max = sum;
486 state = i;
487 }
488 }
489
490 if (max < 0)
491 return -EPROTO;
492 }
493
494 for (i = dec->len - 1; i >= len; i--) {
495 path = dec->paths[i][state] + 1;
496 state = vstate_lshift(state, dec->k, path);
497 }
498
499 if (dec->recursive)
500 _traceback_rec(dec, state, out, len);
501 else
502 _traceback(dec, state, out, len);
503
504 return 0;
505}
506
507/* Release decoder object */
508static void free_vdec(struct vdecoder *dec)
509{
510 if (!dec)
511 return;
512
Tom Tsou35536802016-11-24 19:24:32 +0700513 free_trellis(dec->trellis);
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700514
515 if (dec->paths != NULL) {
516 vdec_free(dec->paths[0]);
517 free(dec->paths);
518 }
519
Tom Tsou35536802016-11-24 19:24:32 +0700520 free(dec);
521}
522
523/* Allocate decoder object
524 * Subtract the constraint length K on the normalization interval to
525 * accommodate the initialization path metric at state zero.
526 */
527static struct vdecoder *alloc_vdec(const struct osmo_conv_code *code)
528{
529 int i, ns;
530 struct vdecoder *dec;
531
532 ns = NUM_STATES(code->K);
533
534 dec = (struct vdecoder *) calloc(1, sizeof(struct vdecoder));
535 dec->n = code->N;
536 dec->k = code->K;
537 dec->recursive = conv_code_recursive(code);
538 dec->intrvl = INT16_MAX / (dec->n * INT8_MAX) - dec->k;
539
540 if (dec->k == 5) {
541 switch (dec->n) {
542 case 2:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700543 dec->metric_func = osmo_conv_metrics_k5_n2;
Tom Tsou35536802016-11-24 19:24:32 +0700544 break;
545 case 3:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700546 dec->metric_func = osmo_conv_metrics_k5_n3;
Tom Tsou35536802016-11-24 19:24:32 +0700547 break;
548 case 4:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700549 dec->metric_func = osmo_conv_metrics_k5_n4;
Tom Tsou35536802016-11-24 19:24:32 +0700550 break;
551 default:
552 goto fail;
553 }
554 } else if (dec->k == 7) {
555 switch (dec->n) {
556 case 2:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700557 dec->metric_func = osmo_conv_metrics_k7_n2;
Tom Tsou35536802016-11-24 19:24:32 +0700558 break;
559 case 3:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700560 dec->metric_func = osmo_conv_metrics_k7_n3;
Tom Tsou35536802016-11-24 19:24:32 +0700561 break;
562 case 4:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700563 dec->metric_func = osmo_conv_metrics_k7_n4;
Tom Tsou35536802016-11-24 19:24:32 +0700564 break;
565 default:
566 goto fail;
567 }
568 } else {
569 goto fail;
570 }
571
572 if (code->term == CONV_TERM_FLUSH)
573 dec->len = code->len + code->K - 1;
574 else
575 dec->len = code->len;
576
577 dec->trellis = generate_trellis(code);
578 if (!dec->trellis)
579 goto fail;
580
581 dec->paths = (int16_t **) malloc(sizeof(int16_t *) * dec->len);
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700582 if (!dec->paths)
583 goto fail;
584
Tom Tsou35536802016-11-24 19:24:32 +0700585 dec->paths[0] = vdec_malloc(ns * dec->len);
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700586 if (!dec->paths[0])
587 goto fail;
588
Tom Tsou35536802016-11-24 19:24:32 +0700589 for (i = 1; i < dec->len; i++)
590 dec->paths[i] = &dec->paths[0][i * ns];
591
592 return dec;
593
594fail:
595 free_vdec(dec);
596 return NULL;
597}
598
599/* Depuncture sequence with nagative value terminated puncturing matrix */
600static int depuncture(const int8_t *in, const int *punc, int8_t *out, int len)
601{
602 int i, n = 0, m = 0;
603
604 for (i = 0; i < len; i++) {
605 if (i == punc[n]) {
606 out[i] = 0;
607 n++;
608 continue;
609 }
610
611 out[i] = in[m++];
612 }
613
614 return 0;
615}
616
617/* Forward trellis recursion
618 * Generate branch metrics and path metrics with a combined function. Only
619 * accumulated path metric sums and path selections are stored. Normalize on
620 * the interval specified by the decoder.
621 */
622static void forward_traverse(struct vdecoder *dec, const int8_t *seq)
623{
624 struct vtrellis *trellis = dec->trellis;
625 int i;
626
627 for (i = 0; i < dec->len; i++) {
628 dec->metric_func(&seq[dec->n * i],
629 trellis->outputs,
630 trellis->sums,
631 dec->paths[i],
632 !(i % dec->intrvl));
633 }
634}
635
636/* Convolutional decode with a decoder object
637 * Initial puncturing run if necessary followed by the forward recursion.
638 * For tail-biting perform a second pass before running the backward
639 * traceback operation.
640 */
641static int conv_decode(struct vdecoder *dec, const int8_t *seq,
642 const int *punc, uint8_t *out, int len, int term)
643{
644 int8_t depunc[dec->len * dec->n];
645
646 reset_decoder(dec, term);
647
648 if (punc) {
649 depuncture(seq, punc, depunc, dec->len * dec->n);
650 seq = depunc;
651 }
652
653 /* Propagate through the trellis with interval normalization */
654 forward_traverse(dec, seq);
655
656 if (term == CONV_TERM_TAIL_BITING)
657 forward_traverse(dec, seq);
658
659 return traceback(dec, out, term, len);
660}
661
Tom Tsou34e228a2017-04-29 00:16:43 +0700662static void osmo_conv_init(void)
663{
664 init_complete = 1;
665
666#ifdef HAVE___BUILTIN_CPU_SUPPORTS
667 /* Detect CPU capabilities */
668 #ifdef HAVE_AVX2
669 avx2_supported = __builtin_cpu_supports("avx2");
670 #endif
671
672 #ifdef HAVE_SSE3
673 sse3_supported = __builtin_cpu_supports("sse3");
674 #endif
675
676 #ifdef HAVE_SSE4_1
677 sse41_supported = __builtin_cpu_supports("sse4.1");
678 #endif
679#endif
680
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700681/**
682 * Usage of curly braces is mandatory,
683 * because we use multi-line define.
684 */
685#if defined(HAVE_SSE3) && defined(HAVE_AVX2)
686 if (sse3_supported && avx2_supported) {
687 INIT_POINTERS(sse_avx);
688 } else if (sse3_supported) {
689 INIT_POINTERS(sse);
690 } else {
691 INIT_POINTERS(gen);
692 }
693#elif defined(HAVE_SSE3)
694 if (sse3_supported) {
695 INIT_POINTERS(sse);
696 } else {
697 INIT_POINTERS(gen);
698 }
Tom Tsou34e228a2017-04-29 00:16:43 +0700699#else
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700700 INIT_POINTERS(gen);
Tom Tsou34e228a2017-04-29 00:16:43 +0700701#endif
702}
703
Tom Tsou35536802016-11-24 19:24:32 +0700704/* All-in-one Viterbi decoding */
705int osmo_conv_decode_acc(const struct osmo_conv_code *code,
706 const sbit_t *input, ubit_t *output)
707{
708 int rc;
709 struct vdecoder *vdec;
710
Tom Tsou34e228a2017-04-29 00:16:43 +0700711 if (!init_complete)
712 osmo_conv_init();
713
Tom Tsou35536802016-11-24 19:24:32 +0700714 if ((code->N < 2) || (code->N > 4) || (code->len < 1) ||
715 ((code->K != 5) && (code->K != 7)))
716 return -EINVAL;
717
718 vdec = alloc_vdec(code);
719 if (!vdec)
720 return -EFAULT;
721
722 rc = conv_decode(vdec, input, code->puncture,
723 output, code->len, code->term);
724
725 free_vdec(vdec);
726
727 return rc;
728}