aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/isa-l/include/erasure_code.h
blob: 04fdfb1bc21b6aa88072c4fd1a6d514b95535c75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/**********************************************************************
  Copyright(c) 2011-2015 Intel Corporation All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.
    * Neither the name of Intel Corporation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************/


#ifndef _ERASURE_CODE_H_
#define _ERASURE_CODE_H_

/**
 *  @file erasure_code.h
 *  @brief Interface to functions supporting erasure code encode and decode.
 *
 *  This file defines the interface to optimized functions used in erasure
 *  codes.  Encode and decode of erasures in GF(2^8) are made by calculating the
 *  dot product of the symbols (bytes in GF(2^8)) across a set of buffers and a
 *  set of coefficients.  Values for the coefficients are determined by the type
 *  of erasure code.  Using a general dot product means that any sequence of
 *  coefficients may be used including erasure codes based on random
 *  coefficients.
 *  Multiple versions of dot product are supplied to calculate 1-6 output
 *  vectors in one pass.
 *  Base GF multiply and divide functions can be sped up by defining
 *  GF_LARGE_TABLES at the expense of memory size.
 *
 */

#include "gf_vect_mul.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Initialize tables for fast Erasure Code encode and decode.
 *
 * Generates the expanded tables needed for fast encode or decode for erasure
 * codes on blocks of data.  32bytes is generated for each input coefficient.
 *
 * @param k      The number of vector sources or rows in the generator matrix
 *               for coding.
 * @param rows   The number of output vectors to concurrently encode/decode.
 * @param a      Pointer to sets of arrays of input coefficients used to encode
 *               or decode data.
 * @param gftbls Pointer to start of space for concatenated output tables
 *               generated from input coefficients.  Must be of size 32*k*rows.
 * @returns none
 */

void ec_init_tables(int k, int rows, unsigned char* a, unsigned char* gftbls);

/**
 * @brief Generate or decode erasure codes on blocks of data, runs appropriate version.
 *
 * Given a list of source data blocks, generate one or multiple blocks of
 * encoded data as specified by a matrix of GF(2^8) coefficients. When given a
 * suitable set of coefficients, this function will perform the fast generation
 * or decoding of Reed-Solomon type erasure codes.
 *
 * This function determines what instruction sets are enabled and
 * selects the appropriate version at runtime.
 *
 * @param len    Length of each block of data (vector) of source or dest data.
 * @param k      The number of vector sources or rows in the generator matrix
 * 		 for coding.
 * @param rows   The number of output vectors to concurrently encode/decode.
 * @param gftbls Pointer to array of input tables generated from coding
 * 		 coefficients in ec_init_tables(). Must be of size 32*k*rows
 * @param data   Array of pointers to source input buffers.
 * @param coding Array of pointers to coded output buffers.
 * @returns none
 */

void ec_encode_data(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
		    unsigned char **coding);

/**
 * @brief Generate or decode erasure codes on blocks of data, runs baseline version.
 *
 * Baseline version of ec_encode_data() with same parameters.
 */
void ec_encode_data_base(int len, int srcs, int dests, unsigned char *v, unsigned char **src,
			 unsigned char **dest);

/**
 * @brief Generate update for encode or decode of erasure codes from single source, runs appropriate version.
 *
 * Given one source data block, update one or multiple blocks of encoded data as
 * specified by a matrix of GF(2^8) coefficients. When given a suitable set of
 * coefficients, this function will perform the fast generation or decoding of
 * Reed-Solomon type erasure codes from one input source at a time.
 *
 * This function determines what instruction sets are enabled and selects the
 * appropriate version at runtime.
 *
 * @param len    Length of each block of data (vector) of source or dest data.
 * @param k      The number of vector sources or rows in the generator matrix
 * 		 for coding.
 * @param rows   The number of output vectors to concurrently encode/decode.
 * @param vec_i  The vector index corresponding to the single input source.
 * @param g_tbls Pointer to array of input tables generated from coding
 * 		 coefficients in ec_init_tables(). Must be of size 32*k*rows
 * @param data   Pointer to single input source used to update output parity.
 * @param coding Array of pointers to coded output buffers.
 * @returns none
 */
void ec_encode_data_update(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
			   unsigned char *data, unsigned char **coding);

/**
 * @brief Generate update for encode or decode of erasure codes from single source.
 *
 * Baseline version of ec_encode_data_update().
 */

