xref: /openbmc/qemu/target/i386/ops_sse.h (revision 034668c329bb3e257a1f259571bd462938522e7a)
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 
5118592d2eSPaul Brook void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
5218592d2eSPaul Brook {
5318592d2eSPaul Brook     Reg *s = d;
5418592d2eSPaul Brook     int shift;
5518592d2eSPaul Brook     if (c->Q(0) > 15) {
5618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
5718592d2eSPaul Brook             d->Q(i) = 0;
5818592d2eSPaul Brook         }
59fcf5ef2aSThomas Huth     } else {
6018592d2eSPaul Brook         shift = c->B(0);
6118592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
6218592d2eSPaul Brook             d->W(i) = FPSRL(s->W(i), shift);
6318592d2eSPaul Brook         }
64fcf5ef2aSThomas Huth     }
65fcf5ef2aSThomas Huth }
66fcf5ef2aSThomas Huth 
6718592d2eSPaul Brook void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
68fcf5ef2aSThomas Huth {
6918592d2eSPaul Brook     Reg *s = d;
70fcf5ef2aSThomas Huth     int shift;
7118592d2eSPaul Brook     if (c->Q(0) > 15) {
7218592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
7318592d2eSPaul Brook             d->Q(i) = 0;
7418592d2eSPaul Brook         }
7518592d2eSPaul Brook     } else {
7618592d2eSPaul Brook         shift = c->B(0);
7718592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
7818592d2eSPaul Brook             d->W(i) = FPSLL(s->W(i), shift);
7918592d2eSPaul Brook         }
8018592d2eSPaul Brook     }
8118592d2eSPaul Brook }
82fcf5ef2aSThomas Huth 
8318592d2eSPaul Brook void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
8418592d2eSPaul Brook {
8518592d2eSPaul Brook     Reg *s = d;
8618592d2eSPaul Brook     int shift;
8718592d2eSPaul Brook     if (c->Q(0) > 15) {
88fcf5ef2aSThomas Huth         shift = 15;
89fcf5ef2aSThomas Huth     } else {
9018592d2eSPaul Brook         shift = c->B(0);
91fcf5ef2aSThomas Huth     }
9218592d2eSPaul Brook     for (int i = 0; i < 4 << SHIFT; i++) {
9318592d2eSPaul Brook         d->W(i) = FPSRAW(s->W(i), shift);
9418592d2eSPaul Brook     }
95fcf5ef2aSThomas Huth }
96fcf5ef2aSThomas Huth 
9718592d2eSPaul Brook void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
98fcf5ef2aSThomas Huth {
9918592d2eSPaul Brook     Reg *s = d;
100fcf5ef2aSThomas Huth     int shift;
10118592d2eSPaul Brook     if (c->Q(0) > 31) {
10218592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
10318592d2eSPaul Brook             d->Q(i) = 0;
10418592d2eSPaul Brook         }
105fcf5ef2aSThomas Huth     } else {
10618592d2eSPaul Brook         shift = c->B(0);
10718592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
10818592d2eSPaul Brook             d->L(i) = FPSRL(s->L(i), shift);
10918592d2eSPaul Brook         }
110fcf5ef2aSThomas Huth     }
111fcf5ef2aSThomas Huth }
112fcf5ef2aSThomas Huth 
11318592d2eSPaul Brook void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
114fcf5ef2aSThomas Huth {
11518592d2eSPaul Brook     Reg *s = d;
116fcf5ef2aSThomas Huth     int shift;
11718592d2eSPaul Brook     if (c->Q(0) > 31) {
11818592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
11918592d2eSPaul Brook             d->Q(i) = 0;
12018592d2eSPaul Brook         }
121fcf5ef2aSThomas Huth     } else {
12218592d2eSPaul Brook         shift = c->B(0);
12318592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
12418592d2eSPaul Brook             d->L(i) = FPSLL(s->L(i), shift);
12518592d2eSPaul Brook         }
126fcf5ef2aSThomas Huth     }
127fcf5ef2aSThomas Huth }
128fcf5ef2aSThomas Huth 
12918592d2eSPaul Brook void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
130fcf5ef2aSThomas Huth {
13118592d2eSPaul Brook     Reg *s = d;
132fcf5ef2aSThomas Huth     int shift;
13318592d2eSPaul Brook     if (c->Q(0) > 31) {
134fcf5ef2aSThomas Huth         shift = 31;
135fcf5ef2aSThomas Huth     } else {
13618592d2eSPaul Brook         shift = c->B(0);
137fcf5ef2aSThomas Huth     }
13818592d2eSPaul Brook     for (int i = 0; i < 2 << SHIFT; i++) {
13918592d2eSPaul Brook         d->L(i) = FPSRAL(s->L(i), shift);
14018592d2eSPaul Brook     }
141fcf5ef2aSThomas Huth }
142fcf5ef2aSThomas Huth 
14318592d2eSPaul Brook void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
144fcf5ef2aSThomas Huth {
14518592d2eSPaul Brook     Reg *s = d;
146fcf5ef2aSThomas Huth     int shift;
14718592d2eSPaul Brook     if (c->Q(0) > 63) {
14818592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14918592d2eSPaul Brook             d->Q(i) = 0;
15018592d2eSPaul Brook         }
151fcf5ef2aSThomas Huth     } else {
15218592d2eSPaul Brook         shift = c->B(0);
15318592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
15418592d2eSPaul Brook             d->Q(i) = FPSRL(s->Q(i), shift);
15518592d2eSPaul Brook         }
156fcf5ef2aSThomas Huth     }
157fcf5ef2aSThomas Huth }
158fcf5ef2aSThomas Huth 
15918592d2eSPaul Brook void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
160fcf5ef2aSThomas Huth {
16118592d2eSPaul Brook     Reg *s = d;
162fcf5ef2aSThomas Huth     int shift;
16318592d2eSPaul Brook     if (c->Q(0) > 63) {
16418592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16518592d2eSPaul Brook             d->Q(i) = 0;
16618592d2eSPaul Brook         }
167fcf5ef2aSThomas Huth     } else {
16818592d2eSPaul Brook         shift = c->B(0);
16918592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
17018592d2eSPaul Brook             d->Q(i) = FPSLL(s->Q(i), shift);
17118592d2eSPaul Brook         }
172fcf5ef2aSThomas Huth     }
173fcf5ef2aSThomas Huth }
174fcf5ef2aSThomas Huth 
17518592d2eSPaul Brook #if SHIFT >= 1
17618592d2eSPaul Brook void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
177fcf5ef2aSThomas Huth {
17818592d2eSPaul Brook     Reg *s = d;
17918592d2eSPaul Brook     int shift, i, j;
180fcf5ef2aSThomas Huth 
18118592d2eSPaul Brook     shift = c->L(0);
182fcf5ef2aSThomas Huth     if (shift > 16) {
183fcf5ef2aSThomas Huth         shift = 16;
184fcf5ef2aSThomas Huth     }
18518592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
186fcf5ef2aSThomas Huth         for (i = 0; i < 16 - shift; i++) {
18718592d2eSPaul Brook             d->B(j + i) = s->B(j + i + shift);
188fcf5ef2aSThomas Huth         }
189fcf5ef2aSThomas Huth         for (i = 16 - shift; i < 16; i++) {
19018592d2eSPaul Brook             d->B(j + i) = 0;
19118592d2eSPaul Brook         }
192fcf5ef2aSThomas Huth     }
193fcf5ef2aSThomas Huth }
194fcf5ef2aSThomas Huth 
19518592d2eSPaul Brook void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
196fcf5ef2aSThomas Huth {
19718592d2eSPaul Brook     Reg *s = d;
19818592d2eSPaul Brook     int shift, i, j;
199fcf5ef2aSThomas Huth 
20018592d2eSPaul Brook     shift = c->L(0);
201fcf5ef2aSThomas Huth     if (shift > 16) {
202fcf5ef2aSThomas Huth         shift = 16;
203fcf5ef2aSThomas Huth     }
20418592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
205fcf5ef2aSThomas Huth         for (i = 15; i >= shift; i--) {
20618592d2eSPaul Brook             d->B(j + i) = s->B(j + i - shift);
207fcf5ef2aSThomas Huth         }
208fcf5ef2aSThomas Huth         for (i = 0; i < shift; i++) {
20918592d2eSPaul Brook             d->B(j + i) = 0;
21018592d2eSPaul Brook         }
211fcf5ef2aSThomas Huth     }
212fcf5ef2aSThomas Huth }
213fcf5ef2aSThomas Huth #endif
214fcf5ef2aSThomas Huth 
215ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F)                        \
216fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
217fcf5ef2aSThomas Huth     {                                                           \
218ee04a3c8SPaul Brook         int n = num;                                            \
219ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
220ee04a3c8SPaul Brook             d->elem(i) = F(s->elem(i));                         \
221ee04a3c8SPaul Brook         }                                                       \
222fcf5ef2aSThomas Huth     }
223fcf5ef2aSThomas Huth 
224ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F)                        \
225ee04a3c8SPaul Brook     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
226ee04a3c8SPaul Brook     {                                                           \
227ee04a3c8SPaul Brook         Reg *v = d;                                             \
228ee04a3c8SPaul Brook         int n = num;                                            \
229ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
230ee04a3c8SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i));             \
231ee04a3c8SPaul Brook         }                                                       \
232ee04a3c8SPaul Brook     }
233ee04a3c8SPaul Brook 
234ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F)                                   \
235ee04a3c8SPaul Brook     SSE_HELPER_2(name, B, 8 << SHIFT, F)
236ee04a3c8SPaul Brook 
237fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
238ee04a3c8SPaul Brook     SSE_HELPER_2(name, W, 4 << SHIFT, F)
239fcf5ef2aSThomas Huth 
240fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
241ee04a3c8SPaul Brook     SSE_HELPER_2(name, L, 2 << SHIFT, F)
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
244ee04a3c8SPaul Brook     SSE_HELPER_2(name, Q, 1 << SHIFT, F)
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth #if SHIFT == 0
247fcf5ef2aSThomas Huth static inline int satub(int x)
248fcf5ef2aSThomas Huth {
249fcf5ef2aSThomas Huth     if (x < 0) {
250fcf5ef2aSThomas Huth         return 0;
251fcf5ef2aSThomas Huth     } else if (x > 255) {
252fcf5ef2aSThomas Huth         return 255;
253fcf5ef2aSThomas Huth     } else {
254fcf5ef2aSThomas Huth         return x;
255fcf5ef2aSThomas Huth     }
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth static inline int satuw(int x)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     if (x < 0) {
261fcf5ef2aSThomas Huth         return 0;
262fcf5ef2aSThomas Huth     } else if (x > 65535) {
263fcf5ef2aSThomas Huth         return 65535;
264fcf5ef2aSThomas Huth     } else {
265fcf5ef2aSThomas Huth         return x;
266fcf5ef2aSThomas Huth     }
267fcf5ef2aSThomas Huth }
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth static inline int satsb(int x)
270fcf5ef2aSThomas Huth {
271fcf5ef2aSThomas Huth     if (x < -128) {
272fcf5ef2aSThomas Huth         return -128;
273fcf5ef2aSThomas Huth     } else if (x > 127) {
274fcf5ef2aSThomas Huth         return 127;
275fcf5ef2aSThomas Huth     } else {
276fcf5ef2aSThomas Huth         return x;
277fcf5ef2aSThomas Huth     }
278fcf5ef2aSThomas Huth }
279fcf5ef2aSThomas Huth 
280fcf5ef2aSThomas Huth static inline int satsw(int x)
281fcf5ef2aSThomas Huth {
282fcf5ef2aSThomas Huth     if (x < -32768) {
283fcf5ef2aSThomas Huth         return -32768;
284fcf5ef2aSThomas Huth     } else if (x > 32767) {
285fcf5ef2aSThomas Huth         return 32767;
286fcf5ef2aSThomas Huth     } else {
287fcf5ef2aSThomas Huth         return x;
288fcf5ef2aSThomas Huth     }
289fcf5ef2aSThomas Huth }
290fcf5ef2aSThomas Huth 
291fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
292fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
293fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
294fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
295fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
296fcf5ef2aSThomas Huth 
297fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
298fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
299fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
300fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
301fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
302fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
303fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
304fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
305fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
306fcf5ef2aSThomas Huth 
307fcf5ef2aSThomas Huth #define FAND(a, b) ((a) & (b))
308fcf5ef2aSThomas Huth #define FANDN(a, b) ((~(a)) & (b))
309fcf5ef2aSThomas Huth #define FOR(a, b) ((a) | (b))
310fcf5ef2aSThomas Huth #define FXOR(a, b) ((a) ^ (b))
311fcf5ef2aSThomas Huth 
312fcf5ef2aSThomas Huth #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
313fcf5ef2aSThomas Huth #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
314fcf5ef2aSThomas Huth #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
315fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth #define FMULLW(a, b) ((a) * (b))
318fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
319fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
320fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
321fcf5ef2aSThomas Huth 
322fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
323fcf5ef2aSThomas Huth #endif
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddb, FADD)
326fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddw, FADD)
327fcf5ef2aSThomas Huth SSE_HELPER_L(helper_paddl, FADD)
328fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_paddq, FADD)
329fcf5ef2aSThomas Huth 
330fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubb, FSUB)
331fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubw, FSUB)
332fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psubl, FSUB)
333fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_psubq, FSUB)
334fcf5ef2aSThomas Huth 
335fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddusb, FADDUB)
336fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddsb, FADDSB)
337fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubusb, FSUBUB)
338fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubsb, FSUBSB)
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddusw, FADDUW)
341fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddsw, FADDSW)
342fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubusw, FSUBUW)
343fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubsw, FSUBSW)
344fcf5ef2aSThomas Huth 
345fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminub, FMINUB)
346fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxub, FMAXUB)
347fcf5ef2aSThomas Huth 
348fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminsw, FMINSW)
349fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxsw, FMAXSW)
350fcf5ef2aSThomas Huth 
351fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pand, FAND)
352fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pandn, FANDN)
353fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_por, FOR)
354fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pxor, FXOR)
355fcf5ef2aSThomas Huth 
356fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
357fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
358fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
359fcf5ef2aSThomas Huth 
360fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
361fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
362fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
363fcf5ef2aSThomas Huth 
364fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmullw, FMULLW)
365fcf5ef2aSThomas Huth #if SHIFT == 0
366fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrw, FMULHRW)
367fcf5ef2aSThomas Huth #endif
368fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
369fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
370fcf5ef2aSThomas Huth 
371fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
372fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
373fcf5ef2aSThomas Huth 
374fcf5ef2aSThomas Huth void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
375fcf5ef2aSThomas Huth {
376e894bae8SPaul Brook     Reg *v = d;
377e894bae8SPaul Brook     int i;
378e894bae8SPaul Brook 
379e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
380e894bae8SPaul Brook         d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2);
381e894bae8SPaul Brook     }
382fcf5ef2aSThomas Huth }
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
385fcf5ef2aSThomas Huth {
386e894bae8SPaul Brook     Reg *v = d;
387fcf5ef2aSThomas Huth     int i;
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
390e894bae8SPaul Brook         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) +
391e894bae8SPaul Brook             (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1);
392fcf5ef2aSThomas Huth     }
393fcf5ef2aSThomas Huth }
394fcf5ef2aSThomas Huth 
395fcf5ef2aSThomas Huth #if SHIFT == 0
396fcf5ef2aSThomas Huth static inline int abs1(int a)
397fcf5ef2aSThomas Huth {
398fcf5ef2aSThomas Huth     if (a < 0) {
399fcf5ef2aSThomas Huth         return -a;
400fcf5ef2aSThomas Huth     } else {
401fcf5ef2aSThomas Huth         return a;
402fcf5ef2aSThomas Huth     }
403fcf5ef2aSThomas Huth }
404fcf5ef2aSThomas Huth #endif
405e894bae8SPaul Brook 
406fcf5ef2aSThomas Huth void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
407fcf5ef2aSThomas Huth {
408e894bae8SPaul Brook     Reg *v = d;
409e894bae8SPaul Brook     int i;
410fcf5ef2aSThomas Huth 
411e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
412e894bae8SPaul Brook         unsigned int val = 0;
413e894bae8SPaul Brook         val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0));
414e894bae8SPaul Brook         val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1));
415e894bae8SPaul Brook         val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2));
416e894bae8SPaul Brook         val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3));
417e894bae8SPaul Brook         val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4));
418e894bae8SPaul Brook         val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5));
419e894bae8SPaul Brook         val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6));
420e894bae8SPaul Brook         val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7));
421e894bae8SPaul Brook         d->Q(i) = val;
422e894bae8SPaul Brook     }
423fcf5ef2aSThomas Huth }
424fcf5ef2aSThomas Huth 
425fd17264aSPaul Brook #if SHIFT < 2
426fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
427fcf5ef2aSThomas Huth                                   target_ulong a0)
428fcf5ef2aSThomas Huth {
429fcf5ef2aSThomas Huth     int i;
430fcf5ef2aSThomas Huth 
431fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
432fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
433fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
434fcf5ef2aSThomas Huth         }
435fcf5ef2aSThomas Huth     }
436fcf5ef2aSThomas Huth }
437fd17264aSPaul Brook #endif
438fcf5ef2aSThomas Huth 
439fcf5ef2aSThomas Huth void glue(helper_movl_mm_T0, SUFFIX)(Reg *d, uint32_t val)
440fcf5ef2aSThomas Huth {
441e894bae8SPaul Brook     int i;
442e894bae8SPaul Brook 
443fcf5ef2aSThomas Huth     d->L(0) = val;
444fcf5ef2aSThomas Huth     d->L(1) = 0;
445e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
446e894bae8SPaul Brook         d->Q(i) = 0;
447e894bae8SPaul Brook     }
448fcf5ef2aSThomas Huth }
449fcf5ef2aSThomas Huth 
450fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
451fcf5ef2aSThomas Huth void glue(helper_movq_mm_T0, SUFFIX)(Reg *d, uint64_t val)
452fcf5ef2aSThomas Huth {
453e894bae8SPaul Brook     int i;
454e894bae8SPaul Brook 
455fcf5ef2aSThomas Huth     d->Q(0) = val;
456e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
457e894bae8SPaul Brook         d->Q(i) = 0;
458e894bae8SPaul Brook     }
459fcf5ef2aSThomas Huth }
460fcf5ef2aSThomas Huth #endif
461fcf5ef2aSThomas Huth 
462d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
463d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
464d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
465d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
466d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
467d45b0de6SPaul Brook     d->F(offset) = r0;                      \
468d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
469d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
470d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
471d45b0de6SPaul Brook     } while (0)
472d45b0de6SPaul Brook 
473fcf5ef2aSThomas Huth #if SHIFT == 0
474fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
475fcf5ef2aSThomas Huth {
476d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
477fcf5ef2aSThomas Huth 
478d45b0de6SPaul Brook     SHUFFLE4(W, s, s, 0);
479fcf5ef2aSThomas Huth }
480fcf5ef2aSThomas Huth #else
481ce4fa29fSPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *s, int order)
482fcf5ef2aSThomas Huth {
483d45b0de6SPaul Brook     Reg *v = d;
484d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
485d45b0de6SPaul Brook     int i;
486fcf5ef2aSThomas Huth 
487d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
488d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
489d45b0de6SPaul Brook     }
490fcf5ef2aSThomas Huth }
491fcf5ef2aSThomas Huth 
492ce4fa29fSPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *s, int order)
493fcf5ef2aSThomas Huth {
494d45b0de6SPaul Brook     Reg *v = d;
495d45b0de6SPaul Brook     uint64_t r0, r1;
496d45b0de6SPaul Brook     int i;
497fcf5ef2aSThomas Huth 
498d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
499d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
500d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
501d45b0de6SPaul Brook         d->Q(i) = r0;
502d45b0de6SPaul Brook         d->Q(i + 1) = r1;
503d45b0de6SPaul Brook         order >>= 2;
504d45b0de6SPaul Brook     }
505fcf5ef2aSThomas Huth }
506fcf5ef2aSThomas Huth 
507fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
508fcf5ef2aSThomas Huth {
509d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
510d45b0de6SPaul Brook     int i;
511fcf5ef2aSThomas Huth 
512d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
513d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
514d45b0de6SPaul Brook     }
515fcf5ef2aSThomas Huth }
516fcf5ef2aSThomas Huth 
517fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
518fcf5ef2aSThomas Huth {
519d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
520d45b0de6SPaul Brook     int i, j;
521fcf5ef2aSThomas Huth 
522d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
523d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
524d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
525d45b0de6SPaul Brook     }
526fcf5ef2aSThomas Huth }
527fcf5ef2aSThomas Huth 
528fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
529fcf5ef2aSThomas Huth {
530d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
531d45b0de6SPaul Brook     int i, j;
532fcf5ef2aSThomas Huth 
533d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
534d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
535d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
536d45b0de6SPaul Brook     }
537fcf5ef2aSThomas Huth }
538fcf5ef2aSThomas Huth #endif
539fcf5ef2aSThomas Huth 
5403403cafeSPaul Brook #if SHIFT >= 1
541fcf5ef2aSThomas Huth /* FPU ops */
542fcf5ef2aSThomas Huth /* XXX: not accurate */
543fcf5ef2aSThomas Huth 
5443403cafeSPaul Brook #define SSE_HELPER_P(name, F)                                           \
5453403cafeSPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
5463403cafeSPaul Brook             Reg *d, Reg *s)                                             \
547fcf5ef2aSThomas Huth     {                                                                   \
5483403cafeSPaul Brook         Reg *v = d;                                                     \
5493403cafeSPaul Brook         int i;                                                          \
5503403cafeSPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
5513403cafeSPaul Brook             d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i));              \
552fcf5ef2aSThomas Huth         }                                                               \
5533403cafeSPaul Brook     }                                                                   \
5543403cafeSPaul Brook                                                                         \
5553403cafeSPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
5563403cafeSPaul Brook             Reg *d, Reg *s)                                     \
5573403cafeSPaul Brook     {                                                                   \
5583403cafeSPaul Brook         Reg *v = d;                                                     \
5593403cafeSPaul Brook         int i;                                                          \
5603403cafeSPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
5613403cafeSPaul Brook             d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i));              \
5623403cafeSPaul Brook         }                                                               \
5633403cafeSPaul Brook     }
5643403cafeSPaul Brook 
5653403cafeSPaul Brook #if SHIFT == 1
5663403cafeSPaul Brook 
5673403cafeSPaul Brook #define SSE_HELPER_S(name, F)                                           \
5683403cafeSPaul Brook     SSE_HELPER_P(name, F)                                               \
569fcf5ef2aSThomas Huth                                                                         \
570fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)\
571fcf5ef2aSThomas Huth     {                                                                   \
5723403cafeSPaul Brook         Reg *v = d;                                                     \
5733403cafeSPaul Brook         d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0));                  \
574fcf5ef2aSThomas Huth     }                                                                   \
575fcf5ef2aSThomas Huth                                                                         \
576fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)\
577fcf5ef2aSThomas Huth     {                                                                   \
5783403cafeSPaul Brook         Reg *v = d;                                                     \
5793403cafeSPaul Brook         d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0));                  \
580fcf5ef2aSThomas Huth     }
581fcf5ef2aSThomas Huth 
5823403cafeSPaul Brook #else
5833403cafeSPaul Brook 
5843403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F)
5853403cafeSPaul Brook 
5863403cafeSPaul Brook #endif
5873403cafeSPaul Brook 
588fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
589fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
590fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
591fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
592fcf5ef2aSThomas Huth 
593fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
594fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
595fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
596fcf5ef2aSThomas Huth  */
597fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
598fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
599fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
600fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
601fcf5ef2aSThomas Huth 
602fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
603fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
604fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
605fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
606fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
607fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
608fcf5ef2aSThomas Huth 
6093403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6103403cafeSPaul Brook {
6113403cafeSPaul Brook     int i;
6123403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
6133403cafeSPaul Brook         d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status);
6143403cafeSPaul Brook     }
6153403cafeSPaul Brook }
6163403cafeSPaul Brook 
6173403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6183403cafeSPaul Brook {
6193403cafeSPaul Brook     int i;
6203403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
6213403cafeSPaul Brook         d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status);
6223403cafeSPaul Brook     }
6233403cafeSPaul Brook }
6243403cafeSPaul Brook 
6253403cafeSPaul Brook #if SHIFT == 1
6263403cafeSPaul Brook void helper_sqrtss(CPUX86State *env, Reg *d, Reg *s)
6273403cafeSPaul Brook {
6283403cafeSPaul Brook     d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status);
6293403cafeSPaul Brook }
6303403cafeSPaul Brook 
6313403cafeSPaul Brook void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *s)
6323403cafeSPaul Brook {
6333403cafeSPaul Brook     d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status);
6343403cafeSPaul Brook }
6353403cafeSPaul Brook #endif
636fcf5ef2aSThomas Huth 
637fcf5ef2aSThomas Huth /* float to float conversions */
638ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
639fcf5ef2aSThomas Huth {
640fd17264aSPaul Brook     int i;
641fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
642fd17264aSPaul Brook         d->ZMM_D(i) = float32_to_float64(s->ZMM_S(i), &env->sse_status);
643fd17264aSPaul Brook     }
644fcf5ef2aSThomas Huth }
645fcf5ef2aSThomas Huth 
646ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
647fcf5ef2aSThomas Huth {
648fd17264aSPaul Brook     int i;
649fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
650fd17264aSPaul Brook          d->ZMM_S(i) = float64_to_float32(s->ZMM_D(i), &env->sse_status);
651fd17264aSPaul Brook     }
652fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
653fd17264aSPaul Brook          d->Q(i) = 0;
654fd17264aSPaul Brook     }
655fcf5ef2aSThomas Huth }
656fcf5ef2aSThomas Huth 
657fd17264aSPaul Brook #if SHIFT == 1
658fcf5ef2aSThomas Huth void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *s)
659fcf5ef2aSThomas Huth {
660fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
661fcf5ef2aSThomas Huth }
662fcf5ef2aSThomas Huth 
663fcf5ef2aSThomas Huth void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *s)
664fcf5ef2aSThomas Huth {
665fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
666fcf5ef2aSThomas Huth }
667fd17264aSPaul Brook #endif
668fcf5ef2aSThomas Huth 
669fcf5ef2aSThomas Huth /* integer to float */
670ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
671fcf5ef2aSThomas Huth {
672fd17264aSPaul Brook     int i;
673fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
674fd17264aSPaul Brook         d->ZMM_S(i) = int32_to_float32(s->ZMM_L(i), &env->sse_status);
675fd17264aSPaul Brook     }
676fcf5ef2aSThomas Huth }
677fcf5ef2aSThomas Huth 
678ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
679fcf5ef2aSThomas Huth {
680fd17264aSPaul Brook     int i;
681fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
682fd17264aSPaul Brook         int32_t l = s->ZMM_L(i);
683fd17264aSPaul Brook         d->ZMM_D(i) = int32_to_float64(l, &env->sse_status);
684fd17264aSPaul Brook     }
685fcf5ef2aSThomas Huth }
686fcf5ef2aSThomas Huth 
687fd17264aSPaul Brook #if SHIFT == 1
688fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
689fcf5ef2aSThomas Huth {
690fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
691fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
692fcf5ef2aSThomas Huth }
693fcf5ef2aSThomas Huth 
694fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
695fcf5ef2aSThomas Huth {
696fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
697fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
698fcf5ef2aSThomas Huth }
699fcf5ef2aSThomas Huth 
700fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
701fcf5ef2aSThomas Huth {
702fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
703fcf5ef2aSThomas Huth }
704fcf5ef2aSThomas Huth 
705fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
706fcf5ef2aSThomas Huth {
707fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
708fcf5ef2aSThomas Huth }
709fcf5ef2aSThomas Huth 
710fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
711fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
712fcf5ef2aSThomas Huth {
713fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
714fcf5ef2aSThomas Huth }
715fcf5ef2aSThomas Huth 
716fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
717fcf5ef2aSThomas Huth {
718fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
719fcf5ef2aSThomas Huth }
720fcf5ef2aSThomas Huth #endif
721fcf5ef2aSThomas Huth 
722fd17264aSPaul Brook #endif
723fd17264aSPaul Brook 
724fcf5ef2aSThomas Huth /* float to integer */
7251e8a98b5SPeter Maydell 
726fd17264aSPaul Brook #if SHIFT == 1
7271e8a98b5SPeter Maydell /*
7281e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7291e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7301e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7311e8a98b5SPeter Maydell  */
7321e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7331e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7341e8a98b5SPeter Maydell     {                                                                   \
7351e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7361e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7371e8a98b5SPeter Maydell                                                                         \
7381e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7391e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7401e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7411e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7421e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7431e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7441e8a98b5SPeter Maydell         }                                                               \
7451e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7461e8a98b5SPeter Maydell         return r;                                                       \
7471e8a98b5SPeter Maydell     }
7481e8a98b5SPeter Maydell 
7491e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7501e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7511e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7521e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7531e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7541e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7551e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7561e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
757fd17264aSPaul Brook #endif
7581e8a98b5SPeter Maydell 
759ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
760fcf5ef2aSThomas Huth {
761fd17264aSPaul Brook     int i;
762fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
763fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32(s->ZMM_S(i), &env->sse_status);
764fd17264aSPaul Brook     }
765fcf5ef2aSThomas Huth }
766fcf5ef2aSThomas Huth 
767ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
768fcf5ef2aSThomas Huth {
769fd17264aSPaul Brook     int i;
770fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
771fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32(s->ZMM_D(i), &env->sse_status);
772fd17264aSPaul Brook     }
773fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
774fd17264aSPaul Brook          d->Q(i) = 0;
775fd17264aSPaul Brook     }
776fcf5ef2aSThomas Huth }
777fcf5ef2aSThomas Huth 
778fd17264aSPaul Brook #if SHIFT == 1
779fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
780fcf5ef2aSThomas Huth {
7811e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7821e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
783fcf5ef2aSThomas Huth }
784fcf5ef2aSThomas Huth 
785fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
786fcf5ef2aSThomas Huth {
7871e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7881e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
789fcf5ef2aSThomas Huth }
790fcf5ef2aSThomas Huth 
791fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
792fcf5ef2aSThomas Huth {
7931e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
794fcf5ef2aSThomas Huth }
795fcf5ef2aSThomas Huth 
796fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
797fcf5ef2aSThomas Huth {
7981e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
799fcf5ef2aSThomas Huth }
800fcf5ef2aSThomas Huth 
801fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
802fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
803fcf5ef2aSThomas Huth {
8041e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
805fcf5ef2aSThomas Huth }
806fcf5ef2aSThomas Huth 
807fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
808fcf5ef2aSThomas Huth {
8091e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
810fcf5ef2aSThomas Huth }
811fcf5ef2aSThomas Huth #endif
812fd17264aSPaul Brook #endif
813fcf5ef2aSThomas Huth 
814fcf5ef2aSThomas Huth /* float to integer truncated */
815ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
816fcf5ef2aSThomas Huth {
817fd17264aSPaul Brook     int i;
818fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
819fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32_round_to_zero(s->ZMM_S(i),
820fd17264aSPaul Brook                                                          &env->sse_status);
821fd17264aSPaul Brook     }
822fcf5ef2aSThomas Huth }
823fcf5ef2aSThomas Huth 
824ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
825fcf5ef2aSThomas Huth {
826fd17264aSPaul Brook     int i;
827fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
828fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32_round_to_zero(s->ZMM_D(i),
829fd17264aSPaul Brook                                                          &env->sse_status);
830fd17264aSPaul Brook     }
831fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
832fd17264aSPaul Brook          d->Q(i) = 0;
833fd17264aSPaul Brook     }
834fcf5ef2aSThomas Huth }
835fcf5ef2aSThomas Huth 
836fd17264aSPaul Brook #if SHIFT == 1
837fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
838fcf5ef2aSThomas Huth {
8391e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8401e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
841fcf5ef2aSThomas Huth }
842fcf5ef2aSThomas Huth 
843fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
844fcf5ef2aSThomas Huth {
8451e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8461e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
847fcf5ef2aSThomas Huth }
848fcf5ef2aSThomas Huth 
849fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
850fcf5ef2aSThomas Huth {
8511e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
852fcf5ef2aSThomas Huth }
853fcf5ef2aSThomas Huth 
854fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
855fcf5ef2aSThomas Huth {
8561e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
857fcf5ef2aSThomas Huth }
858fcf5ef2aSThomas Huth 
859fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
860fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
861fcf5ef2aSThomas Huth {
8621e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
863fcf5ef2aSThomas Huth }
864fcf5ef2aSThomas Huth 
865fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
866fcf5ef2aSThomas Huth {
8671e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
868fcf5ef2aSThomas Huth }
869fcf5ef2aSThomas Huth #endif
870fd17264aSPaul Brook #endif
871fcf5ef2aSThomas Huth 
872ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
873fcf5ef2aSThomas Huth {
874418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8753403cafeSPaul Brook     int i;
8763403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8773403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one,
8783403cafeSPaul Brook                                   float32_sqrt(s->ZMM_S(i), &env->sse_status),
879fcf5ef2aSThomas Huth                                   &env->sse_status);
8803403cafeSPaul Brook     }
881418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
882fcf5ef2aSThomas Huth }
883fcf5ef2aSThomas Huth 
884fd17264aSPaul Brook #if SHIFT == 1
885fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
886fcf5ef2aSThomas Huth {
887418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
888fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
889fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
890fcf5ef2aSThomas Huth                               &env->sse_status);
891418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
892fcf5ef2aSThomas Huth }
893fd17264aSPaul Brook #endif
894fcf5ef2aSThomas Huth 
895ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
896fcf5ef2aSThomas Huth {
897418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8983403cafeSPaul Brook     int i;
8993403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
9003403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status);
9013403cafeSPaul Brook     }
902418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
903fcf5ef2aSThomas Huth }
904fcf5ef2aSThomas Huth 
905fd17264aSPaul Brook #if SHIFT == 1
906fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
907fcf5ef2aSThomas Huth {
908418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
909fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
910418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
911fcf5ef2aSThomas Huth }
912fd17264aSPaul Brook #endif
913fcf5ef2aSThomas Huth 
914fd17264aSPaul Brook #if SHIFT == 1
915fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
916fcf5ef2aSThomas Huth {
917fcf5ef2aSThomas Huth     uint64_t mask;
918fcf5ef2aSThomas Huth 
919fcf5ef2aSThomas Huth     if (len == 0) {
920fcf5ef2aSThomas Huth         mask = ~0LL;
921fcf5ef2aSThomas Huth     } else {
922fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
923fcf5ef2aSThomas Huth     }
924fcf5ef2aSThomas Huth     return (src >> shift) & mask;
925fcf5ef2aSThomas Huth }
926fcf5ef2aSThomas Huth 
927fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
928fcf5ef2aSThomas Huth {
929*034668c3SPaolo Bonzini     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1) & 63, s->ZMM_B(0) & 63);
930fcf5ef2aSThomas Huth }
931fcf5ef2aSThomas Huth 
932fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
933fcf5ef2aSThomas Huth {
934fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
935fcf5ef2aSThomas Huth }
936fcf5ef2aSThomas Huth 
937fcf5ef2aSThomas Huth static inline uint64_t helper_insertq(uint64_t src, int shift, int len)
938fcf5ef2aSThomas Huth {
939fcf5ef2aSThomas Huth     uint64_t mask;
940fcf5ef2aSThomas Huth 
941fcf5ef2aSThomas Huth     if (len == 0) {
942fcf5ef2aSThomas Huth         mask = ~0ULL;
943fcf5ef2aSThomas Huth     } else {
944fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
945fcf5ef2aSThomas Huth     }
946fcf5ef2aSThomas Huth     return (src & ~(mask << shift)) | ((src & mask) << shift);
947fcf5ef2aSThomas Huth }
948fcf5ef2aSThomas Huth 
949fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
950fcf5ef2aSThomas Huth {
951*034668c3SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(s->ZMM_Q(0), s->ZMM_B(9) & 63, s->ZMM_B(8) & 63);
952fcf5ef2aSThomas Huth }
953fcf5ef2aSThomas Huth 
954fcf5ef2aSThomas Huth void helper_insertq_i(CPUX86State *env, ZMMReg *d, int index, int length)
955fcf5ef2aSThomas Huth {
956fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), index, length);
957fcf5ef2aSThomas Huth }
958fd17264aSPaul Brook #endif
959fcf5ef2aSThomas Huth 
9606567ffb4SPaul Brook #define SSE_HELPER_HPS(name, F)  \
9616567ffb4SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
9626567ffb4SPaul Brook {                                                                 \
9636567ffb4SPaul Brook     Reg *v = d;                                                   \
9646567ffb4SPaul Brook     float32 r[2 << SHIFT];                                        \
9656567ffb4SPaul Brook     int i, j, k;                                                  \
9666567ffb4SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {            \
9676567ffb4SPaul Brook         for (i = j = 0; j < 4; i++, j += 2) {                     \
9686567ffb4SPaul Brook             r[i + k] = F(v->ZMM_S(j + k), v->ZMM_S(j + k + 1), &env->sse_status); \
9696567ffb4SPaul Brook         }                                                         \
9706567ffb4SPaul Brook         for (j = 0; j < 4; i++, j += 2) {                         \
9716567ffb4SPaul Brook             r[i + k] = F(s->ZMM_S(j + k), s->ZMM_S(j + k + 1), &env->sse_status); \
9726567ffb4SPaul Brook         }                                                         \
9736567ffb4SPaul Brook     }                                                             \
9746567ffb4SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                            \
9756567ffb4SPaul Brook         d->ZMM_S(i) = r[i];                                       \
9766567ffb4SPaul Brook     }                                                             \
977fcf5ef2aSThomas Huth }
978fcf5ef2aSThomas Huth 
9796567ffb4SPaul Brook SSE_HELPER_HPS(haddps, float32_add)
9806567ffb4SPaul Brook SSE_HELPER_HPS(hsubps, float32_sub)
981fcf5ef2aSThomas Huth 
9826567ffb4SPaul Brook #define SSE_HELPER_HPD(name, F)  \
9836567ffb4SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
9846567ffb4SPaul Brook {                                                                 \
9856567ffb4SPaul Brook     Reg *v = d;                                                   \
9866567ffb4SPaul Brook     float64 r[1 << SHIFT];                                        \
9876567ffb4SPaul Brook     int i, j, k;                                                  \
9886567ffb4SPaul Brook     for (k = 0; k < 1 << SHIFT; k += LANE_WIDTH / 8) {            \
9896567ffb4SPaul Brook         for (i = j = 0; j < 2; i++, j += 2) {                     \
9906567ffb4SPaul Brook             r[i + k] = F(v->ZMM_D(j + k), v->ZMM_D(j + k + 1), &env->sse_status); \
9916567ffb4SPaul Brook         }                                                         \
9926567ffb4SPaul Brook         for (j = 0; j < 2; i++, j += 2) {                         \
9936567ffb4SPaul Brook             r[i + k] = F(s->ZMM_D(j + k), s->ZMM_D(j + k + 1), &env->sse_status); \
9946567ffb4SPaul Brook         }                                                         \
9956567ffb4SPaul Brook     }                                                             \
9966567ffb4SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {                            \
9976567ffb4SPaul Brook         d->ZMM_D(i) = r[i];                                       \
9986567ffb4SPaul Brook     }                                                             \
999fcf5ef2aSThomas Huth }
1000fcf5ef2aSThomas Huth 
10016567ffb4SPaul Brook SSE_HELPER_HPD(haddpd, float64_add)
10026567ffb4SPaul Brook SSE_HELPER_HPD(hsubpd, float64_sub)
1003fcf5ef2aSThomas Huth 
10043403cafeSPaul Brook void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1005fcf5ef2aSThomas Huth {
10063403cafeSPaul Brook     Reg *v = d;
10073403cafeSPaul Brook     int i;
10083403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 2) {
10093403cafeSPaul Brook         d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
10103403cafeSPaul Brook         d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
10113403cafeSPaul Brook     }
1012fcf5ef2aSThomas Huth }
1013fcf5ef2aSThomas Huth 
10143403cafeSPaul Brook void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1015fcf5ef2aSThomas Huth {
10163403cafeSPaul Brook     Reg *v = d;
10173403cafeSPaul Brook     int i;
10183403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
10193403cafeSPaul Brook         d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status);
10203403cafeSPaul Brook         d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status);
10213403cafeSPaul Brook     }
1022fcf5ef2aSThomas Huth }
1023fcf5ef2aSThomas Huth 
1024cbf4ad54SPaul Brook #define SSE_HELPER_CMP_P(name, F, C)                                    \
1025cbf4ad54SPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
1026cbf4ad54SPaul Brook                                              Reg *d, Reg *s)    \
1027fcf5ef2aSThomas Huth     {                                                                   \
1028cbf4ad54SPaul Brook         Reg *v = d;                                                     \
1029cbf4ad54SPaul Brook         int i;                                                          \
1030cbf4ad54SPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
1031cbf4ad54SPaul Brook             d->ZMM_L(i) = C(F(32, v->ZMM_S(i), s->ZMM_S(i))) ? -1 : 0;  \
1032cbf4ad54SPaul Brook         }                                                               \
1033fcf5ef2aSThomas Huth     }                                                                   \
1034fcf5ef2aSThomas Huth                                                                         \
1035cbf4ad54SPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
1036cbf4ad54SPaul Brook                                              Reg *d, Reg *s)    \
1037cbf4ad54SPaul Brook     {                                                                   \
1038cbf4ad54SPaul Brook         Reg *v = d;                                                     \
1039cbf4ad54SPaul Brook         int i;                                                          \
1040cbf4ad54SPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
1041cbf4ad54SPaul Brook             d->ZMM_Q(i) = C(F(64, v->ZMM_D(i), s->ZMM_D(i))) ? -1 : 0;  \
1042cbf4ad54SPaul Brook         }                                                               \
1043cbf4ad54SPaul Brook     }
1044cbf4ad54SPaul Brook 
1045cbf4ad54SPaul Brook #if SHIFT == 1
1046cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C)                                          \
1047cbf4ad54SPaul Brook     SSE_HELPER_CMP_P(name, F, C)                                            \
1048fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)    \
1049fcf5ef2aSThomas Huth     {                                                                       \
1050cbf4ad54SPaul Brook         Reg *v = d;                                                         \
1051cbf4ad54SPaul Brook         d->ZMM_L(0) = C(F(32, v->ZMM_S(0), s->ZMM_S(0))) ? -1 : 0;          \
1052fcf5ef2aSThomas Huth     }                                                                       \
1053fcf5ef2aSThomas Huth                                                                             \
1054fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)    \
1055fcf5ef2aSThomas Huth     {                                                                       \
1056cbf4ad54SPaul Brook         Reg *v = d;                                                         \
1057cbf4ad54SPaul Brook         d->ZMM_Q(0) = C(F(64, v->ZMM_D(0), s->ZMM_D(0))) ? -1 : 0;          \
1058fcf5ef2aSThomas Huth     }
1059fcf5ef2aSThomas Huth 
1060cbf4ad54SPaul Brook #define FPU_EQ(x) (x == float_relation_equal)
1061cbf4ad54SPaul Brook #define FPU_LT(x) (x == float_relation_less)
1062cbf4ad54SPaul Brook #define FPU_LE(x) (x <= float_relation_equal)
1063cbf4ad54SPaul Brook #define FPU_UNORD(x) (x == float_relation_unordered)
1064fcf5ef2aSThomas Huth 
1065cbf4ad54SPaul Brook #define FPU_CMPQ(size, a, b) \
1066cbf4ad54SPaul Brook     float ## size ## _compare_quiet(a, b, &env->sse_status)
1067cbf4ad54SPaul Brook #define FPU_CMPS(size, a, b) \
1068cbf4ad54SPaul Brook     float ## size ## _compare(a, b, &env->sse_status)
1069cbf4ad54SPaul Brook 
1070cbf4ad54SPaul Brook #else
1071cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C) SSE_HELPER_CMP_P(name, F, C)
1072cbf4ad54SPaul Brook #endif
1073cbf4ad54SPaul Brook 
1074cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpeq, FPU_CMPQ, FPU_EQ)
1075cbf4ad54SPaul Brook SSE_HELPER_CMP(cmplt, FPU_CMPS, FPU_LT)
1076cbf4ad54SPaul Brook SSE_HELPER_CMP(cmple, FPU_CMPS, FPU_LE)
1077cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpunord, FPU_CMPQ,  FPU_UNORD)
1078cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpneq, FPU_CMPQ, !FPU_EQ)
1079cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnlt, FPU_CMPS, !FPU_LT)
1080cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnle, FPU_CMPS, !FPU_LE)
1081cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpord, FPU_CMPQ, !FPU_UNORD)
1082cbf4ad54SPaul Brook 
1083cbf4ad54SPaul Brook #undef SSE_HELPER_CMP
1084fcf5ef2aSThomas Huth 
1085fd17264aSPaul Brook #if SHIFT == 1
1086fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1087fcf5ef2aSThomas Huth 
1088fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1089fcf5ef2aSThomas Huth {
109071bfd65cSRichard Henderson     FloatRelation ret;
1091fcf5ef2aSThomas Huth     float32 s0, s1;
1092fcf5ef2aSThomas Huth 
1093fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1094fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1095fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1096fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1097fcf5ef2aSThomas Huth }
1098fcf5ef2aSThomas Huth 
1099fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1100fcf5ef2aSThomas Huth {
110171bfd65cSRichard Henderson     FloatRelation ret;
1102fcf5ef2aSThomas Huth     float32 s0, s1;
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1105fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1106fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1107fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1108fcf5ef2aSThomas Huth }
1109fcf5ef2aSThomas Huth 
1110fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1111fcf5ef2aSThomas Huth {
111271bfd65cSRichard Henderson     FloatRelation ret;
1113fcf5ef2aSThomas Huth     float64 d0, d1;
1114fcf5ef2aSThomas Huth 
1115fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1116fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1117fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1118fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1119fcf5ef2aSThomas Huth }
1120fcf5ef2aSThomas Huth 
1121fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1122fcf5ef2aSThomas Huth {
112371bfd65cSRichard Henderson     FloatRelation ret;
1124fcf5ef2aSThomas Huth     float64 d0, d1;
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1127fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1128fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1129fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1130fcf5ef2aSThomas Huth }
1131fd17264aSPaul Brook #endif
1132fcf5ef2aSThomas Huth 
1133ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1134fcf5ef2aSThomas Huth {
1135fd17264aSPaul Brook     uint32_t mask;
1136fd17264aSPaul Brook     int i;
1137fcf5ef2aSThomas Huth 
1138fd17264aSPaul Brook     mask = 0;
1139fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1140fd17264aSPaul Brook         mask |= (s->ZMM_L(i) >> (31 - i)) & (1 << i);
1141fd17264aSPaul Brook     }
1142fd17264aSPaul Brook     return mask;
1143fcf5ef2aSThomas Huth }
1144fcf5ef2aSThomas Huth 
1145ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1146fcf5ef2aSThomas Huth {
1147fd17264aSPaul Brook     uint32_t mask;
1148fd17264aSPaul Brook     int i;
1149fcf5ef2aSThomas Huth 
1150fd17264aSPaul Brook     mask = 0;
1151fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1152fd17264aSPaul Brook         mask |= (s->ZMM_Q(i) >> (63 - i)) & (1 << i);
1153fd17264aSPaul Brook     }
1154fd17264aSPaul Brook     return mask;
1155fcf5ef2aSThomas Huth }
1156fcf5ef2aSThomas Huth 
1157fcf5ef2aSThomas Huth #endif
1158fcf5ef2aSThomas Huth 
1159fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1160fcf5ef2aSThomas Huth {
1161fcf5ef2aSThomas Huth     uint32_t val;
1162e894bae8SPaul Brook     int i;
1163fcf5ef2aSThomas Huth 
1164fcf5ef2aSThomas Huth     val = 0;
1165e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
1166e894bae8SPaul Brook         uint8_t byte = 0;
1167e894bae8SPaul Brook         byte |= (s->B(8 * i + 0) >> 7);
1168e894bae8SPaul Brook         byte |= (s->B(8 * i + 1) >> 6) & 0x02;
1169e894bae8SPaul Brook         byte |= (s->B(8 * i + 2) >> 5) & 0x04;
1170e894bae8SPaul Brook         byte |= (s->B(8 * i + 3) >> 4) & 0x08;
1171e894bae8SPaul Brook         byte |= (s->B(8 * i + 4) >> 3) & 0x10;
1172e894bae8SPaul Brook         byte |= (s->B(8 * i + 5) >> 2) & 0x20;
1173e894bae8SPaul Brook         byte |= (s->B(8 * i + 6) >> 1) & 0x40;
1174e894bae8SPaul Brook         byte |= (s->B(8 * i + 7)) & 0x80;
1175e894bae8SPaul Brook         val |= byte << (8 * i);
1176e894bae8SPaul Brook     }
1177fcf5ef2aSThomas Huth     return val;
1178fcf5ef2aSThomas Huth }
1179fcf5ef2aSThomas Huth 
1180d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1181d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1182d45b0de6SPaul Brook         Reg *d, Reg *s)                                       \
1183d45b0de6SPaul Brook {                                                             \
1184d45b0de6SPaul Brook     Reg *v = d;                                               \
1185d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1186d45b0de6SPaul Brook     int j, k;                                                 \
1187d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1188d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1189d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1190d45b0de6SPaul Brook         }                                                     \
1191d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1192d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1193d45b0de6SPaul Brook         }                                                     \
1194d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1195d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1196d45b0de6SPaul Brook         }                                                     \
1197d45b0de6SPaul Brook     }                                                         \
1198fcf5ef2aSThomas Huth }
1199fcf5ef2aSThomas Huth 
1200d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1201d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1202fcf5ef2aSThomas Huth 
1203fcf5ef2aSThomas Huth void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1204fcf5ef2aSThomas Huth {
1205d45b0de6SPaul Brook     Reg *v = d;
1206d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1207d45b0de6SPaul Brook     int j, k;
1208fcf5ef2aSThomas Huth 
1209d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1210d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1211d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1212d45b0de6SPaul Brook         }
1213d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1214d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1215d45b0de6SPaul Brook         }
1216d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1217d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1218d45b0de6SPaul Brook         }
1219d45b0de6SPaul Brook     }
1220fcf5ef2aSThomas Huth }
1221fcf5ef2aSThomas Huth 
1222fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1223fcf5ef2aSThomas Huth                                                                         \
1224fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1225fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1226fcf5ef2aSThomas Huth     {                                                                   \
1227d45b0de6SPaul Brook         Reg *v = d;                                                     \
1228d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1229d45b0de6SPaul Brook         int j, i;                                                       \
1230fcf5ef2aSThomas Huth                                                                         \
1231d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1232d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1233d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1234d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1235d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1236d45b0de6SPaul Brook             }                                                           \
1237d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1238d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1239d45b0de6SPaul Brook             }                                                           \
1240d45b0de6SPaul Brook         }                                                               \
1241fcf5ef2aSThomas Huth     }                                                                   \
1242fcf5ef2aSThomas Huth                                                                         \
1243fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1244fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1245fcf5ef2aSThomas Huth     {                                                                   \
1246d45b0de6SPaul Brook         Reg *v = d;                                                     \
1247d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1248d45b0de6SPaul Brook         int j, i;                                                       \
1249fcf5ef2aSThomas Huth                                                                         \
1250d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1251d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1252d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1253d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1254d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1255d45b0de6SPaul Brook             }                                                           \
1256d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1257d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1258d45b0de6SPaul Brook             }                                                           \
1259d45b0de6SPaul Brook         }                                                               \
1260fcf5ef2aSThomas Huth     }                                                                   \
1261fcf5ef2aSThomas Huth                                                                         \
1262fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1263fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1264fcf5ef2aSThomas Huth     {                                                                   \
1265d45b0de6SPaul Brook         Reg *v = d;                                                     \
1266d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1267d45b0de6SPaul Brook         int j, i;                                                       \
1268fcf5ef2aSThomas Huth                                                                         \
1269d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1270d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1271d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1272d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1273d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1274d45b0de6SPaul Brook             }                                                           \
1275d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1276d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1277d45b0de6SPaul Brook             }                                                           \
1278d45b0de6SPaul Brook         }                                                               \
1279fcf5ef2aSThomas Huth     }                                                                   \
1280fcf5ef2aSThomas Huth                                                                         \
1281fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1282d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1283d45b0de6SPaul Brook                         CPUX86State *env, Reg *d, Reg *s)       \
1284fcf5ef2aSThomas Huth              {                                                          \
1285d45b0de6SPaul Brook                  Reg *v = d;                                            \
1286d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1287d45b0de6SPaul Brook                  int i;                                                 \
1288fcf5ef2aSThomas Huth                                                                         \
1289d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1290d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1291d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1292d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1293d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1294d45b0de6SPaul Brook                  }                                                      \
1295fcf5ef2aSThomas Huth              }                                                          \
1296fcf5ef2aSThomas Huth                                                                         )
1297fcf5ef2aSThomas Huth 
1298fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1299fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1300fcf5ef2aSThomas Huth 
1301d45b0de6SPaul Brook #undef PACK_WIDTH
1302d45b0de6SPaul Brook #undef PACK_HELPER_B
1303d45b0de6SPaul Brook #undef UNPCK_OP
1304d45b0de6SPaul Brook 
1305d45b0de6SPaul Brook 
1306fcf5ef2aSThomas Huth /* 3DNow! float ops */
1307fcf5ef2aSThomas Huth #if SHIFT == 0
1308fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1309fcf5ef2aSThomas Huth {
1310fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1311fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1312fcf5ef2aSThomas Huth }
1313fcf5ef2aSThomas Huth 
1314fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1315fcf5ef2aSThomas Huth {
1316fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1317fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1318fcf5ef2aSThomas Huth }
1319fcf5ef2aSThomas Huth 
1320fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1321fcf5ef2aSThomas Huth {
1322fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1323fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1324fcf5ef2aSThomas Huth }
1325fcf5ef2aSThomas Huth 
1326fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1327fcf5ef2aSThomas Huth {
1328fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1329fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1330fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1331fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1332fcf5ef2aSThomas Huth }
1333fcf5ef2aSThomas Huth 
1334fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1335fcf5ef2aSThomas Huth {
133625bdec79SPaolo Bonzini     float32 r;
1337fcf5ef2aSThomas Huth 
133825bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
133925bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
134025bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1341fcf5ef2aSThomas Huth }
1342fcf5ef2aSThomas Huth 
1343fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1344fcf5ef2aSThomas Huth {
1345fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1346fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1347fcf5ef2aSThomas Huth }
1348fcf5ef2aSThomas Huth 
1349fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1350fcf5ef2aSThomas Huth {
1351fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1352fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1353fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1354fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1355fcf5ef2aSThomas Huth }
1356fcf5ef2aSThomas Huth 
1357fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1358fcf5ef2aSThomas Huth {
1359fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1360fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1361fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1362fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1363fcf5ef2aSThomas Huth }
1364fcf5ef2aSThomas Huth 
1365fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1366fcf5ef2aSThomas Huth {
1367fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1368fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1369fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1370fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1371fcf5ef2aSThomas Huth }
1372fcf5ef2aSThomas Huth 
1373fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1374fcf5ef2aSThomas Huth {
1375fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1376fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1377fcf5ef2aSThomas Huth     }
1378fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1379fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1380fcf5ef2aSThomas Huth     }
1381fcf5ef2aSThomas Huth }
1382fcf5ef2aSThomas Huth 
1383fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1384fcf5ef2aSThomas Huth {
1385fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1386fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1387fcf5ef2aSThomas Huth     }
1388fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1389fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1390fcf5ef2aSThomas Huth     }
1391fcf5ef2aSThomas Huth }
1392fcf5ef2aSThomas Huth 
1393fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1394fcf5ef2aSThomas Huth {
1395fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1396fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1397fcf5ef2aSThomas Huth }
1398fcf5ef2aSThomas Huth 
1399fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1400fcf5ef2aSThomas Huth {
140125bdec79SPaolo Bonzini     float32 r;
1402fcf5ef2aSThomas Huth 
140325bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
140425bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
140525bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1406fcf5ef2aSThomas Huth }
1407fcf5ef2aSThomas Huth 
1408fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1409fcf5ef2aSThomas Huth {
141025bdec79SPaolo Bonzini     float32 r;
1411fcf5ef2aSThomas Huth 
141225bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
141325bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
141425bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1415fcf5ef2aSThomas Huth }
1416fcf5ef2aSThomas Huth 
1417fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1418fcf5ef2aSThomas Huth {
1419fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1420fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1421fcf5ef2aSThomas Huth }
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1424fcf5ef2aSThomas Huth {
1425fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1426fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1427fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1428fcf5ef2aSThomas Huth                               &env->mmx_status);
1429fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1430fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1431fcf5ef2aSThomas Huth }
1432fcf5ef2aSThomas Huth 
1433fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1434fcf5ef2aSThomas Huth {
1435fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1436fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1437fcf5ef2aSThomas Huth }
1438fcf5ef2aSThomas Huth 
1439fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1440fcf5ef2aSThomas Huth {
1441fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1442fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1443fcf5ef2aSThomas Huth }
1444fcf5ef2aSThomas Huth 
1445fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1446fcf5ef2aSThomas Huth {
144725bdec79SPaolo Bonzini     uint32_t r;
1448fcf5ef2aSThomas Huth 
144925bdec79SPaolo Bonzini     r = s->MMX_L(0);
145025bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
145125bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1452fcf5ef2aSThomas Huth }
1453fcf5ef2aSThomas Huth #endif
1454fcf5ef2aSThomas Huth 
1455fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1456fcf5ef2aSThomas Huth void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1457fcf5ef2aSThomas Huth {
1458d45b0de6SPaul Brook     Reg *v = d;
1459fcf5ef2aSThomas Huth     int i;
1460d45b0de6SPaul Brook #if SHIFT == 0
1461d45b0de6SPaul Brook     uint8_t r[8];
1462fcf5ef2aSThomas Huth 
1463d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1464d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1465fcf5ef2aSThomas Huth     }
1466d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1467d45b0de6SPaul Brook         d->B(i) = r[i];
1468fcf5ef2aSThomas Huth     }
1469d45b0de6SPaul Brook #else
1470d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1471fcf5ef2aSThomas Huth 
1472d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1473d45b0de6SPaul Brook         int j = i & ~0xf;
1474d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1475fcf5ef2aSThomas Huth     }
1476d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1477d45b0de6SPaul Brook         d->B(i) = r[i];
1478fcf5ef2aSThomas Huth     }
1479fcf5ef2aSThomas Huth #endif
1480fcf5ef2aSThomas Huth }
1481fcf5ef2aSThomas Huth 
1482d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1483d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1484d45b0de6SPaul Brook {                                                          \
1485d45b0de6SPaul Brook     Reg *v = d;                                            \
1486d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1487d45b0de6SPaul Brook     int i, j, k;                                           \
1488d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1489d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1490d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1491d45b0de6SPaul Brook         }                                                  \
1492d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1493d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1494d45b0de6SPaul Brook         }                                                  \
1495d45b0de6SPaul Brook     }                                                      \
1496d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1497d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1498d45b0de6SPaul Brook     }                                                      \
1499fcf5ef2aSThomas Huth }
1500fcf5ef2aSThomas Huth 
1501d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1502d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1503d45b0de6SPaul Brook {                                                          \
1504d45b0de6SPaul Brook     Reg *v = d;                                            \
1505d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1506d45b0de6SPaul Brook     int i, j, k;                                           \
1507d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1508d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1509d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1510d45b0de6SPaul Brook         }                                                  \
1511d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1512d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1513d45b0de6SPaul Brook         }                                                  \
1514d45b0de6SPaul Brook     }                                                      \
1515d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1516d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1517d45b0de6SPaul Brook     }                                                      \
1518fcf5ef2aSThomas Huth }
1519fcf5ef2aSThomas Huth 
1520d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1521d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1522d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1523d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1524d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1525d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
152675046ad7SPaolo Bonzini 
1527d45b0de6SPaul Brook #undef SSE_HELPER_HW
1528d45b0de6SPaul Brook #undef SSE_HELPER_HL
1529d45b0de6SPaul Brook 
1530d45b0de6SPaul Brook void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1531d45b0de6SPaul Brook {
1532d45b0de6SPaul Brook     Reg *v = d;
1533d45b0de6SPaul Brook     int i;
1534d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1535d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1536d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1537d45b0de6SPaul Brook     }
1538fcf5ef2aSThomas Huth }
1539fcf5ef2aSThomas Huth 
1540ee04a3c8SPaul Brook #define FABSB(x) (x > INT8_MAX  ? -(int8_t)x : x)
1541ee04a3c8SPaul Brook #define FABSW(x) (x > INT16_MAX ? -(int16_t)x : x)
1542ee04a3c8SPaul Brook #define FABSL(x) (x > INT32_MAX ? -(int32_t)x : x)
1543ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsb, B, 8 << SHIFT, FABSB)
1544ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsw, W, 4 << SHIFT, FABSW)
1545ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsd, L, 2 << SHIFT, FABSL)
1546fcf5ef2aSThomas Huth 
1547fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1548fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1549fcf5ef2aSThomas Huth 
1550fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1551fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1552fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1553fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1554fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1555fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1556fcf5ef2aSThomas Huth 
1557fcf5ef2aSThomas Huth void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1558fcf5ef2aSThomas Huth                                   int32_t shift)
1559fcf5ef2aSThomas Huth {
1560d45b0de6SPaul Brook     Reg *v = d;
1561d45b0de6SPaul Brook     int i;
1562fcf5ef2aSThomas Huth 
1563fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1564d45b0de6SPaul Brook     if (shift >= (SHIFT ? 32 : 16)) {
1565d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1566d45b0de6SPaul Brook             d->Q(i) = 0;
1567d45b0de6SPaul Brook         }
1568fcf5ef2aSThomas Huth     } else {
1569fcf5ef2aSThomas Huth         shift <<= 3;
1570fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1571fcf5ef2aSThomas Huth #if SHIFT == 0
1572d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1573d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1574fcf5ef2aSThomas Huth #else
1575d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1576d45b0de6SPaul Brook             uint64_t r0, r1;
1577d45b0de6SPaul Brook 
1578d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1579d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1580d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1581d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1582d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1583d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1584d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1585d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1586d45b0de6SPaul Brook             d->Q(i) = r0;
1587d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1588d45b0de6SPaul Brook         }
1589fcf5ef2aSThomas Huth #endif
1590fcf5ef2aSThomas Huth #undef SHR
1591fcf5ef2aSThomas Huth     }
1592fcf5ef2aSThomas Huth }
1593fcf5ef2aSThomas Huth 
15940e29cea5SPaul Brook #if SHIFT >= 1
1595fcf5ef2aSThomas Huth 
1596fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1597fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1598fcf5ef2aSThomas Huth     {                                                                   \
15990e29cea5SPaul Brook         Reg *v = d;                                                     \
16000e29cea5SPaul Brook         Reg *m = &env->xmm_regs[0];                                     \
16010e29cea5SPaul Brook         int i;                                                          \
16020e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
16030e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), m->elem(i));         \
1604fcf5ef2aSThomas Huth         }                                                               \
1605fcf5ef2aSThomas Huth     }
1606fcf5ef2aSThomas Huth 
1607fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
16080e29cea5SPaul Brook     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,   \
16090e29cea5SPaul Brook                             uint32_t imm)                               \
1610fcf5ef2aSThomas Huth     {                                                                   \
16110e29cea5SPaul Brook         Reg *v = d;                                                     \
16120e29cea5SPaul Brook         int i;                                                          \
16130e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
16140e29cea5SPaul Brook             int j = i & 7;                                              \
16150e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), (imm >> j) & 1);     \
1616fcf5ef2aSThomas Huth         }                                                               \
1617fcf5ef2aSThomas Huth     }
1618fcf5ef2aSThomas Huth 
1619fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
16200e29cea5SPaul Brook #define FBLENDVB(v, s, m) ((m & 0x80) ? s : v)
16210e29cea5SPaul Brook #define FBLENDVPS(v, s, m) ((m & 0x80000000) ? s : v)
16220e29cea5SPaul Brook #define FBLENDVPD(v, s, m) ((m & 0x8000000000000000LL) ? s : v)
16230e29cea5SPaul Brook SSE_HELPER_V(helper_pblendvb, B, 8 << SHIFT, FBLENDVB)
16240e29cea5SPaul Brook SSE_HELPER_V(helper_blendvps, L, 2 << SHIFT, FBLENDVPS)
16250e29cea5SPaul Brook SSE_HELPER_V(helper_blendvpd, Q, 1 << SHIFT, FBLENDVPD)
1626fcf5ef2aSThomas Huth 
1627fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1628fcf5ef2aSThomas Huth {
1629e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1630e894bae8SPaul Brook     int i;
1631fcf5ef2aSThomas Huth 
1632e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1633e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1634e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1635e894bae8SPaul Brook     }
1636fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1637fcf5ef2aSThomas Huth }
1638fcf5ef2aSThomas Huth 
1639fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1640fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1641fcf5ef2aSThomas Huth     {                                                           \
1642e894bae8SPaul Brook         int n = num;                                            \
1643e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1644e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1645fcf5ef2aSThomas Huth         }                                                       \
1646fcf5ef2aSThomas Huth     }
1647fcf5ef2aSThomas Huth 
1648e894bae8SPaul Brook #if SHIFT > 0
1649e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1650e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1651e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1652e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1653e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1654e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1655e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1656e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1657e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1658e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1659e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1660e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
1661e894bae8SPaul Brook #endif
1662fcf5ef2aSThomas Huth 
1663fcf5ef2aSThomas Huth void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1664fcf5ef2aSThomas Huth {
1665e894bae8SPaul Brook     Reg *v = d;
1666e894bae8SPaul Brook     int i;
1667e894bae8SPaul Brook 
1668e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1669e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1670e894bae8SPaul Brook     }
1671fcf5ef2aSThomas Huth }
1672fcf5ef2aSThomas Huth 
1673fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1674fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1675fcf5ef2aSThomas Huth 
1676fcf5ef2aSThomas Huth void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1677fcf5ef2aSThomas Huth {
1678d45b0de6SPaul Brook     Reg *v = d;
1679d45b0de6SPaul Brook     uint16_t r[8];
1680d45b0de6SPaul Brook     int i, j, k;
168180e19606SJoseph Myers 
1682d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1683d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1684d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1685d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1686d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1687d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1688d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1689d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1690d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1691d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1692d45b0de6SPaul Brook             d->W(i + k) = r[k];
1693d45b0de6SPaul Brook         }
1694d45b0de6SPaul Brook     }
1695fcf5ef2aSThomas Huth }
1696fcf5ef2aSThomas Huth 
1697fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1698fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1699fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1700fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1701fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1702fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1703fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1704fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1705fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1706fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1707fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1708fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1709fcf5ef2aSThomas Huth 
1710fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1711fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1712fcf5ef2aSThomas Huth 
1713fd17264aSPaul Brook #if SHIFT == 1
1714fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1715fcf5ef2aSThomas Huth {
1716fcf5ef2aSThomas Huth     int idx = 0;
1717fcf5ef2aSThomas Huth 
1718fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1719fcf5ef2aSThomas Huth         idx = 1;
1720fcf5ef2aSThomas Huth     }
1721fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1722fcf5ef2aSThomas Huth         idx = 2;
1723fcf5ef2aSThomas Huth     }
1724fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1725fcf5ef2aSThomas Huth         idx = 3;
1726fcf5ef2aSThomas Huth     }
1727fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1728fcf5ef2aSThomas Huth         idx = 4;
1729fcf5ef2aSThomas Huth     }
1730fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1731fcf5ef2aSThomas Huth         idx = 5;
1732fcf5ef2aSThomas Huth     }
1733fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1734fcf5ef2aSThomas Huth         idx = 6;
1735fcf5ef2aSThomas Huth     }
1736fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1737fcf5ef2aSThomas Huth         idx = 7;
1738fcf5ef2aSThomas Huth     }
1739fcf5ef2aSThomas Huth 
1740fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1741aa406feaSJoseph Myers     d->W(1) = idx;
1742aa406feaSJoseph Myers     d->L(1) = 0;
1743aa406feaSJoseph Myers     d->Q(1) = 0;
1744fcf5ef2aSThomas Huth }
1745fd17264aSPaul Brook #endif
1746fcf5ef2aSThomas Huth 
1747fcf5ef2aSThomas Huth void glue(helper_roundps, 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 < 2 << SHIFT; i++) {
1773fd17264aSPaul Brook         d->ZMM_S(i) = float32_round_to_int(s->ZMM_S(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 
1784fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1785fcf5ef2aSThomas Huth                                   uint32_t mode)
1786fcf5ef2aSThomas Huth {
1787418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1788fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1789fd17264aSPaul Brook     int i;
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 
1809fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1810fd17264aSPaul Brook         d->ZMM_D(i) = float64_round_to_int(s->ZMM_D(i), &env->sse_status);
1811fd17264aSPaul Brook     }
1812fcf5ef2aSThomas Huth 
1813418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1814fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1815fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1816fcf5ef2aSThomas Huth                                   &env->sse_status);
1817fcf5ef2aSThomas Huth     }
1818fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1819fcf5ef2aSThomas Huth }
1820fcf5ef2aSThomas Huth 
1821fd17264aSPaul Brook #if SHIFT == 1
1822fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1823fcf5ef2aSThomas Huth                                   uint32_t mode)
1824fcf5ef2aSThomas Huth {
1825418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1826fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1827fcf5ef2aSThomas Huth 
1828fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1829fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1830fcf5ef2aSThomas Huth         switch (mode & 3) {
1831fcf5ef2aSThomas Huth         case 0:
1832fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1833fcf5ef2aSThomas Huth             break;
1834fcf5ef2aSThomas Huth         case 1:
1835fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1836fcf5ef2aSThomas Huth             break;
1837fcf5ef2aSThomas Huth         case 2:
1838fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1839fcf5ef2aSThomas Huth             break;
1840fcf5ef2aSThomas Huth         case 3:
1841fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1842fcf5ef2aSThomas Huth             break;
1843fcf5ef2aSThomas Huth         }
1844fcf5ef2aSThomas Huth     }
1845fcf5ef2aSThomas Huth 
1846fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1847fcf5ef2aSThomas Huth 
1848418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1849fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1850fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1851fcf5ef2aSThomas Huth                                   &env->sse_status);
1852fcf5ef2aSThomas Huth     }
1853fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1854fcf5ef2aSThomas Huth }
1855fcf5ef2aSThomas Huth 
1856fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1857fcf5ef2aSThomas Huth                                   uint32_t mode)
1858fcf5ef2aSThomas Huth {
1859418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1860fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1861fcf5ef2aSThomas Huth 
1862fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1863fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1864fcf5ef2aSThomas Huth         switch (mode & 3) {
1865fcf5ef2aSThomas Huth         case 0:
1866fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1867fcf5ef2aSThomas Huth             break;
1868fcf5ef2aSThomas Huth         case 1:
1869fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1870fcf5ef2aSThomas Huth             break;
1871fcf5ef2aSThomas Huth         case 2:
1872fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1873fcf5ef2aSThomas Huth             break;
1874fcf5ef2aSThomas Huth         case 3:
1875fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1876fcf5ef2aSThomas Huth             break;
1877fcf5ef2aSThomas Huth         }
1878fcf5ef2aSThomas Huth     }
1879fcf5ef2aSThomas Huth 
1880fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1881fcf5ef2aSThomas Huth 
1882418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1883fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1884fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1885fcf5ef2aSThomas Huth                                   &env->sse_status);
1886fcf5ef2aSThomas Huth     }
1887fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1888fcf5ef2aSThomas Huth }
1889fd17264aSPaul Brook #endif
1890fcf5ef2aSThomas Huth 
18910e29cea5SPaul Brook #define FBLENDP(v, s, m) (m ? s : v)
18920e29cea5SPaul Brook SSE_HELPER_I(helper_blendps, L, 2 << SHIFT, FBLENDP)
18930e29cea5SPaul Brook SSE_HELPER_I(helper_blendpd, Q, 1 << SHIFT, FBLENDP)
18940e29cea5SPaul Brook SSE_HELPER_I(helper_pblendw, W, 4 << SHIFT, FBLENDP)
1895fcf5ef2aSThomas Huth 
18966f218d6eSPaul Brook void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
18976f218d6eSPaul Brook                                uint32_t mask)
1898fcf5ef2aSThomas Huth {
18996f218d6eSPaul Brook     Reg *v = d;
1900bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
19016f218d6eSPaul Brook     int i;
1902fcf5ef2aSThomas Huth 
19036f218d6eSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
1904bf30ad8cSPaolo Bonzini         /*
1905bf30ad8cSPaolo Bonzini          * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1906bf30ad8cSPaolo Bonzini          * to correctly round the intermediate results
1907bf30ad8cSPaolo Bonzini          */
1908fcf5ef2aSThomas Huth         if (mask & (1 << 4)) {
19096f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
1910bf30ad8cSPaolo Bonzini         } else {
1911bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1912fcf5ef2aSThomas Huth         }
1913fcf5ef2aSThomas Huth         if (mask & (1 << 5)) {
19146f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
1915bf30ad8cSPaolo Bonzini         } else {
1916bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1917fcf5ef2aSThomas Huth         }
1918bf30ad8cSPaolo Bonzini         temp2 = float32_add(prod1, prod2, &env->sse_status);
1919fcf5ef2aSThomas Huth         if (mask & (1 << 6)) {
19206f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i+2), s->ZMM_S(i+2), &env->sse_status);
1921bf30ad8cSPaolo Bonzini         } else {
1922bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1923fcf5ef2aSThomas Huth         }
1924fcf5ef2aSThomas Huth         if (mask & (1 << 7)) {
19256f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+3), s->ZMM_S(i+3), &env->sse_status);
1926bf30ad8cSPaolo Bonzini         } else {
1927bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1928fcf5ef2aSThomas Huth         }
1929bf30ad8cSPaolo Bonzini         temp3 = float32_add(prod1, prod2, &env->sse_status);
1930bf30ad8cSPaolo Bonzini         temp4 = float32_add(temp2, temp3, &env->sse_status);
1931bf30ad8cSPaolo Bonzini 
19326f218d6eSPaul Brook         d->ZMM_S(i) = (mask & (1 << 0)) ? temp4 : float32_zero;
19336f218d6eSPaul Brook         d->ZMM_S(i+1) = (mask & (1 << 1)) ? temp4 : float32_zero;
19346f218d6eSPaul Brook         d->ZMM_S(i+2) = (mask & (1 << 2)) ? temp4 : float32_zero;
19356f218d6eSPaul Brook         d->ZMM_S(i+3) = (mask & (1 << 3)) ? temp4 : float32_zero;
19366f218d6eSPaul Brook     }
1937fcf5ef2aSThomas Huth }
1938fcf5ef2aSThomas Huth 
19396f218d6eSPaul Brook #if SHIFT == 1
19406f218d6eSPaul Brook /* Oddly, there is no ymm version of dppd */
19416f218d6eSPaul Brook void glue(helper_dppd, SUFFIX)(CPUX86State *env,
19426f218d6eSPaul Brook                                Reg *d, Reg *s, uint32_t mask)
1943fcf5ef2aSThomas Huth {
19446f218d6eSPaul Brook     Reg *v = d;
1945bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1946fcf5ef2aSThomas Huth 
1947fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
19486f218d6eSPaul Brook         prod1 = float64_mul(v->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1949bf30ad8cSPaolo Bonzini     } else {
1950bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1951fcf5ef2aSThomas Huth     }
1952fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
19536f218d6eSPaul Brook         prod2 = float64_mul(v->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1954bf30ad8cSPaolo Bonzini     } else {
1955bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1956fcf5ef2aSThomas Huth     }
1957bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1958bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1959bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1960fcf5ef2aSThomas Huth }
19616f218d6eSPaul Brook #endif
1962fcf5ef2aSThomas Huth 
1963fcf5ef2aSThomas Huth void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1964fcf5ef2aSThomas Huth                                   uint32_t offset)
1965fcf5ef2aSThomas Huth {
1966d45b0de6SPaul Brook     Reg *v = d;
1967d45b0de6SPaul Brook     int i, j;
1968d45b0de6SPaul Brook     uint16_t r[8];
1969fcf5ef2aSThomas Huth 
1970d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; ) {
1971d45b0de6SPaul Brook         int s0 = (j * 2) + ((offset & 3) << 2);
1972d45b0de6SPaul Brook         int d0 = (j * 2) + ((offset & 4) << 0);
1973d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, d0++) {
1974d45b0de6SPaul Brook             r[i] = 0;
1975d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0));
1976d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1));
1977d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2));
1978d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3));
1979fcf5ef2aSThomas Huth         }
1980d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, j++) {
1981d45b0de6SPaul Brook             d->W(j) = r[i];
1982d45b0de6SPaul Brook         }
1983d45b0de6SPaul Brook         offset >>= 3;
1984d45b0de6SPaul Brook     }
1985fcf5ef2aSThomas Huth }
1986fcf5ef2aSThomas Huth 
1987fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1988fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
1989fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1990fcf5ef2aSThomas Huth 
1991fd17264aSPaul Brook #if SHIFT == 1
1992fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1993fcf5ef2aSThomas Huth {
1994d1da229fSPaul Brook     target_long val, limit;
1995fcf5ef2aSThomas Huth 
1996fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1997fcf5ef2aSThomas Huth     if (ctrl >> 8) {
1998d1da229fSPaul Brook         val = (target_long)env->regs[reg];
1999fcf5ef2aSThomas Huth     } else {
2000d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
2001fcf5ef2aSThomas Huth     }
2002fcf5ef2aSThomas Huth     if (ctrl & 1) {
2003d1da229fSPaul Brook         limit = 8;
2004fcf5ef2aSThomas Huth     } else {
2005d1da229fSPaul Brook         limit = 16;
2006fcf5ef2aSThomas Huth     }
2007d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
2008d1da229fSPaul Brook         return limit;
2009fcf5ef2aSThomas Huth     }
2010d1da229fSPaul Brook     return abs1(val);
2011fcf5ef2aSThomas Huth }
2012fcf5ef2aSThomas Huth 
2013fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
2014fcf5ef2aSThomas Huth {
2015fcf5ef2aSThomas Huth     int val = 0;
2016fcf5ef2aSThomas Huth 
2017fcf5ef2aSThomas Huth     if (ctrl & 1) {
2018fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
2019fcf5ef2aSThomas Huth             val++;
2020fcf5ef2aSThomas Huth         }
2021fcf5ef2aSThomas Huth     } else {
2022fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
2023fcf5ef2aSThomas Huth             val++;
2024fcf5ef2aSThomas Huth         }
2025fcf5ef2aSThomas Huth     }
2026fcf5ef2aSThomas Huth 
2027fcf5ef2aSThomas Huth     return val;
2028fcf5ef2aSThomas Huth }
2029fcf5ef2aSThomas Huth 
2030fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
2031fcf5ef2aSThomas Huth {
2032fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
2033fcf5ef2aSThomas Huth     case 0:
2034fcf5ef2aSThomas Huth         return r->B(i);
2035fcf5ef2aSThomas Huth     case 1:
2036fcf5ef2aSThomas Huth         return r->W(i);
2037fcf5ef2aSThomas Huth     case 2:
2038fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
2039fcf5ef2aSThomas Huth     case 3:
2040fcf5ef2aSThomas Huth     default:
2041fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
2042fcf5ef2aSThomas Huth     }
2043fcf5ef2aSThomas Huth }
2044fcf5ef2aSThomas Huth 
2045fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
2046fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
2047fcf5ef2aSThomas Huth {
2048fcf5ef2aSThomas Huth     unsigned int res = 0;
2049fcf5ef2aSThomas Huth     int v;
2050fcf5ef2aSThomas Huth     int j, i;
2051fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
2052fcf5ef2aSThomas Huth 
2053fcf5ef2aSThomas Huth     valids--;
2054fcf5ef2aSThomas Huth     validd--;
2055fcf5ef2aSThomas Huth 
2056fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2057fcf5ef2aSThomas Huth 
2058fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2059fcf5ef2aSThomas Huth     case 0:
2060fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2061fcf5ef2aSThomas Huth             res <<= 1;
2062fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2063fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2064fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2065fcf5ef2aSThomas Huth             }
2066fcf5ef2aSThomas Huth         }
2067fcf5ef2aSThomas Huth         break;
2068fcf5ef2aSThomas Huth     case 1:
2069fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2070fcf5ef2aSThomas Huth             res <<= 1;
2071fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2072fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2073fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2074fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2075fcf5ef2aSThomas Huth             }
2076fcf5ef2aSThomas Huth         }
2077fcf5ef2aSThomas Huth         break;
2078fcf5ef2aSThomas Huth     case 2:
2079fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2080fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2081fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2082fcf5ef2aSThomas Huth             res <<= 1;
2083fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2084fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2085fcf5ef2aSThomas Huth         }
2086fcf5ef2aSThomas Huth         break;
2087fcf5ef2aSThomas Huth     case 3:
2088ae35eea7SJoseph Myers         if (validd == -1) {
2089ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2090ae35eea7SJoseph Myers             break;
2091ae35eea7SJoseph Myers         }
2092bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2093fcf5ef2aSThomas Huth             res <<= 1;
2094fcf5ef2aSThomas Huth             v = 1;
2095bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2096fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2097fcf5ef2aSThomas Huth             }
2098fcf5ef2aSThomas Huth             res |= v;
2099fcf5ef2aSThomas Huth         }
2100fcf5ef2aSThomas Huth         break;
2101fcf5ef2aSThomas Huth     }
2102fcf5ef2aSThomas Huth 
2103fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2104fcf5ef2aSThomas Huth     case 1:
2105fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2106fcf5ef2aSThomas Huth         break;
2107fcf5ef2aSThomas Huth     case 3:
2108fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2109fcf5ef2aSThomas Huth         break;
2110fcf5ef2aSThomas Huth     }
2111fcf5ef2aSThomas Huth 
2112fcf5ef2aSThomas Huth     if (res) {
2113fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2114fcf5ef2aSThomas Huth     }
2115fcf5ef2aSThomas Huth     if (res & 1) {
2116fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2117fcf5ef2aSThomas Huth     }
2118fcf5ef2aSThomas Huth 
2119fcf5ef2aSThomas Huth     return res;
2120fcf5ef2aSThomas Huth }
2121fcf5ef2aSThomas Huth 
2122fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2123fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2124fcf5ef2aSThomas Huth {
2125fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2126fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2127fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2128fcf5ef2aSThomas Huth 
2129fcf5ef2aSThomas Huth     if (res) {
2130fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2131fcf5ef2aSThomas Huth     } else {
2132fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2133fcf5ef2aSThomas Huth     }
2134fcf5ef2aSThomas Huth }
2135fcf5ef2aSThomas Huth 
2136fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2137fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2138fcf5ef2aSThomas Huth {
2139fcf5ef2aSThomas Huth     int i;
2140fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2141fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2142fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2143fcf5ef2aSThomas Huth 
2144fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2145fcf5ef2aSThomas Huth         if (ctrl & 1) {
2146fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2147fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2148fcf5ef2aSThomas Huth             }
2149fcf5ef2aSThomas Huth         } else {
2150fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2151fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2152fcf5ef2aSThomas Huth             }
2153fcf5ef2aSThomas Huth         }
2154fcf5ef2aSThomas Huth     } else {
2155fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2156fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2157fcf5ef2aSThomas Huth     }
2158fcf5ef2aSThomas Huth }
2159fcf5ef2aSThomas Huth 
2160fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2161fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2162fcf5ef2aSThomas Huth {
2163fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2164fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2165fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2166fcf5ef2aSThomas Huth 
2167fcf5ef2aSThomas Huth     if (res) {
2168fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2169fcf5ef2aSThomas Huth     } else {
2170fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2171fcf5ef2aSThomas Huth     }
2172fcf5ef2aSThomas Huth }
2173fcf5ef2aSThomas Huth 
2174fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2175fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2176fcf5ef2aSThomas Huth {
2177fcf5ef2aSThomas Huth     int i;
2178fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2179fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2180fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2181fcf5ef2aSThomas Huth 
2182fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2183fcf5ef2aSThomas Huth         if (ctrl & 1) {
2184fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2185fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2186fcf5ef2aSThomas Huth             }
2187fcf5ef2aSThomas Huth         } else {
2188fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2189fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2190fcf5ef2aSThomas Huth             }
2191fcf5ef2aSThomas Huth         }
2192fcf5ef2aSThomas Huth     } else {
2193fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2194fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2195fcf5ef2aSThomas Huth     }
2196fcf5ef2aSThomas Huth }
2197fcf5ef2aSThomas Huth 
2198fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2199fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2200fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2201fcf5ef2aSThomas Huth {
2202fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2203fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2204fcf5ef2aSThomas Huth 
2205fcf5ef2aSThomas Huth     while (len--) {
2206fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2207fcf5ef2aSThomas Huth     }
2208fcf5ef2aSThomas Huth 
2209fcf5ef2aSThomas Huth     return crc;
2210fcf5ef2aSThomas Huth }
2211fcf5ef2aSThomas Huth 
2212fd17264aSPaul Brook #endif
2213fd17264aSPaul Brook 
22145a09df21SPaul Brook #if SHIFT == 1
22155a09df21SPaul Brook static void clmulq(uint64_t *dest_l, uint64_t *dest_h,
22165a09df21SPaul Brook                           uint64_t a, uint64_t b)
2217fcf5ef2aSThomas Huth {
22185a09df21SPaul Brook     uint64_t al, ah, resh, resl;
2219fcf5ef2aSThomas Huth 
2220fcf5ef2aSThomas Huth     ah = 0;
22215a09df21SPaul Brook     al = a;
2222fcf5ef2aSThomas Huth     resh = resl = 0;
2223fcf5ef2aSThomas Huth 
2224fcf5ef2aSThomas Huth     while (b) {
2225fcf5ef2aSThomas Huth         if (b & 1) {
2226fcf5ef2aSThomas Huth             resl ^= al;
2227fcf5ef2aSThomas Huth             resh ^= ah;
2228fcf5ef2aSThomas Huth         }
2229fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2230fcf5ef2aSThomas Huth         al <<= 1;
2231fcf5ef2aSThomas Huth         b >>= 1;
2232fcf5ef2aSThomas Huth     }
2233fcf5ef2aSThomas Huth 
22345a09df21SPaul Brook     *dest_l = resl;
22355a09df21SPaul Brook     *dest_h = resh;
22365a09df21SPaul Brook }
22375a09df21SPaul Brook #endif
22385a09df21SPaul Brook 
22395a09df21SPaul Brook void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
22405a09df21SPaul Brook                                     uint32_t ctrl)
22415a09df21SPaul Brook {
22425a09df21SPaul Brook     Reg *v = d;
22435a09df21SPaul Brook     uint64_t a, b;
22445a09df21SPaul Brook     int i;
22455a09df21SPaul Brook 
22465a09df21SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
22475a09df21SPaul Brook         a = v->Q(((ctrl & 1) != 0) + i);
22485a09df21SPaul Brook         b = s->Q(((ctrl & 16) != 0) + i);
22495a09df21SPaul Brook         clmulq(&d->Q(i), &d->Q(i + 1), a, b);
22505a09df21SPaul Brook     }
2251fcf5ef2aSThomas Huth }
2252fcf5ef2aSThomas Huth 
2253fcf5ef2aSThomas Huth void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2254fcf5ef2aSThomas Huth {
2255fcf5ef2aSThomas Huth     int i;
2256fcf5ef2aSThomas Huth     Reg st = *d;
2257fcf5ef2aSThomas Huth     Reg rk = *s;
2258fcf5ef2aSThomas Huth 
2259a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2260a64fc269SPaul Brook         int j = i & 3;
2261a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4 * j + 0])] ^
2262a64fc269SPaul Brook                                     AES_Td1[st.B(AES_ishifts[4 * j + 1])] ^
2263a64fc269SPaul Brook                                     AES_Td2[st.B(AES_ishifts[4 * j + 2])] ^
2264a64fc269SPaul Brook                                     AES_Td3[st.B(AES_ishifts[4 * j + 3])]);
2265fcf5ef2aSThomas Huth     }
2266fcf5ef2aSThomas Huth }
2267fcf5ef2aSThomas Huth 
2268fcf5ef2aSThomas Huth void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2269fcf5ef2aSThomas Huth {
2270fcf5ef2aSThomas Huth     int i;
2271fcf5ef2aSThomas Huth     Reg st = *d;
2272fcf5ef2aSThomas Huth     Reg rk = *s;
2273fcf5ef2aSThomas Huth 
2274a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2275a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i & 15] + (i & ~15))]);
2276fcf5ef2aSThomas Huth     }
2277fcf5ef2aSThomas Huth }
2278fcf5ef2aSThomas Huth 
2279fcf5ef2aSThomas Huth void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2280fcf5ef2aSThomas Huth {
2281fcf5ef2aSThomas Huth     int i;
2282fcf5ef2aSThomas Huth     Reg st = *d;
2283fcf5ef2aSThomas Huth     Reg rk = *s;
2284fcf5ef2aSThomas Huth 
2285a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2286a64fc269SPaul Brook         int j = i & 3;
2287a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4 * j + 0])] ^
2288a64fc269SPaul Brook                                     AES_Te1[st.B(AES_shifts[4 * j + 1])] ^
2289a64fc269SPaul Brook                                     AES_Te2[st.B(AES_shifts[4 * j + 2])] ^
2290a64fc269SPaul Brook                                     AES_Te3[st.B(AES_shifts[4 * j + 3])]);
2291fcf5ef2aSThomas Huth     }
2292fcf5ef2aSThomas Huth }
2293fcf5ef2aSThomas Huth 
2294fcf5ef2aSThomas Huth void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2295fcf5ef2aSThomas Huth {
2296fcf5ef2aSThomas Huth     int i;
2297fcf5ef2aSThomas Huth     Reg st = *d;
2298fcf5ef2aSThomas Huth     Reg rk = *s;
2299fcf5ef2aSThomas Huth 
2300a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2301a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]);
2302a64fc269SPaul Brook     }
2303fcf5ef2aSThomas Huth }
2304fcf5ef2aSThomas Huth 
2305a64fc269SPaul Brook #if SHIFT == 1
2306fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2307fcf5ef2aSThomas Huth {
2308fcf5ef2aSThomas Huth     int i;
2309fcf5ef2aSThomas Huth     Reg tmp = *s;
2310fcf5ef2aSThomas Huth 
2311fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2312fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4 * i + 0)][0] ^
2313fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 1)][1] ^
2314fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 2)][2] ^
2315fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 3)][3]);
2316fcf5ef2aSThomas Huth     }
2317fcf5ef2aSThomas Huth }
2318fcf5ef2aSThomas Huth 
2319fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2320fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2321fcf5ef2aSThomas Huth {
2322fcf5ef2aSThomas Huth     int i;
2323fcf5ef2aSThomas Huth     Reg tmp = *s;
2324fcf5ef2aSThomas Huth 
2325fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2326fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2327fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2328fcf5ef2aSThomas Huth     }
2329fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2330fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2331fcf5ef2aSThomas Huth }
2332fcf5ef2aSThomas Huth #endif
2333a64fc269SPaul Brook #endif
2334fcf5ef2aSThomas Huth 
23353403cafeSPaul Brook #undef SSE_HELPER_S
23363403cafeSPaul Brook 
2337fcf5ef2aSThomas Huth #undef SHIFT
2338fcf5ef2aSThomas Huth #undef XMM_ONLY
2339fcf5ef2aSThomas Huth #undef Reg
2340fcf5ef2aSThomas Huth #undef B
2341fcf5ef2aSThomas Huth #undef W
2342fcf5ef2aSThomas Huth #undef L
2343fcf5ef2aSThomas Huth #undef Q
2344fcf5ef2aSThomas Huth #undef SUFFIX
2345