GNU Linux-libre 4.14.266-gnu1
[releases.git] / lib / reed_solomon / decode_rs.c
1 /*
2  * lib/reed_solomon/decode_rs.c
3  *
4  * Overview:
5  *   Generic Reed Solomon encoder / decoder library
6  *
7  * Copyright 2002, Phil Karn, KA9Q
8  * May be used under the terms of the GNU General Public License (GPL)
9  *
10  * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
11  *
12  * $Id: decode_rs.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
13  *
14  */
15
16 /* Generic data width independent code which is included by the
17  * wrappers.
18  */
19 {
20         int deg_lambda, el, deg_omega;
21         int i, j, r, k, pad;
22         int nn = rs->nn;
23         int nroots = rs->nroots;
24         int fcr = rs->fcr;
25         int prim = rs->prim;
26         int iprim = rs->iprim;
27         uint16_t *alpha_to = rs->alpha_to;
28         uint16_t *index_of = rs->index_of;
29         uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
30         /* Err+Eras Locator poly and syndrome poly The maximum value
31          * of nroots is 8. So the necessary stack size will be about
32          * 220 bytes max.
33          */
34         uint16_t lambda[nroots + 1], syn[nroots];
35         uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1];
36         uint16_t root[nroots], reg[nroots + 1], loc[nroots];
37         int count = 0;
38         uint16_t msk = (uint16_t) rs->nn;
39
40         /* Check length parameter for validity */
41         pad = nn - nroots - len;
42         BUG_ON(pad < 0 || pad >= nn);
43
44         /* Does the caller provide the syndrome ? */
45         if (s != NULL) {
46                 for (i = 0; i < nroots; i++) {
47                         /* The syndrome is in index form,
48                          * so nn represents zero
49                          */
50                         if (s[i] != nn)
51                                 goto decode;
52                 }
53
54                 /* syndrome is zero, no errors to correct  */
55                 return 0;
56         }
57
58         /* form the syndromes; i.e., evaluate data(x) at roots of
59          * g(x) */
60         for (i = 0; i < nroots; i++)
61                 syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk;
62
63         for (j = 1; j < len; j++) {
64                 for (i = 0; i < nroots; i++) {
65                         if (syn[i] == 0) {
66                                 syn[i] = (((uint16_t) data[j]) ^
67                                           invmsk) & msk;
68                         } else {
69                                 syn[i] = ((((uint16_t) data[j]) ^
70                                            invmsk) & msk) ^
71                                         alpha_to[rs_modnn(rs, index_of[syn[i]] +
72                                                        (fcr + i) * prim)];
73                         }
74                 }
75         }
76
77         for (j = 0; j < nroots; j++) {
78                 for (i = 0; i < nroots; i++) {
79                         if (syn[i] == 0) {
80                                 syn[i] = ((uint16_t) par[j]) & msk;
81                         } else {
82                                 syn[i] = (((uint16_t) par[j]) & msk) ^
83                                         alpha_to[rs_modnn(rs, index_of[syn[i]] +
84                                                        (fcr+i)*prim)];
85                         }
86                 }
87         }
88         s = syn;
89
90         /* Convert syndromes to index form, checking for nonzero condition */
91         syn_error = 0;
92         for (i = 0; i < nroots; i++) {
93                 syn_error |= s[i];
94                 s[i] = index_of[s[i]];
95         }
96
97         if (!syn_error) {
98                 /* if syndrome is zero, data[] is a codeword and there are no
99                  * errors to correct. So return data[] unmodified
100                  */
101                 count = 0;
102                 goto finish;
103         }
104
105  decode:
106         memset(&lambda[1], 0, nroots * sizeof(lambda[0]));
107         lambda[0] = 1;
108
109         if (no_eras > 0) {
110                 /* Init lambda to be the erasure locator polynomial */
111                 lambda[1] = alpha_to[rs_modnn(rs,
112                                         prim * (nn - 1 - (eras_pos[0] + pad)))];
113                 for (i = 1; i < no_eras; i++) {
114                         u = rs_modnn(rs, prim * (nn - 1 - (eras_pos[i] + pad)));
115                         for (j = i + 1; j > 0; j--) {
116                                 tmp = index_of[lambda[j - 1]];
117                                 if (tmp != nn) {
118                                         lambda[j] ^=
119                                                 alpha_to[rs_modnn(rs, u + tmp)];
120                                 }
121                         }
122                 }
123         }
124
125         for (i = 0; i < nroots + 1; i++)
126                 b[i] = index_of[lambda[i]];
127
128         /*
129          * Begin Berlekamp-Massey algorithm to determine error+erasure
130          * locator polynomial
131          */
132         r = no_eras;
133         el = no_eras;
134         while (++r <= nroots) { /* r is the step number */
135                 /* Compute discrepancy at the r-th step in poly-form */
136                 discr_r = 0;
137                 for (i = 0; i < r; i++) {
138                         if ((lambda[i] != 0) && (s[r - i - 1] != nn)) {
139                                 discr_r ^=
140                                         alpha_to[rs_modnn(rs,
141                                                           index_of[lambda[i]] +
142                                                           s[r - i - 1])];
143                         }
144                 }
145                 discr_r = index_of[discr_r];    /* Index form */
146                 if (discr_r == nn) {
147                         /* 2 lines below: B(x) <-- x*B(x) */
148                         memmove (&b[1], b, nroots * sizeof (b[0]));
149                         b[0] = nn;
150                 } else {
151                         /* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */
152                         t[0] = lambda[0];
153                         for (i = 0; i < nroots; i++) {
154                                 if (b[i] != nn) {
155                                         t[i + 1] = lambda[i + 1] ^
156                                                 alpha_to[rs_modnn(rs, discr_r +
157                                                                   b[i])];
158                                 } else
159                                         t[i + 1] = lambda[i + 1];
160                         }
161                         if (2 * el <= r + no_eras - 1) {
162                                 el = r + no_eras - el;
163                                 /*
164                                  * 2 lines below: B(x) <-- inv(discr_r) *
165                                  * lambda(x)
166                                  */
167                                 for (i = 0; i <= nroots; i++) {
168                                         b[i] = (lambda[i] == 0) ? nn :
169                                                 rs_modnn(rs, index_of[lambda[i]]
170                                                          - discr_r + nn);
171                                 }
172                         } else {
173                                 /* 2 lines below: B(x) <-- x*B(x) */
174                                 memmove(&b[1], b, nroots * sizeof(b[0]));
175                                 b[0] = nn;
176                         }
177                         memcpy(lambda, t, (nroots + 1) * sizeof(t[0]));
178                 }
179         }
180
181         /* Convert lambda to index form and compute deg(lambda(x)) */
182         deg_lambda = 0;
183         for (i = 0; i < nroots + 1; i++) {
184                 lambda[i] = index_of[lambda[i]];
185                 if (lambda[i] != nn)
186                         deg_lambda = i;
187         }
188         /* Find roots of error+erasure locator polynomial by Chien search */
189         memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
190         count = 0;              /* Number of roots of lambda(x) */
191         for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) {
192                 q = 1;          /* lambda[0] is always 0 */
193                 for (j = deg_lambda; j > 0; j--) {
194                         if (reg[j] != nn) {
195                                 reg[j] = rs_modnn(rs, reg[j] + j);
196                                 q ^= alpha_to[reg[j]];
197                         }
198                 }
199                 if (q != 0)
200                         continue;       /* Not a root */
201                 /* store root (index-form) and error location number */
202                 root[count] = i;
203                 loc[count] = k;
204                 /* If we've already found max possible roots,
205                  * abort the search to save time
206                  */
207                 if (++count == deg_lambda)
208                         break;
209         }
210         if (deg_lambda != count) {
211                 /*
212                  * deg(lambda) unequal to number of roots => uncorrectable
213                  * error detected
214                  */
215                 count = -EBADMSG;
216                 goto finish;
217         }
218         /*
219          * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
220          * x**nroots). in index form. Also find deg(omega).
221          */
222         deg_omega = deg_lambda - 1;
223         for (i = 0; i <= deg_omega; i++) {
224                 tmp = 0;
225                 for (j = i; j >= 0; j--) {
226                         if ((s[i - j] != nn) && (lambda[j] != nn))
227                                 tmp ^=
228                                     alpha_to[rs_modnn(rs, s[i - j] + lambda[j])];
229                 }
230                 omega[i] = index_of[tmp];
231         }
232
233         /*
234          * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
235          * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
236          */
237         for (j = count - 1; j >= 0; j--) {
238                 num1 = 0;
239                 for (i = deg_omega; i >= 0; i--) {
240                         if (omega[i] != nn)
241                                 num1 ^= alpha_to[rs_modnn(rs, omega[i] +
242                                                         i * root[j])];
243                 }
244                 num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
245                 den = 0;
246
247                 /* lambda[i+1] for i even is the formal derivative
248                  * lambda_pr of lambda[i] */
249                 for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) {
250                         if (lambda[i + 1] != nn) {
251                                 den ^= alpha_to[rs_modnn(rs, lambda[i + 1] +
252                                                        i * root[j])];
253                         }
254                 }
255                 /* Apply error to data */
256                 if (num1 != 0 && loc[j] >= pad) {
257                         uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
258                                                        index_of[num2] +
259                                                        nn - index_of[den])];
260                         /* Store the error correction pattern, if a
261                          * correction buffer is available */
262                         if (corr) {
263                                 corr[j] = cor;
264                         } else {
265                                 /* If a data buffer is given and the
266                                  * error is inside the message,
267                                  * correct it */
268                                 if (data && (loc[j] < (nn - nroots)))
269                                         data[loc[j] - pad] ^= cor;
270                         }
271                 }
272         }
273
274 finish:
275         if (eras_pos != NULL) {
276                 for (i = 0; i < count; i++)
277                         eras_pos[i] = loc[i] - pad;
278         }
279         return count;
280
281 }