ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Algorithm-FEC/fec_imp.h
Revision: 1.2
Committed: Tue Sep 9 06:07:28 2003 UTC (23 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +3 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * fec.c -- forward error correction based on Vandermonde matrices
3     * 980624
4     * (C) 1997-98 Luigi Rizzo (luigi@iet.unipi.it)
5     *
6     * Portions derived from code by Phil Karn (karn@ka9q.ampr.org),
7     * Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari
8     * Thirumoorthy (harit@spectra.eng.hawaii.edu), Aug 1995
9     * modified by Marc Lehmann <fec@schmorp.de>, Sep 2003.
10     *
11     * Redistribution and use in source and binary forms, with or without
12     * modification, are permitted provided that the following conditions
13     * are met:
14     *
15     * 1. Redistributions of source code must retain the above copyright
16     * notice, this list of conditions and the following disclaimer.
17     * 2. Redistributions in binary form must reproduce the above
18     * copyright notice, this list of conditions and the following
19     * disclaimer in the documentation and/or other materials
20     * provided with the distribution.
21     *
22     * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND
23     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25     * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
26     * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27     * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
29     * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31     * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33     * OF SUCH DAMAGE.
34     */
35    
36     #include <stdio.h>
37     #include <stdlib.h>
38     #include <string.h>
39 root 1.2 #include <stdint.h>
40 root 1.1
41     #define MSDOS /* LEAVE THIS IN PLACE EVEN ON UNIX! */
42    
43     /*
44     * compatibility stuff
45     */
46     #ifdef MSDOS /* but also for others, e.g. sun... */
47     #define NEED_BCOPY
48     #define bcmp(a,b,n) memcmp(a,b,n)
49     #endif
50    
51     #ifdef NEED_BCOPY
52     #define bcopy(s, d, siz) memcpy((d), (s), (siz))
53     #define bzero(d, siz) memset((d), '\0', (siz))
54     #endif
55    
56     /*
57     * stuff used for testing purposes only
58     */
59    
60     #ifdef TEST
61     #define DEB(x)
62     #define DDB(x) x
63     #define DEBUG 0 /* minimal debugging */
64    
65     #ifdef MSDOS
66     #include <time.h>
67     struct timeval {
68     unsigned long ticks;
69     };
70     #define gettimeofday(x, dummy) { (x)->ticks = clock() ; }
71     #define DIFF_T(a,b) (1+ 1000000*(a.ticks - b.ticks) / CLOCKS_PER_SEC )
72     typedef unsigned long u_long ;
73     typedef unsigned short u_short ;
74     #else /* typically, unix systems */
75     #include <sys/time.h>
76     #define DIFF_T(a,b) \
77     (1+ 1000000*(a.tv_sec - b.tv_sec) + (a.tv_usec - b.tv_usec) )
78     #endif
79    
80     #define TICK(t) \
81     {struct timeval x ; \
82     gettimeofday(&x, NULL) ; \
83     t = x.tv_usec + 1000000* (x.tv_sec & 0xff ) ; \
84     }
85     #define TOCK(t) \
86     { u_long t1 ; TICK(t1) ; \
87     if (t1 < t) t = 256000000 + t1 - t ; \
88     else t = t1 - t ; \
89     if (t == 0) t = 1 ;}
90    
91     u_long ticks[10]; /* vars for timekeeping */
92     #else
93     #define DEB(x)
94     #define DDB(x)
95     #define TICK(x)
96     #define TOCK(x)
97     #endif /* TEST */
98    
99     /*
100     * You should not need to change anything beyond this point.
101     * The first part of the file implements linear algebra in GF.
102     *
103     * gf is the type used to store an element of the Galois Field.
104     * Must constain at least GF_BITS bits.
105     *
106     * Note: unsigned char will work up to GF(256) but int seems to run
107     * faster on the Pentium. We use int whenever have to deal with an
108     * index, since they are generally faster.
109     */
110     #if (GF_BITS < 2 && GF_BITS >16)
111     #error "GF_BITS must be 2 .. 16"
112     #endif
113     #if (GF_BITS <= 8)
114 root 1.2 typedef uint8_t gf;
115 root 1.1 #else
116 root 1.2 typedef uint16_t gf;
117 root 1.1 #endif
118    
119     #define GF_SIZE ((1 << GF_BITS) - 1) /* powers of \alpha */
120    
121     /*
122     * Primitive polynomials - see Lin & Costello, Appendix A,
123     * and Lee & Messerschmitt, p. 453.
124     */
125     static char *allPp[] = { /* GF_BITS polynomial */
126     NULL, /* 0 no code */
127     NULL, /* 1 no code */
128     "111", /* 2 1+x+x^2 */
129     "1101", /* 3 1+x+x^3 */
130     "11001", /* 4 1+x+x^4 */
131     "101001", /* 5 1+x^2+x^5 */
132     "1100001", /* 6 1+x+x^6 */
133     "10010001", /* 7 1 + x^3 + x^7 */
134     "101110001", /* 8 1+x^2+x^3+x^4+x^8 */
135     "1000100001", /* 9 1+x^4+x^9 */
136     "10010000001", /* 10 1+x^3+x^10 */
137     "101000000001", /* 11 1+x^2+x^11 */
138     "1100101000001", /* 12 1+x+x^4+x^6+x^12 */
139     "11011000000001", /* 13 1+x+x^3+x^4+x^13 */
140     "110000100010001", /* 14 1+x+x^6+x^10+x^14 */
141     "1100000000000001", /* 15 1+x+x^15 */
142     "11010000000010001" /* 16 1+x+x^3+x^12+x^16 */
143     };
144    
145    
146     /*
147     * To speed up computations, we have tables for logarithm, exponent
148     * and inverse of a number. If GF_BITS <= 8, we use a table for
149     * multiplication as well (it takes 64K, no big deal even on a PDA,
150     * especially because it can be pre-initialized an put into a ROM!),
151     * otherwhise we use a table of logarithms.
152     * In any case the macro gf_mul(x,y) takes care of multiplications.
153     */
154    
155     static gf gf_exp[2*GF_SIZE]; /* index->poly form conversion table */
156     static int gf_log[GF_SIZE + 1]; /* Poly->index form conversion table */
157     static gf inverse[GF_SIZE+1]; /* inverse of field elem. */
158     /* inv[\alpha**i]=\alpha**(GF_SIZE-i-1) */
159    
160     /*
161     * modnn(x) computes x % GF_SIZE, where GF_SIZE is 2**GF_BITS - 1,
162     * without a slow divide.
163     */
164     static inline gf
165     modnn(int x)
166     {
167     while (x >= GF_SIZE) {
168     x -= GF_SIZE;
169     x = (x >> GF_BITS) + (x & GF_SIZE);
170     }
171     return x;
172     }
173    
174     #define SWAP(a,b,t) {t tmp; tmp=a; a=b; b=tmp;}
175    
176     /*
177     * gf_mul(x,y) multiplies two numbers. If GF_BITS<=8, it is much
178     * faster to use a multiplication table.
179     *
180     * USE_GF_MULC, GF_MULC0(c) and GF_ADDMULC(x) can be used when multiplying
181     * many numbers by the same constant. In this case the first
182     * call sets the constant, and others perform the multiplications.
183     * A value related to the multiplication is held in a local variable
184     * declared with USE_GF_MULC . See usage in addmul1().
185     */
186     #if (GF_BITS <= 8)
187     static gf gf_mul_table[GF_SIZE + 1][GF_SIZE + 1];
188    
189     #define gf_mul(x,y) gf_mul_table[x][y]
190    
191     #define USE_GF_MULC register gf * __gf_mulc_
192     #define GF_MULC0(c) __gf_mulc_ = gf_mul_table[c]
193     #define GF_ADDMULC(dst, x) dst ^= __gf_mulc_[x]
194    
195     static void
196     init_mul_table()
197     {
198     int i, j;
199     for (i=0; i< GF_SIZE+1; i++)
200     for (j=0; j< GF_SIZE+1; j++)
201     gf_mul_table[i][j] = gf_exp[modnn(gf_log[i] + gf_log[j]) ] ;
202    
203     for (j=0; j< GF_SIZE+1; j++)
204     gf_mul_table[0][j] = gf_mul_table[j][0] = 0;
205     }
206     #else /* GF_BITS > 8 */
207     static inline gf
208     gf_mul(x,y)
209     {
210     if ( (x) == 0 || (y)==0 ) return 0;
211    
212     return gf_exp[gf_log[x] + gf_log[y] ] ;
213     }
214     #define init_mul_table()
215    
216     #define USE_GF_MULC register gf * __gf_mulc_
217     #define GF_MULC0(c) __gf_mulc_ = &gf_exp[ gf_log[c] ]
218     #define GF_ADDMULC(dst, x) { if (x) dst ^= __gf_mulc_[ gf_log[x] ] ; }
219     #endif
220    
221     /*
222     * Generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
223     * Lookup tables:
224     * index->polynomial form gf_exp[] contains j= \alpha^i;
225     * polynomial form -> index form gf_log[ j = \alpha^i ] = i
226     * \alpha=x is the primitive element of GF(2^m)
227     *
228     * For efficiency, gf_exp[] has size 2*GF_SIZE, so that a simple
229     * multiplication of two numbers can be resolved without calling modnn
230     */
231    
232     /*
233     * i use malloc so many times, it is easier to put checks all in
234     * one place.
235     */
236     static void *
237     my_malloc(int sz, char *err_string)
238     {
239     void *p = malloc( sz );
240     if (p == NULL) {
241     fprintf(stderr, "-- malloc failure allocating %s\n", err_string);
242     exit(1) ;
243     }
244     return p ;
245     }
246    
247     #define NEW_GF_MATRIX(rows, cols) \
248     (gf *)my_malloc(rows * cols * sizeof(gf), " ## __LINE__ ## " )
249    
250     /*
251     * initialize the data structures used for computations in GF.
252     */
253     static void
254     generate_gf(void)
255     {
256     int i;
257     gf mask;
258     char *Pp = allPp[GF_BITS] ;
259    
260     mask = 1; /* x ** 0 = 1 */
261     gf_exp[GF_BITS] = 0; /* will be updated at the end of the 1st loop */
262     /*
263     * first, generate the (polynomial representation of) powers of \alpha,
264     * which are stored in gf_exp[i] = \alpha ** i .
265     * At the same time build gf_log[gf_exp[i]] = i .
266     * The first GF_BITS powers are simply bits shifted to the left.
267     */
268     for (i = 0; i < GF_BITS; i++, mask <<= 1 ) {
269     gf_exp[i] = mask;
270     gf_log[gf_exp[i]] = i;
271     /*
272     * If Pp[i] == 1 then \alpha ** i occurs in poly-repr
273     * gf_exp[GF_BITS] = \alpha ** GF_BITS
274     */
275     if ( Pp[i] == '1' )
276     gf_exp[GF_BITS] ^= mask;
277     }
278     /*
279     * now gf_exp[GF_BITS] = \alpha ** GF_BITS is complete, so can als
280     * compute its inverse.
281     */
282     gf_log[gf_exp[GF_BITS]] = GF_BITS;
283     /*
284     * Poly-repr of \alpha ** (i+1) is given by poly-repr of
285     * \alpha ** i shifted left one-bit and accounting for any
286     * \alpha ** GF_BITS term that may occur when poly-repr of
287     * \alpha ** i is shifted.
288     */
289     mask = 1 << (GF_BITS - 1 ) ;
290     for (i = GF_BITS + 1; i < GF_SIZE; i++) {
291     if (gf_exp[i - 1] >= mask)
292     gf_exp[i] = gf_exp[GF_BITS] ^ ((gf_exp[i - 1] ^ mask) << 1);
293     else
294     gf_exp[i] = gf_exp[i - 1] << 1;
295     gf_log[gf_exp[i]] = i;
296     }
297     /*
298     * log(0) is not defined, so use a special value
299     */
300     gf_log[0] = GF_SIZE ;
301     /* set the extended gf_exp values for fast multiply */
302     for (i = 0 ; i < GF_SIZE ; i++)
303     gf_exp[i + GF_SIZE] = gf_exp[i] ;
304    
305     /*
306     * again special cases. 0 has no inverse. This used to
307     * be initialized to GF_SIZE, but it should make no difference
308     * since noone is supposed to read from here.
309     */
310     inverse[0] = 0 ;
311     inverse[1] = 1;
312     for (i=2; i<=GF_SIZE; i++)
313     inverse[i] = gf_exp[GF_SIZE-gf_log[i]];
314     }
315    
316     /*
317     * Various linear algebra operations that i use often.
318     */
319    
320     /*
321     * addmul() computes dst[] = dst[] + c * src[]
322     * This is used often, so better optimize it! Currently the loop is
323     * unrolled 16 times, a good value for 486 and pentium-class machines.
324     * The case c=0 is also optimized, whereas c=1 is not. These
325     * calls are unfrequent in my typical apps so I did not bother.
326     *
327     * Note that gcc on
328     */
329     #define addmul(dst, src, c, sz) \
330     if (c != 0) addmul1(dst, src, c, sz)
331    
332     #define UNROLL 16 /* 1, 4, 8, 16 */
333     static void
334     addmul1(gf *dst1, gf *src1, gf c, int sz)
335     {
336     USE_GF_MULC ;
337     register gf *dst = dst1, *src = src1 ;
338     gf *lim = &dst[sz - UNROLL + 1] ;
339    
340     GF_MULC0(c) ;
341    
342     #if (UNROLL > 1) /* unrolling by 8/16 is quite effective on the pentium */
343     for (; dst < lim ; dst += UNROLL, src += UNROLL ) {
344     GF_ADDMULC( dst[0] , src[0] );
345     GF_ADDMULC( dst[1] , src[1] );
346     GF_ADDMULC( dst[2] , src[2] );
347     GF_ADDMULC( dst[3] , src[3] );
348     #if (UNROLL > 4)
349     GF_ADDMULC( dst[4] , src[4] );
350     GF_ADDMULC( dst[5] , src[5] );
351     GF_ADDMULC( dst[6] , src[6] );
352     GF_ADDMULC( dst[7] , src[7] );
353     #endif
354     #if (UNROLL > 8)
355     GF_ADDMULC( dst[8] , src[8] );
356     GF_ADDMULC( dst[9] , src[9] );
357     GF_ADDMULC( dst[10] , src[10] );
358     GF_ADDMULC( dst[11] , src[11] );
359     GF_ADDMULC( dst[12] , src[12] );
360     GF_ADDMULC( dst[13] , src[13] );
361     GF_ADDMULC( dst[14] , src[14] );
362     GF_ADDMULC( dst[15] , src[15] );
363     #endif
364     }
365     #endif
366     lim += UNROLL - 1 ;
367     for (; dst < lim; dst++, src++ ) /* final components */
368     GF_ADDMULC( *dst , *src );
369     }
370    
371     /*
372     * computes C = AB where A is n*k, B is k*m, C is n*m
373     */
374     static void
375     matmul(gf *a, gf *b, gf *c, int n, int k, int m)
376     {
377     int row, col, i ;
378    
379     for (row = 0; row < n ; row++) {
380     for (col = 0; col < m ; col++) {
381     gf *pa = &a[ row * k ];
382     gf *pb = &b[ col ];
383     gf acc = 0 ;
384     for (i = 0; i < k ; i++, pa++, pb += m )
385     acc ^= gf_mul( *pa, *pb ) ;
386     c[ row * m + col ] = acc ;
387     }
388     }
389     }
390    
391     #ifdef DEBUG
392     /*
393     * returns 1 if the square matrix is identiy
394     * (only for test)
395     */
396     static int
397     is_identity(gf *m, int k)
398     {
399     int row, col ;
400     for (row=0; row<k; row++)
401     for (col=0; col<k; col++)
402     if ( (row==col && *m != 1) ||
403     (row!=col && *m != 0) )
404     return 0 ;
405     else
406     m++ ;
407     return 1 ;
408     }
409     #endif /* debug */
410    
411     /*
412     * invert_mat() takes a matrix and produces its inverse
413     * k is the size of the matrix.
414     * (Gauss-Jordan, adapted from Numerical Recipes in C)
415     * Return non-zero if singular.
416     */
417     DEB( int pivloops=0; int pivswaps=0 ; /* diagnostic */)
418     static int
419     invert_mat(gf *src, int k)
420     {
421     gf c, *p ;
422     int irow, icol, row, col, i, ix ;
423    
424     int error = 1 ;
425     int *indxc = my_malloc(k*sizeof(int), "indxc");
426     int *indxr = my_malloc(k*sizeof(int), "indxr");
427     int *ipiv = my_malloc(k*sizeof(int), "ipiv");
428     gf *id_row = NEW_GF_MATRIX(1, k);
429     gf *temp_row = NEW_GF_MATRIX(1, k);
430    
431     bzero(id_row, k*sizeof(gf));
432     DEB( pivloops=0; pivswaps=0 ; /* diagnostic */ )
433     /*
434     * ipiv marks elements already used as pivots.
435     */
436     for (i = 0; i < k ; i++)
437     ipiv[i] = 0 ;
438    
439     for (col = 0; col < k ; col++) {
440     gf *pivot_row ;
441     /*
442     * Zeroing column 'col', look for a non-zero element.
443     * First try on the diagonal, if it fails, look elsewhere.
444     */
445     irow = icol = -1 ;
446     if (ipiv[col] != 1 && src[col*k + col] != 0) {
447     irow = col ;
448     icol = col ;
449     goto found_piv ;
450     }
451     for (row = 0 ; row < k ; row++) {
452     if (ipiv[row] != 1) {
453     for (ix = 0 ; ix < k ; ix++) {
454     DEB( pivloops++ ; )
455     if (ipiv[ix] == 0) {
456     if (src[row*k + ix] != 0) {
457     irow = row ;
458     icol = ix ;
459     goto found_piv ;
460     }
461     } else if (ipiv[ix] > 1) {
462     fprintf(stderr, "singular matrix\n");
463     goto fail ;
464     }
465     }
466     }
467     }
468     if (icol == -1) {
469     fprintf(stderr, "XXX pivot not found!\n");
470     goto fail ;
471     }
472     found_piv:
473     ++(ipiv[icol]) ;
474     /*
475     * swap rows irow and icol, so afterwards the diagonal
476     * element will be correct. Rarely done, not worth
477     * optimizing.
478     */
479     if (irow != icol) {
480     for (ix = 0 ; ix < k ; ix++ ) {
481     SWAP( src[irow*k + ix], src[icol*k + ix], gf) ;
482     }
483     }
484     indxr[col] = irow ;
485     indxc[col] = icol ;
486     pivot_row = &src[icol*k] ;
487     c = pivot_row[icol] ;
488     if (c == 0) {
489     fprintf(stderr, "singular matrix 2\n");
490     goto fail ;
491     }
492     if (c != 1 ) { /* otherwhise this is a NOP */
493     /*
494     * this is done often , but optimizing is not so
495     * fruitful, at least in the obvious ways (unrolling)
496     */
497     DEB( pivswaps++ ; )
498     c = inverse[ c ] ;
499     pivot_row[icol] = 1 ;
500     for (ix = 0 ; ix < k ; ix++ )
501     pivot_row[ix] = gf_mul(c, pivot_row[ix] );
502     }
503     /*
504     * from all rows, remove multiples of the selected row
505     * to zero the relevant entry (in fact, the entry is not zero
506     * because we know it must be zero).
507     * (Here, if we know that the pivot_row is the identity,
508     * we can optimize the addmul).
509     */
510     id_row[icol] = 1;
511     if (bcmp(pivot_row, id_row, k*sizeof(gf)) != 0) {
512     for (p = src, ix = 0 ; ix < k ; ix++, p += k ) {
513     if (ix != icol) {
514     c = p[icol] ;
515     p[icol] = 0 ;
516     addmul(p, pivot_row, c, k );
517     }
518     }
519     }
520     id_row[icol] = 0;
521     } /* done all columns */
522     for (col = k-1 ; col >= 0 ; col-- ) {
523     if (indxr[col] <0 || indxr[col] >= k)
524     fprintf(stderr, "AARGH, indxr[col] %d\n", indxr[col]);
525     else if (indxc[col] <0 || indxc[col] >= k)
526     fprintf(stderr, "AARGH, indxc[col] %d\n", indxc[col]);
527     else
528     if (indxr[col] != indxc[col] ) {
529     for (row = 0 ; row < k ; row++ ) {
530     SWAP( src[row*k + indxr[col]], src[row*k + indxc[col]], gf) ;
531     }
532     }
533     }
534     error = 0 ;
535     fail:
536     free(indxc);
537     free(indxr);
538     free(ipiv);
539     free(id_row);
540     free(temp_row);
541     return error ;
542     }
543    
544     /*
545     * fast code for inverting a vandermonde matrix.
546     * XXX NOTE: It assumes that the matrix
547     * is not singular and _IS_ a vandermonde matrix. Only uses
548     * the second column of the matrix, containing the p_i's.
549     *
550     * Algorithm borrowed from "Numerical recipes in C" -- sec.2.8, but
551     * largely revised for my purposes.
552     * p = coefficients of the matrix (p_i)
553     * q = values of the polynomial (known)
554     */
555    
556     static int
557     invert_vdm(gf *src, int k)
558     {
559     int i, j, row, col ;
560     gf *b, *c, *p;
561     gf t, xx ;
562    
563     if (k == 1) /* degenerate case, matrix must be p^0 = 1 */
564     return 0 ;
565     /*
566     * c holds the coefficient of P(x) = Prod (x - p_i), i=0..k-1
567     * b holds the coefficient for the matrix inversion
568     */
569     c = NEW_GF_MATRIX(1, k);
570     b = NEW_GF_MATRIX(1, k);
571    
572     p = NEW_GF_MATRIX(1, k);
573    
574     for ( j=1, i = 0 ; i < k ; i++, j+=k ) {
575     c[i] = 0 ;
576     p[i] = src[j] ; /* p[i] */
577     }
578     /*
579     * construct coeffs. recursively. We know c[k] = 1 (implicit)
580     * and start P_0 = x - p_0, then at each stage multiply by
581     * x - p_i generating P_i = x P_{i-1} - p_i P_{i-1}
582     * After k steps we are done.
583     */
584     c[k-1] = p[0] ; /* really -p(0), but x = -x in GF(2^m) */
585     for (i = 1 ; i < k ; i++ ) {
586     gf p_i = p[i] ; /* see above comment */
587     for (j = k-1 - ( i - 1 ) ; j < k-1 ; j++ )
588     c[j] ^= gf_mul( p_i, c[j+1] ) ;
589     c[k-1] ^= p_i ;
590     }
591    
592     for (row = 0 ; row < k ; row++ ) {
593     /*
594     * synthetic division etc.
595     */
596     xx = p[row] ;
597     t = 1 ;
598     b[k-1] = 1 ; /* this is in fact c[k] */
599     for (i = k-2 ; i >= 0 ; i-- ) {
600     b[i] = c[i+1] ^ gf_mul(xx, b[i+1]) ;
601     t = gf_mul(xx, t) ^ b[i] ;
602     }
603     for (col = 0 ; col < k ; col++ )
604     src[col*k + row] = gf_mul(inverse[t], b[col] );
605     }
606     free(c) ;
607     free(b) ;
608     free(p) ;
609     return 0 ;
610     }
611    
612     static int fec_initialized = 0 ;
613    
614     static void init_fec()
615     {
616     TICK(ticks[0]);
617     generate_gf();
618     TOCK(ticks[0]);
619     DDB(fprintf(stderr, "generate_gf took %ldus\n", ticks[0]);)
620     TICK(ticks[0]);
621     init_mul_table();
622     TOCK(ticks[0]);
623     DDB(fprintf(stderr, "init_mul_table took %ldus\n", ticks[0]);)
624     fec_initialized = 1 ;
625     }
626    
627     /*
628     * This section contains the proper FEC encoding/decoding routines.
629     * The encoding matrix is computed starting with a Vandermonde matrix,
630     * and then transforming it into a systematic matrix.
631     */
632    
633     #define FEC_MAGIC 0xFECC0DEC
634    
635     struct fec_parms {
636     u_long magic ;
637     int k, n ; /* parameters of the code */
638     gf *enc_matrix ;
639     } ;
640    
641     void
642     fec_free(struct fec_parms *p)
643     {
644     if (p==NULL ||
645     p->magic != ( ( (FEC_MAGIC ^ p->k) ^ p->n) ^ (int)(p->enc_matrix)) ) {
646     fprintf(stderr, "bad parameters to fec_free\n");
647     return ;
648     }
649     free(p->enc_matrix);
650     free(p);
651     }
652    
653     /*
654     * create a new encoder, returning a descriptor. This contains k,n and
655     * the encoding matrix.
656     */
657     struct fec_parms *
658     fec_new(int k, int n)
659     {
660     int row, col ;
661     gf *p, *tmp_m ;
662    
663     struct fec_parms *retval ;
664    
665     if (fec_initialized == 0)
666     init_fec();
667    
668     if (k > GF_SIZE + 1 || n > GF_SIZE + 1 || k > n ) {
669     fprintf(stderr, "Invalid parameters k %d n %d GF_SIZE %d\n",
670     k, n, GF_SIZE );
671     return NULL ;
672     }
673     retval = my_malloc(sizeof(struct fec_parms), "new_code");
674     retval->k = k ;
675     retval->n = n ;
676     retval->enc_matrix = NEW_GF_MATRIX(n, k);
677     retval->magic = ( ( FEC_MAGIC ^ k) ^ n) ^ (int)(retval->enc_matrix) ;
678     tmp_m = NEW_GF_MATRIX(n, k);
679     /*
680     * fill the matrix with powers of field elements, starting from 0.
681     * The first row is special, cannot be computed with exp. table.
682     */
683     tmp_m[0] = 1 ;
684     for (col = 1; col < k ; col++)
685     tmp_m[col] = 0 ;
686     for (p = tmp_m + k, row = 0; row < n-1 ; row++, p += k) {
687     for ( col = 0 ; col < k ; col ++ )
688     p[col] = gf_exp[modnn(row*col)];
689     }
690    
691     /*
692     * quick code to build systematic matrix: invert the top
693     * k*k vandermonde matrix, multiply right the bottom n-k rows
694     * by the inverse, and construct the identity matrix at the top.
695     */
696     TICK(ticks[3]);
697     invert_vdm(tmp_m, k); /* much faster than invert_mat */
698     matmul(tmp_m + k*k, tmp_m, retval->enc_matrix + k*k, n - k, k, k);
699     /*
700     * the upper matrix is I so do not bother with a slow multiply
701     */
702     bzero(retval->enc_matrix, k*k*sizeof(gf) );
703     for (p = retval->enc_matrix, col = 0 ; col < k ; col++, p += k+1 )
704     *p = 1 ;
705     free(tmp_m);
706     TOCK(ticks[3]);
707    
708     DDB(fprintf(stderr, "--- %ld us to build encoding matrix\n",
709     ticks[3]);)
710     DEB(pr_matrix(retval->enc_matrix, n, k, "encoding_matrix");)
711     return retval ;
712     }
713    
714     /*
715     * fec_encode accepts as input pointers to n data packets of size sz,
716     * and produces as output a packet pointed to by fec, computed
717     * with index "index".
718     */
719     void
720     fec_encode(struct fec_parms *code, gf *src[], gf *fec, int index, int sz)
721     {
722     int i, k = code->k ;
723     gf *p ;
724    
725     if (GF_BITS > 8)
726     sz /= 2 ;
727    
728     if (index < k)
729     bcopy(src[index], fec, sz*sizeof(gf) ) ;
730     else if (index < code->n) {
731     p = &(code->enc_matrix[index*k] );
732     bzero(fec, sz*sizeof(gf));
733     for (i = 0; i < k ; i++)
734     addmul(fec, src[i], p[i], sz ) ;
735     } else
736     fprintf(stderr, "Invalid index %d (max %d)\n",
737     index, code->n - 1 );
738     }
739    
740     /*
741     * shuffle move src packets in their position
742     */
743     static int
744     shuffle(gf *pkt[], int index[], int k)
745     {
746     int i;
747    
748     for ( i = 0 ; i < k ; ) {
749     if (index[i] >= k || index[i] == i)
750     i++ ;
751     else {
752     /*
753     * put pkt in the right position (first check for conflicts).
754     */
755     int c = index[i] ;
756    
757     if (index[c] == c) {
758     DEB(fprintf(stderr, "\nshuffle, error at %d\n", i);)
759     return 1 ;
760     }
761     SWAP(index[i], index[c], int) ;
762     SWAP(pkt[i], pkt[c], gf *) ;
763     }
764     }
765     DEB( /* just test that it works... */
766     for ( i = 0 ; i < k ; i++ ) {
767     if (index[i] < k && index[i] != i) {
768     fprintf(stderr, "shuffle: after\n");
769     for (i=0; i<k ; i++) fprintf(stderr, "%3d ", index[i]);
770     fprintf(stderr, "\n");
771     return 1 ;
772     }
773     }
774     )
775     return 0 ;
776     }
777    
778     /*
779     * build_decode_matrix constructs the encoding matrix given the
780     * indexes. The matrix must be already allocated as
781     * a vector of k*k elements, in row-major order
782     */
783     static gf *
784     build_decode_matrix(struct fec_parms *code, gf *pkt[], int index[])
785     {
786     int i , k = code->k ;
787     gf *p, *matrix = NEW_GF_MATRIX(k, k);
788    
789     TICK(ticks[9]);
790     for (i = 0, p = matrix ; i < k ; i++, p += k ) {
791     #if 1 /* this is simply an optimization, not very useful indeed */
792     if (index[i] < k) {
793     bzero(p, k*sizeof(gf) );
794     p[i] = 1 ;
795     } else
796     #endif
797     if (index[i] < code->n )
798     bcopy( &(code->enc_matrix[index[i]*k]), p, k*sizeof(gf) );
799     else {
800     fprintf(stderr, "decode: invalid index %d (max %d)\n",
801     index[i], code->n - 1 );
802     free(matrix) ;
803     return NULL ;
804     }
805     }
806     TICK(ticks[9]);
807     if (invert_mat(matrix, k)) {
808     free(matrix);
809     matrix = NULL ;
810     }
811     TOCK(ticks[9]);
812     return matrix ;
813     }
814    
815     /*
816     * fec_decode receives as input a vector of packets, the indexes of
817     * packets, and produces the correct vector as output.
818     *
819     * Input:
820     * code: pointer to code descriptor
821     * pkt: pointers to received packets. They are modified
822     * to store the output packets (in place)
823     * index: pointer to packet indexes (modified)
824     * sz: size of each packet
825     */
826     int
827     fec_decode(struct fec_parms *code, gf *pkt[], int index[], int sz)
828     {
829     gf *m_dec ;
830     gf **new_pkt ;
831     int row, col , k = code->k ;
832    
833     if (GF_BITS > 8)
834     sz /= 2 ;
835    
836     if (shuffle(pkt, index, k)) /* error if true */
837     return 1 ;
838     m_dec = build_decode_matrix(code, pkt, index);
839    
840     if (m_dec == NULL)
841     return 1 ; /* error */
842     /*
843     * do the actual decoding
844     */
845     new_pkt = my_malloc (k * sizeof (gf * ), "new pkt pointers" );
846     for (row = 0 ; row < k ; row++ ) {
847     if (index[row] >= k) {
848     new_pkt[row] = my_malloc (sz * sizeof (gf), "new pkt buffer" );
849     bzero(new_pkt[row], sz * sizeof(gf) ) ;
850     for (col = 0 ; col < k ; col++ )
851     addmul(new_pkt[row], pkt[col], m_dec[row*k + col], sz) ;
852     }
853     }
854     /*
855     * move pkts to their final destination
856     */
857     for (row = 0 ; row < k ; row++ ) {
858     if (index[row] >= k) {
859     bcopy(new_pkt[row], pkt[row], sz*sizeof(gf));
860     free(new_pkt[row]);
861     index[row] = row;
862     }
863     }
864     free(new_pkt);
865     free(m_dec);
866    
867     return 0;
868     }
869    
870     /*********** end of FEC code -- beginning of test code ************/
871    
872     #if (TEST || DEBUG)
873     void
874     test_gf()
875     {
876     int i ;
877     /*
878     * test gf tables. Sufficiently tested...
879     */
880     for (i=0; i<= GF_SIZE; i++) {
881     if (gf_exp[gf_log[i]] != i)
882     fprintf(stderr, "bad exp/log i %d log %d exp(log) %d\n",
883     i, gf_log[i], gf_exp[gf_log[i]]);
884    
885     if (i != 0 && gf_mul(i, inverse[i]) != 1)
886     fprintf(stderr, "bad mul/inv i %d inv %d i*inv(i) %d\n",
887     i, inverse[i], gf_mul(i, inverse[i]) );
888     if (gf_mul(0,i) != 0)
889     fprintf(stderr, "bad mul table 0,%d\n",i);
890     if (gf_mul(i,0) != 0)
891     fprintf(stderr, "bad mul table %d,0\n",i);
892     }
893     }
894     #endif /* TEST */