[2.0] Backport all changes since r2798 (included) from trunk
[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                    Similarily, 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         Similarily, 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                 mask <<= 1;     /* single left-shift */
260         }
261         Index_of[Alpha_to[MM]] = MM;
262         /*
263          * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
264          * poly-repr of @^i shifted left one-bit and accounting for any @^MM
265          * term that may occur when poly-repr of @^i is shifted.
266          */
267         mask >>= 1;
268         for (i = MM + 1; i < NN; i++) {
269                 if (Alpha_to[i - 1] >= mask)
270                         Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
271                 else
272                         Alpha_to[i] = Alpha_to[i - 1] << 1;
273                 Index_of[Alpha_to[i]] = i;
274         }
275         Index_of[0] = A0;
276         Alpha_to[NN] = 0;
277 }
278
279
280 /*
281  * Obtain the generator polynomial of the TT-error correcting, length
282  * NN=(2**MM -1) Reed Solomon code from the product of (X+@**(B0+i)), i = 0,
283  * ... ,(2*TT-1)
284  *
285  * Examples:
286  *
287  * If B0 = 1, TT = 1. deg(g(x)) = 2*TT = 2.
288  * g(x) = (x+@) (x+@**2)
289  *
290  * If B0 = 0, TT = 2. deg(g(x)) = 2*TT = 4.
291  * g(x) = (x+1) (x+@) (x+@**2) (x+@**3)
292  */
293 void
294 gen_poly(void)
295 {
296         register int i, j;
297
298         Gg[0] = Alpha_to[B0];
299         Gg[1] = 1;              /* g(x) = (X+@**B0) initially */
300         for (i = 2; i <= NN - KK; i++) {
301                 Gg[i] = 1;
302                 /*
303                  * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
304                  * (@**(B0+i-1) + x)
305                  */
306                 for (j = i - 1; j > 0; j--)
307                         if (Gg[j] != 0)
308                                 Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + B0 + i - 1)];
309                         else
310                                 Gg[j] = Gg[j - 1];
311                 /* Gg[0] can never be zero */
312                 Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)];
313         }
314         /* convert Gg[] to index form for quicker encoding */
315         for (i = 0; i <= NN - KK; i++)
316                 Gg[i] = Index_of[Gg[i]];
317 }
318
319
320 /*
321  * take the string of symbols in data[i], i=0..(k-1) and encode
322  * systematically to produce NN-KK parity symbols in bb[0]..bb[NN-KK-1] data[]
323  * is input and bb[] is output in polynomial form. Encoding is done by using
324  * a feedback shift register with appropriate connections specified by the
325  * elements of Gg[], which was generated above. Codeword is   c(X) =
326  * data(X)*X**(NN-KK)+ b(X)
327  */
328 int
329 encode_rs(dtype *data, dtype *bb)
330 {
331         register int i, j;
332         gf feedback;
333
334         CLEAR(bb,NN-KK);
335         for (i = KK - 1; i >= 0; i--) {
336 #if (MM != 8)
337                 if(data[i] > NN)
338                         return -1;      /* Illegal symbol */
339 #endif
340                 feedback = Index_of[data[i] ^ bb[NN - KK - 1]];
341                 if (feedback != A0) {   /* feedback term is non-zero */
342                         for (j = NN - KK - 1; j > 0; j--)
343                                 if (Gg[j] != A0)
344                                         bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)];
345                                 else
346                                         bb[j] = bb[j - 1];
347                         bb[0] = Alpha_to[modnn(Gg[0] + feedback)];
348                 } else {        /* feedback term is zero. encoder becomes a
349                                  * single-byte shifter */
350                         for (j = NN - KK - 1; j > 0; j--)
351                                 bb[j] = bb[j - 1];
352                         bb[0] = 0;
353                 }
354         }
355         return 0;
356 }
357
358 /*
359  * Performs ERRORS+ERASURES decoding of RS codes. If decoding is successful,
360  * writes the codeword into data[] itself. Otherwise data[] is unaltered.
361  *
362  * Return number of symbols corrected, or -1 if codeword is illegal
363  * or uncorrectable.
364  * 
365  * First "no_eras" erasures are declared by the calling program. Then, the
366  * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
367  * If the number of channel errors is not greater than "t_after_eras" the
368  * transmitted codeword will be recovered. Details of algorithm can be found
369  * in R. Blahut's "Theory ... of Error-Correcting Codes".
370  */
371 int
372 eras_dec_rs(dtype *data, int *eras_pos, int no_eras)
373 {
374         int deg_lambda, el, deg_omega;
375         int i, j, r;
376         gf u,q,tmp,num1,num2,den,discr_r;
377         gf recd[NN];
378         /* Err+Eras Locator poly and syndrome poly */
379         /*gf lambda[NN-KK + 1], s[NN-KK + 1];   
380         gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
381         gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];*/
382         gf lambda[NN + 1], s[NN + 1];   
383         gf b[NN + 1], t[NN + 1], omega[NN + 1];
384         gf root[NN], reg[NN + 1], loc[NN];
385         int syn_error, count;
386
387         /* data[] is in polynomial form, copy and convert to index form */
388         for (i = NN-1; i >= 0; i--){
389 #if (MM != 8)
390                 if(data[i] > NN)
391                         return -1;      /* Illegal symbol */
392 #endif
393                 recd[i] = Index_of[data[i]];
394         }
395         /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x)
396          * namely @**(B0+i), i = 0, ... ,(NN-KK-1)
397          */
398         syn_error = 0;
399         for (i = 1; i <= NN-KK; i++) {
400                 tmp = 0;
401                 for (j = 0; j < NN; j++)
402                         if (recd[j] != A0)      /* recd[j] in index form */
403                                 tmp ^= Alpha_to[modnn(recd[j] + (B0+i-1)*j)];
404                 syn_error |= tmp;       /* set flag if non-zero syndrome =>
405                                          * error */
406                 /* store syndrome in index form  */
407                 s[i] = Index_of[tmp];
408         }
409         if (!syn_error) {
410                 /*
411                  * if syndrome is zero, data[] is a codeword and there are no
412                  * errors to correct. So return data[] unmodified
413                  */
414                 return 0;
415         }
416         CLEAR(&lambda[1],NN-KK);
417         lambda[0] = 1;
418         if (no_eras > 0) {
419                 /* Init lambda to be the erasure locator polynomial */
420                 lambda[1] = Alpha_to[eras_pos[0]];
421                 for (i = 1; i < no_eras; i++) {
422                         u = eras_pos[i];
423                         for (j = i+1; j > 0; j--) {
424                                 tmp = Index_of[lambda[j - 1]];
425                                 if(tmp != A0)
426                                         lambda[j] ^= Alpha_to[modnn(u + tmp)];
427                         }
428                 }
429 #ifdef ERASURE_DEBUG
430                 /* find roots of the erasure location polynomial */
431                 for(i=1;i<=no_eras;i++)
432                         reg[i] = Index_of[lambda[i]];
433                 count = 0;
434                 for (i = 1; i <= NN; i++) {
435                         q = 1;
436                         for (j = 1; j <= no_eras; j++)
437                                 if (reg[j] != A0) {
438                                         reg[j] = modnn(reg[j] + j);
439                                         q ^= Alpha_to[reg[j]];
440                                 }
441                         if (!q) {
442                                 /* store root and error location
443                                  * number indices
444                                  */
445                                 root[count] = i;
446                                 loc[count] = NN - i;
447                                 count++;
448                         }
449                 }
450                 if (count != no_eras) {
451                         printf("\n lambda(x) is WRONG\n");
452                         return -1;
453                 }
454 #ifndef NO_PRINT
455                 printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
456                 for (i = 0; i < count; i++)
457                         printf("%d ", loc[i]);
458                 printf("\n");
459 #endif
460 #endif
461         }
462         for(i=0;i<NN-KK+1;i++)
463                 b[i] = Index_of[lambda[i]];
464
465         /*
466          * Begin Berlekamp-Massey algorithm to determine error+erasure
467          * locator polynomial
468          */
469         r = no_eras;
470         el = no_eras;
471         while (++r <= NN-KK) {  /* r is the step number */
472                 /* Compute discrepancy at the r-th step in poly-form */
473                 discr_r = 0;
474                 for (i = 0; i < r; i++){
475                         if ((lambda[i] != 0) && (s[r - i] != A0)) {
476                                 discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
477                         }
478                 }
479                 discr_r = Index_of[discr_r];    /* Index form */
480                 if (discr_r == A0) {
481                         /* 2 lines below: B(x) <-- x*B(x) */
482                         COPYDOWN(&b[1],b,NN-KK);
483                         b[0] = A0;
484                 } else {
485                         /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
486                         t[0] = lambda[0];
487                         for (i = 0 ; i < NN-KK; i++) {
488                                 if(b[i] != A0)
489                                         t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
490                                 else
491                                         t[i+1] = lambda[i+1];
492                         }
493                         if (2 * el <= r + no_eras - 1) {
494                                 el = r + no_eras - el;
495                                 /*
496                                  * 2 lines below: B(x) <-- inv(discr_r) *
497                                  * lambda(x)
498                                  */
499                                 for (i = 0; i <= NN-KK; i++)
500                                         b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
501                         } else {
502                                 /* 2 lines below: B(x) <-- x*B(x) */
503                                 COPYDOWN(&b[1],b,NN-KK);
504                                 b[0] = A0;
505                         }
506                         COPY(lambda,t,NN-KK+1);
507                 }
508         }
509
510         /* Convert lambda to index form and compute deg(lambda(x)) */
511         deg_lambda = 0;
512         for(i=0;i<NN-KK+1;i++){
513                 lambda[i] = Index_of[lambda[i]];
514                 if(lambda[i] != A0)
515                         deg_lambda = i;
516         }
517         /*
518          * Find roots of the error+erasure locator polynomial. By Chien
519          * Search
520          */
521         COPY(&reg[1],&lambda[1],NN-KK);
522         count = 0;              /* Number of roots of lambda(x) */
523         for (i = 1; i <= NN; i++) {
524                 q = 1;
525                 for (j = deg_lambda; j > 0; j--)
526                         if (reg[j] != A0) {
527                                 reg[j] = modnn(reg[j] + j);
528                                 q ^= Alpha_to[reg[j]];
529                         }
530                 if (!q) {
531                         /* store root (index-form) and error location number */
532                         root[count] = i;
533                         loc[count] = NN - i;
534                         count++;
535                 }
536         }
537
538 #ifdef DEBUG
539         printf("\n Final error positions:\t");
540         for (i = 0; i < count; i++)
541                 printf("%d ", loc[i]);
542         printf("\n");
543 #endif
544         if (deg_lambda != count) {
545                 /*
546                  * deg(lambda) unequal to number of roots => uncorrectable
547                  * error detected
548                  */
549                 return -1;
550         }
551         /*
552          * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
553          * x**(NN-KK)). in index form. Also find deg(omega).
554          */
555         deg_omega = 0;
556         for (i = 0; i < NN-KK;i++){
557                 tmp = 0;
558                 j = (deg_lambda < i) ? deg_lambda : i;
559                 for(;j >= 0; j--){
560                         if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
561                                 tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
562                 }
563                 if(tmp != 0)
564                         deg_omega = i;
565                 omega[i] = Index_of[tmp];
566         }
567         omega[NN-KK] = A0;
568
569         /*
570          * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
571          * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
572          */
573         for (j = count-1; j >=0; j--) {
574                 num1 = 0;
575                 for (i = deg_omega; i >= 0; i--) {
576                         if (omega[i] != A0)
577                                 num1  ^= Alpha_to[modnn(omega[i] + i * root[j])];
578                 }
579                 num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
580                 den = 0;
581
582                 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
583                 for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
584                         if(lambda[i+1] != A0)
585                                 den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
586                 }
587                 if (den == 0) {
588 #ifdef DEBUG
589                         printf("\n ERROR: denominator = 0\n");
590 #endif
591                         return -1;
592                 }
593                 /* Apply error to data */
594                 if (num1 != 0) {
595                         data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
596                 }
597         }
598         return count;
599 }
600
601
602 #endif /* USE_JPWL */