-
Notifications
You must be signed in to change notification settings - Fork 15
/
short_bch_code_decoder.hh
130 lines (124 loc) · 2.68 KB
/
short_bch_code_decoder.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Soft decoder for short BCH codes
Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace CODE {
template <int N, int K, int G, int T>
class ShortBCHCodeDecoder
{
static const int P = N - K;
static const int W = 1 << K;
static const int R = 1 << P;
int err[R];
short par[W];
static_assert(N < 8 * sizeof(err[0]), "codeword type not wide enough");
static_assert(P < 8 * sizeof(par[0]), "parity type not wide enough");
static_assert(T > 0 && T <= 3, "unsupported radius T");
static int modgen(int inp)
{
for (int i = K-1; i >= 0; --i) {
int tmp = inp >> (i+P);
inp ^= (tmp & 1) * (G << i);
}
return inp;
}
static int metric(const int8_t *soft, int hard)
{
if (hard < 0)
return -1;
int lol = 8 * sizeof(hard) - 1;
int sum = 0;
for (int i = 0; i < N; ++i)
sum += ((hard << (lol-i)) >> lol) * soft[i];
return sum;
}
public:
ShortBCHCodeDecoder()
{
for (int i = 0; i < W; ++i)
par[i] = modgen(i << P);
for (int i = 0; i < R; ++i)
err[i] = 0;
for (int a = 1<<(N-1); T >= 1 && a; a >>= 1) {
err[modgen(a)] = a;
for (int b = a >> 1; T >= 2 && b; b >>= 1) {
err[modgen(a|b)] = a|b;
for (int c = b >> 1; T >= 3 && c; c >>= 1) {
err[modgen(a|b|c)] = a|b|c;
}
}
}
}
int operator()(int inp)
{
int syn = (par[inp>>P] ^ inp) & (R-1);
int pat = err[syn];
return pat || !syn ? inp ^ pat : -1;
}
int operator()(const int8_t *code)
{
int cw = 0;
for (int i = 0; i < N; ++i)
cw |= (code[i] < 0) << i;
int word = (*this)(cw);
// hard decision
if (0)
return word;
// metric of hard decision
int best = metric(code, word);
int next = -2;
auto update = [code, &word, &best, &next](int hard) {
if (hard == word)
return;
int met = metric(code, hard);
if (met > best) {
next = best;
best = met;
word = hard;
} else if (met > next) {
next = met;
}
};
// flip each bit and see ..
if (0) {
for (int j = 0; j < N; ++j) {
int tmp = 1 << j;
int dec = (*this)(cw ^ tmp);
update(dec);
}
}
// Chase algorithm
if (K > 4) {
const int L = T;
int worst[L] = { 0 };
for (int k = 0; k < N; ++k) {
int j = 0;
while (j < L && std::abs(code[worst[j]]) < std::abs(code[k]))
++j;
for (int i = L-1; i > j; --i)
worst[i] = worst[i-1];
if (j < L)
worst[j] = k;
}
for (int j = 1; j < (1 << L); ++j) {
int tmp = 0;
for (int i = 0; i < L; ++i)
tmp |= ((j>>i)&1) << worst[i];
int dec = (*this)(cw ^ tmp);
update(dec);
}
}
// maximum likelihood
if (K <= 4) {
for (int msg = 0; msg < W; ++msg) {
int enc = (msg << P) | par[msg];
update(enc);
}
}
if (best == next)
return -1;
return word;
}
};
}