fec.cpp 24 KB

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