summaryrefslogtreecommitdiff
path: root/thirdparty/embree/common/simd/arm/avx2neon.h
blob: dd321d3d640f5e7971d88177b324c7283afd1fd5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
#pragma once

#if !defined(__aarch64__)
#error "avx2neon is only supported for AARCH64"
#endif

#include "sse2neon.h"

#define AVX2NEON_ABI static inline  __attribute__((always_inline))


struct __m256 {
    __m128 lo,hi;
    __m256() {}
};




struct __m256i {
    __m128i lo,hi;
    explicit __m256i(const __m256 a) : lo(__m128i(a.lo)),hi(__m128i(a.hi)) {}
    operator __m256() const {__m256 res; res.lo = __m128(lo);res.hi = __m128(hi); return res;}
    __m256i() {}
};




struct __m256d {
    float64x2_t lo,hi;
    __m256d() {}
    __m256d(const __m256& a) : lo(float64x2_t(a.lo)),hi(float64x2_t(a.hi)) {}
    __m256d(const __m256i& a) : lo(float64x2_t(a.lo)),hi(float64x2_t(a.hi)) {}
};

#define UNARY_AVX_OP(type,func,basic_func) AVX2NEON_ABI type func(const type& a) {type res;res.lo=basic_func(a.lo);res.hi=basic_func(a.hi);return res;}


#define BINARY_AVX_OP(type,func,basic_func) AVX2NEON_ABI type func(const type& a,const type& b) {type res;res.lo=basic_func(a.lo,b.lo);res.hi=basic_func(a.hi,b.hi);return res;}
#define BINARY_AVX_OP_CAST(type,func,basic_func,bdst,bsrc) AVX2NEON_ABI type func(const type& a,const type& b) {type res;res.lo=bdst(basic_func(bsrc(a.lo),bsrc(b.lo)));res.hi=bdst(basic_func(bsrc(a.hi),bsrc(b.hi)));return res;}

#define TERNARY_AVX_OP(type,func,basic_func) AVX2NEON_ABI type func(const type& a,const type& b,const type& c) {type res;res.lo=basic_func(a.lo,b.lo,c.lo);res.hi=basic_func(a.hi,b.hi,c.hi);return res;}


#define CAST_SIMD_TYPE(to,name,from,basic_dst) AVX2NEON_ABI to name(const from& a) { to res; res.lo = basic_dst(a.lo); res.hi=basic_dst(a.hi); return res;}



#define _mm_stream_load_si128 _mm_load_si128
#define _mm256_stream_load_si256 _mm256_load_si256


AVX2NEON_ABI
__m128i _mm_blend_epi32 (__m128i a, __m128i b, const int imm8)
{
    __m128 af = _mm_castsi128_ps(a);
    __m128 bf = _mm_castsi128_ps(b);
    __m128 blendf = _mm_blend_ps(af, bf, imm8);
    return _mm_castps_si128(blendf);
}

AVX2NEON_ABI
int _mm_movemask_popcnt(__m128 a)
{
    return __builtin_popcount(_mm_movemask_ps(a));
}

AVX2NEON_ABI
__m128 _mm_maskload_ps (float const * mem_addr, __m128i mask)
{
    float32x4_t res;
    uint32x4_t mask_u32 = vreinterpretq_u32_m128i(mask);
    for (int i=0;i<4;i++) {
        if (mask_u32[i] & 0x80000000) res[i] = mem_addr[i]; else res[i] = 0;
    }
    return vreinterpretq_m128_f32(res);
}

AVX2NEON_ABI
void _mm_maskstore_ps (float * mem_addr, __m128i mask, __m128 a)
{
    float32x4_t a_f32 = vreinterpretq_f32_m128(a);
    uint32x4_t mask_u32 = vreinterpretq_u32_m128i(mask);
    for (int i=0;i<4;i++) {
        if (mask_u32[i] & 0x80000000) mem_addr[i] = a_f32[i];
    }
}

AVX2NEON_ABI
void _mm_maskstore_epi32 (int * mem_addr, __m128i mask, __m128i a)
{
    uint32x4_t mask_u32 = vreinterpretq_u32_m128i(mask);
    int32x4_t a_s32 = vreinterpretq_s32_m128i(a);
    for (int i=0;i<4;i++) {
        if (mask_u32[i] & 0x80000000) mem_addr[i] = a_s32[i];
    }
}


#define _mm_fmadd_ss _mm_fmadd_ps
#define _mm_fmsub_ss _mm_fmsub_ps
#define _mm_fnmsub_ss _mm_fnmsub_ps
#define _mm_fnmadd_ss _mm_fnmadd_ps

template<int code>
AVX2NEON_ABI float32x4_t dpps_neon(const float32x4_t& a,const float32x4_t& b)
{
    float v;
    v = 0;
    v += (code & 0x10) ? a[0]*b[0] : 0;
    v += (code & 0x20) ? a[1]*b[1] : 0;
    v += (code & 0x40) ? a[2]*b[2] : 0;
    v += (code & 0x80) ? a[3]*b[3] : 0;
    float32x4_t res;
    res[0] = (code & 0x1) ? v : 0;
    res[1] = (code & 0x2) ? v : 0;
    res[2] = (code & 0x4) ? v : 0;
    res[3] = (code & 0x8) ? v : 0;
    return res;
}

template<>
inline float32x4_t dpps_neon<0x7f>(const float32x4_t& a,const float32x4_t& b)
{
    float v;
    float32x4_t m = _mm_mul_ps(a,b);
    m[3] = 0;
    v = vaddvq_f32(m);
    return _mm_set1_ps(v);
}

