VirtualBox

source: vbox/trunk/src/recompiler/target-i386/ops_sse.h@ 22173

最後變更 在這個檔案從22173是 18879,由 vboxsync 提交於 16 年 前

REM: cleanup

  • 屬性 svn:eol-style 設為 native
檔案大小: 58.9 KB
 
1/*
2 * MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5 * Copyright (c) 2008 Intel Corporation <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21/*
22 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
24 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
25 * a choice of LGPL license versions is made available with the language indicating
26 * that LGPLv2 or any later version may be used, or where a choice of which version
27 * of the LGPL is applied is otherwise unspecified.
28 */
29
30#if SHIFT == 0
31#define Reg MMXReg
32#ifndef VBOX
33#define XMM_ONLY(x...)
34#else
35#define XMM_ONLY(x)
36#endif
37#define B(n) MMX_B(n)
38#define W(n) MMX_W(n)
39#define L(n) MMX_L(n)
40#define Q(n) q
41#define SUFFIX _mmx
42#else
43#define Reg XMMReg
44#ifndef VBOX
45#define XMM_ONLY(x...) x
46#else
47#define XMM_ONLY(x) x
48#endif
49#define B(n) XMM_B(n)
50#define W(n) XMM_W(n)
51#define L(n) XMM_L(n)
52#define Q(n) XMM_Q(n)
53#define SUFFIX _xmm
54#endif
55
56void glue(helper_psrlw, SUFFIX)(Reg *d, Reg *s)
57{
58 int shift;
59
60 if (s->Q(0) > 15) {
61 d->Q(0) = 0;
62#if SHIFT == 1
63 d->Q(1) = 0;
64#endif
65 } else {
66 shift = s->B(0);
67 d->W(0) >>= shift;
68 d->W(1) >>= shift;
69 d->W(2) >>= shift;
70 d->W(3) >>= shift;
71#if SHIFT == 1
72 d->W(4) >>= shift;
73 d->W(5) >>= shift;
74 d->W(6) >>= shift;
75 d->W(7) >>= shift;
76#endif
77 }
78}
79
80void glue(helper_psraw, SUFFIX)(Reg *d, Reg *s)
81{
82 int shift;
83
84 if (s->Q(0) > 15) {
85 shift = 15;
86 } else {
87 shift = s->B(0);
88 }
89 d->W(0) = (int16_t)d->W(0) >> shift;
90 d->W(1) = (int16_t)d->W(1) >> shift;
91 d->W(2) = (int16_t)d->W(2) >> shift;
92 d->W(3) = (int16_t)d->W(3) >> shift;
93#if SHIFT == 1
94 d->W(4) = (int16_t)d->W(4) >> shift;
95 d->W(5) = (int16_t)d->W(5) >> shift;
96 d->W(6) = (int16_t)d->W(6) >> shift;
97 d->W(7) = (int16_t)d->W(7) >> shift;
98#endif
99}
100
101void glue(helper_psllw, SUFFIX)(Reg *d, Reg *s)
102{
103 int shift;
104
105 if (s->Q(0) > 15) {
106 d->Q(0) = 0;
107#if SHIFT == 1
108 d->Q(1) = 0;
109#endif
110 } else {
111 shift = s->B(0);
112 d->W(0) <<= shift;
113 d->W(1) <<= shift;
114 d->W(2) <<= shift;
115 d->W(3) <<= shift;
116#if SHIFT == 1
117 d->W(4) <<= shift;
118 d->W(5) <<= shift;
119 d->W(6) <<= shift;
120 d->W(7) <<= shift;
121#endif
122 }
123}
124
125void glue(helper_psrld, SUFFIX)(Reg *d, Reg *s)
126{
127 int shift;
128
129 if (s->Q(0) > 31) {
130 d->Q(0) = 0;
131#if SHIFT == 1
132 d->Q(1) = 0;
133#endif
134 } else {
135 shift = s->B(0);
136 d->L(0) >>= shift;
137 d->L(1) >>= shift;
138#if SHIFT == 1
139 d->L(2) >>= shift;
140 d->L(3) >>= shift;
141#endif
142 }
143}
144
145void glue(helper_psrad, SUFFIX)(Reg *d, Reg *s)
146{
147 int shift;
148
149 if (s->Q(0) > 31) {
150 shift = 31;
151 } else {
152 shift = s->B(0);
153 }
154 d->L(0) = (int32_t)d->L(0) >> shift;
155 d->L(1) = (int32_t)d->L(1) >> shift;
156#if SHIFT == 1
157 d->L(2) = (int32_t)d->L(2) >> shift;
158 d->L(3) = (int32_t)d->L(3) >> shift;
159#endif
160}
161
162void glue(helper_pslld, SUFFIX)(Reg *d, Reg *s)
163{
164 int shift;
165
166 if (s->Q(0) > 31) {
167 d->Q(0) = 0;
168#if SHIFT == 1
169 d->Q(1) = 0;
170#endif
171 } else {
172 shift = s->B(0);
173 d->L(0) <<= shift;
174 d->L(1) <<= shift;
175#if SHIFT == 1
176 d->L(2) <<= shift;
177 d->L(3) <<= shift;
178#endif
179 }
180}
181
182void glue(helper_psrlq, SUFFIX)(Reg *d, Reg *s)
183{
184 int shift;
185
186 if (s->Q(0) > 63) {
187 d->Q(0) = 0;
188#if SHIFT == 1
189 d->Q(1) = 0;
190#endif
191 } else {
192 shift = s->B(0);
193 d->Q(0) >>= shift;
194#if SHIFT == 1
195 d->Q(1) >>= shift;
196#endif
197 }
198}
199
200void glue(helper_psllq, SUFFIX)(Reg *d, Reg *s)
201{
202 int shift;
203
204 if (s->Q(0) > 63) {
205 d->Q(0) = 0;
206#if SHIFT == 1
207 d->Q(1) = 0;
208#endif
209 } else {
210 shift = s->B(0);
211 d->Q(0) <<= shift;
212#if SHIFT == 1
213 d->Q(1) <<= shift;
214#endif
215 }
216}
217
218#if SHIFT == 1
219void glue(helper_psrldq, SUFFIX)(Reg *d, Reg *s)
220{
221 int shift, i;
222
223 shift = s->L(0);
224 if (shift > 16)
225 shift = 16;
226 for(i = 0; i < 16 - shift; i++)
227 d->B(i) = d->B(i + shift);
228 for(i = 16 - shift; i < 16; i++)
229 d->B(i) = 0;
230}
231
232void glue(helper_pslldq, SUFFIX)(Reg *d, Reg *s)
233{
234 int shift, i;
235
236 shift = s->L(0);
237 if (shift > 16)
238 shift = 16;
239 for(i = 15; i >= shift; i--)
240 d->B(i) = d->B(i - shift);
241 for(i = 0; i < shift; i++)
242 d->B(i) = 0;
243}
244#endif
245
246#define SSE_HELPER_B(name, F)\
247void glue(name, SUFFIX) (Reg *d, Reg *s)\
248{\
249 d->B(0) = F(d->B(0), s->B(0));\
250 d->B(1) = F(d->B(1), s->B(1));\
251 d->B(2) = F(d->B(2), s->B(2));\
252 d->B(3) = F(d->B(3), s->B(3));\
253 d->B(4) = F(d->B(4), s->B(4));\
254 d->B(5) = F(d->B(5), s->B(5));\
255 d->B(6) = F(d->B(6), s->B(6));\
256 d->B(7) = F(d->B(7), s->B(7));\
257 XMM_ONLY(\
258 d->B(8) = F(d->B(8), s->B(8));\
259 d->B(9) = F(d->B(9), s->B(9));\
260 d->B(10) = F(d->B(10), s->B(10));\
261 d->B(11) = F(d->B(11), s->B(11));\
262 d->B(12) = F(d->B(12), s->B(12));\
263 d->B(13) = F(d->B(13), s->B(13));\
264 d->B(14) = F(d->B(14), s->B(14));\
265 d->B(15) = F(d->B(15), s->B(15));\
266 )\
267}
268
269#define SSE_HELPER_W(name, F)\
270void glue(name, SUFFIX) (Reg *d, Reg *s)\
271{\
272 d->W(0) = F(d->W(0), s->W(0));\
273 d->W(1) = F(d->W(1), s->W(1));\
274 d->W(2) = F(d->W(2), s->W(2));\
275 d->W(3) = F(d->W(3), s->W(3));\
276 XMM_ONLY(\
277 d->W(4) = F(d->W(4), s->W(4));\
278 d->W(5) = F(d->W(5), s->W(5));\
279 d->W(6) = F(d->W(6), s->W(6));\
280 d->W(7) = F(d->W(7), s->W(7));\
281 )\
282}
283
284#define SSE_HELPER_L(name, F)\
285void glue(name, SUFFIX) (Reg *d, Reg *s)\
286{\
287 d->L(0) = F(d->L(0), s->L(0));\
288 d->L(1) = F(d->L(1), s->L(1));\
289 XMM_ONLY(\
290 d->L(2) = F(d->L(2), s->L(2));\
291 d->L(3) = F(d->L(3), s->L(3));\
292 )\
293}
294
295#define SSE_HELPER_Q(name, F)\
296void glue(name, SUFFIX) (Reg *d, Reg *s)\
297{\
298 d->Q(0) = F(d->Q(0), s->Q(0));\
299 XMM_ONLY(\
300 d->Q(1) = F(d->Q(1), s->Q(1));\
301 )\
302}
303
304#if SHIFT == 0
305#ifndef VBOX
306static inline int satub(int x)
307#else /* VBOX */
308DECLINLINE(int) satub(int x)
309#endif /* VBOX */
310{
311 if (x < 0)
312 return 0;
313 else if (x > 255)
314 return 255;
315 else
316 return x;
317}
318
319#ifndef VBOX
320static inline int satuw(int x)
321#else /* VBOX */
322DECLINLINE(int) satuw(int x)
323#endif /* VBOX */
324{
325 if (x < 0)
326 return 0;
327 else if (x > 65535)
328 return 65535;
329 else
330 return x;
331}
332
333#ifndef VBOX
334static inline int satsb(int x)
335#else /* VBOX */
336DECLINLINE(int) satsb(int x)
337#endif /* VBOX */
338{
339 if (x < -128)
340 return -128;
341 else if (x > 127)
342 return 127;
343 else
344 return x;
345}
346
347#ifndef VBOX
348static inline int satsw(int x)
349#else /* VBOX */
350DECLINLINE(int) satsw(int x)
351#endif /* VBOX */
352{
353 if (x < -32768)
354 return -32768;
355 else if (x > 32767)
356 return 32767;
357 else
358 return x;
359}
360
361#define FADD(a, b) ((a) + (b))
362#define FADDUB(a, b) satub((a) + (b))
363#define FADDUW(a, b) satuw((a) + (b))
364#define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
365#define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
366
367#define FSUB(a, b) ((a) - (b))
368#define FSUBUB(a, b) satub((a) - (b))
369#define FSUBUW(a, b) satuw((a) - (b))
370#define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
371#define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
372#define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
373#define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
374#define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
375#define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
376
377#define FAND(a, b) (a) & (b)
378#define FANDN(a, b) ((~(a)) & (b))
379#define FOR(a, b) (a) | (b)
380#define FXOR(a, b) (a) ^ (b)
381
382#define FCMPGTB(a, b) (int8_t)(a) > (int8_t)(b) ? -1 : 0
383#define FCMPGTW(a, b) (int16_t)(a) > (int16_t)(b) ? -1 : 0
384#define FCMPGTL(a, b) (int32_t)(a) > (int32_t)(b) ? -1 : 0
385#define FCMPEQ(a, b) (a) == (b) ? -1 : 0
386
387#define FMULLW(a, b) (a) * (b)
388#define FMULHRW(a, b) ((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16
389#define FMULHUW(a, b) (a) * (b) >> 16
390#define FMULHW(a, b) (int16_t)(a) * (int16_t)(b) >> 16
391
392#define FAVG(a, b) ((a) + (b) + 1) >> 1
393#endif
394
395SSE_HELPER_B(helper_paddb, FADD)
396SSE_HELPER_W(helper_paddw, FADD)
397SSE_HELPER_L(helper_paddl, FADD)
398SSE_HELPER_Q(helper_paddq, FADD)
399
400SSE_HELPER_B(helper_psubb, FSUB)
401SSE_HELPER_W(helper_psubw, FSUB)
402SSE_HELPER_L(helper_psubl, FSUB)
403SSE_HELPER_Q(helper_psubq, FSUB)
404
405SSE_HELPER_B(helper_paddusb, FADDUB)
406SSE_HELPER_B(helper_paddsb, FADDSB)
407SSE_HELPER_B(helper_psubusb, FSUBUB)
408SSE_HELPER_B(helper_psubsb, FSUBSB)
409
410SSE_HELPER_W(helper_paddusw, FADDUW)
411SSE_HELPER_W(helper_paddsw, FADDSW)
412SSE_HELPER_W(helper_psubusw, FSUBUW)
413SSE_HELPER_W(helper_psubsw, FSUBSW)
414
415SSE_HELPER_B(helper_pminub, FMINUB)
416SSE_HELPER_B(helper_pmaxub, FMAXUB)
417
418SSE_HELPER_W(helper_pminsw, FMINSW)
419SSE_HELPER_W(helper_pmaxsw, FMAXSW)
420
421SSE_HELPER_Q(helper_pand, FAND)
422SSE_HELPER_Q(helper_pandn, FANDN)
423SSE_HELPER_Q(helper_por, FOR)
424SSE_HELPER_Q(helper_pxor, FXOR)
425
426SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
427SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
428SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
429
430SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
431SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
432SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
433
434SSE_HELPER_W(helper_pmullw, FMULLW)
435#if SHIFT == 0
436SSE_HELPER_W(helper_pmulhrw, FMULHRW)
437#endif
438SSE_HELPER_W(helper_pmulhuw, FMULHUW)
439SSE_HELPER_W(helper_pmulhw, FMULHW)
440
441SSE_HELPER_B(helper_pavgb, FAVG)
442SSE_HELPER_W(helper_pavgw, FAVG)
443
444void glue(helper_pmuludq, SUFFIX) (Reg *d, Reg *s)
445{
446 d->Q(0) = (uint64_t)s->L(0) * (uint64_t)d->L(0);
447#if SHIFT == 1
448 d->Q(1) = (uint64_t)s->L(2) * (uint64_t)d->L(2);
449#endif
450}
451
452void glue(helper_pmaddwd, SUFFIX) (Reg *d, Reg *s)
453{
454 int i;
455
456 for(i = 0; i < (2 << SHIFT); i++) {
457 d->L(i) = (int16_t)s->W(2*i) * (int16_t)d->W(2*i) +
458 (int16_t)s->W(2*i+1) * (int16_t)d->W(2*i+1);
459 }
460}
461
462#if SHIFT == 0
463#ifndef VBOX
464static inline int abs1(int a)
465#else /* VBOX */
466DECLINLINE(int) abs1(int a)
467#endif /* VBOX */
468{
469 if (a < 0)
470 return -a;
471 else
472 return a;
473}
474#endif
475void glue(helper_psadbw, SUFFIX) (Reg *d, Reg *s)
476{
477 unsigned int val;
478
479 val = 0;
480 val += abs1(d->B(0) - s->B(0));
481 val += abs1(d->B(1) - s->B(1));
482 val += abs1(d->B(2) - s->B(2));
483 val += abs1(d->B(3) - s->B(3));
484 val += abs1(d->B(4) - s->B(4));
485 val += abs1(d->B(5) - s->B(5));
486 val += abs1(d->B(6) - s->B(6));
487 val += abs1(d->B(7) - s->B(7));
488 d->Q(0) = val;
489#if SHIFT == 1
490 val = 0;
491 val += abs1(d->B(8) - s->B(8));
492 val += abs1(d->B(9) - s->B(9));
493 val += abs1(d->B(10) - s->B(10));
494 val += abs1(d->B(11) - s->B(11));
495 val += abs1(d->B(12) - s->B(12));
496 val += abs1(d->B(13) - s->B(13));
497 val += abs1(d->B(14) - s->B(14));
498 val += abs1(d->B(15) - s->B(15));
499 d->Q(1) = val;
500#endif
501}
502
503void glue(helper_maskmov, SUFFIX) (Reg *d, Reg *s, target_ulong a0)
504{
505 int i;
506 for(i = 0; i < (8 << SHIFT); i++) {
507 if (s->B(i) & 0x80)
508 stb(a0 + i, d->B(i));
509 }
510}
511
512void glue(helper_movl_mm_T0, SUFFIX) (Reg *d, uint32_t val)
513{
514 d->L(0) = val;
515 d->L(1) = 0;
516#if SHIFT == 1
517 d->Q(1) = 0;
518#endif
519}
520
521#ifdef TARGET_X86_64
522void glue(helper_movq_mm_T0, SUFFIX) (Reg *d, uint64_t val)
523{
524 d->Q(0) = val;
525#if SHIFT == 1
526 d->Q(1) = 0;
527#endif
528}
529#endif
530
531#if SHIFT == 0
532void glue(helper_pshufw, SUFFIX) (Reg *d, Reg *s, int order)
533{
534 Reg r;
535 r.W(0) = s->W(order & 3);
536 r.W(1) = s->W((order >> 2) & 3);
537 r.W(2) = s->W((order >> 4) & 3);
538 r.W(3) = s->W((order >> 6) & 3);
539 *d = r;
540}
541#else
542void helper_shufps(Reg *d, Reg *s, int order)
543{
544 Reg r;
545 r.L(0) = d->L(order & 3);
546 r.L(1) = d->L((order >> 2) & 3);
547 r.L(2) = s->L((order >> 4) & 3);
548 r.L(3) = s->L((order >> 6) & 3);
549 *d = r;
550}
551
552void helper_shufpd(Reg *d, Reg *s, int order)
553{
554 Reg r;
555 r.Q(0) = d->Q(order & 1);
556 r.Q(1) = s->Q((order >> 1) & 1);
557 *d = r;
558}
559
560void glue(helper_pshufd, SUFFIX) (Reg *d, Reg *s, int order)
561{
562 Reg r;
563 r.L(0) = s->L(order & 3);
564 r.L(1) = s->L((order >> 2) & 3);
565 r.L(2) = s->L((order >> 4) & 3);
566 r.L(3) = s->L((order >> 6) & 3);
567 *d = r;
568}
569
570void glue(helper_pshuflw, SUFFIX) (Reg *d, Reg *s, int order)
571{
572 Reg r;
573 r.W(0) = s->W(order & 3);
574 r.W(1) = s->W((order >> 2) & 3);
575 r.W(2) = s->W((order >> 4) & 3);
576 r.W(3) = s->W((order >> 6) & 3);
577 r.Q(1) = s->Q(1);
578 *d = r;
579}
580
581void glue(helper_pshufhw, SUFFIX) (Reg *d, Reg *s, int order)
582{
583 Reg r;
584 r.Q(0) = s->Q(0);
585 r.W(4) = s->W(4 + (order & 3));
586 r.W(5) = s->W(4 + ((order >> 2) & 3));
587 r.W(6) = s->W(4 + ((order >> 4) & 3));
588 r.W(7) = s->W(4 + ((order >> 6) & 3));
589 *d = r;
590}
591#endif
592
593#if SHIFT == 1
594/* FPU ops */
595/* XXX: not accurate */
596
597#define SSE_HELPER_S(name, F)\
598void helper_ ## name ## ps (Reg *d, Reg *s)\
599{\
600 d->XMM_S(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
601 d->XMM_S(1) = F(32, d->XMM_S(1), s->XMM_S(1));\
602 d->XMM_S(2) = F(32, d->XMM_S(2), s->XMM_S(2));\
603 d->XMM_S(3) = F(32, d->XMM_S(3), s->XMM_S(3));\
604}\
605\
606void helper_ ## name ## ss (Reg *d, Reg *s)\
607{\
608 d->XMM_S(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
609}\
610void helper_ ## name ## pd (Reg *d, Reg *s)\
611{\
612 d->XMM_D(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
613 d->XMM_D(1) = F(64, d->XMM_D(1), s->XMM_D(1));\
614}\
615\
616void helper_ ## name ## sd (Reg *d, Reg *s)\
617{\
618 d->XMM_D(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
619}
620
621#define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
622#define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
623#define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
624#define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
625#define FPU_MIN(size, a, b) (a) < (b) ? (a) : (b)
626#define FPU_MAX(size, a, b) (a) > (b) ? (a) : (b)
627#define FPU_SQRT(size, a, b) float ## size ## _sqrt(b, &env->sse_status)
628
629SSE_HELPER_S(add, FPU_ADD)
630SSE_HELPER_S(sub, FPU_SUB)
631SSE_HELPER_S(mul, FPU_MUL)
632SSE_HELPER_S(div, FPU_DIV)
633SSE_HELPER_S(min, FPU_MIN)
634SSE_HELPER_S(max, FPU_MAX)
635SSE_HELPER_S(sqrt, FPU_SQRT)
636
637
638/* float to float conversions */
639void helper_cvtps2pd(Reg *d, Reg *s)
640{
641 float32 s0, s1;
642 s0 = s->XMM_S(0);
643 s1 = s->XMM_S(1);
644 d->XMM_D(0) = float32_to_float64(s0, &env->sse_status);
645 d->XMM_D(1) = float32_to_float64(s1, &env->sse_status);
646}
647
648void helper_cvtpd2ps(Reg *d, Reg *s)
649{
650 d->XMM_S(0) = float64_to_float32(s->XMM_D(0), &env->sse_status);
651 d->XMM_S(1) = float64_to_float32(s->XMM_D(1), &env->sse_status);
652 d->Q(1) = 0;
653}
654
655void helper_cvtss2sd(Reg *d, Reg *s)
656{
657 d->XMM_D(0) = float32_to_float64(s->XMM_S(0), &env->sse_status);
658}
659
660void helper_cvtsd2ss(Reg *d, Reg *s)
661{
662 d->XMM_S(0) = float64_to_float32(s->XMM_D(0), &env->sse_status);
663}
664
665/* integer to float */
666void helper_cvtdq2ps(Reg *d, Reg *s)
667{
668 d->XMM_S(0) = int32_to_float32(s->XMM_L(0), &env->sse_status);
669 d->XMM_S(1) = int32_to_float32(s->XMM_L(1), &env->sse_status);
670 d->XMM_S(2) = int32_to_float32(s->XMM_L(2), &env->sse_status);
671 d->XMM_S(3) = int32_to_float32(s->XMM_L(3), &env->sse_status);
672}
673
674void helper_cvtdq2pd(Reg *d, Reg *s)
675{
676 int32_t l0, l1;
677 l0 = (int32_t)s->XMM_L(0);
678 l1 = (int32_t)s->XMM_L(1);
679 d->XMM_D(0) = int32_to_float64(l0, &env->sse_status);
680 d->XMM_D(1) = int32_to_float64(l1, &env->sse_status);
681}
682
683void helper_cvtpi2ps(XMMReg *d, MMXReg *s)
684{
685 d->XMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
686 d->XMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
687}
688
689void helper_cvtpi2pd(XMMReg *d, MMXReg *s)
690{
691 d->XMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
692 d->XMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
693}
694
695void helper_cvtsi2ss(XMMReg *d, uint32_t val)
696{
697 d->XMM_S(0) = int32_to_float32(val, &env->sse_status);
698}
699
700void helper_cvtsi2sd(XMMReg *d, uint32_t val)
701{
702 d->XMM_D(0) = int32_to_float64(val, &env->sse_status);
703}
704
705#ifdef TARGET_X86_64
706void helper_cvtsq2ss(XMMReg *d, uint64_t val)
707{
708 d->XMM_S(0) = int64_to_float32(val, &env->sse_status);
709}
710
711void helper_cvtsq2sd(XMMReg *d, uint64_t val)
712{
713 d->XMM_D(0) = int64_to_float64(val, &env->sse_status);
714}
715#endif
716
717/* float to integer */
718void helper_cvtps2dq(XMMReg *d, XMMReg *s)
719{
720 d->XMM_L(0) = float32_to_int32(s->XMM_S(0), &env->sse_status);
721 d->XMM_L(1) = float32_to_int32(s->XMM_S(1), &env->sse_status);
722 d->XMM_L(2) = float32_to_int32(s->XMM_S(2), &env->sse_status);
723 d->XMM_L(3) = float32_to_int32(s->XMM_S(3), &env->sse_status);
724}
725
726void helper_cvtpd2dq(XMMReg *d, XMMReg *s)
727{
728 d->XMM_L(0) = float64_to_int32(s->XMM_D(0), &env->sse_status);
729 d->XMM_L(1) = float64_to_int32(s->XMM_D(1), &env->sse_status);
730 d->XMM_Q(1) = 0;
731}
732
733void helper_cvtps2pi(MMXReg *d, XMMReg *s)
734{
735 d->MMX_L(0) = float32_to_int32(s->XMM_S(0), &env->sse_status);
736 d->MMX_L(1) = float32_to_int32(s->XMM_S(1), &env->sse_status);
737}
738
739void helper_cvtpd2pi(MMXReg *d, XMMReg *s)
740{
741 d->MMX_L(0) = float64_to_int32(s->XMM_D(0), &env->sse_status);
742 d->MMX_L(1) = float64_to_int32(s->XMM_D(1), &env->sse_status);
743}
744
745int32_t helper_cvtss2si(XMMReg *s)
746{
747 return float32_to_int32(s->XMM_S(0), &env->sse_status);
748}
749
750int32_t helper_cvtsd2si(XMMReg *s)
751{
752 return float64_to_int32(s->XMM_D(0), &env->sse_status);
753}
754
755#ifdef TARGET_X86_64
756int64_t helper_cvtss2sq(XMMReg *s)
757{
758 return float32_to_int64(s->XMM_S(0), &env->sse_status);
759}
760
761int64_t helper_cvtsd2sq(XMMReg *s)
762{
763 return float64_to_int64(s->XMM_D(0), &env->sse_status);
764}
765#endif
766
767/* float to integer truncated */
768void helper_cvttps2dq(XMMReg *d, XMMReg *s)
769{
770 d->XMM_L(0) = float32_to_int32_round_to_zero(s->XMM_S(0), &env->sse_status);
771 d->XMM_L(1) = float32_to_int32_round_to_zero(s->XMM_S(1), &env->sse_status);
772 d->XMM_L(2) = float32_to_int32_round_to_zero(s->XMM_S(2), &env->sse_status);
773 d->XMM_L(3) = float32_to_int32_round_to_zero(s->XMM_S(3), &env->sse_status);
774}
775
776void helper_cvttpd2dq(XMMReg *d, XMMReg *s)
777{
778 d->XMM_L(0) = float64_to_int32_round_to_zero(s->XMM_D(0), &env->sse_status);
779 d->XMM_L(1) = float64_to_int32_round_to_zero(s->XMM_D(1), &env->sse_status);
780 d->XMM_Q(1) = 0;
781}
782
783void helper_cvttps2pi(MMXReg *d, XMMReg *s)
784{
785 d->MMX_L(0) = float32_to_int32_round_to_zero(s->XMM_S(0), &env->sse_status);
786 d->MMX_L(1) = float32_to_int32_round_to_zero(s->XMM_S(1), &env->sse_status);
787}
788
789void helper_cvttpd2pi(MMXReg *d, XMMReg *s)
790{
791 d->MMX_L(0) = float64_to_int32_round_to_zero(s->XMM_D(0), &env->sse_status);
792 d->MMX_L(1) = float64_to_int32_round_to_zero(s->XMM_D(1), &env->sse_status);
793}
794
795int32_t helper_cvttss2si(XMMReg *s)
796{
797 return float32_to_int32_round_to_zero(s->XMM_S(0), &env->sse_status);
798}
799
800int32_t helper_cvttsd2si(XMMReg *s)
801{
802 return float64_to_int32_round_to_zero(s->XMM_D(0), &env->sse_status);
803}
804
805#ifdef TARGET_X86_64
806int64_t helper_cvttss2sq(XMMReg *s)
807{
808 return float32_to_int64_round_to_zero(s->XMM_S(0), &env->sse_status);
809}
810
811int64_t helper_cvttsd2sq(XMMReg *s)
812{
813 return float64_to_int64_round_to_zero(s->XMM_D(0), &env->sse_status);
814}
815#endif
816
817void helper_rsqrtps(XMMReg *d, XMMReg *s)
818{
819 d->XMM_S(0) = approx_rsqrt(s->XMM_S(0));
820 d->XMM_S(1) = approx_rsqrt(s->XMM_S(1));
821 d->XMM_S(2) = approx_rsqrt(s->XMM_S(2));
822 d->XMM_S(3) = approx_rsqrt(s->XMM_S(3));
823}
824
825void helper_rsqrtss(XMMReg *d, XMMReg *s)
826{
827 d->XMM_S(0) = approx_rsqrt(s->XMM_S(0));
828}
829
830void helper_rcpps(XMMReg *d, XMMReg *s)
831{
832 d->XMM_S(0) = approx_rcp(s->XMM_S(0));
833 d->XMM_S(1) = approx_rcp(s->XMM_S(1));
834 d->XMM_S(2) = approx_rcp(s->XMM_S(2));
835 d->XMM_S(3) = approx_rcp(s->XMM_S(3));
836}
837
838void helper_rcpss(XMMReg *d, XMMReg *s)
839{
840 d->XMM_S(0) = approx_rcp(s->XMM_S(0));
841}
842
843void helper_haddps(XMMReg *d, XMMReg *s)
844{
845 XMMReg r;
846 r.XMM_S(0) = d->XMM_S(0) + d->XMM_S(1);
847 r.XMM_S(1) = d->XMM_S(2) + d->XMM_S(3);
848 r.XMM_S(2) = s->XMM_S(0) + s->XMM_S(1);
849 r.XMM_S(3) = s->XMM_S(2) + s->XMM_S(3);
850 *d = r;
851}
852
853void helper_haddpd(XMMReg *d, XMMReg *s)
854{
855 XMMReg r;
856 r.XMM_D(0) = d->XMM_D(0) + d->XMM_D(1);
857 r.XMM_D(1) = s->XMM_D(0) + s->XMM_D(1);
858 *d = r;
859}
860
861void helper_hsubps(XMMReg *d, XMMReg *s)
862{
863 XMMReg r;
864 r.XMM_S(0) = d->XMM_S(0) - d->XMM_S(1);
865 r.XMM_S(1) = d->XMM_S(2) - d->XMM_S(3);
866 r.XMM_S(2) = s->XMM_S(0) - s->XMM_S(1);
867 r.XMM_S(3) = s->XMM_S(2) - s->XMM_S(3);
868 *d = r;
869}
870
871void helper_hsubpd(XMMReg *d, XMMReg *s)
872{
873 XMMReg r;
874 r.XMM_D(0) = d->XMM_D(0) - d->XMM_D(1);
875 r.XMM_D(1) = s->XMM_D(0) - s->XMM_D(1);
876 *d = r;
877}
878
879void helper_addsubps(XMMReg *d, XMMReg *s)
880{
881 d->XMM_S(0) = d->XMM_S(0) - s->XMM_S(0);
882 d->XMM_S(1) = d->XMM_S(1) + s->XMM_S(1);
883 d->XMM_S(2) = d->XMM_S(2) - s->XMM_S(2);
884 d->XMM_S(3) = d->XMM_S(3) + s->XMM_S(3);
885}
886
887void helper_addsubpd(XMMReg *d, XMMReg *s)
888{
889 d->XMM_D(0) = d->XMM_D(0) - s->XMM_D(0);
890 d->XMM_D(1) = d->XMM_D(1) + s->XMM_D(1);
891}
892
893/* XXX: unordered */
894#define SSE_HELPER_CMP(name, F)\
895void helper_ ## name ## ps (Reg *d, Reg *s)\
896{\
897 d->XMM_L(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
898 d->XMM_L(1) = F(32, d->XMM_S(1), s->XMM_S(1));\
899 d->XMM_L(2) = F(32, d->XMM_S(2), s->XMM_S(2));\
900 d->XMM_L(3) = F(32, d->XMM_S(3), s->XMM_S(3));\
901}\
902\
903void helper_ ## name ## ss (Reg *d, Reg *s)\
904{\
905 d->XMM_L(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
906}\
907void helper_ ## name ## pd (Reg *d, Reg *s)\
908{\
909 d->XMM_Q(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
910 d->XMM_Q(1) = F(64, d->XMM_D(1), s->XMM_D(1));\
911}\
912\
913void helper_ ## name ## sd (Reg *d, Reg *s)\
914{\
915 d->XMM_Q(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
916}
917
918#define FPU_CMPEQ(size, a, b) float ## size ## _eq(a, b, &env->sse_status) ? -1 : 0
919#define FPU_CMPLT(size, a, b) float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0
920#define FPU_CMPLE(size, a, b) float ## size ## _le(a, b, &env->sse_status) ? -1 : 0
921#define FPU_CMPUNORD(size, a, b) float ## size ## _unordered(a, b, &env->sse_status) ? - 1 : 0
922#define FPU_CMPNEQ(size, a, b) float ## size ## _eq(a, b, &env->sse_status) ? 0 : -1
923#define FPU_CMPNLT(size, a, b) float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1
924#define FPU_CMPNLE(size, a, b) float ## size ## _le(a, b, &env->sse_status) ? 0 : -1
925#define FPU_CMPORD(size, a, b) float ## size ## _unordered(a, b, &env->sse_status) ? 0 : -1
926
927SSE_HELPER_CMP(cmpeq, FPU_CMPEQ)
928SSE_HELPER_CMP(cmplt, FPU_CMPLT)
929SSE_HELPER_CMP(cmple, FPU_CMPLE)
930SSE_HELPER_CMP(cmpunord, FPU_CMPUNORD)
931SSE_HELPER_CMP(cmpneq, FPU_CMPNEQ)
932SSE_HELPER_CMP(cmpnlt, FPU_CMPNLT)
933SSE_HELPER_CMP(cmpnle, FPU_CMPNLE)
934SSE_HELPER_CMP(cmpord, FPU_CMPORD)
935
936const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
937
938void helper_ucomiss(Reg *d, Reg *s)
939{
940 int ret;
941 float32 s0, s1;
942
943 s0 = d->XMM_S(0);
944 s1 = s->XMM_S(0);
945 ret = float32_compare_quiet(s0, s1, &env->sse_status);
946 CC_SRC = comis_eflags[ret + 1];
947}
948
949void helper_comiss(Reg *d, Reg *s)
950{
951 int ret;
952 float32 s0, s1;
953
954 s0 = d->XMM_S(0);
955 s1 = s->XMM_S(0);
956 ret = float32_compare(s0, s1, &env->sse_status);
957 CC_SRC = comis_eflags[ret + 1];
958}
959
960void helper_ucomisd(Reg *d, Reg *s)
961{
962 int ret;
963 float64 d0, d1;
964
965 d0 = d->XMM_D(0);
966 d1 = s->XMM_D(0);
967 ret = float64_compare_quiet(d0, d1, &env->sse_status);
968 CC_SRC = comis_eflags[ret + 1];
969}
970
971void helper_comisd(Reg *d, Reg *s)
972{
973 int ret;
974 float64 d0, d1;
975
976 d0 = d->XMM_D(0);
977 d1 = s->XMM_D(0);
978 ret = float64_compare(d0, d1, &env->sse_status);
979 CC_SRC = comis_eflags[ret + 1];
980}
981
982uint32_t helper_movmskps(Reg *s)
983{
984 int b0, b1, b2, b3;
985 b0 = s->XMM_L(0) >> 31;
986 b1 = s->XMM_L(1) >> 31;
987 b2 = s->XMM_L(2) >> 31;
988 b3 = s->XMM_L(3) >> 31;
989 return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3);
990}
991
992uint32_t helper_movmskpd(Reg *s)
993{
994 int b0, b1;
995 b0 = s->XMM_L(1) >> 31;
996 b1 = s->XMM_L(3) >> 31;
997 return b0 | (b1 << 1);
998}
999
1000#endif
1001
1002uint32_t glue(helper_pmovmskb, SUFFIX)(Reg *s)
1003{
1004 uint32_t val;
1005 val = 0;
1006 val |= (s->XMM_B(0) >> 7);
1007 val |= (s->XMM_B(1) >> 6) & 0x02;
1008 val |= (s->XMM_B(2) >> 5) & 0x04;
1009 val |= (s->XMM_B(3) >> 4) & 0x08;
1010 val |= (s->XMM_B(4) >> 3) & 0x10;
1011 val |= (s->XMM_B(5) >> 2) & 0x20;
1012 val |= (s->XMM_B(6) >> 1) & 0x40;
1013 val |= (s->XMM_B(7)) & 0x80;
1014#if SHIFT == 1
1015 val |= (s->XMM_B(8) << 1) & 0x0100;
1016 val |= (s->XMM_B(9) << 2) & 0x0200;
1017 val |= (s->XMM_B(10) << 3) & 0x0400;
1018 val |= (s->XMM_B(11) << 4) & 0x0800;
1019 val |= (s->XMM_B(12) << 5) & 0x1000;
1020 val |= (s->XMM_B(13) << 6) & 0x2000;
1021 val |= (s->XMM_B(14) << 7) & 0x4000;
1022 val |= (s->XMM_B(15) << 8) & 0x8000;
1023#endif
1024 return val;
1025}
1026
1027void glue(helper_packsswb, SUFFIX) (Reg *d, Reg *s)
1028{
1029 Reg r;
1030
1031 r.B(0) = satsb((int16_t)d->W(0));
1032 r.B(1) = satsb((int16_t)d->W(1));
1033 r.B(2) = satsb((int16_t)d->W(2));
1034 r.B(3) = satsb((int16_t)d->W(3));
1035#if SHIFT == 1
1036 r.B(4) = satsb((int16_t)d->W(4));
1037 r.B(5) = satsb((int16_t)d->W(5));
1038 r.B(6) = satsb((int16_t)d->W(6));
1039 r.B(7) = satsb((int16_t)d->W(7));
1040#endif
1041 r.B((4 << SHIFT) + 0) = satsb((int16_t)s->W(0));
1042 r.B((4 << SHIFT) + 1) = satsb((int16_t)s->W(1));
1043 r.B((4 << SHIFT) + 2) = satsb((int16_t)s->W(2));
1044 r.B((4 << SHIFT) + 3) = satsb((int16_t)s->W(3));
1045#if SHIFT == 1
1046 r.B(12) = satsb((int16_t)s->W(4));
1047 r.B(13) = satsb((int16_t)s->W(5));
1048 r.B(14) = satsb((int16_t)s->W(6));
1049 r.B(15) = satsb((int16_t)s->W(7));
1050#endif
1051 *d = r;
1052}
1053
1054void glue(helper_packuswb, SUFFIX) (Reg *d, Reg *s)
1055{
1056 Reg r;
1057
1058 r.B(0) = satub((int16_t)d->W(0));
1059 r.B(1) = satub((int16_t)d->W(1));
1060 r.B(2) = satub((int16_t)d->W(2));
1061 r.B(3) = satub((int16_t)d->W(3));
1062#if SHIFT == 1
1063 r.B(4) = satub((int16_t)d->W(4));
1064 r.B(5) = satub((int16_t)d->W(5));
1065 r.B(6) = satub((int16_t)d->W(6));
1066 r.B(7) = satub((int16_t)d->W(7));
1067#endif
1068 r.B((4 << SHIFT) + 0) = satub((int16_t)s->W(0));
1069 r.B((4 << SHIFT) + 1) = satub((int16_t)s->W(1));
1070 r.B((4 << SHIFT) + 2) = satub((int16_t)s->W(2));
1071 r.B((4 << SHIFT) + 3) = satub((int16_t)s->W(3));
1072#if SHIFT == 1
1073 r.B(12) = satub((int16_t)s->W(4));
1074 r.B(13) = satub((int16_t)s->W(5));
1075 r.B(14) = satub((int16_t)s->W(6));
1076 r.B(15) = satub((int16_t)s->W(7));
1077#endif
1078 *d = r;
1079}
1080
1081void glue(helper_packssdw, SUFFIX) (Reg *d, Reg *s)
1082{
1083 Reg r;
1084
1085 r.W(0) = satsw(d->L(0));
1086 r.W(1) = satsw(d->L(1));
1087#if SHIFT == 1
1088 r.W(2) = satsw(d->L(2));
1089 r.W(3) = satsw(d->L(3));
1090#endif
1091 r.W((2 << SHIFT) + 0) = satsw(s->L(0));
1092 r.W((2 << SHIFT) + 1) = satsw(s->L(1));
1093#if SHIFT == 1
1094 r.W(6) = satsw(s->L(2));
1095 r.W(7) = satsw(s->L(3));
1096#endif
1097 *d = r;
1098}
1099
1100#define UNPCK_OP(base_name, base) \
1101 \
1102void glue(helper_punpck ## base_name ## bw, SUFFIX) (Reg *d, Reg *s) \
1103{ \
1104 Reg r; \
1105 \
1106 r.B(0) = d->B((base << (SHIFT + 2)) + 0); \
1107 r.B(1) = s->B((base << (SHIFT + 2)) + 0); \
1108 r.B(2) = d->B((base << (SHIFT + 2)) + 1); \
1109 r.B(3) = s->B((base << (SHIFT + 2)) + 1); \
1110 r.B(4) = d->B((base << (SHIFT + 2)) + 2); \
1111 r.B(5) = s->B((base << (SHIFT + 2)) + 2); \
1112 r.B(6) = d->B((base << (SHIFT + 2)) + 3); \
1113 r.B(7) = s->B((base << (SHIFT + 2)) + 3); \
1114XMM_ONLY( \
1115 r.B(8) = d->B((base << (SHIFT + 2)) + 4); \
1116 r.B(9) = s->B((base << (SHIFT + 2)) + 4); \
1117 r.B(10) = d->B((base << (SHIFT + 2)) + 5); \
1118 r.B(11) = s->B((base << (SHIFT + 2)) + 5); \
1119 r.B(12) = d->B((base << (SHIFT + 2)) + 6); \
1120 r.B(13) = s->B((base << (SHIFT + 2)) + 6); \
1121 r.B(14) = d->B((base << (SHIFT + 2)) + 7); \
1122 r.B(15) = s->B((base << (SHIFT + 2)) + 7); \
1123) \
1124 *d = r; \
1125} \
1126 \
1127void glue(helper_punpck ## base_name ## wd, SUFFIX) (Reg *d, Reg *s) \
1128{ \
1129 Reg r; \
1130 \
1131 r.W(0) = d->W((base << (SHIFT + 1)) + 0); \
1132 r.W(1) = s->W((base << (SHIFT + 1)) + 0); \
1133 r.W(2) = d->W((base << (SHIFT + 1)) + 1); \
1134 r.W(3) = s->W((base << (SHIFT + 1)) + 1); \
1135XMM_ONLY( \
1136 r.W(4) = d->W((base << (SHIFT + 1)) + 2); \
1137 r.W(5) = s->W((base << (SHIFT + 1)) + 2); \
1138 r.W(6) = d->W((base << (SHIFT + 1)) + 3); \
1139 r.W(7) = s->W((base << (SHIFT + 1)) + 3); \
1140) \
1141 *d = r; \
1142} \
1143 \
1144void glue(helper_punpck ## base_name ## dq, SUFFIX) (Reg *d, Reg *s) \
1145{ \
1146 Reg r; \
1147 \
1148 r.L(0) = d->L((base << SHIFT) + 0); \
1149 r.L(1) = s->L((base << SHIFT) + 0); \
1150XMM_ONLY( \
1151 r.L(2) = d->L((base << SHIFT) + 1); \
1152 r.L(3) = s->L((base << SHIFT) + 1); \
1153) \
1154 *d = r; \
1155} \
1156 \
1157XMM_ONLY( \
1158void glue(helper_punpck ## base_name ## qdq, SUFFIX) (Reg *d, Reg *s) \
1159{ \
1160 Reg r; \
1161 \
1162 r.Q(0) = d->Q(base); \
1163 r.Q(1) = s->Q(base); \
1164 *d = r; \
1165} \
1166)
1167
1168UNPCK_OP(l, 0)
1169UNPCK_OP(h, 1)
1170
1171/* 3DNow! float ops */
1172#if SHIFT == 0
1173void helper_pi2fd(MMXReg *d, MMXReg *s)
1174{
1175 d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1176 d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1177}
1178
1179void helper_pi2fw(MMXReg *d, MMXReg *s)
1180{
1181 d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1182 d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1183}
1184
1185void helper_pf2id(MMXReg *d, MMXReg *s)
1186{
1187 d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1188 d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1189}
1190
1191void helper_pf2iw(MMXReg *d, MMXReg *s)
1192{
1193 d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status));
1194 d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status));
1195}
1196
1197void helper_pfacc(MMXReg *d, MMXReg *s)
1198{
1199 MMXReg r;
1200 r.MMX_S(0) = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1201 r.MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1202 *d = r;
1203}
1204
1205void helper_pfadd(MMXReg *d, MMXReg *s)
1206{
1207 d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1208 d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1209}
1210
1211void helper_pfcmpeq(MMXReg *d, MMXReg *s)
1212{
1213 d->MMX_L(0) = float32_eq(d->MMX_S(0), s->MMX_S(0), &env->mmx_status) ? -1 : 0;
1214 d->MMX_L(1) = float32_eq(d->MMX_S(1), s->MMX_S(1), &env->mmx_status) ? -1 : 0;
1215}
1216
1217void helper_pfcmpge(MMXReg *d, MMXReg *s)
1218{
1219 d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0), &env->mmx_status) ? -1 : 0;
1220 d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1), &env->mmx_status) ? -1 : 0;
1221}
1222
1223void helper_pfcmpgt(MMXReg *d, MMXReg *s)
1224{
1225 d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status) ? -1 : 0;
1226 d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status) ? -1 : 0;
1227}
1228
1229void helper_pfmax(MMXReg *d, MMXReg *s)
1230{
1231 if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status))
1232 d->MMX_S(0) = s->MMX_S(0);
1233 if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status))
1234 d->MMX_S(1) = s->MMX_S(1);
1235}
1236
1237void helper_pfmin(MMXReg *d, MMXReg *s)
1238{
1239 if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status))
1240 d->MMX_S(0) = s->MMX_S(0);
1241 if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status))
1242 d->MMX_S(1) = s->MMX_S(1);
1243}
1244
1245void helper_pfmul(MMXReg *d, MMXReg *s)
1246{
1247 d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1248 d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1249}
1250
1251void helper_pfnacc(MMXReg *d, MMXReg *s)
1252{
1253 MMXReg r;
1254 r.MMX_S(0) = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1255 r.MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1256 *d = r;
1257}
1258
1259void helper_pfpnacc(MMXReg *d, MMXReg *s)
1260{
1261 MMXReg r;
1262 r.MMX_S(0) = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1263 r.MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1264 *d = r;
1265}
1266
1267void helper_pfrcp(MMXReg *d, MMXReg *s)
1268{
1269 d->MMX_S(0) = approx_rcp(s->MMX_S(0));
1270 d->MMX_S(1) = d->MMX_S(0);
1271}
1272
1273void helper_pfrsqrt(MMXReg *d, MMXReg *s)
1274{
1275 d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1276 d->MMX_S(1) = approx_rsqrt(d->MMX_S(1));
1277 d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1278 d->MMX_L(0) = d->MMX_L(1);
1279}
1280
1281void helper_pfsub(MMXReg *d, MMXReg *s)
1282{
1283 d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1284 d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1285}
1286
1287void helper_pfsubr(MMXReg *d, MMXReg *s)
1288{
1289 d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1290 d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1291}
1292
1293void helper_pswapd(MMXReg *d, MMXReg *s)
1294{
1295 MMXReg r;
1296 r.MMX_L(0) = s->MMX_L(1);
1297 r.MMX_L(1) = s->MMX_L(0);
1298 *d = r;
1299}
1300#endif
1301
1302/* SSSE3 op helpers */
1303void glue(helper_pshufb, SUFFIX) (Reg *d, Reg *s)
1304{
1305 int i;
1306 Reg r;
1307
1308 for (i = 0; i < (8 << SHIFT); i++)
1309 r.B(i) = (s->B(i) & 0x80) ? 0 : (d->B(s->B(i) & ((8 << SHIFT) - 1)));
1310
1311 *d = r;
1312}
1313
1314void glue(helper_phaddw, SUFFIX) (Reg *d, Reg *s)
1315{
1316 d->W(0) = (int16_t)d->W(0) + (int16_t)d->W(1);
1317 d->W(1) = (int16_t)d->W(2) + (int16_t)d->W(3);
1318 XMM_ONLY(d->W(2) = (int16_t)d->W(4) + (int16_t)d->W(5));
1319 XMM_ONLY(d->W(3) = (int16_t)d->W(6) + (int16_t)d->W(7));
1320 d->W((2 << SHIFT) + 0) = (int16_t)s->W(0) + (int16_t)s->W(1);
1321 d->W((2 << SHIFT) + 1) = (int16_t)s->W(2) + (int16_t)s->W(3);
1322 XMM_ONLY(d->W(6) = (int16_t)s->W(4) + (int16_t)s->W(5));
1323 XMM_ONLY(d->W(7) = (int16_t)s->W(6) + (int16_t)s->W(7));
1324}
1325
1326void glue(helper_phaddd, SUFFIX) (Reg *d, Reg *s)
1327{
1328 d->L(0) = (int32_t)d->L(0) + (int32_t)d->L(1);
1329 XMM_ONLY(d->L(1) = (int32_t)d->L(2) + (int32_t)d->L(3));
1330 d->L((1 << SHIFT) + 0) = (int32_t)s->L(0) + (int32_t)s->L(1);
1331 XMM_ONLY(d->L(3) = (int32_t)s->L(2) + (int32_t)s->L(3));
1332}
1333
1334void glue(helper_phaddsw, SUFFIX) (Reg *d, Reg *s)
1335{
1336 d->W(0) = satsw((int16_t)d->W(0) + (int16_t)d->W(1));
1337 d->W(1) = satsw((int16_t)d->W(2) + (int16_t)d->W(3));
1338 XMM_ONLY(d->W(2) = satsw((int16_t)d->W(4) + (int16_t)d->W(5)));
1339 XMM_ONLY(d->W(3) = satsw((int16_t)d->W(6) + (int16_t)d->W(7)));
1340 d->W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) + (int16_t)s->W(1));
1341 d->W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) + (int16_t)s->W(3));
1342 XMM_ONLY(d->W(6) = satsw((int16_t)s->W(4) + (int16_t)s->W(5)));
1343 XMM_ONLY(d->W(7) = satsw((int16_t)s->W(6) + (int16_t)s->W(7)));
1344}
1345
1346void glue(helper_pmaddubsw, SUFFIX) (Reg *d, Reg *s)
1347{
1348 d->W(0) = satsw((int8_t)s->B( 0) * (uint8_t)d->B( 0) +
1349 (int8_t)s->B( 1) * (uint8_t)d->B( 1));
1350 d->W(1) = satsw((int8_t)s->B( 2) * (uint8_t)d->B( 2) +
1351 (int8_t)s->B( 3) * (uint8_t)d->B( 3));
1352 d->W(2) = satsw((int8_t)s->B( 4) * (uint8_t)d->B( 4) +
1353 (int8_t)s->B( 5) * (uint8_t)d->B( 5));
1354 d->W(3) = satsw((int8_t)s->B( 6) * (uint8_t)d->B( 6) +
1355 (int8_t)s->B( 7) * (uint8_t)d->B( 7));
1356#if SHIFT == 1
1357 d->W(4) = satsw((int8_t)s->B( 8) * (uint8_t)d->B( 8) +
1358 (int8_t)s->B( 9) * (uint8_t)d->B( 9));
1359 d->W(5) = satsw((int8_t)s->B(10) * (uint8_t)d->B(10) +
1360 (int8_t)s->B(11) * (uint8_t)d->B(11));
1361 d->W(6) = satsw((int8_t)s->B(12) * (uint8_t)d->B(12) +
1362 (int8_t)s->B(13) * (uint8_t)d->B(13));
1363 d->W(7) = satsw((int8_t)s->B(14) * (uint8_t)d->B(14) +
1364 (int8_t)s->B(15) * (uint8_t)d->B(15));
1365#endif
1366}
1367
1368void glue(helper_phsubw, SUFFIX) (Reg *d, Reg *s)
1369{
1370 d->W(0) = (int16_t)d->W(0) - (int16_t)d->W(1);
1371 d->W(1) = (int16_t)d->W(2) - (int16_t)d->W(3);
1372 XMM_ONLY(d->W(2) = (int16_t)d->W(4) - (int16_t)d->W(5));
1373 XMM_ONLY(d->W(3) = (int16_t)d->W(6) - (int16_t)d->W(7));
1374 d->W((2 << SHIFT) + 0) = (int16_t)s->W(0) - (int16_t)s->W(1);
1375 d->W((2 << SHIFT) + 1) = (int16_t)s->W(2) - (int16_t)s->W(3);
1376 XMM_ONLY(d->W(6) = (int16_t)s->W(4) - (int16_t)s->W(5));
1377 XMM_ONLY(d->W(7) = (int16_t)s->W(6) - (int16_t)s->W(7));
1378}
1379
1380void glue(helper_phsubd, SUFFIX) (Reg *d, Reg *s)
1381{
1382 d->L(0) = (int32_t)d->L(0) - (int32_t)d->L(1);
1383 XMM_ONLY(d->L(1) = (int32_t)d->L(2) - (int32_t)d->L(3));
1384 d->L((1 << SHIFT) + 0) = (int32_t)s->L(0) - (int32_t)s->L(1);
1385 XMM_ONLY(d->L(3) = (int32_t)s->L(2) - (int32_t)s->L(3));
1386}
1387
1388void glue(helper_phsubsw, SUFFIX) (Reg *d, Reg *s)
1389{
1390 d->W(0) = satsw((int16_t)d->W(0) - (int16_t)d->W(1));
1391 d->W(1) = satsw((int16_t)d->W(2) - (int16_t)d->W(3));
1392 XMM_ONLY(d->W(2) = satsw((int16_t)d->W(4) - (int16_t)d->W(5)));
1393 XMM_ONLY(d->W(3) = satsw((int16_t)d->W(6) - (int16_t)d->W(7)));
1394 d->W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) - (int16_t)s->W(1));
1395 d->W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) - (int16_t)s->W(3));
1396 XMM_ONLY(d->W(6) = satsw((int16_t)s->W(4) - (int16_t)s->W(5)));
1397 XMM_ONLY(d->W(7) = satsw((int16_t)s->W(6) - (int16_t)s->W(7)));
1398}
1399
1400#define FABSB(_, x) x > INT8_MAX ? -(int8_t ) x : x
1401#define FABSW(_, x) x > INT16_MAX ? -(int16_t) x : x
1402#define FABSL(_, x) x > INT32_MAX ? -(int32_t) x : x
1403SSE_HELPER_B(helper_pabsb, FABSB)
1404SSE_HELPER_W(helper_pabsw, FABSW)
1405SSE_HELPER_L(helper_pabsd, FABSL)
1406
1407#define FMULHRSW(d, s) ((int16_t) d * (int16_t) s + 0x4000) >> 15
1408SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1409
1410#define FSIGNB(d, s) s <= INT8_MAX ? s ? d : 0 : -(int8_t ) d
1411#define FSIGNW(d, s) s <= INT16_MAX ? s ? d : 0 : -(int16_t) d
1412#define FSIGNL(d, s) s <= INT32_MAX ? s ? d : 0 : -(int32_t) d
1413SSE_HELPER_B(helper_psignb, FSIGNB)
1414SSE_HELPER_W(helper_psignw, FSIGNW)
1415SSE_HELPER_L(helper_psignd, FSIGNL)
1416
1417void glue(helper_palignr, SUFFIX) (Reg *d, Reg *s, int32_t shift)
1418{
1419 Reg r;
1420
1421 /* XXX could be checked during translation */
1422 if (shift >= (16 << SHIFT)) {
1423 r.Q(0) = 0;
1424 XMM_ONLY(r.Q(1) = 0);
1425 } else {
1426 shift <<= 3;
1427#define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1428#if SHIFT == 0
1429 r.Q(0) = SHR(s->Q(0), shift - 0) |
1430 SHR(d->Q(0), shift - 64);
1431#else
1432 r.Q(0) = SHR(s->Q(0), shift - 0) |
1433 SHR(s->Q(1), shift - 64) |
1434 SHR(d->Q(0), shift - 128) |
1435 SHR(d->Q(1), shift - 192);
1436 r.Q(1) = SHR(s->Q(0), shift + 64) |
1437 SHR(s->Q(1), shift - 0) |
1438 SHR(d->Q(0), shift - 64) |
1439 SHR(d->Q(1), shift - 128);
1440#endif
1441#undef SHR
1442 }
1443
1444 *d = r;
1445}
1446
1447#define XMM0 env->xmm_regs[0]
1448
1449#if SHIFT == 1
1450#define SSE_HELPER_V(name, elem, num, F)\
1451void glue(name, SUFFIX) (Reg *d, Reg *s)\
1452{\
1453 d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0));\
1454 d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1));\
1455 if (num > 2) {\
1456 d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2));\
1457 d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3));\
1458 if (num > 4) {\
1459 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4));\
1460 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5));\
1461 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6));\
1462 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7));\
1463 if (num > 8) {\
1464 d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8));\
1465 d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9));\
1466 d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10));\
1467 d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11));\
1468 d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12));\
1469 d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13));\
1470 d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14));\
1471 d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15));\
1472 }\
1473 }\
1474 }\
1475}
1476
1477#define SSE_HELPER_I(name, elem, num, F)\
1478void glue(name, SUFFIX) (Reg *d, Reg *s, uint32_t imm)\
1479{\
1480 d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1));\
1481 d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1));\
1482 if (num > 2) {\
1483 d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1));\
1484 d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1));\
1485 if (num > 4) {\
1486 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1));\
1487 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1));\
1488 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1));\
1489 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1));\
1490 if (num > 8) {\
1491 d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1));\
1492 d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1));\
1493 d->elem(10) = F(d->elem(10), s->elem(10), ((imm >> 10) & 1));\
1494 d->elem(11) = F(d->elem(11), s->elem(11), ((imm >> 11) & 1));\
1495 d->elem(12) = F(d->elem(12), s->elem(12), ((imm >> 12) & 1));\
1496 d->elem(13) = F(d->elem(13), s->elem(13), ((imm >> 13) & 1));\
1497 d->elem(14) = F(d->elem(14), s->elem(14), ((imm >> 14) & 1));\
1498 d->elem(15) = F(d->elem(15), s->elem(15), ((imm >> 15) & 1));\
1499 }\
1500 }\
1501 }\
1502}
1503
1504/* SSE4.1 op helpers */
1505#define FBLENDVB(d, s, m) (m & 0x80) ? s : d
1506#define FBLENDVPS(d, s, m) (m & 0x80000000) ? s : d
1507#define FBLENDVPD(d, s, m) (m & 0x8000000000000000LL) ? s : d
1508SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB)
1509SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS)
1510SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD)
1511
1512void glue(helper_ptest, SUFFIX) (Reg *d, Reg *s)
1513{
1514 uint64_t zf = (s->Q(0) & d->Q(0)) | (s->Q(1) & d->Q(1));
1515 uint64_t cf = (s->Q(0) & ~d->Q(0)) | (s->Q(1) & ~d->Q(1));
1516
1517 CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1518}
1519
1520#define SSE_HELPER_F(name, elem, num, F)\
1521void glue(name, SUFFIX) (Reg *d, Reg *s)\
1522{\
1523 d->elem(0) = F(0);\
1524 d->elem(1) = F(1);\
1525 if (num > 2) {\
1526 d->elem(2) = F(2);\
1527 d->elem(3) = F(3);\
1528 if (num > 4) {\
1529 d->elem(4) = F(4);\
1530 d->elem(5) = F(5);\
1531 d->elem(6) = F(6);\
1532 d->elem(7) = F(7);\
1533 }\
1534 }\
1535}
1536
1537SSE_HELPER_F(helper_pmovsxbw, W, 8, (int8_t) s->B)
1538SSE_HELPER_F(helper_pmovsxbd, L, 4, (int8_t) s->B)
1539SSE_HELPER_F(helper_pmovsxbq, Q, 2, (int8_t) s->B)
1540SSE_HELPER_F(helper_pmovsxwd, L, 4, (int16_t) s->W)
1541SSE_HELPER_F(helper_pmovsxwq, Q, 2, (int16_t) s->W)
1542SSE_HELPER_F(helper_pmovsxdq, Q, 2, (int32_t) s->L)
1543SSE_HELPER_F(helper_pmovzxbw, W, 8, s->B)
1544SSE_HELPER_F(helper_pmovzxbd, L, 4, s->B)
1545SSE_HELPER_F(helper_pmovzxbq, Q, 2, s->B)
1546SSE_HELPER_F(helper_pmovzxwd, L, 4, s->W)
1547SSE_HELPER_F(helper_pmovzxwq, Q, 2, s->W)
1548SSE_HELPER_F(helper_pmovzxdq, Q, 2, s->L)
1549
1550void glue(helper_pmuldq, SUFFIX) (Reg *d, Reg *s)
1551{
1552 d->Q(0) = (int64_t) (int32_t) d->L(0) * (int32_t) s->L(0);
1553 d->Q(1) = (int64_t) (int32_t) d->L(2) * (int32_t) s->L(2);
1554}
1555
1556#define FCMPEQQ(d, s) d == s ? -1 : 0
1557SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1558
1559void glue(helper_packusdw, SUFFIX) (Reg *d, Reg *s)
1560{
1561 d->W(0) = satuw((int32_t) d->L(0));
1562 d->W(1) = satuw((int32_t) d->L(1));
1563 d->W(2) = satuw((int32_t) d->L(2));
1564 d->W(3) = satuw((int32_t) d->L(3));
1565 d->W(4) = satuw((int32_t) s->L(0));
1566 d->W(5) = satuw((int32_t) s->L(1));
1567 d->W(6) = satuw((int32_t) s->L(2));
1568 d->W(7) = satuw((int32_t) s->L(3));
1569}
1570
1571#define FMINSB(d, s) MIN((int8_t) d, (int8_t) s)
1572#define FMINSD(d, s) MIN((int32_t) d, (int32_t) s)
1573#define FMAXSB(d, s) MAX((int8_t) d, (int8_t) s)
1574#define FMAXSD(d, s) MAX((int32_t) d, (int32_t) s)
1575SSE_HELPER_B(helper_pminsb, FMINSB)
1576SSE_HELPER_L(helper_pminsd, FMINSD)
1577SSE_HELPER_W(helper_pminuw, MIN)
1578SSE_HELPER_L(helper_pminud, MIN)
1579SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1580SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1581SSE_HELPER_W(helper_pmaxuw, MAX)
1582SSE_HELPER_L(helper_pmaxud, MAX)
1583
1584#define FMULLD(d, s) (int32_t) d * (int32_t) s
1585SSE_HELPER_L(helper_pmulld, FMULLD)
1586
1587void glue(helper_phminposuw, SUFFIX) (Reg *d, Reg *s)
1588{
1589 int idx = 0;
1590
1591 if (s->W(1) < s->W(idx))
1592 idx = 1;
1593 if (s->W(2) < s->W(idx))
1594 idx = 2;
1595 if (s->W(3) < s->W(idx))
1596 idx = 3;
1597 if (s->W(4) < s->W(idx))
1598 idx = 4;
1599 if (s->W(5) < s->W(idx))
1600 idx = 5;
1601 if (s->W(6) < s->W(idx))
1602 idx = 6;
1603 if (s->W(7) < s->W(idx))
1604 idx = 7;
1605
1606 d->Q(1) = 0;
1607 d->L(1) = 0;
1608 d->W(1) = idx;
1609 d->W(0) = s->W(idx);
1610}
1611
1612void glue(helper_roundps, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1613{
1614 signed char prev_rounding_mode;
1615
1616 prev_rounding_mode = env->sse_status.float_rounding_mode;
1617 if (!(mode & (1 << 2)))
1618 switch (mode & 3) {
1619 case 0:
1620 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1621 break;
1622 case 1:
1623 set_float_rounding_mode(float_round_down, &env->sse_status);
1624 break;
1625 case 2:
1626 set_float_rounding_mode(float_round_up, &env->sse_status);
1627 break;
1628 case 3:
1629 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1630 break;
1631 }
1632
1633 d->L(0) = float64_round_to_int(s->L(0), &env->sse_status);
1634 d->L(1) = float64_round_to_int(s->L(1), &env->sse_status);
1635 d->L(2) = float64_round_to_int(s->L(2), &env->sse_status);
1636 d->L(3) = float64_round_to_int(s->L(3), &env->sse_status);
1637
1638#if 0 /* TODO */
1639 if (mode & (1 << 3))
1640 set_float_exception_flags(
1641 get_float_exception_flags(&env->sse_status) &
1642 ~float_flag_inexact,
1643 &env->sse_status);
1644#endif
1645 env->sse_status.float_rounding_mode = prev_rounding_mode;
1646}
1647
1648void glue(helper_roundpd, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1649{
1650 signed char prev_rounding_mode;
1651
1652 prev_rounding_mode = env->sse_status.float_rounding_mode;
1653 if (!(mode & (1 << 2)))
1654 switch (mode & 3) {
1655 case 0:
1656 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1657 break;
1658 case 1:
1659 set_float_rounding_mode(float_round_down, &env->sse_status);
1660 break;
1661 case 2:
1662 set_float_rounding_mode(float_round_up, &env->sse_status);
1663 break;
1664 case 3:
1665 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1666 break;
1667 }
1668
1669 d->Q(0) = float64_round_to_int(s->Q(0), &env->sse_status);
1670 d->Q(1) = float64_round_to_int(s->Q(1), &env->sse_status);
1671
1672#if 0 /* TODO */
1673 if (mode & (1 << 3))
1674 set_float_exception_flags(
1675 get_float_exception_flags(&env->sse_status) &
1676 ~float_flag_inexact,
1677 &env->sse_status);
1678#endif
1679 env->sse_status.float_rounding_mode = prev_rounding_mode;
1680}
1681
1682void glue(helper_roundss, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1683{
1684 signed char prev_rounding_mode;
1685
1686 prev_rounding_mode = env->sse_status.float_rounding_mode;
1687 if (!(mode & (1 << 2)))
1688 switch (mode & 3) {
1689 case 0:
1690 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1691 break;
1692 case 1:
1693 set_float_rounding_mode(float_round_down, &env->sse_status);
1694 break;
1695 case 2:
1696 set_float_rounding_mode(float_round_up, &env->sse_status);
1697 break;
1698 case 3:
1699 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1700 break;
1701 }
1702
1703 d->L(0) = float64_round_to_int(s->L(0), &env->sse_status);
1704
1705#if 0 /* TODO */
1706 if (mode & (1 << 3))
1707 set_float_exception_flags(
1708 get_float_exception_flags(&env->sse_status) &
1709 ~float_flag_inexact,
1710 &env->sse_status);
1711#endif
1712 env->sse_status.float_rounding_mode = prev_rounding_mode;
1713}
1714
1715void glue(helper_roundsd, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1716{
1717 signed char prev_rounding_mode;
1718
1719 prev_rounding_mode = env->sse_status.float_rounding_mode;
1720 if (!(mode & (1 << 2)))
1721 switch (mode & 3) {
1722 case 0:
1723 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1724 break;
1725 case 1:
1726 set_float_rounding_mode(float_round_down, &env->sse_status);
1727 break;
1728 case 2:
1729 set_float_rounding_mode(float_round_up, &env->sse_status);
1730 break;
1731 case 3:
1732 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1733 break;
1734 }
1735
1736 d->Q(0) = float64_round_to_int(s->Q(0), &env->sse_status);
1737
1738#if 0 /* TODO */
1739 if (mode & (1 << 3))
1740 set_float_exception_flags(
1741 get_float_exception_flags(&env->sse_status) &
1742 ~float_flag_inexact,
1743 &env->sse_status);
1744#endif
1745 env->sse_status.float_rounding_mode = prev_rounding_mode;
1746}
1747
1748#define FBLENDP(d, s, m) m ? s : d
1749SSE_HELPER_I(helper_blendps, L, 4, FBLENDP)
1750SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP)
1751SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)
1752
1753void glue(helper_dpps, SUFFIX) (Reg *d, Reg *s, uint32_t mask)
1754{
1755 float32 iresult = 0 /*float32_zero*/;
1756
1757 if (mask & (1 << 4))
1758 iresult = float32_add(iresult,
1759 float32_mul(d->L(0), s->L(0), &env->sse_status),
1760 &env->sse_status);
1761 if (mask & (1 << 5))
1762 iresult = float32_add(iresult,
1763 float32_mul(d->L(1), s->L(1), &env->sse_status),
1764 &env->sse_status);
1765 if (mask & (1 << 6))
1766 iresult = float32_add(iresult,
1767 float32_mul(d->L(2), s->L(2), &env->sse_status),
1768 &env->sse_status);
1769 if (mask & (1 << 7))
1770 iresult = float32_add(iresult,
1771 float32_mul(d->L(3), s->L(3), &env->sse_status),
1772 &env->sse_status);
1773 d->L(0) = (mask & (1 << 0)) ? iresult : 0 /*float32_zero*/;
1774 d->L(1) = (mask & (1 << 1)) ? iresult : 0 /*float32_zero*/;
1775 d->L(2) = (mask & (1 << 2)) ? iresult : 0 /*float32_zero*/;
1776 d->L(3) = (mask & (1 << 3)) ? iresult : 0 /*float32_zero*/;
1777}
1778
1779void glue(helper_dppd, SUFFIX) (Reg *d, Reg *s, uint32_t mask)
1780{
1781 float64 iresult = 0 /*float64_zero*/;
1782
1783 if (mask & (1 << 4))
1784 iresult = float64_add(iresult,
1785 float64_mul(d->Q(0), s->Q(0), &env->sse_status),
1786 &env->sse_status);
1787 if (mask & (1 << 5))
1788 iresult = float64_add(iresult,
1789 float64_mul(d->Q(1), s->Q(1), &env->sse_status),
1790 &env->sse_status);
1791 d->Q(0) = (mask & (1 << 0)) ? iresult : 0 /*float64_zero*/;
1792 d->Q(1) = (mask & (1 << 1)) ? iresult : 0 /*float64_zero*/;
1793}
1794
1795void glue(helper_mpsadbw, SUFFIX) (Reg *d, Reg *s, uint32_t offset)
1796{
1797 int s0 = (offset & 3) << 2;
1798 int d0 = (offset & 4) << 0;
1799 int i;
1800 Reg r;
1801
1802 for (i = 0; i < 8; i++, d0++) {
1803 r.W(i) = 0;
1804 r.W(i) += abs1(d->B(d0 + 0) - s->B(s0 + 0));
1805 r.W(i) += abs1(d->B(d0 + 1) - s->B(s0 + 1));
1806 r.W(i) += abs1(d->B(d0 + 2) - s->B(s0 + 2));
1807 r.W(i) += abs1(d->B(d0 + 3) - s->B(s0 + 3));
1808 }
1809
1810 *d = r;
1811}
1812
1813/* SSE4.2 op helpers */
1814/* it's unclear whether signed or unsigned */
1815#define FCMPGTQ(d, s) d > s ? -1 : 0
1816SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1817
1818#ifndef VBOX
1819static inline int pcmp_elen(int reg, uint32_t ctrl)
1820#else /* VBOX */
1821DECLINLINE(int) pcmp_elen(int reg, uint32_t ctrl)
1822#endif /* VBOX */
1823{
1824 int val;
1825
1826 /* Presence of REX.W is indicated by a bit higher than 7 set */
1827 if (ctrl >> 8)
1828 val = abs1((int64_t) env->regs[reg]);
1829 else
1830 val = abs1((int32_t) env->regs[reg]);
1831
1832 if (ctrl & 1) {
1833 if (val > 8)
1834 return 8;
1835 } else
1836 if (val > 16)
1837 return 16;
1838
1839 return val;
1840}
1841
1842#ifndef VBOX
1843static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
1844#else /* VBOX */
1845DECLINLINE(int) pcmp_ilen(Reg *r, uint8_t ctrl)
1846#endif /* VBOX */
1847{
1848 int val = 0;
1849
1850 if (ctrl & 1) {
1851 while (val < 8 && r->W(val))
1852 val++;
1853 } else
1854 while (val < 16 && r->B(val))
1855 val++;
1856
1857 return val;
1858}
1859
1860#ifndef VBOX
1861static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
1862#else /* VBOX */
1863DECLINLINE(int) pcmp_val(Reg *r, uint8_t ctrl, int i)
1864#endif /* VBOX */
1865{
1866 switch ((ctrl >> 0) & 3) {
1867 case 0:
1868 return r->B(i);
1869 case 1:
1870 return r->W(i);
1871 case 2:
1872 return (int8_t) r->B(i);
1873 case 3:
1874 default:
1875 return (int16_t) r->W(i);
1876 }
1877}
1878
1879#ifndef VBOX
1880static inline unsigned pcmpxstrx(Reg *d, Reg *s,
1881#else /* VBOX */
1882DECLINLINE(unsigned) pcmpxstrx(Reg *d, Reg *s,
1883#endif /* VBOX */
1884 int8_t ctrl, int valids, int validd)
1885{
1886 unsigned int res = 0;
1887 int v;
1888 int j, i;
1889 int upper = (ctrl & 1) ? 7 : 15;
1890
1891 valids--;
1892 validd--;
1893
1894 CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
1895
1896 switch ((ctrl >> 2) & 3) {
1897 case 0:
1898 for (j = valids; j >= 0; j--) {
1899 res <<= 1;
1900 v = pcmp_val(s, ctrl, j);
1901 for (i = validd; i >= 0; i--)
1902 res |= (v == pcmp_val(d, ctrl, i));
1903 }
1904 break;
1905 case 1:
1906 for (j = valids; j >= 0; j--) {
1907 res <<= 1;
1908 v = pcmp_val(s, ctrl, j);
1909 for (i = ((validd - 1) | 1); i >= 0; i -= 2)
1910 res |= (pcmp_val(d, ctrl, i - 0) <= v &&
1911 pcmp_val(d, ctrl, i - 1) >= v);
1912 }
1913 break;
1914 case 2:
1915 res = (2 << (upper - MAX(valids, validd))) - 1;
1916 res <<= MAX(valids, validd) - MIN(valids, validd);
1917 for (i = MIN(valids, validd); i >= 0; i--) {
1918 res <<= 1;
1919 v = pcmp_val(s, ctrl, i);
1920 res |= (v == pcmp_val(d, ctrl, i));
1921 }
1922 break;
1923 case 3:
1924 for (j = valids - validd; j >= 0; j--) {
1925 res <<= 1;
1926 res |= 1;
1927 for (i = MIN(upper - j, validd); i >= 0; i--)
1928 res &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
1929 }
1930 break;
1931 }
1932
1933 switch ((ctrl >> 4) & 3) {
1934 case 1:
1935 res ^= (2 << upper) - 1;
1936 break;
1937 case 3:
1938 res ^= (2 << valids) - 1;
1939 break;
1940 }
1941
1942 if (res)
1943 CC_SRC |= CC_C;
1944 if (res & 1)
1945 CC_SRC |= CC_O;
1946
1947 return res;
1948}
1949
1950#ifndef VBOX
1951static inline int rffs1(unsigned int val)
1952#else /* VBOX */
1953DECLINLINE(int) rffs1(unsigned int val)
1954#endif /* VBOX */
1955{
1956 int ret = 1, hi;
1957
1958 for (hi = sizeof(val) * 4; hi; hi /= 2)
1959 if (val >> hi) {
1960 val >>= hi;
1961 ret += hi;
1962 }
1963
1964 return ret;
1965}
1966
1967#ifndef VBOX
1968static inline int ffs1(unsigned int val)
1969#else /* VBOX */
1970DECLINLINE(int) ffs1(unsigned int val)
1971#endif /* VBOX */
1972{
1973 int ret = 1, hi;
1974
1975 for (hi = sizeof(val) * 4; hi; hi /= 2)
1976 if (val << hi) {
1977 val <<= hi;
1978 ret += hi;
1979 }
1980
1981 return ret;
1982}
1983
1984void glue(helper_pcmpestri, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
1985{
1986 unsigned int res = pcmpxstrx(d, s, ctrl,
1987 pcmp_elen(R_EDX, ctrl),
1988 pcmp_elen(R_EAX, ctrl));
1989
1990 if (res)
1991#ifndef VBOX
1992 env->regs[R_ECX] = ((ctrl & (1 << 6)) ? rffs1 : ffs1)(res) - 1;
1993#else
1994 env->regs[R_ECX] = ((ctrl & (1 << 6)) ? rffs1(res) : ffs1(res)) - 1;
1995#endif
1996 else
1997 env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
1998}
1999
2000void glue(helper_pcmpestrm, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
2001{
2002 int i;
2003 unsigned int res = pcmpxstrx(d, s, ctrl,
2004 pcmp_elen(R_EDX, ctrl),
2005 pcmp_elen(R_EAX, ctrl));
2006
2007 if ((ctrl >> 6) & 1) {
2008 if (ctrl & 1)
2009 for (i = 0; i <= 8; i--, res >>= 1)
2010 d->W(i) = (res & 1) ? ~0 : 0;
2011 else
2012 for (i = 0; i <= 16; i--, res >>= 1)
2013 d->B(i) = (res & 1) ? ~0 : 0;
2014 } else {
2015 d->Q(1) = 0;
2016 d->Q(0) = res;
2017 }
2018}
2019
2020void glue(helper_pcmpistri, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
2021{
2022 unsigned int res = pcmpxstrx(d, s, ctrl,
2023 pcmp_ilen(s, ctrl),
2024 pcmp_ilen(d, ctrl));
2025
2026 if (res)
2027 env->regs[R_ECX] = ((ctrl & (1 << 6)) ? rffs1 : ffs1)(res) - 1;
2028 else
2029 env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2030}
2031
2032void glue(helper_pcmpistrm, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
2033{
2034 int i;
2035 unsigned int res = pcmpxstrx(d, s, ctrl,
2036 pcmp_ilen(s, ctrl),
2037 pcmp_ilen(d, ctrl));
2038
2039 if ((ctrl >> 6) & 1) {
2040 if (ctrl & 1)
2041 for (i = 0; i <= 8; i--, res >>= 1)
2042 d->W(i) = (res & 1) ? ~0 : 0;
2043 else
2044 for (i = 0; i <= 16; i--, res >>= 1)
2045 d->B(i) = (res & 1) ? ~0 : 0;
2046 } else {
2047 d->Q(1) = 0;
2048 d->Q(0) = res;
2049 }
2050}
2051
2052#define CRCPOLY 0x1edc6f41
2053#define CRCPOLY_BITREV 0x82f63b78
2054target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2055{
2056 target_ulong crc = (msg & ((target_ulong) -1 >>
2057 (TARGET_LONG_BITS - len))) ^ crc1;
2058
2059 while (len--)
2060 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2061
2062 return crc;
2063}
2064
2065#define POPMASK(i) ((target_ulong) -1 / ((1LL << (1 << i)) + 1))
2066#define POPCOUNT(n, i) (n & POPMASK(i)) + ((n >> (1 << i)) & POPMASK(i))
2067target_ulong helper_popcnt(target_ulong n, uint32_t type)
2068{
2069 CC_SRC = n ? 0 : CC_Z;
2070
2071 n = POPCOUNT(n, 0);
2072 n = POPCOUNT(n, 1);
2073 n = POPCOUNT(n, 2);
2074 n = POPCOUNT(n, 3);
2075 if (type == 1)
2076 return n & 0xff;
2077
2078 n = POPCOUNT(n, 4);
2079#ifndef TARGET_X86_64
2080 return n;
2081#else
2082 if (type == 2)
2083 return n & 0xff;
2084
2085 return POPCOUNT(n, 5);
2086#endif
2087}
2088#endif
2089
2090#undef SHIFT
2091#undef XMM_ONLY
2092#undef Reg
2093#undef B
2094#undef W
2095#undef L
2096#undef Q
2097#undef SUFFIX
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette