xref: /openbmc/qemu/target/i386/ops_sse.h (revision 78ef97c0aa6c03d78950116921cdf893632d22b1)
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"
22cc648f50SRichard Henderson #include "crypto/aes-round.h"
237fcb5054SRichard Henderson #include "crypto/clmul.h"
24fcf5ef2aSThomas Huth 
25fcf5ef2aSThomas Huth #if SHIFT == 0
26fcf5ef2aSThomas Huth #define Reg MMXReg
27fcf5ef2aSThomas Huth #define XMM_ONLY(...)
28fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
29fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
30fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
31fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
32fcf5ef2aSThomas Huth #define SUFFIX _mmx
33fcf5ef2aSThomas Huth #else
34fcf5ef2aSThomas Huth #define Reg ZMMReg
35fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
36fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
37fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
38fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
39fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
40b98f886cSPaolo Bonzini #if SHIFT == 1
41fcf5ef2aSThomas Huth #define SUFFIX _xmm
42b98f886cSPaolo Bonzini #else
43b98f886cSPaolo Bonzini #define SUFFIX _ymm
44b98f886cSPaolo Bonzini #endif
45fcf5ef2aSThomas Huth #endif
46fcf5ef2aSThomas Huth 
4718592d2eSPaul Brook #define LANE_WIDTH (SHIFT ? 16 : 8)
48d45b0de6SPaul Brook #define PACK_WIDTH (LANE_WIDTH / 2)
4918592d2eSPaul Brook 
5018592d2eSPaul Brook #if SHIFT == 0
5118592d2eSPaul Brook #define FPSRL(x, c) ((x) >> shift)
5218592d2eSPaul Brook #define FPSRAW(x, c) ((int16_t)(x) >> shift)
5318592d2eSPaul Brook #define FPSRAL(x, c) ((int32_t)(x) >> shift)
5418592d2eSPaul Brook #define FPSLL(x, c) ((x) << shift)
55fcf5ef2aSThomas Huth #endif
5618592d2eSPaul Brook 
glue(helper_psrlw,SUFFIX)57f05f9789SPaolo Bonzini void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
5818592d2eSPaul Brook {
5918592d2eSPaul Brook     int shift;
6018592d2eSPaul Brook     if (c->Q(0) > 15) {
6118592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
6218592d2eSPaul Brook             d->Q(i) = 0;
6318592d2eSPaul Brook         }
64fcf5ef2aSThomas Huth     } else {
6518592d2eSPaul Brook         shift = c->B(0);
6618592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
6718592d2eSPaul Brook             d->W(i) = FPSRL(s->W(i), shift);
6818592d2eSPaul Brook         }
69fcf5ef2aSThomas Huth     }
70fcf5ef2aSThomas Huth }
71fcf5ef2aSThomas Huth 
glue(helper_psllw,SUFFIX)72f05f9789SPaolo Bonzini void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
73fcf5ef2aSThomas Huth {
74fcf5ef2aSThomas Huth     int shift;
7518592d2eSPaul Brook     if (c->Q(0) > 15) {
7618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
7718592d2eSPaul Brook             d->Q(i) = 0;
7818592d2eSPaul Brook         }
7918592d2eSPaul Brook     } else {
8018592d2eSPaul Brook         shift = c->B(0);
8118592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
8218592d2eSPaul Brook             d->W(i) = FPSLL(s->W(i), shift);
8318592d2eSPaul Brook         }
8418592d2eSPaul Brook     }
8518592d2eSPaul Brook }
86fcf5ef2aSThomas Huth 
glue(helper_psraw,SUFFIX)87f05f9789SPaolo Bonzini void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
8818592d2eSPaul Brook {
8918592d2eSPaul Brook     int shift;
9018592d2eSPaul Brook     if (c->Q(0) > 15) {
91fcf5ef2aSThomas Huth         shift = 15;
92fcf5ef2aSThomas Huth     } else {
9318592d2eSPaul Brook         shift = c->B(0);
94fcf5ef2aSThomas Huth     }
9518592d2eSPaul Brook     for (int i = 0; i < 4 << SHIFT; i++) {
9618592d2eSPaul Brook         d->W(i) = FPSRAW(s->W(i), shift);
9718592d2eSPaul Brook     }
98fcf5ef2aSThomas Huth }
99fcf5ef2aSThomas Huth 
glue(helper_psrld,SUFFIX)100f05f9789SPaolo Bonzini void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
101fcf5ef2aSThomas Huth {
102fcf5ef2aSThomas Huth     int shift;
10318592d2eSPaul Brook     if (c->Q(0) > 31) {
10418592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
10518592d2eSPaul Brook             d->Q(i) = 0;
10618592d2eSPaul Brook         }
107fcf5ef2aSThomas Huth     } else {
10818592d2eSPaul Brook         shift = c->B(0);
10918592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
11018592d2eSPaul Brook             d->L(i) = FPSRL(s->L(i), shift);
11118592d2eSPaul Brook         }
112fcf5ef2aSThomas Huth     }
113fcf5ef2aSThomas Huth }
114fcf5ef2aSThomas Huth 
glue(helper_pslld,SUFFIX)115f05f9789SPaolo Bonzini void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
116fcf5ef2aSThomas Huth {
117fcf5ef2aSThomas Huth     int shift;
11818592d2eSPaul Brook     if (c->Q(0) > 31) {
11918592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
12018592d2eSPaul Brook             d->Q(i) = 0;
12118592d2eSPaul Brook         }
122fcf5ef2aSThomas Huth     } else {
12318592d2eSPaul Brook         shift = c->B(0);
12418592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
12518592d2eSPaul Brook             d->L(i) = FPSLL(s->L(i), shift);
12618592d2eSPaul Brook         }
127fcf5ef2aSThomas Huth     }
128fcf5ef2aSThomas Huth }
129fcf5ef2aSThomas Huth 
glue(helper_psrad,SUFFIX)130f05f9789SPaolo Bonzini void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
131fcf5ef2aSThomas Huth {
132fcf5ef2aSThomas Huth     int shift;
13318592d2eSPaul Brook     if (c->Q(0) > 31) {
134fcf5ef2aSThomas Huth         shift = 31;
135fcf5ef2aSThomas Huth     } else {
13618592d2eSPaul Brook         shift = c->B(0);
137fcf5ef2aSThomas Huth     }
13818592d2eSPaul Brook     for (int i = 0; i < 2 << SHIFT; i++) {
13918592d2eSPaul Brook         d->L(i) = FPSRAL(s->L(i), shift);
14018592d2eSPaul Brook     }
141fcf5ef2aSThomas Huth }
142fcf5ef2aSThomas Huth 
glue(helper_psrlq,SUFFIX)143f05f9789SPaolo Bonzini void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
144fcf5ef2aSThomas Huth {
145fcf5ef2aSThomas Huth     int shift;
14618592d2eSPaul Brook     if (c->Q(0) > 63) {
14718592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14818592d2eSPaul Brook             d->Q(i) = 0;
14918592d2eSPaul Brook         }
150fcf5ef2aSThomas Huth     } else {
15118592d2eSPaul Brook         shift = c->B(0);
15218592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
15318592d2eSPaul Brook             d->Q(i) = FPSRL(s->Q(i), shift);
15418592d2eSPaul Brook         }
155fcf5ef2aSThomas Huth     }
156fcf5ef2aSThomas Huth }
157fcf5ef2aSThomas Huth 
glue(helper_psllq,SUFFIX)158f05f9789SPaolo Bonzini void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
159fcf5ef2aSThomas Huth {
160fcf5ef2aSThomas Huth     int shift;
16118592d2eSPaul Brook     if (c->Q(0) > 63) {
16218592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16318592d2eSPaul Brook             d->Q(i) = 0;
16418592d2eSPaul Brook         }
165fcf5ef2aSThomas Huth     } else {
16618592d2eSPaul Brook         shift = c->B(0);
16718592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16818592d2eSPaul Brook             d->Q(i) = FPSLL(s->Q(i), shift);
16918592d2eSPaul Brook         }
170fcf5ef2aSThomas Huth     }
171fcf5ef2aSThomas Huth }
172fcf5ef2aSThomas Huth 
17318592d2eSPaul Brook #if SHIFT >= 1
glue(helper_psrldq,SUFFIX)174f05f9789SPaolo Bonzini void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
175fcf5ef2aSThomas Huth {
17618592d2eSPaul Brook     int shift, i, j;
177fcf5ef2aSThomas Huth 
17818592d2eSPaul Brook     shift = c->L(0);
179fcf5ef2aSThomas Huth     if (shift > 16) {
180fcf5ef2aSThomas Huth         shift = 16;
181fcf5ef2aSThomas Huth     }
18218592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
183fcf5ef2aSThomas Huth         for (i = 0; i < 16 - shift; i++) {
18418592d2eSPaul Brook             d->B(j + i) = s->B(j + i + shift);
185fcf5ef2aSThomas Huth         }
186fcf5ef2aSThomas Huth         for (i = 16 - shift; i < 16; i++) {
18718592d2eSPaul Brook             d->B(j + i) = 0;
18818592d2eSPaul Brook         }
189fcf5ef2aSThomas Huth     }
190fcf5ef2aSThomas Huth }
191fcf5ef2aSThomas Huth 
glue(helper_pslldq,SUFFIX)192f05f9789SPaolo Bonzini void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
193fcf5ef2aSThomas Huth {
19418592d2eSPaul Brook     int shift, i, j;
195fcf5ef2aSThomas Huth 
19618592d2eSPaul Brook     shift = c->L(0);
197fcf5ef2aSThomas Huth     if (shift > 16) {
198fcf5ef2aSThomas Huth         shift = 16;
199fcf5ef2aSThomas Huth     }
20018592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
201fcf5ef2aSThomas Huth         for (i = 15; i >= shift; i--) {
20218592d2eSPaul Brook             d->B(j + i) = s->B(j + i - shift);
203fcf5ef2aSThomas Huth         }
204fcf5ef2aSThomas Huth         for (i = 0; i < shift; i++) {
20518592d2eSPaul Brook             d->B(j + i) = 0;
20618592d2eSPaul Brook         }
207fcf5ef2aSThomas Huth     }
208fcf5ef2aSThomas Huth }
209fcf5ef2aSThomas Huth #endif
210fcf5ef2aSThomas Huth 
211ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F)                        \
212fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
213fcf5ef2aSThomas Huth     {                                                           \
214ee04a3c8SPaul Brook         int n = num;                                            \
215ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
216ee04a3c8SPaul Brook             d->elem(i) = F(s->elem(i));                         \
217ee04a3c8SPaul Brook         }                                                       \
218fcf5ef2aSThomas Huth     }
219fcf5ef2aSThomas Huth 
220ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F)                        \
221f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)   \
222ee04a3c8SPaul Brook     {                                                           \
223ee04a3c8SPaul Brook         int n = num;                                            \
224ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
225ee04a3c8SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i));             \
226ee04a3c8SPaul Brook         }                                                       \
227ee04a3c8SPaul Brook     }
228ee04a3c8SPaul Brook 
229ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F)                                   \
230ee04a3c8SPaul Brook     SSE_HELPER_2(name, B, 8 << SHIFT, F)
231ee04a3c8SPaul Brook 
232fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
233ee04a3c8SPaul Brook     SSE_HELPER_2(name, W, 4 << SHIFT, F)
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
236ee04a3c8SPaul Brook     SSE_HELPER_2(name, L, 2 << SHIFT, F)
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
239ee04a3c8SPaul Brook     SSE_HELPER_2(name, Q, 1 << SHIFT, F)
240fcf5ef2aSThomas Huth 
241fcf5ef2aSThomas Huth #if SHIFT == 0
satub(int x)242fcf5ef2aSThomas Huth static inline int satub(int x)
243fcf5ef2aSThomas Huth {
244fcf5ef2aSThomas Huth     if (x < 0) {
245fcf5ef2aSThomas Huth         return 0;
246fcf5ef2aSThomas Huth     } else if (x > 255) {
247fcf5ef2aSThomas Huth         return 255;
248fcf5ef2aSThomas Huth     } else {
249fcf5ef2aSThomas Huth         return x;
250fcf5ef2aSThomas Huth     }
251fcf5ef2aSThomas Huth }
252fcf5ef2aSThomas Huth 
satuw(int x)253fcf5ef2aSThomas Huth static inline int satuw(int x)
254fcf5ef2aSThomas Huth {
255fcf5ef2aSThomas Huth     if (x < 0) {
256fcf5ef2aSThomas Huth         return 0;
257fcf5ef2aSThomas Huth     } else if (x > 65535) {
258fcf5ef2aSThomas Huth         return 65535;
259fcf5ef2aSThomas Huth     } else {
260fcf5ef2aSThomas Huth         return x;
261fcf5ef2aSThomas Huth     }
262fcf5ef2aSThomas Huth }
263fcf5ef2aSThomas Huth 
satsb(int x)264fcf5ef2aSThomas Huth static inline int satsb(int x)
265fcf5ef2aSThomas Huth {
266fcf5ef2aSThomas Huth     if (x < -128) {
267fcf5ef2aSThomas Huth         return -128;
268fcf5ef2aSThomas Huth     } else if (x > 127) {
269fcf5ef2aSThomas Huth         return 127;
270fcf5ef2aSThomas Huth     } else {
271fcf5ef2aSThomas Huth         return x;
272fcf5ef2aSThomas Huth     }
273fcf5ef2aSThomas Huth }
274fcf5ef2aSThomas Huth 
satsw(int x)275fcf5ef2aSThomas Huth static inline int satsw(int x)
276fcf5ef2aSThomas Huth {
277fcf5ef2aSThomas Huth     if (x < -32768) {
278fcf5ef2aSThomas Huth         return -32768;
279fcf5ef2aSThomas Huth     } else if (x > 32767) {
280fcf5ef2aSThomas Huth         return 32767;
281fcf5ef2aSThomas Huth     } else {
282fcf5ef2aSThomas Huth         return x;
283fcf5ef2aSThomas Huth     }
284fcf5ef2aSThomas Huth }
285fcf5ef2aSThomas Huth 
286fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
287fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
288fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
289fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
290fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
291fcf5ef2aSThomas Huth 
292fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
293fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
294fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
295fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
296fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
297fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
298fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
299fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
300fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
301fcf5ef2aSThomas Huth 
302fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
303fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
304fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
305fcf5ef2aSThomas Huth 
306fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
307fcf5ef2aSThomas Huth #endif
308fcf5ef2aSThomas Huth 
SSE_HELPER_W(helper_pmulhuw,FMULHUW)309fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
310fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
311fcf5ef2aSThomas Huth 
312f05f9789SPaolo Bonzini #if SHIFT == 0
313f05f9789SPaolo Bonzini void glue(helper_pmulhrw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
314f05f9789SPaolo Bonzini {
315f05f9789SPaolo Bonzini     d->W(0) = FMULHRW(d->W(0), s->W(0));
316f05f9789SPaolo Bonzini     d->W(1) = FMULHRW(d->W(1), s->W(1));
317f05f9789SPaolo Bonzini     d->W(2) = FMULHRW(d->W(2), s->W(2));
318f05f9789SPaolo Bonzini     d->W(3) = FMULHRW(d->W(3), s->W(3));
319f05f9789SPaolo Bonzini }
320f05f9789SPaolo Bonzini #endif
321f05f9789SPaolo Bonzini 
SSE_HELPER_B(helper_pavgb,FAVG)322fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
323fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
324fcf5ef2aSThomas Huth 
325f05f9789SPaolo Bonzini void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
326fcf5ef2aSThomas Huth {
327e894bae8SPaul Brook     int i;
328e894bae8SPaul Brook 
329e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
330e894bae8SPaul Brook         d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2);
331e894bae8SPaul Brook     }
332fcf5ef2aSThomas Huth }
333fcf5ef2aSThomas Huth 
glue(helper_pmaddwd,SUFFIX)334f05f9789SPaolo Bonzini void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
335fcf5ef2aSThomas Huth {
336fcf5ef2aSThomas Huth     int i;
337fcf5ef2aSThomas Huth 
338fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
339e894bae8SPaul Brook         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) +
340e894bae8SPaul Brook             (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1);
341fcf5ef2aSThomas Huth     }
342fcf5ef2aSThomas Huth }
343fcf5ef2aSThomas Huth 
344fcf5ef2aSThomas Huth #if SHIFT == 0
abs1(int a)345fcf5ef2aSThomas Huth static inline int abs1(int a)
346fcf5ef2aSThomas Huth {
347fcf5ef2aSThomas Huth     if (a < 0) {
348fcf5ef2aSThomas Huth         return -a;
349fcf5ef2aSThomas Huth     } else {
350fcf5ef2aSThomas Huth         return a;
351fcf5ef2aSThomas Huth     }
352fcf5ef2aSThomas Huth }
353fcf5ef2aSThomas Huth #endif
glue(helper_psadbw,SUFFIX)354f05f9789SPaolo Bonzini void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
355fcf5ef2aSThomas Huth {
356e894bae8SPaul Brook     int i;
357fcf5ef2aSThomas Huth 
358e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
359e894bae8SPaul Brook         unsigned int val = 0;
360e894bae8SPaul Brook         val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0));
361e894bae8SPaul Brook         val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1));
362e894bae8SPaul Brook         val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2));
363e894bae8SPaul Brook         val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3));
364e894bae8SPaul Brook         val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4));
365e894bae8SPaul Brook         val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5));
366e894bae8SPaul Brook         val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6));
367e894bae8SPaul Brook         val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7));
368e894bae8SPaul Brook         d->Q(i) = val;
369e894bae8SPaul Brook     }
370fcf5ef2aSThomas Huth }
371fcf5ef2aSThomas Huth 
372fd17264aSPaul Brook #if SHIFT < 2
glue(helper_maskmov,SUFFIX)373fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
374fcf5ef2aSThomas Huth                                   target_ulong a0)
375fcf5ef2aSThomas Huth {
376fcf5ef2aSThomas Huth     int i;
377fcf5ef2aSThomas Huth 
378fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
379fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
380fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
381fcf5ef2aSThomas Huth         }
382fcf5ef2aSThomas Huth     }
383fcf5ef2aSThomas Huth }
384fd17264aSPaul Brook #endif
385fcf5ef2aSThomas Huth 
386d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
387d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
388d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
389d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
390d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
391d45b0de6SPaul Brook     d->F(offset) = r0;                      \
392d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
393d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
394d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
395d45b0de6SPaul Brook     } while (0)
396d45b0de6SPaul Brook 
397fcf5ef2aSThomas Huth #if SHIFT == 0
glue(helper_pshufw,SUFFIX)398fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
399fcf5ef2aSThomas Huth {
400d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
401fcf5ef2aSThomas Huth 
402d45b0de6SPaul Brook     SHUFFLE4(W, s, s, 0);
403fcf5ef2aSThomas Huth }
404fcf5ef2aSThomas Huth #else
glue(helper_shufps,SUFFIX)405f05f9789SPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
406fcf5ef2aSThomas Huth {
407d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
408d45b0de6SPaul Brook     int i;
409fcf5ef2aSThomas Huth 
410d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
411d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
412d45b0de6SPaul Brook     }
413fcf5ef2aSThomas Huth }
414fcf5ef2aSThomas Huth 
glue(helper_shufpd,SUFFIX)415f05f9789SPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
416fcf5ef2aSThomas Huth {
417d45b0de6SPaul Brook     uint64_t r0, r1;
418d45b0de6SPaul Brook     int i;
419fcf5ef2aSThomas Huth 
420d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
421d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
422d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
423d45b0de6SPaul Brook         d->Q(i) = r0;
424d45b0de6SPaul Brook         d->Q(i + 1) = r1;
425d45b0de6SPaul Brook         order >>= 2;
426d45b0de6SPaul Brook     }
427fcf5ef2aSThomas Huth }
428fcf5ef2aSThomas Huth 
glue(helper_pshufd,SUFFIX)429fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
430fcf5ef2aSThomas Huth {
431d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
432d45b0de6SPaul Brook     int i;
433fcf5ef2aSThomas Huth 
434d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
435d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
436d45b0de6SPaul Brook     }
437fcf5ef2aSThomas Huth }
438fcf5ef2aSThomas Huth 
glue(helper_pshuflw,SUFFIX)439fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
440fcf5ef2aSThomas Huth {
441d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
442d45b0de6SPaul Brook     int i, j;
443fcf5ef2aSThomas Huth 
444d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
445d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
446d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
447d45b0de6SPaul Brook     }
448fcf5ef2aSThomas Huth }
449fcf5ef2aSThomas Huth 
glue(helper_pshufhw,SUFFIX)450fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
451fcf5ef2aSThomas Huth {
452d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
453d45b0de6SPaul Brook     int i, j;
454fcf5ef2aSThomas Huth 
455d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
456d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
457d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
458d45b0de6SPaul Brook     }
459fcf5ef2aSThomas Huth }
460fcf5ef2aSThomas Huth #endif
461fcf5ef2aSThomas Huth 
4623403cafeSPaul Brook #if SHIFT >= 1
463fcf5ef2aSThomas Huth /* FPU ops */
464fcf5ef2aSThomas Huth /* XXX: not accurate */
465fcf5ef2aSThomas Huth 
4663403cafeSPaul Brook #define SSE_HELPER_P(name, F)                                           \
4673403cafeSPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
468f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
469fcf5ef2aSThomas Huth     {                                                                   \
4703403cafeSPaul Brook         int i;                                                          \
4713403cafeSPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
4723403cafeSPaul Brook             d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i));              \
473fcf5ef2aSThomas Huth         }                                                               \
4743403cafeSPaul Brook     }                                                                   \
4753403cafeSPaul Brook                                                                         \
4763403cafeSPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
477f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
4783403cafeSPaul Brook     {                                                                   \
4793403cafeSPaul Brook         int i;                                                          \
4803403cafeSPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
4813403cafeSPaul Brook             d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i));              \
4823403cafeSPaul Brook         }                                                               \
4833403cafeSPaul Brook     }
4843403cafeSPaul Brook 
4853403cafeSPaul Brook #if SHIFT == 1
4863403cafeSPaul Brook 
4873403cafeSPaul Brook #define SSE_HELPER_S(name, F)                                           \
4883403cafeSPaul Brook     SSE_HELPER_P(name, F)                                               \
489fcf5ef2aSThomas Huth                                                                         \
490f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
491fcf5ef2aSThomas Huth     {                                                                   \
4921de9e7e6SPaolo Bonzini         int i;                                                          \
4933403cafeSPaul Brook         d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0));                  \
4941de9e7e6SPaolo Bonzini         for (i = 1; i < 2 << SHIFT; i++) {                              \
4951de9e7e6SPaolo Bonzini             d->ZMM_L(i) = v->ZMM_L(i);                                  \
4961de9e7e6SPaolo Bonzini         }                                                               \
497fcf5ef2aSThomas Huth     }                                                                   \
498fcf5ef2aSThomas Huth                                                                         \
499f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
500fcf5ef2aSThomas Huth     {                                                                   \
5011de9e7e6SPaolo Bonzini         int i;                                                          \
5023403cafeSPaul Brook         d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0));                  \
5031de9e7e6SPaolo Bonzini         for (i = 1; i < 1 << SHIFT; i++) {                              \
5041de9e7e6SPaolo Bonzini             d->ZMM_Q(i) = v->ZMM_Q(i);                                  \
5051de9e7e6SPaolo Bonzini         }                                                               \
506fcf5ef2aSThomas Huth     }
507fcf5ef2aSThomas Huth 
5083403cafeSPaul Brook #else
5093403cafeSPaul Brook 
5103403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F)
5113403cafeSPaul Brook 
5123403cafeSPaul Brook #endif
5133403cafeSPaul Brook 
514fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
515fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
516fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
517fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
518fcf5ef2aSThomas Huth 
519fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
520fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
521fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
522fcf5ef2aSThomas Huth  */
523fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
524fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
525fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
526fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
527fcf5ef2aSThomas Huth 
SSE_HELPER_S(add,FPU_ADD)528fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
529fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
530fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
531fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
532fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
533fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
534fcf5ef2aSThomas Huth 
5353403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
5363403cafeSPaul Brook {
5373403cafeSPaul Brook     int i;
5383403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
5393403cafeSPaul Brook         d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status);
5403403cafeSPaul Brook     }
5413403cafeSPaul Brook }
5423403cafeSPaul Brook 
glue(helper_sqrtpd,SUFFIX)5433403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
5443403cafeSPaul Brook {
5453403cafeSPaul Brook     int i;
5463403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
5473403cafeSPaul Brook         d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status);
5483403cafeSPaul Brook     }
5493403cafeSPaul Brook }
5503403cafeSPaul Brook 
5513403cafeSPaul Brook #if SHIFT == 1
helper_sqrtss(CPUX86State * env,Reg * d,Reg * v,Reg * s)552620f7556SPaolo Bonzini void helper_sqrtss(CPUX86State *env, Reg *d, Reg *v, Reg *s)
5533403cafeSPaul Brook {
554620f7556SPaolo Bonzini     int i;
5553403cafeSPaul Brook     d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status);
556620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
557620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
558620f7556SPaolo Bonzini     }
5593403cafeSPaul Brook }
5603403cafeSPaul Brook 
helper_sqrtsd(CPUX86State * env,Reg * d,Reg * v,Reg * s)561620f7556SPaolo Bonzini void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *v, Reg *s)
5623403cafeSPaul Brook {
563620f7556SPaolo Bonzini     int i;
5643403cafeSPaul Brook     d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status);
565620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
566620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
567620f7556SPaolo Bonzini     }
5683403cafeSPaul Brook }
5693403cafeSPaul Brook #endif
570fcf5ef2aSThomas Huth 
571fcf5ef2aSThomas Huth /* float to float conversions */
glue(helper_cvtps2pd,SUFFIX)572ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
573fcf5ef2aSThomas Huth {
574fd17264aSPaul Brook     int i;
575fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
576fd17264aSPaul Brook         d->ZMM_D(i) = float32_to_float64(s->ZMM_S(i), &env->sse_status);
577fd17264aSPaul Brook     }
578fcf5ef2aSThomas Huth }
579fcf5ef2aSThomas Huth 
glue(helper_cvtpd2ps,SUFFIX)580ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
581fcf5ef2aSThomas Huth {
582fd17264aSPaul Brook     int i;
583fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
584fd17264aSPaul Brook          d->ZMM_S(i) = float64_to_float32(s->ZMM_D(i), &env->sse_status);
585fd17264aSPaul Brook     }
586fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
587fd17264aSPaul Brook          d->Q(i) = 0;
588fd17264aSPaul Brook     }
589fcf5ef2aSThomas Huth }
590fcf5ef2aSThomas Huth 
591cf5ec664SPaolo Bonzini #if SHIFT >= 1
glue(helper_cvtph2ps,SUFFIX)592cf5ec664SPaolo Bonzini void glue(helper_cvtph2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
593cf5ec664SPaolo Bonzini {
594cf5ec664SPaolo Bonzini     int i;
595cf5ec664SPaolo Bonzini 
596cf5ec664SPaolo Bonzini     for (i = 2 << SHIFT; --i >= 0; ) {
597cf5ec664SPaolo Bonzini          d->ZMM_S(i) = float16_to_float32(s->ZMM_H(i), true, &env->sse_status);
598cf5ec664SPaolo Bonzini     }
599cf5ec664SPaolo Bonzini }
600cf5ec664SPaolo Bonzini 
glue(helper_cvtps2ph,SUFFIX)601cf5ec664SPaolo Bonzini void glue(helper_cvtps2ph, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, int mode)
602cf5ec664SPaolo Bonzini {
603cf5ec664SPaolo Bonzini     int i;
604cf5ec664SPaolo Bonzini     FloatRoundMode prev_rounding_mode = env->sse_status.float_rounding_mode;
605cf5ec664SPaolo Bonzini     if (!(mode & (1 << 2))) {
606cf5ec664SPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
607cf5ec664SPaolo Bonzini     }
608cf5ec664SPaolo Bonzini 
609cf5ec664SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i++) {
610cf5ec664SPaolo Bonzini         d->ZMM_H(i) = float32_to_float16(s->ZMM_S(i), true, &env->sse_status);
611cf5ec664SPaolo Bonzini     }
612cf5ec664SPaolo Bonzini     for (i >>= 2; i < 1 << SHIFT; i++) {
613cf5ec664SPaolo Bonzini         d->Q(i) = 0;
614cf5ec664SPaolo Bonzini     }
615cf5ec664SPaolo Bonzini 
616cf5ec664SPaolo Bonzini     env->sse_status.float_rounding_mode = prev_rounding_mode;
617cf5ec664SPaolo Bonzini }
618cf5ec664SPaolo Bonzini #endif
619cf5ec664SPaolo Bonzini 
620fd17264aSPaul Brook #if SHIFT == 1
helper_cvtss2sd(CPUX86State * env,Reg * d,Reg * v,Reg * s)621620f7556SPaolo Bonzini void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)
622fcf5ef2aSThomas Huth {
623620f7556SPaolo Bonzini     int i;
624fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
625620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
626620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
627620f7556SPaolo Bonzini     }
628fcf5ef2aSThomas Huth }
629fcf5ef2aSThomas Huth 
helper_cvtsd2ss(CPUX86State * env,Reg * d,Reg * v,Reg * s)630620f7556SPaolo Bonzini void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)
631fcf5ef2aSThomas Huth {
632620f7556SPaolo Bonzini     int i;
633fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
634620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
635620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
636620f7556SPaolo Bonzini     }
637fcf5ef2aSThomas Huth }
638fd17264aSPaul Brook #endif
639fcf5ef2aSThomas Huth 
640fcf5ef2aSThomas Huth /* integer to float */
glue(helper_cvtdq2ps,SUFFIX)641ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
642fcf5ef2aSThomas Huth {
643fd17264aSPaul Brook     int i;
644fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
645fd17264aSPaul Brook         d->ZMM_S(i) = int32_to_float32(s->ZMM_L(i), &env->sse_status);
646fd17264aSPaul Brook     }
647fcf5ef2aSThomas Huth }
648fcf5ef2aSThomas Huth 
glue(helper_cvtdq2pd,SUFFIX)649ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
650fcf5ef2aSThomas Huth {
651fd17264aSPaul Brook     int i;
652fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
653fd17264aSPaul Brook         int32_t l = s->ZMM_L(i);
654fd17264aSPaul Brook         d->ZMM_D(i) = int32_to_float64(l, &env->sse_status);
655fd17264aSPaul Brook     }
656fcf5ef2aSThomas Huth }
657fcf5ef2aSThomas Huth 
658fd17264aSPaul Brook #if SHIFT == 1
helper_cvtpi2ps(CPUX86State * env,ZMMReg * d,MMXReg * s)659fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
660fcf5ef2aSThomas Huth {
661fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
662fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
663fcf5ef2aSThomas Huth }
664fcf5ef2aSThomas Huth 
helper_cvtpi2pd(CPUX86State * env,ZMMReg * d,MMXReg * s)665fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
666fcf5ef2aSThomas Huth {
667fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
668fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
669fcf5ef2aSThomas Huth }
670fcf5ef2aSThomas Huth 
helper_cvtsi2ss(CPUX86State * env,ZMMReg * d,uint32_t val)671fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
672fcf5ef2aSThomas Huth {
673fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
674fcf5ef2aSThomas Huth }
675fcf5ef2aSThomas Huth 
helper_cvtsi2sd(CPUX86State * env,ZMMReg * d,uint32_t val)676fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
677fcf5ef2aSThomas Huth {
678fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
679fcf5ef2aSThomas Huth }
680fcf5ef2aSThomas Huth 
681fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
helper_cvtsq2ss(CPUX86State * env,ZMMReg * d,uint64_t val)682fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
683fcf5ef2aSThomas Huth {
684fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
685fcf5ef2aSThomas Huth }
686fcf5ef2aSThomas Huth 
helper_cvtsq2sd(CPUX86State * env,ZMMReg * d,uint64_t val)687fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
688fcf5ef2aSThomas Huth {
689fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
690fcf5ef2aSThomas Huth }
691fcf5ef2aSThomas Huth #endif
692fcf5ef2aSThomas Huth 
693fd17264aSPaul Brook #endif
694fd17264aSPaul Brook 
695fcf5ef2aSThomas Huth /* float to integer */
6961e8a98b5SPeter Maydell 
697fd17264aSPaul Brook #if SHIFT == 1
6981e8a98b5SPeter Maydell /*
6991e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7001e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7011e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7021e8a98b5SPeter Maydell  */
7031e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7041e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7051e8a98b5SPeter Maydell     {                                                                   \
7061e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7071e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7081e8a98b5SPeter Maydell                                                                         \
7091e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7101e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7111e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7121e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7131e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7141e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7151e8a98b5SPeter Maydell         }                                                               \
7161e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7171e8a98b5SPeter Maydell         return r;                                                       \
7181e8a98b5SPeter Maydell     }
7191e8a98b5SPeter Maydell 
WRAP_FLOATCONV(int32_t,float32_to_int32,float32,INT32_MIN)7201e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7211e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7221e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7231e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7241e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7251e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7261e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7271e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
728fd17264aSPaul Brook #endif
7291e8a98b5SPeter Maydell 
730ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
731fcf5ef2aSThomas Huth {
732fd17264aSPaul Brook     int i;
733fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
734fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32(s->ZMM_S(i), &env->sse_status);
735fd17264aSPaul Brook     }
736fcf5ef2aSThomas Huth }
737fcf5ef2aSThomas Huth 
glue(helper_cvtpd2dq,SUFFIX)738ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
739fcf5ef2aSThomas Huth {
740fd17264aSPaul Brook     int i;
741fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
742fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32(s->ZMM_D(i), &env->sse_status);
743fd17264aSPaul Brook     }
744fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
745fd17264aSPaul Brook          d->Q(i) = 0;
746fd17264aSPaul Brook     }
747fcf5ef2aSThomas Huth }
748fcf5ef2aSThomas Huth 
749fd17264aSPaul Brook #if SHIFT == 1
helper_cvtps2pi(CPUX86State * env,MMXReg * d,ZMMReg * s)750fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
751fcf5ef2aSThomas Huth {
7521e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7531e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
754fcf5ef2aSThomas Huth }
755fcf5ef2aSThomas Huth 
helper_cvtpd2pi(CPUX86State * env,MMXReg * d,ZMMReg * s)756fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
757fcf5ef2aSThomas Huth {
7581e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7591e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
760fcf5ef2aSThomas Huth }
761fcf5ef2aSThomas Huth 
helper_cvtss2si(CPUX86State * env,ZMMReg * s)762fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
763fcf5ef2aSThomas Huth {
7641e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
765fcf5ef2aSThomas Huth }
766fcf5ef2aSThomas Huth 
helper_cvtsd2si(CPUX86State * env,ZMMReg * s)767fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
768fcf5ef2aSThomas Huth {
7691e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
770fcf5ef2aSThomas Huth }
771fcf5ef2aSThomas Huth 
772fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
helper_cvtss2sq(CPUX86State * env,ZMMReg * s)773fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
774fcf5ef2aSThomas Huth {
7751e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
776fcf5ef2aSThomas Huth }
777fcf5ef2aSThomas Huth 
helper_cvtsd2sq(CPUX86State * env,ZMMReg * s)778fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
779fcf5ef2aSThomas Huth {
7801e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
781fcf5ef2aSThomas Huth }
782fcf5ef2aSThomas Huth #endif
783fd17264aSPaul Brook #endif
784fcf5ef2aSThomas Huth 
785fcf5ef2aSThomas Huth /* float to integer truncated */
glue(helper_cvttps2dq,SUFFIX)786ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
787fcf5ef2aSThomas Huth {
788fd17264aSPaul Brook     int i;
789fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
790fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32_round_to_zero(s->ZMM_S(i),
791fd17264aSPaul Brook                                                          &env->sse_status);
792fd17264aSPaul Brook     }
793fcf5ef2aSThomas Huth }
794fcf5ef2aSThomas Huth 
glue(helper_cvttpd2dq,SUFFIX)795ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
796fcf5ef2aSThomas Huth {
797fd17264aSPaul Brook     int i;
798fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
799fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32_round_to_zero(s->ZMM_D(i),
800fd17264aSPaul Brook                                                          &env->sse_status);
801fd17264aSPaul Brook     }
802fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
803fd17264aSPaul Brook          d->Q(i) = 0;
804fd17264aSPaul Brook     }
805fcf5ef2aSThomas Huth }
806fcf5ef2aSThomas Huth 
807fd17264aSPaul Brook #if SHIFT == 1
helper_cvttps2pi(CPUX86State * env,MMXReg * d,ZMMReg * s)808fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
809fcf5ef2aSThomas Huth {
8101e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8111e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
812fcf5ef2aSThomas Huth }
813fcf5ef2aSThomas Huth 
helper_cvttpd2pi(CPUX86State * env,MMXReg * d,ZMMReg * s)814fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
815fcf5ef2aSThomas Huth {
8161e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8171e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
818fcf5ef2aSThomas Huth }
819fcf5ef2aSThomas Huth 
helper_cvttss2si(CPUX86State * env,ZMMReg * s)820fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
821fcf5ef2aSThomas Huth {
8221e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
823fcf5ef2aSThomas Huth }
824fcf5ef2aSThomas Huth 
helper_cvttsd2si(CPUX86State * env,ZMMReg * s)825fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
826fcf5ef2aSThomas Huth {
8271e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
828fcf5ef2aSThomas Huth }
829fcf5ef2aSThomas Huth 
830fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
helper_cvttss2sq(CPUX86State * env,ZMMReg * s)831fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
832fcf5ef2aSThomas Huth {
8331e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
834fcf5ef2aSThomas Huth }
835fcf5ef2aSThomas Huth 
helper_cvttsd2sq(CPUX86State * env,ZMMReg * s)836fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
837fcf5ef2aSThomas Huth {
8381e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
839fcf5ef2aSThomas Huth }
840fcf5ef2aSThomas Huth #endif
841fd17264aSPaul Brook #endif
842fcf5ef2aSThomas Huth 
glue(helper_rsqrtps,SUFFIX)843ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
844fcf5ef2aSThomas Huth {
845418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8463403cafeSPaul Brook     int i;
8473403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8483403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one,
8493403cafeSPaul Brook                                   float32_sqrt(s->ZMM_S(i), &env->sse_status),
850fcf5ef2aSThomas Huth                                   &env->sse_status);
8513403cafeSPaul Brook     }
852418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
853fcf5ef2aSThomas Huth }
854fcf5ef2aSThomas Huth 
855fd17264aSPaul Brook #if SHIFT == 1
helper_rsqrtss(CPUX86State * env,ZMMReg * d,ZMMReg * v,ZMMReg * s)856620f7556SPaolo Bonzini void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)
857fcf5ef2aSThomas Huth {
858418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
859620f7556SPaolo Bonzini     int i;
860fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
861fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
862fcf5ef2aSThomas Huth                               &env->sse_status);
863418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
864620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
865620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
866620f7556SPaolo Bonzini     }
867fcf5ef2aSThomas Huth }
868fd17264aSPaul Brook #endif
869fcf5ef2aSThomas Huth 
glue(helper_rcpps,SUFFIX)870ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
871fcf5ef2aSThomas Huth {
872418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8733403cafeSPaul Brook     int i;
8743403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8753403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status);
8763403cafeSPaul Brook     }
877418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
878fcf5ef2aSThomas Huth }
879fcf5ef2aSThomas Huth 
880fd17264aSPaul Brook #if SHIFT == 1
helper_rcpss(CPUX86State * env,ZMMReg * d,ZMMReg * v,ZMMReg * s)881620f7556SPaolo Bonzini void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)
882fcf5ef2aSThomas Huth {
883418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
884620f7556SPaolo Bonzini     int i;
885fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
886620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
887620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
888620f7556SPaolo Bonzini     }
889418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
890fcf5ef2aSThomas Huth }
891fd17264aSPaul Brook #endif
892fcf5ef2aSThomas Huth 
893fd17264aSPaul Brook #if SHIFT == 1
helper_extrq(uint64_t src,int shift,int len)894fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
895fcf5ef2aSThomas Huth {
896fcf5ef2aSThomas Huth     uint64_t mask;
897fcf5ef2aSThomas Huth 
898fcf5ef2aSThomas Huth     if (len == 0) {
899fcf5ef2aSThomas Huth         mask = ~0LL;
900fcf5ef2aSThomas Huth     } else {
901fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
902fcf5ef2aSThomas Huth     }
903fcf5ef2aSThomas Huth     return (src >> shift) & mask;
904fcf5ef2aSThomas Huth }
905fcf5ef2aSThomas Huth 
helper_extrq_r(CPUX86State * env,ZMMReg * d,ZMMReg * s)906fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
907fcf5ef2aSThomas Huth {
908034668c3SPaolo Bonzini     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1) & 63, s->ZMM_B(0) & 63);
909fcf5ef2aSThomas Huth }
910fcf5ef2aSThomas Huth 
helper_extrq_i(CPUX86State * env,ZMMReg * d,int index,int length)911fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
912fcf5ef2aSThomas Huth {
913fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
914fcf5ef2aSThomas Huth }
915fcf5ef2aSThomas Huth 
helper_insertq(uint64_t dest,uint64_t src,int shift,int len)916ca4b1b43SPaolo Bonzini static inline uint64_t helper_insertq(uint64_t dest, uint64_t src, int shift, int len)
917fcf5ef2aSThomas Huth {
918fcf5ef2aSThomas Huth     uint64_t mask;
919fcf5ef2aSThomas Huth 
920fcf5ef2aSThomas Huth     if (len == 0) {
921fcf5ef2aSThomas Huth         mask = ~0ULL;
922fcf5ef2aSThomas Huth     } else {
923fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
924fcf5ef2aSThomas Huth     }
925ca4b1b43SPaolo Bonzini     return (dest & ~(mask << shift)) | ((src & mask) << shift);
926fcf5ef2aSThomas Huth }
927fcf5ef2aSThomas Huth 
helper_insertq_r(CPUX86State * env,ZMMReg * d,ZMMReg * s)928fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
929fcf5ef2aSThomas Huth {
930ca4b1b43SPaolo 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);
931fcf5ef2aSThomas Huth }
932fcf5ef2aSThomas Huth 
helper_insertq_i(CPUX86State * env,ZMMReg * d,ZMMReg * s,int index,int length)933ca4b1b43SPaolo Bonzini void helper_insertq_i(CPUX86State *env, ZMMReg *d, ZMMReg *s, int index, int length)
934fcf5ef2aSThomas Huth {
935ca4b1b43SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), s->ZMM_Q(0), index, length);
936fcf5ef2aSThomas Huth }
937fd17264aSPaul Brook #endif
938fcf5ef2aSThomas Huth 
9396567ffb4SPaul Brook #define SSE_HELPER_HPS(name, F)  \
940f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9416567ffb4SPaul Brook {                                                                 \
9426567ffb4SPaul Brook     float32 r[2 << SHIFT];                                        \
9436567ffb4SPaul Brook     int i, j, k;                                                  \
9446567ffb4SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {            \
9456567ffb4SPaul Brook         for (i = j = 0; j < 4; i++, j += 2) {                     \
9466567ffb4SPaul Brook             r[i + k] = F(v->ZMM_S(j + k), v->ZMM_S(j + k + 1), &env->sse_status); \
9476567ffb4SPaul Brook         }                                                         \
9486567ffb4SPaul Brook         for (j = 0; j < 4; i++, j += 2) {                         \
9496567ffb4SPaul Brook             r[i + k] = F(s->ZMM_S(j + k), s->ZMM_S(j + k + 1), &env->sse_status); \
9506567ffb4SPaul Brook         }                                                         \
9516567ffb4SPaul Brook     }                                                             \
9526567ffb4SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                            \
9536567ffb4SPaul Brook         d->ZMM_S(i) = r[i];                                       \
9546567ffb4SPaul Brook     }                                                             \
955fcf5ef2aSThomas Huth }
956fcf5ef2aSThomas Huth 
SSE_HELPER_HPS(haddps,float32_add)9576567ffb4SPaul Brook SSE_HELPER_HPS(haddps, float32_add)
9586567ffb4SPaul Brook SSE_HELPER_HPS(hsubps, float32_sub)
959fcf5ef2aSThomas Huth 
9606567ffb4SPaul Brook #define SSE_HELPER_HPD(name, F)  \
961f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9626567ffb4SPaul Brook {                                                                 \
9636567ffb4SPaul Brook     float64 r[1 << SHIFT];                                        \
9646567ffb4SPaul Brook     int i, j, k;                                                  \
9656567ffb4SPaul Brook     for (k = 0; k < 1 << SHIFT; k += LANE_WIDTH / 8) {            \
9666567ffb4SPaul Brook         for (i = j = 0; j < 2; i++, j += 2) {                     \
9676567ffb4SPaul Brook             r[i + k] = F(v->ZMM_D(j + k), v->ZMM_D(j + k + 1), &env->sse_status); \
9686567ffb4SPaul Brook         }                                                         \
9696567ffb4SPaul Brook         for (j = 0; j < 2; i++, j += 2) {                         \
9706567ffb4SPaul Brook             r[i + k] = F(s->ZMM_D(j + k), s->ZMM_D(j + k + 1), &env->sse_status); \
9716567ffb4SPaul Brook         }                                                         \
9726567ffb4SPaul Brook     }                                                             \
9736567ffb4SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {                            \
9746567ffb4SPaul Brook         d->ZMM_D(i) = r[i];                                       \
9756567ffb4SPaul Brook     }                                                             \
976fcf5ef2aSThomas Huth }
977fcf5ef2aSThomas Huth 
9786567ffb4SPaul Brook SSE_HELPER_HPD(haddpd, float64_add)
9796567ffb4SPaul Brook SSE_HELPER_HPD(hsubpd, float64_sub)
980fcf5ef2aSThomas Huth 
981f05f9789SPaolo Bonzini void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
982fcf5ef2aSThomas Huth {
9833403cafeSPaul Brook     int i;
9843403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 2) {
9853403cafeSPaul Brook         d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
9863403cafeSPaul Brook         d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
9873403cafeSPaul Brook     }
988fcf5ef2aSThomas Huth }
989fcf5ef2aSThomas Huth 
glue(helper_addsubpd,SUFFIX)990f05f9789SPaolo Bonzini void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
991fcf5ef2aSThomas Huth {
9923403cafeSPaul Brook     int i;
9933403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
9943403cafeSPaul Brook         d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status);
9953403cafeSPaul Brook         d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status);
9963403cafeSPaul Brook     }
997fcf5ef2aSThomas Huth }
998fcf5ef2aSThomas Huth 
999cbf4ad54SPaul Brook #define SSE_HELPER_CMP_P(name, F, C)                                    \
1000cbf4ad54SPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
1001f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
1002fcf5ef2aSThomas Huth     {                                                                   \
1003cbf4ad54SPaul Brook         int i;                                                          \
1004cbf4ad54SPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
1005cbf4ad54SPaul Brook             d->ZMM_L(i) = C(F(32, v->ZMM_S(i), s->ZMM_S(i))) ? -1 : 0;  \
1006cbf4ad54SPaul Brook         }                                                               \
1007fcf5ef2aSThomas Huth     }                                                                   \
1008fcf5ef2aSThomas Huth                                                                         \
1009cbf4ad54SPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
1010f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
1011cbf4ad54SPaul Brook     {                                                                   \
1012cbf4ad54SPaul Brook         int i;                                                          \
1013cbf4ad54SPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
1014cbf4ad54SPaul Brook             d->ZMM_Q(i) = C(F(64, v->ZMM_D(i), s->ZMM_D(i))) ? -1 : 0;  \
1015cbf4ad54SPaul Brook         }                                                               \
1016cbf4ad54SPaul Brook     }
1017cbf4ad54SPaul Brook 
1018cbf4ad54SPaul Brook #if SHIFT == 1
1019cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C)                                          \
1020cbf4ad54SPaul Brook     SSE_HELPER_CMP_P(name, F, C)                                            \
1021f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1022fcf5ef2aSThomas Huth     {                                                                       \
10231de9e7e6SPaolo Bonzini         int i;                                                              \
1024cbf4ad54SPaul Brook         d->ZMM_L(0) = C(F(32, v->ZMM_S(0), s->ZMM_S(0))) ? -1 : 0;          \
10251de9e7e6SPaolo Bonzini         for (i = 1; i < 2 << SHIFT; i++) {                                  \
10261de9e7e6SPaolo Bonzini             d->ZMM_L(i) = v->ZMM_L(i);                                      \
10271de9e7e6SPaolo Bonzini         }                                                                   \
1028fcf5ef2aSThomas Huth     }                                                                       \
1029fcf5ef2aSThomas Huth                                                                             \
1030f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1031fcf5ef2aSThomas Huth     {                                                                       \
10321de9e7e6SPaolo Bonzini         int i;                                                              \
1033cbf4ad54SPaul Brook         d->ZMM_Q(0) = C(F(64, v->ZMM_D(0), s->ZMM_D(0))) ? -1 : 0;          \
10341de9e7e6SPaolo Bonzini         for (i = 1; i < 1 << SHIFT; i++) {                                  \
10351de9e7e6SPaolo Bonzini             d->ZMM_Q(i) = v->ZMM_Q(i);                                      \
10361de9e7e6SPaolo Bonzini         }                                                                   \
1037fcf5ef2aSThomas Huth     }
1038fcf5ef2aSThomas Huth 
FPU_EQU(FloatRelation x)10396e0cac78SPaolo Bonzini static inline bool FPU_EQU(FloatRelation x)
10406e0cac78SPaolo Bonzini {
10416e0cac78SPaolo Bonzini     return (x == float_relation_equal || x == float_relation_unordered);
10426e0cac78SPaolo Bonzini }
FPU_GE(FloatRelation x)10436e0cac78SPaolo Bonzini static inline bool FPU_GE(FloatRelation x)
10446e0cac78SPaolo Bonzini {
10456e0cac78SPaolo Bonzini     return (x == float_relation_equal || x == float_relation_greater);
10466e0cac78SPaolo Bonzini }
1047cbf4ad54SPaul Brook #define FPU_EQ(x) (x == float_relation_equal)
1048cbf4ad54SPaul Brook #define FPU_LT(x) (x == float_relation_less)
1049cbf4ad54SPaul Brook #define FPU_LE(x) (x <= float_relation_equal)
10506e0cac78SPaolo Bonzini #define FPU_GT(x) (x == float_relation_greater)
1051cbf4ad54SPaul Brook #define FPU_UNORD(x) (x == float_relation_unordered)
10526e0cac78SPaolo Bonzini /* We must make sure we evaluate the argument in case it is a signalling NAN */
10536e0cac78SPaolo Bonzini #define FPU_FALSE(x) (x == float_relation_equal && 0)
1054fcf5ef2aSThomas Huth 
1055cbf4ad54SPaul Brook #define FPU_CMPQ(size, a, b) \
1056cbf4ad54SPaul Brook     float ## size ## _compare_quiet(a, b, &env->sse_status)
1057cbf4ad54SPaul Brook #define FPU_CMPS(size, a, b) \
1058cbf4ad54SPaul Brook     float ## size ## _compare(a, b, &env->sse_status)
1059cbf4ad54SPaul Brook 
1060cbf4ad54SPaul Brook #else
1061cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C) SSE_HELPER_CMP_P(name, F, C)
1062cbf4ad54SPaul Brook #endif
1063cbf4ad54SPaul Brook 
1064cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpeq, FPU_CMPQ, FPU_EQ)
1065cbf4ad54SPaul Brook SSE_HELPER_CMP(cmplt, FPU_CMPS, FPU_LT)
1066cbf4ad54SPaul Brook SSE_HELPER_CMP(cmple, FPU_CMPS, FPU_LE)
1067cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpunord, FPU_CMPQ,  FPU_UNORD)
1068cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpneq, FPU_CMPQ, !FPU_EQ)
1069cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnlt, FPU_CMPS, !FPU_LT)
1070cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnle, FPU_CMPS, !FPU_LE)
1071cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpord, FPU_CMPQ, !FPU_UNORD)
1072cbf4ad54SPaul Brook 
10736e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpequ, FPU_CMPQ, FPU_EQU)
10746e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnge, FPU_CMPS, !FPU_GE)
10756e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpngt, FPU_CMPS, !FPU_GT)
10766e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpfalse, FPU_CMPQ,  FPU_FALSE)
10776e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnequ, FPU_CMPQ, !FPU_EQU)
10786e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpge, FPU_CMPS, FPU_GE)
10796e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpgt, FPU_CMPS, FPU_GT)
10806e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmptrue, FPU_CMPQ,  !FPU_FALSE)
10816e0cac78SPaolo Bonzini 
10826e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpeqs, FPU_CMPS, FPU_EQ)
10836e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpltq, FPU_CMPQ, FPU_LT)
10846e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpleq, FPU_CMPQ, FPU_LE)
10856e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpunords, FPU_CMPS,  FPU_UNORD)
10866e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpneqq, FPU_CMPS, !FPU_EQ)
10876e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnltq, FPU_CMPQ, !FPU_LT)
10886e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnleq, FPU_CMPQ, !FPU_LE)
10896e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpords, FPU_CMPS, !FPU_UNORD)
10906e0cac78SPaolo Bonzini 
10916e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpequs, FPU_CMPS, FPU_EQU)
10926e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpngeq, FPU_CMPQ, !FPU_GE)
10936e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpngtq, FPU_CMPQ, !FPU_GT)
10946e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpfalses, FPU_CMPS,  FPU_FALSE)
10956e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpnequs, FPU_CMPS, !FPU_EQU)
10966e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpgeq, FPU_CMPQ, FPU_GE)
10976e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmpgtq, FPU_CMPQ, FPU_GT)
10986e0cac78SPaolo Bonzini SSE_HELPER_CMP(cmptrues, FPU_CMPS,  !FPU_FALSE)
10996e0cac78SPaolo Bonzini 
1100cbf4ad54SPaul Brook #undef SSE_HELPER_CMP
1101fcf5ef2aSThomas Huth 
1102fd17264aSPaul Brook #if SHIFT == 1
1103fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1104fcf5ef2aSThomas Huth 
helper_ucomiss(CPUX86State * env,Reg * d,Reg * s)1105fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1106fcf5ef2aSThomas Huth {
110771bfd65cSRichard Henderson     FloatRelation ret;
1108fcf5ef2aSThomas Huth     float32 s0, s1;
1109fcf5ef2aSThomas Huth 
1110fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1111fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1112fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1113fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1114*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
1115fcf5ef2aSThomas Huth }
1116fcf5ef2aSThomas Huth 
helper_comiss(CPUX86State * env,Reg * d,Reg * s)1117fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1118fcf5ef2aSThomas Huth {
111971bfd65cSRichard Henderson     FloatRelation ret;
1120fcf5ef2aSThomas Huth     float32 s0, s1;
1121fcf5ef2aSThomas Huth 
1122fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1123fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1124fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1125fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1126*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
1127fcf5ef2aSThomas Huth }
1128fcf5ef2aSThomas Huth 
helper_ucomisd(CPUX86State * env,Reg * d,Reg * s)1129fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1130fcf5ef2aSThomas Huth {
113171bfd65cSRichard Henderson     FloatRelation ret;
1132fcf5ef2aSThomas Huth     float64 d0, d1;
1133fcf5ef2aSThomas Huth 
1134fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1135fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1136fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1137fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1138*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
1139fcf5ef2aSThomas Huth }
1140fcf5ef2aSThomas Huth 
helper_comisd(CPUX86State * env,Reg * d,Reg * s)1141fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1142fcf5ef2aSThomas Huth {
114371bfd65cSRichard Henderson     FloatRelation ret;
1144fcf5ef2aSThomas Huth     float64 d0, d1;
1145fcf5ef2aSThomas Huth 
1146fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1147fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1148fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1149fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1150*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
1151fcf5ef2aSThomas Huth }
1152fd17264aSPaul Brook #endif
1153fcf5ef2aSThomas Huth 
glue(helper_movmskps,SUFFIX)1154ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1155fcf5ef2aSThomas Huth {
1156fd17264aSPaul Brook     uint32_t mask;
1157fd17264aSPaul Brook     int i;
1158fcf5ef2aSThomas Huth 
1159fd17264aSPaul Brook     mask = 0;
1160fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1161fd17264aSPaul Brook         mask |= (s->ZMM_L(i) >> (31 - i)) & (1 << i);
1162fd17264aSPaul Brook     }
1163fd17264aSPaul Brook     return mask;
1164fcf5ef2aSThomas Huth }
1165fcf5ef2aSThomas Huth 
glue(helper_movmskpd,SUFFIX)1166ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1167fcf5ef2aSThomas Huth {
1168fd17264aSPaul Brook     uint32_t mask;
1169fd17264aSPaul Brook     int i;
1170fcf5ef2aSThomas Huth 
1171fd17264aSPaul Brook     mask = 0;
1172fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1173fd17264aSPaul Brook         mask |= (s->ZMM_Q(i) >> (63 - i)) & (1 << i);
1174fd17264aSPaul Brook     }
1175fd17264aSPaul Brook     return mask;
1176fcf5ef2aSThomas Huth }
1177fcf5ef2aSThomas Huth 
1178fcf5ef2aSThomas Huth #endif
1179fcf5ef2aSThomas Huth 
1180d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1181d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1182f05f9789SPaolo Bonzini         Reg *d, Reg *v, Reg *s)                               \
1183d45b0de6SPaul Brook {                                                             \
1184d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1185d45b0de6SPaul Brook     int j, k;                                                 \
1186d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1187d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1188d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1189d45b0de6SPaul Brook         }                                                     \
1190d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1191d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1192d45b0de6SPaul Brook         }                                                     \
1193d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1194d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1195d45b0de6SPaul Brook         }                                                     \
1196d45b0de6SPaul Brook     }                                                         \
1197fcf5ef2aSThomas Huth }
1198fcf5ef2aSThomas Huth 
PACK_HELPER_B(sswb,satsb)1199d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1200d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1201fcf5ef2aSThomas Huth 
1202f05f9789SPaolo Bonzini void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1203fcf5ef2aSThomas Huth {
1204d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1205d45b0de6SPaul Brook     int j, k;
1206fcf5ef2aSThomas Huth 
1207d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1208d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1209d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1210d45b0de6SPaul Brook         }
1211d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1212d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1213d45b0de6SPaul Brook         }
1214d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1215d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1216d45b0de6SPaul Brook         }
1217d45b0de6SPaul Brook     }
1218fcf5ef2aSThomas Huth }
1219fcf5ef2aSThomas Huth 
1220fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1221fcf5ef2aSThomas Huth                                                                         \
1222fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1223f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1224fcf5ef2aSThomas Huth     {                                                                   \
1225d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1226d45b0de6SPaul Brook         int j, i;                                                       \
1227fcf5ef2aSThomas Huth                                                                         \
1228d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1229d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1230d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1231d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1232d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1233d45b0de6SPaul Brook             }                                                           \
1234d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1235d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1236d45b0de6SPaul Brook             }                                                           \
1237d45b0de6SPaul Brook         }                                                               \
1238fcf5ef2aSThomas Huth     }                                                                   \
1239fcf5ef2aSThomas Huth                                                                         \
1240fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1241f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1242fcf5ef2aSThomas Huth     {                                                                   \
1243d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1244d45b0de6SPaul Brook         int j, i;                                                       \
1245fcf5ef2aSThomas Huth                                                                         \
1246d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1247d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1248d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1249d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1250d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1251d45b0de6SPaul Brook             }                                                           \
1252d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1253d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1254d45b0de6SPaul Brook             }                                                           \
1255d45b0de6SPaul Brook         }                                                               \
1256fcf5ef2aSThomas Huth     }                                                                   \
1257fcf5ef2aSThomas Huth                                                                         \
1258fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1259f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1260fcf5ef2aSThomas Huth     {                                                                   \
1261d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1262d45b0de6SPaul Brook         int j, i;                                                       \
1263fcf5ef2aSThomas Huth                                                                         \
1264d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1265d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1266d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1267d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1268d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1269d45b0de6SPaul Brook             }                                                           \
1270d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1271d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1272d45b0de6SPaul Brook             }                                                           \
1273d45b0de6SPaul Brook         }                                                               \
1274fcf5ef2aSThomas Huth     }                                                                   \
1275fcf5ef2aSThomas Huth                                                                         \
1276fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1277d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1278f05f9789SPaolo Bonzini                         CPUX86State *env, Reg *d, Reg *v, Reg *s)       \
1279fcf5ef2aSThomas Huth              {                                                          \
1280d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1281d45b0de6SPaul Brook                  int i;                                                 \
1282fcf5ef2aSThomas Huth                                                                         \
1283d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1284d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1285d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1286d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1287d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1288d45b0de6SPaul Brook                  }                                                      \
1289fcf5ef2aSThomas Huth              }                                                          \
1290fcf5ef2aSThomas Huth                                                                         )
1291fcf5ef2aSThomas Huth 
1292fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1293fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1294fcf5ef2aSThomas Huth 
1295d45b0de6SPaul Brook #undef PACK_WIDTH
1296d45b0de6SPaul Brook #undef PACK_HELPER_B
1297d45b0de6SPaul Brook #undef UNPCK_OP
1298d45b0de6SPaul Brook 
1299d45b0de6SPaul Brook 
1300fcf5ef2aSThomas Huth /* 3DNow! float ops */
1301fcf5ef2aSThomas Huth #if SHIFT == 0
helper_pi2fd(CPUX86State * env,MMXReg * d,MMXReg * s)1302fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1303fcf5ef2aSThomas Huth {
1304fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1305fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1306fcf5ef2aSThomas Huth }
1307fcf5ef2aSThomas Huth 
helper_pi2fw(CPUX86State * env,MMXReg * d,MMXReg * s)1308fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1309fcf5ef2aSThomas Huth {
1310fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1311fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1312fcf5ef2aSThomas Huth }
1313fcf5ef2aSThomas Huth 
helper_pf2id(CPUX86State * env,MMXReg * d,MMXReg * s)1314fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1315fcf5ef2aSThomas Huth {
1316fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1317fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1318fcf5ef2aSThomas Huth }
1319fcf5ef2aSThomas Huth 
helper_pf2iw(CPUX86State * env,MMXReg * d,MMXReg * s)1320fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1321fcf5ef2aSThomas Huth {
1322fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1323fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1324fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1325fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1326fcf5ef2aSThomas Huth }
1327fcf5ef2aSThomas Huth 
helper_pfacc(CPUX86State * env,MMXReg * d,MMXReg * s)1328fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1329fcf5ef2aSThomas Huth {
133025bdec79SPaolo Bonzini     float32 r;
1331fcf5ef2aSThomas Huth 
133225bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
133325bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
133425bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1335fcf5ef2aSThomas Huth }
1336fcf5ef2aSThomas Huth 
helper_pfadd(CPUX86State * env,MMXReg * d,MMXReg * s)1337fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1338fcf5ef2aSThomas Huth {
1339fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1340fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1341fcf5ef2aSThomas Huth }
1342fcf5ef2aSThomas Huth 
helper_pfcmpeq(CPUX86State * env,MMXReg * d,MMXReg * s)1343fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1344fcf5ef2aSThomas Huth {
1345fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1346fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1347fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1348fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1349fcf5ef2aSThomas Huth }
1350fcf5ef2aSThomas Huth 
helper_pfcmpge(CPUX86State * env,MMXReg * d,MMXReg * s)1351fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1352fcf5ef2aSThomas Huth {
1353fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1354fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1355fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1356fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1357fcf5ef2aSThomas Huth }
1358fcf5ef2aSThomas Huth 
helper_pfcmpgt(CPUX86State * env,MMXReg * d,MMXReg * s)1359fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1360fcf5ef2aSThomas Huth {
1361fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1362fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1363fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1364fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1365fcf5ef2aSThomas Huth }
1366fcf5ef2aSThomas Huth 
helper_pfmax(CPUX86State * env,MMXReg * d,MMXReg * s)1367fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1368fcf5ef2aSThomas Huth {
1369fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1370fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1371fcf5ef2aSThomas Huth     }
1372fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1373fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1374fcf5ef2aSThomas Huth     }
1375fcf5ef2aSThomas Huth }
1376fcf5ef2aSThomas Huth 
helper_pfmin(CPUX86State * env,MMXReg * d,MMXReg * s)1377fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1378fcf5ef2aSThomas Huth {
1379fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1380fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1381fcf5ef2aSThomas Huth     }
1382fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1383fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1384fcf5ef2aSThomas Huth     }
1385fcf5ef2aSThomas Huth }
1386fcf5ef2aSThomas Huth 
helper_pfmul(CPUX86State * env,MMXReg * d,MMXReg * s)1387fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1388fcf5ef2aSThomas Huth {
1389fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1390fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1391fcf5ef2aSThomas Huth }
1392fcf5ef2aSThomas Huth 
helper_pfnacc(CPUX86State * env,MMXReg * d,MMXReg * s)1393fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1394fcf5ef2aSThomas Huth {
139525bdec79SPaolo Bonzini     float32 r;
1396fcf5ef2aSThomas Huth 
139725bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
139825bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
139925bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1400fcf5ef2aSThomas Huth }
1401fcf5ef2aSThomas Huth 
helper_pfpnacc(CPUX86State * env,MMXReg * d,MMXReg * s)1402fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1403fcf5ef2aSThomas Huth {
140425bdec79SPaolo Bonzini     float32 r;
1405fcf5ef2aSThomas Huth 
140625bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
140725bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
140825bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
helper_pfrcp(CPUX86State * env,MMXReg * d,MMXReg * s)1411fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1412fcf5ef2aSThomas Huth {
1413fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1414fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1415fcf5ef2aSThomas Huth }
1416fcf5ef2aSThomas Huth 
helper_pfrsqrt(CPUX86State * env,MMXReg * d,MMXReg * s)1417fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1418fcf5ef2aSThomas Huth {
1419fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1420fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1421fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1422fcf5ef2aSThomas Huth                               &env->mmx_status);
1423fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1424fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1425fcf5ef2aSThomas Huth }
1426fcf5ef2aSThomas Huth 
helper_pfsub(CPUX86State * env,MMXReg * d,MMXReg * s)1427fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1428fcf5ef2aSThomas Huth {
1429fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1430fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1431fcf5ef2aSThomas Huth }
1432fcf5ef2aSThomas Huth 
helper_pfsubr(CPUX86State * env,MMXReg * d,MMXReg * s)1433fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1434fcf5ef2aSThomas Huth {
1435fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1436fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1437fcf5ef2aSThomas Huth }
1438fcf5ef2aSThomas Huth 
helper_pswapd(CPUX86State * env,MMXReg * d,MMXReg * s)1439fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1440fcf5ef2aSThomas Huth {
144125bdec79SPaolo Bonzini     uint32_t r;
1442fcf5ef2aSThomas Huth 
144325bdec79SPaolo Bonzini     r = s->MMX_L(0);
144425bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
144525bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1446fcf5ef2aSThomas Huth }
1447fcf5ef2aSThomas Huth #endif
1448fcf5ef2aSThomas Huth 
1449fcf5ef2aSThomas Huth /* SSSE3 op helpers */
glue(helper_pshufb,SUFFIX)1450f05f9789SPaolo Bonzini void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1451fcf5ef2aSThomas Huth {
1452fcf5ef2aSThomas Huth     int i;
1453d45b0de6SPaul Brook #if SHIFT == 0
1454d45b0de6SPaul Brook     uint8_t r[8];
1455fcf5ef2aSThomas Huth 
1456d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1457d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1458fcf5ef2aSThomas Huth     }
1459d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1460d45b0de6SPaul Brook         d->B(i) = r[i];
1461fcf5ef2aSThomas Huth     }
1462d45b0de6SPaul Brook #else
1463d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1464fcf5ef2aSThomas Huth 
1465d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1466d45b0de6SPaul Brook         int j = i & ~0xf;
1467d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1468fcf5ef2aSThomas Huth     }
1469d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1470d45b0de6SPaul Brook         d->B(i) = r[i];
1471fcf5ef2aSThomas Huth     }
1472fcf5ef2aSThomas Huth #endif
1473fcf5ef2aSThomas Huth }
1474fcf5ef2aSThomas Huth 
1475d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1476f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1477d45b0de6SPaul Brook {                                                          \
1478d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1479d45b0de6SPaul Brook     int i, j, k;                                           \
1480d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1481d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1482d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1483d45b0de6SPaul Brook         }                                                  \
1484d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1485d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1486d45b0de6SPaul Brook         }                                                  \
1487d45b0de6SPaul Brook     }                                                      \
1488d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1489d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1490d45b0de6SPaul Brook     }                                                      \
1491fcf5ef2aSThomas Huth }
1492fcf5ef2aSThomas Huth 
1493d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1494f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1495d45b0de6SPaul Brook {                                                          \
1496d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1497d45b0de6SPaul Brook     int i, j, k;                                           \
1498d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1499d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1500d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1501d45b0de6SPaul Brook         }                                                  \
1502d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1503d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1504d45b0de6SPaul Brook         }                                                  \
1505d45b0de6SPaul Brook     }                                                      \
1506d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1507d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1508d45b0de6SPaul Brook     }                                                      \
1509fcf5ef2aSThomas Huth }
1510fcf5ef2aSThomas Huth 
SSE_HELPER_HW(phaddw,FADD)1511d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1512d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1513d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1514d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1515d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1516d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
151775046ad7SPaolo Bonzini 
1518d45b0de6SPaul Brook #undef SSE_HELPER_HW
1519d45b0de6SPaul Brook #undef SSE_HELPER_HL
1520d45b0de6SPaul Brook 
1521f05f9789SPaolo Bonzini void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1522d45b0de6SPaul Brook {
1523d45b0de6SPaul Brook     int i;
1524d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1525d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1526d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1527d45b0de6SPaul Brook     }
1528fcf5ef2aSThomas Huth }
1529fcf5ef2aSThomas Huth 
1530fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
SSE_HELPER_W(helper_pmulhrsw,FMULHRSW)1531fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1532fcf5ef2aSThomas Huth 
1533fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1534fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1535fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1536fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1537fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1538fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1539fcf5ef2aSThomas Huth 
1540f05f9789SPaolo Bonzini void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1541a64eee3aSPaolo Bonzini                                   uint32_t imm)
1542fcf5ef2aSThomas Huth {
1543d45b0de6SPaul Brook     int i;
1544fcf5ef2aSThomas Huth 
1545fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1546a64eee3aSPaolo Bonzini     if (imm >= (SHIFT ? 32 : 16)) {
1547d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1548d45b0de6SPaul Brook             d->Q(i) = 0;
1549d45b0de6SPaul Brook         }
1550fcf5ef2aSThomas Huth     } else {
1551a64eee3aSPaolo Bonzini         int shift = imm * 8;
1552fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1553fcf5ef2aSThomas Huth #if SHIFT == 0
1554d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1555d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1556fcf5ef2aSThomas Huth #else
1557d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1558d45b0de6SPaul Brook             uint64_t r0, r1;
1559d45b0de6SPaul Brook 
1560d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1561d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1562d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1563d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1564d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1565d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1566d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1567d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1568d45b0de6SPaul Brook             d->Q(i) = r0;
1569d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1570d45b0de6SPaul Brook         }
1571fcf5ef2aSThomas Huth #endif
1572fcf5ef2aSThomas Huth #undef SHR
1573fcf5ef2aSThomas Huth     }
1574fcf5ef2aSThomas Huth }
1575fcf5ef2aSThomas Huth 
15760e29cea5SPaul Brook #if SHIFT >= 1
1577fcf5ef2aSThomas Huth 
1578fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1579f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
1580f05f9789SPaolo Bonzini                             Reg *m)                                     \
1581fcf5ef2aSThomas Huth     {                                                                   \
15820e29cea5SPaul Brook         int i;                                                          \
15830e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
15840e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), m->elem(i));         \
1585fcf5ef2aSThomas Huth         }                                                               \
1586fcf5ef2aSThomas Huth     }
1587fcf5ef2aSThomas Huth 
1588fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1589f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
15900e29cea5SPaul Brook                             uint32_t imm)                               \
1591fcf5ef2aSThomas Huth     {                                                                   \
15920e29cea5SPaul Brook         int i;                                                          \
15930e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
15940e29cea5SPaul Brook             int j = i & 7;                                              \
15950e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), (imm >> j) & 1);     \
1596fcf5ef2aSThomas Huth         }                                                               \
1597fcf5ef2aSThomas Huth     }
1598fcf5ef2aSThomas Huth 
1599fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
16000e29cea5SPaul Brook #define FBLENDVB(v, s, m) ((m & 0x80) ? s : v)
16010e29cea5SPaul Brook #define FBLENDVPS(v, s, m) ((m & 0x80000000) ? s : v)
16020e29cea5SPaul Brook #define FBLENDVPD(v, s, m) ((m & 0x8000000000000000LL) ? s : v)
16030e29cea5SPaul Brook SSE_HELPER_V(helper_pblendvb, B, 8 << SHIFT, FBLENDVB)
16040e29cea5SPaul Brook SSE_HELPER_V(helper_blendvps, L, 2 << SHIFT, FBLENDVPS)
16050e29cea5SPaul Brook SSE_HELPER_V(helper_blendvpd, Q, 1 << SHIFT, FBLENDVPD)
1606fcf5ef2aSThomas Huth 
glue(helper_ptest,SUFFIX)1607fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1608fcf5ef2aSThomas Huth {
1609e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1610e894bae8SPaul Brook     int i;
1611fcf5ef2aSThomas Huth 
1612e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1613e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1614e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1615e894bae8SPaul Brook     }
1616fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1617*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
1618fcf5ef2aSThomas Huth }
1619fcf5ef2aSThomas Huth 
16207170a17eSPaolo Bonzini #define FMOVSLDUP(i) s->L((i) & ~1)
16217170a17eSPaolo Bonzini #define FMOVSHDUP(i) s->L((i) | 1)
16227170a17eSPaolo Bonzini #define FMOVDLDUP(i) s->Q((i) & ~1)
16237170a17eSPaolo Bonzini 
1624fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1625fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1626fcf5ef2aSThomas Huth     {                                                           \
1627e894bae8SPaul Brook         int n = num;                                            \
1628e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1629e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1630fcf5ef2aSThomas Huth         }                                                       \
1631fcf5ef2aSThomas Huth     }
1632fcf5ef2aSThomas Huth 
1633e894bae8SPaul Brook #if SHIFT > 0
1634e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1635e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1636e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1637e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1638e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1639e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1640e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1641e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1642e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1643e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1644e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1645e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
16467170a17eSPaolo Bonzini SSE_HELPER_F(helper_pmovsldup, L, 2 << SHIFT, FMOVSLDUP)
16477170a17eSPaolo Bonzini SSE_HELPER_F(helper_pmovshdup, L, 2 << SHIFT, FMOVSHDUP)
16487170a17eSPaolo Bonzini SSE_HELPER_F(helper_pmovdldup, Q, 1 << SHIFT, FMOVDLDUP)
1649e894bae8SPaul Brook #endif
1650fcf5ef2aSThomas Huth 
glue(helper_pmuldq,SUFFIX)1651f05f9789SPaolo Bonzini void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1652fcf5ef2aSThomas Huth {
1653e894bae8SPaul Brook     int i;
1654e894bae8SPaul Brook 
1655e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1656e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1657e894bae8SPaul Brook     }
1658fcf5ef2aSThomas Huth }
1659fcf5ef2aSThomas Huth 
glue(helper_packusdw,SUFFIX)1660f05f9789SPaolo Bonzini void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1661fcf5ef2aSThomas Huth {
1662d45b0de6SPaul Brook     uint16_t r[8];
1663d45b0de6SPaul Brook     int i, j, k;
166480e19606SJoseph Myers 
1665d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1666d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1667d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1668d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1669d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1670d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1671d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1672d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1673d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1674d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1675d45b0de6SPaul Brook             d->W(i + k) = r[k];
1676d45b0de6SPaul Brook         }
1677d45b0de6SPaul Brook     }
1678fcf5ef2aSThomas Huth }
1679fcf5ef2aSThomas Huth 
1680fd17264aSPaul Brook #if SHIFT == 1
glue(helper_phminposuw,SUFFIX)1681fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1682fcf5ef2aSThomas Huth {
1683fcf5ef2aSThomas Huth     int idx = 0;
1684fcf5ef2aSThomas Huth 
1685fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1686fcf5ef2aSThomas Huth         idx = 1;
1687fcf5ef2aSThomas Huth     }
1688fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1689fcf5ef2aSThomas Huth         idx = 2;
1690fcf5ef2aSThomas Huth     }
1691fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1692fcf5ef2aSThomas Huth         idx = 3;
1693fcf5ef2aSThomas Huth     }
1694fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1695fcf5ef2aSThomas Huth         idx = 4;
1696fcf5ef2aSThomas Huth     }
1697fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1698fcf5ef2aSThomas Huth         idx = 5;
1699fcf5ef2aSThomas Huth     }
1700fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1701fcf5ef2aSThomas Huth         idx = 6;
1702fcf5ef2aSThomas Huth     }
1703fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1704fcf5ef2aSThomas Huth         idx = 7;
1705fcf5ef2aSThomas Huth     }
1706fcf5ef2aSThomas Huth 
1707fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1708aa406feaSJoseph Myers     d->W(1) = idx;
1709aa406feaSJoseph Myers     d->L(1) = 0;
1710aa406feaSJoseph Myers     d->Q(1) = 0;
1711fcf5ef2aSThomas Huth }
1712fd17264aSPaul Brook #endif
1713fcf5ef2aSThomas Huth 
glue(helper_roundps,SUFFIX)1714fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1715fcf5ef2aSThomas Huth                                   uint32_t mode)
1716fcf5ef2aSThomas Huth {
1717418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1718fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1719fd17264aSPaul Brook     int i;
1720fcf5ef2aSThomas Huth 
1721fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1722fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1723314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1724fcf5ef2aSThomas Huth     }
1725fcf5ef2aSThomas Huth 
1726fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1727fd17264aSPaul Brook         d->ZMM_S(i) = float32_round_to_int(s->ZMM_S(i), &env->sse_status);
1728fd17264aSPaul Brook     }
1729fcf5ef2aSThomas Huth 
1730418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1731fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1732fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1733fcf5ef2aSThomas Huth                                   &env->sse_status);
1734fcf5ef2aSThomas Huth     }
1735fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1736fcf5ef2aSThomas Huth }
1737fcf5ef2aSThomas Huth 
glue(helper_roundpd,SUFFIX)1738fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1739fcf5ef2aSThomas Huth                                   uint32_t mode)
1740fcf5ef2aSThomas Huth {
1741418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1742fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1743fd17264aSPaul Brook     int i;
1744fcf5ef2aSThomas Huth 
1745fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1746fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1747314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1748fcf5ef2aSThomas Huth     }
1749fcf5ef2aSThomas Huth 
1750fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1751fd17264aSPaul Brook         d->ZMM_D(i) = float64_round_to_int(s->ZMM_D(i), &env->sse_status);
1752fd17264aSPaul Brook     }
1753fcf5ef2aSThomas Huth 
1754418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1755fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1756fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1757fcf5ef2aSThomas Huth                                   &env->sse_status);
1758fcf5ef2aSThomas Huth     }
1759fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1760fcf5ef2aSThomas Huth }
1761fcf5ef2aSThomas Huth 
1762fd17264aSPaul Brook #if SHIFT == 1
glue(helper_roundss,SUFFIX)1763620f7556SPaolo Bonzini void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1764fcf5ef2aSThomas Huth                                   uint32_t mode)
1765fcf5ef2aSThomas Huth {
1766418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1767fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1768620f7556SPaolo Bonzini     int i;
1769fcf5ef2aSThomas Huth 
1770fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1771fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1772314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1773fcf5ef2aSThomas Huth     }
1774fcf5ef2aSThomas Huth 
1775fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1776620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
1777620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
1778620f7556SPaolo Bonzini     }
1779fcf5ef2aSThomas Huth 
1780418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1781fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1782fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1783fcf5ef2aSThomas Huth                                   &env->sse_status);
1784fcf5ef2aSThomas Huth     }
1785fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1786fcf5ef2aSThomas Huth }
1787fcf5ef2aSThomas Huth 
glue(helper_roundsd,SUFFIX)1788620f7556SPaolo Bonzini void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1789fcf5ef2aSThomas Huth                                   uint32_t mode)
1790fcf5ef2aSThomas Huth {
1791418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1792fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1793620f7556SPaolo Bonzini     int i;
1794fcf5ef2aSThomas Huth 
1795fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1796fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1797314d3effSPaolo Bonzini         set_x86_rounding_mode(mode & 3, &env->sse_status);
1798fcf5ef2aSThomas Huth     }
1799fcf5ef2aSThomas Huth 
1800fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1801620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
1802620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
1803620f7556SPaolo Bonzini     }
1804fcf5ef2aSThomas Huth 
1805418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1806fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1807fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1808fcf5ef2aSThomas Huth                                   &env->sse_status);
1809fcf5ef2aSThomas Huth     }
1810fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1811fcf5ef2aSThomas Huth }
1812fd17264aSPaul Brook #endif
1813fcf5ef2aSThomas Huth 
18140e29cea5SPaul Brook #define FBLENDP(v, s, m) (m ? s : v)
18150e29cea5SPaul Brook SSE_HELPER_I(helper_blendps, L, 2 << SHIFT, FBLENDP)
18160e29cea5SPaul Brook SSE_HELPER_I(helper_blendpd, Q, 1 << SHIFT, FBLENDP)
18170e29cea5SPaul Brook SSE_HELPER_I(helper_pblendw, W, 4 << SHIFT, FBLENDP)
1818fcf5ef2aSThomas Huth 
glue(helper_dpps,SUFFIX)1819f05f9789SPaolo Bonzini void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
18206f218d6eSPaul Brook                                uint32_t mask)
1821fcf5ef2aSThomas Huth {
1822bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
18236f218d6eSPaul Brook     int i;
1824fcf5ef2aSThomas Huth 
18256f218d6eSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
1826bf30ad8cSPaolo Bonzini         /*
1827bf30ad8cSPaolo Bonzini          * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1828bf30ad8cSPaolo Bonzini          * to correctly round the intermediate results
1829bf30ad8cSPaolo Bonzini          */
1830fcf5ef2aSThomas Huth         if (mask & (1 << 4)) {
18316f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
1832bf30ad8cSPaolo Bonzini         } else {
1833bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1834fcf5ef2aSThomas Huth         }
1835fcf5ef2aSThomas Huth         if (mask & (1 << 5)) {
18366f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
1837bf30ad8cSPaolo Bonzini         } else {
1838bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1839fcf5ef2aSThomas Huth         }
1840bf30ad8cSPaolo Bonzini         temp2 = float32_add(prod1, prod2, &env->sse_status);
1841fcf5ef2aSThomas Huth         if (mask & (1 << 6)) {
18426f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i+2), s->ZMM_S(i+2), &env->sse_status);
1843bf30ad8cSPaolo Bonzini         } else {
1844bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1845fcf5ef2aSThomas Huth         }
1846fcf5ef2aSThomas Huth         if (mask & (1 << 7)) {
18476f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+3), s->ZMM_S(i+3), &env->sse_status);
1848bf30ad8cSPaolo Bonzini         } else {
1849bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1850fcf5ef2aSThomas Huth         }
1851bf30ad8cSPaolo Bonzini         temp3 = float32_add(prod1, prod2, &env->sse_status);
1852bf30ad8cSPaolo Bonzini         temp4 = float32_add(temp2, temp3, &env->sse_status);
1853bf30ad8cSPaolo Bonzini 
18546f218d6eSPaul Brook         d->ZMM_S(i) = (mask & (1 << 0)) ? temp4 : float32_zero;
18556f218d6eSPaul Brook         d->ZMM_S(i+1) = (mask & (1 << 1)) ? temp4 : float32_zero;
18566f218d6eSPaul Brook         d->ZMM_S(i+2) = (mask & (1 << 2)) ? temp4 : float32_zero;
18576f218d6eSPaul Brook         d->ZMM_S(i+3) = (mask & (1 << 3)) ? temp4 : float32_zero;
18586f218d6eSPaul Brook     }
1859fcf5ef2aSThomas Huth }
1860fcf5ef2aSThomas Huth 
18616f218d6eSPaul Brook #if SHIFT == 1
18626f218d6eSPaul Brook /* Oddly, there is no ymm version of dppd */
glue(helper_dppd,SUFFIX)18636f218d6eSPaul Brook void glue(helper_dppd, SUFFIX)(CPUX86State *env,
1864f05f9789SPaolo Bonzini                                Reg *d, Reg *v, Reg *s, uint32_t mask)
1865fcf5ef2aSThomas Huth {
1866bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1867fcf5ef2aSThomas Huth 
1868fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
18696f218d6eSPaul Brook         prod1 = float64_mul(v->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1870bf30ad8cSPaolo Bonzini     } else {
1871bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1872fcf5ef2aSThomas Huth     }
1873fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
18746f218d6eSPaul Brook         prod2 = float64_mul(v->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1875bf30ad8cSPaolo Bonzini     } else {
1876bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1877fcf5ef2aSThomas Huth     }
1878bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1879bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1880bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1881fcf5ef2aSThomas Huth }
18826f218d6eSPaul Brook #endif
1883fcf5ef2aSThomas Huth 
glue(helper_mpsadbw,SUFFIX)1884f05f9789SPaolo Bonzini void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1885fcf5ef2aSThomas Huth                                   uint32_t offset)
1886fcf5ef2aSThomas Huth {
1887d45b0de6SPaul Brook     int i, j;
1888d45b0de6SPaul Brook     uint16_t r[8];
1889fcf5ef2aSThomas Huth 
1890d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; ) {
1891d45b0de6SPaul Brook         int s0 = (j * 2) + ((offset & 3) << 2);
1892d45b0de6SPaul Brook         int d0 = (j * 2) + ((offset & 4) << 0);
1893d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, d0++) {
1894d45b0de6SPaul Brook             r[i] = 0;
1895d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0));
1896d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1));
1897d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2));
1898d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3));
1899fcf5ef2aSThomas Huth         }
1900d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, j++) {
1901d45b0de6SPaul Brook             d->W(j) = r[i];
1902d45b0de6SPaul Brook         }
1903d45b0de6SPaul Brook         offset >>= 3;
1904d45b0de6SPaul Brook     }
1905fcf5ef2aSThomas Huth }
1906fcf5ef2aSThomas Huth 
1907fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1908fd17264aSPaul Brook #if SHIFT == 1
pcmp_elen(CPUX86State * env,int reg,uint32_t ctrl)1909fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1910fcf5ef2aSThomas Huth {
1911d1da229fSPaul Brook     target_long val, limit;
1912fcf5ef2aSThomas Huth 
1913fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1914fcf5ef2aSThomas Huth     if (ctrl >> 8) {
1915d1da229fSPaul Brook         val = (target_long)env->regs[reg];
1916fcf5ef2aSThomas Huth     } else {
1917d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
1918fcf5ef2aSThomas Huth     }
1919fcf5ef2aSThomas Huth     if (ctrl & 1) {
1920d1da229fSPaul Brook         limit = 8;
1921fcf5ef2aSThomas Huth     } else {
1922d1da229fSPaul Brook         limit = 16;
1923fcf5ef2aSThomas Huth     }
1924d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
1925d1da229fSPaul Brook         return limit;
1926fcf5ef2aSThomas Huth     }
1927d1da229fSPaul Brook     return abs1(val);
1928fcf5ef2aSThomas Huth }
1929fcf5ef2aSThomas Huth 
pcmp_ilen(Reg * r,uint8_t ctrl)1930fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
1931fcf5ef2aSThomas Huth {
1932fcf5ef2aSThomas Huth     int val = 0;
1933fcf5ef2aSThomas Huth 
1934fcf5ef2aSThomas Huth     if (ctrl & 1) {
1935fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
1936fcf5ef2aSThomas Huth             val++;
1937fcf5ef2aSThomas Huth         }
1938fcf5ef2aSThomas Huth     } else {
1939fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
1940fcf5ef2aSThomas Huth             val++;
1941fcf5ef2aSThomas Huth         }
1942fcf5ef2aSThomas Huth     }
1943fcf5ef2aSThomas Huth 
1944fcf5ef2aSThomas Huth     return val;
1945fcf5ef2aSThomas Huth }
1946fcf5ef2aSThomas Huth 
pcmp_val(Reg * r,uint8_t ctrl,int i)1947fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
1948fcf5ef2aSThomas Huth {
1949fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
1950fcf5ef2aSThomas Huth     case 0:
1951fcf5ef2aSThomas Huth         return r->B(i);
1952fcf5ef2aSThomas Huth     case 1:
1953fcf5ef2aSThomas Huth         return r->W(i);
1954fcf5ef2aSThomas Huth     case 2:
1955fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
1956fcf5ef2aSThomas Huth     case 3:
1957fcf5ef2aSThomas Huth     default:
1958fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
1959fcf5ef2aSThomas Huth     }
1960fcf5ef2aSThomas Huth }
1961fcf5ef2aSThomas Huth 
pcmpxstrx(CPUX86State * env,Reg * d,Reg * s,uint8_t ctrl,int valids,int validd)1962fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
1963a64eee3aSPaolo Bonzini                                  uint8_t ctrl, int valids, int validd)
1964fcf5ef2aSThomas Huth {
1965fcf5ef2aSThomas Huth     unsigned int res = 0;
1966fcf5ef2aSThomas Huth     int v;
1967fcf5ef2aSThomas Huth     int j, i;
1968fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
1969fcf5ef2aSThomas Huth 
1970fcf5ef2aSThomas Huth     valids--;
1971fcf5ef2aSThomas Huth     validd--;
1972fcf5ef2aSThomas Huth 
1973fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
1974*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
1975fcf5ef2aSThomas Huth 
1976fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
1977fcf5ef2aSThomas Huth     case 0:
1978fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
1979fcf5ef2aSThomas Huth             res <<= 1;
1980fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
1981fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
1982fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
1983fcf5ef2aSThomas Huth             }
1984fcf5ef2aSThomas Huth         }
1985fcf5ef2aSThomas Huth         break;
1986fcf5ef2aSThomas Huth     case 1:
1987fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
1988fcf5ef2aSThomas Huth             res <<= 1;
1989fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
1990fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
1991fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
1992fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
1993fcf5ef2aSThomas Huth             }
1994fcf5ef2aSThomas Huth         }
1995fcf5ef2aSThomas Huth         break;
1996fcf5ef2aSThomas Huth     case 2:
1997fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
1998fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
1999fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2000fcf5ef2aSThomas Huth             res <<= 1;
2001fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2002fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2003fcf5ef2aSThomas Huth         }
2004fcf5ef2aSThomas Huth         break;
2005fcf5ef2aSThomas Huth     case 3:
2006ae35eea7SJoseph Myers         if (validd == -1) {
2007ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2008ae35eea7SJoseph Myers             break;
2009ae35eea7SJoseph Myers         }
2010bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2011fcf5ef2aSThomas Huth             res <<= 1;
2012fcf5ef2aSThomas Huth             v = 1;
2013bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2014fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2015fcf5ef2aSThomas Huth             }
2016fcf5ef2aSThomas Huth             res |= v;
2017fcf5ef2aSThomas Huth         }
2018fcf5ef2aSThomas Huth         break;
2019fcf5ef2aSThomas Huth     }
2020fcf5ef2aSThomas Huth 
2021fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2022fcf5ef2aSThomas Huth     case 1:
2023fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2024fcf5ef2aSThomas Huth         break;
2025fcf5ef2aSThomas Huth     case 3:
2026fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2027fcf5ef2aSThomas Huth         break;
2028fcf5ef2aSThomas Huth     }
2029fcf5ef2aSThomas Huth 
2030fcf5ef2aSThomas Huth     if (res) {
2031fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2032fcf5ef2aSThomas Huth     }
2033fcf5ef2aSThomas Huth     if (res & 1) {
2034fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2035fcf5ef2aSThomas Huth     }
2036fcf5ef2aSThomas Huth 
2037fcf5ef2aSThomas Huth     return res;
2038fcf5ef2aSThomas Huth }
2039fcf5ef2aSThomas Huth 
glue(helper_pcmpestri,SUFFIX)2040fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2041fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2042fcf5ef2aSThomas Huth {
2043fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2044fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2045fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2046fcf5ef2aSThomas Huth 
2047fcf5ef2aSThomas Huth     if (res) {
2048fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2049fcf5ef2aSThomas Huth     } else {
2050fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2051fcf5ef2aSThomas Huth     }
2052fcf5ef2aSThomas Huth }
2053fcf5ef2aSThomas Huth 
glue(helper_pcmpestrm,SUFFIX)2054fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2055fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2056fcf5ef2aSThomas Huth {
2057fcf5ef2aSThomas Huth     int i;
2058fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2059fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2060fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2061fcf5ef2aSThomas Huth 
2062fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2063fcf5ef2aSThomas Huth         if (ctrl & 1) {
2064fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2065fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2066fcf5ef2aSThomas Huth             }
2067fcf5ef2aSThomas Huth         } else {
2068fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2069fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2070fcf5ef2aSThomas Huth             }
2071fcf5ef2aSThomas Huth         }
2072fcf5ef2aSThomas Huth     } else {
2073fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2074fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2075fcf5ef2aSThomas Huth     }
2076fcf5ef2aSThomas Huth }
2077fcf5ef2aSThomas Huth 
glue(helper_pcmpistri,SUFFIX)2078fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2079fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2080fcf5ef2aSThomas Huth {
2081fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2082fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2083fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2084fcf5ef2aSThomas Huth 
2085fcf5ef2aSThomas Huth     if (res) {
2086fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2087fcf5ef2aSThomas Huth     } else {
2088fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2089fcf5ef2aSThomas Huth     }
2090fcf5ef2aSThomas Huth }
2091fcf5ef2aSThomas Huth 
glue(helper_pcmpistrm,SUFFIX)2092fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2093fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2094fcf5ef2aSThomas Huth {
2095fcf5ef2aSThomas Huth     int i;
2096fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2097fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2098fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2099fcf5ef2aSThomas Huth 
2100fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2101fcf5ef2aSThomas Huth         if (ctrl & 1) {
2102fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2103fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2104fcf5ef2aSThomas Huth             }
2105fcf5ef2aSThomas Huth         } else {
2106fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2107fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2108fcf5ef2aSThomas Huth             }
2109fcf5ef2aSThomas Huth         }
2110fcf5ef2aSThomas Huth     } else {
2111fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2112fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2113fcf5ef2aSThomas Huth     }
2114fcf5ef2aSThomas Huth }
2115fcf5ef2aSThomas Huth 
2116fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2117fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
helper_crc32(uint32_t crc1,target_ulong msg,uint32_t len)2118fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2119fcf5ef2aSThomas Huth {
2120fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2121fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2122fcf5ef2aSThomas Huth 
2123fcf5ef2aSThomas Huth     while (len--) {
2124fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2125fcf5ef2aSThomas Huth     }
2126fcf5ef2aSThomas Huth 
2127fcf5ef2aSThomas Huth     return crc;
2128fcf5ef2aSThomas Huth }
2129fcf5ef2aSThomas Huth 
2130fd17264aSPaul Brook #endif
2131fd17264aSPaul Brook 
glue(helper_pclmulqdq,SUFFIX)2132f05f9789SPaolo Bonzini void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
21335a09df21SPaul Brook                                     uint32_t ctrl)
21345a09df21SPaul Brook {
21357fcb5054SRichard Henderson     int a_idx = (ctrl & 1) != 0;
21367fcb5054SRichard Henderson     int b_idx = (ctrl & 16) != 0;
21375a09df21SPaul Brook 
21387fcb5054SRichard Henderson     for (int i = 0; i < SHIFT; i++) {
21397fcb5054SRichard Henderson         uint64_t a = v->Q(2 * i + a_idx);
21407fcb5054SRichard Henderson         uint64_t b = s->Q(2 * i + b_idx);
21417fcb5054SRichard Henderson         Int128 *r = (Int128 *)&d->ZMM_X(i);
21427fcb5054SRichard Henderson 
21437fcb5054SRichard Henderson         *r = clmul_64(a, b);
21445a09df21SPaul Brook     }
2145fcf5ef2aSThomas Huth }
2146fcf5ef2aSThomas Huth 
glue(helper_aesdec,SUFFIX)2147f05f9789SPaolo Bonzini void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2148fcf5ef2aSThomas Huth {
214944a0c4a8SRichard Henderson     for (int i = 0; i < SHIFT; i++) {
215044a0c4a8SRichard Henderson         AESState *ad = (AESState *)&d->ZMM_X(i);
215144a0c4a8SRichard Henderson         AESState *st = (AESState *)&v->ZMM_X(i);
215244a0c4a8SRichard Henderson         AESState *rk = (AESState *)&s->ZMM_X(i);
2153fcf5ef2aSThomas Huth 
215444a0c4a8SRichard Henderson         aesdec_ISB_ISR_IMC_AK(ad, st, rk, false);
2155fcf5ef2aSThomas Huth     }
2156fcf5ef2aSThomas Huth }
2157fcf5ef2aSThomas Huth 
glue(helper_aesdeclast,SUFFIX)2158f05f9789SPaolo Bonzini void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2159fcf5ef2aSThomas Huth {
216000b5c7bdSRichard Henderson     for (int i = 0; i < SHIFT; i++) {
216100b5c7bdSRichard Henderson         AESState *ad = (AESState *)&d->ZMM_X(i);
216200b5c7bdSRichard Henderson         AESState *st = (AESState *)&v->ZMM_X(i);
216300b5c7bdSRichard Henderson         AESState *rk = (AESState *)&s->ZMM_X(i);
2164fcf5ef2aSThomas Huth 
216500b5c7bdSRichard Henderson         aesdec_ISB_ISR_AK(ad, st, rk, false);
2166fcf5ef2aSThomas Huth     }
2167fcf5ef2aSThomas Huth }
2168fcf5ef2aSThomas Huth 
glue(helper_aesenc,SUFFIX)2169f05f9789SPaolo Bonzini void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2170fcf5ef2aSThomas Huth {
217103cf414eSRichard Henderson     for (int i = 0; i < SHIFT; i++) {
217203cf414eSRichard Henderson         AESState *ad = (AESState *)&d->ZMM_X(i);
217303cf414eSRichard Henderson         AESState *st = (AESState *)&v->ZMM_X(i);
217403cf414eSRichard Henderson         AESState *rk = (AESState *)&s->ZMM_X(i);
2175fcf5ef2aSThomas Huth 
217603cf414eSRichard Henderson         aesenc_SB_SR_MC_AK(ad, st, rk, false);
2177fcf5ef2aSThomas Huth     }
2178fcf5ef2aSThomas Huth }
2179fcf5ef2aSThomas Huth 
glue(helper_aesenclast,SUFFIX)2180f05f9789SPaolo Bonzini void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2181fcf5ef2aSThomas Huth {
2182cc648f50SRichard Henderson     for (int i = 0; i < SHIFT; i++) {
2183cc648f50SRichard Henderson         AESState *ad = (AESState *)&d->ZMM_X(i);
2184cc648f50SRichard Henderson         AESState *st = (AESState *)&v->ZMM_X(i);
2185cc648f50SRichard Henderson         AESState *rk = (AESState *)&s->ZMM_X(i);
2186fcf5ef2aSThomas Huth 
2187cc648f50SRichard Henderson         aesenc_SB_SR_AK(ad, st, rk, false);
2188a64fc269SPaul Brook     }
2189fcf5ef2aSThomas Huth }
2190fcf5ef2aSThomas Huth 
2191a64fc269SPaul Brook #if SHIFT == 1
glue(helper_aesimc,SUFFIX)2192fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2193fcf5ef2aSThomas Huth {
21945f40edb7SRichard Henderson     AESState *ad = (AESState *)&d->ZMM_X(0);
21955f40edb7SRichard Henderson     AESState *st = (AESState *)&s->ZMM_X(0);
2196fcf5ef2aSThomas Huth 
21975f40edb7SRichard Henderson     aesdec_IMC(ad, st, false);
2198fcf5ef2aSThomas Huth }
2199fcf5ef2aSThomas Huth 
glue(helper_aeskeygenassist,SUFFIX)2200fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2201fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2202fcf5ef2aSThomas Huth {
2203fcf5ef2aSThomas Huth     int i;
2204fcf5ef2aSThomas Huth     Reg tmp = *s;
2205fcf5ef2aSThomas Huth 
2206fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2207fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2208fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2209fcf5ef2aSThomas Huth     }
2210fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2211fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2212fcf5ef2aSThomas Huth }
2213fcf5ef2aSThomas Huth #endif
2214a64fc269SPaul Brook #endif
2215fcf5ef2aSThomas Huth 
221679068477SPaolo Bonzini #if SHIFT >= 1
glue(helper_vpermilpd,SUFFIX)221716fc5726SPaolo Bonzini void glue(helper_vpermilpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
221816fc5726SPaolo Bonzini {
221916fc5726SPaolo Bonzini     uint64_t r0, r1;
222016fc5726SPaolo Bonzini     int i;
222116fc5726SPaolo Bonzini 
222216fc5726SPaolo Bonzini     for (i = 0; i < 1 << SHIFT; i += 2) {
222316fc5726SPaolo Bonzini         r0 = v->Q(i + ((s->Q(i) >> 1) & 1));
222416fc5726SPaolo Bonzini         r1 = v->Q(i + ((s->Q(i+1) >> 1) & 1));
222516fc5726SPaolo Bonzini         d->Q(i) = r0;
222616fc5726SPaolo Bonzini         d->Q(i+1) = r1;
222716fc5726SPaolo Bonzini     }
222816fc5726SPaolo Bonzini }
222916fc5726SPaolo Bonzini 
glue(helper_vpermilps,SUFFIX)223016fc5726SPaolo Bonzini void glue(helper_vpermilps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
223116fc5726SPaolo Bonzini {
223216fc5726SPaolo Bonzini     uint32_t r0, r1, r2, r3;
223316fc5726SPaolo Bonzini     int i;
223416fc5726SPaolo Bonzini 
223516fc5726SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i += 4) {
223616fc5726SPaolo Bonzini         r0 = v->L(i + (s->L(i) & 3));
223716fc5726SPaolo Bonzini         r1 = v->L(i + (s->L(i+1) & 3));
223816fc5726SPaolo Bonzini         r2 = v->L(i + (s->L(i+2) & 3));
223916fc5726SPaolo Bonzini         r3 = v->L(i + (s->L(i+3) & 3));
224016fc5726SPaolo Bonzini         d->L(i) = r0;
224116fc5726SPaolo Bonzini         d->L(i+1) = r1;
224216fc5726SPaolo Bonzini         d->L(i+2) = r2;
224316fc5726SPaolo Bonzini         d->L(i+3) = r3;
224416fc5726SPaolo Bonzini     }
224516fc5726SPaolo Bonzini }
224616fc5726SPaolo Bonzini 
glue(helper_vpermilpd_imm,SUFFIX)224779068477SPaolo Bonzini void glue(helper_vpermilpd_imm, SUFFIX)(Reg *d, Reg *s, uint32_t order)
224879068477SPaolo Bonzini {
224979068477SPaolo Bonzini     uint64_t r0, r1;
225079068477SPaolo Bonzini     int i;
225179068477SPaolo Bonzini 
225279068477SPaolo Bonzini     for (i = 0; i < 1 << SHIFT; i += 2) {
225379068477SPaolo Bonzini         r0 = s->Q(i + ((order >> 0) & 1));
225479068477SPaolo Bonzini         r1 = s->Q(i + ((order >> 1) & 1));
225579068477SPaolo Bonzini         d->Q(i) = r0;
225679068477SPaolo Bonzini         d->Q(i+1) = r1;
225779068477SPaolo Bonzini 
225879068477SPaolo Bonzini         order >>= 2;
225979068477SPaolo Bonzini     }
226079068477SPaolo Bonzini }
226179068477SPaolo Bonzini 
glue(helper_vpermilps_imm,SUFFIX)226279068477SPaolo Bonzini void glue(helper_vpermilps_imm, SUFFIX)(Reg *d, Reg *s, uint32_t order)
226379068477SPaolo Bonzini {
226479068477SPaolo Bonzini     uint32_t r0, r1, r2, r3;
226579068477SPaolo Bonzini     int i;
226679068477SPaolo Bonzini 
226779068477SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i += 4) {
226879068477SPaolo Bonzini         r0 = s->L(i + ((order >> 0) & 3));
226979068477SPaolo Bonzini         r1 = s->L(i + ((order >> 2) & 3));
227079068477SPaolo Bonzini         r2 = s->L(i + ((order >> 4) & 3));
227179068477SPaolo Bonzini         r3 = s->L(i + ((order >> 6) & 3));
227279068477SPaolo Bonzini         d->L(i) = r0;
227379068477SPaolo Bonzini         d->L(i+1) = r1;
227479068477SPaolo Bonzini         d->L(i+2) = r2;
227579068477SPaolo Bonzini         d->L(i+3) = r3;
227679068477SPaolo Bonzini     }
227779068477SPaolo Bonzini }
227879068477SPaolo Bonzini 
227916fc5726SPaolo Bonzini #if SHIFT == 1
228016fc5726SPaolo Bonzini #define FPSRLVD(x, c) (c < 32 ? ((x) >> c) : 0)
228116fc5726SPaolo Bonzini #define FPSRLVQ(x, c) (c < 64 ? ((x) >> c) : 0)
228216fc5726SPaolo Bonzini #define FPSRAVD(x, c) ((int32_t)(x) >> (c < 32 ? c : 31))
228316fc5726SPaolo Bonzini #define FPSRAVQ(x, c) ((int64_t)(x) >> (c < 64 ? c : 63))
228416fc5726SPaolo Bonzini #define FPSLLVD(x, c) (c < 32 ? ((x) << c) : 0)
228516fc5726SPaolo Bonzini #define FPSLLVQ(x, c) (c < 64 ? ((x) << c) : 0)
228616fc5726SPaolo Bonzini #endif
228716fc5726SPaolo Bonzini 
SSE_HELPER_L(helper_vpsrlvd,FPSRLVD)228816fc5726SPaolo Bonzini SSE_HELPER_L(helper_vpsrlvd, FPSRLVD)
228916fc5726SPaolo Bonzini SSE_HELPER_L(helper_vpsravd, FPSRAVD)
229016fc5726SPaolo Bonzini SSE_HELPER_L(helper_vpsllvd, FPSLLVD)
229116fc5726SPaolo Bonzini 
229216fc5726SPaolo Bonzini SSE_HELPER_Q(helper_vpsrlvq, FPSRLVQ)
229316fc5726SPaolo Bonzini SSE_HELPER_Q(helper_vpsravq, FPSRAVQ)
229416fc5726SPaolo Bonzini SSE_HELPER_Q(helper_vpsllvq, FPSLLVQ)
229516fc5726SPaolo Bonzini 
229616fc5726SPaolo Bonzini void glue(helper_vtestps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
229716fc5726SPaolo Bonzini {
229816fc5726SPaolo Bonzini     uint32_t zf = 0, cf = 0;
229916fc5726SPaolo Bonzini     int i;
230016fc5726SPaolo Bonzini 
230116fc5726SPaolo Bonzini     for (i = 0; i < 2 << SHIFT; i++) {
230216fc5726SPaolo Bonzini         zf |= (s->L(i) &  d->L(i));
230316fc5726SPaolo Bonzini         cf |= (s->L(i) & ~d->L(i));
230416fc5726SPaolo Bonzini     }
230516fc5726SPaolo Bonzini     CC_SRC = ((zf >> 31) ? 0 : CC_Z) | ((cf >> 31) ? 0 : CC_C);
2306*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
230716fc5726SPaolo Bonzini }
230816fc5726SPaolo Bonzini 
glue(helper_vtestpd,SUFFIX)230916fc5726SPaolo Bonzini void glue(helper_vtestpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
231016fc5726SPaolo Bonzini {
231116fc5726SPaolo Bonzini     uint64_t zf = 0, cf = 0;
231216fc5726SPaolo Bonzini     int i;
231316fc5726SPaolo Bonzini 
231416fc5726SPaolo Bonzini     for (i = 0; i < 1 << SHIFT; i++) {
231516fc5726SPaolo Bonzini         zf |= (s->Q(i) &  d->Q(i));
231616fc5726SPaolo Bonzini         cf |= (s->Q(i) & ~d->Q(i));
231716fc5726SPaolo Bonzini     }
231816fc5726SPaolo Bonzini     CC_SRC = ((zf >> 63) ? 0 : CC_Z) | ((cf >> 63) ? 0 : CC_C);
2319*abdcc5c8SPaolo Bonzini     CC_OP = CC_OP_EFLAGS;
232016fc5726SPaolo Bonzini }
232116fc5726SPaolo Bonzini 
glue(helper_vpmaskmovd_st,SUFFIX)232216fc5726SPaolo Bonzini void glue(helper_vpmaskmovd_st, SUFFIX)(CPUX86State *env,
232316fc5726SPaolo Bonzini                                         Reg *v, Reg *s, target_ulong a0)
232416fc5726SPaolo Bonzini {
232516fc5726SPaolo Bonzini     int i;
232616fc5726SPaolo Bonzini 
232716fc5726SPaolo Bonzini     for (i = 0; i < (2 << SHIFT); i++) {
232816fc5726SPaolo Bonzini         if (v->L(i) >> 31) {
232916fc5726SPaolo Bonzini             cpu_stl_data_ra(env, a0 + i * 4, s->L(i), GETPC());
233016fc5726SPaolo Bonzini         }
233116fc5726SPaolo Bonzini     }
233216fc5726SPaolo Bonzini }
233316fc5726SPaolo Bonzini 
glue(helper_vpmaskmovq_st,SUFFIX)233416fc5726SPaolo Bonzini void glue(helper_vpmaskmovq_st, SUFFIX)(CPUX86State *env,
233516fc5726SPaolo Bonzini                                         Reg *v, Reg *s, target_ulong a0)
233616fc5726SPaolo Bonzini {
233716fc5726SPaolo Bonzini     int i;
233816fc5726SPaolo Bonzini 
233916fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
234016fc5726SPaolo Bonzini         if (v->Q(i) >> 63) {
234116fc5726SPaolo Bonzini             cpu_stq_data_ra(env, a0 + i * 8, s->Q(i), GETPC());
234216fc5726SPaolo Bonzini         }
234316fc5726SPaolo Bonzini     }
234416fc5726SPaolo Bonzini }
234516fc5726SPaolo Bonzini 
glue(helper_vpmaskmovd,SUFFIX)234616fc5726SPaolo Bonzini void glue(helper_vpmaskmovd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
234716fc5726SPaolo Bonzini {
234816fc5726SPaolo Bonzini     int i;
234916fc5726SPaolo Bonzini 
235016fc5726SPaolo Bonzini     for (i = 0; i < (2 << SHIFT); i++) {
235116fc5726SPaolo Bonzini         d->L(i) = (v->L(i) >> 31) ? s->L(i) : 0;
235216fc5726SPaolo Bonzini     }
235316fc5726SPaolo Bonzini }
235416fc5726SPaolo Bonzini 
glue(helper_vpmaskmovq,SUFFIX)235516fc5726SPaolo Bonzini void glue(helper_vpmaskmovq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
235616fc5726SPaolo Bonzini {
235716fc5726SPaolo Bonzini     int i;
235816fc5726SPaolo Bonzini 
235916fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
236016fc5726SPaolo Bonzini         d->Q(i) = (v->Q(i) >> 63) ? s->Q(i) : 0;
236116fc5726SPaolo Bonzini     }
236216fc5726SPaolo Bonzini }
236316fc5726SPaolo Bonzini 
glue(helper_vpgatherdd,SUFFIX)236416fc5726SPaolo Bonzini void glue(helper_vpgatherdd, SUFFIX)(CPUX86State *env,
236516fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
236616fc5726SPaolo Bonzini {
236716fc5726SPaolo Bonzini     int i;
236816fc5726SPaolo Bonzini     for (i = 0; i < (2 << SHIFT); i++) {
236916fc5726SPaolo Bonzini         if (v->L(i) >> 31) {
237016fc5726SPaolo Bonzini             target_ulong addr = a0
237116fc5726SPaolo Bonzini                 + ((target_ulong)(int32_t)s->L(i) << scale);
237216fc5726SPaolo Bonzini             d->L(i) = cpu_ldl_data_ra(env, addr, GETPC());
237316fc5726SPaolo Bonzini         }
237416fc5726SPaolo Bonzini         v->L(i) = 0;
237516fc5726SPaolo Bonzini     }
237616fc5726SPaolo Bonzini }
237716fc5726SPaolo Bonzini 
glue(helper_vpgatherdq,SUFFIX)237816fc5726SPaolo Bonzini void glue(helper_vpgatherdq, SUFFIX)(CPUX86State *env,
237916fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
238016fc5726SPaolo Bonzini {
238116fc5726SPaolo Bonzini     int i;
238216fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
238316fc5726SPaolo Bonzini         if (v->Q(i) >> 63) {
238416fc5726SPaolo Bonzini             target_ulong addr = a0
238516fc5726SPaolo Bonzini                 + ((target_ulong)(int32_t)s->L(i) << scale);
238616fc5726SPaolo Bonzini             d->Q(i) = cpu_ldq_data_ra(env, addr, GETPC());
238716fc5726SPaolo Bonzini         }
238816fc5726SPaolo Bonzini         v->Q(i) = 0;
238916fc5726SPaolo Bonzini     }
239016fc5726SPaolo Bonzini }
239116fc5726SPaolo Bonzini 
glue(helper_vpgatherqd,SUFFIX)239216fc5726SPaolo Bonzini void glue(helper_vpgatherqd, SUFFIX)(CPUX86State *env,
239316fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
239416fc5726SPaolo Bonzini {
239516fc5726SPaolo Bonzini     int i;
239616fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
239716fc5726SPaolo Bonzini         if (v->L(i) >> 31) {
239816fc5726SPaolo Bonzini             target_ulong addr = a0
239916fc5726SPaolo Bonzini                 + ((target_ulong)(int64_t)s->Q(i) << scale);
240016fc5726SPaolo Bonzini             d->L(i) = cpu_ldl_data_ra(env, addr, GETPC());
240116fc5726SPaolo Bonzini         }
240216fc5726SPaolo Bonzini         v->L(i) = 0;
240316fc5726SPaolo Bonzini     }
240416fc5726SPaolo Bonzini     for (i /= 2; i < 1 << SHIFT; i++) {
240516fc5726SPaolo Bonzini         d->Q(i) = 0;
240616fc5726SPaolo Bonzini         v->Q(i) = 0;
240716fc5726SPaolo Bonzini     }
240816fc5726SPaolo Bonzini }
240916fc5726SPaolo Bonzini 
glue(helper_vpgatherqq,SUFFIX)241016fc5726SPaolo Bonzini void glue(helper_vpgatherqq, SUFFIX)(CPUX86State *env,
241116fc5726SPaolo Bonzini         Reg *d, Reg *v, Reg *s, target_ulong a0, unsigned scale)
241216fc5726SPaolo Bonzini {
241316fc5726SPaolo Bonzini     int i;
241416fc5726SPaolo Bonzini     for (i = 0; i < (1 << SHIFT); i++) {
241516fc5726SPaolo Bonzini         if (v->Q(i) >> 63) {
241616fc5726SPaolo Bonzini             target_ulong addr = a0
241716fc5726SPaolo Bonzini                 + ((target_ulong)(int64_t)s->Q(i) << scale);
241816fc5726SPaolo Bonzini             d->Q(i) = cpu_ldq_data_ra(env, addr, GETPC());
241916fc5726SPaolo Bonzini         }
242016fc5726SPaolo Bonzini         v->Q(i) = 0;
242116fc5726SPaolo Bonzini     }
242216fc5726SPaolo Bonzini }
242316fc5726SPaolo Bonzini #endif
242416fc5726SPaolo Bonzini 
242579068477SPaolo Bonzini #if SHIFT >= 2
helper_vpermdq_ymm(Reg * d,Reg * v,Reg * s,uint32_t order)242679068477SPaolo Bonzini void helper_vpermdq_ymm(Reg *d, Reg *v, Reg *s, uint32_t order)
242779068477SPaolo Bonzini {
242879068477SPaolo Bonzini     uint64_t r0, r1, r2, r3;
242979068477SPaolo Bonzini 
243079068477SPaolo Bonzini     switch (order & 3) {
243179068477SPaolo Bonzini     case 0:
243279068477SPaolo Bonzini         r0 = v->Q(0);
243379068477SPaolo Bonzini         r1 = v->Q(1);
243479068477SPaolo Bonzini         break;
243579068477SPaolo Bonzini     case 1:
243679068477SPaolo Bonzini         r0 = v->Q(2);
243779068477SPaolo Bonzini         r1 = v->Q(3);
243879068477SPaolo Bonzini         break;
243979068477SPaolo Bonzini     case 2:
244079068477SPaolo Bonzini         r0 = s->Q(0);
244179068477SPaolo Bonzini         r1 = s->Q(1);
244279068477SPaolo Bonzini         break;
244379068477SPaolo Bonzini     case 3:
244479068477SPaolo Bonzini         r0 = s->Q(2);
244579068477SPaolo Bonzini         r1 = s->Q(3);
244679068477SPaolo Bonzini         break;
2447c0a6665cSEric Auger     default: /* default case added to help the compiler to avoid warnings */
2448c0a6665cSEric Auger         g_assert_not_reached();
244979068477SPaolo Bonzini     }
245079068477SPaolo Bonzini     switch ((order >> 4) & 3) {
245179068477SPaolo Bonzini     case 0:
245279068477SPaolo Bonzini         r2 = v->Q(0);
245379068477SPaolo Bonzini         r3 = v->Q(1);
245479068477SPaolo Bonzini         break;
245579068477SPaolo Bonzini     case 1:
245679068477SPaolo Bonzini         r2 = v->Q(2);
245779068477SPaolo Bonzini         r3 = v->Q(3);
245879068477SPaolo Bonzini         break;
245979068477SPaolo Bonzini     case 2:
246079068477SPaolo Bonzini         r2 = s->Q(0);
246179068477SPaolo Bonzini         r3 = s->Q(1);
246279068477SPaolo Bonzini         break;
246379068477SPaolo Bonzini     case 3:
246479068477SPaolo Bonzini         r2 = s->Q(2);
246579068477SPaolo Bonzini         r3 = s->Q(3);
246679068477SPaolo Bonzini         break;
2467c0a6665cSEric Auger     default: /* default case added to help the compiler to avoid warnings */
2468c0a6665cSEric Auger         g_assert_not_reached();
246979068477SPaolo Bonzini     }
247079068477SPaolo Bonzini     d->Q(0) = r0;
247179068477SPaolo Bonzini     d->Q(1) = r1;
247279068477SPaolo Bonzini     d->Q(2) = r2;
247379068477SPaolo Bonzini     d->Q(3) = r3;
2474056d6490SXinyu Li     if (order & 0x8) {
2475056d6490SXinyu Li         d->Q(0) = 0;
2476056d6490SXinyu Li         d->Q(1) = 0;
2477056d6490SXinyu Li     }
2478056d6490SXinyu Li     if (order & 0x80) {
2479056d6490SXinyu Li         d->Q(2) = 0;
2480056d6490SXinyu Li         d->Q(3) = 0;
2481056d6490SXinyu Li     }
248279068477SPaolo Bonzini }
248379068477SPaolo Bonzini 
helper_vpermq_ymm(Reg * d,Reg * s,uint32_t order)248479068477SPaolo Bonzini void helper_vpermq_ymm(Reg *d, Reg *s, uint32_t order)
248579068477SPaolo Bonzini {
248679068477SPaolo Bonzini     uint64_t r0, r1, r2, r3;
248779068477SPaolo Bonzini     r0 = s->Q(order & 3);
248879068477SPaolo Bonzini     r1 = s->Q((order >> 2) & 3);
248979068477SPaolo Bonzini     r2 = s->Q((order >> 4) & 3);
249079068477SPaolo Bonzini     r3 = s->Q((order >> 6) & 3);
249179068477SPaolo Bonzini     d->Q(0) = r0;
249279068477SPaolo Bonzini     d->Q(1) = r1;
249379068477SPaolo Bonzini     d->Q(2) = r2;
249479068477SPaolo Bonzini     d->Q(3) = r3;
249579068477SPaolo Bonzini }
249616fc5726SPaolo Bonzini 
helper_vpermd_ymm(Reg * d,Reg * v,Reg * s)249716fc5726SPaolo Bonzini void helper_vpermd_ymm(Reg *d, Reg *v, Reg *s)
249816fc5726SPaolo Bonzini {
249916fc5726SPaolo Bonzini     uint32_t r[8];
250016fc5726SPaolo Bonzini     int i;
250116fc5726SPaolo Bonzini 
250216fc5726SPaolo Bonzini     for (i = 0; i < 8; i++) {
250316fc5726SPaolo Bonzini         r[i] = s->L(v->L(i) & 7);
250416fc5726SPaolo Bonzini     }
250516fc5726SPaolo Bonzini     for (i = 0; i < 8; i++) {
250616fc5726SPaolo Bonzini         d->L(i) = r[i];
250716fc5726SPaolo Bonzini     }
250816fc5726SPaolo Bonzini }
250979068477SPaolo Bonzini #endif
251079068477SPaolo Bonzini 
25112872b0f3SPaolo Bonzini /* FMA3 op helpers */
25122872b0f3SPaolo Bonzini #if SHIFT == 1
25132872b0f3SPaolo Bonzini #define SSE_HELPER_FMAS(name, elem, F)                                         \
25142872b0f3SPaolo Bonzini     void name(CPUX86State *env, Reg *d, Reg *a, Reg *b, Reg *c, int flags)     \
25152872b0f3SPaolo Bonzini     {                                                                          \
25162872b0f3SPaolo Bonzini         d->elem(0) = F(a->elem(0), b->elem(0), c->elem(0), flags, &env->sse_status); \
25172872b0f3SPaolo Bonzini     }
25182872b0f3SPaolo Bonzini #define SSE_HELPER_FMAP(name, elem, num, F)                                    \
25192872b0f3SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *a, Reg *b, Reg *c,  \
25202872b0f3SPaolo Bonzini                             int flags, int flip)                               \
25212872b0f3SPaolo Bonzini     {                                                                          \
25222872b0f3SPaolo Bonzini         int i;                                                                 \
25232872b0f3SPaolo Bonzini         for (i = 0; i < num; i++) {                                            \
25242872b0f3SPaolo Bonzini             d->elem(i) = F(a->elem(i), b->elem(i), c->elem(i), flags, &env->sse_status); \
25252872b0f3SPaolo Bonzini             flags ^= flip;                                                     \
25262872b0f3SPaolo Bonzini         }                                                                      \
25272872b0f3SPaolo Bonzini     }
25282872b0f3SPaolo Bonzini 
SSE_HELPER_FMAS(helper_fma4ss,ZMM_S,float32_muladd)25292872b0f3SPaolo Bonzini SSE_HELPER_FMAS(helper_fma4ss,  ZMM_S, float32_muladd)
25302872b0f3SPaolo Bonzini SSE_HELPER_FMAS(helper_fma4sd,  ZMM_D, float64_muladd)
25312872b0f3SPaolo Bonzini #endif
25322872b0f3SPaolo Bonzini 
25332872b0f3SPaolo Bonzini #if SHIFT >= 1
25342872b0f3SPaolo Bonzini SSE_HELPER_FMAP(helper_fma4ps,  ZMM_S, 2 << SHIFT, float32_muladd)
25352872b0f3SPaolo Bonzini SSE_HELPER_FMAP(helper_fma4pd,  ZMM_D, 1 << SHIFT, float64_muladd)
25362872b0f3SPaolo Bonzini #endif
25372872b0f3SPaolo Bonzini 
2538e582b629SPaolo Bonzini #if SHIFT == 1
2539e582b629SPaolo Bonzini #define SSE_HELPER_SHA1RNDS4(name, F, K) \
2540e582b629SPaolo Bonzini     void name(Reg *d, Reg *a, Reg *b)                                       \
2541e582b629SPaolo Bonzini     {                                                                       \
2542e582b629SPaolo Bonzini         uint32_t A, B, C, D, E, t, i;                                       \
2543e582b629SPaolo Bonzini                                                                             \
2544e582b629SPaolo Bonzini         A = a->L(3);                                                        \
2545e582b629SPaolo Bonzini         B = a->L(2);                                                        \
2546e582b629SPaolo Bonzini         C = a->L(1);                                                        \
2547e582b629SPaolo Bonzini         D = a->L(0);                                                        \
2548e582b629SPaolo Bonzini         E = 0;                                                              \
2549e582b629SPaolo Bonzini                                                                             \
2550e582b629SPaolo Bonzini         for (i = 0; i <= 3; i++) {                                          \
2551e582b629SPaolo Bonzini             t = F(B, C, D) + rol32(A, 5) + b->L(3 - i) + E + K;             \
2552e582b629SPaolo Bonzini             E = D;                                                          \
2553e582b629SPaolo Bonzini             D = C;                                                          \
2554e582b629SPaolo Bonzini             C = rol32(B, 30);                                               \
2555e582b629SPaolo Bonzini             B = A;                                                          \
2556e582b629SPaolo Bonzini             A = t;                                                          \
2557e582b629SPaolo Bonzini         }                                                                   \
2558e582b629SPaolo Bonzini                                                                             \
2559e582b629SPaolo Bonzini         d->L(3) = A;                                                        \
2560e582b629SPaolo Bonzini         d->L(2) = B;                                                        \
2561e582b629SPaolo Bonzini         d->L(1) = C;                                                        \
2562e582b629SPaolo Bonzini         d->L(0) = D;                                                        \
2563e582b629SPaolo Bonzini     }
2564e582b629SPaolo Bonzini 
2565e582b629SPaolo Bonzini #define SHA1_F0(b, c, d) (((b) & (c)) ^ (~(b) & (d)))
2566e582b629SPaolo Bonzini #define SHA1_F1(b, c, d) ((b) ^ (c) ^ (d))
2567e582b629SPaolo Bonzini #define SHA1_F2(b, c, d) (((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
2568e582b629SPaolo Bonzini 
2569e582b629SPaolo Bonzini SSE_HELPER_SHA1RNDS4(helper_sha1rnds4_f0, SHA1_F0, 0x5A827999)
2570e582b629SPaolo Bonzini SSE_HELPER_SHA1RNDS4(helper_sha1rnds4_f1, SHA1_F1, 0x6ED9EBA1)
2571e582b629SPaolo Bonzini SSE_HELPER_SHA1RNDS4(helper_sha1rnds4_f2, SHA1_F2, 0x8F1BBCDC)
2572e582b629SPaolo Bonzini SSE_HELPER_SHA1RNDS4(helper_sha1rnds4_f3, SHA1_F1, 0xCA62C1D6)
2573e582b629SPaolo Bonzini 
2574e582b629SPaolo Bonzini void helper_sha1nexte(Reg *d, Reg *a, Reg *b)
2575e582b629SPaolo Bonzini {
2576e582b629SPaolo Bonzini     d->L(3) = b->L(3) + rol32(a->L(3), 30);
2577e582b629SPaolo Bonzini     d->L(2) = b->L(2);
2578e582b629SPaolo Bonzini     d->L(1) = b->L(1);
2579e582b629SPaolo Bonzini     d->L(0) = b->L(0);
2580e582b629SPaolo Bonzini }
2581e582b629SPaolo Bonzini 
helper_sha1msg1(Reg * d,Reg * a,Reg * b)2582e582b629SPaolo Bonzini void helper_sha1msg1(Reg *d, Reg *a, Reg *b)
2583e582b629SPaolo Bonzini {
2584e582b629SPaolo Bonzini     /* These could be overwritten by the first two assignments, save them.  */
2585e582b629SPaolo Bonzini     uint32_t b3 = b->L(3);
2586e582b629SPaolo Bonzini     uint32_t b2 = b->L(2);
2587e582b629SPaolo Bonzini 
2588e582b629SPaolo Bonzini     d->L(3) = a->L(3) ^ a->L(1);
2589e582b629SPaolo Bonzini     d->L(2) = a->L(2) ^ a->L(0);
2590e582b629SPaolo Bonzini     d->L(1) = a->L(1) ^ b3;
2591e582b629SPaolo Bonzini     d->L(0) = a->L(0) ^ b2;
2592e582b629SPaolo Bonzini }
2593e582b629SPaolo Bonzini 
helper_sha1msg2(Reg * d,Reg * a,Reg * b)2594e582b629SPaolo Bonzini void helper_sha1msg2(Reg *d, Reg *a, Reg *b)
2595e582b629SPaolo Bonzini {
2596e582b629SPaolo Bonzini     d->L(3) = rol32(a->L(3) ^ b->L(2), 1);
2597e582b629SPaolo Bonzini     d->L(2) = rol32(a->L(2) ^ b->L(1), 1);
2598e582b629SPaolo Bonzini     d->L(1) = rol32(a->L(1) ^ b->L(0), 1);
2599e582b629SPaolo Bonzini     d->L(0) = rol32(a->L(0) ^ d->L(3), 1);
2600e582b629SPaolo Bonzini }
2601e582b629SPaolo Bonzini 
2602e582b629SPaolo Bonzini #define SHA256_CH(e, f, g)  (((e) & (f)) ^ (~(e) & (g)))
2603e582b629SPaolo Bonzini #define SHA256_MAJ(a, b, c) (((a) & (b)) ^ ((a) & (c)) ^ ((b) & (c)))
2604e582b629SPaolo Bonzini 
2605e582b629SPaolo Bonzini #define SHA256_RNDS0(w) (ror32((w), 2) ^ ror32((w), 13) ^ ror32((w), 22))
2606e582b629SPaolo Bonzini #define SHA256_RNDS1(w) (ror32((w), 6) ^ ror32((w), 11) ^ ror32((w), 25))
2607e582b629SPaolo Bonzini #define SHA256_MSGS0(w) (ror32((w), 7) ^ ror32((w), 18) ^ ((w) >> 3))
2608e582b629SPaolo Bonzini #define SHA256_MSGS1(w) (ror32((w), 17) ^ ror32((w), 19) ^ ((w) >> 10))
2609e582b629SPaolo Bonzini 
helper_sha256rnds2(Reg * d,Reg * a,Reg * b,uint32_t wk0,uint32_t wk1)2610e582b629SPaolo Bonzini void helper_sha256rnds2(Reg *d, Reg *a, Reg *b, uint32_t wk0, uint32_t wk1)
2611e582b629SPaolo Bonzini {
2612e582b629SPaolo Bonzini     uint32_t t, AA, EE;
2613e582b629SPaolo Bonzini 
2614e582b629SPaolo Bonzini     uint32_t A = b->L(3);
2615e582b629SPaolo Bonzini     uint32_t B = b->L(2);
2616e582b629SPaolo Bonzini     uint32_t C = a->L(3);
2617e582b629SPaolo Bonzini     uint32_t D = a->L(2);
2618e582b629SPaolo Bonzini     uint32_t E = b->L(1);
2619e582b629SPaolo Bonzini     uint32_t F = b->L(0);
2620e582b629SPaolo Bonzini     uint32_t G = a->L(1);
2621e582b629SPaolo Bonzini     uint32_t H = a->L(0);
2622e582b629SPaolo Bonzini 
2623e582b629SPaolo Bonzini     /* Even round */
2624e582b629SPaolo Bonzini     t = SHA256_CH(E, F, G) + SHA256_RNDS1(E) + wk0 + H;
2625e582b629SPaolo Bonzini     AA = t + SHA256_MAJ(A, B, C) + SHA256_RNDS0(A);
2626e582b629SPaolo Bonzini     EE = t + D;
2627e582b629SPaolo Bonzini 
2628e582b629SPaolo Bonzini     /* These will be B and F at the end of the odd round */
2629e582b629SPaolo Bonzini     d->L(2) = AA;
2630e582b629SPaolo Bonzini     d->L(0) = EE;
2631e582b629SPaolo Bonzini 
2632e582b629SPaolo Bonzini     D = C, C = B, B = A, A = AA;
2633e582b629SPaolo Bonzini     H = G, G = F, F = E, E = EE;
2634e582b629SPaolo Bonzini 
2635e582b629SPaolo Bonzini     /* Odd round */
2636e582b629SPaolo Bonzini     t = SHA256_CH(E, F, G) + SHA256_RNDS1(E) + wk1 + H;
2637e582b629SPaolo Bonzini     AA = t + SHA256_MAJ(A, B, C) + SHA256_RNDS0(A);
2638e582b629SPaolo Bonzini     EE = t + D;
2639e582b629SPaolo Bonzini 
2640e582b629SPaolo Bonzini     d->L(3) = AA;
2641e582b629SPaolo Bonzini     d->L(1) = EE;
2642e582b629SPaolo Bonzini }
2643e582b629SPaolo Bonzini 
helper_sha256msg1(Reg * d,Reg * a,Reg * b)2644e582b629SPaolo Bonzini void helper_sha256msg1(Reg *d, Reg *a, Reg *b)
2645e582b629SPaolo Bonzini {
2646e582b629SPaolo Bonzini     /* b->L(0) could be overwritten by the first assignment, save it.  */
2647e582b629SPaolo Bonzini     uint32_t b0 = b->L(0);
2648e582b629SPaolo Bonzini 
2649e582b629SPaolo Bonzini     d->L(0) = a->L(0) + SHA256_MSGS0(a->L(1));
2650e582b629SPaolo Bonzini     d->L(1) = a->L(1) + SHA256_MSGS0(a->L(2));
2651e582b629SPaolo Bonzini     d->L(2) = a->L(2) + SHA256_MSGS0(a->L(3));
2652e582b629SPaolo Bonzini     d->L(3) = a->L(3) + SHA256_MSGS0(b0);
2653e582b629SPaolo Bonzini }
2654e582b629SPaolo Bonzini 
helper_sha256msg2(Reg * d,Reg * a,Reg * b)2655e582b629SPaolo Bonzini void helper_sha256msg2(Reg *d, Reg *a, Reg *b)
2656e582b629SPaolo Bonzini {
2657e582b629SPaolo Bonzini     /* Earlier assignments cannot overwrite any of the two operands.  */
2658e582b629SPaolo Bonzini     d->L(0) = a->L(0) + SHA256_MSGS1(b->L(2));
2659e582b629SPaolo Bonzini     d->L(1) = a->L(1) + SHA256_MSGS1(b->L(3));
2660e582b629SPaolo Bonzini     /* Yes, this reuses the previously computed values.  */
2661e582b629SPaolo Bonzini     d->L(2) = a->L(2) + SHA256_MSGS1(d->L(0));
2662e582b629SPaolo Bonzini     d->L(3) = a->L(3) + SHA256_MSGS1(d->L(1));
2663e582b629SPaolo Bonzini }
2664e582b629SPaolo Bonzini #endif
2665e582b629SPaolo Bonzini 
26663403cafeSPaul Brook #undef SSE_HELPER_S
26673403cafeSPaul Brook 
2668b98f886cSPaolo Bonzini #undef LANE_WIDTH
2669fcf5ef2aSThomas Huth #undef SHIFT
2670fcf5ef2aSThomas Huth #undef XMM_ONLY
2671fcf5ef2aSThomas Huth #undef Reg
2672fcf5ef2aSThomas Huth #undef B
2673fcf5ef2aSThomas Huth #undef W
2674fcf5ef2aSThomas Huth #undef L
2675fcf5ef2aSThomas Huth #undef Q
2676fcf5ef2aSThomas Huth #undef SUFFIX
2677