template<>
inline float32x4_t dpps_neon<0xff>(const float32x4_t& a,const float32x4_t& b)
{
    float v;
    float32x4_t m = _mm_mul_ps(a,b);
    v = vaddvq_f32(m);
    return _mm_set1_ps(v);
}

#define _mm_dp_ps(a,b,c) dpps_neon<c>((a),(b))


AVX2NEON_ABI
__m128 _mm_permutevar_ps (__m128 a, __m128i b)
{
    uint32x4_t b_u32 = vreinterpretq_u32_m128i(b);
    float32x4_t x;
    for (int i=0;i<4;i++)
    {
        x[i] = a[b_u32[i]];
    }
    return vreinterpretq_m128_f32(x);
}

AVX2NEON_ABI
__m256i _mm256_setzero_si256()
{
    __m256i res;
    res.lo = res.hi = vdupq_n_s32(0);
    return res;
}

AVX2NEON_ABI
__m256 _mm256_setzero_ps()
{
    __m256 res;
    res.lo = res.hi = vdupq_n_f32(0.0f);
    return res;
}

AVX2NEON_ABI
__m256i _mm256_undefined_si256()
{
    return _mm256_setzero_si256();
}

AVX2NEON_ABI
__m256 _mm256_undefined_ps()
{
    return _mm256_setzero_ps();
}

CAST_SIMD_TYPE(__m256d, _mm256_castps_pd,    __m256,  float64x2_t)
CAST_SIMD_TYPE(__m256i, _mm256_castps_si256, __m256,  __m128i)
CAST_SIMD_TYPE(__m256,  _mm256_castsi256_ps, __m256i, __m128)
CAST_SIMD_TYPE(__m256,  _mm256_castpd_ps ,   __m256d, __m128)
CAST_SIMD_TYPE(__m256d, _mm256_castsi256_pd, __m256i, float64x2_t)
CAST_SIMD_TYPE(__m256i, _mm256_castpd_si256, __m256d, __m128i)




AVX2NEON_ABI
__m128 _mm256_castps256_ps128 (__m256 a)
{
    return a.lo;
}

AVX2NEON_ABI
__m256i _mm256_castsi128_si256 (__m128i a)
{
    __m256i res;
    res.lo = a ;
    res.hi = vdupq_n_s32(0);
    return res;
}

AVX2NEON_ABI
__m128i _mm256_castsi256_si128 (__m256i a)
{
    return a.lo;
}

AVX2NEON_ABI
__m256 _mm256_castps128_ps256 (__m128 a)
{
    __m256 res;
    res.lo = a;
    res.hi = vdupq_n_f32(0);
    return res;
}


AVX2NEON_ABI
__m256 _mm256_broadcast_ss (float const * mem_addr)
{
    __m256 res;
    res.lo = res.hi = vdupq_n_f32(*mem_addr);
    return res;
}


AVX2NEON_ABI
__m256i _mm256_set_epi32 (int e7, int e6, int e5, int e4, int e3, int e2, int e1, int e0)
{
    __m256i res;
    res.lo = _mm_set_epi32(e3,e2,e1,e0);
    res.hi = _mm_set_epi32(e7,e6,e5,e4);
    return res;

}

AVX2NEON_ABI
__m256i _mm256_set1_epi32 (int a)
{
    __m256i res;
    res.lo = res.hi = vdupq_n_s32(a);
    return res;
}
AVX2NEON_ABI
__m256i _mm256_set1_epi8 (int a)
{
    __m256i res;
    res.lo = res.hi = vdupq_n_s8(a);
    return res;
}
AVX2NEON_ABI
__m256i _mm256_set1_epi16 (int a)
{
    __m256i res;
    res.lo = res.hi = vdupq_n_s16(a);
    return res;
}




AVX2NEON_ABI
int _mm256_movemask_ps(const __m256& v)
{
    return (_mm_movemask_ps(v.hi) << 4) | _mm_movemask_ps(v.lo);
}

template<int imm8>
AVX2NEON_ABI
__m256 __mm256_permute_ps (const __m256& a)
{
    __m256 res;
    res.lo = _mm_shuffle_ps(a.lo,a.lo,imm8);
    res.hi = _mm_shuffle_ps(a.hi,a.hi,imm8);
    return res;

}

#define _mm256_permute_ps(a,c) __mm256_permute_ps<c>(a)


template<int imm8>
AVX2NEON_ABI
__m256 __mm256_shuffle_ps (const __m256 a,const __m256& b)
{
    __m256 res;
    res.lo = _mm_shuffle_ps(a.lo,b.lo,imm8);
    res.hi = _mm_shuffle_ps(a.hi,b.hi,imm8);
    return res;

}

template<int imm8>
AVX2NEON_ABI
__m256i __mm256_shuffle_epi32 (const __m256i a)
{
    __m256i res;
    res.lo = _mm_shuffle_epi32(a.lo,imm8);
    res.hi = _mm_shuffle_epi32(a.hi,imm8);
    return res;

}

template<int imm8>
AVX2NEON_ABI
__m256i __mm256_srli_si256 (__m256i a)
{
    __m256i res;
    res.lo = _mm_srli_si128(a.lo,imm8);
    res.hi = _mm_srli_si128(a.hi,imm8);
    return res;
}

template<int imm8>
AVX2NEON_ABI
__m256i __mm256_slli_si256 (__m256i a)
{
    __m256i res;
    res.lo = _mm_slli_si128(a.lo,imm8);
    res.hi = _mm_slli_si128(a.hi,imm8);
    return res;
}


#define _mm256_srli_si256(a,b) __mm256_srli_si256<b>(a)
#define _mm256_slli_si256(a,b) __mm256_slli_si256<b>(a)



#define _mm256_shuffle_ps(a,b,c) __mm256_shuffle_ps<c>(a,b)
#define _mm256_shuffle_epi32(a,c) __mm256_shuffle_epi32<c>(a)


AVX2NEON_ABI
__m256i _mm256_set1_epi64x (long long a)
{
    __m256i res;
    int64x2_t t = vdupq_n_s64(a);
    res.lo = res.hi = __m128i(t);
    return res;
}


AVX2NEON_ABI
__m256 _mm256_permute2f128_ps (__m256 a, __m256 b, int imm8)
{
    __m256 res;
    __m128 tmp;
    switch (imm8 & 0x7)
    {
        case 0: tmp = a.lo; break;
        case 1: tmp = a.hi; break;
        case 2: tmp = b.lo; break;
        case 3: tmp = b.hi; break;
    }
    if (imm8 & 0x8)
        tmp = _mm_setzero_ps();



    res.lo = tmp;
    imm8 >>= 4;

    switch (imm8 & 0x7)
    {
        case 0: tmp = a.lo; break;
        case 1: tmp = a.hi; break;
        case 2: tmp = b.lo; break;
        case 3: tmp = b.hi; break;
    }
    if (imm8 & 0x8)
        tmp = _mm_setzero_ps();

    res.hi = tmp;

    return res;
}

AVX2NEON_ABI
__m256 _mm256_moveldup_ps (__m256 a)
{
    __m256 res;
    res.lo = _mm_moveldup_ps(a.lo);
    res.hi = _mm_moveldup_ps(a.hi);
    return res;
}

AVX2NEON_ABI
__m256 _mm256_movehdup_ps (__m256 a)
{
    __m256 res;
    res.lo = _mm_movehdup_ps(a.lo);
    res.hi = _mm_movehdup_ps(a.hi);
    return res;
}

AVX2NEON_ABI
__m256 _mm256_insertf128_ps (__m256 a, __m128 b, int imm8)
{
    __m256 res = a;
    if (imm8 & 1) res.hi = b;
    else res.lo = b;
    return res;
}


AVX2NEON_ABI
__m128 _mm256_extractf128_ps (__m256 a, const int imm8)
{
    if (imm8 & 1) return a.hi;
    return a.lo;
}


AVX2NEON_ABI
__m256d _mm256_movedup_pd (__m256d a)
{
    __m256d res;
    res.lo = _mm_movedup_pd(a.lo);
    res.hi = _mm_movedup_pd(a.hi);
    return res;
}

AVX2NEON_ABI
__m256i _mm256_abs_epi32(__m256i a)
{
   __m256i res;
   res.lo = vabsq_s32(a.lo);
   res.hi = vabsq_s32(a.hi);
   return res;
}

UNARY_AVX_OP(__m256,_mm256_sqrt_ps,_mm_sqrt_ps)
UNARY_AVX_OP(__m256,_mm256_rsqrt_ps,_mm_rsqrt_ps)
UNARY_AVX_OP(__m256,_mm256_rcp_ps,_mm_rcp_ps)
UNARY_AVX_OP(__m256,_mm256_floor_ps,vrndmq_f32)
UNARY_AVX_OP(__m256,_mm256_ceil_ps,vrndpq_f32)
UNARY_AVX_OP(__m256i,_mm256_abs_epi16,_mm_abs_epi16)


BINARY_AVX_OP(__m256i,_mm256_add_epi8,_mm_add_epi8)
BINARY_AVX_OP(__m256i,_mm256_adds_epi8,_mm_adds_epi8)

BINARY_AVX_OP(__m256i,_mm256_hadd_epi32,_mm_hadd_epi32)
BINARY_AVX_OP(__m256i,_mm256_add_epi32,_mm_add_epi32)
BINARY_AVX_OP(__m256i,_mm256_sub_epi32,_mm_sub_epi32)
BINARY_AVX_OP(__m256i,_mm256_mullo_epi32,_mm_mullo_epi32)

BINARY_AVX_OP(__m256i,_mm256_min_epi32,_mm_min_epi32)
BINARY_AVX_OP(__m256i,_mm256_max_epi32,_mm_max_epi32)
BINARY_AVX_OP(__m256i,_mm256_min_epi16,_mm_min_epi16)
BINARY_AVX_OP(__m256i,_mm256_max_epi16,_mm_max_epi16)
BINARY_AVX_OP(__m256i,_mm256_min_epi8,_mm_min_epi8)
BINARY_AVX_OP(__m256i,_mm256_max_epi8,_mm_max_epi8)
BINARY_AVX_OP(__m256i,_mm256_min_epu16,_mm_min_epu16)
BINARY_AVX_OP(__m256i,_mm256_max_epu16,_mm_max_epu16)
BINARY_AVX_OP(__m256i,_mm256_min_epu8,_mm_min_epu8)
BINARY_AVX_OP(__m256i,_mm256_max_epu8,_mm_max_epu8)
BINARY_AVX_OP(__m256i,_mm256_sign_epi16,_mm_sign_epi16)


BINARY_AVX_OP_CAST(__m256i,_mm256_min_epu32,vminq_u32,__m128i,uint32x4_t)
BINARY_AVX_OP_CAST(__m256i,_mm256_max_epu32,vmaxq_u32,__m128i,uint32x4_t)

BINARY_AVX_OP(__m256,_mm256_min_ps,_mm_min_ps)
BINARY_AVX_OP(__m256,_mm256_max_ps,_mm_max_ps)

BINARY_AVX_OP(__m256,_mm256_add_ps,_mm_add_ps)
BINARY_AVX_OP(__m256,_mm256_mul_ps,_mm_mul_ps)
BINARY_AVX_OP(__m256,_mm256_sub_ps,_mm_sub_ps)
BINARY_AVX_OP(__m256,_mm256_div_ps,_mm_div_ps)

