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