-
Notifications
You must be signed in to change notification settings - Fork 9
/
ssse3.c
837 lines (727 loc) · 21.1 KB
/
ssse3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/*
.d8888b. .d8888b. .d8888b. 8888888888 .d8888b.
d88P Y88b d88P Y88b d88P Y88b 888 d88P Y88b
Y88b. Y88b. Y88b. 888 .d88P
"Y888b. "Y888b. "Y888b. 8888888 8888"
"Y88b. "Y88b. "Y88b. 888 "Y8b.
"888 "888 "888 888 888 888
Y88b d88P Y88b d88P Y88b d88P 888 Y88b d88P
"Y8888P" "Y8888P" "Y8888P" 8888888888 "Y8888P"
*/
#include "opemu.h"
#include "ssse3_priv.h"
#include <linux/module.h>
#include <linux/ftrace.h>
#include <linux/kallsyms.h>
#include <linux/kernel.h>
#include <linux/linkage.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/uaccess.h>
/**
* Store xmm register somewhere in memory
*/
inline void _sstore_xmm (const uint8_t n, __uint128_t *where)
{
switch (n) {
case 0: storedqu_template(0, where); break;
case 1: storedqu_template(1, where); break;
case 2: storedqu_template(2, where); break;
case 3: storedqu_template(3, where); break;
case 4: storedqu_template(4, where); break;
case 5: storedqu_template(5, where); break;
case 6: storedqu_template(6, where); break;
case 7: storedqu_template(7, where); break;
case 8: storedqu_template(8, where); break;
case 9: storedqu_template(9, where); break;
case 10: storedqu_template(10, where); break;
case 11: storedqu_template(11, where); break;
case 12: storedqu_template(12, where); break;
case 13: storedqu_template(13, where); break;
case 14: storedqu_template(14, where); break;
case 15: storedqu_template(15, where); break;
}}
/**
* Load xmm register from memory
*/
inline void _sload_xmm (const uint8_t n, const __uint128_t *where)
{
switch (n) {
case 0: loaddqu_template(0, where); break;
case 1: loaddqu_template(1, where); break;
case 2: loaddqu_template(2, where); break;
case 3: loaddqu_template(3, where); break;
case 4: loaddqu_template(4, where); break;
case 5: loaddqu_template(5, where); break;
case 6: loaddqu_template(6, where); break;
case 7: loaddqu_template(7, where); break;
case 8: loaddqu_template(8, where); break;
case 9: loaddqu_template(9, where); break;
case 10: loaddqu_template(10, where); break;
case 11: loaddqu_template(11, where); break;
case 12: loaddqu_template(12, where); break;
case 13: loaddqu_template(13, where); break;
case 14: loaddqu_template(14, where); break;
case 15: loaddqu_template(15, where); break;
}}
/**
* Store mmx register somewhere in memory
*/
inline void _sstore_mmx (const uint8_t n, uint64_t *where)
{
switch (n) {
case 0: storeq_template(0, where); break;
case 1: storeq_template(1, where); break;
case 2: storeq_template(2, where); break;
case 3: storeq_template(3, where); break;
case 4: storeq_template(4, where); break;
case 5: storeq_template(5, where); break;
case 6: storeq_template(6, where); break;
case 7: storeq_template(7, where); break;
}}
/**
* Load mmx register from memory
*/
inline void _sload_mmx (const uint8_t n, const uint64_t *where)
{
switch (n) {
case 0: loadq_template(0, where); break;
case 1: loadq_template(1, where); break;
case 2: loadq_template(2, where); break;
case 3: loadq_template(3, where); break;
case 4: loadq_template(4, where); break;
case 5: loadq_template(5, where); break;
case 6: loadq_template(6, where); break;
case 7: loadq_template(7, where); break;
}}
inline void _sstore_gpr32 (ud_type_t n, uint32_t *where) {
struct pt_regs *regs;
switch (n) {
case UD_R_EAX:
*where = ®s->ax;
break;
case UD_R_ECX:
*where = ®s->cx;
break;
case UD_R_EDX:
*where = ®s->dx;
break;
case UD_R_EBX:
*where = ®s->bx;
break;
case UD_R_ESP:
*where = ®s->sp;
break;
case UD_R_EBP:
*where = ®s->bp;
break;
case UD_R_ESI:
*where = ®s->si;
break;
case UD_R_EDI:
*where = ®s->di;
break;
}
}
inline void _sstore_gpr64 (ud_type_t n, uint64_t *where) {
struct pt_regs *regs;
switch (n) {
case UD_R_RAX:
*where = ®s->ax;
break;
case UD_R_RCX:
*where = ®s->cx;
break;
case UD_R_RDX:
*where = ®s->dx;
break;
case UD_R_RBX:
*where = ®s->bx;
break;
case UD_R_RSP:
*where = ®s->sp;
break;
case UD_R_RBP:
*where = ®s->bp;
break;
case UD_R_RSI:
*where = ®s->si;
break;
case UD_R_RDI:
*where = ®s->di;
break;
}
}
inline void _fstore_xmm (const uint8_t n, float *where)
{
switch (n) {
case 0: fstored_template(0, where); break;
case 1: fstored_template(1, where); break;
case 2: fstored_template(2, where); break;
case 3: fstored_template(3, where); break;
case 4: fstored_template(4, where); break;
case 5: fstored_template(5, where); break;
case 6: fstored_template(6, where); break;
case 7: fstored_template(7, where); break;
case 8: fstored_template(8, where); break;
case 9: fstored_template(9, where); break;
case 10: fstored_template(10, where); break;
case 11: fstored_template(11, where); break;
case 12: fstored_template(12, where); break;
case 13: fstored_template(13, where); break;
case 14: fstored_template(14, where); break;
case 15: fstored_template(15, where); break;
}}
inline void _fload_xmm (const uint8_t n, const float *where)
{
switch (n) {
case 0: floadd_template(0, where); break;
case 1: floadd_template(1, where); break;
case 2: floadd_template(2, where); break;
case 3: floadd_template(3, where); break;
case 4: floadd_template(4, where); break;
case 5: floadd_template(5, where); break;
case 6: floadd_template(6, where); break;
case 7: floadd_template(7, where); break;
case 8: floadd_template(8, where); break;
case 9: floadd_template(9, where); break;
case 10: floadd_template(10, where); break;
case 11: floadd_template(11, where); break;
case 12: floadd_template(12, where); break;
case 13: floadd_template(13, where); break;
case 14: floadd_template(14, where); break;
case 15: floadd_template(15, where); break;
}}
/**
* Load operands from memory/register, store in obj.
* @return: 0 if success
*/
int ssse3_grab_operands(ssse3_t *ssse3_obj)
{
if (ssse3_obj->udo_dst->type == UD_OP_REG) {
if (ssse3_obj->ismmx) {
_sstore_mmx (ssse3_obj->udo_dst->base - UD_R_MM0, &ssse3_obj->dst.uint64[0]);
}
else if (ssse3_obj->udo_dst->base >= UD_R_EAX && ssse3_obj->udo_dst->base <= UD_R_R15D) {
_sstore_gpr32 (ssse3_obj->udo_dst->base, &ssse3_obj->dst.uint32[0]);
}
else if (ssse3_obj->udo_dst->base >= UD_R_RAX && ssse3_obj->udo_dst->base <= UD_R_R15) {
_sstore_gpr64 (ssse3_obj->udo_dst->base, &ssse3_obj->dst.uint64[0]);
}
else {
_sstore_xmm (ssse3_obj->udo_dst->base - UD_R_XMM0, &ssse3_obj->dst.uint128);
}
} else {
printk("mem");
}
if (ssse3_obj->udo_src->type == UD_OP_REG) {
if (ud_insn_mnemonic(ssse3_obj->op_obj->ud_obj) == UD_Iroundss) {
_fstore_xmm (ssse3_obj->udo_src->base - UD_R_XMM0, &ssse3_obj->src.fa32[0]);
}
else if (ssse3_obj->udo_src->base >= UD_R_MM0 && ssse3_obj->udo_src->base <= UD_R_MM7) {
_sstore_mmx (ssse3_obj->udo_src->base - UD_R_MM0, &ssse3_obj->src.uint64[0]);
}
else if (ssse3_obj->udo_src->base >= UD_R_EAX && ssse3_obj->udo_src->base <= UD_R_R15D) {
printk("eee %s", ud_insn_asm(ssse3_obj->op_obj->ud_obj));
_sstore_gpr32 (ssse3_obj->udo_src->base, &ssse3_obj->src.uint32[0]);
printk("ssse3_obj->src.uint32[0]: %u", ssse3_obj->src.uint32[0]);
}
else if (ssse3_obj->udo_src->base >= UD_R_RAX && ssse3_obj->udo_src->base <= UD_R_R15) {
_sstore_gpr64 (ssse3_obj->udo_src->base, &ssse3_obj->src.uint64[0]);
}
else {
_sstore_xmm (ssse3_obj->udo_src->base - UD_R_XMM0, &ssse3_obj->src.uint128);
}
} else {
printk("src mem");
if (ssse3_obj->udo_src->size == 32) {
printk("src mem32");
}
else if (ssse3_obj->udo_src->size == 64) {
// m64 load
int64_t disp = 0;
uint8_t disp_size = ssse3_obj->udo_src->offset;
uint64_t address;
if (ssse3_obj->udo_src->scale) goto bad; // TODO
if (retrieve_reg (ssse3_obj->op_obj->state,
ssse3_obj->udo_src->base, NULL, &address) != 0) goto bad;
switch (disp_size) {
case 8: disp = ssse3_obj->udo_src->lval.sbyte; break;
case 16: disp = ssse3_obj->udo_src->lval.sword; break;
case 32: disp = ssse3_obj->udo_src->lval.sdword; break;
case 64: disp = ssse3_obj->udo_src->lval.sqword; break;
}
address += disp;
if (ssse3_obj->op_obj->ring0)
ssse3_obj->src.uint64[0] = * ((uint64_t*) (address));
else copy_from_user((char*) &ssse3_obj->src.uint64[0], address, 8);
}
else if (ssse3_obj->udo_src->size == 128) {
// m128 load
int64_t disp = 0;
uint8_t disp_size = ssse3_obj->udo_src->offset;
uint64_t address;
if (ssse3_obj->udo_src->scale) goto bad; // TODO
if (retrieve_reg (ssse3_obj->op_obj->state,
ssse3_obj->udo_src->base, NULL, &address) != 0) goto bad;
switch (disp_size) {
case 8: disp = ssse3_obj->udo_src->lval.sbyte; break;
case 16: disp = ssse3_obj->udo_src->lval.sword; break;
case 32: disp = ssse3_obj->udo_src->lval.sdword; break;
case 64: disp = ssse3_obj->udo_src->lval.sqword; break;
}
address += disp;
if (ssse3_obj->op_obj->ring0)
ssse3_obj->src.uint128 = * ((__uint128_t*) (address));
else copy_from_user((char*) &ssse3_obj->src.uint128, address, 16);
}
else {
printk("src mem else");
}
}
return 0;
// Only reached if bad
bad: return -1;
}
/**
* Store operands from obj to memory/register.
* @return: 0 if success
*/
int ssse3_commit_results(ssse3_t *ssse3_obj)
{
if (ud_insn_mnemonic(ssse3_obj->op_obj->ud_obj) == UD_Iroundss) {
_fload_xmm (ssse3_obj->udo_dst->base - UD_R_XMM0, (void*) &ssse3_obj->res.fa32[0]);
}
else if (ssse3_obj->ismmx) {
_sload_mmx (ssse3_obj->udo_dst->base - UD_R_MM0, (void*) &ssse3_obj->res.uint64[0]);
}
else if (ssse3_obj->udo_dst->type == UD_OP_REG && ssse3_obj->udo_dst->base >= UD_R_RAX && ssse3_obj->udo_dst->base <= UD_R_R15) {
//printk("dst64");
ssse3_obj->dst64 = 1;
return 0;
}
else if (ssse3_obj->udo_dst->type == UD_OP_REG && ssse3_obj->udo_dst->base >= UD_R_EAX && ssse3_obj->udo_dst->base <= UD_R_R15D) {
ssse3_obj->dst32 = 1;
return 0;
}
else if (ssse3_obj->udo_dst->type == UD_OP_MEM) {
printk("base: %u size: %u", ssse3_obj->udo_dst->base, ssse3_obj->udo_dst->size);
}
else {
_sload_xmm (ssse3_obj->udo_dst->base - UD_R_XMM0, (void*) &ssse3_obj->res.uint128);
}
return 0;
// Only reached if bad
//bad: return -1;
}
/**
* Main function for the ssse3 portion. Check if the offending
* opcode is part of the ssse3 instruction set, if not, quit early.
* if so, then we build the appropriate context, and jump to the right function.
* @param op_obj: opemu object
* @return: zero if an instruction was emulated properly
*/
int op_sse3x_run(op_t *op_obj)
{
ssse3_t ssse3_obj;
ssse3_obj.op_obj = op_obj;
const uint32_t mnemonic = ud_insn_mnemonic(ssse3_obj.op_obj->ud_obj);
ssse3_func opf;
switch (mnemonic) {
case UD_Ipcmpestri: opf = pcmpestri; goto sse42_common;
case UD_Ipcmpestrm: opf = pcmpestrm; goto sse42_common;
case UD_Ipcmpistri: opf = pcmpistri; goto sse42_common;
case UD_Ipcmpistrm: opf = pcmpistrm; goto sse42_common;
case UD_Ipcmpgtq: opf = pcmpgtq; goto sse42_common;
case UD_Ipopcnt: opf = popcnt; goto regop;
case UD_Icrc32: opf = crc32_op; goto regop;
//SSE 4.1
//case UD_Iblendpd: opf = blendpd; goto ssse3_common;
//case UD_Iblendps: opf = blendps; goto ssse3_common;
//case UD_Ipblendw: opf = pblendw; goto ssse3_common;
//case UD_Ipmovsxbd: opf = pmovsxbd; goto ssse3_common;
//case UD_Ipmovsxbq: opf = pmovsxbq; goto ssse3_common;
//case UD_Ipmovsxbw: opf = pmovsxbw; goto ssse3_common;
//case UD_Ipmovsxdq: opf = pmovsxdq; goto ssse3_common;
//case UD_Ipmovsxwd: opf = pmovsxwd; goto ssse3_common;
//case UD_Ipmovsxwq: opf = pmovsxwq; goto ssse3_common;
//case UD_Ipmovzxbd: opf = pmovzxbd; goto ssse3_common;
//case UD_Ipmovzxbq: opf = pmovzxbq; goto ssse3_common;
//case UD_Ipmovzxbw: opf = pmovzxbw; goto ssse3_common;
//case UD_Ipmovzxdq: opf = pmovzxdq; goto ssse3_common;
//case UD_Ipmovzxwd: opf = pmovzxwd; goto ssse3_common;
//case UD_Ipmovzxwq: opf = pmovzxwq; goto ssse3_common;
case UD_Iroundss: opf = roundss; goto ssse3_common;
case UD_Ipextrb: opf = pextrb; goto ssse3_common;
case UD_Ipextrd: opf = pextrd; goto ssse3_common;
case UD_Ipextrq: opf = pextrq; goto ssse3_common;
case UD_Iptest: opf = ptest; goto ssse3_common;
case UD_Ipinsrb: opf = pinsrb; goto ssse3_common;
case UD_Ipinsrd: opf = pinsrd; goto ssse3_common;
case UD_Ipinsrq: opf = pinsrq; goto ssse3_common;
sse42_common:
goto ssse3_common;
case UD_Ipsignb: opf = psignb; goto ssse3_common;
case UD_Ipsignw: opf = psignw; goto ssse3_common;
case UD_Ipsignd: opf = psignd; goto ssse3_common;
case UD_Ipabsb: opf = pabsb; goto ssse3_common;
case UD_Ipabsw: opf = pabsw; goto ssse3_common;
case UD_Ipabsd: opf = pabsd; goto ssse3_common;
case UD_Ipalignr: opf = palignr; goto ssse3_common;
case UD_Ipshufb: opf = pshufb; goto ssse3_common;
case UD_Ipmulhrsw: opf = pmulhrsw; goto ssse3_common;
case UD_Ipmaddubsw: opf = pmaddubsw; goto ssse3_common;
case UD_Iphsubw: opf = phsubw; goto ssse3_common;
case UD_Iphsubd: opf = phsubd; goto ssse3_common;
case UD_Iphsubsw: opf = phsubsw; goto ssse3_common;
case UD_Iphaddw: opf = phaddw; goto ssse3_common;
case UD_Iphaddd: opf = phaddd; goto ssse3_common;
case UD_Iphaddsw: opf = phaddsw; goto ssse3_common;
ssse3_common:
ssse3_obj.udo_src = ud_insn_opr (op_obj->ud_obj, 1);
ssse3_obj.udo_dst = ud_insn_opr (op_obj->ud_obj, 0);
ssse3_obj.udo_imm = ud_insn_opr (op_obj->ud_obj, 2);
// run some sanity checks,
if ((ssse3_obj.udo_dst->type != UD_OP_REG) && (ssse3_obj.udo_dst->type != UD_OP_MEM)) goto bad;
if ((ssse3_obj.udo_src->type != UD_OP_REG) && (ssse3_obj.udo_src->type != UD_OP_MEM)) goto bad;
// i'd like to know if this instruction is legacy mmx
if ((ssse3_obj.udo_dst->base >= UD_R_MM0) && (ssse3_obj.udo_dst->base <= UD_R_MM7)) {
ssse3_obj.ismmx = 1;
} else ssse3_obj.ismmx = 0;
ssse3_obj.dst64 = ssse3_obj.dst32 = 0;
if (ssse3_grab_operands(&ssse3_obj) != 0) goto bad;
opf(&ssse3_obj);
if (ssse3_commit_results(&ssse3_obj)) goto bad;
goto good;
default: goto bad;
}
// Regular (non-SSE regs) handling
regop:
ssse3_obj.udo_src = ud_insn_opr (op_obj->ud_obj, 1);
ssse3_obj.udo_dst = (ud_operand_t *)ud_insn_opr (op_obj->ud_obj, 0);
ssse3_obj.udo_imm = ud_insn_opr (op_obj->ud_obj, 2);
opf(&ssse3_obj);
good:
if (ssse3_obj.dst64) {
//printk("OPEMUq: %s\n", ud_insn_asm(op_obj->ud_obj));
op_obj->dst64 = (uint8_t) 1;
op_obj->res64 = (uint64_t) ssse3_obj.res.uint64[0];
}
if (ssse3_obj.dst32) {
op_obj->dst32 = (uint8_t) 1;
op_obj->res32 = (uint32_t) ssse3_obj.res.uint32[0];
}
//uint64_t ek;
//asm __volatile__ ("movq %%rcx, %0" : "=m" (ek) :);
//printk("good rcx: %u", ek);
return 0;
// Only reached if bad
bad:
return -1;
}
#define SATSW(x) ((x > 32767)? 32767 : ((x < -32768)? -32768 : x) )
/**
* Negate/zero/preserve
*/
void psignb (ssse3_t *this)
{
const int8_t *src = &this->src.int8[0];
const int8_t *dst = &this->dst.int8[0];
int8_t *res = &this->res.int8[0];
int count = (this->ismmx) ? 8 : 16;
int i;
for (i = 0; i < count; ++ i) {
if (*src < 0) *res = - *dst;
else if (*src == 0) *res = 0;
else if (*src > 0) *res = *dst;
++res; ++src; ++dst;
}
}
/**
* Negate/zero/preserve
*/
void psignw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 4 : 8;
int i;
for (i = 0; i < count; ++ i) {
if (*src < 0) *res = - *dst;
else if (*src == 0) *res = 0;
else if (*src > 0) *res = *dst;
++res; ++src; ++dst;
}
}
/**
* Negate/zero/preserve
*/
void psignd (ssse3_t *this)
{
const int32_t *src = &this->src.int32[0];
const int32_t *dst = &this->dst.int32[0];
int32_t *res = &this->res.int32[0];
int count = (this->ismmx) ? 2 : 4;
int i;
for (i = 0; i < count; ++ i) {
if (*src < 0) *res = - *dst;
else if (*src == 0) *res = 0;
else if (*src > 0) *res = *dst;
++res; ++src; ++dst;
}
}
/**
* Absolute value
*/
void pabsb (ssse3_t *this)
{
const int8_t *src = &this->src.int8[0];
const int8_t *dst = &this->dst.int8[0];
int8_t *res = &this->res.int8[0];
int count = (this->ismmx) ? 8 : 16;
int i;
for (i = 0; i < count; ++ i) {
if (*src < 0) *res = - *src;
else *res = *src;
++res; ++src; ++dst;
}
}
/**
* Absolute value
*/
void pabsw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 4 : 8;
int i;
for (i = 0; i < count; ++ i) {
if (*src < 0) *res = - *src;
else *res = *src;
++res; ++src; ++dst;
}
}
/**
* Absolute value
*/
void pabsd (ssse3_t *this)
{
const int32_t *src = &this->src.int32[0];
const int32_t *dst = &this->dst.int32[0];
int32_t *res = &this->res.int32[0];
int count = (this->ismmx) ? 2 : 4;
int i;
for (i = 0; i < count; ++ i) {
if (*src < 0) *res = - *src;
else *res = *src;
++res; ++src; ++dst;
}
}
/**
* Concatenate and shift
*/
void palignr (ssse3_t *this)
{
uint8_t imm = this->udo_imm->lval.ubyte;
if (this->ismmx) {
__uint128_t temp1 = this->dst.uint64[0];
temp1 <<= 64;
temp1 |= this->src.uint64[0];
temp1 >>= (imm * 8);
this->res.uint128 = temp1;
} else {
// AnV - Cast fixed
__uint128_t temp1[2];
uint8_t *shiftp; // that type matters for pointer arithmetic
uint64_t shiftpaddr;
temp1[0] = this->src.uint128;
temp1[1] = this->dst.uint128;
shiftp = (uint8_t*) &temp1[0];
shiftp += imm;
shiftpaddr = (uint64_t)shiftp;
this->res.uint128 = ((__uint128_t*) shiftpaddr);
}
}
/**
* Shuffle Bytes
*/
void pshufb (ssse3_t *this)
{
const uint8_t *src = &this->src.uint8[0];
uint8_t *res = &this->res.uint8[0];
int count = (this->ismmx) ? 8 : 16;
uint8_t mask = (this->ismmx) ? 0b0111 : 0b1111;
int i;
for (i = 0; i < count; ++ i) {
if (*src & 0x80) *res = 0;
else *res = this->dst.uint8[ *src & mask ];
++res; ++src;
}
}
/**
* Multiply high with round and scale
*/
void pmulhrsw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 4 : 8;
int i;
for (i = 0; i < count; ++i) {
int32_t temp1 = (*dst) * (*src);
temp1 >>= 14;
temp1++;
temp1 >>= 1;
*res = temp1 & 0xFFFF;
++res; ++src; ++dst;
}
}
/**
* Multiply and add
*/
void pmaddubsw (ssse3_t *this)
{
const int8_t *src = &this->src.int8[0];
const uint8_t *dst = &this->dst.uint8[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 4 : 8;
int i;
for (i = 0; i < count; ++i) {
int64_t temp1 = (src[0] * dst[0]) + (src[1] * dst[1]);
*res = SATSW(temp1);
++res;
src += 2;
dst += 2;
}
}
/**
* Horizontal subtract
*/
void phsubw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 2 : 4;
int i;
for (i = 0; i < count; ++ i) {
*res = (dst[0]) - (dst[1]);
++res;
dst += 2;
}
for (i = 0; i < count; ++ i) {
*res = (src[0]) - (src[1]);
++res;
src += 2;
}
}
/**
* Horizontal subtract
*/
void phsubd (ssse3_t *this)
{
const int32_t *src = &this->src.int32[0];
const int32_t *dst = &this->dst.int32[0];
int32_t *res = &this->res.int32[0];
int count = (this->ismmx) ? 1 : 2;
int i;
for (i = 0; i < count; ++ i) {
*res = (dst[0]) - (dst[1]);
++res;
dst += 2;
}
for (i = 0; i < count; ++ i) {
*res = (src[0]) - (src[1]);
++res;
src += 2;
}
}
/**
* Horizontal subtract and saturate
*/
void phsubsw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 2 : 4;
int i;
for (i = 0; i < count; ++ i) {
*res = SATSW((dst[0]) - (dst[1]));
++res;
dst += 2;
}
for (i = 0; i < count; ++ i) {
*res = SATSW((src[0]) - (src[1]));
++res;
src += 2;
}
}
/**
* Horizontal add
*/
void phaddw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 2 : 4;
int i;
for (i = 0; i < count; ++ i) {
*res = dst[0] + dst[1];
++res;
dst += 2;
}
for (i = 0; i < count; ++ i) {
*res = src[0] + src[1];
++res;
src += 2;
}
}
/**
* Horizontal add
*/
void phaddd (ssse3_t *this)
{
const int32_t *src = &this->src.int32[0];
const int32_t *dst = &this->dst.int32[0];
int32_t *res = &this->res.int32[0];
int count = (this->ismmx) ? 1 : 2;
int i;
for (i = 0; i < count; ++ i) {
*res = dst[0] + dst[1];
++res;
dst += 2;
}
for (i = 0; i < count; ++ i) {
*res = src[0] + src[1];
++res;
src += 2;
}
}
/**
* Horizontal add and saturate
*/
void phaddsw (ssse3_t *this)
{
const int16_t *src = &this->src.int16[0];
const int16_t *dst = &this->dst.int16[0];
int16_t *res = &this->res.int16[0];
int count = (this->ismmx) ? 2 : 4;
int i;
for (i = 0; i < count; ++ i) {
*res = SATSW(dst[0] + dst[1]);
++res;
dst += 2;
}
for (i = 0; i < count; ++ i) {
*res = SATSW(src[0] + src[1]);
++res;
src += 2;
}
}