xref: /openbmc/qemu/target/i386/ops_sse.h (revision d45b0de63d545aad2a2d91fde7b816ee3641d8d4)
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)
44*d45b0de6SPaul 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 
478*d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
479*d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
480*d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
481*d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
482*d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
483*d45b0de6SPaul Brook     d->F(offset) = r0;                      \
484*d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
485*d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
486*d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
487*d45b0de6SPaul Brook     } while (0)
488*d45b0de6SPaul Brook 
489fcf5ef2aSThomas Huth #if SHIFT == 0
490fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
491fcf5ef2aSThomas Huth {
492*d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
493fcf5ef2aSThomas Huth 
494*d45b0de6SPaul 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 {
499*d45b0de6SPaul Brook     Reg *v = d;
500*d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
501*d45b0de6SPaul Brook     int i;
502fcf5ef2aSThomas Huth 
503*d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
504*d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
505*d45b0de6SPaul Brook     }
506fcf5ef2aSThomas Huth }
507fcf5ef2aSThomas Huth 
508ce4fa29fSPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *s, int order)
509fcf5ef2aSThomas Huth {
510*d45b0de6SPaul Brook     Reg *v = d;
511*d45b0de6SPaul Brook     uint64_t r0, r1;
512*d45b0de6SPaul Brook     int i;
513fcf5ef2aSThomas Huth 
514*d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
515*d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
516*d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
517*d45b0de6SPaul Brook         d->Q(i) = r0;
518*d45b0de6SPaul Brook         d->Q(i + 1) = r1;
519*d45b0de6SPaul Brook         order >>= 2;
520*d45b0de6SPaul Brook     }
521fcf5ef2aSThomas Huth }
522fcf5ef2aSThomas Huth 
523fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
524fcf5ef2aSThomas Huth {
525*d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
526*d45b0de6SPaul Brook     int i;
527fcf5ef2aSThomas Huth 
528*d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
529*d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
530*d45b0de6SPaul Brook     }
531fcf5ef2aSThomas Huth }
532fcf5ef2aSThomas Huth 
533fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
534fcf5ef2aSThomas Huth {
535*d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
536*d45b0de6SPaul Brook     int i, j;
537fcf5ef2aSThomas Huth 
538*d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
539*d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
540*d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
541*d45b0de6SPaul Brook     }
542fcf5ef2aSThomas Huth }
543fcf5ef2aSThomas Huth 
544fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
545fcf5ef2aSThomas Huth {
546*d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
547*d45b0de6SPaul Brook     int i, j;
548fcf5ef2aSThomas Huth 
549*d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
550*d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
551*d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
552*d45b0de6SPaul Brook     }
553fcf5ef2aSThomas Huth }
554fcf5ef2aSThomas Huth #endif
555fcf5ef2aSThomas Huth 
556fcf5ef2aSThomas Huth #if SHIFT == 1
557fcf5ef2aSThomas Huth /* FPU ops */
558fcf5ef2aSThomas Huth /* XXX: not accurate */
559fcf5ef2aSThomas Huth 
560fcf5ef2aSThomas Huth #define SSE_HELPER_S(name, F)                                           \
561ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
562fcf5ef2aSThomas Huth     {                                                                   \
563fcf5ef2aSThomas Huth         d->ZMM_S(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
564fcf5ef2aSThomas Huth         d->ZMM_S(1) = F(32, d->ZMM_S(1), s->ZMM_S(1));                  \
565fcf5ef2aSThomas Huth         d->ZMM_S(2) = F(32, d->ZMM_S(2), s->ZMM_S(2));                  \
566fcf5ef2aSThomas Huth         d->ZMM_S(3) = F(32, d->ZMM_S(3), s->ZMM_S(3));                  \
567fcf5ef2aSThomas Huth     }                                                                   \
568fcf5ef2aSThomas Huth                                                                         \
569fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)        \
570fcf5ef2aSThomas Huth     {                                                                   \
571fcf5ef2aSThomas Huth         d->ZMM_S(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
572fcf5ef2aSThomas Huth     }                                                                   \
573fcf5ef2aSThomas Huth                                                                         \
574ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
575fcf5ef2aSThomas Huth     {                                                                   \
576fcf5ef2aSThomas Huth         d->ZMM_D(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
577fcf5ef2aSThomas Huth         d->ZMM_D(1) = F(64, d->ZMM_D(1), s->ZMM_D(1));                  \
578fcf5ef2aSThomas Huth     }                                                                   \
579fcf5ef2aSThomas Huth                                                                         \
580fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)        \
581fcf5ef2aSThomas Huth     {                                                                   \
582fcf5ef2aSThomas Huth         d->ZMM_D(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
583fcf5ef2aSThomas Huth     }
584fcf5ef2aSThomas Huth 
585fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
586fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
587fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
588fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
589fcf5ef2aSThomas Huth #define FPU_SQRT(size, a, b) float ## size ## _sqrt(b, &env->sse_status)
590fcf5ef2aSThomas Huth 
591fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
592fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
593fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
594fcf5ef2aSThomas Huth  */
595fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
596fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
597fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
598fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
599fcf5ef2aSThomas Huth 
600fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
601fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
602fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
603fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
604fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
605fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
606fcf5ef2aSThomas Huth SSE_HELPER_S(sqrt, FPU_SQRT)
607fcf5ef2aSThomas Huth 
608fcf5ef2aSThomas Huth 
609fcf5ef2aSThomas Huth /* float to float conversions */
610ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
611fcf5ef2aSThomas Huth {
612fcf5ef2aSThomas Huth     float32 s0, s1;
613fcf5ef2aSThomas Huth 
614fcf5ef2aSThomas Huth     s0 = s->ZMM_S(0);
615fcf5ef2aSThomas Huth     s1 = s->ZMM_S(1);
616fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s0, &env->sse_status);
617fcf5ef2aSThomas Huth     d->ZMM_D(1) = float32_to_float64(s1, &env->sse_status);
618fcf5ef2aSThomas Huth }
619fcf5ef2aSThomas Huth 
620ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
621fcf5ef2aSThomas Huth {
622fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
623fcf5ef2aSThomas Huth     d->ZMM_S(1) = float64_to_float32(s->ZMM_D(1), &env->sse_status);
624fcf5ef2aSThomas Huth     d->Q(1) = 0;
625fcf5ef2aSThomas Huth }
626fcf5ef2aSThomas Huth 
627fcf5ef2aSThomas Huth void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *s)
628fcf5ef2aSThomas Huth {
629fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
630fcf5ef2aSThomas Huth }
631fcf5ef2aSThomas Huth 
632fcf5ef2aSThomas Huth void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *s)
633fcf5ef2aSThomas Huth {
634fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
635fcf5ef2aSThomas Huth }
636fcf5ef2aSThomas Huth 
637fcf5ef2aSThomas Huth /* integer to float */
638ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
639fcf5ef2aSThomas Huth {
640fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->ZMM_L(0), &env->sse_status);
641fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->ZMM_L(1), &env->sse_status);
642fcf5ef2aSThomas Huth     d->ZMM_S(2) = int32_to_float32(s->ZMM_L(2), &env->sse_status);
643fcf5ef2aSThomas Huth     d->ZMM_S(3) = int32_to_float32(s->ZMM_L(3), &env->sse_status);
644fcf5ef2aSThomas Huth }
645fcf5ef2aSThomas Huth 
646ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
647fcf5ef2aSThomas Huth {
648fcf5ef2aSThomas Huth     int32_t l0, l1;
649fcf5ef2aSThomas Huth 
650fcf5ef2aSThomas Huth     l0 = (int32_t)s->ZMM_L(0);
651fcf5ef2aSThomas Huth     l1 = (int32_t)s->ZMM_L(1);
652fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(l0, &env->sse_status);
653fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(l1, &env->sse_status);
654fcf5ef2aSThomas Huth }
655fcf5ef2aSThomas Huth 
656fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
657fcf5ef2aSThomas Huth {
658fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
659fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
660fcf5ef2aSThomas Huth }
661fcf5ef2aSThomas Huth 
662fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
663fcf5ef2aSThomas Huth {
664fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
665fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
666fcf5ef2aSThomas Huth }
667fcf5ef2aSThomas Huth 
668fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
669fcf5ef2aSThomas Huth {
670fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
671fcf5ef2aSThomas Huth }
672fcf5ef2aSThomas Huth 
673fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
674fcf5ef2aSThomas Huth {
675fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
676fcf5ef2aSThomas Huth }
677fcf5ef2aSThomas Huth 
678fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
679fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
680fcf5ef2aSThomas Huth {
681fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
682fcf5ef2aSThomas Huth }
683fcf5ef2aSThomas Huth 
684fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
685fcf5ef2aSThomas Huth {
686fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
687fcf5ef2aSThomas Huth }
688fcf5ef2aSThomas Huth #endif
689fcf5ef2aSThomas Huth 
690fcf5ef2aSThomas Huth /* float to integer */
6911e8a98b5SPeter Maydell 
6921e8a98b5SPeter Maydell /*
6931e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
6941e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
6951e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
6961e8a98b5SPeter Maydell  */
6971e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
6981e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
6991e8a98b5SPeter Maydell     {                                                                   \
7001e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7011e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7021e8a98b5SPeter Maydell                                                                         \
7031e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7041e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7051e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7061e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7071e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7081e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7091e8a98b5SPeter Maydell         }                                                               \
7101e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7111e8a98b5SPeter Maydell         return r;                                                       \
7121e8a98b5SPeter Maydell     }
7131e8a98b5SPeter Maydell 
7141e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7151e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7161e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7171e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7181e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7191e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7201e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7211e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
7221e8a98b5SPeter Maydell 
723ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
724fcf5ef2aSThomas Huth {
7251e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7261e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
7271e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32(s->ZMM_S(2), &env->sse_status);
7281e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32(s->ZMM_S(3), &env->sse_status);
729fcf5ef2aSThomas Huth }
730fcf5ef2aSThomas Huth 
731ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
732fcf5ef2aSThomas Huth {
7331e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7341e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
735fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
736fcf5ef2aSThomas Huth }
737fcf5ef2aSThomas Huth 
738fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
739fcf5ef2aSThomas Huth {
7401e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7411e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
742fcf5ef2aSThomas Huth }
743fcf5ef2aSThomas Huth 
744fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
745fcf5ef2aSThomas Huth {
7461e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7471e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
748fcf5ef2aSThomas Huth }
749fcf5ef2aSThomas Huth 
750fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
751fcf5ef2aSThomas Huth {
7521e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
753fcf5ef2aSThomas Huth }
754fcf5ef2aSThomas Huth 
755fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
756fcf5ef2aSThomas Huth {
7571e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
758fcf5ef2aSThomas Huth }
759fcf5ef2aSThomas Huth 
760fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
761fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
762fcf5ef2aSThomas Huth {
7631e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
764fcf5ef2aSThomas Huth }
765fcf5ef2aSThomas Huth 
766fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
767fcf5ef2aSThomas Huth {
7681e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
769fcf5ef2aSThomas Huth }
770fcf5ef2aSThomas Huth #endif
771fcf5ef2aSThomas Huth 
772fcf5ef2aSThomas Huth /* float to integer truncated */
773ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
774fcf5ef2aSThomas Huth {
7751e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
7761e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
7771e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32_round_to_zero(s->ZMM_S(2), &env->sse_status);
7781e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32_round_to_zero(s->ZMM_S(3), &env->sse_status);
779fcf5ef2aSThomas Huth }
780fcf5ef2aSThomas Huth 
781ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
782fcf5ef2aSThomas Huth {
7831e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
7841e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
785fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
786fcf5ef2aSThomas Huth }
787fcf5ef2aSThomas Huth 
788fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
789fcf5ef2aSThomas Huth {
7901e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
7911e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
792fcf5ef2aSThomas Huth }
793fcf5ef2aSThomas Huth 
794fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
795fcf5ef2aSThomas Huth {
7961e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
7971e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
798fcf5ef2aSThomas Huth }
799fcf5ef2aSThomas Huth 
800fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
801fcf5ef2aSThomas Huth {
8021e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
803fcf5ef2aSThomas Huth }
804fcf5ef2aSThomas Huth 
805fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
806fcf5ef2aSThomas Huth {
8071e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
808fcf5ef2aSThomas Huth }
809fcf5ef2aSThomas Huth 
810fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
811fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
812fcf5ef2aSThomas Huth {
8131e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
814fcf5ef2aSThomas Huth }
815fcf5ef2aSThomas Huth 
816fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
817fcf5ef2aSThomas Huth {
8181e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
819fcf5ef2aSThomas Huth }
820fcf5ef2aSThomas Huth #endif
821fcf5ef2aSThomas Huth 
822ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
823fcf5ef2aSThomas Huth {
824418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
825fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
826fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
827fcf5ef2aSThomas Huth                               &env->sse_status);
828fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_div(float32_one,
829fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(1), &env->sse_status),
830fcf5ef2aSThomas Huth                               &env->sse_status);
831fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_div(float32_one,
832fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(2), &env->sse_status),
833fcf5ef2aSThomas Huth                               &env->sse_status);
834fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_div(float32_one,
835fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(3), &env->sse_status),
836fcf5ef2aSThomas Huth                               &env->sse_status);
837418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
838fcf5ef2aSThomas Huth }
839fcf5ef2aSThomas Huth 
840fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
841fcf5ef2aSThomas Huth {
842418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
843fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
844fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
845fcf5ef2aSThomas Huth                               &env->sse_status);
846418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
847fcf5ef2aSThomas Huth }
848fcf5ef2aSThomas Huth 
849ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
850fcf5ef2aSThomas Huth {
851418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
852fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
853fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_div(float32_one, s->ZMM_S(1), &env->sse_status);
854fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_div(float32_one, s->ZMM_S(2), &env->sse_status);
855fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_div(float32_one, s->ZMM_S(3), &env->sse_status);
856418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
857fcf5ef2aSThomas Huth }
858fcf5ef2aSThomas Huth 
859fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
860fcf5ef2aSThomas Huth {
861418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
862fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
863418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
864fcf5ef2aSThomas Huth }
865fcf5ef2aSThomas Huth 
866fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
867fcf5ef2aSThomas Huth {
868fcf5ef2aSThomas Huth     uint64_t mask;
869fcf5ef2aSThomas Huth 
870fcf5ef2aSThomas Huth     if (len == 0) {
871fcf5ef2aSThomas Huth         mask = ~0LL;
872fcf5ef2aSThomas Huth     } else {
873fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
874fcf5ef2aSThomas Huth     }
875fcf5ef2aSThomas Huth     return (src >> shift) & mask;
876fcf5ef2aSThomas Huth }
877fcf5ef2aSThomas Huth 
878fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
879fcf5ef2aSThomas Huth {
880fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1), s->ZMM_B(0));
881fcf5ef2aSThomas Huth }
882fcf5ef2aSThomas Huth 
883fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
884fcf5ef2aSThomas Huth {
885fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
886fcf5ef2aSThomas Huth }
887fcf5ef2aSThomas Huth 
888fcf5ef2aSThomas Huth static inline uint64_t helper_insertq(uint64_t src, int shift, int len)
889fcf5ef2aSThomas Huth {
890fcf5ef2aSThomas Huth     uint64_t mask;
891fcf5ef2aSThomas Huth 
892fcf5ef2aSThomas Huth     if (len == 0) {
893fcf5ef2aSThomas Huth         mask = ~0ULL;
894fcf5ef2aSThomas Huth     } else {
895fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
896fcf5ef2aSThomas Huth     }
897fcf5ef2aSThomas Huth     return (src & ~(mask << shift)) | ((src & mask) << shift);
898fcf5ef2aSThomas Huth }
899fcf5ef2aSThomas Huth 
900fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
901fcf5ef2aSThomas Huth {
902fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(s->ZMM_Q(0), s->ZMM_B(9), s->ZMM_B(8));
903fcf5ef2aSThomas Huth }
904fcf5ef2aSThomas Huth 
905fcf5ef2aSThomas Huth void helper_insertq_i(CPUX86State *env, ZMMReg *d, int index, int length)
906fcf5ef2aSThomas Huth {
907fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), index, length);
908fcf5ef2aSThomas Huth }
909fcf5ef2aSThomas Huth 
910ce4fa29fSPaolo Bonzini void glue(helper_haddps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
911fcf5ef2aSThomas Huth {
912fcf5ef2aSThomas Huth     ZMMReg r;
913fcf5ef2aSThomas Huth 
914fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_add(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
915fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_add(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
916fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_add(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
917fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_add(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
918d22697ddSPaolo Bonzini     MOVE(*d, r);
919fcf5ef2aSThomas Huth }
920fcf5ef2aSThomas Huth 
921ce4fa29fSPaolo Bonzini void glue(helper_haddpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
922fcf5ef2aSThomas Huth {
923fcf5ef2aSThomas Huth     ZMMReg r;
924fcf5ef2aSThomas Huth 
925fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_add(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
926fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_add(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
927d22697ddSPaolo Bonzini     MOVE(*d, r);
928fcf5ef2aSThomas Huth }
929fcf5ef2aSThomas Huth 
930ce4fa29fSPaolo Bonzini void glue(helper_hsubps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
931fcf5ef2aSThomas Huth {
932fcf5ef2aSThomas Huth     ZMMReg r;
933fcf5ef2aSThomas Huth 
934fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_sub(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
935fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_sub(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
936fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_sub(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
937fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_sub(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
938d22697ddSPaolo Bonzini     MOVE(*d, r);
939fcf5ef2aSThomas Huth }
940fcf5ef2aSThomas Huth 
941ce4fa29fSPaolo Bonzini void glue(helper_hsubpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
942fcf5ef2aSThomas Huth {
943fcf5ef2aSThomas Huth     ZMMReg r;
944fcf5ef2aSThomas Huth 
945fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_sub(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
946fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_sub(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
947d22697ddSPaolo Bonzini     MOVE(*d, r);
948fcf5ef2aSThomas Huth }
949fcf5ef2aSThomas Huth 
950ce4fa29fSPaolo Bonzini void glue(helper_addsubps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
951fcf5ef2aSThomas Huth {
952fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_sub(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status);
953fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_add(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status);
954fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_sub(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status);
955fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_add(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status);
956fcf5ef2aSThomas Huth }
957fcf5ef2aSThomas Huth 
958ce4fa29fSPaolo Bonzini void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
959fcf5ef2aSThomas Huth {
960fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_sub(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
961fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_add(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
962fcf5ef2aSThomas Huth }
963fcf5ef2aSThomas Huth 
964fcf5ef2aSThomas Huth /* XXX: unordered */
965fcf5ef2aSThomas Huth #define SSE_HELPER_CMP(name, F)                                         \
966ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
967fcf5ef2aSThomas Huth     {                                                                   \
968fcf5ef2aSThomas Huth         d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
969fcf5ef2aSThomas Huth         d->ZMM_L(1) = F(32, d->ZMM_S(1), s->ZMM_S(1));                  \
970fcf5ef2aSThomas Huth         d->ZMM_L(2) = F(32, d->ZMM_S(2), s->ZMM_S(2));                  \
971fcf5ef2aSThomas Huth         d->ZMM_L(3) = F(32, d->ZMM_S(3), s->ZMM_S(3));                  \
972fcf5ef2aSThomas Huth     }                                                                   \
973fcf5ef2aSThomas Huth                                                                         \
974fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)        \
975fcf5ef2aSThomas Huth     {                                                                   \
976fcf5ef2aSThomas Huth         d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
977fcf5ef2aSThomas Huth     }                                                                   \
978fcf5ef2aSThomas Huth                                                                         \
979ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
980fcf5ef2aSThomas Huth     {                                                                   \
981fcf5ef2aSThomas Huth         d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
982fcf5ef2aSThomas Huth         d->ZMM_Q(1) = F(64, d->ZMM_D(1), s->ZMM_D(1));                  \
983fcf5ef2aSThomas Huth     }                                                                   \
984fcf5ef2aSThomas Huth                                                                         \
985fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)        \
986fcf5ef2aSThomas Huth     {                                                                   \
987fcf5ef2aSThomas Huth         d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
988fcf5ef2aSThomas Huth     }
989fcf5ef2aSThomas Huth 
990fcf5ef2aSThomas Huth #define FPU_CMPEQ(size, a, b)                                           \
991fcf5ef2aSThomas Huth     (float ## size ## _eq_quiet(a, b, &env->sse_status) ? -1 : 0)
992fcf5ef2aSThomas Huth #define FPU_CMPLT(size, a, b)                                           \
993fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0)
994fcf5ef2aSThomas Huth #define FPU_CMPLE(size, a, b)                                           \
995fcf5ef2aSThomas Huth     (float ## size ## _le(a, b, &env->sse_status) ? -1 : 0)
996fcf5ef2aSThomas Huth #define FPU_CMPUNORD(size, a, b)                                        \
997fcf5ef2aSThomas Huth     (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? -1 : 0)
998fcf5ef2aSThomas Huth #define FPU_CMPNEQ(size, a, b)                                          \
999fcf5ef2aSThomas Huth     (float ## size ## _eq_quiet(a, b, &env->sse_status) ? 0 : -1)
1000fcf5ef2aSThomas Huth #define FPU_CMPNLT(size, a, b)                                          \
1001fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1)
1002fcf5ef2aSThomas Huth #define FPU_CMPNLE(size, a, b)                                          \
1003fcf5ef2aSThomas Huth     (float ## size ## _le(a, b, &env->sse_status) ? 0 : -1)
1004fcf5ef2aSThomas Huth #define FPU_CMPORD(size, a, b)                                          \
1005fcf5ef2aSThomas Huth     (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? 0 : -1)
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpeq, FPU_CMPEQ)
1008fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmplt, FPU_CMPLT)
1009fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmple, FPU_CMPLE)
1010fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpunord, FPU_CMPUNORD)
1011fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpneq, FPU_CMPNEQ)
1012fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnlt, FPU_CMPNLT)
1013fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnle, FPU_CMPNLE)
1014fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpord, FPU_CMPORD)
1015fcf5ef2aSThomas Huth 
1016fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1017fcf5ef2aSThomas Huth 
1018fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1019fcf5ef2aSThomas Huth {
102071bfd65cSRichard Henderson     FloatRelation ret;
1021fcf5ef2aSThomas Huth     float32 s0, s1;
1022fcf5ef2aSThomas Huth 
1023fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1024fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1025fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1026fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1027fcf5ef2aSThomas Huth }
1028fcf5ef2aSThomas Huth 
1029fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1030fcf5ef2aSThomas Huth {
103171bfd65cSRichard Henderson     FloatRelation ret;
1032fcf5ef2aSThomas Huth     float32 s0, s1;
1033fcf5ef2aSThomas Huth 
1034fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1035fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1036fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1037fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1038fcf5ef2aSThomas Huth }
1039fcf5ef2aSThomas Huth 
1040fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1041fcf5ef2aSThomas Huth {
104271bfd65cSRichard Henderson     FloatRelation ret;
1043fcf5ef2aSThomas Huth     float64 d0, d1;
1044fcf5ef2aSThomas Huth 
1045fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1046fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1047fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1048fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1049fcf5ef2aSThomas Huth }
1050fcf5ef2aSThomas Huth 
1051fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1052fcf5ef2aSThomas Huth {
105371bfd65cSRichard Henderson     FloatRelation ret;
1054fcf5ef2aSThomas Huth     float64 d0, d1;
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1057fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1058fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1059fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1060fcf5ef2aSThomas Huth }
1061fcf5ef2aSThomas Huth 
1062ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1063fcf5ef2aSThomas Huth {
1064fcf5ef2aSThomas Huth     int b0, b1, b2, b3;
1065fcf5ef2aSThomas Huth 
1066fcf5ef2aSThomas Huth     b0 = s->ZMM_L(0) >> 31;
1067fcf5ef2aSThomas Huth     b1 = s->ZMM_L(1) >> 31;
1068fcf5ef2aSThomas Huth     b2 = s->ZMM_L(2) >> 31;
1069fcf5ef2aSThomas Huth     b3 = s->ZMM_L(3) >> 31;
1070fcf5ef2aSThomas Huth     return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3);
1071fcf5ef2aSThomas Huth }
1072fcf5ef2aSThomas Huth 
1073ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1074fcf5ef2aSThomas Huth {
1075fcf5ef2aSThomas Huth     int b0, b1;
1076fcf5ef2aSThomas Huth 
1077fcf5ef2aSThomas Huth     b0 = s->ZMM_L(1) >> 31;
1078fcf5ef2aSThomas Huth     b1 = s->ZMM_L(3) >> 31;
1079fcf5ef2aSThomas Huth     return b0 | (b1 << 1);
1080fcf5ef2aSThomas Huth }
1081fcf5ef2aSThomas Huth 
1082fcf5ef2aSThomas Huth #endif
1083fcf5ef2aSThomas Huth 
1084fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1085fcf5ef2aSThomas Huth {
1086fcf5ef2aSThomas Huth     uint32_t val;
1087e894bae8SPaul Brook     int i;
1088fcf5ef2aSThomas Huth 
1089fcf5ef2aSThomas Huth     val = 0;
1090e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
1091e894bae8SPaul Brook         uint8_t byte = 0;
1092e894bae8SPaul Brook         byte |= (s->B(8 * i + 0) >> 7);
1093e894bae8SPaul Brook         byte |= (s->B(8 * i + 1) >> 6) & 0x02;
1094e894bae8SPaul Brook         byte |= (s->B(8 * i + 2) >> 5) & 0x04;
1095e894bae8SPaul Brook         byte |= (s->B(8 * i + 3) >> 4) & 0x08;
1096e894bae8SPaul Brook         byte |= (s->B(8 * i + 4) >> 3) & 0x10;
1097e894bae8SPaul Brook         byte |= (s->B(8 * i + 5) >> 2) & 0x20;
1098e894bae8SPaul Brook         byte |= (s->B(8 * i + 6) >> 1) & 0x40;
1099e894bae8SPaul Brook         byte |= (s->B(8 * i + 7)) & 0x80;
1100e894bae8SPaul Brook         val |= byte << (8 * i);
1101e894bae8SPaul Brook     }
1102fcf5ef2aSThomas Huth     return val;
1103fcf5ef2aSThomas Huth }
1104fcf5ef2aSThomas Huth 
1105*d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1106*d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1107*d45b0de6SPaul Brook         Reg *d, Reg *s)                                       \
1108*d45b0de6SPaul Brook {                                                             \
1109*d45b0de6SPaul Brook     Reg *v = d;                                               \
1110*d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1111*d45b0de6SPaul Brook     int j, k;                                                 \
1112*d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1113*d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1114*d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1115*d45b0de6SPaul Brook         }                                                     \
1116*d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1117*d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1118*d45b0de6SPaul Brook         }                                                     \
1119*d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1120*d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1121*d45b0de6SPaul Brook         }                                                     \
1122*d45b0de6SPaul Brook     }                                                         \
1123fcf5ef2aSThomas Huth }
1124fcf5ef2aSThomas Huth 
1125*d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1126*d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1129fcf5ef2aSThomas Huth {
1130*d45b0de6SPaul Brook     Reg *v = d;
1131*d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1132*d45b0de6SPaul Brook     int j, k;
1133fcf5ef2aSThomas Huth 
1134*d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1135*d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1136*d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1137*d45b0de6SPaul Brook         }
1138*d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1139*d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1140*d45b0de6SPaul Brook         }
1141*d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1142*d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1143*d45b0de6SPaul Brook         }
1144*d45b0de6SPaul Brook     }
1145fcf5ef2aSThomas Huth }
1146fcf5ef2aSThomas Huth 
1147fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1148fcf5ef2aSThomas Huth                                                                         \
1149fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1150fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1151fcf5ef2aSThomas Huth     {                                                                   \
1152*d45b0de6SPaul Brook         Reg *v = d;                                                     \
1153*d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1154*d45b0de6SPaul Brook         int j, i;                                                       \
1155fcf5ef2aSThomas Huth                                                                         \
1156*d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1157*d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1158*d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1159*d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1160*d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1161*d45b0de6SPaul Brook             }                                                           \
1162*d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1163*d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1164*d45b0de6SPaul Brook             }                                                           \
1165*d45b0de6SPaul Brook         }                                                               \
1166fcf5ef2aSThomas Huth     }                                                                   \
1167fcf5ef2aSThomas Huth                                                                         \
1168fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1169fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1170fcf5ef2aSThomas Huth     {                                                                   \
1171*d45b0de6SPaul Brook         Reg *v = d;                                                     \
1172*d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1173*d45b0de6SPaul Brook         int j, i;                                                       \
1174fcf5ef2aSThomas Huth                                                                         \
1175*d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1176*d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1177*d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1178*d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1179*d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1180*d45b0de6SPaul Brook             }                                                           \
1181*d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1182*d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1183*d45b0de6SPaul Brook             }                                                           \
1184*d45b0de6SPaul Brook         }                                                               \
1185fcf5ef2aSThomas Huth     }                                                                   \
1186fcf5ef2aSThomas Huth                                                                         \
1187fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1188fcf5ef2aSThomas Huth                                                 Reg *d, Reg *s) \
1189fcf5ef2aSThomas Huth     {                                                                   \
1190*d45b0de6SPaul Brook         Reg *v = d;                                                     \
1191*d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1192*d45b0de6SPaul Brook         int j, i;                                                       \
1193fcf5ef2aSThomas Huth                                                                         \
1194*d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1195*d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1196*d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1197*d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1198*d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1199*d45b0de6SPaul Brook             }                                                           \
1200*d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1201*d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1202*d45b0de6SPaul Brook             }                                                           \
1203*d45b0de6SPaul Brook         }                                                               \
1204fcf5ef2aSThomas Huth     }                                                                   \
1205fcf5ef2aSThomas Huth                                                                         \
1206fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1207*d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1208*d45b0de6SPaul Brook                         CPUX86State *env, Reg *d, Reg *s)       \
1209fcf5ef2aSThomas Huth              {                                                          \
1210*d45b0de6SPaul Brook                  Reg *v = d;                                            \
1211*d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1212*d45b0de6SPaul Brook                  int i;                                                 \
1213fcf5ef2aSThomas Huth                                                                         \
1214*d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1215*d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1216*d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1217*d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1218*d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1219*d45b0de6SPaul Brook                  }                                                      \
1220fcf5ef2aSThomas Huth              }                                                          \
1221fcf5ef2aSThomas Huth                                                                         )
1222fcf5ef2aSThomas Huth 
1223fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1224fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1225fcf5ef2aSThomas Huth 
1226*d45b0de6SPaul Brook #undef PACK_WIDTH
1227*d45b0de6SPaul Brook #undef PACK_HELPER_B
1228*d45b0de6SPaul Brook #undef UNPCK_OP
1229*d45b0de6SPaul Brook 
1230*d45b0de6SPaul Brook 
1231fcf5ef2aSThomas Huth /* 3DNow! float ops */
1232fcf5ef2aSThomas Huth #if SHIFT == 0
1233fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1234fcf5ef2aSThomas Huth {
1235fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1236fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1237fcf5ef2aSThomas Huth }
1238fcf5ef2aSThomas Huth 
1239fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1240fcf5ef2aSThomas Huth {
1241fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1242fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1243fcf5ef2aSThomas Huth }
1244fcf5ef2aSThomas Huth 
1245fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1246fcf5ef2aSThomas Huth {
1247fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1248fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1249fcf5ef2aSThomas Huth }
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1252fcf5ef2aSThomas Huth {
1253fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1254fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1255fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1256fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1257fcf5ef2aSThomas Huth }
1258fcf5ef2aSThomas Huth 
1259fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1260fcf5ef2aSThomas Huth {
126125bdec79SPaolo Bonzini     float32 r;
1262fcf5ef2aSThomas Huth 
126325bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
126425bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
126525bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1266fcf5ef2aSThomas Huth }
1267fcf5ef2aSThomas Huth 
1268fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1269fcf5ef2aSThomas Huth {
1270fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1271fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1272fcf5ef2aSThomas Huth }
1273fcf5ef2aSThomas Huth 
1274fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1275fcf5ef2aSThomas Huth {
1276fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1277fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1278fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1279fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1280fcf5ef2aSThomas Huth }
1281fcf5ef2aSThomas Huth 
1282fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1283fcf5ef2aSThomas Huth {
1284fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1285fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1286fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1287fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1288fcf5ef2aSThomas Huth }
1289fcf5ef2aSThomas Huth 
1290fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1291fcf5ef2aSThomas Huth {
1292fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1293fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1294fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1295fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1296fcf5ef2aSThomas Huth }
1297fcf5ef2aSThomas Huth 
1298fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1299fcf5ef2aSThomas Huth {
1300fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1301fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1302fcf5ef2aSThomas Huth     }
1303fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1304fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1305fcf5ef2aSThomas Huth     }
1306fcf5ef2aSThomas Huth }
1307fcf5ef2aSThomas Huth 
1308fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1309fcf5ef2aSThomas Huth {
1310fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1311fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1312fcf5ef2aSThomas Huth     }
1313fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1314fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1315fcf5ef2aSThomas Huth     }
1316fcf5ef2aSThomas Huth }
1317fcf5ef2aSThomas Huth 
1318fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1319fcf5ef2aSThomas Huth {
1320fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1321fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1322fcf5ef2aSThomas Huth }
1323fcf5ef2aSThomas Huth 
1324fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1325fcf5ef2aSThomas Huth {
132625bdec79SPaolo Bonzini     float32 r;
1327fcf5ef2aSThomas Huth 
132825bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
132925bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
133025bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1331fcf5ef2aSThomas Huth }
1332fcf5ef2aSThomas Huth 
1333fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1334fcf5ef2aSThomas Huth {
133525bdec79SPaolo Bonzini     float32 r;
1336fcf5ef2aSThomas Huth 
133725bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
133825bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
133925bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1340fcf5ef2aSThomas Huth }
1341fcf5ef2aSThomas Huth 
1342fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1343fcf5ef2aSThomas Huth {
1344fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1345fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1346fcf5ef2aSThomas Huth }
1347fcf5ef2aSThomas Huth 
1348fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1349fcf5ef2aSThomas Huth {
1350fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1351fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1352fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1353fcf5ef2aSThomas Huth                               &env->mmx_status);
1354fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1355fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1356fcf5ef2aSThomas Huth }
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1359fcf5ef2aSThomas Huth {
1360fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1361fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1362fcf5ef2aSThomas Huth }
1363fcf5ef2aSThomas Huth 
1364fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1365fcf5ef2aSThomas Huth {
1366fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1367fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1368fcf5ef2aSThomas Huth }
1369fcf5ef2aSThomas Huth 
1370fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1371fcf5ef2aSThomas Huth {
137225bdec79SPaolo Bonzini     uint32_t r;
1373fcf5ef2aSThomas Huth 
137425bdec79SPaolo Bonzini     r = s->MMX_L(0);
137525bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
137625bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1377fcf5ef2aSThomas Huth }
1378fcf5ef2aSThomas Huth #endif
1379fcf5ef2aSThomas Huth 
1380fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1381fcf5ef2aSThomas Huth void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1382fcf5ef2aSThomas Huth {
1383*d45b0de6SPaul Brook     Reg *v = d;
1384fcf5ef2aSThomas Huth     int i;
1385*d45b0de6SPaul Brook #if SHIFT == 0
1386*d45b0de6SPaul Brook     uint8_t r[8];
1387fcf5ef2aSThomas Huth 
1388*d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1389*d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1390fcf5ef2aSThomas Huth     }
1391*d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1392*d45b0de6SPaul Brook         d->B(i) = r[i];
1393fcf5ef2aSThomas Huth     }
1394*d45b0de6SPaul Brook #else
1395*d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1396fcf5ef2aSThomas Huth 
1397*d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1398*d45b0de6SPaul Brook         int j = i & ~0xf;
1399*d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1400fcf5ef2aSThomas Huth     }
1401*d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1402*d45b0de6SPaul Brook         d->B(i) = r[i];
1403fcf5ef2aSThomas Huth     }
1404fcf5ef2aSThomas Huth #endif
1405fcf5ef2aSThomas Huth }
1406fcf5ef2aSThomas Huth 
1407*d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1408*d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1409*d45b0de6SPaul Brook {                                                          \
1410*d45b0de6SPaul Brook     Reg *v = d;                                            \
1411*d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1412*d45b0de6SPaul Brook     int i, j, k;                                           \
1413*d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1414*d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1415*d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1416*d45b0de6SPaul Brook         }                                                  \
1417*d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1418*d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1419*d45b0de6SPaul Brook         }                                                  \
1420*d45b0de6SPaul Brook     }                                                      \
1421*d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1422*d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1423*d45b0de6SPaul Brook     }                                                      \
1424fcf5ef2aSThomas Huth }
1425fcf5ef2aSThomas Huth 
1426*d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1427*d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1428*d45b0de6SPaul Brook {                                                          \
1429*d45b0de6SPaul Brook     Reg *v = d;                                            \
1430*d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1431*d45b0de6SPaul Brook     int i, j, k;                                           \
1432*d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1433*d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1434*d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1435*d45b0de6SPaul Brook         }                                                  \
1436*d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1437*d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1438*d45b0de6SPaul Brook         }                                                  \
1439*d45b0de6SPaul Brook     }                                                      \
1440*d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1441*d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1442*d45b0de6SPaul Brook     }                                                      \
1443fcf5ef2aSThomas Huth }
1444fcf5ef2aSThomas Huth 
1445*d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1446*d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1447*d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1448*d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1449*d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1450*d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
145175046ad7SPaolo Bonzini 
1452*d45b0de6SPaul Brook #undef SSE_HELPER_HW
1453*d45b0de6SPaul Brook #undef SSE_HELPER_HL
1454*d45b0de6SPaul Brook 
1455*d45b0de6SPaul Brook void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1456*d45b0de6SPaul Brook {
1457*d45b0de6SPaul Brook     Reg *v = d;
1458*d45b0de6SPaul Brook     int i;
1459*d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1460*d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1461*d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1462*d45b0de6SPaul Brook     }
1463fcf5ef2aSThomas Huth }
1464fcf5ef2aSThomas Huth 
1465ee04a3c8SPaul Brook #define FABSB(x) (x > INT8_MAX  ? -(int8_t)x : x)
1466ee04a3c8SPaul Brook #define FABSW(x) (x > INT16_MAX ? -(int16_t)x : x)
1467ee04a3c8SPaul Brook #define FABSL(x) (x > INT32_MAX ? -(int32_t)x : x)
1468ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsb, B, 8 << SHIFT, FABSB)
1469ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsw, W, 4 << SHIFT, FABSW)
1470ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsd, L, 2 << SHIFT, FABSL)
1471fcf5ef2aSThomas Huth 
1472fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1473fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1474fcf5ef2aSThomas Huth 
1475fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1476fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1477fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1478fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1479fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1480fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1481fcf5ef2aSThomas Huth 
1482fcf5ef2aSThomas Huth void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1483fcf5ef2aSThomas Huth                                   int32_t shift)
1484fcf5ef2aSThomas Huth {
1485*d45b0de6SPaul Brook     Reg *v = d;
1486*d45b0de6SPaul Brook     int i;
1487fcf5ef2aSThomas Huth 
1488fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1489*d45b0de6SPaul Brook     if (shift >= (SHIFT ? 32 : 16)) {
1490*d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1491*d45b0de6SPaul Brook             d->Q(i) = 0;
1492*d45b0de6SPaul Brook         }
1493fcf5ef2aSThomas Huth     } else {
1494fcf5ef2aSThomas Huth         shift <<= 3;
1495fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1496fcf5ef2aSThomas Huth #if SHIFT == 0
1497*d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1498*d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1499fcf5ef2aSThomas Huth #else
1500*d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1501*d45b0de6SPaul Brook             uint64_t r0, r1;
1502*d45b0de6SPaul Brook 
1503*d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1504*d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1505*d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1506*d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1507*d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1508*d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1509*d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1510*d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1511*d45b0de6SPaul Brook             d->Q(i) = r0;
1512*d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1513*d45b0de6SPaul Brook         }
1514fcf5ef2aSThomas Huth #endif
1515fcf5ef2aSThomas Huth #undef SHR
1516fcf5ef2aSThomas Huth     }
1517fcf5ef2aSThomas Huth }
1518fcf5ef2aSThomas Huth 
1519fcf5ef2aSThomas Huth #define XMM0 (env->xmm_regs[0])
1520fcf5ef2aSThomas Huth 
1521fcf5ef2aSThomas Huth #if SHIFT == 1
1522fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1523fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)           \
1524fcf5ef2aSThomas Huth     {                                                                   \
1525fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0));           \
1526fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1));           \
1527fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1528fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2));       \
1529fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3));       \
1530fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1531fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4));   \
1532fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5));   \
1533fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6));   \
1534fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7));   \
1535fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1536fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \
1537fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \
1538fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \
1539fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \
1540fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \
1541fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \
1542fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \
1543fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \
1544fcf5ef2aSThomas Huth                 }                                                       \
1545fcf5ef2aSThomas Huth             }                                                           \
1546fcf5ef2aSThomas Huth         }                                                               \
1547fcf5ef2aSThomas Huth     }
1548fcf5ef2aSThomas Huth 
1549fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1550fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \
1551fcf5ef2aSThomas Huth     {                                                                   \
1552fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1));       \
1553fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1));       \
1554fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1555fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1));   \
1556fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1));   \
1557fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1558fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \
1559fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \
1560fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \
1561fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \
1562fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1563fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \
1564fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \
1565fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10),           \
1566fcf5ef2aSThomas Huth                                     ((imm >> 10) & 1));                 \
1567fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11),           \
1568fcf5ef2aSThomas Huth                                     ((imm >> 11) & 1));                 \
1569fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12),           \
1570fcf5ef2aSThomas Huth                                     ((imm >> 12) & 1));                 \
1571fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13),           \
1572fcf5ef2aSThomas Huth                                     ((imm >> 13) & 1));                 \
1573fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14),           \
1574fcf5ef2aSThomas Huth                                     ((imm >> 14) & 1));                 \
1575fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15),           \
1576fcf5ef2aSThomas Huth                                     ((imm >> 15) & 1));                 \
1577fcf5ef2aSThomas Huth                 }                                                       \
1578fcf5ef2aSThomas Huth             }                                                           \
1579fcf5ef2aSThomas Huth         }                                                               \
1580fcf5ef2aSThomas Huth     }
1581fcf5ef2aSThomas Huth 
1582fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
1583fcf5ef2aSThomas Huth #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d)
1584fcf5ef2aSThomas Huth #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d)
1585fcf5ef2aSThomas Huth #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d)
1586fcf5ef2aSThomas Huth SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB)
1587fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS)
1588fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD)
1589fcf5ef2aSThomas Huth 
1590fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1591fcf5ef2aSThomas Huth {
1592e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1593e894bae8SPaul Brook     int i;
1594fcf5ef2aSThomas Huth 
1595e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1596e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1597e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1598e894bae8SPaul Brook     }
1599fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1600fcf5ef2aSThomas Huth }
1601fcf5ef2aSThomas Huth 
1602fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1603fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1604fcf5ef2aSThomas Huth     {                                                           \
1605e894bae8SPaul Brook         int n = num;                                            \
1606e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1607e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1608fcf5ef2aSThomas Huth         }                                                       \
1609fcf5ef2aSThomas Huth     }
1610fcf5ef2aSThomas Huth 
1611e894bae8SPaul Brook #if SHIFT > 0
1612e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1613e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1614e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1615e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1616e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1617e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1618e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1619e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1620e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1621e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1622e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1623e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
1624e894bae8SPaul Brook #endif
1625fcf5ef2aSThomas Huth 
1626fcf5ef2aSThomas Huth void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1627fcf5ef2aSThomas Huth {
1628e894bae8SPaul Brook     Reg *v = d;
1629e894bae8SPaul Brook     int i;
1630e894bae8SPaul Brook 
1631e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1632e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1633e894bae8SPaul Brook     }
1634fcf5ef2aSThomas Huth }
1635fcf5ef2aSThomas Huth 
1636fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1637fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1638fcf5ef2aSThomas Huth 
1639fcf5ef2aSThomas Huth void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1640fcf5ef2aSThomas Huth {
1641*d45b0de6SPaul Brook     Reg *v = d;
1642*d45b0de6SPaul Brook     uint16_t r[8];
1643*d45b0de6SPaul Brook     int i, j, k;
164480e19606SJoseph Myers 
1645*d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1646*d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1647*d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1648*d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1649*d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1650*d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1651*d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1652*d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1653*d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1654*d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1655*d45b0de6SPaul Brook             d->W(i + k) = r[k];
1656*d45b0de6SPaul Brook         }
1657*d45b0de6SPaul Brook     }
1658fcf5ef2aSThomas Huth }
1659fcf5ef2aSThomas Huth 
1660fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1661fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1662fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1663fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1664fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1665fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1666fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1667fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1668fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1669fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1670fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1671fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1672fcf5ef2aSThomas Huth 
1673fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1674fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1675fcf5ef2aSThomas Huth 
1676fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1677fcf5ef2aSThomas Huth {
1678fcf5ef2aSThomas Huth     int idx = 0;
1679fcf5ef2aSThomas Huth 
1680fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1681fcf5ef2aSThomas Huth         idx = 1;
1682fcf5ef2aSThomas Huth     }
1683fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1684fcf5ef2aSThomas Huth         idx = 2;
1685fcf5ef2aSThomas Huth     }
1686fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1687fcf5ef2aSThomas Huth         idx = 3;
1688fcf5ef2aSThomas Huth     }
1689fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1690fcf5ef2aSThomas Huth         idx = 4;
1691fcf5ef2aSThomas Huth     }
1692fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1693fcf5ef2aSThomas Huth         idx = 5;
1694fcf5ef2aSThomas Huth     }
1695fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1696fcf5ef2aSThomas Huth         idx = 6;
1697fcf5ef2aSThomas Huth     }
1698fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1699fcf5ef2aSThomas Huth         idx = 7;
1700fcf5ef2aSThomas Huth     }
1701fcf5ef2aSThomas Huth 
1702fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1703aa406feaSJoseph Myers     d->W(1) = idx;
1704aa406feaSJoseph Myers     d->L(1) = 0;
1705aa406feaSJoseph Myers     d->Q(1) = 0;
1706fcf5ef2aSThomas Huth }
1707fcf5ef2aSThomas Huth 
1708fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1709fcf5ef2aSThomas Huth                                   uint32_t mode)
1710fcf5ef2aSThomas Huth {
1711418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1712fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1713fcf5ef2aSThomas Huth 
1714fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1715fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1716fcf5ef2aSThomas Huth         switch (mode & 3) {
1717fcf5ef2aSThomas Huth         case 0:
1718fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1719fcf5ef2aSThomas Huth             break;
1720fcf5ef2aSThomas Huth         case 1:
1721fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1722fcf5ef2aSThomas Huth             break;
1723fcf5ef2aSThomas Huth         case 2:
1724fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1725fcf5ef2aSThomas Huth             break;
1726fcf5ef2aSThomas Huth         case 3:
1727fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1728fcf5ef2aSThomas Huth             break;
1729fcf5ef2aSThomas Huth         }
1730fcf5ef2aSThomas Huth     }
1731fcf5ef2aSThomas Huth 
1732fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1733fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_round_to_int(s->ZMM_S(1), &env->sse_status);
1734fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_round_to_int(s->ZMM_S(2), &env->sse_status);
1735fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_round_to_int(s->ZMM_S(3), &env->sse_status);
1736fcf5ef2aSThomas Huth 
1737418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1738fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1739fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1740fcf5ef2aSThomas Huth                                   &env->sse_status);
1741fcf5ef2aSThomas Huth     }
1742fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1743fcf5ef2aSThomas Huth }
1744fcf5ef2aSThomas Huth 
1745fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1746fcf5ef2aSThomas Huth                                   uint32_t mode)
1747fcf5ef2aSThomas Huth {
1748418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1749fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1750fcf5ef2aSThomas Huth 
1751fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1752fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1753fcf5ef2aSThomas Huth         switch (mode & 3) {
1754fcf5ef2aSThomas Huth         case 0:
1755fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1756fcf5ef2aSThomas Huth             break;
1757fcf5ef2aSThomas Huth         case 1:
1758fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1759fcf5ef2aSThomas Huth             break;
1760fcf5ef2aSThomas Huth         case 2:
1761fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1762fcf5ef2aSThomas Huth             break;
1763fcf5ef2aSThomas Huth         case 3:
1764fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1765fcf5ef2aSThomas Huth             break;
1766fcf5ef2aSThomas Huth         }
1767fcf5ef2aSThomas Huth     }
1768fcf5ef2aSThomas Huth 
1769fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1770fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_round_to_int(s->ZMM_D(1), &env->sse_status);
1771fcf5ef2aSThomas Huth 
1772418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1773fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1774fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1775fcf5ef2aSThomas Huth                                   &env->sse_status);
1776fcf5ef2aSThomas Huth     }
1777fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1778fcf5ef2aSThomas Huth }
1779fcf5ef2aSThomas Huth 
1780fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1781fcf5ef2aSThomas Huth                                   uint32_t mode)
1782fcf5ef2aSThomas Huth {
1783418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1784fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1785fcf5ef2aSThomas Huth 
1786fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1787fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1788fcf5ef2aSThomas Huth         switch (mode & 3) {
1789fcf5ef2aSThomas Huth         case 0:
1790fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1791fcf5ef2aSThomas Huth             break;
1792fcf5ef2aSThomas Huth         case 1:
1793fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1794fcf5ef2aSThomas Huth             break;
1795fcf5ef2aSThomas Huth         case 2:
1796fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1797fcf5ef2aSThomas Huth             break;
1798fcf5ef2aSThomas Huth         case 3:
1799fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1800fcf5ef2aSThomas Huth             break;
1801fcf5ef2aSThomas Huth         }
1802fcf5ef2aSThomas Huth     }
1803fcf5ef2aSThomas Huth 
1804fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1805fcf5ef2aSThomas Huth 
1806418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1807fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1808fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1809fcf5ef2aSThomas Huth                                   &env->sse_status);
1810fcf5ef2aSThomas Huth     }
1811fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1812fcf5ef2aSThomas Huth }
1813fcf5ef2aSThomas Huth 
1814fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1815fcf5ef2aSThomas Huth                                   uint32_t mode)
1816fcf5ef2aSThomas Huth {
1817418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1818fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1819fcf5ef2aSThomas Huth 
1820fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1821fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1822fcf5ef2aSThomas Huth         switch (mode & 3) {
1823fcf5ef2aSThomas Huth         case 0:
1824fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1825fcf5ef2aSThomas Huth             break;
1826fcf5ef2aSThomas Huth         case 1:
1827fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1828fcf5ef2aSThomas Huth             break;
1829fcf5ef2aSThomas Huth         case 2:
1830fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1831fcf5ef2aSThomas Huth             break;
1832fcf5ef2aSThomas Huth         case 3:
1833fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1834fcf5ef2aSThomas Huth             break;
1835fcf5ef2aSThomas Huth         }
1836fcf5ef2aSThomas Huth     }
1837fcf5ef2aSThomas Huth 
1838fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1839fcf5ef2aSThomas Huth 
1840418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1841fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1842fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1843fcf5ef2aSThomas Huth                                   &env->sse_status);
1844fcf5ef2aSThomas Huth     }
1845fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1846fcf5ef2aSThomas Huth }
1847fcf5ef2aSThomas Huth 
1848fcf5ef2aSThomas Huth #define FBLENDP(d, s, m) (m ? s : d)
1849fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendps, L, 4, FBLENDP)
1850fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP)
1851fcf5ef2aSThomas Huth SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)
1852fcf5ef2aSThomas Huth 
1853fcf5ef2aSThomas Huth void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1854fcf5ef2aSThomas Huth {
1855bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
1856fcf5ef2aSThomas Huth 
1857bf30ad8cSPaolo Bonzini     /*
1858bf30ad8cSPaolo Bonzini      * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1859bf30ad8cSPaolo Bonzini      * to correctly round the intermediate results
1860bf30ad8cSPaolo Bonzini      */
1861fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1862bf30ad8cSPaolo Bonzini         prod1 = float32_mul(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status);
1863bf30ad8cSPaolo Bonzini     } else {
1864bf30ad8cSPaolo Bonzini         prod1 = float32_zero;
1865fcf5ef2aSThomas Huth     }
1866fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1867bf30ad8cSPaolo Bonzini         prod2 = float32_mul(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status);
1868bf30ad8cSPaolo Bonzini     } else {
1869bf30ad8cSPaolo Bonzini         prod2 = float32_zero;
1870fcf5ef2aSThomas Huth     }
1871bf30ad8cSPaolo Bonzini     temp2 = float32_add(prod1, prod2, &env->sse_status);
1872fcf5ef2aSThomas Huth     if (mask & (1 << 6)) {
1873bf30ad8cSPaolo Bonzini         prod1 = float32_mul(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status);
1874bf30ad8cSPaolo Bonzini     } else {
1875bf30ad8cSPaolo Bonzini         prod1 = float32_zero;
1876fcf5ef2aSThomas Huth     }
1877fcf5ef2aSThomas Huth     if (mask & (1 << 7)) {
1878bf30ad8cSPaolo Bonzini         prod2 = float32_mul(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status);
1879bf30ad8cSPaolo Bonzini     } else {
1880bf30ad8cSPaolo Bonzini         prod2 = float32_zero;
1881fcf5ef2aSThomas Huth     }
1882bf30ad8cSPaolo Bonzini     temp3 = float32_add(prod1, prod2, &env->sse_status);
1883bf30ad8cSPaolo Bonzini     temp4 = float32_add(temp2, temp3, &env->sse_status);
1884bf30ad8cSPaolo Bonzini 
1885bf30ad8cSPaolo Bonzini     d->ZMM_S(0) = (mask & (1 << 0)) ? temp4 : float32_zero;
1886bf30ad8cSPaolo Bonzini     d->ZMM_S(1) = (mask & (1 << 1)) ? temp4 : float32_zero;
1887bf30ad8cSPaolo Bonzini     d->ZMM_S(2) = (mask & (1 << 2)) ? temp4 : float32_zero;
1888bf30ad8cSPaolo Bonzini     d->ZMM_S(3) = (mask & (1 << 3)) ? temp4 : float32_zero;
1889fcf5ef2aSThomas Huth }
1890fcf5ef2aSThomas Huth 
1891fcf5ef2aSThomas Huth void glue(helper_dppd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1892fcf5ef2aSThomas Huth {
1893bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1894fcf5ef2aSThomas Huth 
1895fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1896bf30ad8cSPaolo Bonzini         prod1 = float64_mul(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1897bf30ad8cSPaolo Bonzini     } else {
1898bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1899fcf5ef2aSThomas Huth     }
1900fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1901bf30ad8cSPaolo Bonzini         prod2 = float64_mul(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1902bf30ad8cSPaolo Bonzini     } else {
1903bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1904fcf5ef2aSThomas Huth     }
1905bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1906bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1907bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1908fcf5ef2aSThomas Huth }
1909fcf5ef2aSThomas Huth 
1910fcf5ef2aSThomas Huth void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1911fcf5ef2aSThomas Huth                                   uint32_t offset)
1912fcf5ef2aSThomas Huth {
1913*d45b0de6SPaul Brook     Reg *v = d;
1914*d45b0de6SPaul Brook     int i, j;
1915*d45b0de6SPaul Brook     uint16_t r[8];
1916fcf5ef2aSThomas Huth 
1917*d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; ) {
1918*d45b0de6SPaul Brook         int s0 = (j * 2) + ((offset & 3) << 2);
1919*d45b0de6SPaul Brook         int d0 = (j * 2) + ((offset & 4) << 0);
1920*d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, d0++) {
1921*d45b0de6SPaul Brook             r[i] = 0;
1922*d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0));
1923*d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1));
1924*d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2));
1925*d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3));
1926fcf5ef2aSThomas Huth         }
1927*d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, j++) {
1928*d45b0de6SPaul Brook             d->W(j) = r[i];
1929*d45b0de6SPaul Brook         }
1930*d45b0de6SPaul Brook         offset >>= 3;
1931*d45b0de6SPaul Brook     }
1932fcf5ef2aSThomas Huth }
1933fcf5ef2aSThomas Huth 
1934fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1935fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
1936fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1937fcf5ef2aSThomas Huth 
1938fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1939fcf5ef2aSThomas Huth {
1940d1da229fSPaul Brook     target_long val, limit;
1941fcf5ef2aSThomas Huth 
1942fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1943fcf5ef2aSThomas Huth     if (ctrl >> 8) {
1944d1da229fSPaul Brook         val = (target_long)env->regs[reg];
1945fcf5ef2aSThomas Huth     } else {
1946d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
1947fcf5ef2aSThomas Huth     }
1948fcf5ef2aSThomas Huth     if (ctrl & 1) {
1949d1da229fSPaul Brook         limit = 8;
1950fcf5ef2aSThomas Huth     } else {
1951d1da229fSPaul Brook         limit = 16;
1952fcf5ef2aSThomas Huth     }
1953d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
1954d1da229fSPaul Brook         return limit;
1955fcf5ef2aSThomas Huth     }
1956d1da229fSPaul Brook     return abs1(val);
1957fcf5ef2aSThomas Huth }
1958fcf5ef2aSThomas Huth 
1959fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
1960fcf5ef2aSThomas Huth {
1961fcf5ef2aSThomas Huth     int val = 0;
1962fcf5ef2aSThomas Huth 
1963fcf5ef2aSThomas Huth     if (ctrl & 1) {
1964fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
1965fcf5ef2aSThomas Huth             val++;
1966fcf5ef2aSThomas Huth         }
1967fcf5ef2aSThomas Huth     } else {
1968fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
1969fcf5ef2aSThomas Huth             val++;
1970fcf5ef2aSThomas Huth         }
1971fcf5ef2aSThomas Huth     }
1972fcf5ef2aSThomas Huth 
1973fcf5ef2aSThomas Huth     return val;
1974fcf5ef2aSThomas Huth }
1975fcf5ef2aSThomas Huth 
1976fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
1977fcf5ef2aSThomas Huth {
1978fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
1979fcf5ef2aSThomas Huth     case 0:
1980fcf5ef2aSThomas Huth         return r->B(i);
1981fcf5ef2aSThomas Huth     case 1:
1982fcf5ef2aSThomas Huth         return r->W(i);
1983fcf5ef2aSThomas Huth     case 2:
1984fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
1985fcf5ef2aSThomas Huth     case 3:
1986fcf5ef2aSThomas Huth     default:
1987fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
1988fcf5ef2aSThomas Huth     }
1989fcf5ef2aSThomas Huth }
1990fcf5ef2aSThomas Huth 
1991fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
1992fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
1993fcf5ef2aSThomas Huth {
1994fcf5ef2aSThomas Huth     unsigned int res = 0;
1995fcf5ef2aSThomas Huth     int v;
1996fcf5ef2aSThomas Huth     int j, i;
1997fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
1998fcf5ef2aSThomas Huth 
1999fcf5ef2aSThomas Huth     valids--;
2000fcf5ef2aSThomas Huth     validd--;
2001fcf5ef2aSThomas Huth 
2002fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2003fcf5ef2aSThomas Huth 
2004fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2005fcf5ef2aSThomas Huth     case 0:
2006fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2007fcf5ef2aSThomas Huth             res <<= 1;
2008fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2009fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2010fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2011fcf5ef2aSThomas Huth             }
2012fcf5ef2aSThomas Huth         }
2013fcf5ef2aSThomas Huth         break;
2014fcf5ef2aSThomas Huth     case 1:
2015fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2016fcf5ef2aSThomas Huth             res <<= 1;
2017fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2018fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2019fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2020fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2021fcf5ef2aSThomas Huth             }
2022fcf5ef2aSThomas Huth         }
2023fcf5ef2aSThomas Huth         break;
2024fcf5ef2aSThomas Huth     case 2:
2025fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2026fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2027fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2028fcf5ef2aSThomas Huth             res <<= 1;
2029fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2030fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2031fcf5ef2aSThomas Huth         }
2032fcf5ef2aSThomas Huth         break;
2033fcf5ef2aSThomas Huth     case 3:
2034ae35eea7SJoseph Myers         if (validd == -1) {
2035ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2036ae35eea7SJoseph Myers             break;
2037ae35eea7SJoseph Myers         }
2038bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2039fcf5ef2aSThomas Huth             res <<= 1;
2040fcf5ef2aSThomas Huth             v = 1;
2041bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2042fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2043fcf5ef2aSThomas Huth             }
2044fcf5ef2aSThomas Huth             res |= v;
2045fcf5ef2aSThomas Huth         }
2046fcf5ef2aSThomas Huth         break;
2047fcf5ef2aSThomas Huth     }
2048fcf5ef2aSThomas Huth 
2049fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2050fcf5ef2aSThomas Huth     case 1:
2051fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2052fcf5ef2aSThomas Huth         break;
2053fcf5ef2aSThomas Huth     case 3:
2054fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2055fcf5ef2aSThomas Huth         break;
2056fcf5ef2aSThomas Huth     }
2057fcf5ef2aSThomas Huth 
2058fcf5ef2aSThomas Huth     if (res) {
2059fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2060fcf5ef2aSThomas Huth     }
2061fcf5ef2aSThomas Huth     if (res & 1) {
2062fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2063fcf5ef2aSThomas Huth     }
2064fcf5ef2aSThomas Huth 
2065fcf5ef2aSThomas Huth     return res;
2066fcf5ef2aSThomas Huth }
2067fcf5ef2aSThomas Huth 
2068fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2069fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2070fcf5ef2aSThomas Huth {
2071fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2072fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2073fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2074fcf5ef2aSThomas Huth 
2075fcf5ef2aSThomas Huth     if (res) {
2076fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2077fcf5ef2aSThomas Huth     } else {
2078fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2079fcf5ef2aSThomas Huth     }
2080fcf5ef2aSThomas Huth }
2081fcf5ef2aSThomas Huth 
2082fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2083fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2084fcf5ef2aSThomas Huth {
2085fcf5ef2aSThomas Huth     int i;
2086fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2087fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2088fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2089fcf5ef2aSThomas Huth 
2090fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2091fcf5ef2aSThomas Huth         if (ctrl & 1) {
2092fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2093fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2094fcf5ef2aSThomas Huth             }
2095fcf5ef2aSThomas Huth         } else {
2096fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2097fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2098fcf5ef2aSThomas Huth             }
2099fcf5ef2aSThomas Huth         }
2100fcf5ef2aSThomas Huth     } else {
2101fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2102fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2103fcf5ef2aSThomas Huth     }
2104fcf5ef2aSThomas Huth }
2105fcf5ef2aSThomas Huth 
2106fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2107fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2108fcf5ef2aSThomas Huth {
2109fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2110fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2111fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2112fcf5ef2aSThomas Huth 
2113fcf5ef2aSThomas Huth     if (res) {
2114fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2115fcf5ef2aSThomas Huth     } else {
2116fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2117fcf5ef2aSThomas Huth     }
2118fcf5ef2aSThomas Huth }
2119fcf5ef2aSThomas Huth 
2120fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2121fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2122fcf5ef2aSThomas Huth {
2123fcf5ef2aSThomas Huth     int i;
2124fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2125fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2126fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2127fcf5ef2aSThomas Huth 
2128fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2129fcf5ef2aSThomas Huth         if (ctrl & 1) {
2130fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2131fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2132fcf5ef2aSThomas Huth             }
2133fcf5ef2aSThomas Huth         } else {
2134fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2135fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2136fcf5ef2aSThomas Huth             }
2137fcf5ef2aSThomas Huth         }
2138fcf5ef2aSThomas Huth     } else {
2139fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2140fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2141fcf5ef2aSThomas Huth     }
2142fcf5ef2aSThomas Huth }
2143fcf5ef2aSThomas Huth 
2144fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2145fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2146fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2147fcf5ef2aSThomas Huth {
2148fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2149fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2150fcf5ef2aSThomas Huth 
2151fcf5ef2aSThomas Huth     while (len--) {
2152fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2153fcf5ef2aSThomas Huth     }
2154fcf5ef2aSThomas Huth 
2155fcf5ef2aSThomas Huth     return crc;
2156fcf5ef2aSThomas Huth }
2157fcf5ef2aSThomas Huth 
2158fcf5ef2aSThomas Huth void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2159fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2160fcf5ef2aSThomas Huth {
2161fcf5ef2aSThomas Huth     uint64_t ah, al, b, resh, resl;
2162fcf5ef2aSThomas Huth 
2163fcf5ef2aSThomas Huth     ah = 0;
2164fcf5ef2aSThomas Huth     al = d->Q((ctrl & 1) != 0);
2165fcf5ef2aSThomas Huth     b = s->Q((ctrl & 16) != 0);
2166fcf5ef2aSThomas Huth     resh = resl = 0;
2167fcf5ef2aSThomas Huth 
2168fcf5ef2aSThomas Huth     while (b) {
2169fcf5ef2aSThomas Huth         if (b & 1) {
2170fcf5ef2aSThomas Huth             resl ^= al;
2171fcf5ef2aSThomas Huth             resh ^= ah;
2172fcf5ef2aSThomas Huth         }
2173fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2174fcf5ef2aSThomas Huth         al <<= 1;
2175fcf5ef2aSThomas Huth         b >>= 1;
2176fcf5ef2aSThomas Huth     }
2177fcf5ef2aSThomas Huth 
2178fcf5ef2aSThomas Huth     d->Q(0) = resl;
2179fcf5ef2aSThomas Huth     d->Q(1) = resh;
2180fcf5ef2aSThomas Huth }
2181fcf5ef2aSThomas Huth 
2182fcf5ef2aSThomas Huth void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2183fcf5ef2aSThomas Huth {
2184fcf5ef2aSThomas Huth     int i;
2185fcf5ef2aSThomas Huth     Reg st = *d;
2186fcf5ef2aSThomas Huth     Reg rk = *s;
2187fcf5ef2aSThomas Huth 
2188fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2189fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4*i+0])] ^
2190fcf5ef2aSThomas Huth                                     AES_Td1[st.B(AES_ishifts[4*i+1])] ^
2191fcf5ef2aSThomas Huth                                     AES_Td2[st.B(AES_ishifts[4*i+2])] ^
2192fcf5ef2aSThomas Huth                                     AES_Td3[st.B(AES_ishifts[4*i+3])]);
2193fcf5ef2aSThomas Huth     }
2194fcf5ef2aSThomas Huth }
2195fcf5ef2aSThomas Huth 
2196fcf5ef2aSThomas Huth void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2197fcf5ef2aSThomas Huth {
2198fcf5ef2aSThomas Huth     int i;
2199fcf5ef2aSThomas Huth     Reg st = *d;
2200fcf5ef2aSThomas Huth     Reg rk = *s;
2201fcf5ef2aSThomas Huth 
2202fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2203fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i])]);
2204fcf5ef2aSThomas Huth     }
2205fcf5ef2aSThomas Huth }
2206fcf5ef2aSThomas Huth 
2207fcf5ef2aSThomas Huth void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2208fcf5ef2aSThomas Huth {
2209fcf5ef2aSThomas Huth     int i;
2210fcf5ef2aSThomas Huth     Reg st = *d;
2211fcf5ef2aSThomas Huth     Reg rk = *s;
2212fcf5ef2aSThomas Huth 
2213fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2214fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4*i+0])] ^
2215fcf5ef2aSThomas Huth                                     AES_Te1[st.B(AES_shifts[4*i+1])] ^
2216fcf5ef2aSThomas Huth                                     AES_Te2[st.B(AES_shifts[4*i+2])] ^
2217fcf5ef2aSThomas Huth                                     AES_Te3[st.B(AES_shifts[4*i+3])]);
2218fcf5ef2aSThomas Huth     }
2219fcf5ef2aSThomas Huth }
2220fcf5ef2aSThomas Huth 
2221fcf5ef2aSThomas Huth void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2222fcf5ef2aSThomas Huth {
2223fcf5ef2aSThomas Huth     int i;
2224fcf5ef2aSThomas Huth     Reg st = *d;
2225fcf5ef2aSThomas Huth     Reg rk = *s;
2226fcf5ef2aSThomas Huth 
2227fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2228fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i])]);
2229fcf5ef2aSThomas Huth     }
2230fcf5ef2aSThomas Huth 
2231fcf5ef2aSThomas Huth }
2232fcf5ef2aSThomas Huth 
2233fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2234fcf5ef2aSThomas Huth {
2235fcf5ef2aSThomas Huth     int i;
2236fcf5ef2aSThomas Huth     Reg tmp = *s;
2237fcf5ef2aSThomas Huth 
2238fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2239fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4*i+0)][0] ^
2240fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+1)][1] ^
2241fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+2)][2] ^
2242fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+3)][3]);
2243fcf5ef2aSThomas Huth     }
2244fcf5ef2aSThomas Huth }
2245fcf5ef2aSThomas Huth 
2246fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2247fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2248fcf5ef2aSThomas Huth {
2249fcf5ef2aSThomas Huth     int i;
2250fcf5ef2aSThomas Huth     Reg tmp = *s;
2251fcf5ef2aSThomas Huth 
2252fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2253fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2254fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2255fcf5ef2aSThomas Huth     }
2256fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2257fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2258fcf5ef2aSThomas Huth }
2259fcf5ef2aSThomas Huth #endif
2260fcf5ef2aSThomas Huth 
2261fcf5ef2aSThomas Huth #undef SHIFT
2262fcf5ef2aSThomas Huth #undef XMM_ONLY
2263fcf5ef2aSThomas Huth #undef Reg
2264fcf5ef2aSThomas Huth #undef B
2265fcf5ef2aSThomas Huth #undef W
2266fcf5ef2aSThomas Huth #undef L
2267fcf5ef2aSThomas Huth #undef Q
2268fcf5ef2aSThomas Huth #undef SUFFIX
2269d22697ddSPaolo Bonzini #undef SIZE
2270