Reformat whole codebase with astyle.options (#128)
[openjpeg.git] / src / lib / openjpwl / rs.c
1 /*
2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
6 *
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #ifdef USE_JPWL
38
39 /**
40 @file rs.c
41 @brief Functions used to compute the Reed-Solomon parity and check of byte arrays
42
43 */
44
45 /**
46  * Reed-Solomon coding and decoding
47  * Phil Karn (karn@ka9q.ampr.org) September 1996
48  *
49  * This file is derived from the program "new_rs_erasures.c" by Robert
50  * Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy
51  * (harit@spectra.eng.hawaii.edu), Aug 1995
52  *
53  * I've made changes to improve performance, clean up the code and make it
54  * easier to follow. Data is now passed to the encoding and decoding functions
55  * through arguments rather than in global arrays. The decode function returns
56  * the number of corrected symbols, or -1 if the word is uncorrectable.
57  *
58  * This code supports a symbol size from 2 bits up to 16 bits,
59  * implying a block size of 3 2-bit symbols (6 bits) up to 65535
60  * 16-bit symbols (1,048,560 bits). The code parameters are set in rs.h.
61  *
62  * Note that if symbols larger than 8 bits are used, the type of each
63  * data array element switches from unsigned char to unsigned int. The
64  * caller must ensure that elements larger than the symbol range are
65  * not passed to the encoder or decoder.
66  *
67  */
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include "rs.h"
71
72 /* This defines the type used to store an element of the Galois Field
73  * used by the code. Make sure this is something larger than a char if
74  * if anything larger than GF(256) is used.
75  *
76  * Note: unsigned char will work up to GF(256) but int seems to run
77  * faster on the Pentium.
78  */
79 typedef int gf;
80
81 /* KK = number of information symbols */
82 static int  KK;
83
84 /* Primitive polynomials - see Lin & Costello, Appendix A,
85  * and  Lee & Messerschmitt, p. 453.
86  */
87 #if(MM == 2)/* Admittedly silly */
88 int Pp[MM + 1] = { 1, 1, 1 };
89
90 #elif(MM == 3)
91 /* 1 + x + x^3 */
92 int Pp[MM + 1] = { 1, 1, 0, 1 };
93
94 #elif(MM == 4)
95 /* 1 + x + x^4 */
96 int Pp[MM + 1] = { 1, 1, 0, 0, 1 };
97
98 #elif(MM == 5)
99 /* 1 + x^2 + x^5 */
100 int Pp[MM + 1] = { 1, 0, 1, 0, 0, 1 };
101
102 #elif(MM == 6)
103 /* 1 + x + x^6 */
104 int Pp[MM + 1] = { 1, 1, 0, 0, 0, 0, 1 };
105
106 #elif(MM == 7)
107 /* 1 + x^3 + x^7 */
108 int Pp[MM + 1] = { 1, 0, 0, 1, 0, 0, 0, 1 };
109
110 #elif(MM == 8)
111 /* 1+x^2+x^3+x^4+x^8 */
112 int Pp[MM + 1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1 };
113
114 #elif(MM == 9)
115 /* 1+x^4+x^9 */
116 int Pp[MM + 1] = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
117
118 #elif(MM == 10)
119 /* 1+x^3+x^10 */
120 int Pp[MM + 1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
121
122 #elif(MM == 11)
123 /* 1+x^2+x^11 */
124 int Pp[MM + 1] = { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
125
126 #elif(MM == 12)
127 /* 1+x+x^4+x^6+x^12 */
128 int Pp[MM + 1] = { 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 };
129
130 #elif(MM == 13)
131 /* 1+x+x^3+x^4+x^13 */
132 int Pp[MM + 1] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
133
134 #elif(MM == 14)
135 /* 1+x+x^6+x^10+x^14 */
136 int Pp[MM + 1] = { 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
137
138 #elif(MM == 15)
139 /* 1+x+x^15 */
140 int Pp[MM + 1] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
141
142 #elif(MM == 16)
143 /* 1+x+x^3+x^12+x^16 */
144 int Pp[MM + 1] = { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
145
146 #else
147 #error "MM must be in range 2-16"
148 #endif
149
150 /* Alpha exponent for the first root of the generator polynomial */
151 #define B0  0  /* Different from the default 1 */
152
153 /* index->polynomial form conversion table */
154 gf Alpha_to[NN + 1];
155
156 /* Polynomial->index form conversion table */
157 gf Index_of[NN + 1];
158
159 /* No legal value in index form represents zero, so
160  * we need a special value for this purpose
161  */
162 #define A0  (NN)
163
164 /* Generator polynomial g(x)
165  * Degree of g(x) = 2*TT
166  * has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1)
167  */
168 /*gf Gg[NN - KK + 1];*/
169 gf      Gg[NN - 1];
170
171 /* Compute x % NN, where NN is 2**MM - 1,
172  * without a slow divide
173  */
174 static /*inline*/ gf
175 modnn(int x)
176 {
177     while (x >= NN) {
178         x -= NN;
179         x = (x >> MM) + (x & NN);
180     }
181     return x;
182 }
183
184 /*#define   min(a,b)    ((a) < (b) ? (a) : (b))*/
185
186 #define CLEAR(a,n) {\
187     int ci;\
188     for(ci=(n)-1;ci >=0;ci--)\
189         (a)[ci] = 0;\
190     }
191
192 #define COPY(a,b,n) {\
193     int ci;\
194     for(ci=(n)-1;ci >=0;ci--)\
195         (a)[ci] = (b)[ci];\
196     }
197 #define COPYDOWN(a,b,n) {\
198     int ci;\
199     for(ci=(n)-1;ci >=0;ci--)\
200         (a)[ci] = (b)[ci];\
201     }
202
203 void init_rs(int k)
204 {
205     KK = k;
206     if (KK >= NN) {
207         printf("KK must be less than 2**MM - 1\n");
208         exit(1);
209     }
210
211     generate_gf();
212     gen_poly();
213 }
214
215 /* generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
216    lookup tables:  index->polynomial form   alpha_to[] contains j=alpha**i;
217                    polynomial form -> index form  index_of[j=alpha**i] = i
218    alpha=2 is the primitive element of GF(2**m)
219    HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
220         Let @ represent the primitive element commonly called "alpha" that
221    is the root of the primitive polynomial p(x). Then in GF(2^m), for any
222    0 <= i <= 2^m-2,
223         @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
224    where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
225    of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
226    example the polynomial representation of @^5 would be given by the binary
227    representation of the integer "alpha_to[5]".
228                    Similarly, index_of[] can be used as follows:
229         As above, let @ represent the primitive element of GF(2^m) that is
230    the root of the primitive polynomial p(x). In order to find the power
231    of @ (alpha) that has the polynomial representation
232         a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
233    we consider the integer "i" whose binary representation with a(0) being LSB
234    and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
235    "index_of[i]". Now, @^index_of[i] is that element whose polynomial
236     representation is (a(0),a(1),a(2),...,a(m-1)).
237    NOTE:
238         The element alpha_to[2^m-1] = 0 always signifying that the
239    representation of "@^infinity" = 0 is (0,0,0,...,0).
240         Similarly, the element index_of[0] = A0 always signifying
241    that the power of alpha which has the polynomial representation
242    (0,0,...,0) is "infinity".
243
244 */
245
246 void
247 generate_gf(void)
248 {
249     register int i, mask;
250
251     mask = 1;
252     Alpha_to[MM] = 0;
253     for (i = 0; i < MM; i++) {
254         Alpha_to[i] = mask;
255         Index_of[Alpha_to[i]] = i;
256         /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
257         if (Pp[i] != 0) {
258             Alpha_to[MM] ^= mask;    /* Bit-wise EXOR operation */
259         }
260         mask <<= 1; /* single left-shift */
261     }
262     Index_of[Alpha_to[MM]] = MM;
263     /*
264      * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
265      * poly-repr of @^i shifted left one-bit and accounting for any @^MM
266      * term that may occur when poly-repr of @^i is shifted.
267      */
268     mask >>= 1;
269     for (i = MM + 1; i < NN; i++) {
270         if (Alpha_to[i - 1] >= mask) {
271             Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
272         } else {
273             Alpha_to[i] = Alpha_to[i - 1] << 1;
274         }
275         Index_of[Alpha_to[i]] = i;
276     }
277     Index_of[0] = A0;
278     Alpha_to[NN] = 0;
279 }
280
281
282 /*
283  * Obtain the generator polynomial of the TT-error correcting, length
284  * NN=(2**MM -1) Reed Solomon code from the product of (X+@**(B0+i)), i = 0,
285  * ... ,(2*TT-1)
286  *
287  * Examples:
288  *
289  * If B0 = 1, TT = 1. deg(g(x)) = 2*TT = 2.
290  * g(x) = (x+@) (x+@**2)
291  *
292  * If B0 = 0, TT = 2. deg(g(x)) = 2*TT = 4.
293  * g(x) = (x+1) (x+@) (x+@**2) (x+@**3)
294  */
295 void
296 gen_poly(void)
297 {
298     register int i, j;
299
300     Gg[0] = Alpha_to[B0];
301     Gg[1] = 1;      /* g(x) = (X+@**B0) initially */
302     for (i = 2; i <= NN - KK; i++) {
303         Gg[i] = 1;
304         /*
305          * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
306          * (@**(B0+i-1) + x)
307          */
308         for (j = i - 1; j > 0; j--)
309             if (Gg[j] != 0) {
310                 Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + B0 + i - 1)];
311             } else {
312                 Gg[j] = Gg[j - 1];
313             }
314         /* Gg[0] can never be zero */
315         Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)];
316     }
317     /* convert Gg[] to index form for quicker encoding */
318     for (i = 0; i <= NN - KK; i++) {
319         Gg[i] = Index_of[Gg[i]];
320     }
321 }
322
323
324 /*
325  * take the string of symbols in data[i], i=0..(k-1) and encode
326  * systematically to produce NN-KK parity symbols in bb[0]..bb[NN-KK-1] data[]
327  * is input and bb[] is output in polynomial form. Encoding is done by using
328  * a feedback shift register with appropriate connections specified by the
329  * elements of Gg[], which was generated above. Codeword is   c(X) =
330  * data(X)*X**(NN-KK)+ b(X)
331  */
332 int
333 encode_rs(dtype *data, dtype *bb)
334 {
335     register int i, j;
336     gf feedback;
337
338     CLEAR(bb, NN - KK);
339     for (i = KK - 1; i >= 0; i--) {
340 #if (MM != 8)
341         if (data[i] > NN) {
342             return -1;    /* Illegal symbol */
343         }
344 #endif
345         feedback = Index_of[data[i] ^ bb[NN - KK - 1]];
346         if (feedback != A0) {   /* feedback term is non-zero */
347             for (j = NN - KK - 1; j > 0; j--)
348                 if (Gg[j] != A0) {
349                     bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)];
350                 } else {
351                     bb[j] = bb[j - 1];
352                 }
353             bb[0] = Alpha_to[modnn(Gg[0] + feedback)];
354         } else {
355             /* feedback term is zero. encoder becomes a
356                      * single-byte shifter */
357             for (j = NN - KK - 1; j > 0; j--) {
358                 bb[j] = bb[j - 1];
359             }
360             bb[0] = 0;
361         }
362     }
363     return 0;
364 }
365
366 /*
367  * Performs ERRORS+ERASURES decoding of RS codes. If decoding is successful,
368  * writes the codeword into data[] itself. Otherwise data[] is unaltered.
369  *
370  * Return number of symbols corrected, or -1 if codeword is illegal
371  * or uncorrectable.
372  *
373  * First "no_eras" erasures are declared by the calling program. Then, the
374  * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
375  * If the number of channel errors is not greater than "t_after_eras" the
376  * transmitted codeword will be recovered. Details of algorithm can be found
377  * in R. Blahut's "Theory ... of Error-Correcting Codes".
378  */
379 int
380 eras_dec_rs(dtype *data, int *eras_pos, int no_eras)
381 {
382     int deg_lambda, el, deg_omega;
383     int i, j, r;
384     gf u, q, tmp, num1, num2, den, discr_r;
385     gf recd[NN];
386     /* Err+Eras Locator poly and syndrome poly */
387     /*gf lambda[NN-KK + 1], s[NN-KK + 1];
388     gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
389     gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];*/
390     gf lambda[NN + 1], s[NN + 1];
391     gf b[NN + 1], t[NN + 1], omega[NN + 1];
392     gf root[NN], reg[NN + 1], loc[NN];
393     int syn_error, count;
394
395     /* data[] is in polynomial form, copy and convert to index form */
396     for (i = NN - 1; i >= 0; i--) {
397 #if (MM != 8)
398         if (data[i] > NN) {
399             return -1;    /* Illegal symbol */
400         }
401 #endif
402         recd[i] = Index_of[data[i]];
403     }
404     /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x)
405      * namely @**(B0+i), i = 0, ... ,(NN-KK-1)
406      */
407     syn_error = 0;
408     for (i = 1; i <= NN - KK; i++) {
409         tmp = 0;
410         for (j = 0; j < NN; j++)
411             if (recd[j] != A0) { /* recd[j] in index form */
412                 tmp ^= Alpha_to[modnn(recd[j] + (B0 + i - 1) * j)];
413             }
414         syn_error |= tmp;   /* set flag if non-zero syndrome =>
415                      * error */
416         /* store syndrome in index form  */
417         s[i] = Index_of[tmp];
418     }
419     if (!syn_error) {
420         /*
421          * if syndrome is zero, data[] is a codeword and there are no
422          * errors to correct. So return data[] unmodified
423          */
424         return 0;
425     }
426     CLEAR(&lambda[1], NN - KK);
427     lambda[0] = 1;
428     if (no_eras > 0) {
429         /* Init lambda to be the erasure locator polynomial */
430         lambda[1] = Alpha_to[eras_pos[0]];
431         for (i = 1; i < no_eras; i++) {
432             u = eras_pos[i];
433             for (j = i + 1; j > 0; j--) {
434                 tmp = Index_of[lambda[j - 1]];
435                 if (tmp != A0) {
436                     lambda[j] ^= Alpha_to[modnn(u + tmp)];
437                 }
438             }
439         }
440 #ifdef ERASURE_DEBUG
441         /* find roots of the erasure location polynomial */
442         for (i = 1; i <= no_eras; i++) {
443             reg[i] = Index_of[lambda[i]];
444         }
445         count = 0;
446         for (i = 1; i <= NN; i++) {
447             q = 1;
448             for (j = 1; j <= no_eras; j++)
449                 if (reg[j] != A0) {
450                     reg[j] = modnn(reg[j] + j);
451                     q ^= Alpha_to[reg[j]];
452                 }
453             if (!q) {
454                 /* store root and error location
455                  * number indices
456                  */
457                 root[count] = i;
458                 loc[count] = NN - i;
459                 count++;
460             }
461         }
462         if (count != no_eras) {
463             printf("\n lambda(x) is WRONG\n");
464             return -1;
465         }
466 #ifndef NO_PRINT
467         printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
468         for (i = 0; i < count; i++) {
469             printf("%d ", loc[i]);
470         }
471         printf("\n");
472 #endif
473 #endif
474     }
475     for (i = 0; i < NN - KK + 1; i++) {
476         b[i] = Index_of[lambda[i]];
477     }
478
479     /*
480      * Begin Berlekamp-Massey algorithm to determine error+erasure
481      * locator polynomial
482      */
483     r = no_eras;
484     el = no_eras;
485     while (++r <= NN - KK) { /* r is the step number */
486         /* Compute discrepancy at the r-th step in poly-form */
487         discr_r = 0;
488         for (i = 0; i < r; i++) {
489             if ((lambda[i] != 0) && (s[r - i] != A0)) {
490                 discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
491             }
492         }
493         discr_r = Index_of[discr_r];    /* Index form */
494         if (discr_r == A0) {
495             /* 2 lines below: B(x) <-- x*B(x) */
496             COPYDOWN(&b[1], b, NN - KK);
497             b[0] = A0;
498         } else {
499             /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
500             t[0] = lambda[0];
501             for (i = 0 ; i < NN - KK; i++) {
502                 if (b[i] != A0) {
503                     t[i + 1] = lambda[i + 1] ^ Alpha_to[modnn(discr_r + b[i])];
504                 } else {
505                     t[i + 1] = lambda[i + 1];
506                 }
507             }
508             if (2 * el <= r + no_eras - 1) {
509                 el = r + no_eras - el;
510                 /*
511                  * 2 lines below: B(x) <-- inv(discr_r) *
512                  * lambda(x)
513                  */
514                 for (i = 0; i <= NN - KK; i++) {
515                     b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
516                 }
517             } else {
518                 /* 2 lines below: B(x) <-- x*B(x) */
519                 COPYDOWN(&b[1], b, NN - KK);
520                 b[0] = A0;
521             }
522             COPY(lambda, t, NN - KK + 1);
523         }
524     }
525
526     /* Convert lambda to index form and compute deg(lambda(x)) */
527     deg_lambda = 0;
528     for (i = 0; i < NN - KK + 1; i++) {
529         lambda[i] = Index_of[lambda[i]];
530         if (lambda[i] != A0) {
531             deg_lambda = i;
532         }
533     }
534     /*
535      * Find roots of the error+erasure locator polynomial. By Chien
536      * Search
537      */
538     COPY(&reg[1], &lambda[1], NN - KK);
539     count = 0;      /* Number of roots of lambda(x) */
540     for (i = 1; i <= NN; i++) {
541         q = 1;
542         for (j = deg_lambda; j > 0; j--)
543             if (reg[j] != A0) {
544                 reg[j] = modnn(reg[j] + j);
545                 q ^= Alpha_to[reg[j]];
546             }
547         if (!q) {
548             /* store root (index-form) and error location number */
549             root[count] = i;
550             loc[count] = NN - i;
551             count++;
552         }
553     }
554
555 #ifdef DEBUG
556     printf("\n Final error positions:\t");
557     for (i = 0; i < count; i++) {
558         printf("%d ", loc[i]);
559     }
560     printf("\n");
561 #endif
562     if (deg_lambda != count) {
563         /*
564          * deg(lambda) unequal to number of roots => uncorrectable
565          * error detected
566          */
567         return -1;
568     }
569     /*
570      * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
571      * x**(NN-KK)). in index form. Also find deg(omega).
572      */
573     deg_omega = 0;
574     for (i = 0; i < NN - KK; i++) {
575         tmp = 0;
576         j = (deg_lambda < i) ? deg_lambda : i;
577         for (; j >= 0; j--) {
578             if ((s[i + 1 - j] != A0) && (lambda[j] != A0)) {
579                 tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
580             }
581         }
582         if (tmp != 0) {
583             deg_omega = i;
584         }
585         omega[i] = Index_of[tmp];
586     }
587     omega[NN - KK] = A0;
588
589     /*
590      * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
591      * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
592      */
593     for (j = count - 1; j >= 0; j--) {
594         num1 = 0;
595         for (i = deg_omega; i >= 0; i--) {
596             if (omega[i] != A0) {
597                 num1  ^= Alpha_to[modnn(omega[i] + i * root[j])];
598             }
599         }
600         num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
601         den = 0;
602
603         /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
604         for (i = min(deg_lambda, NN - KK - 1) & ~1; i >= 0; i -= 2) {
605             if (lambda[i + 1] != A0) {
606                 den ^= Alpha_to[modnn(lambda[i + 1] + i * root[j])];
607             }
608         }
609         if (den == 0) {
610 #ifdef DEBUG
611             printf("\n ERROR: denominator = 0\n");
612 #endif
613             return -1;
614         }
615         /* Apply error to data */
616         if (num1 != 0) {
617             data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN -
618                                            Index_of[den])];
619         }
620     }
621     return count;
622 }
623
624
625 #endif /* USE_JPWL */