void ec_encode_data_update_base(int len, int k, int rows, int vec_i, unsigned char *v,
				unsigned char *data, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product, runs baseline version.
 *
 * Does a GF(2^8) dot product across each byte of the input array and a constant
 * set of coefficients to produce each byte of the output. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 32*vlen byte constant array based on the input coefficients.
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
 *               on the array of input coefficients. Only elements 32*CONST*j + 1
 *               of this array are used, where j = (0, 1, 2...) and CONST is the
 *               number of elements in the array of input coefficients. The
 *               elements used correspond to the original input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Pointer to destination data array.
 * @returns none
 */


void gf_vect_dot_prod_base(int len, int vlen, unsigned char *gftbls,
                        unsigned char **src, unsigned char *dest);

/**
 * @brief GF(2^8) vector dot product, runs appropriate version.
 *
 * Does a GF(2^8) dot product across each byte of the input array and a constant
 * set of coefficients to produce each byte of the output. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 32*vlen byte constant array based on the input coefficients.
 *
 * This function determines what instruction sets are enabled and
 * selects the appropriate version at runtime.
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
 *               on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Pointer to destination data array.
 * @returns none
 */

void gf_vect_dot_prod(int len, int vlen, unsigned char *gftbls,
                        unsigned char **src, unsigned char *dest);

/**
 * @brief GF(2^8) vector multiply accumulate, runs appropriate version.
 *
 * Does a GF(2^8) multiply across each byte of input source with expanded
 * constant and add to destination array. Can be used for erasure coding encode
 * and decode update when only one source is available at a time. Function
 * requires pre-calculation of a 32*vec byte constant array based on the input
 * coefficients.
 *
 * This function determines what instruction sets are enabled and selects the
 * appropriate version at runtime.
 *
 * @param len    Length of each vector in bytes. Must be >= 64.
 * @param vec    The number of vector sources or rows in the generator matrix
 * 		 for coding.
 * @param vec_i  The vector index corresponding to the single input source.
 * @param gftbls Pointer to array of input tables generated from coding
 * 		 coefficients in ec_init_tables(). Must be of size 32*vec.
 * @param src    Array of pointers to source inputs.
 * @param dest   Pointer to destination data array.
 * @returns none
 */

void gf_vect_mad(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		 unsigned char *dest);

/**
 * @brief GF(2^8) vector multiply accumulate, baseline version.
 *
 * Baseline version of gf_vect_mad() with same parameters.
 */

void gf_vect_mad_base(int len, int vec, int vec_i, unsigned char *v, unsigned char *src,
		      unsigned char *dest);

// x86 only
#if defined(__i386__) || defined(__x86_64__)

/**
 * @brief Generate or decode erasure codes on blocks of data.
 *
 * Arch specific version of ec_encode_data() with same parameters.
 * @requires SSE4.1
 */
void ec_encode_data_sse(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
			unsigned char **coding);

/**
 * @brief Generate or decode erasure codes on blocks of data.
 *
 * Arch specific version of ec_encode_data() with same parameters.
 * @requires AVX
 */
void ec_encode_data_avx(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
			unsigned char **coding);

/**
 * @brief Generate or decode erasure codes on blocks of data.
 *
 * Arch specific version of ec_encode_data() with same parameters.
 * @requires AVX2
 */
void ec_encode_data_avx2(int len, int k, int rows, unsigned char *gftbls, unsigned char **data,
			 unsigned char **coding);

/**
 * @brief Generate update for encode or decode of erasure codes from single source.
 *
 * Arch specific version of ec_encode_data_update() with same parameters.
 * @requires SSE4.1
 */

void ec_encode_data_update_sse(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
			       unsigned char *data, unsigned char **coding);

/**
 * @brief Generate update for encode or decode of erasure codes from single source.
 *
 * Arch specific version of ec_encode_data_update() with same parameters.
 * @requires AVX
 */

void ec_encode_data_update_avx(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
			       unsigned char *data, unsigned char **coding);

/**
 * @brief Generate update for encode or decode of erasure codes from single source.
 *
 * Arch specific version of ec_encode_data_update() with same parameters.
 * @requires AVX2
 */

void ec_encode_data_update_avx2(int len, int k, int rows, int vec_i, unsigned char *g_tbls,
				unsigned char *data, unsigned char **coding);

/**
 * @brief GF(2^8) vector dot product.
 *
 * Does a GF(2^8) dot product across each byte of the input array and a constant
 * set of coefficients to produce each byte of the output. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 32*vlen byte constant array based on the input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
 *               on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Pointer to destination data array.
 * @returns none
 */

void gf_vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char *dest);