BINARY_AVX_OP(__m256,_mm256_and_ps,_mm_and_ps)
BINARY_AVX_OP(__m256,_mm256_andnot_ps,_mm_andnot_ps)
BINARY_AVX_OP(__m256,_mm256_or_ps,_mm_or_ps)
BINARY_AVX_OP(__m256,_mm256_xor_ps,_mm_xor_ps)

BINARY_AVX_OP_CAST(__m256d,_mm256_and_pd,vandq_s64,float64x2_t,int64x2_t)
BINARY_AVX_OP_CAST(__m256d,_mm256_or_pd,vorrq_s64,float64x2_t,int64x2_t)
BINARY_AVX_OP_CAST(__m256d,_mm256_xor_pd,veorq_s64,float64x2_t,int64x2_t)



BINARY_AVX_OP(__m256i,_mm256_and_si256,_mm_and_si128)
BINARY_AVX_OP(__m256i,_mm256_andnot_si256,_mm_andnot_si128)
BINARY_AVX_OP(__m256i,_mm256_or_si256,_mm_or_si128)
BINARY_AVX_OP(__m256i,_mm256_xor_si256,_mm_xor_si128)


BINARY_AVX_OP(__m256,_mm256_unpackhi_ps,_mm_unpackhi_ps)
BINARY_AVX_OP(__m256,_mm256_unpacklo_ps,_mm_unpacklo_ps)
TERNARY_AVX_OP(__m256,_mm256_blendv_ps,_mm_blendv_ps)
TERNARY_AVX_OP(__m256i,_mm256_blendv_epi8,_mm_blendv_epi8)


TERNARY_AVX_OP(__m256,_mm256_fmadd_ps,_mm_fmadd_ps)
TERNARY_AVX_OP(__m256,_mm256_fnmadd_ps,_mm_fnmadd_ps)
TERNARY_AVX_OP(__m256,_mm256_fmsub_ps,_mm_fmsub_ps)
TERNARY_AVX_OP(__m256,_mm256_fnmsub_ps,_mm_fnmsub_ps)



BINARY_AVX_OP(__m256i,_mm256_packs_epi32,_mm_packs_epi32)
BINARY_AVX_OP(__m256i,_mm256_packs_epi16,_mm_packs_epi16)
BINARY_AVX_OP(__m256i,_mm256_packus_epi32,_mm_packus_epi32)
BINARY_AVX_OP(__m256i,_mm256_packus_epi16,_mm_packus_epi16)


BINARY_AVX_OP(__m256i,_mm256_unpackhi_epi64,_mm_unpackhi_epi64)
BINARY_AVX_OP(__m256i,_mm256_unpackhi_epi32,_mm_unpackhi_epi32)
BINARY_AVX_OP(__m256i,_mm256_unpackhi_epi16,_mm_unpackhi_epi16)
BINARY_AVX_OP(__m256i,_mm256_unpackhi_epi8,_mm_unpackhi_epi8)

BINARY_AVX_OP(__m256i,_mm256_unpacklo_epi64,_mm_unpacklo_epi64)
BINARY_AVX_OP(__m256i,_mm256_unpacklo_epi32,_mm_unpacklo_epi32)
BINARY_AVX_OP(__m256i,_mm256_unpacklo_epi16,_mm_unpacklo_epi16)
BINARY_AVX_OP(__m256i,_mm256_unpacklo_epi8,_mm_unpacklo_epi8)

BINARY_AVX_OP(__m256i,_mm256_mulhrs_epi16,_mm_mulhrs_epi16)
BINARY_AVX_OP(__m256i,_mm256_mulhi_epu16,_mm_mulhi_epu16)
BINARY_AVX_OP(__m256i,_mm256_mulhi_epi16,_mm_mulhi_epi16)
//BINARY_AVX_OP(__m256i,_mm256_mullo_epu16,_mm_mullo_epu16)
BINARY_AVX_OP(__m256i,_mm256_mullo_epi16,_mm_mullo_epi16)

BINARY_AVX_OP(__m256i,_mm256_subs_epu16,_mm_subs_epu16)
BINARY_AVX_OP(__m256i,_mm256_adds_epu16,_mm_adds_epu16)
BINARY_AVX_OP(__m256i,_mm256_subs_epi16,_mm_subs_epi16)
BINARY_AVX_OP(__m256i,_mm256_adds_epi16,_mm_adds_epi16)
BINARY_AVX_OP(__m256i,_mm256_sub_epi16,_mm_sub_epi16)
BINARY_AVX_OP(__m256i,_mm256_add_epi16,_mm_add_epi16)
BINARY_AVX_OP(__m256i,_mm256_sub_epi8,_mm_sub_epi8)


BINARY_AVX_OP(__m256i,_mm256_hadd_epi16,_mm_hadd_epi16)
BINARY_AVX_OP(__m256i,_mm256_hadds_epi16,_mm_hadds_epi16)




BINARY_AVX_OP(__m256i,_mm256_cmpeq_epi32,_mm_cmpeq_epi32)
BINARY_AVX_OP(__m256i,_mm256_cmpgt_epi32,_mm_cmpgt_epi32)

BINARY_AVX_OP(__m256i,_mm256_cmpeq_epi8,_mm_cmpeq_epi8)
BINARY_AVX_OP(__m256i,_mm256_cmpgt_epi8,_mm_cmpgt_epi8)

BINARY_AVX_OP(__m256i,_mm256_cmpeq_epi16,_mm_cmpeq_epi16)
BINARY_AVX_OP(__m256i,_mm256_cmpgt_epi16,_mm_cmpgt_epi16)


BINARY_AVX_OP(__m256i,_mm256_shuffle_epi8,_mm_shuffle_epi8)


