audio: Make PC speaker audio card available by default
[qemu-kvm.git] / target-i386 / fpu_helper.c
blob6065c2e72d59a0e198282dbba7cd96f199c47c64
1 /*
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/>.
20 #include <math.h>
21 #include "cpu.h"
22 #include "dyngen-exec.h"
23 #include "helper.h"
25 #if !defined(CONFIG_USER_ONLY)
26 #include "softmmu_exec.h"
27 #endif /* !defined(CONFIG_USER_ONLY) */
29 #define FPU_RC_MASK 0xc00
30 #define FPU_RC_NEAR 0x000
31 #define FPU_RC_DOWN 0x400
32 #define FPU_RC_UP 0x800
33 #define FPU_RC_CHOP 0xc00
35 #define MAXTAN 9223372036854775808.0
37 /* the following deal with x86 long double-precision numbers */
38 #define MAXEXPD 0x7fff
39 #define EXPBIAS 16383
40 #define EXPD(fp) (fp.l.upper & 0x7fff)
41 #define SIGND(fp) ((fp.l.upper) & 0x8000)
42 #define MANTD(fp) (fp.l.lower)
43 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
45 #define FPUS_IE (1 << 0)
46 #define FPUS_DE (1 << 1)
47 #define FPUS_ZE (1 << 2)
48 #define FPUS_OE (1 << 3)
49 #define FPUS_UE (1 << 4)
50 #define FPUS_PE (1 << 5)
51 #define FPUS_SF (1 << 6)
52 #define FPUS_SE (1 << 7)
53 #define FPUS_B (1 << 15)
55 #define FPUC_EM 0x3f
57 #define floatx80_lg2 make_floatx80(0x3ffd, 0x9a209a84fbcff799LL)
58 #define floatx80_l2e make_floatx80(0x3fff, 0xb8aa3b295c17f0bcLL)
59 #define floatx80_l2t make_floatx80(0x4000, 0xd49a784bcd1b8afeLL)
61 static inline void fpush(void)
63 env->fpstt = (env->fpstt - 1) & 7;
64 env->fptags[env->fpstt] = 0; /* validate stack entry */
67 static inline void fpop(void)
69 env->fptags[env->fpstt] = 1; /* invalidate stack entry */
70 env->fpstt = (env->fpstt + 1) & 7;
73 static inline floatx80 helper_fldt(target_ulong ptr)
75 CPU_LDoubleU temp;
77 temp.l.lower = ldq(ptr);
78 temp.l.upper = lduw(ptr + 8);
79 return temp.d;
82 static inline void helper_fstt(floatx80 f, target_ulong ptr)
84 CPU_LDoubleU temp;
86 temp.d = f;
87 stq(ptr, temp.l.lower);
88 stw(ptr + 8, temp.l.upper);
91 /* x87 FPU helpers */
93 static inline double floatx80_to_double(floatx80 a)
95 union {
96 float64 f64;
97 double d;
98 } u;
100 u.f64 = floatx80_to_float64(a, &env->fp_status);
101 return u.d;
104 static inline floatx80 double_to_floatx80(double a)
106 union {
107 float64 f64;
108 double d;
109 } u;
111 u.d = a;
112 return float64_to_floatx80(u.f64, &env->fp_status);
115 static void fpu_set_exception(int mask)
117 env->fpus |= mask;
118 if (env->fpus & (~env->fpuc & FPUC_EM)) {
119 env->fpus |= FPUS_SE | FPUS_B;
123 static inline floatx80 helper_fdiv(floatx80 a, floatx80 b)
125 if (floatx80_is_zero(b)) {
126 fpu_set_exception(FPUS_ZE);
128 return floatx80_div(a, b, &env->fp_status);
131 static void fpu_raise_exception(void)
133 if (env->cr[0] & CR0_NE_MASK) {
134 raise_exception(env, EXCP10_COPR);
136 #if !defined(CONFIG_USER_ONLY)
137 else {
138 cpu_set_ferr(env);
140 #endif
143 void helper_flds_FT0(uint32_t val)
145 union {
146 float32 f;
147 uint32_t i;
148 } u;
150 u.i = val;
151 FT0 = float32_to_floatx80(u.f, &env->fp_status);
154 void helper_fldl_FT0(uint64_t val)
156 union {
157 float64 f;
158 uint64_t i;
159 } u;
161 u.i = val;
162 FT0 = float64_to_floatx80(u.f, &env->fp_status);
165 void helper_fildl_FT0(int32_t val)
167 FT0 = int32_to_floatx80(val, &env->fp_status);
170 void helper_flds_ST0(uint32_t val)
172 int new_fpstt;
173 union {
174 float32 f;
175 uint32_t i;
176 } u;
178 new_fpstt = (env->fpstt - 1) & 7;
179 u.i = val;
180 env->fpregs[new_fpstt].d = float32_to_floatx80(u.f, &env->fp_status);
181 env->fpstt = new_fpstt;
182 env->fptags[new_fpstt] = 0; /* validate stack entry */
185 void helper_fldl_ST0(uint64_t val)
187 int new_fpstt;
188 union {
189 float64 f;
190 uint64_t i;
191 } u;
193 new_fpstt = (env->fpstt - 1) & 7;
194 u.i = val;
195 env->fpregs[new_fpstt].d = float64_to_floatx80(u.f, &env->fp_status);
196 env->fpstt = new_fpstt;
197 env->fptags[new_fpstt] = 0; /* validate stack entry */
200 void helper_fildl_ST0(int32_t val)
202 int new_fpstt;
204 new_fpstt = (env->fpstt - 1) & 7;
205 env->fpregs[new_fpstt].d = int32_to_floatx80(val, &env->fp_status);
206 env->fpstt = new_fpstt;
207 env->fptags[new_fpstt] = 0; /* validate stack entry */
210 void helper_fildll_ST0(int64_t val)
212 int new_fpstt;
214 new_fpstt = (env->fpstt - 1) & 7;
215 env->fpregs[new_fpstt].d = int64_to_floatx80(val, &env->fp_status);
216 env->fpstt = new_fpstt;
217 env->fptags[new_fpstt] = 0; /* validate stack entry */
220 uint32_t helper_fsts_ST0(void)
222 union {
223 float32 f;
224 uint32_t i;
225 } u;
227 u.f = floatx80_to_float32(ST0, &env->fp_status);
228 return u.i;
231 uint64_t helper_fstl_ST0(void)
233 union {
234 float64 f;
235 uint64_t i;
236 } u;
238 u.f = floatx80_to_float64(ST0, &env->fp_status);
239 return u.i;
242 int32_t helper_fist_ST0(void)
244 int32_t val;
246 val = floatx80_to_int32(ST0, &env->fp_status);
247 if (val != (int16_t)val) {
248 val = -32768;
250 return val;
253 int32_t helper_fistl_ST0(void)
255 int32_t val;
257 val = floatx80_to_int32(ST0, &env->fp_status);
258 return val;
261 int64_t helper_fistll_ST0(void)
263 int64_t val;
265 val = floatx80_to_int64(ST0, &env->fp_status);
266 return val;
269 int32_t helper_fistt_ST0(void)
271 int32_t val;
273 val = floatx80_to_int32_round_to_zero(ST0, &env->fp_status);
274 if (val != (int16_t)val) {
275 val = -32768;
277 return val;
280 int32_t helper_fisttl_ST0(void)
282 int32_t val;
284 val = floatx80_to_int32_round_to_zero(ST0, &env->fp_status);
285 return val;
288 int64_t helper_fisttll_ST0(void)
290 int64_t val;
292 val = floatx80_to_int64_round_to_zero(ST0, &env->fp_status);
293 return val;
296 void helper_fldt_ST0(target_ulong ptr)
298 int new_fpstt;
300 new_fpstt = (env->fpstt - 1) & 7;
301 env->fpregs[new_fpstt].d = helper_fldt(ptr);
302 env->fpstt = new_fpstt;
303 env->fptags[new_fpstt] = 0; /* validate stack entry */
306 void helper_fstt_ST0(target_ulong ptr)
308 helper_fstt(ST0, ptr);
311 void helper_fpush(void)
313 fpush();
316 void helper_fpop(void)
318 fpop();
321 void helper_fdecstp(void)
323 env->fpstt = (env->fpstt - 1) & 7;
324 env->fpus &= ~0x4700;
327 void helper_fincstp(void)
329 env->fpstt = (env->fpstt + 1) & 7;
330 env->fpus &= ~0x4700;
333 /* FPU move */
335 void helper_ffree_STN(int st_index)
337 env->fptags[(env->fpstt + st_index) & 7] = 1;
340 void helper_fmov_ST0_FT0(void)
342 ST0 = FT0;
345 void helper_fmov_FT0_STN(int st_index)
347 FT0 = ST(st_index);
350 void helper_fmov_ST0_STN(int st_index)
352 ST0 = ST(st_index);
355 void helper_fmov_STN_ST0(int st_index)
357 ST(st_index) = ST0;
360 void helper_fxchg_ST0_STN(int st_index)
362 floatx80 tmp;
364 tmp = ST(st_index);
365 ST(st_index) = ST0;
366 ST0 = tmp;
369 /* FPU operations */
371 static const int fcom_ccval[4] = {0x0100, 0x4000, 0x0000, 0x4500};
373 void helper_fcom_ST0_FT0(void)
375 int ret;
377 ret = floatx80_compare(ST0, FT0, &env->fp_status);
378 env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
381 void helper_fucom_ST0_FT0(void)
383 int ret;
385 ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
386 env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
389 static const int fcomi_ccval[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
391 void helper_fcomi_ST0_FT0(void)
393 int eflags;
394 int ret;
396 ret = floatx80_compare(ST0, FT0, &env->fp_status);
397 eflags = helper_cc_compute_all(CC_OP);
398 eflags = (eflags & ~(CC_Z | CC_P | CC_C)) | fcomi_ccval[ret + 1];
399 CC_SRC = eflags;
402 void helper_fucomi_ST0_FT0(void)
404 int eflags;
405 int ret;
407 ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
408 eflags = helper_cc_compute_all(CC_OP);
409 eflags = (eflags & ~(CC_Z | CC_P | CC_C)) | fcomi_ccval[ret + 1];
410 CC_SRC = eflags;
413 void helper_fadd_ST0_FT0(void)
415 ST0 = floatx80_add(ST0, FT0, &env->fp_status);
418 void helper_fmul_ST0_FT0(void)
420 ST0 = floatx80_mul(ST0, FT0, &env->fp_status);
423 void helper_fsub_ST0_FT0(void)
425 ST0 = floatx80_sub(ST0, FT0, &env->fp_status);
428 void helper_fsubr_ST0_FT0(void)
430 ST0 = floatx80_sub(FT0, ST0, &env->fp_status);
433 void helper_fdiv_ST0_FT0(void)
435 ST0 = helper_fdiv(ST0, FT0);
438 void helper_fdivr_ST0_FT0(void)
440 ST0 = helper_fdiv(FT0, ST0);
443 /* fp operations between STN and ST0 */
445 void helper_fadd_STN_ST0(int st_index)
447 ST(st_index) = floatx80_add(ST(st_index), ST0, &env->fp_status);
450 void helper_fmul_STN_ST0(int st_index)
452 ST(st_index) = floatx80_mul(ST(st_index), ST0, &env->fp_status);
455 void helper_fsub_STN_ST0(int st_index)
457 ST(st_index) = floatx80_sub(ST(st_index), ST0, &env->fp_status);
460 void helper_fsubr_STN_ST0(int st_index)
462 ST(st_index) = floatx80_sub(ST0, ST(st_index), &env->fp_status);
465 void helper_fdiv_STN_ST0(int st_index)
467 floatx80 *p;
469 p = &ST(st_index);
470 *p = helper_fdiv(*p, ST0);
473 void helper_fdivr_STN_ST0(int st_index)
475 floatx80 *p;
477 p = &ST(st_index);
478 *p = helper_fdiv(ST0, *p);
481 /* misc FPU operations */
482 void helper_fchs_ST0(void)
484 ST0 = floatx80_chs(ST0);
487 void helper_fabs_ST0(void)
489 ST0 = floatx80_abs(ST0);
492 void helper_fld1_ST0(void)
494 ST0 = floatx80_one;
497 void helper_fldl2t_ST0(void)
499 ST0 = floatx80_l2t;
502 void helper_fldl2e_ST0(void)
504 ST0 = floatx80_l2e;
507 void helper_fldpi_ST0(void)
509 ST0 = floatx80_pi;
512 void helper_fldlg2_ST0(void)
514 ST0 = floatx80_lg2;
517 void helper_fldln2_ST0(void)
519 ST0 = floatx80_ln2;
522 void helper_fldz_ST0(void)
524 ST0 = floatx80_zero;
527 void helper_fldz_FT0(void)
529 FT0 = floatx80_zero;
532 uint32_t helper_fnstsw(void)
534 return (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
537 uint32_t helper_fnstcw(void)
539 return env->fpuc;
542 static void update_fp_status(void)
544 int rnd_type;
546 /* set rounding mode */
547 switch (env->fpuc & FPU_RC_MASK) {
548 default:
549 case FPU_RC_NEAR:
550 rnd_type = float_round_nearest_even;
551 break;
552 case FPU_RC_DOWN:
553 rnd_type = float_round_down;
554 break;
555 case FPU_RC_UP:
556 rnd_type = float_round_up;
557 break;
558 case FPU_RC_CHOP:
559 rnd_type = float_round_to_zero;
560 break;
562 set_float_rounding_mode(rnd_type, &env->fp_status);
563 switch ((env->fpuc >> 8) & 3) {
564 case 0:
565 rnd_type = 32;
566 break;
567 case 2:
568 rnd_type = 64;
569 break;
570 case 3:
571 default:
572 rnd_type = 80;
573 break;
575 set_floatx80_rounding_precision(rnd_type, &env->fp_status);
578 void helper_fldcw(uint32_t val)
580 env->fpuc = val;
581 update_fp_status();
584 void helper_fclex(void)
586 env->fpus &= 0x7f00;
589 void helper_fwait(void)
591 if (env->fpus & FPUS_SE) {
592 fpu_raise_exception();
596 void helper_fninit(void)
598 env->fpus = 0;
599 env->fpstt = 0;
600 env->fpuc = 0x37f;
601 env->fptags[0] = 1;
602 env->fptags[1] = 1;
603 env->fptags[2] = 1;
604 env->fptags[3] = 1;
605 env->fptags[4] = 1;
606 env->fptags[5] = 1;
607 env->fptags[6] = 1;
608 env->fptags[7] = 1;
611 /* BCD ops */
613 void helper_fbld_ST0(target_ulong ptr)
615 floatx80 tmp;
616 uint64_t val;
617 unsigned int v;
618 int i;
620 val = 0;
621 for (i = 8; i >= 0; i--) {
622 v = ldub(ptr + i);
623 val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
625 tmp = int64_to_floatx80(val, &env->fp_status);
626 if (ldub(ptr + 9) & 0x80) {
627 floatx80_chs(tmp);
629 fpush();
630 ST0 = tmp;
633 void helper_fbst_ST0(target_ulong ptr)
635 int v;
636 target_ulong mem_ref, mem_end;
637 int64_t val;
639 val = floatx80_to_int64(ST0, &env->fp_status);
640 mem_ref = ptr;
641 mem_end = mem_ref + 9;
642 if (val < 0) {
643 stb(mem_end, 0x80);
644 val = -val;
645 } else {
646 stb(mem_end, 0x00);
648 while (mem_ref < mem_end) {
649 if (val == 0) {
650 break;
652 v = val % 100;
653 val = val / 100;
654 v = ((v / 10) << 4) | (v % 10);
655 stb(mem_ref++, v);
657 while (mem_ref < mem_end) {
658 stb(mem_ref++, 0);
662 void helper_f2xm1(void)
664 double val = floatx80_to_double(ST0);
666 val = pow(2.0, val) - 1.0;
667 ST0 = double_to_floatx80(val);
670 void helper_fyl2x(void)
672 double fptemp = floatx80_to_double(ST0);
674 if (fptemp > 0.0) {
675 fptemp = log(fptemp) / log(2.0); /* log2(ST) */
676 fptemp *= floatx80_to_double(ST1);
677 ST1 = double_to_floatx80(fptemp);
678 fpop();
679 } else {
680 env->fpus &= ~0x4700;
681 env->fpus |= 0x400;
685 void helper_fptan(void)
687 double fptemp = floatx80_to_double(ST0);
689 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
690 env->fpus |= 0x400;
691 } else {
692 fptemp = tan(fptemp);
693 ST0 = double_to_floatx80(fptemp);
694 fpush();
695 ST0 = floatx80_one;
696 env->fpus &= ~0x400; /* C2 <-- 0 */
697 /* the above code is for |arg| < 2**52 only */
701 void helper_fpatan(void)
703 double fptemp, fpsrcop;
705 fpsrcop = floatx80_to_double(ST1);
706 fptemp = floatx80_to_double(ST0);
707 ST1 = double_to_floatx80(atan2(fpsrcop, fptemp));
708 fpop();
711 void helper_fxtract(void)
713 CPU_LDoubleU temp;
715 temp.d = ST0;
717 if (floatx80_is_zero(ST0)) {
718 /* Easy way to generate -inf and raising division by 0 exception */
719 ST0 = floatx80_div(floatx80_chs(floatx80_one), floatx80_zero,
720 &env->fp_status);
721 fpush();
722 ST0 = temp.d;
723 } else {
724 int expdif;
726 expdif = EXPD(temp) - EXPBIAS;
727 /* DP exponent bias */
728 ST0 = int32_to_floatx80(expdif, &env->fp_status);
729 fpush();
730 BIASEXPONENT(temp);
731 ST0 = temp.d;
735 void helper_fprem1(void)
737 double st0, st1, dblq, fpsrcop, fptemp;
738 CPU_LDoubleU fpsrcop1, fptemp1;
739 int expdif;
740 signed long long int q;
742 st0 = floatx80_to_double(ST0);
743 st1 = floatx80_to_double(ST1);
745 if (isinf(st0) || isnan(st0) || isnan(st1) || (st1 == 0.0)) {
746 ST0 = double_to_floatx80(0.0 / 0.0); /* NaN */
747 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
748 return;
751 fpsrcop = st0;
752 fptemp = st1;
753 fpsrcop1.d = ST0;
754 fptemp1.d = ST1;
755 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
757 if (expdif < 0) {
758 /* optimisation? taken from the AMD docs */
759 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
760 /* ST0 is unchanged */
761 return;
764 if (expdif < 53) {
765 dblq = fpsrcop / fptemp;
766 /* round dblq towards nearest integer */
767 dblq = rint(dblq);
768 st0 = fpsrcop - fptemp * dblq;
770 /* convert dblq to q by truncating towards zero */
771 if (dblq < 0.0) {
772 q = (signed long long int)(-dblq);
773 } else {
774 q = (signed long long int)dblq;
777 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
778 /* (C0,C3,C1) <-- (q2,q1,q0) */
779 env->fpus |= (q & 0x4) << (8 - 2); /* (C0) <-- q2 */
780 env->fpus |= (q & 0x2) << (14 - 1); /* (C3) <-- q1 */
781 env->fpus |= (q & 0x1) << (9 - 0); /* (C1) <-- q0 */
782 } else {
783 env->fpus |= 0x400; /* C2 <-- 1 */
784 fptemp = pow(2.0, expdif - 50);
785 fpsrcop = (st0 / st1) / fptemp;
786 /* fpsrcop = integer obtained by chopping */
787 fpsrcop = (fpsrcop < 0.0) ?
788 -(floor(fabs(fpsrcop))) : floor(fpsrcop);
789 st0 -= (st1 * fpsrcop * fptemp);
791 ST0 = double_to_floatx80(st0);
794 void helper_fprem(void)
796 double st0, st1, dblq, fpsrcop, fptemp;
797 CPU_LDoubleU fpsrcop1, fptemp1;
798 int expdif;
799 signed long long int q;
801 st0 = floatx80_to_double(ST0);
802 st1 = floatx80_to_double(ST1);
804 if (isinf(st0) || isnan(st0) || isnan(st1) || (st1 == 0.0)) {
805 ST0 = double_to_floatx80(0.0 / 0.0); /* NaN */
806 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
807 return;
810 fpsrcop = st0;
811 fptemp = st1;
812 fpsrcop1.d = ST0;
813 fptemp1.d = ST1;
814 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
816 if (expdif < 0) {
817 /* optimisation? taken from the AMD docs */
818 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
819 /* ST0 is unchanged */
820 return;
823 if (expdif < 53) {
824 dblq = fpsrcop / fptemp; /* ST0 / ST1 */
825 /* round dblq towards zero */
826 dblq = (dblq < 0.0) ? ceil(dblq) : floor(dblq);
827 st0 = fpsrcop - fptemp * dblq; /* fpsrcop is ST0 */
829 /* convert dblq to q by truncating towards zero */
830 if (dblq < 0.0) {
831 q = (signed long long int)(-dblq);
832 } else {
833 q = (signed long long int)dblq;
836 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
837 /* (C0,C3,C1) <-- (q2,q1,q0) */
838 env->fpus |= (q & 0x4) << (8 - 2); /* (C0) <-- q2 */
839 env->fpus |= (q & 0x2) << (14 - 1); /* (C3) <-- q1 */
840 env->fpus |= (q & 0x1) << (9 - 0); /* (C1) <-- q0 */
841 } else {
842 int N = 32 + (expdif % 32); /* as per AMD docs */
844 env->fpus |= 0x400; /* C2 <-- 1 */
845 fptemp = pow(2.0, (double)(expdif - N));
846 fpsrcop = (st0 / st1) / fptemp;
847 /* fpsrcop = integer obtained by chopping */
848 fpsrcop = (fpsrcop < 0.0) ?
849 -(floor(fabs(fpsrcop))) : floor(fpsrcop);
850 st0 -= (st1 * fpsrcop * fptemp);
852 ST0 = double_to_floatx80(st0);
855 void helper_fyl2xp1(void)
857 double fptemp = floatx80_to_double(ST0);
859 if ((fptemp + 1.0) > 0.0) {
860 fptemp = log(fptemp + 1.0) / log(2.0); /* log2(ST + 1.0) */
861 fptemp *= floatx80_to_double(ST1);
862 ST1 = double_to_floatx80(fptemp);
863 fpop();
864 } else {
865 env->fpus &= ~0x4700;
866 env->fpus |= 0x400;
870 void helper_fsqrt(void)
872 if (floatx80_is_neg(ST0)) {
873 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
874 env->fpus |= 0x400;
876 ST0 = floatx80_sqrt(ST0, &env->fp_status);
879 void helper_fsincos(void)
881 double fptemp = floatx80_to_double(ST0);
883 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
884 env->fpus |= 0x400;
885 } else {
886 ST0 = double_to_floatx80(sin(fptemp));
887 fpush();
888 ST0 = double_to_floatx80(cos(fptemp));
889 env->fpus &= ~0x400; /* C2 <-- 0 */
890 /* the above code is for |arg| < 2**63 only */
894 void helper_frndint(void)
896 ST0 = floatx80_round_to_int(ST0, &env->fp_status);
899 void helper_fscale(void)
901 if (floatx80_is_any_nan(ST1)) {
902 ST0 = ST1;
903 } else {
904 int n = floatx80_to_int32_round_to_zero(ST1, &env->fp_status);
905 ST0 = floatx80_scalbn(ST0, n, &env->fp_status);
909 void helper_fsin(void)
911 double fptemp = floatx80_to_double(ST0);
913 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
914 env->fpus |= 0x400;
915 } else {
916 ST0 = double_to_floatx80(sin(fptemp));
917 env->fpus &= ~0x400; /* C2 <-- 0 */
918 /* the above code is for |arg| < 2**53 only */
922 void helper_fcos(void)
924 double fptemp = floatx80_to_double(ST0);
926 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
927 env->fpus |= 0x400;
928 } else {
929 ST0 = double_to_floatx80(cos(fptemp));
930 env->fpus &= ~0x400; /* C2 <-- 0 */
931 /* the above code is for |arg| < 2**63 only */
935 void helper_fxam_ST0(void)
937 CPU_LDoubleU temp;
938 int expdif;
940 temp.d = ST0;
942 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
943 if (SIGND(temp)) {
944 env->fpus |= 0x200; /* C1 <-- 1 */
947 /* XXX: test fptags too */
948 expdif = EXPD(temp);
949 if (expdif == MAXEXPD) {
950 if (MANTD(temp) == 0x8000000000000000ULL) {
951 env->fpus |= 0x500; /* Infinity */
952 } else {
953 env->fpus |= 0x100; /* NaN */
955 } else if (expdif == 0) {
956 if (MANTD(temp) == 0) {
957 env->fpus |= 0x4000; /* Zero */
958 } else {
959 env->fpus |= 0x4400; /* Denormal */
961 } else {
962 env->fpus |= 0x400;
966 void helper_fstenv(target_ulong ptr, int data32)
968 int fpus, fptag, exp, i;
969 uint64_t mant;
970 CPU_LDoubleU tmp;
972 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
973 fptag = 0;
974 for (i = 7; i >= 0; i--) {
975 fptag <<= 2;
976 if (env->fptags[i]) {
977 fptag |= 3;
978 } else {
979 tmp.d = env->fpregs[i].d;
980 exp = EXPD(tmp);
981 mant = MANTD(tmp);
982 if (exp == 0 && mant == 0) {
983 /* zero */
984 fptag |= 1;
985 } else if (exp == 0 || exp == MAXEXPD
986 || (mant & (1LL << 63)) == 0) {
987 /* NaNs, infinity, denormal */
988 fptag |= 2;
992 if (data32) {
993 /* 32 bit */
994 stl(ptr, env->fpuc);
995 stl(ptr + 4, fpus);
996 stl(ptr + 8, fptag);
997 stl(ptr + 12, 0); /* fpip */
998 stl(ptr + 16, 0); /* fpcs */
999 stl(ptr + 20, 0); /* fpoo */
1000 stl(ptr + 24, 0); /* fpos */
1001 } else {
1002 /* 16 bit */
1003 stw(ptr, env->fpuc);
1004 stw(ptr + 2, fpus);
1005 stw(ptr + 4, fptag);
1006 stw(ptr + 6, 0);
1007 stw(ptr + 8, 0);
1008 stw(ptr + 10, 0);
1009 stw(ptr + 12, 0);
1013 void helper_fldenv(target_ulong ptr, int data32)
1015 int i, fpus, fptag;
1017 if (data32) {
1018 env->fpuc = lduw(ptr);
1019 fpus = lduw(ptr + 4);
1020 fptag = lduw(ptr + 8);
1021 } else {
1022 env->fpuc = lduw(ptr);
1023 fpus = lduw(ptr + 2);
1024 fptag = lduw(ptr + 4);
1026 env->fpstt = (fpus >> 11) & 7;
1027 env->fpus = fpus & ~0x3800;
1028 for (i = 0; i < 8; i++) {
1029 env->fptags[i] = ((fptag & 3) == 3);
1030 fptag >>= 2;
1034 void helper_fsave(target_ulong ptr, int data32)
1036 floatx80 tmp;
1037 int i;
1039 helper_fstenv(ptr, data32);
1041 ptr += (14 << data32);
1042 for (i = 0; i < 8; i++) {
1043 tmp = ST(i);
1044 helper_fstt(tmp, ptr);
1045 ptr += 10;
1048 /* fninit */
1049 env->fpus = 0;
1050 env->fpstt = 0;
1051 env->fpuc = 0x37f;
1052 env->fptags[0] = 1;
1053 env->fptags[1] = 1;
1054 env->fptags[2] = 1;
1055 env->fptags[3] = 1;
1056 env->fptags[4] = 1;
1057 env->fptags[5] = 1;
1058 env->fptags[6] = 1;
1059 env->fptags[7] = 1;
1062 void helper_frstor(target_ulong ptr, int data32)
1064 floatx80 tmp;
1065 int i;
1067 helper_fldenv(ptr, data32);
1068 ptr += (14 << data32);
1070 for (i = 0; i < 8; i++) {
1071 tmp = helper_fldt(ptr);
1072 ST(i) = tmp;
1073 ptr += 10;
1077 #if defined(CONFIG_USER_ONLY)
1078 void cpu_x86_fsave(CPUX86State *s, target_ulong ptr, int data32)
1080 CPUX86State *saved_env;
1082 saved_env = env;
1083 env = s;
1085 helper_fsave(ptr, data32);
1087 env = saved_env;
1090 void cpu_x86_frstor(CPUX86State *s, target_ulong ptr, int data32)
1092 CPUX86State *saved_env;
1094 saved_env = env;
1095 env = s;
1097 helper_frstor(ptr, data32);
1099 env = saved_env;
1101 #endif
1103 void helper_fxsave(target_ulong ptr, int data64)
1105 int fpus, fptag, i, nb_xmm_regs;
1106 floatx80 tmp;
1107 target_ulong addr;
1109 /* The operand must be 16 byte aligned */
1110 if (ptr & 0xf) {
1111 raise_exception(env, EXCP0D_GPF);
1114 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1115 fptag = 0;
1116 for (i = 0; i < 8; i++) {
1117 fptag |= (env->fptags[i] << i);
1119 stw(ptr, env->fpuc);
1120 stw(ptr + 2, fpus);
1121 stw(ptr + 4, fptag ^ 0xff);
1122 #ifdef TARGET_X86_64
1123 if (data64) {
1124 stq(ptr + 0x08, 0); /* rip */
1125 stq(ptr + 0x10, 0); /* rdp */
1126 } else
1127 #endif
1129 stl(ptr + 0x08, 0); /* eip */
1130 stl(ptr + 0x0c, 0); /* sel */
1131 stl(ptr + 0x10, 0); /* dp */
1132 stl(ptr + 0x14, 0); /* sel */
1135 addr = ptr + 0x20;
1136 for (i = 0; i < 8; i++) {
1137 tmp = ST(i);
1138 helper_fstt(tmp, addr);
1139 addr += 16;
1142 if (env->cr[4] & CR4_OSFXSR_MASK) {
1143 /* XXX: finish it */
1144 stl(ptr + 0x18, env->mxcsr); /* mxcsr */
1145 stl(ptr + 0x1c, 0x0000ffff); /* mxcsr_mask */
1146 if (env->hflags & HF_CS64_MASK) {
1147 nb_xmm_regs = 16;
1148 } else {
1149 nb_xmm_regs = 8;
1151 addr = ptr + 0xa0;
1152 /* Fast FXSAVE leaves out the XMM registers */
1153 if (!(env->efer & MSR_EFER_FFXSR)
1154 || (env->hflags & HF_CPL_MASK)
1155 || !(env->hflags & HF_LMA_MASK)) {
1156 for (i = 0; i < nb_xmm_regs; i++) {
1157 stq(addr, env->xmm_regs[i].XMM_Q(0));
1158 stq(addr + 8, env->xmm_regs[i].XMM_Q(1));
1159 addr += 16;
1165 void helper_fxrstor(target_ulong ptr, int data64)
1167 int i, fpus, fptag, nb_xmm_regs;
1168 floatx80 tmp;
1169 target_ulong addr;
1171 /* The operand must be 16 byte aligned */
1172 if (ptr & 0xf) {
1173 raise_exception(env, EXCP0D_GPF);
1176 env->fpuc = lduw(ptr);
1177 fpus = lduw(ptr + 2);
1178 fptag = lduw(ptr + 4);
1179 env->fpstt = (fpus >> 11) & 7;
1180 env->fpus = fpus & ~0x3800;
1181 fptag ^= 0xff;
1182 for (i = 0; i < 8; i++) {
1183 env->fptags[i] = ((fptag >> i) & 1);
1186 addr = ptr + 0x20;
1187 for (i = 0; i < 8; i++) {
1188 tmp = helper_fldt(addr);
1189 ST(i) = tmp;
1190 addr += 16;
1193 if (env->cr[4] & CR4_OSFXSR_MASK) {
1194 /* XXX: finish it */
1195 env->mxcsr = ldl(ptr + 0x18);
1196 /* ldl(ptr + 0x1c); */
1197 if (env->hflags & HF_CS64_MASK) {
1198 nb_xmm_regs = 16;
1199 } else {
1200 nb_xmm_regs = 8;
1202 addr = ptr + 0xa0;
1203 /* Fast FXRESTORE leaves out the XMM registers */
1204 if (!(env->efer & MSR_EFER_FFXSR)
1205 || (env->hflags & HF_CPL_MASK)
1206 || !(env->hflags & HF_LMA_MASK)) {
1207 for (i = 0; i < nb_xmm_regs; i++) {
1208 env->xmm_regs[i].XMM_Q(0) = ldq(addr);
1209 env->xmm_regs[i].XMM_Q(1) = ldq(addr + 8);
1210 addr += 16;
1216 void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, floatx80 f)
1218 CPU_LDoubleU temp;
1220 temp.d = f;
1221 *pmant = temp.l.lower;
1222 *pexp = temp.l.upper;
1225 floatx80 cpu_set_fp80(uint64_t mant, uint16_t upper)
1227 CPU_LDoubleU temp;
1229 temp.l.upper = upper;
1230 temp.l.lower = mant;
1231 return temp.d;
1234 /* MMX/SSE */
1235 /* XXX: optimize by storing fptt and fptags in the static cpu state */
1237 #define SSE_DAZ 0x0040
1238 #define SSE_RC_MASK 0x6000
1239 #define SSE_RC_NEAR 0x0000
1240 #define SSE_RC_DOWN 0x2000
1241 #define SSE_RC_UP 0x4000
1242 #define SSE_RC_CHOP 0x6000
1243 #define SSE_FZ 0x8000
1245 static void update_sse_status(void)
1247 int rnd_type;
1249 /* set rounding mode */
1250 switch (env->mxcsr & SSE_RC_MASK) {
1251 default:
1252 case SSE_RC_NEAR:
1253 rnd_type = float_round_nearest_even;
1254 break;
1255 case SSE_RC_DOWN:
1256 rnd_type = float_round_down;
1257 break;
1258 case SSE_RC_UP:
1259 rnd_type = float_round_up;
1260 break;
1261 case SSE_RC_CHOP:
1262 rnd_type = float_round_to_zero;
1263 break;
1265 set_float_rounding_mode(rnd_type, &env->sse_status);
1267 /* set denormals are zero */
1268 set_flush_inputs_to_zero((env->mxcsr & SSE_DAZ) ? 1 : 0, &env->sse_status);
1270 /* set flush to zero */
1271 set_flush_to_zero((env->mxcsr & SSE_FZ) ? 1 : 0, &env->fp_status);
1274 void helper_ldmxcsr(uint32_t val)
1276 env->mxcsr = val;
1277 update_sse_status();
1280 void helper_enter_mmx(void)
1282 env->fpstt = 0;
1283 *(uint32_t *)(env->fptags) = 0;
1284 *(uint32_t *)(env->fptags + 4) = 0;
1287 void helper_emms(void)
1289 /* set to empty state */
1290 *(uint32_t *)(env->fptags) = 0x01010101;
1291 *(uint32_t *)(env->fptags + 4) = 0x01010101;
1294 /* XXX: suppress */
1295 void helper_movq(void *d, void *s)
1297 *(uint64_t *)d = *(uint64_t *)s;
1300 #define SHIFT 0
1301 #include "ops_sse.h"
1303 #define SHIFT 1
1304 #include "ops_sse.h"