/**
 * @brief GF(2^8) vector dot product.
 *
 * Does a GF(2^8) dot product across each byte of the input array and a constant
 * set of coefficients to produce each byte of the output. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 32*vlen byte constant array based on the input coefficients.
 * @requires AVX
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
 *               on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Pointer to destination data array.
 * @returns none
 */

void gf_vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char *dest);

/**
 * @brief GF(2^8) vector dot product.
 *
 * Does a GF(2^8) dot product across each byte of the input array and a constant
 * set of coefficients to produce each byte of the output. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 32*vlen byte constant array based on the input coefficients.
 * @requires AVX2
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 32*vlen byte array of pre-calculated constants based
 *               on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Pointer to destination data array.
 * @returns none
 */

void gf_vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char *dest);

/**
 * @brief GF(2^8) vector dot product with two outputs.
 *
 * Vector dot product optimized to calculate two outputs at a time. Does two
 * GF(2^8) dot products across each byte of the input array and two constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 2*32*vlen byte constant array based on the two sets of input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 2*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_2vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with two outputs.
 *
 * Vector dot product optimized to calculate two outputs at a time. Does two
 * GF(2^8) dot products across each byte of the input array and two constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 2*32*vlen byte constant array based on the two sets of input coefficients.
 * @requires AVX
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 2*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_2vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with two outputs.
 *
 * Vector dot product optimized to calculate two outputs at a time. Does two
 * GF(2^8) dot products across each byte of the input array and two constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 2*32*vlen byte constant array based on the two sets of input coefficients.
 * @requires AVX2
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 2*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_2vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with three outputs.
 *
 * Vector dot product optimized to calculate three outputs at a time. Does three
 * GF(2^8) dot products across each byte of the input array and three constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 3*32*vlen byte constant array based on the three sets of input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 3*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_3vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with three outputs.
 *
 * Vector dot product optimized to calculate three outputs at a time. Does three
 * GF(2^8) dot products across each byte of the input array and three constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 3*32*vlen byte constant array based on the three sets of input coefficients.
 * @requires AVX
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 3*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_3vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with three outputs.
 *
 * Vector dot product optimized to calculate three outputs at a time. Does three
 * GF(2^8) dot products across each byte of the input array and three constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 3*32*vlen byte constant array based on the three sets of input coefficients.
 * @requires AVX2
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 3*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_3vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with four outputs.
 *
 * Vector dot product optimized to calculate four outputs at a time. Does four
 * GF(2^8) dot products across each byte of the input array and four constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 4*32*vlen byte constant array based on the four sets of input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 4*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_4vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with four outputs.
 *
 * Vector dot product optimized to calculate four outputs at a time. Does four
 * GF(2^8) dot products across each byte of the input array and four constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 4*32*vlen byte constant array based on the four sets of input coefficients.
 * @requires AVX
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 4*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_4vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with four outputs.
 *
 * Vector dot product optimized to calculate four outputs at a time. Does four
 * GF(2^8) dot products across each byte of the input array and four constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 4*32*vlen byte constant array based on the four sets of input coefficients.
 * @requires AVX2
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 4*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_4vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with five outputs.
 *
 * Vector dot product optimized to calculate five outputs at a time. Does five
 * GF(2^8) dot products across each byte of the input array and five constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 5*32*vlen byte constant array based on the five sets of input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 5*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_5vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with five outputs.
 *
 * Vector dot product optimized to calculate five outputs at a time. Does five
 * GF(2^8) dot products across each byte of the input array and five constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 5*32*vlen byte constant array based on the five sets of input coefficients.
 * @requires AVX
 *
 * @param len    Length of each vector in bytes. Must >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 5*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_5vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with five outputs.
 *
 * Vector dot product optimized to calculate five outputs at a time. Does five
 * GF(2^8) dot products across each byte of the input array and five constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 5*32*vlen byte constant array based on the five sets of input coefficients.
 * @requires AVX2
 *
 * @param len    Length of each vector in bytes. Must >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 5*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_5vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with six outputs.
 *
 * Vector dot product optimized to calculate six outputs at a time. Does six
 * GF(2^8) dot products across each byte of the input array and six constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 6*32*vlen byte constant array based on the six sets of input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 6*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_6vect_dot_prod_sse(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with six outputs.
 *
 * Vector dot product optimized to calculate six outputs at a time. Does six
 * GF(2^8) dot products across each byte of the input array and six constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 6*32*vlen byte constant array based on the six sets of input coefficients.
 * @requires AVX
 *
 * @param len    Length of each vector in bytes. Must be >= 16.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 6*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_6vect_dot_prod_avx(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector dot product with six outputs.
 *
 * Vector dot product optimized to calculate six outputs at a time. Does six
 * GF(2^8) dot products across each byte of the input array and six constant
 * sets of coefficients to produce each byte of the outputs. Can be used for
 * erasure coding encode and decode. Function requires pre-calculation of a
 * 6*32*vlen byte constant array based on the six sets of input coefficients.
 * @requires AVX2
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vlen   Number of vector sources.
 * @param gftbls Pointer to 6*32*vlen byte array of pre-calculated constants
 *               based on the array of input coefficients.
 * @param src    Array of pointers to source inputs.
 * @param dest   Array of pointers to destination data buffers.
 * @returns none
 */

