xref: /openbmc/qemu/target/i386/ops_sse.h (revision 314d3eff66f41f39191aaca2e5f6e3dc81480c1b)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *  Copyright (c) 2008 Intel Corporation  <andrew.zaborowski@intel.com>
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10d9ff33adSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "crypto/aes.h"
22fcf5ef2aSThomas Huth 
23fcf5ef2aSThomas Huth #if SHIFT == 0
24fcf5ef2aSThomas Huth #define Reg MMXReg
25fcf5ef2aSThomas Huth #define XMM_ONLY(...)
26fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
27fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
28fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
29fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
30fcf5ef2aSThomas Huth #define SUFFIX _mmx
31fcf5ef2aSThomas Huth #else
32fcf5ef2aSThomas Huth #define Reg ZMMReg
33fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
34fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
35fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
36fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
37fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
38b98f886cSPaolo Bonzini #if SHIFT == 1
39fcf5ef2aSThomas Huth #define SUFFIX _xmm
40b98f886cSPaolo Bonzini #else
41b98f886cSPaolo Bonzini #define SUFFIX _ymm
42b98f886cSPaolo Bonzini #endif
43fcf5ef2aSThomas Huth #endif
44fcf5ef2aSThomas Huth 
4518592d2eSPaul Brook #define LANE_WIDTH (SHIFT ? 16 : 8)
46d45b0de6SPaul Brook #define PACK_WIDTH (LANE_WIDTH / 2)
4718592d2eSPaul Brook 
4818592d2eSPaul Brook #if SHIFT == 0
4918592d2eSPaul Brook #define FPSRL(x, c) ((x) >> shift)
5018592d2eSPaul Brook #define FPSRAW(x, c) ((int16_t)(x) >> shift)
5118592d2eSPaul Brook #define FPSRAL(x, c) ((int32_t)(x) >> shift)
5218592d2eSPaul Brook #define FPSLL(x, c) ((x) << shift)
53fcf5ef2aSThomas Huth #endif
5418592d2eSPaul Brook 
55f05f9789SPaolo Bonzini void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
5618592d2eSPaul Brook {
5718592d2eSPaul Brook     int shift;
5818592d2eSPaul Brook     if (c->Q(0) > 15) {
5918592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
6018592d2eSPaul Brook             d->Q(i) = 0;
6118592d2eSPaul Brook         }
62fcf5ef2aSThomas Huth     } else {
6318592d2eSPaul Brook         shift = c->B(0);
6418592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
6518592d2eSPaul Brook             d->W(i) = FPSRL(s->W(i), shift);
6618592d2eSPaul Brook         }
67fcf5ef2aSThomas Huth     }
68fcf5ef2aSThomas Huth }
69fcf5ef2aSThomas Huth 
70f05f9789SPaolo Bonzini void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
71fcf5ef2aSThomas Huth {
72fcf5ef2aSThomas Huth     int shift;
7318592d2eSPaul Brook     if (c->Q(0) > 15) {
7418592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
7518592d2eSPaul Brook             d->Q(i) = 0;
7618592d2eSPaul Brook         }
7718592d2eSPaul Brook     } else {
7818592d2eSPaul Brook         shift = c->B(0);
7918592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
8018592d2eSPaul Brook             d->W(i) = FPSLL(s->W(i), shift);
8118592d2eSPaul Brook         }
8218592d2eSPaul Brook     }
8318592d2eSPaul Brook }
84fcf5ef2aSThomas Huth 
85f05f9789SPaolo Bonzini void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
8618592d2eSPaul Brook {
8718592d2eSPaul Brook     int shift;
8818592d2eSPaul Brook     if (c->Q(0) > 15) {
89fcf5ef2aSThomas Huth         shift = 15;
90fcf5ef2aSThomas Huth     } else {
9118592d2eSPaul Brook         shift = c->B(0);
92fcf5ef2aSThomas Huth     }
9318592d2eSPaul Brook     for (int i = 0; i < 4 << SHIFT; i++) {
9418592d2eSPaul Brook         d->W(i) = FPSRAW(s->W(i), shift);
9518592d2eSPaul Brook     }
96fcf5ef2aSThomas Huth }
97fcf5ef2aSThomas Huth 
98f05f9789SPaolo Bonzini void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     int shift;
10118592d2eSPaul Brook     if (c->Q(0) > 31) {
10218592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
10318592d2eSPaul Brook             d->Q(i) = 0;
10418592d2eSPaul Brook         }
105fcf5ef2aSThomas Huth     } else {
10618592d2eSPaul Brook         shift = c->B(0);
10718592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
10818592d2eSPaul Brook             d->L(i) = FPSRL(s->L(i), shift);
10918592d2eSPaul Brook         }
110fcf5ef2aSThomas Huth     }
111fcf5ef2aSThomas Huth }
112fcf5ef2aSThomas Huth 
113f05f9789SPaolo Bonzini void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
114fcf5ef2aSThomas Huth {
115fcf5ef2aSThomas Huth     int shift;
11618592d2eSPaul Brook     if (c->Q(0) > 31) {
11718592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
11818592d2eSPaul Brook             d->Q(i) = 0;
11918592d2eSPaul Brook         }
120fcf5ef2aSThomas Huth     } else {
12118592d2eSPaul Brook         shift = c->B(0);
12218592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
12318592d2eSPaul Brook             d->L(i) = FPSLL(s->L(i), shift);
12418592d2eSPaul Brook         }
125fcf5ef2aSThomas Huth     }
126fcf5ef2aSThomas Huth }
127fcf5ef2aSThomas Huth 
128f05f9789SPaolo Bonzini void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
129fcf5ef2aSThomas Huth {
130fcf5ef2aSThomas Huth     int shift;
13118592d2eSPaul Brook     if (c->Q(0) > 31) {
132fcf5ef2aSThomas Huth         shift = 31;
133fcf5ef2aSThomas Huth     } else {
13418592d2eSPaul Brook         shift = c->B(0);
135fcf5ef2aSThomas Huth     }
13618592d2eSPaul Brook     for (int i = 0; i < 2 << SHIFT; i++) {
13718592d2eSPaul Brook         d->L(i) = FPSRAL(s->L(i), shift);
13818592d2eSPaul Brook     }
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141f05f9789SPaolo Bonzini void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
142fcf5ef2aSThomas Huth {
143fcf5ef2aSThomas Huth     int shift;
14418592d2eSPaul Brook     if (c->Q(0) > 63) {
14518592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14618592d2eSPaul Brook             d->Q(i) = 0;
14718592d2eSPaul Brook         }
148fcf5ef2aSThomas Huth     } else {
14918592d2eSPaul Brook         shift = c->B(0);
15018592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
15118592d2eSPaul Brook             d->Q(i) = FPSRL(s->Q(i), shift);
15218592d2eSPaul Brook         }
153fcf5ef2aSThomas Huth     }
154fcf5ef2aSThomas Huth }
155fcf5ef2aSThomas Huth 
156f05f9789SPaolo Bonzini void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
157fcf5ef2aSThomas Huth {
158fcf5ef2aSThomas Huth     int shift;
15918592d2eSPaul Brook     if (c->Q(0) > 63) {
16018592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16118592d2eSPaul Brook             d->Q(i) = 0;
16218592d2eSPaul Brook         }
163fcf5ef2aSThomas Huth     } else {
16418592d2eSPaul Brook         shift = c->B(0);
16518592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16618592d2eSPaul Brook             d->Q(i) = FPSLL(s->Q(i), shift);
16718592d2eSPaul Brook         }
168fcf5ef2aSThomas Huth     }
169fcf5ef2aSThomas Huth }
170fcf5ef2aSThomas Huth 
17118592d2eSPaul Brook #if SHIFT >= 1
172f05f9789SPaolo Bonzini void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
173fcf5ef2aSThomas Huth {
17418592d2eSPaul Brook     int shift, i, j;
175fcf5ef2aSThomas Huth 
17618592d2eSPaul Brook     shift = c->L(0);
177fcf5ef2aSThomas Huth     if (shift > 16) {
178fcf5ef2aSThomas Huth         shift = 16;
179fcf5ef2aSThomas Huth     }
18018592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
181fcf5ef2aSThomas Huth         for (i = 0; i < 16 - shift; i++) {
18218592d2eSPaul Brook             d->B(j + i) = s->B(j + i + shift);
183fcf5ef2aSThomas Huth         }
184fcf5ef2aSThomas Huth         for (i = 16 - shift; i < 16; i++) {
18518592d2eSPaul Brook             d->B(j + i) = 0;
18618592d2eSPaul Brook         }
187fcf5ef2aSThomas Huth     }
188fcf5ef2aSThomas Huth }
189fcf5ef2aSThomas Huth 
190f05f9789SPaolo Bonzini void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
191fcf5ef2aSThomas Huth {
19218592d2eSPaul Brook     int shift, i, j;
193fcf5ef2aSThomas Huth 
19418592d2eSPaul Brook     shift = c->L(0);
195fcf5ef2aSThomas Huth     if (shift > 16) {
196fcf5ef2aSThomas Huth         shift = 16;
197fcf5ef2aSThomas Huth     }
19818592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
199fcf5ef2aSThomas Huth         for (i = 15; i >= shift; i--) {
20018592d2eSPaul Brook             d->B(j + i) = s->B(j + i - shift);
201fcf5ef2aSThomas Huth         }
202fcf5ef2aSThomas Huth         for (i = 0; i < shift; i++) {
20318592d2eSPaul Brook             d->B(j + i) = 0;
20418592d2eSPaul Brook         }
205fcf5ef2aSThomas Huth     }
206fcf5ef2aSThomas Huth }
207fcf5ef2aSThomas Huth #endif
208fcf5ef2aSThomas Huth 
209ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F)                        \
210fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
211fcf5ef2aSThomas Huth     {                                                           \
212ee04a3c8SPaul Brook         int n = num;                                            \
213ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
214ee04a3c8SPaul Brook             d->elem(i) = F(s->elem(i));                         \
215ee04a3c8SPaul Brook         }                                                       \
216fcf5ef2aSThomas Huth     }
217fcf5ef2aSThomas Huth 
218ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F)                        \
219f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)   \
220ee04a3c8SPaul Brook     {                                                           \
221ee04a3c8SPaul Brook         int n = num;                                            \
222ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
223ee04a3c8SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i));             \
224ee04a3c8SPaul Brook         }                                                       \
225ee04a3c8SPaul Brook     }
226ee04a3c8SPaul Brook 
227ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F)                                   \
228ee04a3c8SPaul Brook     SSE_HELPER_2(name, B, 8 << SHIFT, F)
229ee04a3c8SPaul Brook 
230fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
231ee04a3c8SPaul Brook     SSE_HELPER_2(name, W, 4 << SHIFT, F)
232fcf5ef2aSThomas Huth 
233fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
234ee04a3c8SPaul Brook     SSE_HELPER_2(name, L, 2 << SHIFT, F)
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
237ee04a3c8SPaul Brook     SSE_HELPER_2(name, Q, 1 << SHIFT, F)
238fcf5ef2aSThomas Huth 
239fcf5ef2aSThomas Huth #if SHIFT == 0
240fcf5ef2aSThomas Huth static inline int satub(int x)
241fcf5ef2aSThomas Huth {
242fcf5ef2aSThomas Huth     if (x < 0) {
243fcf5ef2aSThomas Huth         return 0;
244fcf5ef2aSThomas Huth     } else if (x > 255) {
245fcf5ef2aSThomas Huth         return 255;
246fcf5ef2aSThomas Huth     } else {
247fcf5ef2aSThomas Huth         return x;
248fcf5ef2aSThomas Huth     }
249fcf5ef2aSThomas Huth }
250fcf5ef2aSThomas Huth 
251fcf5ef2aSThomas Huth static inline int satuw(int x)
252fcf5ef2aSThomas Huth {
253fcf5ef2aSThomas Huth     if (x < 0) {
254fcf5ef2aSThomas Huth         return 0;
255fcf5ef2aSThomas Huth     } else if (x > 65535) {
256fcf5ef2aSThomas Huth         return 65535;
257fcf5ef2aSThomas Huth     } else {
258fcf5ef2aSThomas Huth         return x;
259fcf5ef2aSThomas Huth     }
260fcf5ef2aSThomas Huth }
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth static inline int satsb(int x)
263fcf5ef2aSThomas Huth {
264fcf5ef2aSThomas Huth     if (x < -128) {
265fcf5ef2aSThomas Huth         return -128;
266fcf5ef2aSThomas Huth     } else if (x > 127) {
267fcf5ef2aSThomas Huth         return 127;
268fcf5ef2aSThomas Huth     } else {
269fcf5ef2aSThomas Huth         return x;
270fcf5ef2aSThomas Huth     }
271fcf5ef2aSThomas Huth }
272fcf5ef2aSThomas Huth 
273fcf5ef2aSThomas Huth static inline int satsw(int x)
274fcf5ef2aSThomas Huth {
275fcf5ef2aSThomas Huth     if (x < -32768) {
276fcf5ef2aSThomas Huth         return -32768;
277fcf5ef2aSThomas Huth     } else if (x > 32767) {
278fcf5ef2aSThomas Huth         return 32767;
279fcf5ef2aSThomas Huth     } else {
280fcf5ef2aSThomas Huth         return x;
281fcf5ef2aSThomas Huth     }
282fcf5ef2aSThomas Huth }
283fcf5ef2aSThomas Huth 
284fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
285fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
286fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
287fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
288fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
289fcf5ef2aSThomas Huth 
290fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
291fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
292fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
293fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
294fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
295fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
296fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
297fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
298fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
299fcf5ef2aSThomas Huth 
300fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
301fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
302fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
303fcf5ef2aSThomas Huth 
304fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
305fcf5ef2aSThomas Huth #endif
306fcf5ef2aSThomas Huth 
307fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
308fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
309fcf5ef2aSThomas Huth 
310f05f9789SPaolo Bonzini #if SHIFT == 0
311f05f9789SPaolo Bonzini void glue(helper_pmulhrw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
312f05f9789SPaolo Bonzini {
313f05f9789SPaolo Bonzini     d->W(0) = FMULHRW(d->W(0), s->W(0));
314f05f9789SPaolo Bonzini     d->W(1) = FMULHRW(d->W(1), s->W(1));
315f05f9789SPaolo Bonzini     d->W(2) = FMULHRW(d->W(2), s->W(2));
316f05f9789SPaolo Bonzini     d->W(3) = FMULHRW(d->W(3), s->W(3));
317f05f9789SPaolo Bonzini }
318f05f9789SPaolo Bonzini #endif
319f05f9789SPaolo Bonzini 
320fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
321fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
322fcf5ef2aSThomas Huth 
323f05f9789SPaolo Bonzini void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
324fcf5ef2aSThomas Huth {
325e894bae8SPaul Brook     int i;
326e894bae8SPaul Brook 
327e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
328e894bae8SPaul Brook         d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2);
329e894bae8SPaul Brook     }
330fcf5ef2aSThomas Huth }
331fcf5ef2aSThomas Huth 
332f05f9789SPaolo Bonzini void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
333fcf5ef2aSThomas Huth {
334fcf5ef2aSThomas Huth     int i;
335fcf5ef2aSThomas Huth 
336fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
337e894bae8SPaul Brook         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) +
338e894bae8SPaul Brook             (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1);
339fcf5ef2aSThomas Huth     }
340fcf5ef2aSThomas Huth }
341fcf5ef2aSThomas Huth 
342fcf5ef2aSThomas Huth #if SHIFT == 0
343fcf5ef2aSThomas Huth static inline int abs1(int a)
344fcf5ef2aSThomas Huth {
345fcf5ef2aSThomas Huth     if (a < 0) {
346fcf5ef2aSThomas Huth         return -a;
347fcf5ef2aSThomas Huth     } else {
348fcf5ef2aSThomas Huth         return a;
349fcf5ef2aSThomas Huth     }
350fcf5ef2aSThomas Huth }
351fcf5ef2aSThomas Huth #endif
352f05f9789SPaolo Bonzini void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
353fcf5ef2aSThomas Huth {
354e894bae8SPaul Brook     int i;
355fcf5ef2aSThomas Huth 
356e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
357e894bae8SPaul Brook         unsigned int val = 0;
358e894bae8SPaul Brook         val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0));
359e894bae8SPaul Brook         val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1));
360e894bae8SPaul Brook         val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2));
361e894bae8SPaul Brook         val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3));
362e894bae8SPaul Brook         val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4));
363e894bae8SPaul Brook         val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5));
364e894bae8SPaul Brook         val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6));
365e894bae8SPaul Brook         val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7));
366e894bae8SPaul Brook         d->Q(i) = val;
367e894bae8SPaul Brook     }
368fcf5ef2aSThomas Huth }
369fcf5ef2aSThomas Huth 
370fd17264aSPaul Brook #if SHIFT < 2
371fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
372fcf5ef2aSThomas Huth                                   target_ulong a0)
373fcf5ef2aSThomas Huth {
374fcf5ef2aSThomas Huth     int i;
375fcf5ef2aSThomas Huth 
376fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
377fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
378fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
379fcf5ef2aSThomas Huth         }
380fcf5ef2aSThomas Huth     }
381fcf5ef2aSThomas Huth }
382fd17264aSPaul Brook #endif
383fcf5ef2aSThomas Huth 
384d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
385d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
386d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
387d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
388d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
389d45b0de6SPaul Brook     d->F(offset) = r0;                      \
390d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
391d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
392d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
393d45b0de6SPaul Brook     } while (0)
394d45b0de6SPaul Brook 
395fcf5ef2aSThomas Huth #if SHIFT == 0
396fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
397fcf5ef2aSThomas Huth {
398d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
399fcf5ef2aSThomas Huth 
400d45b0de6SPaul Brook     SHUFFLE4(W, s, s, 0);
401fcf5ef2aSThomas Huth }
402fcf5ef2aSThomas Huth #else
403f05f9789SPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
404fcf5ef2aSThomas Huth {
405d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
406d45b0de6SPaul Brook     int i;
407fcf5ef2aSThomas Huth 
408d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
409d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
410d45b0de6SPaul Brook     }
411fcf5ef2aSThomas Huth }
412fcf5ef2aSThomas Huth 
413f05f9789SPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
414fcf5ef2aSThomas Huth {
415d45b0de6SPaul Brook     uint64_t r0, r1;
416d45b0de6SPaul Brook     int i;
417fcf5ef2aSThomas Huth 
418d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
419d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
420d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
421d45b0de6SPaul Brook         d->Q(i) = r0;
422d45b0de6SPaul Brook         d->Q(i + 1) = r1;
423d45b0de6SPaul Brook         order >>= 2;
424d45b0de6SPaul Brook     }
425fcf5ef2aSThomas Huth }
426fcf5ef2aSThomas Huth 
427fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
428fcf5ef2aSThomas Huth {
429d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
430d45b0de6SPaul Brook     int i;
431fcf5ef2aSThomas Huth 
432d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
433d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
434d45b0de6SPaul Brook     }
435fcf5ef2aSThomas Huth }
436fcf5ef2aSThomas Huth 
437fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
438fcf5ef2aSThomas Huth {
439d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
440d45b0de6SPaul Brook     int i, j;
441fcf5ef2aSThomas Huth 
442d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
443d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
444d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
445d45b0de6SPaul Brook     }
446fcf5ef2aSThomas Huth }
447fcf5ef2aSThomas Huth 
448fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
449fcf5ef2aSThomas Huth {
450d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
451d45b0de6SPaul Brook     int i, j;
452fcf5ef2aSThomas Huth 
453d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
454d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
455d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
456d45b0de6SPaul Brook     }
457fcf5ef2aSThomas Huth }
458fcf5ef2aSThomas Huth #endif
459fcf5ef2aSThomas Huth 
4603403cafeSPaul Brook #if SHIFT >= 1
461fcf5ef2aSThomas Huth /* FPU ops */
462fcf5ef2aSThomas Huth /* XXX: not accurate */
463fcf5ef2aSThomas Huth 
4643403cafeSPaul Brook #define SSE_HELPER_P(name, F)                                           \
4653403cafeSPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
466f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
467fcf5ef2aSThomas Huth     {                                                                   \
4683403cafeSPaul Brook         int i;                                                          \
4693403cafeSPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
4703403cafeSPaul Brook             d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i));              \
471fcf5ef2aSThomas Huth         }                                                               \
4723403cafeSPaul Brook     }                                                                   \
4733403cafeSPaul Brook                                                                         \
4743403cafeSPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
475f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
4763403cafeSPaul Brook     {                                                                   \
4773403cafeSPaul Brook         int i;                                                          \
4783403cafeSPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
4793403cafeSPaul Brook             d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i));              \
4803403cafeSPaul Brook         }                                                               \
4813403cafeSPaul Brook     }
4823403cafeSPaul Brook 
4833403cafeSPaul Brook #if SHIFT == 1
4843403cafeSPaul Brook 
4853403cafeSPaul Brook #define SSE_HELPER_S(name, F)                                           \
4863403cafeSPaul Brook     SSE_HELPER_P(name, F)                                               \
487fcf5ef2aSThomas Huth                                                                         \
488f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
489fcf5ef2aSThomas Huth     {                                                                   \
4901de9e7e6SPaolo Bonzini         int i;                                                          \
4913403cafeSPaul Brook         d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0));                  \
4921de9e7e6SPaolo Bonzini         for (i = 1; i < 2 << SHIFT; i++) {                              \
4931de9e7e6SPaolo Bonzini             d->ZMM_L(i) = v->ZMM_L(i);                                  \
4941de9e7e6SPaolo Bonzini         }                                                               \
495fcf5ef2aSThomas Huth     }                                                                   \
496fcf5ef2aSThomas Huth                                                                         \
497f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
498fcf5ef2aSThomas Huth     {                                                                   \
4991de9e7e6SPaolo Bonzini         int i;                                                          \
5003403cafeSPaul Brook         d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0));                  \
5011de9e7e6SPaolo Bonzini         for (i = 1; i < 1 << SHIFT; i++) {                              \
5021de9e7e6SPaolo Bonzini             d->ZMM_Q(i) = v->ZMM_Q(i);                                  \
5031de9e7e6SPaolo Bonzini         }                                                               \
504fcf5ef2aSThomas Huth     }
505fcf5ef2aSThomas Huth 
5063403cafeSPaul Brook #else
5073403cafeSPaul Brook 
5083403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F)
5093403cafeSPaul Brook 
5103403cafeSPaul Brook #endif
5113403cafeSPaul Brook 
512fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
513fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
514fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
515fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
516fcf5ef2aSThomas Huth 
517fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
518fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
519fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
520fcf5ef2aSThomas Huth  */
521fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
522fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
523fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
524fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
525fcf5ef2aSThomas Huth 
526fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
527fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
528fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
529fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
530fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
531fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
532fcf5ef2aSThomas Huth 
5333403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
5343403cafeSPaul Brook {
5353403cafeSPaul Brook     int i;
5363403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
5373403cafeSPaul Brook         d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status);
5383403cafeSPaul Brook     }
5393403cafeSPaul Brook }
5403403cafeSPaul Brook 
5413403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
5423403cafeSPaul Brook {
5433403cafeSPaul Brook     int i;
5443403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
5453403cafeSPaul Brook         d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status);
5463403cafeSPaul Brook     }
5473403cafeSPaul Brook }
5483403cafeSPaul Brook 
5493403cafeSPaul Brook #if SHIFT == 1
550620f7556SPaolo Bonzini void helper_sqrtss(CPUX86State *env, Reg *d, Reg *v, Reg *s)
5513403cafeSPaul Brook {
552620f7556SPaolo Bonzini     int i;
5533403cafeSPaul Brook     d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status);
554620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
555620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
556620f7556SPaolo Bonzini     }
5573403cafeSPaul Brook }
5583403cafeSPaul Brook 
559620f7556SPaolo Bonzini void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *v, Reg *s)
5603403cafeSPaul Brook {
561620f7556SPaolo Bonzini     int i;
5623403cafeSPaul Brook     d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status);
563620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
564620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
565620f7556SPaolo Bonzini     }
5663403cafeSPaul Brook }
5673403cafeSPaul Brook #endif
568fcf5ef2aSThomas Huth 
569fcf5ef2aSThomas Huth /* float to float conversions */
570ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
571fcf5ef2aSThomas Huth {
572fd17264aSPaul Brook     int i;
573fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
574fd17264aSPaul Brook         d->ZMM_D(i) = float32_to_float64(s->ZMM_S(i), &env->sse_status);
575fd17264aSPaul Brook     }
576fcf5ef2aSThomas Huth }
577fcf5ef2aSThomas Huth 
578ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
579fcf5ef2aSThomas Huth {
580fd17264aSPaul Brook     int i;
581fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
582fd17264aSPaul Brook          d->ZMM_S(i) = float64_to_float32(s->ZMM_D(i), &env->sse_status);
583fd17264aSPaul Brook     }
584fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
585fd17264aSPaul Brook          d->Q(i) = 0;
586fd17264aSPaul Brook     }
587fcf5ef2aSThomas Huth }
588fcf5ef2aSThomas Huth 
589fd17264aSPaul Brook #if SHIFT == 1
590620f7556SPaolo Bonzini void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)
591fcf5ef2aSThomas Huth {
592620f7556SPaolo Bonzini     int i;
593fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
594620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
595620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
596620f7556SPaolo Bonzini     }
597fcf5ef2aSThomas Huth }
598fcf5ef2aSThomas Huth 
599620f7556SPaolo Bonzini void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)
600fcf5ef2aSThomas Huth {
601620f7556SPaolo Bonzini     int i;
602fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
603620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
604620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
605620f7556SPaolo Bonzini     }
606fcf5ef2aSThomas Huth }
607fd17264aSPaul Brook #endif
608fcf5ef2aSThomas Huth 
609fcf5ef2aSThomas Huth /* integer to float */
610ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
611fcf5ef2aSThomas Huth {
612fd17264aSPaul Brook     int i;
613fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
614fd17264aSPaul Brook         d->ZMM_S(i) = int32_to_float32(s->ZMM_L(i), &env->sse_status);
615fd17264aSPaul Brook     }
616fcf5ef2aSThomas Huth }
617fcf5ef2aSThomas Huth 
618ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
619fcf5ef2aSThomas Huth {
620fd17264aSPaul Brook     int i;
621fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
622fd17264aSPaul Brook         int32_t l = s->ZMM_L(i);
623fd17264aSPaul Brook         d->ZMM_D(i) = int32_to_float64(l, &env->sse_status);
624fd17264aSPaul Brook     }
625fcf5ef2aSThomas Huth }
626fcf5ef2aSThomas Huth 
627fd17264aSPaul Brook #if SHIFT == 1
628fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
629fcf5ef2aSThomas Huth {
630fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
631fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
632fcf5ef2aSThomas Huth }
633fcf5ef2aSThomas Huth 
634fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
635fcf5ef2aSThomas Huth {
636fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
637fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
638fcf5ef2aSThomas Huth }
639fcf5ef2aSThomas Huth 
640fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
641fcf5ef2aSThomas Huth {
642fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
643fcf5ef2aSThomas Huth }
644fcf5ef2aSThomas Huth 
645fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
646fcf5ef2aSThomas Huth {
647fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
648fcf5ef2aSThomas Huth }
649fcf5ef2aSThomas Huth 
650fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
651fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
652fcf5ef2aSThomas Huth {
653fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
654fcf5ef2aSThomas Huth }
655fcf5ef2aSThomas Huth 
656fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
657fcf5ef2aSThomas Huth {
658fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
659fcf5ef2aSThomas Huth }
660fcf5ef2aSThomas Huth #endif
661fcf5ef2aSThomas Huth 
662fd17264aSPaul Brook #endif
663fd17264aSPaul Brook 
664fcf5ef2aSThomas Huth /* float to integer */
6651e8a98b5SPeter Maydell 
666fd17264aSPaul Brook #if SHIFT == 1
6671e8a98b5SPeter Maydell /*
6681e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
6691e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
6701e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
6711e8a98b5SPeter Maydell  */
6721e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
6731e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
6741e8a98b5SPeter Maydell     {                                                                   \
6751e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
6761e8a98b5SPeter Maydell         RETTYPE r;                                                      \
6771e8a98b5SPeter Maydell                                                                         \
6781e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
6791e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
6801e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
6811e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
6821e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
6831e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
6841e8a98b5SPeter Maydell         }                                                               \
6851e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
6861e8a98b5SPeter Maydell         return r;                                                       \
6871e8a98b5SPeter Maydell     }
6881e8a98b5SPeter Maydell 
6891e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
6901e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
6911e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
6921e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
6931e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
6941e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
6951e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
6961e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
697fd17264aSPaul Brook #endif
6981e8a98b5SPeter Maydell 
699ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
700fcf5ef2aSThomas Huth {
701fd17264aSPaul Brook     int i;
702fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
703fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32(s->ZMM_S(i), &env->sse_status);
704fd17264aSPaul Brook     }
705fcf5ef2aSThomas Huth }
706fcf5ef2aSThomas Huth 
707ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
708fcf5ef2aSThomas Huth {
709fd17264aSPaul Brook     int i;
710fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
711fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32(s->ZMM_D(i), &env->sse_status);
712fd17264aSPaul Brook     }
713fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
714fd17264aSPaul Brook          d->Q(i) = 0;
715fd17264aSPaul Brook     }
716fcf5ef2aSThomas Huth }
717fcf5ef2aSThomas Huth 
718fd17264aSPaul Brook #if SHIFT == 1
719fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
720fcf5ef2aSThomas Huth {
7211e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7221e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
723fcf5ef2aSThomas Huth }
724fcf5ef2aSThomas Huth 
725fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
726fcf5ef2aSThomas Huth {
7271e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7281e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
729fcf5ef2aSThomas Huth }
730fcf5ef2aSThomas Huth 
731fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
732fcf5ef2aSThomas Huth {
7331e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
734fcf5ef2aSThomas Huth }
735fcf5ef2aSThomas Huth 
736fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
737fcf5ef2aSThomas Huth {
7381e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
739fcf5ef2aSThomas Huth }
740fcf5ef2aSThomas Huth 
741fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
742fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
743fcf5ef2aSThomas Huth {
7441e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
745fcf5ef2aSThomas Huth }
746fcf5ef2aSThomas Huth 
747fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
748fcf5ef2aSThomas Huth {
7491e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
750fcf5ef2aSThomas Huth }
751fcf5ef2aSThomas Huth #endif
752fd17264aSPaul Brook #endif
753fcf5ef2aSThomas Huth 
754fcf5ef2aSThomas Huth /* float to integer truncated */
755ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
756fcf5ef2aSThomas Huth {
757fd17264aSPaul Brook     int i;
758fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
759fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32_round_to_zero(s->ZMM_S(i),
760fd17264aSPaul Brook                                                          &env->sse_status);
761fd17264aSPaul Brook     }
762fcf5ef2aSThomas Huth }
763fcf5ef2aSThomas Huth 
764ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
765fcf5ef2aSThomas Huth {
766fd17264aSPaul Brook     int i;
767fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
768fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32_round_to_zero(s->ZMM_D(i),
769fd17264aSPaul Brook                                                          &env->sse_status);
770fd17264aSPaul Brook     }
771fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
772fd17264aSPaul Brook          d->Q(i) = 0;
773fd17264aSPaul Brook     }
774fcf5ef2aSThomas Huth }
775fcf5ef2aSThomas Huth 
776fd17264aSPaul Brook #if SHIFT == 1
777fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
778fcf5ef2aSThomas Huth {
7791e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
7801e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
781fcf5ef2aSThomas Huth }
782fcf5ef2aSThomas Huth 
783fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
784fcf5ef2aSThomas Huth {
7851e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
7861e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
787fcf5ef2aSThomas Huth }
788fcf5ef2aSThomas Huth 
789fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
790fcf5ef2aSThomas Huth {
7911e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
792fcf5ef2aSThomas Huth }
793fcf5ef2aSThomas Huth 
794fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
795fcf5ef2aSThomas Huth {
7961e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
797fcf5ef2aSThomas Huth }
798fcf5ef2aSThomas Huth 
799fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
800fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
801fcf5ef2aSThomas Huth {
8021e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
803fcf5ef2aSThomas Huth }
804fcf5ef2aSThomas Huth 
805fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
806fcf5ef2aSThomas Huth {
8071e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
808fcf5ef2aSThomas Huth }
809fcf5ef2aSThomas Huth #endif
810fd17264aSPaul Brook #endif
811fcf5ef2aSThomas Huth 
812ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
813fcf5ef2aSThomas Huth {
814418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8153403cafeSPaul Brook     int i;
8163403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8173403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one,
8183403cafeSPaul Brook                                   float32_sqrt(s->ZMM_S(i), &env->sse_status),
819fcf5ef2aSThomas Huth                                   &env->sse_status);
8203403cafeSPaul Brook     }
821418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
822fcf5ef2aSThomas Huth }
823fcf5ef2aSThomas Huth 
824fd17264aSPaul Brook #if SHIFT == 1
825620f7556SPaolo Bonzini void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)
826fcf5ef2aSThomas Huth {
827418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
828620f7556SPaolo Bonzini     int i;
829fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
830fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
831fcf5ef2aSThomas Huth                               &env->sse_status);
832418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
833620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
834620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
835620f7556SPaolo Bonzini     }
836fcf5ef2aSThomas Huth }
837fd17264aSPaul Brook #endif
838fcf5ef2aSThomas Huth 
839ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
840fcf5ef2aSThomas Huth {
841418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8423403cafeSPaul Brook     int i;
8433403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8443403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status);
8453403cafeSPaul Brook     }
846418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
847fcf5ef2aSThomas Huth }
848fcf5ef2aSThomas Huth 
849fd17264aSPaul Brook #if SHIFT == 1
850620f7556SPaolo Bonzini void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)
851fcf5ef2aSThomas Huth {
852418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
853620f7556SPaolo Bonzini     int i;
854fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
855620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
856620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
857620f7556SPaolo Bonzini     }
858418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
859fcf5ef2aSThomas Huth }
860fd17264aSPaul Brook #endif
861fcf5ef2aSThomas Huth 
862fd17264aSPaul Brook #if SHIFT == 1
863fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
864fcf5ef2aSThomas Huth {
865fcf5ef2aSThomas Huth     uint64_t mask;
866fcf5ef2aSThomas Huth 
867fcf5ef2aSThomas Huth     if (len == 0) {
868fcf5ef2aSThomas Huth         mask = ~0LL;
869fcf5ef2aSThomas Huth     } else {
870fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
871fcf5ef2aSThomas Huth     }
872fcf5ef2aSThomas Huth     return (src >> shift) & mask;
873fcf5ef2aSThomas Huth }
874fcf5ef2aSThomas Huth 
875fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
876fcf5ef2aSThomas Huth {
877034668c3SPaolo Bonzini     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1) & 63, s->ZMM_B(0) & 63);
878fcf5ef2aSThomas Huth }
879fcf5ef2aSThomas Huth 
880fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
881fcf5ef2aSThomas Huth {
882fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
883fcf5ef2aSThomas Huth }
884fcf5ef2aSThomas Huth 
885ca4b1b43SPaolo Bonzini static inline uint64_t helper_insertq(uint64_t dest, uint64_t src, int shift, int len)
886fcf5ef2aSThomas Huth {
887fcf5ef2aSThomas Huth     uint64_t mask;
888fcf5ef2aSThomas Huth 
889fcf5ef2aSThomas Huth     if (len == 0) {
890fcf5ef2aSThomas Huth         mask = ~0ULL;
891fcf5ef2aSThomas Huth     } else {
892fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
893fcf5ef2aSThomas Huth     }
894ca4b1b43SPaolo Bonzini     return (dest & ~(mask << shift)) | ((src & mask) << shift);
895fcf5ef2aSThomas Huth }
896fcf5ef2aSThomas Huth 
897fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
898fcf5ef2aSThomas Huth {
899ca4b1b43SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), s->ZMM_Q(0), s->ZMM_B(9) & 63, s->ZMM_B(8) & 63);
900fcf5ef2aSThomas Huth }
901fcf5ef2aSThomas Huth 
902ca4b1b43SPaolo Bonzini void helper_insertq_i(CPUX86State *env, ZMMReg *d, ZMMReg *s, int index, int length)
903fcf5ef2aSThomas Huth {
904ca4b1b43SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), s->ZMM_Q(0), index, length);
905fcf5ef2aSThomas Huth }
906fd17264aSPaul Brook #endif
907fcf5ef2aSThomas Huth 
9086567ffb4SPaul Brook #define SSE_HELPER_HPS(name, F)  \
909f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9106567ffb4SPaul Brook {                                                                 \
9116567ffb4SPaul Brook     float32 r[2 << SHIFT];                                        \
9126567ffb4SPaul Brook     int i, j, k;                                                  \
9136567ffb4SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {            \
9146567ffb4SPaul Brook         for (i = j = 0; j < 4; i++, j += 2) {                     \
9156567ffb4SPaul Brook             r[i + k] = F(v->ZMM_S(j + k), v->ZMM_S(j + k + 1), &env->sse_status); \
9166567ffb4SPaul Brook         }                                                         \
9176567ffb4SPaul Brook         for (j = 0; j < 4; i++, j += 2) {                         \
9186567ffb4SPaul Brook             r[i + k] = F(s->ZMM_S(j + k), s->ZMM_S(j + k + 1), &env->sse_status); \
9196567ffb4SPaul Brook         }                                                         \
9206567ffb4SPaul Brook     }                                                             \
9216567ffb4SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                            \
9226567ffb4SPaul Brook         d->ZMM_S(i) = r[i];                                       \
9236567ffb4SPaul Brook     }                                                             \
924fcf5ef2aSThomas Huth }
925fcf5ef2aSThomas Huth 
9266567ffb4SPaul Brook SSE_HELPER_HPS(haddps, float32_add)
9276567ffb4SPaul Brook SSE_HELPER_HPS(hsubps, float32_sub)
928fcf5ef2aSThomas Huth 
9296567ffb4SPaul Brook #define SSE_HELPER_HPD(name, F)  \
930f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9316567ffb4SPaul Brook {                                                                 \
9326567ffb4SPaul Brook     float64 r[1 << SHIFT];                                        \
9336567ffb4SPaul Brook     int i, j, k;                                                  \
9346567ffb4SPaul Brook     for (k = 0; k < 1 << SHIFT; k += LANE_WIDTH / 8) {            \
9356567ffb4SPaul Brook         for (i = j = 0; j < 2; i++, j += 2) {                     \
9366567ffb4SPaul Brook             r[i + k] = F(v->ZMM_D(j + k), v->ZMM_D(j + k + 1), &env->sse_status); \
9376567ffb4SPaul Brook         }                                                         \
9386567ffb4SPaul Brook         for (j = 0; j < 2; i++, j += 2) {                         \
9396567ffb4SPaul Brook             r[i + k] = F(s->ZMM_D(j + k), s->ZMM_D(j + k + 1), &env->sse_status); \
9406567ffb4SPaul Brook         }                                                         \
9416567ffb4SPaul Brook     }                                                             \
9426567ffb4SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {                            \
9436567ffb4SPaul Brook         d->ZMM_D(i) = r[i];                                       \
9446567ffb4SPaul Brook     }                                                             \
945fcf5ef2aSThomas Huth }
946fcf5ef2aSThomas Huth 
9476567ffb4SPaul Brook SSE_HELPER_HPD(haddpd, float64_add)
9486567ffb4SPaul Brook SSE_HELPER_HPD(hsubpd, float64_sub)
949fcf5ef2aSThomas Huth 
950f05f9789SPaolo Bonzini void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
951fcf5ef2aSThomas Huth {
9523403cafeSPaul Brook     int i;
9533403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 2) {
9543403cafeSPaul Brook         d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
9553403cafeSPaul Brook         d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
9563403cafeSPaul Brook     }
957fcf5ef2aSThomas Huth }
958fcf5ef2aSThomas Huth 
959f05f9789SPaolo Bonzini void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
960fcf5ef2aSThomas Huth {
9613403cafeSPaul Brook     int i;
9623403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
9633403cafeSPaul Brook         d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status);
9643403cafeSPaul Brook         d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status);
9653403cafeSPaul Brook     }
966fcf5ef2aSThomas Huth }
967fcf5ef2aSThomas Huth 
968cbf4ad54SPaul Brook #define SSE_HELPER_CMP_P(name, F, C)                                    \
969cbf4ad54SPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
970f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
971fcf5ef2aSThomas Huth     {                                                                   \
972cbf4ad54SPaul Brook         int i;                                                          \
973cbf4ad54SPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
974cbf4ad54SPaul Brook             d->ZMM_L(i) = C(F(32, v->ZMM_S(i), s->ZMM_S(i))) ? -1 : 0;  \
975cbf4ad54SPaul Brook         }                                                               \
976fcf5ef2aSThomas Huth     }                                                                   \
977fcf5ef2aSThomas Huth                                                                         \
978cbf4ad54SPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
979f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
980cbf4ad54SPaul Brook     {                                                                   \
981cbf4ad54SPaul Brook         int i;                                                          \
982cbf4ad54SPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
983cbf4ad54SPaul Brook             d->ZMM_Q(i) = C(F(64, v->ZMM_D(i), s->ZMM_D(i))) ? -1 : 0;  \
984cbf4ad54SPaul Brook         }                                                               \
985cbf4ad54SPaul Brook     }
986cbf4ad54SPaul Brook 
987cbf4ad54SPaul Brook #if SHIFT == 1
988cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C)                                          \
989cbf4ad54SPaul Brook     SSE_HELPER_CMP_P(name, F, C)                                            \
990f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
991fcf5ef2aSThomas Huth     {                                                                       \
9921de9e7e6SPaolo Bonzini         int i;                                                              \
993cbf4ad54SPaul Brook         d->ZMM_L(0) = C(F(32, v->ZMM_S(0), s->ZMM_S(0))) ? -1 : 0;          \
9941de9e7e6SPaolo Bonzini         for (i = 1; i < 2 << SHIFT; i++) {                                  \
9951de9e7e6SPaolo Bonzini             d->ZMM_L(i) = v->ZMM_L(i);                                      \
9961de9e7e6SPaolo Bonzini         }                                                                   \
997fcf5ef2aSThomas Huth     }                                                                       \
998fcf5ef2aSThomas Huth                                                                             \
999f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1000fcf5ef2aSThomas Huth     {                                                                       \
10011de9e7e6SPaolo Bonzini         int i;                                                              \
1002cbf4ad54SPaul Brook         d->ZMM_Q(0) = C(F(64, v->ZMM_D(0), s->ZMM_D(0))) ? -1 : 0;          \
10031de9e7e6SPaolo Bonzini         for (i = 1; i < 1 << SHIFT; i++) {                                  \
10041de9e7e6SPaolo Bonzini             d->ZMM_Q(i) = v->ZMM_Q(i);                                      \
10051de9e7e6SPaolo Bonzini         }                                                                   \
1006fcf5ef2aSThomas Huth     }
1007fcf5ef2aSThomas Huth 
10086e0cac78SPaolo Bonzini static inline bool FPU_EQU(FloatRelation x)
10096e0cac78SPaolo Bonzini {
10106e0cac78SPaolo Bonzini     return (x == float_relation_equal || x == float_relation_unordered);
10116e0cac78SPaolo Bonzini }
10126e0cac78SPaolo Bonzini static inline bool FPU_GE(FloatRelation x)
10136e0cac78SPaolo Bonzini {
10146e0cac78SPaolo Bonzini     return (x == float_relation_equal || x == float_relation_greater);
10156e0cac78SPaolo Bonzini }
1016cbf4ad54SPaul Brook #define FPU_EQ(x) (x == float_relation_equal)
1017cbf4ad54SPaul Brook #define FPU_LT(x) (x == float_relation_less)
1018cbf4ad54SPaul Brook #define FPU_LE(x) (x <= float_relation_equal)
10196e0cac78SPaolo Bonzini #define FPU_GT(x) (x == float_relation_greater)
1020cbf4ad54SPaul Brook #define FPU_UNORD(x) (x == float_relation_unordered)
10216e0cac78SPaolo Bonzini /* We must make sure we evaluate the argument in case it is a signalling NAN */
10226e0cac78SPaolo Bonzini #define FPU_FALSE(x) (x == float_relation_equal && 0)
1023fcf5ef2aSThomas Huth 
1024cbf4ad54SPaul Brook #define FPU_CMPQ(size, a, b) \
1025cbf4ad54SPaul Brook     float ## size ## _compare_quiet(a, b, &env->sse_status)
1026cbf4ad54SPaul Brook #define FPU_CMPS(size, a, b) \
1027cbf4ad54SPaul Brook     float ## size ## _compare(a, b, &env->sse_status)
1028cbf4ad54SPaul Brook 
1029cbf4ad54SPaul Brook #else
1030cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C) SSE_HELPER_CMP_P(name, F, C)
1031cbf4ad54SPaul Brook #endif
1032cbf4ad54SPaul Brook 
1033cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpeq, FPU_CMPQ, FPU_EQ)
1034cbf4ad54SPaul Brook SSE_HELPER_CMP(cmplt, FPU_CMPS, FPU_LT)
1035cbf4ad54SPaul Brook SSE_HELPER_CMP(cmple, FPU_CMPS, FPU_LE)
1036cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpunord, FPU_CMPQ,  FPU_UNORD)
1037cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpneq, FPU_CMPQ, !FPU_EQ)
1038cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnlt, FPU_CMPS, !FPU_LT)
1039cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnle, FPU_CMPS, !FPU_LE)
1040cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpord, FPU_CMPQ, !FPU_UNORD)
1041cbf4ad54SPaul Brook 
10426e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpequ, FPU_CMPQ, FPU_EQU)
10436e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnge, FPU_CMPS, !FPU_GE)
10446e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpngt, FPU_CMPS, !FPU_GT)
10456e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpfalse, FPU_CMPQ,  FPU_FALSE)
10466e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnequ, FPU_CMPQ, !FPU_EQU)
10476e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpge, FPU_CMPS, FPU_GE)
10486e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpgt, FPU_CMPS, FPU_GT)
10496e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmptrue, FPU_CMPQ,  !FPU_FALSE)
10506e0cac78SPaolo Bonzini 
10516e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpeqs, FPU_CMPS, FPU_EQ)
10526e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpltq, FPU_CMPQ, FPU_LT)
10536e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpleq, FPU_CMPQ, FPU_LE)
10546e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpunords, FPU_CMPS,  FPU_UNORD)
10556e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpneqq, FPU_CMPS, !FPU_EQ)
10566e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnltq, FPU_CMPQ, !FPU_LT)
10576e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnleq, FPU_CMPQ, !FPU_LE)
10586e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpords, FPU_CMPS, !FPU_UNORD)
10596e0cac78SPaolo Bonzini 
10606e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpequs, FPU_CMPS, FPU_EQU)
10616e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpngeq, FPU_CMPQ, !FPU_GE)
10626e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpngtq, FPU_CMPQ, !FPU_GT)
10636e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpfalses, FPU_CMPS,  FPU_FALSE)
10646e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnequs, FPU_CMPS, !FPU_EQU)
10656e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpgeq, FPU_CMPQ, FPU_GE)
10666e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpgtq, FPU_CMPQ, FPU_GT)
10676e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmptrues, FPU_CMPS,  !FPU_FALSE)
10686e0cac78SPaolo Bonzini 
1069cbf4ad54SPaul Brook #undef SSE_HELPER_CMP
1070fcf5ef2aSThomas Huth 
1071fd17264aSPaul Brook #if SHIFT == 1
1072fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1073fcf5ef2aSThomas Huth 
1074fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1075fcf5ef2aSThomas Huth {
107671bfd65cSRichard Henderson     FloatRelation ret;
1077fcf5ef2aSThomas Huth     float32 s0, s1;
1078fcf5ef2aSThomas Huth 
1079fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1080fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1081fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1082fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1083fcf5ef2aSThomas Huth }
1084fcf5ef2aSThomas Huth 
1085fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1086fcf5ef2aSThomas Huth {
108771bfd65cSRichard Henderson     FloatRelation ret;
1088fcf5ef2aSThomas Huth     float32 s0, s1;
1089fcf5ef2aSThomas Huth 
1090fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1091fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1092fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1093fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1094fcf5ef2aSThomas Huth }
1095fcf5ef2aSThomas Huth 
1096fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1097fcf5ef2aSThomas Huth {
109871bfd65cSRichard Henderson     FloatRelation ret;
1099fcf5ef2aSThomas Huth     float64 d0, d1;
1100fcf5ef2aSThomas Huth 
1101fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1102fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1103fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1104fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1105fcf5ef2aSThomas Huth }
1106fcf5ef2aSThomas Huth 
1107fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1108fcf5ef2aSThomas Huth {
110971bfd65cSRichard Henderson     FloatRelation ret;
1110fcf5ef2aSThomas Huth     float64 d0, d1;
1111fcf5ef2aSThomas Huth 
1112fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1113fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1114fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1115fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1116fcf5ef2aSThomas Huth }
1117fd17264aSPaul Brook #endif
1118fcf5ef2aSThomas Huth 
1119ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1120fcf5ef2aSThomas Huth {
1121fd17264aSPaul Brook     uint32_t mask;
1122fd17264aSPaul Brook     int i;
1123fcf5ef2aSThomas Huth 
1124fd17264aSPaul Brook     mask = 0;
1125fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1126fd17264aSPaul Brook         mask |= (s->ZMM_L(i) >> (31 - i)) & (1 << i);
1127fd17264aSPaul Brook     }
1128fd17264aSPaul Brook     return mask;
1129fcf5ef2aSThomas Huth }
1130fcf5ef2aSThomas Huth 
1131ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1132fcf5ef2aSThomas Huth {
1133fd17264aSPaul Brook     uint32_t mask;
1134fd17264aSPaul Brook     int i;
1135fcf5ef2aSThomas Huth 
1136fd17264aSPaul Brook     mask = 0;
1137fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1138fd17264aSPaul Brook         mask |= (s->ZMM_Q(i) >> (63 - i)) & (1 << i);
1139fd17264aSPaul Brook     }
1140fd17264aSPaul Brook     return mask;
1141fcf5ef2aSThomas Huth }
1142fcf5ef2aSThomas Huth 
1143fcf5ef2aSThomas Huth #endif
1144fcf5ef2aSThomas Huth 
1145d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1146d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1147f05f9789SPaolo Bonzini         Reg *d, Reg *v, Reg *s)                               \
1148d45b0de6SPaul Brook {                                                             \
1149d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1150d45b0de6SPaul Brook     int j, k;                                                 \
1151d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1152d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1153d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1154d45b0de6SPaul Brook         }                                                     \
1155d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1156d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1157d45b0de6SPaul Brook         }                                                     \
1158d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1159d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1160d45b0de6SPaul Brook         }                                                     \
1161d45b0de6SPaul Brook     }                                                         \
1162fcf5ef2aSThomas Huth }
1163fcf5ef2aSThomas Huth 
1164d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1165d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1166fcf5ef2aSThomas Huth 
1167f05f9789SPaolo Bonzini void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1168fcf5ef2aSThomas Huth {
1169d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1170d45b0de6SPaul Brook     int j, k;
1171fcf5ef2aSThomas Huth 
1172d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1173d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1174d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1175d45b0de6SPaul Brook         }
1176d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1177d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1178d45b0de6SPaul Brook         }
1179d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1180d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1181d45b0de6SPaul Brook         }
1182d45b0de6SPaul Brook     }
1183fcf5ef2aSThomas Huth }
1184fcf5ef2aSThomas Huth 
1185fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1186fcf5ef2aSThomas Huth                                                                         \
1187fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1188f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1189fcf5ef2aSThomas Huth     {                                                                   \
1190d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1191d45b0de6SPaul Brook         int j, i;                                                       \
1192fcf5ef2aSThomas Huth                                                                         \
1193d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1194d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1195d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1196d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1197d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1198d45b0de6SPaul Brook             }                                                           \
1199d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1200d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1201d45b0de6SPaul Brook             }                                                           \
1202d45b0de6SPaul Brook         }                                                               \
1203fcf5ef2aSThomas Huth     }                                                                   \
1204fcf5ef2aSThomas Huth                                                                         \
1205fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1206f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1207fcf5ef2aSThomas Huth     {                                                                   \
1208d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1209d45b0de6SPaul Brook         int j, i;                                                       \
1210fcf5ef2aSThomas Huth                                                                         \
1211d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1212d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1213d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1214d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1215d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1216d45b0de6SPaul Brook             }                                                           \
1217d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1218d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1219d45b0de6SPaul Brook             }                                                           \
1220d45b0de6SPaul Brook         }                                                               \
1221fcf5ef2aSThomas Huth     }                                                                   \
1222fcf5ef2aSThomas Huth                                                                         \
1223fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1224f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1225fcf5ef2aSThomas Huth     {                                                                   \
1226d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1227d45b0de6SPaul Brook         int j, i;                                                       \
1228fcf5ef2aSThomas Huth                                                                         \
1229d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1230d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1231d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1232d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1233d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1234d45b0de6SPaul Brook             }                                                           \
1235d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1236d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1237d45b0de6SPaul Brook             }                                                           \
1238d45b0de6SPaul Brook         }                                                               \
1239fcf5ef2aSThomas Huth     }                                                                   \
1240fcf5ef2aSThomas Huth                                                                         \
1241fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1242d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1243f05f9789SPaolo Bonzini                         CPUX86State *env, Reg *d, Reg *v, Reg *s)       \
1244fcf5ef2aSThomas Huth              {                                                          \
1245d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1246d45b0de6SPaul Brook                  int i;                                                 \
1247fcf5ef2aSThomas Huth                                                                         \
1248d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1249d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1250d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1251d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1252d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1253d45b0de6SPaul Brook                  }                                                      \
1254fcf5ef2aSThomas Huth              }                                                          \
1255fcf5ef2aSThomas Huth                                                                         )
1256fcf5ef2aSThomas Huth 
1257fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1258fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1259fcf5ef2aSThomas Huth 
1260d45b0de6SPaul Brook #undef PACK_WIDTH
1261d45b0de6SPaul Brook #undef PACK_HELPER_B
1262d45b0de6SPaul Brook #undef UNPCK_OP
1263d45b0de6SPaul Brook 
1264d45b0de6SPaul Brook 
1265fcf5ef2aSThomas Huth /* 3DNow! float ops */
1266fcf5ef2aSThomas Huth #if SHIFT == 0
1267fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1268fcf5ef2aSThomas Huth {
1269fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1270fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1271fcf5ef2aSThomas Huth }
1272fcf5ef2aSThomas Huth 
1273fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1274fcf5ef2aSThomas Huth {
1275fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1276fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1277fcf5ef2aSThomas Huth }
1278fcf5ef2aSThomas Huth 
1279fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1280fcf5ef2aSThomas Huth {
1281fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1282fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1283fcf5ef2aSThomas Huth }
1284fcf5ef2aSThomas Huth 
1285fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1286fcf5ef2aSThomas Huth {
1287fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1288fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1289fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1290fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1291fcf5ef2aSThomas Huth }
1292fcf5ef2aSThomas Huth 
1293fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1294fcf5ef2aSThomas Huth {
129525bdec79SPaolo Bonzini     float32 r;
1296fcf5ef2aSThomas Huth 
129725bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
129825bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
129925bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1300fcf5ef2aSThomas Huth }
1301fcf5ef2aSThomas Huth 
1302fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1303fcf5ef2aSThomas Huth {
1304fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1305fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1306fcf5ef2aSThomas Huth }
1307fcf5ef2aSThomas Huth 
1308fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1309fcf5ef2aSThomas Huth {
1310fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1311fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1312fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1313fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1314fcf5ef2aSThomas Huth }
1315fcf5ef2aSThomas Huth 
1316fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1317fcf5ef2aSThomas Huth {
1318fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1319fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1320fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1321fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1322fcf5ef2aSThomas Huth }
1323fcf5ef2aSThomas Huth 
1324fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1325fcf5ef2aSThomas Huth {
1326fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1327fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1328fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1329fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1330fcf5ef2aSThomas Huth }
1331fcf5ef2aSThomas Huth 
1332fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1333fcf5ef2aSThomas Huth {
1334fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1335fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1336fcf5ef2aSThomas Huth     }
1337fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1338fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1339fcf5ef2aSThomas Huth     }
1340fcf5ef2aSThomas Huth }
1341fcf5ef2aSThomas Huth 
1342fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1343fcf5ef2aSThomas Huth {
1344fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1345fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1346fcf5ef2aSThomas Huth     }
1347fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1348fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1349fcf5ef2aSThomas Huth     }
1350fcf5ef2aSThomas Huth }
1351fcf5ef2aSThomas Huth 
1352fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1353fcf5ef2aSThomas Huth {
1354fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1355fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1356fcf5ef2aSThomas Huth }
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1359fcf5ef2aSThomas Huth {
136025bdec79SPaolo Bonzini     float32 r;
1361fcf5ef2aSThomas Huth 
136225bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
136325bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
136425bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1365fcf5ef2aSThomas Huth }
1366fcf5ef2aSThomas Huth 
1367fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1368fcf5ef2aSThomas Huth {
136925bdec79SPaolo Bonzini     float32 r;
1370fcf5ef2aSThomas Huth 
137125bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
137225bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
137325bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1374fcf5ef2aSThomas Huth }
1375fcf5ef2aSThomas Huth 
1376fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1377fcf5ef2aSThomas Huth {
1378fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1379fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1380fcf5ef2aSThomas Huth }
1381fcf5ef2aSThomas Huth 
1382fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1383fcf5ef2aSThomas Huth {
1384fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1385fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1386fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1387fcf5ef2aSThomas Huth                               &env->mmx_status);
1388fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1389fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1390fcf5ef2aSThomas Huth }
1391fcf5ef2aSThomas Huth 
1392fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1393fcf5ef2aSThomas Huth {
1394fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1395fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1396fcf5ef2aSThomas Huth }
1397fcf5ef2aSThomas Huth 
1398fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1399fcf5ef2aSThomas Huth {
1400fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1401fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1402fcf5ef2aSThomas Huth }
1403fcf5ef2aSThomas Huth 
1404fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1405fcf5ef2aSThomas Huth {
140625bdec79SPaolo Bonzini     uint32_t r;
1407fcf5ef2aSThomas Huth 
140825bdec79SPaolo Bonzini     r = s->MMX_L(0);
140925bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
141025bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1411fcf5ef2aSThomas Huth }
1412fcf5ef2aSThomas Huth #endif
1413fcf5ef2aSThomas Huth 
1414fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1415f05f9789SPaolo Bonzini void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1416fcf5ef2aSThomas Huth {
1417fcf5ef2aSThomas Huth     int i;
1418d45b0de6SPaul Brook #if SHIFT == 0
1419d45b0de6SPaul Brook     uint8_t r[8];
1420fcf5ef2aSThomas Huth 
1421d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1422d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1423fcf5ef2aSThomas Huth     }
1424d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1425d45b0de6SPaul Brook         d->B(i) = r[i];
1426fcf5ef2aSThomas Huth     }
1427d45b0de6SPaul Brook #else
1428d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1429fcf5ef2aSThomas Huth 
1430d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1431d45b0de6SPaul Brook         int j = i & ~0xf;
1432d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1433fcf5ef2aSThomas Huth     }
1434d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1435d45b0de6SPaul Brook         d->B(i) = r[i];
1436fcf5ef2aSThomas Huth     }
1437fcf5ef2aSThomas Huth #endif
1438fcf5ef2aSThomas Huth }
1439fcf5ef2aSThomas Huth 
1440d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1441f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1442d45b0de6SPaul Brook {                                                          \
1443d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1444d45b0de6SPaul Brook     int i, j, k;                                           \
1445d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1446d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1447d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1448d45b0de6SPaul Brook         }                                                  \
1449d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1450d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1451d45b0de6SPaul Brook         }                                                  \
1452d45b0de6SPaul Brook     }                                                      \
1453d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1454d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1455d45b0de6SPaul Brook     }                                                      \
1456fcf5ef2aSThomas Huth }
1457fcf5ef2aSThomas Huth 
1458d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1459f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1460d45b0de6SPaul Brook {                                                          \
1461d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1462d45b0de6SPaul Brook     int i, j, k;                                           \
1463d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1464d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1465d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1466d45b0de6SPaul Brook         }                                                  \
1467d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1468d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1469d45b0de6SPaul Brook         }                                                  \
1470d45b0de6SPaul Brook     }                                                      \
1471d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1472d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1473d45b0de6SPaul Brook     }                                                      \
1474fcf5ef2aSThomas Huth }
1475fcf5ef2aSThomas Huth 
1476d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1477d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1478d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1479d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1480d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1481d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
148275046ad7SPaolo Bonzini 
1483d45b0de6SPaul Brook #undef SSE_HELPER_HW
1484d45b0de6SPaul Brook #undef SSE_HELPER_HL
1485d45b0de6SPaul Brook 
1486f05f9789SPaolo Bonzini void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1487d45b0de6SPaul Brook {
1488d45b0de6SPaul Brook     int i;
1489d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1490d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1491d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1492d45b0de6SPaul Brook     }
1493fcf5ef2aSThomas Huth }
1494fcf5ef2aSThomas Huth 
1495fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1496fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1497fcf5ef2aSThomas Huth 
1498fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1499fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1500fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1501fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1502fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1503fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1504fcf5ef2aSThomas Huth 
1505f05f9789SPaolo Bonzini void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1506a64eee3aSPaolo Bonzini                                   uint32_t imm)
1507fcf5ef2aSThomas Huth {
1508d45b0de6SPaul Brook     int i;
1509fcf5ef2aSThomas Huth 
1510fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1511a64eee3aSPaolo Bonzini     if (imm >= (SHIFT ? 32 : 16)) {
1512d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1513d45b0de6SPaul Brook             d->Q(i) = 0;
1514d45b0de6SPaul Brook         }
1515fcf5ef2aSThomas Huth     } else {
1516a64eee3aSPaolo Bonzini         int shift = imm * 8;
1517fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1518fcf5ef2aSThomas Huth #if SHIFT == 0
1519d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1520d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1521fcf5ef2aSThomas Huth #else
1522d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1523d45b0de6SPaul Brook             uint64_t r0, r1;
1524d45b0de6SPaul Brook 
1525d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1526d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1527d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1528d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1529d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1530d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1531d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1532d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1533d45b0de6SPaul Brook             d->Q(i) = r0;
1534d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1535d45b0de6SPaul Brook         }
1536fcf5ef2aSThomas Huth #endif
1537fcf5ef2aSThomas Huth #undef SHR
1538fcf5ef2aSThomas Huth     }
1539fcf5ef2aSThomas Huth }
1540fcf5ef2aSThomas Huth 
15410e29cea5SPaul Brook #if SHIFT >= 1
1542fcf5ef2aSThomas Huth 
1543fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1544f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
1545f05f9789SPaolo Bonzini                             Reg *m)                                     \
1546fcf5ef2aSThomas Huth     {                                                                   \
15470e29cea5SPaul Brook         int i;                                                          \
15480e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
15490e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), m->elem(i));         \
1550fcf5ef2aSThomas Huth         }                                                               \
1551fcf5ef2aSThomas Huth     }
1552fcf5ef2aSThomas Huth 
1553fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1554f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
15550e29cea5SPaul Brook                             uint32_t imm)                               \
1556fcf5ef2aSThomas Huth     {                                                                   \
15570e29cea5SPaul Brook         int i;                                                          \
15580e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
15590e29cea5SPaul Brook             int j = i & 7;                                              \
15600e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), (imm >> j) & 1);     \
1561fcf5ef2aSThomas Huth         }                                                               \
1562fcf5ef2aSThomas Huth     }
1563fcf5ef2aSThomas Huth 
1564fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
15650e29cea5SPaul Brook #define FBLENDVB(v, s, m) ((m & 0x80) ? s : v)
15660e29cea5SPaul Brook #define FBLENDVPS(v, s, m) ((m & 0x80000000) ? s : v)
15670e29cea5SPaul Brook #define FBLENDVPD(v, s, m) ((m & 0x8000000000000000LL) ? s : v)
15680e29cea5SPaul Brook SSE_HELPER_V(helper_pblendvb, B, 8 << SHIFT, FBLENDVB)
15690e29cea5SPaul Brook SSE_HELPER_V(helper_blendvps, L, 2 << SHIFT, FBLENDVPS)
15700e29cea5SPaul Brook SSE_HELPER_V(helper_blendvpd, Q, 1 << SHIFT, FBLENDVPD)
1571fcf5ef2aSThomas Huth 
1572fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1573fcf5ef2aSThomas Huth {
1574e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1575e894bae8SPaul Brook     int i;
1576fcf5ef2aSThomas Huth 
1577e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1578e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1579e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1580e894bae8SPaul Brook     }
1581fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1582fcf5ef2aSThomas Huth }
1583fcf5ef2aSThomas Huth 
15847170a17eSPaolo Bonzini #define FMOVSLDUP(i) s->L((i) & ~1)
15857170a17eSPaolo Bonzini #define FMOVSHDUP(i) s->L((i) | 1)
15867170a17eSPaolo Bonzini #define FMOVDLDUP(i) s->Q((i) & ~1)
15877170a17eSPaolo Bonzini 
1588fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1589fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1590fcf5ef2aSThomas Huth     {                                                           \
1591e894bae8SPaul Brook         int n = num;                                            \
1592e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1593e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1594fcf5ef2aSThomas Huth         }                                                       \
1595fcf5ef2aSThomas Huth     }
1596fcf5ef2aSThomas Huth 
1597e894bae8SPaul Brook #if SHIFT > 0
1598e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1599e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1600e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1601e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1602e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1603e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1604e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1605e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1606e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1607e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1608e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1609e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
16107170a17eSPaolo Bonzini SSE_HELPER_F(helper_pmovsldup, L, 2 << SHIFT, FMOVSLDUP)
16117170a17eSPaolo Bonzini SSE_HELPER_F(helper_pmovshdup, L, 2 << SHIFT, FMOVSHDUP)
16127170a17eSPaolo Bonzini SSE_HELPER_F(helper_pmovdldup, Q, 1 << SHIFT, FMOVDLDUP)
1613e894bae8SPaul Brook #endif
1614fcf5ef2aSThomas Huth 
1615f05f9789SPaolo Bonzini void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1616fcf5ef2aSThomas Huth {
1617e894bae8SPaul Brook     int i;
1618e894bae8SPaul Brook 
1619e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1620e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1621e894bae8SPaul Brook     }
1622fcf5ef2aSThomas Huth }
1623fcf5ef2aSThomas Huth 
1624f05f9789SPaolo Bonzini void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1625fcf5ef2aSThomas Huth {
1626d45b0de6SPaul Brook     uint16_t r[8];
1627d45b0de6SPaul Brook     int i, j, k;
162880e19606SJoseph Myers 
1629d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1630d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1631d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1632d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1633d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1634d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1635d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1636d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1637d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1638d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1639d45b0de6SPaul Brook             d->W(i + k) = r[k];
1640d45b0de6SPaul Brook         }
1641d45b0de6SPaul Brook     }
1642fcf5ef2aSThomas Huth }
1643fcf5ef2aSThomas Huth 
1644fd17264aSPaul Brook #if SHIFT == 1
1645fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1646fcf5ef2aSThomas Huth {
1647fcf5ef2aSThomas Huth     int idx = 0;
1648fcf5ef2aSThomas Huth 
1649fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1650fcf5ef2aSThomas Huth         idx = 1;
1651fcf5ef2aSThomas Huth     }
1652fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1653fcf5ef2aSThomas Huth         idx = 2;
1654fcf5ef2aSThomas Huth     }
1655fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1656fcf5ef2aSThomas Huth         idx = 3;
1657fcf5ef2aSThomas Huth     }
1658fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1659fcf5ef2aSThomas Huth         idx = 4;
1660fcf5ef2aSThomas Huth     }
1661fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1662fcf5ef2aSThomas Huth         idx = 5;
1663fcf5ef2aSThomas Huth     }
1664fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1665fcf5ef2aSThomas Huth         idx = 6;
1666fcf5ef2aSThomas Huth     }
1667fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1668fcf5ef2aSThomas Huth         idx = 7;
1669fcf5ef2aSThomas Huth     }
1670fcf5ef2aSThomas Huth 
1671fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1672aa406feaSJoseph Myers     d->W(1) = idx;
1673aa406feaSJoseph Myers     d->L(1) = 0;
1674aa406feaSJoseph Myers     d->Q(1) = 0;
1675fcf5ef2aSThomas Huth }
1676fd17264aSPaul Brook #endif
1677fcf5ef2aSThomas Huth 
1678fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1679fcf5ef2aSThomas Huth                                   uint32_t mode)
1680fcf5ef2aSThomas Huth {
1681418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1682fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1683fd17264aSPaul Brook     int i;
1684fcf5ef2aSThomas Huth 
1685fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1686fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1687*314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1688fcf5ef2aSThomas Huth     }
1689fcf5ef2aSThomas Huth 
1690fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1691fd17264aSPaul Brook         d->ZMM_S(i) = float32_round_to_int(s->ZMM_S(i), &env->sse_status);
1692fd17264aSPaul Brook     }
1693fcf5ef2aSThomas Huth 
1694418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1695fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1696fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1697fcf5ef2aSThomas Huth                                   &env->sse_status);
1698fcf5ef2aSThomas Huth     }
1699fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1700fcf5ef2aSThomas Huth }
1701fcf5ef2aSThomas Huth 
1702fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1703fcf5ef2aSThomas Huth                                   uint32_t mode)
1704fcf5ef2aSThomas Huth {
1705418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1706fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1707fd17264aSPaul Brook     int i;
1708fcf5ef2aSThomas Huth 
1709fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1710fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1711*314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1712fcf5ef2aSThomas Huth     }
1713fcf5ef2aSThomas Huth 
1714fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1715fd17264aSPaul Brook         d->ZMM_D(i) = float64_round_to_int(s->ZMM_D(i), &env->sse_status);
1716fd17264aSPaul Brook     }
1717fcf5ef2aSThomas Huth 
1718418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1719fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1720fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1721fcf5ef2aSThomas Huth                                   &env->sse_status);
1722fcf5ef2aSThomas Huth     }
1723fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1724fcf5ef2aSThomas Huth }
1725fcf5ef2aSThomas Huth 
1726fd17264aSPaul Brook #if SHIFT == 1
1727620f7556SPaolo Bonzini void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1728fcf5ef2aSThomas Huth                                   uint32_t mode)
1729fcf5ef2aSThomas Huth {
1730418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1731fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1732620f7556SPaolo Bonzini     int i;
1733fcf5ef2aSThomas Huth 
1734fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1735fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1736*314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1737fcf5ef2aSThomas Huth     }
1738fcf5ef2aSThomas Huth 
1739fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1740620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
1741620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
1742620f7556SPaolo Bonzini     }
1743fcf5ef2aSThomas Huth 
1744418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1745fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1746fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1747fcf5ef2aSThomas Huth                                   &env->sse_status);
1748fcf5ef2aSThomas Huth     }
1749fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1750fcf5ef2aSThomas Huth }
1751fcf5ef2aSThomas Huth 
1752620f7556SPaolo Bonzini void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1753fcf5ef2aSThomas Huth                                   uint32_t mode)
1754fcf5ef2aSThomas Huth {
1755418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1756fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1757620f7556SPaolo Bonzini     int i;
1758fcf5ef2aSThomas Huth 
1759fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1760fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1761*314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1762fcf5ef2aSThomas Huth     }
1763fcf5ef2aSThomas Huth 
1764fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1765620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
1766620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
1767620f7556SPaolo Bonzini     }
1768fcf5ef2aSThomas Huth 
1769418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1770fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1771fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1772fcf5ef2aSThomas Huth                                   &env->sse_status);
1773fcf5ef2aSThomas Huth     }
1774fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1775fcf5ef2aSThomas Huth }
1776fd17264aSPaul Brook #endif
1777fcf5ef2aSThomas Huth 
17780e29cea5SPaul Brook #define FBLENDP(v, s, m) (m ? s : v)
17790e29cea5SPaul Brook SSE_HELPER_I(helper_blendps, L, 2 << SHIFT, FBLENDP)
17800e29cea5SPaul Brook SSE_HELPER_I(helper_blendpd, Q, 1 << SHIFT, FBLENDP)
17810e29cea5SPaul Brook SSE_HELPER_I(helper_pblendw, W, 4 << SHIFT, FBLENDP)
1782fcf5ef2aSThomas Huth 
1783f05f9789SPaolo Bonzini void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
17846f218d6eSPaul Brook                                uint32_t mask)
1785fcf5ef2aSThomas Huth {
1786bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
17876f218d6eSPaul Brook     int i;
1788fcf5ef2aSThomas Huth 
17896f218d6eSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
1790bf30ad8cSPaolo Bonzini         /*
1791bf30ad8cSPaolo Bonzini          * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1792bf30ad8cSPaolo Bonzini          * to correctly round the intermediate results
1793bf30ad8cSPaolo Bonzini          */
1794fcf5ef2aSThomas Huth         if (mask & (1 << 4)) {
17956f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
1796bf30ad8cSPaolo Bonzini         } else {
1797bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1798fcf5ef2aSThomas Huth         }
1799fcf5ef2aSThomas Huth         if (mask & (1 << 5)) {
18006f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
1801bf30ad8cSPaolo Bonzini         } else {
1802bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1803fcf5ef2aSThomas Huth         }
1804bf30ad8cSPaolo Bonzini         temp2 = float32_add(prod1, prod2, &env->sse_status);
1805fcf5ef2aSThomas Huth         if (mask & (1 << 6)) {
18066f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i+2), s->ZMM_S(i+2), &env->sse_status);
1807bf30ad8cSPaolo Bonzini         } else {
1808bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1809fcf5ef2aSThomas Huth         }
1810fcf5ef2aSThomas Huth         if (mask & (1 << 7)) {
18116f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+3), s->ZMM_S(i+3), &env->sse_status);
1812bf30ad8cSPaolo Bonzini         } else {
1813bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1814fcf5ef2aSThomas Huth         }
1815bf30ad8cSPaolo Bonzini         temp3 = float32_add(prod1, prod2, &env->sse_status);
1816bf30ad8cSPaolo Bonzini         temp4 = float32_add(temp2, temp3, &env->sse_status);
1817bf30ad8cSPaolo Bonzini 
18186f218d6eSPaul Brook         d->ZMM_S(i) = (mask & (1 << 0)) ? temp4 : float32_zero;
18196f218d6eSPaul Brook         d->ZMM_S(i+1) = (mask & (1 << 1)) ? temp4 : float32_zero;
18206f218d6eSPaul Brook         d->ZMM_S(i+2) = (mask & (1 << 2)) ? temp4 : float32_zero;
18216f218d6eSPaul Brook         d->ZMM_S(i+3) = (mask & (1 << 3)) ? temp4 : float32_zero;
18226f218d6eSPaul Brook     }
1823fcf5ef2aSThomas Huth }
1824fcf5ef2aSThomas Huth 
18256f218d6eSPaul Brook #if SHIFT == 1
18266f218d6eSPaul Brook /* Oddly, there is no ymm version of dppd */
18276f218d6eSPaul Brook void glue(helper_dppd, SUFFIX)(CPUX86State *env,
1828f05f9789SPaolo Bonzini                                Reg *d, Reg *v, Reg *s, uint32_t mask)
1829fcf5ef2aSThomas Huth {
1830bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1831fcf5ef2aSThomas Huth 
1832fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
18336f218d6eSPaul Brook         prod1 = float64_mul(v->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1834bf30ad8cSPaolo Bonzini     } else {
1835bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1836fcf5ef2aSThomas Huth     }
1837fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
18386f218d6eSPaul Brook         prod2 = float64_mul(v->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1839bf30ad8cSPaolo Bonzini     } else {
1840bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1841fcf5ef2aSThomas Huth     }
1842bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1843bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1844bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1845fcf5ef2aSThomas Huth }
18466f218d6eSPaul Brook #endif
1847fcf5ef2aSThomas Huth 
1848f05f9789SPaolo Bonzini void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1849fcf5ef2aSThomas Huth                                   uint32_t offset)
1850fcf5ef2aSThomas Huth {
1851d45b0de6SPaul Brook     int i, j;
1852d45b0de6SPaul Brook     uint16_t r[8];
1853fcf5ef2aSThomas Huth 
1854d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; ) {
1855d45b0de6SPaul Brook         int s0 = (j * 2) + ((offset & 3) << 2);
1856d45b0de6SPaul Brook         int d0 = (j * 2) + ((offset & 4) << 0);
1857d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, d0++) {
1858d45b0de6SPaul Brook             r[i] = 0;
1859d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0));
1860d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1));
1861d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2));
1862d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3));
1863fcf5ef2aSThomas Huth         }
1864d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, j++) {
1865d45b0de6SPaul Brook             d->W(j) = r[i];
1866d45b0de6SPaul Brook         }
1867d45b0de6SPaul Brook         offset >>= 3;
1868d45b0de6SPaul Brook     }
1869fcf5ef2aSThomas Huth }
1870fcf5ef2aSThomas Huth 
1871fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1872fd17264aSPaul Brook #if SHIFT == 1
1873fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1874fcf5ef2aSThomas Huth {
1875d1da229fSPaul Brook     target_long val, limit;
1876fcf5ef2aSThomas Huth 
1877fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1878fcf5ef2aSThomas Huth     if (ctrl >> 8) {
1879d1da229fSPaul Brook         val = (target_long)env->regs[reg];
1880fcf5ef2aSThomas Huth     } else {
1881d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
1882fcf5ef2aSThomas Huth     }
1883fcf5ef2aSThomas Huth     if (ctrl & 1) {
1884d1da229fSPaul Brook         limit = 8;
1885fcf5ef2aSThomas Huth     } else {
1886d1da229fSPaul Brook         limit = 16;
1887fcf5ef2aSThomas Huth     }
1888d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
1889d1da229fSPaul Brook         return limit;
1890fcf5ef2aSThomas Huth     }
1891d1da229fSPaul Brook     return abs1(val);
1892fcf5ef2aSThomas Huth }
1893fcf5ef2aSThomas Huth 
1894fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
1895fcf5ef2aSThomas Huth {
1896fcf5ef2aSThomas Huth     int val = 0;
1897fcf5ef2aSThomas Huth 
1898fcf5ef2aSThomas Huth     if (ctrl & 1) {
1899fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
1900fcf5ef2aSThomas Huth             val++;
1901fcf5ef2aSThomas Huth         }
1902fcf5ef2aSThomas Huth     } else {
1903fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
1904fcf5ef2aSThomas Huth             val++;
1905fcf5ef2aSThomas Huth         }
1906fcf5ef2aSThomas Huth     }
1907fcf5ef2aSThomas Huth 
1908fcf5ef2aSThomas Huth     return val;
1909fcf5ef2aSThomas Huth }
1910fcf5ef2aSThomas Huth 
1911fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
1912fcf5ef2aSThomas Huth {
1913fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
1914fcf5ef2aSThomas Huth     case 0:
1915fcf5ef2aSThomas Huth         return r->B(i);
1916fcf5ef2aSThomas Huth     case 1:
1917fcf5ef2aSThomas Huth         return r->W(i);
1918fcf5ef2aSThomas Huth     case 2:
1919fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
1920fcf5ef2aSThomas Huth     case 3:
1921fcf5ef2aSThomas Huth     default:
1922fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
1923fcf5ef2aSThomas Huth     }
1924fcf5ef2aSThomas Huth }
1925fcf5ef2aSThomas Huth 
1926fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
1927a64eee3aSPaolo Bonzini                                  uint8_t ctrl, int valids, int validd)
1928fcf5ef2aSThomas Huth {
1929fcf5ef2aSThomas Huth     unsigned int res = 0;
1930fcf5ef2aSThomas Huth     int v;
1931fcf5ef2aSThomas Huth     int j, i;
1932fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
1933fcf5ef2aSThomas Huth 
1934fcf5ef2aSThomas Huth     valids--;
1935fcf5ef2aSThomas Huth     validd--;
1936fcf5ef2aSThomas Huth 
1937fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
1938fcf5ef2aSThomas Huth 
1939fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
1940fcf5ef2aSThomas Huth     case 0:
1941fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
1942fcf5ef2aSThomas Huth             res <<= 1;
1943fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
1944fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
1945fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
1946fcf5ef2aSThomas Huth             }
1947fcf5ef2aSThomas Huth         }
1948fcf5ef2aSThomas Huth         break;
1949fcf5ef2aSThomas Huth     case 1:
1950fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
1951fcf5ef2aSThomas Huth             res <<= 1;
1952fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
1953fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
1954fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
1955fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
1956fcf5ef2aSThomas Huth             }
1957fcf5ef2aSThomas Huth         }
1958fcf5ef2aSThomas Huth         break;
1959fcf5ef2aSThomas Huth     case 2:
1960fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
1961fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
1962fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
1963fcf5ef2aSThomas Huth             res <<= 1;
1964fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
1965fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
1966fcf5ef2aSThomas Huth         }
1967fcf5ef2aSThomas Huth         break;
1968fcf5ef2aSThomas Huth     case 3:
1969ae35eea7SJoseph Myers         if (validd == -1) {
1970ae35eea7SJoseph Myers             res = (2 << upper) - 1;
1971ae35eea7SJoseph Myers             break;
1972ae35eea7SJoseph Myers         }
1973bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
1974fcf5ef2aSThomas Huth             res <<= 1;
1975fcf5ef2aSThomas Huth             v = 1;
1976bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
1977fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
1978fcf5ef2aSThomas Huth             }
1979fcf5ef2aSThomas Huth             res |= v;
1980fcf5ef2aSThomas Huth         }
1981fcf5ef2aSThomas Huth         break;
1982fcf5ef2aSThomas Huth     }
1983fcf5ef2aSThomas Huth 
1984fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
1985fcf5ef2aSThomas Huth     case 1:
1986fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
1987fcf5ef2aSThomas Huth         break;
1988fcf5ef2aSThomas Huth     case 3:
1989fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
1990fcf5ef2aSThomas Huth         break;
1991fcf5ef2aSThomas Huth     }
1992fcf5ef2aSThomas Huth 
1993fcf5ef2aSThomas Huth     if (res) {
1994fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
1995fcf5ef2aSThomas Huth     }
1996fcf5ef2aSThomas Huth     if (res & 1) {
1997fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
1998fcf5ef2aSThomas Huth     }
1999fcf5ef2aSThomas Huth 
2000fcf5ef2aSThomas Huth     return res;
2001fcf5ef2aSThomas Huth }
2002fcf5ef2aSThomas Huth 
2003fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2004fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2005fcf5ef2aSThomas Huth {
2006fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2007fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2008fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2009fcf5ef2aSThomas Huth 
2010fcf5ef2aSThomas Huth     if (res) {
2011fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2012fcf5ef2aSThomas Huth     } else {
2013fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2014fcf5ef2aSThomas Huth     }
2015fcf5ef2aSThomas Huth }
2016fcf5ef2aSThomas Huth 
2017fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2018fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2019fcf5ef2aSThomas Huth {
2020fcf5ef2aSThomas Huth     int i;
2021fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2022fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2023fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2024fcf5ef2aSThomas Huth 
2025fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2026fcf5ef2aSThomas Huth         if (ctrl & 1) {
2027fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2028fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2029fcf5ef2aSThomas Huth             }
2030fcf5ef2aSThomas Huth         } else {
2031fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2032fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2033fcf5ef2aSThomas Huth             }
2034fcf5ef2aSThomas Huth         }
2035fcf5ef2aSThomas Huth     } else {
2036fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2037fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2038fcf5ef2aSThomas Huth     }
2039fcf5ef2aSThomas Huth }
2040fcf5ef2aSThomas Huth 
2041fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2042fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2043fcf5ef2aSThomas Huth {
2044fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2045fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2046fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2047fcf5ef2aSThomas Huth 
2048fcf5ef2aSThomas Huth     if (res) {
2049fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2050fcf5ef2aSThomas Huth     } else {
2051fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2052fcf5ef2aSThomas Huth     }
2053fcf5ef2aSThomas Huth }
2054fcf5ef2aSThomas Huth 
2055fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2056fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2057fcf5ef2aSThomas Huth {
2058fcf5ef2aSThomas Huth     int i;
2059fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2060fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2061fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2062fcf5ef2aSThomas Huth 
2063fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2064fcf5ef2aSThomas Huth         if (ctrl & 1) {
2065fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2066fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2067fcf5ef2aSThomas Huth             }
2068fcf5ef2aSThomas Huth         } else {
2069fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2070fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2071fcf5ef2aSThomas Huth             }
2072fcf5ef2aSThomas Huth         }
2073fcf5ef2aSThomas Huth     } else {
2074fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2075fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2076fcf5ef2aSThomas Huth     }
2077fcf5ef2aSThomas Huth }
2078fcf5ef2aSThomas Huth 
2079fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2080fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2081fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2082fcf5ef2aSThomas Huth {
2083fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2084fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2085fcf5ef2aSThomas Huth 
2086fcf5ef2aSThomas Huth     while (len--) {
2087fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2088fcf5ef2aSThomas Huth     }
2089fcf5ef2aSThomas Huth 
2090fcf5ef2aSThomas Huth     return crc;
2091fcf5ef2aSThomas Huth }
2092fcf5ef2aSThomas Huth 
2093fd17264aSPaul Brook #endif
2094fd17264aSPaul Brook 
20955a09df21SPaul Brook #if SHIFT == 1
20965a09df21SPaul Brook static void clmulq(uint64_t *dest_l, uint64_t *dest_h,
20975a09df21SPaul Brook                           uint64_t a, uint64_t b)
2098fcf5ef2aSThomas Huth {
20995a09df21SPaul Brook     uint64_t al, ah, resh, resl;
2100fcf5ef2aSThomas Huth 
2101fcf5ef2aSThomas Huth     ah = 0;
21025a09df21SPaul Brook     al = a;
2103fcf5ef2aSThomas Huth     resh = resl = 0;
2104fcf5ef2aSThomas Huth 
2105fcf5ef2aSThomas Huth     while (b) {
2106fcf5ef2aSThomas Huth         if (b & 1) {
2107fcf5ef2aSThomas Huth             resl ^= al;
2108fcf5ef2aSThomas Huth             resh ^= ah;
2109fcf5ef2aSThomas Huth         }
2110fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2111fcf5ef2aSThomas Huth         al <<= 1;
2112fcf5ef2aSThomas Huth         b >>= 1;
2113fcf5ef2aSThomas Huth     }
2114fcf5ef2aSThomas Huth 
21155a09df21SPaul Brook     *dest_l = resl;
21165a09df21SPaul Brook     *dest_h = resh;
21175a09df21SPaul Brook }
21185a09df21SPaul Brook #endif
21195a09df21SPaul Brook 
2120f05f9789SPaolo Bonzini void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
21215a09df21SPaul Brook                                     uint32_t ctrl)
21225a09df21SPaul Brook {
21235a09df21SPaul Brook     uint64_t a, b;
21245a09df21SPaul Brook     int i;
21255a09df21SPaul Brook 
21265a09df21SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
21275a09df21SPaul Brook         a = v->Q(((ctrl & 1) != 0) + i);
21285a09df21SPaul Brook         b = s->Q(((ctrl & 16) != 0) + i);
21295a09df21SPaul Brook         clmulq(&d->Q(i), &d->Q(i + 1), a, b);
21305a09df21SPaul Brook     }
2131fcf5ef2aSThomas Huth }
2132fcf5ef2aSThomas Huth 
2133f05f9789SPaolo Bonzini void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2134fcf5ef2aSThomas Huth {
2135fcf5ef2aSThomas Huth     int i;
2136f05f9789SPaolo Bonzini     Reg st = *v;
2137fcf5ef2aSThomas Huth     Reg rk = *s;
2138fcf5ef2aSThomas Huth 
2139a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2140a64fc269SPaul Brook         int j = i & 3;
2141a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4 * j + 0])] ^
2142a64fc269SPaul Brook                                     AES_Td1[st.B(AES_ishifts[4 * j + 1])] ^
2143a64fc269SPaul Brook                                     AES_Td2[st.B(AES_ishifts[4 * j + 2])] ^
2144a64fc269SPaul Brook                                     AES_Td3[st.B(AES_ishifts[4 * j + 3])]);
2145fcf5ef2aSThomas Huth     }
2146fcf5ef2aSThomas Huth }
2147fcf5ef2aSThomas Huth 
2148f05f9789SPaolo Bonzini void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2149fcf5ef2aSThomas Huth {
2150fcf5ef2aSThomas Huth     int i;
2151f05f9789SPaolo Bonzini     Reg st = *v;
2152fcf5ef2aSThomas Huth     Reg rk = *s;
2153fcf5ef2aSThomas Huth 
2154a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2155a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i & 15] + (i & ~15))]);
2156fcf5ef2aSThomas Huth     }
2157fcf5ef2aSThomas Huth }
2158fcf5ef2aSThomas Huth 
2159f05f9789SPaolo Bonzini void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2160fcf5ef2aSThomas Huth {
2161fcf5ef2aSThomas Huth     int i;
2162f05f9789SPaolo Bonzini     Reg st = *v;
2163fcf5ef2aSThomas Huth     Reg rk = *s;
2164fcf5ef2aSThomas Huth 
2165a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2166a64fc269SPaul Brook         int j = i & 3;
2167a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4 * j + 0])] ^
2168a64fc269SPaul Brook                                     AES_Te1[st.B(AES_shifts[4 * j + 1])] ^
2169a64fc269SPaul Brook                                     AES_Te2[st.B(AES_shifts[4 * j + 2])] ^
2170a64fc269SPaul Brook                                     AES_Te3[st.B(AES_shifts[4 * j + 3])]);
2171fcf5ef2aSThomas Huth     }
2172fcf5ef2aSThomas Huth }
2173fcf5ef2aSThomas Huth 
2174f05f9789SPaolo Bonzini void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2175fcf5ef2aSThomas Huth {
2176fcf5ef2aSThomas Huth     int i;
2177f05f9789SPaolo Bonzini     Reg st = *v;
2178fcf5ef2aSThomas Huth     Reg rk = *s;
2179fcf5ef2aSThomas Huth 
2180a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2181a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]);
2182a64fc269SPaul Brook     }
2183fcf5ef2aSThomas Huth }
2184fcf5ef2aSThomas Huth 
2185a64fc269SPaul Brook #if SHIFT == 1
2186fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2187fcf5ef2aSThomas Huth {
2188fcf5ef2aSThomas Huth     int i;
2189fcf5ef2aSThomas Huth     Reg tmp = *s;
2190fcf5ef2aSThomas Huth 
2191fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2192fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4 * i + 0)][0] ^
2193fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 1)][1] ^
2194fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 2)][2] ^
2195fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 3)][3]);
2196fcf5ef2aSThomas Huth     }
2197fcf5ef2aSThomas Huth }
2198fcf5ef2aSThomas Huth 
2199fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2200fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2201fcf5ef2aSThomas Huth {
2202fcf5ef2aSThomas Huth     int i;
2203fcf5ef2aSThomas Huth     Reg tmp = *s;
2204fcf5ef2aSThomas Huth 
2205fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2206fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2207fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2208fcf5ef2aSThomas Huth     }
2209fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2210fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2211fcf5ef2aSThomas Huth }
2212fcf5ef2aSThomas Huth #endif
2213a64fc269SPaul Brook #endif
2214fcf5ef2aSThomas Huth 
221579068477SPaolo Bonzini #if SHIFT >= 1
221616fc5726SPaolo Bonzini void glue(helper_vpermilpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
221716fc5726SPaolo Bonzini {
221816fc5726SPaolo Bonzini     uint64_t r0, r1;
221916fc5726SPaolo Bonzini     int i;
222016fc5726SPaolo Bonzini 
222116fc5726SPaolo Bonzini     for (i = 0; i < 1 << SHIFT; i += 2) {
222216fc5726SPaolo Bonzini         r0 = v->Q(i + ((s->Q(i) >> 1) & 1));
222316fc5726SPaolo Bonzini         r1 = v->Q(i + ((s->Q(i+1) >> 1) & 1));
222416fc5726SPaolo Bonzini         d->Q(i) = r0;
222516fc5726SPaolo Bonzini         d->Q(i+1) = r1;
222616fc5726SPaolo Bonzini     }
222716fc5726SPaolo Bonzini }
222816fc5726SPaolo Bonzini 
222916fc5726SPaolo Bonzini void glue(helper_vpermilps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
223016fc5726SPaolo Bonzini {
223116fc5726SPaolo Bonzini     uint32_t r0, r1, r2, r3;
223216fc5726SPaolo Bonzini     int i;
223316fc5726SPaolo Bonzini 
223416fc5726SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i += 4) {
223516fc5726SPaolo Bonzini         r0 = v->L(i + (s->L(i) & 3));
223616fc5726SPaolo Bonzini         r1 = v->L(i + (s->L(i+1) & 3));
223716fc5726SPaolo Bonzini         r2 = v->L(i + (s->L(i+2) & 3));
223816fc5726SPaolo Bonzini         r3 = v->L(i + (s->L(i+3) & 3));
223916fc5726SPaolo Bonzini         d->L(i) = r0;
224016fc5726SPaolo Bonzini         d->L(i+1) = r1;
224116fc5726SPaolo Bonzini         d->L(i+2) = r2;
224216fc5726SPaolo Bonzini         d->L(i+3) = r3;
224316fc5726SPaolo Bonzini     }
224416fc5726SPaolo Bonzini }
224516fc5726SPaolo Bonzini 
224679068477SPaolo Bonzini void glue(helper_vpermilpd_imm, SUFFIX)(Reg *d, Reg *s, uint32_t order)
224779068477SPaolo Bonzini {
224879068477SPaolo Bonzini     uint64_t r0, r1;
224979068477SPaolo Bonzini     int i;
225079068477SPaolo Bonzini 
225179068477SPaolo Bonzini     for (i = 0; i < 1 << SHIFT; i += 2) {
225279068477SPaolo Bonzini         r0 = s->Q(i + ((order >> 0) & 1));
225379068477SPaolo Bonzini         r1 = s->Q(i + ((order >> 1) & 1));
225479068477SPaolo Bonzini         d->Q(i) = r0;
225579068477SPaolo Bonzini         d->Q(i+1) = r1;
225679068477SPaolo Bonzini 
225779068477SPaolo Bonzini         order >>= 2;
225879068477SPaolo Bonzini     }
225979068477SPaolo Bonzini }
226079068477SPaolo Bonzini 
226179068477SPaolo Bonzini void glue(helper_vpermilps_imm, SUFFIX)(Reg *d, Reg *s, uint32_t order)
226279068477SPaolo Bonzini {
226379068477SPaolo Bonzini     uint32_t r0, r1, r2, r3;
226479068477SPaolo Bonzini     int i;
226579068477SPaolo Bonzini 
226679068477SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i += 4) {
226779068477SPaolo Bonzini         r0 = s->L(i + ((order >> 0) & 3));
226879068477SPaolo Bonzini         r1 = s->L(i + ((order >> 2) & 3));
226979068477SPaolo Bonzini         r2 = s->L(i + ((order >> 4) & 3));
227079068477SPaolo Bonzini         r3 = s->L(i + ((order >> 6) & 3));
227179068477SPaolo Bonzini         d->L(i) = r0;
227279068477SPaolo Bonzini         d->L(i+1) = r1;
227379068477SPaolo Bonzini         d->L(i+2) = r2;
227479068477SPaolo Bonzini         d->L(i+3) = r3;
227579068477SPaolo Bonzini     }
227679068477SPaolo Bonzini }
227779068477SPaolo Bonzini 
227816fc5726SPaolo Bonzini #if SHIFT == 1
227916fc5726SPaolo Bonzini #define FPSRLVD(x, c) (c < 32 ? ((x) >> c) : 0)
228016fc5726SPaolo Bonzini #define FPSRLVQ(x, c) (c < 64 ? ((x) >> c) : 0)
228116fc5726SPaolo Bonzini #define FPSRAVD(x, c) ((int32_t)(x) >> (c < 32 ? c : 31))
228216fc5726SPaolo Bonzini #define FPSRAVQ(x, c) ((int64_t)(x) >> (c < 64 ? c : 63))
228316fc5726SPaolo Bonzini #define FPSLLVD(x, c) (c < 32 ? ((x) << c) : 0)
228416fc5726SPaolo Bonzini #define FPSLLVQ(x, c) (c < 64 ? ((x) << c) : 0)
228516fc5726SPaolo Bonzini #endif
228616fc5726SPaolo Bonzini 
228716fc5726SPaolo Bonzini SSE_HELPER_L(helper_vpsrlvd, FPSRLVD)
228816fc5726SPaolo Bonzini SSE_HELPER_L(helper_vpsravd, FPSRAVD)
228916fc5726SPaolo Bonzini SSE_HELPER_L(helper_vpsllvd, FPSLLVD)
229016fc5726SPaolo Bonzini 
229116fc5726SPaolo Bonzini SSE_HELPER_Q(helper_vpsrlvq, FPSRLVQ)
229216fc5726SPaolo Bonzini SSE_HELPER_Q(helper_vpsravq, FPSRAVQ)
229316fc5726SPaolo Bonzini SSE_HELPER_Q(helper_vpsllvq, FPSLLVQ)
229416fc5726SPaolo Bonzini 
229516fc5726SPaolo Bonzini void glue(helper_vtestps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
229616fc5726SPaolo Bonzini {
229716fc5726SPaolo Bonzini     uint32_t zf = 0, cf = 0;
229816fc5726SPaolo Bonzini     int i;
229916fc5726SPaolo Bonzini 
230016fc5726SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i++) {
230116fc5726SPaolo Bonzini         zf |= (s->L(i) &  d->L(i));
230216fc5726SPaolo Bonzini         cf |= (s->L(i) & ~d->L(i));
230316fc5726SPaolo Bonzini     }
230416fc5726SPaolo Bonzini     CC_SRC = ((zf >> 31) ? 0 : CC_Z) | ((cf >> 31) ? 0 : CC_C);
230516fc5726SPaolo Bonzini }
230616fc5726SPaolo Bonzini 
230716fc5726SPaolo Bonzini void glue(helper_vtestpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
230816fc5726SPaolo Bonzini {
230916fc5726SPaolo Bonzini     uint64_t zf = 0, cf = 0;
231016fc5726SPaolo Bonzini     int i;
231116fc5726SPaolo Bonzini 
231216fc5726SPaolo Bonzini     for (i = 0; i < 1 << SHIFT; i++) {
231316fc5726SPaolo Bonzini         zf |= (s->Q(i) &  d->Q(i));
231416fc5726SPaolo Bonzini         cf |= (s->Q(i) & ~d->Q(i));
231516fc5726SPaolo Bonzini     }
231616fc5726SPaolo Bonzini     CC_SRC = ((zf >> 63) ? 0 : CC_Z) | ((cf >> 63) ? 0 : CC_C);
231716fc5726SPaolo Bonzini }
231816fc5726SPaolo Bonzini 
231916fc5726SPaolo Bonzini void glue(helper_vpmaskmovd_st, SUFFIX)(CPUX86State *env,
232016fc5726SPaolo Bonzini                                         Reg *v, Reg *s, target_ulong a0)
232116fc5726SPaolo Bonzini {
232216fc5726SPaolo Bonzini     int i;
232316fc5726SPaolo Bonzini 
232416fc5726SPaolo Bonzini     for (i = 0; i < (2 << SHIFT); i++) {
232516fc5726SPaolo Bonzini         if (v->L(i) >> 31) {
232616fc5726SPaolo Bonzini             cpu_stl_data_ra(env, a0 + i * 4, s->L(i), GETPC());
232716fc5726SPaolo Bonzini         }
232816fc5726SPaolo Bonzini     }
232916fc5726SPaolo Bonzini }
233016fc5726SPaolo Bonzini 
233116fc5726SPaolo Bonzini void glue(helper_vpmaskmovq_st, SUFFIX)(CPUX86State *env,
233216fc5726SPaolo Bonzini                                         Reg *v, Reg *s, target_ulong a0)
233316fc5726SPaolo Bonzini {
233416fc5726SPaolo Bonzini     int i;
233516fc5726SPaolo Bonzini 
233616fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
233716fc5726SPaolo Bonzini         if (v->Q(i) >> 63) {
233816fc5726SPaolo Bonzini             cpu_stq_data_ra(env, a0 + i * 8, s->Q(i), GETPC());
233916fc5726SPaolo Bonzini         }
234016fc5726SPaolo Bonzini     }
234116fc5726SPaolo Bonzini }
234216fc5726SPaolo Bonzini 
234316fc5726SPaolo Bonzini void glue(helper_vpmaskmovd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
234416fc5726SPaolo Bonzini {
234516fc5726SPaolo Bonzini     int i;
234616fc5726SPaolo Bonzini 
234716fc5726SPaolo Bonzini     for (i = 0; i < (2 << SHIFT); i++) {
234816fc5726SPaolo Bonzini         d->L(i) = (v->L(i) >> 31) ? s->L(i) : 0;
234916fc5726SPaolo Bonzini     }
235016fc5726SPaolo Bonzini }
235116fc5726SPaolo Bonzini 
235216fc5726SPaolo Bonzini void glue(helper_vpmaskmovq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
235316fc5726SPaolo Bonzini {
235416fc5726SPaolo Bonzini     int i;
235516fc5726SPaolo Bonzini 
235616fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
235716fc5726SPaolo Bonzini         d->Q(i) = (v->Q(i) >> 63) ? s->Q(i) : 0;
235816fc5726SPaolo Bonzini     }
235916fc5726SPaolo Bonzini }
236016fc5726SPaolo Bonzini 
236116fc5726SPaolo Bonzini void glue(helper_vpgatherdd, SUFFIX)(CPUX86State *env,
236216fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
236316fc5726SPaolo Bonzini {
236416fc5726SPaolo Bonzini     int i;
236516fc5726SPaolo Bonzini     for (i = 0; i < (2 << SHIFT); i++) {
236616fc5726SPaolo Bonzini         if (v->L(i) >> 31) {
236716fc5726SPaolo Bonzini             target_ulong addr = a0
236816fc5726SPaolo Bonzini                 + ((target_ulong)(int32_t)s->L(i) << scale);
236916fc5726SPaolo Bonzini             d->L(i) = cpu_ldl_data_ra(env, addr, GETPC());
237016fc5726SPaolo Bonzini         }
237116fc5726SPaolo Bonzini         v->L(i) = 0;
237216fc5726SPaolo Bonzini     }
237316fc5726SPaolo Bonzini }
237416fc5726SPaolo Bonzini 
237516fc5726SPaolo Bonzini void glue(helper_vpgatherdq, SUFFIX)(CPUX86State *env,
237616fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
237716fc5726SPaolo Bonzini {
237816fc5726SPaolo Bonzini     int i;
237916fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
238016fc5726SPaolo Bonzini         if (v->Q(i) >> 63) {
238116fc5726SPaolo Bonzini             target_ulong addr = a0
238216fc5726SPaolo Bonzini                 + ((target_ulong)(int32_t)s->L(i) << scale);
238316fc5726SPaolo Bonzini             d->Q(i) = cpu_ldq_data_ra(env, addr, GETPC());
238416fc5726SPaolo Bonzini         }
238516fc5726SPaolo Bonzini         v->Q(i) = 0;
238616fc5726SPaolo Bonzini     }
238716fc5726SPaolo Bonzini }
238816fc5726SPaolo Bonzini 
238916fc5726SPaolo Bonzini void glue(helper_vpgatherqd, SUFFIX)(CPUX86State *env,
239016fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
239116fc5726SPaolo Bonzini {
239216fc5726SPaolo Bonzini     int i;
239316fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
239416fc5726SPaolo Bonzini         if (v->L(i) >> 31) {
239516fc5726SPaolo Bonzini             target_ulong addr = a0
239616fc5726SPaolo Bonzini                 + ((target_ulong)(int64_t)s->Q(i) << scale);
239716fc5726SPaolo Bonzini             d->L(i) = cpu_ldl_data_ra(env, addr, GETPC());
239816fc5726SPaolo Bonzini         }
239916fc5726SPaolo Bonzini         v->L(i) = 0;
240016fc5726SPaolo Bonzini     }
240116fc5726SPaolo Bonzini     for (i /= 2; i < 1 << SHIFT; i++) {
240216fc5726SPaolo Bonzini         d->Q(i) = 0;
240316fc5726SPaolo Bonzini         v->Q(i) = 0;
240416fc5726SPaolo Bonzini     }
240516fc5726SPaolo Bonzini }
240616fc5726SPaolo Bonzini 
240716fc5726SPaolo Bonzini void glue(helper_vpgatherqq, SUFFIX)(CPUX86State *env,
240816fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
240916fc5726SPaolo Bonzini {
241016fc5726SPaolo Bonzini     int i;
241116fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
241216fc5726SPaolo Bonzini         if (v->Q(i) >> 63) {
241316fc5726SPaolo Bonzini             target_ulong addr = a0
241416fc5726SPaolo Bonzini                 + ((target_ulong)(int64_t)s->Q(i) << scale);
241516fc5726SPaolo Bonzini             d->Q(i) = cpu_ldq_data_ra(env, addr, GETPC());
241616fc5726SPaolo Bonzini         }
241716fc5726SPaolo Bonzini         v->Q(i) = 0;
241816fc5726SPaolo Bonzini     }
241916fc5726SPaolo Bonzini }
242016fc5726SPaolo Bonzini #endif
242116fc5726SPaolo Bonzini 
242279068477SPaolo Bonzini #if SHIFT >= 2
242379068477SPaolo Bonzini void helper_vpermdq_ymm(Reg *d, Reg *v, Reg *s, uint32_t order)
242479068477SPaolo Bonzini {
242579068477SPaolo Bonzini     uint64_t r0, r1, r2, r3;
242679068477SPaolo Bonzini 
242779068477SPaolo Bonzini     switch (order & 3) {
242879068477SPaolo Bonzini     case 0:
242979068477SPaolo Bonzini         r0 = v->Q(0);
243079068477SPaolo Bonzini         r1 = v->Q(1);
243179068477SPaolo Bonzini         break;
243279068477SPaolo Bonzini     case 1:
243379068477SPaolo Bonzini         r0 = v->Q(2);
243479068477SPaolo Bonzini         r1 = v->Q(3);
243579068477SPaolo Bonzini         break;
243679068477SPaolo Bonzini     case 2:
243779068477SPaolo Bonzini         r0 = s->Q(0);
243879068477SPaolo Bonzini         r1 = s->Q(1);
243979068477SPaolo Bonzini         break;
244079068477SPaolo Bonzini     case 3:
244179068477SPaolo Bonzini         r0 = s->Q(2);
244279068477SPaolo Bonzini         r1 = s->Q(3);
244379068477SPaolo Bonzini         break;
244479068477SPaolo Bonzini     }
244579068477SPaolo Bonzini     switch ((order >> 4) & 3) {
244679068477SPaolo Bonzini     case 0:
244779068477SPaolo Bonzini         r2 = v->Q(0);
244879068477SPaolo Bonzini         r3 = v->Q(1);
244979068477SPaolo Bonzini         break;
245079068477SPaolo Bonzini     case 1:
245179068477SPaolo Bonzini         r2 = v->Q(2);
245279068477SPaolo Bonzini         r3 = v->Q(3);
245379068477SPaolo Bonzini         break;
245479068477SPaolo Bonzini     case 2:
245579068477SPaolo Bonzini         r2 = s->Q(0);
245679068477SPaolo Bonzini         r3 = s->Q(1);
245779068477SPaolo Bonzini         break;
245879068477SPaolo Bonzini     case 3:
245979068477SPaolo Bonzini         r2 = s->Q(2);
246079068477SPaolo Bonzini         r3 = s->Q(3);
246179068477SPaolo Bonzini         break;
246279068477SPaolo Bonzini     }
246379068477SPaolo Bonzini     d->Q(0) = r0;
246479068477SPaolo Bonzini     d->Q(1) = r1;
246579068477SPaolo Bonzini     d->Q(2) = r2;
246679068477SPaolo Bonzini     d->Q(3) = r3;
246779068477SPaolo Bonzini }
246879068477SPaolo Bonzini 
246979068477SPaolo Bonzini void helper_vpermq_ymm(Reg *d, Reg *s, uint32_t order)
247079068477SPaolo Bonzini {
247179068477SPaolo Bonzini     uint64_t r0, r1, r2, r3;
247279068477SPaolo Bonzini     r0 = s->Q(order & 3);
247379068477SPaolo Bonzini     r1 = s->Q((order >> 2) & 3);
247479068477SPaolo Bonzini     r2 = s->Q((order >> 4) & 3);
247579068477SPaolo Bonzini     r3 = s->Q((order >> 6) & 3);
247679068477SPaolo Bonzini     d->Q(0) = r0;
247779068477SPaolo Bonzini     d->Q(1) = r1;
247879068477SPaolo Bonzini     d->Q(2) = r2;
247979068477SPaolo Bonzini     d->Q(3) = r3;
248079068477SPaolo Bonzini }
248116fc5726SPaolo Bonzini 
248216fc5726SPaolo Bonzini void helper_vpermd_ymm(Reg *d, Reg *v, Reg *s)
248316fc5726SPaolo Bonzini {
248416fc5726SPaolo Bonzini     uint32_t r[8];
248516fc5726SPaolo Bonzini     int i;
248616fc5726SPaolo Bonzini 
248716fc5726SPaolo Bonzini     for (i = 0; i < 8; i++) {
248816fc5726SPaolo Bonzini         r[i] = s->L(v->L(i) & 7);
248916fc5726SPaolo Bonzini     }
249016fc5726SPaolo Bonzini     for (i = 0; i < 8; i++) {
249116fc5726SPaolo Bonzini         d->L(i) = r[i];
249216fc5726SPaolo Bonzini     }
249316fc5726SPaolo Bonzini }
249479068477SPaolo Bonzini #endif
249579068477SPaolo Bonzini 
24963403cafeSPaul Brook #undef SSE_HELPER_S
24973403cafeSPaul Brook 
2498b98f886cSPaolo Bonzini #undef LANE_WIDTH
2499fcf5ef2aSThomas Huth #undef SHIFT
2500fcf5ef2aSThomas Huth #undef XMM_ONLY
2501fcf5ef2aSThomas Huth #undef Reg
2502fcf5ef2aSThomas Huth #undef B
2503fcf5ef2aSThomas Huth #undef W
2504fcf5ef2aSThomas Huth #undef L
2505fcf5ef2aSThomas Huth #undef Q
2506fcf5ef2aSThomas Huth #undef SUFFIX
2507