BINARY_AVX_OP(__m256,_mm256_cmpeq_ps,_mm_cmpeq_ps)
BINARY_AVX_OP(__m256,_mm256_cmpneq_ps,_mm_cmpneq_ps)
BINARY_AVX_OP(__m256,_mm256_cmpnlt_ps,_mm_cmpnlt_ps)
BINARY_AVX_OP(__m256,_mm256_cmpngt_ps,_mm_cmpngt_ps)
BINARY_AVX_OP(__m256,_mm256_cmpge_ps,_mm_cmpge_ps)
BINARY_AVX_OP(__m256,_mm256_cmpnge_ps,_mm_cmpnge_ps)
BINARY_AVX_OP(__m256,_mm256_cmplt_ps,_mm_cmplt_ps)
BINARY_AVX_OP(__m256,_mm256_cmple_ps,_mm_cmple_ps)
BINARY_AVX_OP(__m256,_mm256_cmpgt_ps,_mm_cmpgt_ps)
BINARY_AVX_OP(__m256,_mm256_cmpnle_ps,_mm_cmpnle_ps)


AVX2NEON_ABI
__m256i _mm256_cvtps_epi32 (__m256 a)
{
    __m256i res;
    res.lo = _mm_cvtps_epi32(a.lo);
    res.hi = _mm_cvtps_epi32(a.hi);
    return res;

}

AVX2NEON_ABI
__m256i _mm256_cvttps_epi32 (__m256 a)
{
    __m256i res;
    res.lo = _mm_cvttps_epi32(a.lo);
    res.hi = _mm_cvttps_epi32(a.hi);
    return res;

}

AVX2NEON_ABI
__m256 _mm256_loadu_ps (float const * mem_addr)
{
    __m256 res;
    res.lo = *(__m128 *)(mem_addr + 0);
    res.hi = *(__m128 *)(mem_addr + 4);
    return res;
}
#define _mm256_load_ps _mm256_loadu_ps


AVX2NEON_ABI
int _mm256_testz_ps (const __m256& a, const __m256& b)
{
    __m256 t = a;
    if (&a != &b)
        t = _mm256_and_ps(a,b);

    int32x4_t l  = vshrq_n_s32(vreinterpretq_s32_m128(t.lo),31);
    int32x4_t h  = vshrq_n_s32(vreinterpretq_s32_m128(t.hi),31);
    return vaddvq_s32(vaddq_s32(l,h)) == 0;
}


AVX2NEON_ABI
__m256i _mm256_set_epi64x (int64_t e3, int64_t e2, int64_t e1, int64_t e0)
{
    __m256i res;
    int64x2_t t0 = {e0,e1};
    int64x2_t t1 = {e2,e3};
    res.lo = __m128i(t0);
    res.hi = __m128i(t1);
    return res;
}
AVX2NEON_ABI
__m256i _mm256_setr_epi64x (int64_t e0, int64_t e1, int64_t e2, int64_t e3)
{
    __m256i res;
    int64x2_t t0 = {e0,e1};
    int64x2_t t1 = {e2,e3};
    res.lo = __m128i(t0);
    res.hi = __m128i(t1);
    return res;
}



AVX2NEON_ABI
__m256i _mm256_set_epi8 (char e31, char e30, char e29, char e28, char e27, char e26, char e25, char e24, char e23, char e22, char e21, char e20, char e19, char e18, char e17, char e16, char e15, char e14, char e13, char e12, char e11, char e10, char e9, char e8, char e7, char e6, char e5, char e4, char e3, char e2, char e1, char e0)
{
    int8x16_t lo = {e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15};
    int8x16_t hi = {e16,e17,e18,e19,e20,e21,e22,e23,e24,e25,e26,e27,e28,e29,e30,e31};
    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}

AVX2NEON_ABI
__m256i _mm256_setr_epi8 (char e0, char e1, char e2, char e3, char e4, char e5, char e6, char e7, char e8, char e9, char e10, char e11, char e12, char e13, char e14, char e15, char e16, char e17, char e18, char e19, char e20, char e21, char e22, char e23, char e24, char e25, char e26, char e27, char e28, char e29, char e30, char e31)
{
    int8x16_t lo = {e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15};
    int8x16_t hi = {e16,e17,e18,e19,e20,e21,e22,e23,e24,e25,e26,e27,e28,e29,e30,e31};
    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}


AVX2NEON_ABI
__m256i _mm256_set_epi16 (short e15, short e14, short e13, short e12, short e11, short e10, short e9, short e8, short e7, short e6, short e5, short e4, short e3, short e2, short e1, short e0)
{
    int16x8_t lo = {e0,e1,e2,e3,e4,e5,e6,e7};
    int16x8_t hi = {e8,e9,e10,e11,e12,e13,e14,e15};
    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}

AVX2NEON_ABI
__m256i _mm256_setr_epi16 (short e0, short e1, short e2, short e3, short e4, short e5, short e6, short e7, short e8, short e9, short e10, short e11, short e12, short e13, short e14, short e15)
{
    int16x8_t lo = {e0,e1,e2,e3,e4,e5,e6,e7};
    int16x8_t hi = {e8,e9,e10,e11,e12,e13,e14,e15};
    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}




AVX2NEON_ABI
int _mm256_movemask_epi8(const __m256i& a)
{
    return (_mm_movemask_epi8(a.hi) << 16) | _mm_movemask_epi8(a.lo);
}


AVX2NEON_ABI
int _mm256_testz_si256(const __m256i& a,const __m256i& b)
{
    uint32x4_t lo = vandq_u32(a.lo,b.lo);
    uint32x4_t hi = vandq_u32(a.hi,b.hi);

    return (vaddvq_u32(lo) + vaddvq_u32(hi)) == 0;
}