void gf_6vect_dot_prod_avx2(int len, int vlen, unsigned char *gftbls,
			unsigned char **src, unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply accumulate, arch specific version.
 *
 * Arch specific version of gf_vect_mad() with same parameters.
 * @requires SSE4.1
 */

void gf_vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		     unsigned char *dest);
/**
 * @brief GF(2^8) vector multiply accumulate, arch specific version.
 *
 * Arch specific version of gf_vect_mad() with same parameters.
 * @requires AVX
 */

void gf_vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		     unsigned char *dest);

/**
 * @brief GF(2^8) vector multiply accumulate, arch specific version.
 *
 * Arch specific version of gf_vect_mad() with same parameters.
 * @requires AVX2
 */

void gf_vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char *dest);


/**
 * @brief GF(2^8) vector multiply with 2 accumulate.  SSE version.
 *
 * Does a GF(2^8) multiply across each byte of input source with expanded
 * constants and add to destination arrays. Can be used for erasure coding
 * encode and decode update when only one source is available at a
 * time. Function requires pre-calculation of a 32*vec byte constant array based
 * on the input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vec    The number of vector sources or rows in the generator matrix
 * 		 for coding.
 * @param vec_i  The vector index corresponding to the single input source.
 * @param gftbls Pointer to array of input tables generated from coding
 * 		 coefficients in ec_init_tables(). Must be of size 32*vec.
 * @param src    Pointer to source input array.
 * @param dest   Array of pointers to destination input/outputs.
 * @returns none
 */

void gf_2vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 2 accumulate. AVX version of gf_2vect_mad_sse().
 * @requires AVX
 */
void gf_2vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);
/**
 * @brief GF(2^8) vector multiply with 2 accumulate. AVX2 version of gf_2vect_mad_sse().
 * @requires AVX2
 */
void gf_2vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		       unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 3 accumulate. SSE version.
 *
 * Does a GF(2^8) multiply across each byte of input source with expanded
 * constants and add to destination arrays. Can be used for erasure coding
 * encode and decode update when only one source is available at a
 * time. Function requires pre-calculation of a 32*vec byte constant array based
 * on the input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vec    The number of vector sources or rows in the generator matrix
 * 		 for coding.
 * @param vec_i  The vector index corresponding to the single input source.
 * @param gftbls Pointer to array of input tables generated from coding
 * 		 coefficients in ec_init_tables(). Must be of size 32*vec.
 * @param src    Pointer to source input array.
 * @param dest   Array of pointers to destination input/outputs.
 * @returns none
 */

void gf_3vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 3 accumulate. AVX version of gf_3vect_mad_sse().
 * @requires AVX
 */
void gf_3vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 3 accumulate. AVX2 version of gf_3vect_mad_sse().
 * @requires AVX2
 */
void gf_3vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		       unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 4 accumulate. SSE version.
 *
 * Does a GF(2^8) multiply across each byte of input source with expanded
 * constants and add to destination arrays. Can be used for erasure coding
 * encode and decode update when only one source is available at a
 * time. Function requires pre-calculation of a 32*vec byte constant array based
 * on the input coefficients.
 * @requires SSE4.1
 *
 * @param len    Length of each vector in bytes. Must be >= 32.
 * @param vec    The number of vector sources or rows in the generator matrix
 * 		 for coding.
 * @param vec_i  The vector index corresponding to the single input source.
 * @param gftbls Pointer to array of input tables generated from coding
 * 		 coefficients in ec_init_tables(). Must be of size 32*vec.
 * @param src    Pointer to source input array.
 * @param dest   Array of pointers to destination input/outputs.
 * @returns none
 */

