2 * x86 FPU, MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI helpers
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/host-utils.h"
26 #if !defined(CONFIG_USER_ONLY)
27 #include "exec/softmmu_exec.h"
28 #endif /* !defined(CONFIG_USER_ONLY) */
30 #define FPU_RC_MASK 0xc00
31 #define FPU_RC_NEAR 0x000
32 #define FPU_RC_DOWN 0x400
33 #define FPU_RC_UP 0x800
34 #define FPU_RC_CHOP 0xc00
36 #define MAXTAN 9223372036854775808.0
38 /* the following deal with x86 long double-precision numbers */
39 #define MAXEXPD 0x7fff
41 #define EXPD(fp) (fp.l.upper & 0x7fff)
42 #define SIGND(fp) ((fp.l.upper) & 0x8000)
43 #define MANTD(fp) (fp.l.lower)
44 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
46 #define FPUS_IE (1 << 0)
47 #define FPUS_DE (1 << 1)
48 #define FPUS_ZE (1 << 2)
49 #define FPUS_OE (1 << 3)
50 #define FPUS_UE (1 << 4)
51 #define FPUS_PE (1 << 5)
52 #define FPUS_SF (1 << 6)
53 #define FPUS_SE (1 << 7)
54 #define FPUS_B (1 << 15)
58 #define floatx80_lg2 make_floatx80(0x3ffd, 0x9a209a84fbcff799LL)
59 #define floatx80_l2e make_floatx80(0x3fff, 0xb8aa3b295c17f0bcLL)
60 #define floatx80_l2t make_floatx80(0x4000, 0xd49a784bcd1b8afeLL)
62 static inline void fpush(CPUX86State
*env
)
64 env
->fpstt
= (env
->fpstt
- 1) & 7;
65 env
->fptags
[env
->fpstt
] = 0; /* validate stack entry */
68 static inline void fpop(CPUX86State
*env
)
70 env
->fptags
[env
->fpstt
] = 1; /* invalidate stack entry */
71 env
->fpstt
= (env
->fpstt
+ 1) & 7;
74 static inline floatx80
helper_fldt(CPUX86State
*env
, target_ulong ptr
)
78 temp
.l
.lower
= cpu_ldq_data(env
, ptr
);
79 temp
.l
.upper
= cpu_lduw_data(env
, ptr
+ 8);
83 static inline void helper_fstt(CPUX86State
*env
, floatx80 f
, target_ulong ptr
)
88 cpu_stq_data(env
, ptr
, temp
.l
.lower
);
89 cpu_stw_data(env
, ptr
+ 8, temp
.l
.upper
);
94 static inline double floatx80_to_double(CPUX86State
*env
, floatx80 a
)
101 u
.f64
= floatx80_to_float64(a
, &env
->fp_status
);
105 static inline floatx80
double_to_floatx80(CPUX86State
*env
, double a
)
113 return float64_to_floatx80(u
.f64
, &env
->fp_status
);
116 static void fpu_set_exception(CPUX86State
*env
, int mask
)
119 if (env
->fpus
& (~env
->fpuc
& FPUC_EM
)) {
120 env
->fpus
|= FPUS_SE
| FPUS_B
;
124 static inline floatx80
helper_fdiv(CPUX86State
*env
, floatx80 a
, floatx80 b
)
126 if (floatx80_is_zero(b
)) {
127 fpu_set_exception(env
, FPUS_ZE
);
129 return floatx80_div(a
, b
, &env
->fp_status
);
132 static void fpu_raise_exception(CPUX86State
*env
)
134 if (env
->cr
[0] & CR0_NE_MASK
) {
135 raise_exception(env
, EXCP10_COPR
);
137 #if !defined(CONFIG_USER_ONLY)
144 void helper_flds_FT0(CPUX86State
*env
, uint32_t val
)
152 FT0
= float32_to_floatx80(u
.f
, &env
->fp_status
);
155 void helper_fldl_FT0(CPUX86State
*env
, uint64_t val
)
163 FT0
= float64_to_floatx80(u
.f
, &env
->fp_status
);
166 void helper_fildl_FT0(CPUX86State
*env
, int32_t val
)
168 FT0
= int32_to_floatx80(val
, &env
->fp_status
);
171 void helper_flds_ST0(CPUX86State
*env
, uint32_t val
)
179 new_fpstt
= (env
->fpstt
- 1) & 7;
181 env
->fpregs
[new_fpstt
].d
= float32_to_floatx80(u
.f
, &env
->fp_status
);
182 env
->fpstt
= new_fpstt
;
183 env
->fptags
[new_fpstt
] = 0; /* validate stack entry */
186 void helper_fldl_ST0(CPUX86State
*env
, uint64_t val
)
194 new_fpstt
= (env
->fpstt
- 1) & 7;
196 env
->fpregs
[new_fpstt
].d
= float64_to_floatx80(u
.f
, &env
->fp_status
);
197 env
->fpstt
= new_fpstt
;
198 env
->fptags
[new_fpstt
] = 0; /* validate stack entry */
201 void helper_fildl_ST0(CPUX86State
*env
, int32_t val
)
205 new_fpstt
= (env
->fpstt
- 1) & 7;
206 env
->fpregs
[new_fpstt
].d
= int32_to_floatx80(val
, &env
->fp_status
);
207 env
->fpstt
= new_fpstt
;
208 env
->fptags
[new_fpstt
] = 0; /* validate stack entry */
211 void helper_fildll_ST0(CPUX86State
*env
, int64_t val
)
215 new_fpstt
= (env
->fpstt
- 1) & 7;
216 env
->fpregs
[new_fpstt
].d
= int64_to_floatx80(val
, &env
->fp_status
);
217 env
->fpstt
= new_fpstt
;
218 env
->fptags
[new_fpstt
] = 0; /* validate stack entry */
221 uint32_t helper_fsts_ST0(CPUX86State
*env
)
228 u
.f
= floatx80_to_float32(ST0
, &env
->fp_status
);
232 uint64_t helper_fstl_ST0(CPUX86State
*env
)
239 u
.f
= floatx80_to_float64(ST0
, &env
->fp_status
);
243 int32_t helper_fist_ST0(CPUX86State
*env
)
247 val
= floatx80_to_int32(ST0
, &env
->fp_status
);
248 if (val
!= (int16_t)val
) {
254 int32_t helper_fistl_ST0(CPUX86State
*env
)
258 val
= floatx80_to_int32(ST0
, &env
->fp_status
);
262 int64_t helper_fistll_ST0(CPUX86State
*env
)
266 val
= floatx80_to_int64(ST0
, &env
->fp_status
);
270 int32_t helper_fistt_ST0(CPUX86State
*env
)
274 val
= floatx80_to_int32_round_to_zero(ST0
, &env
->fp_status
);
275 if (val
!= (int16_t)val
) {
281 int32_t helper_fisttl_ST0(CPUX86State
*env
)
285 val
= floatx80_to_int32_round_to_zero(ST0
, &env
->fp_status
);
289 int64_t helper_fisttll_ST0(CPUX86State
*env
)
293 val
= floatx80_to_int64_round_to_zero(ST0
, &env
->fp_status
);
297 void helper_fldt_ST0(CPUX86State
*env
, target_ulong ptr
)
301 new_fpstt
= (env
->fpstt
- 1) & 7;
302 env
->fpregs
[new_fpstt
].d
= helper_fldt(env
, ptr
);
303 env
->fpstt
= new_fpstt
;
304 env
->fptags
[new_fpstt
] = 0; /* validate stack entry */
307 void helper_fstt_ST0(CPUX86State
*env
, target_ulong ptr
)
309 helper_fstt(env
, ST0
, ptr
);
312 void helper_fpush(CPUX86State
*env
)
317 void helper_fpop(CPUX86State
*env
)
322 void helper_fdecstp(CPUX86State
*env
)
324 env
->fpstt
= (env
->fpstt
- 1) & 7;
325 env
->fpus
&= ~0x4700;
328 void helper_fincstp(CPUX86State
*env
)
330 env
->fpstt
= (env
->fpstt
+ 1) & 7;
331 env
->fpus
&= ~0x4700;
336 void helper_ffree_STN(CPUX86State
*env
, int st_index
)
338 env
->fptags
[(env
->fpstt
+ st_index
) & 7] = 1;
341 void helper_fmov_ST0_FT0(CPUX86State
*env
)
346 void helper_fmov_FT0_STN(CPUX86State
*env
, int st_index
)
351 void helper_fmov_ST0_STN(CPUX86State
*env
, int st_index
)
356 void helper_fmov_STN_ST0(CPUX86State
*env
, int st_index
)
361 void helper_fxchg_ST0_STN(CPUX86State
*env
, int st_index
)
372 static const int fcom_ccval
[4] = {0x0100, 0x4000, 0x0000, 0x4500};
374 void helper_fcom_ST0_FT0(CPUX86State
*env
)
378 ret
= floatx80_compare(ST0
, FT0
, &env
->fp_status
);
379 env
->fpus
= (env
->fpus
& ~0x4500) | fcom_ccval
[ret
+ 1];
382 void helper_fucom_ST0_FT0(CPUX86State
*env
)
386 ret
= floatx80_compare_quiet(ST0
, FT0
, &env
->fp_status
);
387 env
->fpus
= (env
->fpus
& ~0x4500) | fcom_ccval
[ret
+ 1];
390 static const int fcomi_ccval
[4] = {CC_C
, CC_Z
, 0, CC_Z
| CC_P
| CC_C
};
392 void helper_fcomi_ST0_FT0(CPUX86State
*env
)
397 ret
= floatx80_compare(ST0
, FT0
, &env
->fp_status
);
398 eflags
= cpu_cc_compute_all(env
, CC_OP
);
399 eflags
= (eflags
& ~(CC_Z
| CC_P
| CC_C
)) | fcomi_ccval
[ret
+ 1];
403 void helper_fucomi_ST0_FT0(CPUX86State
*env
)
408 ret
= floatx80_compare_quiet(ST0
, FT0
, &env
->fp_status
);
409 eflags
= cpu_cc_compute_all(env
, CC_OP
);
410 eflags
= (eflags
& ~(CC_Z
| CC_P
| CC_C
)) | fcomi_ccval
[ret
+ 1];
414 void helper_fadd_ST0_FT0(CPUX86State
*env
)
416 ST0
= floatx80_add(ST0
, FT0
, &env
->fp_status
);
419 void helper_fmul_ST0_FT0(CPUX86State
*env
)
421 ST0
= floatx80_mul(ST0
, FT0
, &env
->fp_status
);
424 void helper_fsub_ST0_FT0(CPUX86State
*env
)
426 ST0
= floatx80_sub(ST0
, FT0
, &env
->fp_status
);
429 void helper_fsubr_ST0_FT0(CPUX86State
*env
)
431 ST0
= floatx80_sub(FT0
, ST0
, &env
->fp_status
);
434 void helper_fdiv_ST0_FT0(CPUX86State
*env
)
436 ST0
= helper_fdiv(env
, ST0
, FT0
);
439 void helper_fdivr_ST0_FT0(CPUX86State
*env
)
441 ST0
= helper_fdiv(env
, FT0
, ST0
);
444 /* fp operations between STN and ST0 */
446 void helper_fadd_STN_ST0(CPUX86State
*env
, int st_index
)
448 ST(st_index
) = floatx80_add(ST(st_index
), ST0
, &env
->fp_status
);
451 void helper_fmul_STN_ST0(CPUX86State
*env
, int st_index
)
453 ST(st_index
) = floatx80_mul(ST(st_index
), ST0
, &env
->fp_status
);
456 void helper_fsub_STN_ST0(CPUX86State
*env
, int st_index
)
458 ST(st_index
) = floatx80_sub(ST(st_index
), ST0
, &env
->fp_status
);
461 void helper_fsubr_STN_ST0(CPUX86State
*env
, int st_index
)
463 ST(st_index
) = floatx80_sub(ST0
, ST(st_index
), &env
->fp_status
);
466 void helper_fdiv_STN_ST0(CPUX86State
*env
, int st_index
)
471 *p
= helper_fdiv(env
, *p
, ST0
);
474 void helper_fdivr_STN_ST0(CPUX86State
*env
, int st_index
)
479 *p
= helper_fdiv(env
, ST0
, *p
);
482 /* misc FPU operations */
483 void helper_fchs_ST0(CPUX86State
*env
)
485 ST0
= floatx80_chs(ST0
);
488 void helper_fabs_ST0(CPUX86State
*env
)
490 ST0
= floatx80_abs(ST0
);
493 void helper_fld1_ST0(CPUX86State
*env
)
498 void helper_fldl2t_ST0(CPUX86State
*env
)
503 void helper_fldl2e_ST0(CPUX86State
*env
)
508 void helper_fldpi_ST0(CPUX86State
*env
)
513 void helper_fldlg2_ST0(CPUX86State
*env
)
518 void helper_fldln2_ST0(CPUX86State
*env
)
523 void helper_fldz_ST0(CPUX86State
*env
)
528 void helper_fldz_FT0(CPUX86State
*env
)
533 uint32_t helper_fnstsw(CPUX86State
*env
)
535 return (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
538 uint32_t helper_fnstcw(CPUX86State
*env
)
543 static void update_fp_status(CPUX86State
*env
)
547 /* set rounding mode */
548 switch (env
->fpuc
& FPU_RC_MASK
) {
551 rnd_type
= float_round_nearest_even
;
554 rnd_type
= float_round_down
;
557 rnd_type
= float_round_up
;
560 rnd_type
= float_round_to_zero
;
563 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
564 switch ((env
->fpuc
>> 8) & 3) {
576 set_floatx80_rounding_precision(rnd_type
, &env
->fp_status
);
579 void helper_fldcw(CPUX86State
*env
, uint32_t val
)
582 update_fp_status(env
);
585 void helper_fclex(CPUX86State
*env
)
590 void helper_fwait(CPUX86State
*env
)
592 if (env
->fpus
& FPUS_SE
) {
593 fpu_raise_exception(env
);
597 void helper_fninit(CPUX86State
*env
)
614 void helper_fbld_ST0(CPUX86State
*env
, target_ulong ptr
)
622 for (i
= 8; i
>= 0; i
--) {
623 v
= cpu_ldub_data(env
, ptr
+ i
);
624 val
= (val
* 100) + ((v
>> 4) * 10) + (v
& 0xf);
626 tmp
= int64_to_floatx80(val
, &env
->fp_status
);
627 if (cpu_ldub_data(env
, ptr
+ 9) & 0x80) {
634 void helper_fbst_ST0(CPUX86State
*env
, target_ulong ptr
)
637 target_ulong mem_ref
, mem_end
;
640 val
= floatx80_to_int64(ST0
, &env
->fp_status
);
642 mem_end
= mem_ref
+ 9;
644 cpu_stb_data(env
, mem_end
, 0x80);
647 cpu_stb_data(env
, mem_end
, 0x00);
649 while (mem_ref
< mem_end
) {
655 v
= ((v
/ 10) << 4) | (v
% 10);
656 cpu_stb_data(env
, mem_ref
++, v
);
658 while (mem_ref
< mem_end
) {
659 cpu_stb_data(env
, mem_ref
++, 0);
663 void helper_f2xm1(CPUX86State
*env
)
665 double val
= floatx80_to_double(env
, ST0
);
667 val
= pow(2.0, val
) - 1.0;
668 ST0
= double_to_floatx80(env
, val
);
671 void helper_fyl2x(CPUX86State
*env
)
673 double fptemp
= floatx80_to_double(env
, ST0
);
676 fptemp
= log(fptemp
) / log(2.0); /* log2(ST) */
677 fptemp
*= floatx80_to_double(env
, ST1
);
678 ST1
= double_to_floatx80(env
, fptemp
);
681 env
->fpus
&= ~0x4700;
686 void helper_fptan(CPUX86State
*env
)
688 double fptemp
= floatx80_to_double(env
, ST0
);
690 if ((fptemp
> MAXTAN
) || (fptemp
< -MAXTAN
)) {
693 fptemp
= tan(fptemp
);
694 ST0
= double_to_floatx80(env
, fptemp
);
697 env
->fpus
&= ~0x400; /* C2 <-- 0 */
698 /* the above code is for |arg| < 2**52 only */
702 void helper_fpatan(CPUX86State
*env
)
704 double fptemp
, fpsrcop
;
706 fpsrcop
= floatx80_to_double(env
, ST1
);
707 fptemp
= floatx80_to_double(env
, ST0
);
708 ST1
= double_to_floatx80(env
, atan2(fpsrcop
, fptemp
));
712 void helper_fxtract(CPUX86State
*env
)
718 if (floatx80_is_zero(ST0
)) {
719 /* Easy way to generate -inf and raising division by 0 exception */
720 ST0
= floatx80_div(floatx80_chs(floatx80_one
), floatx80_zero
,
727 expdif
= EXPD(temp
) - EXPBIAS
;
728 /* DP exponent bias */
729 ST0
= int32_to_floatx80(expdif
, &env
->fp_status
);
736 void helper_fprem1(CPUX86State
*env
)
738 double st0
, st1
, dblq
, fpsrcop
, fptemp
;
739 CPU_LDoubleU fpsrcop1
, fptemp1
;
741 signed long long int q
;
743 st0
= floatx80_to_double(env
, ST0
);
744 st1
= floatx80_to_double(env
, ST1
);
746 if (isinf(st0
) || isnan(st0
) || isnan(st1
) || (st1
== 0.0)) {
747 ST0
= double_to_floatx80(env
, 0.0 / 0.0); /* NaN */
748 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
756 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
759 /* optimisation? taken from the AMD docs */
760 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
761 /* ST0 is unchanged */
766 dblq
= fpsrcop
/ fptemp
;
767 /* round dblq towards nearest integer */
769 st0
= fpsrcop
- fptemp
* dblq
;
771 /* convert dblq to q by truncating towards zero */
773 q
= (signed long long int)(-dblq
);
775 q
= (signed long long int)dblq
;
778 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
779 /* (C0,C3,C1) <-- (q2,q1,q0) */
780 env
->fpus
|= (q
& 0x4) << (8 - 2); /* (C0) <-- q2 */
781 env
->fpus
|= (q
& 0x2) << (14 - 1); /* (C3) <-- q1 */
782 env
->fpus
|= (q
& 0x1) << (9 - 0); /* (C1) <-- q0 */
784 env
->fpus
|= 0x400; /* C2 <-- 1 */
785 fptemp
= pow(2.0, expdif
- 50);
786 fpsrcop
= (st0
/ st1
) / fptemp
;
787 /* fpsrcop = integer obtained by chopping */
788 fpsrcop
= (fpsrcop
< 0.0) ?
789 -(floor(fabs(fpsrcop
))) : floor(fpsrcop
);
790 st0
-= (st1
* fpsrcop
* fptemp
);
792 ST0
= double_to_floatx80(env
, st0
);
795 void helper_fprem(CPUX86State
*env
)
797 double st0
, st1
, dblq
, fpsrcop
, fptemp
;
798 CPU_LDoubleU fpsrcop1
, fptemp1
;
800 signed long long int q
;
802 st0
= floatx80_to_double(env
, ST0
);
803 st1
= floatx80_to_double(env
, ST1
);
805 if (isinf(st0
) || isnan(st0
) || isnan(st1
) || (st1
== 0.0)) {
806 ST0
= double_to_floatx80(env
, 0.0 / 0.0); /* NaN */
807 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
815 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
818 /* optimisation? taken from the AMD docs */
819 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
820 /* ST0 is unchanged */
825 dblq
= fpsrcop
/ fptemp
; /* ST0 / ST1 */
826 /* round dblq towards zero */
827 dblq
= (dblq
< 0.0) ? ceil(dblq
) : floor(dblq
);
828 st0
= fpsrcop
- fptemp
* dblq
; /* fpsrcop is ST0 */
830 /* convert dblq to q by truncating towards zero */
832 q
= (signed long long int)(-dblq
);
834 q
= (signed long long int)dblq
;
837 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
838 /* (C0,C3,C1) <-- (q2,q1,q0) */
839 env
->fpus
|= (q
& 0x4) << (8 - 2); /* (C0) <-- q2 */
840 env
->fpus
|= (q
& 0x2) << (14 - 1); /* (C3) <-- q1 */
841 env
->fpus
|= (q
& 0x1) << (9 - 0); /* (C1) <-- q0 */
843 int N
= 32 + (expdif
% 32); /* as per AMD docs */
845 env
->fpus
|= 0x400; /* C2 <-- 1 */
846 fptemp
= pow(2.0, (double)(expdif
- N
));
847 fpsrcop
= (st0
/ st1
) / fptemp
;
848 /* fpsrcop = integer obtained by chopping */
849 fpsrcop
= (fpsrcop
< 0.0) ?
850 -(floor(fabs(fpsrcop
))) : floor(fpsrcop
);
851 st0
-= (st1
* fpsrcop
* fptemp
);
853 ST0
= double_to_floatx80(env
, st0
);
856 void helper_fyl2xp1(CPUX86State
*env
)
858 double fptemp
= floatx80_to_double(env
, ST0
);
860 if ((fptemp
+ 1.0) > 0.0) {
861 fptemp
= log(fptemp
+ 1.0) / log(2.0); /* log2(ST + 1.0) */
862 fptemp
*= floatx80_to_double(env
, ST1
);
863 ST1
= double_to_floatx80(env
, fptemp
);
866 env
->fpus
&= ~0x4700;
871 void helper_fsqrt(CPUX86State
*env
)
873 if (floatx80_is_neg(ST0
)) {
874 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
877 ST0
= floatx80_sqrt(ST0
, &env
->fp_status
);
880 void helper_fsincos(CPUX86State
*env
)
882 double fptemp
= floatx80_to_double(env
, ST0
);
884 if ((fptemp
> MAXTAN
) || (fptemp
< -MAXTAN
)) {
887 ST0
= double_to_floatx80(env
, sin(fptemp
));
889 ST0
= double_to_floatx80(env
, cos(fptemp
));
890 env
->fpus
&= ~0x400; /* C2 <-- 0 */
891 /* the above code is for |arg| < 2**63 only */
895 void helper_frndint(CPUX86State
*env
)
897 ST0
= floatx80_round_to_int(ST0
, &env
->fp_status
);
900 void helper_fscale(CPUX86State
*env
)
902 if (floatx80_is_any_nan(ST1
)) {
905 int n
= floatx80_to_int32_round_to_zero(ST1
, &env
->fp_status
);
906 ST0
= floatx80_scalbn(ST0
, n
, &env
->fp_status
);
910 void helper_fsin(CPUX86State
*env
)
912 double fptemp
= floatx80_to_double(env
, ST0
);
914 if ((fptemp
> MAXTAN
) || (fptemp
< -MAXTAN
)) {
917 ST0
= double_to_floatx80(env
, sin(fptemp
));
918 env
->fpus
&= ~0x400; /* C2 <-- 0 */
919 /* the above code is for |arg| < 2**53 only */
923 void helper_fcos(CPUX86State
*env
)
925 double fptemp
= floatx80_to_double(env
, ST0
);
927 if ((fptemp
> MAXTAN
) || (fptemp
< -MAXTAN
)) {
930 ST0
= double_to_floatx80(env
, cos(fptemp
));
931 env
->fpus
&= ~0x400; /* C2 <-- 0 */
932 /* the above code is for |arg| < 2**63 only */
936 void helper_fxam_ST0(CPUX86State
*env
)
943 env
->fpus
&= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
945 env
->fpus
|= 0x200; /* C1 <-- 1 */
948 /* XXX: test fptags too */
950 if (expdif
== MAXEXPD
) {
951 if (MANTD(temp
) == 0x8000000000000000ULL
) {
952 env
->fpus
|= 0x500; /* Infinity */
954 env
->fpus
|= 0x100; /* NaN */
956 } else if (expdif
== 0) {
957 if (MANTD(temp
) == 0) {
958 env
->fpus
|= 0x4000; /* Zero */
960 env
->fpus
|= 0x4400; /* Denormal */
967 void helper_fstenv(CPUX86State
*env
, target_ulong ptr
, int data32
)
969 int fpus
, fptag
, exp
, i
;
973 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
975 for (i
= 7; i
>= 0; i
--) {
977 if (env
->fptags
[i
]) {
980 tmp
.d
= env
->fpregs
[i
].d
;
983 if (exp
== 0 && mant
== 0) {
986 } else if (exp
== 0 || exp
== MAXEXPD
987 || (mant
& (1LL << 63)) == 0) {
988 /* NaNs, infinity, denormal */
995 cpu_stl_data(env
, ptr
, env
->fpuc
);
996 cpu_stl_data(env
, ptr
+ 4, fpus
);
997 cpu_stl_data(env
, ptr
+ 8, fptag
);
998 cpu_stl_data(env
, ptr
+ 12, 0); /* fpip */
999 cpu_stl_data(env
, ptr
+ 16, 0); /* fpcs */
1000 cpu_stl_data(env
, ptr
+ 20, 0); /* fpoo */
1001 cpu_stl_data(env
, ptr
+ 24, 0); /* fpos */
1004 cpu_stw_data(env
, ptr
, env
->fpuc
);
1005 cpu_stw_data(env
, ptr
+ 2, fpus
);
1006 cpu_stw_data(env
, ptr
+ 4, fptag
);
1007 cpu_stw_data(env
, ptr
+ 6, 0);
1008 cpu_stw_data(env
, ptr
+ 8, 0);
1009 cpu_stw_data(env
, ptr
+ 10, 0);
1010 cpu_stw_data(env
, ptr
+ 12, 0);
1014 void helper_fldenv(CPUX86State
*env
, target_ulong ptr
, int data32
)
1019 env
->fpuc
= cpu_lduw_data(env
, ptr
);
1020 fpus
= cpu_lduw_data(env
, ptr
+ 4);
1021 fptag
= cpu_lduw_data(env
, ptr
+ 8);
1023 env
->fpuc
= cpu_lduw_data(env
, ptr
);
1024 fpus
= cpu_lduw_data(env
, ptr
+ 2);
1025 fptag
= cpu_lduw_data(env
, ptr
+ 4);
1027 env
->fpstt
= (fpus
>> 11) & 7;
1028 env
->fpus
= fpus
& ~0x3800;
1029 for (i
= 0; i
< 8; i
++) {
1030 env
->fptags
[i
] = ((fptag
& 3) == 3);
1035 void helper_fsave(CPUX86State
*env
, target_ulong ptr
, int data32
)
1040 helper_fstenv(env
, ptr
, data32
);
1042 ptr
+= (14 << data32
);
1043 for (i
= 0; i
< 8; i
++) {
1045 helper_fstt(env
, tmp
, ptr
);
1063 void helper_frstor(CPUX86State
*env
, target_ulong ptr
, int data32
)
1068 helper_fldenv(env
, ptr
, data32
);
1069 ptr
+= (14 << data32
);
1071 for (i
= 0; i
< 8; i
++) {
1072 tmp
= helper_fldt(env
, ptr
);
1078 #if defined(CONFIG_USER_ONLY)
1079 void cpu_x86_fsave(CPUX86State
*env
, target_ulong ptr
, int data32
)
1081 helper_fsave(env
, ptr
, data32
);
1084 void cpu_x86_frstor(CPUX86State
*env
, target_ulong ptr
, int data32
)
1086 helper_frstor(env
, ptr
, data32
);
1090 void helper_fxsave(CPUX86State
*env
, target_ulong ptr
, int data64
)
1092 int fpus
, fptag
, i
, nb_xmm_regs
;
1096 /* The operand must be 16 byte aligned */
1098 raise_exception(env
, EXCP0D_GPF
);
1101 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1103 for (i
= 0; i
< 8; i
++) {
1104 fptag
|= (env
->fptags
[i
] << i
);
1106 cpu_stw_data(env
, ptr
, env
->fpuc
);
1107 cpu_stw_data(env
, ptr
+ 2, fpus
);
1108 cpu_stw_data(env
, ptr
+ 4, fptag
^ 0xff);
1109 #ifdef TARGET_X86_64
1111 cpu_stq_data(env
, ptr
+ 0x08, 0); /* rip */
1112 cpu_stq_data(env
, ptr
+ 0x10, 0); /* rdp */
1116 cpu_stl_data(env
, ptr
+ 0x08, 0); /* eip */
1117 cpu_stl_data(env
, ptr
+ 0x0c, 0); /* sel */
1118 cpu_stl_data(env
, ptr
+ 0x10, 0); /* dp */
1119 cpu_stl_data(env
, ptr
+ 0x14, 0); /* sel */
1123 for (i
= 0; i
< 8; i
++) {
1125 helper_fstt(env
, tmp
, addr
);
1129 if (env
->cr
[4] & CR4_OSFXSR_MASK
) {
1130 /* XXX: finish it */
1131 cpu_stl_data(env
, ptr
+ 0x18, env
->mxcsr
); /* mxcsr */
1132 cpu_stl_data(env
, ptr
+ 0x1c, 0x0000ffff); /* mxcsr_mask */
1133 if (env
->hflags
& HF_CS64_MASK
) {
1139 /* Fast FXSAVE leaves out the XMM registers */
1140 if (!(env
->efer
& MSR_EFER_FFXSR
)
1141 || (env
->hflags
& HF_CPL_MASK
)
1142 || !(env
->hflags
& HF_LMA_MASK
)) {
1143 for (i
= 0; i
< nb_xmm_regs
; i
++) {
1144 cpu_stq_data(env
, addr
, env
->xmm_regs
[i
].XMM_Q(0));
1145 cpu_stq_data(env
, addr
+ 8, env
->xmm_regs
[i
].XMM_Q(1));
1152 void helper_fxrstor(CPUX86State
*env
, target_ulong ptr
, int data64
)
1154 int i
, fpus
, fptag
, nb_xmm_regs
;
1158 /* The operand must be 16 byte aligned */
1160 raise_exception(env
, EXCP0D_GPF
);
1163 env
->fpuc
= cpu_lduw_data(env
, ptr
);
1164 fpus
= cpu_lduw_data(env
, ptr
+ 2);
1165 fptag
= cpu_lduw_data(env
, ptr
+ 4);
1166 env
->fpstt
= (fpus
>> 11) & 7;
1167 env
->fpus
= fpus
& ~0x3800;
1169 for (i
= 0; i
< 8; i
++) {
1170 env
->fptags
[i
] = ((fptag
>> i
) & 1);
1174 for (i
= 0; i
< 8; i
++) {
1175 tmp
= helper_fldt(env
, addr
);
1180 if (env
->cr
[4] & CR4_OSFXSR_MASK
) {
1181 /* XXX: finish it */
1182 cpu_set_mxcsr(env
, cpu_ldl_data(env
, ptr
+ 0x18));
1183 /* cpu_ldl_data(env, ptr + 0x1c); */
1184 if (env
->hflags
& HF_CS64_MASK
) {
1190 /* Fast FXRESTORE leaves out the XMM registers */
1191 if (!(env
->efer
& MSR_EFER_FFXSR
)
1192 || (env
->hflags
& HF_CPL_MASK
)
1193 || !(env
->hflags
& HF_LMA_MASK
)) {
1194 for (i
= 0; i
< nb_xmm_regs
; i
++) {
1195 env
->xmm_regs
[i
].XMM_Q(0) = cpu_ldq_data(env
, addr
);
1196 env
->xmm_regs
[i
].XMM_Q(1) = cpu_ldq_data(env
, addr
+ 8);
1203 void cpu_get_fp80(uint64_t *pmant
, uint16_t *pexp
, floatx80 f
)
1208 *pmant
= temp
.l
.lower
;
1209 *pexp
= temp
.l
.upper
;
1212 floatx80
cpu_set_fp80(uint64_t mant
, uint16_t upper
)
1216 temp
.l
.upper
= upper
;
1217 temp
.l
.lower
= mant
;
1222 /* XXX: optimize by storing fptt and fptags in the static cpu state */
1224 #define SSE_DAZ 0x0040
1225 #define SSE_RC_MASK 0x6000
1226 #define SSE_RC_NEAR 0x0000
1227 #define SSE_RC_DOWN 0x2000
1228 #define SSE_RC_UP 0x4000
1229 #define SSE_RC_CHOP 0x6000
1230 #define SSE_FZ 0x8000
1232 void cpu_set_mxcsr(CPUX86State
*env
, uint32_t mxcsr
)
1238 /* set rounding mode */
1239 switch (mxcsr
& SSE_RC_MASK
) {
1242 rnd_type
= float_round_nearest_even
;
1245 rnd_type
= float_round_down
;
1248 rnd_type
= float_round_up
;
1251 rnd_type
= float_round_to_zero
;
1254 set_float_rounding_mode(rnd_type
, &env
->sse_status
);
1256 /* set denormals are zero */
1257 set_flush_inputs_to_zero((mxcsr
& SSE_DAZ
) ? 1 : 0, &env
->sse_status
);
1259 /* set flush to zero */
1260 set_flush_to_zero((mxcsr
& SSE_FZ
) ? 1 : 0, &env
->fp_status
);
1263 void helper_ldmxcsr(CPUX86State
*env
, uint32_t val
)
1265 cpu_set_mxcsr(env
, val
);
1268 void helper_enter_mmx(CPUX86State
*env
)
1271 *(uint32_t *)(env
->fptags
) = 0;
1272 *(uint32_t *)(env
->fptags
+ 4) = 0;
1275 void helper_emms(CPUX86State
*env
)
1277 /* set to empty state */
1278 *(uint32_t *)(env
->fptags
) = 0x01010101;
1279 *(uint32_t *)(env
->fptags
+ 4) = 0x01010101;
1283 void helper_movq(CPUX86State
*env
, void *d
, void *s
)
1285 *(uint64_t *)d
= *(uint64_t *)s
;
1289 #include "ops_sse.h"
1292 #include "ops_sse.h"