AVX2NEON_ABI
__m256d _mm256_setzero_pd ()
{
    __m256d res;
    res.lo = res.hi = vdupq_n_f64(0);
    return res;
}

AVX2NEON_ABI
int _mm256_movemask_pd (__m256d a)
{
    return (_mm_movemask_pd(a.hi) << 2) | _mm_movemask_pd(a.lo);
}

AVX2NEON_ABI
__m256i _mm256_cmpeq_epi64 (__m256i a, __m256i b)
{
    __m256i res;
    res.lo = _mm_cmpeq_epi64(a.lo, b.lo);
    res.hi = _mm_cmpeq_epi64(a.hi, b.hi);
    return res;
}

AVX2NEON_ABI
__m256d _mm256_cmpeq_pd (__m256d a, __m256d b)
{
    __m256d res;
    res.lo = _mm_cmpeq_pd(a.lo, b.lo);
    res.hi = _mm_cmpeq_pd(a.hi, b.hi);
    return res;
}


AVX2NEON_ABI
int _mm256_testz_pd (const __m256d& a, const __m256d& b)
{
    __m256d t = a;

    if (&a != &b)
        t = _mm256_and_pd(a,b);

    return _mm256_movemask_pd(t) == 0;
}

AVX2NEON_ABI
__m256d _mm256_blendv_pd (__m256d a, __m256d b, __m256d mask)
{
    __m256d res;
    res.lo = _mm_blendv_pd(a.lo, b.lo, mask.lo);
    res.hi = _mm_blendv_pd(a.hi, b.hi, mask.hi);
    return res;
}

template<int imm8>
AVX2NEON_ABI
__m256 __mm256_dp_ps (__m256 a, __m256 b)
{
    __m256 res;
    res.lo = _mm_dp_ps(a.lo, b.lo, imm8);
    res.hi = _mm_dp_ps(a.hi, b.hi, imm8);
    return res;
}

#define _mm256_dp_ps(a,b,c) __mm256_dp_ps<c>(a,b)

AVX2NEON_ABI
double _mm256_permute4x64_pd_select(__m256d a, const int imm8)
{
    switch (imm8 & 3) {
        case 0:
            return ((float64x2_t)a.lo)[0];
        case 1:
            return ((float64x2_t)a.lo)[1];
        case 2:
            return ((float64x2_t)a.hi)[0];
        case 3:
            return ((float64x2_t)a.hi)[1];
    }
    __builtin_unreachable();
    return 0;
}

AVX2NEON_ABI
__m256d _mm256_permute4x64_pd (__m256d a, const int imm8)
{
    float64x2_t lo,hi;
    lo[0] = _mm256_permute4x64_pd_select(a,imm8 >> 0);
    lo[1] = _mm256_permute4x64_pd_select(a,imm8 >> 2);
    hi[0] = _mm256_permute4x64_pd_select(a,imm8 >> 4);
    hi[1] = _mm256_permute4x64_pd_select(a,imm8 >> 6);

    __m256d res;
    res.lo = lo; res.hi = hi;
    return res;
}

AVX2NEON_ABI
__m256i _mm256_insertf128_si256 (__m256i a, __m128i b, int imm8)
{
    return __m256i(_mm256_insertf128_ps((__m256)a,(__m128)b,imm8));
}


AVX2NEON_ABI
__m256i _mm256_loadu_si256 (__m256i const * mem_addr)
{
    __m256i res;
    res.lo = *(__m128i *)((int32_t *)mem_addr + 0);
    res.hi = *(__m128i *)((int32_t *)mem_addr + 4);
    return res;
}

#define _mm256_load_si256 _mm256_loadu_si256

AVX2NEON_ABI
void _mm256_storeu_ps (float * mem_addr, __m256 a)
{
    *(__m128 *)(mem_addr + 0) = a.lo;
    *(__m128 *)(mem_addr + 4) = a.hi;
}

#define _mm256_store_ps _mm256_storeu_ps
#define _mm256_stream_ps _mm256_storeu_ps


AVX2NEON_ABI
void _mm256_storeu_si256 (__m256i * mem_addr, __m256i a)
{
    *(__m128i *)((int32_t *)mem_addr + 0) = a.lo;
    *(__m128i *)((int32_t *)mem_addr + 4) = a.hi;
}

#define _mm256_store_si256 _mm256_storeu_si256



AVX2NEON_ABI
__m256i _mm256_permute4x64_epi64 (const __m256i a, const int imm8)
{
    uint8x16x2_t tbl = {a.lo, a.hi};

    uint8_t sz = sizeof(uint64_t);
    uint8_t u64[4] = {
        (uint8_t)(((imm8 >> 0) & 0x3) * sz),
        (uint8_t)(((imm8 >> 2) & 0x3) * sz),
        (uint8_t)(((imm8 >> 4) & 0x3) * sz),
        (uint8_t)(((imm8 >> 6) & 0x3) * sz),
    };

    uint8x16_t idx_lo = {
        // lo[0] bytes
        (uint8_t)(u64[0]+0), (uint8_t)(u64[0]+1), (uint8_t)(u64[0]+2), (uint8_t)(u64[0]+3),
        (uint8_t)(u64[0]+4), (uint8_t)(u64[0]+5), (uint8_t)(u64[0]+6), (uint8_t)(u64[0]+7),

        // lo[1] bytes
        (uint8_t)(u64[1]+0), (uint8_t)(u64[1]+1), (uint8_t)(u64[1]+2), (uint8_t)(u64[1]+3),
        (uint8_t)(u64[1]+4), (uint8_t)(u64[1]+5), (uint8_t)(u64[1]+6), (uint8_t)(u64[1]+7),
    };
    uint8x16_t idx_hi = {
        // hi[0] bytes
        (uint8_t)(u64[2]+0), (uint8_t)(u64[2]+1), (uint8_t)(u64[2]+2), (uint8_t)(u64[2]+3),
        (uint8_t)(u64[2]+4), (uint8_t)(u64[2]+5), (uint8_t)(u64[2]+6), (uint8_t)(u64[2]+7),

        // hi[1] bytes
        (uint8_t)(u64[3]+0), (uint8_t)(u64[3]+1), (uint8_t)(u64[3]+2), (uint8_t)(u64[3]+3),
        (uint8_t)(u64[3]+4), (uint8_t)(u64[3]+5), (uint8_t)(u64[3]+6), (uint8_t)(u64[3]+7),
    };

    uint8x16_t lo = vqtbl2q_u8(tbl, idx_lo);
    uint8x16_t hi = vqtbl2q_u8(tbl, idx_hi);

    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}


