VirtualBox

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

Last change on this file since 36125 was 36125, checked in by vboxsync, 14 years ago

recompiler: Removing traces of attempts at making the recompiler compile with the microsoft compiler. (untested)

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

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