piix3: use new vmstate infrastructure
[qemu/cris-port.git] / target-i386 / ops_sse.h
blob47a01d40f911c3a0bc5d72489a229fd1343a40f2
1 /*
2 * MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
4 * Copyright (c) 2005 Fabrice Bellard
5 * Copyright (c) 2008 Intel Corporation <andrew.zaborowski@intel.com>
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #if SHIFT == 0
21 #define Reg MMXReg
22 #define XMM_ONLY(...)
23 #define B(n) MMX_B(n)
24 #define W(n) MMX_W(n)
25 #define L(n) MMX_L(n)
26 #define Q(n) q
27 #define SUFFIX _mmx
28 #else
29 #define Reg XMMReg
30 #define XMM_ONLY(...) __VA_ARGS__
31 #define B(n) XMM_B(n)
32 #define W(n) XMM_W(n)
33 #define L(n) XMM_L(n)
34 #define Q(n) XMM_Q(n)
35 #define SUFFIX _xmm
36 #endif
38 void glue(helper_psrlw, SUFFIX)(Reg *d, Reg *s)
40 int shift;
42 if (s->Q(0) > 15) {
43 d->Q(0) = 0;
44 #if SHIFT == 1
45 d->Q(1) = 0;
46 #endif
47 } else {
48 shift = s->B(0);
49 d->W(0) >>= shift;
50 d->W(1) >>= shift;
51 d->W(2) >>= shift;
52 d->W(3) >>= shift;
53 #if SHIFT == 1
54 d->W(4) >>= shift;
55 d->W(5) >>= shift;
56 d->W(6) >>= shift;
57 d->W(7) >>= shift;
58 #endif
62 void glue(helper_psraw, SUFFIX)(Reg *d, Reg *s)
64 int shift;
66 if (s->Q(0) > 15) {
67 shift = 15;
68 } else {
69 shift = s->B(0);
71 d->W(0) = (int16_t)d->W(0) >> shift;
72 d->W(1) = (int16_t)d->W(1) >> shift;
73 d->W(2) = (int16_t)d->W(2) >> shift;
74 d->W(3) = (int16_t)d->W(3) >> shift;
75 #if SHIFT == 1
76 d->W(4) = (int16_t)d->W(4) >> shift;
77 d->W(5) = (int16_t)d->W(5) >> shift;
78 d->W(6) = (int16_t)d->W(6) >> shift;
79 d->W(7) = (int16_t)d->W(7) >> shift;
80 #endif
83 void glue(helper_psllw, SUFFIX)(Reg *d, Reg *s)
85 int shift;
87 if (s->Q(0) > 15) {
88 d->Q(0) = 0;
89 #if SHIFT == 1
90 d->Q(1) = 0;
91 #endif
92 } else {
93 shift = s->B(0);
94 d->W(0) <<= shift;
95 d->W(1) <<= shift;
96 d->W(2) <<= shift;
97 d->W(3) <<= shift;
98 #if SHIFT == 1
99 d->W(4) <<= shift;
100 d->W(5) <<= shift;
101 d->W(6) <<= shift;
102 d->W(7) <<= shift;
103 #endif
107 void glue(helper_psrld, SUFFIX)(Reg *d, Reg *s)
109 int shift;
111 if (s->Q(0) > 31) {
112 d->Q(0) = 0;
113 #if SHIFT == 1
114 d->Q(1) = 0;
115 #endif
116 } else {
117 shift = s->B(0);
118 d->L(0) >>= shift;
119 d->L(1) >>= shift;
120 #if SHIFT == 1
121 d->L(2) >>= shift;
122 d->L(3) >>= shift;
123 #endif
127 void glue(helper_psrad, SUFFIX)(Reg *d, Reg *s)
129 int shift;
131 if (s->Q(0) > 31) {
132 shift = 31;
133 } else {
134 shift = s->B(0);
136 d->L(0) = (int32_t)d->L(0) >> shift;
137 d->L(1) = (int32_t)d->L(1) >> shift;
138 #if SHIFT == 1
139 d->L(2) = (int32_t)d->L(2) >> shift;
140 d->L(3) = (int32_t)d->L(3) >> shift;
141 #endif
144 void glue(helper_pslld, SUFFIX)(Reg *d, Reg *s)
146 int shift;
148 if (s->Q(0) > 31) {
149 d->Q(0) = 0;
150 #if SHIFT == 1
151 d->Q(1) = 0;
152 #endif
153 } else {
154 shift = s->B(0);
155 d->L(0) <<= shift;
156 d->L(1) <<= shift;
157 #if SHIFT == 1
158 d->L(2) <<= shift;
159 d->L(3) <<= shift;
160 #endif
164 void glue(helper_psrlq, SUFFIX)(Reg *d, Reg *s)
166 int shift;
168 if (s->Q(0) > 63) {
169 d->Q(0) = 0;
170 #if SHIFT == 1
171 d->Q(1) = 0;
172 #endif
173 } else {
174 shift = s->B(0);
175 d->Q(0) >>= shift;
176 #if SHIFT == 1
177 d->Q(1) >>= shift;
178 #endif
182 void glue(helper_psllq, SUFFIX)(Reg *d, Reg *s)
184 int shift;
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
200 #if SHIFT == 1
201 void glue(helper_psrldq, SUFFIX)(Reg *d, Reg *s)
203 int shift, i;
205 shift = s->L(0);
206 if (shift > 16)
207 shift = 16;
208 for(i = 0; i < 16 - shift; i++)
209 d->B(i) = d->B(i + shift);
210 for(i = 16 - shift; i < 16; i++)
211 d->B(i) = 0;
214 void glue(helper_pslldq, SUFFIX)(Reg *d, Reg *s)
216 int shift, i;
218 shift = s->L(0);
219 if (shift > 16)
220 shift = 16;
221 for(i = 15; i >= shift; i--)
222 d->B(i) = d->B(i - shift);
223 for(i = 0; i < shift; i++)
224 d->B(i) = 0;
226 #endif
228 #define SSE_HELPER_B(name, F)\
229 void glue(name, SUFFIX) (Reg *d, Reg *s)\
231 d->B(0) = F(d->B(0), s->B(0));\
232 d->B(1) = F(d->B(1), s->B(1));\
233 d->B(2) = F(d->B(2), s->B(2));\
234 d->B(3) = F(d->B(3), s->B(3));\
235 d->B(4) = F(d->B(4), s->B(4));\
236 d->B(5) = F(d->B(5), s->B(5));\
237 d->B(6) = F(d->B(6), s->B(6));\
238 d->B(7) = F(d->B(7), s->B(7));\
239 XMM_ONLY(\
240 d->B(8) = F(d->B(8), s->B(8));\
241 d->B(9) = F(d->B(9), s->B(9));\
242 d->B(10) = F(d->B(10), s->B(10));\
243 d->B(11) = F(d->B(11), s->B(11));\
244 d->B(12) = F(d->B(12), s->B(12));\
245 d->B(13) = F(d->B(13), s->B(13));\
246 d->B(14) = F(d->B(14), s->B(14));\
247 d->B(15) = F(d->B(15), s->B(15));\
251 #define SSE_HELPER_W(name, F)\
252 void glue(name, SUFFIX) (Reg *d, Reg *s)\
254 d->W(0) = F(d->W(0), s->W(0));\
255 d->W(1) = F(d->W(1), s->W(1));\
256 d->W(2) = F(d->W(2), s->W(2));\
257 d->W(3) = F(d->W(3), s->W(3));\
258 XMM_ONLY(\
259 d->W(4) = F(d->W(4), s->W(4));\
260 d->W(5) = F(d->W(5), s->W(5));\
261 d->W(6) = F(d->W(6), s->W(6));\
262 d->W(7) = F(d->W(7), s->W(7));\
266 #define SSE_HELPER_L(name, F)\
267 void glue(name, SUFFIX) (Reg *d, Reg *s)\
269 d->L(0) = F(d->L(0), s->L(0));\
270 d->L(1) = F(d->L(1), s->L(1));\
271 XMM_ONLY(\
272 d->L(2) = F(d->L(2), s->L(2));\
273 d->L(3) = F(d->L(3), s->L(3));\
277 #define SSE_HELPER_Q(name, F)\
278 void glue(name, SUFFIX) (Reg *d, Reg *s)\
280 d->Q(0) = F(d->Q(0), s->Q(0));\
281 XMM_ONLY(\
282 d->Q(1) = F(d->Q(1), s->Q(1));\
286 #if SHIFT == 0
287 static inline int satub(int x)
289 if (x < 0)
290 return 0;
291 else if (x > 255)
292 return 255;
293 else
294 return x;
297 static inline int satuw(int x)
299 if (x < 0)
300 return 0;
301 else if (x > 65535)
302 return 65535;
303 else
304 return x;
307 static inline int satsb(int x)
309 if (x < -128)
310 return -128;
311 else if (x > 127)
312 return 127;
313 else
314 return x;
317 static inline int satsw(int x)
319 if (x < -32768)
320 return -32768;
321 else if (x > 32767)
322 return 32767;
323 else
324 return x;
327 #define FADD(a, b) ((a) + (b))
328 #define FADDUB(a, b) satub((a) + (b))
329 #define FADDUW(a, b) satuw((a) + (b))
330 #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
331 #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
333 #define FSUB(a, b) ((a) - (b))
334 #define FSUBUB(a, b) satub((a) - (b))
335 #define FSUBUW(a, b) satuw((a) - (b))
336 #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
337 #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
338 #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
339 #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
340 #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
341 #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
343 #define FAND(a, b) (a) & (b)
344 #define FANDN(a, b) ((~(a)) & (b))
345 #define FOR(a, b) (a) | (b)
346 #define FXOR(a, b) (a) ^ (b)
348 #define FCMPGTB(a, b) (int8_t)(a) > (int8_t)(b) ? -1 : 0
349 #define FCMPGTW(a, b) (int16_t)(a) > (int16_t)(b) ? -1 : 0
350 #define FCMPGTL(a, b) (int32_t)(a) > (int32_t)(b) ? -1 : 0
351 #define FCMPEQ(a, b) (a) == (b) ? -1 : 0
353 #define FMULLW(a, b) (a) * (b)
354 #define FMULHRW(a, b) ((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16
355 #define FMULHUW(a, b) (a) * (b) >> 16
356 #define FMULHW(a, b) (int16_t)(a) * (int16_t)(b) >> 16
358 #define FAVG(a, b) ((a) + (b) + 1) >> 1
359 #endif
361 SSE_HELPER_B(helper_paddb, FADD)
362 SSE_HELPER_W(helper_paddw, FADD)
363 SSE_HELPER_L(helper_paddl, FADD)
364 SSE_HELPER_Q(helper_paddq, FADD)
366 SSE_HELPER_B(helper_psubb, FSUB)
367 SSE_HELPER_W(helper_psubw, FSUB)
368 SSE_HELPER_L(helper_psubl, FSUB)
369 SSE_HELPER_Q(helper_psubq, FSUB)
371 SSE_HELPER_B(helper_paddusb, FADDUB)
372 SSE_HELPER_B(helper_paddsb, FADDSB)
373 SSE_HELPER_B(helper_psubusb, FSUBUB)
374 SSE_HELPER_B(helper_psubsb, FSUBSB)
376 SSE_HELPER_W(helper_paddusw, FADDUW)
377 SSE_HELPER_W(helper_paddsw, FADDSW)
378 SSE_HELPER_W(helper_psubusw, FSUBUW)
379 SSE_HELPER_W(helper_psubsw, FSUBSW)
381 SSE_HELPER_B(helper_pminub, FMINUB)
382 SSE_HELPER_B(helper_pmaxub, FMAXUB)
384 SSE_HELPER_W(helper_pminsw, FMINSW)
385 SSE_HELPER_W(helper_pmaxsw, FMAXSW)
387 SSE_HELPER_Q(helper_pand, FAND)
388 SSE_HELPER_Q(helper_pandn, FANDN)
389 SSE_HELPER_Q(helper_por, FOR)
390 SSE_HELPER_Q(helper_pxor, FXOR)
392 SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
393 SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
394 SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
396 SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
397 SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
398 SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
400 SSE_HELPER_W(helper_pmullw, FMULLW)
401 #if SHIFT == 0
402 SSE_HELPER_W(helper_pmulhrw, FMULHRW)
403 #endif
404 SSE_HELPER_W(helper_pmulhuw, FMULHUW)
405 SSE_HELPER_W(helper_pmulhw, FMULHW)
407 SSE_HELPER_B(helper_pavgb, FAVG)
408 SSE_HELPER_W(helper_pavgw, FAVG)
410 void glue(helper_pmuludq, SUFFIX) (Reg *d, Reg *s)
412 d->Q(0) = (uint64_t)s->L(0) * (uint64_t)d->L(0);
413 #if SHIFT == 1
414 d->Q(1) = (uint64_t)s->L(2) * (uint64_t)d->L(2);
415 #endif
418 void glue(helper_pmaddwd, SUFFIX) (Reg *d, Reg *s)
420 int i;
422 for(i = 0; i < (2 << SHIFT); i++) {
423 d->L(i) = (int16_t)s->W(2*i) * (int16_t)d->W(2*i) +
424 (int16_t)s->W(2*i+1) * (int16_t)d->W(2*i+1);
428 #if SHIFT == 0
429 static inline int abs1(int a)
431 if (a < 0)
432 return -a;
433 else
434 return a;
436 #endif
437 void glue(helper_psadbw, SUFFIX) (Reg *d, Reg *s)
439 unsigned int val;
441 val = 0;
442 val += abs1(d->B(0) - s->B(0));
443 val += abs1(d->B(1) - s->B(1));
444 val += abs1(d->B(2) - s->B(2));
445 val += abs1(d->B(3) - s->B(3));
446 val += abs1(d->B(4) - s->B(4));
447 val += abs1(d->B(5) - s->B(5));
448 val += abs1(d->B(6) - s->B(6));
449 val += abs1(d->B(7) - s->B(7));
450 d->Q(0) = val;
451 #if SHIFT == 1
452 val = 0;
453 val += abs1(d->B(8) - s->B(8));
454 val += abs1(d->B(9) - s->B(9));
455 val += abs1(d->B(10) - s->B(10));
456 val += abs1(d->B(11) - s->B(11));
457 val += abs1(d->B(12) - s->B(12));
458 val += abs1(d->B(13) - s->B(13));
459 val += abs1(d->B(14) - s->B(14));
460 val += abs1(d->B(15) - s->B(15));
461 d->Q(1) = val;
462 #endif
465 void glue(helper_maskmov, SUFFIX) (Reg *d, Reg *s, target_ulong a0)
467 int i;
468 for(i = 0; i < (8 << SHIFT); i++) {
469 if (s->B(i) & 0x80)
470 stb(a0 + i, d->B(i));
474 void glue(helper_movl_mm_T0, SUFFIX) (Reg *d, uint32_t val)
476 d->L(0) = val;
477 d->L(1) = 0;
478 #if SHIFT == 1
479 d->Q(1) = 0;
480 #endif
483 #ifdef TARGET_X86_64
484 void glue(helper_movq_mm_T0, SUFFIX) (Reg *d, uint64_t val)
486 d->Q(0) = val;
487 #if SHIFT == 1
488 d->Q(1) = 0;
489 #endif
491 #endif
493 #if SHIFT == 0
494 void glue(helper_pshufw, SUFFIX) (Reg *d, Reg *s, int order)
496 Reg r;
497 r.W(0) = s->W(order & 3);
498 r.W(1) = s->W((order >> 2) & 3);
499 r.W(2) = s->W((order >> 4) & 3);
500 r.W(3) = s->W((order >> 6) & 3);
501 *d = r;
503 #else
504 void helper_shufps(Reg *d, Reg *s, int order)
506 Reg r;
507 r.L(0) = d->L(order & 3);
508 r.L(1) = d->L((order >> 2) & 3);
509 r.L(2) = s->L((order >> 4) & 3);
510 r.L(3) = s->L((order >> 6) & 3);
511 *d = r;
514 void helper_shufpd(Reg *d, Reg *s, int order)
516 Reg r;
517 r.Q(0) = d->Q(order & 1);
518 r.Q(1) = s->Q((order >> 1) & 1);
519 *d = r;
522 void glue(helper_pshufd, SUFFIX) (Reg *d, Reg *s, int order)
524 Reg r;
525 r.L(0) = s->L(order & 3);
526 r.L(1) = s->L((order >> 2) & 3);
527 r.L(2) = s->L((order >> 4) & 3);
528 r.L(3) = s->L((order >> 6) & 3);
529 *d = r;
532 void glue(helper_pshuflw, SUFFIX) (Reg *d, Reg *s, int order)
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 r.Q(1) = s->Q(1);
540 *d = r;
543 void glue(helper_pshufhw, SUFFIX) (Reg *d, Reg *s, int order)
545 Reg r;
546 r.Q(0) = s->Q(0);
547 r.W(4) = s->W(4 + (order & 3));
548 r.W(5) = s->W(4 + ((order >> 2) & 3));
549 r.W(6) = s->W(4 + ((order >> 4) & 3));
550 r.W(7) = s->W(4 + ((order >> 6) & 3));
551 *d = r;
553 #endif
555 #if SHIFT == 1
556 /* FPU ops */
557 /* XXX: not accurate */
559 #define SSE_HELPER_S(name, F)\
560 void helper_ ## name ## ps (Reg *d, Reg *s)\
562 d->XMM_S(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
563 d->XMM_S(1) = F(32, d->XMM_S(1), s->XMM_S(1));\
564 d->XMM_S(2) = F(32, d->XMM_S(2), s->XMM_S(2));\
565 d->XMM_S(3) = F(32, d->XMM_S(3), s->XMM_S(3));\
568 void helper_ ## name ## ss (Reg *d, Reg *s)\
570 d->XMM_S(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
572 void helper_ ## name ## pd (Reg *d, Reg *s)\
574 d->XMM_D(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
575 d->XMM_D(1) = F(64, d->XMM_D(1), s->XMM_D(1));\
578 void helper_ ## name ## sd (Reg *d, Reg *s)\
580 d->XMM_D(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
583 #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
584 #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
585 #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
586 #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
587 #define FPU_MIN(size, a, b) (a) < (b) ? (a) : (b)
588 #define FPU_MAX(size, a, b) (a) > (b) ? (a) : (b)
589 #define FPU_SQRT(size, a, b) float ## size ## _sqrt(b, &env->sse_status)
591 SSE_HELPER_S(add, FPU_ADD)
592 SSE_HELPER_S(sub, FPU_SUB)
593 SSE_HELPER_S(mul, FPU_MUL)
594 SSE_HELPER_S(div, FPU_DIV)
595 SSE_HELPER_S(min, FPU_MIN)
596 SSE_HELPER_S(max, FPU_MAX)
597 SSE_HELPER_S(sqrt, FPU_SQRT)
600 /* float to float conversions */
601 void helper_cvtps2pd(Reg *d, Reg *s)
603 float32 s0, s1;
604 s0 = s->XMM_S(0);
605 s1 = s->XMM_S(1);
606 d->XMM_D(0) = float32_to_float64(s0, &env->sse_status);
607 d->XMM_D(1) = float32_to_float64(s1, &env->sse_status);
610 void helper_cvtpd2ps(Reg *d, Reg *s)
612 d->XMM_S(0) = float64_to_float32(s->XMM_D(0), &env->sse_status);
613 d->XMM_S(1) = float64_to_float32(s->XMM_D(1), &env->sse_status);
614 d->Q(1) = 0;
617 void helper_cvtss2sd(Reg *d, Reg *s)
619 d->XMM_D(0) = float32_to_float64(s->XMM_S(0), &env->sse_status);
622 void helper_cvtsd2ss(Reg *d, Reg *s)
624 d->XMM_S(0) = float64_to_float32(s->XMM_D(0), &env->sse_status);
627 /* integer to float */
628 void helper_cvtdq2ps(Reg *d, Reg *s)
630 d->XMM_S(0) = int32_to_float32(s->XMM_L(0), &env->sse_status);
631 d->XMM_S(1) = int32_to_float32(s->XMM_L(1), &env->sse_status);
632 d->XMM_S(2) = int32_to_float32(s->XMM_L(2), &env->sse_status);
633 d->XMM_S(3) = int32_to_float32(s->XMM_L(3), &env->sse_status);
636 void helper_cvtdq2pd(Reg *d, Reg *s)
638 int32_t l0, l1;
639 l0 = (int32_t)s->XMM_L(0);
640 l1 = (int32_t)s->XMM_L(1);
641 d->XMM_D(0) = int32_to_float64(l0, &env->sse_status);
642 d->XMM_D(1) = int32_to_float64(l1, &env->sse_status);
645 void helper_cvtpi2ps(XMMReg *d, MMXReg *s)
647 d->XMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
648 d->XMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
651 void helper_cvtpi2pd(XMMReg *d, MMXReg *s)
653 d->XMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
654 d->XMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
657 void helper_cvtsi2ss(XMMReg *d, uint32_t val)
659 d->XMM_S(0) = int32_to_float32(val, &env->sse_status);
662 void helper_cvtsi2sd(XMMReg *d, uint32_t val)
664 d->XMM_D(0) = int32_to_float64(val, &env->sse_status);
667 #ifdef TARGET_X86_64
668 void helper_cvtsq2ss(XMMReg *d, uint64_t val)
670 d->XMM_S(0) = int64_to_float32(val, &env->sse_status);
673 void helper_cvtsq2sd(XMMReg *d, uint64_t val)
675 d->XMM_D(0) = int64_to_float64(val, &env->sse_status);
677 #endif
679 /* float to integer */
680 void helper_cvtps2dq(XMMReg *d, XMMReg *s)
682 d->XMM_L(0) = float32_to_int32(s->XMM_S(0), &env->sse_status);
683 d->XMM_L(1) = float32_to_int32(s->XMM_S(1), &env->sse_status);
684 d->XMM_L(2) = float32_to_int32(s->XMM_S(2), &env->sse_status);
685 d->XMM_L(3) = float32_to_int32(s->XMM_S(3), &env->sse_status);
688 void helper_cvtpd2dq(XMMReg *d, XMMReg *s)
690 d->XMM_L(0) = float64_to_int32(s->XMM_D(0), &env->sse_status);
691 d->XMM_L(1) = float64_to_int32(s->XMM_D(1), &env->sse_status);
692 d->XMM_Q(1) = 0;
695 void helper_cvtps2pi(MMXReg *d, XMMReg *s)
697 d->MMX_L(0) = float32_to_int32(s->XMM_S(0), &env->sse_status);
698 d->MMX_L(1) = float32_to_int32(s->XMM_S(1), &env->sse_status);
701 void helper_cvtpd2pi(MMXReg *d, XMMReg *s)
703 d->MMX_L(0) = float64_to_int32(s->XMM_D(0), &env->sse_status);
704 d->MMX_L(1) = float64_to_int32(s->XMM_D(1), &env->sse_status);
707 int32_t helper_cvtss2si(XMMReg *s)
709 return float32_to_int32(s->XMM_S(0), &env->sse_status);
712 int32_t helper_cvtsd2si(XMMReg *s)
714 return float64_to_int32(s->XMM_D(0), &env->sse_status);
717 #ifdef TARGET_X86_64
718 int64_t helper_cvtss2sq(XMMReg *s)
720 return float32_to_int64(s->XMM_S(0), &env->sse_status);
723 int64_t helper_cvtsd2sq(XMMReg *s)
725 return float64_to_int64(s->XMM_D(0), &env->sse_status);
727 #endif
729 /* float to integer truncated */
730 void helper_cvttps2dq(XMMReg *d, XMMReg *s)
732 d->XMM_L(0) = float32_to_int32_round_to_zero(s->XMM_S(0), &env->sse_status);
733 d->XMM_L(1) = float32_to_int32_round_to_zero(s->XMM_S(1), &env->sse_status);
734 d->XMM_L(2) = float32_to_int32_round_to_zero(s->XMM_S(2), &env->sse_status);
735 d->XMM_L(3) = float32_to_int32_round_to_zero(s->XMM_S(3), &env->sse_status);
738 void helper_cvttpd2dq(XMMReg *d, XMMReg *s)
740 d->XMM_L(0) = float64_to_int32_round_to_zero(s->XMM_D(0), &env->sse_status);
741 d->XMM_L(1) = float64_to_int32_round_to_zero(s->XMM_D(1), &env->sse_status);
742 d->XMM_Q(1) = 0;
745 void helper_cvttps2pi(MMXReg *d, XMMReg *s)
747 d->MMX_L(0) = float32_to_int32_round_to_zero(s->XMM_S(0), &env->sse_status);
748 d->MMX_L(1) = float32_to_int32_round_to_zero(s->XMM_S(1), &env->sse_status);
751 void helper_cvttpd2pi(MMXReg *d, XMMReg *s)
753 d->MMX_L(0) = float64_to_int32_round_to_zero(s->XMM_D(0), &env->sse_status);
754 d->MMX_L(1) = float64_to_int32_round_to_zero(s->XMM_D(1), &env->sse_status);
757 int32_t helper_cvttss2si(XMMReg *s)
759 return float32_to_int32_round_to_zero(s->XMM_S(0), &env->sse_status);
762 int32_t helper_cvttsd2si(XMMReg *s)
764 return float64_to_int32_round_to_zero(s->XMM_D(0), &env->sse_status);
767 #ifdef TARGET_X86_64
768 int64_t helper_cvttss2sq(XMMReg *s)
770 return float32_to_int64_round_to_zero(s->XMM_S(0), &env->sse_status);
773 int64_t helper_cvttsd2sq(XMMReg *s)
775 return float64_to_int64_round_to_zero(s->XMM_D(0), &env->sse_status);
777 #endif
779 void helper_rsqrtps(XMMReg *d, XMMReg *s)
781 d->XMM_S(0) = approx_rsqrt(s->XMM_S(0));
782 d->XMM_S(1) = approx_rsqrt(s->XMM_S(1));
783 d->XMM_S(2) = approx_rsqrt(s->XMM_S(2));
784 d->XMM_S(3) = approx_rsqrt(s->XMM_S(3));
787 void helper_rsqrtss(XMMReg *d, XMMReg *s)
789 d->XMM_S(0) = approx_rsqrt(s->XMM_S(0));
792 void helper_rcpps(XMMReg *d, XMMReg *s)
794 d->XMM_S(0) = approx_rcp(s->XMM_S(0));
795 d->XMM_S(1) = approx_rcp(s->XMM_S(1));
796 d->XMM_S(2) = approx_rcp(s->XMM_S(2));
797 d->XMM_S(3) = approx_rcp(s->XMM_S(3));
800 void helper_rcpss(XMMReg *d, XMMReg *s)
802 d->XMM_S(0) = approx_rcp(s->XMM_S(0));
805 void helper_haddps(XMMReg *d, XMMReg *s)
807 XMMReg r;
808 r.XMM_S(0) = d->XMM_S(0) + d->XMM_S(1);
809 r.XMM_S(1) = d->XMM_S(2) + d->XMM_S(3);
810 r.XMM_S(2) = s->XMM_S(0) + s->XMM_S(1);
811 r.XMM_S(3) = s->XMM_S(2) + s->XMM_S(3);
812 *d = r;
815 void helper_haddpd(XMMReg *d, XMMReg *s)
817 XMMReg r;
818 r.XMM_D(0) = d->XMM_D(0) + d->XMM_D(1);
819 r.XMM_D(1) = s->XMM_D(0) + s->XMM_D(1);
820 *d = r;
823 void helper_hsubps(XMMReg *d, XMMReg *s)
825 XMMReg r;
826 r.XMM_S(0) = d->XMM_S(0) - d->XMM_S(1);
827 r.XMM_S(1) = d->XMM_S(2) - d->XMM_S(3);
828 r.XMM_S(2) = s->XMM_S(0) - s->XMM_S(1);
829 r.XMM_S(3) = s->XMM_S(2) - s->XMM_S(3);
830 *d = r;
833 void helper_hsubpd(XMMReg *d, XMMReg *s)
835 XMMReg r;
836 r.XMM_D(0) = d->XMM_D(0) - d->XMM_D(1);
837 r.XMM_D(1) = s->XMM_D(0) - s->XMM_D(1);
838 *d = r;
841 void helper_addsubps(XMMReg *d, XMMReg *s)
843 d->XMM_S(0) = d->XMM_S(0) - s->XMM_S(0);
844 d->XMM_S(1) = d->XMM_S(1) + s->XMM_S(1);
845 d->XMM_S(2) = d->XMM_S(2) - s->XMM_S(2);
846 d->XMM_S(3) = d->XMM_S(3) + s->XMM_S(3);
849 void helper_addsubpd(XMMReg *d, XMMReg *s)
851 d->XMM_D(0) = d->XMM_D(0) - s->XMM_D(0);
852 d->XMM_D(1) = d->XMM_D(1) + s->XMM_D(1);
855 /* XXX: unordered */
856 #define SSE_HELPER_CMP(name, F)\
857 void helper_ ## name ## ps (Reg *d, Reg *s)\
859 d->XMM_L(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
860 d->XMM_L(1) = F(32, d->XMM_S(1), s->XMM_S(1));\
861 d->XMM_L(2) = F(32, d->XMM_S(2), s->XMM_S(2));\
862 d->XMM_L(3) = F(32, d->XMM_S(3), s->XMM_S(3));\
865 void helper_ ## name ## ss (Reg *d, Reg *s)\
867 d->XMM_L(0) = F(32, d->XMM_S(0), s->XMM_S(0));\
869 void helper_ ## name ## pd (Reg *d, Reg *s)\
871 d->XMM_Q(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
872 d->XMM_Q(1) = F(64, d->XMM_D(1), s->XMM_D(1));\
875 void helper_ ## name ## sd (Reg *d, Reg *s)\
877 d->XMM_Q(0) = F(64, d->XMM_D(0), s->XMM_D(0));\
880 #define FPU_CMPEQ(size, a, b) float ## size ## _eq(a, b, &env->sse_status) ? -1 : 0
881 #define FPU_CMPLT(size, a, b) float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0
882 #define FPU_CMPLE(size, a, b) float ## size ## _le(a, b, &env->sse_status) ? -1 : 0
883 #define FPU_CMPUNORD(size, a, b) float ## size ## _unordered(a, b, &env->sse_status) ? - 1 : 0
884 #define FPU_CMPNEQ(size, a, b) float ## size ## _eq(a, b, &env->sse_status) ? 0 : -1
885 #define FPU_CMPNLT(size, a, b) float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1
886 #define FPU_CMPNLE(size, a, b) float ## size ## _le(a, b, &env->sse_status) ? 0 : -1
887 #define FPU_CMPORD(size, a, b) float ## size ## _unordered(a, b, &env->sse_status) ? 0 : -1
889 SSE_HELPER_CMP(cmpeq, FPU_CMPEQ)
890 SSE_HELPER_CMP(cmplt, FPU_CMPLT)
891 SSE_HELPER_CMP(cmple, FPU_CMPLE)
892 SSE_HELPER_CMP(cmpunord, FPU_CMPUNORD)
893 SSE_HELPER_CMP(cmpneq, FPU_CMPNEQ)
894 SSE_HELPER_CMP(cmpnlt, FPU_CMPNLT)
895 SSE_HELPER_CMP(cmpnle, FPU_CMPNLE)
896 SSE_HELPER_CMP(cmpord, FPU_CMPORD)
898 const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
900 void helper_ucomiss(Reg *d, Reg *s)
902 int ret;
903 float32 s0, s1;
905 s0 = d->XMM_S(0);
906 s1 = s->XMM_S(0);
907 ret = float32_compare_quiet(s0, s1, &env->sse_status);
908 CC_SRC = comis_eflags[ret + 1];
911 void helper_comiss(Reg *d, Reg *s)
913 int ret;
914 float32 s0, s1;
916 s0 = d->XMM_S(0);
917 s1 = s->XMM_S(0);
918 ret = float32_compare(s0, s1, &env->sse_status);
919 CC_SRC = comis_eflags[ret + 1];
922 void helper_ucomisd(Reg *d, Reg *s)
924 int ret;
925 float64 d0, d1;
927 d0 = d->XMM_D(0);
928 d1 = s->XMM_D(0);
929 ret = float64_compare_quiet(d0, d1, &env->sse_status);
930 CC_SRC = comis_eflags[ret + 1];
933 void helper_comisd(Reg *d, Reg *s)
935 int ret;
936 float64 d0, d1;
938 d0 = d->XMM_D(0);
939 d1 = s->XMM_D(0);
940 ret = float64_compare(d0, d1, &env->sse_status);
941 CC_SRC = comis_eflags[ret + 1];
944 uint32_t helper_movmskps(Reg *s)
946 int b0, b1, b2, b3;
947 b0 = s->XMM_L(0) >> 31;
948 b1 = s->XMM_L(1) >> 31;
949 b2 = s->XMM_L(2) >> 31;
950 b3 = s->XMM_L(3) >> 31;
951 return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3);
954 uint32_t helper_movmskpd(Reg *s)
956 int b0, b1;
957 b0 = s->XMM_L(1) >> 31;
958 b1 = s->XMM_L(3) >> 31;
959 return b0 | (b1 << 1);
962 #endif
964 uint32_t glue(helper_pmovmskb, SUFFIX)(Reg *s)
966 uint32_t val;
967 val = 0;
968 val |= (s->B(0) >> 7);
969 val |= (s->B(1) >> 6) & 0x02;
970 val |= (s->B(2) >> 5) & 0x04;
971 val |= (s->B(3) >> 4) & 0x08;
972 val |= (s->B(4) >> 3) & 0x10;
973 val |= (s->B(5) >> 2) & 0x20;
974 val |= (s->B(6) >> 1) & 0x40;
975 val |= (s->B(7)) & 0x80;
976 #if SHIFT == 1
977 val |= (s->B(8) << 1) & 0x0100;
978 val |= (s->B(9) << 2) & 0x0200;
979 val |= (s->B(10) << 3) & 0x0400;
980 val |= (s->B(11) << 4) & 0x0800;
981 val |= (s->B(12) << 5) & 0x1000;
982 val |= (s->B(13) << 6) & 0x2000;
983 val |= (s->B(14) << 7) & 0x4000;
984 val |= (s->B(15) << 8) & 0x8000;
985 #endif
986 return val;
989 void glue(helper_packsswb, SUFFIX) (Reg *d, Reg *s)
991 Reg r;
993 r.B(0) = satsb((int16_t)d->W(0));
994 r.B(1) = satsb((int16_t)d->W(1));
995 r.B(2) = satsb((int16_t)d->W(2));
996 r.B(3) = satsb((int16_t)d->W(3));
997 #if SHIFT == 1
998 r.B(4) = satsb((int16_t)d->W(4));
999 r.B(5) = satsb((int16_t)d->W(5));
1000 r.B(6) = satsb((int16_t)d->W(6));
1001 r.B(7) = satsb((int16_t)d->W(7));
1002 #endif
1003 r.B((4 << SHIFT) + 0) = satsb((int16_t)s->W(0));
1004 r.B((4 << SHIFT) + 1) = satsb((int16_t)s->W(1));
1005 r.B((4 << SHIFT) + 2) = satsb((int16_t)s->W(2));
1006 r.B((4 << SHIFT) + 3) = satsb((int16_t)s->W(3));
1007 #if SHIFT == 1
1008 r.B(12) = satsb((int16_t)s->W(4));
1009 r.B(13) = satsb((int16_t)s->W(5));
1010 r.B(14) = satsb((int16_t)s->W(6));
1011 r.B(15) = satsb((int16_t)s->W(7));
1012 #endif
1013 *d = r;
1016 void glue(helper_packuswb, SUFFIX) (Reg *d, Reg *s)
1018 Reg r;
1020 r.B(0) = satub((int16_t)d->W(0));
1021 r.B(1) = satub((int16_t)d->W(1));
1022 r.B(2) = satub((int16_t)d->W(2));
1023 r.B(3) = satub((int16_t)d->W(3));
1024 #if SHIFT == 1
1025 r.B(4) = satub((int16_t)d->W(4));
1026 r.B(5) = satub((int16_t)d->W(5));
1027 r.B(6) = satub((int16_t)d->W(6));
1028 r.B(7) = satub((int16_t)d->W(7));
1029 #endif
1030 r.B((4 << SHIFT) + 0) = satub((int16_t)s->W(0));
1031 r.B((4 << SHIFT) + 1) = satub((int16_t)s->W(1));
1032 r.B((4 << SHIFT) + 2) = satub((int16_t)s->W(2));
1033 r.B((4 << SHIFT) + 3) = satub((int16_t)s->W(3));
1034 #if SHIFT == 1
1035 r.B(12) = satub((int16_t)s->W(4));
1036 r.B(13) = satub((int16_t)s->W(5));
1037 r.B(14) = satub((int16_t)s->W(6));
1038 r.B(15) = satub((int16_t)s->W(7));
1039 #endif
1040 *d = r;
1043 void glue(helper_packssdw, SUFFIX) (Reg *d, Reg *s)
1045 Reg r;
1047 r.W(0) = satsw(d->L(0));
1048 r.W(1) = satsw(d->L(1));
1049 #if SHIFT == 1
1050 r.W(2) = satsw(d->L(2));
1051 r.W(3) = satsw(d->L(3));
1052 #endif
1053 r.W((2 << SHIFT) + 0) = satsw(s->L(0));
1054 r.W((2 << SHIFT) + 1) = satsw(s->L(1));
1055 #if SHIFT == 1
1056 r.W(6) = satsw(s->L(2));
1057 r.W(7) = satsw(s->L(3));
1058 #endif
1059 *d = r;
1062 #define UNPCK_OP(base_name, base) \
1064 void glue(helper_punpck ## base_name ## bw, SUFFIX) (Reg *d, Reg *s) \
1066 Reg r; \
1068 r.B(0) = d->B((base << (SHIFT + 2)) + 0); \
1069 r.B(1) = s->B((base << (SHIFT + 2)) + 0); \
1070 r.B(2) = d->B((base << (SHIFT + 2)) + 1); \
1071 r.B(3) = s->B((base << (SHIFT + 2)) + 1); \
1072 r.B(4) = d->B((base << (SHIFT + 2)) + 2); \
1073 r.B(5) = s->B((base << (SHIFT + 2)) + 2); \
1074 r.B(6) = d->B((base << (SHIFT + 2)) + 3); \
1075 r.B(7) = s->B((base << (SHIFT + 2)) + 3); \
1076 XMM_ONLY( \
1077 r.B(8) = d->B((base << (SHIFT + 2)) + 4); \
1078 r.B(9) = s->B((base << (SHIFT + 2)) + 4); \
1079 r.B(10) = d->B((base << (SHIFT + 2)) + 5); \
1080 r.B(11) = s->B((base << (SHIFT + 2)) + 5); \
1081 r.B(12) = d->B((base << (SHIFT + 2)) + 6); \
1082 r.B(13) = s->B((base << (SHIFT + 2)) + 6); \
1083 r.B(14) = d->B((base << (SHIFT + 2)) + 7); \
1084 r.B(15) = s->B((base << (SHIFT + 2)) + 7); \
1086 *d = r; \
1089 void glue(helper_punpck ## base_name ## wd, SUFFIX) (Reg *d, Reg *s) \
1091 Reg r; \
1093 r.W(0) = d->W((base << (SHIFT + 1)) + 0); \
1094 r.W(1) = s->W((base << (SHIFT + 1)) + 0); \
1095 r.W(2) = d->W((base << (SHIFT + 1)) + 1); \
1096 r.W(3) = s->W((base << (SHIFT + 1)) + 1); \
1097 XMM_ONLY( \
1098 r.W(4) = d->W((base << (SHIFT + 1)) + 2); \
1099 r.W(5) = s->W((base << (SHIFT + 1)) + 2); \
1100 r.W(6) = d->W((base << (SHIFT + 1)) + 3); \
1101 r.W(7) = s->W((base << (SHIFT + 1)) + 3); \
1103 *d = r; \
1106 void glue(helper_punpck ## base_name ## dq, SUFFIX) (Reg *d, Reg *s) \
1108 Reg r; \
1110 r.L(0) = d->L((base << SHIFT) + 0); \
1111 r.L(1) = s->L((base << SHIFT) + 0); \
1112 XMM_ONLY( \
1113 r.L(2) = d->L((base << SHIFT) + 1); \
1114 r.L(3) = s->L((base << SHIFT) + 1); \
1116 *d = r; \
1119 XMM_ONLY( \
1120 void glue(helper_punpck ## base_name ## qdq, SUFFIX) (Reg *d, Reg *s) \
1122 Reg r; \
1124 r.Q(0) = d->Q(base); \
1125 r.Q(1) = s->Q(base); \
1126 *d = r; \
1130 UNPCK_OP(l, 0)
1131 UNPCK_OP(h, 1)
1133 /* 3DNow! float ops */
1134 #if SHIFT == 0
1135 void helper_pi2fd(MMXReg *d, MMXReg *s)
1137 d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1138 d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1141 void helper_pi2fw(MMXReg *d, MMXReg *s)
1143 d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1144 d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1147 void helper_pf2id(MMXReg *d, MMXReg *s)
1149 d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1150 d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1153 void helper_pf2iw(MMXReg *d, MMXReg *s)
1155 d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status));
1156 d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status));
1159 void helper_pfacc(MMXReg *d, MMXReg *s)
1161 MMXReg r;
1162 r.MMX_S(0) = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1163 r.MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1164 *d = r;
1167 void helper_pfadd(MMXReg *d, MMXReg *s)
1169 d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1170 d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1173 void helper_pfcmpeq(MMXReg *d, MMXReg *s)
1175 d->MMX_L(0) = float32_eq(d->MMX_S(0), s->MMX_S(0), &env->mmx_status) ? -1 : 0;
1176 d->MMX_L(1) = float32_eq(d->MMX_S(1), s->MMX_S(1), &env->mmx_status) ? -1 : 0;
1179 void helper_pfcmpge(MMXReg *d, MMXReg *s)
1181 d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0), &env->mmx_status) ? -1 : 0;
1182 d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1), &env->mmx_status) ? -1 : 0;
1185 void helper_pfcmpgt(MMXReg *d, MMXReg *s)
1187 d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status) ? -1 : 0;
1188 d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status) ? -1 : 0;
1191 void helper_pfmax(MMXReg *d, MMXReg *s)
1193 if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status))
1194 d->MMX_S(0) = s->MMX_S(0);
1195 if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status))
1196 d->MMX_S(1) = s->MMX_S(1);
1199 void helper_pfmin(MMXReg *d, MMXReg *s)
1201 if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status))
1202 d->MMX_S(0) = s->MMX_S(0);
1203 if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status))
1204 d->MMX_S(1) = s->MMX_S(1);
1207 void helper_pfmul(MMXReg *d, MMXReg *s)
1209 d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1210 d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1213 void helper_pfnacc(MMXReg *d, MMXReg *s)
1215 MMXReg r;
1216 r.MMX_S(0) = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1217 r.MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1218 *d = r;
1221 void helper_pfpnacc(MMXReg *d, MMXReg *s)
1223 MMXReg r;
1224 r.MMX_S(0) = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1225 r.MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1226 *d = r;
1229 void helper_pfrcp(MMXReg *d, MMXReg *s)
1231 d->MMX_S(0) = approx_rcp(s->MMX_S(0));
1232 d->MMX_S(1) = d->MMX_S(0);
1235 void helper_pfrsqrt(MMXReg *d, MMXReg *s)
1237 d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1238 d->MMX_S(1) = approx_rsqrt(d->MMX_S(1));
1239 d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1240 d->MMX_L(0) = d->MMX_L(1);
1243 void helper_pfsub(MMXReg *d, MMXReg *s)
1245 d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1246 d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1249 void helper_pfsubr(MMXReg *d, MMXReg *s)
1251 d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1252 d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1255 void helper_pswapd(MMXReg *d, MMXReg *s)
1257 MMXReg r;
1258 r.MMX_L(0) = s->MMX_L(1);
1259 r.MMX_L(1) = s->MMX_L(0);
1260 *d = r;
1262 #endif
1264 /* SSSE3 op helpers */
1265 void glue(helper_pshufb, SUFFIX) (Reg *d, Reg *s)
1267 int i;
1268 Reg r;
1270 for (i = 0; i < (8 << SHIFT); i++)
1271 r.B(i) = (s->B(i) & 0x80) ? 0 : (d->B(s->B(i) & ((8 << SHIFT) - 1)));
1273 *d = r;
1276 void glue(helper_phaddw, SUFFIX) (Reg *d, Reg *s)
1278 d->W(0) = (int16_t)d->W(0) + (int16_t)d->W(1);
1279 d->W(1) = (int16_t)d->W(2) + (int16_t)d->W(3);
1280 XMM_ONLY(d->W(2) = (int16_t)d->W(4) + (int16_t)d->W(5));
1281 XMM_ONLY(d->W(3) = (int16_t)d->W(6) + (int16_t)d->W(7));
1282 d->W((2 << SHIFT) + 0) = (int16_t)s->W(0) + (int16_t)s->W(1);
1283 d->W((2 << SHIFT) + 1) = (int16_t)s->W(2) + (int16_t)s->W(3);
1284 XMM_ONLY(d->W(6) = (int16_t)s->W(4) + (int16_t)s->W(5));
1285 XMM_ONLY(d->W(7) = (int16_t)s->W(6) + (int16_t)s->W(7));
1288 void glue(helper_phaddd, SUFFIX) (Reg *d, Reg *s)
1290 d->L(0) = (int32_t)d->L(0) + (int32_t)d->L(1);
1291 XMM_ONLY(d->L(1) = (int32_t)d->L(2) + (int32_t)d->L(3));
1292 d->L((1 << SHIFT) + 0) = (int32_t)s->L(0) + (int32_t)s->L(1);
1293 XMM_ONLY(d->L(3) = (int32_t)s->L(2) + (int32_t)s->L(3));
1296 void glue(helper_phaddsw, SUFFIX) (Reg *d, Reg *s)
1298 d->W(0) = satsw((int16_t)d->W(0) + (int16_t)d->W(1));
1299 d->W(1) = satsw((int16_t)d->W(2) + (int16_t)d->W(3));
1300 XMM_ONLY(d->W(2) = satsw((int16_t)d->W(4) + (int16_t)d->W(5)));
1301 XMM_ONLY(d->W(3) = satsw((int16_t)d->W(6) + (int16_t)d->W(7)));
1302 d->W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) + (int16_t)s->W(1));
1303 d->W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) + (int16_t)s->W(3));
1304 XMM_ONLY(d->W(6) = satsw((int16_t)s->W(4) + (int16_t)s->W(5)));
1305 XMM_ONLY(d->W(7) = satsw((int16_t)s->W(6) + (int16_t)s->W(7)));
1308 void glue(helper_pmaddubsw, SUFFIX) (Reg *d, Reg *s)
1310 d->W(0) = satsw((int8_t)s->B( 0) * (uint8_t)d->B( 0) +
1311 (int8_t)s->B( 1) * (uint8_t)d->B( 1));
1312 d->W(1) = satsw((int8_t)s->B( 2) * (uint8_t)d->B( 2) +
1313 (int8_t)s->B( 3) * (uint8_t)d->B( 3));
1314 d->W(2) = satsw((int8_t)s->B( 4) * (uint8_t)d->B( 4) +
1315 (int8_t)s->B( 5) * (uint8_t)d->B( 5));
1316 d->W(3) = satsw((int8_t)s->B( 6) * (uint8_t)d->B( 6) +
1317 (int8_t)s->B( 7) * (uint8_t)d->B( 7));
1318 #if SHIFT == 1
1319 d->W(4) = satsw((int8_t)s->B( 8) * (uint8_t)d->B( 8) +
1320 (int8_t)s->B( 9) * (uint8_t)d->B( 9));
1321 d->W(5) = satsw((int8_t)s->B(10) * (uint8_t)d->B(10) +
1322 (int8_t)s->B(11) * (uint8_t)d->B(11));
1323 d->W(6) = satsw((int8_t)s->B(12) * (uint8_t)d->B(12) +
1324 (int8_t)s->B(13) * (uint8_t)d->B(13));
1325 d->W(7) = satsw((int8_t)s->B(14) * (uint8_t)d->B(14) +
1326 (int8_t)s->B(15) * (uint8_t)d->B(15));
1327 #endif
1330 void glue(helper_phsubw, SUFFIX) (Reg *d, Reg *s)
1332 d->W(0) = (int16_t)d->W(0) - (int16_t)d->W(1);
1333 d->W(1) = (int16_t)d->W(2) - (int16_t)d->W(3);
1334 XMM_ONLY(d->W(2) = (int16_t)d->W(4) - (int16_t)d->W(5));
1335 XMM_ONLY(d->W(3) = (int16_t)d->W(6) - (int16_t)d->W(7));
1336 d->W((2 << SHIFT) + 0) = (int16_t)s->W(0) - (int16_t)s->W(1);
1337 d->W((2 << SHIFT) + 1) = (int16_t)s->W(2) - (int16_t)s->W(3);
1338 XMM_ONLY(d->W(6) = (int16_t)s->W(4) - (int16_t)s->W(5));
1339 XMM_ONLY(d->W(7) = (int16_t)s->W(6) - (int16_t)s->W(7));
1342 void glue(helper_phsubd, SUFFIX) (Reg *d, Reg *s)
1344 d->L(0) = (int32_t)d->L(0) - (int32_t)d->L(1);
1345 XMM_ONLY(d->L(1) = (int32_t)d->L(2) - (int32_t)d->L(3));
1346 d->L((1 << SHIFT) + 0) = (int32_t)s->L(0) - (int32_t)s->L(1);
1347 XMM_ONLY(d->L(3) = (int32_t)s->L(2) - (int32_t)s->L(3));
1350 void glue(helper_phsubsw, SUFFIX) (Reg *d, Reg *s)
1352 d->W(0) = satsw((int16_t)d->W(0) - (int16_t)d->W(1));
1353 d->W(1) = satsw((int16_t)d->W(2) - (int16_t)d->W(3));
1354 XMM_ONLY(d->W(2) = satsw((int16_t)d->W(4) - (int16_t)d->W(5)));
1355 XMM_ONLY(d->W(3) = satsw((int16_t)d->W(6) - (int16_t)d->W(7)));
1356 d->W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) - (int16_t)s->W(1));
1357 d->W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) - (int16_t)s->W(3));
1358 XMM_ONLY(d->W(6) = satsw((int16_t)s->W(4) - (int16_t)s->W(5)));
1359 XMM_ONLY(d->W(7) = satsw((int16_t)s->W(6) - (int16_t)s->W(7)));
1362 #define FABSB(_, x) x > INT8_MAX ? -(int8_t ) x : x
1363 #define FABSW(_, x) x > INT16_MAX ? -(int16_t) x : x
1364 #define FABSL(_, x) x > INT32_MAX ? -(int32_t) x : x
1365 SSE_HELPER_B(helper_pabsb, FABSB)
1366 SSE_HELPER_W(helper_pabsw, FABSW)
1367 SSE_HELPER_L(helper_pabsd, FABSL)
1369 #define FMULHRSW(d, s) ((int16_t) d * (int16_t) s + 0x4000) >> 15
1370 SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1372 #define FSIGNB(d, s) s <= INT8_MAX ? s ? d : 0 : -(int8_t ) d
1373 #define FSIGNW(d, s) s <= INT16_MAX ? s ? d : 0 : -(int16_t) d
1374 #define FSIGNL(d, s) s <= INT32_MAX ? s ? d : 0 : -(int32_t) d
1375 SSE_HELPER_B(helper_psignb, FSIGNB)
1376 SSE_HELPER_W(helper_psignw, FSIGNW)
1377 SSE_HELPER_L(helper_psignd, FSIGNL)
1379 void glue(helper_palignr, SUFFIX) (Reg *d, Reg *s, int32_t shift)
1381 Reg r;
1383 /* XXX could be checked during translation */
1384 if (shift >= (16 << SHIFT)) {
1385 r.Q(0) = 0;
1386 XMM_ONLY(r.Q(1) = 0);
1387 } else {
1388 shift <<= 3;
1389 #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1390 #if SHIFT == 0
1391 r.Q(0) = SHR(s->Q(0), shift - 0) |
1392 SHR(d->Q(0), shift - 64);
1393 #else
1394 r.Q(0) = SHR(s->Q(0), shift - 0) |
1395 SHR(s->Q(1), shift - 64) |
1396 SHR(d->Q(0), shift - 128) |
1397 SHR(d->Q(1), shift - 192);
1398 r.Q(1) = SHR(s->Q(0), shift + 64) |
1399 SHR(s->Q(1), shift - 0) |
1400 SHR(d->Q(0), shift - 64) |
1401 SHR(d->Q(1), shift - 128);
1402 #endif
1403 #undef SHR
1406 *d = r;
1409 #define XMM0 env->xmm_regs[0]
1411 #if SHIFT == 1
1412 #define SSE_HELPER_V(name, elem, num, F)\
1413 void glue(name, SUFFIX) (Reg *d, Reg *s)\
1415 d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0));\
1416 d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1));\
1417 if (num > 2) {\
1418 d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2));\
1419 d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3));\
1420 if (num > 4) {\
1421 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4));\
1422 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5));\
1423 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6));\
1424 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7));\
1425 if (num > 8) {\
1426 d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8));\
1427 d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9));\
1428 d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10));\
1429 d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11));\
1430 d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12));\
1431 d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13));\
1432 d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14));\
1433 d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15));\
1439 #define SSE_HELPER_I(name, elem, num, F)\
1440 void glue(name, SUFFIX) (Reg *d, Reg *s, uint32_t imm)\
1442 d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1));\
1443 d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1));\
1444 if (num > 2) {\
1445 d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1));\
1446 d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1));\
1447 if (num > 4) {\
1448 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1));\
1449 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1));\
1450 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1));\
1451 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1));\
1452 if (num > 8) {\
1453 d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1));\
1454 d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1));\
1455 d->elem(10) = F(d->elem(10), s->elem(10), ((imm >> 10) & 1));\
1456 d->elem(11) = F(d->elem(11), s->elem(11), ((imm >> 11) & 1));\
1457 d->elem(12) = F(d->elem(12), s->elem(12), ((imm >> 12) & 1));\
1458 d->elem(13) = F(d->elem(13), s->elem(13), ((imm >> 13) & 1));\
1459 d->elem(14) = F(d->elem(14), s->elem(14), ((imm >> 14) & 1));\
1460 d->elem(15) = F(d->elem(15), s->elem(15), ((imm >> 15) & 1));\
1466 /* SSE4.1 op helpers */
1467 #define FBLENDVB(d, s, m) (m & 0x80) ? s : d
1468 #define FBLENDVPS(d, s, m) (m & 0x80000000) ? s : d
1469 #define FBLENDVPD(d, s, m) (m & 0x8000000000000000LL) ? s : d
1470 SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB)
1471 SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS)
1472 SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD)
1474 void glue(helper_ptest, SUFFIX) (Reg *d, Reg *s)
1476 uint64_t zf = (s->Q(0) & d->Q(0)) | (s->Q(1) & d->Q(1));
1477 uint64_t cf = (s->Q(0) & ~d->Q(0)) | (s->Q(1) & ~d->Q(1));
1479 CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1482 #define SSE_HELPER_F(name, elem, num, F)\
1483 void glue(name, SUFFIX) (Reg *d, Reg *s)\
1485 d->elem(0) = F(0);\
1486 d->elem(1) = F(1);\
1487 if (num > 2) {\
1488 d->elem(2) = F(2);\
1489 d->elem(3) = F(3);\
1490 if (num > 4) {\
1491 d->elem(4) = F(4);\
1492 d->elem(5) = F(5);\
1493 d->elem(6) = F(6);\
1494 d->elem(7) = F(7);\
1499 SSE_HELPER_F(helper_pmovsxbw, W, 8, (int8_t) s->B)
1500 SSE_HELPER_F(helper_pmovsxbd, L, 4, (int8_t) s->B)
1501 SSE_HELPER_F(helper_pmovsxbq, Q, 2, (int8_t) s->B)
1502 SSE_HELPER_F(helper_pmovsxwd, L, 4, (int16_t) s->W)
1503 SSE_HELPER_F(helper_pmovsxwq, Q, 2, (int16_t) s->W)
1504 SSE_HELPER_F(helper_pmovsxdq, Q, 2, (int32_t) s->L)
1505 SSE_HELPER_F(helper_pmovzxbw, W, 8, s->B)
1506 SSE_HELPER_F(helper_pmovzxbd, L, 4, s->B)
1507 SSE_HELPER_F(helper_pmovzxbq, Q, 2, s->B)
1508 SSE_HELPER_F(helper_pmovzxwd, L, 4, s->W)
1509 SSE_HELPER_F(helper_pmovzxwq, Q, 2, s->W)
1510 SSE_HELPER_F(helper_pmovzxdq, Q, 2, s->L)
1512 void glue(helper_pmuldq, SUFFIX) (Reg *d, Reg *s)
1514 d->Q(0) = (int64_t) (int32_t) d->L(0) * (int32_t) s->L(0);
1515 d->Q(1) = (int64_t) (int32_t) d->L(2) * (int32_t) s->L(2);
1518 #define FCMPEQQ(d, s) d == s ? -1 : 0
1519 SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1521 void glue(helper_packusdw, SUFFIX) (Reg *d, Reg *s)
1523 d->W(0) = satuw((int32_t) d->L(0));
1524 d->W(1) = satuw((int32_t) d->L(1));
1525 d->W(2) = satuw((int32_t) d->L(2));
1526 d->W(3) = satuw((int32_t) d->L(3));
1527 d->W(4) = satuw((int32_t) s->L(0));
1528 d->W(5) = satuw((int32_t) s->L(1));
1529 d->W(6) = satuw((int32_t) s->L(2));
1530 d->W(7) = satuw((int32_t) s->L(3));
1533 #define FMINSB(d, s) MIN((int8_t) d, (int8_t) s)
1534 #define FMINSD(d, s) MIN((int32_t) d, (int32_t) s)
1535 #define FMAXSB(d, s) MAX((int8_t) d, (int8_t) s)
1536 #define FMAXSD(d, s) MAX((int32_t) d, (int32_t) s)
1537 SSE_HELPER_B(helper_pminsb, FMINSB)
1538 SSE_HELPER_L(helper_pminsd, FMINSD)
1539 SSE_HELPER_W(helper_pminuw, MIN)
1540 SSE_HELPER_L(helper_pminud, MIN)
1541 SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1542 SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1543 SSE_HELPER_W(helper_pmaxuw, MAX)
1544 SSE_HELPER_L(helper_pmaxud, MAX)
1546 #define FMULLD(d, s) (int32_t) d * (int32_t) s
1547 SSE_HELPER_L(helper_pmulld, FMULLD)
1549 void glue(helper_phminposuw, SUFFIX) (Reg *d, Reg *s)
1551 int idx = 0;
1553 if (s->W(1) < s->W(idx))
1554 idx = 1;
1555 if (s->W(2) < s->W(idx))
1556 idx = 2;
1557 if (s->W(3) < s->W(idx))
1558 idx = 3;
1559 if (s->W(4) < s->W(idx))
1560 idx = 4;
1561 if (s->W(5) < s->W(idx))
1562 idx = 5;
1563 if (s->W(6) < s->W(idx))
1564 idx = 6;
1565 if (s->W(7) < s->W(idx))
1566 idx = 7;
1568 d->Q(1) = 0;
1569 d->L(1) = 0;
1570 d->W(1) = idx;
1571 d->W(0) = s->W(idx);
1574 void glue(helper_roundps, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1576 signed char prev_rounding_mode;
1578 prev_rounding_mode = env->sse_status.float_rounding_mode;
1579 if (!(mode & (1 << 2)))
1580 switch (mode & 3) {
1581 case 0:
1582 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1583 break;
1584 case 1:
1585 set_float_rounding_mode(float_round_down, &env->sse_status);
1586 break;
1587 case 2:
1588 set_float_rounding_mode(float_round_up, &env->sse_status);
1589 break;
1590 case 3:
1591 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1592 break;
1595 d->L(0) = float64_round_to_int(s->L(0), &env->sse_status);
1596 d->L(1) = float64_round_to_int(s->L(1), &env->sse_status);
1597 d->L(2) = float64_round_to_int(s->L(2), &env->sse_status);
1598 d->L(3) = float64_round_to_int(s->L(3), &env->sse_status);
1600 #if 0 /* TODO */
1601 if (mode & (1 << 3))
1602 set_float_exception_flags(
1603 get_float_exception_flags(&env->sse_status) &
1604 ~float_flag_inexact,
1605 &env->sse_status);
1606 #endif
1607 env->sse_status.float_rounding_mode = prev_rounding_mode;
1610 void glue(helper_roundpd, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1612 signed char prev_rounding_mode;
1614 prev_rounding_mode = env->sse_status.float_rounding_mode;
1615 if (!(mode & (1 << 2)))
1616 switch (mode & 3) {
1617 case 0:
1618 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1619 break;
1620 case 1:
1621 set_float_rounding_mode(float_round_down, &env->sse_status);
1622 break;
1623 case 2:
1624 set_float_rounding_mode(float_round_up, &env->sse_status);
1625 break;
1626 case 3:
1627 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1628 break;
1631 d->Q(0) = float64_round_to_int(s->Q(0), &env->sse_status);
1632 d->Q(1) = float64_round_to_int(s->Q(1), &env->sse_status);
1634 #if 0 /* TODO */
1635 if (mode & (1 << 3))
1636 set_float_exception_flags(
1637 get_float_exception_flags(&env->sse_status) &
1638 ~float_flag_inexact,
1639 &env->sse_status);
1640 #endif
1641 env->sse_status.float_rounding_mode = prev_rounding_mode;
1644 void glue(helper_roundss, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1646 signed char prev_rounding_mode;
1648 prev_rounding_mode = env->sse_status.float_rounding_mode;
1649 if (!(mode & (1 << 2)))
1650 switch (mode & 3) {
1651 case 0:
1652 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1653 break;
1654 case 1:
1655 set_float_rounding_mode(float_round_down, &env->sse_status);
1656 break;
1657 case 2:
1658 set_float_rounding_mode(float_round_up, &env->sse_status);
1659 break;
1660 case 3:
1661 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1662 break;
1665 d->L(0) = float64_round_to_int(s->L(0), &env->sse_status);
1667 #if 0 /* TODO */
1668 if (mode & (1 << 3))
1669 set_float_exception_flags(
1670 get_float_exception_flags(&env->sse_status) &
1671 ~float_flag_inexact,
1672 &env->sse_status);
1673 #endif
1674 env->sse_status.float_rounding_mode = prev_rounding_mode;
1677 void glue(helper_roundsd, SUFFIX) (Reg *d, Reg *s, uint32_t mode)
1679 signed char prev_rounding_mode;
1681 prev_rounding_mode = env->sse_status.float_rounding_mode;
1682 if (!(mode & (1 << 2)))
1683 switch (mode & 3) {
1684 case 0:
1685 set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1686 break;
1687 case 1:
1688 set_float_rounding_mode(float_round_down, &env->sse_status);
1689 break;
1690 case 2:
1691 set_float_rounding_mode(float_round_up, &env->sse_status);
1692 break;
1693 case 3:
1694 set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1695 break;
1698 d->Q(0) = float64_round_to_int(s->Q(0), &env->sse_status);
1700 #if 0 /* TODO */
1701 if (mode & (1 << 3))
1702 set_float_exception_flags(
1703 get_float_exception_flags(&env->sse_status) &
1704 ~float_flag_inexact,
1705 &env->sse_status);
1706 #endif
1707 env->sse_status.float_rounding_mode = prev_rounding_mode;
1710 #define FBLENDP(d, s, m) m ? s : d
1711 SSE_HELPER_I(helper_blendps, L, 4, FBLENDP)
1712 SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP)
1713 SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)
1715 void glue(helper_dpps, SUFFIX) (Reg *d, Reg *s, uint32_t mask)
1717 float32 iresult = 0 /*float32_zero*/;
1719 if (mask & (1 << 4))
1720 iresult = float32_add(iresult,
1721 float32_mul(d->L(0), s->L(0), &env->sse_status),
1722 &env->sse_status);
1723 if (mask & (1 << 5))
1724 iresult = float32_add(iresult,
1725 float32_mul(d->L(1), s->L(1), &env->sse_status),
1726 &env->sse_status);
1727 if (mask & (1 << 6))
1728 iresult = float32_add(iresult,
1729 float32_mul(d->L(2), s->L(2), &env->sse_status),
1730 &env->sse_status);
1731 if (mask & (1 << 7))
1732 iresult = float32_add(iresult,
1733 float32_mul(d->L(3), s->L(3), &env->sse_status),
1734 &env->sse_status);
1735 d->L(0) = (mask & (1 << 0)) ? iresult : 0 /*float32_zero*/;
1736 d->L(1) = (mask & (1 << 1)) ? iresult : 0 /*float32_zero*/;
1737 d->L(2) = (mask & (1 << 2)) ? iresult : 0 /*float32_zero*/;
1738 d->L(3) = (mask & (1 << 3)) ? iresult : 0 /*float32_zero*/;
1741 void glue(helper_dppd, SUFFIX) (Reg *d, Reg *s, uint32_t mask)
1743 float64 iresult = 0 /*float64_zero*/;
1745 if (mask & (1 << 4))
1746 iresult = float64_add(iresult,
1747 float64_mul(d->Q(0), s->Q(0), &env->sse_status),
1748 &env->sse_status);
1749 if (mask & (1 << 5))
1750 iresult = float64_add(iresult,
1751 float64_mul(d->Q(1), s->Q(1), &env->sse_status),
1752 &env->sse_status);
1753 d->Q(0) = (mask & (1 << 0)) ? iresult : 0 /*float64_zero*/;
1754 d->Q(1) = (mask & (1 << 1)) ? iresult : 0 /*float64_zero*/;
1757 void glue(helper_mpsadbw, SUFFIX) (Reg *d, Reg *s, uint32_t offset)
1759 int s0 = (offset & 3) << 2;
1760 int d0 = (offset & 4) << 0;
1761 int i;
1762 Reg r;
1764 for (i = 0; i < 8; i++, d0++) {
1765 r.W(i) = 0;
1766 r.W(i) += abs1(d->B(d0 + 0) - s->B(s0 + 0));
1767 r.W(i) += abs1(d->B(d0 + 1) - s->B(s0 + 1));
1768 r.W(i) += abs1(d->B(d0 + 2) - s->B(s0 + 2));
1769 r.W(i) += abs1(d->B(d0 + 3) - s->B(s0 + 3));
1772 *d = r;
1775 /* SSE4.2 op helpers */
1776 /* it's unclear whether signed or unsigned */
1777 #define FCMPGTQ(d, s) d > s ? -1 : 0
1778 SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1780 static inline int pcmp_elen(int reg, uint32_t ctrl)
1782 int val;
1784 /* Presence of REX.W is indicated by a bit higher than 7 set */
1785 if (ctrl >> 8)
1786 val = abs1((int64_t) env->regs[reg]);
1787 else
1788 val = abs1((int32_t) env->regs[reg]);
1790 if (ctrl & 1) {
1791 if (val > 8)
1792 return 8;
1793 } else
1794 if (val > 16)
1795 return 16;
1797 return val;
1800 static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
1802 int val = 0;
1804 if (ctrl & 1) {
1805 while (val < 8 && r->W(val))
1806 val++;
1807 } else
1808 while (val < 16 && r->B(val))
1809 val++;
1811 return val;
1814 static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
1816 switch ((ctrl >> 0) & 3) {
1817 case 0:
1818 return r->B(i);
1819 case 1:
1820 return r->W(i);
1821 case 2:
1822 return (int8_t) r->B(i);
1823 case 3:
1824 default:
1825 return (int16_t) r->W(i);
1829 static inline unsigned pcmpxstrx(Reg *d, Reg *s,
1830 int8_t ctrl, int valids, int validd)
1832 unsigned int res = 0;
1833 int v;
1834 int j, i;
1835 int upper = (ctrl & 1) ? 7 : 15;
1837 valids--;
1838 validd--;
1840 CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
1842 switch ((ctrl >> 2) & 3) {
1843 case 0:
1844 for (j = valids; j >= 0; j--) {
1845 res <<= 1;
1846 v = pcmp_val(s, ctrl, j);
1847 for (i = validd; i >= 0; i--)
1848 res |= (v == pcmp_val(d, ctrl, i));
1850 break;
1851 case 1:
1852 for (j = valids; j >= 0; j--) {
1853 res <<= 1;
1854 v = pcmp_val(s, ctrl, j);
1855 for (i = ((validd - 1) | 1); i >= 0; i -= 2)
1856 res |= (pcmp_val(d, ctrl, i - 0) <= v &&
1857 pcmp_val(d, ctrl, i - 1) >= v);
1859 break;
1860 case 2:
1861 res = (2 << (upper - MAX(valids, validd))) - 1;
1862 res <<= MAX(valids, validd) - MIN(valids, validd);
1863 for (i = MIN(valids, validd); i >= 0; i--) {
1864 res <<= 1;
1865 v = pcmp_val(s, ctrl, i);
1866 res |= (v == pcmp_val(d, ctrl, i));
1868 break;
1869 case 3:
1870 for (j = valids - validd; j >= 0; j--) {
1871 res <<= 1;
1872 res |= 1;
1873 for (i = MIN(upper - j, validd); i >= 0; i--)
1874 res &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
1876 break;
1879 switch ((ctrl >> 4) & 3) {
1880 case 1:
1881 res ^= (2 << upper) - 1;
1882 break;
1883 case 3:
1884 res ^= (2 << valids) - 1;
1885 break;
1888 if (res)
1889 CC_SRC |= CC_C;
1890 if (res & 1)
1891 CC_SRC |= CC_O;
1893 return res;
1896 static inline int rffs1(unsigned int val)
1898 int ret = 1, hi;
1900 for (hi = sizeof(val) * 4; hi; hi /= 2)
1901 if (val >> hi) {
1902 val >>= hi;
1903 ret += hi;
1906 return ret;
1909 static inline int ffs1(unsigned int val)
1911 int ret = 1, hi;
1913 for (hi = sizeof(val) * 4; hi; hi /= 2)
1914 if (val << hi) {
1915 val <<= hi;
1916 ret += hi;
1919 return ret;
1922 void glue(helper_pcmpestri, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
1924 unsigned int res = pcmpxstrx(d, s, ctrl,
1925 pcmp_elen(R_EDX, ctrl),
1926 pcmp_elen(R_EAX, ctrl));
1928 if (res)
1929 env->regs[R_ECX] = ((ctrl & (1 << 6)) ? rffs1 : ffs1)(res) - 1;
1930 else
1931 env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
1934 void glue(helper_pcmpestrm, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
1936 int i;
1937 unsigned int res = pcmpxstrx(d, s, ctrl,
1938 pcmp_elen(R_EDX, ctrl),
1939 pcmp_elen(R_EAX, ctrl));
1941 if ((ctrl >> 6) & 1) {
1942 if (ctrl & 1)
1943 for (i = 0; i <= 8; i--, res >>= 1)
1944 d->W(i) = (res & 1) ? ~0 : 0;
1945 else
1946 for (i = 0; i <= 16; i--, res >>= 1)
1947 d->B(i) = (res & 1) ? ~0 : 0;
1948 } else {
1949 d->Q(1) = 0;
1950 d->Q(0) = res;
1954 void glue(helper_pcmpistri, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
1956 unsigned int res = pcmpxstrx(d, s, ctrl,
1957 pcmp_ilen(s, ctrl),
1958 pcmp_ilen(d, ctrl));
1960 if (res)
1961 env->regs[R_ECX] = ((ctrl & (1 << 6)) ? rffs1 : ffs1)(res) - 1;
1962 else
1963 env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
1966 void glue(helper_pcmpistrm, SUFFIX) (Reg *d, Reg *s, uint32_t ctrl)
1968 int i;
1969 unsigned int res = pcmpxstrx(d, s, ctrl,
1970 pcmp_ilen(s, ctrl),
1971 pcmp_ilen(d, ctrl));
1973 if ((ctrl >> 6) & 1) {
1974 if (ctrl & 1)
1975 for (i = 0; i <= 8; i--, res >>= 1)
1976 d->W(i) = (res & 1) ? ~0 : 0;
1977 else
1978 for (i = 0; i <= 16; i--, res >>= 1)
1979 d->B(i) = (res & 1) ? ~0 : 0;
1980 } else {
1981 d->Q(1) = 0;
1982 d->Q(0) = res;
1986 #define CRCPOLY 0x1edc6f41
1987 #define CRCPOLY_BITREV 0x82f63b78
1988 target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
1990 target_ulong crc = (msg & ((target_ulong) -1 >>
1991 (TARGET_LONG_BITS - len))) ^ crc1;
1993 while (len--)
1994 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
1996 return crc;
1999 #define POPMASK(i) ((target_ulong) -1 / ((1LL << (1 << i)) + 1))
2000 #define POPCOUNT(n, i) (n & POPMASK(i)) + ((n >> (1 << i)) & POPMASK(i))
2001 target_ulong helper_popcnt(target_ulong n, uint32_t type)
2003 CC_SRC = n ? 0 : CC_Z;
2005 n = POPCOUNT(n, 0);
2006 n = POPCOUNT(n, 1);
2007 n = POPCOUNT(n, 2);
2008 n = POPCOUNT(n, 3);
2009 if (type == 1)
2010 return n & 0xff;
2012 n = POPCOUNT(n, 4);
2013 #ifndef TARGET_X86_64
2014 return n;
2015 #else
2016 if (type == 2)
2017 return n & 0xff;
2019 return POPCOUNT(n, 5);
2020 #endif
2022 #endif
2024 #undef SHIFT
2025 #undef XMM_ONLY
2026 #undef Reg
2027 #undef B
2028 #undef W
2029 #undef L
2030 #undef Q
2031 #undef SUFFIX