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