1 /* Reed-Solomon decoder
2 * Copyright 2002 Phil Karn, KA9Q
3 * May be used under the terms of the GNU General Public License (GPL)
13 #define NULL ((void *)0)
14 #define min(a,b) ((a) < (b) ? (a) : (b))
28 DTYPE *data, int *eras_pos, int no_eras){
31 struct rs *rs = (struct rs *)p;
33 int deg_lambda, el, deg_omega;
35 DTYPE u,q,tmp,num1,num2,den,discr_r;
36 DTYPE lambda[NROOTS+1], s[NROOTS]; /* Err+Eras Locator poly
37 * and syndrome poly */
38 DTYPE b[NROOTS+1], t[NROOTS+1], omega[NROOTS+1];
39 DTYPE root[NROOTS], reg[NROOTS+1], loc[NROOTS];
42 /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
47 for(i=0;i<NROOTS;i++){
51 s[i] = data[j] ^ ALPHA_TO[MODNN(INDEX_OF[s[i]] + (FCR+i)*PRIM)];
56 /* Convert syndromes to index form, checking for nonzero condition */
58 for(i=0;i<NROOTS;i++){
60 s[i] = INDEX_OF[s[i]];
64 /* if syndrome is zero, data[] is a codeword and there are no
65 * errors to correct. So return data[] unmodified
70 memset(&lambda[1],0,NROOTS*sizeof(lambda[0]));
74 /* Init lambda to be the erasure locator polynomial */
75 lambda[1] = ALPHA_TO[MODNN(PRIM*(NN-1-eras_pos[0]))];
76 for (i = 1; i < no_eras; i++) {
77 u = MODNN(PRIM*(NN-1-eras_pos[i]));
78 for (j = i+1; j > 0; j--) {
79 tmp = INDEX_OF[lambda[j - 1]];
81 lambda[j] ^= ALPHA_TO[MODNN(u + tmp)];
86 /* Test code that verifies the erasure locator polynomial just constructed
87 Needed only for decoder debugging. */
89 /* find roots of the erasure location polynomial */
90 for(i=1;i<=no_eras;i++)
91 reg[i] = INDEX_OF[lambda[i]];
94 for (i = 1,k=IPRIM-1; i <= NN; i++,k = MODNN(k+IPRIM)) {
96 for (j = 1; j <= no_eras; j++)
98 reg[j] = MODNN(reg[j] + j);
99 q ^= ALPHA_TO[reg[j]];
103 /* store root and error location number indices */
108 if (count != no_eras) {
109 printf("count = %d no_eras = %d\n lambda(x) is WRONG\n",count,no_eras);
114 printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
115 for (i = 0; i < count; i++)
116 printf("%d ", loc[i]);
121 for(i=0;i<NROOTS+1;i++)
122 b[i] = INDEX_OF[lambda[i]];
125 * Begin Berlekamp-Massey algorithm to determine error+erasure
130 while (++r <= NROOTS) { /* r is the step number */
131 /* Compute discrepancy at the r-th step in poly-form */
133 for (i = 0; i < r; i++){
134 if ((lambda[i] != 0) && (s[r-i-1] != A0)) {
135 discr_r ^= ALPHA_TO[MODNN(INDEX_OF[lambda[i]] + s[r-i-1])];
138 discr_r = INDEX_OF[discr_r]; /* Index form */
140 /* 2 lines below: B(x) <-- x*B(x) */
141 memmove(&b[1],b,NROOTS*sizeof(b[0]));
144 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
146 for (i = 0 ; i < NROOTS; i++) {
148 t[i+1] = lambda[i+1] ^ ALPHA_TO[MODNN(discr_r + b[i])];
150 t[i+1] = lambda[i+1];
152 if (2 * el <= r + no_eras - 1) {
153 el = r + no_eras - el;
155 * 2 lines below: B(x) <-- inv(discr_r) *
158 for (i = 0; i <= NROOTS; i++)
159 b[i] = (lambda[i] == 0) ? A0 : MODNN(INDEX_OF[lambda[i]] - discr_r + NN);
161 /* 2 lines below: B(x) <-- x*B(x) */
162 memmove(&b[1],b,NROOTS*sizeof(b[0]));
165 memcpy(lambda,t,(NROOTS+1)*sizeof(t[0]));
169 /* Convert lambda to index form and compute deg(lambda(x)) */
171 for(i=0;i<NROOTS+1;i++){
172 lambda[i] = INDEX_OF[lambda[i]];
176 /* Find roots of the error+erasure locator polynomial by Chien search */
177 memcpy(®[1],&lambda[1],NROOTS*sizeof(reg[0]));
178 count = 0; /* Number of roots of lambda(x) */
179 for (i = 1,k=IPRIM-1; i <= NN; i++,k = MODNN(k+IPRIM)) {
180 q = 1; /* lambda[0] is always 0 */
181 for (j = deg_lambda; j > 0; j--){
183 reg[j] = MODNN(reg[j] + j);
184 q ^= ALPHA_TO[reg[j]];
188 continue; /* Not a root */
189 /* store root (index-form) and error location number */
191 printf("count %d root %d loc %d\n",count,i,k);
195 /* If we've already found max possible roots,
196 * abort the search to save time
198 if(++count == deg_lambda)
201 if (deg_lambda != count) {
203 * deg(lambda) unequal to number of roots => uncorrectable
210 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
211 * x**NROOTS). in index form. Also find deg(omega).
214 for (i = 0; i < NROOTS;i++){
216 j = (deg_lambda < i) ? deg_lambda : i;
218 if ((s[i - j] != A0) && (lambda[j] != A0))
219 tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])];
223 omega[i] = INDEX_OF[tmp];
228 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
229 * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
231 for (j = count-1; j >=0; j--) {
233 for (i = deg_omega; i >= 0; i--) {
235 num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])];
237 num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)];
240 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
241 for (i = min(deg_lambda,NROOTS-1) & ~1; i >= 0; i -=2) {
242 if(lambda[i+1] != A0)
243 den ^= ALPHA_TO[MODNN(lambda[i+1] + i * root[j])];
247 printf("\n ERROR: denominator = 0\n");
252 /* Apply error to data */
254 data[loc[j]] ^= ALPHA_TO[MODNN(INDEX_OF[num1] + INDEX_OF[num2] + NN - INDEX_OF[den])];
258 if(eras_pos != NULL){
260 eras_pos[i] = loc[i];