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