GNU Radio's SATELLITES Package
lib/libfec/decode_rs.h
Go to the documentation of this file.
1 /* The guts of the Reed-Solomon decoder, meant to be #included
2  * into a function body with the following typedefs, macros and variables supplied
3  * according to the code parameters:
4 
5  * data_t - a typedef for the data symbol
6  * data_t data[] - array of NN data and parity symbols to be corrected in place
7  * retval - an integer lvalue into which the decoder's return code is written
8  * NROOTS - the number of roots in the RS code generator polynomial,
9  * which is the same as the number of parity symbols in a block.
10  Integer variable or literal.
11  * NN - the total number of symbols in a RS block. Integer variable or literal.
12  * PAD - the number of pad symbols in a block. Integer variable or literal.
13  * ALPHA_TO - The address of an array of NN elements to convert Galois field
14  * elements in index (log) form to polynomial form. Read only.
15  * INDEX_OF - The address of an array of NN elements to convert Galois field
16  * elements in polynomial form to index (log) form. Read only.
17  * MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
18  * FCR - An integer literal or variable specifying the first consecutive root of the
19  * Reed-Solomon generator polynomial. Integer variable or literal.
20  * PRIM - The primitive root of the generator poly. Integer variable or literal.
21  * DEBUG - If set to 1 or more, do various internal consistency checking. Leave this
22  * undefined for production code
23 
24  * The memset(), memmove(), and memcpy() functions are used. The appropriate header
25  * file declaring these functions (usually <string.h>) must be included by the calling
26  * program.
27  */
28 
29 
30 #if !defined(NROOTS)
31 #error "NROOTS not defined"
32 #endif
33 
34 #if !defined(NN)
35 #error "NN not defined"
36 #endif
37 
38 #if !defined(PAD)
39 #error "PAD not defined"
40 #endif
41 
42 #if !defined(ALPHA_TO)
43 #error "ALPHA_TO not defined"
44 #endif
45 
46 #if !defined(INDEX_OF)
47 #error "INDEX_OF not defined"
48 #endif
49 
50 #if !defined(MODNN)
51 #error "MODNN not defined"
52 #endif
53 
54 #if !defined(FCR)
55 #error "FCR not defined"
56 #endif
57 
58 #if !defined(PRIM)
59 #error "PRIM not defined"
60 #endif
61 
62 #if !defined(NULL)
63 #define NULL ((void*)0)
64 #endif
65 
66 #undef MIN
67 #define MIN(a, b) ((a) < (b) ? (a) : (b))
68 #undef A0
69 #define A0 (NN)
70 
71 {
72  int deg_lambda, el, deg_omega;
73  int i, j, r, k;
75 #ifdef MAX_ARRAY
76  data_t lambda[MAX_ARRAY], s[MAX_ARRAY]; /* Err+Eras Locator poly
77  * and syndrome poly */
78  data_t b[MAX_ARRAY], t[MAX_ARRAY], omega[MAX_ARRAY];
79  data_t root[MAX_ARRAY], reg[MAX_ARRAY], loc[MAX_ARRAY];
80 #else /* MAX_ARRAY */
81  data_t lambda[NROOTS + 1], s[NROOTS]; /* Err+Eras Locator poly
82  * and syndrome poly */
83  data_t b[NROOTS + 1], t[NROOTS + 1], omega[NROOTS + 1];
85 #endif /* MAX_ARRAY */
87 
88  /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
89  for (i = 0; i < NROOTS; i++)
90  s[i] = data[0];
91 
92  for (j = 1; j < NN - PAD; j++) {
93  for (i = 0; i < NROOTS; i++) {
94  if (s[i] == 0) {
95  s[i] = data[j];
96  } else {
97  s[i] = data[j] ^ ALPHA_TO[MODNN(INDEX_OF[s[i]] + (FCR + i) * PRIM)];
98  }
99  }
100  }
101 
102  /* Convert syndromes to index form, checking for nonzero condition */
103  syn_error = 0;
104  for (i = 0; i < NROOTS; i++) {
105  syn_error |= s[i];
106  s[i] = INDEX_OF[s[i]];
107  }
108 
109  if (!syn_error) {
110  /* if syndrome is zero, data[] is a codeword and there are no
111  * errors to correct. So return data[] unmodified
112  */
113  count = 0;
114  goto finish;
115  }
116  memset(&lambda[1], 0, NROOTS * sizeof(lambda[0]));
117  lambda[0] = 1;
118 
119  if (no_eras > 0) {
120  /* Init lambda to be the erasure locator polynomial */
121  lambda[1] = ALPHA_TO[MODNN(PRIM * (NN - 1 - eras_pos[0]))];
122  for (i = 1; i < no_eras; i++) {
123  u = MODNN(PRIM * (NN - 1 - eras_pos[i]));
124  for (j = i + 1; j > 0; j--) {
125  tmp = INDEX_OF[lambda[j - 1]];
126  if (tmp != A0)
127  lambda[j] ^= ALPHA_TO[MODNN(u + tmp)];
128  }
129  }
130 
131 #if DEBUG >= 1
132  /* Test code that verifies the erasure locator polynomial just constructed
133  Needed only for decoder debugging. */
134 
135  /* find roots of the erasure location polynomial */
136  for (i = 1; i <= no_eras; i++)
137  reg[i] = INDEX_OF[lambda[i]];
138 
139  count = 0;
140  for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) {
141  q = 1;
142  for (j = 1; j <= no_eras; j++)
143  if (reg[j] != A0) {
144  reg[j] = MODNN(reg[j] + j);
145  q ^= ALPHA_TO[reg[j]];
146  }
147  if (q != 0)
148  continue;
149  /* store root and error location number indices */
150  root[count] = i;
151  loc[count] = k;
152  count++;
153  }
154  if (count != no_eras) {
155  printf("count = %d no_eras = %d\n lambda(x) is WRONG\n", count, no_eras);
156  count = -1;
157  goto finish;
158  }
159 #if DEBUG >= 2
160  printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
161  for (i = 0; i < count; i++)
162  printf("%d ", loc[i]);
163  printf("\n");
164 #endif
165 #endif
166  }
167  for (i = 0; i < NROOTS + 1; i++)
168  b[i] = INDEX_OF[lambda[i]];
169 
170  /*
171  * Begin Berlekamp-Massey algorithm to determine error+erasure
172  * locator polynomial
173  */
174  r = no_eras;
175  el = no_eras;
176  while (++r <= NROOTS) { /* r is the step number */
177  /* Compute discrepancy at the r-th step in poly-form */
178  discr_r = 0;
179  for (i = 0; i < r; i++) {
180  if ((lambda[i] != 0) && (s[r - i - 1] != A0)) {
181  discr_r ^= ALPHA_TO[MODNN(INDEX_OF[lambda[i]] + s[r - i - 1])];
182  }
183  }
184  discr_r = INDEX_OF[discr_r]; /* Index form */
185  if (discr_r == A0) {
186  /* 2 lines below: B(x) <-- x*B(x) */
187  memmove(&b[1], b, NROOTS * sizeof(b[0]));
188  b[0] = A0;
189  } else {
190  /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
191  t[0] = lambda[0];
192  for (i = 0; i < NROOTS; i++) {
193  if (b[i] != A0)
194  t[i + 1] = lambda[i + 1] ^ ALPHA_TO[MODNN(discr_r + b[i])];
195  else
196  t[i + 1] = lambda[i + 1];
197  }
198  if (2 * el <= r + no_eras - 1) {
199  el = r + no_eras - el;
200  /*
201  * 2 lines below: B(x) <-- inv(discr_r) *
202  * lambda(x)
203  */
204  for (i = 0; i <= NROOTS; i++)
205  b[i] =
206  (lambda[i] == 0) ? A0 : MODNN(INDEX_OF[lambda[i]] - discr_r + NN);
207  } else {
208  /* 2 lines below: B(x) <-- x*B(x) */
209  memmove(&b[1], b, NROOTS * sizeof(b[0]));
210  b[0] = A0;
211  }
212  memcpy(lambda, t, (NROOTS + 1) * sizeof(t[0]));
213  }
214  }
215 
216  /* Convert lambda to index form and compute deg(lambda(x)) */
218  for (i = 0; i < NROOTS + 1; i++) {
219  lambda[i] = INDEX_OF[lambda[i]];
220  if (lambda[i] != A0)
221  deg_lambda = i;
222  }
223  /* Find roots of the error+erasure locator polynomial by Chien search */
224  memcpy(&reg[1], &lambda[1], NROOTS * sizeof(reg[0]));
225  count = 0; /* Number of roots of lambda(x) */
226  for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) {
227  q = 1; /* lambda[0] is always 0 */
228  for (j = deg_lambda; j > 0; j--) {
229  if (reg[j] != A0) {
230  reg[j] = MODNN(reg[j] + j);
231  q ^= ALPHA_TO[reg[j]];
232  }
233  }
234  if (q != 0)
235  continue; /* Not a root */
236  /* store root (index-form) and error location number */
237 #if DEBUG >= 2
238  printf("count %d root %d loc %d\n", count, i, k);
239 #endif
240  root[count] = i;
241  loc[count] = k;
242  /* If we've already found max possible roots,
243  * abort the search to save time
244  */
245  if (++count == deg_lambda)
246  break;
247  }
248  if (deg_lambda != count) {
249  /*
250  * deg(lambda) unequal to number of roots => uncorrectable
251  * error detected
252  */
253  count = -1;
254  goto finish;
255  }
256  /*
257  * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
258  * x**NROOTS). in index form. Also find deg(omega).
259  */
261  for (i = 0; i <= deg_omega; i++) {
262  tmp = 0;
263  for (j = i; j >= 0; j--) {
264  if ((s[i - j] != A0) && (lambda[j] != A0))
265  tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])];
266  }
267  omega[i] = INDEX_OF[tmp];
268  }
269 
270  /*
271  * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
272  * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
273  */
274  for (j = count - 1; j >= 0; j--) {
275  num1 = 0;
276  for (i = deg_omega; i >= 0; i--) {
277  if (omega[i] != A0)
278  num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])];
279  }
280  num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)];
281  den = 0;
282 
283  /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
284  for (i = MIN(deg_lambda, NROOTS - 1) & ~1; i >= 0; i -= 2) {
285  if (lambda[i + 1] != A0)
286  den ^= ALPHA_TO[MODNN(lambda[i + 1] + i * root[j])];
287  }
288 #if DEBUG >= 1
289  if (den == 0) {
290  printf("\n ERROR: denominator = 0\n");
291  count = -1;
292  goto finish;
293  }
294 #endif
295  /* Apply error to data */
296  if (num1 != 0 && loc[j] >= PAD) {
297  data[loc[j] - PAD] ^=
299  }
300  }
301 finish:
302  if (eras_pos != NULL) {
303  for (i = 0; i < count; i++)
304  eras_pos[i] = loc[i];
305  }
306  retval = count;
307 }
#define NN
Definition: ccsds.h:3
#define NROOTS
Definition: ccsds.h:4
unsigned char data_t
Definition: ccsds.h:1
#define FCR
Definition: char.h:16
#define PAD
Definition: char.h:19
#define MODNN(x)
Definition: char.h:8
#define INDEX_OF
Definition: char.h:13
#define PRIM
Definition: char.h:17
#define IPRIM
Definition: char.h:18
#define ALPHA_TO
Definition: char.h:12
data_t q
Definition: lib/libfec/decode_rs.h:74
#define NULL
Definition: lib/libfec/decode_rs.h:63
data_t num2
Definition: lib/libfec/decode_rs.h:74
#define A0
Definition: lib/libfec/decode_rs.h:69
data_t lambda[NROOTS+1]
Definition: lib/libfec/decode_rs.h:81
data_t num1
Definition: lib/libfec/decode_rs.h:74
data_t reg[NROOTS+1]
Definition: lib/libfec/decode_rs.h:84
data_t loc[NROOTS]
Definition: lib/libfec/decode_rs.h:84
int j
Definition: lib/libfec/decode_rs.h:73
#define MIN(a, b)
Definition: lib/libfec/decode_rs.h:67
int syn_error
Definition: lib/libfec/decode_rs.h:86
int r
Definition: lib/libfec/decode_rs.h:73
data_t den
Definition: lib/libfec/decode_rs.h:74
deg_omega
Definition: lib/libfec/decode_rs.h:260
data_t discr_r
Definition: lib/libfec/decode_rs.h:74
el
Definition: lib/libfec/decode_rs.h:175
data_t s[NROOTS]
Definition: lib/libfec/decode_rs.h:81
data_t u
Definition: lib/libfec/decode_rs.h:74
data_t tmp
Definition: lib/libfec/decode_rs.h:74
data_t t[NROOTS+1]
Definition: lib/libfec/decode_rs.h:83
int k
Definition: lib/libfec/decode_rs.h:73
int i
Definition: lib/libfec/decode_rs.h:71
data_t omega[NROOTS+1]
Definition: lib/libfec/decode_rs.h:83
data_t root[NROOTS]
Definition: lib/libfec/decode_rs.h:84
int count
Definition: lib/libfec/decode_rs.h:86
deg_lambda
Definition: lib/libfec/decode_rs.h:217
data_t b[NROOTS+1]
Definition: lib/libfec/decode_rs.h:83
memset(parity, 0, NROOTS *sizeof(data_t))