blob: 7f0362844edf0f29df0b43eabd54417b9d9d61a7 [file] [log] [blame]
Tom Tsou35536802016-11-24 19:24:32 +07001/*
Vadim Yanitskiy46e533c2017-06-19 18:21:02 +07002 * Accelerated Viterbi decoder implementation
Tom Tsou35536802016-11-24 19:24:32 +07003 *
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
Tom Tsou35536802016-11-24 19:24:32 +0700159 * paths - Trellis paths
160 */
161struct vdecoder {
162 int n;
163 int k;
164 int len;
165 int recursive;
166 int intrvl;
167 struct vtrellis *trellis;
Tom Tsou35536802016-11-24 19:24:32 +0700168 int16_t **paths;
169
170 void (*metric_func)(const int8_t *, const int16_t *,
171 int16_t *, int16_t *, int);
172};
173
Tom Tsou35536802016-11-24 19:24:32 +0700174/* Accessor calls */
175static inline int conv_code_recursive(const struct osmo_conv_code *code)
176{
177 return code->next_term_output ? 1 : 0;
178}
179
180/* Left shift and mask for finding the previous state */
181static unsigned vstate_lshift(unsigned reg, int k, int val)
182{
183 unsigned mask;
184
185 if (k == 5)
186 mask = 0x0e;
187 else if (k == 7)
188 mask = 0x3e;
189 else
190 mask = 0;
191
192 return ((reg << 1) & mask) | val;
193}
194
195/* Bit endian manipulators */
196static inline unsigned bitswap2(unsigned v)
197{
198 return ((v & 0x02) >> 1) | ((v & 0x01) << 1);
199}
200
201static inline unsigned bitswap3(unsigned v)
202{
203 return ((v & 0x04) >> 2) | ((v & 0x02) >> 0) |
204 ((v & 0x01) << 2);
205}
206
207static inline unsigned bitswap4(unsigned v)
208{
209 return ((v & 0x08) >> 3) | ((v & 0x04) >> 1) |
210 ((v & 0x02) << 1) | ((v & 0x01) << 3);
211}
212
213static inline unsigned bitswap5(unsigned v)
214{
215 return ((v & 0x10) >> 4) | ((v & 0x08) >> 2) | ((v & 0x04) >> 0) |
216 ((v & 0x02) << 2) | ((v & 0x01) << 4);
217}
218
219static inline unsigned bitswap6(unsigned v)
220{
221 return ((v & 0x20) >> 5) | ((v & 0x10) >> 3) | ((v & 0x08) >> 1) |
222 ((v & 0x04) << 1) | ((v & 0x02) << 3) | ((v & 0x01) << 5);
223}
224
225static unsigned bitswap(unsigned v, unsigned n)
226{
227 switch (n) {
228 case 1:
229 return v;
230 case 2:
231 return bitswap2(v);
232 case 3:
233 return bitswap3(v);
234 case 4:
235 return bitswap4(v);
236 case 5:
237 return bitswap5(v);
238 case 6:
239 return bitswap6(v);
240 default:
241 return 0;
242 }
243}
244
245/* Generate non-recursive state output from generator state table
246 * Note that the shift register moves right (i.e. the most recent bit is
247 * shifted into the register at k-1 bit of the register), which is typical
248 * textbook representation. The API transition table expects the most recent
249 * bit in the low order bit, or left shift. A bitswap operation is required
250 * to accommodate the difference.
251 */
252static unsigned gen_output(struct vstate *state, int val,
253 const struct osmo_conv_code *code)
254{
255 unsigned out, prev;
256
257 prev = bitswap(state->prev[0], code->K - 1);
258 out = code->next_output[prev][val];
259 out = bitswap(out, code->N);
260
261 return out;
262}
263
264/* Populate non-recursive trellis state
265 * For a given state defined by the k-1 length shift register, find the
266 * value of the input bit that drove the trellis to that state. Also
267 * generate the N outputs of the generator polynomial at that state.
268 */
269static int gen_state_info(uint8_t *val, unsigned reg,
270 int16_t *output, const struct osmo_conv_code *code)
271{
272 int i;
273 unsigned out;
274 struct vstate state;
275
276 /* Previous '0' state */
277 state.state = reg;
278 state.prev[0] = vstate_lshift(reg, code->K, 0);
279 state.prev[1] = vstate_lshift(reg, code->K, 1);
280
281 *val = (reg >> (code->K - 2)) & 0x01;
282
283 /* Transition output */
284 out = gen_output(&state, *val, code);
285
286 /* Unpack to NRZ */
287 for (i = 0; i < code->N; i++)
288 output[i] = BIT2NRZ(out, i);
289
290 return 0;
291}
292
293/* Generate recursive state output from generator state table */
294static unsigned gen_recursive_output(struct vstate *state,
295 uint8_t *val, unsigned reg,
296 const struct osmo_conv_code *code, int pos)
297{
298 int val0, val1;
299 unsigned out, prev;
300
301 /* Previous '0' state */
302 prev = vstate_lshift(reg, code->K, 0);
303 prev = bitswap(prev, code->K - 1);
304
305 /* Input value */
306 val0 = (reg >> (code->K - 2)) & 0x01;
307 val1 = (code->next_term_output[prev] >> pos) & 0x01;
308 *val = val0 == val1 ? 0 : 1;
309
310 /* Wrapper for osmocom state access */
311 prev = bitswap(state->prev[0], code->K - 1);
312
313 /* Compute the transition output */
314 out = code->next_output[prev][*val];
315 out = bitswap(out, code->N);
316
317 return out;
318}
319
320/* Populate recursive trellis state
321 * The bit position of the systematic bit is not explicitly marked by the
322 * API, so it must be extracted from the generator table. Otherwise,
323 * populate the trellis similar to the non-recursive version.
324 * Non-systematic recursive codes are not supported.
325 */
326static int gen_recursive_state_info(uint8_t *val,
327 unsigned reg, int16_t *output, const struct osmo_conv_code *code)
328{
329 int i, j, pos = -1;
330 int ns = NUM_STATES(code->K);
331 unsigned out;
332 struct vstate state;
333
334 /* Previous '0' and '1' states */
335 state.state = reg;
336 state.prev[0] = vstate_lshift(reg, code->K, 0);
337 state.prev[1] = vstate_lshift(reg, code->K, 1);
338
339 /* Find recursive bit location */
340 for (i = 0; i < code->N; i++) {
341 for (j = 0; j < ns; j++) {
342 if ((code->next_output[j][0] >> i) & 0x01)
343 break;
344 }
345
346 if (j == ns) {
347 pos = i;
348 break;
349 }
350 }
351
352 /* Non-systematic recursive code not supported */
353 if (pos < 0)
354 return -EPROTO;
355
356 /* Transition output */
357 out = gen_recursive_output(&state, val, reg, code, pos);
358
359 /* Unpack to NRZ */
360 for (i = 0; i < code->N; i++)
361 output[i] = BIT2NRZ(out, i);
362
363 return 0;
364}
365
366/* Release the trellis */
367static void free_trellis(struct vtrellis *trellis)
368{
369 if (!trellis)
370 return;
371
Tom Tsou34e228a2017-04-29 00:16:43 +0700372 vdec_free(trellis->outputs);
373 vdec_free(trellis->sums);
Tom Tsou35536802016-11-24 19:24:32 +0700374 free(trellis->vals);
Tom Tsou35536802016-11-24 19:24:32 +0700375 free(trellis);
376}
377
378/* Allocate and initialize the trellis object
379 * Initialization consists of generating the outputs and output value of a
380 * given state. Due to trellis symmetry and anti-symmetry, only one of the
381 * transition paths is utilized by the butterfly operation in the forward
382 * recursion, so only one set of N outputs is required per state variable.
383 */
384static struct vtrellis *generate_trellis(const struct osmo_conv_code *code)
385{
386 int i, rc = -1;
387 struct vtrellis *trellis;
388 int16_t *outputs;
389
390 int ns = NUM_STATES(code->K);
391 int recursive = conv_code_recursive(code);
392 int olen = (code->N == 2) ? 2 : 4;
393
394 trellis = (struct vtrellis *) calloc(1, sizeof(struct vtrellis));
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700395 if (!trellis)
396 goto fail;
397
Tom Tsou35536802016-11-24 19:24:32 +0700398 trellis->num_states = ns;
399 trellis->sums = vdec_malloc(ns);
400 trellis->outputs = vdec_malloc(ns * olen);
401 trellis->vals = (uint8_t *) malloc(ns * sizeof(uint8_t));
402
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700403 if (!trellis->sums || !trellis->outputs || !trellis->vals)
Tom Tsou35536802016-11-24 19:24:32 +0700404 goto fail;
405
406 /* Populate the trellis state objects */
407 for (i = 0; i < ns; i++) {
408 outputs = &trellis->outputs[olen * i];
409 if (recursive) {
410 rc = gen_recursive_state_info(&trellis->vals[i],
411 i, outputs, code);
412 } else {
413 rc = gen_state_info(&trellis->vals[i],
414 i, outputs, code);
415 }
Vadim Yanitskiy34da6b52017-06-19 05:41:49 +0700416
417 if (rc < 0)
418 goto fail;
419
420 /* Set accumulated path metrics to zero */
421 trellis->sums[i] = 0;
Tom Tsou35536802016-11-24 19:24:32 +0700422 }
423
Vadim Yanitskiy34da6b52017-06-19 05:41:49 +0700424 /**
425 * For termination other than tail-biting, initialize the zero state
426 * as the encoder starting state. Initialize with the maximum
427 * accumulated sum at length equal to the constraint length.
428 */
429 if (code->term != CONV_TERM_TAIL_BITING)
430 trellis->sums[0] = INT8_MAX * code->N * code->K;
Tom Tsou35536802016-11-24 19:24:32 +0700431
432 return trellis;
433
434fail:
435 free_trellis(trellis);
436 return NULL;
437}
438
Tom Tsou35536802016-11-24 19:24:32 +0700439static void _traceback(struct vdecoder *dec,
440 unsigned state, uint8_t *out, int len)
441{
442 int i;
443 unsigned path;
444
445 for (i = len - 1; i >= 0; i--) {
446 path = dec->paths[i][state] + 1;
447 out[i] = dec->trellis->vals[state];
448 state = vstate_lshift(state, dec->k, path);
449 }
450}
451
452static void _traceback_rec(struct vdecoder *dec,
453 unsigned state, uint8_t *out, int len)
454{
455 int i;
456 unsigned path;
457
458 for (i = len - 1; i >= 0; i--) {
459 path = dec->paths[i][state] + 1;
460 out[i] = path ^ dec->trellis->vals[state];
461 state = vstate_lshift(state, dec->k, path);
462 }
463}
464
465/* Traceback and generate decoded output
466 * Find the largest accumulated path metric at the final state except for
467 * the zero terminated case, where we assume the final state is always zero.
468 */
469static int traceback(struct vdecoder *dec, uint8_t *out, int term, int len)
470{
471 int i, sum, max = -1;
472 unsigned path, state = 0;
473
474 if (term != CONV_TERM_FLUSH) {
475 for (i = 0; i < dec->trellis->num_states; i++) {
476 sum = dec->trellis->sums[i];
477 if (sum > max) {
478 max = sum;
479 state = i;
480 }
481 }
482
483 if (max < 0)
484 return -EPROTO;
485 }
486
487 for (i = dec->len - 1; i >= len; i--) {
488 path = dec->paths[i][state] + 1;
489 state = vstate_lshift(state, dec->k, path);
490 }
491
492 if (dec->recursive)
493 _traceback_rec(dec, state, out, len);
494 else
495 _traceback(dec, state, out, len);
496
497 return 0;
498}
499
500/* Release decoder object */
501static void free_vdec(struct vdecoder *dec)
502{
503 if (!dec)
504 return;
505
Tom Tsou35536802016-11-24 19:24:32 +0700506 free_trellis(dec->trellis);
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700507
508 if (dec->paths != NULL) {
509 vdec_free(dec->paths[0]);
510 free(dec->paths);
511 }
512
Tom Tsou35536802016-11-24 19:24:32 +0700513 free(dec);
514}
515
516/* Allocate decoder object
517 * Subtract the constraint length K on the normalization interval to
518 * accommodate the initialization path metric at state zero.
519 */
520static struct vdecoder *alloc_vdec(const struct osmo_conv_code *code)
521{
522 int i, ns;
523 struct vdecoder *dec;
524
525 ns = NUM_STATES(code->K);
526
527 dec = (struct vdecoder *) calloc(1, sizeof(struct vdecoder));
528 dec->n = code->N;
529 dec->k = code->K;
530 dec->recursive = conv_code_recursive(code);
531 dec->intrvl = INT16_MAX / (dec->n * INT8_MAX) - dec->k;
532
533 if (dec->k == 5) {
534 switch (dec->n) {
535 case 2:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700536 dec->metric_func = osmo_conv_metrics_k5_n2;
Tom Tsou35536802016-11-24 19:24:32 +0700537 break;
538 case 3:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700539 dec->metric_func = osmo_conv_metrics_k5_n3;
Tom Tsou35536802016-11-24 19:24:32 +0700540 break;
541 case 4:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700542 dec->metric_func = osmo_conv_metrics_k5_n4;
Tom Tsou35536802016-11-24 19:24:32 +0700543 break;
544 default:
545 goto fail;
546 }
547 } else if (dec->k == 7) {
548 switch (dec->n) {
549 case 2:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700550 dec->metric_func = osmo_conv_metrics_k7_n2;
Tom Tsou35536802016-11-24 19:24:32 +0700551 break;
552 case 3:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700553 dec->metric_func = osmo_conv_metrics_k7_n3;
Tom Tsou35536802016-11-24 19:24:32 +0700554 break;
555 case 4:
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700556 dec->metric_func = osmo_conv_metrics_k7_n4;
Tom Tsou35536802016-11-24 19:24:32 +0700557 break;
558 default:
559 goto fail;
560 }
561 } else {
562 goto fail;
563 }
564
565 if (code->term == CONV_TERM_FLUSH)
566 dec->len = code->len + code->K - 1;
567 else
568 dec->len = code->len;
569
570 dec->trellis = generate_trellis(code);
571 if (!dec->trellis)
572 goto fail;
573
574 dec->paths = (int16_t **) malloc(sizeof(int16_t *) * dec->len);
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700575 if (!dec->paths)
576 goto fail;
577
Tom Tsou35536802016-11-24 19:24:32 +0700578 dec->paths[0] = vdec_malloc(ns * dec->len);
Vadim Yanitskiycfb1eaa2017-06-12 03:13:12 +0700579 if (!dec->paths[0])
580 goto fail;
581
Tom Tsou35536802016-11-24 19:24:32 +0700582 for (i = 1; i < dec->len; i++)
583 dec->paths[i] = &dec->paths[0][i * ns];
584
585 return dec;
586
587fail:
588 free_vdec(dec);
589 return NULL;
590}
591
592/* Depuncture sequence with nagative value terminated puncturing matrix */
593static int depuncture(const int8_t *in, const int *punc, int8_t *out, int len)
594{
595 int i, n = 0, m = 0;
596
597 for (i = 0; i < len; i++) {
598 if (i == punc[n]) {
599 out[i] = 0;
600 n++;
601 continue;
602 }
603
604 out[i] = in[m++];
605 }
606
607 return 0;
608}
609
610/* Forward trellis recursion
611 * Generate branch metrics and path metrics with a combined function. Only
612 * accumulated path metric sums and path selections are stored. Normalize on
613 * the interval specified by the decoder.
614 */
615static void forward_traverse(struct vdecoder *dec, const int8_t *seq)
616{
617 struct vtrellis *trellis = dec->trellis;
618 int i;
619
620 for (i = 0; i < dec->len; i++) {
621 dec->metric_func(&seq[dec->n * i],
622 trellis->outputs,
623 trellis->sums,
624 dec->paths[i],
625 !(i % dec->intrvl));
626 }
627}
628
629/* Convolutional decode with a decoder object
630 * Initial puncturing run if necessary followed by the forward recursion.
631 * For tail-biting perform a second pass before running the backward
632 * traceback operation.
633 */
634static int conv_decode(struct vdecoder *dec, const int8_t *seq,
635 const int *punc, uint8_t *out, int len, int term)
636{
637 int8_t depunc[dec->len * dec->n];
638
Tom Tsou35536802016-11-24 19:24:32 +0700639 if (punc) {
640 depuncture(seq, punc, depunc, dec->len * dec->n);
641 seq = depunc;
642 }
643
644 /* Propagate through the trellis with interval normalization */
645 forward_traverse(dec, seq);
646
647 if (term == CONV_TERM_TAIL_BITING)
648 forward_traverse(dec, seq);
649
650 return traceback(dec, out, term, len);
651}
652
Tom Tsou34e228a2017-04-29 00:16:43 +0700653static void osmo_conv_init(void)
654{
655 init_complete = 1;
656
657#ifdef HAVE___BUILTIN_CPU_SUPPORTS
658 /* Detect CPU capabilities */
659 #ifdef HAVE_AVX2
660 avx2_supported = __builtin_cpu_supports("avx2");
661 #endif
662
663 #ifdef HAVE_SSE3
664 sse3_supported = __builtin_cpu_supports("sse3");
665 #endif
666
667 #ifdef HAVE_SSE4_1
668 sse41_supported = __builtin_cpu_supports("sse4.1");
669 #endif
670#endif
671
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700672/**
673 * Usage of curly braces is mandatory,
674 * because we use multi-line define.
675 */
676#if defined(HAVE_SSE3) && defined(HAVE_AVX2)
677 if (sse3_supported && avx2_supported) {
678 INIT_POINTERS(sse_avx);
679 } else if (sse3_supported) {
680 INIT_POINTERS(sse);
681 } else {
682 INIT_POINTERS(gen);
683 }
684#elif defined(HAVE_SSE3)
685 if (sse3_supported) {
686 INIT_POINTERS(sse);
687 } else {
688 INIT_POINTERS(gen);
689 }
Tom Tsou34e228a2017-04-29 00:16:43 +0700690#else
Vadim Yanitskiy0d49f472017-05-28 18:20:02 +0700691 INIT_POINTERS(gen);
Tom Tsou34e228a2017-04-29 00:16:43 +0700692#endif
693}
694
Tom Tsou35536802016-11-24 19:24:32 +0700695/* All-in-one Viterbi decoding */
696int osmo_conv_decode_acc(const struct osmo_conv_code *code,
697 const sbit_t *input, ubit_t *output)
698{
699 int rc;
700 struct vdecoder *vdec;
701
Tom Tsou34e228a2017-04-29 00:16:43 +0700702 if (!init_complete)
703 osmo_conv_init();
704
Tom Tsou35536802016-11-24 19:24:32 +0700705 if ((code->N < 2) || (code->N > 4) || (code->len < 1) ||
706 ((code->K != 5) && (code->K != 7)))
707 return -EINVAL;
708
709 vdec = alloc_vdec(code);
710 if (!vdec)
711 return -EFAULT;
712
713 rc = conv_decode(vdec, input, code->puncture,
714 output, code->len, code->term);
715
716 free_vdec(vdec);
717
718 return rc;
719}