1 /* Reed-Solomon decoder
2 * Copyright 2002 Phil Karn, KA9Q
3 * May be used under the terms of the GNU General Public License (GPL)
4 */
5
6 #ifdef DEBUG
7 #include <stdio.h>
8 #endif
9
10 #include <string.h>
11
12 #ifndef NULL
13 #define NULL ((void*)0)
14 #endif
15
16 #define min(a, b) ((a) < (b) ? (a) : (b))
17
18 #ifdef FIXED
19 #include "fixed.h"
20 #elif defined(BIGSYM)
21 #include "int.h"
22 #else
23 #include "char.h"
24 #endif
25
DECODE_RS(void * p,DTYPE * data,int * eras_pos,int no_eras)26 int DECODE_RS(
27 #ifndef FIXED
28 void* p,
29 #endif
30 DTYPE* data,
31 int* eras_pos,
32 int no_eras)
33 {
34
35 #ifndef FIXED
36 struct rs* rs = (struct rs*)p;
37 #endif
38 int deg_lambda, el, deg_omega;
39 int i, j, r, k;
40 #ifdef MAX_ARRAY
41 DTYPE u, q, tmp, num1, num2, den, discr_r;
42 DTYPE lambda[MAX_ARRAY], s[MAX_ARRAY]; /* Err+Eras Locator poly
43 * and syndrome poly */
44 DTYPE b[MAX_ARRAY], t[MAX_ARRAY], omega[MAX_ARRAY];
45 DTYPE root[MAX_ARRAY], reg[MAX_ARRAY], loc[MAX_ARRAY];
46 #else
47 DTYPE u, q, tmp, num1, num2, den, discr_r;
48 DTYPE lambda[NROOTS + 1], s[NROOTS]; /* Err+Eras Locator poly
49 * and syndrome poly */
50 DTYPE b[NROOTS + 1], t[NROOTS + 1], omega[NROOTS + 1];
51 DTYPE root[NROOTS], reg[NROOTS + 1], loc[NROOTS];
52 #endif
53 int syn_error, count;
54
55 /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
56 for (i = 0; (unsigned int)i < NROOTS; i++)
57 s[i] = data[0];
58
59 for (j = 1; (unsigned int)j < NN; j++) {
60 for (i = 0; (unsigned int)i < NROOTS; i++) {
61 if (s[i] == 0) {
62 s[i] = data[j];
63 } else {
64 s[i] = data[j] ^ ALPHA_TO[MODNN(INDEX_OF[s[i]] + (FCR + i) * PRIM)];
65 }
66 }
67 }
68
69 /* Convert syndromes to index form, checking for nonzero condition */
70 syn_error = 0;
71 for (i = 0; (unsigned int)i < NROOTS; i++) {
72 syn_error |= s[i];
73 s[i] = INDEX_OF[s[i]];
74 }
75
76 if (!syn_error) {
77 /* if syndrome is zero, data[] is a codeword and there are no
78 * errors to correct. So return data[] unmodified
79 */
80 count = 0;
81 goto finish;
82 }
83 memset(&lambda[1], 0, NROOTS * sizeof(lambda[0]));
84 lambda[0] = 1;
85
86 if (no_eras > 0) {
87 /* Init lambda to be the erasure locator polynomial */
88 lambda[1] = ALPHA_TO[MODNN(PRIM * (NN - 1 - eras_pos[0]))];
89 for (i = 1; i < no_eras; i++) {
90 u = MODNN(PRIM * (NN - 1 - eras_pos[i]));
91 for (j = i + 1; j > 0; j--) {
92 tmp = INDEX_OF[lambda[j - 1]];
93 if (tmp != A0)
94 lambda[j] ^= ALPHA_TO[MODNN(u + tmp)];
95 }
96 }
97
98 #if DEBUG >= 1
99 /* Test code that verifies the erasure locator polynomial just constructed
100 Needed only for decoder debugging. */
101
102 /* find roots of the erasure location polynomial */
103 for (i = 1; i <= no_eras; i++)
104 reg[i] = INDEX_OF[lambda[i]];
105
106 count = 0;
107 for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) {
108 q = 1;
109 for (j = 1; j <= no_eras; j++)
110 if (reg[j] != A0) {
111 reg[j] = MODNN(reg[j] + j);
112 q ^= ALPHA_TO[reg[j]];
113 }
114 if (q != 0)
115 continue;
116 /* store root and error location number indices */
117 root[count] = i;
118 loc[count] = k;
119 count++;
120 }
121 if (count != no_eras) {
122 printf("count = %d no_eras = %d\n lambda(x) is WRONG\n", count, no_eras);
123 count = -1;
124 goto finish;
125 }
126 #if DEBUG >= 2
127 printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
128 for (i = 0; i < count; i++)
129 printf("%d ", loc[i]);
130 printf("\n");
131 #endif
132 #endif
133 }
134 for (i = 0; (unsigned int)i < NROOTS + 1; i++)
135 b[i] = INDEX_OF[lambda[i]];
136
137 /*
138 * Begin Berlekamp-Massey algorithm to determine error+erasure
139 * locator polynomial
140 */
141 r = no_eras;
142 el = no_eras;
143 while ((unsigned int)(++r) <= NROOTS) { /* r is the step number */
144 /* Compute discrepancy at the r-th step in poly-form */
145 discr_r = 0;
146 for (i = 0; i < r; i++) {
147 if ((lambda[i] != 0) && (s[r - i - 1] != A0)) {
148 discr_r ^= ALPHA_TO[MODNN(INDEX_OF[lambda[i]] + s[r - i - 1])];
149 }
150 }
151 discr_r = INDEX_OF[discr_r]; /* Index form */
152 if (discr_r == A0) {
153 /* 2 lines below: B(x) <-- x*B(x) */
154 memmove(&b[1], b, NROOTS * sizeof(b[0]));
155 b[0] = A0;
156 } else {
157 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
158 t[0] = lambda[0];
159 for (i = 0; (unsigned int)i < NROOTS; i++) {
160 if (b[i] != A0)
161 t[i + 1] = lambda[i + 1] ^ ALPHA_TO[MODNN(discr_r + b[i])];
162 else
163 t[i + 1] = lambda[i + 1];
164 }
165 if (2 * el <= r + no_eras - 1) {
166 el = r + no_eras - el;
167 /*
168 * 2 lines below: B(x) <-- inv(discr_r) *
169 * lambda(x)
170 */
171 for (i = 0; (unsigned int)i <= NROOTS; i++)
172 b[i] =
173 (lambda[i] == 0) ? A0 : MODNN(INDEX_OF[lambda[i]] - discr_r + NN);
174 } else {
175 /* 2 lines below: B(x) <-- x*B(x) */
176 memmove(&b[1], b, NROOTS * sizeof(b[0]));
177 b[0] = A0;
178 }
179 memcpy(lambda, t, (NROOTS + 1) * sizeof(t[0]));
180 }
181 }
182
183 /* Convert lambda to index form and compute deg(lambda(x)) */
184 deg_lambda = 0;
185 for (i = 0; (unsigned int)i < NROOTS + 1; i++) {
186 lambda[i] = INDEX_OF[lambda[i]];
187 if (lambda[i] != A0)
188 deg_lambda = i;
189 }
190 /* Find roots of the error+erasure locator polynomial by Chien search */
191 memcpy(®[1], &lambda[1], NROOTS * sizeof(reg[0]));
192 count = 0; /* Number of roots of lambda(x) */
193 for (i = 1, k = IPRIM - 1; (unsigned int)i <= NN; i++, k = MODNN(k + IPRIM)) {
194 q = 1; /* lambda[0] is always 0 */
195 for (j = deg_lambda; j > 0; j--) {
196 if (reg[j] != A0) {
197 reg[j] = MODNN(reg[j] + j);
198 q ^= ALPHA_TO[reg[j]];
199 }
200 }
201 if (q != 0)
202 continue; /* Not a root */
203 /* store root (index-form) and error location number */
204 #if DEBUG >= 2
205 printf("count %d root %d loc %d\n", count, i, k);
206 #endif
207 root[count] = i;
208 loc[count] = k;
209 /* If we've already found max possible roots,
210 * abort the search to save time
211 */
212 if (++count == deg_lambda)
213 break;
214 }
215 if (deg_lambda != count) {
216 /*
217 * deg(lambda) unequal to number of roots => uncorrectable
218 * error detected
219 */
220 count = -1;
221 goto finish;
222 }
223 /*
224 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
225 * x**NROOTS). in index form. Also find deg(omega).
226 */
227 deg_omega = 0;
228 for (i = 0; (unsigned int)i < NROOTS; i++) {
229 tmp = 0;
230 j = (deg_lambda < i) ? deg_lambda : i;
231 for (; j >= 0; j--) {
232 if ((s[i - j] != A0) && (lambda[j] != A0))
233 tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])];
234 }
235 if (tmp != 0)
236 deg_omega = i;
237 omega[i] = INDEX_OF[tmp];
238 }
239 omega[NROOTS] = A0;
240
241 /*
242 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
243 * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
244 */
245 for (j = count - 1; j >= 0; j--) {
246 num1 = 0;
247 for (i = deg_omega; i >= 0; i--) {
248 if (omega[i] != A0)
249 num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])];
250 }
251 num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)];
252 den = 0;
253
254 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
255 for (i = (int)min((unsigned int)deg_lambda, NROOTS - 1) & ~1; i >= 0; i -= 2) {
256 if (lambda[i + 1] != A0)
257 den ^= ALPHA_TO[MODNN(lambda[i + 1] + i * root[j])];
258 }
259 if (den == 0) {
260 #if DEBUG >= 1
261 printf("\n ERROR: denominator = 0\n");
262 #endif
263 count = -1;
264 goto finish;
265 }
266 /* Apply error to data */
267 if (num1 != 0) {
268 data[loc[j]] ^=
269 ALPHA_TO[MODNN(INDEX_OF[num1] + INDEX_OF[num2] + NN - INDEX_OF[den])];
270 }
271 }
272 finish:
273 if (eras_pos != NULL) {
274 for (i = 0; i < count; i++)
275 eras_pos[i] = loc[i];
276 }
277 return count;
278 }
279