xref: /openbmc/qemu/target/i386/ops_sse.h (revision 25bdec79c629b49fbcf134f9eca063aaba6d4094)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *  Copyright (c) 2008 Intel Corporation  <andrew.zaborowski@intel.com>
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10d9ff33adSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "crypto/aes.h"
22fcf5ef2aSThomas Huth 
23fcf5ef2aSThomas Huth #if SHIFT == 0
24fcf5ef2aSThomas Huth #define Reg MMXReg
25d22697ddSPaolo Bonzini #define SIZE 8
26fcf5ef2aSThomas Huth #define XMM_ONLY(...)
27fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
28fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
29fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
30fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
31fcf5ef2aSThomas Huth #define SUFFIX _mmx
32fcf5ef2aSThomas Huth #else
33fcf5ef2aSThomas Huth #define Reg ZMMReg
34d22697ddSPaolo Bonzini #define SIZE 16
35fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
36fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
37fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
38fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
39fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
40fcf5ef2aSThomas Huth #define SUFFIX _xmm
41fcf5ef2aSThomas Huth #endif
42fcf5ef2aSThomas Huth 
43d22697ddSPaolo Bonzini /*
44d22697ddSPaolo Bonzini  * Copy the relevant parts of a Reg value around. In the case where
45d22697ddSPaolo Bonzini  * sizeof(Reg) > SIZE, these helpers operate only on the lower bytes of
46d22697ddSPaolo Bonzini  * a 64 byte ZMMReg, so we must copy only those and keep the top bytes
47d22697ddSPaolo Bonzini  * untouched in the guest-visible destination destination register.
48d22697ddSPaolo Bonzini  * Note that the "lower bytes" are placed last in memory on big-endian
49d22697ddSPaolo Bonzini  * hosts, which store the vector backwards in memory.  In that case the
50d22697ddSPaolo Bonzini  * copy *starts* at B(SIZE - 1) and ends at B(0), the opposite of
51d22697ddSPaolo Bonzini  * the little-endian case.
52d22697ddSPaolo Bonzini  */
53d22697ddSPaolo Bonzini #if HOST_BIG_ENDIAN
54d22697ddSPaolo Bonzini #define MOVE(d, r) memcpy(&((d).B(SIZE - 1)), &(r).B(SIZE - 1), SIZE)
55d22697ddSPaolo Bonzini #else
56d22697ddSPaolo Bonzini #define MOVE(d, r) memcpy(&(d).B(0), &(r).B(0), SIZE)
57d22697ddSPaolo Bonzini #endif
58d22697ddSPaolo Bonzini 
59fcf5ef2aSThomas Huth void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
60fcf5ef2aSThomas Huth {
61fcf5ef2aSThomas Huth     int shift;
62fcf5ef2aSThomas Huth 
63fcf5ef2aSThomas Huth     if (s->Q(0) > 15) {
64fcf5ef2aSThomas Huth         d->Q(0) = 0;
65fcf5ef2aSThomas Huth #if SHIFT == 1
66fcf5ef2aSThomas Huth         d->Q(1) = 0;
67fcf5ef2aSThomas Huth #endif
68fcf5ef2aSThomas Huth     } else {
69fcf5ef2aSThomas Huth         shift = s->B(0);
70fcf5ef2aSThomas Huth         d->W(0) >>= shift;
71fcf5ef2aSThomas Huth         d->W(1) >>= shift;
72fcf5ef2aSThomas Huth         d->W(2) >>= shift;
73fcf5ef2aSThomas Huth         d->W(3) >>= shift;
74fcf5ef2aSThomas Huth #if SHIFT == 1
75fcf5ef2aSThomas Huth         d->W(4) >>= shift;
76fcf5ef2aSThomas Huth         d->W(5) >>= shift;
77fcf5ef2aSThomas Huth         d->W(6) >>= shift;
78fcf5ef2aSThomas Huth         d->W(7) >>= shift;
79fcf5ef2aSThomas Huth #endif
80fcf5ef2aSThomas Huth     }
81fcf5ef2aSThomas Huth }
82fcf5ef2aSThomas Huth 
83fcf5ef2aSThomas Huth void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
84fcf5ef2aSThomas Huth {
85fcf5ef2aSThomas Huth     int shift;
86fcf5ef2aSThomas Huth 
87fcf5ef2aSThomas Huth     if (s->Q(0) > 15) {
88fcf5ef2aSThomas Huth         shift = 15;
89fcf5ef2aSThomas Huth     } else {
90fcf5ef2aSThomas Huth         shift = s->B(0);
91fcf5ef2aSThomas Huth     }
92fcf5ef2aSThomas Huth     d->W(0) = (int16_t)d->W(0) >> shift;
93fcf5ef2aSThomas Huth     d->W(1) = (int16_t)d->W(1) >> shift;
94fcf5ef2aSThomas Huth     d->W(2) = (int16_t)d->W(2) >> shift;
95fcf5ef2aSThomas Huth     d->W(3) = (int16_t)d->W(3) >> shift;
96fcf5ef2aSThomas Huth #if SHIFT == 1
97fcf5ef2aSThomas Huth     d->W(4) = (int16_t)d->W(4) >> shift;
98fcf5ef2aSThomas Huth     d->W(5) = (int16_t)d->W(5) >> shift;
99fcf5ef2aSThomas Huth     d->W(6) = (int16_t)d->W(6) >> shift;
100fcf5ef2aSThomas Huth     d->W(7) = (int16_t)d->W(7) >> shift;
101fcf5ef2aSThomas Huth #endif
102fcf5ef2aSThomas Huth }
103fcf5ef2aSThomas Huth 
104fcf5ef2aSThomas Huth void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
105fcf5ef2aSThomas Huth {
106fcf5ef2aSThomas Huth     int shift;
107fcf5ef2aSThomas Huth 
108fcf5ef2aSThomas Huth     if (s->Q(0) > 15) {
109fcf5ef2aSThomas Huth         d->Q(0) = 0;
110fcf5ef2aSThomas Huth #if SHIFT == 1
111fcf5ef2aSThomas Huth         d->Q(1) = 0;
112fcf5ef2aSThomas Huth #endif
113fcf5ef2aSThomas Huth     } else {
114fcf5ef2aSThomas Huth         shift = s->B(0);
115fcf5ef2aSThomas Huth         d->W(0) <<= shift;
116fcf5ef2aSThomas Huth         d->W(1) <<= shift;
117fcf5ef2aSThomas Huth         d->W(2) <<= shift;
118fcf5ef2aSThomas Huth         d->W(3) <<= shift;
119fcf5ef2aSThomas Huth #if SHIFT == 1
120fcf5ef2aSThomas Huth         d->W(4) <<= shift;
121fcf5ef2aSThomas Huth         d->W(5) <<= shift;
122fcf5ef2aSThomas Huth         d->W(6) <<= shift;
123fcf5ef2aSThomas Huth         d->W(7) <<= shift;
124fcf5ef2aSThomas Huth #endif
125fcf5ef2aSThomas Huth     }
126fcf5ef2aSThomas Huth }
127fcf5ef2aSThomas Huth 
128fcf5ef2aSThomas Huth void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
129fcf5ef2aSThomas Huth {
130fcf5ef2aSThomas Huth     int shift;
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth     if (s->Q(0) > 31) {
133fcf5ef2aSThomas Huth         d->Q(0) = 0;
134fcf5ef2aSThomas Huth #if SHIFT == 1
135fcf5ef2aSThomas Huth         d->Q(1) = 0;
136fcf5ef2aSThomas Huth #endif
137fcf5ef2aSThomas Huth     } else {
138fcf5ef2aSThomas Huth         shift = s->B(0);
139fcf5ef2aSThomas Huth         d->L(0) >>= shift;
140fcf5ef2aSThomas Huth         d->L(1) >>= shift;
141fcf5ef2aSThomas Huth #if SHIFT == 1
142fcf5ef2aSThomas Huth         d->L(2) >>= shift;
143fcf5ef2aSThomas Huth         d->L(3) >>= shift;
144fcf5ef2aSThomas Huth #endif
145fcf5ef2aSThomas Huth     }
146fcf5ef2aSThomas Huth }
147fcf5ef2aSThomas Huth 
148fcf5ef2aSThomas Huth void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
149fcf5ef2aSThomas Huth {
150fcf5ef2aSThomas Huth     int shift;
151fcf5ef2aSThomas Huth 
152fcf5ef2aSThomas Huth     if (s->Q(0) > 31) {
153fcf5ef2aSThomas Huth         shift = 31;
154fcf5ef2aSThomas Huth     } else {
155fcf5ef2aSThomas Huth         shift = s->B(0);
156fcf5ef2aSThomas Huth     }
157fcf5ef2aSThomas Huth     d->L(0) = (int32_t)d->L(0) >> shift;
158fcf5ef2aSThomas Huth     d->L(1) = (int32_t)d->L(1) >> shift;
159fcf5ef2aSThomas Huth #if SHIFT == 1
160fcf5ef2aSThomas Huth     d->L(2) = (int32_t)d->L(2) >> shift;
161fcf5ef2aSThomas Huth     d->L(3) = (int32_t)d->L(3) >> shift;
162fcf5ef2aSThomas Huth #endif
163fcf5ef2aSThomas Huth }
164fcf5ef2aSThomas Huth 
165fcf5ef2aSThomas Huth void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
166fcf5ef2aSThomas Huth {
167fcf5ef2aSThomas Huth     int shift;
168fcf5ef2aSThomas Huth 
169fcf5ef2aSThomas Huth     if (s->Q(0) > 31) {
170fcf5ef2aSThomas Huth         d->Q(0) = 0;
171fcf5ef2aSThomas Huth #if SHIFT == 1
172fcf5ef2aSThomas Huth         d->Q(1) = 0;
173fcf5ef2aSThomas Huth #endif
174fcf5ef2aSThomas Huth     } else {
175fcf5ef2aSThomas Huth         shift = s->B(0);
176fcf5ef2aSThomas Huth         d->L(0) <<= shift;
177fcf5ef2aSThomas Huth         d->L(1) <<= shift;
178fcf5ef2aSThomas Huth #if SHIFT == 1
179fcf5ef2aSThomas Huth         d->L(2) <<= shift;
180fcf5ef2aSThomas Huth         d->L(3) <<= shift;
181fcf5ef2aSThomas Huth #endif
182fcf5ef2aSThomas Huth     }
183fcf5ef2aSThomas Huth }
184fcf5ef2aSThomas Huth 
185fcf5ef2aSThomas Huth void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
186fcf5ef2aSThomas Huth {
187fcf5ef2aSThomas Huth     int shift;
188fcf5ef2aSThomas Huth 
189fcf5ef2aSThomas Huth     if (s->Q(0) > 63) {
190fcf5ef2aSThomas Huth         d->Q(0) = 0;
191fcf5ef2aSThomas Huth #if SHIFT == 1
192fcf5ef2aSThomas Huth         d->Q(1) = 0;
193fcf5ef2aSThomas Huth #endif
194fcf5ef2aSThomas Huth     } else {
195fcf5ef2aSThomas Huth         shift = s->B(0);
196fcf5ef2aSThomas Huth         d->Q(0) >>= shift;
197fcf5ef2aSThomas Huth #if SHIFT == 1
198fcf5ef2aSThomas Huth         d->Q(1) >>= shift;
199fcf5ef2aSThomas Huth #endif
200fcf5ef2aSThomas Huth     }
201fcf5ef2aSThomas Huth }
202fcf5ef2aSThomas Huth 
203fcf5ef2aSThomas Huth void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
204fcf5ef2aSThomas Huth {
205fcf5ef2aSThomas Huth     int shift;
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth     if (s->Q(0) > 63) {
208fcf5ef2aSThomas Huth         d->Q(0) = 0;
209fcf5ef2aSThomas Huth #if SHIFT == 1
210fcf5ef2aSThomas Huth         d->Q(1) = 0;
211fcf5ef2aSThomas Huth #endif
212fcf5ef2aSThomas Huth     } else {
213fcf5ef2aSThomas Huth         shift = s->B(0);
214fcf5ef2aSThomas Huth         d->Q(0) <<= shift;
215fcf5ef2aSThomas Huth #if SHIFT == 1
216fcf5ef2aSThomas Huth         d->Q(1) <<= shift;
217fcf5ef2aSThomas Huth #endif
218fcf5ef2aSThomas Huth     }
219fcf5ef2aSThomas Huth }
220fcf5ef2aSThomas Huth 
221fcf5ef2aSThomas Huth #if SHIFT == 1
222fcf5ef2aSThomas Huth void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
223fcf5ef2aSThomas Huth {
224fcf5ef2aSThomas Huth     int shift, i;
225fcf5ef2aSThomas Huth 
226fcf5ef2aSThomas Huth     shift = s->L(0);
227fcf5ef2aSThomas Huth     if (shift > 16) {
228fcf5ef2aSThomas Huth         shift = 16;
229fcf5ef2aSThomas Huth     }
230fcf5ef2aSThomas Huth     for (i = 0; i < 16 - shift; i++) {
231fcf5ef2aSThomas Huth         d->B(i) = d->B(i + shift);
232fcf5ef2aSThomas Huth     }
233fcf5ef2aSThomas Huth     for (i = 16 - shift; i < 16; i++) {
234fcf5ef2aSThomas Huth         d->B(i) = 0;
235fcf5ef2aSThomas Huth     }
236fcf5ef2aSThomas Huth }
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
239fcf5ef2aSThomas Huth {
240fcf5ef2aSThomas Huth     int shift, i;
241fcf5ef2aSThomas Huth 
242fcf5ef2aSThomas Huth     shift = s->L(0);
243fcf5ef2aSThomas Huth     if (shift > 16) {
244fcf5ef2aSThomas Huth         shift = 16;
245fcf5ef2aSThomas Huth     }
246fcf5ef2aSThomas Huth     for (i = 15; i >= shift; i--) {
247fcf5ef2aSThomas Huth         d->B(i) = d->B(i - shift);
248fcf5ef2aSThomas Huth     }
249fcf5ef2aSThomas Huth     for (i = 0; i < shift; i++) {
250fcf5ef2aSThomas Huth         d->B(i) = 0;
251fcf5ef2aSThomas Huth     }
252fcf5ef2aSThomas Huth }
253fcf5ef2aSThomas Huth #endif
254fcf5ef2aSThomas Huth 
255fcf5ef2aSThomas Huth #define SSE_HELPER_B(name, F)                                   \
256fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
257fcf5ef2aSThomas Huth     {                                                           \
258fcf5ef2aSThomas Huth         d->B(0) = F(d->B(0), s->B(0));                          \
259fcf5ef2aSThomas Huth         d->B(1) = F(d->B(1), s->B(1));                          \
260fcf5ef2aSThomas Huth         d->B(2) = F(d->B(2), s->B(2));                          \
261fcf5ef2aSThomas Huth         d->B(3) = F(d->B(3), s->B(3));                          \
262fcf5ef2aSThomas Huth         d->B(4) = F(d->B(4), s->B(4));                          \
263fcf5ef2aSThomas Huth         d->B(5) = F(d->B(5), s->B(5));                          \
264fcf5ef2aSThomas Huth         d->B(6) = F(d->B(6), s->B(6));                          \
265fcf5ef2aSThomas Huth         d->B(7) = F(d->B(7), s->B(7));                          \
266fcf5ef2aSThomas Huth         XMM_ONLY(                                               \
267fcf5ef2aSThomas Huth                  d->B(8) = F(d->B(8), s->B(8));                 \
268fcf5ef2aSThomas Huth                  d->B(9) = F(d->B(9), s->B(9));                 \
269fcf5ef2aSThomas Huth                  d->B(10) = F(d->B(10), s->B(10));              \
270fcf5ef2aSThomas Huth                  d->B(11) = F(d->B(11), s->B(11));              \
271fcf5ef2aSThomas Huth                  d->B(12) = F(d->B(12), s->B(12));              \
272fcf5ef2aSThomas Huth                  d->B(13) = F(d->B(13), s->B(13));              \
273fcf5ef2aSThomas Huth                  d->B(14) = F(d->B(14), s->B(14));              \
274fcf5ef2aSThomas Huth                  d->B(15) = F(d->B(15), s->B(15));              \
275fcf5ef2aSThomas Huth                                                         )       \
276fcf5ef2aSThomas Huth             }
277fcf5ef2aSThomas Huth 
278fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F)                                   \
279fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
280fcf5ef2aSThomas Huth     {                                                           \
281fcf5ef2aSThomas Huth         d->W(0) = F(d->W(0), s->W(0));                          \
282fcf5ef2aSThomas Huth         d->W(1) = F(d->W(1), s->W(1));                          \
283fcf5ef2aSThomas Huth         d->W(2) = F(d->W(2), s->W(2));                          \
284fcf5ef2aSThomas Huth         d->W(3) = F(d->W(3), s->W(3));                          \
285fcf5ef2aSThomas Huth         XMM_ONLY(                                               \
286fcf5ef2aSThomas Huth                  d->W(4) = F(d->W(4), s->W(4));                 \
287fcf5ef2aSThomas Huth                  d->W(5) = F(d->W(5), s->W(5));                 \
288fcf5ef2aSThomas Huth                  d->W(6) = F(d->W(6), s->W(6));                 \
289fcf5ef2aSThomas Huth                  d->W(7) = F(d->W(7), s->W(7));                 \
290fcf5ef2aSThomas Huth                                                         )       \
291fcf5ef2aSThomas Huth             }
292fcf5ef2aSThomas Huth 
293fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F)                                   \
294fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
295fcf5ef2aSThomas Huth     {                                                           \
296fcf5ef2aSThomas Huth         d->L(0) = F(d->L(0), s->L(0));                          \
297fcf5ef2aSThomas Huth         d->L(1) = F(d->L(1), s->L(1));                          \
298fcf5ef2aSThomas Huth         XMM_ONLY(                                               \
299fcf5ef2aSThomas Huth                  d->L(2) = F(d->L(2), s->L(2));                 \
300fcf5ef2aSThomas Huth                  d->L(3) = F(d->L(3), s->L(3));                 \
301fcf5ef2aSThomas Huth                                                         )       \
302fcf5ef2aSThomas Huth             }
303fcf5ef2aSThomas Huth 
304fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F)                                   \
305fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)   \
306fcf5ef2aSThomas Huth     {                                                           \
307fcf5ef2aSThomas Huth         d->Q(0) = F(d->Q(0), s->Q(0));                          \
308fcf5ef2aSThomas Huth         XMM_ONLY(                                               \
309fcf5ef2aSThomas Huth                  d->Q(1) = F(d->Q(1), s->Q(1));                 \
310fcf5ef2aSThomas Huth                                                         )       \
311fcf5ef2aSThomas Huth             }
312fcf5ef2aSThomas Huth 
313fcf5ef2aSThomas Huth #if SHIFT == 0
314fcf5ef2aSThomas Huth static inline int satub(int x)
315fcf5ef2aSThomas Huth {
316fcf5ef2aSThomas Huth     if (x < 0) {
317fcf5ef2aSThomas Huth         return 0;
318fcf5ef2aSThomas Huth     } else if (x > 255) {
319fcf5ef2aSThomas Huth         return 255;
320fcf5ef2aSThomas Huth     } else {
321fcf5ef2aSThomas Huth         return x;
322fcf5ef2aSThomas Huth     }
323fcf5ef2aSThomas Huth }
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth static inline int satuw(int x)
326fcf5ef2aSThomas Huth {
327fcf5ef2aSThomas Huth     if (x < 0) {
328fcf5ef2aSThomas Huth         return 0;
329fcf5ef2aSThomas Huth     } else if (x > 65535) {
330fcf5ef2aSThomas Huth         return 65535;
331fcf5ef2aSThomas Huth     } else {
332fcf5ef2aSThomas Huth         return x;
333fcf5ef2aSThomas Huth     }
334fcf5ef2aSThomas Huth }
335fcf5ef2aSThomas Huth 
336fcf5ef2aSThomas Huth static inline int satsb(int x)
337fcf5ef2aSThomas Huth {
338fcf5ef2aSThomas Huth     if (x < -128) {
339fcf5ef2aSThomas Huth         return -128;
340fcf5ef2aSThomas Huth     } else if (x > 127) {
341fcf5ef2aSThomas Huth         return 127;
342fcf5ef2aSThomas Huth     } else {
343fcf5ef2aSThomas Huth         return x;
344fcf5ef2aSThomas Huth     }
345fcf5ef2aSThomas Huth }
346fcf5ef2aSThomas Huth 
347fcf5ef2aSThomas Huth static inline int satsw(int x)
348fcf5ef2aSThomas Huth {
349fcf5ef2aSThomas Huth     if (x < -32768) {
350fcf5ef2aSThomas Huth         return -32768;
351fcf5ef2aSThomas Huth     } else if (x > 32767) {
352fcf5ef2aSThomas Huth         return 32767;
353fcf5ef2aSThomas Huth     } else {
354fcf5ef2aSThomas Huth         return x;
355fcf5ef2aSThomas Huth     }
356fcf5ef2aSThomas Huth }
357fcf5ef2aSThomas Huth 
358fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b))
359fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b))
360fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b))
361fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
362fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
363fcf5ef2aSThomas Huth 
364fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b))
365fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b))
366fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b))
367fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
368fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
369fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
370fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
371fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
372fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
373fcf5ef2aSThomas Huth 
374fcf5ef2aSThomas Huth #define FAND(a, b) ((a) & (b))
375fcf5ef2aSThomas Huth #define FANDN(a, b) ((~(a)) & (b))
376fcf5ef2aSThomas Huth #define FOR(a, b) ((a) | (b))
377fcf5ef2aSThomas Huth #define FXOR(a, b) ((a) ^ (b))
378fcf5ef2aSThomas Huth 
379fcf5ef2aSThomas Huth #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
380fcf5ef2aSThomas Huth #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
381fcf5ef2aSThomas Huth #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
382fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth #define FMULLW(a, b) ((a) * (b))
385fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
386fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16)
387fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1)
390fcf5ef2aSThomas Huth #endif
391fcf5ef2aSThomas Huth 
392fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddb, FADD)
393fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddw, FADD)
394fcf5ef2aSThomas Huth SSE_HELPER_L(helper_paddl, FADD)
395fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_paddq, FADD)
396fcf5ef2aSThomas Huth 
397fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubb, FSUB)
398fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubw, FSUB)
399fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psubl, FSUB)
400fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_psubq, FSUB)
401fcf5ef2aSThomas Huth 
402fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddusb, FADDUB)
403fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddsb, FADDSB)
404fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubusb, FSUBUB)
405fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubsb, FSUBSB)
406fcf5ef2aSThomas Huth 
407fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddusw, FADDUW)
408fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddsw, FADDSW)
409fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubusw, FSUBUW)
410fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubsw, FSUBSW)
411fcf5ef2aSThomas Huth 
412fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminub, FMINUB)
413fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxub, FMAXUB)
414fcf5ef2aSThomas Huth 
415fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminsw, FMINSW)
416fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxsw, FMAXSW)
417fcf5ef2aSThomas Huth 
418fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pand, FAND)
419fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pandn, FANDN)
420fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_por, FOR)
421fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pxor, FXOR)
422fcf5ef2aSThomas Huth 
423fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpgtb, FCMPGTB)
424fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpgtw, FCMPGTW)
425fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpgtl, FCMPGTL)
426fcf5ef2aSThomas Huth 
427fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpeqb, FCMPEQ)
428fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpeqw, FCMPEQ)
429fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpeql, FCMPEQ)
430fcf5ef2aSThomas Huth 
431fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmullw, FMULLW)
432fcf5ef2aSThomas Huth #if SHIFT == 0
433fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrw, FMULHRW)
434fcf5ef2aSThomas Huth #endif
435fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW)
436fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW)
437fcf5ef2aSThomas Huth 
438fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG)
439fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG)
440fcf5ef2aSThomas Huth 
441fcf5ef2aSThomas Huth void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
442fcf5ef2aSThomas Huth {
443fcf5ef2aSThomas Huth     d->Q(0) = (uint64_t)s->L(0) * (uint64_t)d->L(0);
444fcf5ef2aSThomas Huth #if SHIFT == 1
445fcf5ef2aSThomas Huth     d->Q(1) = (uint64_t)s->L(2) * (uint64_t)d->L(2);
446fcf5ef2aSThomas Huth #endif
447fcf5ef2aSThomas Huth }
448fcf5ef2aSThomas Huth 
449fcf5ef2aSThomas Huth void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
450fcf5ef2aSThomas Huth {
451fcf5ef2aSThomas Huth     int i;
452fcf5ef2aSThomas Huth 
453fcf5ef2aSThomas Huth     for (i = 0; i < (2 << SHIFT); i++) {
454fcf5ef2aSThomas Huth         d->L(i) = (int16_t)s->W(2 * i) * (int16_t)d->W(2 * i) +
455fcf5ef2aSThomas Huth             (int16_t)s->W(2 * i + 1) * (int16_t)d->W(2 * i + 1);
456fcf5ef2aSThomas Huth     }
457fcf5ef2aSThomas Huth }
458fcf5ef2aSThomas Huth 
459fcf5ef2aSThomas Huth #if SHIFT == 0
460fcf5ef2aSThomas Huth static inline int abs1(int a)
461fcf5ef2aSThomas Huth {
462fcf5ef2aSThomas Huth     if (a < 0) {
463fcf5ef2aSThomas Huth         return -a;
464fcf5ef2aSThomas Huth     } else {
465fcf5ef2aSThomas Huth         return a;
466fcf5ef2aSThomas Huth     }
467fcf5ef2aSThomas Huth }
468fcf5ef2aSThomas Huth #endif
469fcf5ef2aSThomas Huth void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
470fcf5ef2aSThomas Huth {
471fcf5ef2aSThomas Huth     unsigned int val;
472fcf5ef2aSThomas Huth 
473fcf5ef2aSThomas Huth     val = 0;
474fcf5ef2aSThomas Huth     val += abs1(d->B(0) - s->B(0));
475fcf5ef2aSThomas Huth     val += abs1(d->B(1) - s->B(1));
476fcf5ef2aSThomas Huth     val += abs1(d->B(2) - s->B(2));
477fcf5ef2aSThomas Huth     val += abs1(d->B(3) - s->B(3));
478fcf5ef2aSThomas Huth     val += abs1(d->B(4) - s->B(4));
479fcf5ef2aSThomas Huth     val += abs1(d->B(5) - s->B(5));
480fcf5ef2aSThomas Huth     val += abs1(d->B(6) - s->B(6));
481fcf5ef2aSThomas Huth     val += abs1(d->B(7) - s->B(7));
482fcf5ef2aSThomas Huth     d->Q(0) = val;
483fcf5ef2aSThomas Huth #if SHIFT == 1
484fcf5ef2aSThomas Huth     val = 0;
485fcf5ef2aSThomas Huth     val += abs1(d->B(8) - s->B(8));
486fcf5ef2aSThomas Huth     val += abs1(d->B(9) - s->B(9));
487fcf5ef2aSThomas Huth     val += abs1(d->B(10) - s->B(10));
488fcf5ef2aSThomas Huth     val += abs1(d->B(11) - s->B(11));
489fcf5ef2aSThomas Huth     val += abs1(d->B(12) - s->B(12));
490fcf5ef2aSThomas Huth     val += abs1(d->B(13) - s->B(13));
491fcf5ef2aSThomas Huth     val += abs1(d->B(14) - s->B(14));
492fcf5ef2aSThomas Huth     val += abs1(d->B(15) - s->B(15));
493fcf5ef2aSThomas Huth     d->Q(1) = val;
494fcf5ef2aSThomas Huth #endif
495fcf5ef2aSThomas Huth }
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
498fcf5ef2aSThomas Huth                                   target_ulong a0)
499fcf5ef2aSThomas Huth {
500fcf5ef2aSThomas Huth     int i;
501fcf5ef2aSThomas Huth 
502fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
503fcf5ef2aSThomas Huth         if (s->B(i) & 0x80) {
504fcf5ef2aSThomas Huth             cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC());
505fcf5ef2aSThomas Huth         }
506fcf5ef2aSThomas Huth     }
507fcf5ef2aSThomas Huth }
508fcf5ef2aSThomas Huth 
509fcf5ef2aSThomas Huth void glue(helper_movl_mm_T0, SUFFIX)(Reg *d, uint32_t val)
510fcf5ef2aSThomas Huth {
511fcf5ef2aSThomas Huth     d->L(0) = val;
512fcf5ef2aSThomas Huth     d->L(1) = 0;
513fcf5ef2aSThomas Huth #if SHIFT == 1
514fcf5ef2aSThomas Huth     d->Q(1) = 0;
515fcf5ef2aSThomas Huth #endif
516fcf5ef2aSThomas Huth }
517fcf5ef2aSThomas Huth 
518fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
519fcf5ef2aSThomas Huth void glue(helper_movq_mm_T0, SUFFIX)(Reg *d, uint64_t val)
520fcf5ef2aSThomas Huth {
521fcf5ef2aSThomas Huth     d->Q(0) = val;
522fcf5ef2aSThomas Huth #if SHIFT == 1
523fcf5ef2aSThomas Huth     d->Q(1) = 0;
524fcf5ef2aSThomas Huth #endif
525fcf5ef2aSThomas Huth }
526fcf5ef2aSThomas Huth #endif
527fcf5ef2aSThomas Huth 
528fcf5ef2aSThomas Huth #if SHIFT == 0
529fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order)
530fcf5ef2aSThomas Huth {
531fcf5ef2aSThomas Huth     Reg r;
532fcf5ef2aSThomas Huth 
533fcf5ef2aSThomas Huth     r.W(0) = s->W(order & 3);
534fcf5ef2aSThomas Huth     r.W(1) = s->W((order >> 2) & 3);
535fcf5ef2aSThomas Huth     r.W(2) = s->W((order >> 4) & 3);
536fcf5ef2aSThomas Huth     r.W(3) = s->W((order >> 6) & 3);
537d22697ddSPaolo Bonzini     MOVE(*d, r);
538fcf5ef2aSThomas Huth }
539fcf5ef2aSThomas Huth #else
540ce4fa29fSPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *s, int order)
541fcf5ef2aSThomas Huth {
542fcf5ef2aSThomas Huth     Reg r;
543fcf5ef2aSThomas Huth 
544fcf5ef2aSThomas Huth     r.L(0) = d->L(order & 3);
545fcf5ef2aSThomas Huth     r.L(1) = d->L((order >> 2) & 3);
546fcf5ef2aSThomas Huth     r.L(2) = s->L((order >> 4) & 3);
547fcf5ef2aSThomas Huth     r.L(3) = s->L((order >> 6) & 3);
548d22697ddSPaolo Bonzini     MOVE(*d, r);
549fcf5ef2aSThomas Huth }
550fcf5ef2aSThomas Huth 
551ce4fa29fSPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *s, int order)
552fcf5ef2aSThomas Huth {
553fcf5ef2aSThomas Huth     Reg r;
554fcf5ef2aSThomas Huth 
555fcf5ef2aSThomas Huth     r.Q(0) = d->Q(order & 1);
556fcf5ef2aSThomas Huth     r.Q(1) = s->Q((order >> 1) & 1);
557d22697ddSPaolo Bonzini     MOVE(*d, r);
558fcf5ef2aSThomas Huth }
559fcf5ef2aSThomas Huth 
560fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order)
561fcf5ef2aSThomas Huth {
562fcf5ef2aSThomas Huth     Reg r;
563fcf5ef2aSThomas Huth 
564fcf5ef2aSThomas Huth     r.L(0) = s->L(order & 3);
565fcf5ef2aSThomas Huth     r.L(1) = s->L((order >> 2) & 3);
566fcf5ef2aSThomas Huth     r.L(2) = s->L((order >> 4) & 3);
567fcf5ef2aSThomas Huth     r.L(3) = s->L((order >> 6) & 3);
568d22697ddSPaolo Bonzini     MOVE(*d, r);
569fcf5ef2aSThomas Huth }
570fcf5ef2aSThomas Huth 
571fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order)
572fcf5ef2aSThomas Huth {
573fcf5ef2aSThomas Huth     Reg r;
574fcf5ef2aSThomas Huth 
575fcf5ef2aSThomas Huth     r.W(0) = s->W(order & 3);
576fcf5ef2aSThomas Huth     r.W(1) = s->W((order >> 2) & 3);
577fcf5ef2aSThomas Huth     r.W(2) = s->W((order >> 4) & 3);
578fcf5ef2aSThomas Huth     r.W(3) = s->W((order >> 6) & 3);
579fcf5ef2aSThomas Huth     r.Q(1) = s->Q(1);
580d22697ddSPaolo Bonzini     MOVE(*d, r);
581fcf5ef2aSThomas Huth }
582fcf5ef2aSThomas Huth 
583fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order)
584fcf5ef2aSThomas Huth {
585fcf5ef2aSThomas Huth     Reg r;
586fcf5ef2aSThomas Huth 
587fcf5ef2aSThomas Huth     r.Q(0) = s->Q(0);
588fcf5ef2aSThomas Huth     r.W(4) = s->W(4 + (order & 3));
589fcf5ef2aSThomas Huth     r.W(5) = s->W(4 + ((order >> 2) & 3));
590fcf5ef2aSThomas Huth     r.W(6) = s->W(4 + ((order >> 4) & 3));
591fcf5ef2aSThomas Huth     r.W(7) = s->W(4 + ((order >> 6) & 3));
592d22697ddSPaolo Bonzini     MOVE(*d, r);
593fcf5ef2aSThomas Huth }
594fcf5ef2aSThomas Huth #endif
595fcf5ef2aSThomas Huth 
596fcf5ef2aSThomas Huth #if SHIFT == 1
597fcf5ef2aSThomas Huth /* FPU ops */
598fcf5ef2aSThomas Huth /* XXX: not accurate */
599fcf5ef2aSThomas Huth 
600fcf5ef2aSThomas Huth #define SSE_HELPER_S(name, F)                                           \
601ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
602fcf5ef2aSThomas Huth     {                                                                   \
603fcf5ef2aSThomas Huth         d->ZMM_S(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
604fcf5ef2aSThomas Huth         d->ZMM_S(1) = F(32, d->ZMM_S(1), s->ZMM_S(1));                  \
605fcf5ef2aSThomas Huth         d->ZMM_S(2) = F(32, d->ZMM_S(2), s->ZMM_S(2));                  \
606fcf5ef2aSThomas Huth         d->ZMM_S(3) = F(32, d->ZMM_S(3), s->ZMM_S(3));                  \
607fcf5ef2aSThomas Huth     }                                                                   \
608fcf5ef2aSThomas Huth                                                                         \
609fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)        \
610fcf5ef2aSThomas Huth     {                                                                   \
611fcf5ef2aSThomas Huth         d->ZMM_S(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
612fcf5ef2aSThomas Huth     }                                                                   \
613fcf5ef2aSThomas Huth                                                                         \
614ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
615fcf5ef2aSThomas Huth     {                                                                   \
616fcf5ef2aSThomas Huth         d->ZMM_D(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
617fcf5ef2aSThomas Huth         d->ZMM_D(1) = F(64, d->ZMM_D(1), s->ZMM_D(1));                  \
618fcf5ef2aSThomas Huth     }                                                                   \
619fcf5ef2aSThomas Huth                                                                         \
620fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)        \
621fcf5ef2aSThomas Huth     {                                                                   \
622fcf5ef2aSThomas Huth         d->ZMM_D(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
623fcf5ef2aSThomas Huth     }
624fcf5ef2aSThomas Huth 
625fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
626fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
627fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
628fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
629fcf5ef2aSThomas Huth #define FPU_SQRT(size, a, b) float ## size ## _sqrt(b, &env->sse_status)
630fcf5ef2aSThomas Huth 
631fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the
632fcf5ef2aSThomas Huth  * special cases right: for min and max Intel specifies that (-0,0),
633fcf5ef2aSThomas Huth  * (NaN, anything) and (anything, NaN) return the second argument.
634fcf5ef2aSThomas Huth  */
635fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b)                                     \
636fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
637fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b)                                     \
638fcf5ef2aSThomas Huth     (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
639fcf5ef2aSThomas Huth 
640fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD)
641fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB)
642fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL)
643fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV)
644fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN)
645fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX)
646fcf5ef2aSThomas Huth SSE_HELPER_S(sqrt, FPU_SQRT)
647fcf5ef2aSThomas Huth 
648fcf5ef2aSThomas Huth 
649fcf5ef2aSThomas Huth /* float to float conversions */
650ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
651fcf5ef2aSThomas Huth {
652fcf5ef2aSThomas Huth     float32 s0, s1;
653fcf5ef2aSThomas Huth 
654fcf5ef2aSThomas Huth     s0 = s->ZMM_S(0);
655fcf5ef2aSThomas Huth     s1 = s->ZMM_S(1);
656fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s0, &env->sse_status);
657fcf5ef2aSThomas Huth     d->ZMM_D(1) = float32_to_float64(s1, &env->sse_status);
658fcf5ef2aSThomas Huth }
659fcf5ef2aSThomas Huth 
660ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
661fcf5ef2aSThomas Huth {
662fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
663fcf5ef2aSThomas Huth     d->ZMM_S(1) = float64_to_float32(s->ZMM_D(1), &env->sse_status);
664fcf5ef2aSThomas Huth     d->Q(1) = 0;
665fcf5ef2aSThomas Huth }
666fcf5ef2aSThomas Huth 
667fcf5ef2aSThomas Huth void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *s)
668fcf5ef2aSThomas Huth {
669fcf5ef2aSThomas Huth     d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status);
670fcf5ef2aSThomas Huth }
671fcf5ef2aSThomas Huth 
672fcf5ef2aSThomas Huth void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *s)
673fcf5ef2aSThomas Huth {
674fcf5ef2aSThomas Huth     d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status);
675fcf5ef2aSThomas Huth }
676fcf5ef2aSThomas Huth 
677fcf5ef2aSThomas Huth /* integer to float */
678ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
679fcf5ef2aSThomas Huth {
680fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->ZMM_L(0), &env->sse_status);
681fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->ZMM_L(1), &env->sse_status);
682fcf5ef2aSThomas Huth     d->ZMM_S(2) = int32_to_float32(s->ZMM_L(2), &env->sse_status);
683fcf5ef2aSThomas Huth     d->ZMM_S(3) = int32_to_float32(s->ZMM_L(3), &env->sse_status);
684fcf5ef2aSThomas Huth }
685fcf5ef2aSThomas Huth 
686ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
687fcf5ef2aSThomas Huth {
688fcf5ef2aSThomas Huth     int32_t l0, l1;
689fcf5ef2aSThomas Huth 
690fcf5ef2aSThomas Huth     l0 = (int32_t)s->ZMM_L(0);
691fcf5ef2aSThomas Huth     l1 = (int32_t)s->ZMM_L(1);
692fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(l0, &env->sse_status);
693fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(l1, &env->sse_status);
694fcf5ef2aSThomas Huth }
695fcf5ef2aSThomas Huth 
696fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s)
697fcf5ef2aSThomas Huth {
698fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status);
699fcf5ef2aSThomas Huth     d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status);
700fcf5ef2aSThomas Huth }
701fcf5ef2aSThomas Huth 
702fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s)
703fcf5ef2aSThomas Huth {
704fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status);
705fcf5ef2aSThomas Huth     d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status);
706fcf5ef2aSThomas Huth }
707fcf5ef2aSThomas Huth 
708fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val)
709fcf5ef2aSThomas Huth {
710fcf5ef2aSThomas Huth     d->ZMM_S(0) = int32_to_float32(val, &env->sse_status);
711fcf5ef2aSThomas Huth }
712fcf5ef2aSThomas Huth 
713fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val)
714fcf5ef2aSThomas Huth {
715fcf5ef2aSThomas Huth     d->ZMM_D(0) = int32_to_float64(val, &env->sse_status);
716fcf5ef2aSThomas Huth }
717fcf5ef2aSThomas Huth 
718fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
719fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val)
720fcf5ef2aSThomas Huth {
721fcf5ef2aSThomas Huth     d->ZMM_S(0) = int64_to_float32(val, &env->sse_status);
722fcf5ef2aSThomas Huth }
723fcf5ef2aSThomas Huth 
724fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val)
725fcf5ef2aSThomas Huth {
726fcf5ef2aSThomas Huth     d->ZMM_D(0) = int64_to_float64(val, &env->sse_status);
727fcf5ef2aSThomas Huth }
728fcf5ef2aSThomas Huth #endif
729fcf5ef2aSThomas Huth 
730fcf5ef2aSThomas Huth /* float to integer */
7311e8a98b5SPeter Maydell 
7321e8a98b5SPeter Maydell /*
7331e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7341e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7351e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7361e8a98b5SPeter Maydell  */
7371e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7381e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7391e8a98b5SPeter Maydell     {                                                                   \
7401e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7411e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7421e8a98b5SPeter Maydell                                                                         \
7431e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7441e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7451e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7461e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7471e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7481e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7491e8a98b5SPeter Maydell         }                                                               \
7501e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7511e8a98b5SPeter Maydell         return r;                                                       \
7521e8a98b5SPeter Maydell     }
7531e8a98b5SPeter Maydell 
7541e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7551e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7561e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7571e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7581e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7591e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7601e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7611e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
7621e8a98b5SPeter Maydell 
763ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
764fcf5ef2aSThomas Huth {
7651e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7661e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
7671e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32(s->ZMM_S(2), &env->sse_status);
7681e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32(s->ZMM_S(3), &env->sse_status);
769fcf5ef2aSThomas Huth }
770fcf5ef2aSThomas Huth 
771ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
772fcf5ef2aSThomas Huth {
7731e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7741e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
775fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
776fcf5ef2aSThomas Huth }
777fcf5ef2aSThomas Huth 
778fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
779fcf5ef2aSThomas Huth {
7801e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7811e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
782fcf5ef2aSThomas Huth }
783fcf5ef2aSThomas Huth 
784fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
785fcf5ef2aSThomas Huth {
7861e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7871e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
788fcf5ef2aSThomas Huth }
789fcf5ef2aSThomas Huth 
790fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
791fcf5ef2aSThomas Huth {
7921e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
793fcf5ef2aSThomas Huth }
794fcf5ef2aSThomas Huth 
795fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
796fcf5ef2aSThomas Huth {
7971e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
798fcf5ef2aSThomas Huth }
799fcf5ef2aSThomas Huth 
800fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
801fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
802fcf5ef2aSThomas Huth {
8031e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
804fcf5ef2aSThomas Huth }
805fcf5ef2aSThomas Huth 
806fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
807fcf5ef2aSThomas Huth {
8081e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
809fcf5ef2aSThomas Huth }
810fcf5ef2aSThomas Huth #endif
811fcf5ef2aSThomas Huth 
812fcf5ef2aSThomas Huth /* float to integer truncated */
813ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
814fcf5ef2aSThomas Huth {
8151e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8161e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
8171e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32_round_to_zero(s->ZMM_S(2), &env->sse_status);
8181e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32_round_to_zero(s->ZMM_S(3), &env->sse_status);
819fcf5ef2aSThomas Huth }
820fcf5ef2aSThomas Huth 
821ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
822fcf5ef2aSThomas Huth {
8231e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8241e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
825fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
826fcf5ef2aSThomas Huth }
827fcf5ef2aSThomas Huth 
828fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
829fcf5ef2aSThomas Huth {
8301e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8311e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
832fcf5ef2aSThomas Huth }
833fcf5ef2aSThomas Huth 
834fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
835fcf5ef2aSThomas Huth {
8361e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8371e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
838fcf5ef2aSThomas Huth }
839fcf5ef2aSThomas Huth 
840fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
841fcf5ef2aSThomas Huth {
8421e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
843fcf5ef2aSThomas Huth }
844fcf5ef2aSThomas Huth 
845fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
846fcf5ef2aSThomas Huth {
8471e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
848fcf5ef2aSThomas Huth }
849fcf5ef2aSThomas Huth 
850fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
851fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
852fcf5ef2aSThomas Huth {
8531e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
854fcf5ef2aSThomas Huth }
855fcf5ef2aSThomas Huth 
856fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
857fcf5ef2aSThomas Huth {
8581e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
859fcf5ef2aSThomas Huth }
860fcf5ef2aSThomas Huth #endif
861fcf5ef2aSThomas Huth 
862ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
863fcf5ef2aSThomas Huth {
864418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
865fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
866fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
867fcf5ef2aSThomas Huth                               &env->sse_status);
868fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_div(float32_one,
869fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(1), &env->sse_status),
870fcf5ef2aSThomas Huth                               &env->sse_status);
871fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_div(float32_one,
872fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(2), &env->sse_status),
873fcf5ef2aSThomas Huth                               &env->sse_status);
874fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_div(float32_one,
875fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(3), &env->sse_status),
876fcf5ef2aSThomas Huth                               &env->sse_status);
877418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
878fcf5ef2aSThomas Huth }
879fcf5ef2aSThomas Huth 
880fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
881fcf5ef2aSThomas Huth {
882418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
883fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
884fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
885fcf5ef2aSThomas Huth                               &env->sse_status);
886418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
887fcf5ef2aSThomas Huth }
888fcf5ef2aSThomas Huth 
889ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
890fcf5ef2aSThomas Huth {
891418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
892fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
893fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_div(float32_one, s->ZMM_S(1), &env->sse_status);
894fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_div(float32_one, s->ZMM_S(2), &env->sse_status);
895fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_div(float32_one, s->ZMM_S(3), &env->sse_status);
896418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
897fcf5ef2aSThomas Huth }
898fcf5ef2aSThomas Huth 
899fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
900fcf5ef2aSThomas Huth {
901418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
902fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
903418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
904fcf5ef2aSThomas Huth }
905fcf5ef2aSThomas Huth 
906fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
907fcf5ef2aSThomas Huth {
908fcf5ef2aSThomas Huth     uint64_t mask;
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth     if (len == 0) {
911fcf5ef2aSThomas Huth         mask = ~0LL;
912fcf5ef2aSThomas Huth     } else {
913fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
914fcf5ef2aSThomas Huth     }
915fcf5ef2aSThomas Huth     return (src >> shift) & mask;
916fcf5ef2aSThomas Huth }
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
919fcf5ef2aSThomas Huth {
920fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1), s->ZMM_B(0));
921fcf5ef2aSThomas Huth }
922fcf5ef2aSThomas Huth 
923fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
924fcf5ef2aSThomas Huth {
925fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
926fcf5ef2aSThomas Huth }
927fcf5ef2aSThomas Huth 
928fcf5ef2aSThomas Huth static inline uint64_t helper_insertq(uint64_t src, int shift, int len)
929fcf5ef2aSThomas Huth {
930fcf5ef2aSThomas Huth     uint64_t mask;
931fcf5ef2aSThomas Huth 
932fcf5ef2aSThomas Huth     if (len == 0) {
933fcf5ef2aSThomas Huth         mask = ~0ULL;
934fcf5ef2aSThomas Huth     } else {
935fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
936fcf5ef2aSThomas Huth     }
937fcf5ef2aSThomas Huth     return (src & ~(mask << shift)) | ((src & mask) << shift);
938fcf5ef2aSThomas Huth }
939fcf5ef2aSThomas Huth 
940fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
941fcf5ef2aSThomas Huth {
942fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(s->ZMM_Q(0), s->ZMM_B(9), s->ZMM_B(8));
943fcf5ef2aSThomas Huth }
944fcf5ef2aSThomas Huth 
945fcf5ef2aSThomas Huth void helper_insertq_i(CPUX86State *env, ZMMReg *d, int index, int length)
946fcf5ef2aSThomas Huth {
947fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), index, length);
948fcf5ef2aSThomas Huth }
949fcf5ef2aSThomas Huth 
950ce4fa29fSPaolo Bonzini void glue(helper_haddps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
951fcf5ef2aSThomas Huth {
952fcf5ef2aSThomas Huth     ZMMReg r;
953fcf5ef2aSThomas Huth 
954fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_add(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
955fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_add(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
956fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_add(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
957fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_add(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
958d22697ddSPaolo Bonzini     MOVE(*d, r);
959fcf5ef2aSThomas Huth }
960fcf5ef2aSThomas Huth 
961ce4fa29fSPaolo Bonzini void glue(helper_haddpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
962fcf5ef2aSThomas Huth {
963fcf5ef2aSThomas Huth     ZMMReg r;
964fcf5ef2aSThomas Huth 
965fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_add(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
966fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_add(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
967d22697ddSPaolo Bonzini     MOVE(*d, r);
968fcf5ef2aSThomas Huth }
969fcf5ef2aSThomas Huth 
970ce4fa29fSPaolo Bonzini void glue(helper_hsubps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
971fcf5ef2aSThomas Huth {
972fcf5ef2aSThomas Huth     ZMMReg r;
973fcf5ef2aSThomas Huth 
974fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_sub(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
975fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_sub(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
976fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_sub(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
977fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_sub(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
978d22697ddSPaolo Bonzini     MOVE(*d, r);
979fcf5ef2aSThomas Huth }
980fcf5ef2aSThomas Huth 
981ce4fa29fSPaolo Bonzini void glue(helper_hsubpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
982fcf5ef2aSThomas Huth {
983fcf5ef2aSThomas Huth     ZMMReg r;
984fcf5ef2aSThomas Huth 
985fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_sub(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
986fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_sub(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
987d22697ddSPaolo Bonzini     MOVE(*d, r);
988fcf5ef2aSThomas Huth }
989fcf5ef2aSThomas Huth 
990ce4fa29fSPaolo Bonzini void glue(helper_addsubps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
991fcf5ef2aSThomas Huth {
992fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_sub(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status);
993fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_add(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status);
994fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_sub(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status);
995fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_add(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status);
996fcf5ef2aSThomas Huth }
997fcf5ef2aSThomas Huth 
998ce4fa29fSPaolo Bonzini void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)
999fcf5ef2aSThomas Huth {
1000fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_sub(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1001fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_add(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1002fcf5ef2aSThomas Huth }
1003fcf5ef2aSThomas Huth 
1004fcf5ef2aSThomas Huth /* XXX: unordered */
1005fcf5ef2aSThomas Huth #define SSE_HELPER_CMP(name, F)                                         \
1006ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
1007fcf5ef2aSThomas Huth     {                                                                   \
1008fcf5ef2aSThomas Huth         d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
1009fcf5ef2aSThomas Huth         d->ZMM_L(1) = F(32, d->ZMM_S(1), s->ZMM_S(1));                  \
1010fcf5ef2aSThomas Huth         d->ZMM_L(2) = F(32, d->ZMM_S(2), s->ZMM_S(2));                  \
1011fcf5ef2aSThomas Huth         d->ZMM_L(3) = F(32, d->ZMM_S(3), s->ZMM_S(3));                  \
1012fcf5ef2aSThomas Huth     }                                                                   \
1013fcf5ef2aSThomas Huth                                                                         \
1014fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)        \
1015fcf5ef2aSThomas Huth     {                                                                   \
1016fcf5ef2aSThomas Huth         d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
1017fcf5ef2aSThomas Huth     }                                                                   \
1018fcf5ef2aSThomas Huth                                                                         \
1019ce4fa29fSPaolo Bonzini     void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\
1020fcf5ef2aSThomas Huth     {                                                                   \
1021fcf5ef2aSThomas Huth         d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
1022fcf5ef2aSThomas Huth         d->ZMM_Q(1) = F(64, d->ZMM_D(1), s->ZMM_D(1));                  \
1023fcf5ef2aSThomas Huth     }                                                                   \
1024fcf5ef2aSThomas Huth                                                                         \
1025fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)        \
1026fcf5ef2aSThomas Huth     {                                                                   \
1027fcf5ef2aSThomas Huth         d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
1028fcf5ef2aSThomas Huth     }
1029fcf5ef2aSThomas Huth 
1030fcf5ef2aSThomas Huth #define FPU_CMPEQ(size, a, b)                                           \
1031fcf5ef2aSThomas Huth     (float ## size ## _eq_quiet(a, b, &env->sse_status) ? -1 : 0)
1032fcf5ef2aSThomas Huth #define FPU_CMPLT(size, a, b)                                           \
1033fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0)
1034fcf5ef2aSThomas Huth #define FPU_CMPLE(size, a, b)                                           \
1035fcf5ef2aSThomas Huth     (float ## size ## _le(a, b, &env->sse_status) ? -1 : 0)
1036fcf5ef2aSThomas Huth #define FPU_CMPUNORD(size, a, b)                                        \
1037fcf5ef2aSThomas Huth     (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? -1 : 0)
1038fcf5ef2aSThomas Huth #define FPU_CMPNEQ(size, a, b)                                          \
1039fcf5ef2aSThomas Huth     (float ## size ## _eq_quiet(a, b, &env->sse_status) ? 0 : -1)
1040fcf5ef2aSThomas Huth #define FPU_CMPNLT(size, a, b)                                          \
1041fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1)
1042fcf5ef2aSThomas Huth #define FPU_CMPNLE(size, a, b)                                          \
1043fcf5ef2aSThomas Huth     (float ## size ## _le(a, b, &env->sse_status) ? 0 : -1)
1044fcf5ef2aSThomas Huth #define FPU_CMPORD(size, a, b)                                          \
1045fcf5ef2aSThomas Huth     (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? 0 : -1)
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpeq, FPU_CMPEQ)
1048fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmplt, FPU_CMPLT)
1049fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmple, FPU_CMPLE)
1050fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpunord, FPU_CMPUNORD)
1051fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpneq, FPU_CMPNEQ)
1052fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnlt, FPU_CMPNLT)
1053fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnle, FPU_CMPNLE)
1054fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpord, FPU_CMPORD)
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1057fcf5ef2aSThomas Huth 
1058fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1059fcf5ef2aSThomas Huth {
106071bfd65cSRichard Henderson     FloatRelation ret;
1061fcf5ef2aSThomas Huth     float32 s0, s1;
1062fcf5ef2aSThomas Huth 
1063fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1064fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1065fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1066fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1067fcf5ef2aSThomas Huth }
1068fcf5ef2aSThomas Huth 
1069fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1070fcf5ef2aSThomas Huth {
107171bfd65cSRichard Henderson     FloatRelation ret;
1072fcf5ef2aSThomas Huth     float32 s0, s1;
1073fcf5ef2aSThomas Huth 
1074fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1075fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1076fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1077fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1078fcf5ef2aSThomas Huth }
1079fcf5ef2aSThomas Huth 
1080fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1081fcf5ef2aSThomas Huth {
108271bfd65cSRichard Henderson     FloatRelation ret;
1083fcf5ef2aSThomas Huth     float64 d0, d1;
1084fcf5ef2aSThomas Huth 
1085fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1086fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1087fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1088fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1089fcf5ef2aSThomas Huth }
1090fcf5ef2aSThomas Huth 
1091fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1092fcf5ef2aSThomas Huth {
109371bfd65cSRichard Henderson     FloatRelation ret;
1094fcf5ef2aSThomas Huth     float64 d0, d1;
1095fcf5ef2aSThomas Huth 
1096fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1097fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1098fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1099fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1100fcf5ef2aSThomas Huth }
1101fcf5ef2aSThomas Huth 
1102ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s)
1103fcf5ef2aSThomas Huth {
1104fcf5ef2aSThomas Huth     int b0, b1, b2, b3;
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth     b0 = s->ZMM_L(0) >> 31;
1107fcf5ef2aSThomas Huth     b1 = s->ZMM_L(1) >> 31;
1108fcf5ef2aSThomas Huth     b2 = s->ZMM_L(2) >> 31;
1109fcf5ef2aSThomas Huth     b3 = s->ZMM_L(3) >> 31;
1110fcf5ef2aSThomas Huth     return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3);
1111fcf5ef2aSThomas Huth }
1112fcf5ef2aSThomas Huth 
1113ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s)
1114fcf5ef2aSThomas Huth {
1115fcf5ef2aSThomas Huth     int b0, b1;
1116fcf5ef2aSThomas Huth 
1117fcf5ef2aSThomas Huth     b0 = s->ZMM_L(1) >> 31;
1118fcf5ef2aSThomas Huth     b1 = s->ZMM_L(3) >> 31;
1119fcf5ef2aSThomas Huth     return b0 | (b1 << 1);
1120fcf5ef2aSThomas Huth }
1121fcf5ef2aSThomas Huth 
1122fcf5ef2aSThomas Huth #endif
1123fcf5ef2aSThomas Huth 
1124fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1125fcf5ef2aSThomas Huth {
1126fcf5ef2aSThomas Huth     uint32_t val;
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth     val = 0;
1129fcf5ef2aSThomas Huth     val |= (s->B(0) >> 7);
1130fcf5ef2aSThomas Huth     val |= (s->B(1) >> 6) & 0x02;
1131fcf5ef2aSThomas Huth     val |= (s->B(2) >> 5) & 0x04;
1132fcf5ef2aSThomas Huth     val |= (s->B(3) >> 4) & 0x08;
1133fcf5ef2aSThomas Huth     val |= (s->B(4) >> 3) & 0x10;
1134fcf5ef2aSThomas Huth     val |= (s->B(5) >> 2) & 0x20;
1135fcf5ef2aSThomas Huth     val |= (s->B(6) >> 1) & 0x40;
1136fcf5ef2aSThomas Huth     val |= (s->B(7)) & 0x80;
1137fcf5ef2aSThomas Huth #if SHIFT == 1
1138fcf5ef2aSThomas Huth     val |= (s->B(8) << 1) & 0x0100;
1139fcf5ef2aSThomas Huth     val |= (s->B(9) << 2) & 0x0200;
1140fcf5ef2aSThomas Huth     val |= (s->B(10) << 3) & 0x0400;
1141fcf5ef2aSThomas Huth     val |= (s->B(11) << 4) & 0x0800;
1142fcf5ef2aSThomas Huth     val |= (s->B(12) << 5) & 0x1000;
1143fcf5ef2aSThomas Huth     val |= (s->B(13) << 6) & 0x2000;
1144fcf5ef2aSThomas Huth     val |= (s->B(14) << 7) & 0x4000;
1145fcf5ef2aSThomas Huth     val |= (s->B(15) << 8) & 0x8000;
1146fcf5ef2aSThomas Huth #endif
1147fcf5ef2aSThomas Huth     return val;
1148fcf5ef2aSThomas Huth }
1149fcf5ef2aSThomas Huth 
1150fcf5ef2aSThomas Huth void glue(helper_packsswb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1151fcf5ef2aSThomas Huth {
1152fcf5ef2aSThomas Huth     Reg r;
1153fcf5ef2aSThomas Huth 
1154fcf5ef2aSThomas Huth     r.B(0) = satsb((int16_t)d->W(0));
1155fcf5ef2aSThomas Huth     r.B(1) = satsb((int16_t)d->W(1));
1156fcf5ef2aSThomas Huth     r.B(2) = satsb((int16_t)d->W(2));
1157fcf5ef2aSThomas Huth     r.B(3) = satsb((int16_t)d->W(3));
1158fcf5ef2aSThomas Huth #if SHIFT == 1
1159fcf5ef2aSThomas Huth     r.B(4) = satsb((int16_t)d->W(4));
1160fcf5ef2aSThomas Huth     r.B(5) = satsb((int16_t)d->W(5));
1161fcf5ef2aSThomas Huth     r.B(6) = satsb((int16_t)d->W(6));
1162fcf5ef2aSThomas Huth     r.B(7) = satsb((int16_t)d->W(7));
1163fcf5ef2aSThomas Huth #endif
1164fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 0) = satsb((int16_t)s->W(0));
1165fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 1) = satsb((int16_t)s->W(1));
1166fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 2) = satsb((int16_t)s->W(2));
1167fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 3) = satsb((int16_t)s->W(3));
1168fcf5ef2aSThomas Huth #if SHIFT == 1
1169fcf5ef2aSThomas Huth     r.B(12) = satsb((int16_t)s->W(4));
1170fcf5ef2aSThomas Huth     r.B(13) = satsb((int16_t)s->W(5));
1171fcf5ef2aSThomas Huth     r.B(14) = satsb((int16_t)s->W(6));
1172fcf5ef2aSThomas Huth     r.B(15) = satsb((int16_t)s->W(7));
1173fcf5ef2aSThomas Huth #endif
1174d22697ddSPaolo Bonzini     MOVE(*d, r);
1175fcf5ef2aSThomas Huth }
1176fcf5ef2aSThomas Huth 
1177fcf5ef2aSThomas Huth void glue(helper_packuswb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1178fcf5ef2aSThomas Huth {
1179fcf5ef2aSThomas Huth     Reg r;
1180fcf5ef2aSThomas Huth 
1181fcf5ef2aSThomas Huth     r.B(0) = satub((int16_t)d->W(0));
1182fcf5ef2aSThomas Huth     r.B(1) = satub((int16_t)d->W(1));
1183fcf5ef2aSThomas Huth     r.B(2) = satub((int16_t)d->W(2));
1184fcf5ef2aSThomas Huth     r.B(3) = satub((int16_t)d->W(3));
1185fcf5ef2aSThomas Huth #if SHIFT == 1
1186fcf5ef2aSThomas Huth     r.B(4) = satub((int16_t)d->W(4));
1187fcf5ef2aSThomas Huth     r.B(5) = satub((int16_t)d->W(5));
1188fcf5ef2aSThomas Huth     r.B(6) = satub((int16_t)d->W(6));
1189fcf5ef2aSThomas Huth     r.B(7) = satub((int16_t)d->W(7));
1190fcf5ef2aSThomas Huth #endif
1191fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 0) = satub((int16_t)s->W(0));
1192fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 1) = satub((int16_t)s->W(1));
1193fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 2) = satub((int16_t)s->W(2));
1194fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 3) = satub((int16_t)s->W(3));
1195fcf5ef2aSThomas Huth #if SHIFT == 1
1196fcf5ef2aSThomas Huth     r.B(12) = satub((int16_t)s->W(4));
1197fcf5ef2aSThomas Huth     r.B(13) = satub((int16_t)s->W(5));
1198fcf5ef2aSThomas Huth     r.B(14) = satub((int16_t)s->W(6));
1199fcf5ef2aSThomas Huth     r.B(15) = satub((int16_t)s->W(7));
1200fcf5ef2aSThomas Huth #endif
1201d22697ddSPaolo Bonzini     MOVE(*d, r);
1202fcf5ef2aSThomas Huth }
1203fcf5ef2aSThomas Huth 
1204fcf5ef2aSThomas Huth void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1205fcf5ef2aSThomas Huth {
1206fcf5ef2aSThomas Huth     Reg r;
1207fcf5ef2aSThomas Huth 
1208fcf5ef2aSThomas Huth     r.W(0) = satsw(d->L(0));
1209fcf5ef2aSThomas Huth     r.W(1) = satsw(d->L(1));
1210fcf5ef2aSThomas Huth #if SHIFT == 1
1211fcf5ef2aSThomas Huth     r.W(2) = satsw(d->L(2));
1212fcf5ef2aSThomas Huth     r.W(3) = satsw(d->L(3));
1213fcf5ef2aSThomas Huth #endif
1214fcf5ef2aSThomas Huth     r.W((2 << SHIFT) + 0) = satsw(s->L(0));
1215fcf5ef2aSThomas Huth     r.W((2 << SHIFT) + 1) = satsw(s->L(1));
1216fcf5ef2aSThomas Huth #if SHIFT == 1
1217fcf5ef2aSThomas Huth     r.W(6) = satsw(s->L(2));
1218fcf5ef2aSThomas Huth     r.W(7) = satsw(s->L(3));
1219fcf5ef2aSThomas Huth #endif
1220d22697ddSPaolo Bonzini     MOVE(*d, r);
1221fcf5ef2aSThomas Huth }
1222fcf5ef2aSThomas Huth 
1223fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1224fcf5ef2aSThomas Huth                                                                         \
1225fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1226fcf5ef2aSThomas Huth                                                         Reg *d, Reg *s) \
1227fcf5ef2aSThomas Huth     {                                                                   \
1228fcf5ef2aSThomas Huth         Reg r;                                                          \
1229fcf5ef2aSThomas Huth                                                                         \
1230fcf5ef2aSThomas Huth         r.B(0) = d->B((base << (SHIFT + 2)) + 0);                       \
1231fcf5ef2aSThomas Huth         r.B(1) = s->B((base << (SHIFT + 2)) + 0);                       \
1232fcf5ef2aSThomas Huth         r.B(2) = d->B((base << (SHIFT + 2)) + 1);                       \
1233fcf5ef2aSThomas Huth         r.B(3) = s->B((base << (SHIFT + 2)) + 1);                       \
1234fcf5ef2aSThomas Huth         r.B(4) = d->B((base << (SHIFT + 2)) + 2);                       \
1235fcf5ef2aSThomas Huth         r.B(5) = s->B((base << (SHIFT + 2)) + 2);                       \
1236fcf5ef2aSThomas Huth         r.B(6) = d->B((base << (SHIFT + 2)) + 3);                       \
1237fcf5ef2aSThomas Huth         r.B(7) = s->B((base << (SHIFT + 2)) + 3);                       \
1238fcf5ef2aSThomas Huth         XMM_ONLY(                                                       \
1239fcf5ef2aSThomas Huth                  r.B(8) = d->B((base << (SHIFT + 2)) + 4);              \
1240fcf5ef2aSThomas Huth                  r.B(9) = s->B((base << (SHIFT + 2)) + 4);              \
1241fcf5ef2aSThomas Huth                  r.B(10) = d->B((base << (SHIFT + 2)) + 5);             \
1242fcf5ef2aSThomas Huth                  r.B(11) = s->B((base << (SHIFT + 2)) + 5);             \
1243fcf5ef2aSThomas Huth                  r.B(12) = d->B((base << (SHIFT + 2)) + 6);             \
1244fcf5ef2aSThomas Huth                  r.B(13) = s->B((base << (SHIFT + 2)) + 6);             \
1245fcf5ef2aSThomas Huth                  r.B(14) = d->B((base << (SHIFT + 2)) + 7);             \
1246fcf5ef2aSThomas Huth                  r.B(15) = s->B((base << (SHIFT + 2)) + 7);             \
1247fcf5ef2aSThomas Huth                                                                       ) \
1248d22697ddSPaolo Bonzini         MOVE(*d, r);                                                    \
1249fcf5ef2aSThomas Huth     }                                                                   \
1250fcf5ef2aSThomas Huth                                                                         \
1251fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1252fcf5ef2aSThomas Huth                                                         Reg *d, Reg *s) \
1253fcf5ef2aSThomas Huth     {                                                                   \
1254fcf5ef2aSThomas Huth         Reg r;                                                          \
1255fcf5ef2aSThomas Huth                                                                         \
1256fcf5ef2aSThomas Huth         r.W(0) = d->W((base << (SHIFT + 1)) + 0);                       \
1257fcf5ef2aSThomas Huth         r.W(1) = s->W((base << (SHIFT + 1)) + 0);                       \
1258fcf5ef2aSThomas Huth         r.W(2) = d->W((base << (SHIFT + 1)) + 1);                       \
1259fcf5ef2aSThomas Huth         r.W(3) = s->W((base << (SHIFT + 1)) + 1);                       \
1260fcf5ef2aSThomas Huth         XMM_ONLY(                                                       \
1261fcf5ef2aSThomas Huth                  r.W(4) = d->W((base << (SHIFT + 1)) + 2);              \
1262fcf5ef2aSThomas Huth                  r.W(5) = s->W((base << (SHIFT + 1)) + 2);              \
1263fcf5ef2aSThomas Huth                  r.W(6) = d->W((base << (SHIFT + 1)) + 3);              \
1264fcf5ef2aSThomas Huth                  r.W(7) = s->W((base << (SHIFT + 1)) + 3);              \
1265fcf5ef2aSThomas Huth                                                                       ) \
1266d22697ddSPaolo Bonzini             MOVE(*d, r);                                                \
1267fcf5ef2aSThomas Huth     }                                                                   \
1268fcf5ef2aSThomas Huth                                                                         \
1269fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1270fcf5ef2aSThomas Huth                                                         Reg *d, Reg *s) \
1271fcf5ef2aSThomas Huth     {                                                                   \
1272fcf5ef2aSThomas Huth         Reg r;                                                          \
1273fcf5ef2aSThomas Huth                                                                         \
1274fcf5ef2aSThomas Huth         r.L(0) = d->L((base << SHIFT) + 0);                             \
1275fcf5ef2aSThomas Huth         r.L(1) = s->L((base << SHIFT) + 0);                             \
1276fcf5ef2aSThomas Huth         XMM_ONLY(                                                       \
1277fcf5ef2aSThomas Huth                  r.L(2) = d->L((base << SHIFT) + 1);                    \
1278fcf5ef2aSThomas Huth                  r.L(3) = s->L((base << SHIFT) + 1);                    \
1279fcf5ef2aSThomas Huth                                                                       ) \
1280d22697ddSPaolo Bonzini             MOVE(*d, r);                                                \
1281fcf5ef2aSThomas Huth     }                                                                   \
1282fcf5ef2aSThomas Huth                                                                         \
1283fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1284fcf5ef2aSThomas Huth              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(CPUX86State \
1285fcf5ef2aSThomas Huth                                                                   *env, \
1286fcf5ef2aSThomas Huth                                                                   Reg *d, \
1287fcf5ef2aSThomas Huth                                                                   Reg *s) \
1288fcf5ef2aSThomas Huth              {                                                          \
1289fcf5ef2aSThomas Huth                  Reg r;                                                 \
1290fcf5ef2aSThomas Huth                                                                         \
1291fcf5ef2aSThomas Huth                  r.Q(0) = d->Q(base);                                   \
1292fcf5ef2aSThomas Huth                  r.Q(1) = s->Q(base);                                   \
1293d22697ddSPaolo Bonzini                  MOVE(*d, r);                                           \
1294fcf5ef2aSThomas Huth              }                                                          \
1295fcf5ef2aSThomas Huth                                                                         )
1296fcf5ef2aSThomas Huth 
1297fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1298fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1299fcf5ef2aSThomas Huth 
1300fcf5ef2aSThomas Huth /* 3DNow! float ops */
1301fcf5ef2aSThomas Huth #if SHIFT == 0
1302fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1303fcf5ef2aSThomas Huth {
1304fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1305fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1306fcf5ef2aSThomas Huth }
1307fcf5ef2aSThomas Huth 
1308fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1309fcf5ef2aSThomas Huth {
1310fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1311fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1312fcf5ef2aSThomas Huth }
1313fcf5ef2aSThomas Huth 
1314fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1315fcf5ef2aSThomas Huth {
1316fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1317fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1318fcf5ef2aSThomas Huth }
1319fcf5ef2aSThomas Huth 
1320fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1321fcf5ef2aSThomas Huth {
1322fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1323fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1324fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1325fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1326fcf5ef2aSThomas Huth }
1327fcf5ef2aSThomas Huth 
1328fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1329fcf5ef2aSThomas Huth {
1330*25bdec79SPaolo Bonzini     float32 r;
1331fcf5ef2aSThomas Huth 
1332*25bdec79SPaolo Bonzini     r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1333*25bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1334*25bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1335fcf5ef2aSThomas Huth }
1336fcf5ef2aSThomas Huth 
1337fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1338fcf5ef2aSThomas Huth {
1339fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1340fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1341fcf5ef2aSThomas Huth }
1342fcf5ef2aSThomas Huth 
1343fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1344fcf5ef2aSThomas Huth {
1345fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1346fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1347fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1348fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1349fcf5ef2aSThomas Huth }
1350fcf5ef2aSThomas Huth 
1351fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1352fcf5ef2aSThomas Huth {
1353fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1354fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1355fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1356fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1357fcf5ef2aSThomas Huth }
1358fcf5ef2aSThomas Huth 
1359fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1360fcf5ef2aSThomas Huth {
1361fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1362fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1363fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1364fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1365fcf5ef2aSThomas Huth }
1366fcf5ef2aSThomas Huth 
1367fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1368fcf5ef2aSThomas Huth {
1369fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1370fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1371fcf5ef2aSThomas Huth     }
1372fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1373fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1374fcf5ef2aSThomas Huth     }
1375fcf5ef2aSThomas Huth }
1376fcf5ef2aSThomas Huth 
1377fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1378fcf5ef2aSThomas Huth {
1379fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1380fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1381fcf5ef2aSThomas Huth     }
1382fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1383fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1384fcf5ef2aSThomas Huth     }
1385fcf5ef2aSThomas Huth }
1386fcf5ef2aSThomas Huth 
1387fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1388fcf5ef2aSThomas Huth {
1389fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1390fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1391fcf5ef2aSThomas Huth }
1392fcf5ef2aSThomas Huth 
1393fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1394fcf5ef2aSThomas Huth {
1395*25bdec79SPaolo Bonzini     float32 r;
1396fcf5ef2aSThomas Huth 
1397*25bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1398*25bdec79SPaolo Bonzini     d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1399*25bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1400fcf5ef2aSThomas Huth }
1401fcf5ef2aSThomas Huth 
1402fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1403fcf5ef2aSThomas Huth {
1404*25bdec79SPaolo Bonzini     float32 r;
1405fcf5ef2aSThomas Huth 
1406*25bdec79SPaolo Bonzini     r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1407*25bdec79SPaolo Bonzini     d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1408*25bdec79SPaolo Bonzini     d->MMX_S(0) = r;
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1412fcf5ef2aSThomas Huth {
1413fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1414fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1415fcf5ef2aSThomas Huth }
1416fcf5ef2aSThomas Huth 
1417fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1418fcf5ef2aSThomas Huth {
1419fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1420fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1421fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1422fcf5ef2aSThomas Huth                               &env->mmx_status);
1423fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1424fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1425fcf5ef2aSThomas Huth }
1426fcf5ef2aSThomas Huth 
1427fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1428fcf5ef2aSThomas Huth {
1429fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1430fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1431fcf5ef2aSThomas Huth }
1432fcf5ef2aSThomas Huth 
1433fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1434fcf5ef2aSThomas Huth {
1435fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1436fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1437fcf5ef2aSThomas Huth }
1438fcf5ef2aSThomas Huth 
1439fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1440fcf5ef2aSThomas Huth {
1441*25bdec79SPaolo Bonzini     uint32_t r;
1442fcf5ef2aSThomas Huth 
1443*25bdec79SPaolo Bonzini     r = s->MMX_L(0);
1444*25bdec79SPaolo Bonzini     d->MMX_L(0) = s->MMX_L(1);
1445*25bdec79SPaolo Bonzini     d->MMX_L(1) = r;
1446fcf5ef2aSThomas Huth }
1447fcf5ef2aSThomas Huth #endif
1448fcf5ef2aSThomas Huth 
1449fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1450fcf5ef2aSThomas Huth void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1451fcf5ef2aSThomas Huth {
1452fcf5ef2aSThomas Huth     int i;
1453fcf5ef2aSThomas Huth     Reg r;
1454fcf5ef2aSThomas Huth 
1455fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
1456fcf5ef2aSThomas Huth         r.B(i) = (s->B(i) & 0x80) ? 0 : (d->B(s->B(i) & ((8 << SHIFT) - 1)));
1457fcf5ef2aSThomas Huth     }
1458fcf5ef2aSThomas Huth 
1459d22697ddSPaolo Bonzini     MOVE(*d, r);
1460fcf5ef2aSThomas Huth }
1461fcf5ef2aSThomas Huth 
1462fcf5ef2aSThomas Huth void glue(helper_phaddw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1463fcf5ef2aSThomas Huth {
14642dfbea1aSJanne Grunau 
14652dfbea1aSJanne Grunau     Reg r;
14662dfbea1aSJanne Grunau 
14672dfbea1aSJanne Grunau     r.W(0) = (int16_t)d->W(0) + (int16_t)d->W(1);
14682dfbea1aSJanne Grunau     r.W(1) = (int16_t)d->W(2) + (int16_t)d->W(3);
14692dfbea1aSJanne Grunau     XMM_ONLY(r.W(2) = (int16_t)d->W(4) + (int16_t)d->W(5));
14702dfbea1aSJanne Grunau     XMM_ONLY(r.W(3) = (int16_t)d->W(6) + (int16_t)d->W(7));
14712dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 0) = (int16_t)s->W(0) + (int16_t)s->W(1);
14722dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 1) = (int16_t)s->W(2) + (int16_t)s->W(3);
14732dfbea1aSJanne Grunau     XMM_ONLY(r.W(6) = (int16_t)s->W(4) + (int16_t)s->W(5));
14742dfbea1aSJanne Grunau     XMM_ONLY(r.W(7) = (int16_t)s->W(6) + (int16_t)s->W(7));
14752dfbea1aSJanne Grunau 
1476d22697ddSPaolo Bonzini     MOVE(*d, r);
1477fcf5ef2aSThomas Huth }
1478fcf5ef2aSThomas Huth 
1479fcf5ef2aSThomas Huth void glue(helper_phaddd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1480fcf5ef2aSThomas Huth {
14812dfbea1aSJanne Grunau     Reg r;
14822dfbea1aSJanne Grunau 
14832dfbea1aSJanne Grunau     r.L(0) = (int32_t)d->L(0) + (int32_t)d->L(1);
14842dfbea1aSJanne Grunau     XMM_ONLY(r.L(1) = (int32_t)d->L(2) + (int32_t)d->L(3));
14852dfbea1aSJanne Grunau     r.L((1 << SHIFT) + 0) = (int32_t)s->L(0) + (int32_t)s->L(1);
14862dfbea1aSJanne Grunau     XMM_ONLY(r.L(3) = (int32_t)s->L(2) + (int32_t)s->L(3));
14872dfbea1aSJanne Grunau 
1488d22697ddSPaolo Bonzini     MOVE(*d, r);
1489fcf5ef2aSThomas Huth }
1490fcf5ef2aSThomas Huth 
1491fcf5ef2aSThomas Huth void glue(helper_phaddsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1492fcf5ef2aSThomas Huth {
14932dfbea1aSJanne Grunau     Reg r;
14942dfbea1aSJanne Grunau 
14952dfbea1aSJanne Grunau     r.W(0) = satsw((int16_t)d->W(0) + (int16_t)d->W(1));
14962dfbea1aSJanne Grunau     r.W(1) = satsw((int16_t)d->W(2) + (int16_t)d->W(3));
14972dfbea1aSJanne Grunau     XMM_ONLY(r.W(2) = satsw((int16_t)d->W(4) + (int16_t)d->W(5)));
14982dfbea1aSJanne Grunau     XMM_ONLY(r.W(3) = satsw((int16_t)d->W(6) + (int16_t)d->W(7)));
14992dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) + (int16_t)s->W(1));
15002dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) + (int16_t)s->W(3));
15012dfbea1aSJanne Grunau     XMM_ONLY(r.W(6) = satsw((int16_t)s->W(4) + (int16_t)s->W(5)));
15022dfbea1aSJanne Grunau     XMM_ONLY(r.W(7) = satsw((int16_t)s->W(6) + (int16_t)s->W(7)));
15032dfbea1aSJanne Grunau 
1504d22697ddSPaolo Bonzini     MOVE(*d, r);
1505fcf5ef2aSThomas Huth }
1506fcf5ef2aSThomas Huth 
1507fcf5ef2aSThomas Huth void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1508fcf5ef2aSThomas Huth {
1509fcf5ef2aSThomas Huth     d->W(0) = satsw((int8_t)s->B(0) * (uint8_t)d->B(0) +
1510fcf5ef2aSThomas Huth                     (int8_t)s->B(1) * (uint8_t)d->B(1));
1511fcf5ef2aSThomas Huth     d->W(1) = satsw((int8_t)s->B(2) * (uint8_t)d->B(2) +
1512fcf5ef2aSThomas Huth                     (int8_t)s->B(3) * (uint8_t)d->B(3));
1513fcf5ef2aSThomas Huth     d->W(2) = satsw((int8_t)s->B(4) * (uint8_t)d->B(4) +
1514fcf5ef2aSThomas Huth                     (int8_t)s->B(5) * (uint8_t)d->B(5));
1515fcf5ef2aSThomas Huth     d->W(3) = satsw((int8_t)s->B(6) * (uint8_t)d->B(6) +
1516fcf5ef2aSThomas Huth                     (int8_t)s->B(7) * (uint8_t)d->B(7));
1517fcf5ef2aSThomas Huth #if SHIFT == 1
1518fcf5ef2aSThomas Huth     d->W(4) = satsw((int8_t)s->B(8) * (uint8_t)d->B(8) +
1519fcf5ef2aSThomas Huth                     (int8_t)s->B(9) * (uint8_t)d->B(9));
1520fcf5ef2aSThomas Huth     d->W(5) = satsw((int8_t)s->B(10) * (uint8_t)d->B(10) +
1521fcf5ef2aSThomas Huth                     (int8_t)s->B(11) * (uint8_t)d->B(11));
1522fcf5ef2aSThomas Huth     d->W(6) = satsw((int8_t)s->B(12) * (uint8_t)d->B(12) +
1523fcf5ef2aSThomas Huth                     (int8_t)s->B(13) * (uint8_t)d->B(13));
1524fcf5ef2aSThomas Huth     d->W(7) = satsw((int8_t)s->B(14) * (uint8_t)d->B(14) +
1525fcf5ef2aSThomas Huth                     (int8_t)s->B(15) * (uint8_t)d->B(15));
1526fcf5ef2aSThomas Huth #endif
1527fcf5ef2aSThomas Huth }
1528fcf5ef2aSThomas Huth 
1529fcf5ef2aSThomas Huth void glue(helper_phsubw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1530fcf5ef2aSThomas Huth {
153175046ad7SPaolo Bonzini     Reg r;
153275046ad7SPaolo Bonzini 
153375046ad7SPaolo Bonzini     r.W(0) = (int16_t)d->W(0) - (int16_t)d->W(1);
153475046ad7SPaolo Bonzini     r.W(1) = (int16_t)d->W(2) - (int16_t)d->W(3);
153575046ad7SPaolo Bonzini     XMM_ONLY(r.W(2) = (int16_t)d->W(4) - (int16_t)d->W(5));
153675046ad7SPaolo Bonzini     XMM_ONLY(r.W(3) = (int16_t)d->W(6) - (int16_t)d->W(7));
153775046ad7SPaolo Bonzini     r.W((2 << SHIFT) + 0) = (int16_t)s->W(0) - (int16_t)s->W(1);
153875046ad7SPaolo Bonzini     r.W((2 << SHIFT) + 1) = (int16_t)s->W(2) - (int16_t)s->W(3);
153975046ad7SPaolo Bonzini     XMM_ONLY(r.W(6) = (int16_t)s->W(4) - (int16_t)s->W(5));
154075046ad7SPaolo Bonzini     XMM_ONLY(r.W(7) = (int16_t)s->W(6) - (int16_t)s->W(7));
154175046ad7SPaolo Bonzini     MOVE(*d, r);
1542fcf5ef2aSThomas Huth }
1543fcf5ef2aSThomas Huth 
1544fcf5ef2aSThomas Huth void glue(helper_phsubd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1545fcf5ef2aSThomas Huth {
154675046ad7SPaolo Bonzini     Reg r;
154775046ad7SPaolo Bonzini 
154875046ad7SPaolo Bonzini     r.L(0) = (int32_t)d->L(0) - (int32_t)d->L(1);
154975046ad7SPaolo Bonzini     XMM_ONLY(r.L(1) = (int32_t)d->L(2) - (int32_t)d->L(3));
155075046ad7SPaolo Bonzini     r.L((1 << SHIFT) + 0) = (int32_t)s->L(0) - (int32_t)s->L(1);
155175046ad7SPaolo Bonzini     XMM_ONLY(r.L(3) = (int32_t)s->L(2) - (int32_t)s->L(3));
155275046ad7SPaolo Bonzini     MOVE(*d, r);
1553fcf5ef2aSThomas Huth }
1554fcf5ef2aSThomas Huth 
1555fcf5ef2aSThomas Huth void glue(helper_phsubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1556fcf5ef2aSThomas Huth {
155775046ad7SPaolo Bonzini     Reg r;
155875046ad7SPaolo Bonzini 
155975046ad7SPaolo Bonzini     r.W(0) = satsw((int16_t)d->W(0) - (int16_t)d->W(1));
156075046ad7SPaolo Bonzini     r.W(1) = satsw((int16_t)d->W(2) - (int16_t)d->W(3));
156175046ad7SPaolo Bonzini     XMM_ONLY(r.W(2) = satsw((int16_t)d->W(4) - (int16_t)d->W(5)));
156275046ad7SPaolo Bonzini     XMM_ONLY(r.W(3) = satsw((int16_t)d->W(6) - (int16_t)d->W(7)));
156375046ad7SPaolo Bonzini     r.W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) - (int16_t)s->W(1));
156475046ad7SPaolo Bonzini     r.W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) - (int16_t)s->W(3));
156575046ad7SPaolo Bonzini     XMM_ONLY(r.W(6) = satsw((int16_t)s->W(4) - (int16_t)s->W(5)));
156675046ad7SPaolo Bonzini     XMM_ONLY(r.W(7) = satsw((int16_t)s->W(6) - (int16_t)s->W(7)));
156775046ad7SPaolo Bonzini     MOVE(*d, r);
1568fcf5ef2aSThomas Huth }
1569fcf5ef2aSThomas Huth 
1570fcf5ef2aSThomas Huth #define FABSB(_, x) (x > INT8_MAX  ? -(int8_t)x : x)
1571fcf5ef2aSThomas Huth #define FABSW(_, x) (x > INT16_MAX ? -(int16_t)x : x)
1572fcf5ef2aSThomas Huth #define FABSL(_, x) (x > INT32_MAX ? -(int32_t)x : x)
1573fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pabsb, FABSB)
1574fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pabsw, FABSW)
1575fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pabsd, FABSL)
1576fcf5ef2aSThomas Huth 
1577fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1578fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1579fcf5ef2aSThomas Huth 
1580fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1581fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1582fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1583fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1584fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1585fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1586fcf5ef2aSThomas Huth 
1587fcf5ef2aSThomas Huth void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1588fcf5ef2aSThomas Huth                                   int32_t shift)
1589fcf5ef2aSThomas Huth {
1590fcf5ef2aSThomas Huth     Reg r;
1591fcf5ef2aSThomas Huth 
1592fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1593fcf5ef2aSThomas Huth     if (shift >= (16 << SHIFT)) {
1594fcf5ef2aSThomas Huth         r.Q(0) = 0;
1595fcf5ef2aSThomas Huth         XMM_ONLY(r.Q(1) = 0);
1596fcf5ef2aSThomas Huth     } else {
1597fcf5ef2aSThomas Huth         shift <<= 3;
1598fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1599fcf5ef2aSThomas Huth #if SHIFT == 0
1600fcf5ef2aSThomas Huth         r.Q(0) = SHR(s->Q(0), shift - 0) |
1601fcf5ef2aSThomas Huth             SHR(d->Q(0), shift -  64);
1602fcf5ef2aSThomas Huth #else
1603fcf5ef2aSThomas Huth         r.Q(0) = SHR(s->Q(0), shift - 0) |
1604fcf5ef2aSThomas Huth             SHR(s->Q(1), shift -  64) |
1605fcf5ef2aSThomas Huth             SHR(d->Q(0), shift - 128) |
1606fcf5ef2aSThomas Huth             SHR(d->Q(1), shift - 192);
1607fcf5ef2aSThomas Huth         r.Q(1) = SHR(s->Q(0), shift + 64) |
1608fcf5ef2aSThomas Huth             SHR(s->Q(1), shift -   0) |
1609fcf5ef2aSThomas Huth             SHR(d->Q(0), shift -  64) |
1610fcf5ef2aSThomas Huth             SHR(d->Q(1), shift - 128);
1611fcf5ef2aSThomas Huth #endif
1612fcf5ef2aSThomas Huth #undef SHR
1613fcf5ef2aSThomas Huth     }
1614fcf5ef2aSThomas Huth 
1615d22697ddSPaolo Bonzini     MOVE(*d, r);
1616fcf5ef2aSThomas Huth }
1617fcf5ef2aSThomas Huth 
1618fcf5ef2aSThomas Huth #define XMM0 (env->xmm_regs[0])
1619fcf5ef2aSThomas Huth 
1620fcf5ef2aSThomas Huth #if SHIFT == 1
1621fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1622fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)           \
1623fcf5ef2aSThomas Huth     {                                                                   \
1624fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0));           \
1625fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1));           \
1626fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1627fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2));       \
1628fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3));       \
1629fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1630fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4));   \
1631fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5));   \
1632fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6));   \
1633fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7));   \
1634fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1635fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \
1636fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \
1637fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \
1638fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \
1639fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \
1640fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \
1641fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \
1642fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \
1643fcf5ef2aSThomas Huth                 }                                                       \
1644fcf5ef2aSThomas Huth             }                                                           \
1645fcf5ef2aSThomas Huth         }                                                               \
1646fcf5ef2aSThomas Huth     }
1647fcf5ef2aSThomas Huth 
1648fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1649fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \
1650fcf5ef2aSThomas Huth     {                                                                   \
1651fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1));       \
1652fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1));       \
1653fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1654fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1));   \
1655fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1));   \
1656fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1657fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \
1658fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \
1659fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \
1660fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \
1661fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1662fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \
1663fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \
1664fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10),           \
1665fcf5ef2aSThomas Huth                                     ((imm >> 10) & 1));                 \
1666fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11),           \
1667fcf5ef2aSThomas Huth                                     ((imm >> 11) & 1));                 \
1668fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12),           \
1669fcf5ef2aSThomas Huth                                     ((imm >> 12) & 1));                 \
1670fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13),           \
1671fcf5ef2aSThomas Huth                                     ((imm >> 13) & 1));                 \
1672fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14),           \
1673fcf5ef2aSThomas Huth                                     ((imm >> 14) & 1));                 \
1674fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15),           \
1675fcf5ef2aSThomas Huth                                     ((imm >> 15) & 1));                 \
1676fcf5ef2aSThomas Huth                 }                                                       \
1677fcf5ef2aSThomas Huth             }                                                           \
1678fcf5ef2aSThomas Huth         }                                                               \
1679fcf5ef2aSThomas Huth     }
1680fcf5ef2aSThomas Huth 
1681fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
1682fcf5ef2aSThomas Huth #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d)
1683fcf5ef2aSThomas Huth #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d)
1684fcf5ef2aSThomas Huth #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d)
1685fcf5ef2aSThomas Huth SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB)
1686fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS)
1687fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD)
1688fcf5ef2aSThomas Huth 
1689fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1690fcf5ef2aSThomas Huth {
1691fcf5ef2aSThomas Huth     uint64_t zf = (s->Q(0) &  d->Q(0)) | (s->Q(1) &  d->Q(1));
1692fcf5ef2aSThomas Huth     uint64_t cf = (s->Q(0) & ~d->Q(0)) | (s->Q(1) & ~d->Q(1));
1693fcf5ef2aSThomas Huth 
1694fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1695fcf5ef2aSThomas Huth }
1696fcf5ef2aSThomas Huth 
1697fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)        \
1698fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)     \
1699fcf5ef2aSThomas Huth     {                                           \
1700fcf5ef2aSThomas Huth         if (num > 2) {                          \
1701fcf5ef2aSThomas Huth             if (num > 4) {                      \
1702fcf5ef2aSThomas Huth                 d->elem(7) = F(7);              \
1703c6a56c8eSJoseph Myers                 d->elem(6) = F(6);              \
1704c6a56c8eSJoseph Myers                 d->elem(5) = F(5);              \
1705c6a56c8eSJoseph Myers                 d->elem(4) = F(4);              \
1706fcf5ef2aSThomas Huth             }                                   \
1707c6a56c8eSJoseph Myers             d->elem(3) = F(3);                  \
1708c6a56c8eSJoseph Myers             d->elem(2) = F(2);                  \
1709fcf5ef2aSThomas Huth         }                                       \
1710c6a56c8eSJoseph Myers         d->elem(1) = F(1);                      \
1711c6a56c8eSJoseph Myers         d->elem(0) = F(0);                      \
1712fcf5ef2aSThomas Huth     }
1713fcf5ef2aSThomas Huth 
1714fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxbw, W, 8, (int8_t) s->B)
1715fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxbd, L, 4, (int8_t) s->B)
1716fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxbq, Q, 2, (int8_t) s->B)
1717fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxwd, L, 4, (int16_t) s->W)
1718fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxwq, Q, 2, (int16_t) s->W)
1719fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxdq, Q, 2, (int32_t) s->L)
1720fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxbw, W, 8, s->B)
1721fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxbd, L, 4, s->B)
1722fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxbq, Q, 2, s->B)
1723fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxwd, L, 4, s->W)
1724fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxwq, Q, 2, s->W)
1725fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxdq, Q, 2, s->L)
1726fcf5ef2aSThomas Huth 
1727fcf5ef2aSThomas Huth void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1728fcf5ef2aSThomas Huth {
1729fcf5ef2aSThomas Huth     d->Q(0) = (int64_t)(int32_t) d->L(0) * (int32_t) s->L(0);
1730fcf5ef2aSThomas Huth     d->Q(1) = (int64_t)(int32_t) d->L(2) * (int32_t) s->L(2);
1731fcf5ef2aSThomas Huth }
1732fcf5ef2aSThomas Huth 
1733fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1734fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1735fcf5ef2aSThomas Huth 
1736fcf5ef2aSThomas Huth void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1737fcf5ef2aSThomas Huth {
173880e19606SJoseph Myers     Reg r;
173980e19606SJoseph Myers 
174080e19606SJoseph Myers     r.W(0) = satuw((int32_t) d->L(0));
174180e19606SJoseph Myers     r.W(1) = satuw((int32_t) d->L(1));
174280e19606SJoseph Myers     r.W(2) = satuw((int32_t) d->L(2));
174380e19606SJoseph Myers     r.W(3) = satuw((int32_t) d->L(3));
174480e19606SJoseph Myers     r.W(4) = satuw((int32_t) s->L(0));
174580e19606SJoseph Myers     r.W(5) = satuw((int32_t) s->L(1));
174680e19606SJoseph Myers     r.W(6) = satuw((int32_t) s->L(2));
174780e19606SJoseph Myers     r.W(7) = satuw((int32_t) s->L(3));
1748d22697ddSPaolo Bonzini     MOVE(*d, r);
1749fcf5ef2aSThomas Huth }
1750fcf5ef2aSThomas Huth 
1751fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1752fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1753fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1754fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1755fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1756fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1757fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1758fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1759fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1760fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1761fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1762fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1763fcf5ef2aSThomas Huth 
1764fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1765fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1766fcf5ef2aSThomas Huth 
1767fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1768fcf5ef2aSThomas Huth {
1769fcf5ef2aSThomas Huth     int idx = 0;
1770fcf5ef2aSThomas Huth 
1771fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1772fcf5ef2aSThomas Huth         idx = 1;
1773fcf5ef2aSThomas Huth     }
1774fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1775fcf5ef2aSThomas Huth         idx = 2;
1776fcf5ef2aSThomas Huth     }
1777fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1778fcf5ef2aSThomas Huth         idx = 3;
1779fcf5ef2aSThomas Huth     }
1780fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1781fcf5ef2aSThomas Huth         idx = 4;
1782fcf5ef2aSThomas Huth     }
1783fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1784fcf5ef2aSThomas Huth         idx = 5;
1785fcf5ef2aSThomas Huth     }
1786fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1787fcf5ef2aSThomas Huth         idx = 6;
1788fcf5ef2aSThomas Huth     }
1789fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1790fcf5ef2aSThomas Huth         idx = 7;
1791fcf5ef2aSThomas Huth     }
1792fcf5ef2aSThomas Huth 
1793fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1794aa406feaSJoseph Myers     d->W(1) = idx;
1795aa406feaSJoseph Myers     d->L(1) = 0;
1796aa406feaSJoseph Myers     d->Q(1) = 0;
1797fcf5ef2aSThomas Huth }
1798fcf5ef2aSThomas Huth 
1799fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1800fcf5ef2aSThomas Huth                                   uint32_t mode)
1801fcf5ef2aSThomas Huth {
1802418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1803fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1804fcf5ef2aSThomas Huth 
1805fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1806fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1807fcf5ef2aSThomas Huth         switch (mode & 3) {
1808fcf5ef2aSThomas Huth         case 0:
1809fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1810fcf5ef2aSThomas Huth             break;
1811fcf5ef2aSThomas Huth         case 1:
1812fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1813fcf5ef2aSThomas Huth             break;
1814fcf5ef2aSThomas Huth         case 2:
1815fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1816fcf5ef2aSThomas Huth             break;
1817fcf5ef2aSThomas Huth         case 3:
1818fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1819fcf5ef2aSThomas Huth             break;
1820fcf5ef2aSThomas Huth         }
1821fcf5ef2aSThomas Huth     }
1822fcf5ef2aSThomas Huth 
1823fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1824fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_round_to_int(s->ZMM_S(1), &env->sse_status);
1825fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_round_to_int(s->ZMM_S(2), &env->sse_status);
1826fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_round_to_int(s->ZMM_S(3), &env->sse_status);
1827fcf5ef2aSThomas Huth 
1828418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1829fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1830fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1831fcf5ef2aSThomas Huth                                   &env->sse_status);
1832fcf5ef2aSThomas Huth     }
1833fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1834fcf5ef2aSThomas Huth }
1835fcf5ef2aSThomas Huth 
1836fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1837fcf5ef2aSThomas Huth                                   uint32_t mode)
1838fcf5ef2aSThomas Huth {
1839418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1840fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1841fcf5ef2aSThomas Huth 
1842fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1843fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1844fcf5ef2aSThomas Huth         switch (mode & 3) {
1845fcf5ef2aSThomas Huth         case 0:
1846fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1847fcf5ef2aSThomas Huth             break;
1848fcf5ef2aSThomas Huth         case 1:
1849fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1850fcf5ef2aSThomas Huth             break;
1851fcf5ef2aSThomas Huth         case 2:
1852fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1853fcf5ef2aSThomas Huth             break;
1854fcf5ef2aSThomas Huth         case 3:
1855fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1856fcf5ef2aSThomas Huth             break;
1857fcf5ef2aSThomas Huth         }
1858fcf5ef2aSThomas Huth     }
1859fcf5ef2aSThomas Huth 
1860fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1861fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_round_to_int(s->ZMM_D(1), &env->sse_status);
1862fcf5ef2aSThomas Huth 
1863418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1864fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1865fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1866fcf5ef2aSThomas Huth                                   &env->sse_status);
1867fcf5ef2aSThomas Huth     }
1868fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1869fcf5ef2aSThomas Huth }
1870fcf5ef2aSThomas Huth 
1871fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1872fcf5ef2aSThomas Huth                                   uint32_t mode)
1873fcf5ef2aSThomas Huth {
1874418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1875fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1876fcf5ef2aSThomas Huth 
1877fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1878fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1879fcf5ef2aSThomas Huth         switch (mode & 3) {
1880fcf5ef2aSThomas Huth         case 0:
1881fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1882fcf5ef2aSThomas Huth             break;
1883fcf5ef2aSThomas Huth         case 1:
1884fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1885fcf5ef2aSThomas Huth             break;
1886fcf5ef2aSThomas Huth         case 2:
1887fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1888fcf5ef2aSThomas Huth             break;
1889fcf5ef2aSThomas Huth         case 3:
1890fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1891fcf5ef2aSThomas Huth             break;
1892fcf5ef2aSThomas Huth         }
1893fcf5ef2aSThomas Huth     }
1894fcf5ef2aSThomas Huth 
1895fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1896fcf5ef2aSThomas Huth 
1897418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1898fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1899fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1900fcf5ef2aSThomas Huth                                   &env->sse_status);
1901fcf5ef2aSThomas Huth     }
1902fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1903fcf5ef2aSThomas Huth }
1904fcf5ef2aSThomas Huth 
1905fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1906fcf5ef2aSThomas Huth                                   uint32_t mode)
1907fcf5ef2aSThomas Huth {
1908418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1909fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1910fcf5ef2aSThomas Huth 
1911fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1912fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1913fcf5ef2aSThomas Huth         switch (mode & 3) {
1914fcf5ef2aSThomas Huth         case 0:
1915fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1916fcf5ef2aSThomas Huth             break;
1917fcf5ef2aSThomas Huth         case 1:
1918fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1919fcf5ef2aSThomas Huth             break;
1920fcf5ef2aSThomas Huth         case 2:
1921fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1922fcf5ef2aSThomas Huth             break;
1923fcf5ef2aSThomas Huth         case 3:
1924fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1925fcf5ef2aSThomas Huth             break;
1926fcf5ef2aSThomas Huth         }
1927fcf5ef2aSThomas Huth     }
1928fcf5ef2aSThomas Huth 
1929fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1930fcf5ef2aSThomas Huth 
1931418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1932fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1933fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1934fcf5ef2aSThomas Huth                                   &env->sse_status);
1935fcf5ef2aSThomas Huth     }
1936fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1937fcf5ef2aSThomas Huth }
1938fcf5ef2aSThomas Huth 
1939fcf5ef2aSThomas Huth #define FBLENDP(d, s, m) (m ? s : d)
1940fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendps, L, 4, FBLENDP)
1941fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP)
1942fcf5ef2aSThomas Huth SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)
1943fcf5ef2aSThomas Huth 
1944fcf5ef2aSThomas Huth void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1945fcf5ef2aSThomas Huth {
1946bf30ad8cSPaolo Bonzini     float32 prod1, prod2, temp2, temp3, temp4;
1947fcf5ef2aSThomas Huth 
1948bf30ad8cSPaolo Bonzini     /*
1949bf30ad8cSPaolo Bonzini      * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D
1950bf30ad8cSPaolo Bonzini      * to correctly round the intermediate results
1951bf30ad8cSPaolo Bonzini      */
1952fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1953bf30ad8cSPaolo Bonzini         prod1 = float32_mul(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status);
1954bf30ad8cSPaolo Bonzini     } else {
1955bf30ad8cSPaolo Bonzini         prod1 = float32_zero;
1956fcf5ef2aSThomas Huth     }
1957fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1958bf30ad8cSPaolo Bonzini         prod2 = float32_mul(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status);
1959bf30ad8cSPaolo Bonzini     } else {
1960bf30ad8cSPaolo Bonzini         prod2 = float32_zero;
1961fcf5ef2aSThomas Huth     }
1962bf30ad8cSPaolo Bonzini     temp2 = float32_add(prod1, prod2, &env->sse_status);
1963fcf5ef2aSThomas Huth     if (mask & (1 << 6)) {
1964bf30ad8cSPaolo Bonzini         prod1 = float32_mul(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status);
1965bf30ad8cSPaolo Bonzini     } else {
1966bf30ad8cSPaolo Bonzini         prod1 = float32_zero;
1967fcf5ef2aSThomas Huth     }
1968fcf5ef2aSThomas Huth     if (mask & (1 << 7)) {
1969bf30ad8cSPaolo Bonzini         prod2 = float32_mul(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status);
1970bf30ad8cSPaolo Bonzini     } else {
1971bf30ad8cSPaolo Bonzini         prod2 = float32_zero;
1972fcf5ef2aSThomas Huth     }
1973bf30ad8cSPaolo Bonzini     temp3 = float32_add(prod1, prod2, &env->sse_status);
1974bf30ad8cSPaolo Bonzini     temp4 = float32_add(temp2, temp3, &env->sse_status);
1975bf30ad8cSPaolo Bonzini 
1976bf30ad8cSPaolo Bonzini     d->ZMM_S(0) = (mask & (1 << 0)) ? temp4 : float32_zero;
1977bf30ad8cSPaolo Bonzini     d->ZMM_S(1) = (mask & (1 << 1)) ? temp4 : float32_zero;
1978bf30ad8cSPaolo Bonzini     d->ZMM_S(2) = (mask & (1 << 2)) ? temp4 : float32_zero;
1979bf30ad8cSPaolo Bonzini     d->ZMM_S(3) = (mask & (1 << 3)) ? temp4 : float32_zero;
1980fcf5ef2aSThomas Huth }
1981fcf5ef2aSThomas Huth 
1982fcf5ef2aSThomas Huth void glue(helper_dppd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1983fcf5ef2aSThomas Huth {
1984bf30ad8cSPaolo Bonzini     float64 prod1, prod2, temp2;
1985fcf5ef2aSThomas Huth 
1986fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1987bf30ad8cSPaolo Bonzini         prod1 = float64_mul(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
1988bf30ad8cSPaolo Bonzini     } else {
1989bf30ad8cSPaolo Bonzini         prod1 = float64_zero;
1990fcf5ef2aSThomas Huth     }
1991fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1992bf30ad8cSPaolo Bonzini         prod2 = float64_mul(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
1993bf30ad8cSPaolo Bonzini     } else {
1994bf30ad8cSPaolo Bonzini         prod2 = float64_zero;
1995fcf5ef2aSThomas Huth     }
1996bf30ad8cSPaolo Bonzini     temp2 = float64_add(prod1, prod2, &env->sse_status);
1997bf30ad8cSPaolo Bonzini     d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero;
1998bf30ad8cSPaolo Bonzini     d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero;
1999fcf5ef2aSThomas Huth }
2000fcf5ef2aSThomas Huth 
2001fcf5ef2aSThomas Huth void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2002fcf5ef2aSThomas Huth                                   uint32_t offset)
2003fcf5ef2aSThomas Huth {
2004fcf5ef2aSThomas Huth     int s0 = (offset & 3) << 2;
2005fcf5ef2aSThomas Huth     int d0 = (offset & 4) << 0;
2006fcf5ef2aSThomas Huth     int i;
2007fcf5ef2aSThomas Huth     Reg r;
2008fcf5ef2aSThomas Huth 
2009fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++, d0++) {
2010fcf5ef2aSThomas Huth         r.W(i) = 0;
2011fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 0) - s->B(s0 + 0));
2012fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 1) - s->B(s0 + 1));
2013fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 2) - s->B(s0 + 2));
2014fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 3) - s->B(s0 + 3));
2015fcf5ef2aSThomas Huth     }
2016fcf5ef2aSThomas Huth 
2017d22697ddSPaolo Bonzini     MOVE(*d, r);
2018fcf5ef2aSThomas Huth }
2019fcf5ef2aSThomas Huth 
2020fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
2021fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
2022fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
2023fcf5ef2aSThomas Huth 
2024fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
2025fcf5ef2aSThomas Huth {
2026d1da229fSPaul Brook     target_long val, limit;
2027fcf5ef2aSThomas Huth 
2028fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
2029fcf5ef2aSThomas Huth     if (ctrl >> 8) {
2030d1da229fSPaul Brook         val = (target_long)env->regs[reg];
2031fcf5ef2aSThomas Huth     } else {
2032d1da229fSPaul Brook         val = (int32_t)env->regs[reg];
2033fcf5ef2aSThomas Huth     }
2034fcf5ef2aSThomas Huth     if (ctrl & 1) {
2035d1da229fSPaul Brook         limit = 8;
2036fcf5ef2aSThomas Huth     } else {
2037d1da229fSPaul Brook         limit = 16;
2038fcf5ef2aSThomas Huth     }
2039d1da229fSPaul Brook     if ((val > limit) || (val < -limit)) {
2040d1da229fSPaul Brook         return limit;
2041fcf5ef2aSThomas Huth     }
2042d1da229fSPaul Brook     return abs1(val);
2043fcf5ef2aSThomas Huth }
2044fcf5ef2aSThomas Huth 
2045fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
2046fcf5ef2aSThomas Huth {
2047fcf5ef2aSThomas Huth     int val = 0;
2048fcf5ef2aSThomas Huth 
2049fcf5ef2aSThomas Huth     if (ctrl & 1) {
2050fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
2051fcf5ef2aSThomas Huth             val++;
2052fcf5ef2aSThomas Huth         }
2053fcf5ef2aSThomas Huth     } else {
2054fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
2055fcf5ef2aSThomas Huth             val++;
2056fcf5ef2aSThomas Huth         }
2057fcf5ef2aSThomas Huth     }
2058fcf5ef2aSThomas Huth 
2059fcf5ef2aSThomas Huth     return val;
2060fcf5ef2aSThomas Huth }
2061fcf5ef2aSThomas Huth 
2062fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
2063fcf5ef2aSThomas Huth {
2064fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
2065fcf5ef2aSThomas Huth     case 0:
2066fcf5ef2aSThomas Huth         return r->B(i);
2067fcf5ef2aSThomas Huth     case 1:
2068fcf5ef2aSThomas Huth         return r->W(i);
2069fcf5ef2aSThomas Huth     case 2:
2070fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
2071fcf5ef2aSThomas Huth     case 3:
2072fcf5ef2aSThomas Huth     default:
2073fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
2074fcf5ef2aSThomas Huth     }
2075fcf5ef2aSThomas Huth }
2076fcf5ef2aSThomas Huth 
2077fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
2078fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
2079fcf5ef2aSThomas Huth {
2080fcf5ef2aSThomas Huth     unsigned int res = 0;
2081fcf5ef2aSThomas Huth     int v;
2082fcf5ef2aSThomas Huth     int j, i;
2083fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
2084fcf5ef2aSThomas Huth 
2085fcf5ef2aSThomas Huth     valids--;
2086fcf5ef2aSThomas Huth     validd--;
2087fcf5ef2aSThomas Huth 
2088fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2089fcf5ef2aSThomas Huth 
2090fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2091fcf5ef2aSThomas Huth     case 0:
2092fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2093fcf5ef2aSThomas Huth             res <<= 1;
2094fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2095fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2096fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2097fcf5ef2aSThomas Huth             }
2098fcf5ef2aSThomas Huth         }
2099fcf5ef2aSThomas Huth         break;
2100fcf5ef2aSThomas Huth     case 1:
2101fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2102fcf5ef2aSThomas Huth             res <<= 1;
2103fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2104fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2105fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2106fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2107fcf5ef2aSThomas Huth             }
2108fcf5ef2aSThomas Huth         }
2109fcf5ef2aSThomas Huth         break;
2110fcf5ef2aSThomas Huth     case 2:
2111fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2112fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2113fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2114fcf5ef2aSThomas Huth             res <<= 1;
2115fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2116fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2117fcf5ef2aSThomas Huth         }
2118fcf5ef2aSThomas Huth         break;
2119fcf5ef2aSThomas Huth     case 3:
2120ae35eea7SJoseph Myers         if (validd == -1) {
2121ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2122ae35eea7SJoseph Myers             break;
2123ae35eea7SJoseph Myers         }
2124bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2125fcf5ef2aSThomas Huth             res <<= 1;
2126fcf5ef2aSThomas Huth             v = 1;
2127bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2128fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2129fcf5ef2aSThomas Huth             }
2130fcf5ef2aSThomas Huth             res |= v;
2131fcf5ef2aSThomas Huth         }
2132fcf5ef2aSThomas Huth         break;
2133fcf5ef2aSThomas Huth     }
2134fcf5ef2aSThomas Huth 
2135fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2136fcf5ef2aSThomas Huth     case 1:
2137fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2138fcf5ef2aSThomas Huth         break;
2139fcf5ef2aSThomas Huth     case 3:
2140fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2141fcf5ef2aSThomas Huth         break;
2142fcf5ef2aSThomas Huth     }
2143fcf5ef2aSThomas Huth 
2144fcf5ef2aSThomas Huth     if (res) {
2145fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2146fcf5ef2aSThomas Huth     }
2147fcf5ef2aSThomas Huth     if (res & 1) {
2148fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2149fcf5ef2aSThomas Huth     }
2150fcf5ef2aSThomas Huth 
2151fcf5ef2aSThomas Huth     return res;
2152fcf5ef2aSThomas Huth }
2153fcf5ef2aSThomas Huth 
2154fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2155fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2156fcf5ef2aSThomas Huth {
2157fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2158fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2159fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2160fcf5ef2aSThomas Huth 
2161fcf5ef2aSThomas Huth     if (res) {
2162fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2163fcf5ef2aSThomas Huth     } else {
2164fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2165fcf5ef2aSThomas Huth     }
2166fcf5ef2aSThomas Huth }
2167fcf5ef2aSThomas Huth 
2168fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2169fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2170fcf5ef2aSThomas Huth {
2171fcf5ef2aSThomas Huth     int i;
2172fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2173fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2174fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2175fcf5ef2aSThomas Huth 
2176fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2177fcf5ef2aSThomas Huth         if (ctrl & 1) {
2178fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2179fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2180fcf5ef2aSThomas Huth             }
2181fcf5ef2aSThomas Huth         } else {
2182fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2183fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2184fcf5ef2aSThomas Huth             }
2185fcf5ef2aSThomas Huth         }
2186fcf5ef2aSThomas Huth     } else {
2187fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2188fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2189fcf5ef2aSThomas Huth     }
2190fcf5ef2aSThomas Huth }
2191fcf5ef2aSThomas Huth 
2192fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2193fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2194fcf5ef2aSThomas Huth {
2195fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2196fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2197fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2198fcf5ef2aSThomas Huth 
2199fcf5ef2aSThomas Huth     if (res) {
2200fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2201fcf5ef2aSThomas Huth     } else {
2202fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2203fcf5ef2aSThomas Huth     }
2204fcf5ef2aSThomas Huth }
2205fcf5ef2aSThomas Huth 
2206fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2207fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2208fcf5ef2aSThomas Huth {
2209fcf5ef2aSThomas Huth     int i;
2210fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2211fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2212fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2213fcf5ef2aSThomas Huth 
2214fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2215fcf5ef2aSThomas Huth         if (ctrl & 1) {
2216fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2217fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2218fcf5ef2aSThomas Huth             }
2219fcf5ef2aSThomas Huth         } else {
2220fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2221fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2222fcf5ef2aSThomas Huth             }
2223fcf5ef2aSThomas Huth         }
2224fcf5ef2aSThomas Huth     } else {
2225fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2226fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2227fcf5ef2aSThomas Huth     }
2228fcf5ef2aSThomas Huth }
2229fcf5ef2aSThomas Huth 
2230fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2231fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2232fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2233fcf5ef2aSThomas Huth {
2234fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2235fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2236fcf5ef2aSThomas Huth 
2237fcf5ef2aSThomas Huth     while (len--) {
2238fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2239fcf5ef2aSThomas Huth     }
2240fcf5ef2aSThomas Huth 
2241fcf5ef2aSThomas Huth     return crc;
2242fcf5ef2aSThomas Huth }
2243fcf5ef2aSThomas Huth 
2244fcf5ef2aSThomas Huth void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2245fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2246fcf5ef2aSThomas Huth {
2247fcf5ef2aSThomas Huth     uint64_t ah, al, b, resh, resl;
2248fcf5ef2aSThomas Huth 
2249fcf5ef2aSThomas Huth     ah = 0;
2250fcf5ef2aSThomas Huth     al = d->Q((ctrl & 1) != 0);
2251fcf5ef2aSThomas Huth     b = s->Q((ctrl & 16) != 0);
2252fcf5ef2aSThomas Huth     resh = resl = 0;
2253fcf5ef2aSThomas Huth 
2254fcf5ef2aSThomas Huth     while (b) {
2255fcf5ef2aSThomas Huth         if (b & 1) {
2256fcf5ef2aSThomas Huth             resl ^= al;
2257fcf5ef2aSThomas Huth             resh ^= ah;
2258fcf5ef2aSThomas Huth         }
2259fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2260fcf5ef2aSThomas Huth         al <<= 1;
2261fcf5ef2aSThomas Huth         b >>= 1;
2262fcf5ef2aSThomas Huth     }
2263fcf5ef2aSThomas Huth 
2264fcf5ef2aSThomas Huth     d->Q(0) = resl;
2265fcf5ef2aSThomas Huth     d->Q(1) = resh;
2266fcf5ef2aSThomas Huth }
2267fcf5ef2aSThomas Huth 
2268fcf5ef2aSThomas Huth void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2269fcf5ef2aSThomas Huth {
2270fcf5ef2aSThomas Huth     int i;
2271fcf5ef2aSThomas Huth     Reg st = *d;
2272fcf5ef2aSThomas Huth     Reg rk = *s;
2273fcf5ef2aSThomas Huth 
2274fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2275fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4*i+0])] ^
2276fcf5ef2aSThomas Huth                                     AES_Td1[st.B(AES_ishifts[4*i+1])] ^
2277fcf5ef2aSThomas Huth                                     AES_Td2[st.B(AES_ishifts[4*i+2])] ^
2278fcf5ef2aSThomas Huth                                     AES_Td3[st.B(AES_ishifts[4*i+3])]);
2279fcf5ef2aSThomas Huth     }
2280fcf5ef2aSThomas Huth }
2281fcf5ef2aSThomas Huth 
2282fcf5ef2aSThomas Huth void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2283fcf5ef2aSThomas Huth {
2284fcf5ef2aSThomas Huth     int i;
2285fcf5ef2aSThomas Huth     Reg st = *d;
2286fcf5ef2aSThomas Huth     Reg rk = *s;
2287fcf5ef2aSThomas Huth 
2288fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2289fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i])]);
2290fcf5ef2aSThomas Huth     }
2291fcf5ef2aSThomas Huth }
2292fcf5ef2aSThomas Huth 
2293fcf5ef2aSThomas Huth void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2294fcf5ef2aSThomas Huth {
2295fcf5ef2aSThomas Huth     int i;
2296fcf5ef2aSThomas Huth     Reg st = *d;
2297fcf5ef2aSThomas Huth     Reg rk = *s;
2298fcf5ef2aSThomas Huth 
2299fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2300fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4*i+0])] ^
2301fcf5ef2aSThomas Huth                                     AES_Te1[st.B(AES_shifts[4*i+1])] ^
2302fcf5ef2aSThomas Huth                                     AES_Te2[st.B(AES_shifts[4*i+2])] ^
2303fcf5ef2aSThomas Huth                                     AES_Te3[st.B(AES_shifts[4*i+3])]);
2304fcf5ef2aSThomas Huth     }
2305fcf5ef2aSThomas Huth }
2306fcf5ef2aSThomas Huth 
2307fcf5ef2aSThomas Huth void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2308fcf5ef2aSThomas Huth {
2309fcf5ef2aSThomas Huth     int i;
2310fcf5ef2aSThomas Huth     Reg st = *d;
2311fcf5ef2aSThomas Huth     Reg rk = *s;
2312fcf5ef2aSThomas Huth 
2313fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2314fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i])]);
2315fcf5ef2aSThomas Huth     }
2316fcf5ef2aSThomas Huth 
2317fcf5ef2aSThomas Huth }
2318fcf5ef2aSThomas Huth 
2319fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2320fcf5ef2aSThomas Huth {
2321fcf5ef2aSThomas Huth     int i;
2322fcf5ef2aSThomas Huth     Reg tmp = *s;
2323fcf5ef2aSThomas Huth 
2324fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2325fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4*i+0)][0] ^
2326fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+1)][1] ^
2327fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+2)][2] ^
2328fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+3)][3]);
2329fcf5ef2aSThomas Huth     }
2330fcf5ef2aSThomas Huth }
2331fcf5ef2aSThomas Huth 
2332fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2333fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2334fcf5ef2aSThomas Huth {
2335fcf5ef2aSThomas Huth     int i;
2336fcf5ef2aSThomas Huth     Reg tmp = *s;
2337fcf5ef2aSThomas Huth 
2338fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2339fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2340fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2341fcf5ef2aSThomas Huth     }
2342fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2343fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2344fcf5ef2aSThomas Huth }
2345fcf5ef2aSThomas Huth #endif
2346fcf5ef2aSThomas Huth 
2347fcf5ef2aSThomas Huth #undef SHIFT
2348fcf5ef2aSThomas Huth #undef XMM_ONLY
2349fcf5ef2aSThomas Huth #undef Reg
2350fcf5ef2aSThomas Huth #undef B
2351fcf5ef2aSThomas Huth #undef W
2352fcf5ef2aSThomas Huth #undef L
2353fcf5ef2aSThomas Huth #undef Q
2354fcf5ef2aSThomas Huth #undef SUFFIX
2355d22697ddSPaolo Bonzini #undef SIZE
2356