summaryrefslogtreecommitdiff
path: root/controller/fw/src/ldpc_decoder.c
blob: fe59d77881f16a38621f23a6d0105ec5e8e913c3 (plain)
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
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>


void gausselimination(size_t n, size_t k, int8_t *A, int8_t *b);

void inner_logbp(
        size_t m, size_t n,
        size_t bits_count, size_t nodes_count, const uint32_t bits_values[], const uint32_t nodes_values[],
        int8_t Lc[],
        float Lq[], float Lr[],
        unsigned int n_iter,
        float L_posteriori_out[]);

//decode(384, 6, 8, ...)
int decode(size_t n, size_t nodes_count, size_t bits_count, uint32_t bits[], int8_t y[], int8_t out[], unsigned int maxiter) {
    const size_t m = n * nodes_count / bits_count;
    float Lq[m*n];
    float Lr[m*n];
    float L_posteriori[n];

    /* Calculate column bit positions from row bit positions */
    int32_t bits_transposed[nodes_count * n];
    for (size_t i=0; i<nodes_count * n; i++)
        bits_transposed[i] = -1;

    for (size_t i=0; i<m; i++) {
        for (size_t j=0; j<bits_count; j++) {
            int32_t *base = bits_transposed + bits[i*bits_count + j] * nodes_count;
            for (; *base != -1; base++)
                ;
            *base = i;
        }
    }

    /*
    printf("Row positions: [");
    for (size_t i=0; i<m*bits_count; i++) {
        if (i)
            printf(", ");
        if (i%32 == 0)
            printf("\n    ");
        printf("%4d", bits[i]);
    }
    printf("\n]\n");

    printf("Column positions: [");
    for (size_t i=0; i<n*nodes_count; i++) {
        if (i)
            printf(", ");
        if (i%32 == 0)
            printf("\n    ");
        printf("%4d", bits_transposed[i]);
    }
    printf("\n]\n");
    */

    /* Run iterative optimization algorithm */
    for (unsigned int n_iter=0; n_iter<maxiter; n_iter++) {
        inner_logbp(m, n, bits_count, nodes_count, bits, (uint32_t*)bits_transposed, y, Lq, Lr, n_iter, L_posteriori);

        /*
        float *arrs[3] = {Lq, Lr, L_posteriori};
        const char *names[3] = {"Lq", "Lr", "L_posteriori"};
        size_t lens[3] = {m*n, m*n, n};
        const size_t head_tail = 10;
        for (int j=0; j<3; j++) {
            printf("%s=[", names[j]);
            bool ellipsis = false;
            const int w = 16;
            for (size_t i=0; i<lens[j]; i++) {
                if (lens[j] > 1000 && i/w > head_tail && i/w < m*n/w-head_tail) {
                    if (!ellipsis) {
                        ellipsis = true;
                        printf("\n    ...");
                    }
                    continue;
                }
                if (i)
                    printf(", ");
                if (i%w == 0)
                    printf("\n    ");
                float outf = arrs[j][i];
                char *s = outf < 0 ? "\033[91m" : (outf > 0 ? "\033[92m" : "\033[94m");
                printf("%s% 012.6g\033[38;5;240m", s, outf);
            }
            printf("\n]\n");
        }
        */

        for (size_t i=0; i<n; i++)
            out[i] = L_posteriori[i] <= 0.0f;

        for (size_t i=0; i<m; i++) {
            bool sum = 0;
            for (size_t j=0; j<bits_count; j++)
                sum ^= out[bits[i*bits_count + j]];
            if (sum)
                continue;
        }

        fflush(stdout);
        return n_iter;
    }

    fflush(stdout);
    return -1;
}

