Repository navigation
Expand file tree
/
Copy pathpolar_sequence.hh
More file actions
130 lines (121 loc) · 3.05 KB
/
Copy pathpolar_sequence.hh
File metadata and controls
130 lines (121 loc) · 3.05 KB
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
/*
Construct reliability sequences for polar codes
Copyright 2023 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
#include <limits>
#include "sort.hh"
namespace CODE {
// erasure probabilities above one half are stored as their negated
// complement, as values close to one would otherwise round to one.
// squares are kept above zero, so the sign survives underflow and
// doesn't depend on signed zeros, which -ffast-math ignores
template <int MAX_M>
class BhattacharyyaSequence
{
static double complement(double pe)
{
bool comp = pe > 0.5;
if (comp)
pe = 1 - pe;
if (pe < std::numeric_limits<double>::min())
pe = std::numeric_limits<double>::min();
return comp ? -pe : pe;
}
void compute(double pe, int i, int h)
{
if (h) {
bool comp = pe < 0;
double x = comp ? -pe : pe;
double square = x * x;
if (square < std::numeric_limits<double>::min())
square = std::numeric_limits<double>::min();
double grow = x * (2-x);
bool flip = grow > 0.5;
if (flip)
grow = (1-x) * (1-x);
if (comp != flip)
grow = -grow;
if (comp)
square = -square;
compute(comp ? square : grow, i, h/2);
compute(comp ? grow : square, i+h, h/2);
} else {
prob[i] = pe;
}
}
double prob[1<<MAX_M];
MergeSort<int, 1<<MAX_M> sort;
public:
void operator()(int *sequence, int level, double erasure_probability = std::exp(-1.))
{
assert(level <= MAX_M);
int length = 1 << level;
compute(complement(erasure_probability), 0, length / 2);
for (int i = 0; i < length; ++i)
sequence[i] = i;
// least reliable first, ties in the same order as ReedMullerSequence
sort(sequence, length, [this](int a, int b){
bool na = prob[a] < 0, nb = prob[b] < 0;
if (na != nb)
return na;
if (prob[a] != prob[b])
return prob[a] > prob[b];
int x = __builtin_popcount(a);
int y = __builtin_popcount(b);
if (x != y)
return x < y;
return a < b;
});
}
};
template <int MAX_M>
class PolarizationWeightSequence
{
double weight[1<<MAX_M];
MergeSort<int, 1<<MAX_M> sort;
public:
void operator()(int *sequence, int level, double beta = std::pow(2., .25))
{
assert(level <= MAX_M);
int length = 1 << level;
weight[0] = 0;
double power = 1;
for (int h = 1; h < length; h *= 2, power *= beta)
for (int i = 0; i < h; ++i)
weight[i+h] = weight[i] + power;
for (int i = 0; i < length; ++i)
sequence[i] = i;
// least reliable first, ties in the same order as ReedMullerSequence
sort(sequence, length, [this](int a, int b){
if (weight[a] != weight[b])
return weight[a] < weight[b];
int x = __builtin_popcount(a);
int y = __builtin_popcount(b);
if (x != y)
return x < y;
return a < b;
});
}
};
template <int MAX_M>
class ReedMullerSequence
{
MergeSort<int, 1<<MAX_M> sort;
public:
void operator()(int *sequence, int level)
{
assert(level <= MAX_M);
int length = 1 << level;
for (int i = 0; i < length; ++i)
sequence[i] = i;
sort(sequence, length, [](int a, int b){
int x = __builtin_popcount(a);
int y = __builtin_popcount(b);
if (x != y)
return x < y;
return a < b;
});
}
};
}