-
Notifications
You must be signed in to change notification settings - Fork 15
/
polar_decoder.hh
281 lines (264 loc) · 8.83 KB
/
polar_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
/*
Successive cancellation decoding of polar codes
Copyright 2020 Ahmet Inan <[email protected]>
*/
#pragma once
#include <algorithm>
#include "polar_helper.hh"
namespace CODE {
template <typename TYPE, int M>
struct PolarNode
{
typedef PolarHelper<TYPE> PH;
static const int N = 1 << M;
static void trans(TYPE *out, const TYPE *inp)
{
for (int i = 0; i < N; i += 2) {
out[i] = PH::qmul(inp[i], inp[i+1]);
out[i+1] = inp[i+1];
}
for (int h = 2; h < N; h *= 2)
for (int i = 0; i < N; i += 2 * h)
for (int j = i; j < i + h; ++j)
out[j] = PH::qmul(out[j], out[j+h]);
}
static void rate0(TYPE *hard)
{
for (int i = 0; i < N; ++i)
hard[i] = PH::one();
}
static void rate1(TYPE **message, TYPE *hard, TYPE *soft)
{
for (int i = 0; i < N; ++i)
hard[i] = PH::signum(soft[i+N]);
trans(*message, hard);
*message += N;
}
};
template <typename TYPE>
struct PolarNode<TYPE, 0>
{
typedef PolarHelper<TYPE> PH;
static void rate0(TYPE *hard)
{
*hard = PH::one();
}
static void rate1(TYPE **message, TYPE *hard, TYPE *soft)
{
*hard = PH::signum(soft[1]);
*(*message)++ = *hard;
}
};
template <typename TYPE, int M>
struct PolarTree
{
typedef PolarHelper<TYPE> PH;
static const int N = 1 << M;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, const uint32_t *frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
PolarTree<TYPE, M-1>::decode(message, hard, soft, frozen);
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], soft[i+N], soft[i+N/2+N]);
PolarTree<TYPE, M-1>::decode(message, hard+N/2, soft, frozen+N/2/32);
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(hard[i], hard[i+N/2]);
}
};
template <typename TYPE>
struct PolarTree<TYPE, 6>
{
typedef PolarHelper<TYPE> PH;
static const int M = 6;
static const int N = 1 << M;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, const uint32_t *frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
if (frozen[0] == 0xffffffff)
PolarNode<TYPE, M-1>::rate0(hard);
else if (frozen[0] == 0)
PolarNode<TYPE, M-1>::rate1(message, hard, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard, soft, frozen[0]);
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], soft[i+N], soft[i+N/2+N]);
if (frozen[1] == 0xffffffff)
PolarNode<TYPE, M-1>::rate0(hard+N/2);
else if (frozen[1] == 0)
PolarNode<TYPE, M-1>::rate1(message, hard+N/2, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard+N/2, soft, frozen[1]);
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(hard[i], hard[i+N/2]);
}
};
template <typename TYPE>
struct PolarTree<TYPE, 5>
{
typedef PolarHelper<TYPE> PH;
static const int M = 5;
static const int N = 1 << M;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, uint32_t frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
if ((frozen & ((1<<(1<<(M-1)))-1)) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard);
else if ((frozen & ((1<<(1<<(M-1)))-1)) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], soft[i+N], soft[i+N/2+N]);
if (frozen >> (N/2) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard+N/2);
else if (frozen >> (N/2) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard+N/2, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(hard[i], hard[i+N/2]);
}
};
template <typename TYPE>
struct PolarTree<TYPE, 4>
{
typedef PolarHelper<TYPE> PH;
static const int M = 4;
static const int N = 1 << M;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, uint32_t frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
if ((frozen & ((1<<(1<<(M-1)))-1)) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard);
else if ((frozen & ((1<<(1<<(M-1)))-1)) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], soft[i+N], soft[i+N/2+N]);
if (frozen >> (N/2) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard+N/2);
else if (frozen >> (N/2) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard+N/2, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(hard[i], hard[i+N/2]);
}
};
template <typename TYPE>
struct PolarTree<TYPE, 3>
{
typedef PolarHelper<TYPE> PH;
static const int M = 3;
static const int N = 1 << M;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, uint32_t frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
if ((frozen & ((1<<(1<<(M-1)))-1)) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard);
else if ((frozen & ((1<<(1<<(M-1)))-1)) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], soft[i+N], soft[i+N/2+N]);
if (frozen >> (N/2) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard+N/2);
else if (frozen >> (N/2) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard+N/2, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(hard[i], hard[i+N/2]);
}
};
template <typename TYPE>
struct PolarTree<TYPE, 2>
{
typedef PolarHelper<TYPE> PH;
static const int M = 2;
static const int N = 1 << M;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, uint32_t frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
if ((frozen & ((1<<(1<<(M-1)))-1)) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard);
else if ((frozen & ((1<<(1<<(M-1)))-1)) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], soft[i+N], soft[i+N/2+N]);
if (frozen >> (N/2) == ((1<<(1<<(M-1)))-1))
PolarNode<TYPE, M-1>::rate0(hard+N/2);
else if (frozen >> (N/2) == 0)
PolarNode<TYPE, M-1>::rate1(message, hard+N/2, soft);
else
PolarTree<TYPE, M-1>::decode(message, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(hard[i], hard[i+N/2]);
}
};
template <typename TYPE>
struct PolarTree<TYPE, 1>
{
typedef PolarHelper<TYPE> PH;
static void decode(TYPE **message, TYPE *hard, TYPE *soft, uint32_t frozen)
{
soft[1] = PH::prod(soft[2], soft[3]);
if (frozen & 1)
PolarNode<TYPE, 0>::rate0(hard);
else
PolarNode<TYPE, 0>::rate1(message, hard, soft);
soft[1] = PH::madd(hard[0], soft[2], soft[3]);
if (frozen >> 1)
PolarNode<TYPE, 0>::rate0(hard+1);
else
PolarNode<TYPE, 0>::rate1(message, hard+1, soft);
hard[0] = PH::qmul(hard[0], hard[1]);
}
};
template <typename TYPE, int MAX_M>
class PolarDecoder
{
static_assert(MAX_M >= 5 && MAX_M <= 20);
typedef PolarHelper<TYPE> PH;
static const int MAX_N = 1 << MAX_M;
TYPE soft[2*MAX_N];
TYPE hard[MAX_N];
public:
void operator()(TYPE *message, const TYPE *codeword, const uint32_t *frozen, int level)
{
assert(level <= MAX_M);
int length = 1 << level;
for (int i = 0; i < length; ++i)
soft[length+i] = codeword[i];
switch (level) {
case 5: PolarTree<TYPE, 5>::decode(&message, hard, soft, *frozen); break;
case 6: PolarTree<TYPE, 6>::decode(&message, hard, soft, frozen); break;
case 7: PolarTree<TYPE, 7>::decode(&message, hard, soft, frozen); break;
case 8: PolarTree<TYPE, 8>::decode(&message, hard, soft, frozen); break;
case 9: PolarTree<TYPE, 9>::decode(&message, hard, soft, frozen); break;
case 10: PolarTree<TYPE, 10>::decode(&message, hard, soft, frozen); break;
case 11: PolarTree<TYPE, 11>::decode(&message, hard, soft, frozen); break;
case 12: PolarTree<TYPE, 12>::decode(&message, hard, soft, frozen); break;
case 13: PolarTree<TYPE, 13>::decode(&message, hard, soft, frozen); break;
case 14: PolarTree<TYPE, 14>::decode(&message, hard, soft, frozen); break;
case 15: PolarTree<TYPE, 15>::decode(&message, hard, soft, frozen); break;
case 16: PolarTree<TYPE, 16>::decode(&message, hard, soft, frozen); break;
case 17: PolarTree<TYPE, 17>::decode(&message, hard, soft, frozen); break;
case 18: PolarTree<TYPE, 18>::decode(&message, hard, soft, frozen); break;
case 19: PolarTree<TYPE, 19>::decode(&message, hard, soft, frozen); break;
case 20: PolarTree<TYPE, 20>::decode(&message, hard, soft, frozen); break;
default: assert(false);
}
}
};
}