1 /* Reed-Solomon decoder
2 * Copyright 2002 Phil Karn, KA9Q
3 * May be used under the terms of the GNU General Public License (GPL)
12 #define NULL ((void *)0)
13 #define min(a,b) ((a) < (b) ? (a) : (b))
27 DTYPE *data, int *eras_pos, int no_eras){
30 struct rs *rs = (struct rs *)p;
32 int deg_lambda, el, deg_omega;
34 DTYPE u,q,tmp,num1,num2,den,discr_r;
35 DTYPE lambda[NROOTS+1], s[NROOTS]; /* Err+Eras Locator poly
36 * and syndrome poly */
37 DTYPE b[NROOTS+1], t[NROOTS+1], omega[NROOTS+1];
38 DTYPE root[NROOTS], reg[NROOTS+1], loc[NROOTS];
41 /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
46 for(i=0;i<NROOTS;i++){
50 s[i] = data[j] ^ ALPHA_TO[MODNN(INDEX_OF[s[i]] + (FCR+i)*PRIM)];
55 /* Convert syndromes to index form, checking for nonzero condition */
57 for(i=0;i<NROOTS;i++){
59 s[i] = INDEX_OF[s[i]];
63 /* if syndrome is zero, data[] is a codeword and there are no
64 * errors to correct. So return data[] unmodified
69 memset(&lambda[1],0,NROOTS*sizeof(lambda[0]));
73 /* Init lambda to be the erasure locator polynomial */
74 lambda[1] = ALPHA_TO[MODNN(PRIM*(NN-1-eras_pos[0]))];
75 for (i = 1; i < no_eras; i++) {
76 u = MODNN(PRIM*(NN-1-eras_pos[i]));
77 for (j = i+1; j > 0; j--) {
78 tmp = INDEX_OF[lambda[j - 1]];
80 lambda[j] ^= ALPHA_TO[MODNN(u + tmp)];
85 /* Test code that verifies the erasure locator polynomial just constructed
86 Needed only for decoder debugging. */
88 /* find roots of the erasure location polynomial */
89 for(i=1;i<=no_eras;i++)
90 reg[i] = INDEX_OF[lambda[i]];
93 for (i = 1,k=IPRIM-1; i <= NN; i++,k = MODNN(k+IPRIM)) {
95 for (j = 1; j <= no_eras; j++)
97 reg[j] = MODNN(reg[j] + j);
98 q ^= ALPHA_TO[reg[j]];
102 /* store root and error location number indices */
107 if (count != no_eras) {
108 printf("count = %d no_eras = %d\n lambda(x) is WRONG\n",count,no_eras);
113 printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
114 for (i = 0; i < count; i++)
115 printf("%d ", loc[i]);
120 for(i=0;i<NROOTS+1;i++)
121 b[i] = INDEX_OF[lambda[i]];
124 * Begin Berlekamp-Massey algorithm to determine error+erasure
129 while (++r <= NROOTS) { /* r is the step number */
130 /* Compute discrepancy at the r-th step in poly-form */
132 for (i = 0; i < r; i++){
133 if ((lambda[i] != 0) && (s[r-i-1] != A0)) {
134 discr_r ^= ALPHA_TO[MODNN(INDEX_OF[lambda[i]] + s[r-i-1])];
137 discr_r = INDEX_OF[discr_r]; /* Index form */
139 /* 2 lines below: B(x) <-- x*B(x) */
140 memmove(&b[1],b,NROOTS*sizeof(b[0]));
143 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
145 for (i = 0 ; i < NROOTS; i++) {
147 t[i+1] = lambda[i+1] ^ ALPHA_TO[MODNN(discr_r + b[i])];
149 t[i+1] = lambda[i+1];
151 if (2 * el <= r + no_eras - 1) {
152 el = r + no_eras - el;
154 * 2 lines below: B(x) <-- inv(discr_r) *
157 for (i = 0; i <= NROOTS; i++)
158 b[i] = (lambda[i] == 0) ? A0 : MODNN(INDEX_OF[lambda[i]] - discr_r + NN);
160 /* 2 lines below: B(x) <-- x*B(x) */
161 memmove(&b[1],b,NROOTS*sizeof(b[0]));
164 memcpy(lambda,t,(NROOTS+1)*sizeof(t[0]));
168 /* Convert lambda to index form and compute deg(lambda(x)) */
170 for(i=0;i<NROOTS+1;i++){
171 lambda[i] = INDEX_OF[lambda[i]];
175 /* Find roots of the error+erasure locator polynomial by Chien search */
176 memcpy(®[1],&lambda[1],NROOTS*sizeof(reg[0]));
177 count = 0; /* Number of roots of lambda(x) */
178 for (i = 1,k=IPRIM-1; i <= NN; i++,k = MODNN(k+IPRIM)) {
179 q = 1; /* lambda[0] is always 0 */
180 for (j = deg_lambda; j > 0; j--){
182 reg[j] = MODNN(reg[j] + j);
183 q ^= ALPHA_TO[reg[j]];
187 continue; /* Not a root */
188 /* store root (index-form) and error location number */
190 printf("count %d root %d loc %d\n",count,i,k);
194 /* If we've already found max possible roots,
195 * abort the search to save time
197 if(++count == deg_lambda)
200 if (deg_lambda != count) {
202 * deg(lambda) unequal to number of roots => uncorrectable
209 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
210 * x**NROOTS). in index form. Also find deg(omega).
213 for (i = 0; i < NROOTS;i++){
215 j = (deg_lambda < i) ? deg_lambda : i;
217 if ((s[i - j] != A0) && (lambda[j] != A0))
218 tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])];
222 omega[i] = INDEX_OF[tmp];
227 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
228 * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
230 for (j = count-1; j >=0; j--) {
232 for (i = deg_omega; i >= 0; i--) {
234 num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])];
236 num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)];
239 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
240 for (i = min(deg_lambda,NROOTS-1) & ~1; i >= 0; i -=2) {
241 if(lambda[i+1] != A0)
242 den ^= ALPHA_TO[MODNN(lambda[i+1] + i * root[j])];
246 printf("\n ERROR: denominator = 0\n");
251 /* Apply error to data */
253 data[loc[j]] ^= ALPHA_TO[MODNN(INDEX_OF[num1] + INDEX_OF[num2] + NN - INDEX_OF[den])];
257 if(eras_pos != NULL){
259 eras_pos[i] = loc[i];