-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbinding.c
1311 lines (1274 loc) · 36.6 KB
/
binding.c
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <node_api.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RESOURCE_NAME "@ronomon/reed-solomon"
#define OK(call) \
assert((call) == napi_ok);
#define THROW(env, message) \
do { \
napi_throw_error((env), NULL, (message)); \
return NULL; \
} while (0)
#define MAX_K 24
#define MAX_M 6
#define MAX_W 8
// Parameters for (k,m) found by `search()` are in PARAMETERS[k-1][m-1]:
// PARAMETERS[k-1][m-1] = k, m, w, p, x, y, b:
//
// k = The number of data shards.
// m = The number of parity shards.
// w = The Galois Field exponent. The smaller the exponent, the less bits.
// p = The primitive polynomial used to generate the Galois Field.
//
// We do not use x or y when m <= 2:
// x = The column offset used to generate the matrix, -1 when m <= 2.
// y = The row offset used to generate the matrix, -1 when m <= 2.
//
// b = The number of bits in the resulting bit matrix.
static const int PARAMETERS[24][6][7] = {
{
{ 1, 1, 2, 7, -1, -1, 2 },
{ 1, 2, 2, 7, -1, -1, 4 },
{ 1, 3, 2, 7, 0, 1, 6 },
{ 1, 4, 4, 19, 0, 1, 16 },
{ 1, 5, 4, 19, 0, 1, 20 },
{ 1, 6, 4, 19, 0, 1, 24 }
},
{
{ 2, 1, 2, 7, -1, -1, 4 },
{ 2, 2, 2, 7, -1, -1, 9 },
{ 2, 3, 4, 19, 0, 4, 28 },
{ 2, 4, 4, 19, 0, 5, 40 },
{ 2, 5, 4, 19, 2, 9, 51 },
{ 2, 6, 4, 19, 4, 10, 62 }
},
{
{ 3, 1, 2, 7, -1, -1, 6 },
{ 3, 2, 4, 19, -1, -1, 26 },
{ 3, 3, 4, 19, 0, 9, 44 },
{ 3, 4, 4, 19, 1, 8, 63 },
{ 3, 5, 4, 19, 1, 9, 82 },
{ 3, 6, 4, 19, 0, 9, 101 }
},
{
{ 4, 1, 4, 19, -1, -1, 16 },
{ 4, 2, 4, 19, -1, -1, 36 },
{ 4, 3, 4, 19, 0, 9, 63 },
{ 4, 4, 4, 19, 3, 11, 89 },
{ 4, 5, 4, 19, 3, 11, 116 },
{ 4, 6, 4, 19, 11, 4, 145 }
},
{
{ 5, 1, 4, 19, -1, -1, 20 },
{ 5, 2, 4, 19, -1, -1, 47 },
{ 5, 3, 4, 19, 4, 13, 82 },
{ 5, 4, 4, 19, 3, 12, 118 },
{ 5, 5, 4, 19, 0, 9, 152 },
{ 5, 6, 4, 19, 0, 9, 185 }
},
{
{ 6, 1, 4, 19, -1, -1, 24 },
{ 6, 2, 4, 19, -1, -1, 58 },
{ 6, 3, 4, 19, 2, 12, 102 },
{ 6, 4, 4, 19, 2, 12, 144 },
{ 6, 5, 4, 19, 0, 9, 186 },
{ 6, 6, 4, 19, 0, 9, 231 }
},
{
{ 7, 1, 4, 19, -1, -1, 28 },
{ 7, 2, 4, 19, -1, -1, 71 },
{ 7, 3, 4, 19, 1, 13, 123 },
{ 7, 4, 4, 19, 2, 12, 174 },
{ 7, 5, 4, 19, 0, 9, 226 },
{ 7, 6, 4, 19, 7, 0, 277 }
},
{
{ 8, 1, 4, 19, -1, -1, 32 },
{ 8, 2, 4, 19, -1, -1, 84 },
{ 8, 3, 4, 19, 2, 13, 142 },
{ 8, 4, 4, 19, 2, 12, 205 },
{ 8, 5, 4, 19, 0, 9, 265 },
{ 8, 6, 4, 19, 0, 8, 328 }
},
{
{ 9, 1, 4, 19, -1, -1, 36 },
{ 9, 2, 4, 19, -1, -1, 97 },
{ 9, 3, 4, 19, 1, 13, 162 },
{ 9, 4, 4, 19, 2, 12, 237 },
{ 9, 5, 4, 19, 0, 9, 308 },
{ 9, 6, 4, 19, 1, 10, 376 }
},
{
{ 10, 1, 4, 19, -1, -1, 40 },
{ 10, 2, 4, 19, -1, -1, 111 },
{ 10, 3, 4, 19, 1, 13, 186 },
{ 10, 4, 4, 19, 0, 12, 268 },
{ 10, 5, 4, 19, 0, 11, 347 },
{ 10, 6, 4, 19, 0, 10, 426 }
},
{
{ 11, 1, 4, 19, -1, -1, 44 },
{ 11, 2, 4, 19, -1, -1, 125 },
{ 11, 3, 4, 19, 0, 13, 211 },
{ 11, 4, 4, 19, 0, 12, 300 },
{ 11, 5, 4, 19, 0, 11, 390 },
{ 11, 6, 8, 135, 58, 188, 1401 }
},
{
{ 12, 1, 4, 19, -1, -1, 48 },
{ 12, 2, 4, 19, -1, -1, 139 },
{ 12, 3, 4, 19, 3, 0, 234 },
{ 12, 4, 4, 19, 0, 12, 334 },
{ 12, 5, 8, 113, 24, 208, 1269 },
{ 12, 6, 8, 135, 57, 188, 1577 }
},
{
{ 13, 1, 4, 19, -1, -1, 52 },
{ 13, 2, 4, 19, -1, -1, 155 },
{ 13, 3, 4, 19, 0, 13, 261 },
{ 13, 4, 8, 135, 59, 189, 1037 },
{ 13, 5, 8, 113, 27, 236, 1393 },
{ 13, 6, 8, 113, 27, 236, 1733 }
},
{
{ 14, 1, 4, 19, -1, -1, 56 },
{ 14, 2, 4, 19, -1, -1, 171 },
{ 14, 3, 8, 169, 4, 252, 777 },
{ 14, 4, 8, 135, 58, 189, 1121 },
{ 14, 5, 8, 135, 58, 189, 1508 },
{ 14, 6, 8, 135, 58, 188, 1880 }
},
{
{ 15, 1, 4, 19, -1, -1, 60 },
{ 15, 2, 8, 135, -1, -1, 353 },
{ 15, 3, 8, 113, 24, 209, 836 },
{ 15, 4, 8, 135, 58, 189, 1225 },
{ 15, 5, 8, 101, 28, 232, 1644 },
{ 15, 6, 8, 113, 120, 241, 2037 }
},
{
{ 16, 1, 8, 29, -1, -1, 128 },
{ 16, 2, 8, 135, -1, -1, 380 },
{ 16, 3, 8, 113, 22, 213, 901 },
{ 16, 4, 8, 113, 22, 212, 1324 },
{ 16, 5, 8, 101, 28, 232, 1765 },
{ 16, 6, 8, 101, 28, 232, 2195 }
},
{
{ 17, 1, 8, 29, -1, -1, 136 },
{ 17, 2, 8, 135, -1, -1, 407 },
{ 17, 3, 8, 113, 22, 213, 960 },
{ 17, 4, 8, 135, 58, 189, 1423 },
{ 17, 5, 8, 101, 27, 232, 1880 },
{ 17, 6, 8, 101, 27, 232, 2343 }
},
{
{ 18, 1, 8, 29, -1, -1, 144 },
{ 18, 2, 8, 135, -1, -1, 434 },
{ 18, 3, 8, 113, 24, 213, 1027 },
{ 18, 4, 8, 113, 22, 212, 1513 },
{ 18, 5, 8, 195, 8, 32, 2019 },
{ 18, 6, 8, 113, 205, 126, 2500 }
},
{
{ 19, 1, 8, 29, -1, -1, 152 },
{ 19, 2, 8, 135, -1, -1, 462 },
{ 19, 3, 8, 113, 22, 213, 1086 },
{ 19, 4, 8, 113, 23, 212, 1604 },
{ 19, 5, 8, 195, 7, 32, 2131 },
{ 19, 6, 8, 195, 3, 60, 2654 }
},
{
{ 20, 1, 8, 29, -1, -1, 160 },
{ 20, 2, 8, 135, -1, -1, 490 },
{ 20, 3, 8, 113, 22, 213, 1147 },
{ 20, 4, 8, 113, 22, 212, 1695 },
{ 20, 5, 8, 195, 4, 238, 2270 },
{ 20, 6, 8, 113, 21, 233, 2816 }
},
{
{ 21, 1, 8, 29, -1, -1, 168 },
{ 21, 2, 8, 135, -1, -1, 518 },
{ 21, 3, 8, 113, 21, 213, 1225 },
{ 21, 4, 8, 113, 21, 212, 1801 },
{ 21, 5, 8, 195, 3, 60, 2395 },
{ 21, 6, 8, 195, 3, 60, 2980 }
},
{
{ 22, 1, 8, 29, -1, -1, 176 },
{ 22, 2, 8, 135, -1, -1, 546 },
{ 22, 3, 8, 113, 20, 213, 1292 },
{ 22, 4, 8, 113, 21, 212, 1906 },
{ 22, 5, 8, 195, 35, 28, 2512 },
{ 22, 6, 8, 195, 3, 60, 3135 }
},
{
{ 23, 1, 8, 29, -1, -1, 184 },
{ 23, 2, 8, 135, -1, -1, 574 },
{ 23, 3, 8, 113, 19, 213, 1366 },
{ 23, 4, 8, 113, 19, 212, 2008 },
{ 23, 5, 8, 195, 3, 238, 2652 },
{ 23, 6, 8, 113, 205, 126, 3291 }
},
{
{ 24, 1, 8, 29, -1, -1, 192 },
{ 24, 2, 8, 135, -1, -1, 603 },
{ 24, 3, 8, 113, 18, 213, 1437 },
{ 24, 4, 8, 195, 125, 91, 2110 },
{ 24, 5, 8, 195, 3, 238, 2787 },
{ 24, 6, 8, 195, 42, 225, 3466 }
}
};
static int g_divide(
const int* log,
const int* exp,
const int w,
const int a,
const int b
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
const int y = (1 << w) - 1;
assert(a <= y);
assert(b <= y);
assert(b >= 1);
if (a == 0) return 0;
return exp[(log[a] + y - log[b]) % y];
}
static int g_multiply(
const int* log,
const int* exp,
const int w,
const int a,
const int b
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
const int y = (1 << w) - 1;
assert(a <= y);
assert(b <= y);
if (a == 0 || b == 0) return 0;
return exp[(log[a] + log[b]) % y];
}
static int bitmatrix_m0_optimized(
const int w,
const int k,
const uint8_t* bitmatrix
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(k < (1 << w));
// We assume that bitmatrix is an encoding (not decoding) bitmatrix.
// If row 0 is all ones then an erasure of shard < k + 1 can be optimized.
for (int c = 0; c < k; c++) {
for (int a = 0; a < w; a++) {
if (a == 0) {
if (bitmatrix[c * w + a] != 1) return 0;
} else {
if (bitmatrix[c * w + a] != 0) return 0;
}
}
}
return 1;
}
static void create_bitmatrix_decoding_swap(
uint8_t* buffer,
const int x,
const int y
) {
const uint8_t tmp = buffer[x];
buffer[x] = buffer[y];
buffer[y] = tmp;
}
static void create_bitmatrix_decoding_invert(
uint8_t* source,
uint8_t* target,
const int rows
) {
const int cols = rows;
int k = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
target[k++] = (r == c) ? 1 : 0;
}
}
for (int c = 0; c < cols; c++) {
if ((source[c * cols + c]) == 0) {
int r = c + 1;
while (r < rows && source[r * cols + c] == 0) r++;
// Assert that matrix is invertible:
assert(r != rows);
for (int k = 0; k < cols; k++) {
create_bitmatrix_decoding_swap(source, c * cols + k, r * cols + k);
create_bitmatrix_decoding_swap(target, c * cols + k, r * cols + k);
}
}
for (int r = c + 1; r != rows; r++) {
if (source[r * cols + c] != 0) {
for (int k = 0; k < cols; k++) {
source[r * cols + k] ^= source[c * cols + k];
target[r * cols + k] ^= target[c * cols + k];
}
}
}
}
for (int r = rows - 1; r >= 0; r--) {
for (int c = 0; c < r; c++) {
if (source[c * cols + r]) {
for (int k = 0; k < cols; k++) {
source[c * cols + k] ^= source[r * cols + k];
target[c * cols + k] ^= target[r * cols + k];
}
}
}
}
}
static void create_bitmatrix_decoding(
const int w,
const int k,
const int m,
const int* sourceIndex,
const uint8_t* source,
uint8_t* target
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(m >= 1);
assert(m <= MAX_M);
assert(k + m <= (1 << w));
const int kww = k * w * w;
uint8_t matrix[MAX_K * MAX_K * MAX_W * MAX_W];
for (int a = 0; a < k; a++) {
if (sourceIndex[a] < k) {
for (int b = 0; b < kww; b++) matrix[kww * a + b] = 0;
int index = kww * a + sourceIndex[a] * w;
for (int b = 0; b < w; b++) {
matrix[index] = 1;
index += (k * w + 1);
}
} else {
for (int b = 0; b < kww; b++) {
matrix[kww * a + b] = source[kww * (sourceIndex[a] - k) + b];
}
}
}
create_bitmatrix_decoding_invert(matrix, target, k * w);
}
static int create_bitmatrix_encoding(
const int* log,
const int* exp,
const int w,
const int k,
const int m,
const uint8_t* matrix,
uint8_t* bitmatrix
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(m >= 1);
assert(m <= MAX_M);
assert(k + m <= (1 << w));
int count = 0;
for (int r = 0; r < m; r++) {
for (int c = 0; c < k; c++) {
int x = matrix[(k * r) + c];
for (int a = 0; a < w; a++) {
for (int b = 0; b < w; b++) {
int y = (x & (1 << b)) ? 1 : 0;
bitmatrix[(r * w * k * w) + (w * c) + a + (k * w * b)] = y;
count += y;
}
x = g_multiply(log, exp, w, x, 2);
}
}
}
assert(count > 0);
return count;
}
static int create_matrix(
const int* log,
const int* exp,
const int* bit,
const int* min,
const int w,
const int k,
const int m,
const int x,
const int y,
uint8_t* matrix
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(m >= 1);
assert(m <= MAX_M);
assert(k + m <= (1 << w));
const int z = 1 << w;
int count = bit[1] * k;
if (m == 1) {
// Use XOR for row 0.
assert(x == -1);
assert(y == -1);
for (int c = 0; c < k; c++) matrix[c] = 1;
} else if (m == 2) {
// Use XOR for row 0.
// Use integers with least number of bits for row 1.
assert(x == -1);
assert(y == -1);
for (int c = 0; c < k; c++) matrix[c] = 1;
for (int c = 0; c < k; c++) {
matrix[k + c] = min[c + 1];
if (c == 0) assert(matrix[k + c] == 1);
assert(matrix[k + c] > 0);
count += bit[matrix[k + c]];
}
} else {
// Use XOR for row 0.
// Use generic matrix thereafter.
assert(x + k <= z);
assert(y + m <= z);
assert(x != y);
if (x < y) {
assert(x + k <= y);
} else {
assert(y + m <= x);
}
for (int r = 0; r < m; r++) {
for (int c = 0; c < k; c++) {
assert(y + r < z);
assert(x + c < z);
matrix[r * k + c] = g_divide(log, exp, w, 1, (y + r) ^ (x + c));
}
}
// Divide rows by row 0:
for (int r = 1; r < m; r++) {
for (int c = 0; c < k; c++) {
matrix[r * k + c] = g_divide(log, exp, w, matrix[r * k + c], matrix[c]);
}
}
// Divide row 0 by itself to set row 0 to 1:
for (int c = 0; c < k; c++) {
matrix[c] = g_divide(log, exp, w, matrix[c], matrix[c]);
assert(matrix[c] == 1);
}
// Divide columns by the column which minimizes the resulting ones (if any):
for (int r = 1; r < m; r++) {
const int rk = r * k;
int result = 0;
int column = -1;
for (int c = 0; c < k; c++) result += bit[matrix[rk + c]];
for (int c = 0; c < k; c++) {
int bits = 0;
for (int d = 0; d < k; d++) {
bits += bit[g_divide(log, exp, w, matrix[rk + d], matrix[rk + c])];
}
if (bits < result) {
result = bits;
column = matrix[rk + c];
}
}
if (column >= 0) {
for (int c = 0; c < k; c++) {
matrix[rk + c] = g_divide(log, exp, w, matrix[rk + c], column);
}
}
count += result;
}
}
for (int c = 0; c < k; c++) assert(matrix[c] == 1);
assert(count > 0);
return count;
}
static int create_tables_bits(
const int* log,
const int* exp,
const int w,
int n
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
int count = 0;
for (int r = 0; r < w; r++) {
for (int c = 0; c < w; c++) {
count += (n & (1 << c)) ? 1 : 0;
}
n = g_multiply(log, exp, w, n, 2);
}
return count;
}
static void create_tables(
const int w,
const int p,
int* log,
int* exp,
int* bit,
int* min
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
const int y = (1 << w) - 1;
const int z = (1 << w);
// Generate log and exp tables:
for (int a = 0; a < z; a++) {
log[a] = y;
exp[a] = 0;
}
int b = 1;
for (int a = 0; a < y; a++) {
assert(b < z);
assert(log[b] == y);
assert(exp[a] == 0);
log[b] = a;
exp[a] = b;
b = b << 1;
if (b & z) b = (b ^ p) & y;
}
// The logarithm of zero must not be defined:
assert(log[0] == y);
// The last byte of the exponents table must not be defined:
assert(exp[y] == 0);
// Generate bit table (number of bits per matrix number):
for (int n = 0; n < z; n++) {
bit[n] = create_tables_bits(log, exp, w, n);
if (n == 0) {
assert(bit[n] == 0);
} else {
assert(bit[n] > 0);
}
}
// Generate min table (matrix numbers sorted by least number of bits):
assert(bit[0] == 0);
min[0] = 0;
for (int a = 1; a < z; a++) {
assert(a > 0);
int c = min[a - 1];
int d = -1;
for (int b = 1; b < z; b++) {
assert(b > 0);
assert(bit[b] > 0);
if (bit[b] < bit[c]) continue;
if (bit[b] == bit[c] && b <= c) continue;
if (d == -1 || bit[b] < bit[d]) d = b;
}
assert(d > 0);
assert(d < z);
min[a] = d;
}
assert(min[y] > 0);
}
static uintptr_t unaligned64(const uint8_t* pointer) {
return ((uintptr_t) pointer) & ((uintptr_t) 7);
}
static uint32_t dot_chunk_size(
const int w,
const int k,
const uint32_t shardSize
) {
// Every dot loop pair (b * k, c * w) uses ((1 + k * w) * chunkSize) of cache.
// Avoiding cache misses yields a 40-100% improvement when shardSize is large.
// We therefore reduce the chunkSize if necessary to stay within the cache.
// The shardSize should ideally be a power of 2 to do this optimally.
// N.B. The chunkSize changes the encoded parity result.
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(k < (1 << w));
assert(shardSize % w == 0);
uint32_t chunkSize = shardSize / w;
while (
chunkSize > 64 &&
chunkSize % 2 == 0 &&
(1 + k * w) * chunkSize > 1048576
) {
chunkSize /= 2;
}
assert(chunkSize > 0);
assert(shardSize % (w * chunkSize) == 0);
return chunkSize;
}
static void dot_cpy(uint8_t* source, uint8_t* target, uint32_t length) {
assert(length > 0);
assert(source != target);
memcpy(target, source, length);
}
static void dot_xor(uint8_t* source, uint8_t* target, uint32_t length) {
assert(source != target);
assert(length > 0);
uint8_t* sourceEnd = source + length;
uint8_t* targetEnd = target + length;
// XOR 8-bit words if source and target alignment cannot be corrected:
if (unaligned64(source) != unaligned64(target)) {
while (length > 0) {
*target++ ^= *source++;
length--;
}
assert(source == sourceEnd);
assert(target == targetEnd);
assert(length == 0);
return;
}
// XOR 8-bit words to correct source and target alignment:
while (unaligned64(source) && length > 0) {
*target++ ^= *source++;
length--;
}
if (length == 0) {
assert(source == sourceEnd);
assert(target == targetEnd);
return;
}
assert(unaligned64(source) == 0);
assert(unaligned64(target) == 0);
// XOR as many 64-bit words as possible:
uint32_t words = length / 8;
if (words > 0) {
uint32_t width = words * 8;
assert(width <= length);
uint64_t* source64 = (uint64_t*) source;
uint64_t* target64 = (uint64_t*) target;
while (words > 0) {
*target64++ ^= *source64++;
words--;
}
assert(words == 0);
source += width;
target += width;
length -= width;
}
// XOR 8-bit words remainder:
assert(length < 8);
while (length > 0) {
*target++ ^= *source++;
length--;
}
assert(source == sourceEnd);
assert(target == targetEnd);
assert(length == 0);
}
static void dot(
const int w,
const int k,
uint8_t** shards,
const uint32_t shardSize,
const uint8_t* row,
const int* sourceIndex,
const int targetIndex
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(k < (1 << w));
assert(shardSize % w == 0);
uint32_t chunkSize = dot_chunk_size(w, k, shardSize);
assert(w * chunkSize <= shardSize);
assert(shardSize % (w * chunkSize) == 0);
uint32_t shardOffset = 0;
while (shardOffset < shardSize) {
int column = 0;
for (int a = 0; a < w; a++) {
int copied = 0;
uint8_t* target = shards[targetIndex] + shardOffset + a * chunkSize;
for (int b = 0; b < k; b++) {
uint8_t* source = shards[sourceIndex[b]];
for (int c = 0; c < w; c++) {
if (row[column]) {
if (!copied) {
dot_cpy(source + shardOffset + c * chunkSize, target, chunkSize);
copied = 1;
} else {
dot_xor(source + shardOffset + c * chunkSize, target, chunkSize);
}
}
column++;
}
}
}
shardOffset += w * chunkSize;
}
assert(shardOffset == shardSize);
}
static int flags_count(uint32_t flags) {
int count = 0;
while (flags > 0) {
if (flags & 1) count++; // Check lower bit.
flags >>= 1; // Shift lower bit.
}
return count;
}
static int flags_first(const uint32_t flags) {
int i = 0;
while ((flags & (1 << i)) == 0) i++;
assert((flags & (1 << i)) != 0);
return i;
}
static void reed_solomon_encode(
const int w,
const int k,
const int m,
const uint8_t* bitmatrixEncoding,
const uint32_t sources,
const uint32_t targets,
uint8_t** shards,
const uint32_t shardSize
) {
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
assert(k >= 1);
assert(k <= MAX_K);
assert(m >= 1);
assert(m <= MAX_M);
assert(k + m <= (1 << w));
if (k == 1) {
// Optimization for pure replication, encoding only targets:
uint8_t* source = shards[flags_first(sources)];
for (int i = 0; i < k + m; i++) {
if (targets & (1 << i)) dot_cpy(source, shards[i], shardSize);
}
return;
}
if (
flags_count(targets) == 1 &&
flags_count(sources & ((1 << (k + 1)) - 1)) == k &&
flags_count(targets & ((1 << (k + 1)) - 1)) == 1
) {
// Optimization for 1 erasure (i < k + 1), encoding only targets:
uint8_t* target = shards[flags_first(targets)];
int copied = 0;
for (int i = 0; i < k + 1; i++) {
if (sources & (1 << i)) {
if (!copied) {
dot_cpy(shards[i], target, shardSize);
copied = 1;
} else {
dot_xor(shards[i], target, shardSize);
}
}
}
return;
}
const int kww = k * w * w;
int max = k;
int kerasures = 0;
for (int i = 0; i < k; i++) {
if (!(sources & (1 << i))) {
max = i;
kerasures++;
}
}
if (!(sources & (1 << k))) max = k;
if (kerasures > 1 || (kerasures == 1 && !(sources & (1 << k)))) {
int s[MAX_K];
int si = 0;
int sj = 0;
while (sj < k) {
if (sources & (1 << si)) s[sj++] = si;
si++;
}
uint8_t bitmatrixDecoding[MAX_K * MAX_K * MAX_W * MAX_W];
create_bitmatrix_decoding(
w,
k,
m,
s,
bitmatrixEncoding,
bitmatrixDecoding
);
for (int i = 0; kerasures > 0 && i < max; i++) {
if (!(sources & (1 << i))) {
dot(w, k, shards, shardSize, bitmatrixDecoding + kww * i, s, i);
kerasures--;
}
}
}
if (kerasures > 0) {
int s[MAX_K];
for (int si = 0; si < k; si++) s[si] = (si < max) ? si : si + 1;
dot(w, k, shards, shardSize, bitmatrixEncoding, s, max);
}
for (int i = 0; i < m; i++) {
if (!(sources & (1 << (k + i)))) {
int s[MAX_K];
for (int si = 0; si < k; si++) s[si] = si;
dot(w, k, shards, shardSize, bitmatrixEncoding + kww * i, s, k + i);
}
}
}
static int arg_buf(
napi_env env,
napi_value value,
uint8_t** buffer,
uint32_t* buffer_length
) {
assert(value != NULL);
assert(*buffer == NULL);
assert(*buffer_length == 0);
bool is_buffer = 0;
OK(napi_is_buffer(env, value, &is_buffer));
if (!is_buffer) return 0;
size_t length = 0;
OK(napi_get_buffer_info(env, value, (void**) buffer, &length));
assert(*buffer != NULL);
assert(length <= UINT32_MAX);
*buffer_length = length;
return 1;
}
static int arg_int(napi_env env, napi_value value, uint32_t* integer) {
assert(*integer == 0);
double temp = 0;
if (
// We get the value as a double so we can check for NaN, Infinity and float:
// https://github.com/nodejs/node/issues/26323
napi_get_value_double(env, value, &temp) != napi_ok ||
temp < 0 ||
isnan(temp) ||
// Infinity, also prevent UB for double->int cast below:
// https://groups.google.com/forum/#!topic/comp.lang.c/rhPzd4bgKJk
temp > UINT32_MAX ||
// Float:
(double) ((uint32_t) temp) != temp
) {
return 0;
}
*integer = (uint32_t) temp;
return 1;
}
void set_int(
napi_env env,
napi_value object,
const char* name,
const int64_t integer
) {
assert(integer >= 0);
napi_value value;
OK(napi_create_int64(env, integer, &value));
OK(napi_set_named_property(env, object, name, value));
}
void set_method(
napi_env env,
napi_value object,
const char* name,
void* method
) {
napi_value value;
OK(napi_create_function(env, NULL, 0, method, NULL, &value));
OK(napi_set_named_property(env, object, name, value));
}
struct task_data {
uint8_t* context;
uint32_t contextSize;
uint32_t sources;
uint32_t targets;
uint8_t* buffer;
uint32_t bufferSize;
uint8_t* parity;
uint32_t paritySize;
uint32_t shardSize;
napi_ref ref_context;
napi_ref ref_buffer;
napi_ref ref_parity;
napi_ref ref_callback;
napi_async_work async_work;
};
void task_execute(napi_env env, void* data) {
struct task_data* task = data;
assert(task->context != NULL);
assert(task->contextSize > 3);
assert(task->buffer != NULL);
assert(task->parity != NULL);
assert(task->shardSize > 0);
const int w = task->context[0];
assert(w <= MAX_W);
assert(w == 2 || w == 4 || w == 8);
const int k = task->context[1];
assert(k >= 1);
assert(k <= MAX_K);
const int m = task->context[2];
assert(m >= 1);
assert(m <= MAX_M);
assert(k + m <= (1 << w));
assert(task->contextSize == (uint32_t) (3 + k * w * m * w));
const uint8_t* bitmatrix = task->context + 3;
uint8_t* shards[MAX_K + MAX_M];
assert(task->shardSize * k <= task->bufferSize);
for (int index = 0; index < k; index++) {
shards[index] = task->buffer + task->shardSize * index;
}
assert(task->shardSize * m <= task->paritySize);
for (int index = 0; index < m; index++) {
shards[index + k] = task->parity + task->shardSize * index;
}
reed_solomon_encode(
w,
k,
m,
bitmatrix,
task->sources,
task->targets,
shards,
task->shardSize
);
}
void task_complete(napi_env env, napi_status status, void* data) {
struct task_data* task = data;
assert(status == napi_ok);
napi_value scope;
OK(napi_get_global(env, &scope));
napi_value callback;
OK(napi_get_reference_value(env, task->ref_callback, &callback));
// Do not assert the return status of napi_call_function():
// If the callback throws then the return status will not be napi_ok.
napi_call_function(env, scope, callback, 0, NULL, NULL);
assert(task->ref_context != NULL);
assert(task->ref_buffer != NULL);
assert(task->ref_parity != NULL);
assert(task->ref_callback != NULL);
assert(task->async_work != NULL);
OK(napi_delete_reference(env, task->ref_context));
OK(napi_delete_reference(env, task->ref_buffer));
OK(napi_delete_reference(env, task->ref_parity));
OK(napi_delete_reference(env, task->ref_callback));
OK(napi_delete_async_work(env, task->async_work));
free(task);
task = NULL;
}
static napi_value create(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value argv[2];
OK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
uint32_t ku = 0;
uint32_t mu = 0;
if (argc != 2 || !arg_int(env, argv[0], &ku) || !arg_int(env, argv[1], &mu)) {
THROW(env, "bad arguments, expected: (int k, int m)");
}
if (ku < 1) THROW(env, "k < 1");
if (ku > MAX_K) THROW(env, "k > MAX_K");
if (mu < 1) THROW(env, "m < 1");
if (mu > MAX_M) THROW(env, "m > MAX_M");
assert(MAX_K <= INT_MAX);
assert(MAX_M <= INT_MAX);
int k = (int) ku;
int m = (int) mu;
assert(sizeof(PARAMETERS) == MAX_K * MAX_M * 7 * sizeof(int));
assert(PARAMETERS[k - 1][m - 1][0] == k);
assert(PARAMETERS[k - 1][m - 1][1] == m);
int w = PARAMETERS[k - 1][m - 1][2];
assert(w <= MAX_W);