2 * MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support
4 * Copyright (c) 2005 Fabrice Bellard
5 * Copyright (c) 2008 Intel Corporation <andrew.zaborowski@intel.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 #define XMM_ONLY(...) __VA_ARGS__
38 void glue(helper_psrlw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
62 void glue(helper_psraw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
71 d
->W(0) = (int16_t)d
->W(0) >> shift
;
72 d
->W(1) = (int16_t)d
->W(1) >> shift
;
73 d
->W(2) = (int16_t)d
->W(2) >> shift
;
74 d
->W(3) = (int16_t)d
->W(3) >> shift
;
76 d
->W(4) = (int16_t)d
->W(4) >> shift
;
77 d
->W(5) = (int16_t)d
->W(5) >> shift
;
78 d
->W(6) = (int16_t)d
->W(6) >> shift
;
79 d
->W(7) = (int16_t)d
->W(7) >> shift
;
83 void glue(helper_psllw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
107 void glue(helper_psrld
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
127 void glue(helper_psrad
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
136 d
->L(0) = (int32_t)d
->L(0) >> shift
;
137 d
->L(1) = (int32_t)d
->L(1) >> shift
;
139 d
->L(2) = (int32_t)d
->L(2) >> shift
;
140 d
->L(3) = (int32_t)d
->L(3) >> shift
;
144 void glue(helper_pslld
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
164 void glue(helper_psrlq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
182 void glue(helper_psllq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
201 void glue(helper_psrldq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
209 for (i
= 0; i
< 16 - shift
; i
++) {
210 d
->B(i
) = d
->B(i
+ shift
);
212 for (i
= 16 - shift
; i
< 16; i
++) {
217 void glue(helper_pslldq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
225 for (i
= 15; i
>= shift
; i
--) {
226 d
->B(i
) = d
->B(i
- shift
);
228 for (i
= 0; i
< shift
; i
++) {
234 #define SSE_HELPER_B(name, F) \
235 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
237 d->B(0) = F(d->B(0), s->B(0)); \
238 d->B(1) = F(d->B(1), s->B(1)); \
239 d->B(2) = F(d->B(2), s->B(2)); \
240 d->B(3) = F(d->B(3), s->B(3)); \
241 d->B(4) = F(d->B(4), s->B(4)); \
242 d->B(5) = F(d->B(5), s->B(5)); \
243 d->B(6) = F(d->B(6), s->B(6)); \
244 d->B(7) = F(d->B(7), s->B(7)); \
246 d->B(8) = F(d->B(8), s->B(8)); \
247 d->B(9) = F(d->B(9), s->B(9)); \
248 d->B(10) = F(d->B(10), s->B(10)); \
249 d->B(11) = F(d->B(11), s->B(11)); \
250 d->B(12) = F(d->B(12), s->B(12)); \
251 d->B(13) = F(d->B(13), s->B(13)); \
252 d->B(14) = F(d->B(14), s->B(14)); \
253 d->B(15) = F(d->B(15), s->B(15)); \
257 #define SSE_HELPER_W(name, F) \
258 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
260 d->W(0) = F(d->W(0), s->W(0)); \
261 d->W(1) = F(d->W(1), s->W(1)); \
262 d->W(2) = F(d->W(2), s->W(2)); \
263 d->W(3) = F(d->W(3), s->W(3)); \
265 d->W(4) = F(d->W(4), s->W(4)); \
266 d->W(5) = F(d->W(5), s->W(5)); \
267 d->W(6) = F(d->W(6), s->W(6)); \
268 d->W(7) = F(d->W(7), s->W(7)); \
272 #define SSE_HELPER_L(name, F) \
273 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
275 d->L(0) = F(d->L(0), s->L(0)); \
276 d->L(1) = F(d->L(1), s->L(1)); \
278 d->L(2) = F(d->L(2), s->L(2)); \
279 d->L(3) = F(d->L(3), s->L(3)); \
283 #define SSE_HELPER_Q(name, F) \
284 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
286 d->Q(0) = F(d->Q(0), s->Q(0)); \
288 d->Q(1) = F(d->Q(1), s->Q(1)); \
293 static inline int satub(int x
)
297 } else if (x
> 255) {
304 static inline int satuw(int x
)
308 } else if (x
> 65535) {
315 static inline int satsb(int x
)
319 } else if (x
> 127) {
326 static inline int satsw(int x
)
330 } else if (x
> 32767) {
337 #define FADD(a, b) ((a) + (b))
338 #define FADDUB(a, b) satub((a) + (b))
339 #define FADDUW(a, b) satuw((a) + (b))
340 #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
341 #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
343 #define FSUB(a, b) ((a) - (b))
344 #define FSUBUB(a, b) satub((a) - (b))
345 #define FSUBUW(a, b) satuw((a) - (b))
346 #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
347 #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
348 #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
349 #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
350 #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
351 #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
353 #define FAND(a, b) ((a) & (b))
354 #define FANDN(a, b) ((~(a)) & (b))
355 #define FOR(a, b) ((a) | (b))
356 #define FXOR(a, b) ((a) ^ (b))
358 #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
359 #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
360 #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
361 #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
363 #define FMULLW(a, b) ((a) * (b))
364 #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
365 #define FMULHUW(a, b) ((a) * (b) >> 16)
366 #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
368 #define FAVG(a, b) (((a) + (b) + 1) >> 1)
371 SSE_HELPER_B(helper_paddb
, FADD
)
372 SSE_HELPER_W(helper_paddw
, FADD
)
373 SSE_HELPER_L(helper_paddl
, FADD
)
374 SSE_HELPER_Q(helper_paddq
, FADD
)
376 SSE_HELPER_B(helper_psubb
, FSUB
)
377 SSE_HELPER_W(helper_psubw
, FSUB
)
378 SSE_HELPER_L(helper_psubl
, FSUB
)
379 SSE_HELPER_Q(helper_psubq
, FSUB
)
381 SSE_HELPER_B(helper_paddusb
, FADDUB
)
382 SSE_HELPER_B(helper_paddsb
, FADDSB
)
383 SSE_HELPER_B(helper_psubusb
, FSUBUB
)
384 SSE_HELPER_B(helper_psubsb
, FSUBSB
)
386 SSE_HELPER_W(helper_paddusw
, FADDUW
)
387 SSE_HELPER_W(helper_paddsw
, FADDSW
)
388 SSE_HELPER_W(helper_psubusw
, FSUBUW
)
389 SSE_HELPER_W(helper_psubsw
, FSUBSW
)
391 SSE_HELPER_B(helper_pminub
, FMINUB
)
392 SSE_HELPER_B(helper_pmaxub
, FMAXUB
)
394 SSE_HELPER_W(helper_pminsw
, FMINSW
)
395 SSE_HELPER_W(helper_pmaxsw
, FMAXSW
)
397 SSE_HELPER_Q(helper_pand
, FAND
)
398 SSE_HELPER_Q(helper_pandn
, FANDN
)
399 SSE_HELPER_Q(helper_por
, FOR
)
400 SSE_HELPER_Q(helper_pxor
, FXOR
)
402 SSE_HELPER_B(helper_pcmpgtb
, FCMPGTB
)
403 SSE_HELPER_W(helper_pcmpgtw
, FCMPGTW
)
404 SSE_HELPER_L(helper_pcmpgtl
, FCMPGTL
)
406 SSE_HELPER_B(helper_pcmpeqb
, FCMPEQ
)
407 SSE_HELPER_W(helper_pcmpeqw
, FCMPEQ
)
408 SSE_HELPER_L(helper_pcmpeql
, FCMPEQ
)
410 SSE_HELPER_W(helper_pmullw
, FMULLW
)
412 SSE_HELPER_W(helper_pmulhrw
, FMULHRW
)
414 SSE_HELPER_W(helper_pmulhuw
, FMULHUW
)
415 SSE_HELPER_W(helper_pmulhw
, FMULHW
)
417 SSE_HELPER_B(helper_pavgb
, FAVG
)
418 SSE_HELPER_W(helper_pavgw
, FAVG
)
420 void glue(helper_pmuludq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
422 d
->Q(0) = (uint64_t)s
->L(0) * (uint64_t)d
->L(0);
424 d
->Q(1) = (uint64_t)s
->L(2) * (uint64_t)d
->L(2);
428 void glue(helper_pmaddwd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
432 for (i
= 0; i
< (2 << SHIFT
); i
++) {
433 d
->L(i
) = (int16_t)s
->W(2 * i
) * (int16_t)d
->W(2 * i
) +
434 (int16_t)s
->W(2 * i
+ 1) * (int16_t)d
->W(2 * i
+ 1);
439 static inline int abs1(int a
)
448 void glue(helper_psadbw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
453 val
+= abs1(d
->B(0) - s
->B(0));
454 val
+= abs1(d
->B(1) - s
->B(1));
455 val
+= abs1(d
->B(2) - s
->B(2));
456 val
+= abs1(d
->B(3) - s
->B(3));
457 val
+= abs1(d
->B(4) - s
->B(4));
458 val
+= abs1(d
->B(5) - s
->B(5));
459 val
+= abs1(d
->B(6) - s
->B(6));
460 val
+= abs1(d
->B(7) - s
->B(7));
464 val
+= abs1(d
->B(8) - s
->B(8));
465 val
+= abs1(d
->B(9) - s
->B(9));
466 val
+= abs1(d
->B(10) - s
->B(10));
467 val
+= abs1(d
->B(11) - s
->B(11));
468 val
+= abs1(d
->B(12) - s
->B(12));
469 val
+= abs1(d
->B(13) - s
->B(13));
470 val
+= abs1(d
->B(14) - s
->B(14));
471 val
+= abs1(d
->B(15) - s
->B(15));
476 void glue(helper_maskmov
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
481 for (i
= 0; i
< (8 << SHIFT
); i
++) {
482 if (s
->B(i
) & 0x80) {
483 cpu_stb_data(env
, a0
+ i
, d
->B(i
));
488 void glue(helper_movl_mm_T0
, SUFFIX
)(Reg
*d
, uint32_t val
)
498 void glue(helper_movq_mm_T0
, SUFFIX
)(Reg
*d
, uint64_t val
)
508 void glue(helper_pshufw
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
512 r
.W(0) = s
->W(order
& 3);
513 r
.W(1) = s
->W((order
>> 2) & 3);
514 r
.W(2) = s
->W((order
>> 4) & 3);
515 r
.W(3) = s
->W((order
>> 6) & 3);
519 void helper_shufps(Reg
*d
, Reg
*s
, int order
)
523 r
.L(0) = d
->L(order
& 3);
524 r
.L(1) = d
->L((order
>> 2) & 3);
525 r
.L(2) = s
->L((order
>> 4) & 3);
526 r
.L(3) = s
->L((order
>> 6) & 3);
530 void helper_shufpd(Reg
*d
, Reg
*s
, int order
)
534 r
.Q(0) = d
->Q(order
& 1);
535 r
.Q(1) = s
->Q((order
>> 1) & 1);
539 void glue(helper_pshufd
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
543 r
.L(0) = s
->L(order
& 3);
544 r
.L(1) = s
->L((order
>> 2) & 3);
545 r
.L(2) = s
->L((order
>> 4) & 3);
546 r
.L(3) = s
->L((order
>> 6) & 3);
550 void glue(helper_pshuflw
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
554 r
.W(0) = s
->W(order
& 3);
555 r
.W(1) = s
->W((order
>> 2) & 3);
556 r
.W(2) = s
->W((order
>> 4) & 3);
557 r
.W(3) = s
->W((order
>> 6) & 3);
562 void glue(helper_pshufhw
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
567 r
.W(4) = s
->W(4 + (order
& 3));
568 r
.W(5) = s
->W(4 + ((order
>> 2) & 3));
569 r
.W(6) = s
->W(4 + ((order
>> 4) & 3));
570 r
.W(7) = s
->W(4 + ((order
>> 6) & 3));
577 /* XXX: not accurate */
579 #define SSE_HELPER_S(name, F) \
580 void helper_ ## name ## ps(CPUX86State *env, Reg *d, Reg *s) \
582 d->XMM_S(0) = F(32, d->XMM_S(0), s->XMM_S(0)); \
583 d->XMM_S(1) = F(32, d->XMM_S(1), s->XMM_S(1)); \
584 d->XMM_S(2) = F(32, d->XMM_S(2), s->XMM_S(2)); \
585 d->XMM_S(3) = F(32, d->XMM_S(3), s->XMM_S(3)); \
588 void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s) \
590 d->XMM_S(0) = F(32, d->XMM_S(0), s->XMM_S(0)); \
593 void helper_ ## name ## pd(CPUX86State *env, Reg *d, Reg *s) \
595 d->XMM_D(0) = F(64, d->XMM_D(0), s->XMM_D(0)); \
596 d->XMM_D(1) = F(64, d->XMM_D(1), s->XMM_D(1)); \
599 void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s) \
601 d->XMM_D(0) = F(64, d->XMM_D(0), s->XMM_D(0)); \
604 #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
605 #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
606 #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
607 #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
608 #define FPU_SQRT(size, a, b) float ## size ## _sqrt(b, &env->sse_status)
610 /* Note that the choice of comparison op here is important to get the
611 * special cases right: for min and max Intel specifies that (-0,0),
612 * (NaN, anything) and (anything, NaN) return the second argument.
614 #define FPU_MIN(size, a, b) \
615 (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
616 #define FPU_MAX(size, a, b) \
617 (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
619 SSE_HELPER_S(add
, FPU_ADD
)
620 SSE_HELPER_S(sub
, FPU_SUB
)
621 SSE_HELPER_S(mul
, FPU_MUL
)
622 SSE_HELPER_S(div
, FPU_DIV
)
623 SSE_HELPER_S(min
, FPU_MIN
)
624 SSE_HELPER_S(max
, FPU_MAX
)
625 SSE_HELPER_S(sqrt
, FPU_SQRT
)
628 /* float to float conversions */
629 void helper_cvtps2pd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
635 d
->XMM_D(0) = float32_to_float64(s0
, &env
->sse_status
);
636 d
->XMM_D(1) = float32_to_float64(s1
, &env
->sse_status
);
639 void helper_cvtpd2ps(CPUX86State
*env
, Reg
*d
, Reg
*s
)
641 d
->XMM_S(0) = float64_to_float32(s
->XMM_D(0), &env
->sse_status
);
642 d
->XMM_S(1) = float64_to_float32(s
->XMM_D(1), &env
->sse_status
);
646 void helper_cvtss2sd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
648 d
->XMM_D(0) = float32_to_float64(s
->XMM_S(0), &env
->sse_status
);
651 void helper_cvtsd2ss(CPUX86State
*env
, Reg
*d
, Reg
*s
)
653 d
->XMM_S(0) = float64_to_float32(s
->XMM_D(0), &env
->sse_status
);
656 /* integer to float */
657 void helper_cvtdq2ps(CPUX86State
*env
, Reg
*d
, Reg
*s
)
659 d
->XMM_S(0) = int32_to_float32(s
->XMM_L(0), &env
->sse_status
);
660 d
->XMM_S(1) = int32_to_float32(s
->XMM_L(1), &env
->sse_status
);
661 d
->XMM_S(2) = int32_to_float32(s
->XMM_L(2), &env
->sse_status
);
662 d
->XMM_S(3) = int32_to_float32(s
->XMM_L(3), &env
->sse_status
);
665 void helper_cvtdq2pd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
669 l0
= (int32_t)s
->XMM_L(0);
670 l1
= (int32_t)s
->XMM_L(1);
671 d
->XMM_D(0) = int32_to_float64(l0
, &env
->sse_status
);
672 d
->XMM_D(1) = int32_to_float64(l1
, &env
->sse_status
);
675 void helper_cvtpi2ps(CPUX86State
*env
, XMMReg
*d
, MMXReg
*s
)
677 d
->XMM_S(0) = int32_to_float32(s
->MMX_L(0), &env
->sse_status
);
678 d
->XMM_S(1) = int32_to_float32(s
->MMX_L(1), &env
->sse_status
);
681 void helper_cvtpi2pd(CPUX86State
*env
, XMMReg
*d
, MMXReg
*s
)
683 d
->XMM_D(0) = int32_to_float64(s
->MMX_L(0), &env
->sse_status
);
684 d
->XMM_D(1) = int32_to_float64(s
->MMX_L(1), &env
->sse_status
);
687 void helper_cvtsi2ss(CPUX86State
*env
, XMMReg
*d
, uint32_t val
)
689 d
->XMM_S(0) = int32_to_float32(val
, &env
->sse_status
);
692 void helper_cvtsi2sd(CPUX86State
*env
, XMMReg
*d
, uint32_t val
)
694 d
->XMM_D(0) = int32_to_float64(val
, &env
->sse_status
);
698 void helper_cvtsq2ss(CPUX86State
*env
, XMMReg
*d
, uint64_t val
)
700 d
->XMM_S(0) = int64_to_float32(val
, &env
->sse_status
);
703 void helper_cvtsq2sd(CPUX86State
*env
, XMMReg
*d
, uint64_t val
)
705 d
->XMM_D(0) = int64_to_float64(val
, &env
->sse_status
);
709 /* float to integer */
710 void helper_cvtps2dq(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
712 d
->XMM_L(0) = float32_to_int32(s
->XMM_S(0), &env
->sse_status
);
713 d
->XMM_L(1) = float32_to_int32(s
->XMM_S(1), &env
->sse_status
);
714 d
->XMM_L(2) = float32_to_int32(s
->XMM_S(2), &env
->sse_status
);
715 d
->XMM_L(3) = float32_to_int32(s
->XMM_S(3), &env
->sse_status
);
718 void helper_cvtpd2dq(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
720 d
->XMM_L(0) = float64_to_int32(s
->XMM_D(0), &env
->sse_status
);
721 d
->XMM_L(1) = float64_to_int32(s
->XMM_D(1), &env
->sse_status
);
725 void helper_cvtps2pi(CPUX86State
*env
, MMXReg
*d
, XMMReg
*s
)
727 d
->MMX_L(0) = float32_to_int32(s
->XMM_S(0), &env
->sse_status
);
728 d
->MMX_L(1) = float32_to_int32(s
->XMM_S(1), &env
->sse_status
);
731 void helper_cvtpd2pi(CPUX86State
*env
, MMXReg
*d
, XMMReg
*s
)
733 d
->MMX_L(0) = float64_to_int32(s
->XMM_D(0), &env
->sse_status
);
734 d
->MMX_L(1) = float64_to_int32(s
->XMM_D(1), &env
->sse_status
);
737 int32_t helper_cvtss2si(CPUX86State
*env
, XMMReg
*s
)
739 return float32_to_int32(s
->XMM_S(0), &env
->sse_status
);
742 int32_t helper_cvtsd2si(CPUX86State
*env
, XMMReg
*s
)
744 return float64_to_int32(s
->XMM_D(0), &env
->sse_status
);
748 int64_t helper_cvtss2sq(CPUX86State
*env
, XMMReg
*s
)
750 return float32_to_int64(s
->XMM_S(0), &env
->sse_status
);
753 int64_t helper_cvtsd2sq(CPUX86State
*env
, XMMReg
*s
)
755 return float64_to_int64(s
->XMM_D(0), &env
->sse_status
);
759 /* float to integer truncated */
760 void helper_cvttps2dq(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
762 d
->XMM_L(0) = float32_to_int32_round_to_zero(s
->XMM_S(0), &env
->sse_status
);
763 d
->XMM_L(1) = float32_to_int32_round_to_zero(s
->XMM_S(1), &env
->sse_status
);
764 d
->XMM_L(2) = float32_to_int32_round_to_zero(s
->XMM_S(2), &env
->sse_status
);
765 d
->XMM_L(3) = float32_to_int32_round_to_zero(s
->XMM_S(3), &env
->sse_status
);
768 void helper_cvttpd2dq(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
770 d
->XMM_L(0) = float64_to_int32_round_to_zero(s
->XMM_D(0), &env
->sse_status
);
771 d
->XMM_L(1) = float64_to_int32_round_to_zero(s
->XMM_D(1), &env
->sse_status
);
775 void helper_cvttps2pi(CPUX86State
*env
, MMXReg
*d
, XMMReg
*s
)
777 d
->MMX_L(0) = float32_to_int32_round_to_zero(s
->XMM_S(0), &env
->sse_status
);
778 d
->MMX_L(1) = float32_to_int32_round_to_zero(s
->XMM_S(1), &env
->sse_status
);
781 void helper_cvttpd2pi(CPUX86State
*env
, MMXReg
*d
, XMMReg
*s
)
783 d
->MMX_L(0) = float64_to_int32_round_to_zero(s
->XMM_D(0), &env
->sse_status
);
784 d
->MMX_L(1) = float64_to_int32_round_to_zero(s
->XMM_D(1), &env
->sse_status
);
787 int32_t helper_cvttss2si(CPUX86State
*env
, XMMReg
*s
)
789 return float32_to_int32_round_to_zero(s
->XMM_S(0), &env
->sse_status
);
792 int32_t helper_cvttsd2si(CPUX86State
*env
, XMMReg
*s
)
794 return float64_to_int32_round_to_zero(s
->XMM_D(0), &env
->sse_status
);
798 int64_t helper_cvttss2sq(CPUX86State
*env
, XMMReg
*s
)
800 return float32_to_int64_round_to_zero(s
->XMM_S(0), &env
->sse_status
);
803 int64_t helper_cvttsd2sq(CPUX86State
*env
, XMMReg
*s
)
805 return float64_to_int64_round_to_zero(s
->XMM_D(0), &env
->sse_status
);
809 void helper_rsqrtps(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
811 d
->XMM_S(0) = float32_div(float32_one
,
812 float32_sqrt(s
->XMM_S(0), &env
->sse_status
),
814 d
->XMM_S(1) = float32_div(float32_one
,
815 float32_sqrt(s
->XMM_S(1), &env
->sse_status
),
817 d
->XMM_S(2) = float32_div(float32_one
,
818 float32_sqrt(s
->XMM_S(2), &env
->sse_status
),
820 d
->XMM_S(3) = float32_div(float32_one
,
821 float32_sqrt(s
->XMM_S(3), &env
->sse_status
),
825 void helper_rsqrtss(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
827 d
->XMM_S(0) = float32_div(float32_one
,
828 float32_sqrt(s
->XMM_S(0), &env
->sse_status
),
832 void helper_rcpps(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
834 d
->XMM_S(0) = float32_div(float32_one
, s
->XMM_S(0), &env
->sse_status
);
835 d
->XMM_S(1) = float32_div(float32_one
, s
->XMM_S(1), &env
->sse_status
);
836 d
->XMM_S(2) = float32_div(float32_one
, s
->XMM_S(2), &env
->sse_status
);
837 d
->XMM_S(3) = float32_div(float32_one
, s
->XMM_S(3), &env
->sse_status
);
840 void helper_rcpss(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
842 d
->XMM_S(0) = float32_div(float32_one
, s
->XMM_S(0), &env
->sse_status
);
845 static inline uint64_t helper_extrq(uint64_t src
, int shift
, int len
)
852 mask
= (1ULL << len
) - 1;
854 return (src
>> shift
) & mask
;
857 void helper_extrq_r(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
859 d
->XMM_Q(0) = helper_extrq(d
->XMM_Q(0), s
->XMM_B(1), s
->XMM_B(0));
862 void helper_extrq_i(CPUX86State
*env
, XMMReg
*d
, int index
, int length
)
864 d
->XMM_Q(0) = helper_extrq(d
->XMM_Q(0), index
, length
);
867 static inline uint64_t helper_insertq(uint64_t src
, int shift
, int len
)
874 mask
= (1ULL << len
) - 1;
876 return (src
& ~(mask
<< shift
)) | ((src
& mask
) << shift
);
879 void helper_insertq_r(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
881 d
->XMM_Q(0) = helper_insertq(s
->XMM_Q(0), s
->XMM_B(9), s
->XMM_B(8));
884 void helper_insertq_i(CPUX86State
*env
, XMMReg
*d
, int index
, int length
)
886 d
->XMM_Q(0) = helper_insertq(d
->XMM_Q(0), index
, length
);
889 void helper_haddps(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
893 r
.XMM_S(0) = float32_add(d
->XMM_S(0), d
->XMM_S(1), &env
->sse_status
);
894 r
.XMM_S(1) = float32_add(d
->XMM_S(2), d
->XMM_S(3), &env
->sse_status
);
895 r
.XMM_S(2) = float32_add(s
->XMM_S(0), s
->XMM_S(1), &env
->sse_status
);
896 r
.XMM_S(3) = float32_add(s
->XMM_S(2), s
->XMM_S(3), &env
->sse_status
);
900 void helper_haddpd(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
904 r
.XMM_D(0) = float64_add(d
->XMM_D(0), d
->XMM_D(1), &env
->sse_status
);
905 r
.XMM_D(1) = float64_add(s
->XMM_D(0), s
->XMM_D(1), &env
->sse_status
);
909 void helper_hsubps(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
913 r
.XMM_S(0) = float32_sub(d
->XMM_S(0), d
->XMM_S(1), &env
->sse_status
);
914 r
.XMM_S(1) = float32_sub(d
->XMM_S(2), d
->XMM_S(3), &env
->sse_status
);
915 r
.XMM_S(2) = float32_sub(s
->XMM_S(0), s
->XMM_S(1), &env
->sse_status
);
916 r
.XMM_S(3) = float32_sub(s
->XMM_S(2), s
->XMM_S(3), &env
->sse_status
);
920 void helper_hsubpd(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
924 r
.XMM_D(0) = float64_sub(d
->XMM_D(0), d
->XMM_D(1), &env
->sse_status
);
925 r
.XMM_D(1) = float64_sub(s
->XMM_D(0), s
->XMM_D(1), &env
->sse_status
);
929 void helper_addsubps(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
931 d
->XMM_S(0) = float32_sub(d
->XMM_S(0), s
->XMM_S(0), &env
->sse_status
);
932 d
->XMM_S(1) = float32_add(d
->XMM_S(1), s
->XMM_S(1), &env
->sse_status
);
933 d
->XMM_S(2) = float32_sub(d
->XMM_S(2), s
->XMM_S(2), &env
->sse_status
);
934 d
->XMM_S(3) = float32_add(d
->XMM_S(3), s
->XMM_S(3), &env
->sse_status
);
937 void helper_addsubpd(CPUX86State
*env
, XMMReg
*d
, XMMReg
*s
)
939 d
->XMM_D(0) = float64_sub(d
->XMM_D(0), s
->XMM_D(0), &env
->sse_status
);
940 d
->XMM_D(1) = float64_add(d
->XMM_D(1), s
->XMM_D(1), &env
->sse_status
);
944 #define SSE_HELPER_CMP(name, F) \
945 void helper_ ## name ## ps(CPUX86State *env, Reg *d, Reg *s) \
947 d->XMM_L(0) = F(32, d->XMM_S(0), s->XMM_S(0)); \
948 d->XMM_L(1) = F(32, d->XMM_S(1), s->XMM_S(1)); \
949 d->XMM_L(2) = F(32, d->XMM_S(2), s->XMM_S(2)); \
950 d->XMM_L(3) = F(32, d->XMM_S(3), s->XMM_S(3)); \
953 void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s) \
955 d->XMM_L(0) = F(32, d->XMM_S(0), s->XMM_S(0)); \
958 void helper_ ## name ## pd(CPUX86State *env, Reg *d, Reg *s) \
960 d->XMM_Q(0) = F(64, d->XMM_D(0), s->XMM_D(0)); \
961 d->XMM_Q(1) = F(64, d->XMM_D(1), s->XMM_D(1)); \
964 void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s) \
966 d->XMM_Q(0) = F(64, d->XMM_D(0), s->XMM_D(0)); \
969 #define FPU_CMPEQ(size, a, b) \
970 (float ## size ## _eq_quiet(a, b, &env->sse_status) ? -1 : 0)
971 #define FPU_CMPLT(size, a, b) \
972 (float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0)
973 #define FPU_CMPLE(size, a, b) \
974 (float ## size ## _le(a, b, &env->sse_status) ? -1 : 0)
975 #define FPU_CMPUNORD(size, a, b) \
976 (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? -1 : 0)
977 #define FPU_CMPNEQ(size, a, b) \
978 (float ## size ## _eq_quiet(a, b, &env->sse_status) ? 0 : -1)
979 #define FPU_CMPNLT(size, a, b) \
980 (float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1)
981 #define FPU_CMPNLE(size, a, b) \
982 (float ## size ## _le(a, b, &env->sse_status) ? 0 : -1)
983 #define FPU_CMPORD(size, a, b) \
984 (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? 0 : -1)
986 SSE_HELPER_CMP(cmpeq
, FPU_CMPEQ
)
987 SSE_HELPER_CMP(cmplt
, FPU_CMPLT
)
988 SSE_HELPER_CMP(cmple
, FPU_CMPLE
)
989 SSE_HELPER_CMP(cmpunord
, FPU_CMPUNORD
)
990 SSE_HELPER_CMP(cmpneq
, FPU_CMPNEQ
)
991 SSE_HELPER_CMP(cmpnlt
, FPU_CMPNLT
)
992 SSE_HELPER_CMP(cmpnle
, FPU_CMPNLE
)
993 SSE_HELPER_CMP(cmpord
, FPU_CMPORD
)
995 static const int comis_eflags
[4] = {CC_C
, CC_Z
, 0, CC_Z
| CC_P
| CC_C
};
997 void helper_ucomiss(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1004 ret
= float32_compare_quiet(s0
, s1
, &env
->sse_status
);
1005 CC_SRC
= comis_eflags
[ret
+ 1];
1008 void helper_comiss(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1015 ret
= float32_compare(s0
, s1
, &env
->sse_status
);
1016 CC_SRC
= comis_eflags
[ret
+ 1];
1019 void helper_ucomisd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1026 ret
= float64_compare_quiet(d0
, d1
, &env
->sse_status
);
1027 CC_SRC
= comis_eflags
[ret
+ 1];
1030 void helper_comisd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1037 ret
= float64_compare(d0
, d1
, &env
->sse_status
);
1038 CC_SRC
= comis_eflags
[ret
+ 1];
1041 uint32_t helper_movmskps(CPUX86State
*env
, Reg
*s
)
1045 b0
= s
->XMM_L(0) >> 31;
1046 b1
= s
->XMM_L(1) >> 31;
1047 b2
= s
->XMM_L(2) >> 31;
1048 b3
= s
->XMM_L(3) >> 31;
1049 return b0
| (b1
<< 1) | (b2
<< 2) | (b3
<< 3);
1052 uint32_t helper_movmskpd(CPUX86State
*env
, Reg
*s
)
1056 b0
= s
->XMM_L(1) >> 31;
1057 b1
= s
->XMM_L(3) >> 31;
1058 return b0
| (b1
<< 1);
1063 uint32_t glue(helper_pmovmskb
, SUFFIX
)(CPUX86State
*env
, Reg
*s
)
1068 val
|= (s
->B(0) >> 7);
1069 val
|= (s
->B(1) >> 6) & 0x02;
1070 val
|= (s
->B(2) >> 5) & 0x04;
1071 val
|= (s
->B(3) >> 4) & 0x08;
1072 val
|= (s
->B(4) >> 3) & 0x10;
1073 val
|= (s
->B(5) >> 2) & 0x20;
1074 val
|= (s
->B(6) >> 1) & 0x40;
1075 val
|= (s
->B(7)) & 0x80;
1077 val
|= (s
->B(8) << 1) & 0x0100;
1078 val
|= (s
->B(9) << 2) & 0x0200;
1079 val
|= (s
->B(10) << 3) & 0x0400;
1080 val
|= (s
->B(11) << 4) & 0x0800;
1081 val
|= (s
->B(12) << 5) & 0x1000;
1082 val
|= (s
->B(13) << 6) & 0x2000;
1083 val
|= (s
->B(14) << 7) & 0x4000;
1084 val
|= (s
->B(15) << 8) & 0x8000;
1089 void glue(helper_packsswb
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1093 r
.B(0) = satsb((int16_t)d
->W(0));
1094 r
.B(1) = satsb((int16_t)d
->W(1));
1095 r
.B(2) = satsb((int16_t)d
->W(2));
1096 r
.B(3) = satsb((int16_t)d
->W(3));
1098 r
.B(4) = satsb((int16_t)d
->W(4));
1099 r
.B(5) = satsb((int16_t)d
->W(5));
1100 r
.B(6) = satsb((int16_t)d
->W(6));
1101 r
.B(7) = satsb((int16_t)d
->W(7));
1103 r
.B((4 << SHIFT
) + 0) = satsb((int16_t)s
->W(0));
1104 r
.B((4 << SHIFT
) + 1) = satsb((int16_t)s
->W(1));
1105 r
.B((4 << SHIFT
) + 2) = satsb((int16_t)s
->W(2));
1106 r
.B((4 << SHIFT
) + 3) = satsb((int16_t)s
->W(3));
1108 r
.B(12) = satsb((int16_t)s
->W(4));
1109 r
.B(13) = satsb((int16_t)s
->W(5));
1110 r
.B(14) = satsb((int16_t)s
->W(6));
1111 r
.B(15) = satsb((int16_t)s
->W(7));
1116 void glue(helper_packuswb
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1120 r
.B(0) = satub((int16_t)d
->W(0));
1121 r
.B(1) = satub((int16_t)d
->W(1));
1122 r
.B(2) = satub((int16_t)d
->W(2));
1123 r
.B(3) = satub((int16_t)d
->W(3));
1125 r
.B(4) = satub((int16_t)d
->W(4));
1126 r
.B(5) = satub((int16_t)d
->W(5));
1127 r
.B(6) = satub((int16_t)d
->W(6));
1128 r
.B(7) = satub((int16_t)d
->W(7));
1130 r
.B((4 << SHIFT
) + 0) = satub((int16_t)s
->W(0));
1131 r
.B((4 << SHIFT
) + 1) = satub((int16_t)s
->W(1));
1132 r
.B((4 << SHIFT
) + 2) = satub((int16_t)s
->W(2));
1133 r
.B((4 << SHIFT
) + 3) = satub((int16_t)s
->W(3));
1135 r
.B(12) = satub((int16_t)s
->W(4));
1136 r
.B(13) = satub((int16_t)s
->W(5));
1137 r
.B(14) = satub((int16_t)s
->W(6));
1138 r
.B(15) = satub((int16_t)s
->W(7));
1143 void glue(helper_packssdw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1147 r
.W(0) = satsw(d
->L(0));
1148 r
.W(1) = satsw(d
->L(1));
1150 r
.W(2) = satsw(d
->L(2));
1151 r
.W(3) = satsw(d
->L(3));
1153 r
.W((2 << SHIFT
) + 0) = satsw(s
->L(0));
1154 r
.W((2 << SHIFT
) + 1) = satsw(s
->L(1));
1156 r
.W(6) = satsw(s
->L(2));
1157 r
.W(7) = satsw(s
->L(3));
1162 #define UNPCK_OP(base_name, base) \
1164 void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1169 r.B(0) = d->B((base << (SHIFT + 2)) + 0); \
1170 r.B(1) = s->B((base << (SHIFT + 2)) + 0); \
1171 r.B(2) = d->B((base << (SHIFT + 2)) + 1); \
1172 r.B(3) = s->B((base << (SHIFT + 2)) + 1); \
1173 r.B(4) = d->B((base << (SHIFT + 2)) + 2); \
1174 r.B(5) = s->B((base << (SHIFT + 2)) + 2); \
1175 r.B(6) = d->B((base << (SHIFT + 2)) + 3); \
1176 r.B(7) = s->B((base << (SHIFT + 2)) + 3); \
1178 r.B(8) = d->B((base << (SHIFT + 2)) + 4); \
1179 r.B(9) = s->B((base << (SHIFT + 2)) + 4); \
1180 r.B(10) = d->B((base << (SHIFT + 2)) + 5); \
1181 r.B(11) = s->B((base << (SHIFT + 2)) + 5); \
1182 r.B(12) = d->B((base << (SHIFT + 2)) + 6); \
1183 r.B(13) = s->B((base << (SHIFT + 2)) + 6); \
1184 r.B(14) = d->B((base << (SHIFT + 2)) + 7); \
1185 r.B(15) = s->B((base << (SHIFT + 2)) + 7); \
1190 void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1195 r.W(0) = d->W((base << (SHIFT + 1)) + 0); \
1196 r.W(1) = s->W((base << (SHIFT + 1)) + 0); \
1197 r.W(2) = d->W((base << (SHIFT + 1)) + 1); \
1198 r.W(3) = s->W((base << (SHIFT + 1)) + 1); \
1200 r.W(4) = d->W((base << (SHIFT + 1)) + 2); \
1201 r.W(5) = s->W((base << (SHIFT + 1)) + 2); \
1202 r.W(6) = d->W((base << (SHIFT + 1)) + 3); \
1203 r.W(7) = s->W((base << (SHIFT + 1)) + 3); \
1208 void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1213 r.L(0) = d->L((base << SHIFT) + 0); \
1214 r.L(1) = s->L((base << SHIFT) + 0); \
1216 r.L(2) = d->L((base << SHIFT) + 1); \
1217 r.L(3) = s->L((base << SHIFT) + 1); \
1223 void glue(helper_punpck ## base_name ## qdq, SUFFIX)(CPUX86State \
1230 r.Q(0) = d->Q(base); \
1231 r.Q(1) = s->Q(base); \
1239 /* 3DNow! float ops */
1241 void helper_pi2fd(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1243 d
->MMX_S(0) = int32_to_float32(s
->MMX_L(0), &env
->mmx_status
);
1244 d
->MMX_S(1) = int32_to_float32(s
->MMX_L(1), &env
->mmx_status
);
1247 void helper_pi2fw(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1249 d
->MMX_S(0) = int32_to_float32((int16_t)s
->MMX_W(0), &env
->mmx_status
);
1250 d
->MMX_S(1) = int32_to_float32((int16_t)s
->MMX_W(2), &env
->mmx_status
);
1253 void helper_pf2id(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1255 d
->MMX_L(0) = float32_to_int32_round_to_zero(s
->MMX_S(0), &env
->mmx_status
);
1256 d
->MMX_L(1) = float32_to_int32_round_to_zero(s
->MMX_S(1), &env
->mmx_status
);
1259 void helper_pf2iw(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1261 d
->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s
->MMX_S(0),
1263 d
->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s
->MMX_S(1),
1267 void helper_pfacc(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1271 r
.MMX_S(0) = float32_add(d
->MMX_S(0), d
->MMX_S(1), &env
->mmx_status
);
1272 r
.MMX_S(1) = float32_add(s
->MMX_S(0), s
->MMX_S(1), &env
->mmx_status
);
1276 void helper_pfadd(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1278 d
->MMX_S(0) = float32_add(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
);
1279 d
->MMX_S(1) = float32_add(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
);
1282 void helper_pfcmpeq(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1284 d
->MMX_L(0) = float32_eq_quiet(d
->MMX_S(0), s
->MMX_S(0),
1285 &env
->mmx_status
) ? -1 : 0;
1286 d
->MMX_L(1) = float32_eq_quiet(d
->MMX_S(1), s
->MMX_S(1),
1287 &env
->mmx_status
) ? -1 : 0;
1290 void helper_pfcmpge(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1292 d
->MMX_L(0) = float32_le(s
->MMX_S(0), d
->MMX_S(0),
1293 &env
->mmx_status
) ? -1 : 0;
1294 d
->MMX_L(1) = float32_le(s
->MMX_S(1), d
->MMX_S(1),
1295 &env
->mmx_status
) ? -1 : 0;
1298 void helper_pfcmpgt(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1300 d
->MMX_L(0) = float32_lt(s
->MMX_S(0), d
->MMX_S(0),
1301 &env
->mmx_status
) ? -1 : 0;
1302 d
->MMX_L(1) = float32_lt(s
->MMX_S(1), d
->MMX_S(1),
1303 &env
->mmx_status
) ? -1 : 0;
1306 void helper_pfmax(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1308 if (float32_lt(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
)) {
1309 d
->MMX_S(0) = s
->MMX_S(0);
1311 if (float32_lt(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
)) {
1312 d
->MMX_S(1) = s
->MMX_S(1);
1316 void helper_pfmin(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1318 if (float32_lt(s
->MMX_S(0), d
->MMX_S(0), &env
->mmx_status
)) {
1319 d
->MMX_S(0) = s
->MMX_S(0);
1321 if (float32_lt(s
->MMX_S(1), d
->MMX_S(1), &env
->mmx_status
)) {
1322 d
->MMX_S(1) = s
->MMX_S(1);
1326 void helper_pfmul(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1328 d
->MMX_S(0) = float32_mul(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
);
1329 d
->MMX_S(1) = float32_mul(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
);
1332 void helper_pfnacc(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1336 r
.MMX_S(0) = float32_sub(d
->MMX_S(0), d
->MMX_S(1), &env
->mmx_status
);
1337 r
.MMX_S(1) = float32_sub(s
->MMX_S(0), s
->MMX_S(1), &env
->mmx_status
);
1341 void helper_pfpnacc(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1345 r
.MMX_S(0) = float32_sub(d
->MMX_S(0), d
->MMX_S(1), &env
->mmx_status
);
1346 r
.MMX_S(1) = float32_add(s
->MMX_S(0), s
->MMX_S(1), &env
->mmx_status
);
1350 void helper_pfrcp(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1352 d
->MMX_S(0) = float32_div(float32_one
, s
->MMX_S(0), &env
->mmx_status
);
1353 d
->MMX_S(1) = d
->MMX_S(0);
1356 void helper_pfrsqrt(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1358 d
->MMX_L(1) = s
->MMX_L(0) & 0x7fffffff;
1359 d
->MMX_S(1) = float32_div(float32_one
,
1360 float32_sqrt(d
->MMX_S(1), &env
->mmx_status
),
1362 d
->MMX_L(1) |= s
->MMX_L(0) & 0x80000000;
1363 d
->MMX_L(0) = d
->MMX_L(1);
1366 void helper_pfsub(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1368 d
->MMX_S(0) = float32_sub(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
);
1369 d
->MMX_S(1) = float32_sub(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
);
1372 void helper_pfsubr(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1374 d
->MMX_S(0) = float32_sub(s
->MMX_S(0), d
->MMX_S(0), &env
->mmx_status
);
1375 d
->MMX_S(1) = float32_sub(s
->MMX_S(1), d
->MMX_S(1), &env
->mmx_status
);
1378 void helper_pswapd(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1382 r
.MMX_L(0) = s
->MMX_L(1);
1383 r
.MMX_L(1) = s
->MMX_L(0);
1388 /* SSSE3 op helpers */
1389 void glue(helper_pshufb
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1394 for (i
= 0; i
< (8 << SHIFT
); i
++) {
1395 r
.B(i
) = (s
->B(i
) & 0x80) ? 0 : (d
->B(s
->B(i
) & ((8 << SHIFT
) - 1)));
1401 void glue(helper_phaddw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1403 d
->W(0) = (int16_t)d
->W(0) + (int16_t)d
->W(1);
1404 d
->W(1) = (int16_t)d
->W(2) + (int16_t)d
->W(3);
1405 XMM_ONLY(d
->W(2) = (int16_t)d
->W(4) + (int16_t)d
->W(5));
1406 XMM_ONLY(d
->W(3) = (int16_t)d
->W(6) + (int16_t)d
->W(7));
1407 d
->W((2 << SHIFT
) + 0) = (int16_t)s
->W(0) + (int16_t)s
->W(1);
1408 d
->W((2 << SHIFT
) + 1) = (int16_t)s
->W(2) + (int16_t)s
->W(3);
1409 XMM_ONLY(d
->W(6) = (int16_t)s
->W(4) + (int16_t)s
->W(5));
1410 XMM_ONLY(d
->W(7) = (int16_t)s
->W(6) + (int16_t)s
->W(7));
1413 void glue(helper_phaddd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1415 d
->L(0) = (int32_t)d
->L(0) + (int32_t)d
->L(1);
1416 XMM_ONLY(d
->L(1) = (int32_t)d
->L(2) + (int32_t)d
->L(3));
1417 d
->L((1 << SHIFT
) + 0) = (int32_t)s
->L(0) + (int32_t)s
->L(1);
1418 XMM_ONLY(d
->L(3) = (int32_t)s
->L(2) + (int32_t)s
->L(3));
1421 void glue(helper_phaddsw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1423 d
->W(0) = satsw((int16_t)d
->W(0) + (int16_t)d
->W(1));
1424 d
->W(1) = satsw((int16_t)d
->W(2) + (int16_t)d
->W(3));
1425 XMM_ONLY(d
->W(2) = satsw((int16_t)d
->W(4) + (int16_t)d
->W(5)));
1426 XMM_ONLY(d
->W(3) = satsw((int16_t)d
->W(6) + (int16_t)d
->W(7)));
1427 d
->W((2 << SHIFT
) + 0) = satsw((int16_t)s
->W(0) + (int16_t)s
->W(1));
1428 d
->W((2 << SHIFT
) + 1) = satsw((int16_t)s
->W(2) + (int16_t)s
->W(3));
1429 XMM_ONLY(d
->W(6) = satsw((int16_t)s
->W(4) + (int16_t)s
->W(5)));
1430 XMM_ONLY(d
->W(7) = satsw((int16_t)s
->W(6) + (int16_t)s
->W(7)));
1433 void glue(helper_pmaddubsw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1435 d
->W(0) = satsw((int8_t)s
->B(0) * (uint8_t)d
->B(0) +
1436 (int8_t)s
->B(1) * (uint8_t)d
->B(1));
1437 d
->W(1) = satsw((int8_t)s
->B(2) * (uint8_t)d
->B(2) +
1438 (int8_t)s
->B(3) * (uint8_t)d
->B(3));
1439 d
->W(2) = satsw((int8_t)s
->B(4) * (uint8_t)d
->B(4) +
1440 (int8_t)s
->B(5) * (uint8_t)d
->B(5));
1441 d
->W(3) = satsw((int8_t)s
->B(6) * (uint8_t)d
->B(6) +
1442 (int8_t)s
->B(7) * (uint8_t)d
->B(7));
1444 d
->W(4) = satsw((int8_t)s
->B(8) * (uint8_t)d
->B(8) +
1445 (int8_t)s
->B(9) * (uint8_t)d
->B(9));
1446 d
->W(5) = satsw((int8_t)s
->B(10) * (uint8_t)d
->B(10) +
1447 (int8_t)s
->B(11) * (uint8_t)d
->B(11));
1448 d
->W(6) = satsw((int8_t)s
->B(12) * (uint8_t)d
->B(12) +
1449 (int8_t)s
->B(13) * (uint8_t)d
->B(13));
1450 d
->W(7) = satsw((int8_t)s
->B(14) * (uint8_t)d
->B(14) +
1451 (int8_t)s
->B(15) * (uint8_t)d
->B(15));
1455 void glue(helper_phsubw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1457 d
->W(0) = (int16_t)d
->W(0) - (int16_t)d
->W(1);
1458 d
->W(1) = (int16_t)d
->W(2) - (int16_t)d
->W(3);
1459 XMM_ONLY(d
->W(2) = (int16_t)d
->W(4) - (int16_t)d
->W(5));
1460 XMM_ONLY(d
->W(3) = (int16_t)d
->W(6) - (int16_t)d
->W(7));
1461 d
->W((2 << SHIFT
) + 0) = (int16_t)s
->W(0) - (int16_t)s
->W(1);
1462 d
->W((2 << SHIFT
) + 1) = (int16_t)s
->W(2) - (int16_t)s
->W(3);
1463 XMM_ONLY(d
->W(6) = (int16_t)s
->W(4) - (int16_t)s
->W(5));
1464 XMM_ONLY(d
->W(7) = (int16_t)s
->W(6) - (int16_t)s
->W(7));
1467 void glue(helper_phsubd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1469 d
->L(0) = (int32_t)d
->L(0) - (int32_t)d
->L(1);
1470 XMM_ONLY(d
->L(1) = (int32_t)d
->L(2) - (int32_t)d
->L(3));
1471 d
->L((1 << SHIFT
) + 0) = (int32_t)s
->L(0) - (int32_t)s
->L(1);
1472 XMM_ONLY(d
->L(3) = (int32_t)s
->L(2) - (int32_t)s
->L(3));
1475 void glue(helper_phsubsw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1477 d
->W(0) = satsw((int16_t)d
->W(0) - (int16_t)d
->W(1));
1478 d
->W(1) = satsw((int16_t)d
->W(2) - (int16_t)d
->W(3));
1479 XMM_ONLY(d
->W(2) = satsw((int16_t)d
->W(4) - (int16_t)d
->W(5)));
1480 XMM_ONLY(d
->W(3) = satsw((int16_t)d
->W(6) - (int16_t)d
->W(7)));
1481 d
->W((2 << SHIFT
) + 0) = satsw((int16_t)s
->W(0) - (int16_t)s
->W(1));
1482 d
->W((2 << SHIFT
) + 1) = satsw((int16_t)s
->W(2) - (int16_t)s
->W(3));
1483 XMM_ONLY(d
->W(6) = satsw((int16_t)s
->W(4) - (int16_t)s
->W(5)));
1484 XMM_ONLY(d
->W(7) = satsw((int16_t)s
->W(6) - (int16_t)s
->W(7)));
1487 #define FABSB(_, x) (x > INT8_MAX ? -(int8_t)x : x)
1488 #define FABSW(_, x) (x > INT16_MAX ? -(int16_t)x : x)
1489 #define FABSL(_, x) (x > INT32_MAX ? -(int32_t)x : x)
1490 SSE_HELPER_B(helper_pabsb
, FABSB
)
1491 SSE_HELPER_W(helper_pabsw
, FABSW
)
1492 SSE_HELPER_L(helper_pabsd
, FABSL
)
1494 #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1495 SSE_HELPER_W(helper_pmulhrsw
, FMULHRSW
)
1497 #define FSIGNB(d, s) (s <= INT8_MAX ? s ? d : 0 : -(int8_t)d)
1498 #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1499 #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1500 SSE_HELPER_B(helper_psignb
, FSIGNB
)
1501 SSE_HELPER_W(helper_psignw
, FSIGNW
)
1502 SSE_HELPER_L(helper_psignd
, FSIGNL
)
1504 void glue(helper_palignr
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1509 /* XXX could be checked during translation */
1510 if (shift
>= (16 << SHIFT
)) {
1512 XMM_ONLY(r
.Q(1) = 0);
1515 #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1517 r
.Q(0) = SHR(s
->Q(0), shift
- 0) |
1518 SHR(d
->Q(0), shift
- 64);
1520 r
.Q(0) = SHR(s
->Q(0), shift
- 0) |
1521 SHR(s
->Q(1), shift
- 64) |
1522 SHR(d
->Q(0), shift
- 128) |
1523 SHR(d
->Q(1), shift
- 192);
1524 r
.Q(1) = SHR(s
->Q(0), shift
+ 64) |
1525 SHR(s
->Q(1), shift
- 0) |
1526 SHR(d
->Q(0), shift
- 64) |
1527 SHR(d
->Q(1), shift
- 128);
1535 #define XMM0 (env->xmm_regs[0])
1538 #define SSE_HELPER_V(name, elem, num, F) \
1539 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1541 d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0)); \
1542 d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1)); \
1544 d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2)); \
1545 d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3)); \
1547 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4)); \
1548 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5)); \
1549 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6)); \
1550 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7)); \
1552 d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \
1553 d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \
1554 d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \
1555 d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \
1556 d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \
1557 d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \
1558 d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \
1559 d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \
1565 #define SSE_HELPER_I(name, elem, num, F) \
1566 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \
1568 d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1)); \
1569 d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1)); \
1571 d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1)); \
1572 d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1)); \
1574 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \
1575 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \
1576 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \
1577 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \
1579 d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \
1580 d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \
1581 d->elem(10) = F(d->elem(10), s->elem(10), \
1582 ((imm >> 10) & 1)); \
1583 d->elem(11) = F(d->elem(11), s->elem(11), \
1584 ((imm >> 11) & 1)); \
1585 d->elem(12) = F(d->elem(12), s->elem(12), \
1586 ((imm >> 12) & 1)); \
1587 d->elem(13) = F(d->elem(13), s->elem(13), \
1588 ((imm >> 13) & 1)); \
1589 d->elem(14) = F(d->elem(14), s->elem(14), \
1590 ((imm >> 14) & 1)); \
1591 d->elem(15) = F(d->elem(15), s->elem(15), \
1592 ((imm >> 15) & 1)); \
1598 /* SSE4.1 op helpers */
1599 #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d)
1600 #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d)
1601 #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d)
1602 SSE_HELPER_V(helper_pblendvb
, B
, 16, FBLENDVB
)
1603 SSE_HELPER_V(helper_blendvps
, L
, 4, FBLENDVPS
)
1604 SSE_HELPER_V(helper_blendvpd
, Q
, 2, FBLENDVPD
)
1606 void glue(helper_ptest
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1608 uint64_t zf
= (s
->Q(0) & d
->Q(0)) | (s
->Q(1) & d
->Q(1));
1609 uint64_t cf
= (s
->Q(0) & ~d
->Q(0)) | (s
->Q(1) & ~d
->Q(1));
1611 CC_SRC
= (zf
? 0 : CC_Z
) | (cf
? 0 : CC_C
);
1614 #define SSE_HELPER_F(name, elem, num, F) \
1615 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1617 d->elem(0) = F(0); \
1618 d->elem(1) = F(1); \
1620 d->elem(2) = F(2); \
1621 d->elem(3) = F(3); \
1623 d->elem(4) = F(4); \
1624 d->elem(5) = F(5); \
1625 d->elem(6) = F(6); \
1626 d->elem(7) = F(7); \
1631 SSE_HELPER_F(helper_pmovsxbw
, W
, 8, (int8_t) s
->B
)
1632 SSE_HELPER_F(helper_pmovsxbd
, L
, 4, (int8_t) s
->B
)
1633 SSE_HELPER_F(helper_pmovsxbq
, Q
, 2, (int8_t) s
->B
)
1634 SSE_HELPER_F(helper_pmovsxwd
, L
, 4, (int16_t) s
->W
)
1635 SSE_HELPER_F(helper_pmovsxwq
, Q
, 2, (int16_t) s
->W
)
1636 SSE_HELPER_F(helper_pmovsxdq
, Q
, 2, (int32_t) s
->L
)
1637 SSE_HELPER_F(helper_pmovzxbw
, W
, 8, s
->B
)
1638 SSE_HELPER_F(helper_pmovzxbd
, L
, 4, s
->B
)
1639 SSE_HELPER_F(helper_pmovzxbq
, Q
, 2, s
->B
)
1640 SSE_HELPER_F(helper_pmovzxwd
, L
, 4, s
->W
)
1641 SSE_HELPER_F(helper_pmovzxwq
, Q
, 2, s
->W
)
1642 SSE_HELPER_F(helper_pmovzxdq
, Q
, 2, s
->L
)
1644 void glue(helper_pmuldq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1646 d
->Q(0) = (int64_t)(int32_t) d
->L(0) * (int32_t) s
->L(0);
1647 d
->Q(1) = (int64_t)(int32_t) d
->L(2) * (int32_t) s
->L(2);
1650 #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1651 SSE_HELPER_Q(helper_pcmpeqq
, FCMPEQQ
)
1653 void glue(helper_packusdw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1655 d
->W(0) = satuw((int32_t) d
->L(0));
1656 d
->W(1) = satuw((int32_t) d
->L(1));
1657 d
->W(2) = satuw((int32_t) d
->L(2));
1658 d
->W(3) = satuw((int32_t) d
->L(3));
1659 d
->W(4) = satuw((int32_t) s
->L(0));
1660 d
->W(5) = satuw((int32_t) s
->L(1));
1661 d
->W(6) = satuw((int32_t) s
->L(2));
1662 d
->W(7) = satuw((int32_t) s
->L(3));
1665 #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1666 #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1667 #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1668 #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1669 SSE_HELPER_B(helper_pminsb
, FMINSB
)
1670 SSE_HELPER_L(helper_pminsd
, FMINSD
)
1671 SSE_HELPER_W(helper_pminuw
, MIN
)
1672 SSE_HELPER_L(helper_pminud
, MIN
)
1673 SSE_HELPER_B(helper_pmaxsb
, FMAXSB
)
1674 SSE_HELPER_L(helper_pmaxsd
, FMAXSD
)
1675 SSE_HELPER_W(helper_pmaxuw
, MAX
)
1676 SSE_HELPER_L(helper_pmaxud
, MAX
)
1678 #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1679 SSE_HELPER_L(helper_pmulld
, FMULLD
)
1681 void glue(helper_phminposuw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1685 if (s
->W(1) < s
->W(idx
)) {
1688 if (s
->W(2) < s
->W(idx
)) {
1691 if (s
->W(3) < s
->W(idx
)) {
1694 if (s
->W(4) < s
->W(idx
)) {
1697 if (s
->W(5) < s
->W(idx
)) {
1700 if (s
->W(6) < s
->W(idx
)) {
1703 if (s
->W(7) < s
->W(idx
)) {
1710 d
->W(0) = s
->W(idx
);
1713 void glue(helper_roundps
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1716 signed char prev_rounding_mode
;
1718 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1719 if (!(mode
& (1 << 2))) {
1722 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1725 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1728 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1731 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1736 d
->XMM_S(0) = float32_round_to_int(s
->XMM_S(0), &env
->sse_status
);
1737 d
->XMM_S(1) = float32_round_to_int(s
->XMM_S(1), &env
->sse_status
);
1738 d
->XMM_S(2) = float32_round_to_int(s
->XMM_S(2), &env
->sse_status
);
1739 d
->XMM_S(3) = float32_round_to_int(s
->XMM_S(3), &env
->sse_status
);
1742 if (mode
& (1 << 3)) {
1743 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1744 ~float_flag_inexact
,
1748 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1751 void glue(helper_roundpd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1754 signed char prev_rounding_mode
;
1756 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1757 if (!(mode
& (1 << 2))) {
1760 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1763 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1766 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1769 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1774 d
->XMM_D(0) = float64_round_to_int(s
->XMM_D(0), &env
->sse_status
);
1775 d
->XMM_D(1) = float64_round_to_int(s
->XMM_D(1), &env
->sse_status
);
1778 if (mode
& (1 << 3)) {
1779 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1780 ~float_flag_inexact
,
1784 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1787 void glue(helper_roundss
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1790 signed char prev_rounding_mode
;
1792 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1793 if (!(mode
& (1 << 2))) {
1796 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1799 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1802 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1805 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1810 d
->XMM_S(0) = float32_round_to_int(s
->XMM_S(0), &env
->sse_status
);
1813 if (mode
& (1 << 3)) {
1814 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1815 ~float_flag_inexact
,
1819 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1822 void glue(helper_roundsd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1825 signed char prev_rounding_mode
;
1827 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1828 if (!(mode
& (1 << 2))) {
1831 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1834 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1837 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1840 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1845 d
->XMM_D(0) = float64_round_to_int(s
->XMM_D(0), &env
->sse_status
);
1848 if (mode
& (1 << 3)) {
1849 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1850 ~float_flag_inexact
,
1854 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1857 #define FBLENDP(d, s, m) (m ? s : d)
1858 SSE_HELPER_I(helper_blendps
, L
, 4, FBLENDP
)
1859 SSE_HELPER_I(helper_blendpd
, Q
, 2, FBLENDP
)
1860 SSE_HELPER_I(helper_pblendw
, W
, 8, FBLENDP
)
1862 void glue(helper_dpps
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
, uint32_t mask
)
1864 float32 iresult
= float32_zero
;
1866 if (mask
& (1 << 4)) {
1867 iresult
= float32_add(iresult
,
1868 float32_mul(d
->XMM_S(0), s
->XMM_S(0),
1872 if (mask
& (1 << 5)) {
1873 iresult
= float32_add(iresult
,
1874 float32_mul(d
->XMM_S(1), s
->XMM_S(1),
1878 if (mask
& (1 << 6)) {
1879 iresult
= float32_add(iresult
,
1880 float32_mul(d
->XMM_S(2), s
->XMM_S(2),
1884 if (mask
& (1 << 7)) {
1885 iresult
= float32_add(iresult
,
1886 float32_mul(d
->XMM_S(3), s
->XMM_S(3),
1890 d
->XMM_S(0) = (mask
& (1 << 0)) ? iresult
: float32_zero
;
1891 d
->XMM_S(1) = (mask
& (1 << 1)) ? iresult
: float32_zero
;
1892 d
->XMM_S(2) = (mask
& (1 << 2)) ? iresult
: float32_zero
;
1893 d
->XMM_S(3) = (mask
& (1 << 3)) ? iresult
: float32_zero
;
1896 void glue(helper_dppd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
, uint32_t mask
)
1898 float64 iresult
= float64_zero
;
1900 if (mask
& (1 << 4)) {
1901 iresult
= float64_add(iresult
,
1902 float64_mul(d
->XMM_D(0), s
->XMM_D(0),
1906 if (mask
& (1 << 5)) {
1907 iresult
= float64_add(iresult
,
1908 float64_mul(d
->XMM_D(1), s
->XMM_D(1),
1912 d
->XMM_D(0) = (mask
& (1 << 0)) ? iresult
: float64_zero
;
1913 d
->XMM_D(1) = (mask
& (1 << 1)) ? iresult
: float64_zero
;
1916 void glue(helper_mpsadbw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1919 int s0
= (offset
& 3) << 2;
1920 int d0
= (offset
& 4) << 0;
1924 for (i
= 0; i
< 8; i
++, d0
++) {
1926 r
.W(i
) += abs1(d
->B(d0
+ 0) - s
->B(s0
+ 0));
1927 r
.W(i
) += abs1(d
->B(d0
+ 1) - s
->B(s0
+ 1));
1928 r
.W(i
) += abs1(d
->B(d0
+ 2) - s
->B(s0
+ 2));
1929 r
.W(i
) += abs1(d
->B(d0
+ 3) - s
->B(s0
+ 3));
1935 /* SSE4.2 op helpers */
1936 #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
1937 SSE_HELPER_Q(helper_pcmpgtq
, FCMPGTQ
)
1939 static inline int pcmp_elen(CPUX86State
*env
, int reg
, uint32_t ctrl
)
1943 /* Presence of REX.W is indicated by a bit higher than 7 set */
1945 val
= abs1((int64_t)env
->regs
[reg
]);
1947 val
= abs1((int32_t)env
->regs
[reg
]);
1962 static inline int pcmp_ilen(Reg
*r
, uint8_t ctrl
)
1967 while (val
< 8 && r
->W(val
)) {
1971 while (val
< 16 && r
->B(val
)) {
1979 static inline int pcmp_val(Reg
*r
, uint8_t ctrl
, int i
)
1981 switch ((ctrl
>> 0) & 3) {
1987 return (int8_t)r
->B(i
);
1990 return (int16_t)r
->W(i
);
1994 static inline unsigned pcmpxstrx(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1995 int8_t ctrl
, int valids
, int validd
)
1997 unsigned int res
= 0;
2000 int upper
= (ctrl
& 1) ? 7 : 15;
2005 CC_SRC
= (valids
< upper
? CC_Z
: 0) | (validd
< upper
? CC_S
: 0);
2007 switch ((ctrl
>> 2) & 3) {
2009 for (j
= valids
; j
>= 0; j
--) {
2011 v
= pcmp_val(s
, ctrl
, j
);
2012 for (i
= validd
; i
>= 0; i
--) {
2013 res
|= (v
== pcmp_val(d
, ctrl
, i
));
2018 for (j
= valids
; j
>= 0; j
--) {
2020 v
= pcmp_val(s
, ctrl
, j
);
2021 for (i
= ((validd
- 1) | 1); i
>= 0; i
-= 2) {
2022 res
|= (pcmp_val(d
, ctrl
, i
- 0) >= v
&&
2023 pcmp_val(d
, ctrl
, i
- 1) <= v
);
2028 res
= (1 << (upper
- MAX(valids
, validd
))) - 1;
2029 res
<<= MAX(valids
, validd
) - MIN(valids
, validd
);
2030 for (i
= MIN(valids
, validd
); i
>= 0; i
--) {
2032 v
= pcmp_val(s
, ctrl
, i
);
2033 res
|= (v
== pcmp_val(d
, ctrl
, i
));
2037 for (j
= valids
- validd
; j
>= 0; j
--) {
2040 for (i
= MIN(upper
- j
, validd
); i
>= 0; i
--) {
2041 v
&= (pcmp_val(s
, ctrl
, i
+ j
) == pcmp_val(d
, ctrl
, i
));
2048 switch ((ctrl
>> 4) & 3) {
2050 res
^= (2 << upper
) - 1;
2053 res
^= (1 << (valids
+ 1)) - 1;
2067 void glue(helper_pcmpestri
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2070 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2071 pcmp_elen(env
, R_EDX
, ctrl
),
2072 pcmp_elen(env
, R_EAX
, ctrl
));
2075 env
->regs
[R_ECX
] = (ctrl
& (1 << 6)) ? 31 - clz32(res
) : ctz32(res
);
2077 env
->regs
[R_ECX
] = 16 >> (ctrl
& (1 << 0));
2081 void glue(helper_pcmpestrm
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2085 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2086 pcmp_elen(env
, R_EDX
, ctrl
),
2087 pcmp_elen(env
, R_EAX
, ctrl
));
2089 if ((ctrl
>> 6) & 1) {
2091 for (i
= 0; i
< 8; i
++, res
>>= 1) {
2092 env
->xmm_regs
[0].W(i
) = (res
& 1) ? ~0 : 0;
2095 for (i
= 0; i
< 16; i
++, res
>>= 1) {
2096 env
->xmm_regs
[0].B(i
) = (res
& 1) ? ~0 : 0;
2100 env
->xmm_regs
[0].Q(1) = 0;
2101 env
->xmm_regs
[0].Q(0) = res
;
2105 void glue(helper_pcmpistri
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2108 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2110 pcmp_ilen(d
, ctrl
));
2113 env
->regs
[R_ECX
] = (ctrl
& (1 << 6)) ? 31 - clz32(res
) : ctz32(res
);
2115 env
->regs
[R_ECX
] = 16 >> (ctrl
& (1 << 0));
2119 void glue(helper_pcmpistrm
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2123 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2125 pcmp_ilen(d
, ctrl
));
2127 if ((ctrl
>> 6) & 1) {
2129 for (i
= 0; i
< 8; i
++, res
>>= 1) {
2130 env
->xmm_regs
[0].W(i
) = (res
& 1) ? ~0 : 0;
2133 for (i
= 0; i
< 16; i
++, res
>>= 1) {
2134 env
->xmm_regs
[0].B(i
) = (res
& 1) ? ~0 : 0;
2138 env
->xmm_regs
[0].Q(1) = 0;
2139 env
->xmm_regs
[0].Q(0) = res
;
2143 #define CRCPOLY 0x1edc6f41
2144 #define CRCPOLY_BITREV 0x82f63b78
2145 target_ulong
helper_crc32(uint32_t crc1
, target_ulong msg
, uint32_t len
)
2147 target_ulong crc
= (msg
& ((target_ulong
) -1 >>
2148 (TARGET_LONG_BITS
- len
))) ^ crc1
;
2151 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_BITREV
: 0);
2157 #define POPMASK(i) ((target_ulong) -1 / ((1LL << (1 << i)) + 1))
2158 #define POPCOUNT(n, i) ((n & POPMASK(i)) + ((n >> (1 << i)) & POPMASK(i)))
2159 target_ulong
helper_popcnt(CPUX86State
*env
, target_ulong n
, uint32_t type
)
2161 CC_SRC
= n
? 0 : CC_Z
;
2172 #ifndef TARGET_X86_64
2179 return POPCOUNT(n
, 5);
2183 void glue(helper_pclmulqdq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2186 uint64_t ah
, al
, b
, resh
, resl
;
2189 al
= d
->Q((ctrl
& 1) != 0);
2190 b
= s
->Q((ctrl
& 16) != 0);
2198 ah
= (ah
<< 1) | (al
>> 63);
2207 /* AES-NI op helpers */
2208 static const uint8_t aes_shifts
[16] = {
2209 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11
2212 static const uint8_t aes_ishifts
[16] = {
2213 0, 13, 10, 7, 4, 1, 14, 11, 8, 5, 2, 15, 12, 9, 6, 3
2216 void glue(helper_aesdec
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2222 for (i
= 0 ; i
< 4 ; i
++) {
2223 d
->L(i
) = rk
.L(i
) ^ bswap32(AES_Td0
[st
.B(aes_ishifts
[4*i
+0])] ^
2224 AES_Td1
[st
.B(aes_ishifts
[4*i
+1])] ^
2225 AES_Td2
[st
.B(aes_ishifts
[4*i
+2])] ^
2226 AES_Td3
[st
.B(aes_ishifts
[4*i
+3])]);
2230 void glue(helper_aesdeclast
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2236 for (i
= 0; i
< 16; i
++) {
2237 d
->B(i
) = rk
.B(i
) ^ (AES_Td4
[st
.B(aes_ishifts
[i
])] & 0xff);
2241 void glue(helper_aesenc
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2247 for (i
= 0 ; i
< 4 ; i
++) {
2248 d
->L(i
) = rk
.L(i
) ^ bswap32(AES_Te0
[st
.B(aes_shifts
[4*i
+0])] ^
2249 AES_Te1
[st
.B(aes_shifts
[4*i
+1])] ^
2250 AES_Te2
[st
.B(aes_shifts
[4*i
+2])] ^
2251 AES_Te3
[st
.B(aes_shifts
[4*i
+3])]);
2255 void glue(helper_aesenclast
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2261 for (i
= 0; i
< 16; i
++) {
2262 d
->B(i
) = rk
.B(i
) ^ (AES_Te4
[st
.B(aes_shifts
[i
])] & 0xff);
2267 void glue(helper_aesimc
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2272 for (i
= 0 ; i
< 4 ; i
++) {
2273 d
->L(i
) = bswap32(AES_Td0
[AES_Te4
[tmp
.B(4*i
+0)] & 0xff] ^
2274 AES_Td1
[AES_Te4
[tmp
.B(4*i
+1)] & 0xff] ^
2275 AES_Td2
[AES_Te4
[tmp
.B(4*i
+2)] & 0xff] ^
2276 AES_Td3
[AES_Te4
[tmp
.B(4*i
+3)] & 0xff]);
2280 void glue(helper_aeskeygenassist
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2286 for (i
= 0 ; i
< 4 ; i
++) {
2287 d
->B(i
) = AES_Te4
[tmp
.B(i
+ 4)] & 0xff;
2288 d
->B(i
+ 8) = AES_Te4
[tmp
.B(i
+ 12)] & 0xff;
2290 d
->L(1) = (d
->L(0) << 24 | d
->L(0) >> 8) ^ ctrl
;
2291 d
->L(3) = (d
->L(2) << 24 | d
->L(2) >> 8) ^ ctrl
;