forked from BLAKE3-team/BLAKE3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blake3_avx512.c
1207 lines (1114 loc) · 46.9 KB
/
blake3_avx512.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 "blake3_impl.h"
#include <immintrin.h>
#define _mm_shuffle_ps2(a, b, c) \
(_mm_castps_si128( \
_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (c))))
INLINE __m128i loadu_128(const uint8_t src[16]) {
return _mm_loadu_si128((const __m128i *)src);
}
INLINE __m256i loadu_256(const uint8_t src[32]) {
return _mm256_loadu_si256((const __m256i *)src);
}
INLINE __m512i loadu_512(const uint8_t src[64]) {
return _mm512_loadu_si512((const __m512i *)src);
}
INLINE void storeu_128(__m128i src, uint8_t dest[16]) {
_mm_storeu_si128((__m128i *)dest, src);
}
INLINE void storeu_256(__m256i src, uint8_t dest[16]) {
_mm256_storeu_si256((__m256i *)dest, src);
}
INLINE __m128i add_128(__m128i a, __m128i b) { return _mm_add_epi32(a, b); }
INLINE __m256i add_256(__m256i a, __m256i b) { return _mm256_add_epi32(a, b); }
INLINE __m512i add_512(__m512i a, __m512i b) { return _mm512_add_epi32(a, b); }
INLINE __m128i xor_128(__m128i a, __m128i b) { return _mm_xor_si128(a, b); }
INLINE __m256i xor_256(__m256i a, __m256i b) { return _mm256_xor_si256(a, b); }
INLINE __m512i xor_512(__m512i a, __m512i b) { return _mm512_xor_si512(a, b); }
INLINE __m128i set1_128(uint32_t x) { return _mm_set1_epi32((int32_t)x); }
INLINE __m256i set1_256(uint32_t x) { return _mm256_set1_epi32((int32_t)x); }
INLINE __m512i set1_512(uint32_t x) { return _mm512_set1_epi32((int32_t)x); }
INLINE __m128i set4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {
return _mm_setr_epi32((int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d);
}
INLINE __m128i rot16_128(__m128i x) { return _mm_ror_epi32(x, 16); }
INLINE __m256i rot16_256(__m256i x) { return _mm256_ror_epi32(x, 16); }
INLINE __m512i rot16_512(__m512i x) { return _mm512_ror_epi32(x, 16); }
INLINE __m128i rot12_128(__m128i x) { return _mm_ror_epi32(x, 12); }
INLINE __m256i rot12_256(__m256i x) { return _mm256_ror_epi32(x, 12); }
INLINE __m512i rot12_512(__m512i x) { return _mm512_ror_epi32(x, 12); }
INLINE __m128i rot8_128(__m128i x) { return _mm_ror_epi32(x, 8); }
INLINE __m256i rot8_256(__m256i x) { return _mm256_ror_epi32(x, 8); }
INLINE __m512i rot8_512(__m512i x) { return _mm512_ror_epi32(x, 8); }
INLINE __m128i rot7_128(__m128i x) { return _mm_ror_epi32(x, 7); }
INLINE __m256i rot7_256(__m256i x) { return _mm256_ror_epi32(x, 7); }
INLINE __m512i rot7_512(__m512i x) { return _mm512_ror_epi32(x, 7); }
/*
* ----------------------------------------------------------------------------
* compress_avx512
* ----------------------------------------------------------------------------
*/
INLINE void g1(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,
__m128i m) {
*row0 = add_128(add_128(*row0, m), *row1);
*row3 = xor_128(*row3, *row0);
*row3 = rot16_128(*row3);
*row2 = add_128(*row2, *row3);
*row1 = xor_128(*row1, *row2);
*row1 = rot12_128(*row1);
}
INLINE void g2(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,
__m128i m) {
*row0 = add_128(add_128(*row0, m), *row1);
*row3 = xor_128(*row3, *row0);
*row3 = rot8_128(*row3);
*row2 = add_128(*row2, *row3);
*row1 = xor_128(*row1, *row2);
*row1 = rot7_128(*row1);
}
// Note the optimization here of leaving row1 as the unrotated row, rather than
// row0. All the message loads below are adjusted to compensate for this. See
// discussion at https://github.com/sneves/blake2-avx2/pull/4
INLINE void diagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {
*row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(2, 1, 0, 3));
*row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));
*row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(0, 3, 2, 1));
}
INLINE void undiagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {
*row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(0, 3, 2, 1));
*row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));
*row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(2, 1, 0, 3));
}
INLINE void compress_pre(__m128i rows[4], const uint32_t cv[8],
const uint8_t block[BLAKE3_BLOCK_LEN],
uint8_t block_len, uint64_t counter, uint8_t flags) {
rows[0] = loadu_128((uint8_t *)&cv[0]);
rows[1] = loadu_128((uint8_t *)&cv[4]);
rows[2] = set4(IV[0], IV[1], IV[2], IV[3]);
rows[3] = set4(counter_low(counter), counter_high(counter),
(uint32_t)block_len, (uint32_t)flags);
__m128i m0 = loadu_128(&block[sizeof(__m128i) * 0]);
__m128i m1 = loadu_128(&block[sizeof(__m128i) * 1]);
__m128i m2 = loadu_128(&block[sizeof(__m128i) * 2]);
__m128i m3 = loadu_128(&block[sizeof(__m128i) * 3]);
__m128i t0, t1, t2, t3, tt;
// Round 1. The first round permutes the message words from the original
// input order, into the groups that get mixed in parallel.
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(2, 0, 2, 0)); // 6 4 2 0
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 3, 1)); // 7 5 3 1
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(2, 0, 2, 0)); // 14 12 10 8
t2 = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2, 1, 0, 3)); // 12 10 8 14
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 1, 3, 1)); // 15 13 11 9
t3 = _mm_shuffle_epi32(t3, _MM_SHUFFLE(2, 1, 0, 3)); // 13 11 9 15
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
m0 = t0;
m1 = t1;
m2 = t2;
m3 = t3;
// Round 2. This round and all following rounds apply a fixed permutation
// to the message words from the round before.
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
t1 = _mm_blend_epi16(tt, t1, 0xCC);
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_unpacklo_epi64(m3, m1);
tt = _mm_blend_epi16(t2, m2, 0xC0);
t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_unpackhi_epi32(m1, m3);
tt = _mm_unpacklo_epi32(m2, t3);
t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
m0 = t0;
m1 = t1;
m2 = t2;
m3 = t3;
// Round 3
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
t1 = _mm_blend_epi16(tt, t1, 0xCC);
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_unpacklo_epi64(m3, m1);
tt = _mm_blend_epi16(t2, m2, 0xC0);
t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_unpackhi_epi32(m1, m3);
tt = _mm_unpacklo_epi32(m2, t3);
t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
m0 = t0;
m1 = t1;
m2 = t2;
m3 = t3;
// Round 4
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
t1 = _mm_blend_epi16(tt, t1, 0xCC);
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_unpacklo_epi64(m3, m1);
tt = _mm_blend_epi16(t2, m2, 0xC0);
t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_unpackhi_epi32(m1, m3);
tt = _mm_unpacklo_epi32(m2, t3);
t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
m0 = t0;
m1 = t1;
m2 = t2;
m3 = t3;
// Round 5
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
t1 = _mm_blend_epi16(tt, t1, 0xCC);
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_unpacklo_epi64(m3, m1);
tt = _mm_blend_epi16(t2, m2, 0xC0);
t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_unpackhi_epi32(m1, m3);
tt = _mm_unpacklo_epi32(m2, t3);
t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
m0 = t0;
m1 = t1;
m2 = t2;
m3 = t3;
// Round 6
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
t1 = _mm_blend_epi16(tt, t1, 0xCC);
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_unpacklo_epi64(m3, m1);
tt = _mm_blend_epi16(t2, m2, 0xC0);
t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_unpackhi_epi32(m1, m3);
tt = _mm_unpacklo_epi32(m2, t3);
t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
m0 = t0;
m1 = t1;
m2 = t2;
m3 = t3;
// Round 7
t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
t1 = _mm_blend_epi16(tt, t1, 0xCC);
g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
diagonalize(&rows[0], &rows[2], &rows[3]);
t2 = _mm_unpacklo_epi64(m3, m1);
tt = _mm_blend_epi16(t2, m2, 0xC0);
t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
t3 = _mm_unpackhi_epi32(m1, m3);
tt = _mm_unpacklo_epi32(m2, t3);
t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
undiagonalize(&rows[0], &rows[2], &rows[3]);
}
void blake3_compress_xof_avx512(const uint32_t cv[8],
const uint8_t block[BLAKE3_BLOCK_LEN],
uint8_t block_len, uint64_t counter,
uint8_t flags, uint8_t out[64]) {
__m128i rows[4];
compress_pre(rows, cv, block, block_len, counter, flags);
storeu_128(xor_128(rows[0], rows[2]), &out[0]);
storeu_128(xor_128(rows[1], rows[3]), &out[16]);
storeu_128(xor_128(rows[2], loadu_128((uint8_t *)&cv[0])), &out[32]);
storeu_128(xor_128(rows[3], loadu_128((uint8_t *)&cv[4])), &out[48]);
}
void blake3_compress_in_place_avx512(uint32_t cv[8],
const uint8_t block[BLAKE3_BLOCK_LEN],
uint8_t block_len, uint64_t counter,
uint8_t flags) {
__m128i rows[4];
compress_pre(rows, cv, block, block_len, counter, flags);
storeu_128(xor_128(rows[0], rows[2]), (uint8_t *)&cv[0]);
storeu_128(xor_128(rows[1], rows[3]), (uint8_t *)&cv[4]);
}
/*
* ----------------------------------------------------------------------------
* hash4_avx512
* ----------------------------------------------------------------------------
*/
INLINE void round_fn4(__m128i v[16], __m128i m[16], size_t r) {
v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);
v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);
v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);
v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);
v[0] = add_128(v[0], v[4]);
v[1] = add_128(v[1], v[5]);
v[2] = add_128(v[2], v[6]);
v[3] = add_128(v[3], v[7]);
v[12] = xor_128(v[12], v[0]);
v[13] = xor_128(v[13], v[1]);
v[14] = xor_128(v[14], v[2]);
v[15] = xor_128(v[15], v[3]);
v[12] = rot16_128(v[12]);
v[13] = rot16_128(v[13]);
v[14] = rot16_128(v[14]);
v[15] = rot16_128(v[15]);
v[8] = add_128(v[8], v[12]);
v[9] = add_128(v[9], v[13]);
v[10] = add_128(v[10], v[14]);
v[11] = add_128(v[11], v[15]);
v[4] = xor_128(v[4], v[8]);
v[5] = xor_128(v[5], v[9]);
v[6] = xor_128(v[6], v[10]);
v[7] = xor_128(v[7], v[11]);
v[4] = rot12_128(v[4]);
v[5] = rot12_128(v[5]);
v[6] = rot12_128(v[6]);
v[7] = rot12_128(v[7]);
v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);
v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);
v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);
v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);
v[0] = add_128(v[0], v[4]);
v[1] = add_128(v[1], v[5]);
v[2] = add_128(v[2], v[6]);
v[3] = add_128(v[3], v[7]);
v[12] = xor_128(v[12], v[0]);
v[13] = xor_128(v[13], v[1]);
v[14] = xor_128(v[14], v[2]);
v[15] = xor_128(v[15], v[3]);
v[12] = rot8_128(v[12]);
v[13] = rot8_128(v[13]);
v[14] = rot8_128(v[14]);
v[15] = rot8_128(v[15]);
v[8] = add_128(v[8], v[12]);
v[9] = add_128(v[9], v[13]);
v[10] = add_128(v[10], v[14]);
v[11] = add_128(v[11], v[15]);
v[4] = xor_128(v[4], v[8]);
v[5] = xor_128(v[5], v[9]);
v[6] = xor_128(v[6], v[10]);
v[7] = xor_128(v[7], v[11]);
v[4] = rot7_128(v[4]);
v[5] = rot7_128(v[5]);
v[6] = rot7_128(v[6]);
v[7] = rot7_128(v[7]);
v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);
v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);
v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);
v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);
v[0] = add_128(v[0], v[5]);
v[1] = add_128(v[1], v[6]);
v[2] = add_128(v[2], v[7]);
v[3] = add_128(v[3], v[4]);
v[15] = xor_128(v[15], v[0]);
v[12] = xor_128(v[12], v[1]);
v[13] = xor_128(v[13], v[2]);
v[14] = xor_128(v[14], v[3]);
v[15] = rot16_128(v[15]);
v[12] = rot16_128(v[12]);
v[13] = rot16_128(v[13]);
v[14] = rot16_128(v[14]);
v[10] = add_128(v[10], v[15]);
v[11] = add_128(v[11], v[12]);
v[8] = add_128(v[8], v[13]);
v[9] = add_128(v[9], v[14]);
v[5] = xor_128(v[5], v[10]);
v[6] = xor_128(v[6], v[11]);
v[7] = xor_128(v[7], v[8]);
v[4] = xor_128(v[4], v[9]);
v[5] = rot12_128(v[5]);
v[6] = rot12_128(v[6]);
v[7] = rot12_128(v[7]);
v[4] = rot12_128(v[4]);
v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);
v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);
v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);
v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);
v[0] = add_128(v[0], v[5]);
v[1] = add_128(v[1], v[6]);
v[2] = add_128(v[2], v[7]);
v[3] = add_128(v[3], v[4]);
v[15] = xor_128(v[15], v[0]);
v[12] = xor_128(v[12], v[1]);
v[13] = xor_128(v[13], v[2]);
v[14] = xor_128(v[14], v[3]);
v[15] = rot8_128(v[15]);
v[12] = rot8_128(v[12]);
v[13] = rot8_128(v[13]);
v[14] = rot8_128(v[14]);
v[10] = add_128(v[10], v[15]);
v[11] = add_128(v[11], v[12]);
v[8] = add_128(v[8], v[13]);
v[9] = add_128(v[9], v[14]);
v[5] = xor_128(v[5], v[10]);
v[6] = xor_128(v[6], v[11]);
v[7] = xor_128(v[7], v[8]);
v[4] = xor_128(v[4], v[9]);
v[5] = rot7_128(v[5]);
v[6] = rot7_128(v[6]);
v[7] = rot7_128(v[7]);
v[4] = rot7_128(v[4]);
}
INLINE void transpose_vecs_128(__m128i vecs[4]) {
// Interleave 32-bit lates. The low unpack is lanes 00/11 and the high is
// 22/33. Note that this doesn't split the vector into two lanes, as the
// AVX2 counterparts do.
__m128i ab_01 = _mm_unpacklo_epi32(vecs[0], vecs[1]);
__m128i ab_23 = _mm_unpackhi_epi32(vecs[0], vecs[1]);
__m128i cd_01 = _mm_unpacklo_epi32(vecs[2], vecs[3]);
__m128i cd_23 = _mm_unpackhi_epi32(vecs[2], vecs[3]);
// Interleave 64-bit lanes.
__m128i abcd_0 = _mm_unpacklo_epi64(ab_01, cd_01);
__m128i abcd_1 = _mm_unpackhi_epi64(ab_01, cd_01);
__m128i abcd_2 = _mm_unpacklo_epi64(ab_23, cd_23);
__m128i abcd_3 = _mm_unpackhi_epi64(ab_23, cd_23);
vecs[0] = abcd_0;
vecs[1] = abcd_1;
vecs[2] = abcd_2;
vecs[3] = abcd_3;
}
INLINE void transpose_msg_vecs4(const uint8_t *const *inputs,
size_t block_offset, __m128i out[16]) {
out[0] = loadu_128(&inputs[0][block_offset + 0 * sizeof(__m128i)]);
out[1] = loadu_128(&inputs[1][block_offset + 0 * sizeof(__m128i)]);
out[2] = loadu_128(&inputs[2][block_offset + 0 * sizeof(__m128i)]);
out[3] = loadu_128(&inputs[3][block_offset + 0 * sizeof(__m128i)]);
out[4] = loadu_128(&inputs[0][block_offset + 1 * sizeof(__m128i)]);
out[5] = loadu_128(&inputs[1][block_offset + 1 * sizeof(__m128i)]);
out[6] = loadu_128(&inputs[2][block_offset + 1 * sizeof(__m128i)]);
out[7] = loadu_128(&inputs[3][block_offset + 1 * sizeof(__m128i)]);
out[8] = loadu_128(&inputs[0][block_offset + 2 * sizeof(__m128i)]);
out[9] = loadu_128(&inputs[1][block_offset + 2 * sizeof(__m128i)]);
out[10] = loadu_128(&inputs[2][block_offset + 2 * sizeof(__m128i)]);
out[11] = loadu_128(&inputs[3][block_offset + 2 * sizeof(__m128i)]);
out[12] = loadu_128(&inputs[0][block_offset + 3 * sizeof(__m128i)]);
out[13] = loadu_128(&inputs[1][block_offset + 3 * sizeof(__m128i)]);
out[14] = loadu_128(&inputs[2][block_offset + 3 * sizeof(__m128i)]);
out[15] = loadu_128(&inputs[3][block_offset + 3 * sizeof(__m128i)]);
for (size_t i = 0; i < 4; ++i) {
_mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);
}
transpose_vecs_128(&out[0]);
transpose_vecs_128(&out[4]);
transpose_vecs_128(&out[8]);
transpose_vecs_128(&out[12]);
}
INLINE void load_counters4(uint64_t counter, bool increment_counter,
__m128i *out_lo, __m128i *out_hi) {
uint64_t mask = (increment_counter ? ~0 : 0);
__m256i mask_vec = _mm256_set1_epi64x(mask);
__m256i deltas = _mm256_setr_epi64x(0, 1, 2, 3);
deltas = _mm256_and_si256(mask_vec, deltas);
__m256i counters =
_mm256_add_epi64(_mm256_set1_epi64x((int64_t)counter), deltas);
*out_lo = _mm256_cvtepi64_epi32(counters);
*out_hi = _mm256_cvtepi64_epi32(_mm256_srli_epi64(counters, 32));
}
static
void blake3_hash4_avx512(const uint8_t *const *inputs, size_t blocks,
const uint32_t key[8], uint64_t counter,
bool increment_counter, uint8_t flags,
uint8_t flags_start, uint8_t flags_end, uint8_t *out) {
__m128i h_vecs[8] = {
set1_128(key[0]), set1_128(key[1]), set1_128(key[2]), set1_128(key[3]),
set1_128(key[4]), set1_128(key[5]), set1_128(key[6]), set1_128(key[7]),
};
__m128i counter_low_vec, counter_high_vec;
load_counters4(counter, increment_counter, &counter_low_vec,
&counter_high_vec);
uint8_t block_flags = flags | flags_start;
for (size_t block = 0; block < blocks; block++) {
if (block + 1 == blocks) {
block_flags |= flags_end;
}
__m128i block_len_vec = set1_128(BLAKE3_BLOCK_LEN);
__m128i block_flags_vec = set1_128(block_flags);
__m128i msg_vecs[16];
transpose_msg_vecs4(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);
__m128i v[16] = {
h_vecs[0], h_vecs[1], h_vecs[2], h_vecs[3],
h_vecs[4], h_vecs[5], h_vecs[6], h_vecs[7],
set1_128(IV[0]), set1_128(IV[1]), set1_128(IV[2]), set1_128(IV[3]),
counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,
};
round_fn4(v, msg_vecs, 0);
round_fn4(v, msg_vecs, 1);
round_fn4(v, msg_vecs, 2);
round_fn4(v, msg_vecs, 3);
round_fn4(v, msg_vecs, 4);
round_fn4(v, msg_vecs, 5);
round_fn4(v, msg_vecs, 6);
h_vecs[0] = xor_128(v[0], v[8]);
h_vecs[1] = xor_128(v[1], v[9]);
h_vecs[2] = xor_128(v[2], v[10]);
h_vecs[3] = xor_128(v[3], v[11]);
h_vecs[4] = xor_128(v[4], v[12]);
h_vecs[5] = xor_128(v[5], v[13]);
h_vecs[6] = xor_128(v[6], v[14]);
h_vecs[7] = xor_128(v[7], v[15]);
block_flags = flags;
}
transpose_vecs_128(&h_vecs[0]);
transpose_vecs_128(&h_vecs[4]);
// The first four vecs now contain the first half of each output, and the
// second four vecs contain the second half of each output.
storeu_128(h_vecs[0], &out[0 * sizeof(__m128i)]);
storeu_128(h_vecs[4], &out[1 * sizeof(__m128i)]);
storeu_128(h_vecs[1], &out[2 * sizeof(__m128i)]);
storeu_128(h_vecs[5], &out[3 * sizeof(__m128i)]);
storeu_128(h_vecs[2], &out[4 * sizeof(__m128i)]);
storeu_128(h_vecs[6], &out[5 * sizeof(__m128i)]);
storeu_128(h_vecs[3], &out[6 * sizeof(__m128i)]);
storeu_128(h_vecs[7], &out[7 * sizeof(__m128i)]);
}
/*
* ----------------------------------------------------------------------------
* hash8_avx512
* ----------------------------------------------------------------------------
*/
INLINE void round_fn8(__m256i v[16], __m256i m[16], size_t r) {
v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);
v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);
v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);
v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);
v[0] = add_256(v[0], v[4]);
v[1] = add_256(v[1], v[5]);
v[2] = add_256(v[2], v[6]);
v[3] = add_256(v[3], v[7]);
v[12] = xor_256(v[12], v[0]);
v[13] = xor_256(v[13], v[1]);
v[14] = xor_256(v[14], v[2]);
v[15] = xor_256(v[15], v[3]);
v[12] = rot16_256(v[12]);
v[13] = rot16_256(v[13]);
v[14] = rot16_256(v[14]);
v[15] = rot16_256(v[15]);
v[8] = add_256(v[8], v[12]);
v[9] = add_256(v[9], v[13]);
v[10] = add_256(v[10], v[14]);
v[11] = add_256(v[11], v[15]);
v[4] = xor_256(v[4], v[8]);
v[5] = xor_256(v[5], v[9]);
v[6] = xor_256(v[6], v[10]);
v[7] = xor_256(v[7], v[11]);
v[4] = rot12_256(v[4]);
v[5] = rot12_256(v[5]);
v[6] = rot12_256(v[6]);
v[7] = rot12_256(v[7]);
v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);
v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);
v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);
v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);
v[0] = add_256(v[0], v[4]);
v[1] = add_256(v[1], v[5]);
v[2] = add_256(v[2], v[6]);
v[3] = add_256(v[3], v[7]);
v[12] = xor_256(v[12], v[0]);
v[13] = xor_256(v[13], v[1]);
v[14] = xor_256(v[14], v[2]);
v[15] = xor_256(v[15], v[3]);
v[12] = rot8_256(v[12]);
v[13] = rot8_256(v[13]);
v[14] = rot8_256(v[14]);
v[15] = rot8_256(v[15]);
v[8] = add_256(v[8], v[12]);
v[9] = add_256(v[9], v[13]);
v[10] = add_256(v[10], v[14]);
v[11] = add_256(v[11], v[15]);
v[4] = xor_256(v[4], v[8]);
v[5] = xor_256(v[5], v[9]);
v[6] = xor_256(v[6], v[10]);
v[7] = xor_256(v[7], v[11]);
v[4] = rot7_256(v[4]);
v[5] = rot7_256(v[5]);
v[6] = rot7_256(v[6]);
v[7] = rot7_256(v[7]);
v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);
v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);
v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);
v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);
v[0] = add_256(v[0], v[5]);
v[1] = add_256(v[1], v[6]);
v[2] = add_256(v[2], v[7]);
v[3] = add_256(v[3], v[4]);
v[15] = xor_256(v[15], v[0]);
v[12] = xor_256(v[12], v[1]);
v[13] = xor_256(v[13], v[2]);
v[14] = xor_256(v[14], v[3]);
v[15] = rot16_256(v[15]);
v[12] = rot16_256(v[12]);
v[13] = rot16_256(v[13]);
v[14] = rot16_256(v[14]);
v[10] = add_256(v[10], v[15]);
v[11] = add_256(v[11], v[12]);
v[8] = add_256(v[8], v[13]);
v[9] = add_256(v[9], v[14]);
v[5] = xor_256(v[5], v[10]);
v[6] = xor_256(v[6], v[11]);
v[7] = xor_256(v[7], v[8]);
v[4] = xor_256(v[4], v[9]);
v[5] = rot12_256(v[5]);
v[6] = rot12_256(v[6]);
v[7] = rot12_256(v[7]);
v[4] = rot12_256(v[4]);
v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);
v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);
v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);
v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);
v[0] = add_256(v[0], v[5]);
v[1] = add_256(v[1], v[6]);
v[2] = add_256(v[2], v[7]);
v[3] = add_256(v[3], v[4]);
v[15] = xor_256(v[15], v[0]);
v[12] = xor_256(v[12], v[1]);
v[13] = xor_256(v[13], v[2]);
v[14] = xor_256(v[14], v[3]);
v[15] = rot8_256(v[15]);
v[12] = rot8_256(v[12]);
v[13] = rot8_256(v[13]);
v[14] = rot8_256(v[14]);
v[10] = add_256(v[10], v[15]);
v[11] = add_256(v[11], v[12]);
v[8] = add_256(v[8], v[13]);
v[9] = add_256(v[9], v[14]);
v[5] = xor_256(v[5], v[10]);
v[6] = xor_256(v[6], v[11]);
v[7] = xor_256(v[7], v[8]);
v[4] = xor_256(v[4], v[9]);
v[5] = rot7_256(v[5]);
v[6] = rot7_256(v[6]);
v[7] = rot7_256(v[7]);
v[4] = rot7_256(v[4]);
}
INLINE void transpose_vecs_256(__m256i vecs[8]) {
// Interleave 32-bit lanes. The low unpack is lanes 00/11/44/55, and the high
// is 22/33/66/77.
__m256i ab_0145 = _mm256_unpacklo_epi32(vecs[0], vecs[1]);
__m256i ab_2367 = _mm256_unpackhi_epi32(vecs[0], vecs[1]);
__m256i cd_0145 = _mm256_unpacklo_epi32(vecs[2], vecs[3]);
__m256i cd_2367 = _mm256_unpackhi_epi32(vecs[2], vecs[3]);
__m256i ef_0145 = _mm256_unpacklo_epi32(vecs[4], vecs[5]);
__m256i ef_2367 = _mm256_unpackhi_epi32(vecs[4], vecs[5]);
__m256i gh_0145 = _mm256_unpacklo_epi32(vecs[6], vecs[7]);
__m256i gh_2367 = _mm256_unpackhi_epi32(vecs[6], vecs[7]);
// Interleave 64-bit lates. The low unpack is lanes 00/22 and the high is
// 11/33.
__m256i abcd_04 = _mm256_unpacklo_epi64(ab_0145, cd_0145);
__m256i abcd_15 = _mm256_unpackhi_epi64(ab_0145, cd_0145);
__m256i abcd_26 = _mm256_unpacklo_epi64(ab_2367, cd_2367);
__m256i abcd_37 = _mm256_unpackhi_epi64(ab_2367, cd_2367);
__m256i efgh_04 = _mm256_unpacklo_epi64(ef_0145, gh_0145);
__m256i efgh_15 = _mm256_unpackhi_epi64(ef_0145, gh_0145);
__m256i efgh_26 = _mm256_unpacklo_epi64(ef_2367, gh_2367);
__m256i efgh_37 = _mm256_unpackhi_epi64(ef_2367, gh_2367);
// Interleave 128-bit lanes.
vecs[0] = _mm256_permute2x128_si256(abcd_04, efgh_04, 0x20);
vecs[1] = _mm256_permute2x128_si256(abcd_15, efgh_15, 0x20);
vecs[2] = _mm256_permute2x128_si256(abcd_26, efgh_26, 0x20);
vecs[3] = _mm256_permute2x128_si256(abcd_37, efgh_37, 0x20);
vecs[4] = _mm256_permute2x128_si256(abcd_04, efgh_04, 0x31);
vecs[5] = _mm256_permute2x128_si256(abcd_15, efgh_15, 0x31);
vecs[6] = _mm256_permute2x128_si256(abcd_26, efgh_26, 0x31);
vecs[7] = _mm256_permute2x128_si256(abcd_37, efgh_37, 0x31);
}
INLINE void transpose_msg_vecs8(const uint8_t *const *inputs,
size_t block_offset, __m256i out[16]) {
out[0] = loadu_256(&inputs[0][block_offset + 0 * sizeof(__m256i)]);
out[1] = loadu_256(&inputs[1][block_offset + 0 * sizeof(__m256i)]);
out[2] = loadu_256(&inputs[2][block_offset + 0 * sizeof(__m256i)]);
out[3] = loadu_256(&inputs[3][block_offset + 0 * sizeof(__m256i)]);
out[4] = loadu_256(&inputs[4][block_offset + 0 * sizeof(__m256i)]);
out[5] = loadu_256(&inputs[5][block_offset + 0 * sizeof(__m256i)]);
out[6] = loadu_256(&inputs[6][block_offset + 0 * sizeof(__m256i)]);
out[7] = loadu_256(&inputs[7][block_offset + 0 * sizeof(__m256i)]);
out[8] = loadu_256(&inputs[0][block_offset + 1 * sizeof(__m256i)]);
out[9] = loadu_256(&inputs[1][block_offset + 1 * sizeof(__m256i)]);
out[10] = loadu_256(&inputs[2][block_offset + 1 * sizeof(__m256i)]);
out[11] = loadu_256(&inputs[3][block_offset + 1 * sizeof(__m256i)]);
out[12] = loadu_256(&inputs[4][block_offset + 1 * sizeof(__m256i)]);
out[13] = loadu_256(&inputs[5][block_offset + 1 * sizeof(__m256i)]);
out[14] = loadu_256(&inputs[6][block_offset + 1 * sizeof(__m256i)]);
out[15] = loadu_256(&inputs[7][block_offset + 1 * sizeof(__m256i)]);
for (size_t i = 0; i < 8; ++i) {
_mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);
}
transpose_vecs_256(&out[0]);
transpose_vecs_256(&out[8]);
}
INLINE void load_counters8(uint64_t counter, bool increment_counter,
__m256i *out_lo, __m256i *out_hi) {
uint64_t mask = (increment_counter ? ~0 : 0);
__m512i mask_vec = _mm512_set1_epi64(mask);
__m512i deltas = _mm512_setr_epi64(0, 1, 2, 3, 4, 5, 6, 7);
deltas = _mm512_and_si512(mask_vec, deltas);
__m512i counters =
_mm512_add_epi64(_mm512_set1_epi64((int64_t)counter), deltas);
*out_lo = _mm512_cvtepi64_epi32(counters);
*out_hi = _mm512_cvtepi64_epi32(_mm512_srli_epi64(counters, 32));
}
static
void blake3_hash8_avx512(const uint8_t *const *inputs, size_t blocks,
const uint32_t key[8], uint64_t counter,
bool increment_counter, uint8_t flags,
uint8_t flags_start, uint8_t flags_end, uint8_t *out) {
__m256i h_vecs[8] = {
set1_256(key[0]), set1_256(key[1]), set1_256(key[2]), set1_256(key[3]),
set1_256(key[4]), set1_256(key[5]), set1_256(key[6]), set1_256(key[7]),
};
__m256i counter_low_vec, counter_high_vec;
load_counters8(counter, increment_counter, &counter_low_vec,
&counter_high_vec);
uint8_t block_flags = flags | flags_start;
for (size_t block = 0; block < blocks; block++) {
if (block + 1 == blocks) {
block_flags |= flags_end;
}
__m256i block_len_vec = set1_256(BLAKE3_BLOCK_LEN);
__m256i block_flags_vec = set1_256(block_flags);
__m256i msg_vecs[16];
transpose_msg_vecs8(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);
__m256i v[16] = {
h_vecs[0], h_vecs[1], h_vecs[2], h_vecs[3],
h_vecs[4], h_vecs[5], h_vecs[6], h_vecs[7],
set1_256(IV[0]), set1_256(IV[1]), set1_256(IV[2]), set1_256(IV[3]),
counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,
};
round_fn8(v, msg_vecs, 0);
round_fn8(v, msg_vecs, 1);
round_fn8(v, msg_vecs, 2);
round_fn8(v, msg_vecs, 3);
round_fn8(v, msg_vecs, 4);
round_fn8(v, msg_vecs, 5);
round_fn8(v, msg_vecs, 6);
h_vecs[0] = xor_256(v[0], v[8]);
h_vecs[1] = xor_256(v[1], v[9]);
h_vecs[2] = xor_256(v[2], v[10]);
h_vecs[3] = xor_256(v[3], v[11]);
h_vecs[4] = xor_256(v[4], v[12]);
h_vecs[5] = xor_256(v[5], v[13]);
h_vecs[6] = xor_256(v[6], v[14]);
h_vecs[7] = xor_256(v[7], v[15]);
block_flags = flags;
}
transpose_vecs_256(h_vecs);
storeu_256(h_vecs[0], &out[0 * sizeof(__m256i)]);
storeu_256(h_vecs[1], &out[1 * sizeof(__m256i)]);
storeu_256(h_vecs[2], &out[2 * sizeof(__m256i)]);
storeu_256(h_vecs[3], &out[3 * sizeof(__m256i)]);
storeu_256(h_vecs[4], &out[4 * sizeof(__m256i)]);
storeu_256(h_vecs[5], &out[5 * sizeof(__m256i)]);
storeu_256(h_vecs[6], &out[6 * sizeof(__m256i)]);
storeu_256(h_vecs[7], &out[7 * sizeof(__m256i)]);
}
/*
* ----------------------------------------------------------------------------
* hash16_avx512
* ----------------------------------------------------------------------------
*/
INLINE void round_fn16(__m512i v[16], __m512i m[16], size_t r) {
v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);
v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);
v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);
v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);
v[0] = add_512(v[0], v[4]);
v[1] = add_512(v[1], v[5]);
v[2] = add_512(v[2], v[6]);
v[3] = add_512(v[3], v[7]);
v[12] = xor_512(v[12], v[0]);
v[13] = xor_512(v[13], v[1]);
v[14] = xor_512(v[14], v[2]);
v[15] = xor_512(v[15], v[3]);
v[12] = rot16_512(v[12]);
v[13] = rot16_512(v[13]);
v[14] = rot16_512(v[14]);
v[15] = rot16_512(v[15]);
v[8] = add_512(v[8], v[12]);
v[9] = add_512(v[9], v[13]);
v[10] = add_512(v[10], v[14]);
v[11] = add_512(v[11], v[15]);
v[4] = xor_512(v[4], v[8]);
v[5] = xor_512(v[5], v[9]);
v[6] = xor_512(v[6], v[10]);
v[7] = xor_512(v[7], v[11]);
v[4] = rot12_512(v[4]);
v[5] = rot12_512(v[5]);
v[6] = rot12_512(v[6]);
v[7] = rot12_512(v[7]);
v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);
v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);
v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);
v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);
v[0] = add_512(v[0], v[4]);
v[1] = add_512(v[1], v[5]);
v[2] = add_512(v[2], v[6]);
v[3] = add_512(v[3], v[7]);
v[12] = xor_512(v[12], v[0]);
v[13] = xor_512(v[13], v[1]);
v[14] = xor_512(v[14], v[2]);
v[15] = xor_512(v[15], v[3]);
v[12] = rot8_512(v[12]);
v[13] = rot8_512(v[13]);
v[14] = rot8_512(v[14]);
v[15] = rot8_512(v[15]);
v[8] = add_512(v[8], v[12]);
v[9] = add_512(v[9], v[13]);
v[10] = add_512(v[10], v[14]);
v[11] = add_512(v[11], v[15]);
v[4] = xor_512(v[4], v[8]);
v[5] = xor_512(v[5], v[9]);
v[6] = xor_512(v[6], v[10]);
v[7] = xor_512(v[7], v[11]);
v[4] = rot7_512(v[4]);
v[5] = rot7_512(v[5]);
v[6] = rot7_512(v[6]);
v[7] = rot7_512(v[7]);
v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);
v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);
v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);
v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);
v[0] = add_512(v[0], v[5]);
v[1] = add_512(v[1], v[6]);
v[2] = add_512(v[2], v[7]);
v[3] = add_512(v[3], v[4]);
v[15] = xor_512(v[15], v[0]);
v[12] = xor_512(v[12], v[1]);
v[13] = xor_512(v[13], v[2]);
v[14] = xor_512(v[14], v[3]);
v[15] = rot16_512(v[15]);
v[12] = rot16_512(v[12]);
v[13] = rot16_512(v[13]);
v[14] = rot16_512(v[14]);
v[10] = add_512(v[10], v[15]);
v[11] = add_512(v[11], v[12]);
v[8] = add_512(v[8], v[13]);
v[9] = add_512(v[9], v[14]);
v[5] = xor_512(v[5], v[10]);
v[6] = xor_512(v[6], v[11]);
v[7] = xor_512(v[7], v[8]);
v[4] = xor_512(v[4], v[9]);
v[5] = rot12_512(v[5]);
v[6] = rot12_512(v[6]);
v[7] = rot12_512(v[7]);
v[4] = rot12_512(v[4]);
v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);
v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);
v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);
v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);
v[0] = add_512(v[0], v[5]);
v[1] = add_512(v[1], v[6]);
v[2] = add_512(v[2], v[7]);
v[3] = add_512(v[3], v[4]);
v[15] = xor_512(v[15], v[0]);
v[12] = xor_512(v[12], v[1]);
v[13] = xor_512(v[13], v[2]);
v[14] = xor_512(v[14], v[3]);
v[15] = rot8_512(v[15]);
v[12] = rot8_512(v[12]);
v[13] = rot8_512(v[13]);
v[14] = rot8_512(v[14]);
v[10] = add_512(v[10], v[15]);
v[11] = add_512(v[11], v[12]);
v[8] = add_512(v[8], v[13]);
v[9] = add_512(v[9], v[14]);
v[5] = xor_512(v[5], v[10]);
v[6] = xor_512(v[6], v[11]);
v[7] = xor_512(v[7], v[8]);
v[4] = xor_512(v[4], v[9]);
v[5] = rot7_512(v[5]);
v[6] = rot7_512(v[6]);
v[7] = rot7_512(v[7]);
v[4] = rot7_512(v[4]);
}
// 0b10001000, or lanes a0/a2/b0/b2 in little-endian order
#define LO_IMM8 0x88
INLINE __m512i unpack_lo_128(__m512i a, __m512i b) {
return _mm512_shuffle_i32x4(a, b, LO_IMM8);
}
// 0b11011101, or lanes a1/a3/b1/b3 in little-endian order
#define HI_IMM8 0xdd
INLINE __m512i unpack_hi_128(__m512i a, __m512i b) {
return _mm512_shuffle_i32x4(a, b, HI_IMM8);
}
INLINE void transpose_vecs_512(__m512i vecs[16]) {
// Interleave 32-bit lanes. The _0 unpack is lanes
// 0/0/1/1/4/4/5/5/8/8/9/9/12/12/13/13, and the _2 unpack is lanes
// 2/2/3/3/6/6/7/7/10/10/11/11/14/14/15/15.
__m512i ab_0 = _mm512_unpacklo_epi32(vecs[0], vecs[1]);
__m512i ab_2 = _mm512_unpackhi_epi32(vecs[0], vecs[1]);
__m512i cd_0 = _mm512_unpacklo_epi32(vecs[2], vecs[3]);
__m512i cd_2 = _mm512_unpackhi_epi32(vecs[2], vecs[3]);
__m512i ef_0 = _mm512_unpacklo_epi32(vecs[4], vecs[5]);
__m512i ef_2 = _mm512_unpackhi_epi32(vecs[4], vecs[5]);
__m512i gh_0 = _mm512_unpacklo_epi32(vecs[6], vecs[7]);
__m512i gh_2 = _mm512_unpackhi_epi32(vecs[6], vecs[7]);
__m512i ij_0 = _mm512_unpacklo_epi32(vecs[8], vecs[9]);
__m512i ij_2 = _mm512_unpackhi_epi32(vecs[8], vecs[9]);
__m512i kl_0 = _mm512_unpacklo_epi32(vecs[10], vecs[11]);
__m512i kl_2 = _mm512_unpackhi_epi32(vecs[10], vecs[11]);
__m512i mn_0 = _mm512_unpacklo_epi32(vecs[12], vecs[13]);
__m512i mn_2 = _mm512_unpackhi_epi32(vecs[12], vecs[13]);
__m512i op_0 = _mm512_unpacklo_epi32(vecs[14], vecs[15]);
__m512i op_2 = _mm512_unpackhi_epi32(vecs[14], vecs[15]);
// Interleave 64-bit lates. The _0 unpack is lanes
// 0/0/0/0/4/4/4/4/8/8/8/8/12/12/12/12, the _1 unpack is lanes
// 1/1/1/1/5/5/5/5/9/9/9/9/13/13/13/13, the _2 unpack is lanes
// 2/2/2/2/6/6/6/6/10/10/10/10/14/14/14/14, and the _3 unpack is lanes
// 3/3/3/3/7/7/7/7/11/11/11/11/15/15/15/15.
__m512i abcd_0 = _mm512_unpacklo_epi64(ab_0, cd_0);
__m512i abcd_1 = _mm512_unpackhi_epi64(ab_0, cd_0);
__m512i abcd_2 = _mm512_unpacklo_epi64(ab_2, cd_2);
__m512i abcd_3 = _mm512_unpackhi_epi64(ab_2, cd_2);
__m512i efgh_0 = _mm512_unpacklo_epi64(ef_0, gh_0);
__m512i efgh_1 = _mm512_unpackhi_epi64(ef_0, gh_0);
__m512i efgh_2 = _mm512_unpacklo_epi64(ef_2, gh_2);
__m512i efgh_3 = _mm512_unpackhi_epi64(ef_2, gh_2);
__m512i ijkl_0 = _mm512_unpacklo_epi64(ij_0, kl_0);
__m512i ijkl_1 = _mm512_unpackhi_epi64(ij_0, kl_0);
__m512i ijkl_2 = _mm512_unpacklo_epi64(ij_2, kl_2);
__m512i ijkl_3 = _mm512_unpackhi_epi64(ij_2, kl_2);
__m512i mnop_0 = _mm512_unpacklo_epi64(mn_0, op_0);
__m512i mnop_1 = _mm512_unpackhi_epi64(mn_0, op_0);
__m512i mnop_2 = _mm512_unpacklo_epi64(mn_2, op_2);
__m512i mnop_3 = _mm512_unpackhi_epi64(mn_2, op_2);
// Interleave 128-bit lanes. The _0 unpack is
// 0/0/0/0/8/8/8/8/0/0/0/0/8/8/8/8, the _1 unpack is
// 1/1/1/1/9/9/9/9/1/1/1/1/9/9/9/9, and so on.
__m512i abcdefgh_0 = unpack_lo_128(abcd_0, efgh_0);
__m512i abcdefgh_1 = unpack_lo_128(abcd_1, efgh_1);
__m512i abcdefgh_2 = unpack_lo_128(abcd_2, efgh_2);
__m512i abcdefgh_3 = unpack_lo_128(abcd_3, efgh_3);
__m512i abcdefgh_4 = unpack_hi_128(abcd_0, efgh_0);
__m512i abcdefgh_5 = unpack_hi_128(abcd_1, efgh_1);
__m512i abcdefgh_6 = unpack_hi_128(abcd_2, efgh_2);
__m512i abcdefgh_7 = unpack_hi_128(abcd_3, efgh_3);
__m512i ijklmnop_0 = unpack_lo_128(ijkl_0, mnop_0);
__m512i ijklmnop_1 = unpack_lo_128(ijkl_1, mnop_1);
__m512i ijklmnop_2 = unpack_lo_128(ijkl_2, mnop_2);
__m512i ijklmnop_3 = unpack_lo_128(ijkl_3, mnop_3);
__m512i ijklmnop_4 = unpack_hi_128(ijkl_0, mnop_0);
__m512i ijklmnop_5 = unpack_hi_128(ijkl_1, mnop_1);