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