use a rank map instead of a frozen bit array

This commit is contained in:
Ahmet Inan 2026-02-01 22:07:15 +01:00
commit 9c55719f85
3 changed files with 53 additions and 177 deletions

View file

@ -14,10 +14,6 @@ template <typename TYPE>
class PACEncoder
{
typedef PolarHelper<TYPE> PH;
static bool get(const uint32_t *bits, int idx)
{
return (bits[idx/32] >> (idx%32)) & 1;
}
static bool conv(int *state, bool input)
{
// 1011011
@ -30,13 +26,14 @@ class PACEncoder
return output;
}
public:
void operator()(TYPE *codeword, const TYPE *message, const uint32_t *frozen, int level)
void operator()(TYPE *codeword, const TYPE *message, const int *rank_map, int mesg_bits, int level)
{
int length = 1 << level;
int state = 0;
int frozen = length - mesg_bits;
for (int i = 0; i < length; i += 2) {
TYPE msg0 = get(frozen, i) ? PH::one() : *message++;
TYPE msg1 = get(frozen, i+1) ? PH::one() : *message++;
TYPE msg0 = rank_map[i] < frozen ? PH::one() : *message++;
TYPE msg1 = rank_map[i+1] < frozen ? PH::one() : *message++;
msg0 = 1 - 2 * conv(&state, msg0 < 0);
msg1 = 1 - 2 * conv(&state, msg1 < 0);
codeword[i] = PH::qmul(msg0, msg1);

View file

@ -11,8 +11,29 @@ Copyright 2025 Ahmet Inan <inan@aicodix.de>
namespace CODE {
template <typename TYPE, int M>
struct PACListTree
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, TYPE *hard, TYPE *soft, const int *rank, int frozen)
{
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::prod(soft[i+N], soft[i+N/2+N]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, rank, frozen);
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, rank+N/2, frozen);
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListLeaf
struct PACListTree<TYPE, 1>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
@ -76,158 +97,19 @@ struct PACListLeaf
*hard = hrd;
return map;
}
};
template <typename TYPE, int M>
struct PACListTree
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, 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]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, frozen);
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, frozen+N/2/32);
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListTree<TYPE, 6>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int M = 6;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, 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]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, frozen[0]);
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, frozen[1]);
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListTree<TYPE, 5>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int M = 5;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, 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]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListTree<TYPE, 4>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int M = 4;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, 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]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListTree<TYPE, 3>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int M = 3;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, 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]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListTree<TYPE, 2>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static const int M = 2;
static const int N = 1 << M;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, 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]);
MAP lmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard, soft, frozen & ((1<<(1<<(M-1)))-1));
for (int i = 0; i < N/2; ++i)
soft[i+N/2] = PH::madd(hard[i], vshuf(soft[i+N], lmap), vshuf(soft[i+N/2+N], lmap));
MAP rmap = PACListTree<TYPE, M-1>::decode(metric, message, maps, count, state, hard+N/2, soft, frozen >> (N/2));
for (int i = 0; i < N/2; ++i)
hard[i] = PH::qmul(vshuf(hard[i], rmap), hard[i+N/2]);
return vshuf(lmap, rmap);
}
};
template <typename TYPE>
struct PACListTree<TYPE, 1>
{
typedef PolarHelper<TYPE> PH;
typedef typename PH::PATH PATH;
typedef typename PH::MAP MAP;
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, TYPE *hard, TYPE *soft, uint32_t frozen)
static MAP decode(PATH *metric, TYPE *message, MAP *maps, int *count, int *state, TYPE *hard, TYPE *soft, const int *rank, int frozen)
{
soft[1] = PH::prod(soft[2], soft[3]);
MAP lmap, rmap;
if (frozen & 1)
lmap = PACListLeaf<TYPE>::rate0(metric, state, hard, soft);
if (rank[0] < frozen)
lmap = rate0(metric, state, hard, soft);
else
lmap = PACListLeaf<TYPE>::rate1(metric, message, maps, count, state, hard, soft);
lmap = rate1(metric, message, maps, count, state, hard, soft);
soft[1] = PH::madd(hard[0], vshuf(soft[2], lmap), vshuf(soft[3], lmap));
if (frozen >> 1)
rmap = PACListLeaf<TYPE>::rate0(metric, state, hard+1, soft);
if (rank[1] < frozen)
rmap = rate0(metric, state, hard+1, soft);
else
rmap = PACListLeaf<TYPE>::rate1(metric, message, maps, count, state, hard+1, soft);
rmap = rate1(metric, message, maps, count, state, hard+1, soft);
hard[0] = PH::qmul(vshuf(hard[0], rmap), hard[1]);
return vshuf(lmap, rmap);
}
@ -246,7 +128,7 @@ class PACListDecoder
TYPE hard[MAX_N];
MAP maps[MAX_N];
public:
void operator()(int *rank, TYPE *message, const VALUE *codeword, const uint32_t *frozen, int level)
void operator()(int *rank, TYPE *message, const VALUE *codeword, const int *rank_map, int mesg_bits, int level)
{
assert(level <= MAX_M);
PATH metric[TYPE::SIZE];
@ -260,20 +142,21 @@ public:
int state[TYPE::SIZE];
for (int i = 0; i < TYPE::SIZE; ++i)
state[i] = 0;
int frozen = length - mesg_bits;
switch (level) {
case 5: PACListTree<TYPE, 5>::decode(metric, message, maps, &count, state, hard, soft, *frozen); break;
case 6: PACListTree<TYPE, 6>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 7: PACListTree<TYPE, 7>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 8: PACListTree<TYPE, 8>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 9: PACListTree<TYPE, 9>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 10: PACListTree<TYPE, 10>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 11: PACListTree<TYPE, 11>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 12: PACListTree<TYPE, 12>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 13: PACListTree<TYPE, 13>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 14: PACListTree<TYPE, 14>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 15: PACListTree<TYPE, 15>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 16: PACListTree<TYPE, 16>::decode(metric, message, maps, &count, state, hard, soft, frozen); break;
case 5: PACListTree<TYPE, 5>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 6: PACListTree<TYPE, 6>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 7: PACListTree<TYPE, 7>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 8: PACListTree<TYPE, 8>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 9: PACListTree<TYPE, 9>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 10: PACListTree<TYPE, 10>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 11: PACListTree<TYPE, 11>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 12: PACListTree<TYPE, 12>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 13: PACListTree<TYPE, 13>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 14: PACListTree<TYPE, 14>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 15: PACListTree<TYPE, 15>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
case 16: PACListTree<TYPE, 16>::decode(metric, message, maps, &count, state, hard, soft, rank_map, frozen); break;
default: assert(false);
}

View file

@ -46,15 +46,12 @@ int main()
typedef std::default_random_engine generator;
typedef std::uniform_int_distribution<int> distribution;
auto data = std::bind(distribution(0, 1), generator(rd()));
auto frozen = new uint32_t[N/32];
auto codeword = new code_type[N];
double erasure_probability = 0.5;
int K = (1 - erasure_probability) * N;
double design_SNR = 10 * std::log10(-std::log(erasure_probability));
std::cerr << "design SNR: " << design_SNR << std::endl;
for (int i = 0; i < N / 32; ++i)
frozen[i] = 0;
const int *reliability_sequence;
if (1) {
auto construct = new CODE::ReedMullerSequence<10>;
@ -66,12 +63,11 @@ int main()
} else {
reliability_sequence = sequence;
}
for (int i = 0, j = 0; i < 1024 && j < N - K; ++i) {
auto rank_map = new int[N];
for (int i = 0, j = 0; i < 1024 && j < N; ++i) {
int index = reliability_sequence[i];
if (index < N) {
frozen[index/32] |= 1 << (index%32);
++j;
}
if (index < N)
rank_map[index] = j++;
}
std::cerr << "Polar(" << N << ", " << K << ")" << std::endl;
auto message = new code_type[K];
@ -121,7 +117,7 @@ int main()
}
CODE::PACEncoder<code_type> encode;
encode(codeword, message, frozen, M);
encode(codeword, message, rank_map, K, M);
for (int i = 0; i < N; ++i)
orig[i] = codeword[i];
@ -144,7 +140,7 @@ int main()
int rank[L];
auto start = std::chrono::system_clock::now();
(*decode)(rank, decoded, codeword, frozen, M);
(*decode)(rank, decoded, codeword, rank_map, K, M);
auto end = std::chrono::system_clock::now();
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double mbs = (double)K / usec.count();