-
Notifications
You must be signed in to change notification settings - Fork 6
/
transpose.c
1322 lines (1174 loc) · 62 KB
/
transpose.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
/**
Copyright (C) powturbo 2013-2023
SPDX-License-Identifier: GPL v2 License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- homepage : https://sites.google.com/site/powturbo/
- github : https://github.com/powturbo
- twitter : https://twitter.com/powturbo
- email : powturbo [_AT_] gmail [_DOT_] com
**/
// Nibble/Byte transpose
#ifndef ESIZE //---------------------------------- Functions -----------------------------------------------------------------
#include <string.h>
#include "include_/conf.h"
#include "include_/transpose.h"
#ifdef __AVX2__
#include <immintrin.h>
#elif defined(__AVX__)
#include <immintrin.h>
#elif defined(__SSE4_1__)
#include <smmintrin.h>
#elif defined(__SSSE3__)
#ifdef __powerpc64__
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE3__ 1
#define NO_WARN_X86_INTRINSICS 1
#endif
#include <tmmintrin.h>
#elif defined(__SSE2__)
#include <emmintrin.h>
#elif defined(__ARM_NEON)
#include <arm_neon.h>
#include "include_/sse_neon.h"
#endif
#pragma warning( disable : 4005)
#include "include_/bitutil_.h"
#ifdef __ARM_NEON
#define PREFETCH(_ip_,_rw_)
#else
#define PREFETCH(_ip_,_rw_) __builtin_prefetch(_ip_,_rw_)
#endif
#define powof2(n) !((n)&((n)-1))
//-- 24 bits / 3 bytes (scalar only) ----------------------
#define ESIZE 3
#define STRIDE ESIZE
#define TPENC tpenc
#define TPDEC tpdec
#include "transpose.c"
#define TPENC tpzenc
#define TPDEC tpzdec
#include "transpose.c"
#define TPENC tpxenc
#define TPDEC tpxdec
#include "transpose.c"
//-- 128 bits / 16 bytes (scalar only) --------------------
#define ESIZE 16
#define STRIDE ESIZE
#define TPENC tpenc
#define TPDEC tpdec
#include "transpose.c"
#define TPENC tpzenc
#define TPDEC tpzdec
#include "transpose.c"
#define TPENC tpxenc
#define TPDEC tpxdec
#include "transpose.c"
//-----------------------------------------------------------
#define LD128(_ip_) _mm_loadu_si128( (__m128i *)(_ip_))
#define ST128(_op_,_v_) _mm_storeu_si128((__m128i *)(_op_),_v_)
#define LD256(ip) _mm256_loadu_si256(ip)
#define ST256(op,v) _mm256_storeu_si256(op,v)
//---------------------------------------------- 16 bits -------------------------------------------------
#define ESIZE 2
#define USIZE 16
//-- byte ------------------------
#define STRIDE ESIZE
#define TPENC tpenc
#define TPDEC tpdec
#define TPENC128V tpenc128v
#define TPDEC128V tpdec128v
#define TPENC256V tpenc256v
#define TPDEC256V tpdec256v
#define VEINI128
#define VEINI256
#define VE128(_v_,_sv_)
#define VE256(_v_,_sv_)
#define VDINI128
#define VDINI256
#define VD128(_ov_,_sv_)
#define VD256(_ov_,_sv_)
#include "transpose.c"
#undef TPENC256V
#undef TPDEC256V
//--------------
#define ISDELTA
//-- zigzag
#define TPENC tpzenc
#define TPDEC tpzdec
#define TPENC128V tpzenc128v
#define TPDEC128V tpzdec128v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_delta_epi16(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm_zzage_epi16(_tv); }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _v_ = mm_zzagd_epi16( _v_); _sv_ = mm_scan_epi16(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//-- xor
#define TPENC tpxenc
#define TPDEC tpxdec
#define TPENC128V tpxenc128v
#define TPDEC128V tpxdec128v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_xore_epi16(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _sv_ = _v_ = mm_xord_epi16(_v_,_sv_)
#include "transpose.c"
//--
#undef ISDELTA
//----------- nibble ---------
#define STRIDE 4
#define TPENC tp4enc
#define TPDEC tp4dec
#define TPENC128V tp4enc128v
#define TPDEC128V tp4dec128v
#define TPENC256V tp4enc256v
#define TPDEC256V tp4dec256v
#define VEINI128
#define VEINI256
#define VE128(_v_,_sv_)
#define VE256(_v_,_sv_)
#define VDINI128
#define VDINI256
#define VD128(_ov_,_sv_)
#define VD256(_ov_,_sv_)
#include "transpose.c"
#undef TPENC256V
#undef TPDEC256V
#define ISDELTA
//-- zigzag
#define TPENC tp4zenc
#define TPDEC tp4zdec
#define TPENC128V tp4zenc128v
#define TPDEC128V tp4zdec128v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_delta_epi16(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm_zzage_epi16(_tv); }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _v_ = mm_zzagd_epi16( _v_); _sv_ = mm_scan_epi16(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//-- xor
#define TPENC tp4xenc
#define TPDEC tp4xdec
#define TPENC128V tp4xenc128v
#define TPDEC128V tp4xdec128v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_xore_epi16(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _sv_ = _v_ = mm_xord_epi16(_v_,_sv_)
#include "transpose.c"
//--
#undef ISDELTA
//------------------------------------------------ 32 bits ----------------------------------------------------------
#define ESIZE 4
#define USIZE 32
#define STRIDE ESIZE
//----------------- byte ------------
#define TPENC tpenc
#define TPDEC tpdec
#define TPENC128V tpenc128v
#define TPDEC128V tpdec128v
#define TPENC256V tpenc256v
#define TPDEC256V tpdec256v
#define VEINI128
#define VEINI256
#define VE128(_v_,_sv_)
#define VE256(_v_,_sv_)
#define VDINI128
#define VDINI256
#define VD128(_ov_,_sv_)
#define VD256(_ov_,_sv_)
#include "transpose.c"
//---------------------------------
#define ISDELTA
//-- zigzag
#define TPENC tpzenc
#define TPDEC tpzdec
#define TPENC128V tpzenc128v
#define TPDEC128V tpzdec128v
#define TPENC256V tpzenc256v
#define TPDEC256V tpzdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_delta_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm_zzage_epi32(_tv); }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_delta_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm256_zzage_epi32(_tv); }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _v_ = mm_zzagd_epi32( _v_); _sv_ = mm_scan_epi32(_v_,_sv_); _v_ = _sv_
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _v_ = mm256_zzagd_epi32(_v_); _sv_ = mm256_scan_epi32(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//-- xor
#define TPENC tpxenc
#define TPDEC tpxdec
#define TPENC128V tpxenc128v
#define TPDEC128V tpxdec128v
#define TPENC256V tpxenc256v
#define TPDEC256V tpxdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_xore_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_xore_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _sv_ = _v_ = mm_xord_epi32(_v_,_sv_)
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _sv_ = _v_ = mm256_xord_epi32(_v_,_sv_)
#include "transpose.c"
//---------
#undef ISDELTA
//----------------- nibble ----------
#define STRIDE 8
#define TPENC tp4enc
#define TPDEC tp4dec
#define TPENC128V tp4enc128v
#define TPDEC128V tp4dec128v
#define TPENC256V tp4enc256v
#define TPDEC256V tp4dec256v
#define VEINI128
#define VEINI256
#define VE128(_v_,_sv_)
#define VE256(_v_,_sv_)
#define VDINI128
#define VDINI256
#define VD128(_ov_,_sv_)
#define VD256(_ov_,_sv_)
#include "transpose.c"
//-------------
#define ISDELTA
//-- zigzag
#define TPENC tp4zenc
#define TPDEC tp4zdec
#define TPENC128V tp4zenc128v
#define TPDEC128V tp4zdec128v
#define TPENC256V tp4zenc256v
#define TPDEC256V tp4zdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_delta_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm_zzage_epi32(_tv); }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_delta_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm256_zzage_epi32(_tv); }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _v_ = mm_zzagd_epi32( _v_); _sv_ = mm_scan_epi32(_v_,_sv_); _v_ = _sv_
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _v_ = mm256_zzagd_epi32(_v_); _sv_ = mm256_scan_epi32(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//-- xor
#define TPENC tp4xenc
#define TPDEC tp4xdec
#define TPENC128V tp4xenc128v
#define TPDEC128V tp4xdec128v
#define TPENC256V tp4xenc256v
#define TPDEC256V tp4xdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_xore_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_xore_epi32(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _sv_ = mm_xord_epi32(_v_,_sv_); _v_ = _sv_
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _sv_ = mm256_xord_epi32(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//--------------
#undef ISDELTA
//------------------------------- 64 bits ---------------------------------------------------------------
#define ESIZE 8
#define USIZE 64
#define STRIDE ESIZE
//--- byte 64 bits ------------
#define TPENC tpenc
#define TPDEC tpdec
#define TPENC128V tpenc128v
#define TPDEC128V tpdec128v
#define TPENC256V tpenc256v
#define TPDEC256V tpdec256v
#define VEINI128
#define VEINI256
#define VE128(_v_,_sv_)
#define VE256(_v_,_sv_)
#define VDINI128
#define VDINI256
#define VD128(_ov_,_sv_)
#define VD256(_ov_,_sv_)
#include "transpose.c"
//---------------
#define ISDELTA
//--- zigzag
#define TPENC tpzenc
#define TPDEC tpzdec
#define TPENC128V tpzenc128v
#define TPDEC128V tpzdec128v
#define TPENC256V tpzenc256v
#define TPDEC256V tpzdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_delta_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm_zzage_epi64(_tv); }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_delta_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm256_zzage_epi64(_tv); }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _v_ = mm_zzagd_epi64(_v_); _sv_ = mm_scan_epi64(_v_,_sv_); _v_ = _sv_
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _v_ = mm256_zzagd_epi64(_v_); _sv_ = mm256_scan_epi64(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//--- xor
#define TPENC tpxenc
#define TPDEC tpxdec
#define TPENC128V tpxenc128v
#define TPDEC128V tpxdec128v
#define TPENC256V tpxenc256v
#define TPDEC256V tpxdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_xore_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_xore_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _sv_ = _v_ = mm_xord_epi64(_v_,_sv_)
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _sv_ = _v_ = mm256_xord_epi64(_v_,_sv_)
#include "transpose.c"
//-----------
#undef ISDELTA
//---------- nibble ----------
#define STRIDE 16
#define TPENC tp4enc
#define TPDEC tp4dec
#define TPENC128V tp4enc128v
#define TPDEC128V tp4dec128v
#define TPENC256V tp4enc256v
#define TPDEC256V tp4dec256v
#define VEINI128
#define VEINI256
#define VE128(_v_,_sv_)
#define VE256(_v_,_sv_)
#define VDINI128
#define VDINI256
#define VD128(_ov_,_sv_)
#define VD256(_ov_,_sv_)
#include "transpose.c"
//-------------------------
#define ISDELTA
//-- zigzag
#define TPENC tp4zenc
#define TPDEC tp4zdec
#define TPENC128V tp4zenc128v
#define TPDEC128V tp4zdec128v
#define TPENC256V tp4zenc256v
#define TPDEC256V tp4zdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_delta_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm_zzage_epi64(_tv); }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_delta_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = mm256_zzage_epi64(_tv); }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _v_ = mm_zzagd_epi64(_v_); _sv_ = mm_scan_epi64(_v_,_sv_); _v_ = _sv_
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _v_ = mm256_zzagd_epi64(_v_); _sv_ = mm256_scan_epi64(_v_,_sv_); _v_ = _sv_
#include "transpose.c"
//-- xor
#define TPENC tp4xenc
#define TPDEC tp4xdec
#define TPENC128V tp4xenc128v
#define TPDEC128V tp4xdec128v
#define TPENC256V tp4xenc256v
#define TPDEC256V tp4xdec256v
#define VEINI128 __m128i sv = _mm_setzero_si128()
#define VE128(_iv_,_sv_) { __m128i _tv = mm_xore_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VEINI256 __m256i sv = _mm256_setzero_si256()
#define VE256(_iv_,_sv_) { __m256i _tv = mm256_xore_epi64(_iv_,_sv_); _sv_ = _iv_; _iv_ = _tv; }
#define VDINI128 __m128i sv = _mm_setzero_si128()
#define VD128(_v_,_sv_) _sv_ = _v_ = mm_xord_epi64(_v_,_sv_)
#define VDINI256 __m256i sv = _mm256_setzero_si256()
#define VD256(_v_,_sv_) _sv_ = _v_ = mm256_xord_epi64(_v_,_sv_)
#include "transpose.c"
#else //************************************************************* Templates ********************************************************************************************************
#define SIE(p,i) (p+=stride) //faster on ARM //#define SIE(_p_,_i_) (_p_+ _i_*stride)
#define SID(p,i) (p+=stride) //#define SID(_p_,_i_) (_p_+ _i_*stride)
#if defined(__AVX2__)
#ifdef TPENC256V
void T2(TPENC256V, ESIZE)(unsigned char *in, unsigned n, unsigned char *out) {
unsigned v = n&~(ESIZE*32-1);
unsigned stride = v/STRIDE;
unsigned char *op,*ip;
VEINI256;
#if ESIZE == 2
__m256i sf = _mm256_set_epi8( 15, 13, 11, 9, 7, 5, 3, 1,
14, 12, 10, 8, 6, 4, 2, 0,
15, 13, 11, 9, 7, 5, 3, 1,
14, 12, 10, 8, 6, 4, 2, 0);
__m256i sv0 = _mm256_set_epi8(15, 13, 11, 9,
7, 5, 3, 1,
14, 12, 10, 8,
6, 4, 2, 0,
15, 13, 11, 9,
7, 5, 3, 1,
14, 12, 10, 8,
6, 4, 2, 0);
__m256i sv1 = _mm256_set_epi8(14, 12, 10, 8,
6, 4, 2, 0,
15, 13, 11, 9,
7, 5, 3, 1,
14, 12, 10, 8,
6, 4, 2, 0,
15, 13, 11, 9,
7, 5, 3, 1);
#else
__m256i pv = _mm256_set_epi32( 7, 3, 6, 2, 5, 1, 4, 0),
#if ESIZE == 4
sv0 = _mm256_set_epi8(15, 11, 7, 3,
13, 9, 5, 1,
14, 10, 6, 2,
12, 8, 4, 0,
15, 11, 7, 3,
13, 9, 5, 1,
14, 10, 6, 2,
12, 8, 4, 0),
sv1= _mm256_set_epi8(13, 9, 5, 1,
15, 11, 7, 3,
12, 8, 4, 0,
14, 10, 6, 2,
13, 9, 5, 1,
15, 11, 7, 3,
12, 8, 4, 0,
14, 10, 6, 2);
#else
sf = _mm256_set_epi8(15, 7,
14, 6,
13, 5,
12, 4,
11, 3,
10, 2,
9, 1,
8, 0,
15, 7,
14, 6,
13, 5,
12, 4,
11, 3,
10, 2,
9, 1,
8, 0 ),
tv = _mm256_set_epi8(15, 14, 11, 10, 13, 12, 9, 8,
7, 6, 3, 2, 5, 4, 1, 0,
15, 14, 11, 10, 13, 12, 9, 8,
7, 6, 3, 2, 5, 4, 1, 0);
#endif
#endif
#if STRIDE > ESIZE // ------------------ byte transpose ----------------------------------
__m256i cl = _mm256_set1_epi8( 0x0f),
ch = _mm256_set1_epi8( 0xf0),
cb = _mm256_set1_epi16(0xff);
#endif
for(ip = in,op = out; ip != in+v; ip += ESIZE*32, op += ESIZE*32/STRIDE) {
unsigned char *p = op; PREFETCH(ip+ESIZE*192,0);
__m256i iv[ESIZE], ov[ESIZE == 2 ? ESIZE + 2 : ESIZE];
#if ESIZE == 2
ov[0] = LD256((__m256i *) ip ); VE256(ov[0],sv); ov[0] = _mm256_shuffle_epi8(ov[0], sv0);
ov[1] = LD256((__m256i *)(ip+32)); VE256(ov[1],sv); ov[1] = _mm256_shuffle_epi8(ov[1], sv1);
iv[0] = _mm256_permute4x64_epi64(_mm256_blend_epi32(ov[0], ov[1],0b11001100),_MM_SHUFFLE(3, 1, 2, 0));
iv[1] = _mm256_blend_epi32(ov[0], ov[1],0b00110011);
iv[1] = _mm256_permute4x64_epi64(_mm256_shuffle_epi32(iv[1],_MM_SHUFFLE(1, 0, 3, 2)),_MM_SHUFFLE(3, 1, 2, 0));
#elif ESIZE == 4
iv[0] = LD256((__m256i *) ip ); VE256(iv[0],sv); iv[0] = _mm256_shuffle_epi8(iv[0], sv0);
iv[1] = LD256((__m256i *)(ip+32)); VE256(iv[1],sv); iv[1] = _mm256_shuffle_epi8(iv[1], sv1);
iv[2] = LD256((__m256i *)(ip+64)); VE256(iv[2],sv); iv[2] = _mm256_shuffle_epi8(iv[2], sv0);
iv[3] = LD256((__m256i *)(ip+96)); VE256(iv[3],sv); iv[3] = _mm256_shuffle_epi8(iv[3], sv1);
ov[0] = _mm256_blend_epi32(iv[0], iv[1],0b10101010);
ov[1] = _mm256_shuffle_epi32(_mm256_blend_epi32(iv[0], iv[1],0b01010101),_MM_SHUFFLE(2, 3, 0, 1));
ov[2] = _mm256_blend_epi32(iv[2], iv[3],0b10101010);
ov[3] = _mm256_shuffle_epi32(_mm256_blend_epi32(iv[2], iv[3],0b01010101),_MM_SHUFFLE(2, 3, 0, 1));
iv[0] = _mm256_permutevar8x32_epi32(_mm256_unpacklo_epi64(ov[0], ov[2]), pv);
iv[1] = _mm256_permutevar8x32_epi32(_mm256_unpackhi_epi64(ov[0], ov[2]), pv);
iv[2] = _mm256_permutevar8x32_epi32(_mm256_unpacklo_epi64(ov[1], ov[3]), pv);
iv[3] = _mm256_permutevar8x32_epi32(_mm256_unpackhi_epi64(ov[1], ov[3]), pv);
#else
ov[0] = LD256((__m256i *) ip ); VE256(ov[0],sv); ov[0] = _mm256_shuffle_epi8(ov[0], sf);
ov[1] = LD256((__m256i *)(ip+32)); VE256(ov[1],sv); ov[1] = _mm256_shuffle_epi8(ov[1], sf);
ov[2] = LD256((__m256i *)(ip+64)); VE256(ov[2],sv); ov[2] = _mm256_shuffle_epi8(ov[2], sf);
ov[3] = LD256((__m256i *)(ip+96)); VE256(ov[3],sv); ov[3] = _mm256_shuffle_epi8(ov[3], sf);
iv[0] = _mm256_unpacklo_epi16(ov[0], ov[1]); iv[1] = _mm256_unpackhi_epi16(ov[0], ov[1]);
iv[2] = _mm256_unpacklo_epi16(ov[2], ov[3]); iv[3] = _mm256_unpackhi_epi16(ov[2], ov[3]);
ov[0] = _mm256_unpacklo_epi32(iv[0], iv[2]); ov[1] = _mm256_unpackhi_epi32(iv[0], iv[2]);
ov[2] = _mm256_unpacklo_epi32(iv[1], iv[3]); ov[3] = _mm256_unpackhi_epi32(iv[1], iv[3]);
ov[4] = LD256((__m256i *)(ip+128)); VE256(ov[4],sv); ov[4] = _mm256_shuffle_epi8(ov[4], sf);
ov[5] = LD256((__m256i *)(ip+160)); VE256(ov[5],sv); ov[5] = _mm256_shuffle_epi8(ov[5], sf);
ov[6] = LD256((__m256i *)(ip+192)); VE256(ov[6],sv); ov[6] = _mm256_shuffle_epi8(ov[6], sf);
ov[7] = LD256((__m256i *)(ip+224)); VE256(ov[7],sv); ov[7] = _mm256_shuffle_epi8(ov[7], sf);
iv[4] = _mm256_unpacklo_epi16(ov[4], ov[5]); iv[5] = _mm256_unpackhi_epi16(ov[4], ov[5]);
iv[6] = _mm256_unpacklo_epi16(ov[6], ov[7]); iv[7] = _mm256_unpackhi_epi16(ov[6], ov[7]);
ov[4] = _mm256_unpacklo_epi32(iv[4], iv[6]); ov[5] = _mm256_unpackhi_epi32(iv[4], iv[6]);
ov[6] = _mm256_unpacklo_epi32(iv[5], iv[7]); ov[7] = _mm256_unpackhi_epi32(iv[5], iv[7]);
iv[0] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpacklo_epi64(ov[0], ov[4]), pv), tv);
iv[1] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpackhi_epi64(ov[0], ov[4]), pv), tv);
iv[2] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpacklo_epi64(ov[1], ov[5]), pv), tv);
iv[3] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpackhi_epi64(ov[1], ov[5]), pv), tv);
iv[4] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpacklo_epi64(ov[2], ov[6]), pv), tv);
iv[5] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpackhi_epi64(ov[2], ov[6]), pv), tv);
iv[6] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpacklo_epi64(ov[3], ov[7]), pv), tv);
iv[7] = _mm256_shuffle_epi8(_mm256_permutevar8x32_epi32(_mm256_unpackhi_epi64(ov[3], ov[7]), pv), tv);
#endif
#if STRIDE <= ESIZE
_mm256_storeu_si256((__m256i *) p, iv[0]);
_mm256_storeu_si256((__m256i *)(p+=stride), iv[1]);
#if ESIZE > 2
_mm256_storeu_si256((__m256i *)(p+=stride), iv[2]);
_mm256_storeu_si256((__m256i *)(p+=stride), iv[3]);
#if ESIZE > 4
_mm256_storeu_si256((__m256i *)(p+=stride), iv[4]);
_mm256_storeu_si256((__m256i *)(p+=stride), iv[5]);
_mm256_storeu_si256((__m256i *)(p+=stride), iv[6]);
_mm256_storeu_si256((__m256i *)(p+=stride), iv[7]);
#endif
#endif
#else //---------------------- Nibble Transpose ------------------------
#define mm256_packus_epi16(a, b) _mm256_permute4x64_epi64(_mm256_packus_epi16(a, b), _MM_SHUFFLE(3, 1, 2, 0))
#define ST128(_p_,_v_,_i_) _mm_storeu_si128((__m256i *)SIE(_p_,_i_), _mm256_castsi256_si128(_v_))
#define ST1280(_p_,_v_) _mm_storeu_si128((__m256i *)(_p_), _mm256_castsi256_si128(_v_))
ov[0] = _mm256_and_si256(iv[0], cl); ov[0] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[0],4), ov[0]),cb); ov[0] = mm256_packus_epi16(ov[0], _mm256_srli_si256( ov[0],2));
ov[1] = _mm256_srli_epi16(_mm256_and_si256(iv[0], ch),4); ov[1] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[1],4), ov[1]),cb); ov[1] = mm256_packus_epi16(ov[1], _mm256_srli_si256( ov[1],2));
ov[2] = _mm256_and_si256(iv[1], cl); ov[2] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[2],4), ov[2]),cb); ov[2] = mm256_packus_epi16(ov[2], _mm256_srli_si256( ov[2],2));
ov[3] = _mm256_srli_epi16(_mm256_and_si256(iv[1], ch),4); ov[3] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[3],4), ov[3]),cb); ov[3] = mm256_packus_epi16(ov[3], _mm256_srli_si256( ov[3],2));
ST1280(p,ov[0]); ST128(p,ov[1],1); ST128(p,ov[2],2); ST128(p,ov[3],3);
#if ESIZE > 2
ov[0] = _mm256_and_si256(iv[2], cl); ov[0] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[0],4), ov[0]),cb); ov[0] = mm256_packus_epi16(ov[0], _mm256_srli_si256( ov[0],2));
ov[1] = _mm256_srli_epi16(_mm256_and_si256(iv[2], ch),4); ov[1] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[1],4), ov[1]),cb); ov[1] = mm256_packus_epi16(ov[1], _mm256_srli_si256( ov[1],2));
ov[2] = _mm256_and_si256(iv[3], cl); ov[2] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[2],4), ov[2]),cb); ov[2] = mm256_packus_epi16(ov[2], _mm256_srli_si256( ov[2],2));
ov[3] = _mm256_srli_epi16(_mm256_and_si256(iv[3], ch),4); ov[3] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[3],4), ov[3]),cb); ov[3] = mm256_packus_epi16(ov[3], _mm256_srli_si256( ov[3],2));
ST128(p,ov[0],4); ST128(p,ov[1],5); ST128(p,ov[2],6); ST128(p,ov[3],7);
#if ESIZE > 4
ov[0] = _mm256_and_si256(iv[4], cl); ov[0] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[0],4), ov[0]),cb); ov[0] = mm256_packus_epi16(ov[0], _mm256_srli_si256( ov[0],2));
ov[1] = _mm256_srli_epi16(_mm256_and_si256(iv[4], ch),4); ov[1] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[1],4), ov[1]),cb); ov[1] = mm256_packus_epi16(ov[1], _mm256_srli_si256( ov[1],2));
ov[2] = _mm256_and_si256(iv[5], cl); ov[2] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[2],4), ov[2]),cb); ov[2] = mm256_packus_epi16(ov[2], _mm256_srli_si256( ov[2],2));
ov[3] = _mm256_srli_epi16(_mm256_and_si256(iv[5], ch),4); ov[3] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[3],4), ov[3]),cb); ov[3] = mm256_packus_epi16(ov[3], _mm256_srli_si256( ov[3],2));
ST128(p,ov[0],8); ST128(p,ov[1],9); ST128(p,ov[2],10); ST128(p,ov[3],11);
ov[0] = _mm256_and_si256(iv[6], cl); ov[0] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[0],4), ov[0]),cb); ov[0] = mm256_packus_epi16(ov[0], _mm256_srli_si256( ov[0],2));
ov[1] = _mm256_srli_epi16(_mm256_and_si256(iv[6], ch),4); ov[1] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[1],4), ov[1]),cb); ov[1] = mm256_packus_epi16(ov[1], _mm256_srli_si256( ov[1],2));
ov[2] = _mm256_and_si256(iv[7], cl); ov[2] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[2],4), ov[2]),cb); ov[2] = mm256_packus_epi16(ov[2], _mm256_srli_si256( ov[2],2));
ov[3] = _mm256_srli_epi16(_mm256_and_si256(iv[7], ch),4); ov[3] = _mm256_and_si256(_mm256_or_si256(_mm256_srli_epi16(ov[3],4), ov[3]),cb); ov[3] = mm256_packus_epi16(ov[3], _mm256_srli_si256( ov[3],2));
ST128(p,ov[0],12); ST128(p,ov[1],13); ST128(p,ov[2],14); ST128(p,ov[3],15);
#endif
#endif
#endif
}
T2(tpenc,ESIZE)(in+v, n-v, out+v);
}
#endif // TPENC256V
#ifdef TPDEC256V
#define NBL0(x,y) ov[x] = _mm256_permute4x64_epi64(_mm256_castsi128_si256(_mm_loadu_si128((__m128i *)(p ))),_MM_SHUFFLE(3, 1, 2, 0));\
ov[y] = _mm256_permute4x64_epi64(_mm256_castsi128_si256(_mm_loadu_si128((__m128i *)(p+=stride))),_MM_SHUFFLE(3, 1, 2, 0));
#define NBL(x,y) ov[x] = _mm256_permute4x64_epi64(_mm256_castsi128_si256(_mm_loadu_si128((__m128i *)(p+=stride))),_MM_SHUFFLE(3, 1, 2, 0));\
ov[y] = _mm256_permute4x64_epi64(_mm256_castsi128_si256(_mm_loadu_si128((__m128i *)(p+=stride))),_MM_SHUFFLE(3, 1, 2, 0));
#define NB(x,y,_iv_) {\
ov[x] = _mm256_and_si256(_mm256_unpacklo_epi8(ov[x], _mm256_srli_epi16(ov[x],4)), cl);\
ov[y] = _mm256_and_si256(_mm256_unpacklo_epi8(ov[y], _mm256_srli_epi16(ov[y],4)), cl);\
_iv_ = _mm256_or_si256(_mm256_slli_epi16(ov[y],4), ov[x]); \
}
void T2(TPDEC256V, ESIZE)(unsigned char *in, unsigned n, unsigned char *out) {
unsigned v = n&~(ESIZE*32-1);
unsigned stride = v/STRIDE;
unsigned char *op,*ip;
VDINI256;
#if STRIDE > ESIZE
__m256i cl = _mm256_set1_epi8(0x0f), ch=_mm256_set1_epi8(0xf0), cb = _mm256_set1_epi16(0xff);
#endif
for(op = out,ip = in; op != out+v; ip += ESIZE*32/STRIDE, op += ESIZE*32) { unsigned char *p = ip; PREFETCH(ip+ESIZE*192,0);
__m256i iv[ESIZE], ov[ESIZE == 2 ? ESIZE + 2 : ESIZE];
#if STRIDE > ESIZE
NBL0(0,1); NBL( 2,3); NB(0,1,iv[0]); NB(2,3,iv[1]);
#if ESIZE > 2
NBL( 0,1); NBL( 2,3); NB(0,1,iv[2]); NB(2,3,iv[3]);
#if ESIZE > 4
NBL(4,5); NBL( 6,7); NB(4,5,iv[4]); NB(6,7,iv[5]);
NBL(4,5); NBL( 6,7); NB(4,5,iv[6]); NB(6,7,iv[7]);
#endif
#endif
#else
iv[0] = _mm256_loadu_si256((__m256i *) p );
iv[1] = _mm256_loadu_si256((__m256i *)(p+=stride));
#if ESIZE > 2
iv[2] = _mm256_loadu_si256((__m256i *)(p+=stride));
iv[3] = _mm256_loadu_si256((__m256i *)(p+=stride));
#if ESIZE > 4
iv[4] = _mm256_loadu_si256((__m256i *)(p+=stride));
iv[5] = _mm256_loadu_si256((__m256i *)(p+=stride));
iv[6] = _mm256_loadu_si256((__m256i *)(p+=stride));
iv[7] = _mm256_loadu_si256((__m256i *)(p+=stride));
#endif
#endif
#endif
#if ESIZE == 2
ov[0] = _mm256_permute4x64_epi64(iv[0], _MM_SHUFFLE(3, 1, 2, 0));
ov[1] = _mm256_permute4x64_epi64(iv[1], _MM_SHUFFLE(3, 1, 2, 0));
iv[0] = _mm256_unpacklo_epi8(ov[0], ov[1]); VD256(iv[0],sv); _mm256_storeu_si256((__m256i *)op, iv[0]);
iv[1] = _mm256_unpackhi_epi8(ov[0], ov[1]); VD256(iv[1],sv); _mm256_storeu_si256((__m256i *)(op+32), iv[1]);
#elif ESIZE == 4
ov[0] = _mm256_unpacklo_epi8( iv[0], iv[1]); ov[1] = _mm256_unpackhi_epi8( iv[0], iv[1]);
ov[2] = _mm256_unpacklo_epi8( iv[2], iv[3]); ov[3] = _mm256_unpackhi_epi8( iv[2], iv[3]);
iv[0] = _mm256_unpacklo_epi16(ov[0], ov[2]); iv[1] = _mm256_unpackhi_epi16(ov[0], ov[2]);
iv[2] = _mm256_unpacklo_epi16(ov[1], ov[3]); iv[3] = _mm256_unpackhi_epi16(ov[1], ov[3]);
ov[0] = _mm256_permute2x128_si256(iv[0], iv[1], (2 << 4) | 0);
ov[1] = _mm256_permute2x128_si256(iv[2], iv[3], (2 << 4) | 0);
ov[2] = _mm256_permute2x128_si256(iv[0], iv[1], (3 << 4) | 1);
ov[3] = _mm256_permute2x128_si256(iv[2], iv[3], (3 << 4) | 1);
VD256(ov[0],sv); _mm256_storeu_si256((__m256i *) op, ov[0]);
VD256(ov[1],sv); _mm256_storeu_si256((__m256i *)(op+32), ov[1]);
VD256(ov[2],sv); _mm256_storeu_si256((__m256i *)(op+64), ov[2]);
VD256(ov[3],sv); _mm256_storeu_si256((__m256i *)(op+96), ov[3]);
#else
ov[0] = _mm256_unpacklo_epi8(iv[0], iv[1]); ov[1] = _mm256_unpackhi_epi8(iv[0], iv[1]);
ov[2] = _mm256_unpacklo_epi8(iv[2], iv[3]); ov[3] = _mm256_unpackhi_epi8(iv[2], iv[3]);
iv[0] = _mm256_permute4x64_epi64(_mm256_unpacklo_epi16(ov[0], ov[2]), _MM_SHUFFLE(3, 1, 2, 0));
iv[1] = _mm256_permute4x64_epi64(_mm256_unpackhi_epi16(ov[0], ov[2]), _MM_SHUFFLE(3, 1, 2, 0));
iv[2] = _mm256_permute4x64_epi64(_mm256_unpacklo_epi16(ov[1], ov[3]), _MM_SHUFFLE(3, 1, 2, 0));
iv[3] = _mm256_permute4x64_epi64(_mm256_unpackhi_epi16(ov[1], ov[3]), _MM_SHUFFLE(3, 1, 2, 0));
ov[4] = _mm256_unpacklo_epi8(iv[4], iv[5]); ov[5] = _mm256_unpackhi_epi8(iv[4], iv[5]);
ov[6] = _mm256_unpacklo_epi8(iv[6], iv[7]); ov[7] = _mm256_unpackhi_epi8(iv[6], iv[7]);
iv[4] = _mm256_permute4x64_epi64(_mm256_unpacklo_epi16(ov[4], ov[6]), _MM_SHUFFLE(3, 1, 2, 0));
iv[5] = _mm256_permute4x64_epi64(_mm256_unpackhi_epi16(ov[4], ov[6]), _MM_SHUFFLE(3, 1, 2, 0));
iv[6] = _mm256_permute4x64_epi64(_mm256_unpacklo_epi16(ov[5], ov[7]), _MM_SHUFFLE(3, 1, 2, 0));
iv[7] = _mm256_permute4x64_epi64(_mm256_unpackhi_epi16(ov[5], ov[7]), _MM_SHUFFLE(3, 1, 2, 0));
ov[0] = _mm256_unpacklo_epi32(iv[0], iv[4]);
ov[1] = _mm256_unpacklo_epi32(iv[1], iv[5]);
ov[2] = _mm256_unpacklo_epi32(iv[2], iv[6]);
ov[3] = _mm256_unpacklo_epi32(iv[3], iv[7]);
ov[4] = _mm256_unpackhi_epi32(iv[0], iv[4]);
ov[5] = _mm256_unpackhi_epi32(iv[1], iv[5]);
ov[6] = _mm256_unpackhi_epi32(iv[2], iv[6]);
ov[7] = _mm256_unpackhi_epi32(iv[3], iv[7]);
VD256(ov[0],sv); ST256((__m256i *) op, ov[0] );
VD256(ov[1],sv); ST256((__m256i *)(op+ 32), ov[1] );
VD256(ov[2],sv); ST256((__m256i *)(op+ 64), ov[2] );
VD256(ov[3],sv); ST256((__m256i *)(op+ 96), ov[3] );
VD256(ov[4],sv); ST256((__m256i *)(op+128), ov[4] );
VD256(ov[5],sv); ST256((__m256i *)(op+160), ov[5] );
VD256(ov[6],sv); ST256((__m256i *)(op+192), ov[6] );
VD256(ov[7],sv); ST256((__m256i *)(op+224), ov[7] );
#endif
}
if(n-v) T2(tpdec,ESIZE)(in+v, n-v, out+v);
}
#endif //TPDEC256V
#else //__AVX2__
#if (defined(__SSE3__) || defined(__ARM_NEON)) && (ESIZE == 2 || ESIZE == 4 || ESIZE == 8)
#define ST(_p_,_v_,_i_) _mm_storeu_si128((__m128i *)SIE(_p_,_i_), _v_)
#define ST0(_p_,_v_) _mm_storeu_si128((__m128i *)(_p_), _v_)
void T2(TPENC128V, ESIZE)(unsigned char *in, unsigned n, unsigned char *out) {
unsigned v = n&~(ESIZE*32-1);
unsigned stride = v/STRIDE;
unsigned char *op,*ip;
#if defined(__SSE3__) || defined(__ARM_NEON)
#if ESIZE == 2
__m128i sf = _mm_set_epi8(15, 13, 11, 9, 7, 5, 3, 1,
14, 12, 10, 8, 6, 4, 2, 0);
#elif ESIZE == 4
__m128i sf = _mm_set_epi8(15, 11, 7,3,
14, 10, 6,2,
13, 9, 5,1,
12, 8, 4,0);
#else
__m128i sf = _mm_set_epi8(15, 7,
14, 6,
13, 5,
12, 4,
11, 3,
10, 2,
9, 1,
8, 0 );
#endif
#endif
VEINI128;
#if STRIDE > ESIZE
__m128i cl = _mm_set1_epi8(0x0f), ch=_mm_set1_epi8(0xf0), cb = _mm_set1_epi16(0xff);
#endif
for(ip = in, op = out; ip != in+v; ip+=ESIZE*16,op += ESIZE*16/STRIDE) { unsigned char *p = op; PREFETCH(ip+(ESIZE*16)*ESIZE,0);
__m128i iv[ESIZE], ov[ESIZE == 2 ? ESIZE + 2 : ESIZE];
#if defined(__SSSE3__) || defined(__ARM_NEON)
#if ESIZE == 2
#ifdef __ARM_NEON
uint8x16x2_t w = vld2q_u8(ip);
#if STRIDE <= ESIZE
ST0(p,(__m128i)w.val[0]); ST(p,(__m128i)w.val[1],1);
#else
iv[0] = (__m128i)w.val[0]; iv[1] = (__m128i)w.val[1];
#endif
#else
ov[0] = LD128(ip); VE128(ov[0],sv); ov[0] = _mm_shuffle_epi8(ov[0], sf);
ov[1] = LD128(ip+16); VE128(ov[1],sv); ov[1] = _mm_shuffle_epi8(ov[1], sf);
iv[0] = _mm_unpacklo_epi64(ov[0], ov[1]); iv[1] = _mm_unpackhi_epi64(ov[0], ov[1]);
#if STRIDE <= ESIZE
ST0(p,iv[0]); ST(p,iv[1],1);
#endif
#endif
#elif ESIZE == 4
#ifdef __ARM_NEON
uint8x16x4_t w = vld4q_u8(ip);
#if STRIDE <= ESIZE
ST0(p,(__m128i)w.val[0]); ST(p,(__m128i)w.val[1],1); ST(p,(__m128i)w.val[2],2); ST(p,(__m128i)w.val[3],3);
#else
iv[0] = (__m128i)w.val[0]; iv[1] = (__m128i)w.val[1]; iv[2] = (__m128i)w.val[2]; iv[3] = (__m128i)w.val[3];
#endif
#else
iv[0] = LD128(ip ); VE128(iv[0],sv); iv[0] = _mm_shuffle_epi8(iv[0], sf);
iv[1] = LD128(ip+16); VE128(iv[1],sv); iv[1] = _mm_shuffle_epi8(iv[1], sf);
iv[2] = LD128(ip+32); VE128(iv[2],sv); iv[2] = _mm_shuffle_epi8(iv[2], sf);
iv[3] = LD128(ip+48); VE128(iv[3],sv); iv[3] = _mm_shuffle_epi8(iv[3], sf);
ov[0] = _mm_unpacklo_epi32(iv[0], iv[1]); ov[1] = _mm_unpackhi_epi32(iv[0], iv[1]);
ov[2] = _mm_unpacklo_epi32(iv[2], iv[3]); ov[3] = _mm_unpackhi_epi32(iv[2], iv[3]);
iv[0] = _mm_unpacklo_epi64(ov[0], ov[2]); iv[1] = _mm_unpackhi_epi64(ov[0], ov[2]);
iv[2] = _mm_unpacklo_epi64(ov[1], ov[3]); iv[3] = _mm_unpackhi_epi64(ov[1], ov[3]);
#if STRIDE <= ESIZE
ST0(p,iv[0]); ST(p,iv[1],1); ST(p,iv[2],2); ST(p,iv[3],3);
#endif
#endif
#elif ESIZE == 8
#ifdef __ARM_NEON
#define vzipl_u16(_a_,_b_) vzip_u16(vget_low_u16((uint16x8_t)(_a_)), vget_low_u16((uint16x8_t)(_b_)))
#define vziph_u16(_a_,_b_) vzip_u16(vget_high_u16((uint16x8_t)(_a_)), vget_high_u16((uint16x8_t)(_b_)))
//#define VQ
#ifndef VQ
uint16x4x2_t v16[8];
uint32x2x2_t v32[8];
#else
uint8x16x2_t v8[4];
uint16x8x2_t v16[4];
uint32x4x2_t v32[4]; //uint64x2x2_t v64[4];
#endif
#ifdef VQ
ov[0] = LD128(ip ); VE128(ov[0],sv); //ov[0] = _mm_shuffle_epi8(ov[0], sv);
ov[1] = LD128(ip+ 16); VE128(ov[1],sv); //ov[1] = _mm_shuffle_epi8(ov[1], sv);
ov[2] = LD128(ip+ 32); VE128(ov[2],sv); //ov[2] = _mm_shuffle_epi8(ov[2], sv);
ov[3] = LD128(ip+ 48); VE128(ov[3],sv); //ov[3] = _mm_shuffle_epi8(ov[3], sv);
ov[4] = LD128(ip+ 64); VE128(ov[4],sv); //ov[4] = _mm_shuffle_epi8(ov[4], sv);
ov[5] = LD128(ip+ 80); VE128(ov[5],sv); //ov[5] = _mm_shuffle_epi8(ov[5], sv);
ov[6] = LD128(ip+ 96); VE128(ov[6],sv); //ov[6] = _mm_shuffle_epi8(ov[6], sv);
ov[7] = LD128(ip+112); VE128(ov[7],sv); //ov[7] = _mm_shuffle_epi8(ov[7], sv);
v8[0] = vzipq_u8((uint8x16_t)ov[0], (uint8x16_t)ov[1]);
v8[1] = vzipq_u8((uint8x16_t)ov[2], (uint8x16_t)ov[3]);
v8[2] = vzipq_u8((uint8x16_t)ov[4], (uint8x16_t)ov[5]);
v8[3] = vzipq_u8((uint8x16_t)ov[6], (uint8x16_t)ov[7]);
/* v16[0] = vzipq_u16((uint16x8_t)ov[0], (uint16x8_t)ov[1]);
v16[1] = vzipq_u16((uint16x8_t)ov[2], (uint16x8_t)ov[3]);
v16[2] = vzipq_u16((uint16x8_t)ov[4], (uint16x8_t)ov[5]);
v16[3] = vzipq_u16((uint16x8_t)ov[6], (uint16x8_t)ov[7]);*/
v16[0] = vzipq_u16(vreinterpretq_u16_u8( v8[0].val[0]), vreinterpretq_u16_u8(v8[1].val[0]));
v16[1] = vzipq_u16(vreinterpretq_u16_u8( v8[0].val[1]), vreinterpretq_u16_u8(v8[1].val[1]));
v16[2] = vzipq_u16(vreinterpretq_u16_u8( v8[2].val[0]), vreinterpretq_u16_u8(v8[3].val[0]));
v16[3] = vzipq_u16(vreinterpretq_u16_u8( v8[2].val[1]), vreinterpretq_u16_u8(v8[3].val[1]));
v32[0] = vzipq_u32(vreinterpretq_u32_u16(v16[0].val[0]), vreinterpretq_u32_u16(v16[2].val[0]));
v32[1] = vzipq_u32(vreinterpretq_u32_u16(v16[0].val[1]), vreinterpretq_u32_u16(v16[2].val[1]));
v32[2] = vzipq_u32(vreinterpretq_u32_u16(v16[1].val[0]), vreinterpretq_u32_u16(v16[3].val[0]));
v32[3] = vzipq_u32(vreinterpretq_u32_u16(v16[1].val[1]), vreinterpretq_u32_u16(v16[3].val[1]));
iv[0] = _mm_unpacklo_epi64(v32[0].val[0], v32[2].val[0]); iv[1] = _mm_unpackhi_epi64(v32[0].val[0], v32[2].val[0]);
iv[2] = _mm_unpacklo_epi64(v32[0].val[1], v32[2].val[1]); iv[3] = _mm_unpackhi_epi64(v32[0].val[1], v32[2].val[1]);
iv[4] = _mm_unpacklo_epi64(v32[1].val[0], v32[3].val[0]); iv[5] = _mm_unpackhi_epi64(v32[1].val[0], v32[3].val[0]);
iv[6] = _mm_unpacklo_epi64(v32[1].val[1], v32[3].val[1]); iv[7] = _mm_unpackhi_epi64(v32[1].val[1], v32[3].val[1]);
#else
ov[0] = LD128(ip ); VE128(ov[0],sv); ov[0] = _mm_shuffle_epi8(ov[0], sf);
ov[1] = LD128(ip+ 16); VE128(ov[1],sv); ov[1] = _mm_shuffle_epi8(ov[1], sf);
ov[2] = LD128(ip+ 32); VE128(ov[2],sv); ov[2] = _mm_shuffle_epi8(ov[2], sf);
ov[3] = LD128(ip+ 48); VE128(ov[3],sv); ov[3] = _mm_shuffle_epi8(ov[3], sf);
ov[4] = LD128(ip+ 64); VE128(ov[4],sv); ov[4] = _mm_shuffle_epi8(ov[4], sf);
ov[5] = LD128(ip+ 80); VE128(ov[5],sv); ov[5] = _mm_shuffle_epi8(ov[5], sf);
ov[6] = LD128(ip+ 96); VE128(ov[6],sv); ov[6] = _mm_shuffle_epi8(ov[6], sf);
ov[7] = LD128(ip+112); VE128(ov[7],sv); ov[7] = _mm_shuffle_epi8(ov[7], sf);
v16[0] = vzipl_u16(ov[0], ov[1]); v16[1] = vziph_u16(ov[0], ov[1]);
v16[2] = vzipl_u16(ov[2], ov[3]); v16[3] = vziph_u16(ov[2], ov[3]);
v16[4] = vzipl_u16(ov[4], ov[5]); v16[5] = vziph_u16(ov[4], ov[5]);
v16[6] = vzipl_u16(ov[6], ov[7]); v16[7] = vziph_u16(ov[6], ov[7]);
v32[0] = vzip_u32(vreinterpret_u32_u16(v16[0].val[0]), vreinterpret_u32_u16(v16[2].val[0]) );
v32[1] = vzip_u32(vreinterpret_u32_u16(v16[0].val[1]), vreinterpret_u32_u16(v16[2].val[1]) );
v32[2] = vzip_u32(vreinterpret_u32_u16(v16[1].val[0]), vreinterpret_u32_u16(v16[3].val[0]) );
v32[3] = vzip_u32(vreinterpret_u32_u16(v16[1].val[1]), vreinterpret_u32_u16(v16[3].val[1]) );
v32[4] = vzip_u32(vreinterpret_u32_u16(v16[4].val[0]), vreinterpret_u32_u16(v16[6].val[0]) );
v32[5] = vzip_u32(vreinterpret_u32_u16(v16[4].val[1]), vreinterpret_u32_u16(v16[6].val[1]) );
v32[6] = vzip_u32(vreinterpret_u32_u16(v16[5].val[0]), vreinterpret_u32_u16(v16[7].val[0]) );
v32[7] = vzip_u32(vreinterpret_u32_u16(v16[5].val[1]), vreinterpret_u32_u16(v16[7].val[1]) );
iv[0] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[0].val[0]), vreinterpret_u64_u32(v32[4].val[0]) );
iv[1] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[0].val[1]), vreinterpret_u64_u32(v32[4].val[1]) );
iv[2] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[1].val[0]), vreinterpret_u64_u32(v32[5].val[0]) );
iv[3] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[1].val[1]), vreinterpret_u64_u32(v32[5].val[1]) );
iv[4] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[2].val[0]), vreinterpret_u64_u32(v32[6].val[0]) );
iv[5] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[2].val[1]), vreinterpret_u64_u32(v32[6].val[1]) );
iv[6] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[3].val[0]), vreinterpret_u64_u32(v32[7].val[0]) );
iv[7] = (__m128i)vcombine_u64(vreinterpret_u64_u32(v32[3].val[1]), vreinterpret_u64_u32(v32[7].val[1]) );
#endif
#if STRIDE <= ESIZE
ST0(p,iv[0]); ST(p,iv[1],1); ST(p,iv[2],2); ST(p,iv[3],3); ST(p,iv[4],4); ST(p,iv[5],5); ST(p,iv[6],6); ST(p,iv[7],7);
#endif
#else // SSE
ov[0] = LD128(ip ); VE128(ov[0],sv); ov[0] = _mm_shuffle_epi8(ov[0], sf);
ov[1] = LD128(ip+16); VE128(ov[1],sv); ov[1] = _mm_shuffle_epi8(ov[1], sf);
ov[2] = LD128(ip+32); VE128(ov[2],sv); ov[2] = _mm_shuffle_epi8(ov[2], sf);
ov[3] = LD128(ip+48); VE128(ov[3],sv); ov[3] = _mm_shuffle_epi8(ov[3], sf);
iv[0] = _mm_unpacklo_epi16(ov[0], ov[1]); iv[1] = _mm_unpackhi_epi16(ov[0], ov[1]);
iv[2] = _mm_unpacklo_epi16(ov[2], ov[3]); iv[3] = _mm_unpackhi_epi16(ov[2], ov[3]);
ov[0] = _mm_unpacklo_epi32(iv[0], iv[2]); ov[1] = _mm_unpackhi_epi32(iv[0], iv[2]);
ov[2] = _mm_unpacklo_epi32(iv[1], iv[3]); ov[3] = _mm_unpackhi_epi32(iv[1], iv[3]);
ov[4] = LD128(ip+ 64); VE128(ov[4],sv); ov[4] = _mm_shuffle_epi8(ov[4], sf);
ov[5] = LD128(ip+ 80); VE128(ov[5],sv); ov[5] = _mm_shuffle_epi8(ov[5], sf);
ov[6] = LD128(ip+ 96); VE128(ov[6],sv); ov[6] = _mm_shuffle_epi8(ov[6], sf);
ov[7] = LD128(ip+112); VE128(ov[7],sv); ov[7] = _mm_shuffle_epi8(ov[7], sf);
iv[4] = _mm_unpacklo_epi16(ov[4], ov[5]); iv[5] = _mm_unpackhi_epi16(ov[4], ov[5]);
iv[6] = _mm_unpacklo_epi16(ov[6], ov[7]); iv[7] = _mm_unpackhi_epi16(ov[6], ov[7]);
ov[4] = _mm_unpacklo_epi32(iv[4], iv[6]); ov[5] = _mm_unpackhi_epi32(iv[4], iv[6]);
ov[6] = _mm_unpacklo_epi32(iv[5], iv[7]); ov[7] = _mm_unpackhi_epi32(iv[5], iv[7]);
iv[0] = _mm_unpacklo_epi64(ov[0], ov[4]); iv[1] = _mm_unpackhi_epi64(ov[0], ov[4]);
iv[2] = _mm_unpacklo_epi64(ov[1], ov[5]); iv[3] = _mm_unpackhi_epi64(ov[1], ov[5]);
iv[4] = _mm_unpacklo_epi64(ov[2], ov[6]); iv[5] = _mm_unpackhi_epi64(ov[2], ov[6]);
iv[6] = _mm_unpacklo_epi64(ov[3], ov[7]); iv[7] = _mm_unpackhi_epi64(ov[3], ov[7]);
#if STRIDE <= ESIZE
ST0(p,iv[0]); ST(p,iv[1],1); ST(p,iv[2],2); ST(p,iv[3],3); ST(p,iv[4],4); ST(p,iv[5],5); ST(p,iv[6],6); ST(p,iv[7],7);
#endif
#endif
#endif
#elif defined(__SSE3__)
#if ESIZE == 2
iv[0] = LD128(ip ); VE128(iv[0],sv);
iv[1] = LD128(ip+16)); VE128(iv[1],sv);
ov[0] = _mm_unpacklo_epi8(iv[0], iv[1]); ov[1] = _mm_unpackhi_epi8(iv[0], iv[1]);
iv[0] = _mm_unpacklo_epi8(ov[0], ov[1]); iv[1] = _mm_unpackhi_epi8(ov[0], ov[1]);
ov[0] = _mm_unpacklo_epi8(iv[0], iv[1]); ov[1] = _mm_unpackhi_epi8(iv[0], iv[1]);
iv[0] = _mm_unpacklo_epi8(ov[0], ov[1]); iv[1] = _mm_unpackhi_epi8(ov[0], ov[1]);
ST0(p,iv[0]); ST(p,iv[1],1);
#elif ESIZE == 4
iv[0] = LD128(ip ); VE128(iv[0],sv);
iv[1] = LD128(ip+16); VE128(iv[1],sv);
iv[2] = LD128(ip+32); VE128(iv[2],sv);
iv[3] = LD128(ip+48); VE128(iv[3],sv);
ov[0] = _mm_unpacklo_epi8( iv[0], iv[1]); ov[1] = _mm_unpackhi_epi8( iv[0], iv[1]);
iv[0] = _mm_unpacklo_epi8( ov[0], ov[1]); iv[1] = _mm_unpackhi_epi8( ov[0], ov[1]);
ov[0] = _mm_unpacklo_epi8( iv[0], iv[1]); ov[1] = _mm_unpackhi_epi8( iv[0], iv[1]);
iv[0] = _mm_unpacklo_epi64(ov[0], ov[2]); iv[1] = _mm_unpackhi_epi64(ov[0], ov[2]);
ov[2] = _mm_unpacklo_epi8( iv[2], iv[3]); ov[3] = _mm_unpackhi_epi8( iv[2], iv[3]);
iv[2] = _mm_unpacklo_epi8( ov[2], ov[3]); iv[3] = _mm_unpackhi_epi8( ov[2], ov[3]);
ov[2] = _mm_unpacklo_epi8( iv[2], iv[3]); ov[3] = _mm_unpackhi_epi8( iv[2], iv[3]);
iv[2] = _mm_unpacklo_epi64(ov[1], ov[3]); iv[3] = _mm_unpackhi_epi64(ov[1], ov[3]);
ST0(p,iv[0]); ST(p,iv[1],1); ST(p,iv[2],2); ST(p,iv[3],3);
#elif ESIZE == 8
iv[0] = LD128(ip ); VE128(iv[0],sv);
iv[1] = LD128(ip+16); VE128(iv[1],sv);
iv[2] = LD128(ip+32); VE128(iv[2],sv);
iv[3] = LD128(ip+48); VE128(iv[2],sv);
iv[4] = LD128(ip+64); VE128(iv[4],sv);
iv[5] = LD128(ip+80); VE128(iv[5],sv);
iv[6] = LD128(ip+96); VE128(iv[6],sv);
iv[7] = LD128(ip+112);VE128(iv[7],sv);
ov[0] = _mm_unpacklo_epi8( iv[0], iv[1]); ov[1] = _mm_unpackhi_epi8( iv[0], iv[1]);
ov[2] = _mm_unpacklo_epi8( iv[2], iv[3]); ov[3] = _mm_unpackhi_epi8( iv[2], iv[3]);
ov[4] = _mm_unpacklo_epi8( iv[4], iv[5]); ov[5] = _mm_unpackhi_epi8( iv[4], iv[5]);
ov[6] = _mm_unpacklo_epi8( iv[6], iv[7]); ov[7] = _mm_unpackhi_epi8( iv[6], iv[7]);
iv[0] = _mm_unpacklo_epi8( ov[0], ov[1]); iv[1] = _mm_unpackhi_epi8( ov[0], ov[1]);
iv[2] = _mm_unpacklo_epi8( ov[2], ov[3]); iv[3] = _mm_unpackhi_epi8( ov[2], ov[3]);
iv[4] = _mm_unpacklo_epi8( ov[4], ov[5]); iv[5] = _mm_unpackhi_epi8( ov[4], ov[5]);
iv[6] = _mm_unpacklo_epi8( ov[6], ov[7]); iv[7] = _mm_unpackhi_epi8( ov[6], ov[7]);
ov[0] = _mm_unpacklo_epi32(iv[0], iv[2]); ov[1] = _mm_unpackhi_epi32(iv[0], iv[2]);
ov[2] = _mm_unpacklo_epi32(iv[1], iv[3]); ov[3] = _mm_unpackhi_epi32(iv[1], iv[3]);
ov[4] = _mm_unpacklo_epi32(iv[4], iv[6]); ov[5] = _mm_unpackhi_epi32(iv[4], iv[6]);
ov[6] = _mm_unpacklo_epi32(iv[5], iv[7]); ov[7] = _mm_unpackhi_epi32(iv[5], iv[7]);
ST0(p,iv[0]); ST(p,iv[1],1); ST(p,iv[2],2); ST(p,iv[3],3);
iv[0] = _mm_unpacklo_epi64(ov[0], ov[4]); iv[1] = _mm_unpackhi_epi64(ov[0], ov[4]);
iv[2] = _mm_unpacklo_epi64(ov[1], ov[5]); iv[3] = _mm_unpackhi_epi64(ov[1], ov[5]);
iv[4] = _mm_unpacklo_epi64(ov[2], ov[6]); iv[5] = _mm_unpackhi_epi64(ov[2], ov[6]);
iv[6] = _mm_unpacklo_epi64(ov[3], ov[7]); iv[7] = _mm_unpackhi_epi64(ov[3], ov[7]);
ST(p,iv[4],4);
ST(p,iv[5],5);
ST(p,iv[6],6);
ST(p,iv[7],7);
#endif
#endif
#if STRIDE > ESIZE // ---------------------- Nibble -------------------------------------------
#define STL(_p_,_v_,_i_) _mm_storel_epi64((__m128i *)SIE(_p_,_i_), _v_)
#define STL0(_p_,_v_) _mm_storel_epi64((__m128i *)(_p_), _v_)
ov[0] = _mm_and_si128(iv[0], cl); ov[0] = _mm_and_si128(_mm_or_si128(_mm_srli_epi16(ov[0],4), ov[0]),cb); ov[0] = _mm_packus_epi16(ov[0], _mm_srli_si128(ov[0],2));
ov[1] = _mm_srli_epi16(_mm_and_si128(iv[0], ch),4); ov[1] = _mm_and_si128(_mm_or_si128(_mm_srli_epi16(ov[1],4), ov[1]),cb); ov[1] = _mm_packus_epi16(ov[1], _mm_srli_si128(ov[1],2));
ov[2] = _mm_and_si128(iv[1], cl); ov[2] = _mm_and_si128(_mm_or_si128(_mm_srli_epi16(ov[2],4), ov[2]),cb); ov[2] = _mm_packus_epi16(ov[2], _mm_srli_si128(ov[2],2));
ov[3] = _mm_srli_epi16(_mm_and_si128(iv[1], ch),4); ov[3] = _mm_and_si128(_mm_or_si128(_mm_srli_epi16(ov[3],4), ov[3]),cb); ov[3] = _mm_packus_epi16(ov[3], _mm_srli_si128(ov[3],2));
STL0(p,ov[0]); STL(p,ov[1],1);STL(p,ov[2],2);STL(p,ov[3],3);