-
Notifications
You must be signed in to change notification settings - Fork 15
/
ldpc_decoder.hh
288 lines (265 loc) · 6.77 KB
/
ldpc_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
LDPC SISO layered decoder
Copyright 2018 Ahmet Inan <[email protected]>
*/
#pragma once
#include <algorithm>
#include "simd.hh"
#include "rotate.hh"
namespace CODE {
template <typename TABLE, int BETA>
class LDPCDecoder
{
#ifdef __AVX2__
static const int SIMD_SIZE = 32;
// M = 360 = 30 * 12
static const int WORD_SIZE = 30;
#else
static const int SIMD_SIZE = 16;
// M = 360 = 15 * 24
static const int WORD_SIZE = 15;
#endif
static_assert(TABLE::M % WORD_SIZE == 0, "M must be multiple of word size");
static_assert(WORD_SIZE <= SIMD_SIZE, "SIMD size must be bigger or equal word size");
static const int M = TABLE::M;
static const int N = TABLE::N;
static const int K = TABLE::K;
static const int R = N-K;
static const int q = R/M;
static const int D = WORD_SIZE;
static const int W = M/D;
static const int PTY = R/D;
static const int MSG = K/D;
static const int CNC = TABLE::LINKS_MAX_CN - 2;
static const int BNL = (TABLE::LINKS_TOTAL + D-1) / D;
static const int LOC = (TABLE::LINKS_TOTAL - (2*R-1) + D-1) / D;
typedef SIMD<int8_t, SIMD_SIZE> TYPE;
typedef struct { uint16_t off; uint16_t shi; } Loc;
typedef uint32_t wd_t;
static_assert(sizeof(wd_t) * 8 >= CNC, "write disable mask needs at least as many bits as max check node links");
Rotate<TYPE, D> rotate;
TYPE bnl[BNL];
TYPE msg[MSG];
TYPE pty[PTY];
Loc loc[LOC];
wd_t wd[PTY];
uint8_t cnc[q];
static TYPE eor(TYPE a, TYPE b)
{
return vreinterpret<TYPE>(veor(vmask(a), vmask(b)));
}
static TYPE orr(TYPE a, TYPE b)
{
return vreinterpret<TYPE>(vorr(vmask(a), vmask(b)));
}
static TYPE other(TYPE a, TYPE b, TYPE c)
{
return vreinterpret<TYPE>(vbsl(vceq(a, b), vmask(c), vmask(b)));
}
static TYPE mine(TYPE a, TYPE b)
{
return orr(eor(a, b), vdup<TYPE>(127));
}
static TYPE selfcorr(TYPE a, TYPE b)
{
return vreinterpret<TYPE>(vand(vmask(b), vorr(vceqz(a), veor(vcgtz(a), vcltz(b)))));
}
bool bad()
{
Loc *lo = loc;
for (int i = 0; i < q; ++i) {
int cnt = cnc[i];
int deg = cnt + 2;
auto res = vmask(vzero<TYPE>());
for (int j = 0; j < W; ++j) {
TYPE cnv = vdup<TYPE>(1);
for (int k = 0; k < deg; ++k) {
TYPE tmp;
if (k < cnt) {
tmp = rotate(msg[lo[k].off], -lo[k].shi);
} else if (k == cnt) {
tmp = pty[W*i+j];
} else {
if (i) {
tmp = pty[W*(i-1)+j];
} else if (j) {
tmp = pty[W*(q-1)+j-1];
} else {
tmp = rotate(pty[PTY-1], 1);
tmp.v[0] = 127;
}
}
cnv = vsign(cnv, tmp);
}
res = vorr(res, vclez(cnv));
lo += cnt;
}
for (int n = 0; n < D; ++n)
if (res.v[n])
return true;
}
return false;
}
void update()
{
TYPE *bl = bnl;
Loc *lo = loc;
for (int i = 0; i < q; ++i) {
int cnt = cnc[i];
int deg = cnt + 2;
for (int j = 0; j < W; ++j) {
TYPE mags[deg], inps[deg];
TYPE min0 = vdup<TYPE>(127);
TYPE min1 = vdup<TYPE>(127);
TYPE signs = vdup<TYPE>(127);
for (int k = 0; k < deg; ++k) {
TYPE tmp;
if (k < cnt) {
tmp = rotate(msg[lo[k].off], -lo[k].shi);
} else if (k == cnt) {
tmp = pty[W*i+j];
} else {
if (i) {
tmp = pty[W*(i-1)+j];
} else if (j) {
tmp = pty[W*(q-1)+j-1];
} else {
tmp = rotate(pty[PTY-1], 1);
tmp.v[0] = 127;
}
}
TYPE inp = vqsub(tmp, bl[k]);
TYPE mag = vqabs(inp);
if (BETA) {
auto beta = vunsigned(vdup<TYPE>(BETA));
mag = vsigned(vqsub(vunsigned(mag), beta));
}
min1 = vmin(min1, vmax(min0, mag));
min0 = vmin(min0, mag);
signs = eor(signs, inp);
inps[k] = inp;
mags[k] = mag;
}
for (int k = 0; k < deg; ++k) {
TYPE mag = mags[k];
TYPE inp = inps[k];
TYPE out = vsign(other(mag, min0, min1), mine(signs, inp));
out = vclamp(out, -31, 31);
out = selfcorr(bl[k], out);
TYPE tmp = vqadd(inp, out);
if (k < cnt) {
if (!((wd[W*i+j]>>k)&1)) {
bl[k] = out;
msg[lo[k].off] = rotate(tmp, lo[k].shi);
}
} else if (k == cnt) {
bl[k] = out;
pty[W*i+j] = tmp;
} else {
bl[k] = out;
if (i) {
pty[W*(i-1)+j] = tmp;
} else if (j) {
pty[W*(q-1)+j-1] = tmp;
} else {
tmp.v[0] = pty[PTY-1].v[D-1];
pty[PTY-1] = rotate(tmp, -1);
}
}
}
if (wd[W*i+j]) {
for (int first = 0, c = 1; c < cnt; ++c) {
if (lo[first].off != lo[c].off || c == cnt-1) {
int last = c - 1;
if (c == cnt-1)
++last;
if (last != first) {
int count = last - first + 1;
wd_t mask = ((1 << count) - 1) << first;
wd_t cur = wd[W*i+j];
wd_t tmp = cur & mask;
wd_t ror = (tmp >> 1) | (tmp << (count-1));
wd[W*i+j] = (cur & ~mask) | (ror & mask);
}
first = c;
}
}
}
lo += cnt;
bl += deg;
}
}
//assert(bl <= bnl + BNL);
//std::cerr << BNL - (bl - bnl) << std::endl;
}
public:
LDPCDecoder()
{
uint16_t pos[q * CNC];
for (int i = 0; i < q; ++i)
cnc[i] = 0;
int bit_pos = 0;
const int *row_ptr = TABLE::POS;
for (int g = 0; TABLE::LEN[g]; ++g) {
int bit_deg = TABLE::DEG[g];
for (int r = 0; r < TABLE::LEN[g]; ++r) {
for (int d = 0; d < bit_deg; ++d) {
int n = row_ptr[d] % q;
int m = row_ptr[d] / q;
pos[CNC*n+cnc[n]++] = bit_pos + (M - m) % M;
}
row_ptr += bit_deg;
bit_pos += M;
}
}
Loc *lo = loc;
for (int i = 0; i < q; ++i) {
int cnt = cnc[i];
int offset[cnt], shift[cnt];
for (int c = 0; c < cnt; ++c) {
shift[c] = pos[CNC*i+c] % M;
offset[c] = pos[CNC*i+c] - shift[c];
}
for (int j = 0; j < W; ++j) {
for (int c = 0; c < cnt; ++c) {
lo[c].off = offset[c] / D + shift[c] % W;
lo[c].shi = shift[c] / W;
shift[c] = (shift[c] + 1) % M;
}
std::sort(lo, lo + cnt, [](const Loc &a, const Loc &b){ return a.off < b.off; });
wd[W*i+j] = 0;
for (int c = 0; c < cnt-1; ++c)
if (lo[c].off == lo[c+1].off)
wd[W*i+j] |= 1 << c;
lo += cnt;
}
}
//assert(lo <= loc + LOC);
//std::cerr << LOC - (lo - loc) << std::endl;
}
int operator()(int8_t *message, int8_t *parity, int trials = 25)
{
for (int i = 0; i < BNL; ++i)
bnl[i] = vzero<TYPE>();
for (int i = 0; i < K/M; ++i)
for (int j = 0; j < W; ++j)
for (int n = 0; n < D; ++n)
msg[W*i+j].v[n] = message[M*i+W*n+j];
for (int i = 0; i < q; ++i)
for (int j = 0; j < W; ++j)
for (int n = 0; n < D; ++n)
pty[W*i+j].v[n] = parity[q*(W*n+j)+i];
while (bad() && --trials >= 0)
update();
for (int i = 0; i < K/M; ++i)
for (int j = 0; j < W; ++j)
for (int n = 0; n < D; ++n)
message[M*i+W*n+j] = msg[W*i+j].v[n];
for (int i = 0; i < q; ++i)
for (int j = 0; j < W; ++j)
for (int n = 0; n < D; ++n)
parity[q*(W*n+j)+i] = pty[W*i+j].v[n];
return trials;
}
};
}