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