void gf_4vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 4 accumulate. AVX version of gf_4vect_mad_sse().
 * @requires AVX
 */
void gf_4vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);
/**
 * @brief GF(2^8) vector multiply with 4 accumulate. AVX2 version of gf_4vect_mad_sse().
 * @requires AVX2
 */
void gf_4vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		       unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 5 accumulate. SSE version.
 * @requires SSE4.1
 */
void gf_5vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 5 accumulate. AVX version.
 * @requires AVX
 */
void gf_5vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);
/**
 * @brief GF(2^8) vector multiply with 5 accumulate. AVX2 version.
 * @requires AVX2
 */
void gf_5vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		       unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 6 accumulate. SSE version.
 * @requires SSE4.1
 */
void gf_6vect_mad_sse(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);
/**
 * @brief GF(2^8) vector multiply with 6 accumulate. AVX version.
 * @requires AVX
 */
void gf_6vect_mad_avx(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		      unsigned char **dest);

/**
 * @brief GF(2^8) vector multiply with 6 accumulate. AVX2 version.
 * @requires AVX2
 */
void gf_6vect_mad_avx2(int len, int vec, int vec_i, unsigned char *gftbls, unsigned char *src,
		       unsigned char **dest);

#endif

/**********************************************************************
 * The remaining are lib support functions used in GF(2^8) operations.
 */

/**
 * @brief Single element GF(2^8) multiply.
 *
 * @param a  Multiplicand a
 * @param b  Multiplicand b
 * @returns  Product of a and b in GF(2^8)
 */

unsigned char gf_mul_erasure(unsigned char a, unsigned char b);

/**
 * @brief Single element GF(2^8) inverse.
 *
 * @param a  Input element
 * @returns  Field element b such that a x b = {1}
 */

unsigned char gf_inv(unsigned char a);

/**
 * @brief Generate a matrix of coefficients to be used for encoding.
 *
 * Vandermonde matrix example of encoding coefficients where high portion of
 * matrix is identity matrix I and lower portion is constructed as 2^{i*(j-k+1)}
 * i:{0,k-1} j:{k,m-1}. Commonly used method for choosing coefficients in
 * erasure encoding but does not guarantee invertable for every sub matrix. For
 * large pairs of m and k it is possible to find cases where the decode matrix
 * chosen from sources and parity is not invertable. Users may want to adjust
 * for certain pairs m and k. If m and k satisfy one of the following
 * inequalities, no adjustment is required:
 *
 * - k <= 3
 * - k = 4, m <= 25
 * - k = 5, m <= 10
 * - k <= 21, m-k = 4
 * - m - k <= 3.
 *
 * @param a  [m x k] array to hold coefficients
 * @param m  number of rows in matrix corresponding to srcs + parity.
 * @param k  number of columns in matrix corresponding to srcs.
 * @returns  none
 */

void gf_gen_rs_matrix(unsigned char *a, int m, int k);

/**
 * @brief Generate a Cauchy matrix of coefficients to be used for encoding.
 *
 * Cauchy matrix example of encoding coefficients where high portion of matrix
 * is identity matrix I and lower portion is constructed as 1/(i + j) | i != j,
 * i:{0,k-1} j:{k,m-1}.  Any sub-matrix of a Cauchy matrix should be invertable.
 *
 * @param a  [m x k] array to hold coefficients
 * @param m  number of rows in matrix corresponding to srcs + parity.
 * @param k  number of columns in matrix corresponding to srcs.
 * @returns  none
 */

void gf_gen_cauchy1_matrix(unsigned char *a, int m, int k);

/**
 * @brief Invert a matrix in GF(2^8)
 *
 * @param in  input matrix
 * @param out output matrix such that [in] x [out] = [I] - identity matrix
 * @param n   size of matrix [nxn]
 * @returns 0 successful, other fail on singular input matrix
 */

int gf_invert_matrix(unsigned char *in, unsigned char *out, const int n);


/*************************************************************/

#ifdef __cplusplus
}
#endif

#endif //_ERASURE_CODE_H_