erasure_code.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /**********************************************************************
  2. Copyright(c) 2011-2015 Intel Corporation All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Intel Corporation nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. **********************************************************************/
  27. #ifndef _ERASURE_CODE_H_
  28. #define _ERASURE_CODE_H_
  29. /**
  30. * @file erasure_code.h
  31. * @brief Interface to functions supporting erasure code encode and decode.
  32. *
  33. * This file defines the interface to optimized functions used in erasure
  34. * codes. Encode and decode of erasures in GF(2^8) are made by calculating the
  35. * dot product of the symbols (bytes in GF(2^8)) across a set of buffers and a
  36. * set of coefficients. Values for the coefficients are determined by the type
  37. * of erasure code. Using a general dot product means that any sequence of
  38. * coefficients may be used including erasure codes based on random
  39. * coefficients.
  40. * Multiple versions of dot product are supplied to calculate 1-6 output
  41. * vectors in one pass.
  42. * Base GF multiply and divide functions can be sped up by defining
  43. * GF_LARGE_TABLES at the expense of memory size.
  44. *
  45. */
  46. #include "gf_vect_mul.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**
  51. * @brief Initialize tables for fast Erasure Code encode and decode.
  52. *
  53. * Generates the expanded tables needed for fast encode or decode for erasure
  54. * codes on blocks of data. 32bytes is generated for each input coefficient.
  55. *
  56. * @param k The number of vector sources or rows in the generator matrix
  57. * for coding.
  58. * @param rows The number of output vectors to concurrently encode/decode.
  59. * @param a Pointer to sets of arrays of input coefficients used to encode
  60. * or decode data.
  61. * @param gftbls Pointer to start of space for concatenated output tables
  62. * generated from input coefficients. Must be of size 32*k*rows.
  63. * @returns none
  64. */
  65. void ec_init_tables(int k, int rows, unsigned char* a, unsigned char* gftbls);
  66. /**
  67. * @brief Generate or decode erasure codes on blocks of data, runs appropriate version.
  68. *
  69. * Given a list of source data blocks, generate one or multiple blocks of
  70. * encoded data as specified by a matrix of GF(2^8) coefficients. When given a
  71. * suitable set of coefficients, this function will perform the fast generation
  72. * or decoding of Reed-Solomon type erasure codes.
  73. *
  74. * This function determines what instruction sets are enabled and
  75. * selects the appropriate version at runtime.
  76. *
  77. * @param len Length of each block of data (vector) of source or dest data.
  78. * @param k The number of vector sources or rows in the generator matrix
  79. * for coding.
  80. * @param rows The number of output vectors to concurrently encode/decode.
  81. * @param gftbls Pointer to array of input tables generated from coding
  82. * coefficients in ec_init_tables(). Must be of size 32*k*rows
  83. * @param data Array of pointers to source input buffers.
  84. * @param coding Array of pointers to coded output buffers.
  85. * @returns none
  86. */
  87. void ec_encode_data(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
  88. unsigned char **coding);
  89. /**
  90. * @brief Generate or decode erasure codes on blocks of data, runs baseline version.
  91. *
  92. * Baseline version of ec_encode_data() with same parameters.
  93. */
  94. void ec_encode_data_base(int len, int srcs, int dests, unsigned char *v, unsigned char **src,
  95. unsigned char **dest);
  96. /**
  97. * @brief Generate update for encode or decode of erasure codes from single source, runs appropriate version.
  98. *
  99. * Given one source data block, update one or multiple blocks of encoded data as
  100. * specified by a matrix of GF(2^8) coefficients. When given a suitable set of
  101. * coefficients, this function will perform the fast generation or decoding of
  102. * Reed-Solomon type erasure codes from one input source at a time.
  103. *
  104. * This function determines what instruction sets are enabled and selects the
  105. * appropriate version at runtime.
  106. *
  107. * @param len Length of each block of data (vector) of source or dest data.
  108. * @param k The number of vector sources or rows in the generator matrix
  109. * for coding.
  110. * @param rows The number of output vectors to concurrently encode/decode.
  111. * @param vec_i The vector index corresponding to the single input source.
  112. * @param g_tbls Pointer to array of input tables generated from coding
  113. * coefficients in ec_init_tables(). Must be of size 32*k*rows
  114. * @param data Pointer to single input source used to update output parity.
  115. * @param coding Array of pointers to coded output buffers.
  116. * @returns none
  117. */
  118. void ec_encode_data_update(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
  119. unsigned char *data, unsigned char **coding);
  120. /**
  121. * @brief Generate update for encode or decode of erasure codes from single source.
  122. *
  123. * Baseline version of ec_encode_data_update().
  124. */
  125. void ec_encode_data_update_base(int len, int k, int rows, int vec_i, unsigned char *v,
  126. unsigned char *data, unsigned char **dest);
  127. /**
  128. * @brief GF(2^8) vector dot product, runs baseline version.
  129. *
  130. * Does a GF(2^8) dot product across each byte of the input array and a constant
  131. * set of coefficients to produce each byte of the output. Can be used for
  132. * erasure coding encode and decode. Function requires pre-calculation of a
  133. * 32*vlen byte constant array based on the input coefficients.
  134. *
  135. * @param len Length of each vector in bytes. Must be >= 16.
  136. * @param vlen Number of vector sources.
  137. * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
  138. * on the array of input coefficients. Only elements 32*CONST*j + 1
  139. * of this array are used, where j = (0, 1, 2...) and CONST is the
  140. * number of elements in the array of input coefficients. The
  141. * elements used correspond to the original input coefficients.
  142. * @param src Array of pointers to source inputs.
  143. * @param dest Pointer to destination data array.
  144. * @returns none
  145. */
  146. void gf_vect_dot_prod_base(int len, int vlen, unsigned char *gftbls,
  147. unsigned char **src, unsigned char *dest);
  148. /**
  149. * @brief GF(2^8) vector dot product, runs appropriate version.
  150. *
  151. * Does a GF(2^8) dot product across each byte of the input array and a constant
  152. * set of coefficients to produce each byte of the output. Can be used for
  153. * erasure coding encode and decode. Function requires pre-calculation of a
  154. * 32*vlen byte constant array based on the input coefficients.
  155. *
  156. * This function determines what instruction sets are enabled and
  157. * selects the appropriate version at runtime.
  158. *
  159. * @param len Length of each vector in bytes. Must be >= 32.
  160. * @param vlen Number of vector sources.
  161. * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
  162. * on the array of input coefficients.
  163. * @param src Array of pointers to source inputs.
  164. * @param dest Pointer to destination data array.
  165. * @returns none
  166. */
  167. void gf_vect_dot_prod(int len, int vlen, unsigned char *gftbls,
  168. unsigned char **src, unsigned char *dest);
  169. /**
  170. * @brief GF(2^8) vector multiply accumulate, runs appropriate version.
  171. *
  172. * Does a GF(2^8) multiply across each byte of input source with expanded
  173. * constant and add to destination array. Can be used for erasure coding encode
  174. * and decode update when only one source is available at a time. Function
  175. * requires pre-calculation of a 32*vec byte constant array based on the input
  176. * coefficients.
  177. *
  178. * This function determines what instruction sets are enabled and selects the
  179. * appropriate version at runtime.
  180. *
  181. * @param len Length of each vector in bytes. Must be >= 64.
  182. * @param vec The number of vector sources or rows in the generator matrix
  183. * for coding.
  184. * @param vec_i The vector index corresponding to the single input source.
  185. * @param gftbls Pointer to array of input tables generated from coding
  186. * coefficients in ec_init_tables(). Must be of size 32*vec.
  187. * @param src Array of pointers to source inputs.
  188. * @param dest Pointer to destination data array.
  189. * @returns none
  190. */
  191. void gf_vect_mad(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  192. unsigned char *dest);
  193. /**
  194. * @brief GF(2^8) vector multiply accumulate, baseline version.
  195. *
  196. * Baseline version of gf_vect_mad() with same parameters.
  197. */
  198. void gf_vect_mad_base(int len, int vec, int vec_i, unsigned char *v, unsigned char *src,
  199. unsigned char *dest);
  200. // x86 only
  201. #if defined(__i386__) || defined(__x86_64__)
  202. /**
  203. * @brief Generate or decode erasure codes on blocks of data.
  204. *
  205. * Arch specific version of ec_encode_data() with same parameters.
  206. * @requires SSE4.1
  207. */
  208. void ec_encode_data_sse(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
  209. unsigned char **coding);
  210. /**
  211. * @brief Generate or decode erasure codes on blocks of data.
  212. *
  213. * Arch specific version of ec_encode_data() with same parameters.
  214. * @requires AVX
  215. */
  216. void ec_encode_data_avx(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
  217. unsigned char **coding);
  218. /**
  219. * @brief Generate or decode erasure codes on blocks of data.
  220. *
  221. * Arch specific version of ec_encode_data() with same parameters.
  222. * @requires AVX2
  223. */
  224. void ec_encode_data_avx2(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
  225. unsigned char **coding);
  226. /**
  227. * @brief Generate update for encode or decode of erasure codes from single source.
  228. *
  229. * Arch specific version of ec_encode_data_update() with same parameters.
  230. * @requires SSE4.1
  231. */
  232. void ec_encode_data_update_sse(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
  233. unsigned char *data, unsigned char **coding);
  234. /**
  235. * @brief Generate update for encode or decode of erasure codes from single source.
  236. *
  237. * Arch specific version of ec_encode_data_update() with same parameters.
  238. * @requires AVX
  239. */
  240. void ec_encode_data_update_avx(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
  241. unsigned char *data, unsigned char **coding);
  242. /**
  243. * @brief Generate update for encode or decode of erasure codes from single source.
  244. *
  245. * Arch specific version of ec_encode_data_update() with same parameters.
  246. * @requires AVX2
  247. */
  248. void ec_encode_data_update_avx2(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
  249. unsigned char *data, unsigned char **coding);
  250. /**
  251. * @brief GF(2^8) vector dot product.
  252. *
  253. * Does a GF(2^8) dot product across each byte of the input array and a constant
  254. * set of coefficients to produce each byte of the output. Can be used for
  255. * erasure coding encode and decode. Function requires pre-calculation of a
  256. * 32*vlen byte constant array based on the input coefficients.
  257. * @requires SSE4.1
  258. *
  259. * @param len Length of each vector in bytes. Must be >= 16.
  260. * @param vlen Number of vector sources.
  261. * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
  262. * on the array of input coefficients.
  263. * @param src Array of pointers to source inputs.
  264. * @param dest Pointer to destination data array.
  265. * @returns none
  266. */
  267. void gf_vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
  268. unsigned char **src, unsigned char *dest);
  269. /**
  270. * @brief GF(2^8) vector dot product.
  271. *
  272. * Does a GF(2^8) dot product across each byte of the input array and a constant
  273. * set of coefficients to produce each byte of the output. Can be used for
  274. * erasure coding encode and decode. Function requires pre-calculation of a
  275. * 32*vlen byte constant array based on the input coefficients.
  276. * @requires AVX
  277. *
  278. * @param len Length of each vector in bytes. Must be >= 16.
  279. * @param vlen Number of vector sources.
  280. * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
  281. * on the array of input coefficients.
  282. * @param src Array of pointers to source inputs.
  283. * @param dest Pointer to destination data array.
  284. * @returns none
  285. */
  286. void gf_vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
  287. unsigned char **src, unsigned char *dest);
  288. /**
  289. * @brief GF(2^8) vector dot product.
  290. *
  291. * Does a GF(2^8) dot product across each byte of the input array and a constant
  292. * set of coefficients to produce each byte of the output. Can be used for
  293. * erasure coding encode and decode. Function requires pre-calculation of a
  294. * 32*vlen byte constant array based on the input coefficients.
  295. * @requires AVX2
  296. *
  297. * @param len Length of each vector in bytes. Must be >= 32.
  298. * @param vlen Number of vector sources.
  299. * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
  300. * on the array of input coefficients.
  301. * @param src Array of pointers to source inputs.
  302. * @param dest Pointer to destination data array.
  303. * @returns none
  304. */
  305. void gf_vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
  306. unsigned char **src, unsigned char *dest);
  307. /**
  308. * @brief GF(2^8) vector dot product with two outputs.
  309. *
  310. * Vector dot product optimized to calculate two outputs at a time. Does two
  311. * GF(2^8) dot products across each byte of the input array and two constant
  312. * sets of coefficients to produce each byte of the outputs. Can be used for
  313. * erasure coding encode and decode. Function requires pre-calculation of a
  314. * 2*32*vlen byte constant array based on the two sets of input coefficients.
  315. * @requires SSE4.1
  316. *
  317. * @param len Length of each vector in bytes. Must be >= 16.
  318. * @param vlen Number of vector sources.
  319. * @param gftbls Pointer to 2*32*vlen byte array of pre-calculated constants
  320. * based on the array of input coefficients.
  321. * @param src Array of pointers to source inputs.
  322. * @param dest Array of pointers to destination data buffers.
  323. * @returns none
  324. */
  325. void gf_2vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
  326. unsigned char **src, unsigned char **dest);
  327. /**
  328. * @brief GF(2^8) vector dot product with two outputs.
  329. *
  330. * Vector dot product optimized to calculate two outputs at a time. Does two
  331. * GF(2^8) dot products across each byte of the input array and two constant
  332. * sets of coefficients to produce each byte of the outputs. Can be used for
  333. * erasure coding encode and decode. Function requires pre-calculation of a
  334. * 2*32*vlen byte constant array based on the two sets of input coefficients.
  335. * @requires AVX
  336. *
  337. * @param len Length of each vector in bytes. Must be >= 16.
  338. * @param vlen Number of vector sources.
  339. * @param gftbls Pointer to 2*32*vlen byte array of pre-calculated constants
  340. * based on the array of input coefficients.
  341. * @param src Array of pointers to source inputs.
  342. * @param dest Array of pointers to destination data buffers.
  343. * @returns none
  344. */
  345. void gf_2vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
  346. unsigned char **src, unsigned char **dest);
  347. /**
  348. * @brief GF(2^8) vector dot product with two outputs.
  349. *
  350. * Vector dot product optimized to calculate two outputs at a time. Does two
  351. * GF(2^8) dot products across each byte of the input array and two constant
  352. * sets of coefficients to produce each byte of the outputs. Can be used for
  353. * erasure coding encode and decode. Function requires pre-calculation of a
  354. * 2*32*vlen byte constant array based on the two sets of input coefficients.
  355. * @requires AVX2
  356. *
  357. * @param len Length of each vector in bytes. Must be >= 32.
  358. * @param vlen Number of vector sources.
  359. * @param gftbls Pointer to 2*32*vlen byte array of pre-calculated constants
  360. * based on the array of input coefficients.
  361. * @param src Array of pointers to source inputs.
  362. * @param dest Array of pointers to destination data buffers.
  363. * @returns none
  364. */
  365. void gf_2vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
  366. unsigned char **src, unsigned char **dest);
  367. /**
  368. * @brief GF(2^8) vector dot product with three outputs.
  369. *
  370. * Vector dot product optimized to calculate three outputs at a time. Does three
  371. * GF(2^8) dot products across each byte of the input array and three constant
  372. * sets of coefficients to produce each byte of the outputs. Can be used for
  373. * erasure coding encode and decode. Function requires pre-calculation of a
  374. * 3*32*vlen byte constant array based on the three sets of input coefficients.
  375. * @requires SSE4.1
  376. *
  377. * @param len Length of each vector in bytes. Must be >= 16.
  378. * @param vlen Number of vector sources.
  379. * @param gftbls Pointer to 3*32*vlen byte array of pre-calculated constants
  380. * based on the array of input coefficients.
  381. * @param src Array of pointers to source inputs.
  382. * @param dest Array of pointers to destination data buffers.
  383. * @returns none
  384. */
  385. void gf_3vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
  386. unsigned char **src, unsigned char **dest);
  387. /**
  388. * @brief GF(2^8) vector dot product with three outputs.
  389. *
  390. * Vector dot product optimized to calculate three outputs at a time. Does three
  391. * GF(2^8) dot products across each byte of the input array and three constant
  392. * sets of coefficients to produce each byte of the outputs. Can be used for
  393. * erasure coding encode and decode. Function requires pre-calculation of a
  394. * 3*32*vlen byte constant array based on the three sets of input coefficients.
  395. * @requires AVX
  396. *
  397. * @param len Length of each vector in bytes. Must be >= 16.
  398. * @param vlen Number of vector sources.
  399. * @param gftbls Pointer to 3*32*vlen byte array of pre-calculated constants
  400. * based on the array of input coefficients.
  401. * @param src Array of pointers to source inputs.
  402. * @param dest Array of pointers to destination data buffers.
  403. * @returns none
  404. */
  405. void gf_3vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
  406. unsigned char **src, unsigned char **dest);
  407. /**
  408. * @brief GF(2^8) vector dot product with three outputs.
  409. *
  410. * Vector dot product optimized to calculate three outputs at a time. Does three
  411. * GF(2^8) dot products across each byte of the input array and three constant
  412. * sets of coefficients to produce each byte of the outputs. Can be used for
  413. * erasure coding encode and decode. Function requires pre-calculation of a
  414. * 3*32*vlen byte constant array based on the three sets of input coefficients.
  415. * @requires AVX2
  416. *
  417. * @param len Length of each vector in bytes. Must be >= 32.
  418. * @param vlen Number of vector sources.
  419. * @param gftbls Pointer to 3*32*vlen byte array of pre-calculated constants
  420. * based on the array of input coefficients.
  421. * @param src Array of pointers to source inputs.
  422. * @param dest Array of pointers to destination data buffers.
  423. * @returns none
  424. */
  425. void gf_3vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
  426. unsigned char **src, unsigned char **dest);
  427. /**
  428. * @brief GF(2^8) vector dot product with four outputs.
  429. *
  430. * Vector dot product optimized to calculate four outputs at a time. Does four
  431. * GF(2^8) dot products across each byte of the input array and four constant
  432. * sets of coefficients to produce each byte of the outputs. Can be used for
  433. * erasure coding encode and decode. Function requires pre-calculation of a
  434. * 4*32*vlen byte constant array based on the four sets of input coefficients.
  435. * @requires SSE4.1
  436. *
  437. * @param len Length of each vector in bytes. Must be >= 16.
  438. * @param vlen Number of vector sources.
  439. * @param gftbls Pointer to 4*32*vlen byte array of pre-calculated constants
  440. * based on the array of input coefficients.
  441. * @param src Array of pointers to source inputs.
  442. * @param dest Array of pointers to destination data buffers.
  443. * @returns none
  444. */
  445. void gf_4vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
  446. unsigned char **src, unsigned char **dest);
  447. /**
  448. * @brief GF(2^8) vector dot product with four outputs.
  449. *
  450. * Vector dot product optimized to calculate four outputs at a time. Does four
  451. * GF(2^8) dot products across each byte of the input array and four constant
  452. * sets of coefficients to produce each byte of the outputs. Can be used for
  453. * erasure coding encode and decode. Function requires pre-calculation of a
  454. * 4*32*vlen byte constant array based on the four sets of input coefficients.
  455. * @requires AVX
  456. *
  457. * @param len Length of each vector in bytes. Must be >= 16.
  458. * @param vlen Number of vector sources.
  459. * @param gftbls Pointer to 4*32*vlen byte array of pre-calculated constants
  460. * based on the array of input coefficients.
  461. * @param src Array of pointers to source inputs.
  462. * @param dest Array of pointers to destination data buffers.
  463. * @returns none
  464. */
  465. void gf_4vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
  466. unsigned char **src, unsigned char **dest);
  467. /**
  468. * @brief GF(2^8) vector dot product with four outputs.
  469. *
  470. * Vector dot product optimized to calculate four outputs at a time. Does four
  471. * GF(2^8) dot products across each byte of the input array and four constant
  472. * sets of coefficients to produce each byte of the outputs. Can be used for
  473. * erasure coding encode and decode. Function requires pre-calculation of a
  474. * 4*32*vlen byte constant array based on the four sets of input coefficients.
  475. * @requires AVX2
  476. *
  477. * @param len Length of each vector in bytes. Must be >= 32.
  478. * @param vlen Number of vector sources.
  479. * @param gftbls Pointer to 4*32*vlen byte array of pre-calculated constants
  480. * based on the array of input coefficients.
  481. * @param src Array of pointers to source inputs.
  482. * @param dest Array of pointers to destination data buffers.
  483. * @returns none
  484. */
  485. void gf_4vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
  486. unsigned char **src, unsigned char **dest);
  487. /**
  488. * @brief GF(2^8) vector dot product with five outputs.
  489. *
  490. * Vector dot product optimized to calculate five outputs at a time. Does five
  491. * GF(2^8) dot products across each byte of the input array and five constant
  492. * sets of coefficients to produce each byte of the outputs. Can be used for
  493. * erasure coding encode and decode. Function requires pre-calculation of a
  494. * 5*32*vlen byte constant array based on the five sets of input coefficients.
  495. * @requires SSE4.1
  496. *
  497. * @param len Length of each vector in bytes. Must >= 16.
  498. * @param vlen Number of vector sources.
  499. * @param gftbls Pointer to 5*32*vlen byte array of pre-calculated constants
  500. * based on the array of input coefficients.
  501. * @param src Array of pointers to source inputs.
  502. * @param dest Array of pointers to destination data buffers.
  503. * @returns none
  504. */
  505. void gf_5vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
  506. unsigned char **src, unsigned char **dest);
  507. /**
  508. * @brief GF(2^8) vector dot product with five outputs.
  509. *
  510. * Vector dot product optimized to calculate five outputs at a time. Does five
  511. * GF(2^8) dot products across each byte of the input array and five constant
  512. * sets of coefficients to produce each byte of the outputs. Can be used for
  513. * erasure coding encode and decode. Function requires pre-calculation of a
  514. * 5*32*vlen byte constant array based on the five sets of input coefficients.
  515. * @requires AVX
  516. *
  517. * @param len Length of each vector in bytes. Must >= 16.
  518. * @param vlen Number of vector sources.
  519. * @param gftbls Pointer to 5*32*vlen byte array of pre-calculated constants
  520. * based on the array of input coefficients.
  521. * @param src Array of pointers to source inputs.
  522. * @param dest Array of pointers to destination data buffers.
  523. * @returns none
  524. */
  525. void gf_5vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
  526. unsigned char **src, unsigned char **dest);
  527. /**
  528. * @brief GF(2^8) vector dot product with five outputs.
  529. *
  530. * Vector dot product optimized to calculate five outputs at a time. Does five
  531. * GF(2^8) dot products across each byte of the input array and five constant
  532. * sets of coefficients to produce each byte of the outputs. Can be used for
  533. * erasure coding encode and decode. Function requires pre-calculation of a
  534. * 5*32*vlen byte constant array based on the five sets of input coefficients.
  535. * @requires AVX2
  536. *
  537. * @param len Length of each vector in bytes. Must >= 32.
  538. * @param vlen Number of vector sources.
  539. * @param gftbls Pointer to 5*32*vlen byte array of pre-calculated constants
  540. * based on the array of input coefficients.
  541. * @param src Array of pointers to source inputs.
  542. * @param dest Array of pointers to destination data buffers.
  543. * @returns none
  544. */
  545. void gf_5vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
  546. unsigned char **src, unsigned char **dest);
  547. /**
  548. * @brief GF(2^8) vector dot product with six outputs.
  549. *
  550. * Vector dot product optimized to calculate six outputs at a time. Does six
  551. * GF(2^8) dot products across each byte of the input array and six constant
  552. * sets of coefficients to produce each byte of the outputs. Can be used for
  553. * erasure coding encode and decode. Function requires pre-calculation of a
  554. * 6*32*vlen byte constant array based on the six sets of input coefficients.
  555. * @requires SSE4.1
  556. *
  557. * @param len Length of each vector in bytes. Must be >= 16.
  558. * @param vlen Number of vector sources.
  559. * @param gftbls Pointer to 6*32*vlen byte array of pre-calculated constants
  560. * based on the array of input coefficients.
  561. * @param src Array of pointers to source inputs.
  562. * @param dest Array of pointers to destination data buffers.
  563. * @returns none
  564. */
  565. void gf_6vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
  566. unsigned char **src, unsigned char **dest);
  567. /**
  568. * @brief GF(2^8) vector dot product with six outputs.
  569. *
  570. * Vector dot product optimized to calculate six outputs at a time. Does six
  571. * GF(2^8) dot products across each byte of the input array and six constant
  572. * sets of coefficients to produce each byte of the outputs. Can be used for
  573. * erasure coding encode and decode. Function requires pre-calculation of a
  574. * 6*32*vlen byte constant array based on the six sets of input coefficients.
  575. * @requires AVX
  576. *
  577. * @param len Length of each vector in bytes. Must be >= 16.
  578. * @param vlen Number of vector sources.
  579. * @param gftbls Pointer to 6*32*vlen byte array of pre-calculated constants
  580. * based on the array of input coefficients.
  581. * @param src Array of pointers to source inputs.
  582. * @param dest Array of pointers to destination data buffers.
  583. * @returns none
  584. */
  585. void gf_6vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
  586. unsigned char **src, unsigned char **dest);
  587. /**
  588. * @brief GF(2^8) vector dot product with six outputs.
  589. *
  590. * Vector dot product optimized to calculate six outputs at a time. Does six
  591. * GF(2^8) dot products across each byte of the input array and six constant
  592. * sets of coefficients to produce each byte of the outputs. Can be used for
  593. * erasure coding encode and decode. Function requires pre-calculation of a
  594. * 6*32*vlen byte constant array based on the six sets of input coefficients.
  595. * @requires AVX2
  596. *
  597. * @param len Length of each vector in bytes. Must be >= 32.
  598. * @param vlen Number of vector sources.
  599. * @param gftbls Pointer to 6*32*vlen byte array of pre-calculated constants
  600. * based on the array of input coefficients.
  601. * @param src Array of pointers to source inputs.
  602. * @param dest Array of pointers to destination data buffers.
  603. * @returns none
  604. */
  605. void gf_6vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
  606. unsigned char **src, unsigned char **dest);
  607. /**
  608. * @brief GF(2^8) vector multiply accumulate, arch specific version.
  609. *
  610. * Arch specific version of gf_vect_mad() with same parameters.
  611. * @requires SSE4.1
  612. */
  613. void gf_vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  614. unsigned char *dest);
  615. /**
  616. * @brief GF(2^8) vector multiply accumulate, arch specific version.
  617. *
  618. * Arch specific version of gf_vect_mad() with same parameters.
  619. * @requires AVX
  620. */
  621. void gf_vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  622. unsigned char *dest);
  623. /**
  624. * @brief GF(2^8) vector multiply accumulate, arch specific version.
  625. *
  626. * Arch specific version of gf_vect_mad() with same parameters.
  627. * @requires AVX2
  628. */
  629. void gf_vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  630. unsigned char *dest);
  631. /**
  632. * @brief GF(2^8) vector multiply with 2 accumulate. SSE version.
  633. *
  634. * Does a GF(2^8) multiply across each byte of input source with expanded
  635. * constants and add to destination arrays. Can be used for erasure coding
  636. * encode and decode update when only one source is available at a
  637. * time. Function requires pre-calculation of a 32*vec byte constant array based
  638. * on the input coefficients.
  639. * @requires SSE4.1
  640. *
  641. * @param len Length of each vector in bytes. Must be >= 32.
  642. * @param vec The number of vector sources or rows in the generator matrix
  643. * for coding.
  644. * @param vec_i The vector index corresponding to the single input source.
  645. * @param gftbls Pointer to array of input tables generated from coding
  646. * coefficients in ec_init_tables(). Must be of size 32*vec.
  647. * @param src Pointer to source input array.
  648. * @param dest Array of pointers to destination input/outputs.
  649. * @returns none
  650. */
  651. void gf_2vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  652. unsigned char **dest);
  653. /**
  654. * @brief GF(2^8) vector multiply with 2 accumulate. AVX version of gf_2vect_mad_sse().
  655. * @requires AVX
  656. */
  657. void gf_2vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  658. unsigned char **dest);
  659. /**
  660. * @brief GF(2^8) vector multiply with 2 accumulate. AVX2 version of gf_2vect_mad_sse().
  661. * @requires AVX2
  662. */
  663. void gf_2vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  664. unsigned char **dest);
  665. /**
  666. * @brief GF(2^8) vector multiply with 3 accumulate. SSE version.
  667. *
  668. * Does a GF(2^8) multiply across each byte of input source with expanded
  669. * constants and add to destination arrays. Can be used for erasure coding
  670. * encode and decode update when only one source is available at a
  671. * time. Function requires pre-calculation of a 32*vec byte constant array based
  672. * on the input coefficients.
  673. * @requires SSE4.1
  674. *
  675. * @param len Length of each vector in bytes. Must be >= 32.
  676. * @param vec The number of vector sources or rows in the generator matrix
  677. * for coding.
  678. * @param vec_i The vector index corresponding to the single input source.
  679. * @param gftbls Pointer to array of input tables generated from coding
  680. * coefficients in ec_init_tables(). Must be of size 32*vec.
  681. * @param src Pointer to source input array.
  682. * @param dest Array of pointers to destination input/outputs.
  683. * @returns none
  684. */
  685. void gf_3vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  686. unsigned char **dest);
  687. /**
  688. * @brief GF(2^8) vector multiply with 3 accumulate. AVX version of gf_3vect_mad_sse().
  689. * @requires AVX
  690. */
  691. void gf_3vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  692. unsigned char **dest);
  693. /**
  694. * @brief GF(2^8) vector multiply with 3 accumulate. AVX2 version of gf_3vect_mad_sse().
  695. * @requires AVX2
  696. */
  697. void gf_3vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  698. unsigned char **dest);
  699. /**
  700. * @brief GF(2^8) vector multiply with 4 accumulate. SSE version.
  701. *
  702. * Does a GF(2^8) multiply across each byte of input source with expanded
  703. * constants and add to destination arrays. Can be used for erasure coding
  704. * encode and decode update when only one source is available at a
  705. * time. Function requires pre-calculation of a 32*vec byte constant array based
  706. * on the input coefficients.
  707. * @requires SSE4.1
  708. *
  709. * @param len Length of each vector in bytes. Must be >= 32.
  710. * @param vec The number of vector sources or rows in the generator matrix
  711. * for coding.
  712. * @param vec_i The vector index corresponding to the single input source.
  713. * @param gftbls Pointer to array of input tables generated from coding
  714. * coefficients in ec_init_tables(). Must be of size 32*vec.
  715. * @param src Pointer to source input array.
  716. * @param dest Array of pointers to destination input/outputs.
  717. * @returns none
  718. */
  719. void gf_4vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  720. unsigned char **dest);
  721. /**
  722. * @brief GF(2^8) vector multiply with 4 accumulate. AVX version of gf_4vect_mad_sse().
  723. * @requires AVX
  724. */
  725. void gf_4vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  726. unsigned char **dest);
  727. /**
  728. * @brief GF(2^8) vector multiply with 4 accumulate. AVX2 version of gf_4vect_mad_sse().
  729. * @requires AVX2
  730. */
  731. void gf_4vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  732. unsigned char **dest);
  733. /**
  734. * @brief GF(2^8) vector multiply with 5 accumulate. SSE version.
  735. * @requires SSE4.1
  736. */
  737. void gf_5vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  738. unsigned char **dest);
  739. /**
  740. * @brief GF(2^8) vector multiply with 5 accumulate. AVX version.
  741. * @requires AVX
  742. */
  743. void gf_5vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  744. unsigned char **dest);
  745. /**
  746. * @brief GF(2^8) vector multiply with 5 accumulate. AVX2 version.
  747. * @requires AVX2
  748. */
  749. void gf_5vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  750. unsigned char **dest);
  751. /**
  752. * @brief GF(2^8) vector multiply with 6 accumulate. SSE version.
  753. * @requires SSE4.1
  754. */
  755. void gf_6vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  756. unsigned char **dest);
  757. /**
  758. * @brief GF(2^8) vector multiply with 6 accumulate. AVX version.
  759. * @requires AVX
  760. */
  761. void gf_6vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  762. unsigned char **dest);
  763. /**
  764. * @brief GF(2^8) vector multiply with 6 accumulate. AVX2 version.
  765. * @requires AVX2
  766. */
  767. void gf_6vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
  768. unsigned char **dest);
  769. #endif
  770. /**********************************************************************
  771. * The remaining are lib support functions used in GF(2^8) operations.
  772. */
  773. /**
  774. * @brief Single element GF(2^8) multiply.
  775. *
  776. * @param a Multiplicand a
  777. * @param b Multiplicand b
  778. * @returns Product of a and b in GF(2^8)
  779. */
  780. unsigned char gf_mul_erasure(unsigned char a, unsigned char b);
  781. /**
  782. * @brief Single element GF(2^8) inverse.
  783. *
  784. * @param a Input element
  785. * @returns Field element b such that a x b = {1}
  786. */
  787. unsigned char gf_inv(unsigned char a);
  788. /**
  789. * @brief Generate a matrix of coefficients to be used for encoding.
  790. *
  791. * Vandermonde matrix example of encoding coefficients where high portion of
  792. * matrix is identity matrix I and lower portion is constructed as 2^{i*(j-k+1)}
  793. * i:{0,k-1} j:{k,m-1}. Commonly used method for choosing coefficients in
  794. * erasure encoding but does not guarantee invertable for every sub matrix. For
  795. * large pairs of m and k it is possible to find cases where the decode matrix
  796. * chosen from sources and parity is not invertable. Users may want to adjust
  797. * for certain pairs m and k. If m and k satisfy one of the following
  798. * inequalities, no adjustment is required:
  799. *
  800. * - k <= 3
  801. * - k = 4, m <= 25
  802. * - k = 5, m <= 10
  803. * - k <= 21, m-k = 4
  804. * - m - k <= 3.
  805. *
  806. * @param a [m x k] array to hold coefficients
  807. * @param m number of rows in matrix corresponding to srcs + parity.
  808. * @param k number of columns in matrix corresponding to srcs.
  809. * @returns none
  810. */
  811. void gf_gen_rs_matrix(unsigned char *a, int m, int k);
  812. /**
  813. * @brief Generate a Cauchy matrix of coefficients to be used for encoding.
  814. *
  815. * Cauchy matrix example of encoding coefficients where high portion of matrix
  816. * is identity matrix I and lower portion is constructed as 1/(i + j) | i != j,
  817. * i:{0,k-1} j:{k,m-1}. Any sub-matrix of a Cauchy matrix should be invertable.
  818. *
  819. * @param a [m x k] array to hold coefficients
  820. * @param m number of rows in matrix corresponding to srcs + parity.
  821. * @param k number of columns in matrix corresponding to srcs.
  822. * @returns none
  823. */
  824. void gf_gen_cauchy1_matrix(unsigned char *a, int m, int k);
  825. /**
  826. * @brief Invert a matrix in GF(2^8)
  827. *
  828. * @param in input matrix
  829. * @param out output matrix such that [in] x [out] = [I] - identity matrix
  830. * @param n size of matrix [nxn]
  831. * @returns 0 successful, other fail on singular input matrix
  832. */
  833. int gf_invert_matrix(unsigned char *in, unsigned char *out, const int n);
  834. /*************************************************************/
  835. #ifdef __cplusplus
  836. }
  837. #endif
  838. #endif //_ERASURE_CODE_H_