AVX2NEON_ABI
__m256i _mm256_permute2x128_si256(const __m256i a,const __m256i b, const int imm8)
{
    return __m256i(_mm256_permute2f128_ps(__m256(a),__m256(b),imm8));
}



AVX2NEON_ABI
__m256 _mm256_maskload_ps (float const * mem_addr, __m256i mask)
{
    __m256 res;
    res.lo = _mm_maskload_ps(mem_addr,mask.lo);
    res.hi = _mm_maskload_ps(mem_addr + 4,mask.hi);
    return res;
}


AVX2NEON_ABI
__m256i _mm256_cvtepu8_epi32 (__m128i a)
{
    uint8x16_t a_u8 = vreinterpretq_u8_m128i(a);     // xxxx xxxx xxxx xxxx HHGG FFEE DDCC BBAA
    uint16x8_t u16x8 = vmovl_u8(vget_low_u8(a_u8));  // 00HH 00GG 00FF 00EE 00DD 00CC 00BB 00AA
    uint32x4_t lo = vmovl_u16(vget_low_u16(u16x8));  // 0000 00DD 0000 00CC 0000 00BB 0000 00AA
    uint32x4_t hi = vmovl_high_u16(u16x8);           // 0000 00HH 0000 00GG 0000 00FF 0000 00EE

    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}


AVX2NEON_ABI
__m256i _mm256_cvtepi8_epi32 (__m128i a)
{
    int8x16_t a_s8 = vreinterpretq_s8_m128i(a);     // xxxx xxxx xxxx xxxx HHGG FFEE DDCC BBAA
    int16x8_t s16x8 = vmovl_s8(vget_low_s8(a_s8));  // ssHH ssGG ssFF ssEE ssDD ssCC ssBB ssAA
    int32x4_t lo = vmovl_s16(vget_low_s16(s16x8));  // ssss ssDD ssss ssCC ssss ssBB ssss ssAA
    int32x4_t hi = vmovl_high_s16(s16x8);           // ssss ssHH ssss ssGG ssss ssFF ssss ssEE

    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}


AVX2NEON_ABI
__m256i _mm256_cvtepi16_epi32 (__m128i a)
{
    int16x8_t a_s16 = vreinterpretq_s16_m128i(a);   // HHHH GGGG FFFF EEEE DDDD CCCC BBBB AAAA
    int32x4_t lo = vmovl_s16(vget_low_s16(a_s16));  // ssss DDDD ssss CCCC ssss BBBB ssss AAAA
    int32x4_t hi = vmovl_high_s16(a_s16);           // ssss HHHH ssss GGGG ssss FFFF ssss EEEE

    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}



AVX2NEON_ABI
void _mm256_maskstore_epi32 (int* mem_addr, __m256i mask, __m256i a)
{
    _mm_maskstore_epi32(mem_addr,mask.lo,a.lo);
    _mm_maskstore_epi32(mem_addr + 4,mask.hi,a.hi);
}

AVX2NEON_ABI
__m256i _mm256_slli_epi64 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_slli_epi64(a.lo,imm8);
    res.hi = _mm_slli_epi64(a.hi,imm8);
    return res;
}

AVX2NEON_ABI
__m256i _mm256_slli_epi32 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_slli_epi32(a.lo,imm8);
    res.hi = _mm_slli_epi32(a.hi,imm8);
    return res;
}


AVX2NEON_ABI
__m256i __mm256_slli_epi16 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_slli_epi16(a.lo,imm8);
    res.hi = _mm_slli_epi16(a.hi,imm8);
    return res;
}


AVX2NEON_ABI
__m256i _mm256_srli_epi32 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_srli_epi32(a.lo,imm8);
    res.hi = _mm_srli_epi32(a.hi,imm8);
    return res;
}

AVX2NEON_ABI
__m256i __mm256_srli_epi16 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_srli_epi16(a.lo,imm8);
    res.hi = _mm_srli_epi16(a.hi,imm8);
    return res;
}

AVX2NEON_ABI
__m256i _mm256_cvtepu16_epi32(__m128i a)
{
    __m256i res;
    res.lo = vmovl_u16(vget_low_u16(a));
    res.hi = vmovl_high_u16(a);
    return res;
}

AVX2NEON_ABI
__m256i _mm256_cvtepu8_epi16(__m128i a)
{
    __m256i res;
    res.lo = vmovl_u8(vget_low_u8(a));
    res.hi = vmovl_high_u8(a);
    return res;
}


AVX2NEON_ABI
__m256i _mm256_srai_epi32 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_srai_epi32(a.lo,imm8);
    res.hi = _mm_srai_epi32(a.hi,imm8);
    return res;
}

AVX2NEON_ABI
__m256i _mm256_srai_epi16 (__m256i a, int imm8)
{
    __m256i res;
    res.lo = _mm_srai_epi16(a.lo,imm8);
    res.hi = _mm_srai_epi16(a.hi,imm8);
    return res;
}


