Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 1 | /* |
| 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 Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 27 | #include "config.h" |
| 28 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 29 | #include <osmocom/core/conv.h> |
| 30 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 31 | #define BIT2NRZ(REG,N) (((REG >> N) & 0x01) * 2 - 1) * -1 |
| 32 | #define NUM_STATES(K) (K == 7 ? 64 : 16) |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 33 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 34 | static int init_complete = 0; |
| 35 | |
| 36 | __attribute__ ((visibility("hidden"))) int avx2_supported = 0; |
| 37 | __attribute__ ((visibility("hidden"))) int sse3_supported = 0; |
| 38 | __attribute__ ((visibility("hidden"))) int sse41_supported = 0; |
| 39 | |
| 40 | /** |
| 41 | * This pointers will be initialized by the osmo_conv_init() |
| 42 | * depending on supported SIMD extensions. |
| 43 | */ |
| 44 | static int16_t *(*vdec_malloc)(size_t n); |
| 45 | static void (*vdec_free)(int16_t *ptr); |
| 46 | |
| 47 | /* Forward malloc wrappers */ |
| 48 | int16_t *osmo_conv_vdec_malloc(size_t n); |
| 49 | void osmo_conv_vdec_free(int16_t *ptr); |
| 50 | |
| 51 | #ifdef HAVE_SSE3 |
| 52 | int16_t *osmo_conv_vdec_malloc_sse3(size_t n); |
| 53 | void osmo_conv_vdec_free_sse3(int16_t *ptr); |
| 54 | #endif |
| 55 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 56 | /* Forward Metric Units */ |
| 57 | void osmo_conv_gen_metrics_k5_n2(const int8_t *seq, const int16_t *out, |
| 58 | int16_t *sums, int16_t *paths, int norm); |
| 59 | void osmo_conv_gen_metrics_k5_n3(const int8_t *seq, const int16_t *out, |
| 60 | int16_t *sums, int16_t *paths, int norm); |
| 61 | void osmo_conv_gen_metrics_k5_n4(const int8_t *seq, const int16_t *out, |
| 62 | int16_t *sums, int16_t *paths, int norm); |
| 63 | void osmo_conv_gen_metrics_k7_n2(const int8_t *seq, const int16_t *out, |
| 64 | int16_t *sums, int16_t *paths, int norm); |
| 65 | void osmo_conv_gen_metrics_k7_n3(const int8_t *seq, const int16_t *out, |
| 66 | int16_t *sums, int16_t *paths, int norm); |
| 67 | void osmo_conv_gen_metrics_k7_n4(const int8_t *seq, const int16_t *out, |
| 68 | int16_t *sums, int16_t *paths, int norm); |
| 69 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 70 | #ifdef HAVE_SSE3 |
| 71 | void osmo_conv_gen_metrics_k5_n2_sse(const int8_t *seq, const int16_t *out, |
| 72 | int16_t *sums, int16_t *paths, int norm); |
| 73 | void osmo_conv_gen_metrics_k5_n3_sse(const int8_t *seq, const int16_t *out, |
| 74 | int16_t *sums, int16_t *paths, int norm); |
| 75 | void osmo_conv_gen_metrics_k5_n4_sse(const int8_t *seq, const int16_t *out, |
| 76 | int16_t *sums, int16_t *paths, int norm); |
| 77 | void osmo_conv_gen_metrics_k7_n2_sse(const int8_t *seq, const int16_t *out, |
| 78 | int16_t *sums, int16_t *paths, int norm); |
| 79 | void osmo_conv_gen_metrics_k7_n3_sse(const int8_t *seq, const int16_t *out, |
| 80 | int16_t *sums, int16_t *paths, int norm); |
| 81 | void osmo_conv_gen_metrics_k7_n4_sse(const int8_t *seq, const int16_t *out, |
| 82 | int16_t *sums, int16_t *paths, int norm); |
| 83 | #endif |
| 84 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 85 | /* Trellis State |
| 86 | * state - Internal lshift register value |
| 87 | * prev - Register values of previous 0 and 1 states |
| 88 | */ |
| 89 | struct vstate { |
| 90 | unsigned state; |
| 91 | unsigned prev[2]; |
| 92 | }; |
| 93 | |
| 94 | /* Trellis Object |
| 95 | * num_states - Number of states in the trellis |
| 96 | * sums - Accumulated path metrics |
| 97 | * outputs - Trellis output values |
| 98 | * vals - Input value that led to each state |
| 99 | */ |
| 100 | struct vtrellis { |
| 101 | int num_states; |
| 102 | int16_t *sums; |
| 103 | int16_t *outputs; |
| 104 | uint8_t *vals; |
| 105 | }; |
| 106 | |
| 107 | /* Viterbi Decoder |
| 108 | * n - Code order |
| 109 | * k - Constraint length |
| 110 | * len - Horizontal length of trellis |
| 111 | * recursive - Set to '1' if the code is recursive |
| 112 | * intrvl - Normalization interval |
| 113 | * trellis - Trellis object |
| 114 | * punc - Puncturing sequence |
| 115 | * paths - Trellis paths |
| 116 | */ |
| 117 | struct vdecoder { |
| 118 | int n; |
| 119 | int k; |
| 120 | int len; |
| 121 | int recursive; |
| 122 | int intrvl; |
| 123 | struct vtrellis *trellis; |
| 124 | int *punc; |
| 125 | int16_t **paths; |
| 126 | |
| 127 | void (*metric_func)(const int8_t *, const int16_t *, |
| 128 | int16_t *, int16_t *, int); |
| 129 | }; |
| 130 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 131 | /* Accessor calls */ |
| 132 | static inline int conv_code_recursive(const struct osmo_conv_code *code) |
| 133 | { |
| 134 | return code->next_term_output ? 1 : 0; |
| 135 | } |
| 136 | |
| 137 | /* Left shift and mask for finding the previous state */ |
| 138 | static unsigned vstate_lshift(unsigned reg, int k, int val) |
| 139 | { |
| 140 | unsigned mask; |
| 141 | |
| 142 | if (k == 5) |
| 143 | mask = 0x0e; |
| 144 | else if (k == 7) |
| 145 | mask = 0x3e; |
| 146 | else |
| 147 | mask = 0; |
| 148 | |
| 149 | return ((reg << 1) & mask) | val; |
| 150 | } |
| 151 | |
| 152 | /* Bit endian manipulators */ |
| 153 | static inline unsigned bitswap2(unsigned v) |
| 154 | { |
| 155 | return ((v & 0x02) >> 1) | ((v & 0x01) << 1); |
| 156 | } |
| 157 | |
| 158 | static inline unsigned bitswap3(unsigned v) |
| 159 | { |
| 160 | return ((v & 0x04) >> 2) | ((v & 0x02) >> 0) | |
| 161 | ((v & 0x01) << 2); |
| 162 | } |
| 163 | |
| 164 | static inline unsigned bitswap4(unsigned v) |
| 165 | { |
| 166 | return ((v & 0x08) >> 3) | ((v & 0x04) >> 1) | |
| 167 | ((v & 0x02) << 1) | ((v & 0x01) << 3); |
| 168 | } |
| 169 | |
| 170 | static inline unsigned bitswap5(unsigned v) |
| 171 | { |
| 172 | return ((v & 0x10) >> 4) | ((v & 0x08) >> 2) | ((v & 0x04) >> 0) | |
| 173 | ((v & 0x02) << 2) | ((v & 0x01) << 4); |
| 174 | } |
| 175 | |
| 176 | static inline unsigned bitswap6(unsigned v) |
| 177 | { |
| 178 | return ((v & 0x20) >> 5) | ((v & 0x10) >> 3) | ((v & 0x08) >> 1) | |
| 179 | ((v & 0x04) << 1) | ((v & 0x02) << 3) | ((v & 0x01) << 5); |
| 180 | } |
| 181 | |
| 182 | static unsigned bitswap(unsigned v, unsigned n) |
| 183 | { |
| 184 | switch (n) { |
| 185 | case 1: |
| 186 | return v; |
| 187 | case 2: |
| 188 | return bitswap2(v); |
| 189 | case 3: |
| 190 | return bitswap3(v); |
| 191 | case 4: |
| 192 | return bitswap4(v); |
| 193 | case 5: |
| 194 | return bitswap5(v); |
| 195 | case 6: |
| 196 | return bitswap6(v); |
| 197 | default: |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* Generate non-recursive state output from generator state table |
| 203 | * Note that the shift register moves right (i.e. the most recent bit is |
| 204 | * shifted into the register at k-1 bit of the register), which is typical |
| 205 | * textbook representation. The API transition table expects the most recent |
| 206 | * bit in the low order bit, or left shift. A bitswap operation is required |
| 207 | * to accommodate the difference. |
| 208 | */ |
| 209 | static unsigned gen_output(struct vstate *state, int val, |
| 210 | const struct osmo_conv_code *code) |
| 211 | { |
| 212 | unsigned out, prev; |
| 213 | |
| 214 | prev = bitswap(state->prev[0], code->K - 1); |
| 215 | out = code->next_output[prev][val]; |
| 216 | out = bitswap(out, code->N); |
| 217 | |
| 218 | return out; |
| 219 | } |
| 220 | |
| 221 | /* Populate non-recursive trellis state |
| 222 | * For a given state defined by the k-1 length shift register, find the |
| 223 | * value of the input bit that drove the trellis to that state. Also |
| 224 | * generate the N outputs of the generator polynomial at that state. |
| 225 | */ |
| 226 | static int gen_state_info(uint8_t *val, unsigned reg, |
| 227 | int16_t *output, const struct osmo_conv_code *code) |
| 228 | { |
| 229 | int i; |
| 230 | unsigned out; |
| 231 | struct vstate state; |
| 232 | |
| 233 | /* Previous '0' state */ |
| 234 | state.state = reg; |
| 235 | state.prev[0] = vstate_lshift(reg, code->K, 0); |
| 236 | state.prev[1] = vstate_lshift(reg, code->K, 1); |
| 237 | |
| 238 | *val = (reg >> (code->K - 2)) & 0x01; |
| 239 | |
| 240 | /* Transition output */ |
| 241 | out = gen_output(&state, *val, code); |
| 242 | |
| 243 | /* Unpack to NRZ */ |
| 244 | for (i = 0; i < code->N; i++) |
| 245 | output[i] = BIT2NRZ(out, i); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* Generate recursive state output from generator state table */ |
| 251 | static unsigned gen_recursive_output(struct vstate *state, |
| 252 | uint8_t *val, unsigned reg, |
| 253 | const struct osmo_conv_code *code, int pos) |
| 254 | { |
| 255 | int val0, val1; |
| 256 | unsigned out, prev; |
| 257 | |
| 258 | /* Previous '0' state */ |
| 259 | prev = vstate_lshift(reg, code->K, 0); |
| 260 | prev = bitswap(prev, code->K - 1); |
| 261 | |
| 262 | /* Input value */ |
| 263 | val0 = (reg >> (code->K - 2)) & 0x01; |
| 264 | val1 = (code->next_term_output[prev] >> pos) & 0x01; |
| 265 | *val = val0 == val1 ? 0 : 1; |
| 266 | |
| 267 | /* Wrapper for osmocom state access */ |
| 268 | prev = bitswap(state->prev[0], code->K - 1); |
| 269 | |
| 270 | /* Compute the transition output */ |
| 271 | out = code->next_output[prev][*val]; |
| 272 | out = bitswap(out, code->N); |
| 273 | |
| 274 | return out; |
| 275 | } |
| 276 | |
| 277 | /* Populate recursive trellis state |
| 278 | * The bit position of the systematic bit is not explicitly marked by the |
| 279 | * API, so it must be extracted from the generator table. Otherwise, |
| 280 | * populate the trellis similar to the non-recursive version. |
| 281 | * Non-systematic recursive codes are not supported. |
| 282 | */ |
| 283 | static int gen_recursive_state_info(uint8_t *val, |
| 284 | unsigned reg, int16_t *output, const struct osmo_conv_code *code) |
| 285 | { |
| 286 | int i, j, pos = -1; |
| 287 | int ns = NUM_STATES(code->K); |
| 288 | unsigned out; |
| 289 | struct vstate state; |
| 290 | |
| 291 | /* Previous '0' and '1' states */ |
| 292 | state.state = reg; |
| 293 | state.prev[0] = vstate_lshift(reg, code->K, 0); |
| 294 | state.prev[1] = vstate_lshift(reg, code->K, 1); |
| 295 | |
| 296 | /* Find recursive bit location */ |
| 297 | for (i = 0; i < code->N; i++) { |
| 298 | for (j = 0; j < ns; j++) { |
| 299 | if ((code->next_output[j][0] >> i) & 0x01) |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | if (j == ns) { |
| 304 | pos = i; |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* Non-systematic recursive code not supported */ |
| 310 | if (pos < 0) |
| 311 | return -EPROTO; |
| 312 | |
| 313 | /* Transition output */ |
| 314 | out = gen_recursive_output(&state, val, reg, code, pos); |
| 315 | |
| 316 | /* Unpack to NRZ */ |
| 317 | for (i = 0; i < code->N; i++) |
| 318 | output[i] = BIT2NRZ(out, i); |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* Release the trellis */ |
| 324 | static void free_trellis(struct vtrellis *trellis) |
| 325 | { |
| 326 | if (!trellis) |
| 327 | return; |
| 328 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 329 | vdec_free(trellis->outputs); |
| 330 | vdec_free(trellis->sums); |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 331 | free(trellis->vals); |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 332 | free(trellis); |
| 333 | } |
| 334 | |
| 335 | /* Allocate and initialize the trellis object |
| 336 | * Initialization consists of generating the outputs and output value of a |
| 337 | * given state. Due to trellis symmetry and anti-symmetry, only one of the |
| 338 | * transition paths is utilized by the butterfly operation in the forward |
| 339 | * recursion, so only one set of N outputs is required per state variable. |
| 340 | */ |
| 341 | static struct vtrellis *generate_trellis(const struct osmo_conv_code *code) |
| 342 | { |
| 343 | int i, rc = -1; |
| 344 | struct vtrellis *trellis; |
| 345 | int16_t *outputs; |
| 346 | |
| 347 | int ns = NUM_STATES(code->K); |
| 348 | int recursive = conv_code_recursive(code); |
| 349 | int olen = (code->N == 2) ? 2 : 4; |
| 350 | |
| 351 | trellis = (struct vtrellis *) calloc(1, sizeof(struct vtrellis)); |
| 352 | trellis->num_states = ns; |
| 353 | trellis->sums = vdec_malloc(ns); |
| 354 | trellis->outputs = vdec_malloc(ns * olen); |
| 355 | trellis->vals = (uint8_t *) malloc(ns * sizeof(uint8_t)); |
| 356 | |
| 357 | if (!trellis->sums || !trellis->outputs) |
| 358 | goto fail; |
| 359 | |
| 360 | /* Populate the trellis state objects */ |
| 361 | for (i = 0; i < ns; i++) { |
| 362 | outputs = &trellis->outputs[olen * i]; |
| 363 | if (recursive) { |
| 364 | rc = gen_recursive_state_info(&trellis->vals[i], |
| 365 | i, outputs, code); |
| 366 | } else { |
| 367 | rc = gen_state_info(&trellis->vals[i], |
| 368 | i, outputs, code); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (rc < 0) |
| 373 | goto fail; |
| 374 | |
| 375 | return trellis; |
| 376 | |
| 377 | fail: |
| 378 | free_trellis(trellis); |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | /* Reset decoder |
| 383 | * Set accumulated path metrics to zero. For termination other than |
| 384 | * tail-biting, initialize the zero state as the encoder starting state. |
| 385 | * Initialize with the maximum accumulated sum at length equal to the |
| 386 | * constraint length. |
| 387 | */ |
| 388 | static void reset_decoder(struct vdecoder *dec, int term) |
| 389 | { |
| 390 | int ns = dec->trellis->num_states; |
| 391 | |
| 392 | memset(dec->trellis->sums, 0, sizeof(int16_t) * ns); |
| 393 | |
| 394 | if (term != CONV_TERM_TAIL_BITING) |
| 395 | dec->trellis->sums[0] = INT8_MAX * dec->n * dec->k; |
| 396 | } |
| 397 | |
| 398 | static void _traceback(struct vdecoder *dec, |
| 399 | unsigned state, uint8_t *out, int len) |
| 400 | { |
| 401 | int i; |
| 402 | unsigned path; |
| 403 | |
| 404 | for (i = len - 1; i >= 0; i--) { |
| 405 | path = dec->paths[i][state] + 1; |
| 406 | out[i] = dec->trellis->vals[state]; |
| 407 | state = vstate_lshift(state, dec->k, path); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static void _traceback_rec(struct vdecoder *dec, |
| 412 | unsigned state, uint8_t *out, int len) |
| 413 | { |
| 414 | int i; |
| 415 | unsigned path; |
| 416 | |
| 417 | for (i = len - 1; i >= 0; i--) { |
| 418 | path = dec->paths[i][state] + 1; |
| 419 | out[i] = path ^ dec->trellis->vals[state]; |
| 420 | state = vstate_lshift(state, dec->k, path); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /* Traceback and generate decoded output |
| 425 | * Find the largest accumulated path metric at the final state except for |
| 426 | * the zero terminated case, where we assume the final state is always zero. |
| 427 | */ |
| 428 | static int traceback(struct vdecoder *dec, uint8_t *out, int term, int len) |
| 429 | { |
| 430 | int i, sum, max = -1; |
| 431 | unsigned path, state = 0; |
| 432 | |
| 433 | if (term != CONV_TERM_FLUSH) { |
| 434 | for (i = 0; i < dec->trellis->num_states; i++) { |
| 435 | sum = dec->trellis->sums[i]; |
| 436 | if (sum > max) { |
| 437 | max = sum; |
| 438 | state = i; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (max < 0) |
| 443 | return -EPROTO; |
| 444 | } |
| 445 | |
| 446 | for (i = dec->len - 1; i >= len; i--) { |
| 447 | path = dec->paths[i][state] + 1; |
| 448 | state = vstate_lshift(state, dec->k, path); |
| 449 | } |
| 450 | |
| 451 | if (dec->recursive) |
| 452 | _traceback_rec(dec, state, out, len); |
| 453 | else |
| 454 | _traceback(dec, state, out, len); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* Release decoder object */ |
| 460 | static void free_vdec(struct vdecoder *dec) |
| 461 | { |
| 462 | if (!dec) |
| 463 | return; |
| 464 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 465 | vdec_free(dec->paths[0]); |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 466 | free(dec->paths); |
| 467 | free_trellis(dec->trellis); |
| 468 | free(dec); |
| 469 | } |
| 470 | |
| 471 | /* Allocate decoder object |
| 472 | * Subtract the constraint length K on the normalization interval to |
| 473 | * accommodate the initialization path metric at state zero. |
| 474 | */ |
| 475 | static struct vdecoder *alloc_vdec(const struct osmo_conv_code *code) |
| 476 | { |
| 477 | int i, ns; |
| 478 | struct vdecoder *dec; |
| 479 | |
| 480 | ns = NUM_STATES(code->K); |
| 481 | |
| 482 | dec = (struct vdecoder *) calloc(1, sizeof(struct vdecoder)); |
| 483 | dec->n = code->N; |
| 484 | dec->k = code->K; |
| 485 | dec->recursive = conv_code_recursive(code); |
| 486 | dec->intrvl = INT16_MAX / (dec->n * INT8_MAX) - dec->k; |
| 487 | |
| 488 | if (dec->k == 5) { |
| 489 | switch (dec->n) { |
| 490 | case 2: |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 491 | #ifdef HAVE_SSE3 |
| 492 | dec->metric_func = !sse3_supported ? |
| 493 | osmo_conv_gen_metrics_k5_n2 : |
| 494 | osmo_conv_gen_metrics_k5_n2_sse; |
| 495 | #else |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 496 | dec->metric_func = osmo_conv_gen_metrics_k5_n2; |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 497 | #endif |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 498 | break; |
| 499 | case 3: |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 500 | #ifdef HAVE_SSE3 |
| 501 | dec->metric_func = !sse3_supported ? |
| 502 | osmo_conv_gen_metrics_k5_n3 : |
| 503 | osmo_conv_gen_metrics_k5_n3_sse; |
| 504 | #else |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 505 | dec->metric_func = osmo_conv_gen_metrics_k5_n3; |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 506 | #endif |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 507 | break; |
| 508 | case 4: |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 509 | #ifdef HAVE_SSE3 |
| 510 | dec->metric_func = !sse3_supported ? |
| 511 | osmo_conv_gen_metrics_k5_n4 : |
| 512 | osmo_conv_gen_metrics_k5_n4_sse; |
| 513 | #else |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 514 | dec->metric_func = osmo_conv_gen_metrics_k5_n4; |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 515 | #endif |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 516 | break; |
| 517 | default: |
| 518 | goto fail; |
| 519 | } |
| 520 | } else if (dec->k == 7) { |
| 521 | switch (dec->n) { |
| 522 | case 2: |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 523 | #ifdef HAVE_SSE3 |
| 524 | dec->metric_func = !sse3_supported ? |
| 525 | osmo_conv_gen_metrics_k7_n2 : |
| 526 | osmo_conv_gen_metrics_k7_n2_sse; |
| 527 | #else |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 528 | dec->metric_func = osmo_conv_gen_metrics_k7_n2; |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 529 | #endif |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 530 | break; |
| 531 | case 3: |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 532 | #ifdef HAVE_SSE3 |
| 533 | dec->metric_func = !sse3_supported ? |
| 534 | osmo_conv_gen_metrics_k7_n3 : |
| 535 | osmo_conv_gen_metrics_k7_n3_sse; |
| 536 | #else |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 537 | dec->metric_func = osmo_conv_gen_metrics_k7_n3; |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 538 | #endif |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 539 | break; |
| 540 | case 4: |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 541 | #ifdef HAVE_SSE3 |
| 542 | dec->metric_func = !sse3_supported ? |
| 543 | osmo_conv_gen_metrics_k7_n4 : |
| 544 | osmo_conv_gen_metrics_k7_n4_sse; |
| 545 | #else |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 546 | dec->metric_func = osmo_conv_gen_metrics_k7_n4; |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 547 | #endif |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 548 | break; |
| 549 | default: |
| 550 | goto fail; |
| 551 | } |
| 552 | } else { |
| 553 | goto fail; |
| 554 | } |
| 555 | |
| 556 | if (code->term == CONV_TERM_FLUSH) |
| 557 | dec->len = code->len + code->K - 1; |
| 558 | else |
| 559 | dec->len = code->len; |
| 560 | |
| 561 | dec->trellis = generate_trellis(code); |
| 562 | if (!dec->trellis) |
| 563 | goto fail; |
| 564 | |
| 565 | dec->paths = (int16_t **) malloc(sizeof(int16_t *) * dec->len); |
| 566 | dec->paths[0] = vdec_malloc(ns * dec->len); |
| 567 | for (i = 1; i < dec->len; i++) |
| 568 | dec->paths[i] = &dec->paths[0][i * ns]; |
| 569 | |
| 570 | return dec; |
| 571 | |
| 572 | fail: |
| 573 | free_vdec(dec); |
| 574 | return NULL; |
| 575 | } |
| 576 | |
| 577 | /* Depuncture sequence with nagative value terminated puncturing matrix */ |
| 578 | static int depuncture(const int8_t *in, const int *punc, int8_t *out, int len) |
| 579 | { |
| 580 | int i, n = 0, m = 0; |
| 581 | |
| 582 | for (i = 0; i < len; i++) { |
| 583 | if (i == punc[n]) { |
| 584 | out[i] = 0; |
| 585 | n++; |
| 586 | continue; |
| 587 | } |
| 588 | |
| 589 | out[i] = in[m++]; |
| 590 | } |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* Forward trellis recursion |
| 596 | * Generate branch metrics and path metrics with a combined function. Only |
| 597 | * accumulated path metric sums and path selections are stored. Normalize on |
| 598 | * the interval specified by the decoder. |
| 599 | */ |
| 600 | static void forward_traverse(struct vdecoder *dec, const int8_t *seq) |
| 601 | { |
| 602 | struct vtrellis *trellis = dec->trellis; |
| 603 | int i; |
| 604 | |
| 605 | for (i = 0; i < dec->len; i++) { |
| 606 | dec->metric_func(&seq[dec->n * i], |
| 607 | trellis->outputs, |
| 608 | trellis->sums, |
| 609 | dec->paths[i], |
| 610 | !(i % dec->intrvl)); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /* Convolutional decode with a decoder object |
| 615 | * Initial puncturing run if necessary followed by the forward recursion. |
| 616 | * For tail-biting perform a second pass before running the backward |
| 617 | * traceback operation. |
| 618 | */ |
| 619 | static int conv_decode(struct vdecoder *dec, const int8_t *seq, |
| 620 | const int *punc, uint8_t *out, int len, int term) |
| 621 | { |
| 622 | int8_t depunc[dec->len * dec->n]; |
| 623 | |
| 624 | reset_decoder(dec, term); |
| 625 | |
| 626 | if (punc) { |
| 627 | depuncture(seq, punc, depunc, dec->len * dec->n); |
| 628 | seq = depunc; |
| 629 | } |
| 630 | |
| 631 | /* Propagate through the trellis with interval normalization */ |
| 632 | forward_traverse(dec, seq); |
| 633 | |
| 634 | if (term == CONV_TERM_TAIL_BITING) |
| 635 | forward_traverse(dec, seq); |
| 636 | |
| 637 | return traceback(dec, out, term, len); |
| 638 | } |
| 639 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 640 | static void osmo_conv_init(void) |
| 641 | { |
| 642 | init_complete = 1; |
| 643 | |
| 644 | #ifdef HAVE___BUILTIN_CPU_SUPPORTS |
| 645 | /* Detect CPU capabilities */ |
| 646 | #ifdef HAVE_AVX2 |
| 647 | avx2_supported = __builtin_cpu_supports("avx2"); |
| 648 | #endif |
| 649 | |
| 650 | #ifdef HAVE_SSE3 |
| 651 | sse3_supported = __builtin_cpu_supports("sse3"); |
| 652 | #endif |
| 653 | |
| 654 | #ifdef HAVE_SSE4_1 |
| 655 | sse41_supported = __builtin_cpu_supports("sse4.1"); |
| 656 | #endif |
| 657 | #endif |
| 658 | |
| 659 | #ifdef HAVE_SSE3 |
| 660 | vdec_malloc = !sse3_supported ? |
| 661 | &osmo_conv_vdec_malloc : &osmo_conv_vdec_malloc_sse3; |
| 662 | vdec_free = !sse3_supported ? |
| 663 | &osmo_conv_vdec_free : &osmo_conv_vdec_free_sse3; |
| 664 | #else |
| 665 | vdec_malloc = &osmo_conv_vdec_malloc; |
| 666 | vdec_free = &osmo_conv_vdec_free; |
| 667 | #endif |
| 668 | } |
| 669 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 670 | /* All-in-one Viterbi decoding */ |
| 671 | int osmo_conv_decode_acc(const struct osmo_conv_code *code, |
| 672 | const sbit_t *input, ubit_t *output) |
| 673 | { |
| 674 | int rc; |
| 675 | struct vdecoder *vdec; |
| 676 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame^] | 677 | if (!init_complete) |
| 678 | osmo_conv_init(); |
| 679 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 680 | if ((code->N < 2) || (code->N > 4) || (code->len < 1) || |
| 681 | ((code->K != 5) && (code->K != 7))) |
| 682 | return -EINVAL; |
| 683 | |
| 684 | vdec = alloc_vdec(code); |
| 685 | if (!vdec) |
| 686 | return -EFAULT; |
| 687 | |
| 688 | rc = conv_decode(vdec, input, code->puncture, |
| 689 | output, code->len, code->term); |
| 690 | |
| 691 | free_vdec(vdec); |
| 692 | |
| 693 | return rc; |
| 694 | } |