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.1 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/>.
21 #include "crypto/aes.h"
35 #define XMM_ONLY(...) __VA_ARGS__
44 * Copy the relevant parts of a Reg value around. In the case where
45 * sizeof(Reg) > SIZE, these helpers operate only on the lower bytes of
46 * a 64 byte ZMMReg, so we must copy only those and keep the top bytes
47 * untouched in the guest-visible destination destination register.
48 * Note that the "lower bytes" are placed last in memory on big-endian
49 * hosts, which store the vector backwards in memory. In that case the
50 * copy *starts* at B(SIZE - 1) and ends at B(0), the opposite of
51 * the little-endian case.
54 #define MOVE(d, r) memcpy(&((d).B(SIZE - 1)), &(r).B(SIZE - 1), SIZE)
56 #define MOVE(d, r) memcpy(&(d).B(0), &(r).B(0), SIZE)
59 void glue(helper_psrlw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
83 void glue(helper_psraw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
92 d
->W(0) = (int16_t)d
->W(0) >> shift
;
93 d
->W(1) = (int16_t)d
->W(1) >> shift
;
94 d
->W(2) = (int16_t)d
->W(2) >> shift
;
95 d
->W(3) = (int16_t)d
->W(3) >> shift
;
97 d
->W(4) = (int16_t)d
->W(4) >> shift
;
98 d
->W(5) = (int16_t)d
->W(5) >> shift
;
99 d
->W(6) = (int16_t)d
->W(6) >> shift
;
100 d
->W(7) = (int16_t)d
->W(7) >> shift
;
104 void glue(helper_psllw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
128 void glue(helper_psrld
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
148 void glue(helper_psrad
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
157 d
->L(0) = (int32_t)d
->L(0) >> shift
;
158 d
->L(1) = (int32_t)d
->L(1) >> shift
;
160 d
->L(2) = (int32_t)d
->L(2) >> shift
;
161 d
->L(3) = (int32_t)d
->L(3) >> shift
;
165 void glue(helper_pslld
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
185 void glue(helper_psrlq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
203 void glue(helper_psllq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
222 void glue(helper_psrldq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
230 for (i
= 0; i
< 16 - shift
; i
++) {
231 d
->B(i
) = d
->B(i
+ shift
);
233 for (i
= 16 - shift
; i
< 16; i
++) {
238 void glue(helper_pslldq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
246 for (i
= 15; i
>= shift
; i
--) {
247 d
->B(i
) = d
->B(i
- shift
);
249 for (i
= 0; i
< shift
; i
++) {
255 #define SSE_HELPER_B(name, F) \
256 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
258 d->B(0) = F(d->B(0), s->B(0)); \
259 d->B(1) = F(d->B(1), s->B(1)); \
260 d->B(2) = F(d->B(2), s->B(2)); \
261 d->B(3) = F(d->B(3), s->B(3)); \
262 d->B(4) = F(d->B(4), s->B(4)); \
263 d->B(5) = F(d->B(5), s->B(5)); \
264 d->B(6) = F(d->B(6), s->B(6)); \
265 d->B(7) = F(d->B(7), s->B(7)); \
267 d->B(8) = F(d->B(8), s->B(8)); \
268 d->B(9) = F(d->B(9), s->B(9)); \
269 d->B(10) = F(d->B(10), s->B(10)); \
270 d->B(11) = F(d->B(11), s->B(11)); \
271 d->B(12) = F(d->B(12), s->B(12)); \
272 d->B(13) = F(d->B(13), s->B(13)); \
273 d->B(14) = F(d->B(14), s->B(14)); \
274 d->B(15) = F(d->B(15), s->B(15)); \
278 #define SSE_HELPER_W(name, F) \
279 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
281 d->W(0) = F(d->W(0), s->W(0)); \
282 d->W(1) = F(d->W(1), s->W(1)); \
283 d->W(2) = F(d->W(2), s->W(2)); \
284 d->W(3) = F(d->W(3), s->W(3)); \
286 d->W(4) = F(d->W(4), s->W(4)); \
287 d->W(5) = F(d->W(5), s->W(5)); \
288 d->W(6) = F(d->W(6), s->W(6)); \
289 d->W(7) = F(d->W(7), s->W(7)); \
293 #define SSE_HELPER_L(name, F) \
294 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
296 d->L(0) = F(d->L(0), s->L(0)); \
297 d->L(1) = F(d->L(1), s->L(1)); \
299 d->L(2) = F(d->L(2), s->L(2)); \
300 d->L(3) = F(d->L(3), s->L(3)); \
304 #define SSE_HELPER_Q(name, F) \
305 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
307 d->Q(0) = F(d->Q(0), s->Q(0)); \
309 d->Q(1) = F(d->Q(1), s->Q(1)); \
314 static inline int satub(int x
)
318 } else if (x
> 255) {
325 static inline int satuw(int x
)
329 } else if (x
> 65535) {
336 static inline int satsb(int x
)
340 } else if (x
> 127) {
347 static inline int satsw(int x
)
351 } else if (x
> 32767) {
358 #define FADD(a, b) ((a) + (b))
359 #define FADDUB(a, b) satub((a) + (b))
360 #define FADDUW(a, b) satuw((a) + (b))
361 #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b))
362 #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b))
364 #define FSUB(a, b) ((a) - (b))
365 #define FSUBUB(a, b) satub((a) - (b))
366 #define FSUBUW(a, b) satuw((a) - (b))
367 #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b))
368 #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b))
369 #define FMINUB(a, b) ((a) < (b)) ? (a) : (b)
370 #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b)
371 #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b)
372 #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b)
374 #define FAND(a, b) ((a) & (b))
375 #define FANDN(a, b) ((~(a)) & (b))
376 #define FOR(a, b) ((a) | (b))
377 #define FXOR(a, b) ((a) ^ (b))
379 #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0)
380 #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0)
381 #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0)
382 #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0)
384 #define FMULLW(a, b) ((a) * (b))
385 #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16)
386 #define FMULHUW(a, b) ((a) * (b) >> 16)
387 #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16)
389 #define FAVG(a, b) (((a) + (b) + 1) >> 1)
392 SSE_HELPER_B(helper_paddb
, FADD
)
393 SSE_HELPER_W(helper_paddw
, FADD
)
394 SSE_HELPER_L(helper_paddl
, FADD
)
395 SSE_HELPER_Q(helper_paddq
, FADD
)
397 SSE_HELPER_B(helper_psubb
, FSUB
)
398 SSE_HELPER_W(helper_psubw
, FSUB
)
399 SSE_HELPER_L(helper_psubl
, FSUB
)
400 SSE_HELPER_Q(helper_psubq
, FSUB
)
402 SSE_HELPER_B(helper_paddusb
, FADDUB
)
403 SSE_HELPER_B(helper_paddsb
, FADDSB
)
404 SSE_HELPER_B(helper_psubusb
, FSUBUB
)
405 SSE_HELPER_B(helper_psubsb
, FSUBSB
)
407 SSE_HELPER_W(helper_paddusw
, FADDUW
)
408 SSE_HELPER_W(helper_paddsw
, FADDSW
)
409 SSE_HELPER_W(helper_psubusw
, FSUBUW
)
410 SSE_HELPER_W(helper_psubsw
, FSUBSW
)
412 SSE_HELPER_B(helper_pminub
, FMINUB
)
413 SSE_HELPER_B(helper_pmaxub
, FMAXUB
)
415 SSE_HELPER_W(helper_pminsw
, FMINSW
)
416 SSE_HELPER_W(helper_pmaxsw
, FMAXSW
)
418 SSE_HELPER_Q(helper_pand
, FAND
)
419 SSE_HELPER_Q(helper_pandn
, FANDN
)
420 SSE_HELPER_Q(helper_por
, FOR
)
421 SSE_HELPER_Q(helper_pxor
, FXOR
)
423 SSE_HELPER_B(helper_pcmpgtb
, FCMPGTB
)
424 SSE_HELPER_W(helper_pcmpgtw
, FCMPGTW
)
425 SSE_HELPER_L(helper_pcmpgtl
, FCMPGTL
)
427 SSE_HELPER_B(helper_pcmpeqb
, FCMPEQ
)
428 SSE_HELPER_W(helper_pcmpeqw
, FCMPEQ
)
429 SSE_HELPER_L(helper_pcmpeql
, FCMPEQ
)
431 SSE_HELPER_W(helper_pmullw
, FMULLW
)
433 SSE_HELPER_W(helper_pmulhrw
, FMULHRW
)
435 SSE_HELPER_W(helper_pmulhuw
, FMULHUW
)
436 SSE_HELPER_W(helper_pmulhw
, FMULHW
)
438 SSE_HELPER_B(helper_pavgb
, FAVG
)
439 SSE_HELPER_W(helper_pavgw
, FAVG
)
441 void glue(helper_pmuludq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
443 d
->Q(0) = (uint64_t)s
->L(0) * (uint64_t)d
->L(0);
445 d
->Q(1) = (uint64_t)s
->L(2) * (uint64_t)d
->L(2);
449 void glue(helper_pmaddwd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
453 for (i
= 0; i
< (2 << SHIFT
); i
++) {
454 d
->L(i
) = (int16_t)s
->W(2 * i
) * (int16_t)d
->W(2 * i
) +
455 (int16_t)s
->W(2 * i
+ 1) * (int16_t)d
->W(2 * i
+ 1);
460 static inline int abs1(int a
)
469 void glue(helper_psadbw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
474 val
+= abs1(d
->B(0) - s
->B(0));
475 val
+= abs1(d
->B(1) - s
->B(1));
476 val
+= abs1(d
->B(2) - s
->B(2));
477 val
+= abs1(d
->B(3) - s
->B(3));
478 val
+= abs1(d
->B(4) - s
->B(4));
479 val
+= abs1(d
->B(5) - s
->B(5));
480 val
+= abs1(d
->B(6) - s
->B(6));
481 val
+= abs1(d
->B(7) - s
->B(7));
485 val
+= abs1(d
->B(8) - s
->B(8));
486 val
+= abs1(d
->B(9) - s
->B(9));
487 val
+= abs1(d
->B(10) - s
->B(10));
488 val
+= abs1(d
->B(11) - s
->B(11));
489 val
+= abs1(d
->B(12) - s
->B(12));
490 val
+= abs1(d
->B(13) - s
->B(13));
491 val
+= abs1(d
->B(14) - s
->B(14));
492 val
+= abs1(d
->B(15) - s
->B(15));
497 void glue(helper_maskmov
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
502 for (i
= 0; i
< (8 << SHIFT
); i
++) {
503 if (s
->B(i
) & 0x80) {
504 cpu_stb_data_ra(env
, a0
+ i
, d
->B(i
), GETPC());
509 void glue(helper_movl_mm_T0
, SUFFIX
)(Reg
*d
, uint32_t val
)
519 void glue(helper_movq_mm_T0
, SUFFIX
)(Reg
*d
, uint64_t val
)
529 void glue(helper_pshufw
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
533 r
.W(0) = s
->W(order
& 3);
534 r
.W(1) = s
->W((order
>> 2) & 3);
535 r
.W(2) = s
->W((order
>> 4) & 3);
536 r
.W(3) = s
->W((order
>> 6) & 3);
540 void helper_shufps(Reg
*d
, Reg
*s
, int order
)
544 r
.L(0) = d
->L(order
& 3);
545 r
.L(1) = d
->L((order
>> 2) & 3);
546 r
.L(2) = s
->L((order
>> 4) & 3);
547 r
.L(3) = s
->L((order
>> 6) & 3);
551 void helper_shufpd(Reg
*d
, Reg
*s
, int order
)
555 r
.Q(0) = d
->Q(order
& 1);
556 r
.Q(1) = s
->Q((order
>> 1) & 1);
560 void glue(helper_pshufd
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
564 r
.L(0) = s
->L(order
& 3);
565 r
.L(1) = s
->L((order
>> 2) & 3);
566 r
.L(2) = s
->L((order
>> 4) & 3);
567 r
.L(3) = s
->L((order
>> 6) & 3);
571 void glue(helper_pshuflw
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
575 r
.W(0) = s
->W(order
& 3);
576 r
.W(1) = s
->W((order
>> 2) & 3);
577 r
.W(2) = s
->W((order
>> 4) & 3);
578 r
.W(3) = s
->W((order
>> 6) & 3);
583 void glue(helper_pshufhw
, SUFFIX
)(Reg
*d
, Reg
*s
, int order
)
588 r
.W(4) = s
->W(4 + (order
& 3));
589 r
.W(5) = s
->W(4 + ((order
>> 2) & 3));
590 r
.W(6) = s
->W(4 + ((order
>> 4) & 3));
591 r
.W(7) = s
->W(4 + ((order
>> 6) & 3));
598 /* XXX: not accurate */
600 #define SSE_HELPER_S(name, F) \
601 void helper_ ## name ## ps(CPUX86State *env, Reg *d, Reg *s) \
603 d->ZMM_S(0) = F(32, d->ZMM_S(0), s->ZMM_S(0)); \
604 d->ZMM_S(1) = F(32, d->ZMM_S(1), s->ZMM_S(1)); \
605 d->ZMM_S(2) = F(32, d->ZMM_S(2), s->ZMM_S(2)); \
606 d->ZMM_S(3) = F(32, d->ZMM_S(3), s->ZMM_S(3)); \
609 void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s) \
611 d->ZMM_S(0) = F(32, d->ZMM_S(0), s->ZMM_S(0)); \
614 void helper_ ## name ## pd(CPUX86State *env, Reg *d, Reg *s) \
616 d->ZMM_D(0) = F(64, d->ZMM_D(0), s->ZMM_D(0)); \
617 d->ZMM_D(1) = F(64, d->ZMM_D(1), s->ZMM_D(1)); \
620 void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s) \
622 d->ZMM_D(0) = F(64, d->ZMM_D(0), s->ZMM_D(0)); \
625 #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status)
626 #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status)
627 #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status)
628 #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status)
629 #define FPU_SQRT(size, a, b) float ## size ## _sqrt(b, &env->sse_status)
631 /* Note that the choice of comparison op here is important to get the
632 * special cases right: for min and max Intel specifies that (-0,0),
633 * (NaN, anything) and (anything, NaN) return the second argument.
635 #define FPU_MIN(size, a, b) \
636 (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b))
637 #define FPU_MAX(size, a, b) \
638 (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b))
640 SSE_HELPER_S(add
, FPU_ADD
)
641 SSE_HELPER_S(sub
, FPU_SUB
)
642 SSE_HELPER_S(mul
, FPU_MUL
)
643 SSE_HELPER_S(div
, FPU_DIV
)
644 SSE_HELPER_S(min
, FPU_MIN
)
645 SSE_HELPER_S(max
, FPU_MAX
)
646 SSE_HELPER_S(sqrt
, FPU_SQRT
)
649 /* float to float conversions */
650 void helper_cvtps2pd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
656 d
->ZMM_D(0) = float32_to_float64(s0
, &env
->sse_status
);
657 d
->ZMM_D(1) = float32_to_float64(s1
, &env
->sse_status
);
660 void helper_cvtpd2ps(CPUX86State
*env
, Reg
*d
, Reg
*s
)
662 d
->ZMM_S(0) = float64_to_float32(s
->ZMM_D(0), &env
->sse_status
);
663 d
->ZMM_S(1) = float64_to_float32(s
->ZMM_D(1), &env
->sse_status
);
667 void helper_cvtss2sd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
669 d
->ZMM_D(0) = float32_to_float64(s
->ZMM_S(0), &env
->sse_status
);
672 void helper_cvtsd2ss(CPUX86State
*env
, Reg
*d
, Reg
*s
)
674 d
->ZMM_S(0) = float64_to_float32(s
->ZMM_D(0), &env
->sse_status
);
677 /* integer to float */
678 void helper_cvtdq2ps(CPUX86State
*env
, Reg
*d
, Reg
*s
)
680 d
->ZMM_S(0) = int32_to_float32(s
->ZMM_L(0), &env
->sse_status
);
681 d
->ZMM_S(1) = int32_to_float32(s
->ZMM_L(1), &env
->sse_status
);
682 d
->ZMM_S(2) = int32_to_float32(s
->ZMM_L(2), &env
->sse_status
);
683 d
->ZMM_S(3) = int32_to_float32(s
->ZMM_L(3), &env
->sse_status
);
686 void helper_cvtdq2pd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
690 l0
= (int32_t)s
->ZMM_L(0);
691 l1
= (int32_t)s
->ZMM_L(1);
692 d
->ZMM_D(0) = int32_to_float64(l0
, &env
->sse_status
);
693 d
->ZMM_D(1) = int32_to_float64(l1
, &env
->sse_status
);
696 void helper_cvtpi2ps(CPUX86State
*env
, ZMMReg
*d
, MMXReg
*s
)
698 d
->ZMM_S(0) = int32_to_float32(s
->MMX_L(0), &env
->sse_status
);
699 d
->ZMM_S(1) = int32_to_float32(s
->MMX_L(1), &env
->sse_status
);
702 void helper_cvtpi2pd(CPUX86State
*env
, ZMMReg
*d
, MMXReg
*s
)
704 d
->ZMM_D(0) = int32_to_float64(s
->MMX_L(0), &env
->sse_status
);
705 d
->ZMM_D(1) = int32_to_float64(s
->MMX_L(1), &env
->sse_status
);
708 void helper_cvtsi2ss(CPUX86State
*env
, ZMMReg
*d
, uint32_t val
)
710 d
->ZMM_S(0) = int32_to_float32(val
, &env
->sse_status
);
713 void helper_cvtsi2sd(CPUX86State
*env
, ZMMReg
*d
, uint32_t val
)
715 d
->ZMM_D(0) = int32_to_float64(val
, &env
->sse_status
);
719 void helper_cvtsq2ss(CPUX86State
*env
, ZMMReg
*d
, uint64_t val
)
721 d
->ZMM_S(0) = int64_to_float32(val
, &env
->sse_status
);
724 void helper_cvtsq2sd(CPUX86State
*env
, ZMMReg
*d
, uint64_t val
)
726 d
->ZMM_D(0) = int64_to_float64(val
, &env
->sse_status
);
730 /* float to integer */
733 * x86 mandates that we return the indefinite integer value for the result
734 * of any float-to-integer conversion that raises the 'invalid' exception.
735 * Wrap the softfloat functions to get this behaviour.
737 #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE) \
738 static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s) \
740 int oldflags, newflags; \
743 oldflags = get_float_exception_flags(s); \
744 set_float_exception_flags(0, s); \
746 newflags = get_float_exception_flags(s); \
747 if (newflags & float_flag_invalid) { \
750 set_float_exception_flags(newflags | oldflags, s); \
754 WRAP_FLOATCONV(int32_t, float32_to_int32
, float32
, INT32_MIN
)
755 WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero
, float32
, INT32_MIN
)
756 WRAP_FLOATCONV(int32_t, float64_to_int32
, float64
, INT32_MIN
)
757 WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero
, float64
, INT32_MIN
)
758 WRAP_FLOATCONV(int64_t, float32_to_int64
, float32
, INT64_MIN
)
759 WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero
, float32
, INT64_MIN
)
760 WRAP_FLOATCONV(int64_t, float64_to_int64
, float64
, INT64_MIN
)
761 WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero
, float64
, INT64_MIN
)
763 void helper_cvtps2dq(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
765 d
->ZMM_L(0) = x86_float32_to_int32(s
->ZMM_S(0), &env
->sse_status
);
766 d
->ZMM_L(1) = x86_float32_to_int32(s
->ZMM_S(1), &env
->sse_status
);
767 d
->ZMM_L(2) = x86_float32_to_int32(s
->ZMM_S(2), &env
->sse_status
);
768 d
->ZMM_L(3) = x86_float32_to_int32(s
->ZMM_S(3), &env
->sse_status
);
771 void helper_cvtpd2dq(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
773 d
->ZMM_L(0) = x86_float64_to_int32(s
->ZMM_D(0), &env
->sse_status
);
774 d
->ZMM_L(1) = x86_float64_to_int32(s
->ZMM_D(1), &env
->sse_status
);
778 void helper_cvtps2pi(CPUX86State
*env
, MMXReg
*d
, ZMMReg
*s
)
780 d
->MMX_L(0) = x86_float32_to_int32(s
->ZMM_S(0), &env
->sse_status
);
781 d
->MMX_L(1) = x86_float32_to_int32(s
->ZMM_S(1), &env
->sse_status
);
784 void helper_cvtpd2pi(CPUX86State
*env
, MMXReg
*d
, ZMMReg
*s
)
786 d
->MMX_L(0) = x86_float64_to_int32(s
->ZMM_D(0), &env
->sse_status
);
787 d
->MMX_L(1) = x86_float64_to_int32(s
->ZMM_D(1), &env
->sse_status
);
790 int32_t helper_cvtss2si(CPUX86State
*env
, ZMMReg
*s
)
792 return x86_float32_to_int32(s
->ZMM_S(0), &env
->sse_status
);
795 int32_t helper_cvtsd2si(CPUX86State
*env
, ZMMReg
*s
)
797 return x86_float64_to_int32(s
->ZMM_D(0), &env
->sse_status
);
801 int64_t helper_cvtss2sq(CPUX86State
*env
, ZMMReg
*s
)
803 return x86_float32_to_int64(s
->ZMM_S(0), &env
->sse_status
);
806 int64_t helper_cvtsd2sq(CPUX86State
*env
, ZMMReg
*s
)
808 return x86_float64_to_int64(s
->ZMM_D(0), &env
->sse_status
);
812 /* float to integer truncated */
813 void helper_cvttps2dq(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
815 d
->ZMM_L(0) = x86_float32_to_int32_round_to_zero(s
->ZMM_S(0), &env
->sse_status
);
816 d
->ZMM_L(1) = x86_float32_to_int32_round_to_zero(s
->ZMM_S(1), &env
->sse_status
);
817 d
->ZMM_L(2) = x86_float32_to_int32_round_to_zero(s
->ZMM_S(2), &env
->sse_status
);
818 d
->ZMM_L(3) = x86_float32_to_int32_round_to_zero(s
->ZMM_S(3), &env
->sse_status
);
821 void helper_cvttpd2dq(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
823 d
->ZMM_L(0) = x86_float64_to_int32_round_to_zero(s
->ZMM_D(0), &env
->sse_status
);
824 d
->ZMM_L(1) = x86_float64_to_int32_round_to_zero(s
->ZMM_D(1), &env
->sse_status
);
828 void helper_cvttps2pi(CPUX86State
*env
, MMXReg
*d
, ZMMReg
*s
)
830 d
->MMX_L(0) = x86_float32_to_int32_round_to_zero(s
->ZMM_S(0), &env
->sse_status
);
831 d
->MMX_L(1) = x86_float32_to_int32_round_to_zero(s
->ZMM_S(1), &env
->sse_status
);
834 void helper_cvttpd2pi(CPUX86State
*env
, MMXReg
*d
, ZMMReg
*s
)
836 d
->MMX_L(0) = x86_float64_to_int32_round_to_zero(s
->ZMM_D(0), &env
->sse_status
);
837 d
->MMX_L(1) = x86_float64_to_int32_round_to_zero(s
->ZMM_D(1), &env
->sse_status
);
840 int32_t helper_cvttss2si(CPUX86State
*env
, ZMMReg
*s
)
842 return x86_float32_to_int32_round_to_zero(s
->ZMM_S(0), &env
->sse_status
);
845 int32_t helper_cvttsd2si(CPUX86State
*env
, ZMMReg
*s
)
847 return x86_float64_to_int32_round_to_zero(s
->ZMM_D(0), &env
->sse_status
);
851 int64_t helper_cvttss2sq(CPUX86State
*env
, ZMMReg
*s
)
853 return x86_float32_to_int64_round_to_zero(s
->ZMM_S(0), &env
->sse_status
);
856 int64_t helper_cvttsd2sq(CPUX86State
*env
, ZMMReg
*s
)
858 return x86_float64_to_int64_round_to_zero(s
->ZMM_D(0), &env
->sse_status
);
862 void helper_rsqrtps(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
864 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
865 d
->ZMM_S(0) = float32_div(float32_one
,
866 float32_sqrt(s
->ZMM_S(0), &env
->sse_status
),
868 d
->ZMM_S(1) = float32_div(float32_one
,
869 float32_sqrt(s
->ZMM_S(1), &env
->sse_status
),
871 d
->ZMM_S(2) = float32_div(float32_one
,
872 float32_sqrt(s
->ZMM_S(2), &env
->sse_status
),
874 d
->ZMM_S(3) = float32_div(float32_one
,
875 float32_sqrt(s
->ZMM_S(3), &env
->sse_status
),
877 set_float_exception_flags(old_flags
, &env
->sse_status
);
880 void helper_rsqrtss(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
882 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
883 d
->ZMM_S(0) = float32_div(float32_one
,
884 float32_sqrt(s
->ZMM_S(0), &env
->sse_status
),
886 set_float_exception_flags(old_flags
, &env
->sse_status
);
889 void helper_rcpps(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
891 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
892 d
->ZMM_S(0) = float32_div(float32_one
, s
->ZMM_S(0), &env
->sse_status
);
893 d
->ZMM_S(1) = float32_div(float32_one
, s
->ZMM_S(1), &env
->sse_status
);
894 d
->ZMM_S(2) = float32_div(float32_one
, s
->ZMM_S(2), &env
->sse_status
);
895 d
->ZMM_S(3) = float32_div(float32_one
, s
->ZMM_S(3), &env
->sse_status
);
896 set_float_exception_flags(old_flags
, &env
->sse_status
);
899 void helper_rcpss(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
901 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
902 d
->ZMM_S(0) = float32_div(float32_one
, s
->ZMM_S(0), &env
->sse_status
);
903 set_float_exception_flags(old_flags
, &env
->sse_status
);
906 static inline uint64_t helper_extrq(uint64_t src
, int shift
, int len
)
913 mask
= (1ULL << len
) - 1;
915 return (src
>> shift
) & mask
;
918 void helper_extrq_r(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
920 d
->ZMM_Q(0) = helper_extrq(d
->ZMM_Q(0), s
->ZMM_B(1), s
->ZMM_B(0));
923 void helper_extrq_i(CPUX86State
*env
, ZMMReg
*d
, int index
, int length
)
925 d
->ZMM_Q(0) = helper_extrq(d
->ZMM_Q(0), index
, length
);
928 static inline uint64_t helper_insertq(uint64_t src
, int shift
, int len
)
935 mask
= (1ULL << len
) - 1;
937 return (src
& ~(mask
<< shift
)) | ((src
& mask
) << shift
);
940 void helper_insertq_r(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
942 d
->ZMM_Q(0) = helper_insertq(s
->ZMM_Q(0), s
->ZMM_B(9), s
->ZMM_B(8));
945 void helper_insertq_i(CPUX86State
*env
, ZMMReg
*d
, int index
, int length
)
947 d
->ZMM_Q(0) = helper_insertq(d
->ZMM_Q(0), index
, length
);
950 void helper_haddps(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
954 r
.ZMM_S(0) = float32_add(d
->ZMM_S(0), d
->ZMM_S(1), &env
->sse_status
);
955 r
.ZMM_S(1) = float32_add(d
->ZMM_S(2), d
->ZMM_S(3), &env
->sse_status
);
956 r
.ZMM_S(2) = float32_add(s
->ZMM_S(0), s
->ZMM_S(1), &env
->sse_status
);
957 r
.ZMM_S(3) = float32_add(s
->ZMM_S(2), s
->ZMM_S(3), &env
->sse_status
);
961 void helper_haddpd(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
965 r
.ZMM_D(0) = float64_add(d
->ZMM_D(0), d
->ZMM_D(1), &env
->sse_status
);
966 r
.ZMM_D(1) = float64_add(s
->ZMM_D(0), s
->ZMM_D(1), &env
->sse_status
);
970 void helper_hsubps(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
974 r
.ZMM_S(0) = float32_sub(d
->ZMM_S(0), d
->ZMM_S(1), &env
->sse_status
);
975 r
.ZMM_S(1) = float32_sub(d
->ZMM_S(2), d
->ZMM_S(3), &env
->sse_status
);
976 r
.ZMM_S(2) = float32_sub(s
->ZMM_S(0), s
->ZMM_S(1), &env
->sse_status
);
977 r
.ZMM_S(3) = float32_sub(s
->ZMM_S(2), s
->ZMM_S(3), &env
->sse_status
);
981 void helper_hsubpd(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
985 r
.ZMM_D(0) = float64_sub(d
->ZMM_D(0), d
->ZMM_D(1), &env
->sse_status
);
986 r
.ZMM_D(1) = float64_sub(s
->ZMM_D(0), s
->ZMM_D(1), &env
->sse_status
);
990 void helper_addsubps(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
992 d
->ZMM_S(0) = float32_sub(d
->ZMM_S(0), s
->ZMM_S(0), &env
->sse_status
);
993 d
->ZMM_S(1) = float32_add(d
->ZMM_S(1), s
->ZMM_S(1), &env
->sse_status
);
994 d
->ZMM_S(2) = float32_sub(d
->ZMM_S(2), s
->ZMM_S(2), &env
->sse_status
);
995 d
->ZMM_S(3) = float32_add(d
->ZMM_S(3), s
->ZMM_S(3), &env
->sse_status
);
998 void helper_addsubpd(CPUX86State
*env
, ZMMReg
*d
, ZMMReg
*s
)
1000 d
->ZMM_D(0) = float64_sub(d
->ZMM_D(0), s
->ZMM_D(0), &env
->sse_status
);
1001 d
->ZMM_D(1) = float64_add(d
->ZMM_D(1), s
->ZMM_D(1), &env
->sse_status
);
1004 /* XXX: unordered */
1005 #define SSE_HELPER_CMP(name, F) \
1006 void helper_ ## name ## ps(CPUX86State *env, Reg *d, Reg *s) \
1008 d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0)); \
1009 d->ZMM_L(1) = F(32, d->ZMM_S(1), s->ZMM_S(1)); \
1010 d->ZMM_L(2) = F(32, d->ZMM_S(2), s->ZMM_S(2)); \
1011 d->ZMM_L(3) = F(32, d->ZMM_S(3), s->ZMM_S(3)); \
1014 void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s) \
1016 d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0)); \
1019 void helper_ ## name ## pd(CPUX86State *env, Reg *d, Reg *s) \
1021 d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0)); \
1022 d->ZMM_Q(1) = F(64, d->ZMM_D(1), s->ZMM_D(1)); \
1025 void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s) \
1027 d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0)); \
1030 #define FPU_CMPEQ(size, a, b) \
1031 (float ## size ## _eq_quiet(a, b, &env->sse_status) ? -1 : 0)
1032 #define FPU_CMPLT(size, a, b) \
1033 (float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0)
1034 #define FPU_CMPLE(size, a, b) \
1035 (float ## size ## _le(a, b, &env->sse_status) ? -1 : 0)
1036 #define FPU_CMPUNORD(size, a, b) \
1037 (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? -1 : 0)
1038 #define FPU_CMPNEQ(size, a, b) \
1039 (float ## size ## _eq_quiet(a, b, &env->sse_status) ? 0 : -1)
1040 #define FPU_CMPNLT(size, a, b) \
1041 (float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1)
1042 #define FPU_CMPNLE(size, a, b) \
1043 (float ## size ## _le(a, b, &env->sse_status) ? 0 : -1)
1044 #define FPU_CMPORD(size, a, b) \
1045 (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? 0 : -1)
1047 SSE_HELPER_CMP(cmpeq
, FPU_CMPEQ
)
1048 SSE_HELPER_CMP(cmplt
, FPU_CMPLT
)
1049 SSE_HELPER_CMP(cmple
, FPU_CMPLE
)
1050 SSE_HELPER_CMP(cmpunord
, FPU_CMPUNORD
)
1051 SSE_HELPER_CMP(cmpneq
, FPU_CMPNEQ
)
1052 SSE_HELPER_CMP(cmpnlt
, FPU_CMPNLT
)
1053 SSE_HELPER_CMP(cmpnle
, FPU_CMPNLE
)
1054 SSE_HELPER_CMP(cmpord
, FPU_CMPORD
)
1056 static const int comis_eflags
[4] = {CC_C
, CC_Z
, 0, CC_Z
| CC_P
| CC_C
};
1058 void helper_ucomiss(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1065 ret
= float32_compare_quiet(s0
, s1
, &env
->sse_status
);
1066 CC_SRC
= comis_eflags
[ret
+ 1];
1069 void helper_comiss(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1076 ret
= float32_compare(s0
, s1
, &env
->sse_status
);
1077 CC_SRC
= comis_eflags
[ret
+ 1];
1080 void helper_ucomisd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1087 ret
= float64_compare_quiet(d0
, d1
, &env
->sse_status
);
1088 CC_SRC
= comis_eflags
[ret
+ 1];
1091 void helper_comisd(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1098 ret
= float64_compare(d0
, d1
, &env
->sse_status
);
1099 CC_SRC
= comis_eflags
[ret
+ 1];
1102 uint32_t helper_movmskps(CPUX86State
*env
, Reg
*s
)
1106 b0
= s
->ZMM_L(0) >> 31;
1107 b1
= s
->ZMM_L(1) >> 31;
1108 b2
= s
->ZMM_L(2) >> 31;
1109 b3
= s
->ZMM_L(3) >> 31;
1110 return b0
| (b1
<< 1) | (b2
<< 2) | (b3
<< 3);
1113 uint32_t helper_movmskpd(CPUX86State
*env
, Reg
*s
)
1117 b0
= s
->ZMM_L(1) >> 31;
1118 b1
= s
->ZMM_L(3) >> 31;
1119 return b0
| (b1
<< 1);
1124 uint32_t glue(helper_pmovmskb
, SUFFIX
)(CPUX86State
*env
, Reg
*s
)
1129 val
|= (s
->B(0) >> 7);
1130 val
|= (s
->B(1) >> 6) & 0x02;
1131 val
|= (s
->B(2) >> 5) & 0x04;
1132 val
|= (s
->B(3) >> 4) & 0x08;
1133 val
|= (s
->B(4) >> 3) & 0x10;
1134 val
|= (s
->B(5) >> 2) & 0x20;
1135 val
|= (s
->B(6) >> 1) & 0x40;
1136 val
|= (s
->B(7)) & 0x80;
1138 val
|= (s
->B(8) << 1) & 0x0100;
1139 val
|= (s
->B(9) << 2) & 0x0200;
1140 val
|= (s
->B(10) << 3) & 0x0400;
1141 val
|= (s
->B(11) << 4) & 0x0800;
1142 val
|= (s
->B(12) << 5) & 0x1000;
1143 val
|= (s
->B(13) << 6) & 0x2000;
1144 val
|= (s
->B(14) << 7) & 0x4000;
1145 val
|= (s
->B(15) << 8) & 0x8000;
1150 void glue(helper_packsswb
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1154 r
.B(0) = satsb((int16_t)d
->W(0));
1155 r
.B(1) = satsb((int16_t)d
->W(1));
1156 r
.B(2) = satsb((int16_t)d
->W(2));
1157 r
.B(3) = satsb((int16_t)d
->W(3));
1159 r
.B(4) = satsb((int16_t)d
->W(4));
1160 r
.B(5) = satsb((int16_t)d
->W(5));
1161 r
.B(6) = satsb((int16_t)d
->W(6));
1162 r
.B(7) = satsb((int16_t)d
->W(7));
1164 r
.B((4 << SHIFT
) + 0) = satsb((int16_t)s
->W(0));
1165 r
.B((4 << SHIFT
) + 1) = satsb((int16_t)s
->W(1));
1166 r
.B((4 << SHIFT
) + 2) = satsb((int16_t)s
->W(2));
1167 r
.B((4 << SHIFT
) + 3) = satsb((int16_t)s
->W(3));
1169 r
.B(12) = satsb((int16_t)s
->W(4));
1170 r
.B(13) = satsb((int16_t)s
->W(5));
1171 r
.B(14) = satsb((int16_t)s
->W(6));
1172 r
.B(15) = satsb((int16_t)s
->W(7));
1177 void glue(helper_packuswb
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1181 r
.B(0) = satub((int16_t)d
->W(0));
1182 r
.B(1) = satub((int16_t)d
->W(1));
1183 r
.B(2) = satub((int16_t)d
->W(2));
1184 r
.B(3) = satub((int16_t)d
->W(3));
1186 r
.B(4) = satub((int16_t)d
->W(4));
1187 r
.B(5) = satub((int16_t)d
->W(5));
1188 r
.B(6) = satub((int16_t)d
->W(6));
1189 r
.B(7) = satub((int16_t)d
->W(7));
1191 r
.B((4 << SHIFT
) + 0) = satub((int16_t)s
->W(0));
1192 r
.B((4 << SHIFT
) + 1) = satub((int16_t)s
->W(1));
1193 r
.B((4 << SHIFT
) + 2) = satub((int16_t)s
->W(2));
1194 r
.B((4 << SHIFT
) + 3) = satub((int16_t)s
->W(3));
1196 r
.B(12) = satub((int16_t)s
->W(4));
1197 r
.B(13) = satub((int16_t)s
->W(5));
1198 r
.B(14) = satub((int16_t)s
->W(6));
1199 r
.B(15) = satub((int16_t)s
->W(7));
1204 void glue(helper_packssdw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1208 r
.W(0) = satsw(d
->L(0));
1209 r
.W(1) = satsw(d
->L(1));
1211 r
.W(2) = satsw(d
->L(2));
1212 r
.W(3) = satsw(d
->L(3));
1214 r
.W((2 << SHIFT
) + 0) = satsw(s
->L(0));
1215 r
.W((2 << SHIFT
) + 1) = satsw(s
->L(1));
1217 r
.W(6) = satsw(s
->L(2));
1218 r
.W(7) = satsw(s
->L(3));
1223 #define UNPCK_OP(base_name, base) \
1225 void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\
1230 r.B(0) = d->B((base << (SHIFT + 2)) + 0); \
1231 r.B(1) = s->B((base << (SHIFT + 2)) + 0); \
1232 r.B(2) = d->B((base << (SHIFT + 2)) + 1); \
1233 r.B(3) = s->B((base << (SHIFT + 2)) + 1); \
1234 r.B(4) = d->B((base << (SHIFT + 2)) + 2); \
1235 r.B(5) = s->B((base << (SHIFT + 2)) + 2); \
1236 r.B(6) = d->B((base << (SHIFT + 2)) + 3); \
1237 r.B(7) = s->B((base << (SHIFT + 2)) + 3); \
1239 r.B(8) = d->B((base << (SHIFT + 2)) + 4); \
1240 r.B(9) = s->B((base << (SHIFT + 2)) + 4); \
1241 r.B(10) = d->B((base << (SHIFT + 2)) + 5); \
1242 r.B(11) = s->B((base << (SHIFT + 2)) + 5); \
1243 r.B(12) = d->B((base << (SHIFT + 2)) + 6); \
1244 r.B(13) = s->B((base << (SHIFT + 2)) + 6); \
1245 r.B(14) = d->B((base << (SHIFT + 2)) + 7); \
1246 r.B(15) = s->B((base << (SHIFT + 2)) + 7); \
1251 void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\
1256 r.W(0) = d->W((base << (SHIFT + 1)) + 0); \
1257 r.W(1) = s->W((base << (SHIFT + 1)) + 0); \
1258 r.W(2) = d->W((base << (SHIFT + 1)) + 1); \
1259 r.W(3) = s->W((base << (SHIFT + 1)) + 1); \
1261 r.W(4) = d->W((base << (SHIFT + 1)) + 2); \
1262 r.W(5) = s->W((base << (SHIFT + 1)) + 2); \
1263 r.W(6) = d->W((base << (SHIFT + 1)) + 3); \
1264 r.W(7) = s->W((base << (SHIFT + 1)) + 3); \
1269 void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\
1274 r.L(0) = d->L((base << SHIFT) + 0); \
1275 r.L(1) = s->L((base << SHIFT) + 0); \
1277 r.L(2) = d->L((base << SHIFT) + 1); \
1278 r.L(3) = s->L((base << SHIFT) + 1); \
1284 void glue(helper_punpck ## base_name ## qdq, SUFFIX)(CPUX86State \
1291 r.Q(0) = d->Q(base); \
1292 r.Q(1) = s->Q(base); \
1300 /* 3DNow! float ops */
1302 void helper_pi2fd(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1304 d
->MMX_S(0) = int32_to_float32(s
->MMX_L(0), &env
->mmx_status
);
1305 d
->MMX_S(1) = int32_to_float32(s
->MMX_L(1), &env
->mmx_status
);
1308 void helper_pi2fw(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1310 d
->MMX_S(0) = int32_to_float32((int16_t)s
->MMX_W(0), &env
->mmx_status
);
1311 d
->MMX_S(1) = int32_to_float32((int16_t)s
->MMX_W(2), &env
->mmx_status
);
1314 void helper_pf2id(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1316 d
->MMX_L(0) = float32_to_int32_round_to_zero(s
->MMX_S(0), &env
->mmx_status
);
1317 d
->MMX_L(1) = float32_to_int32_round_to_zero(s
->MMX_S(1), &env
->mmx_status
);
1320 void helper_pf2iw(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1322 d
->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s
->MMX_S(0),
1324 d
->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s
->MMX_S(1),
1328 void helper_pfacc(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1332 r
.MMX_S(0) = float32_add(d
->MMX_S(0), d
->MMX_S(1), &env
->mmx_status
);
1333 r
.MMX_S(1) = float32_add(s
->MMX_S(0), s
->MMX_S(1), &env
->mmx_status
);
1337 void helper_pfadd(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1339 d
->MMX_S(0) = float32_add(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
);
1340 d
->MMX_S(1) = float32_add(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
);
1343 void helper_pfcmpeq(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1345 d
->MMX_L(0) = float32_eq_quiet(d
->MMX_S(0), s
->MMX_S(0),
1346 &env
->mmx_status
) ? -1 : 0;
1347 d
->MMX_L(1) = float32_eq_quiet(d
->MMX_S(1), s
->MMX_S(1),
1348 &env
->mmx_status
) ? -1 : 0;
1351 void helper_pfcmpge(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1353 d
->MMX_L(0) = float32_le(s
->MMX_S(0), d
->MMX_S(0),
1354 &env
->mmx_status
) ? -1 : 0;
1355 d
->MMX_L(1) = float32_le(s
->MMX_S(1), d
->MMX_S(1),
1356 &env
->mmx_status
) ? -1 : 0;
1359 void helper_pfcmpgt(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1361 d
->MMX_L(0) = float32_lt(s
->MMX_S(0), d
->MMX_S(0),
1362 &env
->mmx_status
) ? -1 : 0;
1363 d
->MMX_L(1) = float32_lt(s
->MMX_S(1), d
->MMX_S(1),
1364 &env
->mmx_status
) ? -1 : 0;
1367 void helper_pfmax(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1369 if (float32_lt(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
)) {
1370 d
->MMX_S(0) = s
->MMX_S(0);
1372 if (float32_lt(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
)) {
1373 d
->MMX_S(1) = s
->MMX_S(1);
1377 void helper_pfmin(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1379 if (float32_lt(s
->MMX_S(0), d
->MMX_S(0), &env
->mmx_status
)) {
1380 d
->MMX_S(0) = s
->MMX_S(0);
1382 if (float32_lt(s
->MMX_S(1), d
->MMX_S(1), &env
->mmx_status
)) {
1383 d
->MMX_S(1) = s
->MMX_S(1);
1387 void helper_pfmul(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1389 d
->MMX_S(0) = float32_mul(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
);
1390 d
->MMX_S(1) = float32_mul(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
);
1393 void helper_pfnacc(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1397 r
.MMX_S(0) = float32_sub(d
->MMX_S(0), d
->MMX_S(1), &env
->mmx_status
);
1398 r
.MMX_S(1) = float32_sub(s
->MMX_S(0), s
->MMX_S(1), &env
->mmx_status
);
1402 void helper_pfpnacc(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1406 r
.MMX_S(0) = float32_sub(d
->MMX_S(0), d
->MMX_S(1), &env
->mmx_status
);
1407 r
.MMX_S(1) = float32_add(s
->MMX_S(0), s
->MMX_S(1), &env
->mmx_status
);
1411 void helper_pfrcp(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1413 d
->MMX_S(0) = float32_div(float32_one
, s
->MMX_S(0), &env
->mmx_status
);
1414 d
->MMX_S(1) = d
->MMX_S(0);
1417 void helper_pfrsqrt(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1419 d
->MMX_L(1) = s
->MMX_L(0) & 0x7fffffff;
1420 d
->MMX_S(1) = float32_div(float32_one
,
1421 float32_sqrt(d
->MMX_S(1), &env
->mmx_status
),
1423 d
->MMX_L(1) |= s
->MMX_L(0) & 0x80000000;
1424 d
->MMX_L(0) = d
->MMX_L(1);
1427 void helper_pfsub(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1429 d
->MMX_S(0) = float32_sub(d
->MMX_S(0), s
->MMX_S(0), &env
->mmx_status
);
1430 d
->MMX_S(1) = float32_sub(d
->MMX_S(1), s
->MMX_S(1), &env
->mmx_status
);
1433 void helper_pfsubr(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1435 d
->MMX_S(0) = float32_sub(s
->MMX_S(0), d
->MMX_S(0), &env
->mmx_status
);
1436 d
->MMX_S(1) = float32_sub(s
->MMX_S(1), d
->MMX_S(1), &env
->mmx_status
);
1439 void helper_pswapd(CPUX86State
*env
, MMXReg
*d
, MMXReg
*s
)
1443 r
.MMX_L(0) = s
->MMX_L(1);
1444 r
.MMX_L(1) = s
->MMX_L(0);
1449 /* SSSE3 op helpers */
1450 void glue(helper_pshufb
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1455 for (i
= 0; i
< (8 << SHIFT
); i
++) {
1456 r
.B(i
) = (s
->B(i
) & 0x80) ? 0 : (d
->B(s
->B(i
) & ((8 << SHIFT
) - 1)));
1462 void glue(helper_phaddw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1467 r
.W(0) = (int16_t)d
->W(0) + (int16_t)d
->W(1);
1468 r
.W(1) = (int16_t)d
->W(2) + (int16_t)d
->W(3);
1469 XMM_ONLY(r
.W(2) = (int16_t)d
->W(4) + (int16_t)d
->W(5));
1470 XMM_ONLY(r
.W(3) = (int16_t)d
->W(6) + (int16_t)d
->W(7));
1471 r
.W((2 << SHIFT
) + 0) = (int16_t)s
->W(0) + (int16_t)s
->W(1);
1472 r
.W((2 << SHIFT
) + 1) = (int16_t)s
->W(2) + (int16_t)s
->W(3);
1473 XMM_ONLY(r
.W(6) = (int16_t)s
->W(4) + (int16_t)s
->W(5));
1474 XMM_ONLY(r
.W(7) = (int16_t)s
->W(6) + (int16_t)s
->W(7));
1479 void glue(helper_phaddd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1483 r
.L(0) = (int32_t)d
->L(0) + (int32_t)d
->L(1);
1484 XMM_ONLY(r
.L(1) = (int32_t)d
->L(2) + (int32_t)d
->L(3));
1485 r
.L((1 << SHIFT
) + 0) = (int32_t)s
->L(0) + (int32_t)s
->L(1);
1486 XMM_ONLY(r
.L(3) = (int32_t)s
->L(2) + (int32_t)s
->L(3));
1491 void glue(helper_phaddsw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1495 r
.W(0) = satsw((int16_t)d
->W(0) + (int16_t)d
->W(1));
1496 r
.W(1) = satsw((int16_t)d
->W(2) + (int16_t)d
->W(3));
1497 XMM_ONLY(r
.W(2) = satsw((int16_t)d
->W(4) + (int16_t)d
->W(5)));
1498 XMM_ONLY(r
.W(3) = satsw((int16_t)d
->W(6) + (int16_t)d
->W(7)));
1499 r
.W((2 << SHIFT
) + 0) = satsw((int16_t)s
->W(0) + (int16_t)s
->W(1));
1500 r
.W((2 << SHIFT
) + 1) = satsw((int16_t)s
->W(2) + (int16_t)s
->W(3));
1501 XMM_ONLY(r
.W(6) = satsw((int16_t)s
->W(4) + (int16_t)s
->W(5)));
1502 XMM_ONLY(r
.W(7) = satsw((int16_t)s
->W(6) + (int16_t)s
->W(7)));
1507 void glue(helper_pmaddubsw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1509 d
->W(0) = satsw((int8_t)s
->B(0) * (uint8_t)d
->B(0) +
1510 (int8_t)s
->B(1) * (uint8_t)d
->B(1));
1511 d
->W(1) = satsw((int8_t)s
->B(2) * (uint8_t)d
->B(2) +
1512 (int8_t)s
->B(3) * (uint8_t)d
->B(3));
1513 d
->W(2) = satsw((int8_t)s
->B(4) * (uint8_t)d
->B(4) +
1514 (int8_t)s
->B(5) * (uint8_t)d
->B(5));
1515 d
->W(3) = satsw((int8_t)s
->B(6) * (uint8_t)d
->B(6) +
1516 (int8_t)s
->B(7) * (uint8_t)d
->B(7));
1518 d
->W(4) = satsw((int8_t)s
->B(8) * (uint8_t)d
->B(8) +
1519 (int8_t)s
->B(9) * (uint8_t)d
->B(9));
1520 d
->W(5) = satsw((int8_t)s
->B(10) * (uint8_t)d
->B(10) +
1521 (int8_t)s
->B(11) * (uint8_t)d
->B(11));
1522 d
->W(6) = satsw((int8_t)s
->B(12) * (uint8_t)d
->B(12) +
1523 (int8_t)s
->B(13) * (uint8_t)d
->B(13));
1524 d
->W(7) = satsw((int8_t)s
->B(14) * (uint8_t)d
->B(14) +
1525 (int8_t)s
->B(15) * (uint8_t)d
->B(15));
1529 void glue(helper_phsubw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1531 d
->W(0) = (int16_t)d
->W(0) - (int16_t)d
->W(1);
1532 d
->W(1) = (int16_t)d
->W(2) - (int16_t)d
->W(3);
1533 XMM_ONLY(d
->W(2) = (int16_t)d
->W(4) - (int16_t)d
->W(5));
1534 XMM_ONLY(d
->W(3) = (int16_t)d
->W(6) - (int16_t)d
->W(7));
1535 d
->W((2 << SHIFT
) + 0) = (int16_t)s
->W(0) - (int16_t)s
->W(1);
1536 d
->W((2 << SHIFT
) + 1) = (int16_t)s
->W(2) - (int16_t)s
->W(3);
1537 XMM_ONLY(d
->W(6) = (int16_t)s
->W(4) - (int16_t)s
->W(5));
1538 XMM_ONLY(d
->W(7) = (int16_t)s
->W(6) - (int16_t)s
->W(7));
1541 void glue(helper_phsubd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1543 d
->L(0) = (int32_t)d
->L(0) - (int32_t)d
->L(1);
1544 XMM_ONLY(d
->L(1) = (int32_t)d
->L(2) - (int32_t)d
->L(3));
1545 d
->L((1 << SHIFT
) + 0) = (int32_t)s
->L(0) - (int32_t)s
->L(1);
1546 XMM_ONLY(d
->L(3) = (int32_t)s
->L(2) - (int32_t)s
->L(3));
1549 void glue(helper_phsubsw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1551 d
->W(0) = satsw((int16_t)d
->W(0) - (int16_t)d
->W(1));
1552 d
->W(1) = satsw((int16_t)d
->W(2) - (int16_t)d
->W(3));
1553 XMM_ONLY(d
->W(2) = satsw((int16_t)d
->W(4) - (int16_t)d
->W(5)));
1554 XMM_ONLY(d
->W(3) = satsw((int16_t)d
->W(6) - (int16_t)d
->W(7)));
1555 d
->W((2 << SHIFT
) + 0) = satsw((int16_t)s
->W(0) - (int16_t)s
->W(1));
1556 d
->W((2 << SHIFT
) + 1) = satsw((int16_t)s
->W(2) - (int16_t)s
->W(3));
1557 XMM_ONLY(d
->W(6) = satsw((int16_t)s
->W(4) - (int16_t)s
->W(5)));
1558 XMM_ONLY(d
->W(7) = satsw((int16_t)s
->W(6) - (int16_t)s
->W(7)));
1561 #define FABSB(_, x) (x > INT8_MAX ? -(int8_t)x : x)
1562 #define FABSW(_, x) (x > INT16_MAX ? -(int16_t)x : x)
1563 #define FABSL(_, x) (x > INT32_MAX ? -(int32_t)x : x)
1564 SSE_HELPER_B(helper_pabsb
, FABSB
)
1565 SSE_HELPER_W(helper_pabsw
, FABSW
)
1566 SSE_HELPER_L(helper_pabsd
, FABSL
)
1568 #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15)
1569 SSE_HELPER_W(helper_pmulhrsw
, FMULHRSW
)
1571 #define FSIGNB(d, s) (s <= INT8_MAX ? s ? d : 0 : -(int8_t)d)
1572 #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d)
1573 #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d)
1574 SSE_HELPER_B(helper_psignb
, FSIGNB
)
1575 SSE_HELPER_W(helper_psignw
, FSIGNW
)
1576 SSE_HELPER_L(helper_psignd
, FSIGNL
)
1578 void glue(helper_palignr
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1583 /* XXX could be checked during translation */
1584 if (shift
>= (16 << SHIFT
)) {
1586 XMM_ONLY(r
.Q(1) = 0);
1589 #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0)
1591 r
.Q(0) = SHR(s
->Q(0), shift
- 0) |
1592 SHR(d
->Q(0), shift
- 64);
1594 r
.Q(0) = SHR(s
->Q(0), shift
- 0) |
1595 SHR(s
->Q(1), shift
- 64) |
1596 SHR(d
->Q(0), shift
- 128) |
1597 SHR(d
->Q(1), shift
- 192);
1598 r
.Q(1) = SHR(s
->Q(0), shift
+ 64) |
1599 SHR(s
->Q(1), shift
- 0) |
1600 SHR(d
->Q(0), shift
- 64) |
1601 SHR(d
->Q(1), shift
- 128);
1609 #define XMM0 (env->xmm_regs[0])
1612 #define SSE_HELPER_V(name, elem, num, F) \
1613 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1615 d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0)); \
1616 d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1)); \
1618 d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2)); \
1619 d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3)); \
1621 d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4)); \
1622 d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5)); \
1623 d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6)); \
1624 d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7)); \
1626 d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \
1627 d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \
1628 d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \
1629 d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \
1630 d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \
1631 d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \
1632 d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \
1633 d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \
1639 #define SSE_HELPER_I(name, elem, num, F) \
1640 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \
1642 d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1)); \
1643 d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1)); \
1645 d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1)); \
1646 d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1)); \
1648 d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \
1649 d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \
1650 d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \
1651 d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \
1653 d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \
1654 d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \
1655 d->elem(10) = F(d->elem(10), s->elem(10), \
1656 ((imm >> 10) & 1)); \
1657 d->elem(11) = F(d->elem(11), s->elem(11), \
1658 ((imm >> 11) & 1)); \
1659 d->elem(12) = F(d->elem(12), s->elem(12), \
1660 ((imm >> 12) & 1)); \
1661 d->elem(13) = F(d->elem(13), s->elem(13), \
1662 ((imm >> 13) & 1)); \
1663 d->elem(14) = F(d->elem(14), s->elem(14), \
1664 ((imm >> 14) & 1)); \
1665 d->elem(15) = F(d->elem(15), s->elem(15), \
1666 ((imm >> 15) & 1)); \
1672 /* SSE4.1 op helpers */
1673 #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d)
1674 #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d)
1675 #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d)
1676 SSE_HELPER_V(helper_pblendvb
, B
, 16, FBLENDVB
)
1677 SSE_HELPER_V(helper_blendvps
, L
, 4, FBLENDVPS
)
1678 SSE_HELPER_V(helper_blendvpd
, Q
, 2, FBLENDVPD
)
1680 void glue(helper_ptest
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1682 uint64_t zf
= (s
->Q(0) & d
->Q(0)) | (s
->Q(1) & d
->Q(1));
1683 uint64_t cf
= (s
->Q(0) & ~d
->Q(0)) | (s
->Q(1) & ~d
->Q(1));
1685 CC_SRC
= (zf
? 0 : CC_Z
) | (cf
? 0 : CC_C
);
1688 #define SSE_HELPER_F(name, elem, num, F) \
1689 void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \
1693 d->elem(7) = F(7); \
1694 d->elem(6) = F(6); \
1695 d->elem(5) = F(5); \
1696 d->elem(4) = F(4); \
1698 d->elem(3) = F(3); \
1699 d->elem(2) = F(2); \
1701 d->elem(1) = F(1); \
1702 d->elem(0) = F(0); \
1705 SSE_HELPER_F(helper_pmovsxbw
, W
, 8, (int8_t) s
->B
)
1706 SSE_HELPER_F(helper_pmovsxbd
, L
, 4, (int8_t) s
->B
)
1707 SSE_HELPER_F(helper_pmovsxbq
, Q
, 2, (int8_t) s
->B
)
1708 SSE_HELPER_F(helper_pmovsxwd
, L
, 4, (int16_t) s
->W
)
1709 SSE_HELPER_F(helper_pmovsxwq
, Q
, 2, (int16_t) s
->W
)
1710 SSE_HELPER_F(helper_pmovsxdq
, Q
, 2, (int32_t) s
->L
)
1711 SSE_HELPER_F(helper_pmovzxbw
, W
, 8, s
->B
)
1712 SSE_HELPER_F(helper_pmovzxbd
, L
, 4, s
->B
)
1713 SSE_HELPER_F(helper_pmovzxbq
, Q
, 2, s
->B
)
1714 SSE_HELPER_F(helper_pmovzxwd
, L
, 4, s
->W
)
1715 SSE_HELPER_F(helper_pmovzxwq
, Q
, 2, s
->W
)
1716 SSE_HELPER_F(helper_pmovzxdq
, Q
, 2, s
->L
)
1718 void glue(helper_pmuldq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1720 d
->Q(0) = (int64_t)(int32_t) d
->L(0) * (int32_t) s
->L(0);
1721 d
->Q(1) = (int64_t)(int32_t) d
->L(2) * (int32_t) s
->L(2);
1724 #define FCMPEQQ(d, s) (d == s ? -1 : 0)
1725 SSE_HELPER_Q(helper_pcmpeqq
, FCMPEQQ
)
1727 void glue(helper_packusdw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1731 r
.W(0) = satuw((int32_t) d
->L(0));
1732 r
.W(1) = satuw((int32_t) d
->L(1));
1733 r
.W(2) = satuw((int32_t) d
->L(2));
1734 r
.W(3) = satuw((int32_t) d
->L(3));
1735 r
.W(4) = satuw((int32_t) s
->L(0));
1736 r
.W(5) = satuw((int32_t) s
->L(1));
1737 r
.W(6) = satuw((int32_t) s
->L(2));
1738 r
.W(7) = satuw((int32_t) s
->L(3));
1742 #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s)
1743 #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s)
1744 #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s)
1745 #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s)
1746 SSE_HELPER_B(helper_pminsb
, FMINSB
)
1747 SSE_HELPER_L(helper_pminsd
, FMINSD
)
1748 SSE_HELPER_W(helper_pminuw
, MIN
)
1749 SSE_HELPER_L(helper_pminud
, MIN
)
1750 SSE_HELPER_B(helper_pmaxsb
, FMAXSB
)
1751 SSE_HELPER_L(helper_pmaxsd
, FMAXSD
)
1752 SSE_HELPER_W(helper_pmaxuw
, MAX
)
1753 SSE_HELPER_L(helper_pmaxud
, MAX
)
1755 #define FMULLD(d, s) ((int32_t)d * (int32_t)s)
1756 SSE_HELPER_L(helper_pmulld
, FMULLD
)
1758 void glue(helper_phminposuw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
1762 if (s
->W(1) < s
->W(idx
)) {
1765 if (s
->W(2) < s
->W(idx
)) {
1768 if (s
->W(3) < s
->W(idx
)) {
1771 if (s
->W(4) < s
->W(idx
)) {
1774 if (s
->W(5) < s
->W(idx
)) {
1777 if (s
->W(6) < s
->W(idx
)) {
1780 if (s
->W(7) < s
->W(idx
)) {
1784 d
->W(0) = s
->W(idx
);
1790 void glue(helper_roundps
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1793 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
1794 signed char prev_rounding_mode
;
1796 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1797 if (!(mode
& (1 << 2))) {
1800 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1803 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1806 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1809 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1814 d
->ZMM_S(0) = float32_round_to_int(s
->ZMM_S(0), &env
->sse_status
);
1815 d
->ZMM_S(1) = float32_round_to_int(s
->ZMM_S(1), &env
->sse_status
);
1816 d
->ZMM_S(2) = float32_round_to_int(s
->ZMM_S(2), &env
->sse_status
);
1817 d
->ZMM_S(3) = float32_round_to_int(s
->ZMM_S(3), &env
->sse_status
);
1819 if (mode
& (1 << 3) && !(old_flags
& float_flag_inexact
)) {
1820 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1821 ~float_flag_inexact
,
1824 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1827 void glue(helper_roundpd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1830 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
1831 signed char prev_rounding_mode
;
1833 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1834 if (!(mode
& (1 << 2))) {
1837 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1840 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1843 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1846 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1851 d
->ZMM_D(0) = float64_round_to_int(s
->ZMM_D(0), &env
->sse_status
);
1852 d
->ZMM_D(1) = float64_round_to_int(s
->ZMM_D(1), &env
->sse_status
);
1854 if (mode
& (1 << 3) && !(old_flags
& float_flag_inexact
)) {
1855 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1856 ~float_flag_inexact
,
1859 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1862 void glue(helper_roundss
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1865 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
1866 signed char prev_rounding_mode
;
1868 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1869 if (!(mode
& (1 << 2))) {
1872 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1875 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1878 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1881 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1886 d
->ZMM_S(0) = float32_round_to_int(s
->ZMM_S(0), &env
->sse_status
);
1888 if (mode
& (1 << 3) && !(old_flags
& float_flag_inexact
)) {
1889 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1890 ~float_flag_inexact
,
1893 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1896 void glue(helper_roundsd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1899 uint8_t old_flags
= get_float_exception_flags(&env
->sse_status
);
1900 signed char prev_rounding_mode
;
1902 prev_rounding_mode
= env
->sse_status
.float_rounding_mode
;
1903 if (!(mode
& (1 << 2))) {
1906 set_float_rounding_mode(float_round_nearest_even
, &env
->sse_status
);
1909 set_float_rounding_mode(float_round_down
, &env
->sse_status
);
1912 set_float_rounding_mode(float_round_up
, &env
->sse_status
);
1915 set_float_rounding_mode(float_round_to_zero
, &env
->sse_status
);
1920 d
->ZMM_D(0) = float64_round_to_int(s
->ZMM_D(0), &env
->sse_status
);
1922 if (mode
& (1 << 3) && !(old_flags
& float_flag_inexact
)) {
1923 set_float_exception_flags(get_float_exception_flags(&env
->sse_status
) &
1924 ~float_flag_inexact
,
1927 env
->sse_status
.float_rounding_mode
= prev_rounding_mode
;
1930 #define FBLENDP(d, s, m) (m ? s : d)
1931 SSE_HELPER_I(helper_blendps
, L
, 4, FBLENDP
)
1932 SSE_HELPER_I(helper_blendpd
, Q
, 2, FBLENDP
)
1933 SSE_HELPER_I(helper_pblendw
, W
, 8, FBLENDP
)
1935 void glue(helper_dpps
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
, uint32_t mask
)
1937 float32 iresult
= float32_zero
;
1939 if (mask
& (1 << 4)) {
1940 iresult
= float32_add(iresult
,
1941 float32_mul(d
->ZMM_S(0), s
->ZMM_S(0),
1945 if (mask
& (1 << 5)) {
1946 iresult
= float32_add(iresult
,
1947 float32_mul(d
->ZMM_S(1), s
->ZMM_S(1),
1951 if (mask
& (1 << 6)) {
1952 iresult
= float32_add(iresult
,
1953 float32_mul(d
->ZMM_S(2), s
->ZMM_S(2),
1957 if (mask
& (1 << 7)) {
1958 iresult
= float32_add(iresult
,
1959 float32_mul(d
->ZMM_S(3), s
->ZMM_S(3),
1963 d
->ZMM_S(0) = (mask
& (1 << 0)) ? iresult
: float32_zero
;
1964 d
->ZMM_S(1) = (mask
& (1 << 1)) ? iresult
: float32_zero
;
1965 d
->ZMM_S(2) = (mask
& (1 << 2)) ? iresult
: float32_zero
;
1966 d
->ZMM_S(3) = (mask
& (1 << 3)) ? iresult
: float32_zero
;
1969 void glue(helper_dppd
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
, uint32_t mask
)
1971 float64 iresult
= float64_zero
;
1973 if (mask
& (1 << 4)) {
1974 iresult
= float64_add(iresult
,
1975 float64_mul(d
->ZMM_D(0), s
->ZMM_D(0),
1979 if (mask
& (1 << 5)) {
1980 iresult
= float64_add(iresult
,
1981 float64_mul(d
->ZMM_D(1), s
->ZMM_D(1),
1985 d
->ZMM_D(0) = (mask
& (1 << 0)) ? iresult
: float64_zero
;
1986 d
->ZMM_D(1) = (mask
& (1 << 1)) ? iresult
: float64_zero
;
1989 void glue(helper_mpsadbw
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
1992 int s0
= (offset
& 3) << 2;
1993 int d0
= (offset
& 4) << 0;
1997 for (i
= 0; i
< 8; i
++, d0
++) {
1999 r
.W(i
) += abs1(d
->B(d0
+ 0) - s
->B(s0
+ 0));
2000 r
.W(i
) += abs1(d
->B(d0
+ 1) - s
->B(s0
+ 1));
2001 r
.W(i
) += abs1(d
->B(d0
+ 2) - s
->B(s0
+ 2));
2002 r
.W(i
) += abs1(d
->B(d0
+ 3) - s
->B(s0
+ 3));
2008 /* SSE4.2 op helpers */
2009 #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0)
2010 SSE_HELPER_Q(helper_pcmpgtq
, FCMPGTQ
)
2012 static inline int pcmp_elen(CPUX86State
*env
, int reg
, uint32_t ctrl
)
2014 target_long val
, limit
;
2016 /* Presence of REX.W is indicated by a bit higher than 7 set */
2018 val
= (target_long
)env
->regs
[reg
];
2020 val
= (int32_t)env
->regs
[reg
];
2027 if ((val
> limit
) || (val
< -limit
)) {
2033 static inline int pcmp_ilen(Reg
*r
, uint8_t ctrl
)
2038 while (val
< 8 && r
->W(val
)) {
2042 while (val
< 16 && r
->B(val
)) {
2050 static inline int pcmp_val(Reg
*r
, uint8_t ctrl
, int i
)
2052 switch ((ctrl
>> 0) & 3) {
2058 return (int8_t)r
->B(i
);
2061 return (int16_t)r
->W(i
);
2065 static inline unsigned pcmpxstrx(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2066 int8_t ctrl
, int valids
, int validd
)
2068 unsigned int res
= 0;
2071 int upper
= (ctrl
& 1) ? 7 : 15;
2076 CC_SRC
= (valids
< upper
? CC_Z
: 0) | (validd
< upper
? CC_S
: 0);
2078 switch ((ctrl
>> 2) & 3) {
2080 for (j
= valids
; j
>= 0; j
--) {
2082 v
= pcmp_val(s
, ctrl
, j
);
2083 for (i
= validd
; i
>= 0; i
--) {
2084 res
|= (v
== pcmp_val(d
, ctrl
, i
));
2089 for (j
= valids
; j
>= 0; j
--) {
2091 v
= pcmp_val(s
, ctrl
, j
);
2092 for (i
= ((validd
- 1) | 1); i
>= 0; i
-= 2) {
2093 res
|= (pcmp_val(d
, ctrl
, i
- 0) >= v
&&
2094 pcmp_val(d
, ctrl
, i
- 1) <= v
);
2099 res
= (1 << (upper
- MAX(valids
, validd
))) - 1;
2100 res
<<= MAX(valids
, validd
) - MIN(valids
, validd
);
2101 for (i
= MIN(valids
, validd
); i
>= 0; i
--) {
2103 v
= pcmp_val(s
, ctrl
, i
);
2104 res
|= (v
== pcmp_val(d
, ctrl
, i
));
2109 res
= (2 << upper
) - 1;
2112 for (j
= valids
== upper
? valids
: valids
- validd
; j
>= 0; j
--) {
2115 for (i
= MIN(valids
- j
, validd
); i
>= 0; i
--) {
2116 v
&= (pcmp_val(s
, ctrl
, i
+ j
) == pcmp_val(d
, ctrl
, i
));
2123 switch ((ctrl
>> 4) & 3) {
2125 res
^= (2 << upper
) - 1;
2128 res
^= (1 << (valids
+ 1)) - 1;
2142 void glue(helper_pcmpestri
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2145 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2146 pcmp_elen(env
, R_EDX
, ctrl
),
2147 pcmp_elen(env
, R_EAX
, ctrl
));
2150 env
->regs
[R_ECX
] = (ctrl
& (1 << 6)) ? 31 - clz32(res
) : ctz32(res
);
2152 env
->regs
[R_ECX
] = 16 >> (ctrl
& (1 << 0));
2156 void glue(helper_pcmpestrm
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2160 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2161 pcmp_elen(env
, R_EDX
, ctrl
),
2162 pcmp_elen(env
, R_EAX
, ctrl
));
2164 if ((ctrl
>> 6) & 1) {
2166 for (i
= 0; i
< 8; i
++, res
>>= 1) {
2167 env
->xmm_regs
[0].W(i
) = (res
& 1) ? ~0 : 0;
2170 for (i
= 0; i
< 16; i
++, res
>>= 1) {
2171 env
->xmm_regs
[0].B(i
) = (res
& 1) ? ~0 : 0;
2175 env
->xmm_regs
[0].Q(1) = 0;
2176 env
->xmm_regs
[0].Q(0) = res
;
2180 void glue(helper_pcmpistri
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2183 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2185 pcmp_ilen(d
, ctrl
));
2188 env
->regs
[R_ECX
] = (ctrl
& (1 << 6)) ? 31 - clz32(res
) : ctz32(res
);
2190 env
->regs
[R_ECX
] = 16 >> (ctrl
& (1 << 0));
2194 void glue(helper_pcmpistrm
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2198 unsigned int res
= pcmpxstrx(env
, d
, s
, ctrl
,
2200 pcmp_ilen(d
, ctrl
));
2202 if ((ctrl
>> 6) & 1) {
2204 for (i
= 0; i
< 8; i
++, res
>>= 1) {
2205 env
->xmm_regs
[0].W(i
) = (res
& 1) ? ~0 : 0;
2208 for (i
= 0; i
< 16; i
++, res
>>= 1) {
2209 env
->xmm_regs
[0].B(i
) = (res
& 1) ? ~0 : 0;
2213 env
->xmm_regs
[0].Q(1) = 0;
2214 env
->xmm_regs
[0].Q(0) = res
;
2218 #define CRCPOLY 0x1edc6f41
2219 #define CRCPOLY_BITREV 0x82f63b78
2220 target_ulong
helper_crc32(uint32_t crc1
, target_ulong msg
, uint32_t len
)
2222 target_ulong crc
= (msg
& ((target_ulong
) -1 >>
2223 (TARGET_LONG_BITS
- len
))) ^ crc1
;
2226 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_BITREV
: 0);
2232 void glue(helper_pclmulqdq
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2235 uint64_t ah
, al
, b
, resh
, resl
;
2238 al
= d
->Q((ctrl
& 1) != 0);
2239 b
= s
->Q((ctrl
& 16) != 0);
2247 ah
= (ah
<< 1) | (al
>> 63);
2256 void glue(helper_aesdec
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2262 for (i
= 0 ; i
< 4 ; i
++) {
2263 d
->L(i
) = rk
.L(i
) ^ bswap32(AES_Td0
[st
.B(AES_ishifts
[4*i
+0])] ^
2264 AES_Td1
[st
.B(AES_ishifts
[4*i
+1])] ^
2265 AES_Td2
[st
.B(AES_ishifts
[4*i
+2])] ^
2266 AES_Td3
[st
.B(AES_ishifts
[4*i
+3])]);
2270 void glue(helper_aesdeclast
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2276 for (i
= 0; i
< 16; i
++) {
2277 d
->B(i
) = rk
.B(i
) ^ (AES_isbox
[st
.B(AES_ishifts
[i
])]);
2281 void glue(helper_aesenc
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2287 for (i
= 0 ; i
< 4 ; i
++) {
2288 d
->L(i
) = rk
.L(i
) ^ bswap32(AES_Te0
[st
.B(AES_shifts
[4*i
+0])] ^
2289 AES_Te1
[st
.B(AES_shifts
[4*i
+1])] ^
2290 AES_Te2
[st
.B(AES_shifts
[4*i
+2])] ^
2291 AES_Te3
[st
.B(AES_shifts
[4*i
+3])]);
2295 void glue(helper_aesenclast
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2301 for (i
= 0; i
< 16; i
++) {
2302 d
->B(i
) = rk
.B(i
) ^ (AES_sbox
[st
.B(AES_shifts
[i
])]);
2307 void glue(helper_aesimc
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
)
2312 for (i
= 0 ; i
< 4 ; i
++) {
2313 d
->L(i
) = bswap32(AES_imc
[tmp
.B(4*i
+0)][0] ^
2314 AES_imc
[tmp
.B(4*i
+1)][1] ^
2315 AES_imc
[tmp
.B(4*i
+2)][2] ^
2316 AES_imc
[tmp
.B(4*i
+3)][3]);
2320 void glue(helper_aeskeygenassist
, SUFFIX
)(CPUX86State
*env
, Reg
*d
, Reg
*s
,
2326 for (i
= 0 ; i
< 4 ; i
++) {
2327 d
->B(i
) = AES_sbox
[tmp
.B(i
+ 4)];
2328 d
->B(i
+ 8) = AES_sbox
[tmp
.B(i
+ 12)];
2330 d
->L(1) = (d
->L(0) << 24 | d
->L(0) >> 8) ^ ctrl
;
2331 d
->L(3) = (d
->L(2) << 24 | d
->L(2) >> 8) ^ ctrl
;