AVX2NEON_ABI
__m256i _mm256_sllv_epi32 (__m256i a, __m256i count)
{
    __m256i res;
    res.lo = vshlq_s32(a.lo,count.lo);
    res.hi = vshlq_s32(a.hi,count.hi);
    return res;

}


AVX2NEON_ABI
__m256i _mm256_srav_epi32 (__m256i a, __m256i count)
{
    __m256i res;
    res.lo = vshlq_s32(a.lo,vnegq_s32(count.lo));
    res.hi = vshlq_s32(a.hi,vnegq_s32(count.hi));
    return res;

}

AVX2NEON_ABI
__m256i _mm256_srlv_epi32 (__m256i a, __m256i count)
{
    __m256i res;
    res.lo = __m128i(vshlq_u32(uint32x4_t(a.lo),vnegq_s32(count.lo)));
    res.hi = __m128i(vshlq_u32(uint32x4_t(a.hi),vnegq_s32(count.hi)));
    return res;

}


AVX2NEON_ABI
__m256i _mm256_permute2f128_si256 (__m256i a, __m256i b, int imm8)
{
    return __m256i(_mm256_permute2f128_ps(__m256(a),__m256(b),imm8));
}


AVX2NEON_ABI
__m128i _mm256_extractf128_si256 (__m256i a, const int imm8)
{
    if (imm8 & 1) return a.hi;
    return a.lo;
}

AVX2NEON_ABI
__m256 _mm256_set1_ps(float x)
{
    __m256 res;
    res.lo = res.hi = vdupq_n_f32(x);
    return res;
}

AVX2NEON_ABI
__m256 _mm256_set_ps (float e7, float e6, float e5, float e4, float e3, float e2, float e1, float e0)
{
    __m256 res;
    res.lo = _mm_set_ps(e3,e2,e1,e0);
    res.hi = _mm_set_ps(e7,e6,e5,e4);
    return res;
}

AVX2NEON_ABI
__m256 _mm256_broadcast_ps (__m128 const * mem_addr)
{
    __m256 res;
    res.lo = res.hi = *mem_addr;
    return res;
}

AVX2NEON_ABI
__m256 _mm256_cvtepi32_ps (__m256i a)
{
    __m256 res;
    res.lo = _mm_cvtepi32_ps(a.lo);
    res.hi = _mm_cvtepi32_ps(a.hi);
    return res;
}
AVX2NEON_ABI
void _mm256_maskstore_ps (float * mem_addr, __m256i mask, __m256 a)
{
    uint32x4_t mask_lo = mask.lo;
    uint32x4_t mask_hi = mask.hi;
    float32x4_t a_lo = a.lo;
    float32x4_t a_hi = a.hi;

    for (int i=0;i<4;i++) {
        if (mask_lo[i] & 0x80000000) mem_addr[i] = a_lo[i];
        if (mask_hi[i] & 0x80000000) mem_addr[i+4] = a_hi[i];
    }
}

AVX2NEON_ABI
__m256d _mm256_andnot_pd (__m256d a, __m256d b)
{
    __m256d res;
    res.lo = float64x2_t(_mm_andnot_ps(__m128(a.lo),__m128(b.lo)));
    res.hi = float64x2_t(_mm_andnot_ps(__m128(a.hi),__m128(b.hi)));
    return res;
}

AVX2NEON_ABI
__m256 _mm256_blend_ps (__m256 a, __m256 b, const int imm8)
{
    __m256 res;
    res.lo = _mm_blend_ps(a.lo,b.lo,imm8 & 0xf);
    res.hi = _mm_blend_ps(a.hi,b.hi,imm8 >> 4);
    return res;

}


AVX2NEON_ABI
__m256i _mm256_blend_epi32 (__m256i a, __m256i b, const int imm8)
{
    return __m256i(_mm256_blend_ps(__m256(a),__m256(b),imm8));

}

AVX2NEON_ABI
__m256i _mm256_blend_epi16 (__m256i a, __m256i b, const int imm8)
{
    __m256i res;
    res.lo = _mm_blend_epi16(a.lo,b.lo,imm8);
    res.hi = _mm_blend_epi16(a.hi,b.hi,imm8);
    return res;
}



AVX2NEON_ABI
__m256i _mm256_i32gather_epi32 (int const* base_addr, __m256i vindex, const int scale)
{
    int32x4_t vindex_lo = vindex.lo;
    int32x4_t vindex_hi = vindex.hi;
    int32x4_t lo,hi;
    for (int i=0;i<4;i++)
    {
        lo[i] = *(int32_t *)((char *) base_addr + (vindex_lo[i]*scale));
        hi[i] = *(int32_t *)((char *) base_addr + (vindex_hi[i]*scale));
    }

    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}


AVX2NEON_ABI
__m256i _mm256_mask_i32gather_epi32 (__m256i src, int const* base_addr, __m256i vindex, __m256i mask, const int scale)
{
    uint32x4_t mask_lo = mask.lo;
    uint32x4_t mask_hi = mask.hi;
    int32x4_t vindex_lo = vindex.lo;
    int32x4_t vindex_hi = vindex.hi;
    int32x4_t lo,hi;
    lo = hi = _mm_setzero_si128();
    for (int i=0;i<4;i++)
    {
        if (mask_lo[i] >> 31) lo[i] = *(int32_t *)((char *) base_addr + (vindex_lo[i]*scale));
        if (mask_hi[i] >> 31) hi[i] = *(int32_t *)((char *) base_addr + (vindex_hi[i]*scale));
    }

    __m256i res;
    res.lo = lo; res.hi = hi;
    return res;
}