Migration: Add lots of trace events
[qemu.git] / target-i386 / fpu_helper.c
blob30d34d5aee380809a8824a48cf6dcba12281a9b0
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 "exec/helper-proto.h"
23 #include "qemu/aes.h"
24 #include "qemu/host-utils.h"
25 #include "exec/cpu_ldst.h"
27 #define FPU_RC_MASK 0xc00
28 #define FPU_RC_NEAR 0x000
29 #define FPU_RC_DOWN 0x400
30 #define FPU_RC_UP 0x800
31 #define FPU_RC_CHOP 0xc00
33 #define MAXTAN 9223372036854775808.0
35 /* the following deal with x86 long double-precision numbers */
36 #define MAXEXPD 0x7fff
37 #define EXPBIAS 16383
38 #define EXPD(fp) (fp.l.upper & 0x7fff)
39 #define SIGND(fp) ((fp.l.upper) & 0x8000)
40 #define MANTD(fp) (fp.l.lower)
41 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
43 #define FPUS_IE (1 << 0)
44 #define FPUS_DE (1 << 1)
45 #define FPUS_ZE (1 << 2)
46 #define FPUS_OE (1 << 3)
47 #define FPUS_UE (1 << 4)
48 #define FPUS_PE (1 << 5)
49 #define FPUS_SF (1 << 6)
50 #define FPUS_SE (1 << 7)
51 #define FPUS_B (1 << 15)
53 #define FPUC_EM 0x3f
55 #define floatx80_lg2 make_floatx80(0x3ffd, 0x9a209a84fbcff799LL)
56 #define floatx80_l2e make_floatx80(0x3fff, 0xb8aa3b295c17f0bcLL)
57 #define floatx80_l2t make_floatx80(0x4000, 0xd49a784bcd1b8afeLL)
59 static inline void fpush(CPUX86State *env)
61 env->fpstt = (env->fpstt - 1) & 7;
62 env->fptags[env->fpstt] = 0; /* validate stack entry */
65 static inline void fpop(CPUX86State *env)
67 env->fptags[env->fpstt] = 1; /* invalidate stack entry */
68 env->fpstt = (env->fpstt + 1) & 7;
71 static inline floatx80 helper_fldt(CPUX86State *env, target_ulong ptr)
73 CPU_LDoubleU temp;
75 temp.l.lower = cpu_ldq_data(env, ptr);
76 temp.l.upper = cpu_lduw_data(env, ptr + 8);
77 return temp.d;
80 static inline void helper_fstt(CPUX86State *env, floatx80 f, target_ulong ptr)
82 CPU_LDoubleU temp;
84 temp.d = f;
85 cpu_stq_data(env, ptr, temp.l.lower);
86 cpu_stw_data(env, ptr + 8, temp.l.upper);
89 /* x87 FPU helpers */
91 static inline double floatx80_to_double(CPUX86State *env, floatx80 a)
93 union {
94 float64 f64;
95 double d;
96 } u;
98 u.f64 = floatx80_to_float64(a, &env->fp_status);
99 return u.d;
102 static inline floatx80 double_to_floatx80(CPUX86State *env, double a)
104 union {
105 float64 f64;
106 double d;
107 } u;
109 u.d = a;
110 return float64_to_floatx80(u.f64, &env->fp_status);
113 static void fpu_set_exception(CPUX86State *env, int mask)
115 env->fpus |= mask;
116 if (env->fpus & (~env->fpuc & FPUC_EM)) {
117 env->fpus |= FPUS_SE | FPUS_B;
121 static inline floatx80 helper_fdiv(CPUX86State *env, floatx80 a, floatx80 b)
123 if (floatx80_is_zero(b)) {
124 fpu_set_exception(env, FPUS_ZE);
126 return floatx80_div(a, b, &env->fp_status);
129 static void fpu_raise_exception(CPUX86State *env)
131 if (env->cr[0] & CR0_NE_MASK) {
132 raise_exception(env, EXCP10_COPR);
134 #if !defined(CONFIG_USER_ONLY)
135 else {
136 cpu_set_ferr(env);
138 #endif
141 void helper_flds_FT0(CPUX86State *env, uint32_t val)
143 union {
144 float32 f;
145 uint32_t i;
146 } u;
148 u.i = val;
149 FT0 = float32_to_floatx80(u.f, &env->fp_status);
152 void helper_fldl_FT0(CPUX86State *env, uint64_t val)
154 union {
155 float64 f;
156 uint64_t i;
157 } u;
159 u.i = val;
160 FT0 = float64_to_floatx80(u.f, &env->fp_status);
163 void helper_fildl_FT0(CPUX86State *env, int32_t val)
165 FT0 = int32_to_floatx80(val, &env->fp_status);
168 void helper_flds_ST0(CPUX86State *env, uint32_t val)
170 int new_fpstt;
171 union {
172 float32 f;
173 uint32_t i;
174 } u;
176 new_fpstt = (env->fpstt - 1) & 7;
177 u.i = val;
178 env->fpregs[new_fpstt].d = float32_to_floatx80(u.f, &env->fp_status);
179 env->fpstt = new_fpstt;
180 env->fptags[new_fpstt] = 0; /* validate stack entry */
183 void helper_fldl_ST0(CPUX86State *env, uint64_t val)
185 int new_fpstt;
186 union {
187 float64 f;
188 uint64_t i;
189 } u;
191 new_fpstt = (env->fpstt - 1) & 7;
192 u.i = val;
193 env->fpregs[new_fpstt].d = float64_to_floatx80(u.f, &env->fp_status);
194 env->fpstt = new_fpstt;
195 env->fptags[new_fpstt] = 0; /* validate stack entry */
198 void helper_fildl_ST0(CPUX86State *env, int32_t val)
200 int new_fpstt;
202 new_fpstt = (env->fpstt - 1) & 7;
203 env->fpregs[new_fpstt].d = int32_to_floatx80(val, &env->fp_status);
204 env->fpstt = new_fpstt;
205 env->fptags[new_fpstt] = 0; /* validate stack entry */
208 void helper_fildll_ST0(CPUX86State *env, int64_t val)
210 int new_fpstt;
212 new_fpstt = (env->fpstt - 1) & 7;
213 env->fpregs[new_fpstt].d = int64_to_floatx80(val, &env->fp_status);
214 env->fpstt = new_fpstt;
215 env->fptags[new_fpstt] = 0; /* validate stack entry */
218 uint32_t helper_fsts_ST0(CPUX86State *env)
220 union {
221 float32 f;
222 uint32_t i;
223 } u;
225 u.f = floatx80_to_float32(ST0, &env->fp_status);
226 return u.i;
229 uint64_t helper_fstl_ST0(CPUX86State *env)
231 union {
232 float64 f;
233 uint64_t i;
234 } u;
236 u.f = floatx80_to_float64(ST0, &env->fp_status);
237 return u.i;
240 int32_t helper_fist_ST0(CPUX86State *env)
242 int32_t val;
244 val = floatx80_to_int32(ST0, &env->fp_status);
245 if (val != (int16_t)val) {
246 val = -32768;
248 return val;
251 int32_t helper_fistl_ST0(CPUX86State *env)
253 int32_t val;
254 signed char old_exp_flags;
256 old_exp_flags = get_float_exception_flags(&env->fp_status);
257 set_float_exception_flags(0, &env->fp_status);
259 val = floatx80_to_int32(ST0, &env->fp_status);
260 if (get_float_exception_flags(&env->fp_status) & float_flag_invalid) {
261 val = 0x80000000;
263 set_float_exception_flags(get_float_exception_flags(&env->fp_status)
264 | old_exp_flags, &env->fp_status);
265 return val;
268 int64_t helper_fistll_ST0(CPUX86State *env)
270 int64_t val;
271 signed char old_exp_flags;
273 old_exp_flags = get_float_exception_flags(&env->fp_status);
274 set_float_exception_flags(0, &env->fp_status);
276 val = floatx80_to_int32(ST0, &env->fp_status);
277 if (get_float_exception_flags(&env->fp_status) & float_flag_invalid) {
278 val = 0x8000000000000000ULL;
280 set_float_exception_flags(get_float_exception_flags(&env->fp_status)
281 | old_exp_flags, &env->fp_status);
282 return val;
285 int32_t helper_fistt_ST0(CPUX86State *env)
287 int32_t val;
289 val = floatx80_to_int32_round_to_zero(ST0, &env->fp_status);
290 if (val != (int16_t)val) {
291 val = -32768;
293 return val;
296 int32_t helper_fisttl_ST0(CPUX86State *env)
298 int32_t val;
300 val = floatx80_to_int32_round_to_zero(ST0, &env->fp_status);
301 return val;
304 int64_t helper_fisttll_ST0(CPUX86State *env)
306 int64_t val;
308 val = floatx80_to_int64_round_to_zero(ST0, &env->fp_status);
309 return val;
312 void helper_fldt_ST0(CPUX86State *env, target_ulong ptr)
314 int new_fpstt;
316 new_fpstt = (env->fpstt - 1) & 7;
317 env->fpregs[new_fpstt].d = helper_fldt(env, ptr);
318 env->fpstt = new_fpstt;
319 env->fptags[new_fpstt] = 0; /* validate stack entry */
322 void helper_fstt_ST0(CPUX86State *env, target_ulong ptr)
324 helper_fstt(env, ST0, ptr);
327 void helper_fpush(CPUX86State *env)
329 fpush(env);
332 void helper_fpop(CPUX86State *env)
334 fpop(env);
337 void helper_fdecstp(CPUX86State *env)
339 env->fpstt = (env->fpstt - 1) & 7;
340 env->fpus &= ~0x4700;
343 void helper_fincstp(CPUX86State *env)
345 env->fpstt = (env->fpstt + 1) & 7;
346 env->fpus &= ~0x4700;
349 /* FPU move */
351 void helper_ffree_STN(CPUX86State *env, int st_index)
353 env->fptags[(env->fpstt + st_index) & 7] = 1;
356 void helper_fmov_ST0_FT0(CPUX86State *env)
358 ST0 = FT0;
361 void helper_fmov_FT0_STN(CPUX86State *env, int st_index)
363 FT0 = ST(st_index);
366 void helper_fmov_ST0_STN(CPUX86State *env, int st_index)
368 ST0 = ST(st_index);
371 void helper_fmov_STN_ST0(CPUX86State *env, int st_index)
373 ST(st_index) = ST0;
376 void helper_fxchg_ST0_STN(CPUX86State *env, int st_index)
378 floatx80 tmp;
380 tmp = ST(st_index);
381 ST(st_index) = ST0;
382 ST0 = tmp;
385 /* FPU operations */
387 static const int fcom_ccval[4] = {0x0100, 0x4000, 0x0000, 0x4500};
389 void helper_fcom_ST0_FT0(CPUX86State *env)
391 int ret;
393 ret = floatx80_compare(ST0, FT0, &env->fp_status);
394 env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
397 void helper_fucom_ST0_FT0(CPUX86State *env)
399 int ret;
401 ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
402 env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
405 static const int fcomi_ccval[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
407 void helper_fcomi_ST0_FT0(CPUX86State *env)
409 int eflags;
410 int ret;
412 ret = floatx80_compare(ST0, FT0, &env->fp_status);
413 eflags = cpu_cc_compute_all(env, CC_OP);
414 eflags = (eflags & ~(CC_Z | CC_P | CC_C)) | fcomi_ccval[ret + 1];
415 CC_SRC = eflags;
418 void helper_fucomi_ST0_FT0(CPUX86State *env)
420 int eflags;
421 int ret;
423 ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
424 eflags = cpu_cc_compute_all(env, CC_OP);
425 eflags = (eflags & ~(CC_Z | CC_P | CC_C)) | fcomi_ccval[ret + 1];
426 CC_SRC = eflags;
429 void helper_fadd_ST0_FT0(CPUX86State *env)
431 ST0 = floatx80_add(ST0, FT0, &env->fp_status);
434 void helper_fmul_ST0_FT0(CPUX86State *env)
436 ST0 = floatx80_mul(ST0, FT0, &env->fp_status);
439 void helper_fsub_ST0_FT0(CPUX86State *env)
441 ST0 = floatx80_sub(ST0, FT0, &env->fp_status);
444 void helper_fsubr_ST0_FT0(CPUX86State *env)
446 ST0 = floatx80_sub(FT0, ST0, &env->fp_status);
449 void helper_fdiv_ST0_FT0(CPUX86State *env)
451 ST0 = helper_fdiv(env, ST0, FT0);
454 void helper_fdivr_ST0_FT0(CPUX86State *env)
456 ST0 = helper_fdiv(env, FT0, ST0);
459 /* fp operations between STN and ST0 */
461 void helper_fadd_STN_ST0(CPUX86State *env, int st_index)
463 ST(st_index) = floatx80_add(ST(st_index), ST0, &env->fp_status);
466 void helper_fmul_STN_ST0(CPUX86State *env, int st_index)
468 ST(st_index) = floatx80_mul(ST(st_index), ST0, &env->fp_status);
471 void helper_fsub_STN_ST0(CPUX86State *env, int st_index)
473 ST(st_index) = floatx80_sub(ST(st_index), ST0, &env->fp_status);
476 void helper_fsubr_STN_ST0(CPUX86State *env, int st_index)
478 ST(st_index) = floatx80_sub(ST0, ST(st_index), &env->fp_status);
481 void helper_fdiv_STN_ST0(CPUX86State *env, int st_index)
483 floatx80 *p;
485 p = &ST(st_index);
486 *p = helper_fdiv(env, *p, ST0);
489 void helper_fdivr_STN_ST0(CPUX86State *env, int st_index)
491 floatx80 *p;
493 p = &ST(st_index);
494 *p = helper_fdiv(env, ST0, *p);
497 /* misc FPU operations */
498 void helper_fchs_ST0(CPUX86State *env)
500 ST0 = floatx80_chs(ST0);
503 void helper_fabs_ST0(CPUX86State *env)
505 ST0 = floatx80_abs(ST0);
508 void helper_fld1_ST0(CPUX86State *env)
510 ST0 = floatx80_one;
513 void helper_fldl2t_ST0(CPUX86State *env)
515 ST0 = floatx80_l2t;
518 void helper_fldl2e_ST0(CPUX86State *env)
520 ST0 = floatx80_l2e;
523 void helper_fldpi_ST0(CPUX86State *env)
525 ST0 = floatx80_pi;
528 void helper_fldlg2_ST0(CPUX86State *env)
530 ST0 = floatx80_lg2;
533 void helper_fldln2_ST0(CPUX86State *env)
535 ST0 = floatx80_ln2;
538 void helper_fldz_ST0(CPUX86State *env)
540 ST0 = floatx80_zero;
543 void helper_fldz_FT0(CPUX86State *env)
545 FT0 = floatx80_zero;
548 uint32_t helper_fnstsw(CPUX86State *env)
550 return (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
553 uint32_t helper_fnstcw(CPUX86State *env)
555 return env->fpuc;
558 void update_fp_status(CPUX86State *env)
560 int rnd_type;
562 /* set rounding mode */
563 switch (env->fpuc & FPU_RC_MASK) {
564 default:
565 case FPU_RC_NEAR:
566 rnd_type = float_round_nearest_even;
567 break;
568 case FPU_RC_DOWN:
569 rnd_type = float_round_down;
570 break;
571 case FPU_RC_UP:
572 rnd_type = float_round_up;
573 break;
574 case FPU_RC_CHOP:
575 rnd_type = float_round_to_zero;
576 break;
578 set_float_rounding_mode(rnd_type, &env->fp_status);
579 switch ((env->fpuc >> 8) & 3) {
580 case 0:
581 rnd_type = 32;
582 break;
583 case 2:
584 rnd_type = 64;
585 break;
586 case 3:
587 default:
588 rnd_type = 80;
589 break;
591 set_floatx80_rounding_precision(rnd_type, &env->fp_status);
594 void helper_fldcw(CPUX86State *env, uint32_t val)
596 cpu_set_fpuc(env, val);
599 void helper_fclex(CPUX86State *env)
601 env->fpus &= 0x7f00;
604 void helper_fwait(CPUX86State *env)
606 if (env->fpus & FPUS_SE) {
607 fpu_raise_exception(env);
611 void helper_fninit(CPUX86State *env)
613 env->fpus = 0;
614 env->fpstt = 0;
615 cpu_set_fpuc(env, 0x37f);
616 env->fptags[0] = 1;
617 env->fptags[1] = 1;
618 env->fptags[2] = 1;
619 env->fptags[3] = 1;
620 env->fptags[4] = 1;
621 env->fptags[5] = 1;
622 env->fptags[6] = 1;
623 env->fptags[7] = 1;
626 /* BCD ops */
628 void helper_fbld_ST0(CPUX86State *env, target_ulong ptr)
630 floatx80 tmp;
631 uint64_t val;
632 unsigned int v;
633 int i;
635 val = 0;
636 for (i = 8; i >= 0; i--) {
637 v = cpu_ldub_data(env, ptr + i);
638 val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
640 tmp = int64_to_floatx80(val, &env->fp_status);
641 if (cpu_ldub_data(env, ptr + 9) & 0x80) {
642 tmp = floatx80_chs(tmp);
644 fpush(env);
645 ST0 = tmp;
648 void helper_fbst_ST0(CPUX86State *env, target_ulong ptr)
650 int v;
651 target_ulong mem_ref, mem_end;
652 int64_t val;
654 val = floatx80_to_int64(ST0, &env->fp_status);
655 mem_ref = ptr;
656 mem_end = mem_ref + 9;
657 if (val < 0) {
658 cpu_stb_data(env, mem_end, 0x80);
659 val = -val;
660 } else {
661 cpu_stb_data(env, mem_end, 0x00);
663 while (mem_ref < mem_end) {
664 if (val == 0) {
665 break;
667 v = val % 100;
668 val = val / 100;
669 v = ((v / 10) << 4) | (v % 10);
670 cpu_stb_data(env, mem_ref++, v);
672 while (mem_ref < mem_end) {
673 cpu_stb_data(env, mem_ref++, 0);
677 void helper_f2xm1(CPUX86State *env)
679 double val = floatx80_to_double(env, ST0);
681 val = pow(2.0, val) - 1.0;
682 ST0 = double_to_floatx80(env, val);
685 void helper_fyl2x(CPUX86State *env)
687 double fptemp = floatx80_to_double(env, ST0);
689 if (fptemp > 0.0) {
690 fptemp = log(fptemp) / log(2.0); /* log2(ST) */
691 fptemp *= floatx80_to_double(env, ST1);
692 ST1 = double_to_floatx80(env, fptemp);
693 fpop(env);
694 } else {
695 env->fpus &= ~0x4700;
696 env->fpus |= 0x400;
700 void helper_fptan(CPUX86State *env)
702 double fptemp = floatx80_to_double(env, ST0);
704 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
705 env->fpus |= 0x400;
706 } else {
707 fptemp = tan(fptemp);
708 ST0 = double_to_floatx80(env, fptemp);
709 fpush(env);
710 ST0 = floatx80_one;
711 env->fpus &= ~0x400; /* C2 <-- 0 */
712 /* the above code is for |arg| < 2**52 only */
716 void helper_fpatan(CPUX86State *env)
718 double fptemp, fpsrcop;
720 fpsrcop = floatx80_to_double(env, ST1);
721 fptemp = floatx80_to_double(env, ST0);
722 ST1 = double_to_floatx80(env, atan2(fpsrcop, fptemp));
723 fpop(env);
726 void helper_fxtract(CPUX86State *env)
728 CPU_LDoubleU temp;
730 temp.d = ST0;
732 if (floatx80_is_zero(ST0)) {
733 /* Easy way to generate -inf and raising division by 0 exception */
734 ST0 = floatx80_div(floatx80_chs(floatx80_one), floatx80_zero,
735 &env->fp_status);
736 fpush(env);
737 ST0 = temp.d;
738 } else {
739 int expdif;
741 expdif = EXPD(temp) - EXPBIAS;
742 /* DP exponent bias */
743 ST0 = int32_to_floatx80(expdif, &env->fp_status);
744 fpush(env);
745 BIASEXPONENT(temp);
746 ST0 = temp.d;
750 void helper_fprem1(CPUX86State *env)
752 double st0, st1, dblq, fpsrcop, fptemp;
753 CPU_LDoubleU fpsrcop1, fptemp1;
754 int expdif;
755 signed long long int q;
757 st0 = floatx80_to_double(env, ST0);
758 st1 = floatx80_to_double(env, ST1);
760 if (isinf(st0) || isnan(st0) || isnan(st1) || (st1 == 0.0)) {
761 ST0 = double_to_floatx80(env, 0.0 / 0.0); /* NaN */
762 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
763 return;
766 fpsrcop = st0;
767 fptemp = st1;
768 fpsrcop1.d = ST0;
769 fptemp1.d = ST1;
770 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
772 if (expdif < 0) {
773 /* optimisation? taken from the AMD docs */
774 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
775 /* ST0 is unchanged */
776 return;
779 if (expdif < 53) {
780 dblq = fpsrcop / fptemp;
781 /* round dblq towards nearest integer */
782 dblq = rint(dblq);
783 st0 = fpsrcop - fptemp * dblq;
785 /* convert dblq to q by truncating towards zero */
786 if (dblq < 0.0) {
787 q = (signed long long int)(-dblq);
788 } else {
789 q = (signed long long int)dblq;
792 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
793 /* (C0,C3,C1) <-- (q2,q1,q0) */
794 env->fpus |= (q & 0x4) << (8 - 2); /* (C0) <-- q2 */
795 env->fpus |= (q & 0x2) << (14 - 1); /* (C3) <-- q1 */
796 env->fpus |= (q & 0x1) << (9 - 0); /* (C1) <-- q0 */
797 } else {
798 env->fpus |= 0x400; /* C2 <-- 1 */
799 fptemp = pow(2.0, expdif - 50);
800 fpsrcop = (st0 / st1) / fptemp;
801 /* fpsrcop = integer obtained by chopping */
802 fpsrcop = (fpsrcop < 0.0) ?
803 -(floor(fabs(fpsrcop))) : floor(fpsrcop);
804 st0 -= (st1 * fpsrcop * fptemp);
806 ST0 = double_to_floatx80(env, st0);
809 void helper_fprem(CPUX86State *env)
811 double st0, st1, dblq, fpsrcop, fptemp;
812 CPU_LDoubleU fpsrcop1, fptemp1;
813 int expdif;
814 signed long long int q;
816 st0 = floatx80_to_double(env, ST0);
817 st1 = floatx80_to_double(env, ST1);
819 if (isinf(st0) || isnan(st0) || isnan(st1) || (st1 == 0.0)) {
820 ST0 = double_to_floatx80(env, 0.0 / 0.0); /* NaN */
821 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
822 return;
825 fpsrcop = st0;
826 fptemp = st1;
827 fpsrcop1.d = ST0;
828 fptemp1.d = ST1;
829 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
831 if (expdif < 0) {
832 /* optimisation? taken from the AMD docs */
833 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
834 /* ST0 is unchanged */
835 return;
838 if (expdif < 53) {
839 dblq = fpsrcop / fptemp; /* ST0 / ST1 */
840 /* round dblq towards zero */
841 dblq = (dblq < 0.0) ? ceil(dblq) : floor(dblq);
842 st0 = fpsrcop - fptemp * dblq; /* fpsrcop is ST0 */
844 /* convert dblq to q by truncating towards zero */
845 if (dblq < 0.0) {
846 q = (signed long long int)(-dblq);
847 } else {
848 q = (signed long long int)dblq;
851 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
852 /* (C0,C3,C1) <-- (q2,q1,q0) */
853 env->fpus |= (q & 0x4) << (8 - 2); /* (C0) <-- q2 */
854 env->fpus |= (q & 0x2) << (14 - 1); /* (C3) <-- q1 */
855 env->fpus |= (q & 0x1) << (9 - 0); /* (C1) <-- q0 */
856 } else {
857 int N = 32 + (expdif % 32); /* as per AMD docs */
859 env->fpus |= 0x400; /* C2 <-- 1 */
860 fptemp = pow(2.0, (double)(expdif - N));
861 fpsrcop = (st0 / st1) / fptemp;
862 /* fpsrcop = integer obtained by chopping */
863 fpsrcop = (fpsrcop < 0.0) ?
864 -(floor(fabs(fpsrcop))) : floor(fpsrcop);
865 st0 -= (st1 * fpsrcop * fptemp);
867 ST0 = double_to_floatx80(env, st0);
870 void helper_fyl2xp1(CPUX86State *env)
872 double fptemp = floatx80_to_double(env, ST0);
874 if ((fptemp + 1.0) > 0.0) {
875 fptemp = log(fptemp + 1.0) / log(2.0); /* log2(ST + 1.0) */
876 fptemp *= floatx80_to_double(env, ST1);
877 ST1 = double_to_floatx80(env, fptemp);
878 fpop(env);
879 } else {
880 env->fpus &= ~0x4700;
881 env->fpus |= 0x400;
885 void helper_fsqrt(CPUX86State *env)
887 if (floatx80_is_neg(ST0)) {
888 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
889 env->fpus |= 0x400;
891 ST0 = floatx80_sqrt(ST0, &env->fp_status);
894 void helper_fsincos(CPUX86State *env)
896 double fptemp = floatx80_to_double(env, ST0);
898 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
899 env->fpus |= 0x400;
900 } else {
901 ST0 = double_to_floatx80(env, sin(fptemp));
902 fpush(env);
903 ST0 = double_to_floatx80(env, cos(fptemp));
904 env->fpus &= ~0x400; /* C2 <-- 0 */
905 /* the above code is for |arg| < 2**63 only */
909 void helper_frndint(CPUX86State *env)
911 ST0 = floatx80_round_to_int(ST0, &env->fp_status);
914 void helper_fscale(CPUX86State *env)
916 if (floatx80_is_any_nan(ST1)) {
917 ST0 = ST1;
918 } else {
919 int n = floatx80_to_int32_round_to_zero(ST1, &env->fp_status);
920 ST0 = floatx80_scalbn(ST0, n, &env->fp_status);
924 void helper_fsin(CPUX86State *env)
926 double fptemp = floatx80_to_double(env, ST0);
928 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
929 env->fpus |= 0x400;
930 } else {
931 ST0 = double_to_floatx80(env, sin(fptemp));
932 env->fpus &= ~0x400; /* C2 <-- 0 */
933 /* the above code is for |arg| < 2**53 only */
937 void helper_fcos(CPUX86State *env)
939 double fptemp = floatx80_to_double(env, ST0);
941 if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
942 env->fpus |= 0x400;
943 } else {
944 ST0 = double_to_floatx80(env, cos(fptemp));
945 env->fpus &= ~0x400; /* C2 <-- 0 */
946 /* the above code is for |arg| < 2**63 only */
950 void helper_fxam_ST0(CPUX86State *env)
952 CPU_LDoubleU temp;
953 int expdif;
955 temp.d = ST0;
957 env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
958 if (SIGND(temp)) {
959 env->fpus |= 0x200; /* C1 <-- 1 */
962 /* XXX: test fptags too */
963 expdif = EXPD(temp);
964 if (expdif == MAXEXPD) {
965 if (MANTD(temp) == 0x8000000000000000ULL) {
966 env->fpus |= 0x500; /* Infinity */
967 } else {
968 env->fpus |= 0x100; /* NaN */
970 } else if (expdif == 0) {
971 if (MANTD(temp) == 0) {
972 env->fpus |= 0x4000; /* Zero */
973 } else {
974 env->fpus |= 0x4400; /* Denormal */
976 } else {
977 env->fpus |= 0x400;
981 void helper_fstenv(CPUX86State *env, target_ulong ptr, int data32)
983 int fpus, fptag, exp, i;
984 uint64_t mant;
985 CPU_LDoubleU tmp;
987 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
988 fptag = 0;
989 for (i = 7; i >= 0; i--) {
990 fptag <<= 2;
991 if (env->fptags[i]) {
992 fptag |= 3;
993 } else {
994 tmp.d = env->fpregs[i].d;
995 exp = EXPD(tmp);
996 mant = MANTD(tmp);
997 if (exp == 0 && mant == 0) {
998 /* zero */
999 fptag |= 1;
1000 } else if (exp == 0 || exp == MAXEXPD
1001 || (mant & (1LL << 63)) == 0) {
1002 /* NaNs, infinity, denormal */
1003 fptag |= 2;
1007 if (data32) {
1008 /* 32 bit */
1009 cpu_stl_data(env, ptr, env->fpuc);
1010 cpu_stl_data(env, ptr + 4, fpus);
1011 cpu_stl_data(env, ptr + 8, fptag);
1012 cpu_stl_data(env, ptr + 12, 0); /* fpip */
1013 cpu_stl_data(env, ptr + 16, 0); /* fpcs */
1014 cpu_stl_data(env, ptr + 20, 0); /* fpoo */
1015 cpu_stl_data(env, ptr + 24, 0); /* fpos */
1016 } else {
1017 /* 16 bit */
1018 cpu_stw_data(env, ptr, env->fpuc);
1019 cpu_stw_data(env, ptr + 2, fpus);
1020 cpu_stw_data(env, ptr + 4, fptag);
1021 cpu_stw_data(env, ptr + 6, 0);
1022 cpu_stw_data(env, ptr + 8, 0);
1023 cpu_stw_data(env, ptr + 10, 0);
1024 cpu_stw_data(env, ptr + 12, 0);
1028 void helper_fldenv(CPUX86State *env, target_ulong ptr, int data32)
1030 int i, fpus, fptag;
1032 if (data32) {
1033 cpu_set_fpuc(env, cpu_lduw_data(env, ptr));
1034 fpus = cpu_lduw_data(env, ptr + 4);
1035 fptag = cpu_lduw_data(env, ptr + 8);
1036 } else {
1037 cpu_set_fpuc(env, cpu_lduw_data(env, ptr));
1038 fpus = cpu_lduw_data(env, ptr + 2);
1039 fptag = cpu_lduw_data(env, ptr + 4);
1041 env->fpstt = (fpus >> 11) & 7;
1042 env->fpus = fpus & ~0x3800;
1043 for (i = 0; i < 8; i++) {
1044 env->fptags[i] = ((fptag & 3) == 3);
1045 fptag >>= 2;
1049 void helper_fsave(CPUX86State *env, target_ulong ptr, int data32)
1051 floatx80 tmp;
1052 int i;
1054 helper_fstenv(env, ptr, data32);
1056 ptr += (14 << data32);
1057 for (i = 0; i < 8; i++) {
1058 tmp = ST(i);
1059 helper_fstt(env, tmp, ptr);
1060 ptr += 10;
1063 /* fninit */
1064 env->fpus = 0;
1065 env->fpstt = 0;
1066 cpu_set_fpuc(env, 0x37f);
1067 env->fptags[0] = 1;
1068 env->fptags[1] = 1;
1069 env->fptags[2] = 1;
1070 env->fptags[3] = 1;
1071 env->fptags[4] = 1;
1072 env->fptags[5] = 1;
1073 env->fptags[6] = 1;
1074 env->fptags[7] = 1;
1077 void helper_frstor(CPUX86State *env, target_ulong ptr, int data32)
1079 floatx80 tmp;
1080 int i;
1082 helper_fldenv(env, ptr, data32);
1083 ptr += (14 << data32);
1085 for (i = 0; i < 8; i++) {
1086 tmp = helper_fldt(env, ptr);
1087 ST(i) = tmp;
1088 ptr += 10;
1092 #if defined(CONFIG_USER_ONLY)
1093 void cpu_x86_fsave(CPUX86State *env, target_ulong ptr, int data32)
1095 helper_fsave(env, ptr, data32);
1098 void cpu_x86_frstor(CPUX86State *env, target_ulong ptr, int data32)
1100 helper_frstor(env, ptr, data32);
1102 #endif
1104 void helper_fxsave(CPUX86State *env, target_ulong ptr, int data64)
1106 int fpus, fptag, i, nb_xmm_regs;
1107 floatx80 tmp;
1108 target_ulong addr;
1110 /* The operand must be 16 byte aligned */
1111 if (ptr & 0xf) {
1112 raise_exception(env, EXCP0D_GPF);
1115 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1116 fptag = 0;
1117 for (i = 0; i < 8; i++) {
1118 fptag |= (env->fptags[i] << i);
1120 cpu_stw_data(env, ptr, env->fpuc);
1121 cpu_stw_data(env, ptr + 2, fpus);
1122 cpu_stw_data(env, ptr + 4, fptag ^ 0xff);
1123 #ifdef TARGET_X86_64
1124 if (data64) {
1125 cpu_stq_data(env, ptr + 0x08, 0); /* rip */
1126 cpu_stq_data(env, ptr + 0x10, 0); /* rdp */
1127 } else
1128 #endif
1130 cpu_stl_data(env, ptr + 0x08, 0); /* eip */
1131 cpu_stl_data(env, ptr + 0x0c, 0); /* sel */
1132 cpu_stl_data(env, ptr + 0x10, 0); /* dp */
1133 cpu_stl_data(env, ptr + 0x14, 0); /* sel */
1136 addr = ptr + 0x20;
1137 for (i = 0; i < 8; i++) {
1138 tmp = ST(i);
1139 helper_fstt(env, tmp, addr);
1140 addr += 16;
1143 if (env->cr[4] & CR4_OSFXSR_MASK) {
1144 /* XXX: finish it */
1145 cpu_stl_data(env, ptr + 0x18, env->mxcsr); /* mxcsr */
1146 cpu_stl_data(env, ptr + 0x1c, 0x0000ffff); /* mxcsr_mask */
1147 if (env->hflags & HF_CS64_MASK) {
1148 nb_xmm_regs = 16;
1149 } else {
1150 nb_xmm_regs = 8;
1152 addr = ptr + 0xa0;
1153 /* Fast FXSAVE leaves out the XMM registers */
1154 if (!(env->efer & MSR_EFER_FFXSR)
1155 || (env->hflags & HF_CPL_MASK)
1156 || !(env->hflags & HF_LMA_MASK)) {
1157 for (i = 0; i < nb_xmm_regs; i++) {
1158 cpu_stq_data(env, addr, env->xmm_regs[i].XMM_Q(0));
1159 cpu_stq_data(env, addr + 8, env->xmm_regs[i].XMM_Q(1));
1160 addr += 16;
1166 void helper_fxrstor(CPUX86State *env, target_ulong ptr, int data64)
1168 int i, fpus, fptag, nb_xmm_regs;
1169 floatx80 tmp;
1170 target_ulong addr;
1172 /* The operand must be 16 byte aligned */
1173 if (ptr & 0xf) {
1174 raise_exception(env, EXCP0D_GPF);
1177 cpu_set_fpuc(env, cpu_lduw_data(env, ptr));
1178 fpus = cpu_lduw_data(env, ptr + 2);
1179 fptag = cpu_lduw_data(env, ptr + 4);
1180 env->fpstt = (fpus >> 11) & 7;
1181 env->fpus = fpus & ~0x3800;
1182 fptag ^= 0xff;
1183 for (i = 0; i < 8; i++) {
1184 env->fptags[i] = ((fptag >> i) & 1);
1187 addr = ptr + 0x20;
1188 for (i = 0; i < 8; i++) {
1189 tmp = helper_fldt(env, addr);
1190 ST(i) = tmp;
1191 addr += 16;
1194 if (env->cr[4] & CR4_OSFXSR_MASK) {
1195 /* XXX: finish it */
1196 cpu_set_mxcsr(env, cpu_ldl_data(env, ptr + 0x18));
1197 /* cpu_ldl_data(env, ptr + 0x1c); */
1198 if (env->hflags & HF_CS64_MASK) {
1199 nb_xmm_regs = 16;
1200 } else {
1201 nb_xmm_regs = 8;
1203 addr = ptr + 0xa0;
1204 /* Fast FXRESTORE leaves out the XMM registers */
1205 if (!(env->efer & MSR_EFER_FFXSR)
1206 || (env->hflags & HF_CPL_MASK)
1207 || !(env->hflags & HF_LMA_MASK)) {
1208 for (i = 0; i < nb_xmm_regs; i++) {
1209 env->xmm_regs[i].XMM_Q(0) = cpu_ldq_data(env, addr);
1210 env->xmm_regs[i].XMM_Q(1) = cpu_ldq_data(env, addr + 8);
1211 addr += 16;
1217 void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, floatx80 f)
1219 CPU_LDoubleU temp;
1221 temp.d = f;
1222 *pmant = temp.l.lower;
1223 *pexp = temp.l.upper;
1226 floatx80 cpu_set_fp80(uint64_t mant, uint16_t upper)
1228 CPU_LDoubleU temp;
1230 temp.l.upper = upper;
1231 temp.l.lower = mant;
1232 return temp.d;
1235 /* MMX/SSE */
1236 /* XXX: optimize by storing fptt and fptags in the static cpu state */
1238 #define SSE_DAZ 0x0040
1239 #define SSE_RC_MASK 0x6000
1240 #define SSE_RC_NEAR 0x0000
1241 #define SSE_RC_DOWN 0x2000
1242 #define SSE_RC_UP 0x4000
1243 #define SSE_RC_CHOP 0x6000
1244 #define SSE_FZ 0x8000
1246 void cpu_set_mxcsr(CPUX86State *env, uint32_t mxcsr)
1248 int rnd_type;
1250 env->mxcsr = mxcsr;
1252 /* set rounding mode */
1253 switch (mxcsr & SSE_RC_MASK) {
1254 default:
1255 case SSE_RC_NEAR:
1256 rnd_type = float_round_nearest_even;
1257 break;
1258 case SSE_RC_DOWN:
1259 rnd_type = float_round_down;
1260 break;
1261 case SSE_RC_UP:
1262 rnd_type = float_round_up;
1263 break;
1264 case SSE_RC_CHOP:
1265 rnd_type = float_round_to_zero;
1266 break;
1268 set_float_rounding_mode(rnd_type, &env->sse_status);
1270 /* set denormals are zero */
1271 set_flush_inputs_to_zero((mxcsr & SSE_DAZ) ? 1 : 0, &env->sse_status);
1273 /* set flush to zero */
1274 set_flush_to_zero((mxcsr & SSE_FZ) ? 1 : 0, &env->fp_status);
1277 void cpu_set_fpuc(CPUX86State *env, uint16_t val)
1279 env->fpuc = val;
1280 update_fp_status(env);
1283 void helper_ldmxcsr(CPUX86State *env, uint32_t val)
1285 cpu_set_mxcsr(env, val);
1288 void helper_enter_mmx(CPUX86State *env)
1290 env->fpstt = 0;
1291 *(uint32_t *)(env->fptags) = 0;
1292 *(uint32_t *)(env->fptags + 4) = 0;
1295 void helper_emms(CPUX86State *env)
1297 /* set to empty state */
1298 *(uint32_t *)(env->fptags) = 0x01010101;
1299 *(uint32_t *)(env->fptags + 4) = 0x01010101;
1302 /* XXX: suppress */
1303 void helper_movq(CPUX86State *env, void *d, void *s)
1305 *(uint64_t *)d = *(uint64_t *)s;
1308 #define SHIFT 0
1309 #include "ops_sse.h"
1311 #define SHIFT 1
1312 #include "ops_sse.h"