rs.c 29 KB

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