xref: /openbmc/qemu/target/i386/ops_sse.h (revision f05f9789f57d5394fc118fe31aa2a9f563311140)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *  Copyright (c) 2008 Intel Corporation  <andrew.zaborowski@intel.com>
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10d9ff33adSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "crypto/aes.h"
22fcf5ef2aSThomas Huth 
23fcf5ef2aSThomas Huth #if SHIFT == 0
24fcf5ef2aSThomas Huth #define Reg MMXReg
25fcf5ef2aSThomas Huth #define XMM_ONLY(...)
26fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
27fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
28fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
29fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
30fcf5ef2aSThomas Huth #define SUFFIX _mmx
31fcf5ef2aSThomas Huth #else
32fcf5ef2aSThomas Huth #define Reg ZMMReg
33fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
34fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
35fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
36fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
37fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
38fcf5ef2aSThomas Huth #define SUFFIX _xmm
39fcf5ef2aSThomas Huth #endif
40fcf5ef2aSThomas Huth 
4118592d2eSPaul Brook #define LANE_WIDTH (SHIFT ? 16 : 8)
42d45b0de6SPaul Brook #define PACK_WIDTH (LANE_WIDTH / 2)
4318592d2eSPaul Brook 
4418592d2eSPaul Brook #if SHIFT == 0
4518592d2eSPaul Brook #define FPSRL(x, c) ((x) >> shift)
4618592d2eSPaul Brook #define FPSRAW(x, c) ((int16_t)(x) >> shift)
4718592d2eSPaul Brook #define FPSRAL(x, c) ((int32_t)(x) >> shift)
4818592d2eSPaul Brook #define FPSLL(x, c) ((x) << shift)
49fcf5ef2aSThomas Huth #endif
5018592d2eSPaul Brook 
51*f05f9789SPaolo Bonzini void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
5218592d2eSPaul Brook {
5318592d2eSPaul Brook     int shift;
5418592d2eSPaul Brook     if (c->Q(0) > 15) {
5518592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
5618592d2eSPaul Brook             d->Q(i) = 0;
5718592d2eSPaul Brook         }
58fcf5ef2aSThomas Huth     } else {
5918592d2eSPaul Brook         shift = c->B(0);
6018592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
6118592d2eSPaul Brook             d->W(i) = FPSRL(s->W(i), shift);
6218592d2eSPaul Brook         }
63fcf5ef2aSThomas Huth     }
64fcf5ef2aSThomas Huth }
65fcf5ef2aSThomas Huth 
66*f05f9789SPaolo Bonzini void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
67fcf5ef2aSThomas Huth {
68fcf5ef2aSThomas Huth     int shift;
6918592d2eSPaul Brook     if (c->Q(0) > 15) {
7018592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
7118592d2eSPaul Brook             d->Q(i) = 0;
7218592d2eSPaul Brook         }
7318592d2eSPaul Brook     } else {
7418592d2eSPaul Brook         shift = c->B(0);
7518592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
7618592d2eSPaul Brook             d->W(i) = FPSLL(s->W(i), shift);
7718592d2eSPaul Brook         }
7818592d2eSPaul Brook     }
7918592d2eSPaul Brook }
80fcf5ef2aSThomas Huth 
81*f05f9789SPaolo Bonzini void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
8218592d2eSPaul Brook {
8318592d2eSPaul Brook     int shift;
8418592d2eSPaul Brook     if (c->Q(0) > 15) {
85fcf5ef2aSThomas Huth         shift = 15;
86fcf5ef2aSThomas Huth     } else {
8718592d2eSPaul Brook         shift = c->B(0);
88fcf5ef2aSThomas Huth     }
8918592d2eSPaul Brook     for (int i = 0; i < 4 << SHIFT; i++) {
9018592d2eSPaul Brook         d->W(i) = FPSRAW(s->W(i), shift);
9118592d2eSPaul Brook     }
92fcf5ef2aSThomas Huth }
93fcf5ef2aSThomas Huth 
94*f05f9789SPaolo Bonzini void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
95fcf5ef2aSThomas Huth {
96fcf5ef2aSThomas Huth     int shift;
9718592d2eSPaul Brook     if (c->Q(0) > 31) {
9818592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
9918592d2eSPaul Brook             d->Q(i) = 0;
10018592d2eSPaul Brook         }
101fcf5ef2aSThomas Huth     } else {
10218592d2eSPaul Brook         shift = c->B(0);
10318592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
10418592d2eSPaul Brook             d->L(i) = FPSRL(s->L(i), shift);
10518592d2eSPaul Brook         }
106fcf5ef2aSThomas Huth     }
107fcf5ef2aSThomas Huth }
108fcf5ef2aSThomas Huth 
109*f05f9789SPaolo Bonzini void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
110fcf5ef2aSThomas Huth {
111fcf5ef2aSThomas Huth     int shift;
11218592d2eSPaul Brook     if (c->Q(0) > 31) {
11318592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
11418592d2eSPaul Brook             d->Q(i) = 0;
11518592d2eSPaul Brook         }
116fcf5ef2aSThomas Huth     } else {
11718592d2eSPaul Brook         shift = c->B(0);
11818592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
11918592d2eSPaul Brook             d->L(i) = FPSLL(s->L(i), shift);
12018592d2eSPaul Brook         }
121fcf5ef2aSThomas Huth     }
122fcf5ef2aSThomas Huth }
123fcf5ef2aSThomas Huth 
124*f05f9789SPaolo Bonzini void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
125fcf5ef2aSThomas Huth {
126fcf5ef2aSThomas Huth     int shift;
12718592d2eSPaul Brook     if (c->Q(0) > 31) {
128fcf5ef2aSThomas Huth         shift = 31;
129fcf5ef2aSThomas Huth     } else {
13018592d2eSPaul Brook         shift = c->B(0);
131fcf5ef2aSThomas Huth     }
13218592d2eSPaul Brook     for (int i = 0; i < 2 << SHIFT; i++) {
13318592d2eSPaul Brook         d->L(i) = FPSRAL(s->L(i), shift);
13418592d2eSPaul Brook     }
135fcf5ef2aSThomas Huth }
136fcf5ef2aSThomas Huth 
137*f05f9789SPaolo Bonzini void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
138fcf5ef2aSThomas Huth {
139fcf5ef2aSThomas Huth     int shift;
14018592d2eSPaul Brook     if (c->Q(0) > 63) {
14118592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14218592d2eSPaul Brook             d->Q(i) = 0;
14318592d2eSPaul Brook         }
144fcf5ef2aSThomas Huth     } else {
14518592d2eSPaul Brook         shift = c->B(0);
14618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14718592d2eSPaul Brook             d->Q(i) = FPSRL(s->Q(i), shift);
14818592d2eSPaul Brook         }
149fcf5ef2aSThomas Huth     }
150fcf5ef2aSThomas Huth }
151fcf5ef2aSThomas Huth 
152*f05f9789SPaolo Bonzini void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
153fcf5ef2aSThomas Huth {
154fcf5ef2aSThomas Huth     int shift;
15518592d2eSPaul Brook     if (c->Q(0) > 63) {
15618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
15718592d2eSPaul Brook             d->Q(i) = 0;
15818592d2eSPaul Brook         }
159fcf5ef2aSThomas Huth     } else {
16018592d2eSPaul Brook         shift = c->B(0);
16118592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16218592d2eSPaul Brook             d->Q(i) = FPSLL(s->Q(i), shift);
16318592d2eSPaul Brook         }
164fcf5ef2aSThomas Huth     }
165fcf5ef2aSThomas Huth }
166fcf5ef2aSThomas Huth 
16718592d2eSPaul Brook #if SHIFT >= 1
168*f05f9789SPaolo Bonzini void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
169fcf5ef2aSThomas Huth {
17018592d2eSPaul Brook     int shift, i, j;
171fcf5ef2aSThomas Huth 
17218592d2eSPaul Brook     shift = c->L(0);
173fcf5ef2aSThomas Huth     if (shift > 16) {
174fcf5ef2aSThomas Huth         shift = 16;
175fcf5ef2aSThomas Huth     }
17618592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
177fcf5ef2aSThomas Huth         for (i = 0; i < 16 - shift; i++) {
17818592d2eSPaul Brook             d->B(j + i) = s->B(j + i + shift);
179fcf5ef2aSThomas Huth         }
180fcf5ef2aSThomas Huth         for (i = 16 - shift; i < 16; i++) {
18118592d2eSPaul Brook             d->B(j + i) = 0;
18218592d2eSPaul Brook         }
183fcf5ef2aSThomas Huth     }
184fcf5ef2aSThomas Huth }
185fcf5ef2aSThomas Huth 
186*f05f9789SPaolo Bonzini void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
187fcf5ef2aSThomas Huth {
18818592d2eSPaul Brook     int shift, i, j;
189fcf5ef2aSThomas Huth 
19018592d2eSPaul Brook     shift = c->L(0);
191fcf5ef2aSThomas Huth     if (shift > 16) {
192fcf5ef2aSThomas Huth         shift = 16;
193fcf5ef2aSThomas Huth     }
19418592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
195fcf5ef2aSThomas Huth         for (i = 15; i >= shift; i--) {
19618592d2eSPaul Brook             d->B(j + i) = s->B(j + i - shift);
197fcf5ef2aSThomas Huth         }
198fcf5ef2aSThomas Huth         for (i = 0; i < shift; i++) {
19918592d2eSPaul Brook             d->B(j + i) = 0;
20018592d2eSPaul Brook         }
201fcf5ef2aSThomas Huth     }
202fcf5ef2aSThomas Huth }
203fcf5ef2aSThomas Huth #endif
204fcf5ef2aSThomas Huth 
205ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F)                        \
206fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
207fcf5ef2aSThomas Huth     {                                                           \
208ee04a3c8SPaul Brook         int n = num;                                            \
209ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
210ee04a3c8SPaul Brook             d->elem(i) = F(s->elem(i));                         \
211ee04a3c8SPaul Brook         }                                                       \
212fcf5ef2aSThomas Huth     }
213fcf5ef2aSThomas Huth 
214ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F)                        \
215*f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)   \
216ee04a3c8SPaul Brook     {                                                           \
217ee04a3c8SPaul Brook         int n = num;                                            \
218ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
219ee04a3c8SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i));             \
220ee04a3c8SPaul Brook         }                                                       \
221ee04a3c8SPaul Brook     }
222ee04a3c8SPaul Brook 
223ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F)                                   \
224ee04a3c8SPaul Brook     SSE_HELPER_2(name, B, 8 << SHIFT, F)
225ee04a3c8SPaul Brook 
226fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
227ee04a3c8SPaul Brook     SSE_HELPER_2(name, W, 4 << SHIFT, F)
228fcf5ef2aSThomas Huth 
229fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
230ee04a3c8SPaul Brook     SSE_HELPER_2(name, L, 2 << SHIFT, F)
231fcf5ef2aSThomas Huth 
232fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
233ee04a3c8SPaul Brook     SSE_HELPER_2(name, Q, 1 << SHIFT, F)
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth #if SHIFT == 0
236fcf5ef2aSThomas Huth static inline int satub(int x)
237fcf5ef2aSThomas Huth {
238fcf5ef2aSThomas Huth     if (x < 0) {
239fcf5ef2aSThomas Huth         return 0;
240fcf5ef2aSThomas Huth     } else if (x > 255) {
241fcf5ef2aSThomas Huth         return 255;
242fcf5ef2aSThomas Huth     } else {
243fcf5ef2aSThomas Huth         return x;
244fcf5ef2aSThomas Huth     }
245fcf5ef2aSThomas Huth }
246fcf5ef2aSThomas Huth 
247fcf5ef2aSThomas Huth static inline int satuw(int x)
248fcf5ef2aSThomas Huth {
249fcf5ef2aSThomas Huth     if (x < 0) {
250fcf5ef2aSThomas Huth         return 0;
251fcf5ef2aSThomas Huth     } else if (x > 65535) {
252fcf5ef2aSThomas Huth         return 65535;
253fcf5ef2aSThomas Huth     } else {
254fcf5ef2aSThomas Huth         return x;
255fcf5ef2aSThomas Huth     }
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth static inline int satsb(int x)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     if (x < -128) {
261fcf5ef2aSThomas Huth         return -128;
262fcf5ef2aSThomas Huth     } else if (x > 127) {
263fcf5ef2aSThomas Huth         return 127;
264fcf5ef2aSThomas Huth     } else {
265fcf5ef2aSThomas Huth         return x;
266fcf5ef2aSThomas Huth     }
267fcf5ef2aSThomas Huth }
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth static inline int satsw(int x)
270fcf5ef2aSThomas Huth {
271fcf5ef2aSThomas Huth     if (x < -32768) {
272fcf5ef2aSThomas Huth         return -32768;
273fcf5ef2aSThomas Huth     } else if (x > 32767) {
274fcf5ef2aSThomas Huth         return 32767;
275fcf5ef2aSThomas Huth     } else {
276fcf5ef2aSThomas Huth         return x;
277fcf5ef2aSThomas Huth     }
278fcf5ef2aSThomas Huth }
279fcf5ef2aSThomas Huth 
280fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
281fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
282fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
283fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
284fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
285fcf5ef2aSThomas Huth 
286fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
287fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
288fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
289fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
290fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
291fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
292fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
293fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
294fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
295fcf5ef2aSThomas Huth 
296fcf5ef2aSThomas Huth #define FAND(a, b) ((a) & (b))
297fcf5ef2aSThomas Huth #define FANDN(a, b) ((~(a)) & (b))
298fcf5ef2aSThomas Huth #define FOR(a, b) ((a) | (b))
299fcf5ef2aSThomas Huth #define FXOR(a, b) ((a) ^ (b))
300fcf5ef2aSThomas Huth 
301fcf5ef2aSThomas Huth #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
302fcf5ef2aSThomas Huth #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
303fcf5ef2aSThomas Huth #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
304fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
305fcf5ef2aSThomas Huth 
306fcf5ef2aSThomas Huth #define FMULLW(a, b) ((a) * (b))
307fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
308fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
309fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
310fcf5ef2aSThomas Huth 
311fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
312fcf5ef2aSThomas Huth #endif
313fcf5ef2aSThomas Huth 
314fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddb, FADD)
315fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddw, FADD)
316fcf5ef2aSThomas Huth SSE_HELPER_L(helper_paddl, FADD)
317fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_paddq, FADD)
318fcf5ef2aSThomas Huth 
319fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubb, FSUB)
320fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubw, FSUB)
321fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psubl, FSUB)
322fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_psubq, FSUB)
323fcf5ef2aSThomas Huth 
324fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddusb, FADDUB)
325fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddsb, FADDSB)
326fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubusb, FSUBUB)
327fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubsb, FSUBSB)
328fcf5ef2aSThomas Huth 
329fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddusw, FADDUW)
330fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddsw, FADDSW)
331fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubusw, FSUBUW)
332fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubsw, FSUBSW)
333fcf5ef2aSThomas Huth 
334fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminub, FMINUB)
335fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxub, FMAXUB)
336fcf5ef2aSThomas Huth 
337fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminsw, FMINSW)
338fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxsw, FMAXSW)
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pand, FAND)
341fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pandn, FANDN)
342fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_por, FOR)
343fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pxor, FXOR)
344fcf5ef2aSThomas Huth 
345fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
346fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
347fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
348fcf5ef2aSThomas Huth 
349fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
350fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
351fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmullw, FMULLW)
354fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
355fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
356fcf5ef2aSThomas Huth 
357*f05f9789SPaolo Bonzini #if SHIFT == 0
358*f05f9789SPaolo Bonzini void glue(helper_pmulhrw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
359*f05f9789SPaolo Bonzini {
360*f05f9789SPaolo Bonzini     d->W(0) = FMULHRW(d->W(0), s->W(0));
361*f05f9789SPaolo Bonzini     d->W(1) = FMULHRW(d->W(1), s->W(1));
362*f05f9789SPaolo Bonzini     d->W(2) = FMULHRW(d->W(2), s->W(2));
363*f05f9789SPaolo Bonzini     d->W(3) = FMULHRW(d->W(3), s->W(3));
364*f05f9789SPaolo Bonzini }
365*f05f9789SPaolo Bonzini #endif
366*f05f9789SPaolo Bonzini 
367fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
368fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
369fcf5ef2aSThomas Huth 
370*f05f9789SPaolo Bonzini void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
371fcf5ef2aSThomas Huth {
372e894bae8SPaul Brook     int i;
373e894bae8SPaul Brook 
374e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
375e894bae8SPaul Brook         d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2);
376e894bae8SPaul Brook     }
377fcf5ef2aSThomas Huth }
378fcf5ef2aSThomas Huth 
379*f05f9789SPaolo Bonzini void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
380fcf5ef2aSThomas Huth {
381fcf5ef2aSThomas Huth     int i;
382fcf5ef2aSThomas Huth 
383fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
384e894bae8SPaul Brook         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) +
385e894bae8SPaul Brook             (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1);
386fcf5ef2aSThomas Huth     }
387fcf5ef2aSThomas Huth }
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth #if SHIFT == 0
390fcf5ef2aSThomas Huth static inline int abs1(int a)
391fcf5ef2aSThomas Huth {
392fcf5ef2aSThomas Huth     if (a < 0) {
393fcf5ef2aSThomas Huth         return -a;
394fcf5ef2aSThomas Huth     } else {
395fcf5ef2aSThomas Huth         return a;
396fcf5ef2aSThomas Huth     }
397fcf5ef2aSThomas Huth }
398fcf5ef2aSThomas Huth #endif
399*f05f9789SPaolo Bonzini void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
400fcf5ef2aSThomas Huth {
401e894bae8SPaul Brook     int i;
402fcf5ef2aSThomas Huth 
403e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
404e894bae8SPaul Brook         unsigned int val = 0;
405e894bae8SPaul Brook         val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0));
406e894bae8SPaul Brook         val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1));
407e894bae8SPaul Brook         val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2));
408e894bae8SPaul Brook         val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3));
409e894bae8SPaul Brook         val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4));
410e894bae8SPaul Brook         val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5));
411e894bae8SPaul Brook         val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6));
412e894bae8SPaul Brook         val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7));
413e894bae8SPaul Brook         d->Q(i) = val;
414e894bae8SPaul Brook     }
415fcf5ef2aSThomas Huth }
416fcf5ef2aSThomas Huth 
417fd17264aSPaul Brook #if SHIFT < 2
418fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
419fcf5ef2aSThomas Huth                                   target_ulong a0)
420fcf5ef2aSThomas Huth {
421fcf5ef2aSThomas Huth     int i;
422fcf5ef2aSThomas Huth 
423fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
424fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
425fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
426fcf5ef2aSThomas Huth         }
427fcf5ef2aSThomas Huth     }
428fcf5ef2aSThomas Huth }
429fd17264aSPaul Brook #endif
430fcf5ef2aSThomas Huth 
431fcf5ef2aSThomas Huth void glue(helper_movl_mm_T0, SUFFIX)(Reg *d, uint32_t val)
432fcf5ef2aSThomas Huth {
433e894bae8SPaul Brook     int i;
434e894bae8SPaul Brook 
435fcf5ef2aSThomas Huth     d->L(0) = val;
436fcf5ef2aSThomas Huth     d->L(1) = 0;
437e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
438e894bae8SPaul Brook         d->Q(i) = 0;
439e894bae8SPaul Brook     }
440fcf5ef2aSThomas Huth }
441fcf5ef2aSThomas Huth 
442fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
443fcf5ef2aSThomas Huth void glue(helper_movq_mm_T0, SUFFIX)(Reg *d, uint64_t val)
444fcf5ef2aSThomas Huth {
445e894bae8SPaul Brook     int i;
446e894bae8SPaul Brook 
447fcf5ef2aSThomas Huth     d->Q(0) = val;
448e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
449e894bae8SPaul Brook         d->Q(i) = 0;
450e894bae8SPaul Brook     }
451fcf5ef2aSThomas Huth }
452fcf5ef2aSThomas Huth #endif
453fcf5ef2aSThomas Huth 
454d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
455d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
456d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
457d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
458d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
459d45b0de6SPaul Brook     d->F(offset) = r0;                      \
460d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
461d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
462d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
463d45b0de6SPaul Brook     } while (0)
464d45b0de6SPaul Brook 
465fcf5ef2aSThomas Huth #if SHIFT == 0
466fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
467fcf5ef2aSThomas Huth {
468d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
469fcf5ef2aSThomas Huth 
470d45b0de6SPaul Brook     SHUFFLE4(W, s, s, 0);
471fcf5ef2aSThomas Huth }
472fcf5ef2aSThomas Huth #else
473*f05f9789SPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
474fcf5ef2aSThomas Huth {
475d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
476d45b0de6SPaul Brook     int i;
477fcf5ef2aSThomas Huth 
478d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
479d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
480d45b0de6SPaul Brook     }
481fcf5ef2aSThomas Huth }
482fcf5ef2aSThomas Huth 
483*f05f9789SPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
484fcf5ef2aSThomas Huth {
485d45b0de6SPaul Brook     uint64_t r0, r1;
486d45b0de6SPaul Brook     int i;
487fcf5ef2aSThomas Huth 
488d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
489d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
490d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
491d45b0de6SPaul Brook         d->Q(i) = r0;
492d45b0de6SPaul Brook         d->Q(i + 1) = r1;
493d45b0de6SPaul Brook         order >>= 2;
494d45b0de6SPaul Brook     }
495fcf5ef2aSThomas Huth }
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
498fcf5ef2aSThomas Huth {
499d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
500d45b0de6SPaul Brook     int i;
501fcf5ef2aSThomas Huth 
502d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
503d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
504d45b0de6SPaul Brook     }
505fcf5ef2aSThomas Huth }
506fcf5ef2aSThomas Huth 
507fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
508fcf5ef2aSThomas Huth {
509d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
510d45b0de6SPaul Brook     int i, j;
511fcf5ef2aSThomas Huth 
512d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
513d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
514d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
515d45b0de6SPaul Brook     }
516fcf5ef2aSThomas Huth }
517fcf5ef2aSThomas Huth 
518fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
519fcf5ef2aSThomas Huth {
520d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
521d45b0de6SPaul Brook     int i, j;
522fcf5ef2aSThomas Huth 
523d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
524d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
525d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
526d45b0de6SPaul Brook     }
527fcf5ef2aSThomas Huth }
528fcf5ef2aSThomas Huth #endif
529fcf5ef2aSThomas Huth 
5303403cafeSPaul Brook #if SHIFT >= 1
531fcf5ef2aSThomas Huth /* FPU ops */
532fcf5ef2aSThomas Huth /* XXX: not accurate */
533fcf5ef2aSThomas Huth 
5343403cafeSPaul Brook #define SSE_HELPER_P(name, F)                                           \
5353403cafeSPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
536*f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
537fcf5ef2aSThomas Huth     {                                                                   \
5383403cafeSPaul Brook         int i;                                                          \
5393403cafeSPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
5403403cafeSPaul Brook             d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i));              \
541fcf5ef2aSThomas Huth         }                                                               \
5423403cafeSPaul Brook     }                                                                   \
5433403cafeSPaul Brook                                                                         \
5443403cafeSPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
545*f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
5463403cafeSPaul Brook     {                                                                   \
5473403cafeSPaul Brook         int i;                                                          \
5483403cafeSPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
5493403cafeSPaul Brook             d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i));              \
5503403cafeSPaul Brook         }                                                               \
5513403cafeSPaul Brook     }
5523403cafeSPaul Brook 
5533403cafeSPaul Brook #if SHIFT == 1
5543403cafeSPaul Brook 
5553403cafeSPaul Brook #define SSE_HELPER_S(name, F)                                           \
5563403cafeSPaul Brook     SSE_HELPER_P(name, F)                                               \
557fcf5ef2aSThomas Huth                                                                         \
558*f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
559fcf5ef2aSThomas Huth     {                                                                   \
5603403cafeSPaul Brook         d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0));                  \
561fcf5ef2aSThomas Huth     }                                                                   \
562fcf5ef2aSThomas Huth                                                                         \
563*f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
564fcf5ef2aSThomas Huth     {                                                                   \
5653403cafeSPaul Brook         d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0));                  \
566fcf5ef2aSThomas Huth     }
567fcf5ef2aSThomas Huth 
5683403cafeSPaul Brook #else
5693403cafeSPaul Brook 
5703403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F)
5713403cafeSPaul Brook 
5723403cafeSPaul Brook #endif
5733403cafeSPaul Brook 
574fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
575fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
576fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
577fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
578fcf5ef2aSThomas Huth 
579fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
580fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
581fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
582fcf5ef2aSThomas Huth  */
583fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
584fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
585fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
586fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
587fcf5ef2aSThomas Huth 
588fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
589fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
590fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
591fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
592fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
593fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
594fcf5ef2aSThomas Huth 
5953403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
5963403cafeSPaul Brook {
5973403cafeSPaul Brook     int i;
5983403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
5993403cafeSPaul Brook         d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status);
6003403cafeSPaul Brook     }
6013403cafeSPaul Brook }
6023403cafeSPaul Brook 
6033403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6043403cafeSPaul Brook {
6053403cafeSPaul Brook     int i;
6063403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
6073403cafeSPaul Brook         d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status);
6083403cafeSPaul Brook     }
6093403cafeSPaul Brook }
6103403cafeSPaul Brook 
6113403cafeSPaul Brook #if SHIFT == 1
6123403cafeSPaul Brook void helper_sqrtss(CPUX86State *env, Reg *d, Reg *s)
6133403cafeSPaul Brook {
6143403cafeSPaul Brook     d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status);
6153403cafeSPaul Brook }
6163403cafeSPaul Brook 
6173403cafeSPaul Brook void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *s)
6183403cafeSPaul Brook {
6193403cafeSPaul Brook     d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status);
6203403cafeSPaul Brook }
6213403cafeSPaul Brook #endif
622fcf5ef2aSThomas Huth 
623fcf5ef2aSThomas Huth /* float to float conversions */
624ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
625fcf5ef2aSThomas Huth {
626fd17264aSPaul Brook     int i;
627fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
628fd17264aSPaul Brook         d->ZMM_D(i) = float32_to_float64(s->ZMM_S(i), &env->sse_status);
629fd17264aSPaul Brook     }
630fcf5ef2aSThomas Huth }
631fcf5ef2aSThomas Huth 
632ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
633fcf5ef2aSThomas Huth {
634fd17264aSPaul Brook     int i;
635fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
636fd17264aSPaul Brook          d->ZMM_S(i) = float64_to_float32(s->ZMM_D(i), &env->sse_status);
637fd17264aSPaul Brook     }
638fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
639fd17264aSPaul Brook          d->Q(i) = 0;
640fd17264aSPaul Brook     }
641fcf5ef2aSThomas Huth }
642fcf5ef2aSThomas Huth 
643fd17264aSPaul Brook #if SHIFT == 1
644fcf5ef2aSThomas Huth void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *s)
645fcf5ef2aSThomas Huth {
646fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
647fcf5ef2aSThomas Huth }
648fcf5ef2aSThomas Huth 
649fcf5ef2aSThomas Huth void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *s)
650fcf5ef2aSThomas Huth {
651fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
652fcf5ef2aSThomas Huth }
653fd17264aSPaul Brook #endif
654fcf5ef2aSThomas Huth 
655fcf5ef2aSThomas Huth /* integer to float */
656ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
657fcf5ef2aSThomas Huth {
658fd17264aSPaul Brook     int i;
659fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
660fd17264aSPaul Brook         d->ZMM_S(i) = int32_to_float32(s->ZMM_L(i), &env->sse_status);
661fd17264aSPaul Brook     }
662fcf5ef2aSThomas Huth }
663fcf5ef2aSThomas Huth 
664ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
665fcf5ef2aSThomas Huth {
666fd17264aSPaul Brook     int i;
667fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
668fd17264aSPaul Brook         int32_t l = s->ZMM_L(i);
669fd17264aSPaul Brook         d->ZMM_D(i) = int32_to_float64(l, &env->sse_status);
670fd17264aSPaul Brook     }
671fcf5ef2aSThomas Huth }
672fcf5ef2aSThomas Huth 
673fd17264aSPaul Brook #if SHIFT == 1
674fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
675fcf5ef2aSThomas Huth {
676fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
677fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
678fcf5ef2aSThomas Huth }
679fcf5ef2aSThomas Huth 
680fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
681fcf5ef2aSThomas Huth {
682fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
683fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
684fcf5ef2aSThomas Huth }
685fcf5ef2aSThomas Huth 
686fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
687fcf5ef2aSThomas Huth {
688fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
689fcf5ef2aSThomas Huth }
690fcf5ef2aSThomas Huth 
691fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
692fcf5ef2aSThomas Huth {
693fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
694fcf5ef2aSThomas Huth }
695fcf5ef2aSThomas Huth 
696fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
697fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
698fcf5ef2aSThomas Huth {
699fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
700fcf5ef2aSThomas Huth }
701fcf5ef2aSThomas Huth 
702fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
703fcf5ef2aSThomas Huth {
704fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
705fcf5ef2aSThomas Huth }
706fcf5ef2aSThomas Huth #endif
707fcf5ef2aSThomas Huth 
708fd17264aSPaul Brook #endif
709fd17264aSPaul Brook 
710fcf5ef2aSThomas Huth /* float to integer */
7111e8a98b5SPeter Maydell 
712fd17264aSPaul Brook #if SHIFT == 1
7131e8a98b5SPeter Maydell /*
7141e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7151e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7161e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7171e8a98b5SPeter Maydell  */
7181e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7191e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7201e8a98b5SPeter Maydell     {                                                                   \
7211e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7221e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7231e8a98b5SPeter Maydell                                                                         \
7241e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7251e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7261e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7271e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7281e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7291e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7301e8a98b5SPeter Maydell         }                                                               \
7311e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7321e8a98b5SPeter Maydell         return r;                                                       \
7331e8a98b5SPeter Maydell     }
7341e8a98b5SPeter Maydell 
7351e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7361e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7371e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7381e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7391e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7401e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7411e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7421e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
743fd17264aSPaul Brook #endif
7441e8a98b5SPeter Maydell 
745ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
746fcf5ef2aSThomas Huth {
747fd17264aSPaul Brook     int i;
748fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
749fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32(s->ZMM_S(i), &env->sse_status);
750fd17264aSPaul Brook     }
751fcf5ef2aSThomas Huth }
752fcf5ef2aSThomas Huth 
753ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
754fcf5ef2aSThomas Huth {
755fd17264aSPaul Brook     int i;
756fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
757fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32(s->ZMM_D(i), &env->sse_status);
758fd17264aSPaul Brook     }
759fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
760fd17264aSPaul Brook          d->Q(i) = 0;
761fd17264aSPaul Brook     }
762fcf5ef2aSThomas Huth }
763fcf5ef2aSThomas Huth 
764fd17264aSPaul Brook #if SHIFT == 1
765fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
766fcf5ef2aSThomas Huth {
7671e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7681e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
769fcf5ef2aSThomas Huth }
770fcf5ef2aSThomas Huth 
771fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
772fcf5ef2aSThomas Huth {
7731e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7741e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
775fcf5ef2aSThomas Huth }
776fcf5ef2aSThomas Huth 
777fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
778fcf5ef2aSThomas Huth {
7791e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
780fcf5ef2aSThomas Huth }
781fcf5ef2aSThomas Huth 
782fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
783fcf5ef2aSThomas Huth {
7841e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
785fcf5ef2aSThomas Huth }
786fcf5ef2aSThomas Huth 
787fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
788fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
789fcf5ef2aSThomas Huth {
7901e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
791fcf5ef2aSThomas Huth }
792fcf5ef2aSThomas Huth 
793fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
794fcf5ef2aSThomas Huth {
7951e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
796fcf5ef2aSThomas Huth }
797fcf5ef2aSThomas Huth #endif
798fd17264aSPaul Brook #endif
799fcf5ef2aSThomas Huth 
800fcf5ef2aSThomas Huth /* float to integer truncated */
801ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
802fcf5ef2aSThomas Huth {
803fd17264aSPaul Brook     int i;
804fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
805fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32_round_to_zero(s->ZMM_S(i),
806fd17264aSPaul Brook                                                          &env->sse_status);
807fd17264aSPaul Brook     }
808fcf5ef2aSThomas Huth }
809fcf5ef2aSThomas Huth 
810ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
811fcf5ef2aSThomas Huth {
812fd17264aSPaul Brook     int i;
813fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
814fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32_round_to_zero(s->ZMM_D(i),
815fd17264aSPaul Brook                                                          &env->sse_status);
816fd17264aSPaul Brook     }
817fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
818fd17264aSPaul Brook          d->Q(i) = 0;
819fd17264aSPaul Brook     }
820fcf5ef2aSThomas Huth }
821fcf5ef2aSThomas Huth 
822fd17264aSPaul Brook #if SHIFT == 1
823fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
824fcf5ef2aSThomas Huth {
8251e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8261e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
827fcf5ef2aSThomas Huth }
828fcf5ef2aSThomas Huth 
829fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
830fcf5ef2aSThomas Huth {
8311e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8321e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
833fcf5ef2aSThomas Huth }
834fcf5ef2aSThomas Huth 
835fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
836fcf5ef2aSThomas Huth {
8371e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
838fcf5ef2aSThomas Huth }
839fcf5ef2aSThomas Huth 
840fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
841fcf5ef2aSThomas Huth {
8421e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
843fcf5ef2aSThomas Huth }
844fcf5ef2aSThomas Huth 
845fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
846fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
847fcf5ef2aSThomas Huth {
8481e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
849fcf5ef2aSThomas Huth }
850fcf5ef2aSThomas Huth 
851fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
852fcf5ef2aSThomas Huth {
8531e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
854fcf5ef2aSThomas Huth }
855fcf5ef2aSThomas Huth #endif
856fd17264aSPaul Brook #endif
857fcf5ef2aSThomas Huth 
858ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
859fcf5ef2aSThomas Huth {
860418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8613403cafeSPaul Brook     int i;
8623403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8633403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one,
8643403cafeSPaul Brook                                   float32_sqrt(s->ZMM_S(i), &env->sse_status),
865fcf5ef2aSThomas Huth                                   &env->sse_status);
8663403cafeSPaul Brook     }
867418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
868fcf5ef2aSThomas Huth }
869fcf5ef2aSThomas Huth 
870fd17264aSPaul Brook #if SHIFT == 1
871fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
872fcf5ef2aSThomas Huth {
873418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
874fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
875fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
876fcf5ef2aSThomas Huth                               &env->sse_status);
877418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
878fcf5ef2aSThomas Huth }
879fd17264aSPaul Brook #endif
880fcf5ef2aSThomas Huth 
881ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
882fcf5ef2aSThomas Huth {
883418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8843403cafeSPaul Brook     int i;
8853403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8863403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status);
8873403cafeSPaul Brook     }
888418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
889fcf5ef2aSThomas Huth }
890fcf5ef2aSThomas Huth 
891fd17264aSPaul Brook #if SHIFT == 1
892fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
893fcf5ef2aSThomas Huth {
894418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
895fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
896418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
897fcf5ef2aSThomas Huth }
898fd17264aSPaul Brook #endif
899fcf5ef2aSThomas Huth 
900fd17264aSPaul Brook #if SHIFT == 1
901fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
902fcf5ef2aSThomas Huth {
903fcf5ef2aSThomas Huth     uint64_t mask;
904fcf5ef2aSThomas Huth 
905fcf5ef2aSThomas Huth     if (len == 0) {
906fcf5ef2aSThomas Huth         mask = ~0LL;
907fcf5ef2aSThomas Huth     } else {
908fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
909fcf5ef2aSThomas Huth     }
910fcf5ef2aSThomas Huth     return (src >> shift) & mask;
911fcf5ef2aSThomas Huth }
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
914fcf5ef2aSThomas Huth {
915034668c3SPaolo Bonzini     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1) & 63, s->ZMM_B(0) & 63);
916fcf5ef2aSThomas Huth }
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
919fcf5ef2aSThomas Huth {
920fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
921fcf5ef2aSThomas Huth }
922fcf5ef2aSThomas Huth 
923ca4b1b43SPaolo Bonzini static inline uint64_t helper_insertq(uint64_t dest, uint64_t src, int shift, int len)
924fcf5ef2aSThomas Huth {
925fcf5ef2aSThomas Huth     uint64_t mask;
926fcf5ef2aSThomas Huth 
927fcf5ef2aSThomas Huth     if (len == 0) {
928fcf5ef2aSThomas Huth         mask = ~0ULL;
929fcf5ef2aSThomas Huth     } else {
930fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
931fcf5ef2aSThomas Huth     }
932ca4b1b43SPaolo Bonzini     return (dest & ~(mask << shift)) | ((src & mask) << shift);
933fcf5ef2aSThomas Huth }
934fcf5ef2aSThomas Huth 
935fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
936fcf5ef2aSThomas Huth {
937ca4b1b43SPaolo 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);
938fcf5ef2aSThomas Huth }
939fcf5ef2aSThomas Huth 
940ca4b1b43SPaolo Bonzini void helper_insertq_i(CPUX86State *env, ZMMReg *d, ZMMReg *s, int index, int length)
941fcf5ef2aSThomas Huth {
942ca4b1b43SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), s->ZMM_Q(0), index, length);
943fcf5ef2aSThomas Huth }
944fd17264aSPaul Brook #endif
945fcf5ef2aSThomas Huth 
9466567ffb4SPaul Brook #define SSE_HELPER_HPS(name, F)  \
947*f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9486567ffb4SPaul Brook {                                                                 \
9496567ffb4SPaul Brook     float32 r[2 << SHIFT];                                        \
9506567ffb4SPaul Brook     int i, j, k;                                                  \
9516567ffb4SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {            \
9526567ffb4SPaul Brook         for (i = j = 0; j < 4; i++, j += 2) {                     \
9536567ffb4SPaul Brook             r[i + k] = F(v->ZMM_S(j + k), v->ZMM_S(j + k + 1), &env->sse_status); \
9546567ffb4SPaul Brook         }                                                         \
9556567ffb4SPaul Brook         for (j = 0; j < 4; i++, j += 2) {                         \
9566567ffb4SPaul Brook             r[i + k] = F(s->ZMM_S(j + k), s->ZMM_S(j + k + 1), &env->sse_status); \
9576567ffb4SPaul Brook         }                                                         \
9586567ffb4SPaul Brook     }                                                             \
9596567ffb4SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                            \
9606567ffb4SPaul Brook         d->ZMM_S(i) = r[i];                                       \
9616567ffb4SPaul Brook     }                                                             \
962fcf5ef2aSThomas Huth }
963fcf5ef2aSThomas Huth 
9646567ffb4SPaul Brook SSE_HELPER_HPS(haddps, float32_add)
9656567ffb4SPaul Brook SSE_HELPER_HPS(hsubps, float32_sub)
966fcf5ef2aSThomas Huth 
9676567ffb4SPaul Brook #define SSE_HELPER_HPD(name, F)  \
968*f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9696567ffb4SPaul Brook {                                                                 \
9706567ffb4SPaul Brook     float64 r[1 << SHIFT];                                        \
9716567ffb4SPaul Brook     int i, j, k;                                                  \
9726567ffb4SPaul Brook     for (k = 0; k < 1 << SHIFT; k += LANE_WIDTH / 8) {            \
9736567ffb4SPaul Brook         for (i = j = 0; j < 2; i++, j += 2) {                     \
9746567ffb4SPaul Brook             r[i + k] = F(v->ZMM_D(j + k), v->ZMM_D(j + k + 1), &env->sse_status); \
9756567ffb4SPaul Brook         }                                                         \
9766567ffb4SPaul Brook         for (j = 0; j < 2; i++, j += 2) {                         \
9776567ffb4SPaul Brook             r[i + k] = F(s->ZMM_D(j + k), s->ZMM_D(j + k + 1), &env->sse_status); \
9786567ffb4SPaul Brook         }                                                         \
9796567ffb4SPaul Brook     }                                                             \
9806567ffb4SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {                            \
9816567ffb4SPaul Brook         d->ZMM_D(i) = r[i];                                       \
9826567ffb4SPaul Brook     }                                                             \
983fcf5ef2aSThomas Huth }
984fcf5ef2aSThomas Huth 
9856567ffb4SPaul Brook SSE_HELPER_HPD(haddpd, float64_add)
9866567ffb4SPaul Brook SSE_HELPER_HPD(hsubpd, float64_sub)
987fcf5ef2aSThomas Huth 
988*f05f9789SPaolo Bonzini void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
989fcf5ef2aSThomas Huth {
9903403cafeSPaul Brook     int i;
9913403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 2) {
9923403cafeSPaul Brook         d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
9933403cafeSPaul Brook         d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
9943403cafeSPaul Brook     }
995fcf5ef2aSThomas Huth }
996fcf5ef2aSThomas Huth 
997*f05f9789SPaolo Bonzini void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
998fcf5ef2aSThomas Huth {
9993403cafeSPaul Brook     int i;
10003403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
10013403cafeSPaul Brook         d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status);
10023403cafeSPaul Brook         d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status);
10033403cafeSPaul Brook     }
1004fcf5ef2aSThomas Huth }
1005fcf5ef2aSThomas Huth 
1006cbf4ad54SPaul Brook #define SSE_HELPER_CMP_P(name, F, C)                                    \
1007cbf4ad54SPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
1008*f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
1009fcf5ef2aSThomas Huth     {                                                                   \
1010cbf4ad54SPaul Brook         int i;                                                          \
1011cbf4ad54SPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
1012cbf4ad54SPaul Brook             d->ZMM_L(i) = C(F(32, v->ZMM_S(i), s->ZMM_S(i))) ? -1 : 0;  \
1013cbf4ad54SPaul Brook         }                                                               \
1014fcf5ef2aSThomas Huth     }                                                                   \
1015fcf5ef2aSThomas Huth                                                                         \
1016cbf4ad54SPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
1017*f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
1018cbf4ad54SPaul Brook     {                                                                   \
1019cbf4ad54SPaul Brook         int i;                                                          \
1020cbf4ad54SPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
1021cbf4ad54SPaul Brook             d->ZMM_Q(i) = C(F(64, v->ZMM_D(i), s->ZMM_D(i))) ? -1 : 0;  \
1022cbf4ad54SPaul Brook         }                                                               \
1023cbf4ad54SPaul Brook     }
1024cbf4ad54SPaul Brook 
1025cbf4ad54SPaul Brook #if SHIFT == 1
1026cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C)                                          \
1027cbf4ad54SPaul Brook     SSE_HELPER_CMP_P(name, F, C)                                            \
1028*f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1029fcf5ef2aSThomas Huth     {                                                                       \
1030cbf4ad54SPaul Brook         d->ZMM_L(0) = C(F(32, v->ZMM_S(0), s->ZMM_S(0))) ? -1 : 0;          \
1031fcf5ef2aSThomas Huth     }                                                                       \
1032fcf5ef2aSThomas Huth                                                                             \
1033*f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1034fcf5ef2aSThomas Huth     {                                                                       \
1035cbf4ad54SPaul Brook         d->ZMM_Q(0) = C(F(64, v->ZMM_D(0), s->ZMM_D(0))) ? -1 : 0;          \
1036fcf5ef2aSThomas Huth     }
1037fcf5ef2aSThomas Huth 
1038cbf4ad54SPaul Brook #define FPU_EQ(x) (x == float_relation_equal)
1039cbf4ad54SPaul Brook #define FPU_LT(x) (x == float_relation_less)
1040cbf4ad54SPaul Brook #define FPU_LE(x) (x <= float_relation_equal)
1041cbf4ad54SPaul Brook #define FPU_UNORD(x) (x == float_relation_unordered)
1042fcf5ef2aSThomas Huth 
1043cbf4ad54SPaul Brook #define FPU_CMPQ(size, a, b) \
1044cbf4ad54SPaul Brook     float ## size ## _compare_quiet(a, b, &env->sse_status)
1045cbf4ad54SPaul Brook #define FPU_CMPS(size, a, b) \
1046cbf4ad54SPaul Brook     float ## size ## _compare(a, b, &env->sse_status)
1047cbf4ad54SPaul Brook 
1048cbf4ad54SPaul Brook #else
1049cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C) SSE_HELPER_CMP_P(name, F, C)
1050cbf4ad54SPaul Brook #endif
1051cbf4ad54SPaul Brook 
1052cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpeq, FPU_CMPQ, FPU_EQ)
1053cbf4ad54SPaul Brook SSE_HELPER_CMP(cmplt, FPU_CMPS, FPU_LT)
1054cbf4ad54SPaul Brook SSE_HELPER_CMP(cmple, FPU_CMPS, FPU_LE)
1055cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpunord, FPU_CMPQ,  FPU_UNORD)
1056cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpneq, FPU_CMPQ, !FPU_EQ)
1057cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnlt, FPU_CMPS, !FPU_LT)
1058cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnle, FPU_CMPS, !FPU_LE)
1059cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpord, FPU_CMPQ, !FPU_UNORD)
1060cbf4ad54SPaul Brook 
1061cbf4ad54SPaul Brook #undef SSE_HELPER_CMP
1062fcf5ef2aSThomas Huth 
1063fd17264aSPaul Brook #if SHIFT == 1
1064fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1065fcf5ef2aSThomas Huth 
1066fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1067fcf5ef2aSThomas Huth {
106871bfd65cSRichard Henderson     FloatRelation ret;
1069fcf5ef2aSThomas Huth     float32 s0, s1;
1070fcf5ef2aSThomas Huth 
1071fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1072fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1073fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1074fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1075fcf5ef2aSThomas Huth }
1076fcf5ef2aSThomas Huth 
1077fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1078fcf5ef2aSThomas Huth {
107971bfd65cSRichard Henderson     FloatRelation ret;
1080fcf5ef2aSThomas Huth     float32 s0, s1;
1081fcf5ef2aSThomas Huth 
1082fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1083fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1084fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1085fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1086fcf5ef2aSThomas Huth }
1087fcf5ef2aSThomas Huth 
1088fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1089fcf5ef2aSThomas Huth {
109071bfd65cSRichard Henderson     FloatRelation ret;
1091fcf5ef2aSThomas Huth     float64 d0, d1;
1092fcf5ef2aSThomas Huth 
1093fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1094fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1095fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1096fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1097fcf5ef2aSThomas Huth }
1098fcf5ef2aSThomas Huth 
1099fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1100fcf5ef2aSThomas Huth {
110171bfd65cSRichard Henderson     FloatRelation ret;
1102fcf5ef2aSThomas Huth     float64 d0, d1;
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1105fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1106fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1107fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1108fcf5ef2aSThomas Huth }
1109fd17264aSPaul Brook #endif
1110fcf5ef2aSThomas Huth 
1111ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1112fcf5ef2aSThomas Huth {
1113fd17264aSPaul Brook     uint32_t mask;
1114fd17264aSPaul Brook     int i;
1115fcf5ef2aSThomas Huth 
1116fd17264aSPaul Brook     mask = 0;
1117fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1118fd17264aSPaul Brook         mask |= (s->ZMM_L(i) >> (31 - i)) & (1 << i);
1119fd17264aSPaul Brook     }
1120fd17264aSPaul Brook     return mask;
1121fcf5ef2aSThomas Huth }
1122fcf5ef2aSThomas Huth 
1123ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1124fcf5ef2aSThomas Huth {
1125fd17264aSPaul Brook     uint32_t mask;
1126fd17264aSPaul Brook     int i;
1127fcf5ef2aSThomas Huth 
1128fd17264aSPaul Brook     mask = 0;
1129fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1130fd17264aSPaul Brook         mask |= (s->ZMM_Q(i) >> (63 - i)) & (1 << i);
1131fd17264aSPaul Brook     }
1132fd17264aSPaul Brook     return mask;
1133fcf5ef2aSThomas Huth }
1134fcf5ef2aSThomas Huth 
1135fcf5ef2aSThomas Huth #endif
1136fcf5ef2aSThomas Huth 
1137fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1138fcf5ef2aSThomas Huth {
1139fcf5ef2aSThomas Huth     uint32_t val;
1140e894bae8SPaul Brook     int i;
1141fcf5ef2aSThomas Huth 
1142fcf5ef2aSThomas Huth     val = 0;
1143e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
1144e894bae8SPaul Brook         uint8_t byte = 0;
1145e894bae8SPaul Brook         byte |= (s->B(8 * i + 0) >> 7);
1146e894bae8SPaul Brook         byte |= (s->B(8 * i + 1) >> 6) & 0x02;
1147e894bae8SPaul Brook         byte |= (s->B(8 * i + 2) >> 5) & 0x04;
1148e894bae8SPaul Brook         byte |= (s->B(8 * i + 3) >> 4) & 0x08;
1149e894bae8SPaul Brook         byte |= (s->B(8 * i + 4) >> 3) & 0x10;
1150e894bae8SPaul Brook         byte |= (s->B(8 * i + 5) >> 2) & 0x20;
1151e894bae8SPaul Brook         byte |= (s->B(8 * i + 6) >> 1) & 0x40;
1152e894bae8SPaul Brook         byte |= (s->B(8 * i + 7)) & 0x80;
1153e894bae8SPaul Brook         val |= byte << (8 * i);
1154e894bae8SPaul Brook     }
1155fcf5ef2aSThomas Huth     return val;
1156fcf5ef2aSThomas Huth }
1157fcf5ef2aSThomas Huth 
1158d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1159d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1160*f05f9789SPaolo Bonzini         Reg *d, Reg *v, Reg *s)                               \
1161d45b0de6SPaul Brook {                                                             \
1162d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1163d45b0de6SPaul Brook     int j, k;                                                 \
1164d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1165d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1166d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1167d45b0de6SPaul Brook         }                                                     \
1168d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1169d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1170d45b0de6SPaul Brook         }                                                     \
1171d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1172d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1173d45b0de6SPaul Brook         }                                                     \
1174d45b0de6SPaul Brook     }                                                         \
1175fcf5ef2aSThomas Huth }
1176fcf5ef2aSThomas Huth 
1177d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1178d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1179fcf5ef2aSThomas Huth 
1180*f05f9789SPaolo Bonzini void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1181fcf5ef2aSThomas Huth {
1182d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1183d45b0de6SPaul Brook     int j, k;
1184fcf5ef2aSThomas Huth 
1185d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1186d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1187d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1188d45b0de6SPaul Brook         }
1189d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1190d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1191d45b0de6SPaul Brook         }
1192d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1193d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1194d45b0de6SPaul Brook         }
1195d45b0de6SPaul Brook     }
1196fcf5ef2aSThomas Huth }
1197fcf5ef2aSThomas Huth 
1198fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1199fcf5ef2aSThomas Huth                                                                         \
1200fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1201*f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1202fcf5ef2aSThomas Huth     {                                                                   \
1203d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1204d45b0de6SPaul Brook         int j, i;                                                       \
1205fcf5ef2aSThomas Huth                                                                         \
1206d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1207d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1208d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1209d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1210d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1211d45b0de6SPaul Brook             }                                                           \
1212d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1213d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1214d45b0de6SPaul Brook             }                                                           \
1215d45b0de6SPaul Brook         }                                                               \
1216fcf5ef2aSThomas Huth     }                                                                   \
1217fcf5ef2aSThomas Huth                                                                         \
1218fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1219*f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1220fcf5ef2aSThomas Huth     {                                                                   \
1221d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1222d45b0de6SPaul Brook         int j, i;                                                       \
1223fcf5ef2aSThomas Huth                                                                         \
1224d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1225d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1226d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1227d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1228d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1229d45b0de6SPaul Brook             }                                                           \
1230d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1231d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1232d45b0de6SPaul Brook             }                                                           \
1233d45b0de6SPaul Brook         }                                                               \
1234fcf5ef2aSThomas Huth     }                                                                   \
1235fcf5ef2aSThomas Huth                                                                         \
1236fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1237*f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1238fcf5ef2aSThomas Huth     {                                                                   \
1239d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1240d45b0de6SPaul Brook         int j, i;                                                       \
1241fcf5ef2aSThomas Huth                                                                         \
1242d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1243d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1244d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1245d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1246d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1247d45b0de6SPaul Brook             }                                                           \
1248d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1249d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1250d45b0de6SPaul Brook             }                                                           \
1251d45b0de6SPaul Brook         }                                                               \
1252fcf5ef2aSThomas Huth     }                                                                   \
1253fcf5ef2aSThomas Huth                                                                         \
1254fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1255d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1256*f05f9789SPaolo Bonzini                         CPUX86State *env, Reg *d, Reg *v, Reg *s)       \
1257fcf5ef2aSThomas Huth              {                                                          \
1258d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1259d45b0de6SPaul Brook                  int i;                                                 \
1260fcf5ef2aSThomas Huth                                                                         \
1261d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1262d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1263d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1264d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1265d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1266d45b0de6SPaul Brook                  }                                                      \
1267fcf5ef2aSThomas Huth              }                                                          \
1268fcf5ef2aSThomas Huth                                                                         )
1269fcf5ef2aSThomas Huth 
1270fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1271fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1272fcf5ef2aSThomas Huth 
1273d45b0de6SPaul Brook #undef PACK_WIDTH
1274d45b0de6SPaul Brook #undef PACK_HELPER_B
1275d45b0de6SPaul Brook #undef UNPCK_OP
1276d45b0de6SPaul Brook 
1277d45b0de6SPaul Brook 
1278fcf5ef2aSThomas Huth /* 3DNow! float ops */
1279fcf5ef2aSThomas Huth #if SHIFT == 0
1280fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1281fcf5ef2aSThomas Huth {
1282fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1283fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1284fcf5ef2aSThomas Huth }
1285fcf5ef2aSThomas Huth 
1286fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1287fcf5ef2aSThomas Huth {
1288fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1289fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1290fcf5ef2aSThomas Huth }
1291fcf5ef2aSThomas Huth 
1292fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1293fcf5ef2aSThomas Huth {
1294fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1295fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1296fcf5ef2aSThomas Huth }
1297fcf5ef2aSThomas Huth 
1298fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1299fcf5ef2aSThomas Huth {
1300fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1301fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1302fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1303fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1304fcf5ef2aSThomas Huth }
1305fcf5ef2aSThomas Huth 
1306fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1307fcf5ef2aSThomas Huth {
130825bdec79SPaolo Bonzini     float32 r;
1309fcf5ef2aSThomas Huth 
131025bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
131125bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
131225bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1313fcf5ef2aSThomas Huth }
1314fcf5ef2aSThomas Huth 
1315fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1316fcf5ef2aSThomas Huth {
1317fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1318fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1319fcf5ef2aSThomas Huth }
1320fcf5ef2aSThomas Huth 
1321fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1322fcf5ef2aSThomas Huth {
1323fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1324fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1325fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1326fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1327fcf5ef2aSThomas Huth }
1328fcf5ef2aSThomas Huth 
1329fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1330fcf5ef2aSThomas Huth {
1331fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1332fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1333fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1334fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1335fcf5ef2aSThomas Huth }
1336fcf5ef2aSThomas Huth 
1337fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1338fcf5ef2aSThomas Huth {
1339fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1340fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1341fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1342fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1343fcf5ef2aSThomas Huth }
1344fcf5ef2aSThomas Huth 
1345fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1346fcf5ef2aSThomas Huth {
1347fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1348fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1349fcf5ef2aSThomas Huth     }
1350fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1351fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1352fcf5ef2aSThomas Huth     }
1353fcf5ef2aSThomas Huth }
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1356fcf5ef2aSThomas Huth {
1357fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1358fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1359fcf5ef2aSThomas Huth     }
1360fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1361fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1362fcf5ef2aSThomas Huth     }
1363fcf5ef2aSThomas Huth }
1364fcf5ef2aSThomas Huth 
1365fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1366fcf5ef2aSThomas Huth {
1367fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1368fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1369fcf5ef2aSThomas Huth }
1370fcf5ef2aSThomas Huth 
1371fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1372fcf5ef2aSThomas Huth {
137325bdec79SPaolo Bonzini     float32 r;
1374fcf5ef2aSThomas Huth 
137525bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
137625bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
137725bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1378fcf5ef2aSThomas Huth }
1379fcf5ef2aSThomas Huth 
1380fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1381fcf5ef2aSThomas Huth {
138225bdec79SPaolo Bonzini     float32 r;
1383fcf5ef2aSThomas Huth 
138425bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
138525bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
138625bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1387fcf5ef2aSThomas Huth }
1388fcf5ef2aSThomas Huth 
1389fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1390fcf5ef2aSThomas Huth {
1391fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1392fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1393fcf5ef2aSThomas Huth }
1394fcf5ef2aSThomas Huth 
1395fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1396fcf5ef2aSThomas Huth {
1397fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1398fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1399fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1400fcf5ef2aSThomas Huth                               &env->mmx_status);
1401fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1402fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1403fcf5ef2aSThomas Huth }
1404fcf5ef2aSThomas Huth 
1405fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1406fcf5ef2aSThomas Huth {
1407fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1408fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1412fcf5ef2aSThomas Huth {
1413fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1414fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1415fcf5ef2aSThomas Huth }
1416fcf5ef2aSThomas Huth 
1417fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1418fcf5ef2aSThomas Huth {
141925bdec79SPaolo Bonzini     uint32_t r;
1420fcf5ef2aSThomas Huth 
142125bdec79SPaolo Bonzini     r = s->MMX_L(0);
142225bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
142325bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1424fcf5ef2aSThomas Huth }
1425fcf5ef2aSThomas Huth #endif
1426fcf5ef2aSThomas Huth 
1427fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1428*f05f9789SPaolo Bonzini void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1429fcf5ef2aSThomas Huth {
1430fcf5ef2aSThomas Huth     int i;
1431d45b0de6SPaul Brook #if SHIFT == 0
1432d45b0de6SPaul Brook     uint8_t r[8];
1433fcf5ef2aSThomas Huth 
1434d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1435d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1436fcf5ef2aSThomas Huth     }
1437d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1438d45b0de6SPaul Brook         d->B(i) = r[i];
1439fcf5ef2aSThomas Huth     }
1440d45b0de6SPaul Brook #else
1441d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1442fcf5ef2aSThomas Huth 
1443d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1444d45b0de6SPaul Brook         int j = i & ~0xf;
1445d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1446fcf5ef2aSThomas Huth     }
1447d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1448d45b0de6SPaul Brook         d->B(i) = r[i];
1449fcf5ef2aSThomas Huth     }
1450fcf5ef2aSThomas Huth #endif
1451fcf5ef2aSThomas Huth }
1452fcf5ef2aSThomas Huth 
1453d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1454*f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1455d45b0de6SPaul Brook {                                                          \
1456d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1457d45b0de6SPaul Brook     int i, j, k;                                           \
1458d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1459d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1460d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1461d45b0de6SPaul Brook         }                                                  \
1462d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1463d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1464d45b0de6SPaul Brook         }                                                  \
1465d45b0de6SPaul Brook     }                                                      \
1466d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1467d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1468d45b0de6SPaul Brook     }                                                      \
1469fcf5ef2aSThomas Huth }
1470fcf5ef2aSThomas Huth 
1471d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1472*f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1473d45b0de6SPaul Brook {                                                          \
1474d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1475d45b0de6SPaul Brook     int i, j, k;                                           \
1476d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1477d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1478d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1479d45b0de6SPaul Brook         }                                                  \
1480d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1481d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1482d45b0de6SPaul Brook         }                                                  \
1483d45b0de6SPaul Brook     }                                                      \
1484d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1485d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1486d45b0de6SPaul Brook     }                                                      \
1487fcf5ef2aSThomas Huth }
1488fcf5ef2aSThomas Huth 
1489d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1490d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1491d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1492d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1493d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1494d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
149575046ad7SPaolo Bonzini 
1496d45b0de6SPaul Brook #undef SSE_HELPER_HW
1497d45b0de6SPaul Brook #undef SSE_HELPER_HL
1498d45b0de6SPaul Brook 
1499*f05f9789SPaolo Bonzini void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1500d45b0de6SPaul Brook {
1501d45b0de6SPaul Brook     int i;
1502d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1503d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1504d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1505d45b0de6SPaul Brook     }
1506fcf5ef2aSThomas Huth }
1507fcf5ef2aSThomas Huth 
1508ee04a3c8SPaul Brook #define FABSB(x) (x > INT8_MAX  ? -(int8_t)x : x)
1509ee04a3c8SPaul Brook #define FABSW(x) (x > INT16_MAX ? -(int16_t)x : x)
1510ee04a3c8SPaul Brook #define FABSL(x) (x > INT32_MAX ? -(int32_t)x : x)
1511ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsb, B, 8 << SHIFT, FABSB)
1512ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsw, W, 4 << SHIFT, FABSW)
1513ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsd, L, 2 << SHIFT, FABSL)
1514fcf5ef2aSThomas Huth 
1515fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1516fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1517fcf5ef2aSThomas Huth 
1518fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1519fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1520fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1521fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1522fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1523fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1524fcf5ef2aSThomas Huth 
1525*f05f9789SPaolo Bonzini void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1526fcf5ef2aSThomas Huth                                   int32_t shift)
1527fcf5ef2aSThomas Huth {
1528d45b0de6SPaul Brook     int i;
1529fcf5ef2aSThomas Huth 
1530fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1531d45b0de6SPaul Brook     if (shift >= (SHIFT ? 32 : 16)) {
1532d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1533d45b0de6SPaul Brook             d->Q(i) = 0;
1534d45b0de6SPaul Brook         }
1535fcf5ef2aSThomas Huth     } else {
1536fcf5ef2aSThomas Huth         shift <<= 3;
1537fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1538fcf5ef2aSThomas Huth #if SHIFT == 0
1539d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1540d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1541fcf5ef2aSThomas Huth #else
1542d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1543d45b0de6SPaul Brook             uint64_t r0, r1;
1544d45b0de6SPaul Brook 
1545d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1546d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1547d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1548d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1549d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1550d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1551d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1552d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1553d45b0de6SPaul Brook             d->Q(i) = r0;
1554d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1555d45b0de6SPaul Brook         }
1556fcf5ef2aSThomas Huth #endif
1557fcf5ef2aSThomas Huth #undef SHR
1558fcf5ef2aSThomas Huth     }
1559fcf5ef2aSThomas Huth }
1560fcf5ef2aSThomas Huth 
15610e29cea5SPaul Brook #if SHIFT >= 1
1562fcf5ef2aSThomas Huth 
1563fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1564*f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
1565*f05f9789SPaolo Bonzini                             Reg *m)                                     \
1566fcf5ef2aSThomas Huth     {                                                                   \
15670e29cea5SPaul Brook         int i;                                                          \
15680e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
15690e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), m->elem(i));         \
1570fcf5ef2aSThomas Huth         }                                                               \
1571fcf5ef2aSThomas Huth     }
1572fcf5ef2aSThomas Huth 
1573fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1574*f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
15750e29cea5SPaul Brook                             uint32_t imm)                               \
1576fcf5ef2aSThomas Huth     {                                                                   \
15770e29cea5SPaul Brook         int i;                                                          \
15780e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
15790e29cea5SPaul Brook             int j = i & 7;                                              \
15800e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), (imm >> j) & 1);     \
1581fcf5ef2aSThomas Huth         }                                                               \
1582fcf5ef2aSThomas Huth     }
1583fcf5ef2aSThomas Huth 
1584fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
15850e29cea5SPaul Brook #define FBLENDVB(v, s, m) ((m & 0x80) ? s : v)
15860e29cea5SPaul Brook #define FBLENDVPS(v, s, m) ((m & 0x80000000) ? s : v)
15870e29cea5SPaul Brook #define FBLENDVPD(v, s, m) ((m & 0x8000000000000000LL) ? s : v)
15880e29cea5SPaul Brook SSE_HELPER_V(helper_pblendvb, B, 8 << SHIFT, FBLENDVB)
15890e29cea5SPaul Brook SSE_HELPER_V(helper_blendvps, L, 2 << SHIFT, FBLENDVPS)
15900e29cea5SPaul Brook SSE_HELPER_V(helper_blendvpd, Q, 1 << SHIFT, FBLENDVPD)
1591fcf5ef2aSThomas Huth 
1592fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1593fcf5ef2aSThomas Huth {
1594e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1595e894bae8SPaul Brook     int i;
1596fcf5ef2aSThomas Huth 
1597e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1598e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1599e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1600e894bae8SPaul Brook     }
1601fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1602fcf5ef2aSThomas Huth }
1603fcf5ef2aSThomas Huth 
1604fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1605fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1606fcf5ef2aSThomas Huth     {                                                           \
1607e894bae8SPaul Brook         int n = num;                                            \
1608e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1609e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1610fcf5ef2aSThomas Huth         }                                                       \
1611fcf5ef2aSThomas Huth     }
1612fcf5ef2aSThomas Huth 
1613e894bae8SPaul Brook #if SHIFT > 0
1614e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1615e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1616e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1617e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1618e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1619e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1620e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1621e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1622e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1623e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1624e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1625e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
1626e894bae8SPaul Brook #endif
1627fcf5ef2aSThomas Huth 
1628*f05f9789SPaolo Bonzini void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1629fcf5ef2aSThomas Huth {
1630e894bae8SPaul Brook     int i;
1631e894bae8SPaul Brook 
1632e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1633e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1634e894bae8SPaul Brook     }
1635fcf5ef2aSThomas Huth }
1636fcf5ef2aSThomas Huth 
1637fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1638fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1639fcf5ef2aSThomas Huth 
1640*f05f9789SPaolo Bonzini void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1641fcf5ef2aSThomas Huth {
1642d45b0de6SPaul Brook     uint16_t r[8];
1643d45b0de6SPaul Brook     int i, j, k;
164480e19606SJoseph Myers 
1645d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1646d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1647d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1648d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1649d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1650d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1651d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1652d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1653d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1654d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1655d45b0de6SPaul Brook             d->W(i + k) = r[k];
1656d45b0de6SPaul Brook         }
1657d45b0de6SPaul Brook     }
1658fcf5ef2aSThomas Huth }
1659fcf5ef2aSThomas Huth 
1660fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1661fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1662fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1663fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1664fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1665fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1666fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1667fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1668fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1669fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1670fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1671fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1672fcf5ef2aSThomas Huth 
1673fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1674fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1675fcf5ef2aSThomas Huth 
1676fd17264aSPaul Brook #if SHIFT == 1
1677fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1678fcf5ef2aSThomas Huth {
1679fcf5ef2aSThomas Huth     int idx = 0;
1680fcf5ef2aSThomas Huth 
1681fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1682fcf5ef2aSThomas Huth         idx = 1;
1683fcf5ef2aSThomas Huth     }
1684fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1685fcf5ef2aSThomas Huth         idx = 2;
1686fcf5ef2aSThomas Huth     }
1687fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1688fcf5ef2aSThomas Huth         idx = 3;
1689fcf5ef2aSThomas Huth     }
1690fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1691fcf5ef2aSThomas Huth         idx = 4;
1692fcf5ef2aSThomas Huth     }
1693fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1694fcf5ef2aSThomas Huth         idx = 5;
1695fcf5ef2aSThomas Huth     }
1696fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1697fcf5ef2aSThomas Huth         idx = 6;
1698fcf5ef2aSThomas Huth     }
1699fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1700fcf5ef2aSThomas Huth         idx = 7;
1701fcf5ef2aSThomas Huth     }
1702fcf5ef2aSThomas Huth 
1703fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1704aa406feaSJoseph Myers     d->W(1) = idx;
1705aa406feaSJoseph Myers     d->L(1) = 0;
1706aa406feaSJoseph Myers     d->Q(1) = 0;
1707fcf5ef2aSThomas Huth }
1708fd17264aSPaul Brook #endif
1709fcf5ef2aSThomas Huth 
1710fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1711fcf5ef2aSThomas Huth                                   uint32_t mode)
1712fcf5ef2aSThomas Huth {
1713418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1714fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1715fd17264aSPaul Brook     int i;
1716fcf5ef2aSThomas Huth 
1717fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1718fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1719fcf5ef2aSThomas Huth         switch (mode & 3) {
1720fcf5ef2aSThomas Huth         case 0:
1721fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1722fcf5ef2aSThomas Huth             break;
1723fcf5ef2aSThomas Huth         case 1:
1724fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1725fcf5ef2aSThomas Huth             break;
1726fcf5ef2aSThomas Huth         case 2:
1727fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1728fcf5ef2aSThomas Huth             break;
1729fcf5ef2aSThomas Huth         case 3:
1730fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1731fcf5ef2aSThomas Huth             break;
1732fcf5ef2aSThomas Huth         }
1733fcf5ef2aSThomas Huth     }
1734fcf5ef2aSThomas Huth 
1735fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1736fd17264aSPaul Brook         d->ZMM_S(i) = float32_round_to_int(s->ZMM_S(i), &env->sse_status);
1737fd17264aSPaul Brook     }
1738fcf5ef2aSThomas Huth 
1739418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1740fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1741fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1742fcf5ef2aSThomas Huth                                   &env->sse_status);
1743fcf5ef2aSThomas Huth     }
1744fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1745fcf5ef2aSThomas Huth }
1746fcf5ef2aSThomas Huth 
1747fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1748fcf5ef2aSThomas Huth                                   uint32_t mode)
1749fcf5ef2aSThomas Huth {
1750418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1751fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1752fd17264aSPaul Brook     int i;
1753fcf5ef2aSThomas Huth 
1754fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1755fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1756fcf5ef2aSThomas Huth         switch (mode & 3) {
1757fcf5ef2aSThomas Huth         case 0:
1758fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1759fcf5ef2aSThomas Huth             break;
1760fcf5ef2aSThomas Huth         case 1:
1761fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1762fcf5ef2aSThomas Huth             break;
1763fcf5ef2aSThomas Huth         case 2:
1764fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1765fcf5ef2aSThomas Huth             break;
1766fcf5ef2aSThomas Huth         case 3:
1767fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1768fcf5ef2aSThomas Huth             break;
1769fcf5ef2aSThomas Huth         }
1770fcf5ef2aSThomas Huth     }
1771fcf5ef2aSThomas Huth 
1772fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1773fd17264aSPaul Brook         d->ZMM_D(i) = float64_round_to_int(s->ZMM_D(i), &env->sse_status);
1774fd17264aSPaul Brook     }
1775fcf5ef2aSThomas Huth 
1776418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1777fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1778fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1779fcf5ef2aSThomas Huth                                   &env->sse_status);
1780fcf5ef2aSThomas Huth     }
1781fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1782fcf5ef2aSThomas Huth }
1783fcf5ef2aSThomas Huth 
1784fd17264aSPaul Brook #if SHIFT == 1
1785fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1786fcf5ef2aSThomas Huth                                   uint32_t mode)
1787fcf5ef2aSThomas Huth {
1788418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1789fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1790fcf5ef2aSThomas Huth 
1791fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1792fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1793fcf5ef2aSThomas Huth         switch (mode & 3) {
1794fcf5ef2aSThomas Huth         case 0:
1795fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1796fcf5ef2aSThomas Huth             break;
1797fcf5ef2aSThomas Huth         case 1:
1798fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1799fcf5ef2aSThomas Huth             break;
1800fcf5ef2aSThomas Huth         case 2:
1801fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1802fcf5ef2aSThomas Huth             break;
1803fcf5ef2aSThomas Huth         case 3:
1804fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1805fcf5ef2aSThomas Huth             break;
1806fcf5ef2aSThomas Huth         }
1807fcf5ef2aSThomas Huth     }
1808fcf5ef2aSThomas Huth 
1809fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1810fcf5ef2aSThomas Huth 
1811418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1812fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1813fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1814fcf5ef2aSThomas Huth                                   &env->sse_status);
1815fcf5ef2aSThomas Huth     }
1816fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1817fcf5ef2aSThomas Huth }
1818fcf5ef2aSThomas Huth 
1819fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1820fcf5ef2aSThomas Huth                                   uint32_t mode)
1821fcf5ef2aSThomas Huth {
1822418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1823fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1824fcf5ef2aSThomas Huth 
1825fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1826fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1827fcf5ef2aSThomas Huth         switch (mode & 3) {
1828fcf5ef2aSThomas Huth         case 0:
1829fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1830fcf5ef2aSThomas Huth             break;
1831fcf5ef2aSThomas Huth         case 1:
1832fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1833fcf5ef2aSThomas Huth             break;
1834fcf5ef2aSThomas Huth         case 2:
1835fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1836fcf5ef2aSThomas Huth             break;
1837fcf5ef2aSThomas Huth         case 3:
1838fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1839fcf5ef2aSThomas Huth             break;
1840fcf5ef2aSThomas Huth         }
1841fcf5ef2aSThomas Huth     }
1842fcf5ef2aSThomas Huth 
1843fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1844fcf5ef2aSThomas Huth 
1845418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1846fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1847fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1848fcf5ef2aSThomas Huth                                   &env->sse_status);
1849fcf5ef2aSThomas Huth     }
1850fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1851fcf5ef2aSThomas Huth }
1852fd17264aSPaul Brook #endif
1853fcf5ef2aSThomas Huth 
18540e29cea5SPaul Brook #define FBLENDP(v, s, m) (m ? s : v)
18550e29cea5SPaul Brook SSE_HELPER_I(helper_blendps, L, 2 << SHIFT, FBLENDP)
18560e29cea5SPaul Brook SSE_HELPER_I(helper_blendpd, Q, 1 << SHIFT, FBLENDP)
18570e29cea5SPaul Brook SSE_HELPER_I(helper_pblendw, W, 4 << SHIFT, FBLENDP)
1858fcf5ef2aSThomas Huth 
1859*f05f9789SPaolo Bonzini void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
18606f218d6eSPaul Brook                                uint32_t mask)
1861fcf5ef2aSThomas Huth {
1862bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
18636f218d6eSPaul Brook     int i;
1864fcf5ef2aSThomas Huth 
18656f218d6eSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
1866bf30ad8cSPaolo Bonzini         /*
1867bf30ad8cSPaolo Bonzini          * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1868bf30ad8cSPaolo Bonzini          * to correctly round the intermediate results
1869bf30ad8cSPaolo Bonzini          */
1870fcf5ef2aSThomas Huth         if (mask & (1 << 4)) {
18716f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
1872bf30ad8cSPaolo Bonzini         } else {
1873bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1874fcf5ef2aSThomas Huth         }
1875fcf5ef2aSThomas Huth         if (mask & (1 << 5)) {
18766f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
1877bf30ad8cSPaolo Bonzini         } else {
1878bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1879fcf5ef2aSThomas Huth         }
1880bf30ad8cSPaolo Bonzini         temp2 = float32_add(prod1, prod2, &env->sse_status);
1881fcf5ef2aSThomas Huth         if (mask & (1 << 6)) {
18826f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i+2), s->ZMM_S(i+2), &env->sse_status);
1883bf30ad8cSPaolo Bonzini         } else {
1884bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1885fcf5ef2aSThomas Huth         }
1886fcf5ef2aSThomas Huth         if (mask & (1 << 7)) {
18876f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+3), s->ZMM_S(i+3), &env->sse_status);
1888bf30ad8cSPaolo Bonzini         } else {
1889bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1890fcf5ef2aSThomas Huth         }
1891bf30ad8cSPaolo Bonzini         temp3 = float32_add(prod1, prod2, &env->sse_status);
1892bf30ad8cSPaolo Bonzini         temp4 = float32_add(temp2, temp3, &env->sse_status);
1893bf30ad8cSPaolo Bonzini 
18946f218d6eSPaul Brook         d->ZMM_S(i) = (mask & (1 << 0)) ? temp4 : float32_zero;
18956f218d6eSPaul Brook         d->ZMM_S(i+1) = (mask & (1 << 1)) ? temp4 : float32_zero;
18966f218d6eSPaul Brook         d->ZMM_S(i+2) = (mask & (1 << 2)) ? temp4 : float32_zero;
18976f218d6eSPaul Brook         d->ZMM_S(i+3) = (mask & (1 << 3)) ? temp4 : float32_zero;
18986f218d6eSPaul Brook     }
1899fcf5ef2aSThomas Huth }
1900fcf5ef2aSThomas Huth 
19016f218d6eSPaul Brook #if SHIFT == 1
19026f218d6eSPaul Brook /* Oddly, there is no ymm version of dppd */
19036f218d6eSPaul Brook void glue(helper_dppd, SUFFIX)(CPUX86State *env,
1904*f05f9789SPaolo Bonzini                                Reg *d, Reg *v, Reg *s, uint32_t mask)
1905fcf5ef2aSThomas Huth {
1906bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1907fcf5ef2aSThomas Huth 
1908fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
19096f218d6eSPaul Brook         prod1 = float64_mul(v->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1910bf30ad8cSPaolo Bonzini     } else {
1911bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1912fcf5ef2aSThomas Huth     }
1913fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
19146f218d6eSPaul Brook         prod2 = float64_mul(v->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1915bf30ad8cSPaolo Bonzini     } else {
1916bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1917fcf5ef2aSThomas Huth     }
1918bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1919bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1920bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1921fcf5ef2aSThomas Huth }
19226f218d6eSPaul Brook #endif
1923fcf5ef2aSThomas Huth 
1924*f05f9789SPaolo Bonzini void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1925fcf5ef2aSThomas Huth                                   uint32_t offset)
1926fcf5ef2aSThomas Huth {
1927d45b0de6SPaul Brook     int i, j;
1928d45b0de6SPaul Brook     uint16_t r[8];
1929fcf5ef2aSThomas Huth 
1930d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; ) {
1931d45b0de6SPaul Brook         int s0 = (j * 2) + ((offset & 3) << 2);
1932d45b0de6SPaul Brook         int d0 = (j * 2) + ((offset & 4) << 0);
1933d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, d0++) {
1934d45b0de6SPaul Brook             r[i] = 0;
1935d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0));
1936d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1));
1937d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2));
1938d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3));
1939fcf5ef2aSThomas Huth         }
1940d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, j++) {
1941d45b0de6SPaul Brook             d->W(j) = r[i];
1942d45b0de6SPaul Brook         }
1943d45b0de6SPaul Brook         offset >>= 3;
1944d45b0de6SPaul Brook     }
1945fcf5ef2aSThomas Huth }
1946fcf5ef2aSThomas Huth 
1947fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1948fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
1949fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1950fcf5ef2aSThomas Huth 
1951fd17264aSPaul Brook #if SHIFT == 1
1952fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1953fcf5ef2aSThomas Huth {
1954d1da229fSPaul Brook     target_long val, limit;
1955fcf5ef2aSThomas Huth 
1956fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1957fcf5ef2aSThomas Huth     if (ctrl >> 8) {
1958d1da229fSPaul Brook         val = (target_long)env->regs[reg];
1959fcf5ef2aSThomas Huth     } else {
1960d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
1961fcf5ef2aSThomas Huth     }
1962fcf5ef2aSThomas Huth     if (ctrl & 1) {
1963d1da229fSPaul Brook         limit = 8;
1964fcf5ef2aSThomas Huth     } else {
1965d1da229fSPaul Brook         limit = 16;
1966fcf5ef2aSThomas Huth     }
1967d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
1968d1da229fSPaul Brook         return limit;
1969fcf5ef2aSThomas Huth     }
1970d1da229fSPaul Brook     return abs1(val);
1971fcf5ef2aSThomas Huth }
1972fcf5ef2aSThomas Huth 
1973fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
1974fcf5ef2aSThomas Huth {
1975fcf5ef2aSThomas Huth     int val = 0;
1976fcf5ef2aSThomas Huth 
1977fcf5ef2aSThomas Huth     if (ctrl & 1) {
1978fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
1979fcf5ef2aSThomas Huth             val++;
1980fcf5ef2aSThomas Huth         }
1981fcf5ef2aSThomas Huth     } else {
1982fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
1983fcf5ef2aSThomas Huth             val++;
1984fcf5ef2aSThomas Huth         }
1985fcf5ef2aSThomas Huth     }
1986fcf5ef2aSThomas Huth 
1987fcf5ef2aSThomas Huth     return val;
1988fcf5ef2aSThomas Huth }
1989fcf5ef2aSThomas Huth 
1990fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
1991fcf5ef2aSThomas Huth {
1992fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
1993fcf5ef2aSThomas Huth     case 0:
1994fcf5ef2aSThomas Huth         return r->B(i);
1995fcf5ef2aSThomas Huth     case 1:
1996fcf5ef2aSThomas Huth         return r->W(i);
1997fcf5ef2aSThomas Huth     case 2:
1998fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
1999fcf5ef2aSThomas Huth     case 3:
2000fcf5ef2aSThomas Huth     default:
2001fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
2002fcf5ef2aSThomas Huth     }
2003fcf5ef2aSThomas Huth }
2004fcf5ef2aSThomas Huth 
2005fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
2006fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
2007fcf5ef2aSThomas Huth {
2008fcf5ef2aSThomas Huth     unsigned int res = 0;
2009fcf5ef2aSThomas Huth     int v;
2010fcf5ef2aSThomas Huth     int j, i;
2011fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
2012fcf5ef2aSThomas Huth 
2013fcf5ef2aSThomas Huth     valids--;
2014fcf5ef2aSThomas Huth     validd--;
2015fcf5ef2aSThomas Huth 
2016fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2017fcf5ef2aSThomas Huth 
2018fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2019fcf5ef2aSThomas Huth     case 0:
2020fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2021fcf5ef2aSThomas Huth             res <<= 1;
2022fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2023fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2024fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2025fcf5ef2aSThomas Huth             }
2026fcf5ef2aSThomas Huth         }
2027fcf5ef2aSThomas Huth         break;
2028fcf5ef2aSThomas Huth     case 1:
2029fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2030fcf5ef2aSThomas Huth             res <<= 1;
2031fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2032fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2033fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2034fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2035fcf5ef2aSThomas Huth             }
2036fcf5ef2aSThomas Huth         }
2037fcf5ef2aSThomas Huth         break;
2038fcf5ef2aSThomas Huth     case 2:
2039fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2040fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2041fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2042fcf5ef2aSThomas Huth             res <<= 1;
2043fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2044fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2045fcf5ef2aSThomas Huth         }
2046fcf5ef2aSThomas Huth         break;
2047fcf5ef2aSThomas Huth     case 3:
2048ae35eea7SJoseph Myers         if (validd == -1) {
2049ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2050ae35eea7SJoseph Myers             break;
2051ae35eea7SJoseph Myers         }
2052bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2053fcf5ef2aSThomas Huth             res <<= 1;
2054fcf5ef2aSThomas Huth             v = 1;
2055bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2056fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2057fcf5ef2aSThomas Huth             }
2058fcf5ef2aSThomas Huth             res |= v;
2059fcf5ef2aSThomas Huth         }
2060fcf5ef2aSThomas Huth         break;
2061fcf5ef2aSThomas Huth     }
2062fcf5ef2aSThomas Huth 
2063fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2064fcf5ef2aSThomas Huth     case 1:
2065fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2066fcf5ef2aSThomas Huth         break;
2067fcf5ef2aSThomas Huth     case 3:
2068fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2069fcf5ef2aSThomas Huth         break;
2070fcf5ef2aSThomas Huth     }
2071fcf5ef2aSThomas Huth 
2072fcf5ef2aSThomas Huth     if (res) {
2073fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2074fcf5ef2aSThomas Huth     }
2075fcf5ef2aSThomas Huth     if (res & 1) {
2076fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2077fcf5ef2aSThomas Huth     }
2078fcf5ef2aSThomas Huth 
2079fcf5ef2aSThomas Huth     return res;
2080fcf5ef2aSThomas Huth }
2081fcf5ef2aSThomas Huth 
2082fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2083fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2084fcf5ef2aSThomas Huth {
2085fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2086fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2087fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2088fcf5ef2aSThomas Huth 
2089fcf5ef2aSThomas Huth     if (res) {
2090fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2091fcf5ef2aSThomas Huth     } else {
2092fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2093fcf5ef2aSThomas Huth     }
2094fcf5ef2aSThomas Huth }
2095fcf5ef2aSThomas Huth 
2096fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2097fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2098fcf5ef2aSThomas Huth {
2099fcf5ef2aSThomas Huth     int i;
2100fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2101fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2102fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2103fcf5ef2aSThomas Huth 
2104fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2105fcf5ef2aSThomas Huth         if (ctrl & 1) {
2106fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2107fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2108fcf5ef2aSThomas Huth             }
2109fcf5ef2aSThomas Huth         } else {
2110fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2111fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2112fcf5ef2aSThomas Huth             }
2113fcf5ef2aSThomas Huth         }
2114fcf5ef2aSThomas Huth     } else {
2115fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2116fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2117fcf5ef2aSThomas Huth     }
2118fcf5ef2aSThomas Huth }
2119fcf5ef2aSThomas Huth 
2120fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2121fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2122fcf5ef2aSThomas Huth {
2123fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2124fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2125fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2126fcf5ef2aSThomas Huth 
2127fcf5ef2aSThomas Huth     if (res) {
2128fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2129fcf5ef2aSThomas Huth     } else {
2130fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2131fcf5ef2aSThomas Huth     }
2132fcf5ef2aSThomas Huth }
2133fcf5ef2aSThomas Huth 
2134fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2135fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2136fcf5ef2aSThomas Huth {
2137fcf5ef2aSThomas Huth     int i;
2138fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2139fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2140fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2141fcf5ef2aSThomas Huth 
2142fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2143fcf5ef2aSThomas Huth         if (ctrl & 1) {
2144fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2145fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2146fcf5ef2aSThomas Huth             }
2147fcf5ef2aSThomas Huth         } else {
2148fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2149fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2150fcf5ef2aSThomas Huth             }
2151fcf5ef2aSThomas Huth         }
2152fcf5ef2aSThomas Huth     } else {
2153fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2154fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2155fcf5ef2aSThomas Huth     }
2156fcf5ef2aSThomas Huth }
2157fcf5ef2aSThomas Huth 
2158fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2159fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2160fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2161fcf5ef2aSThomas Huth {
2162fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2163fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2164fcf5ef2aSThomas Huth 
2165fcf5ef2aSThomas Huth     while (len--) {
2166fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2167fcf5ef2aSThomas Huth     }
2168fcf5ef2aSThomas Huth 
2169fcf5ef2aSThomas Huth     return crc;
2170fcf5ef2aSThomas Huth }
2171fcf5ef2aSThomas Huth 
2172fd17264aSPaul Brook #endif
2173fd17264aSPaul Brook 
21745a09df21SPaul Brook #if SHIFT == 1
21755a09df21SPaul Brook static void clmulq(uint64_t *dest_l, uint64_t *dest_h,
21765a09df21SPaul Brook                           uint64_t a, uint64_t b)
2177fcf5ef2aSThomas Huth {
21785a09df21SPaul Brook     uint64_t al, ah, resh, resl;
2179fcf5ef2aSThomas Huth 
2180fcf5ef2aSThomas Huth     ah = 0;
21815a09df21SPaul Brook     al = a;
2182fcf5ef2aSThomas Huth     resh = resl = 0;
2183fcf5ef2aSThomas Huth 
2184fcf5ef2aSThomas Huth     while (b) {
2185fcf5ef2aSThomas Huth         if (b & 1) {
2186fcf5ef2aSThomas Huth             resl ^= al;
2187fcf5ef2aSThomas Huth             resh ^= ah;
2188fcf5ef2aSThomas Huth         }
2189fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2190fcf5ef2aSThomas Huth         al <<= 1;
2191fcf5ef2aSThomas Huth         b >>= 1;
2192fcf5ef2aSThomas Huth     }
2193fcf5ef2aSThomas Huth 
21945a09df21SPaul Brook     *dest_l = resl;
21955a09df21SPaul Brook     *dest_h = resh;
21965a09df21SPaul Brook }
21975a09df21SPaul Brook #endif
21985a09df21SPaul Brook 
2199*f05f9789SPaolo Bonzini void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
22005a09df21SPaul Brook                                     uint32_t ctrl)
22015a09df21SPaul Brook {
22025a09df21SPaul Brook     uint64_t a, b;
22035a09df21SPaul Brook     int i;
22045a09df21SPaul Brook 
22055a09df21SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
22065a09df21SPaul Brook         a = v->Q(((ctrl & 1) != 0) + i);
22075a09df21SPaul Brook         b = s->Q(((ctrl & 16) != 0) + i);
22085a09df21SPaul Brook         clmulq(&d->Q(i), &d->Q(i + 1), a, b);
22095a09df21SPaul Brook     }
2210fcf5ef2aSThomas Huth }
2211fcf5ef2aSThomas Huth 
2212*f05f9789SPaolo Bonzini void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2213fcf5ef2aSThomas Huth {
2214fcf5ef2aSThomas Huth     int i;
2215*f05f9789SPaolo Bonzini     Reg st = *v;
2216fcf5ef2aSThomas Huth     Reg rk = *s;
2217fcf5ef2aSThomas Huth 
2218a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2219a64fc269SPaul Brook         int j = i & 3;
2220a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4 * j + 0])] ^
2221a64fc269SPaul Brook                                     AES_Td1[st.B(AES_ishifts[4 * j + 1])] ^
2222a64fc269SPaul Brook                                     AES_Td2[st.B(AES_ishifts[4 * j + 2])] ^
2223a64fc269SPaul Brook                                     AES_Td3[st.B(AES_ishifts[4 * j + 3])]);
2224fcf5ef2aSThomas Huth     }
2225fcf5ef2aSThomas Huth }
2226fcf5ef2aSThomas Huth 
2227*f05f9789SPaolo Bonzini void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2228fcf5ef2aSThomas Huth {
2229fcf5ef2aSThomas Huth     int i;
2230*f05f9789SPaolo Bonzini     Reg st = *v;
2231fcf5ef2aSThomas Huth     Reg rk = *s;
2232fcf5ef2aSThomas Huth 
2233a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2234a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i & 15] + (i & ~15))]);
2235fcf5ef2aSThomas Huth     }
2236fcf5ef2aSThomas Huth }
2237fcf5ef2aSThomas Huth 
2238*f05f9789SPaolo Bonzini void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2239fcf5ef2aSThomas Huth {
2240fcf5ef2aSThomas Huth     int i;
2241*f05f9789SPaolo Bonzini     Reg st = *v;
2242fcf5ef2aSThomas Huth     Reg rk = *s;
2243fcf5ef2aSThomas Huth 
2244a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2245a64fc269SPaul Brook         int j = i & 3;
2246a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4 * j + 0])] ^
2247a64fc269SPaul Brook                                     AES_Te1[st.B(AES_shifts[4 * j + 1])] ^
2248a64fc269SPaul Brook                                     AES_Te2[st.B(AES_shifts[4 * j + 2])] ^
2249a64fc269SPaul Brook                                     AES_Te3[st.B(AES_shifts[4 * j + 3])]);
2250fcf5ef2aSThomas Huth     }
2251fcf5ef2aSThomas Huth }
2252fcf5ef2aSThomas Huth 
2253*f05f9789SPaolo Bonzini void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2254fcf5ef2aSThomas Huth {
2255fcf5ef2aSThomas Huth     int i;
2256*f05f9789SPaolo Bonzini     Reg st = *v;
2257fcf5ef2aSThomas Huth     Reg rk = *s;
2258fcf5ef2aSThomas Huth 
2259a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2260a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]);
2261a64fc269SPaul Brook     }
2262fcf5ef2aSThomas Huth }
2263fcf5ef2aSThomas Huth 
2264a64fc269SPaul Brook #if SHIFT == 1
2265fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2266fcf5ef2aSThomas Huth {
2267fcf5ef2aSThomas Huth     int i;
2268fcf5ef2aSThomas Huth     Reg tmp = *s;
2269fcf5ef2aSThomas Huth 
2270fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2271fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4 * i + 0)][0] ^
2272fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 1)][1] ^
2273fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 2)][2] ^
2274fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 3)][3]);
2275fcf5ef2aSThomas Huth     }
2276fcf5ef2aSThomas Huth }
2277fcf5ef2aSThomas Huth 
2278fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2279fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2280fcf5ef2aSThomas Huth {
2281fcf5ef2aSThomas Huth     int i;
2282fcf5ef2aSThomas Huth     Reg tmp = *s;
2283fcf5ef2aSThomas Huth 
2284fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2285fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2286fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2287fcf5ef2aSThomas Huth     }
2288fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2289fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2290fcf5ef2aSThomas Huth }
2291fcf5ef2aSThomas Huth #endif
2292a64fc269SPaul Brook #endif
2293fcf5ef2aSThomas Huth 
22943403cafeSPaul Brook #undef SSE_HELPER_S
22953403cafeSPaul Brook 
2296fcf5ef2aSThomas Huth #undef SHIFT
2297fcf5ef2aSThomas Huth #undef XMM_ONLY
2298fcf5ef2aSThomas Huth #undef Reg
2299fcf5ef2aSThomas Huth #undef B
2300fcf5ef2aSThomas Huth #undef W
2301fcf5ef2aSThomas Huth #undef L
2302fcf5ef2aSThomas Huth #undef Q
2303fcf5ef2aSThomas Huth #undef SUFFIX
2304