xref: /openbmc/qemu/target/i386/ops_sse.h (revision d9ff33ada7f32ca59f99b270a2d0eb223b3c9c8f)
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
10*d9ff33adSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "crypto/aes.h"
22fcf5ef2aSThomas Huth 
23fcf5ef2aSThomas Huth #if SHIFT == 0
24fcf5ef2aSThomas Huth #define Reg MMXReg
25fcf5ef2aSThomas Huth #define XMM_ONLY(...)
26fcf5ef2aSThomas Huth #define B(n) MMX_B(n)
27fcf5ef2aSThomas Huth #define W(n) MMX_W(n)
28fcf5ef2aSThomas Huth #define L(n) MMX_L(n)
29fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n)
30fcf5ef2aSThomas Huth #define SUFFIX _mmx
31fcf5ef2aSThomas Huth #else
32fcf5ef2aSThomas Huth #define Reg ZMMReg
33fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__
34fcf5ef2aSThomas Huth #define B(n) ZMM_B(n)
35fcf5ef2aSThomas Huth #define W(n) ZMM_W(n)
36fcf5ef2aSThomas Huth #define L(n) ZMM_L(n)
37fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n)
38fcf5ef2aSThomas Huth #define SUFFIX _xmm
39fcf5ef2aSThomas Huth #endif
40fcf5ef2aSThomas Huth 
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 */
7131e8a98b5SPeter Maydell 
7141e8a98b5SPeter Maydell /*
7151e8a98b5SPeter Maydell  * x86 mandates that we return the indefinite integer value for the result
7161e8a98b5SPeter Maydell  * of any float-to-integer conversion that raises the 'invalid' exception.
7171e8a98b5SPeter Maydell  * Wrap the softfloat functions to get this behaviour.
7181e8a98b5SPeter Maydell  */
7191e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE)              \
7201e8a98b5SPeter Maydell     static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s)        \
7211e8a98b5SPeter Maydell     {                                                                   \
7221e8a98b5SPeter Maydell         int oldflags, newflags;                                         \
7231e8a98b5SPeter Maydell         RETTYPE r;                                                      \
7241e8a98b5SPeter Maydell                                                                         \
7251e8a98b5SPeter Maydell         oldflags = get_float_exception_flags(s);                        \
7261e8a98b5SPeter Maydell         set_float_exception_flags(0, s);                                \
7271e8a98b5SPeter Maydell         r = FN(a, s);                                                   \
7281e8a98b5SPeter Maydell         newflags = get_float_exception_flags(s);                        \
7291e8a98b5SPeter Maydell         if (newflags & float_flag_invalid) {                            \
7301e8a98b5SPeter Maydell             r = INDEFVALUE;                                             \
7311e8a98b5SPeter Maydell         }                                                               \
7321e8a98b5SPeter Maydell         set_float_exception_flags(newflags | oldflags, s);              \
7331e8a98b5SPeter Maydell         return r;                                                       \
7341e8a98b5SPeter Maydell     }
7351e8a98b5SPeter Maydell 
7361e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN)
7371e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN)
7381e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN)
7391e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN)
7401e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN)
7411e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN)
7421e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN)
7431e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN)
7441e8a98b5SPeter Maydell 
745fcf5ef2aSThomas Huth void helper_cvtps2dq(CPUX86State *env, ZMMReg *d, ZMMReg *s)
746fcf5ef2aSThomas Huth {
7471e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7481e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
7491e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32(s->ZMM_S(2), &env->sse_status);
7501e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32(s->ZMM_S(3), &env->sse_status);
751fcf5ef2aSThomas Huth }
752fcf5ef2aSThomas Huth 
753fcf5ef2aSThomas Huth void helper_cvtpd2dq(CPUX86State *env, ZMMReg *d, ZMMReg *s)
754fcf5ef2aSThomas Huth {
7551e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7561e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
757fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
758fcf5ef2aSThomas Huth }
759fcf5ef2aSThomas Huth 
760fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
761fcf5ef2aSThomas Huth {
7621e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
7631e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status);
764fcf5ef2aSThomas Huth }
765fcf5ef2aSThomas Huth 
766fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
767fcf5ef2aSThomas Huth {
7681e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
7691e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status);
770fcf5ef2aSThomas Huth }
771fcf5ef2aSThomas Huth 
772fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s)
773fcf5ef2aSThomas Huth {
7741e8a98b5SPeter Maydell     return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status);
775fcf5ef2aSThomas Huth }
776fcf5ef2aSThomas Huth 
777fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s)
778fcf5ef2aSThomas Huth {
7791e8a98b5SPeter Maydell     return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status);
780fcf5ef2aSThomas Huth }
781fcf5ef2aSThomas Huth 
782fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
783fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s)
784fcf5ef2aSThomas Huth {
7851e8a98b5SPeter Maydell     return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status);
786fcf5ef2aSThomas Huth }
787fcf5ef2aSThomas Huth 
788fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s)
789fcf5ef2aSThomas Huth {
7901e8a98b5SPeter Maydell     return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status);
791fcf5ef2aSThomas Huth }
792fcf5ef2aSThomas Huth #endif
793fcf5ef2aSThomas Huth 
794fcf5ef2aSThomas Huth /* float to integer truncated */
795fcf5ef2aSThomas Huth void helper_cvttps2dq(CPUX86State *env, ZMMReg *d, ZMMReg *s)
796fcf5ef2aSThomas Huth {
7971e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
7981e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
7991e8a98b5SPeter Maydell     d->ZMM_L(2) = x86_float32_to_int32_round_to_zero(s->ZMM_S(2), &env->sse_status);
8001e8a98b5SPeter Maydell     d->ZMM_L(3) = x86_float32_to_int32_round_to_zero(s->ZMM_S(3), &env->sse_status);
801fcf5ef2aSThomas Huth }
802fcf5ef2aSThomas Huth 
803fcf5ef2aSThomas Huth void helper_cvttpd2dq(CPUX86State *env, ZMMReg *d, ZMMReg *s)
804fcf5ef2aSThomas Huth {
8051e8a98b5SPeter Maydell     d->ZMM_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8061e8a98b5SPeter Maydell     d->ZMM_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
807fcf5ef2aSThomas Huth     d->ZMM_Q(1) = 0;
808fcf5ef2aSThomas Huth }
809fcf5ef2aSThomas Huth 
810fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
811fcf5ef2aSThomas Huth {
8121e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
8131e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status);
814fcf5ef2aSThomas Huth }
815fcf5ef2aSThomas Huth 
816fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s)
817fcf5ef2aSThomas Huth {
8181e8a98b5SPeter Maydell     d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
8191e8a98b5SPeter Maydell     d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status);
820fcf5ef2aSThomas Huth }
821fcf5ef2aSThomas Huth 
822fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s)
823fcf5ef2aSThomas Huth {
8241e8a98b5SPeter Maydell     return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status);
825fcf5ef2aSThomas Huth }
826fcf5ef2aSThomas Huth 
827fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s)
828fcf5ef2aSThomas Huth {
8291e8a98b5SPeter Maydell     return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status);
830fcf5ef2aSThomas Huth }
831fcf5ef2aSThomas Huth 
832fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
833fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s)
834fcf5ef2aSThomas Huth {
8351e8a98b5SPeter Maydell     return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status);
836fcf5ef2aSThomas Huth }
837fcf5ef2aSThomas Huth 
838fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)
839fcf5ef2aSThomas Huth {
8401e8a98b5SPeter Maydell     return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status);
841fcf5ef2aSThomas Huth }
842fcf5ef2aSThomas Huth #endif
843fcf5ef2aSThomas Huth 
844fcf5ef2aSThomas Huth void helper_rsqrtps(CPUX86State *env, ZMMReg *d, ZMMReg *s)
845fcf5ef2aSThomas Huth {
846418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
847fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
848fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
849fcf5ef2aSThomas Huth                               &env->sse_status);
850fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_div(float32_one,
851fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(1), &env->sse_status),
852fcf5ef2aSThomas Huth                               &env->sse_status);
853fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_div(float32_one,
854fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(2), &env->sse_status),
855fcf5ef2aSThomas Huth                               &env->sse_status);
856fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_div(float32_one,
857fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(3), &env->sse_status),
858fcf5ef2aSThomas Huth                               &env->sse_status);
859418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
860fcf5ef2aSThomas Huth }
861fcf5ef2aSThomas Huth 
862fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
863fcf5ef2aSThomas Huth {
864418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
865fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one,
866fcf5ef2aSThomas Huth                               float32_sqrt(s->ZMM_S(0), &env->sse_status),
867fcf5ef2aSThomas Huth                               &env->sse_status);
868418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
869fcf5ef2aSThomas Huth }
870fcf5ef2aSThomas Huth 
871fcf5ef2aSThomas Huth void helper_rcpps(CPUX86State *env, ZMMReg *d, ZMMReg *s)
872fcf5ef2aSThomas Huth {
873418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
874fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
875fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_div(float32_one, s->ZMM_S(1), &env->sse_status);
876fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_div(float32_one, s->ZMM_S(2), &env->sse_status);
877fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_div(float32_one, s->ZMM_S(3), &env->sse_status);
878418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
879fcf5ef2aSThomas Huth }
880fcf5ef2aSThomas Huth 
881fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s)
882fcf5ef2aSThomas Huth {
883418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
884fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status);
885418b0f93SJoseph Myers     set_float_exception_flags(old_flags, &env->sse_status);
886fcf5ef2aSThomas Huth }
887fcf5ef2aSThomas Huth 
888fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len)
889fcf5ef2aSThomas Huth {
890fcf5ef2aSThomas Huth     uint64_t mask;
891fcf5ef2aSThomas Huth 
892fcf5ef2aSThomas Huth     if (len == 0) {
893fcf5ef2aSThomas Huth         mask = ~0LL;
894fcf5ef2aSThomas Huth     } else {
895fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
896fcf5ef2aSThomas Huth     }
897fcf5ef2aSThomas Huth     return (src >> shift) & mask;
898fcf5ef2aSThomas Huth }
899fcf5ef2aSThomas Huth 
900fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
901fcf5ef2aSThomas Huth {
902fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1), s->ZMM_B(0));
903fcf5ef2aSThomas Huth }
904fcf5ef2aSThomas Huth 
905fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length)
906fcf5ef2aSThomas Huth {
907fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length);
908fcf5ef2aSThomas Huth }
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth static inline uint64_t helper_insertq(uint64_t src, int shift, int len)
911fcf5ef2aSThomas Huth {
912fcf5ef2aSThomas Huth     uint64_t mask;
913fcf5ef2aSThomas Huth 
914fcf5ef2aSThomas Huth     if (len == 0) {
915fcf5ef2aSThomas Huth         mask = ~0ULL;
916fcf5ef2aSThomas Huth     } else {
917fcf5ef2aSThomas Huth         mask = (1ULL << len) - 1;
918fcf5ef2aSThomas Huth     }
919fcf5ef2aSThomas Huth     return (src & ~(mask << shift)) | ((src & mask) << shift);
920fcf5ef2aSThomas Huth }
921fcf5ef2aSThomas Huth 
922fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s)
923fcf5ef2aSThomas Huth {
924fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(s->ZMM_Q(0), s->ZMM_B(9), s->ZMM_B(8));
925fcf5ef2aSThomas Huth }
926fcf5ef2aSThomas Huth 
927fcf5ef2aSThomas Huth void helper_insertq_i(CPUX86State *env, ZMMReg *d, int index, int length)
928fcf5ef2aSThomas Huth {
929fcf5ef2aSThomas Huth     d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), index, length);
930fcf5ef2aSThomas Huth }
931fcf5ef2aSThomas Huth 
932fcf5ef2aSThomas Huth void helper_haddps(CPUX86State *env, ZMMReg *d, ZMMReg *s)
933fcf5ef2aSThomas Huth {
934fcf5ef2aSThomas Huth     ZMMReg r;
935fcf5ef2aSThomas Huth 
936fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_add(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
937fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_add(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
938fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_add(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
939fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_add(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
940fcf5ef2aSThomas Huth     *d = r;
941fcf5ef2aSThomas Huth }
942fcf5ef2aSThomas Huth 
943fcf5ef2aSThomas Huth void helper_haddpd(CPUX86State *env, ZMMReg *d, ZMMReg *s)
944fcf5ef2aSThomas Huth {
945fcf5ef2aSThomas Huth     ZMMReg r;
946fcf5ef2aSThomas Huth 
947fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_add(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
948fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_add(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
949fcf5ef2aSThomas Huth     *d = r;
950fcf5ef2aSThomas Huth }
951fcf5ef2aSThomas Huth 
952fcf5ef2aSThomas Huth void helper_hsubps(CPUX86State *env, ZMMReg *d, ZMMReg *s)
953fcf5ef2aSThomas Huth {
954fcf5ef2aSThomas Huth     ZMMReg r;
955fcf5ef2aSThomas Huth 
956fcf5ef2aSThomas Huth     r.ZMM_S(0) = float32_sub(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status);
957fcf5ef2aSThomas Huth     r.ZMM_S(1) = float32_sub(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status);
958fcf5ef2aSThomas Huth     r.ZMM_S(2) = float32_sub(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status);
959fcf5ef2aSThomas Huth     r.ZMM_S(3) = float32_sub(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status);
960fcf5ef2aSThomas Huth     *d = r;
961fcf5ef2aSThomas Huth }
962fcf5ef2aSThomas Huth 
963fcf5ef2aSThomas Huth void helper_hsubpd(CPUX86State *env, ZMMReg *d, ZMMReg *s)
964fcf5ef2aSThomas Huth {
965fcf5ef2aSThomas Huth     ZMMReg r;
966fcf5ef2aSThomas Huth 
967fcf5ef2aSThomas Huth     r.ZMM_D(0) = float64_sub(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status);
968fcf5ef2aSThomas Huth     r.ZMM_D(1) = float64_sub(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status);
969fcf5ef2aSThomas Huth     *d = r;
970fcf5ef2aSThomas Huth }
971fcf5ef2aSThomas Huth 
972fcf5ef2aSThomas Huth void helper_addsubps(CPUX86State *env, ZMMReg *d, ZMMReg *s)
973fcf5ef2aSThomas Huth {
974fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_sub(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status);
975fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_add(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status);
976fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_sub(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status);
977fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_add(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status);
978fcf5ef2aSThomas Huth }
979fcf5ef2aSThomas Huth 
980fcf5ef2aSThomas Huth void helper_addsubpd(CPUX86State *env, ZMMReg *d, ZMMReg *s)
981fcf5ef2aSThomas Huth {
982fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_sub(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status);
983fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_add(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status);
984fcf5ef2aSThomas Huth }
985fcf5ef2aSThomas Huth 
986fcf5ef2aSThomas Huth /* XXX: unordered */
987fcf5ef2aSThomas Huth #define SSE_HELPER_CMP(name, F)                                         \
988fcf5ef2aSThomas Huth     void helper_ ## name ## ps(CPUX86State *env, Reg *d, Reg *s)        \
989fcf5ef2aSThomas Huth     {                                                                   \
990fcf5ef2aSThomas Huth         d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
991fcf5ef2aSThomas Huth         d->ZMM_L(1) = F(32, d->ZMM_S(1), s->ZMM_S(1));                  \
992fcf5ef2aSThomas Huth         d->ZMM_L(2) = F(32, d->ZMM_S(2), s->ZMM_S(2));                  \
993fcf5ef2aSThomas Huth         d->ZMM_L(3) = F(32, d->ZMM_S(3), s->ZMM_S(3));                  \
994fcf5ef2aSThomas Huth     }                                                                   \
995fcf5ef2aSThomas Huth                                                                         \
996fcf5ef2aSThomas Huth     void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)        \
997fcf5ef2aSThomas Huth     {                                                                   \
998fcf5ef2aSThomas Huth         d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0));                  \
999fcf5ef2aSThomas Huth     }                                                                   \
1000fcf5ef2aSThomas Huth                                                                         \
1001fcf5ef2aSThomas Huth     void helper_ ## name ## pd(CPUX86State *env, Reg *d, Reg *s)        \
1002fcf5ef2aSThomas Huth     {                                                                   \
1003fcf5ef2aSThomas Huth         d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
1004fcf5ef2aSThomas Huth         d->ZMM_Q(1) = F(64, d->ZMM_D(1), s->ZMM_D(1));                  \
1005fcf5ef2aSThomas Huth     }                                                                   \
1006fcf5ef2aSThomas Huth                                                                         \
1007fcf5ef2aSThomas Huth     void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)        \
1008fcf5ef2aSThomas Huth     {                                                                   \
1009fcf5ef2aSThomas Huth         d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0));                  \
1010fcf5ef2aSThomas Huth     }
1011fcf5ef2aSThomas Huth 
1012fcf5ef2aSThomas Huth #define FPU_CMPEQ(size, a, b)                                           \
1013fcf5ef2aSThomas Huth     (float ## size ## _eq_quiet(a, b, &env->sse_status) ? -1 : 0)
1014fcf5ef2aSThomas Huth #define FPU_CMPLT(size, a, b)                                           \
1015fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0)
1016fcf5ef2aSThomas Huth #define FPU_CMPLE(size, a, b)                                           \
1017fcf5ef2aSThomas Huth     (float ## size ## _le(a, b, &env->sse_status) ? -1 : 0)
1018fcf5ef2aSThomas Huth #define FPU_CMPUNORD(size, a, b)                                        \
1019fcf5ef2aSThomas Huth     (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? -1 : 0)
1020fcf5ef2aSThomas Huth #define FPU_CMPNEQ(size, a, b)                                          \
1021fcf5ef2aSThomas Huth     (float ## size ## _eq_quiet(a, b, &env->sse_status) ? 0 : -1)
1022fcf5ef2aSThomas Huth #define FPU_CMPNLT(size, a, b)                                          \
1023fcf5ef2aSThomas Huth     (float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1)
1024fcf5ef2aSThomas Huth #define FPU_CMPNLE(size, a, b)                                          \
1025fcf5ef2aSThomas Huth     (float ## size ## _le(a, b, &env->sse_status) ? 0 : -1)
1026fcf5ef2aSThomas Huth #define FPU_CMPORD(size, a, b)                                          \
1027fcf5ef2aSThomas Huth     (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? 0 : -1)
1028fcf5ef2aSThomas Huth 
1029fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpeq, FPU_CMPEQ)
1030fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmplt, FPU_CMPLT)
1031fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmple, FPU_CMPLE)
1032fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpunord, FPU_CMPUNORD)
1033fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpneq, FPU_CMPNEQ)
1034fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnlt, FPU_CMPNLT)
1035fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnle, FPU_CMPNLE)
1036fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpord, FPU_CMPORD)
1037fcf5ef2aSThomas Huth 
1038fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
1039fcf5ef2aSThomas Huth 
1040fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s)
1041fcf5ef2aSThomas Huth {
104271bfd65cSRichard Henderson     FloatRelation ret;
1043fcf5ef2aSThomas Huth     float32 s0, s1;
1044fcf5ef2aSThomas Huth 
1045fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1046fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1047fcf5ef2aSThomas Huth     ret = float32_compare_quiet(s0, s1, &env->sse_status);
1048fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1049fcf5ef2aSThomas Huth }
1050fcf5ef2aSThomas Huth 
1051fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s)
1052fcf5ef2aSThomas Huth {
105371bfd65cSRichard Henderson     FloatRelation ret;
1054fcf5ef2aSThomas Huth     float32 s0, s1;
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth     s0 = d->ZMM_S(0);
1057fcf5ef2aSThomas Huth     s1 = s->ZMM_S(0);
1058fcf5ef2aSThomas Huth     ret = float32_compare(s0, s1, &env->sse_status);
1059fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1060fcf5ef2aSThomas Huth }
1061fcf5ef2aSThomas Huth 
1062fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s)
1063fcf5ef2aSThomas Huth {
106471bfd65cSRichard Henderson     FloatRelation ret;
1065fcf5ef2aSThomas Huth     float64 d0, d1;
1066fcf5ef2aSThomas Huth 
1067fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1068fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1069fcf5ef2aSThomas Huth     ret = float64_compare_quiet(d0, d1, &env->sse_status);
1070fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1071fcf5ef2aSThomas Huth }
1072fcf5ef2aSThomas Huth 
1073fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s)
1074fcf5ef2aSThomas Huth {
107571bfd65cSRichard Henderson     FloatRelation ret;
1076fcf5ef2aSThomas Huth     float64 d0, d1;
1077fcf5ef2aSThomas Huth 
1078fcf5ef2aSThomas Huth     d0 = d->ZMM_D(0);
1079fcf5ef2aSThomas Huth     d1 = s->ZMM_D(0);
1080fcf5ef2aSThomas Huth     ret = float64_compare(d0, d1, &env->sse_status);
1081fcf5ef2aSThomas Huth     CC_SRC = comis_eflags[ret + 1];
1082fcf5ef2aSThomas Huth }
1083fcf5ef2aSThomas Huth 
1084fcf5ef2aSThomas Huth uint32_t helper_movmskps(CPUX86State *env, Reg *s)
1085fcf5ef2aSThomas Huth {
1086fcf5ef2aSThomas Huth     int b0, b1, b2, b3;
1087fcf5ef2aSThomas Huth 
1088fcf5ef2aSThomas Huth     b0 = s->ZMM_L(0) >> 31;
1089fcf5ef2aSThomas Huth     b1 = s->ZMM_L(1) >> 31;
1090fcf5ef2aSThomas Huth     b2 = s->ZMM_L(2) >> 31;
1091fcf5ef2aSThomas Huth     b3 = s->ZMM_L(3) >> 31;
1092fcf5ef2aSThomas Huth     return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3);
1093fcf5ef2aSThomas Huth }
1094fcf5ef2aSThomas Huth 
1095fcf5ef2aSThomas Huth uint32_t helper_movmskpd(CPUX86State *env, Reg *s)
1096fcf5ef2aSThomas Huth {
1097fcf5ef2aSThomas Huth     int b0, b1;
1098fcf5ef2aSThomas Huth 
1099fcf5ef2aSThomas Huth     b0 = s->ZMM_L(1) >> 31;
1100fcf5ef2aSThomas Huth     b1 = s->ZMM_L(3) >> 31;
1101fcf5ef2aSThomas Huth     return b0 | (b1 << 1);
1102fcf5ef2aSThomas Huth }
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth #endif
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s)
1107fcf5ef2aSThomas Huth {
1108fcf5ef2aSThomas Huth     uint32_t val;
1109fcf5ef2aSThomas Huth 
1110fcf5ef2aSThomas Huth     val = 0;
1111fcf5ef2aSThomas Huth     val |= (s->B(0) >> 7);
1112fcf5ef2aSThomas Huth     val |= (s->B(1) >> 6) & 0x02;
1113fcf5ef2aSThomas Huth     val |= (s->B(2) >> 5) & 0x04;
1114fcf5ef2aSThomas Huth     val |= (s->B(3) >> 4) & 0x08;
1115fcf5ef2aSThomas Huth     val |= (s->B(4) >> 3) & 0x10;
1116fcf5ef2aSThomas Huth     val |= (s->B(5) >> 2) & 0x20;
1117fcf5ef2aSThomas Huth     val |= (s->B(6) >> 1) & 0x40;
1118fcf5ef2aSThomas Huth     val |= (s->B(7)) & 0x80;
1119fcf5ef2aSThomas Huth #if SHIFT == 1
1120fcf5ef2aSThomas Huth     val |= (s->B(8) << 1) & 0x0100;
1121fcf5ef2aSThomas Huth     val |= (s->B(9) << 2) & 0x0200;
1122fcf5ef2aSThomas Huth     val |= (s->B(10) << 3) & 0x0400;
1123fcf5ef2aSThomas Huth     val |= (s->B(11) << 4) & 0x0800;
1124fcf5ef2aSThomas Huth     val |= (s->B(12) << 5) & 0x1000;
1125fcf5ef2aSThomas Huth     val |= (s->B(13) << 6) & 0x2000;
1126fcf5ef2aSThomas Huth     val |= (s->B(14) << 7) & 0x4000;
1127fcf5ef2aSThomas Huth     val |= (s->B(15) << 8) & 0x8000;
1128fcf5ef2aSThomas Huth #endif
1129fcf5ef2aSThomas Huth     return val;
1130fcf5ef2aSThomas Huth }
1131fcf5ef2aSThomas Huth 
1132fcf5ef2aSThomas Huth void glue(helper_packsswb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1133fcf5ef2aSThomas Huth {
1134fcf5ef2aSThomas Huth     Reg r;
1135fcf5ef2aSThomas Huth 
1136fcf5ef2aSThomas Huth     r.B(0) = satsb((int16_t)d->W(0));
1137fcf5ef2aSThomas Huth     r.B(1) = satsb((int16_t)d->W(1));
1138fcf5ef2aSThomas Huth     r.B(2) = satsb((int16_t)d->W(2));
1139fcf5ef2aSThomas Huth     r.B(3) = satsb((int16_t)d->W(3));
1140fcf5ef2aSThomas Huth #if SHIFT == 1
1141fcf5ef2aSThomas Huth     r.B(4) = satsb((int16_t)d->W(4));
1142fcf5ef2aSThomas Huth     r.B(5) = satsb((int16_t)d->W(5));
1143fcf5ef2aSThomas Huth     r.B(6) = satsb((int16_t)d->W(6));
1144fcf5ef2aSThomas Huth     r.B(7) = satsb((int16_t)d->W(7));
1145fcf5ef2aSThomas Huth #endif
1146fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 0) = satsb((int16_t)s->W(0));
1147fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 1) = satsb((int16_t)s->W(1));
1148fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 2) = satsb((int16_t)s->W(2));
1149fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 3) = satsb((int16_t)s->W(3));
1150fcf5ef2aSThomas Huth #if SHIFT == 1
1151fcf5ef2aSThomas Huth     r.B(12) = satsb((int16_t)s->W(4));
1152fcf5ef2aSThomas Huth     r.B(13) = satsb((int16_t)s->W(5));
1153fcf5ef2aSThomas Huth     r.B(14) = satsb((int16_t)s->W(6));
1154fcf5ef2aSThomas Huth     r.B(15) = satsb((int16_t)s->W(7));
1155fcf5ef2aSThomas Huth #endif
1156fcf5ef2aSThomas Huth     *d = r;
1157fcf5ef2aSThomas Huth }
1158fcf5ef2aSThomas Huth 
1159fcf5ef2aSThomas Huth void glue(helper_packuswb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1160fcf5ef2aSThomas Huth {
1161fcf5ef2aSThomas Huth     Reg r;
1162fcf5ef2aSThomas Huth 
1163fcf5ef2aSThomas Huth     r.B(0) = satub((int16_t)d->W(0));
1164fcf5ef2aSThomas Huth     r.B(1) = satub((int16_t)d->W(1));
1165fcf5ef2aSThomas Huth     r.B(2) = satub((int16_t)d->W(2));
1166fcf5ef2aSThomas Huth     r.B(3) = satub((int16_t)d->W(3));
1167fcf5ef2aSThomas Huth #if SHIFT == 1
1168fcf5ef2aSThomas Huth     r.B(4) = satub((int16_t)d->W(4));
1169fcf5ef2aSThomas Huth     r.B(5) = satub((int16_t)d->W(5));
1170fcf5ef2aSThomas Huth     r.B(6) = satub((int16_t)d->W(6));
1171fcf5ef2aSThomas Huth     r.B(7) = satub((int16_t)d->W(7));
1172fcf5ef2aSThomas Huth #endif
1173fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 0) = satub((int16_t)s->W(0));
1174fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 1) = satub((int16_t)s->W(1));
1175fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 2) = satub((int16_t)s->W(2));
1176fcf5ef2aSThomas Huth     r.B((4 << SHIFT) + 3) = satub((int16_t)s->W(3));
1177fcf5ef2aSThomas Huth #if SHIFT == 1
1178fcf5ef2aSThomas Huth     r.B(12) = satub((int16_t)s->W(4));
1179fcf5ef2aSThomas Huth     r.B(13) = satub((int16_t)s->W(5));
1180fcf5ef2aSThomas Huth     r.B(14) = satub((int16_t)s->W(6));
1181fcf5ef2aSThomas Huth     r.B(15) = satub((int16_t)s->W(7));
1182fcf5ef2aSThomas Huth #endif
1183fcf5ef2aSThomas Huth     *d = r;
1184fcf5ef2aSThomas Huth }
1185fcf5ef2aSThomas Huth 
1186fcf5ef2aSThomas Huth void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1187fcf5ef2aSThomas Huth {
1188fcf5ef2aSThomas Huth     Reg r;
1189fcf5ef2aSThomas Huth 
1190fcf5ef2aSThomas Huth     r.W(0) = satsw(d->L(0));
1191fcf5ef2aSThomas Huth     r.W(1) = satsw(d->L(1));
1192fcf5ef2aSThomas Huth #if SHIFT == 1
1193fcf5ef2aSThomas Huth     r.W(2) = satsw(d->L(2));
1194fcf5ef2aSThomas Huth     r.W(3) = satsw(d->L(3));
1195fcf5ef2aSThomas Huth #endif
1196fcf5ef2aSThomas Huth     r.W((2 << SHIFT) + 0) = satsw(s->L(0));
1197fcf5ef2aSThomas Huth     r.W((2 << SHIFT) + 1) = satsw(s->L(1));
1198fcf5ef2aSThomas Huth #if SHIFT == 1
1199fcf5ef2aSThomas Huth     r.W(6) = satsw(s->L(2));
1200fcf5ef2aSThomas Huth     r.W(7) = satsw(s->L(3));
1201fcf5ef2aSThomas Huth #endif
1202fcf5ef2aSThomas Huth     *d = r;
1203fcf5ef2aSThomas Huth }
1204fcf5ef2aSThomas Huth 
1205fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base)                                       \
1206fcf5ef2aSThomas Huth                                                                         \
1207fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1208fcf5ef2aSThomas Huth                                                         Reg *d, Reg *s) \
1209fcf5ef2aSThomas Huth     {                                                                   \
1210fcf5ef2aSThomas Huth         Reg r;                                                          \
1211fcf5ef2aSThomas Huth                                                                         \
1212fcf5ef2aSThomas Huth         r.B(0) = d->B((base << (SHIFT + 2)) + 0);                       \
1213fcf5ef2aSThomas Huth         r.B(1) = s->B((base << (SHIFT + 2)) + 0);                       \
1214fcf5ef2aSThomas Huth         r.B(2) = d->B((base << (SHIFT + 2)) + 1);                       \
1215fcf5ef2aSThomas Huth         r.B(3) = s->B((base << (SHIFT + 2)) + 1);                       \
1216fcf5ef2aSThomas Huth         r.B(4) = d->B((base << (SHIFT + 2)) + 2);                       \
1217fcf5ef2aSThomas Huth         r.B(5) = s->B((base << (SHIFT + 2)) + 2);                       \
1218fcf5ef2aSThomas Huth         r.B(6) = d->B((base << (SHIFT + 2)) + 3);                       \
1219fcf5ef2aSThomas Huth         r.B(7) = s->B((base << (SHIFT + 2)) + 3);                       \
1220fcf5ef2aSThomas Huth         XMM_ONLY(                                                       \
1221fcf5ef2aSThomas Huth                  r.B(8) = d->B((base << (SHIFT + 2)) + 4);              \
1222fcf5ef2aSThomas Huth                  r.B(9) = s->B((base << (SHIFT + 2)) + 4);              \
1223fcf5ef2aSThomas Huth                  r.B(10) = d->B((base << (SHIFT + 2)) + 5);             \
1224fcf5ef2aSThomas Huth                  r.B(11) = s->B((base << (SHIFT + 2)) + 5);             \
1225fcf5ef2aSThomas Huth                  r.B(12) = d->B((base << (SHIFT + 2)) + 6);             \
1226fcf5ef2aSThomas Huth                  r.B(13) = s->B((base << (SHIFT + 2)) + 6);             \
1227fcf5ef2aSThomas Huth                  r.B(14) = d->B((base << (SHIFT + 2)) + 7);             \
1228fcf5ef2aSThomas Huth                  r.B(15) = s->B((base << (SHIFT + 2)) + 7);             \
1229fcf5ef2aSThomas Huth                                                                       ) \
1230fcf5ef2aSThomas Huth             *d = r;                                                     \
1231fcf5ef2aSThomas Huth     }                                                                   \
1232fcf5ef2aSThomas Huth                                                                         \
1233fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1234fcf5ef2aSThomas Huth                                                         Reg *d, Reg *s) \
1235fcf5ef2aSThomas Huth     {                                                                   \
1236fcf5ef2aSThomas Huth         Reg r;                                                          \
1237fcf5ef2aSThomas Huth                                                                         \
1238fcf5ef2aSThomas Huth         r.W(0) = d->W((base << (SHIFT + 1)) + 0);                       \
1239fcf5ef2aSThomas Huth         r.W(1) = s->W((base << (SHIFT + 1)) + 0);                       \
1240fcf5ef2aSThomas Huth         r.W(2) = d->W((base << (SHIFT + 1)) + 1);                       \
1241fcf5ef2aSThomas Huth         r.W(3) = s->W((base << (SHIFT + 1)) + 1);                       \
1242fcf5ef2aSThomas Huth         XMM_ONLY(                                                       \
1243fcf5ef2aSThomas Huth                  r.W(4) = d->W((base << (SHIFT + 1)) + 2);              \
1244fcf5ef2aSThomas Huth                  r.W(5) = s->W((base << (SHIFT + 1)) + 2);              \
1245fcf5ef2aSThomas Huth                  r.W(6) = d->W((base << (SHIFT + 1)) + 3);              \
1246fcf5ef2aSThomas Huth                  r.W(7) = s->W((base << (SHIFT + 1)) + 3);              \
1247fcf5ef2aSThomas Huth                                                                       ) \
1248fcf5ef2aSThomas Huth             *d = r;                                                     \
1249fcf5ef2aSThomas Huth     }                                                                   \
1250fcf5ef2aSThomas Huth                                                                         \
1251fcf5ef2aSThomas Huth     void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1252fcf5ef2aSThomas Huth                                                         Reg *d, Reg *s) \
1253fcf5ef2aSThomas Huth     {                                                                   \
1254fcf5ef2aSThomas Huth         Reg r;                                                          \
1255fcf5ef2aSThomas Huth                                                                         \
1256fcf5ef2aSThomas Huth         r.L(0) = d->L((base << SHIFT) + 0);                             \
1257fcf5ef2aSThomas Huth         r.L(1) = s->L((base << SHIFT) + 0);                             \
1258fcf5ef2aSThomas Huth         XMM_ONLY(                                                       \
1259fcf5ef2aSThomas Huth                  r.L(2) = d->L((base << SHIFT) + 1);                    \
1260fcf5ef2aSThomas Huth                  r.L(3) = s->L((base << SHIFT) + 1);                    \
1261fcf5ef2aSThomas Huth                                                                       ) \
1262fcf5ef2aSThomas Huth             *d = r;                                                     \
1263fcf5ef2aSThomas Huth     }                                                                   \
1264fcf5ef2aSThomas Huth                                                                         \
1265fcf5ef2aSThomas Huth     XMM_ONLY(                                                           \
1266fcf5ef2aSThomas Huth              void glue(helper_punpck ## base_name ## qdq, SUFFIX)(CPUX86State \
1267fcf5ef2aSThomas Huth                                                                   *env, \
1268fcf5ef2aSThomas Huth                                                                   Reg *d, \
1269fcf5ef2aSThomas Huth                                                                   Reg *s) \
1270fcf5ef2aSThomas Huth              {                                                          \
1271fcf5ef2aSThomas Huth                  Reg r;                                                 \
1272fcf5ef2aSThomas Huth                                                                         \
1273fcf5ef2aSThomas Huth                  r.Q(0) = d->Q(base);                                   \
1274fcf5ef2aSThomas Huth                  r.Q(1) = s->Q(base);                                   \
1275fcf5ef2aSThomas Huth                  *d = r;                                                \
1276fcf5ef2aSThomas Huth              }                                                          \
1277fcf5ef2aSThomas Huth                                                                         )
1278fcf5ef2aSThomas Huth 
1279fcf5ef2aSThomas Huth UNPCK_OP(l, 0)
1280fcf5ef2aSThomas Huth UNPCK_OP(h, 1)
1281fcf5ef2aSThomas Huth 
1282fcf5ef2aSThomas Huth /* 3DNow! float ops */
1283fcf5ef2aSThomas Huth #if SHIFT == 0
1284fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s)
1285fcf5ef2aSThomas Huth {
1286fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status);
1287fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status);
1288fcf5ef2aSThomas Huth }
1289fcf5ef2aSThomas Huth 
1290fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s)
1291fcf5ef2aSThomas Huth {
1292fcf5ef2aSThomas Huth     d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status);
1293fcf5ef2aSThomas Huth     d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status);
1294fcf5ef2aSThomas Huth }
1295fcf5ef2aSThomas Huth 
1296fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s)
1297fcf5ef2aSThomas Huth {
1298fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status);
1299fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status);
1300fcf5ef2aSThomas Huth }
1301fcf5ef2aSThomas Huth 
1302fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s)
1303fcf5ef2aSThomas Huth {
1304fcf5ef2aSThomas Huth     d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0),
1305fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1306fcf5ef2aSThomas Huth     d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1),
1307fcf5ef2aSThomas Huth                                                        &env->mmx_status));
1308fcf5ef2aSThomas Huth }
1309fcf5ef2aSThomas Huth 
1310fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1311fcf5ef2aSThomas Huth {
1312fcf5ef2aSThomas Huth     MMXReg r;
1313fcf5ef2aSThomas Huth 
1314fcf5ef2aSThomas Huth     r.MMX_S(0) = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1315fcf5ef2aSThomas Huth     r.MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1316fcf5ef2aSThomas Huth     *d = r;
1317fcf5ef2aSThomas Huth }
1318fcf5ef2aSThomas Huth 
1319fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s)
1320fcf5ef2aSThomas Huth {
1321fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1322fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1323fcf5ef2aSThomas Huth }
1324fcf5ef2aSThomas Huth 
1325fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s)
1326fcf5ef2aSThomas Huth {
1327fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0),
1328fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1329fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1),
1330fcf5ef2aSThomas Huth                                    &env->mmx_status) ? -1 : 0;
1331fcf5ef2aSThomas Huth }
1332fcf5ef2aSThomas Huth 
1333fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s)
1334fcf5ef2aSThomas Huth {
1335fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0),
1336fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1337fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1),
1338fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1339fcf5ef2aSThomas Huth }
1340fcf5ef2aSThomas Huth 
1341fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s)
1342fcf5ef2aSThomas Huth {
1343fcf5ef2aSThomas Huth     d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0),
1344fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1345fcf5ef2aSThomas Huth     d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1),
1346fcf5ef2aSThomas Huth                              &env->mmx_status) ? -1 : 0;
1347fcf5ef2aSThomas Huth }
1348fcf5ef2aSThomas Huth 
1349fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s)
1350fcf5ef2aSThomas Huth {
1351fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) {
1352fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1353fcf5ef2aSThomas Huth     }
1354fcf5ef2aSThomas Huth     if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) {
1355fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1356fcf5ef2aSThomas Huth     }
1357fcf5ef2aSThomas Huth }
1358fcf5ef2aSThomas Huth 
1359fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s)
1360fcf5ef2aSThomas Huth {
1361fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) {
1362fcf5ef2aSThomas Huth         d->MMX_S(0) = s->MMX_S(0);
1363fcf5ef2aSThomas Huth     }
1364fcf5ef2aSThomas Huth     if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) {
1365fcf5ef2aSThomas Huth         d->MMX_S(1) = s->MMX_S(1);
1366fcf5ef2aSThomas Huth     }
1367fcf5ef2aSThomas Huth }
1368fcf5ef2aSThomas Huth 
1369fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s)
1370fcf5ef2aSThomas Huth {
1371fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1372fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1373fcf5ef2aSThomas Huth }
1374fcf5ef2aSThomas Huth 
1375fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1376fcf5ef2aSThomas Huth {
1377fcf5ef2aSThomas Huth     MMXReg r;
1378fcf5ef2aSThomas Huth 
1379fcf5ef2aSThomas Huth     r.MMX_S(0) = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1380fcf5ef2aSThomas Huth     r.MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1381fcf5ef2aSThomas Huth     *d = r;
1382fcf5ef2aSThomas Huth }
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s)
1385fcf5ef2aSThomas Huth {
1386fcf5ef2aSThomas Huth     MMXReg r;
1387fcf5ef2aSThomas Huth 
1388fcf5ef2aSThomas Huth     r.MMX_S(0) = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status);
1389fcf5ef2aSThomas Huth     r.MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status);
1390fcf5ef2aSThomas Huth     *d = r;
1391fcf5ef2aSThomas Huth }
1392fcf5ef2aSThomas Huth 
1393fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s)
1394fcf5ef2aSThomas Huth {
1395fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status);
1396fcf5ef2aSThomas Huth     d->MMX_S(1) = d->MMX_S(0);
1397fcf5ef2aSThomas Huth }
1398fcf5ef2aSThomas Huth 
1399fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s)
1400fcf5ef2aSThomas Huth {
1401fcf5ef2aSThomas Huth     d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff;
1402fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_div(float32_one,
1403fcf5ef2aSThomas Huth                               float32_sqrt(d->MMX_S(1), &env->mmx_status),
1404fcf5ef2aSThomas Huth                               &env->mmx_status);
1405fcf5ef2aSThomas Huth     d->MMX_L(1) |= s->MMX_L(0) & 0x80000000;
1406fcf5ef2aSThomas Huth     d->MMX_L(0) = d->MMX_L(1);
1407fcf5ef2aSThomas Huth }
1408fcf5ef2aSThomas Huth 
1409fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s)
1410fcf5ef2aSThomas Huth {
1411fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status);
1412fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status);
1413fcf5ef2aSThomas Huth }
1414fcf5ef2aSThomas Huth 
1415fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s)
1416fcf5ef2aSThomas Huth {
1417fcf5ef2aSThomas Huth     d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status);
1418fcf5ef2aSThomas Huth     d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status);
1419fcf5ef2aSThomas Huth }
1420fcf5ef2aSThomas Huth 
1421fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s)
1422fcf5ef2aSThomas Huth {
1423fcf5ef2aSThomas Huth     MMXReg r;
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth     r.MMX_L(0) = s->MMX_L(1);
1426fcf5ef2aSThomas Huth     r.MMX_L(1) = s->MMX_L(0);
1427fcf5ef2aSThomas Huth     *d = r;
1428fcf5ef2aSThomas Huth }
1429fcf5ef2aSThomas Huth #endif
1430fcf5ef2aSThomas Huth 
1431fcf5ef2aSThomas Huth /* SSSE3 op helpers */
1432fcf5ef2aSThomas Huth void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1433fcf5ef2aSThomas Huth {
1434fcf5ef2aSThomas Huth     int i;
1435fcf5ef2aSThomas Huth     Reg r;
1436fcf5ef2aSThomas Huth 
1437fcf5ef2aSThomas Huth     for (i = 0; i < (8 << SHIFT); i++) {
1438fcf5ef2aSThomas Huth         r.B(i) = (s->B(i) & 0x80) ? 0 : (d->B(s->B(i) & ((8 << SHIFT) - 1)));
1439fcf5ef2aSThomas Huth     }
1440fcf5ef2aSThomas Huth 
1441fcf5ef2aSThomas Huth     *d = r;
1442fcf5ef2aSThomas Huth }
1443fcf5ef2aSThomas Huth 
1444fcf5ef2aSThomas Huth void glue(helper_phaddw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1445fcf5ef2aSThomas Huth {
14462dfbea1aSJanne Grunau 
14472dfbea1aSJanne Grunau     Reg r;
14482dfbea1aSJanne Grunau 
14492dfbea1aSJanne Grunau     r.W(0) = (int16_t)d->W(0) + (int16_t)d->W(1);
14502dfbea1aSJanne Grunau     r.W(1) = (int16_t)d->W(2) + (int16_t)d->W(3);
14512dfbea1aSJanne Grunau     XMM_ONLY(r.W(2) = (int16_t)d->W(4) + (int16_t)d->W(5));
14522dfbea1aSJanne Grunau     XMM_ONLY(r.W(3) = (int16_t)d->W(6) + (int16_t)d->W(7));
14532dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 0) = (int16_t)s->W(0) + (int16_t)s->W(1);
14542dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 1) = (int16_t)s->W(2) + (int16_t)s->W(3);
14552dfbea1aSJanne Grunau     XMM_ONLY(r.W(6) = (int16_t)s->W(4) + (int16_t)s->W(5));
14562dfbea1aSJanne Grunau     XMM_ONLY(r.W(7) = (int16_t)s->W(6) + (int16_t)s->W(7));
14572dfbea1aSJanne Grunau 
14582dfbea1aSJanne Grunau     *d = r;
1459fcf5ef2aSThomas Huth }
1460fcf5ef2aSThomas Huth 
1461fcf5ef2aSThomas Huth void glue(helper_phaddd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1462fcf5ef2aSThomas Huth {
14632dfbea1aSJanne Grunau     Reg r;
14642dfbea1aSJanne Grunau 
14652dfbea1aSJanne Grunau     r.L(0) = (int32_t)d->L(0) + (int32_t)d->L(1);
14662dfbea1aSJanne Grunau     XMM_ONLY(r.L(1) = (int32_t)d->L(2) + (int32_t)d->L(3));
14672dfbea1aSJanne Grunau     r.L((1 << SHIFT) + 0) = (int32_t)s->L(0) + (int32_t)s->L(1);
14682dfbea1aSJanne Grunau     XMM_ONLY(r.L(3) = (int32_t)s->L(2) + (int32_t)s->L(3));
14692dfbea1aSJanne Grunau 
14702dfbea1aSJanne Grunau     *d = r;
1471fcf5ef2aSThomas Huth }
1472fcf5ef2aSThomas Huth 
1473fcf5ef2aSThomas Huth void glue(helper_phaddsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1474fcf5ef2aSThomas Huth {
14752dfbea1aSJanne Grunau     Reg r;
14762dfbea1aSJanne Grunau 
14772dfbea1aSJanne Grunau     r.W(0) = satsw((int16_t)d->W(0) + (int16_t)d->W(1));
14782dfbea1aSJanne Grunau     r.W(1) = satsw((int16_t)d->W(2) + (int16_t)d->W(3));
14792dfbea1aSJanne Grunau     XMM_ONLY(r.W(2) = satsw((int16_t)d->W(4) + (int16_t)d->W(5)));
14802dfbea1aSJanne Grunau     XMM_ONLY(r.W(3) = satsw((int16_t)d->W(6) + (int16_t)d->W(7)));
14812dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) + (int16_t)s->W(1));
14822dfbea1aSJanne Grunau     r.W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) + (int16_t)s->W(3));
14832dfbea1aSJanne Grunau     XMM_ONLY(r.W(6) = satsw((int16_t)s->W(4) + (int16_t)s->W(5)));
14842dfbea1aSJanne Grunau     XMM_ONLY(r.W(7) = satsw((int16_t)s->W(6) + (int16_t)s->W(7)));
14852dfbea1aSJanne Grunau 
14862dfbea1aSJanne Grunau     *d = r;
1487fcf5ef2aSThomas Huth }
1488fcf5ef2aSThomas Huth 
1489fcf5ef2aSThomas Huth void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1490fcf5ef2aSThomas Huth {
1491fcf5ef2aSThomas Huth     d->W(0) = satsw((int8_t)s->B(0) * (uint8_t)d->B(0) +
1492fcf5ef2aSThomas Huth                     (int8_t)s->B(1) * (uint8_t)d->B(1));
1493fcf5ef2aSThomas Huth     d->W(1) = satsw((int8_t)s->B(2) * (uint8_t)d->B(2) +
1494fcf5ef2aSThomas Huth                     (int8_t)s->B(3) * (uint8_t)d->B(3));
1495fcf5ef2aSThomas Huth     d->W(2) = satsw((int8_t)s->B(4) * (uint8_t)d->B(4) +
1496fcf5ef2aSThomas Huth                     (int8_t)s->B(5) * (uint8_t)d->B(5));
1497fcf5ef2aSThomas Huth     d->W(3) = satsw((int8_t)s->B(6) * (uint8_t)d->B(6) +
1498fcf5ef2aSThomas Huth                     (int8_t)s->B(7) * (uint8_t)d->B(7));
1499fcf5ef2aSThomas Huth #if SHIFT == 1
1500fcf5ef2aSThomas Huth     d->W(4) = satsw((int8_t)s->B(8) * (uint8_t)d->B(8) +
1501fcf5ef2aSThomas Huth                     (int8_t)s->B(9) * (uint8_t)d->B(9));
1502fcf5ef2aSThomas Huth     d->W(5) = satsw((int8_t)s->B(10) * (uint8_t)d->B(10) +
1503fcf5ef2aSThomas Huth                     (int8_t)s->B(11) * (uint8_t)d->B(11));
1504fcf5ef2aSThomas Huth     d->W(6) = satsw((int8_t)s->B(12) * (uint8_t)d->B(12) +
1505fcf5ef2aSThomas Huth                     (int8_t)s->B(13) * (uint8_t)d->B(13));
1506fcf5ef2aSThomas Huth     d->W(7) = satsw((int8_t)s->B(14) * (uint8_t)d->B(14) +
1507fcf5ef2aSThomas Huth                     (int8_t)s->B(15) * (uint8_t)d->B(15));
1508fcf5ef2aSThomas Huth #endif
1509fcf5ef2aSThomas Huth }
1510fcf5ef2aSThomas Huth 
1511fcf5ef2aSThomas Huth void glue(helper_phsubw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1512fcf5ef2aSThomas Huth {
1513fcf5ef2aSThomas Huth     d->W(0) = (int16_t)d->W(0) - (int16_t)d->W(1);
1514fcf5ef2aSThomas Huth     d->W(1) = (int16_t)d->W(2) - (int16_t)d->W(3);
1515fcf5ef2aSThomas Huth     XMM_ONLY(d->W(2) = (int16_t)d->W(4) - (int16_t)d->W(5));
1516fcf5ef2aSThomas Huth     XMM_ONLY(d->W(3) = (int16_t)d->W(6) - (int16_t)d->W(7));
1517fcf5ef2aSThomas Huth     d->W((2 << SHIFT) + 0) = (int16_t)s->W(0) - (int16_t)s->W(1);
1518fcf5ef2aSThomas Huth     d->W((2 << SHIFT) + 1) = (int16_t)s->W(2) - (int16_t)s->W(3);
1519fcf5ef2aSThomas Huth     XMM_ONLY(d->W(6) = (int16_t)s->W(4) - (int16_t)s->W(5));
1520fcf5ef2aSThomas Huth     XMM_ONLY(d->W(7) = (int16_t)s->W(6) - (int16_t)s->W(7));
1521fcf5ef2aSThomas Huth }
1522fcf5ef2aSThomas Huth 
1523fcf5ef2aSThomas Huth void glue(helper_phsubd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1524fcf5ef2aSThomas Huth {
1525fcf5ef2aSThomas Huth     d->L(0) = (int32_t)d->L(0) - (int32_t)d->L(1);
1526fcf5ef2aSThomas Huth     XMM_ONLY(d->L(1) = (int32_t)d->L(2) - (int32_t)d->L(3));
1527fcf5ef2aSThomas Huth     d->L((1 << SHIFT) + 0) = (int32_t)s->L(0) - (int32_t)s->L(1);
1528fcf5ef2aSThomas Huth     XMM_ONLY(d->L(3) = (int32_t)s->L(2) - (int32_t)s->L(3));
1529fcf5ef2aSThomas Huth }
1530fcf5ef2aSThomas Huth 
1531fcf5ef2aSThomas Huth void glue(helper_phsubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1532fcf5ef2aSThomas Huth {
1533fcf5ef2aSThomas Huth     d->W(0) = satsw((int16_t)d->W(0) - (int16_t)d->W(1));
1534fcf5ef2aSThomas Huth     d->W(1) = satsw((int16_t)d->W(2) - (int16_t)d->W(3));
1535fcf5ef2aSThomas Huth     XMM_ONLY(d->W(2) = satsw((int16_t)d->W(4) - (int16_t)d->W(5)));
1536fcf5ef2aSThomas Huth     XMM_ONLY(d->W(3) = satsw((int16_t)d->W(6) - (int16_t)d->W(7)));
1537fcf5ef2aSThomas Huth     d->W((2 << SHIFT) + 0) = satsw((int16_t)s->W(0) - (int16_t)s->W(1));
1538fcf5ef2aSThomas Huth     d->W((2 << SHIFT) + 1) = satsw((int16_t)s->W(2) - (int16_t)s->W(3));
1539fcf5ef2aSThomas Huth     XMM_ONLY(d->W(6) = satsw((int16_t)s->W(4) - (int16_t)s->W(5)));
1540fcf5ef2aSThomas Huth     XMM_ONLY(d->W(7) = satsw((int16_t)s->W(6) - (int16_t)s->W(7)));
1541fcf5ef2aSThomas Huth }
1542fcf5ef2aSThomas Huth 
1543fcf5ef2aSThomas Huth #define FABSB(_, x) (x > INT8_MAX  ? -(int8_t)x : x)
1544fcf5ef2aSThomas Huth #define FABSW(_, x) (x > INT16_MAX ? -(int16_t)x : x)
1545fcf5ef2aSThomas Huth #define FABSL(_, x) (x > INT32_MAX ? -(int32_t)x : x)
1546fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pabsb, FABSB)
1547fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pabsw, FABSW)
1548fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pabsd, FABSL)
1549fcf5ef2aSThomas Huth 
1550fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1551fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW)
1552fcf5ef2aSThomas Huth 
1553fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX  ? s ? d : 0 : -(int8_t)d)
1554fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1555fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1556fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB)
1557fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW)
1558fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL)
1559fcf5ef2aSThomas Huth 
1560fcf5ef2aSThomas Huth void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1561fcf5ef2aSThomas Huth                                   int32_t shift)
1562fcf5ef2aSThomas Huth {
1563fcf5ef2aSThomas Huth     Reg r;
1564fcf5ef2aSThomas Huth 
1565fcf5ef2aSThomas Huth     /* XXX could be checked during translation */
1566fcf5ef2aSThomas Huth     if (shift >= (16 << SHIFT)) {
1567fcf5ef2aSThomas Huth         r.Q(0) = 0;
1568fcf5ef2aSThomas Huth         XMM_ONLY(r.Q(1) = 0);
1569fcf5ef2aSThomas Huth     } else {
1570fcf5ef2aSThomas Huth         shift <<= 3;
1571fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1572fcf5ef2aSThomas Huth #if SHIFT == 0
1573fcf5ef2aSThomas Huth         r.Q(0) = SHR(s->Q(0), shift - 0) |
1574fcf5ef2aSThomas Huth             SHR(d->Q(0), shift -  64);
1575fcf5ef2aSThomas Huth #else
1576fcf5ef2aSThomas Huth         r.Q(0) = SHR(s->Q(0), shift - 0) |
1577fcf5ef2aSThomas Huth             SHR(s->Q(1), shift -  64) |
1578fcf5ef2aSThomas Huth             SHR(d->Q(0), shift - 128) |
1579fcf5ef2aSThomas Huth             SHR(d->Q(1), shift - 192);
1580fcf5ef2aSThomas Huth         r.Q(1) = SHR(s->Q(0), shift + 64) |
1581fcf5ef2aSThomas Huth             SHR(s->Q(1), shift -   0) |
1582fcf5ef2aSThomas Huth             SHR(d->Q(0), shift -  64) |
1583fcf5ef2aSThomas Huth             SHR(d->Q(1), shift - 128);
1584fcf5ef2aSThomas Huth #endif
1585fcf5ef2aSThomas Huth #undef SHR
1586fcf5ef2aSThomas Huth     }
1587fcf5ef2aSThomas Huth 
1588fcf5ef2aSThomas Huth     *d = r;
1589fcf5ef2aSThomas Huth }
1590fcf5ef2aSThomas Huth 
1591fcf5ef2aSThomas Huth #define XMM0 (env->xmm_regs[0])
1592fcf5ef2aSThomas Huth 
1593fcf5ef2aSThomas Huth #if SHIFT == 1
1594fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F)                                \
1595fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)           \
1596fcf5ef2aSThomas Huth     {                                                                   \
1597fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0));           \
1598fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1));           \
1599fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1600fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2));       \
1601fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3));       \
1602fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1603fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4));   \
1604fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5));   \
1605fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6));   \
1606fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7));   \
1607fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1608fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \
1609fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \
1610fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \
1611fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \
1612fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \
1613fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \
1614fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \
1615fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \
1616fcf5ef2aSThomas Huth                 }                                                       \
1617fcf5ef2aSThomas Huth             }                                                           \
1618fcf5ef2aSThomas Huth         }                                                               \
1619fcf5ef2aSThomas Huth     }
1620fcf5ef2aSThomas Huth 
1621fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F)                                \
1622fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \
1623fcf5ef2aSThomas Huth     {                                                                   \
1624fcf5ef2aSThomas Huth         d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1));       \
1625fcf5ef2aSThomas Huth         d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1));       \
1626fcf5ef2aSThomas Huth         if (num > 2) {                                                  \
1627fcf5ef2aSThomas Huth             d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1));   \
1628fcf5ef2aSThomas Huth             d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1));   \
1629fcf5ef2aSThomas Huth             if (num > 4) {                                              \
1630fcf5ef2aSThomas Huth                 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \
1631fcf5ef2aSThomas Huth                 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \
1632fcf5ef2aSThomas Huth                 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \
1633fcf5ef2aSThomas Huth                 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \
1634fcf5ef2aSThomas Huth                 if (num > 8) {                                          \
1635fcf5ef2aSThomas Huth                     d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \
1636fcf5ef2aSThomas Huth                     d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \
1637fcf5ef2aSThomas Huth                     d->elem(10) = F(d->elem(10), s->elem(10),           \
1638fcf5ef2aSThomas Huth                                     ((imm >> 10) & 1));                 \
1639fcf5ef2aSThomas Huth                     d->elem(11) = F(d->elem(11), s->elem(11),           \
1640fcf5ef2aSThomas Huth                                     ((imm >> 11) & 1));                 \
1641fcf5ef2aSThomas Huth                     d->elem(12) = F(d->elem(12), s->elem(12),           \
1642fcf5ef2aSThomas Huth                                     ((imm >> 12) & 1));                 \
1643fcf5ef2aSThomas Huth                     d->elem(13) = F(d->elem(13), s->elem(13),           \
1644fcf5ef2aSThomas Huth                                     ((imm >> 13) & 1));                 \
1645fcf5ef2aSThomas Huth                     d->elem(14) = F(d->elem(14), s->elem(14),           \
1646fcf5ef2aSThomas Huth                                     ((imm >> 14) & 1));                 \
1647fcf5ef2aSThomas Huth                     d->elem(15) = F(d->elem(15), s->elem(15),           \
1648fcf5ef2aSThomas Huth                                     ((imm >> 15) & 1));                 \
1649fcf5ef2aSThomas Huth                 }                                                       \
1650fcf5ef2aSThomas Huth             }                                                           \
1651fcf5ef2aSThomas Huth         }                                                               \
1652fcf5ef2aSThomas Huth     }
1653fcf5ef2aSThomas Huth 
1654fcf5ef2aSThomas Huth /* SSE4.1 op helpers */
1655fcf5ef2aSThomas Huth #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d)
1656fcf5ef2aSThomas Huth #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d)
1657fcf5ef2aSThomas Huth #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d)
1658fcf5ef2aSThomas Huth SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB)
1659fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS)
1660fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD)
1661fcf5ef2aSThomas Huth 
1662fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1663fcf5ef2aSThomas Huth {
1664fcf5ef2aSThomas Huth     uint64_t zf = (s->Q(0) &  d->Q(0)) | (s->Q(1) &  d->Q(1));
1665fcf5ef2aSThomas Huth     uint64_t cf = (s->Q(0) & ~d->Q(0)) | (s->Q(1) & ~d->Q(1));
1666fcf5ef2aSThomas Huth 
1667fcf5ef2aSThomas Huth     CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C);
1668fcf5ef2aSThomas Huth }
1669fcf5ef2aSThomas Huth 
1670fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F)        \
1671fcf5ef2aSThomas Huth     void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)     \
1672fcf5ef2aSThomas Huth     {                                           \
1673fcf5ef2aSThomas Huth         if (num > 2) {                          \
1674fcf5ef2aSThomas Huth             if (num > 4) {                      \
1675fcf5ef2aSThomas Huth                 d->elem(7) = F(7);              \
1676c6a56c8eSJoseph Myers                 d->elem(6) = F(6);              \
1677c6a56c8eSJoseph Myers                 d->elem(5) = F(5);              \
1678c6a56c8eSJoseph Myers                 d->elem(4) = F(4);              \
1679fcf5ef2aSThomas Huth             }                                   \
1680c6a56c8eSJoseph Myers             d->elem(3) = F(3);                  \
1681c6a56c8eSJoseph Myers             d->elem(2) = F(2);                  \
1682fcf5ef2aSThomas Huth         }                                       \
1683c6a56c8eSJoseph Myers         d->elem(1) = F(1);                      \
1684c6a56c8eSJoseph Myers         d->elem(0) = F(0);                      \
1685fcf5ef2aSThomas Huth     }
1686fcf5ef2aSThomas Huth 
1687fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxbw, W, 8, (int8_t) s->B)
1688fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxbd, L, 4, (int8_t) s->B)
1689fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxbq, Q, 2, (int8_t) s->B)
1690fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxwd, L, 4, (int16_t) s->W)
1691fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxwq, Q, 2, (int16_t) s->W)
1692fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovsxdq, Q, 2, (int32_t) s->L)
1693fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxbw, W, 8, s->B)
1694fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxbd, L, 4, s->B)
1695fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxbq, Q, 2, s->B)
1696fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxwd, L, 4, s->W)
1697fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxwq, Q, 2, s->W)
1698fcf5ef2aSThomas Huth SSE_HELPER_F(helper_pmovzxdq, Q, 2, s->L)
1699fcf5ef2aSThomas Huth 
1700fcf5ef2aSThomas Huth void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1701fcf5ef2aSThomas Huth {
1702fcf5ef2aSThomas Huth     d->Q(0) = (int64_t)(int32_t) d->L(0) * (int32_t) s->L(0);
1703fcf5ef2aSThomas Huth     d->Q(1) = (int64_t)(int32_t) d->L(2) * (int32_t) s->L(2);
1704fcf5ef2aSThomas Huth }
1705fcf5ef2aSThomas Huth 
1706fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1707fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ)
1708fcf5ef2aSThomas Huth 
1709fcf5ef2aSThomas Huth void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1710fcf5ef2aSThomas Huth {
171180e19606SJoseph Myers     Reg r;
171280e19606SJoseph Myers 
171380e19606SJoseph Myers     r.W(0) = satuw((int32_t) d->L(0));
171480e19606SJoseph Myers     r.W(1) = satuw((int32_t) d->L(1));
171580e19606SJoseph Myers     r.W(2) = satuw((int32_t) d->L(2));
171680e19606SJoseph Myers     r.W(3) = satuw((int32_t) d->L(3));
171780e19606SJoseph Myers     r.W(4) = satuw((int32_t) s->L(0));
171880e19606SJoseph Myers     r.W(5) = satuw((int32_t) s->L(1));
171980e19606SJoseph Myers     r.W(6) = satuw((int32_t) s->L(2));
172080e19606SJoseph Myers     r.W(7) = satuw((int32_t) s->L(3));
172180e19606SJoseph Myers     *d = r;
1722fcf5ef2aSThomas Huth }
1723fcf5ef2aSThomas Huth 
1724fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1725fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1726fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1727fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1728fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB)
1729fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD)
1730fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN)
1731fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN)
1732fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB)
1733fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD)
1734fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX)
1735fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX)
1736fcf5ef2aSThomas Huth 
1737fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1738fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD)
1739fcf5ef2aSThomas Huth 
1740fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
1741fcf5ef2aSThomas Huth {
1742fcf5ef2aSThomas Huth     int idx = 0;
1743fcf5ef2aSThomas Huth 
1744fcf5ef2aSThomas Huth     if (s->W(1) < s->W(idx)) {
1745fcf5ef2aSThomas Huth         idx = 1;
1746fcf5ef2aSThomas Huth     }
1747fcf5ef2aSThomas Huth     if (s->W(2) < s->W(idx)) {
1748fcf5ef2aSThomas Huth         idx = 2;
1749fcf5ef2aSThomas Huth     }
1750fcf5ef2aSThomas Huth     if (s->W(3) < s->W(idx)) {
1751fcf5ef2aSThomas Huth         idx = 3;
1752fcf5ef2aSThomas Huth     }
1753fcf5ef2aSThomas Huth     if (s->W(4) < s->W(idx)) {
1754fcf5ef2aSThomas Huth         idx = 4;
1755fcf5ef2aSThomas Huth     }
1756fcf5ef2aSThomas Huth     if (s->W(5) < s->W(idx)) {
1757fcf5ef2aSThomas Huth         idx = 5;
1758fcf5ef2aSThomas Huth     }
1759fcf5ef2aSThomas Huth     if (s->W(6) < s->W(idx)) {
1760fcf5ef2aSThomas Huth         idx = 6;
1761fcf5ef2aSThomas Huth     }
1762fcf5ef2aSThomas Huth     if (s->W(7) < s->W(idx)) {
1763fcf5ef2aSThomas Huth         idx = 7;
1764fcf5ef2aSThomas Huth     }
1765fcf5ef2aSThomas Huth 
1766fcf5ef2aSThomas Huth     d->W(0) = s->W(idx);
1767aa406feaSJoseph Myers     d->W(1) = idx;
1768aa406feaSJoseph Myers     d->L(1) = 0;
1769aa406feaSJoseph Myers     d->Q(1) = 0;
1770fcf5ef2aSThomas Huth }
1771fcf5ef2aSThomas Huth 
1772fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1773fcf5ef2aSThomas Huth                                   uint32_t mode)
1774fcf5ef2aSThomas Huth {
1775418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1776fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1777fcf5ef2aSThomas Huth 
1778fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1779fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1780fcf5ef2aSThomas Huth         switch (mode & 3) {
1781fcf5ef2aSThomas Huth         case 0:
1782fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1783fcf5ef2aSThomas Huth             break;
1784fcf5ef2aSThomas Huth         case 1:
1785fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1786fcf5ef2aSThomas Huth             break;
1787fcf5ef2aSThomas Huth         case 2:
1788fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1789fcf5ef2aSThomas Huth             break;
1790fcf5ef2aSThomas Huth         case 3:
1791fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1792fcf5ef2aSThomas Huth             break;
1793fcf5ef2aSThomas Huth         }
1794fcf5ef2aSThomas Huth     }
1795fcf5ef2aSThomas Huth 
1796fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1797fcf5ef2aSThomas Huth     d->ZMM_S(1) = float32_round_to_int(s->ZMM_S(1), &env->sse_status);
1798fcf5ef2aSThomas Huth     d->ZMM_S(2) = float32_round_to_int(s->ZMM_S(2), &env->sse_status);
1799fcf5ef2aSThomas Huth     d->ZMM_S(3) = float32_round_to_int(s->ZMM_S(3), &env->sse_status);
1800fcf5ef2aSThomas Huth 
1801418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1802fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1803fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1804fcf5ef2aSThomas Huth                                   &env->sse_status);
1805fcf5ef2aSThomas Huth     }
1806fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1807fcf5ef2aSThomas Huth }
1808fcf5ef2aSThomas Huth 
1809fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1810fcf5ef2aSThomas Huth                                   uint32_t mode)
1811fcf5ef2aSThomas Huth {
1812418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1813fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1814fcf5ef2aSThomas Huth 
1815fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1816fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1817fcf5ef2aSThomas Huth         switch (mode & 3) {
1818fcf5ef2aSThomas Huth         case 0:
1819fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1820fcf5ef2aSThomas Huth             break;
1821fcf5ef2aSThomas Huth         case 1:
1822fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1823fcf5ef2aSThomas Huth             break;
1824fcf5ef2aSThomas Huth         case 2:
1825fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1826fcf5ef2aSThomas Huth             break;
1827fcf5ef2aSThomas Huth         case 3:
1828fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1829fcf5ef2aSThomas Huth             break;
1830fcf5ef2aSThomas Huth         }
1831fcf5ef2aSThomas Huth     }
1832fcf5ef2aSThomas Huth 
1833fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1834fcf5ef2aSThomas Huth     d->ZMM_D(1) = float64_round_to_int(s->ZMM_D(1), &env->sse_status);
1835fcf5ef2aSThomas Huth 
1836418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1837fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1838fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1839fcf5ef2aSThomas Huth                                   &env->sse_status);
1840fcf5ef2aSThomas Huth     }
1841fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1842fcf5ef2aSThomas Huth }
1843fcf5ef2aSThomas Huth 
1844fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1845fcf5ef2aSThomas Huth                                   uint32_t mode)
1846fcf5ef2aSThomas Huth {
1847418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1848fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1849fcf5ef2aSThomas Huth 
1850fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1851fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1852fcf5ef2aSThomas Huth         switch (mode & 3) {
1853fcf5ef2aSThomas Huth         case 0:
1854fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1855fcf5ef2aSThomas Huth             break;
1856fcf5ef2aSThomas Huth         case 1:
1857fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1858fcf5ef2aSThomas Huth             break;
1859fcf5ef2aSThomas Huth         case 2:
1860fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1861fcf5ef2aSThomas Huth             break;
1862fcf5ef2aSThomas Huth         case 3:
1863fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1864fcf5ef2aSThomas Huth             break;
1865fcf5ef2aSThomas Huth         }
1866fcf5ef2aSThomas Huth     }
1867fcf5ef2aSThomas Huth 
1868fcf5ef2aSThomas Huth     d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status);
1869fcf5ef2aSThomas Huth 
1870418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1871fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1872fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1873fcf5ef2aSThomas Huth                                   &env->sse_status);
1874fcf5ef2aSThomas Huth     }
1875fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1876fcf5ef2aSThomas Huth }
1877fcf5ef2aSThomas Huth 
1878fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1879fcf5ef2aSThomas Huth                                   uint32_t mode)
1880fcf5ef2aSThomas Huth {
1881418b0f93SJoseph Myers     uint8_t old_flags = get_float_exception_flags(&env->sse_status);
1882fcf5ef2aSThomas Huth     signed char prev_rounding_mode;
1883fcf5ef2aSThomas Huth 
1884fcf5ef2aSThomas Huth     prev_rounding_mode = env->sse_status.float_rounding_mode;
1885fcf5ef2aSThomas Huth     if (!(mode & (1 << 2))) {
1886fcf5ef2aSThomas Huth         switch (mode & 3) {
1887fcf5ef2aSThomas Huth         case 0:
1888fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_nearest_even, &env->sse_status);
1889fcf5ef2aSThomas Huth             break;
1890fcf5ef2aSThomas Huth         case 1:
1891fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_down, &env->sse_status);
1892fcf5ef2aSThomas Huth             break;
1893fcf5ef2aSThomas Huth         case 2:
1894fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_up, &env->sse_status);
1895fcf5ef2aSThomas Huth             break;
1896fcf5ef2aSThomas Huth         case 3:
1897fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &env->sse_status);
1898fcf5ef2aSThomas Huth             break;
1899fcf5ef2aSThomas Huth         }
1900fcf5ef2aSThomas Huth     }
1901fcf5ef2aSThomas Huth 
1902fcf5ef2aSThomas Huth     d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status);
1903fcf5ef2aSThomas Huth 
1904418b0f93SJoseph Myers     if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) {
1905fcf5ef2aSThomas Huth         set_float_exception_flags(get_float_exception_flags(&env->sse_status) &
1906fcf5ef2aSThomas Huth                                   ~float_flag_inexact,
1907fcf5ef2aSThomas Huth                                   &env->sse_status);
1908fcf5ef2aSThomas Huth     }
1909fcf5ef2aSThomas Huth     env->sse_status.float_rounding_mode = prev_rounding_mode;
1910fcf5ef2aSThomas Huth }
1911fcf5ef2aSThomas Huth 
1912fcf5ef2aSThomas Huth #define FBLENDP(d, s, m) (m ? s : d)
1913fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendps, L, 4, FBLENDP)
1914fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP)
1915fcf5ef2aSThomas Huth SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)
1916fcf5ef2aSThomas Huth 
1917fcf5ef2aSThomas Huth void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1918fcf5ef2aSThomas Huth {
1919fcf5ef2aSThomas Huth     float32 iresult = float32_zero;
1920fcf5ef2aSThomas Huth 
1921fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1922fcf5ef2aSThomas Huth         iresult = float32_add(iresult,
1923fcf5ef2aSThomas Huth                               float32_mul(d->ZMM_S(0), s->ZMM_S(0),
1924fcf5ef2aSThomas Huth                                           &env->sse_status),
1925fcf5ef2aSThomas Huth                               &env->sse_status);
1926fcf5ef2aSThomas Huth     }
1927fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1928fcf5ef2aSThomas Huth         iresult = float32_add(iresult,
1929fcf5ef2aSThomas Huth                               float32_mul(d->ZMM_S(1), s->ZMM_S(1),
1930fcf5ef2aSThomas Huth                                           &env->sse_status),
1931fcf5ef2aSThomas Huth                               &env->sse_status);
1932fcf5ef2aSThomas Huth     }
1933fcf5ef2aSThomas Huth     if (mask & (1 << 6)) {
1934fcf5ef2aSThomas Huth         iresult = float32_add(iresult,
1935fcf5ef2aSThomas Huth                               float32_mul(d->ZMM_S(2), s->ZMM_S(2),
1936fcf5ef2aSThomas Huth                                           &env->sse_status),
1937fcf5ef2aSThomas Huth                               &env->sse_status);
1938fcf5ef2aSThomas Huth     }
1939fcf5ef2aSThomas Huth     if (mask & (1 << 7)) {
1940fcf5ef2aSThomas Huth         iresult = float32_add(iresult,
1941fcf5ef2aSThomas Huth                               float32_mul(d->ZMM_S(3), s->ZMM_S(3),
1942fcf5ef2aSThomas Huth                                           &env->sse_status),
1943fcf5ef2aSThomas Huth                               &env->sse_status);
1944fcf5ef2aSThomas Huth     }
1945fcf5ef2aSThomas Huth     d->ZMM_S(0) = (mask & (1 << 0)) ? iresult : float32_zero;
1946fcf5ef2aSThomas Huth     d->ZMM_S(1) = (mask & (1 << 1)) ? iresult : float32_zero;
1947fcf5ef2aSThomas Huth     d->ZMM_S(2) = (mask & (1 << 2)) ? iresult : float32_zero;
1948fcf5ef2aSThomas Huth     d->ZMM_S(3) = (mask & (1 << 3)) ? iresult : float32_zero;
1949fcf5ef2aSThomas Huth }
1950fcf5ef2aSThomas Huth 
1951fcf5ef2aSThomas Huth void glue(helper_dppd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask)
1952fcf5ef2aSThomas Huth {
1953fcf5ef2aSThomas Huth     float64 iresult = float64_zero;
1954fcf5ef2aSThomas Huth 
1955fcf5ef2aSThomas Huth     if (mask & (1 << 4)) {
1956fcf5ef2aSThomas Huth         iresult = float64_add(iresult,
1957fcf5ef2aSThomas Huth                               float64_mul(d->ZMM_D(0), s->ZMM_D(0),
1958fcf5ef2aSThomas Huth                                           &env->sse_status),
1959fcf5ef2aSThomas Huth                               &env->sse_status);
1960fcf5ef2aSThomas Huth     }
1961fcf5ef2aSThomas Huth     if (mask & (1 << 5)) {
1962fcf5ef2aSThomas Huth         iresult = float64_add(iresult,
1963fcf5ef2aSThomas Huth                               float64_mul(d->ZMM_D(1), s->ZMM_D(1),
1964fcf5ef2aSThomas Huth                                           &env->sse_status),
1965fcf5ef2aSThomas Huth                               &env->sse_status);
1966fcf5ef2aSThomas Huth     }
1967fcf5ef2aSThomas Huth     d->ZMM_D(0) = (mask & (1 << 0)) ? iresult : float64_zero;
1968fcf5ef2aSThomas Huth     d->ZMM_D(1) = (mask & (1 << 1)) ? iresult : float64_zero;
1969fcf5ef2aSThomas Huth }
1970fcf5ef2aSThomas Huth 
1971fcf5ef2aSThomas Huth void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
1972fcf5ef2aSThomas Huth                                   uint32_t offset)
1973fcf5ef2aSThomas Huth {
1974fcf5ef2aSThomas Huth     int s0 = (offset & 3) << 2;
1975fcf5ef2aSThomas Huth     int d0 = (offset & 4) << 0;
1976fcf5ef2aSThomas Huth     int i;
1977fcf5ef2aSThomas Huth     Reg r;
1978fcf5ef2aSThomas Huth 
1979fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++, d0++) {
1980fcf5ef2aSThomas Huth         r.W(i) = 0;
1981fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 0) - s->B(s0 + 0));
1982fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 1) - s->B(s0 + 1));
1983fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 2) - s->B(s0 + 2));
1984fcf5ef2aSThomas Huth         r.W(i) += abs1(d->B(d0 + 3) - s->B(s0 + 3));
1985fcf5ef2aSThomas Huth     }
1986fcf5ef2aSThomas Huth 
1987fcf5ef2aSThomas Huth     *d = r;
1988fcf5ef2aSThomas Huth }
1989fcf5ef2aSThomas Huth 
1990fcf5ef2aSThomas Huth /* SSE4.2 op helpers */
1991fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
1992fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
1993fcf5ef2aSThomas Huth 
1994fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
1995fcf5ef2aSThomas Huth {
1996fcf5ef2aSThomas Huth     int val;
1997fcf5ef2aSThomas Huth 
1998fcf5ef2aSThomas Huth     /* Presence of REX.W is indicated by a bit higher than 7 set */
1999fcf5ef2aSThomas Huth     if (ctrl >> 8) {
2000fcf5ef2aSThomas Huth         val = abs1((int64_t)env->regs[reg]);
2001fcf5ef2aSThomas Huth     } else {
2002fcf5ef2aSThomas Huth         val = abs1((int32_t)env->regs[reg]);
2003fcf5ef2aSThomas Huth     }
2004fcf5ef2aSThomas Huth 
2005fcf5ef2aSThomas Huth     if (ctrl & 1) {
2006fcf5ef2aSThomas Huth         if (val > 8) {
2007fcf5ef2aSThomas Huth             return 8;
2008fcf5ef2aSThomas Huth         }
2009fcf5ef2aSThomas Huth     } else {
2010fcf5ef2aSThomas Huth         if (val > 16) {
2011fcf5ef2aSThomas Huth             return 16;
2012fcf5ef2aSThomas Huth         }
2013fcf5ef2aSThomas Huth     }
2014fcf5ef2aSThomas Huth     return val;
2015fcf5ef2aSThomas Huth }
2016fcf5ef2aSThomas Huth 
2017fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
2018fcf5ef2aSThomas Huth {
2019fcf5ef2aSThomas Huth     int val = 0;
2020fcf5ef2aSThomas Huth 
2021fcf5ef2aSThomas Huth     if (ctrl & 1) {
2022fcf5ef2aSThomas Huth         while (val < 8 && r->W(val)) {
2023fcf5ef2aSThomas Huth             val++;
2024fcf5ef2aSThomas Huth         }
2025fcf5ef2aSThomas Huth     } else {
2026fcf5ef2aSThomas Huth         while (val < 16 && r->B(val)) {
2027fcf5ef2aSThomas Huth             val++;
2028fcf5ef2aSThomas Huth         }
2029fcf5ef2aSThomas Huth     }
2030fcf5ef2aSThomas Huth 
2031fcf5ef2aSThomas Huth     return val;
2032fcf5ef2aSThomas Huth }
2033fcf5ef2aSThomas Huth 
2034fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i)
2035fcf5ef2aSThomas Huth {
2036fcf5ef2aSThomas Huth     switch ((ctrl >> 0) & 3) {
2037fcf5ef2aSThomas Huth     case 0:
2038fcf5ef2aSThomas Huth         return r->B(i);
2039fcf5ef2aSThomas Huth     case 1:
2040fcf5ef2aSThomas Huth         return r->W(i);
2041fcf5ef2aSThomas Huth     case 2:
2042fcf5ef2aSThomas Huth         return (int8_t)r->B(i);
2043fcf5ef2aSThomas Huth     case 3:
2044fcf5ef2aSThomas Huth     default:
2045fcf5ef2aSThomas Huth         return (int16_t)r->W(i);
2046fcf5ef2aSThomas Huth     }
2047fcf5ef2aSThomas Huth }
2048fcf5ef2aSThomas Huth 
2049fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s,
2050fcf5ef2aSThomas Huth                                  int8_t ctrl, int valids, int validd)
2051fcf5ef2aSThomas Huth {
2052fcf5ef2aSThomas Huth     unsigned int res = 0;
2053fcf5ef2aSThomas Huth     int v;
2054fcf5ef2aSThomas Huth     int j, i;
2055fcf5ef2aSThomas Huth     int upper = (ctrl & 1) ? 7 : 15;
2056fcf5ef2aSThomas Huth 
2057fcf5ef2aSThomas Huth     valids--;
2058fcf5ef2aSThomas Huth     validd--;
2059fcf5ef2aSThomas Huth 
2060fcf5ef2aSThomas Huth     CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0);
2061fcf5ef2aSThomas Huth 
2062fcf5ef2aSThomas Huth     switch ((ctrl >> 2) & 3) {
2063fcf5ef2aSThomas Huth     case 0:
2064fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2065fcf5ef2aSThomas Huth             res <<= 1;
2066fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2067fcf5ef2aSThomas Huth             for (i = validd; i >= 0; i--) {
2068fcf5ef2aSThomas Huth                 res |= (v == pcmp_val(d, ctrl, i));
2069fcf5ef2aSThomas Huth             }
2070fcf5ef2aSThomas Huth         }
2071fcf5ef2aSThomas Huth         break;
2072fcf5ef2aSThomas Huth     case 1:
2073fcf5ef2aSThomas Huth         for (j = valids; j >= 0; j--) {
2074fcf5ef2aSThomas Huth             res <<= 1;
2075fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, j);
2076fcf5ef2aSThomas Huth             for (i = ((validd - 1) | 1); i >= 0; i -= 2) {
2077fcf5ef2aSThomas Huth                 res |= (pcmp_val(d, ctrl, i - 0) >= v &&
2078fcf5ef2aSThomas Huth                         pcmp_val(d, ctrl, i - 1) <= v);
2079fcf5ef2aSThomas Huth             }
2080fcf5ef2aSThomas Huth         }
2081fcf5ef2aSThomas Huth         break;
2082fcf5ef2aSThomas Huth     case 2:
2083fcf5ef2aSThomas Huth         res = (1 << (upper - MAX(valids, validd))) - 1;
2084fcf5ef2aSThomas Huth         res <<= MAX(valids, validd) - MIN(valids, validd);
2085fcf5ef2aSThomas Huth         for (i = MIN(valids, validd); i >= 0; i--) {
2086fcf5ef2aSThomas Huth             res <<= 1;
2087fcf5ef2aSThomas Huth             v = pcmp_val(s, ctrl, i);
2088fcf5ef2aSThomas Huth             res |= (v == pcmp_val(d, ctrl, i));
2089fcf5ef2aSThomas Huth         }
2090fcf5ef2aSThomas Huth         break;
2091fcf5ef2aSThomas Huth     case 3:
2092ae35eea7SJoseph Myers         if (validd == -1) {
2093ae35eea7SJoseph Myers             res = (2 << upper) - 1;
2094ae35eea7SJoseph Myers             break;
2095ae35eea7SJoseph Myers         }
2096bc921b27SJoseph Myers         for (j = valids == upper ? valids : valids - validd; j >= 0; j--) {
2097fcf5ef2aSThomas Huth             res <<= 1;
2098fcf5ef2aSThomas Huth             v = 1;
2099bc921b27SJoseph Myers             for (i = MIN(valids - j, validd); i >= 0; i--) {
2100fcf5ef2aSThomas Huth                 v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i));
2101fcf5ef2aSThomas Huth             }
2102fcf5ef2aSThomas Huth             res |= v;
2103fcf5ef2aSThomas Huth         }
2104fcf5ef2aSThomas Huth         break;
2105fcf5ef2aSThomas Huth     }
2106fcf5ef2aSThomas Huth 
2107fcf5ef2aSThomas Huth     switch ((ctrl >> 4) & 3) {
2108fcf5ef2aSThomas Huth     case 1:
2109fcf5ef2aSThomas Huth         res ^= (2 << upper) - 1;
2110fcf5ef2aSThomas Huth         break;
2111fcf5ef2aSThomas Huth     case 3:
2112fcf5ef2aSThomas Huth         res ^= (1 << (valids + 1)) - 1;
2113fcf5ef2aSThomas Huth         break;
2114fcf5ef2aSThomas Huth     }
2115fcf5ef2aSThomas Huth 
2116fcf5ef2aSThomas Huth     if (res) {
2117fcf5ef2aSThomas Huth         CC_SRC |= CC_C;
2118fcf5ef2aSThomas Huth     }
2119fcf5ef2aSThomas Huth     if (res & 1) {
2120fcf5ef2aSThomas Huth         CC_SRC |= CC_O;
2121fcf5ef2aSThomas Huth     }
2122fcf5ef2aSThomas Huth 
2123fcf5ef2aSThomas Huth     return res;
2124fcf5ef2aSThomas Huth }
2125fcf5ef2aSThomas Huth 
2126fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2127fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2128fcf5ef2aSThomas Huth {
2129fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2130fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2131fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2132fcf5ef2aSThomas Huth 
2133fcf5ef2aSThomas Huth     if (res) {
2134fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2135fcf5ef2aSThomas Huth     } else {
2136fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2137fcf5ef2aSThomas Huth     }
2138fcf5ef2aSThomas Huth }
2139fcf5ef2aSThomas Huth 
2140fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2141fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2142fcf5ef2aSThomas Huth {
2143fcf5ef2aSThomas Huth     int i;
2144fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2145fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EDX, ctrl),
2146fcf5ef2aSThomas Huth                                  pcmp_elen(env, R_EAX, ctrl));
2147fcf5ef2aSThomas Huth 
2148fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2149fcf5ef2aSThomas Huth         if (ctrl & 1) {
2150fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2151fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2152fcf5ef2aSThomas Huth             }
2153fcf5ef2aSThomas Huth         } else {
2154fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2155fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2156fcf5ef2aSThomas Huth             }
2157fcf5ef2aSThomas Huth         }
2158fcf5ef2aSThomas Huth     } else {
2159fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2160fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2161fcf5ef2aSThomas Huth     }
2162fcf5ef2aSThomas Huth }
2163fcf5ef2aSThomas Huth 
2164fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2165fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2166fcf5ef2aSThomas Huth {
2167fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2168fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2169fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2170fcf5ef2aSThomas Huth 
2171fcf5ef2aSThomas Huth     if (res) {
2172fcf5ef2aSThomas Huth         env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res);
2173fcf5ef2aSThomas Huth     } else {
2174fcf5ef2aSThomas Huth         env->regs[R_ECX] = 16 >> (ctrl & (1 << 0));
2175fcf5ef2aSThomas Huth     }
2176fcf5ef2aSThomas Huth }
2177fcf5ef2aSThomas Huth 
2178fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2179fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2180fcf5ef2aSThomas Huth {
2181fcf5ef2aSThomas Huth     int i;
2182fcf5ef2aSThomas Huth     unsigned int res = pcmpxstrx(env, d, s, ctrl,
2183fcf5ef2aSThomas Huth                                  pcmp_ilen(s, ctrl),
2184fcf5ef2aSThomas Huth                                  pcmp_ilen(d, ctrl));
2185fcf5ef2aSThomas Huth 
2186fcf5ef2aSThomas Huth     if ((ctrl >> 6) & 1) {
2187fcf5ef2aSThomas Huth         if (ctrl & 1) {
2188fcf5ef2aSThomas Huth             for (i = 0; i < 8; i++, res >>= 1) {
2189fcf5ef2aSThomas Huth                 env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0;
2190fcf5ef2aSThomas Huth             }
2191fcf5ef2aSThomas Huth         } else {
2192fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++, res >>= 1) {
2193fcf5ef2aSThomas Huth                 env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0;
2194fcf5ef2aSThomas Huth             }
2195fcf5ef2aSThomas Huth         }
2196fcf5ef2aSThomas Huth     } else {
2197fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(1) = 0;
2198fcf5ef2aSThomas Huth         env->xmm_regs[0].Q(0) = res;
2199fcf5ef2aSThomas Huth     }
2200fcf5ef2aSThomas Huth }
2201fcf5ef2aSThomas Huth 
2202fcf5ef2aSThomas Huth #define CRCPOLY        0x1edc6f41
2203fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78
2204fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len)
2205fcf5ef2aSThomas Huth {
2206fcf5ef2aSThomas Huth     target_ulong crc = (msg & ((target_ulong) -1 >>
2207fcf5ef2aSThomas Huth                                (TARGET_LONG_BITS - len))) ^ crc1;
2208fcf5ef2aSThomas Huth 
2209fcf5ef2aSThomas Huth     while (len--) {
2210fcf5ef2aSThomas Huth         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0);
2211fcf5ef2aSThomas Huth     }
2212fcf5ef2aSThomas Huth 
2213fcf5ef2aSThomas Huth     return crc;
2214fcf5ef2aSThomas Huth }
2215fcf5ef2aSThomas Huth 
2216fcf5ef2aSThomas Huth void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2217fcf5ef2aSThomas Huth                                     uint32_t ctrl)
2218fcf5ef2aSThomas Huth {
2219fcf5ef2aSThomas Huth     uint64_t ah, al, b, resh, resl;
2220fcf5ef2aSThomas Huth 
2221fcf5ef2aSThomas Huth     ah = 0;
2222fcf5ef2aSThomas Huth     al = d->Q((ctrl & 1) != 0);
2223fcf5ef2aSThomas Huth     b = s->Q((ctrl & 16) != 0);
2224fcf5ef2aSThomas Huth     resh = resl = 0;
2225fcf5ef2aSThomas Huth 
2226fcf5ef2aSThomas Huth     while (b) {
2227fcf5ef2aSThomas Huth         if (b & 1) {
2228fcf5ef2aSThomas Huth             resl ^= al;
2229fcf5ef2aSThomas Huth             resh ^= ah;
2230fcf5ef2aSThomas Huth         }
2231fcf5ef2aSThomas Huth         ah = (ah << 1) | (al >> 63);
2232fcf5ef2aSThomas Huth         al <<= 1;
2233fcf5ef2aSThomas Huth         b >>= 1;
2234fcf5ef2aSThomas Huth     }
2235fcf5ef2aSThomas Huth 
2236fcf5ef2aSThomas Huth     d->Q(0) = resl;
2237fcf5ef2aSThomas Huth     d->Q(1) = resh;
2238fcf5ef2aSThomas Huth }
2239fcf5ef2aSThomas Huth 
2240fcf5ef2aSThomas Huth void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2241fcf5ef2aSThomas Huth {
2242fcf5ef2aSThomas Huth     int i;
2243fcf5ef2aSThomas Huth     Reg st = *d;
2244fcf5ef2aSThomas Huth     Reg rk = *s;
2245fcf5ef2aSThomas Huth 
2246fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2247fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4*i+0])] ^
2248fcf5ef2aSThomas Huth                                     AES_Td1[st.B(AES_ishifts[4*i+1])] ^
2249fcf5ef2aSThomas Huth                                     AES_Td2[st.B(AES_ishifts[4*i+2])] ^
2250fcf5ef2aSThomas Huth                                     AES_Td3[st.B(AES_ishifts[4*i+3])]);
2251fcf5ef2aSThomas Huth     }
2252fcf5ef2aSThomas Huth }
2253fcf5ef2aSThomas Huth 
2254fcf5ef2aSThomas Huth void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2255fcf5ef2aSThomas Huth {
2256fcf5ef2aSThomas Huth     int i;
2257fcf5ef2aSThomas Huth     Reg st = *d;
2258fcf5ef2aSThomas Huth     Reg rk = *s;
2259fcf5ef2aSThomas Huth 
2260fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2261fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i])]);
2262fcf5ef2aSThomas Huth     }
2263fcf5ef2aSThomas Huth }
2264fcf5ef2aSThomas Huth 
2265fcf5ef2aSThomas Huth void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2266fcf5ef2aSThomas Huth {
2267fcf5ef2aSThomas Huth     int i;
2268fcf5ef2aSThomas Huth     Reg st = *d;
2269fcf5ef2aSThomas Huth     Reg rk = *s;
2270fcf5ef2aSThomas Huth 
2271fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2272fcf5ef2aSThomas Huth         d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4*i+0])] ^
2273fcf5ef2aSThomas Huth                                     AES_Te1[st.B(AES_shifts[4*i+1])] ^
2274fcf5ef2aSThomas Huth                                     AES_Te2[st.B(AES_shifts[4*i+2])] ^
2275fcf5ef2aSThomas Huth                                     AES_Te3[st.B(AES_shifts[4*i+3])]);
2276fcf5ef2aSThomas Huth     }
2277fcf5ef2aSThomas Huth }
2278fcf5ef2aSThomas Huth 
2279fcf5ef2aSThomas Huth void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2280fcf5ef2aSThomas Huth {
2281fcf5ef2aSThomas Huth     int i;
2282fcf5ef2aSThomas Huth     Reg st = *d;
2283fcf5ef2aSThomas Huth     Reg rk = *s;
2284fcf5ef2aSThomas Huth 
2285fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2286fcf5ef2aSThomas Huth         d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i])]);
2287fcf5ef2aSThomas Huth     }
2288fcf5ef2aSThomas Huth 
2289fcf5ef2aSThomas Huth }
2290fcf5ef2aSThomas Huth 
2291fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)
2292fcf5ef2aSThomas Huth {
2293fcf5ef2aSThomas Huth     int i;
2294fcf5ef2aSThomas Huth     Reg tmp = *s;
2295fcf5ef2aSThomas Huth 
2296fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2297fcf5ef2aSThomas Huth         d->L(i) = bswap32(AES_imc[tmp.B(4*i+0)][0] ^
2298fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+1)][1] ^
2299fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+2)][2] ^
2300fcf5ef2aSThomas Huth                           AES_imc[tmp.B(4*i+3)][3]);
2301fcf5ef2aSThomas Huth     }
2302fcf5ef2aSThomas Huth }
2303fcf5ef2aSThomas Huth 
2304fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,
2305fcf5ef2aSThomas Huth                                           uint32_t ctrl)
2306fcf5ef2aSThomas Huth {
2307fcf5ef2aSThomas Huth     int i;
2308fcf5ef2aSThomas Huth     Reg tmp = *s;
2309fcf5ef2aSThomas Huth 
2310fcf5ef2aSThomas Huth     for (i = 0 ; i < 4 ; i++) {
2311fcf5ef2aSThomas Huth         d->B(i) = AES_sbox[tmp.B(i + 4)];
2312fcf5ef2aSThomas Huth         d->B(i + 8) = AES_sbox[tmp.B(i + 12)];
2313fcf5ef2aSThomas Huth     }
2314fcf5ef2aSThomas Huth     d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl;
2315fcf5ef2aSThomas Huth     d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl;
2316fcf5ef2aSThomas Huth }
2317fcf5ef2aSThomas Huth #endif
2318fcf5ef2aSThomas Huth 
2319fcf5ef2aSThomas Huth #undef SHIFT
2320fcf5ef2aSThomas Huth #undef XMM_ONLY
2321fcf5ef2aSThomas Huth #undef Reg
2322fcf5ef2aSThomas Huth #undef B
2323fcf5ef2aSThomas Huth #undef W
2324fcf5ef2aSThomas Huth #undef L
2325fcf5ef2aSThomas Huth #undef Q
2326fcf5ef2aSThomas Huth #undef SUFFIX
2327