/* Perform inner ext LogBP solver */
void inner_logbp(
        size_t m, size_t n,
        size_t bits_count, size_t nodes_count, uint32_t const bits_values[], const uint32_t nodes_values[],
        int8_t Lc[],
        float Lq[], float Lr[],
        unsigned int n_iter,
        float L_posteriori_out[]) {

    /*
    printf("Input data: [");
    for (size_t i=0; i<n; i++) {
        if (i)
            printf(", ");
        if (i%32 == 0)
            printf("\n    ");
        printf("%4d", Lc[i]);
    }
    printf("\n]\n");
    */

    /* step 1 : Horizontal */
    unsigned int bits_counter = 0;
    for (size_t i=0; i<m; i++) {
        //printf("=== i=%zu\n", i);
        for (size_t p=bits_counter; p<bits_counter+bits_count; p++) {
            size_t j = bits_values[p];
            //printf("\033[38;5;240mj=%04zd ", j);

            float x = 1;
            if (n_iter == 0) {
                for (size_t q=bits_counter; q<bits_counter+bits_count; q++) {
                    if (bits_values[q] != j) {
                        //int lcv = Lc[bits_values[q]];
                        //char *s = lcv < 0 ? "\033[91m" : (lcv > 0 ? "\033[92m" : "\033[94m");
                        //printf("nij=%04u Lc=%s%3d\033[38;5;240m ", bits_values[q], s, lcv);
                        x *= tanhf(0.5f * Lc[bits_values[q]]);
                    }
                }

            } else {
                for (size_t q=bits_counter; q<bits_counter+bits_count; q++) {
                    if (bits_values[q] != j)
                        x *= tanhf(0.5f * Lq[i*n + bits_values[q]]);
                }
            }

            //printf("\n==== i=%03zd p=%01zd x=%08f\n", i, p-bits_counter, x);

            float num = 1 + x;
            float denom = 1 - x;
            if (num == 0)
                Lr[i*n + j] = -1.0f;
            else if (denom == 0)
                Lr[i*n + j] = 1.0f;
            else
                Lr[i*n + j] = logf(num/denom);
        }

        bits_counter += bits_count;
    }

    /* step 2 : Vertical */
    unsigned int nodes_counter = 0;
    for (size_t j=0; j<n; j++) {
        for (size_t p=bits_counter; p<nodes_counter+nodes_count; p++) {
            size_t i = nodes_values[p];

            Lq[i*n + j] = Lc[j];

            for (size_t q=bits_counter; q<nodes_counter+nodes_count; q++) {
                if (nodes_values[q] != i)
                    Lq[i*n + j] += Lr[nodes_values[q]*n + j];
            }
        }

        nodes_counter += nodes_count;
    }

    /* LLR a posteriori */
    nodes_counter = 0;
    for (size_t j=0; j<n; j++) {
        float sum = 0;
        for (size_t k=bits_counter; k<nodes_counter+nodes_count; k++)
            sum += Lr[nodes_values[k]*n + j];
        nodes_counter += nodes_count;

        L_posteriori_out[j] = Lc[j] + sum;
    }
}

/* Compute the original (k) bit message from a (n) bit codeword x.
 *
 * tG: (n, k)-matrix
 * x: (n)-vector
 * out: (k)-vector
 */
void get_message(size_t n, size_t k, int8_t *tG, int8_t *x, int8_t *out) {

    gausselimination(n, k, tG, x);

    out[k - 1] = x[k - 1];
    for (ssize_t i=k-2; i>=0; i--) {
        out[i] = x[i];

        uint8_t sum = 0;
        for (size_t j=i+1; j<k; j++)
            sum ^= tG[i*k + j] * out[j];

        out[i] = !!(out[i] - sum);
    }
}

/* Solve linear system in Z/2Z via Gauss Gauss elimination.
 *
 * A: (n, k)-matrix
 * b: (n)-vector
 */
void gausselimination(size_t n, size_t k, int8_t *A, int8_t *b) {
    ssize_t d = k<n ? k : n;
    for (ssize_t j=0; j<d; j++) {

        ssize_t pivot = -1;
        for (size_t i=j; i<n; i++) {
            if (A[i*k + j]) {
                pivot = i;
                break;
            }
        }
        if (pivot == -1)
            continue;

        if (pivot != j) {
            for (size_t i=0; i<k; i++) {
                int8_t tmp = A[j*k + i];
                A[j*k + i] = A[pivot*k + i];
                A[pivot*k + i] = tmp;
            }

            int8_t tmp = b[j];
            b[j] = b[pivot];
            b[pivot] = tmp;
        }

        for (size_t i=j+1; i<n; i++) {
            if (A[i*k + j]) {
                for (size_t p=0; p<k; p++)
                    A[i*k + p] = !!(A[i*k + p] - A[j*k + p]);
                b[i] = !!(b[i] - b[j]);
            }
        }
    }
}