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 | |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 34 | #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 Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 46 | static 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 Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 53 | * These pointers are being initialized at runtime by the |
| 54 | * osmo_conv_init() depending on supported SIMD extensions. |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 55 | */ |
| 56 | static int16_t *(*vdec_malloc)(size_t n); |
| 57 | static void (*vdec_free)(int16_t *ptr); |
| 58 | |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 59 | void (*osmo_conv_metrics_k5_n2)(const int8_t *seq, |
| 60 | const int16_t *out, int16_t *sums, int16_t *paths, int norm); |
| 61 | void (*osmo_conv_metrics_k5_n3)(const int8_t *seq, |
| 62 | const int16_t *out, int16_t *sums, int16_t *paths, int norm); |
| 63 | void (*osmo_conv_metrics_k5_n4)(const int8_t *seq, |
| 64 | const int16_t *out, int16_t *sums, int16_t *paths, int norm); |
| 65 | void (*osmo_conv_metrics_k7_n2)(const int8_t *seq, |
| 66 | const int16_t *out, int16_t *sums, int16_t *paths, int norm); |
| 67 | void (*osmo_conv_metrics_k7_n3)(const int8_t *seq, |
| 68 | const int16_t *out, int16_t *sums, int16_t *paths, int norm); |
| 69 | void (*osmo_conv_metrics_k7_n4)(const int8_t *seq, |
| 70 | const int16_t *out, int16_t *sums, int16_t *paths, int norm); |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 71 | |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 72 | /* Forward malloc wrappers */ |
| 73 | int16_t *osmo_conv_gen_vdec_malloc(size_t n); |
| 74 | void osmo_conv_gen_vdec_free(int16_t *ptr); |
| 75 | |
| 76 | #if defined(HAVE_SSE3) |
| 77 | int16_t *osmo_conv_sse_vdec_malloc(size_t n); |
| 78 | void osmo_conv_sse_vdec_free(int16_t *ptr); |
| 79 | #endif |
| 80 | |
| 81 | #if defined(HAVE_SSE3) && defined(HAVE_AVX2) |
| 82 | int16_t *osmo_conv_sse_avx_vdec_malloc(size_t n); |
| 83 | void osmo_conv_sse_avx_vdec_free(int16_t *ptr); |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 84 | #endif |
| 85 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 86 | /* Forward Metric Units */ |
| 87 | void osmo_conv_gen_metrics_k5_n2(const int8_t *seq, const int16_t *out, |
| 88 | int16_t *sums, int16_t *paths, int norm); |
| 89 | void osmo_conv_gen_metrics_k5_n3(const int8_t *seq, const int16_t *out, |
| 90 | int16_t *sums, int16_t *paths, int norm); |
| 91 | void osmo_conv_gen_metrics_k5_n4(const int8_t *seq, const int16_t *out, |
| 92 | int16_t *sums, int16_t *paths, int norm); |
| 93 | void osmo_conv_gen_metrics_k7_n2(const int8_t *seq, const int16_t *out, |
| 94 | int16_t *sums, int16_t *paths, int norm); |
| 95 | void osmo_conv_gen_metrics_k7_n3(const int8_t *seq, const int16_t *out, |
| 96 | int16_t *sums, int16_t *paths, int norm); |
| 97 | void 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 Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 100 | #if defined(HAVE_SSE3) |
| 101 | void osmo_conv_sse_metrics_k5_n2(const int8_t *seq, const int16_t *out, |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 102 | int16_t *sums, int16_t *paths, int norm); |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 103 | void osmo_conv_sse_metrics_k5_n3(const int8_t *seq, const int16_t *out, |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 104 | int16_t *sums, int16_t *paths, int norm); |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 105 | void osmo_conv_sse_metrics_k5_n4(const int8_t *seq, const int16_t *out, |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 106 | int16_t *sums, int16_t *paths, int norm); |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 107 | void osmo_conv_sse_metrics_k7_n2(const int8_t *seq, const int16_t *out, |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 108 | int16_t *sums, int16_t *paths, int norm); |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 109 | void osmo_conv_sse_metrics_k7_n3(const int8_t *seq, const int16_t *out, |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 110 | int16_t *sums, int16_t *paths, int norm); |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 111 | void 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) |
| 116 | void osmo_conv_sse_avx_metrics_k5_n2(const int8_t *seq, const int16_t *out, |
| 117 | int16_t *sums, int16_t *paths, int norm); |
| 118 | void osmo_conv_sse_avx_metrics_k5_n3(const int8_t *seq, const int16_t *out, |
| 119 | int16_t *sums, int16_t *paths, int norm); |
| 120 | void osmo_conv_sse_avx_metrics_k5_n4(const int8_t *seq, const int16_t *out, |
| 121 | int16_t *sums, int16_t *paths, int norm); |
| 122 | void osmo_conv_sse_avx_metrics_k7_n2(const int8_t *seq, const int16_t *out, |
| 123 | int16_t *sums, int16_t *paths, int norm); |
| 124 | void osmo_conv_sse_avx_metrics_k7_n3(const int8_t *seq, const int16_t *out, |
| 125 | int16_t *sums, int16_t *paths, int norm); |
| 126 | void osmo_conv_sse_avx_metrics_k7_n4(const int8_t *seq, const int16_t *out, |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 127 | int16_t *sums, int16_t *paths, int norm); |
| 128 | #endif |
| 129 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 130 | /* Trellis State |
| 131 | * state - Internal lshift register value |
| 132 | * prev - Register values of previous 0 and 1 states |
| 133 | */ |
| 134 | struct 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 | */ |
| 145 | struct 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 | */ |
| 162 | struct 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 Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 176 | /* Accessor calls */ |
| 177 | static 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 */ |
| 183 | static 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 */ |
| 198 | static inline unsigned bitswap2(unsigned v) |
| 199 | { |
| 200 | return ((v & 0x02) >> 1) | ((v & 0x01) << 1); |
| 201 | } |
| 202 | |
| 203 | static inline unsigned bitswap3(unsigned v) |
| 204 | { |
| 205 | return ((v & 0x04) >> 2) | ((v & 0x02) >> 0) | |
| 206 | ((v & 0x01) << 2); |
| 207 | } |
| 208 | |
| 209 | static inline unsigned bitswap4(unsigned v) |
| 210 | { |
| 211 | return ((v & 0x08) >> 3) | ((v & 0x04) >> 1) | |
| 212 | ((v & 0x02) << 1) | ((v & 0x01) << 3); |
| 213 | } |
| 214 | |
| 215 | static 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 | |
| 221 | static 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 | |
| 227 | static 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 | */ |
| 254 | static 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 | */ |
| 271 | static 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 */ |
| 296 | static 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 | */ |
| 328 | static 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 */ |
| 369 | static void free_trellis(struct vtrellis *trellis) |
| 370 | { |
| 371 | if (!trellis) |
| 372 | return; |
| 373 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 374 | vdec_free(trellis->outputs); |
| 375 | vdec_free(trellis->sums); |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 376 | free(trellis->vals); |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 377 | 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 | */ |
| 386 | static 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)); |
| 397 | trellis->num_states = ns; |
| 398 | trellis->sums = vdec_malloc(ns); |
| 399 | trellis->outputs = vdec_malloc(ns * olen); |
| 400 | trellis->vals = (uint8_t *) malloc(ns * sizeof(uint8_t)); |
| 401 | |
| 402 | if (!trellis->sums || !trellis->outputs) |
| 403 | goto fail; |
| 404 | |
| 405 | /* Populate the trellis state objects */ |
| 406 | for (i = 0; i < ns; i++) { |
| 407 | outputs = &trellis->outputs[olen * i]; |
| 408 | if (recursive) { |
| 409 | rc = gen_recursive_state_info(&trellis->vals[i], |
| 410 | i, outputs, code); |
| 411 | } else { |
| 412 | rc = gen_state_info(&trellis->vals[i], |
| 413 | i, outputs, code); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (rc < 0) |
| 418 | goto fail; |
| 419 | |
| 420 | return trellis; |
| 421 | |
| 422 | fail: |
| 423 | free_trellis(trellis); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | /* Reset decoder |
| 428 | * Set accumulated path metrics to zero. For termination other than |
| 429 | * tail-biting, initialize the zero state as the encoder starting state. |
| 430 | * Initialize with the maximum accumulated sum at length equal to the |
| 431 | * constraint length. |
| 432 | */ |
| 433 | static void reset_decoder(struct vdecoder *dec, int term) |
| 434 | { |
| 435 | int ns = dec->trellis->num_states; |
| 436 | |
| 437 | memset(dec->trellis->sums, 0, sizeof(int16_t) * ns); |
| 438 | |
| 439 | if (term != CONV_TERM_TAIL_BITING) |
| 440 | dec->trellis->sums[0] = INT8_MAX * dec->n * dec->k; |
| 441 | } |
| 442 | |
| 443 | static void _traceback(struct vdecoder *dec, |
| 444 | unsigned state, uint8_t *out, int len) |
| 445 | { |
| 446 | int i; |
| 447 | unsigned path; |
| 448 | |
| 449 | for (i = len - 1; i >= 0; i--) { |
| 450 | path = dec->paths[i][state] + 1; |
| 451 | out[i] = dec->trellis->vals[state]; |
| 452 | state = vstate_lshift(state, dec->k, path); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static void _traceback_rec(struct vdecoder *dec, |
| 457 | unsigned state, uint8_t *out, int len) |
| 458 | { |
| 459 | int i; |
| 460 | unsigned path; |
| 461 | |
| 462 | for (i = len - 1; i >= 0; i--) { |
| 463 | path = dec->paths[i][state] + 1; |
| 464 | out[i] = path ^ dec->trellis->vals[state]; |
| 465 | state = vstate_lshift(state, dec->k, path); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* Traceback and generate decoded output |
| 470 | * Find the largest accumulated path metric at the final state except for |
| 471 | * the zero terminated case, where we assume the final state is always zero. |
| 472 | */ |
| 473 | static int traceback(struct vdecoder *dec, uint8_t *out, int term, int len) |
| 474 | { |
| 475 | int i, sum, max = -1; |
| 476 | unsigned path, state = 0; |
| 477 | |
| 478 | if (term != CONV_TERM_FLUSH) { |
| 479 | for (i = 0; i < dec->trellis->num_states; i++) { |
| 480 | sum = dec->trellis->sums[i]; |
| 481 | if (sum > max) { |
| 482 | max = sum; |
| 483 | state = i; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (max < 0) |
| 488 | return -EPROTO; |
| 489 | } |
| 490 | |
| 491 | for (i = dec->len - 1; i >= len; i--) { |
| 492 | path = dec->paths[i][state] + 1; |
| 493 | state = vstate_lshift(state, dec->k, path); |
| 494 | } |
| 495 | |
| 496 | if (dec->recursive) |
| 497 | _traceback_rec(dec, state, out, len); |
| 498 | else |
| 499 | _traceback(dec, state, out, len); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /* Release decoder object */ |
| 505 | static void free_vdec(struct vdecoder *dec) |
| 506 | { |
| 507 | if (!dec) |
| 508 | return; |
| 509 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 510 | vdec_free(dec->paths[0]); |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 511 | free(dec->paths); |
| 512 | free_trellis(dec->trellis); |
| 513 | 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 | */ |
| 520 | static 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 Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 536 | dec->metric_func = osmo_conv_metrics_k5_n2; |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 537 | break; |
| 538 | case 3: |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 539 | dec->metric_func = osmo_conv_metrics_k5_n3; |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 540 | break; |
| 541 | case 4: |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 542 | dec->metric_func = osmo_conv_metrics_k5_n4; |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 543 | break; |
| 544 | default: |
| 545 | goto fail; |
| 546 | } |
| 547 | } else if (dec->k == 7) { |
| 548 | switch (dec->n) { |
| 549 | case 2: |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 550 | dec->metric_func = osmo_conv_metrics_k7_n2; |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 551 | break; |
| 552 | case 3: |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 553 | dec->metric_func = osmo_conv_metrics_k7_n3; |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 554 | break; |
| 555 | case 4: |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 556 | dec->metric_func = osmo_conv_metrics_k7_n4; |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 557 | 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); |
| 575 | dec->paths[0] = vdec_malloc(ns * dec->len); |
| 576 | for (i = 1; i < dec->len; i++) |
| 577 | dec->paths[i] = &dec->paths[0][i * ns]; |
| 578 | |
| 579 | return dec; |
| 580 | |
| 581 | fail: |
| 582 | free_vdec(dec); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | /* Depuncture sequence with nagative value terminated puncturing matrix */ |
| 587 | static int depuncture(const int8_t *in, const int *punc, int8_t *out, int len) |
| 588 | { |
| 589 | int i, n = 0, m = 0; |
| 590 | |
| 591 | for (i = 0; i < len; i++) { |
| 592 | if (i == punc[n]) { |
| 593 | out[i] = 0; |
| 594 | n++; |
| 595 | continue; |
| 596 | } |
| 597 | |
| 598 | out[i] = in[m++]; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* Forward trellis recursion |
| 605 | * Generate branch metrics and path metrics with a combined function. Only |
| 606 | * accumulated path metric sums and path selections are stored. Normalize on |
| 607 | * the interval specified by the decoder. |
| 608 | */ |
| 609 | static void forward_traverse(struct vdecoder *dec, const int8_t *seq) |
| 610 | { |
| 611 | struct vtrellis *trellis = dec->trellis; |
| 612 | int i; |
| 613 | |
| 614 | for (i = 0; i < dec->len; i++) { |
| 615 | dec->metric_func(&seq[dec->n * i], |
| 616 | trellis->outputs, |
| 617 | trellis->sums, |
| 618 | dec->paths[i], |
| 619 | !(i % dec->intrvl)); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* Convolutional decode with a decoder object |
| 624 | * Initial puncturing run if necessary followed by the forward recursion. |
| 625 | * For tail-biting perform a second pass before running the backward |
| 626 | * traceback operation. |
| 627 | */ |
| 628 | static int conv_decode(struct vdecoder *dec, const int8_t *seq, |
| 629 | const int *punc, uint8_t *out, int len, int term) |
| 630 | { |
| 631 | int8_t depunc[dec->len * dec->n]; |
| 632 | |
| 633 | reset_decoder(dec, term); |
| 634 | |
| 635 | if (punc) { |
| 636 | depuncture(seq, punc, depunc, dec->len * dec->n); |
| 637 | seq = depunc; |
| 638 | } |
| 639 | |
| 640 | /* Propagate through the trellis with interval normalization */ |
| 641 | forward_traverse(dec, seq); |
| 642 | |
| 643 | if (term == CONV_TERM_TAIL_BITING) |
| 644 | forward_traverse(dec, seq); |
| 645 | |
| 646 | return traceback(dec, out, term, len); |
| 647 | } |
| 648 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 649 | static void osmo_conv_init(void) |
| 650 | { |
| 651 | init_complete = 1; |
| 652 | |
| 653 | #ifdef HAVE___BUILTIN_CPU_SUPPORTS |
| 654 | /* Detect CPU capabilities */ |
| 655 | #ifdef HAVE_AVX2 |
| 656 | avx2_supported = __builtin_cpu_supports("avx2"); |
| 657 | #endif |
| 658 | |
| 659 | #ifdef HAVE_SSE3 |
| 660 | sse3_supported = __builtin_cpu_supports("sse3"); |
| 661 | #endif |
| 662 | |
| 663 | #ifdef HAVE_SSE4_1 |
| 664 | sse41_supported = __builtin_cpu_supports("sse4.1"); |
| 665 | #endif |
| 666 | #endif |
| 667 | |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 668 | /** |
| 669 | * Usage of curly braces is mandatory, |
| 670 | * because we use multi-line define. |
| 671 | */ |
| 672 | #if defined(HAVE_SSE3) && defined(HAVE_AVX2) |
| 673 | if (sse3_supported && avx2_supported) { |
| 674 | INIT_POINTERS(sse_avx); |
| 675 | } else if (sse3_supported) { |
| 676 | INIT_POINTERS(sse); |
| 677 | } else { |
| 678 | INIT_POINTERS(gen); |
| 679 | } |
| 680 | #elif defined(HAVE_SSE3) |
| 681 | if (sse3_supported) { |
| 682 | INIT_POINTERS(sse); |
| 683 | } else { |
| 684 | INIT_POINTERS(gen); |
| 685 | } |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 686 | #else |
Vadim Yanitskiy | 0d49f47 | 2017-05-28 18:20:02 +0700 | [diff] [blame] | 687 | INIT_POINTERS(gen); |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 688 | #endif |
| 689 | } |
| 690 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 691 | /* All-in-one Viterbi decoding */ |
| 692 | int osmo_conv_decode_acc(const struct osmo_conv_code *code, |
| 693 | const sbit_t *input, ubit_t *output) |
| 694 | { |
| 695 | int rc; |
| 696 | struct vdecoder *vdec; |
| 697 | |
Tom Tsou | 34e228a | 2017-04-29 00:16:43 +0700 | [diff] [blame] | 698 | if (!init_complete) |
| 699 | osmo_conv_init(); |
| 700 | |
Tom Tsou | 3553680 | 2016-11-24 19:24:32 +0700 | [diff] [blame] | 701 | if ((code->N < 2) || (code->N > 4) || (code->len < 1) || |
| 702 | ((code->K != 5) && (code->K != 7))) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | vdec = alloc_vdec(code); |
| 706 | if (!vdec) |
| 707 | return -EFAULT; |
| 708 | |
| 709 | rc = conv_decode(vdec, input, code->puncture, |
| 710 | output, code->len, code->term); |
| 711 | |
| 712 | free_vdec(vdec); |
| 713 | |
| 714 | return rc; |
| 715 | } |