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
|
#ifndef _SIMD_H_
#define _SIMD_H_
#include "simd_common.h"
#pragma warning(push)
#pragma warning(disable:4799)
#pragma warning(disable:4309)
#pragma warning(disable:4700)
#define MMX_INSTRUCTION(instruction,function) \
static __forceinline void instruction(__m64 &dst,const __m64 &src) {dst=function(dst, src);} \
static __forceinline void instruction(__m64 &dst,const void *src) {dst=function(dst,*(__m64*) src);} \
static __forceinline void instruction(__m64 &dst,const __int64 &src) {dst=function(dst,*(__m64*)&src);}
#define SSE2I_INSTRUCTION(instruction,function) \
static __forceinline void instruction(__m128i &dst,const __m128i &src) {dst=function(dst, src);} \
static __forceinline void instruction(__m128i &dst,const void *src) {dst=function(dst,*(__m128i*)src);}
#include "simd_instructions.h"
#undef MMX_INSTRUCTION
#undef SSE2I_INSTRUCTION
static __forceinline void movq(__m64 &dst, const __m64 &src)
{
dst = src;
}
static __forceinline void movq(__m64 &dst, const void *src)
{
dst = *(__m64*)src;
}
static __forceinline void movq(__m64 &dst, const __int64 &src)
{
dst = *(__m64*)&src;
}
static __forceinline void movq(void *dst, const __m64 &src)
{
*(__m64*)dst = src;
}
static __forceinline void movntq(void *dst, const __m64 &src)
{
_mm_stream_pi((__m64*)dst, src);
}
static __forceinline void movdqu(__m64 &dst, const void *src)
{
dst = *(__m64*)src;
}
static __forceinline void movd(__m64 &dst, int src)
{
dst = _mm_cvtsi32_si64(src);
}
static __forceinline void movd(int &dst, const __m64 &src)
{
dst = _mm_cvtsi64_si32(src);
}
static __forceinline void movd(__m64 &dst, const void *src)
{
dst = _mm_cvtsi32_si64(*(const int*)src);
}
static __forceinline void movd(void *dst, const __m64 &src)
{
*(int*)dst = _mm_cvtsi64_si32(src);
}
static __forceinline void psllq(__m64 &dst, int i)
{
dst = _mm_slli_si64(dst, i);
}
static __forceinline void pslld(__m64 &dst, int i)
{
dst = _mm_slli_pi32(dst, i);
}
static __forceinline void psllw(__m64 &dst, int i)
{
dst = _mm_slli_pi16(dst, i);
}
static __forceinline void psrlq(__m64 &dst, int i)
{
dst = _mm_srli_si64(dst, i);
}
static __forceinline void psrld(__m64 &dst, int i)
{
dst = _mm_srli_pi32(dst, i);
}
static __forceinline void psrlw(__m64 &dst, int i)
{
dst = _mm_srli_pi16(dst, i);
}
static __forceinline void psraw(__m64 &dst, int i)
{
dst = _mm_srai_pi16(dst, i);
}
static __forceinline void psraw(__m64 &dst, const __m64 &src)
{
dst = _mm_sra_pi16(dst, src);
}
static __forceinline void psrad(__m64 &dst, int i)
{
dst = _mm_srai_pi32(dst, i);
}
static __forceinline void prefetcht0(const void *a)
{
_mm_prefetch((char*)a, _MM_HINT_T0);
}
static __forceinline void movaps(__m128 &dst, const __m128 &src)
{
dst = src;
}
static __forceinline void movaps(void *dst, const __m128 &src)
{
*(__m128*)dst = src;
}
static __forceinline void movups(__m128 &dst, const void *src)
{
dst = _mm_loadu_ps((float*)src);
}
static __forceinline void movups(void *dst, const __m128 &src)
{
_mm_storeu_ps((float*)dst, src);
}
static __forceinline void movss(__m128 &dst, const void *src)
{
dst = _mm_load_ss((float*)src);
}
static __forceinline void movss(void *dst, const __m128 &src)
{
_mm_store_ss((float*)dst, src);
}
static __forceinline void movhlps(__m128 &dst, const __m128 &src)
{
dst = _mm_movehl_ps(dst, src);
}
static __forceinline void movlhps(__m128 &dst, const __m128 &src)
{
dst = _mm_movelh_ps(dst, src);
}
static __forceinline void movlps(__m128 &dst, const void *src)
{
dst = _mm_loadl_pi(dst, (const __m64*)src);
}
static __forceinline void movlps(void *dst, const __m128 &src)
{
_mm_storel_pi((__m64*)dst, src);
}
static __forceinline void movhps(__m128 &dst, const void *src)
{
dst = _mm_loadh_pi(dst, (const __m64*)src);
}
static __forceinline void movhps(void *dst, const __m128 &src)
{
_mm_storeh_pi((__m64*)dst, src);
}
static __forceinline void xorps(__m128 &dst, const __m128 &src)
{
dst = _mm_xor_ps(dst, src);
}
static __forceinline void addps(__m128 &dst, const __m128 &src)
{
dst = _mm_add_ps(dst, src);
}
static __forceinline void addss(__m128 &dst, const __m128 &src)
{
dst = _mm_add_ss(dst, src);
}
static __forceinline void mulps(__m128 &dst, const __m128 &src)
{
dst = _mm_mul_ps(dst, src);
}
static __forceinline void mulss(__m128 &dst, const __m128 &src)
{
dst = _mm_mul_ss(dst, src);
}
static __forceinline void minps(__m128 &dst, const __m128 &src)
{
dst = _mm_min_ps(dst, src);
}
static __forceinline void cvtps2pi(__m64 &dst, const __m128 &src)
{
dst = _mm_cvtps_pi32(src);
}
static __forceinline void cmpnltps(__m128 &dst, const __m128 &src)
{
dst = _mm_cmpnlt_ps(dst, src);
}
static __forceinline void cvtpi2ps(__m128 &dst, const __m64 &src)
{
dst = _mm_cvtpi32_ps(dst, src);
}
static __forceinline void movq(__m128i &dst, const __m128i &src)
{
dst = src;
}
static __forceinline void movq(__m128i &dst, const void *src)
{
dst = *(__m128i*)src;
}
static __forceinline void movq(const void *dst, __m128i &src)
{
*(__m128i*)dst = src;
}
static __forceinline void movd(__m128i &dst, const void *src)
{
dst = _mm_loadl_epi64((__m128i*)src);
}
static __forceinline void movd(void *dst, const __m128i &src)
{
_mm_storel_epi64((__m128i*)dst, src);
}
static __forceinline void movdqu(__m128i &dst, const void *src)
{
dst = _mm_loadu_si128((__m128i*)src);
}
static __forceinline void movdqu(__m128i &dst, const __m128i &src)
{
dst = _mm_loadu_si128(&src);
}
static __forceinline void movdqa(__m128i &dst, const __m128i &src)
{
dst = src;
}
static __forceinline void movdqa(__m128i &dst, const void * src)
{
dst = _mm_load_si128((__m128i*)src);
}
static __forceinline void movdqa(void *dst, const __m128i &src)
{
_mm_store_si128((__m128i*)dst, src);
}
static __forceinline void movntdq(void *dst, const __m128i &src)
{
_mm_stream_si128((__m128i*)dst, src);
}
static __forceinline void movdq2q(__m64 &dst, const __m128i &src)
{
dst = _mm_movepi64_pi64(src);
}
static __forceinline void psrlw(__m128i &dst, int i)
{
dst = _mm_srli_epi16(dst, i);
}
static __forceinline void psrlq(__m128i &dst, int i)
{
dst = _mm_srli_epi64(dst, i);
}
static __forceinline void psrad(__m128i &dst, int i)
{
dst = _mm_srai_epi32(dst, i);
}
static __forceinline void psraw(__m128i &dst, int i)
{
dst = _mm_srai_epi16(dst, i);
}
static __forceinline void psraw(__m128i &dst, const __m128i &src)
{
dst = _mm_sra_epi16(dst, src);
}
static __forceinline void psllw(__m128i &dst, int i)
{
dst = _mm_slli_epi16(dst, i);
}
static __forceinline void pslld(__m128i &dst, int i)
{
dst = _mm_slli_epi32(dst, i);
}
static __forceinline void psllq(__m128i &dst, int i)
{
dst = _mm_slli_epi64(dst, i);
}
//static __forceinline void pshufd(__m128i &dst,const __m128i &src,const int i) {dst=_mm_shuffle_epi32(src,i);}
//static __forceinline void pshuflw(__m128i &dst,const __m128i &src,const int i) {dst=_mm_shufflelo_epi16(src,i);}
//static __forceinline void pshufhw(__m128i &dst,const __m128i &src,const int i) {dst=_mm_shufflehi_epi16(src,i);}
static __forceinline void cvtps2dq(__m128i &dst, const __m128 &src)
{
dst = _mm_cvtps_epi32(src);
}
static __forceinline void cvtdq2ps(__m128 &dst, const __m128i &src)
{
dst = _mm_cvtepi32_ps(src);
}
static __forceinline void movlpd(__m128d &dst, const void *src)
{
dst = _mm_loadl_pd(dst, (double*)src);
}
static __forceinline void movhpd(__m128d &dst, const void *src)
{
dst = _mm_loadh_pd(dst, (double*)src);
}
static __forceinline void movlpd(void *dst, const __m128d &src)
{
_mm_storel_pd((double*)dst, src);
}
static __forceinline void movhpd(void *dst, const __m128d &src)
{
_mm_storeh_pd((double*)dst, src);
}
#if defined(__INTEL_COMPILER) || (defined(__GNUC__) && __GNUC__>=4)
static __forceinline void movlpd(__m128i &dst, const void *src)
{
dst = _mm_castpd_si128(_mm_loadl_pd(_mm_castsi128_pd(dst), (double*)src));
}
static __forceinline void movhpd(__m128i &dst, const void *src)
{
dst = _mm_castpd_si128(_mm_loadh_pd(_mm_castsi128_pd(dst), (double*)src));
}
static __forceinline void movlpd(void *dst, const __m128i &src)
{
_mm_storel_pd((double*)dst, _mm_castsi128_pd(src));
}
static __forceinline void movhpd(void *dst, const __m128i &src)
{
_mm_storeh_pd((double*)dst, _mm_castsi128_pd(src));
}
static __forceinline void movlps(__m128i &dst, const void *src)
{
dst = _mm_castps_si128(_mm_loadl_pi(_mm_castsi128_ps(dst), (const __m64*)src));
}
static __forceinline void movlps(void *dst, const __m128i &src)
{
_mm_storel_pi((__m64*)dst, _mm_castsi128_ps(src));
}
static __forceinline void movhps(__m128i &dst, const void *src)
{
dst = _mm_castps_si128(_mm_loadh_pi(_mm_castsi128_ps(dst), (const __m64*)src));
}
static __forceinline void movhps(void *dst, const __m128i &src)
{
_mm_storeh_pi((__m64*)dst, _mm_castsi128_ps(src));
}
static __forceinline void movlhps(__m128i &dst, const __m128i &src)
{
dst = _mm_castps_si128(_mm_movelh_ps(_mm_castsi128_ps(dst), _mm_castsi128_ps(src)));
}
#else
static __forceinline __m128i _mm_castps_si128(__m128 &src)
{
return (__m128i&)src;
}
static __forceinline void movlpd(__m128i &dst, const void *src)
{
(__m128d&)dst = _mm_loadl_pd((__m128d&)dst, (double*)src);
}
static __forceinline void movhpd(__m128i &dst, const void *src)
{
(__m128d&)dst = _mm_loadh_pd((__m128d&)dst, (double*)src);
}
static __forceinline void movlpd(void *dst, const __m128i &src)
{
_mm_storel_pd((double*)dst, (const __m128d&)src);
}
static __forceinline void movhpd(void *dst, const __m128i &src)
{
_mm_storeh_pd((double*)dst, (const __m128d&)src);
}
static __forceinline void movlps(__m128i &dst, const void *src)
{
(__m128&)dst = _mm_loadl_pi((__m128&)dst, (const __m64*)src);
}
static __forceinline void movlps(void *dst, const __m128i &src)
{
_mm_storel_pi((__m64*)dst, (const __m128&)src);
}
static __forceinline void movhps(__m128i &dst, const void *src)
{
(__m128&)dst = _mm_loadh_pi((__m128&)dst, (const __m64*)src);
}
static __forceinline void movhps(void *dst, const __m128i &src)
{
_mm_storeh_pi((__m64*)dst, (const __m128&)src);
}
static __forceinline void movlhps(__m128i &dst, const __m128i &src)
{
(__m128&)dst = _mm_movelh_ps((__m128&)dst, (const __m128&)src);
}
#endif
//======================================= MMX ======================================
#define MMX_INSTRUCTIONS \
static __forceinline __m setzero_si64(void) {return _mm_setzero_si64();} \
static __forceinline __m set_pi8(char b7,char b6,char b5,char b4,char b3,char b2,char b1,char b0) {return _mm_set_pi8(b7,b6,b5,b4,b3,b2,b1,b0);} \
static __forceinline __m set_pi32(int i1,int i0) {return _mm_set_pi32(i1,i0);} \
static __forceinline __m set1_pi8(char b) {return _mm_set1_pi8(b);} \
static __forceinline __m set1_pi16(short s) {return _mm_set1_pi16(s);} \
static __forceinline __m set1_pi64(int64_t s) {return *(__m64*)&s;} \
static __forceinline __m packs_pu16(const __m &m1,const __m &m2) {return _mm_packs_pu16(m1,m2);} \
static __forceinline __m slli_pi16(const __m &m,int count) {return _mm_slli_pi16(m,count);} \
static __forceinline __m srli_pi16(const __m &m,int count) {return _mm_srli_pi16(m,count);} \
static __forceinline __m srli_si64(const __m &m,int count) {return _mm_srli_si64(m,count);} \
static __forceinline __m srai_pi16(const __m &m,int count) {return _mm_srai_pi16(m,count);} \
static __forceinline __m madd_pi16(const __m &m1,const __m &m2) {return _mm_madd_pi16(m1,m2);} \
static __forceinline __m add_pi16(const __m &m1,const __m &m2) {return _mm_add_pi16(m1,m2);} \
static __forceinline __m adds_pi16(const __m &m1,const __m &m2) {return _mm_adds_pi16(m1,m2);} \
static __forceinline __m adds_pu16(const __m &m1,const __m &m2) {return _mm_adds_pu16(m1,m2);} \
static __forceinline __m adds_pu8(const __m &m1,const __m &m2) {return _mm_adds_pu8(m1,m2);} \
static __forceinline __m sub_pi16(const __m &m1,const __m &m2) {return _mm_sub_pi16(m1,m2);} \
static __forceinline __m subs_pi16(const __m &m1,const __m &m2) {return _mm_subs_pi16(m1,m2);} \
static __forceinline __m subs_pu16(const __m &m1,const __m &m2) {return _mm_subs_pu16(m1,m2);} \
static __forceinline __m subs_pu8(const __m &m1,const __m &m2) {return _mm_subs_pu8(m1,m2);} \
static __forceinline __m or_si64(const __m &m1,const __m &m2) {return _mm_or_si64(m1,m2);} \
static __forceinline __m xor_si64(const __m &m1,const __m &m2) {return _mm_xor_si64(m1,m2);} \
static __forceinline __m and_si64(const __m &m1,const __m &m2) {return _mm_and_si64(m1,m2);} \
static __forceinline __m andnot_si64(const __m &m1,const __m &m2) {return _mm_andnot_si64(m1,m2);} \
static __forceinline __m mullo_pi16(const __m &m1,const __m &m2) {return _mm_mullo_pi16(m1,m2);} \
static __forceinline __m mulhi_pi16(const __m &m1,const __m &m2) {return _mm_mulhi_pi16(m1,m2);} \
static __forceinline __m unpacklo_pi8(const __m &m1,const __m &m2) {return _mm_unpacklo_pi8(m1,m2);} \
static __forceinline __m unpackhi_pi8(const __m &m1,const __m &m2) {return _mm_unpackhi_pi8(m1,m2);} \
static __forceinline __m cmpgt_pi16(const __m &m1,const __m &m2) {return _mm_cmpgt_pi16(m1,m2);} \
static __forceinline __m cmpeq_pi16(const __m &m1,const __m &m2) {return _mm_cmpeq_pi16(m1,m2);} \
static __forceinline __m cmpeq_pi8(const __m &m1,const __m &m2) {return _mm_cmpeq_pi8(m1,m2);} \
static __forceinline __m shuffle_pi16_0(__m64 mm3) {return _mm_shuffle_pi16_0(mm3);} \
static __forceinline void store2(void *ptr,const __m &m) {*(int2*)ptr=_mm_cvtsi64_si32(m);} \
static __forceinline __m load2(const void *ptr) {return _mm_cvtsi32_si64(*(int2*)ptr);} \
static __forceinline void storeU(void *ptr,const __m &m) {*(__m*)ptr=m;} \
static __forceinline __m loadU(const void *ptr) {return *(__m*)ptr;} \
static __forceinline void empty(void) {_mm_empty();}
struct Tmmx
{
typedef __m64 __m;
typedef int32_t int2;
typedef int32_t integer2_t;
static const size_t size = sizeof(__m);
static const int align = 0;
typedef Tmmx T64;
static __forceinline void pmaxub(__m64 &mmr1, const __m64 &mmr2)
{
mmr1 = _mm_subs_pu8(mmr1, mmr2);
mmr1 = _mm_adds_pu8(mmr1, mmr2);
}
static __forceinline void pmaxub(__m64 &mmr1, const void *mmr2)
{
pmaxub(mmr1, *(__m64*)mmr2);
}
static __forceinline void pminub(__m64 &mmr1, const __m64 &mmr2)
{
__m64 mmrw;
pcmpeqb(mmrw, mmrw);
psubusb(mmrw, mmr2);
paddusb(mmr1, mmrw);
psubusb(mmr1, mmrw);
}
static __forceinline void pminub(__m64 &mmr1, const void *mmr2)
{
pminub(mmr1, *(__m64*)mmr2);
}
static __forceinline void pmaxsw(__m64 &a, const __m64 &b)
{
psubusw(a, b);
paddw(a, b);
}
static __forceinline void pminsw(__m64 &mm4, const __m64 &mm0)
{
__m64 mm2;
movq(mm2, mm4);
psubusw(mm2, mm0);
psubw(mm4, mm2);
}
static __forceinline void pavgb(__m64 ®a, __m64 regb)
{
__m64 regr;
static const __int64 regfe = 0xfefefefefefefefeULL; //_mm_set1_pi8(/*0xfe*/-2);
movq(regr, rega);
por(regr, regb);
pxor(regb, rega);
pand(regb, regfe);
psrlq(regb, 1);
psubb(regr, regb);
rega = regr;
}
static __forceinline void pavgb(__m64 ®a, const void *regb)
{
pavgb(rega, *(__m64*)regb);
}
static __forceinline void v_pavgb(__m64 &mmr1, const __m64 &mmr2, __m64 &mmrw, const __int64 &smask)
{
movq(mmrw, mmr2);
pand(mmrw, smask);
psrlw(mmrw, 1);
pand(mmr1, smask);
psrlw(mmr1, 1);
paddusb(mmr1, mmrw);
}
static __forceinline void v_pavgb(__m64 &mmr1, const void *mmr2, __m64 &mmrw, const __int64 &smask)
{
v_pavgb(mmr1, *(__m64*)mmr2, mmrw, smask);
}
static __forceinline void sfence(void)
{
}
static __forceinline void movntq(void *dst, const __m64 &src)
{
movq(dst, src);
}
static __forceinline void v_pminub(__m64 &mmr1, const __m64 &mmr2, __m64 &mmrw)
{
pcmpeqb(mmrw, mmrw);
psubusb(mmrw, mmr2);
paddusb(mmr1, mmrw);
psubusb(mmr1, mmrw);
}
static __forceinline void v_pminub(__m64 &mmr1, const __int64 &mmr2, __m64 &mmrw)
{
v_pminub(mmr1, *(const __m64*)&mmr2, mmrw);
}
static __forceinline void pmulhuw(__m64 &mm3, const __m64 &mm2)
{
__m64 mm5;
movq(mm5, mm2);
psraw(mm5, 15);
pand(mm5, mm3);
pmulhw(mm3, mm2);
paddw(mm3, mm5);
}
static __forceinline void prefetchnta(const void*)
{
}
static __forceinline void prefetcht0(const void*)
{
}
static __forceinline __m64 _mm_shuffle_pi16_0(__m64 mm3)
{
__m64 mm2;
static const __int64 qwLowWord = 0x000000000000FFFF;
pand(mm3, qwLowWord); // mm3 = same limited to low word
movq(mm2, mm3); // mm2 = same
psllq(mm3, 16); // mm3 = moved to second word
por(mm2, mm3); // mm2 = copied to first and second words
movq(mm3, mm2); // mm3 = same
psllq(mm3, 32); // mm3 = moved to third and fourth words
por(mm2, mm3); // mm2 = low word copied to all four words
return mm2;
}
static __forceinline __m64 _mm_shuffle_pi16_1(const __m64 &src)
{
static const __int64 const1 = 0x00000000FFFF0000LL;
static const __int64 const2 = 0x000000000000FFFFLL;
__m64 w0 = _mm_srli_si64(_mm_and_si64(src, *(__m64*)&const1), 16);
__m64 w1 = _mm_and_si64(src, *(__m64*)&const2);
return _mm_or_si64(_mm_or_si64(_mm_or_si64(_mm_slli_si64(w1, 48), _mm_slli_si64(w1, 32)), _mm_slli_si64(w1, 16)), w0);
}
static __forceinline __m64 _mm_shuffle_pi16_14(const __m64 &src)
{
static const __int64 const1 = 0x000000000000FFFFLL;
static const __int64 const2 = 0xffffffff00000000ULL;
__m64 w34 = _mm_and_si64(src, *(__m64*)&const1);
__m64 w12 = _mm_srli_si64(_mm_and_si64(src, *(__m64*)&const2), 32);
return _mm_or_si64(w12, _mm_or_si64(_mm_slli_si64(w34, 32), _mm_slli_si64(w34, 48)));
}
static __forceinline __m64 _mm_shuffle_pi16_x50(const __m64 &src)
{
static const __int64 const1 = 0x00000000ffff0000LL;
static const __int64 const2 = 0x000000000000ffffLL;
__m64 w3 = _mm_and_si64(src, *(__m64*)&const1);
__m64 w4 = _mm_and_si64(src, *(__m64*)&const2);
return _mm_or_si64(_mm_or_si64(_mm_slli_si64(w3, 32), _mm_slli_si64(w3, 16)) , _mm_or_si64(_mm_slli_si64(w4, 16), w4));
}
static __forceinline void psadbw(__m64 &mm0, const __m64 &SourceMM)
{
__m64 mm1;
movq(mm1, SourceMM);
__m64 mm4;
movq(mm4, mm0);
psubusb(mm0, mm1);
psubusb(mm1, mm4);
por(mm0, mm1);
__m64 mm7 = _mm_setzero_si64();
movq(mm1, mm0);
punpcklbw(mm0, mm7);
punpckhbw(mm1, mm7);
paddusw(mm0, mm1);
static const __int64 mmx_one = 0x0001000100010001LL;
pmaddwd(mm0, mmx_one);
movq(mm7, mm0);
psrlq(mm7, 32);
paddd(mm0, mm7);
static const __int64 mmx_ffff = 0x00000000000fffffLL;
pand(mm0, mmx_ffff);
}
static __forceinline __m64 min_pu8(const __m64 &mm1, const __m64 &mm2)
{
__m64 mm0 = mm1;
pminub(mm0, mm2);
return mm0;
}
static __forceinline __m64 max_pu8(const __m64 &mm1, const __m64 &mm2)
{
__m64 mm0 = mm1;
pmaxub(mm0, mm2);
return mm0;
}
static __forceinline __m64 min_pi16(const __m64 &mm1, const __m64 &mm2)
{
__m64 mm0 = mm1;
pminsw(mm0, mm2);
return mm0;
}
static __forceinline __m64 max_pi16(const __m64 &mm1, const __m64 &mm2)
{
__m64 mm0 = mm1;
pmaxsw(mm0, mm2);
return mm0;
}
MMX_INSTRUCTIONS
};
//===================================== MMXEXT =====================================
struct Tmmxext
{
typedef Tmmx::__m __m;
typedef Tmmx::int2 int2;
static const size_t size = Tmmx::size;
static const int align = Tmmx::align;
typedef Tmmxext T64;
static __forceinline void pmaxub(__m64 &mmr1, const __m64 &mmr2)
{
mmr1 = _mm_max_pu8(mmr1, mmr2);
}
static __forceinline void pmaxub(__m64 &mmr1, const void *mmr2)
{
pmaxub(mmr1, *(__m64*)mmr2);
}
static __forceinline void pminub(__m64 &mmr1, const __m64 &mmr2)
{
mmr1 = _mm_min_pu8(mmr1, mmr2);
}
static __forceinline void pminub(__m64 &mmr1, const void *mmr2)
{
pminub(mmr1, *(__m64*)mmr2);
}
static __forceinline void pminsw(__m64 &mmr1, const __m64 &mmr2)
{
mmr1 = _mm_min_pi16(mmr1, mmr2);
}
static __forceinline void pavgb(__m64 &mmr1, const __m64 &mmr2)
{
mmr1 = _mm_avg_pu8(mmr1, mmr2);
}
static __forceinline void pavgb(__m64 &mmr1, const void *mmr2)
{
mmr1 = _mm_avg_pu8(mmr1, *(__m64*)mmr2);
}
static __forceinline void v_pavgb(__m64 &mmr1, const __m64 &mmr2, __m64, __int64)
{
mmr1 = _mm_avg_pu8(mmr1, mmr2);
}
static __forceinline void v_pavgb(__m64 &mmr1, const void *mmr2, __m64, __int64)
{
mmr1 = _mm_avg_pu8(mmr1, *(__m64*)mmr2);
}
static __forceinline void sfence(void)
{
_mm_sfence();
}
static __forceinline void movntq(void *dst, const __m64 &src)
{
_mm_stream_pi((__m64*)dst, src);
}
static __forceinline void v_pminub(__m64 &mmr1, const __m64 &mmr2, __m64)
{
mmr1 = _mm_min_pu8(mmr1, mmr2);
}
static __forceinline void v_pminub(__m64 &mmr1, const __int64 &mmr2, __m64 &mmrw)
{
v_pminub(mmr1, *(const __m64*)&mmr2, mmrw);
}
static __forceinline void pmulhuw(__m64 &mmr1, const __m64 &mmr2)
{
mmr1 = _mm_mulhi_pu16(mmr1, mmr2);
}
static __forceinline void prefetchnta(const void *ptr)
{
_mm_prefetch((const char*)ptr, _MM_HINT_NTA);
}
static __forceinline void prefetcht0(const void *ptr)
{
_mm_prefetch((const char*)ptr, _MM_HINT_T0);
}
static __forceinline __m64 _mm_shuffle_pi16_0(const __m64 &src)
{
return _mm_shuffle_pi16(src, 0);
}
static __forceinline __m64 _mm_shuffle_pi16_1(const __m64 &src)
{
return _mm_shuffle_pi16(src, 1);
}
static __forceinline __m64 _mm_shuffle_pi16_14(const __m64 &src)
{
return _mm_shuffle_pi16(src, (3 << 2) + 2);
}
static __forceinline __m64 _mm_shuffle_pi16_x50(const __m64 &src)
{
return _mm_shuffle_pi16(src, 0x50);
}
static __forceinline void psadbw(__m64 &mm3, const __m64 &mm2)
{
mm3 = _mm_sad_pu8(mm3, mm2);
}
static __forceinline __m64 min_pu8(const __m64 &mm1, const __m64 &mm2)
{
return _mm_min_pu8(mm1, mm2);
}
static __forceinline __m64 max_pu8(const __m64 &mm1, const __m64 &mm2)
{
return _mm_max_pu8(mm1, mm2);
}
static __forceinline __m64 min_pi16(const __m64 &mm1, const __m64 &mm2)
{
return _mm_min_pi16(mm1, mm2);
}
static __forceinline __m64 max_pi16(const __m64 &mm1, const __m64 &mm2)
{
return _mm_max_pi16(mm1, mm2);
}
static __forceinline void pmaxsw(__m64 &dst, const __m64 &src)
{
dst = _mm_max_pi16(dst, src);
}
MMX_INSTRUCTIONS
};
static __forceinline __m64 _mm_absdif_u8(__m64 mm1, __m64 mm2)
{
__m64 mm7 = mm1;
mm1 = _mm_subs_pu8(mm1, mm2);
mm2 = _mm_subs_pu8(mm2, mm7);
return _mm_or_si64(mm2, mm1);
}
static __forceinline void memadd(unsigned char *dst, const unsigned char *src, unsigned int len)
{
__m64 *dst8 = (__m64*)dst;
const __m64 *src8 = (__m64*)src;
for(unsigned int i = 0; i < len / 8; i++, src8++, dst8++)
*dst8 = _mm_adds_pu8(*src8, *dst8);
}
//====================================== SSE2 ======================================
struct Tsse2
{
typedef __m128i __m;
typedef __m64 int2;
typedef int64_t integer2_t;
static const size_t size = sizeof(__m);
static const int align = 16;
typedef Tmmxext T64;
static __forceinline __m setzero_si64(void)
{
return _mm_setzero_si128();
}
static __forceinline __m set_pi8(char b7, char b6, char b5, char b4, char b3, char b2, char b1, char b0)
{
return _mm_set_epi8(b7, b6, b5, b4, b3, b2, b1, b0, b7, b6, b5, b4, b3, b2, b1, b0);
}
static __forceinline __m set_pi32(int i1, int i0)
{
return _mm_set_epi32(i1, i0, i1, i0);
}
static __forceinline __m set1_pi8(char b)
{
return _mm_set1_epi8(b);
}
static __forceinline __m set1_pi16(short s)
{
return _mm_set1_epi16(s);
}
static __forceinline __m set1_pi64(int64_t s)
{
__align16(int64_t, x[]) = {s, s}; //__m128i _mm_set1_epi64(*(__m64*)&s); TODO: _mm_set1_epi64x
return *(__m*)x;
}
static __forceinline __m packs_pu16(const __m &m1, const __m &m2)
{
return _mm_packus_epi16(m1, m2);
}
static __forceinline __m slli_pi16(const __m &m, int count)
{
return _mm_slli_epi16(m, count);
}
static __forceinline __m srli_pi16(const __m &m, int count)
{
return _mm_srli_epi16(m, count);
}
static __forceinline __m srli_si64(const __m &m, int count)
{
return _mm_srli_epi64(m, count);
}
static __forceinline __m srai_pi16(const __m &m, int count)
{
return _mm_srai_epi16(m, count);
}
static __forceinline __m madd_pi16(const __m &m1, const __m &m2)
{
return _mm_madd_epi16(m1, m2);
}
static __forceinline __m add_pi16(const __m &m1, const __m &m2)
{
return _mm_add_epi16(m1, m2);
}
static __forceinline __m adds_pi16(const __m &m1, const __m &m2)
{
return _mm_adds_epi16(m1, m2);
}
static __forceinline __m adds_pu16(const __m &m1, const __m &m2)
{
return _mm_adds_epu16(m1, m2);
}
static __forceinline __m adds_pu8(const __m &m1, const __m &m2)
{
return _mm_adds_epu8(m1, m2);
}
static __forceinline __m sub_pi16(const __m &m1, const __m &m2)
{
return _mm_sub_epi16(m1, m2);
}
static __forceinline __m subs_pi16(const __m &m1, const __m &m2)
{
return _mm_subs_epi16(m1, m2);
}
static __forceinline __m subs_pu16(const __m &m1, const __m &m2)
{
return _mm_subs_epu16(m1, m2);
}
static __forceinline __m subs_pu8(const __m &m1, const __m &m2)
{
return _mm_subs_epu8(m1, m2);
}
static __forceinline __m or_si64(const __m &m1, const __m &m2)
{
return _mm_or_si128(m1, m2);
}
static __forceinline __m xor_si64(const __m &m1, const __m &m2)
{
return _mm_xor_si128(m1, m2);
}
static __forceinline __m and_si64(const __m &m1, const __m &m2)
{
return _mm_and_si128(m1, m2);
}
static __forceinline __m andnot_si64(const __m &m1, const __m &m2)
{
return _mm_andnot_si128(m1, m2);
}
static __forceinline __m mullo_pi16(const __m &m1, const __m &m2)
{
return _mm_mullo_epi16(m1, m2);
}
static __forceinline __m mulhi_pi16(const __m &m1, const __m &m2)
{
return _mm_mulhi_epi16(m1, m2);
}
static __forceinline __m unpacklo_pi8(const __m &m1, const __m &m2)
{
return _mm_unpacklo_epi8(m1, m2);
}
static __forceinline __m unpackhi_pi8(const __m &m1, const __m &m2)
{
return _mm_unpackhi_epi8(m1, m2);
}
static __forceinline __m cmpgt_pi16(const __m &m1, const __m &m2)
{
return _mm_cmpgt_epi16(m1, m2);
}
static __forceinline __m cmpeq_pi16(const __m &m1, const __m &m2)
{
return _mm_cmpeq_epi16(m1, m2);
}
static __forceinline __m cmpeq_pi8(const __m &m1, const __m &m2)
{
return _mm_cmpeq_epi8(m1, m2);
}
static __forceinline __m min_pi16(const __m &mm1, const __m &mm2)
{
return _mm_min_epi16(mm1, mm2);
}
static __forceinline __m max_pi16(const __m &mm1, const __m &mm2)
{
return _mm_max_epi16(mm1, mm2);
}
static __forceinline __m load2(const void *ptr)
{
return _mm_loadl_epi64((const __m128i*)ptr);
}
static __forceinline void store2(void *ptr, const __m &m)
{
_mm_storel_epi64((__m128i*)ptr, m);
}
static __forceinline void storeU(void *ptr, const __m &m)
{
_mm_storeu_si128((__m*)ptr, m);
}
static __forceinline __m loadU(const void *ptr)
{
return _mm_loadu_si128((const __m*)ptr);
}
static __forceinline void empty(void)
{
/*_mm_empty();*/
}
static __forceinline void psadbw(__m &mm3, const __m &mm2)
{
mm3 = _mm_sad_epu8(mm3, mm2);
}
static __forceinline void prefetchnta(const void *ptr)
{
_mm_prefetch((const char*)ptr, _MM_HINT_NTA);
}
static __forceinline __m shuffle_pi16_0(const __m &mm0)
{
return _mm_shufflehi_epi16(_mm_shufflelo_epi16(mm0, 0), 0);
}
static __forceinline void pmaxub(__m &mmr1, const __m &mmr2)
{
mmr1 = _mm_max_epu8(mmr1, mmr2);
}
static __forceinline void pmulhuw(__m &mmr1, const __m &mmr2)
{
mmr1 = _mm_mulhi_epu16(mmr1, mmr2);
}
static __forceinline void movntq(void *dst, const __m &src)
{
_mm_stream_si128((__m128i*)dst, src);
}
static __forceinline void pavgb(__m &mmr1, const __m &mmr2)
{
mmr1 = _mm_avg_epu8(mmr1, mmr2);
}
static __forceinline void pavgb(__m &mmr1, const void *mmr2)
{
mmr1 = _mm_avg_epu8(mmr1, *(__m*)mmr2);
}
static __forceinline void sfence(void)
{
_mm_sfence();
}
};
template<class _mm> static __forceinline typename _mm::__m abs_16(const typename _mm::__m &mm0)
{
typename _mm::__m mm6 = _mm::srai_pi16(mm0, 15);
return _mm::sub_pi16(_mm::xor_si64(mm0, mm6), mm6);
}
template<class _mm> static __forceinline typename _mm::__m absdif_s16(typename _mm::__m mm0, typename _mm::__m mm1)
{
typename _mm::__m mm2 = mm0;
mm0 = _mm::cmpgt_pi16(mm0, mm1);
typename _mm::__m mm4 = mm2;
mm2 = _mm::xor_si64(mm2, mm1);
mm2 = _mm::and_si64(mm2, mm0);
typename _mm::__m mm3 = mm2;
mm4 = _mm::xor_si64(mm4, mm2);
mm1 = _mm::xor_si64(mm1, mm3);
return _mm::sub_pi16(mm1, mm4);
}
#pragma warning(pop)
#endif
|