blob: 7972c39653f097d5e46371b31f6c0c13716d56f1 [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 <stdint.h>
24#include <string.h>
25
26/* Add-Compare-Select (ACS-Butterfly)
27 * Compute 4 accumulated path metrics and 4 path selections. Note that path
28 * selections are store as -1 and 0 rather than 0 and 1. This is to match
29 * the output format of the SSE packed compare instruction 'pmaxuw'.
30 */
31
32static void acs_butterfly(int state, int num_states,
33 int16_t metric, int16_t *sum,
34 int16_t *new_sum, int16_t *path)
35{
36 int state0, state1;
37 int sum0, sum1, sum2, sum3;
38
39 state0 = *(sum + (2 * state + 0));
40 state1 = *(sum + (2 * state + 1));
41
42 sum0 = state0 + metric;
43 sum1 = state1 - metric;
44 sum2 = state0 - metric;
45 sum3 = state1 + metric;
46
47 if (sum0 >= sum1) {
48 *new_sum = sum0;
49 *path = -1;
50 } else {
51 *new_sum = sum1;
52 *path = 0;
53 }
54
55 if (sum2 >= sum3) {
56 *(new_sum + num_states / 2) = sum2;
57 *(path + num_states / 2) = -1;
58 } else {
59 *(new_sum + num_states / 2) = sum3;
60 *(path + num_states / 2) = 0;
61 }
62}
63
64/* Branch metrics unit N=2 */
65static void gen_branch_metrics_n2(int num_states, const int8_t *seq,
66 const int16_t *out, int16_t *metrics)
67{
68 int i;
69
70 for (i = 0; i < num_states / 2; i++) {
71 metrics[i] = seq[0] * out[2 * i + 0] +
72 seq[1] * out[2 * i + 1];
73 }
74}
75
76/* Branch metrics unit N=3 */
77static void gen_branch_metrics_n3(int num_states, const int8_t *seq,
78 const int16_t *out, int16_t *metrics)
79{
80 int i;
81
82 for (i = 0; i < num_states / 2; i++) {
83 metrics[i] = seq[0] * out[4 * i + 0] +
84 seq[1] * out[4 * i + 1] +
85 seq[2] * out[4 * i + 2];
86 }
87}
88
89/* Branch metrics unit N=4 */
90static void gen_branch_metrics_n4(int num_states, const int8_t *seq,
91 const int16_t *out, int16_t *metrics)
92{
93 int i;
94
95 for (i = 0; i < num_states / 2; i++) {
96 metrics[i] = seq[0] * out[4 * i + 0] +
97 seq[1] * out[4 * i + 1] +
98 seq[2] * out[4 * i + 2] +
99 seq[3] * out[4 * i + 3];
100 }
101}
102
103/* Path metric unit */
104static void gen_path_metrics(int num_states, int16_t *sums,
105 int16_t *metrics, int16_t *paths, int norm)
106{
107 int i;
108 int16_t min;
109 int16_t new_sums[num_states];
110
111 for (i = 0; i < num_states / 2; i++)
112 acs_butterfly(i, num_states, metrics[i],
113 sums, &new_sums[i], &paths[i]);
114
115 if (norm) {
116 min = new_sums[0];
117
118 for (i = 1; i < num_states; i++)
119 if (new_sums[i] < min)
120 min = new_sums[i];
121
122 for (i = 0; i < num_states; i++)
123 new_sums[i] -= min;
124 }
125
126 memcpy(sums, new_sums, num_states * sizeof(int16_t));
127}
128
129/* 16-state branch-path metrics units (K=5) */
130__attribute__ ((visibility("hidden")))
131void osmo_conv_gen_metrics_k5_n2(const int8_t *seq, const int16_t *out,
132 int16_t *sums, int16_t *paths, int norm)
133{
134 int16_t metrics[8];
135
136 gen_branch_metrics_n2(16, seq, out, metrics);
137 gen_path_metrics(16, sums, metrics, paths, norm);
138}
139
140__attribute__ ((visibility("hidden")))
141void osmo_conv_gen_metrics_k5_n3(const int8_t *seq, const int16_t *out,
142 int16_t *sums, int16_t *paths, int norm)
143{
144 int16_t metrics[8];
145
146 gen_branch_metrics_n3(16, seq, out, metrics);
147 gen_path_metrics(16, sums, metrics, paths, norm);
148
149}
150
151__attribute__ ((visibility("hidden")))
152void osmo_conv_gen_metrics_k5_n4(const int8_t *seq, const int16_t *out,
153 int16_t *sums, int16_t *paths, int norm)
154{
155 int16_t metrics[8];
156
157 gen_branch_metrics_n4(16, seq, out, metrics);
158 gen_path_metrics(16, sums, metrics, paths, norm);
159
160}
161
162/* 64-state branch-path metrics units (K=7) */
163__attribute__ ((visibility("hidden")))
164void osmo_conv_gen_metrics_k7_n2(const int8_t *seq, const int16_t *out,
165 int16_t *sums, int16_t *paths, int norm)
166{
167 int16_t metrics[32];
168
169 gen_branch_metrics_n2(64, seq, out, metrics);
170 gen_path_metrics(64, sums, metrics, paths, norm);
171
172}
173
174__attribute__ ((visibility("hidden")))
175void osmo_conv_gen_metrics_k7_n3(const int8_t *seq, const int16_t *out,
176 int16_t *sums, int16_t *paths, int norm)
177{
178 int16_t metrics[32];
179
180 gen_branch_metrics_n3(64, seq, out, metrics);
181 gen_path_metrics(64, sums, metrics, paths, norm);
182
183}
184
185__attribute__ ((visibility("hidden")))
186void osmo_conv_gen_metrics_k7_n4(const int8_t *seq, const int16_t *out,
187 int16_t *sums, int16_t *paths, int norm)
188{
189 int16_t metrics[32];
190
191 gen_branch_metrics_n4(64, seq, out, metrics);
192 gen_path_metrics(64, sums, metrics, paths, norm);
193}