-
Notifications
You must be signed in to change notification settings - Fork 2
/
sse2msa.h
3764 lines (3265 loc) · 91.7 KB
/
sse2msa.h
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
#ifndef SSE2MSA_H
#define SSE2MSA_H
/*
* This header file provides a simple API translation layer between
* SSE intrinsics to their corresponding MIPS/MIPS64 MSA versions.
*
* This header file does not yet translate all of the SSE intrinsics.
*
* This project may only work with GCC since it has some GCC builtin functions.
*/
/*
* This project is a fork from sse2neon(https://github.com/DLTcollab/sse2neon).
* Contributors to this work are:
* John W. Ratcliff <[email protected]>
* Brandon Rowlett <[email protected]>
* Ken Fast <[email protected]>
* Eric van Beurden <[email protected]>
* Alexander Potylitsin <[email protected]>
* Hasindu Gamaarachchi <[email protected]>
* Jim Huang <[email protected]>
* Mark Cheng <[email protected]>
* Malcolm James MacLeod <[email protected]>
* Devin Hussey (easyaspi314) <[email protected]>
* Sebastian Pop <[email protected]>
* Developer Ecosystem Engineering <[email protected]>
* Danila Kutenin <[email protected]>
* François Turban (JishinMaster) <[email protected]>
* Pei-Hsuan Hung <[email protected]>
* Yang-Hao Yuan <[email protected]>
* Syoyo Fujita <[email protected]>
* Brecht Van Lommel <[email protected]>
* Evidence John <[email protected]>
*/
/*
* sse2msa is freely redistributable under the MIT License.
*
* Copyright (c) 2015-2021, The sse2neon project.
* Copyright (c) 2021, CIP United Co. Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if defined(__GNUC__)
#pragma push_macro("FORCE_INLINE")
#pragma push_macro("ALIGN_STRUCT")
#define FORCE_INLINE static inline __attribute__((always_inline))
#define ALIGN_STRUCT(x) __attribute__((aligned(x)))
#else
#error Unsupported compiler
#endif
#include <msa.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#define SSE2MSA_NO_IMPL 0
#define _MM_SHUFFLE(fp3, fp2, fp1, fp0) \
(((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
#define _MM_FROUND_TO_NEAREST_INT 0x00
#define _MM_FROUND_TO_NEG_INF 0x01
#define _MM_FROUND_TO_POS_INF 0x02
#define _MM_FROUND_TO_ZERO 0x03
#define _MM_FROUND_CUR_DIRECTION 0x04
#define _MM_FROUND_NO_EXC 0x08
#define _MM_ROUND_NEAREST 0x0000
#define _MM_ROUND_DOWN 0x2000
#define _MM_ROUND_UP 0x4000
#define _MM_ROUND_TOWARD_ZERO 0x6000
typedef int32_t v2i32 __attribute__((vector_size(8)));
/* Ref: mmintrin.h emmintrin.h */
typedef v2i32 __m64; /* int */
typedef v4f32 __m128; /* float */
typedef v2f64 __m128d; /* double */
typedef v2i64 __m128i; /* long long */
#if defined(__mips64)
typedef __int128 __i128_t;
typedef unsigned __int128 __u128_t;
#endif
#define v_msa_setzero(type) ((type)__builtin_msa_ldi_b(0))
/* To access the contents of a MSA register */
typedef union ALIGN_STRUCT(16) VREG128 {
/*---------------------------------------------------*/
__m64 m64[2];
__m128 m128; /* MMX, SSE types */
__m128d m128d;
__m128i m128i;
/*---------------------------------------------------*/
#if defined(__mips64)
__i128_t i128; /* GCC extensions */
__u128_t u128;
#endif
/*---------------------------------------------------*/
int8_t i8[16];
int16_t i16[8];
int32_t i32[4];
int64_t i64[2];
uint8_t u8[16]; /* C types */
uint16_t u16[8];
uint32_t u32[4];
uint64_t u64[2];
float f32[4];
double f64[2];
/*---------------------------------------------------*/
v16i8 msa_v16i8;
v16u8 msa_v16u8;
v8i16 msa_v8i16;
v8u16 msa_v8u16;
v4i32 msa_v4i32; /* MSA vector types */
v4u32 msa_v4u32;
v2i64 msa_v2i64;
v2u64 msa_v2u64;
v4f32 msa_v4f32;
v2f64 msa_v2f64;
/*---------------------------------------------------*/
} VREG128;
#define reinterpret_i32(x) \
__extension__(({union {int32_t i; typeof(x) v;} $ = {.v = x}; $.i;}))
#define reinterpret_i64(x) \
__extension__(({union {int64_t i; typeof(x) v;} $ = {.v = x}; $.i;}))
#define vreinterpret_m64(x) ((__m64)(x))
#define vreinterpret_m128(x) ((__m128)(x))
#define vreinterpret_m128d(x) ((__m128d)(x))
#define vreinterpret_m128i(x) ((__m128i)(x))
#define vreinterpret_v16i8(x) ((v16i8)(x))
#define vreinterpret_v8i16(x) ((v8i16)(x))
#define vreinterpret_v4i32(x) ((v4i32)(x))
#define vreinterpret_v2i64(x) ((v2i64)(x))
#define vreinterpret_v16u8(x) ((v16u8)(x))
#define vreinterpret_v8u16(x) ((v8u16)(x))
#define vreinterpret_v4u32(x) ((v4u32)(x))
#define vreinterpret_v2u64(x) ((v2u64)(x))
#define vreinterpret_v4f32(x) ((v4f32)(x))
#define vreinterpret_v2f64(x) ((v2f64)(x))
#define vreinterpret_nth_f32_m128(x, n) (((VREG128*)&x)->f32[n])
#define vreinterpret_nth_f64_m128d(x, n) (((VREG128*)&x)->f64[n])
#define vreinterpret_nth_i16_m128(x, n) (((VREG128*)&x)->i16[n])
#define vreinterpret_nth_i16_m128i(x, n) (((VREG128*)&x)->i16[n])
#define vpreinterpret_nth_i16_m128d(p, n) (((VREG128*)p)->i16[n])
#define vreinterpret_nth_u16_m128(x, n) (((VREG128*)&x)->u16[n])
#define vreinterpret_nth_u16_m128i(x, n) (((VREG128*)&x)->u16[n])
#define vpreinterpret_nth_u16_m128d(p, n) (((VREG128*)p)->u16[n])
#define vreinterpret_nth_i32_m128(x, n) (((VREG128*)&x)->i32[n])
#define vreinterpret_nth_i32_m128i(x, n) (((VREG128*)&x)->i32[n])
#define vpreinterpret_nth_i32_m128d(p, n) (((VREG128*)p)->i32[n])
#define vreinterpret_nth_i64_m128(x, n) (((VREG128*)&x)->i64[n])
#define vreinterpret_nth_i64_m128i(x, n) (((VREG128*)&x)->i64[n])
#define vpreinterpret_nth_i64_m128d(p, n) (((VREG128*)p)->i64[n])
#define vreinterpret_nth_u32_m128(x, n) (((VREG128*)&x)->u32[n])
#define vreinterpret_nth_u32_m128i(x, n) (((VREG128*)&x)->u32[n])
#define vpreinterpret_nth_u32_m128d(p, n) (((VREG128*)p)->u32[n])
#define vreinterpret_nth_u64_m128(x, n) (((VREG128*)&x)->u64[n])
#define vreinterpret_nth_u64_m128i(x, n) (((VREG128*)&x)->u64[n])
#define vpreinterpret_nth_u64_m128d(p, n) (((VREG128*)p)->u64[n])
FORCE_INLINE void _mm_prefetch(const void *p, int i)
{
(void) i;
__builtin_prefetch(p);
}
FORCE_INLINE __m128i _mm_setzero_si128(void)
{
return v_msa_setzero(__m128i);
}
FORCE_INLINE __m128 _mm_setzero_ps(void)
{
return v_msa_setzero(__m128);
}
FORCE_INLINE __m128d _mm_setzero_pd(void)
{
return v_msa_setzero(__m128d);
}
FORCE_INLINE __m128 _mm_set1_ps(float a)
{
return vreinterpret_m128(__builtin_msa_fill_w(reinterpret_i32(a)));
}
FORCE_INLINE __m128 _mm_set_ps1(float a)
{
return vreinterpret_m128(__builtin_msa_fill_w(reinterpret_i32(a)));
}
FORCE_INLINE __m128 _mm_set_ps(float e3, float e2, float e1, float e0)
{
VREG128 v = {
.f32 = {e0, e1, e2, e3}
};
return v.m128;
}
FORCE_INLINE __m128 _mm_set_ss(float a)
{
VREG128 v = {
.f32 = {a, 0, 0, 0}
};
return v.m128;
}
FORCE_INLINE __m128 _mm_setr_ps(float e3, float e2, float e1, float e0)
{
VREG128 v = {
.f32 = {e3, e2, e1, e0}
};
return v.m128;
}
FORCE_INLINE __m128d _mm_setr_pd(double e1, double e0)
{
VREG128 v = {
.f64 = {e1, e0}
};
return v.m128d;
}
FORCE_INLINE __m128i _mm_setr_epi16(
short e7, short e6, short e5, short e4,
short e3, short e2, short e1, short e0)
{
VREG128 v = {
.i16 = {e7, e6, e5, e4, e3, e2, e1, e0}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_setr_epi32(int e3, int e2, int e1, int e0)
{
VREG128 v = {
.i32 = {e3, e2, e1, e0}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_setr_epi64(__m64 e1, __m64 e0)
{
VREG128 v = {
.m64 = {e1, e0}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set1_epi8(char a)
{
VREG128 v = {
.msa_v16i8 = __builtin_msa_fill_b(a)
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set1_epi16(short a)
{
VREG128 v = {
.msa_v8i16 = __builtin_msa_fill_h(a)
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set_epi8(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)
{
VREG128 v = {
.i8 = {e0, e1, e2, e3, e4, e5,
e6, e7, e8, e9, e10, e11, e12, e13, e14, e15}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set_epi16(short e7, short e6, short e5,
short e4, short e3, short e2, short e1, short e0)
{
VREG128 v = {
.i16 = {e0, e1, e2, e3, e4, e5, e6, e7}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_setr_epi8(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)
{
VREG128 v = {
.i8 = {e15, e14, e13, e12, e11, e10,
e9, e8, e7, e6, e5, e4, e3, e2, e1, e0}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set1_epi32(int a)
{
VREG128 v = {
.msa_v4i32 = __builtin_msa_fill_w(a)
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set1_epi64(__m64 a)
{
VREG128 v = {
.m64 = {a, a}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set1_epi64x(int64_t a)
{
VREG128 v = {
.msa_v2i64 = __builtin_msa_fill_d(a)
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set_epi32(int e3, int e2, int e1, int e0)
{
VREG128 v = {
.msa_v4i32 = {e0, e1, e2, e3}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set_epi64x(int64_t e1, int64_t e0)
{
VREG128 v = {
.msa_v2i64 = {e0, e1}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_set_epi64(__m64 e1, __m64 e0)
{
VREG128 v = {
.m64 = {e0, e1}
};
return v.m128i;
}
FORCE_INLINE __m128d _mm_set_pd(double e1, double e0)
{
VREG128 v = {
.f64 = {e0, e1}
};
return v.m128d;
}
FORCE_INLINE __m128d _mm_set_sd(double a)
{
VREG128 v = {
.f64 = {a, 0}
};
return v.m128d;
}
FORCE_INLINE __m128d _mm_set1_pd(double a)
{
return vreinterpret_m128d(
__builtin_msa_fill_d(reinterpret_i64(a)));
}
#define _mm_set_pd1 _mm_set1_pd
FORCE_INLINE void _mm_store_ps(float *p, __m128 a)
{
__builtin_msa_st_w(vreinterpret_v4i32(a), p, 0);
}
FORCE_INLINE void _mm_storer_ps(float *p, __m128 a)
{
__builtin_msa_st_w(__builtin_msa_shf_w(
vreinterpret_v4i32(a), 0x1b), p, 0);
}
FORCE_INLINE void _mm_storer_pd(double *p, __m128d a)
{
p[0] = vreinterpret_nth_f64_m128d(a, 1);
p[1] = vreinterpret_nth_f64_m128d(a, 0);
}
FORCE_INLINE void _mm_store_ps1(float *p, __m128 a)
{
v4i32 v = __builtin_msa_fill_w(vreinterpret_nth_i32_m128(a, 0));
__builtin_msa_st_w(v, p, 0);
}
#define _mm_store1_ps _mm_store_ps1
FORCE_INLINE void _mm_storeu_ps(float *p, __m128 a)
{
__builtin_msa_st_w(vreinterpret_v4i32(a), p, 0);
}
FORCE_INLINE void _mm_store_si128(__m128i *p, __m128i a)
{
__builtin_msa_st_d(vreinterpret_v2i64(a), p, 0);
}
FORCE_INLINE void _mm_storeu_si128(__m128i *p, __m128i a)
{
__builtin_msa_st_d(vreinterpret_v2i64(a), p, 0);
}
FORCE_INLINE void _mm_store_ss(float *p, __m128 a)
{
*p = vreinterpret_nth_f32_m128(a, 0);
}
FORCE_INLINE void _mm_store_pd(double *p, __m128d a)
{
__builtin_msa_st_d(vreinterpret_v2i64(a), p, 0);
}
FORCE_INLINE void _mm_store_pd1(double *p, __m128d a)
{
p[0] = vreinterpret_nth_f64_m128d(a, 0);
p[1] = vreinterpret_nth_f64_m128d(a, 0);
}
#define _mm_store1_pd _mm_store_pd1
FORCE_INLINE void _mm_store_sd(double *p, __m128d a)
{
p[0] = vreinterpret_nth_f64_m128d(a, 0);
}
FORCE_INLINE void _mm_storeh_pd(double *p, __m128d a)
{
p[0] = vreinterpret_nth_f64_m128d(a, 1);
}
FORCE_INLINE void _mm_storel_pd(double *p, __m128d a)
{
p[0] = vreinterpret_nth_f64_m128d(a, 0);
}
FORCE_INLINE void _mm_storeu_pd(double *p, __m128d a)
{
__builtin_msa_st_d(vreinterpret_v2i64(a), p, 0);
}
FORCE_INLINE void _mm_storeu_si16(void *p, __m128i a)
{
*((int16_t*)p) = vreinterpret_nth_i16_m128(a, 0);
}
FORCE_INLINE void _mm_storeu_si32(void *p, __m128i a)
{
*((int32_t*)p) = vreinterpret_nth_i32_m128(a, 0);
}
FORCE_INLINE void _mm_storeu_si64(void *p, __m128i a)
{
*((int64_t*)p) = vreinterpret_nth_i64_m128(a, 0);
}
FORCE_INLINE void _mm_storel_epi64(__m128i *p, __m128i a)
{
*((int64_t*)p) = vreinterpret_nth_i64_m128(a, 0);
}
FORCE_INLINE void _mm_storel_pi(__m64 *p, __m128 a)
{
*(((float*)p) + 0) = vreinterpret_nth_f32_m128(a, 0);
*(((float*)p) + 1) = vreinterpret_nth_f32_m128(a, 1);
}
FORCE_INLINE void _mm_storeh_pi(__m64 *p, __m128 a)
{
*(((float*)p) + 0) = vreinterpret_nth_f32_m128(a, 2);
*(((float*)p) + 1) = vreinterpret_nth_f32_m128(a, 3);
}
FORCE_INLINE __m128i _mm_stream_load_si128(__m128i *p)
{
return vreinterpret_m128i(__builtin_msa_ld_d(p, 0));
}
FORCE_INLINE void _mm_stream_pd(double *p, __m128d a)
{
__builtin_msa_st_d(vreinterpret_v2i64(a), p, 0);
}
FORCE_INLINE void _mm_stream_pi(__m64 *p, __m64 a) { *p = a; }
FORCE_INLINE void _mm_stream_ps(float *p, __m128 a)
{
__builtin_msa_st_w(vreinterpret_v4i32(a), p, 0);
}
FORCE_INLINE void _mm_stream_si128(__m128i *p, __m128i a)
{
__builtin_msa_st_d(vreinterpret_v2i64(a), p, 0);
}
FORCE_INLINE void _mm_stream_si32(int *p, int a) { *p = a; }
FORCE_INLINE void _mm_stream_si64(int64_t *p, int64_t a) { *p = a; }
FORCE_INLINE __m128 _mm_load1_ps(const float *p)
{
return vreinterpret_m128(__builtin_msa_fill_w(reinterpret_i32(*p)));
}
#define _mm_load_ps1 _mm_load1_ps
FORCE_INLINE __m128d _mm_load1_pd(const double *p)
{
return vreinterpret_m128d(__builtin_msa_fill_d(reinterpret_i64(*p)));
}
#define _mm_load_pd1 _mm_load1_pd
FORCE_INLINE __m128 _mm_loadl_pi(__m128 a, __m64 const *p)
{
VREG128 v = {.m128 = a};
v.m64[0] = *p;
return v.m128;
}
FORCE_INLINE __m128 _mm_loadh_pi(__m128 a, __m64 const *p)
{
VREG128 v = {.m128 = a};
v.m64[1] = *p;
return v.m128;
}
FORCE_INLINE __m128 _mm_load_ps(const float *p)
{
return vreinterpret_m128(__builtin_msa_ld_w(p, 0));
}
FORCE_INLINE __m128 _mm_loadr_ps(float const* p)
{
return vreinterpret_m128(__builtin_msa_shf_w(
__builtin_msa_ld_w(p, 0), 0x1b));
}
FORCE_INLINE __m128d _mm_loadr_pd(const double *p)
{
VREG128 v = {
.f64 = {p[1], p[0]}
};
return v.m128d;
}
FORCE_INLINE __m128 _mm_loadu_ps(const float *p)
{
return vreinterpret_m128(__builtin_msa_ld_w(p, 0));
}
FORCE_INLINE __m128d _mm_load_pd(const double *p)
{
return vreinterpret_m128d(__builtin_msa_ld_d(p, 0));
}
FORCE_INLINE __m128d _mm_loadu_pd(const double *p)
{
return vreinterpret_m128d(__builtin_msa_ld_d(p, 0));
}
FORCE_INLINE __m128d _mm_loadh_pd(__m128d a, const double *p)
{
VREG128 v = {.m128d = a};
v.f64[1] = *p;
return v.m128d;
}
FORCE_INLINE __m128d _mm_loadl_pd(__m128d a, const double *p)
{
VREG128 v = {.m128d = a};
v.f64[0] = *p;
return v.m128d;
}
FORCE_INLINE __m128d _mm_loaddup_pd(const double *p)
{
VREG128 v = {.f64 = {*p, *p}};
return v.m128d;
}
FORCE_INLINE __m128 _mm_load_ss(const float *p)
{
VREG128 v = {
.f32 = {*p, 0, 0, 0}
};
return v.m128;
}
FORCE_INLINE __m128d _mm_load_sd(double const *p)
{
VREG128 v = {
.f64 = {*p, 0}
};
return v.m128d;
}
FORCE_INLINE __m128i _mm_loadu_si64(const void *p)
{
VREG128 v = {
.i64 = {*(int64_t*)p, 0}
};
return v.m128i;
}
FORCE_INLINE __m128i _mm_loadl_epi64(__m128i const *p)
{
VREG128 v = {
.i64 = {vpreinterpret_nth_i64_m128d(p, 0), 0}
};
return v.m128i;
}
FORCE_INLINE void *_mm_malloc(size_t size, size_t align)
{
void *ptr;
if (align == 1)
return malloc(size);
if (align == 2 || (sizeof(void *) == 8 && align == 4))
align = sizeof(void *);
if (!posix_memalign(&ptr, align, size))
return ptr;
return NULL;
}
FORCE_INLINE void _mm_free(void *addr)
{
free(addr);
}
FORCE_INLINE __m128 _mm_move_ss(__m128 a, __m128 b)
{
VREG128 v = {.m128 = a};
v.f32[0] = vreinterpret_nth_f32_m128(b, 0);
return v.m128;
}
FORCE_INLINE __m128d _mm_move_sd(__m128d a, __m128d b)
{
VREG128 v = {.m128d = a};
v.f64[0] = vreinterpret_nth_f64_m128d(b, 0);
return v.m128d;
}
FORCE_INLINE __m128i _mm_move_epi64(__m128i a)
{
VREG128 v = {
.i64 = {vreinterpret_nth_i64_m128i(a, 0), 0}
};
return v.m128i;
}
FORCE_INLINE __m128 _mm_undefined_ps(void)
{
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
#endif
__m128 a;
return a;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
FORCE_INLINE __m128d _mm_undefined_pd(void)
{
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
#endif
__m128d a;
return a;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
FORCE_INLINE __m128i _mm_undefined_si128(void)
{
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
#endif
__m128i a;
return a;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
FORCE_INLINE __m128 _mm_andnot_ps(__m128 a, __m128 b)
{
return vreinterpret_m128(
__builtin_msa_and_v((__builtin_msa_nor_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(a))),
vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128d _mm_andnot_pd(__m128d a, __m128d b)
{
return vreinterpret_m128d(
__builtin_msa_and_v((__builtin_msa_nor_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(a))),
vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128i _mm_andnot_si128(__m128i a, __m128i b)
{
return vreinterpret_m128i(
__builtin_msa_and_v((__builtin_msa_nor_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(a))),
vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128i _mm_and_si128(__m128i a, __m128i b)
{
return vreinterpret_m128i(__builtin_msa_and_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128 _mm_and_ps(__m128 a, __m128 b)
{
return vreinterpret_m128(__builtin_msa_and_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128d _mm_and_pd(__m128d a, __m128d b)
{
return vreinterpret_m128d(__builtin_msa_and_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128 _mm_or_ps(__m128 a, __m128 b)
{
return vreinterpret_m128(__builtin_msa_or_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128d _mm_or_pd(__m128d a, __m128d b)
{
return vreinterpret_m128d(__builtin_msa_or_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128 _mm_xor_ps(__m128 a, __m128 b)
{
return vreinterpret_m128(__builtin_msa_xor_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128d _mm_xor_pd(__m128d a, __m128d b)
{
return vreinterpret_m128d(__builtin_msa_xor_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128i _mm_or_si128(__m128i a, __m128i b)
{
return vreinterpret_m128i(__builtin_msa_or_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128i _mm_xor_si128(__m128i a, __m128i b)
{
return vreinterpret_m128i(__builtin_msa_xor_v(
vreinterpret_v16u8(a), vreinterpret_v16u8(b)));
}
FORCE_INLINE __m128d _mm_movedup_pd(__m128d a)
{
VREG128 v = {
.f64 = {
vreinterpret_nth_f64_m128d(a, 0),
vreinterpret_nth_f64_m128d(a, 0),
}
};
return v.m128d;
}
FORCE_INLINE __m128 _mm_movehdup_ps(__m128 a)
{
return vreinterpret_m128(__builtin_msa_shf_w(
vreinterpret_v4i32(a), 0xf5));
}
FORCE_INLINE __m128 _mm_moveldup_ps(__m128 a)
{
return vreinterpret_m128(__builtin_msa_shf_w(
vreinterpret_v4i32(a), 0xa0));
}
FORCE_INLINE __m128 _mm_movehl_ps(__m128 a, __m128 b)
{
return vreinterpret_m128(__builtin_msa_vshf_w(
({v4i32 mask = {6, 7, 2, 3}; mask;}),
vreinterpret_v4i32(b), vreinterpret_v4i32(a)));
}
FORCE_INLINE __m128 _mm_movelh_ps(__m128 a, __m128 b)
{
return vreinterpret_m128(__builtin_msa_vshf_w(
({v4i32 mask = {0, 1, 4, 5}; mask;}),
vreinterpret_v4i32(b), vreinterpret_v4i32(a)));
}
FORCE_INLINE __m128i _mm_abs_epi32(__m128i a)
{
__m128i v = _mm_setzero_si128();
return vreinterpret_m128i(__builtin_msa_add_a_w(
vreinterpret_v4i32(v), vreinterpret_v4i32(a)));
}
FORCE_INLINE __m128i _mm_abs_epi16(__m128i a)
{
__m128i v = _mm_setzero_si128();
return vreinterpret_m128i(__builtin_msa_add_a_h(
vreinterpret_v8i16(v), vreinterpret_v8i16(a)));
}
FORCE_INLINE __m128i _mm_abs_epi8(__m128i a)
{
__m128i v = _mm_setzero_si128();
return vreinterpret_m128i(__builtin_msa_add_a_b(
vreinterpret_v16i8(v), vreinterpret_v16i8(a)));
}
FORCE_INLINE __m64 _mm_abs_pi32(__m64 a)
{
VREG128 v = {.m64 = {a, {0}}};
v.m128i = _mm_abs_epi32(v.m128i);
return v.m64[0];
}
FORCE_INLINE __m64 _mm_abs_pi16(__m64 a)
{
VREG128 v = {.m64 = {a, {0}}};
v.m128i = _mm_abs_epi16(v.m128i);
return v.m64[0];
}
FORCE_INLINE __m64 _mm_abs_pi8(__m64 a)
{
VREG128 v = {.m64 = {a, {0}}};
v.m128i = _mm_abs_epi8(v.m128i);
return v.m64[0];
}
FORCE_INLINE __m128i _mm_sad_epu8(__m128i a, __m128i b)
{
VREG128 v = {
.msa_v16u8 = __builtin_msa_asub_u_b(
vreinterpret_v16u8(a), vreinterpret_v16u8(b))
};
v.msa_v8u16 = __builtin_msa_hadd_u_h(v.msa_v16u8, v.msa_v16u8);
v.msa_v4u32 = __builtin_msa_hadd_u_w(v.msa_v8u16, v.msa_v8u16);
v.msa_v2u64 = __builtin_msa_hadd_u_d(v.msa_v4u32, v.msa_v4u32);
return v.m128i;
}
FORCE_INLINE __m64 _mm_sad_pu8(__m64 a, __m64 b)
{
VREG128 va = {.m64 = {a, {0}}};
VREG128 vb = {.m64 = {b, {0}}};
VREG128 v = {
.m128i = _mm_sad_epu8(va.m128i, vb.m128i)
};
return v.m64[0];
}
#define _m_psadbw(a, b) _mm_sad_pu8(a, b)
FORCE_INLINE __m128 _mm_shuffle_ps(__m128 a, __m128 b, int imm8)
{
return vreinterpret_m128(__builtin_msa_vshf_w(
({v4i32 mask = {imm8 & 0x3, (imm8 >> 2) & 0x3,
((imm8 >> 4) & 0x3) + 4, ((imm8 >> 6) & 0x3) + 4}; mask;}),
vreinterpret_v4i32(b), vreinterpret_v4i32(a)));
}
FORCE_INLINE __m128i _mm_shuffle_epi32(__m128i a, int imm8)
{
return vreinterpret_m128i(__builtin_msa_vshf_w(
({v4i32 mask = {imm8 & 0x3, (imm8 >> 2) & 0x3,
((imm8 >> 4) & 0x3) + 4, ((imm8 >> 6) & 0x3) + 4}; mask;}),
vreinterpret_v4i32(a), vreinterpret_v4i32(a)));
}
FORCE_INLINE __m128i _mm_shuffle_epi8(__m128i a, __m128i b)
{
v16i8 mask = vreinterpret_v16i8((vreinterpret_v16u8(b) << 4) >> 4);
mask = mask | (vreinterpret_v16i8(b) & __builtin_msa_fill_b(0x80));
return vreinterpret_m128i(
__builtin_msa_vshf_b(mask,
vreinterpret_v16i8(a), vreinterpret_v16i8(a)
));
}
FORCE_INLINE __m64 _mm_shuffle_pi8(__m64 a, __m64 b)
{
VREG128 va = {.m64 = {a, {0}}};
VREG128 vb = {.m64 = {b, {0}}};
v16i8 mask = vreinterpret_v16i8((vb.msa_v16u8 << 5) >> 5);
mask = mask | (vb.msa_v16i8 & __builtin_msa_fill_b(0x80));
VREG128 v = {
.msa_v16i8 = __builtin_msa_vshf_b(
mask, va.msa_v16i8, va.msa_v16i8)
};
return v.m64[0];
}
FORCE_INLINE __m64 _mm_shuffle_pi16(__m64 a, int imm8)
{
VREG128 v = {.m64 = {a, a}};
v.msa_v8i16 = __builtin_msa_vshf_h(
({v8i16 mask = {(imm8) & (0x3), ((imm8) >> 2) & 0x3,
(((imm8) >> 4) & 0x3) + 4, (((imm8) >> 6) & 0x3) + 4,
0, 0, 0, 0}; mask;}), v.msa_v8i16, v.msa_v8i16);
return v.m64[0];
}
#define _m_pshufw(a, imm) _mm_shuffle_pi16(a, imm)
FORCE_INLINE __m128i _mm_shufflehi_epi16(__m128i a, int imm8)
{
v8i16 mask = {
0, 1, 2, 3,
((imm8 ) & 0x3) + 4,
((imm8 >> 2) & 0x3) + 4,
((imm8 >> 4) & 0x3) + 4,
((imm8 >> 6) & 0x3) + 4
};
return vreinterpret_m128i(__builtin_msa_vshf_h(mask,
vreinterpret_v8i16(a), vreinterpret_v8i16(a)));
}
FORCE_INLINE __m128i _mm_shufflelo_epi16(__m128i a, int imm8)
{
v8i16 mask = {
(imm8 ) & 0x3,
(imm8 >> 2) & 0x3,
(imm8 >> 4) & 0x3,
(imm8 >> 6) & 0x3,
4, 5, 6, 7
};
return vreinterpret_m128i(__builtin_msa_vshf_h(mask,
vreinterpret_v8i16(a), vreinterpret_v8i16(a)));
}
FORCE_INLINE __m128d _mm_shuffle_pd(__m128d a, __m128d b, int imm8)
{
imm8 = imm8 & 3;
switch (imm8) {
case 0:
return vreinterpret_m128d(
__builtin_msa_vshf_d(({v2i64 mask = {0, 2}; mask;}),
vreinterpret_v2i64(b), vreinterpret_v2i64(a)));
case 1:
return vreinterpret_m128d(
__builtin_msa_vshf_d(({v2i64 mask = {1, 2}; mask;}),
vreinterpret_v2i64(b), vreinterpret_v2i64(a)));
case 2: