-
Notifications
You must be signed in to change notification settings - Fork 24
/
layered_decoder.hh
144 lines (138 loc) · 3.23 KB
/
layered_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
LDPC SISO layered decoder
Copyright 2018 Ahmet Inan <[email protected]>
*/
#ifndef LAYERED_DECODER_HH
#define LAYERED_DECODER_HH
#include "ldpc.hh"
template <typename TYPE, typename ALG>
class LDPCDecoder
{
TYPE *bnl, *pty;
uint16_t *pos;
uint8_t *cnc;
ALG alg;
int M, N, K, R, q, CNL, LT;
bool initialized;
void reset()
{
for (int i = 0; i < LT; ++i)
bnl[i] = alg.zero();
}
bool bad(TYPE *data, TYPE *parity, int blocks)
{
for (int i = 0; i < q; ++i) {
int cnt = cnc[i];
for (int j = 0; j < M; ++j) {
TYPE cnv = alg.sign(alg.one(), parity[M*i+j]);
if (i)
cnv = alg.sign(cnv, parity[M*(i-1)+j]);
else if (j)
cnv = alg.sign(cnv, parity[j+(q-1)*M-1]);
for (int c = 0; c < cnt; ++c)
cnv = alg.sign(cnv, data[pos[CNL*(M*i+j)+c]]);
if (alg.bad(cnv, blocks))
return true;
}
}
return false;
}
void update(TYPE *data, TYPE *parity)
{
TYPE *bl = bnl;
for (int i = 0; i < q; ++i) {
int cnt = cnc[i];
for (int j = 0; j < M; ++j) {
int deg = cnt + 2 - !(i|j);
TYPE inp[deg], out[deg];
for (int c = 0; c < cnt; ++c)
inp[c] = out[c] = alg.sub(data[pos[CNL*(M*i+j)+c]], bl[c]);
inp[cnt] = out[cnt] = alg.sub(parity[M*i+j], bl[cnt]);
if (i)
inp[cnt+1] = out[cnt+1] = alg.sub(parity[M*(i-1)+j], bl[cnt+1]);
else if (j)
inp[cnt+1] = out[cnt+1] = alg.sub(parity[j+(q-1)*M-1], bl[cnt+1]);
alg.finalp(out, deg);
for (int d = 0; d < deg; ++d)
alg.update(bl+d, out[d]);
for (int c = 0; c < cnt; ++c)
data[pos[CNL*(M*i+j)+c]] = alg.add(inp[c], bl[c]);
parity[M*i+j] = alg.add(inp[cnt], bl[cnt]);
if (i)
parity[M*(i-1)+j] = alg.add(inp[cnt+1], bl[cnt+1]);
else if (j)
parity[j+(q-1)*M-1] = alg.add(inp[cnt+1], bl[cnt+1]);
bl += deg;
}
}
}
public:
LDPCDecoder() : initialized(false)
{
}
void init(LDPCInterface *it)
{
if (initialized) {
delete[] bnl;
delete[] pty;
delete[] cnc;
delete[] pos;
}
initialized = true;
LDPCInterface *ldpc = it->clone();
N = ldpc->code_len();
K = ldpc->data_len();
M = ldpc->group_len();
R = N - K;
q = R / M;
CNL = ldpc->links_max_cn() - 2;
pos = new uint16_t[R * CNL];
cnc = new uint8_t[R];
for (int i = 0; i < R; ++i)
cnc[i] = 0;
ldpc->first_bit();
for (int j = 0; j < K; ++j) {
int *acc_pos = ldpc->acc_pos();
int bit_deg = ldpc->bit_deg();
for (int n = 0; n < bit_deg; ++n) {
int i = acc_pos[n];
pos[CNL*i+cnc[i]++] = j;
}
ldpc->next_bit();
}
LT = ldpc->links_total();
delete ldpc;
bnl = new TYPE[LT];
pty = new TYPE[R];
uint16_t *tmp = new uint16_t[R * CNL];
for (int i = 0; i < q; ++i)
for (int j = 0; j < M; ++j)
for (int c = 0; c < CNL; ++c)
tmp[CNL*(M*i+j)+c] = pos[CNL*(q*j+i)+c];
delete[] pos;
pos = tmp;
}
int operator()(TYPE *data, TYPE *parity, int trials = 25, int blocks = 1)
{
reset();
for (int i = 0; i < q; ++i)
for (int j = 0; j < M; ++j)
pty[M*i+j] = parity[q*j+i];
while (bad(data, pty, blocks) && --trials >= 0)
update(data, pty);
for (int i = 0; i < q; ++i)
for (int j = 0; j < M; ++j)
parity[q*j+i] = pty[M*i+j];
return trials;
}
~LDPCDecoder()
{
if (initialized) {
delete[] bnl;
delete[] pty;
delete[] cnc;
delete[] pos;
}
}
};
#endif