xref: /openbmc/qemu/target/i386/ops_sse.h (revision cbf4ad5498039105db7c41e16e116b5b6584a786)
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
25d22697ddSPaolo Bonzini #define SIZE 8
26fcf5ef2aSThomas Huth #define XMM_ONLY(...)
27fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
28fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
29fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
30fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
31fcf5ef2aSThomas Huth #define SUFFIX _mmx
32fcf5ef2aSThomas Huth #else
33fcf5ef2aSThomas Huth #define Reg ZMMReg
34d22697ddSPaolo Bonzini #define SIZE 16
35fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
36fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
37fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
38fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
39fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
40fcf5ef2aSThomas Huth #define SUFFIX _xmm
41fcf5ef2aSThomas Huth #endif
42fcf5ef2aSThomas Huth 
4318592d2eSPaul Brook #define LANE_WIDTH (SHIFT ? 16 : 8)
44d45b0de6SPaul Brook #define PACK_WIDTH (LANE_WIDTH / 2)
4518592d2eSPaul Brook 
46d22697ddSPaolo Bonzini /*
47d22697ddSPaolo Bonzini  * Copy the relevant parts of a Reg value around. In the case where
48d22697ddSPaolo Bonzini  * sizeof(Reg) > SIZE, these helpers operate only on the lower bytes of
49d22697ddSPaolo Bonzini  * a 64 byte ZMMReg, so we must copy only those and keep the top bytes
50d22697ddSPaolo Bonzini  * untouched in the guest-visible destination destination register.
51d22697ddSPaolo Bonzini  * Note that the "lower bytes" are placed last in memory on big-endian
52d22697ddSPaolo Bonzini  * hosts, which store the vector backwards in memory.  In that case the
53d22697ddSPaolo Bonzini  * copy *starts* at B(SIZE - 1) and ends at B(0), the opposite of
54d22697ddSPaolo Bonzini  * the little-endian case.
55d22697ddSPaolo Bonzini  */
56d22697ddSPaolo Bonzini #if HOST_BIG_ENDIAN
57d22697ddSPaolo Bonzini #define MOVE(d, r) memcpy(&((d).B(SIZE - 1)), &(r).B(SIZE - 1), SIZE)
58d22697ddSPaolo Bonzini #else
59d22697ddSPaolo Bonzini #define MOVE(d, r) memcpy(&(d).B(0), &(r).B(0), SIZE)
60d22697ddSPaolo Bonzini #endif
61d22697ddSPaolo Bonzini 
6218592d2eSPaul Brook #if SHIFT == 0
6318592d2eSPaul Brook #define FPSRL(x, c) ((x) >> shift)
6418592d2eSPaul Brook #define FPSRAW(x, c) ((int16_t)(x) >> shift)
6518592d2eSPaul Brook #define FPSRAL(x, c) ((int32_t)(x) >> shift)
6618592d2eSPaul Brook #define FPSLL(x, c) ((x) << shift)
67fcf5ef2aSThomas Huth #endif
6818592d2eSPaul Brook 
6918592d2eSPaul Brook void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
7018592d2eSPaul Brook {
7118592d2eSPaul Brook     Reg *s = d;
7218592d2eSPaul Brook     int shift;
7318592d2eSPaul Brook     if (c->Q(0) > 15) {
7418592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
7518592d2eSPaul Brook             d->Q(i) = 0;
7618592d2eSPaul Brook         }
77fcf5ef2aSThomas Huth     } else {
7818592d2eSPaul Brook         shift = c->B(0);
7918592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
8018592d2eSPaul Brook             d->W(i) = FPSRL(s->W(i), shift);
8118592d2eSPaul Brook         }
82fcf5ef2aSThomas Huth     }
83fcf5ef2aSThomas Huth }
84fcf5ef2aSThomas Huth 
8518592d2eSPaul Brook void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
86fcf5ef2aSThomas Huth {
8718592d2eSPaul Brook     Reg *s = d;
88fcf5ef2aSThomas Huth     int shift;
8918592d2eSPaul Brook     if (c->Q(0) > 15) {
9018592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
9118592d2eSPaul Brook             d->Q(i) = 0;
9218592d2eSPaul Brook         }
9318592d2eSPaul Brook     } else {
9418592d2eSPaul Brook         shift = c->B(0);
9518592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
9618592d2eSPaul Brook             d->W(i) = FPSLL(s->W(i), shift);
9718592d2eSPaul Brook         }
9818592d2eSPaul Brook     }
9918592d2eSPaul Brook }
100fcf5ef2aSThomas Huth 
10118592d2eSPaul Brook void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
10218592d2eSPaul Brook {
10318592d2eSPaul Brook     Reg *s = d;
10418592d2eSPaul Brook     int shift;
10518592d2eSPaul Brook     if (c->Q(0) > 15) {
106fcf5ef2aSThomas Huth         shift = 15;
107fcf5ef2aSThomas Huth     } else {
10818592d2eSPaul Brook         shift = c->B(0);
109fcf5ef2aSThomas Huth     }
11018592d2eSPaul Brook     for (int i = 0; i < 4 << SHIFT; i++) {
11118592d2eSPaul Brook         d->W(i) = FPSRAW(s->W(i), shift);
11218592d2eSPaul Brook     }
113fcf5ef2aSThomas Huth }
114fcf5ef2aSThomas Huth 
11518592d2eSPaul Brook void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
116fcf5ef2aSThomas Huth {
11718592d2eSPaul Brook     Reg *s = d;
118fcf5ef2aSThomas Huth     int shift;
11918592d2eSPaul Brook     if (c->Q(0) > 31) {
12018592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
12118592d2eSPaul Brook             d->Q(i) = 0;
12218592d2eSPaul Brook         }
123fcf5ef2aSThomas Huth     } else {
12418592d2eSPaul Brook         shift = c->B(0);
12518592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
12618592d2eSPaul Brook             d->L(i) = FPSRL(s->L(i), shift);
12718592d2eSPaul Brook         }
128fcf5ef2aSThomas Huth     }
129fcf5ef2aSThomas Huth }
130fcf5ef2aSThomas Huth 
13118592d2eSPaul Brook void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
132fcf5ef2aSThomas Huth {
13318592d2eSPaul Brook     Reg *s = d;
134fcf5ef2aSThomas Huth     int shift;
13518592d2eSPaul Brook     if (c->Q(0) > 31) {
13618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
13718592d2eSPaul Brook             d->Q(i) = 0;
13818592d2eSPaul Brook         }
139fcf5ef2aSThomas Huth     } else {
14018592d2eSPaul Brook         shift = c->B(0);
14118592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
14218592d2eSPaul Brook             d->L(i) = FPSLL(s->L(i), shift);
14318592d2eSPaul Brook         }
144fcf5ef2aSThomas Huth     }
145fcf5ef2aSThomas Huth }
146fcf5ef2aSThomas Huth 
14718592d2eSPaul Brook void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
148fcf5ef2aSThomas Huth {
14918592d2eSPaul Brook     Reg *s = d;
150fcf5ef2aSThomas Huth     int shift;
15118592d2eSPaul Brook     if (c->Q(0) > 31) {
152fcf5ef2aSThomas Huth         shift = 31;
153fcf5ef2aSThomas Huth     } else {
15418592d2eSPaul Brook         shift = c->B(0);
155fcf5ef2aSThomas Huth     }
15618592d2eSPaul Brook     for (int i = 0; i < 2 << SHIFT; i++) {
15718592d2eSPaul Brook         d->L(i) = FPSRAL(s->L(i), shift);
15818592d2eSPaul Brook     }
159fcf5ef2aSThomas Huth }
160fcf5ef2aSThomas Huth 
16118592d2eSPaul Brook void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
162fcf5ef2aSThomas Huth {
16318592d2eSPaul Brook     Reg *s = d;
164fcf5ef2aSThomas Huth     int shift;
16518592d2eSPaul Brook     if (c->Q(0) > 63) {
16618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16718592d2eSPaul Brook             d->Q(i) = 0;
16818592d2eSPaul Brook         }
169fcf5ef2aSThomas Huth     } else {
17018592d2eSPaul Brook         shift = c->B(0);
17118592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
17218592d2eSPaul Brook             d->Q(i) = FPSRL(s->Q(i), shift);
17318592d2eSPaul Brook         }
174fcf5ef2aSThomas Huth     }
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
17718592d2eSPaul Brook void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
178fcf5ef2aSThomas Huth {
17918592d2eSPaul Brook     Reg *s = d;
180fcf5ef2aSThomas Huth     int shift;
18118592d2eSPaul Brook     if (c->Q(0) > 63) {
18218592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
18318592d2eSPaul Brook             d->Q(i) = 0;
18418592d2eSPaul Brook         }
185fcf5ef2aSThomas Huth     } else {
18618592d2eSPaul Brook         shift = c->B(0);
18718592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
18818592d2eSPaul Brook             d->Q(i) = FPSLL(s->Q(i), shift);
18918592d2eSPaul Brook         }
190fcf5ef2aSThomas Huth     }
191fcf5ef2aSThomas Huth }
192fcf5ef2aSThomas Huth 
19318592d2eSPaul Brook #if SHIFT >= 1
19418592d2eSPaul Brook void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
195fcf5ef2aSThomas Huth {
19618592d2eSPaul Brook     Reg *s = d;
19718592d2eSPaul Brook     int shift, i, j;
198fcf5ef2aSThomas Huth 
19918592d2eSPaul Brook     shift = c->L(0);
200fcf5ef2aSThomas Huth     if (shift > 16) {
201fcf5ef2aSThomas Huth         shift = 16;
202fcf5ef2aSThomas Huth     }
20318592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
204fcf5ef2aSThomas Huth         for (i = 0; i < 16 - shift; i++) {
20518592d2eSPaul Brook             d->B(j + i) = s->B(j + i + shift);
206fcf5ef2aSThomas Huth         }
207fcf5ef2aSThomas Huth         for (i = 16 - shift; i < 16; i++) {
20818592d2eSPaul Brook             d->B(j + i) = 0;
20918592d2eSPaul Brook         }
210fcf5ef2aSThomas Huth     }
211fcf5ef2aSThomas Huth }
212fcf5ef2aSThomas Huth 
21318592d2eSPaul Brook void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c)
214fcf5ef2aSThomas Huth {
21518592d2eSPaul Brook     Reg *s = d;
21618592d2eSPaul Brook     int shift, i, j;
217fcf5ef2aSThomas Huth 
21818592d2eSPaul Brook     shift = c->L(0);
219fcf5ef2aSThomas Huth     if (shift > 16) {
220fcf5ef2aSThomas Huth         shift = 16;
221fcf5ef2aSThomas Huth     }
22218592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
223fcf5ef2aSThomas Huth         for (i = 15; i >= shift; i--) {
22418592d2eSPaul Brook             d->B(j + i) = s->B(j + i - shift);
225fcf5ef2aSThomas Huth         }
226fcf5ef2aSThomas Huth         for (i = 0; i < shift; i++) {
22718592d2eSPaul Brook             d->B(j + i) = 0;
22818592d2eSPaul Brook         }
229fcf5ef2aSThomas Huth     }
230fcf5ef2aSThomas Huth }
231fcf5ef2aSThomas Huth #endif
232fcf5ef2aSThomas Huth 
233ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F)                        \
234fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
235fcf5ef2aSThomas Huth     {                                                           \
236ee04a3c8SPaul Brook         int n = num;                                            \
237ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
238ee04a3c8SPaul Brook             d->elem(i) = F(s->elem(i));                         \
239ee04a3c8SPaul Brook         }                                                       \
240fcf5ef2aSThomas Huth     }
241fcf5ef2aSThomas Huth 
242ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F)                        \
243ee04a3c8SPaul Brook     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
244ee04a3c8SPaul Brook     {                                                           \
245ee04a3c8SPaul Brook         Reg *v = d;                                             \
246ee04a3c8SPaul Brook         int n = num;                                            \
247ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
248ee04a3c8SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i));             \
249ee04a3c8SPaul Brook         }                                                       \
250ee04a3c8SPaul Brook     }
251ee04a3c8SPaul Brook 
252ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F)                                   \
253ee04a3c8SPaul Brook     SSE_HELPER_2(name, B, 8 << SHIFT, F)
254ee04a3c8SPaul Brook 
255fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
256ee04a3c8SPaul Brook     SSE_HELPER_2(name, W, 4 << SHIFT, F)
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
259ee04a3c8SPaul Brook     SSE_HELPER_2(name, L, 2 << SHIFT, F)
260fcf5ef2aSThomas Huth 
261fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
262ee04a3c8SPaul Brook     SSE_HELPER_2(name, Q, 1 << SHIFT, F)
263fcf5ef2aSThomas Huth 
264fcf5ef2aSThomas Huth #if SHIFT == 0
265fcf5ef2aSThomas Huth static inline int satub(int x)
266fcf5ef2aSThomas Huth {
267fcf5ef2aSThomas Huth     if (x < 0) {
268fcf5ef2aSThomas Huth         return 0;
269fcf5ef2aSThomas Huth     } else if (x > 255) {
270fcf5ef2aSThomas Huth         return 255;
271fcf5ef2aSThomas Huth     } else {
272fcf5ef2aSThomas Huth         return x;
273fcf5ef2aSThomas Huth     }
274fcf5ef2aSThomas Huth }
275fcf5ef2aSThomas Huth 
276fcf5ef2aSThomas Huth static inline int satuw(int x)
277fcf5ef2aSThomas Huth {
278fcf5ef2aSThomas Huth     if (x < 0) {
279fcf5ef2aSThomas Huth         return 0;
280fcf5ef2aSThomas Huth     } else if (x > 65535) {
281fcf5ef2aSThomas Huth         return 65535;
282fcf5ef2aSThomas Huth     } else {
283fcf5ef2aSThomas Huth         return x;
284fcf5ef2aSThomas Huth     }
285fcf5ef2aSThomas Huth }
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth static inline int satsb(int x)
288fcf5ef2aSThomas Huth {
289fcf5ef2aSThomas Huth     if (x < -128) {
290fcf5ef2aSThomas Huth         return -128;
291fcf5ef2aSThomas Huth     } else if (x > 127) {
292fcf5ef2aSThomas Huth         return 127;
293fcf5ef2aSThomas Huth     } else {
294fcf5ef2aSThomas Huth         return x;
295fcf5ef2aSThomas Huth     }
296fcf5ef2aSThomas Huth }
297fcf5ef2aSThomas Huth 
298fcf5ef2aSThomas Huth static inline int satsw(int x)
299fcf5ef2aSThomas Huth {
300fcf5ef2aSThomas Huth     if (x < -32768) {
301fcf5ef2aSThomas Huth         return -32768;
302fcf5ef2aSThomas Huth     } else if (x > 32767) {
303fcf5ef2aSThomas Huth         return 32767;
304fcf5ef2aSThomas Huth     } else {
305fcf5ef2aSThomas Huth         return x;
306fcf5ef2aSThomas Huth     }
307fcf5ef2aSThomas Huth }
308fcf5ef2aSThomas Huth 
309fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
310fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
311fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
312fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
313fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
314fcf5ef2aSThomas Huth 
315fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
316fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
317fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
318fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
319fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
320fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
321fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
322fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
323fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth #define FAND(a, b) ((a) & (b))
326fcf5ef2aSThomas Huth #define FANDN(a, b) ((~(a)) & (b))
327fcf5ef2aSThomas Huth #define FOR(a, b) ((a) | (b))
328fcf5ef2aSThomas Huth #define FXOR(a, b) ((a) ^ (b))
329fcf5ef2aSThomas Huth 
330fcf5ef2aSThomas Huth #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
331fcf5ef2aSThomas Huth #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
332fcf5ef2aSThomas Huth #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
333fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
334fcf5ef2aSThomas Huth 
335fcf5ef2aSThomas Huth #define FMULLW(a, b) ((a) * (b))
336fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
337fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
338fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
341fcf5ef2aSThomas Huth #endif
342fcf5ef2aSThomas Huth 
343fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddb, FADD)
344fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddw, FADD)
345fcf5ef2aSThomas Huth SSE_HELPER_L(helper_paddl, FADD)
346fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_paddq, FADD)
347fcf5ef2aSThomas Huth 
348fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubb, FSUB)
349fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubw, FSUB)
350fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psubl, FSUB)
351fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_psubq, FSUB)
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddusb, FADDUB)
354fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddsb, FADDSB)
355fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubusb, FSUBUB)
356fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubsb, FSUBSB)
357fcf5ef2aSThomas Huth 
358fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddusw, FADDUW)
359fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddsw, FADDSW)
360fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubusw, FSUBUW)
361fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubsw, FSUBSW)
362fcf5ef2aSThomas Huth 
363fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminub, FMINUB)
364fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxub, FMAXUB)
365fcf5ef2aSThomas Huth 
366fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminsw, FMINSW)
367fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxsw, FMAXSW)
368fcf5ef2aSThomas Huth 
369fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pand, FAND)
370fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pandn, FANDN)
371fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_por, FOR)
372fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pxor, FXOR)
373fcf5ef2aSThomas Huth 
374fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
375fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
376fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
377fcf5ef2aSThomas Huth 
378fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
379fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
380fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
381fcf5ef2aSThomas Huth 
382fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmullw, FMULLW)
383fcf5ef2aSThomas Huth #if SHIFT == 0
384fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrw, FMULHRW)
385fcf5ef2aSThomas Huth #endif
386fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
387fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
390fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
391fcf5ef2aSThomas Huth 
392fcf5ef2aSThomas Huth void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
393fcf5ef2aSThomas Huth {
394e894bae8SPaul Brook     Reg *v = d;
395e894bae8SPaul Brook     int i;
396e894bae8SPaul Brook 
397e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
398e894bae8SPaul Brook         d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2);
399e894bae8SPaul Brook     }
400fcf5ef2aSThomas Huth }
401fcf5ef2aSThomas Huth 
402fcf5ef2aSThomas Huth void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
403fcf5ef2aSThomas Huth {
404e894bae8SPaul Brook     Reg *v = d;
405fcf5ef2aSThomas Huth     int i;
406fcf5ef2aSThomas Huth 
407fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
408e894bae8SPaul Brook         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) +
409e894bae8SPaul Brook             (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1);
410fcf5ef2aSThomas Huth     }
411fcf5ef2aSThomas Huth }
412fcf5ef2aSThomas Huth 
413fcf5ef2aSThomas Huth #if SHIFT == 0
414fcf5ef2aSThomas Huth static inline int abs1(int a)
415fcf5ef2aSThomas Huth {
416fcf5ef2aSThomas Huth     if (a < 0) {
417fcf5ef2aSThomas Huth         return -a;
418fcf5ef2aSThomas Huth     } else {
419fcf5ef2aSThomas Huth         return a;
420fcf5ef2aSThomas Huth     }
421fcf5ef2aSThomas Huth }
422fcf5ef2aSThomas Huth #endif
423e894bae8SPaul Brook 
424fcf5ef2aSThomas Huth void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
425fcf5ef2aSThomas Huth {
426e894bae8SPaul Brook     Reg *v = d;
427e894bae8SPaul Brook     int i;
428fcf5ef2aSThomas Huth 
429e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
430e894bae8SPaul Brook         unsigned int val = 0;
431e894bae8SPaul Brook         val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0));
432e894bae8SPaul Brook         val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1));
433e894bae8SPaul Brook         val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2));
434e894bae8SPaul Brook         val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3));
435e894bae8SPaul Brook         val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4));
436e894bae8SPaul Brook         val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5));
437e894bae8SPaul Brook         val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6));
438e894bae8SPaul Brook         val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7));
439e894bae8SPaul Brook         d->Q(i) = val;
440e894bae8SPaul Brook     }
441fcf5ef2aSThomas Huth }
442fcf5ef2aSThomas Huth 
443fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
444fcf5ef2aSThomas Huth                                   target_ulong a0)
445fcf5ef2aSThomas Huth {
446fcf5ef2aSThomas Huth     int i;
447fcf5ef2aSThomas Huth 
448fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
449fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
450fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
451fcf5ef2aSThomas Huth         }
452fcf5ef2aSThomas Huth     }
453fcf5ef2aSThomas Huth }
454fcf5ef2aSThomas Huth 
455fcf5ef2aSThomas Huth void glue(helper_movl_mm_T0, SUFFIX)(Reg *d, uint32_t val)
456fcf5ef2aSThomas Huth {
457e894bae8SPaul Brook     int i;
458e894bae8SPaul Brook 
459fcf5ef2aSThomas Huth     d->L(0) = val;
460fcf5ef2aSThomas Huth     d->L(1) = 0;
461e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
462e894bae8SPaul Brook         d->Q(i) = 0;
463e894bae8SPaul Brook     }
464fcf5ef2aSThomas Huth }
465fcf5ef2aSThomas Huth 
466fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
467fcf5ef2aSThomas Huth void glue(helper_movq_mm_T0, SUFFIX)(Reg *d, uint64_t val)
468fcf5ef2aSThomas Huth {
469e894bae8SPaul Brook     int i;
470e894bae8SPaul Brook 
471fcf5ef2aSThomas Huth     d->Q(0) = val;
472e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
473e894bae8SPaul Brook         d->Q(i) = 0;
474e894bae8SPaul Brook     }
475fcf5ef2aSThomas Huth }
476fcf5ef2aSThomas Huth #endif
477fcf5ef2aSThomas Huth 
478d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
479d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
480d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
481d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
482d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
483d45b0de6SPaul Brook     d->F(offset) = r0;                      \
484d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
485d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
486d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
487d45b0de6SPaul Brook     } while (0)
488d45b0de6SPaul Brook 
489fcf5ef2aSThomas Huth #if SHIFT == 0
490fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
491fcf5ef2aSThomas Huth {
492d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
493fcf5ef2aSThomas Huth 
494d45b0de6SPaul Brook     SHUFFLE4(W, s, s, 0);
495fcf5ef2aSThomas Huth }
496fcf5ef2aSThomas Huth #else
497ce4fa29fSPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *s, int order)
498fcf5ef2aSThomas Huth {
499d45b0de6SPaul Brook     Reg *v = d;
500d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
501d45b0de6SPaul Brook     int i;
502fcf5ef2aSThomas Huth 
503d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
504d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
505d45b0de6SPaul Brook     }
506fcf5ef2aSThomas Huth }
507fcf5ef2aSThomas Huth 
508ce4fa29fSPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *s, int order)
509fcf5ef2aSThomas Huth {
510d45b0de6SPaul Brook     Reg *v = d;
511d45b0de6SPaul Brook     uint64_t r0, r1;
512d45b0de6SPaul Brook     int i;
513fcf5ef2aSThomas Huth 
514d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
515d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
516d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
517d45b0de6SPaul Brook         d->Q(i) = r0;
518d45b0de6SPaul Brook         d->Q(i + 1) = r1;
519d45b0de6SPaul Brook         order >>= 2;
520d45b0de6SPaul Brook     }
521fcf5ef2aSThomas Huth }
522fcf5ef2aSThomas Huth 
523fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
524fcf5ef2aSThomas Huth {
525d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
526d45b0de6SPaul Brook     int i;
527fcf5ef2aSThomas Huth 
528d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
529d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
530d45b0de6SPaul Brook     }
531fcf5ef2aSThomas Huth }
532fcf5ef2aSThomas Huth 
533fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
534fcf5ef2aSThomas Huth {
535d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
536d45b0de6SPaul Brook     int i, j;
537fcf5ef2aSThomas Huth 
538d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
539d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
540d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
541d45b0de6SPaul Brook     }
542fcf5ef2aSThomas Huth }
543fcf5ef2aSThomas Huth 
544fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
545fcf5ef2aSThomas Huth {
546d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
547d45b0de6SPaul Brook     int i, j;
548fcf5ef2aSThomas Huth 
549d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
550d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
551d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
552d45b0de6SPaul Brook     }
553fcf5ef2aSThomas Huth }
554fcf5ef2aSThomas Huth #endif
555fcf5ef2aSThomas Huth 
5563403cafeSPaul Brook #if SHIFT >= 1
557fcf5ef2aSThomas Huth /* FPU ops */
558fcf5ef2aSThomas Huth /* XXX: not accurate */
559fcf5ef2aSThomas Huth 
5603403cafeSPaul Brook #define SSE_HELPER_P(name, F)                                           \
5613403cafeSPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
5623403cafeSPaul Brook             Reg *d, Reg *s)                                             \
563fcf5ef2aSThomas Huth     {                                                                   \
5643403cafeSPaul Brook         Reg *v = d;                                                     \
5653403cafeSPaul Brook         int i;                                                          \
5663403cafeSPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
5673403cafeSPaul Brook             d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i));              \
568fcf5ef2aSThomas Huth         }                                                               \
5693403cafeSPaul Brook     }                                                                   \
5703403cafeSPaul Brook                                                                         \
5713403cafeSPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
5723403cafeSPaul Brook             Reg *d, Reg *s)                                     \
5733403cafeSPaul Brook     {                                                                   \
5743403cafeSPaul Brook         Reg *v = d;                                                     \
5753403cafeSPaul Brook         int i;                                                          \
5763403cafeSPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
5773403cafeSPaul Brook             d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i));              \
5783403cafeSPaul Brook         }                                                               \
5793403cafeSPaul Brook     }
5803403cafeSPaul Brook 
5813403cafeSPaul Brook #if SHIFT == 1
5823403cafeSPaul Brook 
5833403cafeSPaul Brook #define SSE_HELPER_S(name, F)                                           \
5843403cafeSPaul Brook     SSE_HELPER_P(name, F)                                               \
585fcf5ef2aSThomas Huth                                                                         \
586fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)\
587fcf5ef2aSThomas Huth     {                                                                   \
5883403cafeSPaul Brook         Reg *v = d;                                                     \
5893403cafeSPaul Brook         d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0));                  \
590fcf5ef2aSThomas Huth     }                                                                   \
591fcf5ef2aSThomas Huth                                                                         \
592fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)\
593fcf5ef2aSThomas Huth     {                                                                   \
5943403cafeSPaul Brook         Reg *v = d;                                                     \
5953403cafeSPaul Brook         d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0));                  \
596fcf5ef2aSThomas Huth     }
597fcf5ef2aSThomas Huth 
5983403cafeSPaul Brook #else
5993403cafeSPaul Brook 
6003403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F)
6013403cafeSPaul Brook 
6023403cafeSPaul Brook #endif
6033403cafeSPaul Brook 
604fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
605fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
606fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
607fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
608fcf5ef2aSThomas Huth 
609fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
610fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
611fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
612fcf5ef2aSThomas Huth  */
613fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
614fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
615fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
616fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
617fcf5ef2aSThomas Huth 
618fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
619fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
620fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
621fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
622fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
623fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
624fcf5ef2aSThomas Huth 
6253403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6263403cafeSPaul Brook {
6273403cafeSPaul Brook     int i;
6283403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
6293403cafeSPaul Brook         d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status);
6303403cafeSPaul Brook     }
6313403cafeSPaul Brook }
6323403cafeSPaul Brook 
6333403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6343403cafeSPaul Brook {
6353403cafeSPaul Brook     int i;
6363403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
6373403cafeSPaul Brook         d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status);
6383403cafeSPaul Brook     }
6393403cafeSPaul Brook }
6403403cafeSPaul Brook 
6413403cafeSPaul Brook #if SHIFT == 1
6423403cafeSPaul Brook void helper_sqrtss(CPUX86State *env, Reg *d, Reg *s)
6433403cafeSPaul Brook {
6443403cafeSPaul Brook     d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status);
6453403cafeSPaul Brook }
6463403cafeSPaul Brook 
6473403cafeSPaul Brook void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *s)
6483403cafeSPaul Brook {
6493403cafeSPaul Brook     d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status);
6503403cafeSPaul Brook }
6513403cafeSPaul Brook #endif
652fcf5ef2aSThomas Huth 
653fcf5ef2aSThomas Huth /* float to float conversions */
654ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
655fcf5ef2aSThomas Huth {
656fcf5ef2aSThomas Huth     float32 s0, s1;
657fcf5ef2aSThomas Huth 
658fcf5ef2aSThomas Huth     s0 = s->ZMM_S(0);
659fcf5ef2aSThomas Huth     s1 = s->ZMM_S(1);
660fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s0, &env->sse_status);
661fcf5ef2aSThomas Huth     d->ZMM_D(1) = float32_to_float64(s1, &env->sse_status);
662fcf5ef2aSThomas Huth }
663fcf5ef2aSThomas Huth 
664ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
665fcf5ef2aSThomas Huth {
666fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
667fcf5ef2aSThomas Huth     d->ZMM_S(1) = float64_to_float32(s->ZMM_D(1), &env->sse_status);
668fcf5ef2aSThomas Huth     d->Q(1) = 0;
669fcf5ef2aSThomas Huth }
670fcf5ef2aSThomas Huth 
671fcf5ef2aSThomas Huth void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *s)
672fcf5ef2aSThomas Huth {
673fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
674fcf5ef2aSThomas Huth }
675fcf5ef2aSThomas Huth 
676fcf5ef2aSThomas Huth void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *s)
677fcf5ef2aSThomas Huth {
678fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
679fcf5ef2aSThomas Huth }
680fcf5ef2aSThomas Huth 
681fcf5ef2aSThomas Huth /* integer to float */
682ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
683fcf5ef2aSThomas Huth {
684fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->ZMM_L(0), &env->sse_status);
685fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->ZMM_L(1), &env->sse_status);
686fcf5ef2aSThomas Huth     d->ZMM_S(2) = int32_to_float32(s->ZMM_L(2), &env->sse_status);
687fcf5ef2aSThomas Huth     d->ZMM_S(3) = int32_to_float32(s->ZMM_L(3), &env->sse_status);
688fcf5ef2aSThomas Huth }
689fcf5ef2aSThomas Huth 
690ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
691fcf5ef2aSThomas Huth {
692fcf5ef2aSThomas Huth     int32_t l0, l1;
693fcf5ef2aSThomas Huth 
694fcf5ef2aSThomas Huth     l0 = (int32_t)s->ZMM_L(0);
695fcf5ef2aSThomas Huth     l1 = (int32_t)s->ZMM_L(1);
696fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(l0, &env->sse_status);
697fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(l1, &env->sse_status);
698fcf5ef2aSThomas Huth }
699fcf5ef2aSThomas Huth 
700fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
701fcf5ef2aSThomas Huth {
702fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
703fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
704fcf5ef2aSThomas Huth }
705fcf5ef2aSThomas Huth 
706fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
707fcf5ef2aSThomas Huth {
708fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
709fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
710fcf5ef2aSThomas Huth }
711fcf5ef2aSThomas Huth 
712fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
713fcf5ef2aSThomas Huth {
714fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
715fcf5ef2aSThomas Huth }
716fcf5ef2aSThomas Huth 
717fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
718fcf5ef2aSThomas Huth {
719fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
720fcf5ef2aSThomas Huth }
721fcf5ef2aSThomas Huth 
722fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
723fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
724fcf5ef2aSThomas Huth {
725fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
726fcf5ef2aSThomas Huth }
727fcf5ef2aSThomas Huth 
728fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
729fcf5ef2aSThomas Huth {
730fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
731fcf5ef2aSThomas Huth }
732fcf5ef2aSThomas Huth #endif
733fcf5ef2aSThomas Huth 
734fcf5ef2aSThomas Huth /* float to integer */
7351e8a98b5SPeter Maydell 
7361e8a98b5SPeter Maydell /*
7371e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7381e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7391e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7401e8a98b5SPeter Maydell  */
7411e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7421e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7431e8a98b5SPeter Maydell     {                                                                   \
7441e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7451e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7461e8a98b5SPeter Maydell                                                                         \
7471e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7481e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7491e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7501e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7511e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7521e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7531e8a98b5SPeter Maydell         }                                                               \
7541e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7551e8a98b5SPeter Maydell         return r;                                                       \
7561e8a98b5SPeter Maydell     }
7571e8a98b5SPeter Maydell 
7581e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7591e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7601e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7611e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7621e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7631e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7641e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7651e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
7661e8a98b5SPeter Maydell 
767ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
768fcf5ef2aSThomas Huth {
7691e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7701e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
7711e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32(s->ZMM_S(2), &env->sse_status);
7721e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32(s->ZMM_S(3), &env->sse_status);
773fcf5ef2aSThomas Huth }
774fcf5ef2aSThomas Huth 
775ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
776fcf5ef2aSThomas Huth {
7771e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7781e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
779fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
780fcf5ef2aSThomas Huth }
781fcf5ef2aSThomas Huth 
782fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
783fcf5ef2aSThomas Huth {
7841e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7851e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
786fcf5ef2aSThomas Huth }
787fcf5ef2aSThomas Huth 
788fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
789fcf5ef2aSThomas Huth {
7901e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7911e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
792fcf5ef2aSThomas Huth }
793fcf5ef2aSThomas Huth 
794fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
795fcf5ef2aSThomas Huth {
7961e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
797fcf5ef2aSThomas Huth }
798fcf5ef2aSThomas Huth 
799fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
800fcf5ef2aSThomas Huth {
8011e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
802fcf5ef2aSThomas Huth }
803fcf5ef2aSThomas Huth 
804fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
805fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
806fcf5ef2aSThomas Huth {
8071e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
808fcf5ef2aSThomas Huth }
809fcf5ef2aSThomas Huth 
810fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
811fcf5ef2aSThomas Huth {
8121e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
813fcf5ef2aSThomas Huth }
814fcf5ef2aSThomas Huth #endif
815fcf5ef2aSThomas Huth 
816fcf5ef2aSThomas Huth /* float to integer truncated */
817ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
818fcf5ef2aSThomas Huth {
8191e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8201e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
8211e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32_round_to_zero(s->ZMM_S(2), &env->sse_status);
8221e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32_round_to_zero(s->ZMM_S(3), &env->sse_status);
823fcf5ef2aSThomas Huth }
824fcf5ef2aSThomas Huth 
825ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
826fcf5ef2aSThomas Huth {
8271e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8281e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
829fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
830fcf5ef2aSThomas Huth }
831fcf5ef2aSThomas Huth 
832fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
833fcf5ef2aSThomas Huth {
8341e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8351e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
836fcf5ef2aSThomas Huth }
837fcf5ef2aSThomas Huth 
838fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
839fcf5ef2aSThomas Huth {
8401e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8411e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
842fcf5ef2aSThomas Huth }
843fcf5ef2aSThomas Huth 
844fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
845fcf5ef2aSThomas Huth {
8461e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
847fcf5ef2aSThomas Huth }
848fcf5ef2aSThomas Huth 
849fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
850fcf5ef2aSThomas Huth {
8511e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
852fcf5ef2aSThomas Huth }
853fcf5ef2aSThomas Huth 
854fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
855fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
856fcf5ef2aSThomas Huth {
8571e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
858fcf5ef2aSThomas Huth }
859fcf5ef2aSThomas Huth 
860fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
861fcf5ef2aSThomas Huth {
8621e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
863fcf5ef2aSThomas Huth }
864fcf5ef2aSThomas Huth #endif
865fcf5ef2aSThomas Huth 
866ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
867fcf5ef2aSThomas Huth {
868418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8693403cafeSPaul Brook     int i;
8703403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8713403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one,
8723403cafeSPaul Brook                                   float32_sqrt(s->ZMM_S(i), &env->sse_status),
873fcf5ef2aSThomas Huth                                   &env->sse_status);
8743403cafeSPaul Brook     }
875418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
876fcf5ef2aSThomas Huth }
877fcf5ef2aSThomas Huth 
878fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
879fcf5ef2aSThomas Huth {
880418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
881fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
882fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
883fcf5ef2aSThomas Huth                               &env->sse_status);
884418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
885fcf5ef2aSThomas Huth }
886fcf5ef2aSThomas Huth 
887ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
888fcf5ef2aSThomas Huth {
889418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8903403cafeSPaul Brook     int i;
8913403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8923403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status);
8933403cafeSPaul Brook     }
894418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
895fcf5ef2aSThomas Huth }
896fcf5ef2aSThomas Huth 
897fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
898fcf5ef2aSThomas Huth {
899418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
900fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
901418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
902fcf5ef2aSThomas Huth }
903fcf5ef2aSThomas Huth 
904fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
905fcf5ef2aSThomas Huth {
906fcf5ef2aSThomas Huth     uint64_t mask;
907fcf5ef2aSThomas Huth 
908fcf5ef2aSThomas Huth     if (len == 0) {
909fcf5ef2aSThomas Huth         mask = ~0LL;
910fcf5ef2aSThomas Huth     } else {
911fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
912fcf5ef2aSThomas Huth     }
913fcf5ef2aSThomas Huth     return (src >> shift) & mask;
914fcf5ef2aSThomas Huth }
915fcf5ef2aSThomas Huth 
916fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
917fcf5ef2aSThomas Huth {
918fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1), s->ZMM_B(0));
919fcf5ef2aSThomas Huth }
920fcf5ef2aSThomas Huth 
921fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
922fcf5ef2aSThomas Huth {
923fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
924fcf5ef2aSThomas Huth }
925fcf5ef2aSThomas Huth 
926fcf5ef2aSThomas Huth static inline uint64_t helper_insertq(uint64_t src, int shift, int len)
927fcf5ef2aSThomas Huth {
928fcf5ef2aSThomas Huth     uint64_t mask;
929fcf5ef2aSThomas Huth 
930fcf5ef2aSThomas Huth     if (len == 0) {
931fcf5ef2aSThomas Huth         mask = ~0ULL;
932fcf5ef2aSThomas Huth     } else {
933fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
934fcf5ef2aSThomas Huth     }
935fcf5ef2aSThomas Huth     return (src & ~(mask << shift)) | ((src & mask) << shift);
936fcf5ef2aSThomas Huth }
937fcf5ef2aSThomas Huth 
938fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
939fcf5ef2aSThomas Huth {
940fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(s->ZMM_Q(0), s->ZMM_B(9), s->ZMM_B(8));
941fcf5ef2aSThomas Huth }
942fcf5ef2aSThomas Huth 
943fcf5ef2aSThomas Huth void helper_insertq_i(CPUX86State *env, ZMMReg *d, int index, int length)
944fcf5ef2aSThomas Huth {
945fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), index, length);
946fcf5ef2aSThomas Huth }
947fcf5ef2aSThomas Huth 
948ce4fa29fSPaolo Bonzini void glue(helper_haddps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
949fcf5ef2aSThomas Huth {
950fcf5ef2aSThomas Huth     ZMMReg r;
951fcf5ef2aSThomas Huth 
952fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_add(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
953fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_add(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
954fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_add(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
955fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_add(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
956d22697ddSPaolo Bonzini     MOVE(*d, r);
957fcf5ef2aSThomas Huth }
958fcf5ef2aSThomas Huth 
959ce4fa29fSPaolo Bonzini void glue(helper_haddpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
960fcf5ef2aSThomas Huth {
961fcf5ef2aSThomas Huth     ZMMReg r;
962fcf5ef2aSThomas Huth 
963fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_add(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
964fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_add(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
965d22697ddSPaolo Bonzini     MOVE(*d, r);
966fcf5ef2aSThomas Huth }
967fcf5ef2aSThomas Huth 
968ce4fa29fSPaolo Bonzini void glue(helper_hsubps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
969fcf5ef2aSThomas Huth {
970fcf5ef2aSThomas Huth     ZMMReg r;
971fcf5ef2aSThomas Huth 
972fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_sub(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
973fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_sub(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
974fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_sub(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
975fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_sub(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
976d22697ddSPaolo Bonzini     MOVE(*d, r);
977fcf5ef2aSThomas Huth }
978fcf5ef2aSThomas Huth 
979ce4fa29fSPaolo Bonzini void glue(helper_hsubpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
980fcf5ef2aSThomas Huth {
981fcf5ef2aSThomas Huth     ZMMReg r;
982fcf5ef2aSThomas Huth 
983fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_sub(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
984fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_sub(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
985d22697ddSPaolo Bonzini     MOVE(*d, r);
986fcf5ef2aSThomas Huth }
987fcf5ef2aSThomas Huth 
9883403cafeSPaul Brook void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
989fcf5ef2aSThomas Huth {
9903403cafeSPaul Brook     Reg *v = d;
9913403cafeSPaul Brook     int i;
9923403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 2) {
9933403cafeSPaul Brook         d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
9943403cafeSPaul Brook         d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
9953403cafeSPaul Brook     }
996fcf5ef2aSThomas Huth }
997fcf5ef2aSThomas Huth 
9983403cafeSPaul Brook void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
999fcf5ef2aSThomas Huth {
10003403cafeSPaul Brook     Reg *v = d;
10013403cafeSPaul Brook     int i;
10023403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
10033403cafeSPaul Brook         d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status);
10043403cafeSPaul Brook         d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status);
10053403cafeSPaul Brook     }
1006fcf5ef2aSThomas Huth }
1007fcf5ef2aSThomas Huth 
1008*cbf4ad54SPaul Brook #define SSE_HELPER_CMP_P(name, F, C)                                    \
1009*cbf4ad54SPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
1010*cbf4ad54SPaul Brook                                              Reg *d, Reg *s)    \
1011fcf5ef2aSThomas Huth     {                                                                   \
1012*cbf4ad54SPaul Brook         Reg *v = d;                                                     \
1013*cbf4ad54SPaul Brook         int i;                                                          \
1014*cbf4ad54SPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
1015*cbf4ad54SPaul Brook             d->ZMM_L(i) = C(F(32, v->ZMM_S(i), s->ZMM_S(i))) ? -1 : 0;  \
1016*cbf4ad54SPaul Brook         }                                                               \
1017fcf5ef2aSThomas Huth     }                                                                   \
1018fcf5ef2aSThomas Huth                                                                         \
1019*cbf4ad54SPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
1020*cbf4ad54SPaul Brook                                              Reg *d, Reg *s)    \
1021*cbf4ad54SPaul Brook     {                                                                   \
1022*cbf4ad54SPaul Brook         Reg *v = d;                                                     \
1023*cbf4ad54SPaul Brook         int i;                                                          \
1024*cbf4ad54SPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
1025*cbf4ad54SPaul Brook             d->ZMM_Q(i) = C(F(64, v->ZMM_D(i), s->ZMM_D(i))) ? -1 : 0;  \
1026*cbf4ad54SPaul Brook         }                                                               \
1027*cbf4ad54SPaul Brook     }
1028*cbf4ad54SPaul Brook 
1029*cbf4ad54SPaul Brook #if SHIFT == 1
1030*cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C)                                          \
1031*cbf4ad54SPaul Brook     SSE_HELPER_CMP_P(name, F, C)                                            \
1032fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)    \
1033fcf5ef2aSThomas Huth     {                                                                       \
1034*cbf4ad54SPaul Brook         Reg *v = d;                                                         \
1035*cbf4ad54SPaul Brook         d->ZMM_L(0) = C(F(32, v->ZMM_S(0), s->ZMM_S(0))) ? -1 : 0;          \
1036fcf5ef2aSThomas Huth     }                                                                       \
1037fcf5ef2aSThomas Huth                                                                             \
1038fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)    \
1039fcf5ef2aSThomas Huth     {                                                                       \
1040*cbf4ad54SPaul Brook         Reg *v = d;                                                         \
1041*cbf4ad54SPaul Brook         d->ZMM_Q(0) = C(F(64, v->ZMM_D(0), s->ZMM_D(0))) ? -1 : 0;          \
1042fcf5ef2aSThomas Huth     }
1043fcf5ef2aSThomas Huth 
1044*cbf4ad54SPaul Brook #define FPU_EQ(x) (x == float_relation_equal)
1045*cbf4ad54SPaul Brook #define FPU_LT(x) (x == float_relation_less)
1046*cbf4ad54SPaul Brook #define FPU_LE(x) (x <= float_relation_equal)
1047*cbf4ad54SPaul Brook #define FPU_UNORD(x) (x == float_relation_unordered)
1048fcf5ef2aSThomas Huth 
1049*cbf4ad54SPaul Brook #define FPU_CMPQ(size, a, b) \
1050*cbf4ad54SPaul Brook     float ## size ## _compare_quiet(a, b, &env->sse_status)
1051*cbf4ad54SPaul Brook #define FPU_CMPS(size, a, b) \
1052*cbf4ad54SPaul Brook     float ## size ## _compare(a, b, &env->sse_status)
1053*cbf4ad54SPaul Brook 
1054*cbf4ad54SPaul Brook #else
1055*cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C) SSE_HELPER_CMP_P(name, F, C)
1056*cbf4ad54SPaul Brook #endif
1057*cbf4ad54SPaul Brook 
1058*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpeq, FPU_CMPQ, FPU_EQ)
1059*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmplt, FPU_CMPS, FPU_LT)
1060*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmple, FPU_CMPS, FPU_LE)
1061*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpunord, FPU_CMPQ,  FPU_UNORD)
1062*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpneq, FPU_CMPQ, !FPU_EQ)
1063*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnlt, FPU_CMPS, !FPU_LT)
1064*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnle, FPU_CMPS, !FPU_LE)
1065*cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpord, FPU_CMPQ, !FPU_UNORD)
1066*cbf4ad54SPaul Brook 
1067*cbf4ad54SPaul Brook #undef SSE_HELPER_CMP
1068fcf5ef2aSThomas Huth 
1069fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1070fcf5ef2aSThomas Huth 
1071fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1072fcf5ef2aSThomas Huth {
107371bfd65cSRichard Henderson     FloatRelation ret;
1074fcf5ef2aSThomas Huth     float32 s0, s1;
1075fcf5ef2aSThomas Huth 
1076fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1077fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1078fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1079fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1080fcf5ef2aSThomas Huth }
1081fcf5ef2aSThomas Huth 
1082fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1083fcf5ef2aSThomas Huth {
108471bfd65cSRichard Henderson     FloatRelation ret;
1085fcf5ef2aSThomas Huth     float32 s0, s1;
1086fcf5ef2aSThomas Huth 
1087fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1088fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1089fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1090fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1091fcf5ef2aSThomas Huth }
1092fcf5ef2aSThomas Huth 
1093fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1094fcf5ef2aSThomas Huth {
109571bfd65cSRichard Henderson     FloatRelation ret;
1096fcf5ef2aSThomas Huth     float64 d0, d1;
1097fcf5ef2aSThomas Huth 
1098fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1099fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1100fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1101fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1102fcf5ef2aSThomas Huth }
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1105fcf5ef2aSThomas Huth {
110671bfd65cSRichard Henderson     FloatRelation ret;
1107fcf5ef2aSThomas Huth     float64 d0, d1;
1108fcf5ef2aSThomas Huth 
1109fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1110fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1111fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1112fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1113fcf5ef2aSThomas Huth }
1114fcf5ef2aSThomas Huth 
1115ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1116fcf5ef2aSThomas Huth {
1117fcf5ef2aSThomas Huth     int b0, b1, b2, b3;
1118fcf5ef2aSThomas Huth 
1119fcf5ef2aSThomas Huth     b0 = s->ZMM_L(0) >> 31;
1120fcf5ef2aSThomas Huth     b1 = s->ZMM_L(1) >> 31;
1121fcf5ef2aSThomas Huth     b2 = s->ZMM_L(2) >> 31;
1122fcf5ef2aSThomas Huth     b3 = s->ZMM_L(3) >> 31;
1123fcf5ef2aSThomas Huth     return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3);
1124fcf5ef2aSThomas Huth }
1125fcf5ef2aSThomas Huth 
1126ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1127fcf5ef2aSThomas Huth {
1128fcf5ef2aSThomas Huth     int b0, b1;
1129fcf5ef2aSThomas Huth 
1130fcf5ef2aSThomas Huth     b0 = s->ZMM_L(1) >> 31;
1131fcf5ef2aSThomas Huth     b1 = s->ZMM_L(3) >> 31;
1132fcf5ef2aSThomas Huth     return b0 | (b1 << 1);
1133fcf5ef2aSThomas Huth }
1134fcf5ef2aSThomas Huth 
1135fcf5ef2aSThomas Huth #endif
1136fcf5ef2aSThomas Huth 
1137fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1138fcf5ef2aSThomas Huth {
1139fcf5ef2aSThomas Huth     uint32_t val;
1140e894bae8SPaul Brook     int i;
1141fcf5ef2aSThomas Huth 
1142fcf5ef2aSThomas Huth     val = 0;
1143e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
1144e894bae8SPaul Brook         uint8_t byte = 0;
1145e894bae8SPaul Brook         byte |= (s->B(8 * i + 0) >> 7);
1146e894bae8SPaul Brook         byte |= (s->B(8 * i + 1) >> 6) & 0x02;
1147e894bae8SPaul Brook         byte |= (s->B(8 * i + 2) >> 5) & 0x04;
1148e894bae8SPaul Brook         byte |= (s->B(8 * i + 3) >> 4) & 0x08;
1149e894bae8SPaul Brook         byte |= (s->B(8 * i + 4) >> 3) & 0x10;
1150e894bae8SPaul Brook         byte |= (s->B(8 * i + 5) >> 2) & 0x20;
1151e894bae8SPaul Brook         byte |= (s->B(8 * i + 6) >> 1) & 0x40;
1152e894bae8SPaul Brook         byte |= (s->B(8 * i + 7)) & 0x80;
1153e894bae8SPaul Brook         val |= byte << (8 * i);
1154e894bae8SPaul Brook     }
1155fcf5ef2aSThomas Huth     return val;
1156fcf5ef2aSThomas Huth }
1157fcf5ef2aSThomas Huth 
1158d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1159d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1160d45b0de6SPaul Brook         Reg *d, Reg *s)                                       \
1161d45b0de6SPaul Brook {                                                             \
1162d45b0de6SPaul Brook     Reg *v = d;                                               \
1163d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1164d45b0de6SPaul Brook     int j, k;                                                 \
1165d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1166d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1167d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1168d45b0de6SPaul Brook         }                                                     \
1169d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1170d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1171d45b0de6SPaul Brook         }                                                     \
1172d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1173d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1174d45b0de6SPaul Brook         }                                                     \
1175d45b0de6SPaul Brook     }                                                         \
1176fcf5ef2aSThomas Huth }
1177fcf5ef2aSThomas Huth 
1178d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1179d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1180fcf5ef2aSThomas Huth 
1181fcf5ef2aSThomas Huth void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1182fcf5ef2aSThomas Huth {
1183d45b0de6SPaul Brook     Reg *v = d;
1184d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1185d45b0de6SPaul Brook     int j, k;
1186fcf5ef2aSThomas Huth 
1187d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1188d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1189d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1190d45b0de6SPaul Brook         }
1191d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1192d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1193d45b0de6SPaul Brook         }
1194d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1195d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1196d45b0de6SPaul Brook         }
1197d45b0de6SPaul Brook     }
1198fcf5ef2aSThomas Huth }
1199fcf5ef2aSThomas Huth 
1200fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1201fcf5ef2aSThomas Huth                                                                         \
1202fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1203fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1204fcf5ef2aSThomas Huth     {                                                                   \
1205d45b0de6SPaul Brook         Reg *v = d;                                                     \
1206d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1207d45b0de6SPaul Brook         int j, i;                                                       \
1208fcf5ef2aSThomas Huth                                                                         \
1209d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1210d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1211d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1212d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1213d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1214d45b0de6SPaul Brook             }                                                           \
1215d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1216d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1217d45b0de6SPaul Brook             }                                                           \
1218d45b0de6SPaul Brook         }                                                               \
1219fcf5ef2aSThomas Huth     }                                                                   \
1220fcf5ef2aSThomas Huth                                                                         \
1221fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1222fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1223fcf5ef2aSThomas Huth     {                                                                   \
1224d45b0de6SPaul Brook         Reg *v = d;                                                     \
1225d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1226d45b0de6SPaul Brook         int j, i;                                                       \
1227fcf5ef2aSThomas Huth                                                                         \
1228d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1229d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1230d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1231d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1232d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1233d45b0de6SPaul Brook             }                                                           \
1234d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1235d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1236d45b0de6SPaul Brook             }                                                           \
1237d45b0de6SPaul Brook         }                                                               \
1238fcf5ef2aSThomas Huth     }                                                                   \
1239fcf5ef2aSThomas Huth                                                                         \
1240fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1241fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1242fcf5ef2aSThomas Huth     {                                                                   \
1243d45b0de6SPaul Brook         Reg *v = d;                                                     \
1244d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1245d45b0de6SPaul Brook         int j, i;                                                       \
1246fcf5ef2aSThomas Huth                                                                         \
1247d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1248d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1249d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1250d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1251d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1252d45b0de6SPaul Brook             }                                                           \
1253d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1254d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1255d45b0de6SPaul Brook             }                                                           \
1256d45b0de6SPaul Brook         }                                                               \
1257fcf5ef2aSThomas Huth     }                                                                   \
1258fcf5ef2aSThomas Huth                                                                         \
1259fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1260d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1261d45b0de6SPaul Brook                         CPUX86State *env, Reg *d, Reg *s)       \
1262fcf5ef2aSThomas Huth              {                                                          \
1263d45b0de6SPaul Brook                  Reg *v = d;                                            \
1264d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1265d45b0de6SPaul Brook                  int i;                                                 \
1266fcf5ef2aSThomas Huth                                                                         \
1267d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1268d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1269d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1270d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1271d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1272d45b0de6SPaul Brook                  }                                                      \
1273fcf5ef2aSThomas Huth              }                                                          \
1274fcf5ef2aSThomas Huth                                                                         )
1275fcf5ef2aSThomas Huth 
1276fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1277fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1278fcf5ef2aSThomas Huth 
1279d45b0de6SPaul Brook #undef PACK_WIDTH
1280d45b0de6SPaul Brook #undef PACK_HELPER_B
1281d45b0de6SPaul Brook #undef UNPCK_OP
1282d45b0de6SPaul Brook 
1283d45b0de6SPaul Brook 
1284fcf5ef2aSThomas Huth /* 3DNow! float ops */
1285fcf5ef2aSThomas Huth #if SHIFT == 0
1286fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1287fcf5ef2aSThomas Huth {
1288fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1289fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1290fcf5ef2aSThomas Huth }
1291fcf5ef2aSThomas Huth 
1292fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1293fcf5ef2aSThomas Huth {
1294fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1295fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1296fcf5ef2aSThomas Huth }
1297fcf5ef2aSThomas Huth 
1298fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1299fcf5ef2aSThomas Huth {
1300fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1301fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1302fcf5ef2aSThomas Huth }
1303fcf5ef2aSThomas Huth 
1304fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1305fcf5ef2aSThomas Huth {
1306fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1307fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1308fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1309fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1310fcf5ef2aSThomas Huth }
1311fcf5ef2aSThomas Huth 
1312fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1313fcf5ef2aSThomas Huth {
131425bdec79SPaolo Bonzini     float32 r;
1315fcf5ef2aSThomas Huth 
131625bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
131725bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
131825bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1319fcf5ef2aSThomas Huth }
1320fcf5ef2aSThomas Huth 
1321fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1322fcf5ef2aSThomas Huth {
1323fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1324fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1325fcf5ef2aSThomas Huth }
1326fcf5ef2aSThomas Huth 
1327fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1328fcf5ef2aSThomas Huth {
1329fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1330fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1331fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1332fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1333fcf5ef2aSThomas Huth }
1334fcf5ef2aSThomas Huth 
1335fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1336fcf5ef2aSThomas Huth {
1337fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1338fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1339fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1340fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1341fcf5ef2aSThomas Huth }
1342fcf5ef2aSThomas Huth 
1343fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1344fcf5ef2aSThomas Huth {
1345fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1346fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1347fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1348fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1349fcf5ef2aSThomas Huth }
1350fcf5ef2aSThomas Huth 
1351fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1352fcf5ef2aSThomas Huth {
1353fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1354fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1355fcf5ef2aSThomas Huth     }
1356fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1357fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1358fcf5ef2aSThomas Huth     }
1359fcf5ef2aSThomas Huth }
1360fcf5ef2aSThomas Huth 
1361fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1362fcf5ef2aSThomas Huth {
1363fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1364fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1365fcf5ef2aSThomas Huth     }
1366fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1367fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1368fcf5ef2aSThomas Huth     }
1369fcf5ef2aSThomas Huth }
1370fcf5ef2aSThomas Huth 
1371fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1372fcf5ef2aSThomas Huth {
1373fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1374fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1375fcf5ef2aSThomas Huth }
1376fcf5ef2aSThomas Huth 
1377fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1378fcf5ef2aSThomas Huth {
137925bdec79SPaolo Bonzini     float32 r;
1380fcf5ef2aSThomas Huth 
138125bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
138225bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
138325bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1384fcf5ef2aSThomas Huth }
1385fcf5ef2aSThomas Huth 
1386fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1387fcf5ef2aSThomas Huth {
138825bdec79SPaolo Bonzini     float32 r;
1389fcf5ef2aSThomas Huth 
139025bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
139125bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
139225bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1393fcf5ef2aSThomas Huth }
1394fcf5ef2aSThomas Huth 
1395fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1396fcf5ef2aSThomas Huth {
1397fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1398fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1399fcf5ef2aSThomas Huth }
1400fcf5ef2aSThomas Huth 
1401fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1402fcf5ef2aSThomas Huth {
1403fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1404fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1405fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1406fcf5ef2aSThomas Huth                               &env->mmx_status);
1407fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1408fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1412fcf5ef2aSThomas Huth {
1413fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1414fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1415fcf5ef2aSThomas Huth }
1416fcf5ef2aSThomas Huth 
1417fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1418fcf5ef2aSThomas Huth {
1419fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1420fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1421fcf5ef2aSThomas Huth }
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1424fcf5ef2aSThomas Huth {
142525bdec79SPaolo Bonzini     uint32_t r;
1426fcf5ef2aSThomas Huth 
142725bdec79SPaolo Bonzini     r = s->MMX_L(0);
142825bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
142925bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1430fcf5ef2aSThomas Huth }
1431fcf5ef2aSThomas Huth #endif
1432fcf5ef2aSThomas Huth 
1433fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1434fcf5ef2aSThomas Huth void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1435fcf5ef2aSThomas Huth {
1436d45b0de6SPaul Brook     Reg *v = d;
1437fcf5ef2aSThomas Huth     int i;
1438d45b0de6SPaul Brook #if SHIFT == 0
1439d45b0de6SPaul Brook     uint8_t r[8];
1440fcf5ef2aSThomas Huth 
1441d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1442d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1443fcf5ef2aSThomas Huth     }
1444d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1445d45b0de6SPaul Brook         d->B(i) = r[i];
1446fcf5ef2aSThomas Huth     }
1447d45b0de6SPaul Brook #else
1448d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1449fcf5ef2aSThomas Huth 
1450d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1451d45b0de6SPaul Brook         int j = i & ~0xf;
1452d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1453fcf5ef2aSThomas Huth     }
1454d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1455d45b0de6SPaul Brook         d->B(i) = r[i];
1456fcf5ef2aSThomas Huth     }
1457fcf5ef2aSThomas Huth #endif
1458fcf5ef2aSThomas Huth }
1459fcf5ef2aSThomas Huth 
1460d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1461d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1462d45b0de6SPaul Brook {                                                          \
1463d45b0de6SPaul Brook     Reg *v = d;                                            \
1464d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1465d45b0de6SPaul Brook     int i, j, k;                                           \
1466d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1467d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1468d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1469d45b0de6SPaul Brook         }                                                  \
1470d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1471d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1472d45b0de6SPaul Brook         }                                                  \
1473d45b0de6SPaul Brook     }                                                      \
1474d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1475d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1476d45b0de6SPaul Brook     }                                                      \
1477fcf5ef2aSThomas Huth }
1478fcf5ef2aSThomas Huth 
1479d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1480d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1481d45b0de6SPaul Brook {                                                          \
1482d45b0de6SPaul Brook     Reg *v = d;                                            \
1483d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1484d45b0de6SPaul Brook     int i, j, k;                                           \
1485d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1486d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1487d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1488d45b0de6SPaul Brook         }                                                  \
1489d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1490d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1491d45b0de6SPaul Brook         }                                                  \
1492d45b0de6SPaul Brook     }                                                      \
1493d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1494d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1495d45b0de6SPaul Brook     }                                                      \
1496fcf5ef2aSThomas Huth }
1497fcf5ef2aSThomas Huth 
1498d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1499d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1500d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1501d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1502d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1503d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
150475046ad7SPaolo Bonzini 
1505d45b0de6SPaul Brook #undef SSE_HELPER_HW
1506d45b0de6SPaul Brook #undef SSE_HELPER_HL
1507d45b0de6SPaul Brook 
1508d45b0de6SPaul Brook void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1509d45b0de6SPaul Brook {
1510d45b0de6SPaul Brook     Reg *v = d;
1511d45b0de6SPaul Brook     int i;
1512d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1513d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1514d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1515d45b0de6SPaul Brook     }
1516fcf5ef2aSThomas Huth }
1517fcf5ef2aSThomas Huth 
1518ee04a3c8SPaul Brook #define FABSB(x) (x > INT8_MAX  ? -(int8_t)x : x)
1519ee04a3c8SPaul Brook #define FABSW(x) (x > INT16_MAX ? -(int16_t)x : x)
1520ee04a3c8SPaul Brook #define FABSL(x) (x > INT32_MAX ? -(int32_t)x : x)
1521ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsb, B, 8 << SHIFT, FABSB)
1522ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsw, W, 4 << SHIFT, FABSW)
1523ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsd, L, 2 << SHIFT, FABSL)
1524fcf5ef2aSThomas Huth 
1525fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1526fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1527fcf5ef2aSThomas Huth 
1528fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1529fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1530fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1531fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1532fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1533fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1534fcf5ef2aSThomas Huth 
1535fcf5ef2aSThomas Huth void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1536fcf5ef2aSThomas Huth                                   int32_t shift)
1537fcf5ef2aSThomas Huth {
1538d45b0de6SPaul Brook     Reg *v = d;
1539d45b0de6SPaul Brook     int i;
1540fcf5ef2aSThomas Huth 
1541fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1542d45b0de6SPaul Brook     if (shift >= (SHIFT ? 32 : 16)) {
1543d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1544d45b0de6SPaul Brook             d->Q(i) = 0;
1545d45b0de6SPaul Brook         }
1546fcf5ef2aSThomas Huth     } else {
1547fcf5ef2aSThomas Huth         shift <<= 3;
1548fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1549fcf5ef2aSThomas Huth #if SHIFT == 0
1550d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1551d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1552fcf5ef2aSThomas Huth #else
1553d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1554d45b0de6SPaul Brook             uint64_t r0, r1;
1555d45b0de6SPaul Brook 
1556d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1557d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1558d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1559d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1560d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1561d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1562d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1563d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1564d45b0de6SPaul Brook             d->Q(i) = r0;
1565d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1566d45b0de6SPaul Brook         }
1567fcf5ef2aSThomas Huth #endif
1568fcf5ef2aSThomas Huth #undef SHR
1569fcf5ef2aSThomas Huth     }
1570fcf5ef2aSThomas Huth }
1571fcf5ef2aSThomas Huth 
1572fcf5ef2aSThomas Huth #define XMM0 (env->xmm_regs[0])
1573fcf5ef2aSThomas Huth 
1574fcf5ef2aSThomas Huth #if SHIFT == 1
1575fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1576fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)           \
1577fcf5ef2aSThomas Huth     {                                                                   \
1578fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0));           \
1579fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1));           \
1580fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1581fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2));       \
1582fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3));       \
1583fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1584fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4));   \
1585fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5));   \
1586fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6));   \
1587fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7));   \
1588fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1589fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \
1590fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \
1591fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \
1592fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \
1593fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \
1594fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \
1595fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \
1596fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \
1597fcf5ef2aSThomas Huth                 }                                                       \
1598fcf5ef2aSThomas Huth             }                                                           \
1599fcf5ef2aSThomas Huth         }                                                               \
1600fcf5ef2aSThomas Huth     }
1601fcf5ef2aSThomas Huth 
1602fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1603fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \
1604fcf5ef2aSThomas Huth     {                                                                   \
1605fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1));       \
1606fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1));       \
1607fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1608fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1));   \
1609fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1));   \
1610fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1611fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \
1612fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \
1613fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \
1614fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \
1615fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1616fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \
1617fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \
1618fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10),           \
1619fcf5ef2aSThomas Huth                                     ((imm >> 10) & 1));                 \
1620fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11),           \
1621fcf5ef2aSThomas Huth                                     ((imm >> 11) & 1));                 \
1622fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12),           \
1623fcf5ef2aSThomas Huth                                     ((imm >> 12) & 1));                 \
1624fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13),           \
1625fcf5ef2aSThomas Huth                                     ((imm >> 13) & 1));                 \
1626fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14),           \
1627fcf5ef2aSThomas Huth                                     ((imm >> 14) & 1));                 \
1628fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15),           \
1629fcf5ef2aSThomas Huth                                     ((imm >> 15) & 1));                 \
1630fcf5ef2aSThomas Huth                 }                                                       \
1631fcf5ef2aSThomas Huth             }                                                           \
1632fcf5ef2aSThomas Huth         }                                                               \
1633fcf5ef2aSThomas Huth     }
1634fcf5ef2aSThomas Huth 
1635fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
1636fcf5ef2aSThomas Huth #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d)
1637fcf5ef2aSThomas Huth #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d)
1638fcf5ef2aSThomas Huth #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d)
1639fcf5ef2aSThomas Huth SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB)
1640fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS)
1641fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD)
1642fcf5ef2aSThomas Huth 
1643fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1644fcf5ef2aSThomas Huth {
1645e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1646e894bae8SPaul Brook     int i;
1647fcf5ef2aSThomas Huth 
1648e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1649e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1650e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1651e894bae8SPaul Brook     }
1652fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1653fcf5ef2aSThomas Huth }
1654fcf5ef2aSThomas Huth 
1655fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1656fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1657fcf5ef2aSThomas Huth     {                                                           \
1658e894bae8SPaul Brook         int n = num;                                            \
1659e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1660e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1661fcf5ef2aSThomas Huth         }                                                       \
1662fcf5ef2aSThomas Huth     }
1663fcf5ef2aSThomas Huth 
1664e894bae8SPaul Brook #if SHIFT > 0
1665e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1666e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1667e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1668e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1669e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1670e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1671e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1672e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1673e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1674e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1675e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1676e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
1677e894bae8SPaul Brook #endif
1678fcf5ef2aSThomas Huth 
1679fcf5ef2aSThomas Huth void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1680fcf5ef2aSThomas Huth {
1681e894bae8SPaul Brook     Reg *v = d;
1682e894bae8SPaul Brook     int i;
1683e894bae8SPaul Brook 
1684e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1685e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1686e894bae8SPaul Brook     }
1687fcf5ef2aSThomas Huth }
1688fcf5ef2aSThomas Huth 
1689fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1690fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1691fcf5ef2aSThomas Huth 
1692fcf5ef2aSThomas Huth void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1693fcf5ef2aSThomas Huth {
1694d45b0de6SPaul Brook     Reg *v = d;
1695d45b0de6SPaul Brook     uint16_t r[8];
1696d45b0de6SPaul Brook     int i, j, k;
169780e19606SJoseph Myers 
1698d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1699d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1700d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1701d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1702d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1703d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1704d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1705d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1706d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1707d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1708d45b0de6SPaul Brook             d->W(i + k) = r[k];
1709d45b0de6SPaul Brook         }
1710d45b0de6SPaul Brook     }
1711fcf5ef2aSThomas Huth }
1712fcf5ef2aSThomas Huth 
1713fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1714fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1715fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1716fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1717fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1718fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1719fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1720fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1721fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1722fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1723fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1724fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1725fcf5ef2aSThomas Huth 
1726fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1727fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1728fcf5ef2aSThomas Huth 
1729fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1730fcf5ef2aSThomas Huth {
1731fcf5ef2aSThomas Huth     int idx = 0;
1732fcf5ef2aSThomas Huth 
1733fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1734fcf5ef2aSThomas Huth         idx = 1;
1735fcf5ef2aSThomas Huth     }
1736fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1737fcf5ef2aSThomas Huth         idx = 2;
1738fcf5ef2aSThomas Huth     }
1739fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1740fcf5ef2aSThomas Huth         idx = 3;
1741fcf5ef2aSThomas Huth     }
1742fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1743fcf5ef2aSThomas Huth         idx = 4;
1744fcf5ef2aSThomas Huth     }
1745fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1746fcf5ef2aSThomas Huth         idx = 5;
1747fcf5ef2aSThomas Huth     }
1748fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1749fcf5ef2aSThomas Huth         idx = 6;
1750fcf5ef2aSThomas Huth     }
1751fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1752fcf5ef2aSThomas Huth         idx = 7;
1753fcf5ef2aSThomas Huth     }
1754fcf5ef2aSThomas Huth 
1755fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1756aa406feaSJoseph Myers     d->W(1) = idx;
1757aa406feaSJoseph Myers     d->L(1) = 0;
1758aa406feaSJoseph Myers     d->Q(1) = 0;
1759fcf5ef2aSThomas Huth }
1760fcf5ef2aSThomas Huth 
1761fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1762fcf5ef2aSThomas Huth                                   uint32_t mode)
1763fcf5ef2aSThomas Huth {
1764418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1765fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1766fcf5ef2aSThomas Huth 
1767fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1768fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1769fcf5ef2aSThomas Huth         switch (mode & 3) {
1770fcf5ef2aSThomas Huth         case 0:
1771fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1772fcf5ef2aSThomas Huth             break;
1773fcf5ef2aSThomas Huth         case 1:
1774fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1775fcf5ef2aSThomas Huth             break;
1776fcf5ef2aSThomas Huth         case 2:
1777fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1778fcf5ef2aSThomas Huth             break;
1779fcf5ef2aSThomas Huth         case 3:
1780fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1781fcf5ef2aSThomas Huth             break;
1782fcf5ef2aSThomas Huth         }
1783fcf5ef2aSThomas Huth     }
1784fcf5ef2aSThomas Huth 
1785fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1786fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_round_to_int(s->ZMM_S(1), &env->sse_status);
1787fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_round_to_int(s->ZMM_S(2), &env->sse_status);
1788fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_round_to_int(s->ZMM_S(3), &env->sse_status);
1789fcf5ef2aSThomas Huth 
1790418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1791fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1792fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1793fcf5ef2aSThomas Huth                                   &env->sse_status);
1794fcf5ef2aSThomas Huth     }
1795fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1796fcf5ef2aSThomas Huth }
1797fcf5ef2aSThomas Huth 
1798fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1799fcf5ef2aSThomas Huth                                   uint32_t mode)
1800fcf5ef2aSThomas Huth {
1801418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1802fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1803fcf5ef2aSThomas Huth 
1804fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1805fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1806fcf5ef2aSThomas Huth         switch (mode & 3) {
1807fcf5ef2aSThomas Huth         case 0:
1808fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1809fcf5ef2aSThomas Huth             break;
1810fcf5ef2aSThomas Huth         case 1:
1811fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1812fcf5ef2aSThomas Huth             break;
1813fcf5ef2aSThomas Huth         case 2:
1814fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1815fcf5ef2aSThomas Huth             break;
1816fcf5ef2aSThomas Huth         case 3:
1817fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1818fcf5ef2aSThomas Huth             break;
1819fcf5ef2aSThomas Huth         }
1820fcf5ef2aSThomas Huth     }
1821fcf5ef2aSThomas Huth 
1822fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1823fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_round_to_int(s->ZMM_D(1), &env->sse_status);
1824fcf5ef2aSThomas Huth 
1825418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1826fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1827fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1828fcf5ef2aSThomas Huth                                   &env->sse_status);
1829fcf5ef2aSThomas Huth     }
1830fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1831fcf5ef2aSThomas Huth }
1832fcf5ef2aSThomas Huth 
1833fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1834fcf5ef2aSThomas Huth                                   uint32_t mode)
1835fcf5ef2aSThomas Huth {
1836418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1837fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1838fcf5ef2aSThomas Huth 
1839fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1840fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1841fcf5ef2aSThomas Huth         switch (mode & 3) {
1842fcf5ef2aSThomas Huth         case 0:
1843fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1844fcf5ef2aSThomas Huth             break;
1845fcf5ef2aSThomas Huth         case 1:
1846fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1847fcf5ef2aSThomas Huth             break;
1848fcf5ef2aSThomas Huth         case 2:
1849fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1850fcf5ef2aSThomas Huth             break;
1851fcf5ef2aSThomas Huth         case 3:
1852fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1853fcf5ef2aSThomas Huth             break;
1854fcf5ef2aSThomas Huth         }
1855fcf5ef2aSThomas Huth     }
1856fcf5ef2aSThomas Huth 
1857fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1858fcf5ef2aSThomas Huth 
1859418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1860fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1861fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1862fcf5ef2aSThomas Huth                                   &env->sse_status);
1863fcf5ef2aSThomas Huth     }
1864fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1865fcf5ef2aSThomas Huth }
1866fcf5ef2aSThomas Huth 
1867fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1868fcf5ef2aSThomas Huth                                   uint32_t mode)
1869fcf5ef2aSThomas Huth {
1870418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1871fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1872fcf5ef2aSThomas Huth 
1873fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1874fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1875fcf5ef2aSThomas Huth         switch (mode & 3) {
1876fcf5ef2aSThomas Huth         case 0:
1877fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1878fcf5ef2aSThomas Huth             break;
1879fcf5ef2aSThomas Huth         case 1:
1880fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1881fcf5ef2aSThomas Huth             break;
1882fcf5ef2aSThomas Huth         case 2:
1883fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1884fcf5ef2aSThomas Huth             break;
1885fcf5ef2aSThomas Huth         case 3:
1886fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1887fcf5ef2aSThomas Huth             break;
1888fcf5ef2aSThomas Huth         }
1889fcf5ef2aSThomas Huth     }
1890fcf5ef2aSThomas Huth 
1891fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1892fcf5ef2aSThomas Huth 
1893418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1894fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1895fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1896fcf5ef2aSThomas Huth                                   &env->sse_status);
1897fcf5ef2aSThomas Huth     }
1898fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1899fcf5ef2aSThomas Huth }
1900fcf5ef2aSThomas Huth 
1901fcf5ef2aSThomas Huth #define FBLENDP(d, s, m) (m ? s : d)
1902fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendps, L, 4, FBLENDP)
1903fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP)
1904fcf5ef2aSThomas Huth SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)
1905fcf5ef2aSThomas Huth 
1906fcf5ef2aSThomas Huth void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1907fcf5ef2aSThomas Huth {
1908bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
1909fcf5ef2aSThomas Huth 
1910bf30ad8cSPaolo Bonzini     /*
1911bf30ad8cSPaolo Bonzini      * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1912bf30ad8cSPaolo Bonzini      * to correctly round the intermediate results
1913bf30ad8cSPaolo Bonzini      */
1914fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1915bf30ad8cSPaolo Bonzini         prod1 = float32_mul(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status);
1916bf30ad8cSPaolo Bonzini     } else {
1917bf30ad8cSPaolo Bonzini         prod1 = float32_zero;
1918fcf5ef2aSThomas Huth     }
1919fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1920bf30ad8cSPaolo Bonzini         prod2 = float32_mul(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status);
1921bf30ad8cSPaolo Bonzini     } else {
1922bf30ad8cSPaolo Bonzini         prod2 = float32_zero;
1923fcf5ef2aSThomas Huth     }
1924bf30ad8cSPaolo Bonzini     temp2 = float32_add(prod1, prod2, &env->sse_status);
1925fcf5ef2aSThomas Huth     if (mask & (1 << 6)) {
1926bf30ad8cSPaolo Bonzini         prod1 = float32_mul(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status);
1927bf30ad8cSPaolo Bonzini     } else {
1928bf30ad8cSPaolo Bonzini         prod1 = float32_zero;
1929fcf5ef2aSThomas Huth     }
1930fcf5ef2aSThomas Huth     if (mask & (1 << 7)) {
1931bf30ad8cSPaolo Bonzini         prod2 = float32_mul(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status);
1932bf30ad8cSPaolo Bonzini     } else {
1933bf30ad8cSPaolo Bonzini         prod2 = float32_zero;
1934fcf5ef2aSThomas Huth     }
1935bf30ad8cSPaolo Bonzini     temp3 = float32_add(prod1, prod2, &env->sse_status);
1936bf30ad8cSPaolo Bonzini     temp4 = float32_add(temp2, temp3, &env->sse_status);
1937bf30ad8cSPaolo Bonzini 
1938bf30ad8cSPaolo Bonzini     d->ZMM_S(0) = (mask & (1 << 0)) ? temp4 : float32_zero;
1939bf30ad8cSPaolo Bonzini     d->ZMM_S(1) = (mask & (1 << 1)) ? temp4 : float32_zero;
1940bf30ad8cSPaolo Bonzini     d->ZMM_S(2) = (mask & (1 << 2)) ? temp4 : float32_zero;
1941bf30ad8cSPaolo Bonzini     d->ZMM_S(3) = (mask & (1 << 3)) ? temp4 : float32_zero;
1942fcf5ef2aSThomas Huth }
1943fcf5ef2aSThomas Huth 
1944fcf5ef2aSThomas Huth void glue(helper_dppd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1945fcf5ef2aSThomas Huth {
1946bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1947fcf5ef2aSThomas Huth 
1948fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1949bf30ad8cSPaolo Bonzini         prod1 = float64_mul(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1950bf30ad8cSPaolo Bonzini     } else {
1951bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1952fcf5ef2aSThomas Huth     }
1953fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1954bf30ad8cSPaolo Bonzini         prod2 = float64_mul(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1955bf30ad8cSPaolo Bonzini     } else {
1956bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1957fcf5ef2aSThomas Huth     }
1958bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1959bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1960bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1961fcf5ef2aSThomas Huth }
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 
1991fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1992fcf5ef2aSThomas Huth {
1993d1da229fSPaul Brook     target_long val, limit;
1994fcf5ef2aSThomas Huth 
1995fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1996fcf5ef2aSThomas Huth     if (ctrl >> 8) {
1997d1da229fSPaul Brook         val = (target_long)env->regs[reg];
1998fcf5ef2aSThomas Huth     } else {
1999d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
2000fcf5ef2aSThomas Huth     }
2001fcf5ef2aSThomas Huth     if (ctrl & 1) {
2002d1da229fSPaul Brook         limit = 8;
2003fcf5ef2aSThomas Huth     } else {
2004d1da229fSPaul Brook         limit = 16;
2005fcf5ef2aSThomas Huth     }
2006d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
2007d1da229fSPaul Brook         return limit;
2008fcf5ef2aSThomas Huth     }
2009d1da229fSPaul Brook     return abs1(val);
2010fcf5ef2aSThomas Huth }
2011fcf5ef2aSThomas Huth 
2012fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
2013fcf5ef2aSThomas Huth {
2014fcf5ef2aSThomas Huth     int val = 0;
2015fcf5ef2aSThomas Huth 
2016fcf5ef2aSThomas Huth     if (ctrl & 1) {
2017fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
2018fcf5ef2aSThomas Huth             val++;
2019fcf5ef2aSThomas Huth         }
2020fcf5ef2aSThomas Huth     } else {
2021fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
2022fcf5ef2aSThomas Huth             val++;
2023fcf5ef2aSThomas Huth         }
2024fcf5ef2aSThomas Huth     }
2025fcf5ef2aSThomas Huth 
2026fcf5ef2aSThomas Huth     return val;
2027fcf5ef2aSThomas Huth }
2028fcf5ef2aSThomas Huth 
2029fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
2030fcf5ef2aSThomas Huth {
2031fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
2032fcf5ef2aSThomas Huth     case 0:
2033fcf5ef2aSThomas Huth         return r->B(i);
2034fcf5ef2aSThomas Huth     case 1:
2035fcf5ef2aSThomas Huth         return r->W(i);
2036fcf5ef2aSThomas Huth     case 2:
2037fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
2038fcf5ef2aSThomas Huth     case 3:
2039fcf5ef2aSThomas Huth     default:
2040fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
2041fcf5ef2aSThomas Huth     }
2042fcf5ef2aSThomas Huth }
2043fcf5ef2aSThomas Huth 
2044fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
2045fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
2046fcf5ef2aSThomas Huth {
2047fcf5ef2aSThomas Huth     unsigned int res = 0;
2048fcf5ef2aSThomas Huth     int v;
2049fcf5ef2aSThomas Huth     int j, i;
2050fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
2051fcf5ef2aSThomas Huth 
2052fcf5ef2aSThomas Huth     valids--;
2053fcf5ef2aSThomas Huth     validd--;
2054fcf5ef2aSThomas Huth 
2055fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2056fcf5ef2aSThomas Huth 
2057fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2058fcf5ef2aSThomas Huth     case 0:
2059fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2060fcf5ef2aSThomas Huth             res <<= 1;
2061fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2062fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2063fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2064fcf5ef2aSThomas Huth             }
2065fcf5ef2aSThomas Huth         }
2066fcf5ef2aSThomas Huth         break;
2067fcf5ef2aSThomas Huth     case 1:
2068fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2069fcf5ef2aSThomas Huth             res <<= 1;
2070fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2071fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2072fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2073fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2074fcf5ef2aSThomas Huth             }
2075fcf5ef2aSThomas Huth         }
2076fcf5ef2aSThomas Huth         break;
2077fcf5ef2aSThomas Huth     case 2:
2078fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2079fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2080fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2081fcf5ef2aSThomas Huth             res <<= 1;
2082fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2083fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2084fcf5ef2aSThomas Huth         }
2085fcf5ef2aSThomas Huth         break;
2086fcf5ef2aSThomas Huth     case 3:
2087ae35eea7SJoseph Myers         if (validd == -1) {
2088ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2089ae35eea7SJoseph Myers             break;
2090ae35eea7SJoseph Myers         }
2091bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2092fcf5ef2aSThomas Huth             res <<= 1;
2093fcf5ef2aSThomas Huth             v = 1;
2094bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2095fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2096fcf5ef2aSThomas Huth             }
2097fcf5ef2aSThomas Huth             res |= v;
2098fcf5ef2aSThomas Huth         }
2099fcf5ef2aSThomas Huth         break;
2100fcf5ef2aSThomas Huth     }
2101fcf5ef2aSThomas Huth 
2102fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2103fcf5ef2aSThomas Huth     case 1:
2104fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2105fcf5ef2aSThomas Huth         break;
2106fcf5ef2aSThomas Huth     case 3:
2107fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2108fcf5ef2aSThomas Huth         break;
2109fcf5ef2aSThomas Huth     }
2110fcf5ef2aSThomas Huth 
2111fcf5ef2aSThomas Huth     if (res) {
2112fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2113fcf5ef2aSThomas Huth     }
2114fcf5ef2aSThomas Huth     if (res & 1) {
2115fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2116fcf5ef2aSThomas Huth     }
2117fcf5ef2aSThomas Huth 
2118fcf5ef2aSThomas Huth     return res;
2119fcf5ef2aSThomas Huth }
2120fcf5ef2aSThomas Huth 
2121fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2122fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2123fcf5ef2aSThomas Huth {
2124fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2125fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2126fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2127fcf5ef2aSThomas Huth 
2128fcf5ef2aSThomas Huth     if (res) {
2129fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2130fcf5ef2aSThomas Huth     } else {
2131fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2132fcf5ef2aSThomas Huth     }
2133fcf5ef2aSThomas Huth }
2134fcf5ef2aSThomas Huth 
2135fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2136fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2137fcf5ef2aSThomas Huth {
2138fcf5ef2aSThomas Huth     int i;
2139fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2140fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2141fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2142fcf5ef2aSThomas Huth 
2143fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2144fcf5ef2aSThomas Huth         if (ctrl & 1) {
2145fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2146fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2147fcf5ef2aSThomas Huth             }
2148fcf5ef2aSThomas Huth         } else {
2149fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2150fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2151fcf5ef2aSThomas Huth             }
2152fcf5ef2aSThomas Huth         }
2153fcf5ef2aSThomas Huth     } else {
2154fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2155fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2156fcf5ef2aSThomas Huth     }
2157fcf5ef2aSThomas Huth }
2158fcf5ef2aSThomas Huth 
2159fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2160fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2161fcf5ef2aSThomas Huth {
2162fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2163fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2164fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2165fcf5ef2aSThomas Huth 
2166fcf5ef2aSThomas Huth     if (res) {
2167fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2168fcf5ef2aSThomas Huth     } else {
2169fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2170fcf5ef2aSThomas Huth     }
2171fcf5ef2aSThomas Huth }
2172fcf5ef2aSThomas Huth 
2173fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2174fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2175fcf5ef2aSThomas Huth {
2176fcf5ef2aSThomas Huth     int i;
2177fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2178fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2179fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2180fcf5ef2aSThomas Huth 
2181fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2182fcf5ef2aSThomas Huth         if (ctrl & 1) {
2183fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2184fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2185fcf5ef2aSThomas Huth             }
2186fcf5ef2aSThomas Huth         } else {
2187fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2188fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2189fcf5ef2aSThomas Huth             }
2190fcf5ef2aSThomas Huth         }
2191fcf5ef2aSThomas Huth     } else {
2192fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2193fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2194fcf5ef2aSThomas Huth     }
2195fcf5ef2aSThomas Huth }
2196fcf5ef2aSThomas Huth 
2197fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2198fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2199fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2200fcf5ef2aSThomas Huth {
2201fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2202fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2203fcf5ef2aSThomas Huth 
2204fcf5ef2aSThomas Huth     while (len--) {
2205fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2206fcf5ef2aSThomas Huth     }
2207fcf5ef2aSThomas Huth 
2208fcf5ef2aSThomas Huth     return crc;
2209fcf5ef2aSThomas Huth }
2210fcf5ef2aSThomas Huth 
2211fcf5ef2aSThomas Huth void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2212fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2213fcf5ef2aSThomas Huth {
2214fcf5ef2aSThomas Huth     uint64_t ah, al, b, resh, resl;
2215fcf5ef2aSThomas Huth 
2216fcf5ef2aSThomas Huth     ah = 0;
2217fcf5ef2aSThomas Huth     al = d->Q((ctrl & 1) != 0);
2218fcf5ef2aSThomas Huth     b = s->Q((ctrl & 16) != 0);
2219fcf5ef2aSThomas Huth     resh = resl = 0;
2220fcf5ef2aSThomas Huth 
2221fcf5ef2aSThomas Huth     while (b) {
2222fcf5ef2aSThomas Huth         if (b & 1) {
2223fcf5ef2aSThomas Huth             resl ^= al;
2224fcf5ef2aSThomas Huth             resh ^= ah;
2225fcf5ef2aSThomas Huth         }
2226fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2227fcf5ef2aSThomas Huth         al <<= 1;
2228fcf5ef2aSThomas Huth         b >>= 1;
2229fcf5ef2aSThomas Huth     }
2230fcf5ef2aSThomas Huth 
2231fcf5ef2aSThomas Huth     d->Q(0) = resl;
2232fcf5ef2aSThomas Huth     d->Q(1) = resh;
2233fcf5ef2aSThomas Huth }
2234fcf5ef2aSThomas Huth 
2235fcf5ef2aSThomas Huth void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2236fcf5ef2aSThomas Huth {
2237fcf5ef2aSThomas Huth     int i;
2238fcf5ef2aSThomas Huth     Reg st = *d;
2239fcf5ef2aSThomas Huth     Reg rk = *s;
2240fcf5ef2aSThomas Huth 
2241fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2242fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4*i+0])] ^
2243fcf5ef2aSThomas Huth                                     AES_Td1[st.B(AES_ishifts[4*i+1])] ^
2244fcf5ef2aSThomas Huth                                     AES_Td2[st.B(AES_ishifts[4*i+2])] ^
2245fcf5ef2aSThomas Huth                                     AES_Td3[st.B(AES_ishifts[4*i+3])]);
2246fcf5ef2aSThomas Huth     }
2247fcf5ef2aSThomas Huth }
2248fcf5ef2aSThomas Huth 
2249fcf5ef2aSThomas Huth void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2250fcf5ef2aSThomas Huth {
2251fcf5ef2aSThomas Huth     int i;
2252fcf5ef2aSThomas Huth     Reg st = *d;
2253fcf5ef2aSThomas Huth     Reg rk = *s;
2254fcf5ef2aSThomas Huth 
2255fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2256fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i])]);
2257fcf5ef2aSThomas Huth     }
2258fcf5ef2aSThomas Huth }
2259fcf5ef2aSThomas Huth 
2260fcf5ef2aSThomas Huth void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2261fcf5ef2aSThomas Huth {
2262fcf5ef2aSThomas Huth     int i;
2263fcf5ef2aSThomas Huth     Reg st = *d;
2264fcf5ef2aSThomas Huth     Reg rk = *s;
2265fcf5ef2aSThomas Huth 
2266fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2267fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4*i+0])] ^
2268fcf5ef2aSThomas Huth                                     AES_Te1[st.B(AES_shifts[4*i+1])] ^
2269fcf5ef2aSThomas Huth                                     AES_Te2[st.B(AES_shifts[4*i+2])] ^
2270fcf5ef2aSThomas Huth                                     AES_Te3[st.B(AES_shifts[4*i+3])]);
2271fcf5ef2aSThomas Huth     }
2272fcf5ef2aSThomas Huth }
2273fcf5ef2aSThomas Huth 
2274fcf5ef2aSThomas Huth void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2275fcf5ef2aSThomas Huth {
2276fcf5ef2aSThomas Huth     int i;
2277fcf5ef2aSThomas Huth     Reg st = *d;
2278fcf5ef2aSThomas Huth     Reg rk = *s;
2279fcf5ef2aSThomas Huth 
2280fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2281fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i])]);
2282fcf5ef2aSThomas Huth     }
2283fcf5ef2aSThomas Huth 
2284fcf5ef2aSThomas Huth }
2285fcf5ef2aSThomas Huth 
2286fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2287fcf5ef2aSThomas Huth {
2288fcf5ef2aSThomas Huth     int i;
2289fcf5ef2aSThomas Huth     Reg tmp = *s;
2290fcf5ef2aSThomas Huth 
2291fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2292fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4*i+0)][0] ^
2293fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+1)][1] ^
2294fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+2)][2] ^
2295fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+3)][3]);
2296fcf5ef2aSThomas Huth     }
2297fcf5ef2aSThomas Huth }
2298fcf5ef2aSThomas Huth 
2299fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2300fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2301fcf5ef2aSThomas Huth {
2302fcf5ef2aSThomas Huth     int i;
2303fcf5ef2aSThomas Huth     Reg tmp = *s;
2304fcf5ef2aSThomas Huth 
2305fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2306fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2307fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2308fcf5ef2aSThomas Huth     }
2309fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2310fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2311fcf5ef2aSThomas Huth }
2312fcf5ef2aSThomas Huth #endif
2313fcf5ef2aSThomas Huth 
23143403cafeSPaul Brook #undef SSE_HELPER_S
23153403cafeSPaul Brook 
2316fcf5ef2aSThomas Huth #undef SHIFT
2317fcf5ef2aSThomas Huth #undef XMM_ONLY
2318fcf5ef2aSThomas Huth #undef Reg
2319fcf5ef2aSThomas Huth #undef B
2320fcf5ef2aSThomas Huth #undef W
2321fcf5ef2aSThomas Huth #undef L
2322fcf5ef2aSThomas Huth #undef Q
2323fcf5ef2aSThomas Huth #undef SUFFIX
2324d22697ddSPaolo Bonzini #undef SIZE
2325