xref: /openbmc/qemu/target/i386/ops_sse.h (revision 620f75566a5d81d7b82b3788b83d0b95c7d21dcd)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *  Copyright (c) 2008 Intel Corporation  <andrew.zaborowski@intel.com>
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10d9ff33adSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "crypto/aes.h"
22fcf5ef2aSThomas Huth 
23fcf5ef2aSThomas Huth #if SHIFT == 0
24fcf5ef2aSThomas Huth #define Reg MMXReg
25fcf5ef2aSThomas Huth #define XMM_ONLY(...)
26fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
27fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
28fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
29fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
30fcf5ef2aSThomas Huth #define SUFFIX _mmx
31fcf5ef2aSThomas Huth #else
32fcf5ef2aSThomas Huth #define Reg ZMMReg
33fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
34fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
35fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
36fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
37fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
38fcf5ef2aSThomas Huth #define SUFFIX _xmm
39fcf5ef2aSThomas Huth #endif
40fcf5ef2aSThomas Huth 
4118592d2eSPaul Brook #define LANE_WIDTH (SHIFT ? 16 : 8)
42d45b0de6SPaul Brook #define PACK_WIDTH (LANE_WIDTH / 2)
4318592d2eSPaul Brook 
4418592d2eSPaul Brook #if SHIFT == 0
4518592d2eSPaul Brook #define FPSRL(x, c) ((x) >> shift)
4618592d2eSPaul Brook #define FPSRAW(x, c) ((int16_t)(x) >> shift)
4718592d2eSPaul Brook #define FPSRAL(x, c) ((int32_t)(x) >> shift)
4818592d2eSPaul Brook #define FPSLL(x, c) ((x) << shift)
49fcf5ef2aSThomas Huth #endif
5018592d2eSPaul Brook 
51f05f9789SPaolo Bonzini void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
5218592d2eSPaul Brook {
5318592d2eSPaul Brook     int shift;
5418592d2eSPaul Brook     if (c->Q(0) > 15) {
5518592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
5618592d2eSPaul Brook             d->Q(i) = 0;
5718592d2eSPaul Brook         }
58fcf5ef2aSThomas Huth     } else {
5918592d2eSPaul Brook         shift = c->B(0);
6018592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
6118592d2eSPaul Brook             d->W(i) = FPSRL(s->W(i), shift);
6218592d2eSPaul Brook         }
63fcf5ef2aSThomas Huth     }
64fcf5ef2aSThomas Huth }
65fcf5ef2aSThomas Huth 
66f05f9789SPaolo Bonzini void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
67fcf5ef2aSThomas Huth {
68fcf5ef2aSThomas Huth     int shift;
6918592d2eSPaul Brook     if (c->Q(0) > 15) {
7018592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
7118592d2eSPaul Brook             d->Q(i) = 0;
7218592d2eSPaul Brook         }
7318592d2eSPaul Brook     } else {
7418592d2eSPaul Brook         shift = c->B(0);
7518592d2eSPaul Brook         for (int i = 0; i < 4 << SHIFT; i++) {
7618592d2eSPaul Brook             d->W(i) = FPSLL(s->W(i), shift);
7718592d2eSPaul Brook         }
7818592d2eSPaul Brook     }
7918592d2eSPaul Brook }
80fcf5ef2aSThomas Huth 
81f05f9789SPaolo Bonzini void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
8218592d2eSPaul Brook {
8318592d2eSPaul Brook     int shift;
8418592d2eSPaul Brook     if (c->Q(0) > 15) {
85fcf5ef2aSThomas Huth         shift = 15;
86fcf5ef2aSThomas Huth     } else {
8718592d2eSPaul Brook         shift = c->B(0);
88fcf5ef2aSThomas Huth     }
8918592d2eSPaul Brook     for (int i = 0; i < 4 << SHIFT; i++) {
9018592d2eSPaul Brook         d->W(i) = FPSRAW(s->W(i), shift);
9118592d2eSPaul Brook     }
92fcf5ef2aSThomas Huth }
93fcf5ef2aSThomas Huth 
94f05f9789SPaolo Bonzini void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
95fcf5ef2aSThomas Huth {
96fcf5ef2aSThomas Huth     int shift;
9718592d2eSPaul Brook     if (c->Q(0) > 31) {
9818592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
9918592d2eSPaul Brook             d->Q(i) = 0;
10018592d2eSPaul Brook         }
101fcf5ef2aSThomas Huth     } else {
10218592d2eSPaul Brook         shift = c->B(0);
10318592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
10418592d2eSPaul Brook             d->L(i) = FPSRL(s->L(i), shift);
10518592d2eSPaul Brook         }
106fcf5ef2aSThomas Huth     }
107fcf5ef2aSThomas Huth }
108fcf5ef2aSThomas Huth 
109f05f9789SPaolo Bonzini void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
110fcf5ef2aSThomas Huth {
111fcf5ef2aSThomas Huth     int shift;
11218592d2eSPaul Brook     if (c->Q(0) > 31) {
11318592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
11418592d2eSPaul Brook             d->Q(i) = 0;
11518592d2eSPaul Brook         }
116fcf5ef2aSThomas Huth     } else {
11718592d2eSPaul Brook         shift = c->B(0);
11818592d2eSPaul Brook         for (int i = 0; i < 2 << SHIFT; i++) {
11918592d2eSPaul Brook             d->L(i) = FPSLL(s->L(i), shift);
12018592d2eSPaul Brook         }
121fcf5ef2aSThomas Huth     }
122fcf5ef2aSThomas Huth }
123fcf5ef2aSThomas Huth 
124f05f9789SPaolo Bonzini void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
125fcf5ef2aSThomas Huth {
126fcf5ef2aSThomas Huth     int shift;
12718592d2eSPaul Brook     if (c->Q(0) > 31) {
128fcf5ef2aSThomas Huth         shift = 31;
129fcf5ef2aSThomas Huth     } else {
13018592d2eSPaul Brook         shift = c->B(0);
131fcf5ef2aSThomas Huth     }
13218592d2eSPaul Brook     for (int i = 0; i < 2 << SHIFT; i++) {
13318592d2eSPaul Brook         d->L(i) = FPSRAL(s->L(i), shift);
13418592d2eSPaul Brook     }
135fcf5ef2aSThomas Huth }
136fcf5ef2aSThomas Huth 
137f05f9789SPaolo Bonzini void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
138fcf5ef2aSThomas Huth {
139fcf5ef2aSThomas Huth     int shift;
14018592d2eSPaul Brook     if (c->Q(0) > 63) {
14118592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14218592d2eSPaul Brook             d->Q(i) = 0;
14318592d2eSPaul Brook         }
144fcf5ef2aSThomas Huth     } else {
14518592d2eSPaul Brook         shift = c->B(0);
14618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
14718592d2eSPaul Brook             d->Q(i) = FPSRL(s->Q(i), shift);
14818592d2eSPaul Brook         }
149fcf5ef2aSThomas Huth     }
150fcf5ef2aSThomas Huth }
151fcf5ef2aSThomas Huth 
152f05f9789SPaolo Bonzini void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
153fcf5ef2aSThomas Huth {
154fcf5ef2aSThomas Huth     int shift;
15518592d2eSPaul Brook     if (c->Q(0) > 63) {
15618592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
15718592d2eSPaul Brook             d->Q(i) = 0;
15818592d2eSPaul Brook         }
159fcf5ef2aSThomas Huth     } else {
16018592d2eSPaul Brook         shift = c->B(0);
16118592d2eSPaul Brook         for (int i = 0; i < 1 << SHIFT; i++) {
16218592d2eSPaul Brook             d->Q(i) = FPSLL(s->Q(i), shift);
16318592d2eSPaul Brook         }
164fcf5ef2aSThomas Huth     }
165fcf5ef2aSThomas Huth }
166fcf5ef2aSThomas Huth 
16718592d2eSPaul Brook #if SHIFT >= 1
168f05f9789SPaolo Bonzini void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
169fcf5ef2aSThomas Huth {
17018592d2eSPaul Brook     int shift, i, j;
171fcf5ef2aSThomas Huth 
17218592d2eSPaul Brook     shift = c->L(0);
173fcf5ef2aSThomas Huth     if (shift > 16) {
174fcf5ef2aSThomas Huth         shift = 16;
175fcf5ef2aSThomas Huth     }
17618592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
177fcf5ef2aSThomas Huth         for (i = 0; i < 16 - shift; i++) {
17818592d2eSPaul Brook             d->B(j + i) = s->B(j + i + shift);
179fcf5ef2aSThomas Huth         }
180fcf5ef2aSThomas Huth         for (i = 16 - shift; i < 16; i++) {
18118592d2eSPaul Brook             d->B(j + i) = 0;
18218592d2eSPaul Brook         }
183fcf5ef2aSThomas Huth     }
184fcf5ef2aSThomas Huth }
185fcf5ef2aSThomas Huth 
186f05f9789SPaolo Bonzini void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, Reg *c)
187fcf5ef2aSThomas Huth {
18818592d2eSPaul Brook     int shift, i, j;
189fcf5ef2aSThomas Huth 
19018592d2eSPaul Brook     shift = c->L(0);
191fcf5ef2aSThomas Huth     if (shift > 16) {
192fcf5ef2aSThomas Huth         shift = 16;
193fcf5ef2aSThomas Huth     }
19418592d2eSPaul Brook     for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) {
195fcf5ef2aSThomas Huth         for (i = 15; i >= shift; i--) {
19618592d2eSPaul Brook             d->B(j + i) = s->B(j + i - shift);
197fcf5ef2aSThomas Huth         }
198fcf5ef2aSThomas Huth         for (i = 0; i < shift; i++) {
19918592d2eSPaul Brook             d->B(j + i) = 0;
20018592d2eSPaul Brook         }
201fcf5ef2aSThomas Huth     }
202fcf5ef2aSThomas Huth }
203fcf5ef2aSThomas Huth #endif
204fcf5ef2aSThomas Huth 
205ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F)                        \
206fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
207fcf5ef2aSThomas Huth     {                                                           \
208ee04a3c8SPaul Brook         int n = num;                                            \
209ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
210ee04a3c8SPaul Brook             d->elem(i) = F(s->elem(i));                         \
211ee04a3c8SPaul Brook         }                                                       \
212fcf5ef2aSThomas Huth     }
213fcf5ef2aSThomas Huth 
214ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F)                        \
215f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)   \
216ee04a3c8SPaul Brook     {                                                           \
217ee04a3c8SPaul Brook         int n = num;                                            \
218ee04a3c8SPaul Brook         for (int i = 0; i < n; i++) {                           \
219ee04a3c8SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i));             \
220ee04a3c8SPaul Brook         }                                                       \
221ee04a3c8SPaul Brook     }
222ee04a3c8SPaul Brook 
223ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F)                                   \
224ee04a3c8SPaul Brook     SSE_HELPER_2(name, B, 8 << SHIFT, F)
225ee04a3c8SPaul Brook 
226fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
227ee04a3c8SPaul Brook     SSE_HELPER_2(name, W, 4 << SHIFT, F)
228fcf5ef2aSThomas Huth 
229fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
230ee04a3c8SPaul Brook     SSE_HELPER_2(name, L, 2 << SHIFT, F)
231fcf5ef2aSThomas Huth 
232fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
233ee04a3c8SPaul Brook     SSE_HELPER_2(name, Q, 1 << SHIFT, F)
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth #if SHIFT == 0
236fcf5ef2aSThomas Huth static inline int satub(int x)
237fcf5ef2aSThomas Huth {
238fcf5ef2aSThomas Huth     if (x < 0) {
239fcf5ef2aSThomas Huth         return 0;
240fcf5ef2aSThomas Huth     } else if (x > 255) {
241fcf5ef2aSThomas Huth         return 255;
242fcf5ef2aSThomas Huth     } else {
243fcf5ef2aSThomas Huth         return x;
244fcf5ef2aSThomas Huth     }
245fcf5ef2aSThomas Huth }
246fcf5ef2aSThomas Huth 
247fcf5ef2aSThomas Huth static inline int satuw(int x)
248fcf5ef2aSThomas Huth {
249fcf5ef2aSThomas Huth     if (x < 0) {
250fcf5ef2aSThomas Huth         return 0;
251fcf5ef2aSThomas Huth     } else if (x > 65535) {
252fcf5ef2aSThomas Huth         return 65535;
253fcf5ef2aSThomas Huth     } else {
254fcf5ef2aSThomas Huth         return x;
255fcf5ef2aSThomas Huth     }
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth static inline int satsb(int x)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     if (x < -128) {
261fcf5ef2aSThomas Huth         return -128;
262fcf5ef2aSThomas Huth     } else if (x > 127) {
263fcf5ef2aSThomas Huth         return 127;
264fcf5ef2aSThomas Huth     } else {
265fcf5ef2aSThomas Huth         return x;
266fcf5ef2aSThomas Huth     }
267fcf5ef2aSThomas Huth }
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth static inline int satsw(int x)
270fcf5ef2aSThomas Huth {
271fcf5ef2aSThomas Huth     if (x < -32768) {
272fcf5ef2aSThomas Huth         return -32768;
273fcf5ef2aSThomas Huth     } else if (x > 32767) {
274fcf5ef2aSThomas Huth         return 32767;
275fcf5ef2aSThomas Huth     } else {
276fcf5ef2aSThomas Huth         return x;
277fcf5ef2aSThomas Huth     }
278fcf5ef2aSThomas Huth }
279fcf5ef2aSThomas Huth 
280fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
281fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
282fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
283fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
284fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
285fcf5ef2aSThomas Huth 
286fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
287fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
288fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
289fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
290fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
291fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
292fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
293fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
294fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
295fcf5ef2aSThomas Huth 
296fcf5ef2aSThomas Huth #define FAND(a, b) ((a) & (b))
297fcf5ef2aSThomas Huth #define FANDN(a, b) ((~(a)) & (b))
298fcf5ef2aSThomas Huth #define FOR(a, b) ((a) | (b))
299fcf5ef2aSThomas Huth #define FXOR(a, b) ((a) ^ (b))
300fcf5ef2aSThomas Huth 
301fcf5ef2aSThomas Huth #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
302fcf5ef2aSThomas Huth #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
303fcf5ef2aSThomas Huth #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
304fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
305fcf5ef2aSThomas Huth 
306fcf5ef2aSThomas Huth #define FMULLW(a, b) ((a) * (b))
307fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
308fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
309fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
310fcf5ef2aSThomas Huth 
311fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
312fcf5ef2aSThomas Huth #endif
313fcf5ef2aSThomas Huth 
314fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddb, FADD)
315fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddw, FADD)
316fcf5ef2aSThomas Huth SSE_HELPER_L(helper_paddl, FADD)
317fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_paddq, FADD)
318fcf5ef2aSThomas Huth 
319fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubb, FSUB)
320fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubw, FSUB)
321fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psubl, FSUB)
322fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_psubq, FSUB)
323fcf5ef2aSThomas Huth 
324fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddusb, FADDUB)
325fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddsb, FADDSB)
326fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubusb, FSUBUB)
327fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubsb, FSUBSB)
328fcf5ef2aSThomas Huth 
329fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddusw, FADDUW)
330fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddsw, FADDSW)
331fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubusw, FSUBUW)
332fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubsw, FSUBSW)
333fcf5ef2aSThomas Huth 
334fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminub, FMINUB)
335fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxub, FMAXUB)
336fcf5ef2aSThomas Huth 
337fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminsw, FMINSW)
338fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxsw, FMAXSW)
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pand, FAND)
341fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pandn, FANDN)
342fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_por, FOR)
343fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pxor, FXOR)
344fcf5ef2aSThomas Huth 
345fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
346fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
347fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
348fcf5ef2aSThomas Huth 
349fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
350fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
351fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmullw, FMULLW)
354fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
355fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
356fcf5ef2aSThomas Huth 
357f05f9789SPaolo Bonzini #if SHIFT == 0
358f05f9789SPaolo Bonzini void glue(helper_pmulhrw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
359f05f9789SPaolo Bonzini {
360f05f9789SPaolo Bonzini     d->W(0) = FMULHRW(d->W(0), s->W(0));
361f05f9789SPaolo Bonzini     d->W(1) = FMULHRW(d->W(1), s->W(1));
362f05f9789SPaolo Bonzini     d->W(2) = FMULHRW(d->W(2), s->W(2));
363f05f9789SPaolo Bonzini     d->W(3) = FMULHRW(d->W(3), s->W(3));
364f05f9789SPaolo Bonzini }
365f05f9789SPaolo Bonzini #endif
366f05f9789SPaolo Bonzini 
367fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
368fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
369fcf5ef2aSThomas Huth 
370f05f9789SPaolo Bonzini void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
371fcf5ef2aSThomas Huth {
372e894bae8SPaul Brook     int i;
373e894bae8SPaul Brook 
374e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
375e894bae8SPaul Brook         d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2);
376e894bae8SPaul Brook     }
377fcf5ef2aSThomas Huth }
378fcf5ef2aSThomas Huth 
379f05f9789SPaolo Bonzini void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
380fcf5ef2aSThomas Huth {
381fcf5ef2aSThomas Huth     int i;
382fcf5ef2aSThomas Huth 
383fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
384e894bae8SPaul Brook         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) +
385e894bae8SPaul Brook             (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1);
386fcf5ef2aSThomas Huth     }
387fcf5ef2aSThomas Huth }
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth #if SHIFT == 0
390fcf5ef2aSThomas Huth static inline int abs1(int a)
391fcf5ef2aSThomas Huth {
392fcf5ef2aSThomas Huth     if (a < 0) {
393fcf5ef2aSThomas Huth         return -a;
394fcf5ef2aSThomas Huth     } else {
395fcf5ef2aSThomas Huth         return a;
396fcf5ef2aSThomas Huth     }
397fcf5ef2aSThomas Huth }
398fcf5ef2aSThomas Huth #endif
399f05f9789SPaolo Bonzini void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
400fcf5ef2aSThomas Huth {
401e894bae8SPaul Brook     int i;
402fcf5ef2aSThomas Huth 
403e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
404e894bae8SPaul Brook         unsigned int val = 0;
405e894bae8SPaul Brook         val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0));
406e894bae8SPaul Brook         val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1));
407e894bae8SPaul Brook         val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2));
408e894bae8SPaul Brook         val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3));
409e894bae8SPaul Brook         val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4));
410e894bae8SPaul Brook         val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5));
411e894bae8SPaul Brook         val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6));
412e894bae8SPaul Brook         val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7));
413e894bae8SPaul Brook         d->Q(i) = val;
414e894bae8SPaul Brook     }
415fcf5ef2aSThomas Huth }
416fcf5ef2aSThomas Huth 
417fd17264aSPaul Brook #if SHIFT < 2
418fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
419fcf5ef2aSThomas Huth                                   target_ulong a0)
420fcf5ef2aSThomas Huth {
421fcf5ef2aSThomas Huth     int i;
422fcf5ef2aSThomas Huth 
423fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
424fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
425fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
426fcf5ef2aSThomas Huth         }
427fcf5ef2aSThomas Huth     }
428fcf5ef2aSThomas Huth }
429fd17264aSPaul Brook #endif
430fcf5ef2aSThomas Huth 
431fcf5ef2aSThomas Huth void glue(helper_movl_mm_T0, SUFFIX)(Reg *d, uint32_t val)
432fcf5ef2aSThomas Huth {
433e894bae8SPaul Brook     int i;
434e894bae8SPaul Brook 
435fcf5ef2aSThomas Huth     d->L(0) = val;
436fcf5ef2aSThomas Huth     d->L(1) = 0;
437e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
438e894bae8SPaul Brook         d->Q(i) = 0;
439e894bae8SPaul Brook     }
440fcf5ef2aSThomas Huth }
441fcf5ef2aSThomas Huth 
442fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
443fcf5ef2aSThomas Huth void glue(helper_movq_mm_T0, SUFFIX)(Reg *d, uint64_t val)
444fcf5ef2aSThomas Huth {
445e894bae8SPaul Brook     int i;
446e894bae8SPaul Brook 
447fcf5ef2aSThomas Huth     d->Q(0) = val;
448e894bae8SPaul Brook     for (i = 1; i < (1 << SHIFT); i++) {
449e894bae8SPaul Brook         d->Q(i) = 0;
450e894bae8SPaul Brook     }
451fcf5ef2aSThomas Huth }
452fcf5ef2aSThomas Huth #endif
453fcf5ef2aSThomas Huth 
454d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do {      \
455d45b0de6SPaul Brook     r0 = a->F((order & 3) + offset);        \
456d45b0de6SPaul Brook     r1 = a->F(((order >> 2) & 3) + offset); \
457d45b0de6SPaul Brook     r2 = b->F(((order >> 4) & 3) + offset); \
458d45b0de6SPaul Brook     r3 = b->F(((order >> 6) & 3) + offset); \
459d45b0de6SPaul Brook     d->F(offset) = r0;                      \
460d45b0de6SPaul Brook     d->F(offset + 1) = r1;                  \
461d45b0de6SPaul Brook     d->F(offset + 2) = r2;                  \
462d45b0de6SPaul Brook     d->F(offset + 3) = r3;                  \
463d45b0de6SPaul Brook     } while (0)
464d45b0de6SPaul Brook 
465fcf5ef2aSThomas Huth #if SHIFT == 0
466fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
467fcf5ef2aSThomas Huth {
468d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
469fcf5ef2aSThomas Huth 
470d45b0de6SPaul Brook     SHUFFLE4(W, s, s, 0);
471fcf5ef2aSThomas Huth }
472fcf5ef2aSThomas Huth #else
473f05f9789SPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
474fcf5ef2aSThomas Huth {
475d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
476d45b0de6SPaul Brook     int i;
477fcf5ef2aSThomas Huth 
478d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
479d45b0de6SPaul Brook         SHUFFLE4(L, v, s, i);
480d45b0de6SPaul Brook     }
481fcf5ef2aSThomas Huth }
482fcf5ef2aSThomas Huth 
483f05f9789SPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *v, Reg *s, int order)
484fcf5ef2aSThomas Huth {
485d45b0de6SPaul Brook     uint64_t r0, r1;
486d45b0de6SPaul Brook     int i;
487fcf5ef2aSThomas Huth 
488d45b0de6SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
489d45b0de6SPaul Brook         r0 = v->Q(((order & 1) & 1) + i);
490d45b0de6SPaul Brook         r1 = s->Q(((order >> 1) & 1) + i);
491d45b0de6SPaul Brook         d->Q(i) = r0;
492d45b0de6SPaul Brook         d->Q(i + 1) = r1;
493d45b0de6SPaul Brook         order >>= 2;
494d45b0de6SPaul Brook     }
495fcf5ef2aSThomas Huth }
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
498fcf5ef2aSThomas Huth {
499d45b0de6SPaul Brook     uint32_t r0, r1, r2, r3;
500d45b0de6SPaul Brook     int i;
501fcf5ef2aSThomas Huth 
502d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
503d45b0de6SPaul Brook         SHUFFLE4(L, s, s, i);
504d45b0de6SPaul Brook     }
505fcf5ef2aSThomas Huth }
506fcf5ef2aSThomas Huth 
507fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
508fcf5ef2aSThomas Huth {
509d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
510d45b0de6SPaul Brook     int i, j;
511fcf5ef2aSThomas Huth 
512d45b0de6SPaul Brook     for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) {
513d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
514d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
515d45b0de6SPaul Brook     }
516fcf5ef2aSThomas Huth }
517fcf5ef2aSThomas Huth 
518fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
519fcf5ef2aSThomas Huth {
520d45b0de6SPaul Brook     uint16_t r0, r1, r2, r3;
521d45b0de6SPaul Brook     int i, j;
522fcf5ef2aSThomas Huth 
523d45b0de6SPaul Brook     for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) {
524d45b0de6SPaul Brook         d->Q(j) = s->Q(j);
525d45b0de6SPaul Brook         SHUFFLE4(W, s, s, i);
526d45b0de6SPaul Brook     }
527fcf5ef2aSThomas Huth }
528fcf5ef2aSThomas Huth #endif
529fcf5ef2aSThomas Huth 
5303403cafeSPaul Brook #if SHIFT >= 1
531fcf5ef2aSThomas Huth /* FPU ops */
532fcf5ef2aSThomas Huth /* XXX: not accurate */
533fcf5ef2aSThomas Huth 
5343403cafeSPaul Brook #define SSE_HELPER_P(name, F)                                           \
5353403cafeSPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
536f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
537fcf5ef2aSThomas Huth     {                                                                   \
5383403cafeSPaul Brook         int i;                                                          \
5393403cafeSPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
5403403cafeSPaul Brook             d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i));              \
541fcf5ef2aSThomas Huth         }                                                               \
5423403cafeSPaul Brook     }                                                                   \
5433403cafeSPaul Brook                                                                         \
5443403cafeSPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
545f05f9789SPaolo Bonzini             Reg *d, Reg *v, Reg *s)                                     \
5463403cafeSPaul Brook     {                                                                   \
5473403cafeSPaul Brook         int i;                                                          \
5483403cafeSPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
5493403cafeSPaul Brook             d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i));              \
5503403cafeSPaul Brook         }                                                               \
5513403cafeSPaul Brook     }
5523403cafeSPaul Brook 
5533403cafeSPaul Brook #if SHIFT == 1
5543403cafeSPaul Brook 
5553403cafeSPaul Brook #define SSE_HELPER_S(name, F)                                           \
5563403cafeSPaul Brook     SSE_HELPER_P(name, F)                                               \
557fcf5ef2aSThomas Huth                                                                         \
558f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
559fcf5ef2aSThomas Huth     {                                                                   \
5601de9e7e6SPaolo Bonzini         int i;                                                          \
5613403cafeSPaul Brook         d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0));                  \
5621de9e7e6SPaolo Bonzini         for (i = 1; i < 2 << SHIFT; i++) {                              \
5631de9e7e6SPaolo Bonzini             d->ZMM_L(i) = v->ZMM_L(i);                                  \
5641de9e7e6SPaolo Bonzini         }                                                               \
565fcf5ef2aSThomas Huth     }                                                                   \
566fcf5ef2aSThomas Huth                                                                         \
567f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)\
568fcf5ef2aSThomas Huth     {                                                                   \
5691de9e7e6SPaolo Bonzini         int i;                                                          \
5703403cafeSPaul Brook         d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0));                  \
5711de9e7e6SPaolo Bonzini         for (i = 1; i < 1 << SHIFT; i++) {                              \
5721de9e7e6SPaolo Bonzini             d->ZMM_Q(i) = v->ZMM_Q(i);                                  \
5731de9e7e6SPaolo Bonzini         }                                                               \
574fcf5ef2aSThomas Huth     }
575fcf5ef2aSThomas Huth 
5763403cafeSPaul Brook #else
5773403cafeSPaul Brook 
5783403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F)
5793403cafeSPaul Brook 
5803403cafeSPaul Brook #endif
5813403cafeSPaul Brook 
582fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
583fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
584fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
585fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
586fcf5ef2aSThomas Huth 
587fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
588fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
589fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
590fcf5ef2aSThomas Huth  */
591fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
592fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
593fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
594fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
595fcf5ef2aSThomas Huth 
596fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
597fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
598fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
599fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
600fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
601fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
602fcf5ef2aSThomas Huth 
6033403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6043403cafeSPaul Brook {
6053403cafeSPaul Brook     int i;
6063403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
6073403cafeSPaul Brook         d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status);
6083403cafeSPaul Brook     }
6093403cafeSPaul Brook }
6103403cafeSPaul Brook 
6113403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
6123403cafeSPaul Brook {
6133403cafeSPaul Brook     int i;
6143403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
6153403cafeSPaul Brook         d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status);
6163403cafeSPaul Brook     }
6173403cafeSPaul Brook }
6183403cafeSPaul Brook 
6193403cafeSPaul Brook #if SHIFT == 1
620*620f7556SPaolo Bonzini void helper_sqrtss(CPUX86State *env, Reg *d, Reg *v, Reg *s)
6213403cafeSPaul Brook {
622*620f7556SPaolo Bonzini     int i;
6233403cafeSPaul Brook     d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status);
624*620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
625*620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
626*620f7556SPaolo Bonzini     }
6273403cafeSPaul Brook }
6283403cafeSPaul Brook 
629*620f7556SPaolo Bonzini void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *v, Reg *s)
6303403cafeSPaul Brook {
631*620f7556SPaolo Bonzini     int i;
6323403cafeSPaul Brook     d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status);
633*620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
634*620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
635*620f7556SPaolo Bonzini     }
6363403cafeSPaul Brook }
6373403cafeSPaul Brook #endif
638fcf5ef2aSThomas Huth 
639fcf5ef2aSThomas Huth /* float to float conversions */
640ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
641fcf5ef2aSThomas Huth {
642fd17264aSPaul Brook     int i;
643fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
644fd17264aSPaul Brook         d->ZMM_D(i) = float32_to_float64(s->ZMM_S(i), &env->sse_status);
645fd17264aSPaul Brook     }
646fcf5ef2aSThomas Huth }
647fcf5ef2aSThomas Huth 
648ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
649fcf5ef2aSThomas Huth {
650fd17264aSPaul Brook     int i;
651fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
652fd17264aSPaul Brook          d->ZMM_S(i) = float64_to_float32(s->ZMM_D(i), &env->sse_status);
653fd17264aSPaul Brook     }
654fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
655fd17264aSPaul Brook          d->Q(i) = 0;
656fd17264aSPaul Brook     }
657fcf5ef2aSThomas Huth }
658fcf5ef2aSThomas Huth 
659fd17264aSPaul Brook #if SHIFT == 1
660*620f7556SPaolo Bonzini void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)
661fcf5ef2aSThomas Huth {
662*620f7556SPaolo Bonzini     int i;
663fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
664*620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
665*620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
666*620f7556SPaolo Bonzini     }
667fcf5ef2aSThomas Huth }
668fcf5ef2aSThomas Huth 
669*620f7556SPaolo Bonzini void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)
670fcf5ef2aSThomas Huth {
671*620f7556SPaolo Bonzini     int i;
672fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
673*620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
674*620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
675*620f7556SPaolo Bonzini     }
676fcf5ef2aSThomas Huth }
677fd17264aSPaul Brook #endif
678fcf5ef2aSThomas Huth 
679fcf5ef2aSThomas Huth /* integer to float */
680ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
681fcf5ef2aSThomas Huth {
682fd17264aSPaul Brook     int i;
683fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
684fd17264aSPaul Brook         d->ZMM_S(i) = int32_to_float32(s->ZMM_L(i), &env->sse_status);
685fd17264aSPaul Brook     }
686fcf5ef2aSThomas Huth }
687fcf5ef2aSThomas Huth 
688ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
689fcf5ef2aSThomas Huth {
690fd17264aSPaul Brook     int i;
691fd17264aSPaul Brook     for (i = 1 << SHIFT; --i >= 0; ) {
692fd17264aSPaul Brook         int32_t l = s->ZMM_L(i);
693fd17264aSPaul Brook         d->ZMM_D(i) = int32_to_float64(l, &env->sse_status);
694fd17264aSPaul Brook     }
695fcf5ef2aSThomas Huth }
696fcf5ef2aSThomas Huth 
697fd17264aSPaul Brook #if SHIFT == 1
698fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
699fcf5ef2aSThomas Huth {
700fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
701fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
702fcf5ef2aSThomas Huth }
703fcf5ef2aSThomas Huth 
704fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
705fcf5ef2aSThomas Huth {
706fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
707fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
708fcf5ef2aSThomas Huth }
709fcf5ef2aSThomas Huth 
710fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
711fcf5ef2aSThomas Huth {
712fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
713fcf5ef2aSThomas Huth }
714fcf5ef2aSThomas Huth 
715fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
716fcf5ef2aSThomas Huth {
717fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
718fcf5ef2aSThomas Huth }
719fcf5ef2aSThomas Huth 
720fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
721fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
722fcf5ef2aSThomas Huth {
723fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
724fcf5ef2aSThomas Huth }
725fcf5ef2aSThomas Huth 
726fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
727fcf5ef2aSThomas Huth {
728fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
729fcf5ef2aSThomas Huth }
730fcf5ef2aSThomas Huth #endif
731fcf5ef2aSThomas Huth 
732fd17264aSPaul Brook #endif
733fd17264aSPaul Brook 
734fcf5ef2aSThomas Huth /* float to integer */
7351e8a98b5SPeter Maydell 
736fd17264aSPaul Brook #if SHIFT == 1
7371e8a98b5SPeter Maydell /*
7381e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7391e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7401e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7411e8a98b5SPeter Maydell  */
7421e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7431e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7441e8a98b5SPeter Maydell     {                                                                   \
7451e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7461e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7471e8a98b5SPeter Maydell                                                                         \
7481e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7491e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7501e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7511e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7521e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7531e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7541e8a98b5SPeter Maydell         }                                                               \
7551e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7561e8a98b5SPeter Maydell         return r;                                                       \
7571e8a98b5SPeter Maydell     }
7581e8a98b5SPeter Maydell 
7591e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7601e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7611e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7621e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7631e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7641e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7651e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7661e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
767fd17264aSPaul Brook #endif
7681e8a98b5SPeter Maydell 
769ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
770fcf5ef2aSThomas Huth {
771fd17264aSPaul Brook     int i;
772fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
773fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32(s->ZMM_S(i), &env->sse_status);
774fd17264aSPaul Brook     }
775fcf5ef2aSThomas Huth }
776fcf5ef2aSThomas Huth 
777ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
778fcf5ef2aSThomas Huth {
779fd17264aSPaul Brook     int i;
780fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
781fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32(s->ZMM_D(i), &env->sse_status);
782fd17264aSPaul Brook     }
783fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
784fd17264aSPaul Brook          d->Q(i) = 0;
785fd17264aSPaul Brook     }
786fcf5ef2aSThomas Huth }
787fcf5ef2aSThomas Huth 
788fd17264aSPaul Brook #if SHIFT == 1
789fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
790fcf5ef2aSThomas Huth {
7911e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7921e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
793fcf5ef2aSThomas Huth }
794fcf5ef2aSThomas Huth 
795fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
796fcf5ef2aSThomas Huth {
7971e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7981e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
799fcf5ef2aSThomas Huth }
800fcf5ef2aSThomas Huth 
801fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
802fcf5ef2aSThomas Huth {
8031e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
804fcf5ef2aSThomas Huth }
805fcf5ef2aSThomas Huth 
806fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
807fcf5ef2aSThomas Huth {
8081e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
809fcf5ef2aSThomas Huth }
810fcf5ef2aSThomas Huth 
811fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
812fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
813fcf5ef2aSThomas Huth {
8141e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
815fcf5ef2aSThomas Huth }
816fcf5ef2aSThomas Huth 
817fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
818fcf5ef2aSThomas Huth {
8191e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
820fcf5ef2aSThomas Huth }
821fcf5ef2aSThomas Huth #endif
822fd17264aSPaul Brook #endif
823fcf5ef2aSThomas Huth 
824fcf5ef2aSThomas Huth /* float to integer truncated */
825ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
826fcf5ef2aSThomas Huth {
827fd17264aSPaul Brook     int i;
828fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
829fd17264aSPaul Brook         d->ZMM_L(i) = x86_float32_to_int32_round_to_zero(s->ZMM_S(i),
830fd17264aSPaul Brook                                                          &env->sse_status);
831fd17264aSPaul Brook     }
832fcf5ef2aSThomas Huth }
833fcf5ef2aSThomas Huth 
834ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
835fcf5ef2aSThomas Huth {
836fd17264aSPaul Brook     int i;
837fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
838fd17264aSPaul Brook         d->ZMM_L(i) = x86_float64_to_int32_round_to_zero(s->ZMM_D(i),
839fd17264aSPaul Brook                                                          &env->sse_status);
840fd17264aSPaul Brook     }
841fd17264aSPaul Brook     for (i >>= 1; i < 1 << SHIFT; i++) {
842fd17264aSPaul Brook          d->Q(i) = 0;
843fd17264aSPaul Brook     }
844fcf5ef2aSThomas Huth }
845fcf5ef2aSThomas Huth 
846fd17264aSPaul Brook #if SHIFT == 1
847fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
848fcf5ef2aSThomas Huth {
8491e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8501e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
851fcf5ef2aSThomas Huth }
852fcf5ef2aSThomas Huth 
853fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
854fcf5ef2aSThomas Huth {
8551e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8561e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
857fcf5ef2aSThomas Huth }
858fcf5ef2aSThomas Huth 
859fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
860fcf5ef2aSThomas Huth {
8611e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
862fcf5ef2aSThomas Huth }
863fcf5ef2aSThomas Huth 
864fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
865fcf5ef2aSThomas Huth {
8661e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
867fcf5ef2aSThomas Huth }
868fcf5ef2aSThomas Huth 
869fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
870fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
871fcf5ef2aSThomas Huth {
8721e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
873fcf5ef2aSThomas Huth }
874fcf5ef2aSThomas Huth 
875fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
876fcf5ef2aSThomas Huth {
8771e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
878fcf5ef2aSThomas Huth }
879fcf5ef2aSThomas Huth #endif
880fd17264aSPaul Brook #endif
881fcf5ef2aSThomas Huth 
882ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
883fcf5ef2aSThomas Huth {
884418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
8853403cafeSPaul Brook     int i;
8863403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
8873403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one,
8883403cafeSPaul Brook                                   float32_sqrt(s->ZMM_S(i), &env->sse_status),
889fcf5ef2aSThomas Huth                                   &env->sse_status);
8903403cafeSPaul Brook     }
891418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
892fcf5ef2aSThomas Huth }
893fcf5ef2aSThomas Huth 
894fd17264aSPaul Brook #if SHIFT == 1
895*620f7556SPaolo Bonzini void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)
896fcf5ef2aSThomas Huth {
897418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
898*620f7556SPaolo Bonzini     int i;
899fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
900fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
901fcf5ef2aSThomas Huth                               &env->sse_status);
902418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
903*620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
904*620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
905*620f7556SPaolo Bonzini     }
906fcf5ef2aSThomas Huth }
907fd17264aSPaul Brook #endif
908fcf5ef2aSThomas Huth 
909ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
910fcf5ef2aSThomas Huth {
911418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
9123403cafeSPaul Brook     int i;
9133403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
9143403cafeSPaul Brook         d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status);
9153403cafeSPaul Brook     }
916418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
917fcf5ef2aSThomas Huth }
918fcf5ef2aSThomas Huth 
919fd17264aSPaul Brook #if SHIFT == 1
920*620f7556SPaolo Bonzini void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)
921fcf5ef2aSThomas Huth {
922418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
923*620f7556SPaolo Bonzini     int i;
924fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
925*620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
926*620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
927*620f7556SPaolo Bonzini     }
928418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
929fcf5ef2aSThomas Huth }
930fd17264aSPaul Brook #endif
931fcf5ef2aSThomas Huth 
932fd17264aSPaul Brook #if SHIFT == 1
933fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
934fcf5ef2aSThomas Huth {
935fcf5ef2aSThomas Huth     uint64_t mask;
936fcf5ef2aSThomas Huth 
937fcf5ef2aSThomas Huth     if (len == 0) {
938fcf5ef2aSThomas Huth         mask = ~0LL;
939fcf5ef2aSThomas Huth     } else {
940fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
941fcf5ef2aSThomas Huth     }
942fcf5ef2aSThomas Huth     return (src >> shift) & mask;
943fcf5ef2aSThomas Huth }
944fcf5ef2aSThomas Huth 
945fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
946fcf5ef2aSThomas Huth {
947034668c3SPaolo Bonzini     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1) & 63, s->ZMM_B(0) & 63);
948fcf5ef2aSThomas Huth }
949fcf5ef2aSThomas Huth 
950fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
951fcf5ef2aSThomas Huth {
952fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
953fcf5ef2aSThomas Huth }
954fcf5ef2aSThomas Huth 
955ca4b1b43SPaolo Bonzini static inline uint64_t helper_insertq(uint64_t dest, uint64_t src, int shift, int len)
956fcf5ef2aSThomas Huth {
957fcf5ef2aSThomas Huth     uint64_t mask;
958fcf5ef2aSThomas Huth 
959fcf5ef2aSThomas Huth     if (len == 0) {
960fcf5ef2aSThomas Huth         mask = ~0ULL;
961fcf5ef2aSThomas Huth     } else {
962fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
963fcf5ef2aSThomas Huth     }
964ca4b1b43SPaolo Bonzini     return (dest & ~(mask << shift)) | ((src & mask) << shift);
965fcf5ef2aSThomas Huth }
966fcf5ef2aSThomas Huth 
967fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
968fcf5ef2aSThomas Huth {
969ca4b1b43SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), s->ZMM_Q(0), s->ZMM_B(9) & 63, s->ZMM_B(8) & 63);
970fcf5ef2aSThomas Huth }
971fcf5ef2aSThomas Huth 
972ca4b1b43SPaolo Bonzini void helper_insertq_i(CPUX86State *env, ZMMReg *d, ZMMReg *s, int index, int length)
973fcf5ef2aSThomas Huth {
974ca4b1b43SPaolo Bonzini     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), s->ZMM_Q(0), index, length);
975fcf5ef2aSThomas Huth }
976fd17264aSPaul Brook #endif
977fcf5ef2aSThomas Huth 
9786567ffb4SPaul Brook #define SSE_HELPER_HPS(name, F)  \
979f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
9806567ffb4SPaul Brook {                                                                 \
9816567ffb4SPaul Brook     float32 r[2 << SHIFT];                                        \
9826567ffb4SPaul Brook     int i, j, k;                                                  \
9836567ffb4SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {            \
9846567ffb4SPaul Brook         for (i = j = 0; j < 4; i++, j += 2) {                     \
9856567ffb4SPaul Brook             r[i + k] = F(v->ZMM_S(j + k), v->ZMM_S(j + k + 1), &env->sse_status); \
9866567ffb4SPaul Brook         }                                                         \
9876567ffb4SPaul Brook         for (j = 0; j < 4; i++, j += 2) {                         \
9886567ffb4SPaul Brook             r[i + k] = F(s->ZMM_S(j + k), s->ZMM_S(j + k + 1), &env->sse_status); \
9896567ffb4SPaul Brook         }                                                         \
9906567ffb4SPaul Brook     }                                                             \
9916567ffb4SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                            \
9926567ffb4SPaul Brook         d->ZMM_S(i) = r[i];                                       \
9936567ffb4SPaul Brook     }                                                             \
994fcf5ef2aSThomas Huth }
995fcf5ef2aSThomas Huth 
9966567ffb4SPaul Brook SSE_HELPER_HPS(haddps, float32_add)
9976567ffb4SPaul Brook SSE_HELPER_HPS(hsubps, float32_sub)
998fcf5ef2aSThomas Huth 
9996567ffb4SPaul Brook #define SSE_HELPER_HPD(name, F)  \
1000f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
10016567ffb4SPaul Brook {                                                                 \
10026567ffb4SPaul Brook     float64 r[1 << SHIFT];                                        \
10036567ffb4SPaul Brook     int i, j, k;                                                  \
10046567ffb4SPaul Brook     for (k = 0; k < 1 << SHIFT; k += LANE_WIDTH / 8) {            \
10056567ffb4SPaul Brook         for (i = j = 0; j < 2; i++, j += 2) {                     \
10066567ffb4SPaul Brook             r[i + k] = F(v->ZMM_D(j + k), v->ZMM_D(j + k + 1), &env->sse_status); \
10076567ffb4SPaul Brook         }                                                         \
10086567ffb4SPaul Brook         for (j = 0; j < 2; i++, j += 2) {                         \
10096567ffb4SPaul Brook             r[i + k] = F(s->ZMM_D(j + k), s->ZMM_D(j + k + 1), &env->sse_status); \
10106567ffb4SPaul Brook         }                                                         \
10116567ffb4SPaul Brook     }                                                             \
10126567ffb4SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {                            \
10136567ffb4SPaul Brook         d->ZMM_D(i) = r[i];                                       \
10146567ffb4SPaul Brook     }                                                             \
1015fcf5ef2aSThomas Huth }
1016fcf5ef2aSThomas Huth 
10176567ffb4SPaul Brook SSE_HELPER_HPD(haddpd, float64_add)
10186567ffb4SPaul Brook SSE_HELPER_HPD(hsubpd, float64_sub)
1019fcf5ef2aSThomas Huth 
1020f05f9789SPaolo Bonzini void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1021fcf5ef2aSThomas Huth {
10223403cafeSPaul Brook     int i;
10233403cafeSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 2) {
10243403cafeSPaul Brook         d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
10253403cafeSPaul Brook         d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
10263403cafeSPaul Brook     }
1027fcf5ef2aSThomas Huth }
1028fcf5ef2aSThomas Huth 
1029f05f9789SPaolo Bonzini void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1030fcf5ef2aSThomas Huth {
10313403cafeSPaul Brook     int i;
10323403cafeSPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
10333403cafeSPaul Brook         d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status);
10343403cafeSPaul Brook         d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status);
10353403cafeSPaul Brook     }
1036fcf5ef2aSThomas Huth }
1037fcf5ef2aSThomas Huth 
1038cbf4ad54SPaul Brook #define SSE_HELPER_CMP_P(name, F, C)                                    \
1039cbf4ad54SPaul Brook     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env,          \
1040f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
1041fcf5ef2aSThomas Huth     {                                                                   \
1042cbf4ad54SPaul Brook         int i;                                                          \
1043cbf4ad54SPaul Brook         for (i = 0; i < 2 << SHIFT; i++) {                              \
1044cbf4ad54SPaul Brook             d->ZMM_L(i) = C(F(32, v->ZMM_S(i), s->ZMM_S(i))) ? -1 : 0;  \
1045cbf4ad54SPaul Brook         }                                                               \
1046fcf5ef2aSThomas Huth     }                                                                   \
1047fcf5ef2aSThomas Huth                                                                         \
1048cbf4ad54SPaul Brook     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env,          \
1049f05f9789SPaolo Bonzini                                              Reg *d, Reg *v, Reg *s)    \
1050cbf4ad54SPaul Brook     {                                                                   \
1051cbf4ad54SPaul Brook         int i;                                                          \
1052cbf4ad54SPaul Brook         for (i = 0; i < 1 << SHIFT; i++) {                              \
1053cbf4ad54SPaul Brook             d->ZMM_Q(i) = C(F(64, v->ZMM_D(i), s->ZMM_D(i))) ? -1 : 0;  \
1054cbf4ad54SPaul Brook         }                                                               \
1055cbf4ad54SPaul Brook     }
1056cbf4ad54SPaul Brook 
1057cbf4ad54SPaul Brook #if SHIFT == 1
1058cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C)                                          \
1059cbf4ad54SPaul Brook     SSE_HELPER_CMP_P(name, F, C)                                            \
1060f05f9789SPaolo Bonzini     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1061fcf5ef2aSThomas Huth     {                                                                       \
10621de9e7e6SPaolo Bonzini         int i;                                                              \
1063cbf4ad54SPaul Brook         d->ZMM_L(0) = C(F(32, v->ZMM_S(0), s->ZMM_S(0))) ? -1 : 0;          \
10641de9e7e6SPaolo Bonzini         for (i = 1; i < 2 << SHIFT; i++) {                                  \
10651de9e7e6SPaolo Bonzini             d->ZMM_L(i) = v->ZMM_L(i);                                      \
10661de9e7e6SPaolo Bonzini         }                                                                   \
1067fcf5ef2aSThomas Huth     }                                                                       \
1068fcf5ef2aSThomas Huth                                                                             \
1069f05f9789SPaolo Bonzini     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *v, Reg *s)    \
1070fcf5ef2aSThomas Huth     {                                                                       \
10711de9e7e6SPaolo Bonzini         int i;                                                              \
1072cbf4ad54SPaul Brook         d->ZMM_Q(0) = C(F(64, v->ZMM_D(0), s->ZMM_D(0))) ? -1 : 0;          \
10731de9e7e6SPaolo Bonzini         for (i = 1; i < 1 << SHIFT; i++) {                                  \
10741de9e7e6SPaolo Bonzini             d->ZMM_Q(i) = v->ZMM_Q(i);                                      \
10751de9e7e6SPaolo Bonzini         }                                                                   \
1076fcf5ef2aSThomas Huth     }
1077fcf5ef2aSThomas Huth 
1078cbf4ad54SPaul Brook #define FPU_EQ(x) (x == float_relation_equal)
1079cbf4ad54SPaul Brook #define FPU_LT(x) (x == float_relation_less)
1080cbf4ad54SPaul Brook #define FPU_LE(x) (x <= float_relation_equal)
1081cbf4ad54SPaul Brook #define FPU_UNORD(x) (x == float_relation_unordered)
1082fcf5ef2aSThomas Huth 
1083cbf4ad54SPaul Brook #define FPU_CMPQ(size, a, b) \
1084cbf4ad54SPaul Brook     float ## size ## _compare_quiet(a, b, &env->sse_status)
1085cbf4ad54SPaul Brook #define FPU_CMPS(size, a, b) \
1086cbf4ad54SPaul Brook     float ## size ## _compare(a, b, &env->sse_status)
1087cbf4ad54SPaul Brook 
1088cbf4ad54SPaul Brook #else
1089cbf4ad54SPaul Brook #define SSE_HELPER_CMP(name, F, C) SSE_HELPER_CMP_P(name, F, C)
1090cbf4ad54SPaul Brook #endif
1091cbf4ad54SPaul Brook 
1092cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpeq, FPU_CMPQ, FPU_EQ)
1093cbf4ad54SPaul Brook SSE_HELPER_CMP(cmplt, FPU_CMPS, FPU_LT)
1094cbf4ad54SPaul Brook SSE_HELPER_CMP(cmple, FPU_CMPS, FPU_LE)
1095cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpunord, FPU_CMPQ,  FPU_UNORD)
1096cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpneq, FPU_CMPQ, !FPU_EQ)
1097cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnlt, FPU_CMPS, !FPU_LT)
1098cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpnle, FPU_CMPS, !FPU_LE)
1099cbf4ad54SPaul Brook SSE_HELPER_CMP(cmpord, FPU_CMPQ, !FPU_UNORD)
1100cbf4ad54SPaul Brook 
1101cbf4ad54SPaul Brook #undef SSE_HELPER_CMP
1102fcf5ef2aSThomas Huth 
1103fd17264aSPaul Brook #if SHIFT == 1
1104fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1107fcf5ef2aSThomas Huth {
110871bfd65cSRichard Henderson     FloatRelation ret;
1109fcf5ef2aSThomas Huth     float32 s0, s1;
1110fcf5ef2aSThomas Huth 
1111fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1112fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1113fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1114fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1115fcf5ef2aSThomas Huth }
1116fcf5ef2aSThomas Huth 
1117fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1118fcf5ef2aSThomas Huth {
111971bfd65cSRichard Henderson     FloatRelation ret;
1120fcf5ef2aSThomas Huth     float32 s0, s1;
1121fcf5ef2aSThomas Huth 
1122fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1123fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1124fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1125fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1126fcf5ef2aSThomas Huth }
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1129fcf5ef2aSThomas Huth {
113071bfd65cSRichard Henderson     FloatRelation ret;
1131fcf5ef2aSThomas Huth     float64 d0, d1;
1132fcf5ef2aSThomas Huth 
1133fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1134fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1135fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1136fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1137fcf5ef2aSThomas Huth }
1138fcf5ef2aSThomas Huth 
1139fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1140fcf5ef2aSThomas Huth {
114171bfd65cSRichard Henderson     FloatRelation ret;
1142fcf5ef2aSThomas Huth     float64 d0, d1;
1143fcf5ef2aSThomas Huth 
1144fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1145fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1146fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1147fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1148fcf5ef2aSThomas Huth }
1149fd17264aSPaul Brook #endif
1150fcf5ef2aSThomas Huth 
1151ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1152fcf5ef2aSThomas Huth {
1153fd17264aSPaul Brook     uint32_t mask;
1154fd17264aSPaul Brook     int i;
1155fcf5ef2aSThomas Huth 
1156fd17264aSPaul Brook     mask = 0;
1157fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1158fd17264aSPaul Brook         mask |= (s->ZMM_L(i) >> (31 - i)) & (1 << i);
1159fd17264aSPaul Brook     }
1160fd17264aSPaul Brook     return mask;
1161fcf5ef2aSThomas Huth }
1162fcf5ef2aSThomas Huth 
1163ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1164fcf5ef2aSThomas Huth {
1165fd17264aSPaul Brook     uint32_t mask;
1166fd17264aSPaul Brook     int i;
1167fcf5ef2aSThomas Huth 
1168fd17264aSPaul Brook     mask = 0;
1169fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1170fd17264aSPaul Brook         mask |= (s->ZMM_Q(i) >> (63 - i)) & (1 << i);
1171fd17264aSPaul Brook     }
1172fd17264aSPaul Brook     return mask;
1173fcf5ef2aSThomas Huth }
1174fcf5ef2aSThomas Huth 
1175fcf5ef2aSThomas Huth #endif
1176fcf5ef2aSThomas Huth 
1177fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1178fcf5ef2aSThomas Huth {
1179fcf5ef2aSThomas Huth     uint32_t val;
1180e894bae8SPaul Brook     int i;
1181fcf5ef2aSThomas Huth 
1182fcf5ef2aSThomas Huth     val = 0;
1183e894bae8SPaul Brook     for (i = 0; i < (1 << SHIFT); i++) {
1184e894bae8SPaul Brook         uint8_t byte = 0;
1185e894bae8SPaul Brook         byte |= (s->B(8 * i + 0) >> 7);
1186e894bae8SPaul Brook         byte |= (s->B(8 * i + 1) >> 6) & 0x02;
1187e894bae8SPaul Brook         byte |= (s->B(8 * i + 2) >> 5) & 0x04;
1188e894bae8SPaul Brook         byte |= (s->B(8 * i + 3) >> 4) & 0x08;
1189e894bae8SPaul Brook         byte |= (s->B(8 * i + 4) >> 3) & 0x10;
1190e894bae8SPaul Brook         byte |= (s->B(8 * i + 5) >> 2) & 0x20;
1191e894bae8SPaul Brook         byte |= (s->B(8 * i + 6) >> 1) & 0x40;
1192e894bae8SPaul Brook         byte |= (s->B(8 * i + 7)) & 0x80;
1193e894bae8SPaul Brook         val |= byte << (8 * i);
1194e894bae8SPaul Brook     }
1195fcf5ef2aSThomas Huth     return val;
1196fcf5ef2aSThomas Huth }
1197fcf5ef2aSThomas Huth 
1198d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \
1199d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env,      \
1200f05f9789SPaolo Bonzini         Reg *d, Reg *v, Reg *s)                               \
1201d45b0de6SPaul Brook {                                                             \
1202d45b0de6SPaul Brook     uint8_t r[PACK_WIDTH * 2];                                \
1203d45b0de6SPaul Brook     int j, k;                                                 \
1204d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) {            \
1205d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1206d45b0de6SPaul Brook             r[k] = F((int16_t)v->W(j + k));                   \
1207d45b0de6SPaul Brook         }                                                     \
1208d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {                    \
1209d45b0de6SPaul Brook             r[PACK_WIDTH + k] = F((int16_t)s->W(j + k));      \
1210d45b0de6SPaul Brook         }                                                     \
1211d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH * 2; k++) {                \
1212d45b0de6SPaul Brook             d->B(2 * j + k) = r[k];                           \
1213d45b0de6SPaul Brook         }                                                     \
1214d45b0de6SPaul Brook     }                                                         \
1215fcf5ef2aSThomas Huth }
1216fcf5ef2aSThomas Huth 
1217d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb)
1218d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub)
1219fcf5ef2aSThomas Huth 
1220f05f9789SPaolo Bonzini void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1221fcf5ef2aSThomas Huth {
1222d45b0de6SPaul Brook     uint16_t r[PACK_WIDTH];
1223d45b0de6SPaul Brook     int j, k;
1224fcf5ef2aSThomas Huth 
1225d45b0de6SPaul Brook     for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) {
1226d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1227d45b0de6SPaul Brook             r[k] = satsw(v->L(j + k));
1228d45b0de6SPaul Brook         }
1229d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH / 2; k++) {
1230d45b0de6SPaul Brook             r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k));
1231d45b0de6SPaul Brook         }
1232d45b0de6SPaul Brook         for (k = 0; k < PACK_WIDTH; k++) {
1233d45b0de6SPaul Brook             d->W(2 * j + k) = r[k];
1234d45b0de6SPaul Brook         }
1235d45b0de6SPaul Brook     }
1236fcf5ef2aSThomas Huth }
1237fcf5ef2aSThomas Huth 
1238fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1239fcf5ef2aSThomas Huth                                                                         \
1240fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1241f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1242fcf5ef2aSThomas Huth     {                                                                   \
1243d45b0de6SPaul Brook         uint8_t r[PACK_WIDTH * 2];                                      \
1244d45b0de6SPaul Brook         int j, i;                                                       \
1245fcf5ef2aSThomas Huth                                                                         \
1246d45b0de6SPaul Brook         for (j = 0; j < 8 << SHIFT; ) {                                 \
1247d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH;                              \
1248d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++) {                          \
1249d45b0de6SPaul Brook                 r[2 * i] = v->B(k + i);                                 \
1250d45b0de6SPaul Brook                 r[2 * i + 1] = s->B(k + i);                             \
1251d45b0de6SPaul Brook             }                                                           \
1252d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH * 2; i++, j++) {                 \
1253d45b0de6SPaul Brook                 d->B(j) = r[i];                                         \
1254d45b0de6SPaul Brook             }                                                           \
1255d45b0de6SPaul Brook         }                                                               \
1256fcf5ef2aSThomas Huth     }                                                                   \
1257fcf5ef2aSThomas Huth                                                                         \
1258fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1259f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1260fcf5ef2aSThomas Huth     {                                                                   \
1261d45b0de6SPaul Brook         uint16_t r[PACK_WIDTH];                                         \
1262d45b0de6SPaul Brook         int j, i;                                                       \
1263fcf5ef2aSThomas Huth                                                                         \
1264d45b0de6SPaul Brook         for (j = 0; j < 4 << SHIFT; ) {                                 \
1265d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 2;                          \
1266d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++) {                      \
1267d45b0de6SPaul Brook                 r[2 * i] = v->W(k + i);                                 \
1268d45b0de6SPaul Brook                 r[2 * i + 1] = s->W(k + i);                             \
1269d45b0de6SPaul Brook             }                                                           \
1270d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH; i++, j++) {                     \
1271d45b0de6SPaul Brook                 d->W(j) = r[i];                                         \
1272d45b0de6SPaul Brook             }                                                           \
1273d45b0de6SPaul Brook         }                                                               \
1274fcf5ef2aSThomas Huth     }                                                                   \
1275fcf5ef2aSThomas Huth                                                                         \
1276fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1277f05f9789SPaolo Bonzini                                                 Reg *d, Reg *v, Reg *s) \
1278fcf5ef2aSThomas Huth     {                                                                   \
1279d45b0de6SPaul Brook         uint32_t r[PACK_WIDTH / 2];                                     \
1280d45b0de6SPaul Brook         int j, i;                                                       \
1281fcf5ef2aSThomas Huth                                                                         \
1282d45b0de6SPaul Brook         for (j = 0; j < 2 << SHIFT; ) {                                 \
1283d45b0de6SPaul Brook             int k = j + base * PACK_WIDTH / 4;                          \
1284d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 4; i++) {                      \
1285d45b0de6SPaul Brook                 r[2 * i] = v->L(k + i);                                 \
1286d45b0de6SPaul Brook                 r[2 * i + 1] = s->L(k + i);                             \
1287d45b0de6SPaul Brook             }                                                           \
1288d45b0de6SPaul Brook             for (i = 0; i < PACK_WIDTH / 2; i++, j++) {                 \
1289d45b0de6SPaul Brook                 d->L(j) = r[i];                                         \
1290d45b0de6SPaul Brook             }                                                           \
1291d45b0de6SPaul Brook         }                                                               \
1292fcf5ef2aSThomas Huth     }                                                                   \
1293fcf5ef2aSThomas Huth                                                                         \
1294fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1295d45b0de6SPaul Brook              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(      \
1296f05f9789SPaolo Bonzini                         CPUX86State *env, Reg *d, Reg *v, Reg *s)       \
1297fcf5ef2aSThomas Huth              {                                                          \
1298d45b0de6SPaul Brook                  uint64_t r[2];                                         \
1299d45b0de6SPaul Brook                  int i;                                                 \
1300fcf5ef2aSThomas Huth                                                                         \
1301d45b0de6SPaul Brook                  for (i = 0; i < 1 << SHIFT; i += 2) {                  \
1302d45b0de6SPaul Brook                      r[0] = v->Q(base + i);                             \
1303d45b0de6SPaul Brook                      r[1] = s->Q(base + i);                             \
1304d45b0de6SPaul Brook                      d->Q(i) = r[0];                                    \
1305d45b0de6SPaul Brook                      d->Q(i + 1) = r[1];                                \
1306d45b0de6SPaul Brook                  }                                                      \
1307fcf5ef2aSThomas Huth              }                                                          \
1308fcf5ef2aSThomas Huth                                                                         )
1309fcf5ef2aSThomas Huth 
1310fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1311fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1312fcf5ef2aSThomas Huth 
1313d45b0de6SPaul Brook #undef PACK_WIDTH
1314d45b0de6SPaul Brook #undef PACK_HELPER_B
1315d45b0de6SPaul Brook #undef UNPCK_OP
1316d45b0de6SPaul Brook 
1317d45b0de6SPaul Brook 
1318fcf5ef2aSThomas Huth /* 3DNow! float ops */
1319fcf5ef2aSThomas Huth #if SHIFT == 0
1320fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1321fcf5ef2aSThomas Huth {
1322fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1323fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1324fcf5ef2aSThomas Huth }
1325fcf5ef2aSThomas Huth 
1326fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1327fcf5ef2aSThomas Huth {
1328fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1329fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1330fcf5ef2aSThomas Huth }
1331fcf5ef2aSThomas Huth 
1332fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1333fcf5ef2aSThomas Huth {
1334fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1335fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1336fcf5ef2aSThomas Huth }
1337fcf5ef2aSThomas Huth 
1338fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1339fcf5ef2aSThomas Huth {
1340fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1341fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1342fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1343fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1344fcf5ef2aSThomas Huth }
1345fcf5ef2aSThomas Huth 
1346fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1347fcf5ef2aSThomas Huth {
134825bdec79SPaolo Bonzini     float32 r;
1349fcf5ef2aSThomas Huth 
135025bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
135125bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
135225bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1353fcf5ef2aSThomas Huth }
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1356fcf5ef2aSThomas Huth {
1357fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1358fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1359fcf5ef2aSThomas Huth }
1360fcf5ef2aSThomas Huth 
1361fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1362fcf5ef2aSThomas Huth {
1363fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1364fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1365fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1366fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1367fcf5ef2aSThomas Huth }
1368fcf5ef2aSThomas Huth 
1369fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1370fcf5ef2aSThomas Huth {
1371fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1372fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1373fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1374fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1375fcf5ef2aSThomas Huth }
1376fcf5ef2aSThomas Huth 
1377fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1378fcf5ef2aSThomas Huth {
1379fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1380fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1381fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1382fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1383fcf5ef2aSThomas Huth }
1384fcf5ef2aSThomas Huth 
1385fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1386fcf5ef2aSThomas Huth {
1387fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1388fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1389fcf5ef2aSThomas Huth     }
1390fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1391fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1392fcf5ef2aSThomas Huth     }
1393fcf5ef2aSThomas Huth }
1394fcf5ef2aSThomas Huth 
1395fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1396fcf5ef2aSThomas Huth {
1397fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1398fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1399fcf5ef2aSThomas Huth     }
1400fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1401fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1402fcf5ef2aSThomas Huth     }
1403fcf5ef2aSThomas Huth }
1404fcf5ef2aSThomas Huth 
1405fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1406fcf5ef2aSThomas Huth {
1407fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1408fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1412fcf5ef2aSThomas Huth {
141325bdec79SPaolo Bonzini     float32 r;
1414fcf5ef2aSThomas Huth 
141525bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
141625bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
141725bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1418fcf5ef2aSThomas Huth }
1419fcf5ef2aSThomas Huth 
1420fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1421fcf5ef2aSThomas Huth {
142225bdec79SPaolo Bonzini     float32 r;
1423fcf5ef2aSThomas Huth 
142425bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
142525bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
142625bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1427fcf5ef2aSThomas Huth }
1428fcf5ef2aSThomas Huth 
1429fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1430fcf5ef2aSThomas Huth {
1431fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1432fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1433fcf5ef2aSThomas Huth }
1434fcf5ef2aSThomas Huth 
1435fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1436fcf5ef2aSThomas Huth {
1437fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1438fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1439fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1440fcf5ef2aSThomas Huth                               &env->mmx_status);
1441fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1442fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1443fcf5ef2aSThomas Huth }
1444fcf5ef2aSThomas Huth 
1445fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1446fcf5ef2aSThomas Huth {
1447fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1448fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1449fcf5ef2aSThomas Huth }
1450fcf5ef2aSThomas Huth 
1451fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1452fcf5ef2aSThomas Huth {
1453fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1454fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1455fcf5ef2aSThomas Huth }
1456fcf5ef2aSThomas Huth 
1457fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1458fcf5ef2aSThomas Huth {
145925bdec79SPaolo Bonzini     uint32_t r;
1460fcf5ef2aSThomas Huth 
146125bdec79SPaolo Bonzini     r = s->MMX_L(0);
146225bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
146325bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1464fcf5ef2aSThomas Huth }
1465fcf5ef2aSThomas Huth #endif
1466fcf5ef2aSThomas Huth 
1467fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1468f05f9789SPaolo Bonzini void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1469fcf5ef2aSThomas Huth {
1470fcf5ef2aSThomas Huth     int i;
1471d45b0de6SPaul Brook #if SHIFT == 0
1472d45b0de6SPaul Brook     uint8_t r[8];
1473fcf5ef2aSThomas Huth 
1474d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1475d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7));
1476fcf5ef2aSThomas Huth     }
1477d45b0de6SPaul Brook     for (i = 0; i < 8; i++) {
1478d45b0de6SPaul Brook         d->B(i) = r[i];
1479fcf5ef2aSThomas Huth     }
1480d45b0de6SPaul Brook #else
1481d45b0de6SPaul Brook     uint8_t r[8 << SHIFT];
1482fcf5ef2aSThomas Huth 
1483d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1484d45b0de6SPaul Brook         int j = i & ~0xf;
1485d45b0de6SPaul Brook         r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf));
1486fcf5ef2aSThomas Huth     }
1487d45b0de6SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
1488d45b0de6SPaul Brook         d->B(i) = r[i];
1489fcf5ef2aSThomas Huth     }
1490fcf5ef2aSThomas Huth #endif
1491fcf5ef2aSThomas Huth }
1492fcf5ef2aSThomas Huth 
1493d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F)  \
1494f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1495d45b0de6SPaul Brook {                                                          \
1496d45b0de6SPaul Brook     uint16_t r[4 << SHIFT];                                \
1497d45b0de6SPaul Brook     int i, j, k;                                           \
1498d45b0de6SPaul Brook     for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) {     \
1499d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \
1500d45b0de6SPaul Brook             r[i + k] = F(v->W(j + k), v->W(j + k + 1));    \
1501d45b0de6SPaul Brook         }                                                  \
1502d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) {     \
1503d45b0de6SPaul Brook             r[i + k] = F(s->W(j + k), s->W(j + k + 1));    \
1504d45b0de6SPaul Brook         }                                                  \
1505d45b0de6SPaul Brook     }                                                      \
1506d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {                     \
1507d45b0de6SPaul Brook         d->W(i) = r[i];                                    \
1508d45b0de6SPaul Brook     }                                                      \
1509fcf5ef2aSThomas Huth }
1510fcf5ef2aSThomas Huth 
1511d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F)  \
1512f05f9789SPaolo Bonzini void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s) \
1513d45b0de6SPaul Brook {                                                          \
1514d45b0de6SPaul Brook     uint32_t r[2 << SHIFT];                                \
1515d45b0de6SPaul Brook     int i, j, k;                                           \
1516d45b0de6SPaul Brook     for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) {     \
1517d45b0de6SPaul Brook         for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \
1518d45b0de6SPaul Brook             r[i + k] = F(v->L(j + k), v->L(j + k + 1));    \
1519d45b0de6SPaul Brook         }                                                  \
1520d45b0de6SPaul Brook         for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) {     \
1521d45b0de6SPaul Brook             r[i + k] = F(s->L(j + k), s->L(j + k + 1));    \
1522d45b0de6SPaul Brook         }                                                  \
1523d45b0de6SPaul Brook     }                                                      \
1524d45b0de6SPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {                     \
1525d45b0de6SPaul Brook         d->L(i) = r[i];                                    \
1526d45b0de6SPaul Brook     }                                                      \
1527fcf5ef2aSThomas Huth }
1528fcf5ef2aSThomas Huth 
1529d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD)
1530d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB)
1531d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW)
1532d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW)
1533d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD)
1534d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB)
153575046ad7SPaolo Bonzini 
1536d45b0de6SPaul Brook #undef SSE_HELPER_HW
1537d45b0de6SPaul Brook #undef SSE_HELPER_HL
1538d45b0de6SPaul Brook 
1539f05f9789SPaolo Bonzini void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1540d45b0de6SPaul Brook {
1541d45b0de6SPaul Brook     int i;
1542d45b0de6SPaul Brook     for (i = 0; i < 4 << SHIFT; i++) {
1543d45b0de6SPaul Brook         d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) +
1544d45b0de6SPaul Brook                         (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1));
1545d45b0de6SPaul Brook     }
1546fcf5ef2aSThomas Huth }
1547fcf5ef2aSThomas Huth 
1548ee04a3c8SPaul Brook #define FABSB(x) (x > INT8_MAX  ? -(int8_t)x : x)
1549ee04a3c8SPaul Brook #define FABSW(x) (x > INT16_MAX ? -(int16_t)x : x)
1550ee04a3c8SPaul Brook #define FABSL(x) (x > INT32_MAX ? -(int32_t)x : x)
1551ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsb, B, 8 << SHIFT, FABSB)
1552ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsw, W, 4 << SHIFT, FABSW)
1553ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsd, L, 2 << SHIFT, FABSL)
1554fcf5ef2aSThomas Huth 
1555fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1556fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1557fcf5ef2aSThomas Huth 
1558fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1559fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1560fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1561fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1562fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1563fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1564fcf5ef2aSThomas Huth 
1565f05f9789SPaolo Bonzini void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1566fcf5ef2aSThomas Huth                                   int32_t shift)
1567fcf5ef2aSThomas Huth {
1568d45b0de6SPaul Brook     int i;
1569fcf5ef2aSThomas Huth 
1570fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1571d45b0de6SPaul Brook     if (shift >= (SHIFT ? 32 : 16)) {
1572d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i++) {
1573d45b0de6SPaul Brook             d->Q(i) = 0;
1574d45b0de6SPaul Brook         }
1575fcf5ef2aSThomas Huth     } else {
1576fcf5ef2aSThomas Huth         shift <<= 3;
1577fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1578fcf5ef2aSThomas Huth #if SHIFT == 0
1579d45b0de6SPaul Brook         d->Q(0) = SHR(s->Q(0), shift - 0) |
1580d45b0de6SPaul Brook             SHR(v->Q(0), shift -  64);
1581fcf5ef2aSThomas Huth #else
1582d45b0de6SPaul Brook         for (i = 0; i < (1 << SHIFT); i += 2) {
1583d45b0de6SPaul Brook             uint64_t r0, r1;
1584d45b0de6SPaul Brook 
1585d45b0de6SPaul Brook             r0 = SHR(s->Q(i), shift - 0) |
1586d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -  64) |
1587d45b0de6SPaul Brook                  SHR(v->Q(i), shift - 128) |
1588d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 192);
1589d45b0de6SPaul Brook             r1 = SHR(s->Q(i), shift + 64) |
1590d45b0de6SPaul Brook                  SHR(s->Q(i + 1), shift -   0) |
1591d45b0de6SPaul Brook                  SHR(v->Q(i), shift -  64) |
1592d45b0de6SPaul Brook                  SHR(v->Q(i + 1), shift - 128);
1593d45b0de6SPaul Brook             d->Q(i) = r0;
1594d45b0de6SPaul Brook             d->Q(i + 1) = r1;
1595d45b0de6SPaul Brook         }
1596fcf5ef2aSThomas Huth #endif
1597fcf5ef2aSThomas Huth #undef SHR
1598fcf5ef2aSThomas Huth     }
1599fcf5ef2aSThomas Huth }
1600fcf5ef2aSThomas Huth 
16010e29cea5SPaul Brook #if SHIFT >= 1
1602fcf5ef2aSThomas Huth 
1603fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1604f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
1605f05f9789SPaolo Bonzini                             Reg *m)                                     \
1606fcf5ef2aSThomas Huth     {                                                                   \
16070e29cea5SPaul Brook         int i;                                                          \
16080e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
16090e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), m->elem(i));         \
1610fcf5ef2aSThomas Huth         }                                                               \
1611fcf5ef2aSThomas Huth     }
1612fcf5ef2aSThomas Huth 
1613fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1614f05f9789SPaolo Bonzini     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,   \
16150e29cea5SPaul Brook                             uint32_t imm)                               \
1616fcf5ef2aSThomas Huth     {                                                                   \
16170e29cea5SPaul Brook         int i;                                                          \
16180e29cea5SPaul Brook         for (i = 0; i < num; i++) {                                     \
16190e29cea5SPaul Brook             int j = i & 7;                                              \
16200e29cea5SPaul Brook             d->elem(i) = F(v->elem(i), s->elem(i), (imm >> j) & 1);     \
1621fcf5ef2aSThomas Huth         }                                                               \
1622fcf5ef2aSThomas Huth     }
1623fcf5ef2aSThomas Huth 
1624fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
16250e29cea5SPaul Brook #define FBLENDVB(v, s, m) ((m & 0x80) ? s : v)
16260e29cea5SPaul Brook #define FBLENDVPS(v, s, m) ((m & 0x80000000) ? s : v)
16270e29cea5SPaul Brook #define FBLENDVPD(v, s, m) ((m & 0x8000000000000000LL) ? s : v)
16280e29cea5SPaul Brook SSE_HELPER_V(helper_pblendvb, B, 8 << SHIFT, FBLENDVB)
16290e29cea5SPaul Brook SSE_HELPER_V(helper_blendvps, L, 2 << SHIFT, FBLENDVPS)
16300e29cea5SPaul Brook SSE_HELPER_V(helper_blendvpd, Q, 1 << SHIFT, FBLENDVPD)
1631fcf5ef2aSThomas Huth 
1632fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1633fcf5ef2aSThomas Huth {
1634e894bae8SPaul Brook     uint64_t zf = 0, cf = 0;
1635e894bae8SPaul Brook     int i;
1636fcf5ef2aSThomas Huth 
1637e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1638e894bae8SPaul Brook         zf |= (s->Q(i) &  d->Q(i));
1639e894bae8SPaul Brook         cf |= (s->Q(i) & ~d->Q(i));
1640e894bae8SPaul Brook     }
1641fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1642fcf5ef2aSThomas Huth }
1643fcf5ef2aSThomas Huth 
1644fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)                        \
1645fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
1646fcf5ef2aSThomas Huth     {                                                           \
1647e894bae8SPaul Brook         int n = num;                                            \
1648e894bae8SPaul Brook         for (int i = n; --i >= 0; ) {                           \
1649e894bae8SPaul Brook             d->elem(i) = F(i);                                  \
1650fcf5ef2aSThomas Huth         }                                                       \
1651fcf5ef2aSThomas Huth     }
1652fcf5ef2aSThomas Huth 
1653e894bae8SPaul Brook #if SHIFT > 0
1654e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B)
1655e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B)
1656e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B)
1657e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W)
1658e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W)
1659e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L)
1660e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B)
1661e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B)
1662e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B)
1663e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W)
1664e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W)
1665e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L)
1666e894bae8SPaul Brook #endif
1667fcf5ef2aSThomas Huth 
1668f05f9789SPaolo Bonzini void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1669fcf5ef2aSThomas Huth {
1670e894bae8SPaul Brook     int i;
1671e894bae8SPaul Brook 
1672e894bae8SPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1673e894bae8SPaul Brook         d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i);
1674e894bae8SPaul Brook     }
1675fcf5ef2aSThomas Huth }
1676fcf5ef2aSThomas Huth 
1677fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1678fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1679fcf5ef2aSThomas Huth 
1680f05f9789SPaolo Bonzini void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
1681fcf5ef2aSThomas Huth {
1682d45b0de6SPaul Brook     uint16_t r[8];
1683d45b0de6SPaul Brook     int i, j, k;
168480e19606SJoseph Myers 
1685d45b0de6SPaul Brook     for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) {
1686d45b0de6SPaul Brook         r[0] = satuw(v->L(j));
1687d45b0de6SPaul Brook         r[1] = satuw(v->L(j + 1));
1688d45b0de6SPaul Brook         r[2] = satuw(v->L(j + 2));
1689d45b0de6SPaul Brook         r[3] = satuw(v->L(j + 3));
1690d45b0de6SPaul Brook         r[4] = satuw(s->L(j));
1691d45b0de6SPaul Brook         r[5] = satuw(s->L(j + 1));
1692d45b0de6SPaul Brook         r[6] = satuw(s->L(j + 2));
1693d45b0de6SPaul Brook         r[7] = satuw(s->L(j + 3));
1694d45b0de6SPaul Brook         for (k = 0; k < 8; k++) {
1695d45b0de6SPaul Brook             d->W(i + k) = r[k];
1696d45b0de6SPaul Brook         }
1697d45b0de6SPaul Brook     }
1698fcf5ef2aSThomas Huth }
1699fcf5ef2aSThomas Huth 
1700fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1701fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1702fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1703fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1704fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1705fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1706fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1707fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1708fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1709fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1710fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1711fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1712fcf5ef2aSThomas Huth 
1713fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1714fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1715fcf5ef2aSThomas Huth 
1716fd17264aSPaul Brook #if SHIFT == 1
1717fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1718fcf5ef2aSThomas Huth {
1719fcf5ef2aSThomas Huth     int idx = 0;
1720fcf5ef2aSThomas Huth 
1721fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1722fcf5ef2aSThomas Huth         idx = 1;
1723fcf5ef2aSThomas Huth     }
1724fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1725fcf5ef2aSThomas Huth         idx = 2;
1726fcf5ef2aSThomas Huth     }
1727fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1728fcf5ef2aSThomas Huth         idx = 3;
1729fcf5ef2aSThomas Huth     }
1730fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1731fcf5ef2aSThomas Huth         idx = 4;
1732fcf5ef2aSThomas Huth     }
1733fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1734fcf5ef2aSThomas Huth         idx = 5;
1735fcf5ef2aSThomas Huth     }
1736fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1737fcf5ef2aSThomas Huth         idx = 6;
1738fcf5ef2aSThomas Huth     }
1739fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1740fcf5ef2aSThomas Huth         idx = 7;
1741fcf5ef2aSThomas Huth     }
1742fcf5ef2aSThomas Huth 
1743fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1744aa406feaSJoseph Myers     d->W(1) = idx;
1745aa406feaSJoseph Myers     d->L(1) = 0;
1746aa406feaSJoseph Myers     d->Q(1) = 0;
1747fcf5ef2aSThomas Huth }
1748fd17264aSPaul Brook #endif
1749fcf5ef2aSThomas Huth 
1750fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1751fcf5ef2aSThomas Huth                                   uint32_t mode)
1752fcf5ef2aSThomas Huth {
1753418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1754fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1755fd17264aSPaul Brook     int i;
1756fcf5ef2aSThomas Huth 
1757fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1758fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1759fcf5ef2aSThomas Huth         switch (mode & 3) {
1760fcf5ef2aSThomas Huth         case 0:
1761fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1762fcf5ef2aSThomas Huth             break;
1763fcf5ef2aSThomas Huth         case 1:
1764fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1765fcf5ef2aSThomas Huth             break;
1766fcf5ef2aSThomas Huth         case 2:
1767fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1768fcf5ef2aSThomas Huth             break;
1769fcf5ef2aSThomas Huth         case 3:
1770fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1771fcf5ef2aSThomas Huth             break;
1772fcf5ef2aSThomas Huth         }
1773fcf5ef2aSThomas Huth     }
1774fcf5ef2aSThomas Huth 
1775fd17264aSPaul Brook     for (i = 0; i < 2 << SHIFT; i++) {
1776fd17264aSPaul Brook         d->ZMM_S(i) = float32_round_to_int(s->ZMM_S(i), &env->sse_status);
1777fd17264aSPaul Brook     }
1778fcf5ef2aSThomas Huth 
1779418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1780fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1781fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1782fcf5ef2aSThomas Huth                                   &env->sse_status);
1783fcf5ef2aSThomas Huth     }
1784fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1785fcf5ef2aSThomas Huth }
1786fcf5ef2aSThomas Huth 
1787fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1788fcf5ef2aSThomas Huth                                   uint32_t mode)
1789fcf5ef2aSThomas Huth {
1790418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1791fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1792fd17264aSPaul Brook     int i;
1793fcf5ef2aSThomas Huth 
1794fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1795fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1796fcf5ef2aSThomas Huth         switch (mode & 3) {
1797fcf5ef2aSThomas Huth         case 0:
1798fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1799fcf5ef2aSThomas Huth             break;
1800fcf5ef2aSThomas Huth         case 1:
1801fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1802fcf5ef2aSThomas Huth             break;
1803fcf5ef2aSThomas Huth         case 2:
1804fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1805fcf5ef2aSThomas Huth             break;
1806fcf5ef2aSThomas Huth         case 3:
1807fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1808fcf5ef2aSThomas Huth             break;
1809fcf5ef2aSThomas Huth         }
1810fcf5ef2aSThomas Huth     }
1811fcf5ef2aSThomas Huth 
1812fd17264aSPaul Brook     for (i = 0; i < 1 << SHIFT; i++) {
1813fd17264aSPaul Brook         d->ZMM_D(i) = float64_round_to_int(s->ZMM_D(i), &env->sse_status);
1814fd17264aSPaul Brook     }
1815fcf5ef2aSThomas Huth 
1816418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1817fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1818fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1819fcf5ef2aSThomas Huth                                   &env->sse_status);
1820fcf5ef2aSThomas Huth     }
1821fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1822fcf5ef2aSThomas Huth }
1823fcf5ef2aSThomas Huth 
1824fd17264aSPaul Brook #if SHIFT == 1
1825*620f7556SPaolo Bonzini void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1826fcf5ef2aSThomas Huth                                   uint32_t mode)
1827fcf5ef2aSThomas Huth {
1828418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1829fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1830*620f7556SPaolo Bonzini     int i;
1831fcf5ef2aSThomas Huth 
1832fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1833fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1834fcf5ef2aSThomas Huth         switch (mode & 3) {
1835fcf5ef2aSThomas Huth         case 0:
1836fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1837fcf5ef2aSThomas Huth             break;
1838fcf5ef2aSThomas Huth         case 1:
1839fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1840fcf5ef2aSThomas Huth             break;
1841fcf5ef2aSThomas Huth         case 2:
1842fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1843fcf5ef2aSThomas Huth             break;
1844fcf5ef2aSThomas Huth         case 3:
1845fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1846fcf5ef2aSThomas Huth             break;
1847fcf5ef2aSThomas Huth         }
1848fcf5ef2aSThomas Huth     }
1849fcf5ef2aSThomas Huth 
1850fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1851*620f7556SPaolo Bonzini     for (i = 1; i < 2 << SHIFT; i++) {
1852*620f7556SPaolo Bonzini         d->ZMM_L(i) = v->ZMM_L(i);
1853*620f7556SPaolo Bonzini     }
1854fcf5ef2aSThomas Huth 
1855418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1856fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1857fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1858fcf5ef2aSThomas Huth                                   &env->sse_status);
1859fcf5ef2aSThomas Huth     }
1860fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1861fcf5ef2aSThomas Huth }
1862fcf5ef2aSThomas Huth 
1863*620f7556SPaolo Bonzini void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1864fcf5ef2aSThomas Huth                                   uint32_t mode)
1865fcf5ef2aSThomas Huth {
1866418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1867fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1868*620f7556SPaolo Bonzini     int i;
1869fcf5ef2aSThomas Huth 
1870fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1871fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1872fcf5ef2aSThomas Huth         switch (mode & 3) {
1873fcf5ef2aSThomas Huth         case 0:
1874fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1875fcf5ef2aSThomas Huth             break;
1876fcf5ef2aSThomas Huth         case 1:
1877fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1878fcf5ef2aSThomas Huth             break;
1879fcf5ef2aSThomas Huth         case 2:
1880fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1881fcf5ef2aSThomas Huth             break;
1882fcf5ef2aSThomas Huth         case 3:
1883fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1884fcf5ef2aSThomas Huth             break;
1885fcf5ef2aSThomas Huth         }
1886fcf5ef2aSThomas Huth     }
1887fcf5ef2aSThomas Huth 
1888fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1889*620f7556SPaolo Bonzini     for (i = 1; i < 1 << SHIFT; i++) {
1890*620f7556SPaolo Bonzini         d->ZMM_Q(i) = v->ZMM_Q(i);
1891*620f7556SPaolo Bonzini     }
1892fcf5ef2aSThomas Huth 
1893418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1894fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1895fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1896fcf5ef2aSThomas Huth                                   &env->sse_status);
1897fcf5ef2aSThomas Huth     }
1898fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1899fcf5ef2aSThomas Huth }
1900fd17264aSPaul Brook #endif
1901fcf5ef2aSThomas Huth 
19020e29cea5SPaul Brook #define FBLENDP(v, s, m) (m ? s : v)
19030e29cea5SPaul Brook SSE_HELPER_I(helper_blendps, L, 2 << SHIFT, FBLENDP)
19040e29cea5SPaul Brook SSE_HELPER_I(helper_blendpd, Q, 1 << SHIFT, FBLENDP)
19050e29cea5SPaul Brook SSE_HELPER_I(helper_pblendw, W, 4 << SHIFT, FBLENDP)
1906fcf5ef2aSThomas Huth 
1907f05f9789SPaolo Bonzini void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
19086f218d6eSPaul Brook                                uint32_t mask)
1909fcf5ef2aSThomas Huth {
1910bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
19116f218d6eSPaul Brook     int i;
1912fcf5ef2aSThomas Huth 
19136f218d6eSPaul Brook     for (i = 0; i < 2 << SHIFT; i += 4) {
1914bf30ad8cSPaolo Bonzini         /*
1915bf30ad8cSPaolo Bonzini          * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1916bf30ad8cSPaolo Bonzini          * to correctly round the intermediate results
1917bf30ad8cSPaolo Bonzini          */
1918fcf5ef2aSThomas Huth         if (mask & (1 << 4)) {
19196f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status);
1920bf30ad8cSPaolo Bonzini         } else {
1921bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1922fcf5ef2aSThomas Huth         }
1923fcf5ef2aSThomas Huth         if (mask & (1 << 5)) {
19246f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status);
1925bf30ad8cSPaolo Bonzini         } else {
1926bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1927fcf5ef2aSThomas Huth         }
1928bf30ad8cSPaolo Bonzini         temp2 = float32_add(prod1, prod2, &env->sse_status);
1929fcf5ef2aSThomas Huth         if (mask & (1 << 6)) {
19306f218d6eSPaul Brook             prod1 = float32_mul(v->ZMM_S(i+2), s->ZMM_S(i+2), &env->sse_status);
1931bf30ad8cSPaolo Bonzini         } else {
1932bf30ad8cSPaolo Bonzini             prod1 = float32_zero;
1933fcf5ef2aSThomas Huth         }
1934fcf5ef2aSThomas Huth         if (mask & (1 << 7)) {
19356f218d6eSPaul Brook             prod2 = float32_mul(v->ZMM_S(i+3), s->ZMM_S(i+3), &env->sse_status);
1936bf30ad8cSPaolo Bonzini         } else {
1937bf30ad8cSPaolo Bonzini             prod2 = float32_zero;
1938fcf5ef2aSThomas Huth         }
1939bf30ad8cSPaolo Bonzini         temp3 = float32_add(prod1, prod2, &env->sse_status);
1940bf30ad8cSPaolo Bonzini         temp4 = float32_add(temp2, temp3, &env->sse_status);
1941bf30ad8cSPaolo Bonzini 
19426f218d6eSPaul Brook         d->ZMM_S(i) = (mask & (1 << 0)) ? temp4 : float32_zero;
19436f218d6eSPaul Brook         d->ZMM_S(i+1) = (mask & (1 << 1)) ? temp4 : float32_zero;
19446f218d6eSPaul Brook         d->ZMM_S(i+2) = (mask & (1 << 2)) ? temp4 : float32_zero;
19456f218d6eSPaul Brook         d->ZMM_S(i+3) = (mask & (1 << 3)) ? temp4 : float32_zero;
19466f218d6eSPaul Brook     }
1947fcf5ef2aSThomas Huth }
1948fcf5ef2aSThomas Huth 
19496f218d6eSPaul Brook #if SHIFT == 1
19506f218d6eSPaul Brook /* Oddly, there is no ymm version of dppd */
19516f218d6eSPaul Brook void glue(helper_dppd, SUFFIX)(CPUX86State *env,
1952f05f9789SPaolo Bonzini                                Reg *d, Reg *v, Reg *s, uint32_t mask)
1953fcf5ef2aSThomas Huth {
1954bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1955fcf5ef2aSThomas Huth 
1956fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
19576f218d6eSPaul Brook         prod1 = float64_mul(v->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1958bf30ad8cSPaolo Bonzini     } else {
1959bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1960fcf5ef2aSThomas Huth     }
1961fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
19626f218d6eSPaul Brook         prod2 = float64_mul(v->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1963bf30ad8cSPaolo Bonzini     } else {
1964bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1965fcf5ef2aSThomas Huth     }
1966bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1967bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1968bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1969fcf5ef2aSThomas Huth }
19706f218d6eSPaul Brook #endif
1971fcf5ef2aSThomas Huth 
1972f05f9789SPaolo Bonzini void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
1973fcf5ef2aSThomas Huth                                   uint32_t offset)
1974fcf5ef2aSThomas Huth {
1975d45b0de6SPaul Brook     int i, j;
1976d45b0de6SPaul Brook     uint16_t r[8];
1977fcf5ef2aSThomas Huth 
1978d45b0de6SPaul Brook     for (j = 0; j < 4 << SHIFT; ) {
1979d45b0de6SPaul Brook         int s0 = (j * 2) + ((offset & 3) << 2);
1980d45b0de6SPaul Brook         int d0 = (j * 2) + ((offset & 4) << 0);
1981d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, d0++) {
1982d45b0de6SPaul Brook             r[i] = 0;
1983d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0));
1984d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1));
1985d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2));
1986d45b0de6SPaul Brook             r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3));
1987fcf5ef2aSThomas Huth         }
1988d45b0de6SPaul Brook         for (i = 0; i < LANE_WIDTH / 2; i++, j++) {
1989d45b0de6SPaul Brook             d->W(j) = r[i];
1990d45b0de6SPaul Brook         }
1991d45b0de6SPaul Brook         offset >>= 3;
1992d45b0de6SPaul Brook     }
1993fcf5ef2aSThomas Huth }
1994fcf5ef2aSThomas Huth 
1995fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1996fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
1997fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1998fcf5ef2aSThomas Huth 
1999fd17264aSPaul Brook #if SHIFT == 1
2000fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
2001fcf5ef2aSThomas Huth {
2002d1da229fSPaul Brook     target_long val, limit;
2003fcf5ef2aSThomas Huth 
2004fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
2005fcf5ef2aSThomas Huth     if (ctrl >> 8) {
2006d1da229fSPaul Brook         val = (target_long)env->regs[reg];
2007fcf5ef2aSThomas Huth     } else {
2008d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
2009fcf5ef2aSThomas Huth     }
2010fcf5ef2aSThomas Huth     if (ctrl & 1) {
2011d1da229fSPaul Brook         limit = 8;
2012fcf5ef2aSThomas Huth     } else {
2013d1da229fSPaul Brook         limit = 16;
2014fcf5ef2aSThomas Huth     }
2015d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
2016d1da229fSPaul Brook         return limit;
2017fcf5ef2aSThomas Huth     }
2018d1da229fSPaul Brook     return abs1(val);
2019fcf5ef2aSThomas Huth }
2020fcf5ef2aSThomas Huth 
2021fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
2022fcf5ef2aSThomas Huth {
2023fcf5ef2aSThomas Huth     int val = 0;
2024fcf5ef2aSThomas Huth 
2025fcf5ef2aSThomas Huth     if (ctrl & 1) {
2026fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
2027fcf5ef2aSThomas Huth             val++;
2028fcf5ef2aSThomas Huth         }
2029fcf5ef2aSThomas Huth     } else {
2030fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
2031fcf5ef2aSThomas Huth             val++;
2032fcf5ef2aSThomas Huth         }
2033fcf5ef2aSThomas Huth     }
2034fcf5ef2aSThomas Huth 
2035fcf5ef2aSThomas Huth     return val;
2036fcf5ef2aSThomas Huth }
2037fcf5ef2aSThomas Huth 
2038fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
2039fcf5ef2aSThomas Huth {
2040fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
2041fcf5ef2aSThomas Huth     case 0:
2042fcf5ef2aSThomas Huth         return r->B(i);
2043fcf5ef2aSThomas Huth     case 1:
2044fcf5ef2aSThomas Huth         return r->W(i);
2045fcf5ef2aSThomas Huth     case 2:
2046fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
2047fcf5ef2aSThomas Huth     case 3:
2048fcf5ef2aSThomas Huth     default:
2049fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
2050fcf5ef2aSThomas Huth     }
2051fcf5ef2aSThomas Huth }
2052fcf5ef2aSThomas Huth 
2053fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
2054fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
2055fcf5ef2aSThomas Huth {
2056fcf5ef2aSThomas Huth     unsigned int res = 0;
2057fcf5ef2aSThomas Huth     int v;
2058fcf5ef2aSThomas Huth     int j, i;
2059fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
2060fcf5ef2aSThomas Huth 
2061fcf5ef2aSThomas Huth     valids--;
2062fcf5ef2aSThomas Huth     validd--;
2063fcf5ef2aSThomas Huth 
2064fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2065fcf5ef2aSThomas Huth 
2066fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2067fcf5ef2aSThomas Huth     case 0:
2068fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2069fcf5ef2aSThomas Huth             res <<= 1;
2070fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2071fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2072fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2073fcf5ef2aSThomas Huth             }
2074fcf5ef2aSThomas Huth         }
2075fcf5ef2aSThomas Huth         break;
2076fcf5ef2aSThomas Huth     case 1:
2077fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2078fcf5ef2aSThomas Huth             res <<= 1;
2079fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2080fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2081fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2082fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2083fcf5ef2aSThomas Huth             }
2084fcf5ef2aSThomas Huth         }
2085fcf5ef2aSThomas Huth         break;
2086fcf5ef2aSThomas Huth     case 2:
2087fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2088fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2089fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2090fcf5ef2aSThomas Huth             res <<= 1;
2091fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2092fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2093fcf5ef2aSThomas Huth         }
2094fcf5ef2aSThomas Huth         break;
2095fcf5ef2aSThomas Huth     case 3:
2096ae35eea7SJoseph Myers         if (validd == -1) {
2097ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2098ae35eea7SJoseph Myers             break;
2099ae35eea7SJoseph Myers         }
2100bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2101fcf5ef2aSThomas Huth             res <<= 1;
2102fcf5ef2aSThomas Huth             v = 1;
2103bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2104fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2105fcf5ef2aSThomas Huth             }
2106fcf5ef2aSThomas Huth             res |= v;
2107fcf5ef2aSThomas Huth         }
2108fcf5ef2aSThomas Huth         break;
2109fcf5ef2aSThomas Huth     }
2110fcf5ef2aSThomas Huth 
2111fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2112fcf5ef2aSThomas Huth     case 1:
2113fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2114fcf5ef2aSThomas Huth         break;
2115fcf5ef2aSThomas Huth     case 3:
2116fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2117fcf5ef2aSThomas Huth         break;
2118fcf5ef2aSThomas Huth     }
2119fcf5ef2aSThomas Huth 
2120fcf5ef2aSThomas Huth     if (res) {
2121fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2122fcf5ef2aSThomas Huth     }
2123fcf5ef2aSThomas Huth     if (res & 1) {
2124fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2125fcf5ef2aSThomas Huth     }
2126fcf5ef2aSThomas Huth 
2127fcf5ef2aSThomas Huth     return res;
2128fcf5ef2aSThomas Huth }
2129fcf5ef2aSThomas Huth 
2130fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2131fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2132fcf5ef2aSThomas Huth {
2133fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2134fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2135fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2136fcf5ef2aSThomas Huth 
2137fcf5ef2aSThomas Huth     if (res) {
2138fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2139fcf5ef2aSThomas Huth     } else {
2140fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2141fcf5ef2aSThomas Huth     }
2142fcf5ef2aSThomas Huth }
2143fcf5ef2aSThomas Huth 
2144fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2145fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2146fcf5ef2aSThomas Huth {
2147fcf5ef2aSThomas Huth     int i;
2148fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2149fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2150fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2151fcf5ef2aSThomas Huth 
2152fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2153fcf5ef2aSThomas Huth         if (ctrl & 1) {
2154fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2155fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2156fcf5ef2aSThomas Huth             }
2157fcf5ef2aSThomas Huth         } else {
2158fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2159fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2160fcf5ef2aSThomas Huth             }
2161fcf5ef2aSThomas Huth         }
2162fcf5ef2aSThomas Huth     } else {
2163fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2164fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2165fcf5ef2aSThomas Huth     }
2166fcf5ef2aSThomas Huth }
2167fcf5ef2aSThomas Huth 
2168fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2169fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2170fcf5ef2aSThomas Huth {
2171fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2172fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2173fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2174fcf5ef2aSThomas Huth 
2175fcf5ef2aSThomas Huth     if (res) {
2176fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2177fcf5ef2aSThomas Huth     } else {
2178fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2179fcf5ef2aSThomas Huth     }
2180fcf5ef2aSThomas Huth }
2181fcf5ef2aSThomas Huth 
2182fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2183fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2184fcf5ef2aSThomas Huth {
2185fcf5ef2aSThomas Huth     int i;
2186fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2187fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2188fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2189fcf5ef2aSThomas Huth 
2190fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2191fcf5ef2aSThomas Huth         if (ctrl & 1) {
2192fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2193fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2194fcf5ef2aSThomas Huth             }
2195fcf5ef2aSThomas Huth         } else {
2196fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2197fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2198fcf5ef2aSThomas Huth             }
2199fcf5ef2aSThomas Huth         }
2200fcf5ef2aSThomas Huth     } else {
2201fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2202fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2203fcf5ef2aSThomas Huth     }
2204fcf5ef2aSThomas Huth }
2205fcf5ef2aSThomas Huth 
2206fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2207fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2208fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2209fcf5ef2aSThomas Huth {
2210fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2211fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2212fcf5ef2aSThomas Huth 
2213fcf5ef2aSThomas Huth     while (len--) {
2214fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2215fcf5ef2aSThomas Huth     }
2216fcf5ef2aSThomas Huth 
2217fcf5ef2aSThomas Huth     return crc;
2218fcf5ef2aSThomas Huth }
2219fcf5ef2aSThomas Huth 
2220fd17264aSPaul Brook #endif
2221fd17264aSPaul Brook 
22225a09df21SPaul Brook #if SHIFT == 1
22235a09df21SPaul Brook static void clmulq(uint64_t *dest_l, uint64_t *dest_h,
22245a09df21SPaul Brook                           uint64_t a, uint64_t b)
2225fcf5ef2aSThomas Huth {
22265a09df21SPaul Brook     uint64_t al, ah, resh, resl;
2227fcf5ef2aSThomas Huth 
2228fcf5ef2aSThomas Huth     ah = 0;
22295a09df21SPaul Brook     al = a;
2230fcf5ef2aSThomas Huth     resh = resl = 0;
2231fcf5ef2aSThomas Huth 
2232fcf5ef2aSThomas Huth     while (b) {
2233fcf5ef2aSThomas Huth         if (b & 1) {
2234fcf5ef2aSThomas Huth             resl ^= al;
2235fcf5ef2aSThomas Huth             resh ^= ah;
2236fcf5ef2aSThomas Huth         }
2237fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2238fcf5ef2aSThomas Huth         al <<= 1;
2239fcf5ef2aSThomas Huth         b >>= 1;
2240fcf5ef2aSThomas Huth     }
2241fcf5ef2aSThomas Huth 
22425a09df21SPaul Brook     *dest_l = resl;
22435a09df21SPaul Brook     *dest_h = resh;
22445a09df21SPaul Brook }
22455a09df21SPaul Brook #endif
22465a09df21SPaul Brook 
2247f05f9789SPaolo Bonzini void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,
22485a09df21SPaul Brook                                     uint32_t ctrl)
22495a09df21SPaul Brook {
22505a09df21SPaul Brook     uint64_t a, b;
22515a09df21SPaul Brook     int i;
22525a09df21SPaul Brook 
22535a09df21SPaul Brook     for (i = 0; i < 1 << SHIFT; i += 2) {
22545a09df21SPaul Brook         a = v->Q(((ctrl & 1) != 0) + i);
22555a09df21SPaul Brook         b = s->Q(((ctrl & 16) != 0) + i);
22565a09df21SPaul Brook         clmulq(&d->Q(i), &d->Q(i + 1), a, b);
22575a09df21SPaul Brook     }
2258fcf5ef2aSThomas Huth }
2259fcf5ef2aSThomas Huth 
2260f05f9789SPaolo Bonzini void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2261fcf5ef2aSThomas Huth {
2262fcf5ef2aSThomas Huth     int i;
2263f05f9789SPaolo Bonzini     Reg st = *v;
2264fcf5ef2aSThomas Huth     Reg rk = *s;
2265fcf5ef2aSThomas Huth 
2266a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2267a64fc269SPaul Brook         int j = i & 3;
2268a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4 * j + 0])] ^
2269a64fc269SPaul Brook                                     AES_Td1[st.B(AES_ishifts[4 * j + 1])] ^
2270a64fc269SPaul Brook                                     AES_Td2[st.B(AES_ishifts[4 * j + 2])] ^
2271a64fc269SPaul Brook                                     AES_Td3[st.B(AES_ishifts[4 * j + 3])]);
2272fcf5ef2aSThomas Huth     }
2273fcf5ef2aSThomas Huth }
2274fcf5ef2aSThomas Huth 
2275f05f9789SPaolo Bonzini void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2276fcf5ef2aSThomas Huth {
2277fcf5ef2aSThomas Huth     int i;
2278f05f9789SPaolo Bonzini     Reg st = *v;
2279fcf5ef2aSThomas Huth     Reg rk = *s;
2280fcf5ef2aSThomas Huth 
2281a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2282a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i & 15] + (i & ~15))]);
2283fcf5ef2aSThomas Huth     }
2284fcf5ef2aSThomas Huth }
2285fcf5ef2aSThomas Huth 
2286f05f9789SPaolo Bonzini void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2287fcf5ef2aSThomas Huth {
2288fcf5ef2aSThomas Huth     int i;
2289f05f9789SPaolo Bonzini     Reg st = *v;
2290fcf5ef2aSThomas Huth     Reg rk = *s;
2291fcf5ef2aSThomas Huth 
2292a64fc269SPaul Brook     for (i = 0 ; i < 2 << SHIFT ; i++) {
2293a64fc269SPaul Brook         int j = i & 3;
2294a64fc269SPaul Brook         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4 * j + 0])] ^
2295a64fc269SPaul Brook                                     AES_Te1[st.B(AES_shifts[4 * j + 1])] ^
2296a64fc269SPaul Brook                                     AES_Te2[st.B(AES_shifts[4 * j + 2])] ^
2297a64fc269SPaul Brook                                     AES_Te3[st.B(AES_shifts[4 * j + 3])]);
2298fcf5ef2aSThomas Huth     }
2299fcf5ef2aSThomas Huth }
2300fcf5ef2aSThomas Huth 
2301f05f9789SPaolo Bonzini void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s)
2302fcf5ef2aSThomas Huth {
2303fcf5ef2aSThomas Huth     int i;
2304f05f9789SPaolo Bonzini     Reg st = *v;
2305fcf5ef2aSThomas Huth     Reg rk = *s;
2306fcf5ef2aSThomas Huth 
2307a64fc269SPaul Brook     for (i = 0; i < 8 << SHIFT; i++) {
2308a64fc269SPaul Brook         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i & 15] + (i & ~15))]);
2309a64fc269SPaul Brook     }
2310fcf5ef2aSThomas Huth }
2311fcf5ef2aSThomas Huth 
2312a64fc269SPaul Brook #if SHIFT == 1
2313fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2314fcf5ef2aSThomas Huth {
2315fcf5ef2aSThomas Huth     int i;
2316fcf5ef2aSThomas Huth     Reg tmp = *s;
2317fcf5ef2aSThomas Huth 
2318fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2319fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4 * i + 0)][0] ^
2320fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 1)][1] ^
2321fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 2)][2] ^
2322fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4 * i + 3)][3]);
2323fcf5ef2aSThomas Huth     }
2324fcf5ef2aSThomas Huth }
2325fcf5ef2aSThomas Huth 
2326fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2327fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2328fcf5ef2aSThomas Huth {
2329fcf5ef2aSThomas Huth     int i;
2330fcf5ef2aSThomas Huth     Reg tmp = *s;
2331fcf5ef2aSThomas Huth 
2332fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2333fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2334fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2335fcf5ef2aSThomas Huth     }
2336fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2337fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2338fcf5ef2aSThomas Huth }
2339fcf5ef2aSThomas Huth #endif
2340a64fc269SPaul Brook #endif
2341fcf5ef2aSThomas Huth 
23423403cafeSPaul Brook #undef SSE_HELPER_S
23433403cafeSPaul Brook 
2344fcf5ef2aSThomas Huth #undef SHIFT
2345fcf5ef2aSThomas Huth #undef XMM_ONLY
2346fcf5ef2aSThomas Huth #undef Reg
2347fcf5ef2aSThomas Huth #undef B
2348fcf5ef2aSThomas Huth #undef W
2349fcf5ef2aSThomas Huth #undef L
2350fcf5ef2aSThomas Huth #undef Q
2351fcf5ef2aSThomas Huth #undef SUFFIX
2352